xref: /linux/drivers/usb/gadget/legacy/printer.c (revision 9fe3c93f)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
28443f2d2SAndrzej Pietrasiewicz /*
38443f2d2SAndrzej Pietrasiewicz  * printer.c -- Printer gadget driver
48443f2d2SAndrzej Pietrasiewicz  *
58443f2d2SAndrzej Pietrasiewicz  * Copyright (C) 2003-2005 David Brownell
68443f2d2SAndrzej Pietrasiewicz  * Copyright (C) 2006 Craig W. Nadler
78443f2d2SAndrzej Pietrasiewicz  */
88443f2d2SAndrzej Pietrasiewicz 
98443f2d2SAndrzej Pietrasiewicz #include <linux/module.h>
108443f2d2SAndrzej Pietrasiewicz #include <linux/kernel.h>
118443f2d2SAndrzej Pietrasiewicz #include <asm/byteorder.h>
128443f2d2SAndrzej Pietrasiewicz 
138443f2d2SAndrzej Pietrasiewicz #include <linux/usb/ch9.h>
148443f2d2SAndrzej Pietrasiewicz #include <linux/usb/composite.h>
158443f2d2SAndrzej Pietrasiewicz #include <linux/usb/gadget.h>
168443f2d2SAndrzej Pietrasiewicz #include <linux/usb/g_printer.h>
178443f2d2SAndrzej Pietrasiewicz 
188443f2d2SAndrzej Pietrasiewicz USB_GADGET_COMPOSITE_OPTIONS();
198443f2d2SAndrzej Pietrasiewicz 
208443f2d2SAndrzej Pietrasiewicz #define DRIVER_DESC		"Printer Gadget"
21b185f01aSAndrzej Pietrasiewicz #define DRIVER_VERSION		"2015 FEB 17"
226dd8c2e6SAndrzej Pietrasiewicz 
238443f2d2SAndrzej Pietrasiewicz static const char shortname [] = "printer";
248443f2d2SAndrzej Pietrasiewicz 
2569504f80SAndrzej Pietrasiewicz #include "u_printer.h"
26143d53e1SAndrzej Pietrasiewicz 
278443f2d2SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
288443f2d2SAndrzej Pietrasiewicz 
298443f2d2SAndrzej Pietrasiewicz /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
308443f2d2SAndrzej Pietrasiewicz  * Instead:  allocate your own, using normal USB-IF procedures.
318443f2d2SAndrzej Pietrasiewicz  */
328443f2d2SAndrzej Pietrasiewicz 
338443f2d2SAndrzej Pietrasiewicz /* Thanks to NetChip Technologies for donating this product ID.
348443f2d2SAndrzej Pietrasiewicz  */
358443f2d2SAndrzej Pietrasiewicz #define PRINTER_VENDOR_NUM	0x0525		/* NetChip */
368443f2d2SAndrzej Pietrasiewicz #define PRINTER_PRODUCT_NUM	0xa4a8		/* Linux-USB Printer Gadget */
378443f2d2SAndrzej Pietrasiewicz 
388443f2d2SAndrzej Pietrasiewicz /* Some systems will want different product identifiers published in the
398443f2d2SAndrzej Pietrasiewicz  * device descriptor, either numbers or strings or both.  These string
408443f2d2SAndrzej Pietrasiewicz  * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
418443f2d2SAndrzej Pietrasiewicz  */
428443f2d2SAndrzej Pietrasiewicz 
438443f2d2SAndrzej Pietrasiewicz module_param_named(iSerialNum, coverwrite.serial_number, charp, S_IRUGO);
448443f2d2SAndrzej Pietrasiewicz MODULE_PARM_DESC(iSerialNum, "1");
458443f2d2SAndrzej Pietrasiewicz 
468443f2d2SAndrzej Pietrasiewicz static char *iPNPstring;
478443f2d2SAndrzej Pietrasiewicz module_param(iPNPstring, charp, S_IRUGO);
488443f2d2SAndrzej Pietrasiewicz MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
498443f2d2SAndrzej Pietrasiewicz 
508443f2d2SAndrzej Pietrasiewicz /* Number of requests to allocate per endpoint, not used for ep0. */
518443f2d2SAndrzej Pietrasiewicz static unsigned qlen = 10;
528443f2d2SAndrzej Pietrasiewicz module_param(qlen, uint, S_IRUGO|S_IWUSR);
53*9fe3c93fSWei Ming Chen MODULE_PARM_DESC(qlen, "The number of 8k buffers to use per endpoint");
548443f2d2SAndrzej Pietrasiewicz 
558443f2d2SAndrzej Pietrasiewicz #define QLEN	qlen
568443f2d2SAndrzej Pietrasiewicz 
5769504f80SAndrzej Pietrasiewicz static struct usb_function_instance *fi_printer;
5869504f80SAndrzej Pietrasiewicz static struct usb_function *f_printer;
5969504f80SAndrzej Pietrasiewicz 
608443f2d2SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
618443f2d2SAndrzej Pietrasiewicz 
628443f2d2SAndrzej Pietrasiewicz /*
638443f2d2SAndrzej Pietrasiewicz  * DESCRIPTORS ... most are static, but strings and (full) configuration
648443f2d2SAndrzej Pietrasiewicz  * descriptors are built on demand.
658443f2d2SAndrzej Pietrasiewicz  */
668443f2d2SAndrzej Pietrasiewicz 
678443f2d2SAndrzej Pietrasiewicz static struct usb_device_descriptor device_desc = {
688443f2d2SAndrzej Pietrasiewicz 	.bLength =		sizeof device_desc,
698443f2d2SAndrzej Pietrasiewicz 	.bDescriptorType =	USB_DT_DEVICE,
700aecfc1bSIgor Kotrasinski 	/* .bcdUSB = DYNAMIC */
718443f2d2SAndrzej Pietrasiewicz 	.bDeviceClass =		USB_CLASS_PER_INTERFACE,
728443f2d2SAndrzej Pietrasiewicz 	.bDeviceSubClass =	0,
738443f2d2SAndrzej Pietrasiewicz 	.bDeviceProtocol =	0,
748443f2d2SAndrzej Pietrasiewicz 	.idVendor =		cpu_to_le16(PRINTER_VENDOR_NUM),
758443f2d2SAndrzej Pietrasiewicz 	.idProduct =		cpu_to_le16(PRINTER_PRODUCT_NUM),
768443f2d2SAndrzej Pietrasiewicz 	.bNumConfigurations =	1
778443f2d2SAndrzej Pietrasiewicz };
788443f2d2SAndrzej Pietrasiewicz 
7993d39afdSLi Jun static const struct usb_descriptor_header *otg_desc[2];
808443f2d2SAndrzej Pietrasiewicz 
818443f2d2SAndrzej Pietrasiewicz /*-------------------------------------------------------------------------*/
828443f2d2SAndrzej Pietrasiewicz 
838443f2d2SAndrzej Pietrasiewicz /* descriptors that are built on-demand */
848443f2d2SAndrzej Pietrasiewicz 
858443f2d2SAndrzej Pietrasiewicz static char				product_desc [40] = DRIVER_DESC;
868443f2d2SAndrzej Pietrasiewicz static char				serial_num [40] = "1";
87fdc01cc2SKrzysztof Opasiak static char				*pnp_string =
88fdc01cc2SKrzysztof Opasiak 	"MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
898443f2d2SAndrzej Pietrasiewicz 
908443f2d2SAndrzej Pietrasiewicz /* static strings, in UTF-8 */
918443f2d2SAndrzej Pietrasiewicz static struct usb_string		strings [] = {
928443f2d2SAndrzej Pietrasiewicz 	[USB_GADGET_MANUFACTURER_IDX].s = "",
938443f2d2SAndrzej Pietrasiewicz 	[USB_GADGET_PRODUCT_IDX].s = product_desc,
948443f2d2SAndrzej Pietrasiewicz 	[USB_GADGET_SERIAL_IDX].s =	serial_num,
958443f2d2SAndrzej Pietrasiewicz 	{  }		/* end of list */
968443f2d2SAndrzej Pietrasiewicz };
978443f2d2SAndrzej Pietrasiewicz 
988443f2d2SAndrzej Pietrasiewicz static struct usb_gadget_strings	stringtab_dev = {
998443f2d2SAndrzej Pietrasiewicz 	.language	= 0x0409,	/* en-us */
1008443f2d2SAndrzej Pietrasiewicz 	.strings	= strings,
1018443f2d2SAndrzej Pietrasiewicz };
1028443f2d2SAndrzej Pietrasiewicz 
1038443f2d2SAndrzej Pietrasiewicz static struct usb_gadget_strings *dev_strings[] = {
1048443f2d2SAndrzej Pietrasiewicz 	&stringtab_dev,
1058443f2d2SAndrzej Pietrasiewicz 	NULL,
1068443f2d2SAndrzej Pietrasiewicz };
1078443f2d2SAndrzej Pietrasiewicz 
1088443f2d2SAndrzej Pietrasiewicz static struct usb_configuration printer_cfg_driver = {
1098443f2d2SAndrzej Pietrasiewicz 	.label			= "printer",
1108443f2d2SAndrzej Pietrasiewicz 	.bConfigurationValue	= 1,
1118443f2d2SAndrzej Pietrasiewicz 	.bmAttributes		= USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
1128443f2d2SAndrzej Pietrasiewicz };
1138443f2d2SAndrzej Pietrasiewicz 
printer_do_config(struct usb_configuration * c)114c94e289fSArnd Bergmann static int printer_do_config(struct usb_configuration *c)
115ae2dd0deSAndrzej Pietrasiewicz {
116ae2dd0deSAndrzej Pietrasiewicz 	struct usb_gadget	*gadget = c->cdev->gadget;
11769504f80SAndrzej Pietrasiewicz 	int			status = 0;
118ae2dd0deSAndrzej Pietrasiewicz 
119ae2dd0deSAndrzej Pietrasiewicz 	usb_ep_autoconfig_reset(gadget);
120ae2dd0deSAndrzej Pietrasiewicz 
121ae2dd0deSAndrzej Pietrasiewicz 	usb_gadget_set_selfpowered(gadget);
122ae2dd0deSAndrzej Pietrasiewicz 
123ae2dd0deSAndrzej Pietrasiewicz 	if (gadget_is_otg(gadget)) {
124ae2dd0deSAndrzej Pietrasiewicz 		printer_cfg_driver.descriptors = otg_desc;
125ae2dd0deSAndrzej Pietrasiewicz 		printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
126ae2dd0deSAndrzej Pietrasiewicz 	}
127ae2dd0deSAndrzej Pietrasiewicz 
12869504f80SAndrzej Pietrasiewicz 	f_printer = usb_get_function(fi_printer);
12969504f80SAndrzej Pietrasiewicz 	if (IS_ERR(f_printer))
13069504f80SAndrzej Pietrasiewicz 		return PTR_ERR(f_printer);
13169504f80SAndrzej Pietrasiewicz 
13269504f80SAndrzej Pietrasiewicz 	status = usb_add_function(c, f_printer);
13369504f80SAndrzej Pietrasiewicz 	if (status < 0)
13469504f80SAndrzej Pietrasiewicz 		usb_put_function(f_printer);
13569504f80SAndrzej Pietrasiewicz 
13669504f80SAndrzej Pietrasiewicz 	return status;
137ae2dd0deSAndrzej Pietrasiewicz }
138ae2dd0deSAndrzej Pietrasiewicz 
printer_bind(struct usb_composite_dev * cdev)139c94e289fSArnd Bergmann static int printer_bind(struct usb_composite_dev *cdev)
1408443f2d2SAndrzej Pietrasiewicz {
14169504f80SAndrzej Pietrasiewicz 	struct f_printer_opts *opts;
142fdc01cc2SKrzysztof Opasiak 	int ret;
1438443f2d2SAndrzej Pietrasiewicz 
14469504f80SAndrzej Pietrasiewicz 	fi_printer = usb_get_function_instance("printer");
14569504f80SAndrzej Pietrasiewicz 	if (IS_ERR(fi_printer))
14669504f80SAndrzej Pietrasiewicz 		return PTR_ERR(fi_printer);
14769504f80SAndrzej Pietrasiewicz 
14869504f80SAndrzej Pietrasiewicz 	opts = container_of(fi_printer, struct f_printer_opts, func_inst);
14969504f80SAndrzej Pietrasiewicz 	opts->minor = 0;
15069504f80SAndrzej Pietrasiewicz 	opts->q_len = QLEN;
151fdc01cc2SKrzysztof Opasiak 	if (iPNPstring) {
152fdc01cc2SKrzysztof Opasiak 		opts->pnp_string = kstrdup(iPNPstring, GFP_KERNEL);
153fdc01cc2SKrzysztof Opasiak 		if (!opts->pnp_string) {
154fdc01cc2SKrzysztof Opasiak 			ret = -ENOMEM;
155fdc01cc2SKrzysztof Opasiak 			goto fail_put_func_inst;
156fdc01cc2SKrzysztof Opasiak 		}
157fdc01cc2SKrzysztof Opasiak 		opts->pnp_string_allocated = true;
158fdc01cc2SKrzysztof Opasiak 		/*
159fdc01cc2SKrzysztof Opasiak 		 * we don't free this memory in case of error
160fdc01cc2SKrzysztof Opasiak 		 * as printer cleanup func will do this for us
161fdc01cc2SKrzysztof Opasiak 		 */
162fdc01cc2SKrzysztof Opasiak 	} else {
163fdc01cc2SKrzysztof Opasiak 		opts->pnp_string = pnp_string;
164fdc01cc2SKrzysztof Opasiak 	}
165a844715dSAndrzej Pietrasiewicz 
166a844715dSAndrzej Pietrasiewicz 	ret = usb_string_ids_tab(cdev, strings);
16793d39afdSLi Jun 	if (ret < 0)
16893d39afdSLi Jun 		goto fail_put_func_inst;
16993d39afdSLi Jun 
1708443f2d2SAndrzej Pietrasiewicz 	device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id;
1718443f2d2SAndrzej Pietrasiewicz 	device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id;
1728443f2d2SAndrzej Pietrasiewicz 	device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id;
1738443f2d2SAndrzej Pietrasiewicz 
17493d39afdSLi Jun 	if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
17593d39afdSLi Jun 		struct usb_descriptor_header *usb_desc;
17693d39afdSLi Jun 
17793d39afdSLi Jun 		usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
17893d39afdSLi Jun 		if (!usb_desc) {
17993d39afdSLi Jun 			ret = -ENOMEM;
18093d39afdSLi Jun 			goto fail_put_func_inst;
181a844715dSAndrzej Pietrasiewicz 		}
18293d39afdSLi Jun 		usb_otg_descriptor_init(cdev->gadget, usb_desc);
18393d39afdSLi Jun 		otg_desc[0] = usb_desc;
18493d39afdSLi Jun 		otg_desc[1] = NULL;
18593d39afdSLi Jun 	}
18693d39afdSLi Jun 
18793d39afdSLi Jun 	ret = usb_add_config(cdev, &printer_cfg_driver, printer_do_config);
18893d39afdSLi Jun 	if (ret)
18993d39afdSLi Jun 		goto fail_free_otg_desc;
19093d39afdSLi Jun 
1918443f2d2SAndrzej Pietrasiewicz 	usb_composite_overwrite_options(cdev, &coverwrite);
1928443f2d2SAndrzej Pietrasiewicz 	return ret;
19393d39afdSLi Jun 
19493d39afdSLi Jun fail_free_otg_desc:
19593d39afdSLi Jun 	kfree(otg_desc[0]);
19693d39afdSLi Jun 	otg_desc[0] = NULL;
19793d39afdSLi Jun fail_put_func_inst:
19893d39afdSLi Jun 	usb_put_function_instance(fi_printer);
19993d39afdSLi Jun 	return ret;
2008443f2d2SAndrzej Pietrasiewicz }
2018443f2d2SAndrzej Pietrasiewicz 
printer_unbind(struct usb_composite_dev * cdev)202c94e289fSArnd Bergmann static int printer_unbind(struct usb_composite_dev *cdev)
203a844715dSAndrzej Pietrasiewicz {
20469504f80SAndrzej Pietrasiewicz 	usb_put_function(f_printer);
20569504f80SAndrzej Pietrasiewicz 	usb_put_function_instance(fi_printer);
20669504f80SAndrzej Pietrasiewicz 
20793d39afdSLi Jun 	kfree(otg_desc[0]);
20893d39afdSLi Jun 	otg_desc[0] = NULL;
20993d39afdSLi Jun 
210a844715dSAndrzej Pietrasiewicz 	return 0;
211a844715dSAndrzej Pietrasiewicz }
212a844715dSAndrzej Pietrasiewicz 
213c94e289fSArnd Bergmann static struct usb_composite_driver printer_driver = {
2148443f2d2SAndrzej Pietrasiewicz 	.name           = shortname,
2158443f2d2SAndrzej Pietrasiewicz 	.dev            = &device_desc,
2168443f2d2SAndrzej Pietrasiewicz 	.strings        = dev_strings,
21774df41b4SJorge Ramirez-Ortiz 	.max_speed      = USB_SPEED_SUPER,
2188443f2d2SAndrzej Pietrasiewicz 	.bind		= printer_bind,
219a844715dSAndrzej Pietrasiewicz 	.unbind		= printer_unbind,
2208443f2d2SAndrzej Pietrasiewicz };
2218443f2d2SAndrzej Pietrasiewicz 
222a2a8e48aSAndrzej Pietrasiewicz module_usb_composite_driver(printer_driver);
2238443f2d2SAndrzej Pietrasiewicz 
2248443f2d2SAndrzej Pietrasiewicz MODULE_DESCRIPTION(DRIVER_DESC);
2258443f2d2SAndrzej Pietrasiewicz MODULE_AUTHOR("Craig Nadler");
2268443f2d2SAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
227