blob: 9ca9a13a78da196f62acaae7010623b14045809b [file] [log] [blame]
Karsten Wiese030a07e2008-07-30 15:13:29 +02001/*
2 * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Daniel Mackde48c7b2010-02-22 23:49:13 +010020#include <linux/usb.h>
21#include <linux/usb/audio.h>
Karsten Wiese030a07e2008-07-30 15:13:29 +020022#include <sound/core.h>
23#include <sound/hwdep.h>
24#include <sound/pcm.h>
25#include <sound/initval.h>
26#define MODNAME "US122L"
27#include "usb_stream.c"
28#include "../usbaudio.h"
29#include "us122l.h"
30
31MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
32MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5");
33MODULE_LICENSE("GPL");
34
35static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
36static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
37 /* Enable this card */
38static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
39
40module_param_array(index, int, NULL, 0444);
41MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
42module_param_array(id, charp, NULL, 0444);
43MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
44module_param_array(enable, bool, NULL, 0444);
45MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
46
47static int snd_us122l_card_used[SNDRV_CARDS];
48
49
50static int us122l_create_usbmidi(struct snd_card *card)
51{
52 static struct snd_usb_midi_endpoint_info quirk_data = {
53 .out_ep = 4,
54 .in_ep = 3,
55 .out_cables = 0x001,
56 .in_cables = 0x001
57 };
58 static struct snd_usb_audio_quirk quirk = {
59 .vendor_name = "US122L",
60 .product_name = NAME_ALLCAPS,
61 .ifnum = 1,
62 .type = QUIRK_MIDI_US122L,
63 .data = &quirk_data
64 };
Clemens Ladischa014bba2009-11-16 12:26:30 +010065 struct usb_device *dev = US122L(card)->dev;
Karsten Wiese030a07e2008-07-30 15:13:29 +020066 struct usb_interface *iface = usb_ifnum_to_if(dev, 1);
67
Clemens Ladischd82af9f2009-11-16 12:23:46 +010068 return snd_usbmidi_create(card, iface,
69 &US122L(card)->midi_list, &quirk);
Karsten Wiese030a07e2008-07-30 15:13:29 +020070}
71
Tobias Hansen4f272342009-09-22 16:52:08 +020072static int us144_create_usbmidi(struct snd_card *card)
73{
74 static struct snd_usb_midi_endpoint_info quirk_data = {
75 .out_ep = 4,
76 .in_ep = 3,
77 .out_cables = 0x001,
78 .in_cables = 0x001
79 };
80 static struct snd_usb_audio_quirk quirk = {
81 .vendor_name = "US144",
82 .product_name = NAME_ALLCAPS,
83 .ifnum = 0,
84 .type = QUIRK_MIDI_US122L,
85 .data = &quirk_data
86 };
Clemens Ladischa014bba2009-11-16 12:26:30 +010087 struct usb_device *dev = US122L(card)->dev;
Tobias Hansen4f272342009-09-22 16:52:08 +020088 struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
89
Clemens Ladischd82af9f2009-11-16 12:23:46 +010090 return snd_usbmidi_create(card, iface,
91 &US122L(card)->midi_list, &quirk);
Tobias Hansen4f272342009-09-22 16:52:08 +020092}
93
Karsten Wiese030a07e2008-07-30 15:13:29 +020094/*
95 * Wrapper for usb_control_msg().
96 * Allocates a temp buffer to prevent dmaing from/to the stack.
97 */
98static int us122l_ctl_msg(struct usb_device *dev, unsigned int pipe,
99 __u8 request, __u8 requesttype,
100 __u16 value, __u16 index, void *data,
101 __u16 size, int timeout)
102{
103 int err;
104 void *buf = NULL;
105
106 if (size > 0) {
107 buf = kmemdup(data, size, GFP_KERNEL);
108 if (!buf)
109 return -ENOMEM;
110 }
111 err = usb_control_msg(dev, pipe, request, requesttype,
112 value, index, buf, size, timeout);
113 if (size > 0) {
114 memcpy(data, buf, size);
115 kfree(buf);
116 }
117 return err;
118}
119
120static void pt_info_set(struct usb_device *dev, u8 v)
121{
122 int ret;
123
124 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
125 'I',
126 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
127 v, 0, NULL, 0, 1000);
128 snd_printdd(KERN_DEBUG "%i\n", ret);
129}
130
131static void usb_stream_hwdep_vm_open(struct vm_area_struct *area)
132{
133 struct us122l *us122l = area->vm_private_data;
134 atomic_inc(&us122l->mmap_count);
135 snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
136}
137
138static int usb_stream_hwdep_vm_fault(struct vm_area_struct *area,
139 struct vm_fault *vmf)
140{
141 unsigned long offset;
142 struct page *page;
143 void *vaddr;
144 struct us122l *us122l = area->vm_private_data;
145 struct usb_stream *s;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200146
147 mutex_lock(&us122l->mutex);
148 s = us122l->sk.s;
149 if (!s)
Li Zefan428ffb72008-10-15 10:07:23 +0800150 goto unlock;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200151
152 offset = vmf->pgoff << PAGE_SHIFT;
153 if (offset < PAGE_ALIGN(s->read_size))
154 vaddr = (char *)s + offset;
155 else {
156 offset -= PAGE_ALIGN(s->read_size);
157 if (offset >= PAGE_ALIGN(s->write_size))
Li Zefan428ffb72008-10-15 10:07:23 +0800158 goto unlock;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200159
160 vaddr = us122l->sk.write_page + offset;
161 }
162 page = virt_to_page(vaddr);
163
164 get_page(page);
165 mutex_unlock(&us122l->mutex);
166
167 vmf->page = page;
Li Zefan428ffb72008-10-15 10:07:23 +0800168
169 return 0;
170unlock:
171 mutex_unlock(&us122l->mutex);
172 return VM_FAULT_SIGBUS;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200173}
174
175static void usb_stream_hwdep_vm_close(struct vm_area_struct *area)
176{
177 struct us122l *us122l = area->vm_private_data;
178 atomic_dec(&us122l->mmap_count);
179 snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
180}
181
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400182static const struct vm_operations_struct usb_stream_hwdep_vm_ops = {
Karsten Wiese030a07e2008-07-30 15:13:29 +0200183 .open = usb_stream_hwdep_vm_open,
184 .fault = usb_stream_hwdep_vm_fault,
185 .close = usb_stream_hwdep_vm_close,
186};
187
188
189static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file)
190{
191 struct us122l *us122l = hw->private_data;
192 struct usb_interface *iface;
193 snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
194 if (hw->used >= 2)
195 return -EBUSY;
196
197 if (!us122l->first)
198 us122l->first = file;
Tobias Hansen4f272342009-09-22 16:52:08 +0200199
Tobias Hansen2b6f6c02009-12-07 19:08:19 +0100200 if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
201 us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
Clemens Ladischa014bba2009-11-16 12:26:30 +0100202 iface = usb_ifnum_to_if(us122l->dev, 0);
Tobias Hansen4f272342009-09-22 16:52:08 +0200203 usb_autopm_get_interface(iface);
204 }
Clemens Ladischa014bba2009-11-16 12:26:30 +0100205 iface = usb_ifnum_to_if(us122l->dev, 1);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200206 usb_autopm_get_interface(iface);
207 return 0;
208}
209
210static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
211{
212 struct us122l *us122l = hw->private_data;
Tobias Hansen4f272342009-09-22 16:52:08 +0200213 struct usb_interface *iface;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200214 snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
Tobias Hansen4f272342009-09-22 16:52:08 +0200215
Tobias Hansen2b6f6c02009-12-07 19:08:19 +0100216 if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
217 us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
Clemens Ladischa014bba2009-11-16 12:26:30 +0100218 iface = usb_ifnum_to_if(us122l->dev, 0);
Tobias Hansen4f272342009-09-22 16:52:08 +0200219 usb_autopm_put_interface(iface);
220 }
Clemens Ladischa014bba2009-11-16 12:26:30 +0100221 iface = usb_ifnum_to_if(us122l->dev, 1);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200222 usb_autopm_put_interface(iface);
223 if (us122l->first == file)
224 us122l->first = NULL;
225 mutex_lock(&us122l->mutex);
226 if (us122l->master == file)
227 us122l->master = us122l->slave;
228
229 us122l->slave = NULL;
230 mutex_unlock(&us122l->mutex);
231 return 0;
232}
233
234static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
235 struct file *filp, struct vm_area_struct *area)
236{
237 unsigned long size = area->vm_end - area->vm_start;
238 struct us122l *us122l = hw->private_data;
239 unsigned long offset;
240 struct usb_stream *s;
241 int err = 0;
242 bool read;
243
244 offset = area->vm_pgoff << PAGE_SHIFT;
245 mutex_lock(&us122l->mutex);
246 s = us122l->sk.s;
247 read = offset < s->read_size;
248 if (read && area->vm_flags & VM_WRITE) {
249 err = -EPERM;
250 goto out;
251 }
252 snd_printdd(KERN_DEBUG "%lu %u\n", size,
253 read ? s->read_size : s->write_size);
254 /* if userspace tries to mmap beyond end of our buffer, fail */
255 if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
256 snd_printk(KERN_WARNING "%lu > %u\n", size,
257 read ? s->read_size : s->write_size);
258 err = -EINVAL;
259 goto out;
260 }
261
262 area->vm_ops = &usb_stream_hwdep_vm_ops;
263 area->vm_flags |= VM_RESERVED;
264 area->vm_private_data = us122l;
265 atomic_inc(&us122l->mmap_count);
266out:
267 mutex_unlock(&us122l->mutex);
268 return err;
269}
270
271static unsigned int usb_stream_hwdep_poll(struct snd_hwdep *hw,
272 struct file *file, poll_table *wait)
273{
274 struct us122l *us122l = hw->private_data;
275 struct usb_stream *s = us122l->sk.s;
276 unsigned *polled;
277 unsigned int mask;
278
279 poll_wait(file, &us122l->sk.sleep, wait);
280
281 switch (s->state) {
282 case usb_stream_ready:
283 if (us122l->first == file)
284 polled = &s->periods_polled;
285 else
286 polled = &us122l->second_periods_polled;
287 if (*polled != s->periods_done) {
288 *polled = s->periods_done;
289 mask = POLLIN | POLLOUT | POLLWRNORM;
290 break;
291 }
292 /* Fall through */
293 mask = 0;
294 break;
295 default:
296 mask = POLLIN | POLLOUT | POLLWRNORM | POLLERR;
297 break;
298 }
299 return mask;
300}
301
302static void us122l_stop(struct us122l *us122l)
303{
304 struct list_head *p;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100305 list_for_each(p, &us122l->midi_list)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200306 snd_usbmidi_input_stop(p);
307
308 usb_stream_stop(&us122l->sk);
309 usb_stream_free(&us122l->sk);
310}
311
312static int us122l_set_sample_rate(struct usb_device *dev, int rate)
313{
314 unsigned int ep = 0x81;
315 unsigned char data[3];
316 int err;
317
318 data[0] = rate;
319 data[1] = rate >> 8;
320 data[2] = rate >> 16;
Daniel Mackde48c7b2010-02-22 23:49:13 +0100321 err = us122l_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
Karsten Wiese030a07e2008-07-30 15:13:29 +0200322 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
Daniel Mackde48c7b2010-02-22 23:49:13 +0100323 UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200324 if (err < 0)
325 snd_printk(KERN_ERR "%d: cannot set freq %d to ep 0x%x\n",
326 dev->devnum, rate, ep);
327 return err;
328}
329
330static bool us122l_start(struct us122l *us122l,
331 unsigned rate, unsigned period_frames)
332{
333 struct list_head *p;
334 int err;
335 unsigned use_packsize = 0;
336 bool success = false;
337
Clemens Ladischa014bba2009-11-16 12:26:30 +0100338 if (us122l->dev->speed == USB_SPEED_HIGH) {
Karsten Wiese030a07e2008-07-30 15:13:29 +0200339 /* The us-122l's descriptor defaults to iso max_packsize 78,
340 which isn't needed for samplerates <= 48000.
341 Lets save some memory:
342 */
343 switch (rate) {
344 case 44100:
345 use_packsize = 36;
346 break;
347 case 48000:
348 use_packsize = 42;
349 break;
350 case 88200:
351 use_packsize = 72;
352 break;
353 }
354 }
Clemens Ladischa014bba2009-11-16 12:26:30 +0100355 if (!usb_stream_new(&us122l->sk, us122l->dev, 1, 2,
Karsten Wiese030a07e2008-07-30 15:13:29 +0200356 rate, use_packsize, period_frames, 6))
357 goto out;
358
Clemens Ladischa014bba2009-11-16 12:26:30 +0100359 err = us122l_set_sample_rate(us122l->dev, rate);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200360 if (err < 0) {
361 us122l_stop(us122l);
362 snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
363 goto out;
364 }
365 err = usb_stream_start(&us122l->sk);
366 if (err < 0) {
367 us122l_stop(us122l);
368 snd_printk(KERN_ERR "us122l_start error %i \n", err);
369 goto out;
370 }
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100371 list_for_each(p, &us122l->midi_list)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200372 snd_usbmidi_input_start(p);
373 success = true;
374out:
375 return success;
376}
377
378static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
379 unsigned cmd, unsigned long arg)
380{
381 struct usb_stream_config *cfg;
382 struct us122l *us122l = hw->private_data;
383 unsigned min_period_frames;
384 int err = 0;
385 bool high_speed;
386
387 if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
388 return -ENOTTY;
389
Li Zefan85385c12009-04-10 09:43:59 +0800390 cfg = memdup_user((void *)arg, sizeof(*cfg));
391 if (IS_ERR(cfg))
392 return PTR_ERR(cfg);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200393
Karsten Wiese030a07e2008-07-30 15:13:29 +0200394 if (cfg->version != USB_STREAM_INTERFACE_VERSION) {
395 err = -ENXIO;
396 goto free;
397 }
Clemens Ladischa014bba2009-11-16 12:26:30 +0100398 high_speed = us122l->dev->speed == USB_SPEED_HIGH;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200399 if ((cfg->sample_rate != 44100 && cfg->sample_rate != 48000 &&
400 (!high_speed ||
401 (cfg->sample_rate != 88200 && cfg->sample_rate != 96000))) ||
402 cfg->frame_size != 6 ||
403 cfg->period_frames > 0x3000) {
404 err = -EINVAL;
405 goto free;
406 }
407 switch (cfg->sample_rate) {
408 case 44100:
409 min_period_frames = 48;
410 break;
411 case 48000:
412 min_period_frames = 52;
413 break;
414 default:
415 min_period_frames = 104;
416 break;
417 }
418 if (!high_speed)
419 min_period_frames <<= 1;
420 if (cfg->period_frames < min_period_frames) {
421 err = -EINVAL;
422 goto free;
423 }
424
425 snd_power_wait(hw->card, SNDRV_CTL_POWER_D0);
426
427 mutex_lock(&us122l->mutex);
428 if (!us122l->master)
429 us122l->master = file;
430 else if (us122l->master != file) {
431 if (memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg))) {
432 err = -EIO;
433 goto unlock;
434 }
435 us122l->slave = file;
436 }
437 if (!us122l->sk.s ||
438 memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg)) ||
439 us122l->sk.s->state == usb_stream_xrun) {
440 us122l_stop(us122l);
441 if (!us122l_start(us122l, cfg->sample_rate, cfg->period_frames))
442 err = -EIO;
443 else
444 err = 1;
445 }
446unlock:
447 mutex_unlock(&us122l->mutex);
448free:
449 kfree(cfg);
450 return err;
451}
452
453#define SND_USB_STREAM_ID "USB STREAM"
454static int usb_stream_hwdep_new(struct snd_card *card)
455{
456 int err;
457 struct snd_hwdep *hw;
Clemens Ladischa014bba2009-11-16 12:26:30 +0100458 struct usb_device *dev = US122L(card)->dev;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200459
460 err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw);
461 if (err < 0)
462 return err;
463
464 hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM;
465 hw->private_data = US122L(card);
466 hw->ops.open = usb_stream_hwdep_open;
467 hw->ops.release = usb_stream_hwdep_release;
468 hw->ops.ioctl = usb_stream_hwdep_ioctl;
469 hw->ops.ioctl_compat = usb_stream_hwdep_ioctl;
470 hw->ops.mmap = usb_stream_hwdep_mmap;
471 hw->ops.poll = usb_stream_hwdep_poll;
472
473 sprintf(hw->name, "/proc/bus/usb/%03d/%03d/hwdeppcm",
474 dev->bus->busnum, dev->devnum);
475 return 0;
476}
477
478
479static bool us122l_create_card(struct snd_card *card)
480{
481 int err;
482 struct us122l *us122l = US122L(card);
483
Tobias Hansen2b6f6c02009-12-07 19:08:19 +0100484 if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
485 us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
Clemens Ladischa014bba2009-11-16 12:26:30 +0100486 err = usb_set_interface(us122l->dev, 0, 1);
Tobias Hansen4f272342009-09-22 16:52:08 +0200487 if (err) {
488 snd_printk(KERN_ERR "usb_set_interface error \n");
489 return false;
490 }
491 }
Clemens Ladischa014bba2009-11-16 12:26:30 +0100492 err = usb_set_interface(us122l->dev, 1, 1);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200493 if (err) {
494 snd_printk(KERN_ERR "usb_set_interface error \n");
495 return false;
496 }
497
Clemens Ladischa014bba2009-11-16 12:26:30 +0100498 pt_info_set(us122l->dev, 0x11);
499 pt_info_set(us122l->dev, 0x10);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200500
501 if (!us122l_start(us122l, 44100, 256))
502 return false;
503
Tobias Hansen2b6f6c02009-12-07 19:08:19 +0100504 if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
505 us122l->dev->descriptor.idProduct == USB_ID_US144MKII)
Tobias Hansen4f272342009-09-22 16:52:08 +0200506 err = us144_create_usbmidi(card);
507 else
508 err = us122l_create_usbmidi(card);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200509 if (err < 0) {
510 snd_printk(KERN_ERR "us122l_create_usbmidi error %i \n", err);
511 us122l_stop(us122l);
512 return false;
513 }
514 err = usb_stream_hwdep_new(card);
515 if (err < 0) {
516/* release the midi resources */
517 struct list_head *p;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100518 list_for_each(p, &us122l->midi_list)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200519 snd_usbmidi_disconnect(p);
520
521 us122l_stop(us122l);
522 return false;
523 }
524 return true;
525}
526
Karsten Wiese5d4af1b2009-04-20 13:01:21 +0200527static void snd_us122l_free(struct snd_card *card)
528{
529 struct us122l *us122l = US122L(card);
Clemens Ladischa014bba2009-11-16 12:26:30 +0100530 int index = us122l->card_index;
Karsten Wiese5d4af1b2009-04-20 13:01:21 +0200531 if (index >= 0 && index < SNDRV_CARDS)
532 snd_us122l_card_used[index] = 0;
533}
534
Takashi Iwai51721f72008-12-28 16:55:08 +0100535static int usx2y_create_card(struct usb_device *device, struct snd_card **cardp)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200536{
537 int dev;
538 struct snd_card *card;
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100539 int err;
540
Karsten Wiese030a07e2008-07-30 15:13:29 +0200541 for (dev = 0; dev < SNDRV_CARDS; ++dev)
542 if (enable[dev] && !snd_us122l_card_used[dev])
543 break;
544 if (dev >= SNDRV_CARDS)
Takashi Iwai51721f72008-12-28 16:55:08 +0100545 return -ENODEV;
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100546 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
547 sizeof(struct us122l), &card);
548 if (err < 0)
Takashi Iwai51721f72008-12-28 16:55:08 +0100549 return err;
Clemens Ladischa014bba2009-11-16 12:26:30 +0100550 snd_us122l_card_used[US122L(card)->card_index = dev] = 1;
Karsten Wiese5d4af1b2009-04-20 13:01:21 +0200551 card->private_free = snd_us122l_free;
Clemens Ladischa014bba2009-11-16 12:26:30 +0100552 US122L(card)->dev = device;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200553 mutex_init(&US122L(card)->mutex);
554 init_waitqueue_head(&US122L(card)->sk.sleep);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100555 INIT_LIST_HEAD(&US122L(card)->midi_list);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200556 strcpy(card->driver, "USB "NAME_ALLCAPS"");
557 sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
558 sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
559 card->shortname,
560 le16_to_cpu(device->descriptor.idVendor),
561 le16_to_cpu(device->descriptor.idProduct),
562 0,
Clemens Ladischa014bba2009-11-16 12:26:30 +0100563 US122L(card)->dev->bus->busnum,
564 US122L(card)->dev->devnum
Karsten Wiese030a07e2008-07-30 15:13:29 +0200565 );
Takashi Iwai51721f72008-12-28 16:55:08 +0100566 *cardp = card;
567 return 0;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200568}
569
Takashi Iwai51721f72008-12-28 16:55:08 +0100570static int us122l_usb_probe(struct usb_interface *intf,
571 const struct usb_device_id *device_id,
572 struct snd_card **cardp)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200573{
574 struct usb_device *device = interface_to_usbdev(intf);
Takashi Iwai51721f72008-12-28 16:55:08 +0100575 struct snd_card *card;
576 int err;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200577
Takashi Iwai51721f72008-12-28 16:55:08 +0100578 err = usx2y_create_card(device, &card);
579 if (err < 0)
580 return err;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200581
Takashi Iwai3f5d3462009-07-02 11:51:44 +0200582 snd_card_set_dev(card, &intf->dev);
Takashi Iwai51721f72008-12-28 16:55:08 +0100583 if (!us122l_create_card(card)) {
Karsten Wiese030a07e2008-07-30 15:13:29 +0200584 snd_card_free(card);
Takashi Iwai51721f72008-12-28 16:55:08 +0100585 return -EINVAL;
586 }
587
588 err = snd_card_register(card);
589 if (err < 0) {
590 snd_card_free(card);
591 return err;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200592 }
593
Tobias Hansen4f272342009-09-22 16:52:08 +0200594 usb_get_intf(usb_ifnum_to_if(device, 0));
Karsten Wiese030a07e2008-07-30 15:13:29 +0200595 usb_get_dev(device);
Takashi Iwai51721f72008-12-28 16:55:08 +0100596 *cardp = card;
597 return 0;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200598}
599
600static int snd_us122l_probe(struct usb_interface *intf,
601 const struct usb_device_id *id)
602{
Tobias Hansen4f272342009-09-22 16:52:08 +0200603 struct usb_device *device = interface_to_usbdev(intf);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200604 struct snd_card *card;
Takashi Iwai51721f72008-12-28 16:55:08 +0100605 int err;
606
Tobias Hansen2b6f6c02009-12-07 19:08:19 +0100607 if ((device->descriptor.idProduct == USB_ID_US144 ||
608 device->descriptor.idProduct == USB_ID_US144MKII)
Tobias Hansen4f272342009-09-22 16:52:08 +0200609 && device->speed == USB_SPEED_HIGH) {
610 snd_printk(KERN_ERR "disable ehci-hcd to run US-144 \n");
Tobias Hansena688e482009-10-12 16:24:15 +0200611 return -ENODEV;
Tobias Hansen4f272342009-09-22 16:52:08 +0200612 }
613
Karsten Wiese030a07e2008-07-30 15:13:29 +0200614 snd_printdd(KERN_DEBUG"%p:%i\n",
615 intf, intf->cur_altsetting->desc.bInterfaceNumber);
616 if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
617 return 0;
618
Takashi Iwai51721f72008-12-28 16:55:08 +0100619 err = us122l_usb_probe(usb_get_intf(intf), id, &card);
620 if (err < 0) {
621 usb_put_intf(intf);
622 return err;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200623 }
624
Takashi Iwai51721f72008-12-28 16:55:08 +0100625 usb_set_intfdata(intf, card);
626 return 0;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200627}
628
629static void snd_us122l_disconnect(struct usb_interface *intf)
630{
631 struct snd_card *card;
632 struct us122l *us122l;
633 struct list_head *p;
634
635 card = usb_get_intfdata(intf);
636 if (!card)
637 return;
638
639 snd_card_disconnect(card);
640
641 us122l = US122L(card);
642 mutex_lock(&us122l->mutex);
643 us122l_stop(us122l);
644 mutex_unlock(&us122l->mutex);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200645
646/* release the midi resources */
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100647 list_for_each(p, &us122l->midi_list) {
Karsten Wiese030a07e2008-07-30 15:13:29 +0200648 snd_usbmidi_disconnect(p);
649 }
650
Clemens Ladischa014bba2009-11-16 12:26:30 +0100651 usb_put_intf(usb_ifnum_to_if(us122l->dev, 0));
652 usb_put_intf(usb_ifnum_to_if(us122l->dev, 1));
653 usb_put_dev(us122l->dev);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200654
655 while (atomic_read(&us122l->mmap_count))
656 msleep(500);
657
658 snd_card_free(card);
659}
660
661static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message)
662{
663 struct snd_card *card;
664 struct us122l *us122l;
665 struct list_head *p;
666
Julia Lawallf4e97492009-01-01 18:14:35 +0100667 card = usb_get_intfdata(intf);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200668 if (!card)
669 return 0;
670 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
671
672 us122l = US122L(card);
673 if (!us122l)
674 return 0;
675
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100676 list_for_each(p, &us122l->midi_list)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200677 snd_usbmidi_input_stop(p);
678
679 mutex_lock(&us122l->mutex);
680 usb_stream_stop(&us122l->sk);
681 mutex_unlock(&us122l->mutex);
682
683 return 0;
684}
685
686static int snd_us122l_resume(struct usb_interface *intf)
687{
688 struct snd_card *card;
689 struct us122l *us122l;
690 struct list_head *p;
691 int err;
692
Julia Lawallf4e97492009-01-01 18:14:35 +0100693 card = usb_get_intfdata(intf);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200694 if (!card)
695 return 0;
696
697 us122l = US122L(card);
698 if (!us122l)
699 return 0;
700
701 mutex_lock(&us122l->mutex);
702 /* needed, doesn't restart without: */
Tobias Hansen2b6f6c02009-12-07 19:08:19 +0100703 if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
704 us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
Clemens Ladischa014bba2009-11-16 12:26:30 +0100705 err = usb_set_interface(us122l->dev, 0, 1);
Tobias Hansen4f272342009-09-22 16:52:08 +0200706 if (err) {
707 snd_printk(KERN_ERR "usb_set_interface error \n");
708 goto unlock;
709 }
710 }
Clemens Ladischa014bba2009-11-16 12:26:30 +0100711 err = usb_set_interface(us122l->dev, 1, 1);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200712 if (err) {
713 snd_printk(KERN_ERR "usb_set_interface error \n");
714 goto unlock;
715 }
716
Clemens Ladischa014bba2009-11-16 12:26:30 +0100717 pt_info_set(us122l->dev, 0x11);
718 pt_info_set(us122l->dev, 0x10);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200719
Clemens Ladischa014bba2009-11-16 12:26:30 +0100720 err = us122l_set_sample_rate(us122l->dev,
Karsten Wiese030a07e2008-07-30 15:13:29 +0200721 us122l->sk.s->cfg.sample_rate);
722 if (err < 0) {
723 snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
724 goto unlock;
725 }
726 err = usb_stream_start(&us122l->sk);
727 if (err)
728 goto unlock;
729
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100730 list_for_each(p, &us122l->midi_list)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200731 snd_usbmidi_input_start(p);
732unlock:
733 mutex_unlock(&us122l->mutex);
734 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
735 return err;
736}
737
738static struct usb_device_id snd_us122l_usb_id_table[] = {
739 {
740 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
741 .idVendor = 0x0644,
742 .idProduct = USB_ID_US122L
743 },
Tobias Hansen4f272342009-09-22 16:52:08 +0200744 { /* US-144 only works at USB1.1! Disable module ehci-hcd. */
745 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
746 .idVendor = 0x0644,
747 .idProduct = USB_ID_US144
748 },
Tobias Hansen2b6f6c02009-12-07 19:08:19 +0100749 {
750 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
751 .idVendor = 0x0644,
752 .idProduct = USB_ID_US122MKII
753 },
754 {
755 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
756 .idVendor = 0x0644,
757 .idProduct = USB_ID_US144MKII
758 },
Karsten Wiese030a07e2008-07-30 15:13:29 +0200759 { /* terminator */ }
760};
761
762MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table);
763static struct usb_driver snd_us122l_usb_driver = {
764 .name = "snd-usb-us122l",
765 .probe = snd_us122l_probe,
766 .disconnect = snd_us122l_disconnect,
767 .suspend = snd_us122l_suspend,
768 .resume = snd_us122l_resume,
769 .reset_resume = snd_us122l_resume,
770 .id_table = snd_us122l_usb_id_table,
771 .supports_autosuspend = 1
772};
773
774
775static int __init snd_us122l_module_init(void)
776{
777 return usb_register(&snd_us122l_usb_driver);
778}
779
780static void __exit snd_us122l_module_exit(void)
781{
782 usb_deregister(&snd_us122l_usb_driver);
783}
784
785module_init(snd_us122l_module_init)
786module_exit(snd_us122l_module_exit)