2 * caiaq.c: ALSA driver for caiaq/NativeInstruments devices
4 * Copyright (c) 2007 Daniel Mack <daniel@caiaq.de>
5 * Karsten Wiese <fzu@wemgehoertderstaat.de>
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.
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.
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
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>
35 #include "caiaq-device.h"
36 #include "caiaq-audio.h"
37 #include "caiaq-midi.h"
38 #include "caiaq-control.h"
40 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
41 #include "caiaq-input.h"
44 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
45 MODULE_DESCRIPTION("caiaq USB audio, version 1.3.13");
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 4 DJ},"
53 "{Native Instruments, Audio 8 DJ},"
54 "{Native Instruments, Session I/O},"
55 "{Native Instruments, GuitarRig mobile}");
57 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
58 static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
59 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
60 static int snd_card_used[SNDRV_CARDS];
62 module_param_array(index, int, NULL, 0444);
63 MODULE_PARM_DESC(index, "Index value for the caiaq sound device");
64 module_param_array(id, charp, NULL, 0444);
65 MODULE_PARM_DESC(id, "ID string for the caiaq soundcard.");
66 module_param_array(enable, bool, NULL, 0444);
67 MODULE_PARM_DESC(enable, "Enable the caiaq soundcard.");
73 SAMPLERATE_192000 = 3,
75 SAMPLERATE_INVALID = 0xff
85 static struct usb_device_id snd_usb_id_table[] = {
87 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
88 .idVendor = USB_VID_NATIVEINSTRUMENTS,
89 .idProduct = USB_PID_RIGKONTROL2
92 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
93 .idVendor = USB_VID_NATIVEINSTRUMENTS,
94 .idProduct = USB_PID_RIGKONTROL3
97 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
98 .idVendor = USB_VID_NATIVEINSTRUMENTS,
99 .idProduct = USB_PID_KORECONTROLLER
102 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
103 .idVendor = USB_VID_NATIVEINSTRUMENTS,
104 .idProduct = USB_PID_KORECONTROLLER2
107 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
108 .idVendor = USB_VID_NATIVEINSTRUMENTS,
109 .idProduct = USB_PID_AK1
112 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
113 .idVendor = USB_VID_NATIVEINSTRUMENTS,
114 .idProduct = USB_PID_AUDIO8DJ
117 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
118 .idVendor = USB_VID_NATIVEINSTRUMENTS,
119 .idProduct = USB_PID_SESSIONIO
122 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
123 .idVendor = USB_VID_NATIVEINSTRUMENTS,
124 .idProduct = USB_PID_GUITARRIGMOBILE
127 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
128 .idVendor = USB_VID_NATIVEINSTRUMENTS,
129 .idProduct = USB_PID_AUDIO4DJ
134 static void usb_ep1_command_reply_dispatch (struct urb* urb)
137 struct snd_usb_caiaqdev *dev = urb->context;
138 unsigned char *buf = urb->transfer_buffer;
140 if (urb->status || !dev) {
141 log("received EP1 urb->status = %i\n", urb->status);
146 case EP1_CMD_GET_DEVICE_INFO:
147 memcpy(&dev->spec, buf+1, sizeof(struct caiaq_device_spec));
148 dev->spec.fw_version = le16_to_cpu(dev->spec.fw_version);
149 debug("device spec (firmware %d): audio: %d in, %d out, "
150 "MIDI: %d in, %d out, data alignment %d\n",
151 dev->spec.fw_version,
152 dev->spec.num_analog_audio_in,
153 dev->spec.num_analog_audio_out,
154 dev->spec.num_midi_in,
155 dev->spec.num_midi_out,
156 dev->spec.data_alignment);
158 dev->spec_received++;
159 wake_up(&dev->ep1_wait_queue);
161 case EP1_CMD_AUDIO_PARAMS:
162 dev->audio_parm_answer = buf[1];
163 wake_up(&dev->ep1_wait_queue);
165 case EP1_CMD_MIDI_READ:
166 snd_usb_caiaq_midi_handle_input(dev, buf[1], buf + 3, buf[2]);
168 case EP1_CMD_READ_IO:
169 if (dev->chip.usb_id ==
170 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) {
171 if (urb->actual_length > sizeof(dev->control_state))
172 urb->actual_length = sizeof(dev->control_state);
173 memcpy(dev->control_state, buf + 1, urb->actual_length);
174 wake_up(&dev->ep1_wait_queue);
177 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
178 case EP1_CMD_READ_ERP:
179 case EP1_CMD_READ_ANALOG:
180 snd_usb_caiaq_input_dispatch(dev, buf, urb->actual_length);
185 dev->ep1_in_urb.actual_length = 0;
186 ret = usb_submit_urb(&dev->ep1_in_urb, GFP_ATOMIC);
188 log("unable to submit urb. OOM!?\n");
191 int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *dev,
192 unsigned char command,
193 const unsigned char *buffer,
197 struct usb_device *usb_dev = dev->chip.dev;
202 if (len > EP1_BUFSIZE - 1)
203 len = EP1_BUFSIZE - 1;
205 if (buffer && len > 0)
206 memcpy(dev->ep1_out_buf+1, buffer, len);
208 dev->ep1_out_buf[0] = command;
209 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
210 dev->ep1_out_buf, len+1, &actual_len, 200);
213 int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *dev,
214 int rate, int depth, int bpp)
220 case 44100: tmp[0] = SAMPLERATE_44100; break;
221 case 48000: tmp[0] = SAMPLERATE_48000; break;
222 case 88200: tmp[0] = SAMPLERATE_88200; break;
223 case 96000: tmp[0] = SAMPLERATE_96000; break;
224 case 192000: tmp[0] = SAMPLERATE_192000; break;
225 default: return -EINVAL;
229 case 16: tmp[1] = DEPTH_16; break;
230 case 24: tmp[1] = DEPTH_24; break;
231 default: return -EINVAL;
236 tmp[4] = 1; /* packets per microframe */
238 debug("setting audio params: %d Hz, %d bits, %d bpp\n",
241 dev->audio_parm_answer = -1;
242 ret = snd_usb_caiaq_send_command(dev, EP1_CMD_AUDIO_PARAMS,
248 if (!wait_event_timeout(dev->ep1_wait_queue,
249 dev->audio_parm_answer >= 0, HZ))
252 if (dev->audio_parm_answer != 1)
253 debug("unable to set the device's audio params\n");
257 return dev->audio_parm_answer == 1 ? 0 : -EINVAL;
260 int snd_usb_caiaq_set_auto_msg (struct snd_usb_caiaqdev *dev,
261 int digital, int analog, int erp)
263 char tmp[3] = { digital, analog, erp };
264 return snd_usb_caiaq_send_command(dev, EP1_CMD_AUTO_MSG,
268 static void __devinit setup_card(struct snd_usb_caiaqdev *dev)
273 /* device-specific startup specials */
274 switch (dev->chip.usb_id) {
275 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
276 /* RigKontrol2 - display centered dash ('-') */
280 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 3);
282 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
283 /* RigKontrol2 - display two centered dashes ('--') */
288 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 4);
290 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
291 /* Audio Kontrol 1 - make USB-LED stop blinking */
293 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 1);
295 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
296 /* Audio 8 DJ - trigger read of current settings */
297 dev->control_state[0] = 0xff;
298 snd_usb_caiaq_set_auto_msg(dev, 1, 0, 0);
299 snd_usb_caiaq_send_command(dev, EP1_CMD_READ_IO, NULL, 0);
301 if (!wait_event_timeout(dev->ep1_wait_queue,
302 dev->control_state[0] != 0xff, HZ))
305 /* fix up some defaults */
306 if ((dev->control_state[1] != 2) ||
307 (dev->control_state[2] != 3) ||
308 (dev->control_state[4] != 2)) {
309 dev->control_state[1] = 2;
310 dev->control_state[2] = 3;
311 dev->control_state[4] = 2;
312 snd_usb_caiaq_send_command(dev,
313 EP1_CMD_WRITE_IO, dev->control_state, 6);
317 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
318 /* Audio 4 DJ - default input mode to phono */
319 dev->control_state[0] = 2;
320 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO,
321 dev->control_state, 1);
325 if (dev->spec.num_analog_audio_out +
326 dev->spec.num_analog_audio_in +
327 dev->spec.num_digital_audio_out +
328 dev->spec.num_digital_audio_in > 0) {
329 ret = snd_usb_caiaq_audio_init(dev);
331 log("Unable to set up audio system (ret=%d)\n", ret);
334 if (dev->spec.num_midi_in +
335 dev->spec.num_midi_out > 0) {
336 ret = snd_usb_caiaq_midi_init(dev);
338 log("Unable to set up MIDI system (ret=%d)\n", ret);
341 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
342 ret = snd_usb_caiaq_input_init(dev);
344 log("Unable to set up input system (ret=%d)\n", ret);
347 /* finally, register the card and all its sub-instances */
348 ret = snd_card_register(dev->chip.card);
350 log("snd_card_register() returned %d\n", ret);
351 snd_card_free(dev->chip.card);
354 ret = snd_usb_caiaq_control_init(dev);
356 log("Unable to set up control system (ret=%d)\n", ret);
359 static int create_card(struct usb_device* usb_dev, struct snd_card **cardp)
363 struct snd_card *card;
364 struct snd_usb_caiaqdev *dev;
366 for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
367 if (enable[devnum] && !snd_card_used[devnum])
370 if (devnum >= SNDRV_CARDS)
373 err = snd_card_create(index[devnum], id[devnum], THIS_MODULE,
374 sizeof(struct snd_usb_caiaqdev), &card);
378 dev = caiaqdev(card);
379 dev->chip.dev = usb_dev;
380 dev->chip.card = card;
381 dev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor),
382 le16_to_cpu(usb_dev->descriptor.idProduct));
383 spin_lock_init(&dev->spinlock);
384 snd_card_set_dev(card, &usb_dev->dev);
390 static int __devinit init_card(struct snd_usb_caiaqdev *dev)
393 struct usb_device *usb_dev = dev->chip.dev;
394 struct snd_card *card = dev->chip.card;
397 if (usb_set_interface(usb_dev, 0, 1) != 0) {
398 log("can't set alt interface.\n");
402 usb_init_urb(&dev->ep1_in_urb);
403 usb_init_urb(&dev->midi_out_urb);
405 usb_fill_bulk_urb(&dev->ep1_in_urb, usb_dev,
406 usb_rcvbulkpipe(usb_dev, 0x1),
407 dev->ep1_in_buf, EP1_BUFSIZE,
408 usb_ep1_command_reply_dispatch, dev);
410 usb_fill_bulk_urb(&dev->midi_out_urb, usb_dev,
411 usb_sndbulkpipe(usb_dev, 0x1),
412 dev->midi_out_buf, EP1_BUFSIZE,
413 snd_usb_caiaq_midi_output_done, dev);
415 init_waitqueue_head(&dev->ep1_wait_queue);
416 init_waitqueue_head(&dev->prepare_wait_queue);
418 if (usb_submit_urb(&dev->ep1_in_urb, GFP_KERNEL) != 0)
421 err = snd_usb_caiaq_send_command(dev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
425 if (!wait_event_timeout(dev->ep1_wait_queue, dev->spec_received, HZ))
428 usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
429 dev->vendor_name, CAIAQ_USB_STR_LEN);
431 usb_string(usb_dev, usb_dev->descriptor.iProduct,
432 dev->product_name, CAIAQ_USB_STR_LEN);
434 usb_string(usb_dev, usb_dev->descriptor.iSerialNumber,
435 dev->serial, CAIAQ_USB_STR_LEN);
437 /* terminate serial string at first white space occurence */
438 c = strchr(dev->serial, ' ');
442 strcpy(card->driver, MODNAME);
443 strcpy(card->shortname, dev->product_name);
445 len = snprintf(card->longname, sizeof(card->longname),
446 "%s %s (serial %s, ",
447 dev->vendor_name, dev->product_name, dev->serial);
449 if (len < sizeof(card->longname) - 2)
450 len += usb_make_path(usb_dev, card->longname + len,
451 sizeof(card->longname) - len);
453 card->longname[len++] = ')';
454 card->longname[len] = '\0';
459 static int __devinit snd_probe(struct usb_interface *intf,
460 const struct usb_device_id *id)
463 struct snd_card *card;
464 struct usb_device *device = interface_to_usbdev(intf);
466 ret = create_card(device, &card);
471 usb_set_intfdata(intf, card);
472 ret = init_card(caiaqdev(card));
474 log("unable to init card! (ret=%d)\n", ret);
482 static void snd_disconnect(struct usb_interface *intf)
484 struct snd_usb_caiaqdev *dev;
485 struct snd_card *card = usb_get_intfdata(intf);
487 debug("%s(%p)\n", __func__, intf);
492 dev = caiaqdev(card);
493 snd_card_disconnect(card);
495 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
496 snd_usb_caiaq_input_free(dev);
498 snd_usb_caiaq_audio_free(dev);
500 usb_kill_urb(&dev->ep1_in_urb);
501 usb_kill_urb(&dev->midi_out_urb);
504 usb_reset_device(interface_to_usbdev(intf));
508 MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
509 static struct usb_driver snd_usb_driver = {
512 .disconnect = snd_disconnect,
513 .id_table = snd_usb_id_table,
516 static int __init snd_module_init(void)
518 return usb_register(&snd_usb_driver);
521 static void __exit snd_module_exit(void)
523 usb_deregister(&snd_usb_driver);
526 module_init(snd_module_init)
527 module_exit(snd_module_exit)