if_usb.c 28 KB

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