18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * printer.c -- Printer gadget driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2003-2005 David Brownell
68c2ecf20Sopenharmony_ci * Copyright (C) 2006 Craig W. Nadler
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <asm/byteorder.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/usb/ch9.h>
148c2ecf20Sopenharmony_ci#include <linux/usb/composite.h>
158c2ecf20Sopenharmony_ci#include <linux/usb/gadget.h>
168c2ecf20Sopenharmony_ci#include <linux/usb/g_printer.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ciUSB_GADGET_COMPOSITE_OPTIONS();
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define DRIVER_DESC		"Printer Gadget"
218c2ecf20Sopenharmony_ci#define DRIVER_VERSION		"2015 FEB 17"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic const char shortname [] = "printer";
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include "u_printer.h"
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
308c2ecf20Sopenharmony_ci * Instead:  allocate your own, using normal USB-IF procedures.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* Thanks to NetChip Technologies for donating this product ID.
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ci#define PRINTER_VENDOR_NUM	0x0525		/* NetChip */
368c2ecf20Sopenharmony_ci#define PRINTER_PRODUCT_NUM	0xa4a8		/* Linux-USB Printer Gadget */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/* Some systems will want different product identifiers published in the
398c2ecf20Sopenharmony_ci * device descriptor, either numbers or strings or both.  These string
408c2ecf20Sopenharmony_ci * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cimodule_param_named(iSerialNum, coverwrite.serial_number, charp, S_IRUGO);
448c2ecf20Sopenharmony_ciMODULE_PARM_DESC(iSerialNum, "1");
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic char *iPNPstring;
478c2ecf20Sopenharmony_cimodule_param(iPNPstring, charp, S_IRUGO);
488c2ecf20Sopenharmony_ciMODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/* Number of requests to allocate per endpoint, not used for ep0. */
518c2ecf20Sopenharmony_cistatic unsigned qlen = 10;
528c2ecf20Sopenharmony_cimodule_param(qlen, uint, S_IRUGO|S_IWUSR);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define QLEN	qlen
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic struct usb_function_instance *fi_printer;
578c2ecf20Sopenharmony_cistatic struct usb_function *f_printer;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/*
628c2ecf20Sopenharmony_ci * DESCRIPTORS ... most are static, but strings and (full) configuration
638c2ecf20Sopenharmony_ci * descriptors are built on demand.
648c2ecf20Sopenharmony_ci */
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic struct usb_device_descriptor device_desc = {
678c2ecf20Sopenharmony_ci	.bLength =		sizeof device_desc,
688c2ecf20Sopenharmony_ci	.bDescriptorType =	USB_DT_DEVICE,
698c2ecf20Sopenharmony_ci	/* .bcdUSB = DYNAMIC */
708c2ecf20Sopenharmony_ci	.bDeviceClass =		USB_CLASS_PER_INTERFACE,
718c2ecf20Sopenharmony_ci	.bDeviceSubClass =	0,
728c2ecf20Sopenharmony_ci	.bDeviceProtocol =	0,
738c2ecf20Sopenharmony_ci	.idVendor =		cpu_to_le16(PRINTER_VENDOR_NUM),
748c2ecf20Sopenharmony_ci	.idProduct =		cpu_to_le16(PRINTER_PRODUCT_NUM),
758c2ecf20Sopenharmony_ci	.bNumConfigurations =	1
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic const struct usb_descriptor_header *otg_desc[2];
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/* descriptors that are built on-demand */
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic char				product_desc [40] = DRIVER_DESC;
858c2ecf20Sopenharmony_cistatic char				serial_num [40] = "1";
868c2ecf20Sopenharmony_cistatic char				*pnp_string =
878c2ecf20Sopenharmony_ci	"MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/* static strings, in UTF-8 */
908c2ecf20Sopenharmony_cistatic struct usb_string		strings [] = {
918c2ecf20Sopenharmony_ci	[USB_GADGET_MANUFACTURER_IDX].s = "",
928c2ecf20Sopenharmony_ci	[USB_GADGET_PRODUCT_IDX].s = product_desc,
938c2ecf20Sopenharmony_ci	[USB_GADGET_SERIAL_IDX].s =	serial_num,
948c2ecf20Sopenharmony_ci	{  }		/* end of list */
958c2ecf20Sopenharmony_ci};
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic struct usb_gadget_strings	stringtab_dev = {
988c2ecf20Sopenharmony_ci	.language	= 0x0409,	/* en-us */
998c2ecf20Sopenharmony_ci	.strings	= strings,
1008c2ecf20Sopenharmony_ci};
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic struct usb_gadget_strings *dev_strings[] = {
1038c2ecf20Sopenharmony_ci	&stringtab_dev,
1048c2ecf20Sopenharmony_ci	NULL,
1058c2ecf20Sopenharmony_ci};
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic struct usb_configuration printer_cfg_driver = {
1088c2ecf20Sopenharmony_ci	.label			= "printer",
1098c2ecf20Sopenharmony_ci	.bConfigurationValue	= 1,
1108c2ecf20Sopenharmony_ci	.bmAttributes		= USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic int printer_do_config(struct usb_configuration *c)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	struct usb_gadget	*gadget = c->cdev->gadget;
1168c2ecf20Sopenharmony_ci	int			status = 0;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	usb_ep_autoconfig_reset(gadget);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	usb_gadget_set_selfpowered(gadget);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	if (gadget_is_otg(gadget)) {
1238c2ecf20Sopenharmony_ci		printer_cfg_driver.descriptors = otg_desc;
1248c2ecf20Sopenharmony_ci		printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1258c2ecf20Sopenharmony_ci	}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	f_printer = usb_get_function(fi_printer);
1288c2ecf20Sopenharmony_ci	if (IS_ERR(f_printer))
1298c2ecf20Sopenharmony_ci		return PTR_ERR(f_printer);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	status = usb_add_function(c, f_printer);
1328c2ecf20Sopenharmony_ci	if (status < 0)
1338c2ecf20Sopenharmony_ci		usb_put_function(f_printer);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	return status;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic int printer_bind(struct usb_composite_dev *cdev)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	struct f_printer_opts *opts;
1418c2ecf20Sopenharmony_ci	int ret;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	fi_printer = usb_get_function_instance("printer");
1448c2ecf20Sopenharmony_ci	if (IS_ERR(fi_printer))
1458c2ecf20Sopenharmony_ci		return PTR_ERR(fi_printer);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	opts = container_of(fi_printer, struct f_printer_opts, func_inst);
1488c2ecf20Sopenharmony_ci	opts->minor = 0;
1498c2ecf20Sopenharmony_ci	opts->q_len = QLEN;
1508c2ecf20Sopenharmony_ci	if (iPNPstring) {
1518c2ecf20Sopenharmony_ci		opts->pnp_string = kstrdup(iPNPstring, GFP_KERNEL);
1528c2ecf20Sopenharmony_ci		if (!opts->pnp_string) {
1538c2ecf20Sopenharmony_ci			ret = -ENOMEM;
1548c2ecf20Sopenharmony_ci			goto fail_put_func_inst;
1558c2ecf20Sopenharmony_ci		}
1568c2ecf20Sopenharmony_ci		opts->pnp_string_allocated = true;
1578c2ecf20Sopenharmony_ci		/*
1588c2ecf20Sopenharmony_ci		 * we don't free this memory in case of error
1598c2ecf20Sopenharmony_ci		 * as printer cleanup func will do this for us
1608c2ecf20Sopenharmony_ci		 */
1618c2ecf20Sopenharmony_ci	} else {
1628c2ecf20Sopenharmony_ci		opts->pnp_string = pnp_string;
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	ret = usb_string_ids_tab(cdev, strings);
1668c2ecf20Sopenharmony_ci	if (ret < 0)
1678c2ecf20Sopenharmony_ci		goto fail_put_func_inst;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id;
1708c2ecf20Sopenharmony_ci	device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id;
1718c2ecf20Sopenharmony_ci	device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
1748c2ecf20Sopenharmony_ci		struct usb_descriptor_header *usb_desc;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci		usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
1778c2ecf20Sopenharmony_ci		if (!usb_desc) {
1788c2ecf20Sopenharmony_ci			ret = -ENOMEM;
1798c2ecf20Sopenharmony_ci			goto fail_put_func_inst;
1808c2ecf20Sopenharmony_ci		}
1818c2ecf20Sopenharmony_ci		usb_otg_descriptor_init(cdev->gadget, usb_desc);
1828c2ecf20Sopenharmony_ci		otg_desc[0] = usb_desc;
1838c2ecf20Sopenharmony_ci		otg_desc[1] = NULL;
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	ret = usb_add_config(cdev, &printer_cfg_driver, printer_do_config);
1878c2ecf20Sopenharmony_ci	if (ret)
1888c2ecf20Sopenharmony_ci		goto fail_free_otg_desc;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	usb_composite_overwrite_options(cdev, &coverwrite);
1918c2ecf20Sopenharmony_ci	return ret;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cifail_free_otg_desc:
1948c2ecf20Sopenharmony_ci	kfree(otg_desc[0]);
1958c2ecf20Sopenharmony_ci	otg_desc[0] = NULL;
1968c2ecf20Sopenharmony_cifail_put_func_inst:
1978c2ecf20Sopenharmony_ci	usb_put_function_instance(fi_printer);
1988c2ecf20Sopenharmony_ci	return ret;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic int printer_unbind(struct usb_composite_dev *cdev)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	usb_put_function(f_printer);
2048c2ecf20Sopenharmony_ci	usb_put_function_instance(fi_printer);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	kfree(otg_desc[0]);
2078c2ecf20Sopenharmony_ci	otg_desc[0] = NULL;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	return 0;
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic struct usb_composite_driver printer_driver = {
2138c2ecf20Sopenharmony_ci	.name           = shortname,
2148c2ecf20Sopenharmony_ci	.dev            = &device_desc,
2158c2ecf20Sopenharmony_ci	.strings        = dev_strings,
2168c2ecf20Sopenharmony_ci	.max_speed      = USB_SPEED_SUPER,
2178c2ecf20Sopenharmony_ci	.bind		= printer_bind,
2188c2ecf20Sopenharmony_ci	.unbind		= printer_unbind,
2198c2ecf20Sopenharmony_ci};
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_cimodule_usb_composite_driver(printer_driver);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
2248c2ecf20Sopenharmony_ciMODULE_AUTHOR("Craig Nadler");
2258c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
226