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.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}}");
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];
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.");
71 SAMPLERATE_192000 = 3,
73 SAMPLERATE_INVALID = 0xff
83 static struct usb_device_id snd_usb_id_table[] = {
85 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
86 .idVendor = USB_VID_NATIVEINSTRUMENTS,
87 .idProduct = USB_PID_RIGKONTROL2
90 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
91 .idVendor = USB_VID_NATIVEINSTRUMENTS,
92 .idProduct = USB_PID_RIGKONTROL3
95 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
96 .idVendor = USB_VID_NATIVEINSTRUMENTS,
97 .idProduct = USB_PID_KORECONTROLLER
100 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
101 .idVendor = USB_VID_NATIVEINSTRUMENTS,
102 .idProduct = USB_PID_KORECONTROLLER2
105 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
106 .idVendor = USB_VID_NATIVEINSTRUMENTS,
107 .idProduct = USB_PID_AK1
110 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
111 .idVendor = USB_VID_NATIVEINSTRUMENTS,
112 .idProduct = USB_PID_AUDIO8DJ
115 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
116 .idVendor = USB_VID_NATIVEINSTRUMENTS,
117 .idProduct = USB_PID_SESSIONIO
122 static void usb_ep1_command_reply_dispatch (struct urb* urb)
125 struct snd_usb_caiaqdev *dev = urb->context;
126 unsigned char *buf = urb->transfer_buffer;
128 if (urb->status || !dev) {
129 log("received EP1 urb->status = %i\n", urb->status);
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);
146 dev->spec_received++;
147 wake_up(&dev->ep1_wait_queue);
149 case EP1_CMD_AUDIO_PARAMS:
150 dev->audio_parm_answer = buf[1];
151 wake_up(&dev->ep1_wait_queue);
153 case EP1_CMD_MIDI_READ:
154 snd_usb_caiaq_midi_handle_input(dev, buf[1], buf + 3, buf[2]);
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);
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);
173 dev->ep1_in_urb.actual_length = 0;
174 ret = usb_submit_urb(&dev->ep1_in_urb, GFP_ATOMIC);
176 log("unable to submit urb. OOM!?\n");
179 int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *dev,
180 unsigned char command,
181 const unsigned char *buffer,
185 struct usb_device *usb_dev = dev->chip.dev;
190 if (len > EP1_BUFSIZE - 1)
191 len = EP1_BUFSIZE - 1;
193 if (buffer && len > 0)
194 memcpy(dev->ep1_out_buf+1, buffer, len);
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);
201 int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *dev,
202 int rate, int depth, int bpp)
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;
217 case 16: tmp[1] = DEPTH_16; break;
218 case 24: tmp[1] = DEPTH_24; break;
219 default: return -EINVAL;
224 tmp[4] = 1; /* packets per microframe */
226 debug("setting audio params: %d Hz, %d bits, %d bpp\n",
229 dev->audio_parm_answer = -1;
230 ret = snd_usb_caiaq_send_command(dev, EP1_CMD_AUDIO_PARAMS,
236 if (!wait_event_timeout(dev->ep1_wait_queue,
237 dev->audio_parm_answer >= 0, HZ))
240 if (dev->audio_parm_answer != 1)
241 debug("unable to set the device's audio params\n");
243 return dev->audio_parm_answer == 1 ? 0 : -EINVAL;
246 int snd_usb_caiaq_set_auto_msg (struct snd_usb_caiaqdev *dev,
247 int digital, int analog, int erp)
249 char tmp[3] = { digital, analog, erp };
250 return snd_usb_caiaq_send_command(dev, EP1_CMD_AUTO_MSG,
254 static void __devinit setup_card(struct snd_usb_caiaqdev *dev)
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 ('-') */
266 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 3);
268 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
269 /* RigKontrol2 - display two centered dashes ('--') */
274 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 4);
276 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
277 /* Audio Kontrol 1 - make USB-LED stop blinking */
279 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 1);
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);
287 if (!wait_event_timeout(dev->ep1_wait_queue,
288 dev->control_state[0] != 0xff, HZ))
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);
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);
311 log("Unable to set up audio system (ret=%d)\n", ret);
314 if (dev->spec.num_midi_in +
315 dev->spec.num_midi_out > 0) {
316 ret = snd_usb_caiaq_midi_init(dev);
318 log("Unable to set up MIDI system (ret=%d)\n", ret);
321 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
322 ret = snd_usb_caiaq_input_init(dev);
324 log("Unable to set up input system (ret=%d)\n", ret);
327 /* finally, register the card and all its sub-instances */
328 ret = snd_card_register(dev->chip.card);
330 log("snd_card_register() returned %d\n", ret);
331 snd_card_free(dev->chip.card);
334 ret = snd_usb_caiaq_control_init(dev);
336 log("Unable to set up control system (ret=%d)\n", ret);
339 static int create_card(struct usb_device* usb_dev, struct snd_card **cardp)
343 struct snd_card *card;
344 struct snd_usb_caiaqdev *dev;
346 for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
347 if (enable[devnum] && !snd_card_used[devnum])
350 if (devnum >= SNDRV_CARDS)
353 err = snd_card_create(index[devnum], id[devnum], THIS_MODULE,
354 sizeof(struct snd_usb_caiaqdev), &card);
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);
370 static int __devinit init_card(struct snd_usb_caiaqdev *dev)
373 struct usb_device *usb_dev = dev->chip.dev;
374 struct snd_card *card = dev->chip.card;
377 if (usb_set_interface(usb_dev, 0, 1) != 0) {
378 log("can't set alt interface.\n");
382 usb_init_urb(&dev->ep1_in_urb);
383 usb_init_urb(&dev->midi_out_urb);
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);
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);
395 init_waitqueue_head(&dev->ep1_wait_queue);
396 init_waitqueue_head(&dev->prepare_wait_queue);
398 if (usb_submit_urb(&dev->ep1_in_urb, GFP_KERNEL) != 0)
401 err = snd_usb_caiaq_send_command(dev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
405 if (!wait_event_timeout(dev->ep1_wait_queue, dev->spec_received, HZ))
408 usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
409 dev->vendor_name, CAIAQ_USB_STR_LEN);
411 usb_string(usb_dev, usb_dev->descriptor.iProduct,
412 dev->product_name, CAIAQ_USB_STR_LEN);
414 usb_string(usb_dev, usb_dev->descriptor.iSerialNumber,
415 dev->serial, CAIAQ_USB_STR_LEN);
417 /* terminate serial string at first white space occurence */
418 c = strchr(dev->serial, ' ');
422 strcpy(card->driver, MODNAME);
423 strcpy(card->shortname, dev->product_name);
425 len = snprintf(card->longname, sizeof(card->longname),
426 "%s %s (serial %s, ",
427 dev->vendor_name, dev->product_name, dev->serial);
429 if (len < sizeof(card->longname) - 2)
430 len += usb_make_path(usb_dev, card->longname + len,
431 sizeof(card->longname) - len);
433 card->longname[len++] = ')';
434 card->longname[len] = '\0';
439 static int __devinit snd_probe(struct usb_interface *intf,
440 const struct usb_device_id *id)
443 struct snd_card *card;
444 struct usb_device *device = interface_to_usbdev(intf);
446 ret = create_card(device, &card);
451 usb_set_intfdata(intf, card);
452 ret = init_card(caiaqdev(card));
454 log("unable to init card! (ret=%d)\n", ret);
462 static void snd_disconnect(struct usb_interface *intf)
464 struct snd_usb_caiaqdev *dev;
465 struct snd_card *card = usb_get_intfdata(intf);
467 debug("%s(%p)\n", __func__, intf);
472 dev = caiaqdev(card);
473 snd_card_disconnect(card);
475 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
476 snd_usb_caiaq_input_free(dev);
478 snd_usb_caiaq_audio_free(dev);
480 usb_kill_urb(&dev->ep1_in_urb);
481 usb_kill_urb(&dev->midi_out_urb);
484 usb_reset_device(interface_to_usbdev(intf));
488 MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
489 static struct usb_driver snd_usb_driver = {
492 .disconnect = snd_disconnect,
493 .id_table = snd_usb_id_table,
496 static int __init snd_module_init(void)
498 return usb_register(&snd_usb_driver);
501 static void __exit snd_module_exit(void)
503 usb_deregister(&snd_usb_driver);
506 module_init(snd_module_init)
507 module_exit(snd_module_exit)