mpu401.c 38 KB

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