stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * dbgp.c -- EHCI Debug Port device gadget |
| 3 | * |
| 4 | * Copyright (C) 2010 Stephane Duverger |
| 5 | * |
| 6 | * Released under the GPLv2. |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* verbose messages */ |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/device.h> |
Paul Gortmaker | 6eb0de8 | 2011-07-03 16:09:31 -0400 | [diff] [blame] | 12 | #include <linux/module.h> |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 13 | #include <linux/usb/ch9.h> |
| 14 | #include <linux/usb/gadget.h> |
| 15 | |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 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 | |
| 27 | static 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 | |
| 37 | static 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 | |
| 47 | static struct usb_debug_descriptor dbg_desc = { |
| 48 | .bLength = sizeof dbg_desc, |
| 49 | .bDescriptorType = USB_DT_DEBUG, |
| 50 | }; |
| 51 | |
| 52 | static 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 | |
| 59 | static 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 |
| 67 | static 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 | |
| 82 | static 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 | |
| 90 | static void dbgp_disable_ep(void) |
| 91 | { |
| 92 | __disable_ep(dbgp.i_ep); |
| 93 | __disable_ep(dbgp.o_ep); |
| 94 | } |
| 95 | |
| 96 | static 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 | |
| 123 | release_req: |
| 124 | kfree(req->buf); |
| 125 | usb_ep_free_request(dbgp.o_ep, req); |
| 126 | dbgp_disable_ep(); |
| 127 | fail: |
| 128 | dev_dbg(&dbgp.gadget->dev, |
| 129 | "complete: failure (%d:%d) ==> %d\n", stp, err, status); |
| 130 | } |
| 131 | |
| 132 | static 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 | |
| 161 | fail_3: |
| 162 | kfree(req->buf); |
| 163 | fail_2: |
| 164 | usb_ep_free_request(dbgp.o_ep, req); |
| 165 | fail_1: |
| 166 | dev_dbg(&dbgp.gadget->dev, |
| 167 | "enable ep req: failure (%d:%d)\n", stp, err); |
| 168 | return err; |
| 169 | } |
| 170 | |
| 171 | static int __enable_ep(struct usb_ep *ep, struct usb_endpoint_descriptor *desc) |
| 172 | { |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 173 | int err; |
| 174 | ep->desc = desc; |
| 175 | err = usb_ep_enable(ep); |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 176 | ep->driver_data = dbgp.gadget; |
| 177 | return err; |
| 178 | } |
| 179 | |
| 180 | static 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 | |
| 204 | fail_3: |
| 205 | __disable_ep(dbgp.o_ep); |
| 206 | fail_2: |
| 207 | __disable_ep(dbgp.i_ep); |
| 208 | fail_1: |
| 209 | dev_dbg(&dbgp.gadget->dev, "enable ep: failure (%d:%d)\n", stp, err); |
| 210 | return err; |
| 211 | } |
| 212 | #endif |
| 213 | |
| 214 | static 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 | |
| 223 | static 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 | |
| 236 | static 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 | |
Sven Schnelle | a8779ee | 2011-03-23 15:24:56 +0100 | [diff] [blame] | 263 | dbg_desc.bDebugInEndpoint = i_desc.bEndpointAddress; |
| 264 | dbg_desc.bDebugOutEndpoint = o_desc.bEndpointAddress; |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 265 | |
| 266 | #ifdef CONFIG_USB_G_DBGP_SERIAL |
| 267 | dbgp.serial->in = dbgp.i_ep; |
| 268 | dbgp.serial->out = dbgp.o_ep; |
| 269 | |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 270 | dbgp.serial->in->desc = &i_desc; |
| 271 | dbgp.serial->out->desc = &o_desc; |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 272 | |
| 273 | if (gserial_setup(gadget, 1) < 0) { |
| 274 | stp = 3; |
| 275 | goto fail_3; |
| 276 | } |
| 277 | |
| 278 | return 0; |
| 279 | |
| 280 | fail_3: |
| 281 | dbgp.o_ep->driver_data = NULL; |
| 282 | #else |
| 283 | return 0; |
| 284 | #endif |
| 285 | fail_2: |
| 286 | dbgp.i_ep->driver_data = NULL; |
| 287 | fail_1: |
| 288 | dev_dbg(&dbgp.gadget->dev, "ep config: failure (%d)\n", stp); |
| 289 | return -ENODEV; |
| 290 | } |
| 291 | |
Sebastian Andrzej Siewior | ffe0b33 | 2012-09-07 09:53:17 +0200 | [diff] [blame] | 292 | static int __init dbgp_bind(struct usb_gadget *gadget, |
| 293 | struct usb_gadget_driver *driver) |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 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 | |
| 333 | fail: |
| 334 | dev_dbg(&gadget->dev, "bind: failure (%d:%d)\n", stp, err); |
| 335 | dbgp_unbind(gadget); |
| 336 | return err; |
| 337 | } |
| 338 | |
| 339 | static 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 | |
| 346 | static 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); |
Sven Schnelle | 1744020 | 2011-03-23 21:36:17 +0100 | [diff] [blame] | 353 | int err = -EOPNOTSUPP; |
| 354 | void *data = NULL; |
| 355 | u16 len = 0; |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 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; |
Sebastian Andrzej Siewior | 765f5b8 | 2011-06-23 14:26:11 +0200 | [diff] [blame] | 365 | device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket; |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 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 | } |
Sven Schnelle | 1744020 | 2011-03-23 21:36:17 +0100 | [diff] [blame] | 375 | err = 0; |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 376 | } else if (request == USB_REQ_SET_FEATURE && |
| 377 | value == USB_DEVICE_DEBUG_MODE) { |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 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 | |
Dan Carpenter | c6a7678 | 2010-08-12 09:40:30 +0200 | [diff] [blame] | 389 | req->length = min(length, len); |
| 390 | req->zero = len < req->length; |
| 391 | if (data && req->length) |
| 392 | memcpy(req->buf, data, req->length); |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 393 | |
Dan Carpenter | c6a7678 | 2010-08-12 09:40:30 +0200 | [diff] [blame] | 394 | req->complete = dbgp_setup_complete; |
| 395 | return usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 396 | |
| 397 | fail: |
| 398 | dev_dbg(&dbgp.gadget->dev, |
| 399 | "setup: failure req %x v %x\n", request, value); |
| 400 | return err; |
| 401 | } |
| 402 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 403 | static __refdata struct usb_gadget_driver dbgp_driver = { |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 404 | .function = "dbgp", |
Michal Nazarewicz | 7177aed | 2011-11-19 18:27:38 +0100 | [diff] [blame] | 405 | .max_speed = USB_SPEED_HIGH, |
Sebastian Andrzej Siewior | 9395295 | 2012-09-06 20:11:05 +0200 | [diff] [blame] | 406 | .bind = dbgp_bind, |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 407 | .unbind = dbgp_unbind, |
| 408 | .setup = dbgp_setup, |
| 409 | .disconnect = dbgp_disconnect, |
| 410 | .driver = { |
| 411 | .owner = THIS_MODULE, |
| 412 | .name = "dbgp" |
| 413 | }, |
| 414 | }; |
| 415 | |
| 416 | static int __init dbgp_init(void) |
| 417 | { |
Sebastian Andrzej Siewior | 9395295 | 2012-09-06 20:11:05 +0200 | [diff] [blame] | 418 | return usb_gadget_probe_driver(&dbgp_driver); |
stephane duverger | f6c826a | 2010-07-12 18:37:53 +0200 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | static 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 | |
| 429 | MODULE_AUTHOR("Stephane Duverger"); |
| 430 | MODULE_LICENSE("GPL"); |
| 431 | module_init(dbgp_init); |
| 432 | module_exit(dbgp_exit); |