timer.c 51 KB

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