mpu401.c 38 KB

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