1 // SPDX-License-Identifier: GPL-2.0+
3 * printer.c -- Printer gadget driver
5 * Copyright (C) 2003-2005 David Brownell
6 * Copyright (C) 2006 Craig W. Nadler
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <asm/byteorder.h>
13 #include <linux/usb/ch9.h>
14 #include <linux/usb/composite.h>
15 #include <linux/usb/gadget.h>
16 #include <linux/usb/g_printer.h>
18 USB_GADGET_COMPOSITE_OPTIONS();
20 #define DRIVER_DESC "Printer Gadget"
21 #define DRIVER_VERSION "2015 FEB 17"
23 static const char shortname
[] = "printer";
24 static const char driver_desc
[] = DRIVER_DESC
;
26 #include "u_printer.h"
28 /*-------------------------------------------------------------------------*/
30 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
31 * Instead: allocate your own, using normal USB-IF procedures.
34 /* Thanks to NetChip Technologies for donating this product ID.
36 #define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
37 #define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
39 /* Some systems will want different product identifiers published in the
40 * device descriptor, either numbers or strings or both. These string
41 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
44 module_param_named(iSerialNum
, coverwrite
.serial_number
, charp
, S_IRUGO
);
45 MODULE_PARM_DESC(iSerialNum
, "1");
47 static char *iPNPstring
;
48 module_param(iPNPstring
, charp
, S_IRUGO
);
49 MODULE_PARM_DESC(iPNPstring
, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
51 /* Number of requests to allocate per endpoint, not used for ep0. */
52 static unsigned qlen
= 10;
53 module_param(qlen
, uint
, S_IRUGO
|S_IWUSR
);
57 static struct usb_function_instance
*fi_printer
;
58 static struct usb_function
*f_printer
;
60 /*-------------------------------------------------------------------------*/
63 * DESCRIPTORS ... most are static, but strings and (full) configuration
64 * descriptors are built on demand.
67 static struct usb_device_descriptor device_desc
= {
68 .bLength
= sizeof device_desc
,
69 .bDescriptorType
= USB_DT_DEVICE
,
70 /* .bcdUSB = DYNAMIC */
71 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
74 .idVendor
= cpu_to_le16(PRINTER_VENDOR_NUM
),
75 .idProduct
= cpu_to_le16(PRINTER_PRODUCT_NUM
),
76 .bNumConfigurations
= 1
79 static const struct usb_descriptor_header
*otg_desc
[2];
81 /*-------------------------------------------------------------------------*/
83 /* descriptors that are built on-demand */
85 static char product_desc
[40] = DRIVER_DESC
;
86 static char serial_num
[40] = "1";
87 static char *pnp_string
=
88 "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
90 /* static strings, in UTF-8 */
91 static struct usb_string strings
[] = {
92 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
93 [USB_GADGET_PRODUCT_IDX
].s
= product_desc
,
94 [USB_GADGET_SERIAL_IDX
].s
= serial_num
,
98 static struct usb_gadget_strings stringtab_dev
= {
99 .language
= 0x0409, /* en-us */
103 static struct usb_gadget_strings
*dev_strings
[] = {
108 static struct usb_configuration printer_cfg_driver
= {
110 .bConfigurationValue
= 1,
111 .bmAttributes
= USB_CONFIG_ATT_ONE
| USB_CONFIG_ATT_SELFPOWER
,
114 static int printer_do_config(struct usb_configuration
*c
)
116 struct usb_gadget
*gadget
= c
->cdev
->gadget
;
119 usb_ep_autoconfig_reset(gadget
);
121 usb_gadget_set_selfpowered(gadget
);
123 if (gadget_is_otg(gadget
)) {
124 printer_cfg_driver
.descriptors
= otg_desc
;
125 printer_cfg_driver
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
128 f_printer
= usb_get_function(fi_printer
);
129 if (IS_ERR(f_printer
))
130 return PTR_ERR(f_printer
);
132 status
= usb_add_function(c
, f_printer
);
134 usb_put_function(f_printer
);
139 static int printer_bind(struct usb_composite_dev
*cdev
)
141 struct f_printer_opts
*opts
;
144 fi_printer
= usb_get_function_instance("printer");
145 if (IS_ERR(fi_printer
))
146 return PTR_ERR(fi_printer
);
148 opts
= container_of(fi_printer
, struct f_printer_opts
, func_inst
);
152 opts
->pnp_string
= kstrdup(iPNPstring
, GFP_KERNEL
);
153 if (!opts
->pnp_string
) {
155 goto fail_put_func_inst
;
157 opts
->pnp_string_allocated
= true;
159 * we don't free this memory in case of error
160 * as printer cleanup func will do this for us
163 opts
->pnp_string
= pnp_string
;
166 ret
= usb_string_ids_tab(cdev
, strings
);
168 goto fail_put_func_inst
;
170 device_desc
.iManufacturer
= strings
[USB_GADGET_MANUFACTURER_IDX
].id
;
171 device_desc
.iProduct
= strings
[USB_GADGET_PRODUCT_IDX
].id
;
172 device_desc
.iSerialNumber
= strings
[USB_GADGET_SERIAL_IDX
].id
;
174 if (gadget_is_otg(cdev
->gadget
) && !otg_desc
[0]) {
175 struct usb_descriptor_header
*usb_desc
;
177 usb_desc
= usb_otg_descriptor_alloc(cdev
->gadget
);
180 goto fail_put_func_inst
;
182 usb_otg_descriptor_init(cdev
->gadget
, usb_desc
);
183 otg_desc
[0] = usb_desc
;
187 ret
= usb_add_config(cdev
, &printer_cfg_driver
, printer_do_config
);
189 goto fail_free_otg_desc
;
191 usb_composite_overwrite_options(cdev
, &coverwrite
);
198 usb_put_function_instance(fi_printer
);
202 static int printer_unbind(struct usb_composite_dev
*cdev
)
204 usb_put_function(f_printer
);
205 usb_put_function_instance(fi_printer
);
213 static struct usb_composite_driver printer_driver
= {
216 .strings
= dev_strings
,
217 .max_speed
= USB_SPEED_SUPER
,
218 .bind
= printer_bind
,
219 .unbind
= printer_unbind
,
222 module_usb_composite_driver(printer_driver
);
224 MODULE_DESCRIPTION(DRIVER_DESC
);
225 MODULE_AUTHOR("Craig Nadler");
226 MODULE_LICENSE("GPL");