sb_common.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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 = kmalloc(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. memcpy(detected_devc, devc, sizeof(sb_devc));
  525. MDB(printk(KERN_INFO "SB %d.%02d detected OK (%x)\n", devc->major, devc->minor, hw_config->io_base));
  526. return 1;
  527. }
  528. int sb_dsp_init(struct address_info *hw_config, struct module *owner)
  529. {
  530. sb_devc *devc;
  531. char name[100];
  532. extern int sb_be_quiet;
  533. int mixer22, mixer30;
  534. /*
  535. * Check if we had detected a SB device earlier
  536. */
  537. DDB(printk("sb_dsp_init(%x) entered\n", hw_config->io_base));
  538. name[0] = 0;
  539. if (detected_devc == NULL)
  540. {
  541. MDB(printk("No detected device\n"));
  542. return 0;
  543. }
  544. devc = detected_devc;
  545. detected_devc = NULL;
  546. if (devc->base != hw_config->io_base)
  547. {
  548. DDB(printk("I/O port mismatch\n"));
  549. release_region(devc->base, 16);
  550. return 0;
  551. }
  552. /*
  553. * Now continue initialization of the device
  554. */
  555. devc->caps = hw_config->driver_use_1;
  556. if (!((devc->caps & SB_NO_AUDIO) && (devc->caps & SB_NO_MIDI)) && hw_config->irq > 0)
  557. { /* IRQ setup */
  558. /*
  559. * ESS PCI cards do shared PCI IRQ stuff. Since they
  560. * will get shared PCI irq lines we must cope.
  561. */
  562. int i=(devc->caps&SB_PCI_IRQ)?IRQF_SHARED:0;
  563. if (request_irq(hw_config->irq, sbintr, i, "soundblaster", devc) < 0)
  564. {
  565. printk(KERN_ERR "SB: Can't allocate IRQ%d\n", hw_config->irq);
  566. release_region(devc->base, 16);
  567. return 0;
  568. }
  569. devc->irq_ok = 0;
  570. if (devc->major == 4)
  571. if (!sb16_set_irq_hw(devc, devc->irq)) /* Unsupported IRQ */
  572. {
  573. free_irq(devc->irq, devc);
  574. release_region(devc->base, 16);
  575. return 0;
  576. }
  577. if ((devc->type == 0 || devc->type == MDL_ESS) &&
  578. devc->major == 3 && devc->minor == 1)
  579. { /* Handle various chipsets which claim they are SB Pro compatible */
  580. if ((devc->type != 0 && devc->type != MDL_ESS) ||
  581. !ess_init(devc, hw_config))
  582. {
  583. if ((devc->type != 0 && devc->type != MDL_JAZZ &&
  584. devc->type != MDL_SMW) || !init_Jazz16(devc, hw_config))
  585. {
  586. DDB(printk("This is a genuine SB Pro\n"));
  587. }
  588. }
  589. }
  590. if (devc->major == 4 && devc->minor <= 11 ) /* Won't work */
  591. devc->irq_ok = 1;
  592. else
  593. {
  594. int n;
  595. for (n = 0; n < 3 && devc->irq_ok == 0; n++)
  596. {
  597. if (sb_dsp_command(devc, 0xf2)) /* Cause interrupt immediately */
  598. {
  599. int i;
  600. for (i = 0; !devc->irq_ok && i < 10000; i++);
  601. }
  602. }
  603. if (!devc->irq_ok)
  604. printk(KERN_WARNING "sb: Interrupt test on IRQ%d failed - Probable IRQ conflict\n", devc->irq);
  605. else
  606. {
  607. DDB(printk("IRQ test OK (IRQ%d)\n", devc->irq));
  608. }
  609. }
  610. } /* IRQ setup */
  611. last_sb = devc;
  612. switch (devc->major)
  613. {
  614. case 1: /* SB 1.0 or 1.5 */
  615. devc->model = hw_config->card_subtype = MDL_SB1;
  616. break;
  617. case 2: /* SB 2.x */
  618. if (devc->minor == 0)
  619. devc->model = hw_config->card_subtype = MDL_SB2;
  620. else
  621. devc->model = hw_config->card_subtype = MDL_SB201;
  622. break;
  623. case 3: /* SB Pro and most clones */
  624. switch (devc->model) {
  625. case 0:
  626. devc->model = hw_config->card_subtype = MDL_SBPRO;
  627. if (hw_config->name == NULL)
  628. hw_config->name = "Sound Blaster Pro (8 BIT ONLY)";
  629. break;
  630. case MDL_ESS:
  631. ess_dsp_init(devc, hw_config);
  632. break;
  633. }
  634. break;
  635. case 4:
  636. devc->model = hw_config->card_subtype = MDL_SB16;
  637. /*
  638. * ALS007 and ALS100 return DSP version 4.2 and have 2 post-reset !=0
  639. * registers at 0x3c and 0x4c (output ctrl registers on ALS007) whereas
  640. * a "standard" SB16 doesn't have a register at 0x4c. ALS100 actively
  641. * updates register 0x22 whenever 0x30 changes, as per the SB16 spec.
  642. * Since ALS007 doesn't, this can be used to differentiate the 2 cards.
  643. */
  644. if ((devc->minor == 2) && sb_getmixer(devc,0x3c) && sb_getmixer(devc,0x4c))
  645. {
  646. mixer30 = sb_getmixer(devc,0x30);
  647. sb_setmixer(devc,0x22,(mixer22=sb_getmixer(devc,0x22)) & 0x0f);
  648. sb_setmixer(devc,0x30,0xff);
  649. /* ALS100 will force 0x30 to 0xf8 like SB16; ALS007 will allow 0xff. */
  650. /* Register 0x22 & 0xf0 on ALS100 == 0xf0; on ALS007 it == 0x10. */
  651. if ((sb_getmixer(devc,0x30) != 0xff) || ((sb_getmixer(devc,0x22) & 0xf0) != 0x10))
  652. {
  653. devc->submodel = SUBMDL_ALS100;
  654. if (hw_config->name == NULL)
  655. hw_config->name = "Sound Blaster 16 (ALS-100)";
  656. }
  657. else
  658. {
  659. sb_setmixer(devc,0x3c,0x1f); /* Enable all inputs */
  660. sb_setmixer(devc,0x4c,0x1f);
  661. sb_setmixer(devc,0x22,mixer22); /* Restore 0x22 to original value */
  662. devc->submodel = SUBMDL_ALS007;
  663. if (hw_config->name == NULL)
  664. hw_config->name = "Sound Blaster 16 (ALS-007)";
  665. }
  666. sb_setmixer(devc,0x30,mixer30);
  667. }
  668. else if (hw_config->name == NULL)
  669. hw_config->name = "Sound Blaster 16";
  670. if (hw_config->dma2 == -1)
  671. devc->dma16 = devc->dma8;
  672. else if (hw_config->dma2 < 5 || hw_config->dma2 > 7)
  673. {
  674. printk(KERN_WARNING "SB16: Bad or missing 16 bit DMA channel\n");
  675. devc->dma16 = devc->dma8;
  676. }
  677. else
  678. devc->dma16 = hw_config->dma2;
  679. if(!sb16_set_dma_hw(devc)) {
  680. free_irq(devc->irq, devc);
  681. release_region(hw_config->io_base, 16);
  682. return 0;
  683. }
  684. devc->caps |= SB_NO_MIDI;
  685. }
  686. if (!(devc->caps & SB_NO_MIXER))
  687. if (devc->major == 3 || devc->major == 4)
  688. sb_mixer_init(devc, owner);
  689. if (!(devc->caps & SB_NO_MIDI))
  690. sb_dsp_midi_init(devc, owner);
  691. if (hw_config->name == NULL)
  692. hw_config->name = "Sound Blaster (8 BIT/MONO ONLY)";
  693. sprintf(name, "%s (%d.%02d)", hw_config->name, devc->major, devc->minor);
  694. conf_printf(name, hw_config);
  695. /*
  696. * Assuming that a sound card is Sound Blaster (compatible) is the most common
  697. * configuration error and the mother of all problems. Usually sound cards
  698. * emulate SB Pro but in addition they have a 16 bit native mode which should be
  699. * used in Unix. See Readme.cards for more information about configuring OSS/Free
  700. * properly.
  701. */
  702. if (devc->model <= MDL_SBPRO)
  703. {
  704. if (devc->major == 3 && devc->minor != 1) /* "True" SB Pro should have v3.1 (rare ones may have 3.2). */
  705. {
  706. printk(KERN_INFO "This sound card may not be fully Sound Blaster Pro compatible.\n");
  707. printk(KERN_INFO "In many cases there is another way to configure OSS so that\n");
  708. printk(KERN_INFO "it works properly with OSS (for example in 16 bit mode).\n");
  709. printk(KERN_INFO "Please ignore this message if you _really_ have a SB Pro.\n");
  710. }
  711. else if (!sb_be_quiet && devc->model == MDL_SBPRO)
  712. {
  713. printk(KERN_INFO "SB DSP version is just %d.%02d which means that your card is\n", devc->major, devc->minor);
  714. printk(KERN_INFO "several years old (8 bit only device) or alternatively the sound driver\n");
  715. printk(KERN_INFO "is incorrectly configured.\n");
  716. }
  717. }
  718. hw_config->card_subtype = devc->model;
  719. hw_config->slots[0]=devc->dev;
  720. last_devc = devc; /* For SB MPU detection */
  721. if (!(devc->caps & SB_NO_AUDIO) && devc->dma8 >= 0)
  722. {
  723. if (sound_alloc_dma(devc->dma8, "SoundBlaster8"))
  724. {
  725. printk(KERN_WARNING "Sound Blaster: Can't allocate 8 bit DMA channel %d\n", devc->dma8);
  726. }
  727. if (devc->dma16 >= 0 && devc->dma16 != devc->dma8)
  728. {
  729. if (sound_alloc_dma(devc->dma16, "SoundBlaster16"))
  730. printk(KERN_WARNING "Sound Blaster: can't allocate 16 bit DMA channel %d.\n", devc->dma16);
  731. }
  732. sb_audio_init(devc, name, owner);
  733. hw_config->slots[0]=devc->dev;
  734. }
  735. else
  736. {
  737. MDB(printk("Sound Blaster: no audio devices found.\n"));
  738. }
  739. return 1;
  740. }
  741. /* if (sbmpu) below we allow mpu401 to manage the midi devs
  742. otherwise we have to unload them. (Andrzej Krzysztofowicz) */
  743. void sb_dsp_unload(struct address_info *hw_config, int sbmpu)
  744. {
  745. sb_devc *devc;
  746. devc = audio_devs[hw_config->slots[0]]->devc;
  747. if (devc && devc->base == hw_config->io_base)
  748. {
  749. if ((devc->model & MDL_ESS) && devc->pcibase)
  750. release_region(devc->pcibase, 8);
  751. release_region(devc->base, 16);
  752. if (!(devc->caps & SB_NO_AUDIO))
  753. {
  754. sound_free_dma(devc->dma8);
  755. if (devc->dma16 >= 0)
  756. sound_free_dma(devc->dma16);
  757. }
  758. if (!(devc->caps & SB_NO_AUDIO && devc->caps & SB_NO_MIDI))
  759. {
  760. if (devc->irq > 0)
  761. free_irq(devc->irq, devc);
  762. sb_mixer_unload(devc);
  763. /* We don't have to do this bit any more the UART401 is its own
  764. master -- Krzysztof Halasa */
  765. /* But we have to do it, if UART401 is not detected */
  766. if (!sbmpu)
  767. sound_unload_mididev(devc->my_mididev);
  768. sound_unload_audiodev(devc->dev);
  769. }
  770. kfree(devc);
  771. }
  772. else
  773. release_region(hw_config->io_base, 16);
  774. kfree(detected_devc);
  775. }
  776. /*
  777. * Mixer access routines
  778. *
  779. * ES1887 modifications: some mixer registers reside in the
  780. * range above 0xa0. These must be accessed in another way.
  781. */
  782. void sb_setmixer(sb_devc * devc, unsigned int port, unsigned int value)
  783. {
  784. unsigned long flags;
  785. if (devc->model == MDL_ESS) {
  786. ess_setmixer (devc, port, value);
  787. return;
  788. }
  789. spin_lock_irqsave(&devc->lock, flags);
  790. outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
  791. udelay(20);
  792. outb(((unsigned char) (value & 0xff)), MIXER_DATA);
  793. udelay(20);
  794. spin_unlock_irqrestore(&devc->lock, flags);
  795. }
  796. unsigned int sb_getmixer(sb_devc * devc, unsigned int port)
  797. {
  798. unsigned int val;
  799. unsigned long flags;
  800. if (devc->model == MDL_ESS) return ess_getmixer (devc, port);
  801. spin_lock_irqsave(&devc->lock, flags);
  802. outb(((unsigned char) (port & 0xff)), MIXER_ADDR);
  803. udelay(20);
  804. val = inb(MIXER_DATA);
  805. udelay(20);
  806. spin_unlock_irqrestore(&devc->lock, flags);
  807. return val;
  808. }
  809. void sb_chgmixer
  810. (sb_devc * devc, unsigned int reg, unsigned int mask, unsigned int val)
  811. {
  812. int value;
  813. value = sb_getmixer(devc, reg);
  814. value = (value & ~mask) | (val & mask);
  815. sb_setmixer(devc, reg, value);
  816. }
  817. /*
  818. * MPU401 MIDI initialization.
  819. */
  820. static void smw_putmem(sb_devc * devc, int base, int addr, unsigned char val)
  821. {
  822. unsigned long flags;
  823. spin_lock_irqsave(&jazz16_lock, flags); /* NOT the SB card? */
  824. outb((addr & 0xff), base + 1); /* Low address bits */
  825. outb((addr >> 8), base + 2); /* High address bits */
  826. outb((val), base); /* Data */
  827. spin_unlock_irqrestore(&jazz16_lock, flags);
  828. }
  829. static unsigned char smw_getmem(sb_devc * devc, int base, int addr)
  830. {
  831. unsigned long flags;
  832. unsigned char val;
  833. spin_lock_irqsave(&jazz16_lock, flags); /* NOT the SB card? */
  834. outb((addr & 0xff), base + 1); /* Low address bits */
  835. outb((addr >> 8), base + 2); /* High address bits */
  836. val = inb(base); /* Data */
  837. spin_unlock_irqrestore(&jazz16_lock, flags);
  838. return val;
  839. }
  840. static int smw_midi_init(sb_devc * devc, struct address_info *hw_config)
  841. {
  842. int mpu_base = hw_config->io_base;
  843. int mp_base = mpu_base + 4; /* Microcontroller base */
  844. int i;
  845. unsigned char control;
  846. /*
  847. * Reset the microcontroller so that the RAM can be accessed
  848. */
  849. control = inb(mpu_base + 7);
  850. outb((control | 3), mpu_base + 7); /* Set last two bits to 1 (?) */
  851. outb(((control & 0xfe) | 2), mpu_base + 7); /* xxxxxxx0 resets the mc */
  852. mdelay(3); /* Wait at least 1ms */
  853. outb((control & 0xfc), mpu_base + 7); /* xxxxxx00 enables RAM */
  854. /*
  855. * Detect microcontroller by probing the 8k RAM area
  856. */
  857. smw_putmem(devc, mp_base, 0, 0x00);
  858. smw_putmem(devc, mp_base, 1, 0xff);
  859. udelay(10);
  860. if (smw_getmem(devc, mp_base, 0) != 0x00 || smw_getmem(devc, mp_base, 1) != 0xff)
  861. {
  862. DDB(printk("SM Wave: No microcontroller RAM detected (%02x, %02x)\n", smw_getmem(devc, mp_base, 0), smw_getmem(devc, mp_base, 1)));
  863. return 0; /* No RAM */
  864. }
  865. /*
  866. * There is RAM so assume it's really a SM Wave
  867. */
  868. devc->model = MDL_SMW;
  869. smw_mixer_init(devc);
  870. #ifdef MODULE
  871. if (!smw_ucode)
  872. {
  873. smw_ucodeLen = mod_firmware_load("/etc/sound/midi0001.bin", (void *) &smw_ucode);
  874. smw_free = smw_ucode;
  875. }
  876. #endif
  877. if (smw_ucodeLen > 0)
  878. {
  879. if (smw_ucodeLen != 8192)
  880. {
  881. printk(KERN_ERR "SM Wave: Invalid microcode (MIDI0001.BIN) length\n");
  882. return 1;
  883. }
  884. /*
  885. * Download microcode
  886. */
  887. for (i = 0; i < 8192; i++)
  888. smw_putmem(devc, mp_base, i, smw_ucode[i]);
  889. /*
  890. * Verify microcode
  891. */
  892. for (i = 0; i < 8192; i++)
  893. if (smw_getmem(devc, mp_base, i) != smw_ucode[i])
  894. {
  895. printk(KERN_ERR "SM Wave: Microcode verification failed\n");
  896. return 0;
  897. }
  898. }
  899. control = 0;
  900. #ifdef SMW_SCSI_IRQ
  901. /*
  902. * Set the SCSI interrupt (IRQ2/9, IRQ3 or IRQ10). The SCSI interrupt
  903. * is disabled by default.
  904. *
  905. * FIXME - make this a module option
  906. *
  907. * BTW the Zilog 5380 SCSI controller is located at MPU base + 0x10.
  908. */
  909. {
  910. static unsigned char scsi_irq_bits[] = {
  911. 0, 0, 3, 1, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0
  912. };
  913. control |= scsi_irq_bits[SMW_SCSI_IRQ] << 6;
  914. }
  915. #endif
  916. #ifdef SMW_OPL4_ENABLE
  917. /*
  918. * Make the OPL4 chip visible on the PC bus at 0x380.
  919. *
  920. * There is no need to enable this feature since this driver
  921. * doesn't support OPL4 yet. Also there is no RAM in SM Wave so
  922. * enabling OPL4 is pretty useless.
  923. */
  924. control |= 0x10; /* Uses IRQ12 if bit 0x20 == 0 */
  925. /* control |= 0x20; Uncomment this if you want to use IRQ7 */
  926. #endif
  927. outb((control | 0x03), mpu_base + 7); /* xxxxxx11 restarts */
  928. hw_config->name = "SoundMan Wave";
  929. return 1;
  930. }
  931. static int init_Jazz16_midi(sb_devc * devc, struct address_info *hw_config)
  932. {
  933. int mpu_base = hw_config->io_base;
  934. int sb_base = devc->base;
  935. int irq = hw_config->irq;
  936. unsigned char bits = 0;
  937. unsigned long flags;
  938. if (irq < 0)
  939. irq *= -1;
  940. if (irq < 1 || irq > 15 ||
  941. jazz_irq_bits[irq] == 0)
  942. {
  943. printk(KERN_ERR "Jazz16: Invalid MIDI interrupt (IRQ%d)\n", irq);
  944. return 0;
  945. }
  946. switch (sb_base)
  947. {
  948. case 0x220:
  949. bits = 1;
  950. break;
  951. case 0x240:
  952. bits = 2;
  953. break;
  954. case 0x260:
  955. bits = 3;
  956. break;
  957. default:
  958. return 0;
  959. }
  960. bits = jazz16_bits = bits << 5;
  961. switch (mpu_base)
  962. {
  963. case 0x310:
  964. bits |= 1;
  965. break;
  966. case 0x320:
  967. bits |= 2;
  968. break;
  969. case 0x330:
  970. bits |= 3;
  971. break;
  972. default:
  973. printk(KERN_ERR "Jazz16: Invalid MIDI I/O port %x\n", mpu_base);
  974. return 0;
  975. }
  976. /*
  977. * Magic wake up sequence by writing to 0x201 (aka Joystick port)
  978. */
  979. spin_lock_irqsave(&jazz16_lock, flags);
  980. outb(0xAF, 0x201);
  981. outb(0x50, 0x201);
  982. outb(bits, 0x201);
  983. spin_unlock_irqrestore(&jazz16_lock, flags);
  984. hw_config->name = "Jazz16";
  985. smw_midi_init(devc, hw_config);
  986. if (!sb_dsp_command(devc, 0xfb))
  987. return 0;
  988. if (!sb_dsp_command(devc, jazz_dma_bits[devc->dma8] |
  989. (jazz_dma_bits[devc->dma16] << 4)))
  990. return 0;
  991. if (!sb_dsp_command(devc, jazz_irq_bits[devc->irq] |
  992. (jazz_irq_bits[irq] << 4)))
  993. return 0;
  994. return 1;
  995. }
  996. int probe_sbmpu(struct address_info *hw_config, struct module *owner)
  997. {
  998. sb_devc *devc = last_devc;
  999. int ret;
  1000. if (last_devc == NULL)
  1001. return 0;
  1002. last_devc = NULL;
  1003. if (hw_config->io_base <= 0)
  1004. {
  1005. /* The real vibra16 is fine about this, but we have to go
  1006. wipe up after Cyrix again */
  1007. if(devc->model == MDL_SB16 && devc->minor >= 12)
  1008. {
  1009. unsigned char bits = sb_getmixer(devc, 0x84) & ~0x06;
  1010. sb_setmixer(devc, 0x84, bits | 0x02); /* Disable MPU */
  1011. }
  1012. return 0;
  1013. }
  1014. #if defined(CONFIG_SOUND_MPU401)
  1015. if (devc->model == MDL_ESS)
  1016. {
  1017. struct resource *ports;
  1018. ports = request_region(hw_config->io_base, 2, "mpu401");
  1019. if (!ports) {
  1020. printk(KERN_ERR "sbmpu: I/O port conflict (%x)\n", hw_config->io_base);
  1021. return 0;
  1022. }
  1023. if (!ess_midi_init(devc, hw_config)) {
  1024. release_region(hw_config->io_base, 2);
  1025. return 0;
  1026. }
  1027. hw_config->name = "ESS1xxx MPU";
  1028. devc->midi_irq_cookie = NULL;
  1029. if (!probe_mpu401(hw_config, ports)) {
  1030. release_region(hw_config->io_base, 2);
  1031. return 0;
  1032. }
  1033. attach_mpu401(hw_config, owner);
  1034. if (last_sb->irq == -hw_config->irq)
  1035. last_sb->midi_irq_cookie =
  1036. (void *)(long) hw_config->slots[1];
  1037. return 1;
  1038. }
  1039. #endif
  1040. switch (devc->model)
  1041. {
  1042. case MDL_SB16:
  1043. if (hw_config->io_base != 0x300 && hw_config->io_base != 0x330)
  1044. {
  1045. printk(KERN_ERR "SB16: Invalid MIDI port %x\n", hw_config->io_base);
  1046. return 0;
  1047. }
  1048. hw_config->name = "Sound Blaster 16";
  1049. if (hw_config->irq < 3 || hw_config->irq == devc->irq)
  1050. hw_config->irq = -devc->irq;
  1051. if (devc->minor > 12) /* What is Vibra's version??? */
  1052. sb16_set_mpu_port(devc, hw_config);
  1053. break;
  1054. case MDL_JAZZ:
  1055. if (hw_config->irq < 3 || hw_config->irq == devc->irq)
  1056. hw_config->irq = -devc->irq;
  1057. if (!init_Jazz16_midi(devc, hw_config))
  1058. return 0;
  1059. break;
  1060. case MDL_YMPCI:
  1061. hw_config->name = "Yamaha PCI Legacy";
  1062. printk("Yamaha PCI legacy UART401 check.\n");
  1063. break;
  1064. default:
  1065. return 0;
  1066. }
  1067. ret = probe_uart401(hw_config, owner);
  1068. if (ret)
  1069. last_sb->midi_irq_cookie=midi_devs[hw_config->slots[4]]->devc;
  1070. return ret;
  1071. }
  1072. void unload_sbmpu(struct address_info *hw_config)
  1073. {
  1074. #if defined(CONFIG_SOUND_MPU401)
  1075. if (!strcmp (hw_config->name, "ESS1xxx MPU")) {
  1076. unload_mpu401(hw_config);
  1077. return;
  1078. }
  1079. #endif
  1080. unload_uart401(hw_config);
  1081. }
  1082. EXPORT_SYMBOL(sb_dsp_init);
  1083. EXPORT_SYMBOL(sb_dsp_detect);
  1084. EXPORT_SYMBOL(sb_dsp_unload);
  1085. EXPORT_SYMBOL(sb_be_quiet);
  1086. EXPORT_SYMBOL(probe_sbmpu);
  1087. EXPORT_SYMBOL(unload_sbmpu);
  1088. EXPORT_SYMBOL(smw_free);
  1089. MODULE_LICENSE("GPL");