Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Tascam US-X2Y USB soundcards |
| 3 | * |
| 4 | * FPGA Loader + ALSA Startup |
| 5 | * |
| 6 | * Copyright (c) 2003 by Karsten Wiese <annabellesgarden@yahoo.de> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/usb.h> |
| 25 | #include <sound/core.h> |
| 26 | #include <sound/memalloc.h> |
| 27 | #include <sound/pcm.h> |
| 28 | #include <sound/hwdep.h> |
| 29 | #include "usx2y.h" |
| 30 | #include "usbusx2y.h" |
| 31 | #include "usX2Yhwdep.h" |
| 32 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 33 | int usX2Y_hwdep_pcm_new(struct snd_card *card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | |
Nick Piggin | eb415b8 | 2007-12-13 16:16:40 +0100 | [diff] [blame] | 36 | static int snd_us428ctls_vm_fault(struct vm_area_struct *area, |
| 37 | struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
| 39 | unsigned long offset; |
| 40 | struct page * page; |
| 41 | void *vaddr; |
| 42 | |
Nick Piggin | eb415b8 | 2007-12-13 16:16:40 +0100 | [diff] [blame] | 43 | snd_printdd("ENTER, start %lXh, pgoff %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | area->vm_start, |
Nick Piggin | eb415b8 | 2007-12-13 16:16:40 +0100 | [diff] [blame] | 45 | vmf->pgoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Nick Piggin | eb415b8 | 2007-12-13 16:16:40 +0100 | [diff] [blame] | 47 | offset = vmf->pgoff << PAGE_SHIFT; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 48 | vaddr = (char*)((struct usX2Ydev *)area->vm_private_data)->us428ctls_sharedmem + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | page = virt_to_page(vaddr); |
| 50 | get_page(page); |
Nick Piggin | eb415b8 | 2007-12-13 16:16:40 +0100 | [diff] [blame] | 51 | vmf->page = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Nick Piggin | eb415b8 | 2007-12-13 16:16:40 +0100 | [diff] [blame] | 53 | snd_printdd("vaddr=%p made us428ctls_vm_fault() page %p\n", |
| 54 | vaddr, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Nick Piggin | eb415b8 | 2007-12-13 16:16:40 +0100 | [diff] [blame] | 56 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static struct vm_operations_struct us428ctls_vm_ops = { |
Nick Piggin | eb415b8 | 2007-12-13 16:16:40 +0100 | [diff] [blame] | 60 | .fault = snd_us428ctls_vm_fault, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 63 | static int snd_us428ctls_mmap(struct snd_hwdep * hw, struct file *filp, struct vm_area_struct *area) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
| 65 | unsigned long size = (unsigned long)(area->vm_end - area->vm_start); |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 66 | struct usX2Ydev *us428 = hw->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | // FIXME this hwdep interface is used twice: fpga download and mmap for controlling Lights etc. Maybe better using 2 hwdep devs? |
| 69 | // so as long as the device isn't fully initialised yet we return -EBUSY here. |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 70 | if (!(us428->chip_status & USX2Y_STAT_CHIP_INIT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | return -EBUSY; |
| 72 | |
| 73 | /* if userspace tries to mmap beyond end of our buffer, fail */ |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 74 | if (size > PAGE_ALIGN(sizeof(struct us428ctls_sharedmem))) { |
| 75 | snd_printd( "%lu > %lu\n", size, (unsigned long)sizeof(struct us428ctls_sharedmem)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | return -EINVAL; |
| 77 | } |
| 78 | |
| 79 | if (!us428->us428ctls_sharedmem) { |
| 80 | init_waitqueue_head(&us428->us428ctls_wait_queue_head); |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 81 | if(!(us428->us428ctls_sharedmem = snd_malloc_pages(sizeof(struct us428ctls_sharedmem), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | return -ENOMEM; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 83 | memset(us428->us428ctls_sharedmem, -1, sizeof(struct us428ctls_sharedmem)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | us428->us428ctls_sharedmem->CtlSnapShotLast = -2; |
| 85 | } |
| 86 | area->vm_ops = &us428ctls_vm_ops; |
Nick Piggin | 2f98735 | 2008-02-02 03:08:53 +0100 | [diff] [blame^] | 87 | area->vm_flags |= VM_RESERVED | VM_DONTEXPAND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | area->vm_private_data = hw->private_data; |
| 89 | return 0; |
| 90 | } |
| 91 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 92 | static unsigned int snd_us428ctls_poll(struct snd_hwdep *hw, struct file *file, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
| 94 | unsigned int mask = 0; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 95 | struct usX2Ydev *us428 = hw->private_data; |
| 96 | struct us428ctls_sharedmem *shm = us428->us428ctls_sharedmem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | if (us428->chip_status & USX2Y_STAT_CHIP_HUP) |
| 98 | return POLLHUP; |
| 99 | |
| 100 | poll_wait(file, &us428->us428ctls_wait_queue_head, wait); |
| 101 | |
| 102 | if (shm != NULL && shm->CtlSnapShotLast != shm->CtlSnapShotRed) |
| 103 | mask |= POLLIN; |
| 104 | |
| 105 | return mask; |
| 106 | } |
| 107 | |
| 108 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 109 | static int snd_usX2Y_hwdep_open(struct snd_hwdep *hw, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | return 0; |
| 112 | } |
| 113 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 114 | static int snd_usX2Y_hwdep_release(struct snd_hwdep *hw, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
| 116 | return 0; |
| 117 | } |
| 118 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 119 | static int snd_usX2Y_hwdep_dsp_status(struct snd_hwdep *hw, |
| 120 | struct snd_hwdep_dsp_status *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
| 122 | static char *type_ids[USX2Y_TYPE_NUMS] = { |
| 123 | [USX2Y_TYPE_122] = "us122", |
| 124 | [USX2Y_TYPE_224] = "us224", |
| 125 | [USX2Y_TYPE_428] = "us428", |
| 126 | }; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 127 | struct usX2Ydev *us428 = hw->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | int id = -1; |
| 129 | |
Takashi Iwai | cb43237 | 2005-11-17 10:48:52 +0100 | [diff] [blame] | 130 | switch (le16_to_cpu(us428->chip.dev->descriptor.idProduct)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | case USB_ID_US122: |
| 132 | id = USX2Y_TYPE_122; |
| 133 | break; |
| 134 | case USB_ID_US224: |
| 135 | id = USX2Y_TYPE_224; |
| 136 | break; |
| 137 | case USB_ID_US428: |
| 138 | id = USX2Y_TYPE_428; |
| 139 | break; |
| 140 | } |
| 141 | if (0 > id) |
| 142 | return -ENODEV; |
| 143 | strcpy(info->id, type_ids[id]); |
| 144 | info->num_dsps = 2; // 0: Prepad Data, 1: FPGA Code |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 145 | if (us428->chip_status & USX2Y_STAT_CHIP_INIT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | info->chip_ready = 1; |
| 147 | info->version = USX2Y_DRIVER_VERSION; |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 152 | static int usX2Y_create_usbmidi(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 154 | static struct snd_usb_midi_endpoint_info quirk_data_1 = { |
| 155 | .out_ep = 0x06, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | .in_ep = 0x06, |
| 157 | .out_cables = 0x001, |
| 158 | .in_cables = 0x001 |
| 159 | }; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 160 | static struct snd_usb_audio_quirk quirk_1 = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | .vendor_name = "TASCAM", |
| 162 | .product_name = NAME_ALLCAPS, |
| 163 | .ifnum = 0, |
| 164 | .type = QUIRK_MIDI_FIXED_ENDPOINT, |
| 165 | .data = &quirk_data_1 |
| 166 | }; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 167 | static struct snd_usb_midi_endpoint_info quirk_data_2 = { |
| 168 | .out_ep = 0x06, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | .in_ep = 0x06, |
| 170 | .out_cables = 0x003, |
| 171 | .in_cables = 0x003 |
| 172 | }; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 173 | static struct snd_usb_audio_quirk quirk_2 = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | .vendor_name = "TASCAM", |
| 175 | .product_name = "US428", |
| 176 | .ifnum = 0, |
| 177 | .type = QUIRK_MIDI_FIXED_ENDPOINT, |
| 178 | .data = &quirk_data_2 |
| 179 | }; |
| 180 | struct usb_device *dev = usX2Y(card)->chip.dev; |
| 181 | struct usb_interface *iface = usb_ifnum_to_if(dev, 0); |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 182 | struct snd_usb_audio_quirk *quirk = |
| 183 | le16_to_cpu(dev->descriptor.idProduct) == USB_ID_US428 ? |
| 184 | &quirk_2 : &quirk_1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | snd_printdd("usX2Y_create_usbmidi \n"); |
| 187 | return snd_usb_create_midi_interface(&usX2Y(card)->chip, iface, quirk); |
| 188 | } |
| 189 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 190 | static int usX2Y_create_alsa_devices(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
| 192 | int err; |
| 193 | |
| 194 | do { |
| 195 | if ((err = usX2Y_create_usbmidi(card)) < 0) { |
Takashi Iwai | d3d579f | 2005-10-21 16:20:11 +0200 | [diff] [blame] | 196 | snd_printk(KERN_ERR "usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | break; |
| 198 | } |
| 199 | if ((err = usX2Y_audio_create(card)) < 0) |
| 200 | break; |
| 201 | if ((err = usX2Y_hwdep_pcm_new(card)) < 0) |
| 202 | break; |
| 203 | if ((err = snd_card_register(card)) < 0) |
| 204 | break; |
| 205 | } while (0); |
| 206 | |
| 207 | return err; |
| 208 | } |
| 209 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 210 | static int snd_usX2Y_hwdep_dsp_load(struct snd_hwdep *hw, |
| 211 | struct snd_hwdep_dsp_image *dsp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 213 | struct usX2Ydev *priv = hw->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | int lret, err = -EINVAL; |
| 215 | snd_printdd( "dsp_load %s\n", dsp->name); |
| 216 | |
| 217 | if (access_ok(VERIFY_READ, dsp->image, dsp->length)) { |
| 218 | struct usb_device* dev = priv->chip.dev; |
| 219 | char *buf = kmalloc(dsp->length, GFP_KERNEL); |
| 220 | if (!buf) |
| 221 | return -ENOMEM; |
| 222 | if (copy_from_user(buf, dsp->image, dsp->length)) { |
| 223 | kfree(buf); |
| 224 | return -EFAULT; |
| 225 | } |
| 226 | err = usb_set_interface(dev, 0, 1); |
| 227 | if (err) |
Takashi Iwai | d3d579f | 2005-10-21 16:20:11 +0200 | [diff] [blame] | 228 | snd_printk(KERN_ERR "usb_set_interface error \n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | else |
| 230 | err = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 2), buf, dsp->length, &lret, 6000); |
| 231 | kfree(buf); |
| 232 | } |
| 233 | if (err) |
| 234 | return err; |
| 235 | if (dsp->index == 1) { |
Nishanth Aravamudan | b27c187 | 2005-07-09 10:54:37 +0200 | [diff] [blame] | 236 | msleep(250); // give the device some time |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | err = usX2Y_AsyncSeq04_init(priv); |
| 238 | if (err) { |
Takashi Iwai | d3d579f | 2005-10-21 16:20:11 +0200 | [diff] [blame] | 239 | snd_printk(KERN_ERR "usX2Y_AsyncSeq04_init error \n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | return err; |
| 241 | } |
| 242 | err = usX2Y_In04_init(priv); |
| 243 | if (err) { |
Takashi Iwai | d3d579f | 2005-10-21 16:20:11 +0200 | [diff] [blame] | 244 | snd_printk(KERN_ERR "usX2Y_In04_init error \n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | return err; |
| 246 | } |
| 247 | err = usX2Y_create_alsa_devices(hw->card); |
| 248 | if (err) { |
Takashi Iwai | d3d579f | 2005-10-21 16:20:11 +0200 | [diff] [blame] | 249 | snd_printk(KERN_ERR "usX2Y_create_alsa_devices error %i \n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | snd_card_free(hw->card); |
| 251 | return err; |
| 252 | } |
| 253 | priv->chip_status |= USX2Y_STAT_CHIP_INIT; |
| 254 | snd_printdd("%s: alsa all started\n", hw->name); |
| 255 | } |
| 256 | return err; |
| 257 | } |
| 258 | |
| 259 | |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 260 | int usX2Y_hwdep_new(struct snd_card *card, struct usb_device* device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
| 262 | int err; |
Takashi Iwai | bbe85bb | 2005-11-17 15:08:26 +0100 | [diff] [blame] | 263 | struct snd_hwdep *hw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
| 265 | if ((err = snd_hwdep_new(card, SND_USX2Y_LOADER_ID, 0, &hw)) < 0) |
| 266 | return err; |
| 267 | |
| 268 | hw->iface = SNDRV_HWDEP_IFACE_USX2Y; |
| 269 | hw->private_data = usX2Y(card); |
| 270 | hw->ops.open = snd_usX2Y_hwdep_open; |
| 271 | hw->ops.release = snd_usX2Y_hwdep_release; |
| 272 | hw->ops.dsp_status = snd_usX2Y_hwdep_dsp_status; |
| 273 | hw->ops.dsp_load = snd_usX2Y_hwdep_dsp_load; |
| 274 | hw->ops.mmap = snd_us428ctls_mmap; |
| 275 | hw->ops.poll = snd_us428ctls_poll; |
| 276 | hw->exclusive = 1; |
| 277 | sprintf(hw->name, "/proc/bus/usb/%03d/%03d", device->bus->busnum, device->devnum); |
| 278 | return 0; |
| 279 | } |
| 280 | |