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 const 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 	unsigned long flags;
130 	void *urb_current;
131 
132 	switch (urb->status) {
133 	case 0:
134 	case -ETIMEDOUT:
135 		break;
136 	case -ENOENT:
137 	case -EPROTO:
138 	case -ECONNRESET:
139 	case -ESHUTDOWN:
140 		return;
141 	default:
142 		dev_warn(chip->dev, "unknown audio urb status %i\n",
143 			urb->status);
144 	}
145 
146 	if (!atomic_read(&chip->snd_stream))
147 		return;
148 
149 	frame_bytes = runtime->frame_bits >> 3;
150 	chunk_length = USBTV_CHUNK / frame_bytes;
151 
152 	buffer_pos = chip->snd_buffer_pos;
153 	period_pos = chip->snd_period_pos;
154 	period_elapsed = 0;
155 
156 	for (i = 0; i < urb->actual_length; i += USBTV_CHUNK_SIZE) {
157 		urb_current = urb->transfer_buffer + i + USBTV_AUDIO_HDRSIZE;
158 
159 		if (buffer_pos + chunk_length >= runtime->buffer_size) {
160 			size_t cnt = (runtime->buffer_size - buffer_pos) *
161 				frame_bytes;
162 			memcpy(runtime->dma_area + buffer_pos * frame_bytes,
163 				urb_current, cnt);
164 			memcpy(runtime->dma_area, urb_current + cnt,
165 				chunk_length * frame_bytes - cnt);
166 		} else {
167 			memcpy(runtime->dma_area + buffer_pos * frame_bytes,
168 				urb_current, chunk_length * frame_bytes);
169 		}
170 
171 		buffer_pos += chunk_length;
172 		period_pos += chunk_length;
173 
174 		if (buffer_pos >= runtime->buffer_size)
175 			buffer_pos -= runtime->buffer_size;
176 
177 		if (period_pos >= runtime->period_size) {
178 			period_pos -= runtime->period_size;
179 			period_elapsed = 1;
180 		}
181 	}
182 
183 	snd_pcm_stream_lock_irqsave(substream, flags);
184 
185 	chip->snd_buffer_pos = buffer_pos;
186 	chip->snd_period_pos = period_pos;
187 
188 	snd_pcm_stream_unlock_irqrestore(substream, flags);
189 
190 	if (period_elapsed)
191 		snd_pcm_period_elapsed(substream);
192 
193 	usb_submit_urb(urb, GFP_ATOMIC);
194 }
195 
196 static int usbtv_audio_start(struct usbtv *chip)
197 {
198 	unsigned int pipe;
199 	static const u16 setup[][2] = {
200 		/* These seem to enable the device. */
201 		{ USBTV_BASE + 0x0008, 0x0001 },
202 		{ USBTV_BASE + 0x01d0, 0x00ff },
203 		{ USBTV_BASE + 0x01d9, 0x0002 },
204 
205 		{ USBTV_BASE + 0x01da, 0x0013 },
206 		{ USBTV_BASE + 0x01db, 0x0012 },
207 		{ USBTV_BASE + 0x01e9, 0x0002 },
208 		{ USBTV_BASE + 0x01ec, 0x006c },
209 		{ USBTV_BASE + 0x0294, 0x0020 },
210 		{ USBTV_BASE + 0x0255, 0x00cf },
211 		{ USBTV_BASE + 0x0256, 0x0020 },
212 		{ USBTV_BASE + 0x01eb, 0x0030 },
213 		{ USBTV_BASE + 0x027d, 0x00a6 },
214 		{ USBTV_BASE + 0x0280, 0x0011 },
215 		{ USBTV_BASE + 0x0281, 0x0040 },
216 		{ USBTV_BASE + 0x0282, 0x0011 },
217 		{ USBTV_BASE + 0x0283, 0x0040 },
218 		{ 0xf891, 0x0010 },
219 
220 		/* this sets the input from composite */
221 		{ USBTV_BASE + 0x0284, 0x00aa },
222 	};
223 
224 	chip->snd_bulk_urb = usb_alloc_urb(0, GFP_KERNEL);
225 	if (chip->snd_bulk_urb == NULL)
226 		goto err_alloc_urb;
227 
228 	pipe = usb_rcvbulkpipe(chip->udev, USBTV_AUDIO_ENDP);
229 
230 	chip->snd_bulk_urb->transfer_buffer = kzalloc(
231 		USBTV_AUDIO_URBSIZE, GFP_KERNEL);
232 	if (chip->snd_bulk_urb->transfer_buffer == NULL)
233 		goto err_transfer_buffer;
234 
235 	usb_fill_bulk_urb(chip->snd_bulk_urb, chip->udev, pipe,
236 		chip->snd_bulk_urb->transfer_buffer, USBTV_AUDIO_URBSIZE,
237 		usbtv_audio_urb_received, chip);
238 
239 	/* starting the stream */
240 	usbtv_set_regs(chip, setup, ARRAY_SIZE(setup));
241 
242 	usb_clear_halt(chip->udev, pipe);
243 	usb_submit_urb(chip->snd_bulk_urb, GFP_ATOMIC);
244 
245 	return 0;
246 
247 err_transfer_buffer:
248 	usb_free_urb(chip->snd_bulk_urb);
249 	chip->snd_bulk_urb = NULL;
250 
251 err_alloc_urb:
252 	return -ENOMEM;
253 }
254 
255 static int usbtv_audio_stop(struct usbtv *chip)
256 {
257 	static const u16 setup[][2] = {
258 	/* The original windows driver sometimes sends also:
259 	 *   { USBTV_BASE + 0x00a2, 0x0013 }
260 	 * but it seems useless and its real effects are untested at
261 	 * the moment.
262 	 */
263 		{ USBTV_BASE + 0x027d, 0x0000 },
264 		{ USBTV_BASE + 0x0280, 0x0010 },
265 		{ USBTV_BASE + 0x0282, 0x0010 },
266 	};
267 
268 	if (chip->snd_bulk_urb) {
269 		usb_kill_urb(chip->snd_bulk_urb);
270 		kfree(chip->snd_bulk_urb->transfer_buffer);
271 		usb_free_urb(chip->snd_bulk_urb);
272 		chip->snd_bulk_urb = NULL;
273 	}
274 
275 	usbtv_set_regs(chip, setup, ARRAY_SIZE(setup));
276 
277 	return 0;
278 }
279 
280 void usbtv_audio_suspend(struct usbtv *usbtv)
281 {
282 	if (atomic_read(&usbtv->snd_stream) && usbtv->snd_bulk_urb)
283 		usb_kill_urb(usbtv->snd_bulk_urb);
284 }
285 
286 void usbtv_audio_resume(struct usbtv *usbtv)
287 {
288 	if (atomic_read(&usbtv->snd_stream) && usbtv->snd_bulk_urb)
289 		usb_submit_urb(usbtv->snd_bulk_urb, GFP_ATOMIC);
290 }
291 
292 static void snd_usbtv_trigger(struct work_struct *work)
293 {
294 	struct usbtv *chip = container_of(work, struct usbtv, snd_trigger);
295 
296 	if (!chip->snd)
297 		return;
298 
299 	if (atomic_read(&chip->snd_stream))
300 		usbtv_audio_start(chip);
301 	else
302 		usbtv_audio_stop(chip);
303 }
304 
305 static int snd_usbtv_card_trigger(struct snd_pcm_substream *substream, int cmd)
306 {
307 	struct usbtv *chip = snd_pcm_substream_chip(substream);
308 
309 	switch (cmd) {
310 	case SNDRV_PCM_TRIGGER_START:
311 	case SNDRV_PCM_TRIGGER_RESUME:
312 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
313 		atomic_set(&chip->snd_stream, 1);
314 		break;
315 	case SNDRV_PCM_TRIGGER_STOP:
316 	case SNDRV_PCM_TRIGGER_SUSPEND:
317 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
318 		atomic_set(&chip->snd_stream, 0);
319 		break;
320 	default:
321 		return -EINVAL;
322 	}
323 
324 	schedule_work(&chip->snd_trigger);
325 
326 	return 0;
327 }
328 
329 static snd_pcm_uframes_t snd_usbtv_pointer(struct snd_pcm_substream *substream)
330 {
331 	struct usbtv *chip = snd_pcm_substream_chip(substream);
332 
333 	return chip->snd_buffer_pos;
334 }
335 
336 static const struct snd_pcm_ops snd_usbtv_pcm_ops = {
337 	.open = snd_usbtv_pcm_open,
338 	.close = snd_usbtv_pcm_close,
339 	.ioctl = snd_pcm_lib_ioctl,
340 	.hw_params = snd_usbtv_hw_params,
341 	.hw_free = snd_usbtv_hw_free,
342 	.prepare = snd_usbtv_prepare,
343 	.trigger = snd_usbtv_card_trigger,
344 	.pointer = snd_usbtv_pointer,
345 };
346 
347 int usbtv_audio_init(struct usbtv *usbtv)
348 {
349 	int rv;
350 	struct snd_card *card;
351 	struct snd_pcm *pcm;
352 
353 	INIT_WORK(&usbtv->snd_trigger, snd_usbtv_trigger);
354 	atomic_set(&usbtv->snd_stream, 0);
355 
356 	rv = snd_card_new(&usbtv->udev->dev, SNDRV_DEFAULT_IDX1, "usbtv",
357 		THIS_MODULE, 0, &card);
358 	if (rv < 0)
359 		return rv;
360 
361 	strlcpy(card->driver, usbtv->dev->driver->name, sizeof(card->driver));
362 	strlcpy(card->shortname, "usbtv", sizeof(card->shortname));
363 	snprintf(card->longname, sizeof(card->longname),
364 		"USBTV Audio at bus %d device %d", usbtv->udev->bus->busnum,
365 		usbtv->udev->devnum);
366 
367 	snd_card_set_dev(card, usbtv->dev);
368 
369 	usbtv->snd = card;
370 
371 	rv = snd_pcm_new(card, "USBTV Audio", 0, 0, 1, &pcm);
372 	if (rv < 0)
373 		goto err;
374 
375 	strlcpy(pcm->name, "USBTV Audio Input", sizeof(pcm->name));
376 	pcm->info_flags = 0;
377 	pcm->private_data = usbtv;
378 
379 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usbtv_pcm_ops);
380 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
381 		snd_dma_continuous_data(GFP_KERNEL), USBTV_AUDIO_BUFFER,
382 		USBTV_AUDIO_BUFFER);
383 
384 	rv = snd_card_register(card);
385 	if (rv)
386 		goto err;
387 
388 	return 0;
389 
390 err:
391 	usbtv->snd = NULL;
392 	snd_card_free(card);
393 
394 	return rv;
395 }
396 
397 void usbtv_audio_free(struct usbtv *usbtv)
398 {
399 	cancel_work_sync(&usbtv->snd_trigger);
400 
401 	if (usbtv->snd && usbtv->udev) {
402 		snd_card_free(usbtv->snd);
403 		usbtv->snd = NULL;
404 	}
405 }
406