Linux 4.2.1
[linux/fpc-iii.git] / drivers / usb / gadget / legacy / dbgp.c
blob204b10b1a7e7dd08c36bc00d44205e0c6753b1c2
1 /*
2 * dbgp.c -- EHCI Debug Port device gadget
4 * Copyright (C) 2010 Stephane Duverger
6 * Released under the GPLv2.
7 */
9 /* verbose messages */
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/usb/ch9.h>
14 #include <linux/usb/gadget.h>
16 #include "u_serial.h"
18 #define DRIVER_VENDOR_ID 0x0525 /* NetChip */
19 #define DRIVER_PRODUCT_ID 0xc0de /* undefined */
21 #define USB_DEBUG_MAX_PACKET_SIZE 8
22 #define DBGP_REQ_EP0_LEN 128
23 #define DBGP_REQ_LEN 512
25 static struct dbgp {
26 struct usb_gadget *gadget;
27 struct usb_request *req;
28 struct usb_ep *i_ep;
29 struct usb_ep *o_ep;
30 #ifdef CONFIG_USB_G_DBGP_SERIAL
31 struct gserial *serial;
32 #endif
33 } dbgp;
35 static struct usb_device_descriptor device_desc = {
36 .bLength = sizeof device_desc,
37 .bDescriptorType = USB_DT_DEVICE,
38 .bcdUSB = __constant_cpu_to_le16(0x0200),
39 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
40 .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_ID),
41 .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_ID),
42 .bNumConfigurations = 1,
45 static struct usb_debug_descriptor dbg_desc = {
46 .bLength = sizeof dbg_desc,
47 .bDescriptorType = USB_DT_DEBUG,
50 static struct usb_endpoint_descriptor i_desc = {
51 .bLength = USB_DT_ENDPOINT_SIZE,
52 .bDescriptorType = USB_DT_ENDPOINT,
53 .bmAttributes = USB_ENDPOINT_XFER_BULK,
54 .bEndpointAddress = USB_DIR_IN,
57 static struct usb_endpoint_descriptor o_desc = {
58 .bLength = USB_DT_ENDPOINT_SIZE,
59 .bDescriptorType = USB_DT_ENDPOINT,
60 .bmAttributes = USB_ENDPOINT_XFER_BULK,
61 .bEndpointAddress = USB_DIR_OUT,
64 #ifdef CONFIG_USB_G_DBGP_PRINTK
65 static int dbgp_consume(char *buf, unsigned len)
67 char c;
69 if (!len)
70 return 0;
72 c = buf[len-1];
73 if (c != 0)
74 buf[len-1] = 0;
76 printk(KERN_NOTICE "%s%c", buf, c);
77 return 0;
80 static void __disable_ep(struct usb_ep *ep)
82 if (ep && ep->driver_data == dbgp.gadget) {
83 usb_ep_disable(ep);
84 ep->driver_data = NULL;
88 static void dbgp_disable_ep(void)
90 __disable_ep(dbgp.i_ep);
91 __disable_ep(dbgp.o_ep);
94 static void dbgp_complete(struct usb_ep *ep, struct usb_request *req)
96 int stp;
97 int err = 0;
98 int status = req->status;
100 if (ep == dbgp.i_ep) {
101 stp = 1;
102 goto fail;
105 if (status != 0) {
106 stp = 2;
107 goto release_req;
110 dbgp_consume(req->buf, req->actual);
112 req->length = DBGP_REQ_LEN;
113 err = usb_ep_queue(ep, req, GFP_ATOMIC);
114 if (err < 0) {
115 stp = 3;
116 goto release_req;
119 return;
121 release_req:
122 kfree(req->buf);
123 usb_ep_free_request(dbgp.o_ep, req);
124 dbgp_disable_ep();
125 fail:
126 dev_dbg(&dbgp.gadget->dev,
127 "complete: failure (%d:%d) ==> %d\n", stp, err, status);
130 static int dbgp_enable_ep_req(struct usb_ep *ep)
132 int err, stp;
133 struct usb_request *req;
135 req = usb_ep_alloc_request(ep, GFP_KERNEL);
136 if (!req) {
137 err = -ENOMEM;
138 stp = 1;
139 goto fail_1;
142 req->buf = kmalloc(DBGP_REQ_LEN, GFP_KERNEL);
143 if (!req->buf) {
144 err = -ENOMEM;
145 stp = 2;
146 goto fail_2;
149 req->complete = dbgp_complete;
150 req->length = DBGP_REQ_LEN;
151 err = usb_ep_queue(ep, req, GFP_ATOMIC);
152 if (err < 0) {
153 stp = 3;
154 goto fail_3;
157 return 0;
159 fail_3:
160 kfree(req->buf);
161 fail_2:
162 usb_ep_free_request(dbgp.o_ep, req);
163 fail_1:
164 dev_dbg(&dbgp.gadget->dev,
165 "enable ep req: failure (%d:%d)\n", stp, err);
166 return err;
169 static int __enable_ep(struct usb_ep *ep, struct usb_endpoint_descriptor *desc)
171 int err;
172 ep->desc = desc;
173 err = usb_ep_enable(ep);
174 ep->driver_data = dbgp.gadget;
175 return err;
178 static int dbgp_enable_ep(void)
180 int err, stp;
182 err = __enable_ep(dbgp.i_ep, &i_desc);
183 if (err < 0) {
184 stp = 1;
185 goto fail_1;
188 err = __enable_ep(dbgp.o_ep, &o_desc);
189 if (err < 0) {
190 stp = 2;
191 goto fail_2;
194 err = dbgp_enable_ep_req(dbgp.o_ep);
195 if (err < 0) {
196 stp = 3;
197 goto fail_3;
200 return 0;
202 fail_3:
203 __disable_ep(dbgp.o_ep);
204 fail_2:
205 __disable_ep(dbgp.i_ep);
206 fail_1:
207 dev_dbg(&dbgp.gadget->dev, "enable ep: failure (%d:%d)\n", stp, err);
208 return err;
210 #endif
212 static void dbgp_disconnect(struct usb_gadget *gadget)
214 #ifdef CONFIG_USB_G_DBGP_PRINTK
215 dbgp_disable_ep();
216 #else
217 gserial_disconnect(dbgp.serial);
218 #endif
221 static void dbgp_unbind(struct usb_gadget *gadget)
223 #ifdef CONFIG_USB_G_DBGP_SERIAL
224 kfree(dbgp.serial);
225 dbgp.serial = NULL;
226 #endif
227 if (dbgp.req) {
228 kfree(dbgp.req->buf);
229 usb_ep_free_request(gadget->ep0, dbgp.req);
230 dbgp.req = NULL;
233 gadget->ep0->driver_data = NULL;
236 #ifdef CONFIG_USB_G_DBGP_SERIAL
237 static unsigned char tty_line;
238 #endif
240 static int dbgp_configure_endpoints(struct usb_gadget *gadget)
242 int stp;
244 usb_ep_autoconfig_reset(gadget);
246 dbgp.i_ep = usb_ep_autoconfig(gadget, &i_desc);
247 if (!dbgp.i_ep) {
248 stp = 1;
249 goto fail_1;
252 dbgp.i_ep->driver_data = gadget;
253 i_desc.wMaxPacketSize =
254 __constant_cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
256 dbgp.o_ep = usb_ep_autoconfig(gadget, &o_desc);
257 if (!dbgp.o_ep) {
258 dbgp.i_ep->driver_data = NULL;
259 stp = 2;
260 goto fail_2;
263 dbgp.o_ep->driver_data = gadget;
264 o_desc.wMaxPacketSize =
265 __constant_cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
267 dbg_desc.bDebugInEndpoint = i_desc.bEndpointAddress;
268 dbg_desc.bDebugOutEndpoint = o_desc.bEndpointAddress;
270 #ifdef CONFIG_USB_G_DBGP_SERIAL
271 dbgp.serial->in = dbgp.i_ep;
272 dbgp.serial->out = dbgp.o_ep;
274 dbgp.serial->in->desc = &i_desc;
275 dbgp.serial->out->desc = &o_desc;
276 #endif
278 return 0;
280 fail_2:
281 dbgp.i_ep->driver_data = NULL;
282 fail_1:
283 dev_dbg(&dbgp.gadget->dev, "ep config: failure (%d)\n", stp);
284 return -ENODEV;
287 static int dbgp_bind(struct usb_gadget *gadget,
288 struct usb_gadget_driver *driver)
290 int err, stp;
292 dbgp.gadget = gadget;
294 dbgp.req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
295 if (!dbgp.req) {
296 err = -ENOMEM;
297 stp = 1;
298 goto fail;
301 dbgp.req->buf = kmalloc(DBGP_REQ_EP0_LEN, GFP_KERNEL);
302 if (!dbgp.req->buf) {
303 err = -ENOMEM;
304 stp = 2;
305 goto fail;
308 dbgp.req->length = DBGP_REQ_EP0_LEN;
309 gadget->ep0->driver_data = gadget;
311 #ifdef CONFIG_USB_G_DBGP_SERIAL
312 dbgp.serial = kzalloc(sizeof(struct gserial), GFP_KERNEL);
313 if (!dbgp.serial) {
314 stp = 3;
315 err = -ENOMEM;
316 goto fail;
319 if (gserial_alloc_line(&tty_line)) {
320 stp = 4;
321 err = -ENODEV;
322 goto fail;
324 #endif
326 err = dbgp_configure_endpoints(gadget);
327 if (err < 0) {
328 stp = 5;
329 goto fail;
332 dev_dbg(&dbgp.gadget->dev, "bind: success\n");
333 return 0;
335 fail:
336 dev_dbg(&gadget->dev, "bind: failure (%d:%d)\n", stp, err);
337 dbgp_unbind(gadget);
338 return err;
341 static void dbgp_setup_complete(struct usb_ep *ep,
342 struct usb_request *req)
344 dev_dbg(&dbgp.gadget->dev, "setup complete: %d, %d/%d\n",
345 req->status, req->actual, req->length);
348 static int dbgp_setup(struct usb_gadget *gadget,
349 const struct usb_ctrlrequest *ctrl)
351 struct usb_request *req = dbgp.req;
352 u8 request = ctrl->bRequest;
353 u16 value = le16_to_cpu(ctrl->wValue);
354 u16 length = le16_to_cpu(ctrl->wLength);
355 int err = -EOPNOTSUPP;
356 void *data = NULL;
357 u16 len = 0;
359 gadget->ep0->driver_data = gadget;
361 if (request == USB_REQ_GET_DESCRIPTOR) {
362 switch (value>>8) {
363 case USB_DT_DEVICE:
364 dev_dbg(&dbgp.gadget->dev, "setup: desc device\n");
365 len = sizeof device_desc;
366 data = &device_desc;
367 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
368 break;
369 case USB_DT_DEBUG:
370 dev_dbg(&dbgp.gadget->dev, "setup: desc debug\n");
371 len = sizeof dbg_desc;
372 data = &dbg_desc;
373 break;
374 default:
375 goto fail;
377 err = 0;
378 } else if (request == USB_REQ_SET_FEATURE &&
379 value == USB_DEVICE_DEBUG_MODE) {
380 dev_dbg(&dbgp.gadget->dev, "setup: feat debug\n");
381 #ifdef CONFIG_USB_G_DBGP_PRINTK
382 err = dbgp_enable_ep();
383 #else
384 err = dbgp_configure_endpoints(gadget);
385 if (err < 0) {
386 goto fail;
388 err = gserial_connect(dbgp.serial, tty_line);
389 #endif
390 if (err < 0)
391 goto fail;
392 } else
393 goto fail;
395 req->length = min(length, len);
396 req->zero = len < req->length;
397 if (data && req->length)
398 memcpy(req->buf, data, req->length);
400 req->complete = dbgp_setup_complete;
401 return usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
403 fail:
404 dev_dbg(&dbgp.gadget->dev,
405 "setup: failure req %x v %x\n", request, value);
406 return err;
409 static struct usb_gadget_driver dbgp_driver = {
410 .function = "dbgp",
411 .max_speed = USB_SPEED_HIGH,
412 .bind = dbgp_bind,
413 .unbind = dbgp_unbind,
414 .setup = dbgp_setup,
415 .reset = dbgp_disconnect,
416 .disconnect = dbgp_disconnect,
417 .driver = {
418 .owner = THIS_MODULE,
419 .name = "dbgp"
423 static int __init dbgp_init(void)
425 return usb_gadget_probe_driver(&dbgp_driver);
428 static void __exit dbgp_exit(void)
430 usb_gadget_unregister_driver(&dbgp_driver);
431 #ifdef CONFIG_USB_G_DBGP_SERIAL
432 gserial_free_line(tty_line);
433 #endif
436 MODULE_AUTHOR("Stephane Duverger");
437 MODULE_LICENSE("GPL");
438 module_init(dbgp_init);
439 module_exit(dbgp_exit);