Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
20MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
21MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5");
22MODULE_LICENSE("GPL");
23
24static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
25static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
26 /* Enable this card */
27static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
28
29module_param_array(index, int, NULL, 0444);
30MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
31module_param_array(id, charp, NULL, 0444);
32MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
33module_param_array(enable, bool, NULL, 0444);
34MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
35
36/* driver_info flags */
37#define US122L_FLAG_US144 BIT(0)
38
39static int snd_us122l_card_used[SNDRV_CARDS];
40
41static 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
63static 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
85static 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
92static 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
125static const struct vm_operations_struct usb_stream_hwdep_vm_ops = {
126 .fault = usb_stream_hwdep_vm_fault,
127};
128
129static 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
149static 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
170static 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
200static __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
230static 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
241static 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
260static 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;
304out:
305 return success;
306}
307
308static 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 }
372unlock:
373 wake_up_all(&us122l->sk.sleep);
374 return err;
375}
376
377#define SND_USB_STREAM_ID "USB STREAM"
378static 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
402static 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
446stop:
447 us122l_stop(us122l);
448 return false;
449}
450
451static 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
460static 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
500static 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 *cardp = card;
524 return 0;
525}
526
527static int snd_us122l_probe(struct usb_interface *intf,
528 const struct usb_device_id *id)
529{
530 struct usb_device *device = interface_to_usbdev(intf);
531 struct snd_card *card;
532 int err;
533
534 if (id->driver_info & US122L_FLAG_US144 &&
535 device->speed == USB_SPEED_HIGH) {
536 dev_err(&device->dev, "disable ehci-hcd to run US-144\n");
537 return -ENODEV;
538 }
539
540 if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
541 return 0;
542
543 err = us122l_usb_probe(intf, id, &card);
544 if (err < 0)
545 return err;
546
547 usb_set_intfdata(intf, card);
548 return 0;
549}
550
551static void snd_us122l_disconnect(struct usb_interface *intf)
552{
553 struct snd_card *card;
554 struct us122l *us122l;
555 struct list_head *p;
556
557 card = usb_get_intfdata(intf);
558 if (!card)
559 return;
560
561 snd_card_disconnect(card);
562
563 us122l = US122L(card);
564 scoped_guard(mutex, &us122l->mutex) {
565 us122l_stop(us122l);
566 }
567
568 /* release the midi resources */
569 list_for_each(p, &us122l->midi_list) {
570 snd_usbmidi_disconnect(p);
571 }
572
573 snd_card_free_when_closed(card);
574}
575
576static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message)
577{
578 struct snd_card *card;
579 struct us122l *us122l;
580 struct list_head *p;
581
582 card = usb_get_intfdata(intf);
583 if (!card)
584 return 0;
585 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
586
587 us122l = US122L(card);
588 if (!us122l)
589 return 0;
590
591 list_for_each(p, &us122l->midi_list)
592 snd_usbmidi_input_stop(p);
593
594 guard(mutex)(&us122l->mutex);
595 usb_stream_stop(&us122l->sk);
596
597 return 0;
598}
599
600static int snd_us122l_resume(struct usb_interface *intf)
601{
602 struct snd_card *card;
603 struct us122l *us122l;
604 struct list_head *p;
605 int err;
606
607 card = usb_get_intfdata(intf);
608 if (!card)
609 return 0;
610
611 us122l = US122L(card);
612 if (!us122l)
613 return 0;
614
615 guard(mutex)(&us122l->mutex);
616 /* needed, doesn't restart without: */
617 if (us122l->is_us144) {
618 err = usb_set_interface(us122l->dev, 0, 1);
619 if (err) {
620 dev_err(&us122l->dev->dev, "usb_set_interface error\n");
621 goto unlock;
622 }
623 }
624 err = usb_set_interface(us122l->dev, 1, 1);
625 if (err) {
626 dev_err(&us122l->dev->dev, "usb_set_interface error\n");
627 goto unlock;
628 }
629
630 pt_info_set(us122l->dev, 0x11);
631 pt_info_set(us122l->dev, 0x10);
632
633 err = us122l_set_sample_rate(us122l->dev,
634 us122l->sk.s->cfg.sample_rate);
635 if (err < 0) {
636 dev_err(&us122l->dev->dev, "us122l_set_sample_rate error\n");
637 goto unlock;
638 }
639 err = usb_stream_start(&us122l->sk);
640 if (err)
641 goto unlock;
642
643 list_for_each(p, &us122l->midi_list)
644 snd_usbmidi_input_start(p);
645unlock:
646 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
647 return err;
648}
649
650static const struct usb_device_id snd_us122l_usb_id_table[] = {
651 {
652 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
653 .idVendor = 0x0644,
654 .idProduct = USB_ID_US122L
655 },
656 { /* US-144 only works at USB1.1! Disable module ehci-hcd. */
657 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
658 .idVendor = 0x0644,
659 .idProduct = USB_ID_US144,
660 .driver_info = US122L_FLAG_US144
661 },
662 {
663 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
664 .idVendor = 0x0644,
665 .idProduct = USB_ID_US122MKII
666 },
667 { /* terminator */ }
668};
669MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table);
670
671static struct usb_driver snd_us122l_usb_driver = {
672 .name = "snd-usb-us122l",
673 .probe = snd_us122l_probe,
674 .disconnect = snd_us122l_disconnect,
675 .suspend = snd_us122l_suspend,
676 .resume = snd_us122l_resume,
677 .reset_resume = snd_us122l_resume,
678 .id_table = snd_us122l_usb_id_table,
679 .supports_autosuspend = 1
680};
681
682module_usb_driver(snd_us122l_usb_driver);