hwrng: core - Don't use a stack buffer in add_early_randomness()
[linux/fpc-iii.git] / drivers / media / usb / usbtv / usbtv-audio.c
blob9db31db7d9ac20e7bf6377703033f908d080da50
1 /*
2 * Copyright (c) 2013 Federico Simoncelli
3 * All rights reserved.
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.
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
32 * Product web site:
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>
44 #include "usbtv.h"
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 */
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;
73 return 0;
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);
85 return 0;
88 static int snd_usbtv_hw_params(struct snd_pcm_substream *substream,
89 struct snd_pcm_hw_params *hw_params)
91 int rv;
92 struct usbtv *chip = snd_pcm_substream_chip(substream);
94 rv = snd_pcm_lib_malloc_pages(substream,
95 params_buffer_bytes(hw_params));
97 if (rv < 0) {
98 dev_warn(chip->dev, "pcm audio buffer allocation failure %i\n",
99 rv);
100 return rv;
103 return 0;
106 static int snd_usbtv_hw_free(struct snd_pcm_substream *substream)
108 snd_pcm_lib_free_pages(substream);
109 return 0;
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;
119 return 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;
128 int period_elapsed;
129 void *urb_current;
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);
145 if (!atomic_read(&chip->snd_stream))
146 return;
148 frame_bytes = runtime->frame_bits >> 3;
149 chunk_length = USBTV_CHUNK / frame_bytes;
151 buffer_pos = chip->snd_buffer_pos;
152 period_pos = chip->snd_period_pos;
153 period_elapsed = 0;
155 for (i = 0; i < urb->actual_length; i += USBTV_CHUNK_SIZE) {
156 urb_current = urb->transfer_buffer + i + USBTV_AUDIO_HDRSIZE;
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);
170 buffer_pos += chunk_length;
171 period_pos += chunk_length;
173 if (buffer_pos >= runtime->buffer_size)
174 buffer_pos -= runtime->buffer_size;
176 if (period_pos >= runtime->period_size) {
177 period_pos -= runtime->period_size;
178 period_elapsed = 1;
182 snd_pcm_stream_lock(substream);
184 chip->snd_buffer_pos = buffer_pos;
185 chip->snd_period_pos = period_pos;
187 snd_pcm_stream_unlock(substream);
189 if (period_elapsed)
190 snd_pcm_period_elapsed(substream);
192 usb_submit_urb(urb, GFP_ATOMIC);
195 static int usbtv_audio_start(struct usbtv *chip)
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 },
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 },
219 /* this sets the input from composite */
220 { USBTV_BASE + 0x0284, 0x00aa },
223 chip->snd_bulk_urb = usb_alloc_urb(0, GFP_KERNEL);
224 if (chip->snd_bulk_urb == NULL)
225 goto err_alloc_urb;
227 pipe = usb_rcvbulkpipe(chip->udev, USBTV_AUDIO_ENDP);
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;
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);
238 /* starting the stream */
239 usbtv_set_regs(chip, setup, ARRAY_SIZE(setup));
241 usb_clear_halt(chip->udev, pipe);
242 usb_submit_urb(chip->snd_bulk_urb, GFP_ATOMIC);
244 return 0;
246 err_transfer_buffer:
247 usb_free_urb(chip->snd_bulk_urb);
248 chip->snd_bulk_urb = NULL;
250 err_alloc_urb:
251 return -ENOMEM;
254 static int usbtv_audio_stop(struct usbtv *chip)
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.
262 { USBTV_BASE + 0x027d, 0x0000 },
263 { USBTV_BASE + 0x0280, 0x0010 },
264 { USBTV_BASE + 0x0282, 0x0010 },
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;
274 usbtv_set_regs(chip, setup, ARRAY_SIZE(setup));
276 return 0;
279 void usbtv_audio_suspend(struct usbtv *usbtv)
281 if (atomic_read(&usbtv->snd_stream) && usbtv->snd_bulk_urb)
282 usb_kill_urb(usbtv->snd_bulk_urb);
285 void usbtv_audio_resume(struct usbtv *usbtv)
287 if (atomic_read(&usbtv->snd_stream) && usbtv->snd_bulk_urb)
288 usb_submit_urb(usbtv->snd_bulk_urb, GFP_ATOMIC);
291 static void snd_usbtv_trigger(struct work_struct *work)
293 struct usbtv *chip = container_of(work, struct usbtv, snd_trigger);
295 if (!chip->snd)
296 return;
298 if (atomic_read(&chip->snd_stream))
299 usbtv_audio_start(chip);
300 else
301 usbtv_audio_stop(chip);
304 static int snd_usbtv_card_trigger(struct snd_pcm_substream *substream, int cmd)
306 struct usbtv *chip = snd_pcm_substream_chip(substream);
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;
323 schedule_work(&chip->snd_trigger);
325 return 0;
328 static snd_pcm_uframes_t snd_usbtv_pointer(struct snd_pcm_substream *substream)
330 struct usbtv *chip = snd_pcm_substream_chip(substream);
332 return chip->snd_buffer_pos;
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,
346 int usbtv_audio_init(struct usbtv *usbtv)
348 int rv;
349 struct snd_card *card;
350 struct snd_pcm *pcm;
352 INIT_WORK(&usbtv->snd_trigger, snd_usbtv_trigger);
353 atomic_set(&usbtv->snd_stream, 0);
355 rv = snd_card_new(&usbtv->udev->dev, SNDRV_DEFAULT_IDX1, "usbtv",
356 THIS_MODULE, 0, &card);
357 if (rv < 0)
358 return rv;
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);
366 snd_card_set_dev(card, usbtv->dev);
368 usbtv->snd = card;
370 rv = snd_pcm_new(card, "USBTV Audio", 0, 0, 1, &pcm);
371 if (rv < 0)
372 goto err;
374 strlcpy(pcm->name, "USBTV Audio Input", sizeof(pcm->name));
375 pcm->info_flags = 0;
376 pcm->private_data = usbtv;
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);
383 rv = snd_card_register(card);
384 if (rv)
385 goto err;
387 return 0;
389 err:
390 usbtv->snd = NULL;
391 snd_card_free(card);
393 return rv;
396 void usbtv_audio_free(struct usbtv *usbtv)
398 cancel_work_sync(&usbtv->snd_trigger);
400 if (usbtv->snd && usbtv->udev) {
401 snd_card_free(usbtv->snd);
402 usbtv->snd = NULL;