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