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