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