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/device.h>
24 #include <linux/interrupt.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/gfp.h>
28 #include <linux/usb.h>
29 #include <sound/initval.h>
30 #include <sound/core.h>
31 #include <sound/pcm.h>
39 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
40 MODULE_DESCRIPTION("caiaq USB audio");
41 MODULE_LICENSE("GPL");
42 MODULE_SUPPORTED_DEVICE("{{Native Instruments,RigKontrol2},"
43 "{Native Instruments,RigKontrol3},"
44 "{Native Instruments,Kore Controller},"
45 "{Native Instruments,Kore Controller 2},"
46 "{Native Instruments,Audio Kontrol 1},"
47 "{Native Instruments,Audio 2 DJ},"
48 "{Native Instruments,Audio 4 DJ},"
49 "{Native Instruments,Audio 8 DJ},"
50 "{Native Instruments,Traktor Audio 2},"
51 "{Native Instruments,Session I/O},"
52 "{Native Instruments,GuitarRig mobile},"
53 "{Native Instruments,Traktor Kontrol X1},"
54 "{Native Instruments,Traktor Kontrol S4},"
55 "{Native Instruments,Maschine Controller}}");
57 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
; /* Index 0-max */
58 static char* id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
; /* Id for this card */
59 static bool enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE_PNP
; /* Enable this card */
61 module_param_array(index
, int, NULL
, 0444);
62 MODULE_PARM_DESC(index
, "Index value for the caiaq sound device");
63 module_param_array(id
, charp
, NULL
, 0444);
64 MODULE_PARM_DESC(id
, "ID string for the caiaq soundcard.");
65 module_param_array(enable
, bool, NULL
, 0444);
66 MODULE_PARM_DESC(enable
, "Enable the caiaq soundcard.");
72 SAMPLERATE_192000
= 3,
74 SAMPLERATE_INVALID
= 0xff
84 static struct usb_device_id snd_usb_id_table
[] = {
86 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
87 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
88 .idProduct
= USB_PID_RIGKONTROL2
91 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
92 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
93 .idProduct
= USB_PID_RIGKONTROL3
96 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
97 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
98 .idProduct
= USB_PID_KORECONTROLLER
101 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
102 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
103 .idProduct
= USB_PID_KORECONTROLLER2
106 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
107 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
108 .idProduct
= USB_PID_AK1
111 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
112 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
113 .idProduct
= USB_PID_AUDIO8DJ
116 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
117 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
118 .idProduct
= USB_PID_SESSIONIO
121 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
122 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
123 .idProduct
= USB_PID_GUITARRIGMOBILE
126 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
127 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
128 .idProduct
= USB_PID_AUDIO4DJ
131 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
132 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
133 .idProduct
= USB_PID_AUDIO2DJ
136 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
137 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
138 .idProduct
= USB_PID_TRAKTORKONTROLX1
141 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
142 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
143 .idProduct
= USB_PID_TRAKTORKONTROLS4
146 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
147 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
148 .idProduct
= USB_PID_TRAKTORAUDIO2
151 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
152 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
153 .idProduct
= USB_PID_MASCHINECONTROLLER
158 static void usb_ep1_command_reply_dispatch (struct urb
* urb
)
161 struct device
*dev
= &urb
->dev
->dev
;
162 struct snd_usb_caiaqdev
*cdev
= urb
->context
;
163 unsigned char *buf
= urb
->transfer_buffer
;
165 if (urb
->status
|| !cdev
) {
166 dev_warn(dev
, "received EP1 urb->status = %i\n", urb
->status
);
171 case EP1_CMD_GET_DEVICE_INFO
:
172 memcpy(&cdev
->spec
, buf
+1, sizeof(struct caiaq_device_spec
));
173 cdev
->spec
.fw_version
= le16_to_cpu(cdev
->spec
.fw_version
);
174 dev_dbg(dev
, "device spec (firmware %d): audio: %d in, %d out, "
175 "MIDI: %d in, %d out, data alignment %d\n",
176 cdev
->spec
.fw_version
,
177 cdev
->spec
.num_analog_audio_in
,
178 cdev
->spec
.num_analog_audio_out
,
179 cdev
->spec
.num_midi_in
,
180 cdev
->spec
.num_midi_out
,
181 cdev
->spec
.data_alignment
);
183 cdev
->spec_received
++;
184 wake_up(&cdev
->ep1_wait_queue
);
186 case EP1_CMD_AUDIO_PARAMS
:
187 cdev
->audio_parm_answer
= buf
[1];
188 wake_up(&cdev
->ep1_wait_queue
);
190 case EP1_CMD_MIDI_READ
:
191 snd_usb_caiaq_midi_handle_input(cdev
, buf
[1], buf
+ 3, buf
[2]);
193 case EP1_CMD_READ_IO
:
194 if (cdev
->chip
.usb_id
==
195 USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AUDIO8DJ
)) {
196 if (urb
->actual_length
> sizeof(cdev
->control_state
))
197 urb
->actual_length
= sizeof(cdev
->control_state
);
198 memcpy(cdev
->control_state
, buf
+ 1, urb
->actual_length
);
199 wake_up(&cdev
->ep1_wait_queue
);
202 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
203 case EP1_CMD_READ_ERP
:
204 case EP1_CMD_READ_ANALOG
:
205 snd_usb_caiaq_input_dispatch(cdev
, buf
, urb
->actual_length
);
210 cdev
->ep1_in_urb
.actual_length
= 0;
211 ret
= usb_submit_urb(&cdev
->ep1_in_urb
, GFP_ATOMIC
);
213 dev_err(dev
, "unable to submit urb. OOM!?\n");
216 int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev
*cdev
,
217 unsigned char command
,
218 const unsigned char *buffer
,
222 struct usb_device
*usb_dev
= cdev
->chip
.dev
;
227 if (len
> EP1_BUFSIZE
- 1)
228 len
= EP1_BUFSIZE
- 1;
230 if (buffer
&& len
> 0)
231 memcpy(cdev
->ep1_out_buf
+1, buffer
, len
);
233 cdev
->ep1_out_buf
[0] = command
;
234 return usb_bulk_msg(usb_dev
, usb_sndbulkpipe(usb_dev
, 1),
235 cdev
->ep1_out_buf
, len
+1, &actual_len
, 200);
238 int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev
*cdev
,
239 int rate
, int depth
, int bpp
)
243 struct device
*dev
= caiaqdev_to_dev(cdev
);
246 case 44100: tmp
[0] = SAMPLERATE_44100
; break;
247 case 48000: tmp
[0] = SAMPLERATE_48000
; break;
248 case 88200: tmp
[0] = SAMPLERATE_88200
; break;
249 case 96000: tmp
[0] = SAMPLERATE_96000
; break;
250 case 192000: tmp
[0] = SAMPLERATE_192000
; break;
251 default: return -EINVAL
;
255 case 16: tmp
[1] = DEPTH_16
; break;
256 case 24: tmp
[1] = DEPTH_24
; break;
257 default: return -EINVAL
;
262 tmp
[4] = 1; /* packets per microframe */
264 dev_dbg(dev
, "setting audio params: %d Hz, %d bits, %d bpp\n",
267 cdev
->audio_parm_answer
= -1;
268 ret
= snd_usb_caiaq_send_command(cdev
, EP1_CMD_AUDIO_PARAMS
,
274 if (!wait_event_timeout(cdev
->ep1_wait_queue
,
275 cdev
->audio_parm_answer
>= 0, HZ
))
278 if (cdev
->audio_parm_answer
!= 1)
279 dev_dbg(dev
, "unable to set the device's audio params\n");
283 return cdev
->audio_parm_answer
== 1 ? 0 : -EINVAL
;
286 int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev
*cdev
,
287 int digital
, int analog
, int erp
)
289 char tmp
[3] = { digital
, analog
, erp
};
290 return snd_usb_caiaq_send_command(cdev
, EP1_CMD_AUTO_MSG
,
294 static void setup_card(struct snd_usb_caiaqdev
*cdev
)
298 struct device
*dev
= caiaqdev_to_dev(cdev
);
300 /* device-specific startup specials */
301 switch (cdev
->chip
.usb_id
) {
302 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_RIGKONTROL2
):
303 /* RigKontrol2 - display centered dash ('-') */
307 snd_usb_caiaq_send_command(cdev
, EP1_CMD_WRITE_IO
, val
, 3);
309 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_RIGKONTROL3
):
310 /* RigKontrol2 - display two centered dashes ('--') */
315 snd_usb_caiaq_send_command(cdev
, EP1_CMD_WRITE_IO
, val
, 4);
317 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AK1
):
318 /* Audio Kontrol 1 - make USB-LED stop blinking */
320 snd_usb_caiaq_send_command(cdev
, EP1_CMD_WRITE_IO
, val
, 1);
322 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AUDIO8DJ
):
323 /* Audio 8 DJ - trigger read of current settings */
324 cdev
->control_state
[0] = 0xff;
325 snd_usb_caiaq_set_auto_msg(cdev
, 1, 0, 0);
326 snd_usb_caiaq_send_command(cdev
, EP1_CMD_READ_IO
, NULL
, 0);
328 if (!wait_event_timeout(cdev
->ep1_wait_queue
,
329 cdev
->control_state
[0] != 0xff, HZ
))
332 /* fix up some defaults */
333 if ((cdev
->control_state
[1] != 2) ||
334 (cdev
->control_state
[2] != 3) ||
335 (cdev
->control_state
[4] != 2)) {
336 cdev
->control_state
[1] = 2;
337 cdev
->control_state
[2] = 3;
338 cdev
->control_state
[4] = 2;
339 snd_usb_caiaq_send_command(cdev
,
340 EP1_CMD_WRITE_IO
, cdev
->control_state
, 6);
346 if (cdev
->spec
.num_analog_audio_out
+
347 cdev
->spec
.num_analog_audio_in
+
348 cdev
->spec
.num_digital_audio_out
+
349 cdev
->spec
.num_digital_audio_in
> 0) {
350 ret
= snd_usb_caiaq_audio_init(cdev
);
352 dev_err(dev
, "Unable to set up audio system (ret=%d)\n", ret
);
355 if (cdev
->spec
.num_midi_in
+
356 cdev
->spec
.num_midi_out
> 0) {
357 ret
= snd_usb_caiaq_midi_init(cdev
);
359 dev_err(dev
, "Unable to set up MIDI system (ret=%d)\n", ret
);
362 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
363 ret
= snd_usb_caiaq_input_init(cdev
);
365 dev_err(dev
, "Unable to set up input system (ret=%d)\n", ret
);
368 /* finally, register the card and all its sub-instances */
369 ret
= snd_card_register(cdev
->chip
.card
);
371 dev_err(dev
, "snd_card_register() returned %d\n", ret
);
372 snd_card_free(cdev
->chip
.card
);
375 ret
= snd_usb_caiaq_control_init(cdev
);
377 dev_err(dev
, "Unable to set up control system (ret=%d)\n", ret
);
380 static int create_card(struct usb_device
*usb_dev
,
381 struct usb_interface
*intf
,
382 struct snd_card
**cardp
)
386 struct snd_card
*card
;
387 struct snd_usb_caiaqdev
*cdev
;
389 for (devnum
= 0; devnum
< SNDRV_CARDS
; devnum
++)
393 if (devnum
>= SNDRV_CARDS
)
396 err
= snd_card_create(index
[devnum
], id
[devnum
], THIS_MODULE
,
397 sizeof(struct snd_usb_caiaqdev
), &card
);
401 cdev
= caiaqdev(card
);
402 cdev
->chip
.dev
= usb_dev
;
403 cdev
->chip
.card
= card
;
404 cdev
->chip
.usb_id
= USB_ID(le16_to_cpu(usb_dev
->descriptor
.idVendor
),
405 le16_to_cpu(usb_dev
->descriptor
.idProduct
));
406 spin_lock_init(&cdev
->spinlock
);
407 snd_card_set_dev(card
, &intf
->dev
);
413 static int init_card(struct snd_usb_caiaqdev
*cdev
)
415 char *c
, usbpath
[32];
416 struct usb_device
*usb_dev
= cdev
->chip
.dev
;
417 struct snd_card
*card
= cdev
->chip
.card
;
418 struct device
*dev
= caiaqdev_to_dev(cdev
);
421 if (usb_set_interface(usb_dev
, 0, 1) != 0) {
422 dev_err(dev
, "can't set alt interface.\n");
426 usb_init_urb(&cdev
->ep1_in_urb
);
427 usb_init_urb(&cdev
->midi_out_urb
);
429 usb_fill_bulk_urb(&cdev
->ep1_in_urb
, usb_dev
,
430 usb_rcvbulkpipe(usb_dev
, 0x1),
431 cdev
->ep1_in_buf
, EP1_BUFSIZE
,
432 usb_ep1_command_reply_dispatch
, cdev
);
434 usb_fill_bulk_urb(&cdev
->midi_out_urb
, usb_dev
,
435 usb_sndbulkpipe(usb_dev
, 0x1),
436 cdev
->midi_out_buf
, EP1_BUFSIZE
,
437 snd_usb_caiaq_midi_output_done
, cdev
);
439 init_waitqueue_head(&cdev
->ep1_wait_queue
);
440 init_waitqueue_head(&cdev
->prepare_wait_queue
);
442 if (usb_submit_urb(&cdev
->ep1_in_urb
, GFP_KERNEL
) != 0)
445 err
= snd_usb_caiaq_send_command(cdev
, EP1_CMD_GET_DEVICE_INFO
, NULL
, 0);
449 if (!wait_event_timeout(cdev
->ep1_wait_queue
, cdev
->spec_received
, HZ
))
452 usb_string(usb_dev
, usb_dev
->descriptor
.iManufacturer
,
453 cdev
->vendor_name
, CAIAQ_USB_STR_LEN
);
455 usb_string(usb_dev
, usb_dev
->descriptor
.iProduct
,
456 cdev
->product_name
, CAIAQ_USB_STR_LEN
);
458 strlcpy(card
->driver
, MODNAME
, sizeof(card
->driver
));
459 strlcpy(card
->shortname
, cdev
->product_name
, sizeof(card
->shortname
));
460 strlcpy(card
->mixername
, cdev
->product_name
, sizeof(card
->mixername
));
462 /* if the id was not passed as module option, fill it with a shortened
463 * version of the product string which does not contain any
466 if (*card
->id
== '\0') {
467 char id
[sizeof(card
->id
)];
469 memset(id
, 0, sizeof(id
));
471 for (c
= card
->shortname
, len
= 0;
472 *c
&& len
< sizeof(card
->id
); c
++)
476 snd_card_set_id(card
, id
);
479 usb_make_path(usb_dev
, usbpath
, sizeof(usbpath
));
480 snprintf(card
->longname
, sizeof(card
->longname
), "%s %s (%s)",
481 cdev
->vendor_name
, cdev
->product_name
, usbpath
);
487 static int snd_probe(struct usb_interface
*intf
,
488 const struct usb_device_id
*id
)
491 struct snd_card
*card
= NULL
;
492 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
494 ret
= create_card(usb_dev
, intf
, &card
);
499 usb_set_intfdata(intf
, card
);
500 ret
= init_card(caiaqdev(card
));
502 dev_err(&usb_dev
->dev
, "unable to init card! (ret=%d)\n", ret
);
510 static void snd_disconnect(struct usb_interface
*intf
)
512 struct snd_card
*card
= usb_get_intfdata(intf
);
513 struct device
*dev
= intf
->usb_dev
;
514 struct snd_usb_caiaqdev
*cdev
;
519 cdev
= caiaqdev(card
);
520 dev_dbg(dev
, "%s(%p)\n", __func__
, intf
);
522 snd_card_disconnect(card
);
524 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
525 snd_usb_caiaq_input_free(cdev
);
527 snd_usb_caiaq_audio_free(cdev
);
529 usb_kill_urb(&cdev
->ep1_in_urb
);
530 usb_kill_urb(&cdev
->midi_out_urb
);
533 usb_reset_device(interface_to_usbdev(intf
));
537 MODULE_DEVICE_TABLE(usb
, snd_usb_id_table
);
538 static struct usb_driver snd_usb_driver
= {
541 .disconnect
= snd_disconnect
,
542 .id_table
= snd_usb_id_table
,
545 module_usb_driver(snd_usb_driver
);