sb_common.c 29 KB

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