blob: 0b34dbc8f3020436d3e740bb61dea9f648a745ab [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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
Nick Piggineb415b82007-12-13 16:16:40 +010034static int snd_us428ctls_vm_fault(struct vm_area_struct *area,
35 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 unsigned long offset;
38 struct page * page;
39 void *vaddr;
40
Nick Piggineb415b82007-12-13 16:16:40 +010041 snd_printdd("ENTER, start %lXh, pgoff %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 area->vm_start,
Nick Piggineb415b82007-12-13 16:16:40 +010043 vmf->pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Nick Piggineb415b82007-12-13 16:16:40 +010045 offset = vmf->pgoff << PAGE_SHIFT;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010046 vaddr = (char*)((struct usX2Ydev *)area->vm_private_data)->us428ctls_sharedmem + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 page = virt_to_page(vaddr);
48 get_page(page);
Nick Piggineb415b82007-12-13 16:16:40 +010049 vmf->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Nick Piggineb415b82007-12-13 16:16:40 +010051 snd_printdd("vaddr=%p made us428ctls_vm_fault() page %p\n",
52 vaddr, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Nick Piggineb415b82007-12-13 16:16:40 +010054 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040057static const struct vm_operations_struct us428ctls_vm_ops = {
Nick Piggineb415b82007-12-13 16:16:40 +010058 .fault = snd_us428ctls_vm_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010061static int snd_us428ctls_mmap(struct snd_hwdep * hw, struct file *filp, struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 unsigned long size = (unsigned long)(area->vm_end - area->vm_start);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010064 struct usX2Ydev *us428 = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 // FIXME this hwdep interface is used twice: fpga download and mmap for controlling Lights etc. Maybe better using 2 hwdep devs?
67 // so as long as the device isn't fully initialised yet we return -EBUSY here.
Takashi Iwaicb432372005-11-17 10:48:52 +010068 if (!(us428->chip_status & USX2Y_STAT_CHIP_INIT))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return -EBUSY;
70
71 /* if userspace tries to mmap beyond end of our buffer, fail */
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010072 if (size > PAGE_ALIGN(sizeof(struct us428ctls_sharedmem))) {
73 snd_printd( "%lu > %lu\n", size, (unsigned long)sizeof(struct us428ctls_sharedmem));
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return -EINVAL;
75 }
76
77 if (!us428->us428ctls_sharedmem) {
78 init_waitqueue_head(&us428->us428ctls_wait_queue_head);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010079 if(!(us428->us428ctls_sharedmem = snd_malloc_pages(sizeof(struct us428ctls_sharedmem), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -ENOMEM;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010081 memset(us428->us428ctls_sharedmem, -1, sizeof(struct us428ctls_sharedmem));
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 us428->us428ctls_sharedmem->CtlSnapShotLast = -2;
83 }
84 area->vm_ops = &us428ctls_vm_ops;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -070085 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 area->vm_private_data = hw->private_data;
87 return 0;
88}
89
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010090static unsigned int snd_us428ctls_poll(struct snd_hwdep *hw, struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 unsigned int mask = 0;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +010093 struct usX2Ydev *us428 = hw->private_data;
94 struct us428ctls_sharedmem *shm = us428->us428ctls_sharedmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (us428->chip_status & USX2Y_STAT_CHIP_HUP)
96 return POLLHUP;
97
98 poll_wait(file, &us428->us428ctls_wait_queue_head, wait);
99
100 if (shm != NULL && shm->CtlSnapShotLast != shm->CtlSnapShotRed)
101 mask |= POLLIN;
102
103 return mask;
104}
105
106
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100107static int snd_usX2Y_hwdep_dsp_status(struct snd_hwdep *hw,
108 struct snd_hwdep_dsp_status *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 static char *type_ids[USX2Y_TYPE_NUMS] = {
111 [USX2Y_TYPE_122] = "us122",
112 [USX2Y_TYPE_224] = "us224",
113 [USX2Y_TYPE_428] = "us428",
114 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100115 struct usX2Ydev *us428 = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int id = -1;
117
Clemens Ladischa014bba2009-11-16 12:26:30 +0100118 switch (le16_to_cpu(us428->dev->descriptor.idProduct)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 case USB_ID_US122:
120 id = USX2Y_TYPE_122;
121 break;
122 case USB_ID_US224:
123 id = USX2Y_TYPE_224;
124 break;
125 case USB_ID_US428:
126 id = USX2Y_TYPE_428;
127 break;
128 }
129 if (0 > id)
130 return -ENODEV;
131 strcpy(info->id, type_ids[id]);
132 info->num_dsps = 2; // 0: Prepad Data, 1: FPGA Code
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100133 if (us428->chip_status & USX2Y_STAT_CHIP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 info->chip_ready = 1;
135 info->version = USX2Y_DRIVER_VERSION;
136 return 0;
137}
138
139
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100140static int usX2Y_create_usbmidi(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100142 static struct snd_usb_midi_endpoint_info quirk_data_1 = {
143 .out_ep = 0x06,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .in_ep = 0x06,
145 .out_cables = 0x001,
146 .in_cables = 0x001
147 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100148 static struct snd_usb_audio_quirk quirk_1 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 .vendor_name = "TASCAM",
150 .product_name = NAME_ALLCAPS,
151 .ifnum = 0,
152 .type = QUIRK_MIDI_FIXED_ENDPOINT,
153 .data = &quirk_data_1
154 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100155 static struct snd_usb_midi_endpoint_info quirk_data_2 = {
156 .out_ep = 0x06,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 .in_ep = 0x06,
158 .out_cables = 0x003,
159 .in_cables = 0x003
160 };
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100161 static struct snd_usb_audio_quirk quirk_2 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 .vendor_name = "TASCAM",
163 .product_name = "US428",
164 .ifnum = 0,
165 .type = QUIRK_MIDI_FIXED_ENDPOINT,
166 .data = &quirk_data_2
167 };
Clemens Ladischa014bba2009-11-16 12:26:30 +0100168 struct usb_device *dev = usX2Y(card)->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100170 struct snd_usb_audio_quirk *quirk =
171 le16_to_cpu(dev->descriptor.idProduct) == USB_ID_US428 ?
172 &quirk_2 : &quirk_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 snd_printdd("usX2Y_create_usbmidi \n");
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100175 return snd_usbmidi_create(card, iface, &usX2Y(card)->midi_list, quirk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100178static int usX2Y_create_alsa_devices(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 int err;
181
182 do {
183 if ((err = usX2Y_create_usbmidi(card)) < 0) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200184 snd_printk(KERN_ERR "usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 break;
186 }
187 if ((err = usX2Y_audio_create(card)) < 0)
188 break;
189 if ((err = usX2Y_hwdep_pcm_new(card)) < 0)
190 break;
191 if ((err = snd_card_register(card)) < 0)
192 break;
193 } while (0);
194
195 return err;
196}
197
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100198static int snd_usX2Y_hwdep_dsp_load(struct snd_hwdep *hw,
199 struct snd_hwdep_dsp_image *dsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100201 struct usX2Ydev *priv = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int lret, err = -EINVAL;
203 snd_printdd( "dsp_load %s\n", dsp->name);
204
205 if (access_ok(VERIFY_READ, dsp->image, dsp->length)) {
Clemens Ladischa014bba2009-11-16 12:26:30 +0100206 struct usb_device* dev = priv->dev;
Li Zefan85385c12009-04-10 09:43:59 +0800207 char *buf;
208
209 buf = memdup_user(dsp->image, dsp->length);
210 if (IS_ERR(buf))
211 return PTR_ERR(buf);
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 err = usb_set_interface(dev, 0, 1);
214 if (err)
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200215 snd_printk(KERN_ERR "usb_set_interface error \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 else
217 err = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 2), buf, dsp->length, &lret, 6000);
218 kfree(buf);
219 }
220 if (err)
221 return err;
222 if (dsp->index == 1) {
Nishanth Aravamudanb27c1872005-07-09 10:54:37 +0200223 msleep(250); // give the device some time
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 err = usX2Y_AsyncSeq04_init(priv);
225 if (err) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200226 snd_printk(KERN_ERR "usX2Y_AsyncSeq04_init error \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return err;
228 }
229 err = usX2Y_In04_init(priv);
230 if (err) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200231 snd_printk(KERN_ERR "usX2Y_In04_init error \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return err;
233 }
234 err = usX2Y_create_alsa_devices(hw->card);
235 if (err) {
Takashi Iwaid3d579f2005-10-21 16:20:11 +0200236 snd_printk(KERN_ERR "usX2Y_create_alsa_devices error %i \n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 snd_card_free(hw->card);
238 return err;
239 }
240 priv->chip_status |= USX2Y_STAT_CHIP_INIT;
241 snd_printdd("%s: alsa all started\n", hw->name);
242 }
243 return err;
244}
245
246
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100247int usX2Y_hwdep_new(struct snd_card *card, struct usb_device* device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 int err;
Takashi Iwaibbe85bb2005-11-17 15:08:26 +0100250 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 if ((err = snd_hwdep_new(card, SND_USX2Y_LOADER_ID, 0, &hw)) < 0)
253 return err;
254
255 hw->iface = SNDRV_HWDEP_IFACE_USX2Y;
256 hw->private_data = usX2Y(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 hw->ops.dsp_status = snd_usX2Y_hwdep_dsp_status;
258 hw->ops.dsp_load = snd_usX2Y_hwdep_dsp_load;
259 hw->ops.mmap = snd_us428ctls_mmap;
260 hw->ops.poll = snd_us428ctls_poll;
261 hw->exclusive = 1;
262 sprintf(hw->name, "/proc/bus/usb/%03d/%03d", device->bus->busnum, device->devnum);
263 return 0;
264}
265