f_midi2.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * f_midi2.c -- USB MIDI 2.0 class function driver
  4. */
  5. #include <linux/device.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <sound/core.h>
  10. #include <sound/control.h>
  11. #include <sound/ump.h>
  12. #include <sound/ump_msg.h>
  13. #include <sound/ump_convert.h>
  14. #include <linux/usb/ch9.h>
  15. #include <linux/usb/func_utils.h>
  16. #include <linux/usb/gadget.h>
  17. #include <linux/usb/audio.h>
  18. #include <linux/usb/midi-v2.h>
  19. #include "u_midi2.h"
  20. struct f_midi2;
  21. struct f_midi2_ep;
  22. struct f_midi2_usb_ep;
  23. /* Context for each USB request */
  24. struct f_midi2_req_ctx {
  25. struct f_midi2_usb_ep *usb_ep; /* belonging USB EP */
  26. unsigned int index; /* array index: 0-31 */
  27. struct usb_request *req; /* assigned request */
  28. };
  29. /* Resources for a USB Endpoint */
  30. struct f_midi2_usb_ep {
  31. struct f_midi2 *card; /* belonging card */
  32. struct f_midi2_ep *ep; /* belonging UMP EP (optional) */
  33. struct usb_ep *usb_ep; /* assigned USB EP */
  34. void (*complete)(struct usb_ep *usb_ep, struct usb_request *req);
  35. unsigned long free_reqs; /* bitmap for unused requests */
  36. unsigned int num_reqs; /* number of allocated requests */
  37. struct f_midi2_req_ctx *reqs; /* request context array */
  38. };
  39. /* Resources for UMP Function Block (and USB Group Terminal Block) */
  40. struct f_midi2_block {
  41. struct f_midi2_block_info info; /* FB info, copied from configfs */
  42. struct snd_ump_block *fb; /* assigned FB */
  43. unsigned int gtb_id; /* assigned GTB id */
  44. unsigned int string_id; /* assigned string id */
  45. };
  46. /* Temporary buffer for altset 0 MIDI 1.0 handling */
  47. struct f_midi2_midi1_port {
  48. unsigned int pending; /* pending bytes on the input buffer */
  49. u8 buf[32]; /* raw MIDI 1.0 byte input */
  50. u8 state; /* running status */
  51. u8 data[2]; /* rendered USB MIDI 1.0 packet data */
  52. };
  53. /* MIDI 1.0 message states */
  54. enum {
  55. STATE_INITIAL = 0, /* pseudo state */
  56. STATE_1PARAM,
  57. STATE_2PARAM_1,
  58. STATE_2PARAM_2,
  59. STATE_SYSEX_0,
  60. STATE_SYSEX_1,
  61. STATE_SYSEX_2,
  62. STATE_REAL_TIME,
  63. STATE_FINISHED, /* pseudo state */
  64. };
  65. /* Resources for UMP Endpoint */
  66. struct f_midi2_ep {
  67. struct snd_ump_endpoint *ump; /* assigned UMP EP */
  68. struct f_midi2 *card; /* belonging MIDI 2.0 device */
  69. struct f_midi2_ep_info info; /* UMP EP info, copied from configfs */
  70. unsigned int num_blks; /* number of FBs */
  71. struct f_midi2_block blks[SNDRV_UMP_MAX_BLOCKS]; /* UMP FBs */
  72. struct f_midi2_usb_ep ep_in; /* USB MIDI EP-in */
  73. struct f_midi2_usb_ep ep_out; /* USB MIDI EP-out */
  74. u8 in_group_to_cable[SNDRV_UMP_MAX_GROUPS]; /* map to cable; 1-based! */
  75. };
  76. /* indices for USB strings */
  77. enum {
  78. STR_IFACE = 0,
  79. STR_GTB1 = 1,
  80. };
  81. /* 1-based GTB id to string id */
  82. #define gtb_to_str_id(id) (STR_GTB1 + (id) - 1)
  83. /* mapping from MIDI 1.0 cable to UMP group */
  84. struct midi1_cable_mapping {
  85. struct f_midi2_ep *ep;
  86. unsigned char block;
  87. unsigned char group;
  88. };
  89. /* operation mode */
  90. enum {
  91. MIDI_OP_MODE_UNSET, /* no altset set yet */
  92. MIDI_OP_MODE_MIDI1, /* MIDI 1.0 (altset 0) is used */
  93. MIDI_OP_MODE_MIDI2, /* MIDI 2.0 (altset 1) is used */
  94. };
  95. /* Resources for MIDI 2.0 Device */
  96. struct f_midi2 {
  97. struct usb_function func;
  98. struct usb_gadget *gadget;
  99. struct snd_card *card;
  100. /* MIDI 1.0 in/out USB EPs */
  101. struct f_midi2_usb_ep midi1_ep_in;
  102. struct f_midi2_usb_ep midi1_ep_out;
  103. /* number of MIDI 1.0 I/O cables */
  104. unsigned int num_midi1_in;
  105. unsigned int num_midi1_out;
  106. /* conversion for MIDI 1.0 EP-in */
  107. struct f_midi2_midi1_port midi1_port[MAX_CABLES];
  108. /* conversion for MIDI 1.0 EP-out */
  109. struct ump_cvt_to_ump midi1_ump_cvt;
  110. /* mapping between cables and UMP groups */
  111. struct midi1_cable_mapping in_cable_mapping[MAX_CABLES];
  112. struct midi1_cable_mapping out_cable_mapping[MAX_CABLES];
  113. int midi_if; /* USB MIDI interface number */
  114. int operation_mode; /* current operation mode */
  115. spinlock_t queue_lock;
  116. struct f_midi2_card_info info; /* card info, copied from configfs */
  117. unsigned int num_eps;
  118. struct f_midi2_ep midi2_eps[MAX_UMP_EPS];
  119. unsigned int total_blocks; /* total number of blocks of all EPs */
  120. struct usb_string *string_defs;
  121. struct usb_string *strings;
  122. };
  123. #define func_to_midi2(f) container_of(f, struct f_midi2, func)
  124. /* convert from MIDI protocol number (1 or 2) to SNDRV_UMP_EP_INFO_PROTO_* */
  125. #define to_ump_protocol(v) (((v) & 3) << 8)
  126. /* get EP name string */
  127. static const char *ump_ep_name(const struct f_midi2_ep *ep)
  128. {
  129. return ep->info.ep_name ? ep->info.ep_name : "MIDI 2.0 Gadget";
  130. }
  131. /* get EP product ID string */
  132. static const char *ump_product_id(const struct f_midi2_ep *ep)
  133. {
  134. return ep->info.product_id ? ep->info.product_id : "Unique Product ID";
  135. }
  136. /* get FB name string */
  137. static const char *ump_fb_name(const struct f_midi2_block_info *info)
  138. {
  139. return info->name ? info->name : "MIDI 2.0 Gadget I/O";
  140. }
  141. /*
  142. * USB Descriptor Definitions
  143. */
  144. /* GTB header descriptor */
  145. static struct usb_ms20_gr_trm_block_header_descriptor gtb_header_desc = {
  146. .bLength = sizeof(gtb_header_desc),
  147. .bDescriptorType = USB_DT_CS_GR_TRM_BLOCK,
  148. .bDescriptorSubtype = USB_MS_GR_TRM_BLOCK_HEADER,
  149. .wTotalLength = __cpu_to_le16(0x12), // to be filled
  150. };
  151. /* GTB descriptor template: most items are replaced dynamically */
  152. static struct usb_ms20_gr_trm_block_descriptor gtb_desc = {
  153. .bLength = sizeof(gtb_desc),
  154. .bDescriptorType = USB_DT_CS_GR_TRM_BLOCK,
  155. .bDescriptorSubtype = USB_MS_GR_TRM_BLOCK,
  156. .bGrpTrmBlkID = 0x01,
  157. .bGrpTrmBlkType = USB_MS_GR_TRM_BLOCK_TYPE_BIDIRECTIONAL,
  158. .nGroupTrm = 0x00,
  159. .nNumGroupTrm = 1,
  160. .iBlockItem = 0,
  161. .bMIDIProtocol = USB_MS_MIDI_PROTO_1_0_64,
  162. .wMaxInputBandwidth = 0,
  163. .wMaxOutputBandwidth = 0,
  164. };
  165. DECLARE_USB_MIDI_OUT_JACK_DESCRIPTOR(1);
  166. DECLARE_USB_MS_ENDPOINT_DESCRIPTOR(16);
  167. DECLARE_UAC_AC_HEADER_DESCRIPTOR(1);
  168. DECLARE_USB_MS20_ENDPOINT_DESCRIPTOR(32);
  169. #define EP_MAX_PACKET_INT 8
  170. /* Audio Control Interface */
  171. static struct usb_interface_descriptor midi2_audio_if_desc = {
  172. .bLength = USB_DT_INTERFACE_SIZE,
  173. .bDescriptorType = USB_DT_INTERFACE,
  174. .bInterfaceNumber = 0, // to be filled
  175. .bNumEndpoints = 0,
  176. .bInterfaceClass = USB_CLASS_AUDIO,
  177. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL,
  178. .bInterfaceProtocol = 0,
  179. .iInterface = 0,
  180. };
  181. static struct uac1_ac_header_descriptor_1 midi2_audio_class_desc = {
  182. .bLength = 0x09,
  183. .bDescriptorType = USB_DT_CS_INTERFACE,
  184. .bDescriptorSubtype = 0x01,
  185. .bcdADC = __cpu_to_le16(0x0100),
  186. .wTotalLength = __cpu_to_le16(0x0009),
  187. .bInCollection = 0x01,
  188. .baInterfaceNr = { 0x01 }, // to be filled
  189. };
  190. /* MIDI 1.0 Streaming Interface (altset 0) */
  191. static struct usb_interface_descriptor midi2_midi1_if_desc = {
  192. .bLength = USB_DT_INTERFACE_SIZE,
  193. .bDescriptorType = USB_DT_INTERFACE,
  194. .bInterfaceNumber = 0, // to be filled
  195. .bAlternateSetting = 0,
  196. .bNumEndpoints = 2, // to be filled
  197. .bInterfaceClass = USB_CLASS_AUDIO,
  198. .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
  199. .bInterfaceProtocol = 0,
  200. .iInterface = 0, // to be filled
  201. };
  202. static struct usb_ms_header_descriptor midi2_midi1_class_desc = {
  203. .bLength = 0x07,
  204. .bDescriptorType = USB_DT_CS_INTERFACE,
  205. .bDescriptorSubtype = USB_MS_HEADER,
  206. .bcdMSC = __cpu_to_le16(0x0100),
  207. .wTotalLength = __cpu_to_le16(0x41), // to be calculated
  208. };
  209. /* MIDI 1.0 EP OUT */
  210. static struct usb_endpoint_descriptor midi2_midi1_ep_out_desc = {
  211. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  212. .bDescriptorType = USB_DT_ENDPOINT,
  213. .bEndpointAddress = USB_DIR_OUT | 0, // set up dynamically
  214. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  215. };
  216. static struct usb_ss_ep_comp_descriptor midi2_midi1_ep_out_ss_comp_desc = {
  217. .bLength = sizeof(midi2_midi1_ep_out_ss_comp_desc),
  218. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  219. };
  220. static struct usb_ms_endpoint_descriptor_16 midi2_midi1_ep_out_class_desc = {
  221. .bLength = 0x05, // to be filled
  222. .bDescriptorType = USB_DT_CS_ENDPOINT,
  223. .bDescriptorSubtype = USB_MS_GENERAL,
  224. .bNumEmbMIDIJack = 1,
  225. .baAssocJackID = { 0x01 },
  226. };
  227. /* MIDI 1.0 EP IN */
  228. static struct usb_endpoint_descriptor midi2_midi1_ep_in_desc = {
  229. .bLength = USB_DT_ENDPOINT_AUDIO_SIZE,
  230. .bDescriptorType = USB_DT_ENDPOINT,
  231. .bEndpointAddress = USB_DIR_IN | 0, // set up dynamically
  232. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  233. };
  234. static struct usb_ss_ep_comp_descriptor midi2_midi1_ep_in_ss_comp_desc = {
  235. .bLength = sizeof(midi2_midi1_ep_in_ss_comp_desc),
  236. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  237. };
  238. static struct usb_ms_endpoint_descriptor_16 midi2_midi1_ep_in_class_desc = {
  239. .bLength = 0x05, // to be filled
  240. .bDescriptorType = USB_DT_CS_ENDPOINT,
  241. .bDescriptorSubtype = USB_MS_GENERAL,
  242. .bNumEmbMIDIJack = 1,
  243. .baAssocJackID = { 0x03 },
  244. };
  245. /* MIDI 2.0 Streaming Interface (altset 1) */
  246. static struct usb_interface_descriptor midi2_midi2_if_desc = {
  247. .bLength = USB_DT_INTERFACE_SIZE,
  248. .bDescriptorType = USB_DT_INTERFACE,
  249. .bInterfaceNumber = 0, // to be filled
  250. .bAlternateSetting = 1,
  251. .bNumEndpoints = 2, // to be filled
  252. .bInterfaceClass = USB_CLASS_AUDIO,
  253. .bInterfaceSubClass = USB_SUBCLASS_MIDISTREAMING,
  254. .bInterfaceProtocol = 0,
  255. .iInterface = 0, // to be filled
  256. };
  257. static struct usb_ms_header_descriptor midi2_midi2_class_desc = {
  258. .bLength = 0x07,
  259. .bDescriptorType = USB_DT_CS_INTERFACE,
  260. .bDescriptorSubtype = USB_MS_HEADER,
  261. .bcdMSC = __cpu_to_le16(0x0200),
  262. .wTotalLength = __cpu_to_le16(0x07),
  263. };
  264. /* MIDI 2.0 EP OUT */
  265. static struct usb_endpoint_descriptor midi2_midi2_ep_out_desc[MAX_UMP_EPS];
  266. static struct usb_ss_ep_comp_descriptor midi2_midi2_ep_out_ss_comp_desc = {
  267. .bLength = sizeof(midi2_midi1_ep_out_ss_comp_desc),
  268. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  269. };
  270. static struct usb_ms20_endpoint_descriptor_32 midi2_midi2_ep_out_class_desc[MAX_UMP_EPS];
  271. /* MIDI 2.0 EP IN */
  272. static struct usb_endpoint_descriptor midi2_midi2_ep_in_desc[MAX_UMP_EPS];
  273. static struct usb_ss_ep_comp_descriptor midi2_midi2_ep_in_ss_comp_desc = {
  274. .bLength = sizeof(midi2_midi2_ep_in_ss_comp_desc),
  275. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  276. };
  277. static struct usb_ms20_endpoint_descriptor_32 midi2_midi2_ep_in_class_desc[MAX_UMP_EPS];
  278. /* Arrays of descriptors to be created */
  279. static void *midi2_audio_descs[] = {
  280. &midi2_audio_if_desc,
  281. &midi2_audio_class_desc,
  282. NULL
  283. };
  284. static void *midi2_midi1_descs[] = {
  285. &midi2_midi1_if_desc,
  286. &midi2_midi1_class_desc,
  287. NULL
  288. };
  289. static void *midi2_midi1_ep_out_descs[] = {
  290. &midi2_midi1_ep_out_desc,
  291. &midi2_midi1_ep_out_class_desc,
  292. NULL
  293. };
  294. static void *midi2_midi1_ep_in_descs[] = {
  295. &midi2_midi1_ep_in_desc,
  296. &midi2_midi1_ep_in_class_desc,
  297. NULL
  298. };
  299. static void *midi2_midi1_ep_out_ss_descs[] = {
  300. &midi2_midi1_ep_out_desc,
  301. &midi2_midi1_ep_out_ss_comp_desc,
  302. &midi2_midi1_ep_out_class_desc,
  303. NULL
  304. };
  305. static void *midi2_midi1_ep_in_ss_descs[] = {
  306. &midi2_midi1_ep_in_desc,
  307. &midi2_midi1_ep_in_ss_comp_desc,
  308. &midi2_midi1_ep_in_class_desc,
  309. NULL
  310. };
  311. static void *midi2_midi2_descs[] = {
  312. &midi2_midi2_if_desc,
  313. &midi2_midi2_class_desc,
  314. NULL
  315. };
  316. /*
  317. * USB request handling
  318. */
  319. /* get an empty request for the given EP */
  320. static struct usb_request *get_empty_request(struct f_midi2_usb_ep *usb_ep)
  321. {
  322. struct usb_request *req = NULL;
  323. unsigned long flags;
  324. int index;
  325. spin_lock_irqsave(&usb_ep->card->queue_lock, flags);
  326. if (!usb_ep->free_reqs)
  327. goto unlock;
  328. index = find_first_bit(&usb_ep->free_reqs, usb_ep->num_reqs);
  329. if (index >= usb_ep->num_reqs)
  330. goto unlock;
  331. req = usb_ep->reqs[index].req;
  332. if (!req)
  333. goto unlock;
  334. clear_bit(index, &usb_ep->free_reqs);
  335. req->length = 0;
  336. unlock:
  337. spin_unlock_irqrestore(&usb_ep->card->queue_lock, flags);
  338. return req;
  339. }
  340. /* put the empty request back */
  341. static void put_empty_request(struct usb_request *req)
  342. {
  343. struct f_midi2_req_ctx *ctx = req->context;
  344. unsigned long flags;
  345. spin_lock_irqsave(&ctx->usb_ep->card->queue_lock, flags);
  346. set_bit(ctx->index, &ctx->usb_ep->free_reqs);
  347. spin_unlock_irqrestore(&ctx->usb_ep->card->queue_lock, flags);
  348. }
  349. /*
  350. * UMP v1.1 Stream message handling
  351. */
  352. /* queue a request to UMP EP; request is either queued or freed after this */
  353. static int queue_request_ep_raw(struct usb_request *req)
  354. {
  355. struct f_midi2_req_ctx *ctx = req->context;
  356. int err;
  357. req->complete = ctx->usb_ep->complete;
  358. err = usb_ep_queue(ctx->usb_ep->usb_ep, req, GFP_ATOMIC);
  359. if (err) {
  360. put_empty_request(req);
  361. return err;
  362. }
  363. return 0;
  364. }
  365. /* queue a request with endianness conversion */
  366. static int queue_request_ep_in(struct usb_request *req)
  367. {
  368. /* UMP packets have to be converted to little-endian */
  369. cpu_to_le32_array((u32 *)req->buf, req->length >> 2);
  370. return queue_request_ep_raw(req);
  371. }
  372. /* reply a UMP packet via EP-in */
  373. static int reply_ep_in(struct f_midi2_ep *ep, const void *buf, int len)
  374. {
  375. struct f_midi2_usb_ep *usb_ep = &ep->ep_in;
  376. struct usb_request *req;
  377. req = get_empty_request(usb_ep);
  378. if (!req)
  379. return -ENOSPC;
  380. req->length = len;
  381. memcpy(req->buf, buf, len);
  382. return queue_request_ep_in(req);
  383. }
  384. /* reply a UMP stream EP info */
  385. static void reply_ump_stream_ep_info(struct f_midi2_ep *ep)
  386. {
  387. struct snd_ump_stream_msg_ep_info rep = {
  388. .type = UMP_MSG_TYPE_STREAM,
  389. .status = UMP_STREAM_MSG_STATUS_EP_INFO,
  390. .ump_version_major = 0x01,
  391. .ump_version_minor = 0x01,
  392. .num_function_blocks = ep->num_blks,
  393. .static_function_block = !!ep->card->info.static_block,
  394. .protocol = (UMP_STREAM_MSG_EP_INFO_CAP_MIDI1 |
  395. UMP_STREAM_MSG_EP_INFO_CAP_MIDI2) >> 8,
  396. };
  397. reply_ep_in(ep, &rep, sizeof(rep));
  398. }
  399. /* reply a UMP EP device info */
  400. static void reply_ump_stream_ep_device(struct f_midi2_ep *ep)
  401. {
  402. struct snd_ump_stream_msg_device_info rep = {
  403. .type = UMP_MSG_TYPE_STREAM,
  404. .status = UMP_STREAM_MSG_STATUS_DEVICE_INFO,
  405. .manufacture_id = ep->info.manufacturer,
  406. .family_lsb = ep->info.family & 0xff,
  407. .family_msb = (ep->info.family >> 8) & 0xff,
  408. .model_lsb = ep->info.model & 0xff,
  409. .model_msb = (ep->info.model >> 8) & 0xff,
  410. .sw_revision = ep->info.sw_revision,
  411. };
  412. reply_ep_in(ep, &rep, sizeof(rep));
  413. }
  414. #define UMP_STREAM_PKT_BYTES 16 /* UMP stream packet size = 16 bytes*/
  415. #define UMP_STREAM_EP_STR_OFF 2 /* offset of name string for EP info */
  416. #define UMP_STREAM_FB_STR_OFF 3 /* offset of name string for FB info */
  417. /* Helper to replay a string */
  418. static void reply_ump_stream_string(struct f_midi2_ep *ep, const u8 *name,
  419. unsigned int type, unsigned int extra,
  420. unsigned int start_ofs)
  421. {
  422. struct f_midi2_usb_ep *usb_ep = &ep->ep_in;
  423. struct f_midi2 *midi2 = ep->card;
  424. struct usb_request *req;
  425. unsigned int pos;
  426. u32 *buf;
  427. if (!*name)
  428. return;
  429. req = get_empty_request(usb_ep);
  430. if (!req)
  431. return;
  432. buf = (u32 *)req->buf;
  433. pos = start_ofs;
  434. for (;;) {
  435. if (pos == start_ofs) {
  436. memset(buf, 0, UMP_STREAM_PKT_BYTES);
  437. buf[0] = ump_stream_compose(type, 0) | extra;
  438. }
  439. buf[pos / 4] |= *name++ << ((3 - (pos % 4)) * 8);
  440. if (!*name) {
  441. if (req->length)
  442. buf[0] |= UMP_STREAM_MSG_FORMAT_END << 26;
  443. req->length += UMP_STREAM_PKT_BYTES;
  444. break;
  445. }
  446. if (++pos == UMP_STREAM_PKT_BYTES) {
  447. if (!req->length)
  448. buf[0] |= UMP_STREAM_MSG_FORMAT_START << 26;
  449. else
  450. buf[0] |= UMP_STREAM_MSG_FORMAT_CONTINUE << 26;
  451. req->length += UMP_STREAM_PKT_BYTES;
  452. if (midi2->info.req_buf_size - req->length < UMP_STREAM_PKT_BYTES)
  453. break;
  454. buf += 4;
  455. pos = start_ofs;
  456. }
  457. }
  458. if (req->length)
  459. queue_request_ep_in(req);
  460. else
  461. put_empty_request(req);
  462. }
  463. /* Reply a UMP EP name string */
  464. static void reply_ump_stream_ep_name(struct f_midi2_ep *ep)
  465. {
  466. reply_ump_stream_string(ep, ump_ep_name(ep),
  467. UMP_STREAM_MSG_STATUS_EP_NAME, 0,
  468. UMP_STREAM_EP_STR_OFF);
  469. }
  470. /* Reply a UMP EP product ID string */
  471. static void reply_ump_stream_ep_pid(struct f_midi2_ep *ep)
  472. {
  473. reply_ump_stream_string(ep, ump_product_id(ep),
  474. UMP_STREAM_MSG_STATUS_PRODUCT_ID, 0,
  475. UMP_STREAM_EP_STR_OFF);
  476. }
  477. /* Reply a UMP EP stream config */
  478. static void reply_ump_stream_ep_config(struct f_midi2_ep *ep)
  479. {
  480. struct snd_ump_stream_msg_stream_cfg rep = {
  481. .type = UMP_MSG_TYPE_STREAM,
  482. .status = UMP_STREAM_MSG_STATUS_STREAM_CFG,
  483. };
  484. if (ep->info.protocol == 2)
  485. rep.protocol = UMP_STREAM_MSG_EP_INFO_CAP_MIDI2 >> 8;
  486. else
  487. rep.protocol = UMP_STREAM_MSG_EP_INFO_CAP_MIDI1 >> 8;
  488. reply_ep_in(ep, &rep, sizeof(rep));
  489. }
  490. /* Reply a UMP FB info */
  491. static void reply_ump_stream_fb_info(struct f_midi2_ep *ep, int blk)
  492. {
  493. struct f_midi2_block_info *b = &ep->blks[blk].info;
  494. struct snd_ump_stream_msg_fb_info rep = {
  495. .type = UMP_MSG_TYPE_STREAM,
  496. .status = UMP_STREAM_MSG_STATUS_FB_INFO,
  497. .active = !!b->active,
  498. .function_block_id = blk,
  499. .ui_hint = b->ui_hint,
  500. .midi_10 = b->is_midi1,
  501. .direction = b->direction,
  502. .first_group = b->first_group,
  503. .num_groups = b->num_groups,
  504. .midi_ci_version = b->midi_ci_version,
  505. .sysex8_streams = b->sysex8_streams,
  506. };
  507. reply_ep_in(ep, &rep, sizeof(rep));
  508. }
  509. /* Reply a FB name string */
  510. static void reply_ump_stream_fb_name(struct f_midi2_ep *ep, unsigned int blk)
  511. {
  512. reply_ump_stream_string(ep, ump_fb_name(&ep->blks[blk].info),
  513. UMP_STREAM_MSG_STATUS_FB_NAME, blk << 8,
  514. UMP_STREAM_FB_STR_OFF);
  515. }
  516. /* Process a UMP Stream message */
  517. static void process_ump_stream_msg(struct f_midi2_ep *ep, const u32 *data)
  518. {
  519. struct f_midi2 *midi2 = ep->card;
  520. unsigned int format, status, blk;
  521. format = ump_stream_message_format(*data);
  522. status = ump_stream_message_status(*data);
  523. switch (status) {
  524. case UMP_STREAM_MSG_STATUS_EP_DISCOVERY:
  525. if (format)
  526. return; // invalid
  527. if (data[1] & UMP_STREAM_MSG_REQUEST_EP_INFO)
  528. reply_ump_stream_ep_info(ep);
  529. if (data[1] & UMP_STREAM_MSG_REQUEST_DEVICE_INFO)
  530. reply_ump_stream_ep_device(ep);
  531. if (data[1] & UMP_STREAM_MSG_REQUEST_EP_NAME)
  532. reply_ump_stream_ep_name(ep);
  533. if (data[1] & UMP_STREAM_MSG_REQUEST_PRODUCT_ID)
  534. reply_ump_stream_ep_pid(ep);
  535. if (data[1] & UMP_STREAM_MSG_REQUEST_STREAM_CFG)
  536. reply_ump_stream_ep_config(ep);
  537. return;
  538. case UMP_STREAM_MSG_STATUS_STREAM_CFG_REQUEST:
  539. if (*data & UMP_STREAM_MSG_EP_INFO_CAP_MIDI2) {
  540. ep->info.protocol = 2;
  541. DBG(midi2, "Switching Protocol to MIDI2\n");
  542. } else {
  543. ep->info.protocol = 1;
  544. DBG(midi2, "Switching Protocol to MIDI1\n");
  545. }
  546. snd_ump_switch_protocol(ep->ump, to_ump_protocol(ep->info.protocol));
  547. reply_ump_stream_ep_config(ep);
  548. return;
  549. case UMP_STREAM_MSG_STATUS_FB_DISCOVERY:
  550. if (format)
  551. return; // invalid
  552. blk = (*data >> 8) & 0xff;
  553. if (blk == 0xff) {
  554. /* inquiry for all blocks */
  555. for (blk = 0; blk < ep->num_blks; blk++) {
  556. if (*data & UMP_STREAM_MSG_REQUEST_FB_INFO)
  557. reply_ump_stream_fb_info(ep, blk);
  558. if (*data & UMP_STREAM_MSG_REQUEST_FB_NAME)
  559. reply_ump_stream_fb_name(ep, blk);
  560. }
  561. } else if (blk < ep->num_blks) {
  562. /* only the specified block */
  563. if (*data & UMP_STREAM_MSG_REQUEST_FB_INFO)
  564. reply_ump_stream_fb_info(ep, blk);
  565. if (*data & UMP_STREAM_MSG_REQUEST_FB_NAME)
  566. reply_ump_stream_fb_name(ep, blk);
  567. }
  568. return;
  569. }
  570. }
  571. /* Process UMP messages included in a USB request */
  572. static void process_ump(struct f_midi2_ep *ep, const struct usb_request *req)
  573. {
  574. const u32 *data = (u32 *)req->buf;
  575. int len = req->actual >> 2;
  576. const u32 *in_buf = ep->ump->input_buf;
  577. for (; len > 0; len--, data++) {
  578. if (snd_ump_receive_ump_val(ep->ump, *data) <= 0)
  579. continue;
  580. if (ump_message_type(*in_buf) == UMP_MSG_TYPE_STREAM)
  581. process_ump_stream_msg(ep, in_buf);
  582. }
  583. }
  584. /*
  585. * MIDI 2.0 UMP USB request handling
  586. */
  587. /* complete handler for UMP EP-out requests */
  588. static void f_midi2_ep_out_complete(struct usb_ep *usb_ep,
  589. struct usb_request *req)
  590. {
  591. struct f_midi2_req_ctx *ctx = req->context;
  592. struct f_midi2_ep *ep = ctx->usb_ep->ep;
  593. struct f_midi2 *midi2 = ep->card;
  594. int status = req->status;
  595. if (status) {
  596. DBG(midi2, "%s complete error %d: %d/%d\n",
  597. usb_ep->name, status, req->actual, req->length);
  598. goto error;
  599. }
  600. /* convert to UMP packet in native endianness */
  601. le32_to_cpu_array((u32 *)req->buf, req->actual >> 2);
  602. if (midi2->info.process_ump)
  603. process_ump(ep, req);
  604. snd_ump_receive(ep->ump, req->buf, req->actual & ~3);
  605. if (midi2->operation_mode != MIDI_OP_MODE_MIDI2)
  606. goto error;
  607. if (queue_request_ep_raw(req))
  608. goto error;
  609. return;
  610. error:
  611. put_empty_request(req);
  612. }
  613. /* Transmit UMP packets received from user-space to the gadget */
  614. static void process_ump_transmit(struct f_midi2_ep *ep)
  615. {
  616. struct f_midi2_usb_ep *usb_ep = &ep->ep_in;
  617. struct f_midi2 *midi2 = ep->card;
  618. struct usb_request *req;
  619. int len;
  620. if (!usb_ep->usb_ep->enabled)
  621. return;
  622. for (;;) {
  623. req = get_empty_request(usb_ep);
  624. if (!req)
  625. break;
  626. len = snd_ump_transmit(ep->ump, (u32 *)req->buf,
  627. midi2->info.req_buf_size);
  628. if (len <= 0) {
  629. put_empty_request(req);
  630. break;
  631. }
  632. req->length = len;
  633. if (queue_request_ep_in(req) < 0)
  634. break;
  635. }
  636. }
  637. /* Complete handler for UMP EP-in requests */
  638. static void f_midi2_ep_in_complete(struct usb_ep *usb_ep,
  639. struct usb_request *req)
  640. {
  641. struct f_midi2_req_ctx *ctx = req->context;
  642. struct f_midi2_ep *ep = ctx->usb_ep->ep;
  643. struct f_midi2 *midi2 = ep->card;
  644. int status = req->status;
  645. put_empty_request(req);
  646. if (status) {
  647. DBG(midi2, "%s complete error %d: %d/%d\n",
  648. usb_ep->name, status, req->actual, req->length);
  649. return;
  650. }
  651. process_ump_transmit(ep);
  652. }
  653. /*
  654. * MIDI1 (altset 0) USB request handling
  655. */
  656. /* process one MIDI byte -- copied from f_midi.c
  657. *
  658. * fill the packet or request if needed
  659. * returns true if the request became empty (queued)
  660. */
  661. static bool process_midi1_byte(struct f_midi2 *midi2, u8 cable, u8 b,
  662. struct usb_request **req_p)
  663. {
  664. struct f_midi2_midi1_port *port = &midi2->midi1_port[cable];
  665. u8 p[4] = { cable << 4, 0, 0, 0 };
  666. int next_state = STATE_INITIAL;
  667. struct usb_request *req = *req_p;
  668. switch (b) {
  669. case 0xf8 ... 0xff:
  670. /* System Real-Time Messages */
  671. p[0] |= 0x0f;
  672. p[1] = b;
  673. next_state = port->state;
  674. port->state = STATE_REAL_TIME;
  675. break;
  676. case 0xf7:
  677. /* End of SysEx */
  678. switch (port->state) {
  679. case STATE_SYSEX_0:
  680. p[0] |= 0x05;
  681. p[1] = 0xf7;
  682. next_state = STATE_FINISHED;
  683. break;
  684. case STATE_SYSEX_1:
  685. p[0] |= 0x06;
  686. p[1] = port->data[0];
  687. p[2] = 0xf7;
  688. next_state = STATE_FINISHED;
  689. break;
  690. case STATE_SYSEX_2:
  691. p[0] |= 0x07;
  692. p[1] = port->data[0];
  693. p[2] = port->data[1];
  694. p[3] = 0xf7;
  695. next_state = STATE_FINISHED;
  696. break;
  697. default:
  698. /* Ignore byte */
  699. next_state = port->state;
  700. port->state = STATE_INITIAL;
  701. }
  702. break;
  703. case 0xf0 ... 0xf6:
  704. /* System Common Messages */
  705. port->data[0] = port->data[1] = 0;
  706. port->state = STATE_INITIAL;
  707. switch (b) {
  708. case 0xf0:
  709. port->data[0] = b;
  710. port->data[1] = 0;
  711. next_state = STATE_SYSEX_1;
  712. break;
  713. case 0xf1:
  714. case 0xf3:
  715. port->data[0] = b;
  716. next_state = STATE_1PARAM;
  717. break;
  718. case 0xf2:
  719. port->data[0] = b;
  720. next_state = STATE_2PARAM_1;
  721. break;
  722. case 0xf4:
  723. case 0xf5:
  724. next_state = STATE_INITIAL;
  725. break;
  726. case 0xf6:
  727. p[0] |= 0x05;
  728. p[1] = 0xf6;
  729. next_state = STATE_FINISHED;
  730. break;
  731. }
  732. break;
  733. case 0x80 ... 0xef:
  734. /*
  735. * Channel Voice Messages, Channel Mode Messages
  736. * and Control Change Messages.
  737. */
  738. port->data[0] = b;
  739. port->data[1] = 0;
  740. port->state = STATE_INITIAL;
  741. if (b >= 0xc0 && b <= 0xdf)
  742. next_state = STATE_1PARAM;
  743. else
  744. next_state = STATE_2PARAM_1;
  745. break;
  746. case 0x00 ... 0x7f:
  747. /* Message parameters */
  748. switch (port->state) {
  749. case STATE_1PARAM:
  750. if (port->data[0] < 0xf0)
  751. p[0] |= port->data[0] >> 4;
  752. else
  753. p[0] |= 0x02;
  754. p[1] = port->data[0];
  755. p[2] = b;
  756. /* This is to allow Running State Messages */
  757. next_state = STATE_1PARAM;
  758. break;
  759. case STATE_2PARAM_1:
  760. port->data[1] = b;
  761. next_state = STATE_2PARAM_2;
  762. break;
  763. case STATE_2PARAM_2:
  764. if (port->data[0] < 0xf0)
  765. p[0] |= port->data[0] >> 4;
  766. else
  767. p[0] |= 0x03;
  768. p[1] = port->data[0];
  769. p[2] = port->data[1];
  770. p[3] = b;
  771. /* This is to allow Running State Messages */
  772. next_state = STATE_2PARAM_1;
  773. break;
  774. case STATE_SYSEX_0:
  775. port->data[0] = b;
  776. next_state = STATE_SYSEX_1;
  777. break;
  778. case STATE_SYSEX_1:
  779. port->data[1] = b;
  780. next_state = STATE_SYSEX_2;
  781. break;
  782. case STATE_SYSEX_2:
  783. p[0] |= 0x04;
  784. p[1] = port->data[0];
  785. p[2] = port->data[1];
  786. p[3] = b;
  787. next_state = STATE_SYSEX_0;
  788. break;
  789. }
  790. break;
  791. }
  792. /* States where we have to write into the USB request */
  793. if (next_state == STATE_FINISHED ||
  794. port->state == STATE_SYSEX_2 ||
  795. port->state == STATE_1PARAM ||
  796. port->state == STATE_2PARAM_2 ||
  797. port->state == STATE_REAL_TIME) {
  798. memcpy(req->buf + req->length, p, sizeof(p));
  799. req->length += sizeof(p);
  800. if (next_state == STATE_FINISHED) {
  801. next_state = STATE_INITIAL;
  802. port->data[0] = port->data[1] = 0;
  803. }
  804. if (midi2->info.req_buf_size - req->length <= 4) {
  805. queue_request_ep_raw(req);
  806. *req_p = NULL;
  807. return true;
  808. }
  809. }
  810. port->state = next_state;
  811. return false;
  812. }
  813. /* process all pending MIDI bytes in the internal buffer;
  814. * returns true if the request gets empty
  815. * returns false if all have been processed
  816. */
  817. static bool process_midi1_pending_buf(struct f_midi2 *midi2,
  818. struct usb_request **req_p)
  819. {
  820. unsigned int cable, c;
  821. for (cable = 0; cable < midi2->num_midi1_in; cable++) {
  822. struct f_midi2_midi1_port *port = &midi2->midi1_port[cable];
  823. if (!port->pending)
  824. continue;
  825. for (c = 0; c < port->pending; c++) {
  826. if (process_midi1_byte(midi2, cable, port->buf[c],
  827. req_p)) {
  828. port->pending -= c;
  829. if (port->pending)
  830. memmove(port->buf, port->buf + c,
  831. port->pending);
  832. return true;
  833. }
  834. }
  835. port->pending = 0;
  836. }
  837. return false;
  838. }
  839. /* fill the MIDI bytes onto the temporary buffer
  840. */
  841. static void fill_midi1_pending_buf(struct f_midi2 *midi2, u8 cable, u8 *buf,
  842. unsigned int size)
  843. {
  844. struct f_midi2_midi1_port *port = &midi2->midi1_port[cable];
  845. if (port->pending + size > sizeof(port->buf))
  846. return;
  847. memcpy(port->buf + port->pending, buf, size);
  848. port->pending += size;
  849. }
  850. /* try to process data given from the associated UMP stream */
  851. static void process_midi1_transmit(struct f_midi2 *midi2)
  852. {
  853. struct f_midi2_usb_ep *usb_ep = &midi2->midi1_ep_in;
  854. struct f_midi2_ep *ep = &midi2->midi2_eps[0];
  855. struct usb_request *req = NULL;
  856. /* 12 is the largest outcome (4 MIDI1 cmds) for a single UMP packet */
  857. unsigned char outbuf[12];
  858. unsigned char group, cable;
  859. int len, size;
  860. u32 ump;
  861. if (!usb_ep->usb_ep || !usb_ep->usb_ep->enabled)
  862. return;
  863. for (;;) {
  864. if (!req) {
  865. req = get_empty_request(usb_ep);
  866. if (!req)
  867. break;
  868. }
  869. if (process_midi1_pending_buf(midi2, &req))
  870. continue;
  871. len = snd_ump_transmit(ep->ump, &ump, 4);
  872. if (len <= 0)
  873. break;
  874. if (snd_ump_receive_ump_val(ep->ump, ump) <= 0)
  875. continue;
  876. size = snd_ump_convert_from_ump(ep->ump->input_buf, outbuf,
  877. &group);
  878. if (size <= 0)
  879. continue;
  880. cable = ep->in_group_to_cable[group];
  881. if (!cable)
  882. continue;
  883. cable--; /* to 0-base */
  884. fill_midi1_pending_buf(midi2, cable, outbuf, size);
  885. }
  886. if (req) {
  887. if (req->length)
  888. queue_request_ep_raw(req);
  889. else
  890. put_empty_request(req);
  891. }
  892. }
  893. /* complete handler for MIDI1 EP-in requests */
  894. static void f_midi2_midi1_ep_in_complete(struct usb_ep *usb_ep,
  895. struct usb_request *req)
  896. {
  897. struct f_midi2_req_ctx *ctx = req->context;
  898. struct f_midi2 *midi2 = ctx->usb_ep->card;
  899. int status = req->status;
  900. put_empty_request(req);
  901. if (status) {
  902. DBG(midi2, "%s complete error %d: %d/%d\n",
  903. usb_ep->name, status, req->actual, req->length);
  904. return;
  905. }
  906. process_midi1_transmit(midi2);
  907. }
  908. /* complete handler for MIDI1 EP-out requests */
  909. static void f_midi2_midi1_ep_out_complete(struct usb_ep *usb_ep,
  910. struct usb_request *req)
  911. {
  912. struct f_midi2_req_ctx *ctx = req->context;
  913. struct f_midi2 *midi2 = ctx->usb_ep->card;
  914. struct f_midi2_ep *ep;
  915. struct ump_cvt_to_ump *cvt = &midi2->midi1_ump_cvt;
  916. static const u8 midi1_packet_bytes[16] = {
  917. 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1
  918. };
  919. unsigned int group, cable, bytes, c, len;
  920. int status = req->status;
  921. const u8 *buf = req->buf;
  922. if (status) {
  923. DBG(midi2, "%s complete error %d: %d/%d\n",
  924. usb_ep->name, status, req->actual, req->length);
  925. goto error;
  926. }
  927. len = req->actual >> 2;
  928. for (; len; len--, buf += 4) {
  929. cable = *buf >> 4;
  930. ep = midi2->out_cable_mapping[cable].ep;
  931. if (!ep)
  932. continue;
  933. group = midi2->out_cable_mapping[cable].group;
  934. bytes = midi1_packet_bytes[*buf & 0x0f];
  935. for (c = 0; c < bytes; c++) {
  936. snd_ump_convert_to_ump(cvt, group,
  937. to_ump_protocol(ep->info.protocol),
  938. buf[c + 1]);
  939. if (cvt->ump_bytes) {
  940. snd_ump_receive(ep->ump, cvt->ump,
  941. cvt->ump_bytes);
  942. cvt->ump_bytes = 0;
  943. }
  944. }
  945. }
  946. if (midi2->operation_mode != MIDI_OP_MODE_MIDI1)
  947. goto error;
  948. if (queue_request_ep_raw(req))
  949. goto error;
  950. return;
  951. error:
  952. put_empty_request(req);
  953. }
  954. /*
  955. * Common EP handling helpers
  956. */
  957. /* Start MIDI EP */
  958. static int f_midi2_start_ep(struct f_midi2_usb_ep *usb_ep,
  959. struct usb_function *fn)
  960. {
  961. int err;
  962. if (!usb_ep->usb_ep)
  963. return 0;
  964. usb_ep_disable(usb_ep->usb_ep);
  965. err = config_ep_by_speed(usb_ep->card->gadget, fn, usb_ep->usb_ep);
  966. if (err)
  967. return err;
  968. return usb_ep_enable(usb_ep->usb_ep);
  969. }
  970. /* Drop pending requests */
  971. static void f_midi2_drop_reqs(struct f_midi2_usb_ep *usb_ep)
  972. {
  973. int i;
  974. if (!usb_ep->usb_ep || !usb_ep->num_reqs)
  975. return;
  976. for (i = 0; i < usb_ep->num_reqs; i++) {
  977. if (!test_bit(i, &usb_ep->free_reqs) && usb_ep->reqs[i].req) {
  978. usb_ep_dequeue(usb_ep->usb_ep, usb_ep->reqs[i].req);
  979. set_bit(i, &usb_ep->free_reqs);
  980. }
  981. }
  982. }
  983. /* Allocate requests for the given EP */
  984. static int f_midi2_alloc_ep_reqs(struct f_midi2_usb_ep *usb_ep)
  985. {
  986. struct f_midi2 *midi2 = usb_ep->card;
  987. int i;
  988. if (!usb_ep->usb_ep)
  989. return 0;
  990. if (!usb_ep->reqs)
  991. return -EINVAL;
  992. for (i = 0; i < midi2->info.num_reqs; i++) {
  993. if (usb_ep->reqs[i].req)
  994. continue;
  995. usb_ep->reqs[i].req = alloc_ep_req(usb_ep->usb_ep,
  996. midi2->info.req_buf_size);
  997. if (!usb_ep->reqs[i].req)
  998. return -ENOMEM;
  999. usb_ep->reqs[i].req->context = &usb_ep->reqs[i];
  1000. }
  1001. return 0;
  1002. }
  1003. /* Free allocated requests */
  1004. static void f_midi2_free_ep_reqs(struct f_midi2_usb_ep *usb_ep)
  1005. {
  1006. struct f_midi2 *midi2 = usb_ep->card;
  1007. int i;
  1008. for (i = 0; i < midi2->info.num_reqs; i++) {
  1009. if (!usb_ep->reqs[i].req)
  1010. continue;
  1011. free_ep_req(usb_ep->usb_ep, usb_ep->reqs[i].req);
  1012. usb_ep->reqs[i].req = NULL;
  1013. }
  1014. }
  1015. /* Initialize EP */
  1016. static int f_midi2_init_ep(struct f_midi2 *midi2, struct f_midi2_ep *ep,
  1017. struct f_midi2_usb_ep *usb_ep,
  1018. void *desc,
  1019. void (*complete)(struct usb_ep *usb_ep,
  1020. struct usb_request *req))
  1021. {
  1022. int i;
  1023. usb_ep->card = midi2;
  1024. usb_ep->ep = ep;
  1025. usb_ep->usb_ep = usb_ep_autoconfig(midi2->gadget, desc);
  1026. if (!usb_ep->usb_ep)
  1027. return -ENODEV;
  1028. usb_ep->complete = complete;
  1029. usb_ep->reqs = kcalloc(midi2->info.num_reqs, sizeof(*usb_ep->reqs),
  1030. GFP_KERNEL);
  1031. if (!usb_ep->reqs)
  1032. return -ENOMEM;
  1033. for (i = 0; i < midi2->info.num_reqs; i++) {
  1034. usb_ep->reqs[i].index = i;
  1035. usb_ep->reqs[i].usb_ep = usb_ep;
  1036. set_bit(i, &usb_ep->free_reqs);
  1037. usb_ep->num_reqs++;
  1038. }
  1039. return 0;
  1040. }
  1041. /* Free EP */
  1042. static void f_midi2_free_ep(struct f_midi2_usb_ep *usb_ep)
  1043. {
  1044. f_midi2_drop_reqs(usb_ep);
  1045. f_midi2_free_ep_reqs(usb_ep);
  1046. kfree(usb_ep->reqs);
  1047. usb_ep->num_reqs = 0;
  1048. usb_ep->free_reqs = 0;
  1049. usb_ep->reqs = NULL;
  1050. }
  1051. /* Queue requests for EP-out at start */
  1052. static void f_midi2_queue_out_reqs(struct f_midi2_usb_ep *usb_ep)
  1053. {
  1054. int i, err;
  1055. if (!usb_ep->usb_ep)
  1056. return;
  1057. for (i = 0; i < usb_ep->num_reqs; i++) {
  1058. if (!test_bit(i, &usb_ep->free_reqs) || !usb_ep->reqs[i].req)
  1059. continue;
  1060. usb_ep->reqs[i].req->complete = usb_ep->complete;
  1061. err = usb_ep_queue(usb_ep->usb_ep, usb_ep->reqs[i].req,
  1062. GFP_ATOMIC);
  1063. if (!err)
  1064. clear_bit(i, &usb_ep->free_reqs);
  1065. }
  1066. }
  1067. /*
  1068. * Gadget Function callbacks
  1069. */
  1070. /* stop both IN and OUT EPs */
  1071. static void f_midi2_stop_eps(struct f_midi2_usb_ep *ep_in,
  1072. struct f_midi2_usb_ep *ep_out)
  1073. {
  1074. f_midi2_drop_reqs(ep_in);
  1075. f_midi2_drop_reqs(ep_out);
  1076. f_midi2_free_ep_reqs(ep_in);
  1077. f_midi2_free_ep_reqs(ep_out);
  1078. }
  1079. /* start/queue both IN and OUT EPs */
  1080. static int f_midi2_start_eps(struct f_midi2_usb_ep *ep_in,
  1081. struct f_midi2_usb_ep *ep_out,
  1082. struct usb_function *fn)
  1083. {
  1084. int err;
  1085. err = f_midi2_start_ep(ep_in, fn);
  1086. if (err)
  1087. return err;
  1088. err = f_midi2_start_ep(ep_out, fn);
  1089. if (err)
  1090. return err;
  1091. err = f_midi2_alloc_ep_reqs(ep_in);
  1092. if (err)
  1093. return err;
  1094. err = f_midi2_alloc_ep_reqs(ep_out);
  1095. if (err)
  1096. return err;
  1097. f_midi2_queue_out_reqs(ep_out);
  1098. return 0;
  1099. }
  1100. /* gadget function set_alt callback */
  1101. static int f_midi2_set_alt(struct usb_function *fn, unsigned int intf,
  1102. unsigned int alt)
  1103. {
  1104. struct f_midi2 *midi2 = func_to_midi2(fn);
  1105. struct f_midi2_ep *ep;
  1106. int i, op_mode, err;
  1107. if (intf != midi2->midi_if || alt > 1)
  1108. return 0;
  1109. if (alt == 0)
  1110. op_mode = MIDI_OP_MODE_MIDI1;
  1111. else if (alt == 1)
  1112. op_mode = MIDI_OP_MODE_MIDI2;
  1113. else
  1114. op_mode = MIDI_OP_MODE_UNSET;
  1115. if (midi2->operation_mode == op_mode)
  1116. return 0;
  1117. midi2->operation_mode = op_mode;
  1118. if (op_mode != MIDI_OP_MODE_MIDI1)
  1119. f_midi2_stop_eps(&midi2->midi1_ep_in, &midi2->midi1_ep_out);
  1120. if (op_mode != MIDI_OP_MODE_MIDI2) {
  1121. for (i = 0; i < midi2->num_eps; i++) {
  1122. ep = &midi2->midi2_eps[i];
  1123. f_midi2_stop_eps(&ep->ep_in, &ep->ep_out);
  1124. }
  1125. }
  1126. if (op_mode == MIDI_OP_MODE_MIDI1)
  1127. return f_midi2_start_eps(&midi2->midi1_ep_in,
  1128. &midi2->midi1_ep_out, fn);
  1129. if (op_mode == MIDI_OP_MODE_MIDI2) {
  1130. for (i = 0; i < midi2->num_eps; i++) {
  1131. ep = &midi2->midi2_eps[i];
  1132. err = f_midi2_start_eps(&ep->ep_in, &ep->ep_out, fn);
  1133. if (err)
  1134. return err;
  1135. }
  1136. }
  1137. return 0;
  1138. }
  1139. /* gadget function get_alt callback */
  1140. static int f_midi2_get_alt(struct usb_function *fn, unsigned int intf)
  1141. {
  1142. struct f_midi2 *midi2 = func_to_midi2(fn);
  1143. if (intf == midi2->midi_if &&
  1144. midi2->operation_mode == MIDI_OP_MODE_MIDI2)
  1145. return 1;
  1146. return 0;
  1147. }
  1148. /* convert UMP direction to USB MIDI 2.0 direction */
  1149. static unsigned int ump_to_usb_dir(unsigned int ump_dir)
  1150. {
  1151. switch (ump_dir) {
  1152. case SNDRV_UMP_DIR_INPUT:
  1153. return USB_MS_GR_TRM_BLOCK_TYPE_INPUT_ONLY;
  1154. case SNDRV_UMP_DIR_OUTPUT:
  1155. return USB_MS_GR_TRM_BLOCK_TYPE_OUTPUT_ONLY;
  1156. default:
  1157. return USB_MS_GR_TRM_BLOCK_TYPE_BIDIRECTIONAL;
  1158. }
  1159. }
  1160. /* assign GTB descriptors (for the given request) */
  1161. static void assign_block_descriptors(struct f_midi2 *midi2,
  1162. struct usb_request *req,
  1163. int max_len)
  1164. {
  1165. struct usb_ms20_gr_trm_block_header_descriptor header;
  1166. struct usb_ms20_gr_trm_block_descriptor *desc;
  1167. struct f_midi2_block_info *b;
  1168. struct f_midi2_ep *ep;
  1169. int i, blk, len;
  1170. char *data;
  1171. len = sizeof(gtb_header_desc) + sizeof(gtb_desc) * midi2->total_blocks;
  1172. if (WARN_ON(len > midi2->info.req_buf_size))
  1173. return;
  1174. header = gtb_header_desc;
  1175. header.wTotalLength = cpu_to_le16(len);
  1176. if (max_len < len) {
  1177. len = min_t(int, len, sizeof(header));
  1178. memcpy(req->buf, &header, len);
  1179. req->length = len;
  1180. req->zero = len < max_len;
  1181. return;
  1182. }
  1183. memcpy(req->buf, &header, sizeof(header));
  1184. data = req->buf + sizeof(header);
  1185. for (i = 0; i < midi2->num_eps; i++) {
  1186. ep = &midi2->midi2_eps[i];
  1187. for (blk = 0; blk < ep->num_blks; blk++) {
  1188. b = &ep->blks[blk].info;
  1189. desc = (struct usb_ms20_gr_trm_block_descriptor *)data;
  1190. *desc = gtb_desc;
  1191. desc->bGrpTrmBlkID = ep->blks[blk].gtb_id;
  1192. desc->bGrpTrmBlkType = ump_to_usb_dir(b->direction);
  1193. desc->nGroupTrm = b->first_group;
  1194. desc->nNumGroupTrm = b->num_groups;
  1195. desc->iBlockItem = ep->blks[blk].string_id;
  1196. if (ep->info.protocol == 2)
  1197. desc->bMIDIProtocol = USB_MS_MIDI_PROTO_2_0;
  1198. else
  1199. desc->bMIDIProtocol = USB_MS_MIDI_PROTO_1_0_128;
  1200. if (b->is_midi1 == 2) {
  1201. desc->wMaxInputBandwidth = cpu_to_le16(1);
  1202. desc->wMaxOutputBandwidth = cpu_to_le16(1);
  1203. }
  1204. data += sizeof(*desc);
  1205. }
  1206. }
  1207. req->length = len;
  1208. req->zero = len < max_len;
  1209. }
  1210. /* gadget function setup callback: handle GTB requests */
  1211. static int f_midi2_setup(struct usb_function *fn,
  1212. const struct usb_ctrlrequest *ctrl)
  1213. {
  1214. struct f_midi2 *midi2 = func_to_midi2(fn);
  1215. struct usb_composite_dev *cdev = fn->config->cdev;
  1216. struct usb_request *req = cdev->req;
  1217. u16 value, length;
  1218. if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD ||
  1219. ctrl->bRequest != USB_REQ_GET_DESCRIPTOR)
  1220. return -EOPNOTSUPP;
  1221. value = le16_to_cpu(ctrl->wValue);
  1222. length = le16_to_cpu(ctrl->wLength);
  1223. if ((value >> 8) != USB_DT_CS_GR_TRM_BLOCK)
  1224. return -EOPNOTSUPP;
  1225. /* handle only altset 1 */
  1226. if ((value & 0xff) != 1)
  1227. return -EOPNOTSUPP;
  1228. assign_block_descriptors(midi2, req, length);
  1229. return usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  1230. }
  1231. /* gadget function disable callback */
  1232. static void f_midi2_disable(struct usb_function *fn)
  1233. {
  1234. struct f_midi2 *midi2 = func_to_midi2(fn);
  1235. midi2->operation_mode = MIDI_OP_MODE_UNSET;
  1236. }
  1237. /*
  1238. * ALSA UMP ops: most of them are NOPs, only trigger for write is needed
  1239. */
  1240. static int f_midi2_ump_open(struct snd_ump_endpoint *ump, int dir)
  1241. {
  1242. return 0;
  1243. }
  1244. static void f_midi2_ump_close(struct snd_ump_endpoint *ump, int dir)
  1245. {
  1246. }
  1247. static void f_midi2_ump_trigger(struct snd_ump_endpoint *ump, int dir, int up)
  1248. {
  1249. struct f_midi2_ep *ep = ump->private_data;
  1250. struct f_midi2 *midi2 = ep->card;
  1251. if (up && dir == SNDRV_RAWMIDI_STREAM_OUTPUT) {
  1252. switch (midi2->operation_mode) {
  1253. case MIDI_OP_MODE_MIDI1:
  1254. process_midi1_transmit(midi2);
  1255. break;
  1256. case MIDI_OP_MODE_MIDI2:
  1257. process_ump_transmit(ep);
  1258. break;
  1259. }
  1260. }
  1261. }
  1262. static void f_midi2_ump_drain(struct snd_ump_endpoint *ump, int dir)
  1263. {
  1264. }
  1265. static const struct snd_ump_ops f_midi2_ump_ops = {
  1266. .open = f_midi2_ump_open,
  1267. .close = f_midi2_ump_close,
  1268. .trigger = f_midi2_ump_trigger,
  1269. .drain = f_midi2_ump_drain,
  1270. };
  1271. /*
  1272. * "Operation Mode" control element
  1273. */
  1274. static int f_midi2_operation_mode_info(struct snd_kcontrol *kcontrol,
  1275. struct snd_ctl_elem_info *uinfo)
  1276. {
  1277. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1278. uinfo->count = 1;
  1279. uinfo->value.integer.min = MIDI_OP_MODE_UNSET;
  1280. uinfo->value.integer.max = MIDI_OP_MODE_MIDI2;
  1281. return 0;
  1282. }
  1283. static int f_midi2_operation_mode_get(struct snd_kcontrol *kcontrol,
  1284. struct snd_ctl_elem_value *ucontrol)
  1285. {
  1286. struct f_midi2 *midi2 = snd_kcontrol_chip(kcontrol);
  1287. ucontrol->value.integer.value[0] = midi2->operation_mode;
  1288. return 0;
  1289. }
  1290. static const struct snd_kcontrol_new operation_mode_ctl = {
  1291. .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
  1292. .name = "Operation Mode",
  1293. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  1294. .info = f_midi2_operation_mode_info,
  1295. .get = f_midi2_operation_mode_get,
  1296. };
  1297. /*
  1298. * ALSA UMP instance creation / deletion
  1299. */
  1300. static void f_midi2_free_card(struct f_midi2 *midi2)
  1301. {
  1302. if (midi2->card) {
  1303. snd_card_free_when_closed(midi2->card);
  1304. midi2->card = NULL;
  1305. }
  1306. }
  1307. /* use a reverse direction for the gadget host */
  1308. static int reverse_dir(int dir)
  1309. {
  1310. if (!dir || dir == SNDRV_UMP_DIR_BIDIRECTION)
  1311. return dir;
  1312. return (dir == SNDRV_UMP_DIR_OUTPUT) ?
  1313. SNDRV_UMP_DIR_INPUT : SNDRV_UMP_DIR_OUTPUT;
  1314. }
  1315. static int f_midi2_create_card(struct f_midi2 *midi2)
  1316. {
  1317. struct snd_card *card;
  1318. struct snd_ump_endpoint *ump;
  1319. struct f_midi2_ep *ep;
  1320. int i, id, blk, err;
  1321. __be32 sw;
  1322. err = snd_card_new(&midi2->gadget->dev, -1, NULL, THIS_MODULE, 0,
  1323. &card);
  1324. if (err < 0)
  1325. return err;
  1326. midi2->card = card;
  1327. strcpy(card->driver, "f_midi2");
  1328. strcpy(card->shortname, "MIDI 2.0 Gadget");
  1329. strcpy(card->longname, "MIDI 2.0 Gadget");
  1330. id = 0;
  1331. for (i = 0; i < midi2->num_eps; i++) {
  1332. ep = &midi2->midi2_eps[i];
  1333. err = snd_ump_endpoint_new(card, "MIDI 2.0 Gadget", id,
  1334. 1, 1, &ump);
  1335. if (err < 0)
  1336. goto error;
  1337. id++;
  1338. ep->ump = ump;
  1339. ump->no_process_stream = true;
  1340. ump->private_data = ep;
  1341. ump->ops = &f_midi2_ump_ops;
  1342. if (midi2->info.static_block)
  1343. ump->info.flags |= SNDRV_UMP_EP_INFO_STATIC_BLOCKS;
  1344. ump->info.protocol_caps = (ep->info.protocol_caps & 3) << 8;
  1345. ump->info.protocol = to_ump_protocol(ep->info.protocol);
  1346. ump->info.version = 0x0101;
  1347. ump->info.family_id = ep->info.family;
  1348. ump->info.model_id = ep->info.model;
  1349. ump->info.manufacturer_id = ep->info.manufacturer & 0xffffff;
  1350. sw = cpu_to_be32(ep->info.sw_revision);
  1351. memcpy(ump->info.sw_revision, &sw, 4);
  1352. strscpy(ump->info.name, ump_ep_name(ep),
  1353. sizeof(ump->info.name));
  1354. strscpy(ump->info.product_id, ump_product_id(ep),
  1355. sizeof(ump->info.product_id));
  1356. strscpy(ump->core.name, ump->info.name, sizeof(ump->core.name));
  1357. for (blk = 0; blk < ep->num_blks; blk++) {
  1358. const struct f_midi2_block_info *b = &ep->blks[blk].info;
  1359. struct snd_ump_block *fb;
  1360. err = snd_ump_block_new(ump, blk,
  1361. reverse_dir(b->direction),
  1362. b->first_group, b->num_groups,
  1363. &ep->blks[blk].fb);
  1364. if (err < 0)
  1365. goto error;
  1366. fb = ep->blks[blk].fb;
  1367. fb->info.active = !!b->active;
  1368. fb->info.midi_ci_version = b->midi_ci_version;
  1369. fb->info.ui_hint = reverse_dir(b->ui_hint);
  1370. fb->info.sysex8_streams = b->sysex8_streams;
  1371. if (b->is_midi1 < 2)
  1372. fb->info.flags |= b->is_midi1;
  1373. else
  1374. fb->info.flags |= SNDRV_UMP_BLOCK_IS_MIDI1 |
  1375. SNDRV_UMP_BLOCK_IS_LOWSPEED;
  1376. strscpy(fb->info.name, ump_fb_name(b),
  1377. sizeof(fb->info.name));
  1378. }
  1379. snd_ump_update_group_attrs(ump);
  1380. }
  1381. for (i = 0; i < midi2->num_eps; i++) {
  1382. err = snd_ump_attach_legacy_rawmidi(midi2->midi2_eps[i].ump,
  1383. "Legacy MIDI", id);
  1384. if (err < 0)
  1385. goto error;
  1386. id++;
  1387. }
  1388. err = snd_ctl_add(card, snd_ctl_new1(&operation_mode_ctl, midi2));
  1389. if (err < 0)
  1390. goto error;
  1391. err = snd_card_register(card);
  1392. if (err < 0)
  1393. goto error;
  1394. return 0;
  1395. error:
  1396. f_midi2_free_card(midi2);
  1397. return err;
  1398. }
  1399. /*
  1400. * Creation of USB descriptors
  1401. */
  1402. struct f_midi2_usb_config {
  1403. struct usb_descriptor_header **list;
  1404. unsigned int size;
  1405. unsigned int alloc;
  1406. /* MIDI 1.0 jacks */
  1407. unsigned char jack_in, jack_out, jack_id;
  1408. struct usb_midi_in_jack_descriptor jack_ins[MAX_CABLES];
  1409. struct usb_midi_out_jack_descriptor_1 jack_outs[MAX_CABLES];
  1410. };
  1411. static int append_config(struct f_midi2_usb_config *config, void *d)
  1412. {
  1413. unsigned int size;
  1414. void *buf;
  1415. if (config->size + 2 >= config->alloc) {
  1416. size = config->size + 16;
  1417. buf = krealloc(config->list, size * sizeof(void *), GFP_KERNEL);
  1418. if (!buf)
  1419. return -ENOMEM;
  1420. config->list = buf;
  1421. config->alloc = size;
  1422. }
  1423. config->list[config->size] = d;
  1424. config->size++;
  1425. config->list[config->size] = NULL;
  1426. return 0;
  1427. }
  1428. static int append_configs(struct f_midi2_usb_config *config, void **d)
  1429. {
  1430. int err;
  1431. for (; *d; d++) {
  1432. err = append_config(config, *d);
  1433. if (err)
  1434. return err;
  1435. }
  1436. return 0;
  1437. }
  1438. static int append_midi1_in_jack(struct f_midi2 *midi2,
  1439. struct f_midi2_usb_config *config,
  1440. struct midi1_cable_mapping *map,
  1441. unsigned int type)
  1442. {
  1443. struct usb_midi_in_jack_descriptor *jack =
  1444. &config->jack_ins[config->jack_in++];
  1445. int id = ++config->jack_id;
  1446. int err;
  1447. jack->bLength = 0x06;
  1448. jack->bDescriptorType = USB_DT_CS_INTERFACE;
  1449. jack->bDescriptorSubtype = USB_MS_MIDI_IN_JACK;
  1450. jack->bJackType = type;
  1451. jack->bJackID = id;
  1452. /* use the corresponding block name as jack name */
  1453. if (map->ep)
  1454. jack->iJack = map->ep->blks[map->block].string_id;
  1455. err = append_config(config, jack);
  1456. if (err < 0)
  1457. return err;
  1458. return id;
  1459. }
  1460. static int append_midi1_out_jack(struct f_midi2 *midi2,
  1461. struct f_midi2_usb_config *config,
  1462. struct midi1_cable_mapping *map,
  1463. unsigned int type, unsigned int source)
  1464. {
  1465. struct usb_midi_out_jack_descriptor_1 *jack =
  1466. &config->jack_outs[config->jack_out++];
  1467. int id = ++config->jack_id;
  1468. int err;
  1469. jack->bLength = 0x09;
  1470. jack->bDescriptorType = USB_DT_CS_INTERFACE;
  1471. jack->bDescriptorSubtype = USB_MS_MIDI_OUT_JACK;
  1472. jack->bJackType = type;
  1473. jack->bJackID = id;
  1474. jack->bNrInputPins = 1;
  1475. jack->pins[0].baSourceID = source;
  1476. jack->pins[0].baSourcePin = 0x01;
  1477. /* use the corresponding block name as jack name */
  1478. if (map->ep)
  1479. jack->iJack = map->ep->blks[map->block].string_id;
  1480. err = append_config(config, jack);
  1481. if (err < 0)
  1482. return err;
  1483. return id;
  1484. }
  1485. static int f_midi2_create_usb_configs(struct f_midi2 *midi2,
  1486. struct f_midi2_usb_config *config,
  1487. int speed)
  1488. {
  1489. void **midi1_in_eps, **midi1_out_eps;
  1490. int i, jack, total;
  1491. int err;
  1492. switch (speed) {
  1493. default:
  1494. case USB_SPEED_HIGH:
  1495. midi2_midi1_ep_out_desc.wMaxPacketSize = cpu_to_le16(512);
  1496. midi2_midi1_ep_in_desc.wMaxPacketSize = cpu_to_le16(512);
  1497. for (i = 0; i < midi2->num_eps; i++) {
  1498. midi2_midi2_ep_out_desc[i].wMaxPacketSize =
  1499. cpu_to_le16(512);
  1500. midi2_midi2_ep_in_desc[i].wMaxPacketSize =
  1501. cpu_to_le16(512);
  1502. }
  1503. fallthrough;
  1504. case USB_SPEED_FULL:
  1505. midi1_in_eps = midi2_midi1_ep_in_descs;
  1506. midi1_out_eps = midi2_midi1_ep_out_descs;
  1507. break;
  1508. case USB_SPEED_SUPER:
  1509. midi2_midi1_ep_out_desc.wMaxPacketSize = cpu_to_le16(1024);
  1510. midi2_midi1_ep_in_desc.wMaxPacketSize = cpu_to_le16(1024);
  1511. for (i = 0; i < midi2->num_eps; i++) {
  1512. midi2_midi2_ep_out_desc[i].wMaxPacketSize =
  1513. cpu_to_le16(1024);
  1514. midi2_midi2_ep_in_desc[i].wMaxPacketSize =
  1515. cpu_to_le16(1024);
  1516. }
  1517. midi1_in_eps = midi2_midi1_ep_in_ss_descs;
  1518. midi1_out_eps = midi2_midi1_ep_out_ss_descs;
  1519. break;
  1520. }
  1521. err = append_configs(config, midi2_audio_descs);
  1522. if (err < 0)
  1523. return err;
  1524. if (midi2->num_midi1_in && midi2->num_midi1_out)
  1525. midi2_midi1_if_desc.bNumEndpoints = 2;
  1526. else
  1527. midi2_midi1_if_desc.bNumEndpoints = 1;
  1528. err = append_configs(config, midi2_midi1_descs);
  1529. if (err < 0)
  1530. return err;
  1531. total = USB_DT_MS_HEADER_SIZE;
  1532. if (midi2->num_midi1_out) {
  1533. midi2_midi1_ep_out_class_desc.bLength =
  1534. USB_DT_MS_ENDPOINT_SIZE(midi2->num_midi1_out);
  1535. total += midi2_midi1_ep_out_class_desc.bLength;
  1536. midi2_midi1_ep_out_class_desc.bNumEmbMIDIJack =
  1537. midi2->num_midi1_out;
  1538. total += midi2->num_midi1_out *
  1539. (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
  1540. for (i = 0; i < midi2->num_midi1_out; i++) {
  1541. jack = append_midi1_in_jack(midi2, config,
  1542. &midi2->in_cable_mapping[i],
  1543. USB_MS_EMBEDDED);
  1544. if (jack < 0)
  1545. return jack;
  1546. midi2_midi1_ep_out_class_desc.baAssocJackID[i] = jack;
  1547. jack = append_midi1_out_jack(midi2, config,
  1548. &midi2->in_cable_mapping[i],
  1549. USB_MS_EXTERNAL, jack);
  1550. if (jack < 0)
  1551. return jack;
  1552. }
  1553. }
  1554. if (midi2->num_midi1_in) {
  1555. midi2_midi1_ep_in_class_desc.bLength =
  1556. USB_DT_MS_ENDPOINT_SIZE(midi2->num_midi1_in);
  1557. total += midi2_midi1_ep_in_class_desc.bLength;
  1558. midi2_midi1_ep_in_class_desc.bNumEmbMIDIJack =
  1559. midi2->num_midi1_in;
  1560. total += midi2->num_midi1_in *
  1561. (USB_DT_MIDI_IN_SIZE + USB_DT_MIDI_OUT_SIZE(1));
  1562. for (i = 0; i < midi2->num_midi1_in; i++) {
  1563. jack = append_midi1_in_jack(midi2, config,
  1564. &midi2->out_cable_mapping[i],
  1565. USB_MS_EXTERNAL);
  1566. if (jack < 0)
  1567. return jack;
  1568. jack = append_midi1_out_jack(midi2, config,
  1569. &midi2->out_cable_mapping[i],
  1570. USB_MS_EMBEDDED, jack);
  1571. if (jack < 0)
  1572. return jack;
  1573. midi2_midi1_ep_in_class_desc.baAssocJackID[i] = jack;
  1574. }
  1575. }
  1576. midi2_midi1_class_desc.wTotalLength = cpu_to_le16(total);
  1577. if (midi2->num_midi1_out) {
  1578. err = append_configs(config, midi1_out_eps);
  1579. if (err < 0)
  1580. return err;
  1581. }
  1582. if (midi2->num_midi1_in) {
  1583. err = append_configs(config, midi1_in_eps);
  1584. if (err < 0)
  1585. return err;
  1586. }
  1587. err = append_configs(config, midi2_midi2_descs);
  1588. if (err < 0)
  1589. return err;
  1590. for (i = 0; i < midi2->num_eps; i++) {
  1591. err = append_config(config, &midi2_midi2_ep_out_desc[i]);
  1592. if (err < 0)
  1593. return err;
  1594. if (speed == USB_SPEED_SUPER || speed == USB_SPEED_SUPER_PLUS) {
  1595. err = append_config(config, &midi2_midi2_ep_out_ss_comp_desc);
  1596. if (err < 0)
  1597. return err;
  1598. }
  1599. err = append_config(config, &midi2_midi2_ep_out_class_desc[i]);
  1600. if (err < 0)
  1601. return err;
  1602. err = append_config(config, &midi2_midi2_ep_in_desc[i]);
  1603. if (err < 0)
  1604. return err;
  1605. if (speed == USB_SPEED_SUPER || speed == USB_SPEED_SUPER_PLUS) {
  1606. err = append_config(config, &midi2_midi2_ep_in_ss_comp_desc);
  1607. if (err < 0)
  1608. return err;
  1609. }
  1610. err = append_config(config, &midi2_midi2_ep_in_class_desc[i]);
  1611. if (err < 0)
  1612. return err;
  1613. }
  1614. return 0;
  1615. }
  1616. static void f_midi2_free_usb_configs(struct f_midi2_usb_config *config)
  1617. {
  1618. kfree(config->list);
  1619. memset(config, 0, sizeof(*config));
  1620. }
  1621. /* as we use the static descriptors for simplicity, serialize bind call */
  1622. static DEFINE_MUTEX(f_midi2_desc_mutex);
  1623. /* fill MIDI2 EP class-specific descriptor */
  1624. static void fill_midi2_class_desc(struct f_midi2_ep *ep,
  1625. struct usb_ms20_endpoint_descriptor_32 *cdesc)
  1626. {
  1627. int blk;
  1628. cdesc->bLength = USB_DT_MS20_ENDPOINT_SIZE(ep->num_blks);
  1629. cdesc->bDescriptorType = USB_DT_CS_ENDPOINT;
  1630. cdesc->bDescriptorSubtype = USB_MS_GENERAL_2_0;
  1631. cdesc->bNumGrpTrmBlock = ep->num_blks;
  1632. for (blk = 0; blk < ep->num_blks; blk++)
  1633. cdesc->baAssoGrpTrmBlkID[blk] = ep->blks[blk].gtb_id;
  1634. }
  1635. /* initialize MIDI2 EP-in */
  1636. static int f_midi2_init_midi2_ep_in(struct f_midi2 *midi2, int index)
  1637. {
  1638. struct f_midi2_ep *ep = &midi2->midi2_eps[index];
  1639. struct usb_endpoint_descriptor *desc = &midi2_midi2_ep_in_desc[index];
  1640. desc->bLength = USB_DT_ENDPOINT_SIZE;
  1641. desc->bDescriptorType = USB_DT_ENDPOINT;
  1642. desc->bEndpointAddress = USB_DIR_IN;
  1643. desc->bmAttributes = USB_ENDPOINT_XFER_INT;
  1644. desc->wMaxPacketSize = cpu_to_le16(EP_MAX_PACKET_INT);
  1645. desc->bInterval = 1;
  1646. fill_midi2_class_desc(ep, &midi2_midi2_ep_in_class_desc[index]);
  1647. return f_midi2_init_ep(midi2, ep, &ep->ep_in, desc,
  1648. f_midi2_ep_in_complete);
  1649. }
  1650. /* initialize MIDI2 EP-out */
  1651. static int f_midi2_init_midi2_ep_out(struct f_midi2 *midi2, int index)
  1652. {
  1653. struct f_midi2_ep *ep = &midi2->midi2_eps[index];
  1654. struct usb_endpoint_descriptor *desc = &midi2_midi2_ep_out_desc[index];
  1655. desc->bLength = USB_DT_ENDPOINT_SIZE;
  1656. desc->bDescriptorType = USB_DT_ENDPOINT;
  1657. desc->bEndpointAddress = USB_DIR_OUT;
  1658. desc->bmAttributes = USB_ENDPOINT_XFER_BULK;
  1659. fill_midi2_class_desc(ep, &midi2_midi2_ep_out_class_desc[index]);
  1660. return f_midi2_init_ep(midi2, ep, &ep->ep_out, desc,
  1661. f_midi2_ep_out_complete);
  1662. }
  1663. /* gadget function bind callback */
  1664. static int f_midi2_bind(struct usb_configuration *c, struct usb_function *f)
  1665. {
  1666. struct usb_composite_dev *cdev = c->cdev;
  1667. struct f_midi2 *midi2 = func_to_midi2(f);
  1668. struct f_midi2_ep *ep;
  1669. struct f_midi2_usb_config config = {};
  1670. struct usb_gadget_strings string_fn = {
  1671. .language = 0x0409, /* en-us */
  1672. .strings = midi2->string_defs,
  1673. };
  1674. struct usb_gadget_strings *strings[] = {
  1675. &string_fn,
  1676. NULL,
  1677. };
  1678. int i, blk, status;
  1679. midi2->gadget = cdev->gadget;
  1680. midi2->operation_mode = MIDI_OP_MODE_UNSET;
  1681. status = f_midi2_create_card(midi2);
  1682. if (status < 0)
  1683. goto fail_register;
  1684. /* maybe allocate device-global string ID */
  1685. midi2->strings = usb_gstrings_attach(c->cdev, strings,
  1686. midi2->total_blocks + 1);
  1687. if (IS_ERR(midi2->strings)) {
  1688. status = PTR_ERR(midi2->strings);
  1689. goto fail_string;
  1690. }
  1691. mutex_lock(&f_midi2_desc_mutex);
  1692. midi2_midi1_if_desc.iInterface = midi2->strings[STR_IFACE].id;
  1693. midi2_midi2_if_desc.iInterface = midi2->strings[STR_IFACE].id;
  1694. for (i = 0; i < midi2->num_eps; i++) {
  1695. ep = &midi2->midi2_eps[i];
  1696. for (blk = 0; blk < ep->num_blks; blk++)
  1697. ep->blks[blk].string_id =
  1698. midi2->strings[gtb_to_str_id(ep->blks[blk].gtb_id)].id;
  1699. }
  1700. midi2_midi2_if_desc.bNumEndpoints = midi2->num_eps * 2;
  1701. /* audio interface */
  1702. status = usb_interface_id(c, f);
  1703. if (status < 0)
  1704. goto fail;
  1705. midi2_audio_if_desc.bInterfaceNumber = status;
  1706. /* MIDI streaming */
  1707. status = usb_interface_id(c, f);
  1708. if (status < 0)
  1709. goto fail;
  1710. midi2->midi_if = status;
  1711. midi2_midi1_if_desc.bInterfaceNumber = status;
  1712. midi2_midi2_if_desc.bInterfaceNumber = status;
  1713. midi2_audio_class_desc.baInterfaceNr[0] = status;
  1714. /* allocate instance-specific endpoints */
  1715. if (midi2->midi2_eps[0].blks[0].info.direction != SNDRV_UMP_DIR_OUTPUT) {
  1716. status = f_midi2_init_ep(midi2, NULL, &midi2->midi1_ep_in,
  1717. &midi2_midi1_ep_in_desc,
  1718. f_midi2_midi1_ep_in_complete);
  1719. if (status)
  1720. goto fail;
  1721. }
  1722. if (midi2->midi2_eps[0].blks[0].info.direction != SNDRV_UMP_DIR_INPUT) {
  1723. status = f_midi2_init_ep(midi2, NULL, &midi2->midi1_ep_out,
  1724. &midi2_midi1_ep_out_desc,
  1725. f_midi2_midi1_ep_out_complete);
  1726. if (status)
  1727. goto fail;
  1728. }
  1729. for (i = 0; i < midi2->num_eps; i++) {
  1730. status = f_midi2_init_midi2_ep_in(midi2, i);
  1731. if (status)
  1732. goto fail;
  1733. status = f_midi2_init_midi2_ep_out(midi2, i);
  1734. if (status)
  1735. goto fail;
  1736. }
  1737. status = f_midi2_create_usb_configs(midi2, &config, USB_SPEED_FULL);
  1738. if (status < 0)
  1739. goto fail;
  1740. f->fs_descriptors = usb_copy_descriptors(config.list);
  1741. if (!f->fs_descriptors) {
  1742. status = -ENOMEM;
  1743. goto fail;
  1744. }
  1745. f_midi2_free_usb_configs(&config);
  1746. status = f_midi2_create_usb_configs(midi2, &config, USB_SPEED_HIGH);
  1747. if (status < 0)
  1748. goto fail;
  1749. f->hs_descriptors = usb_copy_descriptors(config.list);
  1750. if (!f->hs_descriptors) {
  1751. status = -ENOMEM;
  1752. goto fail;
  1753. }
  1754. f_midi2_free_usb_configs(&config);
  1755. status = f_midi2_create_usb_configs(midi2, &config, USB_SPEED_SUPER);
  1756. if (status < 0)
  1757. goto fail;
  1758. f->ss_descriptors = usb_copy_descriptors(config.list);
  1759. if (!f->ss_descriptors) {
  1760. status = -ENOMEM;
  1761. goto fail;
  1762. }
  1763. f_midi2_free_usb_configs(&config);
  1764. mutex_unlock(&f_midi2_desc_mutex);
  1765. return 0;
  1766. fail:
  1767. f_midi2_free_usb_configs(&config);
  1768. mutex_unlock(&f_midi2_desc_mutex);
  1769. usb_free_all_descriptors(f);
  1770. fail_string:
  1771. f_midi2_free_card(midi2);
  1772. fail_register:
  1773. ERROR(midi2, "%s: can't bind, err %d\n", f->name, status);
  1774. return status;
  1775. }
  1776. /* gadget function unbind callback */
  1777. static void f_midi2_unbind(struct usb_configuration *c, struct usb_function *f)
  1778. {
  1779. struct f_midi2 *midi2 = func_to_midi2(f);
  1780. int i;
  1781. f_midi2_free_card(midi2);
  1782. f_midi2_free_ep(&midi2->midi1_ep_in);
  1783. f_midi2_free_ep(&midi2->midi1_ep_out);
  1784. for (i = 0; i < midi2->num_eps; i++) {
  1785. f_midi2_free_ep(&midi2->midi2_eps[i].ep_in);
  1786. f_midi2_free_ep(&midi2->midi2_eps[i].ep_out);
  1787. }
  1788. usb_free_all_descriptors(f);
  1789. }
  1790. /*
  1791. * ConfigFS interface
  1792. */
  1793. /* type conversion helpers */
  1794. static inline struct f_midi2_opts *to_f_midi2_opts(struct config_item *item)
  1795. {
  1796. return container_of(to_config_group(item), struct f_midi2_opts,
  1797. func_inst.group);
  1798. }
  1799. static inline struct f_midi2_ep_opts *
  1800. to_f_midi2_ep_opts(struct config_item *item)
  1801. {
  1802. return container_of(to_config_group(item), struct f_midi2_ep_opts,
  1803. group);
  1804. }
  1805. static inline struct f_midi2_block_opts *
  1806. to_f_midi2_block_opts(struct config_item *item)
  1807. {
  1808. return container_of(to_config_group(item), struct f_midi2_block_opts,
  1809. group);
  1810. }
  1811. /* trim the string to be usable for EP and FB name strings */
  1812. static void make_name_string(char *s)
  1813. {
  1814. char *p;
  1815. p = strchr(s, '\n');
  1816. if (p)
  1817. *p = 0;
  1818. p = s + strlen(s);
  1819. for (; p > s && isspace(*p); p--)
  1820. *p = 0;
  1821. }
  1822. /* configfs helpers: generic show/store for unisnged int */
  1823. static ssize_t f_midi2_opts_uint_show(struct f_midi2_opts *opts,
  1824. u32 val, const char *format, char *page)
  1825. {
  1826. int result;
  1827. mutex_lock(&opts->lock);
  1828. result = sprintf(page, format, val);
  1829. mutex_unlock(&opts->lock);
  1830. return result;
  1831. }
  1832. static ssize_t f_midi2_opts_uint_store(struct f_midi2_opts *opts,
  1833. u32 *valp, u32 minval, u32 maxval,
  1834. const char *page, size_t len)
  1835. {
  1836. int ret;
  1837. u32 val;
  1838. mutex_lock(&opts->lock);
  1839. if (opts->refcnt) {
  1840. ret = -EBUSY;
  1841. goto end;
  1842. }
  1843. ret = kstrtou32(page, 0, &val);
  1844. if (ret)
  1845. goto end;
  1846. if (val < minval || val > maxval) {
  1847. ret = -EINVAL;
  1848. goto end;
  1849. }
  1850. *valp = val;
  1851. ret = len;
  1852. end:
  1853. mutex_unlock(&opts->lock);
  1854. return ret;
  1855. }
  1856. /* generic store for bool */
  1857. static ssize_t f_midi2_opts_bool_store(struct f_midi2_opts *opts,
  1858. bool *valp, const char *page, size_t len)
  1859. {
  1860. int ret;
  1861. bool val;
  1862. mutex_lock(&opts->lock);
  1863. if (opts->refcnt) {
  1864. ret = -EBUSY;
  1865. goto end;
  1866. }
  1867. ret = kstrtobool(page, &val);
  1868. if (ret)
  1869. goto end;
  1870. *valp = val;
  1871. ret = len;
  1872. end:
  1873. mutex_unlock(&opts->lock);
  1874. return ret;
  1875. }
  1876. /* generic show/store for string */
  1877. static ssize_t f_midi2_opts_str_show(struct f_midi2_opts *opts,
  1878. const char *str, char *page)
  1879. {
  1880. int result = 0;
  1881. mutex_lock(&opts->lock);
  1882. if (str)
  1883. result = scnprintf(page, PAGE_SIZE, "%s\n", str);
  1884. mutex_unlock(&opts->lock);
  1885. return result;
  1886. }
  1887. static ssize_t f_midi2_opts_str_store(struct f_midi2_opts *opts,
  1888. const char **strp, size_t maxlen,
  1889. const char *page, size_t len)
  1890. {
  1891. char *c;
  1892. int ret;
  1893. mutex_lock(&opts->lock);
  1894. if (opts->refcnt) {
  1895. ret = -EBUSY;
  1896. goto end;
  1897. }
  1898. c = kstrndup(page, min(len, maxlen), GFP_KERNEL);
  1899. if (!c) {
  1900. ret = -ENOMEM;
  1901. goto end;
  1902. }
  1903. kfree(*strp);
  1904. make_name_string(c);
  1905. *strp = c;
  1906. ret = len;
  1907. end:
  1908. mutex_unlock(&opts->lock);
  1909. return ret;
  1910. }
  1911. /*
  1912. * Definitions for UMP Block config
  1913. */
  1914. /* define an uint option for block */
  1915. #define F_MIDI2_BLOCK_OPT(name, format, minval, maxval) \
  1916. static ssize_t f_midi2_block_opts_##name##_show(struct config_item *item,\
  1917. char *page) \
  1918. { \
  1919. struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \
  1920. return f_midi2_opts_uint_show(opts->ep->opts, opts->info.name, \
  1921. format "\n", page); \
  1922. } \
  1923. \
  1924. static ssize_t f_midi2_block_opts_##name##_store(struct config_item *item,\
  1925. const char *page, size_t len) \
  1926. { \
  1927. struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \
  1928. return f_midi2_opts_uint_store(opts->ep->opts, &opts->info.name,\
  1929. minval, maxval, page, len); \
  1930. } \
  1931. \
  1932. CONFIGFS_ATTR(f_midi2_block_opts_, name)
  1933. /* define a boolean option for block */
  1934. #define F_MIDI2_BLOCK_BOOL_OPT(name) \
  1935. static ssize_t f_midi2_block_opts_##name##_show(struct config_item *item,\
  1936. char *page) \
  1937. { \
  1938. struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \
  1939. return f_midi2_opts_uint_show(opts->ep->opts, opts->info.name, \
  1940. "%u\n", page); \
  1941. } \
  1942. \
  1943. static ssize_t f_midi2_block_opts_##name##_store(struct config_item *item,\
  1944. const char *page, size_t len) \
  1945. { \
  1946. struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item); \
  1947. return f_midi2_opts_bool_store(opts->ep->opts, &opts->info.name,\
  1948. page, len); \
  1949. } \
  1950. \
  1951. CONFIGFS_ATTR(f_midi2_block_opts_, name)
  1952. F_MIDI2_BLOCK_OPT(direction, "0x%x", 1, 3);
  1953. F_MIDI2_BLOCK_OPT(first_group, "0x%x", 0, 15);
  1954. F_MIDI2_BLOCK_OPT(num_groups, "0x%x", 1, 16);
  1955. F_MIDI2_BLOCK_OPT(midi1_first_group, "0x%x", 0, 15);
  1956. F_MIDI2_BLOCK_OPT(midi1_num_groups, "0x%x", 0, 16);
  1957. F_MIDI2_BLOCK_OPT(ui_hint, "0x%x", 0, 3);
  1958. F_MIDI2_BLOCK_OPT(midi_ci_version, "%u", 0, 1);
  1959. F_MIDI2_BLOCK_OPT(sysex8_streams, "%u", 0, 255);
  1960. F_MIDI2_BLOCK_OPT(is_midi1, "%u", 0, 2);
  1961. F_MIDI2_BLOCK_BOOL_OPT(active);
  1962. static ssize_t f_midi2_block_opts_name_show(struct config_item *item,
  1963. char *page)
  1964. {
  1965. struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item);
  1966. return f_midi2_opts_str_show(opts->ep->opts, opts->info.name, page);
  1967. }
  1968. static ssize_t f_midi2_block_opts_name_store(struct config_item *item,
  1969. const char *page, size_t len)
  1970. {
  1971. struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item);
  1972. return f_midi2_opts_str_store(opts->ep->opts, &opts->info.name, 128,
  1973. page, len);
  1974. }
  1975. CONFIGFS_ATTR(f_midi2_block_opts_, name);
  1976. static struct configfs_attribute *f_midi2_block_attrs[] = {
  1977. &f_midi2_block_opts_attr_direction,
  1978. &f_midi2_block_opts_attr_first_group,
  1979. &f_midi2_block_opts_attr_num_groups,
  1980. &f_midi2_block_opts_attr_midi1_first_group,
  1981. &f_midi2_block_opts_attr_midi1_num_groups,
  1982. &f_midi2_block_opts_attr_ui_hint,
  1983. &f_midi2_block_opts_attr_midi_ci_version,
  1984. &f_midi2_block_opts_attr_sysex8_streams,
  1985. &f_midi2_block_opts_attr_is_midi1,
  1986. &f_midi2_block_opts_attr_active,
  1987. &f_midi2_block_opts_attr_name,
  1988. NULL,
  1989. };
  1990. static void f_midi2_block_opts_release(struct config_item *item)
  1991. {
  1992. struct f_midi2_block_opts *opts = to_f_midi2_block_opts(item);
  1993. kfree(opts->info.name);
  1994. kfree(opts);
  1995. }
  1996. static struct configfs_item_operations f_midi2_block_item_ops = {
  1997. .release = f_midi2_block_opts_release,
  1998. };
  1999. static const struct config_item_type f_midi2_block_type = {
  2000. .ct_item_ops = &f_midi2_block_item_ops,
  2001. .ct_attrs = f_midi2_block_attrs,
  2002. .ct_owner = THIS_MODULE,
  2003. };
  2004. /* create a f_midi2_block_opts instance for the given block number */
  2005. static int f_midi2_block_opts_create(struct f_midi2_ep_opts *ep_opts,
  2006. unsigned int blk,
  2007. struct f_midi2_block_opts **block_p)
  2008. {
  2009. struct f_midi2_block_opts *block_opts;
  2010. int ret = 0;
  2011. mutex_lock(&ep_opts->opts->lock);
  2012. if (ep_opts->opts->refcnt || ep_opts->blks[blk]) {
  2013. ret = -EBUSY;
  2014. goto out;
  2015. }
  2016. block_opts = kzalloc(sizeof(*block_opts), GFP_KERNEL);
  2017. if (!block_opts) {
  2018. ret = -ENOMEM;
  2019. goto out;
  2020. }
  2021. block_opts->ep = ep_opts;
  2022. block_opts->id = blk;
  2023. /* set up the default values */
  2024. block_opts->info.direction = SNDRV_UMP_DIR_BIDIRECTION;
  2025. block_opts->info.first_group = 0;
  2026. block_opts->info.num_groups = 1;
  2027. block_opts->info.ui_hint = SNDRV_UMP_BLOCK_UI_HINT_BOTH;
  2028. block_opts->info.active = 1;
  2029. ep_opts->blks[blk] = block_opts;
  2030. *block_p = block_opts;
  2031. out:
  2032. mutex_unlock(&ep_opts->opts->lock);
  2033. return ret;
  2034. }
  2035. /* make_group callback for a block */
  2036. static struct config_group *
  2037. f_midi2_opts_block_make(struct config_group *group, const char *name)
  2038. {
  2039. struct f_midi2_ep_opts *ep_opts;
  2040. struct f_midi2_block_opts *block_opts;
  2041. unsigned int blk;
  2042. int ret;
  2043. if (strncmp(name, "block.", 6))
  2044. return ERR_PTR(-EINVAL);
  2045. ret = kstrtouint(name + 6, 10, &blk);
  2046. if (ret)
  2047. return ERR_PTR(ret);
  2048. ep_opts = to_f_midi2_ep_opts(&group->cg_item);
  2049. if (blk >= SNDRV_UMP_MAX_BLOCKS)
  2050. return ERR_PTR(-EINVAL);
  2051. if (ep_opts->blks[blk])
  2052. return ERR_PTR(-EBUSY);
  2053. ret = f_midi2_block_opts_create(ep_opts, blk, &block_opts);
  2054. if (ret)
  2055. return ERR_PTR(ret);
  2056. config_group_init_type_name(&block_opts->group, name,
  2057. &f_midi2_block_type);
  2058. return &block_opts->group;
  2059. }
  2060. /* drop_item callback for a block */
  2061. static void
  2062. f_midi2_opts_block_drop(struct config_group *group, struct config_item *item)
  2063. {
  2064. struct f_midi2_block_opts *block_opts = to_f_midi2_block_opts(item);
  2065. mutex_lock(&block_opts->ep->opts->lock);
  2066. block_opts->ep->blks[block_opts->id] = NULL;
  2067. mutex_unlock(&block_opts->ep->opts->lock);
  2068. config_item_put(item);
  2069. }
  2070. /*
  2071. * Definitions for UMP Endpoint config
  2072. */
  2073. /* define an uint option for EP */
  2074. #define F_MIDI2_EP_OPT(name, format, minval, maxval) \
  2075. static ssize_t f_midi2_ep_opts_##name##_show(struct config_item *item, \
  2076. char *page) \
  2077. { \
  2078. struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \
  2079. return f_midi2_opts_uint_show(opts->opts, opts->info.name, \
  2080. format "\n", page); \
  2081. } \
  2082. \
  2083. static ssize_t f_midi2_ep_opts_##name##_store(struct config_item *item, \
  2084. const char *page, size_t len)\
  2085. { \
  2086. struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \
  2087. return f_midi2_opts_uint_store(opts->opts, &opts->info.name, \
  2088. minval, maxval, page, len); \
  2089. } \
  2090. \
  2091. CONFIGFS_ATTR(f_midi2_ep_opts_, name)
  2092. /* define a string option for EP */
  2093. #define F_MIDI2_EP_STR_OPT(name, maxlen) \
  2094. static ssize_t f_midi2_ep_opts_##name##_show(struct config_item *item, \
  2095. char *page) \
  2096. { \
  2097. struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \
  2098. return f_midi2_opts_str_show(opts->opts, opts->info.name, page);\
  2099. } \
  2100. \
  2101. static ssize_t f_midi2_ep_opts_##name##_store(struct config_item *item, \
  2102. const char *page, size_t len) \
  2103. { \
  2104. struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item); \
  2105. return f_midi2_opts_str_store(opts->opts, &opts->info.name, maxlen,\
  2106. page, len); \
  2107. } \
  2108. \
  2109. CONFIGFS_ATTR(f_midi2_ep_opts_, name)
  2110. F_MIDI2_EP_OPT(protocol, "0x%x", 1, 2);
  2111. F_MIDI2_EP_OPT(protocol_caps, "0x%x", 1, 3);
  2112. F_MIDI2_EP_OPT(manufacturer, "0x%x", 0, 0xffffff);
  2113. F_MIDI2_EP_OPT(family, "0x%x", 0, 0xffff);
  2114. F_MIDI2_EP_OPT(model, "0x%x", 0, 0xffff);
  2115. F_MIDI2_EP_OPT(sw_revision, "0x%x", 0, 0xffffffff);
  2116. F_MIDI2_EP_STR_OPT(ep_name, 128);
  2117. F_MIDI2_EP_STR_OPT(product_id, 128);
  2118. static struct configfs_attribute *f_midi2_ep_attrs[] = {
  2119. &f_midi2_ep_opts_attr_protocol,
  2120. &f_midi2_ep_opts_attr_protocol_caps,
  2121. &f_midi2_ep_opts_attr_ep_name,
  2122. &f_midi2_ep_opts_attr_product_id,
  2123. &f_midi2_ep_opts_attr_manufacturer,
  2124. &f_midi2_ep_opts_attr_family,
  2125. &f_midi2_ep_opts_attr_model,
  2126. &f_midi2_ep_opts_attr_sw_revision,
  2127. NULL,
  2128. };
  2129. static void f_midi2_ep_opts_release(struct config_item *item)
  2130. {
  2131. struct f_midi2_ep_opts *opts = to_f_midi2_ep_opts(item);
  2132. kfree(opts->info.ep_name);
  2133. kfree(opts->info.product_id);
  2134. kfree(opts);
  2135. }
  2136. static struct configfs_item_operations f_midi2_ep_item_ops = {
  2137. .release = f_midi2_ep_opts_release,
  2138. };
  2139. static struct configfs_group_operations f_midi2_ep_group_ops = {
  2140. .make_group = f_midi2_opts_block_make,
  2141. .drop_item = f_midi2_opts_block_drop,
  2142. };
  2143. static const struct config_item_type f_midi2_ep_type = {
  2144. .ct_item_ops = &f_midi2_ep_item_ops,
  2145. .ct_group_ops = &f_midi2_ep_group_ops,
  2146. .ct_attrs = f_midi2_ep_attrs,
  2147. .ct_owner = THIS_MODULE,
  2148. };
  2149. /* create a f_midi2_ep_opts instance */
  2150. static int f_midi2_ep_opts_create(struct f_midi2_opts *opts,
  2151. unsigned int index,
  2152. struct f_midi2_ep_opts **ep_p)
  2153. {
  2154. struct f_midi2_ep_opts *ep_opts;
  2155. ep_opts = kzalloc(sizeof(*ep_opts), GFP_KERNEL);
  2156. if (!ep_opts)
  2157. return -ENOMEM;
  2158. ep_opts->opts = opts;
  2159. ep_opts->index = index;
  2160. /* set up the default values */
  2161. ep_opts->info.protocol = 2;
  2162. ep_opts->info.protocol_caps = 3;
  2163. opts->eps[index] = ep_opts;
  2164. *ep_p = ep_opts;
  2165. return 0;
  2166. }
  2167. /* make_group callback for an EP */
  2168. static struct config_group *
  2169. f_midi2_opts_ep_make(struct config_group *group, const char *name)
  2170. {
  2171. struct f_midi2_opts *opts;
  2172. struct f_midi2_ep_opts *ep_opts;
  2173. unsigned int index;
  2174. int ret;
  2175. if (strncmp(name, "ep.", 3))
  2176. return ERR_PTR(-EINVAL);
  2177. ret = kstrtouint(name + 3, 10, &index);
  2178. if (ret)
  2179. return ERR_PTR(ret);
  2180. opts = to_f_midi2_opts(&group->cg_item);
  2181. if (index >= MAX_UMP_EPS)
  2182. return ERR_PTR(-EINVAL);
  2183. if (opts->eps[index])
  2184. return ERR_PTR(-EBUSY);
  2185. ret = f_midi2_ep_opts_create(opts, index, &ep_opts);
  2186. if (ret)
  2187. return ERR_PTR(ret);
  2188. config_group_init_type_name(&ep_opts->group, name, &f_midi2_ep_type);
  2189. return &ep_opts->group;
  2190. }
  2191. /* drop_item callback for an EP */
  2192. static void
  2193. f_midi2_opts_ep_drop(struct config_group *group, struct config_item *item)
  2194. {
  2195. struct f_midi2_ep_opts *ep_opts = to_f_midi2_ep_opts(item);
  2196. mutex_lock(&ep_opts->opts->lock);
  2197. ep_opts->opts->eps[ep_opts->index] = NULL;
  2198. mutex_unlock(&ep_opts->opts->lock);
  2199. config_item_put(item);
  2200. }
  2201. /*
  2202. * Definitions for card config
  2203. */
  2204. /* define a bool option for card */
  2205. #define F_MIDI2_BOOL_OPT(name) \
  2206. static ssize_t f_midi2_opts_##name##_show(struct config_item *item, \
  2207. char *page) \
  2208. { \
  2209. struct f_midi2_opts *opts = to_f_midi2_opts(item); \
  2210. return f_midi2_opts_uint_show(opts, opts->info.name, \
  2211. "%u\n", page); \
  2212. } \
  2213. \
  2214. static ssize_t f_midi2_opts_##name##_store(struct config_item *item, \
  2215. const char *page, size_t len) \
  2216. { \
  2217. struct f_midi2_opts *opts = to_f_midi2_opts(item); \
  2218. return f_midi2_opts_bool_store(opts, &opts->info.name, \
  2219. page, len); \
  2220. } \
  2221. \
  2222. CONFIGFS_ATTR(f_midi2_opts_, name)
  2223. F_MIDI2_BOOL_OPT(process_ump);
  2224. F_MIDI2_BOOL_OPT(static_block);
  2225. static ssize_t f_midi2_opts_iface_name_show(struct config_item *item,
  2226. char *page)
  2227. {
  2228. struct f_midi2_opts *opts = to_f_midi2_opts(item);
  2229. return f_midi2_opts_str_show(opts, opts->info.iface_name, page);
  2230. }
  2231. static ssize_t f_midi2_opts_iface_name_store(struct config_item *item,
  2232. const char *page, size_t len)
  2233. {
  2234. struct f_midi2_opts *opts = to_f_midi2_opts(item);
  2235. return f_midi2_opts_str_store(opts, &opts->info.iface_name, 128,
  2236. page, len);
  2237. }
  2238. CONFIGFS_ATTR(f_midi2_opts_, iface_name);
  2239. static struct configfs_attribute *f_midi2_attrs[] = {
  2240. &f_midi2_opts_attr_process_ump,
  2241. &f_midi2_opts_attr_static_block,
  2242. &f_midi2_opts_attr_iface_name,
  2243. NULL
  2244. };
  2245. static void f_midi2_opts_release(struct config_item *item)
  2246. {
  2247. struct f_midi2_opts *opts = to_f_midi2_opts(item);
  2248. usb_put_function_instance(&opts->func_inst);
  2249. }
  2250. static struct configfs_item_operations f_midi2_item_ops = {
  2251. .release = f_midi2_opts_release,
  2252. };
  2253. static struct configfs_group_operations f_midi2_group_ops = {
  2254. .make_group = f_midi2_opts_ep_make,
  2255. .drop_item = f_midi2_opts_ep_drop,
  2256. };
  2257. static const struct config_item_type f_midi2_func_type = {
  2258. .ct_item_ops = &f_midi2_item_ops,
  2259. .ct_group_ops = &f_midi2_group_ops,
  2260. .ct_attrs = f_midi2_attrs,
  2261. .ct_owner = THIS_MODULE,
  2262. };
  2263. static void f_midi2_free_inst(struct usb_function_instance *f)
  2264. {
  2265. struct f_midi2_opts *opts;
  2266. opts = container_of(f, struct f_midi2_opts, func_inst);
  2267. kfree(opts->info.iface_name);
  2268. kfree(opts);
  2269. }
  2270. /* gadget alloc_inst */
  2271. static struct usb_function_instance *f_midi2_alloc_inst(void)
  2272. {
  2273. struct f_midi2_opts *opts;
  2274. struct f_midi2_ep_opts *ep_opts;
  2275. struct f_midi2_block_opts *block_opts;
  2276. int ret;
  2277. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  2278. if (!opts)
  2279. return ERR_PTR(-ENOMEM);
  2280. mutex_init(&opts->lock);
  2281. opts->func_inst.free_func_inst = f_midi2_free_inst;
  2282. opts->info.process_ump = true;
  2283. opts->info.static_block = true;
  2284. opts->info.num_reqs = 32;
  2285. opts->info.req_buf_size = 512;
  2286. /* create the default ep */
  2287. ret = f_midi2_ep_opts_create(opts, 0, &ep_opts);
  2288. if (ret) {
  2289. kfree(opts);
  2290. return ERR_PTR(ret);
  2291. }
  2292. /* create the default block */
  2293. ret = f_midi2_block_opts_create(ep_opts, 0, &block_opts);
  2294. if (ret) {
  2295. kfree(ep_opts);
  2296. kfree(opts);
  2297. return ERR_PTR(ret);
  2298. }
  2299. /* set up the default MIDI1 (that is mandatory) */
  2300. block_opts->info.midi1_num_groups = 1;
  2301. config_group_init_type_name(&opts->func_inst.group, "",
  2302. &f_midi2_func_type);
  2303. config_group_init_type_name(&ep_opts->group, "ep.0",
  2304. &f_midi2_ep_type);
  2305. configfs_add_default_group(&ep_opts->group, &opts->func_inst.group);
  2306. config_group_init_type_name(&block_opts->group, "block.0",
  2307. &f_midi2_block_type);
  2308. configfs_add_default_group(&block_opts->group, &ep_opts->group);
  2309. return &opts->func_inst;
  2310. }
  2311. static void do_f_midi2_free(struct f_midi2 *midi2, struct f_midi2_opts *opts)
  2312. {
  2313. mutex_lock(&opts->lock);
  2314. --opts->refcnt;
  2315. mutex_unlock(&opts->lock);
  2316. kfree(midi2->string_defs);
  2317. kfree(midi2);
  2318. }
  2319. static void f_midi2_free(struct usb_function *f)
  2320. {
  2321. do_f_midi2_free(func_to_midi2(f),
  2322. container_of(f->fi, struct f_midi2_opts, func_inst));
  2323. }
  2324. /* verify the parameters set up via configfs;
  2325. * return the number of EPs or a negative error
  2326. */
  2327. static int verify_parameters(struct f_midi2_opts *opts)
  2328. {
  2329. int i, j, num_eps, num_blks;
  2330. struct f_midi2_ep_info *ep;
  2331. struct f_midi2_block_info *bp;
  2332. for (num_eps = 0; num_eps < MAX_UMP_EPS && opts->eps[num_eps];
  2333. num_eps++)
  2334. ;
  2335. if (!num_eps) {
  2336. pr_err("f_midi2: No EP is defined\n");
  2337. return -EINVAL;
  2338. }
  2339. num_blks = 0;
  2340. for (i = 0; i < num_eps; i++) {
  2341. ep = &opts->eps[i]->info;
  2342. if (!(ep->protocol_caps & ep->protocol)) {
  2343. pr_err("f_midi2: Invalid protocol 0x%x (caps 0x%x) for EP %d\n",
  2344. ep->protocol, ep->protocol_caps, i);
  2345. return -EINVAL;
  2346. }
  2347. for (j = 0; j < SNDRV_UMP_MAX_BLOCKS && opts->eps[i]->blks[j];
  2348. j++, num_blks++) {
  2349. bp = &opts->eps[i]->blks[j]->info;
  2350. if (bp->first_group + bp->num_groups > SNDRV_UMP_MAX_GROUPS) {
  2351. pr_err("f_midi2: Invalid group definitions for block %d:%d\n",
  2352. i, j);
  2353. return -EINVAL;
  2354. }
  2355. if (bp->midi1_num_groups) {
  2356. if (bp->midi1_first_group < bp->first_group ||
  2357. bp->midi1_first_group + bp->midi1_num_groups >
  2358. bp->first_group + bp->num_groups) {
  2359. pr_err("f_midi2: Invalid MIDI1 group definitions for block %d:%d\n",
  2360. i, j);
  2361. return -EINVAL;
  2362. }
  2363. }
  2364. }
  2365. }
  2366. if (!num_blks) {
  2367. pr_err("f_midi2: No block is defined\n");
  2368. return -EINVAL;
  2369. }
  2370. return num_eps;
  2371. }
  2372. /* fill mapping between MIDI 1.0 cable and UMP EP/group */
  2373. static void fill_midi1_cable_mapping(struct f_midi2 *midi2,
  2374. struct f_midi2_ep *ep,
  2375. int blk)
  2376. {
  2377. const struct f_midi2_block_info *binfo = &ep->blks[blk].info;
  2378. struct midi1_cable_mapping *map;
  2379. int i, group;
  2380. if (!binfo->midi1_num_groups)
  2381. return;
  2382. if (binfo->direction != SNDRV_UMP_DIR_OUTPUT) {
  2383. group = binfo->midi1_first_group;
  2384. map = midi2->in_cable_mapping + midi2->num_midi1_in;
  2385. for (i = 0; i < binfo->midi1_num_groups; i++, group++, map++) {
  2386. if (midi2->num_midi1_in >= MAX_CABLES)
  2387. break;
  2388. map->ep = ep;
  2389. map->block = blk;
  2390. map->group = group;
  2391. midi2->num_midi1_in++;
  2392. /* store 1-based cable number */
  2393. ep->in_group_to_cable[group] = midi2->num_midi1_in;
  2394. }
  2395. }
  2396. if (binfo->direction != SNDRV_UMP_DIR_INPUT) {
  2397. group = binfo->midi1_first_group;
  2398. map = midi2->out_cable_mapping + midi2->num_midi1_out;
  2399. for (i = 0; i < binfo->midi1_num_groups; i++, group++, map++) {
  2400. if (midi2->num_midi1_out >= MAX_CABLES)
  2401. break;
  2402. map->ep = ep;
  2403. map->block = blk;
  2404. map->group = group;
  2405. midi2->num_midi1_out++;
  2406. }
  2407. }
  2408. }
  2409. /* gadget alloc callback */
  2410. static struct usb_function *f_midi2_alloc(struct usb_function_instance *fi)
  2411. {
  2412. struct f_midi2 *midi2;
  2413. struct f_midi2_opts *opts;
  2414. struct f_midi2_ep *ep;
  2415. struct f_midi2_block *bp;
  2416. int i, num_eps, blk;
  2417. midi2 = kzalloc(sizeof(*midi2), GFP_KERNEL);
  2418. if (!midi2)
  2419. return ERR_PTR(-ENOMEM);
  2420. opts = container_of(fi, struct f_midi2_opts, func_inst);
  2421. mutex_lock(&opts->lock);
  2422. num_eps = verify_parameters(opts);
  2423. if (num_eps < 0) {
  2424. mutex_unlock(&opts->lock);
  2425. kfree(midi2);
  2426. return ERR_PTR(num_eps);
  2427. }
  2428. ++opts->refcnt;
  2429. mutex_unlock(&opts->lock);
  2430. spin_lock_init(&midi2->queue_lock);
  2431. midi2->func.name = "midi2_func";
  2432. midi2->func.bind = f_midi2_bind;
  2433. midi2->func.unbind = f_midi2_unbind;
  2434. midi2->func.get_alt = f_midi2_get_alt;
  2435. midi2->func.set_alt = f_midi2_set_alt;
  2436. midi2->func.setup = f_midi2_setup;
  2437. midi2->func.disable = f_midi2_disable;
  2438. midi2->func.free_func = f_midi2_free;
  2439. midi2->info = opts->info;
  2440. midi2->num_eps = num_eps;
  2441. for (i = 0; i < num_eps; i++) {
  2442. ep = &midi2->midi2_eps[i];
  2443. ep->info = opts->eps[i]->info;
  2444. ep->card = midi2;
  2445. for (blk = 0; blk < SNDRV_UMP_MAX_BLOCKS &&
  2446. opts->eps[i]->blks[blk]; blk++) {
  2447. bp = &ep->blks[blk];
  2448. ep->num_blks++;
  2449. bp->info = opts->eps[i]->blks[blk]->info;
  2450. bp->gtb_id = ++midi2->total_blocks;
  2451. }
  2452. }
  2453. midi2->string_defs = kcalloc(midi2->total_blocks + 1,
  2454. sizeof(*midi2->string_defs), GFP_KERNEL);
  2455. if (!midi2->string_defs) {
  2456. do_f_midi2_free(midi2, opts);
  2457. return ERR_PTR(-ENOMEM);
  2458. }
  2459. if (opts->info.iface_name && *opts->info.iface_name)
  2460. midi2->string_defs[STR_IFACE].s = opts->info.iface_name;
  2461. else
  2462. midi2->string_defs[STR_IFACE].s = ump_ep_name(&midi2->midi2_eps[0]);
  2463. for (i = 0; i < midi2->num_eps; i++) {
  2464. ep = &midi2->midi2_eps[i];
  2465. for (blk = 0; blk < ep->num_blks; blk++) {
  2466. bp = &ep->blks[blk];
  2467. midi2->string_defs[gtb_to_str_id(bp->gtb_id)].s =
  2468. ump_fb_name(&bp->info);
  2469. fill_midi1_cable_mapping(midi2, ep, blk);
  2470. }
  2471. }
  2472. if (!midi2->num_midi1_in && !midi2->num_midi1_out) {
  2473. pr_err("f_midi2: MIDI1 definition is missing\n");
  2474. do_f_midi2_free(midi2, opts);
  2475. return ERR_PTR(-EINVAL);
  2476. }
  2477. return &midi2->func;
  2478. }
  2479. DECLARE_USB_FUNCTION_INIT(midi2, f_midi2_alloc_inst, f_midi2_alloc);
  2480. MODULE_DESCRIPTION("USB MIDI 2.0 class function driver");
  2481. MODULE_LICENSE("GPL");