opti92x-ad1848.c 25 KB

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