ALSA: snd_usb_caiaq: fix legacy input streaming
[linux-2.6.git] / sound / usb / caiaq / device.c
1 /*
2  * caiaq.c: ALSA driver for caiaq/NativeInstruments devices
3  *
4  *   Copyright (c) 2007 Daniel Mack <daniel@caiaq.de>
5  *                      Karsten Wiese <fzu@wemgehoertderstaat.de>
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 2 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 */
21
22 #include <linux/moduleparam.h>
23 #include <linux/interrupt.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/usb.h>
27 #include <sound/initval.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30
31 #include "device.h"
32 #include "audio.h"
33 #include "midi.h"
34 #include "control.h"
35 #include "input.h"
36
37 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
38 MODULE_DESCRIPTION("caiaq USB audio, version 1.3.17");
39 MODULE_LICENSE("GPL");
40 MODULE_SUPPORTED_DEVICE("{{Native Instruments, RigKontrol2},"
41                          "{Native Instruments, RigKontrol3},"
42                          "{Native Instruments, Kore Controller},"
43                          "{Native Instruments, Kore Controller 2},"
44                          "{Native Instruments, Audio Kontrol 1},"
45                          "{Native Instruments, Audio 4 DJ},"
46                          "{Native Instruments, Audio 8 DJ},"
47                          "{Native Instruments, Session I/O},"
48                          "{Native Instruments, GuitarRig mobile}");
49
50 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
51 static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
52 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
53 static int snd_card_used[SNDRV_CARDS];
54
55 module_param_array(index, int, NULL, 0444);
56 MODULE_PARM_DESC(index, "Index value for the caiaq sound device");
57 module_param_array(id, charp, NULL, 0444);
58 MODULE_PARM_DESC(id, "ID string for the caiaq soundcard.");
59 module_param_array(enable, bool, NULL, 0444);
60 MODULE_PARM_DESC(enable, "Enable the caiaq soundcard.");
61
62 enum {
63         SAMPLERATE_44100        = 0,
64         SAMPLERATE_48000        = 1,
65         SAMPLERATE_96000        = 2,
66         SAMPLERATE_192000       = 3,
67         SAMPLERATE_88200        = 4,
68         SAMPLERATE_INVALID      = 0xff
69 };
70
71 enum {
72         DEPTH_NONE      = 0,
73         DEPTH_16        = 1,
74         DEPTH_24        = 2,
75         DEPTH_32        = 3
76 };
77
78 static struct usb_device_id snd_usb_id_table[] = {
79         {
80                 .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
81                 .idVendor =     USB_VID_NATIVEINSTRUMENTS,
82                 .idProduct =    USB_PID_RIGKONTROL2
83         },
84         {
85                 .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
86                 .idVendor =     USB_VID_NATIVEINSTRUMENTS,
87                 .idProduct =    USB_PID_RIGKONTROL3
88         },
89         {
90                 .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
91                 .idVendor =     USB_VID_NATIVEINSTRUMENTS,
92                 .idProduct =    USB_PID_KORECONTROLLER
93         },
94         {
95                 .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
96                 .idVendor =     USB_VID_NATIVEINSTRUMENTS,
97                 .idProduct =    USB_PID_KORECONTROLLER2
98         },
99         {
100                 .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
101                 .idVendor =     USB_VID_NATIVEINSTRUMENTS,
102                 .idProduct =    USB_PID_AK1
103         },
104         {
105                 .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
106                 .idVendor =     USB_VID_NATIVEINSTRUMENTS,
107                 .idProduct =    USB_PID_AUDIO8DJ
108         },
109         {
110                 .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
111                 .idVendor =     USB_VID_NATIVEINSTRUMENTS,
112                 .idProduct =    USB_PID_SESSIONIO
113         },
114         {
115                 .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
116                 .idVendor =     USB_VID_NATIVEINSTRUMENTS,
117                 .idProduct =    USB_PID_GUITARRIGMOBILE
118         },
119         {
120                 .match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
121                 .idVendor =     USB_VID_NATIVEINSTRUMENTS,
122                 .idProduct =    USB_PID_AUDIO4DJ
123         },
124         { /* terminator */ }
125 };
126
127 static void usb_ep1_command_reply_dispatch (struct urb* urb)
128 {
129         int ret;
130         struct snd_usb_caiaqdev *dev = urb->context;
131         unsigned char *buf = urb->transfer_buffer;
132
133         if (urb->status || !dev) {
134                 log("received EP1 urb->status = %i\n", urb->status);
135                 return;
136         }
137
138         switch(buf[0]) {
139         case EP1_CMD_GET_DEVICE_INFO:
140                 memcpy(&dev->spec, buf+1, sizeof(struct caiaq_device_spec));
141                 dev->spec.fw_version = le16_to_cpu(dev->spec.fw_version);
142                 debug("device spec (firmware %d): audio: %d in, %d out, "
143                         "MIDI: %d in, %d out, data alignment %d\n",
144                         dev->spec.fw_version,
145                         dev->spec.num_analog_audio_in,
146                         dev->spec.num_analog_audio_out,
147                         dev->spec.num_midi_in,
148                         dev->spec.num_midi_out,
149                         dev->spec.data_alignment);
150
151                 dev->spec_received++;
152                 wake_up(&dev->ep1_wait_queue);
153                 break;
154         case EP1_CMD_AUDIO_PARAMS:
155                 dev->audio_parm_answer = buf[1];
156                 wake_up(&dev->ep1_wait_queue);
157                 break;
158         case EP1_CMD_MIDI_READ:
159                 snd_usb_caiaq_midi_handle_input(dev, buf[1], buf + 3, buf[2]);
160                 break;
161         case EP1_CMD_READ_IO:
162                 if (dev->chip.usb_id ==
163                         USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) {
164                         if (urb->actual_length > sizeof(dev->control_state))
165                                 urb->actual_length = sizeof(dev->control_state);
166                         memcpy(dev->control_state, buf + 1, urb->actual_length);
167                         wake_up(&dev->ep1_wait_queue);
168                         break;
169                 }
170 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
171         case EP1_CMD_READ_ERP:
172         case EP1_CMD_READ_ANALOG:
173                 snd_usb_caiaq_input_dispatch(dev, buf, urb->actual_length);
174 #endif
175                 break;
176         }
177
178         dev->ep1_in_urb.actual_length = 0;
179         ret = usb_submit_urb(&dev->ep1_in_urb, GFP_ATOMIC);
180         if (ret < 0)
181                 log("unable to submit urb. OOM!?\n");
182 }
183
184 int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *dev,
185                                unsigned char command,
186                                const unsigned char *buffer,
187                                int len)
188 {
189         int actual_len;
190         struct usb_device *usb_dev = dev->chip.dev;
191
192         if (!usb_dev)
193                 return -EIO;
194
195         if (len > EP1_BUFSIZE - 1)
196                 len = EP1_BUFSIZE - 1;
197
198         if (buffer && len > 0)
199                 memcpy(dev->ep1_out_buf+1, buffer, len);
200
201         dev->ep1_out_buf[0] = command;
202         return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
203                            dev->ep1_out_buf, len+1, &actual_len, 200);
204 }
205
206 int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *dev,
207                                     int rate, int depth, int bpp)
208 {
209         int ret;
210         char tmp[5];
211
212         switch (rate) {
213         case 44100:     tmp[0] = SAMPLERATE_44100;   break;
214         case 48000:     tmp[0] = SAMPLERATE_48000;   break;
215         case 88200:     tmp[0] = SAMPLERATE_88200;   break;
216         case 96000:     tmp[0] = SAMPLERATE_96000;   break;
217         case 192000:    tmp[0] = SAMPLERATE_192000;  break;
218         default:        return -EINVAL;
219         }
220
221         switch (depth) {
222         case 16:        tmp[1] = DEPTH_16;   break;
223         case 24:        tmp[1] = DEPTH_24;   break;
224         default:        return -EINVAL;
225         }
226
227         tmp[2] = bpp & 0xff;
228         tmp[3] = bpp >> 8;
229         tmp[4] = 1; /* packets per microframe */
230
231         debug("setting audio params: %d Hz, %d bits, %d bpp\n",
232                 rate, depth, bpp);
233
234         dev->audio_parm_answer = -1;
235         ret = snd_usb_caiaq_send_command(dev, EP1_CMD_AUDIO_PARAMS,
236                                          tmp, sizeof(tmp));
237
238         if (ret)
239                 return ret;
240
241         if (!wait_event_timeout(dev->ep1_wait_queue,
242             dev->audio_parm_answer >= 0, HZ))
243                 return -EPIPE;
244
245         if (dev->audio_parm_answer != 1)
246                 debug("unable to set the device's audio params\n");
247         else
248                 dev->bpp = bpp;
249
250         return dev->audio_parm_answer == 1 ? 0 : -EINVAL;
251 }
252
253 int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev *dev,
254                                int digital, int analog, int erp)
255 {
256         char tmp[3] = { digital, analog, erp };
257         return snd_usb_caiaq_send_command(dev, EP1_CMD_AUTO_MSG,
258                                           tmp, sizeof(tmp));
259 }
260
261 static void __devinit setup_card(struct snd_usb_caiaqdev *dev)
262 {
263         int ret;
264         char val[4];
265
266         /* device-specific startup specials */
267         switch (dev->chip.usb_id) {
268         case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
269                 /* RigKontrol2 - display centered dash ('-') */
270                 val[0] = 0x00;
271                 val[1] = 0x00;
272                 val[2] = 0x01;
273                 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 3);
274                 break;
275         case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
276                 /* RigKontrol2 - display two centered dashes ('--') */
277                 val[0] = 0x00;
278                 val[1] = 0x40;
279                 val[2] = 0x40;
280                 val[3] = 0x00;
281                 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 4);
282                 break;
283         case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
284                 /* Audio Kontrol 1 - make USB-LED stop blinking */
285                 val[0] = 0x00;
286                 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 1);
287                 break;
288         case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
289                 /* Audio 8 DJ - trigger read of current settings */
290                 dev->control_state[0] = 0xff;
291                 snd_usb_caiaq_set_auto_msg(dev, 1, 0, 0);
292                 snd_usb_caiaq_send_command(dev, EP1_CMD_READ_IO, NULL, 0);
293
294                 if (!wait_event_timeout(dev->ep1_wait_queue,
295                                         dev->control_state[0] != 0xff, HZ))
296                         return;
297
298                 /* fix up some defaults */
299                 if ((dev->control_state[1] != 2) ||
300                     (dev->control_state[2] != 3) ||
301                     (dev->control_state[4] != 2)) {
302                         dev->control_state[1] = 2;
303                         dev->control_state[2] = 3;
304                         dev->control_state[4] = 2;
305                         snd_usb_caiaq_send_command(dev,
306                                 EP1_CMD_WRITE_IO, dev->control_state, 6);
307                 }
308
309                 break;
310         case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
311                 /* Audio 4 DJ - default input mode to phono */
312                 dev->control_state[0] = 2;
313                 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO,
314                         dev->control_state, 1);
315                 break;
316         }
317
318         if (dev->spec.num_analog_audio_out +
319             dev->spec.num_analog_audio_in +
320             dev->spec.num_digital_audio_out +
321             dev->spec.num_digital_audio_in > 0) {
322                 ret = snd_usb_caiaq_audio_init(dev);
323                 if (ret < 0)
324                         log("Unable to set up audio system (ret=%d)\n", ret);
325         }
326
327         if (dev->spec.num_midi_in +
328             dev->spec.num_midi_out > 0) {
329                 ret = snd_usb_caiaq_midi_init(dev);
330                 if (ret < 0)
331                         log("Unable to set up MIDI system (ret=%d)\n", ret);
332         }
333
334 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
335         ret = snd_usb_caiaq_input_init(dev);
336         if (ret < 0)
337                 log("Unable to set up input system (ret=%d)\n", ret);
338 #endif
339
340         /* finally, register the card and all its sub-instances */
341         ret = snd_card_register(dev->chip.card);
342         if (ret < 0) {
343                 log("snd_card_register() returned %d\n", ret);
344                 snd_card_free(dev->chip.card);
345         }
346
347         ret = snd_usb_caiaq_control_init(dev);
348         if (ret < 0)
349                 log("Unable to set up control system (ret=%d)\n", ret);
350 }
351
352 static int create_card(struct usb_device* usb_dev, struct snd_card **cardp)
353 {
354         int devnum;
355         int err;
356         struct snd_card *card;
357         struct snd_usb_caiaqdev *dev;
358
359         for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
360                 if (enable[devnum] && !snd_card_used[devnum])
361                         break;
362
363         if (devnum >= SNDRV_CARDS)
364                 return -ENODEV;
365
366         err = snd_card_create(index[devnum], id[devnum], THIS_MODULE,
367                               sizeof(struct snd_usb_caiaqdev), &card);
368         if (err < 0)
369                 return err;
370
371         dev = caiaqdev(card);
372         dev->chip.dev = usb_dev;
373         dev->chip.card = card;
374         dev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor),
375                                   le16_to_cpu(usb_dev->descriptor.idProduct));
376         spin_lock_init(&dev->spinlock);
377         snd_card_set_dev(card, &usb_dev->dev);
378
379         *cardp = card;
380         return 0;
381 }
382
383 static int __devinit init_card(struct snd_usb_caiaqdev *dev)
384 {
385         char *c, usbpath[32];
386         struct usb_device *usb_dev = dev->chip.dev;
387         struct snd_card *card = dev->chip.card;
388         int err, len;
389
390         if (usb_set_interface(usb_dev, 0, 1) != 0) {
391                 log("can't set alt interface.\n");
392                 return -EIO;
393         }
394
395         usb_init_urb(&dev->ep1_in_urb);
396         usb_init_urb(&dev->midi_out_urb);
397
398         usb_fill_bulk_urb(&dev->ep1_in_urb, usb_dev,
399                           usb_rcvbulkpipe(usb_dev, 0x1),
400                           dev->ep1_in_buf, EP1_BUFSIZE,
401                           usb_ep1_command_reply_dispatch, dev);
402
403         usb_fill_bulk_urb(&dev->midi_out_urb, usb_dev,
404                           usb_sndbulkpipe(usb_dev, 0x1),
405                           dev->midi_out_buf, EP1_BUFSIZE,
406                           snd_usb_caiaq_midi_output_done, dev);
407
408         init_waitqueue_head(&dev->ep1_wait_queue);
409         init_waitqueue_head(&dev->prepare_wait_queue);
410
411         if (usb_submit_urb(&dev->ep1_in_urb, GFP_KERNEL) != 0)
412                 return -EIO;
413
414         err = snd_usb_caiaq_send_command(dev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
415         if (err)
416                 return err;
417
418         if (!wait_event_timeout(dev->ep1_wait_queue, dev->spec_received, HZ))
419                 return -ENODEV;
420
421         usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
422                    dev->vendor_name, CAIAQ_USB_STR_LEN);
423
424         usb_string(usb_dev, usb_dev->descriptor.iProduct,
425                    dev->product_name, CAIAQ_USB_STR_LEN);
426
427         strlcpy(card->driver, MODNAME, sizeof(card->driver));
428         strlcpy(card->shortname, dev->product_name, sizeof(card->shortname));
429         strlcpy(card->mixername, dev->product_name, sizeof(card->mixername));
430
431         /* if the id was not passed as module option, fill it with a shortened
432          * version of the product string which does not contain any
433          * whitespaces */
434
435         if (*card->id == '\0') {
436                 char id[sizeof(card->id)];
437
438                 memset(id, 0, sizeof(id));
439
440                 for (c = card->shortname, len = 0;
441                         *c && len < sizeof(card->id); c++)
442                         if (*c != ' ')
443                                 id[len++] = *c;
444
445                 snd_card_set_id(card, id);
446         }
447
448         usb_make_path(usb_dev, usbpath, sizeof(usbpath));
449         snprintf(card->longname, sizeof(card->longname),
450                        "%s %s (%s)",
451                        dev->vendor_name, dev->product_name, usbpath);
452
453         setup_card(dev);
454         return 0;
455 }
456
457 static int __devinit snd_probe(struct usb_interface *intf,
458                      const struct usb_device_id *id)
459 {
460         int ret;
461         struct snd_card *card;
462         struct usb_device *device = interface_to_usbdev(intf);
463
464         ret = create_card(device, &card);
465
466         if (ret < 0)
467                 return ret;
468
469         usb_set_intfdata(intf, card);
470         ret = init_card(caiaqdev(card));
471         if (ret < 0) {
472                 log("unable to init card! (ret=%d)\n", ret);
473                 snd_card_free(card);
474                 return ret;
475         }
476
477         return 0;
478 }
479
480 static void snd_disconnect(struct usb_interface *intf)
481 {
482         struct snd_usb_caiaqdev *dev;
483         struct snd_card *card = usb_get_intfdata(intf);
484
485         debug("%s(%p)\n", __func__, intf);
486
487         if (!card)
488                 return;
489
490         dev = caiaqdev(card);
491         snd_card_disconnect(card);
492
493 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
494         snd_usb_caiaq_input_free(dev);
495 #endif
496         snd_usb_caiaq_audio_free(dev);
497
498         usb_kill_urb(&dev->ep1_in_urb);
499         usb_kill_urb(&dev->midi_out_urb);
500
501         snd_card_free(card);
502         usb_reset_device(interface_to_usbdev(intf));
503 }
504
505
506 MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
507 static struct usb_driver snd_usb_driver = {
508         .name           = MODNAME,
509         .probe          = snd_probe,
510         .disconnect     = snd_disconnect,
511         .id_table       = snd_usb_id_table,
512 };
513
514 static int __init snd_module_init(void)
515 {
516         return usb_register(&snd_usb_driver);
517 }
518
519 static void __exit snd_module_exit(void)
520 {
521         usb_deregister(&snd_usb_driver);
522 }
523
524 module_init(snd_module_init)
525 module_exit(snd_module_exit)
526