opl3.c 27 KB

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