timer.c 51 KB

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