ALSA: snd-usb-caiaq: Fix NULL dereference in input.c
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / sound / usb / 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
523f1dce
DM
22#include <linux/moduleparam.h>
23#include <linux/interrupt.h>
e431cf45
DM
24#include <linux/module.h>
25#include <linux/init.h>
5a0e3ad6 26#include <linux/gfp.h>
523f1dce 27#include <linux/usb.h>
523f1dce 28#include <sound/initval.h>
e431cf45 29#include <sound/core.h>
523f1dce 30#include <sound/pcm.h>
523f1dce 31
936e7d03
DM
32#include "device.h"
33#include "audio.h"
34#include "midi.h"
35#include "control.h"
36#include "input.h"
523f1dce
DM
37
38MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
6008fd5a 39MODULE_DESCRIPTION("caiaq USB audio");
523f1dce
DM
40MODULE_LICENSE("GPL");
41MODULE_SUPPORTED_DEVICE("{{Native Instruments, RigKontrol2},"
ad1e34b5 42 "{Native Instruments, RigKontrol3},"
523f1dce 43 "{Native Instruments, Kore Controller},"
7829d0ec 44 "{Native Instruments, Kore Controller 2},"
f3e9d5d1 45 "{Native Instruments, Audio Kontrol 1},"
b30c4947 46 "{Native Instruments, Audio 2 DJ},"
2165592b 47 "{Native Instruments, Audio 4 DJ},"
f3e9d5d1 48 "{Native Instruments, Audio 8 DJ},"
df8d81a3 49 "{Native Instruments, Traktor Audio 2},"
2165592b 50 "{Native Instruments, Session I/O},"
6da7a2aa 51 "{Native Instruments, GuitarRig mobile}"
15c5ab60
DM
52 "{Native Instruments, Traktor Kontrol X1}"
53 "{Native Instruments, Traktor Kontrol S4}");
523f1dce
DM
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,
9318dce5 87 .idProduct = USB_PID_RIGKONTROL2
523f1dce 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 },
f3e9d5d1
DM
114 {
115 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
116 .idVendor = USB_VID_NATIVEINSTRUMENTS,
117 .idProduct = USB_PID_SESSIONIO
118 },
2165592b
DM
119 {
120 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
121 .idVendor = USB_VID_NATIVEINSTRUMENTS,
122 .idProduct = USB_PID_GUITARRIGMOBILE
123 },
124 {
125 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
126 .idVendor = USB_VID_NATIVEINSTRUMENTS,
127 .idProduct = USB_PID_AUDIO4DJ
128 },
b30c4947
DM
129 {
130 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
131 .idVendor = USB_VID_NATIVEINSTRUMENTS,
132 .idProduct = USB_PID_AUDIO2DJ
133 },
6da7a2aa
DM
134 {
135 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
136 .idVendor = USB_VID_NATIVEINSTRUMENTS,
137 .idProduct = USB_PID_TRAKTORKONTROLX1
138 },
15c5ab60
DM
139 {
140 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
141 .idVendor = USB_VID_NATIVEINSTRUMENTS,
142 .idProduct = USB_PID_TRAKTORKONTROLS4
143 },
df8d81a3
DM
144 {
145 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
146 .idVendor = USB_VID_NATIVEINSTRUMENTS,
147 .idProduct = USB_PID_TRAKTORAUDIO2
148 },
523f1dce
DM
149 { /* terminator */ }
150};
151
152static void usb_ep1_command_reply_dispatch (struct urb* urb)
153{
154 int ret;
155 struct snd_usb_caiaqdev *dev = urb->context;
156 unsigned char *buf = urb->transfer_buffer;
157
158 if (urb->status || !dev) {
159 log("received EP1 urb->status = %i\n", urb->status);
160 return;
161 }
162
163 switch(buf[0]) {
164 case EP1_CMD_GET_DEVICE_INFO:
165 memcpy(&dev->spec, buf+1, sizeof(struct caiaq_device_spec));
166 dev->spec.fw_version = le16_to_cpu(dev->spec.fw_version);
167 debug("device spec (firmware %d): audio: %d in, %d out, "
168 "MIDI: %d in, %d out, data alignment %d\n",
169 dev->spec.fw_version,
170 dev->spec.num_analog_audio_in,
171 dev->spec.num_analog_audio_out,
172 dev->spec.num_midi_in,
173 dev->spec.num_midi_out,
174 dev->spec.data_alignment);
175
176 dev->spec_received++;
177 wake_up(&dev->ep1_wait_queue);
178 break;
179 case EP1_CMD_AUDIO_PARAMS:
180 dev->audio_parm_answer = buf[1];
181 wake_up(&dev->ep1_wait_queue);
182 break;
183 case EP1_CMD_MIDI_READ:
184 snd_usb_caiaq_midi_handle_input(dev, buf[1], buf + 3, buf[2]);
185 break;
8e3cd08e
DM
186 case EP1_CMD_READ_IO:
187 if (dev->chip.usb_id ==
188 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) {
189 if (urb->actual_length > sizeof(dev->control_state))
190 urb->actual_length = sizeof(dev->control_state);
191 memcpy(dev->control_state, buf + 1, urb->actual_length);
192 wake_up(&dev->ep1_wait_queue);
193 break;
194 }
523f1dce
DM
195#ifdef CONFIG_SND_USB_CAIAQ_INPUT
196 case EP1_CMD_READ_ERP:
197 case EP1_CMD_READ_ANALOG:
523f1dce 198 snd_usb_caiaq_input_dispatch(dev, buf, urb->actual_length);
523f1dce 199#endif
8e3cd08e 200 break;
523f1dce
DM
201 }
202
203 dev->ep1_in_urb.actual_length = 0;
204 ret = usb_submit_urb(&dev->ep1_in_urb, GFP_ATOMIC);
205 if (ret < 0)
206 log("unable to submit urb. OOM!?\n");
207}
208
8e3cd08e
DM
209int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *dev,
210 unsigned char command,
211 const unsigned char *buffer,
212 int len)
523f1dce
DM
213{
214 int actual_len;
215 struct usb_device *usb_dev = dev->chip.dev;
216
217 if (!usb_dev)
218 return -EIO;
219
220 if (len > EP1_BUFSIZE - 1)
221 len = EP1_BUFSIZE - 1;
222
223 if (buffer && len > 0)
224 memcpy(dev->ep1_out_buf+1, buffer, len);
9318dce5 225
523f1dce
DM
226 dev->ep1_out_buf[0] = command;
227 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
228 dev->ep1_out_buf, len+1, &actual_len, 200);
229}
230
231int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *dev,
232 int rate, int depth, int bpp)
233{
234 int ret;
235 char tmp[5];
9318dce5 236
523f1dce
DM
237 switch (rate) {
238 case 44100: tmp[0] = SAMPLERATE_44100; break;
239 case 48000: tmp[0] = SAMPLERATE_48000; break;
240 case 88200: tmp[0] = SAMPLERATE_88200; break;
241 case 96000: tmp[0] = SAMPLERATE_96000; break;
242 case 192000: tmp[0] = SAMPLERATE_192000; break;
243 default: return -EINVAL;
244 }
245
246 switch (depth) {
247 case 16: tmp[1] = DEPTH_16; break;
248 case 24: tmp[1] = DEPTH_24; break;
249 default: return -EINVAL;
250 }
251
252 tmp[2] = bpp & 0xff;
253 tmp[3] = bpp >> 8;
254 tmp[4] = 1; /* packets per microframe */
255
256 debug("setting audio params: %d Hz, %d bits, %d bpp\n",
257 rate, depth, bpp);
258
259 dev->audio_parm_answer = -1;
8e3cd08e
DM
260 ret = snd_usb_caiaq_send_command(dev, EP1_CMD_AUDIO_PARAMS,
261 tmp, sizeof(tmp));
523f1dce
DM
262
263 if (ret)
264 return ret;
9318dce5
DM
265
266 if (!wait_event_timeout(dev->ep1_wait_queue,
523f1dce
DM
267 dev->audio_parm_answer >= 0, HZ))
268 return -EPIPE;
9318dce5
DM
269
270 if (dev->audio_parm_answer != 1)
523f1dce 271 debug("unable to set the device's audio params\n");
9311c9b4
DM
272 else
273 dev->bpp = bpp;
523f1dce
DM
274
275 return dev->audio_parm_answer == 1 ? 0 : -EINVAL;
276}
277
9318dce5
DM
278int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev *dev,
279 int digital, int analog, int erp)
523f1dce
DM
280{
281 char tmp[3] = { digital, analog, erp };
8e3cd08e
DM
282 return snd_usb_caiaq_send_command(dev, EP1_CMD_AUTO_MSG,
283 tmp, sizeof(tmp));
523f1dce
DM
284}
285
c598195a 286static void __devinit setup_card(struct snd_usb_caiaqdev *dev)
523f1dce
DM
287{
288 int ret;
ad1e34b5 289 char val[4];
9318dce5 290
523f1dce
DM
291 /* device-specific startup specials */
292 switch (dev->chip.usb_id) {
293 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
294 /* RigKontrol2 - display centered dash ('-') */
295 val[0] = 0x00;
296 val[1] = 0x00;
297 val[2] = 0x01;
8e3cd08e 298 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 3);
523f1dce 299 break;
ad1e34b5
DM
300 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
301 /* RigKontrol2 - display two centered dashes ('--') */
302 val[0] = 0x00;
303 val[1] = 0x40;
304 val[2] = 0x40;
305 val[3] = 0x00;
8e3cd08e 306 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 4);
ad1e34b5 307 break;
523f1dce
DM
308 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
309 /* Audio Kontrol 1 - make USB-LED stop blinking */
310 val[0] = 0x00;
8e3cd08e
DM
311 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 1);
312 break;
313 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
314 /* Audio 8 DJ - trigger read of current settings */
315 dev->control_state[0] = 0xff;
316 snd_usb_caiaq_set_auto_msg(dev, 1, 0, 0);
317 snd_usb_caiaq_send_command(dev, EP1_CMD_READ_IO, NULL, 0);
318
319 if (!wait_event_timeout(dev->ep1_wait_queue,
320 dev->control_state[0] != 0xff, HZ))
321 return;
322
323 /* fix up some defaults */
324 if ((dev->control_state[1] != 2) ||
325 (dev->control_state[2] != 3) ||
326 (dev->control_state[4] != 2)) {
327 dev->control_state[1] = 2;
328 dev->control_state[2] = 3;
329 dev->control_state[4] = 2;
330 snd_usb_caiaq_send_command(dev,
331 EP1_CMD_WRITE_IO, dev->control_state, 6);
332 }
333
523f1dce
DM
334 break;
335 }
9318dce5 336
7829d0ec
DM
337 if (dev->spec.num_analog_audio_out +
338 dev->spec.num_analog_audio_in +
339 dev->spec.num_digital_audio_out +
340 dev->spec.num_digital_audio_in > 0) {
341 ret = snd_usb_caiaq_audio_init(dev);
342 if (ret < 0)
343 log("Unable to set up audio system (ret=%d)\n", ret);
344 }
9318dce5 345
7829d0ec
DM
346 if (dev->spec.num_midi_in +
347 dev->spec.num_midi_out > 0) {
348 ret = snd_usb_caiaq_midi_init(dev);
349 if (ret < 0)
350 log("Unable to set up MIDI system (ret=%d)\n", ret);
351 }
523f1dce
DM
352
353#ifdef CONFIG_SND_USB_CAIAQ_INPUT
354 ret = snd_usb_caiaq_input_init(dev);
355 if (ret < 0)
356 log("Unable to set up input system (ret=%d)\n", ret);
357#endif
358
359 /* finally, register the card and all its sub-instances */
360 ret = snd_card_register(dev->chip.card);
361 if (ret < 0) {
362 log("snd_card_register() returned %d\n", ret);
363 snd_card_free(dev->chip.card);
364 }
8e3cd08e
DM
365
366 ret = snd_usb_caiaq_control_init(dev);
367 if (ret < 0)
368 log("Unable to set up control system (ret=%d)\n", ret);
523f1dce
DM
369}
370
563c2bf5
DM
371static int create_card(struct usb_device *usb_dev,
372 struct usb_interface *intf,
373 struct snd_card **cardp)
523f1dce
DM
374{
375 int devnum;
bd7dd77c 376 int err;
523f1dce
DM
377 struct snd_card *card;
378 struct snd_usb_caiaqdev *dev;
379
380 for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
381 if (enable[devnum] && !snd_card_used[devnum])
382 break;
383
384 if (devnum >= SNDRV_CARDS)
51721f70 385 return -ENODEV;
523f1dce 386
9318dce5 387 err = snd_card_create(index[devnum], id[devnum], THIS_MODULE,
bd7dd77c
TI
388 sizeof(struct snd_usb_caiaqdev), &card);
389 if (err < 0)
51721f70 390 return err;
523f1dce
DM
391
392 dev = caiaqdev(card);
393 dev->chip.dev = usb_dev;
394 dev->chip.card = card;
8c5330a5
AV
395 dev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor),
396 le16_to_cpu(usb_dev->descriptor.idProduct));
523f1dce 397 spin_lock_init(&dev->spinlock);
563c2bf5 398 snd_card_set_dev(card, &intf->dev);
523f1dce 399
51721f70
TI
400 *cardp = card;
401 return 0;
523f1dce
DM
402}
403
c598195a 404static int __devinit init_card(struct snd_usb_caiaqdev *dev)
523f1dce 405{
bafeee5b 406 char *c, usbpath[32];
523f1dce
DM
407 struct usb_device *usb_dev = dev->chip.dev;
408 struct snd_card *card = dev->chip.card;
bafeee5b 409 int err, len;
9318dce5 410
523f1dce
DM
411 if (usb_set_interface(usb_dev, 0, 1) != 0) {
412 log("can't set alt interface.\n");
413 return -EIO;
414 }
415
416 usb_init_urb(&dev->ep1_in_urb);
417 usb_init_urb(&dev->midi_out_urb);
418
9318dce5 419 usb_fill_bulk_urb(&dev->ep1_in_urb, usb_dev,
523f1dce 420 usb_rcvbulkpipe(usb_dev, 0x1),
9318dce5 421 dev->ep1_in_buf, EP1_BUFSIZE,
523f1dce
DM
422 usb_ep1_command_reply_dispatch, dev);
423
9318dce5 424 usb_fill_bulk_urb(&dev->midi_out_urb, usb_dev,
523f1dce 425 usb_sndbulkpipe(usb_dev, 0x1),
9318dce5 426 dev->midi_out_buf, EP1_BUFSIZE,
523f1dce 427 snd_usb_caiaq_midi_output_done, dev);
9318dce5 428
523f1dce
DM
429 init_waitqueue_head(&dev->ep1_wait_queue);
430 init_waitqueue_head(&dev->prepare_wait_queue);
9318dce5 431
523f1dce
DM
432 if (usb_submit_urb(&dev->ep1_in_urb, GFP_KERNEL) != 0)
433 return -EIO;
434
8e3cd08e 435 err = snd_usb_caiaq_send_command(dev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
523f1dce
DM
436 if (err)
437 return err;
438
439 if (!wait_event_timeout(dev->ep1_wait_queue, dev->spec_received, HZ))
440 return -ENODEV;
441
442 usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
443 dev->vendor_name, CAIAQ_USB_STR_LEN);
9318dce5 444
523f1dce
DM
445 usb_string(usb_dev, usb_dev->descriptor.iProduct,
446 dev->product_name, CAIAQ_USB_STR_LEN);
9318dce5 447
d3873a1b
DM
448 strlcpy(card->driver, MODNAME, sizeof(card->driver));
449 strlcpy(card->shortname, dev->product_name, sizeof(card->shortname));
955f2d96 450 strlcpy(card->mixername, dev->product_name, sizeof(card->mixername));
523f1dce 451
bafeee5b
DM
452 /* if the id was not passed as module option, fill it with a shortened
453 * version of the product string which does not contain any
454 * whitespaces */
455
456 if (*card->id == '\0') {
457 char id[sizeof(card->id)];
458
459 memset(id, 0, sizeof(id));
460
461 for (c = card->shortname, len = 0;
462 *c && len < sizeof(card->id); c++)
463 if (*c != ' ')
464 id[len++] = *c;
465
466 snd_card_set_id(card, id);
467 }
468
1a1df6f0
DM
469 usb_make_path(usb_dev, usbpath, sizeof(usbpath));
470 snprintf(card->longname, sizeof(card->longname),
471 "%s %s (%s)",
472 dev->vendor_name, dev->product_name, usbpath);
523f1dce 473
523f1dce
DM
474 setup_card(dev);
475 return 0;
476}
477
9318dce5 478static int __devinit snd_probe(struct usb_interface *intf,
523f1dce
DM
479 const struct usb_device_id *id)
480{
481 int ret;
482 struct snd_card *card;
483 struct usb_device *device = interface_to_usbdev(intf);
9318dce5 484
563c2bf5 485 ret = create_card(device, intf, &card);
9318dce5 486
51721f70
TI
487 if (ret < 0)
488 return ret;
9318dce5 489
f4e9749f 490 usb_set_intfdata(intf, card);
523f1dce
DM
491 ret = init_card(caiaqdev(card));
492 if (ret < 0) {
493 log("unable to init card! (ret=%d)\n", ret);
494 snd_card_free(card);
495 return ret;
496 }
9318dce5 497
523f1dce
DM
498 return 0;
499}
500
501static void snd_disconnect(struct usb_interface *intf)
502{
503 struct snd_usb_caiaqdev *dev;
f4e9749f 504 struct snd_card *card = usb_get_intfdata(intf);
523f1dce 505
8d048841 506 debug("%s(%p)\n", __func__, intf);
523f1dce
DM
507
508 if (!card)
509 return;
510
511 dev = caiaqdev(card);
512 snd_card_disconnect(card);
513
514#ifdef CONFIG_SND_USB_CAIAQ_INPUT
515 snd_usb_caiaq_input_free(dev);
516#endif
517 snd_usb_caiaq_audio_free(dev);
9318dce5 518
523f1dce
DM
519 usb_kill_urb(&dev->ep1_in_urb);
520 usb_kill_urb(&dev->midi_out_urb);
9318dce5 521
523f1dce
DM
522 snd_card_free(card);
523 usb_reset_device(interface_to_usbdev(intf));
524}
525
526
527MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
528static struct usb_driver snd_usb_driver = {
529 .name = MODNAME,
530 .probe = snd_probe,
531 .disconnect = snd_disconnect,
532 .id_table = snd_usb_id_table,
533};
534
535static int __init snd_module_init(void)
536{
537 return usb_register(&snd_usb_driver);
538}
539
540static void __exit snd_module_exit(void)
541{
542 usb_deregister(&snd_usb_driver);
543}
544
545module_init(snd_module_init)
546module_exit(snd_module_exit)
547