Linux 4.2.1
[linux/fpc-iii.git] / drivers / usb / gadget / legacy / printer.c
blob1ce7df1060a5b237ae2532f514d95e3edd63cf34
1 /*
2 * printer.c -- Printer gadget driver
4 * Copyright (C) 2003-2005 David Brownell
5 * Copyright (C) 2006 Craig W. Nadler
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.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <asm/byteorder.h>
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>
22 #include "gadget_chips.h"
24 USB_GADGET_COMPOSITE_OPTIONS();
26 #define DRIVER_DESC "Printer Gadget"
27 #define DRIVER_VERSION "2015 FEB 17"
29 static const char shortname [] = "printer";
30 static const char driver_desc [] = DRIVER_DESC;
32 #include "u_printer.h"
34 /*-------------------------------------------------------------------------*/
36 /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
37 * Instead: allocate your own, using normal USB-IF procedures.
40 /* Thanks to NetChip Technologies for donating this product ID.
42 #define PRINTER_VENDOR_NUM 0x0525 /* NetChip */
43 #define PRINTER_PRODUCT_NUM 0xa4a8 /* Linux-USB Printer Gadget */
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).
50 module_param_named(iSerialNum, coverwrite.serial_number, charp, S_IRUGO);
51 MODULE_PARM_DESC(iSerialNum, "1");
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;");
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);
61 #define QLEN qlen
63 static struct usb_function_instance *fi_printer;
64 static struct usb_function *f_printer;
66 /*-------------------------------------------------------------------------*/
69 * DESCRIPTORS ... most are static, but strings and (full) configuration
70 * descriptors are built on demand.
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
85 static struct usb_otg_descriptor otg_descriptor = {
86 .bLength = sizeof otg_descriptor,
87 .bDescriptorType = USB_DT_OTG,
88 .bmAttributes = USB_OTG_SRP,
91 static const struct usb_descriptor_header *otg_desc[] = {
92 (struct usb_descriptor_header *) &otg_descriptor,
93 NULL,
96 /*-------------------------------------------------------------------------*/
98 /* descriptors that are built on-demand */
100 static char product_desc [40] = DRIVER_DESC;
101 static char serial_num [40] = "1";
102 static char pnp_string[PNP_STRING_LEN] =
103 "XXMFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
105 /* static strings, in UTF-8 */
106 static struct usb_string strings [] = {
107 [USB_GADGET_MANUFACTURER_IDX].s = "",
108 [USB_GADGET_PRODUCT_IDX].s = product_desc,
109 [USB_GADGET_SERIAL_IDX].s = serial_num,
110 { } /* end of list */
113 static struct usb_gadget_strings stringtab_dev = {
114 .language = 0x0409, /* en-us */
115 .strings = strings,
118 static struct usb_gadget_strings *dev_strings[] = {
119 &stringtab_dev,
120 NULL,
123 static struct usb_configuration printer_cfg_driver = {
124 .label = "printer",
125 .bConfigurationValue = 1,
126 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
129 static int printer_do_config(struct usb_configuration *c)
131 struct usb_gadget *gadget = c->cdev->gadget;
132 int status = 0;
134 usb_ep_autoconfig_reset(gadget);
136 usb_gadget_set_selfpowered(gadget);
138 if (gadget_is_otg(gadget)) {
139 otg_descriptor.bmAttributes |= USB_OTG_HNP;
140 printer_cfg_driver.descriptors = otg_desc;
141 printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
144 f_printer = usb_get_function(fi_printer);
145 if (IS_ERR(f_printer))
146 return PTR_ERR(f_printer);
148 status = usb_add_function(c, f_printer);
149 if (status < 0)
150 usb_put_function(f_printer);
152 return status;
155 static int printer_bind(struct usb_composite_dev *cdev)
157 struct f_printer_opts *opts;
158 int ret, len;
160 fi_printer = usb_get_function_instance("printer");
161 if (IS_ERR(fi_printer))
162 return PTR_ERR(fi_printer);
164 if (iPNPstring)
165 strlcpy(&pnp_string[2], iPNPstring, PNP_STRING_LEN - 2);
167 len = strlen(pnp_string);
168 pnp_string[0] = (len >> 8) & 0xFF;
169 pnp_string[1] = len & 0xFF;
171 opts = container_of(fi_printer, struct f_printer_opts, func_inst);
172 opts->minor = 0;
173 memcpy(opts->pnp_string, pnp_string, PNP_STRING_LEN);
174 opts->q_len = QLEN;
176 ret = usb_string_ids_tab(cdev, strings);
177 if (ret < 0) {
178 usb_put_function_instance(fi_printer);
179 return ret;
181 device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id;
182 device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id;
183 device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id;
185 ret = usb_add_config(cdev, &printer_cfg_driver, printer_do_config);
186 if (ret) {
187 usb_put_function_instance(fi_printer);
188 return ret;
190 usb_composite_overwrite_options(cdev, &coverwrite);
191 return ret;
194 static int printer_unbind(struct usb_composite_dev *cdev)
196 usb_put_function(f_printer);
197 usb_put_function_instance(fi_printer);
199 return 0;
202 static struct usb_composite_driver printer_driver = {
203 .name = shortname,
204 .dev = &device_desc,
205 .strings = dev_strings,
206 .max_speed = USB_SPEED_SUPER,
207 .bind = printer_bind,
208 .unbind = printer_unbind,
211 module_usb_composite_driver(printer_driver);
213 MODULE_DESCRIPTION(DRIVER_DESC);
214 MODULE_AUTHOR("Craig Nadler");
215 MODULE_LICENSE("GPL");