blob: 2af3b7eb0a88c5618f962fe60dfcce2e6bcd6000 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Daniel Mack523f1dc2007-03-26 19:11:24 +02002/*
3 * caiaq.c: ALSA driver for caiaq/NativeInstruments devices
4 *
5 * Copyright (c) 2007 Daniel Mack <daniel@caiaq.de>
6 * Karsten Wiese <fzu@wemgehoertderstaat.de>
Daniel Mack523f1dc2007-03-26 19:11:24 +02007*/
8
Daniel Mack523f1dc2007-03-26 19:11:24 +02009#include <linux/moduleparam.h>
Daniel Mackf1f6b8f2013-03-03 20:46:22 +010010#include <linux/device.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020011#include <linux/interrupt.h>
Daniel Macke431cf42009-03-28 21:19:49 +010012#include <linux/module.h>
13#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/gfp.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020015#include <linux/usb.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020016#include <sound/initval.h>
Daniel Macke431cf42009-03-28 21:19:49 +010017#include <sound/core.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020018#include <sound/pcm.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020019
Daniel Mack936e7d02009-04-01 19:05:39 +020020#include "device.h"
21#include "audio.h"
22#include "midi.h"
23#include "control.h"
24#include "input.h"
Daniel Mack523f1dc2007-03-26 19:11:24 +020025
26MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
Daniel Mack6008fd52010-09-10 16:12:34 +080027MODULE_DESCRIPTION("caiaq USB audio");
Daniel Mack523f1dc2007-03-26 19:11:24 +020028MODULE_LICENSE("GPL");
Antonio Ospite0af49ff2013-06-21 13:11:51 +020029MODULE_SUPPORTED_DEVICE("{{Native Instruments,RigKontrol2},"
30 "{Native Instruments,RigKontrol3},"
31 "{Native Instruments,Kore Controller},"
32 "{Native Instruments,Kore Controller 2},"
33 "{Native Instruments,Audio Kontrol 1},"
34 "{Native Instruments,Audio 2 DJ},"
35 "{Native Instruments,Audio 4 DJ},"
36 "{Native Instruments,Audio 8 DJ},"
37 "{Native Instruments,Traktor Audio 2},"
38 "{Native Instruments,Session I/O},"
39 "{Native Instruments,GuitarRig mobile},"
40 "{Native Instruments,Traktor Kontrol X1},"
41 "{Native Instruments,Traktor Kontrol S4},"
42 "{Native Instruments,Maschine Controller}}");
Daniel Mack523f1dc2007-03-26 19:11:24 +020043
44static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
45static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103046static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
Daniel Mack523f1dc2007-03-26 19:11:24 +020047
48module_param_array(index, int, NULL, 0444);
49MODULE_PARM_DESC(index, "Index value for the caiaq sound device");
50module_param_array(id, charp, NULL, 0444);
51MODULE_PARM_DESC(id, "ID string for the caiaq soundcard.");
52module_param_array(enable, bool, NULL, 0444);
53MODULE_PARM_DESC(enable, "Enable the caiaq soundcard.");
54
55enum {
56 SAMPLERATE_44100 = 0,
57 SAMPLERATE_48000 = 1,
58 SAMPLERATE_96000 = 2,
59 SAMPLERATE_192000 = 3,
60 SAMPLERATE_88200 = 4,
61 SAMPLERATE_INVALID = 0xff
62};
63
64enum {
65 DEPTH_NONE = 0,
66 DEPTH_16 = 1,
67 DEPTH_24 = 2,
68 DEPTH_32 = 3
69};
70
Arvind Yadav57ee11e2017-08-06 12:18:55 +053071static const struct usb_device_id snd_usb_id_table[] = {
Daniel Mack523f1dc2007-03-26 19:11:24 +020072 {
73 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
74 .idVendor = USB_VID_NATIVEINSTRUMENTS,
Daniel Mack9318dce2009-06-01 21:36:23 +020075 .idProduct = USB_PID_RIGKONTROL2
Daniel Mack523f1dc2007-03-26 19:11:24 +020076 },
77 {
78 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
79 .idVendor = USB_VID_NATIVEINSTRUMENTS,
Daniel Mackad1e34b2007-09-17 14:45:14 +020080 .idProduct = USB_PID_RIGKONTROL3
81 },
82 {
83 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
84 .idVendor = USB_VID_NATIVEINSTRUMENTS,
Daniel Mack523f1dc2007-03-26 19:11:24 +020085 .idProduct = USB_PID_KORECONTROLLER
86 },
87 {
Daniel Mack7829d0e2007-11-26 09:00:56 +010088 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
89 .idVendor = USB_VID_NATIVEINSTRUMENTS,
90 .idProduct = USB_PID_KORECONTROLLER2
91 },
92 {
Daniel Mack523f1dc2007-03-26 19:11:24 +020093 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
94 .idVendor = USB_VID_NATIVEINSTRUMENTS,
95 .idProduct = USB_PID_AK1
96 },
97 {
98 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
99 .idVendor = USB_VID_NATIVEINSTRUMENTS,
100 .idProduct = USB_PID_AUDIO8DJ
101 },
Daniel Mackf3e9d5d2008-05-08 15:42:15 +0200102 {
103 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
104 .idVendor = USB_VID_NATIVEINSTRUMENTS,
105 .idProduct = USB_PID_SESSIONIO
106 },
Daniel Mack21655922009-01-16 11:03:19 +0100107 {
108 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
109 .idVendor = USB_VID_NATIVEINSTRUMENTS,
110 .idProduct = USB_PID_GUITARRIGMOBILE
111 },
112 {
113 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
114 .idVendor = USB_VID_NATIVEINSTRUMENTS,
115 .idProduct = USB_PID_AUDIO4DJ
116 },
Daniel Mackb30c4942009-07-22 14:13:35 +0200117 {
118 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
119 .idVendor = USB_VID_NATIVEINSTRUMENTS,
120 .idProduct = USB_PID_AUDIO2DJ
121 },
Daniel Mack6da7a2aa2010-03-22 13:13:37 +0100122 {
123 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
124 .idVendor = USB_VID_NATIVEINSTRUMENTS,
125 .idProduct = USB_PID_TRAKTORKONTROLX1
126 },
Daniel Mack15c5ab62010-09-10 17:04:57 +0800127 {
128 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
129 .idVendor = USB_VID_NATIVEINSTRUMENTS,
130 .idProduct = USB_PID_TRAKTORKONTROLS4
131 },
Daniel Mackdf8d81a2010-09-01 16:23:46 +0800132 {
133 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
134 .idVendor = USB_VID_NATIVEINSTRUMENTS,
135 .idProduct = USB_PID_TRAKTORAUDIO2
136 },
William Lighte6535102011-10-10 15:54:23 +0000137 {
138 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
139 .idVendor = USB_VID_NATIVEINSTRUMENTS,
140 .idProduct = USB_PID_MASCHINECONTROLLER
141 },
Daniel Mack523f1dc2007-03-26 19:11:24 +0200142 { /* terminator */ }
143};
144
145static void usb_ep1_command_reply_dispatch (struct urb* urb)
146{
147 int ret;
Daniel Mack2dad9402013-03-04 12:50:05 +0100148 struct device *dev = &urb->dev->dev;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100149 struct snd_usb_caiaqdev *cdev = urb->context;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200150 unsigned char *buf = urb->transfer_buffer;
151
Daniel Mack1c8470c2013-03-03 20:46:21 +0100152 if (urb->status || !cdev) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100153 dev_warn(dev, "received EP1 urb->status = %i\n", urb->status);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200154 return;
155 }
156
157 switch(buf[0]) {
158 case EP1_CMD_GET_DEVICE_INFO:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100159 memcpy(&cdev->spec, buf+1, sizeof(struct caiaq_device_spec));
160 cdev->spec.fw_version = le16_to_cpu(cdev->spec.fw_version);
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100161 dev_dbg(dev, "device spec (firmware %d): audio: %d in, %d out, "
Daniel Mack523f1dc2007-03-26 19:11:24 +0200162 "MIDI: %d in, %d out, data alignment %d\n",
Daniel Mack1c8470c2013-03-03 20:46:21 +0100163 cdev->spec.fw_version,
164 cdev->spec.num_analog_audio_in,
165 cdev->spec.num_analog_audio_out,
166 cdev->spec.num_midi_in,
167 cdev->spec.num_midi_out,
168 cdev->spec.data_alignment);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200169
Daniel Mack1c8470c2013-03-03 20:46:21 +0100170 cdev->spec_received++;
171 wake_up(&cdev->ep1_wait_queue);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200172 break;
173 case EP1_CMD_AUDIO_PARAMS:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100174 cdev->audio_parm_answer = buf[1];
175 wake_up(&cdev->ep1_wait_queue);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200176 break;
177 case EP1_CMD_MIDI_READ:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100178 snd_usb_caiaq_midi_handle_input(cdev, buf[1], buf + 3, buf[2]);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200179 break;
Daniel Mack8e3cd082007-11-22 11:40:04 +0100180 case EP1_CMD_READ_IO:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100181 if (cdev->chip.usb_id ==
Daniel Mack8e3cd082007-11-22 11:40:04 +0100182 USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) {
Daniel Mack1c8470c2013-03-03 20:46:21 +0100183 if (urb->actual_length > sizeof(cdev->control_state))
184 urb->actual_length = sizeof(cdev->control_state);
185 memcpy(cdev->control_state, buf + 1, urb->actual_length);
186 wake_up(&cdev->ep1_wait_queue);
Daniel Mack8e3cd082007-11-22 11:40:04 +0100187 break;
188 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200189#ifdef CONFIG_SND_USB_CAIAQ_INPUT
Takashi Iwaie2d413f2020-07-09 13:17:50 +0200190 fallthrough;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200191 case EP1_CMD_READ_ERP:
192 case EP1_CMD_READ_ANALOG:
Daniel Mack1c8470c2013-03-03 20:46:21 +0100193 snd_usb_caiaq_input_dispatch(cdev, buf, urb->actual_length);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200194#endif
Daniel Mack8e3cd082007-11-22 11:40:04 +0100195 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200196 }
197
Daniel Mack1c8470c2013-03-03 20:46:21 +0100198 cdev->ep1_in_urb.actual_length = 0;
199 ret = usb_submit_urb(&cdev->ep1_in_urb, GFP_ATOMIC);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200200 if (ret < 0)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100201 dev_err(dev, "unable to submit urb. OOM!?\n");
Daniel Mack523f1dc2007-03-26 19:11:24 +0200202}
203
Daniel Mack1c8470c2013-03-03 20:46:21 +0100204int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *cdev,
Daniel Mack8e3cd082007-11-22 11:40:04 +0100205 unsigned char command,
206 const unsigned char *buffer,
207 int len)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200208{
209 int actual_len;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100210 struct usb_device *usb_dev = cdev->chip.dev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200211
212 if (!usb_dev)
213 return -EIO;
214
215 if (len > EP1_BUFSIZE - 1)
216 len = EP1_BUFSIZE - 1;
217
218 if (buffer && len > 0)
Daniel Mack1c8470c2013-03-03 20:46:21 +0100219 memcpy(cdev->ep1_out_buf+1, buffer, len);
Daniel Mack9318dce2009-06-01 21:36:23 +0200220
Daniel Mack1c8470c2013-03-03 20:46:21 +0100221 cdev->ep1_out_buf[0] = command;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200222 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
Daniel Mack1c8470c2013-03-03 20:46:21 +0100223 cdev->ep1_out_buf, len+1, &actual_len, 200);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200224}
225
Hannes Gräulerd2724de2013-09-28 21:51:08 +0200226int snd_usb_caiaq_send_command_bank(struct snd_usb_caiaqdev *cdev,
227 unsigned char command,
228 unsigned char bank,
229 const unsigned char *buffer,
230 int len)
231{
232 int actual_len;
233 struct usb_device *usb_dev = cdev->chip.dev;
234
235 if (!usb_dev)
236 return -EIO;
237
238 if (len > EP1_BUFSIZE - 2)
239 len = EP1_BUFSIZE - 2;
240
241 if (buffer && len > 0)
242 memcpy(cdev->ep1_out_buf+2, buffer, len);
243
244 cdev->ep1_out_buf[0] = command;
245 cdev->ep1_out_buf[1] = bank;
246
247 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
248 cdev->ep1_out_buf, len+2, &actual_len, 200);
249}
250
Daniel Mack1c8470c2013-03-03 20:46:21 +0100251int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200252 int rate, int depth, int bpp)
253{
254 int ret;
255 char tmp[5];
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100256 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack9318dce2009-06-01 21:36:23 +0200257
Daniel Mack523f1dc2007-03-26 19:11:24 +0200258 switch (rate) {
259 case 44100: tmp[0] = SAMPLERATE_44100; break;
260 case 48000: tmp[0] = SAMPLERATE_48000; break;
261 case 88200: tmp[0] = SAMPLERATE_88200; break;
262 case 96000: tmp[0] = SAMPLERATE_96000; break;
263 case 192000: tmp[0] = SAMPLERATE_192000; break;
264 default: return -EINVAL;
265 }
266
267 switch (depth) {
268 case 16: tmp[1] = DEPTH_16; break;
269 case 24: tmp[1] = DEPTH_24; break;
270 default: return -EINVAL;
271 }
272
273 tmp[2] = bpp & 0xff;
274 tmp[3] = bpp >> 8;
275 tmp[4] = 1; /* packets per microframe */
276
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100277 dev_dbg(dev, "setting audio params: %d Hz, %d bits, %d bpp\n",
Daniel Mack523f1dc2007-03-26 19:11:24 +0200278 rate, depth, bpp);
279
Daniel Mack1c8470c2013-03-03 20:46:21 +0100280 cdev->audio_parm_answer = -1;
281 ret = snd_usb_caiaq_send_command(cdev, EP1_CMD_AUDIO_PARAMS,
Daniel Mack8e3cd082007-11-22 11:40:04 +0100282 tmp, sizeof(tmp));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200283
284 if (ret)
285 return ret;
Daniel Mack9318dce2009-06-01 21:36:23 +0200286
Daniel Mack1c8470c2013-03-03 20:46:21 +0100287 if (!wait_event_timeout(cdev->ep1_wait_queue,
288 cdev->audio_parm_answer >= 0, HZ))
Daniel Mack523f1dc2007-03-26 19:11:24 +0200289 return -EPIPE;
Daniel Mack9318dce2009-06-01 21:36:23 +0200290
Daniel Mack1c8470c2013-03-03 20:46:21 +0100291 if (cdev->audio_parm_answer != 1)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100292 dev_dbg(dev, "unable to set the device's audio params\n");
Daniel Mack9311c9b2009-03-18 11:03:54 +0100293 else
Daniel Mack1c8470c2013-03-03 20:46:21 +0100294 cdev->bpp = bpp;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200295
Daniel Mack1c8470c2013-03-03 20:46:21 +0100296 return cdev->audio_parm_answer == 1 ? 0 : -EINVAL;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200297}
298
Daniel Mack1c8470c2013-03-03 20:46:21 +0100299int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev *cdev,
Daniel Mack9318dce2009-06-01 21:36:23 +0200300 int digital, int analog, int erp)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200301{
302 char tmp[3] = { digital, analog, erp };
Daniel Mack1c8470c2013-03-03 20:46:21 +0100303 return snd_usb_caiaq_send_command(cdev, EP1_CMD_AUTO_MSG,
Daniel Mack8e3cd082007-11-22 11:40:04 +0100304 tmp, sizeof(tmp));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200305}
306
Daniel Mack1c8470c2013-03-03 20:46:21 +0100307static void setup_card(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200308{
309 int ret;
Daniel Mackad1e34b2007-09-17 14:45:14 +0200310 char val[4];
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100311 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack9318dce2009-06-01 21:36:23 +0200312
Daniel Mack523f1dc2007-03-26 19:11:24 +0200313 /* device-specific startup specials */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100314 switch (cdev->chip.usb_id) {
Daniel Mack523f1dc2007-03-26 19:11:24 +0200315 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
316 /* RigKontrol2 - display centered dash ('-') */
317 val[0] = 0x00;
318 val[1] = 0x00;
319 val[2] = 0x01;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100320 snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 3);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200321 break;
Daniel Mackad1e34b2007-09-17 14:45:14 +0200322 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
323 /* RigKontrol2 - display two centered dashes ('--') */
324 val[0] = 0x00;
325 val[1] = 0x40;
326 val[2] = 0x40;
327 val[3] = 0x00;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100328 snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 4);
Daniel Mackad1e34b2007-09-17 14:45:14 +0200329 break;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200330 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
331 /* Audio Kontrol 1 - make USB-LED stop blinking */
332 val[0] = 0x00;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100333 snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 1);
Daniel Mack8e3cd082007-11-22 11:40:04 +0100334 break;
335 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ):
336 /* Audio 8 DJ - trigger read of current settings */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100337 cdev->control_state[0] = 0xff;
338 snd_usb_caiaq_set_auto_msg(cdev, 1, 0, 0);
339 snd_usb_caiaq_send_command(cdev, EP1_CMD_READ_IO, NULL, 0);
Daniel Mack8e3cd082007-11-22 11:40:04 +0100340
Daniel Mack1c8470c2013-03-03 20:46:21 +0100341 if (!wait_event_timeout(cdev->ep1_wait_queue,
342 cdev->control_state[0] != 0xff, HZ))
Daniel Mack8e3cd082007-11-22 11:40:04 +0100343 return;
344
345 /* fix up some defaults */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100346 if ((cdev->control_state[1] != 2) ||
347 (cdev->control_state[2] != 3) ||
348 (cdev->control_state[4] != 2)) {
349 cdev->control_state[1] = 2;
350 cdev->control_state[2] = 3;
351 cdev->control_state[4] = 2;
352 snd_usb_caiaq_send_command(cdev,
353 EP1_CMD_WRITE_IO, cdev->control_state, 6);
Daniel Mack8e3cd082007-11-22 11:40:04 +0100354 }
355
Daniel Mack523f1dc2007-03-26 19:11:24 +0200356 break;
357 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200358
Daniel Mack1c8470c2013-03-03 20:46:21 +0100359 if (cdev->spec.num_analog_audio_out +
360 cdev->spec.num_analog_audio_in +
361 cdev->spec.num_digital_audio_out +
362 cdev->spec.num_digital_audio_in > 0) {
363 ret = snd_usb_caiaq_audio_init(cdev);
Daniel Mack7829d0e2007-11-26 09:00:56 +0100364 if (ret < 0)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100365 dev_err(dev, "Unable to set up audio system (ret=%d)\n", ret);
Daniel Mack7829d0e2007-11-26 09:00:56 +0100366 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200367
Daniel Mack1c8470c2013-03-03 20:46:21 +0100368 if (cdev->spec.num_midi_in +
369 cdev->spec.num_midi_out > 0) {
370 ret = snd_usb_caiaq_midi_init(cdev);
Daniel Mack7829d0e2007-11-26 09:00:56 +0100371 if (ret < 0)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100372 dev_err(dev, "Unable to set up MIDI system (ret=%d)\n", ret);
Daniel Mack7829d0e2007-11-26 09:00:56 +0100373 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200374
375#ifdef CONFIG_SND_USB_CAIAQ_INPUT
Daniel Mack1c8470c2013-03-03 20:46:21 +0100376 ret = snd_usb_caiaq_input_init(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200377 if (ret < 0)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100378 dev_err(dev, "Unable to set up input system (ret=%d)\n", ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200379#endif
380
381 /* finally, register the card and all its sub-instances */
Daniel Mack1c8470c2013-03-03 20:46:21 +0100382 ret = snd_card_register(cdev->chip.card);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200383 if (ret < 0) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100384 dev_err(dev, "snd_card_register() returned %d\n", ret);
Daniel Mack1c8470c2013-03-03 20:46:21 +0100385 snd_card_free(cdev->chip.card);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200386 }
Daniel Mack8e3cd082007-11-22 11:40:04 +0100387
Daniel Mack1c8470c2013-03-03 20:46:21 +0100388 ret = snd_usb_caiaq_control_init(cdev);
Daniel Mack8e3cd082007-11-22 11:40:04 +0100389 if (ret < 0)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100390 dev_err(dev, "Unable to set up control system (ret=%d)\n", ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200391}
392
Daniel Mack563c2bf2009-07-02 10:46:35 +0200393static int create_card(struct usb_device *usb_dev,
394 struct usb_interface *intf,
395 struct snd_card **cardp)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200396{
397 int devnum;
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100398 int err;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200399 struct snd_card *card;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100400 struct snd_usb_caiaqdev *cdev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200401
402 for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
Antonio Ospite3dd446a2013-06-21 13:11:48 +0200403 if (enable[devnum])
Daniel Mack523f1dc2007-03-26 19:11:24 +0200404 break;
405
406 if (devnum >= SNDRV_CARDS)
Takashi Iwai51721f72008-12-28 16:55:08 +0100407 return -ENODEV;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200408
Takashi Iwai874b8d42014-01-29 14:22:20 +0100409 err = snd_card_new(&intf->dev,
410 index[devnum], id[devnum], THIS_MODULE,
411 sizeof(struct snd_usb_caiaqdev), &card);
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100412 if (err < 0)
Takashi Iwai51721f72008-12-28 16:55:08 +0100413 return err;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200414
Daniel Mack1c8470c2013-03-03 20:46:21 +0100415 cdev = caiaqdev(card);
416 cdev->chip.dev = usb_dev;
417 cdev->chip.card = card;
418 cdev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor),
Al Viro8c5330a2008-05-21 06:32:11 +0100419 le16_to_cpu(usb_dev->descriptor.idProduct));
Daniel Mack1c8470c2013-03-03 20:46:21 +0100420 spin_lock_init(&cdev->spinlock);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200421
Takashi Iwai51721f72008-12-28 16:55:08 +0100422 *cardp = card;
423 return 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200424}
425
Daniel Mack1c8470c2013-03-03 20:46:21 +0100426static int init_card(struct snd_usb_caiaqdev *cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200427{
Daniel Mackbafeee52009-06-02 12:36:39 +0200428 char *c, usbpath[32];
Daniel Mack1c8470c2013-03-03 20:46:21 +0100429 struct usb_device *usb_dev = cdev->chip.dev;
430 struct snd_card *card = cdev->chip.card;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100431 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mackbafeee52009-06-02 12:36:39 +0200432 int err, len;
Daniel Mack9318dce2009-06-01 21:36:23 +0200433
Daniel Mack523f1dc2007-03-26 19:11:24 +0200434 if (usb_set_interface(usb_dev, 0, 1) != 0) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100435 dev_err(dev, "can't set alt interface.\n");
Daniel Mack523f1dc2007-03-26 19:11:24 +0200436 return -EIO;
437 }
438
Daniel Mack1c8470c2013-03-03 20:46:21 +0100439 usb_init_urb(&cdev->ep1_in_urb);
440 usb_init_urb(&cdev->midi_out_urb);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200441
Daniel Mack1c8470c2013-03-03 20:46:21 +0100442 usb_fill_bulk_urb(&cdev->ep1_in_urb, usb_dev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200443 usb_rcvbulkpipe(usb_dev, 0x1),
Daniel Mack1c8470c2013-03-03 20:46:21 +0100444 cdev->ep1_in_buf, EP1_BUFSIZE,
445 usb_ep1_command_reply_dispatch, cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200446
Daniel Mack1c8470c2013-03-03 20:46:21 +0100447 usb_fill_bulk_urb(&cdev->midi_out_urb, usb_dev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200448 usb_sndbulkpipe(usb_dev, 0x1),
Daniel Mack1c8470c2013-03-03 20:46:21 +0100449 cdev->midi_out_buf, EP1_BUFSIZE,
450 snd_usb_caiaq_midi_output_done, cdev);
Daniel Mack9318dce2009-06-01 21:36:23 +0200451
Takashi Iwai58fc7f72017-10-04 15:07:21 +0200452 /* sanity checks of EPs before actually submitting */
453 if (usb_urb_ep_type_check(&cdev->ep1_in_urb) ||
454 usb_urb_ep_type_check(&cdev->midi_out_urb)) {
455 dev_err(dev, "invalid EPs\n");
456 return -EINVAL;
457 }
458
Daniel Mack1c8470c2013-03-03 20:46:21 +0100459 init_waitqueue_head(&cdev->ep1_wait_queue);
460 init_waitqueue_head(&cdev->prepare_wait_queue);
Daniel Mack9318dce2009-06-01 21:36:23 +0200461
Daniel Mack1c8470c2013-03-03 20:46:21 +0100462 if (usb_submit_urb(&cdev->ep1_in_urb, GFP_KERNEL) != 0)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200463 return -EIO;
464
Daniel Mack1c8470c2013-03-03 20:46:21 +0100465 err = snd_usb_caiaq_send_command(cdev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200466 if (err)
Takashi Iwai99fee5082017-10-11 16:39:02 +0200467 goto err_kill_urb;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200468
Takashi Iwai99fee5082017-10-11 16:39:02 +0200469 if (!wait_event_timeout(cdev->ep1_wait_queue, cdev->spec_received, HZ)) {
470 err = -ENODEV;
471 goto err_kill_urb;
472 }
Daniel Mack523f1dc2007-03-26 19:11:24 +0200473
474 usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
Daniel Mack1c8470c2013-03-03 20:46:21 +0100475 cdev->vendor_name, CAIAQ_USB_STR_LEN);
Daniel Mack9318dce2009-06-01 21:36:23 +0200476
Daniel Mack523f1dc2007-03-26 19:11:24 +0200477 usb_string(usb_dev, usb_dev->descriptor.iProduct,
Daniel Mack1c8470c2013-03-03 20:46:21 +0100478 cdev->product_name, CAIAQ_USB_STR_LEN);
Daniel Mack9318dce2009-06-01 21:36:23 +0200479
Daniel Mackd3873a12009-06-01 21:36:25 +0200480 strlcpy(card->driver, MODNAME, sizeof(card->driver));
Daniel Mack1c8470c2013-03-03 20:46:21 +0100481 strlcpy(card->shortname, cdev->product_name, sizeof(card->shortname));
482 strlcpy(card->mixername, cdev->product_name, sizeof(card->mixername));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200483
Daniel Mackbafeee52009-06-02 12:36:39 +0200484 /* if the id was not passed as module option, fill it with a shortened
485 * version of the product string which does not contain any
486 * whitespaces */
487
488 if (*card->id == '\0') {
489 char id[sizeof(card->id)];
490
491 memset(id, 0, sizeof(id));
492
493 for (c = card->shortname, len = 0;
494 *c && len < sizeof(card->id); c++)
495 if (*c != ' ')
496 id[len++] = *c;
497
498 snd_card_set_id(card, id);
499 }
500
Daniel Mack1a1df6f2009-06-01 21:36:26 +0200501 usb_make_path(usb_dev, usbpath, sizeof(usbpath));
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100502 snprintf(card->longname, sizeof(card->longname), "%s %s (%s)",
Daniel Mack1c8470c2013-03-03 20:46:21 +0100503 cdev->vendor_name, cdev->product_name, usbpath);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200504
Daniel Mack1c8470c2013-03-03 20:46:21 +0100505 setup_card(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200506 return 0;
Takashi Iwai99fee5082017-10-11 16:39:02 +0200507
508 err_kill_urb:
509 usb_kill_urb(&cdev->ep1_in_urb);
510 return err;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200511}
512
Bill Pemberton14c56702012-12-06 12:35:29 -0500513static int snd_probe(struct usb_interface *intf,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200514 const struct usb_device_id *id)
515{
516 int ret;
Daniel Mackda185442012-06-18 21:16:31 +0200517 struct snd_card *card = NULL;
Daniel Mack1c8470c2013-03-03 20:46:21 +0100518 struct usb_device *usb_dev = interface_to_usbdev(intf);
Daniel Mack9318dce2009-06-01 21:36:23 +0200519
Daniel Mack1c8470c2013-03-03 20:46:21 +0100520 ret = create_card(usb_dev, intf, &card);
Daniel Mack9318dce2009-06-01 21:36:23 +0200521
Takashi Iwai51721f72008-12-28 16:55:08 +0100522 if (ret < 0)
523 return ret;
Daniel Mack9318dce2009-06-01 21:36:23 +0200524
Julia Lawallf4e97492009-01-01 18:14:35 +0100525 usb_set_intfdata(intf, card);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200526 ret = init_card(caiaqdev(card));
527 if (ret < 0) {
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100528 dev_err(&usb_dev->dev, "unable to init card! (ret=%d)\n", ret);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200529 snd_card_free(card);
530 return ret;
531 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200532
Daniel Mack523f1dc2007-03-26 19:11:24 +0200533 return 0;
534}
535
536static void snd_disconnect(struct usb_interface *intf)
537{
Julia Lawallf4e97492009-01-01 18:14:35 +0100538 struct snd_card *card = usb_get_intfdata(intf);
Daniel Mack2dad9402013-03-04 12:50:05 +0100539 struct device *dev = intf->usb_dev;
540 struct snd_usb_caiaqdev *cdev;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200541
542 if (!card)
543 return;
544
Daniel Mack2dad9402013-03-04 12:50:05 +0100545 cdev = caiaqdev(card);
Daniel Mackf1f6b8f2013-03-03 20:46:22 +0100546 dev_dbg(dev, "%s(%p)\n", __func__, intf);
547
Daniel Mack523f1dc2007-03-26 19:11:24 +0200548 snd_card_disconnect(card);
549
550#ifdef CONFIG_SND_USB_CAIAQ_INPUT
Daniel Mack1c8470c2013-03-03 20:46:21 +0100551 snd_usb_caiaq_input_free(cdev);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200552#endif
Daniel Mack1c8470c2013-03-03 20:46:21 +0100553 snd_usb_caiaq_audio_free(cdev);
Daniel Mack9318dce2009-06-01 21:36:23 +0200554
Daniel Mack1c8470c2013-03-03 20:46:21 +0100555 usb_kill_urb(&cdev->ep1_in_urb);
556 usb_kill_urb(&cdev->midi_out_urb);
Daniel Mack9318dce2009-06-01 21:36:23 +0200557
Daniel Mack523f1dc2007-03-26 19:11:24 +0200558 snd_card_free(card);
559 usb_reset_device(interface_to_usbdev(intf));
560}
561
562
563MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
564static struct usb_driver snd_usb_driver = {
565 .name = MODNAME,
566 .probe = snd_probe,
567 .disconnect = snd_disconnect,
568 .id_table = snd_usb_id_table,
569};
570
Greg Kroah-Hartman424f0752011-11-18 09:50:44 -0800571module_usb_driver(snd_usb_driver);