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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
|
/*
* core.c - compress offload core
*
* Copyright (C) 2011 Intel Corporation
* Authors: Vinod Koul <vinod.koul@linux.intel.com>
* Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* 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; version 2 of the License.
*
* 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, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
*/
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/list.h>
#include <linux/miscdevice.h>
#include <linux/mm.h>
#include <linux/mutex.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/uio.h>
#include <linux/uaccess.h>
#include <sound/snd_compress_params.h>
#include <sound/compress_offload.h>
#include <sound/compress_driver.h>
/* TODO:
* - Integrate with alsa, compressed devices should register as alsa devices
* as /dev/snd_compr_xxx
* - Integrate with ASoC:
* Opening compressed path should also start the codec dai
* TBD how the cpu dai will be viewed and started.
* ASoC should always be optional part
* (we should be able to use this framework in non asoc systems
* - Multiple node representation
* driver should be able to register multiple nodes
* - Version numbering for API
*/
static DEFINE_MUTEX(device_mutex);
static LIST_HEAD(device_list);
static LIST_HEAD(misc_list);
/*
* currently we are using misc device for registration and exposing ioctls
* this is temporary and will be moved to snd
* the device should be registered as /dev/snd_compr.....
*/
struct snd_compr_misc {
struct miscdevice misc;
struct list_head list;
struct snd_compr *compr;
};
struct snd_ioctl_data {
struct snd_compr_misc *misc;
unsigned long caps;
unsigned int minor;
struct snd_compr_stream stream;
};
static struct snd_compr_misc *snd_compr_get_device(unsigned int minor)
{
struct snd_compr_misc *misc;
list_for_each_entry(misc, &misc_list, list) {
if (minor == misc->misc.minor)
return misc;
}
return NULL;
}
static int snd_compr_open(struct inode *inode, struct file *f)
{
unsigned int minor = iminor(inode);
struct snd_compr_misc *misc = snd_compr_get_device(minor);
struct snd_ioctl_data *data;
struct snd_compr_runtime *runtime;
unsigned int direction;
int ret;
mutex_lock(&device_mutex);
if (f->f_flags & O_WRONLY)
direction = SNDRV_PCM_STREAM_PLAYBACK;
else {
ret = -ENXIO;
goto out;
}
/* curently only encoded playback is supported, above needs to be
* removed once we have recording support */
data = kzalloc(sizeof(*data), GFP_KERNEL);
if (!data) {
ret = -ENOMEM;
goto out;
}
data->misc = misc;
data->minor = minor;
data->stream.ops = misc->compr->ops;
data->stream.direction = direction;
data->stream.private_data = misc->compr->private_data;
data->stream.device = misc->compr;
runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
if (!runtime) {
ret = -ENOMEM;
kfree(data);
goto out;
}
runtime->state = SNDRV_PCM_STATE_OPEN;
init_waitqueue_head(&runtime->sleep);
data->stream.runtime = runtime;
f->private_data = (void *)data;
ret = misc->compr->ops->open(&data->stream);
if (ret) {
kfree(runtime);
kfree(data);
goto out;
}
out:
mutex_unlock(&device_mutex);
return ret;
}
static int snd_compr_free(struct inode *inode, struct file *f)
{
struct snd_ioctl_data *data = f->private_data;
mutex_lock(&device_mutex);
data->stream.ops->free(&data->stream);
kfree(data->stream.runtime->buffer);
kfree(data->stream.runtime);
kfree(data);
mutex_unlock(&device_mutex);
return 0;
}
static void snd_compr_update_tstamp(struct snd_compr_stream *stream,
struct snd_compr_tstamp *tstamp)
{
stream->ops->pointer(stream, tstamp);
stream->runtime->hw_pointer = tstamp->copied_bytes;
}
static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
struct snd_compr_avail *avail)
{
size_t avail_calc;
snd_compr_update_tstamp(stream, &avail->tstamp);
avail_calc = stream->runtime->app_pointer - stream->runtime->hw_pointer;
if (avail_calc < 0)
avail_calc = stream->runtime->buffer_size + avail_calc;
avail->avail = avail_calc;
return avail_calc;
}
static size_t snd_compr_get_avail(struct snd_compr_stream *stream)
{
struct snd_compr_avail avail;
return snd_compr_calc_avail(stream, &avail);
}
static int
snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
{
struct snd_compr_avail ioctl_avail;
snd_compr_calc_avail(stream, &ioctl_avail);
if (copy_to_user((unsigned long __user *)arg, &ioctl_avail, sizeof(ioctl_avail)))
return -EFAULT;
return 0;
}
static int snd_compr_write_data(struct snd_compr_stream *stream,
const char __user *buf, size_t count)
{
void *dstn;
size_t copy;
dstn = stream->runtime->buffer + stream->runtime->app_pointer;
if (count < stream->runtime->buffer_size - stream->runtime->app_pointer) {
if (copy_from_user(dstn, buf, count))
return -EFAULT;
stream->runtime->app_pointer += count;
} else {
copy = stream->runtime->buffer_size - stream->runtime->app_pointer;
if (copy_from_user(dstn, buf, copy))
return -EFAULT;
if (copy_from_user(stream->runtime->buffer, buf + copy, count - copy))
return -EFAULT;
stream->runtime->app_pointer = count - copy;
}
/* if DSP cares, let it know data has been written */
if (stream->ops->ack)
stream->ops->ack(stream);
return count;
}
static ssize_t snd_compr_write(struct file *f, const char __user *buf,
size_t count, loff_t *offset)
{
struct snd_ioctl_data *data = f->private_data;
struct snd_compr_stream *stream;
size_t avail;
int retval;
BUG_ON(!data);
stream = &data->stream;
mutex_lock(&stream->device->lock);
/* write is allowed when stream is running or has been steup */
if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
mutex_unlock(&stream->device->lock);
return -EPERM;
}
avail = snd_compr_get_avail(stream);
/* calculate how much we can write to buffer */
if (avail > count)
avail = count;
if (stream->ops->copy)
retval = stream->ops->copy(stream, buf, avail);
else
retval = snd_compr_write_data(stream, buf, avail);
/* while initiating the stream, write should be called before START
* call, so in setup move state */
if (stream->runtime->state == SNDRV_PCM_STATE_SETUP)
stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
mutex_unlock(&stream->device->lock);
return retval;
}
static ssize_t snd_compr_read(struct file *f, char __user *buf,
size_t count, loff_t *offset)
{
return -ENXIO;
}
static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
{
return -ENXIO;
}
unsigned int snd_compr_poll(struct file *f, poll_table *wait)
{
struct snd_ioctl_data *data = f->private_data;
struct snd_compr_stream *stream;
int retval = 0;
BUG_ON(!data);
stream = &data->stream;
mutex_lock(&stream->device->lock);
if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
retval = -ENXIO;
goto out;
}
poll_wait(f, &stream->runtime->sleep, wait);
/* this would change after read is implemented, we would need to
* check for direction here */
if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
retval = POLLOUT | POLLWRNORM;
out:
mutex_unlock(&stream->device->lock);
return retval;
}
void snd_compr_fragment_elapsed(struct snd_compr_stream *stream)
{
size_t avail;
if (stream->direction != SNDRV_PCM_STREAM_PLAYBACK)
return;
avail = snd_compr_get_avail(stream);
if (avail >= stream->runtime->fragment_size)
wake_up(&stream->runtime->sleep);
}
EXPORT_SYMBOL_GPL(snd_compr_fragment_elapsed);
void snd_compr_frame_elapsed(struct snd_compr_stream *stream)
{
size_t avail;
if (stream->direction != SNDRV_PCM_STREAM_CAPTURE)
return;
avail = snd_compr_get_avail(stream);
if (avail)
wake_up(&stream->runtime->sleep);
}
EXPORT_SYMBOL_GPL(snd_compr_frame_elapsed);
static int snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
{
int retval;
struct snd_compr_caps caps;
if (!stream->ops->get_caps)
return -ENXIO;
retval = stream->ops->get_caps(stream, &caps);
if (retval)
goto out;
if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
retval = -EFAULT;
out:
return retval;
}
static int snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
{
int retval;
struct snd_compr_codec_caps *caps;
if (!stream->ops->get_codec_caps)
return -ENXIO;
caps = kmalloc(sizeof(*caps), GFP_KERNEL);
if (!caps)
return -ENOMEM;
retval = stream->ops->get_codec_caps(stream, caps);
if (retval)
goto out;
if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
retval = -EFAULT;
out:
kfree(caps);
return retval;
}
/* revisit this with snd_pcm_preallocate_xxx */
static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
struct snd_compr_params *params)
{
unsigned int buffer_size;
void *buffer;
buffer_size = params->buffer.fragment_size * params->buffer.fragments;
if (stream->ops->copy) {
buffer = NULL;
/* if copy is defined the driver will be required to copy
* the data from core
*/
} else {
buffer = kmalloc(buffer_size, GFP_KERNEL);
if (!buffer)
return -ENOMEM;
}
stream->runtime->fragment_size = params->buffer.fragment_size;
stream->runtime->fragments = params->buffer.fragments;
stream->runtime->buffer = buffer;
stream->runtime->buffer_size = buffer_size;
return 0;
}
static int snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
{
struct snd_compr_params *params;
int retval;
if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
/*
* we should allow parameter change only when stream has been
* opened not in other cases
*/
params = kmalloc(sizeof(*params), GFP_KERNEL);
if (!params)
return -ENOMEM;
if (copy_from_user(params, (void __user *)arg, sizeof(*params)))
return -EFAULT;
retval = snd_compr_allocate_buffer(stream, params);
if (retval) {
kfree(params);
return -ENOMEM;
}
retval = stream->ops->set_params(stream, params);
if (retval)
goto out;
stream->runtime->state = SNDRV_PCM_STATE_SETUP;
} else
return -EPERM;
out:
kfree(params);
return retval;
}
static int snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
{
struct snd_compr_params *params;
int retval;
if (!stream->ops->get_params)
return -ENXIO;
params = kmalloc(sizeof(*params), GFP_KERNEL);
if (!params)
return -ENOMEM;
retval = stream->ops->get_params(stream, params);
if (retval)
goto out;
if (copy_to_user((char __user *)arg, params, sizeof(*params)))
retval = -EFAULT;
out:
kfree(params);
return retval;
}
static int snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
{
struct snd_compr_tstamp tstamp;
snd_compr_update_tstamp(stream, &tstamp);
if (copy_to_user((struct snd_compr_tstamp __user *)arg, &tstamp, sizeof(tstamp)))
return -EFAULT;
return 0;
}
static int snd_compr_pause(struct snd_compr_stream *stream)
{
int retval;
if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED)
return 0;
retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
if (!retval) {
stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
wake_up(&stream->runtime->sleep);
}
return retval;
}
static int snd_compr_resume(struct snd_compr_stream *stream)
{
int retval;
if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
return -EPERM;
retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
if (!retval)
stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
return retval;
}
static int snd_compr_start(struct snd_compr_stream *stream)
{
int retval;
if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
return -EPERM;
retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
if (!retval)
stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
return retval;
}
static int snd_compr_stop(struct snd_compr_stream *stream)
{
int retval;
if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
return -EPERM;
retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
if (!retval) {
stream->runtime->state = SNDRV_PCM_STATE_SETUP;
wake_up(&stream->runtime->sleep);
}
return retval;
}
static int snd_compr_drain(struct snd_compr_stream *stream)
{
int retval;
if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED ||
stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
return -EPERM;
retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
if (!retval) {
stream->runtime->state = SNDRV_PCM_STATE_SETUP;
wake_up(&stream->runtime->sleep);
}
return retval;
}
static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{
struct snd_ioctl_data *data = f->private_data;
struct snd_compr_stream *stream;
int retval = -ENOTTY;
BUG_ON(!data);
stream = &data->stream;
mutex_lock(&stream->device->lock);
switch (_IOC_NR(cmd)) {
case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
retval = snd_compr_get_caps(stream, arg);
break;
case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
retval = snd_compr_get_codec_caps(stream, arg);
break;
case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
retval = snd_compr_set_params(stream, arg);
break;
case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
retval = snd_compr_get_params(stream, arg);
break;
case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
retval = snd_compr_tstamp(stream, arg);
break;
case _IOC_NR(SNDRV_COMPRESS_AVAIL):
retval = snd_compr_ioctl_avail(stream, arg);
case _IOC_NR(SNDRV_COMPRESS_PAUSE):
retval = snd_compr_pause(stream);
break;
case _IOC_NR(SNDRV_COMPRESS_RESUME):
retval = snd_compr_resume(stream);
break;
case _IOC_NR(SNDRV_COMPRESS_START):
retval = snd_compr_start(stream);
break;
case _IOC_NR(SNDRV_COMPRESS_STOP):
retval = snd_compr_stop(stream);
break;
case _IOC_NR(SNDRV_COMPRESS_DRAIN):
cmd = SND_COMPR_TRIGGER_DRAIN;
retval = snd_compr_drain(stream);
break;
}
mutex_unlock(&stream->device->lock);
return retval;
}
static const struct file_operations snd_comp_file = {
.owner = THIS_MODULE,
.open = snd_compr_open,
.release = snd_compr_free,
.read = snd_compr_read,
.write = snd_compr_write,
.unlocked_ioctl = snd_compr_ioctl,
.mmap = snd_compr_mmap,
.poll = snd_compr_poll,
};
static int snd_compress_add_device(struct snd_compr *device)
{
int ret;
struct snd_compr_misc *misc = kzalloc(sizeof(*misc), GFP_KERNEL);
misc->misc.name = device->name;
misc->misc.fops = &snd_comp_file;
misc->misc.minor = MISC_DYNAMIC_MINOR;
misc->compr = device;
ret = misc_register(&misc->misc);
if (ret) {
pr_err("couldn't register misc device\n");
kfree(misc);
} else {
pr_debug("Got minor %d\n", misc->misc.minor);
list_add_tail(&misc->list, &misc_list);
}
return ret;
}
static int snd_compress_remove_device(struct snd_compr *device)
{
struct snd_compr_misc *misc, *__misc;
list_for_each_entry_safe(misc, __misc, &misc_list, list) {
if (device == misc->compr) {
misc_deregister(&misc->misc);
list_del(&device->list);
kfree(misc);
}
}
return 0;
}
/**
* snd_compress_register - register compressed device
*
* @device: compressed device to register
*/
int snd_compress_register(struct snd_compr *device)
{
int retval;
if (device->name == NULL || device->dev == NULL || device->ops == NULL)
return -EINVAL;
BUG_ON(!device->ops->open);
BUG_ON(!device->ops->free);
BUG_ON(!device->ops->set_params);
BUG_ON(!device->ops->get_params);
BUG_ON(!device->ops->trigger);
BUG_ON(!device->ops->pointer);
BUG_ON(!device->ops->get_caps);
BUG_ON(!device->ops->get_codec_caps);
INIT_LIST_HEAD(&device->list);
/* todo register the compressed streams */
/* todo integrate with asoc */
/* register a compressed card TBD if this needs change */
pr_debug("Registering compressed device %s\n", device->name);
mutex_lock(&device_mutex);
/* register a msic device for now */
retval = snd_compress_add_device(device);
if (!retval)
list_add_tail(&device->list, &device_list);
mutex_unlock(&device_mutex);
return retval;
}
EXPORT_SYMBOL_GPL(snd_compress_register);
int snd_compress_deregister(struct snd_compr *device)
{
pr_debug("Removing compressed device %s\n", device->name);
mutex_lock(&device_mutex);
snd_compress_remove_device(device);
list_del(&device->list);
mutex_unlock(&device_mutex);
return 0;
}
EXPORT_SYMBOL_GPL(snd_compress_deregister);
static int __init snd_compress_init(void)
{
return 0;
}
static void __exit snd_compress_exit(void)
{
}
module_init(snd_compress_init);
module_exit(snd_compress_exit);
|