device.c 15.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * caiaq.c: ALSA driver for caiaq/NativeInstruments devices
 *
 *   Copyright (c) 2007 Daniel Mack <daniel@caiaq.de>
 *                      Karsten Wiese <fzu@wemgehoertderstaat.de>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
*/

#include <linux/moduleparam.h>
23
#include <linux/device.h>
24
#include <linux/interrupt.h>
25 26
#include <linux/module.h>
#include <linux/init.h>
27
#include <linux/gfp.h>
28 29
#include <linux/usb.h>
#include <sound/initval.h>
30
#include <sound/core.h>
31 32
#include <sound/pcm.h>

33 34 35 36 37
#include "device.h"
#include "audio.h"
#include "midi.h"
#include "control.h"
#include "input.h"
38 39

MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
40
MODULE_DESCRIPTION("caiaq USB audio");
41
MODULE_LICENSE("GPL");
42 43 44 45 46 47 48 49 50 51 52 53 54 55
MODULE_SUPPORTED_DEVICE("{{Native Instruments,RigKontrol2},"
			 "{Native Instruments,RigKontrol3},"
			 "{Native Instruments,Kore Controller},"
			 "{Native Instruments,Kore Controller 2},"
			 "{Native Instruments,Audio Kontrol 1},"
			 "{Native Instruments,Audio 2 DJ},"
			 "{Native Instruments,Audio 4 DJ},"
			 "{Native Instruments,Audio 8 DJ},"
			 "{Native Instruments,Traktor Audio 2},"
			 "{Native Instruments,Session I/O},"
			 "{Native Instruments,GuitarRig mobile},"
			 "{Native Instruments,Traktor Kontrol X1},"
			 "{Native Instruments,Traktor Kontrol S4},"
			 "{Native Instruments,Maschine Controller}}");
56 57 58

static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
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 */
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "Index value for the caiaq sound device");
module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ID string for the caiaq soundcard.");
module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "Enable the caiaq soundcard.");

enum {
	SAMPLERATE_44100	= 0,
	SAMPLERATE_48000	= 1,
	SAMPLERATE_96000	= 2,
	SAMPLERATE_192000	= 3,
	SAMPLERATE_88200	= 4,
	SAMPLERATE_INVALID	= 0xff
};

enum {
	DEPTH_NONE	= 0,
	DEPTH_16	= 1,
	DEPTH_24	= 2,
	DEPTH_32	= 3
};

static struct usb_device_id snd_usb_id_table[] = {
	{
		.match_flags =	USB_DEVICE_ID_MATCH_DEVICE,
		.idVendor =	USB_VID_NATIVEINSTRUMENTS,
88
		.idProduct =	USB_PID_RIGKONTROL2
89
	},
90 91 92 93 94
	{
		.match_flags =	USB_DEVICE_ID_MATCH_DEVICE,
		.idVendor =	USB_VID_NATIVEINSTRUMENTS,
		.idProduct =	USB_PID_RIGKONTROL3
	},
95 96 97 98 99
	{
		.match_flags =	USB_DEVICE_ID_MATCH_DEVICE,
		.idVendor =	USB_VID_NATIVEINSTRUMENTS,
		.idProduct =	USB_PID_KORECONTROLLER
	},
100 101 102 103 104
	{
		.match_flags =	USB_DEVICE_ID_MATCH_DEVICE,
		.idVendor =	USB_VID_NATIVEINSTRUMENTS,
		.idProduct =	USB_PID_KORECONTROLLER2
	},
105 106 107 108 109 110 111 112 113 114
	{
		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
		.idProduct =    USB_PID_AK1
	},
	{
		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
		.idProduct =    USB_PID_AUDIO8DJ
	},
115 116 117 118 119
	{
		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
		.idProduct =    USB_PID_SESSIONIO
	},
120 121 122 123 124 125 126 127 128 129
	{
		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
		.idProduct =    USB_PID_GUITARRIGMOBILE
	},
	{
		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
		.idProduct =    USB_PID_AUDIO4DJ
	},
130 131 132 133 134
	{
		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
		.idProduct =    USB_PID_AUDIO2DJ
	},
135 136 137 138 139
	{
		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
		.idProduct =    USB_PID_TRAKTORKONTROLX1
	},
140 141 142 143 144
	{
		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
		.idProduct =    USB_PID_TRAKTORKONTROLS4
	},
145 146 147 148 149
	{
		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
		.idProduct =    USB_PID_TRAKTORAUDIO2
	},
150 151 152 153 154
	{
		.match_flags =  USB_DEVICE_ID_MATCH_DEVICE,
		.idVendor =     USB_VID_NATIVEINSTRUMENTS,
		.idProduct =    USB_PID_MASCHINECONTROLLER
	},
155 156 157 158 159 160
	{ /* terminator */ }
};

