if_usb.c 29 KB

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