control.c 42 KB

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