timer.c 55 KB

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