opl3sa2.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * sound/opl3sa2.c
  3. *
  4. * A low level driver for Yamaha OPL3-SA2 and SA3 cards.
  5. * NOTE: All traces of the name OPL3-SAx have now (December 2000) been
  6. * removed from the driver code, as an email exchange with Yamaha
  7. * provided the information that the YMF-719 is indeed just a
  8. * re-badged 715.
  9. *
  10. * Copyright 1998-2001 Scott Murray <scott@spiteful.org>
  11. *
  12. * Originally based on the CS4232 driver (in cs4232.c) by Hannu Savolainen
  13. * and others. Now incorporates code/ideas from pss.c, also by Hannu
  14. * Savolainen. Both of those files are distributed with the following
  15. * license:
  16. *
  17. * "Copyright (C) by Hannu Savolainen 1993-1997
  18. *
  19. * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  20. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  21. * for more info."
  22. *
  23. * As such, in accordance with the above license, this file, opl3sa2.c, is
  24. * distributed under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2 (June 1991).
  25. * See the "COPYING" file distributed with this software for more information.
  26. *
  27. * Change History
  28. * --------------
  29. * Scott Murray Original driver (Jun 14, 1998)
  30. * Paul J.Y. Lahaie Changed probing / attach code order
  31. * Scott Murray Added mixer support (Dec 03, 1998)
  32. * Scott Murray Changed detection code to be more forgiving,
  33. * added force option as last resort,
  34. * fixed ioctl return values. (Dec 30, 1998)
  35. * Scott Murray Simpler detection code should work all the time now
  36. * (with thanks to Ben Hutchings for the heuristic),
  37. * removed now unnecessary force option. (Jan 5, 1999)
  38. * Christoph Hellwig Adapted to module_init/module_exit (Mar 4, 2000)
  39. * Scott Murray Reworked SA2 versus SA3 mixer code, updated chipset
  40. * version detection code (again!). (Dec 5, 2000)
  41. * Scott Murray Adjusted master volume mixer scaling. (Dec 6, 2000)
  42. * Scott Murray Based on a patch by Joel Yliluoma (aka Bisqwit),
  43. * integrated wide mixer and adjusted mic, bass, treble
  44. * scaling. (Dec 6, 2000)
  45. * Scott Murray Based on a patch by Peter Englmaier, integrated
  46. * ymode and loopback options. (Dec 6, 2000)
  47. * Scott Murray Inspired by a patch by Peter Englmaier, and based on
  48. * what ALSA does, added initialization code for the
  49. * default DMA and IRQ settings. (Dec 6, 2000)
  50. * Scott Murray Added some more checks to the card detection code,
  51. * based on what ALSA does. (Dec 12, 2000)
  52. * Scott Murray Inspired by similar patches from John Fremlin,
  53. * Jim Radford, Mike Rolig, and Ingmar Steen, added 2.4
  54. * ISA PnP API support, mainly based on bits from
  55. * sb_card.c and awe_wave.c. (Dec 12, 2000)
  56. * Scott Murray Some small cleanups to the init code output.
  57. * (Jan 7, 2001)
  58. * Zwane Mwaikambo Added PM support. (Dec 4 2001)
  59. *
  60. * Adam Belay Converted driver to new PnP Layer (Oct 12, 2002)
  61. * Zwane Mwaikambo Code, data structure cleanups. (Feb 15 2002)
  62. * Zwane Mwaikambo Free resources during auxiliary device probe
  63. * failures (Apr 29 2002)
  64. *
  65. */
  66. #include <linux/config.h>
  67. #include <linux/pnp.h>
  68. #include <linux/init.h>
  69. #include <linux/module.h>
  70. #include <linux/delay.h>
  71. #include "sound_config.h"
  72. #include "ad1848.h"
  73. #include "mpu401.h"
  74. #define OPL3SA2_MODULE_NAME "opl3sa2"
  75. #define PFX OPL3SA2_MODULE_NAME ": "
  76. /* Useful control port indexes: */
  77. #define OPL3SA2_PM 0x01
  78. #define OPL3SA2_SYS_CTRL 0x02
  79. #define OPL3SA2_IRQ_CONFIG 0x03
  80. #define OPL3SA2_DMA_CONFIG 0x06
  81. #define OPL3SA2_MASTER_LEFT 0x07
  82. #define OPL3SA2_MASTER_RIGHT 0x08
  83. #define OPL3SA2_MIC 0x09
  84. #define OPL3SA2_MISC 0x0A
  85. #define OPL3SA3_WIDE 0x14
  86. #define OPL3SA3_BASS 0x15
  87. #define OPL3SA3_TREBLE 0x16
  88. /* Useful constants: */
  89. #define DEFAULT_VOLUME 50
  90. #define DEFAULT_MIC 50
  91. #define DEFAULT_TIMBRE 0
  92. /* Power saving modes */
  93. #define OPL3SA2_PM_MODE0 0x00
  94. #define OPL3SA2_PM_MODE1 0x04 /* PSV */
  95. #define OPL3SA2_PM_MODE2 0x05 /* PSV | PDX */
  96. #define OPL3SA2_PM_MODE3 0x27 /* ADOWN | PSV | PDN | PDX */
  97. /* For checking against what the card returns: */
  98. #define VERSION_UNKNOWN 0
  99. #define VERSION_YMF711 1
  100. #define VERSION_YMF715 2
  101. #define VERSION_YMF715B 3
  102. #define VERSION_YMF715E 4
  103. /* also assuming that anything > 4 but <= 7 is a 715E */
  104. /* Chipset type constants for use below */
  105. #define CHIPSET_UNKNOWN -1
  106. #define CHIPSET_OPL3SA2 0
  107. #define CHIPSET_OPL3SA3 1
  108. static const char *CHIPSET_TABLE[] = {"OPL3-SA2", "OPL3-SA3"};
  109. #ifdef CONFIG_PNP
  110. #define OPL3SA2_CARDS_MAX 4
  111. #else
  112. #define OPL3SA2_CARDS_MAX 1
  113. #endif
  114. /* This should be pretty obvious */
  115. static int opl3sa2_cards_num;
  116. typedef struct {
  117. /* device resources */
  118. unsigned short cfg_port;
  119. struct address_info cfg;
  120. struct address_info cfg_mss;
  121. struct address_info cfg_mpu;
  122. #ifdef CONFIG_PNP
  123. /* PnP Stuff */
  124. struct pnp_dev* pdev;
  125. int activated; /* Whether said devices have been activated */
  126. #endif
  127. unsigned int card;
  128. int chipset; /* What's my version(s)? */
  129. char *chipset_name;
  130. /* mixer data */
  131. int mixer;
  132. unsigned int volume_l;
  133. unsigned int volume_r;
  134. unsigned int mic;
  135. unsigned int bass_l;
  136. unsigned int bass_r;
  137. unsigned int treble_l;
  138. unsigned int treble_r;
  139. unsigned int wide_l;
  140. unsigned int wide_r;
  141. } opl3sa2_state_t;
  142. static opl3sa2_state_t opl3sa2_state[OPL3SA2_CARDS_MAX];
  143. /* Our parameters */
  144. static int __initdata io = -1;
  145. static int __initdata mss_io = -1;
  146. static int __initdata mpu_io = -1;
  147. static int __initdata irq = -1;
  148. static int __initdata dma = -1;
  149. static int __initdata dma2 = -1;
  150. static int __initdata ymode = -1;
  151. static int __initdata loopback = -1;
  152. #ifdef CONFIG_PNP
  153. /* PnP specific parameters */
  154. static int __initdata isapnp = 1;
  155. static int __initdata multiple = 1;
  156. /* Whether said devices have been activated */
  157. static int opl3sa2_activated[OPL3SA2_CARDS_MAX];
  158. #else
  159. static int __initdata isapnp; /* = 0 */
  160. static int __initdata multiple; /* = 0 */
  161. #endif
  162. MODULE_DESCRIPTION("Module for OPL3-SA2 and SA3 sound cards (uses AD1848 MSS driver).");
  163. MODULE_AUTHOR("Scott Murray <scott@spiteful.org>");
  164. MODULE_LICENSE("GPL");
  165. module_param(io, int, 0);
  166. MODULE_PARM_DESC(io, "Set I/O base of OPL3-SA2 or SA3 card (usually 0x370. Address must be even and must be from 0x100 to 0xFFE)");
  167. module_param(mss_io, int, 0);
  168. MODULE_PARM_DESC(mss_io, "Set MSS (audio) I/O base (0x530, 0xE80, or other. Address must end in 0 or 4 and must be from 0x530 to 0xF48)");
  169. module_param(mpu_io, int, 0);
  170. MODULE_PARM_DESC(mpu_io, "Set MIDI I/O base (0x330 or other. Address must be even and must be from 0x300 to 0x334)");
  171. module_param(irq, int, 0);
  172. MODULE_PARM_DESC(irq, "Set MSS (audio) IRQ (5, 7, 9, 10, 11, 12)");
  173. module_param(dma, int, 0);
  174. MODULE_PARM_DESC(dma, "Set MSS (audio) first DMA channel (0, 1, 3)");
  175. module_param(dma2, int, 0);
  176. MODULE_PARM_DESC(dma2, "Set MSS (audio) second DMA channel (0, 1, 3)");
  177. module_param(ymode, int, 0);
  178. MODULE_PARM_DESC(ymode, "Set Yamaha 3D enhancement mode (0 = Desktop/Normal, 1 = Notebook PC (1), 2 = Notebook PC (2), 3 = Hi-Fi)");
  179. module_param(loopback, int, 0);
  180. MODULE_PARM_DESC(loopback, "Set A/D input source. Useful for echo cancellation (0 = Mic Rch (default), 1 = Mono output loopback)");
  181. #ifdef CONFIG_PNP
  182. module_param(isapnp, bool, 0);
  183. MODULE_PARM_DESC(isapnp, "When set to 0, ISA PnP support will be disabled");
  184. module_param(multiple, bool, 0);
  185. MODULE_PARM_DESC(multiple, "When set to 0, will not search for multiple cards");
  186. #endif
  187. /*
  188. * Standard read and write functions
  189. */
  190. static inline void opl3sa2_write(unsigned short port,
  191. unsigned char index,
  192. unsigned char data)
  193. {
  194. outb_p(index, port);
  195. outb(data, port + 1);
  196. }
  197. static inline void opl3sa2_read(unsigned short port,
  198. unsigned char index,
  199. unsigned char* data)
  200. {
  201. outb_p(index, port);
  202. *data = inb(port + 1);
  203. }
  204. /*
  205. * All of the mixer functions...
  206. */
  207. static void opl3sa2_set_volume(opl3sa2_state_t* devc, int left, int right)
  208. {
  209. static unsigned char scale[101] = {
  210. 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e,
  211. 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0c,
  212. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b,
  213. 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x09, 0x09,
  214. 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08, 0x08,
  215. 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
  216. 0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  217. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
  218. 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01,
  219. 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  220. 0x00
  221. };
  222. unsigned char vol;
  223. vol = scale[left];
  224. /* If level is zero, turn on mute */
  225. if(!left)
  226. vol |= 0x80;
  227. opl3sa2_write(devc->cfg_port, OPL3SA2_MASTER_LEFT, vol);
  228. vol = scale[right];
  229. /* If level is zero, turn on mute */
  230. if(!right)
  231. vol |= 0x80;
  232. opl3sa2_write(devc->cfg_port, OPL3SA2_MASTER_RIGHT, vol);
  233. }
  234. static void opl3sa2_set_mic(opl3sa2_state_t* devc, int level)
  235. {
  236. unsigned char vol = 0x1F;
  237. if((level >= 0) && (level <= 100))
  238. vol = 0x1F - (unsigned char) (32 * level / 101);
  239. /* If level is zero, turn on mute */
  240. if(!level)
  241. vol |= 0x80;
  242. opl3sa2_write(devc->cfg_port, OPL3SA2_MIC, vol);
  243. }
  244. static void opl3sa3_set_bass(opl3sa2_state_t* devc, int left, int right)
  245. {
  246. unsigned char bass;
  247. bass = left ? ((unsigned char) (8 * left / 101)) : 0;
  248. bass |= (right ? ((unsigned char) (8 * right / 101)) : 0) << 4;
  249. opl3sa2_write(devc->cfg_port, OPL3SA3_BASS, bass);
  250. }
  251. static void opl3sa3_set_treble(opl3sa2_state_t* devc, int left, int right)
  252. {
  253. unsigned char treble;
  254. treble = left ? ((unsigned char) (8 * left / 101)) : 0;
  255. treble |= (right ? ((unsigned char) (8 * right / 101)) : 0) << 4;
  256. opl3sa2_write(devc->cfg_port, OPL3SA3_TREBLE, treble);
  257. }
  258. static void opl3sa2_mixer_reset(opl3sa2_state_t* devc)
  259. {
  260. if (devc) {
  261. opl3sa2_set_volume(devc, DEFAULT_VOLUME, DEFAULT_VOLUME);
  262. devc->volume_l = devc->volume_r = DEFAULT_VOLUME;
  263. opl3sa2_set_mic(devc, DEFAULT_MIC);
  264. devc->mic = DEFAULT_MIC;
  265. if (devc->chipset == CHIPSET_OPL3SA3) {
  266. opl3sa3_set_bass(devc, DEFAULT_TIMBRE, DEFAULT_TIMBRE);
  267. devc->bass_l = devc->bass_r = DEFAULT_TIMBRE;
  268. opl3sa3_set_treble(devc, DEFAULT_TIMBRE, DEFAULT_TIMBRE);
  269. devc->treble_l = devc->treble_r = DEFAULT_TIMBRE;
  270. }
  271. }
  272. }
  273. static inline void arg_to_vol_mono(unsigned int vol, int* value)
  274. {
  275. int left;
  276. left = vol & 0x00ff;
  277. if (left > 100)
  278. left = 100;
  279. *value = left;
  280. }
  281. static inline void arg_to_vol_stereo(unsigned int vol, int* aleft, int* aright)
  282. {
  283. arg_to_vol_mono(vol, aleft);
  284. arg_to_vol_mono(vol >> 8, aright);
  285. }
  286. static inline int ret_vol_mono(int vol)
  287. {
  288. return ((vol << 8) | vol);
  289. }
  290. static inline int ret_vol_stereo(int left, int right)
  291. {
  292. return ((right << 8) | left);
  293. }
  294. static int opl3sa2_mixer_ioctl(int dev, unsigned int cmd, void __user *arg)
  295. {
  296. int retval, value, cmdf = cmd & 0xff;
  297. int __user *p = (int __user *)arg;
  298. opl3sa2_state_t* devc = &opl3sa2_state[dev];
  299. switch (cmdf) {
  300. case SOUND_MIXER_VOLUME:
  301. case SOUND_MIXER_MIC:
  302. case SOUND_MIXER_DEVMASK:
  303. case SOUND_MIXER_STEREODEVS:
  304. case SOUND_MIXER_RECMASK:
  305. case SOUND_MIXER_RECSRC:
  306. case SOUND_MIXER_CAPS:
  307. break;
  308. default:
  309. return -EINVAL;
  310. }
  311. if (((cmd >> 8) & 0xff) != 'M')
  312. return -EINVAL;
  313. retval = 0;
  314. if (_SIOC_DIR (cmd) & _SIOC_WRITE) {
  315. switch (cmdf) {
  316. case SOUND_MIXER_VOLUME:
  317. retval = get_user(value, (unsigned __user *) arg);
  318. if (retval)
  319. break;
  320. arg_to_vol_stereo(value, &devc->volume_l, &devc->volume_r);
  321. opl3sa2_set_volume(devc, devc->volume_l, devc->volume_r);
  322. value = ret_vol_stereo(devc->volume_l, devc->volume_r);
  323. retval = put_user(value, p);
  324. break;
  325. case SOUND_MIXER_MIC:
  326. retval = get_user(value, (unsigned __user *) arg);
  327. if (retval)
  328. break;
  329. arg_to_vol_mono(value, &devc->mic);
  330. opl3sa2_set_mic(devc, devc->mic);
  331. value = ret_vol_mono(devc->mic);
  332. retval = put_user(value, p);
  333. break;
  334. default:
  335. retval = -EINVAL;
  336. }
  337. }
  338. else {
  339. /*
  340. * Return parameters
  341. */
  342. switch (cmdf) {
  343. case SOUND_MIXER_DEVMASK:
  344. retval = put_user(SOUND_MASK_VOLUME | SOUND_MASK_MIC, p);
  345. break;
  346. case SOUND_MIXER_STEREODEVS:
  347. retval = put_user(SOUND_MASK_VOLUME, p);
  348. break;
  349. case SOUND_MIXER_RECMASK:
  350. /* No recording devices */
  351. retval = put_user(0, p);
  352. break;
  353. case SOUND_MIXER_CAPS:
  354. retval = put_user(SOUND_CAP_EXCL_INPUT, p);
  355. break;
  356. case SOUND_MIXER_RECSRC:
  357. /* No recording source */
  358. retval = put_user(0, p);
  359. break;
  360. case SOUND_MIXER_VOLUME:
  361. value = ret_vol_stereo(devc->volume_l, devc->volume_r);
  362. retval = put_user(value, p);
  363. break;
  364. case SOUND_MIXER_MIC:
  365. value = ret_vol_mono(devc->mic);
  366. put_user(value, p);
  367. break;
  368. default:
  369. retval = -EINVAL;
  370. }
  371. }
  372. return retval;
  373. }
  374. /* opl3sa2_mixer_ioctl end */
  375. static int opl3sa3_mixer_ioctl(int dev, unsigned int cmd, void __user * arg)
  376. {
  377. int value, retval, cmdf = cmd & 0xff;
  378. opl3sa2_state_t* devc = &opl3sa2_state[dev];
  379. switch (cmdf) {
  380. case SOUND_MIXER_BASS:
  381. value = ret_vol_stereo(devc->bass_l, devc->bass_r);
  382. retval = put_user(value, (int __user *) arg);
  383. break;
  384. case SOUND_MIXER_TREBLE:
  385. value = ret_vol_stereo(devc->treble_l, devc->treble_r);
  386. retval = put_user(value, (int __user *) arg);
  387. break;
  388. case SOUND_MIXER_DIGITAL1:
  389. value = ret_vol_stereo(devc->wide_l, devc->wide_r);
  390. retval = put_user(value, (int __user *) arg);
  391. break;
  392. default:
  393. retval = -EINVAL;
  394. }
  395. return retval;
  396. }
  397. /* opl3sa3_mixer_ioctl end */
  398. static struct mixer_operations opl3sa2_mixer_operations =
  399. {
  400. .owner = THIS_MODULE,
  401. .id = "OPL3-SA2",
  402. .name = "Yamaha OPL3-SA2",
  403. .ioctl = opl3sa2_mixer_ioctl
  404. };
  405. static struct mixer_operations opl3sa3_mixer_operations =
  406. {
  407. .owner = THIS_MODULE,
  408. .id = "OPL3-SA3",
  409. .name = "Yamaha OPL3-SA3",
  410. .ioctl = opl3sa3_mixer_ioctl
  411. };
  412. /* End of mixer-related stuff */
  413. /*
  414. * Component probe, attach, unload functions
  415. */
  416. static inline void __exit unload_opl3sa2_mpu(struct address_info *hw_config)
  417. {
  418. unload_mpu401(hw_config);
  419. }
  420. static void __init attach_opl3sa2_mss(struct address_info* hw_config, struct resource *ports)
  421. {
  422. int initial_mixers;
  423. initial_mixers = num_mixers;
  424. attach_ms_sound(hw_config, ports, THIS_MODULE); /* Slot 0 */
  425. if (hw_config->slots[0] != -1) {
  426. /* Did the MSS driver install? */
  427. if(num_mixers == (initial_mixers + 1)) {
  428. /* The MSS mixer is installed, reroute mixers appropriately */
  429. AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_CD);
  430. AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_SYNTH);
  431. AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
  432. }
  433. else {
  434. printk(KERN_ERR PFX "MSS mixer not installed?\n");
  435. }
  436. }
  437. }
  438. static inline void __exit unload_opl3sa2_mss(struct address_info* hw_config)
  439. {
  440. unload_ms_sound(hw_config);
  441. }
  442. static int __init probe_opl3sa2(struct address_info* hw_config, int card)
  443. {
  444. unsigned char misc;
  445. unsigned char tmp;
  446. unsigned char version;
  447. /*
  448. * Try and allocate our I/O port range.
  449. */
  450. if (!request_region(hw_config->io_base, 2, OPL3SA2_MODULE_NAME)) {
  451. printk(KERN_ERR PFX "Control I/O port %#x not free\n",
  452. hw_config->io_base);
  453. goto out_nodev;
  454. }
  455. /*
  456. * Check if writing to the read-only version bits of the miscellaneous
  457. * register succeeds or not (it should not).
  458. */
  459. opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, &misc);
  460. opl3sa2_write(hw_config->io_base, OPL3SA2_MISC, misc ^ 0x07);
  461. opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, &tmp);
  462. if(tmp != misc) {
  463. printk(KERN_ERR PFX "Control I/O port %#x is not a YMF7xx chipset!\n",
  464. hw_config->io_base);
  465. goto out_region;
  466. }
  467. /*
  468. * Check if the MIC register is accessible.
  469. */
  470. opl3sa2_read(hw_config->io_base, OPL3SA2_MIC, &tmp);
  471. opl3sa2_write(hw_config->io_base, OPL3SA2_MIC, 0x8a);
  472. opl3sa2_read(hw_config->io_base, OPL3SA2_MIC, &tmp);
  473. if((tmp & 0x9f) != 0x8a) {
  474. printk(KERN_ERR
  475. PFX "Control I/O port %#x is not a YMF7xx chipset!\n",
  476. hw_config->io_base);
  477. goto out_region;
  478. }
  479. opl3sa2_write(hw_config->io_base, OPL3SA2_MIC, tmp);
  480. /*
  481. * Determine chipset type (SA2 or SA3)
  482. *
  483. * This is done by looking at the chipset version in the lower 3 bits
  484. * of the miscellaneous register.
  485. */
  486. version = misc & 0x07;
  487. printk(KERN_DEBUG PFX "Chipset version = %#x\n", version);
  488. switch (version) {
  489. case 0:
  490. opl3sa2_state[card].chipset = CHIPSET_UNKNOWN;
  491. printk(KERN_ERR
  492. PFX "Unknown Yamaha audio controller version\n");
  493. break;
  494. case VERSION_YMF711:
  495. opl3sa2_state[card].chipset = CHIPSET_OPL3SA2;
  496. printk(KERN_INFO PFX "Found OPL3-SA2 (YMF711)\n");
  497. break;
  498. case VERSION_YMF715:
  499. opl3sa2_state[card].chipset = CHIPSET_OPL3SA3;
  500. printk(KERN_INFO
  501. PFX "Found OPL3-SA3 (YMF715 or YMF719)\n");
  502. break;
  503. case VERSION_YMF715B:
  504. opl3sa2_state[card].chipset = CHIPSET_OPL3SA3;
  505. printk(KERN_INFO
  506. PFX "Found OPL3-SA3 (YMF715B or YMF719B)\n");
  507. break;
  508. case VERSION_YMF715E:
  509. default:
  510. opl3sa2_state[card].chipset = CHIPSET_OPL3SA3;
  511. printk(KERN_INFO
  512. PFX "Found OPL3-SA3 (YMF715E or YMF719E)\n");
  513. break;
  514. }
  515. if (opl3sa2_state[card].chipset != CHIPSET_UNKNOWN) {
  516. /* Generate a pretty name */
  517. opl3sa2_state[card].chipset_name = (char *)CHIPSET_TABLE[opl3sa2_state[card].chipset];
  518. return 0;
  519. }
  520. out_region:
  521. release_region(hw_config->io_base, 2);
  522. out_nodev:
  523. return -ENODEV;
  524. }
  525. static void __init attach_opl3sa2(struct address_info* hw_config, int card)
  526. {
  527. /* Initialize IRQ configuration to IRQ-B: -, IRQ-A: WSS+MPU+OPL3 */
  528. opl3sa2_write(hw_config->io_base, OPL3SA2_IRQ_CONFIG, 0x0d);
  529. /* Initialize DMA configuration */
  530. if(hw_config->dma2 == hw_config->dma) {
  531. /* Want DMA configuration DMA-B: -, DMA-A: WSS-P+WSS-R */
  532. opl3sa2_write(hw_config->io_base, OPL3SA2_DMA_CONFIG, 0x03);
  533. }
  534. else {
  535. /* Want DMA configuration DMA-B: WSS-R, DMA-A: WSS-P */
  536. opl3sa2_write(hw_config->io_base, OPL3SA2_DMA_CONFIG, 0x21);
  537. }
  538. }
  539. static void __init attach_opl3sa2_mixer(struct address_info *hw_config, int card)
  540. {
  541. struct mixer_operations* mixer_operations;
  542. opl3sa2_state_t* devc = &opl3sa2_state[card];
  543. /* Install master mixer */
  544. if (devc->chipset == CHIPSET_OPL3SA3) {
  545. mixer_operations = &opl3sa3_mixer_operations;
  546. }
  547. else {
  548. mixer_operations = &opl3sa2_mixer_operations;
  549. }
  550. devc->cfg_port = hw_config->io_base;
  551. devc->mixer = sound_install_mixer(MIXER_DRIVER_VERSION,
  552. mixer_operations->name,
  553. mixer_operations,
  554. sizeof(struct mixer_operations),
  555. devc);
  556. if(devc->mixer < 0) {
  557. printk(KERN_ERR PFX "Could not install %s master mixer\n",
  558. mixer_operations->name);
  559. }
  560. else {
  561. opl3sa2_mixer_reset(devc);
  562. }
  563. }
  564. static void opl3sa2_clear_slots(struct address_info* hw_config)
  565. {
  566. int i;
  567. for(i = 0; i < 6; i++) {
  568. hw_config->slots[i] = -1;
  569. }
  570. }
  571. static void __init opl3sa2_set_ymode(struct address_info* hw_config, int ymode)
  572. {
  573. /*
  574. * Set the Yamaha 3D enhancement mode (aka Ymersion) if asked to and
  575. * it's supported.
  576. *
  577. * 0: Desktop (aka normal) 5-12 cm speakers
  578. * 1: Notebook PC mode 1 3 cm speakers
  579. * 2: Notebook PC mode 2 1.5 cm speakers
  580. * 3: Hi-fi 16-38 cm speakers
  581. */
  582. if(ymode >= 0 && ymode <= 3) {
  583. unsigned char sys_ctrl;
  584. opl3sa2_read(hw_config->io_base, OPL3SA2_SYS_CTRL, &sys_ctrl);
  585. sys_ctrl = (sys_ctrl & 0xcf) | ((ymode & 3) << 4);
  586. opl3sa2_write(hw_config->io_base, OPL3SA2_SYS_CTRL, sys_ctrl);
  587. }
  588. else {
  589. printk(KERN_ERR PFX "not setting ymode, it must be one of 0,1,2,3\n");
  590. }
  591. }
  592. static void __init opl3sa2_set_loopback(struct address_info* hw_config, int loopback)
  593. {
  594. if(loopback >= 0 && loopback <= 1) {
  595. unsigned char misc;
  596. opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, &misc);
  597. misc = (misc & 0xef) | ((loopback & 1) << 4);
  598. opl3sa2_write(hw_config->io_base, OPL3SA2_MISC, misc);
  599. }
  600. else {
  601. printk(KERN_ERR PFX "not setting loopback, it must be either 0 or 1\n");
  602. }
  603. }
  604. static void __exit unload_opl3sa2(struct address_info* hw_config, int card)
  605. {
  606. /* Release control ports */
  607. release_region(hw_config->io_base, 2);
  608. /* Unload mixer */
  609. if(opl3sa2_state[card].mixer >= 0)
  610. sound_unload_mixerdev(opl3sa2_state[card].mixer);
  611. }
  612. #ifdef CONFIG_PNP
  613. static struct pnp_device_id pnp_opl3sa2_list[] = {
  614. {.id = "YMH0021", .driver_data = 0},
  615. {.id = ""}
  616. };
  617. MODULE_DEVICE_TABLE(pnp, pnp_opl3sa2_list);
  618. static int opl3sa2_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
  619. {
  620. int card = opl3sa2_cards_num;
  621. /* we don't actually want to return an error as the user may have specified
  622. * no multiple card search
  623. */
  624. if (opl3sa2_cards_num == OPL3SA2_CARDS_MAX)
  625. return 0;
  626. opl3sa2_activated[card] = 1;
  627. /* Our own config: */
  628. opl3sa2_state[card].cfg.io_base = pnp_port_start(dev, 4);
  629. opl3sa2_state[card].cfg.irq = pnp_irq(dev, 0);
  630. opl3sa2_state[card].cfg.dma = pnp_dma(dev, 0);
  631. opl3sa2_state[card].cfg.dma2 = pnp_dma(dev, 1);
  632. /* The MSS config: */
  633. opl3sa2_state[card].cfg_mss.io_base = pnp_port_start(dev, 1);
  634. opl3sa2_state[card].cfg_mss.irq = pnp_irq(dev, 0);
  635. opl3sa2_state[card].cfg_mss.dma = pnp_dma(dev, 0);
  636. opl3sa2_state[card].cfg_mss.dma2 = pnp_dma(dev, 1);
  637. opl3sa2_state[card].cfg_mss.card_subtype = 1; /* No IRQ or DMA setup */
  638. opl3sa2_state[card].cfg_mpu.io_base = pnp_port_start(dev, 3);
  639. opl3sa2_state[card].cfg_mpu.irq = pnp_irq(dev, 0);
  640. opl3sa2_state[card].cfg_mpu.dma = -1;
  641. opl3sa2_state[card].cfg_mpu.dma2 = -1;
  642. opl3sa2_state[card].cfg_mpu.always_detect = 1; /* It's there, so use shared IRQs */
  643. /* Call me paranoid: */
  644. opl3sa2_clear_slots(&opl3sa2_state[card].cfg);
  645. opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mss);
  646. opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mpu);
  647. opl3sa2_state[card].pdev = dev;
  648. opl3sa2_cards_num++;
  649. return 0;
  650. }
  651. static struct pnp_driver opl3sa2_driver = {
  652. .name = "opl3sa2",
  653. .id_table = pnp_opl3sa2_list,
  654. .probe = opl3sa2_pnp_probe,
  655. };
  656. #endif /* CONFIG_PNP */
  657. /* End of component functions */
  658. /*
  659. * Install OPL3-SA2 based card(s).
  660. *
  661. * Need to have ad1848 and mpu401 loaded ready.
  662. */
  663. static int __init init_opl3sa2(void)
  664. {
  665. int card, max;
  666. /* Sanitize isapnp and multiple settings */
  667. isapnp = isapnp != 0 ? 1 : 0;
  668. multiple = multiple != 0 ? 1 : 0;
  669. max = (multiple && isapnp) ? OPL3SA2_CARDS_MAX : 1;
  670. #ifdef CONFIG_PNP
  671. if (isapnp){
  672. pnp_register_driver(&opl3sa2_driver);
  673. if(!opl3sa2_cards_num){
  674. printk(KERN_INFO PFX "No PnP cards found\n");
  675. isapnp = 0;
  676. }
  677. max = opl3sa2_cards_num;
  678. }
  679. #endif
  680. for (card = 0; card < max; card++) {
  681. /* If a user wants an I/O then assume they meant it */
  682. struct resource *ports;
  683. int base;
  684. if (!isapnp) {
  685. if (io == -1 || irq == -1 || dma == -1 ||
  686. dma2 == -1 || mss_io == -1) {
  687. printk(KERN_ERR
  688. PFX "io, mss_io, irq, dma, and dma2 must be set\n");
  689. return -EINVAL;
  690. }
  691. opl3sa2_cards_num++;
  692. /*
  693. * Our own config:
  694. * (NOTE: IRQ and DMA aren't used, so they're set to
  695. * give pretty output from conf_printf. :)
  696. */
  697. opl3sa2_state[card].cfg.io_base = io;
  698. opl3sa2_state[card].cfg.irq = irq;
  699. opl3sa2_state[card].cfg.dma = dma;
  700. opl3sa2_state[card].cfg.dma2 = dma2;
  701. /* The MSS config: */
  702. opl3sa2_state[card].cfg_mss.io_base = mss_io;
  703. opl3sa2_state[card].cfg_mss.irq = irq;
  704. opl3sa2_state[card].cfg_mss.dma = dma;
  705. opl3sa2_state[card].cfg_mss.dma2 = dma2;
  706. opl3sa2_state[card].cfg_mss.card_subtype = 1; /* No IRQ or DMA setup */
  707. opl3sa2_state[card].cfg_mpu.io_base = mpu_io;
  708. opl3sa2_state[card].cfg_mpu.irq = irq;
  709. opl3sa2_state[card].cfg_mpu.dma = -1;
  710. opl3sa2_state[card].cfg_mpu.always_detect = 1; /* Use shared IRQs */
  711. /* Call me paranoid: */
  712. opl3sa2_clear_slots(&opl3sa2_state[card].cfg);
  713. opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mss);
  714. opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mpu);
  715. }
  716. /* FIXME: leak */
  717. if (probe_opl3sa2(&opl3sa2_state[card].cfg, card))
  718. return -ENODEV;
  719. base = opl3sa2_state[card].cfg_mss.io_base;
  720. if (!request_region(base, 4, "WSS config"))
  721. goto failed;
  722. ports = request_region(base + 4, 4, "ad1848");
  723. if (!ports)
  724. goto failed2;
  725. if (!probe_ms_sound(&opl3sa2_state[card].cfg_mss, ports)) {
  726. /*
  727. * If one or more cards are already registered, don't
  728. * return an error but print a warning. Note, this
  729. * should never really happen unless the hardware or
  730. * ISA PnP screwed up.
  731. */
  732. release_region(base + 4, 4);
  733. failed2:
  734. release_region(base, 4);
  735. failed:
  736. release_region(opl3sa2_state[card].cfg.io_base, 2);
  737. if (opl3sa2_cards_num) {
  738. printk(KERN_WARNING
  739. PFX "There was a problem probing one "
  740. " of the ISA PNP cards, continuing\n");
  741. opl3sa2_cards_num--;
  742. continue;
  743. } else
  744. return -ENODEV;
  745. }
  746. attach_opl3sa2(&opl3sa2_state[card].cfg, card);
  747. conf_printf(opl3sa2_state[card].chipset_name, &opl3sa2_state[card].cfg);
  748. attach_opl3sa2_mixer(&opl3sa2_state[card].cfg, card);
  749. attach_opl3sa2_mss(&opl3sa2_state[card].cfg_mss, ports);
  750. /* ewww =) */
  751. opl3sa2_state[card].card = card;
  752. /*
  753. * Set the Yamaha 3D enhancement mode (aka Ymersion) if asked to and
  754. * it's supported.
  755. */
  756. if (ymode != -1) {
  757. if (opl3sa2_state[card].chipset == CHIPSET_OPL3SA2) {
  758. printk(KERN_ERR
  759. PFX "ymode not supported on OPL3-SA2\n");
  760. }
  761. else {
  762. opl3sa2_set_ymode(&opl3sa2_state[card].cfg, ymode);
  763. }
  764. }
  765. /* Set A/D input to Mono loopback if asked to. */
  766. if (loopback != -1) {
  767. opl3sa2_set_loopback(&opl3sa2_state[card].cfg, loopback);
  768. }
  769. /* Attach MPU if we've been asked to do so, failure isn't fatal */
  770. if (opl3sa2_state[card].cfg_mpu.io_base != -1) {
  771. int base = opl3sa2_state[card].cfg_mpu.io_base;
  772. struct resource *ports;
  773. ports = request_region(base, 2, "mpu401");
  774. if (!ports)
  775. goto out;
  776. if (!probe_mpu401(&opl3sa2_state[card].cfg_mpu, ports)) {
  777. release_region(base, 2);
  778. goto out;
  779. }
  780. if (attach_mpu401(&opl3sa2_state[card].cfg_mpu, THIS_MODULE)) {
  781. printk(KERN_ERR PFX "failed to attach MPU401\n");
  782. opl3sa2_state[card].cfg_mpu.slots[1] = -1;
  783. }
  784. }
  785. }
  786. out:
  787. if (isapnp) {
  788. printk(KERN_NOTICE PFX "%d PnP card(s) found.\n", opl3sa2_cards_num);
  789. }
  790. return 0;
  791. }
  792. /*
  793. * Uninstall OPL3-SA2 based card(s).
  794. */
  795. static void __exit cleanup_opl3sa2(void)
  796. {
  797. int card;
  798. for(card = 0; card < opl3sa2_cards_num; card++) {
  799. if (opl3sa2_state[card].cfg_mpu.slots[1] != -1) {
  800. unload_opl3sa2_mpu(&opl3sa2_state[card].cfg_mpu);
  801. }
  802. unload_opl3sa2_mss(&opl3sa2_state[card].cfg_mss);
  803. unload_opl3sa2(&opl3sa2_state[card].cfg, card);
  804. #ifdef CONFIG_PNP
  805. pnp_unregister_driver(&opl3sa2_driver);
  806. #endif
  807. }
  808. }
  809. module_init(init_opl3sa2);
  810. module_exit(cleanup_opl3sa2);
  811. #ifndef MODULE
  812. static int __init setup_opl3sa2(char *str)
  813. {
  814. /* io, irq, dma, dma2,... */
  815. #ifdef CONFIG_PNP
  816. int ints[11];
  817. #else
  818. int ints[9];
  819. #endif
  820. str = get_options(str, ARRAY_SIZE(ints), ints);
  821. io = ints[1];
  822. irq = ints[2];
  823. dma = ints[3];
  824. dma2 = ints[4];
  825. mss_io = ints[5];
  826. mpu_io = ints[6];
  827. ymode = ints[7];
  828. loopback = ints[8];
  829. #ifdef CONFIG_PNP
  830. isapnp = ints[9];
  831. multiple = ints[10];
  832. #endif
  833. return 1;
  834. }
  835. __setup("opl3sa2=", setup_opl3sa2);
  836. #endif