timer.c 51 KB

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