2 * Copyright (C) 2008, cozybit Inc.
3 * Copyright (C) 2003-2006, Marvell International Ltd.
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.
10 #include "libertas_tf.h"
12 static const struct channel_range channel_ranges
[] = {
13 { LBTF_REGDOMAIN_US
, 1, 12 },
14 { LBTF_REGDOMAIN_CA
, 1, 12 },
15 { LBTF_REGDOMAIN_EU
, 1, 14 },
16 { LBTF_REGDOMAIN_JP
, 1, 14 },
17 { LBTF_REGDOMAIN_SP
, 1, 14 },
18 { LBTF_REGDOMAIN_FR
, 1, 14 },
21 static u16 lbtf_region_code_to_index
[MRVDRV_MAX_REGION_CODE
] =
23 LBTF_REGDOMAIN_US
, LBTF_REGDOMAIN_CA
, LBTF_REGDOMAIN_EU
,
24 LBTF_REGDOMAIN_SP
, LBTF_REGDOMAIN_FR
, LBTF_REGDOMAIN_JP
,
27 static struct cmd_ctrl_node
*lbtf_get_cmd_ctrl_node(struct lbtf_private
*priv
);
31 * lbtf_cmd_copyback - Simple callback that copies response back into command
33 * @priv A pointer to struct lbtf_private structure
34 * @extra A pointer to the original command structure for which
35 * 'resp' is a response
36 * @resp A pointer to the command response
38 * Returns: 0 on success, error on failure
40 int lbtf_cmd_copyback(struct lbtf_private
*priv
, unsigned long extra
,
41 struct cmd_header
*resp
)
43 struct cmd_header
*buf
= (void *)extra
;
46 copy_len
= min(le16_to_cpu(buf
->size
), le16_to_cpu(resp
->size
));
47 memcpy(buf
, resp
, copy_len
);
50 EXPORT_SYMBOL_GPL(lbtf_cmd_copyback
);
52 #define CHAN_TO_IDX(chan) ((chan) - 1)
54 static void lbtf_geo_init(struct lbtf_private
*priv
)
56 const struct channel_range
*range
= channel_ranges
;
60 for (i
= 0; i
< ARRAY_SIZE(channel_ranges
); i
++)
61 if (channel_ranges
[i
].regdomain
== priv
->regioncode
) {
62 range
= &channel_ranges
[i
];
66 for (ch
= priv
->range
.start
; ch
< priv
->range
.end
; ch
++)
67 priv
->channels
[CHAN_TO_IDX(ch
)].flags
= 0;
71 * lbtf_update_hw_spec: Updates the hardware details.
73 * @priv A pointer to struct lbtf_private structure
75 * Returns: 0 on success, error on failure
77 int lbtf_update_hw_spec(struct lbtf_private
*priv
)
79 struct cmd_ds_get_hw_spec cmd
;
84 memset(&cmd
, 0, sizeof(cmd
));
85 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
86 memcpy(cmd
.permanentaddr
, priv
->current_addr
, ETH_ALEN
);
87 ret
= lbtf_cmd_with_response(priv
, CMD_GET_HW_SPEC
, &cmd
);
91 priv
->fwcapinfo
= le32_to_cpu(cmd
.fwcapinfo
);
93 /* The firmware release is in an interesting format: the patch
94 * level is in the most significant nibble ... so fix that: */
95 priv
->fwrelease
= le32_to_cpu(cmd
.fwrelease
);
96 priv
->fwrelease
= (priv
->fwrelease
<< 8) |
97 (priv
->fwrelease
>> 24 & 0xff);
99 printk(KERN_INFO
"libertastf: %s, fw %u.%u.%up%u, cap 0x%08x\n",
100 print_mac(mac
, cmd
.permanentaddr
),
101 priv
->fwrelease
>> 24 & 0xff,
102 priv
->fwrelease
>> 16 & 0xff,
103 priv
->fwrelease
>> 8 & 0xff,
104 priv
->fwrelease
& 0xff,
107 /* Clamp region code to 8-bit since FW spec indicates that it should
108 * only ever be 8-bit, even though the field size is 16-bit. Some
109 * firmware returns non-zero high 8 bits here.
111 priv
->regioncode
= le16_to_cpu(cmd
.regioncode
) & 0xFF;
113 for (i
= 0; i
< MRVDRV_MAX_REGION_CODE
; i
++) {
114 /* use the region code to search for the index */
115 if (priv
->regioncode
== lbtf_region_code_to_index
[i
])
119 /* if it's unidentified region code, use the default (USA) */
120 if (i
>= MRVDRV_MAX_REGION_CODE
)
121 priv
->regioncode
= 0x10;
123 if (priv
->current_addr
[0] == 0xff)
124 memmove(priv
->current_addr
, cmd
.permanentaddr
, ETH_ALEN
);
126 SET_IEEE80211_PERM_ADDR(priv
->hw
, priv
->current_addr
);
134 * lbtf_set_channel: Set the radio channel
136 * @priv A pointer to struct lbtf_private structure
137 * @channel The desired channel, or 0 to clear a locked channel
139 * Returns: 0 on success, error on failure
141 int lbtf_set_channel(struct lbtf_private
*priv
, u8 channel
)
143 struct cmd_ds_802_11_rf_channel cmd
;
145 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
146 cmd
.action
= cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET
);
147 cmd
.channel
= cpu_to_le16(channel
);
149 return lbtf_cmd_with_response(priv
, CMD_802_11_RF_CHANNEL
, &cmd
);
152 int lbtf_beacon_set(struct lbtf_private
*priv
, struct sk_buff
*beacon
)
154 struct cmd_ds_802_11_beacon_set cmd
;
157 if (beacon
->len
> MRVL_MAX_BCN_SIZE
)
159 size
= sizeof(cmd
) - sizeof(cmd
.beacon
) + beacon
->len
;
160 cmd
.hdr
.size
= cpu_to_le16(size
);
161 cmd
.len
= cpu_to_le16(beacon
->len
);
162 memcpy(cmd
.beacon
, (u8
*) beacon
->data
, beacon
->len
);
164 lbtf_cmd_async(priv
, CMD_802_11_BEACON_SET
, &cmd
.hdr
, size
);
168 int lbtf_beacon_ctrl(struct lbtf_private
*priv
, bool beacon_enable
,
170 struct cmd_ds_802_11_beacon_control cmd
;
172 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
173 cmd
.action
= cpu_to_le16(CMD_ACT_SET
);
174 cmd
.beacon_enable
= cpu_to_le16(beacon_enable
);
175 cmd
.beacon_period
= cpu_to_le16(beacon_int
);
177 lbtf_cmd_async(priv
, CMD_802_11_BEACON_CTRL
, &cmd
.hdr
, sizeof(cmd
));
181 static void lbtf_queue_cmd(struct lbtf_private
*priv
,
182 struct cmd_ctrl_node
*cmdnode
)
189 if (!cmdnode
->cmdbuf
->size
)
193 spin_lock_irqsave(&priv
->driver_lock
, flags
);
194 list_add_tail(&cmdnode
->list
, &priv
->cmdpendingq
);
195 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
198 static void lbtf_submit_command(struct lbtf_private
*priv
,
199 struct cmd_ctrl_node
*cmdnode
)
202 struct cmd_header
*cmd
;
208 cmd
= cmdnode
->cmdbuf
;
210 spin_lock_irqsave(&priv
->driver_lock
, flags
);
211 priv
->cur_cmd
= cmdnode
;
212 cmdsize
= le16_to_cpu(cmd
->size
);
213 command
= le16_to_cpu(cmd
->command
);
214 ret
= priv
->hw_host_to_card(priv
, MVMS_CMD
, (u8
*) cmd
, cmdsize
);
215 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
218 /* Let the timer kick in and retry, and potentially reset
219 the whole thing if the condition persists */
222 /* Setup the timer after transmit command */
223 mod_timer(&priv
->command_timer
, jiffies
+ timeo
);
227 * This function inserts command node to cmdfreeq
228 * after cleans it. Requires priv->driver_lock held.
230 static void __lbtf_cleanup_and_insert_cmd(struct lbtf_private
*priv
,
231 struct cmd_ctrl_node
*cmdnode
)
236 cmdnode
->callback
= NULL
;
237 cmdnode
->callback_arg
= 0;
239 memset(cmdnode
->cmdbuf
, 0, LBS_CMD_BUFFER_SIZE
);
241 list_add_tail(&cmdnode
->list
, &priv
->cmdfreeq
);
244 static void lbtf_cleanup_and_insert_cmd(struct lbtf_private
*priv
,
245 struct cmd_ctrl_node
*ptempcmd
)
249 spin_lock_irqsave(&priv
->driver_lock
, flags
);
250 __lbtf_cleanup_and_insert_cmd(priv
, ptempcmd
);
251 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
254 void lbtf_complete_command(struct lbtf_private
*priv
, struct cmd_ctrl_node
*cmd
,
257 cmd
->result
= result
;
258 cmd
->cmdwaitqwoken
= 1;
259 wake_up_interruptible(&cmd
->cmdwait_q
);
262 __lbtf_cleanup_and_insert_cmd(priv
, cmd
);
263 priv
->cur_cmd
= NULL
;
266 int lbtf_cmd_set_mac_multicast_addr(struct lbtf_private
*priv
)
268 struct cmd_ds_mac_multicast_addr cmd
;
270 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
271 cmd
.action
= cpu_to_le16(CMD_ACT_SET
);
273 cmd
.nr_of_adrs
= cpu_to_le16((u16
) priv
->nr_of_multicastmacaddr
);
274 memcpy(cmd
.maclist
, priv
->multicastlist
,
275 priv
->nr_of_multicastmacaddr
* ETH_ALEN
);
277 lbtf_cmd_async(priv
, CMD_MAC_MULTICAST_ADR
, &cmd
.hdr
, sizeof(cmd
));
281 void lbtf_set_mode(struct lbtf_private
*priv
, enum lbtf_mode mode
)
283 struct cmd_ds_set_mode cmd
;
285 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
286 cmd
.mode
= cpu_to_le16(mode
);
287 lbtf_cmd_async(priv
, CMD_802_11_SET_MODE
, &cmd
.hdr
, sizeof(cmd
));
290 void lbtf_set_bssid(struct lbtf_private
*priv
, bool activate
, u8
*bssid
)
292 struct cmd_ds_set_bssid cmd
;
294 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
295 cmd
.activate
= activate
? 1 : 0;
297 memcpy(cmd
.bssid
, bssid
, ETH_ALEN
);
299 lbtf_cmd_async(priv
, CMD_802_11_SET_BSSID
, &cmd
.hdr
, sizeof(cmd
));
302 int lbtf_set_mac_address(struct lbtf_private
*priv
, uint8_t *mac_addr
)
304 struct cmd_ds_802_11_mac_address cmd
;
306 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
307 cmd
.action
= cpu_to_le16(CMD_ACT_SET
);
309 memcpy(cmd
.macadd
, mac_addr
, ETH_ALEN
);
311 lbtf_cmd_async(priv
, CMD_802_11_MAC_ADDRESS
, &cmd
.hdr
, sizeof(cmd
));
315 int lbtf_set_radio_control(struct lbtf_private
*priv
)
318 struct cmd_ds_802_11_radio_control cmd
;
320 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
321 cmd
.action
= cpu_to_le16(CMD_ACT_SET
);
323 switch (priv
->preamble
) {
324 case CMD_TYPE_SHORT_PREAMBLE
:
325 cmd
.control
= cpu_to_le16(SET_SHORT_PREAMBLE
);
328 case CMD_TYPE_LONG_PREAMBLE
:
329 cmd
.control
= cpu_to_le16(SET_LONG_PREAMBLE
);
332 case CMD_TYPE_AUTO_PREAMBLE
:
334 cmd
.control
= cpu_to_le16(SET_AUTO_PREAMBLE
);
339 cmd
.control
|= cpu_to_le16(TURN_ON_RF
);
341 cmd
.control
&= cpu_to_le16(~TURN_ON_RF
);
343 ret
= lbtf_cmd_with_response(priv
, CMD_802_11_RADIO_CONTROL
, &cmd
);
347 void lbtf_set_mac_control(struct lbtf_private
*priv
)
349 struct cmd_ds_mac_control cmd
;
350 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
351 cmd
.action
= cpu_to_le16(priv
->mac_control
);
354 lbtf_cmd_async(priv
, CMD_MAC_CONTROL
,
355 &cmd
.hdr
, sizeof(cmd
));
359 * lbtf_allocate_cmd_buffer - Allocates cmd buffer, links it to free cmd queue
361 * @priv A pointer to struct lbtf_private structure
363 * Returns: 0 on success.
365 int lbtf_allocate_cmd_buffer(struct lbtf_private
*priv
)
369 struct cmd_ctrl_node
*cmdarray
;
371 /* Allocate and initialize the command array */
372 bufsize
= sizeof(struct cmd_ctrl_node
) * LBS_NUM_CMD_BUFFERS
;
373 cmdarray
= kzalloc(bufsize
, GFP_KERNEL
);
376 priv
->cmd_array
= cmdarray
;
378 /* Allocate and initialize each command buffer in the command array */
379 for (i
= 0; i
< LBS_NUM_CMD_BUFFERS
; i
++) {
380 cmdarray
[i
].cmdbuf
= kzalloc(LBS_CMD_BUFFER_SIZE
, GFP_KERNEL
);
381 if (!cmdarray
[i
].cmdbuf
)
385 for (i
= 0; i
< LBS_NUM_CMD_BUFFERS
; i
++) {
386 init_waitqueue_head(&cmdarray
[i
].cmdwait_q
);
387 lbtf_cleanup_and_insert_cmd(priv
, &cmdarray
[i
]);
393 * lbtf_free_cmd_buffer - Frees the cmd buffer.
395 * @priv A pointer to struct lbtf_private structure
399 int lbtf_free_cmd_buffer(struct lbtf_private
*priv
)
401 struct cmd_ctrl_node
*cmdarray
;
404 /* need to check if cmd array is allocated or not */
405 if (priv
->cmd_array
== NULL
)
408 cmdarray
= priv
->cmd_array
;
410 /* Release shared memory buffers */
411 for (i
= 0; i
< LBS_NUM_CMD_BUFFERS
; i
++) {
412 kfree(cmdarray
[i
].cmdbuf
);
413 cmdarray
[i
].cmdbuf
= NULL
;
416 /* Release cmd_ctrl_node */
417 kfree(priv
->cmd_array
);
418 priv
->cmd_array
= NULL
;
424 * lbtf_get_cmd_ctrl_node - Gets free cmd node from free cmd queue.
426 * @priv A pointer to struct lbtf_private structure
428 * Returns: pointer to a struct cmd_ctrl_node or NULL if none available.
430 static struct cmd_ctrl_node
*lbtf_get_cmd_ctrl_node(struct lbtf_private
*priv
)
432 struct cmd_ctrl_node
*tempnode
;
438 spin_lock_irqsave(&priv
->driver_lock
, flags
);
440 if (!list_empty(&priv
->cmdfreeq
)) {
441 tempnode
= list_first_entry(&priv
->cmdfreeq
,
442 struct cmd_ctrl_node
, list
);
443 list_del(&tempnode
->list
);
447 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
453 * lbtf_execute_next_command: execute next command in cmd pending queue.
455 * @priv A pointer to struct lbtf_private structure
457 * Returns: 0 on success.
459 int lbtf_execute_next_command(struct lbtf_private
*priv
)
461 struct cmd_ctrl_node
*cmdnode
= NULL
;
462 struct cmd_header
*cmd
;
465 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
466 * only caller to us is lbtf_thread() and we get even when a
467 * data packet is received */
469 spin_lock_irqsave(&priv
->driver_lock
, flags
);
472 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
476 if (!list_empty(&priv
->cmdpendingq
)) {
477 cmdnode
= list_first_entry(&priv
->cmdpendingq
,
478 struct cmd_ctrl_node
, list
);
482 cmd
= cmdnode
->cmdbuf
;
484 list_del(&cmdnode
->list
);
485 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
486 lbtf_submit_command(priv
, cmdnode
);
488 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
492 static struct cmd_ctrl_node
*__lbtf_cmd_async(struct lbtf_private
*priv
,
493 uint16_t command
, struct cmd_header
*in_cmd
, int in_cmd_size
,
494 int (*callback
)(struct lbtf_private
*, unsigned long,
495 struct cmd_header
*),
496 unsigned long callback_arg
)
498 struct cmd_ctrl_node
*cmdnode
;
500 if (priv
->surpriseremoved
)
501 return ERR_PTR(-ENOENT
);
503 cmdnode
= lbtf_get_cmd_ctrl_node(priv
);
504 if (cmdnode
== NULL
) {
505 /* Wake up main thread to execute next command */
506 queue_work(lbtf_wq
, &priv
->cmd_work
);
507 return ERR_PTR(-ENOBUFS
);
510 cmdnode
->callback
= callback
;
511 cmdnode
->callback_arg
= callback_arg
;
513 /* Copy the incoming command to the buffer */
514 memcpy(cmdnode
->cmdbuf
, in_cmd
, in_cmd_size
);
516 /* Set sequence number, clean result, move to buffer */
518 cmdnode
->cmdbuf
->command
= cpu_to_le16(command
);
519 cmdnode
->cmdbuf
->size
= cpu_to_le16(in_cmd_size
);
520 cmdnode
->cmdbuf
->seqnum
= cpu_to_le16(priv
->seqnum
);
521 cmdnode
->cmdbuf
->result
= 0;
522 cmdnode
->cmdwaitqwoken
= 0;
523 lbtf_queue_cmd(priv
, cmdnode
);
524 queue_work(lbtf_wq
, &priv
->cmd_work
);
529 void lbtf_cmd_async(struct lbtf_private
*priv
, uint16_t command
,
530 struct cmd_header
*in_cmd
, int in_cmd_size
)
532 __lbtf_cmd_async(priv
, command
, in_cmd
, in_cmd_size
, NULL
, 0);
535 int __lbtf_cmd(struct lbtf_private
*priv
, uint16_t command
,
536 struct cmd_header
*in_cmd
, int in_cmd_size
,
537 int (*callback
)(struct lbtf_private
*,
538 unsigned long, struct cmd_header
*),
539 unsigned long callback_arg
)
541 struct cmd_ctrl_node
*cmdnode
;
545 cmdnode
= __lbtf_cmd_async(priv
, command
, in_cmd
, in_cmd_size
,
546 callback
, callback_arg
);
548 return PTR_ERR(cmdnode
);
551 ret
= wait_event_interruptible(cmdnode
->cmdwait_q
,
552 cmdnode
->cmdwaitqwoken
);
555 "libertastf: command 0x%04x interrupted by signal",
560 spin_lock_irqsave(&priv
->driver_lock
, flags
);
561 ret
= cmdnode
->result
;
563 printk(KERN_DEBUG
"libertastf: command 0x%04x failed: %d\n",
566 __lbtf_cleanup_and_insert_cmd(priv
, cmdnode
);
567 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
571 EXPORT_SYMBOL_GPL(__lbtf_cmd
);
573 /* Call holding driver_lock */
574 void lbtf_cmd_response_rx(struct lbtf_private
*priv
)
576 priv
->cmd_response_rxed
= 1;
577 queue_work(lbtf_wq
, &priv
->cmd_work
);
579 EXPORT_SYMBOL_GPL(lbtf_cmd_response_rx
);
581 int lbtf_process_rx_command(struct lbtf_private
*priv
)
583 uint16_t respcmd
, curcmd
;
584 struct cmd_header
*resp
;
589 mutex_lock(&priv
->lock
);
590 spin_lock_irqsave(&priv
->driver_lock
, flags
);
592 if (!priv
->cur_cmd
) {
594 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
598 resp
= (void *)priv
->cmd_resp_buff
;
599 curcmd
= le16_to_cpu(priv
->cur_cmd
->cmdbuf
->command
);
600 respcmd
= le16_to_cpu(resp
->command
);
601 result
= le16_to_cpu(resp
->result
);
604 printk(KERN_DEBUG
"libertastf: cmd response 0x%04x, seq %d, size %d\n",
605 respcmd
, le16_to_cpu(resp
->seqnum
),
606 le16_to_cpu(resp
->size
));
608 if (resp
->seqnum
!= priv
->cur_cmd
->cmdbuf
->seqnum
) {
609 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
613 if (respcmd
!= CMD_RET(curcmd
)) {
614 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
619 if (resp
->result
== cpu_to_le16(0x0004)) {
620 /* 0x0004 means -EAGAIN. Drop the response, let it time out
621 and be resubmitted */
622 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
627 /* Now we got response from FW, cancel the command timer */
628 del_timer(&priv
->command_timer
);
629 priv
->cmd_timed_out
= 0;
630 if (priv
->nr_retries
)
631 priv
->nr_retries
= 0;
633 /* If the command is not successful, cleanup and return failure */
634 if ((result
!= 0 || !(respcmd
& 0x8000))) {
636 * Handling errors here
639 case CMD_RET(CMD_GET_HW_SPEC
):
640 case CMD_RET(CMD_802_11_RESET
):
641 printk(KERN_DEBUG
"libertastf: reset failed\n");
645 lbtf_complete_command(priv
, priv
->cur_cmd
, result
);
646 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
652 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
654 if (priv
->cur_cmd
&& priv
->cur_cmd
->callback
) {
655 ret
= priv
->cur_cmd
->callback(priv
, priv
->cur_cmd
->callback_arg
,
658 spin_lock_irqsave(&priv
->driver_lock
, flags
);
661 /* Clean up and Put current command back to cmdfreeq */
662 lbtf_complete_command(priv
, priv
->cur_cmd
, result
);
664 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
667 mutex_unlock(&priv
->lock
);