f_mbim.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085
  1. /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/kernel.h>
  15. #include <linux/device.h>
  16. #include <linux/usb/cdc.h>
  17. #include <linux/usb/composite.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/spinlock.h>
  20. /*
  21. * This function is a "Mobile Broadband Interface Model" (MBIM) link.
  22. * MBIM is intended to be used with high-speed network attachments.
  23. *
  24. * Note that MBIM requires the use of "alternate settings" for its data
  25. * interface. This means that the set_alt() method has real work to do,
  26. * and also means that a get_alt() method is required.
  27. */
  28. #define MBIM_BULK_BUFFER_SIZE 4096
  29. #define MBIM_IOCTL_MAGIC 'o'
  30. #define MBIM_GET_NTB_SIZE _IOR(MBIM_IOCTL_MAGIC, 2, u32)
  31. #define MBIM_GET_DATAGRAM_COUNT _IOR(MBIM_IOCTL_MAGIC, 3, u16)
  32. #define NR_MBIM_PORTS 1
  33. /* ID for Microsoft OS String */
  34. #define MBIM_OS_STRING_ID 0xEE
  35. struct ctrl_pkt {
  36. void *buf;
  37. int len;
  38. struct list_head list;
  39. };
  40. struct mbim_ep_descs {
  41. struct usb_endpoint_descriptor *in;
  42. struct usb_endpoint_descriptor *out;
  43. struct usb_endpoint_descriptor *notify;
  44. };
  45. struct mbim_notify_port {
  46. struct usb_ep *notify;
  47. struct usb_request *notify_req;
  48. u8 notify_state;
  49. atomic_t notify_count;
  50. };
  51. enum mbim_notify_state {
  52. MBIM_NOTIFY_NONE,
  53. MBIM_NOTIFY_CONNECT,
  54. MBIM_NOTIFY_SPEED,
  55. MBIM_NOTIFY_RESPONSE_AVAILABLE,
  56. };
  57. struct f_mbim {
  58. struct usb_function function;
  59. struct usb_composite_dev *cdev;
  60. atomic_t online;
  61. atomic_t open_excl;
  62. atomic_t ioctl_excl;
  63. atomic_t read_excl;
  64. atomic_t write_excl;
  65. wait_queue_head_t read_wq;
  66. wait_queue_head_t write_wq;
  67. enum transport_type xport;
  68. u8 port_num;
  69. struct data_port bam_port;
  70. struct mbim_notify_port not_port;
  71. struct mbim_ep_descs fs;
  72. struct mbim_ep_descs hs;
  73. u8 ctrl_id, data_id;
  74. u8 data_alt_int;
  75. struct mbim_ndp_parser_opts *parser_opts;
  76. spinlock_t lock;
  77. struct list_head cpkt_req_q;
  78. struct list_head cpkt_resp_q;
  79. u32 ntb_input_size;
  80. u16 ntb_max_datagrams;
  81. atomic_t error;
  82. };
  83. struct mbim_ntb_input_size {
  84. u32 ntb_input_size;
  85. u16 ntb_max_datagrams;
  86. u16 reserved;
  87. };
  88. /* temporary variable used between mbim_open() and mbim_gadget_bind() */
  89. static struct f_mbim *_mbim_dev;
  90. static unsigned int nr_mbim_ports;
  91. static struct mbim_ports {
  92. struct f_mbim *port;
  93. unsigned port_num;
  94. } mbim_ports[NR_MBIM_PORTS];
  95. static inline struct f_mbim *func_to_mbim(struct usb_function *f)
  96. {
  97. return container_of(f, struct f_mbim, function);
  98. }
  99. /* peak (theoretical) bulk transfer rate in bits-per-second */
  100. static inline unsigned mbim_bitrate(struct usb_gadget *g)
  101. {
  102. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  103. return 13 * 512 * 8 * 1000 * 8;
  104. else
  105. return 19 * 64 * 1 * 1000 * 8;
  106. }
  107. /*-------------------------------------------------------------------------*/
  108. #define MBIM_NTB_DEFAULT_IN_SIZE (0x4000)
  109. #define MBIM_NTB_OUT_SIZE (0x1000)
  110. #define MBIM_NDP_IN_DIVISOR (0x4)
  111. #define NTB_DEFAULT_IN_SIZE_IPA (0x2000)
  112. #define MBIM_NTB_OUT_SIZE_IPA (0x2000)
  113. #define MBIM_FORMATS_SUPPORTED USB_CDC_NCM_NTB16_SUPPORTED
  114. static struct usb_cdc_ncm_ntb_parameters mbim_ntb_parameters = {
  115. .wLength = sizeof mbim_ntb_parameters,
  116. .bmNtbFormatsSupported = cpu_to_le16(MBIM_FORMATS_SUPPORTED),
  117. .dwNtbInMaxSize = cpu_to_le32(MBIM_NTB_DEFAULT_IN_SIZE),
  118. .wNdpInDivisor = cpu_to_le16(MBIM_NDP_IN_DIVISOR),
  119. .wNdpInPayloadRemainder = cpu_to_le16(0),
  120. .wNdpInAlignment = cpu_to_le16(4),
  121. .dwNtbOutMaxSize = cpu_to_le32(MBIM_NTB_OUT_SIZE),
  122. .wNdpOutDivisor = cpu_to_le16(4),
  123. .wNdpOutPayloadRemainder = cpu_to_le16(0),
  124. .wNdpOutAlignment = cpu_to_le16(4),
  125. .wNtbOutMaxDatagrams = 0,
  126. };
  127. /*
  128. * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
  129. * packet, to simplify cancellation; and a big transfer interval, to
  130. * waste less bandwidth.
  131. */
  132. #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
  133. #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
  134. static struct usb_interface_assoc_descriptor mbim_iad_desc = {
  135. .bLength = sizeof mbim_iad_desc,
  136. .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
  137. /* .bFirstInterface = DYNAMIC, */
  138. .bInterfaceCount = 2, /* control + data */
  139. .bFunctionClass = 2,
  140. .bFunctionSubClass = 0x0e,
  141. .bFunctionProtocol = 0,
  142. /* .iFunction = DYNAMIC */
  143. };
  144. /* interface descriptor: */
  145. static struct usb_interface_descriptor mbim_control_intf = {
  146. .bLength = sizeof mbim_control_intf,
  147. .bDescriptorType = USB_DT_INTERFACE,
  148. /* .bInterfaceNumber = DYNAMIC */
  149. .bNumEndpoints = 1,
  150. .bInterfaceClass = 0x02,
  151. .bInterfaceSubClass = 0x0e,
  152. .bInterfaceProtocol = 0,
  153. /* .iInterface = DYNAMIC */
  154. };
  155. static struct usb_cdc_header_desc mbim_header_desc = {
  156. .bLength = sizeof mbim_header_desc,
  157. .bDescriptorType = USB_DT_CS_INTERFACE,
  158. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  159. .bcdCDC = cpu_to_le16(0x0110),
  160. };
  161. static struct usb_cdc_union_desc mbim_union_desc = {
  162. .bLength = sizeof(mbim_union_desc),
  163. .bDescriptorType = USB_DT_CS_INTERFACE,
  164. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  165. /* .bMasterInterface0 = DYNAMIC */
  166. /* .bSlaveInterface0 = DYNAMIC */
  167. };
  168. static struct usb_cdc_mbb_desc mbb_desc = {
  169. .bLength = sizeof mbb_desc,
  170. .bDescriptorType = USB_DT_CS_INTERFACE,
  171. .bDescriptorSubType = USB_CDC_MBB_TYPE,
  172. .bcdMbbVersion = cpu_to_le16(0x0100),
  173. .wMaxControlMessage = cpu_to_le16(0x1000),
  174. .bNumberFilters = 0x20,
  175. .bMaxFilterSize = 0x80,
  176. .wMaxSegmentSize = cpu_to_le16(0xfe0),
  177. .bmNetworkCapabilities = 0x20,
  178. };
  179. static struct usb_cdc_ext_mbb_desc ext_mbb_desc = {
  180. .bLength = sizeof ext_mbb_desc,
  181. .bDescriptorType = USB_DT_CS_INTERFACE,
  182. .bDescriptorSubType = USB_CDC_EXT_MBB_TYPE,
  183. .bcdMbbExtendedVersion = cpu_to_le16(0x0100),
  184. .bMaxOutstandingCmdMsges = 64,
  185. .wMTU = 1500,
  186. };
  187. /* the default data interface has no endpoints ... */
  188. static struct usb_interface_descriptor mbim_data_nop_intf = {
  189. .bLength = sizeof mbim_data_nop_intf,
  190. .bDescriptorType = USB_DT_INTERFACE,
  191. /* .bInterfaceNumber = DYNAMIC */
  192. .bAlternateSetting = 0,
  193. .bNumEndpoints = 0,
  194. .bInterfaceClass = 0x0a,
  195. .bInterfaceSubClass = 0,
  196. .bInterfaceProtocol = 0x02,
  197. /* .iInterface = DYNAMIC */
  198. };
  199. /* ... but the "real" data interface has two bulk endpoints */
  200. static struct usb_interface_descriptor mbim_data_intf = {
  201. .bLength = sizeof mbim_data_intf,
  202. .bDescriptorType = USB_DT_INTERFACE,
  203. /* .bInterfaceNumber = DYNAMIC */
  204. .bAlternateSetting = 1,
  205. .bNumEndpoints = 2,
  206. .bInterfaceClass = 0x0a,
  207. .bInterfaceSubClass = 0,
  208. .bInterfaceProtocol = 0x02,
  209. /* .iInterface = DYNAMIC */
  210. };
  211. /* full speed support: */
  212. static struct usb_endpoint_descriptor fs_mbim_notify_desc = {
  213. .bLength = USB_DT_ENDPOINT_SIZE,
  214. .bDescriptorType = USB_DT_ENDPOINT,
  215. .bEndpointAddress = USB_DIR_IN,
  216. .bmAttributes = USB_ENDPOINT_XFER_INT,
  217. .wMaxPacketSize = 4*cpu_to_le16(NCM_STATUS_BYTECOUNT),
  218. .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
  219. };
  220. static struct usb_endpoint_descriptor fs_mbim_in_desc = {
  221. .bLength = USB_DT_ENDPOINT_SIZE,
  222. .bDescriptorType = USB_DT_ENDPOINT,
  223. .bEndpointAddress = USB_DIR_IN,
  224. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  225. };
  226. static struct usb_endpoint_descriptor fs_mbim_out_desc = {
  227. .bLength = USB_DT_ENDPOINT_SIZE,
  228. .bDescriptorType = USB_DT_ENDPOINT,
  229. .bEndpointAddress = USB_DIR_OUT,
  230. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  231. };
  232. static struct usb_descriptor_header *mbim_fs_function[] = {
  233. (struct usb_descriptor_header *) &mbim_iad_desc,
  234. /* MBIM control descriptors */
  235. (struct usb_descriptor_header *) &mbim_control_intf,
  236. (struct usb_descriptor_header *) &mbim_header_desc,
  237. (struct usb_descriptor_header *) &mbim_union_desc,
  238. (struct usb_descriptor_header *) &mbb_desc,
  239. (struct usb_descriptor_header *) &ext_mbb_desc,
  240. (struct usb_descriptor_header *) &fs_mbim_notify_desc,
  241. /* data interface, altsettings 0 and 1 */
  242. (struct usb_descriptor_header *) &mbim_data_nop_intf,
  243. (struct usb_descriptor_header *) &mbim_data_intf,
  244. (struct usb_descriptor_header *) &fs_mbim_in_desc,
  245. (struct usb_descriptor_header *) &fs_mbim_out_desc,
  246. NULL,
  247. };
  248. /* high speed support: */
  249. static struct usb_endpoint_descriptor hs_mbim_notify_desc = {
  250. .bLength = USB_DT_ENDPOINT_SIZE,
  251. .bDescriptorType = USB_DT_ENDPOINT,
  252. .bEndpointAddress = USB_DIR_IN,
  253. .bmAttributes = USB_ENDPOINT_XFER_INT,
  254. .wMaxPacketSize = 4*cpu_to_le16(NCM_STATUS_BYTECOUNT),
  255. .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
  256. };
  257. static struct usb_endpoint_descriptor hs_mbim_in_desc = {
  258. .bLength = USB_DT_ENDPOINT_SIZE,
  259. .bDescriptorType = USB_DT_ENDPOINT,
  260. .bEndpointAddress = USB_DIR_IN,
  261. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  262. .wMaxPacketSize = cpu_to_le16(512),
  263. };
  264. static struct usb_endpoint_descriptor hs_mbim_out_desc = {
  265. .bLength = USB_DT_ENDPOINT_SIZE,
  266. .bDescriptorType = USB_DT_ENDPOINT,
  267. .bEndpointAddress = USB_DIR_OUT,
  268. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  269. .wMaxPacketSize = cpu_to_le16(512),
  270. };
  271. static struct usb_descriptor_header *mbim_hs_function[] = {
  272. (struct usb_descriptor_header *) &mbim_iad_desc,
  273. /* MBIM control descriptors */
  274. (struct usb_descriptor_header *) &mbim_control_intf,
  275. (struct usb_descriptor_header *) &mbim_header_desc,
  276. (struct usb_descriptor_header *) &mbim_union_desc,
  277. (struct usb_descriptor_header *) &mbb_desc,
  278. (struct usb_descriptor_header *) &ext_mbb_desc,
  279. (struct usb_descriptor_header *) &hs_mbim_notify_desc,
  280. /* data interface, altsettings 0 and 1 */
  281. (struct usb_descriptor_header *) &mbim_data_nop_intf,
  282. (struct usb_descriptor_header *) &mbim_data_intf,
  283. (struct usb_descriptor_header *) &hs_mbim_in_desc,
  284. (struct usb_descriptor_header *) &hs_mbim_out_desc,
  285. NULL,
  286. };
  287. /* Super Speed Support */
  288. static struct usb_endpoint_descriptor ss_mbim_notify_desc = {
  289. .bLength = USB_DT_ENDPOINT_SIZE,
  290. .bDescriptorType = USB_DT_ENDPOINT,
  291. .bEndpointAddress = USB_DIR_IN,
  292. .bmAttributes = USB_ENDPOINT_XFER_INT,
  293. .wMaxPacketSize = 4*cpu_to_le16(NCM_STATUS_BYTECOUNT),
  294. .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
  295. };
  296. static struct usb_ss_ep_comp_descriptor ss_mbim_notify_comp_desc = {
  297. .bLength = sizeof(ss_mbim_notify_comp_desc),
  298. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  299. /* the following 3 values can be tweaked if necessary */
  300. /* .bMaxBurst = 0, */
  301. /* .bmAttributes = 0, */
  302. .wBytesPerInterval = 4*cpu_to_le16(NCM_STATUS_BYTECOUNT),
  303. };
  304. static struct usb_endpoint_descriptor ss_mbim_in_desc = {
  305. .bLength = USB_DT_ENDPOINT_SIZE,
  306. .bDescriptorType = USB_DT_ENDPOINT,
  307. .bEndpointAddress = USB_DIR_IN,
  308. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  309. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  310. };
  311. static struct usb_ss_ep_comp_descriptor ss_mbim_in_comp_desc = {
  312. .bLength = sizeof(ss_mbim_in_comp_desc),
  313. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  314. /* the following 2 values can be tweaked if necessary */
  315. /* .bMaxBurst = 0, */
  316. /* .bmAttributes = 0, */
  317. };
  318. static struct usb_endpoint_descriptor ss_mbim_out_desc = {
  319. .bLength = USB_DT_ENDPOINT_SIZE,
  320. .bDescriptorType = USB_DT_ENDPOINT,
  321. .bEndpointAddress = USB_DIR_OUT,
  322. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  323. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  324. };
  325. static struct usb_ss_ep_comp_descriptor ss_mbim_out_comp_desc = {
  326. .bLength = sizeof(ss_mbim_out_comp_desc),
  327. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  328. /* the following 2 values can be tweaked if necessary */
  329. /* .bMaxBurst = 0, */
  330. /* .bmAttributes = 0, */
  331. };
  332. static struct usb_descriptor_header *mbim_ss_function[] = {
  333. (struct usb_descriptor_header *) &mbim_iad_desc,
  334. /* MBIM control descriptors */
  335. (struct usb_descriptor_header *) &mbim_control_intf,
  336. (struct usb_descriptor_header *) &mbim_header_desc,
  337. (struct usb_descriptor_header *) &mbim_union_desc,
  338. (struct usb_descriptor_header *) &mbb_desc,
  339. (struct usb_descriptor_header *) &ext_mbb_desc,
  340. (struct usb_descriptor_header *) &ss_mbim_notify_desc,
  341. (struct usb_descriptor_header *) &ss_mbim_notify_comp_desc,
  342. /* data interface, altsettings 0 and 1 */
  343. (struct usb_descriptor_header *) &mbim_data_nop_intf,
  344. (struct usb_descriptor_header *) &mbim_data_intf,
  345. (struct usb_descriptor_header *) &ss_mbim_in_desc,
  346. (struct usb_descriptor_header *) &ss_mbim_in_comp_desc,
  347. (struct usb_descriptor_header *) &ss_mbim_out_desc,
  348. (struct usb_descriptor_header *) &ss_mbim_out_comp_desc,
  349. NULL,
  350. };
  351. /* string descriptors: */
  352. #define STRING_CTRL_IDX 0
  353. #define STRING_DATA_IDX 1
  354. static struct usb_string mbim_string_defs[] = {
  355. [STRING_CTRL_IDX].s = "MBIM Control",
  356. [STRING_DATA_IDX].s = "MBIM Data",
  357. { } /* end of list */
  358. };
  359. static struct usb_gadget_strings mbim_string_table = {
  360. .language = 0x0409, /* en-us */
  361. .strings = mbim_string_defs,
  362. };
  363. static struct usb_gadget_strings *mbim_strings[] = {
  364. &mbim_string_table,
  365. NULL,
  366. };
  367. /* Microsoft OS Descriptors */
  368. /*
  369. * We specify our own bMS_VendorCode byte which Windows will use
  370. * as the bRequest value in subsequent device get requests.
  371. */
  372. #define MBIM_VENDOR_CODE 0xA5
  373. /* Microsoft OS String */
  374. static u8 mbim_os_string[] = {
  375. 18, /* sizeof(mtp_os_string) */
  376. USB_DT_STRING,
  377. /* Signature field: "MSFT100" */
  378. 'M', 0, 'S', 0, 'F', 0, 'T', 0, '1', 0, '0', 0, '0', 0,
  379. /* vendor code */
  380. MBIM_VENDOR_CODE,
  381. /* padding */
  382. 0
  383. };
  384. /* Microsoft Extended Configuration Descriptor Header Section */
  385. struct mbim_ext_config_desc_header {
  386. __le32 dwLength;
  387. __u16 bcdVersion;
  388. __le16 wIndex;
  389. __u8 bCount;
  390. __u8 reserved[7];
  391. };
  392. /* Microsoft Extended Configuration Descriptor Function Section */
  393. struct mbim_ext_config_desc_function {
  394. __u8 bFirstInterfaceNumber;
  395. __u8 bInterfaceCount;
  396. __u8 compatibleID[8];
  397. __u8 subCompatibleID[8];
  398. __u8 reserved[6];
  399. };
  400. /* Microsoft Extended Configuration Descriptor */
  401. static struct {
  402. struct mbim_ext_config_desc_header header;
  403. struct mbim_ext_config_desc_function function;
  404. } mbim_ext_config_desc = {
  405. .header = {
  406. .dwLength = __constant_cpu_to_le32(sizeof mbim_ext_config_desc),
  407. .bcdVersion = __constant_cpu_to_le16(0x0100),
  408. .wIndex = __constant_cpu_to_le16(4),
  409. .bCount = 1,
  410. },
  411. .function = {
  412. .bFirstInterfaceNumber = 0,
  413. .bInterfaceCount = 1,
  414. .compatibleID = { 'A', 'L', 'T', 'R', 'C', 'F', 'G' },
  415. /* .subCompatibleID = DYNAMIC */
  416. },
  417. };
  418. /*
  419. * Here are options for the Datagram Pointer table (NDP) parser.
  420. * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
  421. * in NDP16 offsets and sizes fields are 1 16bit word wide,
  422. * in NDP32 -- 2 16bit words wide. Also signatures are different.
  423. * To make the parser code the same, put the differences in the structure,
  424. * and switch pointers to the structures when the format is changed.
  425. */
  426. struct mbim_ndp_parser_opts {
  427. u32 nth_sign;
  428. u32 ndp_sign;
  429. unsigned nth_size;
  430. unsigned ndp_size;
  431. unsigned ndplen_align;
  432. /* sizes in u16 units */
  433. unsigned dgram_item_len; /* index or length */
  434. unsigned block_length;
  435. unsigned fp_index;
  436. unsigned reserved1;
  437. unsigned reserved2;
  438. unsigned next_fp_index;
  439. };
  440. #define INIT_NDP16_OPTS { \
  441. .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
  442. .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
  443. .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
  444. .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
  445. .ndplen_align = 4, \
  446. .dgram_item_len = 1, \
  447. .block_length = 1, \
  448. .fp_index = 1, \
  449. .reserved1 = 0, \
  450. .reserved2 = 0, \
  451. .next_fp_index = 1, \
  452. }
  453. #define INIT_NDP32_OPTS { \
  454. .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
  455. .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
  456. .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
  457. .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
  458. .ndplen_align = 8, \
  459. .dgram_item_len = 2, \
  460. .block_length = 2, \
  461. .fp_index = 2, \
  462. .reserved1 = 1, \
  463. .reserved2 = 2, \
  464. .next_fp_index = 2, \
  465. }
  466. static struct mbim_ndp_parser_opts mbim_ndp16_opts = INIT_NDP16_OPTS;
  467. static struct mbim_ndp_parser_opts mbim_ndp32_opts = INIT_NDP32_OPTS;
  468. static inline int mbim_lock(atomic_t *excl)
  469. {
  470. if (atomic_inc_return(excl) == 1) {
  471. return 0;
  472. } else {
  473. atomic_dec(excl);
  474. return -EBUSY;
  475. }
  476. }
  477. static inline void mbim_unlock(atomic_t *excl)
  478. {
  479. atomic_dec(excl);
  480. }
  481. static struct ctrl_pkt *mbim_alloc_ctrl_pkt(unsigned len, gfp_t flags)
  482. {
  483. struct ctrl_pkt *pkt;
  484. pkt = kzalloc(sizeof(struct ctrl_pkt), flags);
  485. if (!pkt)
  486. return ERR_PTR(-ENOMEM);
  487. pkt->buf = kmalloc(len, flags);
  488. if (!pkt->buf) {
  489. kfree(pkt);
  490. return ERR_PTR(-ENOMEM);
  491. }
  492. pkt->len = len;
  493. return pkt;
  494. }
  495. static void mbim_free_ctrl_pkt(struct ctrl_pkt *pkt)
  496. {
  497. if (pkt) {
  498. kfree(pkt->buf);
  499. kfree(pkt);
  500. }
  501. }
  502. static struct usb_request *mbim_alloc_req(struct usb_ep *ep, int buffer_size)
  503. {
  504. struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
  505. if (!req)
  506. return NULL;
  507. req->buf = kmalloc(buffer_size, GFP_KERNEL);
  508. if (!req->buf) {
  509. usb_ep_free_request(ep, req);
  510. return NULL;
  511. }
  512. req->length = buffer_size;
  513. return req;
  514. }
  515. void fmbim_free_req(struct usb_ep *ep, struct usb_request *req)
  516. {
  517. if (req) {
  518. kfree(req->buf);
  519. usb_ep_free_request(ep, req);
  520. }
  521. }
  522. static void fmbim_ctrl_response_available(struct f_mbim *dev)
  523. {
  524. struct usb_request *req = dev->not_port.notify_req;
  525. struct usb_cdc_notification *event = NULL;
  526. unsigned long flags;
  527. int ret;
  528. pr_debug("dev:%pK portno#%d\n", dev, dev->port_num);
  529. spin_lock_irqsave(&dev->lock, flags);
  530. if (!atomic_read(&dev->online)) {
  531. pr_err("dev:%pK is not online\n", dev);
  532. spin_unlock_irqrestore(&dev->lock, flags);
  533. return;
  534. }
  535. if (!req) {
  536. pr_err("dev:%pK req is NULL\n", dev);
  537. spin_unlock_irqrestore(&dev->lock, flags);
  538. return;
  539. }
  540. if (!req->buf) {
  541. pr_err("dev:%pK req->buf is NULL\n", dev);
  542. spin_unlock_irqrestore(&dev->lock, flags);
  543. return;
  544. }
  545. if (atomic_inc_return(&dev->not_port.notify_count) != 1) {
  546. pr_debug("delay ep_queue: notifications queue is busy[%d]",
  547. atomic_read(&dev->not_port.notify_count));
  548. spin_unlock_irqrestore(&dev->lock, flags);
  549. return;
  550. }
  551. req->length = sizeof *event;
  552. event = req->buf;
  553. event->bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
  554. | USB_RECIP_INTERFACE;
  555. event->bNotificationType = USB_CDC_NOTIFY_RESPONSE_AVAILABLE;
  556. event->wValue = cpu_to_le16(0);
  557. event->wIndex = cpu_to_le16(dev->ctrl_id);
  558. event->wLength = cpu_to_le16(0);
  559. spin_unlock_irqrestore(&dev->lock, flags);
  560. ret = usb_ep_queue(dev->not_port.notify,
  561. req, GFP_ATOMIC);
  562. if (ret) {
  563. atomic_dec(&dev->not_port.notify_count);
  564. pr_err("ep enqueue error %d\n", ret);
  565. }
  566. pr_debug("Successful Exit");
  567. }
  568. static int
  569. fmbim_send_cpkt_response(struct f_mbim *gr, struct ctrl_pkt *cpkt)
  570. {
  571. struct f_mbim *dev = gr;
  572. unsigned long flags;
  573. if (!gr || !cpkt) {
  574. pr_err("Invalid cpkt, dev:%pK cpkt:%pK\n",
  575. gr, cpkt);
  576. return -ENODEV;
  577. }
  578. pr_debug("dev:%pK port_num#%d\n", dev, dev->port_num);
  579. if (!atomic_read(&dev->online)) {
  580. pr_err("dev:%pK is not connected\n", dev);
  581. mbim_free_ctrl_pkt(cpkt);
  582. return 0;
  583. }
  584. if (dev->not_port.notify_state != MBIM_NOTIFY_RESPONSE_AVAILABLE) {
  585. pr_err("dev:%pK state=%d, recover!!\n", dev,
  586. dev->not_port.notify_state);
  587. mbim_free_ctrl_pkt(cpkt);
  588. return 0;
  589. }
  590. spin_lock_irqsave(&dev->lock, flags);
  591. list_add_tail(&cpkt->list, &dev->cpkt_resp_q);
  592. spin_unlock_irqrestore(&dev->lock, flags);
  593. fmbim_ctrl_response_available(dev);
  594. return 0;
  595. }
  596. /* ---------------------------- BAM INTERFACE ----------------------------- */
  597. static int mbim_bam_setup(int no_ports)
  598. {
  599. int ret;
  600. pr_info("no_ports:%d\n", no_ports);
  601. ret = bam_data_setup(no_ports);
  602. if (ret) {
  603. pr_err("bam_data_setup failed err: %d\n", ret);
  604. return ret;
  605. }
  606. pr_info("Initialized %d ports\n", no_ports);
  607. return 0;
  608. }
  609. int mbim_configure_params(void)
  610. {
  611. struct teth_aggr_params aggr_params;
  612. int ret = 0;
  613. aggr_params.dl.aggr_prot = TETH_AGGR_PROTOCOL_MBIM;
  614. aggr_params.dl.max_datagrams = mbim_ntb_parameters.wNtbOutMaxDatagrams;
  615. aggr_params.dl.max_transfer_size_byte =
  616. mbim_ntb_parameters.dwNtbInMaxSize;
  617. aggr_params.ul.aggr_prot = TETH_AGGR_PROTOCOL_MBIM;
  618. aggr_params.ul.max_datagrams = mbim_ntb_parameters.wNtbOutMaxDatagrams;
  619. aggr_params.ul.max_transfer_size_byte =
  620. mbim_ntb_parameters.dwNtbOutMaxSize;
  621. ret = teth_bridge_set_aggr_params(&aggr_params);
  622. if (ret)
  623. pr_err("%s: teth_bridge_set_aggr_params failed\n", __func__);
  624. return ret;
  625. }
  626. static int mbim_bam_connect(struct f_mbim *dev)
  627. {
  628. int ret;
  629. u8 src_connection_idx, dst_connection_idx;
  630. struct usb_gadget *gadget = dev->cdev->gadget;
  631. enum peer_bam bam_name = (dev->xport == USB_GADGET_XPORT_BAM2BAM_IPA) ?
  632. IPA_P_BAM : A2_P_BAM;
  633. pr_info("dev:%pK portno:%d\n", dev, dev->port_num);
  634. src_connection_idx = usb_bam_get_connection_idx(gadget->name, bam_name,
  635. USB_TO_PEER_PERIPHERAL, dev->port_num);
  636. dst_connection_idx = usb_bam_get_connection_idx(gadget->name, bam_name,
  637. PEER_PERIPHERAL_TO_USB, dev->port_num);
  638. if (src_connection_idx < 0 || dst_connection_idx < 0) {
  639. pr_err("%s: usb_bam_get_connection_idx failed\n", __func__);
  640. return ret;
  641. }
  642. ret = bam_data_connect(&dev->bam_port, dev->port_num,
  643. dev->xport, src_connection_idx, dst_connection_idx,
  644. USB_FUNC_MBIM);
  645. if (ret) {
  646. pr_err("bam_data_setup failed: err:%d\n",
  647. ret);
  648. return ret;
  649. }
  650. pr_info("mbim bam connected\n");
  651. return 0;
  652. }
  653. static int mbim_bam_disconnect(struct f_mbim *dev)
  654. {
  655. pr_info("dev:%pK port:%d. Do nothing.\n",
  656. dev, dev->port_num);
  657. bam_data_disconnect(&dev->bam_port, dev->port_num);
  658. return 0;
  659. }
  660. /* -------------------------------------------------------------------------*/
  661. static inline void mbim_reset_values(struct f_mbim *mbim)
  662. {
  663. mbim->parser_opts = &mbim_ndp16_opts;
  664. mbim->ntb_input_size = MBIM_NTB_DEFAULT_IN_SIZE;
  665. atomic_set(&mbim->online, 0);
  666. }
  667. static void mbim_reset_function_queue(struct f_mbim *dev)
  668. {
  669. struct ctrl_pkt *cpkt = NULL;
  670. pr_debug("Queue empty packet for QBI");
  671. spin_lock(&dev->lock);
  672. cpkt = mbim_alloc_ctrl_pkt(0, GFP_ATOMIC);
  673. if (!cpkt) {
  674. pr_err("%s: Unable to allocate reset function pkt\n", __func__);
  675. spin_unlock(&dev->lock);
  676. return;
  677. }
  678. list_add_tail(&cpkt->list, &dev->cpkt_req_q);
  679. spin_unlock(&dev->lock);
  680. pr_debug("%s: Wake up read queue", __func__);
  681. wake_up(&dev->read_wq);
  682. }
  683. static void fmbim_reset_cmd_complete(struct usb_ep *ep, struct usb_request *req)
  684. {
  685. struct f_mbim *dev = req->context;
  686. mbim_reset_function_queue(dev);
  687. }
  688. static void mbim_clear_queues(struct f_mbim *mbim)
  689. {
  690. struct ctrl_pkt *cpkt = NULL;
  691. struct list_head *act, *tmp;
  692. spin_lock(&mbim->lock);
  693. list_for_each_safe(act, tmp, &mbim->cpkt_req_q) {
  694. cpkt = list_entry(act, struct ctrl_pkt, list);
  695. list_del(&cpkt->list);
  696. mbim_free_ctrl_pkt(cpkt);
  697. }
  698. list_for_each_safe(act, tmp, &mbim->cpkt_resp_q) {
  699. cpkt = list_entry(act, struct ctrl_pkt, list);
  700. list_del(&cpkt->list);
  701. mbim_free_ctrl_pkt(cpkt);
  702. }
  703. spin_unlock(&mbim->lock);
  704. }
  705. /*
  706. * Context: mbim->lock held
  707. */
  708. static void mbim_do_notify(struct f_mbim *mbim)
  709. {
  710. struct usb_request *req = mbim->not_port.notify_req;
  711. struct usb_cdc_notification *event;
  712. int status;
  713. pr_debug("notify_state: %d", mbim->not_port.notify_state);
  714. if (!req)
  715. return;
  716. event = req->buf;
  717. switch (mbim->not_port.notify_state) {
  718. case MBIM_NOTIFY_NONE:
  719. if (atomic_read(&mbim->not_port.notify_count) > 0)
  720. pr_err("Pending notifications in MBIM_NOTIFY_NONE\n");
  721. else
  722. pr_debug("No pending notifications\n");
  723. return;
  724. case MBIM_NOTIFY_RESPONSE_AVAILABLE:
  725. pr_debug("Notification %02x sent\n", event->bNotificationType);
  726. if (atomic_read(&mbim->not_port.notify_count) <= 0) {
  727. pr_debug("notify_response_avaliable: done");
  728. return;
  729. }
  730. spin_unlock(&mbim->lock);
  731. status = usb_ep_queue(mbim->not_port.notify, req, GFP_ATOMIC);
  732. spin_lock(&mbim->lock);
  733. if (status) {
  734. atomic_dec(&mbim->not_port.notify_count);
  735. pr_err("Queue notify request failed, err: %d", status);
  736. }
  737. return;
  738. }
  739. event->bmRequestType = 0xA1;
  740. event->wIndex = cpu_to_le16(mbim->ctrl_id);
  741. /*
  742. * In double buffering if there is a space in FIFO,
  743. * completion callback can be called right after the call,
  744. * so unlocking
  745. */
  746. atomic_inc(&mbim->not_port.notify_count);
  747. pr_debug("queue request: notify_count = %d",
  748. atomic_read(&mbim->not_port.notify_count));
  749. spin_unlock(&mbim->lock);
  750. status = usb_ep_queue(mbim->not_port.notify, req, GFP_ATOMIC);
  751. spin_lock(&mbim->lock);
  752. if (status) {
  753. atomic_dec(&mbim->not_port.notify_count);
  754. pr_err("usb_ep_queue failed, err: %d", status);
  755. }
  756. }
  757. static void mbim_notify_complete(struct usb_ep *ep, struct usb_request *req)
  758. {
  759. struct f_mbim *mbim = req->context;
  760. struct usb_cdc_notification *event = req->buf;
  761. spin_lock(&mbim->lock);
  762. switch (req->status) {
  763. case 0:
  764. atomic_dec(&mbim->not_port.notify_count);
  765. pr_debug("notify_count = %d",
  766. atomic_read(&mbim->not_port.notify_count));
  767. break;
  768. case -ECONNRESET:
  769. case -ESHUTDOWN:
  770. /* connection gone */
  771. mbim->not_port.notify_state = MBIM_NOTIFY_NONE;
  772. atomic_set(&mbim->not_port.notify_count, 0);
  773. pr_info("ESHUTDOWN/ECONNRESET, connection gone");
  774. spin_unlock(&mbim->lock);
  775. mbim_clear_queues(mbim);
  776. mbim_reset_function_queue(mbim);
  777. spin_lock(&mbim->lock);
  778. break;
  779. default:
  780. pr_err("Unknown event %02x --> %d\n",
  781. event->bNotificationType, req->status);
  782. break;
  783. }
  784. mbim_do_notify(mbim);
  785. spin_unlock(&mbim->lock);
  786. pr_debug("%s: Exit\n", __func__);
  787. }
  788. static void mbim_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
  789. {
  790. /* now for SET_NTB_INPUT_SIZE only */
  791. unsigned in_size = 0;
  792. struct usb_function *f = req->context;
  793. struct f_mbim *mbim = func_to_mbim(f);
  794. struct mbim_ntb_input_size *ntb = NULL;
  795. req->context = NULL;
  796. if (req->status || req->actual != req->length) {
  797. pr_err("Bad control-OUT transfer\n");
  798. goto invalid;
  799. }
  800. if (req->length == 4) {
  801. in_size = get_unaligned_le32(req->buf);
  802. if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
  803. in_size > le32_to_cpu(mbim_ntb_parameters.dwNtbInMaxSize)) {
  804. pr_err("Illegal INPUT SIZE (%d) from host\n", in_size);
  805. goto invalid;
  806. }
  807. } else if (req->length == 8) {
  808. ntb = (struct mbim_ntb_input_size *)req->buf;
  809. in_size = get_unaligned_le32(&(ntb->ntb_input_size));
  810. if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
  811. in_size > le32_to_cpu(mbim_ntb_parameters.dwNtbInMaxSize)) {
  812. pr_err("Illegal INPUT SIZE (%d) from host\n", in_size);
  813. goto invalid;
  814. }
  815. mbim->ntb_max_datagrams =
  816. get_unaligned_le16(&(ntb->ntb_max_datagrams));
  817. } else {
  818. pr_err("Illegal NTB length %d\n", in_size);
  819. goto invalid;
  820. }
  821. pr_debug("Set NTB INPUT SIZE %d\n", in_size);
  822. mbim->ntb_input_size = in_size;
  823. return;
  824. invalid:
  825. usb_ep_set_halt(ep);
  826. pr_err("%s: Failed\n", __func__);
  827. return;
  828. }
  829. static void
  830. fmbim_cmd_complete(struct usb_ep *ep, struct usb_request *req)
  831. {
  832. struct f_mbim *dev = req->context;
  833. struct ctrl_pkt *cpkt = NULL;
  834. int len = req->actual;
  835. if (!dev) {
  836. pr_err("mbim dev is null\n");
  837. return;
  838. }
  839. if (req->status < 0) {
  840. pr_err("mbim command error %d\n", req->status);
  841. return;
  842. }
  843. pr_debug("dev:%pK port#%d\n", dev, dev->port_num);
  844. cpkt = mbim_alloc_ctrl_pkt(len, GFP_ATOMIC);
  845. if (!cpkt) {
  846. pr_err("Unable to allocate ctrl pkt\n");
  847. return;
  848. }
  849. pr_debug("Add to cpkt_req_q packet with len = %d\n", len);
  850. memcpy(cpkt->buf, req->buf, len);
  851. spin_lock(&dev->lock);
  852. list_add_tail(&cpkt->list, &dev->cpkt_req_q);
  853. spin_unlock(&dev->lock);
  854. /* wakeup read thread */
  855. pr_debug("Wake up read queue");
  856. wake_up(&dev->read_wq);
  857. return;
  858. }
  859. static int
  860. mbim_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
  861. {
  862. struct f_mbim *mbim = func_to_mbim(f);
  863. struct usb_composite_dev *cdev = mbim->cdev;
  864. struct usb_request *req = cdev->req;
  865. struct ctrl_pkt *cpkt = NULL;
  866. int value = -EOPNOTSUPP;
  867. u16 w_index = le16_to_cpu(ctrl->wIndex);
  868. u16 w_value = le16_to_cpu(ctrl->wValue);
  869. u16 w_length = le16_to_cpu(ctrl->wLength);
  870. /*
  871. * composite driver infrastructure handles everything except
  872. * CDC class messages; interface activation uses set_alt().
  873. */
  874. if (!atomic_read(&mbim->online)) {
  875. pr_info("usb cable is not connected\n");
  876. return -ENOTCONN;
  877. }
  878. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  879. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  880. | USB_CDC_RESET_FUNCTION:
  881. pr_debug("USB_CDC_RESET_FUNCTION");
  882. value = 0;
  883. req->complete = fmbim_reset_cmd_complete;
  884. req->context = mbim;
  885. break;
  886. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  887. | USB_CDC_SEND_ENCAPSULATED_COMMAND:
  888. pr_debug("USB_CDC_SEND_ENCAPSULATED_COMMAND");
  889. if (w_length > req->length) {
  890. pr_debug("w_length > req->length: %d > %d",
  891. w_length, req->length);
  892. }
  893. value = w_length;
  894. req->complete = fmbim_cmd_complete;
  895. req->context = mbim;
  896. break;
  897. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  898. | USB_CDC_GET_ENCAPSULATED_RESPONSE:
  899. pr_debug("USB_CDC_GET_ENCAPSULATED_RESPONSE");
  900. if (w_value) {
  901. pr_err("w_length > 0: %d", w_length);
  902. break;
  903. }
  904. pr_debug("req%02x.%02x v%04x i%04x l%d\n",
  905. ctrl->bRequestType, ctrl->bRequest,
  906. w_value, w_index, w_length);
  907. spin_lock(&mbim->lock);
  908. if (list_empty(&mbim->cpkt_resp_q)) {
  909. pr_err("ctrl resp queue empty\n");
  910. spin_unlock(&mbim->lock);
  911. break;
  912. }
  913. cpkt = list_first_entry(&mbim->cpkt_resp_q,
  914. struct ctrl_pkt, list);
  915. list_del(&cpkt->list);
  916. spin_unlock(&mbim->lock);
  917. value = min_t(unsigned, w_length, cpkt->len);
  918. memcpy(req->buf, cpkt->buf, value);
  919. mbim_free_ctrl_pkt(cpkt);
  920. pr_debug("copied encapsulated_response %d bytes",
  921. value);
  922. break;
  923. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  924. | USB_CDC_GET_NTB_PARAMETERS:
  925. pr_debug("USB_CDC_GET_NTB_PARAMETERS");
  926. if (w_length == 0 || w_value != 0 || w_index != mbim->ctrl_id)
  927. break;
  928. value = w_length > sizeof mbim_ntb_parameters ?
  929. sizeof mbim_ntb_parameters : w_length;
  930. memcpy(req->buf, &mbim_ntb_parameters, value);
  931. break;
  932. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  933. | USB_CDC_GET_NTB_INPUT_SIZE:
  934. pr_debug("USB_CDC_GET_NTB_INPUT_SIZE");
  935. if (w_length < 4 || w_value != 0 || w_index != mbim->ctrl_id)
  936. break;
  937. put_unaligned_le32(mbim->ntb_input_size, req->buf);
  938. value = 4;
  939. pr_debug("Reply to host INPUT SIZE %d\n",
  940. mbim->ntb_input_size);
  941. break;
  942. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  943. | USB_CDC_SET_NTB_INPUT_SIZE:
  944. pr_debug("USB_CDC_SET_NTB_INPUT_SIZE");
  945. if (w_length != 4 && w_length != 8) {
  946. pr_err("wrong NTB length %d", w_length);
  947. break;
  948. }
  949. if (w_value != 0 || w_index != mbim->ctrl_id)
  950. break;
  951. req->complete = mbim_ep0out_complete;
  952. req->length = w_length;
  953. req->context = f;
  954. value = req->length;
  955. break;
  956. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  957. | USB_CDC_GET_NTB_FORMAT:
  958. {
  959. uint16_t format;
  960. pr_debug("USB_CDC_GET_NTB_FORMAT");
  961. if (w_length < 2 || w_value != 0 || w_index != mbim->ctrl_id)
  962. break;
  963. format = (mbim->parser_opts == &mbim_ndp16_opts) ? 0 : 1;
  964. put_unaligned_le16(format, req->buf);
  965. value = 2;
  966. pr_debug("NTB FORMAT: sending %d\n", format);
  967. break;
  968. }
  969. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
  970. | USB_CDC_SET_NTB_FORMAT:
  971. {
  972. pr_debug("USB_CDC_SET_NTB_FORMAT");
  973. if (w_length != 0 || w_index != mbim->ctrl_id)
  974. break;
  975. switch (w_value) {
  976. case 0x0000:
  977. mbim->parser_opts = &mbim_ndp16_opts;
  978. pr_debug("NCM16 selected\n");
  979. break;
  980. case 0x0001:
  981. mbim->parser_opts = &mbim_ndp32_opts;
  982. pr_debug("NCM32 selected\n");
  983. break;
  984. default:
  985. break;
  986. }
  987. value = 0;
  988. break;
  989. }
  990. /* optional in mbim descriptor: */
  991. /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
  992. /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
  993. default:
  994. pr_err("invalid control req: %02x.%02x v%04x i%04x l%d\n",
  995. ctrl->bRequestType, ctrl->bRequest,
  996. w_value, w_index, w_length);
  997. }
  998. /* respond with data transfer or status phase? */
  999. if (value >= 0) {
  1000. pr_debug("control request: %02x.%02x v%04x i%04x l%d\n",
  1001. ctrl->bRequestType, ctrl->bRequest,
  1002. w_value, w_index, w_length);
  1003. req->zero = (value < w_length);
  1004. req->length = value;
  1005. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  1006. if (value < 0) {
  1007. pr_err("queueing req failed: %02x.%02x, err %d\n",
  1008. ctrl->bRequestType,
  1009. ctrl->bRequest, value);
  1010. }
  1011. } else {
  1012. pr_err("ctrl req err %d: %02x.%02x v%04x i%04x l%d\n",
  1013. value, ctrl->bRequestType, ctrl->bRequest,
  1014. w_value, w_index, w_length);
  1015. }
  1016. /* device either stalls (value < 0) or reports success */
  1017. return value;
  1018. }
  1019. /*
  1020. * This function handles the Microsoft-specific OS descriptor control
  1021. * requests that are issued by Windows host drivers to determine the
  1022. * configuration containing the MBIM function.
  1023. *
  1024. * Unlike mbim_setup() this function handles two specific device requests,
  1025. * and only when a configuration has not yet been selected.
  1026. */
  1027. static int mbim_ctrlrequest(struct usb_composite_dev *cdev,
  1028. const struct usb_ctrlrequest *ctrl)
  1029. {
  1030. int value = -EOPNOTSUPP;
  1031. u16 w_index = le16_to_cpu(ctrl->wIndex);
  1032. u16 w_value = le16_to_cpu(ctrl->wValue);
  1033. u16 w_length = le16_to_cpu(ctrl->wLength);
  1034. /* only respond to OS desciptors when no configuration selected */
  1035. if (cdev->config || !mbim_ext_config_desc.function.subCompatibleID[0])
  1036. return value;
  1037. pr_debug("%02x.%02x v%04x i%04x l%u",
  1038. ctrl->bRequestType, ctrl->bRequest,
  1039. w_value, w_index, w_length);
  1040. /* Handle MSFT OS string */
  1041. if (ctrl->bRequestType ==
  1042. (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE)
  1043. && ctrl->bRequest == USB_REQ_GET_DESCRIPTOR
  1044. && (w_value >> 8) == USB_DT_STRING
  1045. && (w_value & 0xFF) == MBIM_OS_STRING_ID) {
  1046. value = (w_length < sizeof(mbim_os_string) ?
  1047. w_length : sizeof(mbim_os_string));
  1048. memcpy(cdev->req->buf, mbim_os_string, value);
  1049. } else if (ctrl->bRequestType ==
  1050. (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE)
  1051. && ctrl->bRequest == MBIM_VENDOR_CODE && w_index == 4) {
  1052. /* Handle Extended OS descriptor */
  1053. value = (w_length < sizeof(mbim_ext_config_desc) ?
  1054. w_length : sizeof(mbim_ext_config_desc));
  1055. memcpy(cdev->req->buf, &mbim_ext_config_desc, value);
  1056. }
  1057. /* respond with data transfer or status phase? */
  1058. if (value >= 0) {
  1059. int rc;
  1060. cdev->req->zero = value < w_length;
  1061. cdev->req->length = value;
  1062. rc = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
  1063. if (rc < 0)
  1064. pr_err("response queue error: %d", rc);
  1065. }
  1066. return value;
  1067. }
  1068. static int mbim_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  1069. {
  1070. struct f_mbim *mbim = func_to_mbim(f);
  1071. struct usb_composite_dev *cdev = mbim->cdev;
  1072. int ret = 0;
  1073. /* Control interface has only altsetting 0 */
  1074. if (intf == mbim->ctrl_id) {
  1075. pr_info("CONTROL_INTERFACE");
  1076. if (alt != 0)
  1077. goto fail;
  1078. if (mbim->not_port.notify->driver_data) {
  1079. pr_info("reset mbim control %d\n", intf);
  1080. usb_ep_disable(mbim->not_port.notify);
  1081. }
  1082. ret = config_ep_by_speed(cdev->gadget, f,
  1083. mbim->not_port.notify);
  1084. if (ret) {
  1085. mbim->not_port.notify->desc = NULL;
  1086. pr_err("Failed configuring notify ep %s: err %d\n",
  1087. mbim->not_port.notify->name, ret);
  1088. return ret;
  1089. }
  1090. ret = usb_ep_enable(mbim->not_port.notify);
  1091. if (ret) {
  1092. pr_err("usb ep#%s enable failed, err#%d\n",
  1093. mbim->not_port.notify->name, ret);
  1094. return ret;
  1095. }
  1096. mbim->not_port.notify->driver_data = mbim;
  1097. /* Data interface has two altsettings, 0 and 1 */
  1098. } else if (intf == mbim->data_id) {
  1099. pr_info("DATA_INTERFACE");
  1100. if (alt > 1)
  1101. goto fail;
  1102. if (mbim->bam_port.in->driver_data) {
  1103. pr_info("reset mbim\n");
  1104. mbim_reset_values(mbim);
  1105. }
  1106. /*
  1107. * CDC Network only sends data in non-default altsettings.
  1108. * Changing altsettings resets filters, statistics, etc.
  1109. */
  1110. if (alt == 1) {
  1111. pr_info("Alt set 1, initialize ports");
  1112. if (!mbim->bam_port.in->desc) {
  1113. pr_info("Choose endpoints");
  1114. ret = config_ep_by_speed(cdev->gadget, f,
  1115. mbim->bam_port.in);
  1116. if (ret) {
  1117. mbim->bam_port.in->desc = NULL;
  1118. pr_err("IN ep %s failed: %d\n",
  1119. mbim->bam_port.in->name, ret);
  1120. return ret;
  1121. }
  1122. pr_info("Set mbim port in_desc = 0x%pK\n",
  1123. mbim->bam_port.in->desc);
  1124. ret = config_ep_by_speed(cdev->gadget, f,
  1125. mbim->bam_port.out);
  1126. if (ret) {
  1127. mbim->bam_port.out->desc = NULL;
  1128. pr_err("OUT ep %s failed: %d\n",
  1129. mbim->bam_port.out->name, ret);
  1130. return ret;
  1131. }
  1132. pr_info("Set mbim port out_desc = 0x%pK\n",
  1133. mbim->bam_port.out->desc);
  1134. pr_debug("Activate mbim\n");
  1135. mbim_bam_connect(mbim);
  1136. } else {
  1137. pr_info("PORTS already SET");
  1138. }
  1139. }
  1140. mbim->data_alt_int = alt;
  1141. spin_lock(&mbim->lock);
  1142. mbim->not_port.notify_state = MBIM_NOTIFY_RESPONSE_AVAILABLE;
  1143. spin_unlock(&mbim->lock);
  1144. } else {
  1145. goto fail;
  1146. }
  1147. atomic_set(&mbim->online, 1);
  1148. pr_info("SET DEVICE ONLINE");
  1149. /* wakeup file threads */
  1150. wake_up(&mbim->read_wq);
  1151. wake_up(&mbim->write_wq);
  1152. return 0;
  1153. fail:
  1154. pr_err("ERROR: Illegal Interface");
  1155. return -EINVAL;
  1156. }
  1157. /*
  1158. * Because the data interface supports multiple altsettings,
  1159. * this MBIM function *MUST* implement a get_alt() method.
  1160. */
  1161. static int mbim_get_alt(struct usb_function *f, unsigned intf)
  1162. {
  1163. struct f_mbim *mbim = func_to_mbim(f);
  1164. if (intf == mbim->ctrl_id)
  1165. return 0;
  1166. else if (intf == mbim->data_id)
  1167. return mbim->data_alt_int;
  1168. return -EINVAL;
  1169. }
  1170. static void mbim_disable(struct usb_function *f)
  1171. {
  1172. struct f_mbim *mbim = func_to_mbim(f);
  1173. pr_info("SET DEVICE OFFLINE");
  1174. atomic_set(&mbim->online, 0);
  1175. mbim->not_port.notify_state = MBIM_NOTIFY_NONE;
  1176. mbim_clear_queues(mbim);
  1177. mbim_reset_function_queue(mbim);
  1178. mbim_bam_disconnect(mbim);
  1179. if (mbim->not_port.notify->driver_data) {
  1180. usb_ep_disable(mbim->not_port.notify);
  1181. mbim->not_port.notify->driver_data = NULL;
  1182. }
  1183. atomic_set(&mbim->not_port.notify_count, 0);
  1184. pr_info("mbim deactivated\n");
  1185. }
  1186. #define MBIM_ACTIVE_PORT 0
  1187. static void mbim_suspend(struct usb_function *f)
  1188. {
  1189. pr_info("mbim suspended\n");
  1190. bam_data_suspend(MBIM_ACTIVE_PORT);
  1191. }
  1192. static void mbim_resume(struct usb_function *f)
  1193. {
  1194. pr_info("mbim resumed\n");
  1195. bam_data_resume(MBIM_ACTIVE_PORT);
  1196. }
  1197. /*---------------------- function driver setup/binding ---------------------*/
  1198. static int
  1199. mbim_bind(struct usb_configuration *c, struct usb_function *f)
  1200. {
  1201. struct usb_composite_dev *cdev = c->cdev;
  1202. struct f_mbim *mbim = func_to_mbim(f);
  1203. int status;
  1204. struct usb_ep *ep;
  1205. pr_info("Enter");
  1206. mbim->cdev = cdev;
  1207. /* allocate instance-specific interface IDs */
  1208. status = usb_interface_id(c, f);
  1209. if (status < 0)
  1210. goto fail;
  1211. mbim->ctrl_id = status;
  1212. mbim_iad_desc.bFirstInterface = status;
  1213. mbim_control_intf.bInterfaceNumber = status;
  1214. mbim_union_desc.bMasterInterface0 = status;
  1215. status = usb_interface_id(c, f);
  1216. if (status < 0)
  1217. goto fail;
  1218. mbim->data_id = status;
  1219. mbim->data_alt_int = 0;
  1220. mbim_data_nop_intf.bInterfaceNumber = status;
  1221. mbim_data_intf.bInterfaceNumber = status;
  1222. mbim_union_desc.bSlaveInterface0 = status;
  1223. mbim->bam_port.cdev = cdev;
  1224. mbim->bam_port.func = &mbim->function;
  1225. status = -ENODEV;
  1226. /* allocate instance-specific endpoints */
  1227. ep = usb_ep_autoconfig(cdev->gadget, &fs_mbim_in_desc);
  1228. if (!ep) {
  1229. pr_err("usb epin autoconfig failed\n");
  1230. goto fail;
  1231. }
  1232. pr_info("usb epin autoconfig succeeded\n");
  1233. ep->driver_data = cdev; /* claim */
  1234. mbim->bam_port.in = ep;
  1235. ep = usb_ep_autoconfig(cdev->gadget, &fs_mbim_out_desc);
  1236. if (!ep) {
  1237. pr_err("usb epout autoconfig failed\n");
  1238. goto fail;
  1239. }
  1240. pr_info("usb epout autoconfig succeeded\n");
  1241. ep->driver_data = cdev; /* claim */
  1242. mbim->bam_port.out = ep;
  1243. ep = usb_ep_autoconfig(cdev->gadget, &fs_mbim_notify_desc);
  1244. if (!ep) {
  1245. pr_err("usb notify ep autoconfig failed\n");
  1246. goto fail;
  1247. }
  1248. pr_info("usb notify ep autoconfig succeeded\n");
  1249. mbim->not_port.notify = ep;
  1250. ep->driver_data = cdev; /* claim */
  1251. status = -ENOMEM;
  1252. /* allocate notification request and buffer */
  1253. mbim->not_port.notify_req = mbim_alloc_req(ep, NCM_STATUS_BYTECOUNT);
  1254. if (!mbim->not_port.notify_req) {
  1255. pr_info("failed to allocate notify request\n");
  1256. goto fail;
  1257. }
  1258. pr_info("allocated notify ep request & request buffer\n");
  1259. mbim->not_port.notify_req->context = mbim;
  1260. mbim->not_port.notify_req->complete = mbim_notify_complete;
  1261. if (mbim->xport == USB_GADGET_XPORT_BAM2BAM_IPA)
  1262. mbb_desc.wMaxSegmentSize = cpu_to_le16(0x800);
  1263. else
  1264. mbb_desc.wMaxSegmentSize = cpu_to_le16(0xfe0);
  1265. /* copy descriptors, and track endpoint copies */
  1266. f->fs_descriptors = usb_copy_descriptors(mbim_fs_function);
  1267. if (!f->fs_descriptors)
  1268. goto fail;
  1269. /*
  1270. * support all relevant hardware speeds... we expect that when
  1271. * hardware is dual speed, all bulk-capable endpoints work at
  1272. * both speeds
  1273. */
  1274. if (gadget_is_dualspeed(c->cdev->gadget)) {
  1275. hs_mbim_in_desc.bEndpointAddress =
  1276. fs_mbim_in_desc.bEndpointAddress;
  1277. hs_mbim_out_desc.bEndpointAddress =
  1278. fs_mbim_out_desc.bEndpointAddress;
  1279. hs_mbim_notify_desc.bEndpointAddress =
  1280. fs_mbim_notify_desc.bEndpointAddress;
  1281. /* copy descriptors, and track endpoint copies */
  1282. f->hs_descriptors = usb_copy_descriptors(mbim_hs_function);
  1283. if (!f->hs_descriptors)
  1284. goto fail;
  1285. }
  1286. if (gadget_is_superspeed(c->cdev->gadget)) {
  1287. ss_mbim_in_desc.bEndpointAddress =
  1288. fs_mbim_in_desc.bEndpointAddress;
  1289. ss_mbim_out_desc.bEndpointAddress =
  1290. fs_mbim_out_desc.bEndpointAddress;
  1291. ss_mbim_notify_desc.bEndpointAddress =
  1292. fs_mbim_notify_desc.bEndpointAddress;
  1293. /* copy descriptors, and track endpoint copies */
  1294. f->ss_descriptors = usb_copy_descriptors(mbim_ss_function);
  1295. if (!f->ss_descriptors)
  1296. goto fail;
  1297. }
  1298. /*
  1299. * If MBIM is bound in a config other than the first, tell Windows
  1300. * about it by returning the num as a string in the OS descriptor's
  1301. * subCompatibleID field. Windows only supports up to config #4.
  1302. */
  1303. if (c->bConfigurationValue >= 2 && c->bConfigurationValue <= 4) {
  1304. pr_debug("MBIM in configuration %d", c->bConfigurationValue);
  1305. mbim_ext_config_desc.function.subCompatibleID[0] =
  1306. c->bConfigurationValue + '0';
  1307. }
  1308. pr_info("mbim(%d): %s speed IN/%s OUT/%s NOTIFY/%s\n",
  1309. mbim->port_num,
  1310. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  1311. mbim->bam_port.in->name, mbim->bam_port.out->name,
  1312. mbim->not_port.notify->name);
  1313. return 0;
  1314. fail:
  1315. pr_err("%s failed to bind, err %d\n", f->name, status);
  1316. if (f->ss_descriptors)
  1317. usb_free_descriptors(f->ss_descriptors);
  1318. if (f->hs_descriptors)
  1319. usb_free_descriptors(f->hs_descriptors);
  1320. if (f->fs_descriptors)
  1321. usb_free_descriptors(f->fs_descriptors);
  1322. if (mbim->not_port.notify_req) {
  1323. kfree(mbim->not_port.notify_req->buf);
  1324. usb_ep_free_request(mbim->not_port.notify,
  1325. mbim->not_port.notify_req);
  1326. }
  1327. /* we might as well release our claims on endpoints */
  1328. if (mbim->not_port.notify)
  1329. mbim->not_port.notify->driver_data = NULL;
  1330. if (mbim->bam_port.out)
  1331. mbim->bam_port.out->driver_data = NULL;
  1332. if (mbim->bam_port.in)
  1333. mbim->bam_port.in->driver_data = NULL;
  1334. return status;
  1335. }
  1336. static void mbim_unbind(struct usb_configuration *c, struct usb_function *f)
  1337. {
  1338. struct f_mbim *mbim = func_to_mbim(f);
  1339. bam_data_destroy(mbim->port_num);
  1340. if (gadget_is_superspeed(c->cdev->gadget))
  1341. usb_free_descriptors(f->ss_descriptors);
  1342. if (gadget_is_dualspeed(c->cdev->gadget))
  1343. usb_free_descriptors(f->hs_descriptors);
  1344. usb_free_descriptors(f->fs_descriptors);
  1345. kfree(mbim->not_port.notify_req->buf);
  1346. usb_ep_free_request(mbim->not_port.notify, mbim->not_port.notify_req);
  1347. mbim_ext_config_desc.function.subCompatibleID[0] = 0;
  1348. }
  1349. /**
  1350. * mbim_bind_config - add MBIM link to a configuration
  1351. * @c: the configuration to support the network link
  1352. * Context: single threaded during gadget setup
  1353. * Returns zero on success, else negative errno.
  1354. */
  1355. int mbim_bind_config(struct usb_configuration *c, unsigned portno,
  1356. char *xport_name)
  1357. {
  1358. struct f_mbim *mbim = NULL;
  1359. int status = 0;
  1360. pr_info("port number %u", portno);
  1361. if (portno >= nr_mbim_ports) {
  1362. pr_err("Can not add port %u. Max ports = %d",
  1363. portno, nr_mbim_ports);
  1364. return -ENODEV;
  1365. }
  1366. status = mbim_bam_setup(nr_mbim_ports);
  1367. if (status) {
  1368. pr_err("bam setup failed");
  1369. return status;
  1370. }
  1371. /* maybe allocate device-global string IDs */
  1372. if (mbim_string_defs[0].id == 0) {
  1373. /* control interface label */
  1374. status = usb_string_id(c->cdev);
  1375. if (status < 0)
  1376. return status;
  1377. mbim_string_defs[STRING_CTRL_IDX].id = status;
  1378. mbim_control_intf.iInterface = status;
  1379. /* data interface label */
  1380. status = usb_string_id(c->cdev);
  1381. if (status < 0)
  1382. return status;
  1383. mbim_string_defs[STRING_DATA_IDX].id = status;
  1384. mbim_data_nop_intf.iInterface = status;
  1385. mbim_data_intf.iInterface = status;
  1386. }
  1387. /* allocate and initialize one new instance */
  1388. mbim = mbim_ports[0].port;
  1389. if (!mbim) {
  1390. pr_info("mbim struct not allocated");
  1391. return -ENOMEM;
  1392. }
  1393. mbim->cdev = c->cdev;
  1394. mbim_reset_values(mbim);
  1395. mbim->function.name = "usb_mbim";
  1396. mbim->function.strings = mbim_strings;
  1397. mbim->function.bind = mbim_bind;
  1398. mbim->function.unbind = mbim_unbind;
  1399. mbim->function.set_alt = mbim_set_alt;
  1400. mbim->function.get_alt = mbim_get_alt;
  1401. mbim->function.setup = mbim_setup;
  1402. mbim->function.disable = mbim_disable;
  1403. mbim->function.suspend = mbim_suspend;
  1404. mbim->function.resume = mbim_resume;
  1405. mbim->xport = str_to_xport(xport_name);
  1406. if (mbim->xport != USB_GADGET_XPORT_BAM2BAM_IPA) {
  1407. /* Use BAM2BAM by default if not IPA */
  1408. mbim->xport = USB_GADGET_XPORT_BAM2BAM;
  1409. } else {
  1410. /* For IPA we use limit of 16 */
  1411. mbim_ntb_parameters.wNtbOutMaxDatagrams = 16;
  1412. /* For IPA this is proven to give maximum throughput */
  1413. mbim_ntb_parameters.dwNtbInMaxSize =
  1414. cpu_to_le32(NTB_DEFAULT_IN_SIZE_IPA);
  1415. mbim_ntb_parameters.dwNtbOutMaxSize =
  1416. cpu_to_le32(MBIM_NTB_OUT_SIZE_IPA);
  1417. mbim_ntb_parameters.wNdpInDivisor = 1;
  1418. }
  1419. INIT_LIST_HEAD(&mbim->cpkt_req_q);
  1420. INIT_LIST_HEAD(&mbim->cpkt_resp_q);
  1421. status = usb_add_function(c, &mbim->function);
  1422. pr_info("Exit status %d", status);
  1423. return status;
  1424. }
  1425. /* ------------ MBIM DRIVER File Operations API for USER SPACE ------------ */
  1426. static ssize_t
  1427. mbim_read(struct file *fp, char __user *buf, size_t count, loff_t *pos)
  1428. {
  1429. struct f_mbim *dev = fp->private_data;
  1430. struct ctrl_pkt *cpkt = NULL;
  1431. unsigned long flags;
  1432. int ret = 0;
  1433. pr_debug("Enter(%d)\n", count);
  1434. if (!dev) {
  1435. pr_err("Received NULL mbim pointer\n");
  1436. return -ENODEV;
  1437. }
  1438. if (count > MBIM_BULK_BUFFER_SIZE) {
  1439. pr_err("Buffer size is too big %d, should be at most %d\n",
  1440. count, MBIM_BULK_BUFFER_SIZE);
  1441. return -EINVAL;
  1442. }
  1443. if (mbim_lock(&dev->read_excl)) {
  1444. pr_err("Previous reading is not finished yet\n");
  1445. return -EBUSY;
  1446. }
  1447. /* block until mbim online */
  1448. while (!(atomic_read(&dev->online) || atomic_read(&dev->error))) {
  1449. pr_err("USB cable not connected. Wait.\n");
  1450. ret = wait_event_interruptible(dev->read_wq,
  1451. (atomic_read(&dev->online) ||
  1452. atomic_read(&dev->error)));
  1453. if (ret < 0) {
  1454. mbim_unlock(&dev->read_excl);
  1455. return -ERESTARTSYS;
  1456. }
  1457. }
  1458. if (atomic_read(&dev->error)) {
  1459. mbim_unlock(&dev->read_excl);
  1460. return -EIO;
  1461. }
  1462. spin_lock_irqsave(&dev->lock, flags);
  1463. while (list_empty(&dev->cpkt_req_q)) {
  1464. pr_debug("Requests list is empty. Wait.\n");
  1465. spin_unlock_irqrestore(&dev->lock, flags);
  1466. ret = wait_event_interruptible(dev->read_wq,
  1467. !list_empty(&dev->cpkt_req_q));
  1468. if (ret < 0) {
  1469. pr_err("Waiting failed\n");
  1470. mbim_unlock(&dev->read_excl);
  1471. return -ERESTARTSYS;
  1472. }
  1473. pr_debug("Received request packet\n");
  1474. spin_lock_irqsave(&dev->lock, flags);
  1475. }
  1476. cpkt = list_first_entry(&dev->cpkt_req_q, struct ctrl_pkt,
  1477. list);
  1478. if (cpkt->len > count) {
  1479. spin_unlock_irqrestore(&dev->lock, flags);
  1480. mbim_unlock(&dev->read_excl);
  1481. pr_err("cpkt size too big:%d > buf size:%d\n",
  1482. cpkt->len, count);
  1483. return -ENOMEM;
  1484. }
  1485. pr_debug("cpkt size:%d\n", cpkt->len);
  1486. list_del(&cpkt->list);
  1487. spin_unlock_irqrestore(&dev->lock, flags);
  1488. mbim_unlock(&dev->read_excl);
  1489. ret = copy_to_user(buf, cpkt->buf, cpkt->len);
  1490. if (ret) {
  1491. pr_err("copy_to_user failed: err %d\n", ret);
  1492. ret = -ENOMEM;
  1493. } else {
  1494. pr_debug("copied %d bytes to user\n", cpkt->len);
  1495. ret = cpkt->len;
  1496. }
  1497. mbim_free_ctrl_pkt(cpkt);
  1498. return ret;
  1499. }
  1500. static ssize_t
  1501. mbim_write(struct file *fp, const char __user *buf, size_t count, loff_t *pos)
  1502. {
  1503. struct f_mbim *dev = fp->private_data;
  1504. struct ctrl_pkt *cpkt = NULL;
  1505. int ret = 0;
  1506. pr_debug("Enter(%d)", count);
  1507. if (!dev) {
  1508. pr_err("Received NULL mbim pointer\n");
  1509. return -ENODEV;
  1510. }
  1511. if (!count) {
  1512. pr_err("zero length ctrl pkt\n");
  1513. return -ENODEV;
  1514. }
  1515. if (count > MAX_CTRL_PKT_SIZE) {
  1516. pr_err("given pkt size too big:%d > max_pkt_size:%d\n",
  1517. count, MAX_CTRL_PKT_SIZE);
  1518. return -ENOMEM;
  1519. }
  1520. if (mbim_lock(&dev->write_excl)) {
  1521. pr_err("Previous writing not finished yet\n");
  1522. return -EBUSY;
  1523. }
  1524. if (!atomic_read(&dev->online)) {
  1525. pr_err("USB cable not connected\n");
  1526. mbim_unlock(&dev->write_excl);
  1527. return -EPIPE;
  1528. }
  1529. cpkt = mbim_alloc_ctrl_pkt(count, GFP_KERNEL);
  1530. if (!cpkt) {
  1531. pr_err("failed to allocate ctrl pkt\n");
  1532. mbim_unlock(&dev->write_excl);
  1533. return -ENOMEM;
  1534. }
  1535. ret = copy_from_user(cpkt->buf, buf, count);
  1536. if (ret) {
  1537. pr_err("copy_from_user failed err:%d\n", ret);
  1538. mbim_free_ctrl_pkt(cpkt);
  1539. mbim_unlock(&dev->write_excl);
  1540. return 0;
  1541. }
  1542. fmbim_send_cpkt_response(dev, cpkt);
  1543. mbim_unlock(&dev->write_excl);
  1544. pr_debug("Exit(%d)", count);
  1545. return count;
  1546. }
  1547. static int mbim_open(struct inode *ip, struct file *fp)
  1548. {
  1549. pr_info("Open mbim driver\n");
  1550. while (!_mbim_dev) {
  1551. pr_err("mbim_dev not created yet\n");
  1552. return -ENODEV;
  1553. }
  1554. if (mbim_lock(&_mbim_dev->open_excl)) {
  1555. pr_err("Already opened\n");
  1556. return -EBUSY;
  1557. }
  1558. pr_info("Lock mbim_dev->open_excl for open\n");
  1559. if (!atomic_read(&_mbim_dev->online))
  1560. pr_err("USB cable not connected\n");
  1561. fp->private_data = _mbim_dev;
  1562. atomic_set(&_mbim_dev->error, 0);
  1563. pr_info("Exit, mbim file opened\n");
  1564. return 0;
  1565. }
  1566. static int mbim_release(struct inode *ip, struct file *fp)
  1567. {
  1568. pr_info("Close mbim file");
  1569. mbim_unlock(&_mbim_dev->open_excl);
  1570. return 0;
  1571. }
  1572. static long mbim_ioctl(struct file *fp, unsigned cmd, unsigned long arg)
  1573. {
  1574. struct f_mbim *mbim = fp->private_data;
  1575. int ret = 0;
  1576. pr_debug("Received command %d", cmd);
  1577. if (mbim_lock(&mbim->ioctl_excl))
  1578. return -EBUSY;
  1579. switch (cmd) {
  1580. case MBIM_GET_NTB_SIZE:
  1581. ret = copy_to_user((void __user *)arg,
  1582. &mbim->ntb_input_size, sizeof(mbim->ntb_input_size));
  1583. if (ret) {
  1584. pr_err("copying to user space failed");
  1585. ret = -EFAULT;
  1586. }
  1587. pr_info("Sent NTB size %d", mbim->ntb_input_size);
  1588. break;
  1589. case MBIM_GET_DATAGRAM_COUNT:
  1590. ret = copy_to_user((void __user *)arg,
  1591. &mbim->ntb_max_datagrams,
  1592. sizeof(mbim->ntb_max_datagrams));
  1593. if (ret) {
  1594. pr_err("copying to user space failed");
  1595. ret = -EFAULT;
  1596. }
  1597. pr_info("Sent NTB datagrams count %d",
  1598. mbim->ntb_max_datagrams);
  1599. break;
  1600. default:
  1601. pr_err("wrong parameter");
  1602. ret = -EINVAL;
  1603. }
  1604. mbim_unlock(&mbim->ioctl_excl);
  1605. return ret;
  1606. }
  1607. /* file operations for MBIM device /dev/android_mbim */
  1608. static const struct file_operations mbim_fops = {
  1609. .owner = THIS_MODULE,
  1610. .open = mbim_open,
  1611. .release = mbim_release,
  1612. .read = mbim_read,
  1613. .write = mbim_write,
  1614. .unlocked_ioctl = mbim_ioctl,
  1615. };
  1616. static struct miscdevice mbim_device = {
  1617. .minor = MISC_DYNAMIC_MINOR,
  1618. .name = "android_mbim",
  1619. .fops = &mbim_fops,
  1620. };
  1621. static int mbim_init(int instances)
  1622. {
  1623. int i;
  1624. struct f_mbim *dev = NULL;
  1625. int ret;
  1626. pr_info("initialize %d instances\n", instances);
  1627. if (instances > NR_MBIM_PORTS) {
  1628. pr_err("Max-%d instances supported\n", NR_MBIM_PORTS);
  1629. return -EINVAL;
  1630. }
  1631. for (i = 0; i < instances; i++) {
  1632. dev = kzalloc(sizeof(struct f_mbim), GFP_KERNEL);
  1633. if (!dev) {
  1634. pr_err("Failed to allocate mbim dev\n");
  1635. ret = -ENOMEM;
  1636. goto fail_probe;
  1637. }
  1638. dev->port_num = i;
  1639. spin_lock_init(&dev->lock);
  1640. INIT_LIST_HEAD(&dev->cpkt_req_q);
  1641. INIT_LIST_HEAD(&dev->cpkt_resp_q);
  1642. mbim_ports[i].port = dev;
  1643. mbim_ports[i].port_num = i;
  1644. init_waitqueue_head(&dev->read_wq);
  1645. init_waitqueue_head(&dev->write_wq);
  1646. atomic_set(&dev->open_excl, 0);
  1647. atomic_set(&dev->ioctl_excl, 0);
  1648. atomic_set(&dev->read_excl, 0);
  1649. atomic_set(&dev->write_excl, 0);
  1650. nr_mbim_ports++;
  1651. }
  1652. _mbim_dev = dev;
  1653. ret = misc_register(&mbim_device);
  1654. if (ret) {
  1655. pr_err("mbim driver failed to register");
  1656. goto fail_probe;
  1657. }
  1658. pr_info("Initialized %d ports\n", nr_mbim_ports);
  1659. return ret;
  1660. fail_probe:
  1661. pr_err("Failed");
  1662. for (i = 0; i < nr_mbim_ports; i++) {
  1663. kfree(mbim_ports[i].port);
  1664. mbim_ports[i].port = NULL;
  1665. }
  1666. return ret;
  1667. }
  1668. static void fmbim_cleanup(void)
  1669. {
  1670. int i = 0;
  1671. pr_info("Enter");
  1672. for (i = 0; i < nr_mbim_ports; i++) {
  1673. kfree(mbim_ports[i].port);
  1674. mbim_ports[i].port = NULL;
  1675. }
  1676. nr_mbim_ports = 0;
  1677. misc_deregister(&mbim_device);
  1678. _mbim_dev = NULL;
  1679. }