1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de> 4 */ 5 6 #include <linux/slab.h> 7 #include <linux/usb.h> 8 #include <linux/usb/audio.h> 9 #include <linux/module.h> 10 #include <sound/core.h> 11 #include <sound/hwdep.h> 12 #include <sound/pcm.h> 13 #include <sound/initval.h> 14 #define MODNAME "US122L" 15 #include "usb_stream.c" 16 #include "../usbaudio.h" 17 #include "../midi.h" 18 #include "us122l.h" 19 20 MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>"); 21 MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5"); 22 MODULE_LICENSE("GPL"); 23 24 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */ 25 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */ 26 /* Enable this card */ 27 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; 28 29 module_param_array(index, int, NULL, 0444); 30 MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS"."); 31 module_param_array(id, charp, NULL, 0444); 32 MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS"."); 33 module_param_array(enable, bool, NULL, 0444); 34 MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS"."); 35 36 /* driver_info flags */ 37 #define US122L_FLAG_US144 BIT(0) 38 39 static int snd_us122l_card_used[SNDRV_CARDS]; 40 41 static int us122l_create_usbmidi(struct snd_card *card) 42 { 43 static const struct snd_usb_midi_endpoint_info quirk_data = { 44 .out_ep = 4, 45 .in_ep = 3, 46 .out_cables = 0x001, 47 .in_cables = 0x001 48 }; 49 static const struct snd_usb_audio_quirk quirk = { 50 .vendor_name = "US122L", 51 .product_name = NAME_ALLCAPS, 52 .ifnum = 1, 53 .type = QUIRK_MIDI_US122L, 54 .data = &quirk_data 55 }; 56 struct usb_device *dev = US122L(card)->dev; 57 struct usb_interface *iface = usb_ifnum_to_if(dev, 1); 58 59 return snd_usbmidi_create(card, iface, 60 &US122L(card)->midi_list, &quirk); 61 } 62 63 static int us144_create_usbmidi(struct snd_card *card) 64 { 65 static const struct snd_usb_midi_endpoint_info quirk_data = { 66 .out_ep = 4, 67 .in_ep = 3, 68 .out_cables = 0x001, 69 .in_cables = 0x001 70 }; 71 static const struct snd_usb_audio_quirk quirk = { 72 .vendor_name = "US144", 73 .product_name = NAME_ALLCAPS, 74 .ifnum = 0, 75 .type = QUIRK_MIDI_US122L, 76 .data = &quirk_data 77 }; 78 struct usb_device *dev = US122L(card)->dev; 79 struct usb_interface *iface = usb_ifnum_to_if(dev, 0); 80 81 return snd_usbmidi_create(card, iface, 82 &US122L(card)->midi_list, &quirk); 83 } 84 85 static void pt_info_set(struct usb_device *dev, u8 v) 86 { 87 usb_control_msg_send(dev, 0, 'I', 88 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 89 v, 0, NULL, 0, 1000, GFP_NOIO); 90 } 91 92 static vm_fault_t usb_stream_hwdep_vm_fault(struct vm_fault *vmf) 93 { 94 unsigned long offset; 95 struct page *page; 96 void *vaddr; 97 struct us122l *us122l = vmf->vma->vm_private_data; 98 struct usb_stream *s; 99 100 guard(mutex)(&us122l->mutex); 101 s = us122l->sk.s; 102 if (!s) 103 return VM_FAULT_SIGBUS; 104 105 offset = vmf->pgoff << PAGE_SHIFT; 106 if (offset < PAGE_ALIGN(s->read_size)) { 107 vaddr = (char *)s + offset; 108 } else { 109 offset -= PAGE_ALIGN(s->read_size); 110 if (offset >= PAGE_ALIGN(s->write_size)) 111 return VM_FAULT_SIGBUS; 112 113 vaddr = us122l->sk.write_page + offset; 114 } 115 page = virt_to_page(vaddr); 116 117 get_page(page); 118 119 vmf->page = page; 120 121 return 0; 122 } 123 124 125 static const struct vm_operations_struct usb_stream_hwdep_vm_ops = { 126 .fault = usb_stream_hwdep_vm_fault, 127 }; 128 129 static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file) 130 { 131 struct us122l *us122l = hw->private_data; 132 struct usb_interface *iface; 133 134 if (hw->used >= 2) 135 return -EBUSY; 136 137 if (!us122l->first) 138 us122l->first = file; 139 140 if (us122l->is_us144) { 141 iface = usb_ifnum_to_if(us122l->dev, 0); 142 usb_autopm_get_interface(iface); 143 } 144 iface = usb_ifnum_to_if(us122l->dev, 1); 145 usb_autopm_get_interface(iface); 146 return 0; 147 } 148 149 static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file) 150 { 151 struct us122l *us122l = hw->private_data; 152 struct usb_interface *iface; 153 154 if (us122l->is_us144) { 155 iface = usb_ifnum_to_if(us122l->dev, 0); 156 usb_autopm_put_interface(iface); 157 } 158 iface = usb_ifnum_to_if(us122l->dev, 1); 159 usb_autopm_put_interface(iface); 160 if (us122l->first == file) 161 us122l->first = NULL; 162 guard(mutex)(&us122l->mutex); 163 if (us122l->master == file) 164 us122l->master = us122l->slave; 165 166 us122l->slave = NULL; 167 return 0; 168 } 169 170 static int usb_stream_hwdep_mmap(struct snd_hwdep *hw, 171 struct file *filp, struct vm_area_struct *area) 172 { 173 unsigned long size = area->vm_end - area->vm_start; 174 struct us122l *us122l = hw->private_data; 175 unsigned long offset; 176 struct usb_stream *s; 177 bool read; 178 179 offset = area->vm_pgoff << PAGE_SHIFT; 180 guard(mutex)(&us122l->mutex); 181 s = us122l->sk.s; 182 read = offset < s->read_size; 183 if (read && area->vm_flags & VM_WRITE) 184 return -EPERM; 185 /* if userspace tries to mmap beyond end of our buffer, fail */ 186 if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) { 187 dev_warn(hw->card->dev, "%s: size %lu > %u\n", __func__, 188 size, read ? s->read_size : s->write_size); 189 return -EINVAL; 190 } 191 192 area->vm_ops = &usb_stream_hwdep_vm_ops; 193 vm_flags_set(area, VM_DONTDUMP); 194 if (!read) 195 vm_flags_set(area, VM_DONTEXPAND); 196 area->vm_private_data = us122l; 197 return 0; 198 } 199 200 static __poll_t usb_stream_hwdep_poll(struct snd_hwdep *hw, 201 struct file *file, poll_table *wait) 202 { 203 struct us122l *us122l = hw->private_data; 204 unsigned int *polled; 205 __poll_t mask; 206 207 poll_wait(file, &us122l->sk.sleep, wait); 208 209 mask = EPOLLIN | EPOLLOUT | EPOLLWRNORM | EPOLLERR; 210 if (mutex_trylock(&us122l->mutex)) { 211 struct usb_stream *s = us122l->sk.s; 212 213 if (s && s->state == usb_stream_ready) { 214 if (us122l->first == file) 215 polled = &s->periods_polled; 216 else 217 polled = &us122l->second_periods_polled; 218 if (*polled != s->periods_done) { 219 *polled = s->periods_done; 220 mask = EPOLLIN | EPOLLOUT | EPOLLWRNORM; 221 } else { 222 mask = 0; 223 } 224 } 225 mutex_unlock(&us122l->mutex); 226 } 227 return mask; 228 } 229 230 static void us122l_stop(struct us122l *us122l) 231 { 232 struct list_head *p; 233 234 list_for_each(p, &us122l->midi_list) 235 snd_usbmidi_input_stop(p); 236 237 usb_stream_stop(&us122l->sk); 238 usb_stream_free(&us122l->sk); 239 } 240 241 static int us122l_set_sample_rate(struct usb_device *dev, int rate) 242 { 243 unsigned int ep = 0x81; 244 unsigned char data[3]; 245 int err; 246 247 data[0] = rate; 248 data[1] = rate >> 8; 249 data[2] = rate >> 16; 250 err = usb_control_msg_send(dev, 0, UAC_SET_CUR, 251 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT, 252 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 253 1000, GFP_NOIO); 254 if (err) 255 dev_err(&dev->dev, "%d: cannot set freq %d to ep 0x%x\n", 256 dev->devnum, rate, ep); 257 return err; 258 } 259 260 static bool us122l_start(struct us122l *us122l, 261 unsigned int rate, unsigned int period_frames) 262 { 263 struct list_head *p; 264 int err; 265 unsigned int use_packsize = 0; 266 bool success = false; 267 268 if (us122l->dev->speed == USB_SPEED_HIGH) { 269 /* The us-122l's descriptor defaults to iso max_packsize 78, 270 which isn't needed for samplerates <= 48000. 271 Lets save some memory: 272 */ 273 switch (rate) { 274 case 44100: 275 use_packsize = 36; 276 break; 277 case 48000: 278 use_packsize = 42; 279 break; 280 case 88200: 281 use_packsize = 72; 282 break; 283 } 284 } 285 if (!usb_stream_new(&us122l->sk, us122l->dev, 1, 2, 286 rate, use_packsize, period_frames, 6)) 287 goto out; 288 289 err = us122l_set_sample_rate(us122l->dev, rate); 290 if (err < 0) { 291 us122l_stop(us122l); 292 dev_err(&us122l->dev->dev, "us122l_set_sample_rate error\n"); 293 goto out; 294 } 295 err = usb_stream_start(&us122l->sk); 296 if (err < 0) { 297 us122l_stop(us122l); 298 dev_err(&us122l->dev->dev, "%s error %i\n", __func__, err); 299 goto out; 300 } 301 list_for_each(p, &us122l->midi_list) 302 snd_usbmidi_input_start(p); 303 success = true; 304 out: 305 return success; 306 } 307 308 static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, 309 unsigned int cmd, unsigned long arg) 310 { 311 struct usb_stream_config cfg; 312 struct us122l *us122l = hw->private_data; 313 struct usb_stream *s; 314 unsigned int min_period_frames; 315 int err = 0; 316 bool high_speed; 317 318 if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS) 319 return -ENOTTY; 320 321 if (copy_from_user(&cfg, (void __user *)arg, sizeof(cfg))) 322 return -EFAULT; 323 324 if (cfg.version != USB_STREAM_INTERFACE_VERSION) 325 return -ENXIO; 326 327 high_speed = us122l->dev->speed == USB_SPEED_HIGH; 328 if ((cfg.sample_rate != 44100 && cfg.sample_rate != 48000 && 329 (!high_speed || 330 (cfg.sample_rate != 88200 && cfg.sample_rate != 96000))) || 331 cfg.frame_size != 6 || 332 cfg.period_frames > 0x3000) 333 return -EINVAL; 334 335 switch (cfg.sample_rate) { 336 case 44100: 337 min_period_frames = 48; 338 break; 339 case 48000: 340 min_period_frames = 52; 341 break; 342 default: 343 min_period_frames = 104; 344 break; 345 } 346 if (!high_speed) 347 min_period_frames <<= 1; 348 if (cfg.period_frames < min_period_frames) 349 return -EINVAL; 350 351 snd_power_wait(hw->card); 352 353 guard(mutex)(&us122l->mutex); 354 s = us122l->sk.s; 355 if (!us122l->master) { 356 us122l->master = file; 357 } else if (us122l->master != file) { 358 if (!s || memcmp(&cfg, &s->cfg, sizeof(cfg))) { 359 err = -EIO; 360 goto unlock; 361 } 362 us122l->slave = file; 363 } 364 if (!s || memcmp(&cfg, &s->cfg, sizeof(cfg)) || 365 s->state == usb_stream_xrun) { 366 us122l_stop(us122l); 367 if (!us122l_start(us122l, cfg.sample_rate, cfg.period_frames)) 368 err = -EIO; 369 else 370 err = 1; 371 } 372 unlock: 373 wake_up_all(&us122l->sk.sleep); 374 return err; 375 } 376 377 #define SND_USB_STREAM_ID "USB STREAM" 378 static int usb_stream_hwdep_new(struct snd_card *card) 379 { 380 int err; 381 struct snd_hwdep *hw; 382 struct usb_device *dev = US122L(card)->dev; 383 384 err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw); 385 if (err < 0) 386 return err; 387 388 hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM; 389 hw->private_data = US122L(card); 390 hw->ops.open = usb_stream_hwdep_open; 391 hw->ops.release = usb_stream_hwdep_release; 392 hw->ops.ioctl = usb_stream_hwdep_ioctl; 393 hw->ops.ioctl_compat = usb_stream_hwdep_ioctl; 394 hw->ops.mmap = usb_stream_hwdep_mmap; 395 hw->ops.poll = usb_stream_hwdep_poll; 396 397 sprintf(hw->name, "/dev/bus/usb/%03d/%03d/hwdeppcm", 398 dev->bus->busnum, dev->devnum); 399 return 0; 400 } 401 402 static bool us122l_create_card(struct snd_card *card) 403 { 404 int err; 405 struct us122l *us122l = US122L(card); 406 407 if (us122l->is_us144) { 408 err = usb_set_interface(us122l->dev, 0, 1); 409 if (err) { 410 dev_err(card->dev, "usb_set_interface error\n"); 411 return false; 412 } 413 } 414 err = usb_set_interface(us122l->dev, 1, 1); 415 if (err) { 416 dev_err(card->dev, "usb_set_interface error\n"); 417 return false; 418 } 419 420 pt_info_set(us122l->dev, 0x11); 421 pt_info_set(us122l->dev, 0x10); 422 423 if (!us122l_start(us122l, 44100, 256)) 424 return false; 425 426 if (us122l->is_us144) 427 err = us144_create_usbmidi(card); 428 else 429 err = us122l_create_usbmidi(card); 430 if (err < 0) { 431 dev_err(card->dev, "us122l_create_usbmidi error %i\n", err); 432 goto stop; 433 } 434 err = usb_stream_hwdep_new(card); 435 if (err < 0) { 436 /* release the midi resources */ 437 struct list_head *p; 438 439 list_for_each(p, &us122l->midi_list) 440 snd_usbmidi_disconnect(p); 441 442 goto stop; 443 } 444 return true; 445 446 stop: 447 us122l_stop(us122l); 448 return false; 449 } 450 451 static void snd_us122l_free(struct snd_card *card) 452 { 453 struct us122l *us122l = US122L(card); 454 int index = us122l->card_index; 455 456 if (index >= 0 && index < SNDRV_CARDS) 457 snd_us122l_card_used[index] = 0; 458 } 459 460 static int usx2y_create_card(struct usb_device *device, 461 struct usb_interface *intf, 462 struct snd_card **cardp, 463 unsigned long flags) 464 { 465 int dev; 466 struct snd_card *card; 467 int err; 468 469 for (dev = 0; dev < SNDRV_CARDS; ++dev) 470 if (enable[dev] && !snd_us122l_card_used[dev]) 471 break; 472 if (dev >= SNDRV_CARDS) 473 return -ENODEV; 474 err = snd_card_new(&intf->dev, index[dev], id[dev], THIS_MODULE, 475 sizeof(struct us122l), &card); 476 if (err < 0) 477 return err; 478 snd_us122l_card_used[US122L(card)->card_index = dev] = 1; 479 card->private_free = snd_us122l_free; 480 US122L(card)->dev = device; 481 mutex_init(&US122L(card)->mutex); 482 US122L(card)->sk.dev = device; 483 init_waitqueue_head(&US122L(card)->sk.sleep); 484 US122L(card)->is_us144 = flags & US122L_FLAG_US144; 485 INIT_LIST_HEAD(&US122L(card)->midi_list); 486 strscpy(card->driver, "USB "NAME_ALLCAPS""); 487 sprintf(card->shortname, "TASCAM "NAME_ALLCAPS""); 488 sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)", 489 card->shortname, 490 le16_to_cpu(device->descriptor.idVendor), 491 le16_to_cpu(device->descriptor.idProduct), 492 0, 493 US122L(card)->dev->bus->busnum, 494 US122L(card)->dev->devnum 495 ); 496 *cardp = card; 497 return 0; 498 } 499 500 static int us122l_usb_probe(struct usb_interface *intf, 501 const struct usb_device_id *device_id, 502 struct snd_card **cardp) 503 { 504 struct usb_device *device = interface_to_usbdev(intf); 505 struct snd_card *card; 506 int err; 507 508 err = usx2y_create_card(device, intf, &card, device_id->driver_info); 509 if (err < 0) 510 return err; 511 512 if (!us122l_create_card(card)) { 513 snd_card_free(card); 514 return -EINVAL; 515 } 516 517 err = snd_card_register(card); 518 if (err < 0) { 519 snd_card_free(card); 520 return err; 521 } 522 523 usb_get_intf(usb_ifnum_to_if(device, 0)); 524 usb_get_dev(device); 525 *cardp = card; 526 return 0; 527 } 528 529 static int snd_us122l_probe(struct usb_interface *intf, 530 const struct usb_device_id *id) 531 { 532 struct usb_device *device = interface_to_usbdev(intf); 533 struct snd_card *card; 534 int err; 535 536 if (id->driver_info & US122L_FLAG_US144 && 537 device->speed == USB_SPEED_HIGH) { 538 dev_err(&device->dev, "disable ehci-hcd to run US-144\n"); 539 return -ENODEV; 540 } 541 542 if (intf->cur_altsetting->desc.bInterfaceNumber != 1) 543 return 0; 544 545 err = us122l_usb_probe(usb_get_intf(intf), id, &card); 546 if (err < 0) { 547 usb_put_intf(intf); 548 return err; 549 } 550 551 usb_set_intfdata(intf, card); 552 return 0; 553 } 554 555 static void snd_us122l_disconnect(struct usb_interface *intf) 556 { 557 struct snd_card *card; 558 struct us122l *us122l; 559 struct list_head *p; 560 561 card = usb_get_intfdata(intf); 562 if (!card) 563 return; 564 565 snd_card_disconnect(card); 566 567 us122l = US122L(card); 568 scoped_guard(mutex, &us122l->mutex) { 569 us122l_stop(us122l); 570 } 571 572 /* release the midi resources */ 573 list_for_each(p, &us122l->midi_list) { 574 snd_usbmidi_disconnect(p); 575 } 576 577 usb_put_intf(usb_ifnum_to_if(us122l->dev, 0)); 578 usb_put_intf(usb_ifnum_to_if(us122l->dev, 1)); 579 usb_put_dev(us122l->dev); 580 581 snd_card_free_when_closed(card); 582 } 583 584 static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message) 585 { 586 struct snd_card *card; 587 struct us122l *us122l; 588 struct list_head *p; 589 590 card = usb_get_intfdata(intf); 591 if (!card) 592 return 0; 593 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 594 595 us122l = US122L(card); 596 if (!us122l) 597 return 0; 598 599 list_for_each(p, &us122l->midi_list) 600 snd_usbmidi_input_stop(p); 601 602 guard(mutex)(&us122l->mutex); 603 usb_stream_stop(&us122l->sk); 604 605 return 0; 606 } 607 608 static int snd_us122l_resume(struct usb_interface *intf) 609 { 610 struct snd_card *card; 611 struct us122l *us122l; 612 struct list_head *p; 613 int err; 614 615 card = usb_get_intfdata(intf); 616 if (!card) 617 return 0; 618 619 us122l = US122L(card); 620 if (!us122l) 621 return 0; 622 623 guard(mutex)(&us122l->mutex); 624 /* needed, doesn't restart without: */ 625 if (us122l->is_us144) { 626 err = usb_set_interface(us122l->dev, 0, 1); 627 if (err) { 628 dev_err(&us122l->dev->dev, "usb_set_interface error\n"); 629 goto unlock; 630 } 631 } 632 err = usb_set_interface(us122l->dev, 1, 1); 633 if (err) { 634 dev_err(&us122l->dev->dev, "usb_set_interface error\n"); 635 goto unlock; 636 } 637 638 pt_info_set(us122l->dev, 0x11); 639 pt_info_set(us122l->dev, 0x10); 640 641 err = us122l_set_sample_rate(us122l->dev, 642 us122l->sk.s->cfg.sample_rate); 643 if (err < 0) { 644 dev_err(&us122l->dev->dev, "us122l_set_sample_rate error\n"); 645 goto unlock; 646 } 647 err = usb_stream_start(&us122l->sk); 648 if (err) 649 goto unlock; 650 651 list_for_each(p, &us122l->midi_list) 652 snd_usbmidi_input_start(p); 653 unlock: 654 snd_power_change_state(card, SNDRV_CTL_POWER_D0); 655 return err; 656 } 657 658 static const struct usb_device_id snd_us122l_usb_id_table[] = { 659 { 660 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 661 .idVendor = 0x0644, 662 .idProduct = USB_ID_US122L 663 }, 664 { /* US-144 only works at USB1.1! Disable module ehci-hcd. */ 665 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 666 .idVendor = 0x0644, 667 .idProduct = USB_ID_US144, 668 .driver_info = US122L_FLAG_US144 669 }, 670 { 671 .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 672 .idVendor = 0x0644, 673 .idProduct = USB_ID_US122MKII 674 }, 675 { /* terminator */ } 676 }; 677 MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table); 678 679 static struct usb_driver snd_us122l_usb_driver = { 680 .name = "snd-usb-us122l", 681 .probe = snd_us122l_probe, 682 .disconnect = snd_us122l_disconnect, 683 .suspend = snd_us122l_suspend, 684 .resume = snd_us122l_resume, 685 .reset_resume = snd_us122l_resume, 686 .id_table = snd_us122l_usb_id_table, 687 .supports_autosuspend = 1 688 }; 689 690 module_usb_driver(snd_us122l_usb_driver); 691