hal2.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. /*
  2. * Driver for A2 audio system used in SGI machines
  3. * Copyright (c) 2001, 2002, 2003 Ladislav Michl <ladis@linux-mips.org>
  4. *
  5. * Based on Ulf Carlsson's code.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * Supported devices:
  21. * /dev/dsp standard dsp device, (mostly) OSS compatible
  22. * /dev/mixer standard mixer device, (mostly) OSS compatible
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/sched.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/poll.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/sound.h>
  34. #include <linux/soundcard.h>
  35. #include <asm/io.h>
  36. #include <asm/sgi/hpc3.h>
  37. #include <asm/sgi/ip22.h>
  38. #include "hal2.h"
  39. #if 0
  40. #define DEBUG(args...) printk(args)
  41. #else
  42. #define DEBUG(args...)
  43. #endif
  44. #if 0
  45. #define DEBUG_MIX(args...) printk(args)
  46. #else
  47. #define DEBUG_MIX(args...)
  48. #endif
  49. /*
  50. * Before touching these look how it works. It is a bit unusual I know,
  51. * but it helps to keep things simple. This driver is considered complete
  52. * and I won't add any new features although hardware has many cool
  53. * capabilities.
  54. * (Historical note: HAL2 driver was first written by Ulf Carlsson - ALSA
  55. * 0.3 running with 2.2.x kernel. Then ALSA changed completely and it
  56. * seemed easier to me to write OSS driver from scratch - this one. Now
  57. * when ALSA is official part of 2.6 kernel it's time to write ALSA driver
  58. * using (hopefully) final version of ALSA interface)
  59. */
  60. #define H2_BLOCK_SIZE 1024
  61. #define H2_ADC_BUFSIZE 8192
  62. #define H2_DAC_BUFSIZE 16834
  63. struct hal2_pbus {
  64. struct hpc3_pbus_dmacregs *pbus;
  65. int pbusnr;
  66. unsigned int ctrl; /* Current state of pbus->pbdma_ctrl */
  67. };
  68. struct hal2_desc {
  69. struct hpc_dma_desc desc;
  70. u32 cnt; /* don't touch, it is also padding */
  71. };
  72. struct hal2_codec {
  73. unsigned char *buffer;
  74. struct hal2_desc *desc;
  75. int desc_count;
  76. int tail, head; /* tail index, head index */
  77. struct hal2_pbus pbus;
  78. unsigned int format; /* Audio data format */
  79. int voices; /* mono/stereo */
  80. unsigned int sample_rate;
  81. unsigned int master; /* Master frequency */
  82. unsigned short mod; /* MOD value */
  83. unsigned short inc; /* INC value */
  84. wait_queue_head_t dma_wait;
  85. spinlock_t lock;
  86. struct semaphore sem;
  87. int usecount; /* recording and playback are
  88. * independent */
  89. };
  90. #define H2_MIX_OUTPUT_ATT 0
  91. #define H2_MIX_INPUT_GAIN 1
  92. #define H2_MIXERS 2
  93. struct hal2_mixer {
  94. int modcnt;
  95. unsigned int master;
  96. unsigned int volume[H2_MIXERS];
  97. };
  98. struct hal2_card {
  99. int dev_dsp; /* audio device */
  100. int dev_mixer; /* mixer device */
  101. int dev_midi; /* midi device */
  102. struct hal2_ctl_regs *ctl_regs; /* HAL2 ctl registers */
  103. struct hal2_aes_regs *aes_regs; /* HAL2 aes registers */
  104. struct hal2_vol_regs *vol_regs; /* HAL2 vol registers */
  105. struct hal2_syn_regs *syn_regs; /* HAL2 syn registers */
  106. struct hal2_codec dac;
  107. struct hal2_codec adc;
  108. struct hal2_mixer mixer;
  109. };
  110. #define H2_INDIRECT_WAIT(regs) while (regs->isr & H2_ISR_TSTATUS);
  111. #define H2_READ_ADDR(addr) (addr | (1<<7))
  112. #define H2_WRITE_ADDR(addr) (addr)
  113. static char *hal2str = "HAL2";
  114. /*
  115. * I doubt anyone has a machine with two HAL2 cards. It's possible to
  116. * have two HPC's, so it is probably possible to have two HAL2 cards.
  117. * Try to deal with it, but note that it is not tested.
  118. */
  119. #define MAXCARDS 2
  120. static struct hal2_card* hal2_card[MAXCARDS];
  121. static const struct {
  122. unsigned char idx:4, avail:1;
  123. } mixtable[SOUND_MIXER_NRDEVICES] = {
  124. [SOUND_MIXER_PCM] = { H2_MIX_OUTPUT_ATT, 1 }, /* voice */
  125. [SOUND_MIXER_MIC] = { H2_MIX_INPUT_GAIN, 1 }, /* mic */
  126. };
  127. #define H2_SUPPORTED_FORMATS (AFMT_S16_LE | AFMT_S16_BE)
  128. static inline void hal2_isr_write(struct hal2_card *hal2, u16 val)
  129. {
  130. hal2->ctl_regs->isr = val;
  131. }
  132. static inline u16 hal2_isr_look(struct hal2_card *hal2)
  133. {
  134. return hal2->ctl_regs->isr;
  135. }
  136. static inline u16 hal2_rev_look(struct hal2_card *hal2)
  137. {
  138. return hal2->ctl_regs->rev;
  139. }
  140. #ifdef HAL2_DUMP_REGS
  141. static u16 hal2_i_look16(struct hal2_card *hal2, u16 addr)
  142. {
  143. struct hal2_ctl_regs *regs = hal2->ctl_regs;
  144. regs->iar = H2_READ_ADDR(addr);
  145. H2_INDIRECT_WAIT(regs);
  146. return regs->idr0;
  147. }
  148. #endif
  149. static u32 hal2_i_look32(struct hal2_card *hal2, u16 addr)
  150. {
  151. u32 ret;
  152. struct hal2_ctl_regs *regs = hal2->ctl_regs;
  153. regs->iar = H2_READ_ADDR(addr);
  154. H2_INDIRECT_WAIT(regs);
  155. ret = regs->idr0 & 0xffff;
  156. regs->iar = H2_READ_ADDR(addr | 0x1);
  157. H2_INDIRECT_WAIT(regs);
  158. ret |= (regs->idr0 & 0xffff) << 16;
  159. return ret;
  160. }
  161. static void hal2_i_write16(struct hal2_card *hal2, u16 addr, u16 val)
  162. {
  163. struct hal2_ctl_regs *regs = hal2->ctl_regs;
  164. regs->idr0 = val;
  165. regs->idr1 = 0;
  166. regs->idr2 = 0;
  167. regs->idr3 = 0;
  168. regs->iar = H2_WRITE_ADDR(addr);
  169. H2_INDIRECT_WAIT(regs);
  170. }
  171. static void hal2_i_write32(struct hal2_card *hal2, u16 addr, u32 val)
  172. {
  173. struct hal2_ctl_regs *regs = hal2->ctl_regs;
  174. regs->idr0 = val & 0xffff;
  175. regs->idr1 = val >> 16;
  176. regs->idr2 = 0;
  177. regs->idr3 = 0;
  178. regs->iar = H2_WRITE_ADDR(addr);
  179. H2_INDIRECT_WAIT(regs);
  180. }
  181. static void hal2_i_setbit16(struct hal2_card *hal2, u16 addr, u16 bit)
  182. {
  183. struct hal2_ctl_regs *regs = hal2->ctl_regs;
  184. regs->iar = H2_READ_ADDR(addr);
  185. H2_INDIRECT_WAIT(regs);
  186. regs->idr0 = (regs->idr0 & 0xffff) | bit;
  187. regs->idr1 = 0;
  188. regs->idr2 = 0;
  189. regs->idr3 = 0;
  190. regs->iar = H2_WRITE_ADDR(addr);
  191. H2_INDIRECT_WAIT(regs);
  192. }
  193. static void hal2_i_setbit32(struct hal2_card *hal2, u16 addr, u32 bit)
  194. {
  195. u32 tmp;
  196. struct hal2_ctl_regs *regs = hal2->ctl_regs;
  197. regs->iar = H2_READ_ADDR(addr);
  198. H2_INDIRECT_WAIT(regs);
  199. tmp = (regs->idr0 & 0xffff) | (regs->idr1 << 16) | bit;
  200. regs->idr0 = tmp & 0xffff;
  201. regs->idr1 = tmp >> 16;
  202. regs->idr2 = 0;
  203. regs->idr3 = 0;
  204. regs->iar = H2_WRITE_ADDR(addr);
  205. H2_INDIRECT_WAIT(regs);
  206. }
  207. static void hal2_i_clearbit16(struct hal2_card *hal2, u16 addr, u16 bit)
  208. {
  209. struct hal2_ctl_regs *regs = hal2->ctl_regs;
  210. regs->iar = H2_READ_ADDR(addr);
  211. H2_INDIRECT_WAIT(regs);
  212. regs->idr0 = (regs->idr0 & 0xffff) & ~bit;
  213. regs->idr1 = 0;
  214. regs->idr2 = 0;
  215. regs->idr3 = 0;
  216. regs->iar = H2_WRITE_ADDR(addr);
  217. H2_INDIRECT_WAIT(regs);
  218. }
  219. #if 0
  220. static void hal2_i_clearbit32(struct hal2_card *hal2, u16 addr, u32 bit)
  221. {
  222. u32 tmp;
  223. hal2_ctl_regs_t *regs = hal2->ctl_regs;
  224. regs->iar = H2_READ_ADDR(addr);
  225. H2_INDIRECT_WAIT(regs);
  226. tmp = ((regs->idr0 & 0xffff) | (regs->idr1 << 16)) & ~bit;
  227. regs->idr0 = tmp & 0xffff;
  228. regs->idr1 = tmp >> 16;
  229. regs->idr2 = 0;
  230. regs->idr3 = 0;
  231. regs->iar = H2_WRITE_ADDR(addr);
  232. H2_INDIRECT_WAIT(regs);
  233. }
  234. #endif
  235. #ifdef HAL2_DUMP_REGS
  236. static void hal2_dump_regs(struct hal2_card *hal2)
  237. {
  238. DEBUG("isr: %08hx ", hal2_isr_look(hal2));
  239. DEBUG("rev: %08hx\n", hal2_rev_look(hal2));
  240. DEBUG("relay: %04hx\n", hal2_i_look16(hal2, H2I_RELAY_C));
  241. DEBUG("port en: %04hx ", hal2_i_look16(hal2, H2I_DMA_PORT_EN));
  242. DEBUG("dma end: %04hx ", hal2_i_look16(hal2, H2I_DMA_END));
  243. DEBUG("dma drv: %04hx\n", hal2_i_look16(hal2, H2I_DMA_DRV));
  244. DEBUG("syn ctl: %04hx ", hal2_i_look16(hal2, H2I_SYNTH_C));
  245. DEBUG("aesrx ctl: %04hx ", hal2_i_look16(hal2, H2I_AESRX_C));
  246. DEBUG("aestx ctl: %04hx ", hal2_i_look16(hal2, H2I_AESTX_C));
  247. DEBUG("dac ctl1: %04hx ", hal2_i_look16(hal2, H2I_ADC_C1));
  248. DEBUG("dac ctl2: %08x ", hal2_i_look32(hal2, H2I_ADC_C2));
  249. DEBUG("adc ctl1: %04hx ", hal2_i_look16(hal2, H2I_DAC_C1));
  250. DEBUG("adc ctl2: %08x ", hal2_i_look32(hal2, H2I_DAC_C2));
  251. DEBUG("syn map: %04hx\n", hal2_i_look16(hal2, H2I_SYNTH_MAP_C));
  252. DEBUG("bres1 ctl1: %04hx ", hal2_i_look16(hal2, H2I_BRES1_C1));
  253. DEBUG("bres1 ctl2: %04x ", hal2_i_look32(hal2, H2I_BRES1_C2));
  254. DEBUG("bres2 ctl1: %04hx ", hal2_i_look16(hal2, H2I_BRES2_C1));
  255. DEBUG("bres2 ctl2: %04x ", hal2_i_look32(hal2, H2I_BRES2_C2));
  256. DEBUG("bres3 ctl1: %04hx ", hal2_i_look16(hal2, H2I_BRES3_C1));
  257. DEBUG("bres3 ctl2: %04x\n", hal2_i_look32(hal2, H2I_BRES3_C2));
  258. }
  259. #endif
  260. static struct hal2_card* hal2_dsp_find_card(int minor)
  261. {
  262. int i;
  263. for (i = 0; i < MAXCARDS; i++)
  264. if (hal2_card[i] != NULL && hal2_card[i]->dev_dsp == minor)
  265. return hal2_card[i];
  266. return NULL;
  267. }
  268. static struct hal2_card* hal2_mixer_find_card(int minor)
  269. {
  270. int i;
  271. for (i = 0; i < MAXCARDS; i++)
  272. if (hal2_card[i] != NULL && hal2_card[i]->dev_mixer == minor)
  273. return hal2_card[i];
  274. return NULL;
  275. }
  276. static void hal2_inc_head(struct hal2_codec *codec)
  277. {
  278. codec->head++;
  279. if (codec->head == codec->desc_count)
  280. codec->head = 0;
  281. }
  282. static void hal2_inc_tail(struct hal2_codec *codec)
  283. {
  284. codec->tail++;
  285. if (codec->tail == codec->desc_count)
  286. codec->tail = 0;
  287. }
  288. static void hal2_dac_interrupt(struct hal2_codec *dac)
  289. {
  290. int running;
  291. spin_lock(&dac->lock);
  292. /* if tail buffer contains zero samples DMA stream was already
  293. * stopped */
  294. running = dac->desc[dac->tail].cnt;
  295. dac->desc[dac->tail].cnt = 0;
  296. dac->desc[dac->tail].desc.cntinfo = HPCDMA_XIE | HPCDMA_EOX;
  297. /* we just proccessed empty buffer, don't update tail pointer */
  298. if (running)
  299. hal2_inc_tail(dac);
  300. spin_unlock(&dac->lock);
  301. wake_up(&dac->dma_wait);
  302. }
  303. static void hal2_adc_interrupt(struct hal2_codec *adc)
  304. {
  305. int running;
  306. spin_lock(&adc->lock);
  307. /* if head buffer contains nonzero samples DMA stream was already
  308. * stopped */
  309. running = !adc->desc[adc->head].cnt;
  310. adc->desc[adc->head].cnt = H2_BLOCK_SIZE;
  311. adc->desc[adc->head].desc.cntinfo = HPCDMA_XIE | HPCDMA_EOR;
  312. /* we just proccessed empty buffer, don't update head pointer */
  313. if (running)
  314. hal2_inc_head(adc);
  315. spin_unlock(&adc->lock);
  316. wake_up(&adc->dma_wait);
  317. }
  318. static irqreturn_t hal2_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  319. {
  320. struct hal2_card *hal2 = (struct hal2_card*)dev_id;
  321. irqreturn_t ret = IRQ_NONE;
  322. /* decide what caused this interrupt */
  323. if (hal2->dac.pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_INT) {
  324. hal2_dac_interrupt(&hal2->dac);
  325. ret = IRQ_HANDLED;
  326. }
  327. if (hal2->adc.pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_INT) {
  328. hal2_adc_interrupt(&hal2->adc);
  329. ret = IRQ_HANDLED;
  330. }
  331. return ret;
  332. }
  333. static int hal2_compute_rate(struct hal2_codec *codec, unsigned int rate)
  334. {
  335. unsigned short mod;
  336. DEBUG("rate: %d\n", rate);
  337. if (rate < 4000) rate = 4000;
  338. else if (rate > 48000) rate = 48000;
  339. if (44100 % rate < 48000 % rate) {
  340. mod = 4 * 44100 / rate;
  341. codec->master = 44100;
  342. } else {
  343. mod = 4 * 48000 / rate;
  344. codec->master = 48000;
  345. }
  346. codec->inc = 4;
  347. codec->mod = mod;
  348. rate = 4 * codec->master / mod;
  349. DEBUG("real_rate: %d\n", rate);
  350. return rate;
  351. }
  352. static void hal2_set_dac_rate(struct hal2_card *hal2)
  353. {
  354. unsigned int master = hal2->dac.master;
  355. int inc = hal2->dac.inc;
  356. int mod = hal2->dac.mod;
  357. DEBUG("master: %d inc: %d mod: %d\n", master, inc, mod);
  358. hal2_i_write16(hal2, H2I_BRES1_C1, (master == 44100) ? 1 : 0);
  359. hal2_i_write32(hal2, H2I_BRES1_C2, ((0xffff & (inc - mod - 1)) << 16) | inc);
  360. }
  361. static void hal2_set_adc_rate(struct hal2_card *hal2)
  362. {
  363. unsigned int master = hal2->adc.master;
  364. int inc = hal2->adc.inc;
  365. int mod = hal2->adc.mod;
  366. DEBUG("master: %d inc: %d mod: %d\n", master, inc, mod);
  367. hal2_i_write16(hal2, H2I_BRES2_C1, (master == 44100) ? 1 : 0);
  368. hal2_i_write32(hal2, H2I_BRES2_C2, ((0xffff & (inc - mod - 1)) << 16) | inc);
  369. }
  370. static void hal2_setup_dac(struct hal2_card *hal2)
  371. {
  372. unsigned int fifobeg, fifoend, highwater, sample_size;
  373. struct hal2_pbus *pbus = &hal2->dac.pbus;
  374. DEBUG("hal2_setup_dac\n");
  375. /* Now we set up some PBUS information. The PBUS needs information about
  376. * what portion of the fifo it will use. If it's receiving or
  377. * transmitting, and finally whether the stream is little endian or big
  378. * endian. The information is written later, on the start call.
  379. */
  380. sample_size = 2 * hal2->dac.voices;
  381. /* Fifo should be set to hold exactly four samples. Highwater mark
  382. * should be set to two samples. */
  383. highwater = (sample_size * 2) >> 1; /* halfwords */
  384. fifobeg = 0; /* playback is first */
  385. fifoend = (sample_size * 4) >> 3; /* doublewords */
  386. pbus->ctrl = HPC3_PDMACTRL_RT | HPC3_PDMACTRL_LD |
  387. (highwater << 8) | (fifobeg << 16) | (fifoend << 24) |
  388. (hal2->dac.format & AFMT_S16_LE ? HPC3_PDMACTRL_SEL : 0);
  389. /* We disable everything before we do anything at all */
  390. pbus->pbus->pbdma_ctrl = HPC3_PDMACTRL_LD;
  391. hal2_i_clearbit16(hal2, H2I_DMA_PORT_EN, H2I_DMA_PORT_EN_CODECTX);
  392. /* Setup the HAL2 for playback */
  393. hal2_set_dac_rate(hal2);
  394. /* Set endianess */
  395. if (hal2->dac.format & AFMT_S16_LE)
  396. hal2_i_setbit16(hal2, H2I_DMA_END, H2I_DMA_END_CODECTX);
  397. else
  398. hal2_i_clearbit16(hal2, H2I_DMA_END, H2I_DMA_END_CODECTX);
  399. /* Set DMA bus */
  400. hal2_i_setbit16(hal2, H2I_DMA_DRV, (1 << pbus->pbusnr));
  401. /* We are using 1st Bresenham clock generator for playback */
  402. hal2_i_write16(hal2, H2I_DAC_C1, (pbus->pbusnr << H2I_C1_DMA_SHIFT)
  403. | (1 << H2I_C1_CLKID_SHIFT)
  404. | (hal2->dac.voices << H2I_C1_DATAT_SHIFT));
  405. }
  406. static void hal2_setup_adc(struct hal2_card *hal2)
  407. {
  408. unsigned int fifobeg, fifoend, highwater, sample_size;
  409. struct hal2_pbus *pbus = &hal2->adc.pbus;
  410. DEBUG("hal2_setup_adc\n");
  411. sample_size = 2 * hal2->adc.voices;
  412. highwater = (sample_size * 2) >> 1; /* halfwords */
  413. fifobeg = (4 * 4) >> 3; /* record is second */
  414. fifoend = (4 * 4 + sample_size * 4) >> 3; /* doublewords */
  415. pbus->ctrl = HPC3_PDMACTRL_RT | HPC3_PDMACTRL_RCV | HPC3_PDMACTRL_LD |
  416. (highwater << 8) | (fifobeg << 16) | (fifoend << 24) |
  417. (hal2->adc.format & AFMT_S16_LE ? HPC3_PDMACTRL_SEL : 0);
  418. pbus->pbus->pbdma_ctrl = HPC3_PDMACTRL_LD;
  419. hal2_i_clearbit16(hal2, H2I_DMA_PORT_EN, H2I_DMA_PORT_EN_CODECR);
  420. /* Setup the HAL2 for record */
  421. hal2_set_adc_rate(hal2);
  422. /* Set endianess */
  423. if (hal2->adc.format & AFMT_S16_LE)
  424. hal2_i_setbit16(hal2, H2I_DMA_END, H2I_DMA_END_CODECR);
  425. else
  426. hal2_i_clearbit16(hal2, H2I_DMA_END, H2I_DMA_END_CODECR);
  427. /* Set DMA bus */
  428. hal2_i_setbit16(hal2, H2I_DMA_DRV, (1 << pbus->pbusnr));
  429. /* We are using 2nd Bresenham clock generator for record */
  430. hal2_i_write16(hal2, H2I_ADC_C1, (pbus->pbusnr << H2I_C1_DMA_SHIFT)
  431. | (2 << H2I_C1_CLKID_SHIFT)
  432. | (hal2->adc.voices << H2I_C1_DATAT_SHIFT));
  433. }
  434. static dma_addr_t hal2_desc_addr(struct hal2_codec *codec, int i)
  435. {
  436. if (--i < 0)
  437. i = codec->desc_count - 1;
  438. return codec->desc[i].desc.pnext;
  439. }
  440. static void hal2_start_dac(struct hal2_card *hal2)
  441. {
  442. struct hal2_codec *dac = &hal2->dac;
  443. struct hal2_pbus *pbus = &dac->pbus;
  444. pbus->pbus->pbdma_dptr = hal2_desc_addr(dac, dac->tail);
  445. pbus->pbus->pbdma_ctrl = pbus->ctrl | HPC3_PDMACTRL_ACT;
  446. /* enable DAC */
  447. hal2_i_setbit16(hal2, H2I_DMA_PORT_EN, H2I_DMA_PORT_EN_CODECTX);
  448. }
  449. static void hal2_start_adc(struct hal2_card *hal2)
  450. {
  451. struct hal2_codec *adc = &hal2->adc;
  452. struct hal2_pbus *pbus = &adc->pbus;
  453. pbus->pbus->pbdma_dptr = hal2_desc_addr(adc, adc->head);
  454. pbus->pbus->pbdma_ctrl = pbus->ctrl | HPC3_PDMACTRL_ACT;
  455. /* enable ADC */
  456. hal2_i_setbit16(hal2, H2I_DMA_PORT_EN, H2I_DMA_PORT_EN_CODECR);
  457. }
  458. static inline void hal2_stop_dac(struct hal2_card *hal2)
  459. {
  460. hal2->dac.pbus.pbus->pbdma_ctrl = HPC3_PDMACTRL_LD;
  461. /* The HAL2 itself may remain enabled safely */
  462. }
  463. static inline void hal2_stop_adc(struct hal2_card *hal2)
  464. {
  465. hal2->adc.pbus.pbus->pbdma_ctrl = HPC3_PDMACTRL_LD;
  466. }
  467. static int hal2_alloc_dmabuf(struct hal2_codec *codec, int size,
  468. int count, int cntinfo, int dir)
  469. {
  470. struct hal2_desc *desc, *dma_addr;
  471. int i;
  472. DEBUG("allocating %dk DMA buffer.\n", size / 1024);
  473. codec->buffer = (unsigned char *)__get_free_pages(GFP_KERNEL | GFP_DMA,
  474. get_order(size));
  475. if (!codec->buffer)
  476. return -ENOMEM;
  477. desc = dma_alloc_coherent(NULL, count * sizeof(struct hal2_desc),
  478. (dma_addr_t *)&dma_addr, GFP_KERNEL);
  479. if (!desc) {
  480. free_pages((unsigned long)codec->buffer, get_order(size));
  481. return -ENOMEM;
  482. }
  483. codec->desc = desc;
  484. for (i = 0; i < count; i++) {
  485. desc->desc.pbuf = dma_map_single(NULL,
  486. (void *)(codec->buffer + i * H2_BLOCK_SIZE),
  487. H2_BLOCK_SIZE, dir);
  488. desc->desc.cntinfo = cntinfo;
  489. desc->desc.pnext = (i == count - 1) ?
  490. (u32)dma_addr : (u32)(dma_addr + i + 1);
  491. desc->cnt = 0;
  492. desc++;
  493. }
  494. codec->desc_count = count;
  495. codec->head = codec->tail = 0;
  496. return 0;
  497. }
  498. static int hal2_alloc_dac_dmabuf(struct hal2_codec *codec)
  499. {
  500. return hal2_alloc_dmabuf(codec, H2_DAC_BUFSIZE,
  501. H2_DAC_BUFSIZE / H2_BLOCK_SIZE,
  502. HPCDMA_XIE | HPCDMA_EOX,
  503. DMA_TO_DEVICE);
  504. }
  505. static int hal2_alloc_adc_dmabuf(struct hal2_codec *codec)
  506. {
  507. return hal2_alloc_dmabuf(codec, H2_ADC_BUFSIZE,
  508. H2_ADC_BUFSIZE / H2_BLOCK_SIZE,
  509. HPCDMA_XIE | H2_BLOCK_SIZE,
  510. DMA_TO_DEVICE);
  511. }
  512. static void hal2_free_dmabuf(struct hal2_codec *codec, int size, int dir)
  513. {
  514. dma_addr_t dma_addr;
  515. int i;
  516. dma_addr = codec->desc[codec->desc_count - 1].desc.pnext;
  517. for (i = 0; i < codec->desc_count; i++)
  518. dma_unmap_single(NULL, codec->desc[i].desc.pbuf,
  519. H2_BLOCK_SIZE, dir);
  520. dma_free_coherent(NULL, codec->desc_count * sizeof(struct hal2_desc),
  521. (void *)codec->desc, dma_addr);
  522. free_pages((unsigned long)codec->buffer, get_order(size));
  523. }
  524. static void hal2_free_dac_dmabuf(struct hal2_codec *codec)
  525. {
  526. return hal2_free_dmabuf(codec, H2_DAC_BUFSIZE, DMA_TO_DEVICE);
  527. }
  528. static void hal2_free_adc_dmabuf(struct hal2_codec *codec)
  529. {
  530. return hal2_free_dmabuf(codec, H2_ADC_BUFSIZE, DMA_FROM_DEVICE);
  531. }
  532. /*
  533. * Add 'count' bytes to 'buffer' from DMA ring buffers. Return number of
  534. * bytes added or -EFAULT if copy_from_user failed.
  535. */
  536. static int hal2_get_buffer(struct hal2_card *hal2, char *buffer, int count)
  537. {
  538. unsigned long flags;
  539. int size, ret = 0;
  540. unsigned char *buf;
  541. struct hal2_desc *tail;
  542. struct hal2_codec *adc = &hal2->adc;
  543. DEBUG("getting %d bytes ", count);
  544. spin_lock_irqsave(&adc->lock, flags);
  545. tail = &adc->desc[adc->tail];
  546. /* enable DMA stream if there are no data */
  547. if (!tail->cnt && !(adc->pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_ISACT))
  548. hal2_start_adc(hal2);
  549. while (tail->cnt > 0 && count > 0) {
  550. size = min((int)tail->cnt, count);
  551. buf = &adc->buffer[(adc->tail + 1) * H2_BLOCK_SIZE - tail->cnt];
  552. spin_unlock_irqrestore(&adc->lock, flags);
  553. dma_sync_single(NULL, tail->desc.pbuf, size, DMA_FROM_DEVICE);
  554. if (copy_to_user(buffer, buf, size)) {
  555. ret = -EFAULT;
  556. goto out;
  557. }
  558. spin_lock_irqsave(&adc->lock, flags);
  559. tail->cnt -= size;
  560. /* buffer is empty, update tail pointer */
  561. if (tail->cnt == 0) {
  562. tail->desc.cntinfo = HPCDMA_XIE | H2_BLOCK_SIZE;
  563. hal2_inc_tail(adc);
  564. tail = &adc->desc[adc->tail];
  565. /* enable DMA stream again if needed */
  566. if (!(adc->pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_ISACT))
  567. hal2_start_adc(hal2);
  568. }
  569. buffer += size;
  570. ret += size;
  571. count -= size;
  572. DEBUG("(%d) ", size);
  573. }
  574. spin_unlock_irqrestore(&adc->lock, flags);
  575. out:
  576. DEBUG("\n");
  577. return ret;
  578. }
  579. /*
  580. * Add 'count' bytes from 'buffer' to DMA ring buffers. Return number of
  581. * bytes added or -EFAULT if copy_from_user failed.
  582. */
  583. static int hal2_add_buffer(struct hal2_card *hal2, char *buffer, int count)
  584. {
  585. unsigned long flags;
  586. unsigned char *buf;
  587. int size, ret = 0;
  588. struct hal2_desc *head;
  589. struct hal2_codec *dac = &hal2->dac;
  590. DEBUG("adding %d bytes ", count);
  591. spin_lock_irqsave(&dac->lock, flags);
  592. head = &dac->desc[dac->head];
  593. while (head->cnt == 0 && count > 0) {
  594. size = min((int)H2_BLOCK_SIZE, count);
  595. buf = &dac->buffer[dac->head * H2_BLOCK_SIZE];
  596. spin_unlock_irqrestore(&dac->lock, flags);
  597. if (copy_from_user(buf, buffer, size)) {
  598. ret = -EFAULT;
  599. goto out;
  600. }
  601. dma_sync_single(NULL, head->desc.pbuf, size, DMA_TO_DEVICE);
  602. spin_lock_irqsave(&dac->lock, flags);
  603. head->desc.cntinfo = size | HPCDMA_XIE;
  604. head->cnt = size;
  605. buffer += size;
  606. ret += size;
  607. count -= size;
  608. hal2_inc_head(dac);
  609. head = &dac->desc[dac->head];
  610. DEBUG("(%d) ", size);
  611. }
  612. if (!(dac->pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_ISACT) && ret > 0)
  613. hal2_start_dac(hal2);
  614. spin_unlock_irqrestore(&dac->lock, flags);
  615. out:
  616. DEBUG("\n");
  617. return ret;
  618. }
  619. #define hal2_reset_dac_pointer(hal2) hal2_reset_pointer(hal2, 1)
  620. #define hal2_reset_adc_pointer(hal2) hal2_reset_pointer(hal2, 0)
  621. static void hal2_reset_pointer(struct hal2_card *hal2, int is_dac)
  622. {
  623. int i;
  624. struct hal2_codec *codec = (is_dac) ? &hal2->dac : &hal2->adc;
  625. DEBUG("hal2_reset_pointer\n");
  626. for (i = 0; i < codec->desc_count; i++) {
  627. codec->desc[i].cnt = 0;
  628. codec->desc[i].desc.cntinfo = HPCDMA_XIE | (is_dac) ?
  629. HPCDMA_EOX : H2_BLOCK_SIZE;
  630. }
  631. codec->head = codec->tail = 0;
  632. }
  633. static int hal2_sync_dac(struct hal2_card *hal2)
  634. {
  635. DECLARE_WAITQUEUE(wait, current);
  636. struct hal2_codec *dac = &hal2->dac;
  637. int ret = 0;
  638. unsigned long flags;
  639. signed long timeout = 1000 * H2_BLOCK_SIZE * 2 * dac->voices *
  640. HZ / dac->sample_rate / 900;
  641. while (dac->pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_ISACT) {
  642. add_wait_queue(&dac->dma_wait, &wait);
  643. set_current_state(TASK_INTERRUPTIBLE);
  644. schedule_timeout(timeout);
  645. spin_lock_irqsave(&dac->lock, flags);
  646. if (dac->desc[dac->tail].cnt)
  647. ret = -ETIME;
  648. spin_unlock_irqrestore(&dac->lock, flags);
  649. if (signal_pending(current))
  650. ret = -ERESTARTSYS;
  651. if (ret) {
  652. hal2_stop_dac(hal2);
  653. hal2_reset_dac_pointer(hal2);
  654. }
  655. remove_wait_queue(&dac->dma_wait, &wait);
  656. }
  657. return ret;
  658. }
  659. static int hal2_write_mixer(struct hal2_card *hal2, int index, int vol)
  660. {
  661. unsigned int l, r, tmp;
  662. DEBUG_MIX("mixer %d write\n", index);
  663. if (index >= SOUND_MIXER_NRDEVICES || !mixtable[index].avail)
  664. return -EINVAL;
  665. r = (vol >> 8) & 0xff;
  666. if (r > 100)
  667. r = 100;
  668. l = vol & 0xff;
  669. if (l > 100)
  670. l = 100;
  671. hal2->mixer.volume[mixtable[index].idx] = l | (r << 8);
  672. switch (mixtable[index].idx) {
  673. case H2_MIX_OUTPUT_ATT:
  674. DEBUG_MIX("output attenuator %d,%d\n", l, r);
  675. if (r | l) {
  676. tmp = hal2_i_look32(hal2, H2I_DAC_C2);
  677. tmp &= ~(H2I_C2_L_ATT_M | H2I_C2_R_ATT_M | H2I_C2_MUTE);
  678. /* Attenuator has five bits */
  679. l = 31 * (100 - l) / 99;
  680. r = 31 * (100 - r) / 99;
  681. DEBUG_MIX("left: %d, right %d\n", l, r);
  682. tmp |= (l << H2I_C2_L_ATT_SHIFT) & H2I_C2_L_ATT_M;
  683. tmp |= (r << H2I_C2_R_ATT_SHIFT) & H2I_C2_R_ATT_M;
  684. hal2_i_write32(hal2, H2I_DAC_C2, tmp);
  685. } else
  686. hal2_i_setbit32(hal2, H2I_DAC_C2, H2I_C2_MUTE);
  687. break;
  688. case H2_MIX_INPUT_GAIN:
  689. DEBUG_MIX("input gain %d,%d\n", l, r);
  690. tmp = hal2_i_look32(hal2, H2I_ADC_C2);
  691. tmp &= ~(H2I_C2_L_GAIN_M | H2I_C2_R_GAIN_M);
  692. /* Gain control has four bits */
  693. l = 16 * l / 100;
  694. r = 16 * r / 100;
  695. DEBUG_MIX("left: %d, right %d\n", l, r);
  696. tmp |= (l << H2I_C2_L_GAIN_SHIFT) & H2I_C2_L_GAIN_M;
  697. tmp |= (r << H2I_C2_R_GAIN_SHIFT) & H2I_C2_R_GAIN_M;
  698. hal2_i_write32(hal2, H2I_ADC_C2, tmp);
  699. break;
  700. }
  701. return 0;
  702. }
  703. static void hal2_init_mixer(struct hal2_card *hal2)
  704. {
  705. int i;
  706. for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  707. if (mixtable[i].avail)
  708. hal2->mixer.volume[mixtable[i].idx] = 100 | (100 << 8);
  709. /* disable attenuator */
  710. hal2_i_write32(hal2, H2I_DAC_C2, 0);
  711. /* set max input gain */
  712. hal2_i_write32(hal2, H2I_ADC_C2, H2I_C2_MUTE |
  713. (H2I_C2_L_GAIN_M << H2I_C2_L_GAIN_SHIFT) |
  714. (H2I_C2_R_GAIN_M << H2I_C2_R_GAIN_SHIFT));
  715. /* set max volume */
  716. hal2->mixer.master = 0xff;
  717. hal2->vol_regs->left = 0xff;
  718. hal2->vol_regs->right = 0xff;
  719. }
  720. /*
  721. * XXX: later i'll implement mixer for main volume which will be disabled
  722. * by default. enabling it users will be allowed to have master volume level
  723. * control on panel in their favourite X desktop
  724. */
  725. static void hal2_volume_control(int direction)
  726. {
  727. unsigned int master = hal2_card[0]->mixer.master;
  728. struct hal2_vol_regs *vol = hal2_card[0]->vol_regs;
  729. /* volume up */
  730. if (direction > 0 && master < 0xff)
  731. master++;
  732. /* volume down */
  733. else if (direction < 0 && master > 0)
  734. master--;
  735. /* TODO: mute/unmute */
  736. vol->left = master;
  737. vol->right = master;
  738. hal2_card[0]->mixer.master = master;
  739. }
  740. static int hal2_mixer_ioctl(struct hal2_card *hal2, unsigned int cmd,
  741. unsigned long arg)
  742. {
  743. int val;
  744. if (cmd == SOUND_MIXER_INFO) {
  745. mixer_info info;
  746. memset(&info, 0, sizeof(info));
  747. strlcpy(info.id, hal2str, sizeof(info.id));
  748. strlcpy(info.name, hal2str, sizeof(info.name));
  749. info.modify_counter = hal2->mixer.modcnt;
  750. if (copy_to_user((void *)arg, &info, sizeof(info)))
  751. return -EFAULT;
  752. return 0;
  753. }
  754. if (cmd == SOUND_OLD_MIXER_INFO) {
  755. _old_mixer_info info;
  756. memset(&info, 0, sizeof(info));
  757. strlcpy(info.id, hal2str, sizeof(info.id));
  758. strlcpy(info.name, hal2str, sizeof(info.name));
  759. if (copy_to_user((void *)arg, &info, sizeof(info)))
  760. return -EFAULT;
  761. return 0;
  762. }
  763. if (cmd == OSS_GETVERSION)
  764. return put_user(SOUND_VERSION, (int *)arg);
  765. if (_IOC_TYPE(cmd) != 'M' || _IOC_SIZE(cmd) != sizeof(int))
  766. return -EINVAL;
  767. if (_IOC_DIR(cmd) == _IOC_READ) {
  768. switch (_IOC_NR(cmd)) {
  769. /* Give the current record source */
  770. case SOUND_MIXER_RECSRC:
  771. val = 0; /* FIXME */
  772. break;
  773. /* Give the supported mixers, all of them support stereo */
  774. case SOUND_MIXER_DEVMASK:
  775. case SOUND_MIXER_STEREODEVS: {
  776. int i;
  777. for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  778. if (mixtable[i].avail)
  779. val |= 1 << i;
  780. break;
  781. }
  782. /* Arg contains a bit for each supported recording source */
  783. case SOUND_MIXER_RECMASK:
  784. val = 0;
  785. break;
  786. case SOUND_MIXER_CAPS:
  787. val = 0;
  788. break;
  789. /* Read a specific mixer */
  790. default: {
  791. int i = _IOC_NR(cmd);
  792. if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].avail)
  793. return -EINVAL;
  794. val = hal2->mixer.volume[mixtable[i].idx];
  795. break;
  796. }
  797. }
  798. return put_user(val, (int *)arg);
  799. }
  800. if (_IOC_DIR(cmd) != (_IOC_WRITE|_IOC_READ))
  801. return -EINVAL;
  802. hal2->mixer.modcnt++;
  803. if (get_user(val, (int *)arg))
  804. return -EFAULT;
  805. switch (_IOC_NR(cmd)) {
  806. /* Arg contains a bit for each recording source */
  807. case SOUND_MIXER_RECSRC:
  808. return 0; /* FIXME */
  809. default:
  810. return hal2_write_mixer(hal2, _IOC_NR(cmd), val);
  811. }
  812. return 0;
  813. }
  814. static int hal2_open_mixdev(struct inode *inode, struct file *file)
  815. {
  816. struct hal2_card *hal2 = hal2_mixer_find_card(iminor(inode));
  817. if (hal2) {
  818. file->private_data = hal2;
  819. return nonseekable_open(inode, file);
  820. }
  821. return -ENODEV;
  822. }
  823. static int hal2_release_mixdev(struct inode *inode, struct file *file)
  824. {
  825. return 0;
  826. }
  827. static int hal2_ioctl_mixdev(struct inode *inode, struct file *file,
  828. unsigned int cmd, unsigned long arg)
  829. {
  830. return hal2_mixer_ioctl((struct hal2_card *)file->private_data, cmd, arg);
  831. }
  832. static int hal2_ioctl(struct inode *inode, struct file *file,
  833. unsigned int cmd, unsigned long arg)
  834. {
  835. int val;
  836. struct hal2_card *hal2 = (struct hal2_card *) file->private_data;
  837. switch (cmd) {
  838. case OSS_GETVERSION:
  839. return put_user(SOUND_VERSION, (int *)arg);
  840. case SNDCTL_DSP_SYNC:
  841. if (file->f_mode & FMODE_WRITE)
  842. return hal2_sync_dac(hal2);
  843. return 0;
  844. case SNDCTL_DSP_SETDUPLEX:
  845. return 0;
  846. case SNDCTL_DSP_GETCAPS:
  847. return put_user(DSP_CAP_DUPLEX | DSP_CAP_MULTI, (int *)arg);
  848. case SNDCTL_DSP_RESET:
  849. if (file->f_mode & FMODE_READ) {
  850. hal2_stop_adc(hal2);
  851. hal2_reset_adc_pointer(hal2);
  852. }
  853. if (file->f_mode & FMODE_WRITE) {
  854. hal2_stop_dac(hal2);
  855. hal2_reset_dac_pointer(hal2);
  856. }
  857. return 0;
  858. case SNDCTL_DSP_SPEED:
  859. if (get_user(val, (int *)arg))
  860. return -EFAULT;
  861. if (file->f_mode & FMODE_READ) {
  862. hal2_stop_adc(hal2);
  863. val = hal2_compute_rate(&hal2->adc, val);
  864. hal2->adc.sample_rate = val;
  865. hal2_set_adc_rate(hal2);
  866. }
  867. if (file->f_mode & FMODE_WRITE) {
  868. hal2_stop_dac(hal2);
  869. val = hal2_compute_rate(&hal2->dac, val);
  870. hal2->dac.sample_rate = val;
  871. hal2_set_dac_rate(hal2);
  872. }
  873. return put_user(val, (int *)arg);
  874. case SNDCTL_DSP_STEREO:
  875. if (get_user(val, (int *)arg))
  876. return -EFAULT;
  877. if (file->f_mode & FMODE_READ) {
  878. hal2_stop_adc(hal2);
  879. hal2->adc.voices = (val) ? 2 : 1;
  880. hal2_setup_adc(hal2);
  881. }
  882. if (file->f_mode & FMODE_WRITE) {
  883. hal2_stop_dac(hal2);
  884. hal2->dac.voices = (val) ? 2 : 1;
  885. hal2_setup_dac(hal2);
  886. }
  887. return 0;
  888. case SNDCTL_DSP_CHANNELS:
  889. if (get_user(val, (int *)arg))
  890. return -EFAULT;
  891. if (val != 0) {
  892. if (file->f_mode & FMODE_READ) {
  893. hal2_stop_adc(hal2);
  894. hal2->adc.voices = (val == 1) ? 1 : 2;
  895. hal2_setup_adc(hal2);
  896. }
  897. if (file->f_mode & FMODE_WRITE) {
  898. hal2_stop_dac(hal2);
  899. hal2->dac.voices = (val == 1) ? 1 : 2;
  900. hal2_setup_dac(hal2);
  901. }
  902. }
  903. val = -EINVAL;
  904. if (file->f_mode & FMODE_READ)
  905. val = hal2->adc.voices;
  906. if (file->f_mode & FMODE_WRITE)
  907. val = hal2->dac.voices;
  908. return put_user(val, (int *)arg);
  909. case SNDCTL_DSP_GETFMTS: /* Returns a mask */
  910. return put_user(H2_SUPPORTED_FORMATS, (int *)arg);
  911. case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
  912. if (get_user(val, (int *)arg))
  913. return -EFAULT;
  914. if (val != AFMT_QUERY) {
  915. if (!(val & H2_SUPPORTED_FORMATS))
  916. return -EINVAL;
  917. if (file->f_mode & FMODE_READ) {
  918. hal2_stop_adc(hal2);
  919. hal2->adc.format = val;
  920. hal2_setup_adc(hal2);
  921. }
  922. if (file->f_mode & FMODE_WRITE) {
  923. hal2_stop_dac(hal2);
  924. hal2->dac.format = val;
  925. hal2_setup_dac(hal2);
  926. }
  927. } else {
  928. val = -EINVAL;
  929. if (file->f_mode & FMODE_READ)
  930. val = hal2->adc.format;
  931. if (file->f_mode & FMODE_WRITE)
  932. val = hal2->dac.format;
  933. }
  934. return put_user(val, (int *)arg);
  935. case SNDCTL_DSP_POST:
  936. return 0;
  937. case SNDCTL_DSP_GETOSPACE: {
  938. audio_buf_info info;
  939. int i;
  940. unsigned long flags;
  941. struct hal2_codec *dac = &hal2->dac;
  942. if (!(file->f_mode & FMODE_WRITE))
  943. return -EINVAL;
  944. info.fragments = 0;
  945. spin_lock_irqsave(&dac->lock, flags);
  946. for (i = 0; i < dac->desc_count; i++)
  947. if (dac->desc[i].cnt == 0)
  948. info.fragments++;
  949. spin_unlock_irqrestore(&dac->lock, flags);
  950. info.fragstotal = dac->desc_count;
  951. info.fragsize = H2_BLOCK_SIZE;
  952. info.bytes = info.fragsize * info.fragments;
  953. return copy_to_user((void *)arg, &info, sizeof(info)) ? -EFAULT : 0;
  954. }
  955. case SNDCTL_DSP_GETISPACE: {
  956. audio_buf_info info;
  957. int i;
  958. unsigned long flags;
  959. struct hal2_codec *adc = &hal2->adc;
  960. if (!(file->f_mode & FMODE_READ))
  961. return -EINVAL;
  962. info.fragments = 0;
  963. info.bytes = 0;
  964. spin_lock_irqsave(&adc->lock, flags);
  965. for (i = 0; i < adc->desc_count; i++)
  966. if (adc->desc[i].cnt > 0) {
  967. info.fragments++;
  968. info.bytes += adc->desc[i].cnt;
  969. }
  970. spin_unlock_irqrestore(&adc->lock, flags);
  971. info.fragstotal = adc->desc_count;
  972. info.fragsize = H2_BLOCK_SIZE;
  973. return copy_to_user((void *)arg, &info, sizeof(info)) ? -EFAULT : 0;
  974. }
  975. case SNDCTL_DSP_NONBLOCK:
  976. file->f_flags |= O_NONBLOCK;
  977. return 0;
  978. case SNDCTL_DSP_GETBLKSIZE:
  979. return put_user(H2_BLOCK_SIZE, (int *)arg);
  980. case SNDCTL_DSP_SETFRAGMENT:
  981. return 0;
  982. case SOUND_PCM_READ_RATE:
  983. val = -EINVAL;
  984. if (file->f_mode & FMODE_READ)
  985. val = hal2->adc.sample_rate;
  986. if (file->f_mode & FMODE_WRITE)
  987. val = hal2->dac.sample_rate;
  988. return put_user(val, (int *)arg);
  989. case SOUND_PCM_READ_CHANNELS:
  990. val = -EINVAL;
  991. if (file->f_mode & FMODE_READ)
  992. val = hal2->adc.voices;
  993. if (file->f_mode & FMODE_WRITE)
  994. val = hal2->dac.voices;
  995. return put_user(val, (int *)arg);
  996. case SOUND_PCM_READ_BITS:
  997. return put_user(16, (int *)arg);
  998. }
  999. return hal2_mixer_ioctl(hal2, cmd, arg);
  1000. }
  1001. static ssize_t hal2_read(struct file *file, char *buffer,
  1002. size_t count, loff_t *ppos)
  1003. {
  1004. ssize_t err;
  1005. struct hal2_card *hal2 = (struct hal2_card *) file->private_data;
  1006. struct hal2_codec *adc = &hal2->adc;
  1007. if (!count)
  1008. return 0;
  1009. if (down_interruptible(&adc->sem))
  1010. return -EINTR;
  1011. if (file->f_flags & O_NONBLOCK) {
  1012. err = hal2_get_buffer(hal2, buffer, count);
  1013. err = err == 0 ? -EAGAIN : err;
  1014. } else {
  1015. do {
  1016. /* ~10% longer */
  1017. signed long timeout = 1000 * H2_BLOCK_SIZE *
  1018. 2 * adc->voices * HZ / adc->sample_rate / 900;
  1019. unsigned long flags;
  1020. DECLARE_WAITQUEUE(wait, current);
  1021. ssize_t cnt = 0;
  1022. err = hal2_get_buffer(hal2, buffer, count);
  1023. if (err > 0) {
  1024. count -= err;
  1025. cnt += err;
  1026. buffer += err;
  1027. err = cnt;
  1028. }
  1029. if (count > 0 && err >= 0) {
  1030. add_wait_queue(&adc->dma_wait, &wait);
  1031. set_current_state(TASK_INTERRUPTIBLE);
  1032. schedule_timeout(timeout);
  1033. spin_lock_irqsave(&adc->lock, flags);
  1034. if (!adc->desc[adc->tail].cnt)
  1035. err = -EAGAIN;
  1036. spin_unlock_irqrestore(&adc->lock, flags);
  1037. if (signal_pending(current))
  1038. err = -ERESTARTSYS;
  1039. remove_wait_queue(&adc->dma_wait, &wait);
  1040. if (err < 0) {
  1041. hal2_stop_adc(hal2);
  1042. hal2_reset_adc_pointer(hal2);
  1043. }
  1044. }
  1045. } while (count > 0 && err >= 0);
  1046. }
  1047. up(&adc->sem);
  1048. return err;
  1049. }
  1050. static ssize_t hal2_write(struct file *file, const char *buffer,
  1051. size_t count, loff_t *ppos)
  1052. {
  1053. ssize_t err;
  1054. char *buf = (char*) buffer;
  1055. struct hal2_card *hal2 = (struct hal2_card *) file->private_data;
  1056. struct hal2_codec *dac = &hal2->dac;
  1057. if (!count)
  1058. return 0;
  1059. if (down_interruptible(&dac->sem))
  1060. return -EINTR;
  1061. if (file->f_flags & O_NONBLOCK) {
  1062. err = hal2_add_buffer(hal2, buf, count);
  1063. err = err == 0 ? -EAGAIN : err;
  1064. } else {
  1065. do {
  1066. /* ~10% longer */
  1067. signed long timeout = 1000 * H2_BLOCK_SIZE *
  1068. 2 * dac->voices * HZ / dac->sample_rate / 900;
  1069. unsigned long flags;
  1070. DECLARE_WAITQUEUE(wait, current);
  1071. ssize_t cnt = 0;
  1072. err = hal2_add_buffer(hal2, buf, count);
  1073. if (err > 0) {
  1074. count -= err;
  1075. cnt += err;
  1076. buf += err;
  1077. err = cnt;
  1078. }
  1079. if (count > 0 && err >= 0) {
  1080. add_wait_queue(&dac->dma_wait, &wait);
  1081. set_current_state(TASK_INTERRUPTIBLE);
  1082. schedule_timeout(timeout);
  1083. spin_lock_irqsave(&dac->lock, flags);
  1084. if (dac->desc[dac->head].cnt)
  1085. err = -EAGAIN;
  1086. spin_unlock_irqrestore(&dac->lock, flags);
  1087. if (signal_pending(current))
  1088. err = -ERESTARTSYS;
  1089. remove_wait_queue(&dac->dma_wait, &wait);
  1090. if (err < 0) {
  1091. hal2_stop_dac(hal2);
  1092. hal2_reset_dac_pointer(hal2);
  1093. }
  1094. }
  1095. } while (count > 0 && err >= 0);
  1096. }
  1097. up(&dac->sem);
  1098. return err;
  1099. }
  1100. static unsigned int hal2_poll(struct file *file, struct poll_table_struct *wait)
  1101. {
  1102. unsigned long flags;
  1103. unsigned int mask = 0;
  1104. struct hal2_card *hal2 = (struct hal2_card *) file->private_data;
  1105. if (file->f_mode & FMODE_READ) {
  1106. struct hal2_codec *adc = &hal2->adc;
  1107. poll_wait(file, &adc->dma_wait, wait);
  1108. spin_lock_irqsave(&adc->lock, flags);
  1109. if (adc->desc[adc->tail].cnt > 0)
  1110. mask |= POLLIN;
  1111. spin_unlock_irqrestore(&adc->lock, flags);
  1112. }
  1113. if (file->f_mode & FMODE_WRITE) {
  1114. struct hal2_codec *dac = &hal2->dac;
  1115. poll_wait(file, &dac->dma_wait, wait);
  1116. spin_lock_irqsave(&dac->lock, flags);
  1117. if (dac->desc[dac->head].cnt == 0)
  1118. mask |= POLLOUT;
  1119. spin_unlock_irqrestore(&dac->lock, flags);
  1120. }
  1121. return mask;
  1122. }
  1123. static int hal2_open(struct inode *inode, struct file *file)
  1124. {
  1125. int err;
  1126. struct hal2_card *hal2 = hal2_dsp_find_card(iminor(inode));
  1127. if (!hal2)
  1128. return -ENODEV;
  1129. file->private_data = hal2;
  1130. if (file->f_mode & FMODE_READ) {
  1131. struct hal2_codec *adc = &hal2->adc;
  1132. if (adc->usecount)
  1133. return -EBUSY;
  1134. /* OSS spec wanted us to use 8 bit, 8 kHz mono by default,
  1135. * but HAL2 can't do 8bit audio */
  1136. adc->format = AFMT_S16_BE;
  1137. adc->voices = 1;
  1138. adc->sample_rate = hal2_compute_rate(adc, 8000);
  1139. hal2_set_adc_rate(hal2);
  1140. err = hal2_alloc_adc_dmabuf(adc);
  1141. if (err)
  1142. return err;
  1143. hal2_setup_adc(hal2);
  1144. adc->usecount++;
  1145. }
  1146. if (file->f_mode & FMODE_WRITE) {
  1147. struct hal2_codec *dac = &hal2->dac;
  1148. if (dac->usecount)
  1149. return -EBUSY;
  1150. dac->format = AFMT_S16_BE;
  1151. dac->voices = 1;
  1152. dac->sample_rate = hal2_compute_rate(dac, 8000);
  1153. hal2_set_dac_rate(hal2);
  1154. err = hal2_alloc_dac_dmabuf(dac);
  1155. if (err)
  1156. return err;
  1157. hal2_setup_dac(hal2);
  1158. dac->usecount++;
  1159. }
  1160. return nonseekable_open(inode, file);
  1161. }
  1162. static int hal2_release(struct inode *inode, struct file *file)
  1163. {
  1164. struct hal2_card *hal2 = (struct hal2_card *) file->private_data;
  1165. if (file->f_mode & FMODE_READ) {
  1166. struct hal2_codec *adc = &hal2->adc;
  1167. down(&adc->sem);
  1168. hal2_stop_adc(hal2);
  1169. hal2_free_adc_dmabuf(adc);
  1170. adc->usecount--;
  1171. up(&adc->sem);
  1172. }
  1173. if (file->f_mode & FMODE_WRITE) {
  1174. struct hal2_codec *dac = &hal2->dac;
  1175. down(&dac->sem);
  1176. hal2_sync_dac(hal2);
  1177. hal2_free_dac_dmabuf(dac);
  1178. dac->usecount--;
  1179. up(&dac->sem);
  1180. }
  1181. return 0;
  1182. }
  1183. static struct file_operations hal2_audio_fops = {
  1184. .owner = THIS_MODULE,
  1185. .llseek = no_llseek,
  1186. .read = hal2_read,
  1187. .write = hal2_write,
  1188. .poll = hal2_poll,
  1189. .ioctl = hal2_ioctl,
  1190. .open = hal2_open,
  1191. .release = hal2_release,
  1192. };
  1193. static struct file_operations hal2_mixer_fops = {
  1194. .owner = THIS_MODULE,
  1195. .llseek = no_llseek,
  1196. .ioctl = hal2_ioctl_mixdev,
  1197. .open = hal2_open_mixdev,
  1198. .release = hal2_release_mixdev,
  1199. };
  1200. static void hal2_init_codec(struct hal2_codec *codec, struct hpc3_regs *hpc3,
  1201. int index)
  1202. {
  1203. codec->pbus.pbusnr = index;
  1204. codec->pbus.pbus = &hpc3->pbdma[index];
  1205. init_waitqueue_head(&codec->dma_wait);
  1206. init_MUTEX(&codec->sem);
  1207. spin_lock_init(&codec->lock);
  1208. }
  1209. static int hal2_detect(struct hal2_card *hal2)
  1210. {
  1211. unsigned short board, major, minor;
  1212. unsigned short rev;
  1213. /* reset HAL2 */
  1214. hal2_isr_write(hal2, 0);
  1215. /* release reset */
  1216. hal2_isr_write(hal2, H2_ISR_GLOBAL_RESET_N | H2_ISR_CODEC_RESET_N);
  1217. hal2_i_write16(hal2, H2I_RELAY_C, H2I_RELAY_C_STATE);
  1218. if ((rev = hal2_rev_look(hal2)) & H2_REV_AUDIO_PRESENT)
  1219. return -ENODEV;
  1220. board = (rev & H2_REV_BOARD_M) >> 12;
  1221. major = (rev & H2_REV_MAJOR_CHIP_M) >> 4;
  1222. minor = (rev & H2_REV_MINOR_CHIP_M);
  1223. printk(KERN_INFO "SGI HAL2 revision %i.%i.%i\n",
  1224. board, major, minor);
  1225. return 0;
  1226. }
  1227. static int hal2_init_card(struct hal2_card **phal2, struct hpc3_regs *hpc3)
  1228. {
  1229. int ret = 0;
  1230. struct hal2_card *hal2;
  1231. hal2 = (struct hal2_card *) kmalloc(sizeof(struct hal2_card), GFP_KERNEL);
  1232. if (!hal2)
  1233. return -ENOMEM;
  1234. memset(hal2, 0, sizeof(struct hal2_card));
  1235. hal2->ctl_regs = (struct hal2_ctl_regs *)hpc3->pbus_extregs[0];
  1236. hal2->aes_regs = (struct hal2_aes_regs *)hpc3->pbus_extregs[1];
  1237. hal2->vol_regs = (struct hal2_vol_regs *)hpc3->pbus_extregs[2];
  1238. hal2->syn_regs = (struct hal2_syn_regs *)hpc3->pbus_extregs[3];
  1239. if (hal2_detect(hal2) < 0) {
  1240. ret = -ENODEV;
  1241. goto free_card;
  1242. }
  1243. hal2_init_codec(&hal2->dac, hpc3, 0);
  1244. hal2_init_codec(&hal2->adc, hpc3, 1);
  1245. /*
  1246. * All DMA channel interfaces in HAL2 are designed to operate with
  1247. * PBUS programmed for 2 cycles in D3, 2 cycles in D4 and 2 cycles
  1248. * in D5. HAL2 is a 16-bit device which can accept both big and little
  1249. * endian format. It assumes that even address bytes are on high
  1250. * portion of PBUS (15:8) and assumes that HPC3 is programmed to
  1251. * accept a live (unsynchronized) version of P_DREQ_N from HAL2.
  1252. */
  1253. #define HAL2_PBUS_DMACFG ((0 << HPC3_DMACFG_D3R_SHIFT) | \
  1254. (2 << HPC3_DMACFG_D4R_SHIFT) | \
  1255. (2 << HPC3_DMACFG_D5R_SHIFT) | \
  1256. (0 << HPC3_DMACFG_D3W_SHIFT) | \
  1257. (2 << HPC3_DMACFG_D4W_SHIFT) | \
  1258. (2 << HPC3_DMACFG_D5W_SHIFT) | \
  1259. HPC3_DMACFG_DS16 | \
  1260. HPC3_DMACFG_EVENHI | \
  1261. HPC3_DMACFG_RTIME | \
  1262. (8 << HPC3_DMACFG_BURST_SHIFT) | \
  1263. HPC3_DMACFG_DRQLIVE)
  1264. /*
  1265. * Ignore what's mentioned in the specification and write value which
  1266. * works in The Real World (TM)
  1267. */
  1268. hpc3->pbus_dmacfg[hal2->dac.pbus.pbusnr][0] = 0x8208844;
  1269. hpc3->pbus_dmacfg[hal2->adc.pbus.pbusnr][0] = 0x8208844;
  1270. if (request_irq(SGI_HPCDMA_IRQ, hal2_interrupt, SA_SHIRQ,
  1271. hal2str, hal2)) {
  1272. printk(KERN_ERR "HAL2: Can't get irq %d\n", SGI_HPCDMA_IRQ);
  1273. ret = -EAGAIN;
  1274. goto free_card;
  1275. }
  1276. hal2->dev_dsp = register_sound_dsp(&hal2_audio_fops, -1);
  1277. if (hal2->dev_dsp < 0) {
  1278. ret = hal2->dev_dsp;
  1279. goto free_irq;
  1280. }
  1281. hal2->dev_mixer = register_sound_mixer(&hal2_mixer_fops, -1);
  1282. if (hal2->dev_mixer < 0) {
  1283. ret = hal2->dev_mixer;
  1284. goto unregister_dsp;
  1285. }
  1286. hal2_init_mixer(hal2);
  1287. *phal2 = hal2;
  1288. return 0;
  1289. unregister_dsp:
  1290. unregister_sound_dsp(hal2->dev_dsp);
  1291. free_irq:
  1292. free_irq(SGI_HPCDMA_IRQ, hal2);
  1293. free_card:
  1294. kfree(hal2);
  1295. return ret;
  1296. }
  1297. extern void (*indy_volume_button)(int);
  1298. /*
  1299. * Assuming only one HAL2 card. Mail me if you ever meet machine with
  1300. * more than one.
  1301. */
  1302. static int __init init_hal2(void)
  1303. {
  1304. int i, error;
  1305. for (i = 0; i < MAXCARDS; i++)
  1306. hal2_card[i] = NULL;
  1307. error = hal2_init_card(&hal2_card[0], hpc3c0);
  1308. /* let Indy's volume buttons work */
  1309. if (!error && !ip22_is_fullhouse())
  1310. indy_volume_button = hal2_volume_control;
  1311. return error;
  1312. }
  1313. static void __exit exit_hal2(void)
  1314. {
  1315. int i;
  1316. /* unregister volume butons callback function */
  1317. indy_volume_button = NULL;
  1318. for (i = 0; i < MAXCARDS; i++)
  1319. if (hal2_card[i]) {
  1320. free_irq(SGI_HPCDMA_IRQ, hal2_card[i]);
  1321. unregister_sound_dsp(hal2_card[i]->dev_dsp);
  1322. unregister_sound_mixer(hal2_card[i]->dev_mixer);
  1323. kfree(hal2_card[i]);
  1324. }
  1325. }
  1326. module_init(init_hal2);
  1327. module_exit(exit_hal2);
  1328. MODULE_DESCRIPTION("OSS compatible driver for SGI HAL2 audio");
  1329. MODULE_AUTHOR("Ladislav Michl");
  1330. MODULE_LICENSE("GPL");