if_usb.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /*
  2. * Copyright (C) 2008, cozybit Inc.
  3. * Copyright (C) 2003-2006, Marvell International Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. */
  10. #define DRV_NAME "lbtf_usb"
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include "libertas_tf.h"
  13. #include "if_usb.h"
  14. #include <linux/delay.h>
  15. #include <linux/module.h>
  16. #include <linux/firmware.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/slab.h>
  19. #include <linux/usb.h>
  20. #define INSANEDEBUG 0
  21. #define lbtf_deb_usb2(...) do { if (INSANEDEBUG) lbtf_deb_usbd(__VA_ARGS__); } while (0)
  22. #define MESSAGE_HEADER_LEN 4
  23. static char *lbtf_fw_name = "lbtf_usb.bin";
  24. module_param_named(fw_name, lbtf_fw_name, charp, 0644);
  25. MODULE_FIRMWARE("lbtf_usb.bin");
  26. static const struct usb_device_id if_usb_table[] = {
  27. /* Enter the device signature inside */
  28. { USB_DEVICE(0x1286, 0x2001) },
  29. { USB_DEVICE(0x05a3, 0x8388) },
  30. {} /* Terminating entry */
  31. };
  32. MODULE_DEVICE_TABLE(usb, if_usb_table);
  33. static void if_usb_receive(struct urb *urb);
  34. static void if_usb_receive_fwload(struct urb *urb);
  35. static int if_usb_prog_firmware(struct if_usb_card *cardp);
  36. static int if_usb_host_to_card(struct lbtf_private *priv, uint8_t type,
  37. uint8_t *payload, uint16_t nb);
  38. static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
  39. uint16_t nb, u8 data);
  40. static void if_usb_free(struct if_usb_card *cardp);
  41. static int if_usb_submit_rx_urb(struct if_usb_card *cardp);
  42. static int if_usb_reset_device(struct if_usb_card *cardp);
  43. /**
  44. * if_usb_wrike_bulk_callback - call back to handle URB status
  45. *
  46. * @param urb pointer to urb structure
  47. */
  48. static void if_usb_write_bulk_callback(struct urb *urb)
  49. {
  50. if (urb->status != 0) {
  51. /* print the failure status number for debug */
  52. pr_info("URB in failure status: %d\n", urb->status);
  53. } else {
  54. lbtf_deb_usb2(&urb->dev->dev, "URB status is successful\n");
  55. lbtf_deb_usb2(&urb->dev->dev, "Actual length transmitted %d\n",
  56. urb->actual_length);
  57. }
  58. }
  59. /**
  60. * if_usb_free - free tx/rx urb, skb and rx buffer
  61. *
  62. * @param cardp pointer if_usb_card
  63. */
  64. static void if_usb_free(struct if_usb_card *cardp)
  65. {
  66. lbtf_deb_enter(LBTF_DEB_USB);
  67. /* Unlink tx & rx urb */
  68. usb_kill_urb(cardp->tx_urb);
  69. usb_kill_urb(cardp->rx_urb);
  70. usb_kill_urb(cardp->cmd_urb);
  71. usb_free_urb(cardp->tx_urb);
  72. cardp->tx_urb = NULL;
  73. usb_free_urb(cardp->rx_urb);
  74. cardp->rx_urb = NULL;
  75. usb_free_urb(cardp->cmd_urb);
  76. cardp->cmd_urb = NULL;
  77. kfree(cardp->ep_out_buf);
  78. cardp->ep_out_buf = NULL;
  79. lbtf_deb_leave(LBTF_DEB_USB);
  80. }
  81. static void if_usb_setup_firmware(struct lbtf_private *priv)
  82. {
  83. struct if_usb_card *cardp = priv->card;
  84. struct cmd_ds_set_boot2_ver b2_cmd;
  85. lbtf_deb_enter(LBTF_DEB_USB);
  86. if_usb_submit_rx_urb(cardp);
  87. b2_cmd.hdr.size = cpu_to_le16(sizeof(b2_cmd));
  88. b2_cmd.action = 0;
  89. b2_cmd.version = cardp->boot2_version;
  90. if (lbtf_cmd_with_response(priv, CMD_SET_BOOT2_VER, &b2_cmd))
  91. lbtf_deb_usb("Setting boot2 version failed\n");
  92. lbtf_deb_leave(LBTF_DEB_USB);
  93. }
  94. static void if_usb_fw_timeo(struct timer_list *t)
  95. {
  96. struct if_usb_card *cardp = from_timer(cardp, t, fw_timeout);
  97. lbtf_deb_enter(LBTF_DEB_USB);
  98. if (!cardp->fwdnldover) {
  99. /* Download timed out */
  100. cardp->priv->surpriseremoved = 1;
  101. pr_err("Download timed out\n");
  102. } else {
  103. lbtf_deb_usb("Download complete, no event. Assuming success\n");
  104. }
  105. wake_up(&cardp->fw_wq);
  106. lbtf_deb_leave(LBTF_DEB_USB);
  107. }
  108. /**
  109. * if_usb_probe - sets the configuration values
  110. *
  111. * @ifnum interface number
  112. * @id pointer to usb_device_id
  113. *
  114. * Returns: 0 on success, error code on failure
  115. */
  116. static int if_usb_probe(struct usb_interface *intf,
  117. const struct usb_device_id *id)
  118. {
  119. struct usb_device *udev;
  120. struct usb_host_interface *iface_desc;
  121. struct usb_endpoint_descriptor *endpoint;
  122. struct lbtf_private *priv;
  123. struct if_usb_card *cardp;
  124. int i;
  125. lbtf_deb_enter(LBTF_DEB_USB);
  126. udev = interface_to_usbdev(intf);
  127. cardp = kzalloc(sizeof(struct if_usb_card), GFP_KERNEL);
  128. if (!cardp)
  129. goto error;
  130. timer_setup(&cardp->fw_timeout, if_usb_fw_timeo, 0);
  131. init_waitqueue_head(&cardp->fw_wq);
  132. cardp->udev = udev;
  133. iface_desc = intf->cur_altsetting;
  134. lbtf_deb_usbd(&udev->dev, "bcdUSB = 0x%X bDeviceClass = 0x%X"
  135. " bDeviceSubClass = 0x%X, bDeviceProtocol = 0x%X\n",
  136. le16_to_cpu(udev->descriptor.bcdUSB),
  137. udev->descriptor.bDeviceClass,
  138. udev->descriptor.bDeviceSubClass,
  139. udev->descriptor.bDeviceProtocol);
  140. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  141. endpoint = &iface_desc->endpoint[i].desc;
  142. if (usb_endpoint_is_bulk_in(endpoint)) {
  143. cardp->ep_in_size =
  144. le16_to_cpu(endpoint->wMaxPacketSize);
  145. cardp->ep_in = usb_endpoint_num(endpoint);
  146. lbtf_deb_usbd(&udev->dev, "in_endpoint = %d\n",
  147. cardp->ep_in);
  148. lbtf_deb_usbd(&udev->dev, "Bulk in size is %d\n",
  149. cardp->ep_in_size);
  150. } else if (usb_endpoint_is_bulk_out(endpoint)) {
  151. cardp->ep_out_size =
  152. le16_to_cpu(endpoint->wMaxPacketSize);
  153. cardp->ep_out = usb_endpoint_num(endpoint);
  154. lbtf_deb_usbd(&udev->dev, "out_endpoint = %d\n",
  155. cardp->ep_out);
  156. lbtf_deb_usbd(&udev->dev, "Bulk out size is %d\n",
  157. cardp->ep_out_size);
  158. }
  159. }
  160. if (!cardp->ep_out_size || !cardp->ep_in_size) {
  161. lbtf_deb_usbd(&udev->dev, "Endpoints not found\n");
  162. /* Endpoints not found */
  163. goto dealloc;
  164. }
  165. cardp->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  166. if (!cardp->rx_urb)
  167. goto dealloc;
  168. cardp->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  169. if (!cardp->tx_urb)
  170. goto dealloc;
  171. cardp->cmd_urb = usb_alloc_urb(0, GFP_KERNEL);
  172. if (!cardp->cmd_urb)
  173. goto dealloc;
  174. cardp->ep_out_buf = kmalloc(MRVDRV_ETH_TX_PACKET_BUFFER_SIZE,
  175. GFP_KERNEL);
  176. if (!cardp->ep_out_buf) {
  177. lbtf_deb_usbd(&udev->dev, "Could not allocate buffer\n");
  178. goto dealloc;
  179. }
  180. priv = lbtf_add_card(cardp, &udev->dev);
  181. if (!priv)
  182. goto dealloc;
  183. cardp->priv = priv;
  184. priv->hw_host_to_card = if_usb_host_to_card;
  185. priv->hw_prog_firmware = if_usb_prog_firmware;
  186. priv->hw_reset_device = if_usb_reset_device;
  187. cardp->boot2_version = udev->descriptor.bcdDevice;
  188. usb_get_dev(udev);
  189. usb_set_intfdata(intf, cardp);
  190. return 0;
  191. dealloc:
  192. if_usb_free(cardp);
  193. error:
  194. lbtf_deb_leave(LBTF_DEB_MAIN);
  195. return -ENOMEM;
  196. }
  197. /**
  198. * if_usb_disconnect - free resource and cleanup
  199. *
  200. * @intf USB interface structure
  201. */
  202. static void if_usb_disconnect(struct usb_interface *intf)
  203. {
  204. struct if_usb_card *cardp = usb_get_intfdata(intf);
  205. struct lbtf_private *priv = cardp->priv;
  206. lbtf_deb_enter(LBTF_DEB_MAIN);
  207. if_usb_reset_device(cardp);
  208. if (priv)
  209. lbtf_remove_card(priv);
  210. /* Unlink and free urb */
  211. if_usb_free(cardp);
  212. usb_set_intfdata(intf, NULL);
  213. usb_put_dev(interface_to_usbdev(intf));
  214. lbtf_deb_leave(LBTF_DEB_MAIN);
  215. }
  216. /**
  217. * if_usb_send_fw_pkt - This function downloads the FW
  218. *
  219. * @priv pointer to struct lbtf_private
  220. *
  221. * Returns: 0
  222. */
  223. static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
  224. {
  225. struct fwdata *fwdata = cardp->ep_out_buf;
  226. u8 *firmware = (u8 *) cardp->fw->data;
  227. lbtf_deb_enter(LBTF_DEB_FW);
  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. lbtf_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. lbtf_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), 0);
  252. if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
  253. lbtf_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
  254. lbtf_deb_usb2(&cardp->udev->dev,
  255. "seqnum = %d totalbytes = %d\n",
  256. cardp->fwseqnum, cardp->totalbytes);
  257. } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
  258. lbtf_deb_usb2(&cardp->udev->dev,
  259. "Host has finished FW downloading\n");
  260. lbtf_deb_usb2(&cardp->udev->dev, "Donwloading FW JUMP BLOCK\n");
  261. /* Host has finished FW downloading
  262. * Donwloading FW JUMP BLOCK
  263. */
  264. cardp->fwfinalblk = 1;
  265. }
  266. lbtf_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
  267. cardp->totalbytes);
  268. lbtf_deb_leave(LBTF_DEB_FW);
  269. return 0;
  270. }
  271. static int if_usb_reset_device(struct if_usb_card *cardp)
  272. {
  273. struct cmd_ds_802_11_reset *cmd = cardp->ep_out_buf + 4;
  274. int ret;
  275. lbtf_deb_enter(LBTF_DEB_USB);
  276. *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
  277. cmd->hdr.command = cpu_to_le16(CMD_802_11_RESET);
  278. cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_802_11_reset));
  279. cmd->hdr.result = cpu_to_le16(0);
  280. cmd->hdr.seqnum = cpu_to_le16(0x5a5a);
  281. cmd->action = cpu_to_le16(CMD_ACT_HALT);
  282. usb_tx_block(cardp, cardp->ep_out_buf,
  283. 4 + sizeof(struct cmd_ds_802_11_reset), 0);
  284. msleep(100);
  285. ret = usb_reset_device(cardp->udev);
  286. msleep(100);
  287. lbtf_deb_leave_args(LBTF_DEB_USB, "ret %d", ret);
  288. return ret;
  289. }
  290. /**
  291. * usb_tx_block - transfer data to the device
  292. *
  293. * @priv pointer to struct lbtf_private
  294. * @payload pointer to payload data
  295. * @nb data length
  296. * @data non-zero for data, zero for commands
  297. *
  298. * Returns: 0 on success, nonzero otherwise.
  299. */
  300. static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload,
  301. uint16_t nb, u8 data)
  302. {
  303. int ret = -1;
  304. struct urb *urb;
  305. lbtf_deb_enter(LBTF_DEB_USB);
  306. /* check if device is removed */
  307. if (cardp->priv->surpriseremoved) {
  308. lbtf_deb_usbd(&cardp->udev->dev, "Device removed\n");
  309. goto tx_ret;
  310. }
  311. if (data)
  312. urb = cardp->tx_urb;
  313. else
  314. urb = cardp->cmd_urb;
  315. usb_fill_bulk_urb(urb, cardp->udev,
  316. usb_sndbulkpipe(cardp->udev,
  317. cardp->ep_out),
  318. payload, nb, if_usb_write_bulk_callback, cardp);
  319. urb->transfer_flags |= URB_ZERO_PACKET;
  320. if (usb_submit_urb(urb, GFP_ATOMIC)) {
  321. lbtf_deb_usbd(&cardp->udev->dev,
  322. "usb_submit_urb failed: %d\n", ret);
  323. goto tx_ret;
  324. }
  325. lbtf_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
  326. ret = 0;
  327. tx_ret:
  328. lbtf_deb_leave(LBTF_DEB_USB);
  329. return ret;
  330. }
  331. static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
  332. void (*callbackfn)(struct urb *urb))
  333. {
  334. struct sk_buff *skb;
  335. int ret = -1;
  336. lbtf_deb_enter(LBTF_DEB_USB);
  337. skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
  338. if (!skb) {
  339. pr_err("No free skb\n");
  340. lbtf_deb_leave(LBTF_DEB_USB);
  341. return -1;
  342. }
  343. cardp->rx_skb = skb;
  344. /* Fill the receive configuration URB and initialise the Rx call back */
  345. usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
  346. usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
  347. skb_tail_pointer(skb),
  348. MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn, cardp);
  349. lbtf_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n",
  350. cardp->rx_urb);
  351. ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC);
  352. if (ret) {
  353. lbtf_deb_usbd(&cardp->udev->dev,
  354. "Submit Rx URB failed: %d\n", ret);
  355. kfree_skb(skb);
  356. cardp->rx_skb = NULL;
  357. lbtf_deb_leave(LBTF_DEB_USB);
  358. return -1;
  359. } else {
  360. lbtf_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
  361. lbtf_deb_leave(LBTF_DEB_USB);
  362. return 0;
  363. }
  364. }
  365. static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
  366. {
  367. return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
  368. }
  369. static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
  370. {
  371. return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
  372. }
  373. static void if_usb_receive_fwload(struct urb *urb)
  374. {
  375. struct if_usb_card *cardp = urb->context;
  376. struct sk_buff *skb = cardp->rx_skb;
  377. struct fwsyncheader *syncfwheader;
  378. struct bootcmdresp bcmdresp;
  379. lbtf_deb_enter(LBTF_DEB_USB);
  380. if (urb->status) {
  381. lbtf_deb_usbd(&cardp->udev->dev,
  382. "URB status is failed during fw load\n");
  383. kfree_skb(skb);
  384. lbtf_deb_leave(LBTF_DEB_USB);
  385. return;
  386. }
  387. if (cardp->fwdnldover) {
  388. __le32 *tmp = (__le32 *)(skb->data);
  389. if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
  390. tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
  391. /* Firmware ready event received */
  392. pr_info("Firmware ready event received\n");
  393. wake_up(&cardp->fw_wq);
  394. } else {
  395. lbtf_deb_usb("Waiting for confirmation; got %x %x\n",
  396. le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
  397. if_usb_submit_rx_urb_fwload(cardp);
  398. }
  399. kfree_skb(skb);
  400. lbtf_deb_leave(LBTF_DEB_USB);
  401. return;
  402. }
  403. if (cardp->bootcmdresp <= 0) {
  404. memcpy(&bcmdresp, skb->data, sizeof(bcmdresp));
  405. if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
  406. kfree_skb(skb);
  407. if_usb_submit_rx_urb_fwload(cardp);
  408. cardp->bootcmdresp = 1;
  409. /* Received valid boot command response */
  410. lbtf_deb_usbd(&cardp->udev->dev,
  411. "Received valid boot command response\n");
  412. lbtf_deb_leave(LBTF_DEB_USB);
  413. return;
  414. }
  415. if (bcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
  416. if (bcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
  417. bcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
  418. bcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
  419. if (!cardp->bootcmdresp)
  420. pr_info("Firmware already seems alive; resetting\n");
  421. cardp->bootcmdresp = -1;
  422. } else {
  423. pr_info("boot cmd response wrong magic number (0x%x)\n",
  424. le32_to_cpu(bcmdresp.magic));
  425. }
  426. } else if (bcmdresp.cmd != BOOT_CMD_FW_BY_USB) {
  427. pr_info("boot cmd response cmd_tag error (%d)\n",
  428. bcmdresp.cmd);
  429. } else if (bcmdresp.result != BOOT_CMD_RESP_OK) {
  430. pr_info("boot cmd response result error (%d)\n",
  431. bcmdresp.result);
  432. } else {
  433. cardp->bootcmdresp = 1;
  434. lbtf_deb_usbd(&cardp->udev->dev,
  435. "Received valid boot command response\n");
  436. }
  437. kfree_skb(skb);
  438. if_usb_submit_rx_urb_fwload(cardp);
  439. lbtf_deb_leave(LBTF_DEB_USB);
  440. return;
  441. }
  442. syncfwheader = kmemdup(skb->data, sizeof(struct fwsyncheader),
  443. GFP_ATOMIC);
  444. if (!syncfwheader) {
  445. lbtf_deb_usbd(&cardp->udev->dev,
  446. "Failure to allocate syncfwheader\n");
  447. kfree_skb(skb);
  448. lbtf_deb_leave(LBTF_DEB_USB);
  449. return;
  450. }
  451. if (!syncfwheader->cmd) {
  452. lbtf_deb_usb2(&cardp->udev->dev,
  453. "FW received Blk with correct CRC\n");
  454. lbtf_deb_usb2(&cardp->udev->dev,
  455. "FW received Blk seqnum = %d\n",
  456. le32_to_cpu(syncfwheader->seqnum));
  457. cardp->CRC_OK = 1;
  458. } else {
  459. lbtf_deb_usbd(&cardp->udev->dev,
  460. "FW received Blk with CRC error\n");
  461. cardp->CRC_OK = 0;
  462. }
  463. kfree_skb(skb);
  464. /* reschedule timer for 200ms hence */
  465. mod_timer(&cardp->fw_timeout, jiffies + (HZ/5));
  466. if (cardp->fwfinalblk) {
  467. cardp->fwdnldover = 1;
  468. goto exit;
  469. }
  470. if_usb_send_fw_pkt(cardp);
  471. exit:
  472. if_usb_submit_rx_urb_fwload(cardp);
  473. kfree(syncfwheader);
  474. lbtf_deb_leave(LBTF_DEB_USB);
  475. }
  476. #define MRVDRV_MIN_PKT_LEN 30
  477. static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
  478. struct if_usb_card *cardp,
  479. struct lbtf_private *priv)
  480. {
  481. if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
  482. || recvlength < MRVDRV_MIN_PKT_LEN) {
  483. lbtf_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
  484. kfree_skb(skb);
  485. return;
  486. }
  487. skb_put(skb, recvlength);
  488. skb_pull(skb, MESSAGE_HEADER_LEN);
  489. lbtf_rx(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 lbtf_private *priv)
  495. {
  496. unsigned long flags;
  497. if (recvlength < MESSAGE_HEADER_LEN ||
  498. recvlength > LBS_CMD_BUFFER_SIZE) {
  499. lbtf_deb_usbd(&cardp->udev->dev,
  500. "The receive buffer is invalid: %d\n", recvlength);
  501. kfree_skb(skb);
  502. return;
  503. }
  504. spin_lock_irqsave(&priv->driver_lock, flags);
  505. memcpy(priv->cmd_resp_buff, recvbuff + MESSAGE_HEADER_LEN,
  506. recvlength - MESSAGE_HEADER_LEN);
  507. kfree_skb(skb);
  508. lbtf_cmd_response_rx(priv);
  509. spin_unlock_irqrestore(&priv->driver_lock, flags);
  510. }
  511. /**
  512. * if_usb_receive - read data received from the device.
  513. *
  514. * @urb pointer to struct urb
  515. */
  516. static void if_usb_receive(struct urb *urb)
  517. {
  518. struct if_usb_card *cardp = urb->context;
  519. struct sk_buff *skb = cardp->rx_skb;
  520. struct lbtf_private *priv = cardp->priv;
  521. int recvlength = urb->actual_length;
  522. uint8_t *recvbuff = NULL;
  523. uint32_t recvtype = 0;
  524. __le32 *pkt = (__le32 *) skb->data;
  525. lbtf_deb_enter(LBTF_DEB_USB);
  526. if (recvlength) {
  527. if (urb->status) {
  528. lbtf_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
  529. urb->status);
  530. kfree_skb(skb);
  531. goto setup_for_next;
  532. }
  533. recvbuff = skb->data;
  534. recvtype = le32_to_cpu(pkt[0]);
  535. lbtf_deb_usbd(&cardp->udev->dev,
  536. "Recv length = 0x%x, Recv type = 0x%X\n",
  537. recvlength, recvtype);
  538. } else if (urb->status) {
  539. kfree_skb(skb);
  540. lbtf_deb_leave(LBTF_DEB_USB);
  541. return;
  542. }
  543. switch (recvtype) {
  544. case CMD_TYPE_DATA:
  545. process_cmdtypedata(recvlength, skb, cardp, priv);
  546. break;
  547. case CMD_TYPE_REQUEST:
  548. process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
  549. break;
  550. case CMD_TYPE_INDICATION:
  551. {
  552. /* Event cause handling */
  553. u32 event_cause = le32_to_cpu(pkt[1]);
  554. lbtf_deb_usbd(&cardp->udev->dev, "**EVENT** 0x%X\n",
  555. event_cause);
  556. /* Icky undocumented magic special case */
  557. if (event_cause & 0xffff0000) {
  558. u16 tmp;
  559. u8 retrycnt;
  560. u8 failure;
  561. tmp = event_cause >> 16;
  562. retrycnt = tmp & 0x00ff;
  563. failure = (tmp & 0xff00) >> 8;
  564. lbtf_send_tx_feedback(priv, retrycnt, failure);
  565. } else if (event_cause == LBTF_EVENT_BCN_SENT)
  566. lbtf_bcn_sent(priv);
  567. else
  568. lbtf_deb_usbd(&cardp->udev->dev,
  569. "Unsupported notification %d received\n",
  570. event_cause);
  571. kfree_skb(skb);
  572. break;
  573. }
  574. default:
  575. lbtf_deb_usbd(&cardp->udev->dev,
  576. "libertastf: unknown command type 0x%X\n", recvtype);
  577. kfree_skb(skb);
  578. break;
  579. }
  580. setup_for_next:
  581. if_usb_submit_rx_urb(cardp);
  582. lbtf_deb_leave(LBTF_DEB_USB);
  583. }
  584. /**
  585. * if_usb_host_to_card - Download data to the device
  586. *
  587. * @priv pointer to struct lbtf_private structure
  588. * @type type of data
  589. * @buf pointer to data buffer
  590. * @len number of bytes
  591. *
  592. * Returns: 0 on success, nonzero otherwise
  593. */
  594. static int if_usb_host_to_card(struct lbtf_private *priv, uint8_t type,
  595. uint8_t *payload, uint16_t nb)
  596. {
  597. struct if_usb_card *cardp = priv->card;
  598. u8 data = 0;
  599. lbtf_deb_usbd(&cardp->udev->dev, "*** type = %u\n", type);
  600. lbtf_deb_usbd(&cardp->udev->dev, "size after = %d\n", nb);
  601. if (type == MVMS_CMD) {
  602. *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
  603. } else {
  604. *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
  605. data = 1;
  606. }
  607. memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
  608. return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN,
  609. data);
  610. }
  611. /**
  612. * if_usb_issue_boot_command - Issue boot command to Boot2.
  613. *
  614. * @ivalue 1 boots from FW by USB-Download, 2 boots from FW in EEPROM.
  615. *
  616. * Returns: 0
  617. */
  618. static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue)
  619. {
  620. struct bootcmd *bootcmd = cardp->ep_out_buf;
  621. /* Prepare command */
  622. bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
  623. bootcmd->cmd = ivalue;
  624. memset(bootcmd->pad, 0, sizeof(bootcmd->pad));
  625. /* Issue command */
  626. usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd), 0);
  627. return 0;
  628. }
  629. /**
  630. * check_fwfile_format - Check the validity of Boot2/FW image.
  631. *
  632. * @data pointer to image
  633. * @totlen image length
  634. *
  635. * Returns: 0 if the image is valid, nonzero otherwise.
  636. */
  637. static int check_fwfile_format(const u8 *data, u32 totlen)
  638. {
  639. u32 bincmd, exit;
  640. u32 blksize, offset, len;
  641. int ret;
  642. ret = 1;
  643. exit = len = 0;
  644. do {
  645. struct fwheader *fwh = (void *) data;
  646. bincmd = le32_to_cpu(fwh->dnldcmd);
  647. blksize = le32_to_cpu(fwh->datalength);
  648. switch (bincmd) {
  649. case FW_HAS_DATA_TO_RECV:
  650. offset = sizeof(struct fwheader) + blksize;
  651. data += offset;
  652. len += offset;
  653. if (len >= totlen)
  654. exit = 1;
  655. break;
  656. case FW_HAS_LAST_BLOCK:
  657. exit = 1;
  658. ret = 0;
  659. break;
  660. default:
  661. exit = 1;
  662. break;
  663. }
  664. } while (!exit);
  665. if (ret)
  666. pr_err("firmware file format check FAIL\n");
  667. else
  668. lbtf_deb_fw("firmware file format check PASS\n");
  669. return ret;
  670. }
  671. static int if_usb_prog_firmware(struct if_usb_card *cardp)
  672. {
  673. int i = 0;
  674. static int reset_count = 10;
  675. int ret = 0;
  676. lbtf_deb_enter(LBTF_DEB_USB);
  677. kernel_param_lock(THIS_MODULE);
  678. ret = request_firmware(&cardp->fw, lbtf_fw_name, &cardp->udev->dev);
  679. if (ret < 0) {
  680. kernel_param_unlock(THIS_MODULE);
  681. goto done;
  682. }
  683. kernel_param_unlock(THIS_MODULE);
  684. if (check_fwfile_format(cardp->fw->data, cardp->fw->size))
  685. goto release_fw;
  686. restart:
  687. if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
  688. lbtf_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
  689. ret = -1;
  690. goto release_fw;
  691. }
  692. cardp->bootcmdresp = 0;
  693. do {
  694. int j = 0;
  695. i++;
  696. /* Issue Boot command = 1, Boot from Download-FW */
  697. if_usb_issue_boot_command(cardp, BOOT_CMD_FW_BY_USB);
  698. /* wait for command response */
  699. do {
  700. j++;
  701. msleep_interruptible(100);
  702. } while (cardp->bootcmdresp == 0 && j < 10);
  703. } while (cardp->bootcmdresp == 0 && i < 5);
  704. if (cardp->bootcmdresp <= 0) {
  705. if (--reset_count >= 0) {
  706. if_usb_reset_device(cardp);
  707. goto restart;
  708. }
  709. return -1;
  710. }
  711. i = 0;
  712. cardp->totalbytes = 0;
  713. cardp->fwlastblksent = 0;
  714. cardp->CRC_OK = 1;
  715. cardp->fwdnldover = 0;
  716. cardp->fwseqnum = -1;
  717. cardp->totalbytes = 0;
  718. cardp->fwfinalblk = 0;
  719. /* Send the first firmware packet... */
  720. if_usb_send_fw_pkt(cardp);
  721. /* ... and wait for the process to complete */
  722. wait_event_interruptible(cardp->fw_wq, cardp->priv->surpriseremoved ||
  723. cardp->fwdnldover);
  724. del_timer_sync(&cardp->fw_timeout);
  725. usb_kill_urb(cardp->rx_urb);
  726. if (!cardp->fwdnldover) {
  727. pr_info("failed to load fw, resetting device!\n");
  728. if (--reset_count >= 0) {
  729. if_usb_reset_device(cardp);
  730. goto restart;
  731. }
  732. pr_info("FW download failure, time = %d ms\n", i * 100);
  733. ret = -1;
  734. goto release_fw;
  735. }
  736. cardp->priv->fw_ready = 1;
  737. release_fw:
  738. release_firmware(cardp->fw);
  739. cardp->fw = NULL;
  740. if_usb_setup_firmware(cardp->priv);
  741. done:
  742. lbtf_deb_leave_args(LBTF_DEB_USB, "ret %d", ret);
  743. return ret;
  744. }
  745. #define if_usb_suspend NULL
  746. #define if_usb_resume NULL
  747. static struct usb_driver if_usb_driver = {
  748. .name = DRV_NAME,
  749. .probe = if_usb_probe,
  750. .disconnect = if_usb_disconnect,
  751. .id_table = if_usb_table,
  752. .suspend = if_usb_suspend,
  753. .resume = if_usb_resume,
  754. .disable_hub_initiated_lpm = 1,
  755. };
  756. module_usb_driver(if_usb_driver);
  757. MODULE_DESCRIPTION("8388 USB WLAN Thinfirm Driver");
  758. MODULE_AUTHOR("Cozybit Inc.");
  759. MODULE_LICENSE("GPL");