[ALSA] alsa: usx2y nopage
[linux-2.6/zen-sources.git] / sound / usb / usx2y / usX2Yhwdep.c
bloba1dca3441319520bd04c26c53721fa7103636fa2
1 /*
2 * Driver for Tascam US-X2Y USB soundcards
4 * FPGA Loader + ALSA Startup
6 * Copyright (c) 2003 by Karsten Wiese <annabellesgarden@yahoo.de>
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.
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.
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
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"
34 int usX2Y_hwdep_pcm_new(struct snd_card *card);
37 static int snd_us428ctls_vm_fault(struct vm_area_struct *area,
38 struct vm_fault *vmf)
40 unsigned long offset;
41 struct page * page;
42 void *vaddr;
44 snd_printdd("ENTER, start %lXh, pgoff %ld\n",
45 area->vm_start,
46 vmf->pgoff);
48 offset = vmf->pgoff << PAGE_SHIFT;
49 vaddr = (char*)((struct usX2Ydev *)area->vm_private_data)->us428ctls_sharedmem + offset;
50 page = virt_to_page(vaddr);
51 get_page(page);
52 vmf->page = page;
54 snd_printdd("vaddr=%p made us428ctls_vm_fault() page %p\n",
55 vaddr, page);
57 return 0;
60 static struct vm_operations_struct us428ctls_vm_ops = {
61 .fault = snd_us428ctls_vm_fault,
64 static int snd_us428ctls_mmap(struct snd_hwdep * hw, struct file *filp, struct vm_area_struct *area)
66 unsigned long size = (unsigned long)(area->vm_end - area->vm_start);
67 struct usX2Ydev *us428 = hw->private_data;
69 // FIXME this hwdep interface is used twice: fpga download and mmap for controlling Lights etc. Maybe better using 2 hwdep devs?
70 // so as long as the device isn't fully initialised yet we return -EBUSY here.
71 if (!(us428->chip_status & USX2Y_STAT_CHIP_INIT))
72 return -EBUSY;
74 /* if userspace tries to mmap beyond end of our buffer, fail */
75 if (size > PAGE_ALIGN(sizeof(struct us428ctls_sharedmem))) {
76 snd_printd( "%lu > %lu\n", size, (unsigned long)sizeof(struct us428ctls_sharedmem));
77 return -EINVAL;
80 if (!us428->us428ctls_sharedmem) {
81 init_waitqueue_head(&us428->us428ctls_wait_queue_head);
82 if(!(us428->us428ctls_sharedmem = snd_malloc_pages(sizeof(struct us428ctls_sharedmem), GFP_KERNEL)))
83 return -ENOMEM;
84 memset(us428->us428ctls_sharedmem, -1, sizeof(struct us428ctls_sharedmem));
85 us428->us428ctls_sharedmem->CtlSnapShotLast = -2;
87 area->vm_ops = &us428ctls_vm_ops;
88 area->vm_flags |= VM_RESERVED;
89 area->vm_private_data = hw->private_data;
90 return 0;
93 static unsigned int snd_us428ctls_poll(struct snd_hwdep *hw, struct file *file, poll_table *wait)
95 unsigned int mask = 0;
96 struct usX2Ydev *us428 = hw->private_data;
97 struct us428ctls_sharedmem *shm = us428->us428ctls_sharedmem;
98 if (us428->chip_status & USX2Y_STAT_CHIP_HUP)
99 return POLLHUP;
101 poll_wait(file, &us428->us428ctls_wait_queue_head, wait);
103 if (shm != NULL && shm->CtlSnapShotLast != shm->CtlSnapShotRed)
104 mask |= POLLIN;
106 return mask;
110 static int snd_usX2Y_hwdep_open(struct snd_hwdep *hw, struct file *file)
112 return 0;
115 static int snd_usX2Y_hwdep_release(struct snd_hwdep *hw, struct file *file)
117 return 0;
120 static int snd_usX2Y_hwdep_dsp_status(struct snd_hwdep *hw,
121 struct snd_hwdep_dsp_status *info)
123 static char *type_ids[USX2Y_TYPE_NUMS] = {
124 [USX2Y_TYPE_122] = "us122",
125 [USX2Y_TYPE_224] = "us224",
126 [USX2Y_TYPE_428] = "us428",
128 struct usX2Ydev *us428 = hw->private_data;
129 int id = -1;
131 switch (le16_to_cpu(us428->chip.dev->descriptor.idProduct)) {
132 case USB_ID_US122:
133 id = USX2Y_TYPE_122;
134 break;
135 case USB_ID_US224:
136 id = USX2Y_TYPE_224;
137 break;
138 case USB_ID_US428:
139 id = USX2Y_TYPE_428;
140 break;
142 if (0 > id)
143 return -ENODEV;
144 strcpy(info->id, type_ids[id]);
145 info->num_dsps = 2; // 0: Prepad Data, 1: FPGA Code
146 if (us428->chip_status & USX2Y_STAT_CHIP_INIT)
147 info->chip_ready = 1;
148 info->version = USX2Y_DRIVER_VERSION;
149 return 0;
153 static int usX2Y_create_usbmidi(struct snd_card *card)
155 static struct snd_usb_midi_endpoint_info quirk_data_1 = {
156 .out_ep = 0x06,
157 .in_ep = 0x06,
158 .out_cables = 0x001,
159 .in_cables = 0x001
161 static struct snd_usb_audio_quirk quirk_1 = {
162 .vendor_name = "TASCAM",
163 .product_name = NAME_ALLCAPS,
164 .ifnum = 0,
165 .type = QUIRK_MIDI_FIXED_ENDPOINT,
166 .data = &quirk_data_1
168 static struct snd_usb_midi_endpoint_info quirk_data_2 = {
169 .out_ep = 0x06,
170 .in_ep = 0x06,
171 .out_cables = 0x003,
172 .in_cables = 0x003
174 static struct snd_usb_audio_quirk quirk_2 = {
175 .vendor_name = "TASCAM",
176 .product_name = "US428",
177 .ifnum = 0,
178 .type = QUIRK_MIDI_FIXED_ENDPOINT,
179 .data = &quirk_data_2
181 struct usb_device *dev = usX2Y(card)->chip.dev;
182 struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
183 struct snd_usb_audio_quirk *quirk =
184 le16_to_cpu(dev->descriptor.idProduct) == USB_ID_US428 ?
185 &quirk_2 : &quirk_1;
187 snd_printdd("usX2Y_create_usbmidi \n");
188 return snd_usb_create_midi_interface(&usX2Y(card)->chip, iface, quirk);
191 static int usX2Y_create_alsa_devices(struct snd_card *card)
193 int err;
195 do {
196 if ((err = usX2Y_create_usbmidi(card)) < 0) {
197 snd_printk(KERN_ERR "usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err);
198 break;
200 if ((err = usX2Y_audio_create(card)) < 0)
201 break;
202 if ((err = usX2Y_hwdep_pcm_new(card)) < 0)
203 break;
204 if ((err = snd_card_register(card)) < 0)
205 break;
206 } while (0);
208 return err;
211 static int snd_usX2Y_hwdep_dsp_load(struct snd_hwdep *hw,
212 struct snd_hwdep_dsp_image *dsp)
214 struct usX2Ydev *priv = hw->private_data;
215 int lret, err = -EINVAL;
216 snd_printdd( "dsp_load %s\n", dsp->name);
218 if (access_ok(VERIFY_READ, dsp->image, dsp->length)) {
219 struct usb_device* dev = priv->chip.dev;
220 char *buf = kmalloc(dsp->length, GFP_KERNEL);
221 if (!buf)
222 return -ENOMEM;
223 if (copy_from_user(buf, dsp->image, dsp->length)) {
224 kfree(buf);
225 return -EFAULT;
227 err = usb_set_interface(dev, 0, 1);
228 if (err)
229 snd_printk(KERN_ERR "usb_set_interface error \n");
230 else
231 err = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 2), buf, dsp->length, &lret, 6000);
232 kfree(buf);
234 if (err)
235 return err;
236 if (dsp->index == 1) {
237 msleep(250); // give the device some time
238 err = usX2Y_AsyncSeq04_init(priv);
239 if (err) {
240 snd_printk(KERN_ERR "usX2Y_AsyncSeq04_init error \n");
241 return err;
243 err = usX2Y_In04_init(priv);
244 if (err) {
245 snd_printk(KERN_ERR "usX2Y_In04_init error \n");
246 return err;
248 err = usX2Y_create_alsa_devices(hw->card);
249 if (err) {
250 snd_printk(KERN_ERR "usX2Y_create_alsa_devices error %i \n", err);
251 snd_card_free(hw->card);
252 return err;
254 priv->chip_status |= USX2Y_STAT_CHIP_INIT;
255 snd_printdd("%s: alsa all started\n", hw->name);
257 return err;
261 int usX2Y_hwdep_new(struct snd_card *card, struct usb_device* device)
263 int err;
264 struct snd_hwdep *hw;
266 if ((err = snd_hwdep_new(card, SND_USX2Y_LOADER_ID, 0, &hw)) < 0)
267 return err;
269 hw->iface = SNDRV_HWDEP_IFACE_USX2Y;
270 hw->private_data = usX2Y(card);
271 hw->ops.open = snd_usX2Y_hwdep_open;
272 hw->ops.release = snd_usX2Y_hwdep_release;
273 hw->ops.dsp_status = snd_usX2Y_hwdep_dsp_status;
274 hw->ops.dsp_load = snd_usX2Y_hwdep_dsp_load;
275 hw->ops.mmap = snd_us428ctls_mmap;
276 hw->ops.poll = snd_us428ctls_poll;
277 hw->exclusive = 1;
278 sprintf(hw->name, "/proc/bus/usb/%03d/%03d", device->bus->busnum, device->devnum);
279 return 0;