2 * Intel Wireless WiMAX Connection 2400m
3 * Miscellaneous control functions for managing the device
6 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Intel Corporation <linux-wimax@intel.com>
36 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
37 * - Initial implementation
39 * This is a collection of functions used to control the device (plus
42 * There are utilities for handling TLV buffers, hooks on the device's
43 * reports to act on device changes of state [i2400m_report_hook()],
44 * on acks to commands [i2400m_msg_ack_hook()], a helper for sending
45 * commands to the device and blocking until a reply arrives
46 * [i2400m_msg_to_dev()], a few high level commands for manipulating
47 * the device state, powersving mode and configuration plus the
48 * routines to setup the device once communication is stablished with
49 * it [i2400m_dev_initialize()].
53 * i2400m_dev_initalize() Called by i2400m_dev_start()
54 * i2400m_set_init_config()
55 * i2400m_cmd_get_state()
56 * i2400m_dev_shutdown() Called by i2400m_dev_stop()
59 * i2400m_{cmd,get,set}_*()
61 * i2400m_msg_check_status()
63 * i2400m_report_hook() Called on reception of an event
64 * i2400m_report_state_hook()
65 * i2400m_tlv_buffer_walk()
67 * i2400m_report_tlv_system_state()
68 * i2400m_report_tlv_rf_switches_status()
69 * i2400m_report_tlv_media_status()
70 * i2400m_cmd_enter_powersave()
72 * i2400m_msg_ack_hook() Called on reception of a reply to a
78 #include <linux/kernel.h>
79 #include <linux/wimax/i2400m.h>
82 #define D_SUBMODULE control
83 #include "debug-levels.h"
87 * Return if a TLV is of a give type and size
89 * @tlv_hdr: pointer to the TLV
90 * @tlv_type: type of the TLV we are looking for
91 * @tlv_size: expected size of the TLV we are looking for (if -1,
92 * don't check the size). This includes the header
93 * Returns: 0 if the TLV matches
94 * < 0 if it doesn't match at all
95 * > 0 total TLV + payload size, if the type matches, but not
99 ssize_t i2400m_tlv_match(const struct i2400m_tlv_hdr *tlv,
100 enum i2400m_tlv tlv_type, ssize_t tlv_size)
102 if (le16_to_cpu(tlv->type) != tlv_type) /* Not our type? skip */
105 && le16_to_cpu(tlv->length) + sizeof(*tlv) != tlv_size) {
106 size_t size = le16_to_cpu(tlv->length) + sizeof(*tlv);
107 printk(KERN_WARNING "W: tlv type 0x%x mismatched because of "
108 "size (got %zu vs %zu expected)\n",
109 tlv_type, size, tlv_size);
117 * Given a buffer of TLVs, iterate over them
119 * @i2400m: device instance
120 * @tlv_buf: pointer to the beginning of the TLV buffer
121 * @buf_size: buffer size in bytes
122 * @tlv_pos: seek position; this is assumed to be a pointer returned
123 * by i2400m_tlv_buffer_walk() [and thus, validated]. The
124 * TLV returned will be the one following this one.
129 * while (tlv_itr = i2400m_tlv_buffer_walk(i2400m, buf, size, tlv_itr)) {
131 * // Do stuff with tlv_itr, DON'T MODIFY IT
136 const struct i2400m_tlv_hdr *i2400m_tlv_buffer_walk(
137 struct i2400m *i2400m,
138 const void *tlv_buf, size_t buf_size,
139 const struct i2400m_tlv_hdr *tlv_pos)
141 struct device *dev = i2400m_dev(i2400m);
142 const struct i2400m_tlv_hdr *tlv_top = tlv_buf + buf_size;
143 size_t offset, length, avail_size;
146 if (tlv_pos == NULL) /* Take the first one? */
148 else /* Nope, the next one */
149 tlv_pos = (void *) tlv_pos
150 + le16_to_cpu(tlv_pos->length) + sizeof(*tlv_pos);
151 if (tlv_pos == tlv_top) { /* buffer done */
153 goto error_beyond_end;
155 if (tlv_pos > tlv_top) {
158 goto error_beyond_end;
160 offset = (void *) tlv_pos - (void *) tlv_buf;
161 avail_size = buf_size - offset;
162 if (avail_size < sizeof(*tlv_pos)) {
163 dev_err(dev, "HW BUG? tlv_buf %p [%zu bytes], tlv @%zu: "
164 "short header\n", tlv_buf, buf_size, offset);
165 goto error_short_header;
167 type = le16_to_cpu(tlv_pos->type);
168 length = le16_to_cpu(tlv_pos->length);
169 if (avail_size < sizeof(*tlv_pos) + length) {
170 dev_err(dev, "HW BUG? tlv_buf %p [%zu bytes], "
171 "tlv type 0x%04x @%zu: "
172 "short data (%zu bytes vs %zu needed)\n",
173 tlv_buf, buf_size, type, offset, avail_size,
174 sizeof(*tlv_pos) + length);
175 goto error_short_header;
184 * Find a TLV in a buffer of sequential TLVs
186 * @i2400m: device descriptor
187 * @tlv_hdr: pointer to the first TLV in the sequence
188 * @size: size of the buffer in bytes; all TLVs are assumed to fit
189 * fully in the buffer (otherwise we'll complain).
190 * @tlv_type: type of the TLV we are looking for
191 * @tlv_size: expected size of the TLV we are looking for (if -1,
192 * don't check the size). This includes the header
194 * Returns: NULL if the TLV is not found, otherwise a pointer to
195 * it. If the sizes don't match, an error is printed and NULL
199 const struct i2400m_tlv_hdr *i2400m_tlv_find(
200 struct i2400m *i2400m,
201 const struct i2400m_tlv_hdr *tlv_hdr, size_t size,
202 enum i2400m_tlv tlv_type, ssize_t tlv_size)
205 struct device *dev = i2400m_dev(i2400m);
206 const struct i2400m_tlv_hdr *tlv = NULL;
207 while ((tlv = i2400m_tlv_buffer_walk(i2400m, tlv_hdr, size, tlv))) {
208 match = i2400m_tlv_match(tlv, tlv_type, tlv_size);
209 if (match == 0) /* found it :) */
212 dev_warn(dev, "TLV type 0x%04x found with size "
213 "mismatch (%zu vs %zu needed)\n",
214 tlv_type, match, tlv_size);
224 } ms_to_errno[I2400M_MS_MAX] = {
225 [I2400M_MS_DONE_OK] = { "", 0 },
226 [I2400M_MS_DONE_IN_PROGRESS] = { "", 0 },
227 [I2400M_MS_INVALID_OP] = { "invalid opcode", -ENOSYS },
228 [I2400M_MS_BAD_STATE] = { "invalid state", -EILSEQ },
229 [I2400M_MS_ILLEGAL_VALUE] = { "illegal value", -EINVAL },
230 [I2400M_MS_MISSING_PARAMS] = { "missing parameters", -ENOMSG },
231 [I2400M_MS_VERSION_ERROR] = { "bad version", -EIO },
232 [I2400M_MS_ACCESSIBILITY_ERROR] = { "accesibility error", -EIO },
233 [I2400M_MS_BUSY] = { "busy", -EBUSY },
234 [I2400M_MS_CORRUPTED_TLV] = { "corrupted TLV", -EILSEQ },
235 [I2400M_MS_UNINITIALIZED] = { "not unitialized", -EILSEQ },
236 [I2400M_MS_UNKNOWN_ERROR] = { "unknown error", -EIO },
237 [I2400M_MS_PRODUCTION_ERROR] = { "production error", -EIO },
238 [I2400M_MS_NO_RF] = { "no RF", -EIO },
239 [I2400M_MS_NOT_READY_FOR_POWERSAVE] =
240 { "not ready for powersave", -EACCES },
241 [I2400M_MS_THERMAL_CRITICAL] = { "thermal critical", -EL3HLT },
246 * i2400m_msg_check_status - translate a message's status code
248 * @i2400m: device descriptor
249 * @l3l4_hdr: message header
250 * @strbuf: buffer to place a formatted error message (unless NULL).
251 * @strbuf_size: max amount of available space; larger messages will
254 * Returns: errno code corresponding to the status code in @l3l4_hdr
255 * and a message in @strbuf describing the error.
257 int i2400m_msg_check_status(const struct i2400m_l3l4_hdr *l3l4_hdr,
258 char *strbuf, size_t strbuf_size)
261 enum i2400m_ms status = le16_to_cpu(l3l4_hdr->status);
266 if (status > ARRAY_SIZE(ms_to_errno)) {
267 str = "unknown status code";
270 str = ms_to_errno[status].msg;
271 result = ms_to_errno[status].errno;
274 snprintf(strbuf, strbuf_size, "%s (%d)", str, status);
280 * Act on a TLV System State reported by the device
282 * @i2400m: device descriptor
283 * @ss: validated System State TLV
286 void i2400m_report_tlv_system_state(struct i2400m *i2400m,
287 const struct i2400m_tlv_system_state *ss)
289 struct device *dev = i2400m_dev(i2400m);
290 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
291 enum i2400m_system_state i2400m_state = le32_to_cpu(ss->state);
293 d_fnstart(3, dev, "(i2400m %p ss %p [%u])\n", i2400m, ss, i2400m_state);
295 if (unlikely(i2400m->ready == 0)) /* act if up */
297 if (i2400m->state != i2400m_state) {
298 i2400m->state = i2400m_state;
299 wake_up_all(&i2400m->state_wq);
301 switch (i2400m_state) {
302 case I2400M_SS_UNINITIALIZED:
304 case I2400M_SS_CONFIG:
305 case I2400M_SS_PRODUCTION:
306 wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
309 case I2400M_SS_RF_OFF:
310 case I2400M_SS_RF_SHUTDOWN:
311 wimax_state_change(wimax_dev, WIMAX_ST_RADIO_OFF);
314 case I2400M_SS_READY:
315 case I2400M_SS_STANDBY:
316 case I2400M_SS_SLEEPACTIVE:
317 wimax_state_change(wimax_dev, WIMAX_ST_READY);
320 case I2400M_SS_CONNECTING:
321 case I2400M_SS_WIMAX_CONNECTED:
322 wimax_state_change(wimax_dev, WIMAX_ST_READY);
326 case I2400M_SS_OUT_OF_ZONE:
327 wimax_state_change(wimax_dev, WIMAX_ST_SCANNING);
331 d_printf(1, dev, "entering BS-negotiated idle mode\n");
332 case I2400M_SS_DISCONNECTING:
333 case I2400M_SS_DATA_PATH_CONNECTED:
334 wimax_state_change(wimax_dev, WIMAX_ST_CONNECTED);
338 /* Huh? just in case, shut it down */
339 dev_err(dev, "HW BUG? unknown state %u: shutting down\n",
341 i2400m->bus_reset(i2400m, I2400M_RT_WARM);
345 d_fnend(3, dev, "(i2400m %p ss %p [%u]) = void\n",
346 i2400m, ss, i2400m_state);
351 * Parse and act on a TLV Media Status sent by the device
353 * @i2400m: device descriptor
354 * @ms: validated Media Status TLV
356 * This will set the carrier up on down based on the device's link
357 * report. This is done asides of what the WiMAX stack does based on
358 * the device's state as sometimes we need to do a link-renew (the BS
359 * wants us to renew a DHCP lease, for example).
361 * In fact, doc says that everytime we get a link-up, we should do a
362 * DHCP negotiation...
365 void i2400m_report_tlv_media_status(struct i2400m *i2400m,
366 const struct i2400m_tlv_media_status *ms)
368 struct device *dev = i2400m_dev(i2400m);
369 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
370 struct net_device *net_dev = wimax_dev->net_dev;
371 enum i2400m_media_status status = le32_to_cpu(ms->media_status);
373 d_fnstart(3, dev, "(i2400m %p ms %p [%u])\n", i2400m, ms, status);
375 if (unlikely(i2400m->ready == 0)) /* act if up */
378 case I2400M_MEDIA_STATUS_LINK_UP:
379 netif_carrier_on(net_dev);
381 case I2400M_MEDIA_STATUS_LINK_DOWN:
382 netif_carrier_off(net_dev);
385 * This is the network telling us we need to retrain the DHCP
386 * lease -- so far, we are trusting the WiMAX Network Service
387 * in user space to pick this up and poke the DHCP client.
389 case I2400M_MEDIA_STATUS_LINK_RENEW:
390 netif_carrier_on(net_dev);
393 dev_err(dev, "HW BUG? unknown media status %u\n",
397 d_fnend(3, dev, "(i2400m %p ms %p [%u]) = void\n",
403 * Parse a 'state report' and extract carrier on/off information
405 * @i2400m: device descriptor
406 * @l3l4_hdr: pointer to message; it has been already validated for
408 * @size: size of the message (header + payload). The header length
409 * declaration is assumed to be congruent with @size (as in
410 * sizeof(*l3l4_hdr) + l3l4_hdr->length == size)
412 * Extract from the report state the system state TLV and infer from
413 * there if we have a carrier or not. Update our local state and tell
416 * When setting the carrier, it's fine to set OFF twice (for example),
417 * as netif_carrier_off() will not generate two OFF events (just on
421 void i2400m_report_state_hook(struct i2400m *i2400m,
422 const struct i2400m_l3l4_hdr *l3l4_hdr,
423 size_t size, const char *tag)
425 struct device *dev = i2400m_dev(i2400m);
426 const struct i2400m_tlv_hdr *tlv;
427 const struct i2400m_tlv_system_state *ss;
428 const struct i2400m_tlv_rf_switches_status *rfss;
429 const struct i2400m_tlv_media_status *ms;
430 size_t tlv_size = le16_to_cpu(l3l4_hdr->length);
432 d_fnstart(4, dev, "(i2400m %p, l3l4_hdr %p, size %zu, %s)\n",
433 i2400m, l3l4_hdr, size, tag);
436 while ((tlv = i2400m_tlv_buffer_walk(i2400m, &l3l4_hdr->pl,
438 if (0 == i2400m_tlv_match(tlv, I2400M_TLV_SYSTEM_STATE,
440 ss = container_of(tlv, typeof(*ss), hdr);
441 d_printf(2, dev, "%s: system state TLV "
442 "found (0x%04x), state 0x%08x\n",
443 tag, I2400M_TLV_SYSTEM_STATE,
444 le32_to_cpu(ss->state));
445 i2400m_report_tlv_system_state(i2400m, ss);
447 if (0 == i2400m_tlv_match(tlv, I2400M_TLV_RF_STATUS,
449 rfss = container_of(tlv, typeof(*rfss), hdr);
450 d_printf(2, dev, "%s: RF status TLV "
451 "found (0x%04x), sw 0x%02x hw 0x%02x\n",
452 tag, I2400M_TLV_RF_STATUS,
453 le32_to_cpu(rfss->sw_rf_switch),
454 le32_to_cpu(rfss->hw_rf_switch));
455 i2400m_report_tlv_rf_switches_status(i2400m, rfss);
457 if (0 == i2400m_tlv_match(tlv, I2400M_TLV_MEDIA_STATUS,
459 ms = container_of(tlv, typeof(*ms), hdr);
460 d_printf(2, dev, "%s: Media Status TLV: %u\n",
461 tag, le32_to_cpu(ms->media_status));
462 i2400m_report_tlv_media_status(i2400m, ms);
465 d_fnend(4, dev, "(i2400m %p, l3l4_hdr %p, size %zu, %s) = void\n",
466 i2400m, l3l4_hdr, size, tag);
471 * i2400m_report_hook - (maybe) act on a report
473 * @i2400m: device descriptor
474 * @l3l4_hdr: pointer to message; it has been already validated for
476 * @size: size of the message (header + payload). The header length
477 * declaration is assumed to be congruent with @size (as in
478 * sizeof(*l3l4_hdr) + l3l4_hdr->length == size)
480 * Extract information we might need (like carrien on/off) from a
483 void i2400m_report_hook(struct i2400m *i2400m,
484 const struct i2400m_l3l4_hdr *l3l4_hdr, size_t size)
486 struct device *dev = i2400m_dev(i2400m);
489 d_fnstart(3, dev, "(i2400m %p l3l4_hdr %p size %zu)\n",
490 i2400m, l3l4_hdr, size);
491 /* Chew on the message, we might need some information from
493 msg_type = le16_to_cpu(l3l4_hdr->type);
495 case I2400M_MT_REPORT_STATE: /* carrier detection... */
496 i2400m_report_state_hook(i2400m,
497 l3l4_hdr, size, "REPORT STATE");
499 /* If the device is ready for power save, then ask it to do
501 case I2400M_MT_REPORT_POWERSAVE_READY: /* zzzzz */
502 if (l3l4_hdr->status == cpu_to_le16(I2400M_MS_DONE_OK)) {
503 d_printf(1, dev, "ready for powersave, requesting\n");
504 i2400m_cmd_enter_powersave(i2400m);
508 d_fnend(3, dev, "(i2400m %p l3l4_hdr %p size %zu) = void\n",
509 i2400m, l3l4_hdr, size);
514 * i2400m_msg_ack_hook - process cmd/set/get ack for internal status
516 * @i2400m: device descriptor
517 * @l3l4_hdr: pointer to message; it has been already validated for
519 * @size: size of the message
521 * Extract information we might need from acks to commands and act on
522 * it. This is akin to i2400m_report_hook(). Note most of this
523 * processing should be done in the function that calls the
524 * command. This is here for some cases where it can't happen...
526 void i2400m_msg_ack_hook(struct i2400m *i2400m,
527 const struct i2400m_l3l4_hdr *l3l4_hdr, size_t size)
530 struct device *dev = i2400m_dev(i2400m);
531 unsigned ack_type, ack_status;
534 /* Chew on the message, we might need some information from
536 ack_type = le16_to_cpu(l3l4_hdr->type);
537 ack_status = le16_to_cpu(l3l4_hdr->status);
539 case I2400M_MT_CMD_ENTER_POWERSAVE:
540 /* This is just left here for the sake of example, as
541 * the processing is done somewhere else. */
543 result = i2400m_msg_check_status(
544 l3l4_hdr, strerr, sizeof(strerr));
546 d_printf(1, dev, "ready for power save: %zd\n",
556 * i2400m_msg_size_check() - verify message size and header are congruent
558 * It is ok if the total message size is larger than the expected
559 * size, as there can be padding.
561 int i2400m_msg_size_check(struct i2400m *i2400m,
562 const struct i2400m_l3l4_hdr *l3l4_hdr,
566 struct device *dev = i2400m_dev(i2400m);
567 size_t expected_size;
568 d_fnstart(4, dev, "(i2400m %p l3l4_hdr %p msg_size %zu)\n",
569 i2400m, l3l4_hdr, msg_size);
570 if (msg_size < sizeof(*l3l4_hdr)) {
571 dev_err(dev, "bad size for message header "
572 "(expected at least %zu, got %zu)\n",
573 (size_t) sizeof(*l3l4_hdr), msg_size);
577 expected_size = le16_to_cpu(l3l4_hdr->length) + sizeof(*l3l4_hdr);
578 if (msg_size < expected_size) {
579 dev_err(dev, "bad size for message code 0x%04x (expected %zu, "
580 "got %zu)\n", le16_to_cpu(l3l4_hdr->type),
581 expected_size, msg_size);
587 "(i2400m %p l3l4_hdr %p msg_size %zu) = %d\n",
588 i2400m, l3l4_hdr, msg_size, result);
595 * Cancel a wait for a command ACK
597 * @i2400m: device descriptor
598 * @code: [negative] errno code to cancel with (don't use
601 * If there is an ack already filled out, free it.
603 void i2400m_msg_to_dev_cancel_wait(struct i2400m *i2400m, int code)
605 struct sk_buff *ack_skb;
608 spin_lock_irqsave(&i2400m->rx_lock, flags);
609 ack_skb = i2400m->ack_skb;
610 if (ack_skb && !IS_ERR(ack_skb))
612 i2400m->ack_skb = ERR_PTR(code);
613 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
618 * i2400m_msg_to_dev - Send a control message to the device and get a response
620 * @i2400m: device descriptor
624 * @buf: pointer to the buffer containing the message to be sent; it
625 * has to start with a &struct i2400M_l3l4_hdr and then
626 * followed by the payload. Once this function returns, the
627 * buffer can be reused.
629 * @buf_len: buffer size
633 * Pointer to skb containing the ack message. You need to check the
634 * pointer with IS_ERR(), as it might be an error code. Error codes
635 * could happen because:
637 * - the message wasn't formatted correctly
638 * - couldn't send the message
639 * - failed waiting for a response
640 * - the ack message wasn't formatted correctly
642 * The returned skb has been allocated with wimax_msg_to_user_alloc(),
643 * it contains the reponse in a netlink attribute and is ready to be
644 * passed up to user space with wimax_msg_to_user_send(). To access
645 * the payload and its length, use wimax_msg_{data,len}() on the skb.
647 * The skb has to be freed with kfree_skb() once done.
651 * This function delivers a message/command to the device and waits
652 * for an ack to be received. The format is described in
653 * linux/wimax/i2400m.h. In summary, a command/get/set is followed by an
656 * This function will not check the ack status, that's left up to the
657 * caller. Once done with the ack skb, it has to be kfree_skb()ed.
659 * The i2400m handles only one message at the same time, thus we need
660 * the mutex to exclude other players.
662 * We write the message and then wait for an answer to come back. The
663 * RX path intercepts control messages and handles them in
664 * i2400m_rx_ctl(). Reports (notifications) are (maybe) processed
665 * locally and then forwarded (as needed) to user space on the WiMAX
666 * stack message pipe. Acks are saved and passed back to us through an
667 * skb in i2400m->ack_skb which is ready to be given to generic
668 * netlink if need be.
670 struct sk_buff *i2400m_msg_to_dev(struct i2400m *i2400m,
671 const void *buf, size_t buf_len)
674 struct device *dev = i2400m_dev(i2400m);
675 const struct i2400m_l3l4_hdr *msg_l3l4_hdr;
676 struct sk_buff *ack_skb;
677 const struct i2400m_l3l4_hdr *ack_l3l4_hdr;
683 d_fnstart(3, dev, "(i2400m %p buf %p len %zu)\n",
684 i2400m, buf, buf_len);
686 if (i2400m->boot_mode)
687 return ERR_PTR(-ENODEV);
690 /* Check msg & payload consistency */
691 result = i2400m_msg_size_check(i2400m, msg_l3l4_hdr, buf_len);
694 msg_type = le16_to_cpu(msg_l3l4_hdr->type);
695 d_printf(1, dev, "CMD/GET/SET 0x%04x %zu bytes\n",
697 d_dump(2, dev, buf, buf_len);
699 /* Setup the completion, ack_skb ("we are waiting") and send
700 * the message to the device */
701 mutex_lock(&i2400m->msg_mutex);
702 spin_lock_irqsave(&i2400m->rx_lock, flags);
703 i2400m->ack_skb = ERR_PTR(-EINPROGRESS);
704 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
705 init_completion(&i2400m->msg_completion);
706 result = i2400m_tx(i2400m, buf, buf_len, I2400M_PT_CTRL);
708 dev_err(dev, "can't send message 0x%04x: %d\n",
709 le16_to_cpu(msg_l3l4_hdr->type), result);
713 /* Some commands take longer to execute because of crypto ops,
714 * so we give them some more leeway on timeout */
716 case I2400M_MT_GET_TLS_OPERATION_RESULT:
717 case I2400M_MT_CMD_SEND_EAP_RESPONSE:
718 ack_timeout = 5 * HZ;
724 /* The RX path in rx.c will put any response for this message
725 * in i2400m->ack_skb and wake us up. If we cancel the wait,
726 * we need to change the value of i2400m->ack_skb to something
727 * not -EINPROGRESS so RX knows there is no one waiting. */
728 result = wait_for_completion_interruptible_timeout(
729 &i2400m->msg_completion, ack_timeout);
731 dev_err(dev, "timeout waiting for reply to message 0x%04x\n",
734 i2400m_msg_to_dev_cancel_wait(i2400m, result);
735 goto error_wait_for_completion;
736 } else if (result < 0) {
737 dev_err(dev, "error waiting for reply to message 0x%04x: %d\n",
739 i2400m_msg_to_dev_cancel_wait(i2400m, result);
740 goto error_wait_for_completion;
743 /* Pull out the ack data from i2400m->ack_skb -- see if it is
744 * an error and act accordingly */
745 spin_lock_irqsave(&i2400m->rx_lock, flags);
746 ack_skb = i2400m->ack_skb;
748 result = PTR_ERR(ack_skb);
751 i2400m->ack_skb = NULL;
752 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
754 goto error_ack_status;
755 ack_l3l4_hdr = wimax_msg_data_len(ack_skb, &ack_len);
757 /* Check the ack and deliver it if it is ok */
758 result = i2400m_msg_size_check(i2400m, ack_l3l4_hdr, ack_len);
760 dev_err(dev, "HW BUG? reply to message 0x%04x: %d\n",
762 goto error_bad_ack_len;
764 if (msg_type != le16_to_cpu(ack_l3l4_hdr->type)) {
765 dev_err(dev, "HW BUG? bad reply 0x%04x to message 0x%04x\n",
766 le16_to_cpu(ack_l3l4_hdr->type), msg_type);
768 goto error_bad_ack_type;
770 i2400m_msg_ack_hook(i2400m, ack_l3l4_hdr, ack_len);
771 mutex_unlock(&i2400m->msg_mutex);
772 d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %p\n",
773 i2400m, buf, buf_len, ack_skb);
780 error_wait_for_completion:
782 mutex_unlock(&i2400m->msg_mutex);
784 d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %d\n",
785 i2400m, buf, buf_len, result);
786 return ERR_PTR(result);
791 * Definitions for the Enter Power Save command
793 * The Enter Power Save command requests the device to go into power
794 * saving mode. The device will ack or nak the command depending on it
795 * being ready for it. If it acks, we tell the USB subsystem to
797 * As well, the device might request to go into power saving mode by
798 * sending a report (REPORT_POWERSAVE_READY), in which case, we issue
799 * this command. The hookups in the RX coder allow
802 I2400M_WAKEUP_ENABLED = 0x01,
803 I2400M_WAKEUP_DISABLED = 0x02,
804 I2400M_TLV_TYPE_WAKEUP_MODE = 144,
807 struct i2400m_cmd_enter_power_save {
808 struct i2400m_l3l4_hdr hdr;
809 struct i2400m_tlv_hdr tlv;
811 } __attribute__((packed));
815 * Request entering power save
817 * This command is (mainly) executed when the device indicates that it
818 * is ready to go into powersave mode via a REPORT_POWERSAVE_READY.
820 int i2400m_cmd_enter_powersave(struct i2400m *i2400m)
823 struct device *dev = i2400m_dev(i2400m);
824 struct sk_buff *ack_skb;
825 struct i2400m_cmd_enter_power_save *cmd;
829 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
832 cmd->hdr.type = cpu_to_le16(I2400M_MT_CMD_ENTER_POWERSAVE);
833 cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
834 cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
835 cmd->tlv.type = cpu_to_le16(I2400M_TLV_TYPE_WAKEUP_MODE);
836 cmd->tlv.length = cpu_to_le16(sizeof(cmd->val));
837 cmd->val = cpu_to_le32(I2400M_WAKEUP_ENABLED);
839 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
840 result = PTR_ERR(ack_skb);
841 if (IS_ERR(ack_skb)) {
842 dev_err(dev, "Failed to issue 'Enter power save' command: %d\n",
844 goto error_msg_to_dev;
846 result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
847 strerr, sizeof(strerr));
848 if (result == -EACCES)
849 d_printf(1, dev, "Cannot enter power save mode\n");
851 dev_err(dev, "'Enter power save' (0x%04x) command failed: "
852 "%d - %s\n", I2400M_MT_CMD_ENTER_POWERSAVE,
855 d_printf(1, dev, "device ready to power save\n");
862 EXPORT_SYMBOL_GPL(i2400m_cmd_enter_powersave);
866 * Definitions for getting device information
869 I2400M_TLV_DETAILED_DEVICE_INFO = 140
873 * i2400m_get_device_info - Query the device for detailed device information
875 * @i2400m: device descriptor
877 * Returns: an skb whose skb->data points to a 'struct
878 * i2400m_tlv_detailed_device_info'. When done, kfree_skb() it. The
879 * skb is *guaranteed* to contain the whole TLV data structure.
881 * On error, IS_ERR(skb) is true and ERR_PTR(skb) is the error
884 struct sk_buff *i2400m_get_device_info(struct i2400m *i2400m)
887 struct device *dev = i2400m_dev(i2400m);
888 struct sk_buff *ack_skb;
889 struct i2400m_l3l4_hdr *cmd;
890 const struct i2400m_l3l4_hdr *ack;
892 const struct i2400m_tlv_hdr *tlv;
893 const struct i2400m_tlv_detailed_device_info *ddi;
896 ack_skb = ERR_PTR(-ENOMEM);
897 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
900 cmd->type = cpu_to_le16(I2400M_MT_GET_DEVICE_INFO);
902 cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
904 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
905 if (IS_ERR(ack_skb)) {
906 dev_err(dev, "Failed to issue 'get device info' command: %ld\n",
908 goto error_msg_to_dev;
910 ack = wimax_msg_data_len(ack_skb, &ack_len);
911 result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
913 dev_err(dev, "'get device info' (0x%04x) command failed: "
914 "%d - %s\n", I2400M_MT_GET_DEVICE_INFO, result,
916 goto error_cmd_failed;
918 tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
919 I2400M_TLV_DETAILED_DEVICE_INFO, sizeof(*ddi));
921 dev_err(dev, "GET DEVICE INFO: "
922 "detailed device info TLV not found (0x%04x)\n",
923 I2400M_TLV_DETAILED_DEVICE_INFO);
927 skb_pull(ack_skb, (void *) tlv - (void *) ack_skb->data);
937 return ERR_PTR(result);
941 /* Firmware interface versions we support */
943 I2400M_HDIv_MAJOR = 9,
944 I2400M_HDIv_MINOR = 1,
945 I2400M_HDIv_MINOR_2 = 2,
950 * i2400m_firmware_check - check firmware versions are compatible with
953 * @i2400m: device descriptor
955 * Returns: 0 if ok, < 0 errno code an error and a message in the
958 * Long function, but quite simple; first chunk launches the command
959 * and double checks the reply for the right TLV. Then we process the
960 * TLV (where the meat is).
962 * Once we process the TLV that gives us the firmware's interface
963 * version, we encode it and save it in i2400m->fw_version for future
966 int i2400m_firmware_check(struct i2400m *i2400m)
969 struct device *dev = i2400m_dev(i2400m);
970 struct sk_buff *ack_skb;
971 struct i2400m_l3l4_hdr *cmd;
972 const struct i2400m_l3l4_hdr *ack;
974 const struct i2400m_tlv_hdr *tlv;
975 const struct i2400m_tlv_l4_message_versions *l4mv;
977 unsigned major, minor, branch;
980 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
983 cmd->type = cpu_to_le16(I2400M_MT_GET_LM_VERSION);
985 cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
987 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
988 if (IS_ERR(ack_skb)) {
989 result = PTR_ERR(ack_skb);
990 dev_err(dev, "Failed to issue 'get lm version' command: %-d\n",
992 goto error_msg_to_dev;
994 ack = wimax_msg_data_len(ack_skb, &ack_len);
995 result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
997 dev_err(dev, "'get lm version' (0x%04x) command failed: "
998 "%d - %s\n", I2400M_MT_GET_LM_VERSION, result,
1000 goto error_cmd_failed;
1002 tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
1003 I2400M_TLV_L4_MESSAGE_VERSIONS, sizeof(*l4mv));
1005 dev_err(dev, "get lm version: TLV not found (0x%04x)\n",
1006 I2400M_TLV_L4_MESSAGE_VERSIONS);
1010 l4mv = container_of(tlv, typeof(*l4mv), hdr);
1011 major = le16_to_cpu(l4mv->major);
1012 minor = le16_to_cpu(l4mv->minor);
1013 branch = le16_to_cpu(l4mv->branch);
1015 if (major != I2400M_HDIv_MAJOR) {
1016 dev_err(dev, "unsupported major fw version "
1017 "%u.%u.%u\n", major, minor, branch);
1018 goto error_bad_major;
1021 if (minor < I2400M_HDIv_MINOR_2 && minor > I2400M_HDIv_MINOR)
1022 dev_warn(dev, "untested minor fw version %u.%u.%u\n",
1023 major, minor, branch);
1024 /* Yes, we ignore the branch -- we don't have to track it */
1025 i2400m->fw_version = major << 16 | minor;
1026 dev_info(dev, "firmware interface version %u.%u.%u\n",
1027 major, minor, branch);
1040 * Send an DoExitIdle command to the device to ask it to go out of
1041 * basestation-idle mode.
1043 * @i2400m: device descriptor
1045 * This starts a renegotiation with the basestation that might involve
1046 * another crypto handshake with user space.
1048 * Returns: 0 if ok, < 0 errno code on error.
1050 int i2400m_cmd_exit_idle(struct i2400m *i2400m)
1053 struct device *dev = i2400m_dev(i2400m);
1054 struct sk_buff *ack_skb;
1055 struct i2400m_l3l4_hdr *cmd;
1059 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1062 cmd->type = cpu_to_le16(I2400M_MT_CMD_EXIT_IDLE);
1064 cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
1066 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
1067 result = PTR_ERR(ack_skb);
1068 if (IS_ERR(ack_skb)) {
1069 dev_err(dev, "Failed to issue 'exit idle' command: %d\n",
1071 goto error_msg_to_dev;
1073 result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
1074 strerr, sizeof(strerr));
1085 * Query the device for its state, update the WiMAX stack's idea of it
1087 * @i2400m: device descriptor
1089 * Returns: 0 if ok, < 0 errno code on error.
1091 * Executes a 'Get State' command and parses the returned
1094 * Because this is almost identical to a 'Report State', we use
1095 * i2400m_report_state_hook() to parse the answer. This will set the
1096 * carrier state, as well as the RF Kill switches state.
1098 int i2400m_cmd_get_state(struct i2400m *i2400m)
1101 struct device *dev = i2400m_dev(i2400m);
1102 struct sk_buff *ack_skb;
1103 struct i2400m_l3l4_hdr *cmd;
1104 const struct i2400m_l3l4_hdr *ack;
1109 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1112 cmd->type = cpu_to_le16(I2400M_MT_GET_STATE);
1114 cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
1116 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
1117 if (IS_ERR(ack_skb)) {
1118 dev_err(dev, "Failed to issue 'get state' command: %ld\n",
1120 result = PTR_ERR(ack_skb);
1121 goto error_msg_to_dev;
1123 ack = wimax_msg_data_len(ack_skb, &ack_len);
1124 result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
1126 dev_err(dev, "'get state' (0x%04x) command failed: "
1127 "%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
1128 goto error_cmd_failed;
1130 i2400m_report_state_hook(i2400m, ack, ack_len - sizeof(*ack),
1140 EXPORT_SYMBOL_GPL(i2400m_cmd_get_state);
1144 * Set basic configuration settings
1146 * @i2400m: device descriptor
1147 * @args: array of pointers to the TLV headers to send for
1148 * configuration (each followed by its payload).
1149 * TLV headers and payloads must be properly initialized, with the
1150 * right endianess (LE).
1151 * @arg_size: number of pointers in the @args array
1153 int i2400m_set_init_config(struct i2400m *i2400m,
1154 const struct i2400m_tlv_hdr **arg, size_t args)
1157 struct device *dev = i2400m_dev(i2400m);
1158 struct sk_buff *ack_skb;
1159 struct i2400m_l3l4_hdr *cmd;
1161 unsigned argc, argsize, tlv_size;
1162 const struct i2400m_tlv_hdr *tlv_hdr;
1165 d_fnstart(3, dev, "(i2400m %p arg %p args %zu)\n", i2400m, arg, args);
1169 /* Compute the size of all the TLVs, so we can alloc a
1170 * contiguous command block to copy them. */
1172 for (argc = 0; argc < args; argc++) {
1173 tlv_hdr = arg[argc];
1174 argsize += sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
1176 WARN_ON(argc >= 9); /* As per hw spec */
1178 /* Alloc the space for the command and TLVs*/
1180 buf = kzalloc(sizeof(*cmd) + argsize, GFP_KERNEL);
1184 cmd->type = cpu_to_le16(I2400M_MT_SET_INIT_CONFIG);
1185 cmd->length = cpu_to_le16(argsize);
1186 cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
1189 itr = buf + sizeof(*cmd);
1190 for (argc = 0; argc < args; argc++) {
1191 tlv_hdr = arg[argc];
1192 tlv_size = sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
1193 memcpy(itr, tlv_hdr, tlv_size);
1197 /* Send the message! */
1198 ack_skb = i2400m_msg_to_dev(i2400m, buf, sizeof(*cmd) + argsize);
1199 result = PTR_ERR(ack_skb);
1200 if (IS_ERR(ack_skb)) {
1201 dev_err(dev, "Failed to issue 'init config' command: %d\n",
1204 goto error_msg_to_dev;
1206 result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
1207 strerr, sizeof(strerr));
1209 dev_err(dev, "'init config' (0x%04x) command failed: %d - %s\n",
1210 I2400M_MT_SET_INIT_CONFIG, result, strerr);
1216 d_fnend(3, dev, "(i2400m %p arg %p args %zu) = %d\n",
1217 i2400m, arg, args, result);
1221 EXPORT_SYMBOL_GPL(i2400m_set_init_config);
1225 * i2400m_set_idle_timeout - Set the device's idle mode timeout
1227 * @i2400m: i2400m device descriptor
1229 * @msecs: milliseconds for the timeout to enter idle mode. Between
1230 * 100 to 300000 (5m); 0 to disable. In increments of 100.
1232 * After this @msecs of the link being idle (no data being sent or
1233 * received), the device will negotiate with the basestation entering
1234 * idle mode for saving power. The connection is maintained, but
1235 * getting out of it (done in tx.c) will require some negotiation,
1236 * possible crypto re-handshake and a possible DHCP re-lease.
1238 * Only available if fw_version >= 0x00090002.
1240 * Returns: 0 if ok, < 0 errno code on error.
1242 int i2400m_set_idle_timeout(struct i2400m *i2400m, unsigned msecs)
1245 struct device *dev = i2400m_dev(i2400m);
1246 struct sk_buff *ack_skb;
1248 struct i2400m_l3l4_hdr hdr;
1249 struct i2400m_tlv_config_idle_timeout cit;
1251 const struct i2400m_l3l4_hdr *ack;
1256 if (i2400m_le_v1_3(i2400m))
1259 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1262 cmd->hdr.type = cpu_to_le16(I2400M_MT_GET_STATE);
1263 cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
1264 cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
1267 cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
1268 cmd->cit.hdr.length = cpu_to_le16(sizeof(cmd->cit.timeout));
1269 cmd->cit.timeout = cpu_to_le32(msecs);
1271 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
1272 if (IS_ERR(ack_skb)) {
1273 dev_err(dev, "Failed to issue 'set idle timeout' command: "
1274 "%ld\n", PTR_ERR(ack_skb));
1275 result = PTR_ERR(ack_skb);
1276 goto error_msg_to_dev;
1278 ack = wimax_msg_data_len(ack_skb, &ack_len);
1279 result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
1281 dev_err(dev, "'set idle timeout' (0x%04x) command failed: "
1282 "%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
1283 goto error_cmd_failed;
1296 * i2400m_dev_initialize - Initialize the device once communications are ready
1298 * @i2400m: device descriptor
1300 * Returns: 0 if ok, < 0 errno code on error.
1302 * Configures the device to work the way we like it.
1304 * At the point of this call, the device is registered with the WiMAX
1305 * and netdev stacks, firmware is uploaded and we can talk to the
1308 int i2400m_dev_initialize(struct i2400m *i2400m)
1311 struct device *dev = i2400m_dev(i2400m);
1312 struct i2400m_tlv_config_idle_parameters idle_params;
1313 struct i2400m_tlv_config_idle_timeout idle_timeout;
1314 struct i2400m_tlv_config_d2h_data_format df;
1315 struct i2400m_tlv_config_dl_host_reorder dlhr;
1316 const struct i2400m_tlv_hdr *args[9];
1319 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
1320 /* Disable idle mode? (enabled by default) */
1321 if (i2400m_idle_mode_disabled) {
1322 if (i2400m_le_v1_3(i2400m)) {
1323 idle_params.hdr.type =
1324 cpu_to_le16(I2400M_TLV_CONFIG_IDLE_PARAMETERS);
1325 idle_params.hdr.length = cpu_to_le16(
1326 sizeof(idle_params) - sizeof(idle_params.hdr));
1327 idle_params.idle_timeout = 0;
1328 idle_params.idle_paging_interval = 0;
1329 args[argc++] = &idle_params.hdr;
1331 idle_timeout.hdr.type =
1332 cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
1333 idle_timeout.hdr.length = cpu_to_le16(
1334 sizeof(idle_timeout) - sizeof(idle_timeout.hdr));
1335 idle_timeout.timeout = 0;
1336 args[argc++] = &idle_timeout.hdr;
1339 if (i2400m_ge_v1_4(i2400m)) {
1340 /* Enable extended RX data format? */
1342 cpu_to_le16(I2400M_TLV_CONFIG_D2H_DATA_FORMAT);
1343 df.hdr.length = cpu_to_le16(
1344 sizeof(df) - sizeof(df.hdr));
1346 args[argc++] = &df.hdr;
1348 /* Enable RX data reordering?
1349 * (switch flipped in rx.c:i2400m_rx_setup() after fw upload) */
1350 if (i2400m->rx_reorder) {
1352 cpu_to_le16(I2400M_TLV_CONFIG_DL_HOST_REORDER);
1353 dlhr.hdr.length = cpu_to_le16(
1354 sizeof(dlhr) - sizeof(dlhr.hdr));
1356 args[argc++] = &dlhr.hdr;
1359 result = i2400m_set_init_config(i2400m, args, argc);
1363 * Update state: Here it just calls a get state; parsing the
1364 * result (System State TLV and RF Status TLV [done in the rx
1365 * path hooks]) will set the hardware and software RF-Kill
1368 result = i2400m_cmd_get_state(i2400m);
1371 dev_err(dev, "failed to initialize the device: %d\n", result);
1372 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
1378 * i2400m_dev_shutdown - Shutdown a running device
1380 * @i2400m: device descriptor
1382 * Gracefully stops the device, moving it to the lowest power
1383 * consumption state possible.
1385 void i2400m_dev_shutdown(struct i2400m *i2400m)
1387 int result = -ENODEV;
1388 struct device *dev = i2400m_dev(i2400m);
1390 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
1391 result = i2400m->bus_reset(i2400m, I2400M_RT_WARM);
1392 d_fnend(3, dev, "(i2400m %p) = void [%d]\n", i2400m, result);