timer.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008
  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_unregister(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. spin_lock(&timer->lock);
  581. /* now process all callbacks */
  582. while (!list_empty(&timer->sack_list_head)) {
  583. p = timer->sack_list_head.next; /* get first item */
  584. ti = list_entry(p, struct snd_timer_instance, ack_list);
  585. /* remove from ack_list and make empty */
  586. list_del_init(p);
  587. ticks = ti->pticks;
  588. ti->pticks = 0;
  589. resolution = ti->resolution;
  590. ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
  591. spin_unlock(&timer->lock);
  592. if (ti->callback)
  593. ti->callback(ti, resolution, ticks);
  594. spin_lock(&timer->lock);
  595. ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
  596. }
  597. spin_unlock(&timer->lock);
  598. }
  599. /*
  600. * timer interrupt
  601. *
  602. * ticks_left is usually equal to timer->sticks.
  603. *
  604. */
  605. void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
  606. {
  607. struct snd_timer_instance *ti, *ts;
  608. unsigned long resolution, ticks;
  609. struct list_head *p, *q, *n, *ack_list_head;
  610. unsigned long flags;
  611. int use_tasklet = 0;
  612. if (timer == NULL)
  613. return;
  614. spin_lock_irqsave(&timer->lock, flags);
  615. /* remember the current resolution */
  616. if (timer->hw.c_resolution)
  617. resolution = timer->hw.c_resolution(timer);
  618. else
  619. resolution = timer->hw.resolution;
  620. /* loop for all active instances
  621. * Here we cannot use list_for_each because the active_list of a
  622. * processed instance is relinked to done_list_head before the callback
  623. * is called.
  624. */
  625. list_for_each_safe(p, n, &timer->active_list_head) {
  626. ti = list_entry(p, struct snd_timer_instance, active_list);
  627. if (!(ti->flags & SNDRV_TIMER_IFLG_RUNNING))
  628. continue;
  629. ti->pticks += ticks_left;
  630. ti->resolution = resolution;
  631. if (ti->cticks < ticks_left)
  632. ti->cticks = 0;
  633. else
  634. ti->cticks -= ticks_left;
  635. if (ti->cticks) /* not expired */
  636. continue;
  637. if (ti->flags & SNDRV_TIMER_IFLG_AUTO) {
  638. ti->cticks = ti->ticks;
  639. } else {
  640. ti->flags &= ~SNDRV_TIMER_IFLG_RUNNING;
  641. if (--timer->running)
  642. list_del(p);
  643. }
  644. if ((timer->hw.flags & SNDRV_TIMER_HW_TASKLET) ||
  645. (ti->flags & SNDRV_TIMER_IFLG_FAST))
  646. ack_list_head = &timer->ack_list_head;
  647. else
  648. ack_list_head = &timer->sack_list_head;
  649. if (list_empty(&ti->ack_list))
  650. list_add_tail(&ti->ack_list, ack_list_head);
  651. list_for_each(q, &ti->slave_active_head) {
  652. ts = list_entry(q, struct snd_timer_instance, active_list);
  653. ts->pticks = ti->pticks;
  654. ts->resolution = resolution;
  655. if (list_empty(&ts->ack_list))
  656. list_add_tail(&ts->ack_list, ack_list_head);
  657. }
  658. }
  659. if (timer->flags & SNDRV_TIMER_FLG_RESCHED)
  660. snd_timer_reschedule(timer, ticks_left);
  661. if (timer->running) {
  662. if (timer->hw.flags & SNDRV_TIMER_HW_STOP) {
  663. timer->hw.stop(timer);
  664. timer->flags |= SNDRV_TIMER_FLG_CHANGE;
  665. }
  666. if (!(timer->hw.flags & SNDRV_TIMER_HW_AUTO) ||
  667. (timer->flags & SNDRV_TIMER_FLG_CHANGE)) {
  668. /* restart timer */
  669. timer->flags &= ~SNDRV_TIMER_FLG_CHANGE;
  670. timer->hw.start(timer);
  671. }
  672. } else {
  673. timer->hw.stop(timer);
  674. }
  675. /* now process all fast callbacks */
  676. while (!list_empty(&timer->ack_list_head)) {
  677. p = timer->ack_list_head.next; /* get first item */
  678. ti = list_entry(p, struct snd_timer_instance, ack_list);
  679. /* remove from ack_list and make empty */
  680. list_del_init(p);
  681. ticks = ti->pticks;
  682. ti->pticks = 0;
  683. ti->flags |= SNDRV_TIMER_IFLG_CALLBACK;
  684. spin_unlock(&timer->lock);
  685. if (ti->callback)
  686. ti->callback(ti, resolution, ticks);
  687. spin_lock(&timer->lock);
  688. ti->flags &= ~SNDRV_TIMER_IFLG_CALLBACK;
  689. }
  690. /* do we have any slow callbacks? */
  691. use_tasklet = !list_empty(&timer->sack_list_head);
  692. spin_unlock_irqrestore(&timer->lock, flags);
  693. if (use_tasklet)
  694. tasklet_hi_schedule(&timer->task_queue);
  695. }
  696. /*
  697. */
  698. int snd_timer_new(struct snd_card *card, char *id, struct snd_timer_id *tid,
  699. struct snd_timer **rtimer)
  700. {
  701. struct snd_timer *timer;
  702. int err;
  703. static struct snd_device_ops ops = {
  704. .dev_free = snd_timer_dev_free,
  705. .dev_register = snd_timer_dev_register,
  706. .dev_unregister = snd_timer_dev_unregister
  707. };
  708. snd_assert(tid != NULL, return -EINVAL);
  709. snd_assert(rtimer != NULL, return -EINVAL);
  710. *rtimer = NULL;
  711. timer = kzalloc(sizeof(*timer), GFP_KERNEL);
  712. if (timer == NULL) {
  713. snd_printk(KERN_ERR "timer: cannot allocate\n");
  714. return -ENOMEM;
  715. }
  716. timer->tmr_class = tid->dev_class;
  717. timer->card = card;
  718. timer->tmr_device = tid->device;
  719. timer->tmr_subdevice = tid->subdevice;
  720. if (id)
  721. strlcpy(timer->id, id, sizeof(timer->id));
  722. INIT_LIST_HEAD(&timer->device_list);
  723. INIT_LIST_HEAD(&timer->open_list_head);
  724. INIT_LIST_HEAD(&timer->active_list_head);
  725. INIT_LIST_HEAD(&timer->ack_list_head);
  726. INIT_LIST_HEAD(&timer->sack_list_head);
  727. spin_lock_init(&timer->lock);
  728. tasklet_init(&timer->task_queue, snd_timer_tasklet,
  729. (unsigned long)timer);
  730. if (card != NULL) {
  731. timer->module = card->module;
  732. err = snd_device_new(card, SNDRV_DEV_TIMER, timer, &ops);
  733. if (err < 0) {
  734. snd_timer_free(timer);
  735. return err;
  736. }
  737. }
  738. *rtimer = timer;
  739. return 0;
  740. }
  741. static int snd_timer_free(struct snd_timer *timer)
  742. {
  743. snd_assert(timer != NULL, return -ENXIO);
  744. if (timer->private_free)
  745. timer->private_free(timer);
  746. kfree(timer);
  747. return 0;
  748. }
  749. static int snd_timer_dev_free(struct snd_device *device)
  750. {
  751. struct snd_timer *timer = device->device_data;
  752. return snd_timer_free(timer);
  753. }
  754. static int snd_timer_dev_register(struct snd_device *dev)
  755. {
  756. struct snd_timer *timer = dev->device_data;
  757. struct snd_timer *timer1;
  758. struct list_head *p;
  759. snd_assert(timer != NULL && timer->hw.start != NULL &&
  760. timer->hw.stop != NULL, return -ENXIO);
  761. if (!(timer->hw.flags & SNDRV_TIMER_HW_SLAVE) &&
  762. !timer->hw.resolution && timer->hw.c_resolution == NULL)
  763. return -EINVAL;
  764. mutex_lock(&register_mutex);
  765. list_for_each(p, &snd_timer_list) {
  766. timer1 = list_entry(p, struct snd_timer, device_list);
  767. if (timer1->tmr_class > timer->tmr_class)
  768. break;
  769. if (timer1->tmr_class < timer->tmr_class)
  770. continue;
  771. if (timer1->card && timer->card) {
  772. if (timer1->card->number > timer->card->number)
  773. break;
  774. if (timer1->card->number < timer->card->number)
  775. continue;
  776. }
  777. if (timer1->tmr_device > timer->tmr_device)
  778. break;
  779. if (timer1->tmr_device < timer->tmr_device)
  780. continue;
  781. if (timer1->tmr_subdevice > timer->tmr_subdevice)
  782. break;
  783. if (timer1->tmr_subdevice < timer->tmr_subdevice)
  784. continue;
  785. /* conflicts.. */
  786. mutex_unlock(&register_mutex);
  787. return -EBUSY;
  788. }
  789. list_add_tail(&timer->device_list, p);
  790. mutex_unlock(&register_mutex);
  791. return 0;
  792. }
  793. static int snd_timer_unregister(struct snd_timer *timer)
  794. {
  795. struct list_head *p, *n;
  796. struct snd_timer_instance *ti;
  797. snd_assert(timer != NULL, return -ENXIO);
  798. mutex_lock(&register_mutex);
  799. if (! list_empty(&timer->open_list_head)) {
  800. snd_printk(KERN_WARNING "timer 0x%lx is busy?\n", (long)timer);
  801. list_for_each_safe(p, n, &timer->open_list_head) {
  802. list_del_init(p);
  803. ti = list_entry(p, struct snd_timer_instance, open_list);
  804. ti->timer = NULL;
  805. }
  806. }
  807. list_del(&timer->device_list);
  808. mutex_unlock(&register_mutex);
  809. return snd_timer_free(timer);
  810. }
  811. static int snd_timer_dev_unregister(struct snd_device *device)
  812. {
  813. struct snd_timer *timer = device->device_data;
  814. return snd_timer_unregister(timer);
  815. }
  816. void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstamp)
  817. {
  818. unsigned long flags;
  819. unsigned long resolution = 0;
  820. struct snd_timer_instance *ti, *ts;
  821. struct list_head *p, *n;
  822. if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
  823. return;
  824. snd_assert(event >= SNDRV_TIMER_EVENT_MSTART &&
  825. event <= SNDRV_TIMER_EVENT_MRESUME, return);
  826. spin_lock_irqsave(&timer->lock, flags);
  827. if (event == SNDRV_TIMER_EVENT_MSTART ||
  828. event == SNDRV_TIMER_EVENT_MCONTINUE ||
  829. event == SNDRV_TIMER_EVENT_MRESUME) {
  830. if (timer->hw.c_resolution)
  831. resolution = timer->hw.c_resolution(timer);
  832. else
  833. resolution = timer->hw.resolution;
  834. }
  835. list_for_each(p, &timer->active_list_head) {
  836. ti = list_entry(p, struct snd_timer_instance, active_list);
  837. if (ti->ccallback)
  838. ti->ccallback(ti, event, tstamp, resolution);
  839. list_for_each(n, &ti->slave_active_head) {
  840. ts = list_entry(n, struct snd_timer_instance, active_list);
  841. if (ts->ccallback)
  842. ts->ccallback(ts, event, tstamp, resolution);
  843. }
  844. }
  845. spin_unlock_irqrestore(&timer->lock, flags);
  846. }
  847. /*
  848. * exported functions for global timers
  849. */
  850. int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer)
  851. {
  852. struct snd_timer_id tid;
  853. tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
  854. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  855. tid.card = -1;
  856. tid.device = device;
  857. tid.subdevice = 0;
  858. return snd_timer_new(NULL, id, &tid, rtimer);
  859. }
  860. int snd_timer_global_free(struct snd_timer *timer)
  861. {
  862. return snd_timer_free(timer);
  863. }
  864. int snd_timer_global_register(struct snd_timer *timer)
  865. {
  866. struct snd_device dev;
  867. memset(&dev, 0, sizeof(dev));
  868. dev.device_data = timer;
  869. return snd_timer_dev_register(&dev);
  870. }
  871. int snd_timer_global_unregister(struct snd_timer *timer)
  872. {
  873. return snd_timer_unregister(timer);
  874. }
  875. /*
  876. * System timer
  877. */
  878. struct snd_timer_system_private {
  879. struct timer_list tlist;
  880. struct timer * timer;
  881. unsigned long last_expires;
  882. unsigned long last_jiffies;
  883. unsigned long correction;
  884. };
  885. static void snd_timer_s_function(unsigned long data)
  886. {
  887. struct snd_timer *timer = (struct snd_timer *)data;
  888. struct snd_timer_system_private *priv = timer->private_data;
  889. unsigned long jiff = jiffies;
  890. if (time_after(jiff, priv->last_expires))
  891. priv->correction = (long)jiff - (long)priv->last_expires;
  892. snd_timer_interrupt(timer, (long)jiff - (long)priv->last_jiffies);
  893. }
  894. static int snd_timer_s_start(struct snd_timer * timer)
  895. {
  896. struct snd_timer_system_private *priv;
  897. unsigned long njiff;
  898. priv = (struct snd_timer_system_private *) timer->private_data;
  899. njiff = (priv->last_jiffies = jiffies);
  900. if (priv->correction > timer->sticks - 1) {
  901. priv->correction -= timer->sticks - 1;
  902. njiff++;
  903. } else {
  904. njiff += timer->sticks - priv->correction;
  905. priv->correction -= timer->sticks;
  906. }
  907. priv->last_expires = priv->tlist.expires = njiff;
  908. add_timer(&priv->tlist);
  909. return 0;
  910. }
  911. static int snd_timer_s_stop(struct snd_timer * timer)
  912. {
  913. struct snd_timer_system_private *priv;
  914. unsigned long jiff;
  915. priv = (struct snd_timer_system_private *) timer->private_data;
  916. del_timer(&priv->tlist);
  917. jiff = jiffies;
  918. if (time_before(jiff, priv->last_expires))
  919. timer->sticks = priv->last_expires - jiff;
  920. else
  921. timer->sticks = 1;
  922. return 0;
  923. }
  924. static struct snd_timer_hardware snd_timer_system =
  925. {
  926. .flags = SNDRV_TIMER_HW_FIRST | SNDRV_TIMER_HW_TASKLET,
  927. .resolution = 1000000000L / HZ,
  928. .ticks = 10000000L,
  929. .start = snd_timer_s_start,
  930. .stop = snd_timer_s_stop
  931. };
  932. static void snd_timer_free_system(struct snd_timer *timer)
  933. {
  934. kfree(timer->private_data);
  935. }
  936. static int snd_timer_register_system(void)
  937. {
  938. struct snd_timer *timer;
  939. struct snd_timer_system_private *priv;
  940. int err;
  941. err = snd_timer_global_new("system", SNDRV_TIMER_GLOBAL_SYSTEM, &timer);
  942. if (err < 0)
  943. return err;
  944. strcpy(timer->name, "system timer");
  945. timer->hw = snd_timer_system;
  946. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  947. if (priv == NULL) {
  948. snd_timer_free(timer);
  949. return -ENOMEM;
  950. }
  951. init_timer(&priv->tlist);
  952. priv->tlist.function = snd_timer_s_function;
  953. priv->tlist.data = (unsigned long) timer;
  954. timer->private_data = priv;
  955. timer->private_free = snd_timer_free_system;
  956. return snd_timer_global_register(timer);
  957. }
  958. #ifdef CONFIG_PROC_FS
  959. /*
  960. * Info interface
  961. */
  962. static void snd_timer_proc_read(struct snd_info_entry *entry,
  963. struct snd_info_buffer *buffer)
  964. {
  965. struct snd_timer *timer;
  966. struct snd_timer_instance *ti;
  967. struct list_head *p, *q;
  968. mutex_lock(&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. list_for_each(q, &timer->open_list_head) {
  998. ti = list_entry(q, struct snd_timer_instance, open_list);
  999. snd_iprintf(buffer, " Client %s : %s\n",
  1000. ti->owner ? ti->owner : "unknown",
  1001. ti->flags & (SNDRV_TIMER_IFLG_START |
  1002. SNDRV_TIMER_IFLG_RUNNING)
  1003. ? "running" : "stopped");
  1004. }
  1005. }
  1006. mutex_unlock(&register_mutex);
  1007. }
  1008. static struct snd_info_entry *snd_timer_proc_entry;
  1009. static void __init snd_timer_proc_init(void)
  1010. {
  1011. struct snd_info_entry *entry;
  1012. entry = snd_info_create_module_entry(THIS_MODULE, "timers", NULL);
  1013. if (entry != NULL) {
  1014. entry->c.text.read = snd_timer_proc_read;
  1015. if (snd_info_register(entry) < 0) {
  1016. snd_info_free_entry(entry);
  1017. entry = NULL;
  1018. }
  1019. }
  1020. snd_timer_proc_entry = entry;
  1021. }
  1022. static void __exit snd_timer_proc_done(void)
  1023. {
  1024. snd_info_unregister(snd_timer_proc_entry);
  1025. }
  1026. #else /* !CONFIG_PROC_FS */
  1027. #define snd_timer_proc_init()
  1028. #define snd_timer_proc_done()
  1029. #endif
  1030. /*
  1031. * USER SPACE interface
  1032. */
  1033. static void snd_timer_user_interrupt(struct snd_timer_instance *timeri,
  1034. unsigned long resolution,
  1035. unsigned long ticks)
  1036. {
  1037. struct snd_timer_user *tu = timeri->callback_data;
  1038. struct snd_timer_read *r;
  1039. int prev;
  1040. spin_lock(&tu->qlock);
  1041. if (tu->qused > 0) {
  1042. prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
  1043. r = &tu->queue[prev];
  1044. if (r->resolution == resolution) {
  1045. r->ticks += ticks;
  1046. goto __wake;
  1047. }
  1048. }
  1049. if (tu->qused >= tu->queue_size) {
  1050. tu->overrun++;
  1051. } else {
  1052. r = &tu->queue[tu->qtail++];
  1053. tu->qtail %= tu->queue_size;
  1054. r->resolution = resolution;
  1055. r->ticks = ticks;
  1056. tu->qused++;
  1057. }
  1058. __wake:
  1059. spin_unlock(&tu->qlock);
  1060. kill_fasync(&tu->fasync, SIGIO, POLL_IN);
  1061. wake_up(&tu->qchange_sleep);
  1062. }
  1063. static void snd_timer_user_append_to_tqueue(struct snd_timer_user *tu,
  1064. struct snd_timer_tread *tread)
  1065. {
  1066. if (tu->qused >= tu->queue_size) {
  1067. tu->overrun++;
  1068. } else {
  1069. memcpy(&tu->tqueue[tu->qtail++], tread, sizeof(*tread));
  1070. tu->qtail %= tu->queue_size;
  1071. tu->qused++;
  1072. }
  1073. }
  1074. static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
  1075. int event,
  1076. struct timespec *tstamp,
  1077. unsigned long resolution)
  1078. {
  1079. struct snd_timer_user *tu = timeri->callback_data;
  1080. struct snd_timer_tread r1;
  1081. if (event >= SNDRV_TIMER_EVENT_START &&
  1082. event <= SNDRV_TIMER_EVENT_PAUSE)
  1083. tu->tstamp = *tstamp;
  1084. if ((tu->filter & (1 << event)) == 0 || !tu->tread)
  1085. return;
  1086. r1.event = event;
  1087. r1.tstamp = *tstamp;
  1088. r1.val = resolution;
  1089. spin_lock(&tu->qlock);
  1090. snd_timer_user_append_to_tqueue(tu, &r1);
  1091. spin_unlock(&tu->qlock);
  1092. kill_fasync(&tu->fasync, SIGIO, POLL_IN);
  1093. wake_up(&tu->qchange_sleep);
  1094. }
  1095. static void snd_timer_user_tinterrupt(struct snd_timer_instance *timeri,
  1096. unsigned long resolution,
  1097. unsigned long ticks)
  1098. {
  1099. struct snd_timer_user *tu = timeri->callback_data;
  1100. struct snd_timer_tread *r, r1;
  1101. struct timespec tstamp;
  1102. int prev, append = 0;
  1103. memset(&tstamp, 0, sizeof(tstamp));
  1104. spin_lock(&tu->qlock);
  1105. if ((tu->filter & ((1 << SNDRV_TIMER_EVENT_RESOLUTION) |
  1106. (1 << SNDRV_TIMER_EVENT_TICK))) == 0) {
  1107. spin_unlock(&tu->qlock);
  1108. return;
  1109. }
  1110. if (tu->last_resolution != resolution || ticks > 0)
  1111. getnstimeofday(&tstamp);
  1112. if ((tu->filter & (1 << SNDRV_TIMER_EVENT_RESOLUTION)) &&
  1113. tu->last_resolution != resolution) {
  1114. r1.event = SNDRV_TIMER_EVENT_RESOLUTION;
  1115. r1.tstamp = tstamp;
  1116. r1.val = resolution;
  1117. snd_timer_user_append_to_tqueue(tu, &r1);
  1118. tu->last_resolution = resolution;
  1119. append++;
  1120. }
  1121. if ((tu->filter & (1 << SNDRV_TIMER_EVENT_TICK)) == 0)
  1122. goto __wake;
  1123. if (ticks == 0)
  1124. goto __wake;
  1125. if (tu->qused > 0) {
  1126. prev = tu->qtail == 0 ? tu->queue_size - 1 : tu->qtail - 1;
  1127. r = &tu->tqueue[prev];
  1128. if (r->event == SNDRV_TIMER_EVENT_TICK) {
  1129. r->tstamp = tstamp;
  1130. r->val += ticks;
  1131. append++;
  1132. goto __wake;
  1133. }
  1134. }
  1135. r1.event = SNDRV_TIMER_EVENT_TICK;
  1136. r1.tstamp = tstamp;
  1137. r1.val = ticks;
  1138. snd_timer_user_append_to_tqueue(tu, &r1);
  1139. append++;
  1140. __wake:
  1141. spin_unlock(&tu->qlock);
  1142. if (append == 0)
  1143. return;
  1144. kill_fasync(&tu->fasync, SIGIO, POLL_IN);
  1145. wake_up(&tu->qchange_sleep);
  1146. }
  1147. static int snd_timer_user_open(struct inode *inode, struct file *file)
  1148. {
  1149. struct snd_timer_user *tu;
  1150. tu = kzalloc(sizeof(*tu), GFP_KERNEL);
  1151. if (tu == NULL)
  1152. return -ENOMEM;
  1153. spin_lock_init(&tu->qlock);
  1154. init_waitqueue_head(&tu->qchange_sleep);
  1155. mutex_init(&tu->tread_sem);
  1156. tu->ticks = 1;
  1157. tu->queue_size = 128;
  1158. tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
  1159. GFP_KERNEL);
  1160. if (tu->queue == NULL) {
  1161. kfree(tu);
  1162. return -ENOMEM;
  1163. }
  1164. file->private_data = tu;
  1165. return 0;
  1166. }
  1167. static int snd_timer_user_release(struct inode *inode, struct file *file)
  1168. {
  1169. struct snd_timer_user *tu;
  1170. if (file->private_data) {
  1171. tu = file->private_data;
  1172. file->private_data = NULL;
  1173. fasync_helper(-1, file, 0, &tu->fasync);
  1174. if (tu->timeri)
  1175. snd_timer_close(tu->timeri);
  1176. kfree(tu->queue);
  1177. kfree(tu->tqueue);
  1178. kfree(tu);
  1179. }
  1180. return 0;
  1181. }
  1182. static void snd_timer_user_zero_id(struct snd_timer_id *id)
  1183. {
  1184. id->dev_class = SNDRV_TIMER_CLASS_NONE;
  1185. id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  1186. id->card = -1;
  1187. id->device = -1;
  1188. id->subdevice = -1;
  1189. }
  1190. static void snd_timer_user_copy_id(struct snd_timer_id *id, struct snd_timer *timer)
  1191. {
  1192. id->dev_class = timer->tmr_class;
  1193. id->dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  1194. id->card = timer->card ? timer->card->number : -1;
  1195. id->device = timer->tmr_device;
  1196. id->subdevice = timer->tmr_subdevice;
  1197. }
  1198. static int snd_timer_user_next_device(struct snd_timer_id __user *_tid)
  1199. {
  1200. struct snd_timer_id id;
  1201. struct snd_timer *timer;
  1202. struct list_head *p;
  1203. if (copy_from_user(&id, _tid, sizeof(id)))
  1204. return -EFAULT;
  1205. mutex_lock(&register_mutex);
  1206. if (id.dev_class < 0) { /* first item */
  1207. if (list_empty(&snd_timer_list))
  1208. snd_timer_user_zero_id(&id);
  1209. else {
  1210. timer = list_entry(snd_timer_list.next,
  1211. struct snd_timer, device_list);
  1212. snd_timer_user_copy_id(&id, timer);
  1213. }
  1214. } else {
  1215. switch (id.dev_class) {
  1216. case SNDRV_TIMER_CLASS_GLOBAL:
  1217. id.device = id.device < 0 ? 0 : id.device + 1;
  1218. list_for_each(p, &snd_timer_list) {
  1219. timer = list_entry(p, struct snd_timer, device_list);
  1220. if (timer->tmr_class > SNDRV_TIMER_CLASS_GLOBAL) {
  1221. snd_timer_user_copy_id(&id, timer);
  1222. break;
  1223. }
  1224. if (timer->tmr_device >= id.device) {
  1225. snd_timer_user_copy_id(&id, timer);
  1226. break;
  1227. }
  1228. }
  1229. if (p == &snd_timer_list)
  1230. snd_timer_user_zero_id(&id);
  1231. break;
  1232. case SNDRV_TIMER_CLASS_CARD:
  1233. case SNDRV_TIMER_CLASS_PCM:
  1234. if (id.card < 0) {
  1235. id.card = 0;
  1236. } else {
  1237. if (id.card < 0) {
  1238. id.card = 0;
  1239. } else {
  1240. if (id.device < 0) {
  1241. id.device = 0;
  1242. } else {
  1243. if (id.subdevice < 0) {
  1244. id.subdevice = 0;
  1245. } else {
  1246. id.subdevice++;
  1247. }
  1248. }
  1249. }
  1250. }
  1251. list_for_each(p, &snd_timer_list) {
  1252. timer = list_entry(p, struct snd_timer, device_list);
  1253. if (timer->tmr_class > id.dev_class) {
  1254. snd_timer_user_copy_id(&id, timer);
  1255. break;
  1256. }
  1257. if (timer->tmr_class < id.dev_class)
  1258. continue;
  1259. if (timer->card->number > id.card) {
  1260. snd_timer_user_copy_id(&id, timer);
  1261. break;
  1262. }
  1263. if (timer->card->number < id.card)
  1264. continue;
  1265. if (timer->tmr_device > id.device) {
  1266. snd_timer_user_copy_id(&id, timer);
  1267. break;
  1268. }
  1269. if (timer->tmr_device < id.device)
  1270. continue;
  1271. if (timer->tmr_subdevice > id.subdevice) {
  1272. snd_timer_user_copy_id(&id, timer);
  1273. break;
  1274. }
  1275. if (timer->tmr_subdevice < id.subdevice)
  1276. continue;
  1277. snd_timer_user_copy_id(&id, timer);
  1278. break;
  1279. }
  1280. if (p == &snd_timer_list)
  1281. snd_timer_user_zero_id(&id);
  1282. break;
  1283. default:
  1284. snd_timer_user_zero_id(&id);
  1285. }
  1286. }
  1287. mutex_unlock(&register_mutex);
  1288. if (copy_to_user(_tid, &id, sizeof(*_tid)))
  1289. return -EFAULT;
  1290. return 0;
  1291. }
  1292. static int snd_timer_user_ginfo(struct file *file,
  1293. struct snd_timer_ginfo __user *_ginfo)
  1294. {
  1295. struct snd_timer_ginfo *ginfo;
  1296. struct snd_timer_id tid;
  1297. struct snd_timer *t;
  1298. struct list_head *p;
  1299. int err = 0;
  1300. ginfo = kmalloc(sizeof(*ginfo), GFP_KERNEL);
  1301. if (! ginfo)
  1302. return -ENOMEM;
  1303. if (copy_from_user(ginfo, _ginfo, sizeof(*ginfo))) {
  1304. kfree(ginfo);
  1305. return -EFAULT;
  1306. }
  1307. tid = ginfo->tid;
  1308. memset(ginfo, 0, sizeof(*ginfo));
  1309. ginfo->tid = tid;
  1310. mutex_lock(&register_mutex);
  1311. t = snd_timer_find(&tid);
  1312. if (t != NULL) {
  1313. ginfo->card = t->card ? t->card->number : -1;
  1314. if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
  1315. ginfo->flags |= SNDRV_TIMER_FLG_SLAVE;
  1316. strlcpy(ginfo->id, t->id, sizeof(ginfo->id));
  1317. strlcpy(ginfo->name, t->name, sizeof(ginfo->name));
  1318. ginfo->resolution = t->hw.resolution;
  1319. if (t->hw.resolution_min > 0) {
  1320. ginfo->resolution_min = t->hw.resolution_min;
  1321. ginfo->resolution_max = t->hw.resolution_max;
  1322. }
  1323. list_for_each(p, &t->open_list_head) {
  1324. ginfo->clients++;
  1325. }
  1326. } else {
  1327. err = -ENODEV;
  1328. }
  1329. mutex_unlock(&register_mutex);
  1330. if (err >= 0 && copy_to_user(_ginfo, ginfo, sizeof(*ginfo)))
  1331. err = -EFAULT;
  1332. kfree(ginfo);
  1333. return err;
  1334. }
  1335. static int snd_timer_user_gparams(struct file *file,
  1336. struct snd_timer_gparams __user *_gparams)
  1337. {
  1338. struct snd_timer_gparams gparams;
  1339. struct snd_timer *t;
  1340. int err;
  1341. if (copy_from_user(&gparams, _gparams, sizeof(gparams)))
  1342. return -EFAULT;
  1343. mutex_lock(&register_mutex);
  1344. t = snd_timer_find(&gparams.tid);
  1345. if (!t) {
  1346. err = -ENODEV;
  1347. goto _error;
  1348. }
  1349. if (!list_empty(&t->open_list_head)) {
  1350. err = -EBUSY;
  1351. goto _error;
  1352. }
  1353. if (!t->hw.set_period) {
  1354. err = -ENOSYS;
  1355. goto _error;
  1356. }
  1357. err = t->hw.set_period(t, gparams.period_num, gparams.period_den);
  1358. _error:
  1359. mutex_unlock(&register_mutex);
  1360. return err;
  1361. }
  1362. static int snd_timer_user_gstatus(struct file *file,
  1363. struct snd_timer_gstatus __user *_gstatus)
  1364. {
  1365. struct snd_timer_gstatus gstatus;
  1366. struct snd_timer_id tid;
  1367. struct snd_timer *t;
  1368. int err = 0;
  1369. if (copy_from_user(&gstatus, _gstatus, sizeof(gstatus)))
  1370. return -EFAULT;
  1371. tid = gstatus.tid;
  1372. memset(&gstatus, 0, sizeof(gstatus));
  1373. gstatus.tid = tid;
  1374. mutex_lock(&register_mutex);
  1375. t = snd_timer_find(&tid);
  1376. if (t != NULL) {
  1377. if (t->hw.c_resolution)
  1378. gstatus.resolution = t->hw.c_resolution(t);
  1379. else
  1380. gstatus.resolution = t->hw.resolution;
  1381. if (t->hw.precise_resolution) {
  1382. t->hw.precise_resolution(t, &gstatus.resolution_num,
  1383. &gstatus.resolution_den);
  1384. } else {
  1385. gstatus.resolution_num = gstatus.resolution;
  1386. gstatus.resolution_den = 1000000000uL;
  1387. }
  1388. } else {
  1389. err = -ENODEV;
  1390. }
  1391. mutex_unlock(&register_mutex);
  1392. if (err >= 0 && copy_to_user(_gstatus, &gstatus, sizeof(gstatus)))
  1393. err = -EFAULT;
  1394. return err;
  1395. }
  1396. static int snd_timer_user_tselect(struct file *file,
  1397. struct snd_timer_select __user *_tselect)
  1398. {
  1399. struct snd_timer_user *tu;
  1400. struct snd_timer_select tselect;
  1401. char str[32];
  1402. int err = 0;
  1403. tu = file->private_data;
  1404. mutex_lock(&tu->tread_sem);
  1405. if (tu->timeri) {
  1406. snd_timer_close(tu->timeri);
  1407. tu->timeri = NULL;
  1408. }
  1409. if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
  1410. err = -EFAULT;
  1411. goto __err;
  1412. }
  1413. sprintf(str, "application %i", current->pid);
  1414. if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
  1415. tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
  1416. err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
  1417. if (err < 0)
  1418. goto __err;
  1419. kfree(tu->queue);
  1420. tu->queue = NULL;
  1421. kfree(tu->tqueue);
  1422. tu->tqueue = NULL;
  1423. if (tu->tread) {
  1424. tu->tqueue = kmalloc(tu->queue_size * sizeof(struct snd_timer_tread),
  1425. GFP_KERNEL);
  1426. if (tu->tqueue == NULL)
  1427. err = -ENOMEM;
  1428. } else {
  1429. tu->queue = kmalloc(tu->queue_size * sizeof(struct snd_timer_read),
  1430. GFP_KERNEL);
  1431. if (tu->queue == NULL)
  1432. err = -ENOMEM;
  1433. }
  1434. if (err < 0) {
  1435. snd_timer_close(tu->timeri);
  1436. tu->timeri = NULL;
  1437. } else {
  1438. tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
  1439. tu->timeri->callback = tu->tread
  1440. ? snd_timer_user_tinterrupt : snd_timer_user_interrupt;
  1441. tu->timeri->ccallback = snd_timer_user_ccallback;
  1442. tu->timeri->callback_data = (void *)tu;
  1443. }
  1444. __err:
  1445. mutex_unlock(&tu->tread_sem);
  1446. return err;
  1447. }
  1448. static int snd_timer_user_info(struct file *file,
  1449. struct snd_timer_info __user *_info)
  1450. {
  1451. struct snd_timer_user *tu;
  1452. struct snd_timer_info *info;
  1453. struct snd_timer *t;
  1454. int err = 0;
  1455. tu = file->private_data;
  1456. snd_assert(tu->timeri != NULL, return -ENXIO);
  1457. t = tu->timeri->timer;
  1458. snd_assert(t != NULL, return -ENXIO);
  1459. info = kzalloc(sizeof(*info), GFP_KERNEL);
  1460. if (! info)
  1461. return -ENOMEM;
  1462. info->card = t->card ? t->card->number : -1;
  1463. if (t->hw.flags & SNDRV_TIMER_HW_SLAVE)
  1464. info->flags |= SNDRV_TIMER_FLG_SLAVE;
  1465. strlcpy(info->id, t->id, sizeof(info->id));
  1466. strlcpy(info->name, t->name, sizeof(info->name));
  1467. info->resolution = t->hw.resolution;
  1468. if (copy_to_user(_info, info, sizeof(*_info)))
  1469. err = -EFAULT;
  1470. kfree(info);
  1471. return err;
  1472. }
  1473. static int snd_timer_user_params(struct file *file,
  1474. struct snd_timer_params __user *_params)
  1475. {
  1476. struct snd_timer_user *tu;
  1477. struct snd_timer_params params;
  1478. struct snd_timer *t;
  1479. struct snd_timer_read *tr;
  1480. struct snd_timer_tread *ttr;
  1481. int err;
  1482. tu = file->private_data;
  1483. snd_assert(tu->timeri != NULL, return -ENXIO);
  1484. t = tu->timeri->timer;
  1485. snd_assert(t != NULL, return -ENXIO);
  1486. if (copy_from_user(&params, _params, sizeof(params)))
  1487. return -EFAULT;
  1488. if (!(t->hw.flags & SNDRV_TIMER_HW_SLAVE) && params.ticks < 1) {
  1489. err = -EINVAL;
  1490. goto _end;
  1491. }
  1492. if (params.queue_size > 0 &&
  1493. (params.queue_size < 32 || params.queue_size > 1024)) {
  1494. err = -EINVAL;
  1495. goto _end;
  1496. }
  1497. if (params.filter & ~((1<<SNDRV_TIMER_EVENT_RESOLUTION)|
  1498. (1<<SNDRV_TIMER_EVENT_TICK)|
  1499. (1<<SNDRV_TIMER_EVENT_START)|
  1500. (1<<SNDRV_TIMER_EVENT_STOP)|
  1501. (1<<SNDRV_TIMER_EVENT_CONTINUE)|
  1502. (1<<SNDRV_TIMER_EVENT_PAUSE)|
  1503. (1<<SNDRV_TIMER_EVENT_SUSPEND)|
  1504. (1<<SNDRV_TIMER_EVENT_RESUME)|
  1505. (1<<SNDRV_TIMER_EVENT_MSTART)|
  1506. (1<<SNDRV_TIMER_EVENT_MSTOP)|
  1507. (1<<SNDRV_TIMER_EVENT_MCONTINUE)|
  1508. (1<<SNDRV_TIMER_EVENT_MPAUSE)|
  1509. (1<<SNDRV_TIMER_EVENT_MSUSPEND)|
  1510. (1<<SNDRV_TIMER_EVENT_MRESUME))) {
  1511. err = -EINVAL;
  1512. goto _end;
  1513. }
  1514. snd_timer_stop(tu->timeri);
  1515. spin_lock_irq(&t->lock);
  1516. tu->timeri->flags &= ~(SNDRV_TIMER_IFLG_AUTO|
  1517. SNDRV_TIMER_IFLG_EXCLUSIVE|
  1518. SNDRV_TIMER_IFLG_EARLY_EVENT);
  1519. if (params.flags & SNDRV_TIMER_PSFLG_AUTO)
  1520. tu->timeri->flags |= SNDRV_TIMER_IFLG_AUTO;
  1521. if (params.flags & SNDRV_TIMER_PSFLG_EXCLUSIVE)
  1522. tu->timeri->flags |= SNDRV_TIMER_IFLG_EXCLUSIVE;
  1523. if (params.flags & SNDRV_TIMER_PSFLG_EARLY_EVENT)
  1524. tu->timeri->flags |= SNDRV_TIMER_IFLG_EARLY_EVENT;
  1525. spin_unlock_irq(&t->lock);
  1526. if (params.queue_size > 0 &&
  1527. (unsigned int)tu->queue_size != params.queue_size) {
  1528. if (tu->tread) {
  1529. ttr = kmalloc(params.queue_size * sizeof(*ttr),
  1530. GFP_KERNEL);
  1531. if (ttr) {
  1532. kfree(tu->tqueue);
  1533. tu->queue_size = params.queue_size;
  1534. tu->tqueue = ttr;
  1535. }
  1536. } else {
  1537. tr = kmalloc(params.queue_size * sizeof(*tr),
  1538. GFP_KERNEL);
  1539. if (tr) {
  1540. kfree(tu->queue);
  1541. tu->queue_size = params.queue_size;
  1542. tu->queue = tr;
  1543. }
  1544. }
  1545. }
  1546. tu->qhead = tu->qtail = tu->qused = 0;
  1547. if (tu->timeri->flags & SNDRV_TIMER_IFLG_EARLY_EVENT) {
  1548. if (tu->tread) {
  1549. struct snd_timer_tread tread;
  1550. tread.event = SNDRV_TIMER_EVENT_EARLY;
  1551. tread.tstamp.tv_sec = 0;
  1552. tread.tstamp.tv_nsec = 0;
  1553. tread.val = 0;
  1554. snd_timer_user_append_to_tqueue(tu, &tread);
  1555. } else {
  1556. struct snd_timer_read *r = &tu->queue[0];
  1557. r->resolution = 0;
  1558. r->ticks = 0;
  1559. tu->qused++;
  1560. tu->qtail++;
  1561. }
  1562. }
  1563. tu->filter = params.filter;
  1564. tu->ticks = params.ticks;
  1565. err = 0;
  1566. _end:
  1567. if (copy_to_user(_params, &params, sizeof(params)))
  1568. return -EFAULT;
  1569. return err;
  1570. }
  1571. static int snd_timer_user_status(struct file *file,
  1572. struct snd_timer_status __user *_status)
  1573. {
  1574. struct snd_timer_user *tu;
  1575. struct snd_timer_status status;
  1576. tu = file->private_data;
  1577. snd_assert(tu->timeri != NULL, return -ENXIO);
  1578. memset(&status, 0, sizeof(status));
  1579. status.tstamp = tu->tstamp;
  1580. status.resolution = snd_timer_resolution(tu->timeri);
  1581. status.lost = tu->timeri->lost;
  1582. status.overrun = tu->overrun;
  1583. spin_lock_irq(&tu->qlock);
  1584. status.queue = tu->qused;
  1585. spin_unlock_irq(&tu->qlock);
  1586. if (copy_to_user(_status, &status, sizeof(status)))
  1587. return -EFAULT;
  1588. return 0;
  1589. }
  1590. static int snd_timer_user_start(struct file *file)
  1591. {
  1592. int err;
  1593. struct snd_timer_user *tu;
  1594. tu = file->private_data;
  1595. snd_assert(tu->timeri != NULL, return -ENXIO);
  1596. snd_timer_stop(tu->timeri);
  1597. tu->timeri->lost = 0;
  1598. tu->last_resolution = 0;
  1599. return (err = snd_timer_start(tu->timeri, tu->ticks)) < 0 ? err : 0;
  1600. }
  1601. static int snd_timer_user_stop(struct file *file)
  1602. {
  1603. int err;
  1604. struct snd_timer_user *tu;
  1605. tu = file->private_data;
  1606. snd_assert(tu->timeri != NULL, return -ENXIO);
  1607. return (err = snd_timer_stop(tu->timeri)) < 0 ? err : 0;
  1608. }
  1609. static int snd_timer_user_continue(struct file *file)
  1610. {
  1611. int err;
  1612. struct snd_timer_user *tu;
  1613. tu = file->private_data;
  1614. snd_assert(tu->timeri != NULL, return -ENXIO);
  1615. tu->timeri->lost = 0;
  1616. return (err = snd_timer_continue(tu->timeri)) < 0 ? err : 0;
  1617. }
  1618. static int snd_timer_user_pause(struct file *file)
  1619. {
  1620. int err;
  1621. struct snd_timer_user *tu;
  1622. tu = file->private_data;
  1623. snd_assert(tu->timeri != NULL, return -ENXIO);
  1624. return (err = snd_timer_pause(tu->timeri)) < 0 ? err : 0;
  1625. }
  1626. enum {
  1627. SNDRV_TIMER_IOCTL_START_OLD = _IO('T', 0x20),
  1628. SNDRV_TIMER_IOCTL_STOP_OLD = _IO('T', 0x21),
  1629. SNDRV_TIMER_IOCTL_CONTINUE_OLD = _IO('T', 0x22),
  1630. SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23),
  1631. };
  1632. static long snd_timer_user_ioctl(struct file *file, unsigned int cmd,
  1633. unsigned long arg)
  1634. {
  1635. struct snd_timer_user *tu;
  1636. void __user *argp = (void __user *)arg;
  1637. int __user *p = argp;
  1638. tu = file->private_data;
  1639. switch (cmd) {
  1640. case SNDRV_TIMER_IOCTL_PVERSION:
  1641. return put_user(SNDRV_TIMER_VERSION, p) ? -EFAULT : 0;
  1642. case SNDRV_TIMER_IOCTL_NEXT_DEVICE:
  1643. return snd_timer_user_next_device(argp);
  1644. case SNDRV_TIMER_IOCTL_TREAD:
  1645. {
  1646. int xarg;
  1647. mutex_lock(&tu->tread_sem);
  1648. if (tu->timeri) { /* too late */
  1649. mutex_unlock(&tu->tread_sem);
  1650. return -EBUSY;
  1651. }
  1652. if (get_user(xarg, p)) {
  1653. mutex_unlock(&tu->tread_sem);
  1654. return -EFAULT;
  1655. }
  1656. tu->tread = xarg ? 1 : 0;
  1657. mutex_unlock(&tu->tread_sem);
  1658. return 0;
  1659. }
  1660. case SNDRV_TIMER_IOCTL_GINFO:
  1661. return snd_timer_user_ginfo(file, argp);
  1662. case SNDRV_TIMER_IOCTL_GPARAMS:
  1663. return snd_timer_user_gparams(file, argp);
  1664. case SNDRV_TIMER_IOCTL_GSTATUS:
  1665. return snd_timer_user_gstatus(file, argp);
  1666. case SNDRV_TIMER_IOCTL_SELECT:
  1667. return snd_timer_user_tselect(file, argp);
  1668. case SNDRV_TIMER_IOCTL_INFO:
  1669. return snd_timer_user_info(file, argp);
  1670. case SNDRV_TIMER_IOCTL_PARAMS:
  1671. return snd_timer_user_params(file, argp);
  1672. case SNDRV_TIMER_IOCTL_STATUS:
  1673. return snd_timer_user_status(file, argp);
  1674. case SNDRV_TIMER_IOCTL_START:
  1675. case SNDRV_TIMER_IOCTL_START_OLD:
  1676. return snd_timer_user_start(file);
  1677. case SNDRV_TIMER_IOCTL_STOP:
  1678. case SNDRV_TIMER_IOCTL_STOP_OLD:
  1679. return snd_timer_user_stop(file);
  1680. case SNDRV_TIMER_IOCTL_CONTINUE:
  1681. case SNDRV_TIMER_IOCTL_CONTINUE_OLD:
  1682. return snd_timer_user_continue(file);
  1683. case SNDRV_TIMER_IOCTL_PAUSE:
  1684. case SNDRV_TIMER_IOCTL_PAUSE_OLD:
  1685. return snd_timer_user_pause(file);
  1686. }
  1687. return -ENOTTY;
  1688. }
  1689. static int snd_timer_user_fasync(int fd, struct file * file, int on)
  1690. {
  1691. struct snd_timer_user *tu;
  1692. int err;
  1693. tu = file->private_data;
  1694. err = fasync_helper(fd, file, on, &tu->fasync);
  1695. if (err < 0)
  1696. return err;
  1697. return 0;
  1698. }
  1699. static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
  1700. size_t count, loff_t *offset)
  1701. {
  1702. struct snd_timer_user *tu;
  1703. long result = 0, unit;
  1704. int err = 0;
  1705. tu = file->private_data;
  1706. unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
  1707. spin_lock_irq(&tu->qlock);
  1708. while ((long)count - result >= unit) {
  1709. while (!tu->qused) {
  1710. wait_queue_t wait;
  1711. if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
  1712. err = -EAGAIN;
  1713. break;
  1714. }
  1715. set_current_state(TASK_INTERRUPTIBLE);
  1716. init_waitqueue_entry(&wait, current);
  1717. add_wait_queue(&tu->qchange_sleep, &wait);
  1718. spin_unlock_irq(&tu->qlock);
  1719. schedule();
  1720. spin_lock_irq(&tu->qlock);
  1721. remove_wait_queue(&tu->qchange_sleep, &wait);
  1722. if (signal_pending(current)) {
  1723. err = -ERESTARTSYS;
  1724. break;
  1725. }
  1726. }
  1727. spin_unlock_irq(&tu->qlock);
  1728. if (err < 0)
  1729. goto _error;
  1730. if (tu->tread) {
  1731. if (copy_to_user(buffer, &tu->tqueue[tu->qhead++],
  1732. sizeof(struct snd_timer_tread))) {
  1733. err = -EFAULT;
  1734. goto _error;
  1735. }
  1736. } else {
  1737. if (copy_to_user(buffer, &tu->queue[tu->qhead++],
  1738. sizeof(struct snd_timer_read))) {
  1739. err = -EFAULT;
  1740. goto _error;
  1741. }
  1742. }
  1743. tu->qhead %= tu->queue_size;
  1744. result += unit;
  1745. buffer += unit;
  1746. spin_lock_irq(&tu->qlock);
  1747. tu->qused--;
  1748. }
  1749. spin_unlock_irq(&tu->qlock);
  1750. _error:
  1751. return result > 0 ? result : err;
  1752. }
  1753. static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
  1754. {
  1755. unsigned int mask;
  1756. struct snd_timer_user *tu;
  1757. tu = file->private_data;
  1758. poll_wait(file, &tu->qchange_sleep, wait);
  1759. mask = 0;
  1760. if (tu->qused)
  1761. mask |= POLLIN | POLLRDNORM;
  1762. return mask;
  1763. }
  1764. #ifdef CONFIG_COMPAT
  1765. #include "timer_compat.c"
  1766. #else
  1767. #define snd_timer_user_ioctl_compat NULL
  1768. #endif
  1769. static struct file_operations snd_timer_f_ops =
  1770. {
  1771. .owner = THIS_MODULE,
  1772. .read = snd_timer_user_read,
  1773. .open = snd_timer_user_open,
  1774. .release = snd_timer_user_release,
  1775. .poll = snd_timer_user_poll,
  1776. .unlocked_ioctl = snd_timer_user_ioctl,
  1777. .compat_ioctl = snd_timer_user_ioctl_compat,
  1778. .fasync = snd_timer_user_fasync,
  1779. };
  1780. /*
  1781. * ENTRY functions
  1782. */
  1783. static int __init alsa_timer_init(void)
  1784. {
  1785. int err;
  1786. #ifdef SNDRV_OSS_INFO_DEV_TIMERS
  1787. snd_oss_info_register(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1,
  1788. "system timer");
  1789. #endif
  1790. if ((err = snd_timer_register_system()) < 0)
  1791. snd_printk(KERN_ERR "unable to register system timer (%i)\n",
  1792. err);
  1793. if ((err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
  1794. &snd_timer_f_ops, NULL, "timer")) < 0)
  1795. snd_printk(KERN_ERR "unable to register timer device (%i)\n",
  1796. err);
  1797. snd_timer_proc_init();
  1798. return 0;
  1799. }
  1800. static void __exit alsa_timer_exit(void)
  1801. {
  1802. struct list_head *p, *n;
  1803. snd_unregister_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0);
  1804. /* unregister the system timer */
  1805. list_for_each_safe(p, n, &snd_timer_list) {
  1806. struct snd_timer *timer = list_entry(p, struct snd_timer, device_list);
  1807. snd_timer_unregister(timer);
  1808. }
  1809. snd_timer_proc_done();
  1810. #ifdef SNDRV_OSS_INFO_DEV_TIMERS
  1811. snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_TIMERS, SNDRV_CARDS - 1);
  1812. #endif
  1813. }
  1814. module_init(alsa_timer_init)
  1815. module_exit(alsa_timer_exit)
  1816. EXPORT_SYMBOL(snd_timer_open);
  1817. EXPORT_SYMBOL(snd_timer_close);
  1818. EXPORT_SYMBOL(snd_timer_resolution);
  1819. EXPORT_SYMBOL(snd_timer_start);
  1820. EXPORT_SYMBOL(snd_timer_stop);
  1821. EXPORT_SYMBOL(snd_timer_continue);
  1822. EXPORT_SYMBOL(snd_timer_pause);
  1823. EXPORT_SYMBOL(snd_timer_new);
  1824. EXPORT_SYMBOL(snd_timer_notify);
  1825. EXPORT_SYMBOL(snd_timer_global_new);
  1826. EXPORT_SYMBOL(snd_timer_global_free);
  1827. EXPORT_SYMBOL(snd_timer_global_register);
  1828. EXPORT_SYMBOL(snd_timer_global_unregister);
  1829. EXPORT_SYMBOL(snd_timer_interrupt);