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