mpu401.c 7.4 KB

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