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