blob: 45bc4a2dc6f0dc8065aec1a518df9a8bc59c9201 [file] [log] [blame]
Daniel Mack523f1dc2007-03-26 19:11:24 +02001/*
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
Daniel Mack523f1dc2007-03-26 19:11:24 +020022#include <linux/moduleparam.h>
23#include <linux/interrupt.h>
Daniel Macke431cf42009-03-28 21:19:49 +010024#include <linux/module.h>
25#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/gfp.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020027#include <linux/usb.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020028#include <sound/initval.h>
Daniel Macke431cf42009-03-28 21:19:49 +010029#include <sound/core.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020030#include <sound/pcm.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020031
Daniel Mack936e7d02009-04-01 19:05:39 +020032#include "device.h"
33#include "audio.h"
34#include "midi.h"
35#include "control.h"
36#include "input.h"
Daniel Mack523f1dc2007-03-26 19:11:24 +020037
38MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
Daniel Mack6008fd52010-09-10 16:12:34 +080039MODULE_DESCRIPTION("caiaq USB audio");
Daniel Mack523f1dc2007-03-26 19:11:24 +020040MODULE_LICENSE("GPL");
41MODULE_SUPPORTED_DEVICE("{{Native Instruments, RigKontrol2},"
Daniel Mackad1e34b2007-09-17 14:45:14 +020042 "{Native Instruments, RigKontrol3},"
Daniel Mack523f1dc2007-03-26 19:11:24 +020043 "{Native Instruments, Kore Controller},"
Daniel Mack7829d0e2007-11-26 09:00:56 +010044 "{Native Instruments, Kore Controller 2},"
Daniel Mackf3e9d5d2008-05-08 15:42:15 +020045 "{Native Instruments, Audio Kontrol 1},"
Daniel Mackb30c4942009-07-22 14:13:35 +020046 "{Native Instruments, Audio 2 DJ},"
Daniel Mack21655922009-01-16 11:03:19 +010047 "{Native Instruments, Audio 4 DJ},"
Daniel Mackf3e9d5d2008-05-08 15:42:15 +020048 "{Native Instruments, Audio 8 DJ},"
Daniel Mackdf8d81a2010-09-01 16:23:46 +080049 "{Native Instruments, Traktor Audio 2},"
Daniel Mack21655922009-01-16 11:03:19 +010050 "{Native Instruments, Session I/O},"
Daniel Mack6da7a2a2010-03-22 13:13:37 +010051 "{Native Instruments, GuitarRig mobile}"
Daniel Mack15c5ab62010-09-10 17:04:57 +080052 "{Native Instruments, Traktor Kontrol X1}"
53 "{Native Instruments, Traktor Kontrol S4}");
Daniel Mack523f1dc2007-03-26 19:11:24 +020054
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,
Daniel Mack9318dce2009-06-01 21:36:23 +020087 .idProduct = USB_PID_RIGKONTROL2
Daniel Mack523f1dc2007-03-26 19:11:24 +020088 },
89 {
90 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
91 .idVendor = USB_VID_NATIVEINSTRUMENTS,
Daniel Mackad1e34b2007-09-17 14:45:14 +020092 .idProduct = USB_PID_RIGKONTROL3
93 },
94 {
95 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
96 .idVendor = USB_VID_NATIVEINSTRUMENTS,
Daniel Mack523f1dc2007-03-26 19:11:24 +020097 .idProduct = USB_PID_KORECONTROLLER
98 },
99 {
Daniel Mack7829d0e2007-11-26 09:00:56 +0100100 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
101 .idVendor = USB_VID_NATIVEINSTRUMENTS,
102 .idProduct = USB_PID_KORECONTROLLER2
103 },
104 {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200105 .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 },
Daniel Mackf3e9d5d2008-05-08 15:42:15 +0200114 {
115 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
116 .idVendor = USB_VID_NATIVEINSTRUMENTS,
117 .idProduct = USB_PID_SESSIONIO
118 },
Daniel Mack21655922009-01-16 11:03:19 +0100119 {
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 },
Daniel Mackb30c4942009-07-22 14:13:35 +0200129 {
130 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
131 .idVendor = USB_VID_NATIVEINSTRUMENTS,
132 .idProduct = USB_PID_AUDIO2DJ
133 },
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100134 {
135 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
136 .idVendor = USB_VID_NATIVEINSTRUMENTS,
137 .idProduct = USB_PID_TRAKTORKONTROLX1
138 },
Daniel Mack15c5ab62010-09-10 17:04:57 +0800139 {
140 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
141 .idVendor = USB_VID_NATIVEINSTRUMENTS,
142 .idProduct = USB_PID_TRAKTORKONTROLS4
143 },
Daniel Mackdf8d81a2010-09-01 16:23:46 +0800144 {
145 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
146 .idVendor = USB_VID_NATIVEINSTRUMENTS,
147 .idProduct = USB_PID_TRAKTORAUDIO2
148 },
Daniel Mack523f1dc2007-03-26 19:11:24 +0200149 { /* 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;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100186 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 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200195#ifdef CONFIG_SND_USB_CAIAQ_INPUT
196 case EP1_CMD_READ_ERP:
197 case EP1_CMD_READ_ANALOG:
Daniel Mack523f1dc2007-03-26 19:11:24 +0200198 snd_usb_caiaq_input_dispatch(dev, buf, urb->actual_length);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200199#endif
Daniel Mack8e3cd082007-11-22 11:40:04 +0100200 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200201 }
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
Daniel Mack8e3cd082007-11-22 11:40:04 +0100209int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *dev,
210 unsigned char command,
211 const unsigned char *buffer,
212 int len)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200213{
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);
Daniel Mack9318dce2009-06-01 21:36:23 +0200225
Daniel Mack523f1dc2007-03-26 19:11:24 +0200226 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];
Daniel Mack9318dce2009-06-01 21:36:23 +0200236
Daniel Mack523f1dc2007-03-26 19:11:24 +0200237 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;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100260 ret = snd_usb_caiaq_send_command(dev, EP1_CMD_AUDIO_PARAMS,
261 tmp, sizeof(tmp));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200262
263 if (ret)
264 return ret;
Daniel Mack9318dce2009-06-01 21:36:23 +0200265
266 if (!wait_event_timeout(dev->ep1_wait_queue,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200267 dev->audio_parm_answer >= 0, HZ))
268 return -EPIPE;
Daniel Mack9318dce2009-06-01 21:36:23 +0200269
270 if (dev->audio_parm_answer != 1)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200271 debug("unable to set the device's audio params\n");
Daniel Mack9311c9b2009-03-18 11:03:54 +0100272 else
273 dev->bpp = bpp;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200274
275 return dev->audio_parm_answer == 1 ? 0 : -EINVAL;
276}
277
Daniel Mack9318dce2009-06-01 21:36:23 +0200278int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev *dev,
279 int digital, int analog, int erp)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200280{
281 char tmp[3] = { digital, analog, erp };
Daniel Mack8e3cd082007-11-22 11:40:04 +0100282 return snd_usb_caiaq_send_command(dev, EP1_CMD_AUTO_MSG,
283 tmp, sizeof(tmp));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200284}
285
Sam Ravnborgc5981952008-02-22 18:46:47 +0100286static void __devinit setup_card(struct snd_usb_caiaqdev *dev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200287{
288 int ret;
Daniel Mackad1e34b2007-09-17 14:45:14 +0200289 char val[4];
Daniel Mack9318dce2009-06-01 21:36:23 +0200290
Daniel Mack523f1dc2007-03-26 19:11:24 +0200291 /* 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;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100298 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 3);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200299 break;
Daniel Mackad1e34b2007-09-17 14:45:14 +0200300 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;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100306 snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 4);
Daniel Mackad1e34b2007-09-17 14:45:14 +0200307 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200308 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
309 /* Audio Kontrol 1 - make USB-LED stop blinking */
310 val[0] = 0x00;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100311 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
Daniel Mack523f1dc2007-03-26 19:11:24 +0200334 break;
335 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200336
Daniel Mack7829d0e2007-11-26 09:00:56 +0100337 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 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200345
Daniel Mack7829d0e2007-11-26 09:00:56 +0100346 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 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200352
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 }
Daniel Mack8e3cd082007-11-22 11:40:04 +0100365
366 ret = snd_usb_caiaq_control_init(dev);
367 if (ret < 0)
368 log("Unable to set up control system (ret=%d)\n", ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200369}
370
Daniel Mack563c2bf2009-07-02 10:46:35 +0200371static int create_card(struct usb_device *usb_dev,
372 struct usb_interface *intf,
373 struct snd_card **cardp)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200374{
375 int devnum;
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100376 int err;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200377 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)
Takashi Iwai51721f72008-12-28 16:55:08 +0100385 return -ENODEV;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200386
Daniel Mack9318dce2009-06-01 21:36:23 +0200387 err = snd_card_create(index[devnum], id[devnum], THIS_MODULE,
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100388 sizeof(struct snd_usb_caiaqdev), &card);
389 if (err < 0)
Takashi Iwai51721f72008-12-28 16:55:08 +0100390 return err;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200391
392 dev = caiaqdev(card);
393 dev->chip.dev = usb_dev;
394 dev->chip.card = card;
Al Viro8c5330a2008-05-21 06:32:11 +0100395 dev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor),
396 le16_to_cpu(usb_dev->descriptor.idProduct));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200397 spin_lock_init(&dev->spinlock);
Daniel Mack563c2bf2009-07-02 10:46:35 +0200398 snd_card_set_dev(card, &intf->dev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200399
Takashi Iwai51721f72008-12-28 16:55:08 +0100400 *cardp = card;
401 return 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200402}
403
Sam Ravnborgc5981952008-02-22 18:46:47 +0100404static int __devinit init_card(struct snd_usb_caiaqdev *dev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200405{
Daniel Mackbafeee52009-06-02 12:36:39 +0200406 char *c, usbpath[32];
Daniel Mack523f1dc2007-03-26 19:11:24 +0200407 struct usb_device *usb_dev = dev->chip.dev;
408 struct snd_card *card = dev->chip.card;
Daniel Mackbafeee52009-06-02 12:36:39 +0200409 int err, len;
Daniel Mack9318dce2009-06-01 21:36:23 +0200410
Daniel Mack523f1dc2007-03-26 19:11:24 +0200411 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
Daniel Mack9318dce2009-06-01 21:36:23 +0200419 usb_fill_bulk_urb(&dev->ep1_in_urb, usb_dev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200420 usb_rcvbulkpipe(usb_dev, 0x1),
Daniel Mack9318dce2009-06-01 21:36:23 +0200421 dev->ep1_in_buf, EP1_BUFSIZE,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200422 usb_ep1_command_reply_dispatch, dev);
423
Daniel Mack9318dce2009-06-01 21:36:23 +0200424 usb_fill_bulk_urb(&dev->midi_out_urb, usb_dev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200425 usb_sndbulkpipe(usb_dev, 0x1),
Daniel Mack9318dce2009-06-01 21:36:23 +0200426 dev->midi_out_buf, EP1_BUFSIZE,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200427 snd_usb_caiaq_midi_output_done, dev);
Daniel Mack9318dce2009-06-01 21:36:23 +0200428
Daniel Mack523f1dc2007-03-26 19:11:24 +0200429 init_waitqueue_head(&dev->ep1_wait_queue);
430 init_waitqueue_head(&dev->prepare_wait_queue);
Daniel Mack9318dce2009-06-01 21:36:23 +0200431
Daniel Mack523f1dc2007-03-26 19:11:24 +0200432 if (usb_submit_urb(&dev->ep1_in_urb, GFP_KERNEL) != 0)
433 return -EIO;
434
Daniel Mack8e3cd082007-11-22 11:40:04 +0100435 err = snd_usb_caiaq_send_command(dev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200436 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);
Daniel Mack9318dce2009-06-01 21:36:23 +0200444
Daniel Mack523f1dc2007-03-26 19:11:24 +0200445 usb_string(usb_dev, usb_dev->descriptor.iProduct,
446 dev->product_name, CAIAQ_USB_STR_LEN);
Daniel Mack9318dce2009-06-01 21:36:23 +0200447
Daniel Mackd3873a12009-06-01 21:36:25 +0200448 strlcpy(card->driver, MODNAME, sizeof(card->driver));
449 strlcpy(card->shortname, dev->product_name, sizeof(card->shortname));
Daniel Mack955f2d92009-06-10 21:32:01 +0200450 strlcpy(card->mixername, dev->product_name, sizeof(card->mixername));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200451
Daniel Mackbafeee52009-06-02 12:36:39 +0200452 /* 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
Daniel Mack1a1df6f2009-06-01 21:36:26 +0200469 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);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200473
Daniel Mack523f1dc2007-03-26 19:11:24 +0200474 setup_card(dev);
475 return 0;
476}
477
Daniel Mack9318dce2009-06-01 21:36:23 +0200478static int __devinit snd_probe(struct usb_interface *intf,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200479 const struct usb_device_id *id)
480{
481 int ret;
482 struct snd_card *card;
483 struct usb_device *device = interface_to_usbdev(intf);
Daniel Mack9318dce2009-06-01 21:36:23 +0200484
Daniel Mack563c2bf2009-07-02 10:46:35 +0200485 ret = create_card(device, intf, &card);
Daniel Mack9318dce2009-06-01 21:36:23 +0200486
Takashi Iwai51721f72008-12-28 16:55:08 +0100487 if (ret < 0)
488 return ret;
Daniel Mack9318dce2009-06-01 21:36:23 +0200489
Julia Lawallf4e97492009-01-01 18:14:35 +0100490 usb_set_intfdata(intf, card);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200491 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 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200497
Daniel Mack523f1dc2007-03-26 19:11:24 +0200498 return 0;
499}
500
501static void snd_disconnect(struct usb_interface *intf)
502{
503 struct snd_usb_caiaqdev *dev;
Julia Lawallf4e97492009-01-01 18:14:35 +0100504 struct snd_card *card = usb_get_intfdata(intf);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200505
Daniel Mack8d048842008-04-14 15:39:14 +0200506 debug("%s(%p)\n", __func__, intf);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200507
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);
Daniel Mack9318dce2009-06-01 21:36:23 +0200518
Daniel Mack523f1dc2007-03-26 19:11:24 +0200519 usb_kill_urb(&dev->ep1_in_urb);
520 usb_kill_urb(&dev->midi_out_urb);
Daniel Mack9318dce2009-06-01 21:36:23 +0200521
Daniel Mack523f1dc2007-03-26 19:11:24 +0200522 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