opti92x-ad1848.c 25 KB

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