usb.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  1. /*
  2. * Marvell Wireless LAN device driver: USB specific handling
  3. *
  4. * Copyright (C) 2012-2014, Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available by writing to the Free Software Foundation, Inc.,
  11. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12. * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13. *
  14. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  17. * this warranty disclaimer.
  18. */
  19. #include "main.h"
  20. #include "usb.h"
  21. #define USB_VERSION "1.0"
  22. static struct mwifiex_if_ops usb_ops;
  23. static const struct usb_device_id mwifiex_usb_table[] = {
  24. /* 8766 */
  25. {USB_DEVICE(USB8XXX_VID, USB8766_PID_1)},
  26. {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8766_PID_2,
  27. USB_CLASS_VENDOR_SPEC,
  28. USB_SUBCLASS_VENDOR_SPEC, 0xff)},
  29. /* 8797 */
  30. {USB_DEVICE(USB8XXX_VID, USB8797_PID_1)},
  31. {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8797_PID_2,
  32. USB_CLASS_VENDOR_SPEC,
  33. USB_SUBCLASS_VENDOR_SPEC, 0xff)},
  34. /* 8801 */
  35. {USB_DEVICE(USB8XXX_VID, USB8801_PID_1)},
  36. {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8801_PID_2,
  37. USB_CLASS_VENDOR_SPEC,
  38. USB_SUBCLASS_VENDOR_SPEC, 0xff)},
  39. /* 8997 */
  40. {USB_DEVICE(USB8XXX_VID, USB8997_PID_1)},
  41. {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8997_PID_2,
  42. USB_CLASS_VENDOR_SPEC,
  43. USB_SUBCLASS_VENDOR_SPEC, 0xff)},
  44. { } /* Terminating entry */
  45. };
  46. MODULE_DEVICE_TABLE(usb, mwifiex_usb_table);
  47. static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size);
  48. /* This function handles received packet. Necessary action is taken based on
  49. * cmd/event/data.
  50. */
  51. static int mwifiex_usb_recv(struct mwifiex_adapter *adapter,
  52. struct sk_buff *skb, u8 ep)
  53. {
  54. u32 recv_type;
  55. __le32 tmp;
  56. int ret;
  57. if (adapter->hs_activated)
  58. mwifiex_process_hs_config(adapter);
  59. if (skb->len < INTF_HEADER_LEN) {
  60. mwifiex_dbg(adapter, ERROR,
  61. "%s: invalid skb->len\n", __func__);
  62. return -1;
  63. }
  64. switch (ep) {
  65. case MWIFIEX_USB_EP_CMD_EVENT:
  66. mwifiex_dbg(adapter, EVENT,
  67. "%s: EP_CMD_EVENT\n", __func__);
  68. skb_copy_from_linear_data(skb, &tmp, INTF_HEADER_LEN);
  69. recv_type = le32_to_cpu(tmp);
  70. skb_pull(skb, INTF_HEADER_LEN);
  71. switch (recv_type) {
  72. case MWIFIEX_USB_TYPE_CMD:
  73. if (skb->len > MWIFIEX_SIZE_OF_CMD_BUFFER) {
  74. mwifiex_dbg(adapter, ERROR,
  75. "CMD: skb->len too large\n");
  76. ret = -1;
  77. goto exit_restore_skb;
  78. } else if (!adapter->curr_cmd) {
  79. mwifiex_dbg(adapter, WARN, "CMD: no curr_cmd\n");
  80. if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
  81. mwifiex_process_sleep_confirm_resp(
  82. adapter, skb->data,
  83. skb->len);
  84. ret = 0;
  85. goto exit_restore_skb;
  86. }
  87. ret = -1;
  88. goto exit_restore_skb;
  89. }
  90. adapter->curr_cmd->resp_skb = skb;
  91. adapter->cmd_resp_received = true;
  92. break;
  93. case MWIFIEX_USB_TYPE_EVENT:
  94. if (skb->len < sizeof(u32)) {
  95. mwifiex_dbg(adapter, ERROR,
  96. "EVENT: skb->len too small\n");
  97. ret = -1;
  98. goto exit_restore_skb;
  99. }
  100. skb_copy_from_linear_data(skb, &tmp, sizeof(u32));
  101. adapter->event_cause = le32_to_cpu(tmp);
  102. mwifiex_dbg(adapter, EVENT,
  103. "event_cause %#x\n", adapter->event_cause);
  104. if (skb->len > MAX_EVENT_SIZE) {
  105. mwifiex_dbg(adapter, ERROR,
  106. "EVENT: event body too large\n");
  107. ret = -1;
  108. goto exit_restore_skb;
  109. }
  110. memcpy(adapter->event_body, skb->data +
  111. MWIFIEX_EVENT_HEADER_LEN, skb->len);
  112. adapter->event_received = true;
  113. adapter->event_skb = skb;
  114. break;
  115. default:
  116. mwifiex_dbg(adapter, ERROR,
  117. "unknown recv_type %#x\n", recv_type);
  118. return -1;
  119. }
  120. break;
  121. case MWIFIEX_USB_EP_DATA:
  122. mwifiex_dbg(adapter, DATA, "%s: EP_DATA\n", __func__);
  123. if (skb->len > MWIFIEX_RX_DATA_BUF_SIZE) {
  124. mwifiex_dbg(adapter, ERROR,
  125. "DATA: skb->len too large\n");
  126. return -1;
  127. }
  128. skb_queue_tail(&adapter->rx_data_q, skb);
  129. adapter->data_received = true;
  130. atomic_inc(&adapter->rx_pending);
  131. break;
  132. default:
  133. mwifiex_dbg(adapter, ERROR,
  134. "%s: unknown endport %#x\n", __func__, ep);
  135. return -1;
  136. }
  137. return -EINPROGRESS;
  138. exit_restore_skb:
  139. /* The buffer will be reused for further cmds/events */
  140. skb_push(skb, INTF_HEADER_LEN);
  141. return ret;
  142. }
  143. static void mwifiex_usb_rx_complete(struct urb *urb)
  144. {
  145. struct urb_context *context = (struct urb_context *)urb->context;
  146. struct mwifiex_adapter *adapter = context->adapter;
  147. struct sk_buff *skb = context->skb;
  148. struct usb_card_rec *card;
  149. int recv_length = urb->actual_length;
  150. int size, status;
  151. if (!adapter || !adapter->card) {
  152. pr_err("mwifiex adapter or card structure is not valid\n");
  153. return;
  154. }
  155. card = (struct usb_card_rec *)adapter->card;
  156. if (card->rx_cmd_ep == context->ep)
  157. atomic_dec(&card->rx_cmd_urb_pending);
  158. else
  159. atomic_dec(&card->rx_data_urb_pending);
  160. if (recv_length) {
  161. if (urb->status ||
  162. test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags)) {
  163. mwifiex_dbg(adapter, ERROR,
  164. "URB status is failed: %d\n", urb->status);
  165. /* Do not free skb in case of command ep */
  166. if (card->rx_cmd_ep != context->ep)
  167. dev_kfree_skb_any(skb);
  168. goto setup_for_next;
  169. }
  170. if (skb->len > recv_length)
  171. skb_trim(skb, recv_length);
  172. else
  173. skb_put(skb, recv_length - skb->len);
  174. status = mwifiex_usb_recv(adapter, skb, context->ep);
  175. mwifiex_dbg(adapter, INFO,
  176. "info: recv_length=%d, status=%d\n",
  177. recv_length, status);
  178. if (status == -EINPROGRESS) {
  179. mwifiex_queue_main_work(adapter);
  180. /* urb for data_ep is re-submitted now;
  181. * urb for cmd_ep will be re-submitted in callback
  182. * mwifiex_usb_recv_complete
  183. */
  184. if (card->rx_cmd_ep == context->ep)
  185. return;
  186. } else {
  187. if (status == -1)
  188. mwifiex_dbg(adapter, ERROR,
  189. "received data processing failed!\n");
  190. /* Do not free skb in case of command ep */
  191. if (card->rx_cmd_ep != context->ep)
  192. dev_kfree_skb_any(skb);
  193. }
  194. } else if (urb->status) {
  195. if (!test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)) {
  196. mwifiex_dbg(adapter, FATAL,
  197. "Card is removed: %d\n", urb->status);
  198. set_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags);
  199. }
  200. dev_kfree_skb_any(skb);
  201. return;
  202. } else {
  203. /* Do not free skb in case of command ep */
  204. if (card->rx_cmd_ep != context->ep)
  205. dev_kfree_skb_any(skb);
  206. /* fall through setup_for_next */
  207. }
  208. setup_for_next:
  209. if (card->rx_cmd_ep == context->ep)
  210. size = MWIFIEX_RX_CMD_BUF_SIZE;
  211. else
  212. size = MWIFIEX_RX_DATA_BUF_SIZE;
  213. if (card->rx_cmd_ep == context->ep) {
  214. mwifiex_usb_submit_rx_urb(context, size);
  215. } else {
  216. if (atomic_read(&adapter->rx_pending) <= HIGH_RX_PENDING) {
  217. mwifiex_usb_submit_rx_urb(context, size);
  218. } else {
  219. context->skb = NULL;
  220. }
  221. }
  222. return;
  223. }
  224. static void mwifiex_usb_tx_complete(struct urb *urb)
  225. {
  226. struct urb_context *context = (struct urb_context *)(urb->context);
  227. struct mwifiex_adapter *adapter = context->adapter;
  228. struct usb_card_rec *card = adapter->card;
  229. struct usb_tx_data_port *port;
  230. int i;
  231. mwifiex_dbg(adapter, INFO,
  232. "%s: status: %d\n", __func__, urb->status);
  233. if (context->ep == card->tx_cmd_ep) {
  234. mwifiex_dbg(adapter, CMD,
  235. "%s: CMD\n", __func__);
  236. atomic_dec(&card->tx_cmd_urb_pending);
  237. adapter->cmd_sent = false;
  238. } else {
  239. mwifiex_dbg(adapter, DATA,
  240. "%s: DATA\n", __func__);
  241. mwifiex_write_data_complete(adapter, context->skb, 0,
  242. urb->status ? -1 : 0);
  243. for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
  244. port = &card->port[i];
  245. if (context->ep == port->tx_data_ep) {
  246. atomic_dec(&port->tx_data_urb_pending);
  247. port->block_status = false;
  248. break;
  249. }
  250. }
  251. adapter->data_sent = false;
  252. }
  253. if (card->mc_resync_flag)
  254. mwifiex_multi_chan_resync(adapter);
  255. mwifiex_queue_main_work(adapter);
  256. return;
  257. }
  258. static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size)
  259. {
  260. struct mwifiex_adapter *adapter = ctx->adapter;
  261. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  262. if (test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)) {
  263. if (card->rx_cmd_ep == ctx->ep) {
  264. mwifiex_dbg(adapter, INFO, "%s: free rx_cmd skb\n",
  265. __func__);
  266. dev_kfree_skb_any(ctx->skb);
  267. ctx->skb = NULL;
  268. }
  269. mwifiex_dbg(adapter, ERROR,
  270. "%s: card removed/suspended, EP %d rx_cmd URB submit skipped\n",
  271. __func__, ctx->ep);
  272. return -1;
  273. }
  274. if (card->rx_cmd_ep != ctx->ep) {
  275. ctx->skb = dev_alloc_skb(size);
  276. if (!ctx->skb) {
  277. mwifiex_dbg(adapter, ERROR,
  278. "%s: dev_alloc_skb failed\n", __func__);
  279. return -ENOMEM;
  280. }
  281. }
  282. if (card->rx_cmd_ep == ctx->ep &&
  283. card->rx_cmd_ep_type == USB_ENDPOINT_XFER_INT)
  284. usb_fill_int_urb(ctx->urb, card->udev,
  285. usb_rcvintpipe(card->udev, ctx->ep),
  286. ctx->skb->data, size, mwifiex_usb_rx_complete,
  287. (void *)ctx, card->rx_cmd_interval);
  288. else
  289. usb_fill_bulk_urb(ctx->urb, card->udev,
  290. usb_rcvbulkpipe(card->udev, ctx->ep),
  291. ctx->skb->data, size, mwifiex_usb_rx_complete,
  292. (void *)ctx);
  293. if (card->rx_cmd_ep == ctx->ep)
  294. atomic_inc(&card->rx_cmd_urb_pending);
  295. else
  296. atomic_inc(&card->rx_data_urb_pending);
  297. if (usb_submit_urb(ctx->urb, GFP_ATOMIC)) {
  298. mwifiex_dbg(adapter, ERROR, "usb_submit_urb failed\n");
  299. dev_kfree_skb_any(ctx->skb);
  300. ctx->skb = NULL;
  301. if (card->rx_cmd_ep == ctx->ep)
  302. atomic_dec(&card->rx_cmd_urb_pending);
  303. else
  304. atomic_dec(&card->rx_data_urb_pending);
  305. return -1;
  306. }
  307. return 0;
  308. }
  309. static void mwifiex_usb_free(struct usb_card_rec *card)
  310. {
  311. struct usb_tx_data_port *port;
  312. int i, j;
  313. if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
  314. usb_kill_urb(card->rx_cmd.urb);
  315. usb_free_urb(card->rx_cmd.urb);
  316. card->rx_cmd.urb = NULL;
  317. if (atomic_read(&card->rx_data_urb_pending))
  318. for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
  319. if (card->rx_data_list[i].urb)
  320. usb_kill_urb(card->rx_data_list[i].urb);
  321. for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
  322. usb_free_urb(card->rx_data_list[i].urb);
  323. card->rx_data_list[i].urb = NULL;
  324. }
  325. for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
  326. port = &card->port[i];
  327. for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
  328. usb_kill_urb(port->tx_data_list[j].urb);
  329. usb_free_urb(port->tx_data_list[j].urb);
  330. port->tx_data_list[j].urb = NULL;
  331. }
  332. }
  333. usb_free_urb(card->tx_cmd.urb);
  334. card->tx_cmd.urb = NULL;
  335. return;
  336. }
  337. /* This function probes an mwifiex device and registers it. It allocates
  338. * the card structure, initiates the device registration and initialization
  339. * procedure by adding a logical interface.
  340. */
  341. static int mwifiex_usb_probe(struct usb_interface *intf,
  342. const struct usb_device_id *id)
  343. {
  344. struct usb_device *udev = interface_to_usbdev(intf);
  345. struct usb_host_interface *iface_desc = intf->cur_altsetting;
  346. struct usb_endpoint_descriptor *epd;
  347. int ret, i;
  348. struct usb_card_rec *card;
  349. u16 id_vendor, id_product, bcd_device;
  350. card = devm_kzalloc(&intf->dev, sizeof(*card), GFP_KERNEL);
  351. if (!card)
  352. return -ENOMEM;
  353. init_completion(&card->fw_done);
  354. id_vendor = le16_to_cpu(udev->descriptor.idVendor);
  355. id_product = le16_to_cpu(udev->descriptor.idProduct);
  356. bcd_device = le16_to_cpu(udev->descriptor.bcdDevice);
  357. pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n",
  358. id_vendor, id_product, bcd_device);
  359. /* PID_1 is used for firmware downloading only */
  360. switch (id_product) {
  361. case USB8766_PID_1:
  362. case USB8797_PID_1:
  363. case USB8801_PID_1:
  364. case USB8997_PID_1:
  365. card->usb_boot_state = USB8XXX_FW_DNLD;
  366. break;
  367. case USB8766_PID_2:
  368. case USB8797_PID_2:
  369. case USB8801_PID_2:
  370. case USB8997_PID_2:
  371. card->usb_boot_state = USB8XXX_FW_READY;
  372. break;
  373. default:
  374. pr_warn("unknown id_product %#x\n", id_product);
  375. card->usb_boot_state = USB8XXX_FW_DNLD;
  376. break;
  377. }
  378. card->udev = udev;
  379. card->intf = intf;
  380. pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n",
  381. le16_to_cpu(udev->descriptor.bcdUSB),
  382. udev->descriptor.bDeviceClass,
  383. udev->descriptor.bDeviceSubClass,
  384. udev->descriptor.bDeviceProtocol);
  385. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  386. epd = &iface_desc->endpoint[i].desc;
  387. if (usb_endpoint_dir_in(epd) &&
  388. usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
  389. (usb_endpoint_xfer_bulk(epd) ||
  390. usb_endpoint_xfer_int(epd))) {
  391. card->rx_cmd_ep_type = usb_endpoint_type(epd);
  392. card->rx_cmd_interval = epd->bInterval;
  393. pr_debug("info: Rx CMD/EVT:: max pkt size: %d, addr: %d, ep_type: %d\n",
  394. le16_to_cpu(epd->wMaxPacketSize),
  395. epd->bEndpointAddress, card->rx_cmd_ep_type);
  396. card->rx_cmd_ep = usb_endpoint_num(epd);
  397. atomic_set(&card->rx_cmd_urb_pending, 0);
  398. }
  399. if (usb_endpoint_dir_in(epd) &&
  400. usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
  401. usb_endpoint_xfer_bulk(epd)) {
  402. pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
  403. le16_to_cpu(epd->wMaxPacketSize),
  404. epd->bEndpointAddress);
  405. card->rx_data_ep = usb_endpoint_num(epd);
  406. atomic_set(&card->rx_data_urb_pending, 0);
  407. }
  408. if (usb_endpoint_dir_out(epd) &&
  409. usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
  410. usb_endpoint_xfer_bulk(epd)) {
  411. pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
  412. le16_to_cpu(epd->wMaxPacketSize),
  413. epd->bEndpointAddress);
  414. card->port[0].tx_data_ep = usb_endpoint_num(epd);
  415. atomic_set(&card->port[0].tx_data_urb_pending, 0);
  416. }
  417. if (usb_endpoint_dir_out(epd) &&
  418. usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA_CH2 &&
  419. usb_endpoint_xfer_bulk(epd)) {
  420. pr_debug("info: bulk OUT chan2:\t"
  421. "max pkt size: %d, addr: %d\n",
  422. le16_to_cpu(epd->wMaxPacketSize),
  423. epd->bEndpointAddress);
  424. card->port[1].tx_data_ep = usb_endpoint_num(epd);
  425. atomic_set(&card->port[1].tx_data_urb_pending, 0);
  426. }
  427. if (usb_endpoint_dir_out(epd) &&
  428. usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
  429. (usb_endpoint_xfer_bulk(epd) ||
  430. usb_endpoint_xfer_int(epd))) {
  431. card->tx_cmd_ep_type = usb_endpoint_type(epd);
  432. card->tx_cmd_interval = epd->bInterval;
  433. pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
  434. le16_to_cpu(epd->wMaxPacketSize),
  435. epd->bEndpointAddress);
  436. pr_debug("info: Tx CMD:: max pkt size: %d, addr: %d, ep_type: %d\n",
  437. le16_to_cpu(epd->wMaxPacketSize),
  438. epd->bEndpointAddress, card->tx_cmd_ep_type);
  439. card->tx_cmd_ep = usb_endpoint_num(epd);
  440. atomic_set(&card->tx_cmd_urb_pending, 0);
  441. card->bulk_out_maxpktsize =
  442. le16_to_cpu(epd->wMaxPacketSize);
  443. }
  444. }
  445. usb_set_intfdata(intf, card);
  446. ret = mwifiex_add_card(card, &card->fw_done, &usb_ops,
  447. MWIFIEX_USB, &card->udev->dev);
  448. if (ret) {
  449. pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret);
  450. usb_reset_device(udev);
  451. return ret;
  452. }
  453. usb_get_dev(udev);
  454. return 0;
  455. }
  456. /* Kernel needs to suspend all functions separately. Therefore all
  457. * registered functions must have drivers with suspend and resume
  458. * methods. Failing that the kernel simply removes the whole card.
  459. *
  460. * If already not suspended, this function allocates and sends a
  461. * 'host sleep activate' request to the firmware and turns off the traffic.
  462. */
  463. static int mwifiex_usb_suspend(struct usb_interface *intf, pm_message_t message)
  464. {
  465. struct usb_card_rec *card = usb_get_intfdata(intf);
  466. struct mwifiex_adapter *adapter;
  467. struct usb_tx_data_port *port;
  468. int i, j;
  469. /* Might still be loading firmware */
  470. wait_for_completion(&card->fw_done);
  471. adapter = card->adapter;
  472. if (!adapter) {
  473. dev_err(&intf->dev, "card is not valid\n");
  474. return 0;
  475. }
  476. if (unlikely(test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)))
  477. mwifiex_dbg(adapter, WARN,
  478. "Device already suspended\n");
  479. /* Enable the Host Sleep */
  480. if (!mwifiex_enable_hs(adapter)) {
  481. mwifiex_dbg(adapter, ERROR,
  482. "cmd: failed to suspend\n");
  483. clear_bit(MWIFIEX_IS_HS_ENABLING, &adapter->work_flags);
  484. return -EFAULT;
  485. }
  486. /* 'MWIFIEX_IS_SUSPENDED' bit indicates device is suspended.
  487. * It must be set here before the usb_kill_urb() calls. Reason
  488. * is in the complete handlers, urb->status(= -ENOENT) and
  489. * this flag is used in combination to distinguish between a
  490. * 'suspended' state and a 'disconnect' one.
  491. */
  492. set_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
  493. clear_bit(MWIFIEX_IS_HS_ENABLING, &adapter->work_flags);
  494. if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
  495. usb_kill_urb(card->rx_cmd.urb);
  496. if (atomic_read(&card->rx_data_urb_pending))
  497. for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
  498. if (card->rx_data_list[i].urb)
  499. usb_kill_urb(card->rx_data_list[i].urb);
  500. for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
  501. port = &card->port[i];
  502. for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
  503. if (port->tx_data_list[j].urb)
  504. usb_kill_urb(port->tx_data_list[j].urb);
  505. }
  506. }
  507. if (card->tx_cmd.urb)
  508. usb_kill_urb(card->tx_cmd.urb);
  509. return 0;
  510. }
  511. /* Kernel needs to suspend all functions separately. Therefore all
  512. * registered functions must have drivers with suspend and resume
  513. * methods. Failing that the kernel simply removes the whole card.
  514. *
  515. * If already not resumed, this function turns on the traffic and
  516. * sends a 'host sleep cancel' request to the firmware.
  517. */
  518. static int mwifiex_usb_resume(struct usb_interface *intf)
  519. {
  520. struct usb_card_rec *card = usb_get_intfdata(intf);
  521. struct mwifiex_adapter *adapter;
  522. int i;
  523. if (!card->adapter) {
  524. dev_err(&intf->dev, "%s: card->adapter is NULL\n",
  525. __func__);
  526. return 0;
  527. }
  528. adapter = card->adapter;
  529. if (unlikely(!test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags))) {
  530. mwifiex_dbg(adapter, WARN,
  531. "Device already resumed\n");
  532. return 0;
  533. }
  534. /* Indicate device resumed. The netdev queue will be resumed only
  535. * after the urbs have been re-submitted
  536. */
  537. clear_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags);
  538. if (!atomic_read(&card->rx_data_urb_pending))
  539. for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
  540. mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
  541. MWIFIEX_RX_DATA_BUF_SIZE);
  542. if (!atomic_read(&card->rx_cmd_urb_pending)) {
  543. card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
  544. if (card->rx_cmd.skb)
  545. mwifiex_usb_submit_rx_urb(&card->rx_cmd,
  546. MWIFIEX_RX_CMD_BUF_SIZE);
  547. }
  548. /* Disable Host Sleep */
  549. if (adapter->hs_activated)
  550. mwifiex_cancel_hs(mwifiex_get_priv(adapter,
  551. MWIFIEX_BSS_ROLE_ANY),
  552. MWIFIEX_ASYNC_CMD);
  553. return 0;
  554. }
  555. static void mwifiex_usb_disconnect(struct usb_interface *intf)
  556. {
  557. struct usb_card_rec *card = usb_get_intfdata(intf);
  558. struct mwifiex_adapter *adapter;
  559. wait_for_completion(&card->fw_done);
  560. adapter = card->adapter;
  561. if (!adapter || !adapter->priv_num)
  562. return;
  563. if (card->udev->state != USB_STATE_NOTATTACHED && !adapter->mfg_mode) {
  564. mwifiex_deauthenticate_all(adapter);
  565. mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
  566. MWIFIEX_BSS_ROLE_ANY),
  567. MWIFIEX_FUNC_SHUTDOWN);
  568. }
  569. mwifiex_dbg(adapter, FATAL,
  570. "%s: removing card\n", __func__);
  571. mwifiex_remove_card(adapter);
  572. usb_put_dev(interface_to_usbdev(intf));
  573. }
  574. static void mwifiex_usb_coredump(struct device *dev)
  575. {
  576. struct usb_interface *intf = to_usb_interface(dev);
  577. struct usb_card_rec *card = usb_get_intfdata(intf);
  578. mwifiex_fw_dump_event(mwifiex_get_priv(card->adapter,
  579. MWIFIEX_BSS_ROLE_ANY));
  580. }
  581. static struct usb_driver mwifiex_usb_driver = {
  582. .name = "mwifiex_usb",
  583. .probe = mwifiex_usb_probe,
  584. .disconnect = mwifiex_usb_disconnect,
  585. .id_table = mwifiex_usb_table,
  586. .suspend = mwifiex_usb_suspend,
  587. .resume = mwifiex_usb_resume,
  588. .soft_unbind = 1,
  589. .drvwrap.driver = {
  590. .coredump = mwifiex_usb_coredump,
  591. },
  592. };
  593. static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
  594. u32 *len, u8 ep, u32 timeout)
  595. {
  596. struct usb_card_rec *card = adapter->card;
  597. int actual_length, ret;
  598. if (!(*len % card->bulk_out_maxpktsize))
  599. (*len)++;
  600. /* Send the data block */
  601. ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf,
  602. *len, &actual_length, timeout);
  603. if (ret) {
  604. mwifiex_dbg(adapter, ERROR,
  605. "usb_bulk_msg for tx failed: %d\n", ret);
  606. return ret;
  607. }
  608. *len = actual_length;
  609. return ret;
  610. }
  611. static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
  612. u32 *len, u8 ep, u32 timeout)
  613. {
  614. struct usb_card_rec *card = adapter->card;
  615. int actual_length, ret;
  616. /* Receive the data response */
  617. ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf,
  618. *len, &actual_length, timeout);
  619. if (ret) {
  620. mwifiex_dbg(adapter, ERROR,
  621. "usb_bulk_msg for rx failed: %d\n", ret);
  622. return ret;
  623. }
  624. *len = actual_length;
  625. return ret;
  626. }
  627. static void mwifiex_usb_port_resync(struct mwifiex_adapter *adapter)
  628. {
  629. struct usb_card_rec *card = adapter->card;
  630. u8 active_port = MWIFIEX_USB_EP_DATA;
  631. struct mwifiex_private *priv = NULL;
  632. int i;
  633. if (adapter->usb_mc_status) {
  634. for (i = 0; i < adapter->priv_num; i++) {
  635. priv = adapter->priv[i];
  636. if (!priv)
  637. continue;
  638. if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
  639. !priv->bss_started) ||
  640. (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
  641. !priv->media_connected))
  642. priv->usb_port = MWIFIEX_USB_EP_DATA;
  643. }
  644. for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
  645. card->port[i].block_status = false;
  646. } else {
  647. for (i = 0; i < adapter->priv_num; i++) {
  648. priv = adapter->priv[i];
  649. if (!priv)
  650. continue;
  651. if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
  652. priv->bss_started) ||
  653. (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
  654. priv->media_connected)) {
  655. active_port = priv->usb_port;
  656. break;
  657. }
  658. }
  659. for (i = 0; i < adapter->priv_num; i++) {
  660. priv = adapter->priv[i];
  661. if (priv)
  662. priv->usb_port = active_port;
  663. }
  664. for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
  665. if (active_port == card->port[i].tx_data_ep)
  666. card->port[i].block_status = false;
  667. else
  668. card->port[i].block_status = true;
  669. }
  670. }
  671. }
  672. static bool mwifiex_usb_is_port_ready(struct mwifiex_private *priv)
  673. {
  674. struct usb_card_rec *card = priv->adapter->card;
  675. int idx;
  676. for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
  677. if (priv->usb_port == card->port[idx].tx_data_ep)
  678. return !card->port[idx].block_status;
  679. }
  680. return false;
  681. }
  682. static inline u8 mwifiex_usb_data_sent(struct mwifiex_adapter *adapter)
  683. {
  684. struct usb_card_rec *card = adapter->card;
  685. int i;
  686. for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
  687. if (!card->port[i].block_status)
  688. return false;
  689. return true;
  690. }
  691. static int mwifiex_usb_construct_send_urb(struct mwifiex_adapter *adapter,
  692. struct usb_tx_data_port *port, u8 ep,
  693. struct urb_context *context,
  694. struct sk_buff *skb_send)
  695. {
  696. struct usb_card_rec *card = adapter->card;
  697. int ret = -EINPROGRESS;
  698. struct urb *tx_urb;
  699. context->adapter = adapter;
  700. context->ep = ep;
  701. context->skb = skb_send;
  702. tx_urb = context->urb;
  703. if (ep == card->tx_cmd_ep &&
  704. card->tx_cmd_ep_type == USB_ENDPOINT_XFER_INT)
  705. usb_fill_int_urb(tx_urb, card->udev,
  706. usb_sndintpipe(card->udev, ep), skb_send->data,
  707. skb_send->len, mwifiex_usb_tx_complete,
  708. (void *)context, card->tx_cmd_interval);
  709. else
  710. usb_fill_bulk_urb(tx_urb, card->udev,
  711. usb_sndbulkpipe(card->udev, ep),
  712. skb_send->data, skb_send->len,
  713. mwifiex_usb_tx_complete, (void *)context);
  714. tx_urb->transfer_flags |= URB_ZERO_PACKET;
  715. if (ep == card->tx_cmd_ep)
  716. atomic_inc(&card->tx_cmd_urb_pending);
  717. else
  718. atomic_inc(&port->tx_data_urb_pending);
  719. if (ep != card->tx_cmd_ep &&
  720. atomic_read(&port->tx_data_urb_pending) ==
  721. MWIFIEX_TX_DATA_URB) {
  722. port->block_status = true;
  723. adapter->data_sent = mwifiex_usb_data_sent(adapter);
  724. ret = -ENOSR;
  725. }
  726. if (usb_submit_urb(tx_urb, GFP_ATOMIC)) {
  727. mwifiex_dbg(adapter, ERROR,
  728. "%s: usb_submit_urb failed\n", __func__);
  729. if (ep == card->tx_cmd_ep) {
  730. atomic_dec(&card->tx_cmd_urb_pending);
  731. } else {
  732. atomic_dec(&port->tx_data_urb_pending);
  733. port->block_status = false;
  734. adapter->data_sent = false;
  735. if (port->tx_data_ix)
  736. port->tx_data_ix--;
  737. else
  738. port->tx_data_ix = MWIFIEX_TX_DATA_URB;
  739. }
  740. ret = -1;
  741. }
  742. return ret;
  743. }
  744. static int mwifiex_usb_prepare_tx_aggr_skb(struct mwifiex_adapter *adapter,
  745. struct usb_tx_data_port *port,
  746. struct sk_buff **skb_send)
  747. {
  748. struct sk_buff *skb_aggr, *skb_tmp;
  749. u8 *payload, pad;
  750. u16 align = adapter->bus_aggr.tx_aggr_align;
  751. struct mwifiex_txinfo *tx_info = NULL;
  752. bool is_txinfo_set = false;
  753. /* Packets in aggr_list will be send in either skb_aggr or
  754. * write complete, delete the tx_aggr timer
  755. */
  756. if (port->tx_aggr.timer_cnxt.is_hold_timer_set) {
  757. del_timer(&port->tx_aggr.timer_cnxt.hold_timer);
  758. port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
  759. port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
  760. }
  761. skb_aggr = mwifiex_alloc_dma_align_buf(port->tx_aggr.aggr_len,
  762. GFP_ATOMIC);
  763. if (!skb_aggr) {
  764. mwifiex_dbg(adapter, ERROR,
  765. "%s: alloc skb_aggr failed\n", __func__);
  766. while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list)))
  767. mwifiex_write_data_complete(adapter, skb_tmp, 0, -1);
  768. port->tx_aggr.aggr_num = 0;
  769. port->tx_aggr.aggr_len = 0;
  770. return -EBUSY;
  771. }
  772. tx_info = MWIFIEX_SKB_TXCB(skb_aggr);
  773. memset(tx_info, 0, sizeof(*tx_info));
  774. while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list))) {
  775. /* padding for aligning next packet header*/
  776. pad = (align - (skb_tmp->len & (align - 1))) % align;
  777. payload = skb_put(skb_aggr, skb_tmp->len + pad);
  778. memcpy(payload, skb_tmp->data, skb_tmp->len);
  779. if (skb_queue_empty(&port->tx_aggr.aggr_list)) {
  780. /* do not padding for last packet*/
  781. *(u16 *)payload = cpu_to_le16(skb_tmp->len);
  782. *(u16 *)&payload[2] =
  783. cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80);
  784. skb_trim(skb_aggr, skb_aggr->len - pad);
  785. } else {
  786. /* add aggregation interface header */
  787. *(u16 *)payload = cpu_to_le16(skb_tmp->len + pad);
  788. *(u16 *)&payload[2] =
  789. cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2);
  790. }
  791. if (!is_txinfo_set) {
  792. tx_info->bss_num = MWIFIEX_SKB_TXCB(skb_tmp)->bss_num;
  793. tx_info->bss_type = MWIFIEX_SKB_TXCB(skb_tmp)->bss_type;
  794. is_txinfo_set = true;
  795. }
  796. port->tx_aggr.aggr_num--;
  797. port->tx_aggr.aggr_len -= (skb_tmp->len + pad);
  798. mwifiex_write_data_complete(adapter, skb_tmp, 0, 0);
  799. }
  800. tx_info->pkt_len = skb_aggr->len -
  801. (sizeof(struct txpd) + adapter->intf_hdr_len);
  802. tx_info->flags |= MWIFIEX_BUF_FLAG_AGGR_PKT;
  803. port->tx_aggr.aggr_num = 0;
  804. port->tx_aggr.aggr_len = 0;
  805. *skb_send = skb_aggr;
  806. return 0;
  807. }
  808. /* This function prepare data packet to be send under usb tx aggregation
  809. * protocol, check current usb aggregation status, link packet to aggrgation
  810. * list if possible, work flow as below:
  811. * (1) if only 1 packet available, add usb tx aggregation header and send.
  812. * (2) if packet is able to aggregated, link it to current aggregation list.
  813. * (3) if packet is not able to aggregated, aggregate and send exist packets
  814. * in aggrgation list. Then, link packet in the list if there is more
  815. * packet in transmit queue, otherwise try to transmit single packet.
  816. */
  817. static int mwifiex_usb_aggr_tx_data(struct mwifiex_adapter *adapter, u8 ep,
  818. struct sk_buff *skb,
  819. struct mwifiex_tx_param *tx_param,
  820. struct usb_tx_data_port *port)
  821. {
  822. u8 *payload, pad;
  823. u16 align = adapter->bus_aggr.tx_aggr_align;
  824. struct sk_buff *skb_send = NULL;
  825. struct urb_context *context = NULL;
  826. struct txpd *local_tx_pd =
  827. (struct txpd *)((u8 *)skb->data + adapter->intf_hdr_len);
  828. u8 f_send_aggr_buf = 0;
  829. u8 f_send_cur_buf = 0;
  830. u8 f_precopy_cur_buf = 0;
  831. u8 f_postcopy_cur_buf = 0;
  832. u32 timeout;
  833. int ret;
  834. /* padding to ensure each packet alginment */
  835. pad = (align - (skb->len & (align - 1))) % align;
  836. if (tx_param && tx_param->next_pkt_len) {
  837. /* next packet available in tx queue*/
  838. if (port->tx_aggr.aggr_len + skb->len + pad >
  839. adapter->bus_aggr.tx_aggr_max_size) {
  840. f_send_aggr_buf = 1;
  841. f_postcopy_cur_buf = 1;
  842. } else {
  843. /* current packet could be aggregated*/
  844. f_precopy_cur_buf = 1;
  845. if (port->tx_aggr.aggr_len + skb->len + pad +
  846. tx_param->next_pkt_len >
  847. adapter->bus_aggr.tx_aggr_max_size ||
  848. port->tx_aggr.aggr_num + 2 >
  849. adapter->bus_aggr.tx_aggr_max_num) {
  850. /* next packet could not be aggregated
  851. * send current aggregation buffer
  852. */
  853. f_send_aggr_buf = 1;
  854. }
  855. }
  856. } else {
  857. /* last packet in tx queue */
  858. if (port->tx_aggr.aggr_num > 0) {
  859. /* pending packets in aggregation buffer*/
  860. if (port->tx_aggr.aggr_len + skb->len + pad >
  861. adapter->bus_aggr.tx_aggr_max_size) {
  862. /* current packet not be able to aggregated,
  863. * send aggr buffer first, then send packet.
  864. */
  865. f_send_cur_buf = 1;
  866. } else {
  867. /* last packet, Aggregation and send */
  868. f_precopy_cur_buf = 1;
  869. }
  870. f_send_aggr_buf = 1;
  871. } else {
  872. /* no pending packets in aggregation buffer,
  873. * send current packet immediately
  874. */
  875. f_send_cur_buf = 1;
  876. }
  877. }
  878. if (local_tx_pd->flags & MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET) {
  879. /* Send NULL packet immediately*/
  880. if (f_precopy_cur_buf) {
  881. if (skb_queue_empty(&port->tx_aggr.aggr_list)) {
  882. f_precopy_cur_buf = 0;
  883. f_send_aggr_buf = 0;
  884. f_send_cur_buf = 1;
  885. } else {
  886. f_send_aggr_buf = 1;
  887. }
  888. } else if (f_postcopy_cur_buf) {
  889. f_send_cur_buf = 1;
  890. f_postcopy_cur_buf = 0;
  891. }
  892. }
  893. if (f_precopy_cur_buf) {
  894. skb_queue_tail(&port->tx_aggr.aggr_list, skb);
  895. port->tx_aggr.aggr_len += (skb->len + pad);
  896. port->tx_aggr.aggr_num++;
  897. if (f_send_aggr_buf)
  898. goto send_aggr_buf;
  899. /* packet will not been send immediately,
  900. * set a timer to make sure it will be sent under
  901. * strict time limit. Dynamically fit the timeout
  902. * value, according to packets number in aggr_list
  903. */
  904. if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) {
  905. port->tx_aggr.timer_cnxt.hold_tmo_msecs =
  906. MWIFIEX_USB_TX_AGGR_TMO_MIN;
  907. timeout =
  908. port->tx_aggr.timer_cnxt.hold_tmo_msecs;
  909. mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
  910. jiffies + msecs_to_jiffies(timeout));
  911. port->tx_aggr.timer_cnxt.is_hold_timer_set = true;
  912. } else {
  913. if (port->tx_aggr.timer_cnxt.hold_tmo_msecs <
  914. MWIFIEX_USB_TX_AGGR_TMO_MAX) {
  915. /* Dyanmic fit timeout */
  916. timeout =
  917. ++port->tx_aggr.timer_cnxt.hold_tmo_msecs;
  918. mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
  919. jiffies + msecs_to_jiffies(timeout));
  920. }
  921. }
  922. }
  923. send_aggr_buf:
  924. if (f_send_aggr_buf) {
  925. ret = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send);
  926. if (!ret) {
  927. context = &port->tx_data_list[port->tx_data_ix++];
  928. ret = mwifiex_usb_construct_send_urb(adapter, port, ep,
  929. context, skb_send);
  930. if (ret == -1)
  931. mwifiex_write_data_complete(adapter, skb_send,
  932. 0, -1);
  933. }
  934. }
  935. if (f_send_cur_buf) {
  936. if (f_send_aggr_buf) {
  937. if (atomic_read(&port->tx_data_urb_pending) >=
  938. MWIFIEX_TX_DATA_URB) {
  939. port->block_status = true;
  940. adapter->data_sent =
  941. mwifiex_usb_data_sent(adapter);
  942. /* no available urb, postcopy packet*/
  943. f_postcopy_cur_buf = 1;
  944. goto postcopy_cur_buf;
  945. }
  946. if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
  947. port->tx_data_ix = 0;
  948. }
  949. payload = skb->data;
  950. *(u16 *)&payload[2] =
  951. cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80);
  952. *(u16 *)payload = cpu_to_le16(skb->len);
  953. skb_send = skb;
  954. context = &port->tx_data_list[port->tx_data_ix++];
  955. return mwifiex_usb_construct_send_urb(adapter, port, ep,
  956. context, skb_send);
  957. }
  958. postcopy_cur_buf:
  959. if (f_postcopy_cur_buf) {
  960. skb_queue_tail(&port->tx_aggr.aggr_list, skb);
  961. port->tx_aggr.aggr_len += (skb->len + pad);
  962. port->tx_aggr.aggr_num++;
  963. /* New aggregation begin, start timer */
  964. if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) {
  965. port->tx_aggr.timer_cnxt.hold_tmo_msecs =
  966. MWIFIEX_USB_TX_AGGR_TMO_MIN;
  967. timeout = port->tx_aggr.timer_cnxt.hold_tmo_msecs;
  968. mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
  969. jiffies + msecs_to_jiffies(timeout));
  970. port->tx_aggr.timer_cnxt.is_hold_timer_set = true;
  971. }
  972. }
  973. return -EINPROGRESS;
  974. }
  975. static void mwifiex_usb_tx_aggr_tmo(struct timer_list *t)
  976. {
  977. struct urb_context *urb_cnxt = NULL;
  978. struct sk_buff *skb_send = NULL;
  979. struct tx_aggr_tmr_cnxt *timer_context =
  980. from_timer(timer_context, t, hold_timer);
  981. struct mwifiex_adapter *adapter = timer_context->adapter;
  982. struct usb_tx_data_port *port = timer_context->port;
  983. int err = 0;
  984. spin_lock_bh(&port->tx_aggr_lock);
  985. err = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send);
  986. if (err) {
  987. mwifiex_dbg(adapter, ERROR,
  988. "prepare tx aggr skb failed, err=%d\n", err);
  989. goto unlock;
  990. }
  991. if (atomic_read(&port->tx_data_urb_pending) >=
  992. MWIFIEX_TX_DATA_URB) {
  993. port->block_status = true;
  994. adapter->data_sent =
  995. mwifiex_usb_data_sent(adapter);
  996. err = -1;
  997. goto done;
  998. }
  999. if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
  1000. port->tx_data_ix = 0;
  1001. urb_cnxt = &port->tx_data_list[port->tx_data_ix++];
  1002. err = mwifiex_usb_construct_send_urb(adapter, port, port->tx_data_ep,
  1003. urb_cnxt, skb_send);
  1004. done:
  1005. if (err == -1)
  1006. mwifiex_write_data_complete(adapter, skb_send, 0, -1);
  1007. unlock:
  1008. spin_unlock_bh(&port->tx_aggr_lock);
  1009. }
  1010. /* This function write a command/data packet to card. */
  1011. static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep,
  1012. struct sk_buff *skb,
  1013. struct mwifiex_tx_param *tx_param)
  1014. {
  1015. struct usb_card_rec *card = adapter->card;
  1016. struct urb_context *context = NULL;
  1017. struct usb_tx_data_port *port = NULL;
  1018. int idx, ret;
  1019. if (test_bit(MWIFIEX_IS_SUSPENDED, &adapter->work_flags)) {
  1020. mwifiex_dbg(adapter, ERROR,
  1021. "%s: not allowed while suspended\n", __func__);
  1022. return -1;
  1023. }
  1024. if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags)) {
  1025. mwifiex_dbg(adapter, ERROR, "%s: device removed\n", __func__);
  1026. return -1;
  1027. }
  1028. mwifiex_dbg(adapter, INFO, "%s: ep=%d\n", __func__, ep);
  1029. if (ep == card->tx_cmd_ep) {
  1030. context = &card->tx_cmd;
  1031. } else {
  1032. /* get the data port structure for endpoint */
  1033. for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
  1034. if (ep == card->port[idx].tx_data_ep) {
  1035. port = &card->port[idx];
  1036. if (atomic_read(&port->tx_data_urb_pending)
  1037. >= MWIFIEX_TX_DATA_URB) {
  1038. port->block_status = true;
  1039. adapter->data_sent =
  1040. mwifiex_usb_data_sent(adapter);
  1041. return -EBUSY;
  1042. }
  1043. if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
  1044. port->tx_data_ix = 0;
  1045. break;
  1046. }
  1047. }
  1048. if (!port) {
  1049. mwifiex_dbg(adapter, ERROR, "Wrong usb tx data port\n");
  1050. return -1;
  1051. }
  1052. if (adapter->bus_aggr.enable) {
  1053. spin_lock_bh(&port->tx_aggr_lock);
  1054. ret = mwifiex_usb_aggr_tx_data(adapter, ep, skb,
  1055. tx_param, port);
  1056. spin_unlock_bh(&port->tx_aggr_lock);
  1057. return ret;
  1058. }
  1059. context = &port->tx_data_list[port->tx_data_ix++];
  1060. }
  1061. return mwifiex_usb_construct_send_urb(adapter, port, ep, context, skb);
  1062. }
  1063. static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter)
  1064. {
  1065. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  1066. struct usb_tx_data_port *port;
  1067. int i, j;
  1068. card->tx_cmd.adapter = adapter;
  1069. card->tx_cmd.ep = card->tx_cmd_ep;
  1070. card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
  1071. if (!card->tx_cmd.urb)
  1072. return -ENOMEM;
  1073. for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
  1074. port = &card->port[i];
  1075. if (!port->tx_data_ep)
  1076. continue;
  1077. port->tx_data_ix = 0;
  1078. skb_queue_head_init(&port->tx_aggr.aggr_list);
  1079. if (port->tx_data_ep == MWIFIEX_USB_EP_DATA)
  1080. port->block_status = false;
  1081. else
  1082. port->block_status = true;
  1083. for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
  1084. port->tx_data_list[j].adapter = adapter;
  1085. port->tx_data_list[j].ep = port->tx_data_ep;
  1086. port->tx_data_list[j].urb =
  1087. usb_alloc_urb(0, GFP_KERNEL);
  1088. if (!port->tx_data_list[j].urb)
  1089. return -ENOMEM;
  1090. }
  1091. port->tx_aggr.timer_cnxt.adapter = adapter;
  1092. port->tx_aggr.timer_cnxt.port = port;
  1093. port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
  1094. port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
  1095. timer_setup(&port->tx_aggr.timer_cnxt.hold_timer,
  1096. mwifiex_usb_tx_aggr_tmo, 0);
  1097. }
  1098. return 0;
  1099. }
  1100. static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter)
  1101. {
  1102. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  1103. int i;
  1104. card->rx_cmd.adapter = adapter;
  1105. card->rx_cmd.ep = card->rx_cmd_ep;
  1106. card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
  1107. if (!card->rx_cmd.urb)
  1108. return -ENOMEM;
  1109. card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
  1110. if (!card->rx_cmd.skb)
  1111. return -ENOMEM;
  1112. if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE))
  1113. return -1;
  1114. for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
  1115. card->rx_data_list[i].adapter = adapter;
  1116. card->rx_data_list[i].ep = card->rx_data_ep;
  1117. card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
  1118. if (!card->rx_data_list[i].urb)
  1119. return -1;
  1120. if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
  1121. MWIFIEX_RX_DATA_BUF_SIZE))
  1122. return -1;
  1123. }
  1124. return 0;
  1125. }
  1126. /* This function register usb device and initialize parameter. */
  1127. static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
  1128. {
  1129. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  1130. card->adapter = adapter;
  1131. switch (le16_to_cpu(card->udev->descriptor.idProduct)) {
  1132. case USB8997_PID_1:
  1133. case USB8997_PID_2:
  1134. adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K;
  1135. strcpy(adapter->fw_name, USB8997_DEFAULT_FW_NAME);
  1136. adapter->ext_scan = true;
  1137. break;
  1138. case USB8766_PID_1:
  1139. case USB8766_PID_2:
  1140. adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
  1141. strcpy(adapter->fw_name, USB8766_DEFAULT_FW_NAME);
  1142. adapter->ext_scan = true;
  1143. break;
  1144. case USB8801_PID_1:
  1145. case USB8801_PID_2:
  1146. adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
  1147. strcpy(adapter->fw_name, USB8801_DEFAULT_FW_NAME);
  1148. adapter->ext_scan = false;
  1149. break;
  1150. case USB8797_PID_1:
  1151. case USB8797_PID_2:
  1152. default:
  1153. adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
  1154. strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME);
  1155. break;
  1156. }
  1157. adapter->usb_mc_status = false;
  1158. adapter->usb_mc_setup = false;
  1159. return 0;
  1160. }
  1161. static void mwifiex_usb_cleanup_tx_aggr(struct mwifiex_adapter *adapter)
  1162. {
  1163. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  1164. struct usb_tx_data_port *port;
  1165. struct sk_buff *skb_tmp;
  1166. int idx;
  1167. for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
  1168. port = &card->port[idx];
  1169. if (adapter->bus_aggr.enable)
  1170. while ((skb_tmp =
  1171. skb_dequeue(&port->tx_aggr.aggr_list)))
  1172. mwifiex_write_data_complete(adapter, skb_tmp,
  1173. 0, -1);
  1174. if (port->tx_aggr.timer_cnxt.hold_timer.function)
  1175. del_timer_sync(&port->tx_aggr.timer_cnxt.hold_timer);
  1176. port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
  1177. port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
  1178. }
  1179. }
  1180. static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
  1181. {
  1182. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  1183. mwifiex_usb_free(card);
  1184. mwifiex_usb_cleanup_tx_aggr(adapter);
  1185. card->adapter = NULL;
  1186. }
  1187. static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
  1188. struct mwifiex_fw_image *fw)
  1189. {
  1190. int ret = 0;
  1191. u8 *firmware = fw->fw_buf, *recv_buff;
  1192. u32 retries = USB8XXX_FW_MAX_RETRY + 1;
  1193. u32 dlen;
  1194. u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0;
  1195. struct fw_data *fwdata;
  1196. struct fw_sync_header sync_fw;
  1197. u8 check_winner = 1;
  1198. if (!firmware) {
  1199. mwifiex_dbg(adapter, ERROR,
  1200. "No firmware image found! Terminating download\n");
  1201. ret = -1;
  1202. goto fw_exit;
  1203. }
  1204. /* Allocate memory for transmit */
  1205. fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL);
  1206. if (!fwdata) {
  1207. ret = -ENOMEM;
  1208. goto fw_exit;
  1209. }
  1210. /* Allocate memory for receive */
  1211. recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL);
  1212. if (!recv_buff) {
  1213. ret = -ENOMEM;
  1214. goto cleanup;
  1215. }
  1216. do {
  1217. /* Send pseudo data to check winner status first */
  1218. if (check_winner) {
  1219. memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header));
  1220. dlen = 0;
  1221. } else {
  1222. /* copy the header of the fw_data to get the length */
  1223. memcpy(&fwdata->fw_hdr, &firmware[tlen],
  1224. sizeof(struct fw_header));
  1225. dlen = le32_to_cpu(fwdata->fw_hdr.data_len);
  1226. dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd);
  1227. tlen += sizeof(struct fw_header);
  1228. /* Command 7 doesn't have data length field */
  1229. if (dnld_cmd == FW_CMD_7)
  1230. dlen = 0;
  1231. memcpy(fwdata->data, &firmware[tlen], dlen);
  1232. fwdata->seq_num = cpu_to_le32(fw_seqnum);
  1233. tlen += dlen;
  1234. }
  1235. /* If the send/receive fails or CRC occurs then retry */
  1236. while (--retries) {
  1237. u8 *buf = (u8 *)fwdata;
  1238. u32 len = FW_DATA_XMIT_SIZE;
  1239. /* send the firmware block */
  1240. ret = mwifiex_write_data_sync(adapter, buf, &len,
  1241. MWIFIEX_USB_EP_CMD_EVENT,
  1242. MWIFIEX_USB_TIMEOUT);
  1243. if (ret) {
  1244. mwifiex_dbg(adapter, ERROR,
  1245. "write_data_sync: failed: %d\n",
  1246. ret);
  1247. continue;
  1248. }
  1249. buf = recv_buff;
  1250. len = FW_DNLD_RX_BUF_SIZE;
  1251. /* Receive the firmware block response */
  1252. ret = mwifiex_read_data_sync(adapter, buf, &len,
  1253. MWIFIEX_USB_EP_CMD_EVENT,
  1254. MWIFIEX_USB_TIMEOUT);
  1255. if (ret) {
  1256. mwifiex_dbg(adapter, ERROR,
  1257. "read_data_sync: failed: %d\n",
  1258. ret);
  1259. continue;
  1260. }
  1261. memcpy(&sync_fw, recv_buff,
  1262. sizeof(struct fw_sync_header));
  1263. /* check 1st firmware block resp for highest bit set */
  1264. if (check_winner) {
  1265. if (le32_to_cpu(sync_fw.cmd) & 0x80000000) {
  1266. mwifiex_dbg(adapter, WARN,
  1267. "USB is not the winner %#x\n",
  1268. sync_fw.cmd);
  1269. /* returning success */
  1270. ret = 0;
  1271. goto cleanup;
  1272. }
  1273. mwifiex_dbg(adapter, MSG,
  1274. "start to download FW...\n");
  1275. check_winner = 0;
  1276. break;
  1277. }
  1278. /* check the firmware block response for CRC errors */
  1279. if (sync_fw.cmd) {
  1280. mwifiex_dbg(adapter, ERROR,
  1281. "FW received block with CRC %#x\n",
  1282. sync_fw.cmd);
  1283. ret = -1;
  1284. continue;
  1285. }
  1286. retries = USB8XXX_FW_MAX_RETRY + 1;
  1287. break;
  1288. }
  1289. fw_seqnum++;
  1290. } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries);
  1291. cleanup:
  1292. mwifiex_dbg(adapter, MSG,
  1293. "info: FW download over, size %d bytes\n", tlen);
  1294. kfree(recv_buff);
  1295. kfree(fwdata);
  1296. if (retries)
  1297. ret = 0;
  1298. fw_exit:
  1299. return ret;
  1300. }
  1301. static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter,
  1302. struct mwifiex_fw_image *fw)
  1303. {
  1304. int ret;
  1305. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  1306. if (card->usb_boot_state == USB8XXX_FW_DNLD) {
  1307. ret = mwifiex_prog_fw_w_helper(adapter, fw);
  1308. if (ret)
  1309. return -1;
  1310. /* Boot state changes after successful firmware download */
  1311. if (card->usb_boot_state == USB8XXX_FW_DNLD)
  1312. return -1;
  1313. }
  1314. ret = mwifiex_usb_rx_init(adapter);
  1315. if (!ret)
  1316. ret = mwifiex_usb_tx_init(adapter);
  1317. return ret;
  1318. }
  1319. static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep)
  1320. {
  1321. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  1322. skb_push(card->rx_cmd.skb, INTF_HEADER_LEN);
  1323. if ((ep == card->rx_cmd_ep) &&
  1324. (!atomic_read(&card->rx_cmd_urb_pending)))
  1325. mwifiex_usb_submit_rx_urb(&card->rx_cmd,
  1326. MWIFIEX_RX_CMD_BUF_SIZE);
  1327. return;
  1328. }
  1329. static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter,
  1330. struct sk_buff *skb)
  1331. {
  1332. mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT);
  1333. return 0;
  1334. }
  1335. /* This function wakes up the card. */
  1336. static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
  1337. {
  1338. /* Simulation of HS_AWAKE event */
  1339. adapter->pm_wakeup_fw_try = false;
  1340. del_timer(&adapter->wakeup_timer);
  1341. adapter->pm_wakeup_card_req = false;
  1342. adapter->ps_state = PS_STATE_AWAKE;
  1343. return 0;
  1344. }
  1345. static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter *adapter)
  1346. {
  1347. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  1348. int i;
  1349. struct urb_context *ctx;
  1350. for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
  1351. if (card->rx_data_list[i].skb)
  1352. continue;
  1353. ctx = &card->rx_data_list[i];
  1354. mwifiex_usb_submit_rx_urb(ctx, MWIFIEX_RX_DATA_BUF_SIZE);
  1355. }
  1356. }
  1357. /* This function is called after the card has woken up. */
  1358. static inline int
  1359. mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
  1360. {
  1361. return 0;
  1362. }
  1363. static struct mwifiex_if_ops usb_ops = {
  1364. .register_dev = mwifiex_register_dev,
  1365. .unregister_dev = mwifiex_unregister_dev,
  1366. .wakeup = mwifiex_pm_wakeup_card,
  1367. .wakeup_complete = mwifiex_pm_wakeup_card_complete,
  1368. /* USB specific */
  1369. .dnld_fw = mwifiex_usb_dnld_fw,
  1370. .cmdrsp_complete = mwifiex_usb_cmd_event_complete,
  1371. .event_complete = mwifiex_usb_cmd_event_complete,
  1372. .host_to_card = mwifiex_usb_host_to_card,
  1373. .submit_rem_rx_urbs = mwifiex_usb_submit_rem_rx_urbs,
  1374. .multi_port_resync = mwifiex_usb_port_resync,
  1375. .is_port_ready = mwifiex_usb_is_port_ready,
  1376. };
  1377. module_usb_driver(mwifiex_usb_driver);
  1378. MODULE_AUTHOR("Marvell International Ltd.");
  1379. MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION);
  1380. MODULE_VERSION(USB_VERSION);
  1381. MODULE_LICENSE("GPL v2");
  1382. MODULE_FIRMWARE(USB8766_DEFAULT_FW_NAME);
  1383. MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME);
  1384. MODULE_FIRMWARE(USB8801_DEFAULT_FW_NAME);
  1385. MODULE_FIRMWARE(USB8997_DEFAULT_FW_NAME);