opti92x-ad1848.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*
  2. card-opti92x-ad1848.c - driver for OPTi 82c92x based soundcards.
  3. Copyright (C) 1998-2000 by Massimo Piccioni <dafastidio@libero.it>
  4. Part of this code was developed at the Italian Ministry of Air Defence,
  5. Sixth Division (oh, che pace ...), Rome.
  6. Thanks to Maria Grazia Pollarini, Salvatore Vassallo.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/err.h>
  21. #include <linux/isa.h>
  22. #include <linux/delay.h>
  23. #include <linux/slab.h>
  24. #include <linux/pnp.h>
  25. #include <linux/moduleparam.h>
  26. #include <asm/io.h>
  27. #include <asm/dma.h>
  28. #include <sound/core.h>
  29. #if defined(CS4231) || defined(OPTi93X)
  30. #include <sound/wss.h>
  31. #else
  32. #include <sound/ad1848.h>
  33. #endif /* CS4231 */
  34. #include <sound/mpu401.h>
  35. #include <sound/opl3.h>
  36. #ifndef OPTi93X
  37. #include <sound/opl4.h>
  38. #endif
  39. #define SNDRV_LEGACY_FIND_FREE_IRQ
  40. #define SNDRV_LEGACY_FIND_FREE_DMA
  41. #include <sound/initval.h>
  42. MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
  43. MODULE_LICENSE("GPL");
  44. #ifdef OPTi93X
  45. MODULE_DESCRIPTION("OPTi93X");
  46. MODULE_SUPPORTED_DEVICE("{{OPTi,82C931/3}}");
  47. #else /* OPTi93X */
  48. #ifdef CS4231
  49. MODULE_DESCRIPTION("OPTi92X - CS4231");
  50. MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (CS4231)},"
  51. "{OPTi,82C925 (CS4231)}}");
  52. #else /* CS4231 */
  53. MODULE_DESCRIPTION("OPTi92X - AD1848");
  54. MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (AD1848)},"
  55. "{OPTi,82C925 (AD1848)},"
  56. "{OAK,Mozart}}");
  57. #endif /* CS4231 */
  58. #endif /* OPTi93X */
  59. static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
  60. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  61. //static int enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */
  62. #ifdef CONFIG_PNP
  63. static int isapnp = 1; /* Enable ISA PnP detection */
  64. #endif
  65. static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */
  66. static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */
  67. static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */
  68. static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */
  69. static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */
  70. static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
  71. #if defined(CS4231) || defined(OPTi93X)
  72. static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
  73. #endif /* CS4231 || OPTi93X */
  74. module_param(index, int, 0444);
  75. MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard.");
  76. module_param(id, charp, 0444);
  77. MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard.");
  78. //module_param(enable, bool, 0444);
  79. //MODULE_PARM_DESC(enable, "Enable opti9xx soundcard.");
  80. #ifdef CONFIG_PNP
  81. module_param(isapnp, bool, 0444);
  82. MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
  83. #endif
  84. module_param(port, long, 0444);
  85. MODULE_PARM_DESC(port, "WSS port # for opti9xx driver.");
  86. module_param(mpu_port, long, 0444);
  87. MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver.");
  88. module_param(fm_port, long, 0444);
  89. MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver.");
  90. module_param(irq, int, 0444);
  91. MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver.");
  92. module_param(mpu_irq, int, 0444);
  93. MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver.");
  94. module_param(dma1, int, 0444);
  95. MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver.");
  96. #if defined(CS4231) || defined(OPTi93X)
  97. module_param(dma2, int, 0444);
  98. MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver.");
  99. #endif /* CS4231 || OPTi93X */
  100. #define OPTi9XX_HW_82C928 1
  101. #define OPTi9XX_HW_82C929 2
  102. #define OPTi9XX_HW_82C924 3
  103. #define OPTi9XX_HW_82C925 4
  104. #define OPTi9XX_HW_82C930 5
  105. #define OPTi9XX_HW_82C931 6
  106. #define OPTi9XX_HW_82C933 7
  107. #define OPTi9XX_HW_LAST OPTi9XX_HW_82C933
  108. #define OPTi9XX_MC_REG(n) n
  109. #ifdef OPTi93X
  110. #define OPTi93X_STATUS 0x02
  111. #define OPTi93X_PORT(chip, r) ((chip)->port + OPTi93X_##r)
  112. #define OPTi93X_IRQ_PLAYBACK 0x04
  113. #define OPTi93X_IRQ_CAPTURE 0x08
  114. #endif /* OPTi93X */
  115. struct snd_opti9xx {
  116. unsigned short hardware;
  117. unsigned char password;
  118. char name[7];
  119. unsigned long mc_base;
  120. struct resource *res_mc_base;
  121. unsigned long mc_base_size;
  122. #ifdef OPTi93X
  123. unsigned long mc_indir_index;
  124. struct snd_wss *codec;
  125. #endif /* OPTi93X */
  126. unsigned long pwd_reg;
  127. spinlock_t lock;
  128. long wss_base;
  129. int irq;
  130. int dma1;
  131. #if defined(CS4231) || defined(OPTi93X)
  132. int dma2;
  133. #endif /* CS4231 || OPTi93X */
  134. long fm_port;
  135. long mpu_port;
  136. int mpu_irq;
  137. #ifdef CONFIG_PNP
  138. struct pnp_dev *dev;
  139. struct pnp_dev *devmpu;
  140. #endif /* CONFIG_PNP */
  141. };
  142. static int snd_opti9xx_pnp_is_probed;
  143. #ifdef CONFIG_PNP
  144. static struct pnp_card_device_id snd_opti9xx_pnpids[] = {
  145. #ifndef OPTi93X
  146. /* OPTi 82C924 */
  147. { .id = "OPT0924", .devs = { { "OPT0000" }, { "OPT0002" } }, .driver_data = 0x0924 },
  148. /* OPTi 82C925 */
  149. { .id = "OPT0925", .devs = { { "OPT9250" }, { "OPT0002" } }, .driver_data = 0x0925 },
  150. #else
  151. /* OPTi 82C931/3 */
  152. { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } }, .driver_data = 0x0931 },
  153. #endif /* OPTi93X */
  154. { .id = "" }
  155. };
  156. MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
  157. #endif /* CONFIG_PNP */
  158. #ifdef OPTi93X
  159. #define DEV_NAME "opti93x"
  160. #else
  161. #define DEV_NAME "opti92x"
  162. #endif
  163. static char * snd_opti9xx_names[] = {
  164. "unkown",
  165. "82C928", "82C929",
  166. "82C924", "82C925",
  167. "82C930", "82C931", "82C933"
  168. };
  169. static long __devinit snd_legacy_find_free_ioport(long *port_table, long size)
  170. {
  171. while (*port_table != -1) {
  172. if (request_region(*port_table, size, "ALSA test")) {
  173. release_region(*port_table, size);
  174. return *port_table;
  175. }
  176. port_table++;
  177. }
  178. return -1;
  179. }
  180. static int __devinit snd_opti9xx_init(struct snd_opti9xx *chip,
  181. unsigned short hardware)
  182. {
  183. static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
  184. chip->hardware = hardware;
  185. strcpy(chip->name, snd_opti9xx_names[hardware]);
  186. chip->mc_base_size = opti9xx_mc_size[hardware];
  187. spin_lock_init(&chip->lock);
  188. chip->wss_base = -1;
  189. chip->irq = -1;
  190. chip->dma1 = -1;
  191. #if defined(CS4231) || defined (OPTi93X)
  192. chip->dma2 = -1;
  193. #endif /* CS4231 || OPTi93X */
  194. chip->fm_port = -1;
  195. chip->mpu_port = -1;
  196. chip->mpu_irq = -1;
  197. switch (hardware) {
  198. #ifndef OPTi93X
  199. case OPTi9XX_HW_82C928:
  200. case OPTi9XX_HW_82C929:
  201. chip->mc_base = 0xf8c;
  202. chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3;
  203. chip->pwd_reg = 3;
  204. break;
  205. case OPTi9XX_HW_82C924:
  206. case OPTi9XX_HW_82C925:
  207. chip->mc_base = 0xf8c;
  208. chip->password = 0xe5;
  209. chip->pwd_reg = 3;
  210. break;
  211. #else /* OPTi93X */
  212. case OPTi9XX_HW_82C930:
  213. case OPTi9XX_HW_82C931:
  214. case OPTi9XX_HW_82C933:
  215. chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d;
  216. chip->mc_indir_index = 0xe0e;
  217. chip->password = 0xe4;
  218. chip->pwd_reg = 0;
  219. break;
  220. #endif /* OPTi93X */
  221. default:
  222. snd_printk("chip %d not supported\n", hardware);
  223. return -ENODEV;
  224. }
  225. return 0;
  226. }
  227. static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip,
  228. unsigned char reg)
  229. {
  230. unsigned long flags;
  231. unsigned char retval = 0xff;
  232. spin_lock_irqsave(&chip->lock, flags);
  233. outb(chip->password, chip->mc_base + chip->pwd_reg);
  234. switch (chip->hardware) {
  235. #ifndef OPTi93X
  236. case OPTi9XX_HW_82C924:
  237. case OPTi9XX_HW_82C925:
  238. if (reg > 7) {
  239. outb(reg, chip->mc_base + 8);
  240. outb(chip->password, chip->mc_base + chip->pwd_reg);
  241. retval = inb(chip->mc_base + 9);
  242. break;
  243. }
  244. case OPTi9XX_HW_82C928:
  245. case OPTi9XX_HW_82C929:
  246. retval = inb(chip->mc_base + reg);
  247. break;
  248. #else /* OPTi93X */
  249. case OPTi9XX_HW_82C930:
  250. case OPTi9XX_HW_82C931:
  251. case OPTi9XX_HW_82C933:
  252. outb(reg, chip->mc_indir_index);
  253. outb(chip->password, chip->mc_base + chip->pwd_reg);
  254. retval = inb(chip->mc_indir_index + 1);
  255. break;
  256. #endif /* OPTi93X */
  257. default:
  258. snd_printk("chip %d not supported\n", chip->hardware);
  259. }
  260. spin_unlock_irqrestore(&chip->lock, flags);
  261. return retval;
  262. }
  263. static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg,
  264. unsigned char value)
  265. {
  266. unsigned long flags;
  267. spin_lock_irqsave(&chip->lock, flags);
  268. outb(chip->password, chip->mc_base + chip->pwd_reg);
  269. switch (chip->hardware) {
  270. #ifndef OPTi93X
  271. case OPTi9XX_HW_82C924:
  272. case OPTi9XX_HW_82C925:
  273. if (reg > 7) {
  274. outb(reg, chip->mc_base + 8);
  275. outb(chip->password, chip->mc_base + chip->pwd_reg);
  276. outb(value, chip->mc_base + 9);
  277. break;
  278. }
  279. case OPTi9XX_HW_82C928:
  280. case OPTi9XX_HW_82C929:
  281. outb(value, chip->mc_base + reg);
  282. break;
  283. #else /* OPTi93X */
  284. case OPTi9XX_HW_82C930:
  285. case OPTi9XX_HW_82C931:
  286. case OPTi9XX_HW_82C933:
  287. outb(reg, chip->mc_indir_index);
  288. outb(chip->password, chip->mc_base + chip->pwd_reg);
  289. outb(value, chip->mc_indir_index + 1);
  290. break;
  291. #endif /* OPTi93X */
  292. default:
  293. snd_printk("chip %d not supported\n", chip->hardware);
  294. }
  295. spin_unlock_irqrestore(&chip->lock, flags);
  296. }
  297. #define snd_opti9xx_write_mask(chip, reg, value, mask) \
  298. snd_opti9xx_write(chip, reg, \
  299. (snd_opti9xx_read(chip, reg) & ~(mask)) | ((value) & (mask)))
  300. static int __devinit snd_opti9xx_configure(struct snd_opti9xx *chip)
  301. {
  302. unsigned char wss_base_bits;
  303. unsigned char irq_bits;
  304. unsigned char dma_bits;
  305. unsigned char mpu_port_bits = 0;
  306. unsigned char mpu_irq_bits;
  307. switch (chip->hardware) {
  308. #ifndef OPTi93X
  309. case OPTi9XX_HW_82C924:
  310. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
  311. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
  312. case OPTi9XX_HW_82C925:
  313. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
  314. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
  315. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
  316. #ifdef CS4231
  317. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
  318. #else
  319. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
  320. #endif /* CS4231 */
  321. break;
  322. case OPTi9XX_HW_82C928:
  323. case OPTi9XX_HW_82C929:
  324. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
  325. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
  326. /*
  327. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
  328. */
  329. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
  330. #ifdef CS4231
  331. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
  332. #else
  333. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
  334. #endif /* CS4231 */
  335. break;
  336. #else /* OPTi93X */
  337. case OPTi9XX_HW_82C931:
  338. case OPTi9XX_HW_82C933:
  339. /*
  340. * The BTC 1817DW has QS1000 wavetable which is connected
  341. * to the serial digital input of the OPTI931.
  342. */
  343. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff);
  344. /*
  345. * This bit sets OPTI931 to automaticaly select FM
  346. * or digital input signal.
  347. */
  348. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01);
  349. case OPTi9XX_HW_82C930: /* FALL THROUGH */
  350. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
  351. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
  352. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
  353. (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
  354. 0x34);
  355. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
  356. break;
  357. #endif /* OPTi93X */
  358. default:
  359. snd_printk("chip %d not supported\n", chip->hardware);
  360. return -EINVAL;
  361. }
  362. switch (chip->wss_base) {
  363. case 0x530:
  364. wss_base_bits = 0x00;
  365. break;
  366. case 0x604:
  367. wss_base_bits = 0x03;
  368. break;
  369. case 0xe80:
  370. wss_base_bits = 0x01;
  371. break;
  372. case 0xf40:
  373. wss_base_bits = 0x02;
  374. break;
  375. default:
  376. snd_printk("WSS port 0x%lx not valid\n", chip->wss_base);
  377. goto __skip_base;
  378. }
  379. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
  380. __skip_base:
  381. switch (chip->irq) {
  382. //#ifdef OPTi93X
  383. case 5:
  384. irq_bits = 0x05;
  385. break;
  386. //#endif /* OPTi93X */
  387. case 7:
  388. irq_bits = 0x01;
  389. break;
  390. case 9:
  391. irq_bits = 0x02;
  392. break;
  393. case 10:
  394. irq_bits = 0x03;
  395. break;
  396. case 11:
  397. irq_bits = 0x04;
  398. break;
  399. default:
  400. snd_printk("WSS irq # %d not valid\n", chip->irq);
  401. goto __skip_resources;
  402. }
  403. switch (chip->dma1) {
  404. case 0:
  405. dma_bits = 0x01;
  406. break;
  407. case 1:
  408. dma_bits = 0x02;
  409. break;
  410. case 3:
  411. dma_bits = 0x03;
  412. break;
  413. default:
  414. snd_printk("WSS dma1 # %d not valid\n", chip->dma1);
  415. goto __skip_resources;
  416. }
  417. #if defined(CS4231) || defined(OPTi93X)
  418. if (chip->dma1 == chip->dma2) {
  419. snd_printk("don't want to share dmas\n");
  420. return -EBUSY;
  421. }
  422. switch (chip->dma2) {
  423. case 0:
  424. case 1:
  425. break;
  426. default:
  427. snd_printk("WSS dma2 # %d not valid\n", chip->dma2);
  428. goto __skip_resources;
  429. }
  430. dma_bits |= 0x04;
  431. #endif /* CS4231 || OPTi93X */
  432. #ifndef OPTi93X
  433. outb(irq_bits << 3 | dma_bits, chip->wss_base);
  434. #else /* OPTi93X */
  435. snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
  436. #endif /* OPTi93X */
  437. __skip_resources:
  438. if (chip->hardware > OPTi9XX_HW_82C928) {
  439. switch (chip->mpu_port) {
  440. case 0:
  441. case -1:
  442. break;
  443. case 0x300:
  444. mpu_port_bits = 0x03;
  445. break;
  446. case 0x310:
  447. mpu_port_bits = 0x02;
  448. break;
  449. case 0x320:
  450. mpu_port_bits = 0x01;
  451. break;
  452. case 0x330:
  453. mpu_port_bits = 0x00;
  454. break;
  455. default:
  456. snd_printk("MPU-401 port 0x%lx not valid\n",
  457. chip->mpu_port);
  458. goto __skip_mpu;
  459. }
  460. switch (chip->mpu_irq) {
  461. case 5:
  462. mpu_irq_bits = 0x02;
  463. break;
  464. case 7:
  465. mpu_irq_bits = 0x03;
  466. break;
  467. case 9:
  468. mpu_irq_bits = 0x00;
  469. break;
  470. case 10:
  471. mpu_irq_bits = 0x01;
  472. break;
  473. default:
  474. snd_printk("MPU-401 irq # %d not valid\n",
  475. chip->mpu_irq);
  476. goto __skip_mpu;
  477. }
  478. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
  479. (chip->mpu_port <= 0) ? 0x00 :
  480. 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
  481. 0xf8);
  482. }
  483. __skip_mpu:
  484. return 0;
  485. }
  486. #ifdef OPTi93X
  487. static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
  488. {
  489. struct snd_wss *codec = dev_id;
  490. struct snd_opti9xx *chip = codec->card->private_data;
  491. unsigned char status;
  492. status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11));
  493. if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
  494. snd_pcm_period_elapsed(codec->playback_substream);
  495. if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
  496. snd_wss_overrange(codec);
  497. snd_pcm_period_elapsed(codec->capture_substream);
  498. }
  499. outb(0x00, OPTi93X_PORT(codec, STATUS));
  500. return IRQ_HANDLED;
  501. }
  502. #endif /* OPTi93X */
  503. static int __devinit snd_card_opti9xx_detect(struct snd_card *card,
  504. struct snd_opti9xx *chip)
  505. {
  506. int i, err;
  507. #ifndef OPTi93X
  508. for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
  509. unsigned char value;
  510. if ((err = snd_opti9xx_init(chip, i)) < 0)
  511. return err;
  512. if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
  513. continue;
  514. value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
  515. if ((value != 0xff) && (value != inb(chip->mc_base + 1)))
  516. if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
  517. return 1;
  518. release_and_free_resource(chip->res_mc_base);
  519. chip->res_mc_base = NULL;
  520. }
  521. #else /* OPTi93X */
  522. for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
  523. unsigned long flags;
  524. unsigned char value;
  525. if ((err = snd_opti9xx_init(chip, i)) < 0)
  526. return err;
  527. if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
  528. continue;
  529. spin_lock_irqsave(&chip->lock, flags);
  530. outb(chip->password, chip->mc_base + chip->pwd_reg);
  531. outb(((chip->mc_indir_index & (1 << 8)) >> 4) |
  532. ((chip->mc_indir_index & 0xf0) >> 4), chip->mc_base);
  533. spin_unlock_irqrestore(&chip->lock, flags);
  534. value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
  535. snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
  536. if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
  537. return 1;
  538. release_and_free_resource(chip->res_mc_base);
  539. chip->res_mc_base = NULL;
  540. }
  541. #endif /* OPTi93X */
  542. return -ENODEV;
  543. }
  544. #ifdef CONFIG_PNP
  545. static int __devinit snd_card_opti9xx_pnp(struct snd_opti9xx *chip,
  546. struct pnp_card_link *card,
  547. const struct pnp_card_device_id *pid)
  548. {
  549. struct pnp_dev *pdev;
  550. int err;
  551. chip->dev = pnp_request_card_device(card, pid->devs[0].id, NULL);
  552. if (chip->dev == NULL)
  553. return -EBUSY;
  554. chip->devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
  555. pdev = chip->dev;
  556. err = pnp_activate_dev(pdev);
  557. if (err < 0) {
  558. snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
  559. return err;
  560. }
  561. #ifdef OPTi93X
  562. port = pnp_port_start(pdev, 0) - 4;
  563. fm_port = pnp_port_start(pdev, 1) + 8;
  564. #else
  565. if (pid->driver_data != 0x0924)
  566. port = pnp_port_start(pdev, 1);
  567. fm_port = pnp_port_start(pdev, 2) + 8;
  568. #endif /* OPTi93X */
  569. irq = pnp_irq(pdev, 0);
  570. dma1 = pnp_dma(pdev, 0);
  571. #if defined(CS4231) || defined(OPTi93X)
  572. dma2 = pnp_dma(pdev, 1);
  573. #endif /* CS4231 || OPTi93X */
  574. pdev = chip->devmpu;
  575. if (pdev && mpu_port > 0) {
  576. err = pnp_activate_dev(pdev);
  577. if (err < 0) {
  578. snd_printk(KERN_ERR "AUDIO pnp configure failure\n");
  579. mpu_port = -1;
  580. chip->devmpu = NULL;
  581. } else {
  582. mpu_port = pnp_port_start(pdev, 0);
  583. mpu_irq = pnp_irq(pdev, 0);
  584. }
  585. }
  586. return pid->driver_data;
  587. }
  588. #endif /* CONFIG_PNP */
  589. static void snd_card_opti9xx_free(struct snd_card *card)
  590. {
  591. struct snd_opti9xx *chip = card->private_data;
  592. if (chip) {
  593. #ifdef OPTi93X
  594. struct snd_wss *codec = chip->codec;
  595. if (codec && codec->irq > 0) {
  596. disable_irq(codec->irq);
  597. free_irq(codec->irq, codec);
  598. }
  599. #endif
  600. release_and_free_resource(chip->res_mc_base);
  601. }
  602. }
  603. static int __devinit snd_opti9xx_probe(struct snd_card *card)
  604. {
  605. static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
  606. int error;
  607. struct snd_opti9xx *chip = card->private_data;
  608. struct snd_wss *codec;
  609. #ifdef CS4231
  610. struct snd_timer *timer;
  611. #endif
  612. struct snd_pcm *pcm;
  613. struct snd_rawmidi *rmidi;
  614. struct snd_hwdep *synth;
  615. if (! chip->res_mc_base &&
  616. (chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
  617. "OPTi9xx MC")) == NULL)
  618. return -ENOMEM;
  619. chip->wss_base = port;
  620. chip->fm_port = fm_port;
  621. chip->mpu_port = mpu_port;
  622. chip->irq = irq;
  623. chip->mpu_irq = mpu_irq;
  624. chip->dma1 = dma1;
  625. #if defined(CS4231) || defined(OPTi93X)
  626. chip->dma2 = dma2;
  627. #endif
  628. if (chip->wss_base == SNDRV_AUTO_PORT) {
  629. chip->wss_base = snd_legacy_find_free_ioport(possible_ports, 4);
  630. if (chip->wss_base < 0) {
  631. snd_printk("unable to find a free WSS port\n");
  632. return -EBUSY;
  633. }
  634. }
  635. error = snd_opti9xx_configure(chip);
  636. if (error)
  637. return error;
  638. #if defined(CS4231) || defined(OPTi93X)
  639. error = snd_wss_create(card, chip->wss_base + 4, -1,
  640. chip->irq, chip->dma1, chip->dma2,
  641. #ifdef CS4231
  642. WSS_HW_DETECT, 0,
  643. #else /* OPTi93x */
  644. WSS_HW_OPTI93X, WSS_HWSHARE_IRQ,
  645. #endif
  646. &codec);
  647. if (error < 0)
  648. return error;
  649. #ifdef OPTi93X
  650. chip->codec = codec;
  651. #endif
  652. #else
  653. error = snd_ad1848_create(card, chip->wss_base + 4, chip->irq,
  654. chip->dma1, WSS_HW_DETECT, &codec);
  655. if (error < 0)
  656. return error;
  657. #endif
  658. error = snd_wss_pcm(codec, 0, &pcm);
  659. if (error < 0)
  660. return error;
  661. error = snd_wss_mixer(codec);
  662. if (error < 0)
  663. return error;
  664. #ifdef CS4231
  665. error = snd_wss_timer(codec, 0, &timer);
  666. if (error < 0)
  667. return error;
  668. #endif
  669. #ifdef OPTi93X
  670. error = request_irq(chip->irq, snd_opti93x_interrupt,
  671. IRQF_DISABLED, DEV_NAME" - WSS", codec);
  672. if (error < 0) {
  673. snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", chip->irq);
  674. return error;
  675. }
  676. #endif
  677. strcpy(card->driver, chip->name);
  678. sprintf(card->shortname, "OPTi %s", card->driver);
  679. #if defined(CS4231) || defined(OPTi93X)
  680. sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
  681. card->shortname, pcm->name, chip->wss_base + 4,
  682. chip->irq, chip->dma1, chip->dma2);
  683. #else
  684. sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
  685. card->shortname, pcm->name, chip->wss_base + 4,
  686. chip->irq, chip->dma1);
  687. #endif /* CS4231 || OPTi93X */
  688. if (chip->mpu_port <= 0 || chip->mpu_port == SNDRV_AUTO_PORT)
  689. rmidi = NULL;
  690. else
  691. if ((error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
  692. chip->mpu_port, 0, chip->mpu_irq, IRQF_DISABLED,
  693. &rmidi)))
  694. snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
  695. chip->mpu_port);
  696. if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
  697. struct snd_opl3 *opl3 = NULL;
  698. #ifndef OPTi93X
  699. if (chip->hardware == OPTi9XX_HW_82C928 ||
  700. chip->hardware == OPTi9XX_HW_82C929 ||
  701. chip->hardware == OPTi9XX_HW_82C924) {
  702. struct snd_opl4 *opl4;
  703. /* assume we have an OPL4 */
  704. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
  705. 0x20, 0x20);
  706. if (snd_opl4_create(card,
  707. chip->fm_port,
  708. chip->fm_port - 8,
  709. 2, &opl3, &opl4) < 0) {
  710. /* no luck, use OPL3 instead */
  711. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
  712. 0x00, 0x20);
  713. }
  714. }
  715. #endif /* !OPTi93X */
  716. if (!opl3 && snd_opl3_create(card,
  717. chip->fm_port,
  718. chip->fm_port + 2,
  719. OPL3_HW_AUTO, 0, &opl3) < 0) {
  720. snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
  721. chip->fm_port, chip->fm_port + 4 - 1);
  722. }
  723. if (opl3) {
  724. #ifdef CS4231
  725. const int t1dev = 1;
  726. #else
  727. const int t1dev = 0;
  728. #endif
  729. if ((error = snd_opl3_timer_new(opl3, t1dev, t1dev+1)) < 0)
  730. return error;
  731. if ((error = snd_opl3_hwdep_new(opl3, 0, 1, &synth)) < 0)
  732. return error;
  733. }
  734. }
  735. return snd_card_register(card);
  736. }
  737. static struct snd_card *snd_opti9xx_card_new(void)
  738. {
  739. struct snd_card *card;
  740. card = snd_card_new(index, id, THIS_MODULE, sizeof(struct snd_opti9xx));
  741. if (! card)
  742. return NULL;
  743. card->private_free = snd_card_opti9xx_free;
  744. return card;
  745. }
  746. static int __devinit snd_opti9xx_isa_match(struct device *devptr,
  747. unsigned int dev)
  748. {
  749. #ifdef CONFIG_PNP
  750. if (snd_opti9xx_pnp_is_probed)
  751. return 0;
  752. if (isapnp)
  753. return 0;
  754. #endif
  755. return 1;
  756. }
  757. static int __devinit snd_opti9xx_isa_probe(struct device *devptr,
  758. unsigned int dev)
  759. {
  760. struct snd_card *card;
  761. int error;
  762. static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
  763. #ifdef OPTi93X
  764. static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
  765. #else
  766. static int possible_irqs[] = {9, 10, 11, 7, -1};
  767. #endif /* OPTi93X */
  768. static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
  769. static int possible_dma1s[] = {3, 1, 0, -1};
  770. #if defined(CS4231) || defined(OPTi93X)
  771. static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
  772. #endif /* CS4231 || OPTi93X */
  773. if (mpu_port == SNDRV_AUTO_PORT) {
  774. if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
  775. snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
  776. return -EBUSY;
  777. }
  778. }
  779. if (irq == SNDRV_AUTO_IRQ) {
  780. if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
  781. snd_printk(KERN_ERR "unable to find a free IRQ\n");
  782. return -EBUSY;
  783. }
  784. }
  785. if (mpu_irq == SNDRV_AUTO_IRQ) {
  786. if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
  787. snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
  788. return -EBUSY;
  789. }
  790. }
  791. if (dma1 == SNDRV_AUTO_DMA) {
  792. if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
  793. snd_printk(KERN_ERR "unable to find a free DMA1\n");
  794. return -EBUSY;
  795. }
  796. }
  797. #if defined(CS4231) || defined(OPTi93X)
  798. if (dma2 == SNDRV_AUTO_DMA) {
  799. if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
  800. snd_printk("unable to find a free DMA2\n");
  801. return -EBUSY;
  802. }
  803. }
  804. #endif
  805. card = snd_opti9xx_card_new();
  806. if (! card)
  807. return -ENOMEM;
  808. if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
  809. snd_card_free(card);
  810. return error;
  811. }
  812. snd_card_set_dev(card, devptr);
  813. if ((error = snd_opti9xx_probe(card)) < 0) {
  814. snd_card_free(card);
  815. return error;
  816. }
  817. dev_set_drvdata(devptr, card);
  818. return 0;
  819. }
  820. static int __devexit snd_opti9xx_isa_remove(struct device *devptr,
  821. unsigned int dev)
  822. {
  823. snd_card_free(dev_get_drvdata(devptr));
  824. dev_set_drvdata(devptr, NULL);
  825. return 0;
  826. }
  827. static struct isa_driver snd_opti9xx_driver = {
  828. .match = snd_opti9xx_isa_match,
  829. .probe = snd_opti9xx_isa_probe,
  830. .remove = __devexit_p(snd_opti9xx_isa_remove),
  831. /* FIXME: suspend/resume */
  832. .driver = {
  833. .name = DEV_NAME
  834. },
  835. };
  836. #ifdef CONFIG_PNP
  837. static int __devinit snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
  838. const struct pnp_card_device_id *pid)
  839. {
  840. struct snd_card *card;
  841. int error, hw;
  842. struct snd_opti9xx *chip;
  843. if (snd_opti9xx_pnp_is_probed)
  844. return -EBUSY;
  845. if (! isapnp)
  846. return -ENODEV;
  847. card = snd_opti9xx_card_new();
  848. if (! card)
  849. return -ENOMEM;
  850. chip = card->private_data;
  851. hw = snd_card_opti9xx_pnp(chip, pcard, pid);
  852. switch (hw) {
  853. case 0x0924:
  854. hw = OPTi9XX_HW_82C924;
  855. break;
  856. case 0x0925:
  857. hw = OPTi9XX_HW_82C925;
  858. break;
  859. case 0x0931:
  860. hw = OPTi9XX_HW_82C931;
  861. break;
  862. default:
  863. snd_card_free(card);
  864. return -ENODEV;
  865. }
  866. if ((error = snd_opti9xx_init(chip, hw))) {
  867. snd_card_free(card);
  868. return error;
  869. }
  870. if (hw <= OPTi9XX_HW_82C930)
  871. chip->mc_base -= 0x80;
  872. snd_card_set_dev(card, &pcard->card->dev);
  873. if ((error = snd_opti9xx_probe(card)) < 0) {
  874. snd_card_free(card);
  875. return error;
  876. }
  877. pnp_set_card_drvdata(pcard, card);
  878. snd_opti9xx_pnp_is_probed = 1;
  879. return 0;
  880. }
  881. static void __devexit snd_opti9xx_pnp_remove(struct pnp_card_link * pcard)
  882. {
  883. snd_card_free(pnp_get_card_drvdata(pcard));
  884. pnp_set_card_drvdata(pcard, NULL);
  885. snd_opti9xx_pnp_is_probed = 0;
  886. }
  887. static struct pnp_card_driver opti9xx_pnpc_driver = {
  888. .flags = PNP_DRIVER_RES_DISABLE,
  889. .name = "opti9xx",
  890. .id_table = snd_opti9xx_pnpids,
  891. .probe = snd_opti9xx_pnp_probe,
  892. .remove = __devexit_p(snd_opti9xx_pnp_remove),
  893. };
  894. #endif
  895. #ifdef OPTi93X
  896. #define CHIP_NAME "82C93x"
  897. #else
  898. #define CHIP_NAME "82C92x"
  899. #endif
  900. static int __init alsa_card_opti9xx_init(void)
  901. {
  902. #ifdef CONFIG_PNP
  903. pnp_register_card_driver(&opti9xx_pnpc_driver);
  904. if (snd_opti9xx_pnp_is_probed)
  905. return 0;
  906. pnp_unregister_card_driver(&opti9xx_pnpc_driver);
  907. #endif
  908. return isa_register_driver(&snd_opti9xx_driver, 1);
  909. }
  910. static void __exit alsa_card_opti9xx_exit(void)
  911. {
  912. if (!snd_opti9xx_pnp_is_probed) {
  913. isa_unregister_driver(&snd_opti9xx_driver);
  914. return;
  915. }
  916. #ifdef CONFIG_PNP
  917. pnp_unregister_card_driver(&opti9xx_pnpc_driver);
  918. #endif
  919. }
  920. module_init(alsa_card_opti9xx_init)
  921. module_exit(alsa_card_opti9xx_exit)