driver.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * Intel Wireless WiMAX Connection 2400m
  3. * Generic probe/disconnect, reset and message passing
  4. *
  5. *
  6. * Copyright (C) 2007-2008 Intel Corporation <linux-wimax@intel.com>
  7. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version
  11. * 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. *
  23. *
  24. * See i2400m.h for driver documentation. This contains helpers for
  25. * the driver model glue [_setup()/_release()], handling device resets
  26. * [_dev_reset_handle()], and the backends for the WiMAX stack ops
  27. * reset [_op_reset()] and message from user [_op_msg_from_user()].
  28. *
  29. * ROADMAP:
  30. *
  31. * i2400m_op_msg_from_user()
  32. * i2400m_msg_to_dev()
  33. * wimax_msg_to_user_send()
  34. *
  35. * i2400m_op_reset()
  36. * i240m->bus_reset()
  37. *
  38. * i2400m_dev_reset_handle()
  39. * __i2400m_dev_reset_handle()
  40. * __i2400m_dev_stop()
  41. * __i2400m_dev_start()
  42. *
  43. * i2400m_setup()
  44. * i2400m_bootrom_init()
  45. * register_netdev()
  46. * i2400m_dev_start()
  47. * __i2400m_dev_start()
  48. * i2400m_dev_bootstrap()
  49. * i2400m_tx_setup()
  50. * i2400m->bus_dev_start()
  51. * i2400m_check_mac_addr()
  52. * wimax_dev_add()
  53. *
  54. * i2400m_release()
  55. * wimax_dev_rm()
  56. * i2400m_dev_stop()
  57. * __i2400m_dev_stop()
  58. * i2400m_dev_shutdown()
  59. * i2400m->bus_dev_stop()
  60. * i2400m_tx_release()
  61. * unregister_netdev()
  62. */
  63. #include "i2400m.h"
  64. #include <linux/wimax/i2400m.h>
  65. #include <linux/module.h>
  66. #include <linux/moduleparam.h>
  67. #define D_SUBMODULE driver
  68. #include "debug-levels.h"
  69. int i2400m_idle_mode_disabled; /* 0 (idle mode enabled) by default */
  70. module_param_named(idle_mode_disabled, i2400m_idle_mode_disabled, int, 0644);
  71. MODULE_PARM_DESC(idle_mode_disabled,
  72. "If true, the device will not enable idle mode negotiation "
  73. "with the base station (when connected) to save power.");
  74. /**
  75. * i2400m_queue_work - schedule work on a i2400m's queue
  76. *
  77. * @i2400m: device descriptor
  78. *
  79. * @fn: function to run to execute work. It gets passed a 'struct
  80. * work_struct' that is wrapped in a 'struct i2400m_work'. Once
  81. * done, you have to (1) i2400m_put(i2400m_work->i2400m) and then
  82. * (2) kfree(i2400m_work).
  83. *
  84. * @gfp_flags: GFP flags for memory allocation.
  85. *
  86. * @pl: pointer to a payload buffer that you want to pass to the _work
  87. * function. Use this to pack (for example) a struct with extra
  88. * arguments.
  89. *
  90. * @pl_size: size of the payload buffer.
  91. *
  92. * We do this quite often, so this just saves typing; allocate a
  93. * wrapper for a i2400m, get a ref to it, pack arguments and launch
  94. * the work.
  95. *
  96. * A usual workflow is:
  97. *
  98. * struct my_work_args {
  99. * void *something;
  100. * int whatever;
  101. * };
  102. * ...
  103. *
  104. * struct my_work_args my_args = {
  105. * .something = FOO,
  106. * .whaetever = BLAH
  107. * };
  108. * i2400m_queue_work(i2400m, 1, my_work_function, GFP_KERNEL,
  109. * &args, sizeof(args))
  110. *
  111. * And now the work function can unpack the arguments and call the
  112. * real function (or do the job itself):
  113. *
  114. * static
  115. * void my_work_fn((struct work_struct *ws)
  116. * {
  117. * struct i2400m_work *iw =
  118. * container_of(ws, struct i2400m_work, ws);
  119. * struct my_work_args *my_args = (void *) iw->pl;
  120. *
  121. * my_work(iw->i2400m, my_args->something, my_args->whatevert);
  122. * }
  123. */
  124. int i2400m_queue_work(struct i2400m *i2400m,
  125. void (*fn)(struct work_struct *), gfp_t gfp_flags,
  126. const void *pl, size_t pl_size)
  127. {
  128. int result;
  129. struct i2400m_work *iw;
  130. BUG_ON(i2400m->work_queue == NULL);
  131. result = -ENOMEM;
  132. iw = kzalloc(sizeof(*iw) + pl_size, gfp_flags);
  133. if (iw == NULL)
  134. goto error_kzalloc;
  135. iw->i2400m = i2400m_get(i2400m);
  136. memcpy(iw->pl, pl, pl_size);
  137. INIT_WORK(&iw->ws, fn);
  138. result = queue_work(i2400m->work_queue, &iw->ws);
  139. error_kzalloc:
  140. return result;
  141. }
  142. EXPORT_SYMBOL_GPL(i2400m_queue_work);
  143. /*
  144. * Schedule i2400m's specific work on the system's queue.
  145. *
  146. * Used for a few cases where we really need it; otherwise, identical
  147. * to i2400m_queue_work().
  148. *
  149. * Returns < 0 errno code on error, 1 if ok.
  150. *
  151. * If it returns zero, something really bad happened, as it means the
  152. * works struct was already queued, but we have just allocated it, so
  153. * it should not happen.
  154. */
  155. int i2400m_schedule_work(struct i2400m *i2400m,
  156. void (*fn)(struct work_struct *), gfp_t gfp_flags)
  157. {
  158. int result;
  159. struct i2400m_work *iw;
  160. BUG_ON(i2400m->work_queue == NULL);
  161. result = -ENOMEM;
  162. iw = kzalloc(sizeof(*iw), gfp_flags);
  163. if (iw == NULL)
  164. goto error_kzalloc;
  165. iw->i2400m = i2400m_get(i2400m);
  166. INIT_WORK(&iw->ws, fn);
  167. result = schedule_work(&iw->ws);
  168. if (result == 0)
  169. result = -ENXIO;
  170. error_kzalloc:
  171. return result;
  172. }
  173. /*
  174. * WiMAX stack operation: relay a message from user space
  175. *
  176. * @wimax_dev: device descriptor
  177. * @pipe_name: named pipe the message is for
  178. * @msg_buf: pointer to the message bytes
  179. * @msg_len: length of the buffer
  180. * @genl_info: passed by the generic netlink layer
  181. *
  182. * The WiMAX stack will call this function when a message was received
  183. * from user space.
  184. *
  185. * For the i2400m, this is an L3L4 message, as specified in
  186. * include/linux/wimax/i2400m.h, and thus prefixed with a 'struct
  187. * i2400m_l3l4_hdr'. Driver (and device) expect the messages to be
  188. * coded in Little Endian.
  189. *
  190. * This function just verifies that the header declaration and the
  191. * payload are consistent and then deals with it, either forwarding it
  192. * to the device or procesing it locally.
  193. *
  194. * In the i2400m, messages are basically commands that will carry an
  195. * ack, so we use i2400m_msg_to_dev() and then deliver the ack back to
  196. * user space. The rx.c code might intercept the response and use it
  197. * to update the driver's state, but then it will pass it on so it can
  198. * be relayed back to user space.
  199. *
  200. * Note that asynchronous events from the device are processed and
  201. * sent to user space in rx.c.
  202. */
  203. static
  204. int i2400m_op_msg_from_user(struct wimax_dev *wimax_dev,
  205. const char *pipe_name,
  206. const void *msg_buf, size_t msg_len,
  207. const struct genl_info *genl_info)
  208. {
  209. int result;
  210. struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
  211. struct device *dev = i2400m_dev(i2400m);
  212. struct sk_buff *ack_skb;
  213. d_fnstart(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p "
  214. "msg_len %zu genl_info %p)\n", wimax_dev, i2400m,
  215. msg_buf, msg_len, genl_info);
  216. ack_skb = i2400m_msg_to_dev(i2400m, msg_buf, msg_len);
  217. result = PTR_ERR(ack_skb);
  218. if (IS_ERR(ack_skb))
  219. goto error_msg_to_dev;
  220. if (unlikely(i2400m->trace_msg_from_user))
  221. wimax_msg(&i2400m->wimax_dev, "trace",
  222. msg_buf, msg_len, GFP_KERNEL);
  223. result = wimax_msg_send(&i2400m->wimax_dev, ack_skb);
  224. error_msg_to_dev:
  225. d_fnend(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p msg_len %zu "
  226. "genl_info %p) = %d\n", wimax_dev, i2400m, msg_buf, msg_len,
  227. genl_info, result);
  228. return result;
  229. }
  230. /*
  231. * Context to wait for a reset to finalize
  232. */
  233. struct i2400m_reset_ctx {
  234. struct completion completion;
  235. int result;
  236. };
  237. /*
  238. * WiMAX stack operation: reset a device
  239. *
  240. * @wimax_dev: device descriptor
  241. *
  242. * See the documentation for wimax_reset() and wimax_dev->op_reset for
  243. * the requirements of this function. The WiMAX stack guarantees
  244. * serialization on calls to this function.
  245. *
  246. * Do a warm reset on the device; if it fails, resort to a cold reset
  247. * and return -ENODEV. On successful warm reset, we need to block
  248. * until it is complete.
  249. *
  250. * The bus-driver implementation of reset takes care of falling back
  251. * to cold reset if warm fails.
  252. */
  253. static
  254. int i2400m_op_reset(struct wimax_dev *wimax_dev)
  255. {
  256. int result;
  257. struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
  258. struct device *dev = i2400m_dev(i2400m);
  259. struct i2400m_reset_ctx ctx = {
  260. .completion = COMPLETION_INITIALIZER_ONSTACK(ctx.completion),
  261. .result = 0,
  262. };
  263. d_fnstart(4, dev, "(wimax_dev %p)\n", wimax_dev);
  264. mutex_lock(&i2400m->init_mutex);
  265. i2400m->reset_ctx = &ctx;
  266. mutex_unlock(&i2400m->init_mutex);
  267. result = i2400m->bus_reset(i2400m, I2400M_RT_WARM);
  268. if (result < 0)
  269. goto out;
  270. result = wait_for_completion_timeout(&ctx.completion, 4*HZ);
  271. if (result == 0)
  272. result = -ETIMEDOUT;
  273. else if (result > 0)
  274. result = ctx.result;
  275. /* if result < 0, pass it on */
  276. mutex_lock(&i2400m->init_mutex);
  277. i2400m->reset_ctx = NULL;
  278. mutex_unlock(&i2400m->init_mutex);
  279. out:
  280. d_fnend(4, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
  281. return result;
  282. }
  283. /*
  284. * Check the MAC address we got from boot mode is ok
  285. *
  286. * @i2400m: device descriptor
  287. *
  288. * Returns: 0 if ok, < 0 errno code on error.
  289. */
  290. static
  291. int i2400m_check_mac_addr(struct i2400m *i2400m)
  292. {
  293. int result;
  294. struct device *dev = i2400m_dev(i2400m);
  295. struct sk_buff *skb;
  296. const struct i2400m_tlv_detailed_device_info *ddi;
  297. struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  298. const unsigned char zeromac[ETH_ALEN] = { 0 };
  299. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  300. skb = i2400m_get_device_info(i2400m);
  301. if (IS_ERR(skb)) {
  302. result = PTR_ERR(skb);
  303. dev_err(dev, "Cannot verify MAC address, error reading: %d\n",
  304. result);
  305. goto error;
  306. }
  307. /* Extract MAC addresss */
  308. ddi = (void *) skb->data;
  309. BUILD_BUG_ON(ETH_ALEN != sizeof(ddi->mac_address));
  310. d_printf(2, dev, "GET DEVICE INFO: mac addr "
  311. "%02x:%02x:%02x:%02x:%02x:%02x\n",
  312. ddi->mac_address[0], ddi->mac_address[1],
  313. ddi->mac_address[2], ddi->mac_address[3],
  314. ddi->mac_address[4], ddi->mac_address[5]);
  315. if (!memcmp(net_dev->perm_addr, ddi->mac_address,
  316. sizeof(ddi->mac_address)))
  317. goto ok;
  318. dev_warn(dev, "warning: device reports a different MAC address "
  319. "to that of boot mode's\n");
  320. dev_warn(dev, "device reports %02x:%02x:%02x:%02x:%02x:%02x\n",
  321. ddi->mac_address[0], ddi->mac_address[1],
  322. ddi->mac_address[2], ddi->mac_address[3],
  323. ddi->mac_address[4], ddi->mac_address[5]);
  324. dev_warn(dev, "boot mode reported %02x:%02x:%02x:%02x:%02x:%02x\n",
  325. net_dev->perm_addr[0], net_dev->perm_addr[1],
  326. net_dev->perm_addr[2], net_dev->perm_addr[3],
  327. net_dev->perm_addr[4], net_dev->perm_addr[5]);
  328. if (!memcmp(zeromac, ddi->mac_address, sizeof(zeromac)))
  329. dev_err(dev, "device reports an invalid MAC address, "
  330. "not updating\n");
  331. else {
  332. dev_warn(dev, "updating MAC address\n");
  333. net_dev->addr_len = ETH_ALEN;
  334. memcpy(net_dev->perm_addr, ddi->mac_address, ETH_ALEN);
  335. memcpy(net_dev->dev_addr, ddi->mac_address, ETH_ALEN);
  336. }
  337. ok:
  338. result = 0;
  339. kfree_skb(skb);
  340. error:
  341. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  342. return result;
  343. }
  344. /**
  345. * __i2400m_dev_start - Bring up driver communication with the device
  346. *
  347. * @i2400m: device descriptor
  348. * @flags: boot mode flags
  349. *
  350. * Returns: 0 if ok, < 0 errno code on error.
  351. *
  352. * Uploads firmware and brings up all the resources needed to be able
  353. * to communicate with the device.
  354. *
  355. * TX needs to be setup before the bus-specific code (otherwise on
  356. * shutdown, the bus-tx code could try to access it).
  357. */
  358. static
  359. int __i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri flags)
  360. {
  361. int result;
  362. struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  363. struct net_device *net_dev = wimax_dev->net_dev;
  364. struct device *dev = i2400m_dev(i2400m);
  365. int times = 3;
  366. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  367. retry:
  368. result = i2400m_dev_bootstrap(i2400m, flags);
  369. if (result < 0) {
  370. dev_err(dev, "cannot bootstrap device: %d\n", result);
  371. goto error_bootstrap;
  372. }
  373. result = i2400m_tx_setup(i2400m);
  374. if (result < 0)
  375. goto error_tx_setup;
  376. result = i2400m->bus_dev_start(i2400m);
  377. if (result < 0)
  378. goto error_bus_dev_start;
  379. i2400m->work_queue = create_singlethread_workqueue(wimax_dev->name);
  380. if (i2400m->work_queue == NULL) {
  381. result = -ENOMEM;
  382. dev_err(dev, "cannot create workqueue\n");
  383. goto error_create_workqueue;
  384. }
  385. /* At this point is ok to send commands to the device */
  386. result = i2400m_check_mac_addr(i2400m);
  387. if (result < 0)
  388. goto error_check_mac_addr;
  389. i2400m->ready = 1;
  390. wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
  391. result = i2400m_dev_initialize(i2400m);
  392. if (result < 0)
  393. goto error_dev_initialize;
  394. /* At this point, reports will come for the device and set it
  395. * to the right state if it is different than UNINITIALIZED */
  396. d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
  397. net_dev, i2400m, result);
  398. return result;
  399. error_dev_initialize:
  400. error_check_mac_addr:
  401. destroy_workqueue(i2400m->work_queue);
  402. error_create_workqueue:
  403. i2400m->bus_dev_stop(i2400m);
  404. error_bus_dev_start:
  405. i2400m_tx_release(i2400m);
  406. error_tx_setup:
  407. error_bootstrap:
  408. if (result == -ERESTARTSYS && times-- > 0) {
  409. flags = I2400M_BRI_SOFT;
  410. goto retry;
  411. }
  412. d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
  413. net_dev, i2400m, result);
  414. return result;
  415. }
  416. static
  417. int i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri bm_flags)
  418. {
  419. int result;
  420. mutex_lock(&i2400m->init_mutex); /* Well, start the device */
  421. result = __i2400m_dev_start(i2400m, bm_flags);
  422. if (result >= 0)
  423. i2400m->updown = 1;
  424. mutex_unlock(&i2400m->init_mutex);
  425. return result;
  426. }
  427. /**
  428. * i2400m_dev_stop - Tear down driver communication with the device
  429. *
  430. * @i2400m: device descriptor
  431. *
  432. * Returns: 0 if ok, < 0 errno code on error.
  433. *
  434. * Releases all the resources allocated to communicate with the device.
  435. */
  436. static
  437. void __i2400m_dev_stop(struct i2400m *i2400m)
  438. {
  439. struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  440. struct device *dev = i2400m_dev(i2400m);
  441. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  442. wimax_state_change(wimax_dev, __WIMAX_ST_QUIESCING);
  443. i2400m_dev_shutdown(i2400m);
  444. i2400m->ready = 0;
  445. destroy_workqueue(i2400m->work_queue);
  446. i2400m->bus_dev_stop(i2400m);
  447. i2400m_tx_release(i2400m);
  448. wimax_state_change(wimax_dev, WIMAX_ST_DOWN);
  449. d_fnend(3, dev, "(i2400m %p) = 0\n", i2400m);
  450. }
  451. /*
  452. * Watch out -- we only need to stop if there is a need for it. The
  453. * device could have reset itself and failed to come up again (see
  454. * _i2400m_dev_reset_handle()).
  455. */
  456. static
  457. void i2400m_dev_stop(struct i2400m *i2400m)
  458. {
  459. mutex_lock(&i2400m->init_mutex);
  460. if (i2400m->updown) {
  461. __i2400m_dev_stop(i2400m);
  462. i2400m->updown = 0;
  463. }
  464. mutex_unlock(&i2400m->init_mutex);
  465. }
  466. /*
  467. * The device has rebooted; fix up the device and the driver
  468. *
  469. * Tear down the driver communication with the device, reload the
  470. * firmware and reinitialize the communication with the device.
  471. *
  472. * If someone calls a reset when the device's firmware is down, in
  473. * theory we won't see it because we are not listening. However, just
  474. * in case, leave the code to handle it.
  475. *
  476. * If there is a reset context, use it; this means someone is waiting
  477. * for us to tell him when the reset operation is complete and the
  478. * device is ready to rock again.
  479. *
  480. * NOTE: if we are in the process of bringing up or down the
  481. * communication with the device [running i2400m_dev_start() or
  482. * _stop()], don't do anything, let it fail and handle it.
  483. *
  484. * This function is ran always in a thread context
  485. */
  486. static
  487. void __i2400m_dev_reset_handle(struct work_struct *ws)
  488. {
  489. int result;
  490. struct i2400m_work *iw = container_of(ws, struct i2400m_work, ws);
  491. struct i2400m *i2400m = iw->i2400m;
  492. struct device *dev = i2400m_dev(i2400m);
  493. enum wimax_st wimax_state;
  494. struct i2400m_reset_ctx *ctx = i2400m->reset_ctx;
  495. d_fnstart(3, dev, "(ws %p i2400m %p)\n", ws, i2400m);
  496. result = 0;
  497. if (mutex_trylock(&i2400m->init_mutex) == 0) {
  498. /* We are still in i2400m_dev_start() [let it fail] or
  499. * i2400m_dev_stop() [we are shutting down anyway, so
  500. * ignore it] or we are resetting somewhere else. */
  501. dev_err(dev, "device rebooted\n");
  502. i2400m_msg_to_dev_cancel_wait(i2400m, -ERESTARTSYS);
  503. complete(&i2400m->msg_completion);
  504. goto out;
  505. }
  506. wimax_state = wimax_state_get(&i2400m->wimax_dev);
  507. if (wimax_state < WIMAX_ST_UNINITIALIZED) {
  508. dev_info(dev, "device rebooted: it is down, ignoring\n");
  509. goto out_unlock; /* ifconfig up/down wasn't called */
  510. }
  511. dev_err(dev, "device rebooted: reinitializing driver\n");
  512. __i2400m_dev_stop(i2400m);
  513. i2400m->updown = 0;
  514. result = __i2400m_dev_start(i2400m,
  515. I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
  516. if (result < 0) {
  517. dev_err(dev, "device reboot: cannot start the device: %d\n",
  518. result);
  519. result = i2400m->bus_reset(i2400m, I2400M_RT_BUS);
  520. if (result >= 0)
  521. result = -ENODEV;
  522. } else
  523. i2400m->updown = 1;
  524. out_unlock:
  525. if (i2400m->reset_ctx) {
  526. ctx->result = result;
  527. complete(&ctx->completion);
  528. }
  529. mutex_unlock(&i2400m->init_mutex);
  530. out:
  531. i2400m_put(i2400m);
  532. kfree(iw);
  533. d_fnend(3, dev, "(ws %p i2400m %p) = void\n", ws, i2400m);
  534. return;
  535. }
  536. /**
  537. * i2400m_dev_reset_handle - Handle a device's reset in a thread context
  538. *
  539. * Schedule a device reset handling out on a thread context, so it
  540. * is safe to call from atomic context. We can't use the i2400m's
  541. * queue as we are going to destroy it and reinitialize it as part of
  542. * the driver bringup/bringup process.
  543. *
  544. * See __i2400m_dev_reset_handle() for details; that takes care of
  545. * reinitializing the driver to handle the reset, calling into the
  546. * bus-specific functions ops as needed.
  547. */
  548. int i2400m_dev_reset_handle(struct i2400m *i2400m)
  549. {
  550. return i2400m_schedule_work(i2400m, __i2400m_dev_reset_handle,
  551. GFP_ATOMIC);
  552. }
  553. EXPORT_SYMBOL_GPL(i2400m_dev_reset_handle);
  554. /**
  555. * i2400m_setup - bus-generic setup function for the i2400m device
  556. *
  557. * @i2400m: device descriptor (bus-specific parts have been initialized)
  558. *
  559. * Returns: 0 if ok, < 0 errno code on error.
  560. *
  561. * Initializes the bus-generic parts of the i2400m driver; the
  562. * bus-specific parts have been initialized, function pointers filled
  563. * out by the bus-specific probe function.
  564. *
  565. * As well, this registers the WiMAX and net device nodes. Once this
  566. * function returns, the device is operative and has to be ready to
  567. * receive and send network traffic and WiMAX control operations.
  568. */
  569. int i2400m_setup(struct i2400m *i2400m, enum i2400m_bri bm_flags)
  570. {
  571. int result = -ENODEV;
  572. struct device *dev = i2400m_dev(i2400m);
  573. struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  574. struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  575. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  576. snprintf(wimax_dev->name, sizeof(wimax_dev->name),
  577. "i2400m-%s:%s", dev->bus->name, dev->bus_id);
  578. i2400m->bm_cmd_buf = kzalloc(I2400M_BM_CMD_BUF_SIZE, GFP_KERNEL);
  579. if (i2400m->bm_cmd_buf == NULL) {
  580. dev_err(dev, "cannot allocate USB command buffer\n");
  581. goto error_bm_cmd_kzalloc;
  582. }
  583. i2400m->bm_ack_buf = kzalloc(I2400M_BM_ACK_BUF_SIZE, GFP_KERNEL);
  584. if (i2400m->bm_ack_buf == NULL) {
  585. dev_err(dev, "cannot allocate USB ack buffer\n");
  586. goto error_bm_ack_buf_kzalloc;
  587. }
  588. result = i2400m_bootrom_init(i2400m, bm_flags);
  589. if (result < 0) {
  590. dev_err(dev, "read mac addr: bootrom init "
  591. "failed: %d\n", result);
  592. goto error_bootrom_init;
  593. }
  594. result = i2400m_read_mac_addr(i2400m);
  595. if (result < 0)
  596. goto error_read_mac_addr;
  597. result = register_netdev(net_dev); /* Okey dokey, bring it up */
  598. if (result < 0) {
  599. dev_err(dev, "cannot register i2400m network device: %d\n",
  600. result);
  601. goto error_register_netdev;
  602. }
  603. netif_carrier_off(net_dev);
  604. result = i2400m_dev_start(i2400m, bm_flags);
  605. if (result < 0)
  606. goto error_dev_start;
  607. i2400m->wimax_dev.op_msg_from_user = i2400m_op_msg_from_user;
  608. i2400m->wimax_dev.op_rfkill_sw_toggle = i2400m_op_rfkill_sw_toggle;
  609. i2400m->wimax_dev.op_reset = i2400m_op_reset;
  610. result = wimax_dev_add(&i2400m->wimax_dev, net_dev);
  611. if (result < 0)
  612. goto error_wimax_dev_add;
  613. /* User space needs to do some init stuff */
  614. wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
  615. /* Now setup all that requires a registered net and wimax device. */
  616. result = i2400m_debugfs_add(i2400m);
  617. if (result < 0) {
  618. dev_err(dev, "cannot setup i2400m's debugfs: %d\n", result);
  619. goto error_debugfs_setup;
  620. }
  621. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  622. return result;
  623. error_debugfs_setup:
  624. wimax_dev_rm(&i2400m->wimax_dev);
  625. error_wimax_dev_add:
  626. i2400m_dev_stop(i2400m);
  627. error_dev_start:
  628. unregister_netdev(net_dev);
  629. error_register_netdev:
  630. error_read_mac_addr:
  631. error_bootrom_init:
  632. kfree(i2400m->bm_ack_buf);
  633. error_bm_ack_buf_kzalloc:
  634. kfree(i2400m->bm_cmd_buf);
  635. error_bm_cmd_kzalloc:
  636. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  637. return result;
  638. }
  639. EXPORT_SYMBOL_GPL(i2400m_setup);
  640. /**
  641. * i2400m_release - release the bus-generic driver resources
  642. *
  643. * Sends a disconnect message and undoes any setup done by i2400m_setup()
  644. */
  645. void i2400m_release(struct i2400m *i2400m)
  646. {
  647. struct device *dev = i2400m_dev(i2400m);
  648. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  649. netif_stop_queue(i2400m->wimax_dev.net_dev);
  650. i2400m_debugfs_rm(i2400m);
  651. wimax_dev_rm(&i2400m->wimax_dev);
  652. i2400m_dev_stop(i2400m);
  653. unregister_netdev(i2400m->wimax_dev.net_dev);
  654. kfree(i2400m->bm_ack_buf);
  655. kfree(i2400m->bm_cmd_buf);
  656. d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
  657. }
  658. EXPORT_SYMBOL_GPL(i2400m_release);
  659. /*
  660. * Debug levels control; see debug.h
  661. */
  662. struct d_level D_LEVEL[] = {
  663. D_SUBMODULE_DEFINE(control),
  664. D_SUBMODULE_DEFINE(driver),
  665. D_SUBMODULE_DEFINE(debugfs),
  666. D_SUBMODULE_DEFINE(fw),
  667. D_SUBMODULE_DEFINE(netdev),
  668. D_SUBMODULE_DEFINE(rfkill),
  669. D_SUBMODULE_DEFINE(rx),
  670. D_SUBMODULE_DEFINE(tx),
  671. };
  672. size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
  673. static
  674. int __init i2400m_driver_init(void)
  675. {
  676. return 0;
  677. }
  678. module_init(i2400m_driver_init);
  679. static
  680. void __exit i2400m_driver_exit(void)
  681. {
  682. /* for scheds i2400m_dev_reset_handle() */
  683. flush_scheduled_work();
  684. return;
  685. }
  686. module_exit(i2400m_driver_exit);
  687. MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
  688. MODULE_DESCRIPTION("Intel 2400M WiMAX networking bus-generic driver");
  689. MODULE_LICENSE("GPL");