usX2Yhwdep.c 7.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Driver for Tascam US-X2Y USB soundcards
 *
 * FPGA Loader + ALSA Startup
 *
 * Copyright (c) 2003 by Karsten Wiese <annabellesgarden@yahoo.de>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <linux/interrupt.h>
24
#include <linux/slab.h>
L
Linus Torvalds 已提交
25 26 27 28 29 30 31 32 33
#include <linux/usb.h>
#include <sound/core.h>
#include <sound/memalloc.h>
#include <sound/pcm.h>
#include <sound/hwdep.h>
#include "usx2y.h"
#include "usbusx2y.h"
#include "usX2Yhwdep.h"

N
Nick Piggin 已提交
34 35
static int snd_us428ctls_vm_fault(struct vm_area_struct *area,
				  struct vm_fault *vmf)
L
Linus Torvalds 已提交
36 37 38 39 40
{
	unsigned long offset;
	struct page * page;
	void *vaddr;

N
Nick Piggin 已提交
41
	snd_printdd("ENTER, start %lXh, pgoff %ld\n",
L
Linus Torvalds 已提交
42
		   area->vm_start,
N
Nick Piggin 已提交
43
		   vmf->pgoff);
L
Linus Torvalds 已提交
44
	
N
Nick Piggin 已提交
45
	offset = vmf->pgoff << PAGE_SHIFT;
46
	vaddr = (char*)((struct usX2Ydev *)area->vm_private_data)->us428ctls_sharedmem + offset;
L
Linus Torvalds 已提交
47 48
	page = virt_to_page(vaddr);
	get_page(page);
N
Nick Piggin 已提交
49
	vmf->page = page;
L
Linus Torvalds 已提交
50

N
Nick Piggin 已提交
51 52
	snd_printdd("vaddr=%p made us428ctls_vm_fault() page %p\n",
		    vaddr, page);
L
Linus Torvalds 已提交
53

N
Nick Piggin 已提交
54
	return 0;
L
Linus Torvalds 已提交
55 56
}

57
static const struct vm_operations_struct us428ctls_vm_ops = {
N
Nick Piggin 已提交
58
	.fault = snd_us428ctls_vm_fault,
L
Linus Torvalds 已提交
59 60
};

61
static int snd_us428ctls_mmap(struct snd_hwdep * hw, struct file *filp, struct vm_area_struct *area)
L
Linus Torvalds 已提交
62 63
{
	unsigned long	size = (unsigned long)(area->vm_end - area->vm_start);
64
	struct usX2Ydev	*us428 = hw->private_data;
L
Linus Torvalds 已提交
65 66 67

	// FIXME this hwdep interface is used twice: fpga download and mmap for controlling Lights etc. Maybe better using 2 hwdep devs?
	// so as long as the device isn't fully initialised yet we return -EBUSY here.
T
Takashi Iwai 已提交
68
 	if (!(us428->chip_status & USX2Y_STAT_CHIP_INIT))
L
Linus Torvalds 已提交
69 70 71
		return -EBUSY;

	/* if userspace tries to mmap beyond end of our buffer, fail */ 
72 73
        if (size > PAGE_ALIGN(sizeof(struct us428ctls_sharedmem))) {
		snd_printd( "%lu > %lu\n", size, (unsigned long)sizeof(struct us428ctls_sharedmem)); 
L
Linus Torvalds 已提交
74 75 76 77 78
                return -EINVAL;
	}

	if (!us428->us428ctls_sharedmem) {
		init_waitqueue_head(&us428->us428ctls_wait_queue_head);
79
		if(!(us428->us428ctls_sharedmem = snd_malloc_pages(sizeof(struct us428ctls_sharedmem), GFP_KERNEL)))
L
Linus Torvalds 已提交
80
			return -ENOMEM;
81
		memset(us428->us428ctls_sharedmem, -1, sizeof(struct us428ctls_sharedmem));
L
Linus Torvalds 已提交
82 83 84
		us428->us428ctls_sharedmem->CtlSnapShotLast = -2;
	}
	area->vm_ops = &us428ctls_vm_ops;
85
	area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
L
Linus Torvalds 已提交
86 87 88 89
	area->vm_private_data = hw->private_data;
	return 0;
}

