timer.c 51 KB

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