timer.c 53 KB

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