timer.c 50 KB

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