timer.c 55 KB

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