if_usb.c 29 KB

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