timer.c 55 KB

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