]>
Commit | Line | Data |
---|---|---|
030a07e4 KW |
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 | ||
de48c7bc DM |
19 | #include <linux/usb.h> |
20 | #include <linux/usb/audio.h> | |
030a07e4 KW |
21 | #include <sound/core.h> |
22 | #include <sound/hwdep.h> | |
23 | #include <sound/pcm.h> | |
24 | #include <sound/initval.h> | |
25 | #define MODNAME "US122L" | |
26 | #include "usb_stream.c" | |
27 | #include "../usbaudio.h" | |
28 | #include "us122l.h" | |
29 | ||
30 | MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>"); | |
31 | MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5"); | |
32 | MODULE_LICENSE("GPL"); | |
33 | ||
34 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */ | |
35 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */ | |
36 | /* Enable this card */ | |
37 | static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; | |
38 | ||
39 | module_param_array(index, int, NULL, 0444); | |
40 | MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS"."); | |
41 | module_param_array(id, charp, NULL, 0444); | |
42 | MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS"."); | |
43 | module_param_array(enable, bool, NULL, 0444); | |
44 | MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS"."); | |
45 | ||
46 | static int snd_us122l_card_used[SNDRV_CARDS]; | |
47 | ||
48 | ||
49 | static int us122l_create_usbmidi(struct snd_card *card) | |
50 | { | |
51 | static struct snd_usb_midi_endpoint_info quirk_data = { | |
52 | .out_ep = 4, | |
53 | .in_ep = 3, | |
54 | .out_cables = 0x001, | |
55 | .in_cables = 0x001 | |
56 | }; | |
57 | static struct snd_usb_audio_quirk quirk = { | |
58 | .vendor_name = "US122L", | |
59 | .product_name = NAME_ALLCAPS, | |
60 | .ifnum = 1, | |
61 | .type = QUIRK_MIDI_US122L, | |
62 | .data = &quirk_data | |
63 | }; | |
a014bbad | 64 | struct usb_device *dev = US122L(card)->dev; |
030a07e4 KW |
65 | struct usb_interface *iface = usb_ifnum_to_if(dev, 1); |
66 | ||
d82af9f9 CL |
67 | return snd_usbmidi_create(card, iface, |
68 | &US122L(card)->midi_list, &quirk); | |
030a07e4 KW |
69 | } |
70 | ||
4f272341 TH |
71 | static int us144_create_usbmidi(struct snd_card *card) |
72 | { | |
73 | static struct snd_usb_midi_endpoint_info quirk_data = { | |
74 | .out_ep = 4, | |
75 | .in_ep = 3, | |
76 | .out_cables = 0x001, | |
77 | .in_cables = 0x001 | |
78 | }; | |
79 | static struct snd_usb_audio_quirk quirk = { | |
80 | .vendor_name = "US144", | |
81 | .product_name = NAME_ALLCAPS, | |
82 | .ifnum = 0, | |
83 | .type = QUIRK_MIDI_US122L, | |
84 | .data = &quirk_data | |
85 | }; | |
a014bbad | 86 | struct usb_device *dev = US122L(card)->dev; |
4f272341 TH |
87 | struct usb_interface *iface = usb_ifnum_to_if(dev, 0); |
88 | ||
d82af9f9 CL |
89 | return snd_usbmidi_create(card, iface, |
90 | &US122L(card)->midi_list, &quirk); | |
4f272341 TH |
91 | } |
92 | ||
030a07e4 KW |
93 | /* |
94 | * Wrapper for usb_control_msg(). | |
95 | * Allocates a temp buffer to prevent dmaing from/to the stack. | |
96 | */ | |
97 | static int us122l_ctl_msg(struct usb_device *dev, unsigned int pipe, | |
98 | __u8 request, __u8 requesttype, | |
99 | __u16 value, __u16 index, void *data, | |
100 | __u16 size, int timeout) | |
101 | { | |
102 | int err; | |
103 | void *buf = NULL; | |
104 | ||
105 | if (size > 0) { | |
106 | buf = kmemdup(data, size, GFP_KERNEL); | |
107 | if (!buf) | |
108 | return -ENOMEM; | |
109 | } | |
110 | err = usb_control_msg(dev, pipe, request, requesttype, | |
111 | value, index, buf, size, timeout); | |
112 | if (size > 0) { | |
113 | memcpy(data, buf, size); | |
114 | kfree(buf); | |
115 | } | |
116 | return err; | |
117 | } | |
118 | ||
119 | static void pt_info_set(struct usb_device *dev, u8 v) | |
120 | { | |
121 | int ret; | |
122 | ||
123 | ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | |
124 | 'I', | |
125 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
126 | v, 0, NULL, 0, 1000); | |
127 | snd_printdd(KERN_DEBUG "%i\n", ret); | |
128 | } | |
129 | ||
130 | static void usb_stream_hwdep_vm_open(struct vm_area_struct *area) | |
131 | { | |
132 | struct us122l *us122l = area->vm_private_data; | |
133 | atomic_inc(&us122l->mmap_count); | |
134 | snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count)); | |
135 | } | |
136 | ||
137 | static int usb_stream_hwdep_vm_fault(struct vm_area_struct *area, | |
138 | struct vm_fault *vmf) | |
139 | { | |
140 | unsigned long offset; | |
141 | struct page *page; | |
142 | void *vaddr; | |
143 | struct us122l *us122l = area->vm_private_data; | |
144 | struct usb_stream *s; | |
030a07e4 KW |
145 | |
146 | mutex_lock(&us122l->mutex); | |
147 | s = us122l->sk.s; | |
148 | if (!s) | |
428ffb71 | 149 | goto unlock; |
030a07e4 KW |
150 | |
151 | offset = vmf->pgoff << PAGE_SHIFT; | |
152 | if (offset < PAGE_ALIGN(s->read_size)) | |
153 | vaddr = (char *)s + offset; | |
154 | else { | |
155 | offset -= PAGE_ALIGN(s->read_size); | |
156 | if (offset >= PAGE_ALIGN(s->write_size)) | |
428ffb71 | 157 | goto unlock; |
030a07e4 KW |
158 | |
159 | vaddr = us122l->sk.write_page + offset; | |
160 | } | |
161 | page = virt_to_page(vaddr); | |
162 | ||
163 | get_page(page); | |
164 | mutex_unlock(&us122l->mutex); | |
165 | ||
166 | vmf->page = page; | |
428ffb71 LZ |
167 | |
168 | return 0; | |
169 | unlock: | |
170 | mutex_unlock(&us122l->mutex); | |
171 | return VM_FAULT_SIGBUS; | |
030a07e4 KW |
172 | } |
173 | ||
174 | static void usb_stream_hwdep_vm_close(struct vm_area_struct *area) | |
175 | { | |
176 | struct us122l *us122l = area->vm_private_data; | |
177 | atomic_dec(&us122l->mmap_count); | |
178 | snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count)); | |
179 | } | |
180 | ||
f0f37e2f | 181 | static const struct vm_operations_struct usb_stream_hwdep_vm_ops = { |
030a07e4 KW |
182 | .open = usb_stream_hwdep_vm_open, |
183 | .fault = usb_stream_hwdep_vm_fault, | |
184 | .close = usb_stream_hwdep_vm_close, | |
185 | }; | |
186 | ||
187 | ||
188 | static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file) | |
189 | { | |
190 | struct us122l *us122l = hw->private_data; | |
191 | struct usb_interface *iface; | |
192 | snd_printdd(KERN_DEBUG "%p %p\n", hw, file); | |
193 | if (hw->used >= 2) | |
194 | return -EBUSY; | |
195 | ||
196 | if (!us122l->first) | |
197 | us122l->first = file; | |
4f272341 | 198 | |
2b6f6c0d TH |
199 | if (us122l->dev->descriptor.idProduct == USB_ID_US144 || |
200 | us122l->dev->descriptor.idProduct == USB_ID_US144MKII) { | |
a014bbad | 201 | iface = usb_ifnum_to_if(us122l->dev, 0); |
4f272341 TH |
202 | usb_autopm_get_interface(iface); |
203 | } | |
a014bbad | 204 | iface = usb_ifnum_to_if(us122l->dev, 1); |
030a07e4 KW |
205 | usb_autopm_get_interface(iface); |
206 | return 0; | |
207 | } | |
208 | ||
209 | static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file) | |
210 | { | |
211 | struct us122l *us122l = hw->private_data; | |
4f272341 | 212 | struct usb_interface *iface; |
030a07e4 | 213 | snd_printdd(KERN_DEBUG "%p %p\n", hw, file); |
4f272341 | 214 | |
2b6f6c0d TH |
215 | if (us122l->dev->descriptor.idProduct == USB_ID_US144 || |
216 | us122l->dev->descriptor.idProduct == USB_ID_US144MKII) { | |
a014bbad | 217 | iface = usb_ifnum_to_if(us122l->dev, 0); |
4f272341 TH |
218 | usb_autopm_put_interface(iface); |
219 | } | |
a014bbad | 220 | iface = usb_ifnum_to_if(us122l->dev, 1); |
030a07e4 KW |
221 | usb_autopm_put_interface(iface); |
222 | if (us122l->first == file) | |
223 | us122l->first = NULL; | |
224 | mutex_lock(&us122l->mutex); | |
225 | if (us122l->master == file) | |
226 | us122l->master = us122l->slave; | |
227 | ||
228 | us122l->slave = NULL; | |
229 | mutex_unlock(&us122l->mutex); | |
230 | return 0; | |
231 | } | |
232 | ||
233 | static int usb_stream_hwdep_mmap(struct snd_hwdep *hw, | |
234 | struct file *filp, struct vm_area_struct *area) | |
235 | { | |
236 | unsigned long size = area->vm_end - area->vm_start; | |
237 | struct us122l *us122l = hw->private_data; | |
238 | unsigned long offset; | |
239 | struct usb_stream *s; | |
240 | int err = 0; | |
241 | bool read; | |
242 | ||
243 | offset = area->vm_pgoff << PAGE_SHIFT; | |
244 | mutex_lock(&us122l->mutex); | |
245 | s = us122l->sk.s; | |
246 | read = offset < s->read_size; | |
247 | if (read && area->vm_flags & VM_WRITE) { | |
248 | err = -EPERM; | |
249 | goto out; | |
250 | } | |
251 | snd_printdd(KERN_DEBUG "%lu %u\n", size, | |
252 | read ? s->read_size : s->write_size); | |
253 | /* if userspace tries to mmap beyond end of our buffer, fail */ | |
254 | if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) { | |
255 | snd_printk(KERN_WARNING "%lu > %u\n", size, | |
256 | read ? s->read_size : s->write_size); | |
257 | err = -EINVAL; | |
258 | goto out; | |
259 | } | |
260 | ||
261 | area->vm_ops = &usb_stream_hwdep_vm_ops; | |
262 | area->vm_flags |= VM_RESERVED; | |
263 | area->vm_private_data = us122l; | |
264 | atomic_inc(&us122l->mmap_count); | |
265 | out: | |
266 | mutex_unlock(&us122l->mutex); | |
267 | return err; | |
268 | } | |
269 | ||
270 | static unsigned int usb_stream_hwdep_poll(struct snd_hwdep *hw, | |
271 | struct file *file, poll_table *wait) | |
272 | { | |
273 | struct us122l *us122l = hw->private_data; | |
274 | struct usb_stream *s = us122l->sk.s; | |
275 | unsigned *polled; | |
276 | unsigned int mask; | |
277 | ||
278 | poll_wait(file, &us122l->sk.sleep, wait); | |
279 | ||
280 | switch (s->state) { | |
281 | case usb_stream_ready: | |
282 | if (us122l->first == file) | |
283 | polled = &s->periods_polled; | |
284 | else | |
285 | polled = &us122l->second_periods_polled; | |
286 | if (*polled != s->periods_done) { | |
287 | *polled = s->periods_done; | |
288 | mask = POLLIN | POLLOUT | POLLWRNORM; | |
289 | break; | |
290 | } | |
291 | /* Fall through */ | |
292 | mask = 0; | |
293 | break; | |
294 | default: | |
295 | mask = POLLIN | POLLOUT | POLLWRNORM | POLLERR; | |
296 | break; | |
297 | } | |
298 | return mask; | |
299 | } | |
300 | ||
301 | static void us122l_stop(struct us122l *us122l) | |
302 | { | |
303 | struct list_head *p; | |
d82af9f9 | 304 | list_for_each(p, &us122l->midi_list) |
030a07e4 KW |
305 | snd_usbmidi_input_stop(p); |
306 | ||
307 | usb_stream_stop(&us122l->sk); | |
308 | usb_stream_free(&us122l->sk); | |
309 | } | |
310 | ||
311 | static int us122l_set_sample_rate(struct usb_device *dev, int rate) | |
312 | { | |
313 | unsigned int ep = 0x81; | |
314 | unsigned char data[3]; | |
315 | int err; | |
316 | ||
317 | data[0] = rate; | |
318 | data[1] = rate >> 8; | |
319 | data[2] = rate >> 16; | |
de48c7bc | 320 | err = us122l_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR, |
030a07e4 | 321 | USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, |
de48c7bc | 322 | UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000); |
030a07e4 KW |
323 | if (err < 0) |
324 | snd_printk(KERN_ERR "%d: cannot set freq %d to ep 0x%x\n", | |
325 | dev->devnum, rate, ep); | |
326 | return err; | |
327 | } | |
328 | ||
329 | static bool us122l_start(struct us122l *us122l, | |
330 | unsigned rate, unsigned period_frames) | |
331 | { | |
332 | struct list_head *p; | |
333 | int err; | |
334 | unsigned use_packsize = 0; | |
335 | bool success = false; | |
336 | ||
a014bbad | 337 | if (us122l->dev->speed == USB_SPEED_HIGH) { |
030a07e4 KW |
338 | /* The us-122l's descriptor defaults to iso max_packsize 78, |
339 | which isn't needed for samplerates <= 48000. | |
340 | Lets save some memory: | |
341 | */ | |
342 | switch (rate) { | |
343 | case 44100: | |
344 | use_packsize = 36; | |
345 | break; | |
346 | case 48000: | |
347 | use_packsize = 42; | |
348 | break; | |
349 | case 88200: | |
350 | use_packsize = 72; | |
351 | break; | |
352 | } | |
353 | } | |
a014bbad | 354 | if (!usb_stream_new(&us122l->sk, us122l->dev, 1, 2, |
030a07e4 KW |
355 | rate, use_packsize, period_frames, 6)) |
356 | goto out; | |
357 | ||
a014bbad | 358 | err = us122l_set_sample_rate(us122l->dev, rate); |
030a07e4 KW |
359 | if (err < 0) { |
360 | us122l_stop(us122l); | |
361 | snd_printk(KERN_ERR "us122l_set_sample_rate error \n"); | |
362 | goto out; | |
363 | } | |
364 | err = usb_stream_start(&us122l->sk); | |
365 | if (err < 0) { | |
366 | us122l_stop(us122l); | |
367 | snd_printk(KERN_ERR "us122l_start error %i \n", err); | |
368 | goto out; | |
369 | } | |
d82af9f9 | 370 | list_for_each(p, &us122l->midi_list) |
030a07e4 KW |
371 | snd_usbmidi_input_start(p); |
372 | success = true; | |
373 | out: | |
374 | return success; | |
375 | } | |
376 | ||
377 | static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, | |
378 | unsigned cmd, unsigned long arg) | |
379 | { | |
380 | struct usb_stream_config *cfg; | |
381 | struct us122l *us122l = hw->private_data; | |
382 | unsigned min_period_frames; | |
383 | int err = 0; | |
384 | bool high_speed; | |
385 | ||
386 | if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS) | |
387 | return -ENOTTY; | |
388 | ||
85385c15 LZ |
389 | cfg = memdup_user((void *)arg, sizeof(*cfg)); |
390 | if (IS_ERR(cfg)) | |
391 | return PTR_ERR(cfg); | |
030a07e4 | 392 | |
030a07e4 KW |
393 | if (cfg->version != USB_STREAM_INTERFACE_VERSION) { |
394 | err = -ENXIO; | |
395 | goto free; | |
396 | } | |
a014bbad | 397 | high_speed = us122l->dev->speed == USB_SPEED_HIGH; |
030a07e4 KW |
398 | if ((cfg->sample_rate != 44100 && cfg->sample_rate != 48000 && |
399 | (!high_speed || | |
400 | (cfg->sample_rate != 88200 && cfg->sample_rate != 96000))) || | |
401 | cfg->frame_size != 6 || | |
402 | cfg->period_frames > 0x3000) { | |
403 | err = -EINVAL; | |
404 | goto free; | |
405 | } | |
406 | switch (cfg->sample_rate) { | |
407 | case 44100: | |
408 | min_period_frames = 48; | |
409 | break; | |
410 | case 48000: | |
411 | min_period_frames = 52; | |
412 | break; | |
413 | default: | |
414 | min_period_frames = 104; | |
415 | break; | |
416 | } | |
417 | if (!high_speed) | |
418 | min_period_frames <<= 1; | |
419 | if (cfg->period_frames < min_period_frames) { | |
420 | err = -EINVAL; | |
421 | goto free; | |
422 | } | |
423 | ||
424 | snd_power_wait(hw->card, SNDRV_CTL_POWER_D0); | |
425 | ||
426 | mutex_lock(&us122l->mutex); | |
427 | if (!us122l->master) | |
428 | us122l->master = file; | |
429 | else if (us122l->master != file) { | |
430 | if (memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg))) { | |
431 | err = -EIO; | |
432 | goto unlock; | |
433 | } | |
434 | us122l->slave = file; | |
435 | } | |
436 | if (!us122l->sk.s || | |
437 | memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg)) || | |
438 | us122l->sk.s->state == usb_stream_xrun) { | |
439 | us122l_stop(us122l); | |
440 | if (!us122l_start(us122l, cfg->sample_rate, cfg->period_frames)) | |
441 | err = -EIO; | |
442 | else | |
443 | err = 1; | |
444 | } | |
445 | unlock: | |
446 | mutex_unlock(&us122l->mutex); | |
447 | free: | |
448 | kfree(cfg); | |
449 | return err; | |
450 | } | |
451 | ||
452 | #define SND_USB_STREAM_ID "USB STREAM" | |
453 | static int usb_stream_hwdep_new(struct snd_card *card) | |
454 | { | |
455 | int err; | |
456 | struct snd_hwdep *hw; | |
a014bbad | 457 | struct usb_device *dev = US122L(card)->dev; |
030a07e4 KW |
458 | |
459 | err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw); | |
460 | if (err < 0) | |
461 | return err; | |
462 | ||
463 | hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM; | |
464 | hw->private_data = US122L(card); | |
465 | hw->ops.open = usb_stream_hwdep_open; | |
466 | hw->ops.release = usb_stream_hwdep_release; | |
467 | hw->ops.ioctl = usb_stream_hwdep_ioctl; | |
468 | hw->ops.ioctl_compat = usb_stream_hwdep_ioctl; | |
469 | hw->ops.mmap = usb_stream_hwdep_mmap; | |
470 | hw->ops.poll = usb_stream_hwdep_poll; | |
471 | ||
472 | sprintf(hw->name, "/proc/bus/usb/%03d/%03d/hwdeppcm", | |
473 | dev->bus->busnum, dev->devnum); | |
474 | return 0; | |
475 | } | |
476 | ||
477 | ||
478 | static bool us122l_create_card(struct snd_card *card) | |
479 | { | |
480 | int err; | |
481 | struct us122l *us122l = US122L(card); | |
482 | ||
2b6f6c0d TH |
483 | if (us122l->dev->descriptor.idProduct == USB_ID_US144 || |
484 | us122l->dev->descriptor.idProduct == USB_ID_US144MKII) { | |
a014bbad | 485 | err = usb_set_interface(us122l->dev, 0, 1); |
4f272341 TH |
486 | if (err) { |
487 | snd_printk(KERN_ERR "usb_set_interface error \n"); | |
488 | return false; | |
489 | } | |
490 | } | |
a014bbad | 491 | err = usb_set_interface(us122l->dev, 1, 1); |
030a07e4 KW |
492 | if (err) { |
493 | snd_printk(KERN_ERR "usb_set_interface error \n"); | |
494 | return false; | |
495 | } | |
496 | ||
a014bbad CL |
497 | pt_info_set(us122l->dev, 0x11); |
498 | pt_info_set(us122l->dev, 0x10); | |
030a07e4 KW |
499 | |
500 | if (!us122l_start(us122l, 44100, 256)) | |
501 | return false; | |
502 | ||
2b6f6c0d TH |
503 | if (us122l->dev->descriptor.idProduct == USB_ID_US144 || |
504 | us122l->dev->descriptor.idProduct == USB_ID_US144MKII) | |
4f272341 TH |
505 | err = us144_create_usbmidi(card); |
506 | else | |
507 | err = us122l_create_usbmidi(card); | |
030a07e4 KW |
508 | if (err < 0) { |
509 | snd_printk(KERN_ERR "us122l_create_usbmidi error %i \n", err); | |
510 | us122l_stop(us122l); | |
511 | return false; | |
512 | } | |
513 | err = usb_stream_hwdep_new(card); | |
514 | if (err < 0) { | |
515 | /* release the midi resources */ | |
516 | struct list_head *p; | |
d82af9f9 | 517 | list_for_each(p, &us122l->midi_list) |
030a07e4 KW |
518 | snd_usbmidi_disconnect(p); |
519 | ||
520 | us122l_stop(us122l); | |
521 | return false; | |
522 | } | |
523 | return true; | |
524 | } | |
525 | ||
5d4af1be KW |
526 | static void snd_us122l_free(struct snd_card *card) |
527 | { | |
528 | struct us122l *us122l = US122L(card); | |
a014bbad | 529 | int index = us122l->card_index; |
5d4af1be KW |
530 | if (index >= 0 && index < SNDRV_CARDS) |
531 | snd_us122l_card_used[index] = 0; | |
532 | } | |
533 | ||
51721f70 | 534 | static int usx2y_create_card(struct usb_device *device, struct snd_card **cardp) |
030a07e4 KW |
535 | { |
536 | int dev; | |
537 | struct snd_card *card; | |
bd7dd77c TI |
538 | int err; |
539 | ||
030a07e4 KW |
540 | for (dev = 0; dev < SNDRV_CARDS; ++dev) |
541 | if (enable[dev] && !snd_us122l_card_used[dev]) | |
542 | break; | |
543 | if (dev >= SNDRV_CARDS) | |
51721f70 | 544 | return -ENODEV; |
bd7dd77c TI |
545 | err = snd_card_create(index[dev], id[dev], THIS_MODULE, |
546 | sizeof(struct us122l), &card); | |
547 | if (err < 0) | |
51721f70 | 548 | return err; |
a014bbad | 549 | snd_us122l_card_used[US122L(card)->card_index = dev] = 1; |
5d4af1be | 550 | card->private_free = snd_us122l_free; |
a014bbad | 551 | US122L(card)->dev = device; |
030a07e4 KW |
552 | mutex_init(&US122L(card)->mutex); |
553 | init_waitqueue_head(&US122L(card)->sk.sleep); | |
d82af9f9 | 554 | INIT_LIST_HEAD(&US122L(card)->midi_list); |
030a07e4 KW |
555 | strcpy(card->driver, "USB "NAME_ALLCAPS""); |
556 | sprintf(card->shortname, "TASCAM "NAME_ALLCAPS""); | |
557 | sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)", | |
558 | card->shortname, | |
559 | le16_to_cpu(device->descriptor.idVendor), | |
560 | le16_to_cpu(device->descriptor.idProduct), | |
561 | 0, | |
a014bbad CL |
562 | US122L(card)->dev->bus->busnum, |
563 | US122L(card)->dev->devnum | |
030a07e4 | 564 | ); |
51721f70 TI |
565 | *cardp = card; |
566 | return 0; | |
030a07e4 KW |
567 | } |
568 | ||
51721f70 TI |
569 | static int us122l_usb_probe(struct usb_interface *intf, |
570 | const struct usb_device_id *device_id, | |
571 | struct snd_card **cardp) | |
030a07e4 KW |
572 | { |
573 | struct usb_device *device = interface_to_usbdev(intf); | |
51721f70 TI |
574 | struct snd_card *card; |
575 | int err; | |
030a07e4 | 576 | |
51721f70 TI |
577 | err = usx2y_create_card(device, &card); |
578 | if (err < 0) | |
579 | return err; | |
030a07e4 | 580 | |
3f5d3465 | 581 | snd_card_set_dev(card, &intf->dev); |
51721f70 | 582 | if (!us122l_create_card(card)) { |
030a07e4 | 583 | snd_card_free(card); |
51721f70 TI |
584 | return -EINVAL; |
585 | } | |
586 | ||
587 | err = snd_card_register(card); | |
588 | if (err < 0) { | |
589 | snd_card_free(card); | |
590 | return err; | |
030a07e4 KW |
591 | } |
592 | ||
4f272341 | 593 | usb_get_intf(usb_ifnum_to_if(device, 0)); |
030a07e4 | 594 | usb_get_dev(device); |
51721f70 TI |
595 | *cardp = card; |
596 | return 0; | |
030a07e4 KW |
597 | } |
598 | ||
599 | static int snd_us122l_probe(struct usb_interface *intf, | |
600 | const struct usb_device_id *id) | |
601 | { | |
4f272341 | 602 | struct usb_device *device = interface_to_usbdev(intf); |
030a07e4 | 603 | struct snd_card *card; |
51721f70 TI |
604 | int err; |
605 | ||
2b6f6c0d TH |
606 | if ((device->descriptor.idProduct == USB_ID_US144 || |
607 | device->descriptor.idProduct == USB_ID_US144MKII) | |
4f272341 TH |
608 | && device->speed == USB_SPEED_HIGH) { |
609 | snd_printk(KERN_ERR "disable ehci-hcd to run US-144 \n"); | |
a688e488 | 610 | return -ENODEV; |
4f272341 TH |
611 | } |
612 | ||
030a07e4 KW |
613 | snd_printdd(KERN_DEBUG"%p:%i\n", |
614 | intf, intf->cur_altsetting->desc.bInterfaceNumber); | |
615 | if (intf->cur_altsetting->desc.bInterfaceNumber != 1) | |
616 | return 0; | |
617 | ||
51721f70 TI |
618 | err = us122l_usb_probe(usb_get_intf(intf), id, &card); |
619 | if (err < 0) { | |
620 | usb_put_intf(intf); | |
621 | return err; | |
030a07e4 KW |
622 | } |
623 | ||
51721f70 TI |
624 | usb_set_intfdata(intf, card); |
625 | return 0; | |
030a07e4 KW |
626 | } |
627 | ||
628 | static void snd_us122l_disconnect(struct usb_interface *intf) | |
629 | { | |
630 | struct snd_card *card; | |
631 | struct us122l *us122l; | |
632 | struct list_head *p; | |
633 | ||
634 | card = usb_get_intfdata(intf); | |
635 | if (!card) | |
636 | return; | |
637 | ||
638 | snd_card_disconnect(card); | |
639 | ||
640 | us122l = US122L(card); | |
641 | mutex_lock(&us122l->mutex); | |
642 | us122l_stop(us122l); | |
643 | mutex_unlock(&us122l->mutex); | |
030a07e4 KW |
644 | |
645 | /* release the midi resources */ | |
d82af9f9 | 646 | list_for_each(p, &us122l->midi_list) { |
030a07e4 KW |
647 | snd_usbmidi_disconnect(p); |
648 | } | |
649 | ||
a014bbad CL |
650 | usb_put_intf(usb_ifnum_to_if(us122l->dev, 0)); |
651 | usb_put_intf(usb_ifnum_to_if(us122l->dev, 1)); | |
652 | usb_put_dev(us122l->dev); | |
030a07e4 KW |
653 | |
654 | while (atomic_read(&us122l->mmap_count)) | |
655 | msleep(500); | |
656 | ||
657 | snd_card_free(card); | |
658 | } | |
659 | ||
660 | static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message) | |
661 | { | |
662 | struct snd_card *card; | |
663 | struct us122l *us122l; | |
664 | struct list_head *p; | |
665 | ||
f4e9749f | 666 | card = usb_get_intfdata(intf); |
030a07e4 KW |
667 | if (!card) |
668 | return 0; | |
669 | snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); | |
670 | ||
671 | us122l = US122L(card); | |
672 | if (!us122l) | |
673 | return 0; | |
674 | ||
d82af9f9 | 675 | list_for_each(p, &us122l->midi_list) |
030a07e4 KW |
676 | snd_usbmidi_input_stop(p); |
677 | ||
678 | mutex_lock(&us122l->mutex); | |
679 | usb_stream_stop(&us122l->sk); | |
680 | mutex_unlock(&us122l->mutex); | |
681 | ||
682 | return 0; | |
683 | } | |
684 | ||
685 | static int snd_us122l_resume(struct usb_interface *intf) | |
686 | { | |
687 | struct snd_card *card; | |
688 | struct us122l *us122l; | |
689 | struct list_head *p; | |
690 | int err; | |
691 | ||
f4e9749f | 692 | card = usb_get_intfdata(intf); |
030a07e4 KW |
693 | if (!card) |
694 | return 0; | |
695 | ||
696 | us122l = US122L(card); | |
697 | if (!us122l) | |
698 | return 0; | |
699 | ||
700 | mutex_lock(&us122l->mutex); | |
701 | /* needed, doesn't restart without: */ | |
2b6f6c0d TH |
702 | if (us122l->dev->descriptor.idProduct == USB_ID_US144 || |
703 | us122l->dev->descriptor.idProduct == USB_ID_US144MKII) { | |
a014bbad | 704 | err = usb_set_interface(us122l->dev, 0, 1); |
4f272341 TH |
705 | if (err) { |
706 | snd_printk(KERN_ERR "usb_set_interface error \n"); | |
707 | goto unlock; | |
708 | } | |
709 | } | |
a014bbad | 710 | err = usb_set_interface(us122l->dev, 1, 1); |
030a07e4 KW |
711 | if (err) { |
712 | snd_printk(KERN_ERR "usb_set_interface error \n"); | |
713 | goto unlock; | |
714 | } | |
715 | ||
a014bbad CL |
716 | pt_info_set(us122l->dev, 0x11); |
717 | pt_info_set(us122l->dev, 0x10); | |
030a07e4 | 718 | |
a014bbad | 719 | err = us122l_set_sample_rate(us122l->dev, |
030a07e4 KW |
720 | us122l->sk.s->cfg.sample_rate); |
721 | if (err < 0) { | |
722 | snd_printk(KERN_ERR "us122l_set_sample_rate error \n"); | |
723 | goto unlock; | |
724 | } | |
725 | err = usb_stream_start(&us122l->sk); | |
726 | if (err) | |
727 | goto unlock; | |
728 | ||
d82af9f9 | 729 | list_for_each(p, &us122l->midi_list) |
030a07e4 KW |
730 | snd_usbmidi_input_start(p); |
731 | unlock: | |
732 | mutex_unlock(&us122l->mutex); | |
733 | snd_power_change_state(card, SNDRV_CTL_POWER_D0); | |
734 | return err; | |
735 | } | |
736 | ||
737 | static struct usb_device_id snd_us122l_usb_id_table[] = { | |
738 | { | |
739 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, | |
740 | .idVendor = 0x0644, | |
741 | .idProduct = USB_ID_US122L | |
742 | }, | |
4f272341 TH |
743 | { /* US-144 only works at USB1.1! Disable module ehci-hcd. */ |
744 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, | |
745 | .idVendor = 0x0644, | |
746 | .idProduct = USB_ID_US144 | |
747 | }, | |
2b6f6c0d TH |
748 | { |
749 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, | |
750 | .idVendor = 0x0644, | |
751 | .idProduct = USB_ID_US122MKII | |
752 | }, | |
753 | { | |
754 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, | |
755 | .idVendor = 0x0644, | |
756 | .idProduct = USB_ID_US144MKII | |
757 | }, | |
030a07e4 KW |
758 | { /* terminator */ } |
759 | }; | |
760 | ||
761 | MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table); | |
762 | static struct usb_driver snd_us122l_usb_driver = { | |
763 | .name = "snd-usb-us122l", | |
764 | .probe = snd_us122l_probe, | |
765 | .disconnect = snd_us122l_disconnect, | |
766 | .suspend = snd_us122l_suspend, | |
767 | .resume = snd_us122l_resume, | |
768 | .reset_resume = snd_us122l_resume, | |
769 | .id_table = snd_us122l_usb_id_table, | |
770 | .supports_autosuspend = 1 | |
771 | }; | |
772 | ||
773 | ||
774 | static int __init snd_us122l_module_init(void) | |
775 | { | |
776 | return usb_register(&snd_us122l_usb_driver); | |
777 | } | |
778 | ||
779 | static void __exit snd_us122l_module_exit(void) | |
780 | { | |
781 | usb_deregister(&snd_us122l_usb_driver); | |
782 | } | |
783 | ||
784 | module_init(snd_us122l_module_init) | |
785 | module_exit(snd_us122l_module_exit) |