diff options
Diffstat (limited to 'sound/usb/usx2y')
-rw-r--r-- | sound/usb/usx2y/Makefile | 3 | ||||
-rw-r--r-- | sound/usb/usx2y/usX2Yhwdep.c | 280 | ||||
-rw-r--r-- | sound/usb/usx2y/usX2Yhwdep.h | 6 | ||||
-rw-r--r-- | sound/usb/usx2y/usbus428ctldefs.h | 108 | ||||
-rw-r--r-- | sound/usb/usx2y/usbusx2y.c | 461 | ||||
-rw-r--r-- | sound/usb/usx2y/usbusx2y.h | 84 | ||||
-rw-r--r-- | sound/usb/usx2y/usbusx2yaudio.c | 1026 | ||||
-rw-r--r-- | sound/usb/usx2y/usx2y.h | 51 | ||||
-rw-r--r-- | sound/usb/usx2y/usx2yhwdeppcm.c | 807 | ||||
-rw-r--r-- | sound/usb/usx2y/usx2yhwdeppcm.h | 21 |
10 files changed, 2847 insertions, 0 deletions
diff --git a/sound/usb/usx2y/Makefile b/sound/usb/usx2y/Makefile new file mode 100644 index 000000000000..9ac22bce1124 --- /dev/null +++ b/sound/usb/usx2y/Makefile @@ -0,0 +1,3 @@ +snd-usb-usx2y-objs := usbusx2y.o usX2Yhwdep.o usx2yhwdeppcm.o + +obj-$(CONFIG_SND_USB_USX2Y) += snd-usb-usx2y.o diff --git a/sound/usb/usx2y/usX2Yhwdep.c b/sound/usb/usx2y/usX2Yhwdep.c new file mode 100644 index 000000000000..bef9b0c142c4 --- /dev/null +++ b/sound/usb/usx2y/usX2Yhwdep.c @@ -0,0 +1,280 @@ +/* + * Driver for Tascam US-X2Y USB soundcards + * + * FPGA Loader + ALSA Startup + * + * Copyright (c) 2003 by Karsten Wiese <annabellesgarden@yahoo.de> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <sound/driver.h> +#include <linux/interrupt.h> +#include <linux/usb.h> +#include <sound/core.h> +#include <sound/memalloc.h> +#include <sound/pcm.h> +#include <sound/hwdep.h> +#include "usx2y.h" +#include "usbusx2y.h" +#include "usX2Yhwdep.h" + +int usX2Y_hwdep_pcm_new(snd_card_t* card); + + +static struct page * snd_us428ctls_vm_nopage(struct vm_area_struct *area, unsigned long address, int *type) +{ + unsigned long offset; + struct page * page; + void *vaddr; + + snd_printdd("ENTER, start %lXh, ofs %lXh, pgoff %ld, addr %lXh\n", + area->vm_start, + address - area->vm_start, + (address - area->vm_start) >> PAGE_SHIFT, + address); + + offset = area->vm_pgoff << PAGE_SHIFT; + offset += address - area->vm_start; + snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM); + vaddr = (char*)((usX2Ydev_t*)area->vm_private_data)->us428ctls_sharedmem + offset; + page = virt_to_page(vaddr); + get_page(page); + snd_printdd( "vaddr=%p made us428ctls_vm_nopage() return %p; offset=%lX\n", vaddr, page, offset); + + if (type) + *type = VM_FAULT_MINOR; + + return page; +} + +static struct vm_operations_struct us428ctls_vm_ops = { + .nopage = snd_us428ctls_vm_nopage, +}; + +static int snd_us428ctls_mmap(snd_hwdep_t * hw, struct file *filp, struct vm_area_struct *area) +{ + unsigned long size = (unsigned long)(area->vm_end - area->vm_start); + usX2Ydev_t *us428 = (usX2Ydev_t*)hw->private_data; + + // FIXME this hwdep interface is used twice: fpga download and mmap for controlling Lights etc. Maybe better using 2 hwdep devs? + // so as long as the device isn't fully initialised yet we return -EBUSY here. + if (!(((usX2Ydev_t*)hw->private_data)->chip_status & USX2Y_STAT_CHIP_INIT)) + return -EBUSY; + + /* if userspace tries to mmap beyond end of our buffer, fail */ + if (size > ((PAGE_SIZE - 1 + sizeof(us428ctls_sharedmem_t)) / PAGE_SIZE) * PAGE_SIZE) { + snd_printd( "%lu > %lu\n", size, (unsigned long)sizeof(us428ctls_sharedmem_t)); + return -EINVAL; + } + + if (!us428->us428ctls_sharedmem) { + init_waitqueue_head(&us428->us428ctls_wait_queue_head); + if(!(us428->us428ctls_sharedmem = snd_malloc_pages(sizeof(us428ctls_sharedmem_t), GFP_KERNEL))) + return -ENOMEM; + memset(us428->us428ctls_sharedmem, -1, sizeof(us428ctls_sharedmem_t)); + us428->us428ctls_sharedmem->CtlSnapShotLast = -2; + } + area->vm_ops = &us428ctls_vm_ops; + area->vm_flags |= VM_RESERVED; + area->vm_private_data = hw->private_data; + return 0; +} + +static unsigned int snd_us428ctls_poll(snd_hwdep_t *hw, struct file *file, poll_table *wait) +{ + unsigned int mask = 0; + usX2Ydev_t *us428 = (usX2Ydev_t*)hw->private_data; + us428ctls_sharedmem_t *shm = us428->us428ctls_sharedmem; + if (us428->chip_status & USX2Y_STAT_CHIP_HUP) + return POLLHUP; + + poll_wait(file, &us428->us428ctls_wait_queue_head, wait); + + if (shm != NULL && shm->CtlSnapShotLast != shm->CtlSnapShotRed) + mask |= POLLIN; + + return mask; +} + + +static int snd_usX2Y_hwdep_open(snd_hwdep_t *hw, struct file *file) +{ + return 0; +} + +static int snd_usX2Y_hwdep_release(snd_hwdep_t *hw, struct file *file) +{ + return 0; +} + +static int snd_usX2Y_hwdep_dsp_status(snd_hwdep_t *hw, snd_hwdep_dsp_status_t *info) +{ + static char *type_ids[USX2Y_TYPE_NUMS] = { + [USX2Y_TYPE_122] = "us122", + [USX2Y_TYPE_224] = "us224", + [USX2Y_TYPE_428] = "us428", + }; + int id = -1; + + switch (le16_to_cpu(((usX2Ydev_t*)hw->private_data)->chip.dev->descriptor.idProduct)) { + case USB_ID_US122: + id = USX2Y_TYPE_122; + break; + case USB_ID_US224: + id = USX2Y_TYPE_224; + break; + case USB_ID_US428: + id = USX2Y_TYPE_428; + break; + } + if (0 > id) + return -ENODEV; + strcpy(info->id, type_ids[id]); + info->num_dsps = 2; // 0: Prepad Data, 1: FPGA Code + if (((usX2Ydev_t*)hw->private_data)->chip_status & USX2Y_STAT_CHIP_INIT) + info->chip_ready = 1; + info->version = USX2Y_DRIVER_VERSION; + return 0; +} + + +static int usX2Y_create_usbmidi(snd_card_t* card ) +{ + static snd_usb_midi_endpoint_info_t quirk_data_1 = { + .out_ep =0x06, + .in_ep = 0x06, + .out_cables = 0x001, + .in_cables = 0x001 + }; + static snd_usb_audio_quirk_t quirk_1 = { + .vendor_name = "TASCAM", + .product_name = NAME_ALLCAPS, + .ifnum = 0, + .type = QUIRK_MIDI_FIXED_ENDPOINT, + .data = &quirk_data_1 + }; + static snd_usb_midi_endpoint_info_t quirk_data_2 = { + .out_ep =0x06, + .in_ep = 0x06, + .out_cables = 0x003, + .in_cables = 0x003 + }; + static snd_usb_audio_quirk_t quirk_2 = { + .vendor_name = "TASCAM", + .product_name = "US428", + .ifnum = 0, + .type = QUIRK_MIDI_FIXED_ENDPOINT, + .data = &quirk_data_2 + }; + struct usb_device *dev = usX2Y(card)->chip.dev; + struct usb_interface *iface = usb_ifnum_to_if(dev, 0); + snd_usb_audio_quirk_t *quirk = le16_to_cpu(dev->descriptor.idProduct) == USB_ID_US428 ? &quirk_2 : &quirk_1; + + snd_printdd("usX2Y_create_usbmidi \n"); + return snd_usb_create_midi_interface(&usX2Y(card)->chip, iface, quirk); +} + +static int usX2Y_create_alsa_devices(snd_card_t* card) +{ + int err; + + do { + if ((err = usX2Y_create_usbmidi(card)) < 0) { + snd_printk("usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err); + break; + } + if ((err = usX2Y_audio_create(card)) < 0) + break; + if ((err = usX2Y_hwdep_pcm_new(card)) < 0) + break; + if ((err = snd_card_register(card)) < 0) + break; + } while (0); + + return err; +} + +static int snd_usX2Y_hwdep_dsp_load(snd_hwdep_t *hw, snd_hwdep_dsp_image_t *dsp) +{ + usX2Ydev_t *priv = hw->private_data; + int lret, err = -EINVAL; + snd_printdd( "dsp_load %s\n", dsp->name); + + if (access_ok(VERIFY_READ, dsp->image, dsp->length)) { + struct usb_device* dev = priv->chip.dev; + char *buf = kmalloc(dsp->length, GFP_KERNEL); + if (!buf) + return -ENOMEM; + if (copy_from_user(buf, dsp->image, dsp->length)) { + kfree(buf); + return -EFAULT; + } + err = usb_set_interface(dev, 0, 1); + if (err) + snd_printk("usb_set_interface error \n"); + else + err = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 2), buf, dsp->length, &lret, 6000); + kfree(buf); + } + if (err) + return err; + if (dsp->index == 1) { + set_current_state(TASK_UNINTERRUPTIBLE); + schedule_timeout(HZ/4); // give the device some time + err = usX2Y_AsyncSeq04_init(priv); + if (err) { + snd_printk("usX2Y_AsyncSeq04_init error \n"); + return err; + } + err = usX2Y_In04_init(priv); + if (err) { + snd_printk("usX2Y_In04_init error \n"); + return err; + } + err = usX2Y_create_alsa_devices(hw->card); + if (err) { + snd_printk("usX2Y_create_alsa_devices error %i \n", err); + snd_card_free(hw->card); + return err; + } + priv->chip_status |= USX2Y_STAT_CHIP_INIT; + snd_printdd("%s: alsa all started\n", hw->name); + } + return err; +} + + +int usX2Y_hwdep_new(snd_card_t* card, struct usb_device* device) +{ + int err; + snd_hwdep_t *hw; + + if ((err = snd_hwdep_new(card, SND_USX2Y_LOADER_ID, 0, &hw)) < 0) + return err; + + hw->iface = SNDRV_HWDEP_IFACE_USX2Y; + hw->private_data = usX2Y(card); + hw->ops.open = snd_usX2Y_hwdep_open; + hw->ops.release = snd_usX2Y_hwdep_release; + hw->ops.dsp_status = snd_usX2Y_hwdep_dsp_status; + hw->ops.dsp_load = snd_usX2Y_hwdep_dsp_load; + hw->ops.mmap = snd_us428ctls_mmap; + hw->ops.poll = snd_us428ctls_poll; + hw->exclusive = 1; + sprintf(hw->name, "/proc/bus/usb/%03d/%03d", device->bus->busnum, device->devnum); + return 0; +} + diff --git a/sound/usb/usx2y/usX2Yhwdep.h b/sound/usb/usx2y/usX2Yhwdep.h new file mode 100644 index 000000000000..d612a26eb77c --- /dev/null +++ b/sound/usb/usx2y/usX2Yhwdep.h @@ -0,0 +1,6 @@ +#ifndef USX2YHWDEP_H +#define USX2YHWDEP_H + +int usX2Y_hwdep_new(snd_card_t* card, struct usb_device* device); + +#endif diff --git a/sound/usb/usx2y/usbus428ctldefs.h b/sound/usb/usx2y/usbus428ctldefs.h new file mode 100644 index 000000000000..6af16438d2c7 --- /dev/null +++ b/sound/usb/usx2y/usbus428ctldefs.h @@ -0,0 +1,108 @@ +/* + * + * Copyright (c) 2003 by Karsten Wiese <annabellesgarden@yahoo.de> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +enum E_In84{ + eFader0 = 0, + eFader1, + eFader2, + eFader3, + eFader4, + eFader5, + eFader6, + eFader7, + eFaderM, + eTransport, + eModifier = 10, + eFilterSelect, + eSelect, + eMute, + + eSwitch = 15, + eWheelGain, + eWheelFreq, + eWheelQ, + eWheelPan, + eWheel = 20 +}; + +#define T_RECORD 1 +#define T_PLAY 2 +#define T_STOP 4 +#define T_F_FWD 8 +#define T_REW 0x10 +#define T_SOLO 0x20 +#define T_REC 0x40 +#define T_NULL 0x80 + + +struct us428_ctls{ + unsigned char Fader[9]; + unsigned char Transport; + unsigned char Modifier; + unsigned char FilterSelect; + unsigned char Select; + unsigned char Mute; + unsigned char UNKNOWN; + unsigned char Switch; + unsigned char Wheel[5]; +}; + +typedef struct us428_ctls us428_ctls_t; + +typedef struct us428_setByte{ + unsigned char Offset, + Value; +}us428_setByte_t; + +enum { + eLT_Volume = 0, + eLT_Light +}; + +typedef struct usX2Y_volume { + unsigned char Channel, + LH, + LL, + RH, + RL; +} usX2Y_volume_t; + +struct us428_lights{ + us428_setByte_t Light[7]; +}; +typedef struct us428_lights us428_lights_t; + +typedef struct { + char type; + union { + usX2Y_volume_t vol; + us428_lights_t lights; + } val; +} us428_p4out_t; + +#define N_us428_ctl_BUFS 16 +#define N_us428_p4out_BUFS 16 +struct us428ctls_sharedmem{ + us428_ctls_t CtlSnapShot[N_us428_ctl_BUFS]; + int CtlSnapShotDiffersAt[N_us428_ctl_BUFS]; + int CtlSnapShotLast, CtlSnapShotRed; + us428_p4out_t p4out[N_us428_p4out_BUFS]; + int p4outLast, p4outSent; +}; +typedef struct us428ctls_sharedmem us428ctls_sharedmem_t; diff --git a/sound/usb/usx2y/usbusx2y.c b/sound/usb/usx2y/usbusx2y.c new file mode 100644 index 000000000000..b06a267e5dac --- /dev/null +++ b/sound/usb/usx2y/usbusx2y.c @@ -0,0 +1,461 @@ +/* + * usbusy2y.c - ALSA USB US-428 Driver + * +2004-12-14 Karsten Wiese + Version 0.8.7.1: + snd_pcm_open for rawusb pcm-devices now returns -EBUSY if called without rawusb's hwdep device being open. + +2004-12-02 Karsten Wiese + Version 0.8.7: + Use macro usb_maxpacket() for portability. + +2004-10-26 Karsten Wiese + Version 0.8.6: + wake_up() process waiting in usX2Y_urbs_start() on error. + +2004-10-21 Karsten Wiese + Version 0.8.5: + nrpacks is runtime or compiletime configurable now with tested values from 1 to 4. + +2004-10-03 Karsten Wiese + Version 0.8.2: + Avoid any possible racing while in prepare callback. + +2004-09-30 Karsten Wiese + Version 0.8.0: + Simplified things and made ohci work again. + +2004-09-20 Karsten Wiese + Version 0.7.3: + Use usb_kill_urb() instead of deprecated (kernel 2.6.9) usb_unlink_urb(). + +2004-07-13 Karsten Wiese + Version 0.7.1: + Don't sleep in START/STOP callbacks anymore. + us428 channels C/D not handled just for this version, sorry. + +2004-06-21 Karsten Wiese + Version 0.6.4: + Temporarely suspend midi input + to sanely call usb_set_interface() when setting format. + +2004-06-12 Karsten Wiese + Version 0.6.3: + Made it thus the following rule is enforced: + "All pcm substreams of one usX2Y have to operate at the same rate & format." + +2004-04-06 Karsten Wiese + Version 0.6.0: + Runs on 2.6.5 kernel without any "--with-debug=" things. + us224 reported running. + +2004-01-14 Karsten Wiese + Version 0.5.1: + Runs with 2.6.1 kernel. + +2003-12-30 Karsten Wiese + Version 0.4.1: + Fix 24Bit 4Channel capturing for the us428. + +2003-11-27 Karsten Wiese, Martin Langer + Version 0.4: + us122 support. + us224 could be tested by uncommenting the sections containing USB_ID_US224 + +2003-11-03 Karsten Wiese + Version 0.3: + 24Bit support. + "arecord -D hw:1 -c 2 -r 48000 -M -f S24_3LE|aplay -D hw:1 -c 2 -r 48000 -M -f S24_3LE" works. + +2003-08-22 Karsten Wiese + Version 0.0.8: + Removed EZUSB Firmware. First Stage Firmwaredownload is now done by tascam-firmware downloader. + See: + http://usb-midi-fw.sourceforge.net/tascam-firmware.tar.gz + +2003-06-18 Karsten Wiese + Version 0.0.5: + changed to compile with kernel 2.4.21 and alsa 0.9.4 + +2002-10-16 Karsten Wiese + Version 0.0.4: + compiles again with alsa-current. + USB_ISO_ASAP not used anymore (most of the time), instead + urb->start_frame is calculated here now, some calls inside usb-driver don't need to happen anymore. + + To get the best out of this: + Disable APM-support in the kernel as APM-BIOS calls (once each second) hard disable interrupt for many precious milliseconds. + This helped me much on my slowish PII 400 & PIII 500. + ACPI yet untested but might cause the same bad behaviour. + Use a kernel with lowlatency and preemptiv patches applied. + To autoload snd-usb-midi append a line + post-install snd-usb-us428 modprobe snd-usb-midi + to /etc/modules.conf. + + known problems: + sliders, knobs, lights not yet handled except MASTER Volume slider. + "pcm -c 2" doesn't work. "pcm -c 2 -m direct_interleaved" does. + KDE3: "Enable full duplex operation" deadlocks. + + +2002-08-31 Karsten Wiese + Version 0.0.3: audio also simplex; + simplifying: iso urbs only 1 packet, melted structs. + ASYNC_UNLINK not used anymore: no more crashes so far..... + for alsa 0.9 rc3. + +2002-08-09 Karsten Wiese + Version 0.0.2: midi works with snd-usb-midi, audio (only fullduplex now) with i.e. bristol. + The firmware has been sniffed from win2k us-428 driver 3.09. + + * Copyright (c) 2002 - 2004 Karsten Wiese + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include <sound/driver.h> +#include <linux/init.h> +#include <linux/module.h> +#include <linux/moduleparam.h> +#include <linux/interrupt.h> +#include <linux/usb.h> +#include <sound/core.h> +#include <sound/initval.h> +#include <sound/pcm.h> + +#include <sound/rawmidi.h> +#include "usx2y.h" +#include "usbusx2y.h" +#include "usX2Yhwdep.h" + + + +MODULE_AUTHOR("Karsten Wiese <annabellesgarden@yahoo.de>"); +MODULE_DESCRIPTION("TASCAM "NAME_ALLCAPS" Version 0.8.7.1"); +MODULE_LICENSE("GPL"); +MODULE_SUPPORTED_DEVICE("{{TASCAM(0x1604), "NAME_ALLCAPS"(0x8001)(0x8005)(0x8007) }}"); + +static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */ +static char* id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for this card */ +static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */ + +module_param_array(index, int, NULL, 0444); +MODULE_PARM_DESC(index, "Index value for "NAME_ALLCAPS"."); +module_param_array(id, charp, NULL, 0444); +MODULE_PARM_DESC(id, "ID string for "NAME_ALLCAPS"."); +module_param_array(enable, bool, NULL, 0444); +MODULE_PARM_DESC(enable, "Enable "NAME_ALLCAPS"."); + + +static int snd_usX2Y_card_used[SNDRV_CARDS]; + +static void usX2Y_usb_disconnect(struct usb_device* usb_device, void* ptr); +static void snd_usX2Y_card_private_free(snd_card_t *card); + +/* + * pipe 4 is used for switching the lamps, setting samplerate, volumes .... + */ +static void i_usX2Y_Out04Int(struct urb* urb, struct pt_regs *regs) +{ +#ifdef CONFIG_SND_DEBUG + if (urb->status) { + int i; + usX2Ydev_t* usX2Y = urb->context; + for (i = 0; i < 10 && usX2Y->AS04.urb[i] != urb; i++); + snd_printdd("i_usX2Y_Out04Int() urb %i status=%i\n", i, urb->status); + } +#endif +} + +static void i_usX2Y_In04Int(struct urb* urb, struct pt_regs *regs) +{ + int err = 0; + usX2Ydev_t *usX2Y = urb->context; + us428ctls_sharedmem_t *us428ctls = usX2Y->us428ctls_sharedmem; + + usX2Y->In04IntCalls++; + + if (urb->status) { + snd_printdd("Interrupt Pipe 4 came back with status=%i\n", urb->status); + return; + } + + // printk("%i:0x%02X ", 8, (int)((unsigned char*)usX2Y->In04Buf)[8]); Master volume shows 0 here if fader is at max during boot ?!? + if (us428ctls) { + int diff = -1; + if (-2 == us428ctls->CtlSnapShotLast) { + diff = 0; + memcpy(usX2Y->In04Last, usX2Y->In04Buf, sizeof(usX2Y->In04Last)); + us428ctls->CtlSnapShotLast = -1; + } else { + int i; + for (i = 0; i < 21; i++) { + if (usX2Y->In04Last[i] != ((char*)usX2Y->In04Buf)[i]) { + if (diff < 0) + diff = i; + usX2Y->In04Last[i] = ((char*)usX2Y->In04Buf)[i]; + } + } + } + if (0 <= diff) { + int n = us428ctls->CtlSnapShotLast + 1; + if (n >= N_us428_ctl_BUFS || n < 0) + n = 0; + memcpy(us428ctls->CtlSnapShot + n, usX2Y->In04Buf, sizeof(us428ctls->CtlSnapShot[0])); + us428ctls->CtlSnapShotDiffersAt[n] = diff; + us428ctls->CtlSnapShotLast = n; + wake_up(&usX2Y->us428ctls_wait_queue_head); + } + } + + + if (usX2Y->US04) { + if (0 == usX2Y->US04->submitted) + do + err = usb_submit_urb(usX2Y->US04->urb[usX2Y->US04->submitted++], GFP_ATOMIC); + while (!err && usX2Y->US04->submitted < usX2Y->US04->len); + } else + if (us428ctls && us428ctls->p4outLast >= 0 && us428ctls->p4outLast < N_us428_p4out_BUFS) { + if (us428ctls->p4outLast != us428ctls->p4outSent) { + int j, send = us428ctls->p4outSent + 1; + if (send >= N_us428_p4out_BUFS) + send = 0; + for (j = 0; j < URBS_AsyncSeq && !err; ++j) + if (0 == usX2Y->AS04.urb[j]->status) { + us428_p4out_t *p4out = us428ctls->p4out + send; // FIXME if more then 1 p4out is new, 1 gets lost. + usb_fill_bulk_urb(usX2Y->AS04.urb[j], usX2Y->chip.dev, + usb_sndbulkpipe(usX2Y->chip.dev, 0x04), &p4out->val.vol, + p4out->type == eLT_Light ? sizeof(us428_lights_t) : 5, + i_usX2Y_Out04Int, usX2Y); + err = usb_submit_urb(usX2Y->AS04.urb[j], GFP_ATOMIC); + us428ctls->p4outSent = send; + break; + } + } + } + + if (err) { + snd_printk("In04Int() usb_submit_urb err=%i\n", err); + } + + urb->dev = usX2Y->chip.dev; + usb_submit_urb(urb, GFP_ATOMIC); +} + +/* + * Prepare some urbs + */ +int usX2Y_AsyncSeq04_init(usX2Ydev_t* usX2Y) +{ + int err = 0, + i; + + if (NULL == (usX2Y->AS04.buffer = kmalloc(URB_DataLen_AsyncSeq*URBS_AsyncSeq, GFP_KERNEL))) { + err = -ENOMEM; + } else + for (i = 0; i < URBS_AsyncSeq; ++i) { + if (NULL == (usX2Y->AS04.urb[i] = usb_alloc_urb(0, GFP_KERNEL))) { + err = -ENOMEM; + break; + } + usb_fill_bulk_urb( usX2Y->AS04.urb[i], usX2Y->chip.dev, + usb_sndbulkpipe(usX2Y->chip.dev, 0x04), + usX2Y->AS04.buffer + URB_DataLen_AsyncSeq*i, 0, + i_usX2Y_Out04Int, usX2Y + ); + } + return err; +} + +int usX2Y_In04_init(usX2Ydev_t* usX2Y) +{ + int err = 0; + if (! (usX2Y->In04urb = usb_alloc_urb(0, GFP_KERNEL))) + return -ENOMEM; + + if (! (usX2Y->In04Buf = kmalloc(21, GFP_KERNEL))) { + usb_free_urb(usX2Y->In04urb); + return -ENOMEM; + } + + init_waitqueue_head(&usX2Y->In04WaitQueue); + usb_fill_int_urb(usX2Y->In04urb, usX2Y->chip.dev, usb_rcvintpipe(usX2Y->chip.dev, 0x4), + usX2Y->In04Buf, 21, + i_usX2Y_In04Int, usX2Y, + 10); + err = usb_submit_urb(usX2Y->In04urb, GFP_KERNEL); + return err; +} + +static void usX2Y_unlinkSeq(snd_usX2Y_AsyncSeq_t* S) +{ + int i; + for (i = 0; i < URBS_AsyncSeq; ++i) { + if (S[i].urb) { + usb_kill_urb(S->urb[i]); + usb_free_urb(S->urb[i]); + S->urb[i] = NULL; + } + } + kfree(S->buffer); +} + + +static struct usb_device_id snd_usX2Y_usb_id_table[] = { + { + .match_flags = USB_DEVICE_ID_MATCH_DEVICE, + .idVendor = 0x1604, + .idProduct = USB_ID_US428 + }, + { + .match_flags = USB_DEVICE_ID_MATCH_DEVICE, + .idVendor = 0x1604, + .idProduct = USB_ID_US122 + }, + { + .match_flags = USB_DEVICE_ID_MATCH_DEVICE, + .idVendor = 0x1604, + .idProduct = USB_ID_US224 + }, + { /* terminator */ } +}; + +static snd_card_t* usX2Y_create_card(struct usb_device* device) +{ + int dev; + snd_card_t* card; + for (dev = 0; dev < SNDRV_CARDS; ++dev) + if (enable[dev] && !snd_usX2Y_card_used[dev]) + break; + if (dev >= SNDRV_CARDS) + return NULL; + card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(usX2Ydev_t)); + if (!card) + return NULL; + snd_usX2Y_card_used[usX2Y(card)->chip.index = dev] = 1; + card->private_free = snd_usX2Y_card_private_free; + usX2Y(card)->chip.dev = device; + usX2Y(card)->chip.card = card; + init_waitqueue_head(&usX2Y(card)->prepare_wait_queue); + init_MUTEX (&usX2Y(card)->prepare_mutex); + INIT_LIST_HEAD(&usX2Y(card)->chip.midi_list); + strcpy(card->driver, "USB "NAME_ALLCAPS""); + sprintf(card->shortname, "TASCAM "NAME_ALLCAPS""); + sprintf(card->longname, "%s (%x:%x if %d at %03d/%03d)", + card->shortname, + le16_to_cpu(device->descriptor.idVendor), + le16_to_cpu(device->descriptor.idProduct), + 0,//us428(card)->usbmidi.ifnum, + usX2Y(card)->chip.dev->bus->busnum, usX2Y(card)->chip.dev->devnum + ); + snd_card_set_dev(card, &device->dev); + return card; +} + + +static void* usX2Y_usb_probe(struct usb_device* device, struct usb_interface *intf, const struct usb_device_id* device_id) +{ + int err; + snd_card_t* card; + if (le16_to_cpu(device->descriptor.idVendor) != 0x1604 || + (le16_to_cpu(device->descriptor.idProduct) != USB_ID_US122 && + le16_to_cpu(device->descriptor.idProduct) != USB_ID_US224 && + le16_to_cpu(device->descriptor.idProduct) != USB_ID_US428) || + !(card = usX2Y_create_card(device))) + return NULL; + if ((err = usX2Y_hwdep_new(card, device)) < 0 || + (err = snd_card_register(card)) < 0) { + snd_card_free(card); + return NULL; + } + return card; +} + +/* + * new 2.5 USB kernel API + */ +static int snd_usX2Y_probe(struct usb_interface *intf, const struct usb_device_id *id) +{ + void *chip; + chip = usX2Y_usb_probe(interface_to_usbdev(intf), intf, id); + if (chip) { + dev_set_drvdata(&intf->dev, chip); + return 0; + } else + return -EIO; +} + +static void snd_usX2Y_disconnect(struct usb_interface *intf) +{ + usX2Y_usb_disconnect(interface_to_usbdev(intf), + dev_get_drvdata(&intf->dev)); +} + +MODULE_DEVICE_TABLE(usb, snd_usX2Y_usb_id_table); +static struct usb_driver snd_usX2Y_usb_driver = { + .owner = THIS_MODULE, + .name = "snd-usb-usx2y", + .probe = snd_usX2Y_probe, + .disconnect = snd_usX2Y_disconnect, + .id_table = snd_usX2Y_usb_id_table, +}; + +static void snd_usX2Y_card_private_free(snd_card_t *card) +{ + kfree(usX2Y(card)->In04Buf); + usb_free_urb(usX2Y(card)->In04urb); + if (usX2Y(card)->us428ctls_sharedmem) + snd_free_pages(usX2Y(card)->us428ctls_sharedmem, sizeof(*usX2Y(card)->us428ctls_sharedmem)); + if (usX2Y(card)->chip.index >= 0 && usX2Y(card)->chip.index < SNDRV_CARDS) + snd_usX2Y_card_used[usX2Y(card)->chip.index] = 0; +} + +/* + * Frees the device. + */ +static void usX2Y_usb_disconnect(struct usb_device* device, void* ptr) +{ + if (ptr) { + usX2Ydev_t* usX2Y = usX2Y((snd_card_t*)ptr); + struct list_head* p; + if (usX2Y->chip_status == USX2Y_STAT_CHIP_HUP) // on 2.6.1 kernel snd_usbmidi_disconnect() + return; // calls us back. better leave :-) . + usX2Y->chip.shutdown = 1; + usX2Y->chip_status = USX2Y_STAT_CHIP_HUP; + usX2Y_unlinkSeq(&usX2Y->AS04); + usb_kill_urb(usX2Y->In04urb); + snd_card_disconnect((snd_card_t*)ptr); + /* release the midi resources */ + list_for_each(p, &usX2Y->chip.midi_list) { + snd_usbmidi_disconnect(p, &snd_usX2Y_usb_driver); + } + if (usX2Y->us428ctls_sharedmem) + wake_up(&usX2Y->us428ctls_wait_queue_head); + snd_card_free_in_thread((snd_card_t*)ptr); + } +} + +static int __init snd_usX2Y_module_init(void) +{ + return usb_register(&snd_usX2Y_usb_driver); +} + +static void __exit snd_usX2Y_module_exit(void) +{ + usb_deregister(&snd_usX2Y_usb_driver); +} + +module_init(snd_usX2Y_module_init) +module_exit(snd_usX2Y_module_exit) diff --git a/sound/usb/usx2y/usbusx2y.h b/sound/usb/usx2y/usbusx2y.h new file mode 100644 index 000000000000..f65f3a7194ca --- /dev/null +++ b/sound/usb/usx2y/usbusx2y.h @@ -0,0 +1,84 @@ +#ifndef USBUSX2Y_H +#define USBUSX2Y_H +#include "../usbaudio.h" +#include "usbus428ctldefs.h" + +#define NRURBS 2 + + +#define URBS_AsyncSeq 10 +#define URB_DataLen_AsyncSeq 32 +typedef struct { + struct urb* urb[URBS_AsyncSeq]; + char* buffer; +} snd_usX2Y_AsyncSeq_t; + +typedef struct { + int submitted; + int len; + struct urb* urb[0]; +} snd_usX2Y_urbSeq_t; + +typedef struct snd_usX2Y_substream snd_usX2Y_substream_t; +#include "usx2yhwdeppcm.h" + +typedef struct { + snd_usb_audio_t chip; + int stride; + struct urb *In04urb; + void *In04Buf; + char In04Last[24]; + unsigned In04IntCalls; + snd_usX2Y_urbSeq_t *US04; + wait_queue_head_t In04WaitQueue; + snd_usX2Y_AsyncSeq_t AS04; + unsigned int rate, + format; + int chip_status; + struct semaphore prepare_mutex; + us428ctls_sharedmem_t *us428ctls_sharedmem; + int wait_iso_frame; + wait_queue_head_t us428ctls_wait_queue_head; + snd_usX2Y_hwdep_pcm_shm_t *hwdep_pcm_shm; + snd_usX2Y_substream_t *subs[4]; + snd_usX2Y_substream_t * volatile prepare_subs; + wait_queue_head_t prepare_wait_queue; +} usX2Ydev_t; + + +struct snd_usX2Y_substream { + usX2Ydev_t *usX2Y; + snd_pcm_substream_t *pcm_substream; + + int endpoint; + unsigned int maxpacksize; /* max packet size in bytes */ + + atomic_t state; +#define state_STOPPED 0 +#define state_STARTING1 1 +#define state_STARTING2 2 +#define state_STARTING3 3 +#define state_PREPARED 4 +#define state_PRERUNNING 6 +#define state_RUNNING 8 + + int hwptr; /* free frame position in the buffer (only for playback) */ + int hwptr_done; /* processed frame position in the buffer */ + int transfer_done; /* processed frames since last period update */ + + struct urb *urb[NRURBS]; /* data urb table */ + struct urb *completed_urb; + char *tmpbuf; /* temporary buffer for playback */ +}; + + +#define usX2Y(c) ((usX2Ydev_t*)(c)->private_data) + +int usX2Y_audio_create(snd_card_t* card); + +int usX2Y_AsyncSeq04_init(usX2Ydev_t* usX2Y); +int usX2Y_In04_init(usX2Ydev_t* usX2Y); + +#define NAME_ALLCAPS "US-X2Y" + +#endif diff --git a/sound/usb/usx2y/usbusx2yaudio.c b/sound/usb/usx2y/usbusx2yaudio.c new file mode 100644 index 000000000000..4c292e090069 --- /dev/null +++ b/sound/usb/usx2y/usbusx2yaudio.c @@ -0,0 +1,1026 @@ +/* + * US-X2Y AUDIO + * Copyright (c) 2002-2004 by Karsten Wiese + * + * based on + * + * (Tentative) USB Audio Driver for ALSA + * + * Main and PCM part + * + * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de> + * + * Many codes borrowed from audio.c by + * Alan Cox (alan@lxorguk.ukuu.org.uk) + * Thomas Sailer (sailer@ife.ee.ethz.ch) + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include <sound/driver.h> +#include <linux/interrupt.h> +#include <linux/usb.h> +#include <sound/core.h> +#include <sound/info.h> +#include <sound/pcm.h> +#include <sound/pcm_params.h> +#include "usx2y.h" +#include "usbusx2y.h" + +#define USX2Y_NRPACKS 4 /* Default value used for nr of packs per urb. + 1 to 4 have been tested ok on uhci. + To use 3 on ohci, you'd need a patch: + look for "0000425-linux-2.6.9-rc4-mm1_ohci-hcd.patch.gz" on + "https://bugtrack.alsa-project.org/alsa-bug/bug_view_page.php?bug_id=0000425" + . + 1, 2 and 4 work out of the box on ohci, if I recall correctly. + Bigger is safer operation, + smaller gives lower latencies. + */ +#define USX2Y_NRPACKS_VARIABLE y /* If your system works ok with this module's parameter + nrpacks set to 1, you might as well comment + this #define out, and thereby produce smaller, faster code. + You'd also set USX2Y_NRPACKS to 1 then. + */ + +#ifdef USX2Y_NRPACKS_VARIABLE + static int nrpacks = USX2Y_NRPACKS; /* number of packets per urb */ + #define nr_of_packs() nrpacks + module_param(nrpacks, int, 0444); + MODULE_PARM_DESC(nrpacks, "Number of packets per URB."); +#else + #define nr_of_packs() USX2Y_NRPACKS +#endif + + +static int usX2Y_urb_capt_retire(snd_usX2Y_substream_t *subs) +{ + struct urb *urb = subs->completed_urb; + snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime; + unsigned char *cp; + int i, len, lens = 0, hwptr_done = subs->hwptr_done; + usX2Ydev_t *usX2Y = subs->usX2Y; + + for (i = 0; i < nr_of_packs(); i++) { + cp = (unsigned char*)urb->transfer_buffer + urb->iso_frame_desc[i].offset; + if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */ + snd_printk("activ frame status %i. Most propably some hardware problem.\n", urb->iso_frame_desc[i].status); + return urb->iso_frame_desc[i].status; + } + len = urb->iso_frame_desc[i].actual_length / usX2Y->stride; + if (! len) { + snd_printd("0 == len ERROR!\n"); + continue; + } + + /* copy a data chunk */ + if ((hwptr_done + len) > runtime->buffer_size) { + int cnt = runtime->buffer_size - hwptr_done; + int blen = cnt * usX2Y->stride; + memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp, blen); + memcpy(runtime->dma_area, cp + blen, len * usX2Y->stride - blen); + } else { + memcpy(runtime->dma_area + hwptr_done * usX2Y->stride, cp, len * usX2Y->stride); + } + lens += len; + if ((hwptr_done += len) >= runtime->buffer_size) + hwptr_done -= runtime->buffer_size; + } + + subs->hwptr_done = hwptr_done; + subs->transfer_done += lens; + /* update the pointer, call callback if necessary */ + if (subs->transfer_done >= runtime->period_size) { + subs->transfer_done -= runtime->period_size; + snd_pcm_period_elapsed(subs->pcm_substream); + } + return 0; +} +/* + * prepare urb for playback data pipe + * + * we copy the data directly from the pcm buffer. + * the current position to be copied is held in hwptr field. + * since a urb can handle only a single linear buffer, if the total + * transferred area overflows the buffer boundary, we cannot send + * it directly from the buffer. thus the data is once copied to + * a temporary buffer and urb points to that. + */ +static int usX2Y_urb_play_prepare(snd_usX2Y_substream_t *subs, + struct urb *cap_urb, + struct urb *urb) +{ + int count, counts, pack; + usX2Ydev_t* usX2Y = subs->usX2Y; + snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime; + + count = 0; + for (pack = 0; pack < nr_of_packs(); pack++) { + /* calculate the size of a packet */ + counts = cap_urb->iso_frame_desc[pack].actual_length / usX2Y->stride; + count += counts; + if (counts < 43 || counts > 50) { + snd_printk("should not be here with counts=%i\n", counts); + return -EPIPE; + } + /* set up descriptor */ + urb->iso_frame_desc[pack].offset = pack ? + urb->iso_frame_desc[pack - 1].offset + urb->iso_frame_desc[pack - 1].length : + 0; + urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length; + } + if (atomic_read(&subs->state) >= state_PRERUNNING) + if (subs->hwptr + count > runtime->buffer_size) { + /* err, the transferred area goes over buffer boundary. + * copy the data to the temp buffer. + */ + int len; + len = runtime->buffer_size - subs->hwptr; + urb->transfer_buffer = subs->tmpbuf; + memcpy(subs->tmpbuf, runtime->dma_area + subs->hwptr * usX2Y->stride, len * usX2Y->stride); + memcpy(subs->tmpbuf + len * usX2Y->stride, runtime->dma_area, (count - len) * usX2Y->stride); + subs->hwptr += count; + subs->hwptr -= runtime->buffer_size; + } else { + /* set the buffer pointer */ + urb->transfer_buffer = runtime->dma_area + subs->hwptr * usX2Y->stride; + if ((subs->hwptr += count) >= runtime->buffer_size) + subs->hwptr -= runtime->buffer_size; + } + else + urb->transfer_buffer = subs->tmpbuf; + urb->transfer_buffer_length = count * usX2Y->stride; + return 0; +} + +/* + * process after playback data complete + * + * update the current position and call callback if a period is processed. + */ +static void usX2Y_urb_play_retire(snd_usX2Y_substream_t *subs, struct urb *urb) +{ + snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime; + int len = urb->actual_length / subs->usX2Y->stride; + + subs->transfer_done += len; + subs->hwptr_done += len; + if (subs->hwptr_done >= runtime->buffer_size) + subs->hwptr_done -= runtime->buffer_size; + if (subs->transfer_done >= runtime->period_size) { + subs->transfer_done -= runtime->period_size; + snd_pcm_period_elapsed(subs->pcm_substream); + } +} + +static int usX2Y_urb_submit(snd_usX2Y_substream_t *subs, struct urb *urb, int frame) +{ + int err; + if (!urb) + return -ENODEV; + urb->start_frame = (frame + NRURBS * nr_of_packs()); // let hcd do rollover sanity checks + urb->hcpriv = NULL; + urb->dev = subs->usX2Y->chip.dev; /* we need to set this at each time */ + if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { + snd_printk("usb_submit_urb() returned %i\n", err); + return err; + } + return 0; +} + +static inline int usX2Y_usbframe_complete(snd_usX2Y_substream_t *capsubs, snd_usX2Y_substream_t *playbacksubs, int frame) +{ + int err, state; + { + struct urb *urb = playbacksubs->completed_urb; + + state = atomic_read(&playbacksubs->state); + if (NULL != urb) { + if (state == state_RUNNING) + usX2Y_urb_play_retire(playbacksubs, urb); + else + if (state >= state_PRERUNNING) { + atomic_inc(&playbacksubs->state); + } + } else { + switch (state) { + case state_STARTING1: + urb = playbacksubs->urb[0]; + atomic_inc(&playbacksubs->state); + break; + case state_STARTING2: + urb = playbacksubs->urb[1]; + atomic_inc(&playbacksubs->state); + break; + } + } + if (urb) { + if ((err = usX2Y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb)) || + (err = usX2Y_urb_submit(playbacksubs, urb, frame))) { + return err; + } + } + + playbacksubs->completed_urb = NULL; + } + state = atomic_read(&capsubs->state); + if (state >= state_PREPARED) { + if (state == state_RUNNING) { + if ((err = usX2Y_urb_capt_retire(capsubs))) + return err; + } else + if (state >= state_PRERUNNING) { + atomic_inc(&capsubs->state); + } + if ((err = usX2Y_urb_submit(capsubs, capsubs->completed_urb, frame))) + return err; + } + capsubs->completed_urb = NULL; + return 0; +} + + +static void usX2Y_clients_stop(usX2Ydev_t *usX2Y) +{ + int s, u; + for (s = 0; s < 4; s++) { + snd_usX2Y_substream_t *subs = usX2Y->subs[s]; + if (subs) { + snd_printdd("%i %p state=%i\n", s, subs, atomic_read(&subs->state)); + atomic_set(&subs->state, state_STOPPED); + } + } + for (s = 0; s < 4; s++) { + snd_usX2Y_substream_t *subs = usX2Y->subs[s]; + if (subs) { + if (atomic_read(&subs->state) >= state_PRERUNNING) { + snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN); + } + for (u = 0; u < NRURBS; u++) { + struct urb *urb = subs->urb[u]; + if (NULL != urb) + snd_printdd("%i status=%i start_frame=%i\n", u, urb->status, urb->start_frame); + } + } + } + usX2Y->prepare_subs = NULL; + wake_up(&usX2Y->prepare_wait_queue); +} + +static void usX2Y_error_urb_status(usX2Ydev_t *usX2Y, snd_usX2Y_substream_t *subs, struct urb *urb) +{ + snd_printk("ep=%i stalled with status=%i\n", subs->endpoint, urb->status); + urb->status = 0; + usX2Y_clients_stop(usX2Y); +} + +static void usX2Y_error_sequence(usX2Ydev_t *usX2Y, snd_usX2Y_substream_t *subs, struct urb *urb) +{ + snd_printk("Sequence Error!(hcd_frame=%i ep=%i%s;wait=%i,frame=%i).\n" + "Most propably some urb of usb-frame %i is still missing.\n" + "Cause could be too long delays in usb-hcd interrupt handling.\n", + usb_get_current_frame_number(usX2Y->chip.dev), + subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out", usX2Y->wait_iso_frame, urb->start_frame, usX2Y->wait_iso_frame); + usX2Y_clients_stop(usX2Y); +} + +static void i_usX2Y_urb_complete(struct urb *urb, struct pt_regs *regs) +{ + snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t*)urb->context; + usX2Ydev_t *usX2Y = subs->usX2Y; + + if (unlikely(atomic_read(&subs->state) < state_PREPARED)) { + snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n", usb_get_current_frame_number(usX2Y->chip.dev), subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out", urb->status, urb->start_frame); + return; + } + if (unlikely(urb->status)) { + usX2Y_error_urb_status(usX2Y, subs, urb); + return; + } + if (likely((0xFFFF & urb->start_frame) == usX2Y->wait_iso_frame)) + subs->completed_urb = urb; + else { + usX2Y_error_sequence(usX2Y, subs, urb); + return; + } + { + snd_usX2Y_substream_t *capsubs = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE], + *playbacksubs = usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]; + if (capsubs->completed_urb && atomic_read(&capsubs->state) >= state_PREPARED && + (playbacksubs->completed_urb || atomic_read(&playbacksubs->state) < state_PREPARED)) { + if (!usX2Y_usbframe_complete(capsubs, playbacksubs, urb->start_frame)) { + if (nr_of_packs() <= urb->start_frame && + urb->start_frame <= (2 * nr_of_packs() - 1)) // uhci and ohci + usX2Y->wait_iso_frame = urb->start_frame - nr_of_packs(); + else + usX2Y->wait_iso_frame += nr_of_packs(); + } else { + snd_printdd("\n"); + usX2Y_clients_stop(usX2Y); + } + } + } +} + +static void usX2Y_urbs_set_complete(usX2Ydev_t * usX2Y, void (*complete)(struct urb *, struct pt_regs *)) +{ + int s, u; + for (s = 0; s < 4; s++) { + snd_usX2Y_substream_t *subs = usX2Y->subs[s]; + if (NULL != subs) + for (u = 0; u < NRURBS; u++) { + struct urb * urb = subs->urb[u]; + if (NULL != urb) + urb->complete = complete; + } + } +} + +static void usX2Y_subs_startup_finish(usX2Ydev_t * usX2Y) +{ + usX2Y_urbs_set_complete(usX2Y, i_usX2Y_urb_complete); + usX2Y->prepare_subs = NULL; +} + +static void i_usX2Y_subs_startup(struct urb *urb, struct pt_regs *regs) +{ + snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t*)urb->context; + usX2Ydev_t *usX2Y = subs->usX2Y; + snd_usX2Y_substream_t *prepare_subs = usX2Y->prepare_subs; + if (NULL != prepare_subs) + if (urb->start_frame == prepare_subs->urb[0]->start_frame) { + usX2Y_subs_startup_finish(usX2Y); + atomic_inc(&prepare_subs->state); + wake_up(&usX2Y->prepare_wait_queue); + } + + i_usX2Y_urb_complete(urb, regs); +} + +static void usX2Y_subs_prepare(snd_usX2Y_substream_t *subs) +{ + snd_printdd("usX2Y_substream_prepare(%p) ep=%i urb0=%p urb1=%p\n", subs, subs->endpoint, subs->urb[0], subs->urb[1]); + /* reset the pointer */ + subs->hwptr = 0; + subs->hwptr_done = 0; + subs->transfer_done = 0; +} + + +static void usX2Y_urb_release(struct urb** urb, int free_tb) +{ + if (*urb) { + usb_kill_urb(*urb); + if (free_tb) + kfree((*urb)->transfer_buffer); + usb_free_urb(*urb); + *urb = NULL; + } +} +/* + * release a substreams urbs + */ +static void usX2Y_urbs_release(snd_usX2Y_substream_t *subs) +{ + int i; + snd_printdd("usX2Y_urbs_release() %i\n", subs->endpoint); + for (i = 0; i < NRURBS; i++) + usX2Y_urb_release(subs->urb + i, subs != subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]); + + if (subs->tmpbuf) { + kfree(subs->tmpbuf); + subs->tmpbuf = NULL; + } +} +/* + * initialize a substream's urbs + */ +static int usX2Y_urbs_allocate(snd_usX2Y_substream_t *subs) +{ + int i; + unsigned int pipe; + int is_playback = subs == subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]; + struct usb_device *dev = subs->usX2Y->chip.dev; + + pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) : + usb_rcvisocpipe(dev, subs->endpoint); + subs->maxpacksize = usb_maxpacket(dev, pipe, is_playback); + if (!subs->maxpacksize) + return -EINVAL; + + if (is_playback && NULL == subs->tmpbuf) { /* allocate a temporary buffer for playback */ + subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL); + if (NULL == subs->tmpbuf) { + snd_printk(KERN_ERR "cannot malloc tmpbuf\n"); + return -ENOMEM; + } + } + /* allocate and initialize data urbs */ + for (i = 0; i < NRURBS; i++) { + struct urb** purb = subs->urb + i; + if (*purb) { + usb_kill_urb(*purb); + continue; + } + *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL); + if (NULL == *purb) { + usX2Y_urbs_release(subs); + return -ENOMEM; + } + if (!is_playback && !(*purb)->transfer_buffer) { + /* allocate a capture buffer per urb */ + (*purb)->transfer_buffer = kmalloc(subs->maxpacksize * nr_of_packs(), GFP_KERNEL); + if (NULL == (*purb)->transfer_buffer) { + usX2Y_urbs_release(subs); + return -ENOMEM; + } + } + (*purb)->dev = dev; + (*purb)->pipe = pipe; + (*purb)->number_of_packets = nr_of_packs(); + (*purb)->context = subs; + (*purb)->interval = 1; + (*purb)->complete = i_usX2Y_subs_startup; + } + return 0; +} + +static void usX2Y_subs_startup(snd_usX2Y_substream_t *subs) +{ + usX2Ydev_t *usX2Y = subs->usX2Y; + usX2Y->prepare_subs = subs; + subs->urb[0]->start_frame = -1; + wmb(); + usX2Y_urbs_set_complete(usX2Y, i_usX2Y_subs_startup); +} + +static int usX2Y_urbs_start(snd_usX2Y_substream_t *subs) +{ + int i, err; + usX2Ydev_t *usX2Y = subs->usX2Y; + + if ((err = usX2Y_urbs_allocate(subs)) < 0) + return err; + subs->completed_urb = NULL; + for (i = 0; i < 4; i++) { + snd_usX2Y_substream_t *subs = usX2Y->subs[i]; + if (subs != NULL && atomic_read(&subs->state) >= state_PREPARED) + goto start; + } + usX2Y->wait_iso_frame = -1; + start: + { + usX2Y_subs_startup(subs); + for (i = 0; i < NRURBS; i++) { + struct urb *urb = subs->urb[i]; + if (usb_pipein(urb->pipe)) { + unsigned long pack; + if (0 == i) + atomic_set(&subs->state, state_STARTING3); + urb->dev = usX2Y->chip.dev; + urb->transfer_flags = URB_ISO_ASAP; + for (pack = 0; pack < nr_of_packs(); pack++) { + urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack; + urb->iso_frame_desc[pack].length = subs->maxpacksize; + } + urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs(); + if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) { + snd_printk (KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err); + err = -EPIPE; + goto cleanup; + } else { + if (0 > usX2Y->wait_iso_frame) + usX2Y->wait_iso_frame = urb->start_frame; + } + urb->transfer_flags = 0; + } else { + atomic_set(&subs->state, state_STARTING1); + break; + } + } + err = 0; + wait_event(usX2Y->prepare_wait_queue, NULL == usX2Y->prepare_subs); + if (atomic_read(&subs->state) != state_PREPARED) { + err = -EPIPE; + } + + cleanup: + if (err) { + usX2Y_subs_startup_finish(usX2Y); + usX2Y_clients_stop(usX2Y); // something is completely wroong > stop evrything + } + } + return err; +} + +/* + * return the current pcm pointer. just return the hwptr_done value. + */ +static snd_pcm_uframes_t snd_usX2Y_pcm_pointer(snd_pcm_substream_t *substream) +{ + snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)substream->runtime->private_data; + return subs->hwptr_done; +} +/* + * start/stop substream + */ +static int snd_usX2Y_pcm_trigger(snd_pcm_substream_t *substream, int cmd) +{ + snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)substream->runtime->private_data; + + switch (cmd) { + case SNDRV_PCM_TRIGGER_START: + snd_printdd("snd_usX2Y_pcm_trigger(START)\n"); + if (atomic_read(&subs->state) == state_PREPARED && + atomic_read(&subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= state_PREPARED) { + atomic_set(&subs->state, state_PRERUNNING); + } else { + snd_printdd("\n"); + return -EPIPE; + } + break; + case SNDRV_PCM_TRIGGER_STOP: + snd_printdd("snd_usX2Y_pcm_trigger(STOP)\n"); + if (atomic_read(&subs->state) >= state_PRERUNNING) + atomic_set(&subs->state, state_PREPARED); + break; + default: + return -EINVAL; + } + return 0; +} + + +/* + * allocate a buffer, setup samplerate + * + * so far we use a physically linear buffer although packetize transfer + * doesn't need a continuous area. + * if sg buffer is supported on the later version of alsa, we'll follow + * that. + */ +static struct s_c2 +{ + char c1, c2; +} + SetRate44100[] = +{ + { 0x14, 0x08}, // this line sets 44100, well actually a little less + { 0x18, 0x40}, // only tascam / frontier design knows the further lines ....... + { 0x18, 0x42}, + { 0x18, 0x45}, + { 0x18, 0x46}, + { 0x18, 0x48}, + { 0x18, 0x4A}, + { 0x18, 0x4C}, + { 0x18, 0x4E}, + { 0x18, 0x50}, + { 0x18, 0x52}, + { 0x18, 0x54}, + { 0x18, 0x56}, + { 0x18, 0x58}, + { 0x18, 0x5A}, + { 0x18, 0x5C}, + { 0x18, 0x5E}, + { 0x18, 0x60}, + { 0x18, 0x62}, + { 0x18, 0x64}, + { 0x18, 0x66}, + { 0x18, 0x68}, + { 0x18, 0x6A}, + { 0x18, 0x6C}, + { 0x18, 0x6E}, + { 0x18, 0x70}, + { 0x18, 0x72}, + { 0x18, 0x74}, + { 0x18, 0x76}, + { 0x18, 0x78}, + { 0x18, 0x7A}, + { 0x18, 0x7C}, + { 0x18, 0x7E} +}; +static struct s_c2 SetRate48000[] = +{ + { 0x14, 0x09}, // this line sets 48000, well actually a little less + { 0x18, 0x40}, // only tascam / frontier design knows the further lines ....... + { 0x18, 0x42}, + { 0x18, 0x45}, + { 0x18, 0x46}, + { 0x18, 0x48}, + { 0x18, 0x4A}, + { 0x18, 0x4C}, + { 0x18, 0x4E}, + { 0x18, 0x50}, + { 0x18, 0x52}, + { 0x18, 0x54}, + { 0x18, 0x56}, + { 0x18, 0x58}, + { 0x18, 0x5A}, + { 0x18, 0x5C}, + { 0x18, 0x5E}, + { 0x18, 0x60}, + { 0x18, 0x62}, + { 0x18, 0x64}, + { 0x18, 0x66}, + { 0x18, 0x68}, + { 0x18, 0x6A}, + { 0x18, 0x6C}, + { 0x18, 0x6E}, + { 0x18, 0x70}, + { 0x18, 0x73}, + { 0x18, 0x74}, + { 0x18, 0x76}, + { 0x18, 0x78}, + { 0x18, 0x7A}, + { 0x18, 0x7C}, + { 0x18, 0x7E} +}; +#define NOOF_SETRATE_URBS ARRAY_SIZE(SetRate48000) + +static void i_usX2Y_04Int(struct urb* urb, struct pt_regs *regs) +{ + usX2Ydev_t* usX2Y = urb->context; + + if (urb->status) { + snd_printk("snd_usX2Y_04Int() urb->status=%i\n", urb->status); + } + if (0 == --usX2Y->US04->len) + wake_up(&usX2Y->In04WaitQueue); +} + +static int usX2Y_rate_set(usX2Ydev_t *usX2Y, int rate) +{ + int err = 0, i; + snd_usX2Y_urbSeq_t *us = NULL; + int *usbdata = NULL; + struct s_c2 *ra = rate == 48000 ? SetRate48000 : SetRate44100; + + if (usX2Y->rate != rate) { + us = kmalloc(sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS, GFP_KERNEL); + if (NULL == us) { + err = -ENOMEM; + goto cleanup; + } + memset(us, 0, sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS); + usbdata = kmalloc(sizeof(int)*NOOF_SETRATE_URBS, GFP_KERNEL); + if (NULL == usbdata) { + err = -ENOMEM; + goto cleanup; + } + for (i = 0; i < NOOF_SETRATE_URBS; ++i) { + if (NULL == (us->urb[i] = usb_alloc_urb(0, GFP_KERNEL))) { + err = -ENOMEM; + goto cleanup; + } + ((char*)(usbdata + i))[0] = ra[i].c1; + ((char*)(usbdata + i))[1] = ra[i].c2; + usb_fill_bulk_urb(us->urb[i], usX2Y->chip.dev, usb_sndbulkpipe(usX2Y->chip.dev, 4), + usbdata + i, 2, i_usX2Y_04Int, usX2Y); +#ifdef OLD_USB + us->urb[i]->transfer_flags = USB_QUEUE_BULK; +#endif + } + us->submitted = 0; + us->len = NOOF_SETRATE_URBS; + usX2Y->US04 = us; + wait_event_timeout(usX2Y->In04WaitQueue, 0 == us->len, HZ); + usX2Y->US04 = NULL; + if (us->len) + err = -ENODEV; + cleanup: + if (us) { + us->submitted = 2*NOOF_SETRATE_URBS; + for (i = 0; i < NOOF_SETRATE_URBS; ++i) { + struct urb *urb = us->urb[i]; + if (urb->status) { + if (!err) + err = -ENODEV; + usb_kill_urb(urb); + } + usb_free_urb(urb); + } + usX2Y->US04 = NULL; + kfree(usbdata); + kfree(us); + if (!err) { + usX2Y->rate = rate; + } + } + } + + return err; +} + + +static int usX2Y_format_set(usX2Ydev_t *usX2Y, snd_pcm_format_t format) +{ + int alternate, err; + struct list_head* p; + if (format == SNDRV_PCM_FORMAT_S24_3LE) { + alternate = 2; + usX2Y->stride = 6; + } else { + alternate = 1; + usX2Y->stride = 4; + } + list_for_each(p, &usX2Y->chip.midi_list) { + snd_usbmidi_input_stop(p); + } + usb_kill_urb(usX2Y->In04urb); + if ((err = usb_set_interface(usX2Y->chip.dev, 0, alternate))) { + snd_printk("usb_set_interface error \n"); + return err; + } + usX2Y->In04urb->dev = usX2Y->chip.dev; + err = usb_submit_urb(usX2Y->In04urb, GFP_KERNEL); + list_for_each(p, &usX2Y->chip.midi_list) { + snd_usbmidi_input_start(p); + } + usX2Y->format = format; + usX2Y->rate = 0; + return err; +} + + +static int snd_usX2Y_pcm_hw_params(snd_pcm_substream_t *substream, + snd_pcm_hw_params_t *hw_params) +{ + int err = 0; + unsigned int rate = params_rate(hw_params); + snd_pcm_format_t format = params_format(hw_params); + snd_printdd("snd_usX2Y_hw_params(%p, %p)\n", substream, hw_params); + + { // all pcm substreams off one usX2Y have to operate at the same rate & format + snd_card_t *card = substream->pstr->pcm->card; + struct list_head *list; + list_for_each(list, &card->devices) { + snd_device_t *dev; + snd_pcm_t *pcm; + int s; + dev = snd_device(list); + if (dev->type != SNDRV_DEV_PCM) + continue; + pcm = dev->device_data; + for (s = 0; s < 2; ++s) { + snd_pcm_substream_t *test_substream; + test_substream = pcm->streams[s].substream; + if (test_substream && test_substream != substream && + test_substream->runtime && + ((test_substream->runtime->format && + test_substream->runtime->format != format) || + (test_substream->runtime->rate && + test_substream->runtime->rate != rate))) + return -EINVAL; + } + } + } + if (0 > (err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)))) { + snd_printk("snd_pcm_lib_malloc_pages(%p, %i) returned %i\n", substream, params_buffer_bytes(hw_params), err); + return err; + } + return 0; +} + +/* + * free the buffer + */ +static int snd_usX2Y_pcm_hw_free(snd_pcm_substream_t *substream) +{ + snd_pcm_runtime_t *runtime = substream->runtime; + snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data; + down(&subs->usX2Y->prepare_mutex); + snd_printdd("snd_usX2Y_hw_free(%p)\n", substream); + + if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) { + snd_usX2Y_substream_t *cap_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]; + atomic_set(&subs->state, state_STOPPED); + usX2Y_urbs_release(subs); + if (!cap_subs->pcm_substream || + !cap_subs->pcm_substream->runtime || + !cap_subs->pcm_substream->runtime->status || + cap_subs->pcm_substream->runtime->status->state < SNDRV_PCM_STATE_PREPARED) { + atomic_set(&cap_subs->state, state_STOPPED); + usX2Y_urbs_release(cap_subs); + } + } else { + snd_usX2Y_substream_t *playback_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]; + if (atomic_read(&playback_subs->state) < state_PREPARED) { + atomic_set(&subs->state, state_STOPPED); + usX2Y_urbs_release(subs); + } + } + up(&subs->usX2Y->prepare_mutex); + return snd_pcm_lib_free_pages(substream); +} +/* + * prepare callback + * + * set format and initialize urbs + */ +static int snd_usX2Y_pcm_prepare(snd_pcm_substream_t *substream) +{ + snd_pcm_runtime_t *runtime = substream->runtime; + snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data; + usX2Ydev_t *usX2Y = subs->usX2Y; + snd_usX2Y_substream_t *capsubs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]; + int err = 0; + snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream); + + down(&usX2Y->prepare_mutex); + usX2Y_subs_prepare(subs); +// Start hardware streams +// SyncStream first.... + if (atomic_read(&capsubs->state) < state_PREPARED) { + if (usX2Y->format != runtime->format) + if ((err = usX2Y_format_set(usX2Y, runtime->format)) < 0) + goto up_prepare_mutex; + if (usX2Y->rate != runtime->rate) + if ((err = usX2Y_rate_set(usX2Y, runtime->rate)) < 0) + goto up_prepare_mutex; + snd_printdd("starting capture pipe for %s\n", subs == capsubs ? "self" : "playpipe"); + if (0 > (err = usX2Y_urbs_start(capsubs))) + goto up_prepare_mutex; + } + + if (subs != capsubs && atomic_read(&subs->state) < state_PREPARED) + err = usX2Y_urbs_start(subs); + + up_prepare_mutex: + up(&usX2Y->prepare_mutex); + return err; +} + +static snd_pcm_hardware_t snd_usX2Y_2c = +{ + .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | + SNDRV_PCM_INFO_BLOCK_TRANSFER | + SNDRV_PCM_INFO_MMAP_VALID), + .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE, + .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, + .rate_min = 44100, + .rate_max = 48000, + .channels_min = 2, + .channels_max = 2, + .buffer_bytes_max = (2*128*1024), + .period_bytes_min = 64, + .period_bytes_max = (128*1024), + .periods_min = 2, + .periods_max = 1024, + .fifo_size = 0 +}; + + + +static int snd_usX2Y_pcm_open(snd_pcm_substream_t *substream) +{ + snd_usX2Y_substream_t *subs = ((snd_usX2Y_substream_t **) + snd_pcm_substream_chip(substream))[substream->stream]; + snd_pcm_runtime_t *runtime = substream->runtime; + + if (subs->usX2Y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS) + return -EBUSY; + + runtime->hw = snd_usX2Y_2c; + runtime->private_data = subs; + subs->pcm_substream = substream; + snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000); + return 0; +} + + + +static int snd_usX2Y_pcm_close(snd_pcm_substream_t *substream) +{ + snd_pcm_runtime_t *runtime = substream->runtime; + snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data; + int err = 0; + + subs->pcm_substream = NULL; + + return err; +} + + +static snd_pcm_ops_t snd_usX2Y_pcm_ops = +{ + .open = snd_usX2Y_pcm_open, + .close = snd_usX2Y_pcm_close, + .ioctl = snd_pcm_lib_ioctl, + .hw_params = snd_usX2Y_pcm_hw_params, + .hw_free = snd_usX2Y_pcm_hw_free, + .prepare = snd_usX2Y_pcm_prepare, + .trigger = snd_usX2Y_pcm_trigger, + .pointer = snd_usX2Y_pcm_pointer, +}; + + +/* + * free a usb stream instance + */ +static void usX2Y_audio_stream_free(snd_usX2Y_substream_t **usX2Y_substream) +{ + if (NULL != usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]) { + kfree(usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]); + usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK] = NULL; + } + kfree(usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]); + usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE] = NULL; +} + +static void snd_usX2Y_pcm_private_free(snd_pcm_t *pcm) +{ + snd_usX2Y_substream_t **usX2Y_stream = pcm->private_data; + if (usX2Y_stream) { + snd_pcm_lib_preallocate_free_for_all(pcm); + usX2Y_audio_stream_free(usX2Y_stream); + } +} + +static int usX2Y_audio_stream_new(snd_card_t *card, int playback_endpoint, int capture_endpoint) +{ + snd_pcm_t *pcm; + int err, i; + snd_usX2Y_substream_t **usX2Y_substream = + usX2Y(card)->subs + 2 * usX2Y(card)->chip.pcm_devs; + + for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE; + i <= SNDRV_PCM_STREAM_CAPTURE; ++i) { + usX2Y_substream[i] = kcalloc(1, sizeof(snd_usX2Y_substream_t), GFP_KERNEL); + if (NULL == usX2Y_substream[i]) { + snd_printk(KERN_ERR "cannot malloc\n"); + return -ENOMEM; + } + usX2Y_substream[i]->usX2Y = usX2Y(card); + } + + if (playback_endpoint) + usX2Y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint; + usX2Y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint; + + err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usX2Y(card)->chip.pcm_devs, + playback_endpoint ? 1 : 0, 1, + &pcm); + if (err < 0) { + usX2Y_audio_stream_free(usX2Y_substream); + return err; + } + + if (playback_endpoint) + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usX2Y_pcm_ops); + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usX2Y_pcm_ops); + + pcm->private_data = usX2Y_substream; + pcm->private_free = snd_usX2Y_pcm_private_free; + pcm->info_flags = 0; + + sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usX2Y(card)->chip.pcm_devs); + + if ((playback_endpoint && + 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream, + SNDRV_DMA_TYPE_CONTINUOUS, + snd_dma_continuous_data(GFP_KERNEL), + 64*1024, 128*1024))) || + 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream, + SNDRV_DMA_TYPE_CONTINUOUS, + snd_dma_continuous_data(GFP_KERNEL), + 64*1024, 128*1024))) { + snd_usX2Y_pcm_private_free(pcm); + return err; + } + usX2Y(card)->chip.pcm_devs++; + + return 0; +} + +/* + * create a chip instance and set its names. + */ +int usX2Y_audio_create(snd_card_t* card) +{ + int err = 0; + + INIT_LIST_HEAD(&usX2Y(card)->chip.pcm_list); + + if (0 > (err = usX2Y_audio_stream_new(card, 0xA, 0x8))) + return err; + if (le16_to_cpu(usX2Y(card)->chip.dev->descriptor.idProduct) == USB_ID_US428) + if (0 > (err = usX2Y_audio_stream_new(card, 0, 0xA))) + return err; + if (le16_to_cpu(usX2Y(card)->chip.dev->descriptor.idProduct) != USB_ID_US122) + err = usX2Y_rate_set(usX2Y(card), 44100); // Lets us428 recognize output-volume settings, disturbs us122. + return err; +} diff --git a/sound/usb/usx2y/usx2y.h b/sound/usb/usx2y/usx2y.h new file mode 100644 index 000000000000..7e59263dd895 --- /dev/null +++ b/sound/usb/usx2y/usx2y.h @@ -0,0 +1,51 @@ +/* + * Driver for Tascam US-X2Y USB soundcards + * + * Copyright (c) 2003 by Karsten Wiese <annabellesgarden@yahoo.de> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef __SOUND_USX2Y_COMMON_H +#define __SOUND_USX2Y_COMMON_H + + +#define USX2Y_DRIVER_VERSION 0x0100 /* 0.1.0 */ + + +/* hwdep id string */ +#define SND_USX2Y_LOADER_ID "USX2Y Loader" +#define SND_USX2Y_USBPCM_ID "USX2Y USBPCM" + +/* hardware type */ +enum { + USX2Y_TYPE_122, + USX2Y_TYPE_224, + USX2Y_TYPE_428, + USX2Y_TYPE_NUMS +}; + +#define USB_ID_US122 0x8007 +#define USB_ID_US224 0x8005 +#define USB_ID_US428 0x8001 + +/* chip status */ +enum { + USX2Y_STAT_CHIP_INIT = (1 << 0), /* all operational */ + USX2Y_STAT_CHIP_MMAP_PCM_URBS = (1 << 1), /* pcm transport over mmaped urbs */ + USX2Y_STAT_CHIP_HUP = (1 << 31), /* all operational */ +}; + +#endif /* __SOUND_USX2Y_COMMON_H */ diff --git a/sound/usb/usx2y/usx2yhwdeppcm.c b/sound/usb/usx2y/usx2yhwdeppcm.c new file mode 100644 index 000000000000..bb2c8e9000c6 --- /dev/null +++ b/sound/usb/usx2y/usx2yhwdeppcm.c @@ -0,0 +1,807 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* USX2Y "rawusb" aka hwdep_pcm implementation + + Its usb's unableness to atomically handle power of 2 period sized data chuncs + at standard samplerates, + what led to this part of the usx2y module: + It provides the alsa kernel half of the usx2y-alsa-jack driver pair. + The pair uses a hardware dependant alsa-device for mmaped pcm transport. + Advantage achieved: + The usb_hc moves pcm data from/into memory via DMA. + That memory is mmaped by jack's usx2y driver. + Jack's usx2y driver is the first/last to read/write pcm data. + Read/write is a combination of power of 2 period shaping and + float/int conversation. + Compared to mainline alsa/jack we leave out power of 2 period shaping inside + snd-usb-usx2y which needs memcpy() and additional buffers. + As a side effect possible unwanted pcm-data coruption resulting of + standard alsa's snd-usb-usx2y period shaping scheme falls away. + Result is sane jack operation at buffering schemes down to 128frames, + 2 periods. + plain usx2y alsa mode is able to achieve 64frames, 4periods, but only at the + cost of easier triggered i.e. aeolus xruns (128 or 256frames, + 2periods works but is useless cause of crackling). + + This is a first "proof of concept" implementation. + Later, funcionalities should migrate to more apropriate places: + Userland: + - The jackd could mmap its float-pcm buffers directly from alsa-lib. + - alsa-lib could provide power of 2 period sized shaping combined with int/float + conversation. + Currently the usx2y jack driver provides above 2 services. + Kernel: + - rawusb dma pcm buffer transport should go to snd-usb-lib, so also snd-usb-audio + devices can use it. + Currently rawusb dma pcm buffer transport (this file) is only available to snd-usb-usx2y. +*/ + +#include "usbusx2yaudio.c" + +#if defined(USX2Y_NRPACKS_VARIABLE) || (!defined(USX2Y_NRPACKS_VARIABLE) && USX2Y_NRPACKS == 1) + +#include <sound/hwdep.h> + + +static int usX2Y_usbpcm_urb_capt_retire(snd_usX2Y_substream_t *subs) +{ + struct urb *urb = subs->completed_urb; + snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime; + int i, lens = 0, hwptr_done = subs->hwptr_done; + usX2Ydev_t *usX2Y = subs->usX2Y; + if (0 > usX2Y->hwdep_pcm_shm->capture_iso_start) { //FIXME + int head = usX2Y->hwdep_pcm_shm->captured_iso_head + 1; + if (head >= ARRAY_SIZE(usX2Y->hwdep_pcm_shm->captured_iso)) + head = 0; + usX2Y->hwdep_pcm_shm->capture_iso_start = head; + snd_printdd("cap start %i\n", head); + } + for (i = 0; i < nr_of_packs(); i++) { + if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */ + snd_printk("activ frame status %i. Most propably some hardware problem.\n", urb->iso_frame_desc[i].status); + return urb->iso_frame_desc[i].status; + } + lens += urb->iso_frame_desc[i].actual_length / usX2Y->stride; + } + if ((hwptr_done += lens) >= runtime->buffer_size) + hwptr_done -= runtime->buffer_size; + subs->hwptr_done = hwptr_done; + subs->transfer_done += lens; + /* update the pointer, call callback if necessary */ + if (subs->transfer_done >= runtime->period_size) { + subs->transfer_done -= runtime->period_size; + snd_pcm_period_elapsed(subs->pcm_substream); + } + return 0; +} + +static inline int usX2Y_iso_frames_per_buffer(snd_pcm_runtime_t *runtime, usX2Ydev_t * usX2Y) +{ + return (runtime->buffer_size * 1000) / usX2Y->rate + 1; //FIXME: so far only correct period_size == 2^x ? +} + +/* + * prepare urb for playback data pipe + * + * we copy the data directly from the pcm buffer. + * the current position to be copied is held in hwptr field. + * since a urb can handle only a single linear buffer, if the total + * transferred area overflows the buffer boundary, we cannot send + * it directly from the buffer. thus the data is once copied to + * a temporary buffer and urb points to that. + */ +static int usX2Y_hwdep_urb_play_prepare(snd_usX2Y_substream_t *subs, + struct urb *urb) +{ + int count, counts, pack; + usX2Ydev_t *usX2Y = subs->usX2Y; + struct snd_usX2Y_hwdep_pcm_shm *shm = usX2Y->hwdep_pcm_shm; + snd_pcm_runtime_t *runtime = subs->pcm_substream->runtime; + + if (0 > shm->playback_iso_start) { + shm->playback_iso_start = shm->captured_iso_head - + usX2Y_iso_frames_per_buffer(runtime, usX2Y); + if (0 > shm->playback_iso_start) + shm->playback_iso_start += ARRAY_SIZE(shm->captured_iso); + shm->playback_iso_head = shm->playback_iso_start; + } + + count = 0; + for (pack = 0; pack < nr_of_packs(); pack++) { + /* calculate the size of a packet */ + counts = shm->captured_iso[shm->playback_iso_head].length / usX2Y->stride; + if (counts < 43 || counts > 50) { + snd_printk("should not be here with counts=%i\n", counts); + return -EPIPE; + } + /* set up descriptor */ + urb->iso_frame_desc[pack].offset = shm->captured_iso[shm->playback_iso_head].offset; + urb->iso_frame_desc[pack].length = shm->captured_iso[shm->playback_iso_head].length; + if (atomic_read(&subs->state) != state_RUNNING) + memset((char *)urb->transfer_buffer + urb->iso_frame_desc[pack].offset, 0, + urb->iso_frame_desc[pack].length); + if (++shm->playback_iso_head >= ARRAY_SIZE(shm->captured_iso)) + shm->playback_iso_head = 0; + count += counts; + } + urb->transfer_buffer_length = count * usX2Y->stride; + return 0; +} + + +static inline void usX2Y_usbpcm_urb_capt_iso_advance(snd_usX2Y_substream_t *subs, struct urb *urb) +{ + int pack; + for (pack = 0; pack < nr_of_packs(); ++pack) { + struct usb_iso_packet_descriptor *desc = urb->iso_frame_desc + pack; + if (NULL != subs) { + snd_usX2Y_hwdep_pcm_shm_t *shm = subs->usX2Y->hwdep_pcm_shm; + int head = shm->captured_iso_head + 1; + if (head >= ARRAY_SIZE(shm->captured_iso)) + head = 0; + shm->captured_iso[head].frame = urb->start_frame + pack; + shm->captured_iso[head].offset = desc->offset; + shm->captured_iso[head].length = desc->actual_length; + shm->captured_iso_head = head; + shm->captured_iso_frames++; + } + if ((desc->offset += desc->length * NRURBS*nr_of_packs()) + + desc->length >= SSS) + desc->offset -= (SSS - desc->length); + } +} + +static inline int usX2Y_usbpcm_usbframe_complete(snd_usX2Y_substream_t *capsubs, + snd_usX2Y_substream_t *capsubs2, + snd_usX2Y_substream_t *playbacksubs, int frame) +{ + int err, state; + struct urb *urb = playbacksubs->completed_urb; + + state = atomic_read(&playbacksubs->state); + if (NULL != urb) { + if (state == state_RUNNING) + usX2Y_urb_play_retire(playbacksubs, urb); + else + if (state >= state_PRERUNNING) { + atomic_inc(&playbacksubs->state); + } + } else { + switch (state) { + case state_STARTING1: + urb = playbacksubs->urb[0]; + atomic_inc(&playbacksubs->state); + break; + case state_STARTING2: + urb = playbacksubs->urb[1]; + atomic_inc(&playbacksubs->state); + break; + } + } + if (urb) { + if ((err = usX2Y_hwdep_urb_play_prepare(playbacksubs, urb)) || + (err = usX2Y_urb_submit(playbacksubs, urb, frame))) { + return err; + } + } + + playbacksubs->completed_urb = NULL; + + state = atomic_read(&capsubs->state); + if (state >= state_PREPARED) { + if (state == state_RUNNING) { + if ((err = usX2Y_usbpcm_urb_capt_retire(capsubs))) + return err; + } else { + if (state >= state_PRERUNNING) + atomic_inc(&capsubs->state); + } + usX2Y_usbpcm_urb_capt_iso_advance(capsubs, capsubs->completed_urb); + if (NULL != capsubs2) + usX2Y_usbpcm_urb_capt_iso_advance(NULL, capsubs2->completed_urb); + if ((err = usX2Y_urb_submit(capsubs, capsubs->completed_urb, frame))) + return err; + if (NULL != capsubs2) + if ((err = usX2Y_urb_submit(capsubs2, capsubs2->completed_urb, frame))) + return err; + } + capsubs->completed_urb = NULL; + if (NULL != capsubs2) + capsubs2->completed_urb = NULL; + return 0; +} + + +static void i_usX2Y_usbpcm_urb_complete(struct urb *urb, struct pt_regs *regs) +{ + snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t*)urb->context; + usX2Ydev_t *usX2Y = subs->usX2Y; + snd_usX2Y_substream_t *capsubs, *capsubs2, *playbacksubs; + + if (unlikely(atomic_read(&subs->state) < state_PREPARED)) { + snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n", usb_get_current_frame_number(usX2Y->chip.dev), subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out", urb->status, urb->start_frame); + return; + } + if (unlikely(urb->status)) { + usX2Y_error_urb_status(usX2Y, subs, urb); + return; + } + if (likely((0xFFFF & urb->start_frame) == usX2Y->wait_iso_frame)) + subs->completed_urb = urb; + else { + usX2Y_error_sequence(usX2Y, subs, urb); + return; + } + + capsubs = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]; + capsubs2 = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE + 2]; + playbacksubs = usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]; + if (capsubs->completed_urb && atomic_read(&capsubs->state) >= state_PREPARED && + (NULL == capsubs2 || capsubs2->completed_urb) && + (playbacksubs->completed_urb || atomic_read(&playbacksubs->state) < state_PREPARED)) { + if (!usX2Y_usbpcm_usbframe_complete(capsubs, capsubs2, playbacksubs, urb->start_frame)) { + if (nr_of_packs() <= urb->start_frame && + urb->start_frame <= (2 * nr_of_packs() - 1)) // uhci and ohci + usX2Y->wait_iso_frame = urb->start_frame - nr_of_packs(); + else + usX2Y->wait_iso_frame += nr_of_packs(); + } else { + snd_printdd("\n"); + usX2Y_clients_stop(usX2Y); + } + } +} + + +static void usX2Y_hwdep_urb_release(struct urb** urb) +{ + usb_kill_urb(*urb); + usb_free_urb(*urb); + *urb = NULL; +} + +/* + * release a substream + */ +static void usX2Y_usbpcm_urbs_release(snd_usX2Y_substream_t *subs) +{ + int i; + snd_printdd("snd_usX2Y_urbs_release() %i\n", subs->endpoint); + for (i = 0; i < NRURBS; i++) + usX2Y_hwdep_urb_release(subs->urb + i); +} + +static void usX2Y_usbpcm_subs_startup_finish(usX2Ydev_t * usX2Y) +{ + usX2Y_urbs_set_complete(usX2Y, i_usX2Y_usbpcm_urb_complete); + usX2Y->prepare_subs = NULL; +} + +static void i_usX2Y_usbpcm_subs_startup(struct urb *urb, struct pt_regs *regs) +{ + snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t*)urb->context; + usX2Ydev_t *usX2Y = subs->usX2Y; + snd_usX2Y_substream_t *prepare_subs = usX2Y->prepare_subs; + if (NULL != prepare_subs && + urb->start_frame == prepare_subs->urb[0]->start_frame) { + atomic_inc(&prepare_subs->state); + if (prepare_subs == usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]) { + snd_usX2Y_substream_t *cap_subs2 = usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE + 2]; + if (cap_subs2 != NULL) + atomic_inc(&cap_subs2->state); + } + usX2Y_usbpcm_subs_startup_finish(usX2Y); + wake_up(&usX2Y->prepare_wait_queue); + } + + i_usX2Y_usbpcm_urb_complete(urb, regs); +} + +/* + * initialize a substream's urbs + */ +static int usX2Y_usbpcm_urbs_allocate(snd_usX2Y_substream_t *subs) +{ + int i; + unsigned int pipe; + int is_playback = subs == subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]; + struct usb_device *dev = subs->usX2Y->chip.dev; + + pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) : + usb_rcvisocpipe(dev, subs->endpoint); + subs->maxpacksize = usb_maxpacket(dev, pipe, is_playback); + if (!subs->maxpacksize) + return -EINVAL; + + /* allocate and initialize data urbs */ + for (i = 0; i < NRURBS; i++) { + struct urb** purb = subs->urb + i; + if (*purb) { + usb_kill_urb(*purb); + continue; + } + *purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL); + if (NULL == *purb) { + usX2Y_usbpcm_urbs_release(subs); + return -ENOMEM; + } + (*purb)->transfer_buffer = is_playback ? + subs->usX2Y->hwdep_pcm_shm->playback : ( + subs->endpoint == 0x8 ? + subs->usX2Y->hwdep_pcm_shm->capture0x8 : + subs->usX2Y->hwdep_pcm_shm->capture0xA); + + (*purb)->dev = dev; + (*purb)->pipe = pipe; + (*purb)->number_of_packets = nr_of_packs(); + (*purb)->context = subs; + (*purb)->interval = 1; + (*purb)->complete = i_usX2Y_usbpcm_subs_startup; + } + return 0; +} + +/* + * free the buffer + */ +static int snd_usX2Y_usbpcm_hw_free(snd_pcm_substream_t *substream) +{ + snd_pcm_runtime_t *runtime = substream->runtime; + snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data, + *cap_subs2 = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE + 2]; + down(&subs->usX2Y->prepare_mutex); + snd_printdd("snd_usX2Y_usbpcm_hw_free(%p)\n", substream); + + if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) { + snd_usX2Y_substream_t *cap_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]; + atomic_set(&subs->state, state_STOPPED); + usX2Y_usbpcm_urbs_release(subs); + if (!cap_subs->pcm_substream || + !cap_subs->pcm_substream->runtime || + !cap_subs->pcm_substream->runtime->status || + cap_subs->pcm_substream->runtime->status->state < SNDRV_PCM_STATE_PREPARED) { + atomic_set(&cap_subs->state, state_STOPPED); + if (NULL != cap_subs2) + atomic_set(&cap_subs2->state, state_STOPPED); + usX2Y_usbpcm_urbs_release(cap_subs); + if (NULL != cap_subs2) + usX2Y_usbpcm_urbs_release(cap_subs2); + } + } else { + snd_usX2Y_substream_t *playback_subs = subs->usX2Y->subs[SNDRV_PCM_STREAM_PLAYBACK]; + if (atomic_read(&playback_subs->state) < state_PREPARED) { + atomic_set(&subs->state, state_STOPPED); + if (NULL != cap_subs2) + atomic_set(&cap_subs2->state, state_STOPPED); + usX2Y_usbpcm_urbs_release(subs); + if (NULL != cap_subs2) + usX2Y_usbpcm_urbs_release(cap_subs2); + } + } + up(&subs->usX2Y->prepare_mutex); + return snd_pcm_lib_free_pages(substream); +} + +static void usX2Y_usbpcm_subs_startup(snd_usX2Y_substream_t *subs) +{ + usX2Ydev_t * usX2Y = subs->usX2Y; + usX2Y->prepare_subs = subs; + subs->urb[0]->start_frame = -1; + smp_wmb(); // Make shure above modifications are seen by i_usX2Y_subs_startup() + usX2Y_urbs_set_complete(usX2Y, i_usX2Y_usbpcm_subs_startup); +} + +static int usX2Y_usbpcm_urbs_start(snd_usX2Y_substream_t *subs) +{ + int p, u, err, + stream = subs->pcm_substream->stream; + usX2Ydev_t *usX2Y = subs->usX2Y; + + if (SNDRV_PCM_STREAM_CAPTURE == stream) { + usX2Y->hwdep_pcm_shm->captured_iso_head = -1; + usX2Y->hwdep_pcm_shm->captured_iso_frames = 0; + } + + for (p = 0; 3 >= (stream + p); p += 2) { + snd_usX2Y_substream_t *subs = usX2Y->subs[stream + p]; + if (subs != NULL) { + if ((err = usX2Y_usbpcm_urbs_allocate(subs)) < 0) + return err; + subs->completed_urb = NULL; + } + } + + for (p = 0; p < 4; p++) { + snd_usX2Y_substream_t *subs = usX2Y->subs[p]; + if (subs != NULL && atomic_read(&subs->state) >= state_PREPARED) + goto start; + } + usX2Y->wait_iso_frame = -1; + + start: + usX2Y_usbpcm_subs_startup(subs); + for (u = 0; u < NRURBS; u++) { + for (p = 0; 3 >= (stream + p); p += 2) { + snd_usX2Y_substream_t *subs = usX2Y->subs[stream + p]; + if (subs != NULL) { + struct urb *urb = subs->urb[u]; + if (usb_pipein(urb->pipe)) { + unsigned long pack; + if (0 == u) + atomic_set(&subs->state, state_STARTING3); + urb->dev = usX2Y->chip.dev; + urb->transfer_flags = URB_ISO_ASAP; + for (pack = 0; pack < nr_of_packs(); pack++) { + urb->iso_frame_desc[pack].offset = subs->maxpacksize * (pack + u * nr_of_packs()); + urb->iso_frame_desc[pack].length = subs->maxpacksize; + } + urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs(); + if ((err = usb_submit_urb(urb, GFP_KERNEL)) < 0) { + snd_printk (KERN_ERR "cannot usb_submit_urb() for urb %d, err = %d\n", u, err); + err = -EPIPE; + goto cleanup; + } else { + snd_printdd("%i\n", urb->start_frame); + if (0 > usX2Y->wait_iso_frame) + usX2Y->wait_iso_frame = urb->start_frame; + } + urb->transfer_flags = 0; + } else { + atomic_set(&subs->state, state_STARTING1); + break; + } + } + } + } + err = 0; + wait_event(usX2Y->prepare_wait_queue, NULL == usX2Y->prepare_subs); + if (atomic_read(&subs->state) != state_PREPARED) + err = -EPIPE; + + cleanup: + if (err) { + usX2Y_subs_startup_finish(usX2Y); // Call it now + usX2Y_clients_stop(usX2Y); // something is completely wroong > stop evrything + } + return err; +} + +/* + * prepare callback + * + * set format and initialize urbs + */ +static int snd_usX2Y_usbpcm_prepare(snd_pcm_substream_t *substream) +{ + snd_pcm_runtime_t *runtime = substream->runtime; + snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data; + usX2Ydev_t *usX2Y = subs->usX2Y; + snd_usX2Y_substream_t *capsubs = subs->usX2Y->subs[SNDRV_PCM_STREAM_CAPTURE]; + int err = 0; + snd_printdd("snd_usX2Y_pcm_prepare(%p)\n", substream); + + if (NULL == usX2Y->hwdep_pcm_shm) { + if (NULL == (usX2Y->hwdep_pcm_shm = snd_malloc_pages(sizeof(snd_usX2Y_hwdep_pcm_shm_t), GFP_KERNEL))) + return -ENOMEM; + memset(usX2Y->hwdep_pcm_shm, 0, sizeof(snd_usX2Y_hwdep_pcm_shm_t)); + } + + down(&usX2Y->prepare_mutex); + usX2Y_subs_prepare(subs); +// Start hardware streams +// SyncStream first.... + if (atomic_read(&capsubs->state) < state_PREPARED) { + if (usX2Y->format != runtime->format) + if ((err = usX2Y_format_set(usX2Y, runtime->format)) < 0) + goto up_prepare_mutex; + if (usX2Y->rate != runtime->rate) + if ((err = usX2Y_rate_set(usX2Y, runtime->rate)) < 0) + goto up_prepare_mutex; + snd_printdd("starting capture pipe for %s\n", subs == capsubs ? "self" : "playpipe"); + if (0 > (err = usX2Y_usbpcm_urbs_start(capsubs))) + goto up_prepare_mutex; + } + + if (subs != capsubs) { + usX2Y->hwdep_pcm_shm->playback_iso_start = -1; + if (atomic_read(&subs->state) < state_PREPARED) { + while (usX2Y_iso_frames_per_buffer(runtime, usX2Y) > usX2Y->hwdep_pcm_shm->captured_iso_frames) { + signed long timeout; + snd_printd("Wait: iso_frames_per_buffer=%i,captured_iso_frames=%i\n", usX2Y_iso_frames_per_buffer(runtime, usX2Y), usX2Y->hwdep_pcm_shm->captured_iso_frames); + set_current_state(TASK_INTERRUPTIBLE); + timeout = schedule_timeout(HZ/100 + 1); + if (signal_pending(current)) { + err = -ERESTARTSYS; + goto up_prepare_mutex; + } + } + if (0 > (err = usX2Y_usbpcm_urbs_start(subs))) + goto up_prepare_mutex; + } + snd_printd("Ready: iso_frames_per_buffer=%i,captured_iso_frames=%i\n", usX2Y_iso_frames_per_buffer(runtime, usX2Y), usX2Y->hwdep_pcm_shm->captured_iso_frames); + } else + usX2Y->hwdep_pcm_shm->capture_iso_start = -1; + + up_prepare_mutex: + up(&usX2Y->prepare_mutex); + return err; +} + +static snd_pcm_hardware_t snd_usX2Y_4c = +{ + .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | + SNDRV_PCM_INFO_BLOCK_TRANSFER | + SNDRV_PCM_INFO_MMAP_VALID), + .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE, + .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, + .rate_min = 44100, + .rate_max = 48000, + .channels_min = 2, + .channels_max = 4, + .buffer_bytes_max = (2*128*1024), + .period_bytes_min = 64, + .period_bytes_max = (128*1024), + .periods_min = 2, + .periods_max = 1024, + .fifo_size = 0 +}; + + + +static int snd_usX2Y_usbpcm_open(snd_pcm_substream_t *substream) +{ + snd_usX2Y_substream_t *subs = ((snd_usX2Y_substream_t **) + snd_pcm_substream_chip(substream))[substream->stream]; + snd_pcm_runtime_t *runtime = substream->runtime; + + if (!(subs->usX2Y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS)) + return -EBUSY; + + runtime->hw = SNDRV_PCM_STREAM_PLAYBACK == substream->stream ? snd_usX2Y_2c : + (subs->usX2Y->subs[3] ? snd_usX2Y_4c : snd_usX2Y_2c); + runtime->private_data = subs; + subs->pcm_substream = substream; + snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000); + return 0; +} + + +static int snd_usX2Y_usbpcm_close(snd_pcm_substream_t *substream) +{ + snd_pcm_runtime_t *runtime = substream->runtime; + snd_usX2Y_substream_t *subs = (snd_usX2Y_substream_t *)runtime->private_data; + int err = 0; + snd_printd("\n"); + subs->pcm_substream = NULL; + return err; +} + + +static snd_pcm_ops_t snd_usX2Y_usbpcm_ops = +{ + .open = snd_usX2Y_usbpcm_open, + .close = snd_usX2Y_usbpcm_close, + .ioctl = snd_pcm_lib_ioctl, + .hw_params = snd_usX2Y_pcm_hw_params, + .hw_free = snd_usX2Y_usbpcm_hw_free, + .prepare = snd_usX2Y_usbpcm_prepare, + .trigger = snd_usX2Y_pcm_trigger, + .pointer = snd_usX2Y_pcm_pointer, +}; + + +static int usX2Y_pcms_lock_check(snd_card_t *card) +{ + struct list_head *list; + snd_device_t *dev; + snd_pcm_t *pcm; + int err = 0; + list_for_each(list, &card->devices) { + dev = snd_device(list); + if (dev->type != SNDRV_DEV_PCM) + continue; + pcm = dev->device_data; + down(&pcm->open_mutex); + } + list_for_each(list, &card->devices) { + int s; + dev = snd_device(list); + if (dev->type != SNDRV_DEV_PCM) + continue; + pcm = dev->device_data; + for (s = 0; s < 2; ++s) { + snd_pcm_substream_t *substream; + substream = pcm->streams[s].substream; + if (substream && substream->open_flag) + err = -EBUSY; + } + } + return err; +} + + +static void usX2Y_pcms_unlock(snd_card_t *card) +{ + struct list_head *list; + snd_device_t *dev; + snd_pcm_t *pcm; + list_for_each(list, &card->devices) { + dev = snd_device(list); + if (dev->type != SNDRV_DEV_PCM) + continue; + pcm = dev->device_data; + up(&pcm->open_mutex); + } +} + + +static int snd_usX2Y_hwdep_pcm_open(snd_hwdep_t *hw, struct file *file) +{ + // we need to be the first + snd_card_t *card = hw->card; + int err = usX2Y_pcms_lock_check(card); + if (0 == err) + usX2Y(card)->chip_status |= USX2Y_STAT_CHIP_MMAP_PCM_URBS; + usX2Y_pcms_unlock(card); + return err; +} + + +static int snd_usX2Y_hwdep_pcm_release(snd_hwdep_t *hw, struct file *file) +{ + snd_card_t *card = hw->card; + int err = usX2Y_pcms_lock_check(card); + if (0 == err) + usX2Y(hw->card)->chip_status &= ~USX2Y_STAT_CHIP_MMAP_PCM_URBS; + usX2Y_pcms_unlock(card); + return err; +} + + +static void snd_usX2Y_hwdep_pcm_vm_open(struct vm_area_struct *area) +{ +} + + +static void snd_usX2Y_hwdep_pcm_vm_close(struct vm_area_struct *area) +{ +} + + +static struct page * snd_usX2Y_hwdep_pcm_vm_nopage(struct vm_area_struct *area, unsigned long address, int *type) +{ + unsigned long offset; + struct page *page; + void *vaddr; + + offset = area->vm_pgoff << PAGE_SHIFT; + offset += address - area->vm_start; + snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM); + vaddr = (char*)((usX2Ydev_t*)area->vm_private_data)->hwdep_pcm_shm + offset; + page = virt_to_page(vaddr); + + if (type) + *type = VM_FAULT_MINOR; + + return page; +} + + +static struct vm_operations_struct snd_usX2Y_hwdep_pcm_vm_ops = { + .open = snd_usX2Y_hwdep_pcm_vm_open, + .close = snd_usX2Y_hwdep_pcm_vm_close, + .nopage = snd_usX2Y_hwdep_pcm_vm_nopage, +}; + + +static int snd_usX2Y_hwdep_pcm_mmap(snd_hwdep_t * hw, struct file *filp, struct vm_area_struct *area) +{ + unsigned long size = (unsigned long)(area->vm_end - area->vm_start); + usX2Ydev_t *usX2Y = (usX2Ydev_t*)hw->private_data; + + if (!(((usX2Ydev_t*)hw->private_data)->chip_status & USX2Y_STAT_CHIP_INIT)) + return -EBUSY; + + /* if userspace tries to mmap beyond end of our buffer, fail */ + if (size > PAGE_ALIGN(sizeof(snd_usX2Y_hwdep_pcm_shm_t))) { + snd_printd("%lu > %lu\n", size, (unsigned long)sizeof(snd_usX2Y_hwdep_pcm_shm_t)); + return -EINVAL; + } + + if (!usX2Y->hwdep_pcm_shm) { + return -ENODEV; + } + area->vm_ops = &snd_usX2Y_hwdep_pcm_vm_ops; + area->vm_flags |= VM_RESERVED; + snd_printd("vm_flags=0x%lX\n", area->vm_flags); + area->vm_private_data = hw->private_data; + return 0; +} + + +static void snd_usX2Y_hwdep_pcm_private_free(snd_hwdep_t *hwdep) +{ + usX2Ydev_t *usX2Y = (usX2Ydev_t *)hwdep->private_data; + if (NULL != usX2Y->hwdep_pcm_shm) + snd_free_pages(usX2Y->hwdep_pcm_shm, sizeof(snd_usX2Y_hwdep_pcm_shm_t)); +} + + +static void snd_usX2Y_usbpcm_private_free(snd_pcm_t *pcm) +{ + snd_pcm_lib_preallocate_free_for_all(pcm); +} + + +int usX2Y_hwdep_pcm_new(snd_card_t* card) +{ + int err; + snd_hwdep_t *hw; + snd_pcm_t *pcm; + struct usb_device *dev = usX2Y(card)->chip.dev; + if (1 != nr_of_packs()) + return 0; + + if ((err = snd_hwdep_new(card, SND_USX2Y_USBPCM_ID, 1, &hw)) < 0) { + snd_printd("\n"); + return err; + } + hw->iface = SNDRV_HWDEP_IFACE_USX2Y_PCM; + hw->private_data = usX2Y(card); + hw->private_free = snd_usX2Y_hwdep_pcm_private_free; + hw->ops.open = snd_usX2Y_hwdep_pcm_open; + hw->ops.release = snd_usX2Y_hwdep_pcm_release; + hw->ops.mmap = snd_usX2Y_hwdep_pcm_mmap; + hw->exclusive = 1; + sprintf(hw->name, "/proc/bus/usb/%03d/%03d/hwdeppcm", dev->bus->busnum, dev->devnum); + + err = snd_pcm_new(card, NAME_ALLCAPS" hwdep Audio", 2, 1, 1, &pcm); + if (err < 0) { + return err; + } + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usX2Y_usbpcm_ops); + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usX2Y_usbpcm_ops); + + pcm->private_data = usX2Y(card)->subs; + pcm->private_free = snd_usX2Y_usbpcm_private_free; + pcm->info_flags = 0; + + sprintf(pcm->name, NAME_ALLCAPS" hwdep Audio"); + if (0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream, + SNDRV_DMA_TYPE_CONTINUOUS, + snd_dma_continuous_data(GFP_KERNEL), + 64*1024, 128*1024)) || + 0 > (err = snd_pcm_lib_preallocate_pages(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream, + SNDRV_DMA_TYPE_CONTINUOUS, + snd_dma_continuous_data(GFP_KERNEL), + 64*1024, 128*1024))) { + snd_usX2Y_usbpcm_private_free(pcm); + return err; + } + + + return 0; +} + +#else + +int usX2Y_hwdep_pcm_new(snd_card_t* card) +{ + return 0; +} + +#endif diff --git a/sound/usb/usx2y/usx2yhwdeppcm.h b/sound/usb/usx2y/usx2yhwdeppcm.h new file mode 100644 index 000000000000..d68f0cbdbbe2 --- /dev/null +++ b/sound/usb/usx2y/usx2yhwdeppcm.h @@ -0,0 +1,21 @@ +#define MAXPACK 50 +#define MAXBUFFERMS 100 +#define MAXSTRIDE 3 + +#define SSS (((MAXPACK*MAXBUFFERMS*MAXSTRIDE + 4096) / 4096) * 4096) +struct snd_usX2Y_hwdep_pcm_shm { + char playback[SSS]; + char capture0x8[SSS]; + char capture0xA[SSS]; + volatile int playback_iso_head; + int playback_iso_start; + struct { + int frame, + offset, + length; + } captured_iso[128]; + volatile int captured_iso_head; + volatile unsigned captured_iso_frames; + int capture_iso_start; +}; +typedef struct snd_usX2Y_hwdep_pcm_shm snd_usX2Y_hwdep_pcm_shm_t; |