timer.c 53 KB

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