timer.c 51 KB

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