control.c 42 KB

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