usb.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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 u8 user_rmmod;
  23. static struct mwifiex_if_ops usb_ops;
  24. static struct semaphore add_remove_card_sem;
  25. static struct usb_device_id mwifiex_usb_table[] = {
  26. /* 8766 */
  27. {USB_DEVICE(USB8XXX_VID, USB8766_PID_1)},
  28. {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8766_PID_2,
  29. USB_CLASS_VENDOR_SPEC,
  30. USB_SUBCLASS_VENDOR_SPEC, 0xff)},
  31. /* 8797 */
  32. {USB_DEVICE(USB8XXX_VID, USB8797_PID_1)},
  33. {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8797_PID_2,
  34. USB_CLASS_VENDOR_SPEC,
  35. USB_SUBCLASS_VENDOR_SPEC, 0xff)},
  36. /* 8801 */
  37. {USB_DEVICE(USB8XXX_VID, USB8801_PID_1)},
  38. {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8801_PID_2,
  39. USB_CLASS_VENDOR_SPEC,
  40. USB_SUBCLASS_VENDOR_SPEC, 0xff)},
  41. /* 8897 */
  42. {USB_DEVICE(USB8XXX_VID, USB8897_PID_1)},
  43. {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8897_PID_2,
  44. USB_CLASS_VENDOR_SPEC,
  45. USB_SUBCLASS_VENDOR_SPEC, 0xff)},
  46. { } /* Terminating entry */
  47. };
  48. MODULE_DEVICE_TABLE(usb, mwifiex_usb_table);
  49. static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size);
  50. /* This function handles received packet. Necessary action is taken based on
  51. * cmd/event/data.
  52. */
  53. static int mwifiex_usb_recv(struct mwifiex_adapter *adapter,
  54. struct sk_buff *skb, u8 ep)
  55. {
  56. u32 recv_type;
  57. __le32 tmp;
  58. int ret;
  59. if (adapter->hs_activated)
  60. mwifiex_process_hs_config(adapter);
  61. if (skb->len < INTF_HEADER_LEN) {
  62. mwifiex_dbg(adapter, ERROR,
  63. "%s: invalid skb->len\n", __func__);
  64. return -1;
  65. }
  66. switch (ep) {
  67. case MWIFIEX_USB_EP_CMD_EVENT:
  68. mwifiex_dbg(adapter, EVENT,
  69. "%s: EP_CMD_EVENT\n", __func__);
  70. skb_copy_from_linear_data(skb, &tmp, INTF_HEADER_LEN);
  71. recv_type = le32_to_cpu(tmp);
  72. skb_pull(skb, INTF_HEADER_LEN);
  73. switch (recv_type) {
  74. case MWIFIEX_USB_TYPE_CMD:
  75. if (skb->len > MWIFIEX_SIZE_OF_CMD_BUFFER) {
  76. mwifiex_dbg(adapter, ERROR,
  77. "CMD: skb->len too large\n");
  78. ret = -1;
  79. goto exit_restore_skb;
  80. } else if (!adapter->curr_cmd) {
  81. mwifiex_dbg(adapter, WARN, "CMD: no curr_cmd\n");
  82. if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
  83. mwifiex_process_sleep_confirm_resp(
  84. adapter, skb->data,
  85. skb->len);
  86. ret = 0;
  87. goto exit_restore_skb;
  88. }
  89. ret = -1;
  90. goto exit_restore_skb;
  91. }
  92. adapter->curr_cmd->resp_skb = skb;
  93. adapter->cmd_resp_received = true;
  94. break;
  95. case MWIFIEX_USB_TYPE_EVENT:
  96. if (skb->len < sizeof(u32)) {
  97. mwifiex_dbg(adapter, ERROR,
  98. "EVENT: skb->len too small\n");
  99. ret = -1;
  100. goto exit_restore_skb;
  101. }
  102. skb_copy_from_linear_data(skb, &tmp, sizeof(u32));
  103. adapter->event_cause = le32_to_cpu(tmp);
  104. mwifiex_dbg(adapter, EVENT,
  105. "event_cause %#x\n", adapter->event_cause);
  106. if (skb->len > MAX_EVENT_SIZE) {
  107. mwifiex_dbg(adapter, ERROR,
  108. "EVENT: event body too large\n");
  109. ret = -1;
  110. goto exit_restore_skb;
  111. }
  112. memcpy(adapter->event_body, skb->data +
  113. MWIFIEX_EVENT_HEADER_LEN, skb->len);
  114. adapter->event_received = true;
  115. adapter->event_skb = skb;
  116. break;
  117. default:
  118. mwifiex_dbg(adapter, ERROR,
  119. "unknown recv_type %#x\n", recv_type);
  120. return -1;
  121. }
  122. break;
  123. case MWIFIEX_USB_EP_DATA:
  124. mwifiex_dbg(adapter, DATA, "%s: EP_DATA\n", __func__);
  125. if (skb->len > MWIFIEX_RX_DATA_BUF_SIZE) {
  126. mwifiex_dbg(adapter, ERROR,
  127. "DATA: skb->len too large\n");
  128. return -1;
  129. }
  130. skb_queue_tail(&adapter->rx_data_q, skb);
  131. adapter->data_received = true;
  132. atomic_inc(&adapter->rx_pending);
  133. break;
  134. default:
  135. mwifiex_dbg(adapter, ERROR,
  136. "%s: unknown endport %#x\n", __func__, ep);
  137. return -1;
  138. }
  139. return -EINPROGRESS;
  140. exit_restore_skb:
  141. /* The buffer will be reused for further cmds/events */
  142. skb_push(skb, INTF_HEADER_LEN);
  143. return ret;
  144. }
  145. static void mwifiex_usb_rx_complete(struct urb *urb)
  146. {
  147. struct urb_context *context = (struct urb_context *)urb->context;
  148. struct mwifiex_adapter *adapter = context->adapter;
  149. struct sk_buff *skb = context->skb;
  150. struct usb_card_rec *card;
  151. int recv_length = urb->actual_length;
  152. int size, status;
  153. if (!adapter || !adapter->card) {
  154. pr_err("mwifiex adapter or card structure is not valid\n");
  155. return;
  156. }
  157. card = (struct usb_card_rec *)adapter->card;
  158. if (card->rx_cmd_ep == context->ep)
  159. atomic_dec(&card->rx_cmd_urb_pending);
  160. else
  161. atomic_dec(&card->rx_data_urb_pending);
  162. if (recv_length) {
  163. if (urb->status || (adapter->surprise_removed)) {
  164. mwifiex_dbg(adapter, ERROR,
  165. "URB status is failed: %d\n", urb->status);
  166. /* Do not free skb in case of command ep */
  167. if (card->rx_cmd_ep != context->ep)
  168. dev_kfree_skb_any(skb);
  169. goto setup_for_next;
  170. }
  171. if (skb->len > recv_length)
  172. skb_trim(skb, recv_length);
  173. else
  174. skb_put(skb, recv_length - skb->len);
  175. status = mwifiex_usb_recv(adapter, skb, context->ep);
  176. mwifiex_dbg(adapter, INFO,
  177. "info: recv_length=%d, status=%d\n",
  178. recv_length, status);
  179. if (status == -EINPROGRESS) {
  180. mwifiex_queue_main_work(adapter);
  181. /* urb for data_ep is re-submitted now;
  182. * urb for cmd_ep will be re-submitted in callback
  183. * mwifiex_usb_recv_complete
  184. */
  185. if (card->rx_cmd_ep == context->ep)
  186. return;
  187. } else {
  188. if (status == -1)
  189. mwifiex_dbg(adapter, ERROR,
  190. "received data processing failed!\n");
  191. /* Do not free skb in case of command ep */
  192. if (card->rx_cmd_ep != context->ep)
  193. dev_kfree_skb_any(skb);
  194. }
  195. } else if (urb->status) {
  196. if (!adapter->is_suspended) {
  197. mwifiex_dbg(adapter, FATAL,
  198. "Card is removed: %d\n", urb->status);
  199. adapter->surprise_removed = true;
  200. }
  201. dev_kfree_skb_any(skb);
  202. return;
  203. } else {
  204. /* Do not free skb in case of command ep */
  205. if (card->rx_cmd_ep != context->ep)
  206. dev_kfree_skb_any(skb);
  207. /* fall through setup_for_next */
  208. }
  209. setup_for_next:
  210. if (card->rx_cmd_ep == context->ep)
  211. size = MWIFIEX_RX_CMD_BUF_SIZE;
  212. else
  213. size = MWIFIEX_RX_DATA_BUF_SIZE;
  214. if (card->rx_cmd_ep == context->ep) {
  215. mwifiex_usb_submit_rx_urb(context, size);
  216. } else {
  217. context->skb = NULL;
  218. if (atomic_read(&adapter->rx_pending) <= HIGH_RX_PENDING)
  219. mwifiex_usb_submit_rx_urb(context, size);
  220. }
  221. return;
  222. }
  223. static void mwifiex_usb_tx_complete(struct urb *urb)
  224. {
  225. struct urb_context *context = (struct urb_context *)(urb->context);
  226. struct mwifiex_adapter *adapter = context->adapter;
  227. struct usb_card_rec *card = adapter->card;
  228. mwifiex_dbg(adapter, INFO,
  229. "%s: status: %d\n", __func__, urb->status);
  230. if (context->ep == card->tx_cmd_ep) {
  231. mwifiex_dbg(adapter, CMD,
  232. "%s: CMD\n", __func__);
  233. atomic_dec(&card->tx_cmd_urb_pending);
  234. adapter->cmd_sent = false;
  235. } else {
  236. mwifiex_dbg(adapter, DATA,
  237. "%s: DATA\n", __func__);
  238. atomic_dec(&card->tx_data_urb_pending);
  239. mwifiex_write_data_complete(adapter, context->skb, 0,
  240. urb->status ? -1 : 0);
  241. }
  242. mwifiex_queue_main_work(adapter);
  243. return;
  244. }
  245. static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size)
  246. {
  247. struct mwifiex_adapter *adapter = ctx->adapter;
  248. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  249. if (card->rx_cmd_ep != ctx->ep) {
  250. ctx->skb = dev_alloc_skb(size);
  251. if (!ctx->skb) {
  252. mwifiex_dbg(adapter, ERROR,
  253. "%s: dev_alloc_skb failed\n", __func__);
  254. return -ENOMEM;
  255. }
  256. }
  257. usb_fill_bulk_urb(ctx->urb, card->udev,
  258. usb_rcvbulkpipe(card->udev, ctx->ep), ctx->skb->data,
  259. size, mwifiex_usb_rx_complete, (void *)ctx);
  260. if (card->rx_cmd_ep == ctx->ep)
  261. atomic_inc(&card->rx_cmd_urb_pending);
  262. else
  263. atomic_inc(&card->rx_data_urb_pending);
  264. if (usb_submit_urb(ctx->urb, GFP_ATOMIC)) {
  265. mwifiex_dbg(adapter, ERROR, "usb_submit_urb failed\n");
  266. dev_kfree_skb_any(ctx->skb);
  267. ctx->skb = NULL;
  268. if (card->rx_cmd_ep == ctx->ep)
  269. atomic_dec(&card->rx_cmd_urb_pending);
  270. else
  271. atomic_dec(&card->rx_data_urb_pending);
  272. return -1;
  273. }
  274. return 0;
  275. }
  276. static void mwifiex_usb_free(struct usb_card_rec *card)
  277. {
  278. int i;
  279. if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
  280. usb_kill_urb(card->rx_cmd.urb);
  281. usb_free_urb(card->rx_cmd.urb);
  282. card->rx_cmd.urb = NULL;
  283. if (atomic_read(&card->rx_data_urb_pending))
  284. for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
  285. if (card->rx_data_list[i].urb)
  286. usb_kill_urb(card->rx_data_list[i].urb);
  287. for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
  288. usb_free_urb(card->rx_data_list[i].urb);
  289. card->rx_data_list[i].urb = NULL;
  290. }
  291. for (i = 0; i < MWIFIEX_TX_DATA_URB; i++) {
  292. usb_free_urb(card->tx_data_list[i].urb);
  293. card->tx_data_list[i].urb = NULL;
  294. }
  295. usb_free_urb(card->tx_cmd.urb);
  296. card->tx_cmd.urb = NULL;
  297. return;
  298. }
  299. /* This function probes an mwifiex device and registers it. It allocates
  300. * the card structure, initiates the device registration and initialization
  301. * procedure by adding a logical interface.
  302. */
  303. static int mwifiex_usb_probe(struct usb_interface *intf,
  304. const struct usb_device_id *id)
  305. {
  306. struct usb_device *udev = interface_to_usbdev(intf);
  307. struct usb_host_interface *iface_desc = intf->cur_altsetting;
  308. struct usb_endpoint_descriptor *epd;
  309. int ret, i;
  310. struct usb_card_rec *card;
  311. u16 id_vendor, id_product, bcd_device, bcd_usb;
  312. card = kzalloc(sizeof(struct usb_card_rec), GFP_KERNEL);
  313. if (!card)
  314. return -ENOMEM;
  315. id_vendor = le16_to_cpu(udev->descriptor.idVendor);
  316. id_product = le16_to_cpu(udev->descriptor.idProduct);
  317. bcd_device = le16_to_cpu(udev->descriptor.bcdDevice);
  318. bcd_usb = le16_to_cpu(udev->descriptor.bcdUSB);
  319. pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n",
  320. id_vendor, id_product, bcd_device);
  321. /* PID_1 is used for firmware downloading only */
  322. switch (id_product) {
  323. case USB8766_PID_1:
  324. case USB8797_PID_1:
  325. case USB8801_PID_1:
  326. case USB8897_PID_1:
  327. card->usb_boot_state = USB8XXX_FW_DNLD;
  328. break;
  329. case USB8766_PID_2:
  330. case USB8797_PID_2:
  331. case USB8801_PID_2:
  332. case USB8897_PID_2:
  333. card->usb_boot_state = USB8XXX_FW_READY;
  334. break;
  335. default:
  336. pr_warn("unknown id_product %#x\n", id_product);
  337. card->usb_boot_state = USB8XXX_FW_DNLD;
  338. break;
  339. }
  340. card->udev = udev;
  341. card->intf = intf;
  342. pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n",
  343. udev->descriptor.bcdUSB, udev->descriptor.bDeviceClass,
  344. udev->descriptor.bDeviceSubClass,
  345. udev->descriptor.bDeviceProtocol);
  346. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  347. epd = &iface_desc->endpoint[i].desc;
  348. if (usb_endpoint_dir_in(epd) &&
  349. usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
  350. usb_endpoint_xfer_bulk(epd)) {
  351. pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
  352. le16_to_cpu(epd->wMaxPacketSize),
  353. epd->bEndpointAddress);
  354. card->rx_cmd_ep = usb_endpoint_num(epd);
  355. atomic_set(&card->rx_cmd_urb_pending, 0);
  356. }
  357. if (usb_endpoint_dir_in(epd) &&
  358. usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
  359. usb_endpoint_xfer_bulk(epd)) {
  360. pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
  361. le16_to_cpu(epd->wMaxPacketSize),
  362. epd->bEndpointAddress);
  363. card->rx_data_ep = usb_endpoint_num(epd);
  364. atomic_set(&card->rx_data_urb_pending, 0);
  365. }
  366. if (usb_endpoint_dir_out(epd) &&
  367. usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
  368. usb_endpoint_xfer_bulk(epd)) {
  369. pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
  370. le16_to_cpu(epd->wMaxPacketSize),
  371. epd->bEndpointAddress);
  372. card->tx_data_ep = usb_endpoint_num(epd);
  373. atomic_set(&card->tx_data_urb_pending, 0);
  374. }
  375. if (usb_endpoint_dir_out(epd) &&
  376. usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
  377. usb_endpoint_xfer_bulk(epd)) {
  378. pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
  379. le16_to_cpu(epd->wMaxPacketSize),
  380. epd->bEndpointAddress);
  381. card->tx_cmd_ep = usb_endpoint_num(epd);
  382. atomic_set(&card->tx_cmd_urb_pending, 0);
  383. card->bulk_out_maxpktsize =
  384. le16_to_cpu(epd->wMaxPacketSize);
  385. }
  386. }
  387. usb_set_intfdata(intf, card);
  388. ret = mwifiex_add_card(card, &add_remove_card_sem, &usb_ops,
  389. MWIFIEX_USB);
  390. if (ret) {
  391. pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret);
  392. usb_reset_device(udev);
  393. kfree(card);
  394. return ret;
  395. }
  396. usb_get_dev(udev);
  397. return 0;
  398. }
  399. /* Kernel needs to suspend all functions separately. Therefore all
  400. * registered functions must have drivers with suspend and resume
  401. * methods. Failing that the kernel simply removes the whole card.
  402. *
  403. * If already not suspended, this function allocates and sends a
  404. * 'host sleep activate' request to the firmware and turns off the traffic.
  405. */
  406. static int mwifiex_usb_suspend(struct usb_interface *intf, pm_message_t message)
  407. {
  408. struct usb_card_rec *card = usb_get_intfdata(intf);
  409. struct mwifiex_adapter *adapter;
  410. int i;
  411. if (!card || !card->adapter) {
  412. pr_err("%s: card or card->adapter is NULL\n", __func__);
  413. return 0;
  414. }
  415. adapter = card->adapter;
  416. if (unlikely(adapter->is_suspended))
  417. mwifiex_dbg(adapter, WARN,
  418. "Device already suspended\n");
  419. mwifiex_enable_hs(adapter);
  420. /* 'is_suspended' flag indicates device is suspended.
  421. * It must be set here before the usb_kill_urb() calls. Reason
  422. * is in the complete handlers, urb->status(= -ENOENT) and
  423. * this flag is used in combination to distinguish between a
  424. * 'suspended' state and a 'disconnect' one.
  425. */
  426. adapter->is_suspended = true;
  427. adapter->hs_enabling = false;
  428. if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
  429. usb_kill_urb(card->rx_cmd.urb);
  430. if (atomic_read(&card->rx_data_urb_pending))
  431. for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
  432. if (card->rx_data_list[i].urb)
  433. usb_kill_urb(card->rx_data_list[i].urb);
  434. for (i = 0; i < MWIFIEX_TX_DATA_URB; i++)
  435. if (card->tx_data_list[i].urb)
  436. usb_kill_urb(card->tx_data_list[i].urb);
  437. if (card->tx_cmd.urb)
  438. usb_kill_urb(card->tx_cmd.urb);
  439. return 0;
  440. }
  441. /* Kernel needs to suspend all functions separately. Therefore all
  442. * registered functions must have drivers with suspend and resume
  443. * methods. Failing that the kernel simply removes the whole card.
  444. *
  445. * If already not resumed, this function turns on the traffic and
  446. * sends a 'host sleep cancel' request to the firmware.
  447. */
  448. static int mwifiex_usb_resume(struct usb_interface *intf)
  449. {
  450. struct usb_card_rec *card = usb_get_intfdata(intf);
  451. struct mwifiex_adapter *adapter;
  452. int i;
  453. if (!card || !card->adapter) {
  454. pr_err("%s: card or card->adapter is NULL\n", __func__);
  455. return 0;
  456. }
  457. adapter = card->adapter;
  458. if (unlikely(!adapter->is_suspended)) {
  459. mwifiex_dbg(adapter, WARN,
  460. "Device already resumed\n");
  461. return 0;
  462. }
  463. /* Indicate device resumed. The netdev queue will be resumed only
  464. * after the urbs have been re-submitted
  465. */
  466. adapter->is_suspended = false;
  467. if (!atomic_read(&card->rx_data_urb_pending))
  468. for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
  469. mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
  470. MWIFIEX_RX_DATA_BUF_SIZE);
  471. if (!atomic_read(&card->rx_cmd_urb_pending)) {
  472. card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
  473. if (card->rx_cmd.skb)
  474. mwifiex_usb_submit_rx_urb(&card->rx_cmd,
  475. MWIFIEX_RX_CMD_BUF_SIZE);
  476. }
  477. /* Disable Host Sleep */
  478. if (adapter->hs_activated)
  479. mwifiex_cancel_hs(mwifiex_get_priv(adapter,
  480. MWIFIEX_BSS_ROLE_ANY),
  481. MWIFIEX_ASYNC_CMD);
  482. return 0;
  483. }
  484. static void mwifiex_usb_disconnect(struct usb_interface *intf)
  485. {
  486. struct usb_card_rec *card = usb_get_intfdata(intf);
  487. struct mwifiex_adapter *adapter;
  488. if (!card || !card->adapter) {
  489. pr_err("%s: card or card->adapter is NULL\n", __func__);
  490. return;
  491. }
  492. adapter = card->adapter;
  493. if (!adapter->priv_num)
  494. return;
  495. if (user_rmmod) {
  496. #ifdef CONFIG_PM
  497. if (adapter->is_suspended)
  498. mwifiex_usb_resume(intf);
  499. #endif
  500. mwifiex_deauthenticate_all(adapter);
  501. mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
  502. MWIFIEX_BSS_ROLE_ANY),
  503. MWIFIEX_FUNC_SHUTDOWN);
  504. }
  505. mwifiex_usb_free(card);
  506. mwifiex_dbg(adapter, FATAL,
  507. "%s: removing card\n", __func__);
  508. mwifiex_remove_card(adapter, &add_remove_card_sem);
  509. usb_set_intfdata(intf, NULL);
  510. usb_put_dev(interface_to_usbdev(intf));
  511. kfree(card);
  512. return;
  513. }
  514. static struct usb_driver mwifiex_usb_driver = {
  515. .name = "mwifiex_usb",
  516. .probe = mwifiex_usb_probe,
  517. .disconnect = mwifiex_usb_disconnect,
  518. .id_table = mwifiex_usb_table,
  519. .suspend = mwifiex_usb_suspend,
  520. .resume = mwifiex_usb_resume,
  521. .soft_unbind = 1,
  522. };
  523. static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter)
  524. {
  525. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  526. int i;
  527. card->tx_cmd.adapter = adapter;
  528. card->tx_cmd.ep = card->tx_cmd_ep;
  529. card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
  530. if (!card->tx_cmd.urb) {
  531. mwifiex_dbg(adapter, ERROR,
  532. "tx_cmd.urb allocation failed\n");
  533. return -ENOMEM;
  534. }
  535. card->tx_data_ix = 0;
  536. for (i = 0; i < MWIFIEX_TX_DATA_URB; i++) {
  537. card->tx_data_list[i].adapter = adapter;
  538. card->tx_data_list[i].ep = card->tx_data_ep;
  539. card->tx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
  540. if (!card->tx_data_list[i].urb) {
  541. mwifiex_dbg(adapter, ERROR,
  542. "tx_data_list[] urb allocation failed\n");
  543. return -ENOMEM;
  544. }
  545. }
  546. return 0;
  547. }
  548. static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter)
  549. {
  550. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  551. int i;
  552. card->rx_cmd.adapter = adapter;
  553. card->rx_cmd.ep = card->rx_cmd_ep;
  554. card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
  555. if (!card->rx_cmd.urb) {
  556. mwifiex_dbg(adapter, ERROR, "rx_cmd.urb allocation failed\n");
  557. return -ENOMEM;
  558. }
  559. card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
  560. if (!card->rx_cmd.skb)
  561. return -ENOMEM;
  562. if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE))
  563. return -1;
  564. for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
  565. card->rx_data_list[i].adapter = adapter;
  566. card->rx_data_list[i].ep = card->rx_data_ep;
  567. card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
  568. if (!card->rx_data_list[i].urb) {
  569. mwifiex_dbg(adapter, ERROR,
  570. "rx_data_list[] urb allocation failed\n");
  571. return -1;
  572. }
  573. if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
  574. MWIFIEX_RX_DATA_BUF_SIZE))
  575. return -1;
  576. }
  577. return 0;
  578. }
  579. static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
  580. u32 *len, u8 ep, u32 timeout)
  581. {
  582. struct usb_card_rec *card = adapter->card;
  583. int actual_length, ret;
  584. if (!(*len % card->bulk_out_maxpktsize))
  585. (*len)++;
  586. /* Send the data block */
  587. ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf,
  588. *len, &actual_length, timeout);
  589. if (ret) {
  590. mwifiex_dbg(adapter, ERROR,
  591. "usb_bulk_msg for tx failed: %d\n", ret);
  592. return ret;
  593. }
  594. *len = actual_length;
  595. return ret;
  596. }
  597. static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
  598. u32 *len, u8 ep, u32 timeout)
  599. {
  600. struct usb_card_rec *card = adapter->card;
  601. int actual_length, ret;
  602. /* Receive the data response */
  603. ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf,
  604. *len, &actual_length, timeout);
  605. if (ret) {
  606. mwifiex_dbg(adapter, ERROR,
  607. "usb_bulk_msg for rx failed: %d\n", ret);
  608. return ret;
  609. }
  610. *len = actual_length;
  611. return ret;
  612. }
  613. /* This function write a command/data packet to card. */
  614. static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep,
  615. struct sk_buff *skb,
  616. struct mwifiex_tx_param *tx_param)
  617. {
  618. struct usb_card_rec *card = adapter->card;
  619. struct urb_context *context;
  620. u8 *data = (u8 *)skb->data;
  621. struct urb *tx_urb;
  622. if (adapter->is_suspended) {
  623. mwifiex_dbg(adapter, ERROR,
  624. "%s: not allowed while suspended\n", __func__);
  625. return -1;
  626. }
  627. if (adapter->surprise_removed) {
  628. mwifiex_dbg(adapter, ERROR, "%s: device removed\n", __func__);
  629. return -1;
  630. }
  631. if (ep == card->tx_data_ep &&
  632. atomic_read(&card->tx_data_urb_pending) >= MWIFIEX_TX_DATA_URB) {
  633. return -EBUSY;
  634. }
  635. mwifiex_dbg(adapter, INFO, "%s: ep=%d\n", __func__, ep);
  636. if (ep == card->tx_cmd_ep) {
  637. context = &card->tx_cmd;
  638. } else {
  639. if (card->tx_data_ix >= MWIFIEX_TX_DATA_URB)
  640. card->tx_data_ix = 0;
  641. context = &card->tx_data_list[card->tx_data_ix++];
  642. }
  643. context->adapter = adapter;
  644. context->ep = ep;
  645. context->skb = skb;
  646. tx_urb = context->urb;
  647. usb_fill_bulk_urb(tx_urb, card->udev, usb_sndbulkpipe(card->udev, ep),
  648. data, skb->len, mwifiex_usb_tx_complete,
  649. (void *)context);
  650. tx_urb->transfer_flags |= URB_ZERO_PACKET;
  651. if (ep == card->tx_cmd_ep)
  652. atomic_inc(&card->tx_cmd_urb_pending);
  653. else
  654. atomic_inc(&card->tx_data_urb_pending);
  655. if (usb_submit_urb(tx_urb, GFP_ATOMIC)) {
  656. mwifiex_dbg(adapter, ERROR,
  657. "%s: usb_submit_urb failed\n", __func__);
  658. if (ep == card->tx_cmd_ep) {
  659. atomic_dec(&card->tx_cmd_urb_pending);
  660. } else {
  661. atomic_dec(&card->tx_data_urb_pending);
  662. if (card->tx_data_ix)
  663. card->tx_data_ix--;
  664. else
  665. card->tx_data_ix = MWIFIEX_TX_DATA_URB;
  666. }
  667. return -1;
  668. } else {
  669. if (ep == card->tx_data_ep &&
  670. atomic_read(&card->tx_data_urb_pending) ==
  671. MWIFIEX_TX_DATA_URB)
  672. return -ENOSR;
  673. }
  674. return -EINPROGRESS;
  675. }
  676. /* This function register usb device and initialize parameter. */
  677. static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
  678. {
  679. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  680. card->adapter = adapter;
  681. adapter->dev = &card->udev->dev;
  682. switch (le16_to_cpu(card->udev->descriptor.idProduct)) {
  683. case USB8897_PID_1:
  684. case USB8897_PID_2:
  685. adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K;
  686. strcpy(adapter->fw_name, USB8897_DEFAULT_FW_NAME);
  687. adapter->ext_scan = true;
  688. break;
  689. case USB8766_PID_1:
  690. case USB8766_PID_2:
  691. adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
  692. strcpy(adapter->fw_name, USB8766_DEFAULT_FW_NAME);
  693. adapter->ext_scan = true;
  694. break;
  695. case USB8801_PID_1:
  696. case USB8801_PID_2:
  697. adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
  698. strcpy(adapter->fw_name, USB8801_DEFAULT_FW_NAME);
  699. adapter->ext_scan = false;
  700. break;
  701. case USB8797_PID_1:
  702. case USB8797_PID_2:
  703. default:
  704. adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
  705. strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME);
  706. break;
  707. }
  708. return 0;
  709. }
  710. static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
  711. {
  712. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  713. card->adapter = NULL;
  714. }
  715. static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
  716. struct mwifiex_fw_image *fw)
  717. {
  718. int ret = 0;
  719. u8 *firmware = fw->fw_buf, *recv_buff;
  720. u32 retries = USB8XXX_FW_MAX_RETRY, dlen;
  721. u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0;
  722. struct fw_data *fwdata;
  723. struct fw_sync_header sync_fw;
  724. u8 check_winner = 1;
  725. if (!firmware) {
  726. mwifiex_dbg(adapter, ERROR,
  727. "No firmware image found! Terminating download\n");
  728. ret = -1;
  729. goto fw_exit;
  730. }
  731. /* Allocate memory for transmit */
  732. fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL);
  733. if (!fwdata)
  734. goto fw_exit;
  735. /* Allocate memory for receive */
  736. recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL);
  737. if (!recv_buff)
  738. goto cleanup;
  739. do {
  740. /* Send pseudo data to check winner status first */
  741. if (check_winner) {
  742. memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header));
  743. dlen = 0;
  744. } else {
  745. /* copy the header of the fw_data to get the length */
  746. memcpy(&fwdata->fw_hdr, &firmware[tlen],
  747. sizeof(struct fw_header));
  748. dlen = le32_to_cpu(fwdata->fw_hdr.data_len);
  749. dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd);
  750. tlen += sizeof(struct fw_header);
  751. memcpy(fwdata->data, &firmware[tlen], dlen);
  752. fwdata->seq_num = cpu_to_le32(fw_seqnum);
  753. tlen += dlen;
  754. }
  755. /* If the send/receive fails or CRC occurs then retry */
  756. while (retries--) {
  757. u8 *buf = (u8 *)fwdata;
  758. u32 len = FW_DATA_XMIT_SIZE;
  759. /* send the firmware block */
  760. ret = mwifiex_write_data_sync(adapter, buf, &len,
  761. MWIFIEX_USB_EP_CMD_EVENT,
  762. MWIFIEX_USB_TIMEOUT);
  763. if (ret) {
  764. mwifiex_dbg(adapter, ERROR,
  765. "write_data_sync: failed: %d\n",
  766. ret);
  767. continue;
  768. }
  769. buf = recv_buff;
  770. len = FW_DNLD_RX_BUF_SIZE;
  771. /* Receive the firmware block response */
  772. ret = mwifiex_read_data_sync(adapter, buf, &len,
  773. MWIFIEX_USB_EP_CMD_EVENT,
  774. MWIFIEX_USB_TIMEOUT);
  775. if (ret) {
  776. mwifiex_dbg(adapter, ERROR,
  777. "read_data_sync: failed: %d\n",
  778. ret);
  779. continue;
  780. }
  781. memcpy(&sync_fw, recv_buff,
  782. sizeof(struct fw_sync_header));
  783. /* check 1st firmware block resp for highest bit set */
  784. if (check_winner) {
  785. if (le32_to_cpu(sync_fw.cmd) & 0x80000000) {
  786. mwifiex_dbg(adapter, WARN,
  787. "USB is not the winner %#x\n",
  788. sync_fw.cmd);
  789. /* returning success */
  790. ret = 0;
  791. goto cleanup;
  792. }
  793. mwifiex_dbg(adapter, MSG,
  794. "start to download FW...\n");
  795. check_winner = 0;
  796. break;
  797. }
  798. /* check the firmware block response for CRC errors */
  799. if (sync_fw.cmd) {
  800. mwifiex_dbg(adapter, ERROR,
  801. "FW received block with CRC %#x\n",
  802. sync_fw.cmd);
  803. ret = -1;
  804. continue;
  805. }
  806. retries = USB8XXX_FW_MAX_RETRY;
  807. break;
  808. }
  809. fw_seqnum++;
  810. } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries);
  811. cleanup:
  812. mwifiex_dbg(adapter, MSG,
  813. "info: FW download over, size %d bytes\n", tlen);
  814. kfree(recv_buff);
  815. kfree(fwdata);
  816. if (retries)
  817. ret = 0;
  818. fw_exit:
  819. return ret;
  820. }
  821. static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter,
  822. struct mwifiex_fw_image *fw)
  823. {
  824. int ret;
  825. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  826. if (card->usb_boot_state == USB8XXX_FW_DNLD) {
  827. ret = mwifiex_prog_fw_w_helper(adapter, fw);
  828. if (ret)
  829. return -1;
  830. /* Boot state changes after successful firmware download */
  831. if (card->usb_boot_state == USB8XXX_FW_DNLD)
  832. return -1;
  833. }
  834. ret = mwifiex_usb_rx_init(adapter);
  835. if (!ret)
  836. ret = mwifiex_usb_tx_init(adapter);
  837. return ret;
  838. }
  839. static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep)
  840. {
  841. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  842. skb_push(card->rx_cmd.skb, INTF_HEADER_LEN);
  843. if ((ep == card->rx_cmd_ep) &&
  844. (!atomic_read(&card->rx_cmd_urb_pending)))
  845. mwifiex_usb_submit_rx_urb(&card->rx_cmd,
  846. MWIFIEX_RX_CMD_BUF_SIZE);
  847. return;
  848. }
  849. static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter,
  850. struct sk_buff *skb)
  851. {
  852. mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT);
  853. return 0;
  854. }
  855. /* This function wakes up the card. */
  856. static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
  857. {
  858. /* Simulation of HS_AWAKE event */
  859. adapter->pm_wakeup_fw_try = false;
  860. del_timer(&adapter->wakeup_timer);
  861. adapter->pm_wakeup_card_req = false;
  862. adapter->ps_state = PS_STATE_AWAKE;
  863. return 0;
  864. }
  865. static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter *adapter)
  866. {
  867. struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
  868. int i;
  869. struct urb_context *ctx;
  870. for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
  871. if (card->rx_data_list[i].skb)
  872. continue;
  873. ctx = &card->rx_data_list[i];
  874. mwifiex_usb_submit_rx_urb(ctx, MWIFIEX_RX_DATA_BUF_SIZE);
  875. }
  876. }
  877. /* This function is called after the card has woken up. */
  878. static inline int
  879. mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
  880. {
  881. return 0;
  882. }
  883. static struct mwifiex_if_ops usb_ops = {
  884. .register_dev = mwifiex_register_dev,
  885. .unregister_dev = mwifiex_unregister_dev,
  886. .wakeup = mwifiex_pm_wakeup_card,
  887. .wakeup_complete = mwifiex_pm_wakeup_card_complete,
  888. /* USB specific */
  889. .dnld_fw = mwifiex_usb_dnld_fw,
  890. .cmdrsp_complete = mwifiex_usb_cmd_event_complete,
  891. .event_complete = mwifiex_usb_cmd_event_complete,
  892. .host_to_card = mwifiex_usb_host_to_card,
  893. .submit_rem_rx_urbs = mwifiex_usb_submit_rem_rx_urbs,
  894. };
  895. /* This function initializes the USB driver module.
  896. *
  897. * This initiates the semaphore and registers the device with
  898. * USB bus.
  899. */
  900. static int mwifiex_usb_init_module(void)
  901. {
  902. int ret;
  903. pr_debug("Marvell USB8797 Driver\n");
  904. sema_init(&add_remove_card_sem, 1);
  905. ret = usb_register(&mwifiex_usb_driver);
  906. if (ret)
  907. pr_err("Driver register failed!\n");
  908. else
  909. pr_debug("info: Driver registered successfully!\n");
  910. return ret;
  911. }
  912. /* This function cleans up the USB driver.
  913. *
  914. * The following major steps are followed in .disconnect for cleanup:
  915. * - Resume the device if its suspended
  916. * - Disconnect the device if connected
  917. * - Shutdown the firmware
  918. * - Unregister the device from USB bus.
  919. */
  920. static void mwifiex_usb_cleanup_module(void)
  921. {
  922. if (!down_interruptible(&add_remove_card_sem))
  923. up(&add_remove_card_sem);
  924. /* set the flag as user is removing this module */
  925. user_rmmod = 1;
  926. usb_deregister(&mwifiex_usb_driver);
  927. }
  928. module_init(mwifiex_usb_init_module);
  929. module_exit(mwifiex_usb_cleanup_module);
  930. MODULE_AUTHOR("Marvell International Ltd.");
  931. MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION);
  932. MODULE_VERSION(USB_VERSION);
  933. MODULE_LICENSE("GPL v2");
  934. /*(DEBLOBBED)*/