mpu401.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 (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. snd_card_free(card);
  78. return -ENODEV;
  79. }
  80. if ((err = snd_card_register(card)) < 0) {
  81. snd_card_free(card);
  82. return err;
  83. }
  84. *rcard = card;
  85. return 0;
  86. }
  87. static int __devinit snd_mpu401_probe(int dev)
  88. {
  89. if (port[dev] == SNDRV_AUTO_PORT) {
  90. snd_printk(KERN_ERR "specify port\n");
  91. return -EINVAL;
  92. }
  93. if (irq[dev] == SNDRV_AUTO_IRQ) {
  94. snd_printk(KERN_ERR "specify or disable IRQ\n");
  95. return -EINVAL;
  96. }
  97. return snd_mpu401_create(dev, &snd_mpu401_legacy_cards[dev]);
  98. }
  99. #ifdef CONFIG_PNP
  100. #define IO_EXTENT 2
  101. static struct pnp_device_id snd_mpu401_pnpids[] = {
  102. { .id = "PNPb006" },
  103. { .id = "" }
  104. };
  105. MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids);
  106. static int __init snd_mpu401_pnp(int dev, struct pnp_dev *device,
  107. const struct pnp_device_id *id)
  108. {
  109. if (!pnp_port_valid(device, 0) ||
  110. pnp_port_flags(device, 0) & IORESOURCE_DISABLED) {
  111. snd_printk(KERN_ERR "no PnP port\n");
  112. return -ENODEV;
  113. }
  114. if (pnp_port_len(device, 0) < IO_EXTENT) {
  115. snd_printk(KERN_ERR "PnP port length is %ld, expected %d\n",
  116. pnp_port_len(device, 0), IO_EXTENT);
  117. return -ENODEV;
  118. }
  119. port[dev] = pnp_port_start(device, 0);
  120. if (!pnp_irq_valid(device, 0) ||
  121. pnp_irq_flags(device, 0) & IORESOURCE_DISABLED) {
  122. snd_printk(KERN_WARNING "no PnP irq, using polling\n");
  123. irq[dev] = -1;
  124. } else {
  125. irq[dev] = pnp_irq(device, 0);
  126. }
  127. return 0;
  128. }
  129. static int __devinit snd_mpu401_pnp_probe(struct pnp_dev *pnp_dev,
  130. const struct pnp_device_id *id)
  131. {
  132. static int dev;
  133. snd_card_t *card;
  134. int err;
  135. for ( ; dev < SNDRV_CARDS; ++dev) {
  136. if (!enable[dev] || !pnp[dev])
  137. continue;
  138. err = snd_mpu401_pnp(dev, pnp_dev, id);
  139. if (err < 0)
  140. return err;
  141. err = snd_mpu401_create(dev, &card);
  142. if (err < 0)
  143. return err;
  144. snd_card_set_dev(card, &pnp_dev->dev);
  145. pnp_set_drvdata(pnp_dev, card);
  146. ++dev;
  147. return 0;
  148. }
  149. return -ENODEV;
  150. }
  151. static void __devexit snd_mpu401_pnp_remove(struct pnp_dev *dev)
  152. {
  153. snd_card_t *card = (snd_card_t *) pnp_get_drvdata(dev);
  154. snd_card_disconnect(card);
  155. snd_card_free_in_thread(card);
  156. }
  157. static struct pnp_driver snd_mpu401_pnp_driver = {
  158. .name = "mpu401",
  159. .id_table = snd_mpu401_pnpids,
  160. .probe = snd_mpu401_pnp_probe,
  161. .remove = __devexit_p(snd_mpu401_pnp_remove),
  162. };
  163. #else
  164. static struct pnp_driver snd_mpu401_pnp_driver;
  165. #endif
  166. static int __init alsa_card_mpu401_init(void)
  167. {
  168. int dev, devices = 0;
  169. int err;
  170. for (dev = 0; dev < SNDRV_CARDS; dev++) {
  171. if (!enable[dev])
  172. continue;
  173. #ifdef CONFIG_PNP
  174. if (pnp[dev])
  175. continue;
  176. #endif
  177. if (snd_mpu401_probe(dev) >= 0)
  178. devices++;
  179. }
  180. if ((err = pnp_register_driver(&snd_mpu401_pnp_driver)) >= 0) {
  181. pnp_registered = 1;
  182. devices += err;
  183. }
  184. if (!devices) {
  185. #ifdef MODULE
  186. printk(KERN_ERR "MPU-401 device not found or device busy\n");
  187. #endif
  188. if (pnp_registered)
  189. pnp_unregister_driver(&snd_mpu401_pnp_driver);
  190. return -ENODEV;
  191. }
  192. return 0;
  193. }
  194. static void __exit alsa_card_mpu401_exit(void)
  195. {
  196. int idx;
  197. if (pnp_registered)
  198. pnp_unregister_driver(&snd_mpu401_pnp_driver);
  199. for (idx = 0; idx < SNDRV_CARDS; idx++)
  200. snd_card_free(snd_mpu401_legacy_cards[idx]);
  201. }
  202. module_init(alsa_card_mpu401_init)
  203. module_exit(alsa_card_mpu401_exit)