blob: e118bdca983d435f323139ac4fa5b8fd9b47fae4 [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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040022#include <linux/module.h>
Karsten Wiese030a07e2008-07-30 15:13:29 +020023#include <sound/core.h>
24#include <sound/hwdep.h>
25#include <sound/pcm.h>
26#include <sound/initval.h>
27#define MODNAME "US122L"
28#include "usb_stream.c"
29#include "../usbaudio.h"
Daniel Macke5779992010-03-04 19:46:13 +010030#include "../midi.h"
Karsten Wiese030a07e2008-07-30 15:13:29 +020031#include "us122l.h"
32
33MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
34MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5");
35MODULE_LICENSE("GPL");
36
37static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
38static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
39 /* Enable this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103040static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
Karsten Wiese030a07e2008-07-30 15:13:29 +020041
42module_param_array(index, int, NULL, 0444);
43MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
44module_param_array(id, charp, NULL, 0444);
45MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
46module_param_array(enable, bool, NULL, 0444);
47MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
48
49static int snd_us122l_card_used[SNDRV_CARDS];
50
51
52static int us122l_create_usbmidi(struct snd_card *card)
53{
54 static struct snd_usb_midi_endpoint_info quirk_data = {
55 .out_ep = 4,
56 .in_ep = 3,
57 .out_cables = 0x001,
58 .in_cables = 0x001
59 };
60 static struct snd_usb_audio_quirk quirk = {
61 .vendor_name = "US122L",
62 .product_name = NAME_ALLCAPS,
63 .ifnum = 1,
64 .type = QUIRK_MIDI_US122L,
65 .data = &quirk_data
66 };
Clemens Ladischa014bba2009-11-16 12:26:30 +010067 struct usb_device *dev = US122L(card)->dev;
Karsten Wiese030a07e2008-07-30 15:13:29 +020068 struct usb_interface *iface = usb_ifnum_to_if(dev, 1);
69
Clemens Ladischd82af9f2009-11-16 12:23:46 +010070 return snd_usbmidi_create(card, iface,
71 &US122L(card)->midi_list, &quirk);
Karsten Wiese030a07e2008-07-30 15:13:29 +020072}
73
Tobias Hansen4f272342009-09-22 16:52:08 +020074static int us144_create_usbmidi(struct snd_card *card)
75{
76 static struct snd_usb_midi_endpoint_info quirk_data = {
77 .out_ep = 4,
78 .in_ep = 3,
79 .out_cables = 0x001,
80 .in_cables = 0x001
81 };
82 static struct snd_usb_audio_quirk quirk = {
83 .vendor_name = "US144",
84 .product_name = NAME_ALLCAPS,
85 .ifnum = 0,
86 .type = QUIRK_MIDI_US122L,
87 .data = &quirk_data
88 };
Clemens Ladischa014bba2009-11-16 12:26:30 +010089 struct usb_device *dev = US122L(card)->dev;
Tobias Hansen4f272342009-09-22 16:52:08 +020090 struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
91
Clemens Ladischd82af9f2009-11-16 12:23:46 +010092 return snd_usbmidi_create(card, iface,
93 &US122L(card)->midi_list, &quirk);
Tobias Hansen4f272342009-09-22 16:52:08 +020094}
95
Karsten Wiese030a07e2008-07-30 15:13:29 +020096/*
97 * Wrapper for usb_control_msg().
98 * Allocates a temp buffer to prevent dmaing from/to the stack.
99 */
100static int us122l_ctl_msg(struct usb_device *dev, unsigned int pipe,
101 __u8 request, __u8 requesttype,
102 __u16 value, __u16 index, void *data,
103 __u16 size, int timeout)
104{
105 int err;
106 void *buf = NULL;
107
108 if (size > 0) {
109 buf = kmemdup(data, size, GFP_KERNEL);
110 if (!buf)
111 return -ENOMEM;
112 }
113 err = usb_control_msg(dev, pipe, request, requesttype,
114 value, index, buf, size, timeout);
115 if (size > 0) {
116 memcpy(data, buf, size);
117 kfree(buf);
118 }
119 return err;
120}
121
122static void pt_info_set(struct usb_device *dev, u8 v)
123{
124 int ret;
125
126 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
127 'I',
128 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
129 v, 0, NULL, 0, 1000);
130 snd_printdd(KERN_DEBUG "%i\n", ret);
131}
132
133static void usb_stream_hwdep_vm_open(struct vm_area_struct *area)
134{
135 struct us122l *us122l = area->vm_private_data;
136 atomic_inc(&us122l->mmap_count);
137 snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
138}
139
Dave Jiang11bac802017-02-24 14:56:41 -0800140static int usb_stream_hwdep_vm_fault(struct vm_fault *vmf)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200141{
142 unsigned long offset;
143 struct page *page;
144 void *vaddr;
Dave Jiang11bac802017-02-24 14:56:41 -0800145 struct us122l *us122l = vmf->vma->vm_private_data;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200146 struct usb_stream *s;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200147
148 mutex_lock(&us122l->mutex);
149 s = us122l->sk.s;
150 if (!s)
Li Zefan428ffb72008-10-15 10:07:23 +0800151 goto unlock;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200152
153 offset = vmf->pgoff << PAGE_SHIFT;
154 if (offset < PAGE_ALIGN(s->read_size))
155 vaddr = (char *)s + offset;
156 else {
157 offset -= PAGE_ALIGN(s->read_size);
158 if (offset >= PAGE_ALIGN(s->write_size))
Li Zefan428ffb72008-10-15 10:07:23 +0800159 goto unlock;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200160
161 vaddr = us122l->sk.write_page + offset;
162 }
163 page = virt_to_page(vaddr);
164
165 get_page(page);
166 mutex_unlock(&us122l->mutex);
167
168 vmf->page = page;
Li Zefan428ffb72008-10-15 10:07:23 +0800169
170 return 0;
171unlock:
172 mutex_unlock(&us122l->mutex);
173 return VM_FAULT_SIGBUS;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200174}
175
176static void usb_stream_hwdep_vm_close(struct vm_area_struct *area)
177{
178 struct us122l *us122l = area->vm_private_data;
179 atomic_dec(&us122l->mmap_count);
180 snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
181}
182
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400183static const struct vm_operations_struct usb_stream_hwdep_vm_ops = {
Karsten Wiese030a07e2008-07-30 15:13:29 +0200184 .open = usb_stream_hwdep_vm_open,
185 .fault = usb_stream_hwdep_vm_fault,
186 .close = usb_stream_hwdep_vm_close,
187};
188
189
190static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file)
191{
192 struct us122l *us122l = hw->private_data;
193 struct usb_interface *iface;
194 snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
195 if (hw->used >= 2)
196 return -EBUSY;
197
198 if (!us122l->first)
199 us122l->first = file;
Tobias Hansen4f272342009-09-22 16:52:08 +0200200
Tobias Hansen2b6f6c02009-12-07 19:08:19 +0100201 if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
202 us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
Clemens Ladischa014bba2009-11-16 12:26:30 +0100203 iface = usb_ifnum_to_if(us122l->dev, 0);
Tobias Hansen4f272342009-09-22 16:52:08 +0200204 usb_autopm_get_interface(iface);
205 }
Clemens Ladischa014bba2009-11-16 12:26:30 +0100206 iface = usb_ifnum_to_if(us122l->dev, 1);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200207 usb_autopm_get_interface(iface);
208 return 0;
209}
210
211static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
212{
213 struct us122l *us122l = hw->private_data;
Tobias Hansen4f272342009-09-22 16:52:08 +0200214 struct usb_interface *iface;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200215 snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
Tobias Hansen4f272342009-09-22 16:52:08 +0200216
Tobias Hansen2b6f6c02009-12-07 19:08:19 +0100217 if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
218 us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
Clemens Ladischa014bba2009-11-16 12:26:30 +0100219 iface = usb_ifnum_to_if(us122l->dev, 0);
Tobias Hansen4f272342009-09-22 16:52:08 +0200220 usb_autopm_put_interface(iface);
221 }
Clemens Ladischa014bba2009-11-16 12:26:30 +0100222 iface = usb_ifnum_to_if(us122l->dev, 1);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200223 usb_autopm_put_interface(iface);
224 if (us122l->first == file)
225 us122l->first = NULL;
226 mutex_lock(&us122l->mutex);
227 if (us122l->master == file)
228 us122l->master = us122l->slave;
229
230 us122l->slave = NULL;
231 mutex_unlock(&us122l->mutex);
232 return 0;
233}
234
235static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
236 struct file *filp, struct vm_area_struct *area)
237{
238 unsigned long size = area->vm_end - area->vm_start;
239 struct us122l *us122l = hw->private_data;
240 unsigned long offset;
241 struct usb_stream *s;
242 int err = 0;
243 bool read;
244
245 offset = area->vm_pgoff << PAGE_SHIFT;
246 mutex_lock(&us122l->mutex);
247 s = us122l->sk.s;
248 read = offset < s->read_size;
249 if (read && area->vm_flags & VM_WRITE) {
250 err = -EPERM;
251 goto out;
252 }
253 snd_printdd(KERN_DEBUG "%lu %u\n", size,
254 read ? s->read_size : s->write_size);
255 /* if userspace tries to mmap beyond end of our buffer, fail */
256 if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
257 snd_printk(KERN_WARNING "%lu > %u\n", size,
258 read ? s->read_size : s->write_size);
259 err = -EINVAL;
260 goto out;
261 }
262
263 area->vm_ops = &usb_stream_hwdep_vm_ops;
Takashi Iwaiac536a82013-10-14 16:02:15 +0200264 area->vm_flags |= VM_DONTDUMP;
265 if (!read)
266 area->vm_flags |= VM_DONTEXPAND;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200267 area->vm_private_data = us122l;
268 atomic_inc(&us122l->mmap_count);
269out:
270 mutex_unlock(&us122l->mutex);
271 return err;
272}
273
274static unsigned int usb_stream_hwdep_poll(struct snd_hwdep *hw,
275 struct file *file, poll_table *wait)
276{
277 struct us122l *us122l = hw->private_data;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200278 unsigned *polled;
279 unsigned int mask;
280
281 poll_wait(file, &us122l->sk.sleep, wait);
282
Karsten Wiesecdce2db2011-01-04 01:20:37 +0100283 mask = POLLIN | POLLOUT | POLLWRNORM | POLLERR;
284 if (mutex_trylock(&us122l->mutex)) {
285 struct usb_stream *s = us122l->sk.s;
286 if (s && s->state == usb_stream_ready) {
287 if (us122l->first == file)
288 polled = &s->periods_polled;
289 else
290 polled = &us122l->second_periods_polled;
291 if (*polled != s->periods_done) {
292 *polled = s->periods_done;
293 mask = POLLIN | POLLOUT | POLLWRNORM;
294 } else
295 mask = 0;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200296 }
Karsten Wiesecdce2db2011-01-04 01:20:37 +0100297 mutex_unlock(&us122l->mutex);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200298 }
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;
Karsten Wiesecdce2db2011-01-04 01:20:37 +0100383 struct usb_stream *s;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200384 unsigned min_period_frames;
385 int err = 0;
386 bool high_speed;
387
388 if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
389 return -ENOTTY;
390
Li Zefan85385c12009-04-10 09:43:59 +0800391 cfg = memdup_user((void *)arg, sizeof(*cfg));
392 if (IS_ERR(cfg))
393 return PTR_ERR(cfg);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200394
Karsten Wiese030a07e2008-07-30 15:13:29 +0200395 if (cfg->version != USB_STREAM_INTERFACE_VERSION) {
396 err = -ENXIO;
397 goto free;
398 }
Clemens Ladischa014bba2009-11-16 12:26:30 +0100399 high_speed = us122l->dev->speed == USB_SPEED_HIGH;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200400 if ((cfg->sample_rate != 44100 && cfg->sample_rate != 48000 &&
401 (!high_speed ||
402 (cfg->sample_rate != 88200 && cfg->sample_rate != 96000))) ||
403 cfg->frame_size != 6 ||
404 cfg->period_frames > 0x3000) {
405 err = -EINVAL;
406 goto free;
407 }
408 switch (cfg->sample_rate) {
409 case 44100:
410 min_period_frames = 48;
411 break;
412 case 48000:
413 min_period_frames = 52;
414 break;
415 default:
416 min_period_frames = 104;
417 break;
418 }
419 if (!high_speed)
420 min_period_frames <<= 1;
421 if (cfg->period_frames < min_period_frames) {
422 err = -EINVAL;
423 goto free;
424 }
425
426 snd_power_wait(hw->card, SNDRV_CTL_POWER_D0);
427
428 mutex_lock(&us122l->mutex);
Karsten Wiesecdce2db2011-01-04 01:20:37 +0100429 s = us122l->sk.s;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200430 if (!us122l->master)
431 us122l->master = file;
432 else if (us122l->master != file) {
Karsten Wiesecdce2db2011-01-04 01:20:37 +0100433 if (!s || memcmp(cfg, &s->cfg, sizeof(*cfg))) {
Karsten Wiese030a07e2008-07-30 15:13:29 +0200434 err = -EIO;
435 goto unlock;
436 }
437 us122l->slave = file;
438 }
Karsten Wiesecdce2db2011-01-04 01:20:37 +0100439 if (!s || memcmp(cfg, &s->cfg, sizeof(*cfg)) ||
440 s->state == usb_stream_xrun) {
Karsten Wiese030a07e2008-07-30 15:13:29 +0200441 us122l_stop(us122l);
442 if (!us122l_start(us122l, cfg->sample_rate, cfg->period_frames))
443 err = -EIO;
444 else
445 err = 1;
446 }
447unlock:
448 mutex_unlock(&us122l->mutex);
449free:
450 kfree(cfg);
Karsten Wiesecdce2db2011-01-04 01:20:37 +0100451 wake_up_all(&us122l->sk.sleep);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200452 return err;
453}
454
455#define SND_USB_STREAM_ID "USB STREAM"
456static int usb_stream_hwdep_new(struct snd_card *card)
457{
458 int err;
459 struct snd_hwdep *hw;
Clemens Ladischa014bba2009-11-16 12:26:30 +0100460 struct usb_device *dev = US122L(card)->dev;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200461
462 err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw);
463 if (err < 0)
464 return err;
465
466 hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM;
467 hw->private_data = US122L(card);
468 hw->ops.open = usb_stream_hwdep_open;
469 hw->ops.release = usb_stream_hwdep_release;
470 hw->ops.ioctl = usb_stream_hwdep_ioctl;
471 hw->ops.ioctl_compat = usb_stream_hwdep_ioctl;
472 hw->ops.mmap = usb_stream_hwdep_mmap;
473 hw->ops.poll = usb_stream_hwdep_poll;
474
Mauro Carvalho Chehaba5f86612017-04-16 21:51:11 -0300475 sprintf(hw->name, "/dev/bus/usb/%03d/%03d/hwdeppcm",
Karsten Wiese030a07e2008-07-30 15:13:29 +0200476 dev->bus->busnum, dev->devnum);
477 return 0;
478}
479
480
481static bool us122l_create_card(struct snd_card *card)
482{
483 int err;
484 struct us122l *us122l = US122L(card);
485
Tobias Hansen2b6f6c02009-12-07 19:08:19 +0100486 if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
487 us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
Clemens Ladischa014bba2009-11-16 12:26:30 +0100488 err = usb_set_interface(us122l->dev, 0, 1);
Tobias Hansen4f272342009-09-22 16:52:08 +0200489 if (err) {
490 snd_printk(KERN_ERR "usb_set_interface error \n");
491 return false;
492 }
493 }
Clemens Ladischa014bba2009-11-16 12:26:30 +0100494 err = usb_set_interface(us122l->dev, 1, 1);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200495 if (err) {
496 snd_printk(KERN_ERR "usb_set_interface error \n");
497 return false;
498 }
499
Clemens Ladischa014bba2009-11-16 12:26:30 +0100500 pt_info_set(us122l->dev, 0x11);
501 pt_info_set(us122l->dev, 0x10);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200502
503 if (!us122l_start(us122l, 44100, 256))
504 return false;
505
Tobias Hansen2b6f6c02009-12-07 19:08:19 +0100506 if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
507 us122l->dev->descriptor.idProduct == USB_ID_US144MKII)
Tobias Hansen4f272342009-09-22 16:52:08 +0200508 err = us144_create_usbmidi(card);
509 else
510 err = us122l_create_usbmidi(card);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200511 if (err < 0) {
512 snd_printk(KERN_ERR "us122l_create_usbmidi error %i \n", err);
513 us122l_stop(us122l);
514 return false;
515 }
516 err = usb_stream_hwdep_new(card);
517 if (err < 0) {
518/* release the midi resources */
519 struct list_head *p;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100520 list_for_each(p, &us122l->midi_list)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200521 snd_usbmidi_disconnect(p);
522
523 us122l_stop(us122l);
524 return false;
525 }
526 return true;
527}
528
Karsten Wiese5d4af1b2009-04-20 13:01:21 +0200529static void snd_us122l_free(struct snd_card *card)
530{
531 struct us122l *us122l = US122L(card);
Clemens Ladischa014bba2009-11-16 12:26:30 +0100532 int index = us122l->card_index;
Karsten Wiese5d4af1b2009-04-20 13:01:21 +0200533 if (index >= 0 && index < SNDRV_CARDS)
534 snd_us122l_card_used[index] = 0;
535}
536
Takashi Iwai874b8d42014-01-29 14:22:20 +0100537static int usx2y_create_card(struct usb_device *device,
538 struct usb_interface *intf,
539 struct snd_card **cardp)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200540{
541 int dev;
542 struct snd_card *card;
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100543 int err;
544
Karsten Wiese030a07e2008-07-30 15:13:29 +0200545 for (dev = 0; dev < SNDRV_CARDS; ++dev)
546 if (enable[dev] && !snd_us122l_card_used[dev])
547 break;
548 if (dev >= SNDRV_CARDS)
Takashi Iwai51721f72008-12-28 16:55:08 +0100549 return -ENODEV;
Takashi Iwai874b8d42014-01-29 14:22:20 +0100550 err = snd_card_new(&intf->dev, index[dev], id[dev], THIS_MODULE,
551 sizeof(struct us122l), &card);
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100552 if (err < 0)
Takashi Iwai51721f72008-12-28 16:55:08 +0100553 return err;
Clemens Ladischa014bba2009-11-16 12:26:30 +0100554 snd_us122l_card_used[US122L(card)->card_index = dev] = 1;
Karsten Wiese5d4af1b2009-04-20 13:01:21 +0200555 card->private_free = snd_us122l_free;
Clemens Ladischa014bba2009-11-16 12:26:30 +0100556 US122L(card)->dev = device;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200557 mutex_init(&US122L(card)->mutex);
558 init_waitqueue_head(&US122L(card)->sk.sleep);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100559 INIT_LIST_HEAD(&US122L(card)->midi_list);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200560 strcpy(card->driver, "USB "NAME_ALLCAPS"");
561 sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
562 sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
563 card->shortname,
564 le16_to_cpu(device->descriptor.idVendor),
565 le16_to_cpu(device->descriptor.idProduct),
566 0,
Clemens Ladischa014bba2009-11-16 12:26:30 +0100567 US122L(card)->dev->bus->busnum,
568 US122L(card)->dev->devnum
Karsten Wiese030a07e2008-07-30 15:13:29 +0200569 );
Takashi Iwai51721f72008-12-28 16:55:08 +0100570 *cardp = card;
571 return 0;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200572}
573
Takashi Iwai51721f72008-12-28 16:55:08 +0100574static int us122l_usb_probe(struct usb_interface *intf,
575 const struct usb_device_id *device_id,
576 struct snd_card **cardp)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200577{
578 struct usb_device *device = interface_to_usbdev(intf);
Takashi Iwai51721f72008-12-28 16:55:08 +0100579 struct snd_card *card;
580 int err;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200581
Takashi Iwai874b8d42014-01-29 14:22:20 +0100582 err = usx2y_create_card(device, intf, &card);
Takashi Iwai51721f72008-12-28 16:55:08 +0100583 if (err < 0)
584 return err;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200585
Takashi Iwai51721f72008-12-28 16:55:08 +0100586 if (!us122l_create_card(card)) {
Karsten Wiese030a07e2008-07-30 15:13:29 +0200587 snd_card_free(card);
Takashi Iwai51721f72008-12-28 16:55:08 +0100588 return -EINVAL;
589 }
590
591 err = snd_card_register(card);
592 if (err < 0) {
593 snd_card_free(card);
594 return err;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200595 }
596
Tobias Hansen4f272342009-09-22 16:52:08 +0200597 usb_get_intf(usb_ifnum_to_if(device, 0));
Karsten Wiese030a07e2008-07-30 15:13:29 +0200598 usb_get_dev(device);
Takashi Iwai51721f72008-12-28 16:55:08 +0100599 *cardp = card;
600 return 0;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200601}
602
603static int snd_us122l_probe(struct usb_interface *intf,
604 const struct usb_device_id *id)
605{
Tobias Hansen4f272342009-09-22 16:52:08 +0200606 struct usb_device *device = interface_to_usbdev(intf);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200607 struct snd_card *card;
Takashi Iwai51721f72008-12-28 16:55:08 +0100608 int err;
609
Tobias Hansen2b6f6c02009-12-07 19:08:19 +0100610 if ((device->descriptor.idProduct == USB_ID_US144 ||
611 device->descriptor.idProduct == USB_ID_US144MKII)
Tobias Hansen4f272342009-09-22 16:52:08 +0200612 && device->speed == USB_SPEED_HIGH) {
613 snd_printk(KERN_ERR "disable ehci-hcd to run US-144 \n");
Tobias Hansena688e482009-10-12 16:24:15 +0200614 return -ENODEV;
Tobias Hansen4f272342009-09-22 16:52:08 +0200615 }
616
Karsten Wiese030a07e2008-07-30 15:13:29 +0200617 snd_printdd(KERN_DEBUG"%p:%i\n",
618 intf, intf->cur_altsetting->desc.bInterfaceNumber);
619 if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
620 return 0;
621
Takashi Iwai51721f72008-12-28 16:55:08 +0100622 err = us122l_usb_probe(usb_get_intf(intf), id, &card);
623 if (err < 0) {
624 usb_put_intf(intf);
625 return err;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200626 }
627
Takashi Iwai51721f72008-12-28 16:55:08 +0100628 usb_set_intfdata(intf, card);
629 return 0;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200630}
631
632static void snd_us122l_disconnect(struct usb_interface *intf)
633{
634 struct snd_card *card;
635 struct us122l *us122l;
636 struct list_head *p;
637
638 card = usb_get_intfdata(intf);
639 if (!card)
640 return;
641
642 snd_card_disconnect(card);
643
644 us122l = US122L(card);
645 mutex_lock(&us122l->mutex);
646 us122l_stop(us122l);
647 mutex_unlock(&us122l->mutex);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200648
649/* release the midi resources */
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100650 list_for_each(p, &us122l->midi_list) {
Karsten Wiese030a07e2008-07-30 15:13:29 +0200651 snd_usbmidi_disconnect(p);
652 }
653
Clemens Ladischa014bba2009-11-16 12:26:30 +0100654 usb_put_intf(usb_ifnum_to_if(us122l->dev, 0));
655 usb_put_intf(usb_ifnum_to_if(us122l->dev, 1));
656 usb_put_dev(us122l->dev);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200657
658 while (atomic_read(&us122l->mmap_count))
659 msleep(500);
660
661 snd_card_free(card);
662}
663
664static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message)
665{
666 struct snd_card *card;
667 struct us122l *us122l;
668 struct list_head *p;
669
Julia Lawallf4e97492009-01-01 18:14:35 +0100670 card = usb_get_intfdata(intf);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200671 if (!card)
672 return 0;
673 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
674
675 us122l = US122L(card);
676 if (!us122l)
677 return 0;
678
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100679 list_for_each(p, &us122l->midi_list)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200680 snd_usbmidi_input_stop(p);
681
682 mutex_lock(&us122l->mutex);
683 usb_stream_stop(&us122l->sk);
684 mutex_unlock(&us122l->mutex);
685
686 return 0;
687}
688
689static int snd_us122l_resume(struct usb_interface *intf)
690{
691 struct snd_card *card;
692 struct us122l *us122l;
693 struct list_head *p;
694 int err;
695
Julia Lawallf4e97492009-01-01 18:14:35 +0100696 card = usb_get_intfdata(intf);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200697 if (!card)
698 return 0;
699
700 us122l = US122L(card);
701 if (!us122l)
702 return 0;
703
704 mutex_lock(&us122l->mutex);
705 /* needed, doesn't restart without: */
Tobias Hansen2b6f6c02009-12-07 19:08:19 +0100706 if (us122l->dev->descriptor.idProduct == USB_ID_US144 ||
707 us122l->dev->descriptor.idProduct == USB_ID_US144MKII) {
Clemens Ladischa014bba2009-11-16 12:26:30 +0100708 err = usb_set_interface(us122l->dev, 0, 1);
Tobias Hansen4f272342009-09-22 16:52:08 +0200709 if (err) {
710 snd_printk(KERN_ERR "usb_set_interface error \n");
711 goto unlock;
712 }
713 }
Clemens Ladischa014bba2009-11-16 12:26:30 +0100714 err = usb_set_interface(us122l->dev, 1, 1);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200715 if (err) {
716 snd_printk(KERN_ERR "usb_set_interface error \n");
717 goto unlock;
718 }
719
Clemens Ladischa014bba2009-11-16 12:26:30 +0100720 pt_info_set(us122l->dev, 0x11);
721 pt_info_set(us122l->dev, 0x10);
Karsten Wiese030a07e2008-07-30 15:13:29 +0200722
Clemens Ladischa014bba2009-11-16 12:26:30 +0100723 err = us122l_set_sample_rate(us122l->dev,
Karsten Wiese030a07e2008-07-30 15:13:29 +0200724 us122l->sk.s->cfg.sample_rate);
725 if (err < 0) {
726 snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
727 goto unlock;
728 }
729 err = usb_stream_start(&us122l->sk);
730 if (err)
731 goto unlock;
732
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100733 list_for_each(p, &us122l->midi_list)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200734 snd_usbmidi_input_start(p);
735unlock:
736 mutex_unlock(&us122l->mutex);
737 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
738 return err;
739}
740
741static struct usb_device_id snd_us122l_usb_id_table[] = {
742 {
743 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
744 .idVendor = 0x0644,
745 .idProduct = USB_ID_US122L
746 },
Tobias Hansen4f272342009-09-22 16:52:08 +0200747 { /* US-144 only works at USB1.1! Disable module ehci-hcd. */
748 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
749 .idVendor = 0x0644,
750 .idProduct = USB_ID_US144
751 },
Tobias Hansen2b6f6c02009-12-07 19:08:19 +0100752 {
753 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
754 .idVendor = 0x0644,
755 .idProduct = USB_ID_US122MKII
756 },
757 {
758 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
759 .idVendor = 0x0644,
760 .idProduct = USB_ID_US144MKII
761 },
Karsten Wiese030a07e2008-07-30 15:13:29 +0200762 { /* terminator */ }
763};
764
765MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table);
766static struct usb_driver snd_us122l_usb_driver = {
767 .name = "snd-usb-us122l",
768 .probe = snd_us122l_probe,
769 .disconnect = snd_us122l_disconnect,
770 .suspend = snd_us122l_suspend,
771 .resume = snd_us122l_resume,
772 .reset_resume = snd_us122l_resume,
773 .id_table = snd_us122l_usb_id_table,
774 .supports_autosuspend = 1
775};
776
Greg Kroah-Hartman424f0752011-11-18 09:50:44 -0800777module_usb_driver(snd_us122l_usb_driver);