2 * dbgp.c -- EHCI Debug Port device gadget
4 * Copyright (C) 2010 Stephane Duverger
6 * Released under the GPLv2.
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/usb/ch9.h>
13 #include <linux/usb/gadget.h>
15 /* See comments in "zero.c" */
16 #include "epautoconf.c"
18 #ifdef CONFIG_USB_G_DBGP_SERIAL
22 #define DRIVER_VENDOR_ID 0x0525 /* NetChip */
23 #define DRIVER_PRODUCT_ID 0xc0de /* undefined */
25 #define USB_DEBUG_MAX_PACKET_SIZE 8
26 #define DBGP_REQ_EP0_LEN 128
27 #define DBGP_REQ_LEN 512
30 struct usb_gadget *gadget;
31 struct usb_request *req;
34 #ifdef CONFIG_USB_G_DBGP_SERIAL
35 struct gserial *serial;
39 static struct usb_device_descriptor device_desc = {
40 .bLength = sizeof device_desc,
41 .bDescriptorType = USB_DT_DEVICE,
42 .bcdUSB = __constant_cpu_to_le16(0x0200),
43 .bDeviceClass = USB_CLASS_VENDOR_SPEC,
44 .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_ID),
45 .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_ID),
46 .bNumConfigurations = 1,
49 static struct usb_debug_descriptor dbg_desc = {
50 .bLength = sizeof dbg_desc,
51 .bDescriptorType = USB_DT_DEBUG,
54 static struct usb_endpoint_descriptor i_desc = {
55 .bLength = USB_DT_ENDPOINT_SIZE,
56 .bDescriptorType = USB_DT_ENDPOINT,
57 .bmAttributes = USB_ENDPOINT_XFER_BULK,
58 .bEndpointAddress = USB_DIR_IN,
61 static struct usb_endpoint_descriptor o_desc = {
62 .bLength = USB_DT_ENDPOINT_SIZE,
63 .bDescriptorType = USB_DT_ENDPOINT,
64 .bmAttributes = USB_ENDPOINT_XFER_BULK,
65 .bEndpointAddress = USB_DIR_OUT,
68 #ifdef CONFIG_USB_G_DBGP_PRINTK
69 static int dbgp_consume(char *buf, unsigned len)
80 printk(KERN_NOTICE "%s%c", buf, c);
84 static void __disable_ep(struct usb_ep *ep)
86 if (ep && ep->driver_data == dbgp.gadget) {
88 ep->driver_data = NULL;
92 static void dbgp_disable_ep(void)
94 __disable_ep(dbgp.i_ep);
95 __disable_ep(dbgp.o_ep);
98 static void dbgp_complete(struct usb_ep *ep, struct usb_request *req)
102 int status = req->status;
104 if (ep == dbgp.i_ep) {
114 dbgp_consume(req->buf, req->actual);
116 req->length = DBGP_REQ_LEN;
117 err = usb_ep_queue(ep, req, GFP_ATOMIC);
127 usb_ep_free_request(dbgp.o_ep, req);
130 dev_dbg(&dbgp.gadget->dev,
131 "complete: failure (%d:%d) ==> %d\n", stp, err, status);
134 static int dbgp_enable_ep_req(struct usb_ep *ep)
137 struct usb_request *req;
139 req = usb_ep_alloc_request(ep, GFP_KERNEL);
146 req->buf = kmalloc(DBGP_REQ_LEN, GFP_KERNEL);
153 req->complete = dbgp_complete;
154 req->length = DBGP_REQ_LEN;
155 err = usb_ep_queue(ep, req, GFP_ATOMIC);
166 usb_ep_free_request(dbgp.o_ep, req);
168 dev_dbg(&dbgp.gadget->dev,
169 "enable ep req: failure (%d:%d)\n", stp, err);
173 static int __enable_ep(struct usb_ep *ep, struct usb_endpoint_descriptor *desc)
177 err = usb_ep_enable(ep);
178 ep->driver_data = dbgp.gadget;
182 static int dbgp_enable_ep(void)
186 err = __enable_ep(dbgp.i_ep, &i_desc);
192 err = __enable_ep(dbgp.o_ep, &o_desc);
198 err = dbgp_enable_ep_req(dbgp.o_ep);
207 __disable_ep(dbgp.o_ep);
209 __disable_ep(dbgp.i_ep);
211 dev_dbg(&dbgp.gadget->dev, "enable ep: failure (%d:%d)\n", stp, err);
216 static void dbgp_disconnect(struct usb_gadget *gadget)
218 #ifdef CONFIG_USB_G_DBGP_PRINTK
221 gserial_disconnect(dbgp.serial);
225 static void dbgp_unbind(struct usb_gadget *gadget)
227 #ifdef CONFIG_USB_G_DBGP_SERIAL
231 kfree(dbgp.req->buf);
232 usb_ep_free_request(gadget->ep0, dbgp.req);
235 gadget->ep0->driver_data = NULL;
238 static int __init dbgp_configure_endpoints(struct usb_gadget *gadget)
242 usb_ep_autoconfig_reset(gadget);
244 dbgp.i_ep = usb_ep_autoconfig(gadget, &i_desc);
250 dbgp.i_ep->driver_data = gadget;
251 i_desc.wMaxPacketSize =
252 __constant_cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
254 dbgp.o_ep = usb_ep_autoconfig(gadget, &o_desc);
256 dbgp.i_ep->driver_data = NULL;
261 dbgp.o_ep->driver_data = gadget;
262 o_desc.wMaxPacketSize =
263 __constant_cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
265 dbg_desc.bDebugInEndpoint = i_desc.bEndpointAddress;
266 dbg_desc.bDebugOutEndpoint = o_desc.bEndpointAddress;
268 #ifdef CONFIG_USB_G_DBGP_SERIAL
269 dbgp.serial->in = dbgp.i_ep;
270 dbgp.serial->out = dbgp.o_ep;
272 dbgp.serial->in->desc = &i_desc;
273 dbgp.serial->out->desc = &o_desc;
275 if (gserial_setup(gadget, 1) < 0) {
283 dbgp.o_ep->driver_data = NULL;
288 dbgp.i_ep->driver_data = NULL;
290 dev_dbg(&dbgp.gadget->dev, "ep config: failure (%d)\n", stp);
294 static int __init dbgp_bind(struct usb_gadget *gadget)
298 dbgp.gadget = gadget;
300 dbgp.req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
307 dbgp.req->buf = kmalloc(DBGP_REQ_EP0_LEN, GFP_KERNEL);
308 if (!dbgp.req->buf) {
314 dbgp.req->length = DBGP_REQ_EP0_LEN;
315 gadget->ep0->driver_data = gadget;
317 #ifdef CONFIG_USB_G_DBGP_SERIAL
318 dbgp.serial = kzalloc(sizeof(struct gserial), GFP_KERNEL);
325 err = dbgp_configure_endpoints(gadget);
331 dev_dbg(&dbgp.gadget->dev, "bind: success\n");
335 dev_dbg(&gadget->dev, "bind: failure (%d:%d)\n", stp, err);
340 static void dbgp_setup_complete(struct usb_ep *ep,
341 struct usb_request *req)
343 dev_dbg(&dbgp.gadget->dev, "setup complete: %d, %d/%d\n",
344 req->status, req->actual, req->length);
347 static int dbgp_setup(struct usb_gadget *gadget,
348 const struct usb_ctrlrequest *ctrl)
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;
358 gadget->ep0->driver_data = gadget;
360 if (request == USB_REQ_GET_DESCRIPTOR) {
363 dev_dbg(&dbgp.gadget->dev, "setup: desc device\n");
364 len = sizeof device_desc;
366 device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
369 dev_dbg(&dbgp.gadget->dev, "setup: desc debug\n");
370 len = sizeof dbg_desc;
377 } else if (request == USB_REQ_SET_FEATURE &&
378 value == USB_DEVICE_DEBUG_MODE) {
379 dev_dbg(&dbgp.gadget->dev, "setup: feat debug\n");
380 #ifdef CONFIG_USB_G_DBGP_PRINTK
381 err = dbgp_enable_ep();
383 err = gserial_connect(dbgp.serial, 0);
390 req->length = min(length, len);
391 req->zero = len < req->length;
392 if (data && req->length)
393 memcpy(req->buf, data, req->length);
395 req->complete = dbgp_setup_complete;
396 return usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
399 dev_dbg(&dbgp.gadget->dev,
400 "setup: failure req %x v %x\n", request, value);
404 static struct usb_gadget_driver dbgp_driver = {
406 .speed = USB_SPEED_HIGH,
407 .unbind = dbgp_unbind,
409 .disconnect = dbgp_disconnect,
411 .owner = THIS_MODULE,
416 static int __init dbgp_init(void)
418 return usb_gadget_probe_driver(&dbgp_driver, dbgp_bind);
421 static void __exit dbgp_exit(void)
423 usb_gadget_unregister_driver(&dbgp_driver);
424 #ifdef CONFIG_USB_G_DBGP_SERIAL
429 MODULE_AUTHOR("Stephane Duverger");
430 MODULE_LICENSE("GPL");
431 module_init(dbgp_init);
432 module_exit(dbgp_exit);