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