opl3sa2.c 27 KB

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