blob: 0f58418748aa96660ac8aedc2878e1d7d735fc24 [file] [log] [blame]
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001/*
2 * Intel Wireless WiMAX Connection 2400m
3 * Miscellaneous control functions for managing the device
4 *
5 *
6 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
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
17 * distribution.
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.
21 *
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.
33 *
34 *
35 * Intel Corporation <linux-wimax@intel.com>
36 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
37 * - Initial implementation
38 *
39 * This is a collection of functions used to control the device (plus
40 * a few helpers).
41 *
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()].
50 *
51 * ROADMAP
52 *
53 * i2400m_dev_initalize() Called by i2400m_dev_start()
54 * i2400m_set_init_config()
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -080055 * i2400m_cmd_get_state()
56 * i2400m_dev_shutdown() Called by i2400m_dev_stop()
57 * i2400m->bus_reset()
58 *
59 * i2400m_{cmd,get,set}_*()
60 * i2400m_msg_to_dev()
61 * i2400m_msg_check_status()
62 *
63 * i2400m_report_hook() Called on reception of an event
64 * i2400m_report_state_hook()
65 * i2400m_tlv_buffer_walk()
66 * i2400m_tlv_match()
67 * i2400m_report_tlv_system_state()
68 * i2400m_report_tlv_rf_switches_status()
69 * i2400m_report_tlv_media_status()
70 * i2400m_cmd_enter_powersave()
71 *
72 * i2400m_msg_ack_hook() Called on reception of a reply to a
73 * command, get or set
74 */
75
76#include <stdarg.h>
77#include "i2400m.h"
78#include <linux/kernel.h>
79#include <linux/wimax/i2400m.h>
80
81
82#define D_SUBMODULE control
83#include "debug-levels.h"
84
85
86/*
87 * Return if a TLV is of a give type and size
88 *
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
96 * the size
97 */
98static
99ssize_t i2400m_tlv_match(const struct i2400m_tlv_hdr *tlv,
100 enum i2400m_tlv tlv_type, ssize_t tlv_size)
101{
102 if (le16_to_cpu(tlv->type) != tlv_type) /* Not our type? skip */
103 return -1;
104 if (tlv_size != -1
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);
110 return size;
111 }
112 return 0;
113}
114
115
116/*
117 * Given a buffer of TLVs, iterate over them
118 *
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.
125 *
126 * Usage:
127 *
128 * tlv_itr = NULL;
129 * while (tlv_itr = i2400m_tlv_buffer_walk(i2400m, buf, size, tlv_itr)) {
130 * ...
131 * // Do stuff with tlv_itr, DON'T MODIFY IT
132 * ...
133 * }
134 */
135static
136const 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)
140{
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;
144 unsigned type;
145
146 if (tlv_pos == NULL) /* Take the first one? */
147 tlv_pos = tlv_buf;
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 */
152 tlv_pos = NULL;
153 goto error_beyond_end;
154 }
155 if (tlv_pos > tlv_top) {
156 tlv_pos = NULL;
157 WARN_ON(1);
158 goto error_beyond_end;
159 }
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;
166 }
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;
176 }
177error_short_header:
178error_beyond_end:
179 return tlv_pos;
180}
181
182
183/*
184 * Find a TLV in a buffer of sequential TLVs
185 *
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
193 *
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
196 * returned.
197 */
198static
199const 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)
203{
204 ssize_t match;
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 :) */
210 break;
211 if (match > 0)
212 dev_warn(dev, "TLV type 0x%04x found with size "
213 "mismatch (%zu vs %zu needed)\n",
214 tlv_type, match, tlv_size);
215 }
216 return tlv;
217}
218
219
220static const struct
221{
222 char *msg;
223 int errno;
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 },
242};
243
244
245/*
246 * i2400m_msg_check_status - translate a message's status code
247 *
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
252 * be truncated.
253 *
254 * Returns: errno code corresponding to the status code in @l3l4_hdr
255 * and a message in @strbuf describing the error.
256 */
257int i2400m_msg_check_status(const struct i2400m_l3l4_hdr *l3l4_hdr,
258 char *strbuf, size_t strbuf_size)
259{
260 int result;
261 enum i2400m_ms status = le16_to_cpu(l3l4_hdr->status);
262 const char *str;
263
264 if (status == 0)
265 return 0;
266 if (status > ARRAY_SIZE(ms_to_errno)) {
267 str = "unknown status code";
268 result = -EBADR;
269 } else {
270 str = ms_to_errno[status].msg;
271 result = ms_to_errno[status].errno;
272 }
273 if (strbuf)
274 snprintf(strbuf, strbuf_size, "%s (%d)", str, status);
275 return result;
276}
277
278
279/*
280 * Act on a TLV System State reported by the device
281 *
282 * @i2400m: device descriptor
283 * @ss: validated System State TLV
284 */
285static
286void i2400m_report_tlv_system_state(struct i2400m *i2400m,
287 const struct i2400m_tlv_system_state *ss)
288{
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);
292
293 d_fnstart(3, dev, "(i2400m %p ss %p [%u])\n", i2400m, ss, i2400m_state);
294
295 if (unlikely(i2400m->ready == 0)) /* act if up */
296 goto out;
297 if (i2400m->state != i2400m_state) {
298 i2400m->state = i2400m_state;
299 wake_up_all(&i2400m->state_wq);
300 }
301 switch (i2400m_state) {
302 case I2400M_SS_UNINITIALIZED:
303 case I2400M_SS_INIT:
304 case I2400M_SS_CONFIG:
305 case I2400M_SS_PRODUCTION:
306 wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
307 break;
308
309 case I2400M_SS_RF_OFF:
310 case I2400M_SS_RF_SHUTDOWN:
311 wimax_state_change(wimax_dev, WIMAX_ST_RADIO_OFF);
312 break;
313
314 case I2400M_SS_READY:
315 case I2400M_SS_STANDBY:
316 case I2400M_SS_SLEEPACTIVE:
317 wimax_state_change(wimax_dev, WIMAX_ST_READY);
318 break;
319
320 case I2400M_SS_CONNECTING:
321 case I2400M_SS_WIMAX_CONNECTED:
322 wimax_state_change(wimax_dev, WIMAX_ST_READY);
323 break;
324
325 case I2400M_SS_SCAN:
326 case I2400M_SS_OUT_OF_ZONE:
327 wimax_state_change(wimax_dev, WIMAX_ST_SCANNING);
328 break;
329
330 case I2400M_SS_IDLE:
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);
335 break;
336
337 default:
338 /* Huh? just in case, shut it down */
339 dev_err(dev, "HW BUG? unknown state %u: shutting down\n",
340 i2400m_state);
341 i2400m->bus_reset(i2400m, I2400M_RT_WARM);
342 break;
343 };
344out:
345 d_fnend(3, dev, "(i2400m %p ss %p [%u]) = void\n",
346 i2400m, ss, i2400m_state);
347}
348
349
350/*
351 * Parse and act on a TLV Media Status sent by the device
352 *
353 * @i2400m: device descriptor
354 * @ms: validated Media Status TLV
355 *
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).
360 *
361 * In fact, doc says that everytime we get a link-up, we should do a
362 * DHCP negotiation...
363 */
364static
365void i2400m_report_tlv_media_status(struct i2400m *i2400m,
366 const struct i2400m_tlv_media_status *ms)
367{
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);
372
373 d_fnstart(3, dev, "(i2400m %p ms %p [%u])\n", i2400m, ms, status);
374
375 if (unlikely(i2400m->ready == 0)) /* act if up */
376 goto out;
377 switch (status) {
378 case I2400M_MEDIA_STATUS_LINK_UP:
379 netif_carrier_on(net_dev);
380 break;
381 case I2400M_MEDIA_STATUS_LINK_DOWN:
382 netif_carrier_off(net_dev);
383 break;
384 /*
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.
388 */
389 case I2400M_MEDIA_STATUS_LINK_RENEW:
390 netif_carrier_on(net_dev);
391 break;
392 default:
393 dev_err(dev, "HW BUG? unknown media status %u\n",
394 status);
395 };
396out:
397 d_fnend(3, dev, "(i2400m %p ms %p [%u]) = void\n",
398 i2400m, ms, status);
399}
400
401
402/*
403 * Parse a 'state report' and extract carrier on/off information
404 *
405 * @i2400m: device descriptor
406 * @l3l4_hdr: pointer to message; it has been already validated for
407 * consistent size.
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)
411 *
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
414 * netdev.
415 *
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
418 * the transitions).
419 */
420static
421void i2400m_report_state_hook(struct i2400m *i2400m,
422 const struct i2400m_l3l4_hdr *l3l4_hdr,
423 size_t size, const char *tag)
424{
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);
431
432 d_fnstart(4, dev, "(i2400m %p, l3l4_hdr %p, size %zu, %s)\n",
433 i2400m, l3l4_hdr, size, tag);
434 tlv = NULL;
435
436 while ((tlv = i2400m_tlv_buffer_walk(i2400m, &l3l4_hdr->pl,
437 tlv_size, tlv))) {
438 if (0 == i2400m_tlv_match(tlv, I2400M_TLV_SYSTEM_STATE,
439 sizeof(*ss))) {
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);
446 }
447 if (0 == i2400m_tlv_match(tlv, I2400M_TLV_RF_STATUS,
448 sizeof(*rfss))) {
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);
456 }
457 if (0 == i2400m_tlv_match(tlv, I2400M_TLV_MEDIA_STATUS,
458 sizeof(*ms))) {
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);
463 }
464 }
465 d_fnend(4, dev, "(i2400m %p, l3l4_hdr %p, size %zu, %s) = void\n",
466 i2400m, l3l4_hdr, size, tag);
467}
468
469
470/*
471 * i2400m_report_hook - (maybe) act on a report
472 *
473 * @i2400m: device descriptor
474 * @l3l4_hdr: pointer to message; it has been already validated for
475 * consistent size.
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)
479 *
480 * Extract information we might need (like carrien on/off) from a
481 * device report.
482 */
483void i2400m_report_hook(struct i2400m *i2400m,
484 const struct i2400m_l3l4_hdr *l3l4_hdr, size_t size)
485{
486 struct device *dev = i2400m_dev(i2400m);
487 unsigned msg_type;
488
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
492 * here */
493 msg_type = le16_to_cpu(l3l4_hdr->type);
494 switch (msg_type) {
495 case I2400M_MT_REPORT_STATE: /* carrier detection... */
496 i2400m_report_state_hook(i2400m,
497 l3l4_hdr, size, "REPORT STATE");
498 break;
499 /* If the device is ready for power save, then ask it to do
500 * it. */
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);
505 }
506 break;
507 };
508 d_fnend(3, dev, "(i2400m %p l3l4_hdr %p size %zu) = void\n",
509 i2400m, l3l4_hdr, size);
510}
511
512
513/*
514 * i2400m_msg_ack_hook - process cmd/set/get ack for internal status
515 *
516 * @i2400m: device descriptor
517 * @l3l4_hdr: pointer to message; it has been already validated for
518 * consistent size.
519 * @size: size of the message
520 *
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...
525 */
526void i2400m_msg_ack_hook(struct i2400m *i2400m,
527 const struct i2400m_l3l4_hdr *l3l4_hdr, size_t size)
528{
529 int result;
530 struct device *dev = i2400m_dev(i2400m);
531 unsigned ack_type, ack_status;
532 char strerr[32];
533
534 /* Chew on the message, we might need some information from
535 * here */
536 ack_type = le16_to_cpu(l3l4_hdr->type);
537 ack_status = le16_to_cpu(l3l4_hdr->status);
538 switch (ack_type) {
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. */
542 if (0) {
543 result = i2400m_msg_check_status(
544 l3l4_hdr, strerr, sizeof(strerr));
545 if (result >= 0)
546 d_printf(1, dev, "ready for power save: %zd\n",
547 size);
548 }
549 break;
550 };
551 return;
552}
553
554
555/*
556 * i2400m_msg_size_check() - verify message size and header are congruent
557 *
558 * It is ok if the total message size is larger than the expected
559 * size, as there can be padding.
560 */
561int i2400m_msg_size_check(struct i2400m *i2400m,
562 const struct i2400m_l3l4_hdr *l3l4_hdr,
563 size_t msg_size)
564{
565 int result;
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);
574 result = -EIO;
575 goto error_hdr_size;
576 }
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);
582 result = -EIO;
583 } else
584 result = 0;
585error_hdr_size:
586 d_fnend(4, dev,
587 "(i2400m %p l3l4_hdr %p msg_size %zu) = %d\n",
588 i2400m, l3l4_hdr, msg_size, result);
589 return result;
590}
591
592
593
594/*
595 * Cancel a wait for a command ACK
596 *
597 * @i2400m: device descriptor
598 * @code: [negative] errno code to cancel with (don't use
599 * -EINPROGRESS)
600 *
601 * If there is an ack already filled out, free it.
602 */
603void i2400m_msg_to_dev_cancel_wait(struct i2400m *i2400m, int code)
604{
605 struct sk_buff *ack_skb;
606 unsigned long flags;
607
608 spin_lock_irqsave(&i2400m->rx_lock, flags);
609 ack_skb = i2400m->ack_skb;
610 if (ack_skb && !IS_ERR(ack_skb))
Inaky Perez-Gonzalezf4895b82009-01-19 13:19:30 +0000611 kfree_skb(ack_skb);
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800612 i2400m->ack_skb = ERR_PTR(code);
613 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
614}
615
616
617/**
618 * i2400m_msg_to_dev - Send a control message to the device and get a response
619 *
620 * @i2400m: device descriptor
621 *
622 * @msg_skb: an skb *
623 *
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.
628 *
629 * @buf_len: buffer size
630 *
631 * Returns:
632 *
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:
636 *
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
641 *
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.
646 *
647 * The skb has to be freed with kfree_skb() once done.
648 *
649 * Description:
650 *
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
654 * ack.
655 *
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.
658 *
659 * The i2400m handles only one message at the same time, thus we need
660 * the mutex to exclude other players.
661 *
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.
669 */
670struct sk_buff *i2400m_msg_to_dev(struct i2400m *i2400m,
671 const void *buf, size_t buf_len)
672{
673 int result;
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;
678 size_t ack_len;
679 int ack_timeout;
680 unsigned msg_type;
681 unsigned long flags;
682
683 d_fnstart(3, dev, "(i2400m %p buf %p len %zu)\n",
684 i2400m, buf, buf_len);
685
686 if (i2400m->boot_mode)
687 return ERR_PTR(-ENODEV);
688
689 msg_l3l4_hdr = buf;
690 /* Check msg & payload consistency */
691 result = i2400m_msg_size_check(i2400m, msg_l3l4_hdr, buf_len);
692 if (result < 0)
693 goto error_bad_msg;
694 msg_type = le16_to_cpu(msg_l3l4_hdr->type);
695 d_printf(1, dev, "CMD/GET/SET 0x%04x %zu bytes\n",
696 msg_type, buf_len);
697 d_dump(2, dev, buf, buf_len);
698
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);
707 if (result < 0) {
708 dev_err(dev, "can't send message 0x%04x: %d\n",
709 le16_to_cpu(msg_l3l4_hdr->type), result);
710 goto error_tx;
711 }
712
713 /* Some commands take longer to execute because of crypto ops,
714 * so we give them some more leeway on timeout */
715 switch (msg_type) {
716 case I2400M_MT_GET_TLS_OPERATION_RESULT:
717 case I2400M_MT_CMD_SEND_EAP_RESPONSE:
718 ack_timeout = 5 * HZ;
719 break;
720 default:
721 ack_timeout = HZ;
722 };
723
Inaky Perez-Gonzalez223beea2009-04-13 10:26:34 -0700724 if (unlikely(i2400m->trace_msg_from_user))
725 wimax_msg(&i2400m->wimax_dev, "echo", buf, buf_len, GFP_KERNEL);
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800726 /* The RX path in rx.c will put any response for this message
727 * in i2400m->ack_skb and wake us up. If we cancel the wait,
728 * we need to change the value of i2400m->ack_skb to something
729 * not -EINPROGRESS so RX knows there is no one waiting. */
730 result = wait_for_completion_interruptible_timeout(
731 &i2400m->msg_completion, ack_timeout);
732 if (result == 0) {
733 dev_err(dev, "timeout waiting for reply to message 0x%04x\n",
734 msg_type);
735 result = -ETIMEDOUT;
736 i2400m_msg_to_dev_cancel_wait(i2400m, result);
737 goto error_wait_for_completion;
738 } else if (result < 0) {
739 dev_err(dev, "error waiting for reply to message 0x%04x: %d\n",
740 msg_type, result);
741 i2400m_msg_to_dev_cancel_wait(i2400m, result);
742 goto error_wait_for_completion;
743 }
744
745 /* Pull out the ack data from i2400m->ack_skb -- see if it is
746 * an error and act accordingly */
747 spin_lock_irqsave(&i2400m->rx_lock, flags);
748 ack_skb = i2400m->ack_skb;
749 if (IS_ERR(ack_skb))
750 result = PTR_ERR(ack_skb);
751 else
752 result = 0;
753 i2400m->ack_skb = NULL;
754 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
755 if (result < 0)
756 goto error_ack_status;
757 ack_l3l4_hdr = wimax_msg_data_len(ack_skb, &ack_len);
758
759 /* Check the ack and deliver it if it is ok */
Inaky Perez-Gonzalez223beea2009-04-13 10:26:34 -0700760 if (unlikely(i2400m->trace_msg_from_user))
761 wimax_msg(&i2400m->wimax_dev, "echo",
762 ack_l3l4_hdr, ack_len, GFP_KERNEL);
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800763 result = i2400m_msg_size_check(i2400m, ack_l3l4_hdr, ack_len);
764 if (result < 0) {
765 dev_err(dev, "HW BUG? reply to message 0x%04x: %d\n",
766 msg_type, result);
767 goto error_bad_ack_len;
768 }
769 if (msg_type != le16_to_cpu(ack_l3l4_hdr->type)) {
770 dev_err(dev, "HW BUG? bad reply 0x%04x to message 0x%04x\n",
771 le16_to_cpu(ack_l3l4_hdr->type), msg_type);
772 result = -EIO;
773 goto error_bad_ack_type;
774 }
775 i2400m_msg_ack_hook(i2400m, ack_l3l4_hdr, ack_len);
776 mutex_unlock(&i2400m->msg_mutex);
777 d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %p\n",
778 i2400m, buf, buf_len, ack_skb);
779 return ack_skb;
780
781error_bad_ack_type:
782error_bad_ack_len:
783 kfree_skb(ack_skb);
784error_ack_status:
785error_wait_for_completion:
786error_tx:
787 mutex_unlock(&i2400m->msg_mutex);
788error_bad_msg:
789 d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %d\n",
790 i2400m, buf, buf_len, result);
791 return ERR_PTR(result);
792}
793
794
795/*
796 * Definitions for the Enter Power Save command
797 *
798 * The Enter Power Save command requests the device to go into power
799 * saving mode. The device will ack or nak the command depending on it
800 * being ready for it. If it acks, we tell the USB subsystem to
801 *
802 * As well, the device might request to go into power saving mode by
803 * sending a report (REPORT_POWERSAVE_READY), in which case, we issue
804 * this command. The hookups in the RX coder allow
805 */
806enum {
807 I2400M_WAKEUP_ENABLED = 0x01,
808 I2400M_WAKEUP_DISABLED = 0x02,
809 I2400M_TLV_TYPE_WAKEUP_MODE = 144,
810};
811
812struct i2400m_cmd_enter_power_save {
813 struct i2400m_l3l4_hdr hdr;
814 struct i2400m_tlv_hdr tlv;
815 __le32 val;
816} __attribute__((packed));
817
818
819/*
820 * Request entering power save
821 *
822 * This command is (mainly) executed when the device indicates that it
823 * is ready to go into powersave mode via a REPORT_POWERSAVE_READY.
824 */
825int i2400m_cmd_enter_powersave(struct i2400m *i2400m)
826{
827 int result;
828 struct device *dev = i2400m_dev(i2400m);
829 struct sk_buff *ack_skb;
830 struct i2400m_cmd_enter_power_save *cmd;
831 char strerr[32];
832
833 result = -ENOMEM;
834 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
835 if (cmd == NULL)
836 goto error_alloc;
837 cmd->hdr.type = cpu_to_le16(I2400M_MT_CMD_ENTER_POWERSAVE);
838 cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
839 cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
840 cmd->tlv.type = cpu_to_le16(I2400M_TLV_TYPE_WAKEUP_MODE);
841 cmd->tlv.length = cpu_to_le16(sizeof(cmd->val));
842 cmd->val = cpu_to_le32(I2400M_WAKEUP_ENABLED);
843
844 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
845 result = PTR_ERR(ack_skb);
846 if (IS_ERR(ack_skb)) {
847 dev_err(dev, "Failed to issue 'Enter power save' command: %d\n",
848 result);
849 goto error_msg_to_dev;
850 }
851 result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
852 strerr, sizeof(strerr));
853 if (result == -EACCES)
854 d_printf(1, dev, "Cannot enter power save mode\n");
855 else if (result < 0)
856 dev_err(dev, "'Enter power save' (0x%04x) command failed: "
857 "%d - %s\n", I2400M_MT_CMD_ENTER_POWERSAVE,
858 result, strerr);
859 else
860 d_printf(1, dev, "device ready to power save\n");
861 kfree_skb(ack_skb);
862error_msg_to_dev:
863 kfree(cmd);
864error_alloc:
865 return result;
866}
867EXPORT_SYMBOL_GPL(i2400m_cmd_enter_powersave);
868
869
870/*
871 * Definitions for getting device information
872 */
873enum {
874 I2400M_TLV_DETAILED_DEVICE_INFO = 140
875};
876
877/**
878 * i2400m_get_device_info - Query the device for detailed device information
879 *
880 * @i2400m: device descriptor
881 *
882 * Returns: an skb whose skb->data points to a 'struct
883 * i2400m_tlv_detailed_device_info'. When done, kfree_skb() it. The
884 * skb is *guaranteed* to contain the whole TLV data structure.
885 *
886 * On error, IS_ERR(skb) is true and ERR_PTR(skb) is the error
887 * code.
888 */
889struct sk_buff *i2400m_get_device_info(struct i2400m *i2400m)
890{
891 int result;
892 struct device *dev = i2400m_dev(i2400m);
893 struct sk_buff *ack_skb;
894 struct i2400m_l3l4_hdr *cmd;
895 const struct i2400m_l3l4_hdr *ack;
896 size_t ack_len;
897 const struct i2400m_tlv_hdr *tlv;
898 const struct i2400m_tlv_detailed_device_info *ddi;
899 char strerr[32];
900
901 ack_skb = ERR_PTR(-ENOMEM);
902 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
903 if (cmd == NULL)
904 goto error_alloc;
905 cmd->type = cpu_to_le16(I2400M_MT_GET_DEVICE_INFO);
906 cmd->length = 0;
907 cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
908
909 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
910 if (IS_ERR(ack_skb)) {
911 dev_err(dev, "Failed to issue 'get device info' command: %ld\n",
912 PTR_ERR(ack_skb));
913 goto error_msg_to_dev;
914 }
915 ack = wimax_msg_data_len(ack_skb, &ack_len);
916 result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
917 if (result < 0) {
918 dev_err(dev, "'get device info' (0x%04x) command failed: "
919 "%d - %s\n", I2400M_MT_GET_DEVICE_INFO, result,
920 strerr);
921 goto error_cmd_failed;
922 }
923 tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
924 I2400M_TLV_DETAILED_DEVICE_INFO, sizeof(*ddi));
925 if (tlv == NULL) {
926 dev_err(dev, "GET DEVICE INFO: "
927 "detailed device info TLV not found (0x%04x)\n",
928 I2400M_TLV_DETAILED_DEVICE_INFO);
929 result = -EIO;
930 goto error_no_tlv;
931 }
932 skb_pull(ack_skb, (void *) tlv - (void *) ack_skb->data);
933error_msg_to_dev:
934 kfree(cmd);
935error_alloc:
936 return ack_skb;
937
938error_no_tlv:
939error_cmd_failed:
940 kfree_skb(ack_skb);
941 kfree(cmd);
942 return ERR_PTR(result);
943}
944
945
946/* Firmware interface versions we support */
947enum {
948 I2400M_HDIv_MAJOR = 9,
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800949 I2400M_HDIv_MINOR = 1,
Inaky Perez-Gonzalezefa05d02009-02-28 23:42:48 +0000950 I2400M_HDIv_MINOR_2 = 2,
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800951};
952
953
954/**
955 * i2400m_firmware_check - check firmware versions are compatible with
956 * the driver
957 *
958 * @i2400m: device descriptor
959 *
960 * Returns: 0 if ok, < 0 errno code an error and a message in the
961 * kernel log.
962 *
963 * Long function, but quite simple; first chunk launches the command
964 * and double checks the reply for the right TLV. Then we process the
965 * TLV (where the meat is).
Inaky Perez-Gonzalez6a0f7ab2009-02-28 23:42:49 +0000966 *
967 * Once we process the TLV that gives us the firmware's interface
968 * version, we encode it and save it in i2400m->fw_version for future
969 * reference.
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800970 */
971int i2400m_firmware_check(struct i2400m *i2400m)
972{
973 int result;
974 struct device *dev = i2400m_dev(i2400m);
975 struct sk_buff *ack_skb;
976 struct i2400m_l3l4_hdr *cmd;
977 const struct i2400m_l3l4_hdr *ack;
978 size_t ack_len;
979 const struct i2400m_tlv_hdr *tlv;
980 const struct i2400m_tlv_l4_message_versions *l4mv;
981 char strerr[32];
982 unsigned major, minor, branch;
983
984 result = -ENOMEM;
985 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
986 if (cmd == NULL)
987 goto error_alloc;
988 cmd->type = cpu_to_le16(I2400M_MT_GET_LM_VERSION);
989 cmd->length = 0;
990 cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
991
992 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
993 if (IS_ERR(ack_skb)) {
994 result = PTR_ERR(ack_skb);
995 dev_err(dev, "Failed to issue 'get lm version' command: %-d\n",
996 result);
997 goto error_msg_to_dev;
998 }
999 ack = wimax_msg_data_len(ack_skb, &ack_len);
1000 result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
1001 if (result < 0) {
1002 dev_err(dev, "'get lm version' (0x%04x) command failed: "
1003 "%d - %s\n", I2400M_MT_GET_LM_VERSION, result,
1004 strerr);
1005 goto error_cmd_failed;
1006 }
1007 tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
1008 I2400M_TLV_L4_MESSAGE_VERSIONS, sizeof(*l4mv));
1009 if (tlv == NULL) {
1010 dev_err(dev, "get lm version: TLV not found (0x%04x)\n",
1011 I2400M_TLV_L4_MESSAGE_VERSIONS);
1012 result = -EIO;
1013 goto error_no_tlv;
1014 }
1015 l4mv = container_of(tlv, typeof(*l4mv), hdr);
1016 major = le16_to_cpu(l4mv->major);
1017 minor = le16_to_cpu(l4mv->minor);
1018 branch = le16_to_cpu(l4mv->branch);
1019 result = -EINVAL;
Inaky Perez-Gonzalezefa05d02009-02-28 23:42:48 +00001020 if (major != I2400M_HDIv_MAJOR) {
1021 dev_err(dev, "unsupported major fw version "
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001022 "%u.%u.%u\n", major, minor, branch);
1023 goto error_bad_major;
1024 }
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001025 result = 0;
Inaky Perez-Gonzalezefa05d02009-02-28 23:42:48 +00001026 if (minor < I2400M_HDIv_MINOR_2 && minor > I2400M_HDIv_MINOR)
1027 dev_warn(dev, "untested minor fw version %u.%u.%u\n",
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001028 major, minor, branch);
Inaky Perez-Gonzalez6a0f7ab2009-02-28 23:42:49 +00001029 /* Yes, we ignore the branch -- we don't have to track it */
1030 i2400m->fw_version = major << 16 | minor;
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001031 dev_info(dev, "firmware interface version %u.%u.%u\n",
1032 major, minor, branch);
Inaky Perez-Gonzalez6a0f7ab2009-02-28 23:42:49 +00001033error_bad_major:
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001034error_no_tlv:
1035error_cmd_failed:
1036 kfree_skb(ack_skb);
1037error_msg_to_dev:
1038 kfree(cmd);
1039error_alloc:
1040 return result;
1041}
1042
1043
1044/*
1045 * Send an DoExitIdle command to the device to ask it to go out of
1046 * basestation-idle mode.
1047 *
1048 * @i2400m: device descriptor
1049 *
1050 * This starts a renegotiation with the basestation that might involve
1051 * another crypto handshake with user space.
1052 *
1053 * Returns: 0 if ok, < 0 errno code on error.
1054 */
1055int i2400m_cmd_exit_idle(struct i2400m *i2400m)
1056{
1057 int result;
1058 struct device *dev = i2400m_dev(i2400m);
1059 struct sk_buff *ack_skb;
1060 struct i2400m_l3l4_hdr *cmd;
1061 char strerr[32];
1062
1063 result = -ENOMEM;
1064 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1065 if (cmd == NULL)
1066 goto error_alloc;
1067 cmd->type = cpu_to_le16(I2400M_MT_CMD_EXIT_IDLE);
1068 cmd->length = 0;
1069 cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
1070
1071 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
1072 result = PTR_ERR(ack_skb);
1073 if (IS_ERR(ack_skb)) {
1074 dev_err(dev, "Failed to issue 'exit idle' command: %d\n",
1075 result);
1076 goto error_msg_to_dev;
1077 }
1078 result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
1079 strerr, sizeof(strerr));
1080 kfree_skb(ack_skb);
1081error_msg_to_dev:
1082 kfree(cmd);
1083error_alloc:
1084 return result;
1085
1086}
1087
1088
1089/*
1090 * Query the device for its state, update the WiMAX stack's idea of it
1091 *
1092 * @i2400m: device descriptor
1093 *
1094 * Returns: 0 if ok, < 0 errno code on error.
1095 *
1096 * Executes a 'Get State' command and parses the returned
1097 * TLVs.
1098 *
1099 * Because this is almost identical to a 'Report State', we use
1100 * i2400m_report_state_hook() to parse the answer. This will set the
1101 * carrier state, as well as the RF Kill switches state.
1102 */
1103int i2400m_cmd_get_state(struct i2400m *i2400m)
1104{
1105 int result;
1106 struct device *dev = i2400m_dev(i2400m);
1107 struct sk_buff *ack_skb;
1108 struct i2400m_l3l4_hdr *cmd;
1109 const struct i2400m_l3l4_hdr *ack;
1110 size_t ack_len;
1111 char strerr[32];
1112
1113 result = -ENOMEM;
1114 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1115 if (cmd == NULL)
1116 goto error_alloc;
1117 cmd->type = cpu_to_le16(I2400M_MT_GET_STATE);
1118 cmd->length = 0;
1119 cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
1120
1121 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
1122 if (IS_ERR(ack_skb)) {
1123 dev_err(dev, "Failed to issue 'get state' command: %ld\n",
1124 PTR_ERR(ack_skb));
1125 result = PTR_ERR(ack_skb);
1126 goto error_msg_to_dev;
1127 }
1128 ack = wimax_msg_data_len(ack_skb, &ack_len);
1129 result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
1130 if (result < 0) {
1131 dev_err(dev, "'get state' (0x%04x) command failed: "
1132 "%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
1133 goto error_cmd_failed;
1134 }
1135 i2400m_report_state_hook(i2400m, ack, ack_len - sizeof(*ack),
1136 "GET STATE");
1137 result = 0;
1138 kfree_skb(ack_skb);
1139error_cmd_failed:
1140error_msg_to_dev:
1141 kfree(cmd);
1142error_alloc:
1143 return result;
1144}
1145EXPORT_SYMBOL_GPL(i2400m_cmd_get_state);
1146
1147
1148/**
1149 * Set basic configuration settings
1150 *
1151 * @i2400m: device descriptor
1152 * @args: array of pointers to the TLV headers to send for
1153 * configuration (each followed by its payload).
1154 * TLV headers and payloads must be properly initialized, with the
1155 * right endianess (LE).
1156 * @arg_size: number of pointers in the @args array
1157 */
1158int i2400m_set_init_config(struct i2400m *i2400m,
1159 const struct i2400m_tlv_hdr **arg, size_t args)
1160{
1161 int result;
1162 struct device *dev = i2400m_dev(i2400m);
1163 struct sk_buff *ack_skb;
1164 struct i2400m_l3l4_hdr *cmd;
1165 char strerr[32];
1166 unsigned argc, argsize, tlv_size;
1167 const struct i2400m_tlv_hdr *tlv_hdr;
1168 void *buf, *itr;
1169
1170 d_fnstart(3, dev, "(i2400m %p arg %p args %zu)\n", i2400m, arg, args);
1171 result = 0;
1172 if (args == 0)
1173 goto none;
1174 /* Compute the size of all the TLVs, so we can alloc a
1175 * contiguous command block to copy them. */
1176 argsize = 0;
1177 for (argc = 0; argc < args; argc++) {
1178 tlv_hdr = arg[argc];
1179 argsize += sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
1180 }
1181 WARN_ON(argc >= 9); /* As per hw spec */
1182
1183 /* Alloc the space for the command and TLVs*/
1184 result = -ENOMEM;
1185 buf = kzalloc(sizeof(*cmd) + argsize, GFP_KERNEL);
1186 if (buf == NULL)
1187 goto error_alloc;
1188 cmd = buf;
1189 cmd->type = cpu_to_le16(I2400M_MT_SET_INIT_CONFIG);
1190 cmd->length = cpu_to_le16(argsize);
1191 cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
1192
1193 /* Copy the TLVs */
1194 itr = buf + sizeof(*cmd);
1195 for (argc = 0; argc < args; argc++) {
1196 tlv_hdr = arg[argc];
1197 tlv_size = sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
1198 memcpy(itr, tlv_hdr, tlv_size);
1199 itr += tlv_size;
1200 }
1201
1202 /* Send the message! */
1203 ack_skb = i2400m_msg_to_dev(i2400m, buf, sizeof(*cmd) + argsize);
1204 result = PTR_ERR(ack_skb);
1205 if (IS_ERR(ack_skb)) {
1206 dev_err(dev, "Failed to issue 'init config' command: %d\n",
1207 result);
1208
1209 goto error_msg_to_dev;
1210 }
1211 result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
1212 strerr, sizeof(strerr));
1213 if (result < 0)
1214 dev_err(dev, "'init config' (0x%04x) command failed: %d - %s\n",
1215 I2400M_MT_SET_INIT_CONFIG, result, strerr);
1216 kfree_skb(ack_skb);
1217error_msg_to_dev:
1218 kfree(buf);
1219error_alloc:
1220none:
1221 d_fnend(3, dev, "(i2400m %p arg %p args %zu) = %d\n",
1222 i2400m, arg, args, result);
1223 return result;
1224
1225}
1226EXPORT_SYMBOL_GPL(i2400m_set_init_config);
1227
1228
1229/**
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +00001230 * i2400m_set_idle_timeout - Set the device's idle mode timeout
1231 *
1232 * @i2400m: i2400m device descriptor
1233 *
1234 * @msecs: milliseconds for the timeout to enter idle mode. Between
1235 * 100 to 300000 (5m); 0 to disable. In increments of 100.
1236 *
1237 * After this @msecs of the link being idle (no data being sent or
1238 * received), the device will negotiate with the basestation entering
1239 * idle mode for saving power. The connection is maintained, but
1240 * getting out of it (done in tx.c) will require some negotiation,
1241 * possible crypto re-handshake and a possible DHCP re-lease.
1242 *
1243 * Only available if fw_version >= 0x00090002.
1244 *
1245 * Returns: 0 if ok, < 0 errno code on error.
1246 */
1247int i2400m_set_idle_timeout(struct i2400m *i2400m, unsigned msecs)
1248{
1249 int result;
1250 struct device *dev = i2400m_dev(i2400m);
1251 struct sk_buff *ack_skb;
1252 struct {
1253 struct i2400m_l3l4_hdr hdr;
1254 struct i2400m_tlv_config_idle_timeout cit;
1255 } *cmd;
1256 const struct i2400m_l3l4_hdr *ack;
1257 size_t ack_len;
1258 char strerr[32];
1259
1260 result = -ENOSYS;
1261 if (i2400m_le_v1_3(i2400m))
1262 goto error_alloc;
1263 result = -ENOMEM;
1264 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1265 if (cmd == NULL)
1266 goto error_alloc;
1267 cmd->hdr.type = cpu_to_le16(I2400M_MT_GET_STATE);
1268 cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
1269 cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
1270
1271 cmd->cit.hdr.type =
1272 cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
1273 cmd->cit.hdr.length = cpu_to_le16(sizeof(cmd->cit.timeout));
1274 cmd->cit.timeout = cpu_to_le32(msecs);
1275
1276 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
1277 if (IS_ERR(ack_skb)) {
1278 dev_err(dev, "Failed to issue 'set idle timeout' command: "
1279 "%ld\n", PTR_ERR(ack_skb));
1280 result = PTR_ERR(ack_skb);
1281 goto error_msg_to_dev;
1282 }
1283 ack = wimax_msg_data_len(ack_skb, &ack_len);
1284 result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
1285 if (result < 0) {
1286 dev_err(dev, "'set idle timeout' (0x%04x) command failed: "
1287 "%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
1288 goto error_cmd_failed;
1289 }
1290 result = 0;
1291 kfree_skb(ack_skb);
1292error_cmd_failed:
1293error_msg_to_dev:
1294 kfree(cmd);
1295error_alloc:
1296 return result;
1297}
1298
1299
1300/**
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001301 * i2400m_dev_initialize - Initialize the device once communications are ready
1302 *
1303 * @i2400m: device descriptor
1304 *
1305 * Returns: 0 if ok, < 0 errno code on error.
1306 *
1307 * Configures the device to work the way we like it.
1308 *
1309 * At the point of this call, the device is registered with the WiMAX
1310 * and netdev stacks, firmware is uploaded and we can talk to the
1311 * device normally.
1312 */
1313int i2400m_dev_initialize(struct i2400m *i2400m)
1314{
1315 int result;
1316 struct device *dev = i2400m_dev(i2400m);
1317 struct i2400m_tlv_config_idle_parameters idle_params;
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +00001318 struct i2400m_tlv_config_idle_timeout idle_timeout;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001319 struct i2400m_tlv_config_d2h_data_format df;
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001320 struct i2400m_tlv_config_dl_host_reorder dlhr;
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001321 const struct i2400m_tlv_hdr *args[9];
1322 unsigned argc = 0;
1323
1324 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001325 /* Disable idle mode? (enabled by default) */
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001326 if (i2400m_idle_mode_disabled) {
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +00001327 if (i2400m_le_v1_3(i2400m)) {
1328 idle_params.hdr.type =
1329 cpu_to_le16(I2400M_TLV_CONFIG_IDLE_PARAMETERS);
1330 idle_params.hdr.length = cpu_to_le16(
1331 sizeof(idle_params) - sizeof(idle_params.hdr));
1332 idle_params.idle_timeout = 0;
1333 idle_params.idle_paging_interval = 0;
1334 args[argc++] = &idle_params.hdr;
1335 } else {
1336 idle_timeout.hdr.type =
1337 cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
1338 idle_timeout.hdr.length = cpu_to_le16(
1339 sizeof(idle_timeout) - sizeof(idle_timeout.hdr));
1340 idle_timeout.timeout = 0;
1341 args[argc++] = &idle_timeout.hdr;
1342 }
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001343 }
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001344 if (i2400m_ge_v1_4(i2400m)) {
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001345 /* Enable extended RX data format? */
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001346 df.hdr.type =
1347 cpu_to_le16(I2400M_TLV_CONFIG_D2H_DATA_FORMAT);
1348 df.hdr.length = cpu_to_le16(
1349 sizeof(df) - sizeof(df.hdr));
1350 df.format = 1;
1351 args[argc++] = &df.hdr;
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001352
1353 /* Enable RX data reordering?
1354 * (switch flipped in rx.c:i2400m_rx_setup() after fw upload) */
1355 if (i2400m->rx_reorder) {
1356 dlhr.hdr.type =
1357 cpu_to_le16(I2400M_TLV_CONFIG_DL_HOST_REORDER);
1358 dlhr.hdr.length = cpu_to_le16(
1359 sizeof(dlhr) - sizeof(dlhr.hdr));
1360 dlhr.reorder = 1;
1361 args[argc++] = &dlhr.hdr;
1362 }
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001363 }
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001364 result = i2400m_set_init_config(i2400m, args, argc);
1365 if (result < 0)
1366 goto error;
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001367 /*
1368 * Update state: Here it just calls a get state; parsing the
1369 * result (System State TLV and RF Status TLV [done in the rx
1370 * path hooks]) will set the hardware and software RF-Kill
1371 * status.
1372 */
1373 result = i2400m_cmd_get_state(i2400m);
1374error:
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +00001375 if (result < 0)
1376 dev_err(dev, "failed to initialize the device: %d\n", result);
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001377 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
1378 return result;
1379}
1380
1381
1382/**
1383 * i2400m_dev_shutdown - Shutdown a running device
1384 *
1385 * @i2400m: device descriptor
1386 *
1387 * Gracefully stops the device, moving it to the lowest power
1388 * consumption state possible.
1389 */
1390void i2400m_dev_shutdown(struct i2400m *i2400m)
1391{
1392 int result = -ENODEV;
1393 struct device *dev = i2400m_dev(i2400m);
1394
1395 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
1396 result = i2400m->bus_reset(i2400m, I2400M_RT_WARM);
1397 d_fnend(3, dev, "(i2400m %p) = void [%d]\n", i2400m, result);
1398 return;
1399}