static void usb_ep1_command_reply_dispatch (struct urb* urb)
{
	int ret;
161
	struct device *dev = &urb->dev->dev;
162
	struct snd_usb_caiaqdev *cdev = urb->context;
163 164
	unsigned char *buf = urb->transfer_buffer;

165
	if (urb->status || !cdev) {
166
		dev_warn(dev, "received EP1 urb->status = %i\n", urb->status);
167 168 169 170 171
		return;
	}

	switch(buf[0]) {
	case EP1_CMD_GET_DEVICE_INFO:
172 173
	 	memcpy(&cdev->spec, buf+1, sizeof(struct caiaq_device_spec));
		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 177 178 179 180 181 182 183 184
			cdev->spec.fw_version,
			cdev->spec.num_analog_audio_in,
			cdev->spec.num_analog_audio_out,
			cdev->spec.num_midi_in,
			cdev->spec.num_midi_out,
			cdev->spec.data_alignment);

		cdev->spec_received++;
		wake_up(&cdev->ep1_wait_queue);
185 186
		break;
	case EP1_CMD_AUDIO_PARAMS:
187 188
		cdev->audio_parm_answer = buf[1];
		wake_up(&cdev->ep1_wait_queue);
189 190
		break;
	case EP1_CMD_MIDI_READ:
191
		snd_usb_caiaq_midi_handle_input(cdev, buf[1], buf + 3, buf[2]);
192
		break;
193
	case EP1_CMD_READ_IO:
194
		if (cdev->chip.usb_id ==
195
			USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) {
196 197 198 199
			if (urb->actual_length > sizeof(cdev->control_state))
				urb->actual_length = sizeof(cdev->control_state);
			memcpy(cdev->control_state, buf + 1, urb->actual_length);
			wake_up(&cdev->ep1_wait_queue);
200 201
			break;
		}
202 203 204
#ifdef CONFIG_SND_USB_CAIAQ_INPUT
	case EP1_CMD_READ_ERP:
	case EP1_CMD_READ_ANALOG:
205
		snd_usb_caiaq_input_dispatch(cdev, buf, urb->actual_length);
206
#endif
207
		break;
208 209
	}

210 211
	cdev->ep1_in_urb.actual_length = 0;
	ret = usb_submit_urb(&cdev->ep1_in_urb, GFP_ATOMIC);
212
	if (ret < 0)
213
		dev_err(dev, "unable to submit urb. OOM!?\n");
214 215
}

216
int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *cdev,
217 218 219
			       unsigned char command,
			       const unsigned char *buffer,
			       int len)
220 221
{
	int actual_len;
222
	struct usb_device *usb_dev = cdev->chip.dev;
223 224 225 226 227 228 229 230

	if (!usb_dev)
		return -EIO;

	if (len > EP1_BUFSIZE - 1)
		len = EP1_BUFSIZE - 1;

	if (buffer && len > 0)
231
		memcpy(cdev->ep1_out_buf+1, buffer, len);
232

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);
236 237
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
int snd_usb_caiaq_send_command_bank(struct snd_usb_caiaqdev *cdev,
			       unsigned char command,
			       unsigned char bank,
			       const unsigned char *buffer,
			       int len)
{
	int actual_len;
	struct usb_device *usb_dev = cdev->chip.dev;

	if (!usb_dev)
		return -EIO;

	if (len > EP1_BUFSIZE - 2)
		len = EP1_BUFSIZE - 2;

	if (buffer && len > 0)
		memcpy(cdev->ep1_out_buf+2, buffer, len);

	cdev->ep1_out_buf[0] = command;
	cdev->ep1_out_buf[1] = bank;

	return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
			   cdev->ep1_out_buf, len+2, &actual_len, 200);
}