90
static unsigned int snd_us428ctls_poll(struct snd_hwdep *hw, struct file *file, poll_table *wait)
L
Linus Torvalds 已提交
91 92
{
	unsigned int	mask = 0;
93 94
	struct usX2Ydev	*us428 = hw->private_data;
	struct us428ctls_sharedmem *shm = us428->us428ctls_sharedmem;
L
Linus Torvalds 已提交
95 96 97 98 99 100 101 102 103 104 105 106
	if (us428->chip_status & USX2Y_STAT_CHIP_HUP)
		return POLLHUP;

	poll_wait(file, &us428->us428ctls_wait_queue_head, wait);

	if (shm != NULL && shm->CtlSnapShotLast != shm->CtlSnapShotRed)
		mask |= POLLIN;

	return mask;
}


107 108
static int snd_usX2Y_hwdep_dsp_status(struct snd_hwdep *hw,
				      struct snd_hwdep_dsp_status *info)
L
Linus Torvalds 已提交
109 110 111 112 113 114
{
	static char *type_ids[USX2Y_TYPE_NUMS] = {
		[USX2Y_TYPE_122] = "us122",
		[USX2Y_TYPE_224] = "us224",
		[USX2Y_TYPE_428] = "us428",
	};
115
	struct usX2Ydev	*us428 = hw->private_data;
L
Linus Torvalds 已提交
116 117
	int id = -1;

C
Clemens Ladisch 已提交
118
	switch (le16_to_cpu(us428->dev->descriptor.idProduct)) {
L
Linus Torvalds 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132
	case USB_ID_US122:
		id = USX2Y_TYPE_122;
		break;
	case USB_ID_US224:
		id = USX2Y_TYPE_224;
		break;
	case USB_ID_US428:
		id = USX2Y_TYPE_428;
		break;
	}
	if (0 > id)
		return -ENODEV;
	strcpy(info->id, type_ids[id]);
	info->num_dsps = 2;		// 0: Prepad Data, 1: FPGA Code
133
	if (us428->chip_status & USX2Y_STAT_CHIP_INIT)
L
Linus Torvalds 已提交
134 135 136 137 138 139
		info->chip_ready = 1;
 	info->version = USX2Y_DRIVER_VERSION; 
	return 0;
}


140
static int usX2Y_create_usbmidi(struct snd_card *card)
L
Linus Torvalds 已提交
141
{
142 143
	static struct snd_usb_midi_endpoint_info quirk_data_1 = {
		.out_ep = 0x06,
L
Linus Torvalds 已提交
144 145 146 147
		.in_ep = 0x06,
		.out_cables =	0x001,
		.in_cables =	0x001
	};
148
	static struct snd_usb_audio_quirk quirk_1 = {
L
Linus Torvalds 已提交
149 150 151 152 153 154
		.vendor_name =	"TASCAM",
		.product_name =	NAME_ALLCAPS,
		.ifnum = 	0,
       		.type = QUIRK_MIDI_FIXED_ENDPOINT,
		.data = &quirk_data_1
	};
155 156
	static struct snd_usb_midi_endpoint_info quirk_data_2 = {
		.out_ep = 0x06,
L
Linus Torvalds 已提交
157 158 159 160
		.in_ep = 0x06,
		.out_cables =	0x003,
		.in_cables =	0x003
	};
161
	static struct snd_usb_audio_quirk quirk_2 = {
L
Linus Torvalds 已提交
162 163 164 165 166 167
		.vendor_name =	"TASCAM",
		.product_name =	"US428",
		.ifnum = 	0,
       		.type = QUIRK_MIDI_FIXED_ENDPOINT,
		.data = &quirk_data_2
	};
C
Clemens Ladisch 已提交
168
	struct usb_device *dev = usX2Y(card)->dev;
L
Linus Torvalds 已提交
169
	struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
170 171 172
	struct snd_usb_audio_quirk *quirk =
		le16_to_cpu(dev->descriptor.idProduct) == USB_ID_US428 ?
		&quirk_2 : &quirk_1;
L
Linus Torvalds 已提交
173 174

	snd_printdd("usX2Y_create_usbmidi \n");
175
	return snd_usbmidi_create(card, iface, &usX2Y(card)->midi_list, quirk);
L
Linus Torvalds 已提交
176 177
}

