]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/usb/caiaq/device.c
ALSA: snd-usb-caiaq: fix reported elapsed periods
[net-next-2.6.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>");
a9b487fa 38MODULE_DESCRIPTION("caiaq USB audio, version 1.3.14");
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,
82 .idProduct = USB_PID_RIGKONTROL2
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);
200
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];
211
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;
240
241 if (!wait_event_timeout(dev->ep1_wait_queue,
242 dev->audio_parm_answer >= 0, HZ))
243 return -EPIPE;
244
245 if (dev->audio_parm_answer != 1)
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
253int snd_usb_caiaq_set_auto_msg (struct snd_usb_caiaqdev *dev,
254 int digital, int analog, int erp)
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];
523f1dce
DM
265
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
DM
316 }
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 }
523f1dce 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
51721f70 352static int create_card(struct usb_device* usb_dev, struct snd_card **cardp)
523f1dce
DM
353{
354 int devnum;
bd7dd77c 355 int err;
523f1dce
DM
356 struct snd_card *card;
357 struct snd_usb_caiaqdev *dev;
358
359 for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
360 if (enable[devnum] && !snd_card_used[devnum])
361 break;
362
363 if (devnum >= SNDRV_CARDS)
51721f70 364 return -ENODEV;
523f1dce 365
bd7dd77c
TI
366 err = snd_card_create(index[devnum], id[devnum], THIS_MODULE,
367 sizeof(struct snd_usb_caiaqdev), &card);
368 if (err < 0)
51721f70 369 return err;
523f1dce
DM
370
371 dev = caiaqdev(card);
372 dev->chip.dev = usb_dev;
373 dev->chip.card = card;
8c5330a5
AV
374 dev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor),
375 le16_to_cpu(usb_dev->descriptor.idProduct));
523f1dce
DM
376 spin_lock_init(&dev->spinlock);
377 snd_card_set_dev(card, &usb_dev->dev);
378
51721f70
TI
379 *cardp = card;
380 return 0;
523f1dce
DM
381}
382
c598195a 383static int __devinit init_card(struct snd_usb_caiaqdev *dev)
523f1dce
DM
384{
385 char *c;
386 struct usb_device *usb_dev = dev->chip.dev;
387 struct snd_card *card = dev->chip.card;
388 int err, len;
389
390 if (usb_set_interface(usb_dev, 0, 1) != 0) {
391 log("can't set alt interface.\n");
392 return -EIO;
393 }
394
395 usb_init_urb(&dev->ep1_in_urb);
396 usb_init_urb(&dev->midi_out_urb);
397
398 usb_fill_bulk_urb(&dev->ep1_in_urb, usb_dev,
399 usb_rcvbulkpipe(usb_dev, 0x1),
400 dev->ep1_in_buf, EP1_BUFSIZE,
401 usb_ep1_command_reply_dispatch, dev);
402
403 usb_fill_bulk_urb(&dev->midi_out_urb, usb_dev,
404 usb_sndbulkpipe(usb_dev, 0x1),
405 dev->midi_out_buf, EP1_BUFSIZE,
406 snd_usb_caiaq_midi_output_done, dev);
407
408 init_waitqueue_head(&dev->ep1_wait_queue);
409 init_waitqueue_head(&dev->prepare_wait_queue);
410
411 if (usb_submit_urb(&dev->ep1_in_urb, GFP_KERNEL) != 0)
412 return -EIO;
413
8e3cd08e 414 err = snd_usb_caiaq_send_command(dev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
523f1dce
DM
415 if (err)
416 return err;
417
418 if (!wait_event_timeout(dev->ep1_wait_queue, dev->spec_received, HZ))
419 return -ENODEV;
420
421 usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
422 dev->vendor_name, CAIAQ_USB_STR_LEN);
423
424 usb_string(usb_dev, usb_dev->descriptor.iProduct,
425 dev->product_name, CAIAQ_USB_STR_LEN);
426
427 usb_string(usb_dev, usb_dev->descriptor.iSerialNumber,
428 dev->serial, CAIAQ_USB_STR_LEN);
429
430 /* terminate serial string at first white space occurence */
431 c = strchr(dev->serial, ' ');
432 if (c)
433 *c = '\0';
434
435 strcpy(card->driver, MODNAME);
436 strcpy(card->shortname, dev->product_name);
437
438 len = snprintf(card->longname, sizeof(card->longname),
439 "%s %s (serial %s, ",
440 dev->vendor_name, dev->product_name, dev->serial);
441
442 if (len < sizeof(card->longname) - 2)
443 len += usb_make_path(usb_dev, card->longname + len,
444 sizeof(card->longname) - len);
445
446 card->longname[len++] = ')';
447 card->longname[len] = '\0';
448 setup_card(dev);
449 return 0;
450}
451
c598195a 452static int __devinit snd_probe(struct usb_interface *intf,
523f1dce
DM
453 const struct usb_device_id *id)
454{
455 int ret;
456 struct snd_card *card;
457 struct usb_device *device = interface_to_usbdev(intf);
458
51721f70 459 ret = create_card(device, &card);
523f1dce 460
51721f70
TI
461 if (ret < 0)
462 return ret;
523f1dce 463
f4e9749f 464 usb_set_intfdata(intf, card);
523f1dce
DM
465 ret = init_card(caiaqdev(card));
466 if (ret < 0) {
467 log("unable to init card! (ret=%d)\n", ret);
468 snd_card_free(card);
469 return ret;
470 }
471
472 return 0;
473}
474
475static void snd_disconnect(struct usb_interface *intf)
476{
477 struct snd_usb_caiaqdev *dev;
f4e9749f 478 struct snd_card *card = usb_get_intfdata(intf);
523f1dce 479
8d048841 480 debug("%s(%p)\n", __func__, intf);
523f1dce
DM
481
482 if (!card)
483 return;
484
485 dev = caiaqdev(card);
486 snd_card_disconnect(card);
487
488#ifdef CONFIG_SND_USB_CAIAQ_INPUT
489 snd_usb_caiaq_input_free(dev);
490#endif
491 snd_usb_caiaq_audio_free(dev);
492
493 usb_kill_urb(&dev->ep1_in_urb);
494 usb_kill_urb(&dev->midi_out_urb);
495
496 snd_card_free(card);
497 usb_reset_device(interface_to_usbdev(intf));
498}
499
500
501MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
502static struct usb_driver snd_usb_driver = {
503 .name = MODNAME,
504 .probe = snd_probe,
505 .disconnect = snd_disconnect,
506 .id_table = snd_usb_id_table,
507};
508
509static int __init snd_module_init(void)
510{
511 return usb_register(&snd_usb_driver);
512}
513
514static void __exit snd_module_exit(void)
515{
516 usb_deregister(&snd_usb_driver);
517}
518
519module_init(snd_module_init)
520module_exit(snd_module_exit)
521