at v3.8 11 kB view raw
1/* 2 * f_loopback.c - USB peripheral loopback configuration driver 3 * 4 * Copyright (C) 2003-2008 David Brownell 5 * Copyright (C) 2008 by Nokia Corporation 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 */ 12 13/* #define VERBOSE_DEBUG */ 14 15#include <linux/slab.h> 16#include <linux/kernel.h> 17#include <linux/device.h> 18 19#include "g_zero.h" 20#include "gadget_chips.h" 21 22 23/* 24 * LOOPBACK FUNCTION ... a testing vehicle for USB peripherals, 25 * 26 * This takes messages of various sizes written OUT to a device, and loops 27 * them back so they can be read IN from it. It has been used by certain 28 * test applications. It supports limited testing of data queueing logic. 29 * 30 * 31 * This is currently packaged as a configuration driver, which can't be 32 * combined with other functions to make composite devices. However, it 33 * can be combined with other independent configurations. 34 */ 35struct f_loopback { 36 struct usb_function function; 37 38 struct usb_ep *in_ep; 39 struct usb_ep *out_ep; 40}; 41 42static inline struct f_loopback *func_to_loop(struct usb_function *f) 43{ 44 return container_of(f, struct f_loopback, function); 45} 46 47static unsigned qlen = 32; 48module_param(qlen, uint, 0); 49MODULE_PARM_DESC(qlenn, "depth of loopback queue"); 50 51/*-------------------------------------------------------------------------*/ 52 53static struct usb_interface_descriptor loopback_intf = { 54 .bLength = sizeof loopback_intf, 55 .bDescriptorType = USB_DT_INTERFACE, 56 57 .bNumEndpoints = 2, 58 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, 59 /* .iInterface = DYNAMIC */ 60}; 61 62/* full speed support: */ 63 64static struct usb_endpoint_descriptor fs_loop_source_desc = { 65 .bLength = USB_DT_ENDPOINT_SIZE, 66 .bDescriptorType = USB_DT_ENDPOINT, 67 68 .bEndpointAddress = USB_DIR_IN, 69 .bmAttributes = USB_ENDPOINT_XFER_BULK, 70}; 71 72static struct usb_endpoint_descriptor fs_loop_sink_desc = { 73 .bLength = USB_DT_ENDPOINT_SIZE, 74 .bDescriptorType = USB_DT_ENDPOINT, 75 76 .bEndpointAddress = USB_DIR_OUT, 77 .bmAttributes = USB_ENDPOINT_XFER_BULK, 78}; 79 80static struct usb_descriptor_header *fs_loopback_descs[] = { 81 (struct usb_descriptor_header *) &loopback_intf, 82 (struct usb_descriptor_header *) &fs_loop_sink_desc, 83 (struct usb_descriptor_header *) &fs_loop_source_desc, 84 NULL, 85}; 86 87/* high speed support: */ 88 89static struct usb_endpoint_descriptor hs_loop_source_desc = { 90 .bLength = USB_DT_ENDPOINT_SIZE, 91 .bDescriptorType = USB_DT_ENDPOINT, 92 93 .bmAttributes = USB_ENDPOINT_XFER_BULK, 94 .wMaxPacketSize = cpu_to_le16(512), 95}; 96 97static struct usb_endpoint_descriptor hs_loop_sink_desc = { 98 .bLength = USB_DT_ENDPOINT_SIZE, 99 .bDescriptorType = USB_DT_ENDPOINT, 100 101 .bmAttributes = USB_ENDPOINT_XFER_BULK, 102 .wMaxPacketSize = cpu_to_le16(512), 103}; 104 105static struct usb_descriptor_header *hs_loopback_descs[] = { 106 (struct usb_descriptor_header *) &loopback_intf, 107 (struct usb_descriptor_header *) &hs_loop_source_desc, 108 (struct usb_descriptor_header *) &hs_loop_sink_desc, 109 NULL, 110}; 111 112/* super speed support: */ 113 114static struct usb_endpoint_descriptor ss_loop_source_desc = { 115 .bLength = USB_DT_ENDPOINT_SIZE, 116 .bDescriptorType = USB_DT_ENDPOINT, 117 118 .bmAttributes = USB_ENDPOINT_XFER_BULK, 119 .wMaxPacketSize = cpu_to_le16(1024), 120}; 121 122struct usb_ss_ep_comp_descriptor ss_loop_source_comp_desc = { 123 .bLength = USB_DT_SS_EP_COMP_SIZE, 124 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 125 .bMaxBurst = 0, 126 .bmAttributes = 0, 127 .wBytesPerInterval = 0, 128}; 129 130static struct usb_endpoint_descriptor ss_loop_sink_desc = { 131 .bLength = USB_DT_ENDPOINT_SIZE, 132 .bDescriptorType = USB_DT_ENDPOINT, 133 134 .bmAttributes = USB_ENDPOINT_XFER_BULK, 135 .wMaxPacketSize = cpu_to_le16(1024), 136}; 137 138struct usb_ss_ep_comp_descriptor ss_loop_sink_comp_desc = { 139 .bLength = USB_DT_SS_EP_COMP_SIZE, 140 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 141 .bMaxBurst = 0, 142 .bmAttributes = 0, 143 .wBytesPerInterval = 0, 144}; 145 146static struct usb_descriptor_header *ss_loopback_descs[] = { 147 (struct usb_descriptor_header *) &loopback_intf, 148 (struct usb_descriptor_header *) &ss_loop_source_desc, 149 (struct usb_descriptor_header *) &ss_loop_source_comp_desc, 150 (struct usb_descriptor_header *) &ss_loop_sink_desc, 151 (struct usb_descriptor_header *) &ss_loop_sink_comp_desc, 152 NULL, 153}; 154 155/* function-specific strings: */ 156 157static struct usb_string strings_loopback[] = { 158 [0].s = "loop input to output", 159 { } /* end of list */ 160}; 161 162static struct usb_gadget_strings stringtab_loop = { 163 .language = 0x0409, /* en-us */ 164 .strings = strings_loopback, 165}; 166 167static struct usb_gadget_strings *loopback_strings[] = { 168 &stringtab_loop, 169 NULL, 170}; 171 172/*-------------------------------------------------------------------------*/ 173 174static int __init 175loopback_bind(struct usb_configuration *c, struct usb_function *f) 176{ 177 struct usb_composite_dev *cdev = c->cdev; 178 struct f_loopback *loop = func_to_loop(f); 179 int id; 180 int ret; 181 182 /* allocate interface ID(s) */ 183 id = usb_interface_id(c, f); 184 if (id < 0) 185 return id; 186 loopback_intf.bInterfaceNumber = id; 187 188 /* allocate endpoints */ 189 190 loop->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_source_desc); 191 if (!loop->in_ep) { 192autoconf_fail: 193 ERROR(cdev, "%s: can't autoconfigure on %s\n", 194 f->name, cdev->gadget->name); 195 return -ENODEV; 196 } 197 loop->in_ep->driver_data = cdev; /* claim */ 198 199 loop->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_loop_sink_desc); 200 if (!loop->out_ep) 201 goto autoconf_fail; 202 loop->out_ep->driver_data = cdev; /* claim */ 203 204 /* support high speed hardware */ 205 hs_loop_source_desc.bEndpointAddress = 206 fs_loop_source_desc.bEndpointAddress; 207 hs_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress; 208 209 /* support super speed hardware */ 210 ss_loop_source_desc.bEndpointAddress = 211 fs_loop_source_desc.bEndpointAddress; 212 ss_loop_sink_desc.bEndpointAddress = fs_loop_sink_desc.bEndpointAddress; 213 214 ret = usb_assign_descriptors(f, fs_loopback_descs, hs_loopback_descs, 215 ss_loopback_descs); 216 if (ret) 217 return ret; 218 219 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n", 220 (gadget_is_superspeed(c->cdev->gadget) ? "super" : 221 (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")), 222 f->name, loop->in_ep->name, loop->out_ep->name); 223 return 0; 224} 225 226static void 227loopback_unbind(struct usb_configuration *c, struct usb_function *f) 228{ 229 usb_free_all_descriptors(f); 230 kfree(func_to_loop(f)); 231} 232 233static void loopback_complete(struct usb_ep *ep, struct usb_request *req) 234{ 235 struct f_loopback *loop = ep->driver_data; 236 struct usb_composite_dev *cdev = loop->function.config->cdev; 237 int status = req->status; 238 239 switch (status) { 240 241 case 0: /* normal completion? */ 242 if (ep == loop->out_ep) { 243 /* loop this OUT packet back IN to the host */ 244 req->zero = (req->actual < req->length); 245 req->length = req->actual; 246 status = usb_ep_queue(loop->in_ep, req, GFP_ATOMIC); 247 if (status == 0) 248 return; 249 250 /* "should never get here" */ 251 ERROR(cdev, "can't loop %s to %s: %d\n", 252 ep->name, loop->in_ep->name, 253 status); 254 } 255 256 /* queue the buffer for some later OUT packet */ 257 req->length = buflen; 258 status = usb_ep_queue(loop->out_ep, req, GFP_ATOMIC); 259 if (status == 0) 260 return; 261 262 /* "should never get here" */ 263 /* FALLTHROUGH */ 264 265 default: 266 ERROR(cdev, "%s loop complete --> %d, %d/%d\n", ep->name, 267 status, req->actual, req->length); 268 /* FALLTHROUGH */ 269 270 /* NOTE: since this driver doesn't maintain an explicit record 271 * of requests it submitted (just maintains qlen count), we 272 * rely on the hardware driver to clean up on disconnect or 273 * endpoint disable. 274 */ 275 case -ECONNABORTED: /* hardware forced ep reset */ 276 case -ECONNRESET: /* request dequeued */ 277 case -ESHUTDOWN: /* disconnect from host */ 278 free_ep_req(ep, req); 279 return; 280 } 281} 282 283static void disable_loopback(struct f_loopback *loop) 284{ 285 struct usb_composite_dev *cdev; 286 287 cdev = loop->function.config->cdev; 288 disable_endpoints(cdev, loop->in_ep, loop->out_ep, NULL, NULL); 289 VDBG(cdev, "%s disabled\n", loop->function.name); 290} 291 292static int 293enable_loopback(struct usb_composite_dev *cdev, struct f_loopback *loop) 294{ 295 int result = 0; 296 struct usb_ep *ep; 297 struct usb_request *req; 298 unsigned i; 299 300 /* one endpoint writes data back IN to the host */ 301 ep = loop->in_ep; 302 result = config_ep_by_speed(cdev->gadget, &(loop->function), ep); 303 if (result) 304 return result; 305 result = usb_ep_enable(ep); 306 if (result < 0) 307 return result; 308 ep->driver_data = loop; 309 310 /* one endpoint just reads OUT packets */ 311 ep = loop->out_ep; 312 result = config_ep_by_speed(cdev->gadget, &(loop->function), ep); 313 if (result) 314 goto fail0; 315 316 result = usb_ep_enable(ep); 317 if (result < 0) { 318fail0: 319 ep = loop->in_ep; 320 usb_ep_disable(ep); 321 ep->driver_data = NULL; 322 return result; 323 } 324 ep->driver_data = loop; 325 326 /* allocate a bunch of read buffers and queue them all at once. 327 * we buffer at most 'qlen' transfers; fewer if any need more 328 * than 'buflen' bytes each. 329 */ 330 for (i = 0; i < qlen && result == 0; i++) { 331 req = alloc_ep_req(ep, 0); 332 if (req) { 333 req->complete = loopback_complete; 334 result = usb_ep_queue(ep, req, GFP_ATOMIC); 335 if (result) 336 ERROR(cdev, "%s queue req --> %d\n", 337 ep->name, result); 338 } else { 339 usb_ep_disable(ep); 340 ep->driver_data = NULL; 341 result = -ENOMEM; 342 goto fail0; 343 } 344 } 345 346 DBG(cdev, "%s enabled\n", loop->function.name); 347 return result; 348} 349 350static int loopback_set_alt(struct usb_function *f, 351 unsigned intf, unsigned alt) 352{ 353 struct f_loopback *loop = func_to_loop(f); 354 struct usb_composite_dev *cdev = f->config->cdev; 355 356 /* we know alt is zero */ 357 if (loop->in_ep->driver_data) 358 disable_loopback(loop); 359 return enable_loopback(cdev, loop); 360} 361 362static void loopback_disable(struct usb_function *f) 363{ 364 struct f_loopback *loop = func_to_loop(f); 365 366 disable_loopback(loop); 367} 368 369/*-------------------------------------------------------------------------*/ 370 371static int __init loopback_bind_config(struct usb_configuration *c) 372{ 373 struct f_loopback *loop; 374 int status; 375 376 loop = kzalloc(sizeof *loop, GFP_KERNEL); 377 if (!loop) 378 return -ENOMEM; 379 380 loop->function.name = "loopback"; 381 loop->function.bind = loopback_bind; 382 loop->function.unbind = loopback_unbind; 383 loop->function.set_alt = loopback_set_alt; 384 loop->function.disable = loopback_disable; 385 386 status = usb_add_function(c, &loop->function); 387 if (status) 388 kfree(loop); 389 return status; 390} 391 392static struct usb_configuration loopback_driver = { 393 .label = "loopback", 394 .strings = loopback_strings, 395 .bConfigurationValue = 2, 396 .bmAttributes = USB_CONFIG_ATT_SELFPOWER, 397 /* .iConfiguration = DYNAMIC */ 398}; 399 400/** 401 * loopback_add - add a loopback testing configuration to a device 402 * @cdev: the device to support the loopback configuration 403 */ 404int __init loopback_add(struct usb_composite_dev *cdev, bool autoresume) 405{ 406 int id; 407 408 /* allocate string ID(s) */ 409 id = usb_string_id(cdev); 410 if (id < 0) 411 return id; 412 strings_loopback[0].id = id; 413 414 loopback_intf.iInterface = id; 415 loopback_driver.iConfiguration = id; 416 417 /* support autoresume for remote wakeup testing */ 418 if (autoresume) 419 loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 420 421 /* support OTG systems */ 422 if (gadget_is_otg(cdev->gadget)) { 423 loopback_driver.descriptors = otg_desc; 424 loopback_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; 425 } 426 427 return usb_add_config(cdev, &loopback_driver, loopback_bind_config); 428}