2 * WiMedia Logical Link Control Protocol (WLP)
4 * Copyright (C) 2005-2006 Intel Corporation
5 * Reinette Chatre <reinette.chatre@intel.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #include <linux/wlp.h>
26 #include "wlp-internal.h"
29 void wlp_neighbor_init(struct wlp_neighbor_e
*neighbor
)
31 INIT_LIST_HEAD(&neighbor
->wssid
);
35 * Create area for device information storage
37 * wlp->mutex must be held
39 int __wlp_alloc_device_info(struct wlp
*wlp
)
41 struct device
*dev
= &wlp
->rc
->uwb_dev
.dev
;
42 BUG_ON(wlp
->dev_info
!= NULL
);
43 wlp
->dev_info
= kzalloc(sizeof(struct wlp_device_info
), GFP_KERNEL
);
44 if (wlp
->dev_info
== NULL
) {
45 dev_err(dev
, "WLP: Unable to allocate memory for "
46 "device information.\n");
54 * Fill in device information using function provided by driver
56 * wlp->mutex must be held
59 void __wlp_fill_device_info(struct wlp
*wlp
)
61 wlp
->fill_device_info(wlp
, wlp
->dev_info
);
65 * Setup device information
67 * Allocate area for device information and populate it.
69 * wlp->mutex must be held
71 int __wlp_setup_device_info(struct wlp
*wlp
)
74 struct device
*dev
= &wlp
->rc
->uwb_dev
.dev
;
76 result
= __wlp_alloc_device_info(wlp
);
78 dev_err(dev
, "WLP: Unable to allocate area for "
79 "device information.\n");
82 __wlp_fill_device_info(wlp
);
87 * Remove information about neighbor stored temporarily
89 * Information learned during discovey should only be stored when the
90 * device enrolls in the neighbor's WSS. We do need to store this
91 * information temporarily in order to present it to the user.
93 * We are only interested in keeping neighbor WSS information if that
94 * neighbor is accepting enrollment.
96 * should be called with wlp->nbmutex held
98 void wlp_remove_neighbor_tmp_info(struct wlp_neighbor_e
*neighbor
)
100 struct wlp_wssid_e
*wssid_e
, *next
;
102 if (!list_empty(&neighbor
->wssid
)) {
103 list_for_each_entry_safe(wssid_e
, next
, &neighbor
->wssid
,
105 if (wssid_e
->info
!= NULL
) {
106 keep
= wssid_e
->info
->accept_enroll
;
107 kfree(wssid_e
->info
);
108 wssid_e
->info
= NULL
;
110 list_del(&wssid_e
->node
);
116 if (neighbor
->info
!= NULL
) {
117 kfree(neighbor
->info
);
118 neighbor
->info
= NULL
;
123 * Populate WLP neighborhood cache with neighbor information
125 * A new neighbor is found. If it is discoverable then we add it to the
126 * neighborhood cache.
130 int wlp_add_neighbor(struct wlp
*wlp
, struct uwb_dev
*dev
)
134 struct wlp_neighbor_e
*neighbor
;
138 * Use contents of WLP IE found in beacon cache to determine if
139 * neighbor is discoverable.
140 * The device does not support WLP IE yet so this still needs to be
141 * done. Until then we assume all devices are discoverable.
143 discoverable
= 1; /* will be changed when FIXME disappears */
145 /* Add neighbor to cache for discovery */
146 neighbor
= kzalloc(sizeof(*neighbor
), GFP_KERNEL
);
147 if (neighbor
== NULL
) {
148 dev_err(&dev
->dev
, "Unable to create memory for "
153 wlp_neighbor_init(neighbor
);
155 neighbor
->uwb_dev
= dev
;
156 list_add(&neighbor
->node
, &wlp
->neighbors
);
163 * Remove one neighbor from cache
166 void __wlp_neighbor_release(struct wlp_neighbor_e
*neighbor
)
168 struct wlp_wssid_e
*wssid_e
, *next_wssid_e
;
170 list_for_each_entry_safe(wssid_e
, next_wssid_e
,
171 &neighbor
->wssid
, node
) {
172 list_del(&wssid_e
->node
);
175 uwb_dev_put(neighbor
->uwb_dev
);
176 list_del(&neighbor
->node
);
181 * Clear entire neighborhood cache.
184 void __wlp_neighbors_release(struct wlp
*wlp
)
186 struct wlp_neighbor_e
*neighbor
, *next
;
187 if (list_empty(&wlp
->neighbors
))
189 list_for_each_entry_safe(neighbor
, next
, &wlp
->neighbors
, node
) {
190 __wlp_neighbor_release(neighbor
);
195 void wlp_neighbors_release(struct wlp
*wlp
)
197 mutex_lock(&wlp
->nbmutex
);
198 __wlp_neighbors_release(wlp
);
199 mutex_unlock(&wlp
->nbmutex
);
205 * Send D1 message to neighbor, receive D2 message
207 * @neighbor: neighbor to which D1 message will be sent
208 * @wss: if not NULL, it is an enrollment request for this WSS
209 * @wssid: if wss not NULL, this is the wssid of the WSS in which we
212 * A D1/D2 exchange is done for one of two reasons: discovery or
213 * enrollment. If done for discovery the D1 message is sent to the neighbor
214 * and the contents of the D2 response is stored in a temporary cache.
215 * If done for enrollment the @wss and @wssid are provided also. In this
216 * case the D1 message is sent to the neighbor, the D2 response is parsed
217 * for enrollment of the WSS with wssid.
219 * &wss->mutex is held
222 int wlp_d1d2_exchange(struct wlp
*wlp
, struct wlp_neighbor_e
*neighbor
,
223 struct wlp_wss
*wss
, struct wlp_uuid
*wssid
)
226 struct device
*dev
= &wlp
->rc
->uwb_dev
.dev
;
227 DECLARE_COMPLETION_ONSTACK(completion
);
228 struct wlp_session session
;
230 struct wlp_frame_assoc
*resp
;
231 struct uwb_dev_addr
*dev_addr
= &neighbor
->uwb_dev
->dev_addr
;
233 mutex_lock(&wlp
->mutex
);
234 if (!wlp_uuid_is_set(&wlp
->uuid
)) {
235 dev_err(dev
, "WLP: UUID is not set. Set via sysfs to "
240 /* Send D1 association frame */
241 result
= wlp_send_assoc_frame(wlp
, wss
, dev_addr
, WLP_ASSOC_D1
);
243 dev_err(dev
, "Unable to send D1 frame to neighbor "
244 "%02x:%02x (%d)\n", dev_addr
->data
[1],
245 dev_addr
->data
[0], result
);
248 /* Create session, wait for response */
249 session
.exp_message
= WLP_ASSOC_D2
;
250 session
.cb
= wlp_session_cb
;
251 session
.cb_priv
= &completion
;
252 session
.neighbor_addr
= *dev_addr
;
253 BUG_ON(wlp
->session
!= NULL
);
254 wlp
->session
= &session
;
255 /* Wait for D2/F0 frame */
256 result
= wait_for_completion_interruptible_timeout(&completion
,
257 WLP_PER_MSG_TIMEOUT
* HZ
);
260 dev_err(dev
, "Timeout while sending D1 to neighbor "
261 "%02x:%02x.\n", dev_addr
->data
[1],
266 dev_err(dev
, "Unable to discover/enroll neighbor %02x:%02x.\n",
267 dev_addr
->data
[1], dev_addr
->data
[0]);
270 /* Parse message in session->data: it will be either D2 or F0 */
272 resp
= (void *) skb
->data
;
274 if (resp
->type
== WLP_ASSOC_F0
) {
275 result
= wlp_parse_f0(wlp
, skb
);
277 dev_err(dev
, "WLP: Unable to parse F0 from neighbor "
278 "%02x:%02x.\n", dev_addr
->data
[1],
281 goto error_resp_parse
;
285 result
= wlp_parse_d2_frame_to_cache(wlp
, skb
, neighbor
);
287 dev_err(dev
, "WLP: Unable to parse D2 message from "
288 "neighbor %02x:%02x for discovery.\n",
289 dev_addr
->data
[1], dev_addr
->data
[0]);
290 goto error_resp_parse
;
294 result
= wlp_parse_d2_frame_to_enroll(wss
, skb
, neighbor
,
297 dev_err(dev
, "WLP: Unable to parse D2 message from "
298 "neighbor %02x:%02x for enrollment.\n",
299 dev_addr
->data
[1], dev_addr
->data
[0]);
300 goto error_resp_parse
;
308 mutex_unlock(&wlp
->mutex
);
313 * Enroll into WSS of provided WSSID by using neighbor as registrar
315 * &wss->mutex is held
317 int wlp_enroll_neighbor(struct wlp
*wlp
, struct wlp_neighbor_e
*neighbor
,
318 struct wlp_wss
*wss
, struct wlp_uuid
*wssid
)
321 struct device
*dev
= &wlp
->rc
->uwb_dev
.dev
;
322 char buf
[WLP_WSS_UUID_STRSIZE
];
323 struct uwb_dev_addr
*dev_addr
= &neighbor
->uwb_dev
->dev_addr
;
325 wlp_wss_uuid_print(buf
, sizeof(buf
), wssid
);
327 result
= wlp_d1d2_exchange(wlp
, neighbor
, wss
, wssid
);
329 dev_err(dev
, "WLP: D1/D2 message exchange for enrollment "
330 "failed. result = %d \n", result
);
333 if (wss
->state
!= WLP_WSS_STATE_PART_ENROLLED
) {
334 dev_err(dev
, "WLP: Unable to enroll into WSS %s using "
335 "neighbor %02x:%02x. \n", buf
,
336 dev_addr
->data
[1], dev_addr
->data
[0]);
340 if (wss
->secure_status
== WLP_WSS_SECURE
) {
341 dev_err(dev
, "FIXME: need to complete secure enrollment.\n");
345 wss
->state
= WLP_WSS_STATE_ENROLLED
;
346 dev_dbg(dev
, "WLP: Success Enrollment into unsecure WSS "
347 "%s using neighbor %02x:%02x. \n",
348 buf
, dev_addr
->data
[1], dev_addr
->data
[0]);
358 * Discover WSS information of neighbor's active WSS
361 int wlp_discover_neighbor(struct wlp
*wlp
,
362 struct wlp_neighbor_e
*neighbor
)
364 return wlp_d1d2_exchange(wlp
, neighbor
, NULL
, NULL
);
369 * Each neighbor in the neighborhood cache is discoverable. Discover it.
371 * Discovery is done through sending of D1 association frame and parsing
372 * the D2 association frame response. Only wssid from D2 will be included
373 * in neighbor cache, rest is just displayed to user and forgotten.
375 * The discovery is not done in parallel. This is simple and enables us to
376 * maintain only one association context.
378 * The discovery of one neighbor does not affect the other, but if the
379 * discovery of a neighbor fails it is removed from the neighborhood cache.
382 int wlp_discover_all_neighbors(struct wlp
*wlp
)
385 struct device
*dev
= &wlp
->rc
->uwb_dev
.dev
;
386 struct wlp_neighbor_e
*neighbor
, *next
;
388 list_for_each_entry_safe(neighbor
, next
, &wlp
->neighbors
, node
) {
389 result
= wlp_discover_neighbor(wlp
, neighbor
);
391 dev_err(dev
, "WLP: Unable to discover neighbor "
392 "%02x:%02x, removing from neighborhood. \n",
393 neighbor
->uwb_dev
->dev_addr
.data
[1],
394 neighbor
->uwb_dev
->dev_addr
.data
[0]);
395 __wlp_neighbor_release(neighbor
);
401 static int wlp_add_neighbor_helper(struct device
*dev
, void *priv
)
403 struct wlp
*wlp
= priv
;
404 struct uwb_dev
*uwb_dev
= to_uwb_dev(dev
);
406 return wlp_add_neighbor(wlp
, uwb_dev
);
410 * Discover WLP neighborhood
412 * Will send D1 association frame to all devices in beacon group that have
413 * discoverable bit set in WLP IE. D2 frames will be received, information
414 * displayed to user in @buf. Partial information (from D2 association
415 * frame) will be cached to assist with future association
418 * The discovery of the WLP neighborhood is triggered by the user. This
419 * should occur infrequently and we thus free current cache and re-allocate
422 * If one neighbor fails during initial discovery (determining if it is a
423 * neighbor or not), we fail all - note that interaction with neighbor has
424 * not occured at this point so if a failure occurs we know something went wrong
425 * locally. We thus undo everything.
427 ssize_t
wlp_discover(struct wlp
*wlp
)
430 struct device
*dev
= &wlp
->rc
->uwb_dev
.dev
;
432 mutex_lock(&wlp
->nbmutex
);
433 /* Clear current neighborhood cache. */
434 __wlp_neighbors_release(wlp
);
435 /* Determine which devices in neighborhood. Repopulate cache. */
436 result
= uwb_dev_for_each(wlp
->rc
, wlp_add_neighbor_helper
, wlp
);
438 /* May have partial neighbor information, release all. */
439 __wlp_neighbors_release(wlp
);
440 goto error_dev_for_each
;
442 /* Discover the properties of devices in neighborhood. */
443 result
= wlp_discover_all_neighbors(wlp
);
444 /* In case of failure we still print our partial results. */
446 dev_err(dev
, "Unable to fully discover neighborhood. \n");
450 mutex_unlock(&wlp
->nbmutex
);
455 * Handle events from UWB stack
457 * We handle events conservatively. If a neighbor goes off the air we
458 * remove it from the neighborhood. If an association process is in
459 * progress this function will block waiting for the nbmutex to become
460 * free. The association process will thus be allowed to complete before it
464 void wlp_uwb_notifs_cb(void *_wlp
, struct uwb_dev
*uwb_dev
,
465 enum uwb_notifs event
)
467 struct wlp
*wlp
= _wlp
;
468 struct device
*dev
= &wlp
->rc
->uwb_dev
.dev
;
469 struct wlp_neighbor_e
*neighbor
, *next
;
472 case UWB_NOTIF_ONAIR
:
473 result
= wlp_eda_create_node(&wlp
->eda
,
474 uwb_dev
->mac_addr
.data
,
477 dev_err(dev
, "WLP: Unable to add new neighbor "
478 "%02x:%02x to EDA cache.\n",
479 uwb_dev
->dev_addr
.data
[1],
480 uwb_dev
->dev_addr
.data
[0]);
482 case UWB_NOTIF_OFFAIR
:
483 wlp_eda_rm_node(&wlp
->eda
, &uwb_dev
->dev_addr
);
484 mutex_lock(&wlp
->nbmutex
);
485 list_for_each_entry_safe(neighbor
, next
, &wlp
->neighbors
, node
) {
486 if (neighbor
->uwb_dev
== uwb_dev
)
487 __wlp_neighbor_release(neighbor
);
489 mutex_unlock(&wlp
->nbmutex
);
492 dev_err(dev
, "don't know how to handle event %d from uwb\n",
497 static void wlp_channel_changed(struct uwb_pal
*pal
, int channel
)
499 struct wlp
*wlp
= container_of(pal
, struct wlp
, pal
);
502 netif_carrier_off(wlp
->ndev
);
504 netif_carrier_on(wlp
->ndev
);
507 int wlp_setup(struct wlp
*wlp
, struct uwb_rc
*rc
, struct net_device
*ndev
)
511 BUG_ON(wlp
->fill_device_info
== NULL
);
512 BUG_ON(wlp
->xmit_frame
== NULL
);
513 BUG_ON(wlp
->stop_queue
== NULL
);
514 BUG_ON(wlp
->start_queue
== NULL
);
518 wlp_eda_init(&wlp
->eda
);/* Set up address cache */
519 wlp
->uwb_notifs_handler
.cb
= wlp_uwb_notifs_cb
;
520 wlp
->uwb_notifs_handler
.data
= wlp
;
521 uwb_notifs_register(rc
, &wlp
->uwb_notifs_handler
);
523 uwb_pal_init(&wlp
->pal
);
525 wlp
->pal
.channel_changed
= wlp_channel_changed
;
526 result
= uwb_pal_register(&wlp
->pal
);
528 uwb_notifs_deregister(wlp
->rc
, &wlp
->uwb_notifs_handler
);
532 EXPORT_SYMBOL_GPL(wlp_setup
);
534 void wlp_remove(struct wlp
*wlp
)
536 wlp_neighbors_release(wlp
);
537 uwb_pal_unregister(&wlp
->pal
);
538 uwb_notifs_deregister(wlp
->rc
, &wlp
->uwb_notifs_handler
);
539 wlp_eda_release(&wlp
->eda
);
540 mutex_lock(&wlp
->mutex
);
541 if (wlp
->dev_info
!= NULL
)
542 kfree(wlp
->dev_info
);
543 mutex_unlock(&wlp
->mutex
);
546 EXPORT_SYMBOL_GPL(wlp_remove
);
549 * wlp_reset_all - reset the WLP hardware
550 * @wlp: the WLP device to reset.
552 * This schedules a full hardware reset of the WLP device. The radio
553 * controller and any other PALs will also be reset.
555 void wlp_reset_all(struct wlp
*wlp
)
557 uwb_rc_reset_all(wlp
->rc
);
559 EXPORT_SYMBOL_GPL(wlp_reset_all
);