timer.c 51 KB

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