mpu401.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 <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@perex.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. static int uart_enter[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
  43. module_param_array(index, int, NULL, 0444);
  44. MODULE_PARM_DESC(index, "Index value for MPU-401 device.");
  45. module_param_array(id, charp, NULL, 0444);
  46. MODULE_PARM_DESC(id, "ID string for MPU-401 device.");
  47. module_param_array(enable, bool, NULL, 0444);
  48. MODULE_PARM_DESC(enable, "Enable MPU-401 device.");
  49. #ifdef CONFIG_PNP
  50. module_param_array(pnp, bool, NULL, 0444);
  51. MODULE_PARM_DESC(pnp, "PnP detection for MPU-401 device.");
  52. #endif
  53. module_param_array(port, long, NULL, 0444);
  54. MODULE_PARM_DESC(port, "Port # for MPU-401 device.");
  55. module_param_array(irq, int, NULL, 0444);
  56. MODULE_PARM_DESC(irq, "IRQ # for MPU-401 device.");
  57. module_param_array(uart_enter, bool, NULL, 0444);
  58. MODULE_PARM_DESC(uart_enter, "Issue UART_ENTER command at open.");
  59. static struct platform_device *platform_devices[SNDRV_CARDS];
  60. static int pnp_registered;
  61. static unsigned int snd_mpu401_devices;
  62. static int snd_mpu401_create(int dev, 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. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  70. if (card == NULL)
  71. return -ENOMEM;
  72. strcpy(card->driver, "MPU-401 UART");
  73. strcpy(card->shortname, card->driver);
  74. sprintf(card->longname, "%s at %#lx, ", card->shortname, port[dev]);
  75. if (irq[dev] >= 0) {
  76. sprintf(card->longname + strlen(card->longname), "irq %d", irq[dev]);
  77. } else {
  78. strcat(card->longname, "polled");
  79. }
  80. err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, port[dev], 0,
  81. irq[dev], irq[dev] >= 0 ? IRQF_DISABLED : 0,
  82. 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 __devinit 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(dev, &card);
  107. if (err < 0)
  108. return err;
  109. snd_card_set_dev(card, &devptr->dev);
  110. if ((err = snd_card_register(card)) < 0) {
  111. snd_card_free(card);
  112. return err;
  113. }
  114. platform_set_drvdata(devptr, card);
  115. return 0;
  116. }
  117. static int __devexit snd_mpu401_remove(struct platform_device *devptr)
  118. {
  119. snd_card_free(platform_get_drvdata(devptr));
  120. platform_set_drvdata(devptr, NULL);
  121. return 0;
  122. }
  123. #define SND_MPU401_DRIVER "snd_mpu401"
  124. static struct platform_driver snd_mpu401_driver = {
  125. .probe = snd_mpu401_probe,
  126. .remove = __devexit_p(snd_mpu401_remove),
  127. .driver = {
  128. .name = SND_MPU401_DRIVER
  129. },
  130. };
  131. #ifdef CONFIG_PNP
  132. #define IO_EXTENT 2
  133. static struct pnp_device_id snd_mpu401_pnpids[] = {
  134. { .id = "PNPb006" },
  135. { .id = "" }
  136. };
  137. MODULE_DEVICE_TABLE(pnp, snd_mpu401_pnpids);
  138. static int __devinit snd_mpu401_pnp(int dev, struct pnp_dev *device,
  139. const struct pnp_device_id *id)
  140. {
  141. if (!pnp_port_valid(device, 0) ||
  142. pnp_port_flags(device, 0) & IORESOURCE_DISABLED) {
  143. snd_printk(KERN_ERR "no PnP port\n");
  144. return -ENODEV;
  145. }
  146. if (pnp_port_len(device, 0) < IO_EXTENT) {
  147. snd_printk(KERN_ERR "PnP port length is %llu, expected %d\n",
  148. (unsigned long long)pnp_port_len(device, 0),
  149. IO_EXTENT);
  150. return -ENODEV;
  151. }
  152. port[dev] = pnp_port_start(device, 0);
  153. if (!pnp_irq_valid(device, 0) ||
  154. pnp_irq_flags(device, 0) & IORESOURCE_DISABLED) {
  155. snd_printk(KERN_WARNING "no PnP irq, using polling\n");
  156. irq[dev] = -1;
  157. } else {
  158. irq[dev] = pnp_irq(device, 0);
  159. }
  160. return 0;
  161. }
  162. static int __devinit snd_mpu401_pnp_probe(struct pnp_dev *pnp_dev,
  163. const struct pnp_device_id *id)
  164. {
  165. static int dev;
  166. struct snd_card *card;
  167. int err;
  168. for ( ; dev < SNDRV_CARDS; ++dev) {
  169. if (!enable[dev] || !pnp[dev])
  170. continue;
  171. err = snd_mpu401_pnp(dev, pnp_dev, id);
  172. if (err < 0)
  173. return err;
  174. err = snd_mpu401_create(dev, &card);
  175. if (err < 0)
  176. return err;
  177. if ((err = snd_card_register(card)) < 0) {
  178. snd_card_free(card);
  179. return err;
  180. }
  181. snd_card_set_dev(card, &pnp_dev->dev);
  182. pnp_set_drvdata(pnp_dev, card);
  183. snd_mpu401_devices++;
  184. ++dev;
  185. return 0;
  186. }
  187. return -ENODEV;
  188. }
  189. static void __devexit snd_mpu401_pnp_remove(struct pnp_dev *dev)
  190. {
  191. struct snd_card *card = (struct snd_card *) pnp_get_drvdata(dev);
  192. snd_card_disconnect(card);
  193. snd_card_free_when_closed(card);
  194. }
  195. static struct pnp_driver snd_mpu401_pnp_driver = {
  196. .name = "mpu401",
  197. .id_table = snd_mpu401_pnpids,
  198. .probe = snd_mpu401_pnp_probe,
  199. .remove = __devexit_p(snd_mpu401_pnp_remove),
  200. };
  201. #else
  202. static struct pnp_driver snd_mpu401_pnp_driver;
  203. #endif
  204. static void snd_mpu401_unregister_all(void)
  205. {
  206. int i;
  207. if (pnp_registered)
  208. pnp_unregister_driver(&snd_mpu401_pnp_driver);
  209. for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
  210. platform_device_unregister(platform_devices[i]);
  211. platform_driver_unregister(&snd_mpu401_driver);
  212. }
  213. static int __init alsa_card_mpu401_init(void)
  214. {
  215. int i, err;
  216. if ((err = platform_driver_register(&snd_mpu401_driver)) < 0)
  217. return err;
  218. for (i = 0; i < SNDRV_CARDS; i++) {
  219. struct platform_device *device;
  220. if (! enable[i])
  221. continue;
  222. #ifdef CONFIG_PNP
  223. if (pnp[i])
  224. continue;
  225. #endif
  226. device = platform_device_register_simple(SND_MPU401_DRIVER,
  227. i, NULL, 0);
  228. if (IS_ERR(device))
  229. continue;
  230. if (!platform_get_drvdata(device)) {
  231. platform_device_unregister(device);
  232. continue;
  233. }
  234. platform_devices[i] = device;
  235. snd_mpu401_devices++;
  236. }
  237. err = pnp_register_driver(&snd_mpu401_pnp_driver);
  238. if (!err)
  239. pnp_registered = 1;
  240. if (!snd_mpu401_devices) {
  241. #ifdef MODULE
  242. printk(KERN_ERR "MPU-401 device not found or device busy\n");
  243. #endif
  244. snd_mpu401_unregister_all();
  245. return -ENODEV;
  246. }
  247. return 0;
  248. }
  249. static void __exit alsa_card_mpu401_exit(void)
  250. {
  251. snd_mpu401_unregister_all();
  252. }
  253. module_init(alsa_card_mpu401_init)
  254. module_exit(alsa_card_mpu401_exit)