ALSA: usb-audio: add support for E-Mu Tracker Pre
[safe/jmp/linux-2.6] / sound / usb / usx2y / us122l.c
1 /*
2  * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <sound/core.h>
20 #include <sound/hwdep.h>
21 #include <sound/pcm.h>
22 #include <sound/initval.h>
23 #define MODNAME "US122L"
24 #include "usb_stream.c"
25 #include "../usbaudio.h"
26 #include "us122l.h"
27
28 MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
29 MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5");
30 MODULE_LICENSE("GPL");
31
32 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-max */
33 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* Id for this card */
34                                                         /* Enable this card */
35 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
36
37 module_param_array(index, int, NULL, 0444);
38 MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
39 module_param_array(id, charp, NULL, 0444);
40 MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
41 module_param_array(enable, bool, NULL, 0444);
42 MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
43
44 static int snd_us122l_card_used[SNDRV_CARDS];
45
46
47 static int us122l_create_usbmidi(struct snd_card *card)
48 {
49         static struct snd_usb_midi_endpoint_info quirk_data = {
50                 .out_ep = 4,
51                 .in_ep = 3,
52                 .out_cables =   0x001,
53                 .in_cables =    0x001
54         };
55         static struct snd_usb_audio_quirk quirk = {
56                 .vendor_name =  "US122L",
57                 .product_name = NAME_ALLCAPS,
58                 .ifnum =        1,
59                 .type = QUIRK_MIDI_US122L,
60                 .data = &quirk_data
61         };
62         struct usb_device *dev = US122L(card)->chip.dev;
63         struct usb_interface *iface = usb_ifnum_to_if(dev, 1);
64
65         return snd_usb_create_midi_interface(&US122L(card)->chip,
66                                              iface, &quirk);
67 }
68
69 /*
70  * Wrapper for usb_control_msg().
71  * Allocates a temp buffer to prevent dmaing from/to the stack.
72  */
73 static int us122l_ctl_msg(struct usb_device *dev, unsigned int pipe,
74                           __u8 request, __u8 requesttype,
75                           __u16 value, __u16 index, void *data,
76                           __u16 size, int timeout)
77 {
78         int err;
79         void *buf = NULL;
80
81         if (size > 0) {
82                 buf = kmemdup(data, size, GFP_KERNEL);
83                 if (!buf)
84                         return -ENOMEM;
85         }
86         err = usb_control_msg(dev, pipe, request, requesttype,
87                               value, index, buf, size, timeout);
88         if (size > 0) {
89                 memcpy(data, buf, size);
90                 kfree(buf);
91         }
92         return err;
93 }
94
95 static void pt_info_set(struct usb_device *dev, u8 v)
96 {
97         int ret;
98
99         ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
100                               'I',
101                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
102                               v, 0, NULL, 0, 1000);
103         snd_printdd(KERN_DEBUG "%i\n", ret);
104 }
105
106 static void usb_stream_hwdep_vm_open(struct vm_area_struct *area)
107 {
108         struct us122l *us122l = area->vm_private_data;
109         atomic_inc(&us122l->mmap_count);
110         snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
111 }
112
113 static int usb_stream_hwdep_vm_fault(struct vm_area_struct *area,
114                                      struct vm_fault *vmf)
115 {
116         unsigned long offset;
117         struct page *page;
118         void *vaddr;
119         struct us122l *us122l = area->vm_private_data;
120         struct usb_stream *s;
121         int vm_f = VM_FAULT_SIGBUS;
122
123         mutex_lock(&us122l->mutex);
124         s = us122l->sk.s;
125         if (!s)
126                 goto out;
127
128         offset = vmf->pgoff << PAGE_SHIFT;
129         if (offset < PAGE_ALIGN(s->read_size))
130                 vaddr = (char *)s + offset;
131         else {
132                 offset -= PAGE_ALIGN(s->read_size);
133                 if (offset >= PAGE_ALIGN(s->write_size))
134                         goto out;
135
136                 vaddr = us122l->sk.write_page + offset;
137         }
138         page = virt_to_page(vaddr);
139
140         get_page(page);
141         mutex_unlock(&us122l->mutex);
142
143         vmf->page = page;
144         vm_f = 0;
145 out:
146         return vm_f;
147 }
148
149 static void usb_stream_hwdep_vm_close(struct vm_area_struct *area)
150 {
151         struct us122l *us122l = area->vm_private_data;
152         atomic_dec(&us122l->mmap_count);
153         snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
154 }
155
156 static struct vm_operations_struct usb_stream_hwdep_vm_ops = {
157         .open = usb_stream_hwdep_vm_open,
158         .fault = usb_stream_hwdep_vm_fault,
159         .close = usb_stream_hwdep_vm_close,
160 };
161
162
163 static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file)
164 {
165         struct us122l   *us122l = hw->private_data;
166         struct usb_interface *iface;
167         snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
168         if (hw->used >= 2)
169                 return -EBUSY;
170
171         if (!us122l->first)
172                 us122l->first = file;
173         iface = usb_ifnum_to_if(us122l->chip.dev, 1);
174         usb_autopm_get_interface(iface);
175         return 0;
176 }
177
178 static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
179 {
180         struct us122l   *us122l = hw->private_data;
181         struct usb_interface *iface = usb_ifnum_to_if(us122l->chip.dev, 1);
182         snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
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
195 static 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         /* if userspace tries to mmap beyond end of our buffer, fail */
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_RESERVED;
225         area->vm_private_data = us122l;
226         atomic_inc(&us122l->mmap_count);
227 out:
228         mutex_unlock(&us122l->mutex);
229         return err;
230 }
231
232 static unsigned int usb_stream_hwdep_poll(struct snd_hwdep *hw,
233                                           struct file *file, poll_table *wait)
234 {
235         struct us122l   *us122l = hw->private_data;
236         struct usb_stream *s = us122l->sk.s;
237         unsigned        *polled;
238         unsigned int    mask;
239
240         poll_wait(file, &us122l->sk.sleep, wait);
241
242         switch (s->state) {
243         case usb_stream_ready:
244                 if (us122l->first == file)
245                         polled = &s->periods_polled;
246                 else
247                         polled = &us122l->second_periods_polled;
248                 if (*polled != s->periods_done) {
249                         *polled = s->periods_done;
250                         mask = POLLIN | POLLOUT | POLLWRNORM;
251                         break;
252                 }
253                 /* Fall through */
254                 mask = 0;
255                 break;
256         default:
257                 mask = POLLIN | POLLOUT | POLLWRNORM | POLLERR;
258                 break;
259         }
260         return mask;
261 }
262
263 static void us122l_stop(struct us122l *us122l)
264 {
265         struct list_head *p;
266         list_for_each(p, &us122l->chip.midi_list)
267                 snd_usbmidi_input_stop(p);
268
269         usb_stream_stop(&us122l->sk);
270         usb_stream_free(&us122l->sk);
271 }
272
273 static int us122l_set_sample_rate(struct usb_device *dev, int rate)
274 {
275         unsigned int ep = 0x81;
276         unsigned char data[3];
277         int err;
278
279         data[0] = rate;
280         data[1] = rate >> 8;
281         data[2] = rate >> 16;
282         err = us122l_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
283                              USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
284                              SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000);
285         if (err < 0)
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
291 static 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->chip.dev->speed == USB_SPEED_HIGH) {
300                 /* The us-122l's descriptor defaults to iso max_packsize 78,
301                    which isn't needed for samplerates <= 48000.
302                    Lets save some memory:
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->chip.dev, 1, 2,
317                             rate, use_packsize, period_frames, 6))
318                 goto out;
319
320         err = us122l_set_sample_rate(us122l->chip.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->chip.midi_list)
333                 snd_usbmidi_input_start(p);
334         success = true;
335 out:
336         return success;
337 }
338
339 static 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         unsigned min_period_frames;
345         int err = 0;
346         bool high_speed;
347
348         if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
349                 return -ENOTTY;
350
351         cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
352         if (!cfg)
353                 return -ENOMEM;
354
355         if (copy_from_user(cfg, (void *)arg, sizeof(*cfg))) {
356                 err = -EFAULT;
357                 goto free;
358         }
359         if (cfg->version != USB_STREAM_INTERFACE_VERSION) {
360                 err = -ENXIO;
361                 goto free;
362         }
363         high_speed = us122l->chip.dev->speed == USB_SPEED_HIGH;
364         if ((cfg->sample_rate != 44100 && cfg->sample_rate != 48000  &&
365              (!high_speed ||
366               (cfg->sample_rate != 88200 && cfg->sample_rate != 96000))) ||
367             cfg->frame_size != 6 ||
368             cfg->period_frames > 0x3000) {
369                 err = -EINVAL;
370                 goto free;
371         }
372         switch (cfg->sample_rate) {
373         case 44100:
374                 min_period_frames = 48;
375                 break;
376         case 48000:
377                 min_period_frames = 52;
378                 break;
379         default:
380                 min_period_frames = 104;
381                 break;
382         }
383         if (!high_speed)
384                 min_period_frames <<= 1;
385         if (cfg->period_frames < min_period_frames) {
386                 err = -EINVAL;
387                 goto free;
388         }
389
390         snd_power_wait(hw->card, SNDRV_CTL_POWER_D0);
391
392         mutex_lock(&us122l->mutex);
393         if (!us122l->master)
394                 us122l->master = file;
395         else if (us122l->master != file) {
396                 if (memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg))) {
397                         err = -EIO;
398                         goto unlock;
399                 }
400                 us122l->slave = file;
401         }
402         if (!us122l->sk.s ||
403             memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg)) ||
404             us122l->sk.s->state == usb_stream_xrun) {
405                 us122l_stop(us122l);
406                 if (!us122l_start(us122l, cfg->sample_rate, cfg->period_frames))
407                         err = -EIO;
408                 else
409                         err = 1;
410         }
411 unlock:
412         mutex_unlock(&us122l->mutex);
413 free:
414         kfree(cfg);
415         return err;
416 }
417
418 #define SND_USB_STREAM_ID "USB STREAM"
419 static int usb_stream_hwdep_new(struct snd_card *card)
420 {
421         int err;
422         struct snd_hwdep *hw;
423         struct usb_device *dev = US122L(card)->chip.dev;
424
425         err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw);
426         if (err < 0)
427                 return err;
428
429         hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM;
430         hw->private_data = US122L(card);
431         hw->ops.open = usb_stream_hwdep_open;
432         hw->ops.release = usb_stream_hwdep_release;
433         hw->ops.ioctl = usb_stream_hwdep_ioctl;
434         hw->ops.ioctl_compat = usb_stream_hwdep_ioctl;
435         hw->ops.mmap = usb_stream_hwdep_mmap;
436         hw->ops.poll = usb_stream_hwdep_poll;
437
438         sprintf(hw->name, "/proc/bus/usb/%03d/%03d/hwdeppcm",
439                 dev->bus->busnum, dev->devnum);
440         return 0;
441 }
442
443
444 static bool us122l_create_card(struct snd_card *card)
445 {
446         int err;
447         struct us122l *us122l = US122L(card);
448
449         err = usb_set_interface(us122l->chip.dev, 1, 1);
450         if (err) {
451                 snd_printk(KERN_ERR "usb_set_interface error \n");
452                 return false;
453         }
454
455         pt_info_set(us122l->chip.dev, 0x11);
456         pt_info_set(us122l->chip.dev, 0x10);
457
458         if (!us122l_start(us122l, 44100, 256))
459                 return false;
460
461         err = us122l_create_usbmidi(card);
462         if (err < 0) {
463                 snd_printk(KERN_ERR "us122l_create_usbmidi error %i \n", err);
464                 us122l_stop(us122l);
465                 return false;
466         }
467         err = usb_stream_hwdep_new(card);
468         if (err < 0) {
469 /* release the midi resources */
470                 struct list_head *p;
471                 list_for_each(p, &us122l->chip.midi_list)
472                         snd_usbmidi_disconnect(p);
473
474                 us122l_stop(us122l);
475                 return false;
476         }
477         return true;
478 }
479
480 static struct snd_card *usx2y_create_card(struct usb_device *device)
481 {
482         int             dev;
483         struct snd_card *card;
484         for (dev = 0; dev < SNDRV_CARDS; ++dev)
485                 if (enable[dev] && !snd_us122l_card_used[dev])
486                         break;
487         if (dev >= SNDRV_CARDS)
488                 return NULL;
489         card = snd_card_new(index[dev], id[dev], THIS_MODULE,
490                             sizeof(struct us122l));
491         if (!card)
492                 return NULL;
493         snd_us122l_card_used[US122L(card)->chip.index = dev] = 1;
494
495         US122L(card)->chip.dev = device;
496         US122L(card)->chip.card = card;
497         mutex_init(&US122L(card)->mutex);
498         init_waitqueue_head(&US122L(card)->sk.sleep);
499         INIT_LIST_HEAD(&US122L(card)->chip.midi_list);
500         strcpy(card->driver, "USB "NAME_ALLCAPS"");
501         sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
502         sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
503                 card->shortname,
504                 le16_to_cpu(device->descriptor.idVendor),
505                 le16_to_cpu(device->descriptor.idProduct),
506                 0,
507                 US122L(card)->chip.dev->bus->busnum,
508                 US122L(card)->chip.dev->devnum
509                 );
510         snd_card_set_dev(card, &device->dev);
511         return card;
512 }
513
514 static void *us122l_usb_probe(struct usb_interface *intf,
515                               const struct usb_device_id *device_id)
516 {
517         struct usb_device *device = interface_to_usbdev(intf);
518         struct snd_card *card = usx2y_create_card(device);
519
520         if (!card)
521                 return NULL;
522
523         if (!us122l_create_card(card) ||
524             snd_card_register(card) < 0) {
525                 snd_card_free(card);
526                 return NULL;
527         }
528
529         usb_get_dev(device);
530         return card;
531 }
532
533 static int snd_us122l_probe(struct usb_interface *intf,
534                             const struct usb_device_id *id)
535 {
536         struct snd_card *card;
537         snd_printdd(KERN_DEBUG"%p:%i\n",
538                     intf, intf->cur_altsetting->desc.bInterfaceNumber);
539         if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
540                 return 0;
541
542         card = us122l_usb_probe(usb_get_intf(intf), id);
543
544         if (card) {
545                 usb_set_intfdata(intf, card);
546                 return 0;
547         }
548
549         usb_put_intf(intf);
550         return -EIO;
551 }
552
553 static void snd_us122l_disconnect(struct usb_interface *intf)
554 {
555         struct snd_card *card;
556         struct us122l *us122l;
557         struct list_head *p;
558
559         card = usb_get_intfdata(intf);
560         if (!card)
561                 return;
562
563         snd_card_disconnect(card);
564
565         us122l = US122L(card);
566         mutex_lock(&us122l->mutex);
567         us122l_stop(us122l);
568         mutex_unlock(&us122l->mutex);
569         us122l->chip.shutdown = 1;
570
571 /* release the midi resources */
572         list_for_each(p, &us122l->chip.midi_list) {
573                 snd_usbmidi_disconnect(p);
574         }
575
576         usb_put_intf(intf);
577         usb_put_dev(US122L(card)->chip.dev);
578
579         while (atomic_read(&us122l->mmap_count))
580                 msleep(500);
581
582         snd_card_free(card);
583 }
584
585 static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message)
586 {
587         struct snd_card *card;
588         struct us122l *us122l;
589         struct list_head *p;
590
591         card = dev_get_drvdata(&intf->dev);
592         if (!card)
593                 return 0;
594         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
595
596         us122l = US122L(card);
597         if (!us122l)
598                 return 0;
599
600         list_for_each(p, &us122l->chip.midi_list)
601                 snd_usbmidi_input_stop(p);
602
603         mutex_lock(&us122l->mutex);
604         usb_stream_stop(&us122l->sk);
605         mutex_unlock(&us122l->mutex);
606
607         return 0;
608 }
609
610 static int snd_us122l_resume(struct usb_interface *intf)
611 {
612         struct snd_card *card;
613         struct us122l *us122l;
614         struct list_head *p;
615         int err;
616
617         card = dev_get_drvdata(&intf->dev);
618         if (!card)
619                 return 0;
620
621         us122l = US122L(card);
622         if (!us122l)
623                 return 0;
624
625         mutex_lock(&us122l->mutex);
626         /* needed, doesn't restart without: */
627         err = usb_set_interface(us122l->chip.dev, 1, 1);
628         if (err) {
629                 snd_printk(KERN_ERR "usb_set_interface error \n");
630                 goto unlock;
631         }
632
633         pt_info_set(us122l->chip.dev, 0x11);
634         pt_info_set(us122l->chip.dev, 0x10);
635
636         err = us122l_set_sample_rate(us122l->chip.dev,
637                                      us122l->sk.s->cfg.sample_rate);
638         if (err < 0) {
639                 snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
640                 goto unlock;
641         }
642         err = usb_stream_start(&us122l->sk);
643         if (err)
644                 goto unlock;
645
646         list_for_each(p, &us122l->chip.midi_list)
647                 snd_usbmidi_input_start(p);
648 unlock:
649         mutex_unlock(&us122l->mutex);
650         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
651         return err;
652 }
653
654 static struct usb_device_id snd_us122l_usb_id_table[] = {
655         {
656                 .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
657                 .idVendor =     0x0644,
658                 .idProduct =    USB_ID_US122L
659         },
660 /*      { */            /* US-144 maybe works when @USB1.1. Untested. */
661 /*              .match_flags =  USB_DEVICE_ID_MATCH_DEVICE, */
662 /*              .idVendor =     0x0644, */
663 /*              .idProduct =    USB_ID_US144 */
664 /*      }, */
665         { /* terminator */ }
666 };
667
668 MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table);
669 static struct usb_driver snd_us122l_usb_driver = {
670         .name =         "snd-usb-us122l",
671         .probe =        snd_us122l_probe,
672         .disconnect =   snd_us122l_disconnect,
673         .suspend =      snd_us122l_suspend,
674         .resume =       snd_us122l_resume,
675         .reset_resume = snd_us122l_resume,
676         .id_table =     snd_us122l_usb_id_table,
677         .supports_autosuspend = 1
678 };
679
680
681 static int __init snd_us122l_module_init(void)
682 {
683         return usb_register(&snd_us122l_usb_driver);
684 }
685
686 static void __exit snd_us122l_module_exit(void)
687 {
688         usb_deregister(&snd_us122l_usb_driver);
689 }
690
691 module_init(snd_us122l_module_init)
692 module_exit(snd_us122l_module_exit)