timer.c 53 KB

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