timer.c 54 KB

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