timer.c 52 KB

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