blob: 1879b72c40f8d0695bd98883021a84be5477f050 [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
Nick Piggineb415b82007-12-13 16:16:40 +010033static int snd_us428ctls_vm_fault(struct vm_area_struct *area,
34 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 unsigned long offset;
37 struct page * page;
38 void *vaddr;
39
Nick Piggineb415b82007-12-13 16:16:40 +010040 snd_printdd("ENTER, start %lXh, pgoff %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 area->vm_start,
Nick Piggineb415b82007-12-13 16:16:40 +010042 vmf->pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Nick Piggineb415b82007-12-13 16:16:40 +010044 offset = vmf->pgoff << PAGE_SHIFT;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010045 vaddr = (char*)((struct usX2Ydev *)area->vm_private_data)->us428ctls_sharedmem + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 page = virt_to_page(vaddr);
47 get_page(page);
Nick Piggineb415b82007-12-13 16:16:40 +010048 vmf->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Nick Piggineb415b82007-12-13 16:16:40 +010050 snd_printdd("vaddr=%p made us428ctls_vm_fault() page %p\n",
51 vaddr, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Nick Piggineb415b82007-12-13 16:16:40 +010053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040056static const struct vm_operations_struct us428ctls_vm_ops = {
Nick Piggineb415b82007-12-13 16:16:40 +010057 .fault = snd_us428ctls_vm_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058};
59
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010060static int snd_us428ctls_mmap(struct snd_hwdep * hw, struct file *filp, struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 unsigned long size = (unsigned long)(area->vm_end - area->vm_start);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010063 struct usX2Ydev *us428 = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 // FIXME this hwdep interface is used twice: fpga download and mmap for controlling Lights etc. Maybe better using 2 hwdep devs?
66 // so as long as the device isn't fully initialised yet we return -EBUSY here.
Takashi Iwaicb432372005-11-17 10:48:52 +010067 if (!(us428->chip_status & USX2Y_STAT_CHIP_INIT))
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return -EBUSY;
69
70 /* if userspace tries to mmap beyond end of our buffer, fail */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010071 if (size > PAGE_ALIGN(sizeof(struct us428ctls_sharedmem))) {
72 snd_printd( "%lu > %lu\n", size, (unsigned long)sizeof(struct us428ctls_sharedmem));
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return -EINVAL;
74 }
75
76 if (!us428->us428ctls_sharedmem) {
77 init_waitqueue_head(&us428->us428ctls_wait_queue_head);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010078 if(!(us428->us428ctls_sharedmem = snd_malloc_pages(sizeof(struct us428ctls_sharedmem), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return -ENOMEM;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010080 memset(us428->us428ctls_sharedmem, -1, sizeof(struct us428ctls_sharedmem));
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 us428->us428ctls_sharedmem->CtlSnapShotLast = -2;
82 }
83 area->vm_ops = &us428ctls_vm_ops;
Nick Piggin2f987352008-02-02 03:08:53 +010084 area->vm_flags |= VM_RESERVED | VM_DONTEXPAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 area->vm_private_data = hw->private_data;
86 return 0;
87}
88
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010089static unsigned int snd_us428ctls_poll(struct snd_hwdep *hw, struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 unsigned int mask = 0;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010092 struct usX2Ydev *us428 = hw->private_data;
93 struct us428ctls_sharedmem *shm = us428->us428ctls_sharedmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (us428->chip_status & USX2Y_STAT_CHIP_HUP)
95 return POLLHUP;
96
97 poll_wait(file, &us428->us428ctls_wait_queue_head, wait);
98
99 if (shm != NULL && shm->CtlSnapShotLast != shm->CtlSnapShotRed)
100 mask |= POLLIN;
101
102 return mask;
103}
104
105
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100106static int snd_usX2Y_hwdep_dsp_status(struct snd_hwdep *hw,
107 struct snd_hwdep_dsp_status *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 static char *type_ids[USX2Y_TYPE_NUMS] = {
110 [USX2Y_TYPE_122] = "us122",
111 [USX2Y_TYPE_224] = "us224",
112 [USX2Y_TYPE_428] = "us428",
113 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100114 struct usX2Ydev *us428 = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int id = -1;
116
Clemens Ladischa014bba2009-11-16 12:26:30 +0100117 switch (le16_to_cpu(us428->dev->descriptor.idProduct)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 case USB_ID_US122:
119 id = USX2Y_TYPE_122;
120 break;
121 case USB_ID_US224:
122 id = USX2Y_TYPE_224;
123 break;
124 case USB_ID_US428:
125 id = USX2Y_TYPE_428;
126 break;
127 }
128 if (0 > id)
129 return -ENODEV;
130 strcpy(info->id, type_ids[id]);
131 info->num_dsps = 2; // 0: Prepad Data, 1: FPGA Code
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100132 if (us428->chip_status & USX2Y_STAT_CHIP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 info->chip_ready = 1;
134 info->version = USX2Y_DRIVER_VERSION;
135 return 0;
136}
137
138
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100139static int usX2Y_create_usbmidi(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100141 static struct snd_usb_midi_endpoint_info quirk_data_1 = {
142 .out_ep = 0x06,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 .in_ep = 0x06,
144 .out_cables = 0x001,
145 .in_cables = 0x001
146 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100147 static struct snd_usb_audio_quirk quirk_1 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .vendor_name = "TASCAM",
149 .product_name = NAME_ALLCAPS,
150 .ifnum = 0,
151 .type = QUIRK_MIDI_FIXED_ENDPOINT,
152 .data = &quirk_data_1
153 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100154 static struct snd_usb_midi_endpoint_info quirk_data_2 = {
155 .out_ep = 0x06,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 .in_ep = 0x06,
157 .out_cables = 0x003,
158 .in_cables = 0x003
159 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100160 static struct snd_usb_audio_quirk quirk_2 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 .vendor_name = "TASCAM",
162 .product_name = "US428",
163 .ifnum = 0,
164 .type = QUIRK_MIDI_FIXED_ENDPOINT,
165 .data = &quirk_data_2
166 };
Clemens Ladischa014bba2009-11-16 12:26:30 +0100167 struct usb_device *dev = usX2Y(card)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100169 struct snd_usb_audio_quirk *quirk =
170 le16_to_cpu(dev->descriptor.idProduct) == USB_ID_US428 ?
171 &quirk_2 : &quirk_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 snd_printdd("usX2Y_create_usbmidi \n");
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100174 return snd_usbmidi_create(card, iface, &usX2Y(card)->midi_list, quirk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100177static int usX2Y_create_alsa_devices(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 int err;
180
181 do {
182 if ((err = usX2Y_create_usbmidi(card)) < 0) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200183 snd_printk(KERN_ERR "usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185 }
186 if ((err = usX2Y_audio_create(card)) < 0)
187 break;
188 if ((err = usX2Y_hwdep_pcm_new(card)) < 0)
189 break;
190 if ((err = snd_card_register(card)) < 0)
191 break;
192 } while (0);
193
194 return err;
195}
196
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100197static int snd_usX2Y_hwdep_dsp_load(struct snd_hwdep *hw,
198 struct snd_hwdep_dsp_image *dsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100200 struct usX2Ydev *priv = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 int lret, err = -EINVAL;
202 snd_printdd( "dsp_load %s\n", dsp->name);
203
204 if (access_ok(VERIFY_READ, dsp->image, dsp->length)) {
Clemens Ladischa014bba2009-11-16 12:26:30 +0100205 struct usb_device* dev = priv->dev;
Li Zefan85385c12009-04-10 09:43:59 +0800206 char *buf;
207
208 buf = memdup_user(dsp->image, dsp->length);
209 if (IS_ERR(buf))
210 return PTR_ERR(buf);
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 err = usb_set_interface(dev, 0, 1);
213 if (err)
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200214 snd_printk(KERN_ERR "usb_set_interface error \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 else
216 err = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 2), buf, dsp->length, &lret, 6000);
217 kfree(buf);
218 }
219 if (err)
220 return err;
221 if (dsp->index == 1) {
Nishanth Aravamudanb27c1872005-07-09 10:54:37 +0200222 msleep(250); // give the device some time
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 err = usX2Y_AsyncSeq04_init(priv);
224 if (err) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200225 snd_printk(KERN_ERR "usX2Y_AsyncSeq04_init error \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return err;
227 }
228 err = usX2Y_In04_init(priv);
229 if (err) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200230 snd_printk(KERN_ERR "usX2Y_In04_init error \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return err;
232 }
233 err = usX2Y_create_alsa_devices(hw->card);
234 if (err) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200235 snd_printk(KERN_ERR "usX2Y_create_alsa_devices error %i \n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 snd_card_free(hw->card);
237 return err;
238 }
239 priv->chip_status |= USX2Y_STAT_CHIP_INIT;
240 snd_printdd("%s: alsa all started\n", hw->name);
241 }
242 return err;
243}
244
245
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100246int usX2Y_hwdep_new(struct snd_card *card, struct usb_device* device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 int err;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100249 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if ((err = snd_hwdep_new(card, SND_USX2Y_LOADER_ID, 0, &hw)) < 0)
252 return err;
253
254 hw->iface = SNDRV_HWDEP_IFACE_USX2Y;
255 hw->private_data = usX2Y(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 hw->ops.dsp_status = snd_usX2Y_hwdep_dsp_status;
257 hw->ops.dsp_load = snd_usX2Y_hwdep_dsp_load;
258 hw->ops.mmap = snd_us428ctls_mmap;
259 hw->ops.poll = snd_us428ctls_poll;
260 hw->exclusive = 1;
261 sprintf(hw->name, "/proc/bus/usb/%03d/%03d", device->bus->busnum, device->devnum);
262 return 0;
263}
264