if_usb.c 29 KB

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