2 * Copyright (c) 2012 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #include <linux/netdevice.h>
18 #include "brcmu_wifi.h"
19 #include "brcmu_utils.h"
23 #include "tracepoint.h"
29 * struct brcm_ethhdr - broadcom specific ether header.
31 * @subtype: subtype for this packet.
32 * @length: TODO: length of appended data.
33 * @version: version indication.
34 * @oui: OUI of this packet.
35 * @usr_subtype: subtype for this OUI.
45 struct brcmf_event_msg_be
{
54 char ifname
[IFNAMSIZ
];
60 * struct brcmf_event - contents of broadcom event packet.
62 * @eth: standard ether header.
63 * @hdr: broadcom specific ether header.
64 * @msg: common part of the actual event message.
68 struct brcm_ethhdr hdr
;
69 struct brcmf_event_msg_be msg
;
73 * struct brcmf_fweh_queue_item - event item on event queue.
75 * @q: list element for queuing.
77 * @ifidx: interface index related to this event.
78 * @ifaddr: ethernet address for interface.
79 * @emsg: common parameters of the firmware event message.
80 * @data: event specific data part of the firmware event.
82 struct brcmf_fweh_queue_item
{
84 enum brcmf_fweh_event_code code
;
87 struct brcmf_event_msg_be emsg
;
92 * struct brcmf_fweh_event_name - code, name mapping entry.
94 struct brcmf_fweh_event_name
{
95 enum brcmf_fweh_event_code code
;
100 #define BRCMF_ENUM_DEF(id, val) \
103 /* array for mapping code to event name */
104 static struct brcmf_fweh_event_name fweh_event_names
[] = {
105 BRCMF_FWEH_EVENT_ENUM_DEFLIST
107 #undef BRCMF_ENUM_DEF
110 * brcmf_fweh_event_name() - returns name for given event code.
112 * @code: code to lookup.
114 static const char *brcmf_fweh_event_name(enum brcmf_fweh_event_code code
)
117 for (i
= 0; i
< ARRAY_SIZE(fweh_event_names
); i
++) {
118 if (fweh_event_names
[i
].code
== code
)
119 return fweh_event_names
[i
].name
;
124 static const char *brcmf_fweh_event_name(enum brcmf_fweh_event_code code
)
131 * brcmf_fweh_queue_event() - create and queue event.
133 * @fweh: firmware event handling info.
134 * @event: event queue entry.
136 static void brcmf_fweh_queue_event(struct brcmf_fweh_info
*fweh
,
137 struct brcmf_fweh_queue_item
*event
)
141 spin_lock_irqsave(&fweh
->evt_q_lock
, flags
);
142 list_add_tail(&event
->q
, &fweh
->event_q
);
143 spin_unlock_irqrestore(&fweh
->evt_q_lock
, flags
);
144 schedule_work(&fweh
->event_work
);
147 static int brcmf_fweh_call_event_handler(struct brcmf_if
*ifp
,
148 enum brcmf_fweh_event_code code
,
149 struct brcmf_event_msg
*emsg
,
152 struct brcmf_fweh_info
*fweh
;
156 fweh
= &ifp
->drvr
->fweh
;
158 /* handle the event if valid interface and handler */
159 if (fweh
->evt_handler
[code
])
160 err
= fweh
->evt_handler
[code
](ifp
, emsg
, data
);
162 brcmf_err("unhandled event %d ignored\n", code
);
164 brcmf_err("no interface object\n");
170 * brcmf_fweh_handle_if_event() - handle IF event.
172 * @drvr: driver information object.
173 * @item: queue entry.
174 * @ifpp: interface object (may change upon ADD action).
176 static void brcmf_fweh_handle_if_event(struct brcmf_pub
*drvr
,
177 struct brcmf_event_msg
*emsg
,
180 struct brcmf_if_event
*ifevent
= data
;
181 struct brcmf_if
*ifp
;
184 brcmf_dbg(EVENT
, "action: %u idx: %u bsscfg: %u flags: %u role: %u\n",
185 ifevent
->action
, ifevent
->ifidx
, ifevent
->bssidx
,
186 ifevent
->flags
, ifevent
->role
);
188 if (ifevent
->flags
& BRCMF_E_IF_FLAG_NOIF
) {
189 brcmf_dbg(EVENT
, "event can be ignored\n");
192 if (ifevent
->ifidx
>= BRCMF_MAX_IFS
) {
193 brcmf_err("invalid interface index: %u\n",
198 ifp
= drvr
->iflist
[ifevent
->bssidx
];
200 if (ifevent
->action
== BRCMF_E_IF_ADD
) {
201 brcmf_dbg(EVENT
, "adding %s (%pM)\n", emsg
->ifname
,
203 ifp
= brcmf_add_if(drvr
, ifevent
->bssidx
, ifevent
->ifidx
,
204 emsg
->ifname
, emsg
->addr
);
207 brcmf_fws_add_interface(ifp
);
208 if (!drvr
->fweh
.evt_handler
[BRCMF_E_IF
])
209 if (brcmf_net_attach(ifp
, false) < 0)
213 if (ifevent
->action
== BRCMF_E_IF_CHANGE
)
214 brcmf_fws_reset_interface(ifp
);
216 err
= brcmf_fweh_call_event_handler(ifp
, emsg
->event_code
, emsg
, data
);
218 if (ifevent
->action
== BRCMF_E_IF_DEL
) {
219 brcmf_fws_del_interface(ifp
);
220 brcmf_del_if(drvr
, ifevent
->bssidx
);
225 * brcmf_fweh_dequeue_event() - get event from the queue.
227 * @fweh: firmware event handling info.
229 static struct brcmf_fweh_queue_item
*
230 brcmf_fweh_dequeue_event(struct brcmf_fweh_info
*fweh
)
232 struct brcmf_fweh_queue_item
*event
= NULL
;
235 spin_lock_irqsave(&fweh
->evt_q_lock
, flags
);
236 if (!list_empty(&fweh
->event_q
)) {
237 event
= list_first_entry(&fweh
->event_q
,
238 struct brcmf_fweh_queue_item
, q
);
241 spin_unlock_irqrestore(&fweh
->evt_q_lock
, flags
);
247 * brcmf_fweh_event_worker() - firmware event worker.
249 * @work: worker object.
251 static void brcmf_fweh_event_worker(struct work_struct
*work
)
253 struct brcmf_pub
*drvr
;
254 struct brcmf_if
*ifp
;
255 struct brcmf_fweh_info
*fweh
;
256 struct brcmf_fweh_queue_item
*event
;
258 struct brcmf_event_msg_be
*emsg_be
;
259 struct brcmf_event_msg emsg
;
261 fweh
= container_of(work
, struct brcmf_fweh_info
, event_work
);
262 drvr
= container_of(fweh
, struct brcmf_pub
, fweh
);
264 while ((event
= brcmf_fweh_dequeue_event(fweh
))) {
265 brcmf_dbg(EVENT
, "event %s (%u) ifidx %u bsscfg %u addr %pM\n",
266 brcmf_fweh_event_name(event
->code
), event
->code
,
267 event
->emsg
.ifidx
, event
->emsg
.bsscfgidx
,
270 /* convert event message */
271 emsg_be
= &event
->emsg
;
272 emsg
.version
= be16_to_cpu(emsg_be
->version
);
273 emsg
.flags
= be16_to_cpu(emsg_be
->flags
);
274 emsg
.event_code
= event
->code
;
275 emsg
.status
= be32_to_cpu(emsg_be
->status
);
276 emsg
.reason
= be32_to_cpu(emsg_be
->reason
);
277 emsg
.auth_type
= be32_to_cpu(emsg_be
->auth_type
);
278 emsg
.datalen
= be32_to_cpu(emsg_be
->datalen
);
279 memcpy(emsg
.addr
, emsg_be
->addr
, ETH_ALEN
);
280 memcpy(emsg
.ifname
, emsg_be
->ifname
, sizeof(emsg
.ifname
));
281 emsg
.ifidx
= emsg_be
->ifidx
;
282 emsg
.bsscfgidx
= emsg_be
->bsscfgidx
;
284 brcmf_dbg(EVENT
, " version %u flags %u status %u reason %u\n",
285 emsg
.version
, emsg
.flags
, emsg
.status
, emsg
.reason
);
286 brcmf_dbg_hex_dump(BRCMF_EVENT_ON(), event
->data
,
287 min_t(u32
, emsg
.datalen
, 64),
288 "event payload, len=%d\n", emsg
.datalen
);
290 /* special handling of interface event */
291 if (event
->code
== BRCMF_E_IF
) {
292 brcmf_fweh_handle_if_event(drvr
, &emsg
, event
->data
);
296 ifp
= drvr
->iflist
[emsg
.bsscfgidx
];
297 err
= brcmf_fweh_call_event_handler(ifp
, event
->code
, &emsg
,
300 brcmf_err("event handler failed (%d)\n",
310 * brcmf_fweh_attach() - initialize firmware event handling.
312 * @drvr: driver information object.
314 void brcmf_fweh_attach(struct brcmf_pub
*drvr
)
316 struct brcmf_fweh_info
*fweh
= &drvr
->fweh
;
317 INIT_WORK(&fweh
->event_work
, brcmf_fweh_event_worker
);
318 spin_lock_init(&fweh
->evt_q_lock
);
319 INIT_LIST_HEAD(&fweh
->event_q
);
323 * brcmf_fweh_detach() - cleanup firmware event handling.
325 * @drvr: driver information object.
327 void brcmf_fweh_detach(struct brcmf_pub
*drvr
)
329 struct brcmf_fweh_info
*fweh
= &drvr
->fweh
;
330 struct brcmf_if
*ifp
= drvr
->iflist
[0];
331 s8 eventmask
[BRCMF_EVENTING_MASK_LEN
];
334 /* clear all events */
335 memset(eventmask
, 0, BRCMF_EVENTING_MASK_LEN
);
336 (void)brcmf_fil_iovar_data_set(ifp
, "event_msgs",
338 BRCMF_EVENTING_MASK_LEN
);
340 /* cancel the worker */
341 cancel_work_sync(&fweh
->event_work
);
342 WARN_ON(!list_empty(&fweh
->event_q
));
343 memset(fweh
->evt_handler
, 0, sizeof(fweh
->evt_handler
));
347 * brcmf_fweh_register() - register handler for given event code.
349 * @drvr: driver information object.
351 * @handler: handler for the given event code.
353 int brcmf_fweh_register(struct brcmf_pub
*drvr
, enum brcmf_fweh_event_code code
,
354 brcmf_fweh_handler_t handler
)
356 if (drvr
->fweh
.evt_handler
[code
]) {
357 brcmf_err("event code %d already registered\n", code
);
360 drvr
->fweh
.evt_handler
[code
] = handler
;
361 brcmf_dbg(TRACE
, "event handler registered for %s\n",
362 brcmf_fweh_event_name(code
));
367 * brcmf_fweh_unregister() - remove handler for given code.
369 * @drvr: driver information object.
372 void brcmf_fweh_unregister(struct brcmf_pub
*drvr
,
373 enum brcmf_fweh_event_code code
)
375 brcmf_dbg(TRACE
, "event handler cleared for %s\n",
376 brcmf_fweh_event_name(code
));
377 drvr
->fweh
.evt_handler
[code
] = NULL
;
381 * brcmf_fweh_activate_events() - enables firmware events registered.
383 * @ifp: primary interface object.
385 int brcmf_fweh_activate_events(struct brcmf_if
*ifp
)
388 s8 eventmask
[BRCMF_EVENTING_MASK_LEN
];
390 for (i
= 0; i
< BRCMF_E_LAST
; i
++) {
391 if (ifp
->drvr
->fweh
.evt_handler
[i
]) {
392 brcmf_dbg(EVENT
, "enable event %s\n",
393 brcmf_fweh_event_name(i
));
394 setbit(eventmask
, i
);
398 /* want to handle IF event as well */
399 brcmf_dbg(EVENT
, "enable event IF\n");
400 setbit(eventmask
, BRCMF_E_IF
);
402 err
= brcmf_fil_iovar_data_set(ifp
, "event_msgs",
403 eventmask
, BRCMF_EVENTING_MASK_LEN
);
405 brcmf_err("Set event_msgs error (%d)\n", err
);
411 * brcmf_fweh_process_event() - process skb as firmware event.
413 * @drvr: driver information object.
414 * @event_packet: event packet to process.
416 * If the packet buffer contains a firmware event message it will
417 * dispatch the event to a registered handler (using worker).
419 void brcmf_fweh_process_event(struct brcmf_pub
*drvr
,
420 struct brcmf_event
*event_packet
)
422 enum brcmf_fweh_event_code code
;
423 struct brcmf_fweh_info
*fweh
= &drvr
->fweh
;
424 struct brcmf_fweh_queue_item
*event
;
425 gfp_t alloc_flag
= GFP_KERNEL
;
430 code
= get_unaligned_be32(&event_packet
->msg
.event_type
);
431 datalen
= get_unaligned_be32(&event_packet
->msg
.datalen
);
432 data
= &event_packet
[1];
434 if (code
>= BRCMF_E_LAST
)
437 if (code
!= BRCMF_E_IF
&& !fweh
->evt_handler
[code
])
441 alloc_flag
= GFP_ATOMIC
;
443 event
= kzalloc(sizeof(*event
) + datalen
, alloc_flag
);
448 event
->ifidx
= event_packet
->msg
.ifidx
;
450 /* use memcpy to get aligned event message */
451 memcpy(&event
->emsg
, &event_packet
->msg
, sizeof(event
->emsg
));
452 memcpy(event
->data
, data
, datalen
);
453 memcpy(event
->ifaddr
, event_packet
->eth
.h_dest
, ETH_ALEN
);
455 brcmf_fweh_queue_event(fweh
, event
);