opl3.c 27 KB

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