mpu401.c 38 KB

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