timer.c 54 KB

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