nm256_audio.c 43 KB

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