if_usb.c 30 KB

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