blob: ed762ba9b6297b1325510b5178a653e8a3ee4b55 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Craig W. Nadler25a010c2007-11-11 15:00:15 -08002/*
3 * printer.c -- Printer gadget driver
4 *
5 * Copyright (C) 2003-2005 David Brownell
6 * Copyright (C) 2006 Craig W. Nadler
Craig W. Nadler25a010c2007-11-11 15:00:15 -08007 */
8
9#include <linux/module.h>
10#include <linux/kernel.h>
Craig W. Nadler25a010c2007-11-11 15:00:15 -080011#include <asm/byteorder.h>
Craig W. Nadler25a010c2007-11-11 15:00:15 -080012
13#include <linux/usb/ch9.h>
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +020014#include <linux/usb/composite.h>
Craig W. Nadler25a010c2007-11-11 15:00:15 -080015#include <linux/usb/gadget.h>
16#include <linux/usb/g_printer.h>
17
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020018USB_GADGET_COMPOSITE_OPTIONS();
David Brownell0a56b032008-08-18 17:42:49 -070019
Craig W. Nadler25a010c2007-11-11 15:00:15 -080020#define DRIVER_DESC "Printer Gadget"
Andrzej Pietrasiewiczb185f012015-03-03 10:52:27 +010021#define DRIVER_VERSION "2015 FEB 17"
Andrzej Pietrasiewicz6dd8c2e2015-03-03 10:52:26 +010022
Craig W. Nadler25a010c2007-11-11 15:00:15 -080023static const char shortname [] = "printer";
Craig W. Nadler25a010c2007-11-11 15:00:15 -080024
Andrzej Pietrasiewicz69504f82015-03-03 10:52:29 +010025#include "u_printer.h"
Andrzej Pietrasiewicz143d53e2015-03-03 10:52:22 +010026
Craig W. Nadler25a010c2007-11-11 15:00:15 -080027/*-------------------------------------------------------------------------*/
28
29/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
30 * Instead: allocate your own, using normal USB-IF procedures.
31 */
32
33/* Thanks to NetChip Technologies for donating this product ID.
34 */
35#define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
36#define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
37
Lucas De Marchi25985ed2011-03-30 22:57:33 -030038/* Some systems will want different product identifiers published in the
Craig W. Nadler25a010c2007-11-11 15:00:15 -080039 * device descriptor, either numbers or strings or both. These string
40 * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
41 */
42
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +020043module_param_named(iSerialNum, coverwrite.serial_number, charp, S_IRUGO);
Craig W. Nadler25a010c2007-11-11 15:00:15 -080044MODULE_PARM_DESC(iSerialNum, "1");
45
Alan Sternad84e4a2011-01-10 11:24:14 -050046static char *iPNPstring;
Craig W. Nadler25a010c2007-11-11 15:00:15 -080047module_param(iPNPstring, charp, S_IRUGO);
48MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
49
50/* Number of requests to allocate per endpoint, not used for ep0. */
51static unsigned qlen = 10;
52module_param(qlen, uint, S_IRUGO|S_IWUSR);
Wei Ming Chen9fe3c932021-08-21 22:26:47 +080053MODULE_PARM_DESC(qlen, "The number of 8k buffers to use per endpoint");
Craig W. Nadler25a010c2007-11-11 15:00:15 -080054
55#define QLEN qlen
56
Andrzej Pietrasiewicz69504f82015-03-03 10:52:29 +010057static struct usb_function_instance *fi_printer;
58static struct usb_function *f_printer;
59
Craig W. Nadler25a010c2007-11-11 15:00:15 -080060/*-------------------------------------------------------------------------*/
61
Craig W. Nadler25a010c2007-11-11 15:00:15 -080062/*
63 * DESCRIPTORS ... most are static, but strings and (full) configuration
64 * descriptors are built on demand.
65 */
66
Craig W. Nadler25a010c2007-11-11 15:00:15 -080067static struct usb_device_descriptor device_desc = {
68 .bLength = sizeof device_desc,
69 .bDescriptorType = USB_DT_DEVICE,
Igor Kotrasinski0aecfc12015-10-20 18:33:13 +020070 /* .bcdUSB = DYNAMIC */
Craig W. Nadler25a010c2007-11-11 15:00:15 -080071 .bDeviceClass = USB_CLASS_PER_INTERFACE,
72 .bDeviceSubClass = 0,
73 .bDeviceProtocol = 0,
Harvey Harrison551509d2009-02-11 14:11:36 -080074 .idVendor = cpu_to_le16(PRINTER_VENDOR_NUM),
75 .idProduct = cpu_to_le16(PRINTER_PRODUCT_NUM),
Craig W. Nadler25a010c2007-11-11 15:00:15 -080076 .bNumConfigurations = 1
77};
78
Li Jun93d39af2015-07-09 15:18:58 +080079static const struct usb_descriptor_header *otg_desc[2];
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +010080
Craig W. Nadler25a010c2007-11-11 15:00:15 -080081/*-------------------------------------------------------------------------*/
82
83/* descriptors that are built on-demand */
84
Craig W. Nadler25a010c2007-11-11 15:00:15 -080085static char product_desc [40] = DRIVER_DESC;
86static char serial_num [40] = "1";
Krzysztof Opasiakfdc01cc2016-12-21 09:48:45 +010087static char *pnp_string =
88 "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
Craig W. Nadler25a010c2007-11-11 15:00:15 -080089
90/* static strings, in UTF-8 */
91static struct usb_string strings [] = {
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +020092 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020093 [USB_GADGET_PRODUCT_IDX].s = product_desc,
94 [USB_GADGET_SERIAL_IDX].s = serial_num,
Craig W. Nadler25a010c2007-11-11 15:00:15 -080095 { } /* end of list */
96};
97
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +010098static struct usb_gadget_strings stringtab_dev = {
Craig W. Nadler25a010c2007-11-11 15:00:15 -080099 .language = 0x0409, /* en-us */
100 .strings = strings,
101};
102
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100103static struct usb_gadget_strings *dev_strings[] = {
104 &stringtab_dev,
105 NULL,
106};
107
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100108static struct usb_configuration printer_cfg_driver = {
109 .label = "printer",
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100110 .bConfigurationValue = 1,
111 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
112};
113
Arnd Bergmannc94e2892015-04-11 00:14:21 +0200114static int printer_do_config(struct usb_configuration *c)
Andrzej Pietrasiewiczae2dd0d2015-03-03 10:52:12 +0100115{
116 struct usb_gadget *gadget = c->cdev->gadget;
Andrzej Pietrasiewicz69504f82015-03-03 10:52:29 +0100117 int status = 0;
Andrzej Pietrasiewiczae2dd0d2015-03-03 10:52:12 +0100118
119 usb_ep_autoconfig_reset(gadget);
120
121 usb_gadget_set_selfpowered(gadget);
122
123 if (gadget_is_otg(gadget)) {
Andrzej Pietrasiewiczae2dd0d2015-03-03 10:52:12 +0100124 printer_cfg_driver.descriptors = otg_desc;
125 printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
126 }
127
Andrzej Pietrasiewicz69504f82015-03-03 10:52:29 +0100128 f_printer = usb_get_function(fi_printer);
129 if (IS_ERR(f_printer))
130 return PTR_ERR(f_printer);
131
132 status = usb_add_function(c, f_printer);
133 if (status < 0)
134 usb_put_function(f_printer);
135
136 return status;
Andrzej Pietrasiewiczae2dd0d2015-03-03 10:52:12 +0100137}
138
Arnd Bergmannc94e2892015-04-11 00:14:21 +0200139static int printer_bind(struct usb_composite_dev *cdev)
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100140{
Andrzej Pietrasiewicz69504f82015-03-03 10:52:29 +0100141 struct f_printer_opts *opts;
Krzysztof Opasiakfdc01cc2016-12-21 09:48:45 +0100142 int ret;
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +0200143
Andrzej Pietrasiewicz69504f82015-03-03 10:52:29 +0100144 fi_printer = usb_get_function_instance("printer");
145 if (IS_ERR(fi_printer))
146 return PTR_ERR(fi_printer);
147
Andrzej Pietrasiewicz69504f82015-03-03 10:52:29 +0100148 opts = container_of(fi_printer, struct f_printer_opts, func_inst);
149 opts->minor = 0;
Andrzej Pietrasiewicz69504f82015-03-03 10:52:29 +0100150 opts->q_len = QLEN;
Krzysztof Opasiakfdc01cc2016-12-21 09:48:45 +0100151 if (iPNPstring) {
152 opts->pnp_string = kstrdup(iPNPstring, GFP_KERNEL);
153 if (!opts->pnp_string) {
154 ret = -ENOMEM;
155 goto fail_put_func_inst;
156 }
157 opts->pnp_string_allocated = true;
158 /*
159 * we don't free this memory in case of error
160 * as printer cleanup func will do this for us
161 */
162 } else {
163 opts->pnp_string = pnp_string;
164 }
Andrzej Pietrasiewicza8447152015-03-03 10:52:19 +0100165
166 ret = usb_string_ids_tab(cdev, strings);
Li Jun93d39af2015-07-09 15:18:58 +0800167 if (ret < 0)
168 goto fail_put_func_inst;
169
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200170 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;
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +0200173
Li Jun93d39af2015-07-09 15:18:58 +0800174 if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
175 struct usb_descriptor_header *usb_desc;
176
177 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
178 if (!usb_desc) {
179 ret = -ENOMEM;
180 goto fail_put_func_inst;
181 }
182 usb_otg_descriptor_init(cdev->gadget, usb_desc);
183 otg_desc[0] = usb_desc;
184 otg_desc[1] = NULL;
Andrzej Pietrasiewicza8447152015-03-03 10:52:19 +0100185 }
Li Jun93d39af2015-07-09 15:18:58 +0800186
187 ret = usb_add_config(cdev, &printer_cfg_driver, printer_do_config);
188 if (ret)
189 goto fail_free_otg_desc;
190
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200191 usb_composite_overwrite_options(cdev, &coverwrite);
Sebastian Andrzej Siewior5a175bb2012-09-06 20:11:17 +0200192 return ret;
Li Jun93d39af2015-07-09 15:18:58 +0800193
194fail_free_otg_desc:
195 kfree(otg_desc[0]);
196 otg_desc[0] = NULL;
197fail_put_func_inst:
198 usb_put_function_instance(fi_printer);
199 return ret;
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100200}
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800201
Arnd Bergmannc94e2892015-04-11 00:14:21 +0200202static int printer_unbind(struct usb_composite_dev *cdev)
Andrzej Pietrasiewicza8447152015-03-03 10:52:19 +0100203{
Andrzej Pietrasiewicz69504f82015-03-03 10:52:29 +0100204 usb_put_function(f_printer);
205 usb_put_function_instance(fi_printer);
206
Li Jun93d39af2015-07-09 15:18:58 +0800207 kfree(otg_desc[0]);
208 otg_desc[0] = NULL;
209
Andrzej Pietrasiewicza8447152015-03-03 10:52:19 +0100210 return 0;
211}
212
Arnd Bergmannc94e2892015-04-11 00:14:21 +0200213static struct usb_composite_driver printer_driver = {
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100214 .name = shortname,
215 .dev = &device_desc,
216 .strings = dev_strings,
Jorge Ramirez-Ortiz74df41b2014-11-18 15:11:54 -0500217 .max_speed = USB_SPEED_SUPER,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200218 .bind = printer_bind,
Andrzej Pietrasiewicza8447152015-03-03 10:52:19 +0100219 .unbind = printer_unbind,
Craig W. Nadler25a010c2007-11-11 15:00:15 -0800220};
221
Andrzej Pietrasiewicza2a8e482015-03-03 10:52:31 +0100222module_usb_composite_driver(printer_driver);
Sebastian Andrzej Siewior2e87edf2012-02-04 18:55:20 +0100223
224MODULE_DESCRIPTION(DRIVER_DESC);
225MODULE_AUTHOR("Craig Nadler");
226MODULE_LICENSE("GPL");