mpu401.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  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", (void *)m) < 0)
  852. {
  853. printk(KERN_WARNING "mpu401: Failed to allocate IRQ%d\n", devc->irq);
  854. ret = -ENOMEM;
  855. goto out_mididev;
  856. }
  857. }
  858. spin_lock_irqsave(&devc->lock,flags);
  859. mpu401_chk_version(m, devc);
  860. if (devc->version == 0)
  861. mpu401_chk_version(m, devc);
  862. spin_unlock_irqrestore(&devc->lock,flags);
  863. }
  864. if (devc->version != 0)
  865. if (mpu_cmd(m, 0xC5, 0) >= 0) /* Set timebase OK */
  866. if (mpu_cmd(m, 0xE0, 120) >= 0) /* Set tempo OK */
  867. devc->capabilities |= MPU_CAP_INTLG; /* Supports intelligent mode */
  868. mpu401_synth_operations[m] = kmalloc(sizeof(struct synth_operations), GFP_KERNEL);
  869. if (mpu401_synth_operations[m] == NULL)
  870. {
  871. printk(KERN_ERR "mpu401: Can't allocate memory\n");
  872. ret = -ENOMEM;
  873. goto out_irq;
  874. }
  875. if (!(devc->capabilities & MPU_CAP_INTLG)) /* No intelligent mode */
  876. {
  877. memcpy((char *) mpu401_synth_operations[m],
  878. (char *) &std_midi_synth,
  879. sizeof(struct synth_operations));
  880. }
  881. else
  882. {
  883. memcpy((char *) mpu401_synth_operations[m],
  884. (char *) &mpu401_synth_proto,
  885. sizeof(struct synth_operations));
  886. }
  887. if (owner)
  888. mpu401_synth_operations[m]->owner = owner;
  889. memcpy((char *) &mpu401_midi_operations[m],
  890. (char *) &mpu401_midi_proto,
  891. sizeof(struct midi_operations));
  892. mpu401_midi_operations[m].converter = mpu401_synth_operations[m];
  893. memcpy((char *) &mpu_synth_info[m],
  894. (char *) &mpu_synth_info_proto,
  895. sizeof(struct synth_info));
  896. n_mpu_devs++;
  897. if (devc->version == 0x20 && devc->revision >= 0x07) /* MusicQuest interface */
  898. {
  899. int ports = (devc->revision & 0x08) ? 32 : 16;
  900. devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_SMPTE |
  901. MPU_CAP_CLS | MPU_CAP_2PORT;
  902. revision_char = (devc->revision == 0x7f) ? 'M' : ' ';
  903. sprintf(mpu_synth_info[m].name, "MQX-%d%c MIDI Interface #%d",
  904. ports,
  905. revision_char,
  906. n_mpu_devs);
  907. }
  908. else
  909. {
  910. revision_char = devc->revision ? devc->revision + '@' : ' ';
  911. if ((int) devc->revision > ('Z' - '@'))
  912. revision_char = '+';
  913. devc->capabilities |= MPU_CAP_SYNC | MPU_CAP_FSK;
  914. if (hw_config->name)
  915. sprintf(mpu_synth_info[m].name, "%s (MPU401)", hw_config->name);
  916. else
  917. sprintf(mpu_synth_info[m].name,
  918. "MPU-401 %d.%d%c Midi interface #%d",
  919. (int) (devc->version & 0xf0) >> 4,
  920. devc->version & 0x0f,
  921. revision_char,
  922. n_mpu_devs);
  923. }
  924. strcpy(mpu401_midi_operations[m].info.name,
  925. mpu_synth_info[m].name);
  926. conf_printf(mpu_synth_info[m].name, hw_config);
  927. mpu401_synth_operations[m]->midi_dev = devc->devno = m;
  928. mpu401_synth_operations[devc->devno]->info = &mpu_synth_info[devc->devno];
  929. if (devc->capabilities & MPU_CAP_INTLG) /* Intelligent mode */
  930. hw_config->slots[2] = mpu_timer_init(m);
  931. midi_devs[m] = &mpu401_midi_operations[devc->devno];
  932. if (owner)
  933. midi_devs[m]->owner = owner;
  934. hw_config->slots[1] = m;
  935. sequencer_init();
  936. return 0;
  937. out_irq:
  938. free_irq(devc->irq, (void *)m);
  939. out_mididev:
  940. sound_unload_mididev(m);
  941. out_err:
  942. release_region(hw_config->io_base, 2);
  943. return ret;
  944. }
  945. static int reset_mpu401(struct mpu_config *devc)
  946. {
  947. unsigned long flags;
  948. int ok, timeout, n;
  949. int timeout_limit;
  950. /*
  951. * Send the RESET command. Try again if no success at the first time.
  952. * (If the device is in the UART mode, it will not ack the reset cmd).
  953. */
  954. ok = 0;
  955. timeout_limit = devc->initialized ? 30000 : 100000;
  956. devc->initialized = 1;
  957. for (n = 0; n < 2 && !ok; n++)
  958. {
  959. for (timeout = timeout_limit; timeout > 0 && !ok; timeout--)
  960. ok = output_ready(devc);
  961. write_command(devc, MPU_RESET); /*
  962. * Send MPU-401 RESET Command
  963. */
  964. /*
  965. * Wait at least 25 msec. This method is not accurate so let's make the
  966. * loop bit longer. Cannot sleep since this is called during boot.
  967. */
  968. for (timeout = timeout_limit * 2; timeout > 0 && !ok; timeout--)
  969. {
  970. spin_lock_irqsave(&devc->lock,flags);
  971. if (input_avail(devc))
  972. if (read_data(devc) == MPU_ACK)
  973. ok = 1;
  974. spin_unlock_irqrestore(&devc->lock,flags);
  975. }
  976. }
  977. devc->m_state = ST_INIT;
  978. devc->m_ptr = 0;
  979. devc->m_left = 0;
  980. devc->last_status = 0;
  981. devc->uart_mode = 0;
  982. return ok;
  983. }
  984. static void set_uart_mode(int dev, struct mpu_config *devc, int arg)
  985. {
  986. if (!arg && (devc->capabilities & MPU_CAP_INTLG))
  987. return;
  988. if ((devc->uart_mode == 0) == (arg == 0))
  989. return; /* Already set */
  990. reset_mpu401(devc); /* This exits the uart mode */
  991. if (arg)
  992. {
  993. if (mpu_cmd(dev, UART_MODE_ON, 0) < 0)
  994. {
  995. printk(KERN_ERR "mpu401: Can't enter UART mode\n");
  996. devc->uart_mode = 0;
  997. return;
  998. }
  999. }
  1000. devc->uart_mode = arg;
  1001. }
  1002. int probe_mpu401(struct address_info *hw_config, struct resource *ports)
  1003. {
  1004. int ok = 0;
  1005. struct mpu_config tmp_devc;
  1006. tmp_devc.base = hw_config->io_base;
  1007. tmp_devc.irq = hw_config->irq;
  1008. tmp_devc.initialized = 0;
  1009. tmp_devc.opened = 0;
  1010. tmp_devc.osp = hw_config->osp;
  1011. if (hw_config->always_detect)
  1012. return 1;
  1013. if (inb(hw_config->io_base + 1) == 0xff)
  1014. {
  1015. DDB(printk("MPU401: Port %x looks dead.\n", hw_config->io_base));
  1016. return 0; /* Just bus float? */
  1017. }
  1018. ok = reset_mpu401(&tmp_devc);
  1019. if (!ok)
  1020. {
  1021. DDB(printk("MPU401: Reset failed on port %x\n", hw_config->io_base));
  1022. }
  1023. return ok;
  1024. }
  1025. void unload_mpu401(struct address_info *hw_config)
  1026. {
  1027. void *p;
  1028. int n=hw_config->slots[1];
  1029. if (n != -1) {
  1030. release_region(hw_config->io_base, 2);
  1031. if (hw_config->always_detect == 0 && hw_config->irq > 0)
  1032. free_irq(hw_config->irq, (void *)n);
  1033. p=mpu401_synth_operations[n];
  1034. sound_unload_mididev(n);
  1035. sound_unload_timerdev(hw_config->slots[2]);
  1036. kfree(p);
  1037. }
  1038. }
  1039. /*****************************************************
  1040. * Timer stuff
  1041. ****************************************************/
  1042. static volatile int timer_initialized = 0, timer_open = 0, tmr_running = 0;
  1043. static volatile int curr_tempo, curr_timebase, hw_timebase;
  1044. static int max_timebase = 8; /* 8*24=192 ppqn */
  1045. static volatile unsigned long next_event_time;
  1046. static volatile unsigned long curr_ticks, curr_clocks;
  1047. static unsigned long prev_event_time;
  1048. static int metronome_mode;
  1049. static unsigned long clocks2ticks(unsigned long clocks)
  1050. {
  1051. /*
  1052. * The MPU-401 supports just a limited set of possible timebase values.
  1053. * Since the applications require more choices, the driver has to
  1054. * program the HW to do its best and to convert between the HW and
  1055. * actual timebases.
  1056. */
  1057. return ((clocks * curr_timebase) + (hw_timebase / 2)) / hw_timebase;
  1058. }
  1059. static void set_timebase(int midi_dev, int val)
  1060. {
  1061. int hw_val;
  1062. if (val < 48)
  1063. val = 48;
  1064. if (val > 1000)
  1065. val = 1000;
  1066. hw_val = val;
  1067. hw_val = (hw_val + 12) / 24;
  1068. if (hw_val > max_timebase)
  1069. hw_val = max_timebase;
  1070. if (mpu_cmd(midi_dev, 0xC0 | (hw_val & 0x0f), 0) < 0)
  1071. {
  1072. printk(KERN_WARNING "mpu401: Can't set HW timebase to %d\n", hw_val * 24);
  1073. return;
  1074. }
  1075. hw_timebase = hw_val * 24;
  1076. curr_timebase = val;
  1077. }
  1078. static void tmr_reset(struct mpu_config *devc)
  1079. {
  1080. unsigned long flags;
  1081. spin_lock_irqsave(&devc->lock,flags);
  1082. next_event_time = (unsigned long) -1;
  1083. prev_event_time = 0;
  1084. curr_ticks = curr_clocks = 0;
  1085. spin_unlock_irqrestore(&devc->lock,flags);
  1086. }
  1087. static void set_timer_mode(int midi_dev)
  1088. {
  1089. if (timer_mode & TMR_MODE_CLS)
  1090. mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
  1091. else if (timer_mode & TMR_MODE_SMPTE)
  1092. mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
  1093. if (timer_mode & TMR_INTERNAL)
  1094. {
  1095. mpu_cmd(midi_dev, 0x80, 0); /* Use MIDI sync */
  1096. }
  1097. else
  1098. {
  1099. if (timer_mode & (TMR_MODE_MIDI | TMR_MODE_CLS))
  1100. {
  1101. mpu_cmd(midi_dev, 0x82, 0); /* Use MIDI sync */
  1102. mpu_cmd(midi_dev, 0x91, 0); /* Enable ext MIDI ctrl */
  1103. }
  1104. else if (timer_mode & TMR_MODE_FSK)
  1105. mpu_cmd(midi_dev, 0x81, 0); /* Use FSK sync */
  1106. }
  1107. }
  1108. static void stop_metronome(int midi_dev)
  1109. {
  1110. mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
  1111. }
  1112. static void setup_metronome(int midi_dev)
  1113. {
  1114. int numerator, denominator;
  1115. int clks_per_click, num_32nds_per_beat;
  1116. int beats_per_measure;
  1117. numerator = ((unsigned) metronome_mode >> 24) & 0xff;
  1118. denominator = ((unsigned) metronome_mode >> 16) & 0xff;
  1119. clks_per_click = ((unsigned) metronome_mode >> 8) & 0xff;
  1120. num_32nds_per_beat = (unsigned) metronome_mode & 0xff;
  1121. beats_per_measure = (numerator * 4) >> denominator;
  1122. if (!metronome_mode)
  1123. mpu_cmd(midi_dev, 0x84, 0); /* Disable metronome */
  1124. else
  1125. {
  1126. mpu_cmd(midi_dev, 0xE4, clks_per_click);
  1127. mpu_cmd(midi_dev, 0xE6, beats_per_measure);
  1128. mpu_cmd(midi_dev, 0x83, 0); /* Enable metronome without accents */
  1129. }
  1130. }
  1131. static int mpu_start_timer(int midi_dev)
  1132. {
  1133. struct mpu_config *devc= &dev_conf[midi_dev];
  1134. tmr_reset(devc);
  1135. set_timer_mode(midi_dev);
  1136. if (tmr_running)
  1137. return TIMER_NOT_ARMED; /* Already running */
  1138. if (timer_mode & TMR_INTERNAL)
  1139. {
  1140. mpu_cmd(midi_dev, 0x02, 0); /* Send MIDI start */
  1141. tmr_running = 1;
  1142. return TIMER_NOT_ARMED;
  1143. }
  1144. else
  1145. {
  1146. mpu_cmd(midi_dev, 0x35, 0); /* Enable mode messages to PC */
  1147. mpu_cmd(midi_dev, 0x38, 0); /* Enable sys common messages to PC */
  1148. mpu_cmd(midi_dev, 0x39, 0); /* Enable real time messages to PC */
  1149. mpu_cmd(midi_dev, 0x97, 0); /* Enable system exclusive messages to PC */
  1150. }
  1151. return TIMER_ARMED;
  1152. }
  1153. static int mpu_timer_open(int dev, int mode)
  1154. {
  1155. int midi_dev = sound_timer_devs[dev]->devlink;
  1156. struct mpu_config *devc= &dev_conf[midi_dev];
  1157. if (timer_open)
  1158. return -EBUSY;
  1159. tmr_reset(devc);
  1160. curr_tempo = 50;
  1161. mpu_cmd(midi_dev, 0xE0, 50);
  1162. curr_timebase = hw_timebase = 120;
  1163. set_timebase(midi_dev, 120);
  1164. timer_open = 1;
  1165. metronome_mode = 0;
  1166. set_timer_mode(midi_dev);
  1167. mpu_cmd(midi_dev, 0xe7, 0x04); /* Send all clocks to host */
  1168. mpu_cmd(midi_dev, 0x95, 0); /* Enable clock to host */
  1169. return 0;
  1170. }
  1171. static void mpu_timer_close(int dev)
  1172. {
  1173. int midi_dev = sound_timer_devs[dev]->devlink;
  1174. timer_open = tmr_running = 0;
  1175. mpu_cmd(midi_dev, 0x15, 0); /* Stop all */
  1176. mpu_cmd(midi_dev, 0x94, 0); /* Disable clock to host */
  1177. mpu_cmd(midi_dev, 0x8c, 0); /* Disable measure end messages to host */
  1178. stop_metronome(midi_dev);
  1179. }
  1180. static int mpu_timer_event(int dev, unsigned char *event)
  1181. {
  1182. unsigned char command = event[1];
  1183. unsigned long parm = *(unsigned int *) &event[4];
  1184. int midi_dev = sound_timer_devs[dev]->devlink;
  1185. switch (command)
  1186. {
  1187. case TMR_WAIT_REL:
  1188. parm += prev_event_time;
  1189. case TMR_WAIT_ABS:
  1190. if (parm > 0)
  1191. {
  1192. long time;
  1193. if (parm <= curr_ticks) /* It's the time */
  1194. return TIMER_NOT_ARMED;
  1195. time = parm;
  1196. next_event_time = prev_event_time = time;
  1197. return TIMER_ARMED;
  1198. }
  1199. break;
  1200. case TMR_START:
  1201. if (tmr_running)
  1202. break;
  1203. return mpu_start_timer(midi_dev);
  1204. case TMR_STOP:
  1205. mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
  1206. stop_metronome(midi_dev);
  1207. tmr_running = 0;
  1208. break;
  1209. case TMR_CONTINUE:
  1210. if (tmr_running)
  1211. break;
  1212. mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
  1213. setup_metronome(midi_dev);
  1214. tmr_running = 1;
  1215. break;
  1216. case TMR_TEMPO:
  1217. if (parm)
  1218. {
  1219. if (parm < 8)
  1220. parm = 8;
  1221. if (parm > 250)
  1222. parm = 250;
  1223. if (mpu_cmd(midi_dev, 0xE0, parm) < 0)
  1224. printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) parm);
  1225. curr_tempo = parm;
  1226. }
  1227. break;
  1228. case TMR_ECHO:
  1229. seq_copy_to_input(event, 8);
  1230. break;
  1231. case TMR_TIMESIG:
  1232. if (metronome_mode) /* Metronome enabled */
  1233. {
  1234. metronome_mode = parm;
  1235. setup_metronome(midi_dev);
  1236. }
  1237. break;
  1238. default:;
  1239. }
  1240. return TIMER_NOT_ARMED;
  1241. }
  1242. static unsigned long mpu_timer_get_time(int dev)
  1243. {
  1244. if (!timer_open)
  1245. return 0;
  1246. return curr_ticks;
  1247. }
  1248. static int mpu_timer_ioctl(int dev, unsigned int command, void __user *arg)
  1249. {
  1250. int midi_dev = sound_timer_devs[dev]->devlink;
  1251. int __user *p = (int __user *)arg;
  1252. switch (command)
  1253. {
  1254. case SNDCTL_TMR_SOURCE:
  1255. {
  1256. int parm;
  1257. if (get_user(parm, p))
  1258. return -EFAULT;
  1259. parm &= timer_caps;
  1260. if (parm != 0)
  1261. {
  1262. timer_mode = parm;
  1263. if (timer_mode & TMR_MODE_CLS)
  1264. mpu_cmd(midi_dev, 0x3c, 0); /* Use CLS sync */
  1265. else if (timer_mode & TMR_MODE_SMPTE)
  1266. mpu_cmd(midi_dev, 0x3d, 0); /* Use SMPTE sync */
  1267. }
  1268. if (put_user(timer_mode, p))
  1269. return -EFAULT;
  1270. return timer_mode;
  1271. }
  1272. break;
  1273. case SNDCTL_TMR_START:
  1274. mpu_start_timer(midi_dev);
  1275. return 0;
  1276. case SNDCTL_TMR_STOP:
  1277. tmr_running = 0;
  1278. mpu_cmd(midi_dev, 0x01, 0); /* Send MIDI stop */
  1279. stop_metronome(midi_dev);
  1280. return 0;
  1281. case SNDCTL_TMR_CONTINUE:
  1282. if (tmr_running)
  1283. return 0;
  1284. tmr_running = 1;
  1285. mpu_cmd(midi_dev, 0x03, 0); /* Send MIDI continue */
  1286. return 0;
  1287. case SNDCTL_TMR_TIMEBASE:
  1288. {
  1289. int val;
  1290. if (get_user(val, p))
  1291. return -EFAULT;
  1292. if (val)
  1293. set_timebase(midi_dev, val);
  1294. if (put_user(curr_timebase, p))
  1295. return -EFAULT;
  1296. return curr_timebase;
  1297. }
  1298. break;
  1299. case SNDCTL_TMR_TEMPO:
  1300. {
  1301. int val;
  1302. int ret;
  1303. if (get_user(val, p))
  1304. return -EFAULT;
  1305. if (val)
  1306. {
  1307. if (val < 8)
  1308. val = 8;
  1309. if (val > 250)
  1310. val = 250;
  1311. if ((ret = mpu_cmd(midi_dev, 0xE0, val)) < 0)
  1312. {
  1313. printk(KERN_WARNING "mpu401: Can't set tempo to %d\n", (int) val);
  1314. return ret;
  1315. }
  1316. curr_tempo = val;
  1317. }
  1318. if (put_user(curr_tempo, p))
  1319. return -EFAULT;
  1320. return curr_tempo;
  1321. }
  1322. break;
  1323. case SNDCTL_SEQ_CTRLRATE:
  1324. {
  1325. int val;
  1326. if (get_user(val, p))
  1327. return -EFAULT;
  1328. if (val != 0) /* Can't change */
  1329. return -EINVAL;
  1330. val = ((curr_tempo * curr_timebase) + 30)/60;
  1331. if (put_user(val, p))
  1332. return -EFAULT;
  1333. return val;
  1334. }
  1335. break;
  1336. case SNDCTL_SEQ_GETTIME:
  1337. if (put_user(curr_ticks, p))
  1338. return -EFAULT;
  1339. return curr_ticks;
  1340. case SNDCTL_TMR_METRONOME:
  1341. if (get_user(metronome_mode, p))
  1342. return -EFAULT;
  1343. setup_metronome(midi_dev);
  1344. return 0;
  1345. default:;
  1346. }
  1347. return -EINVAL;
  1348. }
  1349. static void mpu_timer_arm(int dev, long time)
  1350. {
  1351. if (time < 0)
  1352. time = curr_ticks + 1;
  1353. else if (time <= curr_ticks) /* It's the time */
  1354. return;
  1355. next_event_time = prev_event_time = time;
  1356. return;
  1357. }
  1358. static struct sound_timer_operations mpu_timer =
  1359. {
  1360. .owner = THIS_MODULE,
  1361. .info = {"MPU-401 Timer", 0},
  1362. .priority = 10, /* Priority */
  1363. .devlink = 0, /* Local device link */
  1364. .open = mpu_timer_open,
  1365. .close = mpu_timer_close,
  1366. .event = mpu_timer_event,
  1367. .get_time = mpu_timer_get_time,
  1368. .ioctl = mpu_timer_ioctl,
  1369. .arm_timer = mpu_timer_arm
  1370. };
  1371. static void mpu_timer_interrupt(void)
  1372. {
  1373. if (!timer_open)
  1374. return;
  1375. if (!tmr_running)
  1376. return;
  1377. curr_clocks++;
  1378. curr_ticks = clocks2ticks(curr_clocks);
  1379. if (curr_ticks >= next_event_time)
  1380. {
  1381. next_event_time = (unsigned long) -1;
  1382. sequencer_timer(0);
  1383. }
  1384. }
  1385. static void timer_ext_event(struct mpu_config *devc, int event, int parm)
  1386. {
  1387. int midi_dev = devc->devno;
  1388. if (!devc->timer_flag)
  1389. return;
  1390. switch (event)
  1391. {
  1392. case TMR_CLOCK:
  1393. printk("<MIDI clk>");
  1394. break;
  1395. case TMR_START:
  1396. printk("Ext MIDI start\n");
  1397. if (!tmr_running)
  1398. {
  1399. if (timer_mode & TMR_EXTERNAL)
  1400. {
  1401. tmr_running = 1;
  1402. setup_metronome(midi_dev);
  1403. next_event_time = 0;
  1404. STORE(SEQ_START_TIMER());
  1405. }
  1406. }
  1407. break;
  1408. case TMR_STOP:
  1409. printk("Ext MIDI stop\n");
  1410. if (timer_mode & TMR_EXTERNAL)
  1411. {
  1412. tmr_running = 0;
  1413. stop_metronome(midi_dev);
  1414. STORE(SEQ_STOP_TIMER());
  1415. }
  1416. break;
  1417. case TMR_CONTINUE:
  1418. printk("Ext MIDI continue\n");
  1419. if (timer_mode & TMR_EXTERNAL)
  1420. {
  1421. tmr_running = 1;
  1422. setup_metronome(midi_dev);
  1423. STORE(SEQ_CONTINUE_TIMER());
  1424. }
  1425. break;
  1426. case TMR_SPP:
  1427. printk("Songpos: %d\n", parm);
  1428. if (timer_mode & TMR_EXTERNAL)
  1429. {
  1430. STORE(SEQ_SONGPOS(parm));
  1431. }
  1432. break;
  1433. }
  1434. }
  1435. static int mpu_timer_init(int midi_dev)
  1436. {
  1437. struct mpu_config *devc;
  1438. int n;
  1439. devc = &dev_conf[midi_dev];
  1440. if (timer_initialized)
  1441. return -1; /* There is already a similar timer */
  1442. timer_initialized = 1;
  1443. mpu_timer.devlink = midi_dev;
  1444. dev_conf[midi_dev].timer_flag = 1;
  1445. n = sound_alloc_timerdev();
  1446. if (n == -1)
  1447. n = 0;
  1448. sound_timer_devs[n] = &mpu_timer;
  1449. if (devc->version < 0x20) /* Original MPU-401 */
  1450. timer_caps = TMR_INTERNAL | TMR_EXTERNAL | TMR_MODE_FSK | TMR_MODE_MIDI;
  1451. else
  1452. {
  1453. /*
  1454. * The version number 2.0 is used (at least) by the
  1455. * MusicQuest cards and the Roland Super-MPU.
  1456. *
  1457. * MusicQuest has given a special meaning to the bits of the
  1458. * revision number. The Super-MPU returns 0.
  1459. */
  1460. if (devc->revision)
  1461. timer_caps |= TMR_EXTERNAL | TMR_MODE_MIDI;
  1462. if (devc->revision & 0x02)
  1463. timer_caps |= TMR_MODE_CLS;
  1464. if (devc->revision & 0x40)
  1465. max_timebase = 10; /* Has the 216 and 240 ppqn modes */
  1466. }
  1467. timer_mode = (TMR_INTERNAL | TMR_MODE_MIDI) & timer_caps;
  1468. return n;
  1469. }
  1470. EXPORT_SYMBOL(probe_mpu401);
  1471. EXPORT_SYMBOL(attach_mpu401);
  1472. EXPORT_SYMBOL(unload_mpu401);
  1473. static struct address_info cfg;
  1474. static int io = -1;
  1475. static int irq = -1;
  1476. module_param(irq, int, 0);
  1477. module_param(io, int, 0);
  1478. static int __init init_mpu401(void)
  1479. {
  1480. int ret;
  1481. /* Can be loaded either for module use or to provide functions
  1482. to others */
  1483. if (io != -1 && irq != -1) {
  1484. struct resource *ports;
  1485. cfg.irq = irq;
  1486. cfg.io_base = io;
  1487. ports = request_region(io, 2, "mpu401");
  1488. if (!ports)
  1489. return -EBUSY;
  1490. if (probe_mpu401(&cfg, ports) == 0) {
  1491. release_region(io, 2);
  1492. return -ENODEV;
  1493. }
  1494. if ((ret = attach_mpu401(&cfg, THIS_MODULE)))
  1495. return ret;
  1496. }
  1497. return 0;
  1498. }
  1499. static void __exit cleanup_mpu401(void)
  1500. {
  1501. if (io != -1 && irq != -1) {
  1502. /* Check for use by, for example, sscape driver */
  1503. unload_mpu401(&cfg);
  1504. }
  1505. }
  1506. module_init(init_mpu401);
  1507. module_exit(cleanup_mpu401);
  1508. #ifndef MODULE
  1509. static int __init setup_mpu401(char *str)
  1510. {
  1511. /* io, irq */
  1512. int ints[3];
  1513. str = get_options(str, ARRAY_SIZE(ints), ints);
  1514. io = ints[1];
  1515. irq = ints[2];
  1516. return 1;
  1517. }
  1518. __setup("mpu401=", setup_mpu401);
  1519. #endif
  1520. MODULE_LICENSE("GPL");