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