aboutsummaryrefslogtreecommitdiffstats
path: root/sound/usb/caiaq/caiaq-device.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/usb/caiaq/caiaq-device.c')
-rw-r--r--sound/usb/caiaq/caiaq-device.c436
1 files changed, 436 insertions, 0 deletions
diff --git a/sound/usb/caiaq/caiaq-device.c b/sound/usb/caiaq/caiaq-device.c
new file mode 100644
index 00000000000..4709347326f
--- /dev/null
+++ b/sound/usb/caiaq/caiaq-device.c
@@ -0,0 +1,436 @@
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
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/interrupt.h>
26#include <linux/usb.h>
27#include <linux/input.h>
28#include <linux/spinlock.h>
29#include <sound/driver.h>
30#include <sound/core.h>
31#include <sound/initval.h>
32#include <sound/pcm.h>
33#include <sound/rawmidi.h>
34
35#include "caiaq-device.h"
36#include "caiaq-audio.h"
37#include "caiaq-midi.h"
38
39#ifdef CONFIG_SND_USB_CAIAQ_INPUT
40#include "caiaq-input.h"
41#endif
42
43MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
44MODULE_DESCRIPTION("caiaq USB audio, version 1.1.0");
45MODULE_LICENSE("GPL");
46MODULE_SUPPORTED_DEVICE("{{Native Instruments, RigKontrol2},"
47 "{Native Instruments, Kore Controller},"
48 "{Native Instruments, Audio Kontrol 1}"
49 "{Native Instruments, Audio 8 DJ}}");
50
51static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
52static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
53static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
54static int snd_card_used[SNDRV_CARDS];
55
56module_param_array(index, int, NULL, 0444);
57MODULE_PARM_DESC(index, "Index value for the caiaq sound device");
58module_param_array(id, charp, NULL, 0444);
59MODULE_PARM_DESC(id, "ID string for the caiaq soundcard.");
60module_param_array(enable, bool, NULL, 0444);
61MODULE_PARM_DESC(enable, "Enable the caiaq soundcard.");
62
63enum {
64 SAMPLERATE_44100 = 0,
65 SAMPLERATE_48000 = 1,
66 SAMPLERATE_96000 = 2,
67 SAMPLERATE_192000 = 3,
68 SAMPLERATE_88200 = 4,
69 SAMPLERATE_INVALID = 0xff
70};
71
72enum {
73 DEPTH_NONE = 0,
74 DEPTH_16 = 1,
75 DEPTH_24 = 2,
76 DEPTH_32 = 3
77};
78
79static struct usb_device_id snd_usb_id_table[] = {
80 {
81 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
82 .idVendor = USB_VID_NATIVEINSTRUMENTS,
83 .idProduct = USB_PID_RIGKONTROL2
84 },
85 {
86 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
87 .idVendor = USB_VID_NATIVEINSTRUMENTS,
88 .idProduct = USB_PID_KORECONTROLLER
89 },
90 {
91 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
92 .idVendor = USB_VID_NATIVEINSTRUMENTS,
93 .idProduct = USB_PID_AK1
94 },
95 {
96 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
97 .idVendor = USB_VID_NATIVEINSTRUMENTS,
98 .idProduct = USB_PID_AUDIO8DJ
99 },
100 { /* terminator */ }
101};
102
103static void usb_ep1_command_reply_dispatch (struct urb* urb)
104{
105 int ret;
106 struct snd_usb_caiaqdev *dev = urb->context;
107 unsigned char *buf = urb->transfer_buffer;
108
109 if (urb->status || !dev) {
110 log("received EP1 urb->status = %i\n", urb->status);
111 return;
112 }
113
114 switch(buf[0]) {
115 case EP1_CMD_GET_DEVICE_INFO:
116 memcpy(&dev->spec, buf+1, sizeof(struct caiaq_device_spec));
117 dev->spec.fw_version = le16_to_cpu(dev->spec.fw_version);
118 debug("device spec (firmware %d): audio: %d in, %d out, "
119 "MIDI: %d in, %d out, data alignment %d\n",
120 dev->spec.fw_version,
121 dev->spec.num_analog_audio_in,
122 dev->spec.num_analog_audio_out,
123 dev->spec.num_midi_in,
124 dev->spec.num_midi_out,
125 dev->spec.data_alignment);
126
127 dev->spec_received++;
128 wake_up(&dev->ep1_wait_queue);
129 break;
130 case EP1_CMD_AUDIO_PARAMS:
131 dev->audio_parm_answer = buf[1];
132 wake_up(&dev->ep1_wait_queue);
133 break;
134 case EP1_CMD_MIDI_READ:
135 snd_usb_caiaq_midi_handle_input(dev, buf[1], buf + 3, buf[2]);
136 break;
137
138#ifdef CONFIG_SND_USB_CAIAQ_INPUT
139 case EP1_CMD_READ_ERP:
140 case EP1_CMD_READ_ANALOG:
141 case EP1_CMD_READ_IO:
142 snd_usb_caiaq_input_dispatch(dev, buf, urb->actual_length);
143 break;
144#endif
145 }
146
147 dev->ep1_in_urb.actual_length = 0;
148 ret = usb_submit_urb(&dev->ep1_in_urb, GFP_ATOMIC);
149 if (ret < 0)
150 log("unable to submit urb. OOM!?\n");
151}
152
153static int send_command (struct snd_usb_caiaqdev *dev,
154 unsigned char command,
155 const unsigned char *buffer,
156 int len)
157{
158 int actual_len;
159 struct usb_device *usb_dev = dev->chip.dev;
160
161 if (!usb_dev)
162 return -EIO;
163
164 if (len > EP1_BUFSIZE - 1)
165 len = EP1_BUFSIZE - 1;
166
167 if (buffer && len > 0)
168 memcpy(dev->ep1_out_buf+1, buffer, len);
169
170 dev->ep1_out_buf[0] = command;
171 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
172 dev->ep1_out_buf, len+1, &actual_len, 200);
173}
174
175int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *dev,
176 int rate, int depth, int bpp)
177{
178 int ret;
179 char tmp[5];
180
181 switch (rate) {
182 case 44100: tmp[0] = SAMPLERATE_44100; break;
183 case 48000: tmp[0] = SAMPLERATE_48000; break;
184 case 88200: tmp[0] = SAMPLERATE_88200; break;
185 case 96000: tmp[0] = SAMPLERATE_96000; break;
186 case 192000: tmp[0] = SAMPLERATE_192000; break;
187 default: return -EINVAL;
188 }
189
190 switch (depth) {
191 case 16: tmp[1] = DEPTH_16; break;
192 case 24: tmp[1] = DEPTH_24; break;
193 default: return -EINVAL;
194 }
195
196 tmp[2] = bpp & 0xff;
197 tmp[3] = bpp >> 8;
198 tmp[4] = 1; /* packets per microframe */
199
200 debug("setting audio params: %d Hz, %d bits, %d bpp\n",
201 rate, depth, bpp);
202
203 dev->audio_parm_answer = -1;
204 ret = send_command(dev, EP1_CMD_AUDIO_PARAMS, tmp, sizeof(tmp));
205
206 if (ret)
207 return ret;
208
209 if (!wait_event_timeout(dev->ep1_wait_queue,
210 dev->audio_parm_answer >= 0, HZ))
211 return -EPIPE;
212
213 if (dev->audio_parm_answer != 1)
214 debug("unable to set the device's audio params\n");
215
216 return dev->audio_parm_answer == 1 ? 0 : -EINVAL;
217}
218
219int snd_usb_caiaq_set_auto_msg (struct snd_usb_caiaqdev *dev,
220 int digital, int analog, int erp)
221{
222 char tmp[3] = { digital, analog, erp };
223 return send_command(dev, EP1_CMD_AUTO_MSG, tmp, sizeof(tmp));
224}
225
226static void setup_card(struct snd_usb_caiaqdev *dev)
227{
228 int ret;
229 char val[3];
230
231 /* device-specific startup specials */
232 switch (dev->chip.usb_id) {
233 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
234 /* RigKontrol2 - display centered dash ('-') */
235 val[0] = 0x00;
236 val[1] = 0x00;
237 val[2] = 0x01;
238 send_command(dev, EP1_CMD_WRITE_IO, val, 3);
239 break;
240 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
241 /* Audio Kontrol 1 - make USB-LED stop blinking */
242 val[0] = 0x00;
243 send_command(dev, EP1_CMD_WRITE_IO, val, 1);
244 break;
245 }
246
247 ret = snd_usb_caiaq_audio_init(dev);
248 if (ret < 0)
249 log("Unable to set up audio system (ret=%d)\n", ret);
250
251 ret = snd_usb_caiaq_midi_init(dev);
252 if (ret < 0)
253 log("Unable to set up MIDI system (ret=%d)\n", ret);
254
255#ifdef CONFIG_SND_USB_CAIAQ_INPUT
256 ret = snd_usb_caiaq_input_init(dev);
257 if (ret < 0)
258 log("Unable to set up input system (ret=%d)\n", ret);
259#endif
260
261 /* finally, register the card and all its sub-instances */
262 ret = snd_card_register(dev->chip.card);
263 if (ret < 0) {
264 log("snd_card_register() returned %d\n", ret);
265 snd_card_free(dev->chip.card);
266 }
267}
268
269static struct snd_card* create_card(struct usb_device* usb_dev)
270{
271 int devnum;
272 struct snd_card *card;
273 struct snd_usb_caiaqdev *dev;
274
275 for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
276 if (enable[devnum] && !snd_card_used[devnum])
277 break;
278
279 if (devnum >= SNDRV_CARDS)
280 return NULL;
281
282 card = snd_card_new(index[devnum], id[devnum], THIS_MODULE,
283 sizeof(struct snd_usb_caiaqdev));
284 if (!card)
285 return NULL;
286
287 dev = caiaqdev(card);
288 dev->chip.dev = usb_dev;
289 dev->chip.card = card;
290 dev->chip.usb_id = USB_ID(usb_dev->descriptor.idVendor,
291 usb_dev->descriptor.idProduct);
292 spin_lock_init(&dev->spinlock);
293 snd_card_set_dev(card, &usb_dev->dev);
294
295 return card;
296}
297
298static int init_card(struct snd_usb_caiaqdev *dev)
299{
300 char *c;
301 struct usb_device *usb_dev = dev->chip.dev;
302 struct snd_card *card = dev->chip.card;
303 int err, len;
304
305 if (usb_set_interface(usb_dev, 0, 1) != 0) {
306 log("can't set alt interface.\n");
307 return -EIO;
308 }
309
310 usb_init_urb(&dev->ep1_in_urb);
311 usb_init_urb(&dev->midi_out_urb);
312
313 usb_fill_bulk_urb(&dev->ep1_in_urb, usb_dev,
314 usb_rcvbulkpipe(usb_dev, 0x1),
315 dev->ep1_in_buf, EP1_BUFSIZE,
316 usb_ep1_command_reply_dispatch, dev);
317
318 usb_fill_bulk_urb(&dev->midi_out_urb, usb_dev,
319 usb_sndbulkpipe(usb_dev, 0x1),
320 dev->midi_out_buf, EP1_BUFSIZE,
321 snd_usb_caiaq_midi_output_done, dev);
322
323 init_waitqueue_head(&dev->ep1_wait_queue);
324 init_waitqueue_head(&dev->prepare_wait_queue);
325
326 if (usb_submit_urb(&dev->ep1_in_urb, GFP_KERNEL) != 0)
327 return -EIO;
328
329 err = send_command(dev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
330 if (err)
331 return err;
332
333 if (!wait_event_timeout(dev->ep1_wait_queue, dev->spec_received, HZ))
334 return -ENODEV;
335
336 usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
337 dev->vendor_name, CAIAQ_USB_STR_LEN);
338
339 usb_string(usb_dev, usb_dev->descriptor.iProduct,
340 dev->product_name, CAIAQ_USB_STR_LEN);
341
342 usb_string(usb_dev, usb_dev->descriptor.iSerialNumber,
343 dev->serial, CAIAQ_USB_STR_LEN);
344
345 /* terminate serial string at first white space occurence */
346 c = strchr(dev->serial, ' ');
347 if (c)
348 *c = '\0';
349
350 strcpy(card->driver, MODNAME);
351 strcpy(card->shortname, dev->product_name);
352
353 len = snprintf(card->longname, sizeof(card->longname),
354 "%s %s (serial %s, ",
355 dev->vendor_name, dev->product_name, dev->serial);
356
357 if (len < sizeof(card->longname) - 2)
358 len += usb_make_path(usb_dev, card->longname + len,
359 sizeof(card->longname) - len);
360
361 card->longname[len++] = ')';
362 card->longname[len] = '\0';
363 setup_card(dev);
364 return 0;
365}
366
367static int snd_probe(struct usb_interface *intf,
368 const struct usb_device_id *id)
369{
370 int ret;
371 struct snd_card *card;
372 struct usb_device *device = interface_to_usbdev(intf);
373
374 card = create_card(device);
375
376 if (!card)
377 return -ENOMEM;
378
379 dev_set_drvdata(&intf->dev, card);
380 ret = init_card(caiaqdev(card));
381 if (ret < 0) {
382 log("unable to init card! (ret=%d)\n", ret);
383 snd_card_free(card);
384 return ret;
385 }
386
387 return 0;
388}
389
390static void snd_disconnect(struct usb_interface *intf)
391{
392 struct snd_usb_caiaqdev *dev;
393 struct snd_card *card = dev_get_drvdata(&intf->dev);
394
395 debug("snd_disconnect(%p)\n", intf);
396
397 if (!card)
398 return;
399
400 dev = caiaqdev(card);
401 snd_card_disconnect(card);
402
403#ifdef CONFIG_SND_USB_CAIAQ_INPUT
404 snd_usb_caiaq_input_free(dev);
405#endif
406 snd_usb_caiaq_audio_free(dev);
407
408 usb_kill_urb(&dev->ep1_in_urb);
409 usb_kill_urb(&dev->midi_out_urb);
410
411 snd_card_free(card);
412 usb_reset_device(interface_to_usbdev(intf));
413}
414
415
416MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
417static struct usb_driver snd_usb_driver = {
418 .name = MODNAME,
419 .probe = snd_probe,
420 .disconnect = snd_disconnect,
421 .id_table = snd_usb_id_table,
422};
423
424static int __init snd_module_init(void)
425{
426 return usb_register(&snd_usb_driver);
427}
428
429static void __exit snd_module_exit(void)
430{
431 usb_deregister(&snd_usb_driver);
432}
433
434module_init(snd_module_init)
435module_exit(snd_module_exit)
436