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