timer.c 51 KB

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