control.c 43 KB

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