1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * f_sourcesink.c - USB peripheral source/sink configuration driver 4 * 5 * Copyright (C) 2003-2008 David Brownell 6 * Copyright (C) 2008 by Nokia Corporation 7 */ 8 9 /* #define VERBOSE_DEBUG */ 10 11 #include <linux/slab.h> 12 #include <linux/kernel.h> 13 #include <linux/device.h> 14 #include <linux/module.h> 15 #include <linux/usb/composite.h> 16 #include <linux/usb/func_utils.h> 17 #include <linux/err.h> 18 19 #include "g_zero.h" 20 21 /* 22 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral 23 * controller drivers. 24 * 25 * This just sinks bulk packets OUT to the peripheral and sources them IN 26 * to the host, optionally with specific data patterns for integrity tests. 27 * As such it supports basic functionality and load tests. 28 * 29 * In terms of control messaging, this supports all the standard requests 30 * plus two that support control-OUT tests. If the optional "autoresume" 31 * mode is enabled, it provides good functional coverage for the "USBCV" 32 * test harness from USB-IF. 33 */ 34 struct f_sourcesink { 35 struct usb_function function; 36 37 struct usb_ep *in_ep; 38 struct usb_ep *out_ep; 39 struct usb_ep *iso_in_ep; 40 struct usb_ep *iso_out_ep; 41 int cur_alt; 42 43 unsigned pattern; 44 unsigned isoc_interval; 45 unsigned isoc_maxpacket; 46 unsigned isoc_mult; 47 unsigned isoc_maxburst; 48 unsigned buflen; 49 unsigned bulk_maxburst; 50 unsigned bulk_qlen; 51 unsigned iso_qlen; 52 }; 53 54 static inline struct f_sourcesink *func_to_ss(struct usb_function *f) 55 { 56 return container_of(f, struct f_sourcesink, function); 57 } 58 59 /*-------------------------------------------------------------------------*/ 60 61 static struct usb_interface_descriptor source_sink_intf_alt0 = { 62 .bLength = USB_DT_INTERFACE_SIZE, 63 .bDescriptorType = USB_DT_INTERFACE, 64 65 .bAlternateSetting = 0, 66 .bNumEndpoints = 2, 67 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, 68 /* .iInterface = DYNAMIC */ 69 }; 70 71 static struct usb_interface_descriptor source_sink_intf_alt1 = { 72 .bLength = USB_DT_INTERFACE_SIZE, 73 .bDescriptorType = USB_DT_INTERFACE, 74 75 .bAlternateSetting = 1, 76 .bNumEndpoints = 4, 77 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, 78 /* .iInterface = DYNAMIC */ 79 }; 80 81 /* full speed support: */ 82 83 static struct usb_endpoint_descriptor fs_source_desc = { 84 .bLength = USB_DT_ENDPOINT_SIZE, 85 .bDescriptorType = USB_DT_ENDPOINT, 86 87 .bEndpointAddress = USB_DIR_IN, 88 .bmAttributes = USB_ENDPOINT_XFER_BULK, 89 }; 90 91 static struct usb_endpoint_descriptor fs_sink_desc = { 92 .bLength = USB_DT_ENDPOINT_SIZE, 93 .bDescriptorType = USB_DT_ENDPOINT, 94 95 .bEndpointAddress = USB_DIR_OUT, 96 .bmAttributes = USB_ENDPOINT_XFER_BULK, 97 }; 98 99 static struct usb_endpoint_descriptor fs_iso_source_desc = { 100 .bLength = USB_DT_ENDPOINT_SIZE, 101 .bDescriptorType = USB_DT_ENDPOINT, 102 103 .bEndpointAddress = USB_DIR_IN, 104 .bmAttributes = USB_ENDPOINT_XFER_ISOC, 105 .wMaxPacketSize = cpu_to_le16(1023), 106 .bInterval = 4, 107 }; 108 109 static struct usb_endpoint_descriptor fs_iso_sink_desc = { 110 .bLength = USB_DT_ENDPOINT_SIZE, 111 .bDescriptorType = USB_DT_ENDPOINT, 112 113 .bEndpointAddress = USB_DIR_OUT, 114 .bmAttributes = USB_ENDPOINT_XFER_ISOC, 115 .wMaxPacketSize = cpu_to_le16(1023), 116 .bInterval = 4, 117 }; 118 119 static struct usb_descriptor_header *fs_source_sink_descs[] = { 120 (struct usb_descriptor_header *) &source_sink_intf_alt0, 121 (struct usb_descriptor_header *) &fs_sink_desc, 122 (struct usb_descriptor_header *) &fs_source_desc, 123 (struct usb_descriptor_header *) &source_sink_intf_alt1, 124 #define FS_ALT_IFC_1_OFFSET 3 125 (struct usb_descriptor_header *) &fs_sink_desc, 126 (struct usb_descriptor_header *) &fs_source_desc, 127 (struct usb_descriptor_header *) &fs_iso_sink_desc, 128 (struct usb_descriptor_header *) &fs_iso_source_desc, 129 NULL, 130 }; 131 132 /* high speed support: */ 133 134 static struct usb_endpoint_descriptor hs_source_desc = { 135 .bLength = USB_DT_ENDPOINT_SIZE, 136 .bDescriptorType = USB_DT_ENDPOINT, 137 138 .bmAttributes = USB_ENDPOINT_XFER_BULK, 139 .wMaxPacketSize = cpu_to_le16(512), 140 }; 141 142 static struct usb_endpoint_descriptor hs_sink_desc = { 143 .bLength = USB_DT_ENDPOINT_SIZE, 144 .bDescriptorType = USB_DT_ENDPOINT, 145 146 .bmAttributes = USB_ENDPOINT_XFER_BULK, 147 .wMaxPacketSize = cpu_to_le16(512), 148 }; 149 150 static struct usb_endpoint_descriptor hs_iso_source_desc = { 151 .bLength = USB_DT_ENDPOINT_SIZE, 152 .bDescriptorType = USB_DT_ENDPOINT, 153 154 .bmAttributes = USB_ENDPOINT_XFER_ISOC, 155 .wMaxPacketSize = cpu_to_le16(1024), 156 .bInterval = 4, 157 }; 158 159 static struct usb_endpoint_descriptor hs_iso_sink_desc = { 160 .bLength = USB_DT_ENDPOINT_SIZE, 161 .bDescriptorType = USB_DT_ENDPOINT, 162 163 .bmAttributes = USB_ENDPOINT_XFER_ISOC, 164 .wMaxPacketSize = cpu_to_le16(1024), 165 .bInterval = 4, 166 }; 167 168 static struct usb_descriptor_header *hs_source_sink_descs[] = { 169 (struct usb_descriptor_header *) &source_sink_intf_alt0, 170 (struct usb_descriptor_header *) &hs_source_desc, 171 (struct usb_descriptor_header *) &hs_sink_desc, 172 (struct usb_descriptor_header *) &source_sink_intf_alt1, 173 #define HS_ALT_IFC_1_OFFSET 3 174 (struct usb_descriptor_header *) &hs_source_desc, 175 (struct usb_descriptor_header *) &hs_sink_desc, 176 (struct usb_descriptor_header *) &hs_iso_source_desc, 177 (struct usb_descriptor_header *) &hs_iso_sink_desc, 178 NULL, 179 }; 180 181 /* super speed support: */ 182 183 static struct usb_endpoint_descriptor ss_source_desc = { 184 .bLength = USB_DT_ENDPOINT_SIZE, 185 .bDescriptorType = USB_DT_ENDPOINT, 186 187 .bmAttributes = USB_ENDPOINT_XFER_BULK, 188 .wMaxPacketSize = cpu_to_le16(1024), 189 }; 190 191 static struct usb_ss_ep_comp_descriptor ss_source_comp_desc = { 192 .bLength = USB_DT_SS_EP_COMP_SIZE, 193 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 194 195 .bMaxBurst = 0, 196 .bmAttributes = 0, 197 .wBytesPerInterval = 0, 198 }; 199 200 static struct usb_endpoint_descriptor ss_sink_desc = { 201 .bLength = USB_DT_ENDPOINT_SIZE, 202 .bDescriptorType = USB_DT_ENDPOINT, 203 204 .bmAttributes = USB_ENDPOINT_XFER_BULK, 205 .wMaxPacketSize = cpu_to_le16(1024), 206 }; 207 208 static struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = { 209 .bLength = USB_DT_SS_EP_COMP_SIZE, 210 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 211 212 .bMaxBurst = 0, 213 .bmAttributes = 0, 214 .wBytesPerInterval = 0, 215 }; 216 217 static struct usb_endpoint_descriptor ss_iso_source_desc = { 218 .bLength = USB_DT_ENDPOINT_SIZE, 219 .bDescriptorType = USB_DT_ENDPOINT, 220 221 .bmAttributes = USB_ENDPOINT_XFER_ISOC, 222 .wMaxPacketSize = cpu_to_le16(1024), 223 .bInterval = 4, 224 }; 225 226 static struct usb_ss_ep_comp_descriptor ss_iso_source_comp_desc = { 227 .bLength = USB_DT_SS_EP_COMP_SIZE, 228 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 229 230 .bMaxBurst = 0, 231 .bmAttributes = 0, 232 .wBytesPerInterval = cpu_to_le16(1024), 233 }; 234 235 static struct usb_endpoint_descriptor ss_iso_sink_desc = { 236 .bLength = USB_DT_ENDPOINT_SIZE, 237 .bDescriptorType = USB_DT_ENDPOINT, 238 239 .bmAttributes = USB_ENDPOINT_XFER_ISOC, 240 .wMaxPacketSize = cpu_to_le16(1024), 241 .bInterval = 4, 242 }; 243 244 static struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc = { 245 .bLength = USB_DT_SS_EP_COMP_SIZE, 246 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, 247 248 .bMaxBurst = 0, 249 .bmAttributes = 0, 250 .wBytesPerInterval = cpu_to_le16(1024), 251 }; 252 253 static struct usb_descriptor_header *ss_source_sink_descs[] = { 254 (struct usb_descriptor_header *) &source_sink_intf_alt0, 255 (struct usb_descriptor_header *) &ss_source_desc, 256 (struct usb_descriptor_header *) &ss_source_comp_desc, 257 (struct usb_descriptor_header *) &ss_sink_desc, 258 (struct usb_descriptor_header *) &ss_sink_comp_desc, 259 (struct usb_descriptor_header *) &source_sink_intf_alt1, 260 #define SS_ALT_IFC_1_OFFSET 5 261 (struct usb_descriptor_header *) &ss_source_desc, 262 (struct usb_descriptor_header *) &ss_source_comp_desc, 263 (struct usb_descriptor_header *) &ss_sink_desc, 264 (struct usb_descriptor_header *) &ss_sink_comp_desc, 265 (struct usb_descriptor_header *) &ss_iso_source_desc, 266 (struct usb_descriptor_header *) &ss_iso_source_comp_desc, 267 (struct usb_descriptor_header *) &ss_iso_sink_desc, 268 (struct usb_descriptor_header *) &ss_iso_sink_comp_desc, 269 NULL, 270 }; 271 272 /* function-specific strings: */ 273 274 static struct usb_string strings_sourcesink[] = { 275 [0].s = "source and sink data", 276 { } /* end of list */ 277 }; 278 279 static struct usb_gadget_strings stringtab_sourcesink = { 280 .language = 0x0409, /* en-us */ 281 .strings = strings_sourcesink, 282 }; 283 284 static struct usb_gadget_strings *sourcesink_strings[] = { 285 &stringtab_sourcesink, 286 NULL, 287 }; 288 289 /*-------------------------------------------------------------------------*/ 290 291 static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len) 292 { 293 return alloc_ep_req(ep, len); 294 } 295 296 static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep) 297 { 298 int value; 299 300 value = usb_ep_disable(ep); 301 if (value < 0) 302 DBG(cdev, "disable %s --> %d\n", ep->name, value); 303 } 304 305 void disable_endpoints(struct usb_composite_dev *cdev, 306 struct usb_ep *in, struct usb_ep *out, 307 struct usb_ep *iso_in, struct usb_ep *iso_out) 308 { 309 disable_ep(cdev, in); 310 disable_ep(cdev, out); 311 if (iso_in) 312 disable_ep(cdev, iso_in); 313 if (iso_out) 314 disable_ep(cdev, iso_out); 315 } 316 317 static int 318 sourcesink_bind(struct usb_configuration *c, struct usb_function *f) 319 { 320 struct usb_composite_dev *cdev = c->cdev; 321 struct f_sourcesink *ss = func_to_ss(f); 322 int id; 323 int ret; 324 325 /* allocate interface ID(s) */ 326 id = usb_interface_id(c, f); 327 if (id < 0) 328 return id; 329 source_sink_intf_alt0.bInterfaceNumber = id; 330 source_sink_intf_alt1.bInterfaceNumber = id; 331 332 if (ss->bulk_maxburst > 15) 333 ss->bulk_maxburst = 15; 334 335 ss_source_comp_desc.bMaxBurst = ss->bulk_maxburst; 336 ss_sink_comp_desc.bMaxBurst = ss->bulk_maxburst; 337 338 /* allocate bulk endpoints */ 339 ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc); 340 if (!ss->in_ep) { 341 autoconf_fail: 342 ERROR(cdev, "%s: can't autoconfigure on %s\n", 343 f->name, cdev->gadget->name); 344 return -ENODEV; 345 } 346 347 ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc); 348 if (!ss->out_ep) 349 goto autoconf_fail; 350 351 /* sanity check the isoc module parameters */ 352 if (ss->isoc_interval < 1) 353 ss->isoc_interval = 1; 354 if (ss->isoc_interval > 16) 355 ss->isoc_interval = 16; 356 if (ss->isoc_mult > 2) 357 ss->isoc_mult = 2; 358 if (ss->isoc_maxburst > 15) 359 ss->isoc_maxburst = 15; 360 361 /* fill in the FS isoc descriptors from the module parameters */ 362 fs_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket > 1023 ? 363 1023 : ss->isoc_maxpacket; 364 fs_iso_source_desc.bInterval = ss->isoc_interval; 365 fs_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket > 1023 ? 366 1023 : ss->isoc_maxpacket; 367 fs_iso_sink_desc.bInterval = ss->isoc_interval; 368 369 /* allocate iso endpoints */ 370 ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc); 371 if (!ss->iso_in_ep) 372 goto no_iso; 373 374 ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc); 375 if (!ss->iso_out_ep) { 376 usb_ep_autoconfig_release(ss->iso_in_ep); 377 ss->iso_in_ep = NULL; 378 no_iso: 379 /* 380 * We still want to work even if the UDC doesn't have isoc 381 * endpoints, so null out the alt interface that contains 382 * them and continue. 383 */ 384 fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL; 385 hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL; 386 ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL; 387 } 388 389 if (ss->isoc_maxpacket > 1024) 390 ss->isoc_maxpacket = 1024; 391 392 /* support high speed hardware */ 393 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress; 394 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress; 395 396 /* 397 * Fill in the HS isoc descriptors from the module parameters. 398 * We assume that the user knows what they are doing and won't 399 * give parameters that their UDC doesn't support. 400 */ 401 hs_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket; 402 hs_iso_source_desc.wMaxPacketSize |= ss->isoc_mult << 11; 403 hs_iso_source_desc.bInterval = ss->isoc_interval; 404 hs_iso_source_desc.bEndpointAddress = 405 fs_iso_source_desc.bEndpointAddress; 406 407 hs_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket; 408 hs_iso_sink_desc.wMaxPacketSize |= ss->isoc_mult << 11; 409 hs_iso_sink_desc.bInterval = ss->isoc_interval; 410 hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress; 411 412 /* support super speed hardware */ 413 ss_source_desc.bEndpointAddress = 414 fs_source_desc.bEndpointAddress; 415 ss_sink_desc.bEndpointAddress = 416 fs_sink_desc.bEndpointAddress; 417 418 /* 419 * Fill in the SS isoc descriptors from the module parameters. 420 * We assume that the user knows what they are doing and won't 421 * give parameters that their UDC doesn't support. 422 */ 423 ss_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket; 424 ss_iso_source_desc.bInterval = ss->isoc_interval; 425 ss_iso_source_comp_desc.bmAttributes = ss->isoc_mult; 426 ss_iso_source_comp_desc.bMaxBurst = ss->isoc_maxburst; 427 ss_iso_source_comp_desc.wBytesPerInterval = ss->isoc_maxpacket * 428 (ss->isoc_mult + 1) * (ss->isoc_maxburst + 1); 429 ss_iso_source_desc.bEndpointAddress = 430 fs_iso_source_desc.bEndpointAddress; 431 432 ss_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket; 433 ss_iso_sink_desc.bInterval = ss->isoc_interval; 434 ss_iso_sink_comp_desc.bmAttributes = ss->isoc_mult; 435 ss_iso_sink_comp_desc.bMaxBurst = ss->isoc_maxburst; 436 ss_iso_sink_comp_desc.wBytesPerInterval = ss->isoc_maxpacket * 437 (ss->isoc_mult + 1) * (ss->isoc_maxburst + 1); 438 ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress; 439 440 ret = usb_assign_descriptors(f, fs_source_sink_descs, 441 hs_source_sink_descs, ss_source_sink_descs, 442 ss_source_sink_descs); 443 if (ret) 444 return ret; 445 446 DBG(cdev, "%s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n", 447 f->name, ss->in_ep->name, ss->out_ep->name, 448 ss->iso_in_ep ? ss->iso_in_ep->name : "<none>", 449 ss->iso_out_ep ? ss->iso_out_ep->name : "<none>"); 450 return 0; 451 } 452 453 static void 454 sourcesink_free_func(struct usb_function *f) 455 { 456 struct f_ss_opts *opts; 457 458 opts = container_of(f->fi, struct f_ss_opts, func_inst); 459 460 mutex_lock(&opts->lock); 461 opts->refcnt--; 462 mutex_unlock(&opts->lock); 463 464 usb_free_all_descriptors(f); 465 kfree(func_to_ss(f)); 466 } 467 468 /* optionally require specific source/sink data patterns */ 469 static int check_read_data(struct f_sourcesink *ss, struct usb_request *req) 470 { 471 unsigned i; 472 u8 *buf = req->buf; 473 struct usb_composite_dev *cdev = ss->function.config->cdev; 474 int max_packet_size = le16_to_cpu(ss->out_ep->desc->wMaxPacketSize); 475 476 if (ss->pattern == 2) 477 return 0; 478 479 for (i = 0; i < req->actual; i++, buf++) { 480 switch (ss->pattern) { 481 482 /* all-zeroes has no synchronization issues */ 483 case 0: 484 if (*buf == 0) 485 continue; 486 break; 487 488 /* "mod63" stays in sync with short-terminated transfers, 489 * OR otherwise when host and gadget agree on how large 490 * each usb transfer request should be. Resync is done 491 * with set_interface or set_config. (We *WANT* it to 492 * get quickly out of sync if controllers or their drivers 493 * stutter for any reason, including buffer duplication...) 494 */ 495 case 1: 496 if (*buf == (u8)((i % max_packet_size) % 63)) 497 continue; 498 break; 499 } 500 ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf); 501 usb_ep_set_halt(ss->out_ep); 502 return -EINVAL; 503 } 504 return 0; 505 } 506 507 static void reinit_write_data(struct usb_ep *ep, struct usb_request *req) 508 { 509 unsigned i; 510 u8 *buf = req->buf; 511 int max_packet_size = le16_to_cpu(ep->desc->wMaxPacketSize); 512 struct f_sourcesink *ss = ep->driver_data; 513 514 switch (ss->pattern) { 515 case 0: 516 memset(req->buf, 0, req->length); 517 break; 518 case 1: 519 for (i = 0; i < req->length; i++) 520 *buf++ = (u8) ((i % max_packet_size) % 63); 521 break; 522 case 2: 523 break; 524 } 525 } 526 527 static void source_sink_complete(struct usb_ep *ep, struct usb_request *req) 528 { 529 struct usb_composite_dev *cdev; 530 struct f_sourcesink *ss = ep->driver_data; 531 int status = req->status; 532 533 /* driver_data will be null if ep has been disabled */ 534 if (!ss) 535 return; 536 537 cdev = ss->function.config->cdev; 538 539 switch (status) { 540 541 case 0: /* normal completion? */ 542 if (ep == ss->out_ep) { 543 check_read_data(ss, req); 544 if (ss->pattern != 2) 545 memset(req->buf, 0x55, req->length); 546 } 547 break; 548 549 /* this endpoint is normally active while we're configured */ 550 case -ECONNABORTED: /* hardware forced ep reset */ 551 case -ECONNRESET: /* request dequeued */ 552 case -ESHUTDOWN: /* disconnect from host */ 553 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status, 554 req->actual, req->length); 555 if (ep == ss->out_ep) 556 check_read_data(ss, req); 557 free_ep_req(ep, req); 558 return; 559 560 case -EOVERFLOW: /* buffer overrun on read means that 561 * we didn't provide a big enough 562 * buffer. 563 */ 564 default: 565 #if 1 566 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name, 567 status, req->actual, req->length); 568 break; 569 #endif 570 case -EREMOTEIO: /* short read */ 571 break; 572 } 573 574 status = usb_ep_queue(ep, req, GFP_ATOMIC); 575 if (status) { 576 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n", 577 ep->name, req->length, status); 578 usb_ep_set_halt(ep); 579 /* FIXME recover later ... somehow */ 580 } 581 } 582 583 static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in, 584 bool is_iso, int speed) 585 { 586 struct usb_ep *ep; 587 struct usb_request *req; 588 int i, size, qlen, status = 0; 589 590 if (is_iso) { 591 switch (speed) { 592 case USB_SPEED_SUPER_PLUS: 593 case USB_SPEED_SUPER: 594 size = ss->isoc_maxpacket * 595 (ss->isoc_mult + 1) * 596 (ss->isoc_maxburst + 1); 597 break; 598 case USB_SPEED_HIGH: 599 size = ss->isoc_maxpacket * (ss->isoc_mult + 1); 600 break; 601 default: 602 size = ss->isoc_maxpacket > 1023 ? 603 1023 : ss->isoc_maxpacket; 604 break; 605 } 606 ep = is_in ? ss->iso_in_ep : ss->iso_out_ep; 607 qlen = ss->iso_qlen; 608 } else { 609 ep = is_in ? ss->in_ep : ss->out_ep; 610 qlen = ss->bulk_qlen; 611 size = ss->buflen; 612 } 613 614 for (i = 0; i < qlen; i++) { 615 req = ss_alloc_ep_req(ep, size); 616 if (!req) 617 return -ENOMEM; 618 619 req->complete = source_sink_complete; 620 if (is_in) 621 reinit_write_data(ep, req); 622 else if (ss->pattern != 2) 623 memset(req->buf, 0x55, req->length); 624 625 status = usb_ep_queue(ep, req, GFP_ATOMIC); 626 if (status) { 627 struct usb_composite_dev *cdev; 628 629 cdev = ss->function.config->cdev; 630 ERROR(cdev, "start %s%s %s --> %d\n", 631 is_iso ? "ISO-" : "", is_in ? "IN" : "OUT", 632 ep->name, status); 633 free_ep_req(ep, req); 634 return status; 635 } 636 } 637 638 return status; 639 } 640 641 static void disable_source_sink(struct f_sourcesink *ss) 642 { 643 struct usb_composite_dev *cdev; 644 645 cdev = ss->function.config->cdev; 646 disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep, 647 ss->iso_out_ep); 648 VDBG(cdev, "%s disabled\n", ss->function.name); 649 } 650 651 static int 652 enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss, 653 int alt) 654 { 655 int result = 0; 656 int speed = cdev->gadget->speed; 657 struct usb_ep *ep; 658 659 /* one bulk endpoint writes (sources) zeroes IN (to the host) */ 660 ep = ss->in_ep; 661 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); 662 if (result) 663 return result; 664 result = usb_ep_enable(ep); 665 if (result < 0) 666 return result; 667 ep->driver_data = ss; 668 669 result = source_sink_start_ep(ss, true, false, speed); 670 if (result < 0) { 671 fail: 672 ep = ss->in_ep; 673 usb_ep_disable(ep); 674 return result; 675 } 676 677 /* one bulk endpoint reads (sinks) anything OUT (from the host) */ 678 ep = ss->out_ep; 679 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); 680 if (result) 681 goto fail; 682 result = usb_ep_enable(ep); 683 if (result < 0) 684 goto fail; 685 ep->driver_data = ss; 686 687 result = source_sink_start_ep(ss, false, false, speed); 688 if (result < 0) { 689 fail2: 690 ep = ss->out_ep; 691 usb_ep_disable(ep); 692 goto fail; 693 } 694 695 if (alt == 0) 696 goto out; 697 698 /* one iso endpoint writes (sources) zeroes IN (to the host) */ 699 ep = ss->iso_in_ep; 700 if (ep) { 701 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); 702 if (result) 703 goto fail2; 704 result = usb_ep_enable(ep); 705 if (result < 0) 706 goto fail2; 707 ep->driver_data = ss; 708 709 result = source_sink_start_ep(ss, true, true, speed); 710 if (result < 0) { 711 fail3: 712 ep = ss->iso_in_ep; 713 if (ep) 714 usb_ep_disable(ep); 715 goto fail2; 716 } 717 } 718 719 /* one iso endpoint reads (sinks) anything OUT (from the host) */ 720 ep = ss->iso_out_ep; 721 if (ep) { 722 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); 723 if (result) 724 goto fail3; 725 result = usb_ep_enable(ep); 726 if (result < 0) 727 goto fail3; 728 ep->driver_data = ss; 729 730 result = source_sink_start_ep(ss, false, true, speed); 731 if (result < 0) { 732 usb_ep_disable(ep); 733 goto fail3; 734 } 735 } 736 out: 737 ss->cur_alt = alt; 738 739 DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt); 740 return result; 741 } 742 743 static int sourcesink_set_alt(struct usb_function *f, 744 unsigned intf, unsigned alt) 745 { 746 struct f_sourcesink *ss = func_to_ss(f); 747 struct usb_composite_dev *cdev = f->config->cdev; 748 749 disable_source_sink(ss); 750 return enable_source_sink(cdev, ss, alt); 751 } 752 753 static int sourcesink_get_alt(struct usb_function *f, unsigned intf) 754 { 755 struct f_sourcesink *ss = func_to_ss(f); 756 757 return ss->cur_alt; 758 } 759 760 static void sourcesink_disable(struct usb_function *f) 761 { 762 struct f_sourcesink *ss = func_to_ss(f); 763 764 disable_source_sink(ss); 765 } 766 767 /*-------------------------------------------------------------------------*/ 768 769 static int sourcesink_setup(struct usb_function *f, 770 const struct usb_ctrlrequest *ctrl) 771 { 772 struct usb_configuration *c = f->config; 773 struct usb_request *req = c->cdev->req; 774 int value = -EOPNOTSUPP; 775 u16 w_index = le16_to_cpu(ctrl->wIndex); 776 u16 w_value = le16_to_cpu(ctrl->wValue); 777 u16 w_length = le16_to_cpu(ctrl->wLength); 778 779 req->length = USB_COMP_EP0_BUFSIZ; 780 781 /* composite driver infrastructure handles everything except 782 * the two control test requests. 783 */ 784 switch (ctrl->bRequest) { 785 786 /* 787 * These are the same vendor-specific requests supported by 788 * Intel's USB 2.0 compliance test devices. We exceed that 789 * device spec by allowing multiple-packet requests. 790 * 791 * NOTE: the Control-OUT data stays in req->buf ... better 792 * would be copying it into a scratch buffer, so that other 793 * requests may safely intervene. 794 */ 795 case 0x5b: /* control WRITE test -- fill the buffer */ 796 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR)) 797 goto unknown; 798 if (w_value || w_index) 799 break; 800 /* just read that many bytes into the buffer */ 801 if (w_length > req->length) 802 break; 803 value = w_length; 804 break; 805 case 0x5c: /* control READ test -- return the buffer */ 806 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR)) 807 goto unknown; 808 if (w_value || w_index) 809 break; 810 /* expect those bytes are still in the buffer; send back */ 811 if (w_length > req->length) 812 break; 813 value = w_length; 814 break; 815 816 default: 817 unknown: 818 VDBG(c->cdev, 819 "unknown control req%02x.%02x v%04x i%04x l%d\n", 820 ctrl->bRequestType, ctrl->bRequest, 821 w_value, w_index, w_length); 822 } 823 824 /* respond with data transfer or status phase? */ 825 if (value >= 0) { 826 VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n", 827 ctrl->bRequestType, ctrl->bRequest, 828 w_value, w_index, w_length); 829 req->zero = 0; 830 req->length = value; 831 value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC); 832 if (value < 0) 833 ERROR(c->cdev, "source/sink response, err %d\n", 834 value); 835 } 836 837 /* device either stalls (value < 0) or reports success */ 838 return value; 839 } 840 841 static struct usb_function *source_sink_alloc_func( 842 struct usb_function_instance *fi) 843 { 844 struct f_sourcesink *ss; 845 struct f_ss_opts *ss_opts; 846 847 ss = kzalloc(sizeof(*ss), GFP_KERNEL); 848 if (!ss) 849 return ERR_PTR(-ENOMEM); 850 851 ss_opts = container_of(fi, struct f_ss_opts, func_inst); 852 853 mutex_lock(&ss_opts->lock); 854 ss_opts->refcnt++; 855 mutex_unlock(&ss_opts->lock); 856 857 ss->pattern = ss_opts->pattern; 858 ss->isoc_interval = ss_opts->isoc_interval; 859 ss->isoc_maxpacket = ss_opts->isoc_maxpacket; 860 ss->isoc_mult = ss_opts->isoc_mult; 861 ss->isoc_maxburst = ss_opts->isoc_maxburst; 862 ss->buflen = ss_opts->bulk_buflen; 863 ss->bulk_maxburst = ss_opts->bulk_maxburst; 864 ss->bulk_qlen = ss_opts->bulk_qlen; 865 ss->iso_qlen = ss_opts->iso_qlen; 866 867 ss->function.name = "source/sink"; 868 ss->function.bind = sourcesink_bind; 869 ss->function.set_alt = sourcesink_set_alt; 870 ss->function.get_alt = sourcesink_get_alt; 871 ss->function.disable = sourcesink_disable; 872 ss->function.setup = sourcesink_setup; 873 ss->function.strings = sourcesink_strings; 874 875 ss->function.free_func = sourcesink_free_func; 876 877 return &ss->function; 878 } 879 880 static inline struct f_ss_opts *to_f_ss_opts(struct config_item *item) 881 { 882 return container_of(to_config_group(item), struct f_ss_opts, 883 func_inst.group); 884 } 885 886 static void ss_attr_release(struct config_item *item) 887 { 888 struct f_ss_opts *ss_opts = to_f_ss_opts(item); 889 890 usb_put_function_instance(&ss_opts->func_inst); 891 } 892 893 static const struct configfs_item_operations ss_item_ops = { 894 .release = ss_attr_release, 895 }; 896 897 static ssize_t f_ss_opts_pattern_show(struct config_item *item, char *page) 898 { 899 struct f_ss_opts *opts = to_f_ss_opts(item); 900 int result; 901 902 mutex_lock(&opts->lock); 903 result = sprintf(page, "%u\n", opts->pattern); 904 mutex_unlock(&opts->lock); 905 906 return result; 907 } 908 909 static ssize_t f_ss_opts_pattern_store(struct config_item *item, 910 const char *page, size_t len) 911 { 912 struct f_ss_opts *opts = to_f_ss_opts(item); 913 int ret; 914 u8 num; 915 916 mutex_lock(&opts->lock); 917 if (opts->refcnt) { 918 ret = -EBUSY; 919 goto end; 920 } 921 922 ret = kstrtou8(page, 0, &num); 923 if (ret) 924 goto end; 925 926 if (num != 0 && num != 1 && num != 2) { 927 ret = -EINVAL; 928 goto end; 929 } 930 931 opts->pattern = num; 932 ret = len; 933 end: 934 mutex_unlock(&opts->lock); 935 return ret; 936 } 937 938 CONFIGFS_ATTR(f_ss_opts_, pattern); 939 940 static ssize_t f_ss_opts_isoc_interval_show(struct config_item *item, char *page) 941 { 942 struct f_ss_opts *opts = to_f_ss_opts(item); 943 int result; 944 945 mutex_lock(&opts->lock); 946 result = sprintf(page, "%u\n", opts->isoc_interval); 947 mutex_unlock(&opts->lock); 948 949 return result; 950 } 951 952 static ssize_t f_ss_opts_isoc_interval_store(struct config_item *item, 953 const char *page, size_t len) 954 { 955 struct f_ss_opts *opts = to_f_ss_opts(item); 956 int ret; 957 u8 num; 958 959 mutex_lock(&opts->lock); 960 if (opts->refcnt) { 961 ret = -EBUSY; 962 goto end; 963 } 964 965 ret = kstrtou8(page, 0, &num); 966 if (ret) 967 goto end; 968 969 if (num > 16) { 970 ret = -EINVAL; 971 goto end; 972 } 973 974 opts->isoc_interval = num; 975 ret = len; 976 end: 977 mutex_unlock(&opts->lock); 978 return ret; 979 } 980 981 CONFIGFS_ATTR(f_ss_opts_, isoc_interval); 982 983 static ssize_t f_ss_opts_isoc_maxpacket_show(struct config_item *item, char *page) 984 { 985 struct f_ss_opts *opts = to_f_ss_opts(item); 986 int result; 987 988 mutex_lock(&opts->lock); 989 result = sprintf(page, "%u\n", opts->isoc_maxpacket); 990 mutex_unlock(&opts->lock); 991 992 return result; 993 } 994 995 static ssize_t f_ss_opts_isoc_maxpacket_store(struct config_item *item, 996 const char *page, size_t len) 997 { 998 struct f_ss_opts *opts = to_f_ss_opts(item); 999 int ret; 1000 u16 num; 1001 1002 mutex_lock(&opts->lock); 1003 if (opts->refcnt) { 1004 ret = -EBUSY; 1005 goto end; 1006 } 1007 1008 ret = kstrtou16(page, 0, &num); 1009 if (ret) 1010 goto end; 1011 1012 if (num > 1024) { 1013 ret = -EINVAL; 1014 goto end; 1015 } 1016 1017 opts->isoc_maxpacket = num; 1018 ret = len; 1019 end: 1020 mutex_unlock(&opts->lock); 1021 return ret; 1022 } 1023 1024 CONFIGFS_ATTR(f_ss_opts_, isoc_maxpacket); 1025 1026 static ssize_t f_ss_opts_isoc_mult_show(struct config_item *item, char *page) 1027 { 1028 struct f_ss_opts *opts = to_f_ss_opts(item); 1029 int result; 1030 1031 mutex_lock(&opts->lock); 1032 result = sprintf(page, "%u\n", opts->isoc_mult); 1033 mutex_unlock(&opts->lock); 1034 1035 return result; 1036 } 1037 1038 static ssize_t f_ss_opts_isoc_mult_store(struct config_item *item, 1039 const char *page, size_t len) 1040 { 1041 struct f_ss_opts *opts = to_f_ss_opts(item); 1042 int ret; 1043 u8 num; 1044 1045 mutex_lock(&opts->lock); 1046 if (opts->refcnt) { 1047 ret = -EBUSY; 1048 goto end; 1049 } 1050 1051 ret = kstrtou8(page, 0, &num); 1052 if (ret) 1053 goto end; 1054 1055 if (num > 2) { 1056 ret = -EINVAL; 1057 goto end; 1058 } 1059 1060 opts->isoc_mult = num; 1061 ret = len; 1062 end: 1063 mutex_unlock(&opts->lock); 1064 return ret; 1065 } 1066 1067 CONFIGFS_ATTR(f_ss_opts_, isoc_mult); 1068 1069 static ssize_t f_ss_opts_isoc_maxburst_show(struct config_item *item, char *page) 1070 { 1071 struct f_ss_opts *opts = to_f_ss_opts(item); 1072 int result; 1073 1074 mutex_lock(&opts->lock); 1075 result = sprintf(page, "%u\n", opts->isoc_maxburst); 1076 mutex_unlock(&opts->lock); 1077 1078 return result; 1079 } 1080 1081 static ssize_t f_ss_opts_isoc_maxburst_store(struct config_item *item, 1082 const char *page, size_t len) 1083 { 1084 struct f_ss_opts *opts = to_f_ss_opts(item); 1085 int ret; 1086 u8 num; 1087 1088 mutex_lock(&opts->lock); 1089 if (opts->refcnt) { 1090 ret = -EBUSY; 1091 goto end; 1092 } 1093 1094 ret = kstrtou8(page, 0, &num); 1095 if (ret) 1096 goto end; 1097 1098 if (num > 15) { 1099 ret = -EINVAL; 1100 goto end; 1101 } 1102 1103 opts->isoc_maxburst = num; 1104 ret = len; 1105 end: 1106 mutex_unlock(&opts->lock); 1107 return ret; 1108 } 1109 1110 CONFIGFS_ATTR(f_ss_opts_, isoc_maxburst); 1111 1112 static ssize_t f_ss_opts_bulk_maxburst_show(struct config_item *item, char *page) 1113 { 1114 struct f_ss_opts *opts = to_f_ss_opts(item); 1115 int result; 1116 1117 mutex_lock(&opts->lock); 1118 result = sysfs_emit(page, "%u\n", opts->bulk_maxburst); 1119 mutex_unlock(&opts->lock); 1120 1121 return result; 1122 } 1123 1124 static ssize_t f_ss_opts_bulk_maxburst_store(struct config_item *item, 1125 const char *page, size_t len) 1126 { 1127 struct f_ss_opts *opts = to_f_ss_opts(item); 1128 int ret; 1129 u8 num; 1130 1131 mutex_lock(&opts->lock); 1132 if (opts->refcnt) { 1133 ret = -EBUSY; 1134 goto end; 1135 } 1136 1137 ret = kstrtou8(page, 0, &num); 1138 if (ret) 1139 goto end; 1140 1141 if (num > 15) { 1142 ret = -EINVAL; 1143 goto end; 1144 } 1145 1146 opts->bulk_maxburst = num; 1147 ret = len; 1148 end: 1149 mutex_unlock(&opts->lock); 1150 return ret; 1151 } 1152 1153 CONFIGFS_ATTR(f_ss_opts_, bulk_maxburst); 1154 1155 static ssize_t f_ss_opts_bulk_buflen_show(struct config_item *item, char *page) 1156 { 1157 struct f_ss_opts *opts = to_f_ss_opts(item); 1158 int result; 1159 1160 mutex_lock(&opts->lock); 1161 result = sprintf(page, "%u\n", opts->bulk_buflen); 1162 mutex_unlock(&opts->lock); 1163 1164 return result; 1165 } 1166 1167 static ssize_t f_ss_opts_bulk_buflen_store(struct config_item *item, 1168 const char *page, size_t len) 1169 { 1170 struct f_ss_opts *opts = to_f_ss_opts(item); 1171 int ret; 1172 u32 num; 1173 1174 mutex_lock(&opts->lock); 1175 if (opts->refcnt) { 1176 ret = -EBUSY; 1177 goto end; 1178 } 1179 1180 ret = kstrtou32(page, 0, &num); 1181 if (ret) 1182 goto end; 1183 1184 opts->bulk_buflen = num; 1185 ret = len; 1186 end: 1187 mutex_unlock(&opts->lock); 1188 return ret; 1189 } 1190 1191 CONFIGFS_ATTR(f_ss_opts_, bulk_buflen); 1192 1193 static ssize_t f_ss_opts_bulk_qlen_show(struct config_item *item, char *page) 1194 { 1195 struct f_ss_opts *opts = to_f_ss_opts(item); 1196 int result; 1197 1198 mutex_lock(&opts->lock); 1199 result = sprintf(page, "%u\n", opts->bulk_qlen); 1200 mutex_unlock(&opts->lock); 1201 1202 return result; 1203 } 1204 1205 static ssize_t f_ss_opts_bulk_qlen_store(struct config_item *item, 1206 const char *page, size_t len) 1207 { 1208 struct f_ss_opts *opts = to_f_ss_opts(item); 1209 int ret; 1210 u32 num; 1211 1212 mutex_lock(&opts->lock); 1213 if (opts->refcnt) { 1214 ret = -EBUSY; 1215 goto end; 1216 } 1217 1218 ret = kstrtou32(page, 0, &num); 1219 if (ret) 1220 goto end; 1221 1222 opts->bulk_qlen = num; 1223 ret = len; 1224 end: 1225 mutex_unlock(&opts->lock); 1226 return ret; 1227 } 1228 1229 CONFIGFS_ATTR(f_ss_opts_, bulk_qlen); 1230 1231 static ssize_t f_ss_opts_iso_qlen_show(struct config_item *item, char *page) 1232 { 1233 struct f_ss_opts *opts = to_f_ss_opts(item); 1234 int result; 1235 1236 mutex_lock(&opts->lock); 1237 result = sprintf(page, "%u\n", opts->iso_qlen); 1238 mutex_unlock(&opts->lock); 1239 1240 return result; 1241 } 1242 1243 static ssize_t f_ss_opts_iso_qlen_store(struct config_item *item, 1244 const char *page, size_t len) 1245 { 1246 struct f_ss_opts *opts = to_f_ss_opts(item); 1247 int ret; 1248 u32 num; 1249 1250 mutex_lock(&opts->lock); 1251 if (opts->refcnt) { 1252 ret = -EBUSY; 1253 goto end; 1254 } 1255 1256 ret = kstrtou32(page, 0, &num); 1257 if (ret) 1258 goto end; 1259 1260 opts->iso_qlen = num; 1261 ret = len; 1262 end: 1263 mutex_unlock(&opts->lock); 1264 return ret; 1265 } 1266 1267 CONFIGFS_ATTR(f_ss_opts_, iso_qlen); 1268 1269 static struct configfs_attribute *ss_attrs[] = { 1270 &f_ss_opts_attr_pattern, 1271 &f_ss_opts_attr_isoc_interval, 1272 &f_ss_opts_attr_isoc_maxpacket, 1273 &f_ss_opts_attr_isoc_mult, 1274 &f_ss_opts_attr_isoc_maxburst, 1275 &f_ss_opts_attr_bulk_buflen, 1276 &f_ss_opts_attr_bulk_maxburst, 1277 &f_ss_opts_attr_bulk_qlen, 1278 &f_ss_opts_attr_iso_qlen, 1279 NULL, 1280 }; 1281 1282 static const struct config_item_type ss_func_type = { 1283 .ct_item_ops = &ss_item_ops, 1284 .ct_attrs = ss_attrs, 1285 .ct_owner = THIS_MODULE, 1286 }; 1287 1288 static void source_sink_free_instance(struct usb_function_instance *fi) 1289 { 1290 struct f_ss_opts *ss_opts; 1291 1292 ss_opts = container_of(fi, struct f_ss_opts, func_inst); 1293 kfree(ss_opts); 1294 } 1295 1296 static struct usb_function_instance *source_sink_alloc_inst(void) 1297 { 1298 struct f_ss_opts *ss_opts; 1299 1300 ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL); 1301 if (!ss_opts) 1302 return ERR_PTR(-ENOMEM); 1303 mutex_init(&ss_opts->lock); 1304 ss_opts->func_inst.free_func_inst = source_sink_free_instance; 1305 ss_opts->isoc_interval = GZERO_ISOC_INTERVAL; 1306 ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET; 1307 ss_opts->bulk_buflen = GZERO_BULK_BUFLEN; 1308 ss_opts->bulk_qlen = GZERO_SS_BULK_QLEN; 1309 ss_opts->iso_qlen = GZERO_SS_ISO_QLEN; 1310 1311 config_group_init_type_name(&ss_opts->func_inst.group, "", 1312 &ss_func_type); 1313 1314 return &ss_opts->func_inst; 1315 } 1316 DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst, 1317 source_sink_alloc_func); 1318 1319 static int __init sslb_modinit(void) 1320 { 1321 int ret; 1322 1323 ret = usb_function_register(&SourceSinkusb_func); 1324 if (ret) 1325 return ret; 1326 ret = lb_modinit(); 1327 if (ret) 1328 usb_function_unregister(&SourceSinkusb_func); 1329 return ret; 1330 } 1331 static void __exit sslb_modexit(void) 1332 { 1333 usb_function_unregister(&SourceSinkusb_func); 1334 lb_modexit(); 1335 } 1336 module_init(sslb_modinit); 1337 module_exit(sslb_modexit); 1338 1339 MODULE_DESCRIPTION("USB peripheral source/sink configuration driver"); 1340 MODULE_LICENSE("GPL"); 1341