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