mpu401.c 6.3 KB

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