Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 1 | /* |
| 2 | * caiaq.c: ALSA driver for caiaq/NativeInstruments devices |
| 3 | * |
| 4 | * Copyright (c) 2007 Daniel Mack <daniel@caiaq.de> |
| 5 | * Karsten Wiese <fzu@wemgehoertderstaat.de> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 22 | #include <linux/moduleparam.h> |
| 23 | #include <linux/interrupt.h> |
Daniel Mack | e431cf4 | 2009-03-28 21:19:49 +0100 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame^] | 26 | #include <linux/gfp.h> |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 27 | #include <linux/usb.h> |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 28 | #include <sound/initval.h> |
Daniel Mack | e431cf4 | 2009-03-28 21:19:49 +0100 | [diff] [blame] | 29 | #include <sound/core.h> |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 30 | #include <sound/pcm.h> |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 31 | |
Daniel Mack | 936e7d0 | 2009-04-01 19:05:39 +0200 | [diff] [blame] | 32 | #include "device.h" |
| 33 | #include "audio.h" |
| 34 | #include "midi.h" |
| 35 | #include "control.h" |
| 36 | #include "input.h" |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 37 | |
| 38 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); |
Mark Hills | 467cc16 | 2009-10-24 12:59:37 +0100 | [diff] [blame] | 39 | MODULE_DESCRIPTION("caiaq USB audio, version 1.3.20"); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 40 | MODULE_LICENSE("GPL"); |
| 41 | MODULE_SUPPORTED_DEVICE("{{Native Instruments, RigKontrol2}," |
Daniel Mack | ad1e34b | 2007-09-17 14:45:14 +0200 | [diff] [blame] | 42 | "{Native Instruments, RigKontrol3}," |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 43 | "{Native Instruments, Kore Controller}," |
Daniel Mack | 7829d0e | 2007-11-26 09:00:56 +0100 | [diff] [blame] | 44 | "{Native Instruments, Kore Controller 2}," |
Daniel Mack | f3e9d5d | 2008-05-08 15:42:15 +0200 | [diff] [blame] | 45 | "{Native Instruments, Audio Kontrol 1}," |
Daniel Mack | b30c494 | 2009-07-22 14:13:35 +0200 | [diff] [blame] | 46 | "{Native Instruments, Audio 2 DJ}," |
Daniel Mack | 2165592 | 2009-01-16 11:03:19 +0100 | [diff] [blame] | 47 | "{Native Instruments, Audio 4 DJ}," |
Daniel Mack | f3e9d5d | 2008-05-08 15:42:15 +0200 | [diff] [blame] | 48 | "{Native Instruments, Audio 8 DJ}," |
Daniel Mack | 2165592 | 2009-01-16 11:03:19 +0100 | [diff] [blame] | 49 | "{Native Instruments, Session I/O}," |
| 50 | "{Native Instruments, GuitarRig mobile}"); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 51 | |
| 52 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */ |
| 53 | static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */ |
| 54 | static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */ |
| 55 | static int snd_card_used[SNDRV_CARDS]; |
| 56 | |
| 57 | module_param_array(index, int, NULL, 0444); |
| 58 | MODULE_PARM_DESC(index, "Index value for the caiaq sound device"); |
| 59 | module_param_array(id, charp, NULL, 0444); |
| 60 | MODULE_PARM_DESC(id, "ID string for the caiaq soundcard."); |
| 61 | module_param_array(enable, bool, NULL, 0444); |
| 62 | MODULE_PARM_DESC(enable, "Enable the caiaq soundcard."); |
| 63 | |
| 64 | enum { |
| 65 | SAMPLERATE_44100 = 0, |
| 66 | SAMPLERATE_48000 = 1, |
| 67 | SAMPLERATE_96000 = 2, |
| 68 | SAMPLERATE_192000 = 3, |
| 69 | SAMPLERATE_88200 = 4, |
| 70 | SAMPLERATE_INVALID = 0xff |
| 71 | }; |
| 72 | |
| 73 | enum { |
| 74 | DEPTH_NONE = 0, |
| 75 | DEPTH_16 = 1, |
| 76 | DEPTH_24 = 2, |
| 77 | DEPTH_32 = 3 |
| 78 | }; |
| 79 | |
| 80 | static struct usb_device_id snd_usb_id_table[] = { |
| 81 | { |
| 82 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 83 | .idVendor = USB_VID_NATIVEINSTRUMENTS, |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 84 | .idProduct = USB_PID_RIGKONTROL2 |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 85 | }, |
| 86 | { |
| 87 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 88 | .idVendor = USB_VID_NATIVEINSTRUMENTS, |
Daniel Mack | ad1e34b | 2007-09-17 14:45:14 +0200 | [diff] [blame] | 89 | .idProduct = USB_PID_RIGKONTROL3 |
| 90 | }, |
| 91 | { |
| 92 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 93 | .idVendor = USB_VID_NATIVEINSTRUMENTS, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 94 | .idProduct = USB_PID_KORECONTROLLER |
| 95 | }, |
| 96 | { |
Daniel Mack | 7829d0e | 2007-11-26 09:00:56 +0100 | [diff] [blame] | 97 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 98 | .idVendor = USB_VID_NATIVEINSTRUMENTS, |
| 99 | .idProduct = USB_PID_KORECONTROLLER2 |
| 100 | }, |
| 101 | { |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 102 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 103 | .idVendor = USB_VID_NATIVEINSTRUMENTS, |
| 104 | .idProduct = USB_PID_AK1 |
| 105 | }, |
| 106 | { |
| 107 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 108 | .idVendor = USB_VID_NATIVEINSTRUMENTS, |
| 109 | .idProduct = USB_PID_AUDIO8DJ |
| 110 | }, |
Daniel Mack | f3e9d5d | 2008-05-08 15:42:15 +0200 | [diff] [blame] | 111 | { |
| 112 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 113 | .idVendor = USB_VID_NATIVEINSTRUMENTS, |
| 114 | .idProduct = USB_PID_SESSIONIO |
| 115 | }, |
Daniel Mack | 2165592 | 2009-01-16 11:03:19 +0100 | [diff] [blame] | 116 | { |
| 117 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 118 | .idVendor = USB_VID_NATIVEINSTRUMENTS, |
| 119 | .idProduct = USB_PID_GUITARRIGMOBILE |
| 120 | }, |
| 121 | { |
| 122 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 123 | .idVendor = USB_VID_NATIVEINSTRUMENTS, |
| 124 | .idProduct = USB_PID_AUDIO4DJ |
| 125 | }, |
Daniel Mack | b30c494 | 2009-07-22 14:13:35 +0200 | [diff] [blame] | 126 | { |
| 127 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 128 | .idVendor = USB_VID_NATIVEINSTRUMENTS, |
| 129 | .idProduct = USB_PID_AUDIO2DJ |
| 130 | }, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 131 | { /* terminator */ } |
| 132 | }; |
| 133 | |
| 134 | static void usb_ep1_command_reply_dispatch (struct urb* urb) |
| 135 | { |
| 136 | int ret; |
| 137 | struct snd_usb_caiaqdev *dev = urb->context; |
| 138 | unsigned char *buf = urb->transfer_buffer; |
| 139 | |
| 140 | if (urb->status || !dev) { |
| 141 | log("received EP1 urb->status = %i\n", urb->status); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | switch(buf[0]) { |
| 146 | case EP1_CMD_GET_DEVICE_INFO: |
| 147 | memcpy(&dev->spec, buf+1, sizeof(struct caiaq_device_spec)); |
| 148 | dev->spec.fw_version = le16_to_cpu(dev->spec.fw_version); |
| 149 | debug("device spec (firmware %d): audio: %d in, %d out, " |
| 150 | "MIDI: %d in, %d out, data alignment %d\n", |
| 151 | dev->spec.fw_version, |
| 152 | dev->spec.num_analog_audio_in, |
| 153 | dev->spec.num_analog_audio_out, |
| 154 | dev->spec.num_midi_in, |
| 155 | dev->spec.num_midi_out, |
| 156 | dev->spec.data_alignment); |
| 157 | |
| 158 | dev->spec_received++; |
| 159 | wake_up(&dev->ep1_wait_queue); |
| 160 | break; |
| 161 | case EP1_CMD_AUDIO_PARAMS: |
| 162 | dev->audio_parm_answer = buf[1]; |
| 163 | wake_up(&dev->ep1_wait_queue); |
| 164 | break; |
| 165 | case EP1_CMD_MIDI_READ: |
| 166 | snd_usb_caiaq_midi_handle_input(dev, buf[1], buf + 3, buf[2]); |
| 167 | break; |
Daniel Mack | 8e3cd08 | 2007-11-22 11:40:04 +0100 | [diff] [blame] | 168 | case EP1_CMD_READ_IO: |
| 169 | if (dev->chip.usb_id == |
| 170 | USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ)) { |
| 171 | if (urb->actual_length > sizeof(dev->control_state)) |
| 172 | urb->actual_length = sizeof(dev->control_state); |
| 173 | memcpy(dev->control_state, buf + 1, urb->actual_length); |
| 174 | wake_up(&dev->ep1_wait_queue); |
| 175 | break; |
| 176 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 177 | #ifdef CONFIG_SND_USB_CAIAQ_INPUT |
| 178 | case EP1_CMD_READ_ERP: |
| 179 | case EP1_CMD_READ_ANALOG: |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 180 | snd_usb_caiaq_input_dispatch(dev, buf, urb->actual_length); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 181 | #endif |
Daniel Mack | 8e3cd08 | 2007-11-22 11:40:04 +0100 | [diff] [blame] | 182 | break; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | dev->ep1_in_urb.actual_length = 0; |
| 186 | ret = usb_submit_urb(&dev->ep1_in_urb, GFP_ATOMIC); |
| 187 | if (ret < 0) |
| 188 | log("unable to submit urb. OOM!?\n"); |
| 189 | } |
| 190 | |
Daniel Mack | 8e3cd08 | 2007-11-22 11:40:04 +0100 | [diff] [blame] | 191 | int snd_usb_caiaq_send_command(struct snd_usb_caiaqdev *dev, |
| 192 | unsigned char command, |
| 193 | const unsigned char *buffer, |
| 194 | int len) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 195 | { |
| 196 | int actual_len; |
| 197 | struct usb_device *usb_dev = dev->chip.dev; |
| 198 | |
| 199 | if (!usb_dev) |
| 200 | return -EIO; |
| 201 | |
| 202 | if (len > EP1_BUFSIZE - 1) |
| 203 | len = EP1_BUFSIZE - 1; |
| 204 | |
| 205 | if (buffer && len > 0) |
| 206 | memcpy(dev->ep1_out_buf+1, buffer, len); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 207 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 208 | dev->ep1_out_buf[0] = command; |
| 209 | return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1), |
| 210 | dev->ep1_out_buf, len+1, &actual_len, 200); |
| 211 | } |
| 212 | |
| 213 | int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *dev, |
| 214 | int rate, int depth, int bpp) |
| 215 | { |
| 216 | int ret; |
| 217 | char tmp[5]; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 218 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 219 | switch (rate) { |
| 220 | case 44100: tmp[0] = SAMPLERATE_44100; break; |
| 221 | case 48000: tmp[0] = SAMPLERATE_48000; break; |
| 222 | case 88200: tmp[0] = SAMPLERATE_88200; break; |
| 223 | case 96000: tmp[0] = SAMPLERATE_96000; break; |
| 224 | case 192000: tmp[0] = SAMPLERATE_192000; break; |
| 225 | default: return -EINVAL; |
| 226 | } |
| 227 | |
| 228 | switch (depth) { |
| 229 | case 16: tmp[1] = DEPTH_16; break; |
| 230 | case 24: tmp[1] = DEPTH_24; break; |
| 231 | default: return -EINVAL; |
| 232 | } |
| 233 | |
| 234 | tmp[2] = bpp & 0xff; |
| 235 | tmp[3] = bpp >> 8; |
| 236 | tmp[4] = 1; /* packets per microframe */ |
| 237 | |
| 238 | debug("setting audio params: %d Hz, %d bits, %d bpp\n", |
| 239 | rate, depth, bpp); |
| 240 | |
| 241 | dev->audio_parm_answer = -1; |
Daniel Mack | 8e3cd08 | 2007-11-22 11:40:04 +0100 | [diff] [blame] | 242 | ret = snd_usb_caiaq_send_command(dev, EP1_CMD_AUDIO_PARAMS, |
| 243 | tmp, sizeof(tmp)); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 244 | |
| 245 | if (ret) |
| 246 | return ret; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 247 | |
| 248 | if (!wait_event_timeout(dev->ep1_wait_queue, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 249 | dev->audio_parm_answer >= 0, HZ)) |
| 250 | return -EPIPE; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 251 | |
| 252 | if (dev->audio_parm_answer != 1) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 253 | debug("unable to set the device's audio params\n"); |
Daniel Mack | 9311c9b | 2009-03-18 11:03:54 +0100 | [diff] [blame] | 254 | else |
| 255 | dev->bpp = bpp; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 256 | |
| 257 | return dev->audio_parm_answer == 1 ? 0 : -EINVAL; |
| 258 | } |
| 259 | |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 260 | int snd_usb_caiaq_set_auto_msg(struct snd_usb_caiaqdev *dev, |
| 261 | int digital, int analog, int erp) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 262 | { |
| 263 | char tmp[3] = { digital, analog, erp }; |
Daniel Mack | 8e3cd08 | 2007-11-22 11:40:04 +0100 | [diff] [blame] | 264 | return snd_usb_caiaq_send_command(dev, EP1_CMD_AUTO_MSG, |
| 265 | tmp, sizeof(tmp)); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Sam Ravnborg | c598195 | 2008-02-22 18:46:47 +0100 | [diff] [blame] | 268 | static void __devinit setup_card(struct snd_usb_caiaqdev *dev) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 269 | { |
| 270 | int ret; |
Daniel Mack | ad1e34b | 2007-09-17 14:45:14 +0200 | [diff] [blame] | 271 | char val[4]; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 272 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 273 | /* device-specific startup specials */ |
| 274 | switch (dev->chip.usb_id) { |
| 275 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2): |
| 276 | /* RigKontrol2 - display centered dash ('-') */ |
| 277 | val[0] = 0x00; |
| 278 | val[1] = 0x00; |
| 279 | val[2] = 0x01; |
Daniel Mack | 8e3cd08 | 2007-11-22 11:40:04 +0100 | [diff] [blame] | 280 | snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 3); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 281 | break; |
Daniel Mack | ad1e34b | 2007-09-17 14:45:14 +0200 | [diff] [blame] | 282 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3): |
| 283 | /* RigKontrol2 - display two centered dashes ('--') */ |
| 284 | val[0] = 0x00; |
| 285 | val[1] = 0x40; |
| 286 | val[2] = 0x40; |
| 287 | val[3] = 0x00; |
Daniel Mack | 8e3cd08 | 2007-11-22 11:40:04 +0100 | [diff] [blame] | 288 | snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 4); |
Daniel Mack | ad1e34b | 2007-09-17 14:45:14 +0200 | [diff] [blame] | 289 | break; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 290 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1): |
| 291 | /* Audio Kontrol 1 - make USB-LED stop blinking */ |
| 292 | val[0] = 0x00; |
Daniel Mack | 8e3cd08 | 2007-11-22 11:40:04 +0100 | [diff] [blame] | 293 | snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, val, 1); |
| 294 | break; |
| 295 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO8DJ): |
| 296 | /* Audio 8 DJ - trigger read of current settings */ |
| 297 | dev->control_state[0] = 0xff; |
| 298 | snd_usb_caiaq_set_auto_msg(dev, 1, 0, 0); |
| 299 | snd_usb_caiaq_send_command(dev, EP1_CMD_READ_IO, NULL, 0); |
| 300 | |
| 301 | if (!wait_event_timeout(dev->ep1_wait_queue, |
| 302 | dev->control_state[0] != 0xff, HZ)) |
| 303 | return; |
| 304 | |
| 305 | /* fix up some defaults */ |
| 306 | if ((dev->control_state[1] != 2) || |
| 307 | (dev->control_state[2] != 3) || |
| 308 | (dev->control_state[4] != 2)) { |
| 309 | dev->control_state[1] = 2; |
| 310 | dev->control_state[2] = 3; |
| 311 | dev->control_state[4] = 2; |
| 312 | snd_usb_caiaq_send_command(dev, |
| 313 | EP1_CMD_WRITE_IO, dev->control_state, 6); |
| 314 | } |
| 315 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 316 | break; |
Mark Hills | e3ca4c9 | 2009-02-04 22:34:31 +0000 | [diff] [blame] | 317 | case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AUDIO4DJ): |
| 318 | /* Audio 4 DJ - default input mode to phono */ |
| 319 | dev->control_state[0] = 2; |
| 320 | snd_usb_caiaq_send_command(dev, EP1_CMD_WRITE_IO, |
| 321 | dev->control_state, 1); |
| 322 | break; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 323 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 324 | |
Daniel Mack | 7829d0e | 2007-11-26 09:00:56 +0100 | [diff] [blame] | 325 | if (dev->spec.num_analog_audio_out + |
| 326 | dev->spec.num_analog_audio_in + |
| 327 | dev->spec.num_digital_audio_out + |
| 328 | dev->spec.num_digital_audio_in > 0) { |
| 329 | ret = snd_usb_caiaq_audio_init(dev); |
| 330 | if (ret < 0) |
| 331 | log("Unable to set up audio system (ret=%d)\n", ret); |
| 332 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 333 | |
Daniel Mack | 7829d0e | 2007-11-26 09:00:56 +0100 | [diff] [blame] | 334 | if (dev->spec.num_midi_in + |
| 335 | dev->spec.num_midi_out > 0) { |
| 336 | ret = snd_usb_caiaq_midi_init(dev); |
| 337 | if (ret < 0) |
| 338 | log("Unable to set up MIDI system (ret=%d)\n", ret); |
| 339 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 340 | |
| 341 | #ifdef CONFIG_SND_USB_CAIAQ_INPUT |
| 342 | ret = snd_usb_caiaq_input_init(dev); |
| 343 | if (ret < 0) |
| 344 | log("Unable to set up input system (ret=%d)\n", ret); |
| 345 | #endif |
| 346 | |
| 347 | /* finally, register the card and all its sub-instances */ |
| 348 | ret = snd_card_register(dev->chip.card); |
| 349 | if (ret < 0) { |
| 350 | log("snd_card_register() returned %d\n", ret); |
| 351 | snd_card_free(dev->chip.card); |
| 352 | } |
Daniel Mack | 8e3cd08 | 2007-11-22 11:40:04 +0100 | [diff] [blame] | 353 | |
| 354 | ret = snd_usb_caiaq_control_init(dev); |
| 355 | if (ret < 0) |
| 356 | log("Unable to set up control system (ret=%d)\n", ret); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Daniel Mack | 563c2bf | 2009-07-02 10:46:35 +0200 | [diff] [blame] | 359 | static int create_card(struct usb_device *usb_dev, |
| 360 | struct usb_interface *intf, |
| 361 | struct snd_card **cardp) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 362 | { |
| 363 | int devnum; |
Takashi Iwai | bd7dd77 | 2008-12-28 16:45:02 +0100 | [diff] [blame] | 364 | int err; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 365 | struct snd_card *card; |
| 366 | struct snd_usb_caiaqdev *dev; |
| 367 | |
| 368 | for (devnum = 0; devnum < SNDRV_CARDS; devnum++) |
| 369 | if (enable[devnum] && !snd_card_used[devnum]) |
| 370 | break; |
| 371 | |
| 372 | if (devnum >= SNDRV_CARDS) |
Takashi Iwai | 51721f7 | 2008-12-28 16:55:08 +0100 | [diff] [blame] | 373 | return -ENODEV; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 374 | |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 375 | err = snd_card_create(index[devnum], id[devnum], THIS_MODULE, |
Takashi Iwai | bd7dd77 | 2008-12-28 16:45:02 +0100 | [diff] [blame] | 376 | sizeof(struct snd_usb_caiaqdev), &card); |
| 377 | if (err < 0) |
Takashi Iwai | 51721f7 | 2008-12-28 16:55:08 +0100 | [diff] [blame] | 378 | return err; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 379 | |
| 380 | dev = caiaqdev(card); |
| 381 | dev->chip.dev = usb_dev; |
| 382 | dev->chip.card = card; |
Al Viro | 8c5330a | 2008-05-21 06:32:11 +0100 | [diff] [blame] | 383 | dev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor), |
| 384 | le16_to_cpu(usb_dev->descriptor.idProduct)); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 385 | spin_lock_init(&dev->spinlock); |
Daniel Mack | 563c2bf | 2009-07-02 10:46:35 +0200 | [diff] [blame] | 386 | snd_card_set_dev(card, &intf->dev); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 387 | |
Takashi Iwai | 51721f7 | 2008-12-28 16:55:08 +0100 | [diff] [blame] | 388 | *cardp = card; |
| 389 | return 0; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Sam Ravnborg | c598195 | 2008-02-22 18:46:47 +0100 | [diff] [blame] | 392 | static int __devinit init_card(struct snd_usb_caiaqdev *dev) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 393 | { |
Daniel Mack | bafeee5 | 2009-06-02 12:36:39 +0200 | [diff] [blame] | 394 | char *c, usbpath[32]; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 395 | struct usb_device *usb_dev = dev->chip.dev; |
| 396 | struct snd_card *card = dev->chip.card; |
Daniel Mack | bafeee5 | 2009-06-02 12:36:39 +0200 | [diff] [blame] | 397 | int err, len; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 398 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 399 | if (usb_set_interface(usb_dev, 0, 1) != 0) { |
| 400 | log("can't set alt interface.\n"); |
| 401 | return -EIO; |
| 402 | } |
| 403 | |
| 404 | usb_init_urb(&dev->ep1_in_urb); |
| 405 | usb_init_urb(&dev->midi_out_urb); |
| 406 | |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 407 | usb_fill_bulk_urb(&dev->ep1_in_urb, usb_dev, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 408 | usb_rcvbulkpipe(usb_dev, 0x1), |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 409 | dev->ep1_in_buf, EP1_BUFSIZE, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 410 | usb_ep1_command_reply_dispatch, dev); |
| 411 | |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 412 | usb_fill_bulk_urb(&dev->midi_out_urb, usb_dev, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 413 | usb_sndbulkpipe(usb_dev, 0x1), |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 414 | dev->midi_out_buf, EP1_BUFSIZE, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 415 | snd_usb_caiaq_midi_output_done, dev); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 416 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 417 | init_waitqueue_head(&dev->ep1_wait_queue); |
| 418 | init_waitqueue_head(&dev->prepare_wait_queue); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 419 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 420 | if (usb_submit_urb(&dev->ep1_in_urb, GFP_KERNEL) != 0) |
| 421 | return -EIO; |
| 422 | |
Daniel Mack | 8e3cd08 | 2007-11-22 11:40:04 +0100 | [diff] [blame] | 423 | err = snd_usb_caiaq_send_command(dev, EP1_CMD_GET_DEVICE_INFO, NULL, 0); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 424 | if (err) |
| 425 | return err; |
| 426 | |
| 427 | if (!wait_event_timeout(dev->ep1_wait_queue, dev->spec_received, HZ)) |
| 428 | return -ENODEV; |
| 429 | |
| 430 | usb_string(usb_dev, usb_dev->descriptor.iManufacturer, |
| 431 | dev->vendor_name, CAIAQ_USB_STR_LEN); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 432 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 433 | usb_string(usb_dev, usb_dev->descriptor.iProduct, |
| 434 | dev->product_name, CAIAQ_USB_STR_LEN); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 435 | |
Daniel Mack | d3873a1 | 2009-06-01 21:36:25 +0200 | [diff] [blame] | 436 | strlcpy(card->driver, MODNAME, sizeof(card->driver)); |
| 437 | strlcpy(card->shortname, dev->product_name, sizeof(card->shortname)); |
Daniel Mack | 955f2d9 | 2009-06-10 21:32:01 +0200 | [diff] [blame] | 438 | strlcpy(card->mixername, dev->product_name, sizeof(card->mixername)); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 439 | |
Daniel Mack | bafeee5 | 2009-06-02 12:36:39 +0200 | [diff] [blame] | 440 | /* if the id was not passed as module option, fill it with a shortened |
| 441 | * version of the product string which does not contain any |
| 442 | * whitespaces */ |
| 443 | |
| 444 | if (*card->id == '\0') { |
| 445 | char id[sizeof(card->id)]; |
| 446 | |
| 447 | memset(id, 0, sizeof(id)); |
| 448 | |
| 449 | for (c = card->shortname, len = 0; |
| 450 | *c && len < sizeof(card->id); c++) |
| 451 | if (*c != ' ') |
| 452 | id[len++] = *c; |
| 453 | |
| 454 | snd_card_set_id(card, id); |
| 455 | } |
| 456 | |
Daniel Mack | 1a1df6f | 2009-06-01 21:36:26 +0200 | [diff] [blame] | 457 | usb_make_path(usb_dev, usbpath, sizeof(usbpath)); |
| 458 | snprintf(card->longname, sizeof(card->longname), |
| 459 | "%s %s (%s)", |
| 460 | dev->vendor_name, dev->product_name, usbpath); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 461 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 462 | setup_card(dev); |
| 463 | return 0; |
| 464 | } |
| 465 | |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 466 | static int __devinit snd_probe(struct usb_interface *intf, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 467 | const struct usb_device_id *id) |
| 468 | { |
| 469 | int ret; |
| 470 | struct snd_card *card; |
| 471 | struct usb_device *device = interface_to_usbdev(intf); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 472 | |
Daniel Mack | 563c2bf | 2009-07-02 10:46:35 +0200 | [diff] [blame] | 473 | ret = create_card(device, intf, &card); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 474 | |
Takashi Iwai | 51721f7 | 2008-12-28 16:55:08 +0100 | [diff] [blame] | 475 | if (ret < 0) |
| 476 | return ret; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 477 | |
Julia Lawall | f4e9749 | 2009-01-01 18:14:35 +0100 | [diff] [blame] | 478 | usb_set_intfdata(intf, card); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 479 | ret = init_card(caiaqdev(card)); |
| 480 | if (ret < 0) { |
| 481 | log("unable to init card! (ret=%d)\n", ret); |
| 482 | snd_card_free(card); |
| 483 | return ret; |
| 484 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 485 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static void snd_disconnect(struct usb_interface *intf) |
| 490 | { |
| 491 | struct snd_usb_caiaqdev *dev; |
Julia Lawall | f4e9749 | 2009-01-01 18:14:35 +0100 | [diff] [blame] | 492 | struct snd_card *card = usb_get_intfdata(intf); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 493 | |
Daniel Mack | 8d04884 | 2008-04-14 15:39:14 +0200 | [diff] [blame] | 494 | debug("%s(%p)\n", __func__, intf); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 495 | |
| 496 | if (!card) |
| 497 | return; |
| 498 | |
| 499 | dev = caiaqdev(card); |
| 500 | snd_card_disconnect(card); |
| 501 | |
| 502 | #ifdef CONFIG_SND_USB_CAIAQ_INPUT |
| 503 | snd_usb_caiaq_input_free(dev); |
| 504 | #endif |
| 505 | snd_usb_caiaq_audio_free(dev); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 506 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 507 | usb_kill_urb(&dev->ep1_in_urb); |
| 508 | usb_kill_urb(&dev->midi_out_urb); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 509 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 510 | snd_card_free(card); |
| 511 | usb_reset_device(interface_to_usbdev(intf)); |
| 512 | } |
| 513 | |
| 514 | |
| 515 | MODULE_DEVICE_TABLE(usb, snd_usb_id_table); |
| 516 | static struct usb_driver snd_usb_driver = { |
| 517 | .name = MODNAME, |
| 518 | .probe = snd_probe, |
| 519 | .disconnect = snd_disconnect, |
| 520 | .id_table = snd_usb_id_table, |
| 521 | }; |
| 522 | |
| 523 | static int __init snd_module_init(void) |
| 524 | { |
| 525 | return usb_register(&snd_usb_driver); |
| 526 | } |
| 527 | |
| 528 | static void __exit snd_module_exit(void) |
| 529 | { |
| 530 | usb_deregister(&snd_usb_driver); |
| 531 | } |
| 532 | |
| 533 | module_init(snd_module_init) |
| 534 | module_exit(snd_module_exit) |
| 535 | |