timer.c 53 KB

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