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