timer.c 54 KB

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