timer.c 51 KB

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