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