if_usb.c 23 KB

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