nm256_audio.c 43 KB

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