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