if_usb.c 24 KB

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