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