wf_midi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /*
  2. * sound/wf_midi.c
  3. *
  4. * The low level driver for the WaveFront ICS2115 MIDI interface(s)
  5. * Note that there is also an MPU-401 emulation (actually, a UART-401
  6. * emulation) on the CS4232 on the Tropez Plus. This code has nothing
  7. * to do with that interface at all.
  8. *
  9. * The interface is essentially just a UART-401, but is has the
  10. * interesting property of supporting what Turtle Beach called
  11. * "Virtual MIDI" mode. In this mode, there are effectively *two*
  12. * MIDI buses accessible via the interface, one that is routed
  13. * solely to/from the external WaveFront synthesizer and the other
  14. * corresponding to the pin/socket connector used to link external
  15. * MIDI devices to the board.
  16. *
  17. * This driver fully supports this mode, allowing two distinct
  18. * midi devices (/dev/midiNN and /dev/midiNN+1) to be used
  19. * completely independently, giving 32 channels of MIDI routing,
  20. * 16 to the WaveFront synth and 16 to the external MIDI bus.
  21. *
  22. * Switching between the two is accomplished externally by the driver
  23. * using the two otherwise unused MIDI bytes. See the code for more details.
  24. *
  25. * NOTE: VIRTUAL MIDI MODE IS ON BY DEFAULT (see wavefront.c)
  26. *
  27. * The main reason to turn off Virtual MIDI mode is when you want to
  28. * tightly couple the WaveFront synth with an external MIDI
  29. * device. You won't be able to distinguish the source of any MIDI
  30. * data except via SysEx ID, but thats probably OK, since for the most
  31. * part, the WaveFront won't be sending any MIDI data at all.
  32. *
  33. * The main reason to turn on Virtual MIDI Mode is to provide two
  34. * completely independent 16-channel MIDI buses, one to the
  35. * WaveFront and one to any external MIDI devices. Given the 32
  36. * voice nature of the WaveFront, its pretty easy to find a use
  37. * for all 16 channels driving just that synth.
  38. *
  39. */
  40. /*
  41. * Copyright (C) by Paul Barton-Davis 1998
  42. * Some portions of this file are derived from work that is:
  43. *
  44. * CopyriGht (C) by Hannu Savolainen 1993-1996
  45. *
  46. * USS/Lite for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  47. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  48. * for more info.
  49. */
  50. #include <linux/init.h>
  51. #include <linux/interrupt.h>
  52. #include <linux/spinlock.h>
  53. #include "sound_config.h"
  54. #include <linux/wavefront.h>
  55. #ifdef MODULE
  56. struct wf_mpu_config {
  57. int base;
  58. #define DATAPORT(d) (d)->base
  59. #define COMDPORT(d) (d)->base+1
  60. #define STATPORT(d) (d)->base+1
  61. int irq;
  62. int opened;
  63. int devno;
  64. int synthno;
  65. int mode;
  66. #define MODE_MIDI 1
  67. #define MODE_SYNTH 2
  68. void (*inputintr) (int dev, unsigned char data);
  69. char isvirtual; /* do virtual I/O stuff */
  70. };
  71. static struct wf_mpu_config devs[2];
  72. static struct wf_mpu_config *phys_dev = &devs[0];
  73. static struct wf_mpu_config *virt_dev = &devs[1];
  74. static void start_uart_mode (void);
  75. static DEFINE_SPINLOCK(lock);
  76. #define OUTPUT_READY 0x40
  77. #define INPUT_AVAIL 0x80
  78. #define MPU_ACK 0xFE
  79. #define UART_MODE_ON 0x3F
  80. static inline int wf_mpu_status (void)
  81. {
  82. return inb (STATPORT (phys_dev));
  83. }
  84. static inline int input_avail (void)
  85. {
  86. return !(wf_mpu_status() & INPUT_AVAIL);
  87. }
  88. static inline int output_ready (void)
  89. {
  90. return !(wf_mpu_status() & OUTPUT_READY);
  91. }
  92. static inline int read_data (void)
  93. {
  94. return inb (DATAPORT (phys_dev));
  95. }
  96. static inline void write_data (unsigned char byte)
  97. {
  98. outb (byte, DATAPORT (phys_dev));
  99. }
  100. /*
  101. * States for the input scanner (should be in dev_table.h)
  102. */
  103. #define MST_SYSMSG 100 /* System message (sysx etc). */
  104. #define MST_MTC 102 /* Midi Time Code (MTC) qframe msg */
  105. #define MST_SONGSEL 103 /* Song select */
  106. #define MST_SONGPOS 104 /* Song position pointer */
  107. #define MST_TIMED 105 /* Leading timing byte rcvd */
  108. /* buffer space check for input scanner */
  109. #define BUFTEST(mi) if (mi->m_ptr >= MI_MAX || mi->m_ptr < 0) \
  110. {printk(KERN_ERR "WF-MPU: Invalid buffer pointer %d/%d, s=%d\n", \
  111. mi->m_ptr, mi->m_left, mi->m_state);mi->m_ptr--;}
  112. static unsigned char len_tab[] = /* # of data bytes following a status
  113. */
  114. {
  115. 2, /* 8x */
  116. 2, /* 9x */
  117. 2, /* Ax */
  118. 2, /* Bx */
  119. 1, /* Cx */
  120. 1, /* Dx */
  121. 2, /* Ex */
  122. 0 /* Fx */
  123. };
  124. static int
  125. wf_mpu_input_scanner (int devno, int synthdev, unsigned char midic)
  126. {
  127. struct midi_input_info *mi = &midi_devs[devno]->in_info;
  128. switch (mi->m_state) {
  129. case MST_INIT:
  130. switch (midic) {
  131. case 0xf8:
  132. /* Timer overflow */
  133. break;
  134. case 0xfc:
  135. break;
  136. case 0xfd:
  137. /* XXX do something useful with this. If there is
  138. an external MIDI timer (e.g. a hardware sequencer,
  139. a useful timer can be derived ...
  140. For now, no timer support.
  141. */
  142. break;
  143. case 0xfe:
  144. return MPU_ACK;
  145. break;
  146. case 0xf0:
  147. case 0xf1:
  148. case 0xf2:
  149. case 0xf3:
  150. case 0xf4:
  151. case 0xf5:
  152. case 0xf6:
  153. case 0xf7:
  154. break;
  155. case 0xf9:
  156. break;
  157. case 0xff:
  158. mi->m_state = MST_SYSMSG;
  159. break;
  160. default:
  161. if (midic <= 0xef) {
  162. mi->m_state = MST_TIMED;
  163. }
  164. else
  165. printk (KERN_ERR "<MPU: Unknown event %02x> ",
  166. midic);
  167. }
  168. break;
  169. case MST_TIMED:
  170. {
  171. int msg = ((int) (midic & 0xf0) >> 4);
  172. mi->m_state = MST_DATA;
  173. if (msg < 8) { /* Data byte */
  174. msg = ((int) (mi->m_prev_status & 0xf0) >> 4);
  175. msg -= 8;
  176. mi->m_left = len_tab[msg] - 1;
  177. mi->m_ptr = 2;
  178. mi->m_buf[0] = mi->m_prev_status;
  179. mi->m_buf[1] = midic;
  180. if (mi->m_left <= 0) {
  181. mi->m_state = MST_INIT;
  182. do_midi_msg (synthdev, mi->m_buf, mi->m_ptr);
  183. mi->m_ptr = 0;
  184. }
  185. } else if (msg == 0xf) { /* MPU MARK */
  186. mi->m_state = MST_INIT;
  187. switch (midic) {
  188. case 0xf8:
  189. break;
  190. case 0xf9:
  191. break;
  192. case 0xfc:
  193. break;
  194. default:
  195. break;
  196. }
  197. } else {
  198. mi->m_prev_status = midic;
  199. msg -= 8;
  200. mi->m_left = len_tab[msg];
  201. mi->m_ptr = 1;
  202. mi->m_buf[0] = midic;
  203. if (mi->m_left <= 0) {
  204. mi->m_state = MST_INIT;
  205. do_midi_msg (synthdev, mi->m_buf, mi->m_ptr);
  206. mi->m_ptr = 0;
  207. }
  208. }
  209. }
  210. break;
  211. case MST_SYSMSG:
  212. switch (midic) {
  213. case 0xf0:
  214. mi->m_state = MST_SYSEX;
  215. break;
  216. case 0xf1:
  217. mi->m_state = MST_MTC;
  218. break;
  219. case 0xf2:
  220. mi->m_state = MST_SONGPOS;
  221. mi->m_ptr = 0;
  222. break;
  223. case 0xf3:
  224. mi->m_state = MST_SONGSEL;
  225. break;
  226. case 0xf6:
  227. mi->m_state = MST_INIT;
  228. /*
  229. * Real time messages
  230. */
  231. case 0xf8:
  232. /* midi clock */
  233. mi->m_state = MST_INIT;
  234. /* XXX need ext MIDI timer support */
  235. break;
  236. case 0xfA:
  237. mi->m_state = MST_INIT;
  238. /* XXX need ext MIDI timer support */
  239. break;
  240. case 0xFB:
  241. mi->m_state = MST_INIT;
  242. /* XXX need ext MIDI timer support */
  243. break;
  244. case 0xFC:
  245. mi->m_state = MST_INIT;
  246. /* XXX need ext MIDI timer support */
  247. break;
  248. case 0xFE:
  249. /* active sensing */
  250. mi->m_state = MST_INIT;
  251. break;
  252. case 0xff:
  253. mi->m_state = MST_INIT;
  254. break;
  255. default:
  256. printk (KERN_ERR "unknown MIDI sysmsg %0x\n", midic);
  257. mi->m_state = MST_INIT;
  258. }
  259. break;
  260. case MST_MTC:
  261. mi->m_state = MST_INIT;
  262. break;
  263. case MST_SYSEX:
  264. if (midic == 0xf7) {
  265. mi->m_state = MST_INIT;
  266. } else {
  267. /* XXX fix me */
  268. }
  269. break;
  270. case MST_SONGPOS:
  271. BUFTEST (mi);
  272. mi->m_buf[mi->m_ptr++] = midic;
  273. if (mi->m_ptr == 2) {
  274. mi->m_state = MST_INIT;
  275. mi->m_ptr = 0;
  276. /* XXX need ext MIDI timer support */
  277. }
  278. break;
  279. case MST_DATA:
  280. BUFTEST (mi);
  281. mi->m_buf[mi->m_ptr++] = midic;
  282. if ((--mi->m_left) <= 0) {
  283. mi->m_state = MST_INIT;
  284. do_midi_msg (synthdev, mi->m_buf, mi->m_ptr);
  285. mi->m_ptr = 0;
  286. }
  287. break;
  288. default:
  289. printk (KERN_ERR "Bad state %d ", mi->m_state);
  290. mi->m_state = MST_INIT;
  291. }
  292. return 1;
  293. }
  294. static irqreturn_t
  295. wf_mpuintr(int irq, void *dev_id, struct pt_regs *dummy)
  296. {
  297. struct wf_mpu_config *physical_dev = dev_id;
  298. static struct wf_mpu_config *input_dev;
  299. struct midi_input_info *mi = &midi_devs[physical_dev->devno]->in_info;
  300. int n;
  301. if (!input_avail()) { /* not for us */
  302. return IRQ_NONE;
  303. }
  304. if (mi->m_busy)
  305. return IRQ_HANDLED;
  306. spin_lock(&lock);
  307. mi->m_busy = 1;
  308. if (!input_dev) {
  309. input_dev = physical_dev;
  310. }
  311. n = 50; /* XXX why ? */
  312. do {
  313. unsigned char c = read_data ();
  314. if (phys_dev->isvirtual) {
  315. if (c == WF_EXTERNAL_SWITCH) {
  316. input_dev = virt_dev;
  317. continue;
  318. } else if (c == WF_INTERNAL_SWITCH) {
  319. input_dev = phys_dev;
  320. continue;
  321. } /* else just leave it as it is */
  322. } else {
  323. input_dev = phys_dev;
  324. }
  325. if (input_dev->mode == MODE_SYNTH) {
  326. wf_mpu_input_scanner (input_dev->devno,
  327. input_dev->synthno, c);
  328. } else if (input_dev->opened & OPEN_READ) {
  329. if (input_dev->inputintr) {
  330. input_dev->inputintr (input_dev->devno, c);
  331. }
  332. }
  333. } while (input_avail() && n-- > 0);
  334. mi->m_busy = 0;
  335. spin_unlock(&lock);
  336. return IRQ_HANDLED;
  337. }
  338. static int
  339. wf_mpu_open (int dev, int mode,
  340. void (*input) (int dev, unsigned char data),
  341. void (*output) (int dev)
  342. )
  343. {
  344. struct wf_mpu_config *devc;
  345. if (dev < 0 || dev >= num_midis || midi_devs[dev]==NULL)
  346. return -(ENXIO);
  347. if (phys_dev->devno == dev) {
  348. devc = phys_dev;
  349. } else if (phys_dev->isvirtual && virt_dev->devno == dev) {
  350. devc = virt_dev;
  351. } else {
  352. printk (KERN_ERR "WF-MPU: unknown device number %d\n", dev);
  353. return -(EINVAL);
  354. }
  355. if (devc->opened) {
  356. return -(EBUSY);
  357. }
  358. devc->mode = MODE_MIDI;
  359. devc->opened = mode;
  360. devc->synthno = 0;
  361. devc->inputintr = input;
  362. return 0;
  363. }
  364. static void
  365. wf_mpu_close (int dev)
  366. {
  367. struct wf_mpu_config *devc;
  368. if (dev < 0 || dev >= num_midis || midi_devs[dev]==NULL)
  369. return;
  370. if (phys_dev->devno == dev) {
  371. devc = phys_dev;
  372. } else if (phys_dev->isvirtual && virt_dev->devno == dev) {
  373. devc = virt_dev;
  374. } else {
  375. printk (KERN_ERR "WF-MPU: unknown device number %d\n", dev);
  376. return;
  377. }
  378. devc->mode = 0;
  379. devc->inputintr = NULL;
  380. devc->opened = 0;
  381. }
  382. static int
  383. wf_mpu_out (int dev, unsigned char midi_byte)
  384. {
  385. int timeout;
  386. unsigned long flags;
  387. static int lastoutdev = -1;
  388. unsigned char switchch;
  389. if (phys_dev->isvirtual && lastoutdev != dev) {
  390. if (dev == phys_dev->devno) {
  391. switchch = WF_INTERNAL_SWITCH;
  392. } else if (dev == virt_dev->devno) {
  393. switchch = WF_EXTERNAL_SWITCH;
  394. } else {
  395. printk (KERN_ERR "WF-MPU: bad device number %d", dev);
  396. return (0);
  397. }
  398. /* XXX fix me */
  399. for (timeout = 30000; timeout > 0 && !output_ready ();
  400. timeout--);
  401. spin_lock_irqsave(&lock,flags);
  402. if (!output_ready ()) {
  403. printk (KERN_WARNING "WF-MPU: Send switch "
  404. "byte timeout\n");
  405. spin_unlock_irqrestore(&lock,flags);
  406. return 0;
  407. }
  408. write_data (switchch);
  409. spin_unlock_irqrestore(&lock,flags);
  410. }
  411. lastoutdev = dev;
  412. /*
  413. * Sometimes it takes about 30000 loops before the output becomes ready
  414. * (After reset). Normally it takes just about 10 loops.
  415. */
  416. /* XXX fix me */
  417. for (timeout = 30000; timeout > 0 && !output_ready (); timeout--);
  418. spin_lock_irqsave(&lock,flags);
  419. if (!output_ready ()) {
  420. spin_unlock_irqrestore(&lock,flags);
  421. printk (KERN_WARNING "WF-MPU: Send data timeout\n");
  422. return 0;
  423. }
  424. write_data (midi_byte);
  425. spin_unlock_irqrestore(&lock,flags);
  426. return 1;
  427. }
  428. static inline int wf_mpu_start_read (int dev) {
  429. return 0;
  430. }
  431. static inline int wf_mpu_end_read (int dev) {
  432. return 0;
  433. }
  434. static int wf_mpu_ioctl (int dev, unsigned cmd, void __user *arg)
  435. {
  436. printk (KERN_WARNING
  437. "WF-MPU: Intelligent mode not supported by hardware.\n");
  438. return -(EINVAL);
  439. }
  440. static int wf_mpu_buffer_status (int dev)
  441. {
  442. return 0;
  443. }
  444. static struct synth_operations wf_mpu_synth_operations[2];
  445. static struct midi_operations wf_mpu_midi_operations[2];
  446. static struct midi_operations wf_mpu_midi_proto =
  447. {
  448. .owner = THIS_MODULE,
  449. .info = {"WF-MPU MIDI", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
  450. .in_info = {0}, /* in_info */
  451. .open = wf_mpu_open,
  452. .close = wf_mpu_close,
  453. .ioctl = wf_mpu_ioctl,
  454. .outputc = wf_mpu_out,
  455. .start_read = wf_mpu_start_read,
  456. .end_read = wf_mpu_end_read,
  457. .buffer_status = wf_mpu_buffer_status,
  458. };
  459. static struct synth_info wf_mpu_synth_info_proto =
  460. {"WaveFront MPU-401 interface", 0,
  461. SYNTH_TYPE_MIDI, MIDI_TYPE_MPU401, 0, 128, 0, 128, SYNTH_CAP_INPUT};
  462. static struct synth_info wf_mpu_synth_info[2];
  463. static int
  464. wf_mpu_synth_ioctl (int dev, unsigned int cmd, void __user *arg)
  465. {
  466. int midi_dev;
  467. int index;
  468. midi_dev = synth_devs[dev]->midi_dev;
  469. if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev]==NULL)
  470. return -(ENXIO);
  471. if (midi_dev == phys_dev->devno) {
  472. index = 0;
  473. } else if (phys_dev->isvirtual && midi_dev == virt_dev->devno) {
  474. index = 1;
  475. } else {
  476. return -(EINVAL);
  477. }
  478. switch (cmd) {
  479. case SNDCTL_SYNTH_INFO:
  480. if (copy_to_user(arg,
  481. &wf_mpu_synth_info[index],
  482. sizeof (struct synth_info)))
  483. return -EFAULT;
  484. return 0;
  485. case SNDCTL_SYNTH_MEMAVL:
  486. return 0x7fffffff;
  487. default:
  488. return -EINVAL;
  489. }
  490. }
  491. static int
  492. wf_mpu_synth_open (int dev, int mode)
  493. {
  494. int midi_dev;
  495. struct wf_mpu_config *devc;
  496. midi_dev = synth_devs[dev]->midi_dev;
  497. if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev]==NULL) {
  498. return -(ENXIO);
  499. }
  500. if (phys_dev->devno == midi_dev) {
  501. devc = phys_dev;
  502. } else if (phys_dev->isvirtual && virt_dev->devno == midi_dev) {
  503. devc = virt_dev;
  504. } else {
  505. printk (KERN_ERR "WF-MPU: unknown device number %d\n", dev);
  506. return -(EINVAL);
  507. }
  508. if (devc->opened) {
  509. return -(EBUSY);
  510. }
  511. devc->mode = MODE_SYNTH;
  512. devc->synthno = dev;
  513. devc->opened = mode;
  514. devc->inputintr = NULL;
  515. return 0;
  516. }
  517. static void
  518. wf_mpu_synth_close (int dev)
  519. {
  520. int midi_dev;
  521. struct wf_mpu_config *devc;
  522. midi_dev = synth_devs[dev]->midi_dev;
  523. if (phys_dev->devno == midi_dev) {
  524. devc = phys_dev;
  525. } else if (phys_dev->isvirtual && virt_dev->devno == midi_dev) {
  526. devc = virt_dev;
  527. } else {
  528. printk (KERN_ERR "WF-MPU: unknown device number %d\n", dev);
  529. return;
  530. }
  531. devc->inputintr = NULL;
  532. devc->opened = 0;
  533. devc->mode = 0;
  534. }
  535. #define _MIDI_SYNTH_C_
  536. #define MIDI_SYNTH_NAME "WaveFront (MIDI)"
  537. #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
  538. #include "midi_synth.h"
  539. static struct synth_operations wf_mpu_synth_proto =
  540. {
  541. .owner = THIS_MODULE,
  542. .id = "WaveFront (ICS2115)",
  543. .info = NULL, /* info field, filled in during configuration */
  544. .midi_dev = 0, /* MIDI dev XXX should this be -1 ? */
  545. .synth_type = SYNTH_TYPE_MIDI,
  546. .synth_subtype = SAMPLE_TYPE_WAVEFRONT,
  547. .open = wf_mpu_synth_open,
  548. .close = wf_mpu_synth_close,
  549. .ioctl = wf_mpu_synth_ioctl,
  550. .kill_note = midi_synth_kill_note,
  551. .start_note = midi_synth_start_note,
  552. .set_instr = midi_synth_set_instr,
  553. .reset = midi_synth_reset,
  554. .hw_control = midi_synth_hw_control,
  555. .load_patch = midi_synth_load_patch,
  556. .aftertouch = midi_synth_aftertouch,
  557. .controller = midi_synth_controller,
  558. .panning = midi_synth_panning,
  559. .bender = midi_synth_bender,
  560. .setup_voice = midi_synth_setup_voice,
  561. .send_sysex = midi_synth_send_sysex
  562. };
  563. static int
  564. config_wf_mpu (struct wf_mpu_config *dev)
  565. {
  566. int is_external;
  567. char *name;
  568. int index;
  569. if (dev == phys_dev) {
  570. name = "WaveFront internal MIDI";
  571. is_external = 0;
  572. index = 0;
  573. memcpy ((char *) &wf_mpu_synth_operations[index],
  574. (char *) &wf_mpu_synth_proto,
  575. sizeof (struct synth_operations));
  576. } else {
  577. name = "WaveFront external MIDI";
  578. is_external = 1;
  579. index = 1;
  580. /* no synth operations for an external MIDI interface */
  581. }
  582. memcpy ((char *) &wf_mpu_synth_info[dev->devno],
  583. (char *) &wf_mpu_synth_info_proto,
  584. sizeof (struct synth_info));
  585. strcpy (wf_mpu_synth_info[index].name, name);
  586. wf_mpu_synth_operations[index].midi_dev = dev->devno;
  587. wf_mpu_synth_operations[index].info = &wf_mpu_synth_info[index];
  588. memcpy ((char *) &wf_mpu_midi_operations[index],
  589. (char *) &wf_mpu_midi_proto,
  590. sizeof (struct midi_operations));
  591. if (is_external) {
  592. wf_mpu_midi_operations[index].converter = NULL;
  593. } else {
  594. wf_mpu_midi_operations[index].converter =
  595. &wf_mpu_synth_operations[index];
  596. }
  597. strcpy (wf_mpu_midi_operations[index].info.name, name);
  598. midi_devs[dev->devno] = &wf_mpu_midi_operations[index];
  599. midi_devs[dev->devno]->in_info.m_busy = 0;
  600. midi_devs[dev->devno]->in_info.m_state = MST_INIT;
  601. midi_devs[dev->devno]->in_info.m_ptr = 0;
  602. midi_devs[dev->devno]->in_info.m_left = 0;
  603. midi_devs[dev->devno]->in_info.m_prev_status = 0;
  604. devs[index].opened = 0;
  605. devs[index].mode = 0;
  606. return (0);
  607. }
  608. int virtual_midi_enable (void)
  609. {
  610. if ((virt_dev->devno < 0) &&
  611. (virt_dev->devno = sound_alloc_mididev()) == -1) {
  612. printk (KERN_ERR
  613. "WF-MPU: too many midi devices detected\n");
  614. return -1;
  615. }
  616. config_wf_mpu (virt_dev);
  617. phys_dev->isvirtual = 1;
  618. return virt_dev->devno;
  619. }
  620. int
  621. virtual_midi_disable (void)
  622. {
  623. unsigned long flags;
  624. spin_lock_irqsave(&lock,flags);
  625. wf_mpu_close (virt_dev->devno);
  626. /* no synth on virt_dev, so no need to call wf_mpu_synth_close() */
  627. phys_dev->isvirtual = 0;
  628. spin_unlock_irqrestore(&lock,flags);
  629. return 0;
  630. }
  631. int __init detect_wf_mpu (int irq, int io_base)
  632. {
  633. if (!request_region(io_base, 2, "wavefront midi")) {
  634. printk (KERN_WARNING "WF-MPU: I/O port %x already in use.\n",
  635. io_base);
  636. return -1;
  637. }
  638. phys_dev->base = io_base;
  639. phys_dev->irq = irq;
  640. phys_dev->devno = -1;
  641. virt_dev->devno = -1;
  642. return 0;
  643. }
  644. int __init install_wf_mpu (void)
  645. {
  646. if ((phys_dev->devno = sound_alloc_mididev()) < 0){
  647. printk (KERN_ERR "WF-MPU: Too many MIDI devices detected.\n");
  648. release_region(phys_dev->base, 2);
  649. return -1;
  650. }
  651. phys_dev->isvirtual = 0;
  652. if (config_wf_mpu (phys_dev)) {
  653. printk (KERN_WARNING
  654. "WF-MPU: configuration for MIDI device %d failed\n",
  655. phys_dev->devno);
  656. sound_unload_mididev (phys_dev->devno);
  657. }
  658. /* OK, now we're configured to handle an interrupt ... */
  659. if (request_irq (phys_dev->irq, wf_mpuintr, IRQF_DISABLED|IRQF_SHARED,
  660. "wavefront midi", phys_dev) < 0) {
  661. printk (KERN_ERR "WF-MPU: Failed to allocate IRQ%d\n",
  662. phys_dev->irq);
  663. return -1;
  664. }
  665. /* This being a WaveFront (ICS-2115) emulated MPU-401, we have
  666. to switch it into UART (dumb) mode, because otherwise, it
  667. won't do anything at all.
  668. */
  669. start_uart_mode ();
  670. return phys_dev->devno;
  671. }
  672. void
  673. uninstall_wf_mpu (void)
  674. {
  675. release_region (phys_dev->base, 2);
  676. free_irq (phys_dev->irq, phys_dev);
  677. sound_unload_mididev (phys_dev->devno);
  678. if (virt_dev->devno >= 0) {
  679. sound_unload_mididev (virt_dev->devno);
  680. }
  681. }
  682. static void
  683. start_uart_mode (void)
  684. {
  685. int ok, i;
  686. unsigned long flags;
  687. spin_lock_irqsave(&lock,flags);
  688. /* XXX fix me */
  689. for (i = 0; i < 30000 && !output_ready (); i++);
  690. outb (UART_MODE_ON, COMDPORT(phys_dev));
  691. for (ok = 0, i = 50000; i > 0 && !ok; i--) {
  692. if (input_avail ()) {
  693. if (read_data () == MPU_ACK) {
  694. ok = 1;
  695. }
  696. }
  697. }
  698. spin_unlock_irqrestore(&lock,flags);
  699. }
  700. #endif