timer.c 55 KB

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