timer.c 51 KB

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