control.c 38 KB

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