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