timer.c 50 KB

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