timer.c 53 KB

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