mpu401.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  1. /*
  2. * sound/mpu401.c
  3. *
  4. * The low level driver for Roland MPU-401 compatible Midi cards.
  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. * Alan Cox modularisation, use normal request_irq, use dev_id
  16. * Bartlomiej Zolnierkiewicz removed some __init to allow using many drivers
  17. * Chris Rankin Update the module-usage counter for the coprocessor
  18. * Zwane Mwaikambo Changed attach/unload resource freeing
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/spinlock.h>
  24. #define USE_SEQ_MACROS
  25. #define USE_SIMPLE_MACROS
  26. #include "sound_config.h"
  27. #include "coproc.h"
  28. #include "mpu401.h"
  29. static int timer_mode = TMR_INTERNAL, timer_caps = TMR_INTERNAL;
  30. struct mpu_config
  31. {
  32. int base; /*
  33. * I/O base
  34. */
  35. int irq;
  36. int opened; /*
  37. * Open mode
  38. */
  39. int devno;
  40. int synthno;
  41. int uart_mode;
  42. int initialized;
  43. int mode;
  44. #define MODE_MIDI 1
  45. #define MODE_SYNTH 2
  46. unsigned char version, revision;
  47. unsigned int capabilities;
  48. #define MPU_CAP_INTLG 0x10000000
  49. #define MPU_CAP_SYNC 0x00000010
  50. #define MPU_CAP_FSK 0x00000020
  51. #define MPU_CAP_CLS 0x00000040
  52. #define MPU_CAP_SMPTE 0x00000080
  53. #define MPU_CAP_2PORT 0x00000001
  54. int timer_flag;
  55. #define MBUF_MAX 10
  56. #define BUFTEST(dc) if (dc->m_ptr >= MBUF_MAX || dc->m_ptr < 0) \
  57. {printk( "MPU: Invalid buffer pointer %d/%d, s=%d\n", dc->m_ptr, dc->m_left, dc->m_state);dc->m_ptr--;}
  58. int m_busy;
  59. unsigned char m_buf[MBUF_MAX];
  60. int m_ptr;
  61. int m_state;
  62. int m_left;
  63. unsigned char last_status;
  64. void (*inputintr) (int dev, unsigned char data);
  65. int shared_irq;
  66. int *osp;
  67. spinlock_t lock;
  68. };
  69. #define DATAPORT(base) (base)
  70. #define COMDPORT(base) (base+1)
  71. #define STATPORT(base) (base+1)
  72. static void mpu401_close(int dev);
  73. static inline int mpu401_status(struct mpu_config *devc)
  74. {
  75. return inb(STATPORT(devc->base));
  76. }
  77. #define input_avail(devc) (!(mpu401_status(devc)&INPUT_AVAIL))
  78. #define output_ready(devc) (!(mpu401_status(devc)&OUTPUT_READY))
  79. static inline void write_command(struct mpu_config *devc, unsigned char cmd)
  80. {
  81. outb(cmd, COMDPORT(devc->base));
  82. }
  83. static inline int read_data(struct mpu_config *devc)
  84. {
  85. return inb(DATAPORT(devc->base));
  86. }
  87. static inline void write_data(struct mpu_config *devc, unsigned char byte)
  88. {
  89. outb(byte, DATAPORT(devc->base));
  90. }
  91. #define OUTPUT_READY 0x40
  92. #define INPUT_AVAIL 0x80
  93. #define MPU_ACK 0xFE
  94. #define MPU_RESET 0xFF
  95. #define UART_MODE_ON 0x3F
  96. static struct mpu_config dev_conf[MAX_MIDI_DEV];
  97. static int n_mpu_devs;
  98. static int reset_mpu401(struct mpu_config *devc);
  99. static void set_uart_mode(int dev, struct mpu_config *devc, int arg);
  100. static int mpu_timer_init(int midi_dev);
  101. static void mpu_timer_interrupt(void);
  102. static void timer_ext_event(struct mpu_config *devc, int event, int parm);
  103. static struct synth_info mpu_synth_info_proto = {
  104. "MPU-401 MIDI interface",
  105. 0,
  106. SYNTH_TYPE_MIDI,
  107. MIDI_TYPE_MPU401,
  108. 0, 128,
  109. 0, 128,
  110. SYNTH_CAP_INPUT
  111. };
  112. static struct synth_info mpu_synth_info[MAX_MIDI_DEV];
  113. /*
  114. * States for the input scanner
  115. */
  116. #define ST_INIT 0 /* Ready for timing byte or msg */
  117. #define ST_TIMED 1 /* Leading timing byte rcvd */
  118. #define ST_DATABYTE 2 /* Waiting for (nr_left) data bytes */
  119. #define ST_SYSMSG 100 /* System message (sysx etc). */
  120. #define ST_SYSEX 101 /* System exclusive msg */
  121. #define ST_MTC 102 /* Midi Time Code (MTC) qframe msg */
  122. #define ST_SONGSEL 103 /* Song select */
  123. #define ST_SONGPOS 104 /* Song position pointer */
  124. static unsigned char len_tab[] = /* # of data bytes following a status
  125. */
  126. {
  127. 2, /* 8x */
  128. 2, /* 9x */
  129. 2, /* Ax */
  130. 2, /* Bx */
  131. 1, /* Cx */
  132. 1, /* Dx */
  133. 2, /* Ex */
  134. 0 /* Fx */
  135. };
  136. #define STORE(cmd) \
  137. { \
  138. int len; \
  139. unsigned char obuf[8]; \
  140. cmd; \
  141. seq_input_event(obuf, len); \
  142. }
  143. #define _seqbuf obuf
  144. #define _seqbufptr 0
  145. #define _SEQ_ADVBUF(x) len=x
  146. static int mpu_input_scanner(struct mpu_config *devc, unsigned char midic)
  147. {
  148. switch (devc->m_state)
  149. {
  150. case ST_INIT:
  151. switch (midic)
  152. {
  153. case 0xf8:
  154. /* Timer overflow */
  155. break;
  156. case 0xfc:
  157. printk("<all end>");
  158. break;
  159. case 0xfd:
  160. if (devc->timer_flag)
  161. mpu_timer_interrupt();
  162. break;
  163. case 0xfe:
  164. return MPU_ACK;
  165. case 0xf0:
  166. case 0xf1:
  167. case 0xf2:
  168. case 0xf3:
  169. case 0xf4:
  170. case 0xf5:
  171. case 0xf6:
  172. case 0xf7:
  173. printk("<Trk data rq #%d>", midic & 0x0f);
  174. break;
  175. case 0xf9:
  176. printk("<conductor rq>");
  177. break;
  178. case 0xff:
  179. devc->m_state = ST_SYSMSG;
  180. break;
  181. default:
  182. if (midic <= 0xef)
  183. {
  184. /* printk( "mpu time: %d ", midic); */
  185. devc->m_state = ST_TIMED;
  186. }
  187. else
  188. printk("<MPU: Unknown event %02x> ", midic);
  189. }
  190. break;
  191. case ST_TIMED:
  192. {
  193. int msg = ((int) (midic & 0xf0) >> 4);
  194. devc->m_state = ST_DATABYTE;
  195. if (msg < 8) /* Data byte */
  196. {
  197. /* printk( "midi msg (running status) "); */
  198. msg = ((int) (devc->last_status & 0xf0) >> 4);
  199. msg -= 8;
  200. devc->m_left = len_tab[msg] - 1;
  201. devc->m_ptr = 2;
  202. devc->m_buf[0] = devc->last_status;
  203. devc->m_buf[1] = midic;
  204. if (devc->m_left <= 0)
  205. {
  206. devc->m_state = ST_INIT;
  207. do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
  208. devc->m_ptr = 0;
  209. }
  210. }
  211. else if (msg == 0xf) /* MPU MARK */
  212. {
  213. devc->m_state = ST_INIT;
  214. switch (midic)
  215. {
  216. case 0xf8:
  217. /* printk( "NOP "); */
  218. break;
  219. case 0xf9:
  220. /* printk( "meas end "); */
  221. break;
  222. case 0xfc:
  223. /* printk( "data end "); */
  224. break;
  225. default:
  226. printk("Unknown MPU mark %02x\n", midic);
  227. }
  228. }
  229. else
  230. {
  231. devc->last_status = midic;
  232. /* printk( "midi msg "); */
  233. msg -= 8;
  234. devc->m_left = len_tab[msg];
  235. devc->m_ptr = 1;
  236. devc->m_buf[0] = midic;
  237. if (devc->m_left <= 0)
  238. {
  239. devc->m_state = ST_INIT;
  240. do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
  241. devc->m_ptr = 0;
  242. }
  243. }
  244. }
  245. break;
  246. case ST_SYSMSG:
  247. switch (midic)
  248. {
  249. case 0xf0:
  250. printk("<SYX>");
  251. devc->m_state = ST_SYSEX;
  252. break;
  253. case 0xf1:
  254. devc->m_state = ST_MTC;
  255. break;
  256. case 0xf2:
  257. devc->m_state = ST_SONGPOS;
  258. devc->m_ptr = 0;
  259. break;
  260. case 0xf3:
  261. devc->m_state = ST_SONGSEL;
  262. break;
  263. case 0xf6:
  264. /* printk( "tune_request\n"); */
  265. devc->m_state = ST_INIT;
  266. /*
  267. * Real time messages
  268. */
  269. case 0xf8:
  270. /* midi clock */
  271. devc->m_state = ST_INIT;
  272. timer_ext_event(devc, TMR_CLOCK, 0);
  273. break;
  274. case 0xfA:
  275. devc->m_state = ST_INIT;
  276. timer_ext_event(devc, TMR_START, 0);
  277. break;
  278. case 0xFB:
  279. devc->m_state = ST_INIT;
  280. timer_ext_event(devc, TMR_CONTINUE, 0);
  281. break;
  282. case 0xFC:
  283. devc->m_state = ST_INIT;
  284. timer_ext_event(devc, TMR_STOP, 0);
  285. break;
  286. case 0xFE:
  287. /* active sensing */
  288. devc->m_state = ST_INIT;
  289. break;
  290. case 0xff:
  291. /* printk( "midi hard reset"); */
  292. devc->m_state = ST_INIT;
  293. break;
  294. default:
  295. printk("unknown MIDI sysmsg %0x\n", midic);
  296. devc->m_state = ST_INIT;
  297. }
  298. break;
  299. case ST_MTC:
  300. devc->m_state = ST_INIT;
  301. printk("MTC frame %x02\n", midic);
  302. break;
  303. case ST_SYSEX:
  304. if (midic == 0xf7)
  305. {
  306. printk("<EOX>");
  307. devc->m_state = ST_INIT;
  308. }
  309. else
  310. printk("%02x ", midic);
  311. break;
  312. case ST_SONGPOS:
  313. BUFTEST(devc);
  314. devc->m_buf[devc->m_ptr++] = midic;
  315. if (devc->m_ptr == 2)
  316. {
  317. devc->m_state = ST_INIT;
  318. devc->m_ptr = 0;
  319. timer_ext_event(devc, TMR_SPP,
  320. ((devc->m_buf[1] & 0x7f) << 7) |
  321. (devc->m_buf[0] & 0x7f));
  322. }
  323. break;
  324. case ST_DATABYTE:
  325. BUFTEST(devc);
  326. devc->m_buf[devc->m_ptr++] = midic;
  327. if ((--devc->m_left) <= 0)
  328. {
  329. devc->m_state = ST_INIT;
  330. do_midi_msg(devc->synthno, devc->m_buf, devc->m_ptr);
  331. devc->m_ptr = 0;
  332. }
  333. break;
  334. default:
  335. printk("Bad state %d ", devc->m_state);
  336. devc->m_state = ST_INIT;
  337. }
  338. return 1;
  339. }
  340. static void mpu401_input_loop(struct mpu_config *devc)
  341. {
  342. unsigned long flags;
  343. int busy;
  344. int n;
  345. spin_lock_irqsave(&devc->lock,flags);
  346. busy = devc->m_busy;
  347. devc->m_busy = 1;
  348. spin_unlock_irqrestore(&devc->lock,flags);
  349. if (busy) /* Already inside the scanner */
  350. return;
  351. n = 50;
  352. while (input_avail(devc) && n-- > 0)
  353. {
  354. unsigned char c = read_data(devc);
  355. if (devc->mode == MODE_SYNTH)
  356. {
  357. mpu_input_scanner(devc, c);
  358. }
  359. else if (devc->opened & OPEN_READ && devc->inputintr != NULL)
  360. devc->inputintr(devc->devno, c);
  361. }
  362. devc->m_busy = 0;
  363. }
  364. int intchk_mpu401(void *dev_id)
  365. {
  366. struct mpu_config *devc;
  367. int dev = (int) dev_id;
  368. devc = &dev_conf[dev];
  369. return input_avail(devc);
  370. }
  371. irqreturn_t mpuintr(int irq, void *dev_id, struct pt_regs *dummy)
  372. {
  373. struct mpu_config *devc;
  374. int dev = (int) dev_id;
  375. int handled = 0;
  376. devc = &dev_conf[dev];
  377. if (input_avail(devc))
  378. {
  379. handled = 1;
  380. if (devc->base != 0 && (devc->opened & OPEN_READ || devc->mode == MODE_SYNTH))
  381. mpu401_input_loop(devc);
  382. else
  383. {
  384. /* Dummy read (just to acknowledge the interrupt) */
  385. read_data(devc);
  386. }
  387. }
  388. return IRQ_RETVAL(handled);
  389. }
  390. static int mpu401_open(int dev, int mode,
  391. void (*input) (int dev, unsigned char data),
  392. void (*output) (int dev)
  393. )
  394. {
  395. int err;
  396. struct mpu_config *devc;
  397. struct coproc_operations *coprocessor;
  398. if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
  399. return -ENXIO;
  400. devc = &dev_conf[dev];
  401. if (devc->opened)
  402. return -EBUSY;
  403. /*
  404. * Verify that the device is really running.
  405. * Some devices (such as Ensoniq SoundScape don't
  406. * work before the on board processor (OBP) is initialized
  407. * by downloading its microcode.
  408. */
  409. if (!devc->initialized)
  410. {
  411. if (mpu401_status(devc) == 0xff) /* Bus float */
  412. {
  413. printk(KERN_ERR "mpu401: Device not initialized properly\n");
  414. return -EIO;
  415. }
  416. reset_mpu401(devc);
  417. }
  418. if ( (coprocessor = midi_devs[dev]->coproc) != NULL )
  419. {
  420. if (!try_module_get(coprocessor->owner)) {
  421. mpu401_close(dev);
  422. return -ENODEV;
  423. }
  424. if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
  425. {
  426. printk(KERN_WARNING "MPU-401: Can't access coprocessor device\n");
  427. mpu401_close(dev);
  428. return err;
  429. }
  430. }
  431. set_uart_mode(dev, devc, 1);
  432. devc->mode = MODE_MIDI;
  433. devc->synthno = 0;
  434. mpu401_input_loop(devc);
  435. devc->inputintr = input;
  436. devc->opened = mode;
  437. return 0;
  438. }
  439. static void mpu401_close(int dev)
  440. {
  441. struct mpu_config *devc;
  442. struct coproc_operations *coprocessor;
  443. devc = &dev_conf[dev];
  444. if (devc->uart_mode)
  445. reset_mpu401(devc); /*
  446. * This disables the UART mode
  447. */
  448. devc->mode = 0;
  449. devc->inputintr = NULL;
  450. coprocessor = midi_devs[dev]->coproc;
  451. if (coprocessor) {
  452. coprocessor->close(coprocessor->devc, COPR_MIDI);
  453. module_put(coprocessor->owner);
  454. }
  455. devc->opened = 0;
  456. }
  457. static int mpu401_out(int dev, unsigned char midi_byte)
  458. {
  459. int timeout;
  460. unsigned long flags;
  461. struct mpu_config *devc;
  462. devc = &dev_conf[dev];
  463. /*
  464. * Sometimes it takes about 30000 loops before the output becomes ready
  465. * (After reset). Normally it takes just about 10 loops.
  466. */
  467. for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
  468. spin_lock_irqsave(&devc->lock,flags);
  469. if (!output_ready(devc))
  470. {
  471. printk(KERN_WARNING "mpu401: Send data timeout\n");
  472. spin_unlock_irqrestore(&devc->lock,flags);
  473. return 0;
  474. }
  475. write_data(devc, midi_byte);
  476. spin_unlock_irqrestore(&devc->lock,flags);
  477. return 1;
  478. }
  479. static int mpu401_command(int dev, mpu_command_rec * cmd)
  480. {
  481. int i, timeout, ok;
  482. int ret = 0;
  483. unsigned long flags;
  484. struct mpu_config *devc;
  485. devc = &dev_conf[dev];
  486. if (devc->uart_mode) /*
  487. * Not possible in UART mode
  488. */
  489. {
  490. printk(KERN_WARNING "mpu401: commands not possible in the UART mode\n");
  491. return -EINVAL;
  492. }
  493. /*
  494. * Test for input since pending input seems to block the output.
  495. */
  496. if (input_avail(devc))
  497. mpu401_input_loop(devc);
  498. /*
  499. * Sometimes it takes about 50000 loops before the output becomes ready
  500. * (After reset). Normally it takes just about 10 loops.
  501. */
  502. timeout = 50000;
  503. retry:
  504. if (timeout-- <= 0)
  505. {
  506. printk(KERN_WARNING "mpu401: Command (0x%x) timeout\n", (int) cmd->cmd);
  507. return -EIO;
  508. }
  509. spin_lock_irqsave(&devc->lock,flags);
  510. if (!output_ready(devc))
  511. {
  512. spin_unlock_irqrestore(&devc->lock,flags);
  513. goto retry;
  514. }
  515. write_command(devc, cmd->cmd);
  516. ok = 0;
  517. for (timeout = 50000; timeout > 0 && !ok; timeout--)
  518. {
  519. if (input_avail(devc))
  520. {
  521. if (devc->opened && devc->mode == MODE_SYNTH)
  522. {
  523. if (mpu_input_scanner(devc, read_data(devc)) == MPU_ACK)
  524. ok = 1;
  525. }
  526. else
  527. {
  528. /* Device is not currently open. Use simpler method */
  529. if (read_data(devc) == MPU_ACK)
  530. ok = 1;
  531. }
  532. }
  533. }
  534. if (!ok)
  535. {
  536. spin_unlock_irqrestore(&devc->lock,flags);
  537. return -EIO;
  538. }
  539. if (cmd->nr_args)
  540. {
  541. for (i = 0; i < cmd->nr_args; i++)
  542. {
  543. for (timeout = 3000; timeout > 0 && !output_ready(devc); timeout--);
  544. if (!mpu401_out(dev, cmd->data[i]))
  545. {
  546. spin_unlock_irqrestore(&devc->lock,flags);
  547. printk(KERN_WARNING "mpu401: Command (0x%x), parm send failed.\n", (int) cmd->cmd);
  548. return -EIO;
  549. }
  550. }
  551. }
  552. ret = 0;
  553. cmd->data[0] = 0;
  554. if (cmd->nr_returns)
  555. {
  556. for (i = 0; i < cmd->nr_returns; i++)
  557. {
  558. ok = 0;
  559. for (timeout = 5000; timeout > 0 && !ok; timeout--)
  560. if (input_avail(devc))
  561. {
  562. cmd->data[i] = read_data(devc);
  563. ok = 1;
  564. }
  565. if (!ok)
  566. {
  567. spin_unlock_irqrestore(&devc->lock,flags);
  568. return -EIO;
  569. }
  570. }
  571. }
  572. spin_unlock_irqrestore(&devc->lock,flags);
  573. return ret;
  574. }
  575. static int mpu_cmd(int dev, int cmd, int data)
  576. {
  577. int ret;
  578. static mpu_command_rec rec;
  579. rec.cmd = cmd & 0xff;
  580. rec.nr_args = ((cmd & 0xf0) == 0xE0);
  581. rec.nr_returns = ((cmd & 0xf0) == 0xA0);
  582. rec.data[0] = data & 0xff;
  583. if ((ret = mpu401_command(dev, &rec)) < 0)
  584. return ret;
  585. return (unsigned char) rec.data[0];
  586. }
  587. static int mpu401_prefix_cmd(int dev, unsigned char status)
  588. {
  589. struct mpu_config *devc = &dev_conf[dev];
  590. if (devc->uart_mode)
  591. return 1;
  592. if (status < 0xf0)
  593. {
  594. if (mpu_cmd(dev, 0xD0, 0) < 0)
  595. return 0;
  596. return 1;
  597. }
  598. switch (status)
  599. {
  600. case 0xF0:
  601. if (mpu_cmd(dev, 0xDF, 0) < 0)
  602. return 0;
  603. return 1;
  604. default:
  605. return 0;
  606. }
  607. }
  608. static int mpu401_start_read(int dev)
  609. {
  610. return 0;
  611. }
  612. static int mpu401_end_read(int dev)
  613. {
  614. return 0;
  615. }
  616. static int mpu401_ioctl(int dev, unsigned cmd, void __user *arg)
  617. {
  618. struct mpu_config *devc;
  619. mpu_command_rec rec;
  620. int val, ret;
  621. devc = &dev_conf[dev];
  622. switch (cmd)
  623. {
  624. case SNDCTL_MIDI_MPUMODE:
  625. if (!(devc->capabilities & MPU_CAP_INTLG)) { /* No intelligent mode */
  626. printk(KERN_WARNING "mpu401: Intelligent mode not supported by the HW\n");
  627. return -EINVAL;
  628. }
  629. if (get_user(val, (int __user *)arg))
  630. return -EFAULT;
  631. set_uart_mode(dev, devc, !val);
  632. return 0;
  633. case SNDCTL_MIDI_MPUCMD:
  634. if (copy_from_user(&rec, arg, sizeof(rec)))
  635. return -EFAULT;
  636. if ((ret = mpu401_command(dev, &rec)) < 0)
  637. return ret;
  638. if (copy_to_user(arg, &rec, sizeof(rec)))
  639. return -EFAULT;
  640. return 0;
  641. default:
  642. return -EINVAL;
  643. }
  644. }
  645. static void mpu401_kick(int dev)
  646. {
  647. }
  648. static int mpu401_buffer_status(int dev)
  649. {
  650. return 0; /*
  651. * No data in buffers
  652. */
  653. }
  654. static int mpu_synth_ioctl(int dev, unsigned int cmd, void __user *arg)
  655. {
  656. int midi_dev;
  657. struct mpu_config *devc;
  658. midi_dev = synth_devs[dev]->midi_dev;
  659. if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev] == NULL)
  660. return -ENXIO;
  661. devc = &dev_conf[midi_dev];
  662. switch (cmd)
  663. {
  664. case SNDCTL_SYNTH_INFO:
  665. if (copy_to_user(arg, &mpu_synth_info[midi_dev],
  666. sizeof(struct synth_info)))
  667. return -EFAULT;
  668. return 0;
  669. case SNDCTL_SYNTH_MEMAVL:
  670. return 0x7fffffff;
  671. default:
  672. return -EINVAL;
  673. }
  674. }
  675. static int mpu_synth_open(int dev, int mode)
  676. {
  677. int midi_dev, err;
  678. struct mpu_config *devc;
  679. struct coproc_operations *coprocessor;
  680. midi_dev = synth_devs[dev]->midi_dev;
  681. if (midi_dev < 0 || midi_dev > num_midis || midi_devs[midi_dev] == NULL)
  682. return -ENXIO;
  683. devc = &dev_conf[midi_dev];
  684. /*
  685. * Verify that the device is really running.
  686. * Some devices (such as Ensoniq SoundScape don't
  687. * work before the on board processor (OBP) is initialized
  688. * by downloading its microcode.
  689. */
  690. if (!devc->initialized)
  691. {
  692. if (mpu401_status(devc) == 0xff) /* Bus float */
  693. {
  694. printk(KERN_ERR "mpu401: Device not initialized properly\n");
  695. return -EIO;
  696. }
  697. reset_mpu401(devc);
  698. }
  699. if (devc->opened)
  700. return -EBUSY;
  701. devc->mode = MODE_SYNTH;
  702. devc->synthno = dev;
  703. devc->inputintr = NULL;
  704. coprocessor = midi_devs[midi_dev]->coproc;
  705. if (coprocessor) {
  706. if (!try_module_get(coprocessor->owner))
  707. return -ENODEV;
  708. if ((err = coprocessor->open(coprocessor->devc, COPR_MIDI)) < 0)
  709. {
  710. printk(KERN_WARNING "mpu401: Can't access coprocessor device\n");
  711. return err;
  712. }
  713. }
  714. devc->opened = mode;
  715. reset_mpu401(devc);
  716. if (mode & OPEN_READ)
  717. {
  718. mpu_cmd(midi_dev, 0x8B, 0); /* Enable data in stop mode */
  719. mpu_cmd(midi_dev, 0x34, 0); /* Return timing bytes in stop mode */
  720. mpu_cmd(midi_dev, 0x87, 0); /* Enable pitch & controller */
  721. }
  722. return 0;
  723. }
  724. static void mpu_synth_close(int dev)
  725. {
  726. int midi_dev;
  727. struct mpu_config *devc;
  728. struct coproc_operations *coprocessor;
  729. midi_dev = synth_devs[dev]->midi_dev;
  730. devc = &dev_conf[midi_dev];
  731. mpu_cmd(midi_dev, 0x15, 0); /* Stop recording, playback and MIDI */
  732. mpu_cmd(midi_dev, 0x8a, 0); /* Disable data in stopped mode */
  733. devc->inputintr = NULL;
  734. coprocessor = midi_devs[midi_dev]->coproc;
  735. if (coprocessor) {
  736. coprocessor->close(coprocessor->devc, COPR_MIDI);
  737. module_put(coprocessor->owner);
  738. }
  739. devc->opened = 0;
  740. devc->mode = 0;
  741. }
  742. #define MIDI_SYNTH_NAME "MPU-401 UART Midi"
  743. #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
  744. #include "midi_synth.h"
  745. static struct synth_operations mpu401_synth_proto =
  746. {
  747. .owner = THIS_MODULE,
  748. .id = "MPU401",
  749. .info = NULL,
  750. .midi_dev = 0,
  751. .synth_type = SYNTH_TYPE_MIDI,
  752. .synth_subtype = 0,
  753. .open = mpu_synth_open,
  754. .close = mpu_synth_close,
  755. .ioctl = mpu_synth_ioctl,
  756. .kill_note = midi_synth_kill_note,
  757. .start_note = midi_synth_start_note,
  758. .set_instr = midi_synth_set_instr,
  759. .reset = midi_synth_reset,
  760. .hw_control = midi_synth_hw_control,
  761. .load_patch = midi_synth_load_patch,
  762. .aftertouch = midi_synth_aftertouch,
  763. .controller = midi_synth_controller,
  764. .panning = midi_synth_panning,
  765. .bender = midi_synth_bender,
  766. .setup_voice = midi_synth_setup_voice,
  767. .send_sysex = midi_synth_send_sysex
  768. };
  769. static struct synth_operations *mpu401_synth_operations[MAX_MIDI_DEV];
  770. static struct midi_operations mpu401_midi_proto =
  771. {
  772. .owner = THIS_MODULE,
  773. .info = {"MPU-401 Midi", 0, MIDI_CAP_MPU401, SNDCARD_MPU401},
  774. .in_info = {0},
  775. .open = mpu401_open,
  776. .close = mpu401_close,
  777. .ioctl = mpu401_ioctl,
  778. .outputc = mpu401_out,
  779. .start_read = mpu401_start_read,
  780. .end_read = mpu401_end_read,
  781. .kick = mpu401_kick,
  782. .buffer_status = mpu401_buffer_status,
  783. .prefix_cmd = mpu401_prefix_cmd
  784. };
  785. static struct midi_operations mpu401_midi_operations[MAX_MIDI_DEV];
  786. static void mpu401_chk_version(int n, struct mpu_config *devc)
  787. {
  788. int tmp;
  789. unsigned long flags;
  790. devc->version = devc->revision = 0;
  791. spin_lock_irqsave(&devc->lock,flags);
  792. if ((tmp = mpu_cmd(n, 0xAC, 0)) < 0)
  793. {
  794. spin_unlock_irqrestore(&devc->lock,flags);
  795. return;
  796. }
  797. if ((tmp & 0xf0) > 0x20) /* Why it's larger than 2.x ??? */
  798. {
  799. spin_unlock_irqrestore(&devc->lock,flags);
  800. return;
  801. }
  802. devc->version = tmp;
  803. if ((tmp = mpu_cmd(n, 0xAD, 0)) < 0)
  804. {
  805. devc->version = 0;
  806. spin_unlock_irqrestore(&devc->lock,flags);
  807. return;
  808. }
  809. devc->revision = tmp;
  810. spin_unlock_irqrestore(&devc->lock,flags);
  811. }
  812. int attach_mpu401(struct address_info *hw_config, struct module *owner)
  813. {
  814. unsigned long flags;
  815. char revision_char;
  816. int m, ret;
  817. struct mpu_config *devc;
  818. hw_config->slots[1] = -1;
  819. m = sound_alloc_mididev();
  820. if (m == -1)
  821. {
  822. printk(KERN_WARNING "MPU-401: Too many midi devices detected\n");
  823. ret = -ENOMEM;
  824. goto out_err;
  825. }
  826. devc = &dev_conf[m];
  827. devc->base = hw_config->io_base;
  828. devc->osp = hw_config->osp;
  829. devc->irq = hw_config->irq;
  830. devc->opened = 0;
  831. devc->uart_mode = 0;
  832. devc->initialized = 0;
  833. devc->version = 0;
  834. devc->revision = 0;
  835. devc->capabilities = 0;
  836. devc->timer_flag = 0;
  837. devc->m_busy = 0;
  838. devc->m_state = ST_INIT;
  839. devc->shared_irq = hw_config->always_detect;
  840. devc->irq = hw_config->irq;
  841. spin_lock_init(&devc->lock);
  842. if (devc->irq < 0)
  843. {
  844. devc->irq *= -1;
  845. devc->shared_irq = 1;
  846. }
  847. if (!hw_config->always_detect)
  848. {
  849. /* Verify the hardware again */
  850. if (!reset_mpu401(devc))
  851. {
  852. printk(KERN_WARNING "mpu401: Device didn't respond\n");
  853. ret = -ENODEV;
  854. goto out_mididev;
  855. }
  856. if (!devc->shared_irq)
  857. {
  858. if (request_irq(devc->irq, mpuintr, 0, "mpu401", (void *)m) < 0)
  859. {
  860. printk(KERN_WARNING "mpu401: Failed to allocate IRQ%d\n", devc->irq);
  861. ret = -ENOMEM;
  862. goto out_mididev;
  863. }
  864. }
  865. spin_lock_irqsave(&devc->lock,flags);
  866. mpu401_chk_version(m, devc);
  867. if (devc->version == 0)
  868. mpu401_chk_version(m, devc);
  869. spin_unlock_irqrestore(&devc->lock,flags);
  870. }
  871. if (devc->version != 0)
  872. if (mpu_cmd(m, 0xC5, 0) >= 0) /* Set timebase OK */
  873. if (mpu_cmd(m, 0xE0, 120) >= 0) /* Set tempo OK */
  874. devc->capabilities |= MPU_CAP_INTLG; /* Supports intelligent mode */
  875. mpu401_synth_operations[m] = (struct synth_operations *)kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
  876. if (mpu401_synth_operations[m] == NULL)
  877. {
  878. printk(KERN_ERR "mpu401: Can't allocate memory\n");
  879. ret = -ENOMEM;
  880. goto out_irq;
  881. }
  882. if (!(devc->capabilities & MPU_CAP_INTLG)) /* No intelligent mode */
  883. {
  884. memcpy((char *) mpu401_synth_operations[m],
  885. (char *) &std_midi_synth,
  886. sizeof(struct synth_operations));
  887. }
  888. else
  889. {
  890. memcpy((char *) mpu401_synth_operations[m],
  891. (char *) &mpu401_synth_proto,
  892. sizeof(struct synth_operations));
  893. }
  894. if (owner)
  895. mpu401_synth_operations[m]->owner = owner;
  896. memcpy((char *) &mpu401_midi_operations[m],
  897. (char *) &mpu401_midi_proto,
  898. sizeof(struct midi_operations));
  899. mpu401_midi_operations[m].converter = mpu401_synth_operations[m];
  900. memcpy((char *) &mpu_synth_info[m],
  901. (char *) &mpu_synth_info_proto,
  902. sizeof(struct synth_info));
  903. n_mpu_devs++;
  904. if (devc->version == 0x20 && devc->revision >= 0x07) /* MusicQuest interface */
  905. {
  906. int ports = (devc->revision & 0x08) ? 32 : 16;
  907. devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_SMPTE |
  908. MPU_CAP_CLS | MPU_CAP_2PORT;
  909. revision_char = (devc->revision == 0x7f) ? 'M' : ' ';
  910. sprintf(mpu_synth_info[m].name, "MQX-%d%c MIDI Interface #%d",
  911. ports,
  912. revision_char,
  913. n_mpu_devs);
  914. }
  915. else
  916. {
  917. revision_char = devc->revision ? devc->revision + '@' : ' ';
  918. if ((int) devc->revision > ('Z' - '@'))
  919. revision_char = '+';
  920. devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_FSK;
  921. if (hw_config->name)
  922. sprintf(mpu_synth_info[m].name, "%s (MPU401)", hw_config->name);
  923. else
  924. sprintf(mpu_synth_info[m].name,
  925. "MPU-401 %d.%d%c Midi interface #%d",
  926. (int) (devc->version & 0xf0) >> 4,
  927. devc->version & 0x0f,
  928. revision_char,
  929. n_mpu_devs);
  930. }
  931. strcpy(mpu401_midi_operations[m].info.name,
  932. mpu_synth_info[m].name);
  933. conf_printf(mpu_synth_info[m].name, hw_config);
  934. mpu401_synth_operations[m]->midi_dev = devc->devno = m;
  935. mpu401_synth_operations[devc->devno]->info = &mpu_synth_info[devc->devno];
  936. if (devc->capabilities & MPU_CAP_INTLG) /* Intelligent mode */
  937. hw_config->slots[2] = mpu_timer_init(m);
  938. midi_devs[m] = &mpu401_midi_operations[devc->devno];
  939. if (owner)
  940. midi_devs[m]->owner = owner;
  941. hw_config->slots[1] = m;
  942. sequencer_init();
  943. return 0;
  944. out_irq:
  945. free_irq(devc->irq, (void *)m);
  946. out_mididev:
  947. sound_unload_mididev(m);
  948. out_err:
  949. release_region(hw_config->io_base, 2);
  950. return ret;
  951. }
  952. static int reset_mpu401(struct mpu_config *devc)
  953. {
  954. unsigned long flags;
  955. int ok, timeout, n;
  956. int timeout_limit;
  957. /*
  958. * Send the RESET command. Try again if no success at the first time.
  959. * (If the device is in the UART mode, it will not ack the reset cmd).
  960. */
  961. ok = 0;
  962. timeout_limit = devc->initialized ? 30000 : 100000;
  963. devc->initialized = 1;
  964. for (n = 0; n < 2 && !ok; n++)
  965. {
  966. for (timeout = timeout_limit; timeout > 0 && !ok; timeout--)
  967. ok = output_ready(devc);
  968. write_command(devc, MPU_RESET); /*
  969. * Send MPU-401 RESET Command
  970. */
  971. /*
  972. * Wait at least 25 msec. This method is not accurate so let's make the
  973. * loop bit longer. Cannot sleep since this is called during boot.
  974. */
  975. for (timeout = timeout_limit * 2; timeout > 0 && !ok; timeout--)
  976. {
  977. spin_lock_irqsave(&devc->lock,flags);
  978. if (input_avail(devc))
  979. if (read_data(devc) == MPU_ACK)
  980. ok = 1;
  981. spin_unlock_irqrestore(&devc->lock,flags);
  982. }
  983. }
  984. devc->m_state = ST_INIT;
  985. devc->m_ptr = 0;
  986. devc->m_left = 0;
  987. devc->last_status = 0;
  988. devc->uart_mode = 0;
  989. return ok;
  990. }
  991. static void set_uart_mode(int dev, struct mpu_config *devc, int arg)
  992. {
  993. if (!arg && (devc->capabilities & MPU_CAP_INTLG))
  994. return;
  995. if ((devc->uart_mode == 0) == (arg == 0))
  996. return; /* Already set */
  997. reset_mpu401(devc); /* This exits the uart mode */
  998. if (arg)
  999. {
  1000. if (mpu_cmd(dev, UART_MODE_ON, 0) < 0)
  1001. {
  1002. printk(KERN_ERR "mpu401: Can't enter UART mode\n");
  1003. devc->uart_mode = 0;
  1004. return;
  1005. }
  1006. }
  1007. devc->uart_mode = arg;
  1008. }
  1009. int probe_mpu401(struct address_info *hw_config, struct resource *ports)
  1010. {
  1011. int ok = 0;
  1012. struct mpu_config tmp_devc;
  1013. tmp_devc.base = hw_config->io_base;
  1014. tmp_devc.irq = hw_config->irq;
  1015. tmp_devc.initialized = 0;
  1016. tmp_devc.opened = 0;
  1017. tmp_devc.osp = hw_config->osp;
  1018. if (hw_config->always_detect)
  1019. return 1;
  1020. if (inb(hw_config->io_base + 1) == 0xff)
  1021. {
  1022. DDB(printk("MPU401: Port %x looks dead.\n", hw_config->io_base));
  1023. return 0; /* Just bus float? */
  1024. }
  1025. ok = reset_mpu401(&tmp_devc);
  1026. if (!ok)
  1027. {
  1028. DDB(printk("MPU401: Reset failed on port %x\n", hw_config->io_base));
  1029. }
  1030. return ok;
  1031. }
  1032. void unload_mpu401(struct address_info *hw_config)
  1033. {
  1034. void *p;
  1035. int n=hw_config->slots[1];
  1036. if (n != -1) {
  1037. release_region(hw_config->io_base, 2);
  1038. if (hw_config->always_detect == 0 && hw_config->irq > 0)
  1039. free_irq(hw_config->irq, (void *)n);
  1040. p=mpu401_synth_operations[n];
  1041. sound_unload_mididev(n);
  1042. sound_unload_timerdev(hw_config->slots[2]);
  1043. kfree(p);
  1044. }
  1045. }
  1046. /*****************************************************
  1047. * Timer stuff
  1048. ****************************************************/
  1049. static volatile int timer_initialized = 0, timer_open = 0, tmr_running = 0;
  1050. static volatile int curr_tempo, curr_timebase, hw_timebase;
  1051. static int max_timebase = 8; /* 8*24=192 ppqn */
  1052. static volatile unsigned long next_event_time;
  1053. static volatile unsigned long curr_ticks, curr_clocks;
  1054. static unsigned long prev_event_time;
  1055. static int metronome_mode;
  1056. static unsigned long clocks2ticks(unsigned long clocks)
  1057. {
  1058. /*
  1059. * The MPU-401 supports just a limited set of possible timebase values.
  1060. * Since the applications require more choices, the driver has to
  1061. * program the HW to do its best and to convert between the HW and
  1062. * actual timebases.
  1063. */
  1064. return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
  1065. }
  1066. static void set_timebase(int midi_dev, int val)
  1067. {
  1068. int hw_val;
  1069. if (val < 48)
  1070. val = 48;
  1071. if (val > 1000)
  1072. val = 1000;
  1073. hw_val = val;
  1074. hw_val = (hw_val + 12) / 24;
  1075. if (hw_val > max_timebase)
  1076. hw_val = max_timebase;
  1077. if (mpu_cmd(midi_dev, 0xC0 | (hw_val & 0x0f), 0) < 0)
  1078. {
  1079. printk(KERN_WARNING "mpu401: Can't set HW timebase to %d\n", hw_val * 24);
  1080. return;
  1081. }
  1082. hw_timebase = hw_val * 24;
  1083. curr_timebase = val;
  1084. }
  1085. static void tmr_reset(struct mpu_config *devc)
  1086. {
  1087. unsigned long flags;
  1088. spin_lock_irqsave(&devc->lock,flags);
  1089. next_event_time = (unsigned long) -1;
  1090. prev_event_time = 0;
  1091. curr_ticks = curr_clocks = 0;
  1092. spin_unlock_irqrestore(&devc->lock,flags);
  1093. }
  1094. static void set_timer_mode(int midi_dev)
  1095. {
  1096. if (timer_mode & TMR_MODE_CLS)
  1097. mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
  1098. else if (timer_mode & TMR_MODE_SMPTE)
  1099. mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
  1100. if (timer_mode & TMR_INTERNAL)
  1101. {
  1102. mpu_cmd(midi_dev, 0x80, 0); /* Use MIDI sync */
  1103. }
  1104. else
  1105. {
  1106. if (timer_mode & (TMR_MODE_MIDI | TMR_MODE_CLS))
  1107. {
  1108. mpu_cmd(midi_dev, 0x82, 0); /* Use MIDI sync */
  1109. mpu_cmd(midi_dev, 0x91, 0); /* Enable ext MIDI ctrl */
  1110. }
  1111. else if (timer_mode & TMR_MODE_FSK)
  1112. mpu_cmd(midi_dev, 0x81, 0); /* Use FSK sync */
  1113. }
  1114. }
  1115. static void stop_metronome(int midi_dev)
  1116. {
  1117. mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
  1118. }
  1119. static void setup_metronome(int midi_dev)
  1120. {
  1121. int numerator, denominator;
  1122. int clks_per_click, num_32nds_per_beat;
  1123. int beats_per_measure;
  1124. numerator = ((unsigned) metronome_mode >> 24) & 0xff;
  1125. denominator = ((unsigned) metronome_mode >> 16) & 0xff;
  1126. clks_per_click = ((unsigned) metronome_mode >> 8) & 0xff;
  1127. num_32nds_per_beat = (unsigned) metronome_mode & 0xff;
  1128. beats_per_measure = (numerator * 4) >> denominator;
  1129. if (!metronome_mode)
  1130. mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
  1131. else
  1132. {
  1133. mpu_cmd(midi_dev, 0xE4, clks_per_click);
  1134. mpu_cmd(midi_dev, 0xE6, beats_per_measure);
  1135. mpu_cmd(midi_dev, 0x83, 0); /* Enable metronome without accents */
  1136. }
  1137. }
  1138. static int mpu_start_timer(int midi_dev)
  1139. {
  1140. struct mpu_config *devc= &dev_conf[midi_dev];
  1141. tmr_reset(devc);
  1142. set_timer_mode(midi_dev);
  1143. if (tmr_running)
  1144. return TIMER_NOT_ARMED; /* Already running */
  1145. if (timer_mode & TMR_INTERNAL)
  1146. {
  1147. mpu_cmd(midi_dev, 0x02, 0); /* Send MIDI start */
  1148. tmr_running = 1;
  1149. return TIMER_NOT_ARMED;
  1150. }
  1151. else
  1152. {
  1153. mpu_cmd(midi_dev, 0x35, 0); /* Enable mode messages to PC */
  1154. mpu_cmd(midi_dev, 0x38, 0); /* Enable sys common messages to PC */
  1155. mpu_cmd(midi_dev, 0x39, 0); /* Enable real time messages to PC */
  1156. mpu_cmd(midi_dev, 0x97, 0); /* Enable system exclusive messages to PC */
  1157. }
  1158. return TIMER_ARMED;
  1159. }
  1160. static int mpu_timer_open(int dev, int mode)
  1161. {
  1162. int midi_dev = sound_timer_devs[dev]->devlink;
  1163. struct mpu_config *devc= &dev_conf[midi_dev];
  1164. if (timer_open)
  1165. return -EBUSY;
  1166. tmr_reset(devc);
  1167. curr_tempo = 50;
  1168. mpu_cmd(midi_dev, 0xE0, 50);
  1169. curr_timebase = hw_timebase = 120;
  1170. set_timebase(midi_dev, 120);
  1171. timer_open = 1;
  1172. metronome_mode = 0;
  1173. set_timer_mode(midi_dev);
  1174. mpu_cmd(midi_dev, 0xe7, 0x04); /* Send all clocks to host */
  1175. mpu_cmd(midi_dev, 0x95, 0); /* Enable clock to host */
  1176. return 0;
  1177. }
  1178. static void mpu_timer_close(int dev)
  1179. {
  1180. int midi_dev = sound_timer_devs[dev]->devlink;
  1181. timer_open = tmr_running = 0;
  1182. mpu_cmd(midi_dev, 0x15, 0); /* Stop all */
  1183. mpu_cmd(midi_dev, 0x94, 0); /* Disable clock to host */
  1184. mpu_cmd(midi_dev, 0x8c, 0); /* Disable measure end messages to host */
  1185. stop_metronome(midi_dev);
  1186. }
  1187. static int mpu_timer_event(int dev, unsigned char *event)
  1188. {
  1189. unsigned char command = event[1];
  1190. unsigned long parm = *(unsigned int *) &event[4];
  1191. int midi_dev = sound_timer_devs[dev]->devlink;
  1192. switch (command)
  1193. {
  1194. case TMR_WAIT_REL:
  1195. parm += prev_event_time;
  1196. case TMR_WAIT_ABS:
  1197. if (parm > 0)
  1198. {
  1199. long time;
  1200. if (parm <= curr_ticks) /* It's the time */
  1201. return TIMER_NOT_ARMED;
  1202. time = parm;
  1203. next_event_time = prev_event_time = time;
  1204. return TIMER_ARMED;
  1205. }
  1206. break;
  1207. case TMR_START:
  1208. if (tmr_running)
  1209. break;
  1210. return mpu_start_timer(midi_dev);
  1211. case TMR_STOP:
  1212. mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
  1213. stop_metronome(midi_dev);
  1214. tmr_running = 0;
  1215. break;
  1216. case TMR_CONTINUE:
  1217. if (tmr_running)
  1218. break;
  1219. mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
  1220. setup_metronome(midi_dev);
  1221. tmr_running = 1;
  1222. break;
  1223. case TMR_TEMPO:
  1224. if (parm)
  1225. {
  1226. if (parm < 8)
  1227. parm = 8;
  1228. if (parm > 250)
  1229. parm = 250;
  1230. if (mpu_cmd(midi_dev, 0xE0, parm) < 0)
  1231. printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) parm);
  1232. curr_tempo = parm;
  1233. }
  1234. break;
  1235. case TMR_ECHO:
  1236. seq_copy_to_input(event, 8);
  1237. break;
  1238. case TMR_TIMESIG:
  1239. if (metronome_mode) /* Metronome enabled */
  1240. {
  1241. metronome_mode = parm;
  1242. setup_metronome(midi_dev);
  1243. }
  1244. break;
  1245. default:;
  1246. }
  1247. return TIMER_NOT_ARMED;
  1248. }
  1249. static unsigned long mpu_timer_get_time(int dev)
  1250. {
  1251. if (!timer_open)
  1252. return 0;
  1253. return curr_ticks;
  1254. }
  1255. static int mpu_timer_ioctl(int dev, unsigned int command, void __user *arg)
  1256. {
  1257. int midi_dev = sound_timer_devs[dev]->devlink;
  1258. int __user *p = (int __user *)arg;
  1259. switch (command)
  1260. {
  1261. case SNDCTL_TMR_SOURCE:
  1262. {
  1263. int parm;
  1264. if (get_user(parm, p))
  1265. return -EFAULT;
  1266. parm &= timer_caps;
  1267. if (parm != 0)
  1268. {
  1269. timer_mode = parm;
  1270. if (timer_mode & TMR_MODE_CLS)
  1271. mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
  1272. else if (timer_mode & TMR_MODE_SMPTE)
  1273. mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
  1274. }
  1275. if (put_user(timer_mode, p))
  1276. return -EFAULT;
  1277. return timer_mode;
  1278. }
  1279. break;
  1280. case SNDCTL_TMR_START:
  1281. mpu_start_timer(midi_dev);
  1282. return 0;
  1283. case SNDCTL_TMR_STOP:
  1284. tmr_running = 0;
  1285. mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
  1286. stop_metronome(midi_dev);
  1287. return 0;
  1288. case SNDCTL_TMR_CONTINUE:
  1289. if (tmr_running)
  1290. return 0;
  1291. tmr_running = 1;
  1292. mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
  1293. return 0;
  1294. case SNDCTL_TMR_TIMEBASE:
  1295. {
  1296. int val;
  1297. if (get_user(val, p))
  1298. return -EFAULT;
  1299. if (val)
  1300. set_timebase(midi_dev, val);
  1301. if (put_user(curr_timebase, p))
  1302. return -EFAULT;
  1303. return curr_timebase;
  1304. }
  1305. break;
  1306. case SNDCTL_TMR_TEMPO:
  1307. {
  1308. int val;
  1309. int ret;
  1310. if (get_user(val, p))
  1311. return -EFAULT;
  1312. if (val)
  1313. {
  1314. if (val < 8)
  1315. val = 8;
  1316. if (val > 250)
  1317. val = 250;
  1318. if ((ret = mpu_cmd(midi_dev, 0xE0, val)) < 0)
  1319. {
  1320. printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) val);
  1321. return ret;
  1322. }
  1323. curr_tempo = val;
  1324. }
  1325. if (put_user(curr_tempo, p))
  1326. return -EFAULT;
  1327. return curr_tempo;
  1328. }
  1329. break;
  1330. case SNDCTL_SEQ_CTRLRATE:
  1331. {
  1332. int val;
  1333. if (get_user(val, p))
  1334. return -EFAULT;
  1335. if (val != 0) /* Can't change */
  1336. return -EINVAL;
  1337. val = ((curr_tempo * curr_timebase) + 30)/60;
  1338. if (put_user(val, p))
  1339. return -EFAULT;
  1340. return val;
  1341. }
  1342. break;
  1343. case SNDCTL_SEQ_GETTIME:
  1344. if (put_user(curr_ticks, p))
  1345. return -EFAULT;
  1346. return curr_ticks;
  1347. case SNDCTL_TMR_METRONOME:
  1348. if (get_user(metronome_mode, p))
  1349. return -EFAULT;
  1350. setup_metronome(midi_dev);
  1351. return 0;
  1352. default:;
  1353. }
  1354. return -EINVAL;
  1355. }
  1356. static void mpu_timer_arm(int dev, long time)
  1357. {
  1358. if (time < 0)
  1359. time = curr_ticks + 1;
  1360. else if (time <= curr_ticks) /* It's the time */
  1361. return;
  1362. next_event_time = prev_event_time = time;
  1363. return;
  1364. }
  1365. static struct sound_timer_operations mpu_timer =
  1366. {
  1367. .owner = THIS_MODULE,
  1368. .info = {"MPU-401 Timer", 0},
  1369. .priority = 10, /* Priority */
  1370. .devlink = 0, /* Local device link */
  1371. .open = mpu_timer_open,
  1372. .close = mpu_timer_close,
  1373. .event = mpu_timer_event,
  1374. .get_time = mpu_timer_get_time,
  1375. .ioctl = mpu_timer_ioctl,
  1376. .arm_timer = mpu_timer_arm
  1377. };
  1378. static void mpu_timer_interrupt(void)
  1379. {
  1380. if (!timer_open)
  1381. return;
  1382. if (!tmr_running)
  1383. return;
  1384. curr_clocks++;
  1385. curr_ticks = clocks2ticks(curr_clocks);
  1386. if (curr_ticks >= next_event_time)
  1387. {
  1388. next_event_time = (unsigned long) -1;
  1389. sequencer_timer(0);
  1390. }
  1391. }
  1392. static void timer_ext_event(struct mpu_config *devc, int event, int parm)
  1393. {
  1394. int midi_dev = devc->devno;
  1395. if (!devc->timer_flag)
  1396. return;
  1397. switch (event)
  1398. {
  1399. case TMR_CLOCK:
  1400. printk("<MIDI clk>");
  1401. break;
  1402. case TMR_START:
  1403. printk("Ext MIDI start\n");
  1404. if (!tmr_running)
  1405. {
  1406. if (timer_mode & TMR_EXTERNAL)
  1407. {
  1408. tmr_running = 1;
  1409. setup_metronome(midi_dev);
  1410. next_event_time = 0;
  1411. STORE(SEQ_START_TIMER());
  1412. }
  1413. }
  1414. break;
  1415. case TMR_STOP:
  1416. printk("Ext MIDI stop\n");
  1417. if (timer_mode & TMR_EXTERNAL)
  1418. {
  1419. tmr_running = 0;
  1420. stop_metronome(midi_dev);
  1421. STORE(SEQ_STOP_TIMER());
  1422. }
  1423. break;
  1424. case TMR_CONTINUE:
  1425. printk("Ext MIDI continue\n");
  1426. if (timer_mode & TMR_EXTERNAL)
  1427. {
  1428. tmr_running = 1;
  1429. setup_metronome(midi_dev);
  1430. STORE(SEQ_CONTINUE_TIMER());
  1431. }
  1432. break;
  1433. case TMR_SPP:
  1434. printk("Songpos: %d\n", parm);
  1435. if (timer_mode & TMR_EXTERNAL)
  1436. {
  1437. STORE(SEQ_SONGPOS(parm));
  1438. }
  1439. break;
  1440. }
  1441. }
  1442. static int mpu_timer_init(int midi_dev)
  1443. {
  1444. struct mpu_config *devc;
  1445. int n;
  1446. devc = &dev_conf[midi_dev];
  1447. if (timer_initialized)
  1448. return -1; /* There is already a similar timer */
  1449. timer_initialized = 1;
  1450. mpu_timer.devlink = midi_dev;
  1451. dev_conf[midi_dev].timer_flag = 1;
  1452. n = sound_alloc_timerdev();
  1453. if (n == -1)
  1454. n = 0;
  1455. sound_timer_devs[n] = &mpu_timer;
  1456. if (devc->version < 0x20) /* Original MPU-401 */
  1457. timer_caps = TMR_INTERNAL | TMR_EXTERNAL | TMR_MODE_FSK | TMR_MODE_MIDI;
  1458. else
  1459. {
  1460. /*
  1461. * The version number 2.0 is used (at least) by the
  1462. * MusicQuest cards and the Roland Super-MPU.
  1463. *
  1464. * MusicQuest has given a special meaning to the bits of the
  1465. * revision number. The Super-MPU returns 0.
  1466. */
  1467. if (devc->revision)
  1468. timer_caps |= TMR_EXTERNAL | TMR_MODE_MIDI;
  1469. if (devc->revision & 0x02)
  1470. timer_caps |= TMR_MODE_CLS;
  1471. if (devc->revision & 0x40)
  1472. max_timebase = 10; /* Has the 216 and 240 ppqn modes */
  1473. }
  1474. timer_mode = (TMR_INTERNAL | TMR_MODE_MIDI) & timer_caps;
  1475. return n;
  1476. }
  1477. EXPORT_SYMBOL(probe_mpu401);
  1478. EXPORT_SYMBOL(attach_mpu401);
  1479. EXPORT_SYMBOL(unload_mpu401);
  1480. EXPORT_SYMBOL(intchk_mpu401);
  1481. EXPORT_SYMBOL(mpuintr);
  1482. static struct address_info cfg;
  1483. static int io = -1;
  1484. static int irq = -1;
  1485. module_param(irq, int, 0);
  1486. module_param(io, int, 0);
  1487. static int __init init_mpu401(void)
  1488. {
  1489. int ret;
  1490. /* Can be loaded either for module use or to provide functions
  1491. to others */
  1492. if (io != -1 && irq != -1) {
  1493. struct resource *ports;
  1494. cfg.irq = irq;
  1495. cfg.io_base = io;
  1496. ports = request_region(io, 2, "mpu401");
  1497. if (!ports)
  1498. return -EBUSY;
  1499. if (probe_mpu401(&cfg, ports) == 0) {
  1500. release_region(io, 2);
  1501. return -ENODEV;
  1502. }
  1503. if ((ret = attach_mpu401(&cfg, THIS_MODULE)))
  1504. return ret;
  1505. }
  1506. return 0;
  1507. }
  1508. static void __exit cleanup_mpu401(void)
  1509. {
  1510. if (io != -1 && irq != -1) {
  1511. /* Check for use by, for example, sscape driver */
  1512. unload_mpu401(&cfg);
  1513. }
  1514. }
  1515. module_init(init_mpu401);
  1516. module_exit(cleanup_mpu401);
  1517. #ifndef MODULE
  1518. static int __init setup_mpu401(char *str)
  1519. {
  1520. /* io, irq */
  1521. int ints[3];
  1522. str = get_options(str, ARRAY_SIZE(ints), ints);
  1523. io = ints[1];
  1524. irq = ints[2];
  1525. return 1;
  1526. }
  1527. __setup("mpu401=", setup_mpu401);
  1528. #endif
  1529. MODULE_LICENSE("GPL");