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