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