nm256_audio.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  1. /*
  2. * Audio driver for the NeoMagic 256AV and 256ZX chipsets in native
  3. * mode, with AC97 mixer support.
  4. *
  5. * Overall design and parts of this code stolen from vidc_*.c and
  6. * skeleton.c.
  7. *
  8. * Yeah, there are a lot of magic constants in here. You tell ME what
  9. * they are. I just get this stuff psychically, remember?
  10. *
  11. * This driver was written by someone who wishes to remain anonymous.
  12. * It is in the public domain, so share and enjoy. Try to make a profit
  13. * off of it; go on, I dare you.
  14. *
  15. * Changes:
  16. * 11-10-2000 Bartlomiej Zolnierkiewicz <bkz@linux-ide.org>
  17. * Added some __init
  18. * 19-04-2001 Marcus Meissner <mm@caldera.de>
  19. * Ported to 2.4 PCI API.
  20. */
  21. #include <linux/pci.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/delay.h>
  27. #include <linux/spinlock.h>
  28. #include "sound_config.h"
  29. static int nm256_debug;
  30. static int force_load;
  31. #include "nm256.h"
  32. #include "nm256_coeff.h"
  33. /*
  34. * The size of the playback reserve. When the playback buffer has less
  35. * than NM256_PLAY_WMARK_SIZE bytes to output, we request a new
  36. * buffer.
  37. */
  38. #define NM256_PLAY_WMARK_SIZE 512
  39. static struct audio_driver nm256_audio_driver;
  40. static int nm256_grabInterrupt (struct nm256_info *card);
  41. static int nm256_releaseInterrupt (struct nm256_info *card);
  42. static irqreturn_t nm256_interrupt (int irq, void *dev_id);
  43. static irqreturn_t nm256_interrupt_zx (int irq, void *dev_id);
  44. /* These belong in linux/pci.h. */
  45. #define PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO 0x8005
  46. #define PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO 0x8006
  47. #define PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO 0x8016
  48. /* List of cards. */
  49. static struct nm256_info *nmcard_list;
  50. /* Release the mapped-in memory for CARD. */
  51. static void
  52. nm256_release_ports (struct nm256_info *card)
  53. {
  54. int x;
  55. for (x = 0; x < 2; x++) {
  56. if (card->port[x].ptr != NULL) {
  57. iounmap (card->port[x].ptr);
  58. card->port[x].ptr = NULL;
  59. }
  60. }
  61. }
  62. /*
  63. * Map in the memory ports for CARD, if they aren't already mapped in
  64. * and have been configured. If successful, a zero value is returned;
  65. * otherwise any previously mapped-in areas are released and a non-zero
  66. * value is returned.
  67. *
  68. * This is invoked twice, once for each port. Ideally it would only be
  69. * called once, but we now need to map in the second port in order to
  70. * check how much memory the card has on the 256ZX.
  71. */
  72. static int
  73. nm256_remap_ports (struct nm256_info *card)
  74. {
  75. int x;
  76. for (x = 0; x < 2; x++) {
  77. if (card->port[x].ptr == NULL && card->port[x].end_offset > 0) {
  78. u32 physaddr
  79. = card->port[x].physaddr + card->port[x].start_offset;
  80. u32 size
  81. = card->port[x].end_offset - card->port[x].start_offset;
  82. card->port[x].ptr = ioremap_nocache (physaddr, size);
  83. if (card->port[x].ptr == NULL) {
  84. printk (KERN_ERR "NM256: Unable to remap port %d\n", x + 1);
  85. nm256_release_ports (card);
  86. return -1;
  87. }
  88. }
  89. }
  90. return 0;
  91. }
  92. /* Locate the card in our list. */
  93. static struct nm256_info *
  94. nm256_find_card (int dev)
  95. {
  96. struct nm256_info *card;
  97. for (card = nmcard_list; card != NULL; card = card->next_card)
  98. if (card->dev[0] == dev || card->dev[1] == dev)
  99. return card;
  100. return NULL;
  101. }
  102. /*
  103. * Ditto, but find the card struct corresponding to the mixer device DEV
  104. * instead.
  105. */
  106. static struct nm256_info *
  107. nm256_find_card_for_mixer (int dev)
  108. {
  109. struct nm256_info *card;
  110. for (card = nmcard_list; card != NULL; card = card->next_card)
  111. if (card->mixer_oss_dev == dev)
  112. return card;
  113. return NULL;
  114. }
  115. static int usecache;
  116. static int buffertop;
  117. /* Check to see if we're using the bank of cached coefficients. */
  118. static int
  119. nm256_cachedCoefficients (struct nm256_info *card)
  120. {
  121. return usecache;
  122. }
  123. /* The actual rates supported by the card. */
  124. static int samplerates[9] = {
  125. 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000, 99999999
  126. };
  127. /*
  128. * Set the card samplerate, word size and stereo mode to correspond to
  129. * the settings in the CARD struct for the specified device in DEV.
  130. * We keep two separate sets of information, one for each device; the
  131. * hardware is not actually configured until a read or write is
  132. * attempted.
  133. */
  134. static int
  135. nm256_setInfo (int dev, struct nm256_info *card)
  136. {
  137. int x;
  138. int w;
  139. int targetrate;
  140. if (card->dev[0] == dev)
  141. w = 0;
  142. else if (card->dev[1] == dev)
  143. w = 1;
  144. else
  145. return -ENODEV;
  146. targetrate = card->sinfo[w].samplerate;
  147. if ((card->sinfo[w].bits != 8 && card->sinfo[w].bits != 16)
  148. || targetrate < samplerates[0]
  149. || targetrate > samplerates[7])
  150. return -EINVAL;
  151. for (x = 0; x < 8; x++)
  152. if (targetrate < ((samplerates[x] + samplerates[x + 1]) / 2))
  153. break;
  154. if (x < 8) {
  155. u8 ratebits = ((x << 4) & NM_RATE_MASK);
  156. if (card->sinfo[w].bits == 16)
  157. ratebits |= NM_RATE_BITS_16;
  158. if (card->sinfo[w].stereo)
  159. ratebits |= NM_RATE_STEREO;
  160. card->sinfo[w].samplerate = samplerates[x];
  161. if (card->dev_for_play == dev && card->playing) {
  162. if (nm256_debug)
  163. printk (KERN_DEBUG "Setting play ratebits to 0x%x\n",
  164. ratebits);
  165. nm256_loadCoefficient (card, 0, x);
  166. nm256_writePort8 (card, 2,
  167. NM_PLAYBACK_REG_OFFSET + NM_RATE_REG_OFFSET,
  168. ratebits);
  169. }
  170. if (card->dev_for_record == dev && card->recording) {
  171. if (nm256_debug)
  172. printk (KERN_DEBUG "Setting record ratebits to 0x%x\n",
  173. ratebits);
  174. nm256_loadCoefficient (card, 1, x);
  175. nm256_writePort8 (card, 2,
  176. NM_RECORD_REG_OFFSET + NM_RATE_REG_OFFSET,
  177. ratebits);
  178. }
  179. return 0;
  180. }
  181. else
  182. return -EINVAL;
  183. }
  184. /* Start the play process going. */
  185. static void
  186. startPlay (struct nm256_info *card)
  187. {
  188. if (! card->playing) {
  189. card->playing = 1;
  190. if (nm256_grabInterrupt (card) == 0) {
  191. nm256_setInfo (card->dev_for_play, card);
  192. /* Enable playback engine and interrupts. */
  193. nm256_writePort8 (card, 2, NM_PLAYBACK_ENABLE_REG,
  194. NM_PLAYBACK_ENABLE_FLAG | NM_PLAYBACK_FREERUN);
  195. /* Enable both channels. */
  196. nm256_writePort16 (card, 2, NM_AUDIO_MUTE_REG, 0x0);
  197. }
  198. }
  199. }
  200. /*
  201. * Request one chunk of AMT bytes from the recording device. When the
  202. * operation is complete, the data will be copied into BUFFER and the
  203. * function DMAbuf_inputintr will be invoked.
  204. */
  205. static void
  206. nm256_startRecording (struct nm256_info *card, char *buffer, u32 amt)
  207. {
  208. u32 endpos;
  209. int enableEngine = 0;
  210. u32 ringsize = card->recordBufferSize;
  211. unsigned long flags;
  212. if (amt > (ringsize / 2)) {
  213. /*
  214. * Of course this won't actually work right, because the
  215. * caller is going to assume we will give what we got asked
  216. * for.
  217. */
  218. printk (KERN_ERR "NM256: Read request too large: %d\n", amt);
  219. amt = ringsize / 2;
  220. }
  221. if (amt < 8) {
  222. printk (KERN_ERR "NM256: Read request too small; %d\n", amt);
  223. return;
  224. }
  225. spin_lock_irqsave(&card->lock,flags);
  226. /*
  227. * If we're not currently recording, set up the start and end registers
  228. * for the recording engine.
  229. */
  230. if (! card->recording) {
  231. card->recording = 1;
  232. if (nm256_grabInterrupt (card) == 0) {
  233. card->curRecPos = 0;
  234. nm256_setInfo (card->dev_for_record, card);
  235. nm256_writePort32 (card, 2, NM_RBUFFER_START, card->abuf2);
  236. nm256_writePort32 (card, 2, NM_RBUFFER_END,
  237. card->abuf2 + ringsize);
  238. nm256_writePort32 (card, 2, NM_RBUFFER_CURRP,
  239. card->abuf2 + card->curRecPos);
  240. enableEngine = 1;
  241. }
  242. else {
  243. /* Not sure what else to do here. */
  244. spin_unlock_irqrestore(&card->lock,flags);
  245. return;
  246. }
  247. }
  248. /*
  249. * If we happen to go past the end of the buffer a bit (due to a
  250. * delayed interrupt) it's OK. So might as well set the watermark
  251. * right at the end of the data we want.
  252. */
  253. endpos = card->abuf2 + ((card->curRecPos + amt) % ringsize);
  254. card->recBuf = buffer;
  255. card->requestedRecAmt = amt;
  256. nm256_writePort32 (card, 2, NM_RBUFFER_WMARK, endpos);
  257. /* Enable recording engine and interrupts. */
  258. if (enableEngine)
  259. nm256_writePort8 (card, 2, NM_RECORD_ENABLE_REG,
  260. NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);
  261. spin_unlock_irqrestore(&card->lock,flags);
  262. }
  263. /* Stop the play engine. */
  264. static void
  265. stopPlay (struct nm256_info *card)
  266. {
  267. /* Shut off sound from both channels. */
  268. nm256_writePort16 (card, 2, NM_AUDIO_MUTE_REG,
  269. NM_AUDIO_MUTE_LEFT | NM_AUDIO_MUTE_RIGHT);
  270. /* Disable play engine. */
  271. nm256_writePort8 (card, 2, NM_PLAYBACK_ENABLE_REG, 0);
  272. if (card->playing) {
  273. nm256_releaseInterrupt (card);
  274. /* Reset the relevant state bits. */
  275. card->playing = 0;
  276. card->curPlayPos = 0;
  277. }
  278. }
  279. /* Stop recording. */
  280. static void
  281. stopRecord (struct nm256_info *card)
  282. {
  283. /* Disable recording engine. */
  284. nm256_writePort8 (card, 2, NM_RECORD_ENABLE_REG, 0);
  285. if (card->recording) {
  286. nm256_releaseInterrupt (card);
  287. card->recording = 0;
  288. card->curRecPos = 0;
  289. }
  290. }
  291. /*
  292. * Ring buffers, man. That's where the hip-hop, wild-n-wooly action's at.
  293. * 1972? (Well, I suppose it was cheep-n-easy to implement.)
  294. *
  295. * Write AMT bytes of BUFFER to the playback ring buffer, and start the
  296. * playback engine running. It will only accept up to 1/2 of the total
  297. * size of the ring buffer. No check is made that we're about to overwrite
  298. * the currently-playing sample.
  299. */
  300. static void
  301. nm256_write_block (struct nm256_info *card, char *buffer, u32 amt)
  302. {
  303. u32 ringsize = card->playbackBufferSize;
  304. u32 endstop;
  305. unsigned long flags;
  306. if (amt > (ringsize / 2)) {
  307. printk (KERN_ERR "NM256: Write request too large: %d\n", amt);
  308. amt = (ringsize / 2);
  309. }
  310. if (amt < NM256_PLAY_WMARK_SIZE) {
  311. printk (KERN_ERR "NM256: Write request too small: %d\n", amt);
  312. return;
  313. }
  314. card->curPlayPos %= ringsize;
  315. card->requested_amt = amt;
  316. spin_lock_irqsave(&card->lock,flags);
  317. if ((card->curPlayPos + amt) >= ringsize) {
  318. u32 rem = ringsize - card->curPlayPos;
  319. nm256_writeBuffer8 (card, buffer, 1,
  320. card->abuf1 + card->curPlayPos,
  321. rem);
  322. if (amt > rem)
  323. nm256_writeBuffer8 (card, buffer + rem, 1, card->abuf1,
  324. amt - rem);
  325. }
  326. else
  327. nm256_writeBuffer8 (card, buffer, 1,
  328. card->abuf1 + card->curPlayPos,
  329. amt);
  330. /*
  331. * Setup the start-n-stop-n-limit registers, and start that engine
  332. * goin'.
  333. *
  334. * Normally we just let it wrap around to avoid the click-click
  335. * action scene.
  336. */
  337. if (! card->playing) {
  338. /* The PBUFFER_END register in this case points to one sample
  339. before the end of the buffer. */
  340. int w = (card->dev_for_play == card->dev[0] ? 0 : 1);
  341. int sampsize = (card->sinfo[w].bits == 16 ? 2 : 1);
  342. if (card->sinfo[w].stereo)
  343. sampsize *= 2;
  344. /* Need to set the not-normally-changing-registers up. */
  345. nm256_writePort32 (card, 2, NM_PBUFFER_START,
  346. card->abuf1 + card->curPlayPos);
  347. nm256_writePort32 (card, 2, NM_PBUFFER_END,
  348. card->abuf1 + ringsize - sampsize);
  349. nm256_writePort32 (card, 2, NM_PBUFFER_CURRP,
  350. card->abuf1 + card->curPlayPos);
  351. }
  352. endstop = (card->curPlayPos + amt - NM256_PLAY_WMARK_SIZE) % ringsize;
  353. nm256_writePort32 (card, 2, NM_PBUFFER_WMARK, card->abuf1 + endstop);
  354. if (! card->playing)
  355. startPlay (card);
  356. spin_unlock_irqrestore(&card->lock,flags);
  357. }
  358. /* We just got a card playback interrupt; process it. */
  359. static void
  360. nm256_get_new_block (struct nm256_info *card)
  361. {
  362. /* Check to see how much got played so far. */
  363. u32 amt = nm256_readPort32 (card, 2, NM_PBUFFER_CURRP) - card->abuf1;
  364. if (amt >= card->playbackBufferSize) {
  365. printk (KERN_ERR "NM256: Sound playback pointer invalid!\n");
  366. amt = 0;
  367. }
  368. if (amt < card->curPlayPos)
  369. amt = (card->playbackBufferSize - card->curPlayPos) + amt;
  370. else
  371. amt -= card->curPlayPos;
  372. if (card->requested_amt > (amt + NM256_PLAY_WMARK_SIZE)) {
  373. u32 endstop =
  374. card->curPlayPos + card->requested_amt - NM256_PLAY_WMARK_SIZE;
  375. nm256_writePort32 (card, 2, NM_PBUFFER_WMARK, card->abuf1 + endstop);
  376. }
  377. else {
  378. card->curPlayPos += card->requested_amt;
  379. /* Get a new block to write. This will eventually invoke
  380. nm256_write_block () or stopPlay (). */
  381. DMAbuf_outputintr (card->dev_for_play, 1);
  382. }
  383. }
  384. /*
  385. * Read the last-recorded block from the ring buffer, copy it into the
  386. * saved buffer pointer, and invoke DMAuf_inputintr() with the recording
  387. * device.
  388. */
  389. static void
  390. nm256_read_block (struct nm256_info *card)
  391. {
  392. /* Grab the current position of the recording pointer. */
  393. u32 currptr = nm256_readPort32 (card, 2, NM_RBUFFER_CURRP) - card->abuf2;
  394. u32 amtToRead = card->requestedRecAmt;
  395. u32 ringsize = card->recordBufferSize;
  396. if (currptr >= card->recordBufferSize) {
  397. printk (KERN_ERR "NM256: Sound buffer record pointer invalid!\n");
  398. currptr = 0;
  399. }
  400. /*
  401. * This test is probably redundant; we shouldn't be here unless
  402. * it's true.
  403. */
  404. if (card->recording) {
  405. /* If we wrapped around, copy everything from the start of our
  406. recording buffer to the end of the buffer. */
  407. if (currptr < card->curRecPos) {
  408. u32 amt = min (ringsize - card->curRecPos, amtToRead);
  409. nm256_readBuffer8 (card, card->recBuf, 1,
  410. card->abuf2 + card->curRecPos,
  411. amt);
  412. amtToRead -= amt;
  413. card->curRecPos += amt;
  414. card->recBuf += amt;
  415. if (card->curRecPos == ringsize)
  416. card->curRecPos = 0;
  417. }
  418. if ((card->curRecPos < currptr) && (amtToRead > 0)) {
  419. u32 amt = min (currptr - card->curRecPos, amtToRead);
  420. nm256_readBuffer8 (card, card->recBuf, 1,
  421. card->abuf2 + card->curRecPos, amt);
  422. card->curRecPos = ((card->curRecPos + amt) % ringsize);
  423. }
  424. card->recBuf = NULL;
  425. card->requestedRecAmt = 0;
  426. DMAbuf_inputintr (card->dev_for_record);
  427. }
  428. }
  429. /*
  430. * Initialize the hardware.
  431. */
  432. static void
  433. nm256_initHw (struct nm256_info *card)
  434. {
  435. /* Reset everything. */
  436. nm256_writePort8 (card, 2, 0x0, 0x11);
  437. nm256_writePort16 (card, 2, 0x214, 0);
  438. stopRecord (card);
  439. stopPlay (card);
  440. }
  441. /*
  442. * Handle a potential interrupt for the device referred to by DEV_ID.
  443. *
  444. * I don't like the cut-n-paste job here either between the two routines,
  445. * but there are sufficient differences between the two interrupt handlers
  446. * that parameterizing it isn't all that great either. (Could use a macro,
  447. * I suppose...yucky bleah.)
  448. */
  449. static irqreturn_t
  450. nm256_interrupt (int irq, void *dev_id)
  451. {
  452. struct nm256_info *card = (struct nm256_info *)dev_id;
  453. u16 status;
  454. static int badintrcount;
  455. int handled = 0;
  456. if ((card == NULL) || (card->magsig != NM_MAGIC_SIG)) {
  457. printk (KERN_ERR "NM256: Bad card pointer\n");
  458. return IRQ_NONE;
  459. }
  460. status = nm256_readPort16 (card, 2, NM_INT_REG);
  461. /* Not ours. */
  462. if (status == 0) {
  463. if (badintrcount++ > 1000) {
  464. /*
  465. * I'm not sure if the best thing is to stop the card from
  466. * playing or just release the interrupt (after all, we're in
  467. * a bad situation, so doing fancy stuff may not be such a good
  468. * idea).
  469. *
  470. * I worry about the card engine continuing to play noise
  471. * over and over, however--that could become a very
  472. * obnoxious problem. And we know that when this usually
  473. * happens things are fairly safe, it just means the user's
  474. * inserted a PCMCIA card and someone's spamming us with IRQ 9s.
  475. */
  476. handled = 1;
  477. if (card->playing)
  478. stopPlay (card);
  479. if (card->recording)
  480. stopRecord (card);
  481. badintrcount = 0;
  482. }
  483. return IRQ_RETVAL(handled);
  484. }
  485. badintrcount = 0;
  486. /* Rather boring; check for individual interrupts and process them. */
  487. if (status & NM_PLAYBACK_INT) {
  488. handled = 1;
  489. status &= ~NM_PLAYBACK_INT;
  490. NM_ACK_INT (card, NM_PLAYBACK_INT);
  491. if (card->playing)
  492. nm256_get_new_block (card);
  493. }
  494. if (status & NM_RECORD_INT) {
  495. handled = 1;
  496. status &= ~NM_RECORD_INT;
  497. NM_ACK_INT (card, NM_RECORD_INT);
  498. if (card->recording)
  499. nm256_read_block (card);
  500. }
  501. if (status & NM_MISC_INT_1) {
  502. u8 cbyte;
  503. handled = 1;
  504. status &= ~NM_MISC_INT_1;
  505. printk (KERN_ERR "NM256: Got misc interrupt #1\n");
  506. NM_ACK_INT (card, NM_MISC_INT_1);
  507. nm256_writePort16 (card, 2, NM_INT_REG, 0x8000);
  508. cbyte = nm256_readPort8 (card, 2, 0x400);
  509. nm256_writePort8 (card, 2, 0x400, cbyte | 2);
  510. }
  511. if (status & NM_MISC_INT_2) {
  512. u8 cbyte;
  513. handled = 1;
  514. status &= ~NM_MISC_INT_2;
  515. printk (KERN_ERR "NM256: Got misc interrupt #2\n");
  516. NM_ACK_INT (card, NM_MISC_INT_2);
  517. cbyte = nm256_readPort8 (card, 2, 0x400);
  518. nm256_writePort8 (card, 2, 0x400, cbyte & ~2);
  519. }
  520. /* Unknown interrupt. */
  521. if (status) {
  522. handled = 1;
  523. printk (KERN_ERR "NM256: Fire in the hole! Unknown status 0x%x\n",
  524. status);
  525. /* Pray. */
  526. NM_ACK_INT (card, status);
  527. }
  528. return IRQ_RETVAL(handled);
  529. }
  530. /*
  531. * Handle a potential interrupt for the device referred to by DEV_ID.
  532. * This handler is for the 256ZX, and is very similar to the non-ZX
  533. * routine.
  534. */
  535. static irqreturn_t
  536. nm256_interrupt_zx (int irq, void *dev_id)
  537. {
  538. struct nm256_info *card = (struct nm256_info *)dev_id;
  539. u32 status;
  540. static int badintrcount;
  541. int handled = 0;
  542. if ((card == NULL) || (card->magsig != NM_MAGIC_SIG)) {
  543. printk (KERN_ERR "NM256: Bad card pointer\n");
  544. return IRQ_NONE;
  545. }
  546. status = nm256_readPort32 (card, 2, NM_INT_REG);
  547. /* Not ours. */
  548. if (status == 0) {
  549. if (badintrcount++ > 1000) {
  550. printk (KERN_ERR "NM256: Releasing interrupt, over 1000 invalid interrupts\n");
  551. /*
  552. * I'm not sure if the best thing is to stop the card from
  553. * playing or just release the interrupt (after all, we're in
  554. * a bad situation, so doing fancy stuff may not be such a good
  555. * idea).
  556. *
  557. * I worry about the card engine continuing to play noise
  558. * over and over, however--that could become a very
  559. * obnoxious problem. And we know that when this usually
  560. * happens things are fairly safe, it just means the user's
  561. * inserted a PCMCIA card and someone's spamming us with
  562. * IRQ 9s.
  563. */
  564. handled = 1;
  565. if (card->playing)
  566. stopPlay (card);
  567. if (card->recording)
  568. stopRecord (card);
  569. badintrcount = 0;
  570. }
  571. return IRQ_RETVAL(handled);
  572. }
  573. badintrcount = 0;
  574. /* Rather boring; check for individual interrupts and process them. */
  575. if (status & NM2_PLAYBACK_INT) {
  576. handled = 1;
  577. status &= ~NM2_PLAYBACK_INT;
  578. NM2_ACK_INT (card, NM2_PLAYBACK_INT);
  579. if (card->playing)
  580. nm256_get_new_block (card);
  581. }
  582. if (status & NM2_RECORD_INT) {
  583. handled = 1;
  584. status &= ~NM2_RECORD_INT;
  585. NM2_ACK_INT (card, NM2_RECORD_INT);
  586. if (card->recording)
  587. nm256_read_block (card);
  588. }
  589. if (status & NM2_MISC_INT_1) {
  590. u8 cbyte;
  591. handled = 1;
  592. status &= ~NM2_MISC_INT_1;
  593. printk (KERN_ERR "NM256: Got misc interrupt #1\n");
  594. NM2_ACK_INT (card, NM2_MISC_INT_1);
  595. cbyte = nm256_readPort8 (card, 2, 0x400);
  596. nm256_writePort8 (card, 2, 0x400, cbyte | 2);
  597. }
  598. if (status & NM2_MISC_INT_2) {
  599. u8 cbyte;
  600. handled = 1;
  601. status &= ~NM2_MISC_INT_2;
  602. printk (KERN_ERR "NM256: Got misc interrupt #2\n");
  603. NM2_ACK_INT (card, NM2_MISC_INT_2);
  604. cbyte = nm256_readPort8 (card, 2, 0x400);
  605. nm256_writePort8 (card, 2, 0x400, cbyte & ~2);
  606. }
  607. /* Unknown interrupt. */
  608. if (status) {
  609. handled = 1;
  610. printk (KERN_ERR "NM256: Fire in the hole! Unknown status 0x%x\n",
  611. status);
  612. /* Pray. */
  613. NM2_ACK_INT (card, status);
  614. }
  615. return IRQ_RETVAL(handled);
  616. }
  617. /*
  618. * Request our interrupt.
  619. */
  620. static int
  621. nm256_grabInterrupt (struct nm256_info *card)
  622. {
  623. if (card->has_irq++ == 0) {
  624. if (request_irq (card->irq, card->introutine, IRQF_SHARED,
  625. "NM256_audio", card) < 0) {
  626. printk (KERN_ERR "NM256: can't obtain IRQ %d\n", card->irq);
  627. return -1;
  628. }
  629. }
  630. return 0;
  631. }
  632. /*
  633. * Release our interrupt.
  634. */
  635. static int
  636. nm256_releaseInterrupt (struct nm256_info *card)
  637. {
  638. if (card->has_irq <= 0) {
  639. printk (KERN_ERR "nm256: too many calls to releaseInterrupt\n");
  640. return -1;
  641. }
  642. card->has_irq--;
  643. if (card->has_irq == 0) {
  644. free_irq (card->irq, card);
  645. }
  646. return 0;
  647. }
  648. /*
  649. * Waits for the mixer to become ready to be written; returns a zero value
  650. * if it timed out.
  651. */
  652. static int
  653. nm256_isReady (struct ac97_hwint *dev)
  654. {
  655. struct nm256_info *card = (struct nm256_info *)dev->driver_private;
  656. int t2 = 10;
  657. u32 testaddr;
  658. u16 testb;
  659. int done = 0;
  660. if (card->magsig != NM_MAGIC_SIG) {
  661. printk (KERN_ERR "NM256: Bad magic signature in isReady!\n");
  662. return 0;
  663. }
  664. testaddr = card->mixer_status_offset;
  665. testb = card->mixer_status_mask;
  666. /*
  667. * Loop around waiting for the mixer to become ready.
  668. */
  669. while (! done && t2-- > 0) {
  670. if ((nm256_readPort16 (card, 2, testaddr) & testb) == 0)
  671. done = 1;
  672. else
  673. udelay (100);
  674. }
  675. return done;
  676. }
  677. /*
  678. * Return the contents of the AC97 mixer register REG. Returns a positive
  679. * value if successful, or a negative error code.
  680. */
  681. static int
  682. nm256_readAC97Reg (struct ac97_hwint *dev, u8 reg)
  683. {
  684. struct nm256_info *card = (struct nm256_info *)dev->driver_private;
  685. if (card->magsig != NM_MAGIC_SIG) {
  686. printk (KERN_ERR "NM256: Bad magic signature in readAC97Reg!\n");
  687. return -EINVAL;
  688. }
  689. if (reg < 128) {
  690. int res;
  691. nm256_isReady (dev);
  692. res = nm256_readPort16 (card, 2, card->mixer + reg);
  693. /* Magic delay. Bleah yucky. */
  694. udelay (1000);
  695. return res;
  696. }
  697. else
  698. return -EINVAL;
  699. }
  700. /*
  701. * Writes VALUE to AC97 mixer register REG. Returns 0 if successful, or
  702. * a negative error code.
  703. */
  704. static int
  705. nm256_writeAC97Reg (struct ac97_hwint *dev, u8 reg, u16 value)
  706. {
  707. unsigned long flags;
  708. int tries = 2;
  709. int done = 0;
  710. u32 base;
  711. struct nm256_info *card = (struct nm256_info *)dev->driver_private;
  712. if (card->magsig != NM_MAGIC_SIG) {
  713. printk (KERN_ERR "NM256: Bad magic signature in writeAC97Reg!\n");
  714. return -EINVAL;
  715. }
  716. base = card->mixer;
  717. spin_lock_irqsave(&card->lock,flags);
  718. nm256_isReady (dev);
  719. /* Wait for the write to take, too. */
  720. while ((tries-- > 0) && !done) {
  721. nm256_writePort16 (card, 2, base + reg, value);
  722. if (nm256_isReady (dev)) {
  723. done = 1;
  724. break;
  725. }
  726. }
  727. spin_unlock_irqrestore(&card->lock,flags);
  728. udelay (1000);
  729. return ! done;
  730. }
  731. /*
  732. * Initial register values to be written to the AC97 mixer.
  733. * While most of these are identical to the reset values, we do this
  734. * so that we have most of the register contents cached--this avoids
  735. * reading from the mixer directly (which seems to be problematic,
  736. * probably due to ignorance).
  737. */
  738. struct initialValues
  739. {
  740. unsigned short port;
  741. unsigned short value;
  742. };
  743. static struct initialValues nm256_ac97_initial_values[] =
  744. {
  745. { AC97_MASTER_VOL_STEREO, 0x8000 },
  746. { AC97_HEADPHONE_VOL, 0x8000 },
  747. { AC97_MASTER_VOL_MONO, 0x0000 },
  748. { AC97_PCBEEP_VOL, 0x0000 },
  749. { AC97_PHONE_VOL, 0x0008 },
  750. { AC97_MIC_VOL, 0x8000 },
  751. { AC97_LINEIN_VOL, 0x8808 },
  752. { AC97_CD_VOL, 0x8808 },
  753. { AC97_VIDEO_VOL, 0x8808 },
  754. { AC97_AUX_VOL, 0x8808 },
  755. { AC97_PCMOUT_VOL, 0x0808 },
  756. { AC97_RECORD_SELECT, 0x0000 },
  757. { AC97_RECORD_GAIN, 0x0B0B },
  758. { AC97_GENERAL_PURPOSE, 0x0000 },
  759. { 0xffff, 0xffff }
  760. };
  761. /* Initialize the AC97 into a known state. */
  762. static int
  763. nm256_resetAC97 (struct ac97_hwint *dev)
  764. {
  765. struct nm256_info *card = (struct nm256_info *)dev->driver_private;
  766. int x;
  767. if (card->magsig != NM_MAGIC_SIG) {
  768. printk (KERN_ERR "NM256: Bad magic signature in resetAC97!\n");
  769. return -EINVAL;
  770. }
  771. /* Reset the mixer. 'Tis magic! */
  772. nm256_writePort8 (card, 2, 0x6c0, 1);
  773. // nm256_writePort8 (card, 2, 0x6cc, 0x87); /* This crashes Dell latitudes */
  774. nm256_writePort8 (card, 2, 0x6cc, 0x80);
  775. nm256_writePort8 (card, 2, 0x6cc, 0x0);
  776. if (! card->mixer_values_init) {
  777. for (x = 0; nm256_ac97_initial_values[x].port != 0xffff; x++) {
  778. ac97_put_register (dev,
  779. nm256_ac97_initial_values[x].port,
  780. nm256_ac97_initial_values[x].value);
  781. card->mixer_values_init = 1;
  782. }
  783. }
  784. return 0;
  785. }
  786. /*
  787. * We don't do anything particularly special here; it just passes the
  788. * mixer ioctl to the AC97 driver.
  789. */
  790. static int
  791. nm256_default_mixer_ioctl (int dev, unsigned int cmd, void __user *arg)
  792. {
  793. struct nm256_info *card = nm256_find_card_for_mixer (dev);
  794. if (card != NULL)
  795. return ac97_mixer_ioctl (&(card->mdev), cmd, arg);
  796. else
  797. return -ENODEV;
  798. }
  799. static struct mixer_operations nm256_mixer_operations = {
  800. .owner = THIS_MODULE,
  801. .id = "NeoMagic",
  802. .name = "NM256AC97Mixer",
  803. .ioctl = nm256_default_mixer_ioctl
  804. };
  805. /*
  806. * Default settings for the OSS mixer. These are set last, after the
  807. * mixer is initialized.
  808. *
  809. * I "love" C sometimes. Got braces?
  810. */
  811. static struct ac97_mixer_value_list mixer_defaults[] = {
  812. { SOUND_MIXER_VOLUME, { { 85, 85 } } },
  813. { SOUND_MIXER_SPEAKER, { { 100 } } },
  814. { SOUND_MIXER_PCM, { { 65, 65 } } },
  815. { SOUND_MIXER_CD, { { 65, 65 } } },
  816. { -1, { { 0, 0 } } }
  817. };
  818. /* Installs the AC97 mixer into CARD. */
  819. static int __devinit
  820. nm256_install_mixer (struct nm256_info *card)
  821. {
  822. int mixer;
  823. card->mdev.reset_device = nm256_resetAC97;
  824. card->mdev.read_reg = nm256_readAC97Reg;
  825. card->mdev.write_reg = nm256_writeAC97Reg;
  826. card->mdev.driver_private = (void *)card;
  827. if (ac97_init (&(card->mdev)))
  828. return -1;
  829. mixer = sound_alloc_mixerdev();
  830. if (num_mixers >= MAX_MIXER_DEV) {
  831. printk ("NM256 mixer: Unable to alloc mixerdev\n");
  832. return -1;
  833. }
  834. mixer_devs[mixer] = &nm256_mixer_operations;
  835. card->mixer_oss_dev = mixer;
  836. /* Some reasonable default values. */
  837. ac97_set_values (&(card->mdev), mixer_defaults);
  838. printk(KERN_INFO "Initialized AC97 mixer\n");
  839. return 0;
  840. }
  841. /*
  842. * See if the signature left by the NM256 BIOS is intact; if so, we use
  843. * the associated address as the end of our audio buffer in the video
  844. * RAM.
  845. */
  846. static void __devinit
  847. nm256_peek_for_sig (struct nm256_info *card)
  848. {
  849. u32 port1offset
  850. = card->port[0].physaddr + card->port[0].end_offset - 0x0400;
  851. /* The signature is located 1K below the end of video RAM. */
  852. char __iomem *temp = ioremap_nocache (port1offset, 16);
  853. /* Default buffer end is 5120 bytes below the top of RAM. */
  854. u32 default_value = card->port[0].end_offset - 0x1400;
  855. u32 sig;
  856. /* Install the default value first, so we don't have to repeatedly
  857. do it if there is a problem. */
  858. card->port[0].end_offset = default_value;
  859. if (temp == NULL) {
  860. printk (KERN_ERR "NM256: Unable to scan for card signature in video RAM\n");
  861. return;
  862. }
  863. sig = readl (temp);
  864. if ((sig & NM_SIG_MASK) == NM_SIGNATURE) {
  865. u32 pointer = readl (temp + 4);
  866. /*
  867. * If it's obviously invalid, don't use it (the port already has a
  868. * suitable default value set).
  869. */
  870. if (pointer != 0xffffffff)
  871. card->port[0].end_offset = pointer;
  872. printk (KERN_INFO "NM256: Found card signature in video RAM: 0x%x\n",
  873. pointer);
  874. }
  875. iounmap (temp);
  876. }
  877. /*
  878. * Install a driver for the PCI device referenced by PCIDEV.
  879. * VERSTR is a human-readable version string.
  880. */
  881. static int __devinit
  882. nm256_install(struct pci_dev *pcidev, enum nm256rev rev, char *verstr)
  883. {
  884. struct nm256_info *card;
  885. int x;
  886. if (pci_enable_device(pcidev))
  887. return 0;
  888. card = kmalloc (sizeof (struct nm256_info), GFP_KERNEL);
  889. if (card == NULL) {
  890. printk (KERN_ERR "NM256: out of memory!\n");
  891. return 0;
  892. }
  893. card->magsig = NM_MAGIC_SIG;
  894. card->playing = 0;
  895. card->recording = 0;
  896. card->rev = rev;
  897. spin_lock_init(&card->lock);
  898. /* Init the memory port info. */
  899. for (x = 0; x < 2; x++) {
  900. card->port[x].physaddr = pci_resource_start (pcidev, x);
  901. card->port[x].ptr = NULL;
  902. card->port[x].start_offset = 0;
  903. card->port[x].end_offset = 0;
  904. }
  905. /* Port 2 is easy. */
  906. card->port[1].start_offset = 0;
  907. card->port[1].end_offset = NM_PORT2_SIZE;
  908. /* Yuck. But we have to map in port 2 so we can check how much RAM the
  909. card has. */
  910. if (nm256_remap_ports (card)) {
  911. kfree (card);
  912. return 0;
  913. }
  914. /*
  915. * The NM256 has two memory ports. The first port is nothing
  916. * more than a chunk of video RAM, which is used as the I/O ring
  917. * buffer. The second port has the actual juicy stuff (like the
  918. * mixer and the playback engine control registers).
  919. */
  920. if (card->rev == REV_NM256AV) {
  921. /* Ok, try to see if this is a non-AC97 version of the hardware. */
  922. int pval = nm256_readPort16 (card, 2, NM_MIXER_PRESENCE);
  923. if ((pval & NM_PRESENCE_MASK) != NM_PRESENCE_VALUE) {
  924. if (! force_load) {
  925. printk (KERN_ERR "NM256: This doesn't look to me like the AC97-compatible version.\n");
  926. printk (KERN_ERR " You can force the driver to load by passing in the module\n");
  927. printk (KERN_ERR " parameter:\n");
  928. printk (KERN_ERR " force_load = 1\n");
  929. printk (KERN_ERR "\n");
  930. printk (KERN_ERR " More likely, you should be using the appropriate SB-16 or\n");
  931. printk (KERN_ERR " CS4232 driver instead. (If your BIOS has settings for\n");
  932. printk (KERN_ERR " IRQ and/or DMA for the sound card, this is *not* the correct\n");
  933. printk (KERN_ERR " driver to use.)\n");
  934. nm256_release_ports (card);
  935. kfree (card);
  936. return 0;
  937. }
  938. else {
  939. printk (KERN_INFO "NM256: Forcing driver load as per user request.\n");
  940. }
  941. }
  942. else {
  943. /* printk (KERN_INFO "NM256: Congratulations. You're not running Eunice.\n")*/;
  944. }
  945. card->port[0].end_offset = 2560 * 1024;
  946. card->introutine = nm256_interrupt;
  947. card->mixer_status_offset = NM_MIXER_STATUS_OFFSET;
  948. card->mixer_status_mask = NM_MIXER_READY_MASK;
  949. }
  950. else {
  951. /* Not sure if there is any relevant detect for the ZX or not. */
  952. if (nm256_readPort8 (card, 2, 0xa0b) != 0)
  953. card->port[0].end_offset = 6144 * 1024;
  954. else
  955. card->port[0].end_offset = 4096 * 1024;
  956. card->introutine = nm256_interrupt_zx;
  957. card->mixer_status_offset = NM2_MIXER_STATUS_OFFSET;
  958. card->mixer_status_mask = NM2_MIXER_READY_MASK;
  959. }
  960. if (buffertop >= 98304 && buffertop < card->port[0].end_offset)
  961. card->port[0].end_offset = buffertop;
  962. else
  963. nm256_peek_for_sig (card);
  964. card->port[0].start_offset = card->port[0].end_offset - 98304;
  965. printk (KERN_INFO "NM256: Mapping port 1 from 0x%x - 0x%x\n",
  966. card->port[0].start_offset, card->port[0].end_offset);
  967. if (nm256_remap_ports (card)) {
  968. kfree (card);
  969. return 0;
  970. }
  971. /* See if we can get the interrupt. */
  972. card->irq = pcidev->irq;
  973. card->has_irq = 0;
  974. if (nm256_grabInterrupt (card) != 0) {
  975. nm256_release_ports (card);
  976. kfree (card);
  977. return 0;
  978. }
  979. nm256_releaseInterrupt (card);
  980. /*
  981. * Init the board.
  982. */
  983. card->playbackBufferSize = 16384;
  984. card->recordBufferSize = 16384;
  985. card->coeffBuf = card->port[0].end_offset - NM_MAX_COEFFICIENT;
  986. card->abuf2 = card->coeffBuf - card->recordBufferSize;
  987. card->abuf1 = card->abuf2 - card->playbackBufferSize;
  988. card->allCoeffBuf = card->abuf2 - (NM_TOTAL_COEFF_COUNT * 4);
  989. /* Fixed setting. */
  990. card->mixer = NM_MIXER_OFFSET;
  991. card->mixer_values_init = 0;
  992. card->is_open_play = 0;
  993. card->is_open_record = 0;
  994. card->coeffsCurrent = 0;
  995. card->opencnt[0] = 0; card->opencnt[1] = 0;
  996. /* Reasonable default settings, but largely unnecessary. */
  997. for (x = 0; x < 2; x++) {
  998. card->sinfo[x].bits = 8;
  999. card->sinfo[x].stereo = 0;
  1000. card->sinfo[x].samplerate = 8000;
  1001. }
  1002. nm256_initHw (card);
  1003. for (x = 0; x < 2; x++) {
  1004. if ((card->dev[x] =
  1005. sound_install_audiodrv(AUDIO_DRIVER_VERSION,
  1006. "NM256", &nm256_audio_driver,
  1007. sizeof(struct audio_driver),
  1008. DMA_NODMA, AFMT_U8 | AFMT_S16_LE,
  1009. NULL, -1, -1)) >= 0) {
  1010. /* 1K minimum buffer size. */
  1011. audio_devs[card->dev[x]]->min_fragment = 10;
  1012. /* Maximum of 8K buffer size. */
  1013. audio_devs[card->dev[x]]->max_fragment = 13;
  1014. }
  1015. else {
  1016. printk(KERN_ERR "NM256: Too many PCM devices available\n");
  1017. nm256_release_ports (card);
  1018. kfree (card);
  1019. return 0;
  1020. }
  1021. }
  1022. pci_set_drvdata(pcidev,card);
  1023. /* Insert the card in the list. */
  1024. card->next_card = nmcard_list;
  1025. nmcard_list = card;
  1026. printk(KERN_INFO "Initialized NeoMagic %s audio in PCI native mode\n",
  1027. verstr);
  1028. /*
  1029. * And our mixer. (We should allow support for other mixers, maybe.)
  1030. */
  1031. nm256_install_mixer (card);
  1032. return 1;
  1033. }
  1034. static int __devinit
  1035. nm256_probe(struct pci_dev *pcidev,const struct pci_device_id *pciid)
  1036. {
  1037. if (pcidev->device == PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO)
  1038. return nm256_install(pcidev, REV_NM256AV, "256AV");
  1039. if (pcidev->device == PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO)
  1040. return nm256_install(pcidev, REV_NM256ZX, "256ZX");
  1041. if (pcidev->device == PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO)
  1042. return nm256_install(pcidev, REV_NM256ZX, "256XL+");
  1043. return -1; /* should not come here ... */
  1044. }
  1045. static void __devinit
  1046. nm256_remove(struct pci_dev *pcidev) {
  1047. struct nm256_info *xcard = pci_get_drvdata(pcidev);
  1048. struct nm256_info *card,*next_card = NULL;
  1049. for (card = nmcard_list; card != NULL; card = next_card) {
  1050. next_card = card->next_card;
  1051. if (card == xcard) {
  1052. stopPlay (card);
  1053. stopRecord (card);
  1054. if (card->has_irq)
  1055. free_irq (card->irq, card);
  1056. nm256_release_ports (card);
  1057. sound_unload_mixerdev (card->mixer_oss_dev);
  1058. sound_unload_audiodev (card->dev[0]);
  1059. sound_unload_audiodev (card->dev[1]);
  1060. kfree (card);
  1061. break;
  1062. }
  1063. }
  1064. if (nmcard_list == card)
  1065. nmcard_list = next_card;
  1066. }
  1067. /*
  1068. * Open the device
  1069. *
  1070. * DEV - device
  1071. * MODE - mode to open device (logical OR of OPEN_READ and OPEN_WRITE)
  1072. *
  1073. * Called when opening the DMAbuf (dmabuf.c:259)
  1074. */
  1075. static int
  1076. nm256_audio_open(int dev, int mode)
  1077. {
  1078. struct nm256_info *card = nm256_find_card (dev);
  1079. int w;
  1080. if (card == NULL)
  1081. return -ENODEV;
  1082. if (card->dev[0] == dev)
  1083. w = 0;
  1084. else if (card->dev[1] == dev)
  1085. w = 1;
  1086. else
  1087. return -ENODEV;
  1088. if (card->opencnt[w] > 0)
  1089. return -EBUSY;
  1090. /* No bits set? Huh? */
  1091. if (! ((mode & OPEN_READ) || (mode & OPEN_WRITE)))
  1092. return -EIO;
  1093. /*
  1094. * If it's open for both read and write, and the card's currently
  1095. * being read or written to, then do the opposite of what has
  1096. * already been done. Otherwise, don't specify any mode until the
  1097. * user actually tries to do I/O. (Some programs open the device
  1098. * for both read and write, but only actually do reading or writing.)
  1099. */
  1100. if ((mode & OPEN_WRITE) && (mode & OPEN_READ)) {
  1101. if (card->is_open_play)
  1102. mode = OPEN_WRITE;
  1103. else if (card->is_open_record)
  1104. mode = OPEN_READ;
  1105. else mode = 0;
  1106. }
  1107. if (mode & OPEN_WRITE) {
  1108. if (card->is_open_play == 0) {
  1109. card->dev_for_play = dev;
  1110. card->is_open_play = 1;
  1111. }
  1112. else
  1113. return -EBUSY;
  1114. }
  1115. if (mode & OPEN_READ) {
  1116. if (card->is_open_record == 0) {
  1117. card->dev_for_record = dev;
  1118. card->is_open_record = 1;
  1119. }
  1120. else
  1121. return -EBUSY;
  1122. }
  1123. card->opencnt[w]++;
  1124. return 0;
  1125. }
  1126. /*
  1127. * Close the device
  1128. *
  1129. * DEV - device
  1130. *
  1131. * Called when closing the DMAbuf (dmabuf.c:477)
  1132. * after halt_xfer
  1133. */
  1134. static void
  1135. nm256_audio_close(int dev)
  1136. {
  1137. struct nm256_info *card = nm256_find_card (dev);
  1138. if (card != NULL) {
  1139. int w;
  1140. if (card->dev[0] == dev)
  1141. w = 0;
  1142. else if (card->dev[1] == dev)
  1143. w = 1;
  1144. else
  1145. return;
  1146. card->opencnt[w]--;
  1147. if (card->opencnt[w] <= 0) {
  1148. card->opencnt[w] = 0;
  1149. if (card->dev_for_play == dev) {
  1150. stopPlay (card);
  1151. card->is_open_play = 0;
  1152. card->dev_for_play = -1;
  1153. }
  1154. if (card->dev_for_record == dev) {
  1155. stopRecord (card);
  1156. card->is_open_record = 0;
  1157. card->dev_for_record = -1;
  1158. }
  1159. }
  1160. }
  1161. }
  1162. /* Standard ioctl handler. */
  1163. static int
  1164. nm256_audio_ioctl(int dev, unsigned int cmd, void __user *arg)
  1165. {
  1166. int ret;
  1167. u32 oldinfo;
  1168. int w;
  1169. struct nm256_info *card = nm256_find_card (dev);
  1170. if (card == NULL)
  1171. return -ENODEV;
  1172. if (dev == card->dev[0])
  1173. w = 0;
  1174. else
  1175. w = 1;
  1176. /*
  1177. * The code here is messy. There are probably better ways to do
  1178. * it. (It should be possible to handle it the same way the AC97 mixer
  1179. * is done.)
  1180. */
  1181. switch (cmd)
  1182. {
  1183. case SOUND_PCM_WRITE_RATE:
  1184. if (get_user(ret, (int __user *) arg))
  1185. return -EFAULT;
  1186. if (ret != 0) {
  1187. oldinfo = card->sinfo[w].samplerate;
  1188. card->sinfo[w].samplerate = ret;
  1189. ret = nm256_setInfo(dev, card);
  1190. if (ret != 0)
  1191. card->sinfo[w].samplerate = oldinfo;
  1192. }
  1193. if (ret == 0)
  1194. ret = card->sinfo[w].samplerate;
  1195. break;
  1196. case SOUND_PCM_READ_RATE:
  1197. ret = card->sinfo[w].samplerate;
  1198. break;
  1199. case SNDCTL_DSP_STEREO:
  1200. if (get_user(ret, (int __user *) arg))
  1201. return -EFAULT;
  1202. card->sinfo[w].stereo = ret ? 1 : 0;
  1203. ret = nm256_setInfo (dev, card);
  1204. if (ret == 0)
  1205. ret = card->sinfo[w].stereo;
  1206. break;
  1207. case SOUND_PCM_WRITE_CHANNELS:
  1208. if (get_user(ret, (int __user *) arg))
  1209. return -EFAULT;
  1210. if (ret < 1 || ret > 3)
  1211. ret = card->sinfo[w].stereo + 1;
  1212. else {
  1213. card->sinfo[w].stereo = ret - 1;
  1214. ret = nm256_setInfo (dev, card);
  1215. if (ret == 0)
  1216. ret = card->sinfo[w].stereo + 1;
  1217. }
  1218. break;
  1219. case SOUND_PCM_READ_CHANNELS:
  1220. ret = card->sinfo[w].stereo + 1;
  1221. break;
  1222. case SNDCTL_DSP_SETFMT:
  1223. if (get_user(ret, (int __user *) arg))
  1224. return -EFAULT;
  1225. if (ret != 0) {
  1226. oldinfo = card->sinfo[w].bits;
  1227. card->sinfo[w].bits = ret;
  1228. ret = nm256_setInfo (dev, card);
  1229. if (ret != 0)
  1230. card->sinfo[w].bits = oldinfo;
  1231. }
  1232. if (ret == 0)
  1233. ret = card->sinfo[w].bits;
  1234. break;
  1235. case SOUND_PCM_READ_BITS:
  1236. ret = card->sinfo[w].bits;
  1237. break;
  1238. default:
  1239. return -EINVAL;
  1240. }
  1241. return put_user(ret, (int __user *) arg);
  1242. }
  1243. /*
  1244. * Given the sound device DEV and an associated physical buffer PHYSBUF,
  1245. * return a pointer to the actual buffer in kernel space.
  1246. *
  1247. * This routine should exist as part of the soundcore routines.
  1248. */
  1249. static char *
  1250. nm256_getDMAbuffer (int dev, unsigned long physbuf)
  1251. {
  1252. struct audio_operations *adev = audio_devs[dev];
  1253. struct dma_buffparms *dmap = adev->dmap_out;
  1254. char *dma_start =
  1255. (char *)(physbuf - (unsigned long)dmap->raw_buf_phys
  1256. + (unsigned long)dmap->raw_buf);
  1257. return dma_start;
  1258. }
  1259. /*
  1260. * Output a block to sound device
  1261. *
  1262. * dev - device number
  1263. * buf - physical address of buffer
  1264. * total_count - total byte count in buffer
  1265. * intrflag - set if this has been called from an interrupt
  1266. * (via DMAbuf_outputintr)
  1267. * restart_dma - set if engine needs to be re-initialised
  1268. *
  1269. * Called when:
  1270. * 1. Starting output (dmabuf.c:1327)
  1271. * 2. (dmabuf.c:1504)
  1272. * 3. A new buffer needs to be sent to the device (dmabuf.c:1579)
  1273. */
  1274. static void
  1275. nm256_audio_output_block(int dev, unsigned long physbuf,
  1276. int total_count, int intrflag)
  1277. {
  1278. struct nm256_info *card = nm256_find_card (dev);
  1279. if (card != NULL) {
  1280. char *dma_buf = nm256_getDMAbuffer (dev, physbuf);
  1281. card->is_open_play = 1;
  1282. card->dev_for_play = dev;
  1283. nm256_write_block (card, dma_buf, total_count);
  1284. }
  1285. }
  1286. /* Ditto, but do recording instead. */
  1287. static void
  1288. nm256_audio_start_input(int dev, unsigned long physbuf, int count,
  1289. int intrflag)
  1290. {
  1291. struct nm256_info *card = nm256_find_card (dev);
  1292. if (card != NULL) {
  1293. char *dma_buf = nm256_getDMAbuffer (dev, physbuf);
  1294. card->is_open_record = 1;
  1295. card->dev_for_record = dev;
  1296. nm256_startRecording (card, dma_buf, count);
  1297. }
  1298. }
  1299. /*
  1300. * Prepare for inputting samples to DEV.
  1301. * Each requested buffer will be BSIZE byes long, with a total of
  1302. * BCOUNT buffers.
  1303. */
  1304. static int
  1305. nm256_audio_prepare_for_input(int dev, int bsize, int bcount)
  1306. {
  1307. struct nm256_info *card = nm256_find_card (dev);
  1308. if (card == NULL)
  1309. return -ENODEV;
  1310. if (card->is_open_record && card->dev_for_record != dev)
  1311. return -EBUSY;
  1312. audio_devs[dev]->dmap_in->flags |= DMA_NODMA;
  1313. return 0;
  1314. }
  1315. /*
  1316. * Prepare for outputting samples to `dev'
  1317. *
  1318. * Each buffer that will be passed will be `bsize' bytes long,
  1319. * with a total of `bcount' buffers.
  1320. *
  1321. * Called when:
  1322. * 1. A trigger enables audio output (dmabuf.c:978)
  1323. * 2. We get a write buffer without dma_mode setup (dmabuf.c:1152)
  1324. * 3. We restart a transfer (dmabuf.c:1324)
  1325. */
  1326. static int
  1327. nm256_audio_prepare_for_output(int dev, int bsize, int bcount)
  1328. {
  1329. struct nm256_info *card = nm256_find_card (dev);
  1330. if (card == NULL)
  1331. return -ENODEV;
  1332. if (card->is_open_play && card->dev_for_play != dev)
  1333. return -EBUSY;
  1334. audio_devs[dev]->dmap_out->flags |= DMA_NODMA;
  1335. return 0;
  1336. }
  1337. /* Stop the current operations associated with DEV. */
  1338. static void
  1339. nm256_audio_reset(int dev)
  1340. {
  1341. struct nm256_info *card = nm256_find_card (dev);
  1342. if (card != NULL) {
  1343. if (card->dev_for_play == dev)
  1344. stopPlay (card);
  1345. if (card->dev_for_record == dev)
  1346. stopRecord (card);
  1347. }
  1348. }
  1349. static int
  1350. nm256_audio_local_qlen(int dev)
  1351. {
  1352. return 0;
  1353. }
  1354. static struct audio_driver nm256_audio_driver =
  1355. {
  1356. .owner = THIS_MODULE,
  1357. .open = nm256_audio_open,
  1358. .close = nm256_audio_close,
  1359. .output_block = nm256_audio_output_block,
  1360. .start_input = nm256_audio_start_input,
  1361. .ioctl = nm256_audio_ioctl,
  1362. .prepare_for_input = nm256_audio_prepare_for_input,
  1363. .prepare_for_output = nm256_audio_prepare_for_output,
  1364. .halt_io = nm256_audio_reset,
  1365. .local_qlen = nm256_audio_local_qlen,
  1366. };
  1367. static struct pci_device_id nm256_pci_tbl[] = {
  1368. {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO,
  1369. PCI_ANY_ID, PCI_ANY_ID, 0, 0},
  1370. {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO,
  1371. PCI_ANY_ID, PCI_ANY_ID, 0, 0},
  1372. {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO,
  1373. PCI_ANY_ID, PCI_ANY_ID, 0, 0},
  1374. {0,}
  1375. };
  1376. MODULE_DEVICE_TABLE(pci, nm256_pci_tbl);
  1377. MODULE_LICENSE("GPL");
  1378. static struct pci_driver nm256_pci_driver = {
  1379. .name = "nm256_audio",
  1380. .id_table = nm256_pci_tbl,
  1381. .probe = nm256_probe,
  1382. .remove = nm256_remove,
  1383. };
  1384. module_param(usecache, bool, 0);
  1385. module_param(buffertop, int, 0);
  1386. module_param(nm256_debug, bool, 0644);
  1387. module_param(force_load, bool, 0);
  1388. static int __init do_init_nm256(void)
  1389. {
  1390. printk (KERN_INFO "NeoMagic 256AV/256ZX audio driver, version 1.1p\n");
  1391. return pci_register_driver(&nm256_pci_driver);
  1392. }
  1393. static void __exit cleanup_nm256 (void)
  1394. {
  1395. pci_unregister_driver(&nm256_pci_driver);
  1396. }
  1397. module_init(do_init_nm256);
  1398. module_exit(cleanup_nm256);
  1399. /*
  1400. * Local variables:
  1401. * c-basic-offset: 4
  1402. * End:
  1403. */