263
int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *cdev,
264 265 266 267
		   		    int rate, int depth, int bpp)
{
	int ret;
	char tmp[5];
268
	struct device *dev = caiaqdev_to_dev(cdev);
269

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
	switch (rate) {
	case 44100:	tmp[0] = SAMPLERATE_44100;   break;
	case 48000:	tmp[0] = SAMPLERATE_48000;   break;
	case 88200:	tmp[0] = SAMPLERATE_88200;   break;
	case 96000:	tmp[0] = SAMPLERATE_96000;   break;
	case 192000:	tmp[0] = SAMPLERATE_192000;  break;
	default:	return -EINVAL;
	}

	switch (depth) {
	case 16:	tmp[1] = DEPTH_16;   break;
	case 24:	tmp[1] = DEPTH_24;   break;
	default:	return -EINVAL;
	}

	tmp[2] = bpp & 0xff;
	tmp[3] = bpp >> 8;
	tmp[4] = 1; /* packets per microframe */

289
	dev_dbg(dev, "setting audio params: %d Hz, %d bits, %d bpp\n",
290 291
		rate, depth, bpp);

292 293
	cdev->audio_parm_answer = -1;
	ret = snd_usb_caiaq_send_command(cdev, EP1_CMD_AUDIO_PARAMS,
294
					 tmp, sizeof(tmp));
295 296 297

	if (ret)
		return ret;
298

299 300
	if (!wait_event_timeout(cdev->ep1_wait_queue,
	    cdev->audio_parm_answer >= 0, HZ))
301
		return -EPIPE;
302

303
	if (cdev->audio_parm_answer != 1)
304
		dev_dbg(dev, "unable to set the device's audio params\n");
305
	else
306
		cdev->bpp = bpp;
307

308
	return cdev->audio_parm_answer == 1 ? 0 : -EINVAL;
309 310
}

311
int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev *cdev,
312
			       int digital, int analog, int erp)
313 314
{
	char tmp[3] = { digital, analog, erp };
315
	return snd_usb_caiaq_send_command(cdev, EP1_CMD_AUTO_MSG,
316
					  tmp, sizeof(tmp));
317 318
}

319
static void setup_card(struct snd_usb_caiaqdev *cdev)
320 321
{
	int ret;
322
	char val[4];
323
	struct device *dev = caiaqdev_to_dev(cdev);
324

325
	/* device-specific startup specials */
326
	switch (cdev->chip.usb_id) {
327 328 329 330 331
	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
		/* RigKontrol2 - display centered dash ('-') */
		val[0] = 0x00;
		val[1] = 0x00;
		val[2] = 0x01;
332
		snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 3);
333
		break;
334 335 336 337 338 339
	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
		/* RigKontrol2 - display two centered dashes ('--') */
		val[0] = 0x00;
		val[1] = 0x40;
		val[2] = 0x40;
		val[3] = 0x00;
340
		snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 4);
341
		break;
342 343 344
	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
		/* Audio Kontrol 1 - make USB-LED stop blinking */
		val[0] = 0x00;
345
		snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 1);
346 347 348
		break;
	case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
		/* Audio 8 DJ - trigger read of current settings */
349 350 351
		cdev->control_state[0] = 0xff;
		snd_usb_caiaq_set_auto_msg(cdev, 1, 0, 0);
		snd_usb_caiaq_send_command(cdev, EP1_CMD_READ_IO, NULL, 0);
352

353 354
		if (!wait_event_timeout(cdev->ep1_wait_queue,
					cdev->control_state[0] != 0xff, HZ))
355 356 357
			return;

		/* fix up some defaults */
