timer.c 51 KB

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