if_usb.c 30 KB

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