devuda1341.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. /*
  2. * SAC/UDA 1341 Audio driver for the Bitsy
  3. *
  4. * The Philips UDA 1341 sound chip is accessed through the Serial Audio
  5. * Controller (SAC) of the StrongARM SA-1110. This is much more a SAC
  6. * controller than a UDA controller, but we have a devsac.c already.
  7. *
  8. * The code morphs Nicolas Pitre's <nico@cam.org> Linux controller
  9. * and Ken's Soundblaster controller.
  10. *
  11. * The interface should be identical to that of devaudio.c
  12. */
  13. #include "u.h"
  14. #include "../port/lib.h"
  15. #include "mem.h"
  16. #include "dat.h"
  17. #include "fns.h"
  18. #include "../port/error.h"
  19. #include "io.h"
  20. #include "sa1110dma.h"
  21. static int debug = 0;
  22. /*
  23. * GPIO based L3 bus support.
  24. *
  25. * This provides control of Philips L3 type devices.
  26. * GPIO lines are used for clock, data and mode pins.
  27. *
  28. * Note: The L3 pins are shared with I2C devices. This should not present
  29. * any problems as long as an I2C start sequence is not generated. This is
  30. * defined as a 1->0 transition on the data lines when the clock is high.
  31. * It is critical this code only allow data transitions when the clock
  32. * is low. This is always legal in L3.
  33. *
  34. * The IIC interface requires the clock and data pin to be LOW when idle. We
  35. * must make sure we leave them in this state.
  36. *
  37. * It appears the read data is generated on the falling edge of the clock
  38. * and should be held stable during the clock high time.
  39. */
  40. /*
  41. * L3 setup and hold times (expressed in µs)
  42. */
  43. enum {
  44. L3_AcquireTime = 1,
  45. L3_ReleaseTime = 1,
  46. L3_DataSetupTime = (190+999)/1000, /* 190 ns */
  47. L3_DataHoldTime = ( 30+999)/1000,
  48. L3_ModeSetupTime = (190+999)/1000,
  49. L3_ModeHoldTime = (190+999)/1000,
  50. L3_ClockHighTime = (100+999)/1000,
  51. L3_ClockLowTime = (100+999)/1000,
  52. L3_HaltTime = (190+999)/1000,
  53. };
  54. /* UDA 1341 Registers */
  55. enum {
  56. /* Status0 register */
  57. UdaStatusDC = 0, /* 1 bit */
  58. UdaStatusIF = 1, /* 3 bits */
  59. UdaStatusSC = 4, /* 2 bits */
  60. UdaStatusRST = 6, /* 1 bit */
  61. };
  62. enum {
  63. /* Status1 register */
  64. UdaStatusPC = 0, /* 2 bits */
  65. UdaStatusDS = 2, /* 1 bit */
  66. UdaStatusPDA = 3, /* 1 bit */
  67. UdaStatusPAD = 4, /* 1 bit */
  68. UdaStatusIGS = 5, /* 1 bit */
  69. UdaStatusOGS = 6, /* 1 bit */
  70. };
  71. /*
  72. * UDA1341 L3 address and command types
  73. */
  74. enum {
  75. UDA1341_DATA0 = 0,
  76. UDA1341_DATA1,
  77. UDA1341_STATUS,
  78. UDA1341_L3Addr = 0x14,
  79. };
  80. typedef struct AQueue AQueue;
  81. typedef struct Buf Buf;
  82. typedef struct IOstate IOstate;
  83. enum
  84. {
  85. Qdir = 0,
  86. Qaudio,
  87. Qvolume,
  88. Qstatus,
  89. Qstats,
  90. Fmono = 1,
  91. Fin = 2,
  92. Fout = 4,
  93. Aclosed = 0,
  94. Aread,
  95. Awrite,
  96. Vaudio = 0,
  97. Vmic,
  98. Vtreb,
  99. Vbass,
  100. Vspeed,
  101. Vbufsize,
  102. Vfilter,
  103. Vinvert,
  104. Nvol,
  105. Bufsize = 4* 1024, /* 46 ms each */
  106. Nbuf = 10, /* 1.5 seconds total */
  107. Speed = 44100,
  108. Ncmd = 50, /* max volume command words */
  109. };
  110. enum {
  111. Flushbuf = 0xe0000000,
  112. };
  113. /* System Clock -- according to the manual, it seems that when the UDA is
  114. configured in non MSB/I2S mode, it uses a divisor of 256 to the 12.288MHz
  115. clock. The other rates are only supported in MSB mode, which should be
  116. implemented at some point */
  117. enum {
  118. SC512FS = 0 << 2,
  119. SC384FS = 1 << 2,
  120. SC256FS = 2 << 2,
  121. CLOCKMASK = 3 << 2,
  122. };
  123. /* Format */
  124. enum {
  125. LSB16 = 1 << 1,
  126. LSB18 = 2 << 1,
  127. LSB20 = 3 << 1,
  128. MSB = 4 << 1,
  129. MSB16 = 5 << 1,
  130. MSB18 = 6 << 1,
  131. MSB20 = 7 << 1,
  132. };
  133. Dirtab
  134. audiodir[] =
  135. {
  136. ".", {Qdir, 0, QTDIR}, 0, DMDIR|0555,
  137. "audio", {Qaudio}, 0, 0666,
  138. "volume", {Qvolume}, 0, 0666,
  139. "audiostatus", {Qstatus}, 0, 0444,
  140. "audiostats", {Qstats}, 0, 0444,
  141. };
  142. struct Buf
  143. {
  144. uchar* virt;
  145. ulong phys;
  146. uint nbytes;
  147. };
  148. struct IOstate
  149. {
  150. QLock;
  151. Lock ilock;
  152. Rendez vous;
  153. Chan *chan; /* chan of open */
  154. int dma; /* dma chan, alloc on open, free on close */
  155. int bufinit; /* boolean, if buffers allocated */
  156. Buf buf[Nbuf]; /* buffers and queues */
  157. volatile Buf *current; /* next dma to finish */
  158. volatile Buf *next; /* next candidate for dma */
  159. volatile Buf *filling; /* buffer being filled */
  160. /* to have defines just like linux — there's a real operating system */
  161. #define emptying filling
  162. };
  163. static struct
  164. {
  165. QLock;
  166. int amode; /* Aclosed/Aread/Awrite for /audio */
  167. int intr; /* boolean an interrupt has happened */
  168. int rivol[Nvol]; /* right/left input/output volumes */
  169. int livol[Nvol];
  170. int rovol[Nvol];
  171. int lovol[Nvol];
  172. ulong totcount; /* how many bytes processed since open */
  173. vlong tottime; /* time at which totcount bytes were processed */
  174. IOstate i;
  175. IOstate o;
  176. } audio;
  177. int zerodma; /* dma buffer used for sending zero */
  178. typedef struct Iostats Iostats;
  179. struct Iostats {
  180. ulong totaldma;
  181. ulong idledma;
  182. ulong faildma;
  183. ulong samedma;
  184. ulong empties;
  185. };
  186. static struct
  187. {
  188. ulong bytes;
  189. Iostats rx, tx;
  190. } iostats;
  191. static void setaudio(int in, int out, int left, int right, int value);
  192. static void setspeed(int in, int out, int left, int right, int value);
  193. static void setbufsize(int in, int out, int left, int right, int value);
  194. static struct
  195. {
  196. char* name;
  197. int flag;
  198. int ilval; /* initial values */
  199. int irval;
  200. void (*setval)(int, int, int, int, int);
  201. } volumes[] =
  202. {
  203. [Vaudio] {"audio", Fout|Fmono, 80, 80, setaudio },
  204. [Vmic] {"mic", Fin|Fmono, 0, 0, nil },
  205. [Vtreb] {"treb", Fout|Fmono, 50, 50, nil },
  206. [Vbass] {"bass", Fout|Fmono, 50, 50, nil },
  207. [Vspeed] {"speed", Fin|Fout|Fmono, Speed, Speed, setspeed },
  208. [Vbufsize] {"bufsize", Fin|Fout|Fmono, Bufsize, Bufsize, setbufsize },
  209. [Vfilter] {"filter", Fout|Fmono, 0, 0, nil },
  210. [Vinvert] {"invert", Fin|Fout|Fmono, 0, 0, nil },
  211. [Nvol] {0}
  212. };
  213. static void setreg(char *name, int val, int n);
  214. /*
  215. * Grab control of the IIC/L3 shared pins
  216. */
  217. static void
  218. L3_acquirepins(void)
  219. {
  220. gpioregs->set = (GPIO_L3_MODE_o | GPIO_L3_SCLK_o | GPIO_L3_SDA_io);
  221. gpioregs->direction |= (GPIO_L3_MODE_o | GPIO_L3_SCLK_o | GPIO_L3_SDA_io);
  222. microdelay(L3_AcquireTime);
  223. }
  224. /*
  225. * Release control of the IIC/L3 shared pins
  226. */
  227. static void
  228. L3_releasepins(void)
  229. {
  230. gpioregs->direction &= ~(GPIO_L3_MODE_o | GPIO_L3_SCLK_o | GPIO_L3_SDA_io);
  231. microdelay(L3_ReleaseTime);
  232. }
  233. /*
  234. * Initialize the interface
  235. */
  236. static void
  237. L3_init(void)
  238. {
  239. gpioregs->altfunc &= ~(GPIO_L3_SDA_io | GPIO_L3_SCLK_o | GPIO_L3_MODE_o);
  240. L3_releasepins();
  241. }
  242. /*
  243. * Get a bit. The clock is high on entry and on exit. Data is read after
  244. * the clock low time has expired.
  245. */
  246. static int
  247. L3_getbit(void)
  248. {
  249. int data;
  250. gpioregs->clear = GPIO_L3_SCLK_o;
  251. microdelay(L3_ClockLowTime);
  252. data = (gpioregs->level & GPIO_L3_SDA_io) ? 1 : 0;
  253. gpioregs->set = GPIO_L3_SCLK_o;
  254. microdelay(L3_ClockHighTime);
  255. return data;
  256. }
  257. /*
  258. * Send a bit. The clock is high on entry and on exit. Data is sent only
  259. * when the clock is low (I2C compatibility).
  260. */
  261. static void
  262. L3_sendbit(int bit)
  263. {
  264. gpioregs->clear = GPIO_L3_SCLK_o;
  265. if (bit & 1)
  266. gpioregs->set = GPIO_L3_SDA_io;
  267. else
  268. gpioregs->clear = GPIO_L3_SDA_io;
  269. /* Assumes L3_DataSetupTime < L3_ClockLowTime */
  270. microdelay(L3_ClockLowTime);
  271. gpioregs->set = GPIO_L3_SCLK_o;
  272. microdelay(L3_ClockHighTime);
  273. }
  274. /*
  275. * Send a byte. The mode line is set or pulsed based on the mode sequence
  276. * count. The mode line is high on entry and exit. The mod line is pulsed
  277. * before the second data byte and before ech byte thereafter.
  278. */
  279. static void
  280. L3_sendbyte(char data, int mode)
  281. {
  282. int i;
  283. switch(mode) {
  284. case 0: /* Address mode */
  285. gpioregs->clear = GPIO_L3_MODE_o;
  286. break;
  287. case 1: /* First data byte */
  288. break;
  289. default: /* Subsequent bytes */
  290. gpioregs->clear = GPIO_L3_MODE_o;
  291. microdelay(L3_HaltTime);
  292. gpioregs->set = GPIO_L3_MODE_o;
  293. break;
  294. }
  295. microdelay(L3_ModeSetupTime);
  296. for (i = 0; i < 8; i++)
  297. L3_sendbit(data >> i);
  298. if (mode == 0) /* Address mode */
  299. gpioregs->set = GPIO_L3_MODE_o;
  300. microdelay(L3_ModeHoldTime);
  301. }
  302. /*
  303. * Get a byte. The mode line is set or pulsed based on the mode sequence
  304. * count. The mode line is high on entry and exit. The mod line is pulsed
  305. * before the second data byte and before each byte thereafter. This
  306. * function is never valid with mode == 0 (address cycle) as the address
  307. * is always sent on the bus, not read.
  308. */
  309. static char
  310. L3_getbyte(int mode)
  311. {
  312. char data = 0;
  313. int i;
  314. switch(mode) {
  315. case 0: /* Address mode - never valid */
  316. break;
  317. case 1: /* First data byte */
  318. break;
  319. default: /* Subsequent bytes */
  320. gpioregs->clear = GPIO_L3_MODE_o;
  321. microdelay(L3_HaltTime);
  322. gpioregs->set = GPIO_L3_MODE_o;
  323. break;
  324. }
  325. microdelay(L3_ModeSetupTime);
  326. for (i = 0; i < 8; i++)
  327. data |= (L3_getbit() << i);
  328. microdelay(L3_ModeHoldTime);
  329. return data;
  330. }
  331. /*
  332. * Write data to a device on the L3 bus. The address is passed as well as
  333. * the data and length. The length written is returned. The register space
  334. * is encoded in the address (low two bits are set and device address is
  335. * in the upper 6 bits).
  336. */
  337. static int
  338. L3_write(uchar addr, uchar *data, int len)
  339. {
  340. int mode = 0;
  341. int bytes = len;
  342. L3_acquirepins();
  343. L3_sendbyte(addr, mode++);
  344. while(len--)
  345. L3_sendbyte(*data++, mode++);
  346. L3_releasepins();
  347. return bytes;
  348. }
  349. /*
  350. * Read data from a device on the L3 bus. The address is passed as well as
  351. * the data and length. The length read is returned. The register space
  352. * is encoded in the address (low two bits are set and device address is
  353. * in the upper 6 bits).
  354. * Commented out, not used
  355. static int
  356. L3_read(uchar addr, uchar *data, int len)
  357. {
  358. int mode = 0;
  359. int bytes = len;
  360. L3_acquirepins();
  361. L3_sendbyte(addr, mode++);
  362. gpioregs->direction &= ~(GPIO_L3_SDA_io);
  363. while(len--)
  364. *data++ = L3_getbyte(mode++);
  365. L3_releasepins();
  366. return bytes;
  367. }
  368. */
  369. void
  370. audiomute(int on)
  371. {
  372. egpiobits(EGPIO_audio_mute, on);
  373. }
  374. static char Emode[] = "illegal open mode";
  375. static char Evolume[] = "illegal volume specifier";
  376. static void
  377. bufinit(IOstate *b)
  378. {
  379. int i;
  380. if (debug) print("bufinit\n");
  381. for (i = 0; i < Nbuf; i++) {
  382. b->buf[i].virt = xalloc(Bufsize);
  383. b->buf[i].phys = PADDR(b->buf[i].virt);
  384. memset(b->buf[i].virt, 0xAA, Bufsize);
  385. }
  386. b->bufinit = 1;
  387. };
  388. static void
  389. setempty(IOstate *b)
  390. {
  391. int i;
  392. if (debug) print("setempty\n");
  393. for (i = 0; i < Nbuf; i++) {
  394. b->buf[i].nbytes = 0;
  395. }
  396. b->filling = b->buf;
  397. b->current = b->buf;
  398. b->next = b->buf;
  399. }
  400. static int
  401. audioqnotempty(void *x)
  402. {
  403. IOstate *s = x;
  404. return dmaidle(s->dma) || s->emptying != s->current;
  405. }
  406. static int
  407. audioqnotfull(void *x)
  408. {
  409. IOstate *s = x;
  410. return dmaidle(s->dma) || s->filling != s->current;
  411. }
  412. SSPregs *sspregs;
  413. MCPregs *mcpregs;
  414. static void
  415. audioinit(void)
  416. {
  417. /* Turn MCP operations off */
  418. mcpregs = mapspecial(MCPREGS, sizeof(MCPregs));
  419. mcpregs->status &= ~(1<<16);
  420. sspregs = mapspecial(SSPREGS, sizeof(SSPregs));
  421. }
  422. uchar status0[1] = {0x02};
  423. uchar status1[1] = {0x80};
  424. uchar data00[1] = {0x00}; /* volume control, bits 0 – 5 */
  425. uchar data01[1] = {0x40};
  426. uchar data02[1] = {0x80};
  427. uchar data0e0[2] = {0xc0, 0xe0};
  428. uchar data0e1[2] = {0xc1, 0xe0};
  429. uchar data0e2[2] = {0xc2, 0xf2};
  430. /* there is no data0e3 */
  431. uchar data0e4[2] = {0xc4, 0xe0};
  432. uchar data0e5[2] = {0xc5, 0xe0};
  433. uchar data0e6[2] = {0xc6, 0xe3};
  434. static void
  435. enable(void)
  436. {
  437. uchar data[1];
  438. L3_init();
  439. /* Setup the uarts */
  440. ppcregs->assignment &= ~(1<<18);
  441. sspregs->control0 = 0;
  442. sspregs->control0 = 0x031f; /* 16 bits, TI frames, serial clock rate 3 */
  443. sspregs->control1 = 0x0020; /* ext clock */
  444. sspregs->control0 = 0x039f; /* enable */
  445. /* Enable the audio power */
  446. audioicpower(1);
  447. egpiobits(EGPIO_codec_reset, 1);
  448. setspeed(0, 0, 0, 0, volumes[Vspeed].ilval);
  449. data[0] = status0[0] | 1 << UdaStatusRST;
  450. L3_write(UDA1341_L3Addr | UDA1341_STATUS, data, 1 );
  451. gpioregs->clear = EGPIO_codec_reset;
  452. gpioregs->set = EGPIO_codec_reset;
  453. /* write uda 1341 status[0] */
  454. data[0] = status0[0];
  455. L3_write(UDA1341_L3Addr | UDA1341_STATUS, data, 1);
  456. if (debug)
  457. print("enable: status0 = 0x%2.2ux\n", data[0]);
  458. L3_write(UDA1341_L3Addr | UDA1341_STATUS, status1, 1);
  459. L3_write(UDA1341_L3Addr | UDA1341_DATA0, data02, 1);
  460. L3_write(UDA1341_L3Addr | UDA1341_DATA0, data0e2, 2);
  461. L3_write(UDA1341_L3Addr | UDA1341_DATA0, data0e6, 2 );
  462. if (debug) {
  463. print("enable: status0 = 0x%2.2ux\n", data[0]);
  464. print("enable: status1 = 0x%2.2ux\n", status1[0]);
  465. print("enable: data02 = 0x%2.2ux\n", data02[0]);
  466. print("enable: data0e2 = 0x%4.4ux\n", data0e2[0] | data0e2[1]<<8);
  467. print("enable: data0e4 = 0x%4.4ux\n", data0e4[0] | data0e4[1]<<8);
  468. print("enable: data0e6 = 0x%4.4ux\n", data0e6[0] | data0e6[1]<<8);
  469. print("enable: sspregs->control0 = 0x%lux\n", sspregs->control0);
  470. print("enable: sspregs->control1 = 0x%lux\n", sspregs->control1);
  471. }
  472. }
  473. static void
  474. resetlevel(void)
  475. {
  476. int i;
  477. for(i=0; volumes[i].name; i++) {
  478. audio.lovol[i] = volumes[i].ilval;
  479. audio.rovol[i] = volumes[i].irval;
  480. audio.livol[i] = volumes[i].ilval;
  481. audio.rivol[i] = volumes[i].irval;
  482. }
  483. }
  484. static void
  485. mxvolume(void) {
  486. int *left, *right;
  487. setspeed(0, 0, 0, 0, volumes[Vspeed].ilval);
  488. if (!dmaidle(audio.i.dma) || !dmaidle(audio.o.dma))
  489. L3_write(UDA1341_L3Addr | UDA1341_STATUS, status0, 1);
  490. if(audio.amode & Aread){
  491. left = audio.livol;
  492. right = audio.rivol;
  493. if (left[Vmic]+right[Vmic] == 0) {
  494. /* Turn on automatic gain control (AGC) */
  495. data0e4[1] |= 0x10;
  496. L3_write(UDA1341_L3Addr | UDA1341_DATA0, data0e4, 2 );
  497. } else {
  498. int v;
  499. /* Turn on manual gain control */
  500. v = ((left[Vmic]+right[Vmic])*0x7f/200)&0x7f;
  501. data0e4[1] &= ~0x13;
  502. data0e5[1] &= ~0x1f;
  503. data0e4[1] |= v & 0x3;
  504. data0e5[0] |= (v & 0x7c)<<6;
  505. data0e5[1] |= (v & 0x7c)>>2;
  506. L3_write(UDA1341_L3Addr | UDA1341_DATA0, data0e4, 2 );
  507. L3_write(UDA1341_L3Addr | UDA1341_DATA0, data0e5, 2 );
  508. }
  509. if (left[Vinvert]+right[Vinvert] == 0)
  510. status1[0] &= ~0x04;
  511. else
  512. status1[0] |= 0x04;
  513. L3_write(UDA1341_L3Addr | UDA1341_STATUS, status1, 1);
  514. if (debug) {
  515. print("mxvolume: status1 = 0x%2.2ux\n", status1[0]);
  516. print("mxvolume: data0e4 = 0x%4.4ux\n", data0e4[0]|data0e4[0]<<8);
  517. print("mxvolume: data0e5 = 0x%4.4ux\n", data0e5[0]|data0e5[0]<<8);
  518. }
  519. }
  520. if(audio.amode & Awrite){
  521. left = audio.lovol;
  522. right = audio.rovol;
  523. data00[0] &= ~0x3f;
  524. data00[0] |= ((200-left[Vaudio]-right[Vaudio])*0x3f/200)&0x3f;
  525. if (left[Vtreb]+right[Vtreb] <= 100
  526. && left[Vbass]+right[Vbass] <= 100)
  527. /* settings neutral */
  528. data02[0] &= ~0x03;
  529. else {
  530. data02[0] |= 0x03;
  531. data01[0] &= ~0x3f;
  532. data01[0] |= ((left[Vtreb]+right[Vtreb]-100)*0x3/100)&0x03;
  533. data01[0] |= (((left[Vbass]+right[Vbass]-100)*0xf/100)&0xf)<<2;
  534. }
  535. if (left[Vfilter]+right[Vfilter] == 0)
  536. data02[0] &= ~0x10;
  537. else
  538. data02[0]|= 0x10;
  539. if (left[Vinvert]+right[Vinvert] == 0)
  540. status1[0] &= ~0x10;
  541. else
  542. status1[0] |= 0x10;
  543. L3_write(UDA1341_L3Addr | UDA1341_STATUS, status1, 1);
  544. L3_write(UDA1341_L3Addr | UDA1341_DATA0, data00, 1);
  545. L3_write(UDA1341_L3Addr | UDA1341_DATA0, data01, 1);
  546. L3_write(UDA1341_L3Addr | UDA1341_DATA0, data02, 1);
  547. if (debug) {
  548. print("mxvolume: status1 = 0x%2.2ux\n", status1[0]);
  549. print("mxvolume: data00 = 0x%2.2ux\n", data00[0]);
  550. print("mxvolume: data01 = 0x%2.2ux\n", data01[0]);
  551. print("mxvolume: data02 = 0x%2.2ux\n", data02[0]);
  552. }
  553. }
  554. }
  555. static void
  556. setreg(char *name, int val, int n)
  557. {
  558. uchar x[2];
  559. int i;
  560. x[0] = val;
  561. x[1] = val>>8;
  562. if(strcmp(name, "pause") == 0){
  563. for(i = 0; i < n; i++)
  564. microdelay(val);
  565. return;
  566. }
  567. switch(n){
  568. case 1:
  569. case 2:
  570. break;
  571. default:
  572. error("setreg");
  573. }
  574. if(strcmp(name, "status") == 0){
  575. L3_write(UDA1341_L3Addr | UDA1341_STATUS, x, n);
  576. } else if(strcmp(name, "data0") == 0){
  577. L3_write(UDA1341_L3Addr | UDA1341_DATA0, x, n);
  578. } else if(strcmp(name, "data1") == 0){
  579. L3_write(UDA1341_L3Addr | UDA1341_DATA1, x, n);
  580. } else
  581. error("setreg");
  582. }
  583. static void
  584. outenable(void) {
  585. /* turn on DAC, set output gain switch */
  586. audioamppower(1);
  587. audiomute(0);
  588. status1[0] |= 0x41;
  589. L3_write(UDA1341_L3Addr | UDA1341_STATUS, status1, 1);
  590. /* set volume */
  591. data00[0] |= 0xf;
  592. L3_write(UDA1341_L3Addr | UDA1341_DATA0, data00, 1);
  593. if (debug) {
  594. print("outenable: status1 = 0x%2.2ux\n", status1[0]);
  595. print("outenable: data00 = 0x%2.2ux\n", data00[0]);
  596. }
  597. }
  598. static void
  599. outdisable(void) {
  600. dmastop(audio.o.dma);
  601. /* turn off DAC, clear output gain switch */
  602. audiomute(1);
  603. status1[0] &= ~0x41;
  604. L3_write(UDA1341_L3Addr | UDA1341_STATUS, status1, 1);
  605. if (debug) {
  606. print("outdisable: status1 = 0x%2.2ux\n", status1[0]);
  607. }
  608. audioamppower(0);
  609. }
  610. static void
  611. inenable(void) {
  612. /* turn on ADC, set input gain switch */
  613. status1[0] |= 0x22;
  614. L3_write(UDA1341_L3Addr | UDA1341_STATUS, status1, 1);
  615. if (debug) {
  616. print("inenable: status1 = 0x%2.2ux\n", status1[0]);
  617. }
  618. }
  619. static void
  620. indisable(void) {
  621. dmastop(audio.i.dma);
  622. /* turn off ADC, clear input gain switch */
  623. status1[0] &= ~0x22;
  624. L3_write(UDA1341_L3Addr | UDA1341_STATUS, status1, 1);
  625. if (debug) {
  626. print("indisable: status1 = 0x%2.2ux\n", status1[0]);
  627. }
  628. }
  629. static void
  630. sendaudio(IOstate *s) {
  631. /* interrupt routine calls this too */
  632. int n;
  633. if (debug > 1) print("#A: sendaudio\n");
  634. ilock(&s->ilock);
  635. if ((audio.amode & Aread) && s->next == s->filling && dmaidle(s->dma)) {
  636. // send an empty buffer to provide an input clock
  637. zerodma |= dmastart(s->dma, Flushbuf, volumes[Vbufsize].ilval) & 0xff;
  638. if (zerodma == 0)
  639. if (debug) print("emptyfail\n");
  640. iostats.tx.empties++;
  641. iunlock(&s->ilock);
  642. return;
  643. }
  644. while (s->next != s->filling) {
  645. s->next->nbytes &= ~0x3; /* must be a multiple of 4 */
  646. if(s->next->nbytes) {
  647. if ((n = dmastart(s->dma, s->next->phys, s->next->nbytes)) == 0) {
  648. iostats.tx.faildma++;
  649. break;
  650. }
  651. iostats.tx.totaldma++;
  652. switch (n >> 8) {
  653. case 1:
  654. iostats.tx.idledma++;
  655. break;
  656. case 3:
  657. iostats.tx.faildma++;
  658. break;
  659. }
  660. if (debug) {
  661. if (debug > 1)
  662. print("dmastart @%p\n", s->next);
  663. else
  664. iprint("+");
  665. }
  666. s->next->nbytes = 0;
  667. }
  668. s->next++;
  669. if (s->next == &s->buf[Nbuf])
  670. s->next = &s->buf[0];
  671. }
  672. iunlock(&s->ilock);
  673. }
  674. static void
  675. recvaudio(IOstate *s) {
  676. /* interrupt routine calls this too */
  677. int n;
  678. if (debug > 1) print("#A: recvaudio\n");
  679. ilock(&s->ilock);
  680. while (s->next != s->emptying) {
  681. assert(s->next->nbytes == 0);
  682. if ((n = dmastart(s->dma, s->next->phys, volumes[Vbufsize].ilval)) == 0) {
  683. iostats.rx.faildma++;
  684. break;
  685. }
  686. iostats.rx.totaldma++;
  687. switch (n >> 8) {
  688. case 1:
  689. iostats.rx.idledma++;
  690. break;
  691. case 3:
  692. iostats.rx.faildma++;
  693. break;
  694. }
  695. if (debug) {
  696. if (debug > 1)
  697. print("dmastart @%p\n", s->next);
  698. else
  699. iprint("+");
  700. }
  701. s->next++;
  702. if (s->next == &s->buf[Nbuf])
  703. s->next = &s->buf[0];
  704. }
  705. iunlock(&s->ilock);
  706. }
  707. void
  708. audiopower(int flag) {
  709. IOstate *s;
  710. if (debug) {
  711. iprint("audiopower %d\n", flag);
  712. }
  713. if (flag) {
  714. /* power on only when necessary */
  715. if (audio.amode) {
  716. audioamppower(1);
  717. audioicpower(1);
  718. egpiobits(EGPIO_codec_reset, 1);
  719. enable();
  720. if (audio.amode & Aread) {
  721. inenable();
  722. s = &audio.i;
  723. dmareset(s->dma, 1, 0, 4, 2, SSPRecvDMA, Port4SSP);
  724. recvaudio(s);
  725. }
  726. if (audio.amode & Awrite) {
  727. outenable();
  728. s = &audio.o;
  729. dmareset(s->dma, 0, 0, 4, 2, SSPXmitDMA, Port4SSP);
  730. sendaudio(s);
  731. }
  732. mxvolume();
  733. }
  734. } else {
  735. /* power off */
  736. if (audio.amode & Aread)
  737. indisable();
  738. if (audio.amode & Awrite)
  739. outdisable();
  740. egpiobits(EGPIO_codec_reset, 0);
  741. audioamppower(0);
  742. audioicpower(0);
  743. }
  744. }
  745. static void
  746. audiointr(void *x, ulong ndma) {
  747. IOstate *s = x;
  748. if (debug) {
  749. if (debug > 1)
  750. iprint("#A: audio interrupt @%p\n", s->current);
  751. else
  752. iprint("-");
  753. }
  754. if (s == &audio.i || (ndma & ~zerodma)) {
  755. /* A dma, not of a zero buffer completed, update current
  756. * Only interrupt routine touches s->current
  757. */
  758. s->current->nbytes = (s == &audio.i)? volumes[Vbufsize].ilval: 0;
  759. s->current++;
  760. if (s->current == &s->buf[Nbuf])
  761. s->current = &s->buf[0];
  762. }
  763. if (ndma) {
  764. if (s == &audio.o) {
  765. zerodma &= ~ndma;
  766. sendaudio(s);
  767. } else if (s == &audio.i)
  768. recvaudio(s);
  769. }
  770. wakeup(&s->vous);
  771. }
  772. static Chan*
  773. audioattach(char *param)
  774. {
  775. return devattach('A', param);
  776. }
  777. static Walkqid*
  778. audiowalk(Chan *c, Chan *nc, char **name, int nname)
  779. {
  780. return devwalk(c, nc, name, nname, audiodir, nelem(audiodir), devgen);
  781. }
  782. static int
  783. audiostat(Chan *c, uchar *db, int n)
  784. {
  785. return devstat(c, db, n, audiodir, nelem(audiodir), devgen);
  786. }
  787. static Chan*
  788. audioopen(Chan *c, int mode)
  789. {
  790. IOstate *s;
  791. int omode = mode;
  792. switch((ulong)c->qid.path) {
  793. default:
  794. error(Eperm);
  795. break;
  796. case Qstatus:
  797. case Qstats:
  798. if((omode&7) != OREAD)
  799. error(Eperm);
  800. case Qvolume:
  801. case Qdir:
  802. break;
  803. case Qaudio:
  804. omode = (omode & 0x7) + 1;
  805. if (omode & ~(Aread | Awrite))
  806. error(Ebadarg);
  807. qlock(&audio);
  808. if(audio.amode & omode){
  809. qunlock(&audio);
  810. error(Einuse);
  811. }
  812. enable();
  813. memset(&iostats, 0, sizeof(iostats));
  814. if (omode & Aread) {
  815. inenable();
  816. s = &audio.i;
  817. if(s->bufinit == 0)
  818. bufinit(s);
  819. setempty(s);
  820. s->emptying = &s->buf[Nbuf-1];
  821. s->chan = c;
  822. s->dma = dmaalloc(1, 0, 4, 2, SSPRecvDMA, Port4SSP, audiointr, (void*)s);
  823. audio.amode |= Aread;
  824. }
  825. if (omode & (Aread|Awrite) && (audio.amode & Awrite) == 0) {
  826. s = &audio.o;
  827. if(s->bufinit == 0)
  828. bufinit(s);
  829. setempty(s);
  830. s->chan = c;
  831. s->dma = dmaalloc(0, 0, 4, 2, SSPXmitDMA, Port4SSP, audiointr, (void*)s);
  832. }
  833. if (omode & Awrite) {
  834. audio.amode |= Awrite;
  835. outenable();
  836. }
  837. mxvolume();
  838. if (audio.amode & Aread)
  839. sendaudio(&audio.o);
  840. qunlock(&audio);
  841. if (debug) print("open done\n");
  842. break;
  843. }
  844. c = devopen(c, mode, audiodir, nelem(audiodir), devgen);
  845. c->mode = openmode(mode);
  846. c->flag |= COPEN;
  847. c->offset = 0;
  848. return c;
  849. }
  850. static void
  851. audioclose(Chan *c)
  852. {
  853. IOstate *s;
  854. switch((ulong)c->qid.path) {
  855. default:
  856. error(Eperm);
  857. break;
  858. case Qdir:
  859. case Qvolume:
  860. case Qstatus:
  861. case Qstats:
  862. break;
  863. case Qaudio:
  864. if (debug > 1) print("#A: close\n");
  865. if(c->flag & COPEN) {
  866. qlock(&audio);
  867. if (audio.i.chan == c) {
  868. /* closing the read end */
  869. audio.amode &= ~Aread;
  870. s = &audio.i;
  871. qlock(s);
  872. indisable();
  873. setempty(s);
  874. dmafree(s->dma);
  875. qunlock(s);
  876. if ((audio.amode & Awrite) == 0) {
  877. s = &audio.o;
  878. qlock(s);
  879. while(waserror())
  880. ;
  881. dmawait(s->dma);
  882. poperror();
  883. outdisable();
  884. setempty(s);
  885. dmafree(s->dma);
  886. qunlock(s);
  887. }
  888. }
  889. if (audio.o.chan == c) {
  890. /* closing the write end */
  891. audio.amode &= ~Awrite;
  892. s = &audio.o;
  893. qlock(s);
  894. if (s->filling->nbytes) {
  895. /* send remaining partial buffer */
  896. s->filling++;
  897. if (s->filling == &s->buf[Nbuf])
  898. s->filling = &s->buf[0];
  899. sendaudio(s);
  900. }
  901. while(waserror())
  902. ;
  903. dmawait(s->dma);
  904. poperror();
  905. outdisable();
  906. setempty(s);
  907. if ((audio.amode & Aread) == 0)
  908. dmafree(s->dma);
  909. qunlock(s);
  910. }
  911. if (audio.amode == 0) {
  912. /* turn audio off */
  913. egpiobits(EGPIO_codec_reset, 0);
  914. audioicpower(0);
  915. }
  916. qunlock(&audio);
  917. }
  918. break;
  919. }
  920. }
  921. static long
  922. audioread(Chan *c, void *v, long n, vlong off)
  923. {
  924. int liv, riv, lov, rov;
  925. long m, n0;
  926. char buf[300];
  927. int j;
  928. ulong offset = off;
  929. char *p;
  930. IOstate *s;
  931. n0 = n;
  932. p = v;
  933. switch((ulong)c->qid.path) {
  934. default:
  935. error(Eperm);
  936. break;
  937. case Qdir:
  938. return devdirread(c, p, n, audiodir, nelem(audiodir), devgen);
  939. case Qaudio:
  940. if (debug > 1) print("#A: read %ld\n", n);
  941. if((audio.amode & Aread) == 0)
  942. error(Emode);
  943. s = &audio.i;
  944. qlock(s);
  945. if(waserror()){
  946. qunlock(s);
  947. nexterror();
  948. }
  949. while(n > 0) {
  950. if(s->emptying->nbytes == 0) {
  951. if (debug > 1) print("#A: emptied @%p\n", s->emptying);
  952. recvaudio(s);
  953. s->emptying++;
  954. if (s->emptying == &s->buf[Nbuf])
  955. s->emptying = s->buf;
  956. }
  957. /* wait if dma in progress */
  958. while (!dmaidle(s->dma) && s->emptying == s->current) {
  959. if (debug > 1) print("#A: sleep\n");
  960. sleep(&s->vous, audioqnotempty, s);
  961. }
  962. m = (s->emptying->nbytes > n)? n: s->emptying->nbytes;
  963. memmove(p, s->emptying->virt + volumes[Vbufsize].ilval -
  964. s->emptying->nbytes, m);
  965. s->emptying->nbytes -= m;
  966. n -= m;
  967. p += m;
  968. }
  969. poperror();
  970. qunlock(s);
  971. break;
  972. case Qstatus:
  973. buf[0] = 0;
  974. snprint(buf, sizeof(buf), "bytes %lud\ntime %lld\n",
  975. audio.totcount, audio.tottime);
  976. return readstr(offset, p, n, buf);
  977. case Qstats:
  978. buf[0] = 0;
  979. snprint(buf, sizeof(buf),
  980. "bytes %lud\nRX dmas %lud, while idle %lud, while busy %lud, "
  981. "out-of-order %lud, empty dmas %lud\n"
  982. "TX dmas %lud, while idle %lud, while busy %lud, "
  983. "out-of-order %lud, empty dmas %lud\n",
  984. iostats.bytes, iostats.rx.totaldma, iostats.rx.idledma,
  985. iostats.rx.faildma, iostats.rx.samedma, iostats.rx.empties,
  986. iostats.tx.totaldma, iostats.tx.idledma,
  987. iostats.tx.faildma, iostats.tx.samedma, iostats.tx.empties);
  988. return readstr(offset, p, n, buf);
  989. case Qvolume:
  990. j = 0;
  991. buf[0] = 0;
  992. for(m=0; volumes[m].name; m++){
  993. if (m == Vaudio) {
  994. liv = audio.livol[m];
  995. riv = audio.rivol[m];
  996. lov = audio.lovol[m];
  997. rov = audio.rovol[m];
  998. }
  999. else {
  1000. lov = liv = volumes[m].ilval;
  1001. rov = riv = volumes[m].irval;
  1002. }
  1003. j += snprint(buf+j, sizeof(buf)-j, "%s", volumes[m].name);
  1004. if((volumes[m].flag & Fmono) || liv==riv && lov==rov){
  1005. if((volumes[m].flag&(Fin|Fout))==(Fin|Fout) && liv==lov)
  1006. j += snprint(buf+j, sizeof(buf)-j, " %d", liv);
  1007. else{
  1008. if(volumes[m].flag & Fin)
  1009. j += snprint(buf+j, sizeof(buf)-j,
  1010. " in %d", liv);
  1011. if(volumes[m].flag & Fout)
  1012. j += snprint(buf+j, sizeof(buf)-j,
  1013. " out %d", lov);
  1014. }
  1015. }else{
  1016. if((volumes[m].flag&(Fin|Fout))==(Fin|Fout) &&
  1017. liv==lov && riv==rov)
  1018. j += snprint(buf+j, sizeof(buf)-j,
  1019. " left %d right %d",
  1020. liv, riv);
  1021. else{
  1022. if(volumes[m].flag & Fin)
  1023. j += snprint(buf+j, sizeof(buf)-j,
  1024. " in left %d right %d",
  1025. liv, riv);
  1026. if(volumes[m].flag & Fout)
  1027. j += snprint(buf+j, sizeof(buf)-j,
  1028. " out left %d right %d",
  1029. lov, rov);
  1030. }
  1031. }
  1032. j += snprint(buf+j, sizeof(buf)-j, "\n");
  1033. }
  1034. return readstr(offset, p, n, buf);
  1035. }
  1036. return n0-n;
  1037. }
  1038. static void
  1039. setaudio(int in, int out, int left, int right, int value)
  1040. {
  1041. if (value < 0 || value > 100)
  1042. error(Evolume);
  1043. if(left && out)
  1044. audio.lovol[Vaudio] = value;
  1045. if(left && in)
  1046. audio.livol[Vaudio] = value;
  1047. if(right && out)
  1048. audio.rovol[Vaudio] = value;
  1049. if(right && in)
  1050. audio.rivol[Vaudio] = value;
  1051. }
  1052. static void
  1053. setspeed(int, int, int, int, int speed)
  1054. {
  1055. uchar clock;
  1056. /* external clock configured for 44100 samples/sec */
  1057. switch (speed) {
  1058. case 32000:
  1059. /* 00 */
  1060. gpioregs->clear = GPIO_CLK_SET0_o|GPIO_CLK_SET1_o;
  1061. clock = SC384FS; /* Only works in MSB mode! */
  1062. break;
  1063. case 48000:
  1064. /* 00 */
  1065. gpioregs->clear = GPIO_CLK_SET0_o|GPIO_CLK_SET1_o;
  1066. clock = SC256FS;
  1067. break;
  1068. default:
  1069. speed = 44100;
  1070. case 44100:
  1071. /* 01 */
  1072. gpioregs->set = GPIO_CLK_SET0_o;
  1073. gpioregs->clear = GPIO_CLK_SET1_o;
  1074. clock = SC256FS;
  1075. break;
  1076. case 8000:
  1077. /* 10 */
  1078. gpioregs->set = GPIO_CLK_SET1_o;
  1079. gpioregs->clear = GPIO_CLK_SET0_o;
  1080. clock = SC512FS; /* Only works in MSB mode! */
  1081. break;
  1082. case 16000:
  1083. /* 10 */
  1084. gpioregs->set = GPIO_CLK_SET1_o;
  1085. gpioregs->clear = GPIO_CLK_SET0_o;
  1086. clock = SC256FS;
  1087. break;
  1088. case 11025:
  1089. /* 11 */
  1090. gpioregs->set = GPIO_CLK_SET0_o|GPIO_CLK_SET1_o;
  1091. clock = SC512FS; /* Only works in MSB mode! */
  1092. break;
  1093. case 22050:
  1094. /* 11 */
  1095. gpioregs->set = GPIO_CLK_SET0_o|GPIO_CLK_SET1_o;
  1096. clock = SC256FS;
  1097. break;
  1098. }
  1099. /* Wait for the UDA1341 to wake up */
  1100. delay(100);
  1101. /* Reset the chip */
  1102. status0[0] &= ~CLOCKMASK;
  1103. status0[0] |=clock;
  1104. volumes[Vspeed].ilval = speed;
  1105. }
  1106. static void
  1107. setbufsize(int, int, int, int, int value)
  1108. {
  1109. if ((value % 8) != 0 || value < 8 || value >= Bufsize)
  1110. error(Ebadarg);
  1111. volumes[Vbufsize].ilval = value;
  1112. }
  1113. static long
  1114. audiowrite(Chan *c, void *vp, long n, vlong)
  1115. {
  1116. long m, n0;
  1117. int i, v, left, right, in, out;
  1118. char *p;
  1119. IOstate *a;
  1120. Cmdbuf *cb;
  1121. p = vp;
  1122. n0 = n;
  1123. switch((ulong)c->qid.path) {
  1124. default:
  1125. error(Eperm);
  1126. break;
  1127. case Qvolume:
  1128. v = Vaudio;
  1129. left = 1;
  1130. right = 1;
  1131. in = 1;
  1132. out = 1;
  1133. cb = parsecmd(p, n);
  1134. if(waserror()){
  1135. free(cb);
  1136. nexterror();
  1137. }
  1138. for(i = 0; i < cb->nf; i++){
  1139. /*
  1140. * a number is volume
  1141. */
  1142. if(cb->f[i][0] >= '0' && cb->f[i][0] <= '9') {
  1143. m = strtoul(cb->f[i], 0, 10);
  1144. if (volumes[v].setval)
  1145. volumes[v].setval(in, out, left, right, m);
  1146. goto cont0;
  1147. }
  1148. for(m=0; volumes[m].name; m++) {
  1149. if(strcmp(cb->f[i], volumes[m].name) == 0) {
  1150. v = m;
  1151. in = 1;
  1152. out = 1;
  1153. left = 1;
  1154. right = 1;
  1155. goto cont0;
  1156. }
  1157. }
  1158. if(strcmp(cb->f[i], "reset") == 0) {
  1159. resetlevel();
  1160. goto cont0;
  1161. }
  1162. if(strcmp(cb->f[i], "debug") == 0) {
  1163. debug = debug?0:1;
  1164. goto cont0;
  1165. }
  1166. if(strcmp(cb->f[i], "in") == 0) {
  1167. in = 1;
  1168. out = 0;
  1169. goto cont0;
  1170. }
  1171. if(strcmp(cb->f[i], "out") == 0) {
  1172. in = 0;
  1173. out = 1;
  1174. goto cont0;
  1175. }
  1176. if(strcmp(cb->f[i], "left") == 0) {
  1177. left = 1;
  1178. right = 0;
  1179. goto cont0;
  1180. }
  1181. if(strcmp(cb->f[i], "right") == 0) {
  1182. left = 0;
  1183. right = 1;
  1184. goto cont0;
  1185. }
  1186. if(strcmp(cb->f[i], "reg") == 0) {
  1187. if(cb->nf < 3)
  1188. error(Evolume);
  1189. setreg(cb->f[1], atoi(cb->f[2]), cb->nf == 4 ? atoi(cb->f[3]):1);
  1190. return n0;
  1191. }
  1192. error(Evolume);
  1193. break;
  1194. cont0:;
  1195. }
  1196. mxvolume();
  1197. poperror();
  1198. free(cb);
  1199. break;
  1200. case Qaudio:
  1201. if (debug > 1) print("#A: write %ld\n", n);
  1202. if((audio.amode & Awrite) == 0)
  1203. error(Emode);
  1204. a = &audio.o;
  1205. qlock(a);
  1206. if(waserror()){
  1207. qunlock(a);
  1208. nexterror();
  1209. }
  1210. while(n > 0) {
  1211. /* wait if dma in progress */
  1212. while (!dmaidle(a->dma) && !zerodma && a->filling == a->current) {
  1213. if (debug > 1) print("#A: sleep\n");
  1214. sleep(&a->vous, audioqnotfull, a);
  1215. }
  1216. m = volumes[Vbufsize].ilval - a->filling->nbytes;
  1217. if(m > n)
  1218. m = n;
  1219. memmove(a->filling->virt + a->filling->nbytes, p, m);
  1220. a->filling->nbytes += m;
  1221. n -= m;
  1222. p += m;
  1223. if(a->filling->nbytes >= volumes[Vbufsize].ilval) {
  1224. if (debug > 1) print("#A: filled @%p\n", a->filling);
  1225. a->filling++;
  1226. if (a->filling == &a->buf[Nbuf])
  1227. a->filling = a->buf;
  1228. sendaudio(a);
  1229. }
  1230. }
  1231. poperror();
  1232. qunlock(a);
  1233. break;
  1234. }
  1235. return n0 - n;
  1236. }
  1237. Dev uda1341devtab = {
  1238. 'A',
  1239. "audio",
  1240. devreset,
  1241. audioinit,
  1242. devshutdown,
  1243. audioattach,
  1244. audiowalk,
  1245. audiostat,
  1246. audioopen,
  1247. devcreate,
  1248. audioclose,
  1249. audioread,
  1250. devbread,
  1251. audiowrite,
  1252. devbwrite,
  1253. devremove,
  1254. devwstat,
  1255. audiopower,
  1256. };