timer.c 52 KB

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