timer.c 51 KB

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