xref: /openbmc/linux/sound/usb/usx2y/usX2Yhwdep.c (revision 423997ff)
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 <linux/interrupt.h>
24 #include <linux/slab.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 
34 static vm_fault_t snd_us428ctls_vm_fault(struct vm_fault *vmf)
35 {
36 	unsigned long offset;
37 	struct page * page;
38 	void *vaddr;
39 
40 	snd_printdd("ENTER, start %lXh, pgoff %ld\n",
41 		   vmf->vma->vm_start,
42 		   vmf->pgoff);
43 
44 	offset = vmf->pgoff << PAGE_SHIFT;
45 	vaddr = (char *)((struct usX2Ydev *)vmf->vma->vm_private_data)->us428ctls_sharedmem + offset;
46 	page = virt_to_page(vaddr);
47 	get_page(page);
48 	vmf->page = page;
49 
50 	snd_printdd("vaddr=%p made us428ctls_vm_fault() page %p\n",
51 		    vaddr, page);
52 
53 	return 0;
54 }
55 
56 static const struct vm_operations_struct us428ctls_vm_ops = {
57 	.fault = snd_us428ctls_vm_fault,
58 };
59 
60 static int snd_us428ctls_mmap(struct snd_hwdep * hw, struct file *filp, struct vm_area_struct *area)
61 {
62 	unsigned long	size = (unsigned long)(area->vm_end - area->vm_start);
63 	struct usX2Ydev	*us428 = hw->private_data;
64 
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.
67  	if (!(us428->chip_status & USX2Y_STAT_CHIP_INIT))
68 		return -EBUSY;
69 
70 	/* if userspace tries to mmap beyond end of our buffer, fail */
71         if (size > PAGE_ALIGN(sizeof(struct us428ctls_sharedmem))) {
72 		snd_printd( "%lu > %lu\n", size, (unsigned long)sizeof(struct us428ctls_sharedmem));
73                 return -EINVAL;
74 	}
75 
76 	if (!us428->us428ctls_sharedmem) {
77 		init_waitqueue_head(&us428->us428ctls_wait_queue_head);
78 		us428->us428ctls_sharedmem = alloc_pages_exact(sizeof(struct us428ctls_sharedmem), GFP_KERNEL);
79 		if (!us428->us428ctls_sharedmem)
80 			return -ENOMEM;
81 		memset(us428->us428ctls_sharedmem, -1, sizeof(struct us428ctls_sharedmem));
82 		us428->us428ctls_sharedmem->CtlSnapShotLast = -2;
83 	}
84 	area->vm_ops = &us428ctls_vm_ops;
85 	area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
86 	area->vm_private_data = hw->private_data;
87 	return 0;
88 }
89 
90 static __poll_t snd_us428ctls_poll(struct snd_hwdep *hw, struct file *file, poll_table *wait)
91 {
92 	__poll_t	mask = 0;
93 	struct usX2Ydev	*us428 = hw->private_data;
94 	struct us428ctls_sharedmem *shm = us428->us428ctls_sharedmem;
95 	if (us428->chip_status & USX2Y_STAT_CHIP_HUP)
96 		return EPOLLHUP;
97 
98 	poll_wait(file, &us428->us428ctls_wait_queue_head, wait);
99 
100 	if (shm != NULL && shm->CtlSnapShotLast != shm->CtlSnapShotRed)
101 		mask |= EPOLLIN;
102 
103 	return mask;
104 }
105 
106 
107 static int snd_usX2Y_hwdep_dsp_status(struct snd_hwdep *hw,
108 				      struct snd_hwdep_dsp_status *info)
109 {
110 	static char *type_ids[USX2Y_TYPE_NUMS] = {
111 		[USX2Y_TYPE_122] = "us122",
112 		[USX2Y_TYPE_224] = "us224",
113 		[USX2Y_TYPE_428] = "us428",
114 	};
115 	struct usX2Ydev	*us428 = hw->private_data;
116 	int id = -1;
117 
118 	switch (le16_to_cpu(us428->dev->descriptor.idProduct)) {
119 	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
133 	if (us428->chip_status & USX2Y_STAT_CHIP_INIT)
134 		info->chip_ready = 1;
135  	info->version = USX2Y_DRIVER_VERSION;
136 	return 0;
137 }
138 
139 
140 static int usX2Y_create_usbmidi(struct snd_card *card)
141 {
142 	static struct snd_usb_midi_endpoint_info quirk_data_1 = {
143 		.out_ep = 0x06,
144 		.in_ep = 0x06,
145 		.out_cables =	0x001,
146 		.in_cables =	0x001
147 	};
148 	static struct snd_usb_audio_quirk quirk_1 = {
149 		.vendor_name =	"TASCAM",
150 		.product_name =	NAME_ALLCAPS,
151 		.ifnum = 	0,
152        		.type = QUIRK_MIDI_FIXED_ENDPOINT,
153 		.data = &quirk_data_1
154 	};
155 	static struct snd_usb_midi_endpoint_info quirk_data_2 = {
156 		.out_ep = 0x06,
157 		.in_ep = 0x06,
158 		.out_cables =	0x003,
159 		.in_cables =	0x003
160 	};
161 	static struct snd_usb_audio_quirk quirk_2 = {
162 		.vendor_name =	"TASCAM",
163 		.product_name =	"US428",
164 		.ifnum = 	0,
165        		.type = QUIRK_MIDI_FIXED_ENDPOINT,
166 		.data = &quirk_data_2
167 	};
168 	struct usb_device *dev = usX2Y(card)->dev;
169 	struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
170 	struct snd_usb_audio_quirk *quirk =
171 		le16_to_cpu(dev->descriptor.idProduct) == USB_ID_US428 ?
172 		&quirk_2 : &quirk_1;
173 
174 	snd_printdd("usX2Y_create_usbmidi \n");
175 	return snd_usbmidi_create(card, iface, &usX2Y(card)->midi_list, quirk);
176 }
177 
178 static int usX2Y_create_alsa_devices(struct snd_card *card)
179 {
180 	int err;
181 
182 	do {
183 		if ((err = usX2Y_create_usbmidi(card)) < 0) {
184 			snd_printk(KERN_ERR "usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err);
185 			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 
198 static int snd_usX2Y_hwdep_dsp_load(struct snd_hwdep *hw,
199 				    struct snd_hwdep_dsp_image *dsp)
200 {
201 	struct usX2Ydev *priv = hw->private_data;
202 	struct usb_device* dev = priv->dev;
203 	int lret, err;
204 	char *buf;
205 
206 	snd_printdd( "dsp_load %s\n", dsp->name);
207 
208 	buf = memdup_user(dsp->image, dsp->length);
209 	if (IS_ERR(buf))
210 		return PTR_ERR(buf);
211 
212 	err = usb_set_interface(dev, 0, 1);
213 	if (err)
214 		snd_printk(KERN_ERR "usb_set_interface error \n");
215 	else
216 		err = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 2), buf, dsp->length, &lret, 6000);
217 	kfree(buf);
218 	if (err)
219 		return err;
220 	if (dsp->index == 1) {
221 		msleep(250);				// give the device some time
222 		err = usX2Y_AsyncSeq04_init(priv);
223 		if (err) {
224 			snd_printk(KERN_ERR "usX2Y_AsyncSeq04_init error \n");
225 			return err;
226 		}
227 		err = usX2Y_In04_init(priv);
228 		if (err) {
229 			snd_printk(KERN_ERR "usX2Y_In04_init error \n");
230 			return err;
231 		}
232 		err = usX2Y_create_alsa_devices(hw->card);
233 		if (err) {
234 			snd_printk(KERN_ERR "usX2Y_create_alsa_devices error %i \n", err);
235 			snd_card_free(hw->card);
236 			return err;
237 		}
238 		priv->chip_status |= USX2Y_STAT_CHIP_INIT;
239 		snd_printdd("%s: alsa all started\n", hw->name);
240 	}
241 	return err;
242 }
243 
244 
245 int usX2Y_hwdep_new(struct snd_card *card, struct usb_device* device)
246 {
247 	int err;
248 	struct snd_hwdep *hw;
249 
250 	if ((err = snd_hwdep_new(card, SND_USX2Y_LOADER_ID, 0, &hw)) < 0)
251 		return err;
252 
253 	hw->iface = SNDRV_HWDEP_IFACE_USX2Y;
254 	hw->private_data = usX2Y(card);
255 	hw->ops.dsp_status = snd_usX2Y_hwdep_dsp_status;
256 	hw->ops.dsp_load = snd_usX2Y_hwdep_dsp_load;
257 	hw->ops.mmap = snd_us428ctls_mmap;
258 	hw->ops.poll = snd_us428ctls_poll;
259 	hw->exclusive = 1;
260 	sprintf(hw->name, "/dev/bus/usb/%03d/%03d", device->bus->busnum, device->devnum);
261 	return 0;
262 }
263 
264