Blame view
sound/core/compress_offload.c
25.7 KB
b21c60a4e ALSA: core: add s... |
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 |
/* * compress_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. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * */ #define FORMAT(fmt) "%s: %d: " fmt, __func__, __LINE__ #define pr_fmt(fmt) KBUILD_MODNAME ": " FORMAT(fmt) #include <linux/file.h> #include <linux/fs.h> #include <linux/list.h> |
f0283b58d ALSA: compress_co... |
31 |
#include <linux/math64.h> |
b21c60a4e ALSA: core: add s... |
32 33 34 35 36 |
#include <linux/mm.h> #include <linux/mutex.h> #include <linux/poll.h> #include <linux/slab.h> #include <linux/sched.h> |
f0283b58d ALSA: compress_co... |
37 |
#include <linux/types.h> |
b21c60a4e ALSA: core: add s... |
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 |
#include <linux/uio.h> #include <linux/uaccess.h> #include <linux/module.h> #include <sound/core.h> #include <sound/initval.h> #include <sound/compress_params.h> #include <sound/compress_offload.h> #include <sound/compress_driver.h> /* TODO: * - add substream support for multiple devices in case of * SND_DYNAMIC_MINORS is not used * - Multiple node representation * driver should be able to register multiple nodes */ static DEFINE_MUTEX(device_mutex); struct snd_compr_file { unsigned long caps; struct snd_compr_stream stream; }; /* * a note on stream states used: * we use follwing states in the compressed core * SNDRV_PCM_STATE_OPEN: When stream has been opened. * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by * calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain. * SNDRV_PCM_STATE_RUNNING: When stream has been started and is * decoding/encoding and rendering/capturing data. * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done * by calling SNDRV_COMPRESS_DRAIN. * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively. */ static int snd_compr_open(struct inode *inode, struct file *f) { struct snd_compr *compr; struct snd_compr_file *data; struct snd_compr_runtime *runtime; enum snd_compr_direction dirn; int maj = imajor(inode); int ret; |
81cb32467 ALSA: compress_co... |
84 |
if ((f->f_flags & O_ACCMODE) == O_WRONLY) |
b21c60a4e ALSA: core: add s... |
85 |
dirn = SND_COMPRESS_PLAYBACK; |
81cb32467 ALSA: compress_co... |
86 |
else if ((f->f_flags & O_ACCMODE) == O_RDONLY) |
b21c60a4e ALSA: core: add s... |
87 |
dirn = SND_COMPRESS_CAPTURE; |
81cb32467 ALSA: compress_co... |
88 |
else |
b21c60a4e ALSA: core: add s... |
89 |
return -EINVAL; |
b21c60a4e ALSA: core: add s... |
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
if (maj == snd_major) compr = snd_lookup_minor_data(iminor(inode), SNDRV_DEVICE_TYPE_COMPRESS); else return -EBADFD; if (compr == NULL) { pr_err("no device data!!! "); return -ENODEV; } if (dirn != compr->direction) { pr_err("this device doesn't support this direction "); |
a0830dbd4 ALSA: Add a refer... |
106 |
snd_card_unref(compr->card); |
b21c60a4e ALSA: core: add s... |
107 108 109 110 |
return -EINVAL; } data = kzalloc(sizeof(*data), GFP_KERNEL); |
a0830dbd4 ALSA: Add a refer... |
111 112 |
if (!data) { snd_card_unref(compr->card); |
b21c60a4e ALSA: core: add s... |
113 |
return -ENOMEM; |
a0830dbd4 ALSA: Add a refer... |
114 |
} |
b21c60a4e ALSA: core: add s... |
115 116 117 118 119 120 121 |
data->stream.ops = compr->ops; data->stream.direction = dirn; data->stream.private_data = compr->private_data; data->stream.device = compr; runtime = kzalloc(sizeof(*runtime), GFP_KERNEL); if (!runtime) { kfree(data); |
a0830dbd4 ALSA: Add a refer... |
122 |
snd_card_unref(compr->card); |
b21c60a4e ALSA: core: add s... |
123 124 125 126 127 128 129 130 131 132 133 134 135 |
return -ENOMEM; } runtime->state = SNDRV_PCM_STATE_OPEN; init_waitqueue_head(&runtime->sleep); data->stream.runtime = runtime; f->private_data = (void *)data; mutex_lock(&compr->lock); ret = compr->ops->open(&data->stream); mutex_unlock(&compr->lock); if (ret) { kfree(runtime); kfree(data); } |
a0830dbd4 ALSA: Add a refer... |
136 |
snd_card_unref(compr->card); |
749d32237 ALSA: compress: P... |
137 |
return ret; |
b21c60a4e ALSA: core: add s... |
138 139 140 141 142 |
} static int snd_compr_free(struct inode *inode, struct file *f) { struct snd_compr_file *data = f->private_data; |
b26d19e44 ALSA: compress: M... |
143 144 145 146 147 148 149 150 151 152 153 |
struct snd_compr_runtime *runtime = data->stream.runtime; switch (runtime->state) { case SNDRV_PCM_STATE_RUNNING: case SNDRV_PCM_STATE_DRAINING: case SNDRV_PCM_STATE_PAUSED: data->stream.ops->trigger(&data->stream, SNDRV_PCM_TRIGGER_STOP); break; default: break; } |
b21c60a4e ALSA: core: add s... |
154 155 156 157 158 159 |
data->stream.ops->free(&data->stream); kfree(data->stream.runtime->buffer); kfree(data->stream.runtime); kfree(data); return 0; } |
17ac8e5c6 ALSA: core: don't... |
160 |
static int snd_compr_update_tstamp(struct snd_compr_stream *stream, |
b21c60a4e ALSA: core: add s... |
161 162 163 |
struct snd_compr_tstamp *tstamp) { if (!stream->ops->pointer) |
17ac8e5c6 ALSA: core: don't... |
164 |
return -ENOTSUPP; |
b21c60a4e ALSA: core: add s... |
165 166 167 168 |
stream->ops->pointer(stream, tstamp); pr_debug("dsp consumed till %d total %d bytes ", tstamp->byte_offset, tstamp->copied_total); |
5b1f79f70 ALSA: compress_co... |
169 170 171 172 |
if (stream->direction == SND_COMPRESS_PLAYBACK) stream->runtime->total_bytes_transferred = tstamp->copied_total; else stream->runtime->total_bytes_available = tstamp->copied_total; |
17ac8e5c6 ALSA: core: don't... |
173 |
return 0; |
b21c60a4e ALSA: core: add s... |
174 175 176 177 178 |
} static size_t snd_compr_calc_avail(struct snd_compr_stream *stream, struct snd_compr_avail *avail) { |
17ac8e5c6 ALSA: core: don't... |
179 |
memset(avail, 0, sizeof(*avail)); |
b21c60a4e ALSA: core: add s... |
180 |
snd_compr_update_tstamp(stream, &avail->tstamp); |
17ac8e5c6 ALSA: core: don't... |
181 |
/* Still need to return avail even if tstamp can't be filled in */ |
b21c60a4e ALSA: core: add s... |
182 |
|
b21c60a4e ALSA: core: add s... |
183 |
if (stream->runtime->total_bytes_available == 0 && |
5b1f79f70 ALSA: compress_co... |
184 185 |
stream->runtime->state == SNDRV_PCM_STATE_SETUP && stream->direction == SND_COMPRESS_PLAYBACK) { |
b21c60a4e ALSA: core: add s... |
186 187 188 189 190 191 192 193 194 195 |
pr_debug("detected init and someone forgot to do a write "); return stream->runtime->buffer_size; } pr_debug("app wrote %lld, DSP consumed %lld ", stream->runtime->total_bytes_available, stream->runtime->total_bytes_transferred); if (stream->runtime->total_bytes_available == stream->runtime->total_bytes_transferred) { |
5b1f79f70 ALSA: compress_co... |
196 197 198 199 200 201 202 203 204 |
if (stream->direction == SND_COMPRESS_PLAYBACK) { pr_debug("both pointers are same, returning full avail "); return stream->runtime->buffer_size; } else { pr_debug("both pointers are same, returning no avail "); return 0; } |
b21c60a4e ALSA: core: add s... |
205 |
} |
5b1f79f70 ALSA: compress_co... |
206 207 208 209 |
avail->avail = stream->runtime->total_bytes_available - stream->runtime->total_bytes_transferred; if (stream->direction == SND_COMPRESS_PLAYBACK) avail->avail = stream->runtime->buffer_size - avail->avail; |
4c28e32d6 ALSA: compress_co... |
210 211 212 |
pr_debug("ret avail as %lld ", avail->avail); return avail->avail; |
b21c60a4e ALSA: core: add s... |
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 |
} static inline 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; size_t avail; avail = snd_compr_calc_avail(stream, &ioctl_avail); ioctl_avail.avail = avail; if (copy_to_user((__u64 __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; struct snd_compr_runtime *runtime = stream->runtime; |
f0283b58d ALSA: compress_co... |
243 244 245 246 247 |
/* 64-bit Modulus */ u64 app_pointer = div64_u64(runtime->total_bytes_available, runtime->buffer_size); app_pointer = runtime->total_bytes_available - (app_pointer * runtime->buffer_size); |
b21c60a4e ALSA: core: add s... |
248 |
|
f0283b58d ALSA: compress_co... |
249 |
dstn = runtime->buffer + app_pointer; |
b21c60a4e ALSA: core: add s... |
250 251 |
pr_debug("copying %ld at %lld ", |
f0283b58d ALSA: compress_co... |
252 253 |
(unsigned long)count, app_pointer); if (count < runtime->buffer_size - app_pointer) { |
b21c60a4e ALSA: core: add s... |
254 255 |
if (copy_from_user(dstn, buf, count)) return -EFAULT; |
b21c60a4e ALSA: core: add s... |
256 |
} else { |
f0283b58d ALSA: compress_co... |
257 |
copy = runtime->buffer_size - app_pointer; |
b21c60a4e ALSA: core: add s... |
258 259 260 261 |
if (copy_from_user(dstn, buf, copy)) return -EFAULT; if (copy_from_user(runtime->buffer, buf + copy, count - copy)) return -EFAULT; |
b21c60a4e ALSA: core: add s... |
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 |
} /* if DSP cares, let it know data has been written */ if (stream->ops->ack) stream->ops->ack(stream, count); return count; } static ssize_t snd_compr_write(struct file *f, const char __user *buf, size_t count, loff_t *offset) { struct snd_compr_file *data = f->private_data; struct snd_compr_stream *stream; size_t avail; int retval; if (snd_BUG_ON(!data)) return -EFAULT; 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 -EBADFD; } avail = snd_compr_get_avail(stream); pr_debug("avail returned %ld ", (unsigned long)avail); /* calculate how much we can write to buffer */ if (avail > count) avail = count; |
4daf891cd ALSA: compress_co... |
295 296 297 298 |
if (stream->ops->copy) { char __user* cbuf = (char __user*)buf; retval = stream->ops->copy(stream, cbuf, avail); } else { |
b21c60a4e ALSA: core: add s... |
299 |
retval = snd_compr_write_data(stream, buf, avail); |
4daf891cd ALSA: compress_co... |
300 |
} |
b21c60a4e ALSA: core: add s... |
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
if (retval > 0) stream->runtime->total_bytes_available += retval; /* 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; pr_debug("stream prepared, Houston we are good to go "); } 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) { |
49bb6402f ALSA: compress_co... |
320 321 322 323 324 325 326 327 328 329 |
struct snd_compr_file *data = f->private_data; struct snd_compr_stream *stream; size_t avail; int retval; if (snd_BUG_ON(!data)) return -EFAULT; stream = &data->stream; mutex_lock(&stream->device->lock); |
754813473 ALSA: compress: f... |
330 331 332 333 334 335 336 337 338 339 |
/* read is allowed when stream is running, paused, draining and setup * (yes setup is state which we transition to after stop, so if user * wants to read data after stop we allow that) */ switch (stream->runtime->state) { case SNDRV_PCM_STATE_OPEN: case SNDRV_PCM_STATE_PREPARED: case SNDRV_PCM_STATE_XRUN: case SNDRV_PCM_STATE_SUSPENDED: case SNDRV_PCM_STATE_DISCONNECTED: |
49bb6402f ALSA: compress_co... |
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
retval = -EBADFD; goto out; } avail = snd_compr_get_avail(stream); pr_debug("avail returned %ld ", (unsigned long)avail); /* calculate how much we can read from buffer */ if (avail > count) avail = count; if (stream->ops->copy) { retval = stream->ops->copy(stream, buf, avail); } else { retval = -ENXIO; goto out; } if (retval > 0) stream->runtime->total_bytes_transferred += retval; out: mutex_unlock(&stream->device->lock); return retval; |
b21c60a4e ALSA: core: add s... |
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 |
} static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma) { return -ENXIO; } static inline int snd_compr_get_poll(struct snd_compr_stream *stream) { if (stream->direction == SND_COMPRESS_PLAYBACK) return POLLOUT | POLLWRNORM; else return POLLIN | POLLRDNORM; } static unsigned int snd_compr_poll(struct file *f, poll_table *wait) { struct snd_compr_file *data = f->private_data; struct snd_compr_stream *stream; size_t avail; int retval = 0; if (snd_BUG_ON(!data)) return -EFAULT; stream = &data->stream; if (snd_BUG_ON(!stream)) return -EFAULT; mutex_lock(&stream->device->lock); |
c15b149a3 ALSA: compress_co... |
392 |
if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) { |
b21c60a4e ALSA: core: add s... |
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 |
retval = -EBADFD; goto out; } poll_wait(f, &stream->runtime->sleep, wait); avail = snd_compr_get_avail(stream); pr_debug("avail is %ld ", (unsigned long)avail); /* check if we have at least one fragment to fill */ switch (stream->runtime->state) { case SNDRV_PCM_STATE_DRAINING: /* stream has been woken up after drain is complete * draining done so set stream state to stopped */ retval = snd_compr_get_poll(stream); stream->runtime->state = SNDRV_PCM_STATE_SETUP; break; case SNDRV_PCM_STATE_RUNNING: case SNDRV_PCM_STATE_PREPARED: case SNDRV_PCM_STATE_PAUSED: if (avail >= stream->runtime->fragment_size) retval = snd_compr_get_poll(stream); break; default: if (stream->direction == SND_COMPRESS_PLAYBACK) retval = POLLOUT | POLLWRNORM | POLLERR; else retval = POLLIN | POLLRDNORM | POLLERR; break; } out: mutex_unlock(&stream->device->lock); return retval; } 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; |
1c62e9f2b ALSA: compress: i... |
436 |
memset(&caps, 0, sizeof(caps)); |
b21c60a4e ALSA: core: add s... |
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
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; |
47966e977 ALSA: compress: U... |
454 |
caps = kzalloc(sizeof(*caps), GFP_KERNEL); |
b21c60a4e ALSA: core: add s... |
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 |
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; } |
4dc040a0b ALSA: compress - ... |
493 494 495 496 497 498 |
static int snd_compress_check_input(struct snd_compr_params *params) { /* first let's check the buffer parameter's */ if (params->buffer.fragment_size == 0 || params->buffer.fragments > SIZE_MAX / params->buffer.fragment_size) return -EINVAL; |
fb4a9779f ALSA: Compress - ... |
499 500 501 502 503 504 |
/* now codec parameters */ if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX) return -EINVAL; if (params->codec.ch_in == 0 || params->codec.ch_out == 0) return -EINVAL; |
fb4a9779f ALSA: Compress - ... |
505 |
|
4dc040a0b ALSA: compress - ... |
506 507 |
return 0; } |
b21c60a4e ALSA: core: add s... |
508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
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; |
769fab2a4 ALSA: Fix memory ... |
522 523 524 525 |
if (copy_from_user(params, (void __user *)arg, sizeof(*params))) { retval = -EFAULT; goto out; } |
4dc040a0b ALSA: compress - ... |
526 527 528 529 |
retval = snd_compress_check_input(params); if (retval) goto out; |
b21c60a4e ALSA: core: add s... |
530 531 |
retval = snd_compr_allocate_buffer(stream, params); if (retval) { |
769fab2a4 ALSA: Fix memory ... |
532 533 |
retval = -ENOMEM; goto out; |
b21c60a4e ALSA: core: add s... |
534 |
} |
4dc040a0b ALSA: compress - ... |
535 |
|
b21c60a4e ALSA: core: add s... |
536 537 538 |
retval = stream->ops->set_params(stream, params); if (retval) goto out; |
49bb6402f ALSA: compress_co... |
539 |
|
9727b490e ALSA: compress: a... |
540 541 |
stream->metadata_set = false; stream->next_track = false; |
49bb6402f ALSA: compress_co... |
542 543 544 545 546 |
if (stream->direction == SND_COMPRESS_PLAYBACK) stream->runtime->state = SNDRV_PCM_STATE_SETUP; else stream->runtime->state = SNDRV_PCM_STATE_PREPARED; |
769fab2a4 ALSA: Fix memory ... |
547 |
} else { |
b21c60a4e ALSA: core: add s... |
548 |
return -EPERM; |
769fab2a4 ALSA: Fix memory ... |
549 |
} |
b21c60a4e ALSA: core: add s... |
550 551 552 553 554 555 556 557 558 559 560 561 562 |
out: kfree(params); return retval; } static int snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg) { struct snd_codec *params; int retval; if (!stream->ops->get_params) return -EBADFD; |
47966e977 ALSA: compress: U... |
563 |
params = kzalloc(sizeof(*params), GFP_KERNEL); |
b21c60a4e ALSA: core: add s... |
564 565 566 567 568 569 570 571 572 573 574 575 |
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; } |
9727b490e ALSA: compress: a... |
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 |
static int snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg) { struct snd_compr_metadata metadata; int retval; if (!stream->ops->get_metadata) return -ENXIO; if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata))) return -EFAULT; retval = stream->ops->get_metadata(stream, &metadata); if (retval != 0) return retval; if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata))) return -EFAULT; return 0; } static int snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg) { struct snd_compr_metadata metadata; int retval; if (!stream->ops->set_metadata) return -ENXIO; /* * we should allow parameter change only when stream has been * opened not in other cases */ if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata))) return -EFAULT; retval = stream->ops->set_metadata(stream, &metadata); stream->metadata_set = true; return retval; } |
b21c60a4e ALSA: core: add s... |
618 619 620 |
static inline int snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg) { |
17ac8e5c6 ALSA: core: don't... |
621 622 |
struct snd_compr_tstamp tstamp = {0}; int ret; |
b21c60a4e ALSA: core: add s... |
623 |
|
17ac8e5c6 ALSA: core: don't... |
624 625 626 627 628 |
ret = snd_compr_update_tstamp(stream, &tstamp); if (ret == 0) ret = copy_to_user((struct snd_compr_tstamp __user *)arg, &tstamp, sizeof(tstamp)) ? -EFAULT : 0; return ret; |
b21c60a4e ALSA: core: add s... |
629 630 631 632 633 634 635 636 637 |
} static int snd_compr_pause(struct snd_compr_stream *stream) { int retval; if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING) return -EPERM; retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH); |
6b18f7939 ALSA: compress_co... |
638 |
if (!retval) |
b21c60a4e ALSA: core: add s... |
639 |
stream->runtime->state = SNDRV_PCM_STATE_PAUSED; |
b21c60a4e ALSA: core: add s... |
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 |
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 || stream->runtime->state == SNDRV_PCM_STATE_SETUP) return -EPERM; retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP); if (!retval) { |
917f4b5cb ALSA: compress: f... |
676 |
snd_compr_drain_notify(stream); |
8b21460ac ALSA: compress_co... |
677 678 |
stream->runtime->total_bytes_available = 0; stream->runtime->total_bytes_transferred = 0; |
b21c60a4e ALSA: core: add s... |
679 680 681 |
} return retval; } |
917f4b5cb ALSA: compress: f... |
682 683 |
static int snd_compress_wait_for_drain(struct snd_compr_stream *stream) { |
f44f2a541 ALSA: compress: f... |
684 |
int ret; |
917f4b5cb ALSA: compress: f... |
685 686 687 688 689 690 691 692 693 694 |
/* * We are called with lock held. So drop the lock while we wait for * drain complete notfication from the driver * * It is expected that driver will notify the drain completion and then * stream will be moved to SETUP state, even if draining resulted in an * error. We can trigger next track after this. */ stream->runtime->state = SNDRV_PCM_STATE_DRAINING; mutex_unlock(&stream->device->lock); |
f44f2a541 ALSA: compress: f... |
695 696 697 698 699 700 701 702 703 704 705 706 707 |
/* we wait for drain to complete here, drain can return when * interruption occurred, wait returned error or success. * For the first two cases we don't do anything different here and * return after waking up */ ret = wait_event_interruptible(stream->runtime->sleep, (stream->runtime->state != SNDRV_PCM_STATE_DRAINING)); if (ret == -ERESTARTSYS) pr_debug("wait aborted by a signal"); else if (ret) pr_debug("wait for drain failed with %d ", ret); |
917f4b5cb ALSA: compress: f... |
708 709 710 |
wake_up(&stream->runtime->sleep); mutex_lock(&stream->device->lock); |
f44f2a541 ALSA: compress: f... |
711 |
return ret; |
917f4b5cb ALSA: compress: f... |
712 |
} |
b21c60a4e ALSA: core: add s... |
713 714 715 716 717 718 719 |
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_SETUP) return -EPERM; |
917f4b5cb ALSA: compress: f... |
720 |
|
b21c60a4e ALSA: core: add s... |
721 |
retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN); |
917f4b5cb ALSA: compress: f... |
722 |
if (retval) { |
f44f2a541 ALSA: compress: f... |
723 724 |
pr_debug("SND_COMPR_TRIGGER_DRAIN failed %d ", retval); |
b21c60a4e ALSA: core: add s... |
725 |
wake_up(&stream->runtime->sleep); |
917f4b5cb ALSA: compress: f... |
726 |
return retval; |
b21c60a4e ALSA: core: add s... |
727 |
} |
917f4b5cb ALSA: compress: f... |
728 |
|
f44f2a541 ALSA: compress: f... |
729 |
return snd_compress_wait_for_drain(stream); |
b21c60a4e ALSA: core: add s... |
730 |
} |
9727b490e ALSA: compress: a... |
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 |
static int snd_compr_next_track(struct snd_compr_stream *stream) { int retval; /* only a running stream can transition to next track */ if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING) return -EPERM; /* you can signal next track isf this is intended to be a gapless stream * and current track metadata is set */ if (stream->metadata_set == false) return -EPERM; retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK); if (retval != 0) return retval; stream->metadata_set = false; stream->next_track = true; return 0; } static int snd_compr_partial_drain(struct snd_compr_stream *stream) { int retval; if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED || stream->runtime->state == SNDRV_PCM_STATE_SETUP) return -EPERM; /* stream can be drained only when next track has been signalled */ if (stream->next_track == false) return -EPERM; retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN); |
917f4b5cb ALSA: compress: f... |
764 |
if (retval) { |
f44f2a541 ALSA: compress: f... |
765 766 |
pr_debug("Partial drain returned failure "); |
917f4b5cb ALSA: compress: f... |
767 768 769 |
wake_up(&stream->runtime->sleep); return retval; } |
9727b490e ALSA: compress: a... |
770 771 |
stream->next_track = false; |
917f4b5cb ALSA: compress: f... |
772 |
return snd_compress_wait_for_drain(stream); |
9727b490e ALSA: compress: a... |
773 |
} |
b21c60a4e ALSA: core: add s... |
774 775 776 777 778 779 780 781 782 783 784 785 786 787 |
static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg) { struct snd_compr_file *data = f->private_data; struct snd_compr_stream *stream; int retval = -ENOTTY; if (snd_BUG_ON(!data)) return -EFAULT; stream = &data->stream; if (snd_BUG_ON(!stream)) return -EFAULT; mutex_lock(&stream->device->lock); switch (_IOC_NR(cmd)) { case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION): |
a8d30608e ALSA: compress: f... |
788 |
retval = put_user(SNDRV_COMPRESS_VERSION, |
b21c60a4e ALSA: core: add s... |
789 790 791 792 793 794 795 796 797 798 799 800 801 802 |
(int __user *)arg) ? -EFAULT : 0; break; 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; |
9727b490e ALSA: compress: a... |
803 804 805 806 807 808 |
case _IOC_NR(SNDRV_COMPRESS_SET_METADATA): retval = snd_compr_set_metadata(stream, arg); break; case _IOC_NR(SNDRV_COMPRESS_GET_METADATA): retval = snd_compr_get_metadata(stream, arg); break; |
b21c60a4e ALSA: core: add s... |
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 |
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); break; 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): retval = snd_compr_drain(stream); break; |
9727b490e ALSA: compress: a... |
830 831 832 833 834 835 |
case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN): retval = snd_compr_partial_drain(stream); break; case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK): retval = snd_compr_next_track(stream); break; |
b21c60a4e ALSA: core: add s... |
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 |
} mutex_unlock(&stream->device->lock); return retval; } static const struct file_operations snd_compr_file_ops = { .owner = THIS_MODULE, .open = snd_compr_open, .release = snd_compr_free, .write = snd_compr_write, .read = snd_compr_read, .unlocked_ioctl = snd_compr_ioctl, .mmap = snd_compr_mmap, .poll = snd_compr_poll, }; static int snd_compress_dev_register(struct snd_device *device) { int ret = -EINVAL; char str[16]; struct snd_compr *compr; if (snd_BUG_ON(!device || !device->device_data)) return -EBADFD; compr = device->device_data; sprintf(str, "comprC%iD%i", compr->card->number, compr->device); pr_debug("reg %s for device %s, direction %d ", str, compr->name, compr->direction); /* register compressed device */ ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card, compr->device, &snd_compr_file_ops, compr, str); if (ret < 0) { pr_err("snd_register_device failed %d", ret); return ret; } return ret; } static int snd_compress_dev_disconnect(struct snd_device *device) { struct snd_compr *compr; compr = device->device_data; |
4028b6c4c ALSA: compress: F... |
883 884 |
snd_unregister_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card, compr->device); |
b21c60a4e ALSA: core: add s... |
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 |
return 0; } /* * snd_compress_new: create new compress device * @card: sound card pointer * @device: device number * @dirn: device direction, should be of type enum snd_compr_direction * @compr: compress device pointer */ int snd_compress_new(struct snd_card *card, int device, int dirn, struct snd_compr *compr) { static struct snd_device_ops ops = { .dev_free = NULL, .dev_register = snd_compress_dev_register, .dev_disconnect = snd_compress_dev_disconnect, }; compr->card = card; compr->device = device; compr->direction = dirn; return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops); } EXPORT_SYMBOL_GPL(snd_compress_new); static int snd_compress_add_device(struct snd_compr *device) { int ret; if (!device->card) return -EINVAL; /* register the card */ ret = snd_card_register(device->card); if (ret) goto out; return 0; out: pr_err("failed with %d ", ret); return ret; } static int snd_compress_remove_device(struct snd_compr *device) { return snd_card_free(device->card); } /** * 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; pr_debug("Registering compressed device %s ", device->name); if (snd_BUG_ON(!device->ops->open)) return -EINVAL; if (snd_BUG_ON(!device->ops->free)) return -EINVAL; if (snd_BUG_ON(!device->ops->set_params)) return -EINVAL; if (snd_BUG_ON(!device->ops->trigger)) return -EINVAL; mutex_init(&device->lock); /* register a compressed card */ mutex_lock(&device_mutex); retval = snd_compress_add_device(device); 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 ", device->name); mutex_lock(&device_mutex); snd_compress_remove_device(device); 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); MODULE_DESCRIPTION("ALSA Compressed offload framework"); MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>"); MODULE_LICENSE("GPL v2"); |