sb_common.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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 "sound_config.h"
  34. #include "sound_firmware.h"
  35. #include "mpu401.h"
  36. #include "sb_mixer.h"
  37. #include "sb.h"
  38. #include "sb_ess.h"
  39. /*
  40. * global module flag
  41. */
  42. int sb_be_quiet;
  43. static sb_devc *detected_devc; /* For communication from probe to init */
  44. static sb_devc *last_devc; /* For MPU401 initialization */
  45. static unsigned char jazz_irq_bits[] = {
  46. 0, 0, 2, 3, 0, 1, 0, 4, 0, 2, 5, 0, 0, 0, 0, 6
  47. };
  48. static unsigned char jazz_dma_bits[] = {
  49. 0, 1, 0, 2, 0, 3, 0, 4
  50. };
  51. void *smw_free;
  52. /*
  53. * Jazz16 chipset specific control variables
  54. */
  55. static int jazz16_base; /* Not detected */
  56. static unsigned char jazz16_bits; /* I/O relocation bits */
  57. static DEFINE_SPINLOCK(jazz16_lock);
  58. /*
  59. * Logitech Soundman Wave specific initialization code
  60. */
  61. #ifdef SMW_MIDI0001_INCLUDED
  62. #include "smw-midi0001.h"
  63. #else
  64. static unsigned char *smw_ucode;
  65. static int smw_ucodeLen;
  66. #endif
  67. static sb_devc *last_sb; /* Last sb loaded */
  68. int sb_dsp_command(sb_devc * devc, unsigned char val)
  69. {
  70. int i;
  71. unsigned long limit;
  72. limit = jiffies + HZ / 10; /* Timeout */
  73. /*
  74. * Note! the i<500000 is an emergency exit. The sb_dsp_command() is sometimes
  75. * called while interrupts are disabled. This means that the timer is
  76. * disabled also. However the timeout situation is a abnormal condition.
  77. * Normally the DSP should be ready to accept commands after just couple of
  78. * loops.
  79. */
  80. for (i = 0; i < 500000 && (limit-jiffies)>0; i++)
  81. {
  82. if ((inb(DSP_STATUS) & 0x80) == 0)
  83. {
  84. outb((val), DSP_COMMAND);
  85. return 1;
  86. }
  87. }
  88. printk(KERN_WARNING "Sound Blaster: DSP command(%x) timeout.\n", val);
  89. return 0;
  90. }
  91. int sb_dsp_get_byte(sb_devc * devc)
  92. {
  93. int i;
  94. for (i = 1000; i; i--)
  95. {
  96. if (inb(DSP_DATA_AVAIL) & 0x80)
  97. return inb(DSP_READ);
  98. }
  99. return 0xffff;
  100. }
  101. static void sb_intr (sb_devc *devc)
  102. {
  103. int status;
  104. unsigned char src = 0xff;
  105. if (devc->model == MDL_SB16)
  106. {
  107. src = sb_getmixer(devc, IRQ_STAT); /* Interrupt source register */
  108. if (src & 4) /* MPU401 interrupt */
  109. if(devc->midi_irq_cookie)
  110. uart401intr(devc->irq, devc->midi_irq_cookie);
  111. if (!(src & 3))
  112. return; /* Not a DSP interrupt */
  113. }
  114. if (devc->intr_active && (!devc->fullduplex || (src & 0x01)))
  115. {
  116. switch (devc->irq_mode)
  117. {
  118. case IMODE_OUTPUT:
  119. DMAbuf_outputintr(devc->dev, 1);
  120. break;
  121. case IMODE_INPUT:
  122. DMAbuf_inputintr(devc->dev);
  123. break;
  124. case IMODE_INIT:
  125. break;
  126. case IMODE_MIDI:
  127. sb_midi_interrupt(devc);
  128. break;
  129. default:
  130. /* printk(KERN_WARN "Sound Blaster: Unexpected interrupt\n"); */
  131. ;
  132. }
  133. }
  134. else if (devc->intr_active_16 && (src & 0x02))
  135. {
  136. switch (devc->irq_mode_16)
  137. {
  138. case IMODE_OUTPUT:
  139. DMAbuf_outputintr(devc->dev, 1);
  140. break;
  141. case IMODE_INPUT:
  142. DMAbuf_inputintr(devc->dev);
  143. break;
  144. case IMODE_INIT:
  145. break;
  146. default:
  147. /* printk(KERN_WARN "Sound Blaster: Unexpected interrupt\n"); */
  148. ;
  149. }
  150. }
  151. /*
  152. * Acknowledge interrupts
  153. */
  154. if (src & 0x01)
  155. status = inb(DSP_DATA_AVAIL);
  156. if (devc->model == MDL_SB16 && src & 0x02)
  157. status = inb(DSP_DATA_AVL16);
  158. }
  159. static void pci_intr(sb_devc *devc)
  160. {
  161. int src = inb(devc->pcibase+0x1A);
  162. src&=3;
  163. if(src)
  164. sb_intr(devc);
  165. }
  166. static irqreturn_t sbintr(int irq, void *dev_id)
  167. {
  168. sb_devc *devc = dev_id;
  169. devc->irq_ok = 1;
  170. switch (devc->model) {
  171. case MDL_ESSPCI:
  172. pci_intr (devc);
  173. break;
  174. case MDL_ESS:
  175. ess_intr (devc);
  176. break;
  177. default:
  178. sb_intr (devc);
  179. break;
  180. }
  181. return IRQ_HANDLED;
  182. }
  183. int sb_dsp_reset(sb_devc * devc)
  184. {
  185. int loopc;
  186. DEB(printk("Entered sb_dsp_reset()\n"));
  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. DEB(printk("sb_dsp_reset() OK\n"));
  200. return 1;
  201. }
  202. static void dsp_get_vers(sb_devc * devc)
  203. {
  204. int i;
  205. unsigned long flags;
  206. DDB(printk("Entered dsp_get_vers()\n"));
  207. spin_lock_irqsave(&devc->lock, flags);
  208. devc->major = devc->minor = 0;
  209. sb_dsp_command(devc, 0xe1); /* Get version */
  210. for (i = 100000; i; i--)
  211. {
  212. if (inb(DSP_DATA_AVAIL) & 0x80)
  213. {
  214. if (devc->major == 0)
  215. devc->major = inb(DSP_READ);
  216. else
  217. {
  218. devc->minor = inb(DSP_READ);
  219. break;
  220. }
  221. }
  222. }
  223. spin_unlock_irqrestore(&devc->lock, flags);
  224. DDB(printk("DSP version %d.%02d\n", devc->major, devc->minor));
  225. }
  226. static int sb16_set_dma_hw(sb_devc * devc)
  227. {
  228. int bits;
  229. if (devc->dma8 != 0 && devc->dma8 != 1 && devc->dma8 != 3)
  230. {
  231. printk(KERN_ERR "SB16: Invalid 8 bit DMA (%d)\n", devc->dma8);
  232. return 0;
  233. }
  234. bits = (1 << devc->dma8);
  235. if (devc->dma16 >= 5 && devc->dma16 <= 7)
  236. bits |= (1 << devc->dma16);
  237. sb_setmixer(devc, DMA_NR, bits);
  238. return 1;
  239. }
  240. static void sb16_set_mpu_port(sb_devc * devc, struct address_info *hw_config)
  241. {
  242. /*
  243. * This routine initializes new MIDI port setup register of SB Vibra (CT2502).
  244. */
  245. unsigned char bits = sb_getmixer(devc, 0x84) & ~0x06;
  246. switch (hw_config->io_base)
  247. {
  248. case 0x300:
  249. sb_setmixer(devc, 0x84, bits | 0x04);
  250. break;
  251. case 0x330:
  252. sb_setmixer(devc, 0x84, bits | 0x00);
  253. break;
  254. default:
  255. sb_setmixer(devc, 0x84, bits | 0x02); /* Disable MPU */
  256. printk(KERN_ERR "SB16: Invalid MIDI I/O port %x\n", hw_config->io_base);
  257. }
  258. }
  259. static int sb16_set_irq_hw(sb_devc * devc, int level)
  260. {
  261. int ival;
  262. switch (level)
  263. {
  264. case 5:
  265. ival = 2;
  266. break;
  267. case 7:
  268. ival = 4;
  269. break;
  270. case 9:
  271. ival = 1;
  272. break;
  273. case 10:
  274. ival = 8;
  275. break;
  276. default:
  277. printk(KERN_ERR "SB16: Invalid IRQ%d\n", level);
  278. return 0;
  279. }
  280. sb_setmixer(devc, IRQ_NR, ival);
  281. return 1;
  282. }
  283. static void relocate_Jazz16(sb_devc * devc, struct address_info *hw_config)
  284. {
  285. unsigned char bits = 0;
  286. unsigned long flags;
  287. if (jazz16_base != 0 && jazz16_base != hw_config->io_base)
  288. return;
  289. switch (hw_config->io_base)
  290. {
  291. case 0x220:
  292. bits = 1;
  293. break;
  294. case 0x240:
  295. bits = 2;
  296. break;
  297. case 0x260:
  298. bits = 3;
  299. break;
  300. default:
  301. return;
  302. }
  303. bits = jazz16_bits = bits << 5;
  304. jazz16_base = hw_config->io_base;
  305. /*
  306. * Magic wake up sequence by writing to 0x201 (aka Joystick port)
  307. */
  308. spin_lock_irqsave(&jazz16_lock, flags);
  309. outb((0xAF), 0x201);
  310. outb((0x50), 0x201);
  311. outb((bits), 0x201);
  312. spin_unlock_irqrestore(&jazz16_lock, flags);
  313. }
  314. static int init_Jazz16(sb_devc * devc, struct address_info *hw_config)
  315. {
  316. char name[100];
  317. /*
  318. * First try to check that the card has Jazz16 chip. It identifies itself
  319. * by returning 0x12 as response to DSP command 0xfa.
  320. */
  321. if (!sb_dsp_command(devc, 0xfa))
  322. return 0;
  323. if (sb_dsp_get_byte(devc) != 0x12)
  324. return 0;
  325. /*
  326. * OK so far. Now configure the IRQ and DMA channel used by the card.
  327. */
  328. if (hw_config->irq < 1 || hw_config->irq > 15 || jazz_irq_bits[hw_config->irq] == 0)
  329. {
  330. printk(KERN_ERR "Jazz16: Invalid interrupt (IRQ%d)\n", hw_config->irq);
  331. return 0;
  332. }
  333. if (hw_config->dma < 0 || hw_config->dma > 3 || jazz_dma_bits[hw_config->dma] == 0)
  334. {
  335. printk(KERN_ERR "Jazz16: Invalid 8 bit DMA (DMA%d)\n", hw_config->dma);
  336. return 0;
  337. }
  338. if (hw_config->dma2 < 0)
  339. {
  340. printk(KERN_ERR "Jazz16: No 16 bit DMA channel defined\n");
  341. return 0;
  342. }
  343. if (hw_config->dma2 < 5 || hw_config->dma2 > 7 || jazz_dma_bits[hw_config->dma2] == 0)
  344. {
  345. printk(KERN_ERR "Jazz16: Invalid 16 bit DMA (DMA%d)\n", hw_config->dma2);
  346. return 0;
  347. }
  348. devc->dma16 = hw_config->dma2;
  349. if (!sb_dsp_command(devc, 0xfb))
  350. return 0;
  351. if (!sb_dsp_command(devc, jazz_dma_bits[hw_config->dma] |
  352. (jazz_dma_bits[hw_config->dma2] << 4)))
  353. return 0;
  354. if (!sb_dsp_command(devc, jazz_irq_bits[hw_config->irq]))
  355. return 0;
  356. /*
  357. * Now we have configured a standard Jazz16 device.
  358. */
  359. devc->model = MDL_JAZZ;
  360. strcpy(name, "Jazz16");
  361. hw_config->name = "Jazz16";
  362. devc->caps |= SB_NO_MIDI;
  363. return 1;
  364. }
  365. static void relocate_ess1688(sb_devc * devc)
  366. {
  367. unsigned char bits;
  368. switch (devc->base)
  369. {
  370. case 0x220:
  371. bits = 0x04;
  372. break;
  373. case 0x230:
  374. bits = 0x05;
  375. break;
  376. case 0x240:
  377. bits = 0x06;
  378. break;
  379. case 0x250:
  380. bits = 0x07;
  381. break;
  382. default:
  383. return; /* Wrong port */
  384. }
  385. DDB(printk("Doing ESS1688 address selection\n"));
  386. /*
  387. * ES1688 supports two alternative ways for software address config.
  388. * First try the so called Read-Sequence-Key method.
  389. */
  390. /* Reset the sequence logic */
  391. inb(0x229);
  392. inb(0x229);
  393. inb(0x229);
  394. /* Perform the read sequence */
  395. inb(0x22b);
  396. inb(0x229);
  397. inb(0x22b);
  398. inb(0x229);
  399. inb(0x229);
  400. inb(0x22b);
  401. inb(0x229);
  402. /* Select the base address by reading from it. Then probe using the port. */
  403. inb(devc->base);
  404. if (sb_dsp_reset(devc)) /* Bingo */
  405. return;
  406. #if 0 /* This causes system lockups (Nokia 386/25 at least) */
  407. /*
  408. * The last resort is the system control register method.
  409. */
  410. outb((0x00), 0xfb); /* 0xFB is the unlock register */
  411. outb((0x00), 0xe0); /* Select index 0 */
  412. outb((bits), 0xe1); /* Write the config bits */
  413. outb((0x00), 0xf9); /* 0xFB is the lock register */
  414. #endif
  415. }
  416. int sb_dsp_detect(struct address_info *hw_config, int pci, int pciio, struct sb_module_options *sbmo)
  417. {
  418. sb_devc sb_info;
  419. sb_devc *devc = &sb_info;
  420. memset((char *) &sb_info, 0, sizeof(sb_info)); /* Zero everything */
  421. /* Copy module options in place */
  422. if(sbmo) memcpy(&devc->sbmo, sbmo, sizeof(struct sb_module_options));
  423. sb_info.my_mididev = -1;
  424. sb_info.my_mixerdev = -1;
  425. sb_info.dev = -1;
  426. /*
  427. * Initialize variables
  428. */
  429. DDB(printk("sb_dsp_detect(%x) entered\n", hw_config->io_base));
  430. spin_lock_init(&devc->lock);
  431. devc->type = hw_config->card_subtype;
  432. devc->base = hw_config->io_base;
  433. devc->irq = hw_config->irq;
  434. devc->dma8 = hw_config->dma;
  435. devc->dma16 = -1;
  436. devc->pcibase = pciio;
  437. if(pci == SB_PCI_ESSMAESTRO)
  438. {
  439. devc->model = MDL_ESSPCI;
  440. devc->caps |= SB_PCI_IRQ;
  441. hw_config->driver_use_1 |= SB_PCI_IRQ;
  442. hw_config->card_subtype = MDL_ESSPCI;
  443. }
  444. if(pci == SB_PCI_YAMAHA)
  445. {
  446. devc->model = MDL_YMPCI;
  447. devc->caps |= SB_PCI_IRQ;
  448. hw_config->driver_use_1 |= SB_PCI_IRQ;
  449. hw_config->card_subtype = MDL_YMPCI;
  450. printk("Yamaha PCI mode.\n");
  451. }
  452. if (devc->sbmo.acer)
  453. {
  454. unsigned long flags;
  455. spin_lock_irqsave(&devc->lock, flags);
  456. inb(devc->base + 0x09);
  457. inb(devc->base + 0x09);
  458. inb(devc->base + 0x09);
  459. inb(devc->base + 0x0b);
  460. inb(devc->base + 0x09);
  461. inb(devc->base + 0x0b);
  462. inb(devc->base + 0x09);
  463. inb(devc->base + 0x09);
  464. inb(devc->base + 0x0b);
  465. inb(devc->base + 0x09);
  466. inb(devc->base + 0x00);
  467. spin_unlock_irqrestore(&devc->lock, flags);
  468. }
  469. /*
  470. * Detect the device
  471. */
  472. if (sb_dsp_reset(devc))
  473. dsp_get_vers(devc);
  474. else
  475. devc->major = 0;
  476. if (devc->type == 0 || devc->type == MDL_JAZZ || devc->type == MDL_SMW)
  477. if (devc->major == 0 || (devc->major == 3 && devc->minor == 1))
  478. relocate_Jazz16(devc, hw_config);
  479. if (devc->major == 0 && (devc->type == MDL_ESS || devc->type == 0))
  480. relocate_ess1688(devc);
  481. if (!sb_dsp_reset(devc))
  482. {
  483. DDB(printk("SB reset failed\n"));
  484. #ifdef MODULE
  485. printk(KERN_INFO "sb: dsp reset failed.\n");
  486. #endif
  487. return 0;
  488. }
  489. if (devc->major == 0)
  490. dsp_get_vers(devc);
  491. if (devc->major == 3 && devc->minor == 1)
  492. {
  493. if (devc->type == MDL_AZTECH) /* SG Washington? */
  494. {
  495. if (sb_dsp_command(devc, 0x09))
  496. if (sb_dsp_command(devc, 0x00)) /* Enter WSS mode */
  497. {
  498. int i;
  499. /* Have some delay */
  500. for (i = 0; i < 10000; i++)
  501. inb(DSP_DATA_AVAIL);
  502. devc->caps = SB_NO_AUDIO | SB_NO_MIDI; /* Mixer only */
  503. devc->model = MDL_AZTECH;
  504. }
  505. }
  506. }
  507. if(devc->type == MDL_ESSPCI)
  508. devc->model = MDL_ESSPCI;
  509. if(devc->type == MDL_YMPCI)
  510. {
  511. printk("YMPCI selected\n");
  512. devc->model = MDL_YMPCI;
  513. }
  514. /*
  515. * Save device information for sb_dsp_init()
  516. */
  517. detected_devc = kmalloc(sizeof(sb_devc), GFP_KERNEL);
  518. if (detected_devc == NULL)
  519. {
  520. printk(KERN_ERR "sb: Can't allocate memory for device information\n");
  521. return 0;
  522. }
  523. memcpy(detected_devc, devc, sizeof(sb_devc));
  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=(void *)hw_config->slots[1];
  1035. return 1;
  1036. }
  1037. #endif
  1038. switch (devc->model)
  1039. {
  1040. case MDL_SB16:
  1041. if (hw_config->io_base != 0x300 && hw_config->io_base != 0x330)
  1042. {
  1043. printk(KERN_ERR "SB16: Invalid MIDI port %x\n", hw_config->io_base);
  1044. return 0;
  1045. }
  1046. hw_config->name = "Sound Blaster 16";
  1047. if (hw_config->irq < 3 || hw_config->irq == devc->irq)
  1048. hw_config->irq = -devc->irq;
  1049. if (devc->minor > 12) /* What is Vibra's version??? */
  1050. sb16_set_mpu_port(devc, hw_config);
  1051. break;
  1052. case MDL_JAZZ:
  1053. if (hw_config->irq < 3 || hw_config->irq == devc->irq)
  1054. hw_config->irq = -devc->irq;
  1055. if (!init_Jazz16_midi(devc, hw_config))
  1056. return 0;
  1057. break;
  1058. case MDL_YMPCI:
  1059. hw_config->name = "Yamaha PCI Legacy";
  1060. printk("Yamaha PCI legacy UART401 check.\n");
  1061. break;
  1062. default:
  1063. return 0;
  1064. }
  1065. ret = probe_uart401(hw_config, owner);
  1066. if (ret)
  1067. last_sb->midi_irq_cookie=midi_devs[hw_config->slots[4]]->devc;
  1068. return ret;
  1069. }
  1070. void unload_sbmpu(struct address_info *hw_config)
  1071. {
  1072. #if defined(CONFIG_SOUND_MPU401)
  1073. if (!strcmp (hw_config->name, "ESS1xxx MPU")) {
  1074. unload_mpu401(hw_config);
  1075. return;
  1076. }
  1077. #endif
  1078. unload_uart401(hw_config);
  1079. }
  1080. EXPORT_SYMBOL(sb_dsp_init);
  1081. EXPORT_SYMBOL(sb_dsp_detect);
  1082. EXPORT_SYMBOL(sb_dsp_unload);
  1083. EXPORT_SYMBOL(sb_be_quiet);
  1084. EXPORT_SYMBOL(probe_sbmpu);
  1085. EXPORT_SYMBOL(unload_sbmpu);
  1086. EXPORT_SYMBOL(smw_free);
  1087. MODULE_LICENSE("GPL");