1/*
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
34int usX2Y_hwdep_pcm_new(struct snd_card *card);
35
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;
51	snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_SIGBUS);
52	vaddr = (char*)((struct usX2Ydev *)area->vm_private_data)->us428ctls_sharedmem + offset;
53	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
67static int snd_us428ctls_mmap(struct snd_hwdep * hw, struct file *filp, struct vm_area_struct *area)
68{
69	unsigned long	size = (unsigned long)(area->vm_end - area->vm_start);
70	struct usX2Ydev	*us428 = hw->private_data;
71
72	// so as long as the device isn't fully initialised yet we return -EBUSY here.
73 	if (!(us428->chip_status & USX2Y_STAT_CHIP_INIT))
74		return -EBUSY;
75
76	/* if userspace tries to mmap beyond end of our buffer, fail */
77        if (size > PAGE_ALIGN(sizeof(struct us428ctls_sharedmem))) {
78		snd_printd( "%lu > %lu\n", size, (unsigned long)sizeof(struct us428ctls_sharedmem));
79                return -EINVAL;
80	}
81
82	if (!us428->us428ctls_sharedmem) {
83		init_waitqueue_head(&us428->us428ctls_wait_queue_head);
84		if(!(us428->us428ctls_sharedmem = snd_malloc_pages(sizeof(struct us428ctls_sharedmem), GFP_KERNEL)))
85			return -ENOMEM;
86		memset(us428->us428ctls_sharedmem, -1, sizeof(struct us428ctls_sharedmem));
87		us428->us428ctls_sharedmem->CtlSnapShotLast = -2;
88	}
89	area->vm_ops = &us428ctls_vm_ops;
90	area->vm_flags |= VM_RESERVED;
91	area->vm_private_data = hw->private_data;
92	return 0;
93}
94
95static unsigned int snd_us428ctls_poll(struct snd_hwdep *hw, struct file *file, poll_table *wait)
96{
97	unsigned int	mask = 0;
98	struct usX2Ydev	*us428 = hw->private_data;
99	struct us428ctls_sharedmem *shm = us428->us428ctls_sharedmem;
100	if (us428->chip_status & USX2Y_STAT_CHIP_HUP)
101		return POLLHUP;
102
103	poll_wait(file, &us428->us428ctls_wait_queue_head, wait);
104
105	if (shm != NULL && shm->CtlSnapShotLast != shm->CtlSnapShotRed)
106		mask |= POLLIN;
107
108	return mask;
109}
110
111
112static int snd_usX2Y_hwdep_open(struct snd_hwdep *hw, struct file *file)
113{
114	return 0;
115}
116
117static int snd_usX2Y_hwdep_release(struct snd_hwdep *hw, struct file *file)
118{
119	return 0;
120}
121
122static int snd_usX2Y_hwdep_dsp_status(struct snd_hwdep *hw,
123				      struct snd_hwdep_dsp_status *info)
124{
125	static char *type_ids[USX2Y_TYPE_NUMS] = {
126		[USX2Y_TYPE_122] = "us122",
127		[USX2Y_TYPE_224] = "us224",
128		[USX2Y_TYPE_428] = "us428",
129	};
130	struct usX2Ydev	*us428 = hw->private_data;
131	int id = -1;
132
133	switch (le16_to_cpu(us428->chip.dev->descriptor.idProduct)) {
134	case USB_ID_US122:
135		id = USX2Y_TYPE_122;
136		break;
137	case USB_ID_US224:
138		id = USX2Y_TYPE_224;
139		break;
140	case USB_ID_US428:
141		id = USX2Y_TYPE_428;
142		break;
143	}
144	if (0 > id)
145		return -ENODEV;
146	strcpy(info->id, type_ids[id]);
147	info->num_dsps = 2;		// 0: Prepad Data, 1: FPGA Code
148	if (us428->chip_status & USX2Y_STAT_CHIP_INIT)
149		info->chip_ready = 1;
150 	info->version = USX2Y_DRIVER_VERSION;
151	return 0;
152}
153
154
155static int usX2Y_create_usbmidi(struct snd_card *card)
156{
157	static struct snd_usb_midi_endpoint_info quirk_data_1 = {
158		.out_ep = 0x06,
159		.in_ep = 0x06,
160		.out_cables =	0x001,
161		.in_cables =	0x001
162	};
163	static struct snd_usb_audio_quirk quirk_1 = {
164		.vendor_name =	"TASCAM",
165		.product_name =	NAME_ALLCAPS,
166		.ifnum = 	0,
167       		.type = QUIRK_MIDI_FIXED_ENDPOINT,
168		.data = &quirk_data_1
169	};
170	static struct snd_usb_midi_endpoint_info quirk_data_2 = {
171		.out_ep = 0x06,
172		.in_ep = 0x06,
173		.out_cables =	0x003,
174		.in_cables =	0x003
175	};
176	static struct snd_usb_audio_quirk quirk_2 = {
177		.vendor_name =	"TASCAM",
178		.product_name =	"US428",
179		.ifnum = 	0,
180       		.type = QUIRK_MIDI_FIXED_ENDPOINT,
181		.data = &quirk_data_2
182	};
183	struct usb_device *dev = usX2Y(card)->chip.dev;
184	struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
185	struct snd_usb_audio_quirk *quirk =
186		le16_to_cpu(dev->descriptor.idProduct) == USB_ID_US428 ?
187		&quirk_2 : &quirk_1;
188
189	snd_printdd("usX2Y_create_usbmidi \n");
190	return snd_usb_create_midi_interface(&usX2Y(card)->chip, iface, quirk);
191}
192
193static int usX2Y_create_alsa_devices(struct snd_card *card)
194{
195	int err;
196
197	do {
198		if ((err = usX2Y_create_usbmidi(card)) < 0) {
199			snd_printk(KERN_ERR "usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err);
200			break;
201		}
202		if ((err = usX2Y_audio_create(card)) < 0)
203			break;
204		if ((err = usX2Y_hwdep_pcm_new(card)) < 0)
205			break;
206		if ((err = snd_card_register(card)) < 0)
207			break;
208	} while (0);
209
210	return err;
211}
212
213static int snd_usX2Y_hwdep_dsp_load(struct snd_hwdep *hw,
214				    struct snd_hwdep_dsp_image *dsp)
215{
216	struct usX2Ydev *priv = hw->private_data;
217	int	lret, err = -EINVAL;
218	snd_printdd( "dsp_load %s\n", dsp->name);
219
220	if (access_ok(VERIFY_READ, dsp->image, dsp->length)) {
221		struct usb_device* dev = priv->chip.dev;
222		char *buf = kmalloc(dsp->length, GFP_KERNEL);
223		if (!buf)
224			return -ENOMEM;
225		if (copy_from_user(buf, dsp->image, dsp->length)) {
226			kfree(buf);
227			return -EFAULT;
228		}
229		err = usb_set_interface(dev, 0, 1);
230		if (err)
231			snd_printk(KERN_ERR "usb_set_interface error \n");
232		else
233			err = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 2), buf, dsp->length, &lret, 6000);
234		kfree(buf);
235	}
236	if (err)
237		return err;
238	if (dsp->index == 1) {
239		msleep(250);				// give the device some time
240		err = usX2Y_AsyncSeq04_init(priv);
241		if (err) {
242			snd_printk(KERN_ERR "usX2Y_AsyncSeq04_init error \n");
243			return err;
244		}
245		err = usX2Y_In04_init(priv);
246		if (err) {
247			snd_printk(KERN_ERR "usX2Y_In04_init error \n");
248			return err;
249		}
250		err = usX2Y_create_alsa_devices(hw->card);
251		if (err) {
252			snd_printk(KERN_ERR "usX2Y_create_alsa_devices error %i \n", err);
253			snd_card_free(hw->card);
254			return err;
255		}
256		priv->chip_status |= USX2Y_STAT_CHIP_INIT;
257		snd_printdd("%s: alsa all started\n", hw->name);
258	}
259	return err;
260}
261
262
263int usX2Y_hwdep_new(struct snd_card *card, struct usb_device* device)
264{
265	int err;
266	struct snd_hwdep *hw;
267
268	if ((err = snd_hwdep_new(card, SND_USX2Y_LOADER_ID, 0, &hw)) < 0)
269		return err;
270
271	hw->iface = SNDRV_HWDEP_IFACE_USX2Y;
272	hw->private_data = usX2Y(card);
273	hw->ops.open = snd_usX2Y_hwdep_open;
274	hw->ops.release = snd_usX2Y_hwdep_release;
275	hw->ops.dsp_status = snd_usX2Y_hwdep_dsp_status;
276	hw->ops.dsp_load = snd_usX2Y_hwdep_dsp_load;
277	hw->ops.mmap = snd_us428ctls_mmap;
278	hw->ops.poll = snd_us428ctls_poll;
279	hw->exclusive = 1;
280	sprintf(hw->name, "/proc/bus/usb/%03d/%03d", device->bus->busnum, device->devnum);
281	return 0;
282}
283