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