mpu401_uart.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.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. Without interrupt,
  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 <asm/io.h>
  31. #include <linux/delay.h>
  32. #include <linux/init.h>
  33. #include <linux/slab.h>
  34. #include <linux/ioport.h>
  35. #include <linux/module.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@perex.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) \
  48. (!(mpu->read(mpu, MPU401C(mpu)) & MPU401_RX_EMPTY))
  49. #define snd_mpu401_output_ready(mpu) \
  50. (!(mpu->read(mpu, MPU401C(mpu)) & MPU401_TX_FULL))
  51. /* Build in lowlevel io */
  52. static void mpu401_write_port(struct snd_mpu401 *mpu, unsigned char data,
  53. unsigned long addr)
  54. {
  55. outb(data, addr);
  56. }
  57. static unsigned char mpu401_read_port(struct snd_mpu401 *mpu,
  58. unsigned long addr)
  59. {
  60. return inb(addr);
  61. }
  62. static void mpu401_write_mmio(struct snd_mpu401 *mpu, unsigned char data,
  63. unsigned long addr)
  64. {
  65. writeb(data, (void __iomem *)addr);
  66. }
  67. static unsigned char mpu401_read_mmio(struct snd_mpu401 *mpu,
  68. unsigned long addr)
  69. {
  70. return readb((void __iomem *)addr);
  71. }
  72. /* */
  73. static void snd_mpu401_uart_clear_rx(struct snd_mpu401 *mpu)
  74. {
  75. int timeout = 100000;
  76. for (; timeout > 0 && snd_mpu401_input_avail(mpu); timeout--)
  77. mpu->read(mpu, MPU401D(mpu));
  78. #ifdef CONFIG_SND_DEBUG
  79. if (timeout <= 0)
  80. snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n",
  81. mpu->read(mpu, MPU401C(mpu)));
  82. #endif
  83. }
  84. static void uart_interrupt_tx(struct snd_mpu401 *mpu)
  85. {
  86. unsigned long flags;
  87. if (test_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode) &&
  88. test_bit(MPU401_MODE_BIT_OUTPUT_TRIGGER, &mpu->mode)) {
  89. spin_lock_irqsave(&mpu->output_lock, flags);
  90. snd_mpu401_uart_output_write(mpu);
  91. spin_unlock_irqrestore(&mpu->output_lock, flags);
  92. }
  93. }
  94. static void _snd_mpu401_uart_interrupt(struct snd_mpu401 *mpu)
  95. {
  96. unsigned long flags;
  97. if (mpu->info_flags & MPU401_INFO_INPUT) {
  98. spin_lock_irqsave(&mpu->input_lock, flags);
  99. if (test_bit(MPU401_MODE_BIT_INPUT, &mpu->mode))
  100. snd_mpu401_uart_input_read(mpu);
  101. else
  102. snd_mpu401_uart_clear_rx(mpu);
  103. spin_unlock_irqrestore(&mpu->input_lock, flags);
  104. }
  105. if (! (mpu->info_flags & MPU401_INFO_TX_IRQ))
  106. /* ok. for better Tx performance try do some output
  107. when input is done */
  108. uart_interrupt_tx(mpu);
  109. }
  110. /**
  111. * snd_mpu401_uart_interrupt - generic MPU401-UART interrupt handler
  112. * @irq: the irq number
  113. * @dev_id: mpu401 instance
  114. *
  115. * Processes the interrupt for MPU401-UART i/o.
  116. *
  117. * Return: %IRQ_HANDLED if the interrupt was handled. %IRQ_NONE otherwise.
  118. */
  119. irqreturn_t snd_mpu401_uart_interrupt(int irq, void *dev_id)
  120. {
  121. struct snd_mpu401 *mpu = dev_id;
  122. if (mpu == NULL)
  123. return IRQ_NONE;
  124. _snd_mpu401_uart_interrupt(mpu);
  125. return IRQ_HANDLED;
  126. }
  127. EXPORT_SYMBOL(snd_mpu401_uart_interrupt);
  128. /**
  129. * snd_mpu401_uart_interrupt_tx - generic MPU401-UART transmit irq handler
  130. * @irq: the irq number
  131. * @dev_id: mpu401 instance
  132. *
  133. * Processes the interrupt for MPU401-UART output.
  134. *
  135. * Return: %IRQ_HANDLED if the interrupt was handled. %IRQ_NONE otherwise.
  136. */
  137. irqreturn_t snd_mpu401_uart_interrupt_tx(int irq, void *dev_id)
  138. {
  139. struct snd_mpu401 *mpu = dev_id;
  140. if (mpu == NULL)
  141. return IRQ_NONE;
  142. uart_interrupt_tx(mpu);
  143. return IRQ_HANDLED;
  144. }
  145. EXPORT_SYMBOL(snd_mpu401_uart_interrupt_tx);
  146. /*
  147. * timer callback
  148. * reprogram the timer and call the interrupt job
  149. */
  150. static void snd_mpu401_uart_timer(unsigned long data)
  151. {
  152. struct snd_mpu401 *mpu = (struct snd_mpu401 *)data;
  153. unsigned long flags;
  154. spin_lock_irqsave(&mpu->timer_lock, flags);
  155. /*mpu->mode |= MPU401_MODE_TIMER;*/
  156. mpu->timer.expires = 1 + jiffies;
  157. add_timer(&mpu->timer);
  158. spin_unlock_irqrestore(&mpu->timer_lock, flags);
  159. if (mpu->rmidi)
  160. _snd_mpu401_uart_interrupt(mpu);
  161. }
  162. /*
  163. * initialize the timer callback if not programmed yet
  164. */
  165. static void snd_mpu401_uart_add_timer (struct snd_mpu401 *mpu, int input)
  166. {
  167. unsigned long flags;
  168. spin_lock_irqsave (&mpu->timer_lock, flags);
  169. if (mpu->timer_invoked == 0) {
  170. init_timer(&mpu->timer);
  171. mpu->timer.data = (unsigned long)mpu;
  172. mpu->timer.function = snd_mpu401_uart_timer;
  173. mpu->timer.expires = 1 + jiffies;
  174. add_timer(&mpu->timer);
  175. }
  176. mpu->timer_invoked |= input ? MPU401_MODE_INPUT_TIMER :
  177. MPU401_MODE_OUTPUT_TIMER;
  178. spin_unlock_irqrestore (&mpu->timer_lock, flags);
  179. }
  180. /*
  181. * remove the timer callback if still active
  182. */
  183. static void snd_mpu401_uart_remove_timer (struct snd_mpu401 *mpu, int input)
  184. {
  185. unsigned long flags;
  186. spin_lock_irqsave (&mpu->timer_lock, flags);
  187. if (mpu->timer_invoked) {
  188. mpu->timer_invoked &= input ? ~MPU401_MODE_INPUT_TIMER :
  189. ~MPU401_MODE_OUTPUT_TIMER;
  190. if (! mpu->timer_invoked)
  191. del_timer(&mpu->timer);
  192. }
  193. spin_unlock_irqrestore (&mpu->timer_lock, flags);
  194. }
  195. /*
  196. * send a UART command
  197. * return zero if successful, non-zero for some errors
  198. */
  199. static int snd_mpu401_uart_cmd(struct snd_mpu401 * mpu, unsigned char cmd,
  200. int ack)
  201. {
  202. unsigned long flags;
  203. int timeout, ok;
  204. spin_lock_irqsave(&mpu->input_lock, flags);
  205. if (mpu->hardware != MPU401_HW_TRID4DWAVE) {
  206. mpu->write(mpu, 0x00, MPU401D(mpu));
  207. /*snd_mpu401_uart_clear_rx(mpu);*/
  208. }
  209. /* ok. standard MPU-401 initialization */
  210. if (mpu->hardware != MPU401_HW_SB) {
  211. for (timeout = 1000; timeout > 0 &&
  212. !snd_mpu401_output_ready(mpu); timeout--)
  213. udelay(10);
  214. #ifdef CONFIG_SND_DEBUG
  215. if (!timeout)
  216. snd_printk(KERN_ERR "cmd: tx timeout (status = 0x%x)\n",
  217. mpu->read(mpu, MPU401C(mpu)));
  218. #endif
  219. }
  220. mpu->write(mpu, cmd, MPU401C(mpu));
  221. if (ack && !(mpu->info_flags & MPU401_INFO_NO_ACK)) {
  222. ok = 0;
  223. timeout = 10000;
  224. while (!ok && timeout-- > 0) {
  225. if (snd_mpu401_input_avail(mpu)) {
  226. if (mpu->read(mpu, MPU401D(mpu)) == MPU401_ACK)
  227. ok = 1;
  228. }
  229. }
  230. if (!ok && mpu->read(mpu, MPU401D(mpu)) == MPU401_ACK)
  231. ok = 1;
  232. } else
  233. ok = 1;
  234. spin_unlock_irqrestore(&mpu->input_lock, flags);
  235. if (!ok) {
  236. snd_printk(KERN_ERR "cmd: 0x%x failed at 0x%lx "
  237. "(status = 0x%x, data = 0x%x)\n", cmd, mpu->port,
  238. mpu->read(mpu, MPU401C(mpu)),
  239. mpu->read(mpu, MPU401D(mpu)));
  240. return 1;
  241. }
  242. return 0;
  243. }
  244. static int snd_mpu401_do_reset(struct snd_mpu401 *mpu)
  245. {
  246. if (snd_mpu401_uart_cmd(mpu, MPU401_RESET, 1))
  247. return -EIO;
  248. if (snd_mpu401_uart_cmd(mpu, MPU401_ENTER_UART, 0))
  249. return -EIO;
  250. return 0;
  251. }
  252. /*
  253. * input/output open/close - protected by open_mutex in rawmidi.c
  254. */
  255. static int snd_mpu401_uart_input_open(struct snd_rawmidi_substream *substream)
  256. {
  257. struct snd_mpu401 *mpu;
  258. int err;
  259. mpu = substream->rmidi->private_data;
  260. if (mpu->open_input && (err = mpu->open_input(mpu)) < 0)
  261. return err;
  262. if (! test_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode)) {
  263. if (snd_mpu401_do_reset(mpu) < 0)
  264. goto error_out;
  265. }
  266. mpu->substream_input = substream;
  267. set_bit(MPU401_MODE_BIT_INPUT, &mpu->mode);
  268. return 0;
  269. error_out:
  270. if (mpu->open_input && mpu->close_input)
  271. mpu->close_input(mpu);
  272. return -EIO;
  273. }
  274. static int snd_mpu401_uart_output_open(struct snd_rawmidi_substream *substream)
  275. {
  276. struct snd_mpu401 *mpu;
  277. int err;
  278. mpu = substream->rmidi->private_data;
  279. if (mpu->open_output && (err = mpu->open_output(mpu)) < 0)
  280. return err;
  281. if (! test_bit(MPU401_MODE_BIT_INPUT, &mpu->mode)) {
  282. if (snd_mpu401_do_reset(mpu) < 0)
  283. goto error_out;
  284. }
  285. mpu->substream_output = substream;
  286. set_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode);
  287. return 0;
  288. error_out:
  289. if (mpu->open_output && mpu->close_output)
  290. mpu->close_output(mpu);
  291. return -EIO;
  292. }
  293. static int snd_mpu401_uart_input_close(struct snd_rawmidi_substream *substream)
  294. {
  295. struct snd_mpu401 *mpu;
  296. int err = 0;
  297. mpu = substream->rmidi->private_data;
  298. clear_bit(MPU401_MODE_BIT_INPUT, &mpu->mode);
  299. mpu->substream_input = NULL;
  300. if (! test_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode))
  301. err = snd_mpu401_uart_cmd(mpu, MPU401_RESET, 0);
  302. if (mpu->close_input)
  303. mpu->close_input(mpu);
  304. if (err)
  305. return -EIO;
  306. return 0;
  307. }
  308. static int snd_mpu401_uart_output_close(struct snd_rawmidi_substream *substream)
  309. {
  310. struct snd_mpu401 *mpu;
  311. int err = 0;
  312. mpu = substream->rmidi->private_data;
  313. clear_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode);
  314. mpu->substream_output = NULL;
  315. if (! test_bit(MPU401_MODE_BIT_INPUT, &mpu->mode))
  316. err = snd_mpu401_uart_cmd(mpu, MPU401_RESET, 0);
  317. if (mpu->close_output)
  318. mpu->close_output(mpu);
  319. if (err)
  320. return -EIO;
  321. return 0;
  322. }
  323. /*
  324. * trigger input callback
  325. */
  326. static void
  327. snd_mpu401_uart_input_trigger(struct snd_rawmidi_substream *substream, int up)
  328. {
  329. unsigned long flags;
  330. struct snd_mpu401 *mpu;
  331. int max = 64;
  332. mpu = substream->rmidi->private_data;
  333. if (up) {
  334. if (! test_and_set_bit(MPU401_MODE_BIT_INPUT_TRIGGER,
  335. &mpu->mode)) {
  336. /* first time - flush FIFO */
  337. while (max-- > 0)
  338. mpu->read(mpu, MPU401D(mpu));
  339. if (mpu->info_flags & MPU401_INFO_USE_TIMER)
  340. snd_mpu401_uart_add_timer(mpu, 1);
  341. }
  342. /* read data in advance */
  343. spin_lock_irqsave(&mpu->input_lock, flags);
  344. snd_mpu401_uart_input_read(mpu);
  345. spin_unlock_irqrestore(&mpu->input_lock, flags);
  346. } else {
  347. if (mpu->info_flags & MPU401_INFO_USE_TIMER)
  348. snd_mpu401_uart_remove_timer(mpu, 1);
  349. clear_bit(MPU401_MODE_BIT_INPUT_TRIGGER, &mpu->mode);
  350. }
  351. }
  352. /*
  353. * transfer input pending data
  354. * call with input_lock spinlock held
  355. */
  356. static void snd_mpu401_uart_input_read(struct snd_mpu401 * mpu)
  357. {
  358. int max = 128;
  359. unsigned char byte;
  360. while (max-- > 0) {
  361. if (! snd_mpu401_input_avail(mpu))
  362. break; /* input not available */
  363. byte = mpu->read(mpu, MPU401D(mpu));
  364. if (test_bit(MPU401_MODE_BIT_INPUT_TRIGGER, &mpu->mode))
  365. snd_rawmidi_receive(mpu->substream_input, &byte, 1);
  366. }
  367. }
  368. /*
  369. * Tx FIFO sizes:
  370. * CS4237B - 16 bytes
  371. * AudioDrive ES1688 - 12 bytes
  372. * S3 SonicVibes - 8 bytes
  373. * SoundBlaster AWE 64 - 2 bytes (ugly hardware)
  374. */
  375. /*
  376. * write output pending bytes
  377. * call with output_lock spinlock held
  378. */
  379. static void snd_mpu401_uart_output_write(struct snd_mpu401 * mpu)
  380. {
  381. unsigned char byte;
  382. int max = 256;
  383. do {
  384. if (snd_rawmidi_transmit_peek(mpu->substream_output,
  385. &byte, 1) == 1) {
  386. /*
  387. * Try twice because there is hardware that insists on
  388. * setting the output busy bit after each write.
  389. */
  390. if (!snd_mpu401_output_ready(mpu) &&
  391. !snd_mpu401_output_ready(mpu))
  392. break; /* Tx FIFO full - try again later */
  393. mpu->write(mpu, byte, MPU401D(mpu));
  394. snd_rawmidi_transmit_ack(mpu->substream_output, 1);
  395. } else {
  396. snd_mpu401_uart_remove_timer (mpu, 0);
  397. break; /* no other data - leave the tx loop */
  398. }
  399. } while (--max > 0);
  400. }
  401. /*
  402. * output trigger callback
  403. */
  404. static void
  405. snd_mpu401_uart_output_trigger(struct snd_rawmidi_substream *substream, int up)
  406. {
  407. unsigned long flags;
  408. struct snd_mpu401 *mpu;
  409. mpu = substream->rmidi->private_data;
  410. if (up) {
  411. set_bit(MPU401_MODE_BIT_OUTPUT_TRIGGER, &mpu->mode);
  412. /* try to add the timer at each output trigger,
  413. * since the output timer might have been removed in
  414. * snd_mpu401_uart_output_write().
  415. */
  416. if (! (mpu->info_flags & MPU401_INFO_TX_IRQ))
  417. snd_mpu401_uart_add_timer(mpu, 0);
  418. /* output pending data */
  419. spin_lock_irqsave(&mpu->output_lock, flags);
  420. snd_mpu401_uart_output_write(mpu);
  421. spin_unlock_irqrestore(&mpu->output_lock, flags);
  422. } else {
  423. if (! (mpu->info_flags & MPU401_INFO_TX_IRQ))
  424. snd_mpu401_uart_remove_timer(mpu, 0);
  425. clear_bit(MPU401_MODE_BIT_OUTPUT_TRIGGER, &mpu->mode);
  426. }
  427. }
  428. /*
  429. */
  430. static struct snd_rawmidi_ops snd_mpu401_uart_output =
  431. {
  432. .open = snd_mpu401_uart_output_open,
  433. .close = snd_mpu401_uart_output_close,
  434. .trigger = snd_mpu401_uart_output_trigger,
  435. };
  436. static struct snd_rawmidi_ops snd_mpu401_uart_input =
  437. {
  438. .open = snd_mpu401_uart_input_open,
  439. .close = snd_mpu401_uart_input_close,
  440. .trigger = snd_mpu401_uart_input_trigger,
  441. };
  442. static void snd_mpu401_uart_free(struct snd_rawmidi *rmidi)
  443. {
  444. struct snd_mpu401 *mpu = rmidi->private_data;
  445. if (mpu->irq >= 0)
  446. free_irq(mpu->irq, (void *) mpu);
  447. release_and_free_resource(mpu->res);
  448. kfree(mpu);
  449. }
  450. /**
  451. * snd_mpu401_uart_new - create an MPU401-UART instance
  452. * @card: the card instance
  453. * @device: the device index, zero-based
  454. * @hardware: the hardware type, MPU401_HW_XXXX
  455. * @port: the base address of MPU401 port
  456. * @info_flags: bitflags MPU401_INFO_XXX
  457. * @irq: the ISA irq number, -1 if not to be allocated
  458. * @rrawmidi: the pointer to store the new rawmidi instance
  459. *
  460. * Creates a new MPU-401 instance.
  461. *
  462. * Note that the rawmidi instance is returned on the rrawmidi argument,
  463. * not the mpu401 instance itself. To access to the mpu401 instance,
  464. * cast from rawmidi->private_data (with struct snd_mpu401 magic-cast).
  465. *
  466. * Return: Zero if successful, or a negative error code.
  467. */
  468. int snd_mpu401_uart_new(struct snd_card *card, int device,
  469. unsigned short hardware,
  470. unsigned long port,
  471. unsigned int info_flags,
  472. int irq,
  473. struct snd_rawmidi ** rrawmidi)
  474. {
  475. struct snd_mpu401 *mpu;
  476. struct snd_rawmidi *rmidi;
  477. int in_enable, out_enable;
  478. int err;
  479. if (rrawmidi)
  480. *rrawmidi = NULL;
  481. if (! (info_flags & (MPU401_INFO_INPUT | MPU401_INFO_OUTPUT)))
  482. info_flags |= MPU401_INFO_INPUT | MPU401_INFO_OUTPUT;
  483. in_enable = (info_flags & MPU401_INFO_INPUT) ? 1 : 0;
  484. out_enable = (info_flags & MPU401_INFO_OUTPUT) ? 1 : 0;
  485. if ((err = snd_rawmidi_new(card, "MPU-401U", device,
  486. out_enable, in_enable, &rmidi)) < 0)
  487. return err;
  488. mpu = kzalloc(sizeof(*mpu), GFP_KERNEL);
  489. if (mpu == NULL) {
  490. snd_printk(KERN_ERR "mpu401_uart: cannot allocate\n");
  491. snd_device_free(card, rmidi);
  492. return -ENOMEM;
  493. }
  494. rmidi->private_data = mpu;
  495. rmidi->private_free = snd_mpu401_uart_free;
  496. spin_lock_init(&mpu->input_lock);
  497. spin_lock_init(&mpu->output_lock);
  498. spin_lock_init(&mpu->timer_lock);
  499. mpu->hardware = hardware;
  500. mpu->irq = -1;
  501. if (! (info_flags & MPU401_INFO_INTEGRATED)) {
  502. int res_size = hardware == MPU401_HW_PC98II ? 4 : 2;
  503. mpu->res = request_region(port, res_size, "MPU401 UART");
  504. if (mpu->res == NULL) {
  505. snd_printk(KERN_ERR "mpu401_uart: "
  506. "unable to grab port 0x%lx size %d\n",
  507. port, res_size);
  508. snd_device_free(card, rmidi);
  509. return -EBUSY;
  510. }
  511. }
  512. if (info_flags & MPU401_INFO_MMIO) {
  513. mpu->write = mpu401_write_mmio;
  514. mpu->read = mpu401_read_mmio;
  515. } else {
  516. mpu->write = mpu401_write_port;
  517. mpu->read = mpu401_read_port;
  518. }
  519. mpu->port = port;
  520. if (hardware == MPU401_HW_PC98II)
  521. mpu->cport = port + 2;
  522. else
  523. mpu->cport = port + 1;
  524. if (irq >= 0) {
  525. if (request_irq(irq, snd_mpu401_uart_interrupt, 0,
  526. "MPU401 UART", (void *) mpu)) {
  527. snd_printk(KERN_ERR "mpu401_uart: "
  528. "unable to grab IRQ %d\n", irq);
  529. snd_device_free(card, rmidi);
  530. return -EBUSY;
  531. }
  532. }
  533. if (irq < 0 && !(info_flags & MPU401_INFO_IRQ_HOOK))
  534. info_flags |= MPU401_INFO_USE_TIMER;
  535. mpu->info_flags = info_flags;
  536. mpu->irq = irq;
  537. if (card->shortname[0])
  538. snprintf(rmidi->name, sizeof(rmidi->name), "%s MIDI",
  539. card->shortname);
  540. else
  541. sprintf(rmidi->name, "MPU-401 MIDI %d-%d",card->number, device);
  542. if (out_enable) {
  543. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  544. &snd_mpu401_uart_output);
  545. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
  546. }
  547. if (in_enable) {
  548. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  549. &snd_mpu401_uart_input);
  550. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
  551. if (out_enable)
  552. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;
  553. }
  554. mpu->rmidi = rmidi;
  555. if (rrawmidi)
  556. *rrawmidi = rmidi;
  557. return 0;
  558. }
  559. EXPORT_SYMBOL(snd_mpu401_uart_new);
  560. /*
  561. * INIT part
  562. */
  563. static int __init alsa_mpu401_uart_init(void)
  564. {
  565. return 0;
  566. }
  567. static void __exit alsa_mpu401_uart_exit(void)
  568. {
  569. }
  570. module_init(alsa_mpu401_uart_init)
  571. module_exit(alsa_mpu401_uart_exit)