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