blob: b76b3dd9df25d48d878548238f040018c443b075 [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
23#include <sound/driver.h>
24#include <linux/interrupt.h>
25#include <linux/usb.h>
26#include <sound/core.h>
27#include <sound/memalloc.h>
28#include <sound/pcm.h>
29#include <sound/hwdep.h>
30#include "usx2y.h"
31#include "usbusx2y.h"
32#include "usX2Yhwdep.h"
33
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010034int usX2Y_hwdep_pcm_new(struct snd_card *card);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36
37static struct page * snd_us428ctls_vm_nopage(struct vm_area_struct *area, unsigned long address, int *type)
38{
39 unsigned long offset;
40 struct page * page;
41 void *vaddr;
42
43 snd_printdd("ENTER, start %lXh, ofs %lXh, pgoff %ld, addr %lXh\n",
44 area->vm_start,
45 address - area->vm_start,
46 (address - area->vm_start) >> PAGE_SHIFT,
47 address);
48
49 offset = area->vm_pgoff << PAGE_SHIFT;
50 offset += address - area->vm_start;
Nick Piggincd54e7e2006-12-06 20:31:53 -080051 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_SIGBUS);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010052 vaddr = (char*)((struct usX2Ydev *)area->vm_private_data)->us428ctls_sharedmem + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 page = virt_to_page(vaddr);
54 get_page(page);
55 snd_printdd( "vaddr=%p made us428ctls_vm_nopage() return %p; offset=%lX\n", vaddr, page, offset);
56
57 if (type)
58 *type = VM_FAULT_MINOR;
59
60 return page;
61}
62
63static struct vm_operations_struct us428ctls_vm_ops = {
64 .nopage = snd_us428ctls_vm_nopage,
65};
66
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010067static int snd_us428ctls_mmap(struct snd_hwdep * hw, struct file *filp, struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 unsigned long size = (unsigned long)(area->vm_end - area->vm_start);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010070 struct usX2Ydev *us428 = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 // FIXME this hwdep interface is used twice: fpga download and mmap for controlling Lights etc. Maybe better using 2 hwdep devs?
73 // so as long as the device isn't fully initialised yet we return -EBUSY here.
Takashi Iwaicb432372005-11-17 10:48:52 +010074 if (!(us428->chip_status & USX2Y_STAT_CHIP_INIT))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return -EBUSY;
76
77 /* if userspace tries to mmap beyond end of our buffer, fail */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010078 if (size > PAGE_ALIGN(sizeof(struct us428ctls_sharedmem))) {
79 snd_printd( "%lu > %lu\n", size, (unsigned long)sizeof(struct us428ctls_sharedmem));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EINVAL;
81 }
82
83 if (!us428->us428ctls_sharedmem) {
84 init_waitqueue_head(&us428->us428ctls_wait_queue_head);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010085 if(!(us428->us428ctls_sharedmem = snd_malloc_pages(sizeof(struct us428ctls_sharedmem), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return -ENOMEM;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010087 memset(us428->us428ctls_sharedmem, -1, sizeof(struct us428ctls_sharedmem));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 us428->us428ctls_sharedmem->CtlSnapShotLast = -2;
89 }
90 area->vm_ops = &us428ctls_vm_ops;
91 area->vm_flags |= VM_RESERVED;
92 area->vm_private_data = hw->private_data;
93 return 0;
94}
95
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010096static unsigned int snd_us428ctls_poll(struct snd_hwdep *hw, struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 unsigned int mask = 0;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010099 struct usX2Ydev *us428 = hw->private_data;
100 struct us428ctls_sharedmem *shm = us428->us428ctls_sharedmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (us428->chip_status & USX2Y_STAT_CHIP_HUP)
102 return POLLHUP;
103
104 poll_wait(file, &us428->us428ctls_wait_queue_head, wait);
105
106 if (shm != NULL && shm->CtlSnapShotLast != shm->CtlSnapShotRed)
107 mask |= POLLIN;
108
109 return mask;
110}
111
112
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100113static int snd_usX2Y_hwdep_open(struct snd_hwdep *hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 return 0;
116}
117
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100118static int snd_usX2Y_hwdep_release(struct snd_hwdep *hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 return 0;
121}
122
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100123static int snd_usX2Y_hwdep_dsp_status(struct snd_hwdep *hw,
124 struct snd_hwdep_dsp_status *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 static char *type_ids[USX2Y_TYPE_NUMS] = {
127 [USX2Y_TYPE_122] = "us122",
128 [USX2Y_TYPE_224] = "us224",
129 [USX2Y_TYPE_428] = "us428",
130 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100131 struct usX2Ydev *us428 = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int id = -1;
133
Takashi Iwaicb432372005-11-17 10:48:52 +0100134 switch (le16_to_cpu(us428->chip.dev->descriptor.idProduct)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 case USB_ID_US122:
136 id = USX2Y_TYPE_122;
137 break;
138 case USB_ID_US224:
139 id = USX2Y_TYPE_224;
140 break;
141 case USB_ID_US428:
142 id = USX2Y_TYPE_428;
143 break;
144 }
145 if (0 > id)
146 return -ENODEV;
147 strcpy(info->id, type_ids[id]);
148 info->num_dsps = 2; // 0: Prepad Data, 1: FPGA Code
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100149 if (us428->chip_status & USX2Y_STAT_CHIP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 info->chip_ready = 1;
151 info->version = USX2Y_DRIVER_VERSION;
152 return 0;
153}
154
155
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100156static int usX2Y_create_usbmidi(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100158 static struct snd_usb_midi_endpoint_info quirk_data_1 = {
159 .out_ep = 0x06,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 .in_ep = 0x06,
161 .out_cables = 0x001,
162 .in_cables = 0x001
163 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100164 static struct snd_usb_audio_quirk quirk_1 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .vendor_name = "TASCAM",
166 .product_name = NAME_ALLCAPS,
167 .ifnum = 0,
168 .type = QUIRK_MIDI_FIXED_ENDPOINT,
169 .data = &quirk_data_1
170 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100171 static struct snd_usb_midi_endpoint_info quirk_data_2 = {
172 .out_ep = 0x06,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 .in_ep = 0x06,
174 .out_cables = 0x003,
175 .in_cables = 0x003
176 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100177 static struct snd_usb_audio_quirk quirk_2 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 .vendor_name = "TASCAM",
179 .product_name = "US428",
180 .ifnum = 0,
181 .type = QUIRK_MIDI_FIXED_ENDPOINT,
182 .data = &quirk_data_2
183 };
184 struct usb_device *dev = usX2Y(card)->chip.dev;
185 struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100186 struct snd_usb_audio_quirk *quirk =
187 le16_to_cpu(dev->descriptor.idProduct) == USB_ID_US428 ?
188 &quirk_2 : &quirk_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 snd_printdd("usX2Y_create_usbmidi \n");
191 return snd_usb_create_midi_interface(&usX2Y(card)->chip, iface, quirk);
192}
193
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100194static int usX2Y_create_alsa_devices(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 int err;
197
198 do {
199 if ((err = usX2Y_create_usbmidi(card)) < 0) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200200 snd_printk(KERN_ERR "usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 break;
202 }
203 if ((err = usX2Y_audio_create(card)) < 0)
204 break;
205 if ((err = usX2Y_hwdep_pcm_new(card)) < 0)
206 break;
207 if ((err = snd_card_register(card)) < 0)
208 break;
209 } while (0);
210
211 return err;
212}
213
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100214static int snd_usX2Y_hwdep_dsp_load(struct snd_hwdep *hw,
215 struct snd_hwdep_dsp_image *dsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100217 struct usX2Ydev *priv = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 int lret, err = -EINVAL;
219 snd_printdd( "dsp_load %s\n", dsp->name);
220
221 if (access_ok(VERIFY_READ, dsp->image, dsp->length)) {
222 struct usb_device* dev = priv->chip.dev;
223 char *buf = kmalloc(dsp->length, GFP_KERNEL);
224 if (!buf)
225 return -ENOMEM;
226 if (copy_from_user(buf, dsp->image, dsp->length)) {
227 kfree(buf);
228 return -EFAULT;
229 }
230 err = usb_set_interface(dev, 0, 1);
231 if (err)
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200232 snd_printk(KERN_ERR "usb_set_interface error \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 else
234 err = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 2), buf, dsp->length, &lret, 6000);
235 kfree(buf);
236 }
237 if (err)
238 return err;
239 if (dsp->index == 1) {
Nishanth Aravamudanb27c1872005-07-09 10:54:37 +0200240 msleep(250); // give the device some time
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 err = usX2Y_AsyncSeq04_init(priv);
242 if (err) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200243 snd_printk(KERN_ERR "usX2Y_AsyncSeq04_init error \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return err;
245 }
246 err = usX2Y_In04_init(priv);
247 if (err) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200248 snd_printk(KERN_ERR "usX2Y_In04_init error \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return err;
250 }
251 err = usX2Y_create_alsa_devices(hw->card);
252 if (err) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200253 snd_printk(KERN_ERR "usX2Y_create_alsa_devices error %i \n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 snd_card_free(hw->card);
255 return err;
256 }
257 priv->chip_status |= USX2Y_STAT_CHIP_INIT;
258 snd_printdd("%s: alsa all started\n", hw->name);
259 }
260 return err;
261}
262
263
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100264int usX2Y_hwdep_new(struct snd_card *card, struct usb_device* device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 int err;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100267 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 if ((err = snd_hwdep_new(card, SND_USX2Y_LOADER_ID, 0, &hw)) < 0)
270 return err;
271
272 hw->iface = SNDRV_HWDEP_IFACE_USX2Y;
273 hw->private_data = usX2Y(card);
274 hw->ops.open = snd_usX2Y_hwdep_open;
275 hw->ops.release = snd_usX2Y_hwdep_release;
276 hw->ops.dsp_status = snd_usX2Y_hwdep_dsp_status;
277 hw->ops.dsp_load = snd_usX2Y_hwdep_dsp_load;
278 hw->ops.mmap = snd_us428ctls_mmap;
279 hw->ops.poll = snd_us428ctls_poll;
280 hw->exclusive = 1;
281 sprintf(hw->name, "/proc/bus/usb/%03d/%03d", device->bus->busnum, device->devnum);
282 return 0;
283}
284