opl3.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. /*
  2. * sound/opl3.c
  3. *
  4. * A low level driver for Yamaha YM3812 and OPL-3 -chips
  5. *
  6. *
  7. * Copyright (C) by Hannu Savolainen 1993-1997
  8. *
  9. * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  10. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  11. * for more info.
  12. *
  13. *
  14. * Changes
  15. * Thomas Sailer ioctl code reworked (vmalloc/vfree removed)
  16. * Alan Cox modularisation, fixed sound_mem allocs.
  17. * Christoph Hellwig Adapted to module_init/module_exit
  18. * Arnaldo C. de Melo get rid of check_region, use request_region for
  19. * OPL4, release it on exit, some cleanups.
  20. *
  21. * Status
  22. * Believed to work. Badly needs rewriting a bit to support multiple
  23. * OPL3 devices.
  24. */
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/delay.h>
  28. /*
  29. * Major improvements to the FM handling 30AUG92 by Rob Hooft,
  30. * hooft@chem.ruu.nl
  31. */
  32. #include "sound_config.h"
  33. #include "opl3.h"
  34. #include "opl3_hw.h"
  35. #define MAX_VOICE 18
  36. #define OFFS_4OP 11
  37. struct voice_info
  38. {
  39. unsigned char keyon_byte;
  40. long bender;
  41. long bender_range;
  42. unsigned long orig_freq;
  43. unsigned long current_freq;
  44. int volume;
  45. int mode;
  46. int panning; /* 0xffff means not set */
  47. };
  48. typedef struct opl_devinfo
  49. {
  50. int base;
  51. int left_io, right_io;
  52. int nr_voice;
  53. int lv_map[MAX_VOICE];
  54. struct voice_info voc[MAX_VOICE];
  55. struct voice_alloc_info *v_alloc;
  56. struct channel_info *chn_info;
  57. struct sbi_instrument i_map[SBFM_MAXINSTR];
  58. struct sbi_instrument *act_i[MAX_VOICE];
  59. struct synth_info fm_info;
  60. int busy;
  61. int model;
  62. unsigned char cmask;
  63. int is_opl4;
  64. int *osp;
  65. } opl_devinfo;
  66. static struct opl_devinfo *devc = NULL;
  67. static int detected_model;
  68. static int store_instr(int instr_no, struct sbi_instrument *instr);
  69. static void freq_to_fnum(int freq, int *block, int *fnum);
  70. static void opl3_command(int io_addr, unsigned int addr, unsigned int val);
  71. static int opl3_kill_note(int dev, int voice, int note, int velocity);
  72. static void enter_4op_mode(void)
  73. {
  74. int i;
  75. static int v4op[MAX_VOICE] = {
  76. 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17
  77. };
  78. devc->cmask = 0x3f; /* Connect all possible 4 OP voice operators */
  79. opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x3f);
  80. for (i = 0; i < 3; i++)
  81. pv_map[i].voice_mode = 4;
  82. for (i = 3; i < 6; i++)
  83. pv_map[i].voice_mode = 0;
  84. for (i = 9; i < 12; i++)
  85. pv_map[i].voice_mode = 4;
  86. for (i = 12; i < 15; i++)
  87. pv_map[i].voice_mode = 0;
  88. for (i = 0; i < 12; i++)
  89. devc->lv_map[i] = v4op[i];
  90. devc->v_alloc->max_voice = devc->nr_voice = 12;
  91. }
  92. static int opl3_ioctl(int dev, unsigned int cmd, void __user * arg)
  93. {
  94. struct sbi_instrument ins;
  95. switch (cmd) {
  96. case SNDCTL_FM_LOAD_INSTR:
  97. printk(KERN_WARNING "Warning: Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. Fix the program.\n");
  98. if (copy_from_user(&ins, arg, sizeof(ins)))
  99. return -EFAULT;
  100. if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR) {
  101. printk(KERN_WARNING "FM Error: Invalid instrument number %d\n", ins.channel);
  102. return -EINVAL;
  103. }
  104. return store_instr(ins.channel, &ins);
  105. case SNDCTL_SYNTH_INFO:
  106. devc->fm_info.nr_voices = (devc->nr_voice == 12) ? 6 : devc->nr_voice;
  107. if (copy_to_user(arg, &devc->fm_info, sizeof(devc->fm_info)))
  108. return -EFAULT;
  109. return 0;
  110. case SNDCTL_SYNTH_MEMAVL:
  111. return 0x7fffffff;
  112. case SNDCTL_FM_4OP_ENABLE:
  113. if (devc->model == 2)
  114. enter_4op_mode();
  115. return 0;
  116. default:
  117. return -EINVAL;
  118. }
  119. }
  120. int opl3_detect(int ioaddr, int *osp)
  121. {
  122. /*
  123. * This function returns 1 if the FM chip is present at the given I/O port
  124. * The detection algorithm plays with the timer built in the FM chip and
  125. * looks for a change in the status register.
  126. *
  127. * Note! The timers of the FM chip are not connected to AdLib (and compatible)
  128. * boards.
  129. *
  130. * Note2! The chip is initialized if detected.
  131. */
  132. unsigned char stat1, signature;
  133. int i;
  134. if (devc != NULL)
  135. {
  136. printk(KERN_ERR "opl3: Only one OPL3 supported.\n");
  137. return 0;
  138. }
  139. devc = (struct opl_devinfo *)kmalloc(sizeof(*devc), GFP_KERNEL);
  140. if (devc == NULL)
  141. {
  142. printk(KERN_ERR "opl3: Can't allocate memory for the device control "
  143. "structure \n ");
  144. return 0;
  145. }
  146. memset(devc, 0, sizeof(*devc));
  147. strcpy(devc->fm_info.name, "OPL2");
  148. if (!request_region(ioaddr, 4, devc->fm_info.name)) {
  149. printk(KERN_WARNING "opl3: I/O port 0x%x already in use\n", ioaddr);
  150. goto cleanup_devc;
  151. }
  152. devc->osp = osp;
  153. devc->base = ioaddr;
  154. /* Reset timers 1 and 2 */
  155. opl3_command(ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
  156. /* Reset the IRQ of the FM chip */
  157. opl3_command(ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
  158. signature = stat1 = inb(ioaddr); /* Status register */
  159. if (signature != 0x00 && signature != 0x06 && signature != 0x02 &&
  160. signature != 0x0f)
  161. {
  162. MDB(printk(KERN_INFO "OPL3 not detected %x\n", signature));
  163. goto cleanup_region;
  164. }
  165. if (signature == 0x06) /* OPL2 */
  166. {
  167. detected_model = 2;
  168. }
  169. else if (signature == 0x00 || signature == 0x0f) /* OPL3 or OPL4 */
  170. {
  171. unsigned char tmp;
  172. detected_model = 3;
  173. /*
  174. * Detect availability of OPL4 (_experimental_). Works probably
  175. * only after a cold boot. In addition the OPL4 port
  176. * of the chip may not be connected to the PC bus at all.
  177. */
  178. opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0x00);
  179. opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, OPL3_ENABLE | OPL4_ENABLE);
  180. if ((tmp = inb(ioaddr)) == 0x02) /* Have a OPL4 */
  181. {
  182. detected_model = 4;
  183. }
  184. if (request_region(ioaddr - 8, 2, "OPL4")) /* OPL4 port was free */
  185. {
  186. int tmp;
  187. outb((0x02), ioaddr - 8); /* Select OPL4 ID register */
  188. udelay(10);
  189. tmp = inb(ioaddr - 7); /* Read it */
  190. udelay(10);
  191. if (tmp == 0x20) /* OPL4 should return 0x20 here */
  192. {
  193. detected_model = 4;
  194. outb((0xF8), ioaddr - 8); /* Select OPL4 FM mixer control */
  195. udelay(10);
  196. outb((0x1B), ioaddr - 7); /* Write value */
  197. udelay(10);
  198. }
  199. else
  200. { /* release OPL4 port */
  201. release_region(ioaddr - 8, 2);
  202. detected_model = 3;
  203. }
  204. }
  205. opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0);
  206. }
  207. for (i = 0; i < 9; i++)
  208. opl3_command(ioaddr, KEYON_BLOCK + i, 0); /*
  209. * Note off
  210. */
  211. opl3_command(ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
  212. opl3_command(ioaddr, PERCOSSION_REGISTER, 0x00); /*
  213. * Melodic mode.
  214. */
  215. return 1;
  216. cleanup_region:
  217. release_region(ioaddr, 4);
  218. cleanup_devc:
  219. kfree(devc);
  220. devc = NULL;
  221. return 0;
  222. }
  223. static int opl3_kill_note (int devno, int voice, int note, int velocity)
  224. {
  225. struct physical_voice_info *map;
  226. if (voice < 0 || voice >= devc->nr_voice)
  227. return 0;
  228. devc->v_alloc->map[voice] = 0;
  229. map = &pv_map[devc->lv_map[voice]];
  230. DEB(printk("Kill note %d\n", voice));
  231. if (map->voice_mode == 0)
  232. return 0;
  233. opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, devc->voc[voice].keyon_byte & ~0x20);
  234. devc->voc[voice].keyon_byte = 0;
  235. devc->voc[voice].bender = 0;
  236. devc->voc[voice].volume = 64;
  237. devc->voc[voice].panning = 0xffff; /* Not set */
  238. devc->voc[voice].bender_range = 200;
  239. devc->voc[voice].orig_freq = 0;
  240. devc->voc[voice].current_freq = 0;
  241. devc->voc[voice].mode = 0;
  242. return 0;
  243. }
  244. #define HIHAT 0
  245. #define CYMBAL 1
  246. #define TOMTOM 2
  247. #define SNARE 3
  248. #define BDRUM 4
  249. #define UNDEFINED TOMTOM
  250. #define DEFAULT TOMTOM
  251. static int store_instr(int instr_no, struct sbi_instrument *instr)
  252. {
  253. if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || devc->model != 2))
  254. printk(KERN_WARNING "FM warning: Invalid patch format field (key) 0x%x\n", instr->key);
  255. memcpy((char *) &(devc->i_map[instr_no]), (char *) instr, sizeof(*instr));
  256. return 0;
  257. }
  258. static int opl3_set_instr (int dev, int voice, int instr_no)
  259. {
  260. if (voice < 0 || voice >= devc->nr_voice)
  261. return 0;
  262. if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
  263. instr_no = 0; /* Acoustic piano (usually) */
  264. devc->act_i[voice] = &devc->i_map[instr_no];
  265. return 0;
  266. }
  267. /*
  268. * The next table looks magical, but it certainly is not. Its values have
  269. * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
  270. * for i=0. This log-table converts a linear volume-scaling (0..127) to a
  271. * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
  272. * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
  273. * volume -8 it was implemented as a table because it is only 128 bytes and
  274. * it saves a lot of log() calculations. (RH)
  275. */
  276. static char fm_volume_table[128] =
  277. {
  278. -64, -48, -40, -35, -32, -29, -27, -26,
  279. -24, -23, -21, -20, -19, -18, -18, -17,
  280. -16, -15, -15, -14, -13, -13, -12, -12,
  281. -11, -11, -10, -10, -10, -9, -9, -8,
  282. -8, -8, -7, -7, -7, -6, -6, -6,
  283. -5, -5, -5, -5, -4, -4, -4, -4,
  284. -3, -3, -3, -3, -2, -2, -2, -2,
  285. -2, -1, -1, -1, -1, 0, 0, 0,
  286. 0, 0, 0, 1, 1, 1, 1, 1,
  287. 1, 2, 2, 2, 2, 2, 2, 2,
  288. 3, 3, 3, 3, 3, 3, 3, 4,
  289. 4, 4, 4, 4, 4, 4, 4, 5,
  290. 5, 5, 5, 5, 5, 5, 5, 5,
  291. 6, 6, 6, 6, 6, 6, 6, 6,
  292. 6, 7, 7, 7, 7, 7, 7, 7,
  293. 7, 7, 7, 8, 8, 8, 8, 8
  294. };
  295. static void calc_vol(unsigned char *regbyte, int volume, int main_vol)
  296. {
  297. int level = (~*regbyte & 0x3f);
  298. if (main_vol > 127)
  299. main_vol = 127;
  300. volume = (volume * main_vol) / 127;
  301. if (level)
  302. level += fm_volume_table[volume];
  303. if (level > 0x3f)
  304. level = 0x3f;
  305. if (level < 0)
  306. level = 0;
  307. *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
  308. }
  309. static void set_voice_volume(int voice, int volume, int main_vol)
  310. {
  311. unsigned char vol1, vol2, vol3, vol4;
  312. struct sbi_instrument *instr;
  313. struct physical_voice_info *map;
  314. if (voice < 0 || voice >= devc->nr_voice)
  315. return;
  316. map = &pv_map[devc->lv_map[voice]];
  317. instr = devc->act_i[voice];
  318. if (!instr)
  319. instr = &devc->i_map[0];
  320. if (instr->channel < 0)
  321. return;
  322. if (devc->voc[voice].mode == 0)
  323. return;
  324. if (devc->voc[voice].mode == 2)
  325. {
  326. vol1 = instr->operators[2];
  327. vol2 = instr->operators[3];
  328. if ((instr->operators[10] & 0x01))
  329. {
  330. calc_vol(&vol1, volume, main_vol);
  331. calc_vol(&vol2, volume, main_vol);
  332. }
  333. else
  334. {
  335. calc_vol(&vol2, volume, main_vol);
  336. }
  337. opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
  338. opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
  339. }
  340. else
  341. { /*
  342. * 4 OP voice
  343. */
  344. int connection;
  345. vol1 = instr->operators[2];
  346. vol2 = instr->operators[3];
  347. vol3 = instr->operators[OFFS_4OP + 2];
  348. vol4 = instr->operators[OFFS_4OP + 3];
  349. /*
  350. * The connection method for 4 OP devc->voc is defined by the rightmost
  351. * bits at the offsets 10 and 10+OFFS_4OP
  352. */
  353. connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
  354. switch (connection)
  355. {
  356. case 0:
  357. calc_vol(&vol4, volume, main_vol);
  358. break;
  359. case 1:
  360. calc_vol(&vol2, volume, main_vol);
  361. calc_vol(&vol4, volume, main_vol);
  362. break;
  363. case 2:
  364. calc_vol(&vol1, volume, main_vol);
  365. calc_vol(&vol4, volume, main_vol);
  366. break;
  367. case 3:
  368. calc_vol(&vol1, volume, main_vol);
  369. calc_vol(&vol3, volume, main_vol);
  370. calc_vol(&vol4, volume, main_vol);
  371. break;
  372. default:
  373. ;
  374. }
  375. opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
  376. opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
  377. opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], vol3);
  378. opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], vol4);
  379. }
  380. }
  381. static int opl3_start_note (int dev, int voice, int note, int volume)
  382. {
  383. unsigned char data, fpc;
  384. int block, fnum, freq, voice_mode, pan;
  385. struct sbi_instrument *instr;
  386. struct physical_voice_info *map;
  387. if (voice < 0 || voice >= devc->nr_voice)
  388. return 0;
  389. map = &pv_map[devc->lv_map[voice]];
  390. pan = devc->voc[voice].panning;
  391. if (map->voice_mode == 0)
  392. return 0;
  393. if (note == 255) /*
  394. * Just change the volume
  395. */
  396. {
  397. set_voice_volume(voice, volume, devc->voc[voice].volume);
  398. return 0;
  399. }
  400. /*
  401. * Kill previous note before playing
  402. */
  403. opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], 0xff); /*
  404. * Carrier
  405. * volume to
  406. * min
  407. */
  408. opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], 0xff); /*
  409. * Modulator
  410. * volume to
  411. */
  412. if (map->voice_mode == 4)
  413. {
  414. opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
  415. opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
  416. }
  417. opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00); /*
  418. * Note
  419. * off
  420. */
  421. instr = devc->act_i[voice];
  422. if (!instr)
  423. instr = &devc->i_map[0];
  424. if (instr->channel < 0)
  425. {
  426. printk(KERN_WARNING "opl3: Initializing voice %d with undefined instrument\n", voice);
  427. return 0;
  428. }
  429. if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
  430. return 0; /*
  431. * Cannot play
  432. */
  433. voice_mode = map->voice_mode;
  434. if (voice_mode == 4)
  435. {
  436. int voice_shift;
  437. voice_shift = (map->ioaddr == devc->left_io) ? 0 : 3;
  438. voice_shift += map->voice_num;
  439. if (instr->key != OPL3_PATCH) /*
  440. * Just 2 OP patch
  441. */
  442. {
  443. voice_mode = 2;
  444. devc->cmask &= ~(1 << voice_shift);
  445. }
  446. else
  447. {
  448. devc->cmask |= (1 << voice_shift);
  449. }
  450. opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
  451. }
  452. /*
  453. * Set Sound Characteristics
  454. */
  455. opl3_command(map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);
  456. opl3_command(map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);
  457. /*
  458. * Set Attack/Decay
  459. */
  460. opl3_command(map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);
  461. opl3_command(map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);
  462. /*
  463. * Set Sustain/Release
  464. */
  465. opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[0], instr->operators[6]);
  466. opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
  467. /*
  468. * Set Wave Select
  469. */
  470. opl3_command(map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);
  471. opl3_command(map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);
  472. /*
  473. * Set Feedback/Connection
  474. */
  475. fpc = instr->operators[10];
  476. if (pan != 0xffff)
  477. {
  478. fpc &= ~STEREO_BITS;
  479. if (pan < -64)
  480. fpc |= VOICE_TO_LEFT;
  481. else
  482. if (pan > 64)
  483. fpc |= VOICE_TO_RIGHT;
  484. else
  485. fpc |= (VOICE_TO_LEFT | VOICE_TO_RIGHT);
  486. }
  487. if (!(fpc & 0x30))
  488. fpc |= 0x30; /*
  489. * Ensure that at least one chn is enabled
  490. */
  491. opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num, fpc);
  492. /*
  493. * If the voice is a 4 OP one, initialize the operators 3 and 4 also
  494. */
  495. if (voice_mode == 4)
  496. {
  497. /*
  498. * Set Sound Characteristics
  499. */
  500. opl3_command(map->ioaddr, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]);
  501. opl3_command(map->ioaddr, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]);
  502. /*
  503. * Set Attack/Decay
  504. */
  505. opl3_command(map->ioaddr, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]);
  506. opl3_command(map->ioaddr, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]);
  507. /*
  508. * Set Sustain/Release
  509. */
  510. opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]);
  511. opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]);
  512. /*
  513. * Set Wave Select
  514. */
  515. opl3_command(map->ioaddr, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]);
  516. opl3_command(map->ioaddr, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]);
  517. /*
  518. * Set Feedback/Connection
  519. */
  520. fpc = instr->operators[OFFS_4OP + 10];
  521. if (!(fpc & 0x30))
  522. fpc |= 0x30; /*
  523. * Ensure that at least one chn is enabled
  524. */
  525. opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num + 3, fpc);
  526. }
  527. devc->voc[voice].mode = voice_mode;
  528. set_voice_volume(voice, volume, devc->voc[voice].volume);
  529. freq = devc->voc[voice].orig_freq = note_to_freq(note) / 1000;
  530. /*
  531. * Since the pitch bender may have been set before playing the note, we
  532. * have to calculate the bending now.
  533. */
  534. freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range, 0);
  535. devc->voc[voice].current_freq = freq;
  536. freq_to_fnum(freq, &block, &fnum);
  537. /*
  538. * Play note
  539. */
  540. data = fnum & 0xff; /*
  541. * Least significant bits of fnumber
  542. */
  543. opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
  544. data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
  545. devc->voc[voice].keyon_byte = data;
  546. opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
  547. if (voice_mode == 4)
  548. opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);
  549. return 0;
  550. }
  551. static void freq_to_fnum (int freq, int *block, int *fnum)
  552. {
  553. int f, octave;
  554. /*
  555. * Converts the note frequency to block and fnum values for the FM chip
  556. */
  557. /*
  558. * First try to compute the block -value (octave) where the note belongs
  559. */
  560. f = freq;
  561. octave = 5;
  562. if (f == 0)
  563. octave = 0;
  564. else if (f < 261)
  565. {
  566. while (f < 261)
  567. {
  568. octave--;
  569. f <<= 1;
  570. }
  571. }
  572. else if (f > 493)
  573. {
  574. while (f > 493)
  575. {
  576. octave++;
  577. f >>= 1;
  578. }
  579. }
  580. if (octave > 7)
  581. octave = 7;
  582. *fnum = freq * (1 << (20 - octave)) / 49716;
  583. *block = octave;
  584. }
  585. static void opl3_command (int io_addr, unsigned int addr, unsigned int val)
  586. {
  587. int i;
  588. /*
  589. * The original 2-OP synth requires a quite long delay after writing to a
  590. * register. The OPL-3 survives with just two INBs
  591. */
  592. outb(((unsigned char) (addr & 0xff)), io_addr);
  593. if (devc->model != 2)
  594. udelay(10);
  595. else
  596. for (i = 0; i < 2; i++)
  597. inb(io_addr);
  598. outb(((unsigned char) (val & 0xff)), io_addr + 1);
  599. if (devc->model != 2)
  600. udelay(30);
  601. else
  602. for (i = 0; i < 2; i++)
  603. inb(io_addr);
  604. }
  605. static void opl3_reset(int devno)
  606. {
  607. int i;
  608. for (i = 0; i < 18; i++)
  609. devc->lv_map[i] = i;
  610. for (i = 0; i < devc->nr_voice; i++)
  611. {
  612. opl3_command(pv_map[devc->lv_map[i]].ioaddr,
  613. KSL_LEVEL + pv_map[devc->lv_map[i]].op[0], 0xff);
  614. opl3_command(pv_map[devc->lv_map[i]].ioaddr,
  615. KSL_LEVEL + pv_map[devc->lv_map[i]].op[1], 0xff);
  616. if (pv_map[devc->lv_map[i]].voice_mode == 4)
  617. {
  618. opl3_command(pv_map[devc->lv_map[i]].ioaddr,
  619. KSL_LEVEL + pv_map[devc->lv_map[i]].op[2], 0xff);
  620. opl3_command(pv_map[devc->lv_map[i]].ioaddr,
  621. KSL_LEVEL + pv_map[devc->lv_map[i]].op[3], 0xff);
  622. }
  623. opl3_kill_note(devno, i, 0, 64);
  624. }
  625. if (devc->model == 2)
  626. {
  627. devc->v_alloc->max_voice = devc->nr_voice = 18;
  628. for (i = 0; i < 18; i++)
  629. pv_map[i].voice_mode = 2;
  630. }
  631. }
  632. static int opl3_open(int dev, int mode)
  633. {
  634. int i;
  635. if (devc->busy)
  636. return -EBUSY;
  637. devc->busy = 1;
  638. devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
  639. devc->v_alloc->timestamp = 0;
  640. for (i = 0; i < 18; i++)
  641. {
  642. devc->v_alloc->map[i] = 0;
  643. devc->v_alloc->alloc_times[i] = 0;
  644. }
  645. devc->cmask = 0x00; /*
  646. * Just 2 OP mode
  647. */
  648. if (devc->model == 2)
  649. opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
  650. return 0;
  651. }
  652. static void opl3_close(int dev)
  653. {
  654. devc->busy = 0;
  655. devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9;
  656. devc->fm_info.nr_drums = 0;
  657. devc->fm_info.perc_mode = 0;
  658. opl3_reset(dev);
  659. }
  660. static void opl3_hw_control(int dev, unsigned char *event)
  661. {
  662. }
  663. static int opl3_load_patch(int dev, int format, const char __user *addr,
  664. int offs, int count, int pmgr_flag)
  665. {
  666. struct sbi_instrument ins;
  667. if (count <sizeof(ins))
  668. {
  669. printk(KERN_WARNING "FM Error: Patch record too short\n");
  670. return -EINVAL;
  671. }
  672. /*
  673. * What the fuck is going on here? We leave junk in the beginning
  674. * of ins and then check the field pretty close to that beginning?
  675. */
  676. if(copy_from_user(&((char *) &ins)[offs], addr + offs, sizeof(ins) - offs))
  677. return -EFAULT;
  678. if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
  679. {
  680. printk(KERN_WARNING "FM Error: Invalid instrument number %d\n", ins.channel);
  681. return -EINVAL;
  682. }
  683. ins.key = format;
  684. return store_instr(ins.channel, &ins);
  685. }
  686. static void opl3_panning(int dev, int voice, int value)
  687. {
  688. devc->voc[voice].panning = value;
  689. }
  690. static void opl3_volume_method(int dev, int mode)
  691. {
  692. }
  693. #define SET_VIBRATO(cell) { \
  694. tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
  695. if (pressure > 110) \
  696. tmp |= 0x40; /* Vibrato on */ \
  697. opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
  698. static void opl3_aftertouch(int dev, int voice, int pressure)
  699. {
  700. int tmp;
  701. struct sbi_instrument *instr;
  702. struct physical_voice_info *map;
  703. if (voice < 0 || voice >= devc->nr_voice)
  704. return;
  705. map = &pv_map[devc->lv_map[voice]];
  706. DEB(printk("Aftertouch %d\n", voice));
  707. if (map->voice_mode == 0)
  708. return;
  709. /*
  710. * Adjust the amount of vibrato depending the pressure
  711. */
  712. instr = devc->act_i[voice];
  713. if (!instr)
  714. instr = &devc->i_map[0];
  715. if (devc->voc[voice].mode == 4)
  716. {
  717. int connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
  718. switch (connection)
  719. {
  720. case 0:
  721. SET_VIBRATO(4);
  722. break;
  723. case 1:
  724. SET_VIBRATO(2);
  725. SET_VIBRATO(4);
  726. break;
  727. case 2:
  728. SET_VIBRATO(1);
  729. SET_VIBRATO(4);
  730. break;
  731. case 3:
  732. SET_VIBRATO(1);
  733. SET_VIBRATO(3);
  734. SET_VIBRATO(4);
  735. break;
  736. }
  737. /*
  738. * Not implemented yet
  739. */
  740. }
  741. else
  742. {
  743. SET_VIBRATO(1);
  744. if ((instr->operators[10] & 0x01)) /*
  745. * Additive synthesis
  746. */
  747. SET_VIBRATO(2);
  748. }
  749. }
  750. #undef SET_VIBRATO
  751. static void bend_pitch(int dev, int voice, int value)
  752. {
  753. unsigned char data;
  754. int block, fnum, freq;
  755. struct physical_voice_info *map;
  756. map = &pv_map[devc->lv_map[voice]];
  757. if (map->voice_mode == 0)
  758. return;
  759. devc->voc[voice].bender = value;
  760. if (!value)
  761. return;
  762. if (!(devc->voc[voice].keyon_byte & 0x20))
  763. return; /*
  764. * Not keyed on
  765. */
  766. freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range, 0);
  767. devc->voc[voice].current_freq = freq;
  768. freq_to_fnum(freq, &block, &fnum);
  769. data = fnum & 0xff; /*
  770. * Least significant bits of fnumber
  771. */
  772. opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
  773. data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
  774. devc->voc[voice].keyon_byte = data;
  775. opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
  776. }
  777. static void opl3_controller (int dev, int voice, int ctrl_num, int value)
  778. {
  779. if (voice < 0 || voice >= devc->nr_voice)
  780. return;
  781. switch (ctrl_num)
  782. {
  783. case CTRL_PITCH_BENDER:
  784. bend_pitch(dev, voice, value);
  785. break;
  786. case CTRL_PITCH_BENDER_RANGE:
  787. devc->voc[voice].bender_range = value;
  788. break;
  789. case CTL_MAIN_VOLUME:
  790. devc->voc[voice].volume = value / 128;
  791. break;
  792. case CTL_PAN:
  793. devc->voc[voice].panning = (value * 2) - 128;
  794. break;
  795. }
  796. }
  797. static void opl3_bender(int dev, int voice, int value)
  798. {
  799. if (voice < 0 || voice >= devc->nr_voice)
  800. return;
  801. bend_pitch(dev, voice, value - 8192);
  802. }
  803. static int opl3_alloc_voice(int dev, int chn, int note, struct voice_alloc_info *alloc)
  804. {
  805. int i, p, best, first, avail, best_time = 0x7fffffff;
  806. struct sbi_instrument *instr;
  807. int is4op;
  808. int instr_no;
  809. if (chn < 0 || chn > 15)
  810. instr_no = 0;
  811. else
  812. instr_no = devc->chn_info[chn].pgm_num;
  813. instr = &devc->i_map[instr_no];
  814. if (instr->channel < 0 || /* Instrument not loaded */
  815. devc->nr_voice != 12) /* Not in 4 OP mode */
  816. is4op = 0;
  817. else if (devc->nr_voice == 12) /* 4 OP mode */
  818. is4op = (instr->key == OPL3_PATCH);
  819. else
  820. is4op = 0;
  821. if (is4op)
  822. {
  823. first = p = 0;
  824. avail = 6;
  825. }
  826. else
  827. {
  828. if (devc->nr_voice == 12) /* 4 OP mode. Use the '2 OP only' operators first */
  829. first = p = 6;
  830. else
  831. first = p = 0;
  832. avail = devc->nr_voice;
  833. }
  834. /*
  835. * Now try to find a free voice
  836. */
  837. best = first;
  838. for (i = 0; i < avail; i++)
  839. {
  840. if (alloc->map[p] == 0)
  841. {
  842. return p;
  843. }
  844. if (alloc->alloc_times[p] < best_time) /* Find oldest playing note */
  845. {
  846. best_time = alloc->alloc_times[p];
  847. best = p;
  848. }
  849. p = (p + 1) % avail;
  850. }
  851. /*
  852. * Insert some kind of priority mechanism here.
  853. */
  854. if (best < 0)
  855. best = 0;
  856. if (best > devc->nr_voice)
  857. best -= devc->nr_voice;
  858. return best; /* All devc->voc in use. Select the first one. */
  859. }
  860. static void opl3_setup_voice(int dev, int voice, int chn)
  861. {
  862. struct channel_info *info =
  863. &synth_devs[dev]->chn_info[chn];
  864. opl3_set_instr(dev, voice, info->pgm_num);
  865. devc->voc[voice].bender = 0;
  866. devc->voc[voice].bender_range = info->bender_range;
  867. devc->voc[voice].volume = info->controllers[CTL_MAIN_VOLUME];
  868. devc->voc[voice].panning = (info->controllers[CTL_PAN] * 2) - 128;
  869. }
  870. static struct synth_operations opl3_operations =
  871. {
  872. .owner = THIS_MODULE,
  873. .id = "OPL",
  874. .info = NULL,
  875. .midi_dev = 0,
  876. .synth_type = SYNTH_TYPE_FM,
  877. .synth_subtype = FM_TYPE_ADLIB,
  878. .open = opl3_open,
  879. .close = opl3_close,
  880. .ioctl = opl3_ioctl,
  881. .kill_note = opl3_kill_note,
  882. .start_note = opl3_start_note,
  883. .set_instr = opl3_set_instr,
  884. .reset = opl3_reset,
  885. .hw_control = opl3_hw_control,
  886. .load_patch = opl3_load_patch,
  887. .aftertouch = opl3_aftertouch,
  888. .controller = opl3_controller,
  889. .panning = opl3_panning,
  890. .volume_method = opl3_volume_method,
  891. .bender = opl3_bender,
  892. .alloc_voice = opl3_alloc_voice,
  893. .setup_voice = opl3_setup_voice
  894. };
  895. int opl3_init(int ioaddr, int *osp, struct module *owner)
  896. {
  897. int i;
  898. int me;
  899. if (devc == NULL)
  900. {
  901. printk(KERN_ERR "opl3: Device control structure not initialized.\n");
  902. return -1;
  903. }
  904. if ((me = sound_alloc_synthdev()) == -1)
  905. {
  906. printk(KERN_WARNING "opl3: Too many synthesizers\n");
  907. return -1;
  908. }
  909. devc->nr_voice = 9;
  910. devc->fm_info.device = 0;
  911. devc->fm_info.synth_type = SYNTH_TYPE_FM;
  912. devc->fm_info.synth_subtype = FM_TYPE_ADLIB;
  913. devc->fm_info.perc_mode = 0;
  914. devc->fm_info.nr_voices = 9;
  915. devc->fm_info.nr_drums = 0;
  916. devc->fm_info.instr_bank_size = SBFM_MAXINSTR;
  917. devc->fm_info.capabilities = 0;
  918. devc->left_io = ioaddr;
  919. devc->right_io = ioaddr + 2;
  920. if (detected_model <= 2)
  921. devc->model = 1;
  922. else
  923. {
  924. devc->model = 2;
  925. if (detected_model == 4)
  926. devc->is_opl4 = 1;
  927. }
  928. opl3_operations.info = &devc->fm_info;
  929. synth_devs[me] = &opl3_operations;
  930. if (owner)
  931. synth_devs[me]->owner = owner;
  932. sequencer_init();
  933. devc->v_alloc = &opl3_operations.alloc;
  934. devc->chn_info = &opl3_operations.chn_info[0];
  935. if (devc->model == 2)
  936. {
  937. if (devc->is_opl4)
  938. strcpy(devc->fm_info.name, "Yamaha OPL4/OPL3 FM");
  939. else
  940. strcpy(devc->fm_info.name, "Yamaha OPL3");
  941. devc->v_alloc->max_voice = devc->nr_voice = 18;
  942. devc->fm_info.nr_drums = 0;
  943. devc->fm_info.synth_subtype = FM_TYPE_OPL3;
  944. devc->fm_info.capabilities |= SYNTH_CAP_OPL3;
  945. for (i = 0; i < 18; i++)
  946. {
  947. if (pv_map[i].ioaddr == USE_LEFT)
  948. pv_map[i].ioaddr = devc->left_io;
  949. else
  950. pv_map[i].ioaddr = devc->right_io;
  951. }
  952. opl3_command(devc->right_io, OPL3_MODE_REGISTER, OPL3_ENABLE);
  953. opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x00);
  954. }
  955. else
  956. {
  957. strcpy(devc->fm_info.name, "Yamaha OPL2");
  958. devc->v_alloc->max_voice = devc->nr_voice = 9;
  959. devc->fm_info.nr_drums = 0;
  960. for (i = 0; i < 18; i++)
  961. pv_map[i].ioaddr = devc->left_io;
  962. };
  963. conf_printf2(devc->fm_info.name, ioaddr, 0, -1, -1);
  964. for (i = 0; i < SBFM_MAXINSTR; i++)
  965. devc->i_map[i].channel = -1;
  966. return me;
  967. }
  968. EXPORT_SYMBOL(opl3_init);
  969. EXPORT_SYMBOL(opl3_detect);
  970. static int me;
  971. static int io = -1;
  972. module_param(io, int, 0);
  973. static int __init init_opl3 (void)
  974. {
  975. printk(KERN_INFO "YM3812 and OPL-3 driver Copyright (C) by Hannu Savolainen, Rob Hooft 1993-1996\n");
  976. if (io != -1) /* User loading pure OPL3 module */
  977. {
  978. if (!opl3_detect(io, NULL))
  979. {
  980. return -ENODEV;
  981. }
  982. me = opl3_init(io, NULL, THIS_MODULE);
  983. }
  984. return 0;
  985. }
  986. static void __exit cleanup_opl3(void)
  987. {
  988. if (devc && io != -1)
  989. {
  990. if (devc->base) {
  991. release_region(devc->base,4);
  992. if (devc->is_opl4)
  993. release_region(devc->base - 8, 2);
  994. }
  995. kfree(devc);
  996. devc = NULL;
  997. sound_unload_synthdev(me);
  998. }
  999. }
  1000. module_init(init_opl3);
  1001. module_exit(cleanup_opl3);
  1002. #ifndef MODULE
  1003. static int __init setup_opl3(char *str)
  1004. {
  1005. /* io */
  1006. int ints[2];
  1007. str = get_options(str, ARRAY_SIZE(ints), ints);
  1008. io = ints[1];
  1009. return 1;
  1010. }
  1011. __setup("opl3=", setup_opl3);
  1012. #endif
  1013. MODULE_LICENSE("GPL");