xref: /openbmc/linux/sound/usb/caiaq/device.c (revision 76426e23)
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 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}}");
43 
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 */
47 
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.");
54 
55 enum {
56 	SAMPLERATE_44100	= 0,
57 	SAMPLERATE_48000	= 1,
58 	SAMPLERATE_96000	= 2,
59 	SAMPLERATE_192000	= 3,
60 	SAMPLERATE_88200	= 4,
61 	SAMPLERATE_INVALID	= 0xff
62 };
63 
64 enum {
65 	DEPTH_NONE	= 0,
66 	DEPTH_16	= 1,
67 	DEPTH_24	= 2,
68 	DEPTH_32	= 3
69 };
70 
71 static const struct usb_device_id snd_usb_id_table[] = {
72 	{
73 		.match_flags =	USB_DEVICE_ID_MATCH_DEVICE,
74 		.idVendor =	USB_VID_NATIVEINSTRUMENTS,
75 		.idProduct =	USB_PID_RIGKONTROL2
76 	},
77 	{
78 		.match_flags =	USB_DEVICE_ID_MATCH_DEVICE,
79 		.idVendor =	USB_VID_NATIVEINSTRUMENTS,
80 		.idProduct =	USB_PID_RIGKONTROL3
81 	},
82 	{
83 		.match_flags =	USB_DEVICE_ID_MATCH_DEVICE,
84 		.idVendor =	USB_VID_NATIVEINSTRUMENTS,
85 		.idProduct =	USB_PID_KORECONTROLLER
86 	},
87 	{
88 		.match_flags =	USB_DEVICE_ID_MATCH_DEVICE,
89 		.idVendor =	USB_VID_NATIVEINSTRUMENTS,
90 		.idProduct =	USB_PID_KORECONTROLLER2
91 	},
92 	{
93 		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
94 		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
95 		.idProduct =    USB_PID_AK1
96 	},
97 	{
98 		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
99 		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
100 		.idProduct =    USB_PID_AUDIO8DJ
101 	},
102 	{
103 		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
104 		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
105 		.idProduct =    USB_PID_SESSIONIO
106 	},
107 	{
108 		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
109 		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
110 		.idProduct =    USB_PID_GUITARRIGMOBILE
111 	},
112 	{
113 		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
114 		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
115 		.idProduct =    USB_PID_AUDIO4DJ
116 	},
117 	{
118 		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
119 		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
120 		.idProduct =    USB_PID_AUDIO2DJ
121 	},
122 	{
123 		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
124 		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
125 		.idProduct =    USB_PID_TRAKTORKONTROLX1
126 	},
127 	{
128 		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
129 		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
130 		.idProduct =    USB_PID_TRAKTORKONTROLS4
131 	},
132 	{
133 		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
134 		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
135 		.idProduct =    USB_PID_TRAKTORAUDIO2
136 	},
137 	{
138 		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
139 		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
140 		.idProduct =    USB_PID_MASCHINECONTROLLER
141 	},
142 	{ /* terminator */ }
143 };
144 
145 static void usb_ep1_command_reply_dispatch (struct urb* urb)
146 {
147 	int ret;
148 	struct device *dev = &urb->dev->dev;
149 	struct snd_usb_caiaqdev *cdev = urb->context;
150 	unsigned char *buf = urb->transfer_buffer;
151 
152 	if (urb->status || !cdev) {
153 		dev_warn(dev, "received EP1 urb->status = %i\n", urb->status);
154 		return;
155 	}
156 
157 	switch(buf[0]) {
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);
169 
170 		cdev->spec_received++;
171 		wake_up(&cdev->ep1_wait_queue);
172 		break;
173 	case EP1_CMD_AUDIO_PARAMS:
174 		cdev->audio_parm_answer = buf[1];
175 		wake_up(&cdev->ep1_wait_queue);
176 		break;
177 	case EP1_CMD_MIDI_READ:
178 		snd_usb_caiaq_midi_handle_input(cdev, buf[1], buf + 3, buf[2]);
179 		break;
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);
187 			break;
188 		}
189 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
190 		/* fall through */
191 	case EP1_CMD_READ_ERP:
192 	case EP1_CMD_READ_ANALOG:
193 		snd_usb_caiaq_input_dispatch(cdev, buf, urb->actual_length);
194 #endif
195 		break;
196 	}
197 
198 	cdev->ep1_in_urb.actual_length = 0;
199 	ret = usb_submit_urb(&cdev->ep1_in_urb, GFP_ATOMIC);
200 	if (ret < 0)
201 		dev_err(dev, "unable to submit urb. OOM!?\n");
202 }
203 
204 int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *cdev,
205 			       unsigned char command,
206 			       const unsigned char *buffer,
207 			       int len)
208 {
209 	int actual_len;
210 	struct usb_device *usb_dev = cdev->chip.dev;
211 
212 	if (!usb_dev)
213 		return -EIO;
214 
215 	if (len > EP1_BUFSIZE - 1)
216 		len = EP1_BUFSIZE - 1;
217 
218 	if (buffer && len > 0)
219 		memcpy(cdev->ep1_out_buf+1, buffer, len);
220 
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);
224 }
225 
226 int snd_usb_caiaq_send_command_bank(struct snd_usb_caiaqdev *cdev,
227 			       unsigned char command,
228 			       unsigned char bank,
229 			       const unsigned char *buffer,
230 			       int len)
231 {
232 	int actual_len;
233 	struct usb_device *usb_dev = cdev->chip.dev;
234 
235 	if (!usb_dev)
236 		return -EIO;
237 
238 	if (len > EP1_BUFSIZE - 2)
239 		len = EP1_BUFSIZE - 2;
240 
241 	if (buffer && len > 0)
242 		memcpy(cdev->ep1_out_buf+2, buffer, len);
243 
244 	cdev->ep1_out_buf[0] = command;
245 	cdev->ep1_out_buf[1] = bank;
246 
247 	return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
248 			   cdev->ep1_out_buf, len+2, &actual_len, 200);
249 }
250 
251 int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *cdev,
252 		   		    int rate, int depth, int bpp)
253 {
254 	int ret;
255 	char tmp[5];
256 	struct device *dev = caiaqdev_to_dev(cdev);
257 
258 	switch (rate) {
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;
265 	}
266 
267 	switch (depth) {
268 	case 16:	tmp[1] = DEPTH_16;   break;
269 	case 24:	tmp[1] = DEPTH_24;   break;
270 	default:	return -EINVAL;
271 	}
272 
273 	tmp[2] = bpp & 0xff;
274 	tmp[3] = bpp >> 8;
275 	tmp[4] = 1; /* packets per microframe */
276 
277 	dev_dbg(dev, "setting audio params: %d Hz, %d bits, %d bpp\n",
278 		rate, depth, bpp);
279 
280 	cdev->audio_parm_answer = -1;
281 	ret = snd_usb_caiaq_send_command(cdev, EP1_CMD_AUDIO_PARAMS,
282 					 tmp, sizeof(tmp));
283 
284 	if (ret)
285 		return ret;
286 
287 	if (!wait_event_timeout(cdev->ep1_wait_queue,
288 	    cdev->audio_parm_answer >= 0, HZ))
289 		return -EPIPE;
290 
291 	if (cdev->audio_parm_answer != 1)
292 		dev_dbg(dev, "unable to set the device's audio params\n");
293 	else
294 		cdev->bpp = bpp;
295 
296 	return cdev->audio_parm_answer == 1 ? 0 : -EINVAL;
297 }
298 
299 int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev *cdev,
300 			       int digital, int analog, int erp)
301 {
302 	char tmp[3] = { digital, analog, erp };
303 	return snd_usb_caiaq_send_command(cdev, EP1_CMD_AUTO_MSG,
304 					  tmp, sizeof(tmp));
305 }
306 
307 static void setup_card(struct snd_usb_caiaqdev *cdev)
308 {
309 	int ret;
310 	char val[4];
311 	struct device *dev = caiaqdev_to_dev(cdev);
312 
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 ('-') */
317 		val[0] = 0x00;
318 		val[1] = 0x00;
319 		val[2] = 0x01;
320 		snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 3);
321 		break;
322 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
323 		/* RigKontrol2 - display two centered dashes ('--') */
324 		val[0] = 0x00;
325 		val[1] = 0x40;
326 		val[2] = 0x40;
327 		val[3] = 0x00;
328 		snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 4);
329 		break;
330 	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
331 		/* Audio Kontrol 1 - make USB-LED stop blinking */
332 		val[0] = 0x00;
333 		snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 1);
334 		break;
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);
340 
341 		if (!wait_event_timeout(cdev->ep1_wait_queue,
342 					cdev->control_state[0] != 0xff, HZ))
343 			return;
344 
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);
354 		}
355 
356 		break;
357 	}
358 
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);
364 		if (ret < 0)
365 			dev_err(dev, "Unable to set up audio system (ret=%d)\n", ret);
366 	}
367 
368 	if (cdev->spec.num_midi_in +
369 	    cdev->spec.num_midi_out > 0) {
370 		ret = snd_usb_caiaq_midi_init(cdev);
371 		if (ret < 0)
372 			dev_err(dev, "Unable to set up MIDI system (ret=%d)\n", ret);
373 	}
374 
375 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
376 	ret = snd_usb_caiaq_input_init(cdev);
377 	if (ret < 0)
378 		dev_err(dev, "Unable to set up input system (ret=%d)\n", ret);
379 #endif
380 
381 	/* finally, register the card and all its sub-instances */
382 	ret = snd_card_register(cdev->chip.card);
383 	if (ret < 0) {
384 		dev_err(dev, "snd_card_register() returned %d\n", ret);
385 		snd_card_free(cdev->chip.card);
386 	}
387 
388 	ret = snd_usb_caiaq_control_init(cdev);
389 	if (ret < 0)
390 		dev_err(dev, "Unable to set up control system (ret=%d)\n", ret);
391 }
392 
393 static int create_card(struct usb_device *usb_dev,
394 		       struct usb_interface *intf,
395 		       struct snd_card **cardp)
396 {
397 	int devnum;
398 	int err;
399 	struct snd_card *card;
400 	struct snd_usb_caiaqdev *cdev;
401 
402 	for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
403 		if (enable[devnum])
404 			break;
405 
406 	if (devnum >= SNDRV_CARDS)
407 		return -ENODEV;
408 
409 	err = snd_card_new(&intf->dev,
410 			   index[devnum], id[devnum], THIS_MODULE,
411 			   sizeof(struct snd_usb_caiaqdev), &card);
412 	if (err < 0)
413 		return err;
414 
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);
421 
422 	*cardp = card;
423 	return 0;
424 }
425 
426 static int init_card(struct snd_usb_caiaqdev *cdev)
427 {
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);
432 	int err, len;
433 
434 	if (usb_set_interface(usb_dev, 0, 1) != 0) {
435 		dev_err(dev, "can't set alt interface.\n");
436 		return -EIO;
437 	}
438 
439 	usb_init_urb(&cdev->ep1_in_urb);
440 	usb_init_urb(&cdev->midi_out_urb);
441 
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);
446 
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);
451 
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");
456 		return -EINVAL;
457 	}
458 
459 	init_waitqueue_head(&cdev->ep1_wait_queue);
460 	init_waitqueue_head(&cdev->prepare_wait_queue);
461 
462 	if (usb_submit_urb(&cdev->ep1_in_urb, GFP_KERNEL) != 0)
463 		return -EIO;
464 
465 	err = snd_usb_caiaq_send_command(cdev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
466 	if (err)
467 		goto err_kill_urb;
468 
469 	if (!wait_event_timeout(cdev->ep1_wait_queue, cdev->spec_received, HZ)) {
470 		err = -ENODEV;
471 		goto err_kill_urb;
472 	}
473 
474 	usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
475 		   cdev->vendor_name, CAIAQ_USB_STR_LEN);
476 
477 	usb_string(usb_dev, usb_dev->descriptor.iProduct,
478 		   cdev->product_name, CAIAQ_USB_STR_LEN);
479 
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));
483 
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
486 	 * whitespaces */
487 
488 	if (*card->id == '\0') {
489 		char id[sizeof(card->id)];
490 
491 		memset(id, 0, sizeof(id));
492 
493 		for (c = card->shortname, len = 0;
494 			*c && len < sizeof(card->id); c++)
495 			if (*c != ' ')
496 				id[len++] = *c;
497 
498 		snd_card_set_id(card, id);
499 	}
500 
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);
504 
505 	setup_card(cdev);
506 	return 0;
507 
508  err_kill_urb:
509 	usb_kill_urb(&cdev->ep1_in_urb);
510 	return err;
511 }
512 
513 static int snd_probe(struct usb_interface *intf,
514 		     const struct usb_device_id *id)
515 {
516 	int ret;
517 	struct snd_card *card = NULL;
518 	struct usb_device *usb_dev = interface_to_usbdev(intf);
519 
520 	ret = create_card(usb_dev, intf, &card);
521 
522 	if (ret < 0)
523 		return ret;
524 
525 	usb_set_intfdata(intf, card);
526 	ret = init_card(caiaqdev(card));
527 	if (ret < 0) {
528 		dev_err(&usb_dev->dev, "unable to init card! (ret=%d)\n", ret);
529 		snd_card_free(card);
530 		return ret;
531 	}
532 
533 	return 0;
534 }
535 
536 static void snd_disconnect(struct usb_interface *intf)
537 {
538 	struct snd_card *card = usb_get_intfdata(intf);
539 	struct device *dev = intf->usb_dev;
540 	struct snd_usb_caiaqdev *cdev;
541 
542 	if (!card)
543 		return;
544 
545 	cdev = caiaqdev(card);
546 	dev_dbg(dev, "%s(%p)\n", __func__, intf);
547 
548 	snd_card_disconnect(card);
549 
550 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
551 	snd_usb_caiaq_input_free(cdev);
552 #endif
553 	snd_usb_caiaq_audio_free(cdev);
554 
555 	usb_kill_urb(&cdev->ep1_in_urb);
556 	usb_kill_urb(&cdev->midi_out_urb);
557 
558 	snd_card_free(card);
559 	usb_reset_device(interface_to_usbdev(intf));
560 }
561 
562 
563 MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
564 static struct usb_driver snd_usb_driver = {
565 	.name 		= MODNAME,
566 	.probe 		= snd_probe,
567 	.disconnect	= snd_disconnect,
568 	.id_table 	= snd_usb_id_table,
569 };
570 
571 module_usb_driver(snd_usb_driver);
572