timer.c 54 KB

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