timer.c 53 KB

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