PageRenderTime 62ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/sound/core/timer.c

https://github.com/mstsirkin/kvm
C | 1919 lines | 1636 code | 168 blank | 115 comment | 319 complexity | bc4b06fa78fb353d31a5d9c74fdc06eb MD5 | raw file
  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. #include <linux/delay.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/time.h>
  25. #include <linux/mutex.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/string.h>
  28. #include <sound/core.h>
  29. #include <sound/timer.h>
  30. #include <sound/control.h>
  31. #include <sound/info.h>
  32. #include <sound/minors.h>
  33. #include <sound/initval.h>
  34. #include <linux/kmod.h>
  35. #if defined(CONFIG_SND_HRTIMER) || defined(CONFIG_SND_HRTIMER_MODULE)
  36. #define DEFAULT_TIMER_LIMIT 4
  37. #elif defined(CONFIG_SND_RTCTIMER) || defined(CONFIG_SND_RTCTIMER_MODULE)
  38. #define DEFAULT_TIMER_LIMIT 2
  39. #else
  40. #define DEFAULT_TIMER_LIMIT 1
  41. #endif
  42. static int timer_limit = DEFAULT_TIMER_LIMIT;
  43. static int timer_tstamp_monotonic = 1;
  44. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
  45. MODULE_DESCRIPTION("ALSA timer interface");
  46. MODULE_LICENSE("GPL");
  47. module_param(timer_limit, int, 0444);
  48. MODULE_PARM_DESC(timer_limit, "Maximum global timers in system.");
  49. module_param(timer_tstamp_monotonic, int, 0444);
  50. MODULE_PARM_DESC(timer_tstamp_monotonic, "Use posix monotonic clock source for timestamps (default).");
  51. MODULE_ALIAS_CHARDEV(CONFIG_SND_MAJOR, SNDRV_MINOR_TIMER);
  52. MODULE_ALIAS("devname:snd/timer");
  53. struct snd_timer_user {
  54. struct snd_timer_instance *timeri;
  55. int tread; /* enhanced read with timestamps and events */
  56. unsigned long ticks;
  57. unsigned long overrun;
  58. int qhead;
  59. int qtail;
  60. int qused;
  61. int queue_size;
  62. struct snd_timer_read *queue;
  63. struct snd_timer_tread *tqueue;
  64. spinlock_t qlock;
  65. unsigned long last_resolution;
  66. unsigned int filter;
  67. struct timespec tstamp; /* trigger tstamp */
  68. wait_queue_head_t qchange_sleep;
  69. struct fasync_struct *fasync;
  70. struct mutex tread_sem;
  71. };
  72. /* list of timers */
  73. static LIST_HEAD(snd_timer_list);
  74. /* list of slave instances */
  75. static LIST_HEAD(snd_timer_slave_list);
  76. /* lock for slave active lists */
  77. static DEFINE_SPINLOCK(slave_active_lock);
  78. static DEFINE_MUTEX(register_mutex);
  79. static int snd_timer_free(struct snd_timer *timer);
  80. static int snd_timer_dev_free(struct snd_device *device);
  81. static int snd_timer_dev_register(struct snd_device *device);
  82. static int snd_timer_dev_disconnect(struct snd_device *device);
  83. static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left);
  84. /*
  85. * create a timer instance with the given owner string.
  86. * when timer is not NULL, increments the module counter
  87. */
  88. static struct snd_timer_instance *snd_timer_instance_new(char *owner,
  89. struct snd_timer *timer)
  90. {
  91. struct snd_timer_instance *timeri;
  92. timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
  93. if (timeri == NULL)
  94. return NULL;
  95. timeri->owner = kstrdup(owner, GFP_KERNEL);
  96. if (! timeri->owner) {
  97. kfree(timeri);
  98. return NULL;
  99. }
  100. INIT_LIST_HEAD(&timeri->open_list);
  101. INIT_LIST_HEAD(&timeri->active_list);
  102. INIT_LIST_HEAD(&timeri->ack_list);
  103. INIT_LIST_HEAD(&timeri->slave_list_head);
  104. INIT_LIST_HEAD(&timeri->slave_active_head);
  105. timeri->timer = timer;
  106. if (timer && !try_module_get(timer->module)) {
  107. kfree(timeri->owner);
  108. kfree(timeri);
  109. return NULL;
  110. }
  111. return timeri;
  112. }
  113. /*
  114. * find a timer instance from the given timer id
  115. */
  116. static struct snd_timer *snd_timer_find(struct snd_timer_id *tid)
  117. {
  118. struct snd_timer *timer = NULL;
  119. list_for_each_entry(timer, &snd_timer_list, device_list) {
  120. if (timer->tmr_class != tid->dev_class)
  121. continue;
  122. if ((timer->tmr_class == SNDRV_TIMER_CLASS_CARD ||
  123. timer->tmr_class == SNDRV_TIMER_CLASS_PCM) &&
  124. (timer->card == NULL ||
  125. timer->card->number != tid->card))
  126. continue;
  127. if (timer->tmr_device != tid->device)
  128. continue;
  129. if (timer->tmr_subdevice != tid->subdevice)
  130. continue;
  131. return timer;
  132. }
  133. return NULL;
  134. }
  135. #ifdef CONFIG_MODULES
  136. static void snd_timer_request(struct snd_timer_id *tid)
  137. {
  138. switch (tid->dev_class) {
  139. case SNDRV_TIMER_CLASS_GLOBAL:
  140. if (tid->device < timer_limit)
  141. request_module("snd-timer-%i", tid->device);
  142. break;
  143. case SNDRV_TIMER_CLASS_CARD:
  144. case SNDRV_TIMER_CLASS_PCM:
  145. if (tid->card < snd_ecards_limit)
  146. request_module("snd-card-%i", tid->card);
  147. break;
  148. default:
  149. break;
  150. }
  151. }
  152. #endif
  153. /*
  154. * look for a master instance matching with the slave id of the given slave.
  155. * when found, relink the open_link of the slave.
  156. *
  157. * call this with register_mutex down.
  158. */
  159. static void snd_timer_check_slave(struct snd_timer_instance *slave)
  160. {
  161. struct snd_timer *timer;
  162. struct snd_timer_instance *master;
  163. /* FIXME: it's really dumb to look up all entries.. */
  164. list_for_each_entry(timer, &snd_timer_list, device_list) {
  165. list_for_each_entry(master, &timer->open_list_head, open_list) {
  166. if (slave->slave_class == master->slave_class &&
  167. slave->slave_id == master->slave_id) {
  168. list_move_tail(&slave->open_list,
  169. &master->slave_list_head);
  170. spin_lock_irq(&slave_active_lock);
  171. slave->master = master;
  172. slave->timer = master->timer;
  173. spin_unlock_irq(&slave_active_lock);
  174. return;
  175. }
  176. }
  177. }
  178. }
  179. /*
  180. * look for slave instances matching with the slave id of the given master.
  181. * when found, relink the open_link of slaves.
  182. *
  183. * call this with register_mutex down.
  184. */
  185. static void snd_timer_check_master(struct snd_timer_instance *master)
  186. {
  187. struct snd_timer_instance *slave, *tmp;
  188. /* check all pending slaves */
  189. list_for_each_entry_safe(slave, tmp, &snd_timer_slave_list, open_list) {
  190. if (slave->slave_class == master->slave_class &&
  191. slave->slave_id == master->slave_id) {
  192. list_move_tail(&slave->open_list, &master->slave_list_head);
  193. spin_lock_irq(&slave_active_lock);
  194. slave->master = master;
  195. slave->timer = master->timer;
  196. if (slave->flags & SNDRV_TIMER_IFLG_RUNNING)
  197. list_add_tail(&slave->active_list,
  198. &master->slave_active_head);
  199. spin_unlock_irq(&slave_active_lock);
  200. }
  201. }
  202. }
  203. /*
  204. * open a timer instance
  205. * when opening a master, the slave id must be here given.
  206. */
  207. int snd_timer_open(struct snd_timer_instance **ti,
  208. char *owner, struct snd_timer_id *tid,
  209. unsigned int slave_id)
  210. {
  211. struct snd_timer *timer;
  212. struct snd_timer_instance *timeri = NULL;
  213. if (tid->dev_class == SNDRV_TIMER_CLASS_SLAVE) {
  214. /* open a slave instance */
  215. if (tid->dev_sclass <= SNDRV_TIMER_SCLASS_NONE ||
  216. tid->dev_sclass > SNDRV_TIMER_SCLASS_OSS_SEQUENCER) {
  217. snd_printd("invalid slave class %i\n", tid->dev_sclass);
  218. return -EINVAL;
  219. }
  220. mutex_lock(&register_mutex);
  221. timeri = snd_timer_instance_new(owner, NULL);
  222. if (!timeri) {
  223. mutex_unlock(&register_mutex);
  224. return -ENOMEM;
  225. }
  226. timeri->slave_class = tid->dev_sclass;
  227. timeri->slave_id = tid->device;
  228. timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
  229. list_add_tail(&timeri->open_list, &snd_timer_slave_list);
  230. snd_timer_check_slave(timeri);
  231. mutex_unlock(&register_mutex);
  232. *ti = timeri;
  233. return 0;
  234. }
  235. /* open a master instance */
  236. mutex_lock(&register_mutex);
  237. timer = snd_timer_find(tid);
  238. #ifdef CONFIG_MODULES
  239. if (!timer) {
  240. mutex_unlock(&register_mutex);
  241. snd_timer_request(tid);
  242. mutex_lock(&register_mutex);
  243. timer = snd_timer_find(tid);
  244. }
  245. #endif
  246. if (!timer) {
  247. mutex_unlock(&register_mutex);
  248. return -ENODEV;
  249. }
  250. if (!list_empty(&timer->open_list_head)) {
  251. timeri = list_entry(timer->open_list_head.next,
  252. struct snd_timer_instance, open_list);
  253. if (timeri->flags & SNDRV_TIMER_IFLG_EXCLUSIVE) {
  254. mutex_unlock(&register_mutex);
  255. return -EBUSY;
  256. }
  257. }
  258. timeri = snd_timer_instance_new(owner, timer);
  259. if (!timeri) {
  260. mutex_unlock(&register_mutex);
  261. return -ENOMEM;
  262. }
  263. timeri->slave_class = tid->dev_sclass;
  264. timeri->slave_id = slave_id;
  265. if (list_empty(&timer->open_list_head) && timer->hw.open)
  266. timer->hw.open(timer);
  267. list_add_tail(&timeri->open_list, &timer->open_list_head);
  268. snd_timer_check_master(timeri);
  269. mutex_unlock(&register_mutex);
  270. *ti = timeri;
  271. return 0;
  272. }
  273. static int _snd_timer_stop(struct snd_timer_instance *timeri,
  274. int keep_flag, int event);
  275. /*
  276. * close a timer instance
  277. */
  278. int snd_timer_close(struct snd_timer_instance *timeri)
  279. {
  280. struct snd_timer *timer = NULL;
  281. struct snd_timer_instance *slave, *tmp;
  282. if (snd_BUG_ON(!timeri))
  283. return -ENXIO;
  284. /* force to stop the timer */
  285. snd_timer_stop(timeri);
  286. if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
  287. /* wait, until the active callback is finished */
  288. spin_lock_irq(&slave_active_lock);
  289. while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
  290. spin_unlock_irq(&slave_active_lock);
  291. udelay(10);
  292. spin_lock_irq(&slave_active_lock);
  293. }
  294. spin_unlock_irq(&slave_active_lock);
  295. mutex_lock(&register_mutex);
  296. list_del(&timeri->open_list);
  297. mutex_unlock(&register_mutex);
  298. } else {
  299. timer = timeri->timer;
  300. if (snd_BUG_ON(!timer))
  301. goto out;
  302. /* wait, until the active callback is finished */
  303. spin_lock_irq(&timer->lock);
  304. while (timeri->flags & SNDRV_TIMER_IFLG_CALLBACK) {
  305. spin_unlock_irq(&timer->lock);
  306. udelay(10);
  307. spin_lock_irq(&timer->lock);
  308. }
  309. spin_unlock_irq(&timer->lock);
  310. mutex_lock(&register_mutex);
  311. list_del(&timeri->open_list);
  312. if (timer && list_empty(&timer->open_list_head) &&
  313. timer->hw.close)
  314. timer->hw.close(timer);
  315. /* remove slave links */
  316. list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
  317. open_list) {
  318. spin_lock_irq(&slave_active_lock);
  319. _snd_timer_stop(slave, 1, SNDRV_TIMER_EVENT_RESOLUTION);
  320. list_move_tail(&slave->open_list, &snd_timer_slave_list);
  321. slave->master = NULL;
  322. slave->timer = NULL;
  323. spin_unlock_irq(&slave_active_lock);
  324. }
  325. mutex_unlock(&register_mutex);
  326. }
  327. out:
  328. if (timeri->private_free)
  329. timeri->private_free(timeri);
  330. kfree(timeri->owner);
  331. kfree(timeri);
  332. if (timer)
  333. module_put(timer->module);
  334. return 0;
  335. }
  336. unsigned long snd_timer_resolution(struct snd_timer_instance *timeri)
  337. {
  338. struct snd_timer * timer;
  339. if (timeri == NULL)
  340. return 0;
  341. if ((timer = timeri->timer) != NULL) {
  342. if (timer->hw.c_resolution)
  343. return timer->hw.c_resolution(timer);
  344. return timer->hw.resolution;
  345. }
  346. return 0;
  347. }
  348. static void snd_timer_notify1(struct snd_timer_instance *ti, int event)
  349. {
  350. struct snd_timer *timer;
  351. unsigned long flags;
  352. unsigned long resolution = 0;
  353. struct snd_timer_instance *ts;
  354. struct timespec tstamp;
  355. if (timer_tstamp_monotonic)
  356. do_posix_clock_monotonic_gettime(&tstamp);
  357. else
  358. getnstimeofday(&tstamp);
  359. if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_START ||
  360. event > SNDRV_TIMER_EVENT_PAUSE))
  361. return;
  362. if (event == SNDRV_TIMER_EVENT_START ||
  363. event == SNDRV_TIMER_EVENT_CONTINUE)
  364. resolution = snd_timer_resolution(ti);
  365. if (ti->ccallback)
  366. ti->ccallback(ti, event, &tstamp, resolution);
  367. if (ti->flags & SNDRV_TIMER_IFLG_SLAVE)
  368. return;
  369. timer = ti->timer;
  370. if (timer == NULL)
  371. return;
  372. if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
  373. return;
  374. spin_lock_irqsave(&timer->lock, flags);
  375. list_for_each_entry(ts, &ti->slave_active_head, active_list)
  376. if (ts->ccallback)
  377. ts->ccallback(ti, event + 100, &tstamp, resolution);
  378. spin_unlock_irqrestore(&timer->lock, flags);
  379. }
  380. static int snd_timer_start1(struct snd_timer *timer, struct snd_timer_instance *timeri,
  381. unsigned long sticks)
  382. {
  383. list_move_tail(&timeri->active_list, &timer->active_list_head);
  384. if (timer->running) {
  385. if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
  386. goto __start_now;
  387. timer->flags |= SNDRV_TIMER_FLG_RESCHED;
  388. timeri->flags |= SNDRV_TIMER_IFLG_START;
  389. return 1; /* delayed start */
  390. } else {
  391. timer->sticks = sticks;
  392. timer->hw.start(timer);
  393. __start_now:
  394. timer->running++;
  395. timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
  396. return 0;
  397. }
  398. }
  399. static int snd_timer_start_slave(struct snd_timer_instance *timeri)
  400. {
  401. unsigned long flags;
  402. spin_lock_irqsave(&slave_active_lock, flags);
  403. timeri->flags |= SNDRV_TIMER_IFLG_RUNNING;
  404. if (timeri->master)
  405. list_add_tail(&timeri->active_list,
  406. &timeri->master->slave_active_head);
  407. spin_unlock_irqrestore(&slave_active_lock, flags);
  408. return 1; /* delayed start */
  409. }
  410. /*
  411. * start the timer instance
  412. */
  413. int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
  414. {
  415. struct snd_timer *timer;
  416. int result = -EINVAL;
  417. unsigned long flags;
  418. if (timeri == NULL || ticks < 1)
  419. return -EINVAL;
  420. if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
  421. result = snd_timer_start_slave(timeri);
  422. snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
  423. return result;
  424. }
  425. timer = timeri->timer;
  426. if (timer == NULL)
  427. return -EINVAL;
  428. spin_lock_irqsave(&timer->lock, flags);
  429. timeri->ticks = timeri->cticks = ticks;
  430. timeri->pticks = 0;
  431. result = snd_timer_start1(timer, timeri, ticks);
  432. spin_unlock_irqrestore(&timer->lock, flags);
  433. snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_START);
  434. return result;
  435. }
  436. static int _snd_timer_stop(struct snd_timer_instance * timeri,
  437. int keep_flag, int event)
  438. {
  439. struct snd_timer *timer;
  440. unsigned long flags;
  441. if (snd_BUG_ON(!timeri))
  442. return -ENXIO;
  443. if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE) {
  444. if (!keep_flag) {
  445. spin_lock_irqsave(&slave_active_lock, flags);
  446. timeri->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
  447. spin_unlock_irqrestore(&slave_active_lock, flags);
  448. }
  449. goto __end;
  450. }
  451. timer = timeri->timer;
  452. if (!timer)
  453. return -EINVAL;
  454. spin_lock_irqsave(&timer->lock, flags);
  455. list_del_init(&timeri->ack_list);
  456. list_del_init(&timeri->active_list);
  457. if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
  458. !(--timer->running)) {
  459. timer->hw.stop(timer);
  460. if (timer->flags & SNDRV_TIMER_FLG_RESCHED) {
  461. timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
  462. snd_timer_reschedule(timer, 0);
  463. if (timer->flags & SNDRV_TIMER_FLG_CHANGE) {
  464. timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
  465. timer->hw.start(timer);
  466. }
  467. }
  468. }
  469. if (!keep_flag)
  470. timeri->flags &=
  471. ~(SNDRV_TIMER_IFLG_RUNNING | SNDRV_TIMER_IFLG_START);
  472. spin_unlock_irqrestore(&timer->lock, flags);
  473. __end:
  474. if (event != SNDRV_TIMER_EVENT_RESOLUTION)
  475. snd_timer_notify1(timeri, event);
  476. return 0;
  477. }
  478. /*
  479. * stop the timer instance.
  480. *
  481. * do not call this from the timer callback!
  482. */
  483. int snd_timer_stop(struct snd_timer_instance *timeri)
  484. {
  485. struct snd_timer *timer;
  486. unsigned long flags;
  487. int err;
  488. err = _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_STOP);
  489. if (err < 0)
  490. return err;
  491. timer = timeri->timer;
  492. if (!timer)
  493. return -EINVAL;
  494. spin_lock_irqsave(&timer->lock, flags);
  495. timeri->cticks = timeri->ticks;
  496. timeri->pticks = 0;
  497. spin_unlock_irqrestore(&timer->lock, flags);
  498. return 0;
  499. }
  500. /*
  501. * start again.. the tick is kept.
  502. */
  503. int snd_timer_continue(struct snd_timer_instance *timeri)
  504. {
  505. struct snd_timer *timer;
  506. int result = -EINVAL;
  507. unsigned long flags;
  508. if (timeri == NULL)
  509. return result;
  510. if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
  511. return snd_timer_start_slave(timeri);
  512. timer = timeri->timer;
  513. if (! timer)
  514. return -EINVAL;
  515. spin_lock_irqsave(&timer->lock, flags);
  516. if (!timeri->cticks)
  517. timeri->cticks = 1;
  518. timeri->pticks = 0;
  519. result = snd_timer_start1(timer, timeri, timer->sticks);
  520. spin_unlock_irqrestore(&timer->lock, flags);
  521. snd_timer_notify1(timeri, SNDRV_TIMER_EVENT_CONTINUE);
  522. return result;
  523. }
  524. /*
  525. * pause.. remember the ticks left
  526. */
  527. int snd_timer_pause(struct snd_timer_instance * timeri)
  528. {
  529. return _snd_timer_stop(timeri, 0, SNDRV_TIMER_EVENT_PAUSE);
  530. }
  531. /*
  532. * reschedule the timer
  533. *
  534. * start pending instances and check the scheduling ticks.
  535. * when the scheduling ticks is changed set CHANGE flag to reprogram the timer.
  536. */
  537. static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_left)
  538. {
  539. struct snd_timer_instance *ti;
  540. unsigned long ticks = ~0UL;
  541. list_for_each_entry(ti, &timer->active_list_head, active_list) {
  542. if (ti->flags & SNDRV_TIMER_IFLG_START) {
  543. ti->flags &= ~SNDRV_TIMER_IFLG_START;
  544. ti->flags |= SNDRV_TIMER_IFLG_RUNNING;
  545. timer->running++;
  546. }
  547. if (ti->flags & SNDRV_TIMER_IFLG_RUNNING) {
  548. if (ticks > ti->cticks)
  549. ticks = ti->cticks;
  550. }
  551. }
  552. if (ticks == ~0UL) {
  553. timer->flags &= ~SNDRV_TIMER_FLG_RESCHED;
  554. return;
  555. }
  556. if (ticks > timer->hw.ticks)
  557. ticks = timer->hw.ticks;
  558. if (ticks_left != ticks)
  559. timer->flags |= SNDRV_TIMER_FLG_CHANGE;
  560. timer->sticks = ticks;
  561. }
  562. /*
  563. * timer tasklet
  564. *
  565. */
  566. static void snd_timer_tasklet(unsigned long arg)
  567. {
  568. struct snd_timer *timer = (struct snd_timer *) arg;
  569. struct snd_timer_instance *ti;
  570. struct list_head *p;
  571. unsigned long resolution, ticks;
  572. unsigned long flags;
  573. spin_lock_irqsave(&timer->lock, flags);
  574. /* now process all callbacks */
  575. while (!list_empty(&timer->sack_list_head)) {
  576. p = timer->sack_list_head.next; /* get first item */
  577. ti = list_entry(p, struct snd_timer_instance, ack_list);
  578. /* remove from ack_list and make empty */
  579. list_del_init(p);
  580. ticks = ti->pticks;
  581. ti->pticks = 0;
  582. resolution = ti->resolution;
  583. ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
  584. spin_unlock(&timer->lock);
  585. if (ti->callback)
  586. ti->callback(ti, resolution, ticks);
  587. spin_lock(&timer->lock);
  588. ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
  589. }
  590. spin_unlock_irqrestore(&timer->lock, flags);
  591. }
  592. /*
  593. * timer interrupt
  594. *
  595. * ticks_left is usually equal to timer->sticks.
  596. *
  597. */
  598. void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
  599. {
  600. struct snd_timer_instance *ti, *ts, *tmp;
  601. unsigned long resolution, ticks;
  602. struct list_head *p, *ack_list_head;
  603. unsigned long flags;
  604. int use_tasklet = 0;
  605. if (timer == NULL)
  606. return;
  607. spin_lock_irqsave(&timer->lock, flags);
  608. /* remember the current resolution */
  609. if (timer->hw.c_resolution)
  610. resolution = timer->hw.c_resolution(timer);
  611. else
  612. resolution = timer->hw.resolution;
  613. /* loop for all active instances
  614. * Here we cannot use list_for_each_entry because the active_list of a
  615. * processed instance is relinked to done_list_head before the callback
  616. * is called.
  617. */
  618. list_for_each_entry_safe(ti, tmp, &timer->active_list_head,
  619. active_list) {
  620. if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
  621. continue;
  622. ti->pticks += ticks_left;
  623. ti->resolution = resolution;
  624. if (ti->cticks < ticks_left)
  625. ti->cticks = 0;
  626. else
  627. ti->cticks -= ticks_left;
  628. if (ti->cticks) /* not expired */
  629. continue;
  630. if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
  631. ti->cticks = ti->ticks;
  632. } else {
  633. ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
  634. if (--timer->running)
  635. list_del(&ti->active_list);
  636. }
  637. if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
  638. (ti->flags & SNDRV_TIMER_IFLG_FAST))
  639. ack_list_head = &timer->ack_list_head;
  640. else
  641. ack_list_head = &timer->sack_list_head;
  642. if (list_empty(&ti->ack_list))
  643. list_add_tail(&ti->ack_list, ack_list_head);
  644. list_for_each_entry(ts, &ti->slave_active_head, active_list) {
  645. ts->pticks = ti->pticks;
  646. ts->resolution = resolution;
  647. if (list_empty(&ts->ack_list))
  648. list_add_tail(&ts->ack_list, ack_list_head);
  649. }
  650. }
  651. if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
  652. snd_timer_reschedule(timer, timer->sticks);
  653. if (timer->running) {
  654. if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
  655. timer->hw.stop(timer);
  656. timer->flags |= SNDRV_TIMER_FLG_CHANGE;
  657. }
  658. if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
  659. (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
  660. /* restart timer */
  661. timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
  662. timer->hw.start(timer);
  663. }
  664. } else {
  665. timer->hw.stop(timer);
  666. }
  667. /* now process all fast callbacks */
  668. while (!list_empty(&timer->ack_list_head)) {
  669. p = timer->ack_list_head.next; /* get first item */
  670. ti = list_entry(p, struct snd_timer_instance, ack_list);
  671. /* remove from ack_list and make empty */
  672. list_del_init(p);
  673. ticks = ti->pticks;
  674. ti->pticks = 0;
  675. ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
  676. spin_unlock(&timer->lock);
  677. if (ti->callback)
  678. ti->callback(ti, resolution, ticks);
  679. spin_lock(&timer->lock);
  680. ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
  681. }
  682. /* do we have any slow callbacks? */
  683. use_tasklet = !list_empty(&timer->sack_list_head);
  684. spin_unlock_irqrestore(&timer->lock, flags);
  685. if (use_tasklet)
  686. tasklet_schedule(&timer->task_queue);
  687. }
  688. /*
  689. */
  690. int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
  691. struct snd_timer **rtimer)
  692. {
  693. struct snd_timer *timer;
  694. int err;
  695. static struct snd_device_ops ops = {
  696. .dev_free = snd_timer_dev_free,
  697. .dev_register = snd_timer_dev_register,
  698. .dev_disconnect = snd_timer_dev_disconnect,
  699. };
  700. if (snd_BUG_ON(!tid))
  701. return -EINVAL;
  702. if (rtimer)
  703. *rtimer = NULL;
  704. timer = kzalloc(sizeof(*timer), GFP_KERNEL);
  705. if (timer == NULL) {
  706. snd_printk(KERN_ERR "timer: cannot allocate\n");
  707. return -ENOMEM;
  708. }
  709. timer->tmr_class = tid->dev_class;
  710. timer->card = card;
  711. timer->tmr_device = tid->device;
  712. timer->tmr_subdevice = tid->subdevice;
  713. if (id)
  714. strlcpy(timer->id, id, sizeof(timer->id));
  715. INIT_LIST_HEAD(&timer->device_list);
  716. INIT_LIST_HEAD(&timer->open_list_head);
  717. INIT_LIST_HEAD(&timer->active_list_head);
  718. INIT_LIST_HEAD(&timer->ack_list_head);
  719. INIT_LIST_HEAD(&timer->sack_list_head);
  720. spin_lock_init(&timer->lock);
  721. tasklet_init(&timer->task_queue, snd_timer_tasklet,
  722. (unsigned long)timer);
  723. if (card != NULL) {
  724. timer->module = card->module;
  725. err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
  726. if (err < 0) {
  727. snd_timer_free(timer);
  728. return err;
  729. }
  730. }
  731. if (rtimer)
  732. *rtimer = timer;
  733. return 0;
  734. }
  735. static int snd_timer_free(struct snd_timer *timer)
  736. {
  737. if (!timer)
  738. return 0;
  739. mutex_lock(&register_mutex);
  740. if (! list_empty(&timer->open_list_head)) {
  741. struct list_head *p, *n;
  742. struct snd_timer_instance *ti;
  743. snd_printk(KERN_WARNING "timer %p is busy?\n", timer);
  744. list_for_each_safe(p, n, &timer->open_list_head) {
  745. list_del_init(p);
  746. ti = list_entry(p, struct snd_timer_instance, open_list);
  747. ti->timer = NULL;
  748. }
  749. }
  750. list_del(&timer->device_list);
  751. mutex_unlock(&register_mutex);
  752. if (timer->private_free)
  753. timer->private_free(timer);
  754. kfree(timer);
  755. return 0;
  756. }
  757. static int snd_timer_dev_free(struct snd_device *device)
  758. {
  759. struct snd_timer *timer = device->device_data;
  760. return snd_timer_free(timer);
  761. }
  762. static int snd_timer_dev_register(struct snd_device *dev)
  763. {
  764. struct snd_timer *timer = dev->device_data;
  765. struct snd_timer *timer1;
  766. if (snd_BUG_ON(!timer || !timer->hw.start || !timer->hw.stop))
  767. return -ENXIO;
  768. if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
  769. !timer->hw.resolution && timer->hw.c_resolution == NULL)
  770. return -EINVAL;
  771. mutex_lock(&register_mutex);
  772. list_for_each_entry(timer1, &snd_timer_list, device_list) {
  773. if (timer1->tmr_class > timer->tmr_class)
  774. break;
  775. if (timer1->tmr_class < timer->tmr_class)
  776. continue;
  777. if (timer1->card && timer->card) {
  778. if (timer1->card->number > timer->card->number)
  779. break;
  780. if (timer1->card->number < timer->card->number)
  781. continue;
  782. }
  783. if (timer1->tmr_device > timer->tmr_device)
  784. break;
  785. if (timer1->tmr_device < timer->tmr_device)
  786. continue;
  787. if (timer1->tmr_subdevice > timer->tmr_subdevice)
  788. break;
  789. if (timer1->tmr_subdevice < timer->tmr_subdevice)
  790. continue;
  791. /* conflicts.. */
  792. mutex_unlock(&register_mutex);
  793. return -EBUSY;
  794. }
  795. list_add_tail(&timer->device_list, &timer1->device_list);
  796. mutex_unlock(&register_mutex);
  797. return 0;
  798. }
  799. static int snd_timer_dev_disconnect(struct snd_device *device)
  800. {
  801. struct snd_timer *timer = device->device_data;
  802. mutex_lock(&register_mutex);
  803. list_del_init(&timer->device_list);
  804. mutex_unlock(&register_mutex);
  805. return 0;
  806. }
  807. void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
  808. {
  809. unsigned long flags;
  810. unsigned long resolution = 0;
  811. struct snd_timer_instance *ti, *ts;
  812. if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
  813. return;
  814. if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
  815. event > SNDRV_TIMER_EVENT_MRESUME))
  816. return;
  817. spin_lock_irqsave(&timer->lock, flags);
  818. if (event == SNDRV_TIMER_EVENT_MSTART ||
  819. event == SNDRV_TIMER_EVENT_MCONTINUE ||
  820. event == SNDRV_TIMER_EVENT_MRESUME) {
  821. if (timer->hw.c_resolution)
  822. resolution = timer->hw.c_resolution(timer);
  823. else
  824. resolution = timer->hw.resolution;
  825. }
  826. list_for_each_entry(ti, &timer->active_list_head, active_list) {
  827. if (ti->ccallback)
  828. ti->ccallback(ti, event, tstamp, resolution);
  829. list_for_each_entry(ts, &ti->slave_active_head, active_list)
  830. if (ts->ccallback)
  831. ts->ccallback(ts, event, tstamp, resolution);
  832. }
  833. spin_unlock_irqrestore(&timer->lock, flags);
  834. }
  835. /*
  836. * exported functions for global timers
  837. */
  838. int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
  839. {
  840. struct snd_timer_id tid;
  841. tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
  842. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  843. tid.card = -1;
  844. tid.device = device;
  845. tid.subdevice = 0;
  846. return snd_timer_new(NULL, id, &tid, rtimer);
  847. }
  848. int snd_timer_global_free(struct snd_timer *timer)
  849. {
  850. return snd_timer_free(timer);
  851. }
  852. int snd_timer_global_register(struct snd_timer *timer)
  853. {
  854. struct snd_device dev;
  855. memset(&dev, 0, sizeof(dev));
  856. dev.device_data = timer;
  857. return snd_timer_dev_register(&dev);
  858. }
  859. /*
  860. * System timer
  861. */
  862. struct snd_timer_system_private {
  863. struct timer_list tlist;
  864. unsigned long last_expires;
  865. unsigned long last_jiffies;
  866. unsigned long correction;
  867. };
  868. static void snd_timer_s_function(unsigned long data)
  869. {
  870. struct snd_timer *timer = (struct snd_timer *)data;
  871. struct snd_timer_system_private *priv = timer->private_data;
  872. unsigned long jiff = jiffies;
  873. if (time_after(jiff, priv->last_expires))
  874. priv->correction += (long)jiff - (long)priv->last_expires;
  875. snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
  876. }
  877. static int snd_timer_s_start(struct snd_timer * timer)
  878. {
  879. struct snd_timer_system_private *priv;
  880. unsigned long njiff;
  881. priv = (struct snd_timer_system_private *) timer->private_data;
  882. njiff = (priv->last_jiffies = jiffies);
  883. if (priv->correction > timer->sticks - 1) {
  884. priv->correction -= timer->sticks - 1;
  885. njiff++;
  886. } else {
  887. njiff += timer->sticks - priv->correction;
  888. priv->correction = 0;
  889. }
  890. priv->last_expires = priv->tlist.expires = njiff;
  891. add_timer(&priv->tlist);
  892. return 0;
  893. }
  894. static int snd_timer_s_stop(struct snd_timer * timer)
  895. {
  896. struct snd_timer_system_private *priv;
  897. unsigned long jiff;
  898. priv = (struct snd_timer_system_private *) timer->private_data;
  899. del_timer(&priv->tlist);
  900. jiff = jiffies;
  901. if (time_before(jiff, priv->last_expires))
  902. timer->sticks = priv->last_expires - jiff;
  903. else
  904. timer->sticks = 1;
  905. priv->correction = 0;
  906. return 0;
  907. }
  908. static struct snd_timer_hardware snd_timer_system =
  909. {
  910. .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
  911. .resolution = 1000000000L / HZ,
  912. .ticks = 10000000L,
  913. .start = snd_timer_s_start,
  914. .stop = snd_timer_s_stop
  915. };
  916. static void snd_timer_free_system(struct snd_timer *timer)
  917. {
  918. kfree(timer->private_data);
  919. }
  920. static int snd_timer_register_system(void)
  921. {
  922. struct snd_timer *timer;
  923. struct snd_timer_system_private *priv;
  924. int err;
  925. err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
  926. if (err < 0)
  927. return err;
  928. strcpy(timer->name, "system timer");
  929. timer->hw = snd_timer_system;
  930. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  931. if (priv == NULL) {
  932. snd_timer_free(timer);
  933. return -ENOMEM;
  934. }
  935. init_timer(&priv->tlist);
  936. priv->tlist.function = snd_timer_s_function;
  937. priv->tlist.data = (unsigned long) timer;
  938. timer->private_data = priv;
  939. timer->private_free = snd_timer_free_system;
  940. return snd_timer_global_register(timer);
  941. }
  942. #ifdef CONFIG_PROC_FS
  943. /*
  944. * Info interface
  945. */
  946. static void snd_timer_proc_read(struct snd_info_entry *entry,
  947. struct snd_info_buffer *buffer)
  948. {
  949. struct snd_timer *timer;
  950. struct snd_timer_instance *ti;
  951. mutex_lock(&register_mutex);
  952. list_for_each_entry(timer, &snd_timer_list, device_list) {
  953. switch (timer->tmr_class) {
  954. case SNDRV_TIMER_CLASS_GLOBAL:
  955. snd_iprintf(buffer, "G%i: ", timer->tmr_device);
  956. break;
  957. case SNDRV_TIMER_CLASS_CARD:
  958. snd_iprintf(buffer, "C%i-%i: ",
  959. timer->card->number, timer->tmr_device);
  960. break;
  961. case SNDRV_TIMER_CLASS_PCM:
  962. snd_iprintf(buffer, "P%i-%i-%i: ", timer->card->number,
  963. timer->tmr_device, timer->tmr_subdevice);
  964. break;
  965. default:
  966. snd_iprintf(buffer, "?%i-%i-%i-%i: ", timer->tmr_class,
  967. timer->card ? timer->card->number : -1,
  968. timer->tmr_device, timer->tmr_subdevice);
  969. }
  970. snd_iprintf(buffer, "%s :", timer->name);
  971. if (timer->hw.resolution)
  972. snd_iprintf(buffer, " %lu.%03luus (%lu ticks)",
  973. timer->hw.resolution / 1000,
  974. timer->hw.resolution % 1000,
  975. timer->hw.ticks);
  976. if (timer->hw.flags & SNDRV_TIMER_HW_SLAVE)
  977. snd_iprintf(buffer, " SLAVE");
  978. snd_iprintf(buffer, "\n");
  979. list_for_each_entry(ti, &timer->open_list_head, open_list)
  980. snd_iprintf(buffer, " Client %s : %s\n",
  981. ti->owner ? ti->owner : "unknown",
  982. ti->flags & (SNDRV_TIMER_IFLG_START |
  983. SNDRV_TIMER_IFLG_RUNNING)
  984. ? "running" : "stopped");
  985. }
  986. mutex_unlock(&register_mutex);
  987. }
  988. static struct snd_info_entry *snd_timer_proc_entry;
  989. static void __init snd_timer_proc_init(void)
  990. {
  991. struct snd_info_entry *entry;
  992. entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
  993. if (entry != NULL) {
  994. entry->c.text.read = snd_timer_proc_read;
  995. if (snd_info_register(entry) < 0) {
  996. snd_info_free_entry(entry);
  997. entry = NULL;
  998. }
  999. }
  1000. snd_timer_proc_entry = entry;
  1001. }
  1002. static void __exit snd_timer_proc_done(void)
  1003. {
  1004. snd_info_free_entry(snd_timer_proc_entry);
  1005. }
  1006. #else /* !CONFIG_PROC_FS */
  1007. #define snd_timer_proc_init()
  1008. #define snd_timer_proc_done()
  1009. #endif
  1010. /*
  1011. * USER SPACE interface
  1012. */
  1013. static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
  1014. unsigned long resolution,
  1015. unsigned long ticks)
  1016. {
  1017. struct snd_timer_user *tu = timeri->callback_data;
  1018. struct snd_timer_read *r;
  1019. int prev;
  1020. spin_lock(&tu->qlock);
  1021. if (tu->qused > 0) {
  1022. prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
  1023. r = &tu->queue[prev];
  1024. if (r->resolution == resolution) {
  1025. r->ticks += ticks;
  1026. goto __wake;
  1027. }
  1028. }
  1029. if (tu->qused >= tu->queue_size) {
  1030. tu->overrun++;
  1031. } else {
  1032. r = &tu->queue[tu->qtail++];
  1033. tu->qtail %= tu->queue_size;
  1034. r->resolution = resolution;
  1035. r->ticks = ticks;
  1036. tu->qused++;
  1037. }
  1038. __wake:
  1039. spin_unlock(&tu->qlock);
  1040. kill_fasync(&tu->fasync, SIGIO, POLL_IN);
  1041. wake_up(&tu->qchange_sleep);
  1042. }
  1043. static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
  1044. struct snd_timer_tread *tread)
  1045. {
  1046. if (tu->qused >= tu->queue_size) {
  1047. tu->overrun++;
  1048. } else {
  1049. memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
  1050. tu->qtail %= tu->queue_size;
  1051. tu->qused++;
  1052. }
  1053. }
  1054. static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
  1055. int event,
  1056. struct timespec *tstamp,
  1057. unsigned long resolution)
  1058. {
  1059. struct snd_timer_user *tu = timeri->callback_data;
  1060. struct snd_timer_tread r1;
  1061. unsigned long flags;
  1062. if (event >= SNDRV_TIMER_EVENT_START &&
  1063. event <= SNDRV_TIMER_EVENT_PAUSE)
  1064. tu->tstamp = *tstamp;
  1065. if ((tu->filter & (1 << event)) == 0 || !tu->tread)
  1066. return;
  1067. r1.event = event;
  1068. r1.tstamp = *tstamp;
  1069. r1.val = resolution;
  1070. spin_lock_irqsave(&tu->qlock, flags);
  1071. snd_timer_user_append_to_tqueue(tu, &r1);
  1072. spin_unlock_irqrestore(&tu->qlock, flags);
  1073. kill_fasync(&tu->fasync, SIGIO, POLL_IN);
  1074. wake_up(&tu->qchange_sleep);
  1075. }
  1076. static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
  1077. unsigned long resolution,
  1078. unsigned long ticks)
  1079. {
  1080. struct snd_timer_user *tu = timeri->callback_data;
  1081. struct snd_timer_tread *r, r1;
  1082. struct timespec tstamp;
  1083. int prev, append = 0;
  1084. memset(&tstamp, 0, sizeof(tstamp));
  1085. spin_lock(&tu->qlock);
  1086. if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
  1087. (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
  1088. spin_unlock(&tu->qlock);
  1089. return;
  1090. }
  1091. if (tu->last_resolution != resolution || ticks > 0) {
  1092. if (timer_tstamp_monotonic)
  1093. do_posix_clock_monotonic_gettime(&tstamp);
  1094. else
  1095. getnstimeofday(&tstamp);
  1096. }
  1097. if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
  1098. tu->last_resolution != resolution) {
  1099. r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
  1100. r1.tstamp = tstamp;
  1101. r1.val = resolution;
  1102. snd_timer_user_append_to_tqueue(tu, &r1);
  1103. tu->last_resolution = resolution;
  1104. append++;
  1105. }
  1106. if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
  1107. goto __wake;
  1108. if (ticks == 0)
  1109. goto __wake;
  1110. if (tu->qused > 0) {
  1111. prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
  1112. r = &tu->tqueue[prev];
  1113. if (r->event == SNDRV_TIMER_EVENT_TICK) {
  1114. r->tstamp = tstamp;
  1115. r->val += ticks;
  1116. append++;
  1117. goto __wake;
  1118. }
  1119. }
  1120. r1.event = SNDRV_TIMER_EVENT_TICK;
  1121. r1.tstamp = tstamp;
  1122. r1.val = ticks;
  1123. snd_timer_user_append_to_tqueue(tu, &r1);
  1124. append++;
  1125. __wake:
  1126. spin_unlock(&tu->qlock);
  1127. if (append == 0)
  1128. return;
  1129. kill_fasync(&tu->fasync, SIGIO, POLL_IN);
  1130. wake_up(&tu->qchange_sleep);
  1131. }
  1132. static int snd_timer_user_open(struct inode *inode, struct file *file)
  1133. {
  1134. struct snd_timer_user *tu;
  1135. int err;
  1136. err = nonseekable_open(inode, file);
  1137. if (err < 0)
  1138. return err;
  1139. tu = kzalloc(sizeof(*tu), GFP_KERNEL);
  1140. if (tu == NULL)
  1141. return -ENOMEM;
  1142. spin_lock_init(&tu->qlock);
  1143. init_waitqueue_head(&tu->qchange_sleep);
  1144. mutex_init(&tu->tread_sem);
  1145. tu->ticks = 1;
  1146. tu->queue_size = 128;
  1147. tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
  1148. GFP_KERNEL);
  1149. if (tu->queue == NULL) {
  1150. kfree(tu);
  1151. return -ENOMEM;
  1152. }
  1153. file->private_data = tu;
  1154. return 0;
  1155. }
  1156. static int snd_timer_user_release(struct inode *inode, struct file *file)
  1157. {
  1158. struct snd_timer_user *tu;
  1159. if (file->private_data) {
  1160. tu = file->private_data;
  1161. file->private_data = NULL;
  1162. if (tu->timeri)
  1163. snd_timer_close(tu->timeri);
  1164. kfree(tu->queue);
  1165. kfree(tu->tqueue);
  1166. kfree(tu);
  1167. }
  1168. return 0;
  1169. }
  1170. static void snd_timer_user_zero_id(struct snd_timer_id *id)
  1171. {
  1172. id->dev_class = SNDRV_TIMER_CLASS_NONE;
  1173. id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  1174. id->card = -1;
  1175. id->device = -1;
  1176. id->subdevice = -1;
  1177. }
  1178. static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
  1179. {
  1180. id->dev_class = timer->tmr_class;
  1181. id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  1182. id->card = timer->card ? timer->card->number : -1;
  1183. id->device = timer->tmr_device;
  1184. id->subdevice = timer->tmr_subdevice;
  1185. }
  1186. static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
  1187. {
  1188. struct snd_timer_id id;
  1189. struct snd_timer *timer;
  1190. struct list_head *p;
  1191. if (copy_from_user(&id, _tid, sizeof(id)))
  1192. return -EFAULT;
  1193. mutex_lock(&register_mutex);
  1194. if (id.dev_class < 0) { /* first item */
  1195. if (list_empty(&snd_timer_list))
  1196. snd_timer_user_zero_id(&id);
  1197. else {
  1198. timer = list_entry(snd_timer_list.next,
  1199. struct snd_timer, device_list);
  1200. snd_timer_user_copy_id(&id, timer);
  1201. }
  1202. } else {
  1203. switch (id.dev_class) {
  1204. case SNDRV_TIMER_CLASS_GLOBAL:
  1205. id.device = id.device < 0 ? 0 : id.device + 1;
  1206. list_for_each(p, &snd_timer_list) {
  1207. timer = list_entry(p, struct snd_timer, device_list);
  1208. if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
  1209. snd_timer_user_copy_id(&id, timer);
  1210. break;
  1211. }
  1212. if (timer->tmr_device >= id.device) {
  1213. snd_timer_user_copy_id(&id, timer);
  1214. break;
  1215. }
  1216. }
  1217. if (p == &snd_timer_list)
  1218. snd_timer_user_zero_id(&id);
  1219. break;
  1220. case SNDRV_TIMER_CLASS_CARD:
  1221. case SNDRV_TIMER_CLASS_PCM:
  1222. if (id.card < 0) {
  1223. id.card = 0;
  1224. } else {
  1225. if (id.card < 0) {
  1226. id.card = 0;
  1227. } else {
  1228. if (id.device < 0) {
  1229. id.device = 0;
  1230. } else {
  1231. if (id.subdevice < 0) {
  1232. id.subdevice = 0;
  1233. } else {
  1234. id.subdevice++;
  1235. }
  1236. }
  1237. }
  1238. }
  1239. list_for_each(p, &snd_timer_list) {
  1240. timer = list_entry(p, struct snd_timer, device_list);
  1241. if (timer->tmr_class > id.dev_class) {
  1242. snd_timer_user_copy_id(&id, timer);
  1243. break;
  1244. }
  1245. if (timer->tmr_class < id.dev_class)
  1246. continue;
  1247. if (timer->card->number > id.card) {
  1248. snd_timer_user_copy_id(&id, timer);
  1249. break;
  1250. }
  1251. if (timer->card->number < id.card)
  1252. continue;
  1253. if (timer->tmr_device > id.device) {
  1254. snd_timer_user_copy_id(&id, timer);
  1255. break;
  1256. }
  1257. if (timer->tmr_device < id.device)
  1258. continue;
  1259. if (timer->tmr_subdevice > id.subdevice) {
  1260. snd_timer_user_copy_id(&id, timer);
  1261. break;
  1262. }
  1263. if (timer->tmr_subdevice < id.subdevice)
  1264. continue;
  1265. snd_timer_user_copy_id(&id, timer);
  1266. break;
  1267. }
  1268. if (p == &snd_timer_list)
  1269. snd_timer_user_zero_id(&id);
  1270. break;
  1271. default:
  1272. snd_timer_user_zero_id(&id);
  1273. }
  1274. }
  1275. mutex_unlock(&register_mutex);
  1276. if (copy_to_user(_tid, &id, sizeof(*_tid)))
  1277. return -EFAULT;
  1278. return 0;
  1279. }
  1280. static int snd_timer_user_ginfo(struct file *file,
  1281. struct snd_timer_ginfo __user *_ginfo)
  1282. {
  1283. struct snd_timer_ginfo *ginfo;
  1284. struct snd_timer_id tid;
  1285. struct snd_timer *t;
  1286. struct list_head *p;
  1287. int err = 0;
  1288. ginfo = memdup_user(_ginfo, sizeof(*ginfo));
  1289. if (IS_ERR(ginfo))
  1290. return PTR_ERR(ginfo);
  1291. tid = ginfo->tid;
  1292. memset(ginfo, 0, sizeof(*ginfo));
  1293. ginfo->tid = tid;
  1294. mutex_lock(&register_mutex);
  1295. t = snd_timer_find(&tid);
  1296. if (t != NULL) {
  1297. ginfo->card = t->card ? t->card->number : -1;
  1298. if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
  1299. ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
  1300. strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
  1301. strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
  1302. ginfo->resolution = t->hw.resolution;
  1303. if (t->hw.resolution_min > 0) {
  1304. ginfo->resolution_min = t->hw.resolution_min;
  1305. ginfo->resolution_max = t->hw.resolution_max;
  1306. }
  1307. list_for_each(p, &t->open_list_head) {
  1308. ginfo->clients++;
  1309. }
  1310. } else {
  1311. err = -ENODEV;
  1312. }
  1313. mutex_unlock(&register_mutex);
  1314. if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
  1315. err = -EFAULT;
  1316. kfree(ginfo);
  1317. return err;
  1318. }
  1319. static int snd_timer_user_gparams(struct file *file,
  1320. struct snd_timer_gparams __user *_gparams)
  1321. {
  1322. struct snd_timer_gparams gparams;
  1323. struct snd_timer *t;
  1324. int err;
  1325. if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
  1326. return -EFAULT;
  1327. mutex_lock(&register_mutex);
  1328. t = snd_timer_find(&gparams.tid);
  1329. if (!t) {
  1330. err = -ENODEV;
  1331. goto _error;
  1332. }
  1333. if (!list_empty(&t->open_list_head)) {
  1334. err = -EBUSY;
  1335. goto _error;
  1336. }
  1337. if (!t->hw.set_period) {
  1338. err = -ENOSYS;
  1339. goto _error;
  1340. }
  1341. err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
  1342. _error:
  1343. mutex_unlock(&register_mutex);
  1344. return err;
  1345. }
  1346. static int snd_timer_user_gstatus(struct file *file,
  1347. struct snd_timer_gstatus __user *_gstatus)
  1348. {
  1349. struct snd_timer_gstatus gstatus;
  1350. struct snd_timer_id tid;
  1351. struct snd_timer *t;
  1352. int err = 0;
  1353. if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
  1354. return -EFAULT;
  1355. tid = gstatus.tid;
  1356. memset(&gstatus, 0, sizeof(gstatus));
  1357. gstatus.tid = tid;
  1358. mutex_lock(&register_mutex);
  1359. t = snd_timer_find(&tid);
  1360. if (t != NULL) {
  1361. if (t->hw.c_resolution)
  1362. gstatus.resolution = t->hw.c_resolution(t);
  1363. else
  1364. gstatus.resolution = t->hw.resolution;
  1365. if (t->hw.precise_resolution) {
  1366. t->hw.precise_resolution(t, &gstatus.resolution_num,
  1367. &gstatus.resolution_den);
  1368. } else {
  1369. gstatus.resolution_num = gstatus.resolution;
  1370. gstatus.resolution_den = 1000000000uL;
  1371. }
  1372. } else {
  1373. err = -ENODEV;
  1374. }
  1375. mutex_unlock(&register_mutex);
  1376. if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
  1377. err = -EFAULT;
  1378. return err;
  1379. }
  1380. static int snd_timer_user_tselect(struct file *file,
  1381. struct snd_timer_select __user *_tselect)
  1382. {
  1383. struct snd_timer_user *tu;
  1384. struct snd_timer_select tselect;
  1385. char str[32];
  1386. int err = 0;
  1387. tu = file->private_data;
  1388. mutex_lock(&tu->tread_sem);
  1389. if (tu->timeri) {
  1390. snd_timer_close(tu->timeri);
  1391. tu->timeri = NULL;
  1392. }
  1393. if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
  1394. err = -EFAULT;
  1395. goto __err;
  1396. }
  1397. sprintf(str, "application %i", current->pid);
  1398. if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
  1399. tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
  1400. err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
  1401. if (err < 0)
  1402. goto __err;
  1403. kfree(tu->queue);
  1404. tu->queue = NULL;
  1405. kfree(tu->tqueue);
  1406. tu->tqueue = NULL;
  1407. if (tu->tread) {
  1408. tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
  1409. GFP_KERNEL);
  1410. if (tu->tqueue == NULL)
  1411. err = -ENOMEM;
  1412. } else {
  1413. tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
  1414. GFP_KERNEL);
  1415. if (tu->queue == NULL)
  1416. err = -ENOMEM;
  1417. }
  1418. if (err < 0) {
  1419. snd_timer_close(tu->timeri);
  1420. tu->timeri = NULL;
  1421. } else {
  1422. tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
  1423. tu->timeri->callback = tu->tread
  1424. ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
  1425. tu->timeri->ccallback = snd_timer_user_ccallback;
  1426. tu->timeri->callback_data = (void *)tu;
  1427. }
  1428. __err:
  1429. mutex_unlock(&tu->tread_sem);
  1430. return err;
  1431. }
  1432. static int snd_timer_user_info(struct file *file,
  1433. struct snd_timer_info __user *_info)
  1434. {
  1435. struct snd_timer_user *tu;
  1436. struct snd_timer_info *info;
  1437. struct snd_timer *t;
  1438. int err = 0;
  1439. tu = file->private_data;
  1440. if (!tu->timeri)
  1441. return -EBADFD;
  1442. t = tu->timeri->timer;
  1443. if (!t)
  1444. return -EBADFD;
  1445. info = kzalloc(sizeof(*info), GFP_KERNEL);
  1446. if (! info)
  1447. return -ENOMEM;
  1448. info->card = t->card ? t->card->number : -1;
  1449. if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
  1450. info->flags |= SNDRV_TIMER_FLG_SLAVE;
  1451. strlcpy(info->id, t->id, sizeof(info->id));
  1452. strlcpy(info->name, t->name, sizeof(info->name));
  1453. info->resolution = t->hw.resolution;
  1454. if (copy_to_user(_info, info, sizeof(*_info)))
  1455. err = -EFAULT;
  1456. kfree(info);
  1457. return err;
  1458. }
  1459. static int snd_timer_user_params(struct file *file,
  1460. struct snd_timer_params __user *_params)
  1461. {
  1462. struct snd_timer_user *tu;
  1463. struct snd_timer_params params;
  1464. struct snd_timer *t;
  1465. struct snd_timer_read *tr;
  1466. struct snd_timer_tread *ttr;
  1467. int err;
  1468. tu = file->private_data;
  1469. if (!tu->timeri)
  1470. return -EBADFD;
  1471. t = tu->timeri->timer;
  1472. if (!t)
  1473. return -EBADFD;
  1474. if (copy_from_user(&params, _params, sizeof(params)))
  1475. return -EFAULT;
  1476. if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
  1477. err = -EINVAL;
  1478. goto _end;
  1479. }
  1480. if (params.queue_size > 0 &&
  1481. (params.queue_size < 32 || params.queue_size > 1024)) {
  1482. err = -EINVAL;
  1483. goto _end;
  1484. }
  1485. if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
  1486. (1<<SNDRV_TIMER_EVENT_TICK)|
  1487. (1<<SNDRV_TIMER_EVENT_START)|
  1488. (1<<SNDRV_TIMER_EVENT_STOP)|
  1489. (1<<SNDRV_TIMER_EVENT_CONTINUE)|
  1490. (1<<SNDRV_TIMER_EVENT_PAUSE)|
  1491. (1<<SNDRV_TIMER_EVENT_SUSPEND)|
  1492. (1<<SNDRV_TIMER_EVENT_RESUME)|
  1493. (1<<SNDRV_TIMER_EVENT_MSTART)|
  1494. (1<<SNDRV_TIMER_EVENT_MSTOP)|
  1495. (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
  1496. (1<<SNDRV_TIMER_EVENT_MPAUSE)|
  1497. (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
  1498. (1<<SNDRV_TIMER_EVENT_MRESUME))) {
  1499. err = -EINVAL;
  1500. goto _end;
  1501. }
  1502. snd_timer_stop(tu->timeri);
  1503. spin_lock_irq(&t->lock);
  1504. tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
  1505. SNDRV_TIMER_IFLG_EXCLUSIVE|
  1506. SNDRV_TIMER_IFLG_EARLY_EVENT);
  1507. if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
  1508. tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
  1509. if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
  1510. tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
  1511. if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
  1512. tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
  1513. spin_unlock_irq(&t->lock);
  1514. if (params.queue_size > 0 &&
  1515. (unsigned int)tu->queue_size != params.queue_size) {
  1516. if (tu->tread) {
  1517. ttr = kmalloc(params.queue_size * sizeof(*ttr),
  1518. GFP_KERNEL);
  1519. if (ttr) {
  1520. kfree(tu->tqueue);
  1521. tu->queue_size = params.queue_size;
  1522. tu->tqueue = ttr;
  1523. }
  1524. } else {
  1525. tr = kmalloc(params.queue_size * sizeof(*tr),
  1526. GFP_KERNEL);
  1527. if (tr) {
  1528. kfree(tu->queue);
  1529. tu->queue_size = params.queue_size;
  1530. tu->queue = tr;
  1531. }
  1532. }
  1533. }
  1534. tu->qhead = tu->qtail = tu->qused = 0;
  1535. if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
  1536. if (tu->tread) {
  1537. struct snd_timer_tread tread;
  1538. tread.event = SNDRV_TIMER_EVENT_EARLY;
  1539. tread.tstamp.tv_sec = 0;
  1540. tread.tstamp.tv_nsec = 0;
  1541. tread.val = 0;
  1542. snd_timer_user_append_to_tqueue(tu, &tread);
  1543. } else {
  1544. struct snd_timer_read *r = &tu->queue[0];
  1545. r->resolution = 0;
  1546. r->ticks = 0;
  1547. tu->qused++;
  1548. tu->qtail++;
  1549. }
  1550. }
  1551. tu->filter = params.filter;
  1552. tu->ticks = params.ticks;
  1553. err = 0;
  1554. _end:
  1555. if (copy_to_user(_params, &params, sizeof(params)))
  1556. return -EFAULT;
  1557. return err;
  1558. }
  1559. static int snd_timer_user_status(struct file *file,
  1560. struct snd_timer_status __user *_status)
  1561. {
  1562. struct snd_timer_user *tu;
  1563. struct snd_timer_status status;
  1564. tu = file->private_data;
  1565. if (!tu->timeri)
  1566. return -EBADFD;
  1567. memset(&status, 0, sizeof(status));
  1568. status.tstamp = tu->tstamp;
  1569. status.resolution = snd_timer_resolution(tu->timeri);
  1570. status.lost = tu->timeri->lost;
  1571. status.overrun = tu->overrun;
  1572. spin_lock_irq(&tu->qlock);
  1573. status.queue = tu->qused;
  1574. spin_unlock_irq(&tu->qlock);
  1575. if (copy_to_user(_status, &status, sizeof(status)))
  1576. return -EFAULT;
  1577. return 0;
  1578. }
  1579. static int snd_timer_user_start(struct file *file)
  1580. {
  1581. int err;
  1582. struct snd_timer_user *tu;
  1583. tu = file->private_data;
  1584. if (!tu->timeri)
  1585. return -EBADFD;
  1586. snd_timer_stop(tu->timeri);
  1587. tu->timeri->lost = 0;
  1588. tu->last_resolution = 0;
  1589. return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
  1590. }
  1591. static int snd_timer_user_stop(struct file *file)
  1592. {
  1593. int err;
  1594. struct snd_timer_user *tu;
  1595. tu = file->private_data;
  1596. if (!tu->timeri)
  1597. return -EBADFD;
  1598. return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
  1599. }
  1600. static int snd_timer_user_continue(struct file *file)
  1601. {
  1602. int err;
  1603. struct snd_timer_user *tu;
  1604. tu = file->private_data;
  1605. if (!tu->timeri)
  1606. return -EBADFD;
  1607. tu->timeri->lost = 0;
  1608. return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
  1609. }
  1610. static int snd_timer_user_pause(struct file *file)
  1611. {
  1612. int err;
  1613. struct snd_timer_user *tu;
  1614. tu = file->private_data;
  1615. if (!tu->timeri)
  1616. return -EBADFD;
  1617. return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
  1618. }
  1619. enum {
  1620. SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
  1621. SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
  1622. SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
  1623. SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
  1624. };
  1625. static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
  1626. unsigned long arg)
  1627. {
  1628. struct snd_timer_user *tu;
  1629. void __user *argp = (void __user *)arg;
  1630. int __user *p = argp;
  1631. tu = file->private_data;
  1632. switch (cmd) {
  1633. case SNDRV_TIMER_IOCTL_PVERSION:
  1634. return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
  1635. case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
  1636. return snd_timer_user_next_device(argp);
  1637. case SNDRV_TIMER_IOCTL_TREAD:
  1638. {
  1639. int xarg;
  1640. mutex_lock(&tu->tread_sem);
  1641. if (tu->timeri) { /* too late */
  1642. mutex_unlock(&tu->tread_sem);
  1643. return -EBUSY;
  1644. }
  1645. if (get_user(xarg, p)) {
  1646. mutex_unlock(&tu->tread_sem);
  1647. return -EFAULT;
  1648. }
  1649. tu->tread = xarg ? 1 : 0;
  1650. mutex_unlock(&tu->tread_sem);
  1651. return 0;
  1652. }
  1653. case SNDRV_TIMER_IOCTL_GINFO:
  1654. return snd_timer_user_ginfo(file, argp);
  1655. case SNDRV_TIMER_IOCTL_GPARAMS:
  1656. return snd_timer_user_gparams(file, argp);
  1657. case SNDRV_TIMER_IOCTL_GSTATUS:
  1658. return snd_timer_user_gstatus(file, argp);
  1659. case SNDRV_TIMER_IOCTL_SELECT:
  1660. return snd_timer_user_tselect(file, argp);
  1661. case SNDRV_TIMER_IOCTL_INFO:
  1662. return snd_timer_user_info(file, argp);
  1663. case SNDRV_TIMER_IOCTL_PARAMS:
  1664. return snd_timer_user_params(file, argp);
  1665. case SNDRV_TIMER_IOCTL_STATUS:
  1666. return snd_timer_user_status(file, argp);
  1667. case SNDRV_TIMER_IOCTL_START:
  1668. case SNDRV_TIMER_IOCTL_START_OLD:
  1669. return snd_timer_user_start(file);
  1670. case SNDRV_TIMER_IOCTL_STOP:
  1671. case SNDRV_TIMER_IOCTL_STOP_OLD:
  1672. return snd_timer_user_stop(file);
  1673. case SNDRV_TIMER_IOCTL_CONTINUE:
  1674. case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
  1675. return snd_timer_user_continue(file);
  1676. case SNDRV_TIMER_IOCTL_PAUSE:
  1677. case SNDRV_TIMER_IOCTL_PAUSE_OLD:
  1678. return snd_timer_user_pause(file);
  1679. }
  1680. return -ENOTTY;
  1681. }
  1682. static int snd_timer_user_fasync(int fd, struct file * file, int on)
  1683. {
  1684. struct snd_timer_user *tu;
  1685. tu = file->private_data;
  1686. return fasync_helper(fd, file, on, &tu->fasync);
  1687. }
  1688. static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
  1689. size_t count, loff_t *offset)
  1690. {
  1691. struct snd_timer_user *tu;
  1692. long result = 0, unit;
  1693. int err = 0;
  1694. tu = file->private_data;
  1695. unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
  1696. spin_lock_irq(&tu->qlock);
  1697. while ((long)count - result >= unit) {
  1698. while (!tu->qused) {
  1699. wait_queue_t wait;
  1700. if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
  1701. err = -EAGAIN;
  1702. break;
  1703. }
  1704. set_current_state(TASK_INTERRUPTIBLE);
  1705. init_waitqueue_entry(&wait, current);
  1706. add_wait_queue(&tu->qchange_sleep, &wait);
  1707. spin_unlock_irq(&tu->qlock);
  1708. schedule();
  1709. spin_lock_irq(&tu->qlock);
  1710. remove_wait_queue(&tu->qchange_sleep, &wait);
  1711. if (signal_pending(current)) {
  1712. err = -ERESTARTSYS;
  1713. break;
  1714. }
  1715. }
  1716. spin_unlock_irq(&tu->qlock);
  1717. if (err < 0)
  1718. goto _error;
  1719. if (tu->tread) {
  1720. if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
  1721. sizeof(struct snd_timer_tread))) {
  1722. err = -EFAULT;
  1723. goto _error;
  1724. }
  1725. } else {
  1726. if (copy_to_user(buffer, &tu->queue[tu->qhead++],
  1727. sizeof(struct snd_timer_read))) {
  1728. err = -EFAULT;
  1729. goto _error;
  1730. }
  1731. }
  1732. tu->qhead %= tu->queue_size;
  1733. result += unit;
  1734. buffer += unit;
  1735. spin_lock_irq(&tu->qlock);
  1736. tu->qused--;
  1737. }
  1738. spin_unlock_irq(&tu->qlock);
  1739. _error:
  1740. return result > 0 ? result : err;
  1741. }
  1742. static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
  1743. {
  1744. unsigned int mask;
  1745. struct snd_timer_user *tu;
  1746. tu = file->private_data;
  1747. poll_wait(file, &tu->qchange_sleep, wait);
  1748. mask = 0;
  1749. if (tu->qused)
  1750. ma