aboutsummaryrefslogtreecommitdiffstats
path: root/sound
diff options
context:
space:
mode:
Diffstat (limited to 'sound')
-rw-r--r--sound/usb/Kconfig11
-rw-r--r--sound/usb/Makefile1
-rw-r--r--sound/usb/usbaudio.h1
-rw-r--r--sound/usb/usbmidi.c39
-rw-r--r--sound/usb/usx2y/Makefile2
-rw-r--r--sound/usb/usx2y/us122l.c692
-rw-r--r--sound/usb/usx2y/us122l.h27
-rw-r--r--sound/usb/usx2y/usb_stream.c761
-rw-r--r--sound/usb/usx2y/usb_stream.h112
9 files changed, 1646 insertions, 0 deletions
diff --git a/sound/usb/Kconfig b/sound/usb/Kconfig
index ffcdc8f4ef66..0463b342ec77 100644
--- a/sound/usb/Kconfig
+++ b/sound/usb/Kconfig
@@ -67,5 +67,16 @@ config SND_USB_CAIAQ_INPUT
67 * Native Instruments Kore Controller 2 67 * Native Instruments Kore Controller 2
68 * Native Instruments Audio Kontrol 1 68 * Native Instruments Audio Kontrol 1
69 69
70config SND_USB_US122L
71 tristate "Tascam US-122L USB driver"
72 depends on X86 && EXPERIMENTAL
73 select SND_RAWMIDI
74 help
75 Say Y here to include support for Tascam US-122L USB Audio/MIDI
76 interfaces.
77
78 To compile this driver as a module, choose M here: the module
79 will be called snd-usb-us122l.
80
70endif # SND_USB 81endif # SND_USB
71 82
diff --git a/sound/usb/Makefile b/sound/usb/Makefile
index aa252ef2ebfb..abb288bfe35d 100644
--- a/sound/usb/Makefile
+++ b/sound/usb/Makefile
@@ -8,5 +8,6 @@ snd-usb-lib-objs := usbmidi.o
8# Toplevel Module Dependency 8# Toplevel Module Dependency
9obj-$(CONFIG_SND_USB_AUDIO) += snd-usb-audio.o snd-usb-lib.o 9obj-$(CONFIG_SND_USB_AUDIO) += snd-usb-audio.o snd-usb-lib.o
10obj-$(CONFIG_SND_USB_USX2Y) += snd-usb-lib.o 10obj-$(CONFIG_SND_USB_USX2Y) += snd-usb-lib.o
11obj-$(CONFIG_SND_USB_US122L) += snd-usb-lib.o
11 12
12obj-$(CONFIG_SND) += usx2y/ caiaq/ 13obj-$(CONFIG_SND) += usx2y/ caiaq/
diff --git a/sound/usb/usbaudio.h b/sound/usb/usbaudio.h
index 7cf18c38dc42..140ba363414c 100644
--- a/sound/usb/usbaudio.h
+++ b/sound/usb/usbaudio.h
@@ -156,6 +156,7 @@ enum quirk_type {
156 QUIRK_MIDI_RAW, 156 QUIRK_MIDI_RAW,
157 QUIRK_MIDI_EMAGIC, 157 QUIRK_MIDI_EMAGIC,
158 QUIRK_MIDI_CME, 158 QUIRK_MIDI_CME,
159 QUIRK_MIDI_US122L,
159 QUIRK_AUDIO_STANDARD_INTERFACE, 160 QUIRK_AUDIO_STANDARD_INTERFACE,
160 QUIRK_AUDIO_FIXED_ENDPOINT, 161 QUIRK_AUDIO_FIXED_ENDPOINT,
161 QUIRK_AUDIO_EDIROL_UA700_UA25, 162 QUIRK_AUDIO_EDIROL_UA700_UA25,
diff --git a/sound/usb/usbmidi.c b/sound/usb/usbmidi.c
index 6676a177c99e..c0c7770198ad 100644
--- a/sound/usb/usbmidi.c
+++ b/sound/usb/usbmidi.c
@@ -669,6 +669,42 @@ static struct usb_protocol_ops snd_usbmidi_raw_ops = {
669 .output = snd_usbmidi_raw_output, 669 .output = snd_usbmidi_raw_output,
670}; 670};
671 671
672static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
673 uint8_t *buffer, int buffer_length)
674{
675 if (buffer_length != 9)
676 return;
677 buffer_length = 8;
678 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
679 buffer_length--;
680 if (buffer_length)
681 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
682}
683
684static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep)
685{
686 int count;
687
688 if (!ep->ports[0].active)
689 return;
690 count = ep->urb->dev->speed == USB_SPEED_HIGH ? 1 : 2;
691 count = snd_rawmidi_transmit(ep->ports[0].substream,
692 ep->urb->transfer_buffer,
693 count);
694 if (count < 1) {
695 ep->ports[0].active = 0;
696 return;
697 }
698
699 memset(ep->urb->transfer_buffer + count, 0xFD, 9 - count);
700 ep->urb->transfer_buffer_length = count;
701}
702
703static struct usb_protocol_ops snd_usbmidi_122l_ops = {
704 .input = snd_usbmidi_us122l_input,
705 .output = snd_usbmidi_us122l_output,
706};
707
672/* 708/*
673 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching. 709 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
674 */ 710 */
@@ -1714,6 +1750,9 @@ int snd_usb_create_midi_interface(struct snd_usb_audio* chip,
1714 umidi->usb_protocol_ops = 1750 umidi->usb_protocol_ops =
1715 &snd_usbmidi_maudio_broken_running_status_ops; 1751 &snd_usbmidi_maudio_broken_running_status_ops;
1716 break; 1752 break;
1753 case QUIRK_MIDI_US122L:
1754 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
1755 /* fall through */
1717 case QUIRK_MIDI_FIXED_ENDPOINT: 1756 case QUIRK_MIDI_FIXED_ENDPOINT:
1718 memcpy(&endpoints[0], quirk->data, 1757 memcpy(&endpoints[0], quirk->data,
1719 sizeof(struct snd_usb_midi_endpoint_info)); 1758 sizeof(struct snd_usb_midi_endpoint_info));
diff --git a/sound/usb/usx2y/Makefile b/sound/usb/usx2y/Makefile
index 9ac22bce1124..748933054b6c 100644
--- a/sound/usb/usx2y/Makefile
+++ b/sound/usb/usx2y/Makefile
@@ -1,3 +1,5 @@
1snd-usb-usx2y-objs := usbusx2y.o usX2Yhwdep.o usx2yhwdeppcm.o 1snd-usb-usx2y-objs := usbusx2y.o usX2Yhwdep.o usx2yhwdeppcm.o
2snd-usb-us122l-objs := us122l.o
2 3
3obj-$(CONFIG_SND_USB_USX2Y) += snd-usb-usx2y.o 4obj-$(CONFIG_SND_USB_USX2Y) += snd-usb-usx2y.o
5obj-$(CONFIG_SND_USB_US122L) += snd-usb-us122l.o
diff --git a/sound/usb/usx2y/us122l.c b/sound/usb/usx2y/us122l.c
new file mode 100644
index 000000000000..b441fe2cd190
--- /dev/null
+++ b/sound/usb/usx2y/us122l.c
@@ -0,0 +1,692 @@
1/*
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
19#include <sound/core.h>
20#include <sound/hwdep.h>
21#include <sound/pcm.h>
22#include <sound/initval.h>
23#define MODNAME "US122L"
24#include "usb_stream.c"
25#include "../usbaudio.h"
26#include "us122l.h"
27
28MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>");
29MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5");
30MODULE_LICENSE("GPL");
31
32static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
33static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */
34 /* Enable this card */
35static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
36
37module_param_array(index, int, NULL, 0444);
38MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS".");
39module_param_array(id, charp, NULL, 0444);
40MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS".");
41module_param_array(enable, bool, NULL, 0444);
42MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS".");
43
44static int snd_us122l_card_used[SNDRV_CARDS];
45
46
47static int us122l_create_usbmidi(struct snd_card *card)
48{
49 static struct snd_usb_midi_endpoint_info quirk_data = {
50 .out_ep = 4,
51 .in_ep = 3,
52 .out_cables = 0x001,
53 .in_cables = 0x001
54 };
55 static struct snd_usb_audio_quirk quirk = {
56 .vendor_name = "US122L",
57 .product_name = NAME_ALLCAPS,
58 .ifnum = 1,
59 .type = QUIRK_MIDI_US122L,
60 .data = &quirk_data
61 };
62 struct usb_device *dev = US122L(card)->chip.dev;
63 struct usb_interface *iface = usb_ifnum_to_if(dev, 1);
64
65 return snd_usb_create_midi_interface(&US122L(card)->chip,
66 iface, &quirk);
67}
68
69/*
70 * Wrapper for usb_control_msg().
71 * Allocates a temp buffer to prevent dmaing from/to the stack.
72 */
73static int us122l_ctl_msg(struct usb_device *dev, unsigned int pipe,
74 __u8 request, __u8 requesttype,
75 __u16 value, __u16 index, void *data,
76 __u16 size, int timeout)
77{
78 int err;
79 void *buf = NULL;
80
81 if (size > 0) {
82 buf = kmemdup(data, size, GFP_KERNEL);
83 if (!buf)
84 return -ENOMEM;
85 }
86 err = usb_control_msg(dev, pipe, request, requesttype,
87 value, index, buf, size, timeout);
88 if (size > 0) {
89 memcpy(data, buf, size);
90 kfree(buf);
91 }
92 return err;
93}
94
95static void pt_info_set(struct usb_device *dev, u8 v)
96{
97 int ret;
98
99 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
100 'I',
101 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
102 v, 0, NULL, 0, 1000);
103 snd_printdd(KERN_DEBUG "%i\n", ret);
104}
105
106static void usb_stream_hwdep_vm_open(struct vm_area_struct *area)
107{
108 struct us122l *us122l = area->vm_private_data;
109 atomic_inc(&us122l->mmap_count);
110 snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
111}
112
113static int usb_stream_hwdep_vm_fault(struct vm_area_struct *area,
114 struct vm_fault *vmf)
115{
116 unsigned long offset;
117 struct page *page;
118 void *vaddr;
119 struct us122l *us122l = area->vm_private_data;
120 struct usb_stream *s;
121 int vm_f = VM_FAULT_SIGBUS;
122
123 mutex_lock(&us122l->mutex);
124 s = us122l->sk.s;
125 if (!s)
126 goto out;
127
128 offset = vmf->pgoff << PAGE_SHIFT;
129 if (offset < PAGE_ALIGN(s->read_size))
130 vaddr = (char *)s + offset;
131 else {
132 offset -= PAGE_ALIGN(s->read_size);
133 if (offset >= PAGE_ALIGN(s->write_size))
134 goto out;
135
136 vaddr = us122l->sk.write_page + offset;
137 }
138 page = virt_to_page(vaddr);
139
140 get_page(page);
141 mutex_unlock(&us122l->mutex);
142
143 vmf->page = page;
144 vm_f = 0;
145out:
146 return vm_f;
147}
148
149static void usb_stream_hwdep_vm_close(struct vm_area_struct *area)
150{
151 struct us122l *us122l = area->vm_private_data;
152 atomic_dec(&us122l->mmap_count);
153 snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count));
154}
155
156static struct vm_operations_struct usb_stream_hwdep_vm_ops = {
157 .open = usb_stream_hwdep_vm_open,
158 .fault = usb_stream_hwdep_vm_fault,
159 .close = usb_stream_hwdep_vm_close,
160};
161
162
163static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file)
164{
165 struct us122l *us122l = hw->private_data;
166 struct usb_interface *iface;
167 snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
168 if (hw->used >= 2)
169 return -EBUSY;
170
171 if (!us122l->first)
172 us122l->first = file;
173 iface = usb_ifnum_to_if(us122l->chip.dev, 1);
174 usb_autopm_get_interface(iface);
175 return 0;
176}
177
178static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
179{
180 struct us122l *us122l = hw->private_data;
181 struct usb_interface *iface = usb_ifnum_to_if(us122l->chip.dev, 1);
182 snd_printdd(KERN_DEBUG "%p %p\n", hw, file);
183 usb_autopm_put_interface(iface);
184 if (us122l->first == file)
185 us122l->first = NULL;
186 mutex_lock(&us122l->mutex);
187 if (us122l->master == file)
188 us122l->master = us122l->slave;
189
190 us122l->slave = NULL;
191 mutex_unlock(&us122l->mutex);
192 return 0;
193}
194
195static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
196 struct file *filp, struct vm_area_struct *area)
197{
198 unsigned long size = area->vm_end - area->vm_start;
199 struct us122l *us122l = hw->private_data;
200 unsigned long offset;
201 struct usb_stream *s;
202 int err = 0;
203 bool read;
204
205 offset = area->vm_pgoff << PAGE_SHIFT;
206 mutex_lock(&us122l->mutex);
207 s = us122l->sk.s;
208 read = offset < s->read_size;
209 if (read && area->vm_flags & VM_WRITE) {
210 err = -EPERM;
211 goto out;
212 }
213 snd_printdd(KERN_DEBUG "%lu %u\n", size,
214 read ? s->read_size : s->write_size);
215 /* if userspace tries to mmap beyond end of our buffer, fail */
216 if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
217 snd_printk(KERN_WARNING "%lu > %u\n", size,
218 read ? s->read_size : s->write_size);
219 err = -EINVAL;
220 goto out;
221 }
222
223 area->vm_ops = &usb_stream_hwdep_vm_ops;
224 area->vm_flags |= VM_RESERVED;
225 area->vm_private_data = us122l;
226 atomic_inc(&us122l->mmap_count);
227out:
228 mutex_unlock(&us122l->mutex);
229 return err;
230}
231
232static unsigned int usb_stream_hwdep_poll(struct snd_hwdep *hw,
233 struct file *file, poll_table *wait)
234{
235 struct us122l *us122l = hw->private_data;
236 struct usb_stream *s = us122l->sk.s;
237 unsigned *polled;
238 unsigned int mask;
239
240 poll_wait(file, &us122l->sk.sleep, wait);
241
242 switch (s->state) {
243 case usb_stream_ready:
244 if (us122l->first == file)
245 polled = &s->periods_polled;
246 else
247 polled = &us122l->second_periods_polled;
248 if (*polled != s->periods_done) {
249 *polled = s->periods_done;
250 mask = POLLIN | POLLOUT | POLLWRNORM;
251 break;
252 }
253 /* Fall through */
254 mask = 0;
255 break;
256 default:
257 mask = POLLIN | POLLOUT | POLLWRNORM | POLLERR;
258 break;
259 }
260 return mask;
261}
262
263static void us122l_stop(struct us122l *us122l)
264{
265 struct list_head *p;
266 list_for_each(p, &us122l->chip.midi_list)
267 snd_usbmidi_input_stop(p);
268
269 usb_stream_stop(&us122l->sk);
270 usb_stream_free(&us122l->sk);
271}
272
273static int us122l_set_sample_rate(struct usb_device *dev, int rate)
274{
275 unsigned int ep = 0x81;
276 unsigned char data[3];
277 int err;
278
279 data[0] = rate;
280 data[1] = rate >> 8;
281 data[2] = rate >> 16;
282 err = us122l_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
283 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
284 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000);
285 if (err < 0)
286 snd_printk(KERN_ERR "%d: cannot set freq %d to ep 0x%x\n",
287 dev->devnum, rate, ep);
288 return err;
289}
290
291static bool us122l_start(struct us122l *us122l,
292 unsigned rate, unsigned period_frames)
293{
294 struct list_head *p;
295 int err;
296 unsigned use_packsize = 0;
297 bool success = false;
298
299 if (us122l->chip.dev->speed == USB_SPEED_HIGH) {
300 /* The us-122l's descriptor defaults to iso max_packsize 78,
301 which isn't needed for samplerates <= 48000.
302 Lets save some memory:
303 */
304 switch (rate) {
305 case 44100:
306 use_packsize = 36;
307 break;
308 case 48000:
309 use_packsize = 42;
310 break;
311 case 88200:
312 use_packsize = 72;
313 break;
314 }
315 }
316 if (!usb_stream_new(&us122l->sk, us122l->chip.dev, 1, 2,
317 rate, use_packsize, period_frames, 6))
318 goto out;
319
320 err = us122l_set_sample_rate(us122l->chip.dev, rate);
321 if (err < 0) {
322 us122l_stop(us122l);
323 snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
324 goto out;
325 }
326 err = usb_stream_start(&us122l->sk);
327 if (err < 0) {
328 us122l_stop(us122l);
329 snd_printk(KERN_ERR "us122l_start error %i \n", err);
330 goto out;
331 }
332 list_for_each(p, &us122l->chip.midi_list)
333 snd_usbmidi_input_start(p);
334 success = true;
335out:
336 return success;
337}
338
339static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
340 unsigned cmd, unsigned long arg)
341{
342 struct usb_stream_config *cfg;
343 struct us122l *us122l = hw->private_data;
344 unsigned min_period_frames;
345 int err = 0;
346 bool high_speed;
347
348 if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS)
349 return -ENOTTY;
350
351 cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
352 if (!cfg)
353 return -ENOMEM;
354
355 if (copy_from_user(cfg, (void *)arg, sizeof(*cfg))) {
356 err = -EFAULT;
357 goto free;
358 }
359 if (cfg->version != USB_STREAM_INTERFACE_VERSION) {
360 err = -ENXIO;
361 goto free;
362 }
363 high_speed = us122l->chip.dev->speed == USB_SPEED_HIGH;
364 if ((cfg->sample_rate != 44100 && cfg->sample_rate != 48000 &&
365 (!high_speed ||
366 (cfg->sample_rate != 88200 && cfg->sample_rate != 96000))) ||
367 cfg->frame_size != 6 ||
368 cfg->period_frames > 0x3000) {
369 err = -EINVAL;
370 goto free;
371 }
372 switch (cfg->sample_rate) {
373 case 44100:
374 min_period_frames = 48;
375 break;
376 case 48000:
377 min_period_frames = 52;
378 break;
379 default:
380 min_period_frames = 104;
381 break;
382 }
383 if (!high_speed)
384 min_period_frames <<= 1;
385 if (cfg->period_frames < min_period_frames) {
386 err = -EINVAL;
387 goto free;
388 }
389
390 snd_power_wait(hw->card, SNDRV_CTL_POWER_D0);
391
392 mutex_lock(&us122l->mutex);
393 if (!us122l->master)
394 us122l->master = file;
395 else if (us122l->master != file) {
396 if (memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg))) {
397 err = -EIO;
398 goto unlock;
399 }
400 us122l->slave = file;
401 }
402 if (!us122l->sk.s ||
403 memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg)) ||
404 us122l->sk.s->state == usb_stream_xrun) {
405 us122l_stop(us122l);
406 if (!us122l_start(us122l, cfg->sample_rate, cfg->period_frames))
407 err = -EIO;
408 else
409 err = 1;
410 }
411unlock:
412 mutex_unlock(&us122l->mutex);
413free:
414 kfree(cfg);
415 return err;
416}
417
418#define SND_USB_STREAM_ID "USB STREAM"
419static int usb_stream_hwdep_new(struct snd_card *card)
420{
421 int err;
422 struct snd_hwdep *hw;
423 struct usb_device *dev = US122L(card)->chip.dev;
424
425 err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw);
426 if (err < 0)
427 return err;
428
429 hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM;
430 hw->private_data = US122L(card);
431 hw->ops.open = usb_stream_hwdep_open;
432 hw->ops.release = usb_stream_hwdep_release;
433 hw->ops.ioctl = usb_stream_hwdep_ioctl;
434 hw->ops.ioctl_compat = usb_stream_hwdep_ioctl;
435 hw->ops.mmap = usb_stream_hwdep_mmap;
436 hw->ops.poll = usb_stream_hwdep_poll;
437
438 sprintf(hw->name, "/proc/bus/usb/%03d/%03d/hwdeppcm",
439 dev->bus->busnum, dev->devnum);
440 return 0;
441}
442
443
444static bool us122l_create_card(struct snd_card *card)
445{
446 int err;
447 struct us122l *us122l = US122L(card);
448
449 err = usb_set_interface(us122l->chip.dev, 1, 1);
450 if (err) {
451 snd_printk(KERN_ERR "usb_set_interface error \n");
452 return false;
453 }
454
455 pt_info_set(us122l->chip.dev, 0x11);
456 pt_info_set(us122l->chip.dev, 0x10);
457
458 if (!us122l_start(us122l, 44100, 256))
459 return false;
460
461 err = us122l_create_usbmidi(card);
462 if (err < 0) {
463 snd_printk(KERN_ERR "us122l_create_usbmidi error %i \n", err);
464 us122l_stop(us122l);
465 return false;
466 }
467 err = usb_stream_hwdep_new(card);
468 if (err < 0) {
469/* release the midi resources */
470 struct list_head *p;
471 list_for_each(p, &us122l->chip.midi_list)
472 snd_usbmidi_disconnect(p);
473
474 us122l_stop(us122l);
475 return false;
476 }
477 return true;
478}
479
480static struct snd_card *usx2y_create_card(struct usb_device *device)
481{
482 int dev;
483 struct snd_card *card;
484 for (dev = 0; dev < SNDRV_CARDS; ++dev)
485 if (enable[dev] && !snd_us122l_card_used[dev])
486 break;
487 if (dev >= SNDRV_CARDS)
488 return NULL;
489 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
490 sizeof(struct us122l));
491 if (!card)
492 return NULL;
493 snd_us122l_card_used[US122L(card)->chip.index = dev] = 1;
494
495 US122L(card)->chip.dev = device;
496 US122L(card)->chip.card = card;
497 mutex_init(&US122L(card)->mutex);
498 init_waitqueue_head(&US122L(card)->sk.sleep);
499 INIT_LIST_HEAD(&US122L(card)->chip.midi_list);
500 strcpy(card->driver, "USB "NAME_ALLCAPS"");
501 sprintf(card->shortname, "TASCAM "NAME_ALLCAPS"");
502 sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)",
503 card->shortname,
504 le16_to_cpu(device->descriptor.idVendor),
505 le16_to_cpu(device->descriptor.idProduct),
506 0,
507 US122L(card)->chip.dev->bus->busnum,
508 US122L(card)->chip.dev->devnum
509 );
510 snd_card_set_dev(card, &device->dev);
511 return card;
512}
513
514static void *us122l_usb_probe(struct usb_interface *intf,
515 const struct usb_device_id *device_id)
516{
517 struct usb_device *device = interface_to_usbdev(intf);
518 struct snd_card *card = usx2y_create_card(device);
519
520 if (!card)
521 return NULL;
522
523 if (!us122l_create_card(card) ||
524 snd_card_register(card) < 0) {
525 snd_card_free(card);
526 return NULL;
527 }
528
529 usb_get_dev(device);
530 return card;
531}
532
533static int snd_us122l_probe(struct usb_interface *intf,
534 const struct usb_device_id *id)
535{
536 struct snd_card *card;
537 snd_printdd(KERN_DEBUG"%p:%i\n",
538 intf, intf->cur_altsetting->desc.bInterfaceNumber);
539 if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
540 return 0;
541
542 card = us122l_usb_probe(usb_get_intf(intf), id);
543
544 if (card) {
545 usb_set_intfdata(intf, card);
546 return 0;
547 }
548
549 usb_put_intf(intf);
550 return -EIO;
551}
552
553static void snd_us122l_disconnect(struct usb_interface *intf)
554{
555 struct snd_card *card;
556 struct us122l *us122l;
557 struct list_head *p;
558
559 card = usb_get_intfdata(intf);
560 if (!card)
561 return;
562
563 snd_card_disconnect(card);
564
565 us122l = US122L(card);
566 mutex_lock(&us122l->mutex);
567 us122l_stop(us122l);
568 mutex_unlock(&us122l->mutex);
569 us122l->chip.shutdown = 1;
570
571/* release the midi resources */
572 list_for_each(p, &us122l->chip.midi_list) {
573 snd_usbmidi_disconnect(p);
574 }
575
576 usb_put_intf(intf);
577 usb_put_dev(US122L(card)->chip.dev);
578
579 while (atomic_read(&us122l->mmap_count))
580 msleep(500);
581
582 snd_card_free(card);
583}
584
585static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message)
586{
587 struct snd_card *card;
588 struct us122l *us122l;
589 struct list_head *p;
590
591 card = dev_get_drvdata(&intf->dev);
592 if (!card)
593 return 0;
594 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
595
596 us122l = US122L(card);
597 if (!us122l)
598 return 0;
599
600 list_for_each(p, &us122l->chip.midi_list)
601 snd_usbmidi_input_stop(p);
602
603 mutex_lock(&us122l->mutex);
604 usb_stream_stop(&us122l->sk);
605 mutex_unlock(&us122l->mutex);
606
607 return 0;
608}
609
610static int snd_us122l_resume(struct usb_interface *intf)
611{
612 struct snd_card *card;
613 struct us122l *us122l;
614 struct list_head *p;
615 int err;
616
617 card = dev_get_drvdata(&intf->dev);
618 if (!card)
619 return 0;
620
621 us122l = US122L(card);
622 if (!us122l)
623 return 0;
624
625 mutex_lock(&us122l->mutex);
626 /* needed, doesn't restart without: */
627 err = usb_set_interface(us122l->chip.dev, 1, 1);
628 if (err) {
629 snd_printk(KERN_ERR "usb_set_interface error \n");
630 goto unlock;
631 }
632
633 pt_info_set(us122l->chip.dev, 0x11);
634 pt_info_set(us122l->chip.dev, 0x10);
635
636 err = us122l_set_sample_rate(us122l->chip.dev,
637 us122l->sk.s->cfg.sample_rate);
638 if (err < 0) {
639 snd_printk(KERN_ERR "us122l_set_sample_rate error \n");
640 goto unlock;
641 }
642 err = usb_stream_start(&us122l->sk);
643 if (err)
644 goto unlock;
645
646 list_for_each(p, &us122l->chip.midi_list)
647 snd_usbmidi_input_start(p);
648unlock:
649 mutex_unlock(&us122l->mutex);
650 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
651 return err;
652}
653
654static struct usb_device_id snd_us122l_usb_id_table[] = {
655 {
656 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
657 .idVendor = 0x0644,
658 .idProduct = USB_ID_US122L
659 },
660/* { */ /* US-144 maybe works when @USB1.1. Untested. */
661/* .match_flags = USB_DEVICE_ID_MATCH_DEVICE, */
662/* .idVendor = 0x0644, */
663/* .idProduct = USB_ID_US144 */
664/* }, */
665 { /* terminator */ }
666};
667
668MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table);
669static struct usb_driver snd_us122l_usb_driver = {
670 .name = "snd-usb-us122l",
671 .probe = snd_us122l_probe,
672 .disconnect = snd_us122l_disconnect,
673 .suspend = snd_us122l_suspend,
674 .resume = snd_us122l_resume,
675 .reset_resume = snd_us122l_resume,
676 .id_table = snd_us122l_usb_id_table,
677 .supports_autosuspend = 1
678};
679
680
681static int __init snd_us122l_module_init(void)
682{
683 return usb_register(&snd_us122l_usb_driver);
684}
685
686static void __exit snd_us122l_module_exit(void)
687{
688 usb_deregister(&snd_us122l_usb_driver);
689}
690
691module_init(snd_us122l_module_init)
692module_exit(snd_us122l_module_exit)
diff --git a/sound/usb/usx2y/us122l.h b/sound/usb/usx2y/us122l.h
new file mode 100644
index 000000000000..3d10c4b2a0f5
--- /dev/null
+++ b/sound/usb/usx2y/us122l.h
@@ -0,0 +1,27 @@
1#ifndef US122L_H
2#define US122L_H
3
4
5struct us122l {
6 struct snd_usb_audio chip;
7 int stride;
8 struct usb_stream_kernel sk;
9
10 struct mutex mutex;
11 struct file *first;
12 unsigned second_periods_polled;
13 struct file *master;
14 struct file *slave;
15
16 atomic_t mmap_count;
17};
18
19
20#define US122L(c) ((struct us122l *)(c)->private_data)
21
22#define NAME_ALLCAPS "US-122L"
23
24#define USB_ID_US122L 0x800E
25#define USB_ID_US144 0x800F
26
27#endif
diff --git a/sound/usb/usx2y/usb_stream.c b/sound/usb/usx2y/usb_stream.c
new file mode 100644
index 000000000000..ff23cc1ce3b9
--- /dev/null
+++ b/sound/usb/usx2y/usb_stream.c
@@ -0,0 +1,761 @@
1/*
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
19#include <linux/usb.h>
20
21#include "usb_stream.h"
22
23
24/* setup */
25
26static unsigned usb_stream_next_packet_size(struct usb_stream_kernel *sk)
27{
28 struct usb_stream *s = sk->s;
29 sk->out_phase_peeked = (sk->out_phase & 0xffff) + sk->freqn;
30 return (sk->out_phase_peeked >> 16) * s->cfg.frame_size;
31}
32
33static void playback_prep_freqn(struct usb_stream_kernel *sk, struct urb *urb)
34{
35 struct usb_stream *s = sk->s;
36 unsigned l = 0;
37 int pack;
38
39 urb->iso_frame_desc[0].offset = 0;
40 urb->iso_frame_desc[0].length = usb_stream_next_packet_size(sk);
41 sk->out_phase = sk->out_phase_peeked;
42 urb->transfer_buffer_length = urb->iso_frame_desc[0].length;
43
44 for (pack = 1; pack < sk->n_o_ps; pack++) {
45 l = usb_stream_next_packet_size(sk);
46 if (s->idle_outsize + urb->transfer_buffer_length + l >
47 s->period_size)
48 goto check;
49
50 sk->out_phase = sk->out_phase_peeked;
51 urb->iso_frame_desc[pack].offset = urb->transfer_buffer_length;
52 urb->iso_frame_desc[pack].length = l;
53 urb->transfer_buffer_length += l;
54 }
55 snd_printdd(KERN_DEBUG "%i\n", urb->transfer_buffer_length);
56
57check:
58 urb->number_of_packets = pack;
59 s->idle_outsize += urb->transfer_buffer_length - s->period_size;
60 snd_printdd(KERN_DEBUG "idle=%i ul=%i ps=%i\n", s->idle_outsize,
61 urb->transfer_buffer_length, s->period_size);
62}
63
64static void init_pipe_urbs(struct usb_stream_kernel *sk, unsigned use_packsize,
65 struct urb **urbs, char *transfer,
66 struct usb_device *dev, int pipe)
67{
68 int u, p;
69 int maxpacket = use_packsize ?
70 use_packsize : usb_maxpacket(dev, pipe, usb_pipeout(pipe));
71 int transfer_length = maxpacket * sk->n_o_ps;
72
73 for (u = 0; u < USB_STREAM_NURBS;
74 ++u, transfer += transfer_length) {
75 struct urb *urb = urbs[u];
76 struct usb_iso_packet_descriptor *desc;
77 urb->transfer_flags = URB_ISO_ASAP;
78 urb->transfer_buffer = transfer;
79 urb->dev = dev;
80 urb->pipe = pipe;
81 urb->number_of_packets = sk->n_o_ps;
82 urb->context = sk;
83 urb->interval = 1;
84 if (usb_pipeout(pipe))
85 continue;
86
87 urb->transfer_buffer_length = transfer_length;
88 desc = urb->iso_frame_desc;
89 desc->offset = 0;
90 desc->length = maxpacket;
91 for (p = 1; p < sk->n_o_ps; ++p) {
92 desc[p].offset = desc[p - 1].offset + maxpacket;
93 desc[p].length = maxpacket;
94 }
95 }
96}
97
98static void init_urbs(struct usb_stream_kernel *sk, unsigned use_packsize,
99 struct usb_device *dev, int in_pipe, int out_pipe)
100{
101 struct usb_stream *s = sk->s;
102 char *indata = (char *)s + sizeof(*s) +
103 sizeof(struct usb_stream_packet) *
104 s->inpackets;
105 int u;
106
107 for (u = 0; u < USB_STREAM_NURBS; ++u) {
108 sk->inurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
109 sk->outurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
110 }
111
112 init_pipe_urbs(sk, use_packsize, sk->inurb, indata, dev, in_pipe);
113 init_pipe_urbs(sk, use_packsize, sk->outurb, sk->write_page, dev,
114 out_pipe);
115}
116
117
118/*
119 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
120 * this will overflow at approx 524 kHz
121 */
122static inline unsigned get_usb_full_speed_rate(unsigned rate)
123{
124 return ((rate << 13) + 62) / 125;
125}
126
127/*
128 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
129 * this will overflow at approx 4 MHz
130 */
131static inline unsigned get_usb_high_speed_rate(unsigned rate)
132{
133 return ((rate << 10) + 62) / 125;
134}
135
136void usb_stream_free(struct usb_stream_kernel *sk)
137{
138 struct usb_stream *s;
139 unsigned u;
140
141 for (u = 0; u < USB_STREAM_NURBS; ++u) {
142 usb_free_urb(sk->inurb[u]);
143 sk->inurb[u] = NULL;
144 usb_free_urb(sk->outurb[u]);
145 sk->outurb[u] = NULL;
146 }
147
148 s = sk->s;
149 if (!s)
150 return;
151
152 free_pages((unsigned long)sk->write_page, get_order(s->write_size));
153 sk->write_page = NULL;
154 free_pages((unsigned long)s, get_order(s->read_size));
155 sk->s = NULL;
156}
157
158struct usb_stream *usb_stream_new(struct usb_stream_kernel *sk,
159 struct usb_device *dev,
160 unsigned in_endpoint, unsigned out_endpoint,
161 unsigned sample_rate, unsigned use_packsize,
162 unsigned period_frames, unsigned frame_size)
163{
164 int packets, max_packsize;
165 int in_pipe, out_pipe;
166 int read_size = sizeof(struct usb_stream);
167 int write_size;
168 int usb_frames = dev->speed == USB_SPEED_HIGH ? 8000 : 1000;
169 int pg;
170
171 in_pipe = usb_rcvisocpipe(dev, in_endpoint);
172 out_pipe = usb_sndisocpipe(dev, out_endpoint);
173
174 max_packsize = use_packsize ?
175 use_packsize : usb_maxpacket(dev, in_pipe, 0);
176
177 /*
178 t_period = period_frames / sample_rate
179 iso_packs = t_period / t_iso_frame
180 = (period_frames / sample_rate) * (1 / t_iso_frame)
181 */
182
183 packets = period_frames * usb_frames / sample_rate + 1;
184
185 if (dev->speed == USB_SPEED_HIGH)
186 packets = (packets + 7) & ~7;
187
188 read_size += packets * USB_STREAM_URBDEPTH *
189 (max_packsize + sizeof(struct usb_stream_packet));
190
191 max_packsize = usb_maxpacket(dev, out_pipe, 1);
192 write_size = max_packsize * packets * USB_STREAM_URBDEPTH;
193
194 if (read_size >= 256*PAGE_SIZE || write_size >= 256*PAGE_SIZE) {
195 snd_printk(KERN_WARNING "a size exceeds 128*PAGE_SIZE\n");
196 goto out;
197 }
198
199 pg = get_order(read_size);
200 sk->s = (void *) __get_free_pages(GFP_KERNEL|__GFP_COMP|__GFP_ZERO, pg);
201 if (!sk->s) {
202 snd_printk(KERN_WARNING "couldn't __get_free_pages()\n");
203 goto out;
204 }
205 sk->s->cfg.version = USB_STREAM_INTERFACE_VERSION;
206
207 sk->s->read_size = read_size;
208
209 sk->s->cfg.sample_rate = sample_rate;
210 sk->s->cfg.frame_size = frame_size;
211 sk->n_o_ps = packets;
212 sk->s->inpackets = packets * USB_STREAM_URBDEPTH;
213 sk->s->cfg.period_frames = period_frames;
214 sk->s->period_size = frame_size * period_frames;
215
216 sk->s->write_size = write_size;
217 pg = get_order(write_size);
218
219 sk->write_page =
220 (void *)__get_free_pages(GFP_KERNEL|__GFP_COMP|__GFP_ZERO, pg);
221 if (!sk->write_page) {
222 snd_printk(KERN_WARNING "couldn't __get_free_pages()\n");
223 usb_stream_free(sk);
224 return NULL;
225 }
226
227 /* calculate the frequency in 16.16 format */
228 if (dev->speed == USB_SPEED_FULL)
229 sk->freqn = get_usb_full_speed_rate(sample_rate);
230 else
231 sk->freqn = get_usb_high_speed_rate(sample_rate);
232
233 init_urbs(sk, use_packsize, dev, in_pipe, out_pipe);
234 sk->s->state = usb_stream_stopped;
235out:
236 return sk->s;
237}
238
239
240/* start */
241
242static bool balance_check(struct usb_stream_kernel *sk, struct urb *urb)
243{
244 bool r;
245 if (unlikely(urb->status)) {
246 if (urb->status != -ESHUTDOWN && urb->status != -ENOENT)
247 snd_printk(KERN_WARNING "status=%i\n", urb->status);
248 sk->iso_frame_balance = 0x7FFFFFFF;
249 return false;
250 }
251 r = sk->iso_frame_balance == 0;
252 if (!r)
253 sk->i_urb = urb;
254 return r;
255}
256
257static bool balance_playback(struct usb_stream_kernel *sk, struct urb *urb)
258{
259 sk->iso_frame_balance += urb->number_of_packets;
260 return balance_check(sk, urb);
261}
262
263static bool balance_capture(struct usb_stream_kernel *sk, struct urb *urb)
264{
265 sk->iso_frame_balance -= urb->number_of_packets;
266 return balance_check(sk, urb);
267}
268
269static void subs_set_complete(struct urb **urbs, void (*complete)(struct urb *))
270{
271 int u;
272
273 for (u = 0; u < USB_STREAM_NURBS; u++) {
274 struct urb *urb = urbs[u];
275 urb->complete = complete;
276 }
277}
278
279int usb_stream_prepare_playback(struct usb_stream_kernel *sk, struct urb *inurb)
280{
281 struct usb_stream *s = sk->s;
282 struct urb *io;
283 struct usb_iso_packet_descriptor *id, *od;
284 int p, l = 0;
285
286 io = sk->idle_outurb;
287 od = io->iso_frame_desc;
288 io->transfer_buffer_length = 0;
289
290 for (p = 0; s->sync_packet < 0; ++p, ++s->sync_packet) {
291 struct urb *ii = sk->completed_inurb;
292 id = ii->iso_frame_desc +
293 ii->number_of_packets + s->sync_packet;
294 l = id->actual_length;
295
296 od[p].length = l;
297 od[p].offset = io->transfer_buffer_length;
298 io->transfer_buffer_length += l;
299 }
300
301 for (;
302 s->sync_packet < inurb->number_of_packets && p < sk->n_o_ps;
303 ++p, ++s->sync_packet) {
304 l = inurb->iso_frame_desc[s->sync_packet].actual_length;
305
306 if (s->idle_outsize + io->transfer_buffer_length + l >
307 s->period_size)
308 goto check_ok;
309
310 od[p].length = l;
311 od[p].offset = io->transfer_buffer_length;
312 io->transfer_buffer_length += l;
313 }
314
315check_ok:
316 s->sync_packet -= inurb->number_of_packets;
317 if (s->sync_packet < -2 || s->sync_packet > 0) {
318 snd_printk(KERN_WARNING "invalid sync_packet = %i;"
319 " p=%i nop=%i %i %x %x %x > %x\n",
320 s->sync_packet, p, inurb->number_of_packets,
321 s->idle_outsize + io->transfer_buffer_length + l,
322 s->idle_outsize, io->transfer_buffer_length, l,
323 s->period_size);
324 return -1;
325 }
326 if (io->transfer_buffer_length % s->cfg.frame_size) {
327 snd_printk(KERN_WARNING"invalid outsize = %i\n",
328 io->transfer_buffer_length);
329 return -1;
330 }
331 s->idle_outsize += io->transfer_buffer_length - s->period_size;
332 io->number_of_packets = p;
333 if (s->idle_outsize > 0) {
334 snd_printk(KERN_WARNING "idle=%i\n", s->idle_outsize);
335 return -1;
336 }
337 return 0;
338}
339
340static void prepare_inurb(int number_of_packets, struct urb *iu)
341{
342 struct usb_iso_packet_descriptor *id;
343 int p;
344
345 iu->number_of_packets = number_of_packets;
346 id = iu->iso_frame_desc;
347 id->offset = 0;
348 for (p = 0; p < iu->number_of_packets - 1; ++p)
349 id[p + 1].offset = id[p].offset + id[p].length;
350
351 iu->transfer_buffer_length =
352 id[0].length * iu->number_of_packets;
353}
354
355static int submit_urbs(struct usb_stream_kernel *sk,
356 struct urb *inurb, struct urb *outurb)
357{
358 int err;
359 prepare_inurb(sk->idle_outurb->number_of_packets, sk->idle_inurb);
360 err = usb_submit_urb(sk->idle_inurb, GFP_ATOMIC);
361 if (err < 0) {
362 snd_printk(KERN_ERR "%i\n", err);
363 return err;
364 }
365 sk->idle_inurb = sk->completed_inurb;
366 sk->completed_inurb = inurb;
367 err = usb_submit_urb(sk->idle_outurb, GFP_ATOMIC);
368 if (err < 0) {
369 snd_printk(KERN_ERR "%i\n", err);
370 return err;
371 }
372 sk->idle_outurb = sk->completed_outurb;
373 sk->completed_outurb = outurb;
374 return 0;
375}
376
377#ifdef DEBUG_LOOP_BACK
378/*
379 This loop_back() shows how to read/write the period data.
380 */
381static void loop_back(struct usb_stream *s)
382{
383 char *i, *o;
384 int il, ol, l, p;
385 struct urb *iu;
386 struct usb_iso_packet_descriptor *id;
387
388 o = s->playback1st_to;
389 ol = s->playback1st_size;
390 l = 0;
391
392 if (s->insplit_pack >= 0) {
393 iu = sk->idle_inurb;
394 id = iu->iso_frame_desc;
395 p = s->insplit_pack;
396 } else
397 goto second;
398loop:
399 for (; p < iu->number_of_packets && l < s->period_size; ++p) {
400 i = iu->transfer_buffer + id[p].offset;
401 il = id[p].actual_length;
402 if (l + il > s->period_size)
403 il = s->period_size - l;
404 if (il <= ol) {
405 memcpy(o, i, il);
406 o += il;
407 ol -= il;
408 } else {
409 memcpy(o, i, ol);
410 singen_6pack(o, ol);
411 o = s->playback_to;
412 memcpy(o, i + ol, il - ol);
413 o += il - ol;
414 ol = s->period_size - s->playback1st_size;
415 }
416 l += il;
417 }
418 if (iu == sk->completed_inurb) {
419 if (l != s->period_size)
420 printk(KERN_DEBUG"%s:%i %i\n", __func__, __LINE__,
421 l/(int)s->cfg.frame_size);
422
423 return;
424 }
425second:
426 iu = sk->completed_inurb;
427 id = iu->iso_frame_desc;
428 p = 0;
429 goto loop;
430
431}
432#else
433static void loop_back(struct usb_stream *s)
434{
435}
436#endif
437
438static void stream_idle(struct usb_stream_kernel *sk,
439 struct urb *inurb, struct urb *outurb)
440{
441 struct usb_stream *s = sk->s;
442 int l, p;
443 int insize = s->idle_insize;
444 int urb_size = 0;
445
446 s->inpacket_split = s->next_inpacket_split;
447 s->inpacket_split_at = s->next_inpacket_split_at;
448 s->next_inpacket_split = -1;
449 s->next_inpacket_split_at = 0;
450
451 for (p = 0; p < inurb->number_of_packets; ++p) {
452 struct usb_iso_packet_descriptor *id = inurb->iso_frame_desc;
453 l = id[p].actual_length;
454 if (unlikely(l == 0 || id[p].status)) {
455 snd_printk(KERN_WARNING "underrun, status=%u\n",
456 id[p].status);
457 goto err_out;
458 }
459 s->inpacket_head++;
460 s->inpacket_head %= s->inpackets;
461 if (s->inpacket_split == -1)
462 s->inpacket_split = s->inpacket_head;
463
464 s->inpacket[s->inpacket_head].offset =
465 id[p].offset + (inurb->transfer_buffer - (void *)s);
466 s->inpacket[s->inpacket_head].length = l;
467 if (insize + l > s->period_size &&
468 s->next_inpacket_split == -1) {
469 s->next_inpacket_split = s->inpacket_head;
470 s->next_inpacket_split_at = s->period_size - insize;
471 }
472 insize += l;
473 urb_size += l;
474 }
475 s->idle_insize += urb_size - s->period_size;
476 if (s->idle_insize < 0) {
477 snd_printk(KERN_WARNING "%i\n",
478 (s->idle_insize)/(int)s->cfg.frame_size);
479 goto err_out;
480 }
481 s->insize_done += urb_size;
482
483 l = s->idle_outsize;
484 s->outpacket[0].offset = (sk->idle_outurb->transfer_buffer -
485 sk->write_page) - l;
486
487 if (usb_stream_prepare_playback(sk, inurb) < 0)
488 goto err_out;
489
490 s->outpacket[0].length = sk->idle_outurb->transfer_buffer_length + l;
491 s->outpacket[1].offset = sk->completed_outurb->transfer_buffer -
492 sk->write_page;
493
494 if (submit_urbs(sk, inurb, outurb) < 0)
495 goto err_out;
496
497 loop_back(s);
498 s->periods_done++;
499 wake_up_all(&sk->sleep);
500 return;
501err_out:
502 s->state = usb_stream_xrun;
503 wake_up_all(&sk->sleep);
504}
505
506static void i_capture_idle(struct urb *urb)
507{
508 struct usb_stream_kernel *sk = urb->context;
509 if (balance_capture(sk, urb))
510 stream_idle(sk, urb, sk->i_urb);
511}
512
513static void i_playback_idle(struct urb *urb)
514{
515 struct usb_stream_kernel *sk = urb->context;
516 if (balance_playback(sk, urb))
517 stream_idle(sk, sk->i_urb, urb);
518}
519
520static void stream_start(struct usb_stream_kernel *sk,
521 struct urb *inurb, struct urb *outurb)
522{
523 struct usb_stream *s = sk->s;
524 if (s->state >= usb_stream_sync1) {
525 int l, p, max_diff, max_diff_0;
526 int urb_size = 0;
527 unsigned frames_per_packet, min_frames = 0;
528 frames_per_packet = (s->period_size - s->idle_insize);
529 frames_per_packet <<= 8;
530 frames_per_packet /=
531 s->cfg.frame_size * inurb->number_of_packets;
532 frames_per_packet++;
533
534 max_diff_0 = s->cfg.frame_size;
535 if (s->cfg.period_frames >= 256)
536 max_diff_0 <<= 1;
537 if (s->cfg.period_frames >= 1024)
538 max_diff_0 <<= 1;
539 max_diff = max_diff_0;
540 for (p = 0; p < inurb->number_of_packets; ++p) {
541 int diff;
542 l = inurb->iso_frame_desc[p].actual_length;
543 urb_size += l;
544
545 min_frames += frames_per_packet;
546 diff = urb_size -
547 (min_frames >> 8) * s->cfg.frame_size;
548 if (diff < max_diff) {
549 snd_printdd(KERN_DEBUG "%i %i %i %i\n",
550 s->insize_done,
551 urb_size / (int)s->cfg.frame_size,
552 inurb->number_of_packets, diff);
553 max_diff = diff;
554 }
555 }
556 s->idle_insize -= max_diff - max_diff_0;
557 s->idle_insize += urb_size - s->period_size;
558 if (s->idle_insize < 0) {
559 snd_printk("%i %i %i\n",
560 s->idle_insize, urb_size, s->period_size);
561 return;
562 } else if (s->idle_insize == 0) {
563 s->next_inpacket_split =
564 (s->inpacket_head + 1) % s->inpackets;
565 s->next_inpacket_split_at = 0;
566 } else {
567 unsigned split = s->inpacket_head;
568 l = s->idle_insize;
569 while (l > s->inpacket[split].length) {
570 l -= s->inpacket[split].length;
571 if (split == 0)
572 split = s->inpackets - 1;
573 else
574 split--;
575 }
576 s->next_inpacket_split = split;
577 s->next_inpacket_split_at =
578 s->inpacket[split].length - l;
579 }
580
581 s->insize_done += urb_size;
582
583 if (usb_stream_prepare_playback(sk, inurb) < 0)
584 return;
585
586 } else
587 playback_prep_freqn(sk, sk->idle_outurb);
588
589 if (submit_urbs(sk, inurb, outurb) < 0)
590 return;
591
592 if (s->state == usb_stream_sync1 && s->insize_done > 360000) {
593 /* just guesswork ^^^^^^ */
594 s->state = usb_stream_ready;
595 subs_set_complete(sk->inurb, i_capture_idle);
596 subs_set_complete(sk->outurb, i_playback_idle);
597 }
598}
599
600static void i_capture_start(struct urb *urb)
601{
602 struct usb_iso_packet_descriptor *id = urb->iso_frame_desc;
603 struct usb_stream_kernel *sk = urb->context;
604 struct usb_stream *s = sk->s;
605 int p;
606 int empty = 0;
607
608 if (urb->status) {
609 snd_printk(KERN_WARNING "status=%i\n", urb->status);
610 return;
611 }
612
613 for (p = 0; p < urb->number_of_packets; ++p) {
614 int l = id[p].actual_length;
615 if (l < s->cfg.frame_size) {
616 ++empty;
617 if (s->state >= usb_stream_sync0) {
618 snd_printk(KERN_WARNING "%i\n", l);
619 return;
620 }
621 }
622 s->inpacket_head++;
623 s->inpacket_head %= s->inpackets;
624 s->inpacket[s->inpacket_head].offset =
625 id[p].offset + (urb->transfer_buffer - (void *)s);
626 s->inpacket[s->inpacket_head].length = l;
627 }
628#ifdef SHOW_EMPTY
629 if (empty) {
630 printk(KERN_DEBUG"%s:%i: %i", __func__, __LINE__,
631 urb->iso_frame_desc[0].actual_length);
632 for (pack = 1; pack < urb->number_of_packets; ++pack) {
633 int l = urb->iso_frame_desc[pack].actual_length;
634 printk(" %i", l);
635 }
636 printk("\n");
637 }
638#endif
639 if (!empty && s->state < usb_stream_sync1)
640 ++s->state;
641
642 if (balance_capture(sk, urb))
643 stream_start(sk, urb, sk->i_urb);
644}
645
646static void i_playback_start(struct urb *urb)
647{
648 struct usb_stream_kernel *sk = urb->context;
649 if (balance_playback(sk, urb))
650 stream_start(sk, sk->i_urb, urb);
651}
652
653int usb_stream_start(struct usb_stream_kernel *sk)
654{
655 struct usb_stream *s = sk->s;
656 int frame = 0, iters = 0;
657 int u, err;
658 int try = 0;
659
660 if (s->state != usb_stream_stopped)
661 return -EAGAIN;
662
663 subs_set_complete(sk->inurb, i_capture_start);
664 subs_set_complete(sk->outurb, i_playback_start);
665 memset(sk->write_page, 0, s->write_size);
666dotry:
667 s->insize_done = 0;
668 s->idle_insize = 0;
669 s->idle_outsize = 0;
670 s->sync_packet = -1;
671 s->inpacket_head = -1;
672 sk->iso_frame_balance = 0;
673 ++try;
674 for (u = 0; u < 2; u++) {
675 struct urb *inurb = sk->inurb[u];
676 struct urb *outurb = sk->outurb[u];
677 playback_prep_freqn(sk, outurb);
678 inurb->number_of_packets = outurb->number_of_packets;
679 inurb->transfer_buffer_length =
680 inurb->number_of_packets *
681 inurb->iso_frame_desc[0].length;
682 preempt_disable();
683 if (u == 0) {
684 int now;
685 struct usb_device *dev = inurb->dev;
686 frame = usb_get_current_frame_number(dev);
687 do {
688 now = usb_get_current_frame_number(dev);
689 ++iters;
690 } while (now > -1 && now == frame);
691 }
692 err = usb_submit_urb(inurb, GFP_ATOMIC);
693 if (err < 0) {
694 preempt_enable();
695 snd_printk(KERN_ERR"usb_submit_urb(sk->inurb[%i])"
696 " returned %i\n", u, err);
697 return err;
698 }
699 err = usb_submit_urb(outurb, GFP_ATOMIC);
700 if (err < 0) {
701 preempt_enable();
702 snd_printk(KERN_ERR"usb_submit_urb(sk->outurb[%i])"
703 " returned %i\n", u, err);
704 return err;
705 }
706 preempt_enable();
707 if (inurb->start_frame != outurb->start_frame) {
708 snd_printd(KERN_DEBUG
709 "u[%i] start_frames differ in:%u out:%u\n",
710 u, inurb->start_frame, outurb->start_frame);
711 goto check_retry;
712 }
713 }
714 snd_printdd(KERN_DEBUG "%i %i\n", frame, iters);
715 try = 0;
716check_retry:
717 if (try) {
718 usb_stream_stop(sk);
719 if (try < 5) {
720 msleep(1500);
721 snd_printd(KERN_DEBUG "goto dotry;\n");
722 goto dotry;
723 }
724 snd_printk(KERN_WARNING"couldn't start"
725 " all urbs on the same start_frame.\n");
726 return -EFAULT;
727 }
728
729 sk->idle_inurb = sk->inurb[USB_STREAM_NURBS - 2];
730 sk->idle_outurb = sk->outurb[USB_STREAM_NURBS - 2];
731 sk->completed_inurb = sk->inurb[USB_STREAM_NURBS - 1];
732 sk->completed_outurb = sk->outurb[USB_STREAM_NURBS - 1];
733
734/* wait, check */
735 {
736 int wait_ms = 3000;
737 while (s->state != usb_stream_ready && wait_ms > 0) {
738 snd_printdd(KERN_DEBUG "%i\n", s->state);
739 msleep(200);
740 wait_ms -= 200;
741 }
742 }
743
744 return s->state == usb_stream_ready ? 0 : -EFAULT;
745}
746
747
748/* stop */
749
750void usb_stream_stop(struct usb_stream_kernel *sk)
751{
752 int u;
753 if (!sk->s)
754 return;
755 for (u = 0; u < USB_STREAM_NURBS; ++u) {
756 usb_kill_urb(sk->inurb[u]);
757 usb_kill_urb(sk->outurb[u]);
758 }
759 sk->s->state = usb_stream_stopped;
760 msleep(400);
761}
diff --git a/sound/usb/usx2y/usb_stream.h b/sound/usb/usx2y/usb_stream.h
new file mode 100644
index 000000000000..4dd74ab1e9cc
--- /dev/null
+++ b/sound/usb/usx2y/usb_stream.h
@@ -0,0 +1,112 @@
1/*
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
19#define USB_STREAM_INTERFACE_VERSION 2
20
21#define SNDRV_USB_STREAM_IOCTL_SET_PARAMS \
22 _IOW('H', 0x90, struct usb_stream_config)
23
24struct usb_stream_packet {
25 unsigned offset;
26 unsigned length;
27};
28
29
30struct usb_stream_config {
31 unsigned version;
32 unsigned sample_rate;
33 unsigned period_frames;
34 unsigned frame_size;
35};
36
37struct usb_stream {
38 struct usb_stream_config cfg;
39 unsigned read_size;
40 unsigned write_size;
41
42 int period_size;
43
44 unsigned state;
45
46 int idle_insize;
47 int idle_outsize;
48 int sync_packet;
49 unsigned insize_done;
50 unsigned periods_done;
51 unsigned periods_polled;
52
53 struct usb_stream_packet outpacket[2];
54 unsigned inpackets;
55 unsigned inpacket_head;
56 unsigned inpacket_split;
57 unsigned inpacket_split_at;
58 unsigned next_inpacket_split;
59 unsigned next_inpacket_split_at;
60 struct usb_stream_packet inpacket[0];
61};
62
63enum usb_stream_state {
64 usb_stream_invalid,
65 usb_stream_stopped,
66 usb_stream_sync0,
67 usb_stream_sync1,
68 usb_stream_ready,
69 usb_stream_running,
70 usb_stream_xrun,
71};
72
73#if __KERNEL__
74
75#define USB_STREAM_NURBS 4
76#define USB_STREAM_URBDEPTH 4
77
78struct usb_stream_kernel {
79 struct usb_stream *s;
80
81 void *write_page;
82
83 unsigned n_o_ps;
84
85 struct urb *inurb[USB_STREAM_NURBS];
86 struct urb *idle_inurb;
87 struct urb *completed_inurb;
88 struct urb *outurb[USB_STREAM_NURBS];
89 struct urb *idle_outurb;
90 struct urb *completed_outurb;
91 struct urb *i_urb;
92
93 int iso_frame_balance;
94
95 wait_queue_head_t sleep;
96
97 unsigned out_phase;
98 unsigned out_phase_peeked;
99 unsigned freqn;
100};
101
102struct usb_stream *usb_stream_new(struct usb_stream_kernel *sk,
103 struct usb_device *dev,
104 unsigned in_endpoint, unsigned out_endpoint,
105 unsigned sample_rate, unsigned use_packsize,
106 unsigned period_frames, unsigned frame_size);
107void usb_stream_free(struct usb_stream_kernel *);
108int usb_stream_start(struct usb_stream_kernel *);
109void usb_stream_stop(struct usb_stream_kernel *);
110
111
112#endif