wusbhc.c 13 KB

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