1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * caiaq.c: ALSA driver for caiaq/NativeInstruments devices
5 * Copyright (c) 2007 Daniel Mack <daniel@caiaq.de>
6 * Karsten Wiese <fzu@wemgehoertderstaat.de>
9 #include <linux/moduleparam.h>
10 #include <linux/device.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/gfp.h>
15 #include <linux/usb.h>
16 #include <sound/initval.h>
17 #include <sound/core.h>
18 #include <sound/pcm.h>
26 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
27 MODULE_DESCRIPTION("caiaq USB audio");
28 MODULE_LICENSE("GPL");
29 MODULE_SUPPORTED_DEVICE("{{Native Instruments,RigKontrol2},"
30 "{Native Instruments,RigKontrol3},"
31 "{Native Instruments,Kore Controller},"
32 "{Native Instruments,Kore Controller 2},"
33 "{Native Instruments,Audio Kontrol 1},"
34 "{Native Instruments,Audio 2 DJ},"
35 "{Native Instruments,Audio 4 DJ},"
36 "{Native Instruments,Audio 8 DJ},"
37 "{Native Instruments,Traktor Audio 2},"
38 "{Native Instruments,Session I/O},"
39 "{Native Instruments,GuitarRig mobile},"
40 "{Native Instruments,Traktor Kontrol X1},"
41 "{Native Instruments,Traktor Kontrol S4},"
42 "{Native Instruments,Maschine Controller}}");
44 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
; /* Index 0-max */
45 static char* id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
; /* Id for this card */
46 static bool enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE_PNP
; /* Enable this card */
48 module_param_array(index
, int, NULL
, 0444);
49 MODULE_PARM_DESC(index
, "Index value for the caiaq sound device");
50 module_param_array(id
, charp
, NULL
, 0444);
51 MODULE_PARM_DESC(id
, "ID string for the caiaq soundcard.");
52 module_param_array(enable
, bool, NULL
, 0444);
53 MODULE_PARM_DESC(enable
, "Enable the caiaq soundcard.");
59 SAMPLERATE_192000
= 3,
61 SAMPLERATE_INVALID
= 0xff
71 static const struct usb_device_id snd_usb_id_table
[] = {
73 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
74 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
75 .idProduct
= USB_PID_RIGKONTROL2
78 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
79 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
80 .idProduct
= USB_PID_RIGKONTROL3
83 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
84 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
85 .idProduct
= USB_PID_KORECONTROLLER
88 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
89 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
90 .idProduct
= USB_PID_KORECONTROLLER2
93 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
94 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
95 .idProduct
= USB_PID_AK1
98 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
99 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
100 .idProduct
= USB_PID_AUDIO8DJ
103 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
104 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
105 .idProduct
= USB_PID_SESSIONIO
108 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
109 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
110 .idProduct
= USB_PID_GUITARRIGMOBILE
113 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
114 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
115 .idProduct
= USB_PID_AUDIO4DJ
118 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
119 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
120 .idProduct
= USB_PID_AUDIO2DJ
123 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
124 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
125 .idProduct
= USB_PID_TRAKTORKONTROLX1
128 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
129 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
130 .idProduct
= USB_PID_TRAKTORKONTROLS4
133 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
134 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
135 .idProduct
= USB_PID_TRAKTORAUDIO2
138 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
139 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
140 .idProduct
= USB_PID_MASCHINECONTROLLER
145 static void usb_ep1_command_reply_dispatch (struct urb
* urb
)
148 struct device
*dev
= &urb
->dev
->dev
;
149 struct snd_usb_caiaqdev
*cdev
= urb
->context
;
150 unsigned char *buf
= urb
->transfer_buffer
;
152 if (urb
->status
|| !cdev
) {
153 dev_warn(dev
, "received EP1 urb->status = %i\n", urb
->status
);
158 case EP1_CMD_GET_DEVICE_INFO
:
159 memcpy(&cdev
->spec
, buf
+1, sizeof(struct caiaq_device_spec
));
160 cdev
->spec
.fw_version
= le16_to_cpu(cdev
->spec
.fw_version
);
161 dev_dbg(dev
, "device spec (firmware %d): audio: %d in, %d out, "
162 "MIDI: %d in, %d out, data alignment %d\n",
163 cdev
->spec
.fw_version
,
164 cdev
->spec
.num_analog_audio_in
,
165 cdev
->spec
.num_analog_audio_out
,
166 cdev
->spec
.num_midi_in
,
167 cdev
->spec
.num_midi_out
,
168 cdev
->spec
.data_alignment
);
170 cdev
->spec_received
++;
171 wake_up(&cdev
->ep1_wait_queue
);
173 case EP1_CMD_AUDIO_PARAMS
:
174 cdev
->audio_parm_answer
= buf
[1];
175 wake_up(&cdev
->ep1_wait_queue
);
177 case EP1_CMD_MIDI_READ
:
178 snd_usb_caiaq_midi_handle_input(cdev
, buf
[1], buf
+ 3, buf
[2]);
180 case EP1_CMD_READ_IO
:
181 if (cdev
->chip
.usb_id
==
182 USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AUDIO8DJ
)) {
183 if (urb
->actual_length
> sizeof(cdev
->control_state
))
184 urb
->actual_length
= sizeof(cdev
->control_state
);
185 memcpy(cdev
->control_state
, buf
+ 1, urb
->actual_length
);
186 wake_up(&cdev
->ep1_wait_queue
);
189 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
191 case EP1_CMD_READ_ERP
:
192 case EP1_CMD_READ_ANALOG
:
193 snd_usb_caiaq_input_dispatch(cdev
, buf
, urb
->actual_length
);
198 cdev
->ep1_in_urb
.actual_length
= 0;
199 ret
= usb_submit_urb(&cdev
->ep1_in_urb
, GFP_ATOMIC
);
201 dev_err(dev
, "unable to submit urb. OOM!?\n");
204 int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev
*cdev
,
205 unsigned char command
,
206 const unsigned char *buffer
,
210 struct usb_device
*usb_dev
= cdev
->chip
.dev
;
215 if (len
> EP1_BUFSIZE
- 1)
216 len
= EP1_BUFSIZE
- 1;
218 if (buffer
&& len
> 0)
219 memcpy(cdev
->ep1_out_buf
+1, buffer
, len
);
221 cdev
->ep1_out_buf
[0] = command
;
222 return usb_bulk_msg(usb_dev
, usb_sndbulkpipe(usb_dev
, 1),
223 cdev
->ep1_out_buf
, len
+1, &actual_len
, 200);
226 int snd_usb_caiaq_send_command_bank(struct snd_usb_caiaqdev
*cdev
,
227 unsigned char command
,
229 const unsigned char *buffer
,
233 struct usb_device
*usb_dev
= cdev
->chip
.dev
;
238 if (len
> EP1_BUFSIZE
- 2)
239 len
= EP1_BUFSIZE
- 2;
241 if (buffer
&& len
> 0)
242 memcpy(cdev
->ep1_out_buf
+2, buffer
, len
);
244 cdev
->ep1_out_buf
[0] = command
;
245 cdev
->ep1_out_buf
[1] = bank
;
247 return usb_bulk_msg(usb_dev
, usb_sndbulkpipe(usb_dev
, 1),
248 cdev
->ep1_out_buf
, len
+2, &actual_len
, 200);
251 int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev
*cdev
,
252 int rate
, int depth
, int bpp
)
256 struct device
*dev
= caiaqdev_to_dev(cdev
);
259 case 44100: tmp
[0] = SAMPLERATE_44100
; break;
260 case 48000: tmp
[0] = SAMPLERATE_48000
; break;
261 case 88200: tmp
[0] = SAMPLERATE_88200
; break;
262 case 96000: tmp
[0] = SAMPLERATE_96000
; break;
263 case 192000: tmp
[0] = SAMPLERATE_192000
; break;
264 default: return -EINVAL
;
268 case 16: tmp
[1] = DEPTH_16
; break;
269 case 24: tmp
[1] = DEPTH_24
; break;
270 default: return -EINVAL
;
275 tmp
[4] = 1; /* packets per microframe */
277 dev_dbg(dev
, "setting audio params: %d Hz, %d bits, %d bpp\n",
280 cdev
->audio_parm_answer
= -1;
281 ret
= snd_usb_caiaq_send_command(cdev
, EP1_CMD_AUDIO_PARAMS
,
287 if (!wait_event_timeout(cdev
->ep1_wait_queue
,
288 cdev
->audio_parm_answer
>= 0, HZ
))
291 if (cdev
->audio_parm_answer
!= 1)
292 dev_dbg(dev
, "unable to set the device's audio params\n");
296 return cdev
->audio_parm_answer
== 1 ? 0 : -EINVAL
;
299 int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev
*cdev
,
300 int digital
, int analog
, int erp
)
302 char tmp
[3] = { digital
, analog
, erp
};
303 return snd_usb_caiaq_send_command(cdev
, EP1_CMD_AUTO_MSG
,
307 static void setup_card(struct snd_usb_caiaqdev
*cdev
)
311 struct device
*dev
= caiaqdev_to_dev(cdev
);
313 /* device-specific startup specials */
314 switch (cdev
->chip
.usb_id
) {
315 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_RIGKONTROL2
):
316 /* RigKontrol2 - display centered dash ('-') */
320 snd_usb_caiaq_send_command(cdev
, EP1_CMD_WRITE_IO
, val
, 3);
322 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_RIGKONTROL3
):
323 /* RigKontrol2 - display two centered dashes ('--') */
328 snd_usb_caiaq_send_command(cdev
, EP1_CMD_WRITE_IO
, val
, 4);
330 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AK1
):
331 /* Audio Kontrol 1 - make USB-LED stop blinking */
333 snd_usb_caiaq_send_command(cdev
, EP1_CMD_WRITE_IO
, val
, 1);
335 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AUDIO8DJ
):
336 /* Audio 8 DJ - trigger read of current settings */
337 cdev
->control_state
[0] = 0xff;
338 snd_usb_caiaq_set_auto_msg(cdev
, 1, 0, 0);
339 snd_usb_caiaq_send_command(cdev
, EP1_CMD_READ_IO
, NULL
, 0);
341 if (!wait_event_timeout(cdev
->ep1_wait_queue
,
342 cdev
->control_state
[0] != 0xff, HZ
))
345 /* fix up some defaults */
346 if ((cdev
->control_state
[1] != 2) ||
347 (cdev
->control_state
[2] != 3) ||
348 (cdev
->control_state
[4] != 2)) {
349 cdev
->control_state
[1] = 2;
350 cdev
->control_state
[2] = 3;
351 cdev
->control_state
[4] = 2;
352 snd_usb_caiaq_send_command(cdev
,
353 EP1_CMD_WRITE_IO
, cdev
->control_state
, 6);
359 if (cdev
->spec
.num_analog_audio_out
+
360 cdev
->spec
.num_analog_audio_in
+
361 cdev
->spec
.num_digital_audio_out
+
362 cdev
->spec
.num_digital_audio_in
> 0) {
363 ret
= snd_usb_caiaq_audio_init(cdev
);
365 dev_err(dev
, "Unable to set up audio system (ret=%d)\n", ret
);
368 if (cdev
->spec
.num_midi_in
+
369 cdev
->spec
.num_midi_out
> 0) {
370 ret
= snd_usb_caiaq_midi_init(cdev
);
372 dev_err(dev
, "Unable to set up MIDI system (ret=%d)\n", ret
);
375 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
376 ret
= snd_usb_caiaq_input_init(cdev
);
378 dev_err(dev
, "Unable to set up input system (ret=%d)\n", ret
);
381 /* finally, register the card and all its sub-instances */
382 ret
= snd_card_register(cdev
->chip
.card
);
384 dev_err(dev
, "snd_card_register() returned %d\n", ret
);
385 snd_card_free(cdev
->chip
.card
);
388 ret
= snd_usb_caiaq_control_init(cdev
);
390 dev_err(dev
, "Unable to set up control system (ret=%d)\n", ret
);
393 static int create_card(struct usb_device
*usb_dev
,
394 struct usb_interface
*intf
,
395 struct snd_card
**cardp
)
399 struct snd_card
*card
;
400 struct snd_usb_caiaqdev
*cdev
;
402 for (devnum
= 0; devnum
< SNDRV_CARDS
; devnum
++)
406 if (devnum
>= SNDRV_CARDS
)
409 err
= snd_card_new(&intf
->dev
,
410 index
[devnum
], id
[devnum
], THIS_MODULE
,
411 sizeof(struct snd_usb_caiaqdev
), &card
);
415 cdev
= caiaqdev(card
);
416 cdev
->chip
.dev
= usb_dev
;
417 cdev
->chip
.card
= card
;
418 cdev
->chip
.usb_id
= USB_ID(le16_to_cpu(usb_dev
->descriptor
.idVendor
),
419 le16_to_cpu(usb_dev
->descriptor
.idProduct
));
420 spin_lock_init(&cdev
->spinlock
);
426 static int init_card(struct snd_usb_caiaqdev
*cdev
)
428 char *c
, usbpath
[32];
429 struct usb_device
*usb_dev
= cdev
->chip
.dev
;
430 struct snd_card
*card
= cdev
->chip
.card
;
431 struct device
*dev
= caiaqdev_to_dev(cdev
);
434 if (usb_set_interface(usb_dev
, 0, 1) != 0) {
435 dev_err(dev
, "can't set alt interface.\n");
439 usb_init_urb(&cdev
->ep1_in_urb
);
440 usb_init_urb(&cdev
->midi_out_urb
);
442 usb_fill_bulk_urb(&cdev
->ep1_in_urb
, usb_dev
,
443 usb_rcvbulkpipe(usb_dev
, 0x1),
444 cdev
->ep1_in_buf
, EP1_BUFSIZE
,
445 usb_ep1_command_reply_dispatch
, cdev
);
447 usb_fill_bulk_urb(&cdev
->midi_out_urb
, usb_dev
,
448 usb_sndbulkpipe(usb_dev
, 0x1),
449 cdev
->midi_out_buf
, EP1_BUFSIZE
,
450 snd_usb_caiaq_midi_output_done
, cdev
);
452 /* sanity checks of EPs before actually submitting */
453 if (usb_urb_ep_type_check(&cdev
->ep1_in_urb
) ||
454 usb_urb_ep_type_check(&cdev
->midi_out_urb
)) {
455 dev_err(dev
, "invalid EPs\n");
459 init_waitqueue_head(&cdev
->ep1_wait_queue
);
460 init_waitqueue_head(&cdev
->prepare_wait_queue
);
462 if (usb_submit_urb(&cdev
->ep1_in_urb
, GFP_KERNEL
) != 0)
465 err
= snd_usb_caiaq_send_command(cdev
, EP1_CMD_GET_DEVICE_INFO
, NULL
, 0);
469 if (!wait_event_timeout(cdev
->ep1_wait_queue
, cdev
->spec_received
, HZ
)) {
474 usb_string(usb_dev
, usb_dev
->descriptor
.iManufacturer
,
475 cdev
->vendor_name
, CAIAQ_USB_STR_LEN
);
477 usb_string(usb_dev
, usb_dev
->descriptor
.iProduct
,
478 cdev
->product_name
, CAIAQ_USB_STR_LEN
);
480 strlcpy(card
->driver
, MODNAME
, sizeof(card
->driver
));
481 strlcpy(card
->shortname
, cdev
->product_name
, sizeof(card
->shortname
));
482 strlcpy(card
->mixername
, cdev
->product_name
, sizeof(card
->mixername
));
484 /* if the id was not passed as module option, fill it with a shortened
485 * version of the product string which does not contain any
488 if (*card
->id
== '\0') {
489 char id
[sizeof(card
->id
)];
491 memset(id
, 0, sizeof(id
));
493 for (c
= card
->shortname
, len
= 0;
494 *c
&& len
< sizeof(card
->id
); c
++)
498 snd_card_set_id(card
, id
);
501 usb_make_path(usb_dev
, usbpath
, sizeof(usbpath
));
502 snprintf(card
->longname
, sizeof(card
->longname
), "%s %s (%s)",
503 cdev
->vendor_name
, cdev
->product_name
, usbpath
);
509 usb_kill_urb(&cdev
->ep1_in_urb
);
513 static int snd_probe(struct usb_interface
*intf
,
514 const struct usb_device_id
*id
)
517 struct snd_card
*card
= NULL
;
518 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
520 ret
= create_card(usb_dev
, intf
, &card
);
525 usb_set_intfdata(intf
, card
);
526 ret
= init_card(caiaqdev(card
));
528 dev_err(&usb_dev
->dev
, "unable to init card! (ret=%d)\n", ret
);
536 static void snd_disconnect(struct usb_interface
*intf
)
538 struct snd_card
*card
= usb_get_intfdata(intf
);
539 struct device
*dev
= intf
->usb_dev
;
540 struct snd_usb_caiaqdev
*cdev
;
545 cdev
= caiaqdev(card
);
546 dev_dbg(dev
, "%s(%p)\n", __func__
, intf
);
548 snd_card_disconnect(card
);
550 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
551 snd_usb_caiaq_input_free(cdev
);
553 snd_usb_caiaq_audio_free(cdev
);
555 usb_kill_urb(&cdev
->ep1_in_urb
);
556 usb_kill_urb(&cdev
->midi_out_urb
);
559 usb_reset_device(interface_to_usbdev(intf
));
563 MODULE_DEVICE_TABLE(usb
, snd_usb_id_table
);
564 static struct usb_driver snd_usb_driver
= {
567 .disconnect
= snd_disconnect
,
568 .id_table
= snd_usb_id_table
,
571 module_usb_driver(snd_usb_driver
);