control.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. /*
  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()
  55. * i2400m_cmd_get_state()
  56. * i2400m_dev_shutdown() Called by i2400m_dev_stop()
  57. * i2400m_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. #include <stdarg.h>
  76. #include "i2400m.h"
  77. #include <linux/kernel.h>
  78. #include <linux/wimax/i2400m.h>
  79. #define D_SUBMODULE control
  80. #include "debug-levels.h"
  81. int i2400m_passive_mode; /* 0 (passive mode disabled) by default */
  82. module_param_named(passive_mode, i2400m_passive_mode, int, 0644);
  83. MODULE_PARM_DESC(passive_mode,
  84. "If true, the driver will not do any device setup "
  85. "and leave it up to user space, who must be properly "
  86. "setup.");
  87. /*
  88. * Return if a TLV is of a give type and size
  89. *
  90. * @tlv_hdr: pointer to the TLV
  91. * @tlv_type: type of the TLV we are looking for
  92. * @tlv_size: expected size of the TLV we are looking for (if -1,
  93. * don't check the size). This includes the header
  94. * Returns: 0 if the TLV matches
  95. * < 0 if it doesn't match at all
  96. * > 0 total TLV + payload size, if the type matches, but not
  97. * the size
  98. */
  99. static
  100. ssize_t i2400m_tlv_match(const struct i2400m_tlv_hdr *tlv,
  101. enum i2400m_tlv tlv_type, ssize_t tlv_size)
  102. {
  103. if (le16_to_cpu(tlv->type) != tlv_type) /* Not our type? skip */
  104. return -1;
  105. if (tlv_size != -1
  106. && le16_to_cpu(tlv->length) + sizeof(*tlv) != tlv_size) {
  107. size_t size = le16_to_cpu(tlv->length) + sizeof(*tlv);
  108. printk(KERN_WARNING "W: tlv type 0x%x mismatched because of "
  109. "size (got %zu vs %zu expected)\n",
  110. tlv_type, size, tlv_size);
  111. return size;
  112. }
  113. return 0;
  114. }
  115. /*
  116. * Given a buffer of TLVs, iterate over them
  117. *
  118. * @i2400m: device instance
  119. * @tlv_buf: pointer to the beginning of the TLV buffer
  120. * @buf_size: buffer size in bytes
  121. * @tlv_pos: seek position; this is assumed to be a pointer returned
  122. * by i2400m_tlv_buffer_walk() [and thus, validated]. The
  123. * TLV returned will be the one following this one.
  124. *
  125. * Usage:
  126. *
  127. * tlv_itr = NULL;
  128. * while (tlv_itr = i2400m_tlv_buffer_walk(i2400m, buf, size, tlv_itr)) {
  129. * ...
  130. * // Do stuff with tlv_itr, DON'T MODIFY IT
  131. * ...
  132. * }
  133. */
  134. static
  135. const struct i2400m_tlv_hdr *i2400m_tlv_buffer_walk(
  136. struct i2400m *i2400m,
  137. const void *tlv_buf, size_t buf_size,
  138. const struct i2400m_tlv_hdr *tlv_pos)
  139. {
  140. struct device *dev = i2400m_dev(i2400m);
  141. const struct i2400m_tlv_hdr *tlv_top = tlv_buf + buf_size;
  142. size_t offset, length, avail_size;
  143. unsigned type;
  144. if (tlv_pos == NULL) /* Take the first one? */
  145. tlv_pos = tlv_buf;
  146. else /* Nope, the next one */
  147. tlv_pos = (void *) tlv_pos
  148. + le16_to_cpu(tlv_pos->length) + sizeof(*tlv_pos);
  149. if (tlv_pos == tlv_top) { /* buffer done */
  150. tlv_pos = NULL;
  151. goto error_beyond_end;
  152. }
  153. if (tlv_pos > tlv_top) {
  154. tlv_pos = NULL;
  155. WARN_ON(1);
  156. goto error_beyond_end;
  157. }
  158. offset = (void *) tlv_pos - (void *) tlv_buf;
  159. avail_size = buf_size - offset;
  160. if (avail_size < sizeof(*tlv_pos)) {
  161. dev_err(dev, "HW BUG? tlv_buf %p [%zu bytes], tlv @%zu: "
  162. "short header\n", tlv_buf, buf_size, offset);
  163. goto error_short_header;
  164. }
  165. type = le16_to_cpu(tlv_pos->type);
  166. length = le16_to_cpu(tlv_pos->length);
  167. if (avail_size < sizeof(*tlv_pos) + length) {
  168. dev_err(dev, "HW BUG? tlv_buf %p [%zu bytes], "
  169. "tlv type 0x%04x @%zu: "
  170. "short data (%zu bytes vs %zu needed)\n",
  171. tlv_buf, buf_size, type, offset, avail_size,
  172. sizeof(*tlv_pos) + length);
  173. goto error_short_header;
  174. }
  175. error_short_header:
  176. error_beyond_end:
  177. return tlv_pos;
  178. }
  179. /*
  180. * Find a TLV in a buffer of sequential TLVs
  181. *
  182. * @i2400m: device descriptor
  183. * @tlv_hdr: pointer to the first TLV in the sequence
  184. * @size: size of the buffer in bytes; all TLVs are assumed to fit
  185. * fully in the buffer (otherwise we'll complain).
  186. * @tlv_type: type of the TLV we are looking for
  187. * @tlv_size: expected size of the TLV we are looking for (if -1,
  188. * don't check the size). This includes the header
  189. *
  190. * Returns: NULL if the TLV is not found, otherwise a pointer to
  191. * it. If the sizes don't match, an error is printed and NULL
  192. * returned.
  193. */
  194. static
  195. const struct i2400m_tlv_hdr *i2400m_tlv_find(
  196. struct i2400m *i2400m,
  197. const struct i2400m_tlv_hdr *tlv_hdr, size_t size,
  198. enum i2400m_tlv tlv_type, ssize_t tlv_size)
  199. {
  200. ssize_t match;
  201. struct device *dev = i2400m_dev(i2400m);
  202. const struct i2400m_tlv_hdr *tlv = NULL;
  203. while ((tlv = i2400m_tlv_buffer_walk(i2400m, tlv_hdr, size, tlv))) {
  204. match = i2400m_tlv_match(tlv, tlv_type, tlv_size);
  205. if (match == 0) /* found it :) */
  206. break;
  207. if (match > 0)
  208. dev_warn(dev, "TLV type 0x%04x found with size "
  209. "mismatch (%zu vs %zu needed)\n",
  210. tlv_type, match, tlv_size);
  211. }
  212. return tlv;
  213. }
  214. static const struct
  215. {
  216. char *msg;
  217. int errno;
  218. } ms_to_errno[I2400M_MS_MAX] = {
  219. [I2400M_MS_DONE_OK] = { "", 0 },
  220. [I2400M_MS_DONE_IN_PROGRESS] = { "", 0 },
  221. [I2400M_MS_INVALID_OP] = { "invalid opcode", -ENOSYS },
  222. [I2400M_MS_BAD_STATE] = { "invalid state", -EILSEQ },
  223. [I2400M_MS_ILLEGAL_VALUE] = { "illegal value", -EINVAL },
  224. [I2400M_MS_MISSING_PARAMS] = { "missing parameters", -ENOMSG },
  225. [I2400M_MS_VERSION_ERROR] = { "bad version", -EIO },
  226. [I2400M_MS_ACCESSIBILITY_ERROR] = { "accesibility error", -EIO },
  227. [I2400M_MS_BUSY] = { "busy", -EBUSY },
  228. [I2400M_MS_CORRUPTED_TLV] = { "corrupted TLV", -EILSEQ },
  229. [I2400M_MS_UNINITIALIZED] = { "not unitialized", -EILSEQ },
  230. [I2400M_MS_UNKNOWN_ERROR] = { "unknown error", -EIO },
  231. [I2400M_MS_PRODUCTION_ERROR] = { "production error", -EIO },
  232. [I2400M_MS_NO_RF] = { "no RF", -EIO },
  233. [I2400M_MS_NOT_READY_FOR_POWERSAVE] =
  234. { "not ready for powersave", -EACCES },
  235. [I2400M_MS_THERMAL_CRITICAL] = { "thermal critical", -EL3HLT },
  236. };
  237. /*
  238. * i2400m_msg_check_status - translate a message's status code
  239. *
  240. * @i2400m: device descriptor
  241. * @l3l4_hdr: message header
  242. * @strbuf: buffer to place a formatted error message (unless NULL).
  243. * @strbuf_size: max amount of available space; larger messages will
  244. * be truncated.
  245. *
  246. * Returns: errno code corresponding to the status code in @l3l4_hdr
  247. * and a message in @strbuf describing the error.
  248. */
  249. int i2400m_msg_check_status(const struct i2400m_l3l4_hdr *l3l4_hdr,
  250. char *strbuf, size_t strbuf_size)
  251. {
  252. int result;
  253. enum i2400m_ms status = le16_to_cpu(l3l4_hdr->status);
  254. const char *str;
  255. if (status == 0)
  256. return 0;
  257. if (status >= ARRAY_SIZE(ms_to_errno)) {
  258. str = "unknown status code";
  259. result = -EBADR;
  260. } else {
  261. str = ms_to_errno[status].msg;
  262. result = ms_to_errno[status].errno;
  263. }
  264. if (strbuf)
  265. snprintf(strbuf, strbuf_size, "%s (%d)", str, status);
  266. return result;
  267. }
  268. /*
  269. * Act on a TLV System State reported by the device
  270. *
  271. * @i2400m: device descriptor
  272. * @ss: validated System State TLV
  273. */
  274. static
  275. void i2400m_report_tlv_system_state(struct i2400m *i2400m,
  276. const struct i2400m_tlv_system_state *ss)
  277. {
  278. struct device *dev = i2400m_dev(i2400m);
  279. struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  280. enum i2400m_system_state i2400m_state = le32_to_cpu(ss->state);
  281. d_fnstart(3, dev, "(i2400m %p ss %p [%u])\n", i2400m, ss, i2400m_state);
  282. if (i2400m->state != i2400m_state) {
  283. i2400m->state = i2400m_state;
  284. wake_up_all(&i2400m->state_wq);
  285. }
  286. switch (i2400m_state) {
  287. case I2400M_SS_UNINITIALIZED:
  288. case I2400M_SS_INIT:
  289. case I2400M_SS_CONFIG:
  290. case I2400M_SS_PRODUCTION:
  291. wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
  292. break;
  293. case I2400M_SS_RF_OFF:
  294. case I2400M_SS_RF_SHUTDOWN:
  295. wimax_state_change(wimax_dev, WIMAX_ST_RADIO_OFF);
  296. break;
  297. case I2400M_SS_READY:
  298. case I2400M_SS_STANDBY:
  299. case I2400M_SS_SLEEPACTIVE:
  300. wimax_state_change(wimax_dev, WIMAX_ST_READY);
  301. break;
  302. case I2400M_SS_CONNECTING:
  303. case I2400M_SS_WIMAX_CONNECTED:
  304. wimax_state_change(wimax_dev, WIMAX_ST_READY);
  305. break;
  306. case I2400M_SS_SCAN:
  307. case I2400M_SS_OUT_OF_ZONE:
  308. wimax_state_change(wimax_dev, WIMAX_ST_SCANNING);
  309. break;
  310. case I2400M_SS_IDLE:
  311. d_printf(1, dev, "entering BS-negotiated idle mode\n");
  312. case I2400M_SS_DISCONNECTING:
  313. case I2400M_SS_DATA_PATH_CONNECTED:
  314. wimax_state_change(wimax_dev, WIMAX_ST_CONNECTED);
  315. break;
  316. default:
  317. /* Huh? just in case, shut it down */
  318. dev_err(dev, "HW BUG? unknown state %u: shutting down\n",
  319. i2400m_state);
  320. i2400m_reset(i2400m, I2400M_RT_WARM);
  321. break;
  322. };
  323. d_fnend(3, dev, "(i2400m %p ss %p [%u]) = void\n",
  324. i2400m, ss, i2400m_state);
  325. }
  326. /*
  327. * Parse and act on a TLV Media Status sent by the device
  328. *
  329. * @i2400m: device descriptor
  330. * @ms: validated Media Status TLV
  331. *
  332. * This will set the carrier up on down based on the device's link
  333. * report. This is done asides of what the WiMAX stack does based on
  334. * the device's state as sometimes we need to do a link-renew (the BS
  335. * wants us to renew a DHCP lease, for example).
  336. *
  337. * In fact, doc says that everytime we get a link-up, we should do a
  338. * DHCP negotiation...
  339. */
  340. static
  341. void i2400m_report_tlv_media_status(struct i2400m *i2400m,
  342. const struct i2400m_tlv_media_status *ms)
  343. {
  344. struct device *dev = i2400m_dev(i2400m);
  345. struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  346. struct net_device *net_dev = wimax_dev->net_dev;
  347. enum i2400m_media_status status = le32_to_cpu(ms->media_status);
  348. d_fnstart(3, dev, "(i2400m %p ms %p [%u])\n", i2400m, ms, status);
  349. switch (status) {
  350. case I2400M_MEDIA_STATUS_LINK_UP:
  351. netif_carrier_on(net_dev);
  352. break;
  353. case I2400M_MEDIA_STATUS_LINK_DOWN:
  354. netif_carrier_off(net_dev);
  355. break;
  356. /*
  357. * This is the network telling us we need to retrain the DHCP
  358. * lease -- so far, we are trusting the WiMAX Network Service
  359. * in user space to pick this up and poke the DHCP client.
  360. */
  361. case I2400M_MEDIA_STATUS_LINK_RENEW:
  362. netif_carrier_on(net_dev);
  363. break;
  364. default:
  365. dev_err(dev, "HW BUG? unknown media status %u\n",
  366. status);
  367. };
  368. d_fnend(3, dev, "(i2400m %p ms %p [%u]) = void\n",
  369. i2400m, ms, status);
  370. }
  371. /*
  372. * Process a TLV from a 'state report'
  373. *
  374. * @i2400m: device descriptor
  375. * @tlv: pointer to the TLV header; it has been already validated for
  376. * consistent size.
  377. * @tag: for error messages
  378. *
  379. * Act on the TLVs from a 'state report'.
  380. */
  381. static
  382. void i2400m_report_state_parse_tlv(struct i2400m *i2400m,
  383. const struct i2400m_tlv_hdr *tlv,
  384. const char *tag)
  385. {
  386. struct device *dev = i2400m_dev(i2400m);
  387. const struct i2400m_tlv_media_status *ms;
  388. const struct i2400m_tlv_system_state *ss;
  389. const struct i2400m_tlv_rf_switches_status *rfss;
  390. if (0 == i2400m_tlv_match(tlv, I2400M_TLV_SYSTEM_STATE, sizeof(*ss))) {
  391. ss = container_of(tlv, typeof(*ss), hdr);
  392. d_printf(2, dev, "%s: system state TLV "
  393. "found (0x%04x), state 0x%08x\n",
  394. tag, I2400M_TLV_SYSTEM_STATE,
  395. le32_to_cpu(ss->state));
  396. i2400m_report_tlv_system_state(i2400m, ss);
  397. }
  398. if (0 == i2400m_tlv_match(tlv, I2400M_TLV_RF_STATUS, sizeof(*rfss))) {
  399. rfss = container_of(tlv, typeof(*rfss), hdr);
  400. d_printf(2, dev, "%s: RF status TLV "
  401. "found (0x%04x), sw 0x%02x hw 0x%02x\n",
  402. tag, I2400M_TLV_RF_STATUS,
  403. le32_to_cpu(rfss->sw_rf_switch),
  404. le32_to_cpu(rfss->hw_rf_switch));
  405. i2400m_report_tlv_rf_switches_status(i2400m, rfss);
  406. }
  407. if (0 == i2400m_tlv_match(tlv, I2400M_TLV_MEDIA_STATUS, sizeof(*ms))) {
  408. ms = container_of(tlv, typeof(*ms), hdr);
  409. d_printf(2, dev, "%s: Media Status TLV: %u\n",
  410. tag, le32_to_cpu(ms->media_status));
  411. i2400m_report_tlv_media_status(i2400m, ms);
  412. }
  413. }
  414. /*
  415. * Parse a 'state report' and extract information
  416. *
  417. * @i2400m: device descriptor
  418. * @l3l4_hdr: pointer to message; it has been already validated for
  419. * consistent size.
  420. * @size: size of the message (header + payload). The header length
  421. * declaration is assumed to be congruent with @size (as in
  422. * sizeof(*l3l4_hdr) + l3l4_hdr->length == size)
  423. *
  424. * Walk over the TLVs in a report state and act on them.
  425. */
  426. static
  427. void i2400m_report_state_hook(struct i2400m *i2400m,
  428. const struct i2400m_l3l4_hdr *l3l4_hdr,
  429. size_t size, const char *tag)
  430. {
  431. struct device *dev = i2400m_dev(i2400m);
  432. const struct i2400m_tlv_hdr *tlv;
  433. size_t tlv_size = le16_to_cpu(l3l4_hdr->length);
  434. d_fnstart(4, dev, "(i2400m %p, l3l4_hdr %p, size %zu, %s)\n",
  435. i2400m, l3l4_hdr, size, tag);
  436. tlv = NULL;
  437. while ((tlv = i2400m_tlv_buffer_walk(i2400m, &l3l4_hdr->pl,
  438. tlv_size, tlv)))
  439. i2400m_report_state_parse_tlv(i2400m, tlv, tag);
  440. d_fnend(4, dev, "(i2400m %p, l3l4_hdr %p, size %zu, %s) = void\n",
  441. i2400m, l3l4_hdr, size, tag);
  442. }
  443. /*
  444. * i2400m_report_hook - (maybe) act on a report
  445. *
  446. * @i2400m: device descriptor
  447. * @l3l4_hdr: pointer to message; it has been already validated for
  448. * consistent size.
  449. * @size: size of the message (header + payload). The header length
  450. * declaration is assumed to be congruent with @size (as in
  451. * sizeof(*l3l4_hdr) + l3l4_hdr->length == size)
  452. *
  453. * Extract information we might need (like carrien on/off) from a
  454. * device report.
  455. */
  456. void i2400m_report_hook(struct i2400m *i2400m,
  457. const struct i2400m_l3l4_hdr *l3l4_hdr, size_t size)
  458. {
  459. struct device *dev = i2400m_dev(i2400m);
  460. unsigned msg_type;
  461. d_fnstart(3, dev, "(i2400m %p l3l4_hdr %p size %zu)\n",
  462. i2400m, l3l4_hdr, size);
  463. /* Chew on the message, we might need some information from
  464. * here */
  465. msg_type = le16_to_cpu(l3l4_hdr->type);
  466. switch (msg_type) {
  467. case I2400M_MT_REPORT_STATE: /* carrier detection... */
  468. i2400m_report_state_hook(i2400m,
  469. l3l4_hdr, size, "REPORT STATE");
  470. break;
  471. /* If the device is ready for power save, then ask it to do
  472. * it. */
  473. case I2400M_MT_REPORT_POWERSAVE_READY: /* zzzzz */
  474. if (l3l4_hdr->status == cpu_to_le16(I2400M_MS_DONE_OK)) {
  475. if (i2400m_power_save_disabled)
  476. d_printf(1, dev, "ready for powersave, "
  477. "not requesting (disabled by module "
  478. "parameter)\n");
  479. else {
  480. d_printf(1, dev, "ready for powersave, "
  481. "requesting\n");
  482. i2400m_cmd_enter_powersave(i2400m);
  483. }
  484. }
  485. break;
  486. };
  487. d_fnend(3, dev, "(i2400m %p l3l4_hdr %p size %zu) = void\n",
  488. i2400m, l3l4_hdr, size);
  489. }
  490. /*
  491. * i2400m_msg_ack_hook - process cmd/set/get ack for internal status
  492. *
  493. * @i2400m: device descriptor
  494. * @l3l4_hdr: pointer to message; it has been already validated for
  495. * consistent size.
  496. * @size: size of the message
  497. *
  498. * Extract information we might need from acks to commands and act on
  499. * it. This is akin to i2400m_report_hook(). Note most of this
  500. * processing should be done in the function that calls the
  501. * command. This is here for some cases where it can't happen...
  502. */
  503. void i2400m_msg_ack_hook(struct i2400m *i2400m,
  504. const struct i2400m_l3l4_hdr *l3l4_hdr, size_t size)
  505. {
  506. int result;
  507. struct device *dev = i2400m_dev(i2400m);
  508. unsigned ack_type, ack_status;
  509. char strerr[32];
  510. /* Chew on the message, we might need some information from
  511. * here */
  512. ack_type = le16_to_cpu(l3l4_hdr->type);
  513. ack_status = le16_to_cpu(l3l4_hdr->status);
  514. switch (ack_type) {
  515. case I2400M_MT_CMD_ENTER_POWERSAVE:
  516. /* This is just left here for the sake of example, as
  517. * the processing is done somewhere else. */
  518. if (0) {
  519. result = i2400m_msg_check_status(
  520. l3l4_hdr, strerr, sizeof(strerr));
  521. if (result >= 0)
  522. d_printf(1, dev, "ready for power save: %zd\n",
  523. size);
  524. }
  525. break;
  526. };
  527. return;
  528. }
  529. /*
  530. * i2400m_msg_size_check() - verify message size and header are congruent
  531. *
  532. * It is ok if the total message size is larger than the expected
  533. * size, as there can be padding.
  534. */
  535. int i2400m_msg_size_check(struct i2400m *i2400m,
  536. const struct i2400m_l3l4_hdr *l3l4_hdr,
  537. size_t msg_size)
  538. {
  539. int result;
  540. struct device *dev = i2400m_dev(i2400m);
  541. size_t expected_size;
  542. d_fnstart(4, dev, "(i2400m %p l3l4_hdr %p msg_size %zu)\n",
  543. i2400m, l3l4_hdr, msg_size);
  544. if (msg_size < sizeof(*l3l4_hdr)) {
  545. dev_err(dev, "bad size for message header "
  546. "(expected at least %zu, got %zu)\n",
  547. (size_t) sizeof(*l3l4_hdr), msg_size);
  548. result = -EIO;
  549. goto error_hdr_size;
  550. }
  551. expected_size = le16_to_cpu(l3l4_hdr->length) + sizeof(*l3l4_hdr);
  552. if (msg_size < expected_size) {
  553. dev_err(dev, "bad size for message code 0x%04x (expected %zu, "
  554. "got %zu)\n", le16_to_cpu(l3l4_hdr->type),
  555. expected_size, msg_size);
  556. result = -EIO;
  557. } else
  558. result = 0;
  559. error_hdr_size:
  560. d_fnend(4, dev,
  561. "(i2400m %p l3l4_hdr %p msg_size %zu) = %d\n",
  562. i2400m, l3l4_hdr, msg_size, result);
  563. return result;
  564. }
  565. /*
  566. * Cancel a wait for a command ACK
  567. *
  568. * @i2400m: device descriptor
  569. * @code: [negative] errno code to cancel with (don't use
  570. * -EINPROGRESS)
  571. *
  572. * If there is an ack already filled out, free it.
  573. */
  574. void i2400m_msg_to_dev_cancel_wait(struct i2400m *i2400m, int code)
  575. {
  576. struct sk_buff *ack_skb;
  577. unsigned long flags;
  578. spin_lock_irqsave(&i2400m->rx_lock, flags);
  579. ack_skb = i2400m->ack_skb;
  580. if (ack_skb && !IS_ERR(ack_skb))
  581. kfree_skb(ack_skb);
  582. i2400m->ack_skb = ERR_PTR(code);
  583. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  584. }
  585. /**
  586. * i2400m_msg_to_dev - Send a control message to the device and get a response
  587. *
  588. * @i2400m: device descriptor
  589. *
  590. * @msg_skb: an skb *
  591. *
  592. * @buf: pointer to the buffer containing the message to be sent; it
  593. * has to start with a &struct i2400M_l3l4_hdr and then
  594. * followed by the payload. Once this function returns, the
  595. * buffer can be reused.
  596. *
  597. * @buf_len: buffer size
  598. *
  599. * Returns:
  600. *
  601. * Pointer to skb containing the ack message. You need to check the
  602. * pointer with IS_ERR(), as it might be an error code. Error codes
  603. * could happen because:
  604. *
  605. * - the message wasn't formatted correctly
  606. * - couldn't send the message
  607. * - failed waiting for a response
  608. * - the ack message wasn't formatted correctly
  609. *
  610. * The returned skb has been allocated with wimax_msg_to_user_alloc(),
  611. * it contains the reponse in a netlink attribute and is ready to be
  612. * passed up to user space with wimax_msg_to_user_send(). To access
  613. * the payload and its length, use wimax_msg_{data,len}() on the skb.
  614. *
  615. * The skb has to be freed with kfree_skb() once done.
  616. *
  617. * Description:
  618. *
  619. * This function delivers a message/command to the device and waits
  620. * for an ack to be received. The format is described in
  621. * linux/wimax/i2400m.h. In summary, a command/get/set is followed by an
  622. * ack.
  623. *
  624. * This function will not check the ack status, that's left up to the
  625. * caller. Once done with the ack skb, it has to be kfree_skb()ed.
  626. *
  627. * The i2400m handles only one message at the same time, thus we need
  628. * the mutex to exclude other players.
  629. *
  630. * We write the message and then wait for an answer to come back. The
  631. * RX path intercepts control messages and handles them in
  632. * i2400m_rx_ctl(). Reports (notifications) are (maybe) processed
  633. * locally and then forwarded (as needed) to user space on the WiMAX
  634. * stack message pipe. Acks are saved and passed back to us through an
  635. * skb in i2400m->ack_skb which is ready to be given to generic
  636. * netlink if need be.
  637. */
  638. struct sk_buff *i2400m_msg_to_dev(struct i2400m *i2400m,
  639. const void *buf, size_t buf_len)
  640. {
  641. int result;
  642. struct device *dev = i2400m_dev(i2400m);
  643. const struct i2400m_l3l4_hdr *msg_l3l4_hdr;
  644. struct sk_buff *ack_skb;
  645. const struct i2400m_l3l4_hdr *ack_l3l4_hdr;
  646. size_t ack_len;
  647. int ack_timeout;
  648. unsigned msg_type;
  649. unsigned long flags;
  650. d_fnstart(3, dev, "(i2400m %p buf %p len %zu)\n",
  651. i2400m, buf, buf_len);
  652. rmb(); /* Make sure we see what i2400m_dev_reset_handle() */
  653. if (i2400m->boot_mode)
  654. return ERR_PTR(-EL3RST);
  655. msg_l3l4_hdr = buf;
  656. /* Check msg & payload consistency */
  657. result = i2400m_msg_size_check(i2400m, msg_l3l4_hdr, buf_len);
  658. if (result < 0)
  659. goto error_bad_msg;
  660. msg_type = le16_to_cpu(msg_l3l4_hdr->type);
  661. d_printf(1, dev, "CMD/GET/SET 0x%04x %zu bytes\n",
  662. msg_type, buf_len);
  663. d_dump(2, dev, buf, buf_len);
  664. /* Setup the completion, ack_skb ("we are waiting") and send
  665. * the message to the device */
  666. mutex_lock(&i2400m->msg_mutex);
  667. spin_lock_irqsave(&i2400m->rx_lock, flags);
  668. i2400m->ack_skb = ERR_PTR(-EINPROGRESS);
  669. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  670. init_completion(&i2400m->msg_completion);
  671. result = i2400m_tx(i2400m, buf, buf_len, I2400M_PT_CTRL);
  672. if (result < 0) {
  673. dev_err(dev, "can't send message 0x%04x: %d\n",
  674. le16_to_cpu(msg_l3l4_hdr->type), result);
  675. goto error_tx;
  676. }
  677. /* Some commands take longer to execute because of crypto ops,
  678. * so we give them some more leeway on timeout */
  679. switch (msg_type) {
  680. case I2400M_MT_GET_TLS_OPERATION_RESULT:
  681. case I2400M_MT_CMD_SEND_EAP_RESPONSE:
  682. ack_timeout = 5 * HZ;
  683. break;
  684. default:
  685. ack_timeout = HZ;
  686. };
  687. if (unlikely(i2400m->trace_msg_from_user))
  688. wimax_msg(&i2400m->wimax_dev, "echo", buf, buf_len, GFP_KERNEL);
  689. /* The RX path in rx.c will put any response for this message
  690. * in i2400m->ack_skb and wake us up. If we cancel the wait,
  691. * we need to change the value of i2400m->ack_skb to something
  692. * not -EINPROGRESS so RX knows there is no one waiting. */
  693. result = wait_for_completion_interruptible_timeout(
  694. &i2400m->msg_completion, ack_timeout);
  695. if (result == 0) {
  696. dev_err(dev, "timeout waiting for reply to message 0x%04x\n",
  697. msg_type);
  698. result = -ETIMEDOUT;
  699. i2400m_msg_to_dev_cancel_wait(i2400m, result);
  700. goto error_wait_for_completion;
  701. } else if (result < 0) {
  702. dev_err(dev, "error waiting for reply to message 0x%04x: %d\n",
  703. msg_type, result);
  704. i2400m_msg_to_dev_cancel_wait(i2400m, result);
  705. goto error_wait_for_completion;
  706. }
  707. /* Pull out the ack data from i2400m->ack_skb -- see if it is
  708. * an error and act accordingly */
  709. spin_lock_irqsave(&i2400m->rx_lock, flags);
  710. ack_skb = i2400m->ack_skb;
  711. if (IS_ERR(ack_skb))
  712. result = PTR_ERR(ack_skb);
  713. else
  714. result = 0;
  715. i2400m->ack_skb = NULL;
  716. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  717. if (result < 0)
  718. goto error_ack_status;
  719. ack_l3l4_hdr = wimax_msg_data_len(ack_skb, &ack_len);
  720. /* Check the ack and deliver it if it is ok */
  721. if (unlikely(i2400m->trace_msg_from_user))
  722. wimax_msg(&i2400m->wimax_dev, "echo",
  723. ack_l3l4_hdr, ack_len, GFP_KERNEL);
  724. result = i2400m_msg_size_check(i2400m, ack_l3l4_hdr, ack_len);
  725. if (result < 0) {
  726. dev_err(dev, "HW BUG? reply to message 0x%04x: %d\n",
  727. msg_type, result);
  728. goto error_bad_ack_len;
  729. }
  730. if (msg_type != le16_to_cpu(ack_l3l4_hdr->type)) {
  731. dev_err(dev, "HW BUG? bad reply 0x%04x to message 0x%04x\n",
  732. le16_to_cpu(ack_l3l4_hdr->type), msg_type);
  733. result = -EIO;
  734. goto error_bad_ack_type;
  735. }
  736. i2400m_msg_ack_hook(i2400m, ack_l3l4_hdr, ack_len);
  737. mutex_unlock(&i2400m->msg_mutex);
  738. d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %p\n",
  739. i2400m, buf, buf_len, ack_skb);
  740. return ack_skb;
  741. error_bad_ack_type:
  742. error_bad_ack_len:
  743. kfree_skb(ack_skb);
  744. error_ack_status:
  745. error_wait_for_completion:
  746. error_tx:
  747. mutex_unlock(&i2400m->msg_mutex);
  748. error_bad_msg:
  749. d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %d\n",
  750. i2400m, buf, buf_len, result);
  751. return ERR_PTR(result);
  752. }
  753. /*
  754. * Definitions for the Enter Power Save command
  755. *
  756. * The Enter Power Save command requests the device to go into power
  757. * saving mode. The device will ack or nak the command depending on it
  758. * being ready for it. If it acks, we tell the USB subsystem to
  759. *
  760. * As well, the device might request to go into power saving mode by
  761. * sending a report (REPORT_POWERSAVE_READY), in which case, we issue
  762. * this command. The hookups in the RX coder allow
  763. */
  764. enum {
  765. I2400M_WAKEUP_ENABLED = 0x01,
  766. I2400M_WAKEUP_DISABLED = 0x02,
  767. I2400M_TLV_TYPE_WAKEUP_MODE = 144,
  768. };
  769. struct i2400m_cmd_enter_power_save {
  770. struct i2400m_l3l4_hdr hdr;
  771. struct i2400m_tlv_hdr tlv;
  772. __le32 val;
  773. } __attribute__((packed));
  774. /*
  775. * Request entering power save
  776. *
  777. * This command is (mainly) executed when the device indicates that it
  778. * is ready to go into powersave mode via a REPORT_POWERSAVE_READY.
  779. */
  780. int i2400m_cmd_enter_powersave(struct i2400m *i2400m)
  781. {
  782. int result;
  783. struct device *dev = i2400m_dev(i2400m);
  784. struct sk_buff *ack_skb;
  785. struct i2400m_cmd_enter_power_save *cmd;
  786. char strerr[32];
  787. result = -ENOMEM;
  788. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  789. if (cmd == NULL)
  790. goto error_alloc;
  791. cmd->hdr.type = cpu_to_le16(I2400M_MT_CMD_ENTER_POWERSAVE);
  792. cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
  793. cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
  794. cmd->tlv.type = cpu_to_le16(I2400M_TLV_TYPE_WAKEUP_MODE);
  795. cmd->tlv.length = cpu_to_le16(sizeof(cmd->val));
  796. cmd->val = cpu_to_le32(I2400M_WAKEUP_ENABLED);
  797. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  798. result = PTR_ERR(ack_skb);
  799. if (IS_ERR(ack_skb)) {
  800. dev_err(dev, "Failed to issue 'Enter power save' command: %d\n",
  801. result);
  802. goto error_msg_to_dev;
  803. }
  804. result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
  805. strerr, sizeof(strerr));
  806. if (result == -EACCES)
  807. d_printf(1, dev, "Cannot enter power save mode\n");
  808. else if (result < 0)
  809. dev_err(dev, "'Enter power save' (0x%04x) command failed: "
  810. "%d - %s\n", I2400M_MT_CMD_ENTER_POWERSAVE,
  811. result, strerr);
  812. else
  813. d_printf(1, dev, "device ready to power save\n");
  814. kfree_skb(ack_skb);
  815. error_msg_to_dev:
  816. kfree(cmd);
  817. error_alloc:
  818. return result;
  819. }
  820. EXPORT_SYMBOL_GPL(i2400m_cmd_enter_powersave);
  821. /*
  822. * Definitions for getting device information
  823. */
  824. enum {
  825. I2400M_TLV_DETAILED_DEVICE_INFO = 140
  826. };
  827. /**
  828. * i2400m_get_device_info - Query the device for detailed device information
  829. *
  830. * @i2400m: device descriptor
  831. *
  832. * Returns: an skb whose skb->data points to a 'struct
  833. * i2400m_tlv_detailed_device_info'. When done, kfree_skb() it. The
  834. * skb is *guaranteed* to contain the whole TLV data structure.
  835. *
  836. * On error, IS_ERR(skb) is true and ERR_PTR(skb) is the error
  837. * code.
  838. */
  839. struct sk_buff *i2400m_get_device_info(struct i2400m *i2400m)
  840. {
  841. int result;
  842. struct device *dev = i2400m_dev(i2400m);
  843. struct sk_buff *ack_skb;
  844. struct i2400m_l3l4_hdr *cmd;
  845. const struct i2400m_l3l4_hdr *ack;
  846. size_t ack_len;
  847. const struct i2400m_tlv_hdr *tlv;
  848. const struct i2400m_tlv_detailed_device_info *ddi;
  849. char strerr[32];
  850. ack_skb = ERR_PTR(-ENOMEM);
  851. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  852. if (cmd == NULL)
  853. goto error_alloc;
  854. cmd->type = cpu_to_le16(I2400M_MT_GET_DEVICE_INFO);
  855. cmd->length = 0;
  856. cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
  857. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  858. if (IS_ERR(ack_skb)) {
  859. dev_err(dev, "Failed to issue 'get device info' command: %ld\n",
  860. PTR_ERR(ack_skb));
  861. goto error_msg_to_dev;
  862. }
  863. ack = wimax_msg_data_len(ack_skb, &ack_len);
  864. result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
  865. if (result < 0) {
  866. dev_err(dev, "'get device info' (0x%04x) command failed: "
  867. "%d - %s\n", I2400M_MT_GET_DEVICE_INFO, result,
  868. strerr);
  869. goto error_cmd_failed;
  870. }
  871. tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
  872. I2400M_TLV_DETAILED_DEVICE_INFO, sizeof(*ddi));
  873. if (tlv == NULL) {
  874. dev_err(dev, "GET DEVICE INFO: "
  875. "detailed device info TLV not found (0x%04x)\n",
  876. I2400M_TLV_DETAILED_DEVICE_INFO);
  877. result = -EIO;
  878. goto error_no_tlv;
  879. }
  880. skb_pull(ack_skb, (void *) tlv - (void *) ack_skb->data);
  881. error_msg_to_dev:
  882. kfree(cmd);
  883. error_alloc:
  884. return ack_skb;
  885. error_no_tlv:
  886. error_cmd_failed:
  887. kfree_skb(ack_skb);
  888. kfree(cmd);
  889. return ERR_PTR(result);
  890. }
  891. /* Firmware interface versions we support */
  892. enum {
  893. I2400M_HDIv_MAJOR = 9,
  894. I2400M_HDIv_MINOR = 1,
  895. I2400M_HDIv_MINOR_2 = 2,
  896. };
  897. /**
  898. * i2400m_firmware_check - check firmware versions are compatible with
  899. * the driver
  900. *
  901. * @i2400m: device descriptor
  902. *
  903. * Returns: 0 if ok, < 0 errno code an error and a message in the
  904. * kernel log.
  905. *
  906. * Long function, but quite simple; first chunk launches the command
  907. * and double checks the reply for the right TLV. Then we process the
  908. * TLV (where the meat is).
  909. *
  910. * Once we process the TLV that gives us the firmware's interface
  911. * version, we encode it and save it in i2400m->fw_version for future
  912. * reference.
  913. */
  914. int i2400m_firmware_check(struct i2400m *i2400m)
  915. {
  916. int result;
  917. struct device *dev = i2400m_dev(i2400m);
  918. struct sk_buff *ack_skb;
  919. struct i2400m_l3l4_hdr *cmd;
  920. const struct i2400m_l3l4_hdr *ack;
  921. size_t ack_len;
  922. const struct i2400m_tlv_hdr *tlv;
  923. const struct i2400m_tlv_l4_message_versions *l4mv;
  924. char strerr[32];
  925. unsigned major, minor, branch;
  926. result = -ENOMEM;
  927. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  928. if (cmd == NULL)
  929. goto error_alloc;
  930. cmd->type = cpu_to_le16(I2400M_MT_GET_LM_VERSION);
  931. cmd->length = 0;
  932. cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
  933. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  934. if (IS_ERR(ack_skb)) {
  935. result = PTR_ERR(ack_skb);
  936. dev_err(dev, "Failed to issue 'get lm version' command: %-d\n",
  937. result);
  938. goto error_msg_to_dev;
  939. }
  940. ack = wimax_msg_data_len(ack_skb, &ack_len);
  941. result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
  942. if (result < 0) {
  943. dev_err(dev, "'get lm version' (0x%04x) command failed: "
  944. "%d - %s\n", I2400M_MT_GET_LM_VERSION, result,
  945. strerr);
  946. goto error_cmd_failed;
  947. }
  948. tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
  949. I2400M_TLV_L4_MESSAGE_VERSIONS, sizeof(*l4mv));
  950. if (tlv == NULL) {
  951. dev_err(dev, "get lm version: TLV not found (0x%04x)\n",
  952. I2400M_TLV_L4_MESSAGE_VERSIONS);
  953. result = -EIO;
  954. goto error_no_tlv;
  955. }
  956. l4mv = container_of(tlv, typeof(*l4mv), hdr);
  957. major = le16_to_cpu(l4mv->major);
  958. minor = le16_to_cpu(l4mv->minor);
  959. branch = le16_to_cpu(l4mv->branch);
  960. result = -EINVAL;
  961. if (major != I2400M_HDIv_MAJOR) {
  962. dev_err(dev, "unsupported major fw version "
  963. "%u.%u.%u\n", major, minor, branch);
  964. goto error_bad_major;
  965. }
  966. result = 0;
  967. if (minor < I2400M_HDIv_MINOR_2 && minor > I2400M_HDIv_MINOR)
  968. dev_warn(dev, "untested minor fw version %u.%u.%u\n",
  969. major, minor, branch);
  970. /* Yes, we ignore the branch -- we don't have to track it */
  971. i2400m->fw_version = major << 16 | minor;
  972. dev_info(dev, "firmware interface version %u.%u.%u\n",
  973. major, minor, branch);
  974. error_bad_major:
  975. error_no_tlv:
  976. error_cmd_failed:
  977. kfree_skb(ack_skb);
  978. error_msg_to_dev:
  979. kfree(cmd);
  980. error_alloc:
  981. return result;
  982. }
  983. /*
  984. * Send an DoExitIdle command to the device to ask it to go out of
  985. * basestation-idle mode.
  986. *
  987. * @i2400m: device descriptor
  988. *
  989. * This starts a renegotiation with the basestation that might involve
  990. * another crypto handshake with user space.
  991. *
  992. * Returns: 0 if ok, < 0 errno code on error.
  993. */
  994. int i2400m_cmd_exit_idle(struct i2400m *i2400m)
  995. {
  996. int result;
  997. struct device *dev = i2400m_dev(i2400m);
  998. struct sk_buff *ack_skb;
  999. struct i2400m_l3l4_hdr *cmd;
  1000. char strerr[32];
  1001. result = -ENOMEM;
  1002. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1003. if (cmd == NULL)
  1004. goto error_alloc;
  1005. cmd->type = cpu_to_le16(I2400M_MT_CMD_EXIT_IDLE);
  1006. cmd->length = 0;
  1007. cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
  1008. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  1009. result = PTR_ERR(ack_skb);
  1010. if (IS_ERR(ack_skb)) {
  1011. dev_err(dev, "Failed to issue 'exit idle' command: %d\n",
  1012. result);
  1013. goto error_msg_to_dev;
  1014. }
  1015. result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
  1016. strerr, sizeof(strerr));
  1017. kfree_skb(ack_skb);
  1018. error_msg_to_dev:
  1019. kfree(cmd);
  1020. error_alloc:
  1021. return result;
  1022. }
  1023. /*
  1024. * Query the device for its state, update the WiMAX stack's idea of it
  1025. *
  1026. * @i2400m: device descriptor
  1027. *
  1028. * Returns: 0 if ok, < 0 errno code on error.
  1029. *
  1030. * Executes a 'Get State' command and parses the returned
  1031. * TLVs.
  1032. *
  1033. * Because this is almost identical to a 'Report State', we use
  1034. * i2400m_report_state_hook() to parse the answer. This will set the
  1035. * carrier state, as well as the RF Kill switches state.
  1036. */
  1037. int i2400m_cmd_get_state(struct i2400m *i2400m)
  1038. {
  1039. int result;
  1040. struct device *dev = i2400m_dev(i2400m);
  1041. struct sk_buff *ack_skb;
  1042. struct i2400m_l3l4_hdr *cmd;
  1043. const struct i2400m_l3l4_hdr *ack;
  1044. size_t ack_len;
  1045. char strerr[32];
  1046. result = -ENOMEM;
  1047. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1048. if (cmd == NULL)
  1049. goto error_alloc;
  1050. cmd->type = cpu_to_le16(I2400M_MT_GET_STATE);
  1051. cmd->length = 0;
  1052. cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
  1053. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  1054. if (IS_ERR(ack_skb)) {
  1055. dev_err(dev, "Failed to issue 'get state' command: %ld\n",
  1056. PTR_ERR(ack_skb));
  1057. result = PTR_ERR(ack_skb);
  1058. goto error_msg_to_dev;
  1059. }
  1060. ack = wimax_msg_data_len(ack_skb, &ack_len);
  1061. result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
  1062. if (result < 0) {
  1063. dev_err(dev, "'get state' (0x%04x) command failed: "
  1064. "%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
  1065. goto error_cmd_failed;
  1066. }
  1067. i2400m_report_state_hook(i2400m, ack, ack_len - sizeof(*ack),
  1068. "GET STATE");
  1069. result = 0;
  1070. kfree_skb(ack_skb);
  1071. error_cmd_failed:
  1072. error_msg_to_dev:
  1073. kfree(cmd);
  1074. error_alloc:
  1075. return result;
  1076. }
  1077. EXPORT_SYMBOL_GPL(i2400m_cmd_get_state);
  1078. /**
  1079. * Set basic configuration settings
  1080. *
  1081. * @i2400m: device descriptor
  1082. * @args: array of pointers to the TLV headers to send for
  1083. * configuration (each followed by its payload).
  1084. * TLV headers and payloads must be properly initialized, with the
  1085. * right endianess (LE).
  1086. * @arg_size: number of pointers in the @args array
  1087. */
  1088. int i2400m_set_init_config(struct i2400m *i2400m,
  1089. const struct i2400m_tlv_hdr **arg, size_t args)
  1090. {
  1091. int result;
  1092. struct device *dev = i2400m_dev(i2400m);
  1093. struct sk_buff *ack_skb;
  1094. struct i2400m_l3l4_hdr *cmd;
  1095. char strerr[32];
  1096. unsigned argc, argsize, tlv_size;
  1097. const struct i2400m_tlv_hdr *tlv_hdr;
  1098. void *buf, *itr;
  1099. d_fnstart(3, dev, "(i2400m %p arg %p args %zu)\n", i2400m, arg, args);
  1100. result = 0;
  1101. if (args == 0)
  1102. goto none;
  1103. /* Compute the size of all the TLVs, so we can alloc a
  1104. * contiguous command block to copy them. */
  1105. argsize = 0;
  1106. for (argc = 0; argc < args; argc++) {
  1107. tlv_hdr = arg[argc];
  1108. argsize += sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
  1109. }
  1110. WARN_ON(argc >= 9); /* As per hw spec */
  1111. /* Alloc the space for the command and TLVs*/
  1112. result = -ENOMEM;
  1113. buf = kzalloc(sizeof(*cmd) + argsize, GFP_KERNEL);
  1114. if (buf == NULL)
  1115. goto error_alloc;
  1116. cmd = buf;
  1117. cmd->type = cpu_to_le16(I2400M_MT_SET_INIT_CONFIG);
  1118. cmd->length = cpu_to_le16(argsize);
  1119. cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
  1120. /* Copy the TLVs */
  1121. itr = buf + sizeof(*cmd);
  1122. for (argc = 0; argc < args; argc++) {
  1123. tlv_hdr = arg[argc];
  1124. tlv_size = sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
  1125. memcpy(itr, tlv_hdr, tlv_size);
  1126. itr += tlv_size;
  1127. }
  1128. /* Send the message! */
  1129. ack_skb = i2400m_msg_to_dev(i2400m, buf, sizeof(*cmd) + argsize);
  1130. result = PTR_ERR(ack_skb);
  1131. if (IS_ERR(ack_skb)) {
  1132. dev_err(dev, "Failed to issue 'init config' command: %d\n",
  1133. result);
  1134. goto error_msg_to_dev;
  1135. }
  1136. result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
  1137. strerr, sizeof(strerr));
  1138. if (result < 0)
  1139. dev_err(dev, "'init config' (0x%04x) command failed: %d - %s\n",
  1140. I2400M_MT_SET_INIT_CONFIG, result, strerr);
  1141. kfree_skb(ack_skb);
  1142. error_msg_to_dev:
  1143. kfree(buf);
  1144. error_alloc:
  1145. none:
  1146. d_fnend(3, dev, "(i2400m %p arg %p args %zu) = %d\n",
  1147. i2400m, arg, args, result);
  1148. return result;
  1149. }
  1150. EXPORT_SYMBOL_GPL(i2400m_set_init_config);
  1151. /**
  1152. * i2400m_set_idle_timeout - Set the device's idle mode timeout
  1153. *
  1154. * @i2400m: i2400m device descriptor
  1155. *
  1156. * @msecs: milliseconds for the timeout to enter idle mode. Between
  1157. * 100 to 300000 (5m); 0 to disable. In increments of 100.
  1158. *
  1159. * After this @msecs of the link being idle (no data being sent or
  1160. * received), the device will negotiate with the basestation entering
  1161. * idle mode for saving power. The connection is maintained, but
  1162. * getting out of it (done in tx.c) will require some negotiation,
  1163. * possible crypto re-handshake and a possible DHCP re-lease.
  1164. *
  1165. * Only available if fw_version >= 0x00090002.
  1166. *
  1167. * Returns: 0 if ok, < 0 errno code on error.
  1168. */
  1169. int i2400m_set_idle_timeout(struct i2400m *i2400m, unsigned msecs)
  1170. {
  1171. int result;
  1172. struct device *dev = i2400m_dev(i2400m);
  1173. struct sk_buff *ack_skb;
  1174. struct {
  1175. struct i2400m_l3l4_hdr hdr;
  1176. struct i2400m_tlv_config_idle_timeout cit;
  1177. } *cmd;
  1178. const struct i2400m_l3l4_hdr *ack;
  1179. size_t ack_len;
  1180. char strerr[32];
  1181. result = -ENOSYS;
  1182. if (i2400m_le_v1_3(i2400m))
  1183. goto error_alloc;
  1184. result = -ENOMEM;
  1185. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1186. if (cmd == NULL)
  1187. goto error_alloc;
  1188. cmd->hdr.type = cpu_to_le16(I2400M_MT_GET_STATE);
  1189. cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
  1190. cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
  1191. cmd->cit.hdr.type =
  1192. cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
  1193. cmd->cit.hdr.length = cpu_to_le16(sizeof(cmd->cit.timeout));
  1194. cmd->cit.timeout = cpu_to_le32(msecs);
  1195. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  1196. if (IS_ERR(ack_skb)) {
  1197. dev_err(dev, "Failed to issue 'set idle timeout' command: "
  1198. "%ld\n", PTR_ERR(ack_skb));
  1199. result = PTR_ERR(ack_skb);
  1200. goto error_msg_to_dev;
  1201. }
  1202. ack = wimax_msg_data_len(ack_skb, &ack_len);
  1203. result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
  1204. if (result < 0) {
  1205. dev_err(dev, "'set idle timeout' (0x%04x) command failed: "
  1206. "%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
  1207. goto error_cmd_failed;
  1208. }
  1209. result = 0;
  1210. kfree_skb(ack_skb);
  1211. error_cmd_failed:
  1212. error_msg_to_dev:
  1213. kfree(cmd);
  1214. error_alloc:
  1215. return result;
  1216. }
  1217. /**
  1218. * i2400m_dev_initialize - Initialize the device once communications are ready
  1219. *
  1220. * @i2400m: device descriptor
  1221. *
  1222. * Returns: 0 if ok, < 0 errno code on error.
  1223. *
  1224. * Configures the device to work the way we like it.
  1225. *
  1226. * At the point of this call, the device is registered with the WiMAX
  1227. * and netdev stacks, firmware is uploaded and we can talk to the
  1228. * device normally.
  1229. */
  1230. int i2400m_dev_initialize(struct i2400m *i2400m)
  1231. {
  1232. int result;
  1233. struct device *dev = i2400m_dev(i2400m);
  1234. struct i2400m_tlv_config_idle_parameters idle_params;
  1235. struct i2400m_tlv_config_idle_timeout idle_timeout;
  1236. struct i2400m_tlv_config_d2h_data_format df;
  1237. struct i2400m_tlv_config_dl_host_reorder dlhr;
  1238. const struct i2400m_tlv_hdr *args[9];
  1239. unsigned argc = 0;
  1240. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  1241. if (i2400m_passive_mode)
  1242. goto out_passive;
  1243. /* Disable idle mode? (enabled by default) */
  1244. if (i2400m_idle_mode_disabled) {
  1245. if (i2400m_le_v1_3(i2400m)) {
  1246. idle_params.hdr.type =
  1247. cpu_to_le16(I2400M_TLV_CONFIG_IDLE_PARAMETERS);
  1248. idle_params.hdr.length = cpu_to_le16(
  1249. sizeof(idle_params) - sizeof(idle_params.hdr));
  1250. idle_params.idle_timeout = 0;
  1251. idle_params.idle_paging_interval = 0;
  1252. args[argc++] = &idle_params.hdr;
  1253. } else {
  1254. idle_timeout.hdr.type =
  1255. cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
  1256. idle_timeout.hdr.length = cpu_to_le16(
  1257. sizeof(idle_timeout) - sizeof(idle_timeout.hdr));
  1258. idle_timeout.timeout = 0;
  1259. args[argc++] = &idle_timeout.hdr;
  1260. }
  1261. }
  1262. if (i2400m_ge_v1_4(i2400m)) {
  1263. /* Enable extended RX data format? */
  1264. df.hdr.type =
  1265. cpu_to_le16(I2400M_TLV_CONFIG_D2H_DATA_FORMAT);
  1266. df.hdr.length = cpu_to_le16(
  1267. sizeof(df) - sizeof(df.hdr));
  1268. df.format = 1;
  1269. args[argc++] = &df.hdr;
  1270. /* Enable RX data reordering?
  1271. * (switch flipped in rx.c:i2400m_rx_setup() after fw upload) */
  1272. if (i2400m->rx_reorder) {
  1273. dlhr.hdr.type =
  1274. cpu_to_le16(I2400M_TLV_CONFIG_DL_HOST_REORDER);
  1275. dlhr.hdr.length = cpu_to_le16(
  1276. sizeof(dlhr) - sizeof(dlhr.hdr));
  1277. dlhr.reorder = 1;
  1278. args[argc++] = &dlhr.hdr;
  1279. }
  1280. }
  1281. result = i2400m_set_init_config(i2400m, args, argc);
  1282. if (result < 0)
  1283. goto error;
  1284. out_passive:
  1285. /*
  1286. * Update state: Here it just calls a get state; parsing the
  1287. * result (System State TLV and RF Status TLV [done in the rx
  1288. * path hooks]) will set the hardware and software RF-Kill
  1289. * status.
  1290. */
  1291. result = i2400m_cmd_get_state(i2400m);
  1292. error:
  1293. if (result < 0)
  1294. dev_err(dev, "failed to initialize the device: %d\n", result);
  1295. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  1296. return result;
  1297. }
  1298. /**
  1299. * i2400m_dev_shutdown - Shutdown a running device
  1300. *
  1301. * @i2400m: device descriptor
  1302. *
  1303. * Release resources acquired during the running of the device; in
  1304. * theory, should also tell the device to go to sleep, switch off the
  1305. * radio, all that, but at this point, in most cases (driver
  1306. * disconnection, reset handling) we can't even talk to the device.
  1307. */
  1308. void i2400m_dev_shutdown(struct i2400m *i2400m)
  1309. {
  1310. struct device *dev = i2400m_dev(i2400m);
  1311. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  1312. d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
  1313. return;
  1314. }