mpu401.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Driver for generic MPU-401 boards (UART mode only)
  3. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  4. * Copyright (c) 2004 by Castet Matthieu <castet.matthieu@free.fr>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <sound/driver.h>
  23. #include <linux/init.h>
  24. #include <linux/pnp.h>
  25. #include <linux/moduleparam.h>
  26. #include <sound/core.h>
  27. #include <sound/mpu401.h>
  28. #include <sound/initval.h>
  29. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  30. MODULE_DESCRIPTION("MPU-401 UART");
  31. MODULE_LICENSE("GPL");
  32. static int index[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -2}; /* exclude the first card */
  33. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  34. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
  35. #ifdef CONFIG_PNP
  36. static int pnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
  37. #endif
  38. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* MPU-401 port number */
  39. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* MPU-401 IRQ */
  40. module_param_array(index, int, NULL, 0444);
  41. MODULE_PARM_DESC(index, "Index value for MPU-401 device.");
  42. module_param_array(id, charp, NULL, 0444);
  43. MODULE_PARM_DESC(id, "ID string for MPU-401 device.");
  44. module_param_array(enable, bool, NULL, 0444);
  45. MODULE_PARM_DESC(enable, "Enable MPU-401 device.");
  46. #ifdef CONFIG_PNP
  47. module_param_array(pnp, bool, NULL, 0444);
  48. MODULE_PARM_DESC(pnp, "PnP detection for MPU-401 device.");
  49. #endif
  50. module_param_array(port, long, NULL, 0444);
  51. MODULE_PARM_DESC(port, "Port # for MPU-401 device.");
  52. module_param_array(irq, int, NULL, 0444);
  53. MODULE_PARM_DESC(irq, "IRQ # for MPU-401 device.");
  54. static snd_card_t *snd_mpu401_legacy_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
  55. static int pnp_registered = 0;
  56. static int snd_mpu401_create(int dev, snd_card_t **rcard)
  57. {
  58. snd_card_t *card;
  59. int err;
  60. *rcard = NULL;
  61. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  62. if (card == NULL)
  63. return -ENOMEM;
  64. strcpy(card->driver, "MPU-401 UART");
  65. strcpy(card->shortname, card->driver);
  66. sprintf(card->longname, "%s at %#lx, ", card->shortname, port[dev]);
  67. if (irq[dev] >= 0) {
  68. sprintf(card->longname + strlen(card->longname), "irq %d", irq[dev]);
  69. } else {
  70. strcat(card->longname, "polled");
  71. }
  72. if ((err = snd_mpu401_uart_new(card, 0,
  73. MPU401_HW_MPU401,
  74. port[dev], 0,
  75. irq[dev], irq[dev] >= 0 ? SA_INTERRUPT : 0, NULL)) < 0) {
  76. printk(KERN_ERR "MPU401 not detected at 0x%lx\n", port[dev]);
  77. goto _err;
  78. }
  79. if ((err = snd_card_set_generic_dev(card)) < 0)
  80. goto _err;
  81. if ((err = snd_card_register(card)) < 0)
  82. goto _err;
  83. *rcard = card;
  84. return 0;
  85. _err:
  86. snd_card_free(card);
  87. return err;
  88. }
  89. static int __devinit snd_mpu401_probe(int dev)
  90. {
  91. if (port[dev] == SNDRV_AUTO_PORT) {
  92. snd_printk(KERN_ERR "specify port\n");
  93. return -EINVAL;
  94. }
  95. if (irq[dev] == SNDRV_AUTO_IRQ) {
  96. snd_printk(KERN_ERR "specify or disable IRQ\n");
  97. return -EINVAL;
  98. }
  99. return snd_mpu401_create(dev, &snd_mpu401_legacy_cards[dev]);
  100. }
  101. #ifdef CONFIG_PNP
  102. #define IO_EXTENT 2
  103. static struct pnp_device_id snd_mpu401_pnpids[] = {
  104. { .id = "PNPb006" },
  105. { .id = "" }
  106. };
  107. MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids);
  108. static int __init snd_mpu401_pnp(int dev, struct pnp_dev *device,
  109. const struct pnp_device_id *id)
  110. {
  111. if (!pnp_port_valid(device, 0) ||
  112. pnp_port_flags(device, 0) & IORESOURCE_DISABLED) {
  113. snd_printk(KERN_ERR "no PnP port\n");
  114. return -ENODEV;
  115. }
  116. if (pnp_port_len(device, 0) < IO_EXTENT) {
  117. snd_printk(KERN_ERR "PnP port length is %ld, expected %d\n",
  118. pnp_port_len(device, 0), IO_EXTENT);
  119. return -ENODEV;
  120. }
  121. port[dev] = pnp_port_start(device, 0);
  122. if (!pnp_irq_valid(device, 0) ||
  123. pnp_irq_flags(device, 0) & IORESOURCE_DISABLED) {
  124. snd_printk(KERN_WARNING "no PnP irq, using polling\n");
  125. irq[dev] = -1;
  126. } else {
  127. irq[dev] = pnp_irq(device, 0);
  128. }
  129. return 0;
  130. }
  131. static int __devinit snd_mpu401_pnp_probe(struct pnp_dev *pnp_dev,
  132. const struct pnp_device_id *id)
  133. {
  134. static int dev;
  135. snd_card_t *card;
  136. int err;
  137. for ( ; dev < SNDRV_CARDS; ++dev) {
  138. if (!enable[dev] || !pnp[dev])
  139. continue;
  140. err = snd_mpu401_pnp(dev, pnp_dev, id);
  141. if (err < 0)
  142. return err;
  143. err = snd_mpu401_create(dev, &card);
  144. if (err < 0)
  145. return err;
  146. snd_card_set_dev(card, &pnp_dev->dev);
  147. pnp_set_drvdata(pnp_dev, card);
  148. ++dev;
  149. return 0;
  150. }
  151. return -ENODEV;
  152. }
  153. static void __devexit snd_mpu401_pnp_remove(struct pnp_dev *dev)
  154. {
  155. snd_card_t *card = (snd_card_t *) pnp_get_drvdata(dev);
  156. snd_card_disconnect(card);
  157. snd_card_free_in_thread(card);
  158. }
  159. static struct pnp_driver snd_mpu401_pnp_driver = {
  160. .name = "mpu401",
  161. .id_table = snd_mpu401_pnpids,
  162. .probe = snd_mpu401_pnp_probe,
  163. .remove = __devexit_p(snd_mpu401_pnp_remove),
  164. };
  165. #else
  166. static struct pnp_driver snd_mpu401_pnp_driver;
  167. #endif
  168. static int __init alsa_card_mpu401_init(void)
  169. {
  170. int dev, devices = 0;
  171. int err;
  172. for (dev = 0; dev < SNDRV_CARDS; dev++) {
  173. if (!enable[dev])
  174. continue;
  175. #ifdef CONFIG_PNP
  176. if (pnp[dev])
  177. continue;
  178. #endif
  179. if (snd_mpu401_probe(dev) >= 0)
  180. devices++;
  181. }
  182. if ((err = pnp_register_driver(&snd_mpu401_pnp_driver)) >= 0) {
  183. pnp_registered = 1;
  184. devices += err;
  185. }
  186. if (!devices) {
  187. #ifdef MODULE
  188. printk(KERN_ERR "MPU-401 device not found or device busy\n");
  189. #endif
  190. if (pnp_registered)
  191. pnp_unregister_driver(&snd_mpu401_pnp_driver);
  192. return -ENODEV;
  193. }
  194. return 0;
  195. }
  196. static void __exit alsa_card_mpu401_exit(void)
  197. {
  198. int idx;
  199. if (pnp_registered)
  200. pnp_unregister_driver(&snd_mpu401_pnp_driver);
  201. for (idx = 0; idx < SNDRV_CARDS; idx++)
  202. snd_card_free(snd_mpu401_legacy_cards[idx]);
  203. }
  204. module_init(alsa_card_mpu401_init)
  205. module_exit(alsa_card_mpu401_exit)