mpu401.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Driver for generic MPU-401 boards (UART mode only)
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.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 <linux/init.h>
  23. #include <linux/pnp.h>
  24. #include <linux/err.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/module.h>
  27. #include <sound/core.h>
  28. #include <sound/mpu401.h>
  29. #include <sound/initval.h>
  30. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  31. MODULE_DESCRIPTION("MPU-401 UART");
  32. MODULE_LICENSE("GPL");
  33. static int index[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -2}; /* exclude the first card */
  34. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  35. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */
  36. #ifdef CONFIG_PNP
  37. static bool pnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
  38. #endif
  39. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* MPU-401 port number */
  40. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* MPU-401 IRQ */
  41. static bool uart_enter[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
  42. module_param_array(index, int, NULL, 0444);
  43. MODULE_PARM_DESC(index, "Index value for MPU-401 device.");
  44. module_param_array(id, charp, NULL, 0444);
  45. MODULE_PARM_DESC(id, "ID string for MPU-401 device.");
  46. module_param_array(enable, bool, NULL, 0444);
  47. MODULE_PARM_DESC(enable, "Enable MPU-401 device.");
  48. #ifdef CONFIG_PNP
  49. module_param_array(pnp, bool, NULL, 0444);
  50. MODULE_PARM_DESC(pnp, "PnP detection for MPU-401 device.");
  51. #endif
  52. module_param_array(port, long, NULL, 0444);
  53. MODULE_PARM_DESC(port, "Port # for MPU-401 device.");
  54. module_param_array(irq, int, NULL, 0444);
  55. MODULE_PARM_DESC(irq, "IRQ # for MPU-401 device.");
  56. module_param_array(uart_enter, bool, NULL, 0444);
  57. MODULE_PARM_DESC(uart_enter, "Issue UART_ENTER command at open.");
  58. static struct platform_device *platform_devices[SNDRV_CARDS];
  59. static int pnp_registered;
  60. static unsigned int snd_mpu401_devices;
  61. static int snd_mpu401_create(struct device *devptr, int dev,
  62. struct snd_card **rcard)
  63. {
  64. struct snd_card *card;
  65. int err;
  66. if (!uart_enter[dev])
  67. snd_printk(KERN_ERR "the uart_enter option is obsolete; remove it\n");
  68. *rcard = NULL;
  69. err = snd_card_new(devptr, index[dev], id[dev], THIS_MODULE,
  70. 0, &card);
  71. if (err < 0)
  72. return err;
  73. strcpy(card->driver, "MPU-401 UART");
  74. strcpy(card->shortname, card->driver);
  75. sprintf(card->longname, "%s at %#lx, ", card->shortname, port[dev]);
  76. if (irq[dev] >= 0) {
  77. sprintf(card->longname + strlen(card->longname), "irq %d", irq[dev]);
  78. } else {
  79. strcat(card->longname, "polled");
  80. }
  81. err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, port[dev], 0,
  82. irq[dev], NULL);
  83. if (err < 0) {
  84. printk(KERN_ERR "MPU401 not detected at 0x%lx\n", port[dev]);
  85. goto _err;
  86. }
  87. *rcard = card;
  88. return 0;
  89. _err:
  90. snd_card_free(card);
  91. return err;
  92. }
  93. static int snd_mpu401_probe(struct platform_device *devptr)
  94. {
  95. int dev = devptr->id;
  96. int err;
  97. struct snd_card *card;
  98. if (port[dev] == SNDRV_AUTO_PORT) {
  99. snd_printk(KERN_ERR "specify port\n");
  100. return -EINVAL;
  101. }
  102. if (irq[dev] == SNDRV_AUTO_IRQ) {
  103. snd_printk(KERN_ERR "specify or disable IRQ\n");
  104. return -EINVAL;
  105. }
  106. err = snd_mpu401_create(&devptr->dev, dev, &card);
  107. if (err < 0)
  108. return err;
  109. if ((err = snd_card_register(card)) < 0) {
  110. snd_card_free(card);
  111. return err;
  112. }
  113. platform_set_drvdata(devptr, card);
  114. return 0;
  115. }
  116. static int snd_mpu401_remove(struct platform_device *devptr)
  117. {
  118. snd_card_free(platform_get_drvdata(devptr));
  119. return 0;
  120. }
  121. #define SND_MPU401_DRIVER "snd_mpu401"
  122. static struct platform_driver snd_mpu401_driver = {
  123. .probe = snd_mpu401_probe,
  124. .remove = snd_mpu401_remove,
  125. .driver = {
  126. .name = SND_MPU401_DRIVER,
  127. },
  128. };
  129. #ifdef CONFIG_PNP
  130. #define IO_EXTENT 2
  131. static struct pnp_device_id snd_mpu401_pnpids[] = {
  132. { .id = "PNPb006" },
  133. { .id = "" }
  134. };
  135. MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids);
  136. static int snd_mpu401_pnp(int dev, struct pnp_dev *device,
  137. const struct pnp_device_id *id)
  138. {
  139. if (!pnp_port_valid(device, 0) ||
  140. pnp_port_flags(device, 0) & IORESOURCE_DISABLED) {
  141. snd_printk(KERN_ERR "no PnP port\n");
  142. return -ENODEV;
  143. }
  144. if (pnp_port_len(device, 0) < IO_EXTENT) {
  145. snd_printk(KERN_ERR "PnP port length is %llu, expected %d\n",
  146. (unsigned long long)pnp_port_len(device, 0),
  147. IO_EXTENT);
  148. return -ENODEV;
  149. }
  150. port[dev] = pnp_port_start(device, 0);
  151. if (!pnp_irq_valid(device, 0) ||
  152. pnp_irq_flags(device, 0) & IORESOURCE_DISABLED) {
  153. snd_printk(KERN_WARNING "no PnP irq, using polling\n");
  154. irq[dev] = -1;
  155. } else {
  156. irq[dev] = pnp_irq(device, 0);
  157. }
  158. return 0;
  159. }
  160. static int snd_mpu401_pnp_probe(struct pnp_dev *pnp_dev,
  161. const struct pnp_device_id *id)
  162. {
  163. static int dev;
  164. struct snd_card *card;
  165. int err;
  166. for ( ; dev < SNDRV_CARDS; ++dev) {
  167. if (!enable[dev] || !pnp[dev])
  168. continue;
  169. err = snd_mpu401_pnp(dev, pnp_dev, id);
  170. if (err < 0)
  171. return err;
  172. err = snd_mpu401_create(&pnp_dev->dev, dev, &card);
  173. if (err < 0)
  174. return err;
  175. if ((err = snd_card_register(card)) < 0) {
  176. snd_card_free(card);
  177. return err;
  178. }
  179. pnp_set_drvdata(pnp_dev, card);
  180. snd_mpu401_devices++;
  181. ++dev;
  182. return 0;
  183. }
  184. return -ENODEV;
  185. }
  186. static void snd_mpu401_pnp_remove(struct pnp_dev *dev)
  187. {
  188. struct snd_card *card = (struct snd_card *) pnp_get_drvdata(dev);
  189. snd_card_disconnect(card);
  190. snd_card_free_when_closed(card);
  191. }
  192. static struct pnp_driver snd_mpu401_pnp_driver = {
  193. .name = "mpu401",
  194. .id_table = snd_mpu401_pnpids,
  195. .probe = snd_mpu401_pnp_probe,
  196. .remove = snd_mpu401_pnp_remove,
  197. };
  198. #else
  199. static struct pnp_driver snd_mpu401_pnp_driver;
  200. #endif
  201. static void snd_mpu401_unregister_all(void)
  202. {
  203. int i;
  204. if (pnp_registered)
  205. pnp_unregister_driver(&snd_mpu401_pnp_driver);
  206. for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
  207. platform_device_unregister(platform_devices[i]);
  208. platform_driver_unregister(&snd_mpu401_driver);
  209. }
  210. static int __init alsa_card_mpu401_init(void)
  211. {
  212. int i, err;
  213. if ((err = platform_driver_register(&snd_mpu401_driver)) < 0)
  214. return err;
  215. for (i = 0; i < SNDRV_CARDS; i++) {
  216. struct platform_device *device;
  217. if (! enable[i])
  218. continue;
  219. #ifdef CONFIG_PNP
  220. if (pnp[i])
  221. continue;
  222. #endif
  223. device = platform_device_register_simple(SND_MPU401_DRIVER,
  224. i, NULL, 0);
  225. if (IS_ERR(device))
  226. continue;
  227. if (!platform_get_drvdata(device)) {
  228. platform_device_unregister(device);
  229. continue;
  230. }
  231. platform_devices[i] = device;
  232. snd_mpu401_devices++;
  233. }
  234. err = pnp_register_driver(&snd_mpu401_pnp_driver);
  235. if (!err)
  236. pnp_registered = 1;
  237. if (!snd_mpu401_devices) {
  238. #ifdef MODULE
  239. printk(KERN_ERR "MPU-401 device not found or device busy\n");
  240. #endif
  241. snd_mpu401_unregister_all();
  242. return -ENODEV;
  243. }
  244. return 0;
  245. }
  246. static void __exit alsa_card_mpu401_exit(void)
  247. {
  248. snd_mpu401_unregister_all();
  249. }
  250. module_init(alsa_card_mpu401_init)
  251. module_exit(alsa_card_mpu401_exit)