358 359 360 361 362 363 364 365
		if ((cdev->control_state[1] != 2) ||
		    (cdev->control_state[2] != 3) ||
		    (cdev->control_state[4] != 2)) {
			cdev->control_state[1] = 2;
			cdev->control_state[2] = 3;
			cdev->control_state[4] = 2;
			snd_usb_caiaq_send_command(cdev,
				EP1_CMD_WRITE_IO, cdev->control_state, 6);
366 367
		}

368 369
		break;
	}
370

371 372 373 374 375
	if (cdev->spec.num_analog_audio_out +
	    cdev->spec.num_analog_audio_in +
	    cdev->spec.num_digital_audio_out +
	    cdev->spec.num_digital_audio_in > 0) {
		ret = snd_usb_caiaq_audio_init(cdev);
376
		if (ret < 0)
377
			dev_err(dev, "Unable to set up audio system (ret=%d)\n", ret);
378
	}
379

380 381 382
	if (cdev->spec.num_midi_in +
	    cdev->spec.num_midi_out > 0) {
		ret = snd_usb_caiaq_midi_init(cdev);
383
		if (ret < 0)
384
			dev_err(dev, "Unable to set up MIDI system (ret=%d)\n", ret);
385
	}
386 387

#ifdef CONFIG_SND_USB_CAIAQ_INPUT
388
	ret = snd_usb_caiaq_input_init(cdev);
389
	if (ret < 0)
390
		dev_err(dev, "Unable to set up input system (ret=%d)\n", ret);
391 392 393
#endif

	/* finally, register the card and all its sub-instances */
394
	ret = snd_card_register(cdev->chip.card);
395
	if (ret < 0) {
396
		dev_err(dev, "snd_card_register() returned %d\n", ret);
397
		snd_card_free(cdev->chip.card);
398
	}
399

400
	ret = snd_usb_caiaq_control_init(cdev);
401
	if (ret < 0)
402
		dev_err(dev, "Unable to set up control system (ret=%d)\n", ret);
403 404
}

405 406 407
static int create_card(struct usb_device *usb_dev,
		       struct usb_interface *intf,
		       struct snd_card **cardp)
408 409
{
	int devnum;
410
	int err;
411
	struct snd_card *card;
412
	struct snd_usb_caiaqdev *cdev;
413 414

	for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
415
		if (enable[devnum])
416 417 418
			break;

	if (devnum >= SNDRV_CARDS)
419
		return -ENODEV;
420

421
	err = snd_card_create(index[devnum], id[devnum], THIS_MODULE,
422 423
			      sizeof(struct snd_usb_caiaqdev), &card);
	if (err < 0)
424
		return err;
425

426 427 428 429
	cdev = caiaqdev(card);
	cdev->chip.dev = usb_dev;
	cdev->chip.card = card;
	cdev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor),
A
Al Viro 已提交
430
				  le16_to_cpu(usb_dev->descriptor.idProduct));
431
	spin_lock_init(&cdev->spinlock);
432
	snd_card_set_dev(card, &intf->dev);
433

434 435
	*cardp = card;
	return 0;
436 437
}

