timer.c 51 KB

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