mpu401_uart.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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(struct snd_mpu401 * mpu);
  44. static void snd_mpu401_uart_output_write(struct snd_mpu401 * 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(struct snd_mpu401 *mpu, unsigned char data, unsigned long addr)
  54. {
  55. outb(data, addr);
  56. }
  57. static unsigned char mpu401_read_port(struct snd_mpu401 *mpu, unsigned long addr)
  58. {
  59. return inb(addr);
  60. }
  61. static void mpu401_write_mmio(struct snd_mpu401 *mpu, unsigned char data, unsigned long addr)
  62. {
  63. writeb(data, (void __iomem *)addr);
  64. }
  65. static unsigned char mpu401_read_mmio(struct snd_mpu401 *mpu, unsigned long addr)
  66. {
  67. return readb((void __iomem *)addr);
  68. }
  69. /* */
  70. static void snd_mpu401_uart_clear_rx(struct snd_mpu401 *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(struct snd_mpu401 *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. struct snd_mpu401 *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. struct snd_mpu401 *mpu = (struct snd_mpu401 *)data;
  120. unsigned long flags;
  121. spin_lock_irqsave(&mpu->timer_lock, flags);
  122. /*mpu->mode |= MPU401_MODE_TIMER;*/
  123. mpu->timer.expires = 1 + jiffies;
  124. add_timer(&mpu->timer);
  125. spin_unlock_irqrestore(&mpu->timer_lock, flags);
  126. if (mpu->rmidi)
  127. _snd_mpu401_uart_interrupt(mpu);
  128. }
  129. /*
  130. * initialize the timer callback if not programmed yet
  131. */
  132. static void snd_mpu401_uart_add_timer (struct snd_mpu401 *mpu, int input)
  133. {
  134. unsigned long flags;
  135. spin_lock_irqsave (&mpu->timer_lock, flags);
  136. if (mpu->timer_invoked == 0) {
  137. init_timer(&mpu->timer);
  138. mpu->timer.data = (unsigned long)mpu;
  139. mpu->timer.function = snd_mpu401_uart_timer;
  140. mpu->timer.expires = 1 + jiffies;
  141. add_timer(&mpu->timer);
  142. }
  143. mpu->timer_invoked |= input ? MPU401_MODE_INPUT_TIMER : MPU401_MODE_OUTPUT_TIMER;
  144. spin_unlock_irqrestore (&mpu->timer_lock, flags);
  145. }
  146. /*
  147. * remove the timer callback if still active
  148. */
  149. static void snd_mpu401_uart_remove_timer (struct snd_mpu401 *mpu, int input)
  150. {
  151. unsigned long flags;
  152. spin_lock_irqsave (&mpu->timer_lock, flags);
  153. if (mpu->timer_invoked) {
  154. mpu->timer_invoked &= input ? ~MPU401_MODE_INPUT_TIMER : ~MPU401_MODE_OUTPUT_TIMER;
  155. if (! mpu->timer_invoked)
  156. del_timer(&mpu->timer);
  157. }
  158. spin_unlock_irqrestore (&mpu->timer_lock, flags);
  159. }
  160. /*
  161. */
  162. static void snd_mpu401_uart_cmd(struct snd_mpu401 * mpu, unsigned char cmd, int ack)
  163. {
  164. unsigned long flags;
  165. int timeout, ok;
  166. spin_lock_irqsave(&mpu->input_lock, flags);
  167. if (mpu->hardware != MPU401_HW_TRID4DWAVE) {
  168. mpu->write(mpu, 0x00, MPU401D(mpu));
  169. /*snd_mpu401_uart_clear_rx(mpu);*/
  170. }
  171. /* ok. standard MPU-401 initialization */
  172. if (mpu->hardware != MPU401_HW_SB) {
  173. for (timeout = 1000; timeout > 0 && !snd_mpu401_output_ready(mpu); timeout--)
  174. udelay(10);
  175. #ifdef CONFIG_SND_DEBUG
  176. if (!timeout)
  177. snd_printk("cmd: tx timeout (status = 0x%x)\n", mpu->read(mpu, MPU401C(mpu)));
  178. #endif
  179. }
  180. mpu->write(mpu, cmd, MPU401C(mpu));
  181. if (ack) {
  182. ok = 0;
  183. timeout = 10000;
  184. while (!ok && timeout-- > 0) {
  185. if (snd_mpu401_input_avail(mpu)) {
  186. if (mpu->read(mpu, MPU401D(mpu)) == MPU401_ACK)
  187. ok = 1;
  188. }
  189. }
  190. if (!ok && mpu->read(mpu, MPU401D(mpu)) == MPU401_ACK)
  191. ok = 1;
  192. } else {
  193. ok = 1;
  194. }
  195. spin_unlock_irqrestore(&mpu->input_lock, flags);
  196. if (! ok)
  197. 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)));
  198. // 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)));
  199. }
  200. /*
  201. * input/output open/close - protected by open_mutex in rawmidi.c
  202. */
  203. static int snd_mpu401_uart_input_open(struct snd_rawmidi_substream *substream)
  204. {
  205. struct snd_mpu401 *mpu;
  206. int err;
  207. mpu = substream->rmidi->private_data;
  208. if (mpu->open_input && (err = mpu->open_input(mpu)) < 0)
  209. return err;
  210. if (! test_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode)) {
  211. snd_mpu401_uart_cmd(mpu, MPU401_RESET, 1);
  212. snd_mpu401_uart_cmd(mpu, MPU401_ENTER_UART, 1);
  213. }
  214. mpu->substream_input = substream;
  215. set_bit(MPU401_MODE_BIT_INPUT, &mpu->mode);
  216. return 0;
  217. }
  218. static int snd_mpu401_uart_output_open(struct snd_rawmidi_substream *substream)
  219. {
  220. struct snd_mpu401 *mpu;
  221. int err;
  222. mpu = substream->rmidi->private_data;
  223. if (mpu->open_output && (err = mpu->open_output(mpu)) < 0)
  224. return err;
  225. if (! test_bit(MPU401_MODE_BIT_INPUT, &mpu->mode)) {
  226. snd_mpu401_uart_cmd(mpu, MPU401_RESET, 1);
  227. snd_mpu401_uart_cmd(mpu, MPU401_ENTER_UART, 1);
  228. }
  229. mpu->substream_output = substream;
  230. set_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode);
  231. return 0;
  232. }
  233. static int snd_mpu401_uart_input_close(struct snd_rawmidi_substream *substream)
  234. {
  235. struct snd_mpu401 *mpu;
  236. mpu = substream->rmidi->private_data;
  237. clear_bit(MPU401_MODE_BIT_INPUT, &mpu->mode);
  238. mpu->substream_input = NULL;
  239. if (! test_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode))
  240. snd_mpu401_uart_cmd(mpu, MPU401_RESET, 0);
  241. if (mpu->close_input)
  242. mpu->close_input(mpu);
  243. return 0;
  244. }
  245. static int snd_mpu401_uart_output_close(struct snd_rawmidi_substream *substream)
  246. {
  247. struct snd_mpu401 *mpu;
  248. mpu = substream->rmidi->private_data;
  249. clear_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode);
  250. mpu->substream_output = NULL;
  251. if (! test_bit(MPU401_MODE_BIT_INPUT, &mpu->mode))
  252. snd_mpu401_uart_cmd(mpu, MPU401_RESET, 0);
  253. if (mpu->close_output)
  254. mpu->close_output(mpu);
  255. return 0;
  256. }
  257. /*
  258. * trigger input callback
  259. */
  260. static void snd_mpu401_uart_input_trigger(struct snd_rawmidi_substream *substream, int up)
  261. {
  262. unsigned long flags;
  263. struct snd_mpu401 *mpu;
  264. int max = 64;
  265. mpu = substream->rmidi->private_data;
  266. if (up) {
  267. if (! test_and_set_bit(MPU401_MODE_BIT_INPUT_TRIGGER, &mpu->mode)) {
  268. /* first time - flush FIFO */
  269. while (max-- > 0)
  270. mpu->read(mpu, MPU401D(mpu));
  271. if (mpu->irq < 0)
  272. snd_mpu401_uart_add_timer(mpu, 1);
  273. }
  274. /* read data in advance */
  275. spin_lock_irqsave(&mpu->input_lock, flags);
  276. snd_mpu401_uart_input_read(mpu);
  277. spin_unlock_irqrestore(&mpu->input_lock, flags);
  278. } else {
  279. if (mpu->irq < 0)
  280. snd_mpu401_uart_remove_timer(mpu, 1);
  281. clear_bit(MPU401_MODE_BIT_INPUT_TRIGGER, &mpu->mode);
  282. }
  283. }
  284. /*
  285. * transfer input pending data
  286. * call with input_lock spinlock held
  287. */
  288. static void snd_mpu401_uart_input_read(struct snd_mpu401 * mpu)
  289. {
  290. int max = 128;
  291. unsigned char byte;
  292. while (max-- > 0) {
  293. if (snd_mpu401_input_avail(mpu)) {
  294. byte = mpu->read(mpu, MPU401D(mpu));
  295. if (test_bit(MPU401_MODE_BIT_INPUT_TRIGGER, &mpu->mode))
  296. snd_rawmidi_receive(mpu->substream_input, &byte, 1);
  297. } else {
  298. break; /* input not available */
  299. }
  300. }
  301. }
  302. /*
  303. * Tx FIFO sizes:
  304. * CS4237B - 16 bytes
  305. * AudioDrive ES1688 - 12 bytes
  306. * S3 SonicVibes - 8 bytes
  307. * SoundBlaster AWE 64 - 2 bytes (ugly hardware)
  308. */
  309. /*
  310. * write output pending bytes
  311. * call with output_lock spinlock held
  312. */
  313. static void snd_mpu401_uart_output_write(struct snd_mpu401 * mpu)
  314. {
  315. unsigned char byte;
  316. int max = 256, timeout;
  317. do {
  318. if (snd_rawmidi_transmit_peek(mpu->substream_output, &byte, 1) == 1) {
  319. for (timeout = 100; timeout > 0; timeout--) {
  320. if (snd_mpu401_output_ready(mpu)) {
  321. mpu->write(mpu, byte, MPU401D(mpu));
  322. snd_rawmidi_transmit_ack(mpu->substream_output, 1);
  323. break;
  324. }
  325. }
  326. if (timeout == 0)
  327. break; /* Tx FIFO full - try again later */
  328. } else {
  329. snd_mpu401_uart_remove_timer (mpu, 0);
  330. break; /* no other data - leave the tx loop */
  331. }
  332. } while (--max > 0);
  333. }
  334. /*
  335. * output trigger callback
  336. */
  337. static void snd_mpu401_uart_output_trigger(struct snd_rawmidi_substream *substream, int up)
  338. {
  339. unsigned long flags;
  340. struct snd_mpu401 *mpu;
  341. mpu = substream->rmidi->private_data;
  342. if (up) {
  343. set_bit(MPU401_MODE_BIT_OUTPUT_TRIGGER, &mpu->mode);
  344. /* try to add the timer at each output trigger,
  345. * since the output timer might have been removed in
  346. * snd_mpu401_uart_output_write().
  347. */
  348. snd_mpu401_uart_add_timer(mpu, 0);
  349. /* output pending data */
  350. spin_lock_irqsave(&mpu->output_lock, flags);
  351. snd_mpu401_uart_output_write(mpu);
  352. spin_unlock_irqrestore(&mpu->output_lock, flags);
  353. } else {
  354. snd_mpu401_uart_remove_timer(mpu, 0);
  355. clear_bit(MPU401_MODE_BIT_OUTPUT_TRIGGER, &mpu->mode);
  356. }
  357. }
  358. /*
  359. */
  360. static struct snd_rawmidi_ops snd_mpu401_uart_output =
  361. {
  362. .open = snd_mpu401_uart_output_open,
  363. .close = snd_mpu401_uart_output_close,
  364. .trigger = snd_mpu401_uart_output_trigger,
  365. };
  366. static struct snd_rawmidi_ops snd_mpu401_uart_input =
  367. {
  368. .open = snd_mpu401_uart_input_open,
  369. .close = snd_mpu401_uart_input_close,
  370. .trigger = snd_mpu401_uart_input_trigger,
  371. };
  372. static void snd_mpu401_uart_free(struct snd_rawmidi *rmidi)
  373. {
  374. struct snd_mpu401 *mpu = rmidi->private_data;
  375. if (mpu->irq_flags && mpu->irq >= 0)
  376. free_irq(mpu->irq, (void *) mpu);
  377. release_and_free_resource(mpu->res);
  378. kfree(mpu);
  379. }
  380. /**
  381. * snd_mpu401_uart_new - create an MPU401-UART instance
  382. * @card: the card instance
  383. * @device: the device index, zero-based
  384. * @hardware: the hardware type, MPU401_HW_XXXX
  385. * @port: the base address of MPU401 port
  386. * @integrated: non-zero if the port was already reserved by the chip
  387. * @irq: the irq number, -1 if no interrupt for mpu
  388. * @irq_flags: the irq request flags (SA_XXX), 0 if irq was already reserved.
  389. * @rrawmidi: the pointer to store the new rawmidi instance
  390. *
  391. * Creates a new MPU-401 instance.
  392. *
  393. * Note that the rawmidi instance is returned on the rrawmidi argument,
  394. * not the mpu401 instance itself. To access to the mpu401 instance,
  395. * cast from rawmidi->private_data (with struct snd_mpu401 magic-cast).
  396. *
  397. * Returns zero if successful, or a negative error code.
  398. */
  399. int snd_mpu401_uart_new(struct snd_card *card, int device,
  400. unsigned short hardware,
  401. unsigned long port, int integrated,
  402. int irq, int irq_flags,
  403. struct snd_rawmidi ** rrawmidi)
  404. {
  405. struct snd_mpu401 *mpu;
  406. struct snd_rawmidi *rmidi;
  407. int err;
  408. if (rrawmidi)
  409. *rrawmidi = NULL;
  410. if ((err = snd_rawmidi_new(card, "MPU-401U", device, 1, 1, &rmidi)) < 0)
  411. return err;
  412. mpu = kzalloc(sizeof(*mpu), GFP_KERNEL);
  413. if (mpu == NULL) {
  414. snd_printk(KERN_ERR "mpu401_uart: cannot allocate\n");
  415. snd_device_free(card, rmidi);
  416. return -ENOMEM;
  417. }
  418. rmidi->private_data = mpu;
  419. rmidi->private_free = snd_mpu401_uart_free;
  420. spin_lock_init(&mpu->input_lock);
  421. spin_lock_init(&mpu->output_lock);
  422. spin_lock_init(&mpu->timer_lock);
  423. mpu->hardware = hardware;
  424. if (!integrated) {
  425. int res_size = hardware == MPU401_HW_PC98II ? 4 : 2;
  426. if ((mpu->res = request_region(port, res_size, "MPU401 UART")) == NULL) {
  427. snd_printk(KERN_ERR "mpu401_uart: unable to grab port 0x%lx size %d\n", port, res_size);
  428. snd_device_free(card, rmidi);
  429. return -EBUSY;
  430. }
  431. }
  432. switch (hardware) {
  433. case MPU401_HW_AUREAL:
  434. mpu->write = mpu401_write_mmio;
  435. mpu->read = mpu401_read_mmio;
  436. break;
  437. default:
  438. mpu->write = mpu401_write_port;
  439. mpu->read = mpu401_read_port;
  440. break;
  441. }
  442. mpu->port = port;
  443. if (hardware == MPU401_HW_PC98II)
  444. mpu->cport = port + 2;
  445. else
  446. mpu->cport = port + 1;
  447. if (irq >= 0 && irq_flags) {
  448. if (request_irq(irq, snd_mpu401_uart_interrupt, irq_flags, "MPU401 UART", (void *) mpu)) {
  449. snd_printk(KERN_ERR "mpu401_uart: unable to grab IRQ %d\n", irq);
  450. snd_device_free(card, rmidi);
  451. return -EBUSY;
  452. }
  453. }
  454. mpu->irq = irq;
  455. mpu->irq_flags = irq_flags;
  456. if (card->shortname[0])
  457. snprintf(rmidi->name, sizeof(rmidi->name), "%s MIDI", card->shortname);
  458. else
  459. sprintf(rmidi->name, "MPU-401 MIDI %d-%d", card->number, device);
  460. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_mpu401_uart_output);
  461. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_mpu401_uart_input);
  462. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
  463. SNDRV_RAWMIDI_INFO_INPUT |
  464. SNDRV_RAWMIDI_INFO_DUPLEX;
  465. mpu->rmidi = rmidi;
  466. if (rrawmidi)
  467. *rrawmidi = rmidi;
  468. return 0;
  469. }
  470. EXPORT_SYMBOL(snd_mpu401_uart_interrupt);
  471. EXPORT_SYMBOL(snd_mpu401_uart_new);
  472. /*
  473. * INIT part
  474. */
  475. static int __init alsa_mpu401_uart_init(void)
  476. {
  477. return 0;
  478. }
  479. static void __exit alsa_mpu401_uart_exit(void)
  480. {
  481. }
  482. module_init(alsa_mpu401_uart_init)
  483. module_exit(alsa_mpu401_uart_exit)