1 /* 2 * compress_core.c - compress offload core 3 * 4 * Copyright (C) 2011 Intel Corporation 5 * Authors: Vinod Koul <vinod.koul@linux.intel.com> 6 * Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com> 7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; version 2 of the License. 12 * 13 * This program is distributed in the hope that it will be useful, but 14 * WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License along 19 * with this program; if not, write to the Free Software Foundation, Inc., 20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 21 * 22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 23 * 24 */ 25 #define FORMAT(fmt) "%s: %d: " fmt, __func__, __LINE__ 26 #define pr_fmt(fmt) KBUILD_MODNAME ": " FORMAT(fmt) 27 28 #include <linux/file.h> 29 #include <linux/fs.h> 30 #include <linux/list.h> 31 #include <linux/mm.h> 32 #include <linux/mutex.h> 33 #include <linux/poll.h> 34 #include <linux/slab.h> 35 #include <linux/sched.h> 36 #include <linux/uio.h> 37 #include <linux/uaccess.h> 38 #include <linux/module.h> 39 #include <sound/core.h> 40 #include <sound/initval.h> 41 #include <sound/compress_params.h> 42 #include <sound/compress_offload.h> 43 #include <sound/compress_driver.h> 44 45 /* TODO: 46 * - add substream support for multiple devices in case of 47 * SND_DYNAMIC_MINORS is not used 48 * - Multiple node representation 49 * driver should be able to register multiple nodes 50 */ 51 52 static DEFINE_MUTEX(device_mutex); 53 54 struct snd_compr_file { 55 unsigned long caps; 56 struct snd_compr_stream stream; 57 }; 58 59 /* 60 * a note on stream states used: 61 * we use follwing states in the compressed core 62 * SNDRV_PCM_STATE_OPEN: When stream has been opened. 63 * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by 64 * calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this 65 * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain. 66 * SNDRV_PCM_STATE_RUNNING: When stream has been started and is 67 * decoding/encoding and rendering/capturing data. 68 * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done 69 * by calling SNDRV_COMPRESS_DRAIN. 70 * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling 71 * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling 72 * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively. 73 */ 74 static int snd_compr_open(struct inode *inode, struct file *f) 75 { 76 struct snd_compr *compr; 77 struct snd_compr_file *data; 78 struct snd_compr_runtime *runtime; 79 enum snd_compr_direction dirn; 80 int maj = imajor(inode); 81 int ret; 82 83 if ((f->f_flags & O_ACCMODE) == O_WRONLY) 84 dirn = SND_COMPRESS_PLAYBACK; 85 else if ((f->f_flags & O_ACCMODE) == O_RDONLY) 86 dirn = SND_COMPRESS_CAPTURE; 87 else 88 return -EINVAL; 89 90 if (maj == snd_major) 91 compr = snd_lookup_minor_data(iminor(inode), 92 SNDRV_DEVICE_TYPE_COMPRESS); 93 else 94 return -EBADFD; 95 96 if (compr == NULL) { 97 pr_err("no device data!!!\n"); 98 return -ENODEV; 99 } 100 101 if (dirn != compr->direction) { 102 pr_err("this device doesn't support this direction\n"); 103 snd_card_unref(compr->card); 104 return -EINVAL; 105 } 106 107 data = kzalloc(sizeof(*data), GFP_KERNEL); 108 if (!data) { 109 snd_card_unref(compr->card); 110 return -ENOMEM; 111 } 112 data->stream.ops = compr->ops; 113 data->stream.direction = dirn; 114 data->stream.private_data = compr->private_data; 115 data->stream.device = compr; 116 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL); 117 if (!runtime) { 118 kfree(data); 119 snd_card_unref(compr->card); 120 return -ENOMEM; 121 } 122 runtime->state = SNDRV_PCM_STATE_OPEN; 123 init_waitqueue_head(&runtime->sleep); 124 data->stream.runtime = runtime; 125 f->private_data = (void *)data; 126 mutex_lock(&compr->lock); 127 ret = compr->ops->open(&data->stream); 128 mutex_unlock(&compr->lock); 129 if (ret) { 130 kfree(runtime); 131 kfree(data); 132 } 133 snd_card_unref(compr->card); 134 return 0; 135 } 136 137 static int snd_compr_free(struct inode *inode, struct file *f) 138 { 139 struct snd_compr_file *data = f->private_data; 140 data->stream.ops->free(&data->stream); 141 kfree(data->stream.runtime->buffer); 142 kfree(data->stream.runtime); 143 kfree(data); 144 return 0; 145 } 146 147 static int snd_compr_update_tstamp(struct snd_compr_stream *stream, 148 struct snd_compr_tstamp *tstamp) 149 { 150 if (!stream->ops->pointer) 151 return -ENOTSUPP; 152 stream->ops->pointer(stream, tstamp); 153 pr_debug("dsp consumed till %d total %d bytes\n", 154 tstamp->byte_offset, tstamp->copied_total); 155 stream->runtime->hw_pointer = tstamp->byte_offset; 156 stream->runtime->total_bytes_transferred = tstamp->copied_total; 157 return 0; 158 } 159 160 static size_t snd_compr_calc_avail(struct snd_compr_stream *stream, 161 struct snd_compr_avail *avail) 162 { 163 long avail_calc; /*this needs to be signed variable */ 164 165 memset(avail, 0, sizeof(*avail)); 166 snd_compr_update_tstamp(stream, &avail->tstamp); 167 /* Still need to return avail even if tstamp can't be filled in */ 168 169 /* FIXME: This needs to be different for capture stream, 170 available is # of compressed data, for playback it's 171 remainder of buffer */ 172 173 if (stream->runtime->total_bytes_available == 0 && 174 stream->runtime->state == SNDRV_PCM_STATE_SETUP) { 175 pr_debug("detected init and someone forgot to do a write\n"); 176 return stream->runtime->buffer_size; 177 } 178 pr_debug("app wrote %lld, DSP consumed %lld\n", 179 stream->runtime->total_bytes_available, 180 stream->runtime->total_bytes_transferred); 181 if (stream->runtime->total_bytes_available == 182 stream->runtime->total_bytes_transferred) { 183 pr_debug("both pointers are same, returning full avail\n"); 184 return stream->runtime->buffer_size; 185 } 186 187 /* FIXME: this routine isn't consistent, in one test we use 188 * cumulative values and in the other byte offsets. Do we 189 * really need the byte offsets if the cumulative values have 190 * been updated? In the PCM interface app_ptr and hw_ptr are 191 * already cumulative */ 192 193 avail_calc = stream->runtime->buffer_size - 194 (stream->runtime->app_pointer - stream->runtime->hw_pointer); 195 pr_debug("calc avail as %ld, app_ptr %lld, hw+ptr %lld\n", avail_calc, 196 stream->runtime->app_pointer, 197 stream->runtime->hw_pointer); 198 if (avail_calc >= stream->runtime->buffer_size) 199 avail_calc -= stream->runtime->buffer_size; 200 pr_debug("ret avail as %ld\n", avail_calc); 201 avail->avail = avail_calc; 202 return avail_calc; 203 } 204 205 static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream) 206 { 207 struct snd_compr_avail avail; 208 209 return snd_compr_calc_avail(stream, &avail); 210 } 211 212 static int 213 snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg) 214 { 215 struct snd_compr_avail ioctl_avail; 216 size_t avail; 217 218 avail = snd_compr_calc_avail(stream, &ioctl_avail); 219 ioctl_avail.avail = avail; 220 221 if (copy_to_user((__u64 __user *)arg, 222 &ioctl_avail, sizeof(ioctl_avail))) 223 return -EFAULT; 224 return 0; 225 } 226 227 static int snd_compr_write_data(struct snd_compr_stream *stream, 228 const char __user *buf, size_t count) 229 { 230 void *dstn; 231 size_t copy; 232 struct snd_compr_runtime *runtime = stream->runtime; 233 234 dstn = runtime->buffer + runtime->app_pointer; 235 pr_debug("copying %ld at %lld\n", 236 (unsigned long)count, runtime->app_pointer); 237 if (count < runtime->buffer_size - runtime->app_pointer) { 238 if (copy_from_user(dstn, buf, count)) 239 return -EFAULT; 240 runtime->app_pointer += count; 241 } else { 242 copy = runtime->buffer_size - runtime->app_pointer; 243 if (copy_from_user(dstn, buf, copy)) 244 return -EFAULT; 245 if (copy_from_user(runtime->buffer, buf + copy, count - copy)) 246 return -EFAULT; 247 runtime->app_pointer = count - copy; 248 } 249 /* if DSP cares, let it know data has been written */ 250 if (stream->ops->ack) 251 stream->ops->ack(stream, count); 252 return count; 253 } 254 255 static ssize_t snd_compr_write(struct file *f, const char __user *buf, 256 size_t count, loff_t *offset) 257 { 258 struct snd_compr_file *data = f->private_data; 259 struct snd_compr_stream *stream; 260 size_t avail; 261 int retval; 262 263 if (snd_BUG_ON(!data)) 264 return -EFAULT; 265 266 stream = &data->stream; 267 mutex_lock(&stream->device->lock); 268 /* write is allowed when stream is running or has been steup */ 269 if (stream->runtime->state != SNDRV_PCM_STATE_SETUP && 270 stream->runtime->state != SNDRV_PCM_STATE_RUNNING) { 271 mutex_unlock(&stream->device->lock); 272 return -EBADFD; 273 } 274 275 avail = snd_compr_get_avail(stream); 276 pr_debug("avail returned %ld\n", (unsigned long)avail); 277 /* calculate how much we can write to buffer */ 278 if (avail > count) 279 avail = count; 280 281 if (stream->ops->copy) 282 retval = stream->ops->copy(stream, buf, avail); 283 else 284 retval = snd_compr_write_data(stream, buf, avail); 285 if (retval > 0) 286 stream->runtime->total_bytes_available += retval; 287 288 /* while initiating the stream, write should be called before START 289 * call, so in setup move state */ 290 if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) { 291 stream->runtime->state = SNDRV_PCM_STATE_PREPARED; 292 pr_debug("stream prepared, Houston we are good to go\n"); 293 } 294 295 mutex_unlock(&stream->device->lock); 296 return retval; 297 } 298 299 300 static ssize_t snd_compr_read(struct file *f, char __user *buf, 301 size_t count, loff_t *offset) 302 { 303 return -ENXIO; 304 } 305 306 static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma) 307 { 308 return -ENXIO; 309 } 310 311 static inline int snd_compr_get_poll(struct snd_compr_stream *stream) 312 { 313 if (stream->direction == SND_COMPRESS_PLAYBACK) 314 return POLLOUT | POLLWRNORM; 315 else 316 return POLLIN | POLLRDNORM; 317 } 318 319 static unsigned int snd_compr_poll(struct file *f, poll_table *wait) 320 { 321 struct snd_compr_file *data = f->private_data; 322 struct snd_compr_stream *stream; 323 size_t avail; 324 int retval = 0; 325 326 if (snd_BUG_ON(!data)) 327 return -EFAULT; 328 stream = &data->stream; 329 if (snd_BUG_ON(!stream)) 330 return -EFAULT; 331 332 mutex_lock(&stream->device->lock); 333 if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED || 334 stream->runtime->state == SNDRV_PCM_STATE_OPEN) { 335 retval = -EBADFD; 336 goto out; 337 } 338 poll_wait(f, &stream->runtime->sleep, wait); 339 340 avail = snd_compr_get_avail(stream); 341 pr_debug("avail is %ld\n", (unsigned long)avail); 342 /* check if we have at least one fragment to fill */ 343 switch (stream->runtime->state) { 344 case SNDRV_PCM_STATE_DRAINING: 345 /* stream has been woken up after drain is complete 346 * draining done so set stream state to stopped 347 */ 348 retval = snd_compr_get_poll(stream); 349 stream->runtime->state = SNDRV_PCM_STATE_SETUP; 350 break; 351 case SNDRV_PCM_STATE_RUNNING: 352 case SNDRV_PCM_STATE_PREPARED: 353 case SNDRV_PCM_STATE_PAUSED: 354 if (avail >= stream->runtime->fragment_size) 355 retval = snd_compr_get_poll(stream); 356 break; 357 default: 358 if (stream->direction == SND_COMPRESS_PLAYBACK) 359 retval = POLLOUT | POLLWRNORM | POLLERR; 360 else 361 retval = POLLIN | POLLRDNORM | POLLERR; 362 break; 363 } 364 out: 365 mutex_unlock(&stream->device->lock); 366 return retval; 367 } 368 369 static int 370 snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg) 371 { 372 int retval; 373 struct snd_compr_caps caps; 374 375 if (!stream->ops->get_caps) 376 return -ENXIO; 377 378 retval = stream->ops->get_caps(stream, &caps); 379 if (retval) 380 goto out; 381 if (copy_to_user((void __user *)arg, &caps, sizeof(caps))) 382 retval = -EFAULT; 383 out: 384 return retval; 385 } 386 387 static int 388 snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg) 389 { 390 int retval; 391 struct snd_compr_codec_caps *caps; 392 393 if (!stream->ops->get_codec_caps) 394 return -ENXIO; 395 396 caps = kmalloc(sizeof(*caps), GFP_KERNEL); 397 if (!caps) 398 return -ENOMEM; 399 400 retval = stream->ops->get_codec_caps(stream, caps); 401 if (retval) 402 goto out; 403 if (copy_to_user((void __user *)arg, caps, sizeof(*caps))) 404 retval = -EFAULT; 405 406 out: 407 kfree(caps); 408 return retval; 409 } 410 411 /* revisit this with snd_pcm_preallocate_xxx */ 412 static int snd_compr_allocate_buffer(struct snd_compr_stream *stream, 413 struct snd_compr_params *params) 414 { 415 unsigned int buffer_size; 416 void *buffer; 417 418 buffer_size = params->buffer.fragment_size * params->buffer.fragments; 419 if (stream->ops->copy) { 420 buffer = NULL; 421 /* if copy is defined the driver will be required to copy 422 * the data from core 423 */ 424 } else { 425 buffer = kmalloc(buffer_size, GFP_KERNEL); 426 if (!buffer) 427 return -ENOMEM; 428 } 429 stream->runtime->fragment_size = params->buffer.fragment_size; 430 stream->runtime->fragments = params->buffer.fragments; 431 stream->runtime->buffer = buffer; 432 stream->runtime->buffer_size = buffer_size; 433 return 0; 434 } 435 436 static int snd_compress_check_input(struct snd_compr_params *params) 437 { 438 /* first let's check the buffer parameter's */ 439 if (params->buffer.fragment_size == 0 || 440 params->buffer.fragments > SIZE_MAX / params->buffer.fragment_size) 441 return -EINVAL; 442 443 /* now codec parameters */ 444 if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX) 445 return -EINVAL; 446 447 if (params->codec.ch_in == 0 || params->codec.ch_out == 0) 448 return -EINVAL; 449 450 if (!(params->codec.sample_rate & SNDRV_PCM_RATE_8000_192000)) 451 return -EINVAL; 452 453 return 0; 454 } 455 456 static int 457 snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg) 458 { 459 struct snd_compr_params *params; 460 int retval; 461 462 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) { 463 /* 464 * we should allow parameter change only when stream has been 465 * opened not in other cases 466 */ 467 params = kmalloc(sizeof(*params), GFP_KERNEL); 468 if (!params) 469 return -ENOMEM; 470 if (copy_from_user(params, (void __user *)arg, sizeof(*params))) { 471 retval = -EFAULT; 472 goto out; 473 } 474 475 retval = snd_compress_check_input(params); 476 if (retval) 477 goto out; 478 479 retval = snd_compr_allocate_buffer(stream, params); 480 if (retval) { 481 retval = -ENOMEM; 482 goto out; 483 } 484 485 retval = stream->ops->set_params(stream, params); 486 if (retval) 487 goto out; 488 stream->runtime->state = SNDRV_PCM_STATE_SETUP; 489 stream->metadata_set = false; 490 stream->next_track = false; 491 } else { 492 return -EPERM; 493 } 494 out: 495 kfree(params); 496 return retval; 497 } 498 499 static int 500 snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg) 501 { 502 struct snd_codec *params; 503 int retval; 504 505 if (!stream->ops->get_params) 506 return -EBADFD; 507 508 params = kmalloc(sizeof(*params), GFP_KERNEL); 509 if (!params) 510 return -ENOMEM; 511 retval = stream->ops->get_params(stream, params); 512 if (retval) 513 goto out; 514 if (copy_to_user((char __user *)arg, params, sizeof(*params))) 515 retval = -EFAULT; 516 517 out: 518 kfree(params); 519 return retval; 520 } 521 522 static int 523 snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg) 524 { 525 struct snd_compr_metadata metadata; 526 int retval; 527 528 if (!stream->ops->get_metadata) 529 return -ENXIO; 530 531 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata))) 532 return -EFAULT; 533 534 retval = stream->ops->get_metadata(stream, &metadata); 535 if (retval != 0) 536 return retval; 537 538 if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata))) 539 return -EFAULT; 540 541 return 0; 542 } 543 544 static int 545 snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg) 546 { 547 struct snd_compr_metadata metadata; 548 int retval; 549 550 if (!stream->ops->set_metadata) 551 return -ENXIO; 552 /* 553 * we should allow parameter change only when stream has been 554 * opened not in other cases 555 */ 556 if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata))) 557 return -EFAULT; 558 559 retval = stream->ops->set_metadata(stream, &metadata); 560 stream->metadata_set = true; 561 562 return retval; 563 } 564 565 static inline int 566 snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg) 567 { 568 struct snd_compr_tstamp tstamp = {0}; 569 int ret; 570 571 ret = snd_compr_update_tstamp(stream, &tstamp); 572 if (ret == 0) 573 ret = copy_to_user((struct snd_compr_tstamp __user *)arg, 574 &tstamp, sizeof(tstamp)) ? -EFAULT : 0; 575 return ret; 576 } 577 578 static int snd_compr_pause(struct snd_compr_stream *stream) 579 { 580 int retval; 581 582 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING) 583 return -EPERM; 584 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH); 585 if (!retval) 586 stream->runtime->state = SNDRV_PCM_STATE_PAUSED; 587 return retval; 588 } 589 590 static int snd_compr_resume(struct snd_compr_stream *stream) 591 { 592 int retval; 593 594 if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED) 595 return -EPERM; 596 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE); 597 if (!retval) 598 stream->runtime->state = SNDRV_PCM_STATE_RUNNING; 599 return retval; 600 } 601 602 static int snd_compr_start(struct snd_compr_stream *stream) 603 { 604 int retval; 605 606 if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED) 607 return -EPERM; 608 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START); 609 if (!retval) 610 stream->runtime->state = SNDRV_PCM_STATE_RUNNING; 611 return retval; 612 } 613 614 static int snd_compr_stop(struct snd_compr_stream *stream) 615 { 616 int retval; 617 618 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED || 619 stream->runtime->state == SNDRV_PCM_STATE_SETUP) 620 return -EPERM; 621 retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP); 622 if (!retval) { 623 stream->runtime->state = SNDRV_PCM_STATE_SETUP; 624 wake_up(&stream->runtime->sleep); 625 stream->runtime->hw_pointer = 0; 626 stream->runtime->app_pointer = 0; 627 stream->runtime->total_bytes_available = 0; 628 stream->runtime->total_bytes_transferred = 0; 629 } 630 return retval; 631 } 632 633 static int snd_compr_drain(struct snd_compr_stream *stream) 634 { 635 int retval; 636 637 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED || 638 stream->runtime->state == SNDRV_PCM_STATE_SETUP) 639 return -EPERM; 640 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN); 641 if (!retval) { 642 stream->runtime->state = SNDRV_PCM_STATE_DRAINING; 643 wake_up(&stream->runtime->sleep); 644 } 645 return retval; 646 } 647 648 static int snd_compr_next_track(struct snd_compr_stream *stream) 649 { 650 int retval; 651 652 /* only a running stream can transition to next track */ 653 if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING) 654 return -EPERM; 655 656 /* you can signal next track isf this is intended to be a gapless stream 657 * and current track metadata is set 658 */ 659 if (stream->metadata_set == false) 660 return -EPERM; 661 662 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK); 663 if (retval != 0) 664 return retval; 665 stream->metadata_set = false; 666 stream->next_track = true; 667 return 0; 668 } 669 670 static int snd_compr_partial_drain(struct snd_compr_stream *stream) 671 { 672 int retval; 673 if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED || 674 stream->runtime->state == SNDRV_PCM_STATE_SETUP) 675 return -EPERM; 676 /* stream can be drained only when next track has been signalled */ 677 if (stream->next_track == false) 678 return -EPERM; 679 680 retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN); 681 682 stream->next_track = false; 683 return retval; 684 } 685 686 static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg) 687 { 688 struct snd_compr_file *data = f->private_data; 689 struct snd_compr_stream *stream; 690 int retval = -ENOTTY; 691 692 if (snd_BUG_ON(!data)) 693 return -EFAULT; 694 stream = &data->stream; 695 if (snd_BUG_ON(!stream)) 696 return -EFAULT; 697 mutex_lock(&stream->device->lock); 698 switch (_IOC_NR(cmd)) { 699 case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION): 700 put_user(SNDRV_COMPRESS_VERSION, 701 (int __user *)arg) ? -EFAULT : 0; 702 break; 703 case _IOC_NR(SNDRV_COMPRESS_GET_CAPS): 704 retval = snd_compr_get_caps(stream, arg); 705 break; 706 case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS): 707 retval = snd_compr_get_codec_caps(stream, arg); 708 break; 709 case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS): 710 retval = snd_compr_set_params(stream, arg); 711 break; 712 case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS): 713 retval = snd_compr_get_params(stream, arg); 714 break; 715 case _IOC_NR(SNDRV_COMPRESS_SET_METADATA): 716 retval = snd_compr_set_metadata(stream, arg); 717 break; 718 case _IOC_NR(SNDRV_COMPRESS_GET_METADATA): 719 retval = snd_compr_get_metadata(stream, arg); 720 break; 721 case _IOC_NR(SNDRV_COMPRESS_TSTAMP): 722 retval = snd_compr_tstamp(stream, arg); 723 break; 724 case _IOC_NR(SNDRV_COMPRESS_AVAIL): 725 retval = snd_compr_ioctl_avail(stream, arg); 726 break; 727 case _IOC_NR(SNDRV_COMPRESS_PAUSE): 728 retval = snd_compr_pause(stream); 729 break; 730 case _IOC_NR(SNDRV_COMPRESS_RESUME): 731 retval = snd_compr_resume(stream); 732 break; 733 case _IOC_NR(SNDRV_COMPRESS_START): 734 retval = snd_compr_start(stream); 735 break; 736 case _IOC_NR(SNDRV_COMPRESS_STOP): 737 retval = snd_compr_stop(stream); 738 break; 739 case _IOC_NR(SNDRV_COMPRESS_DRAIN): 740 retval = snd_compr_drain(stream); 741 break; 742 case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN): 743 retval = snd_compr_partial_drain(stream); 744 break; 745 case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK): 746 retval = snd_compr_next_track(stream); 747 break; 748 749 } 750 mutex_unlock(&stream->device->lock); 751 return retval; 752 } 753 754 static const struct file_operations snd_compr_file_ops = { 755 .owner = THIS_MODULE, 756 .open = snd_compr_open, 757 .release = snd_compr_free, 758 .write = snd_compr_write, 759 .read = snd_compr_read, 760 .unlocked_ioctl = snd_compr_ioctl, 761 .mmap = snd_compr_mmap, 762 .poll = snd_compr_poll, 763 }; 764 765 static int snd_compress_dev_register(struct snd_device *device) 766 { 767 int ret = -EINVAL; 768 char str[16]; 769 struct snd_compr *compr; 770 771 if (snd_BUG_ON(!device || !device->device_data)) 772 return -EBADFD; 773 compr = device->device_data; 774 775 sprintf(str, "comprC%iD%i", compr->card->number, compr->device); 776 pr_debug("reg %s for device %s, direction %d\n", str, compr->name, 777 compr->direction); 778 /* register compressed device */ 779 ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card, 780 compr->device, &snd_compr_file_ops, compr, str); 781 if (ret < 0) { 782 pr_err("snd_register_device failed\n %d", ret); 783 return ret; 784 } 785 return ret; 786 787 } 788 789 static int snd_compress_dev_disconnect(struct snd_device *device) 790 { 791 struct snd_compr *compr; 792 793 compr = device->device_data; 794 snd_unregister_device(compr->direction, compr->card, compr->device); 795 return 0; 796 } 797 798 /* 799 * snd_compress_new: create new compress device 800 * @card: sound card pointer 801 * @device: device number 802 * @dirn: device direction, should be of type enum snd_compr_direction 803 * @compr: compress device pointer 804 */ 805 int snd_compress_new(struct snd_card *card, int device, 806 int dirn, struct snd_compr *compr) 807 { 808 static struct snd_device_ops ops = { 809 .dev_free = NULL, 810 .dev_register = snd_compress_dev_register, 811 .dev_disconnect = snd_compress_dev_disconnect, 812 }; 813 814 compr->card = card; 815 compr->device = device; 816 compr->direction = dirn; 817 return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops); 818 } 819 EXPORT_SYMBOL_GPL(snd_compress_new); 820 821 static int snd_compress_add_device(struct snd_compr *device) 822 { 823 int ret; 824 825 if (!device->card) 826 return -EINVAL; 827 828 /* register the card */ 829 ret = snd_card_register(device->card); 830 if (ret) 831 goto out; 832 return 0; 833 834 out: 835 pr_err("failed with %d\n", ret); 836 return ret; 837 838 } 839 840 static int snd_compress_remove_device(struct snd_compr *device) 841 { 842 return snd_card_free(device->card); 843 } 844 845 /** 846 * snd_compress_register - register compressed device 847 * 848 * @device: compressed device to register 849 */ 850 int snd_compress_register(struct snd_compr *device) 851 { 852 int retval; 853 854 if (device->name == NULL || device->dev == NULL || device->ops == NULL) 855 return -EINVAL; 856 857 pr_debug("Registering compressed device %s\n", device->name); 858 if (snd_BUG_ON(!device->ops->open)) 859 return -EINVAL; 860 if (snd_BUG_ON(!device->ops->free)) 861 return -EINVAL; 862 if (snd_BUG_ON(!device->ops->set_params)) 863 return -EINVAL; 864 if (snd_BUG_ON(!device->ops->trigger)) 865 return -EINVAL; 866 867 mutex_init(&device->lock); 868 869 /* register a compressed card */ 870 mutex_lock(&device_mutex); 871 retval = snd_compress_add_device(device); 872 mutex_unlock(&device_mutex); 873 return retval; 874 } 875 EXPORT_SYMBOL_GPL(snd_compress_register); 876 877 int snd_compress_deregister(struct snd_compr *device) 878 { 879 pr_debug("Removing compressed device %s\n", device->name); 880 mutex_lock(&device_mutex); 881 snd_compress_remove_device(device); 882 mutex_unlock(&device_mutex); 883 return 0; 884 } 885 EXPORT_SYMBOL_GPL(snd_compress_deregister); 886 887 static int __init snd_compress_init(void) 888 { 889 return 0; 890 } 891 892 static void __exit snd_compress_exit(void) 893 { 894 } 895 896 module_init(snd_compress_init); 897 module_exit(snd_compress_exit); 898 899 MODULE_DESCRIPTION("ALSA Compressed offload framework"); 900 MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>"); 901 MODULE_LICENSE("GPL v2"); 902