blob: a26d8d83d3eb85893564463681bc06247dcbe364 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070023#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 Iwaibbe85bb2005-11-17 15:08:26 +010033int usX2Y_hwdep_pcm_new(struct snd_card *card);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35
Nick Piggineb415b82007-12-13 16:16:40 +010036static int snd_us428ctls_vm_fault(struct vm_area_struct *area,
37 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 unsigned long offset;
40 struct page * page;
41 void *vaddr;
42
Nick Piggineb415b82007-12-13 16:16:40 +010043 snd_printdd("ENTER, start %lXh, pgoff %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 area->vm_start,
Nick Piggineb415b82007-12-13 16:16:40 +010045 vmf->pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Nick Piggineb415b82007-12-13 16:16:40 +010047 offset = vmf->pgoff << PAGE_SHIFT;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010048 vaddr = (char*)((struct usX2Ydev *)area->vm_private_data)->us428ctls_sharedmem + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 page = virt_to_page(vaddr);
50 get_page(page);
Nick Piggineb415b82007-12-13 16:16:40 +010051 vmf->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Nick Piggineb415b82007-12-13 16:16:40 +010053 snd_printdd("vaddr=%p made us428ctls_vm_fault() page %p\n",
54 vaddr, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Nick Piggineb415b82007-12-13 16:16:40 +010056 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59static struct vm_operations_struct us428ctls_vm_ops = {
Nick Piggineb415b82007-12-13 16:16:40 +010060 .fault = snd_us428ctls_vm_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010063static int snd_us428ctls_mmap(struct snd_hwdep * hw, struct file *filp, struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 unsigned long size = (unsigned long)(area->vm_end - area->vm_start);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010066 struct usX2Ydev *us428 = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
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 Iwaicb432372005-11-17 10:48:52 +010070 if (!(us428->chip_status & USX2Y_STAT_CHIP_INIT))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return -EBUSY;
72
73 /* if userspace tries to mmap beyond end of our buffer, fail */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010074 if (size > PAGE_ALIGN(sizeof(struct us428ctls_sharedmem))) {
75 snd_printd( "%lu > %lu\n", size, (unsigned long)sizeof(struct us428ctls_sharedmem));
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return -EINVAL;
77 }
78
79 if (!us428->us428ctls_sharedmem) {
80 init_waitqueue_head(&us428->us428ctls_wait_queue_head);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010081 if(!(us428->us428ctls_sharedmem = snd_malloc_pages(sizeof(struct us428ctls_sharedmem), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return -ENOMEM;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010083 memset(us428->us428ctls_sharedmem, -1, sizeof(struct us428ctls_sharedmem));
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 us428->us428ctls_sharedmem->CtlSnapShotLast = -2;
85 }
86 area->vm_ops = &us428ctls_vm_ops;
Nick Piggin2f987352008-02-02 03:08:53 +010087 area->vm_flags |= VM_RESERVED | VM_DONTEXPAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 area->vm_private_data = hw->private_data;
89 return 0;
90}
91
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010092static unsigned int snd_us428ctls_poll(struct snd_hwdep *hw, struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 unsigned int mask = 0;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010095 struct usX2Ydev *us428 = hw->private_data;
96 struct us428ctls_sharedmem *shm = us428->us428ctls_sharedmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 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 Iwaibbe85bb2005-11-17 15:08:26 +0100109static int snd_usX2Y_hwdep_dsp_status(struct snd_hwdep *hw,
110 struct snd_hwdep_dsp_status *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 static char *type_ids[USX2Y_TYPE_NUMS] = {
113 [USX2Y_TYPE_122] = "us122",
114 [USX2Y_TYPE_224] = "us224",
115 [USX2Y_TYPE_428] = "us428",
116 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100117 struct usX2Ydev *us428 = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 int id = -1;
119
Takashi Iwaicb432372005-11-17 10:48:52 +0100120 switch (le16_to_cpu(us428->chip.dev->descriptor.idProduct)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 case USB_ID_US122:
122 id = USX2Y_TYPE_122;
123 break;
124 case USB_ID_US224:
125 id = USX2Y_TYPE_224;
126 break;
127 case USB_ID_US428:
128 id = USX2Y_TYPE_428;
129 break;
130 }
131 if (0 > id)
132 return -ENODEV;
133 strcpy(info->id, type_ids[id]);
134 info->num_dsps = 2; // 0: Prepad Data, 1: FPGA Code
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100135 if (us428->chip_status & USX2Y_STAT_CHIP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 info->chip_ready = 1;
137 info->version = USX2Y_DRIVER_VERSION;
138 return 0;
139}
140
141
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100142static int usX2Y_create_usbmidi(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100144 static struct snd_usb_midi_endpoint_info quirk_data_1 = {
145 .out_ep = 0x06,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .in_ep = 0x06,
147 .out_cables = 0x001,
148 .in_cables = 0x001
149 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100150 static struct snd_usb_audio_quirk quirk_1 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 .vendor_name = "TASCAM",
152 .product_name = NAME_ALLCAPS,
153 .ifnum = 0,
154 .type = QUIRK_MIDI_FIXED_ENDPOINT,
155 .data = &quirk_data_1
156 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100157 static struct snd_usb_midi_endpoint_info quirk_data_2 = {
158 .out_ep = 0x06,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 .in_ep = 0x06,
160 .out_cables = 0x003,
161 .in_cables = 0x003
162 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100163 static struct snd_usb_audio_quirk quirk_2 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 .vendor_name = "TASCAM",
165 .product_name = "US428",
166 .ifnum = 0,
167 .type = QUIRK_MIDI_FIXED_ENDPOINT,
168 .data = &quirk_data_2
169 };
170 struct usb_device *dev = usX2Y(card)->chip.dev;
171 struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100172 struct snd_usb_audio_quirk *quirk =
173 le16_to_cpu(dev->descriptor.idProduct) == USB_ID_US428 ?
174 &quirk_2 : &quirk_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 snd_printdd("usX2Y_create_usbmidi \n");
177 return snd_usb_create_midi_interface(&usX2Y(card)->chip, iface, quirk);
178}
179
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100180static int usX2Y_create_alsa_devices(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 int err;
183
184 do {
185 if ((err = usX2Y_create_usbmidi(card)) < 0) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200186 snd_printk(KERN_ERR "usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 break;
188 }
189 if ((err = usX2Y_audio_create(card)) < 0)
190 break;
191 if ((err = usX2Y_hwdep_pcm_new(card)) < 0)
192 break;
193 if ((err = snd_card_register(card)) < 0)
194 break;
195 } while (0);
196
197 return err;
198}
199
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100200static int snd_usX2Y_hwdep_dsp_load(struct snd_hwdep *hw,
201 struct snd_hwdep_dsp_image *dsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100203 struct usX2Ydev *priv = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 int lret, err = -EINVAL;
205 snd_printdd( "dsp_load %s\n", dsp->name);
206
207 if (access_ok(VERIFY_READ, dsp->image, dsp->length)) {
208 struct usb_device* dev = priv->chip.dev;
209 char *buf = kmalloc(dsp->length, GFP_KERNEL);
210 if (!buf)
211 return -ENOMEM;
212 if (copy_from_user(buf, dsp->image, dsp->length)) {
213 kfree(buf);
214 return -EFAULT;
215 }
216 err = usb_set_interface(dev, 0, 1);
217 if (err)
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200218 snd_printk(KERN_ERR "usb_set_interface error \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 else
220 err = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 2), buf, dsp->length, &lret, 6000);
221 kfree(buf);
222 }
223 if (err)
224 return err;
225 if (dsp->index == 1) {
Nishanth Aravamudanb27c1872005-07-09 10:54:37 +0200226 msleep(250); // give the device some time
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 err = usX2Y_AsyncSeq04_init(priv);
228 if (err) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200229 snd_printk(KERN_ERR "usX2Y_AsyncSeq04_init error \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return err;
231 }
232 err = usX2Y_In04_init(priv);
233 if (err) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200234 snd_printk(KERN_ERR "usX2Y_In04_init error \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return err;
236 }
237 err = usX2Y_create_alsa_devices(hw->card);
238 if (err) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200239 snd_printk(KERN_ERR "usX2Y_create_alsa_devices error %i \n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 snd_card_free(hw->card);
241 return err;
242 }
243 priv->chip_status |= USX2Y_STAT_CHIP_INIT;
244 snd_printdd("%s: alsa all started\n", hw->name);
245 }
246 return err;
247}
248
249
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100250int usX2Y_hwdep_new(struct snd_card *card, struct usb_device* device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 int err;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100253 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 if ((err = snd_hwdep_new(card, SND_USX2Y_LOADER_ID, 0, &hw)) < 0)
256 return err;
257
258 hw->iface = SNDRV_HWDEP_IFACE_USX2Y;
259 hw->private_data = usX2Y(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 hw->ops.dsp_status = snd_usX2Y_hwdep_dsp_status;
261 hw->ops.dsp_load = snd_usX2Y_hwdep_dsp_load;
262 hw->ops.mmap = snd_us428ctls_mmap;
263 hw->ops.poll = snd_us428ctls_poll;
264 hw->exclusive = 1;
265 sprintf(hw->name, "/proc/bus/usb/%03d/%03d", device->bus->busnum, device->devnum);
266 return 0;
267}
268