opl3.c 27 KB

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