-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmi2021_audio.c
401 lines (321 loc) · 9.89 KB
/
smi2021_audio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/************************************************************************
* smi2021_audio.c *
* *
* USB Driver for SMI2021 - EasyCap *
* **********************************************************************
*
* Copyright 2011-2013 Jon Arne Jørgensen
* <jonjon.arnearne--a.t--gmail.com>
*
* Copyright 2011, 2012 Tony Brown, Michal Demin, Jeffry Johnston
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* This driver is heavily influensed by the STK1160 driver.
* Copyright (C) 2012 Ezequiel Garcia
* <elezegarcia--a.t--gmail.com>
*
*/
#include "smi2021.h"
static void pcm_buffer_free(struct snd_pcm_substream *substream)
{
vfree(substream->runtime->dma_area);
substream->runtime->dma_area = NULL;
substream->runtime->dma_bytes = 0;
}
static int pcm_buffer_alloc(struct snd_pcm_substream *substream, int size)
{
if (substream->runtime->dma_area) {
if (substream->runtime->dma_bytes > size)
return 0;
pcm_buffer_free(substream);
}
substream->runtime->dma_area = vmalloc(size);
if (substream->runtime->dma_area == NULL)
return -ENOMEM;
substream->runtime->dma_bytes = size;
return 0;
}
static const struct snd_pcm_hardware smi2021_pcm_hw = {
.info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
SNDRV_PCM_INFO_INTERLEAVED |
SNDRV_PCM_INFO_MMAP |
SNDRV_PCM_INFO_MMAP_VALID |
SNDRV_PCM_INFO_BATCH,
.formats = SNDRV_PCM_FMTBIT_S32_LE,
.rates = SNDRV_PCM_RATE_48000,
.rate_min = 48000,
.rate_max = 48000,
.channels_min = 2,
.channels_max = 2,
.period_bytes_min = 992, /* 32640 */ /* 15296 */
.period_bytes_max = 15872, /* 65280 */
.periods_min = 1, /* 1 */
.periods_max = 16, /* 2 */
.buffer_bytes_max = 65280, /* 65280 */
};
static int smi2021_pcm_open(struct snd_pcm_substream *substream)
{
struct smi2021 *smi2021 = snd_pcm_substream_chip(substream);
struct snd_pcm_runtime *runtime = substream->runtime;
int rc;
rc = snd_pcm_hw_constraint_pow2(runtime, 0,
SNDRV_PCM_HW_PARAM_PERIODS);
if (rc < 0)
return rc;
smi2021->pcm_substream = substream;
runtime->hw = smi2021_pcm_hw;
snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
return 0;
}
static int smi2021_pcm_close(struct snd_pcm_substream *substream)
{
struct smi2021 *smi2021 = snd_pcm_substream_chip(substream);
if (atomic_read(&smi2021->adev_capturing)) {
atomic_set(&smi2021->adev_capturing, 0);
schedule_work(&smi2021->adev_capture_trigger);
}
return 0;
}
static int smi2021_pcm_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *hw_params)
{
int size, rc;
size = params_period_bytes(hw_params) * params_periods(hw_params);
rc = pcm_buffer_alloc(substream, size);
if (rc < 0)
return rc;
return 0;
}
static int smi2021_pcm_hw_free(struct snd_pcm_substream *substream)
{
struct smi2021 *smi2021 = snd_pcm_substream_chip(substream);
if (atomic_read(&smi2021->adev_capturing)) {
atomic_set(&smi2021->adev_capturing, 0);
schedule_work(&smi2021->adev_capture_trigger);
}
pcm_buffer_free(substream);
return 0;
}
static int smi2021_pcm_prepare(struct snd_pcm_substream *substream)
{
struct smi2021 *smi2021 = snd_pcm_substream_chip(substream);
smi2021->pcm_complete_samples = 0;
smi2021->pcm_read_offset = 0;
smi2021->pcm_write_ptr = 0;
return 0;
}
static void capture_trigger(struct work_struct *work)
{
struct smi2021 *smi2021 = container_of(work, struct smi2021,
adev_capture_trigger);
if (atomic_read(&smi2021->adev_capturing))
smi2021_toggle_audio(smi2021, true);
else
smi2021_toggle_audio(smi2021, false);
}
/* This callback is ATOMIC, must not sleep */
static int smi2021_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
{
struct smi2021 *smi2021 = snd_pcm_substream_chip(substream);
switch (cmd) {
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
case SNDRV_PCM_TRIGGER_RESUME:
case SNDRV_PCM_TRIGGER_START:
atomic_set(&smi2021->adev_capturing, 1);
break;
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
case SNDRV_PCM_TRIGGER_SUSPEND:
case SNDRV_PCM_TRIGGER_STOP:
atomic_set(&smi2021->adev_capturing, 0);
break;
default:
return -EINVAL;
}
schedule_work(&smi2021->adev_capture_trigger);
return 0;
}
static snd_pcm_uframes_t smi2021_pcm_pointer(
struct snd_pcm_substream *substream)
{
struct smi2021 *smi2021 = snd_pcm_substream_chip(substream);
return smi2021->pcm_write_ptr / 8;
}
static struct page *smi2021_pcm_get_vmalloc_page(
struct snd_pcm_substream *subs,
unsigned long offset)
{
void *pageptr = subs->runtime->dma_area + offset;
return vmalloc_to_page(pageptr);
}
static struct snd_pcm_ops smi2021_pcm_ops = {
.open = smi2021_pcm_open,
.close = smi2021_pcm_close,
.ioctl = snd_pcm_lib_ioctl,
.hw_params = smi2021_pcm_hw_params,
.hw_free = smi2021_pcm_hw_free,
.prepare = smi2021_pcm_prepare,
.trigger = smi2021_pcm_trigger,
.pointer = smi2021_pcm_pointer,
.page = smi2021_pcm_get_vmalloc_page,
};
int smi2021_snd_register(struct smi2021 *smi2021)
{
struct snd_card *card;
struct snd_pcm *pcm;
int rc = 0;
rc = snd_card_new(smi2021->v4l2_dev.dev, SNDRV_DEFAULT_IDX1,
SNDRV_DEFAULT_STR1, THIS_MODULE, 0, &card);
if (rc < 0)
return rc;
rc = snd_pcm_new(card, "smi2021 Audio", 0, 0, 1, &pcm);
if (rc < 0)
goto err_free_card;
snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &smi2021_pcm_ops);
pcm->info_flags = 0;
pcm->private_data = smi2021;
strcpy(pcm->name, "Somagic smi2021 Capture");
strcpy(card->driver, "smi2021-Audio");
strcpy(card->shortname, "smi2021 Audio");
strcpy(card->longname, "Somagic smi2021 Audio");
INIT_WORK(&smi2021->adev_capture_trigger, capture_trigger);
rc = snd_card_register(card);
if (rc < 0)
goto err_free_card;
smi2021->snd_card = card;
return 0;
err_free_card:
snd_card_free(card);
return rc;
}
void smi2021_snd_unregister(struct smi2021 *smi2021)
{
if (smi2021 == NULL)
return;
if (smi2021->snd_card == NULL)
return;
snd_card_free(smi2021->snd_card);
smi2021->snd_card = NULL;
}
void smi2021_stop_audio(struct smi2021 *smi2021)
{
/*
* HACK: Stop the audio subsystem,
* without this, the pcm middle-layer will hang waiting for more data.
*
* Is there a better way to do this?
*/
if (smi2021->pcm_substream && smi2021->pcm_substream->runtime) {
struct snd_pcm_runtime *runtime;
runtime = smi2021->pcm_substream->runtime;
if (runtime->status) {
runtime->status->state = SNDRV_PCM_STATE_DRAINING;
wake_up(&runtime->sleep);
}
}
}
void smi2021_audio(struct smi2021 *smi2021, u8 *data, int len)
{
struct snd_pcm_runtime *runtime;
u8 offset;
int new_offset = 0;
int skip;
unsigned int stride, oldptr, headptr;
int diff = 0;
int samples = 0;
bool period_elapsed = false;
if (smi2021->udev == NULL)
return;
if (atomic_read(&smi2021->adev_capturing) == 0)
return;
if (smi2021->pcm_substream == NULL)
return;
runtime = smi2021->pcm_substream->runtime;
if (!runtime || !runtime->dma_area)
return;
offset = smi2021->pcm_read_offset;
stride = runtime->frame_bits >> 3;
if (stride == 0)
return;
diff = smi2021->pcm_write_ptr;
/*
* Check that the end of the last buffer was correct.
* If not correct, we mark any partial frames in buffer as complete
*/
headptr = smi2021->pcm_write_ptr - offset - 4;
if (smi2021->pcm_write_ptr > 10
&& runtime->dma_area[headptr] != 0x00) {
skip = stride - (smi2021->pcm_write_ptr % stride);
snd_pcm_stream_lock(smi2021->pcm_substream);
smi2021->pcm_write_ptr += skip;
if (smi2021->pcm_write_ptr >= runtime->dma_bytes)
smi2021->pcm_write_ptr -= runtime->dma_bytes;
snd_pcm_stream_unlock(smi2021->pcm_substream);
offset = smi2021->pcm_read_offset = 0;
}
/*
* The device is actually sending 24Bit pcm data
* with 0x00 as the header byte before each sample.
* We look for this byte to make sure we did not
* loose any bytes during transfer.
*/
while (len > stride && (data[offset] != 0x00 ||
data[offset + (stride / 2)] != 0x00)) {
new_offset++;
data++;
len--;
}
if (len <= stride) {
/* We exhausted the buffer looking for 0x00 */
smi2021->pcm_read_offset = 0;
return;
}
if (new_offset != 0) {
/*
* This buffer can not be appended to the current buffer,
* so we mark any partial frames in the buffer as complete.
*/
skip = stride - (smi2021->pcm_write_ptr % stride);
snd_pcm_stream_lock(smi2021->pcm_substream);
smi2021->pcm_write_ptr += skip;
if (smi2021->pcm_write_ptr >= runtime->dma_bytes)
smi2021->pcm_write_ptr -= runtime->dma_bytes;
snd_pcm_stream_unlock(smi2021->pcm_substream);
offset = smi2021->pcm_read_offset = new_offset % (stride / 2);
}
oldptr = smi2021->pcm_write_ptr;
if (oldptr + len >= runtime->dma_bytes) {
unsigned int cnt = runtime->dma_bytes - oldptr;
memcpy(runtime->dma_area + oldptr, data, cnt);
memcpy(runtime->dma_area, data + cnt, len - cnt);
} else {
memcpy(runtime->dma_area + oldptr, data, len);
}
snd_pcm_stream_lock(smi2021->pcm_substream);
smi2021->pcm_write_ptr += len;
if (smi2021->pcm_write_ptr >= runtime->dma_bytes)
smi2021->pcm_write_ptr -= runtime->dma_bytes;
samples = smi2021->pcm_write_ptr - diff;
if (samples < 0)
samples += runtime->dma_bytes;
samples /= (stride / 2);
smi2021->pcm_complete_samples += samples;
if (smi2021->pcm_complete_samples / 2 >= runtime->period_size) {
smi2021->pcm_complete_samples -= runtime->period_size * 2;
period_elapsed = true;
}
snd_pcm_stream_unlock(smi2021->pcm_substream);
if (period_elapsed)
snd_pcm_period_elapsed(smi2021->pcm_substream);
}