1
2
3
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;
25static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
26
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
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 int ret;
88
89 ret = usb_control_msg_send(dev, 0, 'I',
90 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
91 v, 0, NULL, 0, 1000, GFP_NOIO);
92 snd_printdd(KERN_DEBUG "%i\n", ret);
93}
94
95static void usb_stream_hwdep_vm_open(struct vm_area_struct *area)
96{
97 struct us122l *us122l = area->vm_private_data;
98 atomic_inc(&us122l->mmap_count);
99 snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
100}
101
102static vm_fault_t usb_stream_hwdep_vm_fault(struct vm_fault *vmf)
103{
104 unsigned long offset;
105 struct page *page;
106 void *vaddr;
107 struct us122l *us122l = vmf->vma->vm_private_data;
108 struct usb_stream *s;
109
110 mutex_lock(&us122l->mutex);
111 s = us122l->sk.s;
112 if (!s)
113 goto unlock;
114
115 offset = vmf->pgoff << PAGE_SHIFT;
116 if (offset < PAGE_ALIGN(s->read_size))
117 vaddr = (char *)s + offset;
118 else {
119 offset -= PAGE_ALIGN(s->read_size);
120 if (offset >= PAGE_ALIGN(s->write_size))
121 goto unlock;
122
123 vaddr = us122l->sk.write_page + offset;
124 }
125 page = virt_to_page(vaddr);
126
127 get_page(page);
128 mutex_unlock(&us122l->mutex);
129
130 vmf->page = page;
131
132 return 0;
133unlock:
134 mutex_unlock(&us122l->mutex);
135 return VM_FAULT_SIGBUS;
136}
137
138static void usb_stream_hwdep_vm_close(struct vm_area_struct *area)
139{
140 struct us122l *us122l = area->vm_private_data;
141 atomic_dec(&us122l->mmap_count);
142 snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
143}
144
145static const struct vm_operations_struct usb_stream_hwdep_vm_ops = {
146 .open = usb_stream_hwdep_vm_open,
147 .fault = usb_stream_hwdep_vm_fault,
148 .close = usb_stream_hwdep_vm_close,
149};
150
151
152static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file)
153{
154 struct us122l *us122l = hw->private_data;
155 struct usb_interface *iface;
156 snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
157 if (hw->used >= 2)
158 return -EBUSY;
159
160 if (!us122l->first)
161 us122l->first = file;
162
163 if (us122l->is_us144) {
164 iface = usb_ifnum_to_if(us122l->dev, 0);
165 usb_autopm_get_interface(iface);
166 }
167 iface = usb_ifnum_to_if(us122l->dev, 1);
168 usb_autopm_get_interface(iface);
169 return 0;
170}
171
172static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
173{
174 struct us122l *us122l = hw->private_data;
175 struct usb_interface *iface;
176 snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
177
178 if (us122l->is_us144) {
179 iface = usb_ifnum_to_if(us122l->dev, 0);
180 usb_autopm_put_interface(iface);
181 }
182 iface = usb_ifnum_to_if(us122l->dev, 1);
183 usb_autopm_put_interface(iface);
184 if (us122l->first == file)
185 us122l->first = NULL;
186 mutex_lock(&us122l->mutex);
187 if (us122l->master == file)
188 us122l->master = us122l->slave;
189
190 us122l->slave = NULL;
191 mutex_unlock(&us122l->mutex);
192 return 0;
193}
194
195static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
196 struct file *filp, struct vm_area_struct *area)
197{
198 unsigned long size = area->vm_end - area->vm_start;
199 struct us122l *us122l = hw->private_data;
200 unsigned long offset;
201 struct usb_stream *s;
202 int err = 0;
203 bool read;
204
205 offset = area->vm_pgoff << PAGE_SHIFT;
206 mutex_lock(&us122l->mutex);
207 s = us122l->sk.s;
208 read = offset < s->read_size;
209 if (read && area->vm_flags & VM_WRITE) {
210 err = -EPERM;
211 goto out;
212 }
213 snd_printdd(KERN_DEBUG "%lu %u\n", size,
214 read ? s->read_size : s->write_size);
215
216 if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
217 snd_printk(KERN_WARNING "%lu > %u\n", size,
218 read ? s->read_size : s->write_size);
219 err = -EINVAL;
220 goto out;
221 }
222
223 area->vm_ops = &usb_stream_hwdep_vm_ops;
224 area->vm_flags |= VM_DONTDUMP;
225 if (!read)
226 area->vm_flags |= VM_DONTEXPAND;
227 area->vm_private_data = us122l;
228 atomic_inc(&us122l->mmap_count);
229out:
230 mutex_unlock(&us122l->mutex);
231 return err;
232}
233
234static __poll_t usb_stream_hwdep_poll(struct snd_hwdep *hw,
235 struct file *file, poll_table *wait)
236{
237 struct us122l *us122l = hw->private_data;
238 unsigned *polled;
239 __poll_t mask;
240
241 poll_wait(file, &us122l->sk.sleep, wait);
242
243 mask = EPOLLIN | EPOLLOUT | EPOLLWRNORM | EPOLLERR;
244 if (mutex_trylock(&us122l->mutex)) {
245 struct usb_stream *s = us122l->sk.s;
246 if (s && s->state == usb_stream_ready) {
247 if (us122l->first == file)
248 polled = &s->periods_polled;
249 else
250 polled = &us122l->second_periods_polled;
251 if (*polled != s->periods_done) {
252 *polled = s->periods_done;
253 mask = EPOLLIN | EPOLLOUT | EPOLLWRNORM;
254 } else
255 mask = 0;
256 }
257 mutex_unlock(&us122l->mutex);
258 }
259 return mask;
260}
261
262static void us122l_stop(struct us122l *us122l)
263{
264 struct list_head *p;
265 list_for_each(p, &us122l->midi_list)
266 snd_usbmidi_input_stop(p);
267
268 usb_stream_stop(&us122l->sk);
269 usb_stream_free(&us122l->sk);
270}
271
272static int us122l_set_sample_rate(struct usb_device *dev, int rate)
273{
274 unsigned int ep = 0x81;
275 unsigned char data[3];
276 int err;
277
278 data[0] = rate;
279 data[1] = rate >> 8;
280 data[2] = rate >> 16;
281 err = usb_control_msg_send(dev, 0, UAC_SET_CUR,
282 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
283 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3,
284 1000, GFP_NOIO);
285 if (err)
286 snd_printk(KERN_ERR "%d: cannot set freq %d to ep 0x%x\n",
287 dev->devnum, rate, ep);
288 return err;
289}
290
291static bool us122l_start(struct us122l *us122l,
292 unsigned rate, unsigned period_frames)
293{
294 struct list_head *p;
295 int err;
296 unsigned use_packsize = 0;
297 bool success = false;
298
299 if (us122l->dev->speed == USB_SPEED_HIGH) {
300
301
302
303
304 switch (rate) {
305 case 44100:
306 use_packsize = 36;
307 break;
308 case 48000:
309 use_packsize = 42;
310 break;
311 case 88200:
312 use_packsize = 72;
313 break;
314 }
315 }
316 if (!usb_stream_new(&us122l->sk, us122l->dev, 1, 2,
317 rate, use_packsize, period_frames, 6))
318 goto out;
319
320 err = us122l_set_sample_rate(us122l->dev, rate);
321 if (err < 0) {
322 us122l_stop(us122l);
323 snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
324 goto out;
325 }
326 err = usb_stream_start(&us122l->sk);
327 if (err < 0) {
328 us122l_stop(us122l);
329 snd_printk(KERN_ERR "us122l_start error %i \n", err);
330 goto out;
331 }
332 list_for_each(p, &us122l->midi_list)
333 snd_usbmidi_input_start(p);
334 success = true;
335out:
336 return success;
337}
338
339static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
340 unsigned cmd, unsigned long arg)
341{
342 struct usb_stream_config cfg;
343 struct us122l *us122l = hw->private_data;
344 struct usb_stream *s;
345 unsigned min_period_frames;
346 int err = 0;
347 bool high_speed;
348
349 if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
350 return -ENOTTY;
351
352 if (copy_from_user(&cfg, (void __user *)arg, sizeof(cfg)))
353 return -EFAULT;
354
355 if (cfg.version != USB_STREAM_INTERFACE_VERSION)
356 return -ENXIO;
357
358 high_speed = us122l->dev->speed == USB_SPEED_HIGH;
359 if ((cfg.sample_rate != 44100 && cfg.sample_rate != 48000 &&
360 (!high_speed ||
361 (cfg.sample_rate != 88200 && cfg.sample_rate != 96000))) ||
362 cfg.frame_size != 6 ||
363 cfg.period_frames > 0x3000)
364 return -EINVAL;
365
366 switch (cfg.sample_rate) {
367 case 44100:
368 min_period_frames = 48;
369 break;
370 case 48000:
371 min_period_frames = 52;
372 break;
373 default:
374 min_period_frames = 104;
375 break;
376 }
377 if (!high_speed)
378 min_period_frames <<= 1;
379 if (cfg.period_frames < min_period_frames)
380 return -EINVAL;
381
382 snd_power_wait(hw->card, SNDRV_CTL_POWER_D0);
383
384 mutex_lock(&us122l->mutex);
385 s = us122l->sk.s;
386 if (!us122l->master)
387 us122l->master = file;
388 else if (us122l->master != file) {
389 if (!s || memcmp(&cfg, &s->cfg, sizeof(cfg))) {
390 err = -EIO;
391 goto unlock;
392 }
393 us122l->slave = file;
394 }
395 if (!s || memcmp(&cfg, &s->cfg, sizeof(cfg)) ||
396 s->state == usb_stream_xrun) {
397 us122l_stop(us122l);
398 if (!us122l_start(us122l, cfg.sample_rate, cfg.period_frames))
399 err = -EIO;
400 else
401 err = 1;
402 }
403unlock:
404 mutex_unlock(&us122l->mutex);
405 wake_up_all(&us122l->sk.sleep);
406 return err;
407}
408
409#define SND_USB_STREAM_ID "USB STREAM"
410static int usb_stream_hwdep_new(struct snd_card *card)
411{
412 int err;
413 struct snd_hwdep *hw;
414 struct usb_device *dev = US122L(card)->dev;
415
416 err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw);
417 if (err < 0)
418 return err;
419
420 hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM;
421 hw->private_data = US122L(card);
422 hw->ops.open = usb_stream_hwdep_open;
423 hw->ops.release = usb_stream_hwdep_release;
424 hw->ops.ioctl = usb_stream_hwdep_ioctl;
425 hw->ops.ioctl_compat = usb_stream_hwdep_ioctl;
426 hw->ops.mmap = usb_stream_hwdep_mmap;
427 hw->ops.poll = usb_stream_hwdep_poll;
428
429 sprintf(hw->name, "/dev/bus/usb/%03d/%03d/hwdeppcm",
430 dev->bus->busnum, dev->devnum);
431 return 0;
432}
433
434
435static bool us122l_create_card(struct snd_card *card)
436{
437 int err;
438 struct us122l *us122l = US122L(card);
439
440 if (us122l->is_us144) {
441 err = usb_set_interface(us122l->dev, 0, 1);
442 if (err) {
443 snd_printk(KERN_ERR "usb_set_interface error \n");
444 return false;
445 }
446 }
447 err = usb_set_interface(us122l->dev, 1, 1);
448 if (err) {
449 snd_printk(KERN_ERR "usb_set_interface error \n");
450 return false;
451 }
452
453 pt_info_set(us122l->dev, 0x11);
454 pt_info_set(us122l->dev, 0x10);
455
456 if (!us122l_start(us122l, 44100, 256))
457 return false;
458
459 if (us122l->is_us144)
460 err = us144_create_usbmidi(card);
461 else
462 err = us122l_create_usbmidi(card);
463 if (err < 0) {
464 snd_printk(KERN_ERR "us122l_create_usbmidi error %i \n", err);
465 goto stop;
466 }
467 err = usb_stream_hwdep_new(card);
468 if (err < 0) {
469
470 struct list_head *p;
471 list_for_each(p, &us122l->midi_list)
472 snd_usbmidi_disconnect(p);
473
474 goto stop;
475 }
476 return true;
477
478stop:
479 us122l_stop(us122l);
480 return false;
481}
482
483static void snd_us122l_free(struct snd_card *card)
484{
485 struct us122l *us122l = US122L(card);
486 int index = us122l->card_index;
487 if (index >= 0 && index < SNDRV_CARDS)
488 snd_us122l_card_used[index] = 0;
489}
490
491static int usx2y_create_card(struct usb_device *device,
492 struct usb_interface *intf,
493 struct snd_card **cardp,
494 unsigned long flags)
495{
496 int dev;
497 struct snd_card *card;
498 int err;
499
500 for (dev = 0; dev < SNDRV_CARDS; ++dev)
501 if (enable[dev] && !snd_us122l_card_used[dev])
502 break;
503 if (dev >= SNDRV_CARDS)
504 return -ENODEV;
505 err = snd_card_new(&intf->dev, index[dev], id[dev], THIS_MODULE,
506 sizeof(struct us122l), &card);
507 if (err < 0)
508 return err;
509 snd_us122l_card_used[US122L(card)->card_index = dev] = 1;
510 card->private_free = snd_us122l_free;
511 US122L(card)->dev = device;
512 mutex_init(&US122L(card)->mutex);
513 init_waitqueue_head(&US122L(card)->sk.sleep);
514 US122L(card)->is_us144 = flags & US122L_FLAG_US144;
515 INIT_LIST_HEAD(&US122L(card)->midi_list);
516 strcpy(card->driver, "USB "NAME_ALLCAPS"");
517 sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
518 sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
519 card->shortname,
520 le16_to_cpu(device->descriptor.idVendor),
521 le16_to_cpu(device->descriptor.idProduct),
522 0,
523 US122L(card)->dev->bus->busnum,
524 US122L(card)->dev->devnum
525 );
526 *cardp = card;
527 return 0;
528}
529
530static int us122l_usb_probe(struct usb_interface *intf,
531 const struct usb_device_id *device_id,
532 struct snd_card **cardp)
533{
534 struct usb_device *device = interface_to_usbdev(intf);
535 struct snd_card *card;
536 int err;
537
538 err = usx2y_create_card(device, intf, &card, device_id->driver_info);
539 if (err < 0)
540 return err;
541
542 if (!us122l_create_card(card)) {
543 snd_card_free(card);
544 return -EINVAL;
545 }
546
547 err = snd_card_register(card);
548 if (err < 0) {
549 snd_card_free(card);
550 return err;
551 }
552
553 usb_get_intf(usb_ifnum_to_if(device, 0));
554 usb_get_dev(device);
555 *cardp = card;
556 return 0;
557}
558
559static int snd_us122l_probe(struct usb_interface *intf,
560 const struct usb_device_id *id)
561{
562 struct usb_device *device = interface_to_usbdev(intf);
563 struct snd_card *card;
564 int err;
565
566 if (id->driver_info & US122L_FLAG_US144 &&
567 device->speed == USB_SPEED_HIGH) {
568 snd_printk(KERN_ERR "disable ehci-hcd to run US-144 \n");
569 return -ENODEV;
570 }
571
572 snd_printdd(KERN_DEBUG"%p:%i\n",
573 intf, intf->cur_altsetting->desc.bInterfaceNumber);
574 if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
575 return 0;
576
577 err = us122l_usb_probe(usb_get_intf(intf), id, &card);
578 if (err < 0) {
579 usb_put_intf(intf);
580 return err;
581 }
582
583 usb_set_intfdata(intf, card);
584 return 0;
585}
586
587static void snd_us122l_disconnect(struct usb_interface *intf)
588{
589 struct snd_card *card;
590 struct us122l *us122l;
591 struct list_head *p;
592
593 card = usb_get_intfdata(intf);
594 if (!card)
595 return;
596
597 snd_card_disconnect(card);
598
599 us122l = US122L(card);
600 mutex_lock(&us122l->mutex);
601 us122l_stop(us122l);
602 mutex_unlock(&us122l->mutex);
603
604
605 list_for_each(p, &us122l->midi_list) {
606 snd_usbmidi_disconnect(p);
607 }
608
609 usb_put_intf(usb_ifnum_to_if(us122l->dev, 0));
610 usb_put_intf(usb_ifnum_to_if(us122l->dev, 1));
611 usb_put_dev(us122l->dev);
612
613 while (atomic_read(&us122l->mmap_count))
614 msleep(500);
615
616 snd_card_free(card);
617}
618
619static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message)
620{
621 struct snd_card *card;
622 struct us122l *us122l;
623 struct list_head *p;
624
625 card = usb_get_intfdata(intf);
626 if (!card)
627 return 0;
628 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
629
630 us122l = US122L(card);
631 if (!us122l)
632 return 0;
633
634 list_for_each(p, &us122l->midi_list)
635 snd_usbmidi_input_stop(p);
636
637 mutex_lock(&us122l->mutex);
638 usb_stream_stop(&us122l->sk);
639 mutex_unlock(&us122l->mutex);
640
641 return 0;
642}
643
644static int snd_us122l_resume(struct usb_interface *intf)
645{
646 struct snd_card *card;
647 struct us122l *us122l;
648 struct list_head *p;
649 int err;
650
651 card = usb_get_intfdata(intf);
652 if (!card)
653 return 0;
654
655 us122l = US122L(card);
656 if (!us122l)
657 return 0;
658
659 mutex_lock(&us122l->mutex);
660
661 if (us122l->is_us144) {
662 err = usb_set_interface(us122l->dev, 0, 1);
663 if (err) {
664 snd_printk(KERN_ERR "usb_set_interface error \n");
665 goto unlock;
666 }
667 }
668 err = usb_set_interface(us122l->dev, 1, 1);
669 if (err) {
670 snd_printk(KERN_ERR "usb_set_interface error \n");
671 goto unlock;
672 }
673
674 pt_info_set(us122l->dev, 0x11);
675 pt_info_set(us122l->dev, 0x10);
676
677 err = us122l_set_sample_rate(us122l->dev,
678 us122l->sk.s->cfg.sample_rate);
679 if (err < 0) {
680 snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
681 goto unlock;
682 }
683 err = usb_stream_start(&us122l->sk);
684 if (err)
685 goto unlock;
686
687 list_for_each(p, &us122l->midi_list)
688 snd_usbmidi_input_start(p);
689unlock:
690 mutex_unlock(&us122l->mutex);
691 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
692 return err;
693}
694
695static const struct usb_device_id snd_us122l_usb_id_table[] = {
696 {
697 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
698 .idVendor = 0x0644,
699 .idProduct = USB_ID_US122L
700 },
701 {
702 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
703 .idVendor = 0x0644,
704 .idProduct = USB_ID_US144,
705 .driver_info = US122L_FLAG_US144
706 },
707 {
708 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
709 .idVendor = 0x0644,
710 .idProduct = USB_ID_US122MKII
711 },
712 {
713 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
714 .idVendor = 0x0644,
715 .idProduct = USB_ID_US144MKII,
716 .driver_info = US122L_FLAG_US144
717 },
718 { }
719};
720
721MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table);
722static struct usb_driver snd_us122l_usb_driver = {
723 .name = "snd-usb-us122l",
724 .probe = snd_us122l_probe,
725 .disconnect = snd_us122l_disconnect,
726 .suspend = snd_us122l_suspend,
727 .resume = snd_us122l_resume,
728 .reset_resume = snd_us122l_resume,
729 .id_table = snd_us122l_usb_id_table,
730 .supports_autosuspend = 1
731};
732
733module_usb_driver(snd_us122l_usb_driver);
734