xref: /linux/drivers/usb/gadget/legacy/printer.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * printer.c -- Printer gadget driver
4  *
5  * Copyright (C) 2003-2005 David Brownell
6  * Copyright (C) 2006 Craig W. Nadler
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <asm/byteorder.h>
17 
18 #include <linux/usb/ch9.h>
19 #include <linux/usb/composite.h>
20 #include <linux/usb/gadget.h>
21 #include <linux/usb/g_printer.h>
22 
23 USB_GADGET_COMPOSITE_OPTIONS();
24 
25 #define DRIVER_DESC		"Printer Gadget"
26 #define DRIVER_VERSION		"2015 FEB 17"
27 
28 static const char shortname [] = "printer";
29 static const char driver_desc [] = DRIVER_DESC;
30 
31 #include "u_printer.h"
32 
33 /*-------------------------------------------------------------------------*/
34 
35 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
36  * Instead:  allocate your own, using normal USB-IF procedures.
37  */
38 
39 /* Thanks to NetChip Technologies for donating this product ID.
40  */
41 #define PRINTER_VENDOR_NUM	0x0525		/* NetChip */
42 #define PRINTER_PRODUCT_NUM	0xa4a8		/* Linux-USB Printer Gadget */
43 
44 /* Some systems will want different product identifiers published in the
45  * device descriptor, either numbers or strings or both.  These string
46  * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
47  */
48 
49 module_param_named(iSerialNum, coverwrite.serial_number, charp, S_IRUGO);
50 MODULE_PARM_DESC(iSerialNum, "1");
51 
52 static char *iPNPstring;
53 module_param(iPNPstring, charp, S_IRUGO);
54 MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
55 
56 /* Number of requests to allocate per endpoint, not used for ep0. */
57 static unsigned qlen = 10;
58 module_param(qlen, uint, S_IRUGO|S_IWUSR);
59 
60 #define QLEN	qlen
61 
62 static struct usb_function_instance *fi_printer;
63 static struct usb_function *f_printer;
64 
65 /*-------------------------------------------------------------------------*/
66 
67 /*
68  * DESCRIPTORS ... most are static, but strings and (full) configuration
69  * descriptors are built on demand.
70  */
71 
72 static struct usb_device_descriptor device_desc = {
73 	.bLength =		sizeof device_desc,
74 	.bDescriptorType =	USB_DT_DEVICE,
75 	/* .bcdUSB = DYNAMIC */
76 	.bDeviceClass =		USB_CLASS_PER_INTERFACE,
77 	.bDeviceSubClass =	0,
78 	.bDeviceProtocol =	0,
79 	.idVendor =		cpu_to_le16(PRINTER_VENDOR_NUM),
80 	.idProduct =		cpu_to_le16(PRINTER_PRODUCT_NUM),
81 	.bNumConfigurations =	1
82 };
83 
84 static const struct usb_descriptor_header *otg_desc[2];
85 
86 /*-------------------------------------------------------------------------*/
87 
88 /* descriptors that are built on-demand */
89 
90 static char				product_desc [40] = DRIVER_DESC;
91 static char				serial_num [40] = "1";
92 static char				*pnp_string =
93 	"MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
94 
95 /* static strings, in UTF-8 */
96 static struct usb_string		strings [] = {
97 	[USB_GADGET_MANUFACTURER_IDX].s = "",
98 	[USB_GADGET_PRODUCT_IDX].s = product_desc,
99 	[USB_GADGET_SERIAL_IDX].s =	serial_num,
100 	{  }		/* end of list */
101 };
102 
103 static struct usb_gadget_strings	stringtab_dev = {
104 	.language	= 0x0409,	/* en-us */
105 	.strings	= strings,
106 };
107 
108 static struct usb_gadget_strings *dev_strings[] = {
109 	&stringtab_dev,
110 	NULL,
111 };
112 
113 static struct usb_configuration printer_cfg_driver = {
114 	.label			= "printer",
115 	.bConfigurationValue	= 1,
116 	.bmAttributes		= USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
117 };
118 
119 static int printer_do_config(struct usb_configuration *c)
120 {
121 	struct usb_gadget	*gadget = c->cdev->gadget;
122 	int			status = 0;
123 
124 	usb_ep_autoconfig_reset(gadget);
125 
126 	usb_gadget_set_selfpowered(gadget);
127 
128 	if (gadget_is_otg(gadget)) {
129 		printer_cfg_driver.descriptors = otg_desc;
130 		printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
131 	}
132 
133 	f_printer = usb_get_function(fi_printer);
134 	if (IS_ERR(f_printer))
135 		return PTR_ERR(f_printer);
136 
137 	status = usb_add_function(c, f_printer);
138 	if (status < 0)
139 		usb_put_function(f_printer);
140 
141 	return status;
142 }
143 
144 static int printer_bind(struct usb_composite_dev *cdev)
145 {
146 	struct f_printer_opts *opts;
147 	int ret;
148 
149 	fi_printer = usb_get_function_instance("printer");
150 	if (IS_ERR(fi_printer))
151 		return PTR_ERR(fi_printer);
152 
153 	opts = container_of(fi_printer, struct f_printer_opts, func_inst);
154 	opts->minor = 0;
155 	opts->q_len = QLEN;
156 	if (iPNPstring) {
157 		opts->pnp_string = kstrdup(iPNPstring, GFP_KERNEL);
158 		if (!opts->pnp_string) {
159 			ret = -ENOMEM;
160 			goto fail_put_func_inst;
161 		}
162 		opts->pnp_string_allocated = true;
163 		/*
164 		 * we don't free this memory in case of error
165 		 * as printer cleanup func will do this for us
166 		 */
167 	} else {
168 		opts->pnp_string = pnp_string;
169 	}
170 
171 	ret = usb_string_ids_tab(cdev, strings);
172 	if (ret < 0)
173 		goto fail_put_func_inst;
174 
175 	device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id;
176 	device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id;
177 	device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id;
178 
179 	if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
180 		struct usb_descriptor_header *usb_desc;
181 
182 		usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
183 		if (!usb_desc) {
184 			ret = -ENOMEM;
185 			goto fail_put_func_inst;
186 		}
187 		usb_otg_descriptor_init(cdev->gadget, usb_desc);
188 		otg_desc[0] = usb_desc;
189 		otg_desc[1] = NULL;
190 	}
191 
192 	ret = usb_add_config(cdev, &printer_cfg_driver, printer_do_config);
193 	if (ret)
194 		goto fail_free_otg_desc;
195 
196 	usb_composite_overwrite_options(cdev, &coverwrite);
197 	return ret;
198 
199 fail_free_otg_desc:
200 	kfree(otg_desc[0]);
201 	otg_desc[0] = NULL;
202 fail_put_func_inst:
203 	usb_put_function_instance(fi_printer);
204 	return ret;
205 }
206 
207 static int printer_unbind(struct usb_composite_dev *cdev)
208 {
209 	usb_put_function(f_printer);
210 	usb_put_function_instance(fi_printer);
211 
212 	kfree(otg_desc[0]);
213 	otg_desc[0] = NULL;
214 
215 	return 0;
216 }
217 
218 static struct usb_composite_driver printer_driver = {
219 	.name           = shortname,
220 	.dev            = &device_desc,
221 	.strings        = dev_strings,
222 	.max_speed      = USB_SPEED_SUPER,
223 	.bind		= printer_bind,
224 	.unbind		= printer_unbind,
225 };
226 
227 module_usb_composite_driver(printer_driver);
228 
229 MODULE_DESCRIPTION(DRIVER_DESC);
230 MODULE_AUTHOR("Craig Nadler");
231 MODULE_LICENSE("GPL");
232