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