sb_common.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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("/etc/sound/midi0001.bin", (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");