mpu401.c 38 KB

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