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"
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.");
70 SAMPLERATE_192000
= 3,
72 SAMPLERATE_INVALID
= 0xff
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
116 static void usb_ep1_command_reply_dispatch (struct urb
* urb
)
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
);
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
);
143 case EP1_CMD_AUDIO_PARAMS
:
144 dev
->audio_parm_answer
= buf
[1];
145 wake_up(&dev
->ep1_wait_queue
);
147 case EP1_CMD_MIDI_READ
:
148 snd_usb_caiaq_midi_handle_input(dev
, buf
[1], buf
+ 3, buf
[2]);
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
);
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
);
167 dev
->ep1_in_urb
.actual_length
= 0;
168 ret
= usb_submit_urb(&dev
->ep1_in_urb
, GFP_ATOMIC
);
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
,
179 struct usb_device
*usb_dev
= dev
->chip
.dev
;
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
)
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
;
211 case 16: tmp
[1] = DEPTH_16
; break;
212 case 24: tmp
[1] = DEPTH_24
; break;
213 default: return -EINVAL
;
218 tmp
[4] = 1; /* packets per microframe */
220 debug("setting audio params: %d Hz, %d bits, %d bpp\n",
223 dev
->audio_parm_answer
= -1;
224 ret
= snd_usb_caiaq_send_command(dev
, EP1_CMD_AUDIO_PARAMS
,
230 if (!wait_event_timeout(dev
->ep1_wait_queue
,
231 dev
->audio_parm_answer
>= 0, HZ
))
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
,
248 static void __devinit
setup_card(struct snd_usb_caiaqdev
*dev
)
253 /* device-specific startup specials */
254 switch (dev
->chip
.usb_id
) {
255 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_RIGKONTROL2
):
256 /* RigKontrol2 - display centered dash ('-') */
260 snd_usb_caiaq_send_command(dev
, EP1_CMD_WRITE_IO
, val
, 3);
262 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_RIGKONTROL3
):
263 /* RigKontrol2 - display two centered dashes ('--') */
268 snd_usb_caiaq_send_command(dev
, EP1_CMD_WRITE_IO
, val
, 4);
270 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AK1
):
271 /* Audio Kontrol 1 - make USB-LED stop blinking */
273 snd_usb_caiaq_send_command(dev
, EP1_CMD_WRITE_IO
, val
, 1);
275 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AUDIO8DJ
):
276 /* Audio 8 DJ - trigger read of current settings */
277 dev
->control_state
[0] = 0xff;
278 snd_usb_caiaq_set_auto_msg(dev
, 1, 0, 0);
279 snd_usb_caiaq_send_command(dev
, EP1_CMD_READ_IO
, NULL
, 0);
281 if (!wait_event_timeout(dev
->ep1_wait_queue
,
282 dev
->control_state
[0] != 0xff, HZ
))
285 /* fix up some defaults */
286 if ((dev
->control_state
[1] != 2) ||
287 (dev
->control_state
[2] != 3) ||
288 (dev
->control_state
[4] != 2)) {
289 dev
->control_state
[1] = 2;
290 dev
->control_state
[2] = 3;
291 dev
->control_state
[4] = 2;
292 snd_usb_caiaq_send_command(dev
,
293 EP1_CMD_WRITE_IO
, dev
->control_state
, 6);
299 if (dev
->spec
.num_analog_audio_out
+
300 dev
->spec
.num_analog_audio_in
+
301 dev
->spec
.num_digital_audio_out
+
302 dev
->spec
.num_digital_audio_in
> 0) {
303 ret
= snd_usb_caiaq_audio_init(dev
);
305 log("Unable to set up audio system (ret=%d)\n", ret
);
308 if (dev
->spec
.num_midi_in
+
309 dev
->spec
.num_midi_out
> 0) {
310 ret
= snd_usb_caiaq_midi_init(dev
);
312 log("Unable to set up MIDI system (ret=%d)\n", ret
);
315 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
316 ret
= snd_usb_caiaq_input_init(dev
);
318 log("Unable to set up input system (ret=%d)\n", ret
);
321 /* finally, register the card and all its sub-instances */
322 ret
= snd_card_register(dev
->chip
.card
);
324 log("snd_card_register() returned %d\n", ret
);
325 snd_card_free(dev
->chip
.card
);
328 ret
= snd_usb_caiaq_control_init(dev
);
330 log("Unable to set up control system (ret=%d)\n", ret
);
333 static struct snd_card
* create_card(struct usb_device
* usb_dev
)
336 struct snd_card
*card
;
337 struct snd_usb_caiaqdev
*dev
;
339 for (devnum
= 0; devnum
< SNDRV_CARDS
; devnum
++)
340 if (enable
[devnum
] && !snd_card_used
[devnum
])
343 if (devnum
>= SNDRV_CARDS
)
346 card
= snd_card_new(index
[devnum
], id
[devnum
], THIS_MODULE
,
347 sizeof(struct snd_usb_caiaqdev
));
351 dev
= caiaqdev(card
);
352 dev
->chip
.dev
= usb_dev
;
353 dev
->chip
.card
= card
;
354 dev
->chip
.usb_id
= USB_ID(usb_dev
->descriptor
.idVendor
,
355 usb_dev
->descriptor
.idProduct
);
356 spin_lock_init(&dev
->spinlock
);
357 snd_card_set_dev(card
, &usb_dev
->dev
);
362 static int __devinit
init_card(struct snd_usb_caiaqdev
*dev
)
365 struct usb_device
*usb_dev
= dev
->chip
.dev
;
366 struct snd_card
*card
= dev
->chip
.card
;
369 if (usb_set_interface(usb_dev
, 0, 1) != 0) {
370 log("can't set alt interface.\n");
374 usb_init_urb(&dev
->ep1_in_urb
);
375 usb_init_urb(&dev
->midi_out_urb
);
377 usb_fill_bulk_urb(&dev
->ep1_in_urb
, usb_dev
,
378 usb_rcvbulkpipe(usb_dev
, 0x1),
379 dev
->ep1_in_buf
, EP1_BUFSIZE
,
380 usb_ep1_command_reply_dispatch
, dev
);
382 usb_fill_bulk_urb(&dev
->midi_out_urb
, usb_dev
,
383 usb_sndbulkpipe(usb_dev
, 0x1),
384 dev
->midi_out_buf
, EP1_BUFSIZE
,
385 snd_usb_caiaq_midi_output_done
, dev
);
387 init_waitqueue_head(&dev
->ep1_wait_queue
);
388 init_waitqueue_head(&dev
->prepare_wait_queue
);
390 if (usb_submit_urb(&dev
->ep1_in_urb
, GFP_KERNEL
) != 0)
393 err
= snd_usb_caiaq_send_command(dev
, EP1_CMD_GET_DEVICE_INFO
, NULL
, 0);
397 if (!wait_event_timeout(dev
->ep1_wait_queue
, dev
->spec_received
, HZ
))
400 usb_string(usb_dev
, usb_dev
->descriptor
.iManufacturer
,
401 dev
->vendor_name
, CAIAQ_USB_STR_LEN
);
403 usb_string(usb_dev
, usb_dev
->descriptor
.iProduct
,
404 dev
->product_name
, CAIAQ_USB_STR_LEN
);
406 usb_string(usb_dev
, usb_dev
->descriptor
.iSerialNumber
,
407 dev
->serial
, CAIAQ_USB_STR_LEN
);
409 /* terminate serial string at first white space occurence */
410 c
= strchr(dev
->serial
, ' ');
414 strcpy(card
->driver
, MODNAME
);
415 strcpy(card
->shortname
, dev
->product_name
);
417 len
= snprintf(card
->longname
, sizeof(card
->longname
),
418 "%s %s (serial %s, ",
419 dev
->vendor_name
, dev
->product_name
, dev
->serial
);
421 if (len
< sizeof(card
->longname
) - 2)
422 len
+= usb_make_path(usb_dev
, card
->longname
+ len
,
423 sizeof(card
->longname
) - len
);
425 card
->longname
[len
++] = ')';
426 card
->longname
[len
] = '\0';
431 static int __devinit
snd_probe(struct usb_interface
*intf
,
432 const struct usb_device_id
*id
)
435 struct snd_card
*card
;
436 struct usb_device
*device
= interface_to_usbdev(intf
);
438 card
= create_card(device
);
443 dev_set_drvdata(&intf
->dev
, card
);
444 ret
= init_card(caiaqdev(card
));
446 log("unable to init card! (ret=%d)\n", ret
);
454 static void snd_disconnect(struct usb_interface
*intf
)
456 struct snd_usb_caiaqdev
*dev
;
457 struct snd_card
*card
= dev_get_drvdata(&intf
->dev
);
459 debug("snd_disconnect(%p)\n", intf
);
464 dev
= caiaqdev(card
);
465 snd_card_disconnect(card
);
467 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
468 snd_usb_caiaq_input_free(dev
);
470 snd_usb_caiaq_audio_free(dev
);
472 usb_kill_urb(&dev
->ep1_in_urb
);
473 usb_kill_urb(&dev
->midi_out_urb
);
476 usb_reset_device(interface_to_usbdev(intf
));
480 MODULE_DEVICE_TABLE(usb
, snd_usb_id_table
);
481 static struct usb_driver snd_usb_driver
= {
484 .disconnect
= snd_disconnect
,
485 .id_table
= snd_usb_id_table
,
488 static int __init
snd_module_init(void)
490 return usb_register(&snd_usb_driver
);
493 static void __exit
snd_module_exit(void)
495 usb_deregister(&snd_usb_driver
);
498 module_init(snd_module_init
)
499 module_exit(snd_module_exit
)