2 * Copyright (c) 2013 Federico Simoncelli
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * Alternatively, this software may be distributed under the terms of the
15 * GNU General Public License ("GPL").
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.
30 * Fushicai USBTV007 Audio-Video Grabber Driver
33 * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
35 * No physical hardware was harmed running Windows during the
36 * reverse-engineering activity
39 #include <sound/core.h>
40 #include <sound/initval.h>
41 #include <sound/ac97_codec.h>
42 #include <sound/pcm_params.h>
46 static const struct snd_pcm_hardware snd_usbtv_digital_hw
= {
47 .info
= SNDRV_PCM_INFO_BATCH
|
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
,
58 .period_bytes_min
= 11059,
59 .period_bytes_max
= 13516,
62 .buffer_bytes_max
= 62720 * 8, /* value in usbaudio.c */
65 static int snd_usbtv_pcm_open(struct snd_pcm_substream
*substream
)
67 struct usbtv
*chip
= snd_pcm_substream_chip(substream
);
68 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
70 chip
->snd_substream
= substream
;
71 runtime
->hw
= snd_usbtv_digital_hw
;
76 static int snd_usbtv_pcm_close(struct snd_pcm_substream
*substream
)
78 struct usbtv
*chip
= snd_pcm_substream_chip(substream
);
80 if (atomic_read(&chip
->snd_stream
)) {
81 atomic_set(&chip
->snd_stream
, 0);
82 schedule_work(&chip
->snd_trigger
);
88 static int snd_usbtv_hw_params(struct snd_pcm_substream
*substream
,
89 struct snd_pcm_hw_params
*hw_params
)
92 struct usbtv
*chip
= snd_pcm_substream_chip(substream
);
94 rv
= snd_pcm_lib_malloc_pages(substream
,
95 params_buffer_bytes(hw_params
));
98 dev_warn(chip
->dev
, "pcm audio buffer allocation failure %i\n",
106 static int snd_usbtv_hw_free(struct snd_pcm_substream
*substream
)
108 snd_pcm_lib_free_pages(substream
);
112 static int snd_usbtv_prepare(struct snd_pcm_substream
*substream
)
114 struct usbtv
*chip
= snd_pcm_substream_chip(substream
);
116 chip
->snd_buffer_pos
= 0;
117 chip
->snd_period_pos
= 0;
122 static void usbtv_audio_urb_received(struct urb
*urb
)
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
;
132 switch (urb
->status
) {
142 dev_warn(chip
->dev
, "unknown audio urb status %i\n",
146 if (!atomic_read(&chip
->snd_stream
))
149 frame_bytes
= runtime
->frame_bits
>> 3;
150 chunk_length
= USBTV_CHUNK
/ frame_bytes
;
152 buffer_pos
= chip
->snd_buffer_pos
;
153 period_pos
= chip
->snd_period_pos
;
156 for (i
= 0; i
< urb
->actual_length
; i
+= USBTV_CHUNK_SIZE
) {
157 urb_current
= urb
->transfer_buffer
+ i
+ USBTV_AUDIO_HDRSIZE
;
159 if (buffer_pos
+ chunk_length
>= runtime
->buffer_size
) {
160 size_t cnt
= (runtime
->buffer_size
- buffer_pos
) *
162 memcpy(runtime
->dma_area
+ buffer_pos
* frame_bytes
,
164 memcpy(runtime
->dma_area
, urb_current
+ cnt
,
165 chunk_length
* frame_bytes
- cnt
);
167 memcpy(runtime
->dma_area
+ buffer_pos
* frame_bytes
,
168 urb_current
, chunk_length
* frame_bytes
);
171 buffer_pos
+= chunk_length
;
172 period_pos
+= chunk_length
;
174 if (buffer_pos
>= runtime
->buffer_size
)
175 buffer_pos
-= runtime
->buffer_size
;
177 if (period_pos
>= runtime
->period_size
) {
178 period_pos
-= runtime
->period_size
;
183 snd_pcm_stream_lock_irqsave(substream
, flags
);
185 chip
->snd_buffer_pos
= buffer_pos
;
186 chip
->snd_period_pos
= period_pos
;
188 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
191 snd_pcm_period_elapsed(substream
);
193 usb_submit_urb(urb
, GFP_ATOMIC
);
196 static int usbtv_audio_start(struct usbtv
*chip
)
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 },
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 },
220 /* this sets the input from composite */
221 { USBTV_BASE
+ 0x0284, 0x00aa },
224 chip
->snd_bulk_urb
= usb_alloc_urb(0, GFP_KERNEL
);
225 if (chip
->snd_bulk_urb
== NULL
)
228 pipe
= usb_rcvbulkpipe(chip
->udev
, USBTV_AUDIO_ENDP
);
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
;
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
);
239 /* starting the stream */
240 usbtv_set_regs(chip
, setup
, ARRAY_SIZE(setup
));
242 usb_clear_halt(chip
->udev
, pipe
);
243 usb_submit_urb(chip
->snd_bulk_urb
, GFP_ATOMIC
);
248 usb_free_urb(chip
->snd_bulk_urb
);
249 chip
->snd_bulk_urb
= NULL
;
255 static int usbtv_audio_stop(struct usbtv
*chip
)
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
263 { USBTV_BASE
+ 0x027d, 0x0000 },
264 { USBTV_BASE
+ 0x0280, 0x0010 },
265 { USBTV_BASE
+ 0x0282, 0x0010 },
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
;
275 usbtv_set_regs(chip
, setup
, ARRAY_SIZE(setup
));
280 void usbtv_audio_suspend(struct usbtv
*usbtv
)
282 if (atomic_read(&usbtv
->snd_stream
) && usbtv
->snd_bulk_urb
)
283 usb_kill_urb(usbtv
->snd_bulk_urb
);
286 void usbtv_audio_resume(struct usbtv
*usbtv
)
288 if (atomic_read(&usbtv
->snd_stream
) && usbtv
->snd_bulk_urb
)
289 usb_submit_urb(usbtv
->snd_bulk_urb
, GFP_ATOMIC
);
292 static void snd_usbtv_trigger(struct work_struct
*work
)
294 struct usbtv
*chip
= container_of(work
, struct usbtv
, snd_trigger
);
299 if (atomic_read(&chip
->snd_stream
))
300 usbtv_audio_start(chip
);
302 usbtv_audio_stop(chip
);
305 static int snd_usbtv_card_trigger(struct snd_pcm_substream
*substream
, int cmd
)
307 struct usbtv
*chip
= snd_pcm_substream_chip(substream
);
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);
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);
324 schedule_work(&chip
->snd_trigger
);
329 static snd_pcm_uframes_t
snd_usbtv_pointer(struct snd_pcm_substream
*substream
)
331 struct usbtv
*chip
= snd_pcm_substream_chip(substream
);
333 return chip
->snd_buffer_pos
;
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
,
347 int usbtv_audio_init(struct usbtv
*usbtv
)
350 struct snd_card
*card
;
353 INIT_WORK(&usbtv
->snd_trigger
, snd_usbtv_trigger
);
354 atomic_set(&usbtv
->snd_stream
, 0);
356 rv
= snd_card_new(&usbtv
->udev
->dev
, SNDRV_DEFAULT_IDX1
, "usbtv",
357 THIS_MODULE
, 0, &card
);
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
);
367 snd_card_set_dev(card
, usbtv
->dev
);
371 rv
= snd_pcm_new(card
, "USBTV Audio", 0, 0, 1, &pcm
);
375 strlcpy(pcm
->name
, "USBTV Audio Input", sizeof(pcm
->name
));
377 pcm
->private_data
= usbtv
;
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
,
384 rv
= snd_card_register(card
);
397 void usbtv_audio_free(struct usbtv
*usbtv
)
399 cancel_work_sync(&usbtv
->snd_trigger
);
401 if (usbtv
->snd
&& usbtv
->udev
) {
402 snd_card_free(usbtv
->snd
);