]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - sound/usb/caiaq/caiaq-device.c
[ALSA] usb-caiaq - add support for Kore controller 2
[mirror_ubuntu-zesty-kernel.git] / sound / usb / caiaq / caiaq-device.c
CommitLineData
523f1dce
DM
1/*
2 * caiaq.c: ALSA driver for caiaq/NativeInstruments devices
3 *
4 * Copyright (c) 2007 Daniel Mack <daniel@caiaq.de>
5 * Karsten Wiese <fzu@wemgehoertderstaat.de>
6 *
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.
11 *
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.
16 *
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
20*/
21
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/driver.h>
30#include <sound/core.h>
31#include <sound/initval.h>
32#include <sound/pcm.h>
33#include <sound/rawmidi.h>
8e3cd08e 34#include <sound/control.h>
523f1dce
DM
35
36#include "caiaq-device.h"
37#include "caiaq-audio.h"
38#include "caiaq-midi.h"
8e3cd08e 39#include "caiaq-control.h"
523f1dce
DM
40
41#ifdef CONFIG_SND_USB_CAIAQ_INPUT
42#include "caiaq-input.h"
43#endif
44
45MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
7829d0ec 46MODULE_DESCRIPTION("caiaq USB audio, version 1.3.1");
523f1dce
DM
47MODULE_LICENSE("GPL");
48MODULE_SUPPORTED_DEVICE("{{Native Instruments, RigKontrol2},"
ad1e34b5 49 "{Native Instruments, RigKontrol3},"
523f1dce 50 "{Native Instruments, Kore Controller},"
7829d0ec 51 "{Native Instruments, Kore Controller 2},"
523f1dce
DM
52 "{Native Instruments, Audio Kontrol 1}"
53 "{Native Instruments, Audio 8 DJ}}");
54
55static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
56static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
57static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
58static int snd_card_used[SNDRV_CARDS];
59
60module_param_array(index, int, NULL, 0444);
61MODULE_PARM_DESC(index, "Index value for the caiaq sound device");
62module_param_array(id, charp, NULL, 0444);
63MODULE_PARM_DESC(id, "ID string for the caiaq soundcard.");
64module_param_array(enable, bool, NULL, 0444);
65MODULE_PARM_DESC(enable, "Enable the caiaq soundcard.");
66
67enum {
68 SAMPLERATE_44100 = 0,
69 SAMPLERATE_48000 = 1,
70 SAMPLERATE_96000 = 2,
71 SAMPLERATE_192000 = 3,
72 SAMPLERATE_88200 = 4,
73 SAMPLERATE_INVALID = 0xff
74};
75
76enum {
77 DEPTH_NONE = 0,
78 DEPTH_16 = 1,
79 DEPTH_24 = 2,
80 DEPTH_32 = 3
81};
82
83static struct usb_device_id snd_usb_id_table[] = {
84 {
85 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
86 .idVendor = USB_VID_NATIVEINSTRUMENTS,
87 .idProduct = USB_PID_RIGKONTROL2
88 },
ad1e34b5
DM
89 {
90 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
91 .idVendor = USB_VID_NATIVEINSTRUMENTS,
92 .idProduct = USB_PID_RIGKONTROL3
93 },
523f1dce
DM
94 {
95 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
96 .idVendor = USB_VID_NATIVEINSTRUMENTS,
97 .idProduct = USB_PID_KORECONTROLLER
98 },
7829d0ec
DM
99 {
100 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
101 .idVendor = USB_VID_NATIVEINSTRUMENTS,
102 .idProduct = USB_PID_KORECONTROLLER2
103 },
523f1dce
DM
104 {
105 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
106 .idVendor = USB_VID_NATIVEINSTRUMENTS,
107 .idProduct = USB_PID_AK1
108 },
109 {
110 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
111 .idVendor = USB_VID_NATIVEINSTRUMENTS,
112 .idProduct = USB_PID_AUDIO8DJ
113 },
114 { /* terminator */ }
115};
116
117static void usb_ep1_command_reply_dispatch (struct urb* urb)
118{
119 int ret;
120 struct snd_usb_caiaqdev *dev = urb->context;
121 unsigned char *buf = urb->transfer_buffer;
122
123 if (urb->status || !dev) {
124 log("received EP1 urb->status = %i\n", urb->status);
125 return;
126 }
127
128 switch(buf[0]) {
129 case EP1_CMD_GET_DEVICE_INFO:
130 memcpy(&dev->spec, buf+1, sizeof(struct caiaq_device_spec));
131 dev->spec.fw_version = le16_to_cpu(dev->spec.fw_version);
132 debug("device spec (firmware %d): audio: %d in, %d out, "
133 "MIDI: %d in, %d out, data alignment %d\n",
134 dev->spec.fw_version,
135 dev->spec.num_analog_audio_in,
136 dev->spec.num_analog_audio_out,
137 dev->spec.num_midi_in,
138 dev->spec.num_midi_out,
139 dev->spec.data_alignment);
140
141 dev->spec_received++;
142 wake_up(&dev->ep1_wait_queue);
143 break;
144 case EP1_CMD_AUDIO_PARAMS:
145 dev->audio_parm_answer = buf[1];
146 wake_up(&dev->ep1_wait_queue);
147 break;
148 case EP1_CMD_MIDI_READ:
149 snd_usb_caiaq_midi_handle_input(dev, buf[1], buf + 3, buf[2]);
150 break;
8e3cd08e
DM
151 case EP1_CMD_READ_IO:
152 if (dev->chip.usb_id ==
153 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) {
154 if (urb->actual_length > sizeof(dev->control_state))
155 urb->actual_length = sizeof(dev->control_state);
156 memcpy(dev->control_state, buf + 1, urb->actual_length);
157 wake_up(&dev->ep1_wait_queue);
158 break;
159 }
523f1dce
DM
160#ifdef CONFIG_SND_USB_CAIAQ_INPUT
161 case EP1_CMD_READ_ERP:
162 case EP1_CMD_READ_ANALOG:
523f1dce 163 snd_usb_caiaq_input_dispatch(dev, buf, urb->actual_length);
523f1dce 164#endif
8e3cd08e 165 break;
523f1dce
DM
166 }
167
168 dev->ep1_in_urb.actual_length = 0;
169 ret = usb_submit_urb(&dev->ep1_in_urb, GFP_ATOMIC);
170 if (ret < 0)
171 log("unable to submit urb. OOM!?\n");
172}
173
8e3cd08e
DM
174int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *dev,
175 unsigned char command,
176 const unsigned char *buffer,
177 int len)
523f1dce
DM
178{
179 int actual_len;
180 struct usb_device *usb_dev = dev->chip.dev;
181
182 if (!usb_dev)
183 return -EIO;
184
185 if (len > EP1_BUFSIZE - 1)
186 len = EP1_BUFSIZE - 1;
187
188 if (buffer && len > 0)
189 memcpy(dev->ep1_out_buf+1, buffer, len);
190
191 dev->ep1_out_buf[0] = command;
192 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
193 dev->ep1_out_buf, len+1, &actual_len, 200);
194}
195
196int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *dev,
197 int rate, int depth, int bpp)
198{
199 int ret;
200 char tmp[5];
201
202 switch (rate) {
203 case 44100: tmp[0] = SAMPLERATE_44100; break;
204 case 48000: tmp[0] = SAMPLERATE_48000; break;
205 case 88200: tmp[0] = SAMPLERATE_88200; break;
206 case 96000: tmp[0] = SAMPLERATE_96000; break;
207 case 192000: tmp[0] = SAMPLERATE_192000; break;
208 default: return -EINVAL;
209 }
210
211 switch (depth) {
212 case 16: tmp[1] = DEPTH_16; break;
213 case 24: tmp[1] = DEPTH_24; break;
214 default: return -EINVAL;
215 }
216
217 tmp[2] = bpp & 0xff;
218 tmp[3] = bpp >> 8;
219 tmp[4] = 1; /* packets per microframe */
220
221 debug("setting audio params: %d Hz, %d bits, %d bpp\n",
222 rate, depth, bpp);
223
224 dev->audio_parm_answer = -1;
8e3cd08e
DM
225 ret = snd_usb_caiaq_send_command(dev, EP1_CMD_AUDIO_PARAMS,
226 tmp, sizeof(tmp));
523f1dce
DM
227
228 if (ret)
229 return ret;
230
231 if (!wait_event_timeout(dev->ep1_wait_queue,
232 dev->audio_parm_answer >= 0, HZ))
233 return -EPIPE;
234
235 if (dev->audio_parm_answer != 1)
236 debug("unable to set the device's audio params\n");
237
238 return dev->audio_parm_answer == 1 ? 0 : -EINVAL;
239}
240
241int snd_usb_caiaq_set_auto_msg (struct snd_usb_caiaqdev *dev,
242 int digital, int analog, int erp)
243{
244 char tmp[3] = { digital, analog, erp };
8e3cd08e
DM
245 return snd_usb_caiaq_send_command(dev, EP1_CMD_AUTO_MSG,
246 tmp, sizeof(tmp));
523f1dce
DM
247}
248
249static void setup_card(struct snd_usb_caiaqdev *dev)
250{
251 int ret;
ad1e34b5 252 char val[4];
523f1dce
DM
253
254 /* device-specific startup specials */
255 switch (dev->chip.usb_id) {
256 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
257 /* RigKontrol2 - display centered dash ('-') */
258 val[0] = 0x00;
259 val[1] = 0x00;
260 val[2] = 0x01;
8e3cd08e 261 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 3);
523f1dce 262 break;
ad1e34b5
DM
263 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
264 /* RigKontrol2 - display two centered dashes ('--') */
265 val[0] = 0x00;
266 val[1] = 0x40;
267 val[2] = 0x40;
268 val[3] = 0x00;
8e3cd08e 269 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 4);
ad1e34b5 270 break;
523f1dce
DM
271 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
272 /* Audio Kontrol 1 - make USB-LED stop blinking */
273 val[0] = 0x00;
8e3cd08e
DM
274 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 1);
275 break;
276 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
277 /* Audio 8 DJ - trigger read of current settings */
278 dev->control_state[0] = 0xff;
279 snd_usb_caiaq_set_auto_msg(dev, 1, 0, 0);
280 snd_usb_caiaq_send_command(dev, EP1_CMD_READ_IO, NULL, 0);
281
282 if (!wait_event_timeout(dev->ep1_wait_queue,
283 dev->control_state[0] != 0xff, HZ))
284 return;
285
286 /* fix up some defaults */
287 if ((dev->control_state[1] != 2) ||
288 (dev->control_state[2] != 3) ||
289 (dev->control_state[4] != 2)) {
290 dev->control_state[1] = 2;
291 dev->control_state[2] = 3;
292 dev->control_state[4] = 2;
293 snd_usb_caiaq_send_command(dev,
294 EP1_CMD_WRITE_IO, dev->control_state, 6);
295 }
296
523f1dce
DM
297 break;
298 }
299
7829d0ec
DM
300 if (dev->spec.num_analog_audio_out +
301 dev->spec.num_analog_audio_in +
302 dev->spec.num_digital_audio_out +
303 dev->spec.num_digital_audio_in > 0) {
304 ret = snd_usb_caiaq_audio_init(dev);
305 if (ret < 0)
306 log("Unable to set up audio system (ret=%d)\n", ret);
307 }
523f1dce 308
7829d0ec
DM
309 if (dev->spec.num_midi_in +
310 dev->spec.num_midi_out > 0) {
311 ret = snd_usb_caiaq_midi_init(dev);
312 if (ret < 0)
313 log("Unable to set up MIDI system (ret=%d)\n", ret);
314 }
523f1dce
DM
315
316#ifdef CONFIG_SND_USB_CAIAQ_INPUT
317 ret = snd_usb_caiaq_input_init(dev);
318 if (ret < 0)
319 log("Unable to set up input system (ret=%d)\n", ret);
320#endif
321
322 /* finally, register the card and all its sub-instances */
323 ret = snd_card_register(dev->chip.card);
324 if (ret < 0) {
325 log("snd_card_register() returned %d\n", ret);
326 snd_card_free(dev->chip.card);
327 }
8e3cd08e
DM
328
329 ret = snd_usb_caiaq_control_init(dev);
330 if (ret < 0)
331 log("Unable to set up control system (ret=%d)\n", ret);
523f1dce
DM
332}
333
334static struct snd_card* create_card(struct usb_device* usb_dev)
335{
336 int devnum;
337 struct snd_card *card;
338 struct snd_usb_caiaqdev *dev;
339
340 for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
341 if (enable[devnum] && !snd_card_used[devnum])
342 break;
343
344 if (devnum >= SNDRV_CARDS)
345 return NULL;
346
347 card = snd_card_new(index[devnum], id[devnum], THIS_MODULE,
348 sizeof(struct snd_usb_caiaqdev));
349 if (!card)
350 return NULL;
351
352 dev = caiaqdev(card);
353 dev->chip.dev = usb_dev;
354 dev->chip.card = card;
355 dev->chip.usb_id = USB_ID(usb_dev->descriptor.idVendor,
356 usb_dev->descriptor.idProduct);
357 spin_lock_init(&dev->spinlock);
358 snd_card_set_dev(card, &usb_dev->dev);
359
360 return card;
361}
362
363static int init_card(struct snd_usb_caiaqdev *dev)
364{
365 char *c;
366 struct usb_device *usb_dev = dev->chip.dev;
367 struct snd_card *card = dev->chip.card;
368 int err, len;
369
370 if (usb_set_interface(usb_dev, 0, 1) != 0) {
371 log("can't set alt interface.\n");
372 return -EIO;
373 }
374
375 usb_init_urb(&dev->ep1_in_urb);
376 usb_init_urb(&dev->midi_out_urb);
377
378 usb_fill_bulk_urb(&dev->ep1_in_urb, usb_dev,
379 usb_rcvbulkpipe(usb_dev, 0x1),
380 dev->ep1_in_buf, EP1_BUFSIZE,
381 usb_ep1_command_reply_dispatch, dev);
382
383 usb_fill_bulk_urb(&dev->midi_out_urb, usb_dev,
384 usb_sndbulkpipe(usb_dev, 0x1),
385 dev->midi_out_buf, EP1_BUFSIZE,
386 snd_usb_caiaq_midi_output_done, dev);
387
388 init_waitqueue_head(&dev->ep1_wait_queue);
389 init_waitqueue_head(&dev->prepare_wait_queue);
390
391 if (usb_submit_urb(&dev->ep1_in_urb, GFP_KERNEL) != 0)
392 return -EIO;
393
8e3cd08e 394 err = snd_usb_caiaq_send_command(dev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
523f1dce
DM
395 if (err)
396 return err;
397
398 if (!wait_event_timeout(dev->ep1_wait_queue, dev->spec_received, HZ))
399 return -ENODEV;
400
401 usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
402 dev->vendor_name, CAIAQ_USB_STR_LEN);
403
404 usb_string(usb_dev, usb_dev->descriptor.iProduct,
405 dev->product_name, CAIAQ_USB_STR_LEN);
406
407 usb_string(usb_dev, usb_dev->descriptor.iSerialNumber,
408 dev->serial, CAIAQ_USB_STR_LEN);
409
410 /* terminate serial string at first white space occurence */
411 c = strchr(dev->serial, ' ');
412 if (c)
413 *c = '\0';
414
415 strcpy(card->driver, MODNAME);
416 strcpy(card->shortname, dev->product_name);
417
418 len = snprintf(card->longname, sizeof(card->longname),
419 "%s %s (serial %s, ",
420 dev->vendor_name, dev->product_name, dev->serial);
421
422 if (len < sizeof(card->longname) - 2)
423 len += usb_make_path(usb_dev, card->longname + len,
424 sizeof(card->longname) - len);
425
426 card->longname[len++] = ')';
427 card->longname[len] = '\0';
428 setup_card(dev);
429 return 0;
430}
431
432static int snd_probe(struct usb_interface *intf,
433 const struct usb_device_id *id)
434{
435 int ret;
436 struct snd_card *card;
437 struct usb_device *device = interface_to_usbdev(intf);
438
439 card = create_card(device);
440
441 if (!card)
442 return -ENOMEM;
443
444 dev_set_drvdata(&intf->dev, card);
445 ret = init_card(caiaqdev(card));
446 if (ret < 0) {
447 log("unable to init card! (ret=%d)\n", ret);
448 snd_card_free(card);
449 return ret;
450 }
451
452 return 0;
453}
454
455static void snd_disconnect(struct usb_interface *intf)
456{
457 struct snd_usb_caiaqdev *dev;
458 struct snd_card *card = dev_get_drvdata(&intf->dev);
459
460 debug("snd_disconnect(%p)\n", intf);
461
462 if (!card)
463 return;
464
465 dev = caiaqdev(card);
466 snd_card_disconnect(card);
467
468#ifdef CONFIG_SND_USB_CAIAQ_INPUT
469 snd_usb_caiaq_input_free(dev);
470#endif
471 snd_usb_caiaq_audio_free(dev);
472
473 usb_kill_urb(&dev->ep1_in_urb);
474 usb_kill_urb(&dev->midi_out_urb);
475
476 snd_card_free(card);
477 usb_reset_device(interface_to_usbdev(intf));
478}
479
480
481MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
482static struct usb_driver snd_usb_driver = {
483 .name = MODNAME,
484 .probe = snd_probe,
485 .disconnect = snd_disconnect,
486 .id_table = snd_usb_id_table,
487};
488
489static int __init snd_module_init(void)
490{
491 return usb_register(&snd_usb_driver);
492}
493
494static void __exit snd_module_exit(void)
495{
496 usb_deregister(&snd_usb_driver);
497}
498
499module_init(snd_module_init)
500module_exit(snd_module_exit)
501