if_usb.c 24 KB

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