if_usb.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. /**
  2. * This file contains functions used in USB interface module.
  3. */
  4. #include <linux/delay.h>
  5. #include <linux/moduleparam.h>
  6. #include <linux/firmware.h>
  7. #include <linux/netdevice.h>
  8. #include <linux/usb.h>
  9. #define DRV_NAME "usb8xxx"
  10. #include "host.h"
  11. #include "decl.h"
  12. #include "defs.h"
  13. #include "dev.h"
  14. #include "cmd.h"
  15. #include "if_usb.h"
  16. #define INSANEDEBUG 0
  17. #define lbs_deb_usb2(...) do { if (INSANEDEBUG) lbs_deb_usbd(__VA_ARGS__); } while (0)
  18. #define MESSAGE_HEADER_LEN 4
  19. static char *lbs_fw_name = "usb8388.bin";
  20. module_param_named(fw_name, lbs_fw_name, charp, 0644);
  21. static struct usb_device_id if_usb_table[] = {
  22. /* Enter the device signature inside */
  23. { USB_DEVICE(0x1286, 0x2001) },
  24. { USB_DEVICE(0x05a3, 0x8388) },
  25. {} /* Terminating entry */
  26. };
  27. MODULE_DEVICE_TABLE(usb, if_usb_table);
  28. static void if_usb_receive(struct urb *urb);
  29. static void if_usb_receive_fwload(struct urb *urb);
  30. static int if_usb_prog_firmware(struct if_usb_card *cardp);
  31. static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
  32. uint8_t *payload, uint16_t nb);
  33. static int if_usb_get_int_status(struct lbs_private *priv, uint8_t *);
  34. static int if_usb_read_event_cause(struct lbs_private *);
  35. static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
  36. uint16_t nb);
  37. static void if_usb_free(struct if_usb_card *cardp);
  38. static int if_usb_submit_rx_urb(struct if_usb_card *cardp);
  39. static int if_usb_reset_device(struct if_usb_card *cardp);
  40. /**
  41. * @brief call back function to handle the status of the URB
  42. * @param urb pointer to urb structure
  43. * @return N/A
  44. */
  45. static void if_usb_write_bulk_callback(struct urb *urb)
  46. {
  47. struct if_usb_card *cardp = (struct if_usb_card *) urb->context;
  48. /* handle the transmission complete validations */
  49. if (urb->status == 0) {
  50. struct lbs_private *priv = cardp->priv;
  51. lbs_deb_usb2(&urb->dev->dev, "URB status is successful\n");
  52. lbs_deb_usb2(&urb->dev->dev, "Actual length transmitted %d\n",
  53. urb->actual_length);
  54. /* Used for both firmware TX and regular TX. priv isn't
  55. * valid at firmware load time.
  56. */
  57. if (priv)
  58. lbs_host_to_card_done(priv);
  59. } else {
  60. /* print the failure status number for debug */
  61. lbs_pr_info("URB in failure status: %d\n", urb->status);
  62. }
  63. return;
  64. }
  65. /**
  66. * @brief free tx/rx urb, skb and rx buffer
  67. * @param cardp pointer if_usb_card
  68. * @return N/A
  69. */
  70. static void if_usb_free(struct if_usb_card *cardp)
  71. {
  72. lbs_deb_enter(LBS_DEB_USB);
  73. /* Unlink tx & rx urb */
  74. usb_kill_urb(cardp->tx_urb);
  75. usb_kill_urb(cardp->rx_urb);
  76. usb_free_urb(cardp->tx_urb);
  77. cardp->tx_urb = NULL;
  78. usb_free_urb(cardp->rx_urb);
  79. cardp->rx_urb = NULL;
  80. kfree(cardp->ep_out_buf);
  81. cardp->ep_out_buf = NULL;
  82. lbs_deb_leave(LBS_DEB_USB);
  83. }
  84. static void if_usb_setup_firmware(struct lbs_private *priv)
  85. {
  86. struct if_usb_card *cardp = priv->card;
  87. struct cmd_ds_set_boot2_ver b2_cmd;
  88. struct cmd_ds_802_11_fw_wake_method wake_method;
  89. b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd));
  90. b2_cmd.action = 0;
  91. b2_cmd.version = cardp->boot2_version;
  92. if (lbs_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd))
  93. lbs_deb_usb("Setting boot2 version failed\n");
  94. priv->wol_gpio = 2; /* Wake via GPIO2... */
  95. priv->wol_gap = 20; /* ... after 20ms */
  96. lbs_host_sleep_cfg(priv, EHS_WAKE_ON_UNICAST_DATA);
  97. wake_method.hdr.size = cpu_to_le16(sizeof(wake_method));
  98. wake_method.action = cpu_to_le16(CMD_ACT_GET);
  99. if (lbs_cmd_with_response(priv, CMD_802_11_FW_WAKE_METHOD, &wake_method)) {
  100. lbs_pr_info("Firmware does not seem to support PS mode\n");
  101. } else {
  102. if (le16_to_cpu(wake_method.method) == CMD_WAKE_METHOD_COMMAND_INT) {
  103. lbs_deb_usb("Firmware seems to support PS with wake-via-command\n");
  104. priv->ps_supported = 1;
  105. } else {
  106. /* The versions which boot up this way don't seem to
  107. work even if we set it to the command interrupt */
  108. lbs_pr_info("Firmware doesn't wake via command interrupt; disabling PS mode\n");
  109. }
  110. }
  111. }
  112. static void if_usb_fw_timeo(unsigned long priv)
  113. {
  114. struct if_usb_card *cardp = (void *)priv;
  115. if (cardp->fwdnldover) {
  116. lbs_deb_usb("Download complete, no event. Assuming success\n");
  117. } else {
  118. lbs_pr_err("Download timed out\n");
  119. cardp->surprise_removed = 1;
  120. }
  121. wake_up(&cardp->fw_wq);
  122. }
  123. /**
  124. * @brief sets the configuration values
  125. * @param ifnum interface number
  126. * @param id pointer to usb_device_id
  127. * @return 0 on success, error code on failure
  128. */
  129. static int if_usb_probe(struct usb_interface *intf,
  130. const struct usb_device_id *id)
  131. {
  132. struct usb_device *udev;
  133. struct usb_host_interface *iface_desc;
  134. struct usb_endpoint_descriptor *endpoint;
  135. struct lbs_private *priv;
  136. struct if_usb_card *cardp;
  137. int i;
  138. udev = interface_to_usbdev(intf);
  139. cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL);
  140. if (!cardp) {
  141. lbs_pr_err("Out of memory allocating private data.\n");
  142. goto error;
  143. }
  144. setup_timer(&cardp->fw_timeout, if_usb_fw_timeo, (unsigned long)cardp);
  145. init_waitqueue_head(&cardp->fw_wq);
  146. cardp->udev = udev;
  147. iface_desc = intf->cur_altsetting;
  148. lbs_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
  149. " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
  150. le16_to_cpu(udev->descriptor.bcdUSB),
  151. udev->descriptor.bDeviceClass,
  152. udev->descriptor.bDeviceSubClass,
  153. udev->descriptor.bDeviceProtocol);
  154. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  155. endpoint = &iface_desc->endpoint[i].desc;
  156. if (usb_endpoint_is_bulk_in(endpoint)) {
  157. cardp->ep_in_size = le16_to_cpu(endpoint->wMaxPacketSize);
  158. cardp->ep_in = usb_endpoint_num(endpoint);
  159. lbs_deb_usbd(&udev->dev, "in_endpoint = %d\n", cardp->ep_in);
  160. lbs_deb_usbd(&udev->dev, "Bulk in size is %d\n", cardp->ep_in_size);
  161. } else if (usb_endpoint_is_bulk_out(endpoint)) {
  162. cardp->ep_out_size = le16_to_cpu(endpoint->wMaxPacketSize);
  163. cardp->ep_out = usb_endpoint_num(endpoint);
  164. lbs_deb_usbd(&udev->dev, "out_endpoint = %d\n", cardp->ep_out);
  165. lbs_deb_usbd(&udev->dev, "Bulk out size is %d\n", cardp->ep_out_size);
  166. }
  167. }
  168. if (!cardp->ep_out_size || !cardp->ep_in_size) {
  169. lbs_deb_usbd(&udev->dev, "Endpoints not found\n");
  170. goto dealloc;
  171. }
  172. if (!(cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
  173. lbs_deb_usbd(&udev->dev, "Rx URB allocation failed\n");
  174. goto dealloc;
  175. }
  176. if (!(cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL))) {
  177. lbs_deb_usbd(&udev->dev, "Tx URB allocation failed\n");
  178. goto dealloc;
  179. }
  180. cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE, GFP_KERNEL);
  181. if (!cardp->ep_out_buf) {
  182. lbs_deb_usbd(&udev->dev, "Could not allocate buffer\n");
  183. goto dealloc;
  184. }
  185. /* Upload firmware */
  186. if (if_usb_prog_firmware(cardp)) {
  187. lbs_deb_usbd(&udev->dev, "FW upload failed\n");
  188. goto err_prog_firmware;
  189. }
  190. if (!(priv = lbs_add_card(cardp, &udev->dev)))
  191. goto err_prog_firmware;
  192. cardp->priv = priv;
  193. cardp->priv->fw_ready = 1;
  194. priv->hw_host_to_card = if_usb_host_to_card;
  195. priv->hw_get_int_status = if_usb_get_int_status;
  196. priv->hw_read_event_cause = if_usb_read_event_cause;
  197. cardp->boot2_version = udev->descriptor.bcdDevice;
  198. if_usb_submit_rx_urb(cardp);
  199. if (lbs_start_card(priv))
  200. goto err_start_card;
  201. if_usb_setup_firmware(priv);
  202. usb_get_dev(udev);
  203. usb_set_intfdata(intf, cardp);
  204. return 0;
  205. err_start_card:
  206. lbs_remove_card(priv);
  207. err_prog_firmware:
  208. if_usb_reset_device(cardp);
  209. dealloc:
  210. if_usb_free(cardp);
  211. error:
  212. return -ENOMEM;
  213. }
  214. /**
  215. * @brief free resource and cleanup
  216. * @param intf USB interface structure
  217. * @return N/A
  218. */
  219. static void if_usb_disconnect(struct usb_interface *intf)
  220. {
  221. struct if_usb_card *cardp = usb_get_intfdata(intf);
  222. struct lbs_private *priv = (struct lbs_private *) cardp->priv;
  223. lbs_deb_enter(LBS_DEB_MAIN);
  224. cardp->surprise_removed = 1;
  225. if (priv) {
  226. priv->surpriseremoved = 1;
  227. lbs_stop_card(priv);
  228. lbs_remove_card(priv);
  229. }
  230. /* Unlink and free urb */
  231. if_usb_free(cardp);
  232. usb_set_intfdata(intf, NULL);
  233. usb_put_dev(interface_to_usbdev(intf));
  234. lbs_deb_leave(LBS_DEB_MAIN);
  235. }
  236. /**
  237. * @brief This function download FW
  238. * @param priv pointer to struct lbs_private
  239. * @return 0
  240. */
  241. static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
  242. {
  243. struct fwdata *fwdata = cardp->ep_out_buf;
  244. uint8_t *firmware = cardp->fw->data;
  245. /* If we got a CRC failure on the last block, back
  246. up and retry it */
  247. if (!cardp->CRC_OK) {
  248. cardp->totalbytes = cardp->fwlastblksent;
  249. cardp->fwseqnum--;
  250. }
  251. lbs_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n",
  252. cardp->totalbytes);
  253. /* struct fwdata (which we sent to the card) has an
  254. extra __le32 field in between the header and the data,
  255. which is not in the struct fwheader in the actual
  256. firmware binary. Insert the seqnum in the middle... */
  257. memcpy(&fwdata->hdr, &firmware[cardp->totalbytes],
  258. sizeof(struct fwheader));
  259. cardp->fwlastblksent = cardp->totalbytes;
  260. cardp->totalbytes += sizeof(struct fwheader);
  261. memcpy(fwdata->data, &firmware[cardp->totalbytes],
  262. le32_to_cpu(fwdata->hdr.datalength));
  263. lbs_deb_usb2(&cardp->udev->dev, "Data length = %d\n",
  264. le32_to_cpu(fwdata->hdr.datalength));
  265. fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum);
  266. cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength);
  267. usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) +
  268. le32_to_cpu(fwdata->hdr.datalength));
  269. if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
  270. lbs_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
  271. lbs_deb_usb2(&cardp->udev->dev, "seqnum = %d totalbytes = %d\n",
  272. cardp->fwseqnum, cardp->totalbytes);
  273. } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
  274. lbs_deb_usb2(&cardp->udev->dev, "Host has finished FW downloading\n");
  275. lbs_deb_usb2(&cardp->udev->dev, "Donwloading FW JUMP BLOCK\n");
  276. cardp->fwfinalblk = 1;
  277. }
  278. lbs_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
  279. cardp->totalbytes);
  280. return 0;
  281. }
  282. static int if_usb_reset_device(struct if_usb_card *cardp)
  283. {
  284. struct cmd_ds_command *cmd = cardp->ep_out_buf + 4;
  285. int ret;
  286. lbs_deb_enter(LBS_DEB_USB);
  287. *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
  288. cmd->command = cpu_to_le16(CMD_802_11_RESET);
  289. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset) + S_DS_GEN);
  290. cmd->result = cpu_to_le16(0);
  291. cmd->seqnum = cpu_to_le16(0x5a5a);
  292. cmd->params.reset.action = cpu_to_le16(CMD_ACT_HALT);
  293. usb_tx_block(cardp, cardp->ep_out_buf, 4 + S_DS_GEN + sizeof(struct cmd_ds_802_11_reset));
  294. msleep(100);
  295. ret = usb_reset_device(cardp->udev);
  296. msleep(100);
  297. lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
  298. return ret;
  299. }
  300. /**
  301. * @brief This function transfer the data to the device.
  302. * @param priv pointer to struct lbs_private
  303. * @param payload pointer to payload data
  304. * @param nb data length
  305. * @return 0 or -1
  306. */
  307. static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload, uint16_t nb)
  308. {
  309. int ret = -1;
  310. /* check if device is removed */
  311. if (cardp->surprise_removed) {
  312. lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
  313. goto tx_ret;
  314. }
  315. usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
  316. usb_sndbulkpipe(cardp->udev,
  317. cardp->ep_out),
  318. payload, nb, if_usb_write_bulk_callback, cardp);
  319. cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
  320. if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
  321. lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
  322. ret = -1;
  323. } else {
  324. lbs_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
  325. ret = 0;
  326. }
  327. tx_ret:
  328. return ret;
  329. }
  330. static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
  331. void (*callbackfn)(struct urb *urb))
  332. {
  333. struct sk_buff *skb;
  334. int ret = -1;
  335. if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
  336. lbs_pr_err("No free skb\n");
  337. goto rx_ret;
  338. }
  339. cardp->rx_skb = skb;
  340. /* Fill the receive configuration URB and initialise the Rx call back */
  341. usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
  342. usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
  343. (void *) (skb->tail + (size_t) IPFIELD_ALIGN_OFFSET),
  344. MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
  345. cardp);
  346. cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
  347. lbs_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb);
  348. if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
  349. lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
  350. kfree_skb(skb);
  351. cardp->rx_skb = NULL;
  352. ret = -1;
  353. } else {
  354. lbs_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
  355. ret = 0;
  356. }
  357. rx_ret:
  358. return ret;
  359. }
  360. static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
  361. {
  362. return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
  363. }
  364. static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
  365. {
  366. return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
  367. }
  368. static void if_usb_receive_fwload(struct urb *urb)
  369. {
  370. struct if_usb_card *cardp = urb->context;
  371. struct sk_buff *skb = cardp->rx_skb;
  372. struct fwsyncheader *syncfwheader;
  373. struct bootcmdresp bootcmdresp;
  374. if (urb->status) {
  375. lbs_deb_usbd(&cardp->udev->dev,
  376. "URB status is failed during fw load\n");
  377. kfree_skb(skb);
  378. return;
  379. }
  380. if (cardp->fwdnldover) {
  381. __le32 *tmp = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
  382. if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
  383. tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
  384. lbs_pr_info("Firmware ready event received\n");
  385. wake_up(&cardp->fw_wq);
  386. } else {
  387. lbs_deb_usb("Waiting for confirmation; got %x %x\n",
  388. le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
  389. if_usb_submit_rx_urb_fwload(cardp);
  390. }
  391. kfree_skb(skb);
  392. return;
  393. }
  394. if (cardp->bootcmdresp <= 0) {
  395. memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
  396. sizeof(bootcmdresp));
  397. if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
  398. kfree_skb(skb);
  399. if_usb_submit_rx_urb_fwload(cardp);
  400. cardp->bootcmdresp = 1;
  401. lbs_deb_usbd(&cardp->udev->dev,
  402. "Received valid boot command response\n");
  403. return;
  404. }
  405. if (bootcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
  406. if (bootcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
  407. bootcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
  408. bootcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
  409. if (!cardp->bootcmdresp)
  410. lbs_pr_info("Firmware already seems alive; resetting\n");
  411. cardp->bootcmdresp = -1;
  412. } else {
  413. lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
  414. le32_to_cpu(bootcmdresp.magic));
  415. }
  416. } else if (bootcmdresp.cmd != BOOT_CMD_FW_BY_USB) {
  417. lbs_pr_info("boot cmd response cmd_tag error (%d)\n",
  418. bootcmdresp.cmd);
  419. } else if (bootcmdresp.result != BOOT_CMD_RESP_OK) {
  420. lbs_pr_info("boot cmd response result error (%d)\n",
  421. bootcmdresp.result);
  422. } else {
  423. cardp->bootcmdresp = 1;
  424. lbs_deb_usbd(&cardp->udev->dev,
  425. "Received valid boot command response\n");
  426. }
  427. kfree_skb(skb);
  428. if_usb_submit_rx_urb_fwload(cardp);
  429. return;
  430. }
  431. syncfwheader = kmalloc(sizeof(struct fwsyncheader), GFP_ATOMIC);
  432. if (!syncfwheader) {
  433. lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
  434. kfree_skb(skb);
  435. return;
  436. }
  437. memcpy(syncfwheader, skb->data + IPFIELD_ALIGN_OFFSET,
  438. sizeof(struct fwsyncheader));
  439. if (!syncfwheader->cmd) {
  440. lbs_deb_usb2(&cardp->udev->dev, "FW received Blk with correct CRC\n");
  441. lbs_deb_usb2(&cardp->udev->dev, "FW received Blk seqnum = %d\n",
  442. le32_to_cpu(syncfwheader->seqnum));
  443. cardp->CRC_OK = 1;
  444. } else {
  445. lbs_deb_usbd(&cardp->udev->dev, "FW received Blk with CRC error\n");
  446. cardp->CRC_OK = 0;
  447. }
  448. kfree_skb(skb);
  449. /* reschedule timer for 200ms hence */
  450. mod_timer(&cardp->fw_timeout, jiffies + (HZ/5));
  451. if (cardp->fwfinalblk) {
  452. cardp->fwdnldover = 1;
  453. goto exit;
  454. }
  455. if_usb_send_fw_pkt(cardp);
  456. exit:
  457. if_usb_submit_rx_urb_fwload(cardp);
  458. kfree(syncfwheader);
  459. return;
  460. }
  461. #define MRVDRV_MIN_PKT_LEN 30
  462. static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
  463. struct if_usb_card *cardp,
  464. struct lbs_private *priv)
  465. {
  466. if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
  467. || recvlength < MRVDRV_MIN_PKT_LEN) {
  468. lbs_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
  469. kfree_skb(skb);
  470. return;
  471. }
  472. skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
  473. skb_put(skb, recvlength);
  474. skb_pull(skb, MESSAGE_HEADER_LEN);
  475. lbs_process_rxed_packet(priv, skb);
  476. priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
  477. }
  478. static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff,
  479. struct sk_buff *skb,
  480. struct if_usb_card *cardp,
  481. struct lbs_private *priv)
  482. {
  483. if (recvlength > LBS_CMD_BUFFER_SIZE) {
  484. lbs_deb_usbd(&cardp->udev->dev,
  485. "The receive buffer is too large\n");
  486. kfree_skb(skb);
  487. return;
  488. }
  489. if (!in_interrupt())
  490. BUG();
  491. spin_lock(&priv->driver_lock);
  492. cardp->usb_int_cause |= MRVDRV_CMD_UPLD_RDY;
  493. priv->upld_len = (recvlength - MESSAGE_HEADER_LEN);
  494. memcpy(priv->upld_buf, recvbuff + MESSAGE_HEADER_LEN, priv->upld_len);
  495. kfree_skb(skb);
  496. lbs_interrupt(priv);
  497. spin_unlock(&priv->driver_lock);
  498. lbs_deb_usbd(&cardp->udev->dev,
  499. "Wake up main thread to handle cmd response\n");
  500. }
  501. /**
  502. * @brief This function reads of the packet into the upload buff,
  503. * wake up the main thread and initialise the Rx callack.
  504. *
  505. * @param urb pointer to struct urb
  506. * @return N/A
  507. */
  508. static void if_usb_receive(struct urb *urb)
  509. {
  510. struct if_usb_card *cardp = urb->context;
  511. struct sk_buff *skb = cardp->rx_skb;
  512. struct lbs_private *priv = cardp->priv;
  513. int recvlength = urb->actual_length;
  514. uint8_t *recvbuff = NULL;
  515. uint32_t recvtype = 0;
  516. __le32 *pkt = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
  517. lbs_deb_enter(LBS_DEB_USB);
  518. if (recvlength) {
  519. if (urb->status) {
  520. lbs_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
  521. urb->status);
  522. kfree_skb(skb);
  523. goto setup_for_next;
  524. }
  525. recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
  526. recvtype = le32_to_cpu(pkt[0]);
  527. lbs_deb_usbd(&cardp->udev->dev,
  528. "Recv length = 0x%x, Recv type = 0x%X\n",
  529. recvlength, recvtype);
  530. } else if (urb->status) {
  531. kfree_skb(skb);
  532. goto rx_exit;
  533. }
  534. switch (recvtype) {
  535. case CMD_TYPE_DATA:
  536. process_cmdtypedata(recvlength, skb, cardp, priv);
  537. break;
  538. case CMD_TYPE_REQUEST:
  539. process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
  540. break;
  541. case CMD_TYPE_INDICATION:
  542. /* Event cause handling */
  543. spin_lock(&priv->driver_lock);
  544. cardp->usb_event_cause = le32_to_cpu(pkt[1]);
  545. lbs_deb_usbd(&cardp->udev->dev,"**EVENT** 0x%X\n",
  546. cardp->usb_event_cause);
  547. /* Icky undocumented magic special case */
  548. if (cardp->usb_event_cause & 0xffff0000) {
  549. lbs_send_tx_feedback(priv);
  550. spin_unlock(&priv->driver_lock);
  551. break;
  552. }
  553. cardp->usb_event_cause <<= 3;
  554. cardp->usb_int_cause |= MRVDRV_CARDEVENT;
  555. kfree_skb(skb);
  556. lbs_interrupt(priv);
  557. spin_unlock(&priv->driver_lock);
  558. goto rx_exit;
  559. default:
  560. lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
  561. recvtype);
  562. kfree_skb(skb);
  563. break;
  564. }
  565. setup_for_next:
  566. if_usb_submit_rx_urb(cardp);
  567. rx_exit:
  568. lbs_deb_leave(LBS_DEB_USB);
  569. }
  570. /**
  571. * @brief This function downloads data to FW
  572. * @param priv pointer to struct lbs_private structure
  573. * @param type type of data
  574. * @param buf pointer to data buffer
  575. * @param len number of bytes
  576. * @return 0 or -1
  577. */
  578. static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
  579. uint8_t *payload, uint16_t nb)
  580. {
  581. struct if_usb_card *cardp = priv->card;
  582. lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
  583. lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
  584. if (type == MVMS_CMD) {
  585. *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
  586. priv->dnld_sent = DNLD_CMD_SENT;
  587. } else {
  588. *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
  589. priv->dnld_sent = DNLD_DATA_SENT;
  590. }
  591. memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
  592. return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN);
  593. }
  594. /* called with priv->driver_lock held */
  595. static int if_usb_get_int_status(struct lbs_private *priv, uint8_t *ireg)
  596. {
  597. struct if_usb_card *cardp = priv->card;
  598. *ireg = cardp->usb_int_cause;
  599. cardp->usb_int_cause = 0;
  600. lbs_deb_usbd(&cardp->udev->dev, "Int cause is 0x%X\n", *ireg);
  601. return 0;
  602. }
  603. static int if_usb_read_event_cause(struct lbs_private *priv)
  604. {
  605. struct if_usb_card *cardp = priv->card;
  606. priv->eventcause = cardp->usb_event_cause;
  607. /* Re-submit rx urb here to avoid event lost issue */
  608. if_usb_submit_rx_urb(cardp);
  609. return 0;
  610. }
  611. /**
  612. * @brief This function issues Boot command to the Boot2 code
  613. * @param ivalue 1:Boot from FW by USB-Download
  614. * 2:Boot from FW in EEPROM
  615. * @return 0
  616. */
  617. static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue)
  618. {
  619. struct bootcmd *bootcmd = cardp->ep_out_buf;
  620. /* Prepare command */
  621. bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
  622. bootcmd->cmd = ivalue;
  623. memset(bootcmd->pad, 0, sizeof(bootcmd->pad));
  624. /* Issue command */
  625. usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd));
  626. return 0;
  627. }
  628. /**
  629. * @brief This function checks the validity of Boot2/FW image.
  630. *
  631. * @param data pointer to image
  632. * len image length
  633. * @return 0 or -1
  634. */
  635. static int check_fwfile_format(uint8_t *data, uint32_t totlen)
  636. {
  637. uint32_t bincmd, exit;
  638. uint32_t blksize, offset, len;
  639. int ret;
  640. ret = 1;
  641. exit = len = 0;
  642. do {
  643. struct fwheader *fwh = (void *)data;
  644. bincmd = le32_to_cpu(fwh->dnldcmd);
  645. blksize = le32_to_cpu(fwh->datalength);
  646. switch (bincmd) {
  647. case FW_HAS_DATA_TO_RECV:
  648. offset = sizeof(struct fwheader) + blksize;
  649. data += offset;
  650. len += offset;
  651. if (len >= totlen)
  652. exit = 1;
  653. break;
  654. case FW_HAS_LAST_BLOCK:
  655. exit = 1;
  656. ret = 0;
  657. break;
  658. default:
  659. exit = 1;
  660. break;
  661. }
  662. } while (!exit);
  663. if (ret)
  664. lbs_pr_err("firmware file format check FAIL\n");
  665. else
  666. lbs_deb_fw("firmware file format check PASS\n");
  667. return ret;
  668. }
  669. static int if_usb_prog_firmware(struct if_usb_card *cardp)
  670. {
  671. int i = 0;
  672. static int reset_count = 10;
  673. int ret = 0;
  674. lbs_deb_enter(LBS_DEB_USB);
  675. if ((ret = request_firmware(&cardp->fw, lbs_fw_name,
  676. &cardp->udev->dev)) < 0) {
  677. lbs_pr_err("request_firmware() failed with %#x\n", ret);
  678. lbs_pr_err("firmware %s not found\n", lbs_fw_name);
  679. goto done;
  680. }
  681. if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
  682. goto release_fw;
  683. restart:
  684. if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
  685. lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
  686. ret = -1;
  687. goto release_fw;
  688. }
  689. cardp->bootcmdresp = 0;
  690. do {
  691. int j = 0;
  692. i++;
  693. /* Issue Boot command = 1, Boot from Download-FW */
  694. if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
  695. /* wait for command response */
  696. do {
  697. j++;
  698. msleep_interruptible(100);
  699. } while (cardp->bootcmdresp == 0 && j < 10);
  700. } while (cardp->bootcmdresp == 0 && i < 5);
  701. if (cardp->bootcmdresp <= 0) {
  702. if (--reset_count >= 0) {
  703. if_usb_reset_device(cardp);
  704. goto restart;
  705. }
  706. return -1;
  707. }
  708. i = 0;
  709. cardp->totalbytes = 0;
  710. cardp->fwlastblksent = 0;
  711. cardp->CRC_OK = 1;
  712. cardp->fwdnldover = 0;
  713. cardp->fwseqnum = -1;
  714. cardp->totalbytes = 0;
  715. cardp->fwfinalblk = 0;
  716. /* Send the first firmware packet... */
  717. if_usb_send_fw_pkt(cardp);
  718. /* ... and wait for the process to complete */
  719. wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
  720. del_timer_sync(&cardp->fw_timeout);
  721. usb_kill_urb(cardp->rx_urb);
  722. if (!cardp->fwdnldover) {
  723. lbs_pr_info("failed to load fw, resetting device!\n");
  724. if (--reset_count >= 0) {
  725. if_usb_reset_device(cardp);
  726. goto restart;
  727. }
  728. lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
  729. ret = -1;
  730. goto release_fw;
  731. }
  732. release_fw:
  733. release_firmware(cardp->fw);
  734. cardp->fw = NULL;
  735. done:
  736. lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
  737. return ret;
  738. }
  739. #ifdef CONFIG_PM
  740. static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
  741. {
  742. struct if_usb_card *cardp = usb_get_intfdata(intf);
  743. struct lbs_private *priv = cardp->priv;
  744. int ret;
  745. lbs_deb_enter(LBS_DEB_USB);
  746. if (priv->psstate != PS_STATE_FULL_POWER)
  747. return -1;
  748. ret = lbs_suspend(priv);
  749. if (ret)
  750. goto out;
  751. /* Unlink tx & rx urb */
  752. usb_kill_urb(cardp->tx_urb);
  753. usb_kill_urb(cardp->rx_urb);
  754. out:
  755. lbs_deb_leave(LBS_DEB_USB);
  756. return ret;
  757. }
  758. static int if_usb_resume(struct usb_interface *intf)
  759. {
  760. struct if_usb_card *cardp = usb_get_intfdata(intf);
  761. struct lbs_private *priv = cardp->priv;
  762. lbs_deb_enter(LBS_DEB_USB);
  763. if_usb_submit_rx_urb(cardp);
  764. lbs_resume(priv);
  765. lbs_deb_leave(LBS_DEB_USB);
  766. return 0;
  767. }
  768. #else
  769. #define if_usb_suspend NULL
  770. #define if_usb_resume NULL
  771. #endif
  772. static struct usb_driver if_usb_driver = {
  773. .name = DRV_NAME,
  774. .probe = if_usb_probe,
  775. .disconnect = if_usb_disconnect,
  776. .id_table = if_usb_table,
  777. .suspend = if_usb_suspend,
  778. .resume = if_usb_resume,
  779. };
  780. static int __init if_usb_init_module(void)
  781. {
  782. int ret = 0;
  783. lbs_deb_enter(LBS_DEB_MAIN);
  784. ret = usb_register(&if_usb_driver);
  785. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  786. return ret;
  787. }
  788. static void __exit if_usb_exit_module(void)
  789. {
  790. lbs_deb_enter(LBS_DEB_MAIN);
  791. usb_deregister(&if_usb_driver);
  792. lbs_deb_leave(LBS_DEB_MAIN);
  793. }
  794. module_init(if_usb_init_module);
  795. module_exit(if_usb_exit_module);
  796. MODULE_DESCRIPTION("8388 USB WLAN Driver");
  797. MODULE_AUTHOR("Marvell International Ltd. and Red Hat, Inc.");
  798. MODULE_LICENSE("GPL");