opl3.c 27 KB

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