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/module.h>
13 #include <linux/usb/ch9.h>
14 #include <linux/usb/gadget.h>
16 /* See comments in "zero.c" */
17 #include "epautoconf.c"
19 #ifdef CONFIG_USB_G_DBGP_SERIAL
23 #define DRIVER_VENDOR_ID 0x0525 /* NetChip */
24 #define DRIVER_PRODUCT_ID 0xc0de /* undefined */
26 #define USB_DEBUG_MAX_PACKET_SIZE 8
27 #define DBGP_REQ_EP0_LEN 128
28 #define DBGP_REQ_LEN 512
31 struct usb_gadget
*gadget
;
32 struct usb_request
*req
;
35 #ifdef CONFIG_USB_G_DBGP_SERIAL
36 struct gserial
*serial
;
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,
50 static struct usb_debug_descriptor dbg_desc
= {
51 .bLength
= sizeof dbg_desc
,
52 .bDescriptorType
= USB_DT_DEBUG
,
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
,
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
,
69 #ifdef CONFIG_USB_G_DBGP_PRINTK
70 static int dbgp_consume(char *buf
, unsigned len
)
81 printk(KERN_NOTICE
"%s%c", buf
, c
);
85 static void __disable_ep(struct usb_ep
*ep
)
87 if (ep
&& ep
->driver_data
== dbgp
.gadget
) {
89 ep
->driver_data
= NULL
;
93 static void dbgp_disable_ep(void)
95 __disable_ep(dbgp
.i_ep
);
96 __disable_ep(dbgp
.o_ep
);
99 static void dbgp_complete(struct usb_ep
*ep
, struct usb_request
*req
)
103 int status
= req
->status
;
105 if (ep
== dbgp
.i_ep
) {
115 dbgp_consume(req
->buf
, req
->actual
);
117 req
->length
= DBGP_REQ_LEN
;
118 err
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
128 usb_ep_free_request(dbgp
.o_ep
, req
);
131 dev_dbg(&dbgp
.gadget
->dev
,
132 "complete: failure (%d:%d) ==> %d\n", stp
, err
, status
);
135 static int dbgp_enable_ep_req(struct usb_ep
*ep
)
138 struct usb_request
*req
;
140 req
= usb_ep_alloc_request(ep
, GFP_KERNEL
);
147 req
->buf
= kmalloc(DBGP_REQ_LEN
, GFP_KERNEL
);
154 req
->complete
= dbgp_complete
;
155 req
->length
= DBGP_REQ_LEN
;
156 err
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
167 usb_ep_free_request(dbgp
.o_ep
, req
);
169 dev_dbg(&dbgp
.gadget
->dev
,
170 "enable ep req: failure (%d:%d)\n", stp
, err
);
174 static int __enable_ep(struct usb_ep
*ep
, struct usb_endpoint_descriptor
*desc
)
178 err
= usb_ep_enable(ep
);
179 ep
->driver_data
= dbgp
.gadget
;
183 static int dbgp_enable_ep(void)
187 err
= __enable_ep(dbgp
.i_ep
, &i_desc
);
193 err
= __enable_ep(dbgp
.o_ep
, &o_desc
);
199 err
= dbgp_enable_ep_req(dbgp
.o_ep
);
208 __disable_ep(dbgp
.o_ep
);
210 __disable_ep(dbgp
.i_ep
);
212 dev_dbg(&dbgp
.gadget
->dev
, "enable ep: failure (%d:%d)\n", stp
, err
);
217 static void dbgp_disconnect(struct usb_gadget
*gadget
)
219 #ifdef CONFIG_USB_G_DBGP_PRINTK
222 gserial_disconnect(dbgp
.serial
);
226 static void dbgp_unbind(struct usb_gadget
*gadget
)
228 #ifdef CONFIG_USB_G_DBGP_SERIAL
232 kfree(dbgp
.req
->buf
);
233 usb_ep_free_request(gadget
->ep0
, dbgp
.req
);
236 gadget
->ep0
->driver_data
= NULL
;
239 static int __init
dbgp_configure_endpoints(struct usb_gadget
*gadget
)
243 usb_ep_autoconfig_reset(gadget
);
245 dbgp
.i_ep
= usb_ep_autoconfig(gadget
, &i_desc
);
251 dbgp
.i_ep
->driver_data
= gadget
;
252 i_desc
.wMaxPacketSize
=
253 __constant_cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE
);
255 dbgp
.o_ep
= usb_ep_autoconfig(gadget
, &o_desc
);
257 dbgp
.i_ep
->driver_data
= NULL
;
262 dbgp
.o_ep
->driver_data
= gadget
;
263 o_desc
.wMaxPacketSize
=
264 __constant_cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE
);
266 dbg_desc
.bDebugInEndpoint
= i_desc
.bEndpointAddress
;
267 dbg_desc
.bDebugOutEndpoint
= o_desc
.bEndpointAddress
;
269 #ifdef CONFIG_USB_G_DBGP_SERIAL
270 dbgp
.serial
->in
= dbgp
.i_ep
;
271 dbgp
.serial
->out
= dbgp
.o_ep
;
273 dbgp
.serial
->in
->desc
= &i_desc
;
274 dbgp
.serial
->out
->desc
= &o_desc
;
276 if (gserial_setup(gadget
, 1) < 0) {
284 dbgp
.o_ep
->driver_data
= NULL
;
289 dbgp
.i_ep
->driver_data
= NULL
;
291 dev_dbg(&dbgp
.gadget
->dev
, "ep config: failure (%d)\n", stp
);
295 static int __init
dbgp_bind(struct usb_gadget
*gadget
)
299 dbgp
.gadget
= gadget
;
301 dbgp
.req
= usb_ep_alloc_request(gadget
->ep0
, GFP_KERNEL
);
308 dbgp
.req
->buf
= kmalloc(DBGP_REQ_EP0_LEN
, GFP_KERNEL
);
309 if (!dbgp
.req
->buf
) {
315 dbgp
.req
->length
= DBGP_REQ_EP0_LEN
;
316 gadget
->ep0
->driver_data
= gadget
;
318 #ifdef CONFIG_USB_G_DBGP_SERIAL
319 dbgp
.serial
= kzalloc(sizeof(struct gserial
), GFP_KERNEL
);
326 err
= dbgp_configure_endpoints(gadget
);
332 dev_dbg(&dbgp
.gadget
->dev
, "bind: success\n");
336 dev_dbg(&gadget
->dev
, "bind: failure (%d:%d)\n", stp
, err
);
341 static void dbgp_setup_complete(struct usb_ep
*ep
,
342 struct usb_request
*req
)
344 dev_dbg(&dbgp
.gadget
->dev
, "setup complete: %d, %d/%d\n",
345 req
->status
, req
->actual
, req
->length
);
348 static int dbgp_setup(struct usb_gadget
*gadget
,
349 const struct usb_ctrlrequest
*ctrl
)
351 struct usb_request
*req
= dbgp
.req
;
352 u8 request
= ctrl
->bRequest
;
353 u16 value
= le16_to_cpu(ctrl
->wValue
);
354 u16 length
= le16_to_cpu(ctrl
->wLength
);
355 int err
= -EOPNOTSUPP
;
359 gadget
->ep0
->driver_data
= gadget
;
361 if (request
== USB_REQ_GET_DESCRIPTOR
) {
364 dev_dbg(&dbgp
.gadget
->dev
, "setup: desc device\n");
365 len
= sizeof device_desc
;
367 device_desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
370 dev_dbg(&dbgp
.gadget
->dev
, "setup: desc debug\n");
371 len
= sizeof dbg_desc
;
378 } else if (request
== USB_REQ_SET_FEATURE
&&
379 value
== USB_DEVICE_DEBUG_MODE
) {
380 dev_dbg(&dbgp
.gadget
->dev
, "setup: feat debug\n");
381 #ifdef CONFIG_USB_G_DBGP_PRINTK
382 err
= dbgp_enable_ep();
384 err
= gserial_connect(dbgp
.serial
, 0);
391 req
->length
= min(length
, len
);
392 req
->zero
= len
< req
->length
;
393 if (data
&& req
->length
)
394 memcpy(req
->buf
, data
, req
->length
);
396 req
->complete
= dbgp_setup_complete
;
397 return usb_ep_queue(gadget
->ep0
, req
, GFP_ATOMIC
);
400 dev_dbg(&dbgp
.gadget
->dev
,
401 "setup: failure req %x v %x\n", request
, value
);
405 static struct usb_gadget_driver dbgp_driver
= {
407 .max_speed
= USB_SPEED_HIGH
,
408 .unbind
= dbgp_unbind
,
410 .disconnect
= dbgp_disconnect
,
412 .owner
= THIS_MODULE
,
417 static int __init
dbgp_init(void)
419 return usb_gadget_probe_driver(&dbgp_driver
, dbgp_bind
);
422 static void __exit
dbgp_exit(void)
424 usb_gadget_unregister_driver(&dbgp_driver
);
425 #ifdef CONFIG_USB_G_DBGP_SERIAL
430 MODULE_AUTHOR("Stephane Duverger");
431 MODULE_LICENSE("GPL");
432 module_init(dbgp_init
);
433 module_exit(dbgp_exit
);