Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / usb / gadget / dbgp.c
blobe8737b59c8dc41ff9457e2933467e4d43c7310d3
1 /*
2 * dbgp.c -- EHCI Debug Port device gadget
4 * Copyright (C) 2010 Stephane Duverger
6 * Released under the GPLv2.
8 */
10 /* verbose messages */
11 #include <linux/kernel.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/usb/ch9.h>
15 #include <linux/usb/gadget.h>
17 /* See comments in "zero.c" */
18 #include "epautoconf.c"
20 #ifdef CONFIG_USB_G_DBGP_SERIAL
21 #include "u_serial.c"
22 #endif
24 #define DRIVER_VENDOR_ID 0x0525 /* NetChip */
25 #define DRIVER_PRODUCT_ID 0xc0de /* undefined */
27 #define USB_DEBUG_MAX_PACKET_SIZE 8
28 #define DBGP_REQ_EP0_LEN 128
29 #define DBGP_REQ_LEN 512
31 static struct dbgp {
32 struct usb_gadget *gadget;
33 struct usb_request *req;
34 struct usb_ep *i_ep;
35 struct usb_ep *o_ep;
36 #ifdef CONFIG_USB_G_DBGP_SERIAL
37 struct gserial *serial;
38 #endif
39 } dbgp;
41 static struct usb_device_descriptor device_desc = {
42 .bLength = sizeof device_desc,
43 .bDescriptorType = USB_DT_DEVICE,
44 .bcdUSB = __constant_cpu_to_le16(0x0200),
45 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
46 .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_ID),
47 .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_ID),
48 .bNumConfigurations = 1,
51 static struct usb_debug_descriptor dbg_desc = {
52 .bLength = sizeof dbg_desc,
53 .bDescriptorType = USB_DT_DEBUG,
56 static struct usb_endpoint_descriptor i_desc = {
57 .bLength = USB_DT_ENDPOINT_SIZE,
58 .bDescriptorType = USB_DT_ENDPOINT,
59 .bmAttributes = USB_ENDPOINT_XFER_BULK,
60 .bEndpointAddress = USB_DIR_IN,
63 static struct usb_endpoint_descriptor o_desc = {
64 .bLength = USB_DT_ENDPOINT_SIZE,
65 .bDescriptorType = USB_DT_ENDPOINT,
66 .bmAttributes = USB_ENDPOINT_XFER_BULK,
67 .bEndpointAddress = USB_DIR_OUT,
70 #ifdef CONFIG_USB_G_DBGP_PRINTK
71 static int dbgp_consume(char *buf, unsigned len)
73 char c;
75 if (!len)
76 return 0;
78 c = buf[len-1];
79 if (c != 0)
80 buf[len-1] = 0;
82 printk(KERN_NOTICE "%s%c", buf, c);
83 return 0;
86 static void __disable_ep(struct usb_ep *ep)
88 if (ep && ep->driver_data == dbgp.gadget) {
89 usb_ep_disable(ep);
90 ep->driver_data = NULL;
94 static void dbgp_disable_ep(void)
96 __disable_ep(dbgp.i_ep);
97 __disable_ep(dbgp.o_ep);
100 static void dbgp_complete(struct usb_ep *ep, struct usb_request *req)
102 int stp;
103 int err = 0;
104 int status = req->status;
106 if (ep == dbgp.i_ep) {
107 stp = 1;
108 goto fail;
111 if (status != 0) {
112 stp = 2;
113 goto release_req;
116 dbgp_consume(req->buf, req->actual);
118 req->length = DBGP_REQ_LEN;
119 err = usb_ep_queue(ep, req, GFP_ATOMIC);
120 if (err < 0) {
121 stp = 3;
122 goto release_req;
125 return;
127 release_req:
128 kfree(req->buf);
129 usb_ep_free_request(dbgp.o_ep, req);
130 dbgp_disable_ep();
131 fail:
132 dev_dbg(&dbgp.gadget->dev,
133 "complete: failure (%d:%d) ==> %d\n", stp, err, status);
136 static int dbgp_enable_ep_req(struct usb_ep *ep)
138 int err, stp;
139 struct usb_request *req;
141 req = usb_ep_alloc_request(ep, GFP_KERNEL);
142 if (!req) {
143 err = -ENOMEM;
144 stp = 1;
145 goto fail_1;
148 req->buf = kmalloc(DBGP_REQ_LEN, GFP_KERNEL);
149 if (!req->buf) {
150 err = -ENOMEM;
151 stp = 2;
152 goto fail_2;
155 req->complete = dbgp_complete;
156 req->length = DBGP_REQ_LEN;
157 err = usb_ep_queue(ep, req, GFP_ATOMIC);
158 if (err < 0) {
159 stp = 3;
160 goto fail_3;
163 return 0;
165 fail_3:
166 kfree(req->buf);
167 fail_2:
168 usb_ep_free_request(dbgp.o_ep, req);
169 fail_1:
170 dev_dbg(&dbgp.gadget->dev,
171 "enable ep req: failure (%d:%d)\n", stp, err);
172 return err;
175 static int __enable_ep(struct usb_ep *ep, struct usb_endpoint_descriptor *desc)
177 int err;
178 ep->desc = desc;
179 err = usb_ep_enable(ep);
180 ep->driver_data = dbgp.gadget;
181 return err;
184 static int dbgp_enable_ep(void)
186 int err, stp;
188 err = __enable_ep(dbgp.i_ep, &i_desc);
189 if (err < 0) {
190 stp = 1;
191 goto fail_1;
194 err = __enable_ep(dbgp.o_ep, &o_desc);
195 if (err < 0) {
196 stp = 2;
197 goto fail_2;
200 err = dbgp_enable_ep_req(dbgp.o_ep);
201 if (err < 0) {
202 stp = 3;
203 goto fail_3;
206 return 0;
208 fail_3:
209 __disable_ep(dbgp.o_ep);
210 fail_2:
211 __disable_ep(dbgp.i_ep);
212 fail_1:
213 dev_dbg(&dbgp.gadget->dev, "enable ep: failure (%d:%d)\n", stp, err);
214 return err;
216 #endif
218 static void dbgp_disconnect(struct usb_gadget *gadget)
220 #ifdef CONFIG_USB_G_DBGP_PRINTK
221 dbgp_disable_ep();
222 #else
223 gserial_disconnect(dbgp.serial);
224 #endif
227 static void dbgp_unbind(struct usb_gadget *gadget)
229 #ifdef CONFIG_USB_G_DBGP_SERIAL
230 kfree(dbgp.serial);
231 #endif
232 if (dbgp.req) {
233 kfree(dbgp.req->buf);
234 usb_ep_free_request(gadget->ep0, dbgp.req);
237 gadget->ep0->driver_data = NULL;
240 static int __init 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;
277 if (gserial_setup(gadget, 1) < 0) {
278 stp = 3;
279 goto fail_3;
282 return 0;
284 fail_3:
285 dbgp.o_ep->driver_data = NULL;
286 #else
287 return 0;
288 #endif
289 fail_2:
290 dbgp.i_ep->driver_data = NULL;
291 fail_1:
292 dev_dbg(&dbgp.gadget->dev, "ep config: failure (%d)\n", stp);
293 return -ENODEV;
296 static int __init dbgp_bind(struct usb_gadget *gadget)
298 int err, stp;
300 dbgp.gadget = gadget;
302 dbgp.req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
303 if (!dbgp.req) {
304 err = -ENOMEM;
305 stp = 1;
306 goto fail;
309 dbgp.req->buf = kmalloc(DBGP_REQ_EP0_LEN, GFP_KERNEL);
310 if (!dbgp.req->buf) {
311 err = -ENOMEM;
312 stp = 2;
313 goto fail;
316 dbgp.req->length = DBGP_REQ_EP0_LEN;
317 gadget->ep0->driver_data = gadget;
319 #ifdef CONFIG_USB_G_DBGP_SERIAL
320 dbgp.serial = kzalloc(sizeof(struct gserial), GFP_KERNEL);
321 if (!dbgp.serial) {
322 stp = 3;
323 err = -ENOMEM;
324 goto fail;
326 #endif
327 err = dbgp_configure_endpoints(gadget);
328 if (err < 0) {
329 stp = 4;
330 goto fail;
333 dev_dbg(&dbgp.gadget->dev, "bind: success\n");
334 return 0;
336 fail:
337 dev_dbg(&gadget->dev, "bind: failure (%d:%d)\n", stp, err);
338 dbgp_unbind(gadget);
339 return err;
342 static void dbgp_setup_complete(struct usb_ep *ep,
343 struct usb_request *req)
345 dev_dbg(&dbgp.gadget->dev, "setup complete: %d, %d/%d\n",
346 req->status, req->actual, req->length);
349 static int dbgp_setup(struct usb_gadget *gadget,
350 const struct usb_ctrlrequest *ctrl)
352 struct usb_request *req = dbgp.req;
353 u8 request = ctrl->bRequest;
354 u16 value = le16_to_cpu(ctrl->wValue);
355 u16 length = le16_to_cpu(ctrl->wLength);
356 int err = -EOPNOTSUPP;
357 void *data = NULL;
358 u16 len = 0;
360 gadget->ep0->driver_data = gadget;
362 if (request == USB_REQ_GET_DESCRIPTOR) {
363 switch (value>>8) {
364 case USB_DT_DEVICE:
365 dev_dbg(&dbgp.gadget->dev, "setup: desc device\n");
366 len = sizeof device_desc;
367 data = &device_desc;
368 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
369 break;
370 case USB_DT_DEBUG:
371 dev_dbg(&dbgp.gadget->dev, "setup: desc debug\n");
372 len = sizeof dbg_desc;
373 data = &dbg_desc;
374 break;
375 default:
376 goto fail;
378 err = 0;
379 } else if (request == USB_REQ_SET_FEATURE &&
380 value == USB_DEVICE_DEBUG_MODE) {
381 dev_dbg(&dbgp.gadget->dev, "setup: feat debug\n");
382 #ifdef CONFIG_USB_G_DBGP_PRINTK
383 err = dbgp_enable_ep();
384 #else
385 err = gserial_connect(dbgp.serial, 0);
386 #endif
387 if (err < 0)
388 goto fail;
389 } else
390 goto fail;
392 req->length = min(length, len);
393 req->zero = len < req->length;
394 if (data && req->length)
395 memcpy(req->buf, data, req->length);
397 req->complete = dbgp_setup_complete;
398 return usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
400 fail:
401 dev_dbg(&dbgp.gadget->dev,
402 "setup: failure req %x v %x\n", request, value);
403 return err;
406 static struct usb_gadget_driver dbgp_driver = {
407 .function = "dbgp",
408 .speed = USB_SPEED_HIGH,
409 .unbind = dbgp_unbind,
410 .setup = dbgp_setup,
411 .disconnect = dbgp_disconnect,
412 .driver = {
413 .owner = THIS_MODULE,
414 .name = "dbgp"
418 static int __init dbgp_init(void)
420 return usb_gadget_probe_driver(&dbgp_driver, dbgp_bind);
423 static void __exit dbgp_exit(void)
425 usb_gadget_unregister_driver(&dbgp_driver);
426 #ifdef CONFIG_USB_G_DBGP_SERIAL
427 gserial_cleanup();
428 #endif
431 MODULE_AUTHOR("Stephane Duverger");
432 MODULE_LICENSE("GPL");
433 module_init(dbgp_init);
434 module_exit(dbgp_exit);