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/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. 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. };
  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. 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 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. if (copy_from_user(&ins, addr, sizeof(ins)))
  669. return -EFAULT;
  670. if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
  671. {
  672. printk(KERN_WARNING "FM Error: Invalid instrument number %d\n", ins.channel);
  673. return -EINVAL;
  674. }
  675. ins.key = format;
  676. return store_instr(ins.channel, &ins);
  677. }
  678. static void opl3_panning(int dev, int voice, int value)
  679. {
  680. if (voice < 0 || voice >= devc->nr_voice)
  681. return;
  682. devc->voc[voice].panning = value;
  683. }
  684. static void opl3_volume_method(int dev, int mode)
  685. {
  686. }
  687. #define SET_VIBRATO(cell) { \
  688. tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
  689. if (pressure > 110) \
  690. tmp |= 0x40; /* Vibrato on */ \
  691. opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
  692. static void opl3_aftertouch(int dev, int voice, int pressure)
  693. {
  694. int tmp;
  695. struct sbi_instrument *instr;
  696. struct physical_voice_info *map;
  697. if (voice < 0 || voice >= devc->nr_voice)
  698. return;
  699. map = &pv_map[devc->lv_map[voice]];
  700. if (map->voice_mode == 0)
  701. return;
  702. /*
  703. * Adjust the amount of vibrato depending the pressure
  704. */
  705. instr = devc->act_i[voice];
  706. if (!instr)
  707. instr = &devc->i_map[0];
  708. if (devc->voc[voice].mode == 4)
  709. {
  710. int connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
  711. switch (connection)
  712. {
  713. case 0:
  714. SET_VIBRATO(4);
  715. break;
  716. case 1:
  717. SET_VIBRATO(2);
  718. SET_VIBRATO(4);
  719. break;
  720. case 2:
  721. SET_VIBRATO(1);
  722. SET_VIBRATO(4);
  723. break;
  724. case 3:
  725. SET_VIBRATO(1);
  726. SET_VIBRATO(3);
  727. SET_VIBRATO(4);
  728. break;
  729. }
  730. /*
  731. * Not implemented yet
  732. */
  733. }
  734. else
  735. {
  736. SET_VIBRATO(1);
  737. if ((instr->operators[10] & 0x01)) /*
  738. * Additive synthesis
  739. */
  740. SET_VIBRATO(2);
  741. }
  742. }
  743. #undef SET_VIBRATO
  744. static void bend_pitch(int dev, int voice, int value)
  745. {
  746. unsigned char data;
  747. int block, fnum, freq;
  748. struct physical_voice_info *map;
  749. map = &pv_map[devc->lv_map[voice]];
  750. if (map->voice_mode == 0)
  751. return;
  752. devc->voc[voice].bender = value;
  753. if (!value)
  754. return;
  755. if (!(devc->voc[voice].keyon_byte & 0x20))
  756. return; /*
  757. * Not keyed on
  758. */
  759. freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range, 0);
  760. devc->voc[voice].current_freq = freq;
  761. freq_to_fnum(freq, &block, &fnum);
  762. data = fnum & 0xff; /*
  763. * Least significant bits of fnumber
  764. */
  765. opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data);
  766. data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
  767. devc->voc[voice].keyon_byte = data;
  768. opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data);
  769. }
  770. static void opl3_controller (int dev, int voice, int ctrl_num, int value)
  771. {
  772. if (voice < 0 || voice >= devc->nr_voice)
  773. return;
  774. switch (ctrl_num)
  775. {
  776. case CTRL_PITCH_BENDER:
  777. bend_pitch(dev, voice, value);
  778. break;
  779. case CTRL_PITCH_BENDER_RANGE:
  780. devc->voc[voice].bender_range = value;
  781. break;
  782. case CTL_MAIN_VOLUME:
  783. devc->voc[voice].volume = value / 128;
  784. break;
  785. case CTL_PAN:
  786. devc->voc[voice].panning = (value * 2) - 128;
  787. break;
  788. }
  789. }
  790. static void opl3_bender(int dev, int voice, int value)
  791. {
  792. if (voice < 0 || voice >= devc->nr_voice)
  793. return;
  794. bend_pitch(dev, voice, value - 8192);
  795. }
  796. static int opl3_alloc_voice(int dev, int chn, int note, struct voice_alloc_info *alloc)
  797. {
  798. int i, p, best, first, avail, best_time = 0x7fffffff;
  799. struct sbi_instrument *instr;
  800. int is4op;
  801. int instr_no;
  802. if (chn < 0 || chn > 15)
  803. instr_no = 0;
  804. else
  805. instr_no = devc->chn_info[chn].pgm_num;
  806. instr = &devc->i_map[instr_no];
  807. if (instr->channel < 0 || /* Instrument not loaded */
  808. devc->nr_voice != 12) /* Not in 4 OP mode */
  809. is4op = 0;
  810. else if (devc->nr_voice == 12) /* 4 OP mode */
  811. is4op = (instr->key == OPL3_PATCH);
  812. else
  813. is4op = 0;
  814. if (is4op)
  815. {
  816. first = p = 0;
  817. avail = 6;
  818. }
  819. else
  820. {
  821. if (devc->nr_voice == 12) /* 4 OP mode. Use the '2 OP only' operators first */
  822. first = p = 6;
  823. else
  824. first = p = 0;
  825. avail = devc->nr_voice;
  826. }
  827. /*
  828. * Now try to find a free voice
  829. */
  830. best = first;
  831. for (i = 0; i < avail; i++)
  832. {
  833. if (alloc->map[p] == 0)
  834. {
  835. return p;
  836. }
  837. if (alloc->alloc_times[p] < best_time) /* Find oldest playing note */
  838. {
  839. best_time = alloc->alloc_times[p];
  840. best = p;
  841. }
  842. p = (p + 1) % avail;
  843. }
  844. /*
  845. * Insert some kind of priority mechanism here.
  846. */
  847. if (best < 0)
  848. best = 0;
  849. if (best > devc->nr_voice)
  850. best -= devc->nr_voice;
  851. return best; /* All devc->voc in use. Select the first one. */
  852. }
  853. static void opl3_setup_voice(int dev, int voice, int chn)
  854. {
  855. struct channel_info *info;
  856. if (voice < 0 || voice >= devc->nr_voice)
  857. return;
  858. if (chn < 0 || chn > 15)
  859. return;
  860. info = &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_hw(io, int, ioport, 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");