if_usb.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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. if (__if_usb_prog_firmware(cardp, lbs_fw_name, BOOT_CMD_FW_BY_USB)) {
  256. lbs_deb_usbd(&udev->dev, "FW upload failed\n");
  257. goto err_prog_firmware;
  258. }
  259. if (!(priv = lbs_add_card(cardp, &udev->dev)))
  260. goto err_prog_firmware;
  261. cardp->priv = priv;
  262. cardp->priv->fw_ready = 1;
  263. priv->hw_host_to_card = if_usb_host_to_card;
  264. priv->enter_deep_sleep = NULL;
  265. priv->exit_deep_sleep = NULL;
  266. priv->reset_deep_sleep_wakeup = NULL;
  267. #ifdef CONFIG_OLPC
  268. if (machine_is_olpc())
  269. priv->reset_card = if_usb_reset_olpc_card;
  270. #endif
  271. cardp->boot2_version = udev->descriptor.bcdDevice;
  272. if_usb_submit_rx_urb(cardp);
  273. if (lbs_start_card(priv))
  274. goto err_start_card;
  275. if_usb_setup_firmware(priv);
  276. usb_get_dev(udev);
  277. usb_set_intfdata(intf, cardp);
  278. if (device_create_file(&priv->dev->dev, &dev_attr_lbs_flash_fw))
  279. lbs_pr_err("cannot register lbs_flash_fw attribute\n");
  280. if (device_create_file(&priv->dev->dev, &dev_attr_lbs_flash_boot2))
  281. lbs_pr_err("cannot register lbs_flash_boot2 attribute\n");
  282. return 0;
  283. err_start_card:
  284. lbs_remove_card(priv);
  285. err_prog_firmware:
  286. if_usb_reset_device(cardp);
  287. dealloc:
  288. if_usb_free(cardp);
  289. error:
  290. return -ENOMEM;
  291. }
  292. /**
  293. * @brief free resource and cleanup
  294. * @param intf USB interface structure
  295. * @return N/A
  296. */
  297. static void if_usb_disconnect(struct usb_interface *intf)
  298. {
  299. struct if_usb_card *cardp = usb_get_intfdata(intf);
  300. struct lbs_private *priv = (struct lbs_private *) cardp->priv;
  301. lbs_deb_enter(LBS_DEB_MAIN);
  302. device_remove_file(&priv->dev->dev, &dev_attr_lbs_flash_boot2);
  303. device_remove_file(&priv->dev->dev, &dev_attr_lbs_flash_fw);
  304. cardp->surprise_removed = 1;
  305. if (priv) {
  306. priv->surpriseremoved = 1;
  307. lbs_stop_card(priv);
  308. lbs_remove_card(priv);
  309. }
  310. /* Unlink and free urb */
  311. if_usb_free(cardp);
  312. usb_set_intfdata(intf, NULL);
  313. usb_put_dev(interface_to_usbdev(intf));
  314. lbs_deb_leave(LBS_DEB_MAIN);
  315. }
  316. /**
  317. * @brief This function download FW
  318. * @param priv pointer to struct lbs_private
  319. * @return 0
  320. */
  321. static int if_usb_send_fw_pkt(struct if_usb_card *cardp)
  322. {
  323. struct fwdata *fwdata = cardp->ep_out_buf;
  324. const uint8_t *firmware = cardp->fw->data;
  325. /* If we got a CRC failure on the last block, back
  326. up and retry it */
  327. if (!cardp->CRC_OK) {
  328. cardp->totalbytes = cardp->fwlastblksent;
  329. cardp->fwseqnum--;
  330. }
  331. lbs_deb_usb2(&cardp->udev->dev, "totalbytes = %d\n",
  332. cardp->totalbytes);
  333. /* struct fwdata (which we sent to the card) has an
  334. extra __le32 field in between the header and the data,
  335. which is not in the struct fwheader in the actual
  336. firmware binary. Insert the seqnum in the middle... */
  337. memcpy(&fwdata->hdr, &firmware[cardp->totalbytes],
  338. sizeof(struct fwheader));
  339. cardp->fwlastblksent = cardp->totalbytes;
  340. cardp->totalbytes += sizeof(struct fwheader);
  341. memcpy(fwdata->data, &firmware[cardp->totalbytes],
  342. le32_to_cpu(fwdata->hdr.datalength));
  343. lbs_deb_usb2(&cardp->udev->dev, "Data length = %d\n",
  344. le32_to_cpu(fwdata->hdr.datalength));
  345. fwdata->seqnum = cpu_to_le32(++cardp->fwseqnum);
  346. cardp->totalbytes += le32_to_cpu(fwdata->hdr.datalength);
  347. usb_tx_block(cardp, cardp->ep_out_buf, sizeof(struct fwdata) +
  348. le32_to_cpu(fwdata->hdr.datalength));
  349. if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_DATA_TO_RECV)) {
  350. lbs_deb_usb2(&cardp->udev->dev, "There are data to follow\n");
  351. lbs_deb_usb2(&cardp->udev->dev, "seqnum = %d totalbytes = %d\n",
  352. cardp->fwseqnum, cardp->totalbytes);
  353. } else if (fwdata->hdr.dnldcmd == cpu_to_le32(FW_HAS_LAST_BLOCK)) {
  354. lbs_deb_usb2(&cardp->udev->dev, "Host has finished FW downloading\n");
  355. lbs_deb_usb2(&cardp->udev->dev, "Donwloading FW JUMP BLOCK\n");
  356. cardp->fwfinalblk = 1;
  357. }
  358. lbs_deb_usb2(&cardp->udev->dev, "Firmware download done; size %d\n",
  359. cardp->totalbytes);
  360. return 0;
  361. }
  362. static int if_usb_reset_device(struct if_usb_card *cardp)
  363. {
  364. struct cmd_header *cmd = cardp->ep_out_buf + 4;
  365. int ret;
  366. lbs_deb_enter(LBS_DEB_USB);
  367. *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
  368. cmd->command = cpu_to_le16(CMD_802_11_RESET);
  369. cmd->size = cpu_to_le16(sizeof(cmd));
  370. cmd->result = cpu_to_le16(0);
  371. cmd->seqnum = cpu_to_le16(0x5a5a);
  372. usb_tx_block(cardp, cardp->ep_out_buf, 4 + sizeof(struct cmd_header));
  373. msleep(100);
  374. ret = usb_reset_device(cardp->udev);
  375. msleep(100);
  376. #ifdef CONFIG_OLPC
  377. if (ret && machine_is_olpc())
  378. if_usb_reset_olpc_card(NULL);
  379. #endif
  380. lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
  381. return ret;
  382. }
  383. /**
  384. * @brief This function transfer the data to the device.
  385. * @param priv pointer to struct lbs_private
  386. * @param payload pointer to payload data
  387. * @param nb data length
  388. * @return 0 or -1
  389. */
  390. static int usb_tx_block(struct if_usb_card *cardp, uint8_t *payload, uint16_t nb)
  391. {
  392. int ret = -1;
  393. /* check if device is removed */
  394. if (cardp->surprise_removed) {
  395. lbs_deb_usbd(&cardp->udev->dev, "Device removed\n");
  396. goto tx_ret;
  397. }
  398. usb_fill_bulk_urb(cardp->tx_urb, cardp->udev,
  399. usb_sndbulkpipe(cardp->udev,
  400. cardp->ep_out),
  401. payload, nb, if_usb_write_bulk_callback, cardp);
  402. cardp->tx_urb->transfer_flags |= URB_ZERO_PACKET;
  403. if ((ret = usb_submit_urb(cardp->tx_urb, GFP_ATOMIC))) {
  404. lbs_deb_usbd(&cardp->udev->dev, "usb_submit_urb failed: %d\n", ret);
  405. ret = -1;
  406. } else {
  407. lbs_deb_usb2(&cardp->udev->dev, "usb_submit_urb success\n");
  408. ret = 0;
  409. }
  410. tx_ret:
  411. return ret;
  412. }
  413. static int __if_usb_submit_rx_urb(struct if_usb_card *cardp,
  414. void (*callbackfn)(struct urb *urb))
  415. {
  416. struct sk_buff *skb;
  417. int ret = -1;
  418. if (!(skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE))) {
  419. lbs_pr_err("No free skb\n");
  420. goto rx_ret;
  421. }
  422. cardp->rx_skb = skb;
  423. /* Fill the receive configuration URB and initialise the Rx call back */
  424. usb_fill_bulk_urb(cardp->rx_urb, cardp->udev,
  425. usb_rcvbulkpipe(cardp->udev, cardp->ep_in),
  426. skb->data + IPFIELD_ALIGN_OFFSET,
  427. MRVDRV_ETH_RX_PACKET_BUFFER_SIZE, callbackfn,
  428. cardp);
  429. cardp->rx_urb->transfer_flags |= URB_ZERO_PACKET;
  430. lbs_deb_usb2(&cardp->udev->dev, "Pointer for rx_urb %p\n", cardp->rx_urb);
  431. if ((ret = usb_submit_urb(cardp->rx_urb, GFP_ATOMIC))) {
  432. lbs_deb_usbd(&cardp->udev->dev, "Submit Rx URB failed: %d\n", ret);
  433. kfree_skb(skb);
  434. cardp->rx_skb = NULL;
  435. ret = -1;
  436. } else {
  437. lbs_deb_usb2(&cardp->udev->dev, "Submit Rx URB success\n");
  438. ret = 0;
  439. }
  440. rx_ret:
  441. return ret;
  442. }
  443. static int if_usb_submit_rx_urb_fwload(struct if_usb_card *cardp)
  444. {
  445. return __if_usb_submit_rx_urb(cardp, &if_usb_receive_fwload);
  446. }
  447. static int if_usb_submit_rx_urb(struct if_usb_card *cardp)
  448. {
  449. return __if_usb_submit_rx_urb(cardp, &if_usb_receive);
  450. }
  451. static void if_usb_receive_fwload(struct urb *urb)
  452. {
  453. struct if_usb_card *cardp = urb->context;
  454. struct sk_buff *skb = cardp->rx_skb;
  455. struct fwsyncheader *syncfwheader;
  456. struct bootcmdresp bootcmdresp;
  457. if (urb->status) {
  458. lbs_deb_usbd(&cardp->udev->dev,
  459. "URB status is failed during fw load\n");
  460. kfree_skb(skb);
  461. return;
  462. }
  463. if (cardp->fwdnldover) {
  464. __le32 *tmp = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
  465. if (tmp[0] == cpu_to_le32(CMD_TYPE_INDICATION) &&
  466. tmp[1] == cpu_to_le32(MACREG_INT_CODE_FIRMWARE_READY)) {
  467. lbs_pr_info("Firmware ready event received\n");
  468. wake_up(&cardp->fw_wq);
  469. } else {
  470. lbs_deb_usb("Waiting for confirmation; got %x %x\n",
  471. le32_to_cpu(tmp[0]), le32_to_cpu(tmp[1]));
  472. if_usb_submit_rx_urb_fwload(cardp);
  473. }
  474. kfree_skb(skb);
  475. return;
  476. }
  477. if (cardp->bootcmdresp <= 0) {
  478. memcpy (&bootcmdresp, skb->data + IPFIELD_ALIGN_OFFSET,
  479. sizeof(bootcmdresp));
  480. if (le16_to_cpu(cardp->udev->descriptor.bcdDevice) < 0x3106) {
  481. kfree_skb(skb);
  482. if_usb_submit_rx_urb_fwload(cardp);
  483. cardp->bootcmdresp = BOOT_CMD_RESP_OK;
  484. lbs_deb_usbd(&cardp->udev->dev,
  485. "Received valid boot command response\n");
  486. return;
  487. }
  488. if (bootcmdresp.magic != cpu_to_le32(BOOT_CMD_MAGIC_NUMBER)) {
  489. if (bootcmdresp.magic == cpu_to_le32(CMD_TYPE_REQUEST) ||
  490. bootcmdresp.magic == cpu_to_le32(CMD_TYPE_DATA) ||
  491. bootcmdresp.magic == cpu_to_le32(CMD_TYPE_INDICATION)) {
  492. if (!cardp->bootcmdresp)
  493. lbs_pr_info("Firmware already seems alive; resetting\n");
  494. cardp->bootcmdresp = -1;
  495. } else {
  496. lbs_pr_info("boot cmd response wrong magic number (0x%x)\n",
  497. le32_to_cpu(bootcmdresp.magic));
  498. }
  499. } else if ((bootcmdresp.cmd != BOOT_CMD_FW_BY_USB) &&
  500. (bootcmdresp.cmd != BOOT_CMD_UPDATE_FW) &&
  501. (bootcmdresp.cmd != BOOT_CMD_UPDATE_BOOT2)) {
  502. lbs_pr_info("boot cmd response cmd_tag error (%d)\n",
  503. bootcmdresp.cmd);
  504. } else if (bootcmdresp.result != BOOT_CMD_RESP_OK) {
  505. lbs_pr_info("boot cmd response result error (%d)\n",
  506. bootcmdresp.result);
  507. } else {
  508. cardp->bootcmdresp = 1;
  509. lbs_deb_usbd(&cardp->udev->dev,
  510. "Received valid boot command response\n");
  511. }
  512. kfree_skb(skb);
  513. if_usb_submit_rx_urb_fwload(cardp);
  514. return;
  515. }
  516. syncfwheader = kmemdup(skb->data + IPFIELD_ALIGN_OFFSET,
  517. sizeof(struct fwsyncheader), GFP_ATOMIC);
  518. if (!syncfwheader) {
  519. lbs_deb_usbd(&cardp->udev->dev, "Failure to allocate syncfwheader\n");
  520. kfree_skb(skb);
  521. return;
  522. }
  523. if (!syncfwheader->cmd) {
  524. lbs_deb_usb2(&cardp->udev->dev, "FW received Blk with correct CRC\n");
  525. lbs_deb_usb2(&cardp->udev->dev, "FW received Blk seqnum = %d\n",
  526. le32_to_cpu(syncfwheader->seqnum));
  527. cardp->CRC_OK = 1;
  528. } else {
  529. lbs_deb_usbd(&cardp->udev->dev, "FW received Blk with CRC error\n");
  530. cardp->CRC_OK = 0;
  531. }
  532. kfree_skb(skb);
  533. /* Give device 5s to either write firmware to its RAM or eeprom */
  534. mod_timer(&cardp->fw_timeout, jiffies + (HZ*5));
  535. if (cardp->fwfinalblk) {
  536. cardp->fwdnldover = 1;
  537. goto exit;
  538. }
  539. if_usb_send_fw_pkt(cardp);
  540. exit:
  541. if_usb_submit_rx_urb_fwload(cardp);
  542. kfree(syncfwheader);
  543. }
  544. #define MRVDRV_MIN_PKT_LEN 30
  545. static inline void process_cmdtypedata(int recvlength, struct sk_buff *skb,
  546. struct if_usb_card *cardp,
  547. struct lbs_private *priv)
  548. {
  549. if (recvlength > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + MESSAGE_HEADER_LEN
  550. || recvlength < MRVDRV_MIN_PKT_LEN) {
  551. lbs_deb_usbd(&cardp->udev->dev, "Packet length is Invalid\n");
  552. kfree_skb(skb);
  553. return;
  554. }
  555. skb_reserve(skb, IPFIELD_ALIGN_OFFSET);
  556. skb_put(skb, recvlength);
  557. skb_pull(skb, MESSAGE_HEADER_LEN);
  558. lbs_process_rxed_packet(priv, skb);
  559. }
  560. static inline void process_cmdrequest(int recvlength, uint8_t *recvbuff,
  561. struct sk_buff *skb,
  562. struct if_usb_card *cardp,
  563. struct lbs_private *priv)
  564. {
  565. u8 i;
  566. if (recvlength > LBS_CMD_BUFFER_SIZE) {
  567. lbs_deb_usbd(&cardp->udev->dev,
  568. "The receive buffer is too large\n");
  569. kfree_skb(skb);
  570. return;
  571. }
  572. BUG_ON(!in_interrupt());
  573. spin_lock(&priv->driver_lock);
  574. i = (priv->resp_idx == 0) ? 1 : 0;
  575. BUG_ON(priv->resp_len[i]);
  576. priv->resp_len[i] = (recvlength - MESSAGE_HEADER_LEN);
  577. memcpy(priv->resp_buf[i], recvbuff + MESSAGE_HEADER_LEN,
  578. priv->resp_len[i]);
  579. kfree_skb(skb);
  580. lbs_notify_command_response(priv, i);
  581. spin_unlock(&priv->driver_lock);
  582. lbs_deb_usbd(&cardp->udev->dev,
  583. "Wake up main thread to handle cmd response\n");
  584. }
  585. /**
  586. * @brief This function reads of the packet into the upload buff,
  587. * wake up the main thread and initialise the Rx callack.
  588. *
  589. * @param urb pointer to struct urb
  590. * @return N/A
  591. */
  592. static void if_usb_receive(struct urb *urb)
  593. {
  594. struct if_usb_card *cardp = urb->context;
  595. struct sk_buff *skb = cardp->rx_skb;
  596. struct lbs_private *priv = cardp->priv;
  597. int recvlength = urb->actual_length;
  598. uint8_t *recvbuff = NULL;
  599. uint32_t recvtype = 0;
  600. __le32 *pkt = (__le32 *)(skb->data + IPFIELD_ALIGN_OFFSET);
  601. uint32_t event;
  602. lbs_deb_enter(LBS_DEB_USB);
  603. if (recvlength) {
  604. if (urb->status) {
  605. lbs_deb_usbd(&cardp->udev->dev, "RX URB failed: %d\n",
  606. urb->status);
  607. kfree_skb(skb);
  608. goto setup_for_next;
  609. }
  610. recvbuff = skb->data + IPFIELD_ALIGN_OFFSET;
  611. recvtype = le32_to_cpu(pkt[0]);
  612. lbs_deb_usbd(&cardp->udev->dev,
  613. "Recv length = 0x%x, Recv type = 0x%X\n",
  614. recvlength, recvtype);
  615. } else if (urb->status) {
  616. kfree_skb(skb);
  617. goto rx_exit;
  618. }
  619. switch (recvtype) {
  620. case CMD_TYPE_DATA:
  621. process_cmdtypedata(recvlength, skb, cardp, priv);
  622. break;
  623. case CMD_TYPE_REQUEST:
  624. process_cmdrequest(recvlength, recvbuff, skb, cardp, priv);
  625. break;
  626. case CMD_TYPE_INDICATION:
  627. /* Event handling */
  628. event = le32_to_cpu(pkt[1]);
  629. lbs_deb_usbd(&cardp->udev->dev, "**EVENT** 0x%X\n", event);
  630. kfree_skb(skb);
  631. /* Icky undocumented magic special case */
  632. if (event & 0xffff0000) {
  633. u32 trycount = (event & 0xffff0000) >> 16;
  634. lbs_send_tx_feedback(priv, trycount);
  635. } else
  636. lbs_queue_event(priv, event & 0xFF);
  637. break;
  638. default:
  639. lbs_deb_usbd(&cardp->udev->dev, "Unknown command type 0x%X\n",
  640. recvtype);
  641. kfree_skb(skb);
  642. break;
  643. }
  644. setup_for_next:
  645. if_usb_submit_rx_urb(cardp);
  646. rx_exit:
  647. lbs_deb_leave(LBS_DEB_USB);
  648. }
  649. /**
  650. * @brief This function downloads data to FW
  651. * @param priv pointer to struct lbs_private structure
  652. * @param type type of data
  653. * @param buf pointer to data buffer
  654. * @param len number of bytes
  655. * @return 0 or -1
  656. */
  657. static int if_usb_host_to_card(struct lbs_private *priv, uint8_t type,
  658. uint8_t *payload, uint16_t nb)
  659. {
  660. struct if_usb_card *cardp = priv->card;
  661. lbs_deb_usbd(&cardp->udev->dev,"*** type = %u\n", type);
  662. lbs_deb_usbd(&cardp->udev->dev,"size after = %d\n", nb);
  663. if (type == MVMS_CMD) {
  664. *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_REQUEST);
  665. priv->dnld_sent = DNLD_CMD_SENT;
  666. } else {
  667. *(__le32 *)cardp->ep_out_buf = cpu_to_le32(CMD_TYPE_DATA);
  668. priv->dnld_sent = DNLD_DATA_SENT;
  669. }
  670. memcpy((cardp->ep_out_buf + MESSAGE_HEADER_LEN), payload, nb);
  671. return usb_tx_block(cardp, cardp->ep_out_buf, nb + MESSAGE_HEADER_LEN);
  672. }
  673. /**
  674. * @brief This function issues Boot command to the Boot2 code
  675. * @param ivalue 1:Boot from FW by USB-Download
  676. * 2:Boot from FW in EEPROM
  677. * @return 0
  678. */
  679. static int if_usb_issue_boot_command(struct if_usb_card *cardp, int ivalue)
  680. {
  681. struct bootcmd *bootcmd = cardp->ep_out_buf;
  682. /* Prepare command */
  683. bootcmd->magic = cpu_to_le32(BOOT_CMD_MAGIC_NUMBER);
  684. bootcmd->cmd = ivalue;
  685. memset(bootcmd->pad, 0, sizeof(bootcmd->pad));
  686. /* Issue command */
  687. usb_tx_block(cardp, cardp->ep_out_buf, sizeof(*bootcmd));
  688. return 0;
  689. }
  690. /**
  691. * @brief This function checks the validity of Boot2/FW image.
  692. *
  693. * @param data pointer to image
  694. * len image length
  695. * @return 0 or -1
  696. */
  697. static int check_fwfile_format(const uint8_t *data, uint32_t totlen)
  698. {
  699. uint32_t bincmd, exit;
  700. uint32_t blksize, offset, len;
  701. int ret;
  702. ret = 1;
  703. exit = len = 0;
  704. do {
  705. struct fwheader *fwh = (void *)data;
  706. bincmd = le32_to_cpu(fwh->dnldcmd);
  707. blksize = le32_to_cpu(fwh->datalength);
  708. switch (bincmd) {
  709. case FW_HAS_DATA_TO_RECV:
  710. offset = sizeof(struct fwheader) + blksize;
  711. data += offset;
  712. len += offset;
  713. if (len >= totlen)
  714. exit = 1;
  715. break;
  716. case FW_HAS_LAST_BLOCK:
  717. exit = 1;
  718. ret = 0;
  719. break;
  720. default:
  721. exit = 1;
  722. break;
  723. }
  724. } while (!exit);
  725. if (ret)
  726. lbs_pr_err("firmware file format check FAIL\n");
  727. else
  728. lbs_deb_fw("firmware file format check PASS\n");
  729. return ret;
  730. }
  731. /**
  732. * @brief This function programs the firmware subject to cmd
  733. *
  734. * @param cardp the if_usb_card descriptor
  735. * fwname firmware or boot2 image file name
  736. * cmd either BOOT_CMD_FW_BY_USB, BOOT_CMD_UPDATE_FW,
  737. * or BOOT_CMD_UPDATE_BOOT2.
  738. * @return 0 or error code
  739. */
  740. static int if_usb_prog_firmware(struct if_usb_card *cardp,
  741. const char *fwname, int cmd)
  742. {
  743. struct lbs_private *priv = cardp->priv;
  744. unsigned long flags, caps;
  745. int ret;
  746. caps = priv->fwcapinfo;
  747. if (((cmd == BOOT_CMD_UPDATE_FW) && !(caps & FW_CAPINFO_FIRMWARE_UPGRADE)) ||
  748. ((cmd == BOOT_CMD_UPDATE_BOOT2) && !(caps & FW_CAPINFO_BOOT2_UPGRADE)))
  749. return -EOPNOTSUPP;
  750. /* Ensure main thread is idle. */
  751. spin_lock_irqsave(&priv->driver_lock, flags);
  752. while (priv->cur_cmd != NULL || priv->dnld_sent != DNLD_RES_RECEIVED) {
  753. spin_unlock_irqrestore(&priv->driver_lock, flags);
  754. if (wait_event_interruptible(priv->waitq,
  755. (priv->cur_cmd == NULL &&
  756. priv->dnld_sent == DNLD_RES_RECEIVED))) {
  757. return -ERESTARTSYS;
  758. }
  759. spin_lock_irqsave(&priv->driver_lock, flags);
  760. }
  761. priv->dnld_sent = DNLD_BOOTCMD_SENT;
  762. spin_unlock_irqrestore(&priv->driver_lock, flags);
  763. ret = __if_usb_prog_firmware(cardp, fwname, cmd);
  764. spin_lock_irqsave(&priv->driver_lock, flags);
  765. priv->dnld_sent = DNLD_RES_RECEIVED;
  766. spin_unlock_irqrestore(&priv->driver_lock, flags);
  767. wake_up_interruptible(&priv->waitq);
  768. return ret;
  769. }
  770. /* table of firmware file names */
  771. static const struct {
  772. u32 model;
  773. const char *fwname;
  774. } fw_table[] = {
  775. { MODEL_8388, "libertas/usb8388_v9.bin" },
  776. { MODEL_8388, "libertas/usb8388_v5.bin" },
  777. { MODEL_8388, "libertas/usb8388.bin" },
  778. { MODEL_8388, "usb8388.bin" },
  779. { MODEL_8682, "libertas/usb8682.bin" }
  780. };
  781. static int get_fw(struct if_usb_card *cardp, const char *fwname)
  782. {
  783. int i;
  784. /* Try user-specified firmware first */
  785. if (fwname)
  786. return request_firmware(&cardp->fw, fwname, &cardp->udev->dev);
  787. /* Otherwise search for firmware to use */
  788. for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
  789. if (fw_table[i].model != cardp->model)
  790. continue;
  791. if (request_firmware(&cardp->fw, fw_table[i].fwname,
  792. &cardp->udev->dev) == 0)
  793. return 0;
  794. }
  795. return -ENOENT;
  796. }
  797. static int __if_usb_prog_firmware(struct if_usb_card *cardp,
  798. const char *fwname, int cmd)
  799. {
  800. int i = 0;
  801. static int reset_count = 10;
  802. int ret = 0;
  803. lbs_deb_enter(LBS_DEB_USB);
  804. ret = get_fw(cardp, fwname);
  805. if (ret) {
  806. lbs_pr_err("failed to find firmware (%d)\n", ret);
  807. goto done;
  808. }
  809. if (check_fwfile_format(cardp->fw->data, cardp->fw->size)) {
  810. ret = -EINVAL;
  811. goto release_fw;
  812. }
  813. /* Cancel any pending usb business */
  814. usb_kill_urb(cardp->rx_urb);
  815. usb_kill_urb(cardp->tx_urb);
  816. cardp->fwlastblksent = 0;
  817. cardp->fwdnldover = 0;
  818. cardp->totalbytes = 0;
  819. cardp->fwfinalblk = 0;
  820. cardp->bootcmdresp = 0;
  821. restart:
  822. if (if_usb_submit_rx_urb_fwload(cardp) < 0) {
  823. lbs_deb_usbd(&cardp->udev->dev, "URB submission is failed\n");
  824. ret = -EIO;
  825. goto release_fw;
  826. }
  827. cardp->bootcmdresp = 0;
  828. do {
  829. int j = 0;
  830. i++;
  831. if_usb_issue_boot_command(cardp, cmd);
  832. /* wait for command response */
  833. do {
  834. j++;
  835. msleep_interruptible(100);
  836. } while (cardp->bootcmdresp == 0 && j < 10);
  837. } while (cardp->bootcmdresp == 0 && i < 5);
  838. if (cardp->bootcmdresp == BOOT_CMD_RESP_NOT_SUPPORTED) {
  839. /* Return to normal operation */
  840. ret = -EOPNOTSUPP;
  841. usb_kill_urb(cardp->rx_urb);
  842. usb_kill_urb(cardp->tx_urb);
  843. if (if_usb_submit_rx_urb(cardp) < 0)
  844. ret = -EIO;
  845. goto release_fw;
  846. } else if (cardp->bootcmdresp <= 0) {
  847. if (--reset_count >= 0) {
  848. if_usb_reset_device(cardp);
  849. goto restart;
  850. }
  851. ret = -EIO;
  852. goto release_fw;
  853. }
  854. i = 0;
  855. cardp->totalbytes = 0;
  856. cardp->fwlastblksent = 0;
  857. cardp->CRC_OK = 1;
  858. cardp->fwdnldover = 0;
  859. cardp->fwseqnum = -1;
  860. cardp->totalbytes = 0;
  861. cardp->fwfinalblk = 0;
  862. /* Send the first firmware packet... */
  863. if_usb_send_fw_pkt(cardp);
  864. /* ... and wait for the process to complete */
  865. wait_event_interruptible(cardp->fw_wq, cardp->surprise_removed || cardp->fwdnldover);
  866. del_timer_sync(&cardp->fw_timeout);
  867. usb_kill_urb(cardp->rx_urb);
  868. if (!cardp->fwdnldover) {
  869. lbs_pr_info("failed to load fw, resetting device!\n");
  870. if (--reset_count >= 0) {
  871. if_usb_reset_device(cardp);
  872. goto restart;
  873. }
  874. lbs_pr_info("FW download failure, time = %d ms\n", i * 100);
  875. ret = -EIO;
  876. goto release_fw;
  877. }
  878. release_fw:
  879. release_firmware(cardp->fw);
  880. cardp->fw = NULL;
  881. done:
  882. lbs_deb_leave_args(LBS_DEB_USB, "ret %d", ret);
  883. return ret;
  884. }
  885. #ifdef CONFIG_PM
  886. static int if_usb_suspend(struct usb_interface *intf, pm_message_t message)
  887. {
  888. struct if_usb_card *cardp = usb_get_intfdata(intf);
  889. struct lbs_private *priv = cardp->priv;
  890. int ret;
  891. lbs_deb_enter(LBS_DEB_USB);
  892. if (priv->psstate != PS_STATE_FULL_POWER)
  893. return -1;
  894. if (priv->wol_criteria == EHS_REMOVE_WAKEUP) {
  895. lbs_pr_info("Suspend attempt without "
  896. "configuring wake params!\n");
  897. return -ENOSYS;
  898. }
  899. ret = lbs_suspend(priv);
  900. if (ret)
  901. goto out;
  902. /* Unlink tx & rx urb */
  903. usb_kill_urb(cardp->tx_urb);
  904. usb_kill_urb(cardp->rx_urb);
  905. out:
  906. lbs_deb_leave(LBS_DEB_USB);
  907. return ret;
  908. }
  909. static int if_usb_resume(struct usb_interface *intf)
  910. {
  911. struct if_usb_card *cardp = usb_get_intfdata(intf);
  912. struct lbs_private *priv = cardp->priv;
  913. lbs_deb_enter(LBS_DEB_USB);
  914. if_usb_submit_rx_urb(cardp);
  915. lbs_resume(priv);
  916. lbs_deb_leave(LBS_DEB_USB);
  917. return 0;
  918. }
  919. #else
  920. #define if_usb_suspend NULL
  921. #define if_usb_resume NULL
  922. #endif
  923. static struct usb_driver if_usb_driver = {
  924. .name = DRV_NAME,
  925. .probe = if_usb_probe,
  926. .disconnect = if_usb_disconnect,
  927. .id_table = if_usb_table,
  928. .suspend = if_usb_suspend,
  929. .resume = if_usb_resume,
  930. .reset_resume = if_usb_resume,
  931. };
  932. static int __init if_usb_init_module(void)
  933. {
  934. int ret = 0;
  935. lbs_deb_enter(LBS_DEB_MAIN);
  936. ret = usb_register(&if_usb_driver);
  937. lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
  938. return ret;
  939. }
  940. static void __exit if_usb_exit_module(void)
  941. {
  942. lbs_deb_enter(LBS_DEB_MAIN);
  943. usb_deregister(&if_usb_driver);
  944. lbs_deb_leave(LBS_DEB_MAIN);
  945. }
  946. module_init(if_usb_init_module);
  947. module_exit(if_usb_exit_module);
  948. MODULE_DESCRIPTION("8388 USB WLAN Driver");
  949. MODULE_AUTHOR("Marvell International Ltd. and Red Hat, Inc.");
  950. MODULE_LICENSE("GPL");