printf: Remove unused 'bprintf'
[drm/drm-misc.git] / sound / usb / usx2y / us122l.c
blob6bcf8b859ebb200a0834cf4f3c67ba3acd75811e
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de>
4 */
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"
20 MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
21 MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5");
22 MODULE_LICENSE("GPL");
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;
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".");
36 /* driver_info flags */
37 #define US122L_FLAG_US144 BIT(0)
39 static int snd_us122l_card_used[SNDRV_CARDS];
41 static int us122l_create_usbmidi(struct snd_card *card)
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
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
56 struct usb_device *dev = US122L(card)->dev;
57 struct usb_interface *iface = usb_ifnum_to_if(dev, 1);
59 return snd_usbmidi_create(card, iface,
60 &US122L(card)->midi_list, &quirk);
63 static int us144_create_usbmidi(struct snd_card *card)
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
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
78 struct usb_device *dev = US122L(card)->dev;
79 struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
81 return snd_usbmidi_create(card, iface,
82 &US122L(card)->midi_list, &quirk);
85 static void pt_info_set(struct usb_device *dev, u8 v)
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);
92 static vm_fault_t usb_stream_hwdep_vm_fault(struct vm_fault *vmf)
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;
100 mutex_lock(&us122l->mutex);
101 s = us122l->sk.s;
102 if (!s)
103 goto unlock;
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 goto unlock;
113 vaddr = us122l->sk.write_page + offset;
115 page = virt_to_page(vaddr);
117 get_page(page);
118 mutex_unlock(&us122l->mutex);
120 vmf->page = page;
122 return 0;
123 unlock:
124 mutex_unlock(&us122l->mutex);
125 return VM_FAULT_SIGBUS;
129 static const struct vm_operations_struct usb_stream_hwdep_vm_ops = {
130 .fault = usb_stream_hwdep_vm_fault,
133 static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file)
135 struct us122l *us122l = hw->private_data;
136 struct usb_interface *iface;
138 if (hw->used >= 2)
139 return -EBUSY;
141 if (!us122l->first)
142 us122l->first = file;
144 if (us122l->is_us144) {
145 iface = usb_ifnum_to_if(us122l->dev, 0);
146 usb_autopm_get_interface(iface);
148 iface = usb_ifnum_to_if(us122l->dev, 1);
149 usb_autopm_get_interface(iface);
150 return 0;
153 static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
155 struct us122l *us122l = hw->private_data;
156 struct usb_interface *iface;
158 if (us122l->is_us144) {
159 iface = usb_ifnum_to_if(us122l->dev, 0);
160 usb_autopm_put_interface(iface);
162 iface = usb_ifnum_to_if(us122l->dev, 1);
163 usb_autopm_put_interface(iface);
164 if (us122l->first == file)
165 us122l->first = NULL;
166 mutex_lock(&us122l->mutex);
167 if (us122l->master == file)
168 us122l->master = us122l->slave;
170 us122l->slave = NULL;
171 mutex_unlock(&us122l->mutex);
172 return 0;
175 static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
176 struct file *filp, struct vm_area_struct *area)
178 unsigned long size = area->vm_end - area->vm_start;
179 struct us122l *us122l = hw->private_data;
180 unsigned long offset;
181 struct usb_stream *s;
182 int err = 0;
183 bool read;
185 offset = area->vm_pgoff << PAGE_SHIFT;
186 mutex_lock(&us122l->mutex);
187 s = us122l->sk.s;
188 read = offset < s->read_size;
189 if (read && area->vm_flags & VM_WRITE) {
190 err = -EPERM;
191 goto out;
193 /* if userspace tries to mmap beyond end of our buffer, fail */
194 if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
195 dev_warn(hw->card->dev, "%s: size %lu > %u\n", __func__,
196 size, read ? s->read_size : s->write_size);
197 err = -EINVAL;
198 goto out;
201 area->vm_ops = &usb_stream_hwdep_vm_ops;
202 vm_flags_set(area, VM_DONTDUMP);
203 if (!read)
204 vm_flags_set(area, VM_DONTEXPAND);
205 area->vm_private_data = us122l;
206 out:
207 mutex_unlock(&us122l->mutex);
208 return err;
211 static __poll_t usb_stream_hwdep_poll(struct snd_hwdep *hw,
212 struct file *file, poll_table *wait)
214 struct us122l *us122l = hw->private_data;
215 unsigned int *polled;
216 __poll_t mask;
218 poll_wait(file, &us122l->sk.sleep, wait);
220 mask = EPOLLIN | EPOLLOUT | EPOLLWRNORM | EPOLLERR;
221 if (mutex_trylock(&us122l->mutex)) {
222 struct usb_stream *s = us122l->sk.s;
224 if (s && s->state == usb_stream_ready) {
225 if (us122l->first == file)
226 polled = &s->periods_polled;
227 else
228 polled = &us122l->second_periods_polled;
229 if (*polled != s->periods_done) {
230 *polled = s->periods_done;
231 mask = EPOLLIN | EPOLLOUT | EPOLLWRNORM;
232 } else {
233 mask = 0;
236 mutex_unlock(&us122l->mutex);
238 return mask;
241 static void us122l_stop(struct us122l *us122l)
243 struct list_head *p;
245 list_for_each(p, &us122l->midi_list)
246 snd_usbmidi_input_stop(p);
248 usb_stream_stop(&us122l->sk);
249 usb_stream_free(&us122l->sk);
252 static int us122l_set_sample_rate(struct usb_device *dev, int rate)
254 unsigned int ep = 0x81;
255 unsigned char data[3];
256 int err;
258 data[0] = rate;
259 data[1] = rate >> 8;
260 data[2] = rate >> 16;
261 err = usb_control_msg_send(dev, 0, UAC_SET_CUR,
262 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
263 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3,
264 1000, GFP_NOIO);
265 if (err)
266 dev_err(&dev->dev, "%d: cannot set freq %d to ep 0x%x\n",
267 dev->devnum, rate, ep);
268 return err;
271 static bool us122l_start(struct us122l *us122l,
272 unsigned int rate, unsigned int period_frames)
274 struct list_head *p;
275 int err;
276 unsigned int use_packsize = 0;
277 bool success = false;
279 if (us122l->dev->speed == USB_SPEED_HIGH) {
280 /* The us-122l's descriptor defaults to iso max_packsize 78,
281 which isn't needed for samplerates <= 48000.
282 Lets save some memory:
284 switch (rate) {
285 case 44100:
286 use_packsize = 36;
287 break;
288 case 48000:
289 use_packsize = 42;
290 break;
291 case 88200:
292 use_packsize = 72;
293 break;
296 if (!usb_stream_new(&us122l->sk, us122l->dev, 1, 2,
297 rate, use_packsize, period_frames, 6))
298 goto out;
300 err = us122l_set_sample_rate(us122l->dev, rate);
301 if (err < 0) {
302 us122l_stop(us122l);
303 dev_err(&us122l->dev->dev, "us122l_set_sample_rate error\n");
304 goto out;
306 err = usb_stream_start(&us122l->sk);
307 if (err < 0) {
308 us122l_stop(us122l);
309 dev_err(&us122l->dev->dev, "%s error %i\n", __func__, err);
310 goto out;
312 list_for_each(p, &us122l->midi_list)
313 snd_usbmidi_input_start(p);
314 success = true;
315 out:
316 return success;
319 static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
320 unsigned int cmd, unsigned long arg)
322 struct usb_stream_config cfg;
323 struct us122l *us122l = hw->private_data;
324 struct usb_stream *s;
325 unsigned int min_period_frames;
326 int err = 0;
327 bool high_speed;
329 if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
330 return -ENOTTY;
332 if (copy_from_user(&cfg, (void __user *)arg, sizeof(cfg)))
333 return -EFAULT;
335 if (cfg.version != USB_STREAM_INTERFACE_VERSION)
336 return -ENXIO;
338 high_speed = us122l->dev->speed == USB_SPEED_HIGH;
339 if ((cfg.sample_rate != 44100 && cfg.sample_rate != 48000 &&
340 (!high_speed ||
341 (cfg.sample_rate != 88200 && cfg.sample_rate != 96000))) ||
342 cfg.frame_size != 6 ||
343 cfg.period_frames > 0x3000)
344 return -EINVAL;
346 switch (cfg.sample_rate) {
347 case 44100:
348 min_period_frames = 48;
349 break;
350 case 48000:
351 min_period_frames = 52;
352 break;
353 default:
354 min_period_frames = 104;
355 break;
357 if (!high_speed)
358 min_period_frames <<= 1;
359 if (cfg.period_frames < min_period_frames)
360 return -EINVAL;
362 snd_power_wait(hw->card);
364 mutex_lock(&us122l->mutex);
365 s = us122l->sk.s;
366 if (!us122l->master) {
367 us122l->master = file;
368 } else if (us122l->master != file) {
369 if (!s || memcmp(&cfg, &s->cfg, sizeof(cfg))) {
370 err = -EIO;
371 goto unlock;
373 us122l->slave = file;
375 if (!s || memcmp(&cfg, &s->cfg, sizeof(cfg)) ||
376 s->state == usb_stream_xrun) {
377 us122l_stop(us122l);
378 if (!us122l_start(us122l, cfg.sample_rate, cfg.period_frames))
379 err = -EIO;
380 else
381 err = 1;
383 unlock:
384 mutex_unlock(&us122l->mutex);
385 wake_up_all(&us122l->sk.sleep);
386 return err;
389 #define SND_USB_STREAM_ID "USB STREAM"
390 static int usb_stream_hwdep_new(struct snd_card *card)
392 int err;
393 struct snd_hwdep *hw;
394 struct usb_device *dev = US122L(card)->dev;
396 err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw);
397 if (err < 0)
398 return err;
400 hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM;
401 hw->private_data = US122L(card);
402 hw->ops.open = usb_stream_hwdep_open;
403 hw->ops.release = usb_stream_hwdep_release;
404 hw->ops.ioctl = usb_stream_hwdep_ioctl;
405 hw->ops.ioctl_compat = usb_stream_hwdep_ioctl;
406 hw->ops.mmap = usb_stream_hwdep_mmap;
407 hw->ops.poll = usb_stream_hwdep_poll;
409 sprintf(hw->name, "/dev/bus/usb/%03d/%03d/hwdeppcm",
410 dev->bus->busnum, dev->devnum);
411 return 0;
414 static bool us122l_create_card(struct snd_card *card)
416 int err;
417 struct us122l *us122l = US122L(card);
419 if (us122l->is_us144) {
420 err = usb_set_interface(us122l->dev, 0, 1);
421 if (err) {
422 dev_err(card->dev, "usb_set_interface error\n");
423 return false;
426 err = usb_set_interface(us122l->dev, 1, 1);
427 if (err) {
428 dev_err(card->dev, "usb_set_interface error\n");
429 return false;
432 pt_info_set(us122l->dev, 0x11);
433 pt_info_set(us122l->dev, 0x10);
435 if (!us122l_start(us122l, 44100, 256))
436 return false;
438 if (us122l->is_us144)
439 err = us144_create_usbmidi(card);
440 else
441 err = us122l_create_usbmidi(card);
442 if (err < 0) {
443 dev_err(card->dev, "us122l_create_usbmidi error %i\n", err);
444 goto stop;
446 err = usb_stream_hwdep_new(card);
447 if (err < 0) {
448 /* release the midi resources */
449 struct list_head *p;
451 list_for_each(p, &us122l->midi_list)
452 snd_usbmidi_disconnect(p);
454 goto stop;
456 return true;
458 stop:
459 us122l_stop(us122l);
460 return false;
463 static void snd_us122l_free(struct snd_card *card)
465 struct us122l *us122l = US122L(card);
466 int index = us122l->card_index;
468 if (index >= 0 && index < SNDRV_CARDS)
469 snd_us122l_card_used[index] = 0;
472 static int usx2y_create_card(struct usb_device *device,
473 struct usb_interface *intf,
474 struct snd_card **cardp,
475 unsigned long flags)
477 int dev;
478 struct snd_card *card;
479 int err;
481 for (dev = 0; dev < SNDRV_CARDS; ++dev)
482 if (enable[dev] && !snd_us122l_card_used[dev])
483 break;
484 if (dev >= SNDRV_CARDS)
485 return -ENODEV;
486 err = snd_card_new(&intf->dev, index[dev], id[dev], THIS_MODULE,
487 sizeof(struct us122l), &card);
488 if (err < 0)
489 return err;
490 snd_us122l_card_used[US122L(card)->card_index = dev] = 1;
491 card->private_free = snd_us122l_free;
492 US122L(card)->dev = device;
493 mutex_init(&US122L(card)->mutex);
494 US122L(card)->sk.dev = device;
495 init_waitqueue_head(&US122L(card)->sk.sleep);
496 US122L(card)->is_us144 = flags & US122L_FLAG_US144;
497 INIT_LIST_HEAD(&US122L(card)->midi_list);
498 strcpy(card->driver, "USB "NAME_ALLCAPS"");
499 sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
500 sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
501 card->shortname,
502 le16_to_cpu(device->descriptor.idVendor),
503 le16_to_cpu(device->descriptor.idProduct),
505 US122L(card)->dev->bus->busnum,
506 US122L(card)->dev->devnum
508 *cardp = card;
509 return 0;
512 static int us122l_usb_probe(struct usb_interface *intf,
513 const struct usb_device_id *device_id,
514 struct snd_card **cardp)
516 struct usb_device *device = interface_to_usbdev(intf);
517 struct snd_card *card;
518 int err;
520 err = usx2y_create_card(device, intf, &card, device_id->driver_info);
521 if (err < 0)
522 return err;
524 if (!us122l_create_card(card)) {
525 snd_card_free(card);
526 return -EINVAL;
529 err = snd_card_register(card);
530 if (err < 0) {
531 snd_card_free(card);
532 return err;
535 usb_get_intf(usb_ifnum_to_if(device, 0));
536 usb_get_dev(device);
537 *cardp = card;
538 return 0;
541 static int snd_us122l_probe(struct usb_interface *intf,
542 const struct usb_device_id *id)
544 struct usb_device *device = interface_to_usbdev(intf);
545 struct snd_card *card;
546 int err;
548 if (id->driver_info & US122L_FLAG_US144 &&
549 device->speed == USB_SPEED_HIGH) {
550 dev_err(&device->dev, "disable ehci-hcd to run US-144\n");
551 return -ENODEV;
554 if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
555 return 0;
557 err = us122l_usb_probe(usb_get_intf(intf), id, &card);
558 if (err < 0) {
559 usb_put_intf(intf);
560 return err;
563 usb_set_intfdata(intf, card);
564 return 0;
567 static void snd_us122l_disconnect(struct usb_interface *intf)
569 struct snd_card *card;
570 struct us122l *us122l;
571 struct list_head *p;
573 card = usb_get_intfdata(intf);
574 if (!card)
575 return;
577 snd_card_disconnect(card);
579 us122l = US122L(card);
580 mutex_lock(&us122l->mutex);
581 us122l_stop(us122l);
582 mutex_unlock(&us122l->mutex);
584 /* release the midi resources */
585 list_for_each(p, &us122l->midi_list) {
586 snd_usbmidi_disconnect(p);
589 usb_put_intf(usb_ifnum_to_if(us122l->dev, 0));
590 usb_put_intf(usb_ifnum_to_if(us122l->dev, 1));
591 usb_put_dev(us122l->dev);
593 snd_card_free_when_closed(card);
596 static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message)
598 struct snd_card *card;
599 struct us122l *us122l;
600 struct list_head *p;
602 card = usb_get_intfdata(intf);
603 if (!card)
604 return 0;
605 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
607 us122l = US122L(card);
608 if (!us122l)
609 return 0;
611 list_for_each(p, &us122l->midi_list)
612 snd_usbmidi_input_stop(p);
614 mutex_lock(&us122l->mutex);
615 usb_stream_stop(&us122l->sk);
616 mutex_unlock(&us122l->mutex);
618 return 0;
621 static int snd_us122l_resume(struct usb_interface *intf)
623 struct snd_card *card;
624 struct us122l *us122l;
625 struct list_head *p;
626 int err;
628 card = usb_get_intfdata(intf);
629 if (!card)
630 return 0;
632 us122l = US122L(card);
633 if (!us122l)
634 return 0;
636 mutex_lock(&us122l->mutex);
637 /* needed, doesn't restart without: */
638 if (us122l->is_us144) {
639 err = usb_set_interface(us122l->dev, 0, 1);
640 if (err) {
641 dev_err(&us122l->dev->dev, "usb_set_interface error\n");
642 goto unlock;
645 err = usb_set_interface(us122l->dev, 1, 1);
646 if (err) {
647 dev_err(&us122l->dev->dev, "usb_set_interface error\n");
648 goto unlock;
651 pt_info_set(us122l->dev, 0x11);
652 pt_info_set(us122l->dev, 0x10);
654 err = us122l_set_sample_rate(us122l->dev,
655 us122l->sk.s->cfg.sample_rate);
656 if (err < 0) {
657 dev_err(&us122l->dev->dev, "us122l_set_sample_rate error\n");
658 goto unlock;
660 err = usb_stream_start(&us122l->sk);
661 if (err)
662 goto unlock;
664 list_for_each(p, &us122l->midi_list)
665 snd_usbmidi_input_start(p);
666 unlock:
667 mutex_unlock(&us122l->mutex);
668 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
669 return err;
672 static const struct usb_device_id snd_us122l_usb_id_table[] = {
674 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
675 .idVendor = 0x0644,
676 .idProduct = USB_ID_US122L
678 { /* US-144 only works at USB1.1! Disable module ehci-hcd. */
679 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
680 .idVendor = 0x0644,
681 .idProduct = USB_ID_US144,
682 .driver_info = US122L_FLAG_US144
685 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
686 .idVendor = 0x0644,
687 .idProduct = USB_ID_US122MKII
690 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
691 .idVendor = 0x0644,
692 .idProduct = USB_ID_US144MKII,
693 .driver_info = US122L_FLAG_US144
695 { /* terminator */ }
697 MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table);
699 static struct usb_driver snd_us122l_usb_driver = {
700 .name = "snd-usb-us122l",
701 .probe = snd_us122l_probe,
702 .disconnect = snd_us122l_disconnect,
703 .suspend = snd_us122l_suspend,
704 .resume = snd_us122l_resume,
705 .reset_resume = snd_us122l_resume,
706 .id_table = snd_us122l_usb_id_table,
707 .supports_autosuspend = 1
710 module_usb_driver(snd_us122l_usb_driver);