usb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * Intel Wireless WiMAX Connection 2400m
  3. * Linux driver model glue for USB device, reset & fw upload
  4. *
  5. *
  6. * Copyright (C) 2007-2008 Intel Corporation <linux-wimax@intel.com>
  7. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  8. * Yanir Lubetkin <yanirx.lubetkin@intel.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License version
  12. * 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  22. * 02110-1301, USA.
  23. *
  24. *
  25. * See i2400m-usb.h for a general description of this driver.
  26. *
  27. * This file implements driver model glue, and hook ups for the
  28. * generic driver to implement the bus-specific functions (device
  29. * communication setup/tear down, firmware upload and resetting).
  30. *
  31. * ROADMAP
  32. *
  33. * i2400mu_probe()
  34. * alloc_netdev()...
  35. * i2400mu_netdev_setup()
  36. * i2400mu_init()
  37. * i2400m_netdev_setup()
  38. * i2400m_setup()...
  39. *
  40. * i2400mu_disconnect
  41. * i2400m_release()
  42. * free_netdev()
  43. *
  44. * i2400mu_suspend()
  45. * i2400m_cmd_enter_powersave()
  46. * i2400mu_notification_release()
  47. *
  48. * i2400mu_resume()
  49. * i2400mu_notification_setup()
  50. *
  51. * i2400mu_bus_dev_start() Called by i2400m_dev_start() [who is
  52. * i2400mu_tx_setup() called by i2400m_setup()]
  53. * i2400mu_rx_setup()
  54. * i2400mu_notification_setup()
  55. *
  56. * i2400mu_bus_dev_stop() Called by i2400m_dev_stop() [who is
  57. * i2400mu_notification_release() called by i2400m_release()]
  58. * i2400mu_rx_release()
  59. * i2400mu_tx_release()
  60. *
  61. * i2400mu_bus_reset() Called by i2400m_reset
  62. * __i2400mu_reset()
  63. * __i2400mu_send_barker()
  64. * usb_reset_device()
  65. */
  66. #include "i2400m-usb.h"
  67. #include <linux/wimax/i2400m.h>
  68. #include <linux/debugfs.h>
  69. #include <linux/slab.h>
  70. #define D_SUBMODULE usb
  71. #include "usb-debug-levels.h"
  72. static char i2400mu_debug_params[128];
  73. module_param_string(debug, i2400mu_debug_params, sizeof(i2400mu_debug_params),
  74. 0644);
  75. MODULE_PARM_DESC(debug,
  76. "String of space-separated NAME:VALUE pairs, where NAMEs "
  77. "are the different debug submodules and VALUE are the "
  78. "initial debug value to set.");
  79. /* Our firmware file name */
  80. static const char *i2400mu_bus_fw_names_5x50[] = {
  81. #define I2400MU_FW_FILE_NAME_v1_4 "i2400m-fw-usb-1.4.sbcf"
  82. I2400MU_FW_FILE_NAME_v1_4,
  83. NULL,
  84. };
  85. static const char *i2400mu_bus_fw_names_6050[] = {
  86. #define I6050U_FW_FILE_NAME_v1_5 "i6050-fw-usb-1.5.sbcf"
  87. I6050U_FW_FILE_NAME_v1_5,
  88. NULL,
  89. };
  90. static
  91. int i2400mu_bus_dev_start(struct i2400m *i2400m)
  92. {
  93. int result;
  94. struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
  95. struct device *dev = &i2400mu->usb_iface->dev;
  96. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  97. result = i2400mu_tx_setup(i2400mu);
  98. if (result < 0)
  99. goto error_usb_tx_setup;
  100. result = i2400mu_rx_setup(i2400mu);
  101. if (result < 0)
  102. goto error_usb_rx_setup;
  103. result = i2400mu_notification_setup(i2400mu);
  104. if (result < 0)
  105. goto error_notif_setup;
  106. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  107. return result;
  108. error_notif_setup:
  109. i2400mu_rx_release(i2400mu);
  110. error_usb_rx_setup:
  111. i2400mu_tx_release(i2400mu);
  112. error_usb_tx_setup:
  113. d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
  114. return result;
  115. }
  116. static
  117. void i2400mu_bus_dev_stop(struct i2400m *i2400m)
  118. {
  119. struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
  120. struct device *dev = &i2400mu->usb_iface->dev;
  121. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  122. i2400mu_notification_release(i2400mu);
  123. i2400mu_rx_release(i2400mu);
  124. i2400mu_tx_release(i2400mu);
  125. d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
  126. }
  127. /*
  128. * Sends a barker buffer to the device
  129. *
  130. * This helper will allocate a kmalloced buffer and use it to transmit
  131. * (then free it). Reason for this is that other arches cannot use
  132. * stack/vmalloc/text areas for DMA transfers.
  133. *
  134. * Error recovery here is simpler: anything is considered a hard error
  135. * and will move the reset code to use a last-resort bus-based reset.
  136. */
  137. static
  138. int __i2400mu_send_barker(struct i2400mu *i2400mu,
  139. const __le32 *barker,
  140. size_t barker_size,
  141. unsigned endpoint)
  142. {
  143. struct usb_endpoint_descriptor *epd = NULL;
  144. int pipe, actual_len, ret;
  145. struct device *dev = &i2400mu->usb_iface->dev;
  146. void *buffer;
  147. int do_autopm = 1;
  148. ret = usb_autopm_get_interface(i2400mu->usb_iface);
  149. if (ret < 0) {
  150. dev_err(dev, "RESET: can't get autopm: %d\n", ret);
  151. do_autopm = 0;
  152. }
  153. ret = -ENOMEM;
  154. buffer = kmalloc(barker_size, GFP_KERNEL);
  155. if (buffer == NULL)
  156. goto error_kzalloc;
  157. epd = usb_get_epd(i2400mu->usb_iface, endpoint);
  158. pipe = usb_sndbulkpipe(i2400mu->usb_dev, epd->bEndpointAddress);
  159. memcpy(buffer, barker, barker_size);
  160. retry:
  161. ret = usb_bulk_msg(i2400mu->usb_dev, pipe, buffer, barker_size,
  162. &actual_len, 200);
  163. switch (ret) {
  164. case 0:
  165. if (actual_len != barker_size) { /* Too short? drop it */
  166. dev_err(dev, "E: %s: short write (%d B vs %zu "
  167. "expected)\n",
  168. __func__, actual_len, barker_size);
  169. ret = -EIO;
  170. }
  171. break;
  172. case -EPIPE:
  173. /*
  174. * Stall -- maybe the device is choking with our
  175. * requests. Clear it and give it some time. If they
  176. * happen to often, it might be another symptom, so we
  177. * reset.
  178. *
  179. * No error handling for usb_clear_halt(0; if it
  180. * works, the retry works; if it fails, this switch
  181. * does the error handling for us.
  182. */
  183. if (edc_inc(&i2400mu->urb_edc,
  184. 10 * EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
  185. dev_err(dev, "E: %s: too many stalls in "
  186. "URB; resetting device\n", __func__);
  187. usb_queue_reset_device(i2400mu->usb_iface);
  188. /* fallthrough */
  189. } else {
  190. usb_clear_halt(i2400mu->usb_dev, pipe);
  191. msleep(10); /* give the device some time */
  192. goto retry;
  193. }
  194. case -EINVAL: /* while removing driver */
  195. case -ENODEV: /* dev disconnect ... */
  196. case -ENOENT: /* just ignore it */
  197. case -ESHUTDOWN: /* and exit */
  198. case -ECONNRESET:
  199. ret = -ESHUTDOWN;
  200. break;
  201. default: /* Some error? */
  202. if (edc_inc(&i2400mu->urb_edc,
  203. EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME)) {
  204. dev_err(dev, "E: %s: maximum errors in URB "
  205. "exceeded; resetting device\n",
  206. __func__);
  207. usb_queue_reset_device(i2400mu->usb_iface);
  208. } else {
  209. dev_warn(dev, "W: %s: cannot send URB: %d\n",
  210. __func__, ret);
  211. goto retry;
  212. }
  213. }
  214. kfree(buffer);
  215. error_kzalloc:
  216. if (do_autopm)
  217. usb_autopm_put_interface(i2400mu->usb_iface);
  218. return ret;
  219. }
  220. /*
  221. * Reset a device at different levels (warm, cold or bus)
  222. *
  223. * @i2400m: device descriptor
  224. * @reset_type: soft, warm or bus reset (I2400M_RT_WARM/SOFT/BUS)
  225. *
  226. * Warm and cold resets get a USB reset if they fail.
  227. *
  228. * Warm reset:
  229. *
  230. * The device will be fully reset internally, but won't be
  231. * disconnected from the USB bus (so no reenumeration will
  232. * happen). Firmware upload will be necessary.
  233. *
  234. * The device will send a reboot barker in the notification endpoint
  235. * that will trigger the driver to reinitialize the state
  236. * automatically from notif.c:i2400m_notification_grok() into
  237. * i2400m_dev_bootstrap_delayed().
  238. *
  239. * Cold and bus (USB) reset:
  240. *
  241. * The device will be fully reset internally, disconnected from the
  242. * USB bus an a reenumeration will happen. Firmware upload will be
  243. * necessary. Thus, we don't do any locking or struct
  244. * reinitialization, as we are going to be fully disconnected and
  245. * reenumerated.
  246. *
  247. * Note we need to return -ENODEV if a warm reset was requested and we
  248. * had to resort to a bus reset. See i2400m_op_reset(), wimax_reset()
  249. * and wimax_dev->op_reset.
  250. *
  251. * WARNING: no driver state saved/fixed
  252. */
  253. static
  254. int i2400mu_bus_reset(struct i2400m *i2400m, enum i2400m_reset_type rt)
  255. {
  256. int result;
  257. struct i2400mu *i2400mu =
  258. container_of(i2400m, struct i2400mu, i2400m);
  259. struct device *dev = i2400m_dev(i2400m);
  260. static const __le32 i2400m_WARM_BOOT_BARKER[4] = {
  261. cpu_to_le32(I2400M_WARM_RESET_BARKER),
  262. cpu_to_le32(I2400M_WARM_RESET_BARKER),
  263. cpu_to_le32(I2400M_WARM_RESET_BARKER),
  264. cpu_to_le32(I2400M_WARM_RESET_BARKER),
  265. };
  266. static const __le32 i2400m_COLD_BOOT_BARKER[4] = {
  267. cpu_to_le32(I2400M_COLD_RESET_BARKER),
  268. cpu_to_le32(I2400M_COLD_RESET_BARKER),
  269. cpu_to_le32(I2400M_COLD_RESET_BARKER),
  270. cpu_to_le32(I2400M_COLD_RESET_BARKER),
  271. };
  272. d_fnstart(3, dev, "(i2400m %p rt %u)\n", i2400m, rt);
  273. if (rt == I2400M_RT_WARM)
  274. result = __i2400mu_send_barker(
  275. i2400mu, i2400m_WARM_BOOT_BARKER,
  276. sizeof(i2400m_WARM_BOOT_BARKER),
  277. i2400mu->endpoint_cfg.bulk_out);
  278. else if (rt == I2400M_RT_COLD)
  279. result = __i2400mu_send_barker(
  280. i2400mu, i2400m_COLD_BOOT_BARKER,
  281. sizeof(i2400m_COLD_BOOT_BARKER),
  282. i2400mu->endpoint_cfg.reset_cold);
  283. else if (rt == I2400M_RT_BUS) {
  284. result = usb_reset_device(i2400mu->usb_dev);
  285. switch (result) {
  286. case 0:
  287. case -EINVAL: /* device is gone */
  288. case -ENODEV:
  289. case -ENOENT:
  290. case -ESHUTDOWN:
  291. result = 0;
  292. break; /* We assume the device is disconnected */
  293. default:
  294. dev_err(dev, "USB reset failed (%d), giving up!\n",
  295. result);
  296. }
  297. } else {
  298. result = -EINVAL; /* shut gcc up in certain arches */
  299. BUG();
  300. }
  301. if (result < 0
  302. && result != -EINVAL /* device is gone */
  303. && rt != I2400M_RT_BUS) {
  304. /*
  305. * Things failed -- resort to lower level reset, that
  306. * we queue in another context; the reason for this is
  307. * that the pre and post reset functionality requires
  308. * the i2400m->init_mutex; RT_WARM and RT_COLD can
  309. * come from areas where i2400m->init_mutex is taken.
  310. */
  311. dev_err(dev, "%s reset failed (%d); trying USB reset\n",
  312. rt == I2400M_RT_WARM ? "warm" : "cold", result);
  313. usb_queue_reset_device(i2400mu->usb_iface);
  314. result = -ENODEV;
  315. }
  316. d_fnend(3, dev, "(i2400m %p rt %u) = %d\n", i2400m, rt, result);
  317. return result;
  318. }
  319. static
  320. void i2400mu_netdev_setup(struct net_device *net_dev)
  321. {
  322. struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
  323. struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
  324. i2400mu_init(i2400mu);
  325. i2400m_netdev_setup(net_dev);
  326. }
  327. /*
  328. * Debug levels control; see debug.h
  329. */
  330. struct d_level D_LEVEL[] = {
  331. D_SUBMODULE_DEFINE(usb),
  332. D_SUBMODULE_DEFINE(fw),
  333. D_SUBMODULE_DEFINE(notif),
  334. D_SUBMODULE_DEFINE(rx),
  335. D_SUBMODULE_DEFINE(tx),
  336. };
  337. size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
  338. #define __debugfs_register(prefix, name, parent) \
  339. do { \
  340. result = d_level_register_debugfs(prefix, name, parent); \
  341. if (result < 0) \
  342. goto error; \
  343. } while (0)
  344. static
  345. int i2400mu_debugfs_add(struct i2400mu *i2400mu)
  346. {
  347. int result;
  348. struct device *dev = &i2400mu->usb_iface->dev;
  349. struct dentry *dentry = i2400mu->i2400m.wimax_dev.debugfs_dentry;
  350. struct dentry *fd;
  351. dentry = debugfs_create_dir("i2400m-usb", dentry);
  352. result = PTR_ERR(dentry);
  353. if (IS_ERR(dentry)) {
  354. if (result == -ENODEV)
  355. result = 0; /* No debugfs support */
  356. goto error;
  357. }
  358. i2400mu->debugfs_dentry = dentry;
  359. __debugfs_register("dl_", usb, dentry);
  360. __debugfs_register("dl_", fw, dentry);
  361. __debugfs_register("dl_", notif, dentry);
  362. __debugfs_register("dl_", rx, dentry);
  363. __debugfs_register("dl_", tx, dentry);
  364. /* Don't touch these if you don't know what you are doing */
  365. fd = debugfs_create_u8("rx_size_auto_shrink", 0600, dentry,
  366. &i2400mu->rx_size_auto_shrink);
  367. result = PTR_ERR(fd);
  368. if (IS_ERR(fd) && result != -ENODEV) {
  369. dev_err(dev, "Can't create debugfs entry "
  370. "rx_size_auto_shrink: %d\n", result);
  371. goto error;
  372. }
  373. fd = debugfs_create_size_t("rx_size", 0600, dentry,
  374. &i2400mu->rx_size);
  375. result = PTR_ERR(fd);
  376. if (IS_ERR(fd) && result != -ENODEV) {
  377. dev_err(dev, "Can't create debugfs entry "
  378. "rx_size: %d\n", result);
  379. goto error;
  380. }
  381. return 0;
  382. error:
  383. debugfs_remove_recursive(i2400mu->debugfs_dentry);
  384. return result;
  385. }
  386. static struct device_type i2400mu_type = {
  387. .name = "wimax",
  388. };
  389. /*
  390. * Probe a i2400m interface and register it
  391. *
  392. * @iface: USB interface to link to
  393. * @id: USB class/subclass/protocol id
  394. * @returns: 0 if ok, < 0 errno code on error.
  395. *
  396. * Alloc a net device, initialize the bus-specific details and then
  397. * calls the bus-generic initialization routine. That will register
  398. * the wimax and netdev devices, upload the firmware [using
  399. * _bus_bm_*()], call _bus_dev_start() to finalize the setup of the
  400. * communication with the device and then will start to talk to it to
  401. * finnish setting it up.
  402. */
  403. static
  404. int i2400mu_probe(struct usb_interface *iface,
  405. const struct usb_device_id *id)
  406. {
  407. int result;
  408. struct net_device *net_dev;
  409. struct device *dev = &iface->dev;
  410. struct i2400m *i2400m;
  411. struct i2400mu *i2400mu;
  412. struct usb_device *usb_dev = interface_to_usbdev(iface);
  413. if (usb_dev->speed != USB_SPEED_HIGH)
  414. dev_err(dev, "device not connected as high speed\n");
  415. /* Allocate instance [calls i2400m_netdev_setup() on it]. */
  416. result = -ENOMEM;
  417. net_dev = alloc_netdev(sizeof(*i2400mu), "wmx%d",
  418. i2400mu_netdev_setup);
  419. if (net_dev == NULL) {
  420. dev_err(dev, "no memory for network device instance\n");
  421. goto error_alloc_netdev;
  422. }
  423. SET_NETDEV_DEV(net_dev, dev);
  424. SET_NETDEV_DEVTYPE(net_dev, &i2400mu_type);
  425. i2400m = net_dev_to_i2400m(net_dev);
  426. i2400mu = container_of(i2400m, struct i2400mu, i2400m);
  427. i2400m->wimax_dev.net_dev = net_dev;
  428. i2400mu->usb_dev = usb_get_dev(usb_dev);
  429. i2400mu->usb_iface = iface;
  430. usb_set_intfdata(iface, i2400mu);
  431. i2400m->bus_tx_block_size = I2400MU_BLK_SIZE;
  432. i2400m->bus_pl_size_max = I2400MU_PL_SIZE_MAX;
  433. i2400m->bus_setup = NULL;
  434. i2400m->bus_dev_start = i2400mu_bus_dev_start;
  435. i2400m->bus_dev_stop = i2400mu_bus_dev_stop;
  436. i2400m->bus_release = NULL;
  437. i2400m->bus_tx_kick = i2400mu_bus_tx_kick;
  438. i2400m->bus_reset = i2400mu_bus_reset;
  439. i2400m->bus_bm_retries = I2400M_USB_BOOT_RETRIES;
  440. i2400m->bus_bm_cmd_send = i2400mu_bus_bm_cmd_send;
  441. i2400m->bus_bm_wait_for_ack = i2400mu_bus_bm_wait_for_ack;
  442. i2400m->bus_bm_mac_addr_impaired = 0;
  443. switch (id->idProduct) {
  444. case USB_DEVICE_ID_I6050:
  445. case USB_DEVICE_ID_I6050_2:
  446. i2400mu->i6050 = 1;
  447. break;
  448. default:
  449. break;
  450. }
  451. if (i2400mu->i6050) {
  452. i2400m->bus_fw_names = i2400mu_bus_fw_names_6050;
  453. i2400mu->endpoint_cfg.bulk_out = 0;
  454. i2400mu->endpoint_cfg.notification = 3;
  455. i2400mu->endpoint_cfg.reset_cold = 2;
  456. i2400mu->endpoint_cfg.bulk_in = 1;
  457. } else {
  458. i2400m->bus_fw_names = i2400mu_bus_fw_names_5x50;
  459. i2400mu->endpoint_cfg.bulk_out = 0;
  460. i2400mu->endpoint_cfg.notification = 1;
  461. i2400mu->endpoint_cfg.reset_cold = 2;
  462. i2400mu->endpoint_cfg.bulk_in = 3;
  463. }
  464. #ifdef CONFIG_PM
  465. iface->needs_remote_wakeup = 1; /* autosuspend (15s delay) */
  466. device_init_wakeup(dev, 1);
  467. usb_dev->autosuspend_delay = 15 * HZ;
  468. usb_dev->autosuspend_disabled = 0;
  469. #endif
  470. result = i2400m_setup(i2400m, I2400M_BRI_MAC_REINIT);
  471. if (result < 0) {
  472. dev_err(dev, "cannot setup device: %d\n", result);
  473. goto error_setup;
  474. }
  475. result = i2400mu_debugfs_add(i2400mu);
  476. if (result < 0) {
  477. dev_err(dev, "Can't register i2400mu's debugfs: %d\n", result);
  478. goto error_debugfs_add;
  479. }
  480. return 0;
  481. error_debugfs_add:
  482. i2400m_release(i2400m);
  483. error_setup:
  484. usb_set_intfdata(iface, NULL);
  485. usb_put_dev(i2400mu->usb_dev);
  486. free_netdev(net_dev);
  487. error_alloc_netdev:
  488. return result;
  489. }
  490. /*
  491. * Disconect a i2400m from the system.
  492. *
  493. * i2400m_stop() has been called before, so al the rx and tx contexts
  494. * have been taken down already. Make sure the queue is stopped,
  495. * unregister netdev and i2400m, free and kill.
  496. */
  497. static
  498. void i2400mu_disconnect(struct usb_interface *iface)
  499. {
  500. struct i2400mu *i2400mu = usb_get_intfdata(iface);
  501. struct i2400m *i2400m = &i2400mu->i2400m;
  502. struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  503. struct device *dev = &iface->dev;
  504. d_fnstart(3, dev, "(iface %p i2400m %p)\n", iface, i2400m);
  505. debugfs_remove_recursive(i2400mu->debugfs_dentry);
  506. i2400m_release(i2400m);
  507. usb_set_intfdata(iface, NULL);
  508. usb_put_dev(i2400mu->usb_dev);
  509. free_netdev(net_dev);
  510. d_fnend(3, dev, "(iface %p i2400m %p) = void\n", iface, i2400m);
  511. }
  512. /*
  513. * Get the device ready for USB port or system standby and hibernation
  514. *
  515. * USB port and system standby are handled the same.
  516. *
  517. * When the system hibernates, the USB device is powered down and then
  518. * up, so we don't really have to do much here, as it will be seen as
  519. * a reconnect. Still for simplicity we consider this case the same as
  520. * suspend, so that the device has a chance to do notify the base
  521. * station (if connected).
  522. *
  523. * So at the end, the three cases require common handling.
  524. *
  525. * If at the time of this call the device's firmware is not loaded,
  526. * nothing has to be done. Note we can be "loose" about not reading
  527. * i2400m->updown under i2400m->init_mutex. If it happens to change
  528. * inmediately, other parts of the call flow will fail and effectively
  529. * catch it.
  530. *
  531. * If the firmware is loaded, we need to:
  532. *
  533. * - tell the device to go into host interface power save mode, wait
  534. * for it to ack
  535. *
  536. * This is quite more interesting than it is; we need to execute a
  537. * command, but this time, we don't want the code in usb-{tx,rx}.c
  538. * to call the usb_autopm_get/put_interface() barriers as it'd
  539. * deadlock, so we need to decrement i2400mu->do_autopm, that acts
  540. * as a poor man's semaphore. Ugly, but it works.
  541. *
  542. * As well, the device might refuse going to sleep for whichever
  543. * reason. In this case we just fail. For system suspend/hibernate,
  544. * we *can't* fail. We check PM_EVENT_AUTO to see if the
  545. * suspend call comes from the USB stack or from the system and act
  546. * in consequence.
  547. *
  548. * - stop the notification endpoint polling
  549. */
  550. static
  551. int i2400mu_suspend(struct usb_interface *iface, pm_message_t pm_msg)
  552. {
  553. int result = 0;
  554. struct device *dev = &iface->dev;
  555. struct i2400mu *i2400mu = usb_get_intfdata(iface);
  556. unsigned is_autosuspend = 0;
  557. struct i2400m *i2400m = &i2400mu->i2400m;
  558. #ifdef CONFIG_PM
  559. if (pm_msg.event & PM_EVENT_AUTO)
  560. is_autosuspend = 1;
  561. #endif
  562. d_fnstart(3, dev, "(iface %p pm_msg %u)\n", iface, pm_msg.event);
  563. rmb(); /* see i2400m->updown's documentation */
  564. if (i2400m->updown == 0)
  565. goto no_firmware;
  566. if (i2400m->state == I2400M_SS_DATA_PATH_CONNECTED && is_autosuspend) {
  567. /* ugh -- the device is connected and this suspend
  568. * request is an autosuspend one (not a system standby
  569. * / hibernate).
  570. *
  571. * The only way the device can go to standby is if the
  572. * link with the base station is in IDLE mode; that
  573. * were the case, we'd be in status
  574. * I2400M_SS_CONNECTED_IDLE. But we are not.
  575. *
  576. * If we *tell* him to go power save now, it'll reset
  577. * as a precautionary measure, so if this is an
  578. * autosuspend thing, say no and it'll come back
  579. * later, when the link is IDLE
  580. */
  581. result = -EBADF;
  582. d_printf(1, dev, "fw up, link up, not-idle, autosuspend: "
  583. "not entering powersave\n");
  584. goto error_not_now;
  585. }
  586. d_printf(1, dev, "fw up: entering powersave\n");
  587. atomic_dec(&i2400mu->do_autopm);
  588. result = i2400m_cmd_enter_powersave(i2400m);
  589. atomic_inc(&i2400mu->do_autopm);
  590. if (result < 0 && !is_autosuspend) {
  591. /* System suspend, can't fail */
  592. dev_err(dev, "failed to suspend, will reset on resume\n");
  593. result = 0;
  594. }
  595. if (result < 0)
  596. goto error_enter_powersave;
  597. i2400mu_notification_release(i2400mu);
  598. d_printf(1, dev, "powersave requested\n");
  599. error_enter_powersave:
  600. error_not_now:
  601. no_firmware:
  602. d_fnend(3, dev, "(iface %p pm_msg %u) = %d\n",
  603. iface, pm_msg.event, result);
  604. return result;
  605. }
  606. static
  607. int i2400mu_resume(struct usb_interface *iface)
  608. {
  609. int ret = 0;
  610. struct device *dev = &iface->dev;
  611. struct i2400mu *i2400mu = usb_get_intfdata(iface);
  612. struct i2400m *i2400m = &i2400mu->i2400m;
  613. d_fnstart(3, dev, "(iface %p)\n", iface);
  614. rmb(); /* see i2400m->updown's documentation */
  615. if (i2400m->updown == 0) {
  616. d_printf(1, dev, "fw was down, no resume neeed\n");
  617. goto out;
  618. }
  619. d_printf(1, dev, "fw was up, resuming\n");
  620. i2400mu_notification_setup(i2400mu);
  621. /* USB has flow control, so we don't need to give it time to
  622. * come back; otherwise, we'd use something like a get-state
  623. * command... */
  624. out:
  625. d_fnend(3, dev, "(iface %p) = %d\n", iface, ret);
  626. return ret;
  627. }
  628. static
  629. int i2400mu_reset_resume(struct usb_interface *iface)
  630. {
  631. int result;
  632. struct device *dev = &iface->dev;
  633. struct i2400mu *i2400mu = usb_get_intfdata(iface);
  634. struct i2400m *i2400m = &i2400mu->i2400m;
  635. d_fnstart(3, dev, "(iface %p)\n", iface);
  636. result = i2400m_dev_reset_handle(i2400m, "device reset on resume");
  637. d_fnend(3, dev, "(iface %p) = %d\n", iface, result);
  638. return result < 0 ? result : 0;
  639. }
  640. /*
  641. * Another driver or user space is triggering a reset on the device
  642. * which contains the interface passed as an argument. Cease IO and
  643. * save any device state you need to restore.
  644. *
  645. * If you need to allocate memory here, use GFP_NOIO or GFP_ATOMIC, if
  646. * you are in atomic context.
  647. */
  648. static
  649. int i2400mu_pre_reset(struct usb_interface *iface)
  650. {
  651. struct i2400mu *i2400mu = usb_get_intfdata(iface);
  652. return i2400m_pre_reset(&i2400mu->i2400m);
  653. }
  654. /*
  655. * The reset has completed. Restore any saved device state and begin
  656. * using the device again.
  657. *
  658. * If you need to allocate memory here, use GFP_NOIO or GFP_ATOMIC, if
  659. * you are in atomic context.
  660. */
  661. static
  662. int i2400mu_post_reset(struct usb_interface *iface)
  663. {
  664. struct i2400mu *i2400mu = usb_get_intfdata(iface);
  665. return i2400m_post_reset(&i2400mu->i2400m);
  666. }
  667. static
  668. struct usb_device_id i2400mu_id_table[] = {
  669. { USB_DEVICE(0x8086, USB_DEVICE_ID_I6050) },
  670. { USB_DEVICE(0x8086, USB_DEVICE_ID_I6050_2) },
  671. { USB_DEVICE(0x8086, 0x0181) },
  672. { USB_DEVICE(0x8086, 0x1403) },
  673. { USB_DEVICE(0x8086, 0x1405) },
  674. { USB_DEVICE(0x8086, 0x0180) },
  675. { USB_DEVICE(0x8086, 0x0182) },
  676. { USB_DEVICE(0x8086, 0x1406) },
  677. { USB_DEVICE(0x8086, 0x1403) },
  678. { },
  679. };
  680. MODULE_DEVICE_TABLE(usb, i2400mu_id_table);
  681. static
  682. struct usb_driver i2400mu_driver = {
  683. .name = KBUILD_MODNAME,
  684. .suspend = i2400mu_suspend,
  685. .resume = i2400mu_resume,
  686. .reset_resume = i2400mu_reset_resume,
  687. .probe = i2400mu_probe,
  688. .disconnect = i2400mu_disconnect,
  689. .pre_reset = i2400mu_pre_reset,
  690. .post_reset = i2400mu_post_reset,
  691. .id_table = i2400mu_id_table,
  692. .supports_autosuspend = 1,
  693. };
  694. static
  695. int __init i2400mu_driver_init(void)
  696. {
  697. d_parse_params(D_LEVEL, D_LEVEL_SIZE, i2400mu_debug_params,
  698. "i2400m_usb.debug");
  699. return usb_register(&i2400mu_driver);
  700. }
  701. module_init(i2400mu_driver_init);
  702. static
  703. void __exit i2400mu_driver_exit(void)
  704. {
  705. flush_scheduled_work(); /* for the stuff we schedule from sysfs.c */
  706. usb_deregister(&i2400mu_driver);
  707. }
  708. module_exit(i2400mu_driver_exit);
  709. MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
  710. MODULE_DESCRIPTION("Driver for USB based Intel Wireless WiMAX Connection 2400M "
  711. "(5x50 & 6050)");
  712. MODULE_LICENSE("GPL");
  713. MODULE_FIRMWARE(I2400MU_FW_FILE_NAME_v1_4);