timer.c 54 KB

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