1
2
3
4
5
6
7
8
9
10
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
22USB_GADGET_COMPOSITE_OPTIONS();
23
24#define DRIVER_DESC "Printer Gadget"
25#define DRIVER_VERSION "2015 FEB 17"
26
27static const char shortname [] = "printer";
28static const char driver_desc [] = DRIVER_DESC;
29
30#include "u_printer.h"
31
32
33
34
35
36
37
38
39
40#define PRINTER_VENDOR_NUM 0x0525
41#define PRINTER_PRODUCT_NUM 0xa4a8
42
43
44
45
46
47
48module_param_named(iSerialNum, coverwrite.serial_number, charp, S_IRUGO);
49MODULE_PARM_DESC(iSerialNum, "1");
50
51static char *iPNPstring;
52module_param(iPNPstring, charp, S_IRUGO);
53MODULE_PARM_DESC(iPNPstring, "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;");
54
55
56static unsigned qlen = 10;
57module_param(qlen, uint, S_IRUGO|S_IWUSR);
58
59#define QLEN qlen
60
61static struct usb_function_instance *fi_printer;
62static struct usb_function *f_printer;
63
64
65
66
67
68
69
70
71static struct usb_device_descriptor device_desc = {
72 .bLength = sizeof device_desc,
73 .bDescriptorType = USB_DT_DEVICE,
74
75 .bDeviceClass = USB_CLASS_PER_INTERFACE,
76 .bDeviceSubClass = 0,
77 .bDeviceProtocol = 0,
78 .idVendor = cpu_to_le16(PRINTER_VENDOR_NUM),
79 .idProduct = cpu_to_le16(PRINTER_PRODUCT_NUM),
80 .bNumConfigurations = 1
81};
82
83static const struct usb_descriptor_header *otg_desc[2];
84
85
86
87
88
89static char product_desc [40] = DRIVER_DESC;
90static char serial_num [40] = "1";
91static char *pnp_string =
92 "MFG:linux;MDL:g_printer;CLS:PRINTER;SN:1;";
93
94
95static struct usb_string strings [] = {
96 [USB_GADGET_MANUFACTURER_IDX].s = "",
97 [USB_GADGET_PRODUCT_IDX].s = product_desc,
98 [USB_GADGET_SERIAL_IDX].s = serial_num,
99 { }
100};
101
102static struct usb_gadget_strings stringtab_dev = {
103 .language = 0x0409,
104 .strings = strings,
105};
106
107static struct usb_gadget_strings *dev_strings[] = {
108 &stringtab_dev,
109 NULL,
110};
111
112static struct usb_configuration printer_cfg_driver = {
113 .label = "printer",
114 .bConfigurationValue = 1,
115 .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
116};
117
118static int printer_do_config(struct usb_configuration *c)
119{
120 struct usb_gadget *gadget = c->cdev->gadget;
121 int status = 0;
122
123 usb_ep_autoconfig_reset(gadget);
124
125 usb_gadget_set_selfpowered(gadget);
126
127 if (gadget_is_otg(gadget)) {
128 printer_cfg_driver.descriptors = otg_desc;
129 printer_cfg_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
130 }
131
132 f_printer = usb_get_function(fi_printer);
133 if (IS_ERR(f_printer))
134 return PTR_ERR(f_printer);
135
136 status = usb_add_function(c, f_printer);
137 if (status < 0)
138 usb_put_function(f_printer);
139
140 return status;
141}
142
143static int printer_bind(struct usb_composite_dev *cdev)
144{
145 struct f_printer_opts *opts;
146 int ret;
147
148 fi_printer = usb_get_function_instance("printer");
149 if (IS_ERR(fi_printer))
150 return PTR_ERR(fi_printer);
151
152 opts = container_of(fi_printer, struct f_printer_opts, func_inst);
153 opts->minor = 0;
154 opts->q_len = QLEN;
155 if (iPNPstring) {
156 opts->pnp_string = kstrdup(iPNPstring, GFP_KERNEL);
157 if (!opts->pnp_string) {
158 ret = -ENOMEM;
159 goto fail_put_func_inst;
160 }
161 opts->pnp_string_allocated = true;
162
163
164
165
166 } else {
167 opts->pnp_string = pnp_string;
168 }
169
170 ret = usb_string_ids_tab(cdev, strings);
171 if (ret < 0)
172 goto fail_put_func_inst;
173
174 device_desc.iManufacturer = strings[USB_GADGET_MANUFACTURER_IDX].id;
175 device_desc.iProduct = strings[USB_GADGET_PRODUCT_IDX].id;
176 device_desc.iSerialNumber = strings[USB_GADGET_SERIAL_IDX].id;
177
178 if (gadget_is_otg(cdev->gadget) && !otg_desc[0]) {
179 struct usb_descriptor_header *usb_desc;
180
181 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
182 if (!usb_desc) {
183 ret = -ENOMEM;
184 goto fail_put_func_inst;
185 }
186 usb_otg_descriptor_init(cdev->gadget, usb_desc);
187 otg_desc[0] = usb_desc;
188 otg_desc[1] = NULL;
189 }
190
191 ret = usb_add_config(cdev, &printer_cfg_driver, printer_do_config);
192 if (ret)
193 goto fail_free_otg_desc;
194
195 usb_composite_overwrite_options(cdev, &coverwrite);
196 return ret;
197
198fail_free_otg_desc:
199 kfree(otg_desc[0]);
200 otg_desc[0] = NULL;
201fail_put_func_inst:
202 usb_put_function_instance(fi_printer);
203 return ret;
204}
205
206static int printer_unbind(struct usb_composite_dev *cdev)
207{
208 usb_put_function(f_printer);
209 usb_put_function_instance(fi_printer);
210
211 kfree(otg_desc[0]);
212 otg_desc[0] = NULL;
213
214 return 0;
215}
216
217static struct usb_composite_driver printer_driver = {
218 .name = shortname,
219 .dev = &device_desc,
220 .strings = dev_strings,
221 .max_speed = USB_SPEED_SUPER,
222 .bind = printer_bind,
223 .unbind = printer_unbind,
224};
225
226module_usb_composite_driver(printer_driver);
227
228MODULE_DESCRIPTION(DRIVER_DESC);
229MODULE_AUTHOR("Craig Nadler");
230MODULE_LICENSE("GPL");
231