blob: b42e347279835b56d9d7fb3b5627e1a3b132d674 [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/*
Inaky Perez-Gonzalez8ac11012009-04-25 00:26:14 -0700403 * Process a TLV from a 'state report'
404 *
405 * @i2400m: device descriptor
406 * @tlv: pointer to the TLV header; it has been already validated for
407 * consistent size.
408 * @tag: for error messages
409 *
410 * Act on the TLVs from a 'state report'.
411 */
412static
413void i2400m_report_state_parse_tlv(struct i2400m *i2400m,
414 const struct i2400m_tlv_hdr *tlv,
415 const char *tag)
416{
417 struct device *dev = i2400m_dev(i2400m);
418 const struct i2400m_tlv_media_status *ms;
419 const struct i2400m_tlv_system_state *ss;
420 const struct i2400m_tlv_rf_switches_status *rfss;
421
422 if (0 == i2400m_tlv_match(tlv, I2400M_TLV_SYSTEM_STATE, sizeof(*ss))) {
423 ss = container_of(tlv, typeof(*ss), hdr);
424 d_printf(2, dev, "%s: system state TLV "
425 "found (0x%04x), state 0x%08x\n",
426 tag, I2400M_TLV_SYSTEM_STATE,
427 le32_to_cpu(ss->state));
428 i2400m_report_tlv_system_state(i2400m, ss);
429 }
430 if (0 == i2400m_tlv_match(tlv, I2400M_TLV_RF_STATUS, sizeof(*rfss))) {
431 rfss = container_of(tlv, typeof(*rfss), hdr);
432 d_printf(2, dev, "%s: RF status TLV "
433 "found (0x%04x), sw 0x%02x hw 0x%02x\n",
434 tag, I2400M_TLV_RF_STATUS,
435 le32_to_cpu(rfss->sw_rf_switch),
436 le32_to_cpu(rfss->hw_rf_switch));
437 i2400m_report_tlv_rf_switches_status(i2400m, rfss);
438 }
439 if (0 == i2400m_tlv_match(tlv, I2400M_TLV_MEDIA_STATUS, sizeof(*ms))) {
440 ms = container_of(tlv, typeof(*ms), hdr);
441 d_printf(2, dev, "%s: Media Status TLV: %u\n",
442 tag, le32_to_cpu(ms->media_status));
443 i2400m_report_tlv_media_status(i2400m, ms);
444 }
445}
446
447
448/*
449 * Parse a 'state report' and extract information
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800450 *
451 * @i2400m: device descriptor
452 * @l3l4_hdr: pointer to message; it has been already validated for
453 * consistent size.
454 * @size: size of the message (header + payload). The header length
455 * declaration is assumed to be congruent with @size (as in
456 * sizeof(*l3l4_hdr) + l3l4_hdr->length == size)
457 *
Inaky Perez-Gonzalez8ac11012009-04-25 00:26:14 -0700458 * Walk over the TLVs in a report state and act on them.
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800459 */
460static
461void i2400m_report_state_hook(struct i2400m *i2400m,
462 const struct i2400m_l3l4_hdr *l3l4_hdr,
463 size_t size, const char *tag)
464{
465 struct device *dev = i2400m_dev(i2400m);
466 const struct i2400m_tlv_hdr *tlv;
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800467 size_t tlv_size = le16_to_cpu(l3l4_hdr->length);
468
469 d_fnstart(4, dev, "(i2400m %p, l3l4_hdr %p, size %zu, %s)\n",
470 i2400m, l3l4_hdr, size, tag);
471 tlv = NULL;
472
473 while ((tlv = i2400m_tlv_buffer_walk(i2400m, &l3l4_hdr->pl,
Inaky Perez-Gonzalez8ac11012009-04-25 00:26:14 -0700474 tlv_size, tlv)))
475 i2400m_report_state_parse_tlv(i2400m, tlv, tag);
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800476 d_fnend(4, dev, "(i2400m %p, l3l4_hdr %p, size %zu, %s) = void\n",
477 i2400m, l3l4_hdr, size, tag);
478}
479
480
481/*
482 * i2400m_report_hook - (maybe) act on a report
483 *
484 * @i2400m: device descriptor
485 * @l3l4_hdr: pointer to message; it has been already validated for
486 * consistent size.
487 * @size: size of the message (header + payload). The header length
488 * declaration is assumed to be congruent with @size (as in
489 * sizeof(*l3l4_hdr) + l3l4_hdr->length == size)
490 *
491 * Extract information we might need (like carrien on/off) from a
492 * device report.
493 */
494void i2400m_report_hook(struct i2400m *i2400m,
495 const struct i2400m_l3l4_hdr *l3l4_hdr, size_t size)
496{
497 struct device *dev = i2400m_dev(i2400m);
498 unsigned msg_type;
499
500 d_fnstart(3, dev, "(i2400m %p l3l4_hdr %p size %zu)\n",
501 i2400m, l3l4_hdr, size);
502 /* Chew on the message, we might need some information from
503 * here */
504 msg_type = le16_to_cpu(l3l4_hdr->type);
505 switch (msg_type) {
506 case I2400M_MT_REPORT_STATE: /* carrier detection... */
507 i2400m_report_state_hook(i2400m,
508 l3l4_hdr, size, "REPORT STATE");
509 break;
510 /* If the device is ready for power save, then ask it to do
511 * it. */
512 case I2400M_MT_REPORT_POWERSAVE_READY: /* zzzzz */
513 if (l3l4_hdr->status == cpu_to_le16(I2400M_MS_DONE_OK)) {
514 d_printf(1, dev, "ready for powersave, requesting\n");
515 i2400m_cmd_enter_powersave(i2400m);
516 }
517 break;
518 };
519 d_fnend(3, dev, "(i2400m %p l3l4_hdr %p size %zu) = void\n",
520 i2400m, l3l4_hdr, size);
521}
522
523
524/*
525 * i2400m_msg_ack_hook - process cmd/set/get ack for internal status
526 *
527 * @i2400m: device descriptor
528 * @l3l4_hdr: pointer to message; it has been already validated for
529 * consistent size.
530 * @size: size of the message
531 *
532 * Extract information we might need from acks to commands and act on
533 * it. This is akin to i2400m_report_hook(). Note most of this
534 * processing should be done in the function that calls the
535 * command. This is here for some cases where it can't happen...
536 */
537void i2400m_msg_ack_hook(struct i2400m *i2400m,
538 const struct i2400m_l3l4_hdr *l3l4_hdr, size_t size)
539{
540 int result;
541 struct device *dev = i2400m_dev(i2400m);
542 unsigned ack_type, ack_status;
543 char strerr[32];
544
545 /* Chew on the message, we might need some information from
546 * here */
547 ack_type = le16_to_cpu(l3l4_hdr->type);
548 ack_status = le16_to_cpu(l3l4_hdr->status);
549 switch (ack_type) {
550 case I2400M_MT_CMD_ENTER_POWERSAVE:
551 /* This is just left here for the sake of example, as
552 * the processing is done somewhere else. */
553 if (0) {
554 result = i2400m_msg_check_status(
555 l3l4_hdr, strerr, sizeof(strerr));
556 if (result >= 0)
557 d_printf(1, dev, "ready for power save: %zd\n",
558 size);
559 }
560 break;
561 };
562 return;
563}
564
565
566/*
567 * i2400m_msg_size_check() - verify message size and header are congruent
568 *
569 * It is ok if the total message size is larger than the expected
570 * size, as there can be padding.
571 */
572int i2400m_msg_size_check(struct i2400m *i2400m,
573 const struct i2400m_l3l4_hdr *l3l4_hdr,
574 size_t msg_size)
575{
576 int result;
577 struct device *dev = i2400m_dev(i2400m);
578 size_t expected_size;
579 d_fnstart(4, dev, "(i2400m %p l3l4_hdr %p msg_size %zu)\n",
580 i2400m, l3l4_hdr, msg_size);
581 if (msg_size < sizeof(*l3l4_hdr)) {
582 dev_err(dev, "bad size for message header "
583 "(expected at least %zu, got %zu)\n",
584 (size_t) sizeof(*l3l4_hdr), msg_size);
585 result = -EIO;
586 goto error_hdr_size;
587 }
588 expected_size = le16_to_cpu(l3l4_hdr->length) + sizeof(*l3l4_hdr);
589 if (msg_size < expected_size) {
590 dev_err(dev, "bad size for message code 0x%04x (expected %zu, "
591 "got %zu)\n", le16_to_cpu(l3l4_hdr->type),
592 expected_size, msg_size);
593 result = -EIO;
594 } else
595 result = 0;
596error_hdr_size:
597 d_fnend(4, dev,
598 "(i2400m %p l3l4_hdr %p msg_size %zu) = %d\n",
599 i2400m, l3l4_hdr, msg_size, result);
600 return result;
601}
602
603
604
605/*
606 * Cancel a wait for a command ACK
607 *
608 * @i2400m: device descriptor
609 * @code: [negative] errno code to cancel with (don't use
610 * -EINPROGRESS)
611 *
612 * If there is an ack already filled out, free it.
613 */
614void i2400m_msg_to_dev_cancel_wait(struct i2400m *i2400m, int code)
615{
616 struct sk_buff *ack_skb;
617 unsigned long flags;
618
619 spin_lock_irqsave(&i2400m->rx_lock, flags);
620 ack_skb = i2400m->ack_skb;
621 if (ack_skb && !IS_ERR(ack_skb))
Inaky Perez-Gonzalezf4895b82009-01-19 13:19:30 +0000622 kfree_skb(ack_skb);
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800623 i2400m->ack_skb = ERR_PTR(code);
624 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
625}
626
627
628/**
629 * i2400m_msg_to_dev - Send a control message to the device and get a response
630 *
631 * @i2400m: device descriptor
632 *
633 * @msg_skb: an skb *
634 *
635 * @buf: pointer to the buffer containing the message to be sent; it
636 * has to start with a &struct i2400M_l3l4_hdr and then
637 * followed by the payload. Once this function returns, the
638 * buffer can be reused.
639 *
640 * @buf_len: buffer size
641 *
642 * Returns:
643 *
644 * Pointer to skb containing the ack message. You need to check the
645 * pointer with IS_ERR(), as it might be an error code. Error codes
646 * could happen because:
647 *
648 * - the message wasn't formatted correctly
649 * - couldn't send the message
650 * - failed waiting for a response
651 * - the ack message wasn't formatted correctly
652 *
653 * The returned skb has been allocated with wimax_msg_to_user_alloc(),
654 * it contains the reponse in a netlink attribute and is ready to be
655 * passed up to user space with wimax_msg_to_user_send(). To access
656 * the payload and its length, use wimax_msg_{data,len}() on the skb.
657 *
658 * The skb has to be freed with kfree_skb() once done.
659 *
660 * Description:
661 *
662 * This function delivers a message/command to the device and waits
663 * for an ack to be received. The format is described in
664 * linux/wimax/i2400m.h. In summary, a command/get/set is followed by an
665 * ack.
666 *
667 * This function will not check the ack status, that's left up to the
668 * caller. Once done with the ack skb, it has to be kfree_skb()ed.
669 *
670 * The i2400m handles only one message at the same time, thus we need
671 * the mutex to exclude other players.
672 *
673 * We write the message and then wait for an answer to come back. The
674 * RX path intercepts control messages and handles them in
675 * i2400m_rx_ctl(). Reports (notifications) are (maybe) processed
676 * locally and then forwarded (as needed) to user space on the WiMAX
677 * stack message pipe. Acks are saved and passed back to us through an
678 * skb in i2400m->ack_skb which is ready to be given to generic
679 * netlink if need be.
680 */
681struct sk_buff *i2400m_msg_to_dev(struct i2400m *i2400m,
682 const void *buf, size_t buf_len)
683{
684 int result;
685 struct device *dev = i2400m_dev(i2400m);
686 const struct i2400m_l3l4_hdr *msg_l3l4_hdr;
687 struct sk_buff *ack_skb;
688 const struct i2400m_l3l4_hdr *ack_l3l4_hdr;
689 size_t ack_len;
690 int ack_timeout;
691 unsigned msg_type;
692 unsigned long flags;
693
694 d_fnstart(3, dev, "(i2400m %p buf %p len %zu)\n",
695 i2400m, buf, buf_len);
696
697 if (i2400m->boot_mode)
698 return ERR_PTR(-ENODEV);
699
700 msg_l3l4_hdr = buf;
701 /* Check msg & payload consistency */
702 result = i2400m_msg_size_check(i2400m, msg_l3l4_hdr, buf_len);
703 if (result < 0)
704 goto error_bad_msg;
705 msg_type = le16_to_cpu(msg_l3l4_hdr->type);
706 d_printf(1, dev, "CMD/GET/SET 0x%04x %zu bytes\n",
707 msg_type, buf_len);
708 d_dump(2, dev, buf, buf_len);
709
710 /* Setup the completion, ack_skb ("we are waiting") and send
711 * the message to the device */
712 mutex_lock(&i2400m->msg_mutex);
713 spin_lock_irqsave(&i2400m->rx_lock, flags);
714 i2400m->ack_skb = ERR_PTR(-EINPROGRESS);
715 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
716 init_completion(&i2400m->msg_completion);
717 result = i2400m_tx(i2400m, buf, buf_len, I2400M_PT_CTRL);
718 if (result < 0) {
719 dev_err(dev, "can't send message 0x%04x: %d\n",
720 le16_to_cpu(msg_l3l4_hdr->type), result);
721 goto error_tx;
722 }
723
724 /* Some commands take longer to execute because of crypto ops,
725 * so we give them some more leeway on timeout */
726 switch (msg_type) {
727 case I2400M_MT_GET_TLS_OPERATION_RESULT:
728 case I2400M_MT_CMD_SEND_EAP_RESPONSE:
729 ack_timeout = 5 * HZ;
730 break;
731 default:
732 ack_timeout = HZ;
733 };
734
Inaky Perez-Gonzalez223beea2009-04-13 10:26:34 -0700735 if (unlikely(i2400m->trace_msg_from_user))
736 wimax_msg(&i2400m->wimax_dev, "echo", buf, buf_len, GFP_KERNEL);
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800737 /* The RX path in rx.c will put any response for this message
738 * in i2400m->ack_skb and wake us up. If we cancel the wait,
739 * we need to change the value of i2400m->ack_skb to something
740 * not -EINPROGRESS so RX knows there is no one waiting. */
741 result = wait_for_completion_interruptible_timeout(
742 &i2400m->msg_completion, ack_timeout);
743 if (result == 0) {
744 dev_err(dev, "timeout waiting for reply to message 0x%04x\n",
745 msg_type);
746 result = -ETIMEDOUT;
747 i2400m_msg_to_dev_cancel_wait(i2400m, result);
748 goto error_wait_for_completion;
749 } else if (result < 0) {
750 dev_err(dev, "error waiting for reply to message 0x%04x: %d\n",
751 msg_type, result);
752 i2400m_msg_to_dev_cancel_wait(i2400m, result);
753 goto error_wait_for_completion;
754 }
755
756 /* Pull out the ack data from i2400m->ack_skb -- see if it is
757 * an error and act accordingly */
758 spin_lock_irqsave(&i2400m->rx_lock, flags);
759 ack_skb = i2400m->ack_skb;
760 if (IS_ERR(ack_skb))
761 result = PTR_ERR(ack_skb);
762 else
763 result = 0;
764 i2400m->ack_skb = NULL;
765 spin_unlock_irqrestore(&i2400m->rx_lock, flags);
766 if (result < 0)
767 goto error_ack_status;
768 ack_l3l4_hdr = wimax_msg_data_len(ack_skb, &ack_len);
769
770 /* Check the ack and deliver it if it is ok */
Inaky Perez-Gonzalez223beea2009-04-13 10:26:34 -0700771 if (unlikely(i2400m->trace_msg_from_user))
772 wimax_msg(&i2400m->wimax_dev, "echo",
773 ack_l3l4_hdr, ack_len, GFP_KERNEL);
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800774 result = i2400m_msg_size_check(i2400m, ack_l3l4_hdr, ack_len);
775 if (result < 0) {
776 dev_err(dev, "HW BUG? reply to message 0x%04x: %d\n",
777 msg_type, result);
778 goto error_bad_ack_len;
779 }
780 if (msg_type != le16_to_cpu(ack_l3l4_hdr->type)) {
781 dev_err(dev, "HW BUG? bad reply 0x%04x to message 0x%04x\n",
782 le16_to_cpu(ack_l3l4_hdr->type), msg_type);
783 result = -EIO;
784 goto error_bad_ack_type;
785 }
786 i2400m_msg_ack_hook(i2400m, ack_l3l4_hdr, ack_len);
787 mutex_unlock(&i2400m->msg_mutex);
788 d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %p\n",
789 i2400m, buf, buf_len, ack_skb);
790 return ack_skb;
791
792error_bad_ack_type:
793error_bad_ack_len:
794 kfree_skb(ack_skb);
795error_ack_status:
796error_wait_for_completion:
797error_tx:
798 mutex_unlock(&i2400m->msg_mutex);
799error_bad_msg:
800 d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %d\n",
801 i2400m, buf, buf_len, result);
802 return ERR_PTR(result);
803}
804
805
806/*
807 * Definitions for the Enter Power Save command
808 *
809 * The Enter Power Save command requests the device to go into power
810 * saving mode. The device will ack or nak the command depending on it
811 * being ready for it. If it acks, we tell the USB subsystem to
812 *
813 * As well, the device might request to go into power saving mode by
814 * sending a report (REPORT_POWERSAVE_READY), in which case, we issue
815 * this command. The hookups in the RX coder allow
816 */
817enum {
818 I2400M_WAKEUP_ENABLED = 0x01,
819 I2400M_WAKEUP_DISABLED = 0x02,
820 I2400M_TLV_TYPE_WAKEUP_MODE = 144,
821};
822
823struct i2400m_cmd_enter_power_save {
824 struct i2400m_l3l4_hdr hdr;
825 struct i2400m_tlv_hdr tlv;
826 __le32 val;
827} __attribute__((packed));
828
829
830/*
831 * Request entering power save
832 *
833 * This command is (mainly) executed when the device indicates that it
834 * is ready to go into powersave mode via a REPORT_POWERSAVE_READY.
835 */
836int i2400m_cmd_enter_powersave(struct i2400m *i2400m)
837{
838 int result;
839 struct device *dev = i2400m_dev(i2400m);
840 struct sk_buff *ack_skb;
841 struct i2400m_cmd_enter_power_save *cmd;
842 char strerr[32];
843
844 result = -ENOMEM;
845 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
846 if (cmd == NULL)
847 goto error_alloc;
848 cmd->hdr.type = cpu_to_le16(I2400M_MT_CMD_ENTER_POWERSAVE);
849 cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
850 cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
851 cmd->tlv.type = cpu_to_le16(I2400M_TLV_TYPE_WAKEUP_MODE);
852 cmd->tlv.length = cpu_to_le16(sizeof(cmd->val));
853 cmd->val = cpu_to_le32(I2400M_WAKEUP_ENABLED);
854
855 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
856 result = PTR_ERR(ack_skb);
857 if (IS_ERR(ack_skb)) {
858 dev_err(dev, "Failed to issue 'Enter power save' command: %d\n",
859 result);
860 goto error_msg_to_dev;
861 }
862 result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
863 strerr, sizeof(strerr));
864 if (result == -EACCES)
865 d_printf(1, dev, "Cannot enter power save mode\n");
866 else if (result < 0)
867 dev_err(dev, "'Enter power save' (0x%04x) command failed: "
868 "%d - %s\n", I2400M_MT_CMD_ENTER_POWERSAVE,
869 result, strerr);
870 else
871 d_printf(1, dev, "device ready to power save\n");
872 kfree_skb(ack_skb);
873error_msg_to_dev:
874 kfree(cmd);
875error_alloc:
876 return result;
877}
878EXPORT_SYMBOL_GPL(i2400m_cmd_enter_powersave);
879
880
881/*
882 * Definitions for getting device information
883 */
884enum {
885 I2400M_TLV_DETAILED_DEVICE_INFO = 140
886};
887
888/**
889 * i2400m_get_device_info - Query the device for detailed device information
890 *
891 * @i2400m: device descriptor
892 *
893 * Returns: an skb whose skb->data points to a 'struct
894 * i2400m_tlv_detailed_device_info'. When done, kfree_skb() it. The
895 * skb is *guaranteed* to contain the whole TLV data structure.
896 *
897 * On error, IS_ERR(skb) is true and ERR_PTR(skb) is the error
898 * code.
899 */
900struct sk_buff *i2400m_get_device_info(struct i2400m *i2400m)
901{
902 int result;
903 struct device *dev = i2400m_dev(i2400m);
904 struct sk_buff *ack_skb;
905 struct i2400m_l3l4_hdr *cmd;
906 const struct i2400m_l3l4_hdr *ack;
907 size_t ack_len;
908 const struct i2400m_tlv_hdr *tlv;
909 const struct i2400m_tlv_detailed_device_info *ddi;
910 char strerr[32];
911
912 ack_skb = ERR_PTR(-ENOMEM);
913 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
914 if (cmd == NULL)
915 goto error_alloc;
916 cmd->type = cpu_to_le16(I2400M_MT_GET_DEVICE_INFO);
917 cmd->length = 0;
918 cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
919
920 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
921 if (IS_ERR(ack_skb)) {
922 dev_err(dev, "Failed to issue 'get device info' command: %ld\n",
923 PTR_ERR(ack_skb));
924 goto error_msg_to_dev;
925 }
926 ack = wimax_msg_data_len(ack_skb, &ack_len);
927 result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
928 if (result < 0) {
929 dev_err(dev, "'get device info' (0x%04x) command failed: "
930 "%d - %s\n", I2400M_MT_GET_DEVICE_INFO, result,
931 strerr);
932 goto error_cmd_failed;
933 }
934 tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
935 I2400M_TLV_DETAILED_DEVICE_INFO, sizeof(*ddi));
936 if (tlv == NULL) {
937 dev_err(dev, "GET DEVICE INFO: "
938 "detailed device info TLV not found (0x%04x)\n",
939 I2400M_TLV_DETAILED_DEVICE_INFO);
940 result = -EIO;
941 goto error_no_tlv;
942 }
943 skb_pull(ack_skb, (void *) tlv - (void *) ack_skb->data);
944error_msg_to_dev:
945 kfree(cmd);
946error_alloc:
947 return ack_skb;
948
949error_no_tlv:
950error_cmd_failed:
951 kfree_skb(ack_skb);
952 kfree(cmd);
953 return ERR_PTR(result);
954}
955
956
957/* Firmware interface versions we support */
958enum {
959 I2400M_HDIv_MAJOR = 9,
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800960 I2400M_HDIv_MINOR = 1,
Inaky Perez-Gonzalezefa05d02009-02-28 23:42:48 +0000961 I2400M_HDIv_MINOR_2 = 2,
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800962};
963
964
965/**
966 * i2400m_firmware_check - check firmware versions are compatible with
967 * the driver
968 *
969 * @i2400m: device descriptor
970 *
971 * Returns: 0 if ok, < 0 errno code an error and a message in the
972 * kernel log.
973 *
974 * Long function, but quite simple; first chunk launches the command
975 * and double checks the reply for the right TLV. Then we process the
976 * TLV (where the meat is).
Inaky Perez-Gonzalez6a0f7ab2009-02-28 23:42:49 +0000977 *
978 * Once we process the TLV that gives us the firmware's interface
979 * version, we encode it and save it in i2400m->fw_version for future
980 * reference.
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -0800981 */
982int i2400m_firmware_check(struct i2400m *i2400m)
983{
984 int result;
985 struct device *dev = i2400m_dev(i2400m);
986 struct sk_buff *ack_skb;
987 struct i2400m_l3l4_hdr *cmd;
988 const struct i2400m_l3l4_hdr *ack;
989 size_t ack_len;
990 const struct i2400m_tlv_hdr *tlv;
991 const struct i2400m_tlv_l4_message_versions *l4mv;
992 char strerr[32];
993 unsigned major, minor, branch;
994
995 result = -ENOMEM;
996 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
997 if (cmd == NULL)
998 goto error_alloc;
999 cmd->type = cpu_to_le16(I2400M_MT_GET_LM_VERSION);
1000 cmd->length = 0;
1001 cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
1002
1003 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
1004 if (IS_ERR(ack_skb)) {
1005 result = PTR_ERR(ack_skb);
1006 dev_err(dev, "Failed to issue 'get lm version' command: %-d\n",
1007 result);
1008 goto error_msg_to_dev;
1009 }
1010 ack = wimax_msg_data_len(ack_skb, &ack_len);
1011 result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
1012 if (result < 0) {
1013 dev_err(dev, "'get lm version' (0x%04x) command failed: "
1014 "%d - %s\n", I2400M_MT_GET_LM_VERSION, result,
1015 strerr);
1016 goto error_cmd_failed;
1017 }
1018 tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
1019 I2400M_TLV_L4_MESSAGE_VERSIONS, sizeof(*l4mv));
1020 if (tlv == NULL) {
1021 dev_err(dev, "get lm version: TLV not found (0x%04x)\n",
1022 I2400M_TLV_L4_MESSAGE_VERSIONS);
1023 result = -EIO;
1024 goto error_no_tlv;
1025 }
1026 l4mv = container_of(tlv, typeof(*l4mv), hdr);
1027 major = le16_to_cpu(l4mv->major);
1028 minor = le16_to_cpu(l4mv->minor);
1029 branch = le16_to_cpu(l4mv->branch);
1030 result = -EINVAL;
Inaky Perez-Gonzalezefa05d02009-02-28 23:42:48 +00001031 if (major != I2400M_HDIv_MAJOR) {
1032 dev_err(dev, "unsupported major fw version "
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001033 "%u.%u.%u\n", major, minor, branch);
1034 goto error_bad_major;
1035 }
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001036 result = 0;
Inaky Perez-Gonzalezefa05d02009-02-28 23:42:48 +00001037 if (minor < I2400M_HDIv_MINOR_2 && minor > I2400M_HDIv_MINOR)
1038 dev_warn(dev, "untested minor fw version %u.%u.%u\n",
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001039 major, minor, branch);
Inaky Perez-Gonzalez6a0f7ab2009-02-28 23:42:49 +00001040 /* Yes, we ignore the branch -- we don't have to track it */
1041 i2400m->fw_version = major << 16 | minor;
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001042 dev_info(dev, "firmware interface version %u.%u.%u\n",
1043 major, minor, branch);
Inaky Perez-Gonzalez6a0f7ab2009-02-28 23:42:49 +00001044error_bad_major:
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001045error_no_tlv:
1046error_cmd_failed:
1047 kfree_skb(ack_skb);
1048error_msg_to_dev:
1049 kfree(cmd);
1050error_alloc:
1051 return result;
1052}
1053
1054
1055/*
1056 * Send an DoExitIdle command to the device to ask it to go out of
1057 * basestation-idle mode.
1058 *
1059 * @i2400m: device descriptor
1060 *
1061 * This starts a renegotiation with the basestation that might involve
1062 * another crypto handshake with user space.
1063 *
1064 * Returns: 0 if ok, < 0 errno code on error.
1065 */
1066int i2400m_cmd_exit_idle(struct i2400m *i2400m)
1067{
1068 int result;
1069 struct device *dev = i2400m_dev(i2400m);
1070 struct sk_buff *ack_skb;
1071 struct i2400m_l3l4_hdr *cmd;
1072 char strerr[32];
1073
1074 result = -ENOMEM;
1075 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1076 if (cmd == NULL)
1077 goto error_alloc;
1078 cmd->type = cpu_to_le16(I2400M_MT_CMD_EXIT_IDLE);
1079 cmd->length = 0;
1080 cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
1081
1082 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
1083 result = PTR_ERR(ack_skb);
1084 if (IS_ERR(ack_skb)) {
1085 dev_err(dev, "Failed to issue 'exit idle' command: %d\n",
1086 result);
1087 goto error_msg_to_dev;
1088 }
1089 result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
1090 strerr, sizeof(strerr));
1091 kfree_skb(ack_skb);
1092error_msg_to_dev:
1093 kfree(cmd);
1094error_alloc:
1095 return result;
1096
1097}
1098
1099
1100/*
1101 * Query the device for its state, update the WiMAX stack's idea of it
1102 *
1103 * @i2400m: device descriptor
1104 *
1105 * Returns: 0 if ok, < 0 errno code on error.
1106 *
1107 * Executes a 'Get State' command and parses the returned
1108 * TLVs.
1109 *
1110 * Because this is almost identical to a 'Report State', we use
1111 * i2400m_report_state_hook() to parse the answer. This will set the
1112 * carrier state, as well as the RF Kill switches state.
1113 */
1114int i2400m_cmd_get_state(struct i2400m *i2400m)
1115{
1116 int result;
1117 struct device *dev = i2400m_dev(i2400m);
1118 struct sk_buff *ack_skb;
1119 struct i2400m_l3l4_hdr *cmd;
1120 const struct i2400m_l3l4_hdr *ack;
1121 size_t ack_len;
1122 char strerr[32];
1123
1124 result = -ENOMEM;
1125 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1126 if (cmd == NULL)
1127 goto error_alloc;
1128 cmd->type = cpu_to_le16(I2400M_MT_GET_STATE);
1129 cmd->length = 0;
1130 cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
1131
1132 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
1133 if (IS_ERR(ack_skb)) {
1134 dev_err(dev, "Failed to issue 'get state' command: %ld\n",
1135 PTR_ERR(ack_skb));
1136 result = PTR_ERR(ack_skb);
1137 goto error_msg_to_dev;
1138 }
1139 ack = wimax_msg_data_len(ack_skb, &ack_len);
1140 result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
1141 if (result < 0) {
1142 dev_err(dev, "'get state' (0x%04x) command failed: "
1143 "%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
1144 goto error_cmd_failed;
1145 }
1146 i2400m_report_state_hook(i2400m, ack, ack_len - sizeof(*ack),
1147 "GET STATE");
1148 result = 0;
1149 kfree_skb(ack_skb);
1150error_cmd_failed:
1151error_msg_to_dev:
1152 kfree(cmd);
1153error_alloc:
1154 return result;
1155}
1156EXPORT_SYMBOL_GPL(i2400m_cmd_get_state);
1157
1158
1159/**
1160 * Set basic configuration settings
1161 *
1162 * @i2400m: device descriptor
1163 * @args: array of pointers to the TLV headers to send for
1164 * configuration (each followed by its payload).
1165 * TLV headers and payloads must be properly initialized, with the
1166 * right endianess (LE).
1167 * @arg_size: number of pointers in the @args array
1168 */
1169int i2400m_set_init_config(struct i2400m *i2400m,
1170 const struct i2400m_tlv_hdr **arg, size_t args)
1171{
1172 int result;
1173 struct device *dev = i2400m_dev(i2400m);
1174 struct sk_buff *ack_skb;
1175 struct i2400m_l3l4_hdr *cmd;
1176 char strerr[32];
1177 unsigned argc, argsize, tlv_size;
1178 const struct i2400m_tlv_hdr *tlv_hdr;
1179 void *buf, *itr;
1180
1181 d_fnstart(3, dev, "(i2400m %p arg %p args %zu)\n", i2400m, arg, args);
1182 result = 0;
1183 if (args == 0)
1184 goto none;
1185 /* Compute the size of all the TLVs, so we can alloc a
1186 * contiguous command block to copy them. */
1187 argsize = 0;
1188 for (argc = 0; argc < args; argc++) {
1189 tlv_hdr = arg[argc];
1190 argsize += sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
1191 }
1192 WARN_ON(argc >= 9); /* As per hw spec */
1193
1194 /* Alloc the space for the command and TLVs*/
1195 result = -ENOMEM;
1196 buf = kzalloc(sizeof(*cmd) + argsize, GFP_KERNEL);
1197 if (buf == NULL)
1198 goto error_alloc;
1199 cmd = buf;
1200 cmd->type = cpu_to_le16(I2400M_MT_SET_INIT_CONFIG);
1201 cmd->length = cpu_to_le16(argsize);
1202 cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
1203
1204 /* Copy the TLVs */
1205 itr = buf + sizeof(*cmd);
1206 for (argc = 0; argc < args; argc++) {
1207 tlv_hdr = arg[argc];
1208 tlv_size = sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
1209 memcpy(itr, tlv_hdr, tlv_size);
1210 itr += tlv_size;
1211 }
1212
1213 /* Send the message! */
1214 ack_skb = i2400m_msg_to_dev(i2400m, buf, sizeof(*cmd) + argsize);
1215 result = PTR_ERR(ack_skb);
1216 if (IS_ERR(ack_skb)) {
1217 dev_err(dev, "Failed to issue 'init config' command: %d\n",
1218 result);
1219
1220 goto error_msg_to_dev;
1221 }
1222 result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
1223 strerr, sizeof(strerr));
1224 if (result < 0)
1225 dev_err(dev, "'init config' (0x%04x) command failed: %d - %s\n",
1226 I2400M_MT_SET_INIT_CONFIG, result, strerr);
1227 kfree_skb(ack_skb);
1228error_msg_to_dev:
1229 kfree(buf);
1230error_alloc:
1231none:
1232 d_fnend(3, dev, "(i2400m %p arg %p args %zu) = %d\n",
1233 i2400m, arg, args, result);
1234 return result;
1235
1236}
1237EXPORT_SYMBOL_GPL(i2400m_set_init_config);
1238
1239
1240/**
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +00001241 * i2400m_set_idle_timeout - Set the device's idle mode timeout
1242 *
1243 * @i2400m: i2400m device descriptor
1244 *
1245 * @msecs: milliseconds for the timeout to enter idle mode. Between
1246 * 100 to 300000 (5m); 0 to disable. In increments of 100.
1247 *
1248 * After this @msecs of the link being idle (no data being sent or
1249 * received), the device will negotiate with the basestation entering
1250 * idle mode for saving power. The connection is maintained, but
1251 * getting out of it (done in tx.c) will require some negotiation,
1252 * possible crypto re-handshake and a possible DHCP re-lease.
1253 *
1254 * Only available if fw_version >= 0x00090002.
1255 *
1256 * Returns: 0 if ok, < 0 errno code on error.
1257 */
1258int i2400m_set_idle_timeout(struct i2400m *i2400m, unsigned msecs)
1259{
1260 int result;
1261 struct device *dev = i2400m_dev(i2400m);
1262 struct sk_buff *ack_skb;
1263 struct {
1264 struct i2400m_l3l4_hdr hdr;
1265 struct i2400m_tlv_config_idle_timeout cit;
1266 } *cmd;
1267 const struct i2400m_l3l4_hdr *ack;
1268 size_t ack_len;
1269 char strerr[32];
1270
1271 result = -ENOSYS;
1272 if (i2400m_le_v1_3(i2400m))
1273 goto error_alloc;
1274 result = -ENOMEM;
1275 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1276 if (cmd == NULL)
1277 goto error_alloc;
1278 cmd->hdr.type = cpu_to_le16(I2400M_MT_GET_STATE);
1279 cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
1280 cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
1281
1282 cmd->cit.hdr.type =
1283 cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
1284 cmd->cit.hdr.length = cpu_to_le16(sizeof(cmd->cit.timeout));
1285 cmd->cit.timeout = cpu_to_le32(msecs);
1286
1287 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
1288 if (IS_ERR(ack_skb)) {
1289 dev_err(dev, "Failed to issue 'set idle timeout' command: "
1290 "%ld\n", PTR_ERR(ack_skb));
1291 result = PTR_ERR(ack_skb);
1292 goto error_msg_to_dev;
1293 }
1294 ack = wimax_msg_data_len(ack_skb, &ack_len);
1295 result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
1296 if (result < 0) {
1297 dev_err(dev, "'set idle timeout' (0x%04x) command failed: "
1298 "%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
1299 goto error_cmd_failed;
1300 }
1301 result = 0;
1302 kfree_skb(ack_skb);
1303error_cmd_failed:
1304error_msg_to_dev:
1305 kfree(cmd);
1306error_alloc:
1307 return result;
1308}
1309
1310
1311/**
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001312 * i2400m_dev_initialize - Initialize the device once communications are ready
1313 *
1314 * @i2400m: device descriptor
1315 *
1316 * Returns: 0 if ok, < 0 errno code on error.
1317 *
1318 * Configures the device to work the way we like it.
1319 *
1320 * At the point of this call, the device is registered with the WiMAX
1321 * and netdev stacks, firmware is uploaded and we can talk to the
1322 * device normally.
1323 */
1324int i2400m_dev_initialize(struct i2400m *i2400m)
1325{
1326 int result;
1327 struct device *dev = i2400m_dev(i2400m);
1328 struct i2400m_tlv_config_idle_parameters idle_params;
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +00001329 struct i2400m_tlv_config_idle_timeout idle_timeout;
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001330 struct i2400m_tlv_config_d2h_data_format df;
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001331 struct i2400m_tlv_config_dl_host_reorder dlhr;
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001332 const struct i2400m_tlv_hdr *args[9];
1333 unsigned argc = 0;
1334
1335 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001336 /* Disable idle mode? (enabled by default) */
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001337 if (i2400m_idle_mode_disabled) {
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +00001338 if (i2400m_le_v1_3(i2400m)) {
1339 idle_params.hdr.type =
1340 cpu_to_le16(I2400M_TLV_CONFIG_IDLE_PARAMETERS);
1341 idle_params.hdr.length = cpu_to_le16(
1342 sizeof(idle_params) - sizeof(idle_params.hdr));
1343 idle_params.idle_timeout = 0;
1344 idle_params.idle_paging_interval = 0;
1345 args[argc++] = &idle_params.hdr;
1346 } else {
1347 idle_timeout.hdr.type =
1348 cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
1349 idle_timeout.hdr.length = cpu_to_le16(
1350 sizeof(idle_timeout) - sizeof(idle_timeout.hdr));
1351 idle_timeout.timeout = 0;
1352 args[argc++] = &idle_timeout.hdr;
1353 }
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001354 }
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001355 if (i2400m_ge_v1_4(i2400m)) {
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001356 /* Enable extended RX data format? */
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001357 df.hdr.type =
1358 cpu_to_le16(I2400M_TLV_CONFIG_D2H_DATA_FORMAT);
1359 df.hdr.length = cpu_to_le16(
1360 sizeof(df) - sizeof(df.hdr));
1361 df.format = 1;
1362 args[argc++] = &df.hdr;
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +00001363
1364 /* Enable RX data reordering?
1365 * (switch flipped in rx.c:i2400m_rx_setup() after fw upload) */
1366 if (i2400m->rx_reorder) {
1367 dlhr.hdr.type =
1368 cpu_to_le16(I2400M_TLV_CONFIG_DL_HOST_REORDER);
1369 dlhr.hdr.length = cpu_to_le16(
1370 sizeof(dlhr) - sizeof(dlhr.hdr));
1371 dlhr.reorder = 1;
1372 args[argc++] = &dlhr.hdr;
1373 }
Inaky Perez-Gonzalezfd5c5652009-02-28 23:42:52 +00001374 }
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001375 result = i2400m_set_init_config(i2400m, args, argc);
1376 if (result < 0)
1377 goto error;
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001378 /*
1379 * Update state: Here it just calls a get state; parsing the
1380 * result (System State TLV and RF Status TLV [done in the rx
1381 * path hooks]) will set the hardware and software RF-Kill
1382 * status.
1383 */
1384 result = i2400m_cmd_get_state(i2400m);
1385error:
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +00001386 if (result < 0)
1387 dev_err(dev, "failed to initialize the device: %d\n", result);
Inaky Perez-Gonzalez3a35a1d2008-12-20 16:57:48 -08001388 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
1389 return result;
1390}
1391
1392
1393/**
1394 * i2400m_dev_shutdown - Shutdown a running device
1395 *
1396 * @i2400m: device descriptor
1397 *
1398 * Gracefully stops the device, moving it to the lowest power
1399 * consumption state possible.
1400 */
1401void i2400m_dev_shutdown(struct i2400m *i2400m)
1402{
1403 int result = -ENODEV;
1404 struct device *dev = i2400m_dev(i2400m);
1405
1406 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
1407 result = i2400m->bus_reset(i2400m, I2400M_RT_WARM);
1408 d_fnend(3, dev, "(i2400m %p) = void [%d]\n", i2400m, result);
1409 return;
1410}