1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * caiaq.c: ALSA driver for caiaq/NativeInstruments devices
4 *
5 * Copyright (c) 2007 Daniel Mack <daniel@caiaq.de>
6 * Karsten Wiese <fzu@wemgehoertderstaat.de>
7 */
8
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>
19
20 #include "device.h"
21 #include "audio.h"
22 #include "midi.h"
23 #include "control.h"
24 #include "input.h"
25
26 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
27 MODULE_DESCRIPTION("caiaq USB audio");
28 MODULE_LICENSE("GPL");
29
30 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
31 static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
32 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
33
34 module_param_array(index, int, NULL, 0444);
35 MODULE_PARM_DESC(index, "Index value for the caiaq sound device");
36 module_param_array(id, charp, NULL, 0444);
37 MODULE_PARM_DESC(id, "ID string for the caiaq soundcard.");
38 module_param_array(enable, bool, NULL, 0444);
39 MODULE_PARM_DESC(enable, "Enable the caiaq soundcard.");
40
41 enum {
42 SAMPLERATE_44100 = 0,
43 SAMPLERATE_48000 = 1,
44 SAMPLERATE_96000 = 2,
45 SAMPLERATE_192000 = 3,
46 SAMPLERATE_88200 = 4,
47 SAMPLERATE_INVALID = 0xff
48 };
49
50 enum {
51 DEPTH_NONE = 0,
52 DEPTH_16 = 1,
53 DEPTH_24 = 2,
54 DEPTH_32 = 3
55 };
56
57 static const struct usb_device_id snd_usb_id_table[] = {
58 {
59 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
60 .idVendor = USB_VID_NATIVEINSTRUMENTS,
61 .idProduct = USB_PID_RIGKONTROL2
62 },
63 {
64 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
65 .idVendor = USB_VID_NATIVEINSTRUMENTS,
66 .idProduct = USB_PID_RIGKONTROL3
67 },
68 {
69 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
70 .idVendor = USB_VID_NATIVEINSTRUMENTS,
71 .idProduct = USB_PID_KORECONTROLLER
72 },
73 {
74 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
75 .idVendor = USB_VID_NATIVEINSTRUMENTS,
76 .idProduct = USB_PID_KORECONTROLLER2
77 },
78 {
79 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
80 .idVendor = USB_VID_NATIVEINSTRUMENTS,
81 .idProduct = USB_PID_AK1
82 },
83 {
84 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
85 .idVendor = USB_VID_NATIVEINSTRUMENTS,
86 .idProduct = USB_PID_AUDIO8DJ
87 },
88 {
89 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
90 .idVendor = USB_VID_NATIVEINSTRUMENTS,
91 .idProduct = USB_PID_SESSIONIO
92 },
93 {
94 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
95 .idVendor = USB_VID_NATIVEINSTRUMENTS,
96 .idProduct = USB_PID_GUITARRIGMOBILE
97 },
98 {
99 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
100 .idVendor = USB_VID_NATIVEINSTRUMENTS,
101 .idProduct = USB_PID_AUDIO4DJ
102 },
103 {
104 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
105 .idVendor = USB_VID_NATIVEINSTRUMENTS,
106 .idProduct = USB_PID_AUDIO2DJ
107 },
108 {
109 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
110 .idVendor = USB_VID_NATIVEINSTRUMENTS,
111 .idProduct = USB_PID_TRAKTORKONTROLX1
112 },
113 {
114 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
115 .idVendor = USB_VID_NATIVEINSTRUMENTS,
116 .idProduct = USB_PID_TRAKTORKONTROLS4
117 },
118 {
119 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
120 .idVendor = USB_VID_NATIVEINSTRUMENTS,
121 .idProduct = USB_PID_TRAKTORAUDIO2
122 },
123 {
124 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
125 .idVendor = USB_VID_NATIVEINSTRUMENTS,
126 .idProduct = USB_PID_MASCHINECONTROLLER
127 },
128 { /* terminator */ }
129 };
130
usb_ep1_command_reply_dispatch(struct urb * urb)131 static void usb_ep1_command_reply_dispatch (struct urb* urb)
132 {
133 int ret;
134 struct device *dev = &urb->dev->dev;
135 struct snd_usb_caiaqdev *cdev = urb->context;
136 unsigned char *buf = urb->transfer_buffer;
137 unsigned int payload_len;
138 unsigned int copy_len;
139
140 if (urb->status || !cdev) {
141 dev_warn(dev, "received EP1 urb->status = %i\n", urb->status);
142 return;
143 }
144 if (urb->actual_length < 1)
145 return;
146
147 payload_len = urb->actual_length - 1;
148
149 switch(buf[0]) {
150 case EP1_CMD_GET_DEVICE_INFO:
151 if (payload_len < sizeof(struct caiaq_device_spec))
152 break;
153 memcpy(&cdev->spec, buf+1, sizeof(struct caiaq_device_spec));
154 cdev->spec.fw_version = le16_to_cpu(cdev->spec.fw_version);
155 dev_dbg(dev, "device spec (firmware %d): audio: %d in, %d out, "
156 "MIDI: %d in, %d out, data alignment %d\n",
157 cdev->spec.fw_version,
158 cdev->spec.num_analog_audio_in,
159 cdev->spec.num_analog_audio_out,
160 cdev->spec.num_midi_in,
161 cdev->spec.num_midi_out,
162 cdev->spec.data_alignment);
163
164 cdev->spec_received++;
165 wake_up(&cdev->ep1_wait_queue);
166 break;
167 case EP1_CMD_AUDIO_PARAMS:
168 if (payload_len < 1)
169 break;
170 cdev->audio_parm_answer = buf[1];
171 wake_up(&cdev->ep1_wait_queue);
172 break;
173 case EP1_CMD_MIDI_READ:
174 if (urb->actual_length < 3 || urb->actual_length - 3 < buf[2])
175 break;
176 snd_usb_caiaq_midi_handle_input(cdev, buf[1], buf + 3, buf[2]);
177 break;
178 case EP1_CMD_READ_IO:
179 if (cdev->chip.usb_id ==
180 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) {
181 copy_len = min_t(unsigned int, payload_len, sizeof(cdev->control_state));
182 memcpy(cdev->control_state, buf + 1, copy_len);
183 wake_up(&cdev->ep1_wait_queue);
184 break;
185 }
186 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
187 fallthrough;
188 case EP1_CMD_READ_ERP:
189 case EP1_CMD_READ_ANALOG:
190 snd_usb_caiaq_input_dispatch(cdev, buf, urb->actual_length);
191 #endif
192 break;
193 }
194
195 cdev->ep1_in_urb.actual_length = 0;
196 ret = usb_submit_urb(&cdev->ep1_in_urb, GFP_ATOMIC);
197 if (ret < 0)
198 dev_err(dev, "unable to submit urb. OOM!?\n");
199 }
200
snd_usb_caiaq_send_command(struct snd_usb_caiaqdev * cdev,unsigned char command,const unsigned char * buffer,int len)201 int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *cdev,
202 unsigned char command,
203 const unsigned char *buffer,
204 int len)
205 {
206 int actual_len;
207 struct usb_device *usb_dev = cdev->chip.dev;
208
209 if (!usb_dev)
210 return -EIO;
211
212 if (len > EP1_BUFSIZE - 1)
213 len = EP1_BUFSIZE - 1;
214
215 if (buffer && len > 0)
216 memcpy(cdev->ep1_out_buf+1, buffer, len);
217
218 cdev->ep1_out_buf[0] = command;
219 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
220 cdev->ep1_out_buf, len+1, &actual_len, 200);
221 }
222
snd_usb_caiaq_send_command_bank(struct snd_usb_caiaqdev * cdev,unsigned char command,unsigned char bank,const unsigned char * buffer,int len)223 int snd_usb_caiaq_send_command_bank(struct snd_usb_caiaqdev *cdev,
224 unsigned char command,
225 unsigned char bank,
226 const unsigned char *buffer,
227 int len)
228 {
229 int actual_len;
230 struct usb_device *usb_dev = cdev->chip.dev;
231
232 if (!usb_dev)
233 return -EIO;
234
235 if (len > EP1_BUFSIZE - 2)
236 len = EP1_BUFSIZE - 2;
237
238 if (buffer && len > 0)
239 memcpy(cdev->ep1_out_buf+2, buffer, len);
240
241 cdev->ep1_out_buf[0] = command;
242 cdev->ep1_out_buf[1] = bank;
243
244 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
245 cdev->ep1_out_buf, len+2, &actual_len, 200);
246 }
247
snd_usb_caiaq_set_audio_params(struct snd_usb_caiaqdev * cdev,int rate,int depth,int bpp)248 int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *cdev,
249 int rate, int depth, int bpp)
250 {
251 int ret;
252 char tmp[5];
253 struct device *dev = caiaqdev_to_dev(cdev);
254
255 switch (rate) {
256 case 44100: tmp[0] = SAMPLERATE_44100; break;
257 case 48000: tmp[0] = SAMPLERATE_48000; break;
258 case 88200: tmp[0] = SAMPLERATE_88200; break;
259 case 96000: tmp[0] = SAMPLERATE_96000; break;
260 case 192000: tmp[0] = SAMPLERATE_192000; break;
261 default: return -EINVAL;
262 }
263
264 switch (depth) {
265 case 16: tmp[1] = DEPTH_16; break;
266 case 24: tmp[1] = DEPTH_24; break;
267 default: return -EINVAL;
268 }
269
270 tmp[2] = bpp & 0xff;
271 tmp[3] = bpp >> 8;
272 tmp[4] = 1; /* packets per microframe */
273
274 dev_dbg(dev, "setting audio params: %d Hz, %d bits, %d bpp\n",
275 rate, depth, bpp);
276
277 cdev->audio_parm_answer = -1;
278 ret = snd_usb_caiaq_send_command(cdev, EP1_CMD_AUDIO_PARAMS,
279 tmp, sizeof(tmp));
280
281 if (ret)
282 return ret;
283
284 if (!wait_event_timeout(cdev->ep1_wait_queue,
285 cdev->audio_parm_answer >= 0, HZ))
286 return -EPIPE;
287
288 if (cdev->audio_parm_answer != 1)
289 dev_dbg(dev, "unable to set the device's audio params\n");
290 else
291 cdev->bpp = bpp;
292
293 return cdev->audio_parm_answer == 1 ? 0 : -EINVAL;
294 }
295
snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev * cdev,int digital,int analog,int erp)296 int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev *cdev,
297 int digital, int analog, int erp)
298 {
299 char tmp[3] = { digital, analog, erp };
300 return snd_usb_caiaq_send_command(cdev, EP1_CMD_AUTO_MSG,
301 tmp, sizeof(tmp));
302 }
303
setup_card(struct snd_usb_caiaqdev * cdev)304 static int setup_card(struct snd_usb_caiaqdev *cdev)
305 {
306 int ret;
307 char val[4];
308 struct device *dev = caiaqdev_to_dev(cdev);
309
310 /* device-specific startup specials */
311 switch (cdev->chip.usb_id) {
312 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
313 /* RigKontrol2 - display centered dash ('-') */
314 val[0] = 0x00;
315 val[1] = 0x00;
316 val[2] = 0x01;
317 snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 3);
318 break;
319 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
320 /* RigKontrol2 - display two centered dashes ('--') */
321 val[0] = 0x00;
322 val[1] = 0x40;
323 val[2] = 0x40;
324 val[3] = 0x00;
325 snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 4);
326 break;
327 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
328 /* Audio Kontrol 1 - make USB-LED stop blinking */
329 val[0] = 0x00;
330 snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 1);
331 break;
332 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
333 /* Audio 8 DJ - trigger read of current settings */
334 cdev->control_state[0] = 0xff;
335 snd_usb_caiaq_set_auto_msg(cdev, 1, 0, 0);
336 snd_usb_caiaq_send_command(cdev, EP1_CMD_READ_IO, NULL, 0);
337
338 if (!wait_event_timeout(cdev->ep1_wait_queue,
339 cdev->control_state[0] != 0xff, HZ)) {
340 dev_err(dev, "Read timeout for control state\n");
341 return -EINVAL;
342 }
343
344 /* fix up some defaults */
345 if ((cdev->control_state[1] != 2) ||
346 (cdev->control_state[2] != 3) ||
347 (cdev->control_state[4] != 2)) {
348 cdev->control_state[1] = 2;
349 cdev->control_state[2] = 3;
350 cdev->control_state[4] = 2;
351 snd_usb_caiaq_send_command(cdev,
352 EP1_CMD_WRITE_IO, cdev->control_state, 6);
353 }
354
355 break;
356 }
357
358 if (cdev->spec.num_analog_audio_out +
359 cdev->spec.num_analog_audio_in +
360 cdev->spec.num_digital_audio_out +
361 cdev->spec.num_digital_audio_in > 0) {
362 ret = snd_usb_caiaq_audio_init(cdev);
363 if (ret < 0) {
364 dev_err(dev, "Unable to set up audio system (ret=%d)\n", ret);
365 return ret;
366 }
367 }
368
369 if (cdev->spec.num_midi_in +
370 cdev->spec.num_midi_out > 0) {
371 ret = snd_usb_caiaq_midi_init(cdev);
372 if (ret < 0) {
373 dev_err(dev, "Unable to set up MIDI system (ret=%d)\n", ret);
374 return ret;
375 }
376 }
377
378 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
379 ret = snd_usb_caiaq_input_init(cdev);
380 if (ret < 0 && ret != -ENODEV) {
381 dev_err(dev, "Unable to set up input system (ret=%d)\n", ret);
382 return ret;
383 }
384 #endif
385
386 /* finally, register the card and all its sub-instances */
387 ret = snd_card_register(cdev->chip.card);
388 if (ret < 0) {
389 dev_err(dev, "snd_card_register() returned %d\n", ret);
390 return ret;
391 }
392
393 ret = snd_usb_caiaq_control_init(cdev);
394 if (ret < 0) {
395 dev_err(dev, "Unable to set up control system (ret=%d)\n", ret);
396 return ret;
397 }
398
399 return 0;
400 }
401
card_free(struct snd_card * card)402 static void card_free(struct snd_card *card)
403 {
404 struct snd_usb_caiaqdev *cdev = caiaqdev(card);
405
406 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
407 snd_usb_caiaq_input_free(cdev);
408 #endif
409 snd_usb_caiaq_audio_free(cdev);
410 usb_put_dev(cdev->chip.dev);
411 }
412
create_card(struct usb_device * usb_dev,struct usb_interface * intf,struct snd_card ** cardp)413 static int create_card(struct usb_device *usb_dev,
414 struct usb_interface *intf,
415 struct snd_card **cardp)
416 {
417 int devnum;
418 int err;
419 struct snd_card *card;
420 struct snd_usb_caiaqdev *cdev;
421
422 for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
423 if (enable[devnum])
424 break;
425
426 if (devnum >= SNDRV_CARDS)
427 return -ENODEV;
428
429 err = snd_card_new(&intf->dev,
430 index[devnum], id[devnum], THIS_MODULE,
431 sizeof(struct snd_usb_caiaqdev), &card);
432 if (err < 0)
433 return err;
434
435 cdev = caiaqdev(card);
436 cdev->chip.dev = usb_get_dev(usb_dev);
437 card->private_free = card_free;
438 cdev->chip.card = card;
439 cdev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor),
440 le16_to_cpu(usb_dev->descriptor.idProduct));
441 spin_lock_init(&cdev->spinlock);
442
443 *cardp = card;
444 return 0;
445 }
446
init_card(struct snd_usb_caiaqdev * cdev)447 static int init_card(struct snd_usb_caiaqdev *cdev)
448 {
449 char *c, usbpath[32];
450 struct usb_device *usb_dev = cdev->chip.dev;
451 struct snd_card *card = cdev->chip.card;
452 struct device *dev = caiaqdev_to_dev(cdev);
453 int err, len;
454
455 if (usb_set_interface(usb_dev, 0, 1) != 0) {
456 dev_err(dev, "can't set alt interface.\n");
457 return -EIO;
458 }
459
460 usb_init_urb(&cdev->ep1_in_urb);
461 usb_init_urb(&cdev->midi_out_urb);
462
463 usb_fill_bulk_urb(&cdev->ep1_in_urb, usb_dev,
464 usb_rcvbulkpipe(usb_dev, 0x1),
465 cdev->ep1_in_buf, EP1_BUFSIZE,
466 usb_ep1_command_reply_dispatch, cdev);
467
468 usb_fill_bulk_urb(&cdev->midi_out_urb, usb_dev,
469 usb_sndbulkpipe(usb_dev, 0x1),
470 cdev->midi_out_buf, EP1_BUFSIZE,
471 snd_usb_caiaq_midi_output_done, cdev);
472
473 /* sanity checks of EPs before actually submitting */
474 if (usb_urb_ep_type_check(&cdev->ep1_in_urb) ||
475 usb_urb_ep_type_check(&cdev->midi_out_urb)) {
476 dev_err(dev, "invalid EPs\n");
477 return -EINVAL;
478 }
479
480 init_waitqueue_head(&cdev->ep1_wait_queue);
481 init_waitqueue_head(&cdev->prepare_wait_queue);
482
483 if (usb_submit_urb(&cdev->ep1_in_urb, GFP_KERNEL) != 0)
484 return -EIO;
485
486 err = snd_usb_caiaq_send_command(cdev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
487 if (err)
488 goto err_kill_urb;
489
490 if (!wait_event_timeout(cdev->ep1_wait_queue, cdev->spec_received, HZ)) {
491 err = -ENODEV;
492 goto err_kill_urb;
493 }
494
495 usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
496 cdev->vendor_name, CAIAQ_USB_STR_LEN);
497
498 usb_string(usb_dev, usb_dev->descriptor.iProduct,
499 cdev->product_name, CAIAQ_USB_STR_LEN);
500
501 strscpy(card->driver, MODNAME, sizeof(card->driver));
502 strscpy(card->shortname, cdev->product_name, sizeof(card->shortname));
503 strscpy(card->mixername, cdev->product_name, sizeof(card->mixername));
504
505 /* if the id was not passed as module option, fill it with a shortened
506 * version of the product string which does not contain any
507 * whitespaces */
508
509 if (*card->id == '\0') {
510 char id[sizeof(card->id)];
511
512 memset(id, 0, sizeof(id));
513
514 for (c = card->shortname, len = 0;
515 *c && len < sizeof(card->id) - 1; c++)
516 if (*c != ' ')
517 id[len++] = *c;
518
519 snd_card_set_id(card, id);
520 }
521
522 usb_make_path(usb_dev, usbpath, sizeof(usbpath));
523 scnprintf(card->longname, sizeof(card->longname), "%s %s (%s)",
524 cdev->vendor_name, cdev->product_name, usbpath);
525
526 err = setup_card(cdev);
527 if (err < 0)
528 goto err_kill_urb;
529
530 return 0;
531
532 err_kill_urb:
533 usb_kill_urb(&cdev->ep1_in_urb);
534 return err;
535 }
536
snd_probe(struct usb_interface * intf,const struct usb_device_id * id)537 static int snd_probe(struct usb_interface *intf,
538 const struct usb_device_id *id)
539 {
540 int ret;
541 struct snd_card *card = NULL;
542 struct usb_device *usb_dev = interface_to_usbdev(intf);
543
544 ret = create_card(usb_dev, intf, &card);
545
546 if (ret < 0)
547 return ret;
548
549 usb_set_intfdata(intf, card);
550 ret = init_card(caiaqdev(card));
551 if (ret < 0) {
552 dev_err(&usb_dev->dev, "unable to init card! (ret=%d)\n", ret);
553 snd_card_free(card);
554 return ret;
555 }
556
557 return 0;
558 }
559
snd_disconnect(struct usb_interface * intf)560 static void snd_disconnect(struct usb_interface *intf)
561 {
562 struct snd_card *card = usb_get_intfdata(intf);
563 struct device *dev = intf->usb_dev;
564 struct snd_usb_caiaqdev *cdev;
565
566 if (!card)
567 return;
568
569 cdev = caiaqdev(card);
570 dev_dbg(dev, "%s(%p)\n", __func__, intf);
571
572 snd_card_disconnect(card);
573
574 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
575 snd_usb_caiaq_input_disconnect(cdev);
576 #endif
577 snd_usb_caiaq_audio_disconnect(cdev);
578
579 usb_kill_urb(&cdev->ep1_in_urb);
580 usb_kill_urb(&cdev->midi_out_urb);
581
582 snd_card_free_when_closed(card);
583 }
584
585
586 MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
587 static struct usb_driver snd_usb_driver = {
588 .name = MODNAME,
589 .probe = snd_probe,
590 .disconnect = snd_disconnect,
591 .id_table = snd_usb_id_table,
592 };
593
594 module_usb_driver(snd_usb_driver);
595