timer.c 55 KB

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