timer.c 50 KB

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