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