Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / sound / usb / caiaq / caiaq-device.c
blob4b7f1254f6eea054e5fa69ddf1f15c4d2fcc3ae3
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/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"
42 #endif
44 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
45 MODULE_DESCRIPTION("caiaq USB audio, version 1.3.2");
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}}");
54 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
55 static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
56 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
57 static int snd_card_used[SNDRV_CARDS];
59 module_param_array(index, int, NULL, 0444);
60 MODULE_PARM_DESC(index, "Index value for the caiaq sound device");
61 module_param_array(id, charp, NULL, 0444);
62 MODULE_PARM_DESC(id, "ID string for the caiaq soundcard.");
63 module_param_array(enable, bool, NULL, 0444);
64 MODULE_PARM_DESC(enable, "Enable the caiaq soundcard.");
66 enum {
67 SAMPLERATE_44100 = 0,
68 SAMPLERATE_48000 = 1,
69 SAMPLERATE_96000 = 2,
70 SAMPLERATE_192000 = 3,
71 SAMPLERATE_88200 = 4,
72 SAMPLERATE_INVALID = 0xff
75 enum {
76 DEPTH_NONE = 0,
77 DEPTH_16 = 1,
78 DEPTH_24 = 2,
79 DEPTH_32 = 3
82 static struct usb_device_id snd_usb_id_table[] = {
84 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
85 .idVendor = USB_VID_NATIVEINSTRUMENTS,
86 .idProduct = USB_PID_RIGKONTROL2
89 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
90 .idVendor = USB_VID_NATIVEINSTRUMENTS,
91 .idProduct = USB_PID_RIGKONTROL3
94 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
95 .idVendor = USB_VID_NATIVEINSTRUMENTS,
96 .idProduct = USB_PID_KORECONTROLLER
99 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
100 .idVendor = USB_VID_NATIVEINSTRUMENTS,
101 .idProduct = USB_PID_KORECONTROLLER2
104 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
105 .idVendor = USB_VID_NATIVEINSTRUMENTS,
106 .idProduct = USB_PID_AK1
109 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
110 .idVendor = USB_VID_NATIVEINSTRUMENTS,
111 .idProduct = USB_PID_AUDIO8DJ
113 { /* terminator */ }
116 static void usb_ep1_command_reply_dispatch (struct urb* urb)
118 int ret;
119 struct snd_usb_caiaqdev *dev = urb->context;
120 unsigned char *buf = urb->transfer_buffer;
122 if (urb->status || !dev) {
123 log("received EP1 urb->status = %i\n", urb->status);
124 return;
127 switch(buf[0]) {
128 case EP1_CMD_GET_DEVICE_INFO:
129 memcpy(&dev->spec, buf+1, sizeof(struct caiaq_device_spec));
130 dev->spec.fw_version = le16_to_cpu(dev->spec.fw_version);
131 debug("device spec (firmware %d): audio: %d in, %d out, "
132 "MIDI: %d in, %d out, data alignment %d\n",
133 dev->spec.fw_version,
134 dev->spec.num_analog_audio_in,
135 dev->spec.num_analog_audio_out,
136 dev->spec.num_midi_in,
137 dev->spec.num_midi_out,
138 dev->spec.data_alignment);
140 dev->spec_received++;
141 wake_up(&dev->ep1_wait_queue);
142 break;
143 case EP1_CMD_AUDIO_PARAMS:
144 dev->audio_parm_answer = buf[1];
145 wake_up(&dev->ep1_wait_queue);
146 break;
147 case EP1_CMD_MIDI_READ:
148 snd_usb_caiaq_midi_handle_input(dev, buf[1], buf + 3, buf[2]);
149 break;
150 case EP1_CMD_READ_IO:
151 if (dev->chip.usb_id ==
152 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) {
153 if (urb->actual_length > sizeof(dev->control_state))
154 urb->actual_length = sizeof(dev->control_state);
155 memcpy(dev->control_state, buf + 1, urb->actual_length);
156 wake_up(&dev->ep1_wait_queue);
157 break;
159 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
160 case EP1_CMD_READ_ERP:
161 case EP1_CMD_READ_ANALOG:
162 snd_usb_caiaq_input_dispatch(dev, buf, urb->actual_length);
163 #endif
164 break;
167 dev->ep1_in_urb.actual_length = 0;
168 ret = usb_submit_urb(&dev->ep1_in_urb, GFP_ATOMIC);
169 if (ret < 0)
170 log("unable to submit urb. OOM!?\n");
173 int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *dev,
174 unsigned char command,
175 const unsigned char *buffer,
176 int len)
178 int actual_len;
179 struct usb_device *usb_dev = dev->chip.dev;
181 if (!usb_dev)
182 return -EIO;
184 if (len > EP1_BUFSIZE - 1)
185 len = EP1_BUFSIZE - 1;
187 if (buffer && len > 0)
188 memcpy(dev->ep1_out_buf+1, buffer, len);
190 dev->ep1_out_buf[0] = command;
191 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
192 dev->ep1_out_buf, len+1, &actual_len, 200);
195 int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *dev,
196 int rate, int depth, int bpp)
198 int ret;
199 char tmp[5];
201 switch (rate) {
202 case 44100: tmp[0] = SAMPLERATE_44100; break;
203 case 48000: tmp[0] = SAMPLERATE_48000; break;
204 case 88200: tmp[0] = SAMPLERATE_88200; break;
205 case 96000: tmp[0] = SAMPLERATE_96000; break;
206 case 192000: tmp[0] = SAMPLERATE_192000; break;
207 default: return -EINVAL;
210 switch (depth) {
211 case 16: tmp[1] = DEPTH_16; break;
212 case 24: tmp[1] = DEPTH_24; break;
213 default: return -EINVAL;
216 tmp[2] = bpp & 0xff;
217 tmp[3] = bpp >> 8;
218 tmp[4] = 1; /* packets per microframe */
220 debug("setting audio params: %d Hz, %d bits, %d bpp\n",
221 rate, depth, bpp);
223 dev->audio_parm_answer = -1;
224 ret = snd_usb_caiaq_send_command(dev, EP1_CMD_AUDIO_PARAMS,
225 tmp, sizeof(tmp));
227 if (ret)
228 return ret;
230 if (!wait_event_timeout(dev->ep1_wait_queue,
231 dev->audio_parm_answer >= 0, HZ))
232 return -EPIPE;
234 if (dev->audio_parm_answer != 1)
235 debug("unable to set the device's audio params\n");
237 return dev->audio_parm_answer == 1 ? 0 : -EINVAL;
240 int snd_usb_caiaq_set_auto_msg (struct snd_usb_caiaqdev *dev,
241 int digital, int analog, int erp)
243 char tmp[3] = { digital, analog, erp };
244 return snd_usb_caiaq_send_command(dev, EP1_CMD_AUTO_MSG,
245 tmp, sizeof(tmp));
248 <<<<<<< HEAD:sound/usb/caiaq/caiaq-device.c
249 static void setup_card(struct snd_usb_caiaqdev *dev)
250 =======
251 static void __devinit setup_card(struct snd_usb_caiaqdev *dev)
252 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:sound/usb/caiaq/caiaq-device.c
254 int ret;
255 char val[4];
257 /* device-specific startup specials */
258 switch (dev->chip.usb_id) {
259 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
260 /* RigKontrol2 - display centered dash ('-') */
261 val[0] = 0x00;
262 val[1] = 0x00;
263 val[2] = 0x01;
264 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 3);
265 break;
266 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
267 /* RigKontrol2 - display two centered dashes ('--') */
268 val[0] = 0x00;
269 val[1] = 0x40;
270 val[2] = 0x40;
271 val[3] = 0x00;
272 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 4);
273 break;
274 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
275 /* Audio Kontrol 1 - make USB-LED stop blinking */
276 val[0] = 0x00;
277 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 1);
278 break;
279 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
280 /* Audio 8 DJ - trigger read of current settings */
281 dev->control_state[0] = 0xff;
282 snd_usb_caiaq_set_auto_msg(dev, 1, 0, 0);
283 snd_usb_caiaq_send_command(dev, EP1_CMD_READ_IO, NULL, 0);
285 if (!wait_event_timeout(dev->ep1_wait_queue,
286 dev->control_state[0] != 0xff, HZ))
287 return;
289 /* fix up some defaults */
290 if ((dev->control_state[1] != 2) ||
291 (dev->control_state[2] != 3) ||
292 (dev->control_state[4] != 2)) {
293 dev->control_state[1] = 2;
294 dev->control_state[2] = 3;
295 dev->control_state[4] = 2;
296 snd_usb_caiaq_send_command(dev,
297 EP1_CMD_WRITE_IO, dev->control_state, 6);
300 break;
303 if (dev->spec.num_analog_audio_out +
304 dev->spec.num_analog_audio_in +
305 dev->spec.num_digital_audio_out +
306 dev->spec.num_digital_audio_in > 0) {
307 ret = snd_usb_caiaq_audio_init(dev);
308 if (ret < 0)
309 log("Unable to set up audio system (ret=%d)\n", ret);
312 if (dev->spec.num_midi_in +
313 dev->spec.num_midi_out > 0) {
314 ret = snd_usb_caiaq_midi_init(dev);
315 if (ret < 0)
316 log("Unable to set up MIDI system (ret=%d)\n", ret);
319 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
320 ret = snd_usb_caiaq_input_init(dev);
321 if (ret < 0)
322 log("Unable to set up input system (ret=%d)\n", ret);
323 #endif
325 /* finally, register the card and all its sub-instances */
326 ret = snd_card_register(dev->chip.card);
327 if (ret < 0) {
328 log("snd_card_register() returned %d\n", ret);
329 snd_card_free(dev->chip.card);
332 ret = snd_usb_caiaq_control_init(dev);
333 if (ret < 0)
334 log("Unable to set up control system (ret=%d)\n", ret);
337 static struct snd_card* create_card(struct usb_device* usb_dev)
339 int devnum;
340 struct snd_card *card;
341 struct snd_usb_caiaqdev *dev;
343 for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
344 if (enable[devnum] && !snd_card_used[devnum])
345 break;
347 if (devnum >= SNDRV_CARDS)
348 return NULL;
350 card = snd_card_new(index[devnum], id[devnum], THIS_MODULE,
351 sizeof(struct snd_usb_caiaqdev));
352 if (!card)
353 return NULL;
355 dev = caiaqdev(card);
356 dev->chip.dev = usb_dev;
357 dev->chip.card = card;
358 dev->chip.usb_id = USB_ID(usb_dev->descriptor.idVendor,
359 usb_dev->descriptor.idProduct);
360 spin_lock_init(&dev->spinlock);
361 snd_card_set_dev(card, &usb_dev->dev);
363 return card;
366 <<<<<<< HEAD:sound/usb/caiaq/caiaq-device.c
367 static int init_card(struct snd_usb_caiaqdev *dev)
368 =======
369 static int __devinit init_card(struct snd_usb_caiaqdev *dev)
370 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:sound/usb/caiaq/caiaq-device.c
372 char *c;
373 struct usb_device *usb_dev = dev->chip.dev;
374 struct snd_card *card = dev->chip.card;
375 int err, len;
377 if (usb_set_interface(usb_dev, 0, 1) != 0) {
378 log("can't set alt interface.\n");
379 return -EIO;
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)
399 return -EIO;
401 err = snd_usb_caiaq_send_command(dev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
402 if (err)
403 return err;
405 if (!wait_event_timeout(dev->ep1_wait_queue, dev->spec_received, HZ))
406 return -ENODEV;
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, ' ');
419 if (c)
420 *c = '\0';
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';
435 setup_card(dev);
436 return 0;
439 <<<<<<< HEAD:sound/usb/caiaq/caiaq-device.c
440 static int snd_probe(struct usb_interface *intf,
441 =======
442 static int __devinit snd_probe(struct usb_interface *intf,
443 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:sound/usb/caiaq/caiaq-device.c
444 const struct usb_device_id *id)
446 int ret;
447 struct snd_card *card;
448 struct usb_device *device = interface_to_usbdev(intf);
450 card = create_card(device);
452 if (!card)
453 return -ENOMEM;
455 dev_set_drvdata(&intf->dev, card);
456 ret = init_card(caiaqdev(card));
457 if (ret < 0) {
458 log("unable to init card! (ret=%d)\n", ret);
459 snd_card_free(card);
460 return ret;
463 return 0;
466 static void snd_disconnect(struct usb_interface *intf)
468 struct snd_usb_caiaqdev *dev;
469 struct snd_card *card = dev_get_drvdata(&intf->dev);
471 debug("snd_disconnect(%p)\n", intf);
473 if (!card)
474 return;
476 dev = caiaqdev(card);
477 snd_card_disconnect(card);
479 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
480 snd_usb_caiaq_input_free(dev);
481 #endif
482 snd_usb_caiaq_audio_free(dev);
484 usb_kill_urb(&dev->ep1_in_urb);
485 usb_kill_urb(&dev->midi_out_urb);
487 snd_card_free(card);
488 usb_reset_device(interface_to_usbdev(intf));
492 MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
493 static struct usb_driver snd_usb_driver = {
494 .name = MODNAME,
495 .probe = snd_probe,
496 .disconnect = snd_disconnect,
497 .id_table = snd_usb_id_table,
500 static int __init snd_module_init(void)
502 return usb_register(&snd_usb_driver);
505 static void __exit snd_module_exit(void)
507 usb_deregister(&snd_usb_driver);
510 module_init(snd_module_init)
511 module_exit(snd_module_exit)