1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Timers abstract layer 4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 5 */ 6 7 #include <linux/delay.h> 8 #include <linux/init.h> 9 #include <linux/slab.h> 10 #include <linux/time.h> 11 #include <linux/mutex.h> 12 #include <linux/device.h> 13 #include <linux/module.h> 14 #include <linux/string.h> 15 #include <linux/sched/signal.h> 16 #include <sound/core.h> 17 #include <sound/timer.h> 18 #include <sound/control.h> 19 #include <sound/info.h> 20 #include <sound/minors.h> 21 #include <sound/initval.h> 22 #include <linux/kmod.h> 23 24 /* internal flags */ 25 #define SNDRV_TIMER_IFLG_PAUSED 0x00010000 26 #define SNDRV_TIMER_IFLG_DEAD 0x00020000 27 28 #if IS_ENABLED(CONFIG_SND_HRTIMER) 29 #define DEFAULT_TIMER_LIMIT 4 30 #else 31 #define DEFAULT_TIMER_LIMIT 1 32 #endif 33 34 static int timer_limit = DEFAULT_TIMER_LIMIT; 35 static int timer_tstamp_monotonic = 1; 36 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>"); 37 MODULE_DESCRIPTION("ALSA timer interface"); 38 MODULE_LICENSE("GPL"); 39 module_param(timer_limit, int, 0444); 40 MODULE_PARM_DESC(timer_limit, "Maximum global timers in system."); 41 module_param(timer_tstamp_monotonic, int, 0444); 42 MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default)."); 43 44 MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER); 45 MODULE_ALIAS("devname:snd/timer"); 46 47 enum timer_tread_format { 48 TREAD_FORMAT_NONE = 0, 49 TREAD_FORMAT_TIME64, 50 TREAD_FORMAT_TIME32, 51 }; 52 53 struct snd_timer_tread32 { 54 int event; 55 s32 tstamp_sec; 56 s32 tstamp_nsec; 57 unsigned int val; 58 }; 59 60 struct snd_timer_tread64 { 61 int event; 62 u8 pad1[4]; 63 s64 tstamp_sec; 64 s64 tstamp_nsec; 65 unsigned int val; 66 u8 pad2[4]; 67 }; 68 69 struct snd_timer_user { 70 struct snd_timer_instance *timeri; 71 int tread; /* enhanced read with timestamps and events */ 72 unsigned long ticks; 73 unsigned long overrun; 74 int qhead; 75 int qtail; 76 int qused; 77 int queue_size; 78 bool disconnected; 79 struct snd_timer_read *queue; 80 struct snd_timer_tread64 *tqueue; 81 spinlock_t qlock; 82 unsigned long last_resolution; 83 unsigned int filter; 84 struct timespec64 tstamp; /* trigger tstamp */ 85 wait_queue_head_t qchange_sleep; 86 struct snd_fasync *fasync; 87 struct mutex ioctl_lock; 88 }; 89 90 struct snd_timer_status32 { 91 s32 tstamp_sec; /* Timestamp - last update */ 92 s32 tstamp_nsec; 93 unsigned int resolution; /* current period resolution in ns */ 94 unsigned int lost; /* counter of master tick lost */ 95 unsigned int overrun; /* count of read queue overruns */ 96 unsigned int queue; /* used queue size */ 97 unsigned char reserved[64]; /* reserved */ 98 }; 99 100 #define SNDRV_TIMER_IOCTL_STATUS32 _IOR('T', 0x14, struct snd_timer_status32) 101 102 struct snd_timer_status64 { 103 s64 tstamp_sec; /* Timestamp - last update */ 104 s64 tstamp_nsec; 105 unsigned int resolution; /* current period resolution in ns */ 106 unsigned int lost; /* counter of master tick lost */ 107 unsigned int overrun; /* count of read queue overruns */ 108 unsigned int queue; /* used queue size */ 109 unsigned char reserved[64]; /* reserved */ 110 }; 111 112 #define SNDRV_TIMER_IOCTL_STATUS64 _IOR('T', 0x14, struct snd_timer_status64) 113 114 /* list of timers */ 115 static LIST_HEAD(snd_timer_list); 116 117 /* list of slave instances */ 118 static LIST_HEAD(snd_timer_slave_list); 119 120 /* lock for slave active lists */ 121 static DEFINE_SPINLOCK(slave_active_lock); 122 123 #define MAX_SLAVE_INSTANCES 1000 124 static int num_slaves; 125 126 static DEFINE_MUTEX(register_mutex); 127 128 static int snd_timer_free(struct snd_timer *timer); 129 static int snd_timer_dev_free(struct snd_device *device); 130 static int snd_timer_dev_register(struct snd_device *device); 131 static int snd_timer_dev_disconnect(struct snd_device *device); 132 133 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left); 134 135 /* 136 * create a timer instance with the given owner string. 137 */ 138 struct snd_timer_instance *snd_timer_instance_new(const char *owner) 139 { 140 struct snd_timer_instance *timeri; 141 142 timeri = kzalloc(sizeof(*timeri), GFP_KERNEL); 143 if (timeri == NULL) 144 return NULL; 145 timeri->owner = kstrdup(owner, GFP_KERNEL); 146 if (! timeri->owner) { 147 kfree(timeri); 148 return NULL; 149 } 150 INIT_LIST_HEAD(&timeri->open_list); 151 INIT_LIST_HEAD(&timeri->active_list); 152 INIT_LIST_HEAD(&timeri->ack_list); 153 INIT_LIST_HEAD(&timeri->slave_list_head); 154 INIT_LIST_HEAD(&timeri->slave_active_head); 155 156 return timeri; 157 } 158 EXPORT_SYMBOL(snd_timer_instance_new); 159 160 void snd_timer_instance_free(struct snd_timer_instance *timeri) 161 { 162 if (timeri) { 163 if (timeri->private_free) 164 timeri->private_free(timeri); 165 kfree(timeri->owner); 166 kfree(timeri); 167 } 168 } 169 EXPORT_SYMBOL(snd_timer_instance_free); 170 171 /* 172 * find a timer instance from the given timer id 173 */ 174 static struct snd_timer *snd_timer_find(struct snd_timer_id *tid) 175 { 176 struct snd_timer *timer; 177 178 list_for_each_entry(timer, &snd_timer_list, device_list) { 179 if (timer->tmr_class != tid->dev_class) 180 continue; 181 if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD || 182 timer->tmr_class == SNDRV_TIMER_CLASS_PCM) && 183 (timer->card == NULL || 184 timer->card->number != tid->card)) 185 continue; 186 if (timer->tmr_device != tid->device) 187 continue; 188 if (timer->tmr_subdevice != tid->subdevice) 189 continue; 190 return timer; 191 } 192 return NULL; 193 } 194 195 #ifdef CONFIG_MODULES 196 197 static void snd_timer_request(struct snd_timer_id *tid) 198 { 199 switch (tid->dev_class) { 200 case SNDRV_TIMER_CLASS_GLOBAL: 201 if (tid->device < timer_limit) 202 request_module("snd-timer-%i", tid->device); 203 break; 204 case SNDRV_TIMER_CLASS_CARD: 205 case SNDRV_TIMER_CLASS_PCM: 206 if (tid->card < snd_ecards_limit) 207 request_module("snd-card-%i", tid->card); 208 break; 209 default: 210 break; 211 } 212 } 213 214 #endif 215 216 /* move the slave if it belongs to the master; return 1 if match */ 217 static int check_matching_master_slave(struct snd_timer_instance *master, 218 struct snd_timer_instance *slave) 219 { 220 if (slave->slave_class != master->slave_class || 221 slave->slave_id != master->slave_id) 222 return 0; 223 if (master->timer->num_instances >= master->timer->max_instances) 224 return -EBUSY; 225 list_move_tail(&slave->open_list, &master->slave_list_head); 226 master->timer->num_instances++; 227 guard(spinlock_irq)(&slave_active_lock); 228 guard(spinlock)(&master->timer->lock); 229 slave->master = master; 230 slave->timer = master->timer; 231 if (slave->flags & SNDRV_TIMER_IFLG_RUNNING) 232 list_add_tail(&slave->active_list, &master->slave_active_head); 233 return 1; 234 } 235 236 /* 237 * look for a master instance matching with the slave id of the given slave. 238 * when found, relink the open_link of the slave. 239 * 240 * call this with register_mutex down. 241 */ 242 static int snd_timer_check_slave(struct snd_timer_instance *slave) 243 { 244 struct snd_timer *timer; 245 struct snd_timer_instance *master; 246 int err = 0; 247 248 /* FIXME: it's really dumb to look up all entries.. */ 249 list_for_each_entry(timer, &snd_timer_list, device_list) { 250 list_for_each_entry(master, &timer->open_list_head, open_list) { 251 err = check_matching_master_slave(master, slave); 252 if (err != 0) /* match found or error */ 253 goto out; 254 } 255 } 256 out: 257 return err < 0 ? err : 0; 258 } 259 260 /* 261 * look for slave instances matching with the slave id of the given master. 262 * when found, relink the open_link of slaves. 263 * 264 * call this with register_mutex down. 265 */ 266 static int snd_timer_check_master(struct snd_timer_instance *master) 267 { 268 struct snd_timer_instance *slave, *tmp; 269 int err = 0; 270 271 /* check all pending slaves */ 272 list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) { 273 err = check_matching_master_slave(master, slave); 274 if (err < 0) 275 break; 276 } 277 return err < 0 ? err : 0; 278 } 279 280 static void snd_timer_close_locked(struct snd_timer_instance *timeri, 281 struct device **card_devp_to_put); 282 283 /* 284 * open a timer instance 285 * when opening a master, the slave id must be here given. 286 */ 287 int snd_timer_open(struct snd_timer_instance *timeri, 288 struct snd_timer_id *tid, 289 unsigned int slave_id) 290 { 291 struct snd_timer *timer; 292 struct device *card_dev_to_put = NULL; 293 int err; 294 295 mutex_lock(®ister_mutex); 296 if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) { 297 /* open a slave instance */ 298 if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE || 299 tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) { 300 pr_debug("ALSA: timer: invalid slave class %i\n", 301 tid->dev_sclass); 302 err = -EINVAL; 303 goto unlock; 304 } 305 if (num_slaves >= MAX_SLAVE_INSTANCES) { 306 err = -EBUSY; 307 goto unlock; 308 } 309 timeri->slave_class = tid->dev_sclass; 310 timeri->slave_id = tid->device; 311 timeri->flags |= SNDRV_TIMER_IFLG_SLAVE; 312 list_add_tail(&timeri->open_list, &snd_timer_slave_list); 313 num_slaves++; 314 err = snd_timer_check_slave(timeri); 315 goto list_added; 316 } 317 318 /* open a master instance */ 319 timer = snd_timer_find(tid); 320 #ifdef CONFIG_MODULES 321 if (!timer) { 322 mutex_unlock(®ister_mutex); 323 snd_timer_request(tid); 324 mutex_lock(®ister_mutex); 325 timer = snd_timer_find(tid); 326 } 327 #endif 328 if (!timer) { 329 err = -ENODEV; 330 goto unlock; 331 } 332 if (!list_empty(&timer->open_list_head)) { 333 struct snd_timer_instance *t = 334 list_entry(timer->open_list_head.next, 335 struct snd_timer_instance, open_list); 336 if (t->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) { 337 err = -EBUSY; 338 goto unlock; 339 } 340 } 341 if (timer->num_instances >= timer->max_instances) { 342 err = -EBUSY; 343 goto unlock; 344 } 345 if (!try_module_get(timer->module)) { 346 err = -EBUSY; 347 goto unlock; 348 } 349 /* take a card refcount for safe disconnection */ 350 if (timer->card) { 351 get_device(&timer->card->card_dev); 352 card_dev_to_put = &timer->card->card_dev; 353 } 354 355 if (list_empty(&timer->open_list_head) && timer->hw.open) { 356 err = timer->hw.open(timer); 357 if (err) { 358 module_put(timer->module); 359 goto unlock; 360 } 361 } 362 363 timeri->timer = timer; 364 timeri->slave_class = tid->dev_sclass; 365 timeri->slave_id = slave_id; 366 367 list_add_tail(&timeri->open_list, &timer->open_list_head); 368 timer->num_instances++; 369 err = snd_timer_check_master(timeri); 370 list_added: 371 if (err < 0) 372 snd_timer_close_locked(timeri, &card_dev_to_put); 373 374 unlock: 375 mutex_unlock(®ister_mutex); 376 /* put_device() is called after unlock for avoiding deadlock */ 377 if (err < 0 && card_dev_to_put) 378 put_device(card_dev_to_put); 379 return err; 380 } 381 EXPORT_SYMBOL(snd_timer_open); 382 383 /* remove slave links, called from snd_timer_close_locked() below */ 384 static void remove_slave_links(struct snd_timer_instance *timeri, 385 struct snd_timer *timer) 386 { 387 struct snd_timer_instance *slave, *tmp; 388 389 guard(spinlock_irq)(&slave_active_lock); 390 guard(spinlock)(&timer->lock); 391 timeri->timer = NULL; 392 list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head, open_list) { 393 list_move_tail(&slave->open_list, &snd_timer_slave_list); 394 timer->num_instances--; 395 slave->master = NULL; 396 slave->timer = NULL; 397 list_del_init(&slave->ack_list); 398 list_del_init(&slave->active_list); 399 } 400 } 401 402 /* 403 * close a timer instance 404 * call this with register_mutex down. 405 */ 406 static void snd_timer_close_locked(struct snd_timer_instance *timeri, 407 struct device **card_devp_to_put) 408 { 409 struct snd_timer *timer = timeri->timer; 410 411 if (timer) { 412 guard(spinlock_irq)(&timer->lock); 413 timeri->flags |= SNDRV_TIMER_IFLG_DEAD; 414 } 415 416 if (!list_empty(&timeri->open_list)) { 417 list_del_init(&timeri->open_list); 418 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) 419 num_slaves--; 420 } 421 422 /* force to stop the timer */ 423 snd_timer_stop(timeri); 424 425 if (timer) { 426 timer->num_instances--; 427 /* wait, until the active callback is finished */ 428 spin_lock_irq(&timer->lock); 429 while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) { 430 spin_unlock_irq(&timer->lock); 431 udelay(10); 432 spin_lock_irq(&timer->lock); 433 } 434 spin_unlock_irq(&timer->lock); 435 436 remove_slave_links(timeri, timer); 437 438 /* slave doesn't need to release timer resources below */ 439 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) 440 timer = NULL; 441 } 442 443 if (timer) { 444 if (list_empty(&timer->open_list_head) && timer->hw.close) 445 timer->hw.close(timer); 446 /* release a card refcount for safe disconnection */ 447 if (timer->card) 448 *card_devp_to_put = &timer->card->card_dev; 449 module_put(timer->module); 450 } 451 } 452 453 /* 454 * close a timer instance 455 */ 456 void snd_timer_close(struct snd_timer_instance *timeri) 457 { 458 struct device *card_dev_to_put = NULL; 459 460 if (snd_BUG_ON(!timeri)) 461 return; 462 463 scoped_guard(mutex, ®ister_mutex) 464 snd_timer_close_locked(timeri, &card_dev_to_put); 465 /* put_device() is called after unlock for avoiding deadlock */ 466 if (card_dev_to_put) 467 put_device(card_dev_to_put); 468 } 469 EXPORT_SYMBOL(snd_timer_close); 470 471 static unsigned long snd_timer_hw_resolution(struct snd_timer *timer) 472 { 473 if (timer->hw.c_resolution) 474 return timer->hw.c_resolution(timer); 475 else 476 return timer->hw.resolution; 477 } 478 479 unsigned long snd_timer_resolution(struct snd_timer_instance *timeri) 480 { 481 struct snd_timer * timer; 482 unsigned long ret = 0; 483 484 if (timeri == NULL) 485 return 0; 486 timer = timeri->timer; 487 if (timer) { 488 guard(spinlock_irqsave)(&timer->lock); 489 ret = snd_timer_hw_resolution(timer); 490 } 491 return ret; 492 } 493 EXPORT_SYMBOL(snd_timer_resolution); 494 495 static void snd_timer_notify1(struct snd_timer_instance *ti, int event) 496 { 497 struct snd_timer *timer = ti->timer; 498 unsigned long resolution = 0; 499 struct snd_timer_instance *ts; 500 struct timespec64 tstamp; 501 502 if (timer_tstamp_monotonic) 503 ktime_get_ts64(&tstamp); 504 else 505 ktime_get_real_ts64(&tstamp); 506 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START || 507 event > SNDRV_TIMER_EVENT_PAUSE)) 508 return; 509 if (timer && 510 (event == SNDRV_TIMER_EVENT_START || 511 event == SNDRV_TIMER_EVENT_CONTINUE)) 512 resolution = snd_timer_hw_resolution(timer); 513 if (ti->ccallback) 514 ti->ccallback(ti, event, &tstamp, resolution); 515 if (ti->flags & SNDRV_TIMER_IFLG_SLAVE) 516 return; 517 if (timer == NULL) 518 return; 519 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE) 520 return; 521 event += 10; /* convert to SNDRV_TIMER_EVENT_MXXX */ 522 list_for_each_entry(ts, &ti->slave_active_head, active_list) 523 if (ts->ccallback) 524 ts->ccallback(ts, event, &tstamp, resolution); 525 } 526 527 /* start/continue a master timer */ 528 static int snd_timer_start1(struct snd_timer_instance *timeri, 529 bool start, unsigned long ticks) 530 { 531 struct snd_timer *timer; 532 int result; 533 534 timer = timeri->timer; 535 if (!timer) 536 return -EINVAL; 537 538 guard(spinlock_irqsave)(&timer->lock); 539 if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) 540 return -EINVAL; 541 if (timer->card && timer->card->shutdown) 542 return -ENODEV; 543 if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING | 544 SNDRV_TIMER_IFLG_START)) 545 return -EBUSY; 546 547 if (start) 548 timeri->ticks = timeri->cticks = ticks; 549 else if (!timeri->cticks) 550 timeri->cticks = 1; 551 timeri->pticks = 0; 552 553 list_move_tail(&timeri->active_list, &timer->active_list_head); 554 if (timer->running) { 555 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE) 556 goto __start_now; 557 timer->flags |= SNDRV_TIMER_FLG_RESCHED; 558 timeri->flags |= SNDRV_TIMER_IFLG_START; 559 result = 1; /* delayed start */ 560 } else { 561 if (start) 562 timer->sticks = ticks; 563 timer->hw.start(timer); 564 __start_now: 565 timer->running++; 566 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING; 567 result = 0; 568 } 569 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START : 570 SNDRV_TIMER_EVENT_CONTINUE); 571 return result; 572 } 573 574 /* start/continue a slave timer */ 575 static int snd_timer_start_slave(struct snd_timer_instance *timeri, 576 bool start) 577 { 578 guard(spinlock_irqsave)(&slave_active_lock); 579 if (timeri->flags & SNDRV_TIMER_IFLG_DEAD) 580 return -EINVAL; 581 if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) 582 return -EBUSY; 583 timeri->flags |= SNDRV_TIMER_IFLG_RUNNING; 584 if (timeri->master && timeri->timer) { 585 guard(spinlock)(&timeri->timer->lock); 586 list_add_tail(&timeri->active_list, 587 &timeri->master->slave_active_head); 588 snd_timer_notify1(timeri, start ? SNDRV_TIMER_EVENT_START : 589 SNDRV_TIMER_EVENT_CONTINUE); 590 } 591 return 1; /* delayed start */ 592 } 593 594 /* stop/pause a master timer */ 595 static int snd_timer_stop1(struct snd_timer_instance *timeri, bool stop) 596 { 597 struct snd_timer *timer; 598 599 timer = timeri->timer; 600 if (!timer) 601 return -EINVAL; 602 guard(spinlock_irqsave)(&timer->lock); 603 list_del_init(&timeri->ack_list); 604 list_del_init(&timeri->active_list); 605 if (!(timeri->flags & (SNDRV_TIMER_IFLG_RUNNING | 606 SNDRV_TIMER_IFLG_START))) 607 return -EBUSY; 608 if (timer->card && timer->card->shutdown) 609 return 0; 610 if (stop) { 611 timeri->cticks = timeri->ticks; 612 timeri->pticks = 0; 613 } 614 if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) && 615 !(--timer->running)) { 616 timer->hw.stop(timer); 617 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) { 618 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED; 619 snd_timer_reschedule(timer, 0); 620 if (timer->flags & SNDRV_TIMER_FLG_CHANGE) { 621 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE; 622 timer->hw.start(timer); 623 } 624 } 625 } 626 timeri->flags &= ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START); 627 if (stop) 628 timeri->flags &= ~SNDRV_TIMER_IFLG_PAUSED; 629 else 630 timeri->flags |= SNDRV_TIMER_IFLG_PAUSED; 631 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP : 632 SNDRV_TIMER_EVENT_PAUSE); 633 return 0; 634 } 635 636 /* stop/pause a slave timer */ 637 static int snd_timer_stop_slave(struct snd_timer_instance *timeri, bool stop) 638 { 639 bool running; 640 641 guard(spinlock_irqsave)(&slave_active_lock); 642 running = timeri->flags & SNDRV_TIMER_IFLG_RUNNING; 643 timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING; 644 if (timeri->timer) { 645 guard(spinlock)(&timeri->timer->lock); 646 list_del_init(&timeri->ack_list); 647 list_del_init(&timeri->active_list); 648 if (running) 649 snd_timer_notify1(timeri, stop ? SNDRV_TIMER_EVENT_STOP : 650 SNDRV_TIMER_EVENT_PAUSE); 651 } 652 return running ? 0 : -EBUSY; 653 } 654 655 /* 656 * start the timer instance 657 */ 658 int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks) 659 { 660 if (timeri == NULL || ticks < 1) 661 return -EINVAL; 662 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) 663 return snd_timer_start_slave(timeri, true); 664 else 665 return snd_timer_start1(timeri, true, ticks); 666 } 667 EXPORT_SYMBOL(snd_timer_start); 668 669 /* 670 * stop the timer instance. 671 * 672 * do not call this from the timer callback! 673 */ 674 int snd_timer_stop(struct snd_timer_instance *timeri) 675 { 676 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) 677 return snd_timer_stop_slave(timeri, true); 678 else 679 return snd_timer_stop1(timeri, true); 680 } 681 EXPORT_SYMBOL(snd_timer_stop); 682 683 /* 684 * start again.. the tick is kept. 685 */ 686 int snd_timer_continue(struct snd_timer_instance *timeri) 687 { 688 /* timer can continue only after pause */ 689 if (!(timeri->flags & SNDRV_TIMER_IFLG_PAUSED)) 690 return -EINVAL; 691 692 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) 693 return snd_timer_start_slave(timeri, false); 694 else 695 return snd_timer_start1(timeri, false, 0); 696 } 697 EXPORT_SYMBOL(snd_timer_continue); 698 699 /* 700 * pause.. remember the ticks left 701 */ 702 int snd_timer_pause(struct snd_timer_instance * timeri) 703 { 704 if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) 705 return snd_timer_stop_slave(timeri, false); 706 else 707 return snd_timer_stop1(timeri, false); 708 } 709 EXPORT_SYMBOL(snd_timer_pause); 710 711 /* 712 * reschedule the timer 713 * 714 * start pending instances and check the scheduling ticks. 715 * when the scheduling ticks is changed set CHANGE flag to reprogram the timer. 716 */ 717 static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left) 718 { 719 struct snd_timer_instance *ti; 720 unsigned long ticks = ~0UL; 721 722 list_for_each_entry(ti, &timer->active_list_head, active_list) { 723 if (ti->flags & SNDRV_TIMER_IFLG_START) { 724 ti->flags &= ~SNDRV_TIMER_IFLG_START; 725 ti->flags |= SNDRV_TIMER_IFLG_RUNNING; 726 timer->running++; 727 } 728 if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) { 729 if (ticks > ti->cticks) 730 ticks = ti->cticks; 731 } 732 } 733 if (ticks == ~0UL) { 734 timer->flags &= ~SNDRV_TIMER_FLG_RESCHED; 735 return; 736 } 737 if (ticks > timer->hw.ticks) 738 ticks = timer->hw.ticks; 739 if (ticks_left != ticks) 740 timer->flags |= SNDRV_TIMER_FLG_CHANGE; 741 timer->sticks = ticks; 742 } 743 744 /* call callbacks in timer ack list */ 745 static void snd_timer_process_callbacks(struct snd_timer *timer, 746 struct list_head *head) 747 { 748 struct snd_timer_instance *ti; 749 unsigned long resolution, ticks; 750 751 while (!list_empty(head)) { 752 ti = list_first_entry(head, struct snd_timer_instance, 753 ack_list); 754 755 /* remove from ack_list and make empty */ 756 list_del_init(&ti->ack_list); 757 758 if (!(ti->flags & SNDRV_TIMER_IFLG_DEAD)) { 759 ticks = ti->pticks; 760 ti->pticks = 0; 761 resolution = ti->resolution; 762 ti->flags |= SNDRV_TIMER_IFLG_CALLBACK; 763 spin_unlock(&timer->lock); 764 if (ti->callback) 765 ti->callback(ti, resolution, ticks); 766 spin_lock(&timer->lock); 767 ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK; 768 } 769 } 770 } 771 772 /* clear pending instances from ack list */ 773 static void snd_timer_clear_callbacks(struct snd_timer *timer, 774 struct list_head *head) 775 { 776 guard(spinlock_irqsave)(&timer->lock); 777 while (!list_empty(head)) 778 list_del_init(head->next); 779 } 780 781 /* 782 * timer work 783 * 784 */ 785 static void snd_timer_work(struct work_struct *work) 786 { 787 struct snd_timer *timer = container_of(work, struct snd_timer, task_work); 788 789 if (timer->card && timer->card->shutdown) { 790 snd_timer_clear_callbacks(timer, &timer->sack_list_head); 791 return; 792 } 793 794 guard(spinlock_irqsave)(&timer->lock); 795 snd_timer_process_callbacks(timer, &timer->sack_list_head); 796 } 797 798 /* 799 * timer interrupt 800 * 801 * ticks_left is usually equal to timer->sticks. 802 * 803 */ 804 void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left) 805 { 806 struct snd_timer_instance *ti, *ts, *tmp; 807 unsigned long resolution; 808 struct list_head *ack_list_head; 809 810 if (timer == NULL) 811 return; 812 813 if (timer->card && timer->card->shutdown) { 814 snd_timer_clear_callbacks(timer, &timer->ack_list_head); 815 return; 816 } 817 818 guard(spinlock_irqsave)(&timer->lock); 819 820 /* remember the current resolution */ 821 resolution = snd_timer_hw_resolution(timer); 822 823 /* loop for all active instances 824 * Here we cannot use list_for_each_entry because the active_list of a 825 * processed instance is relinked to done_list_head before the callback 826 * is called. 827 */ 828 list_for_each_entry_safe(ti, tmp, &timer->active_list_head, 829 active_list) { 830 if (ti->flags & SNDRV_TIMER_IFLG_DEAD) 831 continue; 832 if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING)) 833 continue; 834 ti->pticks += ticks_left; 835 ti->resolution = resolution; 836 if (ti->cticks < ticks_left) 837 ti->cticks = 0; 838 else 839 ti->cticks -= ticks_left; 840 if (ti->cticks) /* not expired */ 841 continue; 842 if (ti->flags & SNDRV_TIMER_IFLG_AUTO) { 843 ti->cticks = ti->ticks; 844 } else { 845 ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING; 846 --timer->running; 847 list_del_init(&ti->active_list); 848 } 849 if ((timer->hw.flags & SNDRV_TIMER_HW_WORK) || 850 (ti->flags & SNDRV_TIMER_IFLG_FAST)) 851 ack_list_head = &timer->ack_list_head; 852 else 853 ack_list_head = &timer->sack_list_head; 854 if (list_empty(&ti->ack_list)) 855 list_add_tail(&ti->ack_list, ack_list_head); 856 list_for_each_entry(ts, &ti->slave_active_head, active_list) { 857 ts->pticks = ti->pticks; 858 ts->resolution = resolution; 859 if (list_empty(&ts->ack_list)) 860 list_add_tail(&ts->ack_list, ack_list_head); 861 } 862 } 863 if (timer->flags & SNDRV_TIMER_FLG_RESCHED) 864 snd_timer_reschedule(timer, timer->sticks); 865 if (timer->running) { 866 if (timer->hw.flags & SNDRV_TIMER_HW_STOP) { 867 timer->hw.stop(timer); 868 timer->flags |= SNDRV_TIMER_FLG_CHANGE; 869 } 870 if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) || 871 (timer->flags & SNDRV_TIMER_FLG_CHANGE)) { 872 /* restart timer */ 873 timer->flags &= ~SNDRV_TIMER_FLG_CHANGE; 874 timer->hw.start(timer); 875 } 876 } else { 877 timer->hw.stop(timer); 878 } 879 880 /* now process all fast callbacks */ 881 snd_timer_process_callbacks(timer, &timer->ack_list_head); 882 883 /* do we have any slow callbacks? */ 884 if (!list_empty(&timer->sack_list_head)) 885 queue_work(system_highpri_wq, &timer->task_work); 886 } 887 EXPORT_SYMBOL(snd_timer_interrupt); 888 889 /* 890 891 */ 892 893 int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid, 894 struct snd_timer **rtimer) 895 { 896 struct snd_timer *timer; 897 int err; 898 static const struct snd_device_ops ops = { 899 .dev_free = snd_timer_dev_free, 900 .dev_register = snd_timer_dev_register, 901 .dev_disconnect = snd_timer_dev_disconnect, 902 }; 903 904 if (snd_BUG_ON(!tid)) 905 return -EINVAL; 906 if (tid->dev_class == SNDRV_TIMER_CLASS_CARD || 907 tid->dev_class == SNDRV_TIMER_CLASS_PCM) { 908 if (WARN_ON(!card)) 909 return -EINVAL; 910 } 911 if (rtimer) 912 *rtimer = NULL; 913 timer = kzalloc(sizeof(*timer), GFP_KERNEL); 914 if (!timer) 915 return -ENOMEM; 916 timer->tmr_class = tid->dev_class; 917 timer->card = card; 918 timer->tmr_device = tid->device; 919 timer->tmr_subdevice = tid->subdevice; 920 if (id) 921 strscpy(timer->id, id, sizeof(timer->id)); 922 timer->sticks = 1; 923 INIT_LIST_HEAD(&timer->device_list); 924 INIT_LIST_HEAD(&timer->open_list_head); 925 INIT_LIST_HEAD(&timer->active_list_head); 926 INIT_LIST_HEAD(&timer->ack_list_head); 927 INIT_LIST_HEAD(&timer->sack_list_head); 928 spin_lock_init(&timer->lock); 929 INIT_WORK(&timer->task_work, snd_timer_work); 930 timer->max_instances = 1000; /* default limit per timer */ 931 if (card != NULL) { 932 timer->module = card->module; 933 err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops); 934 if (err < 0) { 935 snd_timer_free(timer); 936 return err; 937 } 938 } 939 if (rtimer) 940 *rtimer = timer; 941 return 0; 942 } 943 EXPORT_SYMBOL(snd_timer_new); 944 945 static int snd_timer_free(struct snd_timer *timer) 946 { 947 if (!timer) 948 return 0; 949 950 guard(mutex)(®ister_mutex); 951 if (! list_empty(&timer->open_list_head)) { 952 struct list_head *p, *n; 953 struct snd_timer_instance *ti; 954 pr_warn("ALSA: timer %p is busy?\n", timer); 955 list_for_each_safe(p, n, &timer->open_list_head) { 956 list_del_init(p); 957 ti = list_entry(p, struct snd_timer_instance, open_list); 958 ti->timer = NULL; 959 } 960 } 961 list_del(&timer->device_list); 962 963 if (timer->private_free) 964 timer->private_free(timer); 965 kfree(timer); 966 return 0; 967 } 968 969 static int snd_timer_dev_free(struct snd_device *device) 970 { 971 struct snd_timer *timer = device->device_data; 972 return snd_timer_free(timer); 973 } 974 975 static int snd_timer_dev_register(struct snd_device *dev) 976 { 977 struct snd_timer *timer = dev->device_data; 978 struct snd_timer *timer1; 979 980 if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop)) 981 return -ENXIO; 982 if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) && 983 !timer->hw.resolution && timer->hw.c_resolution == NULL) 984 return -EINVAL; 985 986 guard(mutex)(®ister_mutex); 987 list_for_each_entry(timer1, &snd_timer_list, device_list) { 988 if (timer1->tmr_class > timer->tmr_class) 989 break; 990 if (timer1->tmr_class < timer->tmr_class) 991 continue; 992 if (timer1->card && timer->card) { 993 if (timer1->card->number > timer->card->number) 994 break; 995 if (timer1->card->number < timer->card->number) 996 continue; 997 } 998 if (timer1->tmr_device > timer->tmr_device) 999 break; 1000 if (timer1->tmr_device < timer->tmr_device) 1001 continue; 1002 if (timer1->tmr_subdevice > timer->tmr_subdevice) 1003 break; 1004 if (timer1->tmr_subdevice < timer->tmr_subdevice) 1005 continue; 1006 /* conflicts.. */ 1007 return -EBUSY; 1008 } 1009 list_add_tail(&timer->device_list, &timer1->device_list); 1010 return 0; 1011 } 1012 1013 static int snd_timer_dev_disconnect(struct snd_device *device) 1014 { 1015 struct snd_timer *timer = device->device_data; 1016 struct snd_timer_instance *ti; 1017 1018 guard(mutex)(®ister_mutex); 1019 list_del_init(&timer->device_list); 1020 /* wake up pending sleepers */ 1021 list_for_each_entry(ti, &timer->open_list_head, open_list) { 1022 if (ti->disconnect) 1023 ti->disconnect(ti); 1024 } 1025 return 0; 1026 } 1027 1028 void snd_timer_notify(struct snd_timer *timer, int event, struct timespec64 *tstamp) 1029 { 1030 unsigned long resolution = 0; 1031 struct snd_timer_instance *ti, *ts; 1032 1033 if (timer->card && timer->card->shutdown) 1034 return; 1035 if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)) 1036 return; 1037 if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART || 1038 event > SNDRV_TIMER_EVENT_MRESUME)) 1039 return; 1040 guard(spinlock_irqsave)(&timer->lock); 1041 if (event == SNDRV_TIMER_EVENT_MSTART || 1042 event == SNDRV_TIMER_EVENT_MCONTINUE || 1043 event == SNDRV_TIMER_EVENT_MRESUME) 1044 resolution = snd_timer_hw_resolution(timer); 1045 list_for_each_entry(ti, &timer->active_list_head, active_list) { 1046 if (ti->ccallback) 1047 ti->ccallback(ti, event, tstamp, resolution); 1048 list_for_each_entry(ts, &ti->slave_active_head, active_list) 1049 if (ts->ccallback) 1050 ts->ccallback(ts, event, tstamp, resolution); 1051 } 1052 } 1053 EXPORT_SYMBOL(snd_timer_notify); 1054 1055 /* 1056 * exported functions for global timers 1057 */ 1058 int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer) 1059 { 1060 struct snd_timer_id tid; 1061 1062 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL; 1063 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE; 1064 tid.card = -1; 1065 tid.device = device; 1066 tid.subdevice = 0; 1067 return snd_timer_new(NULL, id, &tid, rtimer); 1068 } 1069 EXPORT_SYMBOL(snd_timer_global_new); 1070 1071 int snd_timer_global_free(struct snd_timer *timer) 1072 { 1073 return snd_timer_free(timer); 1074 } 1075 EXPORT_SYMBOL(snd_timer_global_free); 1076 1077 int snd_timer_global_register(struct snd_timer *timer) 1078 { 1079 struct snd_device dev; 1080 1081 memset(&dev, 0, sizeof(dev)); 1082 dev.device_data = timer; 1083 return snd_timer_dev_register(&dev); 1084 } 1085 EXPORT_SYMBOL(snd_timer_global_register); 1086 1087 /* 1088 * System timer 1089 */ 1090 1091 struct snd_timer_system_private { 1092 struct timer_list tlist; 1093 struct snd_timer *snd_timer; 1094 unsigned long last_expires; 1095 unsigned long last_jiffies; 1096 unsigned long correction; 1097 }; 1098 1099 static void snd_timer_s_function(struct timer_list *t) 1100 { 1101 struct snd_timer_system_private *priv = from_timer(priv, t, 1102 tlist); 1103 struct snd_timer *timer = priv->snd_timer; 1104 unsigned long jiff = jiffies; 1105 if (time_after(jiff, priv->last_expires)) 1106 priv->correction += (long)jiff - (long)priv->last_expires; 1107 snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies); 1108 } 1109 1110 static int snd_timer_s_start(struct snd_timer * timer) 1111 { 1112 struct snd_timer_system_private *priv; 1113 unsigned long njiff; 1114 1115 priv = (struct snd_timer_system_private *) timer->private_data; 1116 njiff = (priv->last_jiffies = jiffies); 1117 if (priv->correction > timer->sticks - 1) { 1118 priv->correction -= timer->sticks - 1; 1119 njiff++; 1120 } else { 1121 njiff += timer->sticks - priv->correction; 1122 priv->correction = 0; 1123 } 1124 priv->last_expires = njiff; 1125 mod_timer(&priv->tlist, njiff); 1126 return 0; 1127 } 1128 1129 static int snd_timer_s_stop(struct snd_timer * timer) 1130 { 1131 struct snd_timer_system_private *priv; 1132 unsigned long jiff; 1133 1134 priv = (struct snd_timer_system_private *) timer->private_data; 1135 del_timer(&priv->tlist); 1136 jiff = jiffies; 1137 if (time_before(jiff, priv->last_expires)) 1138 timer->sticks = priv->last_expires - jiff; 1139 else 1140 timer->sticks = 1; 1141 priv->correction = 0; 1142 return 0; 1143 } 1144 1145 static int snd_timer_s_close(struct snd_timer *timer) 1146 { 1147 struct snd_timer_system_private *priv; 1148 1149 priv = (struct snd_timer_system_private *)timer->private_data; 1150 del_timer_sync(&priv->tlist); 1151 return 0; 1152 } 1153 1154 static const struct snd_timer_hardware snd_timer_system = 1155 { 1156 .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_WORK, 1157 .resolution = 1000000000L / HZ, 1158 .ticks = 10000000L, 1159 .close = snd_timer_s_close, 1160 .start = snd_timer_s_start, 1161 .stop = snd_timer_s_stop 1162 }; 1163 1164 static void snd_timer_free_system(struct snd_timer *timer) 1165 { 1166 kfree(timer->private_data); 1167 } 1168 1169 static int snd_timer_register_system(void) 1170 { 1171 struct snd_timer *timer; 1172 struct snd_timer_system_private *priv; 1173 int err; 1174 1175 err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer); 1176 if (err < 0) 1177 return err; 1178 strcpy(timer->name, "system timer"); 1179 timer->hw = snd_timer_system; 1180 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 1181 if (priv == NULL) { 1182 snd_timer_free(timer); 1183 return -ENOMEM; 1184 } 1185 priv->snd_timer = timer; 1186 timer_setup(&priv->tlist, snd_timer_s_function, 0); 1187 timer->private_data = priv; 1188 timer->private_free = snd_timer_free_system; 1189 return snd_timer_global_register(timer); 1190 } 1191 1192 #ifdef CONFIG_SND_PROC_FS 1193 /* 1194 * Info interface 1195 */ 1196 1197 static void snd_timer_proc_read(struct snd_info_entry *entry, 1198 struct snd_info_buffer *buffer) 1199 { 1200 struct snd_timer *timer; 1201 struct snd_timer_instance *ti; 1202 unsigned long resolution; 1203 1204 guard(mutex)(®ister_mutex); 1205 list_for_each_entry(timer, &snd_timer_list, device_list) { 1206 if (timer->card && timer->card->shutdown) 1207 continue; 1208 switch (timer->tmr_class) { 1209 case SNDRV_TIMER_CLASS_GLOBAL: 1210 snd_iprintf(buffer, "G%i: ", timer->tmr_device); 1211 break; 1212 case SNDRV_TIMER_CLASS_CARD: 1213 snd_iprintf(buffer, "C%i-%i: ", 1214 timer->card->number, timer->tmr_device); 1215 break; 1216 case SNDRV_TIMER_CLASS_PCM: 1217 snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number, 1218 timer->tmr_device, timer->tmr_subdevice); 1219 break; 1220 default: 1221 snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class, 1222 timer->card ? timer->card->number : -1, 1223 timer->tmr_device, timer->tmr_subdevice); 1224 } 1225 snd_iprintf(buffer, "%s :", timer->name); 1226 scoped_guard(spinlock_irq, &timer->lock) 1227 resolution = snd_timer_hw_resolution(timer); 1228 if (resolution) 1229 snd_iprintf(buffer, " %lu.%03luus (%lu ticks)", 1230 resolution / 1000, 1231 resolution % 1000, 1232 timer->hw.ticks); 1233 if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE) 1234 snd_iprintf(buffer, " SLAVE"); 1235 snd_iprintf(buffer, "\n"); 1236 list_for_each_entry(ti, &timer->open_list_head, open_list) 1237 snd_iprintf(buffer, " Client %s : %s\n", 1238 ti->owner ? ti->owner : "unknown", 1239 (ti->flags & (SNDRV_TIMER_IFLG_START | 1240 SNDRV_TIMER_IFLG_RUNNING)) 1241 ? "running" : "stopped"); 1242 } 1243 } 1244 1245 static struct snd_info_entry *snd_timer_proc_entry; 1246 1247 static void __init snd_timer_proc_init(void) 1248 { 1249 struct snd_info_entry *entry; 1250 1251 entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL); 1252 if (entry != NULL) { 1253 entry->c.text.read = snd_timer_proc_read; 1254 if (snd_info_register(entry) < 0) { 1255 snd_info_free_entry(entry); 1256 entry = NULL; 1257 } 1258 } 1259 snd_timer_proc_entry = entry; 1260 } 1261 1262 static void __exit snd_timer_proc_done(void) 1263 { 1264 snd_info_free_entry(snd_timer_proc_entry); 1265 } 1266 #else /* !CONFIG_SND_PROC_FS */ 1267 #define snd_timer_proc_init() 1268 #define snd_timer_proc_done() 1269 #endif 1270 1271 /* 1272 * USER SPACE interface 1273 */ 1274 1275 static void snd_timer_user_interrupt(struct snd_timer_instance *timeri, 1276 unsigned long resolution, 1277 unsigned long ticks) 1278 { 1279 struct snd_timer_user *tu = timeri->callback_data; 1280 struct snd_timer_read *r; 1281 int prev; 1282 1283 guard(spinlock)(&tu->qlock); 1284 if (tu->qused > 0) { 1285 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1; 1286 r = &tu->queue[prev]; 1287 if (r->resolution == resolution) { 1288 r->ticks += ticks; 1289 goto __wake; 1290 } 1291 } 1292 if (tu->qused >= tu->queue_size) { 1293 tu->overrun++; 1294 } else { 1295 r = &tu->queue[tu->qtail++]; 1296 tu->qtail %= tu->queue_size; 1297 r->resolution = resolution; 1298 r->ticks = ticks; 1299 tu->qused++; 1300 } 1301 __wake: 1302 snd_kill_fasync(tu->fasync, SIGIO, POLL_IN); 1303 wake_up(&tu->qchange_sleep); 1304 } 1305 1306 static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu, 1307 struct snd_timer_tread64 *tread) 1308 { 1309 if (tu->qused >= tu->queue_size) { 1310 tu->overrun++; 1311 } else { 1312 memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread)); 1313 tu->qtail %= tu->queue_size; 1314 tu->qused++; 1315 } 1316 } 1317 1318 static void snd_timer_user_ccallback(struct snd_timer_instance *timeri, 1319 int event, 1320 struct timespec64 *tstamp, 1321 unsigned long resolution) 1322 { 1323 struct snd_timer_user *tu = timeri->callback_data; 1324 struct snd_timer_tread64 r1; 1325 1326 if (event >= SNDRV_TIMER_EVENT_START && 1327 event <= SNDRV_TIMER_EVENT_PAUSE) 1328 tu->tstamp = *tstamp; 1329 if ((tu->filter & (1 << event)) == 0 || !tu->tread) 1330 return; 1331 memset(&r1, 0, sizeof(r1)); 1332 r1.event = event; 1333 r1.tstamp_sec = tstamp->tv_sec; 1334 r1.tstamp_nsec = tstamp->tv_nsec; 1335 r1.val = resolution; 1336 scoped_guard(spinlock_irqsave, &tu->qlock) 1337 snd_timer_user_append_to_tqueue(tu, &r1); 1338 snd_kill_fasync(tu->fasync, SIGIO, POLL_IN); 1339 wake_up(&tu->qchange_sleep); 1340 } 1341 1342 static void snd_timer_user_disconnect(struct snd_timer_instance *timeri) 1343 { 1344 struct snd_timer_user *tu = timeri->callback_data; 1345 1346 tu->disconnected = true; 1347 wake_up(&tu->qchange_sleep); 1348 } 1349 1350 static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri, 1351 unsigned long resolution, 1352 unsigned long ticks) 1353 { 1354 struct snd_timer_user *tu = timeri->callback_data; 1355 struct snd_timer_tread64 *r, r1; 1356 struct timespec64 tstamp; 1357 int prev, append = 0; 1358 1359 memset(&r1, 0, sizeof(r1)); 1360 memset(&tstamp, 0, sizeof(tstamp)); 1361 scoped_guard(spinlock, &tu->qlock) { 1362 if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) | 1363 (1 << SNDRV_TIMER_EVENT_TICK))) == 0) 1364 return; 1365 if (tu->last_resolution != resolution || ticks > 0) { 1366 if (timer_tstamp_monotonic) 1367 ktime_get_ts64(&tstamp); 1368 else 1369 ktime_get_real_ts64(&tstamp); 1370 } 1371 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) && 1372 tu->last_resolution != resolution) { 1373 r1.event = SNDRV_TIMER_EVENT_RESOLUTION; 1374 r1.tstamp_sec = tstamp.tv_sec; 1375 r1.tstamp_nsec = tstamp.tv_nsec; 1376 r1.val = resolution; 1377 snd_timer_user_append_to_tqueue(tu, &r1); 1378 tu->last_resolution = resolution; 1379 append++; 1380 } 1381 if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0) 1382 break; 1383 if (ticks == 0) 1384 break; 1385 if (tu->qused > 0) { 1386 prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1; 1387 r = &tu->tqueue[prev]; 1388 if (r->event == SNDRV_TIMER_EVENT_TICK) { 1389 r->tstamp_sec = tstamp.tv_sec; 1390 r->tstamp_nsec = tstamp.tv_nsec; 1391 r->val += ticks; 1392 append++; 1393 break; 1394 } 1395 } 1396 r1.event = SNDRV_TIMER_EVENT_TICK; 1397 r1.tstamp_sec = tstamp.tv_sec; 1398 r1.tstamp_nsec = tstamp.tv_nsec; 1399 r1.val = ticks; 1400 snd_timer_user_append_to_tqueue(tu, &r1); 1401 append++; 1402 } 1403 if (append == 0) 1404 return; 1405 snd_kill_fasync(tu->fasync, SIGIO, POLL_IN); 1406 wake_up(&tu->qchange_sleep); 1407 } 1408 1409 static int realloc_user_queue(struct snd_timer_user *tu, int size) 1410 { 1411 struct snd_timer_read *queue = NULL; 1412 struct snd_timer_tread64 *tqueue = NULL; 1413 1414 if (tu->tread) { 1415 tqueue = kcalloc(size, sizeof(*tqueue), GFP_KERNEL); 1416 if (!tqueue) 1417 return -ENOMEM; 1418 } else { 1419 queue = kcalloc(size, sizeof(*queue), GFP_KERNEL); 1420 if (!queue) 1421 return -ENOMEM; 1422 } 1423 1424 guard(spinlock_irq)(&tu->qlock); 1425 kfree(tu->queue); 1426 kfree(tu->tqueue); 1427 tu->queue_size = size; 1428 tu->queue = queue; 1429 tu->tqueue = tqueue; 1430 tu->qhead = tu->qtail = tu->qused = 0; 1431 1432 return 0; 1433 } 1434 1435 static int snd_timer_user_open(struct inode *inode, struct file *file) 1436 { 1437 struct snd_timer_user *tu; 1438 int err; 1439 1440 err = stream_open(inode, file); 1441 if (err < 0) 1442 return err; 1443 1444 tu = kzalloc(sizeof(*tu), GFP_KERNEL); 1445 if (tu == NULL) 1446 return -ENOMEM; 1447 spin_lock_init(&tu->qlock); 1448 init_waitqueue_head(&tu->qchange_sleep); 1449 mutex_init(&tu->ioctl_lock); 1450 tu->ticks = 1; 1451 if (realloc_user_queue(tu, 128) < 0) { 1452 kfree(tu); 1453 return -ENOMEM; 1454 } 1455 file->private_data = tu; 1456 return 0; 1457 } 1458 1459 static int snd_timer_user_release(struct inode *inode, struct file *file) 1460 { 1461 struct snd_timer_user *tu; 1462 1463 if (file->private_data) { 1464 tu = file->private_data; 1465 file->private_data = NULL; 1466 scoped_guard(mutex, &tu->ioctl_lock) { 1467 if (tu->timeri) { 1468 snd_timer_close(tu->timeri); 1469 snd_timer_instance_free(tu->timeri); 1470 } 1471 } 1472 snd_fasync_free(tu->fasync); 1473 kfree(tu->queue); 1474 kfree(tu->tqueue); 1475 kfree(tu); 1476 } 1477 return 0; 1478 } 1479 1480 static void snd_timer_user_zero_id(struct snd_timer_id *id) 1481 { 1482 id->dev_class = SNDRV_TIMER_CLASS_NONE; 1483 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE; 1484 id->card = -1; 1485 id->device = -1; 1486 id->subdevice = -1; 1487 } 1488 1489 static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer) 1490 { 1491 id->dev_class = timer->tmr_class; 1492 id->dev_sclass = SNDRV_TIMER_SCLASS_NONE; 1493 id->card = timer->card ? timer->card->number : -1; 1494 id->device = timer->tmr_device; 1495 id->subdevice = timer->tmr_subdevice; 1496 } 1497 1498 static int snd_timer_user_next_device(struct snd_timer_id __user *_tid) 1499 { 1500 struct snd_timer_id id; 1501 struct snd_timer *timer; 1502 struct list_head *p; 1503 1504 if (copy_from_user(&id, _tid, sizeof(id))) 1505 return -EFAULT; 1506 guard(mutex)(®ister_mutex); 1507 if (id.dev_class < 0) { /* first item */ 1508 if (list_empty(&snd_timer_list)) 1509 snd_timer_user_zero_id(&id); 1510 else { 1511 timer = list_entry(snd_timer_list.next, 1512 struct snd_timer, device_list); 1513 snd_timer_user_copy_id(&id, timer); 1514 } 1515 } else { 1516 switch (id.dev_class) { 1517 case SNDRV_TIMER_CLASS_GLOBAL: 1518 id.device = id.device < 0 ? 0 : id.device + 1; 1519 list_for_each(p, &snd_timer_list) { 1520 timer = list_entry(p, struct snd_timer, device_list); 1521 if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) { 1522 snd_timer_user_copy_id(&id, timer); 1523 break; 1524 } 1525 if (timer->tmr_device >= id.device) { 1526 snd_timer_user_copy_id(&id, timer); 1527 break; 1528 } 1529 } 1530 if (p == &snd_timer_list) 1531 snd_timer_user_zero_id(&id); 1532 break; 1533 case SNDRV_TIMER_CLASS_CARD: 1534 case SNDRV_TIMER_CLASS_PCM: 1535 if (id.card < 0) { 1536 id.card = 0; 1537 } else { 1538 if (id.device < 0) { 1539 id.device = 0; 1540 } else { 1541 if (id.subdevice < 0) 1542 id.subdevice = 0; 1543 else if (id.subdevice < INT_MAX) 1544 id.subdevice++; 1545 } 1546 } 1547 list_for_each(p, &snd_timer_list) { 1548 timer = list_entry(p, struct snd_timer, device_list); 1549 if (timer->tmr_class > id.dev_class) { 1550 snd_timer_user_copy_id(&id, timer); 1551 break; 1552 } 1553 if (timer->tmr_class < id.dev_class) 1554 continue; 1555 if (timer->card->number > id.card) { 1556 snd_timer_user_copy_id(&id, timer); 1557 break; 1558 } 1559 if (timer->card->number < id.card) 1560 continue; 1561 if (timer->tmr_device > id.device) { 1562 snd_timer_user_copy_id(&id, timer); 1563 break; 1564 } 1565 if (timer->tmr_device < id.device) 1566 continue; 1567 if (timer->tmr_subdevice > id.subdevice) { 1568 snd_timer_user_copy_id(&id, timer); 1569 break; 1570 } 1571 if (timer->tmr_subdevice < id.subdevice) 1572 continue; 1573 snd_timer_user_copy_id(&id, timer); 1574 break; 1575 } 1576 if (p == &snd_timer_list) 1577 snd_timer_user_zero_id(&id); 1578 break; 1579 default: 1580 snd_timer_user_zero_id(&id); 1581 } 1582 } 1583 if (copy_to_user(_tid, &id, sizeof(*_tid))) 1584 return -EFAULT; 1585 return 0; 1586 } 1587 1588 static int snd_timer_user_ginfo(struct file *file, 1589 struct snd_timer_ginfo __user *_ginfo) 1590 { 1591 struct snd_timer_ginfo *ginfo __free(kfree) = NULL; 1592 struct snd_timer_id tid; 1593 struct snd_timer *t; 1594 struct list_head *p; 1595 1596 ginfo = memdup_user(_ginfo, sizeof(*ginfo)); 1597 if (IS_ERR(ginfo)) 1598 return PTR_ERR(no_free_ptr(ginfo)); 1599 1600 tid = ginfo->tid; 1601 memset(ginfo, 0, sizeof(*ginfo)); 1602 ginfo->tid = tid; 1603 guard(mutex)(®ister_mutex); 1604 t = snd_timer_find(&tid); 1605 if (!t) 1606 return -ENODEV; 1607 ginfo->card = t->card ? t->card->number : -1; 1608 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE) 1609 ginfo->flags |= SNDRV_TIMER_FLG_SLAVE; 1610 strscpy(ginfo->id, t->id, sizeof(ginfo->id)); 1611 strscpy(ginfo->name, t->name, sizeof(ginfo->name)); 1612 scoped_guard(spinlock_irq, &t->lock) 1613 ginfo->resolution = snd_timer_hw_resolution(t); 1614 if (t->hw.resolution_min > 0) { 1615 ginfo->resolution_min = t->hw.resolution_min; 1616 ginfo->resolution_max = t->hw.resolution_max; 1617 } 1618 list_for_each(p, &t->open_list_head) { 1619 ginfo->clients++; 1620 } 1621 if (copy_to_user(_ginfo, ginfo, sizeof(*ginfo))) 1622 return -EFAULT; 1623 return 0; 1624 } 1625 1626 static int timer_set_gparams(struct snd_timer_gparams *gparams) 1627 { 1628 struct snd_timer *t; 1629 1630 guard(mutex)(®ister_mutex); 1631 t = snd_timer_find(&gparams->tid); 1632 if (!t) 1633 return -ENODEV; 1634 if (!list_empty(&t->open_list_head)) 1635 return -EBUSY; 1636 if (!t->hw.set_period) 1637 return -ENOSYS; 1638 return t->hw.set_period(t, gparams->period_num, gparams->period_den); 1639 } 1640 1641 static int snd_timer_user_gparams(struct file *file, 1642 struct snd_timer_gparams __user *_gparams) 1643 { 1644 struct snd_timer_gparams gparams; 1645 1646 if (copy_from_user(&gparams, _gparams, sizeof(gparams))) 1647 return -EFAULT; 1648 return timer_set_gparams(&gparams); 1649 } 1650 1651 static int snd_timer_user_gstatus(struct file *file, 1652 struct snd_timer_gstatus __user *_gstatus) 1653 { 1654 struct snd_timer_gstatus gstatus; 1655 struct snd_timer_id tid; 1656 struct snd_timer *t; 1657 int err = 0; 1658 1659 if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus))) 1660 return -EFAULT; 1661 tid = gstatus.tid; 1662 memset(&gstatus, 0, sizeof(gstatus)); 1663 gstatus.tid = tid; 1664 guard(mutex)(®ister_mutex); 1665 t = snd_timer_find(&tid); 1666 if (t != NULL) { 1667 guard(spinlock_irq)(&t->lock); 1668 gstatus.resolution = snd_timer_hw_resolution(t); 1669 if (t->hw.precise_resolution) { 1670 t->hw.precise_resolution(t, &gstatus.resolution_num, 1671 &gstatus.resolution_den); 1672 } else { 1673 gstatus.resolution_num = gstatus.resolution; 1674 gstatus.resolution_den = 1000000000uL; 1675 } 1676 } else { 1677 err = -ENODEV; 1678 } 1679 if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus))) 1680 err = -EFAULT; 1681 return err; 1682 } 1683 1684 static int snd_timer_user_tselect(struct file *file, 1685 struct snd_timer_select __user *_tselect) 1686 { 1687 struct snd_timer_user *tu; 1688 struct snd_timer_select tselect; 1689 char str[32]; 1690 int err = 0; 1691 1692 tu = file->private_data; 1693 if (tu->timeri) { 1694 snd_timer_close(tu->timeri); 1695 snd_timer_instance_free(tu->timeri); 1696 tu->timeri = NULL; 1697 } 1698 if (copy_from_user(&tselect, _tselect, sizeof(tselect))) { 1699 err = -EFAULT; 1700 goto __err; 1701 } 1702 sprintf(str, "application %i", current->pid); 1703 if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE) 1704 tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION; 1705 tu->timeri = snd_timer_instance_new(str); 1706 if (!tu->timeri) { 1707 err = -ENOMEM; 1708 goto __err; 1709 } 1710 1711 tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST; 1712 tu->timeri->callback = tu->tread 1713 ? snd_timer_user_tinterrupt : snd_timer_user_interrupt; 1714 tu->timeri->ccallback = snd_timer_user_ccallback; 1715 tu->timeri->callback_data = (void *)tu; 1716 tu->timeri->disconnect = snd_timer_user_disconnect; 1717 1718 err = snd_timer_open(tu->timeri, &tselect.id, current->pid); 1719 if (err < 0) { 1720 snd_timer_instance_free(tu->timeri); 1721 tu->timeri = NULL; 1722 } 1723 1724 __err: 1725 return err; 1726 } 1727 1728 static int snd_timer_user_info(struct file *file, 1729 struct snd_timer_info __user *_info) 1730 { 1731 struct snd_timer_user *tu; 1732 struct snd_timer_info *info __free(kfree) = NULL; 1733 struct snd_timer *t; 1734 1735 tu = file->private_data; 1736 if (!tu->timeri) 1737 return -EBADFD; 1738 t = tu->timeri->timer; 1739 if (!t) 1740 return -EBADFD; 1741 1742 info = kzalloc(sizeof(*info), GFP_KERNEL); 1743 if (! info) 1744 return -ENOMEM; 1745 info->card = t->card ? t->card->number : -1; 1746 if (t->hw.flags & SNDRV_TIMER_HW_SLAVE) 1747 info->flags |= SNDRV_TIMER_FLG_SLAVE; 1748 strscpy(info->id, t->id, sizeof(info->id)); 1749 strscpy(info->name, t->name, sizeof(info->name)); 1750 scoped_guard(spinlock_irq, &t->lock) 1751 info->resolution = snd_timer_hw_resolution(t); 1752 if (copy_to_user(_info, info, sizeof(*_info))) 1753 return -EFAULT; 1754 return 0; 1755 } 1756 1757 static int snd_timer_user_params(struct file *file, 1758 struct snd_timer_params __user *_params) 1759 { 1760 struct snd_timer_user *tu; 1761 struct snd_timer_params params; 1762 struct snd_timer *t; 1763 int err; 1764 1765 tu = file->private_data; 1766 if (!tu->timeri) 1767 return -EBADFD; 1768 t = tu->timeri->timer; 1769 if (!t) 1770 return -EBADFD; 1771 if (copy_from_user(¶ms, _params, sizeof(params))) 1772 return -EFAULT; 1773 if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE)) { 1774 u64 resolution; 1775 1776 if (params.ticks < 1) { 1777 err = -EINVAL; 1778 goto _end; 1779 } 1780 1781 /* Don't allow resolution less than 1ms */ 1782 resolution = snd_timer_resolution(tu->timeri); 1783 resolution *= params.ticks; 1784 if (resolution < 1000000) { 1785 err = -EINVAL; 1786 goto _end; 1787 } 1788 } 1789 if (params.queue_size > 0 && 1790 (params.queue_size < 32 || params.queue_size > 1024)) { 1791 err = -EINVAL; 1792 goto _end; 1793 } 1794 if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)| 1795 (1<<SNDRV_TIMER_EVENT_TICK)| 1796 (1<<SNDRV_TIMER_EVENT_START)| 1797 (1<<SNDRV_TIMER_EVENT_STOP)| 1798 (1<<SNDRV_TIMER_EVENT_CONTINUE)| 1799 (1<<SNDRV_TIMER_EVENT_PAUSE)| 1800 (1<<SNDRV_TIMER_EVENT_SUSPEND)| 1801 (1<<SNDRV_TIMER_EVENT_RESUME)| 1802 (1<<SNDRV_TIMER_EVENT_MSTART)| 1803 (1<<SNDRV_TIMER_EVENT_MSTOP)| 1804 (1<<SNDRV_TIMER_EVENT_MCONTINUE)| 1805 (1<<SNDRV_TIMER_EVENT_MPAUSE)| 1806 (1<<SNDRV_TIMER_EVENT_MSUSPEND)| 1807 (1<<SNDRV_TIMER_EVENT_MRESUME))) { 1808 err = -EINVAL; 1809 goto _end; 1810 } 1811 snd_timer_stop(tu->timeri); 1812 scoped_guard(spinlock_irq, &t->lock) { 1813 tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO| 1814 SNDRV_TIMER_IFLG_EXCLUSIVE| 1815 SNDRV_TIMER_IFLG_EARLY_EVENT); 1816 if (params.flags & SNDRV_TIMER_PSFLG_AUTO) 1817 tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO; 1818 if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE) 1819 tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE; 1820 if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT) 1821 tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT; 1822 } 1823 if (params.queue_size > 0 && 1824 (unsigned int)tu->queue_size != params.queue_size) { 1825 err = realloc_user_queue(tu, params.queue_size); 1826 if (err < 0) 1827 goto _end; 1828 } 1829 scoped_guard(spinlock_irq, &tu->qlock) { 1830 tu->qhead = tu->qtail = tu->qused = 0; 1831 if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) { 1832 if (tu->tread) { 1833 struct snd_timer_tread64 tread; 1834 1835 memset(&tread, 0, sizeof(tread)); 1836 tread.event = SNDRV_TIMER_EVENT_EARLY; 1837 tread.tstamp_sec = 0; 1838 tread.tstamp_nsec = 0; 1839 tread.val = 0; 1840 snd_timer_user_append_to_tqueue(tu, &tread); 1841 } else { 1842 struct snd_timer_read *r = &tu->queue[0]; 1843 1844 r->resolution = 0; 1845 r->ticks = 0; 1846 tu->qused++; 1847 tu->qtail++; 1848 } 1849 } 1850 tu->filter = params.filter; 1851 tu->ticks = params.ticks; 1852 } 1853 err = 0; 1854 _end: 1855 if (copy_to_user(_params, ¶ms, sizeof(params))) 1856 return -EFAULT; 1857 return err; 1858 } 1859 1860 static int snd_timer_user_status32(struct file *file, 1861 struct snd_timer_status32 __user *_status) 1862 { 1863 struct snd_timer_user *tu; 1864 struct snd_timer_status32 status; 1865 1866 tu = file->private_data; 1867 if (!tu->timeri) 1868 return -EBADFD; 1869 memset(&status, 0, sizeof(status)); 1870 status.tstamp_sec = tu->tstamp.tv_sec; 1871 status.tstamp_nsec = tu->tstamp.tv_nsec; 1872 status.resolution = snd_timer_resolution(tu->timeri); 1873 status.lost = tu->timeri->lost; 1874 status.overrun = tu->overrun; 1875 scoped_guard(spinlock_irq, &tu->qlock) 1876 status.queue = tu->qused; 1877 if (copy_to_user(_status, &status, sizeof(status))) 1878 return -EFAULT; 1879 return 0; 1880 } 1881 1882 static int snd_timer_user_status64(struct file *file, 1883 struct snd_timer_status64 __user *_status) 1884 { 1885 struct snd_timer_user *tu; 1886 struct snd_timer_status64 status; 1887 1888 tu = file->private_data; 1889 if (!tu->timeri) 1890 return -EBADFD; 1891 memset(&status, 0, sizeof(status)); 1892 status.tstamp_sec = tu->tstamp.tv_sec; 1893 status.tstamp_nsec = tu->tstamp.tv_nsec; 1894 status.resolution = snd_timer_resolution(tu->timeri); 1895 status.lost = tu->timeri->lost; 1896 status.overrun = tu->overrun; 1897 scoped_guard(spinlock_irq, &tu->qlock) 1898 status.queue = tu->qused; 1899 if (copy_to_user(_status, &status, sizeof(status))) 1900 return -EFAULT; 1901 return 0; 1902 } 1903 1904 static int snd_timer_user_start(struct file *file) 1905 { 1906 int err; 1907 struct snd_timer_user *tu; 1908 1909 tu = file->private_data; 1910 if (!tu->timeri) 1911 return -EBADFD; 1912 snd_timer_stop(tu->timeri); 1913 tu->timeri->lost = 0; 1914 tu->last_resolution = 0; 1915 err = snd_timer_start(tu->timeri, tu->ticks); 1916 if (err < 0) 1917 return err; 1918 return 0; 1919 } 1920 1921 static int snd_timer_user_stop(struct file *file) 1922 { 1923 int err; 1924 struct snd_timer_user *tu; 1925 1926 tu = file->private_data; 1927 if (!tu->timeri) 1928 return -EBADFD; 1929 err = snd_timer_stop(tu->timeri); 1930 if (err < 0) 1931 return err; 1932 return 0; 1933 } 1934 1935 static int snd_timer_user_continue(struct file *file) 1936 { 1937 int err; 1938 struct snd_timer_user *tu; 1939 1940 tu = file->private_data; 1941 if (!tu->timeri) 1942 return -EBADFD; 1943 /* start timer instead of continue if it's not used before */ 1944 if (!(tu->timeri->flags & SNDRV_TIMER_IFLG_PAUSED)) 1945 return snd_timer_user_start(file); 1946 tu->timeri->lost = 0; 1947 err = snd_timer_continue(tu->timeri); 1948 if (err < 0) 1949 return err; 1950 return 0; 1951 } 1952 1953 static int snd_timer_user_pause(struct file *file) 1954 { 1955 int err; 1956 struct snd_timer_user *tu; 1957 1958 tu = file->private_data; 1959 if (!tu->timeri) 1960 return -EBADFD; 1961 err = snd_timer_pause(tu->timeri); 1962 if (err < 0) 1963 return err; 1964 return 0; 1965 } 1966 1967 static int snd_timer_user_tread(void __user *argp, struct snd_timer_user *tu, 1968 unsigned int cmd, bool compat) 1969 { 1970 int __user *p = argp; 1971 int xarg, old_tread; 1972 1973 if (tu->timeri) /* too late */ 1974 return -EBUSY; 1975 if (get_user(xarg, p)) 1976 return -EFAULT; 1977 1978 old_tread = tu->tread; 1979 1980 if (!xarg) 1981 tu->tread = TREAD_FORMAT_NONE; 1982 else if (cmd == SNDRV_TIMER_IOCTL_TREAD64 || 1983 (IS_ENABLED(CONFIG_64BIT) && !compat)) 1984 tu->tread = TREAD_FORMAT_TIME64; 1985 else 1986 tu->tread = TREAD_FORMAT_TIME32; 1987 1988 if (tu->tread != old_tread && 1989 realloc_user_queue(tu, tu->queue_size) < 0) { 1990 tu->tread = old_tread; 1991 return -ENOMEM; 1992 } 1993 1994 return 0; 1995 } 1996 1997 enum { 1998 SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20), 1999 SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21), 2000 SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22), 2001 SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23), 2002 }; 2003 2004 static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd, 2005 unsigned long arg, bool compat) 2006 { 2007 struct snd_timer_user *tu; 2008 void __user *argp = (void __user *)arg; 2009 int __user *p = argp; 2010 2011 tu = file->private_data; 2012 switch (cmd) { 2013 case SNDRV_TIMER_IOCTL_PVERSION: 2014 return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0; 2015 case SNDRV_TIMER_IOCTL_NEXT_DEVICE: 2016 return snd_timer_user_next_device(argp); 2017 case SNDRV_TIMER_IOCTL_TREAD_OLD: 2018 case SNDRV_TIMER_IOCTL_TREAD64: 2019 return snd_timer_user_tread(argp, tu, cmd, compat); 2020 case SNDRV_TIMER_IOCTL_GINFO: 2021 return snd_timer_user_ginfo(file, argp); 2022 case SNDRV_TIMER_IOCTL_GPARAMS: 2023 return snd_timer_user_gparams(file, argp); 2024 case SNDRV_TIMER_IOCTL_GSTATUS: 2025 return snd_timer_user_gstatus(file, argp); 2026 case SNDRV_TIMER_IOCTL_SELECT: 2027 return snd_timer_user_tselect(file, argp); 2028 case SNDRV_TIMER_IOCTL_INFO: 2029 return snd_timer_user_info(file, argp); 2030 case SNDRV_TIMER_IOCTL_PARAMS: 2031 return snd_timer_user_params(file, argp); 2032 case SNDRV_TIMER_IOCTL_STATUS32: 2033 return snd_timer_user_status32(file, argp); 2034 case SNDRV_TIMER_IOCTL_STATUS64: 2035 return snd_timer_user_status64(file, argp); 2036 case SNDRV_TIMER_IOCTL_START: 2037 case SNDRV_TIMER_IOCTL_START_OLD: 2038 return snd_timer_user_start(file); 2039 case SNDRV_TIMER_IOCTL_STOP: 2040 case SNDRV_TIMER_IOCTL_STOP_OLD: 2041 return snd_timer_user_stop(file); 2042 case SNDRV_TIMER_IOCTL_CONTINUE: 2043 case SNDRV_TIMER_IOCTL_CONTINUE_OLD: 2044 return snd_timer_user_continue(file); 2045 case SNDRV_TIMER_IOCTL_PAUSE: 2046 case SNDRV_TIMER_IOCTL_PAUSE_OLD: 2047 return snd_timer_user_pause(file); 2048 } 2049 return -ENOTTY; 2050 } 2051 2052 static long snd_timer_user_ioctl(struct file *file, unsigned int cmd, 2053 unsigned long arg) 2054 { 2055 struct snd_timer_user *tu = file->private_data; 2056 2057 guard(mutex)(&tu->ioctl_lock); 2058 return __snd_timer_user_ioctl(file, cmd, arg, false); 2059 } 2060 2061 static int snd_timer_user_fasync(int fd, struct file * file, int on) 2062 { 2063 struct snd_timer_user *tu; 2064 2065 tu = file->private_data; 2066 return snd_fasync_helper(fd, file, on, &tu->fasync); 2067 } 2068 2069 static ssize_t snd_timer_user_read(struct file *file, char __user *buffer, 2070 size_t count, loff_t *offset) 2071 { 2072 struct snd_timer_tread64 *tread; 2073 struct snd_timer_tread32 tread32; 2074 struct snd_timer_user *tu; 2075 long result = 0, unit; 2076 int qhead; 2077 int err = 0; 2078 2079 tu = file->private_data; 2080 switch (tu->tread) { 2081 case TREAD_FORMAT_TIME64: 2082 unit = sizeof(struct snd_timer_tread64); 2083 break; 2084 case TREAD_FORMAT_TIME32: 2085 unit = sizeof(struct snd_timer_tread32); 2086 break; 2087 case TREAD_FORMAT_NONE: 2088 unit = sizeof(struct snd_timer_read); 2089 break; 2090 default: 2091 WARN_ONCE(1, "Corrupt snd_timer_user\n"); 2092 return -ENOTSUPP; 2093 } 2094 2095 mutex_lock(&tu->ioctl_lock); 2096 spin_lock_irq(&tu->qlock); 2097 while ((long)count - result >= unit) { 2098 while (!tu->qused) { 2099 wait_queue_entry_t wait; 2100 2101 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) { 2102 err = -EAGAIN; 2103 goto _error; 2104 } 2105 2106 set_current_state(TASK_INTERRUPTIBLE); 2107 init_waitqueue_entry(&wait, current); 2108 add_wait_queue(&tu->qchange_sleep, &wait); 2109 2110 spin_unlock_irq(&tu->qlock); 2111 mutex_unlock(&tu->ioctl_lock); 2112 schedule(); 2113 mutex_lock(&tu->ioctl_lock); 2114 spin_lock_irq(&tu->qlock); 2115 2116 remove_wait_queue(&tu->qchange_sleep, &wait); 2117 2118 if (tu->disconnected) { 2119 err = -ENODEV; 2120 goto _error; 2121 } 2122 if (signal_pending(current)) { 2123 err = -ERESTARTSYS; 2124 goto _error; 2125 } 2126 } 2127 2128 qhead = tu->qhead++; 2129 tu->qhead %= tu->queue_size; 2130 tu->qused--; 2131 spin_unlock_irq(&tu->qlock); 2132 2133 tread = &tu->tqueue[qhead]; 2134 2135 switch (tu->tread) { 2136 case TREAD_FORMAT_TIME64: 2137 if (copy_to_user(buffer, tread, 2138 sizeof(struct snd_timer_tread64))) 2139 err = -EFAULT; 2140 break; 2141 case TREAD_FORMAT_TIME32: 2142 memset(&tread32, 0, sizeof(tread32)); 2143 tread32 = (struct snd_timer_tread32) { 2144 .event = tread->event, 2145 .tstamp_sec = tread->tstamp_sec, 2146 .tstamp_nsec = tread->tstamp_nsec, 2147 .val = tread->val, 2148 }; 2149 2150 if (copy_to_user(buffer, &tread32, sizeof(tread32))) 2151 err = -EFAULT; 2152 break; 2153 case TREAD_FORMAT_NONE: 2154 if (copy_to_user(buffer, &tu->queue[qhead], 2155 sizeof(struct snd_timer_read))) 2156 err = -EFAULT; 2157 break; 2158 default: 2159 err = -ENOTSUPP; 2160 break; 2161 } 2162 2163 spin_lock_irq(&tu->qlock); 2164 if (err < 0) 2165 goto _error; 2166 result += unit; 2167 buffer += unit; 2168 } 2169 _error: 2170 spin_unlock_irq(&tu->qlock); 2171 mutex_unlock(&tu->ioctl_lock); 2172 return result > 0 ? result : err; 2173 } 2174 2175 static __poll_t snd_timer_user_poll(struct file *file, poll_table * wait) 2176 { 2177 __poll_t mask; 2178 struct snd_timer_user *tu; 2179 2180 tu = file->private_data; 2181 2182 poll_wait(file, &tu->qchange_sleep, wait); 2183 2184 mask = 0; 2185 guard(spinlock_irq)(&tu->qlock); 2186 if (tu->qused) 2187 mask |= EPOLLIN | EPOLLRDNORM; 2188 if (tu->disconnected) 2189 mask |= EPOLLERR; 2190 2191 return mask; 2192 } 2193 2194 #ifdef CONFIG_COMPAT 2195 #include "timer_compat.c" 2196 #else 2197 #define snd_timer_user_ioctl_compat NULL 2198 #endif 2199 2200 static const struct file_operations snd_timer_f_ops = 2201 { 2202 .owner = THIS_MODULE, 2203 .read = snd_timer_user_read, 2204 .open = snd_timer_user_open, 2205 .release = snd_timer_user_release, 2206 .llseek = no_llseek, 2207 .poll = snd_timer_user_poll, 2208 .unlocked_ioctl = snd_timer_user_ioctl, 2209 .compat_ioctl = snd_timer_user_ioctl_compat, 2210 .fasync = snd_timer_user_fasync, 2211 }; 2212 2213 /* unregister the system timer */ 2214 static void snd_timer_free_all(void) 2215 { 2216 struct snd_timer *timer, *n; 2217 2218 list_for_each_entry_safe(timer, n, &snd_timer_list, device_list) 2219 snd_timer_free(timer); 2220 } 2221 2222 static struct device *timer_dev; 2223 2224 /* 2225 * ENTRY functions 2226 */ 2227 2228 static int __init alsa_timer_init(void) 2229 { 2230 int err; 2231 2232 err = snd_device_alloc(&timer_dev, NULL); 2233 if (err < 0) 2234 return err; 2235 dev_set_name(timer_dev, "timer"); 2236 2237 #ifdef SNDRV_OSS_INFO_DEV_TIMERS 2238 snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1, 2239 "system timer"); 2240 #endif 2241 2242 err = snd_timer_register_system(); 2243 if (err < 0) { 2244 pr_err("ALSA: unable to register system timer (%i)\n", err); 2245 goto put_timer; 2246 } 2247 2248 err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0, 2249 &snd_timer_f_ops, NULL, timer_dev); 2250 if (err < 0) { 2251 pr_err("ALSA: unable to register timer device (%i)\n", err); 2252 snd_timer_free_all(); 2253 goto put_timer; 2254 } 2255 2256 snd_timer_proc_init(); 2257 return 0; 2258 2259 put_timer: 2260 put_device(timer_dev); 2261 return err; 2262 } 2263 2264 static void __exit alsa_timer_exit(void) 2265 { 2266 snd_unregister_device(timer_dev); 2267 snd_timer_free_all(); 2268 put_device(timer_dev); 2269 snd_timer_proc_done(); 2270 #ifdef SNDRV_OSS_INFO_DEV_TIMERS 2271 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1); 2272 #endif 2273 } 2274 2275 module_init(alsa_timer_init) 2276 module_exit(alsa_timer_exit) 2277