x86: 64-bit, add the new split_large_page() function
[wrt350n-kernel.git] / sound / usb / caiaq / caiaq-device.c
blob58af8142c57100cf3b549dbbe73bd844d7e85d46
1 /*
2 * caiaq.c: ALSA driver for caiaq/NativeInstruments devices
4 * Copyright (c) 2007 Daniel Mack <daniel@caiaq.de>
5 * Karsten Wiese <fzu@wemgehoertderstaat.de>
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.
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.
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
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>
35 #include "caiaq-device.h"
36 #include "caiaq-audio.h"
37 #include "caiaq-midi.h"
39 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
40 #include "caiaq-input.h"
41 #endif
43 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
44 MODULE_DESCRIPTION("caiaq USB audio, version 1.2.0");
45 MODULE_LICENSE("GPL");
46 MODULE_SUPPORTED_DEVICE("{{Native Instruments, RigKontrol2},"
47 "{Native Instruments, RigKontrol3},"
48 "{Native Instruments, Kore Controller},"
49 "{Native Instruments, Audio Kontrol 1}"
50 "{Native Instruments, Audio 8 DJ}}");
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];
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.");
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
73 enum {
74 DEPTH_NONE = 0,
75 DEPTH_16 = 1,
76 DEPTH_24 = 2,
77 DEPTH_32 = 3
80 static struct usb_device_id snd_usb_id_table[] = {
82 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
83 .idVendor = USB_VID_NATIVEINSTRUMENTS,
84 .idProduct = USB_PID_RIGKONTROL2
87 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
88 .idVendor = USB_VID_NATIVEINSTRUMENTS,
89 .idProduct = USB_PID_RIGKONTROL3
92 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
93 .idVendor = USB_VID_NATIVEINSTRUMENTS,
94 .idProduct = USB_PID_KORECONTROLLER
97 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
98 .idVendor = USB_VID_NATIVEINSTRUMENTS,
99 .idProduct = USB_PID_AK1
102 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
103 .idVendor = USB_VID_NATIVEINSTRUMENTS,
104 .idProduct = USB_PID_AUDIO8DJ
106 { /* terminator */ }
109 static void usb_ep1_command_reply_dispatch (struct urb* urb)
111 int ret;
112 struct snd_usb_caiaqdev *dev = urb->context;
113 unsigned char *buf = urb->transfer_buffer;
115 if (urb->status || !dev) {
116 log("received EP1 urb->status = %i\n", urb->status);
117 return;
120 switch(buf[0]) {
121 case EP1_CMD_GET_DEVICE_INFO:
122 memcpy(&dev->spec, buf+1, sizeof(struct caiaq_device_spec));
123 dev->spec.fw_version = le16_to_cpu(dev->spec.fw_version);
124 debug("device spec (firmware %d): audio: %d in, %d out, "
125 "MIDI: %d in, %d out, data alignment %d\n",
126 dev->spec.fw_version,
127 dev->spec.num_analog_audio_in,
128 dev->spec.num_analog_audio_out,
129 dev->spec.num_midi_in,
130 dev->spec.num_midi_out,
131 dev->spec.data_alignment);
133 dev->spec_received++;
134 wake_up(&dev->ep1_wait_queue);
135 break;
136 case EP1_CMD_AUDIO_PARAMS:
137 dev->audio_parm_answer = buf[1];
138 wake_up(&dev->ep1_wait_queue);
139 break;
140 case EP1_CMD_MIDI_READ:
141 snd_usb_caiaq_midi_handle_input(dev, buf[1], buf + 3, buf[2]);
142 break;
144 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
145 case EP1_CMD_READ_ERP:
146 case EP1_CMD_READ_ANALOG:
147 case EP1_CMD_READ_IO:
148 snd_usb_caiaq_input_dispatch(dev, buf, urb->actual_length);
149 break;
150 #endif
153 dev->ep1_in_urb.actual_length = 0;
154 ret = usb_submit_urb(&dev->ep1_in_urb, GFP_ATOMIC);
155 if (ret < 0)
156 log("unable to submit urb. OOM!?\n");
159 static int send_command (struct snd_usb_caiaqdev *dev,
160 unsigned char command,
161 const unsigned char *buffer,
162 int len)
164 int actual_len;
165 struct usb_device *usb_dev = dev->chip.dev;
167 if (!usb_dev)
168 return -EIO;
170 if (len > EP1_BUFSIZE - 1)
171 len = EP1_BUFSIZE - 1;
173 if (buffer && len > 0)
174 memcpy(dev->ep1_out_buf+1, buffer, len);
176 dev->ep1_out_buf[0] = command;
177 return usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, 1),
178 dev->ep1_out_buf, len+1, &actual_len, 200);
181 int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev *dev,
182 int rate, int depth, int bpp)
184 int ret;
185 char tmp[5];
187 switch (rate) {
188 case 44100: tmp[0] = SAMPLERATE_44100; break;
189 case 48000: tmp[0] = SAMPLERATE_48000; break;
190 case 88200: tmp[0] = SAMPLERATE_88200; break;
191 case 96000: tmp[0] = SAMPLERATE_96000; break;
192 case 192000: tmp[0] = SAMPLERATE_192000; break;
193 default: return -EINVAL;
196 switch (depth) {
197 case 16: tmp[1] = DEPTH_16; break;
198 case 24: tmp[1] = DEPTH_24; break;
199 default: return -EINVAL;
202 tmp[2] = bpp & 0xff;
203 tmp[3] = bpp >> 8;
204 tmp[4] = 1; /* packets per microframe */
206 debug("setting audio params: %d Hz, %d bits, %d bpp\n",
207 rate, depth, bpp);
209 dev->audio_parm_answer = -1;
210 ret = send_command(dev, EP1_CMD_AUDIO_PARAMS, tmp, sizeof(tmp));
212 if (ret)
213 return ret;
215 if (!wait_event_timeout(dev->ep1_wait_queue,
216 dev->audio_parm_answer >= 0, HZ))
217 return -EPIPE;
219 if (dev->audio_parm_answer != 1)
220 debug("unable to set the device's audio params\n");
222 return dev->audio_parm_answer == 1 ? 0 : -EINVAL;
225 int snd_usb_caiaq_set_auto_msg (struct snd_usb_caiaqdev *dev,
226 int digital, int analog, int erp)
228 char tmp[3] = { digital, analog, erp };
229 return send_command(dev, EP1_CMD_AUTO_MSG, tmp, sizeof(tmp));
232 static void setup_card(struct snd_usb_caiaqdev *dev)
234 int ret;
235 char val[4];
237 /* device-specific startup specials */
238 switch (dev->chip.usb_id) {
239 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL2):
240 /* RigKontrol2 - display centered dash ('-') */
241 val[0] = 0x00;
242 val[1] = 0x00;
243 val[2] = 0x01;
244 send_command(dev, EP1_CMD_WRITE_IO, val, 3);
245 break;
246 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_RIGKONTROL3):
247 /* RigKontrol2 - display two centered dashes ('--') */
248 val[0] = 0x00;
249 val[1] = 0x40;
250 val[2] = 0x40;
251 val[3] = 0x00;
252 send_command(dev, EP1_CMD_WRITE_IO, val, 4);
253 break;
254 case USB_ID(USB_VID_NATIVEINSTRUMENTS, USB_PID_AK1):
255 /* Audio Kontrol 1 - make USB-LED stop blinking */
256 val[0] = 0x00;
257 send_command(dev, EP1_CMD_WRITE_IO, val, 1);
258 break;
261 ret = snd_usb_caiaq_audio_init(dev);
262 if (ret < 0)
263 log("Unable to set up audio system (ret=%d)\n", ret);
265 ret = snd_usb_caiaq_midi_init(dev);
266 if (ret < 0)
267 log("Unable to set up MIDI system (ret=%d)\n", ret);
269 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
270 ret = snd_usb_caiaq_input_init(dev);
271 if (ret < 0)
272 log("Unable to set up input system (ret=%d)\n", ret);
273 #endif
275 /* finally, register the card and all its sub-instances */
276 ret = snd_card_register(dev->chip.card);
277 if (ret < 0) {
278 log("snd_card_register() returned %d\n", ret);
279 snd_card_free(dev->chip.card);
283 static struct snd_card* create_card(struct usb_device* usb_dev)
285 int devnum;
286 struct snd_card *card;
287 struct snd_usb_caiaqdev *dev;
289 for (devnum = 0; devnum < SNDRV_CARDS; devnum++)
290 if (enable[devnum] && !snd_card_used[devnum])
291 break;
293 if (devnum >= SNDRV_CARDS)
294 return NULL;
296 card = snd_card_new(index[devnum], id[devnum], THIS_MODULE,
297 sizeof(struct snd_usb_caiaqdev));
298 if (!card)
299 return NULL;
301 dev = caiaqdev(card);
302 dev->chip.dev = usb_dev;
303 dev->chip.card = card;
304 dev->chip.usb_id = USB_ID(usb_dev->descriptor.idVendor,
305 usb_dev->descriptor.idProduct);
306 spin_lock_init(&dev->spinlock);
307 snd_card_set_dev(card, &usb_dev->dev);
309 return card;
312 static int init_card(struct snd_usb_caiaqdev *dev)
314 char *c;
315 struct usb_device *usb_dev = dev->chip.dev;
316 struct snd_card *card = dev->chip.card;
317 int err, len;
319 if (usb_set_interface(usb_dev, 0, 1) != 0) {
320 log("can't set alt interface.\n");
321 return -EIO;
324 usb_init_urb(&dev->ep1_in_urb);
325 usb_init_urb(&dev->midi_out_urb);
327 usb_fill_bulk_urb(&dev->ep1_in_urb, usb_dev,
328 usb_rcvbulkpipe(usb_dev, 0x1),
329 dev->ep1_in_buf, EP1_BUFSIZE,
330 usb_ep1_command_reply_dispatch, dev);
332 usb_fill_bulk_urb(&dev->midi_out_urb, usb_dev,
333 usb_sndbulkpipe(usb_dev, 0x1),
334 dev->midi_out_buf, EP1_BUFSIZE,
335 snd_usb_caiaq_midi_output_done, dev);
337 init_waitqueue_head(&dev->ep1_wait_queue);
338 init_waitqueue_head(&dev->prepare_wait_queue);
340 if (usb_submit_urb(&dev->ep1_in_urb, GFP_KERNEL) != 0)
341 return -EIO;
343 err = send_command(dev, EP1_CMD_GET_DEVICE_INFO, NULL, 0);
344 if (err)
345 return err;
347 if (!wait_event_timeout(dev->ep1_wait_queue, dev->spec_received, HZ))
348 return -ENODEV;
350 usb_string(usb_dev, usb_dev->descriptor.iManufacturer,
351 dev->vendor_name, CAIAQ_USB_STR_LEN);
353 usb_string(usb_dev, usb_dev->descriptor.iProduct,
354 dev->product_name, CAIAQ_USB_STR_LEN);
356 usb_string(usb_dev, usb_dev->descriptor.iSerialNumber,
357 dev->serial, CAIAQ_USB_STR_LEN);
359 /* terminate serial string at first white space occurence */
360 c = strchr(dev->serial, ' ');
361 if (c)
362 *c = '\0';
364 strcpy(card->driver, MODNAME);
365 strcpy(card->shortname, dev->product_name);
367 len = snprintf(card->longname, sizeof(card->longname),
368 "%s %s (serial %s, ",
369 dev->vendor_name, dev->product_name, dev->serial);
371 if (len < sizeof(card->longname) - 2)
372 len += usb_make_path(usb_dev, card->longname + len,
373 sizeof(card->longname) - len);
375 card->longname[len++] = ')';
376 card->longname[len] = '\0';
377 setup_card(dev);
378 return 0;
381 static int snd_probe(struct usb_interface *intf,
382 const struct usb_device_id *id)
384 int ret;
385 struct snd_card *card;
386 struct usb_device *device = interface_to_usbdev(intf);
388 card = create_card(device);
390 if (!card)
391 return -ENOMEM;
393 dev_set_drvdata(&intf->dev, card);
394 ret = init_card(caiaqdev(card));
395 if (ret < 0) {
396 log("unable to init card! (ret=%d)\n", ret);
397 snd_card_free(card);
398 return ret;
401 return 0;
404 static void snd_disconnect(struct usb_interface *intf)
406 struct snd_usb_caiaqdev *dev;
407 struct snd_card *card = dev_get_drvdata(&intf->dev);
409 debug("snd_disconnect(%p)\n", intf);
411 if (!card)
412 return;
414 dev = caiaqdev(card);
415 snd_card_disconnect(card);
417 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
418 snd_usb_caiaq_input_free(dev);
419 #endif
420 snd_usb_caiaq_audio_free(dev);
422 usb_kill_urb(&dev->ep1_in_urb);
423 usb_kill_urb(&dev->midi_out_urb);
425 snd_card_free(card);
426 usb_reset_device(interface_to_usbdev(intf));
430 MODULE_DEVICE_TABLE(usb, snd_usb_id_table);
431 static struct usb_driver snd_usb_driver = {
432 .name = MODNAME,
433 .probe = snd_probe,
434 .disconnect = snd_disconnect,
435 .id_table = snd_usb_id_table,
438 static int __init snd_module_init(void)
440 return usb_register(&snd_usb_driver);
443 static void __exit snd_module_exit(void)
445 usb_deregister(&snd_usb_driver);
448 module_init(snd_module_init)
449 module_exit(snd_module_exit)