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 <linux/slab.h>
12 #include "libertas_tf.h"
14 static const struct channel_range channel_ranges
[] = {
15 { LBTF_REGDOMAIN_US
, 1, 12 },
16 { LBTF_REGDOMAIN_CA
, 1, 12 },
17 { LBTF_REGDOMAIN_EU
, 1, 14 },
18 { LBTF_REGDOMAIN_JP
, 1, 14 },
19 { LBTF_REGDOMAIN_SP
, 1, 14 },
20 { LBTF_REGDOMAIN_FR
, 1, 14 },
23 static u16 lbtf_region_code_to_index
[MRVDRV_MAX_REGION_CODE
] =
25 LBTF_REGDOMAIN_US
, LBTF_REGDOMAIN_CA
, LBTF_REGDOMAIN_EU
,
26 LBTF_REGDOMAIN_SP
, LBTF_REGDOMAIN_FR
, LBTF_REGDOMAIN_JP
,
29 static struct cmd_ctrl_node
*lbtf_get_cmd_ctrl_node(struct lbtf_private
*priv
);
33 * lbtf_cmd_copyback - Simple callback that copies response back into command
35 * @priv A pointer to struct lbtf_private structure
36 * @extra A pointer to the original command structure for which
37 * 'resp' is a response
38 * @resp A pointer to the command response
40 * Returns: 0 on success, error on failure
42 int lbtf_cmd_copyback(struct lbtf_private
*priv
, unsigned long extra
,
43 struct cmd_header
*resp
)
45 struct cmd_header
*buf
= (void *)extra
;
48 copy_len
= min(le16_to_cpu(buf
->size
), le16_to_cpu(resp
->size
));
49 memcpy(buf
, resp
, copy_len
);
52 EXPORT_SYMBOL_GPL(lbtf_cmd_copyback
);
54 #define CHAN_TO_IDX(chan) ((chan) - 1)
56 static void lbtf_geo_init(struct lbtf_private
*priv
)
58 const struct channel_range
*range
= channel_ranges
;
62 for (i
= 0; i
< ARRAY_SIZE(channel_ranges
); i
++)
63 if (channel_ranges
[i
].regdomain
== priv
->regioncode
) {
64 range
= &channel_ranges
[i
];
68 for (ch
= priv
->range
.start
; ch
< priv
->range
.end
; ch
++)
69 priv
->channels
[CHAN_TO_IDX(ch
)].flags
= 0;
73 * lbtf_update_hw_spec: Updates the hardware details.
75 * @priv A pointer to struct lbtf_private structure
77 * Returns: 0 on success, error on failure
79 int lbtf_update_hw_spec(struct lbtf_private
*priv
)
81 struct cmd_ds_get_hw_spec cmd
;
85 memset(&cmd
, 0, sizeof(cmd
));
86 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
87 memcpy(cmd
.permanentaddr
, priv
->current_addr
, ETH_ALEN
);
88 ret
= lbtf_cmd_with_response(priv
, CMD_GET_HW_SPEC
, &cmd
);
92 priv
->fwcapinfo
= le32_to_cpu(cmd
.fwcapinfo
);
94 /* The firmware release is in an interesting format: the patch
95 * level is in the most significant nibble ... so fix that: */
96 priv
->fwrelease
= le32_to_cpu(cmd
.fwrelease
);
97 priv
->fwrelease
= (priv
->fwrelease
<< 8) |
98 (priv
->fwrelease
>> 24 & 0xff);
100 printk(KERN_INFO
"libertastf: %pM, fw %u.%u.%up%u, cap 0x%08x\n",
102 priv
->fwrelease
>> 24 & 0xff,
103 priv
->fwrelease
>> 16 & 0xff,
104 priv
->fwrelease
>> 8 & 0xff,
105 priv
->fwrelease
& 0xff,
108 /* Clamp region code to 8-bit since FW spec indicates that it should
109 * only ever be 8-bit, even though the field size is 16-bit. Some
110 * firmware returns non-zero high 8 bits here.
112 priv
->regioncode
= le16_to_cpu(cmd
.regioncode
) & 0xFF;
114 for (i
= 0; i
< MRVDRV_MAX_REGION_CODE
; i
++) {
115 /* use the region code to search for the index */
116 if (priv
->regioncode
== lbtf_region_code_to_index
[i
])
120 /* if it's unidentified region code, use the default (USA) */
121 if (i
>= MRVDRV_MAX_REGION_CODE
)
122 priv
->regioncode
= 0x10;
124 if (priv
->current_addr
[0] == 0xff)
125 memmove(priv
->current_addr
, cmd
.permanentaddr
, ETH_ALEN
);
127 SET_IEEE80211_PERM_ADDR(priv
->hw
, priv
->current_addr
);
135 * lbtf_set_channel: Set the radio channel
137 * @priv A pointer to struct lbtf_private structure
138 * @channel The desired channel, or 0 to clear a locked channel
140 * Returns: 0 on success, error on failure
142 int lbtf_set_channel(struct lbtf_private
*priv
, u8 channel
)
144 struct cmd_ds_802_11_rf_channel cmd
;
146 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
147 cmd
.action
= cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET
);
148 cmd
.channel
= cpu_to_le16(channel
);
150 return lbtf_cmd_with_response(priv
, CMD_802_11_RF_CHANNEL
, &cmd
);
153 int lbtf_beacon_set(struct lbtf_private
*priv
, struct sk_buff
*beacon
)
155 struct cmd_ds_802_11_beacon_set cmd
;
158 if (beacon
->len
> MRVL_MAX_BCN_SIZE
)
160 size
= sizeof(cmd
) - sizeof(cmd
.beacon
) + beacon
->len
;
161 cmd
.hdr
.size
= cpu_to_le16(size
);
162 cmd
.len
= cpu_to_le16(beacon
->len
);
163 memcpy(cmd
.beacon
, (u8
*) beacon
->data
, beacon
->len
);
165 lbtf_cmd_async(priv
, CMD_802_11_BEACON_SET
, &cmd
.hdr
, size
);
169 int lbtf_beacon_ctrl(struct lbtf_private
*priv
, bool beacon_enable
,
171 struct cmd_ds_802_11_beacon_control cmd
;
173 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
174 cmd
.action
= cpu_to_le16(CMD_ACT_SET
);
175 cmd
.beacon_enable
= cpu_to_le16(beacon_enable
);
176 cmd
.beacon_period
= cpu_to_le16(beacon_int
);
178 lbtf_cmd_async(priv
, CMD_802_11_BEACON_CTRL
, &cmd
.hdr
, sizeof(cmd
));
182 static void lbtf_queue_cmd(struct lbtf_private
*priv
,
183 struct cmd_ctrl_node
*cmdnode
)
190 if (!cmdnode
->cmdbuf
->size
)
194 spin_lock_irqsave(&priv
->driver_lock
, flags
);
195 list_add_tail(&cmdnode
->list
, &priv
->cmdpendingq
);
196 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
199 static void lbtf_submit_command(struct lbtf_private
*priv
,
200 struct cmd_ctrl_node
*cmdnode
)
203 struct cmd_header
*cmd
;
209 cmd
= cmdnode
->cmdbuf
;
211 spin_lock_irqsave(&priv
->driver_lock
, flags
);
212 priv
->cur_cmd
= cmdnode
;
213 cmdsize
= le16_to_cpu(cmd
->size
);
214 command
= le16_to_cpu(cmd
->command
);
215 ret
= priv
->hw_host_to_card(priv
, MVMS_CMD
, (u8
*) cmd
, cmdsize
);
216 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
219 /* Let the timer kick in and retry, and potentially reset
220 the whole thing if the condition persists */
223 /* Setup the timer after transmit command */
224 mod_timer(&priv
->command_timer
, jiffies
+ timeo
);
228 * This function inserts command node to cmdfreeq
229 * after cleans it. Requires priv->driver_lock held.
231 static void __lbtf_cleanup_and_insert_cmd(struct lbtf_private
*priv
,
232 struct cmd_ctrl_node
*cmdnode
)
237 cmdnode
->callback
= NULL
;
238 cmdnode
->callback_arg
= 0;
240 memset(cmdnode
->cmdbuf
, 0, LBS_CMD_BUFFER_SIZE
);
242 list_add_tail(&cmdnode
->list
, &priv
->cmdfreeq
);
245 static void lbtf_cleanup_and_insert_cmd(struct lbtf_private
*priv
,
246 struct cmd_ctrl_node
*ptempcmd
)
250 spin_lock_irqsave(&priv
->driver_lock
, flags
);
251 __lbtf_cleanup_and_insert_cmd(priv
, ptempcmd
);
252 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
255 void lbtf_complete_command(struct lbtf_private
*priv
, struct cmd_ctrl_node
*cmd
,
258 cmd
->result
= result
;
259 cmd
->cmdwaitqwoken
= 1;
260 wake_up_interruptible(&cmd
->cmdwait_q
);
263 __lbtf_cleanup_and_insert_cmd(priv
, cmd
);
264 priv
->cur_cmd
= NULL
;
267 int lbtf_cmd_set_mac_multicast_addr(struct lbtf_private
*priv
)
269 struct cmd_ds_mac_multicast_addr cmd
;
271 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
272 cmd
.action
= cpu_to_le16(CMD_ACT_SET
);
274 cmd
.nr_of_adrs
= cpu_to_le16((u16
) priv
->nr_of_multicastmacaddr
);
275 memcpy(cmd
.maclist
, priv
->multicastlist
,
276 priv
->nr_of_multicastmacaddr
* ETH_ALEN
);
278 lbtf_cmd_async(priv
, CMD_MAC_MULTICAST_ADR
, &cmd
.hdr
, sizeof(cmd
));
282 void lbtf_set_mode(struct lbtf_private
*priv
, enum lbtf_mode mode
)
284 struct cmd_ds_set_mode cmd
;
286 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
287 cmd
.mode
= cpu_to_le16(mode
);
288 lbtf_cmd_async(priv
, CMD_802_11_SET_MODE
, &cmd
.hdr
, sizeof(cmd
));
291 void lbtf_set_bssid(struct lbtf_private
*priv
, bool activate
, const u8
*bssid
)
293 struct cmd_ds_set_bssid cmd
;
295 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
296 cmd
.activate
= activate
? 1 : 0;
298 memcpy(cmd
.bssid
, bssid
, ETH_ALEN
);
300 lbtf_cmd_async(priv
, CMD_802_11_SET_BSSID
, &cmd
.hdr
, sizeof(cmd
));
303 int lbtf_set_mac_address(struct lbtf_private
*priv
, uint8_t *mac_addr
)
305 struct cmd_ds_802_11_mac_address cmd
;
307 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
308 cmd
.action
= cpu_to_le16(CMD_ACT_SET
);
310 memcpy(cmd
.macadd
, mac_addr
, ETH_ALEN
);
312 lbtf_cmd_async(priv
, CMD_802_11_MAC_ADDRESS
, &cmd
.hdr
, sizeof(cmd
));
316 int lbtf_set_radio_control(struct lbtf_private
*priv
)
319 struct cmd_ds_802_11_radio_control cmd
;
321 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
322 cmd
.action
= cpu_to_le16(CMD_ACT_SET
);
324 switch (priv
->preamble
) {
325 case CMD_TYPE_SHORT_PREAMBLE
:
326 cmd
.control
= cpu_to_le16(SET_SHORT_PREAMBLE
);
329 case CMD_TYPE_LONG_PREAMBLE
:
330 cmd
.control
= cpu_to_le16(SET_LONG_PREAMBLE
);
333 case CMD_TYPE_AUTO_PREAMBLE
:
335 cmd
.control
= cpu_to_le16(SET_AUTO_PREAMBLE
);
340 cmd
.control
|= cpu_to_le16(TURN_ON_RF
);
342 cmd
.control
&= cpu_to_le16(~TURN_ON_RF
);
344 ret
= lbtf_cmd_with_response(priv
, CMD_802_11_RADIO_CONTROL
, &cmd
);
348 void lbtf_set_mac_control(struct lbtf_private
*priv
)
350 struct cmd_ds_mac_control cmd
;
351 cmd
.hdr
.size
= cpu_to_le16(sizeof(cmd
));
352 cmd
.action
= cpu_to_le16(priv
->mac_control
);
355 lbtf_cmd_async(priv
, CMD_MAC_CONTROL
,
356 &cmd
.hdr
, sizeof(cmd
));
360 * lbtf_allocate_cmd_buffer - Allocates cmd buffer, links it to free cmd queue
362 * @priv A pointer to struct lbtf_private structure
364 * Returns: 0 on success.
366 int lbtf_allocate_cmd_buffer(struct lbtf_private
*priv
)
370 struct cmd_ctrl_node
*cmdarray
;
372 /* Allocate and initialize the command array */
373 bufsize
= sizeof(struct cmd_ctrl_node
) * LBS_NUM_CMD_BUFFERS
;
374 cmdarray
= kzalloc(bufsize
, GFP_KERNEL
);
377 priv
->cmd_array
= cmdarray
;
379 /* Allocate and initialize each command buffer in the command array */
380 for (i
= 0; i
< LBS_NUM_CMD_BUFFERS
; i
++) {
381 cmdarray
[i
].cmdbuf
= kzalloc(LBS_CMD_BUFFER_SIZE
, GFP_KERNEL
);
382 if (!cmdarray
[i
].cmdbuf
)
386 for (i
= 0; i
< LBS_NUM_CMD_BUFFERS
; i
++) {
387 init_waitqueue_head(&cmdarray
[i
].cmdwait_q
);
388 lbtf_cleanup_and_insert_cmd(priv
, &cmdarray
[i
]);
394 * lbtf_free_cmd_buffer - Frees the cmd buffer.
396 * @priv A pointer to struct lbtf_private structure
400 int lbtf_free_cmd_buffer(struct lbtf_private
*priv
)
402 struct cmd_ctrl_node
*cmdarray
;
405 /* need to check if cmd array is allocated or not */
406 if (priv
->cmd_array
== NULL
)
409 cmdarray
= priv
->cmd_array
;
411 /* Release shared memory buffers */
412 for (i
= 0; i
< LBS_NUM_CMD_BUFFERS
; i
++) {
413 kfree(cmdarray
[i
].cmdbuf
);
414 cmdarray
[i
].cmdbuf
= NULL
;
417 /* Release cmd_ctrl_node */
418 kfree(priv
->cmd_array
);
419 priv
->cmd_array
= NULL
;
425 * lbtf_get_cmd_ctrl_node - Gets free cmd node from free cmd queue.
427 * @priv A pointer to struct lbtf_private structure
429 * Returns: pointer to a struct cmd_ctrl_node or NULL if none available.
431 static struct cmd_ctrl_node
*lbtf_get_cmd_ctrl_node(struct lbtf_private
*priv
)
433 struct cmd_ctrl_node
*tempnode
;
439 spin_lock_irqsave(&priv
->driver_lock
, flags
);
441 if (!list_empty(&priv
->cmdfreeq
)) {
442 tempnode
= list_first_entry(&priv
->cmdfreeq
,
443 struct cmd_ctrl_node
, list
);
444 list_del(&tempnode
->list
);
448 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
454 * lbtf_execute_next_command: execute next command in cmd pending queue.
456 * @priv A pointer to struct lbtf_private structure
458 * Returns: 0 on success.
460 int lbtf_execute_next_command(struct lbtf_private
*priv
)
462 struct cmd_ctrl_node
*cmdnode
= NULL
;
463 struct cmd_header
*cmd
;
466 /* Debug group is LBS_DEB_THREAD and not LBS_DEB_HOST, because the
467 * only caller to us is lbtf_thread() and we get even when a
468 * data packet is received */
470 spin_lock_irqsave(&priv
->driver_lock
, flags
);
473 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
477 if (!list_empty(&priv
->cmdpendingq
)) {
478 cmdnode
= list_first_entry(&priv
->cmdpendingq
,
479 struct cmd_ctrl_node
, list
);
483 cmd
= cmdnode
->cmdbuf
;
485 list_del(&cmdnode
->list
);
486 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
487 lbtf_submit_command(priv
, cmdnode
);
489 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
493 static struct cmd_ctrl_node
*__lbtf_cmd_async(struct lbtf_private
*priv
,
494 uint16_t command
, struct cmd_header
*in_cmd
, int in_cmd_size
,
495 int (*callback
)(struct lbtf_private
*, unsigned long,
496 struct cmd_header
*),
497 unsigned long callback_arg
)
499 struct cmd_ctrl_node
*cmdnode
;
501 if (priv
->surpriseremoved
)
502 return ERR_PTR(-ENOENT
);
504 cmdnode
= lbtf_get_cmd_ctrl_node(priv
);
505 if (cmdnode
== NULL
) {
506 /* Wake up main thread to execute next command */
507 queue_work(lbtf_wq
, &priv
->cmd_work
);
508 return ERR_PTR(-ENOBUFS
);
511 cmdnode
->callback
= callback
;
512 cmdnode
->callback_arg
= callback_arg
;
514 /* Copy the incoming command to the buffer */
515 memcpy(cmdnode
->cmdbuf
, in_cmd
, in_cmd_size
);
517 /* Set sequence number, clean result, move to buffer */
519 cmdnode
->cmdbuf
->command
= cpu_to_le16(command
);
520 cmdnode
->cmdbuf
->size
= cpu_to_le16(in_cmd_size
);
521 cmdnode
->cmdbuf
->seqnum
= cpu_to_le16(priv
->seqnum
);
522 cmdnode
->cmdbuf
->result
= 0;
523 cmdnode
->cmdwaitqwoken
= 0;
524 lbtf_queue_cmd(priv
, cmdnode
);
525 queue_work(lbtf_wq
, &priv
->cmd_work
);
530 void lbtf_cmd_async(struct lbtf_private
*priv
, uint16_t command
,
531 struct cmd_header
*in_cmd
, int in_cmd_size
)
533 __lbtf_cmd_async(priv
, command
, in_cmd
, in_cmd_size
, NULL
, 0);
536 int __lbtf_cmd(struct lbtf_private
*priv
, uint16_t command
,
537 struct cmd_header
*in_cmd
, int in_cmd_size
,
538 int (*callback
)(struct lbtf_private
*,
539 unsigned long, struct cmd_header
*),
540 unsigned long callback_arg
)
542 struct cmd_ctrl_node
*cmdnode
;
546 cmdnode
= __lbtf_cmd_async(priv
, command
, in_cmd
, in_cmd_size
,
547 callback
, callback_arg
);
549 return PTR_ERR(cmdnode
);
552 ret
= wait_event_interruptible(cmdnode
->cmdwait_q
,
553 cmdnode
->cmdwaitqwoken
);
556 "libertastf: command 0x%04x interrupted by signal",
561 spin_lock_irqsave(&priv
->driver_lock
, flags
);
562 ret
= cmdnode
->result
;
564 printk(KERN_DEBUG
"libertastf: command 0x%04x failed: %d\n",
567 __lbtf_cleanup_and_insert_cmd(priv
, cmdnode
);
568 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
572 EXPORT_SYMBOL_GPL(__lbtf_cmd
);
574 /* Call holding driver_lock */
575 void lbtf_cmd_response_rx(struct lbtf_private
*priv
)
577 priv
->cmd_response_rxed
= 1;
578 queue_work(lbtf_wq
, &priv
->cmd_work
);
580 EXPORT_SYMBOL_GPL(lbtf_cmd_response_rx
);
582 int lbtf_process_rx_command(struct lbtf_private
*priv
)
584 uint16_t respcmd
, curcmd
;
585 struct cmd_header
*resp
;
590 mutex_lock(&priv
->lock
);
591 spin_lock_irqsave(&priv
->driver_lock
, flags
);
593 if (!priv
->cur_cmd
) {
595 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
599 resp
= (void *)priv
->cmd_resp_buff
;
600 curcmd
= le16_to_cpu(priv
->cur_cmd
->cmdbuf
->command
);
601 respcmd
= le16_to_cpu(resp
->command
);
602 result
= le16_to_cpu(resp
->result
);
605 printk(KERN_DEBUG
"libertastf: cmd response 0x%04x, seq %d, size %d\n",
606 respcmd
, le16_to_cpu(resp
->seqnum
),
607 le16_to_cpu(resp
->size
));
609 if (resp
->seqnum
!= priv
->cur_cmd
->cmdbuf
->seqnum
) {
610 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
614 if (respcmd
!= CMD_RET(curcmd
)) {
615 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
620 if (resp
->result
== cpu_to_le16(0x0004)) {
621 /* 0x0004 means -EAGAIN. Drop the response, let it time out
622 and be resubmitted */
623 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
628 /* Now we got response from FW, cancel the command timer */
629 del_timer(&priv
->command_timer
);
630 priv
->cmd_timed_out
= 0;
631 if (priv
->nr_retries
)
632 priv
->nr_retries
= 0;
634 /* If the command is not successful, cleanup and return failure */
635 if ((result
!= 0 || !(respcmd
& 0x8000))) {
637 * Handling errors here
640 case CMD_RET(CMD_GET_HW_SPEC
):
641 case CMD_RET(CMD_802_11_RESET
):
642 printk(KERN_DEBUG
"libertastf: reset failed\n");
646 lbtf_complete_command(priv
, priv
->cur_cmd
, result
);
647 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
653 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
655 if (priv
->cur_cmd
&& priv
->cur_cmd
->callback
) {
656 ret
= priv
->cur_cmd
->callback(priv
, priv
->cur_cmd
->callback_arg
,
659 spin_lock_irqsave(&priv
->driver_lock
, flags
);
662 /* Clean up and Put current command back to cmdfreeq */
663 lbtf_complete_command(priv
, priv
->cur_cmd
, result
);
665 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
668 mutex_unlock(&priv
->lock
);