timer.c 51 KB

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