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