if_usb.c 30 KB

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