if_usb.c 28 KB

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