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_firmware_check()
56 * i2400m_cmd_get_state()
57 * i2400m_dev_shutdown() Called by i2400m_dev_stop()
60 * i2400m_{cmd,get,set}_*()
62 * i2400m_msg_check_status()
64 * i2400m_report_hook() Called on reception of an event
65 * i2400m_report_state_hook()
66 * i2400m_tlv_buffer_walk()
68 * i2400m_report_tlv_system_state()
69 * i2400m_report_tlv_rf_switches_status()
70 * i2400m_report_tlv_media_status()
71 * i2400m_cmd_enter_powersave()
73 * i2400m_msg_ack_hook() Called on reception of a reply to a
79 #include <linux/kernel.h>
80 #include <linux/wimax/i2400m.h>
83 #define D_SUBMODULE control
84 #include "debug-levels.h"
88 * Return if a TLV is of a give type and size
90 * @tlv_hdr: pointer to the TLV
91 * @tlv_type: type of the TLV we are looking for
92 * @tlv_size: expected size of the TLV we are looking for (if -1,
93 * don't check the size). This includes the header
94 * Returns: 0 if the TLV matches
95 * < 0 if it doesn't match at all
96 * > 0 total TLV + payload size, if the type matches, but not
100 ssize_t
i2400m_tlv_match(const struct i2400m_tlv_hdr
*tlv
,
101 enum i2400m_tlv tlv_type
, ssize_t tlv_size
)
103 if (le16_to_cpu(tlv
->type
) != tlv_type
) /* Not our type? skip */
106 && le16_to_cpu(tlv
->length
) + sizeof(*tlv
) != tlv_size
) {
107 size_t size
= le16_to_cpu(tlv
->length
) + sizeof(*tlv
);
108 printk(KERN_WARNING
"W: tlv type 0x%x mismatched because of "
109 "size (got %zu vs %zu expected)\n",
110 tlv_type
, size
, tlv_size
);
118 * Given a buffer of TLVs, iterate over them
120 * @i2400m: device instance
121 * @tlv_buf: pointer to the beginning of the TLV buffer
122 * @buf_size: buffer size in bytes
123 * @tlv_pos: seek position; this is assumed to be a pointer returned
124 * by i2400m_tlv_buffer_walk() [and thus, validated]. The
125 * TLV returned will be the one following this one.
130 * while (tlv_itr = i2400m_tlv_buffer_walk(i2400m, buf, size, tlv_itr)) {
132 * // Do stuff with tlv_itr, DON'T MODIFY IT
137 const struct i2400m_tlv_hdr
*i2400m_tlv_buffer_walk(
138 struct i2400m
*i2400m
,
139 const void *tlv_buf
, size_t buf_size
,
140 const struct i2400m_tlv_hdr
*tlv_pos
)
142 struct device
*dev
= i2400m_dev(i2400m
);
143 const struct i2400m_tlv_hdr
*tlv_top
= tlv_buf
+ buf_size
;
144 size_t offset
, length
, avail_size
;
147 if (tlv_pos
== NULL
) /* Take the first one? */
149 else /* Nope, the next one */
150 tlv_pos
= (void *) tlv_pos
151 + le16_to_cpu(tlv_pos
->length
) + sizeof(*tlv_pos
);
152 if (tlv_pos
== tlv_top
) { /* buffer done */
154 goto error_beyond_end
;
156 if (tlv_pos
> tlv_top
) {
159 goto error_beyond_end
;
161 offset
= (void *) tlv_pos
- (void *) tlv_buf
;
162 avail_size
= buf_size
- offset
;
163 if (avail_size
< sizeof(*tlv_pos
)) {
164 dev_err(dev
, "HW BUG? tlv_buf %p [%zu bytes], tlv @%zu: "
165 "short header\n", tlv_buf
, buf_size
, offset
);
166 goto error_short_header
;
168 type
= le16_to_cpu(tlv_pos
->type
);
169 length
= le16_to_cpu(tlv_pos
->length
);
170 if (avail_size
< sizeof(*tlv_pos
) + length
) {
171 dev_err(dev
, "HW BUG? tlv_buf %p [%zu bytes], "
172 "tlv type 0x%04x @%zu: "
173 "short data (%zu bytes vs %zu needed)\n",
174 tlv_buf
, buf_size
, type
, offset
, avail_size
,
175 sizeof(*tlv_pos
) + length
);
176 goto error_short_header
;
185 * Find a TLV in a buffer of sequential TLVs
187 * @i2400m: device descriptor
188 * @tlv_hdr: pointer to the first TLV in the sequence
189 * @size: size of the buffer in bytes; all TLVs are assumed to fit
190 * fully in the buffer (otherwise we'll complain).
191 * @tlv_type: type of the TLV we are looking for
192 * @tlv_size: expected size of the TLV we are looking for (if -1,
193 * don't check the size). This includes the header
195 * Returns: NULL if the TLV is not found, otherwise a pointer to
196 * it. If the sizes don't match, an error is printed and NULL
200 const struct i2400m_tlv_hdr
*i2400m_tlv_find(
201 struct i2400m
*i2400m
,
202 const struct i2400m_tlv_hdr
*tlv_hdr
, size_t size
,
203 enum i2400m_tlv tlv_type
, ssize_t tlv_size
)
206 struct device
*dev
= i2400m_dev(i2400m
);
207 const struct i2400m_tlv_hdr
*tlv
= NULL
;
208 while ((tlv
= i2400m_tlv_buffer_walk(i2400m
, tlv_hdr
, size
, tlv
))) {
209 match
= i2400m_tlv_match(tlv
, tlv_type
, tlv_size
);
210 if (match
== 0) /* found it :) */
213 dev_warn(dev
, "TLV type 0x%04x found with size "
214 "mismatch (%zu vs %zu needed)\n",
215 tlv_type
, match
, tlv_size
);
225 } ms_to_errno
[I2400M_MS_MAX
] = {
226 [I2400M_MS_DONE_OK
] = { "", 0 },
227 [I2400M_MS_DONE_IN_PROGRESS
] = { "", 0 },
228 [I2400M_MS_INVALID_OP
] = { "invalid opcode", -ENOSYS
},
229 [I2400M_MS_BAD_STATE
] = { "invalid state", -EILSEQ
},
230 [I2400M_MS_ILLEGAL_VALUE
] = { "illegal value", -EINVAL
},
231 [I2400M_MS_MISSING_PARAMS
] = { "missing parameters", -ENOMSG
},
232 [I2400M_MS_VERSION_ERROR
] = { "bad version", -EIO
},
233 [I2400M_MS_ACCESSIBILITY_ERROR
] = { "accesibility error", -EIO
},
234 [I2400M_MS_BUSY
] = { "busy", -EBUSY
},
235 [I2400M_MS_CORRUPTED_TLV
] = { "corrupted TLV", -EILSEQ
},
236 [I2400M_MS_UNINITIALIZED
] = { "not unitialized", -EILSEQ
},
237 [I2400M_MS_UNKNOWN_ERROR
] = { "unknown error", -EIO
},
238 [I2400M_MS_PRODUCTION_ERROR
] = { "production error", -EIO
},
239 [I2400M_MS_NO_RF
] = { "no RF", -EIO
},
240 [I2400M_MS_NOT_READY_FOR_POWERSAVE
] =
241 { "not ready for powersave", -EACCES
},
242 [I2400M_MS_THERMAL_CRITICAL
] = { "thermal critical", -EL3HLT
},
247 * i2400m_msg_check_status - translate a message's status code
249 * @i2400m: device descriptor
250 * @l3l4_hdr: message header
251 * @strbuf: buffer to place a formatted error message (unless NULL).
252 * @strbuf_size: max amount of available space; larger messages will
255 * Returns: errno code corresponding to the status code in @l3l4_hdr
256 * and a message in @strbuf describing the error.
258 int i2400m_msg_check_status(const struct i2400m_l3l4_hdr
*l3l4_hdr
,
259 char *strbuf
, size_t strbuf_size
)
262 enum i2400m_ms status
= le16_to_cpu(l3l4_hdr
->status
);
267 if (status
> ARRAY_SIZE(ms_to_errno
)) {
268 str
= "unknown status code";
271 str
= ms_to_errno
[status
].msg
;
272 result
= ms_to_errno
[status
].errno
;
275 snprintf(strbuf
, strbuf_size
, "%s (%d)", str
, status
);
281 * Act on a TLV System State reported by the device
283 * @i2400m: device descriptor
284 * @ss: validated System State TLV
287 void i2400m_report_tlv_system_state(struct i2400m
*i2400m
,
288 const struct i2400m_tlv_system_state
*ss
)
290 struct device
*dev
= i2400m_dev(i2400m
);
291 struct wimax_dev
*wimax_dev
= &i2400m
->wimax_dev
;
292 enum i2400m_system_state i2400m_state
= le32_to_cpu(ss
->state
);
294 d_fnstart(3, dev
, "(i2400m %p ss %p [%u])\n", i2400m
, ss
, i2400m_state
);
296 if (unlikely(i2400m
->ready
== 0)) /* act if up */
298 if (i2400m
->state
!= i2400m_state
) {
299 i2400m
->state
= i2400m_state
;
300 wake_up_all(&i2400m
->state_wq
);
302 switch (i2400m_state
) {
303 case I2400M_SS_UNINITIALIZED
:
305 case I2400M_SS_CONFIG
:
306 case I2400M_SS_PRODUCTION
:
307 wimax_state_change(wimax_dev
, WIMAX_ST_UNINITIALIZED
);
310 case I2400M_SS_RF_OFF
:
311 case I2400M_SS_RF_SHUTDOWN
:
312 wimax_state_change(wimax_dev
, WIMAX_ST_RADIO_OFF
);
315 case I2400M_SS_READY
:
316 case I2400M_SS_STANDBY
:
317 case I2400M_SS_SLEEPACTIVE
:
318 wimax_state_change(wimax_dev
, WIMAX_ST_READY
);
321 case I2400M_SS_CONNECTING
:
322 case I2400M_SS_WIMAX_CONNECTED
:
323 wimax_state_change(wimax_dev
, WIMAX_ST_READY
);
327 case I2400M_SS_OUT_OF_ZONE
:
328 wimax_state_change(wimax_dev
, WIMAX_ST_SCANNING
);
332 d_printf(1, dev
, "entering BS-negotiated idle mode\n");
333 case I2400M_SS_DISCONNECTING
:
334 case I2400M_SS_DATA_PATH_CONNECTED
:
335 wimax_state_change(wimax_dev
, WIMAX_ST_CONNECTED
);
339 /* Huh? just in case, shut it down */
340 dev_err(dev
, "HW BUG? unknown state %u: shutting down\n",
342 i2400m
->bus_reset(i2400m
, I2400M_RT_WARM
);
346 d_fnend(3, dev
, "(i2400m %p ss %p [%u]) = void\n",
347 i2400m
, ss
, i2400m_state
);
352 * Parse and act on a TLV Media Status sent by the device
354 * @i2400m: device descriptor
355 * @ms: validated Media Status TLV
357 * This will set the carrier up on down based on the device's link
358 * report. This is done asides of what the WiMAX stack does based on
359 * the device's state as sometimes we need to do a link-renew (the BS
360 * wants us to renew a DHCP lease, for example).
362 * In fact, doc says that everytime we get a link-up, we should do a
363 * DHCP negotiation...
366 void i2400m_report_tlv_media_status(struct i2400m
*i2400m
,
367 const struct i2400m_tlv_media_status
*ms
)
369 struct device
*dev
= i2400m_dev(i2400m
);
370 struct wimax_dev
*wimax_dev
= &i2400m
->wimax_dev
;
371 struct net_device
*net_dev
= wimax_dev
->net_dev
;
372 enum i2400m_media_status status
= le32_to_cpu(ms
->media_status
);
374 d_fnstart(3, dev
, "(i2400m %p ms %p [%u])\n", i2400m
, ms
, status
);
376 if (unlikely(i2400m
->ready
== 0)) /* act if up */
379 case I2400M_MEDIA_STATUS_LINK_UP
:
380 netif_carrier_on(net_dev
);
382 case I2400M_MEDIA_STATUS_LINK_DOWN
:
383 netif_carrier_off(net_dev
);
386 * This is the network telling us we need to retrain the DHCP
387 * lease -- so far, we are trusting the WiMAX Network Service
388 * in user space to pick this up and poke the DHCP client.
390 case I2400M_MEDIA_STATUS_LINK_RENEW
:
391 netif_carrier_on(net_dev
);
394 dev_err(dev
, "HW BUG? unknown media status %u\n",
398 d_fnend(3, dev
, "(i2400m %p ms %p [%u]) = void\n",
404 * Parse a 'state report' and extract carrier on/off information
406 * @i2400m: device descriptor
407 * @l3l4_hdr: pointer to message; it has been already validated for
409 * @size: size of the message (header + payload). The header length
410 * declaration is assumed to be congruent with @size (as in
411 * sizeof(*l3l4_hdr) + l3l4_hdr->length == size)
413 * Extract from the report state the system state TLV and infer from
414 * there if we have a carrier or not. Update our local state and tell
417 * When setting the carrier, it's fine to set OFF twice (for example),
418 * as netif_carrier_off() will not generate two OFF events (just on
422 void i2400m_report_state_hook(struct i2400m
*i2400m
,
423 const struct i2400m_l3l4_hdr
*l3l4_hdr
,
424 size_t size
, const char *tag
)
426 struct device
*dev
= i2400m_dev(i2400m
);
427 const struct i2400m_tlv_hdr
*tlv
;
428 const struct i2400m_tlv_system_state
*ss
;
429 const struct i2400m_tlv_rf_switches_status
*rfss
;
430 const struct i2400m_tlv_media_status
*ms
;
431 size_t tlv_size
= le16_to_cpu(l3l4_hdr
->length
);
433 d_fnstart(4, dev
, "(i2400m %p, l3l4_hdr %p, size %zu, %s)\n",
434 i2400m
, l3l4_hdr
, size
, tag
);
437 while ((tlv
= i2400m_tlv_buffer_walk(i2400m
, &l3l4_hdr
->pl
,
439 if (0 == i2400m_tlv_match(tlv
, I2400M_TLV_SYSTEM_STATE
,
441 ss
= container_of(tlv
, typeof(*ss
), hdr
);
442 d_printf(2, dev
, "%s: system state TLV "
443 "found (0x%04x), state 0x%08x\n",
444 tag
, I2400M_TLV_SYSTEM_STATE
,
445 le32_to_cpu(ss
->state
));
446 i2400m_report_tlv_system_state(i2400m
, ss
);
448 if (0 == i2400m_tlv_match(tlv
, I2400M_TLV_RF_STATUS
,
450 rfss
= container_of(tlv
, typeof(*rfss
), hdr
);
451 d_printf(2, dev
, "%s: RF status TLV "
452 "found (0x%04x), sw 0x%02x hw 0x%02x\n",
453 tag
, I2400M_TLV_RF_STATUS
,
454 le32_to_cpu(rfss
->sw_rf_switch
),
455 le32_to_cpu(rfss
->hw_rf_switch
));
456 i2400m_report_tlv_rf_switches_status(i2400m
, rfss
);
458 if (0 == i2400m_tlv_match(tlv
, I2400M_TLV_MEDIA_STATUS
,
460 ms
= container_of(tlv
, typeof(*ms
), hdr
);
461 d_printf(2, dev
, "%s: Media Status TLV: %u\n",
462 tag
, le32_to_cpu(ms
->media_status
));
463 i2400m_report_tlv_media_status(i2400m
, ms
);
466 d_fnend(4, dev
, "(i2400m %p, l3l4_hdr %p, size %zu, %s) = void\n",
467 i2400m
, l3l4_hdr
, size
, tag
);
472 * i2400m_report_hook - (maybe) act on a report
474 * @i2400m: device descriptor
475 * @l3l4_hdr: pointer to message; it has been already validated for
477 * @size: size of the message (header + payload). The header length
478 * declaration is assumed to be congruent with @size (as in
479 * sizeof(*l3l4_hdr) + l3l4_hdr->length == size)
481 * Extract information we might need (like carrien on/off) from a
484 void i2400m_report_hook(struct i2400m
*i2400m
,
485 const struct i2400m_l3l4_hdr
*l3l4_hdr
, size_t size
)
487 struct device
*dev
= i2400m_dev(i2400m
);
490 d_fnstart(3, dev
, "(i2400m %p l3l4_hdr %p size %zu)\n",
491 i2400m
, l3l4_hdr
, size
);
492 /* Chew on the message, we might need some information from
494 msg_type
= le16_to_cpu(l3l4_hdr
->type
);
496 case I2400M_MT_REPORT_STATE
: /* carrier detection... */
497 i2400m_report_state_hook(i2400m
,
498 l3l4_hdr
, size
, "REPORT STATE");
500 /* If the device is ready for power save, then ask it to do
502 case I2400M_MT_REPORT_POWERSAVE_READY
: /* zzzzz */
503 if (l3l4_hdr
->status
== cpu_to_le16(I2400M_MS_DONE_OK
)) {
504 d_printf(1, dev
, "ready for powersave, requesting\n");
505 i2400m_cmd_enter_powersave(i2400m
);
509 d_fnend(3, dev
, "(i2400m %p l3l4_hdr %p size %zu) = void\n",
510 i2400m
, l3l4_hdr
, size
);
515 * i2400m_msg_ack_hook - process cmd/set/get ack for internal status
517 * @i2400m: device descriptor
518 * @l3l4_hdr: pointer to message; it has been already validated for
520 * @size: size of the message
522 * Extract information we might need from acks to commands and act on
523 * it. This is akin to i2400m_report_hook(). Note most of this
524 * processing should be done in the function that calls the
525 * command. This is here for some cases where it can't happen...
527 void i2400m_msg_ack_hook(struct i2400m
*i2400m
,
528 const struct i2400m_l3l4_hdr
*l3l4_hdr
, size_t size
)
531 struct device
*dev
= i2400m_dev(i2400m
);
532 unsigned ack_type
, ack_status
;
535 /* Chew on the message, we might need some information from
537 ack_type
= le16_to_cpu(l3l4_hdr
->type
);
538 ack_status
= le16_to_cpu(l3l4_hdr
->status
);
540 case I2400M_MT_CMD_ENTER_POWERSAVE
:
541 /* This is just left here for the sake of example, as
542 * the processing is done somewhere else. */
544 result
= i2400m_msg_check_status(
545 l3l4_hdr
, strerr
, sizeof(strerr
));
547 d_printf(1, dev
, "ready for power save: %zd\n",
557 * i2400m_msg_size_check() - verify message size and header are congruent
559 * It is ok if the total message size is larger than the expected
560 * size, as there can be padding.
562 int i2400m_msg_size_check(struct i2400m
*i2400m
,
563 const struct i2400m_l3l4_hdr
*l3l4_hdr
,
567 struct device
*dev
= i2400m_dev(i2400m
);
568 size_t expected_size
;
569 d_fnstart(4, dev
, "(i2400m %p l3l4_hdr %p msg_size %zu)\n",
570 i2400m
, l3l4_hdr
, msg_size
);
571 if (msg_size
< sizeof(*l3l4_hdr
)) {
572 dev_err(dev
, "bad size for message header "
573 "(expected at least %zu, got %zu)\n",
574 (size_t) sizeof(*l3l4_hdr
), msg_size
);
578 expected_size
= le16_to_cpu(l3l4_hdr
->length
) + sizeof(*l3l4_hdr
);
579 if (msg_size
< expected_size
) {
580 dev_err(dev
, "bad size for message code 0x%04x (expected %zu, "
581 "got %zu)\n", le16_to_cpu(l3l4_hdr
->type
),
582 expected_size
, msg_size
);
588 "(i2400m %p l3l4_hdr %p msg_size %zu) = %d\n",
589 i2400m
, l3l4_hdr
, msg_size
, result
);
596 * Cancel a wait for a command ACK
598 * @i2400m: device descriptor
599 * @code: [negative] errno code to cancel with (don't use
602 * If there is an ack already filled out, free it.
604 void i2400m_msg_to_dev_cancel_wait(struct i2400m
*i2400m
, int code
)
606 struct sk_buff
*ack_skb
;
609 spin_lock_irqsave(&i2400m
->rx_lock
, flags
);
610 ack_skb
= i2400m
->ack_skb
;
611 if (ack_skb
&& !IS_ERR(ack_skb
))
613 i2400m
->ack_skb
= ERR_PTR(code
);
614 spin_unlock_irqrestore(&i2400m
->rx_lock
, flags
);
619 * i2400m_msg_to_dev - Send a control message to the device and get a response
621 * @i2400m: device descriptor
625 * @buf: pointer to the buffer containing the message to be sent; it
626 * has to start with a &struct i2400M_l3l4_hdr and then
627 * followed by the payload. Once this function returns, the
628 * buffer can be reused.
630 * @buf_len: buffer size
634 * Pointer to skb containing the ack message. You need to check the
635 * pointer with IS_ERR(), as it might be an error code. Error codes
636 * could happen because:
638 * - the message wasn't formatted correctly
639 * - couldn't send the message
640 * - failed waiting for a response
641 * - the ack message wasn't formatted correctly
643 * The returned skb has been allocated with wimax_msg_to_user_alloc(),
644 * it contains the reponse in a netlink attribute and is ready to be
645 * passed up to user space with wimax_msg_to_user_send(). To access
646 * the payload and its length, use wimax_msg_{data,len}() on the skb.
648 * The skb has to be freed with kfree_skb() once done.
652 * This function delivers a message/command to the device and waits
653 * for an ack to be received. The format is described in
654 * linux/wimax/i2400m.h. In summary, a command/get/set is followed by an
657 * This function will not check the ack status, that's left up to the
658 * caller. Once done with the ack skb, it has to be kfree_skb()ed.
660 * The i2400m handles only one message at the same time, thus we need
661 * the mutex to exclude other players.
663 * We write the message and then wait for an answer to come back. The
664 * RX path intercepts control messages and handles them in
665 * i2400m_rx_ctl(). Reports (notifications) are (maybe) processed
666 * locally and then forwarded (as needed) to user space on the WiMAX
667 * stack message pipe. Acks are saved and passed back to us through an
668 * skb in i2400m->ack_skb which is ready to be given to generic
669 * netlink if need be.
671 struct sk_buff
*i2400m_msg_to_dev(struct i2400m
*i2400m
,
672 const void *buf
, size_t buf_len
)
675 struct device
*dev
= i2400m_dev(i2400m
);
676 const struct i2400m_l3l4_hdr
*msg_l3l4_hdr
;
677 struct sk_buff
*ack_skb
;
678 const struct i2400m_l3l4_hdr
*ack_l3l4_hdr
;
684 d_fnstart(3, dev
, "(i2400m %p buf %p len %zu)\n",
685 i2400m
, buf
, buf_len
);
687 if (i2400m
->boot_mode
)
688 return ERR_PTR(-ENODEV
);
691 /* Check msg & payload consistency */
692 result
= i2400m_msg_size_check(i2400m
, msg_l3l4_hdr
, buf_len
);
695 msg_type
= le16_to_cpu(msg_l3l4_hdr
->type
);
696 d_printf(1, dev
, "CMD/GET/SET 0x%04x %zu bytes\n",
698 d_dump(2, dev
, buf
, buf_len
);
700 /* Setup the completion, ack_skb ("we are waiting") and send
701 * the message to the device */
702 mutex_lock(&i2400m
->msg_mutex
);
703 spin_lock_irqsave(&i2400m
->rx_lock
, flags
);
704 i2400m
->ack_skb
= ERR_PTR(-EINPROGRESS
);
705 spin_unlock_irqrestore(&i2400m
->rx_lock
, flags
);
706 init_completion(&i2400m
->msg_completion
);
707 result
= i2400m_tx(i2400m
, buf
, buf_len
, I2400M_PT_CTRL
);
709 dev_err(dev
, "can't send message 0x%04x: %d\n",
710 le16_to_cpu(msg_l3l4_hdr
->type
), result
);
714 /* Some commands take longer to execute because of crypto ops,
715 * so we give them some more leeway on timeout */
717 case I2400M_MT_GET_TLS_OPERATION_RESULT
:
718 case I2400M_MT_CMD_SEND_EAP_RESPONSE
:
719 ack_timeout
= 5 * HZ
;
725 /* The RX path in rx.c will put any response for this message
726 * in i2400m->ack_skb and wake us up. If we cancel the wait,
727 * we need to change the value of i2400m->ack_skb to something
728 * not -EINPROGRESS so RX knows there is no one waiting. */
729 result
= wait_for_completion_interruptible_timeout(
730 &i2400m
->msg_completion
, ack_timeout
);
732 dev_err(dev
, "timeout waiting for reply to message 0x%04x\n",
735 i2400m_msg_to_dev_cancel_wait(i2400m
, result
);
736 goto error_wait_for_completion
;
737 } else if (result
< 0) {
738 dev_err(dev
, "error waiting for reply to message 0x%04x: %d\n",
740 i2400m_msg_to_dev_cancel_wait(i2400m
, result
);
741 goto error_wait_for_completion
;
744 /* Pull out the ack data from i2400m->ack_skb -- see if it is
745 * an error and act accordingly */
746 spin_lock_irqsave(&i2400m
->rx_lock
, flags
);
747 ack_skb
= i2400m
->ack_skb
;
749 result
= PTR_ERR(ack_skb
);
752 i2400m
->ack_skb
= NULL
;
753 spin_unlock_irqrestore(&i2400m
->rx_lock
, flags
);
755 goto error_ack_status
;
756 ack_l3l4_hdr
= wimax_msg_data_len(ack_skb
, &ack_len
);
758 /* Check the ack and deliver it if it is ok */
759 result
= i2400m_msg_size_check(i2400m
, ack_l3l4_hdr
, ack_len
);
761 dev_err(dev
, "HW BUG? reply to message 0x%04x: %d\n",
763 goto error_bad_ack_len
;
765 if (msg_type
!= le16_to_cpu(ack_l3l4_hdr
->type
)) {
766 dev_err(dev
, "HW BUG? bad reply 0x%04x to message 0x%04x\n",
767 le16_to_cpu(ack_l3l4_hdr
->type
), msg_type
);
769 goto error_bad_ack_type
;
771 i2400m_msg_ack_hook(i2400m
, ack_l3l4_hdr
, ack_len
);
772 mutex_unlock(&i2400m
->msg_mutex
);
773 d_fnend(3, dev
, "(i2400m %p buf %p len %zu) = %p\n",
774 i2400m
, buf
, buf_len
, ack_skb
);
781 error_wait_for_completion
:
783 mutex_unlock(&i2400m
->msg_mutex
);
785 d_fnend(3, dev
, "(i2400m %p buf %p len %zu) = %d\n",
786 i2400m
, buf
, buf_len
, result
);
787 return ERR_PTR(result
);
792 * Definitions for the Enter Power Save command
794 * The Enter Power Save command requests the device to go into power
795 * saving mode. The device will ack or nak the command depending on it
796 * being ready for it. If it acks, we tell the USB subsystem to
798 * As well, the device might request to go into power saving mode by
799 * sending a report (REPORT_POWERSAVE_READY), in which case, we issue
800 * this command. The hookups in the RX coder allow
803 I2400M_WAKEUP_ENABLED
= 0x01,
804 I2400M_WAKEUP_DISABLED
= 0x02,
805 I2400M_TLV_TYPE_WAKEUP_MODE
= 144,
808 struct i2400m_cmd_enter_power_save
{
809 struct i2400m_l3l4_hdr hdr
;
810 struct i2400m_tlv_hdr tlv
;
812 } __attribute__((packed
));
816 * Request entering power save
818 * This command is (mainly) executed when the device indicates that it
819 * is ready to go into powersave mode via a REPORT_POWERSAVE_READY.
821 int i2400m_cmd_enter_powersave(struct i2400m
*i2400m
)
824 struct device
*dev
= i2400m_dev(i2400m
);
825 struct sk_buff
*ack_skb
;
826 struct i2400m_cmd_enter_power_save
*cmd
;
830 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
833 cmd
->hdr
.type
= cpu_to_le16(I2400M_MT_CMD_ENTER_POWERSAVE
);
834 cmd
->hdr
.length
= cpu_to_le16(sizeof(*cmd
) - sizeof(cmd
->hdr
));
835 cmd
->hdr
.version
= cpu_to_le16(I2400M_L3L4_VERSION
);
836 cmd
->tlv
.type
= cpu_to_le16(I2400M_TLV_TYPE_WAKEUP_MODE
);
837 cmd
->tlv
.length
= cpu_to_le16(sizeof(cmd
->val
));
838 cmd
->val
= cpu_to_le32(I2400M_WAKEUP_ENABLED
);
840 ack_skb
= i2400m_msg_to_dev(i2400m
, cmd
, sizeof(*cmd
));
841 result
= PTR_ERR(ack_skb
);
842 if (IS_ERR(ack_skb
)) {
843 dev_err(dev
, "Failed to issue 'Enter power save' command: %d\n",
845 goto error_msg_to_dev
;
847 result
= i2400m_msg_check_status(wimax_msg_data(ack_skb
),
848 strerr
, sizeof(strerr
));
849 if (result
== -EACCES
)
850 d_printf(1, dev
, "Cannot enter power save mode\n");
852 dev_err(dev
, "'Enter power save' (0x%04x) command failed: "
853 "%d - %s\n", I2400M_MT_CMD_ENTER_POWERSAVE
,
856 d_printf(1, dev
, "device ready to power save\n");
863 EXPORT_SYMBOL_GPL(i2400m_cmd_enter_powersave
);
867 * Definitions for getting device information
870 I2400M_TLV_DETAILED_DEVICE_INFO
= 140
874 * i2400m_get_device_info - Query the device for detailed device information
876 * @i2400m: device descriptor
878 * Returns: an skb whose skb->data points to a 'struct
879 * i2400m_tlv_detailed_device_info'. When done, kfree_skb() it. The
880 * skb is *guaranteed* to contain the whole TLV data structure.
882 * On error, IS_ERR(skb) is true and ERR_PTR(skb) is the error
885 struct sk_buff
*i2400m_get_device_info(struct i2400m
*i2400m
)
888 struct device
*dev
= i2400m_dev(i2400m
);
889 struct sk_buff
*ack_skb
;
890 struct i2400m_l3l4_hdr
*cmd
;
891 const struct i2400m_l3l4_hdr
*ack
;
893 const struct i2400m_tlv_hdr
*tlv
;
894 const struct i2400m_tlv_detailed_device_info
*ddi
;
897 ack_skb
= ERR_PTR(-ENOMEM
);
898 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
901 cmd
->type
= cpu_to_le16(I2400M_MT_GET_DEVICE_INFO
);
903 cmd
->version
= cpu_to_le16(I2400M_L3L4_VERSION
);
905 ack_skb
= i2400m_msg_to_dev(i2400m
, cmd
, sizeof(*cmd
));
906 if (IS_ERR(ack_skb
)) {
907 dev_err(dev
, "Failed to issue 'get device info' command: %ld\n",
909 goto error_msg_to_dev
;
911 ack
= wimax_msg_data_len(ack_skb
, &ack_len
);
912 result
= i2400m_msg_check_status(ack
, strerr
, sizeof(strerr
));
914 dev_err(dev
, "'get device info' (0x%04x) command failed: "
915 "%d - %s\n", I2400M_MT_GET_DEVICE_INFO
, result
,
917 goto error_cmd_failed
;
919 tlv
= i2400m_tlv_find(i2400m
, ack
->pl
, ack_len
- sizeof(*ack
),
920 I2400M_TLV_DETAILED_DEVICE_INFO
, sizeof(*ddi
));
922 dev_err(dev
, "GET DEVICE INFO: "
923 "detailed device info TLV not found (0x%04x)\n",
924 I2400M_TLV_DETAILED_DEVICE_INFO
);
928 skb_pull(ack_skb
, (void *) tlv
- (void *) ack_skb
->data
);
938 return ERR_PTR(result
);
942 /* Firmware interface versions we support */
944 I2400M_HDIv_MAJOR
= 9,
945 I2400M_HDIv_MAJOR_2
= 8,
946 I2400M_HDIv_MINOR
= 1,
951 * i2400m_firmware_check - check firmware versions are compatible with
954 * @i2400m: device descriptor
956 * Returns: 0 if ok, < 0 errno code an error and a message in the
959 * Long function, but quite simple; first chunk launches the command
960 * and double checks the reply for the right TLV. Then we process the
961 * TLV (where the meat is).
963 int i2400m_firmware_check(struct i2400m
*i2400m
)
966 struct device
*dev
= i2400m_dev(i2400m
);
967 struct sk_buff
*ack_skb
;
968 struct i2400m_l3l4_hdr
*cmd
;
969 const struct i2400m_l3l4_hdr
*ack
;
971 const struct i2400m_tlv_hdr
*tlv
;
972 const struct i2400m_tlv_l4_message_versions
*l4mv
;
974 unsigned major
, minor
, branch
;
977 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
980 cmd
->type
= cpu_to_le16(I2400M_MT_GET_LM_VERSION
);
982 cmd
->version
= cpu_to_le16(I2400M_L3L4_VERSION
);
984 ack_skb
= i2400m_msg_to_dev(i2400m
, cmd
, sizeof(*cmd
));
985 if (IS_ERR(ack_skb
)) {
986 result
= PTR_ERR(ack_skb
);
987 dev_err(dev
, "Failed to issue 'get lm version' command: %-d\n",
989 goto error_msg_to_dev
;
991 ack
= wimax_msg_data_len(ack_skb
, &ack_len
);
992 result
= i2400m_msg_check_status(ack
, strerr
, sizeof(strerr
));
994 dev_err(dev
, "'get lm version' (0x%04x) command failed: "
995 "%d - %s\n", I2400M_MT_GET_LM_VERSION
, result
,
997 goto error_cmd_failed
;
999 tlv
= i2400m_tlv_find(i2400m
, ack
->pl
, ack_len
- sizeof(*ack
),
1000 I2400M_TLV_L4_MESSAGE_VERSIONS
, sizeof(*l4mv
));
1002 dev_err(dev
, "get lm version: TLV not found (0x%04x)\n",
1003 I2400M_TLV_L4_MESSAGE_VERSIONS
);
1007 l4mv
= container_of(tlv
, typeof(*l4mv
), hdr
);
1008 major
= le16_to_cpu(l4mv
->major
);
1009 minor
= le16_to_cpu(l4mv
->minor
);
1010 branch
= le16_to_cpu(l4mv
->branch
);
1012 if (major
!= I2400M_HDIv_MAJOR
1013 && major
!= I2400M_HDIv_MAJOR_2
) {
1014 dev_err(dev
, "unsupported major fw interface version "
1015 "%u.%u.%u\n", major
, minor
, branch
);
1016 goto error_bad_major
;
1018 if (major
== I2400M_HDIv_MAJOR_2
)
1019 dev_err(dev
, "deprecated major fw interface version "
1020 "%u.%u.%u\n", major
, minor
, branch
);
1022 if (minor
!= I2400M_HDIv_MINOR
)
1023 dev_warn(dev
, "untested minor fw firmware version %u.%u.%u\n",
1024 major
, minor
, branch
);
1026 dev_info(dev
, "firmware interface version %u.%u.%u\n",
1027 major
, minor
, branch
);
1039 * Send an DoExitIdle command to the device to ask it to go out of
1040 * basestation-idle mode.
1042 * @i2400m: device descriptor
1044 * This starts a renegotiation with the basestation that might involve
1045 * another crypto handshake with user space.
1047 * Returns: 0 if ok, < 0 errno code on error.
1049 int i2400m_cmd_exit_idle(struct i2400m
*i2400m
)
1052 struct device
*dev
= i2400m_dev(i2400m
);
1053 struct sk_buff
*ack_skb
;
1054 struct i2400m_l3l4_hdr
*cmd
;
1058 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
1061 cmd
->type
= cpu_to_le16(I2400M_MT_CMD_EXIT_IDLE
);
1063 cmd
->version
= cpu_to_le16(I2400M_L3L4_VERSION
);
1065 ack_skb
= i2400m_msg_to_dev(i2400m
, cmd
, sizeof(*cmd
));
1066 result
= PTR_ERR(ack_skb
);
1067 if (IS_ERR(ack_skb
)) {
1068 dev_err(dev
, "Failed to issue 'exit idle' command: %d\n",
1070 goto error_msg_to_dev
;
1072 result
= i2400m_msg_check_status(wimax_msg_data(ack_skb
),
1073 strerr
, sizeof(strerr
));
1084 * Query the device for its state, update the WiMAX stack's idea of it
1086 * @i2400m: device descriptor
1088 * Returns: 0 if ok, < 0 errno code on error.
1090 * Executes a 'Get State' command and parses the returned
1093 * Because this is almost identical to a 'Report State', we use
1094 * i2400m_report_state_hook() to parse the answer. This will set the
1095 * carrier state, as well as the RF Kill switches state.
1097 int i2400m_cmd_get_state(struct i2400m
*i2400m
)
1100 struct device
*dev
= i2400m_dev(i2400m
);
1101 struct sk_buff
*ack_skb
;
1102 struct i2400m_l3l4_hdr
*cmd
;
1103 const struct i2400m_l3l4_hdr
*ack
;
1108 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
1111 cmd
->type
= cpu_to_le16(I2400M_MT_GET_STATE
);
1113 cmd
->version
= cpu_to_le16(I2400M_L3L4_VERSION
);
1115 ack_skb
= i2400m_msg_to_dev(i2400m
, cmd
, sizeof(*cmd
));
1116 if (IS_ERR(ack_skb
)) {
1117 dev_err(dev
, "Failed to issue 'get state' command: %ld\n",
1119 result
= PTR_ERR(ack_skb
);
1120 goto error_msg_to_dev
;
1122 ack
= wimax_msg_data_len(ack_skb
, &ack_len
);
1123 result
= i2400m_msg_check_status(ack
, strerr
, sizeof(strerr
));
1125 dev_err(dev
, "'get state' (0x%04x) command failed: "
1126 "%d - %s\n", I2400M_MT_GET_STATE
, result
, strerr
);
1127 goto error_cmd_failed
;
1129 i2400m_report_state_hook(i2400m
, ack
, ack_len
- sizeof(*ack
),
1139 EXPORT_SYMBOL_GPL(i2400m_cmd_get_state
);
1143 * Set basic configuration settings
1145 * @i2400m: device descriptor
1146 * @args: array of pointers to the TLV headers to send for
1147 * configuration (each followed by its payload).
1148 * TLV headers and payloads must be properly initialized, with the
1149 * right endianess (LE).
1150 * @arg_size: number of pointers in the @args array
1152 int i2400m_set_init_config(struct i2400m
*i2400m
,
1153 const struct i2400m_tlv_hdr
**arg
, size_t args
)
1156 struct device
*dev
= i2400m_dev(i2400m
);
1157 struct sk_buff
*ack_skb
;
1158 struct i2400m_l3l4_hdr
*cmd
;
1160 unsigned argc
, argsize
, tlv_size
;
1161 const struct i2400m_tlv_hdr
*tlv_hdr
;
1164 d_fnstart(3, dev
, "(i2400m %p arg %p args %zu)\n", i2400m
, arg
, args
);
1168 /* Compute the size of all the TLVs, so we can alloc a
1169 * contiguous command block to copy them. */
1171 for (argc
= 0; argc
< args
; argc
++) {
1172 tlv_hdr
= arg
[argc
];
1173 argsize
+= sizeof(*tlv_hdr
) + le16_to_cpu(tlv_hdr
->length
);
1175 WARN_ON(argc
>= 9); /* As per hw spec */
1177 /* Alloc the space for the command and TLVs*/
1179 buf
= kzalloc(sizeof(*cmd
) + argsize
, GFP_KERNEL
);
1183 cmd
->type
= cpu_to_le16(I2400M_MT_SET_INIT_CONFIG
);
1184 cmd
->length
= cpu_to_le16(argsize
);
1185 cmd
->version
= cpu_to_le16(I2400M_L3L4_VERSION
);
1188 itr
= buf
+ sizeof(*cmd
);
1189 for (argc
= 0; argc
< args
; argc
++) {
1190 tlv_hdr
= arg
[argc
];
1191 tlv_size
= sizeof(*tlv_hdr
) + le16_to_cpu(tlv_hdr
->length
);
1192 memcpy(itr
, tlv_hdr
, tlv_size
);
1196 /* Send the message! */
1197 ack_skb
= i2400m_msg_to_dev(i2400m
, buf
, sizeof(*cmd
) + argsize
);
1198 result
= PTR_ERR(ack_skb
);
1199 if (IS_ERR(ack_skb
)) {
1200 dev_err(dev
, "Failed to issue 'init config' command: %d\n",
1203 goto error_msg_to_dev
;
1205 result
= i2400m_msg_check_status(wimax_msg_data(ack_skb
),
1206 strerr
, sizeof(strerr
));
1208 dev_err(dev
, "'init config' (0x%04x) command failed: %d - %s\n",
1209 I2400M_MT_SET_INIT_CONFIG
, result
, strerr
);
1215 d_fnend(3, dev
, "(i2400m %p arg %p args %zu) = %d\n",
1216 i2400m
, arg
, args
, result
);
1220 EXPORT_SYMBOL_GPL(i2400m_set_init_config
);
1224 * i2400m_dev_initialize - Initialize the device once communications are ready
1226 * @i2400m: device descriptor
1228 * Returns: 0 if ok, < 0 errno code on error.
1230 * Configures the device to work the way we like it.
1232 * At the point of this call, the device is registered with the WiMAX
1233 * and netdev stacks, firmware is uploaded and we can talk to the
1236 int i2400m_dev_initialize(struct i2400m
*i2400m
)
1239 struct device
*dev
= i2400m_dev(i2400m
);
1240 struct i2400m_tlv_config_idle_parameters idle_params
;
1241 const struct i2400m_tlv_hdr
*args
[9];
1244 d_fnstart(3, dev
, "(i2400m %p)\n", i2400m
);
1245 /* Useless for now...might change */
1246 if (i2400m_idle_mode_disabled
) {
1247 idle_params
.hdr
.type
=
1248 cpu_to_le16(I2400M_TLV_CONFIG_IDLE_PARAMETERS
);
1249 idle_params
.hdr
.length
= cpu_to_le16(
1250 sizeof(idle_params
) - sizeof(idle_params
.hdr
));
1251 idle_params
.idle_timeout
= 0;
1252 idle_params
.idle_paging_interval
= 0;
1253 args
[argc
++] = &idle_params
.hdr
;
1255 result
= i2400m_set_init_config(i2400m
, args
, argc
);
1258 result
= i2400m_firmware_check(i2400m
); /* fw versions ok? */
1262 * Update state: Here it just calls a get state; parsing the
1263 * result (System State TLV and RF Status TLV [done in the rx
1264 * path hooks]) will set the hardware and software RF-Kill
1267 result
= i2400m_cmd_get_state(i2400m
);
1269 d_fnend(3, dev
, "(i2400m %p) = %d\n", i2400m
, result
);
1275 * i2400m_dev_shutdown - Shutdown a running device
1277 * @i2400m: device descriptor
1279 * Gracefully stops the device, moving it to the lowest power
1280 * consumption state possible.
1282 void i2400m_dev_shutdown(struct i2400m
*i2400m
)
1284 int result
= -ENODEV
;
1285 struct device
*dev
= i2400m_dev(i2400m
);
1287 d_fnstart(3, dev
, "(i2400m %p)\n", i2400m
);
1288 result
= i2400m
->bus_reset(i2400m
, I2400M_RT_WARM
);
1289 d_fnend(3, dev
, "(i2400m %p) = void [%d]\n", i2400m
, result
);