Merge branch 'topic/sscape-fix' into for-linus
[linux-2.6] / 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.10");
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 int create_card(struct usb_device* usb_dev, struct snd_card **cardp)
340 {
341         int devnum;
342         int err;
343         struct snd_card *card;
344         struct snd_usb_caiaqdev *dev;
345
346         for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
347                 if (enable[devnum] && !snd_card_used[devnum])
348                         break;
349
350         if (devnum >= SNDRV_CARDS)
351                 return -ENODEV;
352
353         err = snd_card_create(index[devnum], id[devnum], THIS_MODULE, 
354                               sizeof(struct snd_usb_caiaqdev), &card);
355         if (err < 0)
356                 return err;
357
358         dev = caiaqdev(card);
359         dev->chip.dev = usb_dev;
360         dev->chip.card = card;
361         dev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor),
362                                   le16_to_cpu(usb_dev->descriptor.idProduct));
363         spin_lock_init(&dev->spinlock);
364         snd_card_set_dev(card, &usb_dev->dev);
365
366         *cardp = card;
367         return 0;
368 }
369
370 static int __devinit init_card(struct snd_usb_caiaqdev *dev)
371 {
372         char *c;
373         struct usb_device *usb_dev = dev->chip.dev;
374         struct snd_card *card = dev->chip.card;
375         int err, len;
376         
377         if (usb_set_interface(usb_dev, 0, 1) != 0) {
378                 log("can't set alt interface.\n");
379                 return -EIO;
380         }
381
382         usb_init_urb(&dev->ep1_in_urb);
383         usb_init_urb(&dev->midi_out_urb);
384
385         usb_fill_bulk_urb(&dev->ep1_in_urb, usb_dev, 
386                           usb_rcvbulkpipe(usb_dev, 0x1),
387                           dev->ep1_in_buf, EP1_BUFSIZE, 
388                           usb_ep1_command_reply_dispatch, dev);
389
390         usb_fill_bulk_urb(&dev->midi_out_urb, usb_dev, 
391                           usb_sndbulkpipe(usb_dev, 0x1),
392                           dev->midi_out_buf, EP1_BUFSIZE, 
393                           snd_usb_caiaq_midi_output_done, dev);
394         
395         init_waitqueue_head(&dev->ep1_wait_queue);
396         init_waitqueue_head(&dev->prepare_wait_queue);
397         
398         if (usb_submit_urb(&dev->ep1_in_urb, GFP_KERNEL) != 0)
399                 return -EIO;
400
401         err = snd_usb_caiaq_send_command(dev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
402         if (err)
403                 return err;
404
405         if (!wait_event_timeout(dev->ep1_wait_queue, dev->spec_received, HZ))
406                 return -ENODEV;
407
408         usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
409                    dev->vendor_name, CAIAQ_USB_STR_LEN);
410         
411         usb_string(usb_dev, usb_dev->descriptor.iProduct,
412                    dev->product_name, CAIAQ_USB_STR_LEN);
413         
414         usb_string(usb_dev, usb_dev->descriptor.iSerialNumber,
415                    dev->serial, CAIAQ_USB_STR_LEN);
416
417         /* terminate serial string at first white space occurence */
418         c = strchr(dev->serial, ' ');
419         if (c)
420                 *c = '\0';
421         
422         strcpy(card->driver, MODNAME);
423         strcpy(card->shortname, dev->product_name);
424
425         len = snprintf(card->longname, sizeof(card->longname),
426                        "%s %s (serial %s, ",
427                        dev->vendor_name, dev->product_name, dev->serial);
428
429         if (len < sizeof(card->longname) - 2)
430                 len += usb_make_path(usb_dev, card->longname + len,
431                                      sizeof(card->longname) - len);
432
433         card->longname[len++] = ')';
434         card->longname[len] = '\0';
435         setup_card(dev);
436         return 0;
437 }
438
439 static int __devinit snd_probe(struct usb_interface *intf, 
440                      const struct usb_device_id *id)
441 {
442         int ret;
443         struct snd_card *card;
444         struct usb_device *device = interface_to_usbdev(intf);
445         
446         ret = create_card(device, &card);
447         
448         if (ret < 0)
449                 return ret;
450                         
451         usb_set_intfdata(intf, card);
452         ret = init_card(caiaqdev(card));
453         if (ret < 0) {
454                 log("unable to init card! (ret=%d)\n", ret);
455                 snd_card_free(card);
456                 return ret;
457         }
458         
459         return 0;
460 }
461
462 static void snd_disconnect(struct usb_interface *intf)
463 {
464         struct snd_usb_caiaqdev *dev;
465         struct snd_card *card = usb_get_intfdata(intf);
466
467         debug("%s(%p)\n", __func__, intf);
468
469         if (!card)
470                 return;
471
472         dev = caiaqdev(card);
473         snd_card_disconnect(card);
474
475 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
476         snd_usb_caiaq_input_free(dev);
477 #endif
478         snd_usb_caiaq_audio_free(dev);
479         
480         usb_kill_urb(&dev->ep1_in_urb);
481         usb_kill_urb(&dev->midi_out_urb);
482         
483         snd_card_free(card);
484         usb_reset_device(interface_to_usbdev(intf));
485 }
486
487
488 MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
489 static struct usb_driver snd_usb_driver = {
490         .name           = MODNAME,
491         .probe          = snd_probe,
492         .disconnect     = snd_disconnect,
493         .id_table       = snd_usb_id_table,
494 };
495
496 static int __init snd_module_init(void)
497 {
498         return usb_register(&snd_usb_driver);
499 }
500
501 static void __exit snd_module_exit(void)
502 {
503         usb_deregister(&snd_usb_driver);
504 }
505
506 module_init(snd_module_init)
507 module_exit(snd_module_exit)
508