Add linux-next specific files for 20110831
[linux-2.6/next.git] / sound / usb / caiaq / device.c
blob45bc4a2dc6f0dc8065aec1a518df9a8bc59c9201
1 /*
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/moduleparam.h>
23 #include <linux/interrupt.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/gfp.h>
27 #include <linux/usb.h>
28 #include <sound/initval.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
32 #include "device.h"
33 #include "audio.h"
34 #include "midi.h"
35 #include "control.h"
36 #include "input.h"
38 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
39 MODULE_DESCRIPTION("caiaq USB audio");
40 MODULE_LICENSE("GPL");
41 MODULE_SUPPORTED_DEVICE("{{Native Instruments, RigKontrol2},"
42 "{Native Instruments, RigKontrol3},"
43 "{Native Instruments, Kore Controller},"
44 "{Native Instruments, Kore Controller 2},"
45 "{Native Instruments, Audio Kontrol 1},"
46 "{Native Instruments, Audio 2 DJ},"
47 "{Native Instruments, Audio 4 DJ},"
48 "{Native Instruments, Audio 8 DJ},"
49 "{Native Instruments, Traktor Audio 2},"
50 "{Native Instruments, Session I/O},"
51 "{Native Instruments, GuitarRig mobile}"
52 "{Native Instruments, Traktor Kontrol X1}"
53 "{Native Instruments, Traktor Kontrol S4}");
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.");
67 enum {
68 SAMPLERATE_44100 = 0,
69 SAMPLERATE_48000 = 1,
70 SAMPLERATE_96000 = 2,
71 SAMPLERATE_192000 = 3,
72 SAMPLERATE_88200 = 4,
73 SAMPLERATE_INVALID = 0xff
76 enum {
77 DEPTH_NONE = 0,
78 DEPTH_16 = 1,
79 DEPTH_24 = 2,
80 DEPTH_32 = 3
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
120 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
121 .idVendor = USB_VID_NATIVEINSTRUMENTS,
122 .idProduct = USB_PID_GUITARRIGMOBILE
125 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
126 .idVendor = USB_VID_NATIVEINSTRUMENTS,
127 .idProduct = USB_PID_AUDIO4DJ
130 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
131 .idVendor = USB_VID_NATIVEINSTRUMENTS,
132 .idProduct = USB_PID_AUDIO2DJ
135 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
136 .idVendor = USB_VID_NATIVEINSTRUMENTS,
137 .idProduct = USB_PID_TRAKTORKONTROLX1
140 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
141 .idVendor = USB_VID_NATIVEINSTRUMENTS,
142 .idProduct = USB_PID_TRAKTORKONTROLS4
145 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
146 .idVendor = USB_VID_NATIVEINSTRUMENTS,
147 .idProduct = USB_PID_TRAKTORAUDIO2
149 { /* terminator */ }
152 static void usb_ep1_command_reply_dispatch (struct urb* urb)
154 int ret;
155 struct snd_usb_caiaqdev *dev = urb->context;
156 unsigned char *buf = urb->transfer_buffer;
158 if (urb->status || !dev) {
159 log("received EP1 urb->status = %i\n", urb->status);
160 return;
163 switch(buf[0]) {
164 case EP1_CMD_GET_DEVICE_INFO:
165 memcpy(&dev->spec, buf+1, sizeof(struct caiaq_device_spec));
166 dev->spec.fw_version = le16_to_cpu(dev->spec.fw_version);
167 debug("device spec (firmware %d): audio: %d in, %d out, "
168 "MIDI: %d in, %d out, data alignment %d\n",
169 dev->spec.fw_version,
170 dev->spec.num_analog_audio_in,
171 dev->spec.num_analog_audio_out,
172 dev->spec.num_midi_in,
173 dev->spec.num_midi_out,
174 dev->spec.data_alignment);
176 dev->spec_received++;
177 wake_up(&dev->ep1_wait_queue);
178 break;
179 case EP1_CMD_AUDIO_PARAMS:
180 dev->audio_parm_answer = buf[1];
181 wake_up(&dev->ep1_wait_queue);
182 break;
183 case EP1_CMD_MIDI_READ:
184 snd_usb_caiaq_midi_handle_input(dev, buf[1], buf + 3, buf[2]);
185 break;
186 case EP1_CMD_READ_IO:
187 if (dev->chip.usb_id ==
188 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) {
189 if (urb->actual_length > sizeof(dev->control_state))
190 urb->actual_length = sizeof(dev->control_state);
191 memcpy(dev->control_state, buf + 1, urb->actual_length);
192 wake_up(&dev->ep1_wait_queue);
193 break;
195 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
196 case EP1_CMD_READ_ERP:
197 case EP1_CMD_READ_ANALOG:
198 snd_usb_caiaq_input_dispatch(dev, buf, urb->actual_length);
199 #endif
200 break;
203 dev->ep1_in_urb.actual_length = 0;
204 ret = usb_submit_urb(&dev->ep1_in_urb, GFP_ATOMIC);
205 if (ret < 0)
206 log("unable to submit urb. OOM!?\n");
209 int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *dev,
210 unsigned char command,
211 const unsigned char *buffer,
212 int len)
214 int actual_len;
215 struct usb_device *usb_dev = dev->chip.dev;
217 if (!usb_dev)
218 return -EIO;
220 if (len > EP1_BUFSIZE - 1)
221 len = EP1_BUFSIZE - 1;
223 if (buffer && len > 0)
224 memcpy(dev->ep1_out_buf+1, buffer, len);
226 dev->ep1_out_buf[0] = command;
227 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
228 dev->ep1_out_buf, len+1, &actual_len, 200);
231 int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *dev,
232 int rate, int depth, int bpp)
234 int ret;
235 char tmp[5];
237 switch (rate) {
238 case 44100: tmp[0] = SAMPLERATE_44100; break;
239 case 48000: tmp[0] = SAMPLERATE_48000; break;
240 case 88200: tmp[0] = SAMPLERATE_88200; break;
241 case 96000: tmp[0] = SAMPLERATE_96000; break;
242 case 192000: tmp[0] = SAMPLERATE_192000; break;
243 default: return -EINVAL;
246 switch (depth) {
247 case 16: tmp[1] = DEPTH_16; break;
248 case 24: tmp[1] = DEPTH_24; break;
249 default: return -EINVAL;
252 tmp[2] = bpp & 0xff;
253 tmp[3] = bpp >> 8;
254 tmp[4] = 1; /* packets per microframe */
256 debug("setting audio params: %d Hz, %d bits, %d bpp\n",
257 rate, depth, bpp);
259 dev->audio_parm_answer = -1;
260 ret = snd_usb_caiaq_send_command(dev, EP1_CMD_AUDIO_PARAMS,
261 tmp, sizeof(tmp));
263 if (ret)
264 return ret;
266 if (!wait_event_timeout(dev->ep1_wait_queue,
267 dev->audio_parm_answer >= 0, HZ))
268 return -EPIPE;
270 if (dev->audio_parm_answer != 1)
271 debug("unable to set the device's audio params\n");
272 else
273 dev->bpp = bpp;
275 return dev->audio_parm_answer == 1 ? 0 : -EINVAL;
278 int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev *dev,
279 int digital, int analog, int erp)
281 char tmp[3] = { digital, analog, erp };
282 return snd_usb_caiaq_send_command(dev, EP1_CMD_AUTO_MSG,
283 tmp, sizeof(tmp));
286 static void __devinit setup_card(struct snd_usb_caiaqdev *dev)
288 int ret;
289 char val[4];
291 /* device-specific startup specials */
292 switch (dev->chip.usb_id) {
293 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
294 /* RigKontrol2 - display centered dash ('-') */
295 val[0] = 0x00;
296 val[1] = 0x00;
297 val[2] = 0x01;
298 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 3);
299 break;
300 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
301 /* RigKontrol2 - display two centered dashes ('--') */
302 val[0] = 0x00;
303 val[1] = 0x40;
304 val[2] = 0x40;
305 val[3] = 0x00;
306 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 4);
307 break;
308 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
309 /* Audio Kontrol 1 - make USB-LED stop blinking */
310 val[0] = 0x00;
311 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 1);
312 break;
313 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
314 /* Audio 8 DJ - trigger read of current settings */
315 dev->control_state[0] = 0xff;
316 snd_usb_caiaq_set_auto_msg(dev, 1, 0, 0);
317 snd_usb_caiaq_send_command(dev, EP1_CMD_READ_IO, NULL, 0);
319 if (!wait_event_timeout(dev->ep1_wait_queue,
320 dev->control_state[0] != 0xff, HZ))
321 return;
323 /* fix up some defaults */
324 if ((dev->control_state[1] != 2) ||
325 (dev->control_state[2] != 3) ||
326 (dev->control_state[4] != 2)) {
327 dev->control_state[1] = 2;
328 dev->control_state[2] = 3;
329 dev->control_state[4] = 2;
330 snd_usb_caiaq_send_command(dev,
331 EP1_CMD_WRITE_IO, dev->control_state, 6);
334 break;
337 if (dev->spec.num_analog_audio_out +
338 dev->spec.num_analog_audio_in +
339 dev->spec.num_digital_audio_out +
340 dev->spec.num_digital_audio_in > 0) {
341 ret = snd_usb_caiaq_audio_init(dev);
342 if (ret < 0)
343 log("Unable to set up audio system (ret=%d)\n", ret);
346 if (dev->spec.num_midi_in +
347 dev->spec.num_midi_out > 0) {
348 ret = snd_usb_caiaq_midi_init(dev);
349 if (ret < 0)
350 log("Unable to set up MIDI system (ret=%d)\n", ret);
353 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
354 ret = snd_usb_caiaq_input_init(dev);
355 if (ret < 0)
356 log("Unable to set up input system (ret=%d)\n", ret);
357 #endif
359 /* finally, register the card and all its sub-instances */
360 ret = snd_card_register(dev->chip.card);
361 if (ret < 0) {
362 log("snd_card_register() returned %d\n", ret);
363 snd_card_free(dev->chip.card);
366 ret = snd_usb_caiaq_control_init(dev);
367 if (ret < 0)
368 log("Unable to set up control system (ret=%d)\n", ret);
371 static int create_card(struct usb_device *usb_dev,
372 struct usb_interface *intf,
373 struct snd_card **cardp)
375 int devnum;
376 int err;
377 struct snd_card *card;
378 struct snd_usb_caiaqdev *dev;
380 for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
381 if (enable[devnum] && !snd_card_used[devnum])
382 break;
384 if (devnum >= SNDRV_CARDS)
385 return -ENODEV;
387 err = snd_card_create(index[devnum], id[devnum], THIS_MODULE,
388 sizeof(struct snd_usb_caiaqdev), &card);
389 if (err < 0)
390 return err;
392 dev = caiaqdev(card);
393 dev->chip.dev = usb_dev;
394 dev->chip.card = card;
395 dev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor),
396 le16_to_cpu(usb_dev->descriptor.idProduct));
397 spin_lock_init(&dev->spinlock);
398 snd_card_set_dev(card, &intf->dev);
400 *cardp = card;
401 return 0;
404 static int __devinit init_card(struct snd_usb_caiaqdev *dev)
406 char *c, usbpath[32];
407 struct usb_device *usb_dev = dev->chip.dev;
408 struct snd_card *card = dev->chip.card;
409 int err, len;
411 if (usb_set_interface(usb_dev, 0, 1) != 0) {
412 log("can't set alt interface.\n");
413 return -EIO;
416 usb_init_urb(&dev->ep1_in_urb);
417 usb_init_urb(&dev->midi_out_urb);
419 usb_fill_bulk_urb(&dev->ep1_in_urb, usb_dev,
420 usb_rcvbulkpipe(usb_dev, 0x1),
421 dev->ep1_in_buf, EP1_BUFSIZE,
422 usb_ep1_command_reply_dispatch, dev);
424 usb_fill_bulk_urb(&dev->midi_out_urb, usb_dev,
425 usb_sndbulkpipe(usb_dev, 0x1),
426 dev->midi_out_buf, EP1_BUFSIZE,
427 snd_usb_caiaq_midi_output_done, dev);
429 init_waitqueue_head(&dev->ep1_wait_queue);
430 init_waitqueue_head(&dev->prepare_wait_queue);
432 if (usb_submit_urb(&dev->ep1_in_urb, GFP_KERNEL) != 0)
433 return -EIO;
435 err = snd_usb_caiaq_send_command(dev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
436 if (err)
437 return err;
439 if (!wait_event_timeout(dev->ep1_wait_queue, dev->spec_received, HZ))
440 return -ENODEV;
442 usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
443 dev->vendor_name, CAIAQ_USB_STR_LEN);
445 usb_string(usb_dev, usb_dev->descriptor.iProduct,
446 dev->product_name, CAIAQ_USB_STR_LEN);
448 strlcpy(card->driver, MODNAME, sizeof(card->driver));
449 strlcpy(card->shortname, dev->product_name, sizeof(card->shortname));
450 strlcpy(card->mixername, dev->product_name, sizeof(card->mixername));
452 /* if the id was not passed as module option, fill it with a shortened
453 * version of the product string which does not contain any
454 * whitespaces */
456 if (*card->id == '\0') {
457 char id[sizeof(card->id)];
459 memset(id, 0, sizeof(id));
461 for (c = card->shortname, len = 0;
462 *c && len < sizeof(card->id); c++)
463 if (*c != ' ')
464 id[len++] = *c;
466 snd_card_set_id(card, id);
469 usb_make_path(usb_dev, usbpath, sizeof(usbpath));
470 snprintf(card->longname, sizeof(card->longname),
471 "%s %s (%s)",
472 dev->vendor_name, dev->product_name, usbpath);
474 setup_card(dev);
475 return 0;
478 static int __devinit snd_probe(struct usb_interface *intf,
479 const struct usb_device_id *id)
481 int ret;
482 struct snd_card *card;
483 struct usb_device *device = interface_to_usbdev(intf);
485 ret = create_card(device, intf, &card);
487 if (ret < 0)
488 return ret;
490 usb_set_intfdata(intf, card);
491 ret = init_card(caiaqdev(card));
492 if (ret < 0) {
493 log("unable to init card! (ret=%d)\n", ret);
494 snd_card_free(card);
495 return ret;
498 return 0;
501 static void snd_disconnect(struct usb_interface *intf)
503 struct snd_usb_caiaqdev *dev;
504 struct snd_card *card = usb_get_intfdata(intf);
506 debug("%s(%p)\n", __func__, intf);
508 if (!card)
509 return;
511 dev = caiaqdev(card);
512 snd_card_disconnect(card);
514 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
515 snd_usb_caiaq_input_free(dev);
516 #endif
517 snd_usb_caiaq_audio_free(dev);
519 usb_kill_urb(&dev->ep1_in_urb);
520 usb_kill_urb(&dev->midi_out_urb);
522 snd_card_free(card);
523 usb_reset_device(interface_to_usbdev(intf));
527 MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
528 static struct usb_driver snd_usb_driver = {
529 .name = MODNAME,
530 .probe = snd_probe,
531 .disconnect = snd_disconnect,
532 .id_table = snd_usb_id_table,
535 static int __init snd_module_init(void)
537 return usb_register(&snd_usb_driver);
540 static void __exit snd_module_exit(void)
542 usb_deregister(&snd_usb_driver);
545 module_init(snd_module_init)
546 module_exit(snd_module_exit)