blob: b871ba407e4ec8aa710c5f79a99f2e52fb7741b7 [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>
Daniel Mackf1f6b8f2013-03-03 20:46:22 +010023#include <linux/device.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020024#include <linux/interrupt.h>
Daniel Macke431cf42009-03-28 21:19:49 +010025#include <linux/module.h>
26#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/gfp.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020028#include <linux/usb.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020029#include <sound/initval.h>
Daniel Macke431cf42009-03-28 21:19:49 +010030#include <sound/core.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020031#include <sound/pcm.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020032
Daniel Mack936e7d02009-04-01 19:05:39 +020033#include "device.h"
34#include "audio.h"
35#include "midi.h"
36#include "control.h"
37#include "input.h"
Daniel Mack523f1dc2007-03-26 19:11:24 +020038
39MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
Daniel Mack6008fd52010-09-10 16:12:34 +080040MODULE_DESCRIPTION("caiaq USB audio");
Daniel Mack523f1dc2007-03-26 19:11:24 +020041MODULE_LICENSE("GPL");
Antonio Ospite0af49ff2013-06-21 13:11:51 +020042MODULE_SUPPORTED_DEVICE("{{Native Instruments,RigKontrol2},"
43 "{Native Instruments,RigKontrol3},"
44 "{Native Instruments,Kore Controller},"
45 "{Native Instruments,Kore Controller 2},"
46 "{Native Instruments,Audio Kontrol 1},"
47 "{Native Instruments,Audio 2 DJ},"
48 "{Native Instruments,Audio 4 DJ},"
49 "{Native Instruments,Audio 8 DJ},"
50 "{Native Instruments,Traktor Audio 2},"
51 "{Native Instruments,Session I/O},"
52 "{Native Instruments,GuitarRig mobile},"
53 "{Native Instruments,Traktor Kontrol X1},"
54 "{Native Instruments,Traktor Kontrol S4},"
55 "{Native Instruments,Maschine Controller}}");
Daniel Mack523f1dc2007-03-26 19:11:24 +020056
57static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
58static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103059static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
Daniel Mack523f1dc2007-03-26 19:11:24 +020060
61module_param_array(index, int, NULL, 0444);
62MODULE_PARM_DESC(index, "Index value for the caiaq sound device");
63module_param_array(id, charp, NULL, 0444);
64MODULE_PARM_DESC(id, "ID string for the caiaq soundcard.");
65module_param_array(enable, bool, NULL, 0444);
66MODULE_PARM_DESC(enable, "Enable the caiaq soundcard.");
67
68enum {
69 SAMPLERATE_44100 = 0,
70 SAMPLERATE_48000 = 1,
71 SAMPLERATE_96000 = 2,
72 SAMPLERATE_192000 = 3,
73 SAMPLERATE_88200 = 4,
74 SAMPLERATE_INVALID = 0xff
75};
76
77enum {
78 DEPTH_NONE = 0,
79 DEPTH_16 = 1,
80 DEPTH_24 = 2,
81 DEPTH_32 = 3
82};
83
84static struct usb_device_id snd_usb_id_table[] = {
85 {
86 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
87 .idVendor = USB_VID_NATIVEINSTRUMENTS,
Daniel Mack9318dce2009-06-01 21:36:23 +020088 .idProduct = USB_PID_RIGKONTROL2
Daniel Mack523f1dc2007-03-26 19:11:24 +020089 },
90 {
91 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
92 .idVendor = USB_VID_NATIVEINSTRUMENTS,
Daniel Mackad1e34b2007-09-17 14:45:14 +020093 .idProduct = USB_PID_RIGKONTROL3
94 },
95 {
96 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
97 .idVendor = USB_VID_NATIVEINSTRUMENTS,
Daniel Mack523f1dc2007-03-26 19:11:24 +020098 .idProduct = USB_PID_KORECONTROLLER
99 },
100 {
Daniel Mack7829d0e2007-11-26 09:00:56 +0100101 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
102 .idVendor = USB_VID_NATIVEINSTRUMENTS,
103 .idProduct = USB_PID_KORECONTROLLER2
104 },
105 {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200106 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
107 .idVendor = USB_VID_NATIVEINSTRUMENTS,
108 .idProduct = USB_PID_AK1
109 },
110 {
111 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
112 .idVendor = USB_VID_NATIVEINSTRUMENTS,
113 .idProduct = USB_PID_AUDIO8DJ
114 },
Daniel Mackf3e9d5d2008-05-08 15:42:15 +0200115 {
116 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
117 .idVendor = USB_VID_NATIVEINSTRUMENTS,
118 .idProduct = USB_PID_SESSIONIO
119 },
Daniel Mack21655922009-01-16 11:03:19 +0100120 {
121 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
122 .idVendor = USB_VID_NATIVEINSTRUMENTS,
123 .idProduct = USB_PID_GUITARRIGMOBILE
124 },
125 {
126 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
127 .idVendor = USB_VID_NATIVEINSTRUMENTS,
128 .idProduct = USB_PID_AUDIO4DJ
129 },
Daniel Mackb30c4942009-07-22 14:13:35 +0200130 {
131 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
132 .idVendor = USB_VID_NATIVEINSTRUMENTS,
133 .idProduct = USB_PID_AUDIO2DJ
134 },
Daniel Mack6da7a2a2010-03-22 13:13:37 +0100135 {
136 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
137 .idVendor = USB_VID_NATIVEINSTRUMENTS,
138 .idProduct = USB_PID_TRAKTORKONTROLX1
139 },
Daniel Mack15c5ab62010-09-10 17:04:57 +0800140 {
141 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
142 .idVendor = USB_VID_NATIVEINSTRUMENTS,
143 .idProduct = USB_PID_TRAKTORKONTROLS4
144 },
Daniel Mackdf8d81a2010-09-01 16:23:46 +0800145 {
146 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
147 .idVendor = USB_VID_NATIVEINSTRUMENTS,
148 .idProduct = USB_PID_TRAKTORAUDIO2
149 },
William Lighte6535102011-10-10 15:54:23 +0000150 {
151 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
152 .idVendor = USB_VID_NATIVEINSTRUMENTS,
153 .idProduct = USB_PID_MASCHINECONTROLLER
154 },
Daniel Mack523f1dc2007-03-26 19:11:24 +0200155 { /* terminator */ }
156};
157
158static void usb_ep1_command_reply_dispatch (struct urb* urb)
159{
160 int ret;
Daniel Mack2dad9402013-03-04 12:50:05 +0100161 struct device *dev = &urb->dev->dev;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100162 struct snd_usb_caiaqdev *cdev = urb->context;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200163 unsigned char *buf = urb->transfer_buffer;
164
Daniel Mack1c8470c2013-03-03 20:46:21 +0100165 if (urb->status || !cdev) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100166 dev_warn(dev, "received EP1 urb->status = %i\n", urb->status);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200167 return;
168 }
169
170 switch(buf[0]) {
171 case EP1_CMD_GET_DEVICE_INFO:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100172 memcpy(&cdev->spec, buf+1, sizeof(struct caiaq_device_spec));
173 cdev->spec.fw_version = le16_to_cpu(cdev->spec.fw_version);
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100174 dev_dbg(dev, "device spec (firmware %d): audio: %d in, %d out, "
Daniel Mack523f1dc2007-03-26 19:11:24 +0200175 "MIDI: %d in, %d out, data alignment %d\n",
Daniel Mack1c8470c2013-03-03 20:46:21 +0100176 cdev->spec.fw_version,
177 cdev->spec.num_analog_audio_in,
178 cdev->spec.num_analog_audio_out,
179 cdev->spec.num_midi_in,
180 cdev->spec.num_midi_out,
181 cdev->spec.data_alignment);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200182
Daniel Mack1c8470c2013-03-03 20:46:21 +0100183 cdev->spec_received++;
184 wake_up(&cdev->ep1_wait_queue);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200185 break;
186 case EP1_CMD_AUDIO_PARAMS:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100187 cdev->audio_parm_answer = buf[1];
188 wake_up(&cdev->ep1_wait_queue);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200189 break;
190 case EP1_CMD_MIDI_READ:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100191 snd_usb_caiaq_midi_handle_input(cdev, buf[1], buf + 3, buf[2]);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200192 break;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100193 case EP1_CMD_READ_IO:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100194 if (cdev->chip.usb_id ==
Daniel Mack8e3cd082007-11-22 11:40:04 +0100195 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100196 if (urb->actual_length > sizeof(cdev->control_state))
197 urb->actual_length = sizeof(cdev->control_state);
198 memcpy(cdev->control_state, buf + 1, urb->actual_length);
199 wake_up(&cdev->ep1_wait_queue);
Daniel Mack8e3cd082007-11-22 11:40:04 +0100200 break;
201 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200202#ifdef CONFIG_SND_USB_CAIAQ_INPUT
203 case EP1_CMD_READ_ERP:
204 case EP1_CMD_READ_ANALOG:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100205 snd_usb_caiaq_input_dispatch(cdev, buf, urb->actual_length);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200206#endif
Daniel Mack8e3cd082007-11-22 11:40:04 +0100207 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200208 }
209
Daniel Mack1c8470c2013-03-03 20:46:21 +0100210 cdev->ep1_in_urb.actual_length = 0;
211 ret = usb_submit_urb(&cdev->ep1_in_urb, GFP_ATOMIC);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200212 if (ret < 0)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100213 dev_err(dev, "unable to submit urb. OOM!?\n");
Daniel Mack523f1dc2007-03-26 19:11:24 +0200214}
215
Daniel Mack1c8470c2013-03-03 20:46:21 +0100216int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *cdev,
Daniel Mack8e3cd082007-11-22 11:40:04 +0100217 unsigned char command,
218 const unsigned char *buffer,
219 int len)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200220{
221 int actual_len;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100222 struct usb_device *usb_dev = cdev->chip.dev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200223
224 if (!usb_dev)
225 return -EIO;
226
227 if (len > EP1_BUFSIZE - 1)
228 len = EP1_BUFSIZE - 1;
229
230 if (buffer && len > 0)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100231 memcpy(cdev->ep1_out_buf+1, buffer, len);
Daniel Mack9318dce2009-06-01 21:36:23 +0200232
Daniel Mack1c8470c2013-03-03 20:46:21 +0100233 cdev->ep1_out_buf[0] = command;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200234 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
Daniel Mack1c8470c2013-03-03 20:46:21 +0100235 cdev->ep1_out_buf, len+1, &actual_len, 200);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200236}
237
Hannes Gräulerd2724de2013-09-28 21:51:08 +0200238int snd_usb_caiaq_send_command_bank(struct snd_usb_caiaqdev *cdev,
239 unsigned char command,
240 unsigned char bank,
241 const unsigned char *buffer,
242 int len)
243{
244 int actual_len;
245 struct usb_device *usb_dev = cdev->chip.dev;
246
247 if (!usb_dev)
248 return -EIO;
249
250 if (len > EP1_BUFSIZE - 2)
251 len = EP1_BUFSIZE - 2;
252
253 if (buffer && len > 0)
254 memcpy(cdev->ep1_out_buf+2, buffer, len);
255
256 cdev->ep1_out_buf[0] = command;
257 cdev->ep1_out_buf[1] = bank;
258
259 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
260 cdev->ep1_out_buf, len+2, &actual_len, 200);
261}
262
Daniel Mack1c8470c2013-03-03 20:46:21 +0100263int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200264 int rate, int depth, int bpp)
265{
266 int ret;
267 char tmp[5];
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100268 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack9318dce2009-06-01 21:36:23 +0200269
Daniel Mack523f1dc2007-03-26 19:11:24 +0200270 switch (rate) {
271 case 44100: tmp[0] = SAMPLERATE_44100; break;
272 case 48000: tmp[0] = SAMPLERATE_48000; break;
273 case 88200: tmp[0] = SAMPLERATE_88200; break;
274 case 96000: tmp[0] = SAMPLERATE_96000; break;
275 case 192000: tmp[0] = SAMPLERATE_192000; break;
276 default: return -EINVAL;
277 }
278
279 switch (depth) {
280 case 16: tmp[1] = DEPTH_16; break;
281 case 24: tmp[1] = DEPTH_24; break;
282 default: return -EINVAL;
283 }
284
285 tmp[2] = bpp & 0xff;
286 tmp[3] = bpp >> 8;
287 tmp[4] = 1; /* packets per microframe */
288
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100289 dev_dbg(dev, "setting audio params: %d Hz, %d bits, %d bpp\n",
Daniel Mack523f1dc2007-03-26 19:11:24 +0200290 rate, depth, bpp);
291
Daniel Mack1c8470c2013-03-03 20:46:21 +0100292 cdev->audio_parm_answer = -1;
293 ret = snd_usb_caiaq_send_command(cdev, EP1_CMD_AUDIO_PARAMS,
Daniel Mack8e3cd082007-11-22 11:40:04 +0100294 tmp, sizeof(tmp));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200295
296 if (ret)
297 return ret;
Daniel Mack9318dce2009-06-01 21:36:23 +0200298
Daniel Mack1c8470c2013-03-03 20:46:21 +0100299 if (!wait_event_timeout(cdev->ep1_wait_queue,
300 cdev->audio_parm_answer >= 0, HZ))
Daniel Mack523f1dc2007-03-26 19:11:24 +0200301 return -EPIPE;
Daniel Mack9318dce2009-06-01 21:36:23 +0200302
Daniel Mack1c8470c2013-03-03 20:46:21 +0100303 if (cdev->audio_parm_answer != 1)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100304 dev_dbg(dev, "unable to set the device's audio params\n");
Daniel Mack9311c9b2009-03-18 11:03:54 +0100305 else
Daniel Mack1c8470c2013-03-03 20:46:21 +0100306 cdev->bpp = bpp;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200307
Daniel Mack1c8470c2013-03-03 20:46:21 +0100308 return cdev->audio_parm_answer == 1 ? 0 : -EINVAL;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200309}
310
Daniel Mack1c8470c2013-03-03 20:46:21 +0100311int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev *cdev,
Daniel Mack9318dce2009-06-01 21:36:23 +0200312 int digital, int analog, int erp)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200313{
314 char tmp[3] = { digital, analog, erp };
Daniel Mack1c8470c2013-03-03 20:46:21 +0100315 return snd_usb_caiaq_send_command(cdev, EP1_CMD_AUTO_MSG,
Daniel Mack8e3cd082007-11-22 11:40:04 +0100316 tmp, sizeof(tmp));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200317}
318
Daniel Mack1c8470c2013-03-03 20:46:21 +0100319static void setup_card(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200320{
321 int ret;
Daniel Mackad1e34b2007-09-17 14:45:14 +0200322 char val[4];
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100323 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack9318dce2009-06-01 21:36:23 +0200324
Daniel Mack523f1dc2007-03-26 19:11:24 +0200325 /* device-specific startup specials */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100326 switch (cdev->chip.usb_id) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200327 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
328 /* RigKontrol2 - display centered dash ('-') */
329 val[0] = 0x00;
330 val[1] = 0x00;
331 val[2] = 0x01;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100332 snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 3);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200333 break;
Daniel Mackad1e34b2007-09-17 14:45:14 +0200334 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
335 /* RigKontrol2 - display two centered dashes ('--') */
336 val[0] = 0x00;
337 val[1] = 0x40;
338 val[2] = 0x40;
339 val[3] = 0x00;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100340 snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 4);
Daniel Mackad1e34b2007-09-17 14:45:14 +0200341 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200342 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
343 /* Audio Kontrol 1 - make USB-LED stop blinking */
344 val[0] = 0x00;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100345 snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 1);
Daniel Mack8e3cd082007-11-22 11:40:04 +0100346 break;
347 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
348 /* Audio 8 DJ - trigger read of current settings */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100349 cdev->control_state[0] = 0xff;
350 snd_usb_caiaq_set_auto_msg(cdev, 1, 0, 0);
351 snd_usb_caiaq_send_command(cdev, EP1_CMD_READ_IO, NULL, 0);
Daniel Mack8e3cd082007-11-22 11:40:04 +0100352
Daniel Mack1c8470c2013-03-03 20:46:21 +0100353 if (!wait_event_timeout(cdev->ep1_wait_queue,
354 cdev->control_state[0] != 0xff, HZ))
Daniel Mack8e3cd082007-11-22 11:40:04 +0100355 return;
356
357 /* fix up some defaults */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100358 if ((cdev->control_state[1] != 2) ||
359 (cdev->control_state[2] != 3) ||
360 (cdev->control_state[4] != 2)) {
361 cdev->control_state[1] = 2;
362 cdev->control_state[2] = 3;
363 cdev->control_state[4] = 2;
364 snd_usb_caiaq_send_command(cdev,
365 EP1_CMD_WRITE_IO, cdev->control_state, 6);
Daniel Mack8e3cd082007-11-22 11:40:04 +0100366 }
367
Daniel Mack523f1dc2007-03-26 19:11:24 +0200368 break;
369 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200370
Daniel Mack1c8470c2013-03-03 20:46:21 +0100371 if (cdev->spec.num_analog_audio_out +
372 cdev->spec.num_analog_audio_in +
373 cdev->spec.num_digital_audio_out +
374 cdev->spec.num_digital_audio_in > 0) {
375 ret = snd_usb_caiaq_audio_init(cdev);
Daniel Mack7829d0e2007-11-26 09:00:56 +0100376 if (ret < 0)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100377 dev_err(dev, "Unable to set up audio system (ret=%d)\n", ret);
Daniel Mack7829d0e2007-11-26 09:00:56 +0100378 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200379
Daniel Mack1c8470c2013-03-03 20:46:21 +0100380 if (cdev->spec.num_midi_in +
381 cdev->spec.num_midi_out > 0) {
382 ret = snd_usb_caiaq_midi_init(cdev);
Daniel Mack7829d0e2007-11-26 09:00:56 +0100383 if (ret < 0)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100384 dev_err(dev, "Unable to set up MIDI system (ret=%d)\n", ret);
Daniel Mack7829d0e2007-11-26 09:00:56 +0100385 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200386
387#ifdef CONFIG_SND_USB_CAIAQ_INPUT
Daniel Mack1c8470c2013-03-03 20:46:21 +0100388 ret = snd_usb_caiaq_input_init(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200389 if (ret < 0)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100390 dev_err(dev, "Unable to set up input system (ret=%d)\n", ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200391#endif
392
393 /* finally, register the card and all its sub-instances */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100394 ret = snd_card_register(cdev->chip.card);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200395 if (ret < 0) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100396 dev_err(dev, "snd_card_register() returned %d\n", ret);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100397 snd_card_free(cdev->chip.card);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200398 }
Daniel Mack8e3cd082007-11-22 11:40:04 +0100399
Daniel Mack1c8470c2013-03-03 20:46:21 +0100400 ret = snd_usb_caiaq_control_init(cdev);
Daniel Mack8e3cd082007-11-22 11:40:04 +0100401 if (ret < 0)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100402 dev_err(dev, "Unable to set up control system (ret=%d)\n", ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200403}
404
Daniel Mack563c2bf2009-07-02 10:46:35 +0200405static int create_card(struct usb_device *usb_dev,
406 struct usb_interface *intf,
407 struct snd_card **cardp)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200408{
409 int devnum;
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100410 int err;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200411 struct snd_card *card;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100412 struct snd_usb_caiaqdev *cdev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200413
414 for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
Antonio Ospite3dd446a2013-06-21 13:11:48 +0200415 if (enable[devnum])
Daniel Mack523f1dc2007-03-26 19:11:24 +0200416 break;
417
418 if (devnum >= SNDRV_CARDS)
Takashi Iwai51721f72008-12-28 16:55:08 +0100419 return -ENODEV;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200420
Takashi Iwai874b8d42014-01-29 14:22:20 +0100421 err = snd_card_new(&intf->dev,
422 index[devnum], id[devnum], THIS_MODULE,
423 sizeof(struct snd_usb_caiaqdev), &card);
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100424 if (err < 0)
Takashi Iwai51721f72008-12-28 16:55:08 +0100425 return err;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200426
Daniel Mack1c8470c2013-03-03 20:46:21 +0100427 cdev = caiaqdev(card);
428 cdev->chip.dev = usb_dev;
429 cdev->chip.card = card;
430 cdev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor),
Al Viro8c5330a2008-05-21 06:32:11 +0100431 le16_to_cpu(usb_dev->descriptor.idProduct));
Daniel Mack1c8470c2013-03-03 20:46:21 +0100432 spin_lock_init(&cdev->spinlock);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200433
Takashi Iwai51721f72008-12-28 16:55:08 +0100434 *cardp = card;
435 return 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200436}
437
Daniel Mack1c8470c2013-03-03 20:46:21 +0100438static int init_card(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200439{
Daniel Mackbafeee52009-06-02 12:36:39 +0200440 char *c, usbpath[32];
Daniel Mack1c8470c2013-03-03 20:46:21 +0100441 struct usb_device *usb_dev = cdev->chip.dev;
442 struct snd_card *card = cdev->chip.card;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100443 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mackbafeee52009-06-02 12:36:39 +0200444 int err, len;
Daniel Mack9318dce2009-06-01 21:36:23 +0200445
Daniel Mack523f1dc2007-03-26 19:11:24 +0200446 if (usb_set_interface(usb_dev, 0, 1) != 0) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100447 dev_err(dev, "can't set alt interface.\n");
Daniel Mack523f1dc2007-03-26 19:11:24 +0200448 return -EIO;
449 }
450
Daniel Mack1c8470c2013-03-03 20:46:21 +0100451 usb_init_urb(&cdev->ep1_in_urb);
452 usb_init_urb(&cdev->midi_out_urb);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200453
Daniel Mack1c8470c2013-03-03 20:46:21 +0100454 usb_fill_bulk_urb(&cdev->ep1_in_urb, usb_dev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200455 usb_rcvbulkpipe(usb_dev, 0x1),
Daniel Mack1c8470c2013-03-03 20:46:21 +0100456 cdev->ep1_in_buf, EP1_BUFSIZE,
457 usb_ep1_command_reply_dispatch, cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200458
Daniel Mack1c8470c2013-03-03 20:46:21 +0100459 usb_fill_bulk_urb(&cdev->midi_out_urb, usb_dev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200460 usb_sndbulkpipe(usb_dev, 0x1),
Daniel Mack1c8470c2013-03-03 20:46:21 +0100461 cdev->midi_out_buf, EP1_BUFSIZE,
462 snd_usb_caiaq_midi_output_done, cdev);
Daniel Mack9318dce2009-06-01 21:36:23 +0200463
Daniel Mack1c8470c2013-03-03 20:46:21 +0100464 init_waitqueue_head(&cdev->ep1_wait_queue);
465 init_waitqueue_head(&cdev->prepare_wait_queue);
Daniel Mack9318dce2009-06-01 21:36:23 +0200466
Daniel Mack1c8470c2013-03-03 20:46:21 +0100467 if (usb_submit_urb(&cdev->ep1_in_urb, GFP_KERNEL) != 0)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200468 return -EIO;
469
Daniel Mack1c8470c2013-03-03 20:46:21 +0100470 err = snd_usb_caiaq_send_command(cdev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200471 if (err)
472 return err;
473
Daniel Mack1c8470c2013-03-03 20:46:21 +0100474 if (!wait_event_timeout(cdev->ep1_wait_queue, cdev->spec_received, HZ))
Daniel Mack523f1dc2007-03-26 19:11:24 +0200475 return -ENODEV;
476
477 usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
Daniel Mack1c8470c2013-03-03 20:46:21 +0100478 cdev->vendor_name, CAIAQ_USB_STR_LEN);
Daniel Mack9318dce2009-06-01 21:36:23 +0200479
Daniel Mack523f1dc2007-03-26 19:11:24 +0200480 usb_string(usb_dev, usb_dev->descriptor.iProduct,
Daniel Mack1c8470c2013-03-03 20:46:21 +0100481 cdev->product_name, CAIAQ_USB_STR_LEN);
Daniel Mack9318dce2009-06-01 21:36:23 +0200482
Daniel Mackd3873a12009-06-01 21:36:25 +0200483 strlcpy(card->driver, MODNAME, sizeof(card->driver));
Daniel Mack1c8470c2013-03-03 20:46:21 +0100484 strlcpy(card->shortname, cdev->product_name, sizeof(card->shortname));
485 strlcpy(card->mixername, cdev->product_name, sizeof(card->mixername));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200486
Daniel Mackbafeee52009-06-02 12:36:39 +0200487 /* if the id was not passed as module option, fill it with a shortened
488 * version of the product string which does not contain any
489 * whitespaces */
490
491 if (*card->id == '\0') {
492 char id[sizeof(card->id)];
493
494 memset(id, 0, sizeof(id));
495
496 for (c = card->shortname, len = 0;
497 *c && len < sizeof(card->id); c++)
498 if (*c != ' ')
499 id[len++] = *c;
500
501 snd_card_set_id(card, id);
502 }
503
Daniel Mack1a1df6f2009-06-01 21:36:26 +0200504 usb_make_path(usb_dev, usbpath, sizeof(usbpath));
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100505 snprintf(card->longname, sizeof(card->longname), "%s %s (%s)",
Daniel Mack1c8470c2013-03-03 20:46:21 +0100506 cdev->vendor_name, cdev->product_name, usbpath);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200507
Daniel Mack1c8470c2013-03-03 20:46:21 +0100508 setup_card(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200509 return 0;
510}
511
Bill Pemberton14c56702012-12-06 12:35:29 -0500512static int snd_probe(struct usb_interface *intf,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200513 const struct usb_device_id *id)
514{
515 int ret;
Daniel Mackda185442012-06-18 21:16:31 +0200516 struct snd_card *card = NULL;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100517 struct usb_device *usb_dev = interface_to_usbdev(intf);
Daniel Mack9318dce2009-06-01 21:36:23 +0200518
Daniel Mack1c8470c2013-03-03 20:46:21 +0100519 ret = create_card(usb_dev, intf, &card);
Daniel Mack9318dce2009-06-01 21:36:23 +0200520
Takashi Iwai51721f72008-12-28 16:55:08 +0100521 if (ret < 0)
522 return ret;
Daniel Mack9318dce2009-06-01 21:36:23 +0200523
Julia Lawallf4e97492009-01-01 18:14:35 +0100524 usb_set_intfdata(intf, card);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200525 ret = init_card(caiaqdev(card));
526 if (ret < 0) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100527 dev_err(&usb_dev->dev, "unable to init card! (ret=%d)\n", ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200528 snd_card_free(card);
529 return ret;
530 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200531
Daniel Mack523f1dc2007-03-26 19:11:24 +0200532 return 0;
533}
534
535static void snd_disconnect(struct usb_interface *intf)
536{
Julia Lawallf4e97492009-01-01 18:14:35 +0100537 struct snd_card *card = usb_get_intfdata(intf);
Daniel Mack2dad9402013-03-04 12:50:05 +0100538 struct device *dev = intf->usb_dev;
539 struct snd_usb_caiaqdev *cdev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200540
541 if (!card)
542 return;
543
Daniel Mack2dad9402013-03-04 12:50:05 +0100544 cdev = caiaqdev(card);
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100545 dev_dbg(dev, "%s(%p)\n", __func__, intf);
546
Daniel Mack523f1dc2007-03-26 19:11:24 +0200547 snd_card_disconnect(card);
548
549#ifdef CONFIG_SND_USB_CAIAQ_INPUT
Daniel Mack1c8470c2013-03-03 20:46:21 +0100550 snd_usb_caiaq_input_free(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200551#endif
Daniel Mack1c8470c2013-03-03 20:46:21 +0100552 snd_usb_caiaq_audio_free(cdev);
Daniel Mack9318dce2009-06-01 21:36:23 +0200553
Daniel Mack1c8470c2013-03-03 20:46:21 +0100554 usb_kill_urb(&cdev->ep1_in_urb);
555 usb_kill_urb(&cdev->midi_out_urb);
Daniel Mack9318dce2009-06-01 21:36:23 +0200556
Daniel Mack523f1dc2007-03-26 19:11:24 +0200557 snd_card_free(card);
558 usb_reset_device(interface_to_usbdev(intf));
559}
560
561
562MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
563static struct usb_driver snd_usb_driver = {
564 .name = MODNAME,
565 .probe = snd_probe,
566 .disconnect = snd_disconnect,
567 .id_table = snd_usb_id_table,
568};
569
Greg Kroah-Hartman424f0752011-11-18 09:50:44 -0800570module_usb_driver(snd_usb_driver);