Merge branch 'perfcounters-fixes-for-linus' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.git] / sound / usb / caiaq / device.c
CommitLineData
523f1dce
DM
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
523f1dce
DM
22#include <linux/moduleparam.h>
23#include <linux/interrupt.h>
e431cf45
DM
24#include <linux/module.h>
25#include <linux/init.h>
523f1dce 26#include <linux/usb.h>
523f1dce 27#include <sound/initval.h>
e431cf45 28#include <sound/core.h>
523f1dce 29#include <sound/pcm.h>
523f1dce 30
936e7d03
DM
31#include "device.h"
32#include "audio.h"
33#include "midi.h"
34#include "control.h"
35#include "input.h"
523f1dce
DM
36
37MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
563c2bf5 38MODULE_DESCRIPTION("caiaq USB audio, version 1.3.18");
523f1dce
DM
39MODULE_LICENSE("GPL");
40MODULE_SUPPORTED_DEVICE("{{Native Instruments, RigKontrol2},"
ad1e34b5 41 "{Native Instruments, RigKontrol3},"
523f1dce 42 "{Native Instruments, Kore Controller},"
7829d0ec 43 "{Native Instruments, Kore Controller 2},"
f3e9d5d1 44 "{Native Instruments, Audio Kontrol 1},"
2165592b 45 "{Native Instruments, Audio 4 DJ},"
f3e9d5d1 46 "{Native Instruments, Audio 8 DJ},"
2165592b
DM
47 "{Native Instruments, Session I/O},"
48 "{Native Instruments, GuitarRig mobile}");
523f1dce
DM
49
50static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
51static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
52static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
53static int snd_card_used[SNDRV_CARDS];
54
55module_param_array(index, int, NULL, 0444);
56MODULE_PARM_DESC(index, "Index value for the caiaq sound device");
57module_param_array(id, charp, NULL, 0444);
58MODULE_PARM_DESC(id, "ID string for the caiaq soundcard.");
59module_param_array(enable, bool, NULL, 0444);
60MODULE_PARM_DESC(enable, "Enable the caiaq soundcard.");
61
62enum {
63 SAMPLERATE_44100 = 0,
64 SAMPLERATE_48000 = 1,
65 SAMPLERATE_96000 = 2,
66 SAMPLERATE_192000 = 3,
67 SAMPLERATE_88200 = 4,
68 SAMPLERATE_INVALID = 0xff
69};
70
71enum {
72 DEPTH_NONE = 0,
73 DEPTH_16 = 1,
74 DEPTH_24 = 2,
75 DEPTH_32 = 3
76};
77
78static struct usb_device_id snd_usb_id_table[] = {
79 {
80 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
81 .idVendor = USB_VID_NATIVEINSTRUMENTS,
9318dce5 82 .idProduct = USB_PID_RIGKONTROL2
523f1dce 83 },
ad1e34b5
DM
84 {
85 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
86 .idVendor = USB_VID_NATIVEINSTRUMENTS,
87 .idProduct = USB_PID_RIGKONTROL3
88 },
523f1dce
DM
89 {
90 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
91 .idVendor = USB_VID_NATIVEINSTRUMENTS,
92 .idProduct = USB_PID_KORECONTROLLER
93 },
7829d0ec
DM
94 {
95 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
96 .idVendor = USB_VID_NATIVEINSTRUMENTS,
97 .idProduct = USB_PID_KORECONTROLLER2
98 },
523f1dce
DM
99 {
100 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
101 .idVendor = USB_VID_NATIVEINSTRUMENTS,
102 .idProduct = USB_PID_AK1
103 },
104 {
105 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
106 .idVendor = USB_VID_NATIVEINSTRUMENTS,
107 .idProduct = USB_PID_AUDIO8DJ
108 },
f3e9d5d1
DM
109 {
110 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
111 .idVendor = USB_VID_NATIVEINSTRUMENTS,
112 .idProduct = USB_PID_SESSIONIO
113 },
2165592b
DM
114 {
115 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
116 .idVendor = USB_VID_NATIVEINSTRUMENTS,
117 .idProduct = USB_PID_GUITARRIGMOBILE
118 },
119 {
120 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
121 .idVendor = USB_VID_NATIVEINSTRUMENTS,
122 .idProduct = USB_PID_AUDIO4DJ
123 },
523f1dce
DM
124 { /* terminator */ }
125};
126
127static void usb_ep1_command_reply_dispatch (struct urb* urb)
128{
129 int ret;
130 struct snd_usb_caiaqdev *dev = urb->context;
131 unsigned char *buf = urb->transfer_buffer;
132
133 if (urb->status || !dev) {
134 log("received EP1 urb->status = %i\n", urb->status);
135 return;
136 }
137
138 switch(buf[0]) {
139 case EP1_CMD_GET_DEVICE_INFO:
140 memcpy(&dev->spec, buf+1, sizeof(struct caiaq_device_spec));
141 dev->spec.fw_version = le16_to_cpu(dev->spec.fw_version);
142 debug("device spec (firmware %d): audio: %d in, %d out, "
143 "MIDI: %d in, %d out, data alignment %d\n",
144 dev->spec.fw_version,
145 dev->spec.num_analog_audio_in,
146 dev->spec.num_analog_audio_out,
147 dev->spec.num_midi_in,
148 dev->spec.num_midi_out,
149 dev->spec.data_alignment);
150
151 dev->spec_received++;
152 wake_up(&dev->ep1_wait_queue);
153 break;
154 case EP1_CMD_AUDIO_PARAMS:
155 dev->audio_parm_answer = buf[1];
156 wake_up(&dev->ep1_wait_queue);
157 break;
158 case EP1_CMD_MIDI_READ:
159 snd_usb_caiaq_midi_handle_input(dev, buf[1], buf + 3, buf[2]);
160 break;
8e3cd08e
DM
161 case EP1_CMD_READ_IO:
162 if (dev->chip.usb_id ==
163 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) {
164 if (urb->actual_length > sizeof(dev->control_state))
165 urb->actual_length = sizeof(dev->control_state);
166 memcpy(dev->control_state, buf + 1, urb->actual_length);
167 wake_up(&dev->ep1_wait_queue);
168 break;
169 }
523f1dce
DM
170#ifdef CONFIG_SND_USB_CAIAQ_INPUT
171 case EP1_CMD_READ_ERP:
172 case EP1_CMD_READ_ANALOG:
523f1dce 173 snd_usb_caiaq_input_dispatch(dev, buf, urb->actual_length);
523f1dce 174#endif
8e3cd08e 175 break;
523f1dce
DM
176 }
177
178 dev->ep1_in_urb.actual_length = 0;
179 ret = usb_submit_urb(&dev->ep1_in_urb, GFP_ATOMIC);
180 if (ret < 0)
181 log("unable to submit urb. OOM!?\n");
182}
183
8e3cd08e
DM
184int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *dev,
185 unsigned char command,
186 const unsigned char *buffer,
187 int len)
523f1dce
DM
188{
189 int actual_len;
190 struct usb_device *usb_dev = dev->chip.dev;
191
192 if (!usb_dev)
193 return -EIO;
194
195 if (len > EP1_BUFSIZE - 1)
196 len = EP1_BUFSIZE - 1;
197
198 if (buffer && len > 0)
199 memcpy(dev->ep1_out_buf+1, buffer, len);
9318dce5 200
523f1dce
DM
201 dev->ep1_out_buf[0] = command;
202 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
203 dev->ep1_out_buf, len+1, &actual_len, 200);
204}
205
206int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *dev,
207 int rate, int depth, int bpp)
208{
209 int ret;
210 char tmp[5];
9318dce5 211
523f1dce
DM
212 switch (rate) {
213 case 44100: tmp[0] = SAMPLERATE_44100; break;
214 case 48000: tmp[0] = SAMPLERATE_48000; break;
215 case 88200: tmp[0] = SAMPLERATE_88200; break;
216 case 96000: tmp[0] = SAMPLERATE_96000; break;
217 case 192000: tmp[0] = SAMPLERATE_192000; break;
218 default: return -EINVAL;
219 }
220
221 switch (depth) {
222 case 16: tmp[1] = DEPTH_16; break;
223 case 24: tmp[1] = DEPTH_24; break;
224 default: return -EINVAL;
225 }
226
227 tmp[2] = bpp & 0xff;
228 tmp[3] = bpp >> 8;
229 tmp[4] = 1; /* packets per microframe */
230
231 debug("setting audio params: %d Hz, %d bits, %d bpp\n",
232 rate, depth, bpp);
233
234 dev->audio_parm_answer = -1;
8e3cd08e
DM
235 ret = snd_usb_caiaq_send_command(dev, EP1_CMD_AUDIO_PARAMS,
236 tmp, sizeof(tmp));
523f1dce
DM
237
238 if (ret)
239 return ret;
9318dce5
DM
240
241 if (!wait_event_timeout(dev->ep1_wait_queue,
523f1dce
DM
242 dev->audio_parm_answer >= 0, HZ))
243 return -EPIPE;
9318dce5
DM
244
245 if (dev->audio_parm_answer != 1)
523f1dce 246 debug("unable to set the device's audio params\n");
9311c9b4
DM
247 else
248 dev->bpp = bpp;
523f1dce
DM
249
250 return dev->audio_parm_answer == 1 ? 0 : -EINVAL;
251}
252
9318dce5
DM
253int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev *dev,
254 int digital, int analog, int erp)
523f1dce
DM
255{
256 char tmp[3] = { digital, analog, erp };
8e3cd08e
DM
257 return snd_usb_caiaq_send_command(dev, EP1_CMD_AUTO_MSG,
258 tmp, sizeof(tmp));
523f1dce
DM
259}
260
c598195a 261static void __devinit setup_card(struct snd_usb_caiaqdev *dev)
523f1dce
DM
262{
263 int ret;
ad1e34b5 264 char val[4];
9318dce5 265
523f1dce
DM
266 /* device-specific startup specials */
267 switch (dev->chip.usb_id) {
268 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
269 /* RigKontrol2 - display centered dash ('-') */
270 val[0] = 0x00;
271 val[1] = 0x00;
272 val[2] = 0x01;
8e3cd08e 273 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 3);
523f1dce 274 break;
ad1e34b5
DM
275 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
276 /* RigKontrol2 - display two centered dashes ('--') */
277 val[0] = 0x00;
278 val[1] = 0x40;
279 val[2] = 0x40;
280 val[3] = 0x00;
8e3cd08e 281 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 4);
ad1e34b5 282 break;
523f1dce
DM
283 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
284 /* Audio Kontrol 1 - make USB-LED stop blinking */
285 val[0] = 0x00;
8e3cd08e
DM
286 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 1);
287 break;
288 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
289 /* Audio 8 DJ - trigger read of current settings */
290 dev->control_state[0] = 0xff;
291 snd_usb_caiaq_set_auto_msg(dev, 1, 0, 0);
292 snd_usb_caiaq_send_command(dev, EP1_CMD_READ_IO, NULL, 0);
293
294 if (!wait_event_timeout(dev->ep1_wait_queue,
295 dev->control_state[0] != 0xff, HZ))
296 return;
297
298 /* fix up some defaults */
299 if ((dev->control_state[1] != 2) ||
300 (dev->control_state[2] != 3) ||
301 (dev->control_state[4] != 2)) {
302 dev->control_state[1] = 2;
303 dev->control_state[2] = 3;
304 dev->control_state[4] = 2;
305 snd_usb_caiaq_send_command(dev,
306 EP1_CMD_WRITE_IO, dev->control_state, 6);
307 }
308
523f1dce 309 break;
e3ca4c99
MH
310 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ):
311 /* Audio 4 DJ - default input mode to phono */
312 dev->control_state[0] = 2;
313 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO,
314 dev->control_state, 1);
315 break;
523f1dce 316 }
9318dce5 317
7829d0ec
DM
318 if (dev->spec.num_analog_audio_out +
319 dev->spec.num_analog_audio_in +
320 dev->spec.num_digital_audio_out +
321 dev->spec.num_digital_audio_in > 0) {
322 ret = snd_usb_caiaq_audio_init(dev);
323 if (ret < 0)
324 log("Unable to set up audio system (ret=%d)\n", ret);
325 }
9318dce5 326
7829d0ec
DM
327 if (dev->spec.num_midi_in +
328 dev->spec.num_midi_out > 0) {
329 ret = snd_usb_caiaq_midi_init(dev);
330 if (ret < 0)
331 log("Unable to set up MIDI system (ret=%d)\n", ret);
332 }
523f1dce
DM
333
334#ifdef CONFIG_SND_USB_CAIAQ_INPUT
335 ret = snd_usb_caiaq_input_init(dev);
336 if (ret < 0)
337 log("Unable to set up input system (ret=%d)\n", ret);
338#endif
339
340 /* finally, register the card and all its sub-instances */
341 ret = snd_card_register(dev->chip.card);
342 if (ret < 0) {
343 log("snd_card_register() returned %d\n", ret);
344 snd_card_free(dev->chip.card);
345 }
8e3cd08e
DM
346
347 ret = snd_usb_caiaq_control_init(dev);
348 if (ret < 0)
349 log("Unable to set up control system (ret=%d)\n", ret);
523f1dce
DM
350}
351
563c2bf5
DM
352static int create_card(struct usb_device *usb_dev,
353 struct usb_interface *intf,
354 struct snd_card **cardp)
523f1dce
DM
355{
356 int devnum;
bd7dd77c 357 int err;
523f1dce
DM
358 struct snd_card *card;
359 struct snd_usb_caiaqdev *dev;
360
361 for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
362 if (enable[devnum] && !snd_card_used[devnum])
363 break;
364
365 if (devnum >= SNDRV_CARDS)
51721f70 366 return -ENODEV;
523f1dce 367
9318dce5 368 err = snd_card_create(index[devnum], id[devnum], THIS_MODULE,
bd7dd77c
TI
369 sizeof(struct snd_usb_caiaqdev), &card);
370 if (err < 0)
51721f70 371 return err;
523f1dce
DM
372
373 dev = caiaqdev(card);
374 dev->chip.dev = usb_dev;
375 dev->chip.card = card;
8c5330a5
AV
376 dev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor),
377 le16_to_cpu(usb_dev->descriptor.idProduct));
523f1dce 378 spin_lock_init(&dev->spinlock);
563c2bf5 379 snd_card_set_dev(card, &intf->dev);
523f1dce 380
51721f70
TI
381 *cardp = card;
382 return 0;
523f1dce
DM
383}
384
c598195a 385static int __devinit init_card(struct snd_usb_caiaqdev *dev)
523f1dce 386{
bafeee5b 387 char *c, usbpath[32];
523f1dce
DM
388 struct usb_device *usb_dev = dev->chip.dev;
389 struct snd_card *card = dev->chip.card;
bafeee5b 390 int err, len;
9318dce5 391
523f1dce
DM
392 if (usb_set_interface(usb_dev, 0, 1) != 0) {
393 log("can't set alt interface.\n");
394 return -EIO;
395 }
396
397 usb_init_urb(&dev->ep1_in_urb);
398 usb_init_urb(&dev->midi_out_urb);
399
9318dce5 400 usb_fill_bulk_urb(&dev->ep1_in_urb, usb_dev,
523f1dce 401 usb_rcvbulkpipe(usb_dev, 0x1),
9318dce5 402 dev->ep1_in_buf, EP1_BUFSIZE,
523f1dce
DM
403 usb_ep1_command_reply_dispatch, dev);
404
9318dce5 405 usb_fill_bulk_urb(&dev->midi_out_urb, usb_dev,
523f1dce 406 usb_sndbulkpipe(usb_dev, 0x1),
9318dce5 407 dev->midi_out_buf, EP1_BUFSIZE,
523f1dce 408 snd_usb_caiaq_midi_output_done, dev);
9318dce5 409
523f1dce
DM
410 init_waitqueue_head(&dev->ep1_wait_queue);
411 init_waitqueue_head(&dev->prepare_wait_queue);
9318dce5 412
523f1dce
DM
413 if (usb_submit_urb(&dev->ep1_in_urb, GFP_KERNEL) != 0)
414 return -EIO;
415
8e3cd08e 416 err = snd_usb_caiaq_send_command(dev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
523f1dce
DM
417 if (err)
418 return err;
419
420 if (!wait_event_timeout(dev->ep1_wait_queue, dev->spec_received, HZ))
421 return -ENODEV;
422
423 usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
424 dev->vendor_name, CAIAQ_USB_STR_LEN);
9318dce5 425
523f1dce
DM
426 usb_string(usb_dev, usb_dev->descriptor.iProduct,
427 dev->product_name, CAIAQ_USB_STR_LEN);
9318dce5 428
d3873a1b
DM
429 strlcpy(card->driver, MODNAME, sizeof(card->driver));
430 strlcpy(card->shortname, dev->product_name, sizeof(card->shortname));
955f2d96 431 strlcpy(card->mixername, dev->product_name, sizeof(card->mixername));
523f1dce 432
bafeee5b
DM
433 /* if the id was not passed as module option, fill it with a shortened
434 * version of the product string which does not contain any
435 * whitespaces */
436
437 if (*card->id == '\0') {
438 char id[sizeof(card->id)];
439
440 memset(id, 0, sizeof(id));
441
442 for (c = card->shortname, len = 0;
443 *c && len < sizeof(card->id); c++)
444 if (*c != ' ')
445 id[len++] = *c;
446
447 snd_card_set_id(card, id);
448 }
449
1a1df6f0
DM
450 usb_make_path(usb_dev, usbpath, sizeof(usbpath));
451 snprintf(card->longname, sizeof(card->longname),
452 "%s %s (%s)",
453 dev->vendor_name, dev->product_name, usbpath);
523f1dce 454
523f1dce
DM
455 setup_card(dev);
456 return 0;
457}
458
9318dce5 459static int __devinit snd_probe(struct usb_interface *intf,
523f1dce
DM
460 const struct usb_device_id *id)
461{
462 int ret;
463 struct snd_card *card;
464 struct usb_device *device = interface_to_usbdev(intf);
9318dce5 465
563c2bf5 466 ret = create_card(device, intf, &card);
9318dce5 467
51721f70
TI
468 if (ret < 0)
469 return ret;
9318dce5 470
f4e9749f 471 usb_set_intfdata(intf, card);
523f1dce
DM
472 ret = init_card(caiaqdev(card));
473 if (ret < 0) {
474 log("unable to init card! (ret=%d)\n", ret);
475 snd_card_free(card);
476 return ret;
477 }
9318dce5 478
523f1dce
DM
479 return 0;
480}
481
482static void snd_disconnect(struct usb_interface *intf)
483{
484 struct snd_usb_caiaqdev *dev;
f4e9749f 485 struct snd_card *card = usb_get_intfdata(intf);
523f1dce 486
8d048841 487 debug("%s(%p)\n", __func__, intf);
523f1dce
DM
488
489 if (!card)
490 return;
491
492 dev = caiaqdev(card);
493 snd_card_disconnect(card);
494
495#ifdef CONFIG_SND_USB_CAIAQ_INPUT
496 snd_usb_caiaq_input_free(dev);
497#endif
498 snd_usb_caiaq_audio_free(dev);
9318dce5 499
523f1dce
DM
500 usb_kill_urb(&dev->ep1_in_urb);
501 usb_kill_urb(&dev->midi_out_urb);
9318dce5 502
523f1dce
DM
503 snd_card_free(card);
504 usb_reset_device(interface_to_usbdev(intf));
505}
506
507
508MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
509static struct usb_driver snd_usb_driver = {
510 .name = MODNAME,
511 .probe = snd_probe,
512 .disconnect = snd_disconnect,
513 .id_table = snd_usb_id_table,
514};
515
516static int __init snd_module_init(void)
517{
518 return usb_register(&snd_usb_driver);
519}
520
521static void __exit snd_module_exit(void)
522{
523 usb_deregister(&snd_usb_driver);
524}
525
526module_init(snd_module_init)
527module_exit(snd_module_exit)
528