wusbhc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Wireless USB Host Controller
  3. * sysfs glue, wusbcore module support and life cycle management
  4. *
  5. *
  6. * Copyright (C) 2005-2006 Intel Corporation
  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. * Creation/destruction of wusbhc is split in two parts; that that
  25. * doesn't require the HCD to be added (wusbhc_{create,destroy}) and
  26. * the one that requires (phase B, wusbhc_b_{create,destroy}).
  27. *
  28. * This is so because usb_add_hcd() will start the HC, and thus, all
  29. * the HC specific stuff has to be already initialized (like sysfs
  30. * thingies).
  31. */
  32. #include <linux/device.h>
  33. #include <linux/module.h>
  34. #include "wusbhc.h"
  35. /**
  36. * Extract the wusbhc that corresponds to a USB Host Controller class device
  37. *
  38. * WARNING! Apply only if @dev is that of a
  39. * wusbhc.usb_hcd.self->class_dev; otherwise, you loose.
  40. */
  41. static struct wusbhc *usbhc_dev_to_wusbhc(struct device *dev)
  42. {
  43. struct usb_bus *usb_bus = dev_get_drvdata(dev);
  44. struct usb_hcd *usb_hcd = bus_to_hcd(usb_bus);
  45. return usb_hcd_to_wusbhc(usb_hcd);
  46. }
  47. /*
  48. * Show & store the current WUSB trust timeout
  49. *
  50. * We don't do locking--it is an 'atomic' value.
  51. *
  52. * The units that we store/show are always MILLISECONDS. However, the
  53. * value of trust_timeout is jiffies.
  54. */
  55. static ssize_t wusb_trust_timeout_show(struct device *dev,
  56. struct device_attribute *attr,
  57. char *buf)
  58. {
  59. struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
  60. return scnprintf(buf, PAGE_SIZE, "%u\n", wusbhc->trust_timeout);
  61. }
  62. static ssize_t wusb_trust_timeout_store(struct device *dev,
  63. struct device_attribute *attr,
  64. const char *buf, size_t size)
  65. {
  66. struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
  67. ssize_t result = -ENOSYS;
  68. unsigned trust_timeout;
  69. result = sscanf(buf, "%u", &trust_timeout);
  70. if (result != 1) {
  71. result = -EINVAL;
  72. goto out;
  73. }
  74. wusbhc->trust_timeout = min_t(unsigned, trust_timeout, 500);
  75. cancel_delayed_work(&wusbhc->keep_alive_timer);
  76. flush_workqueue(wusbd);
  77. queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
  78. msecs_to_jiffies(wusbhc->trust_timeout / 2));
  79. out:
  80. return result < 0 ? result : size;
  81. }
  82. static DEVICE_ATTR(wusb_trust_timeout, 0644, wusb_trust_timeout_show,
  83. wusb_trust_timeout_store);
  84. /*
  85. * Show the current WUSB CHID.
  86. */
  87. static ssize_t wusb_chid_show(struct device *dev,
  88. struct device_attribute *attr, char *buf)
  89. {
  90. struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
  91. const struct wusb_ckhdid *chid;
  92. ssize_t result = 0;
  93. if (wusbhc->wuie_host_info != NULL)
  94. chid = &wusbhc->wuie_host_info->CHID;
  95. else
  96. chid = &wusb_ckhdid_zero;
  97. result += ckhdid_printf(buf, PAGE_SIZE, chid);
  98. result += sprintf(buf + result, "\n");
  99. return result;
  100. }
  101. /*
  102. * Store a new CHID.
  103. *
  104. * - Write an all zeros CHID and it will stop the controller
  105. * - Write a non-zero CHID and it will start it.
  106. *
  107. * See wusbhc_chid_set() for more info.
  108. */
  109. static ssize_t wusb_chid_store(struct device *dev,
  110. struct device_attribute *attr,
  111. const char *buf, size_t size)
  112. {
  113. struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
  114. struct wusb_ckhdid chid;
  115. ssize_t result;
  116. result = sscanf(buf,
  117. "%02hhx %02hhx %02hhx %02hhx "
  118. "%02hhx %02hhx %02hhx %02hhx "
  119. "%02hhx %02hhx %02hhx %02hhx "
  120. "%02hhx %02hhx %02hhx %02hhx\n",
  121. &chid.data[0] , &chid.data[1] ,
  122. &chid.data[2] , &chid.data[3] ,
  123. &chid.data[4] , &chid.data[5] ,
  124. &chid.data[6] , &chid.data[7] ,
  125. &chid.data[8] , &chid.data[9] ,
  126. &chid.data[10], &chid.data[11],
  127. &chid.data[12], &chid.data[13],
  128. &chid.data[14], &chid.data[15]);
  129. if (result != 16) {
  130. dev_err(dev, "Unrecognized CHID (need 16 8-bit hex digits): "
  131. "%d\n", (int)result);
  132. return -EINVAL;
  133. }
  134. result = wusbhc_chid_set(wusbhc, &chid);
  135. return result < 0 ? result : size;
  136. }
  137. static DEVICE_ATTR(wusb_chid, 0644, wusb_chid_show, wusb_chid_store);
  138. static ssize_t wusb_phy_rate_show(struct device *dev,
  139. struct device_attribute *attr,
  140. char *buf)
  141. {
  142. struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
  143. return sprintf(buf, "%d\n", wusbhc->phy_rate);
  144. }
  145. static ssize_t wusb_phy_rate_store(struct device *dev,
  146. struct device_attribute *attr,
  147. const char *buf, size_t size)
  148. {
  149. struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
  150. uint8_t phy_rate;
  151. ssize_t result;
  152. result = sscanf(buf, "%hhu", &phy_rate);
  153. if (result != 1)
  154. return -EINVAL;
  155. if (phy_rate >= UWB_PHY_RATE_INVALID)
  156. return -EINVAL;
  157. wusbhc->phy_rate = phy_rate;
  158. return size;
  159. }
  160. static DEVICE_ATTR(wusb_phy_rate, 0644, wusb_phy_rate_show,
  161. wusb_phy_rate_store);
  162. static ssize_t wusb_dnts_show(struct device *dev,
  163. struct device_attribute *attr,
  164. char *buf)
  165. {
  166. struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
  167. return sprintf(buf, "num slots: %d\ninterval: %dms\n",
  168. wusbhc->dnts_num_slots, wusbhc->dnts_interval);
  169. }
  170. static ssize_t wusb_dnts_store(struct device *dev,
  171. struct device_attribute *attr,
  172. const char *buf, size_t size)
  173. {
  174. struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
  175. uint8_t num_slots, interval;
  176. ssize_t result;
  177. result = sscanf(buf, "%hhu %hhu", &num_slots, &interval);
  178. if (result != 2)
  179. return -EINVAL;
  180. wusbhc->dnts_num_slots = num_slots;
  181. wusbhc->dnts_interval = interval;
  182. return size;
  183. }
  184. static DEVICE_ATTR(wusb_dnts, 0644, wusb_dnts_show, wusb_dnts_store);
  185. static ssize_t wusb_retry_count_show(struct device *dev,
  186. struct device_attribute *attr,
  187. char *buf)
  188. {
  189. struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
  190. return sprintf(buf, "%d\n", wusbhc->retry_count);
  191. }
  192. static ssize_t wusb_retry_count_store(struct device *dev,
  193. struct device_attribute *attr,
  194. const char *buf, size_t size)
  195. {
  196. struct wusbhc *wusbhc = usbhc_dev_to_wusbhc(dev);
  197. uint8_t retry_count;
  198. ssize_t result;
  199. result = sscanf(buf, "%hhu", &retry_count);
  200. if (result != 1)
  201. return -EINVAL;
  202. wusbhc->retry_count = max_t(uint8_t, retry_count,
  203. WUSB_RETRY_COUNT_MAX);
  204. return size;
  205. }
  206. static DEVICE_ATTR(wusb_retry_count, 0644, wusb_retry_count_show,
  207. wusb_retry_count_store);
  208. /* Group all the WUSBHC attributes */
  209. static struct attribute *wusbhc_attrs[] = {
  210. &dev_attr_wusb_trust_timeout.attr,
  211. &dev_attr_wusb_chid.attr,
  212. &dev_attr_wusb_phy_rate.attr,
  213. &dev_attr_wusb_dnts.attr,
  214. &dev_attr_wusb_retry_count.attr,
  215. NULL,
  216. };
  217. static struct attribute_group wusbhc_attr_group = {
  218. .name = NULL, /* we want them in the same directory */
  219. .attrs = wusbhc_attrs,
  220. };
  221. /*
  222. * Create a wusbhc instance
  223. *
  224. * NOTEs:
  225. *
  226. * - assumes *wusbhc has been zeroed and wusbhc->usb_hcd has been
  227. * initialized but not added.
  228. *
  229. * - fill out ports_max, mmcies_max and mmcie_{add,rm} before calling.
  230. *
  231. * - fill out wusbhc->uwb_rc and refcount it before calling
  232. * - fill out the wusbhc->sec_modes array
  233. */
  234. int wusbhc_create(struct wusbhc *wusbhc)
  235. {
  236. int result = 0;
  237. /* set defaults. These can be overwritten using sysfs attributes. */
  238. wusbhc->trust_timeout = WUSB_TRUST_TIMEOUT_MS;
  239. wusbhc->phy_rate = UWB_PHY_RATE_INVALID - 1;
  240. wusbhc->dnts_num_slots = 4;
  241. wusbhc->dnts_interval = 2;
  242. wusbhc->retry_count = WUSB_RETRY_COUNT_INFINITE;
  243. mutex_init(&wusbhc->mutex);
  244. result = wusbhc_mmcie_create(wusbhc);
  245. if (result < 0)
  246. goto error_mmcie_create;
  247. result = wusbhc_devconnect_create(wusbhc);
  248. if (result < 0)
  249. goto error_devconnect_create;
  250. result = wusbhc_rh_create(wusbhc);
  251. if (result < 0)
  252. goto error_rh_create;
  253. result = wusbhc_sec_create(wusbhc);
  254. if (result < 0)
  255. goto error_sec_create;
  256. return 0;
  257. error_sec_create:
  258. wusbhc_rh_destroy(wusbhc);
  259. error_rh_create:
  260. wusbhc_devconnect_destroy(wusbhc);
  261. error_devconnect_create:
  262. wusbhc_mmcie_destroy(wusbhc);
  263. error_mmcie_create:
  264. return result;
  265. }
  266. EXPORT_SYMBOL_GPL(wusbhc_create);
  267. static inline struct kobject *wusbhc_kobj(struct wusbhc *wusbhc)
  268. {
  269. return &wusbhc->usb_hcd.self.controller->kobj;
  270. }
  271. /*
  272. * Phase B of a wusbhc instance creation
  273. *
  274. * Creates fields that depend on wusbhc->usb_hcd having been
  275. * added. This is where we create the sysfs files in
  276. * /sys/class/usb_host/usb_hostX/.
  277. *
  278. * NOTE: Assumes wusbhc->usb_hcd has been already added by the upper
  279. * layer (hwahc or whci)
  280. */
  281. int wusbhc_b_create(struct wusbhc *wusbhc)
  282. {
  283. int result = 0;
  284. struct device *dev = wusbhc->usb_hcd.self.controller;
  285. result = sysfs_create_group(wusbhc_kobj(wusbhc), &wusbhc_attr_group);
  286. if (result < 0) {
  287. dev_err(dev, "Cannot register WUSBHC attributes: %d\n",
  288. result);
  289. goto error_create_attr_group;
  290. }
  291. return 0;
  292. error_create_attr_group:
  293. return result;
  294. }
  295. EXPORT_SYMBOL_GPL(wusbhc_b_create);
  296. void wusbhc_b_destroy(struct wusbhc *wusbhc)
  297. {
  298. wusbhc_pal_unregister(wusbhc);
  299. sysfs_remove_group(wusbhc_kobj(wusbhc), &wusbhc_attr_group);
  300. }
  301. EXPORT_SYMBOL_GPL(wusbhc_b_destroy);
  302. void wusbhc_destroy(struct wusbhc *wusbhc)
  303. {
  304. wusbhc_sec_destroy(wusbhc);
  305. wusbhc_rh_destroy(wusbhc);
  306. wusbhc_devconnect_destroy(wusbhc);
  307. wusbhc_mmcie_destroy(wusbhc);
  308. }
  309. EXPORT_SYMBOL_GPL(wusbhc_destroy);
  310. struct workqueue_struct *wusbd;
  311. EXPORT_SYMBOL_GPL(wusbd);
  312. /*
  313. * WUSB Cluster ID allocation map
  314. *
  315. * Each WUSB bus in a channel is identified with a Cluster Id in the
  316. * unauth address pace (WUSB1.0[4.3]). We take the range 0xe0 to 0xff
  317. * (that's space for 31 WUSB controllers, as 0xff can't be taken). We
  318. * start taking from 0xff, 0xfe, 0xfd... (hence the += or -= 0xff).
  319. *
  320. * For each one we taken, we pin it in the bitap
  321. */
  322. #define CLUSTER_IDS 32
  323. static DECLARE_BITMAP(wusb_cluster_id_table, CLUSTER_IDS);
  324. static DEFINE_SPINLOCK(wusb_cluster_ids_lock);
  325. /*
  326. * Get a WUSB Cluster ID
  327. *
  328. * Need to release with wusb_cluster_id_put() when done w/ it.
  329. */
  330. /* FIXME: coordinate with the choose_addres() from the USB stack */
  331. /* we want to leave the top of the 128 range for cluster addresses and
  332. * the bottom for device addresses (as we map them one on one with
  333. * ports). */
  334. u8 wusb_cluster_id_get(void)
  335. {
  336. u8 id;
  337. spin_lock(&wusb_cluster_ids_lock);
  338. id = find_first_zero_bit(wusb_cluster_id_table, CLUSTER_IDS);
  339. if (id >= CLUSTER_IDS) {
  340. id = 0;
  341. goto out;
  342. }
  343. set_bit(id, wusb_cluster_id_table);
  344. id = (u8) 0xff - id;
  345. out:
  346. spin_unlock(&wusb_cluster_ids_lock);
  347. return id;
  348. }
  349. EXPORT_SYMBOL_GPL(wusb_cluster_id_get);
  350. /*
  351. * Release a WUSB Cluster ID
  352. *
  353. * Obtained it with wusb_cluster_id_get()
  354. */
  355. void wusb_cluster_id_put(u8 id)
  356. {
  357. id = 0xff - id;
  358. BUG_ON(id >= CLUSTER_IDS);
  359. spin_lock(&wusb_cluster_ids_lock);
  360. WARN_ON(!test_bit(id, wusb_cluster_id_table));
  361. clear_bit(id, wusb_cluster_id_table);
  362. spin_unlock(&wusb_cluster_ids_lock);
  363. }
  364. EXPORT_SYMBOL_GPL(wusb_cluster_id_put);
  365. /**
  366. * wusbhc_giveback_urb - return an URB to the USB core
  367. * @wusbhc: the host controller the URB is from.
  368. * @urb: the URB.
  369. * @status: the URB's status.
  370. *
  371. * Return an URB to the USB core doing some additional WUSB specific
  372. * processing.
  373. *
  374. * - After a successful transfer, update the trust timeout timestamp
  375. * for the WUSB device.
  376. *
  377. * - [WUSB] sections 4.13 and 7.5.1 specify the stop retransmission
  378. * condition for the WCONNECTACK_IE is that the host has observed
  379. * the associated device responding to a control transfer.
  380. */
  381. void wusbhc_giveback_urb(struct wusbhc *wusbhc, struct urb *urb, int status)
  382. {
  383. struct wusb_dev *wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc,
  384. urb->dev);
  385. if (status == 0 && wusb_dev) {
  386. wusb_dev->entry_ts = jiffies;
  387. /* wusbhc_devconnect_acked() can't be called from
  388. atomic context so defer it to a work queue. */
  389. if (!list_empty(&wusb_dev->cack_node))
  390. queue_work(wusbd, &wusb_dev->devconnect_acked_work);
  391. else
  392. wusb_dev_put(wusb_dev);
  393. }
  394. usb_hcd_giveback_urb(&wusbhc->usb_hcd, urb, status);
  395. }
  396. EXPORT_SYMBOL_GPL(wusbhc_giveback_urb);
  397. /**
  398. * wusbhc_reset_all - reset the HC hardware
  399. * @wusbhc: the host controller to reset.
  400. *
  401. * Request a full hardware reset of the chip. This will also reset
  402. * the radio controller and any other PALs.
  403. */
  404. void wusbhc_reset_all(struct wusbhc *wusbhc)
  405. {
  406. if (wusbhc->uwb_rc)
  407. uwb_rc_reset_all(wusbhc->uwb_rc);
  408. }
  409. EXPORT_SYMBOL_GPL(wusbhc_reset_all);
  410. static struct notifier_block wusb_usb_notifier = {
  411. .notifier_call = wusb_usb_ncb,
  412. .priority = INT_MAX /* Need to be called first of all */
  413. };
  414. static int __init wusbcore_init(void)
  415. {
  416. int result;
  417. result = wusb_crypto_init();
  418. if (result < 0)
  419. goto error_crypto_init;
  420. /* WQ is singlethread because we need to serialize notifications */
  421. wusbd = create_singlethread_workqueue("wusbd");
  422. if (wusbd == NULL) {
  423. result = -ENOMEM;
  424. printk(KERN_ERR "WUSB-core: Cannot create wusbd workqueue\n");
  425. goto error_wusbd_create;
  426. }
  427. usb_register_notify(&wusb_usb_notifier);
  428. bitmap_zero(wusb_cluster_id_table, CLUSTER_IDS);
  429. set_bit(0, wusb_cluster_id_table); /* reserve Cluster ID 0xff */
  430. return 0;
  431. error_wusbd_create:
  432. wusb_crypto_exit();
  433. error_crypto_init:
  434. return result;
  435. }
  436. module_init(wusbcore_init);
  437. static void __exit wusbcore_exit(void)
  438. {
  439. clear_bit(0, wusb_cluster_id_table);
  440. if (!bitmap_empty(wusb_cluster_id_table, CLUSTER_IDS)) {
  441. printk(KERN_ERR "BUG: WUSB Cluster IDs not released on exit: %*pb\n",
  442. CLUSTER_IDS, wusb_cluster_id_table);
  443. WARN_ON(1);
  444. }
  445. usb_unregister_notify(&wusb_usb_notifier);
  446. destroy_workqueue(wusbd);
  447. wusb_crypto_exit();
  448. }
  449. module_exit(wusbcore_exit);
  450. MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
  451. MODULE_DESCRIPTION("Wireless USB core");
  452. MODULE_LICENSE("GPL");