timer.c 54 KB

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