2 * Fushicai USBTV007 Audio-Video Grabber Driver
5 * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
7 * Copyright (c) 2013 Federico Simoncelli
9 * No physical hardware was harmed running Windows during the
10 * reverse-engineering activity
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
21 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL").
25 #include <sound/core.h>
26 #include <sound/initval.h>
27 #include <sound/ac97_codec.h>
28 #include <sound/pcm_params.h>
32 static struct snd_pcm_hardware snd_usbtv_digital_hw
= {
33 .info
= SNDRV_PCM_INFO_BATCH
|
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
,
44 .period_bytes_min
= 11059,
45 .period_bytes_max
= 13516,
48 .buffer_bytes_max
= 62720 * 8, /* value in usbaudio.c */
51 static int snd_usbtv_pcm_open(struct snd_pcm_substream
*substream
)
53 struct usbtv
*chip
= snd_pcm_substream_chip(substream
);
54 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
56 chip
->snd_substream
= substream
;
57 runtime
->hw
= snd_usbtv_digital_hw
;
62 static int snd_usbtv_pcm_close(struct snd_pcm_substream
*substream
)
64 struct usbtv
*chip
= snd_pcm_substream_chip(substream
);
66 if (atomic_read(&chip
->snd_stream
)) {
67 atomic_set(&chip
->snd_stream
, 0);
68 schedule_work(&chip
->snd_trigger
);
74 static int snd_usbtv_hw_params(struct snd_pcm_substream
*substream
,
75 struct snd_pcm_hw_params
*hw_params
)
78 struct usbtv
*chip
= snd_pcm_substream_chip(substream
);
80 rv
= snd_pcm_lib_malloc_pages(substream
,
81 params_buffer_bytes(hw_params
));
84 dev_warn(chip
->dev
, "pcm audio buffer allocation failure %i\n",
92 static int snd_usbtv_hw_free(struct snd_pcm_substream
*substream
)
94 snd_pcm_lib_free_pages(substream
);
98 static int snd_usbtv_prepare(struct snd_pcm_substream
*substream
)
100 struct usbtv
*chip
= snd_pcm_substream_chip(substream
);
102 chip
->snd_buffer_pos
= 0;
103 chip
->snd_period_pos
= 0;
108 static void usbtv_audio_urb_received(struct urb
*urb
)
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
;
117 switch (urb
->status
) {
127 dev_warn(chip
->dev
, "unknown audio urb status %i\n",
131 if (!atomic_read(&chip
->snd_stream
))
134 frame_bytes
= runtime
->frame_bits
>> 3;
135 chunk_length
= USBTV_CHUNK
/ frame_bytes
;
137 buffer_pos
= chip
->snd_buffer_pos
;
138 period_pos
= chip
->snd_period_pos
;
141 for (i
= 0; i
< urb
->actual_length
; i
+= USBTV_CHUNK_SIZE
) {
142 urb_current
= urb
->transfer_buffer
+ i
+ USBTV_AUDIO_HDRSIZE
;
144 if (buffer_pos
+ chunk_length
>= runtime
->buffer_size
) {
145 size_t cnt
= (runtime
->buffer_size
- buffer_pos
) *
147 memcpy(runtime
->dma_area
+ buffer_pos
* frame_bytes
,
149 memcpy(runtime
->dma_area
, urb_current
+ cnt
,
150 chunk_length
* frame_bytes
- cnt
);
152 memcpy(runtime
->dma_area
+ buffer_pos
* frame_bytes
,
153 urb_current
, chunk_length
* frame_bytes
);
156 buffer_pos
+= chunk_length
;
157 period_pos
+= chunk_length
;
159 if (buffer_pos
>= runtime
->buffer_size
)
160 buffer_pos
-= runtime
->buffer_size
;
162 if (period_pos
>= runtime
->period_size
) {
163 period_pos
-= runtime
->period_size
;
168 snd_pcm_stream_lock(substream
);
170 chip
->snd_buffer_pos
= buffer_pos
;
171 chip
->snd_period_pos
= period_pos
;
173 snd_pcm_stream_unlock(substream
);
176 snd_pcm_period_elapsed(substream
);
178 usb_submit_urb(urb
, GFP_ATOMIC
);
181 static int usbtv_audio_start(struct usbtv
*chip
)
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 },
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 },
205 /* this sets the input from composite */
206 { USBTV_BASE
+ 0x0284, 0x00aa },
209 chip
->snd_bulk_urb
= usb_alloc_urb(0, GFP_KERNEL
);
210 if (chip
->snd_bulk_urb
== NULL
)
213 pipe
= usb_rcvbulkpipe(chip
->udev
, USBTV_AUDIO_ENDP
);
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
;
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
);
224 /* starting the stream */
225 usbtv_set_regs(chip
, setup
, ARRAY_SIZE(setup
));
227 usb_clear_halt(chip
->udev
, pipe
);
228 usb_submit_urb(chip
->snd_bulk_urb
, GFP_ATOMIC
);
233 usb_free_urb(chip
->snd_bulk_urb
);
234 chip
->snd_bulk_urb
= NULL
;
240 static int usbtv_audio_stop(struct usbtv
*chip
)
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
248 { USBTV_BASE
+ 0x027d, 0x0000 },
249 { USBTV_BASE
+ 0x0280, 0x0010 },
250 { USBTV_BASE
+ 0x0282, 0x0010 },
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
;
260 usbtv_set_regs(chip
, setup
, ARRAY_SIZE(setup
));
265 void usbtv_audio_suspend(struct usbtv
*usbtv
)
267 if (atomic_read(&usbtv
->snd_stream
) && usbtv
->snd_bulk_urb
)
268 usb_kill_urb(usbtv
->snd_bulk_urb
);
271 void usbtv_audio_resume(struct usbtv
*usbtv
)
273 if (atomic_read(&usbtv
->snd_stream
) && usbtv
->snd_bulk_urb
)
274 usb_submit_urb(usbtv
->snd_bulk_urb
, GFP_ATOMIC
);
277 static void snd_usbtv_trigger(struct work_struct
*work
)
279 struct usbtv
*chip
= container_of(work
, struct usbtv
, snd_trigger
);
281 if (atomic_read(&chip
->snd_stream
))
282 usbtv_audio_start(chip
);
284 usbtv_audio_stop(chip
);
287 static int snd_usbtv_card_trigger(struct snd_pcm_substream
*substream
, int cmd
)
289 struct usbtv
*chip
= snd_pcm_substream_chip(substream
);
292 case SNDRV_PCM_TRIGGER_START
:
293 case SNDRV_PCM_TRIGGER_RESUME
:
294 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
295 atomic_set(&chip
->snd_stream
, 1);
297 case SNDRV_PCM_TRIGGER_STOP
:
298 case SNDRV_PCM_TRIGGER_SUSPEND
:
299 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
300 atomic_set(&chip
->snd_stream
, 0);
306 schedule_work(&chip
->snd_trigger
);
311 static snd_pcm_uframes_t
snd_usbtv_pointer(struct snd_pcm_substream
*substream
)
313 struct usbtv
*chip
= snd_pcm_substream_chip(substream
);
315 return chip
->snd_buffer_pos
;
318 static struct snd_pcm_ops snd_usbtv_pcm_ops
= {
319 .open
= snd_usbtv_pcm_open
,
320 .close
= snd_usbtv_pcm_close
,
321 .ioctl
= snd_pcm_lib_ioctl
,
322 .hw_params
= snd_usbtv_hw_params
,
323 .hw_free
= snd_usbtv_hw_free
,
324 .prepare
= snd_usbtv_prepare
,
325 .trigger
= snd_usbtv_card_trigger
,
326 .pointer
= snd_usbtv_pointer
,
329 int usbtv_audio_init(struct usbtv
*usbtv
)
332 struct snd_card
*card
;
335 INIT_WORK(&usbtv
->snd_trigger
, snd_usbtv_trigger
);
336 atomic_set(&usbtv
->snd_stream
, 0);
338 rv
= snd_card_new(&usbtv
->udev
->dev
, SNDRV_DEFAULT_IDX1
, "usbtv",
339 THIS_MODULE
, 0, &card
);
343 strlcpy(card
->driver
, usbtv
->dev
->driver
->name
, sizeof(card
->driver
));
344 strlcpy(card
->shortname
, "usbtv", sizeof(card
->shortname
));
345 snprintf(card
->longname
, sizeof(card
->longname
),
346 "USBTV Audio at bus %d device %d", usbtv
->udev
->bus
->busnum
,
347 usbtv
->udev
->devnum
);
349 snd_card_set_dev(card
, usbtv
->dev
);
353 rv
= snd_pcm_new(card
, "USBTV Audio", 0, 0, 1, &pcm
);
357 strlcpy(pcm
->name
, "USBTV Audio Input", sizeof(pcm
->name
));
359 pcm
->private_data
= usbtv
;
361 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_usbtv_pcm_ops
);
362 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_CONTINUOUS
,
363 snd_dma_continuous_data(GFP_KERNEL
), USBTV_AUDIO_BUFFER
,
366 rv
= snd_card_register(card
);
379 void usbtv_audio_free(struct usbtv
*usbtv
)
381 if (usbtv
->snd
&& usbtv
->udev
) {
382 snd_card_free(usbtv
->snd
);