timer.c 51 KB

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