timer.c 53 KB

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