438
static int init_card(struct snd_usb_caiaqdev *cdev)
439
{
440
	char *c, usbpath[32];
441 442
	struct usb_device *usb_dev = cdev->chip.dev;
	struct snd_card *card = cdev->chip.card;
443
	struct device *dev = caiaqdev_to_dev(cdev);
444
	int err, len;
445

446
	if (usb_set_interface(usb_dev, 0, 1) != 0) {
447
		dev_err(dev, "can't set alt interface.\n");
448 449 450
		return -EIO;
	}

451 452
	usb_init_urb(&cdev->ep1_in_urb);
	usb_init_urb(&cdev->midi_out_urb);
453

454
	usb_fill_bulk_urb(&cdev->ep1_in_urb, usb_dev,
455
			  usb_rcvbulkpipe(usb_dev, 0x1),
456 457
			  cdev->ep1_in_buf, EP1_BUFSIZE,
			  usb_ep1_command_reply_dispatch, cdev);
458

459
	usb_fill_bulk_urb(&cdev->midi_out_urb, usb_dev,
460
			  usb_sndbulkpipe(usb_dev, 0x1),
461 462
			  cdev->midi_out_buf, EP1_BUFSIZE,
			  snd_usb_caiaq_midi_output_done, cdev);
463

464 465
	init_waitqueue_head(&cdev->ep1_wait_queue);
	init_waitqueue_head(&cdev->prepare_wait_queue);
466

467
	if (usb_submit_urb(&cdev->ep1_in_urb, GFP_KERNEL) != 0)
468 469
		return -EIO;

470
	err = snd_usb_caiaq_send_command(cdev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
471 472 473
	if (err)
		return err;

474
	if (!wait_event_timeout(cdev->ep1_wait_queue, cdev->spec_received, HZ))
475 476 477
		return -ENODEV;

	usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
478
		   cdev->vendor_name, CAIAQ_USB_STR_LEN);
479

480
	usb_string(usb_dev, usb_dev->descriptor.iProduct,
481
		   cdev->product_name, CAIAQ_USB_STR_LEN);
482

D
Daniel Mack 已提交
483
	strlcpy(card->driver, MODNAME, sizeof(card->driver));
484 485
	strlcpy(card->shortname, cdev->product_name, sizeof(card->shortname));
	strlcpy(card->mixername, cdev->product_name, sizeof(card->mixername));
486

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
	/* if the id was not passed as module option, fill it with a shortened
	 * version of the product string which does not contain any
	 * whitespaces */

	if (*card->id == '\0') {
		char id[sizeof(card->id)];

		memset(id, 0, sizeof(id));

		for (c = card->shortname, len = 0;
			*c && len < sizeof(card->id); c++)
			if (*c != ' ')
				id[len++] = *c;

		snd_card_set_id(card, id);
	}

504
	usb_make_path(usb_dev, usbpath, sizeof(usbpath));
505
	snprintf(card->longname, sizeof(card->longname), "%s %s (%s)",
506
		       cdev->vendor_name, cdev->product_name, usbpath);
507

508
	setup_card(cdev);
509 510 511
	return 0;
}

512
static int snd_probe(struct usb_interface *intf,
513 514 515
		     const struct usb_device_id *id)
{
	int ret;
516
	struct snd_card *card = NULL;
517
	struct usb_device *usb_dev = interface_to_usbdev(intf);
518

519
	ret = create_card(usb_dev, intf, &card);
520

521 522
	if (ret < 0)
		return ret;
523

J
Julia Lawall 已提交
524
	usb_set_intfdata(intf, card);
525 526
	ret = init_card(caiaqdev(card));
	if (ret < 0) {
527
		dev_err(&usb_dev->dev, "unable to init card! (ret=%d)\n", ret);
528 529 530
		snd_card_free(card);
		return ret;
	}
531

532 533 534 535 536
	return 0;
}

static void snd_disconnect(struct usb_interface *intf)
{
J
Julia Lawall 已提交
537
	struct snd_card *card = usb_get_intfdata(intf);
538 539
	struct device *dev = intf->usb_dev;
	struct snd_usb_caiaqdev *cdev;
540 541 542 543

	if (!card)
		return;

544
	cdev = caiaqdev(card);
545 546
	dev_dbg(dev, "%s(%p)\n", __func__, intf);

547 548 549
	snd_card_disconnect(card);

#ifdef CONFIG_SND_USB_CAIAQ_INPUT
550
	snd_usb_caiaq_input_free(cdev);
551
#endif
552
	snd_usb_caiaq_audio_free(cdev);
553

554 555
	usb_kill_urb(&cdev->ep1_in_urb);
	usb_kill_urb(&cdev->midi_out_urb);
556

557 558 559 560 561 562 563 564 565 566 567 568 569
	snd_card_free(card);
	usb_reset_device(interface_to_usbdev(intf));
}


MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
static struct usb_driver snd_usb_driver = {
	.name 		= MODNAME,
	.probe 		= snd_probe,
	.disconnect	= snd_disconnect,
	.id_table 	= snd_usb_id_table,
};

570
module_usb_driver(snd_usb_driver);