usb.c 24 KB

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