timer.c 51 KB

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