audio.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  1. /* $OpenBSD: audio.c,v 1.138 2015/07/29 21:13:32 ratchov Exp $ */
  2. /*
  3. * Copyright (c) 2015 Alexandre Ratchov <alex@caoua.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/param.h>
  18. #include <sys/fcntl.h>
  19. #include <sys/systm.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/conf.h>
  22. #include <sys/poll.h>
  23. #include <sys/kernel.h>
  24. #include <sys/task.h>
  25. #include <sys/vnode.h>
  26. #include <sys/malloc.h>
  27. #include <sys/device.h>
  28. #include <sys/audioio.h>
  29. #include <dev/audio_if.h>
  30. #include <dev/mulaw.h>
  31. #include "audio.h"
  32. #include "wskbd.h"
  33. #ifdef AUDIO_DEBUG
  34. #define DPRINTF(...) \
  35. do { \
  36. if (audio_debug) \
  37. printf(__VA_ARGS__); \
  38. } while(0)
  39. #define DPRINTFN(n, ...) \
  40. do { \
  41. if (audio_debug > (n)) \
  42. printf(__VA_ARGS__); \
  43. } while(0)
  44. #else
  45. #define DPRINTF(...) do {} while(0)
  46. #define DPRINTFN(n, ...) do {} while(0)
  47. #endif
  48. #define DEVNAME(sc) ((sc)->dev.dv_xname)
  49. #define AUDIO_UNIT(n) (minor(n) & 0x0f)
  50. #define AUDIO_DEV(n) (minor(n) & 0xf0)
  51. #define AUDIO_DEV_SOUND 0 /* minor of /dev/sound0 */
  52. #define AUDIO_DEV_MIXER 0x10 /* minor of /dev/mixer0 */
  53. #define AUDIO_DEV_AUDIO 0x80 /* minor of /dev/audio0 */
  54. #define AUDIO_DEV_AUDIOCTL 0xc0 /* minor of /dev/audioctl */
  55. #define AUDIO_BUFSZ 65536 /* buffer size in bytes */
  56. /*
  57. * dma buffer
  58. */
  59. struct audio_buf {
  60. unsigned char *data; /* DMA memory block */
  61. size_t datalen; /* size of DMA memory block */
  62. size_t len; /* size of DMA FIFO */
  63. size_t start; /* first byte used in the FIFO */
  64. size_t used; /* bytes used in the FIFO */
  65. size_t blksz; /* DMA block size */
  66. struct selinfo sel; /* to record & wakeup poll(2) */
  67. unsigned int pos; /* bytes transferred */
  68. unsigned int xrun; /* bytes lost by xruns */
  69. int blocking; /* read/write blocking */
  70. };
  71. #if NWSKBD > 0
  72. struct wskbd_vol
  73. {
  74. int val; /* index of the value control */
  75. int mute; /* index of the mute control */
  76. int step; /* increment/decrement step */
  77. int nch; /* channels in the value control */
  78. int val_pending; /* pending change of val */
  79. int mute_pending; /* pending mute toggles */
  80. };
  81. #endif
  82. /*
  83. * device structure
  84. */
  85. struct audio_softc {
  86. struct device dev;
  87. struct audio_hw_if *ops; /* driver funcs */
  88. void *arg; /* first arg to driver funcs */
  89. int mode; /* bitmask of AUMODE_* */
  90. int quiesce; /* device suspended */
  91. struct audio_buf play, rec;
  92. unsigned int sw_enc; /* user exposed AUDIO_ENCODING_* */
  93. unsigned int hw_enc; /* harware AUDIO_ENCODING_* */
  94. unsigned int bits; /* bits per sample */
  95. unsigned int bps; /* bytes-per-sample */
  96. unsigned int msb; /* sample are MSB aligned */
  97. unsigned int rate; /* rate in Hz */
  98. unsigned int round; /* block size in frames */
  99. unsigned int nblks; /* number of play blocks */
  100. unsigned int pchan, rchan; /* number of channels */
  101. unsigned char silence[4]; /* a sample of silence */
  102. int pause; /* not trying to start DMA */
  103. int active; /* DMA in process */
  104. int offs; /* offset between play & rec dir */
  105. void (*conv_enc)(unsigned char *, int); /* encode to native */
  106. void (*conv_dec)(unsigned char *, int); /* decode to user */
  107. #if NWSKBD > 0
  108. struct wskbd_vol spkr, mic;
  109. struct task wskbd_task;
  110. int wskbd_taskset;
  111. #endif
  112. };
  113. int audio_match(struct device *, void *, void *);
  114. void audio_attach(struct device *, struct device *, void *);
  115. int audio_activate(struct device *, int);
  116. int audio_detach(struct device *, int);
  117. void audio_pintr(void *);
  118. void audio_rintr(void *);
  119. #if NWSKBD > 0
  120. void wskbd_mixer_init(struct audio_softc *);
  121. #endif
  122. const struct cfattach audio_ca = {
  123. sizeof(struct audio_softc), audio_match, audio_attach,
  124. audio_detach, audio_activate
  125. };
  126. struct cfdriver audio_cd = {
  127. NULL, "audio", DV_DULL
  128. };
  129. /*
  130. * This mutex protects data structures (including registers on the
  131. * sound-card) that are manipulated by both the interrupt handler and
  132. * syscall code-paths.
  133. *
  134. * Note that driver methods may sleep (e.g. in malloc); consequently the
  135. * audio layer calls them with the mutex unlocked. Driver methods are
  136. * responsible for locking the mutex when they manipulate data used by
  137. * the interrupt handler and interrupts may occur.
  138. *
  139. * Similarly, the driver is responsible for locking the mutex in its
  140. * interrupt handler and to call the audio layer call-backs (i.e.
  141. * audio_{p,r}int()) with the mutex locked.
  142. */
  143. struct mutex audio_lock = MUTEX_INITIALIZER(IPL_AUDIO);
  144. #ifdef AUDIO_DEBUG
  145. /*
  146. * 0 - nothing, as if AUDIO_DEBUG isn't defined
  147. * 1 - initialisations & setup
  148. * 2 - blocks & interrupts
  149. */
  150. int audio_debug = 1;
  151. #endif
  152. unsigned int
  153. audio_gcd(unsigned int a, unsigned int b)
  154. {
  155. unsigned int r;
  156. while (b > 0) {
  157. r = a % b;
  158. a = b;
  159. b = r;
  160. }
  161. return a;
  162. }
  163. int
  164. audio_buf_init(struct audio_softc *sc, struct audio_buf *buf, int dir)
  165. {
  166. if (sc->ops->round_buffersize) {
  167. buf->datalen = sc->ops->round_buffersize(sc->arg,
  168. dir, AUDIO_BUFSZ);
  169. } else
  170. buf->datalen = AUDIO_BUFSZ;
  171. if (sc->ops->allocm) {
  172. buf->data = sc->ops->allocm(sc->arg, dir, buf->datalen,
  173. M_DEVBUF, M_WAITOK);
  174. } else
  175. buf->data = malloc(buf->datalen, M_DEVBUF, M_WAITOK);
  176. if (buf->data == NULL)
  177. return ENOMEM;
  178. return 0;
  179. }
  180. void
  181. audio_buf_done(struct audio_softc *sc, struct audio_buf *buf)
  182. {
  183. if (sc->ops->freem)
  184. sc->ops->freem(sc->arg, buf->data, M_DEVBUF);
  185. else
  186. free(buf->data, M_DEVBUF, buf->datalen);
  187. }
  188. /*
  189. * return the reader pointer and the number of bytes available
  190. */
  191. unsigned char *
  192. audio_buf_rgetblk(struct audio_buf *buf, size_t *rsize)
  193. {
  194. size_t count;
  195. count = buf->len - buf->start;
  196. if (count > buf->used)
  197. count = buf->used;
  198. *rsize = count;
  199. return buf->data + buf->start;
  200. }
  201. /*
  202. * discard "count" bytes at the start postion.
  203. */
  204. void
  205. audio_buf_rdiscard(struct audio_buf *buf, size_t count)
  206. {
  207. #ifdef AUDIO_DEBUG
  208. if (count > buf->used) {
  209. panic("audio_buf_rdiscard: bad count = %zu, "
  210. "start = %zu, used = %zu\n", count, buf->start, buf->used);
  211. }
  212. #endif
  213. buf->used -= count;
  214. buf->start += count;
  215. if (buf->start >= buf->len)
  216. buf->start -= buf->len;
  217. }
  218. /*
  219. * advance the writer pointer by "count" bytes
  220. */
  221. void
  222. audio_buf_wcommit(struct audio_buf *buf, size_t count)
  223. {
  224. #ifdef AUDIO_DEBUG
  225. if (count > (buf->len - buf->used)) {
  226. panic("audio_buf_wcommit: bad count = %zu, "
  227. "start = %zu, used = %zu\n", count, buf->start, buf->used);
  228. }
  229. #endif
  230. buf->used += count;
  231. }
  232. /*
  233. * get writer pointer and the number of bytes writable
  234. */
  235. unsigned char *
  236. audio_buf_wgetblk(struct audio_buf *buf, size_t *rsize)
  237. {
  238. size_t end, avail, count;
  239. end = buf->start + buf->used;
  240. if (end >= buf->len)
  241. end -= buf->len;
  242. avail = buf->len - buf->used;
  243. count = buf->len - end;
  244. if (count > avail)
  245. count = avail;
  246. *rsize = count;
  247. return buf->data + end;
  248. }
  249. void
  250. audio_calc_sil(struct audio_softc *sc)
  251. {
  252. unsigned char *q;
  253. unsigned int s, i;
  254. int d, e;
  255. e = sc->sw_enc;
  256. #ifdef AUDIO_DEBUG
  257. switch (e) {
  258. case AUDIO_ENCODING_SLINEAR_LE:
  259. case AUDIO_ENCODING_ULINEAR_LE:
  260. case AUDIO_ENCODING_SLINEAR_BE:
  261. case AUDIO_ENCODING_ULINEAR_BE:
  262. break;
  263. default:
  264. printf("%s: unhandled play encoding %d\n", DEVNAME(sc), e);
  265. memset(sc->silence, 0, sc->bps);
  266. return;
  267. }
  268. #endif
  269. if (e == AUDIO_ENCODING_SLINEAR_BE || e == AUDIO_ENCODING_ULINEAR_BE) {
  270. d = -1;
  271. q = sc->silence + sc->bps - 1;
  272. } else {
  273. d = 1;
  274. q = sc->silence;
  275. }
  276. if (e == AUDIO_ENCODING_SLINEAR_LE || e == AUDIO_ENCODING_SLINEAR_BE) {
  277. s = 0;
  278. } else {
  279. s = 0x80000000;
  280. if (sc->msb)
  281. s >>= 32 - 8 * sc->bps;
  282. else
  283. s >>= 32 - sc->bits;
  284. }
  285. for (i = 0; i < sc->bps; i++) {
  286. *q = s;
  287. q += d;
  288. s >>= 8;
  289. }
  290. if (sc->conv_enc)
  291. sc->conv_enc(sc->silence, sc->bps);
  292. }
  293. void
  294. audio_fill_sil(struct audio_softc *sc, unsigned char *ptr, size_t count)
  295. {
  296. unsigned char *q, *p;
  297. size_t i, j;
  298. q = ptr;
  299. for (j = count / sc->bps; j > 0; j--) {
  300. p = sc->silence;
  301. for (i = sc->bps; i > 0; i--)
  302. *q++ = *p++;
  303. }
  304. }
  305. void
  306. audio_clear(struct audio_softc *sc)
  307. {
  308. if (sc->mode & AUMODE_PLAY) {
  309. sc->play.used = sc->play.start = 0;
  310. sc->play.pos = sc->play.xrun = 0;
  311. audio_fill_sil(sc, sc->play.data, sc->play.len);
  312. }
  313. if (sc->mode & AUMODE_RECORD) {
  314. sc->rec.used = sc->rec.start = 0;
  315. sc->rec.pos = sc->rec.xrun = 0;
  316. audio_fill_sil(sc, sc->rec.data, sc->rec.len);
  317. }
  318. }
  319. /*
  320. * called whenever a block is consumed by the driver
  321. */
  322. void
  323. audio_pintr(void *addr)
  324. {
  325. struct audio_softc *sc = addr;
  326. unsigned char *ptr;
  327. size_t count;
  328. int error, nblk, todo;
  329. MUTEX_ASSERT_LOCKED(&audio_lock);
  330. if (!(sc->mode & AUMODE_PLAY) || !sc->active) {
  331. printf("%s: play interrupt but not playing\n", DEVNAME(sc));
  332. return;
  333. }
  334. if (sc->quiesce) {
  335. DPRINTF("%s: quesced, skipping play intr\n", DEVNAME(sc));
  336. return;
  337. }
  338. /*
  339. * check if record pointer wrapped, see explanation
  340. * in audio_rintr()
  341. */
  342. if (sc->mode & AUMODE_RECORD) {
  343. sc->offs--;
  344. nblk = sc->rec.len / sc->rec.blksz;
  345. todo = -sc->offs;
  346. if (todo >= nblk) {
  347. todo -= todo % nblk;
  348. DPRINTFN(1, "%s: rec ptr wrapped, moving %d blocks\n",
  349. DEVNAME(sc), todo);
  350. while (todo-- > 0)
  351. audio_rintr(sc);
  352. }
  353. }
  354. sc->play.pos += sc->play.blksz;
  355. audio_fill_sil(sc, sc->play.data + sc->play.start, sc->play.blksz);
  356. audio_buf_rdiscard(&sc->play, sc->play.blksz);
  357. if (sc->play.used < sc->play.blksz) {
  358. DPRINTFN(1, "%s: play underrun\n", DEVNAME(sc));
  359. sc->play.xrun += sc->play.blksz;
  360. audio_buf_wcommit(&sc->play, sc->play.blksz);
  361. }
  362. DPRINTFN(1, "%s: play intr, used -> %zu, start -> %zu\n",
  363. DEVNAME(sc), sc->play.used, sc->play.start);
  364. if (!sc->ops->trigger_output) {
  365. ptr = audio_buf_rgetblk(&sc->play, &count);
  366. error = sc->ops->start_output(sc->arg,
  367. ptr, sc->play.blksz, audio_pintr, (void *)sc);
  368. if (error) {
  369. printf("%s: play restart failed: %d\n",
  370. DEVNAME(sc), error);
  371. }
  372. }
  373. if (sc->play.used < sc->play.len) {
  374. DPRINTFN(1, "%s: play wakeup, chan = %d\n",
  375. DEVNAME(sc), sc->play.blocking);
  376. if (sc->play.blocking) {
  377. wakeup(&sc->play.blocking);
  378. sc->play.blocking = 0;
  379. }
  380. selwakeup(&sc->play.sel);
  381. }
  382. }
  383. /*
  384. * called whenever a block is produced by the driver
  385. */
  386. void
  387. audio_rintr(void *addr)
  388. {
  389. struct audio_softc *sc = addr;
  390. unsigned char *ptr;
  391. size_t count;
  392. int error, nblk, todo;
  393. MUTEX_ASSERT_LOCKED(&audio_lock);
  394. if (!(sc->mode & AUMODE_RECORD) || !sc->active) {
  395. printf("%s: rec interrupt but not recording\n", DEVNAME(sc));
  396. return;
  397. }
  398. if (sc->quiesce) {
  399. DPRINTF("%s: quesced, skipping rec intr\n", DEVNAME(sc));
  400. return;
  401. }
  402. /*
  403. * Interrupts may be masked by other sub-systems during 320ms
  404. * and more. During such a delay the hardware doesn't stop
  405. * playing and the play buffer pointers may wrap, this can't be
  406. * detected and corrected by low level drivers. This makes the
  407. * record stream ahead of the play stream; this is detected as a
  408. * hardware anomaly by userland and cause programs to misbehave.
  409. *
  410. * We fix this by advancing play position by an integer count of
  411. * full buffers, so it reaches the record position.
  412. */
  413. if (sc->mode & AUMODE_PLAY) {
  414. sc->offs++;
  415. nblk = sc->play.len / sc->play.blksz;
  416. todo = sc->offs;
  417. if (todo >= nblk) {
  418. todo -= todo % nblk;
  419. DPRINTFN(1, "%s: play ptr wrapped, moving %d blocks\n",
  420. DEVNAME(sc), todo);
  421. while (todo-- > 0)
  422. audio_pintr(sc);
  423. }
  424. }
  425. sc->rec.pos += sc->rec.blksz;
  426. audio_buf_wcommit(&sc->rec, sc->rec.blksz);
  427. if (sc->rec.used == sc->rec.len) {
  428. DPRINTFN(1, "%s: rec overrun\n", DEVNAME(sc));
  429. sc->rec.xrun += sc->rec.blksz;
  430. audio_buf_rdiscard(&sc->rec, sc->rec.blksz);
  431. }
  432. DPRINTFN(1, "%s: rec intr, used -> %zu\n", DEVNAME(sc), sc->rec.used);
  433. if (!sc->ops->trigger_input) {
  434. ptr = audio_buf_wgetblk(&sc->rec, &count);
  435. error = sc->ops->start_input(sc->arg,
  436. ptr, sc->rec.blksz, audio_rintr, (void *)sc);
  437. if (error) {
  438. printf("%s: rec restart failed: %d\n",
  439. DEVNAME(sc), error);
  440. }
  441. }
  442. if (sc->rec.used > 0) {
  443. DPRINTFN(1, "%s: rec wakeup, chan = %d\n",
  444. DEVNAME(sc), sc->rec.blocking);
  445. if (sc->rec.blocking) {
  446. wakeup(&sc->rec.blocking);
  447. sc->rec.blocking = 0;
  448. }
  449. selwakeup(&sc->rec.sel);
  450. }
  451. }
  452. int
  453. audio_start_do(struct audio_softc *sc)
  454. {
  455. int error;
  456. struct audio_params p;
  457. unsigned char *ptr;
  458. size_t count;
  459. DPRINTFN(1, "%s: start play: "
  460. "start = %zu, used = %zu, "
  461. "len = %zu, blksz = %zu\n",
  462. DEVNAME(sc), sc->play.start, sc->play.used,
  463. sc->play.len, sc->play.blksz);
  464. DPRINTFN(1, "%s: start rec: "
  465. "start = %zu, used = %zu, "
  466. "len = %zu, blksz = %zu\n",
  467. DEVNAME(sc), sc->rec.start, sc->rec.used,
  468. sc->rec.len, sc->rec.blksz);
  469. error = 0;
  470. sc->offs = 0;
  471. if (sc->mode & AUMODE_PLAY) {
  472. if (sc->ops->trigger_output) {
  473. p.encoding = sc->hw_enc;
  474. p.precision = sc->bits;
  475. p.bps = sc->bps;
  476. p.msb = sc->msb;
  477. p.sample_rate = sc->rate;
  478. p.channels = sc->pchan;
  479. error = sc->ops->trigger_output(sc->arg,
  480. sc->play.data,
  481. sc->play.data + sc->play.len,
  482. sc->play.blksz,
  483. audio_pintr, (void *)sc, &p);
  484. } else {
  485. mtx_enter(&audio_lock);
  486. ptr = audio_buf_rgetblk(&sc->play, &count);
  487. error = sc->ops->start_output(sc->arg,
  488. ptr, sc->play.blksz, audio_pintr, (void *)sc);
  489. mtx_leave(&audio_lock);
  490. }
  491. if (error)
  492. printf("%s: failed to start playback\n", DEVNAME(sc));
  493. }
  494. if (sc->mode & AUMODE_RECORD) {
  495. if (sc->ops->trigger_input) {
  496. p.encoding = sc->hw_enc;
  497. p.precision = sc->bits;
  498. p.bps = sc->bps;
  499. p.msb = sc->msb;
  500. p.sample_rate = sc->rate;
  501. p.channels = sc->rchan;
  502. error = sc->ops->trigger_input(sc->arg,
  503. sc->rec.data,
  504. sc->rec.data + sc->rec.len,
  505. sc->rec.blksz,
  506. audio_rintr, (void *)sc, &p);
  507. } else {
  508. mtx_enter(&audio_lock);
  509. ptr = audio_buf_wgetblk(&sc->rec, &count);
  510. error = sc->ops->start_input(sc->arg,
  511. ptr, sc->rec.blksz, audio_rintr, (void *)sc);
  512. mtx_leave(&audio_lock);
  513. }
  514. if (error)
  515. printf("%s: failed to start recording\n", DEVNAME(sc));
  516. }
  517. return error;
  518. }
  519. int
  520. audio_stop_do(struct audio_softc *sc)
  521. {
  522. if (sc->mode & AUMODE_PLAY)
  523. sc->ops->halt_output(sc->arg);
  524. if (sc->mode & AUMODE_RECORD)
  525. sc->ops->halt_input(sc->arg);
  526. return 0;
  527. }
  528. int
  529. audio_start(struct audio_softc *sc)
  530. {
  531. sc->active = 1;
  532. sc->play.xrun = sc->play.pos = sc->rec.xrun = sc->rec.pos = 0;
  533. return audio_start_do(sc);
  534. }
  535. int
  536. audio_stop(struct audio_softc *sc)
  537. {
  538. int error;
  539. error = audio_stop_do(sc);
  540. if (error)
  541. return error;
  542. audio_clear(sc);
  543. sc->active = 0;
  544. return 0;
  545. }
  546. int
  547. audio_setpar(struct audio_softc *sc)
  548. {
  549. struct audio_params p, r;
  550. unsigned int nr, np, max, min, mult;
  551. int error;
  552. DPRINTF("%s: setpar: req enc=%d bits=%d, bps=%d, msb=%d "
  553. "rate=%d, pchan=%d, rchan=%d, round=%u, nblks=%d\n",
  554. DEVNAME(sc), sc->sw_enc, sc->bits, sc->bps, sc->msb,
  555. sc->rate, sc->pchan, sc->rchan, sc->round, sc->nblks);
  556. /*
  557. * AUDIO_ENCODING_SLINEAR and AUDIO_ENCODING_ULINEAR are not
  558. * used anymore, promote them to the _LE and _BE equivalents
  559. */
  560. if (sc->sw_enc == AUDIO_ENCODING_SLINEAR) {
  561. #if BYTE_ORDER == LITTLE_ENDIAN
  562. sc->sw_enc = AUDIO_ENCODING_SLINEAR_LE;
  563. #else
  564. sc->sw_enc = AUDIO_ENCODING_SLINEAR_BE;
  565. #endif
  566. }
  567. if (sc->sw_enc == AUDIO_ENCODING_ULINEAR) {
  568. #if BYTE_ORDER == LITTLE_ENDIAN
  569. sc->sw_enc = AUDIO_ENCODING_ULINEAR_LE;
  570. #else
  571. sc->sw_enc = AUDIO_ENCODING_ULINEAR_BE;
  572. #endif
  573. }
  574. /*
  575. * check if requested parameters are in the allowed ranges
  576. */
  577. if (sc->mode & AUMODE_PLAY) {
  578. if (sc->pchan < 1)
  579. sc->pchan = 1;
  580. if (sc->pchan > 64)
  581. sc->pchan = 64;
  582. }
  583. if (sc->mode & AUMODE_RECORD) {
  584. if (sc->rchan < 1)
  585. sc->rchan = 1;
  586. if (sc->rchan > 64)
  587. sc->rchan = 64;
  588. }
  589. switch (sc->sw_enc) {
  590. case AUDIO_ENCODING_ULAW:
  591. case AUDIO_ENCODING_ALAW:
  592. case AUDIO_ENCODING_SLINEAR_LE:
  593. case AUDIO_ENCODING_SLINEAR_BE:
  594. case AUDIO_ENCODING_ULINEAR_LE:
  595. case AUDIO_ENCODING_ULINEAR_BE:
  596. break;
  597. default:
  598. sc->sw_enc = AUDIO_ENCODING_SLINEAR_LE;
  599. }
  600. if (sc->bits < 8)
  601. sc->bits = 8;
  602. if (sc->bits > 32)
  603. sc->bits = 32;
  604. if (sc->bps < 1)
  605. sc->bps = 1;
  606. if (sc->bps > 4)
  607. sc->bps = 4;
  608. if (sc->rate < 4000)
  609. sc->rate = 4000;
  610. if (sc->rate > 192000)
  611. sc->rate = 192000;
  612. /*
  613. * copy into struct audio_params, required by drivers
  614. */
  615. p.encoding = r.encoding = sc->sw_enc;
  616. p.precision = r.precision = sc->bits;
  617. p.bps = r.bps = sc->bps;
  618. p.msb = r.msb = sc->msb;
  619. p.sample_rate = r.sample_rate = sc->rate;
  620. p.channels = sc->pchan;
  621. r.channels = sc->rchan;
  622. /*
  623. * set parameters
  624. */
  625. error = sc->ops->set_params(sc->arg, sc->mode, sc->mode, &p, &r);
  626. if (error)
  627. return error;
  628. if (sc->mode == (AUMODE_PLAY | AUMODE_RECORD)) {
  629. if (p.encoding != r.encoding ||
  630. p.precision != r.precision ||
  631. p.bps != r.bps ||
  632. p.msb != r.msb ||
  633. p.sample_rate != r.sample_rate) {
  634. printf("%s: different play and record parameters "
  635. "returned by hardware\n", DEVNAME(sc));
  636. return ENODEV;
  637. }
  638. }
  639. if (sc->mode & AUMODE_PLAY) {
  640. sc->hw_enc = p.encoding;
  641. sc->bits = p.precision;
  642. sc->bps = p.bps;
  643. sc->msb = p.msb;
  644. sc->rate = p.sample_rate;
  645. sc->pchan = p.channels;
  646. }
  647. if (sc->mode & AUMODE_RECORD) {
  648. sc->hw_enc = r.encoding;
  649. sc->bits = r.precision;
  650. sc->bps = r.bps;
  651. sc->msb = r.msb;
  652. sc->rate = r.sample_rate;
  653. sc->rchan = r.channels;
  654. }
  655. if (sc->rate == 0 || sc->bps == 0 || sc->bits == 0) {
  656. printf("%s: invalid parameters returned by hardware\n",
  657. DEVNAME(sc));
  658. return ENODEV;
  659. }
  660. if (sc->ops->commit_settings) {
  661. error = sc->ops->commit_settings(sc->arg);
  662. if (error)
  663. return error;
  664. }
  665. /*
  666. * conversion from/to exotic/dead encoding, for drivers not supporting
  667. * linear
  668. */
  669. switch (sc->hw_enc) {
  670. case AUDIO_ENCODING_SLINEAR_LE:
  671. case AUDIO_ENCODING_SLINEAR_BE:
  672. case AUDIO_ENCODING_ULINEAR_LE:
  673. case AUDIO_ENCODING_ULINEAR_BE:
  674. sc->sw_enc = sc->hw_enc;
  675. sc->conv_dec = sc->conv_enc = NULL;
  676. break;
  677. case AUDIO_ENCODING_ULAW:
  678. #if BYTE_ORDER == LITTLE_ENDIAN
  679. sc->sw_enc = AUDIO_ENCODING_SLINEAR_LE;
  680. #else
  681. sc->sw_enc = AUDIO_ENCODING_SLINEAR_BE;
  682. #endif
  683. if (sc->bits == 8) {
  684. sc->conv_enc = slinear8_to_mulaw;
  685. sc->conv_dec = mulaw_to_slinear8;
  686. break;
  687. } else if (sc->bits == 24) {
  688. sc->conv_enc = slinear24_to_mulaw24;
  689. sc->conv_dec = mulaw24_to_slinear24;
  690. break;
  691. }
  692. sc->sw_enc = sc->hw_enc;
  693. sc->conv_dec = sc->conv_enc = NULL;
  694. break;
  695. default:
  696. printf("%s: setpar: enc = %d, bits = %d: emulation skipped\n",
  697. DEVNAME(sc), sc->hw_enc, sc->bits);
  698. sc->sw_enc = sc->hw_enc;
  699. sc->conv_dec = sc->conv_enc = NULL;
  700. }
  701. audio_calc_sil(sc);
  702. /*
  703. * get least multiplier of the number of frames per block
  704. */
  705. if (sc->ops->round_blocksize) {
  706. mult = sc->ops->round_blocksize(sc->arg, 1);
  707. if (mult == 0) {
  708. printf("%s: 0x%x: bad block size multiplier\n",
  709. DEVNAME(sc), mult);
  710. return ENODEV;
  711. }
  712. } else
  713. mult = 1;
  714. DPRINTF("%s: hw block size multiplier: %u\n", DEVNAME(sc), mult);
  715. if (sc->mode & AUMODE_PLAY) {
  716. np = mult / audio_gcd(sc->pchan * sc->bps, mult);
  717. if (!(sc->mode & AUMODE_RECORD))
  718. nr = np;
  719. DPRINTF("%s: play number of frames multiplier: %u\n",
  720. DEVNAME(sc), np);
  721. }
  722. if (sc->mode & AUMODE_RECORD) {
  723. nr = mult / audio_gcd(sc->rchan * sc->bps, mult);
  724. if (!(sc->mode & AUMODE_PLAY))
  725. np = nr;
  726. DPRINTF("%s: record number of frames multiplier: %u\n",
  727. DEVNAME(sc), nr);
  728. }
  729. mult = nr * np / audio_gcd(nr, np);
  730. DPRINTF("%s: least common number of frames multiplier: %u\n",
  731. DEVNAME(sc), mult);
  732. /*
  733. * get minumum and maximum frames per block
  734. */
  735. if (sc->mode & AUMODE_PLAY) {
  736. np = sc->play.datalen / (sc->pchan * sc->bps * 2);
  737. if (!(sc->mode & AUMODE_RECORD))
  738. nr = np;
  739. }
  740. if (sc->mode & AUMODE_RECORD) {
  741. nr = sc->rec.datalen / (sc->rchan * sc->bps * 2);
  742. if (!(sc->mode & AUMODE_PLAY))
  743. np = nr;
  744. }
  745. max = np < nr ? np : nr;
  746. max -= max % mult;
  747. min = sc->rate / 1000 + mult - 1;
  748. min -= min % mult;
  749. DPRINTF("%s: frame number range: %u..%u\n", DEVNAME(sc), min, max);
  750. if (max < min) {
  751. printf("%s: %u: bad max frame number\n", DEVNAME(sc), max);
  752. return EIO;
  753. }
  754. /*
  755. * adjust the frame per block to match our constraints
  756. */
  757. sc->round += mult / 2;
  758. sc->round -= sc->round % mult;
  759. if (sc->round > max)
  760. sc->round = max;
  761. if (sc->round < min)
  762. sc->round = min;
  763. sc->round = sc->round;
  764. /*
  765. * set buffer size (number of blocks)
  766. */
  767. if (sc->mode & AUMODE_PLAY) {
  768. sc->play.blksz = sc->round * sc->pchan * sc->bps;
  769. max = sc->play.datalen / sc->play.blksz;
  770. if (sc->nblks > max)
  771. sc->nblks = max;
  772. if (sc->nblks < 2)
  773. sc->nblks = 2;
  774. sc->play.len = sc->nblks * sc->play.blksz;
  775. sc->nblks = sc->nblks;
  776. }
  777. if (sc->mode & AUMODE_RECORD) {
  778. /*
  779. * for recording, buffer size is not the latency (it's
  780. * exactly one block), so let's get the maximum buffer
  781. * size of maximum reliability during xruns
  782. */
  783. sc->rec.blksz = sc->round * sc->rchan * sc->bps;
  784. sc->rec.len = sc->rec.datalen;
  785. sc->rec.len -= sc->rec.datalen % sc->rec.blksz;
  786. }
  787. DPRINTF("%s: setpar: new enc=%d bits=%d, bps=%d, msb=%d "
  788. "rate=%d, pchan=%d, rchan=%d, round=%u, nblks=%d\n",
  789. DEVNAME(sc), sc->sw_enc, sc->bits, sc->bps, sc->msb,
  790. sc->rate, sc->pchan, sc->rchan, sc->round, sc->nblks);
  791. return 0;
  792. }
  793. int
  794. audio_setinfo(struct audio_softc *sc, struct audio_info *ai)
  795. {
  796. struct audio_prinfo *r = &ai->record, *p = &ai->play;
  797. int error;
  798. int set;
  799. /*
  800. * stop the device if requested to stop
  801. */
  802. if (sc->mode != 0) {
  803. if (sc->mode & AUMODE_PLAY) {
  804. if (p->pause != (unsigned char)~0)
  805. sc->pause = p->pause;
  806. }
  807. if (sc->mode & AUMODE_RECORD) {
  808. if (r->pause != (unsigned char)~0)
  809. sc->pause = r->pause;
  810. }
  811. if (sc->pause) {
  812. if (sc->active)
  813. audio_stop(sc);
  814. }
  815. }
  816. /*
  817. * copy parameters into the softc structure
  818. */
  819. set = 0;
  820. if (ai->play.encoding != ~0) {
  821. sc->sw_enc = ai->play.encoding;
  822. set = 1;
  823. }
  824. if (ai->play.precision != ~0) {
  825. sc->bits = ai->play.precision;
  826. set = 1;
  827. }
  828. if (ai->play.bps != ~0) {
  829. sc->bps = ai->play.bps;
  830. set = 1;
  831. }
  832. if (ai->play.msb != ~0) {
  833. sc->msb = ai->play.msb;
  834. set = 1;
  835. }
  836. if (ai->play.sample_rate != ~0) {
  837. sc->rate = ai->play.sample_rate;
  838. set = 1;
  839. }
  840. if (ai->play.channels != ~0) {
  841. sc->pchan = ai->play.channels;
  842. set = 1;
  843. }
  844. if (ai->play.block_size != ~0) {
  845. sc->round = ai->play.block_size /
  846. (sc->bps * sc->pchan);
  847. set = 1;
  848. }
  849. if (ai->hiwat != ~0) {
  850. sc->nblks = ai->hiwat;
  851. set = 1;
  852. }
  853. if (ai->record.encoding != ~0) {
  854. sc->sw_enc = ai->record.encoding;
  855. set = 1;
  856. }
  857. if (ai->record.precision != ~0) {
  858. sc->bits = ai->record.precision;
  859. set = 1;
  860. }
  861. if (ai->record.bps != ~0) {
  862. sc->bps = ai->record.bps;
  863. set = 1;
  864. }
  865. if (ai->record.msb != ~0) {
  866. sc->msb = ai->record.msb;
  867. set = 1;
  868. }
  869. if (ai->record.sample_rate != ~0) {
  870. sc->rate = ai->record.sample_rate;
  871. set = 1;
  872. }
  873. if (ai->record.channels != ~0) {
  874. sc->rchan = ai->record.channels;
  875. set = 1;
  876. }
  877. if (ai->record.block_size != ~0) {
  878. sc->round = ai->record.block_size /
  879. (sc->bps * sc->rchan);
  880. set = 1;
  881. }
  882. DPRINTF("%s: setinfo: set = %d, mode = %d, pause = %d\n",
  883. DEVNAME(sc), set, sc->mode, sc->pause);
  884. /*
  885. * if the device not opened, we're done, don't touch the hardware
  886. */
  887. if (sc->mode == 0)
  888. return 0;
  889. /*
  890. * change parameters and recalculate buffer sizes
  891. */
  892. if (set) {
  893. if (sc->active) {
  894. DPRINTF("%s: can't change params during dma\n",
  895. DEVNAME(sc));
  896. return EBUSY;
  897. }
  898. error = audio_setpar(sc);
  899. if (error)
  900. return error;
  901. audio_clear(sc);
  902. if ((sc->mode & AUMODE_PLAY) && sc->ops->init_output) {
  903. error = sc->ops->init_output(sc->arg,
  904. sc->play.data, sc->play.len);
  905. if (error)
  906. return error;
  907. }
  908. if ((sc->mode & AUMODE_RECORD) && sc->ops->init_input) {
  909. error = sc->ops->init_input(sc->arg,
  910. sc->rec.data, sc->rec.len);
  911. if (error)
  912. return error;
  913. }
  914. }
  915. /*
  916. * if unpaused, start
  917. */
  918. if (!sc->pause && !sc->active) {
  919. error = audio_start(sc);
  920. if (error)
  921. return error;
  922. }
  923. return 0;
  924. }
  925. int
  926. audio_getinfo(struct audio_softc *sc, struct audio_info *ai)
  927. {
  928. ai->play.sample_rate = ai->record.sample_rate = sc->rate;
  929. ai->play.encoding = ai->record.encoding = sc->sw_enc;
  930. ai->play.precision = ai->record.precision = sc->bits;
  931. ai->play.bps = ai->record.bps = sc->bps;
  932. ai->play.msb = ai->record.msb = sc->msb;
  933. ai->play.channels = sc->pchan;
  934. ai->record.channels = sc->rchan;
  935. /*
  936. * XXX: this is used only to display counters through audioctl
  937. * and the pos counters are more useful
  938. */
  939. mtx_enter(&audio_lock);
  940. ai->play.samples = sc->play.pos - sc->play.xrun;
  941. ai->record.samples = sc->rec.pos - sc->rec.xrun;
  942. mtx_leave(&audio_lock);
  943. ai->play.pause = ai->record.pause = sc->pause;
  944. ai->play.active = ai->record.active = sc->active;
  945. ai->play.buffer_size = sc->play.datalen;
  946. ai->record.buffer_size = sc->rec.datalen;
  947. ai->play.block_size = sc->round * sc->bps * sc->pchan;
  948. ai->record.block_size = sc->round * sc->bps * sc->rchan;
  949. ai->hiwat = sc->nblks;
  950. ai->lowat = sc->nblks;
  951. ai->mode = sc->mode;
  952. return 0;
  953. }
  954. int
  955. audio_match(struct device *parent, void *match, void *aux)
  956. {
  957. struct audio_attach_args *sa = aux;
  958. return (sa->type == AUDIODEV_TYPE_AUDIO) ? 1 : 0;
  959. }
  960. void
  961. audio_attach(struct device *parent, struct device *self, void *aux)
  962. {
  963. struct audio_softc *sc = (void *)self;
  964. struct audio_attach_args *sa = aux;
  965. struct audio_hw_if *ops = sa->hwif;
  966. void *arg = sa->hdl;
  967. int error;
  968. printf("\n");
  969. #ifdef DIAGNOSTIC
  970. if (ops == 0 ||
  971. ops->open == 0 ||
  972. ops->close == 0 ||
  973. ops->query_encoding == 0 ||
  974. ops->set_params == 0 ||
  975. (ops->start_output == 0 && ops->trigger_output == 0) ||
  976. (ops->start_input == 0 && ops->trigger_input == 0) ||
  977. ops->halt_output == 0 ||
  978. ops->halt_input == 0 ||
  979. ops->getdev == 0 ||
  980. ops->set_port == 0 ||
  981. ops->get_port == 0 ||
  982. ops->query_devinfo == 0 ||
  983. ops->get_props == 0) {
  984. printf("%s: missing method\n", DEVNAME(sc));
  985. sc->ops = 0;
  986. return;
  987. }
  988. #endif
  989. sc->ops = ops;
  990. sc->arg = arg;
  991. #if NWSKBD > 0
  992. wskbd_mixer_init(sc);
  993. #endif /* NWSKBD > 0 */
  994. error = audio_buf_init(sc, &sc->play, AUMODE_PLAY);
  995. if (error) {
  996. sc->ops = 0;
  997. printf("%s: could not allocate play buffer\n", DEVNAME(sc));
  998. return;
  999. }
  1000. error = audio_buf_init(sc, &sc->rec, AUMODE_RECORD);
  1001. if (error) {
  1002. audio_buf_done(sc, &sc->play);
  1003. sc->ops = 0;
  1004. printf("%s: could not allocate record buffer\n", DEVNAME(sc));
  1005. return;
  1006. }
  1007. /* set defaults */
  1008. sc->sw_enc = AUDIO_ENCODING_SLINEAR;
  1009. sc->bits = 16;
  1010. sc->bps = 2;
  1011. sc->msb = 1;
  1012. sc->rate = 48000;
  1013. sc->pchan = 2;
  1014. sc->rchan = 2;
  1015. sc->round = 960;
  1016. sc->nblks = 2;
  1017. sc->play.pos = sc->play.xrun = sc->rec.pos = sc->rec.xrun = 0;
  1018. }
  1019. int
  1020. audio_activate(struct device *self, int act)
  1021. {
  1022. struct audio_softc *sc = (struct audio_softc *)self;
  1023. switch (act) {
  1024. case DVACT_QUIESCE:
  1025. /*
  1026. * good drivers run play and rec handlers in a single
  1027. * interrupt. Grab the lock to ensure we expose the same
  1028. * sc->quiesce value to both play and rec handlers
  1029. */
  1030. mtx_enter(&audio_lock);
  1031. sc->quiesce = 1;
  1032. mtx_leave(&audio_lock);
  1033. /*
  1034. * once sc->quiesce is set, interrupts may occur, but
  1035. * counters are not advanced and consequently processes
  1036. * keep sleeping.
  1037. *
  1038. * XXX: ensure read/write/ioctl don't start/stop
  1039. * DMA at the same time, this needs a "ready" condvar
  1040. */
  1041. if (sc->mode != 0 && sc->active)
  1042. audio_stop_do(sc);
  1043. DPRINTF("%s: quesce: active = %d\n", DEVNAME(sc), sc->active);
  1044. break;
  1045. case DVACT_WAKEUP:
  1046. DPRINTF("%s: wakeup: active = %d\n", DEVNAME(sc), sc->active);
  1047. /*
  1048. * keep buffer usage the same, but set start pointer to
  1049. * the beginning of the buffer.
  1050. *
  1051. * No need to grab the audio_lock as DMA is stopped and
  1052. * this is the only thread running (caller ensures this)
  1053. */
  1054. sc->quiesce = 0;
  1055. wakeup(&sc->quiesce);
  1056. if(sc->mode != 0) {
  1057. if (audio_setpar(sc) != 0)
  1058. break;
  1059. if (sc->mode & AUMODE_PLAY) {
  1060. sc->play.start = 0;
  1061. audio_fill_sil(sc, sc->play.data, sc->play.len);
  1062. }
  1063. if (sc->mode & AUMODE_RECORD) {
  1064. sc->rec.start = sc->rec.len - sc->rec.used;
  1065. audio_fill_sil(sc, sc->rec.data, sc->rec.len);
  1066. }
  1067. if (sc->active)
  1068. audio_start_do(sc);
  1069. }
  1070. break;
  1071. }
  1072. return 0;
  1073. }
  1074. int
  1075. audio_detach(struct device *self, int flags)
  1076. {
  1077. struct audio_softc *sc = (struct audio_softc *)self;
  1078. int maj, mn;
  1079. DPRINTF("%s: audio_detach: flags = %d\n", DEVNAME(sc), flags);
  1080. wakeup(&sc->quiesce);
  1081. /* locate the major number */
  1082. for (maj = 0; maj < nchrdev; maj++)
  1083. if (cdevsw[maj].d_open == audioopen)
  1084. break;
  1085. /*
  1086. * Nuke the vnodes for any open instances, calls close but as
  1087. * close uses device_lookup, it returns EXIO and does nothing
  1088. */
  1089. mn = self->dv_unit;
  1090. vdevgone(maj, mn | AUDIO_DEV_SOUND, mn | AUDIO_DEV_SOUND, VCHR);
  1091. vdevgone(maj, mn | AUDIO_DEV_AUDIO, mn | AUDIO_DEV_AUDIO, VCHR);
  1092. vdevgone(maj, mn | AUDIO_DEV_AUDIOCTL, mn | AUDIO_DEV_AUDIOCTL, VCHR);
  1093. vdevgone(maj, mn | AUDIO_DEV_MIXER, mn | AUDIO_DEV_MIXER, VCHR);
  1094. /*
  1095. * The close() method did nothing, quickly halt DMA (normally
  1096. * parent is already gone, and code below is no-op), and wake-up
  1097. * user-land blocked in read/write/ioctl, which return EIO.
  1098. */
  1099. if (sc->mode != 0) {
  1100. if (sc->active) {
  1101. wakeup(&sc->play.blocking);
  1102. selwakeup(&sc->play.sel);
  1103. wakeup(&sc->rec.blocking);
  1104. selwakeup(&sc->rec.sel);
  1105. audio_stop(sc);
  1106. }
  1107. sc->ops->close(sc->arg);
  1108. sc->mode = 0;
  1109. }
  1110. /* free resources */
  1111. audio_buf_done(sc, &sc->play);
  1112. audio_buf_done(sc, &sc->rec);
  1113. return 0;
  1114. }
  1115. struct device *
  1116. audio_attach_mi(struct audio_hw_if *ops, void *arg, struct device *dev)
  1117. {
  1118. struct audio_attach_args aa;
  1119. aa.type = AUDIODEV_TYPE_AUDIO;
  1120. aa.hwif = ops;
  1121. aa.hdl = arg;
  1122. /*
  1123. * attach this driver to the caller (hardware driver), this
  1124. * checks the kernel config and possibly calls audio_attach()
  1125. */
  1126. return config_found(dev, &aa, audioprint);
  1127. }
  1128. int
  1129. audioprint(void *aux, const char *pnp)
  1130. {
  1131. struct audio_attach_args *arg = aux;
  1132. const char *type;
  1133. if (pnp != NULL) {
  1134. switch (arg->type) {
  1135. case AUDIODEV_TYPE_AUDIO:
  1136. type = "audio";
  1137. break;
  1138. case AUDIODEV_TYPE_OPL:
  1139. type = "opl";
  1140. break;
  1141. case AUDIODEV_TYPE_MPU:
  1142. type = "mpu";
  1143. break;
  1144. default:
  1145. panic("audioprint: unknown type %d", arg->type);
  1146. }
  1147. printf("%s at %s", type, pnp);
  1148. }
  1149. return UNCONF;
  1150. }
  1151. int
  1152. audio_open(struct audio_softc *sc, int flags)
  1153. {
  1154. int error;
  1155. int props;
  1156. if (sc->mode)
  1157. return EBUSY;
  1158. error = sc->ops->open(sc->arg, flags);
  1159. if (error)
  1160. return error;
  1161. sc->active = 0;
  1162. sc->pause = 1;
  1163. sc->rec.blocking = 0;
  1164. sc->play.blocking = 0;
  1165. sc->mode = 0;
  1166. if (flags & FWRITE)
  1167. sc->mode |= AUMODE_PLAY;
  1168. if (flags & FREAD)
  1169. sc->mode |= AUMODE_RECORD;
  1170. props = sc->ops->get_props(sc->arg);
  1171. if (sc->mode == (AUMODE_PLAY | AUMODE_RECORD)) {
  1172. if (!(props & AUDIO_PROP_FULLDUPLEX)) {
  1173. error = ENOTTY;
  1174. goto bad;
  1175. }
  1176. if (sc->ops->setfd) {
  1177. error = sc->ops->setfd(sc->arg, 1);
  1178. if (error)
  1179. goto bad;
  1180. }
  1181. }
  1182. if (sc->ops->speaker_ctl) {
  1183. /*
  1184. * XXX: what is this used for?
  1185. */
  1186. sc->ops->speaker_ctl(sc->arg,
  1187. (sc->mode & AUMODE_PLAY) ? SPKR_ON : SPKR_OFF);
  1188. }
  1189. error = audio_setpar(sc);
  1190. if (error)
  1191. goto bad;
  1192. audio_clear(sc);
  1193. /*
  1194. * allow read(2)/write(2) to automatically start DMA, without
  1195. * the need for ioctl(), to make /dev/audio usable in scripts
  1196. */
  1197. sc->pause = 0;
  1198. return 0;
  1199. bad:
  1200. sc->ops->close(sc->arg);
  1201. sc->mode = 0;
  1202. return error;
  1203. }
  1204. int
  1205. audio_drain(struct audio_softc *sc)
  1206. {
  1207. int error, xrun;
  1208. unsigned char *ptr;
  1209. size_t count, bpf;
  1210. DPRINTF("%s: drain: mode = %d, pause = %d, active = %d, used = %zu\n",
  1211. DEVNAME(sc), sc->mode, sc->pause, sc->active, sc->play.used);
  1212. if (!(sc->mode & AUMODE_PLAY) || sc->pause)
  1213. return 0;
  1214. /* discard partial samples, required by audio_fill_sil() */
  1215. mtx_enter(&audio_lock);
  1216. bpf = sc->pchan * sc->bps;
  1217. sc->play.used -= sc->play.used % bpf;
  1218. if (sc->play.used == 0) {
  1219. mtx_leave(&audio_lock);
  1220. return 0;
  1221. }
  1222. if (!sc->active) {
  1223. /*
  1224. * dma not started yet because buffer was not full
  1225. * enough to start automatically. Pad it and start now.
  1226. */
  1227. for (;;) {
  1228. ptr = audio_buf_wgetblk(&sc->play, &count);
  1229. if (count == 0)
  1230. break;
  1231. audio_fill_sil(sc, ptr, count);
  1232. audio_buf_wcommit(&sc->play, count);
  1233. }
  1234. mtx_leave(&audio_lock);
  1235. error = audio_start(sc);
  1236. if (error)
  1237. return error;
  1238. mtx_enter(&audio_lock);
  1239. }
  1240. xrun = sc->play.xrun;
  1241. while (sc->play.xrun == xrun) {
  1242. DPRINTF("%s: drain: used = %zu, xrun = %d\n",
  1243. DEVNAME(sc), sc->play.used, sc->play.xrun);
  1244. /*
  1245. * set a 5 second timeout, in case interrupts don't
  1246. * work, useful only for debugging drivers
  1247. */
  1248. sc->play.blocking = 1;
  1249. error = msleep(&sc->play.blocking, &audio_lock,
  1250. PWAIT | PCATCH, "au_dr", 5 * hz);
  1251. if (!(sc->dev.dv_flags & DVF_ACTIVE))
  1252. error = EIO;
  1253. if (error) {
  1254. DPRINTF("%s: drain, err = %d\n", DEVNAME(sc), error);
  1255. break;
  1256. }
  1257. }
  1258. mtx_leave(&audio_lock);
  1259. return error;
  1260. }
  1261. int
  1262. audio_close(struct audio_softc *sc)
  1263. {
  1264. audio_drain(sc);
  1265. if (sc->active)
  1266. audio_stop(sc);
  1267. sc->ops->close(sc->arg);
  1268. sc->mode = 0;
  1269. DPRINTF("%s: close: done\n", DEVNAME(sc));
  1270. return 0;
  1271. }
  1272. int
  1273. audio_read(struct audio_softc *sc, struct uio *uio, int ioflag)
  1274. {
  1275. unsigned char *ptr;
  1276. size_t count;
  1277. int error;
  1278. DPRINTFN(1, "%s: read: resid = %zd\n", DEVNAME(sc), uio->uio_resid);
  1279. /* block if quiesced */
  1280. while (sc->quiesce)
  1281. tsleep(&sc->quiesce, 0, "au_qrd", 0);
  1282. /* start automatically if setinfo() was never called */
  1283. mtx_enter(&audio_lock);
  1284. if (!sc->active && !sc->pause && sc->rec.used == 0) {
  1285. mtx_leave(&audio_lock);
  1286. error = audio_start(sc);
  1287. if (error)
  1288. return error;
  1289. mtx_enter(&audio_lock);
  1290. }
  1291. /* if there is no data then sleep */
  1292. while (sc->rec.used == 0) {
  1293. if (ioflag & IO_NDELAY) {
  1294. mtx_leave(&audio_lock);
  1295. return EWOULDBLOCK;
  1296. }
  1297. DPRINTFN(1, "%s: read sleep\n", DEVNAME(sc));
  1298. sc->rec.blocking = 1;
  1299. error = msleep(&sc->rec.blocking,
  1300. &audio_lock, PWAIT | PCATCH, "au_rd", 0);
  1301. if (!(sc->dev.dv_flags & DVF_ACTIVE))
  1302. error = EIO;
  1303. #ifdef AUDIO_DEBUG
  1304. if (error) {
  1305. DPRINTF("%s: read woke up error = %d\n",
  1306. DEVNAME(sc), error);
  1307. }
  1308. #endif
  1309. if (error) {
  1310. mtx_leave(&audio_lock);
  1311. return error;
  1312. }
  1313. }
  1314. /* at this stage, there is data to transfer */
  1315. while (uio->uio_resid > 0 && sc->rec.used > 0) {
  1316. ptr = audio_buf_rgetblk(&sc->rec, &count);
  1317. if (count > uio->uio_resid)
  1318. count = uio->uio_resid;
  1319. mtx_leave(&audio_lock);
  1320. DPRINTFN(1, "%s: read: start = %zu, count = %zu\n",
  1321. DEVNAME(sc), ptr - sc->rec.data, count);
  1322. if (sc->conv_dec)
  1323. sc->conv_dec(ptr, count);
  1324. error = uiomove(ptr, count, uio);
  1325. if (error)
  1326. return error;
  1327. mtx_enter(&audio_lock);
  1328. audio_buf_rdiscard(&sc->rec, count);
  1329. }
  1330. mtx_leave(&audio_lock);
  1331. return 0;
  1332. }
  1333. int
  1334. audio_write(struct audio_softc *sc, struct uio *uio, int ioflag)
  1335. {
  1336. unsigned char *ptr;
  1337. size_t count;
  1338. int error;
  1339. DPRINTFN(1, "%s: write: resid = %zd\n", DEVNAME(sc), uio->uio_resid);
  1340. /* block if quiesced */
  1341. while (sc->quiesce)
  1342. tsleep(&sc->quiesce, 0, "au_qwr", 0);
  1343. /*
  1344. * if IO_NDELAY flag is set then check if there is enough room
  1345. * in the buffer to store at least one byte. If not then dont
  1346. * start the write process.
  1347. */
  1348. mtx_enter(&audio_lock);
  1349. if (uio->uio_resid > 0 && (ioflag & IO_NDELAY)) {
  1350. if (sc->play.used == sc->play.len) {
  1351. mtx_leave(&audio_lock);
  1352. return EWOULDBLOCK;
  1353. }
  1354. }
  1355. while (uio->uio_resid > 0) {
  1356. while (1) {
  1357. ptr = audio_buf_wgetblk(&sc->play, &count);
  1358. if (count > 0)
  1359. break;
  1360. if (ioflag & IO_NDELAY) {
  1361. /*
  1362. * At this stage at least one byte is already
  1363. * moved so we do not return EWOULDBLOCK
  1364. */
  1365. mtx_leave(&audio_lock);
  1366. return 0;
  1367. }
  1368. DPRINTFN(1, "%s: write sleep\n", DEVNAME(sc));
  1369. sc->play.blocking = 1;
  1370. error = msleep(&sc->play.blocking,
  1371. &audio_lock, PWAIT | PCATCH, "au_wr", 0);
  1372. if (!(sc->dev.dv_flags & DVF_ACTIVE))
  1373. error = EIO;
  1374. #ifdef AUDIO_DEBUG
  1375. if (error) {
  1376. DPRINTF("%s: write woke up error = %d\n",
  1377. DEVNAME(sc), error);
  1378. }
  1379. #endif
  1380. if (error) {
  1381. mtx_leave(&audio_lock);
  1382. return error;
  1383. }
  1384. }
  1385. if (count > uio->uio_resid)
  1386. count = uio->uio_resid;
  1387. mtx_leave(&audio_lock);
  1388. error = uiomove(ptr, count, uio);
  1389. if (error)
  1390. return 0;
  1391. if (sc->conv_enc) {
  1392. sc->conv_enc(ptr, count);
  1393. DPRINTFN(1, "audio_write: converted count = %zu\n",
  1394. count);
  1395. }
  1396. mtx_enter(&audio_lock);
  1397. audio_buf_wcommit(&sc->play, count);
  1398. /* start automatically if setinfo() was never called */
  1399. if (!sc->active && !sc->pause &&
  1400. sc->play.used == sc->play.len) {
  1401. mtx_leave(&audio_lock);
  1402. error = audio_start(sc);
  1403. if (error)
  1404. return error;
  1405. mtx_enter(&audio_lock);
  1406. }
  1407. }
  1408. mtx_leave(&audio_lock);
  1409. return 0;
  1410. }
  1411. int
  1412. audio_ioctl(struct audio_softc *sc, unsigned long cmd, void *addr)
  1413. {
  1414. struct audio_offset *ao;
  1415. struct audio_pos *ap;
  1416. int error = 0, fd;
  1417. /* block if quiesced */
  1418. while (sc->quiesce)
  1419. tsleep(&sc->quiesce, 0, "au_qio", 0);
  1420. switch (cmd) {
  1421. case FIONBIO:
  1422. /* All handled in the upper FS layer. */
  1423. break;
  1424. case AUDIO_PERROR:
  1425. mtx_enter(&audio_lock);
  1426. *(int *)addr = sc->play.xrun / (sc->pchan * sc->bps);
  1427. mtx_leave(&audio_lock);
  1428. break;
  1429. case AUDIO_RERROR:
  1430. mtx_enter(&audio_lock);
  1431. *(int *)addr = sc->rec.xrun / (sc->rchan * sc->bps);
  1432. mtx_leave(&audio_lock);
  1433. break;
  1434. case AUDIO_GETOOFFS:
  1435. mtx_enter(&audio_lock);
  1436. ao = (struct audio_offset *)addr;
  1437. ao->samples = sc->play.pos;
  1438. mtx_leave(&audio_lock);
  1439. break;
  1440. case AUDIO_GETIOFFS:
  1441. mtx_enter(&audio_lock);
  1442. ao = (struct audio_offset *)addr;
  1443. ao->samples = sc->rec.pos;
  1444. mtx_leave(&audio_lock);
  1445. break;
  1446. case AUDIO_GETPOS:
  1447. mtx_enter(&audio_lock);
  1448. ap = (struct audio_pos *)addr;
  1449. ap->play_pos = sc->play.pos;
  1450. ap->play_xrun = sc->play.xrun;
  1451. ap->rec_pos = sc->rec.pos;
  1452. ap->rec_xrun = sc->rec.xrun;
  1453. mtx_leave(&audio_lock);
  1454. break;
  1455. case AUDIO_SETINFO:
  1456. error = audio_setinfo(sc, (struct audio_info *)addr);
  1457. break;
  1458. case AUDIO_GETINFO:
  1459. error = audio_getinfo(sc, (struct audio_info *)addr);
  1460. break;
  1461. case AUDIO_GETDEV:
  1462. error = sc->ops->getdev(sc->arg, (audio_device_t *)addr);
  1463. break;
  1464. case AUDIO_GETENC:
  1465. error = sc->ops->query_encoding(sc->arg,
  1466. (struct audio_encoding *)addr);
  1467. break;
  1468. case AUDIO_GETFD:
  1469. *(int *)addr = (sc->mode & (AUMODE_PLAY | AUMODE_RECORD)) ==
  1470. (AUMODE_PLAY | AUMODE_RECORD);
  1471. break;
  1472. case AUDIO_SETFD:
  1473. fd = *(int *)addr;
  1474. if ((sc->mode & (AUMODE_PLAY | AUMODE_RECORD)) !=
  1475. (AUMODE_PLAY | AUMODE_RECORD) || !fd)
  1476. return EINVAL;
  1477. break;
  1478. case AUDIO_GETPROPS:
  1479. *(int *)addr = sc->ops->get_props(sc->arg);
  1480. break;
  1481. default:
  1482. DPRINTF("%s: unknown ioctl 0x%lx\n", DEVNAME(sc), cmd);
  1483. error = ENOTTY;
  1484. break;
  1485. }
  1486. return error;
  1487. }
  1488. int
  1489. audio_ioctl_mixer(struct audio_softc *sc, unsigned long cmd, void *addr)
  1490. {
  1491. int error;
  1492. /* block if quiesced */
  1493. while (sc->quiesce)
  1494. tsleep(&sc->quiesce, 0, "mix_qio", 0);
  1495. switch (cmd) {
  1496. case FIONBIO:
  1497. /* All handled in the upper FS layer. */
  1498. break;
  1499. case AUDIO_MIXER_DEVINFO:
  1500. ((mixer_devinfo_t *)addr)->un.v.delta = 0;
  1501. return sc->ops->query_devinfo(sc->arg, (mixer_devinfo_t *)addr);
  1502. case AUDIO_MIXER_READ:
  1503. return sc->ops->get_port(sc->arg, (mixer_ctrl_t *)addr);
  1504. case AUDIO_MIXER_WRITE:
  1505. error = sc->ops->set_port(sc->arg, (mixer_ctrl_t *)addr);
  1506. if (error)
  1507. return error;
  1508. if (sc->ops->commit_settings)
  1509. return sc->ops->commit_settings(sc->arg);
  1510. break;
  1511. default:
  1512. return ENOTTY;
  1513. }
  1514. return 0;
  1515. }
  1516. int
  1517. audio_poll(struct audio_softc *sc, int events, struct proc *p)
  1518. {
  1519. int revents = 0;
  1520. mtx_enter(&audio_lock);
  1521. if ((sc->mode & AUMODE_RECORD) && sc->rec.used > 0)
  1522. revents |= events & (POLLIN | POLLRDNORM);
  1523. if ((sc->mode & AUMODE_PLAY) && sc->play.used < sc->play.len)
  1524. revents |= events & (POLLOUT | POLLWRNORM);
  1525. if (revents == 0) {
  1526. if (events & (POLLIN | POLLRDNORM))
  1527. selrecord(p, &sc->rec.sel);
  1528. if (events & (POLLOUT | POLLWRNORM))
  1529. selrecord(p, &sc->play.sel);
  1530. }
  1531. mtx_leave(&audio_lock);
  1532. return revents;
  1533. }
  1534. int
  1535. audioopen(dev_t dev, int flags, int mode, struct proc *p)
  1536. {
  1537. struct audio_softc *sc;
  1538. int error;
  1539. sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
  1540. if (sc == NULL)
  1541. return ENXIO;
  1542. if (sc->ops == NULL)
  1543. error = ENXIO;
  1544. else {
  1545. switch (AUDIO_DEV(dev)) {
  1546. case AUDIO_DEV_SOUND:
  1547. case AUDIO_DEV_AUDIO:
  1548. error = audio_open(sc, flags);
  1549. break;
  1550. case AUDIO_DEV_AUDIOCTL:
  1551. case AUDIO_DEV_MIXER:
  1552. error = 0;
  1553. break;
  1554. default:
  1555. error = ENXIO;
  1556. }
  1557. }
  1558. device_unref(&sc->dev);
  1559. return error;
  1560. }
  1561. int
  1562. audioclose(dev_t dev, int flags, int ifmt, struct proc *p)
  1563. {
  1564. struct audio_softc *sc;
  1565. int error;
  1566. sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
  1567. if (sc == NULL)
  1568. return ENXIO;
  1569. switch (AUDIO_DEV(dev)) {
  1570. case AUDIO_DEV_SOUND:
  1571. case AUDIO_DEV_AUDIO:
  1572. error = audio_close(sc);
  1573. break;
  1574. case AUDIO_DEV_MIXER:
  1575. case AUDIO_DEV_AUDIOCTL:
  1576. error = 0;
  1577. default:
  1578. error = ENXIO;
  1579. }
  1580. device_unref(&sc->dev);
  1581. return error;
  1582. }
  1583. int
  1584. audioread(dev_t dev, struct uio *uio, int ioflag)
  1585. {
  1586. struct audio_softc *sc;
  1587. int error;
  1588. sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
  1589. if (sc == NULL)
  1590. return ENXIO;
  1591. switch (AUDIO_DEV(dev)) {
  1592. case AUDIO_DEV_SOUND:
  1593. case AUDIO_DEV_AUDIO:
  1594. error = audio_read(sc, uio, ioflag);
  1595. break;
  1596. case AUDIO_DEV_AUDIOCTL:
  1597. case AUDIO_DEV_MIXER:
  1598. error = ENODEV;
  1599. break;
  1600. default:
  1601. error = ENXIO;
  1602. }
  1603. device_unref(&sc->dev);
  1604. return error;
  1605. }
  1606. int
  1607. audiowrite(dev_t dev, struct uio *uio, int ioflag)
  1608. {
  1609. struct audio_softc *sc;
  1610. int error;
  1611. sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
  1612. if (sc == NULL)
  1613. return ENXIO;
  1614. switch (AUDIO_DEV(dev)) {
  1615. case AUDIO_DEV_SOUND:
  1616. case AUDIO_DEV_AUDIO:
  1617. error = audio_write(sc, uio, ioflag);
  1618. break;
  1619. case AUDIO_DEV_AUDIOCTL:
  1620. case AUDIO_DEV_MIXER:
  1621. error = ENODEV;
  1622. break;
  1623. default:
  1624. error = ENXIO;
  1625. }
  1626. device_unref(&sc->dev);
  1627. return error;
  1628. }
  1629. int
  1630. audioioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
  1631. {
  1632. struct audio_softc *sc;
  1633. int error;
  1634. sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
  1635. if (sc == NULL)
  1636. return ENXIO;
  1637. switch (AUDIO_DEV(dev)) {
  1638. case AUDIO_DEV_SOUND:
  1639. case AUDIO_DEV_AUDIO:
  1640. error = audio_ioctl(sc, cmd, addr);
  1641. break;
  1642. case AUDIO_DEV_AUDIOCTL:
  1643. if (cmd == AUDIO_SETINFO && sc->mode != 0) {
  1644. error = EBUSY;
  1645. break;
  1646. }
  1647. error = audio_ioctl(sc, cmd, addr);
  1648. break;
  1649. case AUDIO_DEV_MIXER:
  1650. error = audio_ioctl_mixer(sc, cmd, addr);
  1651. break;
  1652. default:
  1653. error = ENXIO;
  1654. }
  1655. device_unref(&sc->dev);
  1656. return error;
  1657. }
  1658. int
  1659. audiopoll(dev_t dev, int events, struct proc *p)
  1660. {
  1661. struct audio_softc *sc;
  1662. int revents;
  1663. sc = (struct audio_softc *)device_lookup(&audio_cd, AUDIO_UNIT(dev));
  1664. if (sc == NULL)
  1665. return POLLERR;
  1666. switch (AUDIO_DEV(dev)) {
  1667. case AUDIO_DEV_SOUND:
  1668. case AUDIO_DEV_AUDIO:
  1669. revents = audio_poll(sc, events, p);
  1670. break;
  1671. case AUDIO_DEV_AUDIOCTL:
  1672. case AUDIO_DEV_MIXER:
  1673. revents = 0;
  1674. break;
  1675. default:
  1676. revents = 0;
  1677. }
  1678. device_unref(&sc->dev);
  1679. return revents;
  1680. }
  1681. #if NWSKBD > 0
  1682. int
  1683. wskbd_initmute(struct audio_softc *sc, struct mixer_devinfo *vol)
  1684. {
  1685. struct mixer_devinfo mi;
  1686. mi.index = vol->next;
  1687. for (mi.index = vol->next; mi.index != -1; mi.index = mi.next) {
  1688. if (sc->ops->query_devinfo(sc->arg, &mi) != 0)
  1689. break;
  1690. if (strcmp(mi.label.name, AudioNmute) == 0)
  1691. return mi.index;
  1692. }
  1693. return -1;
  1694. }
  1695. int
  1696. wskbd_initvol(struct audio_softc *sc, struct wskbd_vol *vol, char *cn, char *dn)
  1697. {
  1698. struct mixer_devinfo dev, cls;
  1699. for (dev.index = 0; ; dev.index++) {
  1700. if (sc->ops->query_devinfo(sc->arg, &dev) != 0)
  1701. break;
  1702. cls.index = dev.mixer_class;
  1703. if (sc->ops->query_devinfo(sc->arg, &cls) != 0)
  1704. continue;
  1705. if (strcmp(cls.label.name, cn) == 0 &&
  1706. strcmp(dev.label.name, dn) == 0) {
  1707. vol->val = dev.index;
  1708. vol->nch = dev.un.v.num_channels;
  1709. vol->step = dev.un.v.delta > 8 ? dev.un.v.delta : 8;
  1710. vol->mute = wskbd_initmute(sc, &dev);
  1711. vol->val_pending = vol->mute_pending = 0;
  1712. DPRINTF("%s: wskbd using %s.%s, %s\n",
  1713. DEVNAME(sc), cn, dn, vol->mute >= -1 ? "mute control" : "");
  1714. return 1;
  1715. }
  1716. }
  1717. vol->val = vol->mute = -1;
  1718. return 0;
  1719. }
  1720. void
  1721. wskbd_mixer_init(struct audio_softc *sc)
  1722. {
  1723. static struct {
  1724. char *cn, *dn;
  1725. } spkr_names[] = {
  1726. {AudioCoutputs, AudioNmaster},
  1727. {AudioCinputs, AudioNdac},
  1728. {AudioCoutputs, AudioNdac},
  1729. {AudioCoutputs, AudioNoutput}
  1730. }, mic_names[] = {
  1731. {AudioCrecord, AudioNrecord},
  1732. {AudioCrecord, AudioNvolume},
  1733. {AudioCinputs, AudioNrecord},
  1734. {AudioCinputs, AudioNvolume},
  1735. {AudioCinputs, AudioNinput}
  1736. };
  1737. int i;
  1738. if (sc->dev.dv_unit != 0) {
  1739. DPRINTF("%s: not configuring wskbd keys\n", DEVNAME(sc));
  1740. return;
  1741. }
  1742. for (i = 0; i < sizeof(spkr_names) / sizeof(spkr_names[0]); i++) {
  1743. if (wskbd_initvol(sc, &sc->spkr,
  1744. spkr_names[i].cn, spkr_names[i].dn))
  1745. break;
  1746. }
  1747. for (i = 0; i < sizeof(mic_names) / sizeof(mic_names[0]); i++) {
  1748. if (wskbd_initvol(sc, &sc->mic,
  1749. mic_names[i].cn, mic_names[i].dn))
  1750. break;
  1751. }
  1752. }
  1753. void
  1754. wskbd_mixer_update(struct audio_softc *sc, struct wskbd_vol *vol)
  1755. {
  1756. struct mixer_ctrl ctrl;
  1757. int val_pending, mute_pending, i, gain, error, s;
  1758. s = spltty();
  1759. val_pending = vol->val_pending;
  1760. vol->val_pending = 0;
  1761. mute_pending = vol->mute_pending;
  1762. vol->mute_pending = 0;
  1763. splx(s);
  1764. if (sc->ops == NULL)
  1765. return;
  1766. if (vol->mute >= 0 && mute_pending) {
  1767. ctrl.dev = vol->mute;
  1768. ctrl.type = AUDIO_MIXER_ENUM;
  1769. error = sc->ops->get_port(sc->arg, &ctrl);
  1770. if (error) {
  1771. DPRINTF("%s: get mute err = %d\n", DEVNAME(sc), error);
  1772. return;
  1773. }
  1774. ctrl.un.ord = ctrl.un.ord ^ mute_pending;
  1775. DPRINTFN(1, "%s: wskbd mute setting to %d\n",
  1776. DEVNAME(sc), ctrl.un.ord);
  1777. error = sc->ops->set_port(sc->arg, &ctrl);
  1778. if (error) {
  1779. DPRINTF("%s: set mute err = %d\n", DEVNAME(sc), error);
  1780. return;
  1781. }
  1782. }
  1783. if (vol->val >= 0 && val_pending) {
  1784. ctrl.dev = vol->val;
  1785. ctrl.type = AUDIO_MIXER_VALUE;
  1786. ctrl.un.value.num_channels = vol->nch;
  1787. error = sc->ops->get_port(sc->arg, &ctrl);
  1788. if (error) {
  1789. DPRINTF("%s: get mute err = %d\n", DEVNAME(sc), error);
  1790. return;
  1791. }
  1792. for (i = 0; i < vol->nch; i++) {
  1793. gain = ctrl.un.value.level[i] + vol->step * val_pending;
  1794. if (gain > AUDIO_MAX_GAIN)
  1795. gain = AUDIO_MAX_GAIN;
  1796. if (gain < AUDIO_MIN_GAIN)
  1797. gain = AUDIO_MIN_GAIN;
  1798. ctrl.un.value.level[i] = gain;
  1799. DPRINTFN(1, "%s: wskbd level %d set to %d\n",
  1800. DEVNAME(sc), i, gain);
  1801. }
  1802. error = sc->ops->set_port(sc->arg, &ctrl);
  1803. if (error) {
  1804. DPRINTF("%s: set vol err = %d\n", DEVNAME(sc), error);
  1805. return;
  1806. }
  1807. }
  1808. }
  1809. void
  1810. wskbd_mixer_cb(void *addr)
  1811. {
  1812. struct audio_softc *sc = addr;
  1813. int s;
  1814. wskbd_mixer_update(sc, &sc->spkr);
  1815. wskbd_mixer_update(sc, &sc->mic);
  1816. s = spltty();
  1817. sc->wskbd_taskset = 0;
  1818. splx(s);
  1819. device_unref(&sc->dev);
  1820. }
  1821. int
  1822. wskbd_set_mixervolume(long dir, long out)
  1823. {
  1824. struct audio_softc *sc;
  1825. struct wskbd_vol *vol;
  1826. sc = (struct audio_softc *)device_lookup(&audio_cd, 0);
  1827. if (sc == NULL)
  1828. return ENODEV;
  1829. vol = out ? &sc->spkr : &sc->mic;
  1830. if (dir == 0)
  1831. vol->mute_pending ^= 1;
  1832. else
  1833. vol->val_pending += dir;
  1834. if (!sc->wskbd_taskset) {
  1835. task_set(&sc->wskbd_task, wskbd_mixer_cb, sc);
  1836. task_add(systq, &sc->wskbd_task);
  1837. sc->wskbd_taskset = 1;
  1838. }
  1839. return 0;
  1840. }
  1841. #endif /* NWSKBD > 0 */