midi_synth.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * sound/oss/midi_synth.c
  3. *
  4. * High level midi sequencer manager for dumb MIDI interfaces.
  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. * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
  15. * Andrew Veliath : fixed running status in MIDI input state machine
  16. */
  17. #define USE_SEQ_MACROS
  18. #define USE_SIMPLE_MACROS
  19. #include "sound_config.h"
  20. #define _MIDI_SYNTH_C_
  21. #include "midi_synth.h"
  22. static int midi2synth[MAX_MIDI_DEV];
  23. static int sysex_state[MAX_MIDI_DEV] =
  24. {0};
  25. static unsigned char prev_out_status[MAX_MIDI_DEV];
  26. #define STORE(cmd) \
  27. { \
  28. int len; \
  29. unsigned char obuf[8]; \
  30. cmd; \
  31. seq_input_event(obuf, len); \
  32. }
  33. #define _seqbuf obuf
  34. #define _seqbufptr 0
  35. #define _SEQ_ADVBUF(x) len=x
  36. void
  37. do_midi_msg(int synthno, unsigned char *msg, int mlen)
  38. {
  39. switch (msg[0] & 0xf0)
  40. {
  41. case 0x90:
  42. if (msg[2] != 0)
  43. {
  44. STORE(SEQ_START_NOTE(synthno, msg[0] & 0x0f, msg[1], msg[2]));
  45. break;
  46. }
  47. msg[2] = 64;
  48. case 0x80:
  49. STORE(SEQ_STOP_NOTE(synthno, msg[0] & 0x0f, msg[1], msg[2]));
  50. break;
  51. case 0xA0:
  52. STORE(SEQ_KEY_PRESSURE(synthno, msg[0] & 0x0f, msg[1], msg[2]));
  53. break;
  54. case 0xB0:
  55. STORE(SEQ_CONTROL(synthno, msg[0] & 0x0f,
  56. msg[1], msg[2]));
  57. break;
  58. case 0xC0:
  59. STORE(SEQ_SET_PATCH(synthno, msg[0] & 0x0f, msg[1]));
  60. break;
  61. case 0xD0:
  62. STORE(SEQ_CHN_PRESSURE(synthno, msg[0] & 0x0f, msg[1]));
  63. break;
  64. case 0xE0:
  65. STORE(SEQ_BENDER(synthno, msg[0] & 0x0f,
  66. (msg[1] & 0x7f) | ((msg[2] & 0x7f) << 7)));
  67. break;
  68. default:
  69. /* printk( "MPU: Unknown midi channel message %02x\n", msg[0]); */
  70. ;
  71. }
  72. }
  73. EXPORT_SYMBOL(do_midi_msg);
  74. static void
  75. midi_outc(int midi_dev, int data)
  76. {
  77. int timeout;
  78. for (timeout = 0; timeout < 3200; timeout++)
  79. if (midi_devs[midi_dev]->outputc(midi_dev, (unsigned char) (data & 0xff)))
  80. {
  81. if (data & 0x80) /*
  82. * Status byte
  83. */
  84. prev_out_status[midi_dev] =
  85. (unsigned char) (data & 0xff); /*
  86. * Store for running status
  87. */
  88. return; /*
  89. * Mission complete
  90. */
  91. }
  92. /*
  93. * Sorry! No space on buffers.
  94. */
  95. printk("Midi send timed out\n");
  96. }
  97. static int
  98. prefix_cmd(int midi_dev, unsigned char status)
  99. {
  100. if ((char *) midi_devs[midi_dev]->prefix_cmd == NULL)
  101. return 1;
  102. return midi_devs[midi_dev]->prefix_cmd(midi_dev, status);
  103. }
  104. static void
  105. midi_synth_input(int orig_dev, unsigned char data)
  106. {
  107. int dev;
  108. struct midi_input_info *inc;
  109. static unsigned char len_tab[] = /* # of data bytes following a status
  110. */
  111. {
  112. 2, /* 8x */
  113. 2, /* 9x */
  114. 2, /* Ax */
  115. 2, /* Bx */
  116. 1, /* Cx */
  117. 1, /* Dx */
  118. 2, /* Ex */
  119. 0 /* Fx */
  120. };
  121. if (orig_dev < 0 || orig_dev > num_midis || midi_devs[orig_dev] == NULL)
  122. return;
  123. if (data == 0xfe) /* Ignore active sensing */
  124. return;
  125. dev = midi2synth[orig_dev];
  126. inc = &midi_devs[orig_dev]->in_info;
  127. switch (inc->m_state)
  128. {
  129. case MST_INIT:
  130. if (data & 0x80) /* MIDI status byte */
  131. {
  132. if ((data & 0xf0) == 0xf0) /* Common message */
  133. {
  134. switch (data)
  135. {
  136. case 0xf0: /* Sysex */
  137. inc->m_state = MST_SYSEX;
  138. break; /* Sysex */
  139. case 0xf1: /* MTC quarter frame */
  140. case 0xf3: /* Song select */
  141. inc->m_state = MST_DATA;
  142. inc->m_ptr = 1;
  143. inc->m_left = 1;
  144. inc->m_buf[0] = data;
  145. break;
  146. case 0xf2: /* Song position pointer */
  147. inc->m_state = MST_DATA;
  148. inc->m_ptr = 1;
  149. inc->m_left = 2;
  150. inc->m_buf[0] = data;
  151. break;
  152. default:
  153. inc->m_buf[0] = data;
  154. inc->m_ptr = 1;
  155. do_midi_msg(dev, inc->m_buf, inc->m_ptr);
  156. inc->m_ptr = 0;
  157. inc->m_left = 0;
  158. }
  159. } else
  160. {
  161. inc->m_state = MST_DATA;
  162. inc->m_ptr = 1;
  163. inc->m_left = len_tab[(data >> 4) - 8];
  164. inc->m_buf[0] = inc->m_prev_status = data;
  165. }
  166. } else if (inc->m_prev_status & 0x80) {
  167. /* Data byte (use running status) */
  168. inc->m_ptr = 2;
  169. inc->m_buf[1] = data;
  170. inc->m_buf[0] = inc->m_prev_status;
  171. inc->m_left = len_tab[(inc->m_buf[0] >> 4) - 8] - 1;
  172. if (inc->m_left > 0)
  173. inc->m_state = MST_DATA; /* Not done yet */
  174. else {
  175. inc->m_state = MST_INIT;
  176. do_midi_msg(dev, inc->m_buf, inc->m_ptr);
  177. inc->m_ptr = 0;
  178. }
  179. }
  180. break; /* MST_INIT */
  181. case MST_DATA:
  182. inc->m_buf[inc->m_ptr++] = data;
  183. if (--inc->m_left <= 0)
  184. {
  185. inc->m_state = MST_INIT;
  186. do_midi_msg(dev, inc->m_buf, inc->m_ptr);
  187. inc->m_ptr = 0;
  188. }
  189. break; /* MST_DATA */
  190. case MST_SYSEX:
  191. if (data == 0xf7) /* Sysex end */
  192. {
  193. inc->m_state = MST_INIT;
  194. inc->m_left = 0;
  195. inc->m_ptr = 0;
  196. }
  197. break; /* MST_SYSEX */
  198. default:
  199. printk("MIDI%d: Unexpected state %d (%02x)\n", orig_dev, inc->m_state, (int) data);
  200. inc->m_state = MST_INIT;
  201. }
  202. }
  203. static void
  204. leave_sysex(int dev)
  205. {
  206. int orig_dev = synth_devs[dev]->midi_dev;
  207. int timeout = 0;
  208. if (!sysex_state[dev])
  209. return;
  210. sysex_state[dev] = 0;
  211. while (!midi_devs[orig_dev]->outputc(orig_dev, 0xf7) &&
  212. timeout < 1000)
  213. timeout++;
  214. sysex_state[dev] = 0;
  215. }
  216. static void
  217. midi_synth_output(int dev)
  218. {
  219. /*
  220. * Currently NOP
  221. */
  222. }
  223. int midi_synth_ioctl(int dev, unsigned int cmd, void __user *arg)
  224. {
  225. /*
  226. * int orig_dev = synth_devs[dev]->midi_dev;
  227. */
  228. switch (cmd) {
  229. case SNDCTL_SYNTH_INFO:
  230. if (__copy_to_user(arg, synth_devs[dev]->info, sizeof(struct synth_info)))
  231. return -EFAULT;
  232. return 0;
  233. case SNDCTL_SYNTH_MEMAVL:
  234. return 0x7fffffff;
  235. default:
  236. return -EINVAL;
  237. }
  238. }
  239. EXPORT_SYMBOL(midi_synth_ioctl);
  240. int
  241. midi_synth_kill_note(int dev, int channel, int note, int velocity)
  242. {
  243. int orig_dev = synth_devs[dev]->midi_dev;
  244. int msg, chn;
  245. if (note < 0 || note > 127)
  246. return 0;
  247. if (channel < 0 || channel > 15)
  248. return 0;
  249. if (velocity < 0)
  250. velocity = 0;
  251. if (velocity > 127)
  252. velocity = 127;
  253. leave_sysex(dev);
  254. msg = prev_out_status[orig_dev] & 0xf0;
  255. chn = prev_out_status[orig_dev] & 0x0f;
  256. if (chn == channel && ((msg == 0x90 && velocity == 64) || msg == 0x80))
  257. { /*
  258. * Use running status
  259. */
  260. if (!prefix_cmd(orig_dev, note))
  261. return 0;
  262. midi_outc(orig_dev, note);
  263. if (msg == 0x90) /*
  264. * Running status = Note on
  265. */
  266. midi_outc(orig_dev, 0); /*
  267. * Note on with velocity 0 == note
  268. * off
  269. */
  270. else
  271. midi_outc(orig_dev, velocity);
  272. } else
  273. {
  274. if (velocity == 64)
  275. {
  276. if (!prefix_cmd(orig_dev, 0x90 | (channel & 0x0f)))
  277. return 0;
  278. midi_outc(orig_dev, 0x90 | (channel & 0x0f)); /*
  279. * Note on
  280. */
  281. midi_outc(orig_dev, note);
  282. midi_outc(orig_dev, 0); /*
  283. * Zero G
  284. */
  285. } else
  286. {
  287. if (!prefix_cmd(orig_dev, 0x80 | (channel & 0x0f)))
  288. return 0;
  289. midi_outc(orig_dev, 0x80 | (channel & 0x0f)); /*
  290. * Note off
  291. */
  292. midi_outc(orig_dev, note);
  293. midi_outc(orig_dev, velocity);
  294. }
  295. }
  296. return 0;
  297. }
  298. EXPORT_SYMBOL(midi_synth_kill_note);
  299. int
  300. midi_synth_set_instr(int dev, int channel, int instr_no)
  301. {
  302. int orig_dev = synth_devs[dev]->midi_dev;
  303. if (instr_no < 0 || instr_no > 127)
  304. instr_no = 0;
  305. if (channel < 0 || channel > 15)
  306. return 0;
  307. leave_sysex(dev);
  308. if (!prefix_cmd(orig_dev, 0xc0 | (channel & 0x0f)))
  309. return 0;
  310. midi_outc(orig_dev, 0xc0 | (channel & 0x0f)); /*
  311. * Program change
  312. */
  313. midi_outc(orig_dev, instr_no);
  314. return 0;
  315. }
  316. EXPORT_SYMBOL(midi_synth_set_instr);
  317. int
  318. midi_synth_start_note(int dev, int channel, int note, int velocity)
  319. {
  320. int orig_dev = synth_devs[dev]->midi_dev;
  321. int msg, chn;
  322. if (note < 0 || note > 127)
  323. return 0;
  324. if (channel < 0 || channel > 15)
  325. return 0;
  326. if (velocity < 0)
  327. velocity = 0;
  328. if (velocity > 127)
  329. velocity = 127;
  330. leave_sysex(dev);
  331. msg = prev_out_status[orig_dev] & 0xf0;
  332. chn = prev_out_status[orig_dev] & 0x0f;
  333. if (chn == channel && msg == 0x90)
  334. { /*
  335. * Use running status
  336. */
  337. if (!prefix_cmd(orig_dev, note))
  338. return 0;
  339. midi_outc(orig_dev, note);
  340. midi_outc(orig_dev, velocity);
  341. } else
  342. {
  343. if (!prefix_cmd(orig_dev, 0x90 | (channel & 0x0f)))
  344. return 0;
  345. midi_outc(orig_dev, 0x90 | (channel & 0x0f)); /*
  346. * Note on
  347. */
  348. midi_outc(orig_dev, note);
  349. midi_outc(orig_dev, velocity);
  350. }
  351. return 0;
  352. }
  353. EXPORT_SYMBOL(midi_synth_start_note);
  354. void
  355. midi_synth_reset(int dev)
  356. {
  357. leave_sysex(dev);
  358. }
  359. EXPORT_SYMBOL(midi_synth_reset);
  360. int
  361. midi_synth_open(int dev, int mode)
  362. {
  363. int orig_dev = synth_devs[dev]->midi_dev;
  364. int err;
  365. struct midi_input_info *inc;
  366. if (orig_dev < 0 || orig_dev >= num_midis || midi_devs[orig_dev] == NULL)
  367. return -ENXIO;
  368. midi2synth[orig_dev] = dev;
  369. sysex_state[dev] = 0;
  370. prev_out_status[orig_dev] = 0;
  371. if ((err = midi_devs[orig_dev]->open(orig_dev, mode,
  372. midi_synth_input, midi_synth_output)) < 0)
  373. return err;
  374. inc = &midi_devs[orig_dev]->in_info;
  375. /* save_flags(flags);
  376. cli();
  377. don't know against what irqhandler to protect*/
  378. inc->m_busy = 0;
  379. inc->m_state = MST_INIT;
  380. inc->m_ptr = 0;
  381. inc->m_left = 0;
  382. inc->m_prev_status = 0x00;
  383. /* restore_flags(flags); */
  384. return 1;
  385. }
  386. EXPORT_SYMBOL(midi_synth_open);
  387. void
  388. midi_synth_close(int dev)
  389. {
  390. int orig_dev = synth_devs[dev]->midi_dev;
  391. leave_sysex(dev);
  392. /*
  393. * Shut up the synths by sending just single active sensing message.
  394. */
  395. midi_devs[orig_dev]->outputc(orig_dev, 0xfe);
  396. midi_devs[orig_dev]->close(orig_dev);
  397. }
  398. EXPORT_SYMBOL(midi_synth_close);
  399. void
  400. midi_synth_hw_control(int dev, unsigned char *event)
  401. {
  402. }
  403. EXPORT_SYMBOL(midi_synth_hw_control);
  404. int
  405. midi_synth_load_patch(int dev, int format, const char __user *addr,
  406. int count, int pmgr_flag)
  407. {
  408. int orig_dev = synth_devs[dev]->midi_dev;
  409. struct sysex_info sysex;
  410. int i;
  411. unsigned long left, src_offs, eox_seen = 0;
  412. int first_byte = 1;
  413. int hdr_size = (unsigned long) &sysex.data[0] - (unsigned long) &sysex;
  414. leave_sysex(dev);
  415. if (!prefix_cmd(orig_dev, 0xf0))
  416. return 0;
  417. /* Invalid patch format */
  418. if (format != SYSEX_PATCH)
  419. return -EINVAL;
  420. /* Patch header too short */
  421. if (count < hdr_size)
  422. return -EINVAL;
  423. count -= hdr_size;
  424. /*
  425. * Copy the header from user space
  426. */
  427. if (copy_from_user(&sysex, addr, hdr_size))
  428. return -EFAULT;
  429. /* Sysex record too short */
  430. if ((unsigned)count < (unsigned)sysex.len)
  431. sysex.len = count;
  432. left = sysex.len;
  433. src_offs = 0;
  434. for (i = 0; i < left && !signal_pending(current); i++)
  435. {
  436. unsigned char data;
  437. if (get_user(data,
  438. (unsigned char __user *)(addr + hdr_size + i)))
  439. return -EFAULT;
  440. eox_seen = (i > 0 && data & 0x80); /* End of sysex */
  441. if (eox_seen && data != 0xf7)
  442. data = 0xf7;
  443. if (i == 0)
  444. {
  445. if (data != 0xf0)
  446. {
  447. printk(KERN_WARNING "midi_synth: Sysex start missing\n");
  448. return -EINVAL;
  449. }
  450. }
  451. while (!midi_devs[orig_dev]->outputc(orig_dev, (unsigned char) (data & 0xff)) &&
  452. !signal_pending(current))
  453. schedule();
  454. if (!first_byte && data & 0x80)
  455. return 0;
  456. first_byte = 0;
  457. }
  458. if (!eox_seen)
  459. midi_outc(orig_dev, 0xf7);
  460. return 0;
  461. }
  462. EXPORT_SYMBOL(midi_synth_load_patch);
  463. void midi_synth_panning(int dev, int channel, int pressure)
  464. {
  465. }
  466. EXPORT_SYMBOL(midi_synth_panning);
  467. void midi_synth_aftertouch(int dev, int channel, int pressure)
  468. {
  469. int orig_dev = synth_devs[dev]->midi_dev;
  470. int msg, chn;
  471. if (pressure < 0 || pressure > 127)
  472. return;
  473. if (channel < 0 || channel > 15)
  474. return;
  475. leave_sysex(dev);
  476. msg = prev_out_status[orig_dev] & 0xf0;
  477. chn = prev_out_status[orig_dev] & 0x0f;
  478. if (msg != 0xd0 || chn != channel) /*
  479. * Test for running status
  480. */
  481. {
  482. if (!prefix_cmd(orig_dev, 0xd0 | (channel & 0x0f)))
  483. return;
  484. midi_outc(orig_dev, 0xd0 | (channel & 0x0f)); /*
  485. * Channel pressure
  486. */
  487. } else if (!prefix_cmd(orig_dev, pressure))
  488. return;
  489. midi_outc(orig_dev, pressure);
  490. }
  491. EXPORT_SYMBOL(midi_synth_aftertouch);
  492. void
  493. midi_synth_controller(int dev, int channel, int ctrl_num, int value)
  494. {
  495. int orig_dev = synth_devs[dev]->midi_dev;
  496. int chn, msg;
  497. if (ctrl_num < 0 || ctrl_num > 127)
  498. return;
  499. if (channel < 0 || channel > 15)
  500. return;
  501. leave_sysex(dev);
  502. msg = prev_out_status[orig_dev] & 0xf0;
  503. chn = prev_out_status[orig_dev] & 0x0f;
  504. if (msg != 0xb0 || chn != channel)
  505. {
  506. if (!prefix_cmd(orig_dev, 0xb0 | (channel & 0x0f)))
  507. return;
  508. midi_outc(orig_dev, 0xb0 | (channel & 0x0f));
  509. } else if (!prefix_cmd(orig_dev, ctrl_num))
  510. return;
  511. midi_outc(orig_dev, ctrl_num);
  512. midi_outc(orig_dev, value & 0x7f);
  513. }
  514. EXPORT_SYMBOL(midi_synth_controller);
  515. void
  516. midi_synth_bender(int dev, int channel, int value)
  517. {
  518. int orig_dev = synth_devs[dev]->midi_dev;
  519. int msg, prev_chn;
  520. if (channel < 0 || channel > 15)
  521. return;
  522. if (value < 0 || value > 16383)
  523. return;
  524. leave_sysex(dev);
  525. msg = prev_out_status[orig_dev] & 0xf0;
  526. prev_chn = prev_out_status[orig_dev] & 0x0f;
  527. if (msg != 0xd0 || prev_chn != channel) /*
  528. * Test for running status
  529. */
  530. {
  531. if (!prefix_cmd(orig_dev, 0xe0 | (channel & 0x0f)))
  532. return;
  533. midi_outc(orig_dev, 0xe0 | (channel & 0x0f));
  534. } else if (!prefix_cmd(orig_dev, value & 0x7f))
  535. return;
  536. midi_outc(orig_dev, value & 0x7f);
  537. midi_outc(orig_dev, (value >> 7) & 0x7f);
  538. }
  539. EXPORT_SYMBOL(midi_synth_bender);
  540. void
  541. midi_synth_setup_voice(int dev, int voice, int channel)
  542. {
  543. }
  544. EXPORT_SYMBOL(midi_synth_setup_voice);
  545. int
  546. midi_synth_send_sysex(int dev, unsigned char *bytes, int len)
  547. {
  548. int orig_dev = synth_devs[dev]->midi_dev;
  549. int i;
  550. for (i = 0; i < len; i++)
  551. {
  552. switch (bytes[i])
  553. {
  554. case 0xf0: /* Start sysex */
  555. if (!prefix_cmd(orig_dev, 0xf0))
  556. return 0;
  557. sysex_state[dev] = 1;
  558. break;
  559. case 0xf7: /* End sysex */
  560. if (!sysex_state[dev]) /* Orphan sysex end */
  561. return 0;
  562. sysex_state[dev] = 0;
  563. break;
  564. default:
  565. if (!sysex_state[dev])
  566. return 0;
  567. if (bytes[i] & 0x80) /* Error. Another message before sysex end */
  568. {
  569. bytes[i] = 0xf7; /* Sysex end */
  570. sysex_state[dev] = 0;
  571. }
  572. }
  573. if (!midi_devs[orig_dev]->outputc(orig_dev, bytes[i]))
  574. {
  575. /*
  576. * Hardware level buffer is full. Abort the sysex message.
  577. */
  578. int timeout = 0;
  579. bytes[i] = 0xf7;
  580. sysex_state[dev] = 0;
  581. while (!midi_devs[orig_dev]->outputc(orig_dev, bytes[i]) &&
  582. timeout < 1000)
  583. timeout++;
  584. }
  585. if (!sysex_state[dev])
  586. return 0;
  587. }
  588. return 0;
  589. }
  590. EXPORT_SYMBOL(midi_synth_send_sysex);