1 /*
2  * Copyright (c) 2013 Federico Simoncelli
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. The name of the author may not be used to endorse or promote products
12  *    derived from this software without specific prior written permission.
13  *
14  * Alternatively, this software may be distributed under the terms of the
15  * GNU General Public License ("GPL").
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 /*
30  * Fushicai USBTV007 Audio-Video Grabber Driver
31  *
32  * Product web site:
33  * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
34  *
35  * No physical hardware was harmed running Windows during the
36  * reverse-engineering activity
37  */
38 
39 #include <sound/core.h>
40 #include <sound/initval.h>
41 #include <sound/ac97_codec.h>
42 #include <sound/pcm_params.h>
43 
44 #include "usbtv.h"
45 
46 static struct snd_pcm_hardware snd_usbtv_digital_hw = {
47 	.info = SNDRV_PCM_INFO_BATCH |
48 		SNDRV_PCM_INFO_MMAP |
49 		SNDRV_PCM_INFO_INTERLEAVED |
50 		SNDRV_PCM_INFO_BLOCK_TRANSFER |
51 		SNDRV_PCM_INFO_MMAP_VALID,
52 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
53 	.rates = SNDRV_PCM_RATE_48000,
54 	.rate_min = 48000,
55 	.rate_max = 48000,
56 	.channels_min = 2,
57 	.channels_max = 2,
58 	.period_bytes_min = 11059,
59 	.period_bytes_max = 13516,
60 	.periods_min = 2,
61 	.periods_max = 98,
62 	.buffer_bytes_max = 62720 * 8, /* value in usbaudio.c */
63 };
64 
65 static int snd_usbtv_pcm_open(struct snd_pcm_substream *substream)
66 {
67 	struct usbtv *chip = snd_pcm_substream_chip(substream);
68 	struct snd_pcm_runtime *runtime = substream->runtime;
69 
70 	chip->snd_substream = substream;
71 	runtime->hw = snd_usbtv_digital_hw;
72 
73 	return 0;
74 }
75 
76 static int snd_usbtv_pcm_close(struct snd_pcm_substream *substream)
77 {
78 	struct usbtv *chip = snd_pcm_substream_chip(substream);
79 
80 	if (atomic_read(&chip->snd_stream)) {
81 		atomic_set(&chip->snd_stream, 0);
82 		schedule_work(&chip->snd_trigger);
83 	}
84 
85 	return 0;
86 }
87 
88 static int snd_usbtv_hw_params(struct snd_pcm_substream *substream,
89 		struct snd_pcm_hw_params *hw_params)
90 {
91 	int rv;
92 	struct usbtv *chip = snd_pcm_substream_chip(substream);
93 
94 	rv = snd_pcm_lib_malloc_pages(substream,
95 		params_buffer_bytes(hw_params));
96 
97 	if (rv < 0) {
98 		dev_warn(chip->dev, "pcm audio buffer allocation failure %i\n",
99 			rv);
100 		return rv;
101 	}
102 
103 	return 0;
104 }
105 
106 static int snd_usbtv_hw_free(struct snd_pcm_substream *substream)
107 {
108 	snd_pcm_lib_free_pages(substream);
109 	return 0;
110 }
111 
112 static int snd_usbtv_prepare(struct snd_pcm_substream *substream)
113 {
114 	struct usbtv *chip = snd_pcm_substream_chip(substream);
115 
116 	chip->snd_buffer_pos = 0;
117 	chip->snd_period_pos = 0;
118 
119 	return 0;
120 }
121 
122 static void usbtv_audio_urb_received(struct urb *urb)
123 {
124 	struct usbtv *chip = urb->context;
125 	struct snd_pcm_substream *substream = chip->snd_substream;
126 	struct snd_pcm_runtime *runtime = substream->runtime;
127 	size_t i, frame_bytes, chunk_length, buffer_pos, period_pos;
128 	int period_elapsed;
129 	void *urb_current;
130 
131 	switch (urb->status) {
132 	case 0:
133 	case -ETIMEDOUT:
134 		break;
135 	case -ENOENT:
136 	case -EPROTO:
137 	case -ECONNRESET:
138 	case -ESHUTDOWN:
139 		return;
140 	default:
141 		dev_warn(chip->dev, "unknown audio urb status %i\n",
142 			urb->status);
143 	}
144 
145 	if (!atomic_read(&chip->snd_stream))
146 		return;
147 
148 	frame_bytes = runtime->frame_bits >> 3;
149 	chunk_length = USBTV_CHUNK / frame_bytes;
150 
151 	buffer_pos = chip->snd_buffer_pos;
152 	period_pos = chip->snd_period_pos;
153 	period_elapsed = 0;
154 
155 	for (i = 0; i < urb->actual_length; i += USBTV_CHUNK_SIZE) {
156 		urb_current = urb->transfer_buffer + i + USBTV_AUDIO_HDRSIZE;
157 
158 		if (buffer_pos + chunk_length >= runtime->buffer_size) {
159 			size_t cnt = (runtime->buffer_size - buffer_pos) *
160 				frame_bytes;
161 			memcpy(runtime->dma_area + buffer_pos * frame_bytes,
162 				urb_current, cnt);
163 			memcpy(runtime->dma_area, urb_current + cnt,
164 				chunk_length * frame_bytes - cnt);
165 		} else {
166 			memcpy(runtime->dma_area + buffer_pos * frame_bytes,
167 				urb_current, chunk_length * frame_bytes);
168 		}
169 
170 		buffer_pos += chunk_length;
171 		period_pos += chunk_length;
172 
173 		if (buffer_pos >= runtime->buffer_size)
174 			buffer_pos -= runtime->buffer_size;
175 
176 		if (period_pos >= runtime->period_size) {
177 			period_pos -= runtime->period_size;
178 			period_elapsed = 1;
179 		}
180 	}
181 
182 	snd_pcm_stream_lock(substream);
183 
184 	chip->snd_buffer_pos = buffer_pos;
185 	chip->snd_period_pos = period_pos;
186 
187 	snd_pcm_stream_unlock(substream);
188 
189 	if (period_elapsed)
190 		snd_pcm_period_elapsed(substream);
191 
192 	usb_submit_urb(urb, GFP_ATOMIC);
193 }
194 
195 static int usbtv_audio_start(struct usbtv *chip)
196 {
197 	unsigned int pipe;
198 	static const u16 setup[][2] = {
199 		/* These seem to enable the device. */
200 		{ USBTV_BASE + 0x0008, 0x0001 },
201 		{ USBTV_BASE + 0x01d0, 0x00ff },
202 		{ USBTV_BASE + 0x01d9, 0x0002 },
203 
204 		{ USBTV_BASE + 0x01da, 0x0013 },
205 		{ USBTV_BASE + 0x01db, 0x0012 },
206 		{ USBTV_BASE + 0x01e9, 0x0002 },
207 		{ USBTV_BASE + 0x01ec, 0x006c },
208 		{ USBTV_BASE + 0x0294, 0x0020 },
209 		{ USBTV_BASE + 0x0255, 0x00cf },
210 		{ USBTV_BASE + 0x0256, 0x0020 },
211 		{ USBTV_BASE + 0x01eb, 0x0030 },
212 		{ USBTV_BASE + 0x027d, 0x00a6 },
213 		{ USBTV_BASE + 0x0280, 0x0011 },
214 		{ USBTV_BASE + 0x0281, 0x0040 },
215 		{ USBTV_BASE + 0x0282, 0x0011 },
216 		{ USBTV_BASE + 0x0283, 0x0040 },
217 		{ 0xf891, 0x0010 },
218 
219 		/* this sets the input from composite */
220 		{ USBTV_BASE + 0x0284, 0x00aa },
221 	};
222 
223 	chip->snd_bulk_urb = usb_alloc_urb(0, GFP_KERNEL);
224 	if (chip->snd_bulk_urb == NULL)
225 		goto err_alloc_urb;
226 
227 	pipe = usb_rcvbulkpipe(chip->udev, USBTV_AUDIO_ENDP);
228 
229 	chip->snd_bulk_urb->transfer_buffer = kzalloc(
230 		USBTV_AUDIO_URBSIZE, GFP_KERNEL);
231 	if (chip->snd_bulk_urb->transfer_buffer == NULL)
232 		goto err_transfer_buffer;
233 
234 	usb_fill_bulk_urb(chip->snd_bulk_urb, chip->udev, pipe,
235 		chip->snd_bulk_urb->transfer_buffer, USBTV_AUDIO_URBSIZE,
236 		usbtv_audio_urb_received, chip);
237 
238 	/* starting the stream */
239 	usbtv_set_regs(chip, setup, ARRAY_SIZE(setup));
240 
241 	usb_clear_halt(chip->udev, pipe);
242 	usb_submit_urb(chip->snd_bulk_urb, GFP_ATOMIC);
243 
244 	return 0;
245 
246 err_transfer_buffer:
247 	usb_free_urb(chip->snd_bulk_urb);
248 	chip->snd_bulk_urb = NULL;
249 
250 err_alloc_urb:
251 	return -ENOMEM;
252 }
253 
254 static int usbtv_audio_stop(struct usbtv *chip)
255 {
256 	static const u16 setup[][2] = {
257 	/* The original windows driver sometimes sends also:
258 	 *   { USBTV_BASE + 0x00a2, 0x0013 }
259 	 * but it seems useless and its real effects are untested at
260 	 * the moment.
261 	 */
262 		{ USBTV_BASE + 0x027d, 0x0000 },
263 		{ USBTV_BASE + 0x0280, 0x0010 },
264 		{ USBTV_BASE + 0x0282, 0x0010 },
265 	};
266 
267 	if (chip->snd_bulk_urb) {
268 		usb_kill_urb(chip->snd_bulk_urb);
269 		kfree(chip->snd_bulk_urb->transfer_buffer);
270 		usb_free_urb(chip->snd_bulk_urb);
271 		chip->snd_bulk_urb = NULL;
272 	}
273 
274 	usbtv_set_regs(chip, setup, ARRAY_SIZE(setup));
275 
276 	return 0;
277 }
278 
279 void usbtv_audio_suspend(struct usbtv *usbtv)
280 {
281 	if (atomic_read(&usbtv->snd_stream) && usbtv->snd_bulk_urb)
282 		usb_kill_urb(usbtv->snd_bulk_urb);
283 }
284 
285 void usbtv_audio_resume(struct usbtv *usbtv)
286 {
287 	if (atomic_read(&usbtv->snd_stream) && usbtv->snd_bulk_urb)
288 		usb_submit_urb(usbtv->snd_bulk_urb, GFP_ATOMIC);
289 }
290 
291 static void snd_usbtv_trigger(struct work_struct *work)
292 {
293 	struct usbtv *chip = container_of(work, struct usbtv, snd_trigger);
294 
295 	if (!chip->snd)
296 		return;
297 
298 	if (atomic_read(&chip->snd_stream))
299 		usbtv_audio_start(chip);
300 	else
301 		usbtv_audio_stop(chip);
302 }
303 
304 static int snd_usbtv_card_trigger(struct snd_pcm_substream *substream, int cmd)
305 {
306 	struct usbtv *chip = snd_pcm_substream_chip(substream);
307 
308 	switch (cmd) {
309 	case SNDRV_PCM_TRIGGER_START:
310 	case SNDRV_PCM_TRIGGER_RESUME:
311 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
312 		atomic_set(&chip->snd_stream, 1);
313 		break;
314 	case SNDRV_PCM_TRIGGER_STOP:
315 	case SNDRV_PCM_TRIGGER_SUSPEND:
316 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
317 		atomic_set(&chip->snd_stream, 0);
318 		break;
319 	default:
320 		return -EINVAL;
321 	}
322 
323 	schedule_work(&chip->snd_trigger);
324 
325 	return 0;
326 }
327 
328 static snd_pcm_uframes_t snd_usbtv_pointer(struct snd_pcm_substream *substream)
329 {
330 	struct usbtv *chip = snd_pcm_substream_chip(substream);
331 
332 	return chip->snd_buffer_pos;
333 }
334 
335 static const struct snd_pcm_ops snd_usbtv_pcm_ops = {
336 	.open = snd_usbtv_pcm_open,
337 	.close = snd_usbtv_pcm_close,
338 	.ioctl = snd_pcm_lib_ioctl,
339 	.hw_params = snd_usbtv_hw_params,
340 	.hw_free = snd_usbtv_hw_free,
341 	.prepare = snd_usbtv_prepare,
342 	.trigger = snd_usbtv_card_trigger,
343 	.pointer = snd_usbtv_pointer,
344 };
345 
346 int usbtv_audio_init(struct usbtv *usbtv)
347 {
348 	int rv;
349 	struct snd_card *card;
350 	struct snd_pcm *pcm;
351 
352 	INIT_WORK(&usbtv->snd_trigger, snd_usbtv_trigger);
353 	atomic_set(&usbtv->snd_stream, 0);
354 
355 	rv = snd_card_new(&usbtv->udev->dev, SNDRV_DEFAULT_IDX1, "usbtv",
356 		THIS_MODULE, 0, &card);
357 	if (rv < 0)
358 		return rv;
359 
360 	strlcpy(card->driver, usbtv->dev->driver->name, sizeof(card->driver));
361 	strlcpy(card->shortname, "usbtv", sizeof(card->shortname));
362 	snprintf(card->longname, sizeof(card->longname),
363 		"USBTV Audio at bus %d device %d", usbtv->udev->bus->busnum,
364 		usbtv->udev->devnum);
365 
366 	snd_card_set_dev(card, usbtv->dev);
367 
368 	usbtv->snd = card;
369 
370 	rv = snd_pcm_new(card, "USBTV Audio", 0, 0, 1, &pcm);
371 	if (rv < 0)
372 		goto err;
373 
374 	strlcpy(pcm->name, "USBTV Audio Input", sizeof(pcm->name));
375 	pcm->info_flags = 0;
376 	pcm->private_data = usbtv;
377 
378 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usbtv_pcm_ops);
379 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
380 		snd_dma_continuous_data(GFP_KERNEL), USBTV_AUDIO_BUFFER,
381 		USBTV_AUDIO_BUFFER);
382 
383 	rv = snd_card_register(card);
384 	if (rv)
385 		goto err;
386 
387 	return 0;
388 
389 err:
390 	usbtv->snd = NULL;
391 	snd_card_free(card);
392 
393 	return rv;
394 }
395 
396 void usbtv_audio_free(struct usbtv *usbtv)
397 {
398 	cancel_work_sync(&usbtv->snd_trigger);
399 
400 	if (usbtv->snd && usbtv->udev) {
401 		snd_card_free(usbtv->snd);
402 		usbtv->snd = NULL;
403 	}
404 }
405