sb_common.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. /*
  2. * sound/oss/sb_common.c
  3. *
  4. * Common routines for Sound Blaster compatible cards.
  5. *
  6. *
  7. * Copyright (C) by Hannu Savolainen 1993-1997
  8. *
  9. * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  10. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  11. * for more info.
  12. *
  13. *
  14. * Daniel J. Rodriksson: Modified sbintr to handle 8 and 16 bit interrupts
  15. * for full duplex support ( only sb16 by now )
  16. * Rolf Fokkens: Added (BETA?) support for ES1887 chips.
  17. * (fokkensr@vertis.nl) Which means: You can adjust the recording levels.
  18. *
  19. * 2000/01/18 - separated sb_card and sb_common -
  20. * Jeff Garzik <jgarzik@pobox.com>
  21. *
  22. * 2000/09/18 - got rid of attach_uart401
  23. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  24. *
  25. * 2001/01/26 - replaced CLI/STI with spinlocks
  26. * Chris Rankin <rankinc@zipworld.com.au>
  27. */
  28. #include <linux/init.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/module.h>
  31. #include <linux/delay.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/slab.h>
  34. #include "sound_config.h"
  35. #include "sound_firmware.h"
  36. #include "mpu401.h"
  37. #include "sb_mixer.h"
  38. #include "sb.h"
  39. #include "sb_ess.h"
  40. /*
  41. * global module flag
  42. */
  43. int sb_be_quiet;
  44. static sb_devc *detected_devc; /* For communication from probe to init */
  45. static sb_devc *last_devc; /* For MPU401 initialization */
  46. static unsigned char jazz_irq_bits[] = {
  47. 0, 0, 2, 3, 0, 1, 0, 4, 0, 2, 5, 0, 0, 0, 0, 6
  48. };
  49. static unsigned char jazz_dma_bits[] = {
  50. 0, 1, 0, 2, 0, 3, 0, 4
  51. };
  52. void *smw_free;
  53. /*
  54. * Jazz16 chipset specific control variables
  55. */
  56. static int jazz16_base; /* Not detected */
  57. static unsigned char jazz16_bits; /* I/O relocation bits */
  58. static DEFINE_SPINLOCK(jazz16_lock);
  59. /*
  60. * Logitech Soundman Wave specific initialization code
  61. */
  62. #ifdef SMW_MIDI0001_INCLUDED
  63. #include "smw-midi0001.h"
  64. #else
  65. static unsigned char *smw_ucode;
  66. static int smw_ucodeLen;
  67. #endif
  68. static sb_devc *last_sb; /* Last sb loaded */
  69. int sb_dsp_command(sb_devc * devc, unsigned char val)
  70. {
  71. int i;
  72. unsigned long limit;
  73. limit = jiffies + HZ / 10; /* Timeout */
  74. /*
  75. * Note! the i<500000 is an emergency exit. The sb_dsp_command() is sometimes
  76. * called while interrupts are disabled. This means that the timer is
  77. * disabled also. However the timeout situation is a abnormal condition.
  78. * Normally the DSP should be ready to accept commands after just couple of
  79. * loops.
  80. */
  81. for (i = 0; i < 500000 && (limit-jiffies)>0; i++)
  82. {
  83. if ((inb(DSP_STATUS) & 0x80) == 0)
  84. {
  85. outb((val), DSP_COMMAND);
  86. return 1;
  87. }
  88. }
  89. printk(KERN_WARNING "Sound Blaster: DSP command(%x) timeout.\n", val);
  90. return 0;
  91. }
  92. int sb_dsp_get_byte(sb_devc * devc)
  93. {
  94. int i;
  95. for (i = 1000; i; i--)
  96. {
  97. if (inb(DSP_DATA_AVAIL) & 0x80)
  98. return inb(DSP_READ);
  99. }
  100. return 0xffff;
  101. }
  102. static void sb_intr (sb_devc *devc)
  103. {
  104. int status;
  105. unsigned char src = 0xff;
  106. if (devc->model == MDL_SB16)
  107. {
  108. src = sb_getmixer(devc, IRQ_STAT); /* Interrupt source register */
  109. if (src & 4) /* MPU401 interrupt */
  110. if(devc->midi_irq_cookie)
  111. uart401intr(devc->irq, devc->midi_irq_cookie);
  112. if (!(src & 3))
  113. return; /* Not a DSP interrupt */
  114. }
  115. if (devc->intr_active && (!devc->fullduplex || (src & 0x01)))
  116. {
  117. switch (devc->irq_mode)
  118. {
  119. case IMODE_OUTPUT:
  120. DMAbuf_outputintr(devc->dev, 1);
  121. break;
  122. case IMODE_INPUT:
  123. DMAbuf_inputintr(devc->dev);
  124. break;
  125. case IMODE_INIT:
  126. break;
  127. case IMODE_MIDI:
  128. sb_midi_interrupt(devc);
  129. break;
  130. default:
  131. /* printk(KERN_WARNING "Sound Blaster: Unexpected interrupt\n"); */
  132. ;
  133. }
  134. }
  135. else if (devc->intr_active_16 && (src & 0x02))
  136. {
  137. switch (devc->irq_mode_16)
  138. {
  139. case IMODE_OUTPUT:
  140. DMAbuf_outputintr(devc->dev, 1);
  141. break;
  142. case IMODE_INPUT:
  143. DMAbuf_inputintr(devc->dev);
  144. break;
  145. case IMODE_INIT:
  146. break;
  147. default:
  148. /* printk(KERN_WARNING "Sound Blaster: Unexpected interrupt\n"); */
  149. ;
  150. }
  151. }
  152. /*
  153. * Acknowledge interrupts
  154. */
  155. if (src & 0x01)
  156. status = inb(DSP_DATA_AVAIL);
  157. if (devc->model == MDL_SB16 && src & 0x02)
  158. status = inb(DSP_DATA_AVL16);
  159. }
  160. static void pci_intr(sb_devc *devc)
  161. {
  162. int src = inb(devc->pcibase+0x1A);
  163. src&=3;
  164. if(src)
  165. sb_intr(devc);
  166. }
  167. static irqreturn_t sbintr(int irq, void *dev_id)
  168. {
  169. sb_devc *devc = dev_id;
  170. devc->irq_ok = 1;
  171. switch (devc->model) {
  172. case MDL_ESSPCI:
  173. pci_intr (devc);
  174. break;
  175. case MDL_ESS:
  176. ess_intr (devc);
  177. break;
  178. default:
  179. sb_intr (devc);
  180. break;
  181. }
  182. return IRQ_HANDLED;
  183. }
  184. int sb_dsp_reset(sb_devc * devc)
  185. {
  186. int loopc;
  187. if (devc->model == MDL_ESS) return ess_dsp_reset (devc);
  188. /* This is only for non-ESS chips */
  189. outb(1, DSP_RESET);
  190. udelay(10);
  191. outb(0, DSP_RESET);
  192. udelay(30);
  193. for (loopc = 0; loopc < 1000 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++);
  194. if (inb(DSP_READ) != 0xAA)
  195. {
  196. DDB(printk("sb: No response to RESET\n"));
  197. return 0; /* Sorry */
  198. }
  199. return 1;
  200. }
  201. static void dsp_get_vers(sb_devc * devc)
  202. {
  203. int i;
  204. unsigned long flags;
  205. DDB(printk("Entered dsp_get_vers()\n"));
  206. spin_lock_irqsave(&devc->lock, flags);
  207. devc->major = devc->minor = 0;
  208. sb_dsp_command(devc, 0xe1); /* Get version */
  209. for (i = 100000; i; i--)
  210. {
  211. if (inb(DSP_DATA_AVAIL) & 0x80)
  212. {
  213. if (devc->major == 0)
  214. devc->major = inb(DSP_READ);
  215. else
  216. {
  217. devc->minor = inb(DSP_READ);
  218. break;
  219. }
  220. }
  221. }
  222. spin_unlock_irqrestore(&devc->lock, flags);
  223. DDB(printk("DSP version %d.%02d\n", devc->major, devc->minor));
  224. }
  225. static int sb16_set_dma_hw(sb_devc * devc)
  226. {
  227. int bits;
  228. if (devc->dma8 != 0 && devc->dma8 != 1 && devc->dma8 != 3)
  229. {
  230. printk(KERN_ERR "SB16: Invalid 8 bit DMA (%d)\n", devc->dma8);
  231. return 0;
  232. }
  233. bits = (1 << devc->dma8);
  234. if (devc->dma16 >= 5 && devc->dma16 <= 7)
  235. bits |= (1 << devc->dma16);
  236. sb_setmixer(devc, DMA_NR, bits);
  237. return 1;
  238. }
  239. static void sb16_set_mpu_port(sb_devc * devc, struct address_info *hw_config)
  240. {
  241. /*
  242. * This routine initializes new MIDI port setup register of SB Vibra (CT2502).
  243. */
  244. unsigned char bits = sb_getmixer(devc, 0x84) & ~0x06;
  245. switch (hw_config->io_base)
  246. {
  247. case 0x300:
  248. sb_setmixer(devc, 0x84, bits | 0x04);
  249. break;
  250. case 0x330:
  251. sb_setmixer(devc, 0x84, bits | 0x00);
  252. break;
  253. default:
  254. sb_setmixer(devc, 0x84, bits | 0x02); /* Disable MPU */
  255. printk(KERN_ERR "SB16: Invalid MIDI I/O port %x\n", hw_config->io_base);
  256. }
  257. }
  258. static int sb16_set_irq_hw(sb_devc * devc, int level)
  259. {
  260. int ival;
  261. switch (level)
  262. {
  263. case 5:
  264. ival = 2;
  265. break;
  266. case 7:
  267. ival = 4;
  268. break;
  269. case 9:
  270. ival = 1;
  271. break;
  272. case 10:
  273. ival = 8;
  274. break;
  275. default:
  276. printk(KERN_ERR "SB16: Invalid IRQ%d\n", level);
  277. return 0;
  278. }
  279. sb_setmixer(devc, IRQ_NR, ival);
  280. return 1;
  281. }
  282. static void relocate_Jazz16(sb_devc * devc, struct address_info *hw_config)
  283. {
  284. unsigned char bits = 0;
  285. unsigned long flags;
  286. if (jazz16_base != 0 && jazz16_base != hw_config->io_base)
  287. return;
  288. switch (hw_config->io_base)
  289. {
  290. case 0x220:
  291. bits = 1;
  292. break;
  293. case 0x240:
  294. bits = 2;
  295. break;
  296. case 0x260:
  297. bits = 3;
  298. break;
  299. default:
  300. return;
  301. }
  302. bits = jazz16_bits = bits << 5;
  303. jazz16_base = hw_config->io_base;
  304. /*
  305. * Magic wake up sequence by writing to 0x201 (aka Joystick port)
  306. */
  307. spin_lock_irqsave(&jazz16_lock, flags);
  308. outb((0xAF), 0x201);
  309. outb((0x50), 0x201);
  310. outb((bits), 0x201);
  311. spin_unlock_irqrestore(&jazz16_lock, flags);
  312. }
  313. static int init_Jazz16(sb_devc * devc, struct address_info *hw_config)
  314. {
  315. char name[100];
  316. /*
  317. * First try to check that the card has Jazz16 chip. It identifies itself
  318. * by returning 0x12 as response to DSP command 0xfa.
  319. */
  320. if (!sb_dsp_command(devc, 0xfa))
  321. return 0;
  322. if (sb_dsp_get_byte(devc) != 0x12)
  323. return 0;
  324. /*
  325. * OK so far. Now configure the IRQ and DMA channel used by the card.
  326. */
  327. if (hw_config->irq < 1 || hw_config->irq > 15 || jazz_irq_bits[hw_config->irq] == 0)
  328. {
  329. printk(KERN_ERR "Jazz16: Invalid interrupt (IRQ%d)\n", hw_config->irq);
  330. return 0;
  331. }
  332. if (hw_config->dma < 0 || hw_config->dma > 3 || jazz_dma_bits[hw_config->dma] == 0)
  333. {
  334. printk(KERN_ERR "Jazz16: Invalid 8 bit DMA (DMA%d)\n", hw_config->dma);
  335. return 0;
  336. }
  337. if (hw_config->dma2 < 0)
  338. {
  339. printk(KERN_ERR "Jazz16: No 16 bit DMA channel defined\n");
  340. return 0;
  341. }
  342. if (hw_config->dma2 < 5 || hw_config->dma2 > 7 || jazz_dma_bits[hw_config->dma2] == 0)
  343. {
  344. printk(KERN_ERR "Jazz16: Invalid 16 bit DMA (DMA%d)\n", hw_config->dma2);
  345. return 0;
  346. }
  347. devc->dma16 = hw_config->dma2;
  348. if (!sb_dsp_command(devc, 0xfb))
  349. return 0;
  350. if (!sb_dsp_command(devc, jazz_dma_bits[hw_config->dma] |
  351. (jazz_dma_bits[hw_config->dma2] << 4)))
  352. return 0;
  353. if (!sb_dsp_command(devc, jazz_irq_bits[hw_config->irq]))
  354. return 0;
  355. /*
  356. * Now we have configured a standard Jazz16 device.
  357. */
  358. devc->model = MDL_JAZZ;
  359. strcpy(name, "Jazz16");
  360. hw_config->name = "Jazz16";
  361. devc->caps |= SB_NO_MIDI;
  362. return 1;
  363. }
  364. static void relocate_ess1688(sb_devc * devc)
  365. {
  366. unsigned char bits;
  367. switch (devc->base)
  368. {
  369. case 0x220:
  370. bits = 0x04;
  371. break;
  372. case 0x230:
  373. bits = 0x05;
  374. break;
  375. case 0x240:
  376. bits = 0x06;
  377. break;
  378. case 0x250:
  379. bits = 0x07;
  380. break;
  381. default:
  382. return; /* Wrong port */
  383. }
  384. DDB(printk("Doing ESS1688 address selection\n"));
  385. /*
  386. * ES1688 supports two alternative ways for software address config.
  387. * First try the so called Read-Sequence-Key method.
  388. */
  389. /* Reset the sequence logic */
  390. inb(0x229);
  391. inb(0x229);
  392. inb(0x229);
  393. /* Perform the read sequence */
  394. inb(0x22b);
  395. inb(0x229);
  396. inb(0x22b);
  397. inb(0x229);
  398. inb(0x229);
  399. inb(0x22b);
  400. inb(0x229);
  401. /* Select the base address by reading from it. Then probe using the port. */
  402. inb(devc->base);
  403. if (sb_dsp_reset(devc)) /* Bingo */
  404. return;
  405. #if 0 /* This causes system lockups (Nokia 386/25 at least) */
  406. /*
  407. * The last resort is the system control register method.
  408. */
  409. outb((0x00), 0xfb); /* 0xFB is the unlock register */
  410. outb((0x00), 0xe0); /* Select index 0 */
  411. outb((bits), 0xe1); /* Write the config bits */
  412. outb((0x00), 0xf9); /* 0xFB is the lock register */
  413. #endif
  414. }
  415. int sb_dsp_detect(struct address_info *hw_config, int pci, int pciio, struct sb_module_options *sbmo)
  416. {
  417. sb_devc sb_info;
  418. sb_devc *devc = &sb_info;
  419. memset((char *) &sb_info, 0, sizeof(sb_info)); /* Zero everything */
  420. /* Copy module options in place */
  421. if(sbmo) memcpy(&devc->sbmo, sbmo, sizeof(struct sb_module_options));
  422. sb_info.my_mididev = -1;
  423. sb_info.my_mixerdev = -1;
  424. sb_info.dev = -1;
  425. /*
  426. * Initialize variables
  427. */
  428. DDB(printk("sb_dsp_detect(%x) entered\n", hw_config->io_base));
  429. spin_lock_init(&devc->lock);
  430. devc->type = hw_config->card_subtype;
  431. devc->base = hw_config->io_base;
  432. devc->irq = hw_config->irq;
  433. devc->dma8 = hw_config->dma;
  434. devc->dma16 = -1;
  435. devc->pcibase = pciio;
  436. if(pci == SB_PCI_ESSMAESTRO)
  437. {
  438. devc->model = MDL_ESSPCI;
  439. devc->caps |= SB_PCI_IRQ;
  440. hw_config->driver_use_1 |= SB_PCI_IRQ;
  441. hw_config->card_subtype = MDL_ESSPCI;
  442. }
  443. if(pci == SB_PCI_YAMAHA)
  444. {
  445. devc->model = MDL_YMPCI;
  446. devc->caps |= SB_PCI_IRQ;
  447. hw_config->driver_use_1 |= SB_PCI_IRQ;
  448. hw_config->card_subtype = MDL_YMPCI;
  449. printk("Yamaha PCI mode.\n");
  450. }
  451. if (devc->sbmo.acer)
  452. {
  453. unsigned long flags;
  454. spin_lock_irqsave(&devc->lock, flags);
  455. inb(devc->base + 0x09);
  456. inb(devc->base + 0x09);
  457. inb(devc->base + 0x09);
  458. inb(devc->base + 0x0b);
  459. inb(devc->base + 0x09);
  460. inb(devc->base + 0x0b);
  461. inb(devc->base + 0x09);
  462. inb(devc->base + 0x09);
  463. inb(devc->base + 0x0b);
  464. inb(devc->base + 0x09);
  465. inb(devc->base + 0x00);
  466. spin_unlock_irqrestore(&devc->lock, flags);
  467. }
  468. /*
  469. * Detect the device
  470. */
  471. if (sb_dsp_reset(devc))
  472. dsp_get_vers(devc);
  473. else
  474. devc->major = 0;
  475. if (devc->type == 0 || devc->type == MDL_JAZZ || devc->type == MDL_SMW)
  476. if (devc->major == 0 || (devc->major == 3 && devc->minor == 1))
  477. relocate_Jazz16(devc, hw_config);
  478. if (devc->major == 0 && (devc->type == MDL_ESS || devc->type == 0))
  479. relocate_ess1688(devc);
  480. if (!sb_dsp_reset(devc))
  481. {
  482. DDB(printk("SB reset failed\n"));
  483. #ifdef MODULE
  484. printk(KERN_INFO "sb: dsp reset failed.\n");
  485. #endif
  486. return 0;
  487. }
  488. if (devc->major == 0)
  489. dsp_get_vers(devc);
  490. if (devc->major == 3 && devc->minor == 1)
  491. {
  492. if (devc->type == MDL_AZTECH) /* SG Washington? */
  493. {
  494. if (sb_dsp_command(devc, 0x09))
  495. if (sb_dsp_command(devc, 0x00)) /* Enter WSS mode */
  496. {
  497. int i;
  498. /* Have some delay */
  499. for (i = 0; i < 10000; i++)
  500. inb(DSP_DATA_AVAIL);
  501. devc->caps = SB_NO_AUDIO | SB_NO_MIDI; /* Mixer only */
  502. devc->model = MDL_AZTECH;
  503. }
  504. }
  505. }
  506. if(devc->type == MDL_ESSPCI)
  507. devc->model = MDL_ESSPCI;
  508. if(devc->type == MDL_YMPCI)
  509. {
  510. printk("YMPCI selected\n");
  511. devc->model = MDL_YMPCI;
  512. }
  513. /*
  514. * Save device information for sb_dsp_init()
  515. */
  516. detected_devc = kmemdup(devc, sizeof(sb_devc), GFP_KERNEL);
  517. if (detected_devc == NULL)
  518. {
  519. printk(KERN_ERR "sb: Can't allocate memory for device information\n");
  520. return 0;
  521. }
  522. MDB(printk(KERN_INFO "SB %d.%02d detected OK (%x)\n", devc->major, devc->minor, hw_config->io_base));
  523. return 1;
  524. }
  525. int sb_dsp_init(struct address_info *hw_config, struct module *owner)
  526. {
  527. sb_devc *devc;
  528. char name[100];
  529. extern int sb_be_quiet;
  530. int mixer22, mixer30;
  531. /*
  532. * Check if we had detected a SB device earlier
  533. */
  534. DDB(printk("sb_dsp_init(%x) entered\n", hw_config->io_base));
  535. name[0] = 0;
  536. if (detected_devc == NULL)
  537. {
  538. MDB(printk("No detected device\n"));
  539. return 0;
  540. }
  541. devc = detected_devc;
  542. detected_devc = NULL;
  543. if (devc->base != hw_config->io_base)
  544. {
  545. DDB(printk("I/O port mismatch\n"));
  546. release_region(devc->base, 16);
  547. return 0;
  548. }
  549. /*
  550. * Now continue initialization of the device
  551. */
  552. devc->caps = hw_config->driver_use_1;
  553. if (!((devc->caps & SB_NO_AUDIO) && (devc->caps & SB_NO_MIDI)) && hw_config->irq > 0)
  554. { /* IRQ setup */
  555. /*
  556. * ESS PCI cards do shared PCI IRQ stuff. Since they
  557. * will get shared PCI irq lines we must cope.
  558. */
  559. int i=(devc->caps&SB_PCI_IRQ)?IRQF_SHARED:0;
  560. if (request_irq(hw_config->irq, sbintr, i, "soundblaster", devc) < 0)
  561. {
  562. printk(KERN_ERR "SB: Can't allocate IRQ%d\n", hw_config->irq);
  563. release_region(devc->base, 16);
  564. return 0;
  565. }
  566. devc->irq_ok = 0;
  567. if (devc->major == 4)
  568. if (!sb16_set_irq_hw(devc, devc->irq)) /* Unsupported IRQ */
  569. {
  570. free_irq(devc->irq, devc);
  571. release_region(devc->base, 16);
  572. return 0;
  573. }
  574. if ((devc->type == 0 || devc->type == MDL_ESS) &&
  575. devc->major == 3 && devc->minor == 1)
  576. { /* Handle various chipsets which claim they are SB Pro compatible */
  577. if ((devc->type != 0 && devc->type != MDL_ESS) ||
  578. !ess_init(devc, hw_config))
  579. {
  580. if ((devc->type != 0 && devc->type != MDL_JAZZ &&
  581. devc->type != MDL_SMW) || !init_Jazz16(devc, hw_config))
  582. {
  583. DDB(printk("This is a genuine SB Pro\n"));
  584. }
  585. }
  586. }
  587. if (devc->major == 4 && devc->minor <= 11 ) /* Won't work */
  588. devc->irq_ok = 1;
  589. else
  590. {
  591. int n;
  592. for (n = 0; n < 3 && devc->irq_ok == 0; n++)
  593. {
  594. if (sb_dsp_command(devc, 0xf2)) /* Cause interrupt immediately */
  595. {
  596. int i;
  597. for (i = 0; !devc->irq_ok && i < 10000; i++);
  598. }
  599. }
  600. if (!devc->irq_ok)
  601. printk(KERN_WARNING "sb: Interrupt test on IRQ%d failed - Probable IRQ conflict\n", devc->irq);
  602. else
  603. {
  604. DDB(printk("IRQ test OK (IRQ%d)\n", devc->irq));
  605. }
  606. }
  607. } /* IRQ setup */
  608. last_sb = devc;
  609. switch (devc->major)
  610. {
  611. case 1: /* SB 1.0 or 1.5 */
  612. devc->model = hw_config->card_subtype = MDL_SB1;
  613. break;
  614. case 2: /* SB 2.x */
  615. if (devc->minor == 0)
  616. devc->model = hw_config->card_subtype = MDL_SB2;
  617. else
  618. devc->model = hw_config->card_subtype = MDL_SB201;
  619. break;
  620. case 3: /* SB Pro and most clones */
  621. switch (devc->model) {
  622. case 0:
  623. devc->model = hw_config->card_subtype = MDL_SBPRO;
  624. if (hw_config->name == NULL)
  625. hw_config->name = "Sound Blaster Pro (8 BIT ONLY)";
  626. break;
  627. case MDL_ESS:
  628. ess_dsp_init(devc, hw_config);
  629. break;
  630. }
  631. break;
  632. case 4:
  633. devc->model = hw_config->card_subtype = MDL_SB16;
  634. /*
  635. * ALS007 and ALS100 return DSP version 4.2 and have 2 post-reset !=0
  636. * registers at 0x3c and 0x4c (output ctrl registers on ALS007) whereas
  637. * a "standard" SB16 doesn't have a register at 0x4c. ALS100 actively
  638. * updates register 0x22 whenever 0x30 changes, as per the SB16 spec.
  639. * Since ALS007 doesn't, this can be used to differentiate the 2 cards.
  640. */
  641. if ((devc->minor == 2) && sb_getmixer(devc,0x3c) && sb_getmixer(devc,0x4c))
  642. {
  643. mixer30 = sb_getmixer(devc,0x30);
  644. sb_setmixer(devc,0x22,(mixer22=sb_getmixer(devc,0x22)) & 0x0f);
  645. sb_setmixer(devc,0x30,0xff);
  646. /* ALS100 will force 0x30 to 0xf8 like SB16; ALS007 will allow 0xff. */
  647. /* Register 0x22 & 0xf0 on ALS100 == 0xf0; on ALS007 it == 0x10. */
  648. if ((sb_getmixer(devc,0x30) != 0xff) || ((sb_getmixer(devc,0x22) & 0xf0) != 0x10))
  649. {
  650. devc->submodel = SUBMDL_ALS100;
  651. if (hw_config->name == NULL)
  652. hw_config->name = "Sound Blaster 16 (ALS-100)";
  653. }
  654. else
  655. {
  656. sb_setmixer(devc,0x3c,0x1f); /* Enable all inputs */
  657. sb_setmixer(devc,0x4c,0x1f);
  658. sb_setmixer(devc,0x22,mixer22); /* Restore 0x22 to original value */
  659. devc->submodel = SUBMDL_ALS007;
  660. if (hw_config->name == NULL)
  661. hw_config->name = "Sound Blaster 16 (ALS-007)";
  662. }
  663. sb_setmixer(devc,0x30,mixer30);
  664. }
  665. else if (hw_config->name == NULL)
  666. hw_config->name = "Sound Blaster 16";
  667. if (hw_config->dma2 == -1)
  668. devc->dma16 = devc->dma8;
  669. else if (hw_config->dma2 < 5 || hw_config->dma2 > 7)
  670. {
  671. printk(KERN_WARNING "SB16: Bad or missing 16 bit DMA channel\n");
  672. devc->dma16 = devc->dma8;
  673. }
  674. else
  675. devc->dma16 = hw_config->dma2;
  676. if(!sb16_set_dma_hw(devc)) {
  677. free_irq(devc->irq, devc);
  678. release_region(hw_config->io_base, 16);
  679. return 0;
  680. }
  681. devc->caps |= SB_NO_MIDI;
  682. }
  683. if (!(devc->caps & SB_NO_MIXER))
  684. if (devc->major == 3 || devc->major == 4)
  685. sb_mixer_init(devc, owner);
  686. if (!(devc->caps & SB_NO_MIDI))
  687. sb_dsp_midi_init(devc, owner);
  688. if (hw_config->name == NULL)
  689. hw_config->name = "Sound Blaster (8 BIT/MONO ONLY)";
  690. sprintf(name, "%s (%d.%02d)", hw_config->name, devc->major, devc->minor);
  691. conf_printf(name, hw_config);
  692. /*
  693. * Assuming that a sound card is Sound Blaster (compatible) is the most common
  694. * configuration error and the mother of all problems. Usually sound cards
  695. * emulate SB Pro but in addition they have a 16 bit native mode which should be
  696. * used in Unix. See Readme.cards for more information about configuring OSS/Free
  697. * properly.
  698. */
  699. if (devc->model <= MDL_SBPRO)
  700. {
  701. if (devc->major == 3 && devc->minor != 1) /* "True" SB Pro should have v3.1 (rare ones may have 3.2). */
  702. {
  703. printk(KERN_INFO "This sound card may not be fully Sound Blaster Pro compatible.\n");
  704. printk(KERN_INFO "In many cases there is another way to configure OSS so that\n");
  705. printk(KERN_INFO "it works properly with OSS (for example in 16 bit mode).\n");
  706. printk(KERN_INFO "Please ignore this message if you _really_ have a SB Pro.\n");
  707. }
  708. else if (!sb_be_quiet && devc->model == MDL_SBPRO)
  709. {
  710. printk(KERN_INFO "SB DSP version is just %d.%02d which means that your card is\n", devc->major, devc->minor);
  711. printk(KERN_INFO "several years old (8 bit only device) or alternatively the sound driver\n");
  712. printk(KERN_INFO "is incorrectly configured.\n");
  713. }
  714. }
  715. hw_config->card_subtype = devc->model;
  716. hw_config->slots[0]=devc->dev;
  717. last_devc = devc; /* For SB MPU detection */
  718. if (!(devc->caps & SB_NO_AUDIO) && devc->dma8 >= 0)
  719. {
  720. if (sound_alloc_dma(devc->dma8, "SoundBlaster8"))
  721. {
  722. printk(KERN_WARNING "Sound Blaster: Can't allocate 8 bit DMA channel %d\n", devc->dma8);
  723. }
  724. if (devc->dma16 >= 0 && devc->dma16 != devc->dma8)
  725. {
  726. if (sound_alloc_dma(devc->dma16, "SoundBlaster16"))
  727. printk(KERN_WARNING "Sound Blaster: can't allocate 16 bit DMA channel %d.\n", devc->dma16);
  728. }
  729. sb_audio_init(devc, name, owner);
  730. hw_config->slots[0]=devc->dev;
  731. }
  732. else
  733. {
  734. MDB(printk("Sound Blaster: no audio devices found.\n"));
  735. }
  736. return 1;
  737. }
  738. /* if (sbmpu) below we allow mpu401 to manage the midi devs
  739. otherwise we have to unload them. (Andrzej Krzysztofowicz) */
  740. void sb_dsp_unload(struct address_info *hw_config, int sbmpu)
  741. {
  742. sb_devc *devc;
  743. devc = audio_devs[hw_config->slots[0]]->devc;
  744. if (devc && devc->base == hw_config->io_base)
  745. {
  746. if ((devc->model & MDL_ESS) && devc->pcibase)
  747. release_region(devc->pcibase, 8);
  748. release_region(devc->base, 16);
  749. if (!(devc->caps & SB_NO_AUDIO))
  750. {
  751. sound_free_dma(devc->dma8);
  752. if (devc->dma16 >= 0)
  753. sound_free_dma(devc->dma16);
  754. }
  755. if (!(devc->caps & SB_NO_AUDIO && devc->caps & SB_NO_MIDI))
  756. {
  757. if (devc->irq > 0)
  758. free_irq(devc->irq, devc);
  759. sb_mixer_unload(devc);
  760. /* We don't have to do this bit any more the UART401 is its own
  761. master -- Krzysztof Halasa */
  762. /* But we have to do it, if UART401 is not detected */
  763. if (!sbmpu)
  764. sound_unload_mididev(devc->my_mididev);
  765. sound_unload_audiodev(devc->dev);
  766. }
  767. kfree(devc);
  768. }
  769. else
  770. release_region(hw_config->io_base, 16);
  771. kfree(detected_devc);
  772. }
  773. /*
  774. * Mixer access routines
  775. *
  776. * ES1887 modifications: some mixer registers reside in the
  777. * range above 0xa0. These must be accessed in another way.
  778. */
  779. void sb_setmixer(sb_devc * devc, unsigned int port, unsigned int value)
  780. {
  781. unsigned long flags;
  782. if (devc->model == MDL_ESS) {
  783. ess_setmixer (devc, port, value);
  784. return;
  785. }
  786. spin_lock_irqsave(&devc->lock, flags);
  787. outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
  788. udelay(20);
  789. outb(((unsigned char) (value & 0xff)), MIXER_DATA);
  790. udelay(20);
  791. spin_unlock_irqrestore(&devc->lock, flags);
  792. }
  793. unsigned int sb_getmixer(sb_devc * devc, unsigned int port)
  794. {
  795. unsigned int val;
  796. unsigned long flags;
  797. if (devc->model == MDL_ESS) return ess_getmixer (devc, port);
  798. spin_lock_irqsave(&devc->lock, flags);
  799. outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
  800. udelay(20);
  801. val = inb(MIXER_DATA);
  802. udelay(20);
  803. spin_unlock_irqrestore(&devc->lock, flags);
  804. return val;
  805. }
  806. void sb_chgmixer
  807. (sb_devc * devc, unsigned int reg, unsigned int mask, unsigned int val)
  808. {
  809. int value;
  810. value = sb_getmixer(devc, reg);
  811. value = (value & ~mask) | (val & mask);
  812. sb_setmixer(devc, reg, value);
  813. }
  814. /*
  815. * MPU401 MIDI initialization.
  816. */
  817. static void smw_putmem(sb_devc * devc, int base, int addr, unsigned char val)
  818. {
  819. unsigned long flags;
  820. spin_lock_irqsave(&jazz16_lock, flags); /* NOT the SB card? */
  821. outb((addr & 0xff), base + 1); /* Low address bits */
  822. outb((addr >> 8), base + 2); /* High address bits */
  823. outb((val), base); /* Data */
  824. spin_unlock_irqrestore(&jazz16_lock, flags);
  825. }
  826. static unsigned char smw_getmem(sb_devc * devc, int base, int addr)
  827. {
  828. unsigned long flags;
  829. unsigned char val;
  830. spin_lock_irqsave(&jazz16_lock, flags); /* NOT the SB card? */
  831. outb((addr & 0xff), base + 1); /* Low address bits */
  832. outb((addr >> 8), base + 2); /* High address bits */
  833. val = inb(base); /* Data */
  834. spin_unlock_irqrestore(&jazz16_lock, flags);
  835. return val;
  836. }
  837. static int smw_midi_init(sb_devc * devc, struct address_info *hw_config)
  838. {
  839. int mpu_base = hw_config->io_base;
  840. int mp_base = mpu_base + 4; /* Microcontroller base */
  841. int i;
  842. unsigned char control;
  843. /*
  844. * Reset the microcontroller so that the RAM can be accessed
  845. */
  846. control = inb(mpu_base + 7);
  847. outb((control | 3), mpu_base + 7); /* Set last two bits to 1 (?) */
  848. outb(((control & 0xfe) | 2), mpu_base + 7); /* xxxxxxx0 resets the mc */
  849. mdelay(3); /* Wait at least 1ms */
  850. outb((control & 0xfc), mpu_base + 7); /* xxxxxx00 enables RAM */
  851. /*
  852. * Detect microcontroller by probing the 8k RAM area
  853. */
  854. smw_putmem(devc, mp_base, 0, 0x00);
  855. smw_putmem(devc, mp_base, 1, 0xff);
  856. udelay(10);
  857. if (smw_getmem(devc, mp_base, 0) != 0x00 || smw_getmem(devc, mp_base, 1) != 0xff)
  858. {
  859. DDB(printk("SM Wave: No microcontroller RAM detected (%02x, %02x)\n", smw_getmem(devc, mp_base, 0), smw_getmem(devc, mp_base, 1)));
  860. return 0; /* No RAM */
  861. }
  862. /*
  863. * There is RAM so assume it's really a SM Wave
  864. */
  865. devc->model = MDL_SMW;
  866. smw_mixer_init(devc);
  867. #ifdef MODULE
  868. if (!smw_ucode)
  869. {
  870. smw_ucodeLen = mod_firmware_load("/*(DEBLOBBED)*/", (void *) &smw_ucode);
  871. smw_free = smw_ucode;
  872. }
  873. #endif
  874. if (smw_ucodeLen > 0)
  875. {
  876. if (smw_ucodeLen != 8192)
  877. {
  878. printk(KERN_ERR "SM Wave: Invalid microcode (MIDI0001.BIN) length\n");
  879. return 1;
  880. }
  881. /*
  882. * Download microcode
  883. */
  884. for (i = 0; i < 8192; i++)
  885. smw_putmem(devc, mp_base, i, smw_ucode[i]);
  886. /*
  887. * Verify microcode
  888. */
  889. for (i = 0; i < 8192; i++)
  890. if (smw_getmem(devc, mp_base, i) != smw_ucode[i])
  891. {
  892. printk(KERN_ERR "SM Wave: Microcode verification failed\n");
  893. return 0;
  894. }
  895. }
  896. control = 0;
  897. #ifdef SMW_SCSI_IRQ
  898. /*
  899. * Set the SCSI interrupt (IRQ2/9, IRQ3 or IRQ10). The SCSI interrupt
  900. * is disabled by default.
  901. *
  902. * FIXME - make this a module option
  903. *
  904. * BTW the Zilog 5380 SCSI controller is located at MPU base + 0x10.
  905. */
  906. {
  907. static unsigned char scsi_irq_bits[] = {
  908. 0, 0, 3, 1, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0
  909. };
  910. control |= scsi_irq_bits[SMW_SCSI_IRQ] << 6;
  911. }
  912. #endif
  913. #ifdef SMW_OPL4_ENABLE
  914. /*
  915. * Make the OPL4 chip visible on the PC bus at 0x380.
  916. *
  917. * There is no need to enable this feature since this driver
  918. * doesn't support OPL4 yet. Also there is no RAM in SM Wave so
  919. * enabling OPL4 is pretty useless.
  920. */
  921. control |= 0x10; /* Uses IRQ12 if bit 0x20 == 0 */
  922. /* control |= 0x20; Uncomment this if you want to use IRQ7 */
  923. #endif
  924. outb((control | 0x03), mpu_base + 7); /* xxxxxx11 restarts */
  925. hw_config->name = "SoundMan Wave";
  926. return 1;
  927. }
  928. static int init_Jazz16_midi(sb_devc * devc, struct address_info *hw_config)
  929. {
  930. int mpu_base = hw_config->io_base;
  931. int sb_base = devc->base;
  932. int irq = hw_config->irq;
  933. unsigned char bits = 0;
  934. unsigned long flags;
  935. if (irq < 0)
  936. irq *= -1;
  937. if (irq < 1 || irq > 15 ||
  938. jazz_irq_bits[irq] == 0)
  939. {
  940. printk(KERN_ERR "Jazz16: Invalid MIDI interrupt (IRQ%d)\n", irq);
  941. return 0;
  942. }
  943. switch (sb_base)
  944. {
  945. case 0x220:
  946. bits = 1;
  947. break;
  948. case 0x240:
  949. bits = 2;
  950. break;
  951. case 0x260:
  952. bits = 3;
  953. break;
  954. default:
  955. return 0;
  956. }
  957. bits = jazz16_bits = bits << 5;
  958. switch (mpu_base)
  959. {
  960. case 0x310:
  961. bits |= 1;
  962. break;
  963. case 0x320:
  964. bits |= 2;
  965. break;
  966. case 0x330:
  967. bits |= 3;
  968. break;
  969. default:
  970. printk(KERN_ERR "Jazz16: Invalid MIDI I/O port %x\n", mpu_base);
  971. return 0;
  972. }
  973. /*
  974. * Magic wake up sequence by writing to 0x201 (aka Joystick port)
  975. */
  976. spin_lock_irqsave(&jazz16_lock, flags);
  977. outb(0xAF, 0x201);
  978. outb(0x50, 0x201);
  979. outb(bits, 0x201);
  980. spin_unlock_irqrestore(&jazz16_lock, flags);
  981. hw_config->name = "Jazz16";
  982. smw_midi_init(devc, hw_config);
  983. if (!sb_dsp_command(devc, 0xfb))
  984. return 0;
  985. if (!sb_dsp_command(devc, jazz_dma_bits[devc->dma8] |
  986. (jazz_dma_bits[devc->dma16] << 4)))
  987. return 0;
  988. if (!sb_dsp_command(devc, jazz_irq_bits[devc->irq] |
  989. (jazz_irq_bits[irq] << 4)))
  990. return 0;
  991. return 1;
  992. }
  993. int probe_sbmpu(struct address_info *hw_config, struct module *owner)
  994. {
  995. sb_devc *devc = last_devc;
  996. int ret;
  997. if (last_devc == NULL)
  998. return 0;
  999. last_devc = NULL;
  1000. if (hw_config->io_base <= 0)
  1001. {
  1002. /* The real vibra16 is fine about this, but we have to go
  1003. wipe up after Cyrix again */
  1004. if(devc->model == MDL_SB16 && devc->minor >= 12)
  1005. {
  1006. unsigned char bits = sb_getmixer(devc, 0x84) & ~0x06;
  1007. sb_setmixer(devc, 0x84, bits | 0x02); /* Disable MPU */
  1008. }
  1009. return 0;
  1010. }
  1011. #if defined(CONFIG_SOUND_MPU401)
  1012. if (devc->model == MDL_ESS)
  1013. {
  1014. struct resource *ports;
  1015. ports = request_region(hw_config->io_base, 2, "mpu401");
  1016. if (!ports) {
  1017. printk(KERN_ERR "sbmpu: I/O port conflict (%x)\n", hw_config->io_base);
  1018. return 0;
  1019. }
  1020. if (!ess_midi_init(devc, hw_config)) {
  1021. release_region(hw_config->io_base, 2);
  1022. return 0;
  1023. }
  1024. hw_config->name = "ESS1xxx MPU";
  1025. devc->midi_irq_cookie = NULL;
  1026. if (!probe_mpu401(hw_config, ports)) {
  1027. release_region(hw_config->io_base, 2);
  1028. return 0;
  1029. }
  1030. attach_mpu401(hw_config, owner);
  1031. if (last_sb->irq == -hw_config->irq)
  1032. last_sb->midi_irq_cookie =
  1033. (void *)(long) hw_config->slots[1];
  1034. return 1;
  1035. }
  1036. #endif
  1037. switch (devc->model)
  1038. {
  1039. case MDL_SB16:
  1040. if (hw_config->io_base != 0x300 && hw_config->io_base != 0x330)
  1041. {
  1042. printk(KERN_ERR "SB16: Invalid MIDI port %x\n", hw_config->io_base);
  1043. return 0;
  1044. }
  1045. hw_config->name = "Sound Blaster 16";
  1046. if (hw_config->irq < 3 || hw_config->irq == devc->irq)
  1047. hw_config->irq = -devc->irq;
  1048. if (devc->minor > 12) /* What is Vibra's version??? */
  1049. sb16_set_mpu_port(devc, hw_config);
  1050. break;
  1051. case MDL_JAZZ:
  1052. if (hw_config->irq < 3 || hw_config->irq == devc->irq)
  1053. hw_config->irq = -devc->irq;
  1054. if (!init_Jazz16_midi(devc, hw_config))
  1055. return 0;
  1056. break;
  1057. case MDL_YMPCI:
  1058. hw_config->name = "Yamaha PCI Legacy";
  1059. printk("Yamaha PCI legacy UART401 check.\n");
  1060. break;
  1061. default:
  1062. return 0;
  1063. }
  1064. ret = probe_uart401(hw_config, owner);
  1065. if (ret)
  1066. last_sb->midi_irq_cookie=midi_devs[hw_config->slots[4]]->devc;
  1067. return ret;
  1068. }
  1069. void unload_sbmpu(struct address_info *hw_config)
  1070. {
  1071. #if defined(CONFIG_SOUND_MPU401)
  1072. if (!strcmp (hw_config->name, "ESS1xxx MPU")) {
  1073. unload_mpu401(hw_config);
  1074. return;
  1075. }
  1076. #endif
  1077. unload_uart401(hw_config);
  1078. }
  1079. EXPORT_SYMBOL(sb_dsp_init);
  1080. EXPORT_SYMBOL(sb_dsp_detect);
  1081. EXPORT_SYMBOL(sb_dsp_unload);
  1082. EXPORT_SYMBOL(sb_be_quiet);
  1083. EXPORT_SYMBOL(probe_sbmpu);
  1084. EXPORT_SYMBOL(unload_sbmpu);
  1085. EXPORT_SYMBOL(smw_free);
  1086. MODULE_LICENSE("GPL");