timer.c 51 KB

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