2 * dbgp.c -- EHCI Debug Port device gadget
4 * Copyright (C) 2010 Stephane Duverger
6 * Released under the GPLv2.
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
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
32 struct usb_gadget
*gadget
;
33 struct usb_request
*req
;
36 #ifdef CONFIG_USB_G_DBGP_SERIAL
37 struct gserial
*serial
;
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
)
82 printk(KERN_NOTICE
"%s%c", buf
, c
);
86 static void __disable_ep(struct usb_ep
*ep
)
88 if (ep
&& ep
->driver_data
== dbgp
.gadget
) {
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
)
104 int status
= req
->status
;
106 if (ep
== dbgp
.i_ep
) {
116 dbgp_consume(req
->buf
, req
->actual
);
118 req
->length
= DBGP_REQ_LEN
;
119 err
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
129 usb_ep_free_request(dbgp
.o_ep
, req
);
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
)
139 struct usb_request
*req
;
141 req
= usb_ep_alloc_request(ep
, GFP_KERNEL
);
148 req
->buf
= kmalloc(DBGP_REQ_LEN
, GFP_KERNEL
);
155 req
->complete
= dbgp_complete
;
156 req
->length
= DBGP_REQ_LEN
;
157 err
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
168 usb_ep_free_request(dbgp
.o_ep
, req
);
170 dev_dbg(&dbgp
.gadget
->dev
,
171 "enable ep req: failure (%d:%d)\n", stp
, err
);
175 static int __enable_ep(struct usb_ep
*ep
, struct usb_endpoint_descriptor
*desc
)
179 err
= usb_ep_enable(ep
);
180 ep
->driver_data
= dbgp
.gadget
;
184 static int dbgp_enable_ep(void)
188 err
= __enable_ep(dbgp
.i_ep
, &i_desc
);
194 err
= __enable_ep(dbgp
.o_ep
, &o_desc
);
200 err
= dbgp_enable_ep_req(dbgp
.o_ep
);
209 __disable_ep(dbgp
.o_ep
);
211 __disable_ep(dbgp
.i_ep
);
213 dev_dbg(&dbgp
.gadget
->dev
, "enable ep: failure (%d:%d)\n", stp
, err
);
218 static void dbgp_disconnect(struct usb_gadget
*gadget
)
220 #ifdef CONFIG_USB_G_DBGP_PRINTK
223 gserial_disconnect(dbgp
.serial
);
227 static void dbgp_unbind(struct usb_gadget
*gadget
)
229 #ifdef CONFIG_USB_G_DBGP_SERIAL
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
)
244 usb_ep_autoconfig_reset(gadget
);
246 dbgp
.i_ep
= usb_ep_autoconfig(gadget
, &i_desc
);
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
);
258 dbgp
.i_ep
->driver_data
= NULL
;
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) {
285 dbgp
.o_ep
->driver_data
= NULL
;
290 dbgp
.i_ep
->driver_data
= NULL
;
292 dev_dbg(&dbgp
.gadget
->dev
, "ep config: failure (%d)\n", stp
);
296 static int __init
dbgp_bind(struct usb_gadget
*gadget
)
300 dbgp
.gadget
= gadget
;
302 dbgp
.req
= usb_ep_alloc_request(gadget
->ep0
, GFP_KERNEL
);
309 dbgp
.req
->buf
= kmalloc(DBGP_REQ_EP0_LEN
, GFP_KERNEL
);
310 if (!dbgp
.req
->buf
) {
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
);
327 err
= dbgp_configure_endpoints(gadget
);
333 dev_dbg(&dbgp
.gadget
->dev
, "bind: success\n");
337 dev_dbg(&gadget
->dev
, "bind: failure (%d:%d)\n", stp
, 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
;
360 gadget
->ep0
->driver_data
= gadget
;
362 if (request
== USB_REQ_GET_DESCRIPTOR
) {
365 dev_dbg(&dbgp
.gadget
->dev
, "setup: desc device\n");
366 len
= sizeof device_desc
;
368 device_desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
371 dev_dbg(&dbgp
.gadget
->dev
, "setup: desc debug\n");
372 len
= sizeof dbg_desc
;
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();
385 err
= gserial_connect(dbgp
.serial
, 0);
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
);
401 dev_dbg(&dbgp
.gadget
->dev
,
402 "setup: failure req %x v %x\n", request
, value
);
406 static struct usb_gadget_driver dbgp_driver
= {
408 .speed
= USB_SPEED_HIGH
,
409 .unbind
= dbgp_unbind
,
411 .disconnect
= dbgp_disconnect
,
413 .owner
= THIS_MODULE
,
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
431 MODULE_AUTHOR("Stephane Duverger");
432 MODULE_LICENSE("GPL");
433 module_init(dbgp_init
);
434 module_exit(dbgp_exit
);