mpu401_uart.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  3. * Routines for control of MPU-401 in UART mode
  4. *
  5. * MPU-401 supports UART mode which is not capable generate transmit
  6. * interrupts thus output is done via polling. Also, if irq < 0, then
  7. * input is done also via polling. Do not expect good performance.
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * 13-03-2003:
  25. * Added support for different kind of hardware I/O. Build in choices
  26. * are port and mmio. For other kind of I/O, set mpu->read and
  27. * mpu->write to your own I/O functions.
  28. *
  29. */
  30. #include <sound/driver.h>
  31. #include <asm/io.h>
  32. #include <linux/delay.h>
  33. #include <linux/init.h>
  34. #include <linux/slab.h>
  35. #include <linux/ioport.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/errno.h>
  38. #include <sound/core.h>
  39. #include <sound/mpu401.h>
  40. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  41. MODULE_DESCRIPTION("Routines for control of MPU-401 in UART mode");
  42. MODULE_LICENSE("GPL");
  43. static void snd_mpu401_uart_input_read(mpu401_t * mpu);
  44. static void snd_mpu401_uart_output_write(mpu401_t * mpu);
  45. /*
  46. */
  47. #define snd_mpu401_input_avail(mpu) (!(mpu->read(mpu, MPU401C(mpu)) & 0x80))
  48. #define snd_mpu401_output_ready(mpu) (!(mpu->read(mpu, MPU401C(mpu)) & 0x40))
  49. #define MPU401_RESET 0xff
  50. #define MPU401_ENTER_UART 0x3f
  51. #define MPU401_ACK 0xfe
  52. /* Build in lowlevel io */
  53. static void mpu401_write_port(mpu401_t *mpu, unsigned char data, unsigned long addr)
  54. {
  55. outb(data, addr);
  56. }
  57. static unsigned char mpu401_read_port(mpu401_t *mpu, unsigned long addr)
  58. {
  59. return inb(addr);
  60. }
  61. static void mpu401_write_mmio(mpu401_t *mpu, unsigned char data, unsigned long addr)
  62. {
  63. writeb(data, (void __iomem *)addr);
  64. }
  65. static unsigned char mpu401_read_mmio(mpu401_t *mpu, unsigned long addr)
  66. {
  67. return readb((void __iomem *)addr);
  68. }
  69. /* */
  70. static void snd_mpu401_uart_clear_rx(mpu401_t *mpu)
  71. {
  72. int timeout = 100000;
  73. for (; timeout > 0 && snd_mpu401_input_avail(mpu); timeout--)
  74. mpu->read(mpu, MPU401D(mpu));
  75. #ifdef CONFIG_SND_DEBUG
  76. if (timeout <= 0)
  77. snd_printk("cmd: clear rx timeout (status = 0x%x)\n", mpu->read(mpu, MPU401C(mpu)));
  78. #endif
  79. }
  80. static void _snd_mpu401_uart_interrupt(mpu401_t *mpu)
  81. {
  82. spin_lock(&mpu->input_lock);
  83. if (test_bit(MPU401_MODE_BIT_INPUT, &mpu->mode)) {
  84. snd_mpu401_uart_input_read(mpu);
  85. } else {
  86. snd_mpu401_uart_clear_rx(mpu);
  87. }
  88. spin_unlock(&mpu->input_lock);
  89. /* ok. for better Tx performance try do some output when input is done */
  90. if (test_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode) &&
  91. test_bit(MPU401_MODE_BIT_OUTPUT_TRIGGER, &mpu->mode)) {
  92. spin_lock(&mpu->output_lock);
  93. snd_mpu401_uart_output_write(mpu);
  94. spin_unlock(&mpu->output_lock);
  95. }
  96. }
  97. /**
  98. * snd_mpu401_uart_interrupt - generic MPU401-UART interrupt handler
  99. * @irq: the irq number
  100. * @dev_id: mpu401 instance
  101. * @regs: the reigster
  102. *
  103. * Processes the interrupt for MPU401-UART i/o.
  104. */
  105. irqreturn_t snd_mpu401_uart_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  106. {
  107. mpu401_t *mpu = dev_id;
  108. if (mpu == NULL)
  109. return IRQ_NONE;
  110. _snd_mpu401_uart_interrupt(mpu);
  111. return IRQ_HANDLED;
  112. }
  113. /*
  114. * timer callback
  115. * reprogram the timer and call the interrupt job
  116. */
  117. static void snd_mpu401_uart_timer(unsigned long data)
  118. {
  119. mpu401_t *mpu = (mpu401_t *)data;
  120. spin_lock(&mpu->timer_lock);
  121. /*mpu->mode |= MPU401_MODE_TIMER;*/
  122. mpu->timer.expires = 1 + jiffies;
  123. add_timer(&mpu->timer);
  124. spin_unlock(&mpu->timer_lock);
  125. if (mpu->rmidi)
  126. _snd_mpu401_uart_interrupt(mpu);
  127. }
  128. /*
  129. * initialize the timer callback if not programmed yet
  130. */
  131. static void snd_mpu401_uart_add_timer (mpu401_t *mpu, int input)
  132. {
  133. unsigned long flags;
  134. spin_lock_irqsave (&mpu->timer_lock, flags);
  135. if (mpu->timer_invoked == 0) {
  136. init_timer(&mpu->timer);
  137. mpu->timer.data = (unsigned long)mpu;
  138. mpu->timer.function = snd_mpu401_uart_timer;
  139. mpu->timer.expires = 1 + jiffies;
  140. add_timer(&mpu->timer);
  141. }
  142. mpu->timer_invoked |= input ? MPU401_MODE_INPUT_TIMER : MPU401_MODE_OUTPUT_TIMER;
  143. spin_unlock_irqrestore (&mpu->timer_lock, flags);
  144. }
  145. /*
  146. * remove the timer callback if still active
  147. */
  148. static void snd_mpu401_uart_remove_timer (mpu401_t *mpu, int input)
  149. {
  150. unsigned long flags;
  151. spin_lock_irqsave (&mpu->timer_lock, flags);
  152. if (mpu->timer_invoked) {
  153. mpu->timer_invoked &= input ? ~MPU401_MODE_INPUT_TIMER : ~MPU401_MODE_OUTPUT_TIMER;
  154. if (! mpu->timer_invoked)
  155. del_timer(&mpu->timer);
  156. }
  157. spin_unlock_irqrestore (&mpu->timer_lock, flags);
  158. }
  159. /*
  160. */
  161. static void snd_mpu401_uart_cmd(mpu401_t * mpu, unsigned char cmd, int ack)
  162. {
  163. unsigned long flags;
  164. int timeout, ok;
  165. spin_lock_irqsave(&mpu->input_lock, flags);
  166. if (mpu->hardware != MPU401_HW_TRID4DWAVE) {
  167. mpu->write(mpu, 0x00, MPU401D(mpu));
  168. /*snd_mpu401_uart_clear_rx(mpu);*/
  169. }
  170. /* ok. standard MPU-401 initialization */
  171. if (mpu->hardware != MPU401_HW_SB) {
  172. for (timeout = 1000; timeout > 0 && !snd_mpu401_output_ready(mpu); timeout--)
  173. udelay(10);
  174. #ifdef CONFIG_SND_DEBUG
  175. if (!timeout)
  176. snd_printk("cmd: tx timeout (status = 0x%x)\n", mpu->read(mpu, MPU401C(mpu)));
  177. #endif
  178. }
  179. mpu->write(mpu, cmd, MPU401C(mpu));
  180. if (ack) {
  181. ok = 0;
  182. timeout = 10000;
  183. while (!ok && timeout-- > 0) {
  184. if (snd_mpu401_input_avail(mpu)) {
  185. if (mpu->read(mpu, MPU401D(mpu)) == MPU401_ACK)
  186. ok = 1;
  187. }
  188. }
  189. if (!ok && mpu->read(mpu, MPU401D(mpu)) == MPU401_ACK)
  190. ok = 1;
  191. } else {
  192. ok = 1;
  193. }
  194. spin_unlock_irqrestore(&mpu->input_lock, flags);
  195. if (! ok)
  196. snd_printk("cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)\n", cmd, mpu->port, mpu->read(mpu, MPU401C(mpu)), mpu->read(mpu, MPU401D(mpu)));
  197. // snd_printk("cmd: 0x%x at 0x%lx (status = 0x%x, data = 0x%x)\n", cmd, mpu->port, mpu->read(mpu, MPU401C(mpu)), mpu->read(mpu, MPU401D(mpu)));
  198. }
  199. /*
  200. * input/output open/close - protected by open_mutex in rawmidi.c
  201. */
  202. static int snd_mpu401_uart_input_open(snd_rawmidi_substream_t * substream)
  203. {
  204. mpu401_t *mpu;
  205. int err;
  206. mpu = substream->rmidi->private_data;
  207. if (mpu->open_input && (err = mpu->open_input(mpu)) < 0)
  208. return err;
  209. if (! test_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode)) {
  210. snd_mpu401_uart_cmd(mpu, MPU401_RESET, 1);
  211. snd_mpu401_uart_cmd(mpu, MPU401_ENTER_UART, 1);
  212. }
  213. mpu->substream_input = substream;
  214. set_bit(MPU401_MODE_BIT_INPUT, &mpu->mode);
  215. return 0;
  216. }
  217. static int snd_mpu401_uart_output_open(snd_rawmidi_substream_t * substream)
  218. {
  219. mpu401_t *mpu;
  220. int err;
  221. mpu = substream->rmidi->private_data;
  222. if (mpu->open_output && (err = mpu->open_output(mpu)) < 0)
  223. return err;
  224. if (! test_bit(MPU401_MODE_BIT_INPUT, &mpu->mode)) {
  225. snd_mpu401_uart_cmd(mpu, MPU401_RESET, 1);
  226. snd_mpu401_uart_cmd(mpu, MPU401_ENTER_UART, 1);
  227. }
  228. mpu->substream_output = substream;
  229. set_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode);
  230. return 0;
  231. }
  232. static int snd_mpu401_uart_input_close(snd_rawmidi_substream_t * substream)
  233. {
  234. mpu401_t *mpu;
  235. mpu = substream->rmidi->private_data;
  236. clear_bit(MPU401_MODE_BIT_INPUT, &mpu->mode);
  237. mpu->substream_input = NULL;
  238. if (! test_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode))
  239. snd_mpu401_uart_cmd(mpu, MPU401_RESET, 0);
  240. if (mpu->close_input)
  241. mpu->close_input(mpu);
  242. return 0;
  243. }
  244. static int snd_mpu401_uart_output_close(snd_rawmidi_substream_t * substream)
  245. {
  246. mpu401_t *mpu;
  247. mpu = substream->rmidi->private_data;
  248. clear_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode);
  249. mpu->substream_output = NULL;
  250. if (! test_bit(MPU401_MODE_BIT_INPUT, &mpu->mode))
  251. snd_mpu401_uart_cmd(mpu, MPU401_RESET, 0);
  252. if (mpu->close_output)
  253. mpu->close_output(mpu);
  254. return 0;
  255. }
  256. /*
  257. * trigger input callback
  258. */
  259. static void snd_mpu401_uart_input_trigger(snd_rawmidi_substream_t * substream, int up)
  260. {
  261. unsigned long flags;
  262. mpu401_t *mpu;
  263. int max = 64;
  264. mpu = substream->rmidi->private_data;
  265. if (up) {
  266. if (! test_and_set_bit(MPU401_MODE_BIT_INPUT_TRIGGER, &mpu->mode)) {
  267. /* first time - flush FIFO */
  268. while (max-- > 0)
  269. mpu->read(mpu, MPU401D(mpu));
  270. if (mpu->irq < 0)
  271. snd_mpu401_uart_add_timer(mpu, 1);
  272. }
  273. /* read data in advance */
  274. spin_lock_irqsave(&mpu->input_lock, flags);
  275. snd_mpu401_uart_input_read(mpu);
  276. spin_unlock_irqrestore(&mpu->input_lock, flags);
  277. } else {
  278. if (mpu->irq < 0)
  279. snd_mpu401_uart_remove_timer(mpu, 1);
  280. clear_bit(MPU401_MODE_BIT_INPUT_TRIGGER, &mpu->mode);
  281. }
  282. }
  283. /*
  284. * transfer input pending data
  285. * call with input_lock spinlock held
  286. */
  287. static void snd_mpu401_uart_input_read(mpu401_t * mpu)
  288. {
  289. int max = 128;
  290. unsigned char byte;
  291. while (max-- > 0) {
  292. if (snd_mpu401_input_avail(mpu)) {
  293. byte = mpu->read(mpu, MPU401D(mpu));
  294. if (test_bit(MPU401_MODE_BIT_INPUT_TRIGGER, &mpu->mode))
  295. snd_rawmidi_receive(mpu->substream_input, &byte, 1);
  296. } else {
  297. break; /* input not available */
  298. }
  299. }
  300. }
  301. /*
  302. * Tx FIFO sizes:
  303. * CS4237B - 16 bytes
  304. * AudioDrive ES1688 - 12 bytes
  305. * S3 SonicVibes - 8 bytes
  306. * SoundBlaster AWE 64 - 2 bytes (ugly hardware)
  307. */
  308. /*
  309. * write output pending bytes
  310. * call with output_lock spinlock held
  311. */
  312. static void snd_mpu401_uart_output_write(mpu401_t * mpu)
  313. {
  314. unsigned char byte;
  315. int max = 256, timeout;
  316. do {
  317. if (snd_rawmidi_transmit_peek(mpu->substream_output, &byte, 1) == 1) {
  318. for (timeout = 100; timeout > 0; timeout--) {
  319. if (snd_mpu401_output_ready(mpu)) {
  320. mpu->write(mpu, byte, MPU401D(mpu));
  321. snd_rawmidi_transmit_ack(mpu->substream_output, 1);
  322. break;
  323. }
  324. }
  325. if (timeout == 0)
  326. break; /* Tx FIFO full - try again later */
  327. } else {
  328. snd_mpu401_uart_remove_timer (mpu, 0);
  329. break; /* no other data - leave the tx loop */
  330. }
  331. } while (--max > 0);
  332. }
  333. /*
  334. * output trigger callback
  335. */
  336. static void snd_mpu401_uart_output_trigger(snd_rawmidi_substream_t * substream, int up)
  337. {
  338. unsigned long flags;
  339. mpu401_t *mpu;
  340. mpu = substream->rmidi->private_data;
  341. if (up) {
  342. set_bit(MPU401_MODE_BIT_OUTPUT_TRIGGER, &mpu->mode);
  343. /* try to add the timer at each output trigger,
  344. * since the output timer might have been removed in
  345. * snd_mpu401_uart_output_write().
  346. */
  347. snd_mpu401_uart_add_timer(mpu, 0);
  348. /* output pending data */
  349. spin_lock_irqsave(&mpu->output_lock, flags);
  350. snd_mpu401_uart_output_write(mpu);
  351. spin_unlock_irqrestore(&mpu->output_lock, flags);
  352. } else {
  353. snd_mpu401_uart_remove_timer(mpu, 0);
  354. clear_bit(MPU401_MODE_BIT_OUTPUT_TRIGGER, &mpu->mode);
  355. }
  356. }
  357. /*
  358. */
  359. static snd_rawmidi_ops_t snd_mpu401_uart_output =
  360. {
  361. .open = snd_mpu401_uart_output_open,
  362. .close = snd_mpu401_uart_output_close,
  363. .trigger = snd_mpu401_uart_output_trigger,
  364. };
  365. static snd_rawmidi_ops_t snd_mpu401_uart_input =
  366. {
  367. .open = snd_mpu401_uart_input_open,
  368. .close = snd_mpu401_uart_input_close,
  369. .trigger = snd_mpu401_uart_input_trigger,
  370. };
  371. static void snd_mpu401_uart_free(snd_rawmidi_t *rmidi)
  372. {
  373. mpu401_t *mpu = rmidi->private_data;
  374. if (mpu->irq_flags && mpu->irq >= 0)
  375. free_irq(mpu->irq, (void *) mpu);
  376. if (mpu->res) {
  377. release_resource(mpu->res);
  378. kfree_nocheck(mpu->res);
  379. }
  380. kfree(mpu);
  381. }
  382. /**
  383. * snd_mpu401_uart_new - create an MPU401-UART instance
  384. * @card: the card instance
  385. * @device: the device index, zero-based
  386. * @hardware: the hardware type, MPU401_HW_XXXX
  387. * @port: the base address of MPU401 port
  388. * @integrated: non-zero if the port was already reserved by the chip
  389. * @irq: the irq number, -1 if no interrupt for mpu
  390. * @irq_flags: the irq request flags (SA_XXX), 0 if irq was already reserved.
  391. * @rrawmidi: the pointer to store the new rawmidi instance
  392. *
  393. * Creates a new MPU-401 instance.
  394. *
  395. * Note that the rawmidi instance is returned on the rrawmidi argument,
  396. * not the mpu401 instance itself. To access to the mpu401 instance,
  397. * cast from rawmidi->private_data (with mpu401_t magic-cast).
  398. *
  399. * Returns zero if successful, or a negative error code.
  400. */
  401. int snd_mpu401_uart_new(snd_card_t * card, int device,
  402. unsigned short hardware,
  403. unsigned long port, int integrated,
  404. int irq, int irq_flags,
  405. snd_rawmidi_t ** rrawmidi)
  406. {
  407. mpu401_t *mpu;
  408. snd_rawmidi_t *rmidi;
  409. int err;
  410. if (rrawmidi)
  411. *rrawmidi = NULL;
  412. if ((err = snd_rawmidi_new(card, "MPU-401U", device, 1, 1, &rmidi)) < 0)
  413. return err;
  414. mpu = kcalloc(1, sizeof(*mpu), GFP_KERNEL);
  415. if (mpu == NULL) {
  416. snd_device_free(card, rmidi);
  417. return -ENOMEM;
  418. }
  419. rmidi->private_data = mpu;
  420. rmidi->private_free = snd_mpu401_uart_free;
  421. spin_lock_init(&mpu->input_lock);
  422. spin_lock_init(&mpu->output_lock);
  423. spin_lock_init(&mpu->timer_lock);
  424. mpu->hardware = hardware;
  425. if (!integrated) {
  426. int res_size = hardware == MPU401_HW_PC98II ? 4 : 2;
  427. if ((mpu->res = request_region(port, res_size, "MPU401 UART")) == NULL) {
  428. snd_printk(KERN_ERR "mpu401_uart: unable to grab port 0x%lx size %d\n", port, res_size);
  429. snd_device_free(card, rmidi);
  430. return -EBUSY;
  431. }
  432. }
  433. switch (hardware) {
  434. case MPU401_HW_AUREAL:
  435. mpu->write = mpu401_write_mmio;
  436. mpu->read = mpu401_read_mmio;
  437. break;
  438. default:
  439. mpu->write = mpu401_write_port;
  440. mpu->read = mpu401_read_port;
  441. break;
  442. }
  443. mpu->port = port;
  444. if (hardware == MPU401_HW_PC98II)
  445. mpu->cport = port + 2;
  446. else
  447. mpu->cport = port + 1;
  448. if (irq >= 0 && irq_flags) {
  449. if (request_irq(irq, snd_mpu401_uart_interrupt, irq_flags, "MPU401 UART", (void *) mpu)) {
  450. snd_printk(KERN_ERR "mpu401_uart: unable to grab IRQ %d\n", irq);
  451. snd_device_free(card, rmidi);
  452. return -EBUSY;
  453. }
  454. }
  455. mpu->irq = irq;
  456. mpu->irq_flags = irq_flags;
  457. if (card->shortname[0])
  458. snprintf(rmidi->name, sizeof(rmidi->name), "%s MIDI", card->shortname);
  459. else
  460. sprintf(rmidi->name, "MPU-401 MIDI %d-%d", card->number, device);
  461. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_mpu401_uart_output);
  462. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_mpu401_uart_input);
  463. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
  464. SNDRV_RAWMIDI_INFO_INPUT |
  465. SNDRV_RAWMIDI_INFO_DUPLEX;
  466. mpu->rmidi = rmidi;
  467. if (rrawmidi)
  468. *rrawmidi = rmidi;
  469. return 0;
  470. }
  471. EXPORT_SYMBOL(snd_mpu401_uart_interrupt);
  472. EXPORT_SYMBOL(snd_mpu401_uart_new);
  473. /*
  474. * INIT part
  475. */
  476. static int __init alsa_mpu401_uart_init(void)
  477. {
  478. return 0;
  479. }
  480. static void __exit alsa_mpu401_uart_exit(void)
  481. {
  482. }
  483. module_init(alsa_mpu401_uart_init)
  484. module_exit(alsa_mpu401_uart_exit)