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"
43 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
44 MODULE_DESCRIPTION("caiaq USB audio, version 1.1.0");
45 MODULE_LICENSE("GPL");
46 MODULE_SUPPORTED_DEVICE("{{Native Instruments, RigKontrol2},"
47 "{Native Instruments, Kore Controller},"
48 "{Native Instruments, Audio Kontrol 1}"
49 "{Native Instruments, Audio 8 DJ}}");
51 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
; /* Index 0-max */
52 static char* id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
; /* Id for this card */
53 static int enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE_PNP
; /* Enable this card */
54 static int snd_card_used
[SNDRV_CARDS
];
56 module_param_array(index
, int, NULL
, 0444);
57 MODULE_PARM_DESC(index
, "Index value for the caiaq sound device");
58 module_param_array(id
, charp
, NULL
, 0444);
59 MODULE_PARM_DESC(id
, "ID string for the caiaq soundcard.");
60 module_param_array(enable
, bool, NULL
, 0444);
61 MODULE_PARM_DESC(enable
, "Enable the caiaq soundcard.");
67 SAMPLERATE_192000
= 3,
69 SAMPLERATE_INVALID
= 0xff
79 static struct usb_device_id snd_usb_id_table
[] = {
81 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
82 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
83 .idProduct
= USB_PID_RIGKONTROL2
86 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
87 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
88 .idProduct
= USB_PID_KORECONTROLLER
91 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
92 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
93 .idProduct
= USB_PID_AK1
96 .match_flags
= USB_DEVICE_ID_MATCH_DEVICE
,
97 .idVendor
= USB_VID_NATIVEINSTRUMENTS
,
98 .idProduct
= USB_PID_AUDIO8DJ
103 static void usb_ep1_command_reply_dispatch (struct urb
* urb
)
106 struct snd_usb_caiaqdev
*dev
= urb
->context
;
107 unsigned char *buf
= urb
->transfer_buffer
;
109 if (urb
->status
|| !dev
) {
110 log("received EP1 urb->status = %i\n", urb
->status
);
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
);
127 dev
->spec_received
++;
128 wake_up(&dev
->ep1_wait_queue
);
130 case EP1_CMD_AUDIO_PARAMS
:
131 dev
->audio_parm_answer
= buf
[1];
132 wake_up(&dev
->ep1_wait_queue
);
134 case EP1_CMD_MIDI_READ
:
135 snd_usb_caiaq_midi_handle_input(dev
, buf
[1], buf
+ 3, buf
[2]);
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
);
147 dev
->ep1_in_urb
.actual_length
= 0;
148 ret
= usb_submit_urb(&dev
->ep1_in_urb
, GFP_ATOMIC
);
150 log("unable to submit urb. OOM!?\n");
153 static int send_command (struct snd_usb_caiaqdev
*dev
,
154 unsigned char command
,
155 const unsigned char *buffer
,
159 struct usb_device
*usb_dev
= dev
->chip
.dev
;
164 if (len
> EP1_BUFSIZE
- 1)
165 len
= EP1_BUFSIZE
- 1;
167 if (buffer
&& len
> 0)
168 memcpy(dev
->ep1_out_buf
+1, buffer
, len
);
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);
175 int snd_usb_caiaq_set_audio_params (struct snd_usb_caiaqdev
*dev
,
176 int rate
, int depth
, int bpp
)
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
;
191 case 16: tmp
[1] = DEPTH_16
; break;
192 case 24: tmp
[1] = DEPTH_24
; break;
193 default: return -EINVAL
;
198 tmp
[4] = 1; /* packets per microframe */
200 debug("setting audio params: %d Hz, %d bits, %d bpp\n",
203 dev
->audio_parm_answer
= -1;
204 ret
= send_command(dev
, EP1_CMD_AUDIO_PARAMS
, tmp
, sizeof(tmp
));
209 if (!wait_event_timeout(dev
->ep1_wait_queue
,
210 dev
->audio_parm_answer
>= 0, HZ
))
213 if (dev
->audio_parm_answer
!= 1)
214 debug("unable to set the device's audio params\n");
216 return dev
->audio_parm_answer
== 1 ? 0 : -EINVAL
;
219 int snd_usb_caiaq_set_auto_msg (struct snd_usb_caiaqdev
*dev
,
220 int digital
, int analog
, int erp
)
222 char tmp
[3] = { digital
, analog
, erp
};
223 return send_command(dev
, EP1_CMD_AUTO_MSG
, tmp
, sizeof(tmp
));
226 static void setup_card(struct snd_usb_caiaqdev
*dev
)
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 ('-') */
238 send_command(dev
, EP1_CMD_WRITE_IO
, val
, 3);
240 case USB_ID(USB_VID_NATIVEINSTRUMENTS
, USB_PID_AK1
):
241 /* Audio Kontrol 1 - make USB-LED stop blinking */
243 send_command(dev
, EP1_CMD_WRITE_IO
, val
, 1);
247 ret
= snd_usb_caiaq_audio_init(dev
);
249 log("Unable to set up audio system (ret=%d)\n", ret
);
251 ret
= snd_usb_caiaq_midi_init(dev
);
253 log("Unable to set up MIDI system (ret=%d)\n", ret
);
255 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
256 ret
= snd_usb_caiaq_input_init(dev
);
258 log("Unable to set up input system (ret=%d)\n", ret
);
261 /* finally, register the card and all its sub-instances */
262 ret
= snd_card_register(dev
->chip
.card
);
264 log("snd_card_register() returned %d\n", ret
);
265 snd_card_free(dev
->chip
.card
);
269 static struct snd_card
* create_card(struct usb_device
* usb_dev
)
272 struct snd_card
*card
;
273 struct snd_usb_caiaqdev
*dev
;
275 for (devnum
= 0; devnum
< SNDRV_CARDS
; devnum
++)
276 if (enable
[devnum
] && !snd_card_used
[devnum
])
279 if (devnum
>= SNDRV_CARDS
)
282 card
= snd_card_new(index
[devnum
], id
[devnum
], THIS_MODULE
,
283 sizeof(struct snd_usb_caiaqdev
));
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
);
298 static int init_card(struct snd_usb_caiaqdev
*dev
)
301 struct usb_device
*usb_dev
= dev
->chip
.dev
;
302 struct snd_card
*card
= dev
->chip
.card
;
305 if (usb_set_interface(usb_dev
, 0, 1) != 0) {
306 log("can't set alt interface.\n");
310 usb_init_urb(&dev
->ep1_in_urb
);
311 usb_init_urb(&dev
->midi_out_urb
);
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
);
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
);
323 init_waitqueue_head(&dev
->ep1_wait_queue
);
324 init_waitqueue_head(&dev
->prepare_wait_queue
);
326 if (usb_submit_urb(&dev
->ep1_in_urb
, GFP_KERNEL
) != 0)
329 err
= send_command(dev
, EP1_CMD_GET_DEVICE_INFO
, NULL
, 0);
333 if (!wait_event_timeout(dev
->ep1_wait_queue
, dev
->spec_received
, HZ
))
336 usb_string(usb_dev
, usb_dev
->descriptor
.iManufacturer
,
337 dev
->vendor_name
, CAIAQ_USB_STR_LEN
);
339 usb_string(usb_dev
, usb_dev
->descriptor
.iProduct
,
340 dev
->product_name
, CAIAQ_USB_STR_LEN
);
342 usb_string(usb_dev
, usb_dev
->descriptor
.iSerialNumber
,
343 dev
->serial
, CAIAQ_USB_STR_LEN
);
345 /* terminate serial string at first white space occurence */
346 c
= strchr(dev
->serial
, ' ');
350 strcpy(card
->driver
, MODNAME
);
351 strcpy(card
->shortname
, dev
->product_name
);
353 len
= snprintf(card
->longname
, sizeof(card
->longname
),
354 "%s %s (serial %s, ",
355 dev
->vendor_name
, dev
->product_name
, dev
->serial
);
357 if (len
< sizeof(card
->longname
) - 2)
358 len
+= usb_make_path(usb_dev
, card
->longname
+ len
,
359 sizeof(card
->longname
) - len
);
361 card
->longname
[len
++] = ')';
362 card
->longname
[len
] = '\0';
367 static int snd_probe(struct usb_interface
*intf
,
368 const struct usb_device_id
*id
)
371 struct snd_card
*card
;
372 struct usb_device
*device
= interface_to_usbdev(intf
);
374 card
= create_card(device
);
379 dev_set_drvdata(&intf
->dev
, card
);
380 ret
= init_card(caiaqdev(card
));
382 log("unable to init card! (ret=%d)\n", ret
);
390 static void snd_disconnect(struct usb_interface
*intf
)
392 struct snd_usb_caiaqdev
*dev
;
393 struct snd_card
*card
= dev_get_drvdata(&intf
->dev
);
395 debug("snd_disconnect(%p)\n", intf
);
400 dev
= caiaqdev(card
);
401 snd_card_disconnect(card
);
403 #ifdef CONFIG_SND_USB_CAIAQ_INPUT
404 snd_usb_caiaq_input_free(dev
);
406 snd_usb_caiaq_audio_free(dev
);
408 usb_kill_urb(&dev
->ep1_in_urb
);
409 usb_kill_urb(&dev
->midi_out_urb
);
412 usb_reset_device(interface_to_usbdev(intf
));
416 MODULE_DEVICE_TABLE(usb
, snd_usb_id_table
);
417 static struct usb_driver snd_usb_driver
= {
420 .disconnect
= snd_disconnect
,
421 .id_table
= snd_usb_id_table
,
424 static int __init
snd_module_init(void)
426 return usb_register(&snd_usb_driver
);
429 static void __exit
snd_module_exit(void)
431 usb_deregister(&snd_usb_driver
);
434 module_init(snd_module_init
)
435 module_exit(snd_module_exit
)