Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
523f1dce DM |
2 | /* |
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> | |
523f1dce DM |
7 | */ |
8 | ||
523f1dce | 9 | #include <linux/moduleparam.h> |
f1f6b8f6 | 10 | #include <linux/device.h> |
523f1dce | 11 | #include <linux/interrupt.h> |
e431cf45 DM |
12 | #include <linux/module.h> |
13 | #include <linux/init.h> | |
5a0e3ad6 | 14 | #include <linux/gfp.h> |
523f1dce | 15 | #include <linux/usb.h> |
523f1dce | 16 | #include <sound/initval.h> |
e431cf45 | 17 | #include <sound/core.h> |
523f1dce | 18 | #include <sound/pcm.h> |
523f1dce | 19 | |
936e7d03 DM |
20 | #include "device.h" |
21 | #include "audio.h" | |
22 | #include "midi.h" | |
23 | #include "control.h" | |
24 | #include "input.h" | |
523f1dce DM |
25 | |
26 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); | |
6008fd5a | 27 | MODULE_DESCRIPTION("caiaq USB audio"); |
523f1dce | 28 | MODULE_LICENSE("GPL"); |
0af49ffe AO |
29 | MODULE_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}}"); | |
523f1dce DM |
43 | |
44 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */ | |
45 | static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */ | |
a67ff6a5 | 46 | static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */ |
523f1dce DM |
47 | |
48 | module_param_array(index, int, NULL, 0444); | |
49 | MODULE_PARM_DESC(index, "Index value for the caiaq sound device"); | |
50 | module_param_array(id, charp, NULL, 0444); | |
51 | MODULE_PARM_DESC(id, "ID string for the caiaq soundcard."); | |
52 | module_param_array(enable, bool, NULL, 0444); | |
53 | MODULE_PARM_DESC(enable, "Enable the caiaq soundcard."); | |
54 | ||
55 | enum { | |
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 | ||
64 | enum { | |
65 | DEPTH_NONE = 0, | |
66 | DEPTH_16 = 1, | |
67 | DEPTH_24 = 2, | |
68 | DEPTH_32 = 3 | |
69 | }; | |
70 | ||
57ee11ea | 71 | static const struct usb_device_id snd_usb_id_table[] = { |
523f1dce DM |
72 | { |
73 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, | |
74 | .idVendor = USB_VID_NATIVEINSTRUMENTS, | |
9318dce5 | 75 | .idProduct = USB_PID_RIGKONTROL2 |
523f1dce | 76 | }, |
ad1e34b5 DM |
77 | { |
78 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, | |
79 | .idVendor = USB_VID_NATIVEINSTRUMENTS, | |
80 | .idProduct = USB_PID_RIGKONTROL3 | |
81 | }, | |
523f1dce DM |
82 | { |
83 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, | |
84 | .idVendor = USB_VID_NATIVEINSTRUMENTS, | |
85 | .idProduct = USB_PID_KORECONTROLLER | |
86 | }, | |
7829d0ec DM |
87 | { |
88 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, | |
89 | .idVendor = USB_VID_NATIVEINSTRUMENTS, | |
90 | .idProduct = USB_PID_KORECONTROLLER2 | |
91 | }, | |
523f1dce DM |
92 | { |
93 | .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 | }, | |
f3e9d5d1 DM |
102 | { |
103 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, | |
104 | .idVendor = USB_VID_NATIVEINSTRUMENTS, | |
105 | .idProduct = USB_PID_SESSIONIO | |
106 | }, | |
2165592b DM |
107 | { |
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 | }, | |
b30c4947 DM |
117 | { |
118 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, | |
119 | .idVendor = USB_VID_NATIVEINSTRUMENTS, | |
120 | .idProduct = USB_PID_AUDIO2DJ | |
121 | }, | |
6da7a2aa DM |
122 | { |
123 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, | |
124 | .idVendor = USB_VID_NATIVEINSTRUMENTS, | |
125 | .idProduct = USB_PID_TRAKTORKONTROLX1 | |
126 | }, | |
15c5ab60 DM |
127 | { |
128 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, | |
129 | .idVendor = USB_VID_NATIVEINSTRUMENTS, | |
130 | .idProduct = USB_PID_TRAKTORKONTROLS4 | |
131 | }, | |
df8d81a3 DM |
132 | { |
133 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, | |
134 | .idVendor = USB_VID_NATIVEINSTRUMENTS, | |
135 | .idProduct = USB_PID_TRAKTORAUDIO2 | |
136 | }, | |
e653510a WL |
137 | { |
138 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, | |
139 | .idVendor = USB_VID_NATIVEINSTRUMENTS, | |
140 | .idProduct = USB_PID_MASCHINECONTROLLER | |
141 | }, | |
523f1dce DM |
142 | { /* terminator */ } |
143 | }; | |
144 | ||
145 | static void usb_ep1_command_reply_dispatch (struct urb* urb) | |
146 | { | |
147 | int ret; | |
2dad9402 | 148 | struct device *dev = &urb->dev->dev; |
1c8470ce | 149 | struct snd_usb_caiaqdev *cdev = urb->context; |
523f1dce DM |
150 | unsigned char *buf = urb->transfer_buffer; |
151 | ||
1c8470ce | 152 | if (urb->status || !cdev) { |
f1f6b8f6 | 153 | dev_warn(dev, "received EP1 urb->status = %i\n", urb->status); |
523f1dce DM |
154 | return; |
155 | } | |
156 | ||
157 | switch(buf[0]) { | |
158 | case EP1_CMD_GET_DEVICE_INFO: | |
1c8470ce DM |
159 | memcpy(&cdev->spec, buf+1, sizeof(struct caiaq_device_spec)); |
160 | cdev->spec.fw_version = le16_to_cpu(cdev->spec.fw_version); | |
f1f6b8f6 | 161 | dev_dbg(dev, "device spec (firmware %d): audio: %d in, %d out, " |
523f1dce | 162 | "MIDI: %d in, %d out, data alignment %d\n", |
1c8470ce DM |
163 | 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); | |
169 | ||
170 | cdev->spec_received++; | |
171 | wake_up(&cdev->ep1_wait_queue); | |
523f1dce DM |
172 | break; |
173 | case EP1_CMD_AUDIO_PARAMS: | |
1c8470ce DM |
174 | cdev->audio_parm_answer = buf[1]; |
175 | wake_up(&cdev->ep1_wait_queue); | |
523f1dce DM |
176 | break; |
177 | case EP1_CMD_MIDI_READ: | |
1c8470ce | 178 | snd_usb_caiaq_midi_handle_input(cdev, buf[1], buf + 3, buf[2]); |
523f1dce | 179 | break; |
8e3cd08e | 180 | case EP1_CMD_READ_IO: |
1c8470ce | 181 | if (cdev->chip.usb_id == |
8e3cd08e | 182 | USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) { |
1c8470ce DM |
183 | 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); | |
8e3cd08e DM |
187 | break; |
188 | } | |
523f1dce | 189 | #ifdef CONFIG_SND_USB_CAIAQ_INPUT |
590b516e | 190 | /* fall through */ |
523f1dce DM |
191 | case EP1_CMD_READ_ERP: |
192 | case EP1_CMD_READ_ANALOG: | |
1c8470ce | 193 | snd_usb_caiaq_input_dispatch(cdev, buf, urb->actual_length); |
523f1dce | 194 | #endif |
8e3cd08e | 195 | break; |
523f1dce DM |
196 | } |
197 | ||
1c8470ce DM |
198 | cdev->ep1_in_urb.actual_length = 0; |
199 | ret = usb_submit_urb(&cdev->ep1_in_urb, GFP_ATOMIC); | |
523f1dce | 200 | if (ret < 0) |
f1f6b8f6 | 201 | dev_err(dev, "unable to submit urb. OOM!?\n"); |
523f1dce DM |
202 | } |
203 | ||
1c8470ce | 204 | int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *cdev, |
8e3cd08e DM |
205 | unsigned char command, |
206 | const unsigned char *buffer, | |
207 | int len) | |
523f1dce DM |
208 | { |
209 | int actual_len; | |
1c8470ce | 210 | struct usb_device *usb_dev = cdev->chip.dev; |
523f1dce DM |
211 | |
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) | |
1c8470ce | 219 | memcpy(cdev->ep1_out_buf+1, buffer, len); |
9318dce5 | 220 | |
1c8470ce | 221 | cdev->ep1_out_buf[0] = command; |
523f1dce | 222 | return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1), |
1c8470ce | 223 | cdev->ep1_out_buf, len+1, &actual_len, 200); |
523f1dce DM |
224 | } |
225 | ||
d2724de1 HG |
226 | int 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 | ||
1c8470ce | 251 | int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *cdev, |
523f1dce DM |
252 | int rate, int depth, int bpp) |
253 | { | |
254 | int ret; | |
255 | char tmp[5]; | |
f1f6b8f6 | 256 | struct device *dev = caiaqdev_to_dev(cdev); |
9318dce5 | 257 | |
523f1dce DM |
258 | 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 | ||
f1f6b8f6 | 277 | dev_dbg(dev, "setting audio params: %d Hz, %d bits, %d bpp\n", |
523f1dce DM |
278 | rate, depth, bpp); |
279 | ||
1c8470ce DM |
280 | cdev->audio_parm_answer = -1; |
281 | ret = snd_usb_caiaq_send_command(cdev, EP1_CMD_AUDIO_PARAMS, | |
8e3cd08e | 282 | tmp, sizeof(tmp)); |
523f1dce DM |
283 | |
284 | if (ret) | |
285 | return ret; | |
9318dce5 | 286 | |
1c8470ce DM |
287 | if (!wait_event_timeout(cdev->ep1_wait_queue, |
288 | cdev->audio_parm_answer >= 0, HZ)) | |
523f1dce | 289 | return -EPIPE; |
9318dce5 | 290 | |
1c8470ce | 291 | if (cdev->audio_parm_answer != 1) |
f1f6b8f6 | 292 | dev_dbg(dev, "unable to set the device's audio params\n"); |
9311c9b4 | 293 | else |
1c8470ce | 294 | cdev->bpp = bpp; |
523f1dce | 295 | |
1c8470ce | 296 | return cdev->audio_parm_answer == 1 ? 0 : -EINVAL; |
523f1dce DM |
297 | } |
298 | ||
1c8470ce | 299 | int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev *cdev, |
9318dce5 | 300 | int digital, int analog, int erp) |
523f1dce DM |
301 | { |
302 | char tmp[3] = { digital, analog, erp }; | |
1c8470ce | 303 | return snd_usb_caiaq_send_command(cdev, EP1_CMD_AUTO_MSG, |
8e3cd08e | 304 | tmp, sizeof(tmp)); |
523f1dce DM |
305 | } |
306 | ||
1c8470ce | 307 | static void setup_card(struct snd_usb_caiaqdev *cdev) |
523f1dce DM |
308 | { |
309 | int ret; | |
ad1e34b5 | 310 | char val[4]; |
f1f6b8f6 | 311 | struct device *dev = caiaqdev_to_dev(cdev); |
9318dce5 | 312 | |
523f1dce | 313 | /* device-specific startup specials */ |
1c8470ce | 314 | switch (cdev->chip.usb_id) { |
523f1dce DM |
315 | 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; | |
1c8470ce | 320 | snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 3); |
523f1dce | 321 | break; |
ad1e34b5 DM |
322 | 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; | |
1c8470ce | 328 | snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 4); |
ad1e34b5 | 329 | break; |
523f1dce DM |
330 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1): |
331 | /* Audio Kontrol 1 - make USB-LED stop blinking */ | |
332 | val[0] = 0x00; | |
1c8470ce | 333 | snd_usb_caiaq_send_command(cdev, EP1_CMD_WRITE_IO, val, 1); |
8e3cd08e DM |
334 | break; |
335 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ): | |
336 | /* Audio 8 DJ - trigger read of current settings */ | |
1c8470ce DM |
337 | 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); | |
8e3cd08e | 340 | |
1c8470ce DM |
341 | if (!wait_event_timeout(cdev->ep1_wait_queue, |
342 | cdev->control_state[0] != 0xff, HZ)) | |
8e3cd08e DM |
343 | return; |
344 | ||
345 | /* fix up some defaults */ | |
1c8470ce DM |
346 | 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); | |
8e3cd08e DM |
354 | } |
355 | ||
523f1dce DM |
356 | break; |
357 | } | |
9318dce5 | 358 | |
1c8470ce DM |
359 | 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); | |
7829d0ec | 364 | if (ret < 0) |
f1f6b8f6 | 365 | dev_err(dev, "Unable to set up audio system (ret=%d)\n", ret); |
7829d0ec | 366 | } |
9318dce5 | 367 | |
1c8470ce DM |
368 | if (cdev->spec.num_midi_in + |
369 | cdev->spec.num_midi_out > 0) { | |
370 | ret = snd_usb_caiaq_midi_init(cdev); | |
7829d0ec | 371 | if (ret < 0) |
f1f6b8f6 | 372 | dev_err(dev, "Unable to set up MIDI system (ret=%d)\n", ret); |
7829d0ec | 373 | } |
523f1dce DM |
374 | |
375 | #ifdef CONFIG_SND_USB_CAIAQ_INPUT | |
1c8470ce | 376 | ret = snd_usb_caiaq_input_init(cdev); |
523f1dce | 377 | if (ret < 0) |
f1f6b8f6 | 378 | dev_err(dev, "Unable to set up input system (ret=%d)\n", ret); |
523f1dce DM |
379 | #endif |
380 | ||
381 | /* finally, register the card and all its sub-instances */ | |
1c8470ce | 382 | ret = snd_card_register(cdev->chip.card); |
523f1dce | 383 | if (ret < 0) { |
f1f6b8f6 | 384 | dev_err(dev, "snd_card_register() returned %d\n", ret); |
1c8470ce | 385 | snd_card_free(cdev->chip.card); |
523f1dce | 386 | } |
8e3cd08e | 387 | |
1c8470ce | 388 | ret = snd_usb_caiaq_control_init(cdev); |
8e3cd08e | 389 | if (ret < 0) |
f1f6b8f6 | 390 | dev_err(dev, "Unable to set up control system (ret=%d)\n", ret); |
523f1dce DM |
391 | } |
392 | ||
563c2bf5 DM |
393 | static int create_card(struct usb_device *usb_dev, |
394 | struct usb_interface *intf, | |
395 | struct snd_card **cardp) | |
523f1dce DM |
396 | { |
397 | int devnum; | |
bd7dd77c | 398 | int err; |
523f1dce | 399 | struct snd_card *card; |
1c8470ce | 400 | struct snd_usb_caiaqdev *cdev; |
523f1dce DM |
401 | |
402 | for (devnum = 0; devnum < SNDRV_CARDS; devnum++) | |
3dd446a7 | 403 | if (enable[devnum]) |
523f1dce DM |
404 | break; |
405 | ||
406 | if (devnum >= SNDRV_CARDS) | |
51721f70 | 407 | return -ENODEV; |
523f1dce | 408 | |
874b8d42 TI |
409 | err = snd_card_new(&intf->dev, |
410 | index[devnum], id[devnum], THIS_MODULE, | |
411 | sizeof(struct snd_usb_caiaqdev), &card); | |
bd7dd77c | 412 | if (err < 0) |
51721f70 | 413 | return err; |
523f1dce | 414 | |
1c8470ce DM |
415 | 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), | |
8c5330a5 | 419 | le16_to_cpu(usb_dev->descriptor.idProduct)); |
1c8470ce | 420 | spin_lock_init(&cdev->spinlock); |
523f1dce | 421 | |
51721f70 TI |
422 | *cardp = card; |
423 | return 0; | |
523f1dce DM |
424 | } |
425 | ||
1c8470ce | 426 | static int init_card(struct snd_usb_caiaqdev *cdev) |
523f1dce | 427 | { |
bafeee5b | 428 | char *c, usbpath[32]; |
1c8470ce DM |
429 | struct usb_device *usb_dev = cdev->chip.dev; |
430 | struct snd_card *card = cdev->chip.card; | |
f1f6b8f6 | 431 | struct device *dev = caiaqdev_to_dev(cdev); |
bafeee5b | 432 | int err, len; |
9318dce5 | 433 | |
523f1dce | 434 | if (usb_set_interface(usb_dev, 0, 1) != 0) { |
f1f6b8f6 | 435 | dev_err(dev, "can't set alt interface.\n"); |
523f1dce DM |
436 | return -EIO; |
437 | } | |
438 | ||
1c8470ce DM |
439 | usb_init_urb(&cdev->ep1_in_urb); |
440 | usb_init_urb(&cdev->midi_out_urb); | |
523f1dce | 441 | |
1c8470ce | 442 | usb_fill_bulk_urb(&cdev->ep1_in_urb, usb_dev, |
523f1dce | 443 | usb_rcvbulkpipe(usb_dev, 0x1), |
1c8470ce DM |
444 | cdev->ep1_in_buf, EP1_BUFSIZE, |
445 | usb_ep1_command_reply_dispatch, cdev); | |
523f1dce | 446 | |
1c8470ce | 447 | usb_fill_bulk_urb(&cdev->midi_out_urb, usb_dev, |
523f1dce | 448 | usb_sndbulkpipe(usb_dev, 0x1), |
1c8470ce DM |
449 | cdev->midi_out_buf, EP1_BUFSIZE, |
450 | snd_usb_caiaq_midi_output_done, cdev); | |
9318dce5 | 451 | |
58fc7f73 TI |
452 | /* 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 | ||
1c8470ce DM |
459 | init_waitqueue_head(&cdev->ep1_wait_queue); |
460 | init_waitqueue_head(&cdev->prepare_wait_queue); | |
9318dce5 | 461 | |
1c8470ce | 462 | if (usb_submit_urb(&cdev->ep1_in_urb, GFP_KERNEL) != 0) |
523f1dce DM |
463 | return -EIO; |
464 | ||
1c8470ce | 465 | err = snd_usb_caiaq_send_command(cdev, EP1_CMD_GET_DEVICE_INFO, NULL, 0); |
523f1dce | 466 | if (err) |
99fee508 | 467 | goto err_kill_urb; |
523f1dce | 468 | |
99fee508 TI |
469 | if (!wait_event_timeout(cdev->ep1_wait_queue, cdev->spec_received, HZ)) { |
470 | err = -ENODEV; | |
471 | goto err_kill_urb; | |
472 | } | |
523f1dce DM |
473 | |
474 | usb_string(usb_dev, usb_dev->descriptor.iManufacturer, | |
1c8470ce | 475 | cdev->vendor_name, CAIAQ_USB_STR_LEN); |
9318dce5 | 476 | |
523f1dce | 477 | usb_string(usb_dev, usb_dev->descriptor.iProduct, |
1c8470ce | 478 | cdev->product_name, CAIAQ_USB_STR_LEN); |
9318dce5 | 479 | |
d3873a1b | 480 | strlcpy(card->driver, MODNAME, sizeof(card->driver)); |
1c8470ce DM |
481 | strlcpy(card->shortname, cdev->product_name, sizeof(card->shortname)); |
482 | strlcpy(card->mixername, cdev->product_name, sizeof(card->mixername)); | |
523f1dce | 483 | |
bafeee5b DM |
484 | /* 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 | ||
1a1df6f0 | 501 | usb_make_path(usb_dev, usbpath, sizeof(usbpath)); |
f1f6b8f6 | 502 | snprintf(card->longname, sizeof(card->longname), "%s %s (%s)", |
1c8470ce | 503 | cdev->vendor_name, cdev->product_name, usbpath); |
523f1dce | 504 | |
1c8470ce | 505 | setup_card(cdev); |
523f1dce | 506 | return 0; |
99fee508 TI |
507 | |
508 | err_kill_urb: | |
509 | usb_kill_urb(&cdev->ep1_in_urb); | |
510 | return err; | |
523f1dce DM |
511 | } |
512 | ||
14c56706 | 513 | static int snd_probe(struct usb_interface *intf, |
523f1dce DM |
514 | const struct usb_device_id *id) |
515 | { | |
516 | int ret; | |
da185443 | 517 | struct snd_card *card = NULL; |
1c8470ce | 518 | struct usb_device *usb_dev = interface_to_usbdev(intf); |
9318dce5 | 519 | |
1c8470ce | 520 | ret = create_card(usb_dev, intf, &card); |
9318dce5 | 521 | |
51721f70 TI |
522 | if (ret < 0) |
523 | return ret; | |
9318dce5 | 524 | |
f4e9749f | 525 | usb_set_intfdata(intf, card); |
523f1dce DM |
526 | ret = init_card(caiaqdev(card)); |
527 | if (ret < 0) { | |
f1f6b8f6 | 528 | dev_err(&usb_dev->dev, "unable to init card! (ret=%d)\n", ret); |
523f1dce DM |
529 | snd_card_free(card); |
530 | return ret; | |
531 | } | |
9318dce5 | 532 | |
523f1dce DM |
533 | return 0; |
534 | } | |
535 | ||
536 | static void snd_disconnect(struct usb_interface *intf) | |
537 | { | |
f4e9749f | 538 | struct snd_card *card = usb_get_intfdata(intf); |
2dad9402 DM |
539 | struct device *dev = intf->usb_dev; |
540 | struct snd_usb_caiaqdev *cdev; | |
523f1dce DM |
541 | |
542 | if (!card) | |
543 | return; | |
544 | ||
2dad9402 | 545 | cdev = caiaqdev(card); |
f1f6b8f6 DM |
546 | dev_dbg(dev, "%s(%p)\n", __func__, intf); |
547 | ||
523f1dce DM |
548 | snd_card_disconnect(card); |
549 | ||
550 | #ifdef CONFIG_SND_USB_CAIAQ_INPUT | |
1c8470ce | 551 | snd_usb_caiaq_input_free(cdev); |
523f1dce | 552 | #endif |
1c8470ce | 553 | snd_usb_caiaq_audio_free(cdev); |
9318dce5 | 554 | |
1c8470ce DM |
555 | usb_kill_urb(&cdev->ep1_in_urb); |
556 | usb_kill_urb(&cdev->midi_out_urb); | |
9318dce5 | 557 | |
523f1dce DM |
558 | snd_card_free(card); |
559 | usb_reset_device(interface_to_usbdev(intf)); | |
560 | } | |
561 | ||
562 | ||
563 | MODULE_DEVICE_TABLE(usb, snd_usb_id_table); | |
564 | static 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 | ||
424f0750 | 571 | module_usb_driver(snd_usb_driver); |