Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[pandora-kernel.git] / drivers / usb / gadget / dbgp.c
1 /*
2  * dbgp.c -- EHCI Debug Port device gadget
3  *
4  * Copyright (C) 2010 Stephane Duverger
5  *
6  * Released under the GPLv2.
7  *
8  */
9
10 /* verbose messages */
11 #include <linux/kernel.h>
12 #include <linux/device.h>
13 #include <linux/usb/ch9.h>
14 #include <linux/usb/gadget.h>
15
16 /* See comments in "zero.c" */
17 #include "epautoconf.c"
18
19 #ifdef CONFIG_USB_G_DBGP_SERIAL
20 #include "u_serial.c"
21 #endif
22
23 #define DRIVER_VENDOR_ID        0x0525 /* NetChip */
24 #define DRIVER_PRODUCT_ID       0xc0de /* undefined */
25
26 #define USB_DEBUG_MAX_PACKET_SIZE     8
27 #define DBGP_REQ_EP0_LEN              128
28 #define DBGP_REQ_LEN                  512
29
30 static struct dbgp {
31         struct usb_gadget  *gadget;
32         struct usb_request *req;
33         struct usb_ep      *i_ep;
34         struct usb_ep      *o_ep;
35 #ifdef CONFIG_USB_G_DBGP_SERIAL
36         struct gserial     *serial;
37 #endif
38 } dbgp;
39
40 static struct usb_device_descriptor device_desc = {
41         .bLength = sizeof device_desc,
42         .bDescriptorType = USB_DT_DEVICE,
43         .bcdUSB = __constant_cpu_to_le16(0x0200),
44         .bDeviceClass = USB_CLASS_VENDOR_SPEC,
45         .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_ID),
46         .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_ID),
47         .bNumConfigurations = 1,
48 };
49
50 static struct usb_debug_descriptor dbg_desc = {
51         .bLength = sizeof dbg_desc,
52         .bDescriptorType = USB_DT_DEBUG,
53 };
54
55 static struct usb_endpoint_descriptor i_desc = {
56         .bLength = USB_DT_ENDPOINT_SIZE,
57         .bDescriptorType = USB_DT_ENDPOINT,
58         .bmAttributes = USB_ENDPOINT_XFER_BULK,
59         .bEndpointAddress = USB_DIR_IN,
60 };
61
62 static struct usb_endpoint_descriptor o_desc = {
63         .bLength = USB_DT_ENDPOINT_SIZE,
64         .bDescriptorType = USB_DT_ENDPOINT,
65         .bmAttributes = USB_ENDPOINT_XFER_BULK,
66         .bEndpointAddress = USB_DIR_OUT,
67 };
68
69 #ifdef CONFIG_USB_G_DBGP_PRINTK
70 static int dbgp_consume(char *buf, unsigned len)
71 {
72         char c;
73
74         if (!len)
75                 return 0;
76
77         c = buf[len-1];
78         if (c != 0)
79                 buf[len-1] = 0;
80
81         printk(KERN_NOTICE "%s%c", buf, c);
82         return 0;
83 }
84
85 static void __disable_ep(struct usb_ep *ep)
86 {
87         if (ep && ep->driver_data == dbgp.gadget) {
88                 usb_ep_disable(ep);
89                 ep->driver_data = NULL;
90         }
91 }
92
93 static void dbgp_disable_ep(void)
94 {
95         __disable_ep(dbgp.i_ep);
96         __disable_ep(dbgp.o_ep);
97 }
98
99 static void dbgp_complete(struct usb_ep *ep, struct usb_request *req)
100 {
101         int stp;
102         int err = 0;
103         int status = req->status;
104
105         if (ep == dbgp.i_ep) {
106                 stp = 1;
107                 goto fail;
108         }
109
110         if (status != 0) {
111                 stp = 2;
112                 goto release_req;
113         }
114
115         dbgp_consume(req->buf, req->actual);
116
117         req->length = DBGP_REQ_LEN;
118         err = usb_ep_queue(ep, req, GFP_ATOMIC);
119         if (err < 0) {
120                 stp = 3;
121                 goto release_req;
122         }
123
124         return;
125
126 release_req:
127         kfree(req->buf);
128         usb_ep_free_request(dbgp.o_ep, req);
129         dbgp_disable_ep();
130 fail:
131         dev_dbg(&dbgp.gadget->dev,
132                 "complete: failure (%d:%d) ==> %d\n", stp, err, status);
133 }
134
135 static int dbgp_enable_ep_req(struct usb_ep *ep)
136 {
137         int err, stp;
138         struct usb_request *req;
139
140         req = usb_ep_alloc_request(ep, GFP_KERNEL);
141         if (!req) {
142                 err = -ENOMEM;
143                 stp = 1;
144                 goto fail_1;
145         }
146
147         req->buf = kmalloc(DBGP_REQ_LEN, GFP_KERNEL);
148         if (!req->buf) {
149                 err = -ENOMEM;
150                 stp = 2;
151                 goto fail_2;
152         }
153
154         req->complete = dbgp_complete;
155         req->length = DBGP_REQ_LEN;
156         err = usb_ep_queue(ep, req, GFP_ATOMIC);
157         if (err < 0) {
158                 stp = 3;
159                 goto fail_3;
160         }
161
162         return 0;
163
164 fail_3:
165         kfree(req->buf);
166 fail_2:
167         usb_ep_free_request(dbgp.o_ep, req);
168 fail_1:
169         dev_dbg(&dbgp.gadget->dev,
170                 "enable ep req: failure (%d:%d)\n", stp, err);
171         return err;
172 }
173
174 static int __enable_ep(struct usb_ep *ep, struct usb_endpoint_descriptor *desc)
175 {
176         int err = usb_ep_enable(ep, desc);
177         ep->driver_data = dbgp.gadget;
178         return err;
179 }
180
181 static int dbgp_enable_ep(void)
182 {
183         int err, stp;
184
185         err = __enable_ep(dbgp.i_ep, &i_desc);
186         if (err < 0) {
187                 stp = 1;
188                 goto fail_1;
189         }
190
191         err = __enable_ep(dbgp.o_ep, &o_desc);
192         if (err < 0) {
193                 stp = 2;
194                 goto fail_2;
195         }
196
197         err = dbgp_enable_ep_req(dbgp.o_ep);
198         if (err < 0) {
199                 stp = 3;
200                 goto fail_3;
201         }
202
203         return 0;
204
205 fail_3:
206         __disable_ep(dbgp.o_ep);
207 fail_2:
208         __disable_ep(dbgp.i_ep);
209 fail_1:
210         dev_dbg(&dbgp.gadget->dev, "enable ep: failure (%d:%d)\n", stp, err);
211         return err;
212 }
213 #endif
214
215 static void dbgp_disconnect(struct usb_gadget *gadget)
216 {
217 #ifdef CONFIG_USB_G_DBGP_PRINTK
218         dbgp_disable_ep();
219 #else
220         gserial_disconnect(dbgp.serial);
221 #endif
222 }
223
224 static void dbgp_unbind(struct usb_gadget *gadget)
225 {
226 #ifdef CONFIG_USB_G_DBGP_SERIAL
227         kfree(dbgp.serial);
228 #endif
229         if (dbgp.req) {
230                 kfree(dbgp.req->buf);
231                 usb_ep_free_request(gadget->ep0, dbgp.req);
232         }
233
234         gadget->ep0->driver_data = NULL;
235 }
236
237 static int __init dbgp_configure_endpoints(struct usb_gadget *gadget)
238 {
239         int stp;
240
241         usb_ep_autoconfig_reset(gadget);
242
243         dbgp.i_ep = usb_ep_autoconfig(gadget, &i_desc);
244         if (!dbgp.i_ep) {
245                 stp = 1;
246                 goto fail_1;
247         }
248
249         dbgp.i_ep->driver_data = gadget;
250         i_desc.wMaxPacketSize =
251                 __constant_cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
252
253         dbgp.o_ep = usb_ep_autoconfig(gadget, &o_desc);
254         if (!dbgp.o_ep) {
255                 dbgp.i_ep->driver_data = NULL;
256                 stp = 2;
257                 goto fail_2;
258         }
259
260         dbgp.o_ep->driver_data = gadget;
261         o_desc.wMaxPacketSize =
262                 __constant_cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
263
264         dbg_desc.bDebugInEndpoint = i_desc.bEndpointAddress;
265         dbg_desc.bDebugOutEndpoint = o_desc.bEndpointAddress;
266
267 #ifdef CONFIG_USB_G_DBGP_SERIAL
268         dbgp.serial->in = dbgp.i_ep;
269         dbgp.serial->out = dbgp.o_ep;
270
271         dbgp.serial->in_desc = &i_desc;
272         dbgp.serial->out_desc = &o_desc;
273
274         if (gserial_setup(gadget, 1) < 0) {
275                 stp = 3;
276                 goto fail_3;
277         }
278
279         return 0;
280
281 fail_3:
282         dbgp.o_ep->driver_data = NULL;
283 #else
284         return 0;
285 #endif
286 fail_2:
287         dbgp.i_ep->driver_data = NULL;
288 fail_1:
289         dev_dbg(&dbgp.gadget->dev, "ep config: failure (%d)\n", stp);
290         return -ENODEV;
291 }
292
293 static int __init dbgp_bind(struct usb_gadget *gadget)
294 {
295         int err, stp;
296
297         dbgp.gadget = gadget;
298
299         dbgp.req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
300         if (!dbgp.req) {
301                 err = -ENOMEM;
302                 stp = 1;
303                 goto fail;
304         }
305
306         dbgp.req->buf = kmalloc(DBGP_REQ_EP0_LEN, GFP_KERNEL);
307         if (!dbgp.req->buf) {
308                 err = -ENOMEM;
309                 stp = 2;
310                 goto fail;
311         }
312
313         dbgp.req->length = DBGP_REQ_EP0_LEN;
314         gadget->ep0->driver_data = gadget;
315         device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
316
317 #ifdef CONFIG_USB_G_DBGP_SERIAL
318         dbgp.serial = kzalloc(sizeof(struct gserial), GFP_KERNEL);
319         if (!dbgp.serial) {
320                 stp = 3;
321                 err = -ENOMEM;
322                 goto fail;
323         }
324 #endif
325         err = dbgp_configure_endpoints(gadget);
326         if (err < 0) {
327                 stp = 4;
328                 goto fail;
329         }
330
331         dev_dbg(&dbgp.gadget->dev, "bind: success\n");
332         return 0;
333
334 fail:
335         dev_dbg(&gadget->dev, "bind: failure (%d:%d)\n", stp, err);
336         dbgp_unbind(gadget);
337         return err;
338 }
339
340 static void dbgp_setup_complete(struct usb_ep *ep,
341                                 struct usb_request *req)
342 {
343         dev_dbg(&dbgp.gadget->dev, "setup complete: %d, %d/%d\n",
344                 req->status, req->actual, req->length);
345 }
346
347 static int dbgp_setup(struct usb_gadget *gadget,
348                       const struct usb_ctrlrequest *ctrl)
349 {
350         struct usb_request *req = dbgp.req;
351         u8 request = ctrl->bRequest;
352         u16 value = le16_to_cpu(ctrl->wValue);
353         u16 length = le16_to_cpu(ctrl->wLength);
354         int err = -EOPNOTSUPP;
355         void *data = NULL;
356         u16 len = 0;
357
358         gadget->ep0->driver_data = gadget;
359
360         if (request == USB_REQ_GET_DESCRIPTOR) {
361                 switch (value>>8) {
362                 case USB_DT_DEVICE:
363                         dev_dbg(&dbgp.gadget->dev, "setup: desc device\n");
364                         len = sizeof device_desc;
365                         data = &device_desc;
366                         break;
367                 case USB_DT_DEBUG:
368                         dev_dbg(&dbgp.gadget->dev, "setup: desc debug\n");
369                         len = sizeof dbg_desc;
370                         data = &dbg_desc;
371                         break;
372                 default:
373                         goto fail;
374                 }
375                 err = 0;
376         } else if (request == USB_REQ_SET_FEATURE &&
377                    value == USB_DEVICE_DEBUG_MODE) {
378                 dev_dbg(&dbgp.gadget->dev, "setup: feat debug\n");
379 #ifdef CONFIG_USB_G_DBGP_PRINTK
380                 err = dbgp_enable_ep();
381 #else
382                 err = gserial_connect(dbgp.serial, 0);
383 #endif
384                 if (err < 0)
385                         goto fail;
386         } else
387                 goto fail;
388
389         req->length = min(length, len);
390         req->zero = len < req->length;
391         if (data && req->length)
392                 memcpy(req->buf, data, req->length);
393
394         req->complete = dbgp_setup_complete;
395         return usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
396
397 fail:
398         dev_dbg(&dbgp.gadget->dev,
399                 "setup: failure req %x v %x\n", request, value);
400         return err;
401 }
402
403 static struct usb_gadget_driver dbgp_driver = {
404         .function = "dbgp",
405         .speed = USB_SPEED_HIGH,
406         .unbind = dbgp_unbind,
407         .setup = dbgp_setup,
408         .disconnect = dbgp_disconnect,
409         .driver = {
410                 .owner = THIS_MODULE,
411                 .name = "dbgp"
412         },
413 };
414
415 static int __init dbgp_init(void)
416 {
417         return usb_gadget_probe_driver(&dbgp_driver, dbgp_bind);
418 }
419
420 static void __exit dbgp_exit(void)
421 {
422         usb_gadget_unregister_driver(&dbgp_driver);
423 #ifdef CONFIG_USB_G_DBGP_SERIAL
424         gserial_cleanup();
425 #endif
426 }
427
428 MODULE_AUTHOR("Stephane Duverger");
429 MODULE_LICENSE("GPL");
430 module_init(dbgp_init);
431 module_exit(dbgp_exit);