timer.c 52 KB

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