1030a07e4SKarsten Wiese /* 2030a07e4SKarsten Wiese * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de> 3030a07e4SKarsten Wiese * 4030a07e4SKarsten Wiese * This program is free software; you can redistribute it and/or modify it 5030a07e4SKarsten Wiese * under the terms of the GNU General Public License as published by the 6030a07e4SKarsten Wiese * Free Software Foundation; either version 2 of the License, or (at your 7030a07e4SKarsten Wiese * option) any later version. 8030a07e4SKarsten Wiese * 9030a07e4SKarsten Wiese * This program is distributed in the hope that it will be useful, but 10030a07e4SKarsten Wiese * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 11030a07e4SKarsten Wiese * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12030a07e4SKarsten Wiese * for more details. 13030a07e4SKarsten Wiese * 14030a07e4SKarsten Wiese * You should have received a copy of the GNU General Public License 15030a07e4SKarsten Wiese * along with this program; if not, write to the Free Software Foundation, 16030a07e4SKarsten Wiese * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17030a07e4SKarsten Wiese */ 18030a07e4SKarsten Wiese 19030a07e4SKarsten Wiese #include <sound/core.h> 20030a07e4SKarsten Wiese #include <sound/hwdep.h> 21030a07e4SKarsten Wiese #include <sound/pcm.h> 22030a07e4SKarsten Wiese #include <sound/initval.h> 23030a07e4SKarsten Wiese #define MODNAME "US122L" 24030a07e4SKarsten Wiese #include "usb_stream.c" 25030a07e4SKarsten Wiese #include "../usbaudio.h" 26030a07e4SKarsten Wiese #include "us122l.h" 27030a07e4SKarsten Wiese 28030a07e4SKarsten Wiese MODULE_AUTHOR("Karsten Wiese <fzu@wemgehoertderstaat.de>"); 29030a07e4SKarsten Wiese MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.5"); 30030a07e4SKarsten Wiese MODULE_LICENSE("GPL"); 31030a07e4SKarsten Wiese 32030a07e4SKarsten Wiese static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */ 33030a07e4SKarsten Wiese static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */ 34030a07e4SKarsten Wiese /* Enable this card */ 35030a07e4SKarsten Wiese static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; 36030a07e4SKarsten Wiese 37030a07e4SKarsten Wiese module_param_array(index, int, NULL, 0444); 38030a07e4SKarsten Wiese MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS"."); 39030a07e4SKarsten Wiese module_param_array(id, charp, NULL, 0444); 40030a07e4SKarsten Wiese MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS"."); 41030a07e4SKarsten Wiese module_param_array(enable, bool, NULL, 0444); 42030a07e4SKarsten Wiese MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS"."); 43030a07e4SKarsten Wiese 44030a07e4SKarsten Wiese static int snd_us122l_card_used[SNDRV_CARDS]; 45030a07e4SKarsten Wiese 46030a07e4SKarsten Wiese 47030a07e4SKarsten Wiese static int us122l_create_usbmidi(struct snd_card *card) 48030a07e4SKarsten Wiese { 49030a07e4SKarsten Wiese static struct snd_usb_midi_endpoint_info quirk_data = { 50030a07e4SKarsten Wiese .out_ep = 4, 51030a07e4SKarsten Wiese .in_ep = 3, 52030a07e4SKarsten Wiese .out_cables = 0x001, 53030a07e4SKarsten Wiese .in_cables = 0x001 54030a07e4SKarsten Wiese }; 55030a07e4SKarsten Wiese static struct snd_usb_audio_quirk quirk = { 56030a07e4SKarsten Wiese .vendor_name = "US122L", 57030a07e4SKarsten Wiese .product_name = NAME_ALLCAPS, 58030a07e4SKarsten Wiese .ifnum = 1, 59030a07e4SKarsten Wiese .type = QUIRK_MIDI_US122L, 60030a07e4SKarsten Wiese .data = &quirk_data 61030a07e4SKarsten Wiese }; 62030a07e4SKarsten Wiese struct usb_device *dev = US122L(card)->chip.dev; 63030a07e4SKarsten Wiese struct usb_interface *iface = usb_ifnum_to_if(dev, 1); 64030a07e4SKarsten Wiese 65030a07e4SKarsten Wiese return snd_usb_create_midi_interface(&US122L(card)->chip, 66030a07e4SKarsten Wiese iface, &quirk); 67030a07e4SKarsten Wiese } 68030a07e4SKarsten Wiese 69030a07e4SKarsten Wiese /* 70030a07e4SKarsten Wiese * Wrapper for usb_control_msg(). 71030a07e4SKarsten Wiese * Allocates a temp buffer to prevent dmaing from/to the stack. 72030a07e4SKarsten Wiese */ 73030a07e4SKarsten Wiese static int us122l_ctl_msg(struct usb_device *dev, unsigned int pipe, 74030a07e4SKarsten Wiese __u8 request, __u8 requesttype, 75030a07e4SKarsten Wiese __u16 value, __u16 index, void *data, 76030a07e4SKarsten Wiese __u16 size, int timeout) 77030a07e4SKarsten Wiese { 78030a07e4SKarsten Wiese int err; 79030a07e4SKarsten Wiese void *buf = NULL; 80030a07e4SKarsten Wiese 81030a07e4SKarsten Wiese if (size > 0) { 82030a07e4SKarsten Wiese buf = kmemdup(data, size, GFP_KERNEL); 83030a07e4SKarsten Wiese if (!buf) 84030a07e4SKarsten Wiese return -ENOMEM; 85030a07e4SKarsten Wiese } 86030a07e4SKarsten Wiese err = usb_control_msg(dev, pipe, request, requesttype, 87030a07e4SKarsten Wiese value, index, buf, size, timeout); 88030a07e4SKarsten Wiese if (size > 0) { 89030a07e4SKarsten Wiese memcpy(data, buf, size); 90030a07e4SKarsten Wiese kfree(buf); 91030a07e4SKarsten Wiese } 92030a07e4SKarsten Wiese return err; 93030a07e4SKarsten Wiese } 94030a07e4SKarsten Wiese 95030a07e4SKarsten Wiese static void pt_info_set(struct usb_device *dev, u8 v) 96030a07e4SKarsten Wiese { 97030a07e4SKarsten Wiese int ret; 98030a07e4SKarsten Wiese 99030a07e4SKarsten Wiese ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 100030a07e4SKarsten Wiese 'I', 101030a07e4SKarsten Wiese USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 102030a07e4SKarsten Wiese v, 0, NULL, 0, 1000); 103030a07e4SKarsten Wiese snd_printdd(KERN_DEBUG "%i\n", ret); 104030a07e4SKarsten Wiese } 105030a07e4SKarsten Wiese 106030a07e4SKarsten Wiese static void usb_stream_hwdep_vm_open(struct vm_area_struct *area) 107030a07e4SKarsten Wiese { 108030a07e4SKarsten Wiese struct us122l *us122l = area->vm_private_data; 109030a07e4SKarsten Wiese atomic_inc(&us122l->mmap_count); 110030a07e4SKarsten Wiese snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count)); 111030a07e4SKarsten Wiese } 112030a07e4SKarsten Wiese 113030a07e4SKarsten Wiese static int usb_stream_hwdep_vm_fault(struct vm_area_struct *area, 114030a07e4SKarsten Wiese struct vm_fault *vmf) 115030a07e4SKarsten Wiese { 116030a07e4SKarsten Wiese unsigned long offset; 117030a07e4SKarsten Wiese struct page *page; 118030a07e4SKarsten Wiese void *vaddr; 119030a07e4SKarsten Wiese struct us122l *us122l = area->vm_private_data; 120030a07e4SKarsten Wiese struct usb_stream *s; 121030a07e4SKarsten Wiese 122030a07e4SKarsten Wiese mutex_lock(&us122l->mutex); 123030a07e4SKarsten Wiese s = us122l->sk.s; 124030a07e4SKarsten Wiese if (!s) 125428ffb71SLi Zefan goto unlock; 126030a07e4SKarsten Wiese 127030a07e4SKarsten Wiese offset = vmf->pgoff << PAGE_SHIFT; 128030a07e4SKarsten Wiese if (offset < PAGE_ALIGN(s->read_size)) 129030a07e4SKarsten Wiese vaddr = (char *)s + offset; 130030a07e4SKarsten Wiese else { 131030a07e4SKarsten Wiese offset -= PAGE_ALIGN(s->read_size); 132030a07e4SKarsten Wiese if (offset >= PAGE_ALIGN(s->write_size)) 133428ffb71SLi Zefan goto unlock; 134030a07e4SKarsten Wiese 135030a07e4SKarsten Wiese vaddr = us122l->sk.write_page + offset; 136030a07e4SKarsten Wiese } 137030a07e4SKarsten Wiese page = virt_to_page(vaddr); 138030a07e4SKarsten Wiese 139030a07e4SKarsten Wiese get_page(page); 140030a07e4SKarsten Wiese mutex_unlock(&us122l->mutex); 141030a07e4SKarsten Wiese 142030a07e4SKarsten Wiese vmf->page = page; 143428ffb71SLi Zefan 144428ffb71SLi Zefan return 0; 145428ffb71SLi Zefan unlock: 146428ffb71SLi Zefan mutex_unlock(&us122l->mutex); 147428ffb71SLi Zefan return VM_FAULT_SIGBUS; 148030a07e4SKarsten Wiese } 149030a07e4SKarsten Wiese 150030a07e4SKarsten Wiese static void usb_stream_hwdep_vm_close(struct vm_area_struct *area) 151030a07e4SKarsten Wiese { 152030a07e4SKarsten Wiese struct us122l *us122l = area->vm_private_data; 153030a07e4SKarsten Wiese atomic_dec(&us122l->mmap_count); 154030a07e4SKarsten Wiese snd_printdd(KERN_DEBUG "%i\n", atomic_read(&us122l->mmap_count)); 155030a07e4SKarsten Wiese } 156030a07e4SKarsten Wiese 157030a07e4SKarsten Wiese static struct vm_operations_struct usb_stream_hwdep_vm_ops = { 158030a07e4SKarsten Wiese .open = usb_stream_hwdep_vm_open, 159030a07e4SKarsten Wiese .fault = usb_stream_hwdep_vm_fault, 160030a07e4SKarsten Wiese .close = usb_stream_hwdep_vm_close, 161030a07e4SKarsten Wiese }; 162030a07e4SKarsten Wiese 163030a07e4SKarsten Wiese 164030a07e4SKarsten Wiese static int usb_stream_hwdep_open(struct snd_hwdep *hw, struct file *file) 165030a07e4SKarsten Wiese { 166030a07e4SKarsten Wiese struct us122l *us122l = hw->private_data; 167030a07e4SKarsten Wiese struct usb_interface *iface; 168030a07e4SKarsten Wiese snd_printdd(KERN_DEBUG "%p %p\n", hw, file); 169030a07e4SKarsten Wiese if (hw->used >= 2) 170030a07e4SKarsten Wiese return -EBUSY; 171030a07e4SKarsten Wiese 172030a07e4SKarsten Wiese if (!us122l->first) 173030a07e4SKarsten Wiese us122l->first = file; 174030a07e4SKarsten Wiese iface = usb_ifnum_to_if(us122l->chip.dev, 1); 175030a07e4SKarsten Wiese usb_autopm_get_interface(iface); 176030a07e4SKarsten Wiese return 0; 177030a07e4SKarsten Wiese } 178030a07e4SKarsten Wiese 179030a07e4SKarsten Wiese static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file) 180030a07e4SKarsten Wiese { 181030a07e4SKarsten Wiese struct us122l *us122l = hw->private_data; 182030a07e4SKarsten Wiese struct usb_interface *iface = usb_ifnum_to_if(us122l->chip.dev, 1); 183030a07e4SKarsten Wiese snd_printdd(KERN_DEBUG "%p %p\n", hw, file); 184030a07e4SKarsten Wiese usb_autopm_put_interface(iface); 185030a07e4SKarsten Wiese if (us122l->first == file) 186030a07e4SKarsten Wiese us122l->first = NULL; 187030a07e4SKarsten Wiese mutex_lock(&us122l->mutex); 188030a07e4SKarsten Wiese if (us122l->master == file) 189030a07e4SKarsten Wiese us122l->master = us122l->slave; 190030a07e4SKarsten Wiese 191030a07e4SKarsten Wiese us122l->slave = NULL; 192030a07e4SKarsten Wiese mutex_unlock(&us122l->mutex); 193030a07e4SKarsten Wiese return 0; 194030a07e4SKarsten Wiese } 195030a07e4SKarsten Wiese 196030a07e4SKarsten Wiese static int usb_stream_hwdep_mmap(struct snd_hwdep *hw, 197030a07e4SKarsten Wiese struct file *filp, struct vm_area_struct *area) 198030a07e4SKarsten Wiese { 199030a07e4SKarsten Wiese unsigned long size = area->vm_end - area->vm_start; 200030a07e4SKarsten Wiese struct us122l *us122l = hw->private_data; 201030a07e4SKarsten Wiese unsigned long offset; 202030a07e4SKarsten Wiese struct usb_stream *s; 203030a07e4SKarsten Wiese int err = 0; 204030a07e4SKarsten Wiese bool read; 205030a07e4SKarsten Wiese 206030a07e4SKarsten Wiese offset = area->vm_pgoff << PAGE_SHIFT; 207030a07e4SKarsten Wiese mutex_lock(&us122l->mutex); 208030a07e4SKarsten Wiese s = us122l->sk.s; 209030a07e4SKarsten Wiese read = offset < s->read_size; 210030a07e4SKarsten Wiese if (read && area->vm_flags & VM_WRITE) { 211030a07e4SKarsten Wiese err = -EPERM; 212030a07e4SKarsten Wiese goto out; 213030a07e4SKarsten Wiese } 214030a07e4SKarsten Wiese snd_printdd(KERN_DEBUG "%lu %u\n", size, 215030a07e4SKarsten Wiese read ? s->read_size : s->write_size); 216030a07e4SKarsten Wiese /* if userspace tries to mmap beyond end of our buffer, fail */ 217030a07e4SKarsten Wiese if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) { 218030a07e4SKarsten Wiese snd_printk(KERN_WARNING "%lu > %u\n", size, 219030a07e4SKarsten Wiese read ? s->read_size : s->write_size); 220030a07e4SKarsten Wiese err = -EINVAL; 221030a07e4SKarsten Wiese goto out; 222030a07e4SKarsten Wiese } 223030a07e4SKarsten Wiese 224030a07e4SKarsten Wiese area->vm_ops = &usb_stream_hwdep_vm_ops; 225030a07e4SKarsten Wiese area->vm_flags |= VM_RESERVED; 226030a07e4SKarsten Wiese area->vm_private_data = us122l; 227030a07e4SKarsten Wiese atomic_inc(&us122l->mmap_count); 228030a07e4SKarsten Wiese out: 229030a07e4SKarsten Wiese mutex_unlock(&us122l->mutex); 230030a07e4SKarsten Wiese return err; 231030a07e4SKarsten Wiese } 232030a07e4SKarsten Wiese 233030a07e4SKarsten Wiese static unsigned int usb_stream_hwdep_poll(struct snd_hwdep *hw, 234030a07e4SKarsten Wiese struct file *file, poll_table *wait) 235030a07e4SKarsten Wiese { 236030a07e4SKarsten Wiese struct us122l *us122l = hw->private_data; 237030a07e4SKarsten Wiese struct usb_stream *s = us122l->sk.s; 238030a07e4SKarsten Wiese unsigned *polled; 239030a07e4SKarsten Wiese unsigned int mask; 240030a07e4SKarsten Wiese 241030a07e4SKarsten Wiese poll_wait(file, &us122l->sk.sleep, wait); 242030a07e4SKarsten Wiese 243030a07e4SKarsten Wiese switch (s->state) { 244030a07e4SKarsten Wiese case usb_stream_ready: 245030a07e4SKarsten Wiese if (us122l->first == file) 246030a07e4SKarsten Wiese polled = &s->periods_polled; 247030a07e4SKarsten Wiese else 248030a07e4SKarsten Wiese polled = &us122l->second_periods_polled; 249030a07e4SKarsten Wiese if (*polled != s->periods_done) { 250030a07e4SKarsten Wiese *polled = s->periods_done; 251030a07e4SKarsten Wiese mask = POLLIN | POLLOUT | POLLWRNORM; 252030a07e4SKarsten Wiese break; 253030a07e4SKarsten Wiese } 254030a07e4SKarsten Wiese /* Fall through */ 255030a07e4SKarsten Wiese mask = 0; 256030a07e4SKarsten Wiese break; 257030a07e4SKarsten Wiese default: 258030a07e4SKarsten Wiese mask = POLLIN | POLLOUT | POLLWRNORM | POLLERR; 259030a07e4SKarsten Wiese break; 260030a07e4SKarsten Wiese } 261030a07e4SKarsten Wiese return mask; 262030a07e4SKarsten Wiese } 263030a07e4SKarsten Wiese 264030a07e4SKarsten Wiese static void us122l_stop(struct us122l *us122l) 265030a07e4SKarsten Wiese { 266030a07e4SKarsten Wiese struct list_head *p; 267030a07e4SKarsten Wiese list_for_each(p, &us122l->chip.midi_list) 268030a07e4SKarsten Wiese snd_usbmidi_input_stop(p); 269030a07e4SKarsten Wiese 270030a07e4SKarsten Wiese usb_stream_stop(&us122l->sk); 271030a07e4SKarsten Wiese usb_stream_free(&us122l->sk); 272030a07e4SKarsten Wiese } 273030a07e4SKarsten Wiese 274030a07e4SKarsten Wiese static int us122l_set_sample_rate(struct usb_device *dev, int rate) 275030a07e4SKarsten Wiese { 276030a07e4SKarsten Wiese unsigned int ep = 0x81; 277030a07e4SKarsten Wiese unsigned char data[3]; 278030a07e4SKarsten Wiese int err; 279030a07e4SKarsten Wiese 280030a07e4SKarsten Wiese data[0] = rate; 281030a07e4SKarsten Wiese data[1] = rate >> 8; 282030a07e4SKarsten Wiese data[2] = rate >> 16; 283030a07e4SKarsten Wiese err = us122l_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, 284030a07e4SKarsten Wiese USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, 285030a07e4SKarsten Wiese SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000); 286030a07e4SKarsten Wiese if (err < 0) 287030a07e4SKarsten Wiese snd_printk(KERN_ERR "%d: cannot set freq %d to ep 0x%x\n", 288030a07e4SKarsten Wiese dev->devnum, rate, ep); 289030a07e4SKarsten Wiese return err; 290030a07e4SKarsten Wiese } 291030a07e4SKarsten Wiese 292030a07e4SKarsten Wiese static bool us122l_start(struct us122l *us122l, 293030a07e4SKarsten Wiese unsigned rate, unsigned period_frames) 294030a07e4SKarsten Wiese { 295030a07e4SKarsten Wiese struct list_head *p; 296030a07e4SKarsten Wiese int err; 297030a07e4SKarsten Wiese unsigned use_packsize = 0; 298030a07e4SKarsten Wiese bool success = false; 299030a07e4SKarsten Wiese 300030a07e4SKarsten Wiese if (us122l->chip.dev->speed == USB_SPEED_HIGH) { 301030a07e4SKarsten Wiese /* The us-122l's descriptor defaults to iso max_packsize 78, 302030a07e4SKarsten Wiese which isn't needed for samplerates <= 48000. 303030a07e4SKarsten Wiese Lets save some memory: 304030a07e4SKarsten Wiese */ 305030a07e4SKarsten Wiese switch (rate) { 306030a07e4SKarsten Wiese case 44100: 307030a07e4SKarsten Wiese use_packsize = 36; 308030a07e4SKarsten Wiese break; 309030a07e4SKarsten Wiese case 48000: 310030a07e4SKarsten Wiese use_packsize = 42; 311030a07e4SKarsten Wiese break; 312030a07e4SKarsten Wiese case 88200: 313030a07e4SKarsten Wiese use_packsize = 72; 314030a07e4SKarsten Wiese break; 315030a07e4SKarsten Wiese } 316030a07e4SKarsten Wiese } 317030a07e4SKarsten Wiese if (!usb_stream_new(&us122l->sk, us122l->chip.dev, 1, 2, 318030a07e4SKarsten Wiese rate, use_packsize, period_frames, 6)) 319030a07e4SKarsten Wiese goto out; 320030a07e4SKarsten Wiese 321030a07e4SKarsten Wiese err = us122l_set_sample_rate(us122l->chip.dev, rate); 322030a07e4SKarsten Wiese if (err < 0) { 323030a07e4SKarsten Wiese us122l_stop(us122l); 324030a07e4SKarsten Wiese snd_printk(KERN_ERR "us122l_set_sample_rate error \n"); 325030a07e4SKarsten Wiese goto out; 326030a07e4SKarsten Wiese } 327030a07e4SKarsten Wiese err = usb_stream_start(&us122l->sk); 328030a07e4SKarsten Wiese if (err < 0) { 329030a07e4SKarsten Wiese us122l_stop(us122l); 330030a07e4SKarsten Wiese snd_printk(KERN_ERR "us122l_start error %i \n", err); 331030a07e4SKarsten Wiese goto out; 332030a07e4SKarsten Wiese } 333030a07e4SKarsten Wiese list_for_each(p, &us122l->chip.midi_list) 334030a07e4SKarsten Wiese snd_usbmidi_input_start(p); 335030a07e4SKarsten Wiese success = true; 336030a07e4SKarsten Wiese out: 337030a07e4SKarsten Wiese return success; 338030a07e4SKarsten Wiese } 339030a07e4SKarsten Wiese 340030a07e4SKarsten Wiese static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, 341030a07e4SKarsten Wiese unsigned cmd, unsigned long arg) 342030a07e4SKarsten Wiese { 343030a07e4SKarsten Wiese struct usb_stream_config *cfg; 344030a07e4SKarsten Wiese struct us122l *us122l = hw->private_data; 345030a07e4SKarsten Wiese unsigned min_period_frames; 346030a07e4SKarsten Wiese int err = 0; 347030a07e4SKarsten Wiese bool high_speed; 348030a07e4SKarsten Wiese 349030a07e4SKarsten Wiese if (cmd != SNDRV_USB_STREAM_IOCTL_SET_PARAMS) 350030a07e4SKarsten Wiese return -ENOTTY; 351030a07e4SKarsten Wiese 352030a07e4SKarsten Wiese cfg = kmalloc(sizeof(*cfg), GFP_KERNEL); 353030a07e4SKarsten Wiese if (!cfg) 354030a07e4SKarsten Wiese return -ENOMEM; 355030a07e4SKarsten Wiese 356030a07e4SKarsten Wiese if (copy_from_user(cfg, (void *)arg, sizeof(*cfg))) { 357030a07e4SKarsten Wiese err = -EFAULT; 358030a07e4SKarsten Wiese goto free; 359030a07e4SKarsten Wiese } 360030a07e4SKarsten Wiese if (cfg->version != USB_STREAM_INTERFACE_VERSION) { 361030a07e4SKarsten Wiese err = -ENXIO; 362030a07e4SKarsten Wiese goto free; 363030a07e4SKarsten Wiese } 364030a07e4SKarsten Wiese high_speed = us122l->chip.dev->speed == USB_SPEED_HIGH; 365030a07e4SKarsten Wiese if ((cfg->sample_rate != 44100 && cfg->sample_rate != 48000 && 366030a07e4SKarsten Wiese (!high_speed || 367030a07e4SKarsten Wiese (cfg->sample_rate != 88200 && cfg->sample_rate != 96000))) || 368030a07e4SKarsten Wiese cfg->frame_size != 6 || 369030a07e4SKarsten Wiese cfg->period_frames > 0x3000) { 370030a07e4SKarsten Wiese err = -EINVAL; 371030a07e4SKarsten Wiese goto free; 372030a07e4SKarsten Wiese } 373030a07e4SKarsten Wiese switch (cfg->sample_rate) { 374030a07e4SKarsten Wiese case 44100: 375030a07e4SKarsten Wiese min_period_frames = 48; 376030a07e4SKarsten Wiese break; 377030a07e4SKarsten Wiese case 48000: 378030a07e4SKarsten Wiese min_period_frames = 52; 379030a07e4SKarsten Wiese break; 380030a07e4SKarsten Wiese default: 381030a07e4SKarsten Wiese min_period_frames = 104; 382030a07e4SKarsten Wiese break; 383030a07e4SKarsten Wiese } 384030a07e4SKarsten Wiese if (!high_speed) 385030a07e4SKarsten Wiese min_period_frames <<= 1; 386030a07e4SKarsten Wiese if (cfg->period_frames < min_period_frames) { 387030a07e4SKarsten Wiese err = -EINVAL; 388030a07e4SKarsten Wiese goto free; 389030a07e4SKarsten Wiese } 390030a07e4SKarsten Wiese 391030a07e4SKarsten Wiese snd_power_wait(hw->card, SNDRV_CTL_POWER_D0); 392030a07e4SKarsten Wiese 393030a07e4SKarsten Wiese mutex_lock(&us122l->mutex); 394030a07e4SKarsten Wiese if (!us122l->master) 395030a07e4SKarsten Wiese us122l->master = file; 396030a07e4SKarsten Wiese else if (us122l->master != file) { 397030a07e4SKarsten Wiese if (memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg))) { 398030a07e4SKarsten Wiese err = -EIO; 399030a07e4SKarsten Wiese goto unlock; 400030a07e4SKarsten Wiese } 401030a07e4SKarsten Wiese us122l->slave = file; 402030a07e4SKarsten Wiese } 403030a07e4SKarsten Wiese if (!us122l->sk.s || 404030a07e4SKarsten Wiese memcmp(cfg, &us122l->sk.s->cfg, sizeof(*cfg)) || 405030a07e4SKarsten Wiese us122l->sk.s->state == usb_stream_xrun) { 406030a07e4SKarsten Wiese us122l_stop(us122l); 407030a07e4SKarsten Wiese if (!us122l_start(us122l, cfg->sample_rate, cfg->period_frames)) 408030a07e4SKarsten Wiese err = -EIO; 409030a07e4SKarsten Wiese else 410030a07e4SKarsten Wiese err = 1; 411030a07e4SKarsten Wiese } 412030a07e4SKarsten Wiese unlock: 413030a07e4SKarsten Wiese mutex_unlock(&us122l->mutex); 414030a07e4SKarsten Wiese free: 415030a07e4SKarsten Wiese kfree(cfg); 416030a07e4SKarsten Wiese return err; 417030a07e4SKarsten Wiese } 418030a07e4SKarsten Wiese 419030a07e4SKarsten Wiese #define SND_USB_STREAM_ID "USB STREAM" 420030a07e4SKarsten Wiese static int usb_stream_hwdep_new(struct snd_card *card) 421030a07e4SKarsten Wiese { 422030a07e4SKarsten Wiese int err; 423030a07e4SKarsten Wiese struct snd_hwdep *hw; 424030a07e4SKarsten Wiese struct usb_device *dev = US122L(card)->chip.dev; 425030a07e4SKarsten Wiese 426030a07e4SKarsten Wiese err = snd_hwdep_new(card, SND_USB_STREAM_ID, 0, &hw); 427030a07e4SKarsten Wiese if (err < 0) 428030a07e4SKarsten Wiese return err; 429030a07e4SKarsten Wiese 430030a07e4SKarsten Wiese hw->iface = SNDRV_HWDEP_IFACE_USB_STREAM; 431030a07e4SKarsten Wiese hw->private_data = US122L(card); 432030a07e4SKarsten Wiese hw->ops.open = usb_stream_hwdep_open; 433030a07e4SKarsten Wiese hw->ops.release = usb_stream_hwdep_release; 434030a07e4SKarsten Wiese hw->ops.ioctl = usb_stream_hwdep_ioctl; 435030a07e4SKarsten Wiese hw->ops.ioctl_compat = usb_stream_hwdep_ioctl; 436030a07e4SKarsten Wiese hw->ops.mmap = usb_stream_hwdep_mmap; 437030a07e4SKarsten Wiese hw->ops.poll = usb_stream_hwdep_poll; 438030a07e4SKarsten Wiese 439030a07e4SKarsten Wiese sprintf(hw->name, "/proc/bus/usb/%03d/%03d/hwdeppcm", 440030a07e4SKarsten Wiese dev->bus->busnum, dev->devnum); 441030a07e4SKarsten Wiese return 0; 442030a07e4SKarsten Wiese } 443030a07e4SKarsten Wiese 444030a07e4SKarsten Wiese 445030a07e4SKarsten Wiese static bool us122l_create_card(struct snd_card *card) 446030a07e4SKarsten Wiese { 447030a07e4SKarsten Wiese int err; 448030a07e4SKarsten Wiese struct us122l *us122l = US122L(card); 449030a07e4SKarsten Wiese 450030a07e4SKarsten Wiese err = usb_set_interface(us122l->chip.dev, 1, 1); 451030a07e4SKarsten Wiese if (err) { 452030a07e4SKarsten Wiese snd_printk(KERN_ERR "usb_set_interface error \n"); 453030a07e4SKarsten Wiese return false; 454030a07e4SKarsten Wiese } 455030a07e4SKarsten Wiese 456030a07e4SKarsten Wiese pt_info_set(us122l->chip.dev, 0x11); 457030a07e4SKarsten Wiese pt_info_set(us122l->chip.dev, 0x10); 458030a07e4SKarsten Wiese 459030a07e4SKarsten Wiese if (!us122l_start(us122l, 44100, 256)) 460030a07e4SKarsten Wiese return false; 461030a07e4SKarsten Wiese 462030a07e4SKarsten Wiese err = us122l_create_usbmidi(card); 463030a07e4SKarsten Wiese if (err < 0) { 464030a07e4SKarsten Wiese snd_printk(KERN_ERR "us122l_create_usbmidi error %i \n", err); 465030a07e4SKarsten Wiese us122l_stop(us122l); 466030a07e4SKarsten Wiese return false; 467030a07e4SKarsten Wiese } 468030a07e4SKarsten Wiese err = usb_stream_hwdep_new(card); 469030a07e4SKarsten Wiese if (err < 0) { 470030a07e4SKarsten Wiese /* release the midi resources */ 471030a07e4SKarsten Wiese struct list_head *p; 472030a07e4SKarsten Wiese list_for_each(p, &us122l->chip.midi_list) 473030a07e4SKarsten Wiese snd_usbmidi_disconnect(p); 474030a07e4SKarsten Wiese 475030a07e4SKarsten Wiese us122l_stop(us122l); 476030a07e4SKarsten Wiese return false; 477030a07e4SKarsten Wiese } 478030a07e4SKarsten Wiese return true; 479030a07e4SKarsten Wiese } 480030a07e4SKarsten Wiese 48151721f70STakashi Iwai static int usx2y_create_card(struct usb_device *device, struct snd_card **cardp) 482030a07e4SKarsten Wiese { 483030a07e4SKarsten Wiese int dev; 484030a07e4SKarsten Wiese struct snd_card *card; 485bd7dd77cSTakashi Iwai int err; 486bd7dd77cSTakashi Iwai 487030a07e4SKarsten Wiese for (dev = 0; dev < SNDRV_CARDS; ++dev) 488030a07e4SKarsten Wiese if (enable[dev] && !snd_us122l_card_used[dev]) 489030a07e4SKarsten Wiese break; 490030a07e4SKarsten Wiese if (dev >= SNDRV_CARDS) 49151721f70STakashi Iwai return -ENODEV; 492bd7dd77cSTakashi Iwai err = snd_card_create(index[dev], id[dev], THIS_MODULE, 493bd7dd77cSTakashi Iwai sizeof(struct us122l), &card); 494bd7dd77cSTakashi Iwai if (err < 0) 49551721f70STakashi Iwai return err; 496030a07e4SKarsten Wiese snd_us122l_card_used[US122L(card)->chip.index = dev] = 1; 497030a07e4SKarsten Wiese 498030a07e4SKarsten Wiese US122L(card)->chip.dev = device; 499030a07e4SKarsten Wiese US122L(card)->chip.card = card; 500030a07e4SKarsten Wiese mutex_init(&US122L(card)->mutex); 501030a07e4SKarsten Wiese init_waitqueue_head(&US122L(card)->sk.sleep); 502030a07e4SKarsten Wiese INIT_LIST_HEAD(&US122L(card)->chip.midi_list); 503030a07e4SKarsten Wiese strcpy(card->driver, "USB "NAME_ALLCAPS""); 504030a07e4SKarsten Wiese sprintf(card->shortname, "TASCAM "NAME_ALLCAPS""); 505030a07e4SKarsten Wiese sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)", 506030a07e4SKarsten Wiese card->shortname, 507030a07e4SKarsten Wiese le16_to_cpu(device->descriptor.idVendor), 508030a07e4SKarsten Wiese le16_to_cpu(device->descriptor.idProduct), 509030a07e4SKarsten Wiese 0, 510030a07e4SKarsten Wiese US122L(card)->chip.dev->bus->busnum, 511030a07e4SKarsten Wiese US122L(card)->chip.dev->devnum 512030a07e4SKarsten Wiese ); 513030a07e4SKarsten Wiese snd_card_set_dev(card, &device->dev); 51451721f70STakashi Iwai *cardp = card; 51551721f70STakashi Iwai return 0; 516030a07e4SKarsten Wiese } 517030a07e4SKarsten Wiese 51851721f70STakashi Iwai static int us122l_usb_probe(struct usb_interface *intf, 51951721f70STakashi Iwai const struct usb_device_id *device_id, 52051721f70STakashi Iwai struct snd_card **cardp) 521030a07e4SKarsten Wiese { 522030a07e4SKarsten Wiese struct usb_device *device = interface_to_usbdev(intf); 52351721f70STakashi Iwai struct snd_card *card; 52451721f70STakashi Iwai int err; 525030a07e4SKarsten Wiese 52651721f70STakashi Iwai err = usx2y_create_card(device, &card); 52751721f70STakashi Iwai if (err < 0) 52851721f70STakashi Iwai return err; 529030a07e4SKarsten Wiese 53051721f70STakashi Iwai if (!us122l_create_card(card)) { 531030a07e4SKarsten Wiese snd_card_free(card); 53251721f70STakashi Iwai return -EINVAL; 53351721f70STakashi Iwai } 53451721f70STakashi Iwai 53551721f70STakashi Iwai err = snd_card_register(card); 53651721f70STakashi Iwai if (err < 0) { 53751721f70STakashi Iwai snd_card_free(card); 53851721f70STakashi Iwai return err; 539030a07e4SKarsten Wiese } 540030a07e4SKarsten Wiese 541030a07e4SKarsten Wiese usb_get_dev(device); 54251721f70STakashi Iwai *cardp = card; 54351721f70STakashi Iwai return 0; 544030a07e4SKarsten Wiese } 545030a07e4SKarsten Wiese 546030a07e4SKarsten Wiese static int snd_us122l_probe(struct usb_interface *intf, 547030a07e4SKarsten Wiese const struct usb_device_id *id) 548030a07e4SKarsten Wiese { 549030a07e4SKarsten Wiese struct snd_card *card; 55051721f70STakashi Iwai int err; 55151721f70STakashi Iwai 552030a07e4SKarsten Wiese snd_printdd(KERN_DEBUG"%p:%i\n", 553030a07e4SKarsten Wiese intf, intf->cur_altsetting->desc.bInterfaceNumber); 554030a07e4SKarsten Wiese if (intf->cur_altsetting->desc.bInterfaceNumber != 1) 555030a07e4SKarsten Wiese return 0; 556030a07e4SKarsten Wiese 55751721f70STakashi Iwai err = us122l_usb_probe(usb_get_intf(intf), id, &card); 55851721f70STakashi Iwai if (err < 0) { 55951721f70STakashi Iwai usb_put_intf(intf); 56051721f70STakashi Iwai return err; 561030a07e4SKarsten Wiese } 562030a07e4SKarsten Wiese 56351721f70STakashi Iwai usb_set_intfdata(intf, card); 56451721f70STakashi Iwai return 0; 565030a07e4SKarsten Wiese } 566030a07e4SKarsten Wiese 567030a07e4SKarsten Wiese static void snd_us122l_disconnect(struct usb_interface *intf) 568030a07e4SKarsten Wiese { 569030a07e4SKarsten Wiese struct snd_card *card; 570030a07e4SKarsten Wiese struct us122l *us122l; 571030a07e4SKarsten Wiese struct list_head *p; 572030a07e4SKarsten Wiese 573030a07e4SKarsten Wiese card = usb_get_intfdata(intf); 574030a07e4SKarsten Wiese if (!card) 575030a07e4SKarsten Wiese return; 576030a07e4SKarsten Wiese 577030a07e4SKarsten Wiese snd_card_disconnect(card); 578030a07e4SKarsten Wiese 579030a07e4SKarsten Wiese us122l = US122L(card); 580030a07e4SKarsten Wiese mutex_lock(&us122l->mutex); 581030a07e4SKarsten Wiese us122l_stop(us122l); 582030a07e4SKarsten Wiese mutex_unlock(&us122l->mutex); 583030a07e4SKarsten Wiese us122l->chip.shutdown = 1; 584030a07e4SKarsten Wiese 585030a07e4SKarsten Wiese /* release the midi resources */ 586030a07e4SKarsten Wiese list_for_each(p, &us122l->chip.midi_list) { 587030a07e4SKarsten Wiese snd_usbmidi_disconnect(p); 588030a07e4SKarsten Wiese } 589030a07e4SKarsten Wiese 590030a07e4SKarsten Wiese usb_put_intf(intf); 591030a07e4SKarsten Wiese usb_put_dev(US122L(card)->chip.dev); 592030a07e4SKarsten Wiese 593030a07e4SKarsten Wiese while (atomic_read(&us122l->mmap_count)) 594030a07e4SKarsten Wiese msleep(500); 595030a07e4SKarsten Wiese 596030a07e4SKarsten Wiese snd_card_free(card); 597030a07e4SKarsten Wiese } 598030a07e4SKarsten Wiese 599030a07e4SKarsten Wiese static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message) 600030a07e4SKarsten Wiese { 601030a07e4SKarsten Wiese struct snd_card *card; 602030a07e4SKarsten Wiese struct us122l *us122l; 603030a07e4SKarsten Wiese struct list_head *p; 604030a07e4SKarsten Wiese 605f4e9749fSJulia Lawall card = usb_get_intfdata(intf); 606030a07e4SKarsten Wiese if (!card) 607030a07e4SKarsten Wiese return 0; 608030a07e4SKarsten Wiese snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 609030a07e4SKarsten Wiese 610030a07e4SKarsten Wiese us122l = US122L(card); 611030a07e4SKarsten Wiese if (!us122l) 612030a07e4SKarsten Wiese return 0; 613030a07e4SKarsten Wiese 614030a07e4SKarsten Wiese list_for_each(p, &us122l->chip.midi_list) 615030a07e4SKarsten Wiese snd_usbmidi_input_stop(p); 616030a07e4SKarsten Wiese 617030a07e4SKarsten Wiese mutex_lock(&us122l->mutex); 618030a07e4SKarsten Wiese usb_stream_stop(&us122l->sk); 619030a07e4SKarsten Wiese mutex_unlock(&us122l->mutex); 620030a07e4SKarsten Wiese 621030a07e4SKarsten Wiese return 0; 622030a07e4SKarsten Wiese } 623030a07e4SKarsten Wiese 624030a07e4SKarsten Wiese static int snd_us122l_resume(struct usb_interface *intf) 625030a07e4SKarsten Wiese { 626030a07e4SKarsten Wiese struct snd_card *card; 627030a07e4SKarsten Wiese struct us122l *us122l; 628030a07e4SKarsten Wiese struct list_head *p; 629030a07e4SKarsten Wiese int err; 630030a07e4SKarsten Wiese 631f4e9749fSJulia Lawall card = usb_get_intfdata(intf); 632030a07e4SKarsten Wiese if (!card) 633030a07e4SKarsten Wiese return 0; 634030a07e4SKarsten Wiese 635030a07e4SKarsten Wiese us122l = US122L(card); 636030a07e4SKarsten Wiese if (!us122l) 637030a07e4SKarsten Wiese return 0; 638030a07e4SKarsten Wiese 639030a07e4SKarsten Wiese mutex_lock(&us122l->mutex); 640030a07e4SKarsten Wiese /* needed, doesn't restart without: */ 641030a07e4SKarsten Wiese err = usb_set_interface(us122l->chip.dev, 1, 1); 642030a07e4SKarsten Wiese if (err) { 643030a07e4SKarsten Wiese snd_printk(KERN_ERR "usb_set_interface error \n"); 644030a07e4SKarsten Wiese goto unlock; 645030a07e4SKarsten Wiese } 646030a07e4SKarsten Wiese 647030a07e4SKarsten Wiese pt_info_set(us122l->chip.dev, 0x11); 648030a07e4SKarsten Wiese pt_info_set(us122l->chip.dev, 0x10); 649030a07e4SKarsten Wiese 650030a07e4SKarsten Wiese err = us122l_set_sample_rate(us122l->chip.dev, 651030a07e4SKarsten Wiese us122l->sk.s->cfg.sample_rate); 652030a07e4SKarsten Wiese if (err < 0) { 653030a07e4SKarsten Wiese snd_printk(KERN_ERR "us122l_set_sample_rate error \n"); 654030a07e4SKarsten Wiese goto unlock; 655030a07e4SKarsten Wiese } 656030a07e4SKarsten Wiese err = usb_stream_start(&us122l->sk); 657030a07e4SKarsten Wiese if (err) 658030a07e4SKarsten Wiese goto unlock; 659030a07e4SKarsten Wiese 660030a07e4SKarsten Wiese list_for_each(p, &us122l->chip.midi_list) 661030a07e4SKarsten Wiese snd_usbmidi_input_start(p); 662030a07e4SKarsten Wiese unlock: 663030a07e4SKarsten Wiese mutex_unlock(&us122l->mutex); 664030a07e4SKarsten Wiese snd_power_change_state(card, SNDRV_CTL_POWER_D0); 665030a07e4SKarsten Wiese return err; 666030a07e4SKarsten Wiese } 667030a07e4SKarsten Wiese 668030a07e4SKarsten Wiese static struct usb_device_id snd_us122l_usb_id_table[] = { 669030a07e4SKarsten Wiese { 670030a07e4SKarsten Wiese .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 671030a07e4SKarsten Wiese .idVendor = 0x0644, 672030a07e4SKarsten Wiese .idProduct = USB_ID_US122L 673030a07e4SKarsten Wiese }, 674030a07e4SKarsten Wiese /* { */ /* US-144 maybe works when @USB1.1. Untested. */ 675030a07e4SKarsten Wiese /* .match_flags = USB_DEVICE_ID_MATCH_DEVICE, */ 676030a07e4SKarsten Wiese /* .idVendor = 0x0644, */ 677030a07e4SKarsten Wiese /* .idProduct = USB_ID_US144 */ 678030a07e4SKarsten Wiese /* }, */ 679030a07e4SKarsten Wiese { /* terminator */ } 680030a07e4SKarsten Wiese }; 681030a07e4SKarsten Wiese 682030a07e4SKarsten Wiese MODULE_DEVICE_TABLE(usb, snd_us122l_usb_id_table); 683030a07e4SKarsten Wiese static struct usb_driver snd_us122l_usb_driver = { 684030a07e4SKarsten Wiese .name = "snd-usb-us122l", 685030a07e4SKarsten Wiese .probe = snd_us122l_probe, 686030a07e4SKarsten Wiese .disconnect = snd_us122l_disconnect, 687030a07e4SKarsten Wiese .suspend = snd_us122l_suspend, 688030a07e4SKarsten Wiese .resume = snd_us122l_resume, 689030a07e4SKarsten Wiese .reset_resume = snd_us122l_resume, 690030a07e4SKarsten Wiese .id_table = snd_us122l_usb_id_table, 691030a07e4SKarsten Wiese .supports_autosuspend = 1 692030a07e4SKarsten Wiese }; 693030a07e4SKarsten Wiese 694030a07e4SKarsten Wiese 695030a07e4SKarsten Wiese static int __init snd_us122l_module_init(void) 696030a07e4SKarsten Wiese { 697030a07e4SKarsten Wiese return usb_register(&snd_us122l_usb_driver); 698030a07e4SKarsten Wiese } 699030a07e4SKarsten Wiese 700030a07e4SKarsten Wiese static void __exit snd_us122l_module_exit(void) 701030a07e4SKarsten Wiese { 702030a07e4SKarsten Wiese usb_deregister(&snd_us122l_usb_driver); 703030a07e4SKarsten Wiese } 704030a07e4SKarsten Wiese 705030a07e4SKarsten Wiese module_init(snd_us122l_module_init) 706030a07e4SKarsten Wiese module_exit(snd_us122l_module_exit) 707