midi_synth.c 14 KB

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