timer.c 58 KB

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