if_usb.c 25 KB

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