if_usb.c 25 KB

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