gmidi.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * gmidi.c -- USB MIDI Gadget Driver
  4. *
  5. * Copyright (C) 2006 Thumtronics Pty Ltd.
  6. * Developed for Thumtronics by Grey Innovation
  7. * Ben Williamson <ben.williamson@greyinnovation.com>
  8. *
  9. * This code is based in part on:
  10. *
  11. * Gadget Zero driver, Copyright (C) 2003-2004 David Brownell.
  12. * USB Audio driver, Copyright (C) 2002 by Takashi Iwai.
  13. * USB MIDI driver, Copyright (C) 2002-2005 Clemens Ladisch.
  14. *
  15. * Refer to the USB Device Class Definition for MIDI Devices:
  16. * http://www.usb.org/developers/devclass_docs/midi10.pdf
  17. */
  18. /* #define VERBOSE_DEBUG */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <sound/initval.h>
  22. #include <linux/usb/composite.h>
  23. #include <linux/usb/gadget.h>
  24. #include "u_midi.h"
  25. /*-------------------------------------------------------------------------*/
  26. MODULE_AUTHOR("Ben Williamson");
  27. MODULE_LICENSE("GPL v2");
  28. static const char longname[] = "MIDI Gadget";
  29. USB_GADGET_COMPOSITE_OPTIONS();
  30. static int index = SNDRV_DEFAULT_IDX1;
  31. module_param(index, int, S_IRUGO);
  32. MODULE_PARM_DESC(index, "Index value for the USB MIDI Gadget adapter.");
  33. static char *id = SNDRV_DEFAULT_STR1;
  34. module_param(id, charp, S_IRUGO);
  35. MODULE_PARM_DESC(id, "ID string for the USB MIDI Gadget adapter.");
  36. static unsigned int buflen = 512;
  37. module_param(buflen, uint, S_IRUGO);
  38. MODULE_PARM_DESC(buflen, "MIDI buffer length");
  39. static unsigned int qlen = 32;
  40. module_param(qlen, uint, S_IRUGO);
  41. MODULE_PARM_DESC(qlen, "USB read and write request queue length");
  42. static unsigned int in_ports = 1;
  43. module_param(in_ports, uint, S_IRUGO);
  44. MODULE_PARM_DESC(in_ports, "Number of MIDI input ports");
  45. static unsigned int out_ports = 1;
  46. module_param(out_ports, uint, S_IRUGO);
  47. MODULE_PARM_DESC(out_ports, "Number of MIDI output ports");
  48. /* Thanks to Grey Innovation for donating this product ID.
  49. *
  50. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  51. * Instead: allocate your own, using normal USB-IF procedures.
  52. */
  53. #define DRIVER_VENDOR_NUM 0x17b3 /* Grey Innovation */
  54. #define DRIVER_PRODUCT_NUM 0x0004 /* Linux-USB "MIDI Gadget" */
  55. /* string IDs are assigned dynamically */
  56. #define STRING_DESCRIPTION_IDX USB_GADGET_FIRST_AVAIL_IDX
  57. static struct usb_device_descriptor device_desc = {
  58. .bLength = USB_DT_DEVICE_SIZE,
  59. .bDescriptorType = USB_DT_DEVICE,
  60. /* .bcdUSB = DYNAMIC */
  61. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  62. .idVendor = cpu_to_le16(DRIVER_VENDOR_NUM),
  63. .idProduct = cpu_to_le16(DRIVER_PRODUCT_NUM),
  64. /* .iManufacturer = DYNAMIC */
  65. /* .iProduct = DYNAMIC */
  66. .bNumConfigurations = 1,
  67. };
  68. static struct usb_string strings_dev[] = {
  69. [USB_GADGET_MANUFACTURER_IDX].s = "Grey Innovation",
  70. [USB_GADGET_PRODUCT_IDX].s = "MIDI Gadget",
  71. [USB_GADGET_SERIAL_IDX].s = "",
  72. [STRING_DESCRIPTION_IDX].s = "MIDI",
  73. { } /* end of list */
  74. };
  75. static struct usb_gadget_strings stringtab_dev = {
  76. .language = 0x0409, /* en-us */
  77. .strings = strings_dev,
  78. };
  79. static struct usb_gadget_strings *dev_strings[] = {
  80. &stringtab_dev,
  81. NULL,
  82. };
  83. static struct usb_function_instance *fi_midi;
  84. static struct usb_function *f_midi;
  85. static int midi_unbind(struct usb_composite_dev *dev)
  86. {
  87. usb_put_function(f_midi);
  88. usb_put_function_instance(fi_midi);
  89. return 0;
  90. }
  91. static struct usb_configuration midi_config = {
  92. .label = "MIDI Gadget",
  93. .bConfigurationValue = 1,
  94. /* .iConfiguration = DYNAMIC */
  95. .bmAttributes = USB_CONFIG_ATT_ONE,
  96. .MaxPower = CONFIG_USB_GADGET_VBUS_DRAW,
  97. };
  98. static int midi_bind_config(struct usb_configuration *c)
  99. {
  100. int status;
  101. f_midi = usb_get_function(fi_midi);
  102. if (IS_ERR(f_midi))
  103. return PTR_ERR(f_midi);
  104. status = usb_add_function(c, f_midi);
  105. if (status < 0) {
  106. usb_put_function(f_midi);
  107. return status;
  108. }
  109. return 0;
  110. }
  111. static int midi_bind(struct usb_composite_dev *cdev)
  112. {
  113. struct f_midi_opts *midi_opts;
  114. int status;
  115. fi_midi = usb_get_function_instance("midi");
  116. if (IS_ERR(fi_midi))
  117. return PTR_ERR(fi_midi);
  118. midi_opts = container_of(fi_midi, struct f_midi_opts, func_inst);
  119. midi_opts->index = index;
  120. midi_opts->id = id;
  121. midi_opts->in_ports = in_ports;
  122. midi_opts->out_ports = out_ports;
  123. midi_opts->buflen = buflen;
  124. midi_opts->qlen = qlen;
  125. status = usb_string_ids_tab(cdev, strings_dev);
  126. if (status < 0)
  127. goto put;
  128. device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
  129. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  130. midi_config.iConfiguration = strings_dev[STRING_DESCRIPTION_IDX].id;
  131. status = usb_add_config(cdev, &midi_config, midi_bind_config);
  132. if (status < 0)
  133. goto put;
  134. usb_composite_overwrite_options(cdev, &coverwrite);
  135. pr_info("%s\n", longname);
  136. return 0;
  137. put:
  138. usb_put_function_instance(fi_midi);
  139. return status;
  140. }
  141. static struct usb_composite_driver midi_driver = {
  142. .name = (char *) longname,
  143. .dev = &device_desc,
  144. .strings = dev_strings,
  145. .max_speed = USB_SPEED_HIGH,
  146. .bind = midi_bind,
  147. .unbind = midi_unbind,
  148. };
  149. module_usb_composite_driver(midi_driver);