timer.c 62 KB

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