1 // SPDX-License-Identifier: GPL-2.0
3 * dbgp.c -- EHCI Debug Port device gadget
5 * Copyright (C) 2010 Stephane Duverger
7 * 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>
19 #define DRIVER_VENDOR_ID 0x0525 /* NetChip */
20 #define DRIVER_PRODUCT_ID 0xc0de /* undefined */
22 #define USB_DEBUG_MAX_PACKET_SIZE 8
23 #define DBGP_REQ_EP0_LEN 128
24 #define DBGP_REQ_LEN 512
27 struct usb_gadget
*gadget
;
28 struct usb_request
*req
;
31 #ifdef CONFIG_USB_G_DBGP_SERIAL
32 struct gserial
*serial
;
36 static struct usb_device_descriptor device_desc
= {
37 .bLength
= sizeof device_desc
,
38 .bDescriptorType
= USB_DT_DEVICE
,
39 .bcdUSB
= cpu_to_le16(0x0200),
40 .bDeviceClass
= USB_CLASS_VENDOR_SPEC
,
41 .idVendor
= cpu_to_le16(DRIVER_VENDOR_ID
),
42 .idProduct
= cpu_to_le16(DRIVER_PRODUCT_ID
),
43 .bNumConfigurations
= 1,
46 static struct usb_debug_descriptor dbg_desc
= {
47 .bLength
= sizeof dbg_desc
,
48 .bDescriptorType
= USB_DT_DEBUG
,
51 static struct usb_endpoint_descriptor i_desc
= {
52 .bLength
= USB_DT_ENDPOINT_SIZE
,
53 .bDescriptorType
= USB_DT_ENDPOINT
,
54 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
55 .bEndpointAddress
= USB_DIR_IN
,
58 static struct usb_endpoint_descriptor o_desc
= {
59 .bLength
= USB_DT_ENDPOINT_SIZE
,
60 .bDescriptorType
= USB_DT_ENDPOINT
,
61 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
62 .bEndpointAddress
= USB_DIR_OUT
,
65 #ifdef CONFIG_USB_G_DBGP_PRINTK
66 static int dbgp_consume(char *buf
, unsigned len
)
77 printk(KERN_NOTICE
"%s%c", buf
, c
);
81 static void __disable_ep(struct usb_ep
*ep
)
86 static void dbgp_disable_ep(void)
88 __disable_ep(dbgp
.i_ep
);
89 __disable_ep(dbgp
.o_ep
);
92 static void dbgp_complete(struct usb_ep
*ep
, struct usb_request
*req
)
96 int status
= req
->status
;
98 if (ep
== dbgp
.i_ep
) {
108 dbgp_consume(req
->buf
, req
->actual
);
110 req
->length
= DBGP_REQ_LEN
;
111 err
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
121 usb_ep_free_request(dbgp
.o_ep
, req
);
124 dev_dbg(&dbgp
.gadget
->dev
,
125 "complete: failure (%d:%d) ==> %d\n", stp
, err
, status
);
128 static int dbgp_enable_ep_req(struct usb_ep
*ep
)
131 struct usb_request
*req
;
133 req
= usb_ep_alloc_request(ep
, GFP_KERNEL
);
140 req
->buf
= kmalloc(DBGP_REQ_LEN
, GFP_KERNEL
);
147 req
->complete
= dbgp_complete
;
148 req
->length
= DBGP_REQ_LEN
;
149 err
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
160 usb_ep_free_request(dbgp
.o_ep
, req
);
162 dev_dbg(&dbgp
.gadget
->dev
,
163 "enable ep req: failure (%d:%d)\n", stp
, err
);
167 static int __enable_ep(struct usb_ep
*ep
, struct usb_endpoint_descriptor
*desc
)
171 err
= usb_ep_enable(ep
);
175 static int dbgp_enable_ep(void)
179 err
= __enable_ep(dbgp
.i_ep
, &i_desc
);
185 err
= __enable_ep(dbgp
.o_ep
, &o_desc
);
191 err
= dbgp_enable_ep_req(dbgp
.o_ep
);
200 __disable_ep(dbgp
.o_ep
);
202 __disable_ep(dbgp
.i_ep
);
204 dev_dbg(&dbgp
.gadget
->dev
, "enable ep: failure (%d:%d)\n", stp
, err
);
209 static void dbgp_disconnect(struct usb_gadget
*gadget
)
211 #ifdef CONFIG_USB_G_DBGP_PRINTK
214 gserial_disconnect(dbgp
.serial
);
218 static void dbgp_unbind(struct usb_gadget
*gadget
)
220 #ifdef CONFIG_USB_G_DBGP_SERIAL
225 kfree(dbgp
.req
->buf
);
226 usb_ep_free_request(gadget
->ep0
, dbgp
.req
);
231 #ifdef CONFIG_USB_G_DBGP_SERIAL
232 static unsigned char tty_line
;
235 static int dbgp_configure_endpoints(struct usb_gadget
*gadget
)
239 usb_ep_autoconfig_reset(gadget
);
241 dbgp
.i_ep
= usb_ep_autoconfig(gadget
, &i_desc
);
247 i_desc
.wMaxPacketSize
=
248 cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE
);
250 dbgp
.o_ep
= usb_ep_autoconfig(gadget
, &o_desc
);
256 o_desc
.wMaxPacketSize
=
257 cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE
);
259 dbg_desc
.bDebugInEndpoint
= i_desc
.bEndpointAddress
;
260 dbg_desc
.bDebugOutEndpoint
= o_desc
.bEndpointAddress
;
262 #ifdef CONFIG_USB_G_DBGP_SERIAL
263 dbgp
.serial
->in
= dbgp
.i_ep
;
264 dbgp
.serial
->out
= dbgp
.o_ep
;
266 dbgp
.serial
->in
->desc
= &i_desc
;
267 dbgp
.serial
->out
->desc
= &o_desc
;
273 dev_dbg(&dbgp
.gadget
->dev
, "ep config: failure (%d)\n", stp
);
277 static int dbgp_bind(struct usb_gadget
*gadget
,
278 struct usb_gadget_driver
*driver
)
282 dbgp
.gadget
= gadget
;
284 dbgp
.req
= usb_ep_alloc_request(gadget
->ep0
, GFP_KERNEL
);
291 dbgp
.req
->buf
= kmalloc(DBGP_REQ_EP0_LEN
, GFP_KERNEL
);
292 if (!dbgp
.req
->buf
) {
298 dbgp
.req
->length
= DBGP_REQ_EP0_LEN
;
300 #ifdef CONFIG_USB_G_DBGP_SERIAL
301 dbgp
.serial
= kzalloc(sizeof(struct gserial
), GFP_KERNEL
);
308 if (gserial_alloc_line(&tty_line
)) {
315 err
= dbgp_configure_endpoints(gadget
);
321 dev_dbg(&dbgp
.gadget
->dev
, "bind: success\n");
325 dev_dbg(&gadget
->dev
, "bind: failure (%d:%d)\n", stp
, err
);
330 static void dbgp_setup_complete(struct usb_ep
*ep
,
331 struct usb_request
*req
)
333 dev_dbg(&dbgp
.gadget
->dev
, "setup complete: %d, %d/%d\n",
334 req
->status
, req
->actual
, req
->length
);
337 static int dbgp_setup(struct usb_gadget
*gadget
,
338 const struct usb_ctrlrequest
*ctrl
)
340 struct usb_request
*req
= dbgp
.req
;
341 u8 request
= ctrl
->bRequest
;
342 u16 value
= le16_to_cpu(ctrl
->wValue
);
343 u16 length
= le16_to_cpu(ctrl
->wLength
);
344 int err
= -EOPNOTSUPP
;
348 if (request
== USB_REQ_GET_DESCRIPTOR
) {
351 dev_dbg(&dbgp
.gadget
->dev
, "setup: desc device\n");
352 len
= sizeof device_desc
;
354 device_desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
357 dev_dbg(&dbgp
.gadget
->dev
, "setup: desc debug\n");
358 len
= sizeof dbg_desc
;
365 } else if (request
== USB_REQ_SET_FEATURE
&&
366 value
== USB_DEVICE_DEBUG_MODE
) {
367 dev_dbg(&dbgp
.gadget
->dev
, "setup: feat debug\n");
368 #ifdef CONFIG_USB_G_DBGP_PRINTK
369 err
= dbgp_enable_ep();
371 err
= dbgp_configure_endpoints(gadget
);
375 err
= gserial_connect(dbgp
.serial
, tty_line
);
382 req
->length
= min(length
, len
);
383 req
->zero
= len
< req
->length
;
384 if (data
&& req
->length
)
385 memcpy(req
->buf
, data
, req
->length
);
387 req
->complete
= dbgp_setup_complete
;
388 return usb_ep_queue(gadget
->ep0
, req
, GFP_ATOMIC
);
391 dev_dbg(&dbgp
.gadget
->dev
,
392 "setup: failure req %x v %x\n", request
, value
);
396 static struct usb_gadget_driver dbgp_driver
= {
398 .max_speed
= USB_SPEED_HIGH
,
400 .unbind
= dbgp_unbind
,
402 .reset
= dbgp_disconnect
,
403 .disconnect
= dbgp_disconnect
,
405 .owner
= THIS_MODULE
,
410 static int __init
dbgp_init(void)
412 return usb_gadget_probe_driver(&dbgp_driver
);
415 static void __exit
dbgp_exit(void)
417 usb_gadget_unregister_driver(&dbgp_driver
);
418 #ifdef CONFIG_USB_G_DBGP_SERIAL
419 gserial_free_line(tty_line
);
423 MODULE_AUTHOR("Stephane Duverger");
424 MODULE_LICENSE("GPL");
425 module_init(dbgp_init
);
426 module_exit(dbgp_exit
);