178
static int usX2Y_create_alsa_devices(struct snd_card *card)
L
Linus Torvalds 已提交
179 180 181 182 183
{
	int err;

	do {
		if ((err = usX2Y_create_usbmidi(card)) < 0) {
T
Takashi Iwai 已提交
184
			snd_printk(KERN_ERR "usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err);
L
Linus Torvalds 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197
			break;
		}
		if ((err = usX2Y_audio_create(card)) < 0) 
			break;
		if ((err = usX2Y_hwdep_pcm_new(card)) < 0)
			break;
		if ((err = snd_card_register(card)) < 0)
			break;
	} while (0);

	return err;
} 

198 199
static int snd_usX2Y_hwdep_dsp_load(struct snd_hwdep *hw,
				    struct snd_hwdep_dsp_image *dsp)
L
Linus Torvalds 已提交
200
{
201
	struct usX2Ydev *priv = hw->private_data;
L
Linus Torvalds 已提交
202 203 204 205
	int	lret, err = -EINVAL;
	snd_printdd( "dsp_load %s\n", dsp->name);

	if (access_ok(VERIFY_READ, dsp->image, dsp->length)) {
C
Clemens Ladisch 已提交
206
		struct usb_device* dev = priv->dev;
L
Li Zefan 已提交
207 208 209 210 211 212
		char *buf;

		buf = memdup_user(dsp->image, dsp->length);
		if (IS_ERR(buf))
			return PTR_ERR(buf);

L
Linus Torvalds 已提交
213 214
		err = usb_set_interface(dev, 0, 1);
		if (err)
T
Takashi Iwai 已提交
215
			snd_printk(KERN_ERR "usb_set_interface error \n");
L
Linus Torvalds 已提交
216 217 218 219 220 221 222
		else
			err = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 2), buf, dsp->length, &lret, 6000);
		kfree(buf);
	}
	if (err)
		return err;
	if (dsp->index == 1) {
223
		msleep(250);				// give the device some time
L
Linus Torvalds 已提交
224 225
		err = usX2Y_AsyncSeq04_init(priv);
		if (err) {
T
Takashi Iwai 已提交
226
			snd_printk(KERN_ERR "usX2Y_AsyncSeq04_init error \n");
L
Linus Torvalds 已提交
227 228 229 230
			return err;
		}
		err = usX2Y_In04_init(priv);
		if (err) {
T
Takashi Iwai 已提交
231
			snd_printk(KERN_ERR "usX2Y_In04_init error \n");
L
Linus Torvalds 已提交
232 233 234 235
			return err;
		}
		err = usX2Y_create_alsa_devices(hw->card);
		if (err) {
T
Takashi Iwai 已提交
236
			snd_printk(KERN_ERR "usX2Y_create_alsa_devices error %i \n", err);
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244 245 246
			snd_card_free(hw->card);
			return err;
		}
		priv->chip_status |= USX2Y_STAT_CHIP_INIT; 
		snd_printdd("%s: alsa all started\n", hw->name);
	}
	return err;
}


247
int usX2Y_hwdep_new(struct snd_card *card, struct usb_device* device)
L
Linus Torvalds 已提交
248 249
{
	int err;
250
	struct snd_hwdep *hw;
L
Linus Torvalds 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

	if ((err = snd_hwdep_new(card, SND_USX2Y_LOADER_ID, 0, &hw)) < 0)
		return err;

	hw->iface = SNDRV_HWDEP_IFACE_USX2Y;
	hw->private_data = usX2Y(card);
	hw->ops.dsp_status = snd_usX2Y_hwdep_dsp_status;
	hw->ops.dsp_load = snd_usX2Y_hwdep_dsp_load;
	hw->ops.mmap = snd_us428ctls_mmap;
	hw->ops.poll = snd_us428ctls_poll;
	hw->exclusive = 1;
	sprintf(hw->name, "/proc/bus/usb/%03d/%03d", device->bus->busnum, device->devnum);
	return 0;
}