devconnect.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. /*
  2. * WUSB Wire Adapter: Control/Data Streaming Interface (WUSB[8])
  3. * Device Connect handling
  4. *
  5. * Copyright (C) 2006 Intel Corporation
  6. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA.
  21. *
  22. *
  23. * FIXME: docs
  24. * FIXME: this file needs to be broken up, it's grown too big
  25. *
  26. *
  27. * WUSB1.0[7.1, 7.5.1, ]
  28. *
  29. * WUSB device connection is kind of messy. Some background:
  30. *
  31. * When a device wants to connect it scans the UWB radio channels
  32. * looking for a WUSB Channel; a WUSB channel is defined by MMCs
  33. * (Micro Managed Commands or something like that) [see
  34. * Design-overview for more on this] .
  35. *
  36. * So, device scans the radio, finds MMCs and thus a host and checks
  37. * when the next DNTS is. It sends a Device Notification Connect
  38. * (DN_Connect); the host picks it up (through nep.c and notif.c, ends
  39. * up in wusb_devconnect_ack(), which creates a wusb_dev structure in
  40. * wusbhc->port[port_number].wusb_dev), assigns an unauth address
  41. * to the device (this means from 0x80 to 0xfe) and sends, in the MMC
  42. * a Connect Ack Information Element (ConnAck IE).
  43. *
  44. * So now the device now has a WUSB address. From now on, we use
  45. * that to talk to it in the RPipes.
  46. *
  47. * ASSUMPTIONS:
  48. *
  49. * - We use the the as device address the port number where it is
  50. * connected (port 0 doesn't exist). For unauth, it is 128 + that.
  51. *
  52. * ROADMAP:
  53. *
  54. * This file contains the logic for doing that--entry points:
  55. *
  56. * wusb_devconnect_ack() Ack a device until _acked() called.
  57. * Called by notif.c:wusb_handle_dn_connect()
  58. * when a DN_Connect is received.
  59. *
  60. * wusb_devconnect_acked() Ack done, release resources.
  61. *
  62. * wusb_handle_dn_alive() Called by notif.c:wusb_handle_dn()
  63. * for processing a DN_Alive pong from a device.
  64. *
  65. * wusb_handle_dn_disconnect()Called by notif.c:wusb_handle_dn() to
  66. * process a disconenct request from a
  67. * device.
  68. *
  69. * __wusb_dev_disable() Called by rh.c:wusbhc_rh_clear_port_feat() when
  70. * disabling a port.
  71. *
  72. * wusb_devconnect_create() Called when creating the host by
  73. * lc.c:wusbhc_create().
  74. *
  75. * wusb_devconnect_destroy() Cleanup called removing the host. Called
  76. * by lc.c:wusbhc_destroy().
  77. *
  78. * Each Wireless USB host maintains a list of DN_Connect requests
  79. * (actually we maintain a list of pending Connect Acks, the
  80. * wusbhc->ca_list).
  81. *
  82. * LIFE CYCLE OF port->wusb_dev
  83. *
  84. * Before the @wusbhc structure put()s the reference it owns for
  85. * port->wusb_dev [and clean the wusb_dev pointer], it needs to
  86. * lock @wusbhc->mutex.
  87. */
  88. #include <linux/jiffies.h>
  89. #include <linux/ctype.h>
  90. #include <linux/slab.h>
  91. #include <linux/workqueue.h>
  92. #include <linux/export.h>
  93. #include "wusbhc.h"
  94. static void wusbhc_devconnect_acked_work(struct work_struct *work);
  95. static void wusb_dev_free(struct wusb_dev *wusb_dev)
  96. {
  97. if (wusb_dev) {
  98. kfree(wusb_dev->set_gtk_req);
  99. usb_free_urb(wusb_dev->set_gtk_urb);
  100. kfree(wusb_dev);
  101. }
  102. }
  103. static struct wusb_dev *wusb_dev_alloc(struct wusbhc *wusbhc)
  104. {
  105. struct wusb_dev *wusb_dev;
  106. struct urb *urb;
  107. struct usb_ctrlrequest *req;
  108. wusb_dev = kzalloc(sizeof(*wusb_dev), GFP_KERNEL);
  109. if (wusb_dev == NULL)
  110. goto err;
  111. wusb_dev->wusbhc = wusbhc;
  112. INIT_WORK(&wusb_dev->devconnect_acked_work, wusbhc_devconnect_acked_work);
  113. urb = usb_alloc_urb(0, GFP_KERNEL);
  114. if (urb == NULL)
  115. goto err;
  116. wusb_dev->set_gtk_urb = urb;
  117. req = kmalloc(sizeof(*req), GFP_KERNEL);
  118. if (req == NULL)
  119. goto err;
  120. wusb_dev->set_gtk_req = req;
  121. req->bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
  122. req->bRequest = USB_REQ_SET_DESCRIPTOR;
  123. req->wValue = cpu_to_le16(USB_DT_KEY << 8 | wusbhc->gtk_index);
  124. req->wIndex = 0;
  125. req->wLength = cpu_to_le16(wusbhc->gtk.descr.bLength);
  126. return wusb_dev;
  127. err:
  128. wusb_dev_free(wusb_dev);
  129. return NULL;
  130. }
  131. /*
  132. * Using the Connect-Ack list, fill out the @wusbhc Connect-Ack WUSB IE
  133. * properly so that it can be added to the MMC.
  134. *
  135. * We just get the @wusbhc->ca_list and fill out the first four ones or
  136. * less (per-spec WUSB1.0[7.5, before T7-38). If the ConnectAck WUSB
  137. * IE is not allocated, we alloc it.
  138. *
  139. * @wusbhc->mutex must be taken
  140. */
  141. static void wusbhc_fill_cack_ie(struct wusbhc *wusbhc)
  142. {
  143. unsigned cnt;
  144. struct wusb_dev *dev_itr;
  145. struct wuie_connect_ack *cack_ie;
  146. cack_ie = &wusbhc->cack_ie;
  147. cnt = 0;
  148. list_for_each_entry(dev_itr, &wusbhc->cack_list, cack_node) {
  149. cack_ie->blk[cnt].CDID = dev_itr->cdid;
  150. cack_ie->blk[cnt].bDeviceAddress = dev_itr->addr;
  151. if (++cnt >= WUIE_ELT_MAX)
  152. break;
  153. }
  154. cack_ie->hdr.bLength = sizeof(cack_ie->hdr)
  155. + cnt * sizeof(cack_ie->blk[0]);
  156. }
  157. /*
  158. * Register a new device that wants to connect
  159. *
  160. * A new device wants to connect, so we add it to the Connect-Ack
  161. * list. We give it an address in the unauthorized range (bit 8 set);
  162. * user space will have to drive authorization further on.
  163. *
  164. * @dev_addr: address to use for the device (which is also the port
  165. * number).
  166. *
  167. * @wusbhc->mutex must be taken
  168. */
  169. static struct wusb_dev *wusbhc_cack_add(struct wusbhc *wusbhc,
  170. struct wusb_dn_connect *dnc,
  171. const char *pr_cdid, u8 port_idx)
  172. {
  173. struct device *dev = wusbhc->dev;
  174. struct wusb_dev *wusb_dev;
  175. int new_connection = wusb_dn_connect_new_connection(dnc);
  176. u8 dev_addr;
  177. int result;
  178. /* Is it registered already? */
  179. list_for_each_entry(wusb_dev, &wusbhc->cack_list, cack_node)
  180. if (!memcmp(&wusb_dev->cdid, &dnc->CDID,
  181. sizeof(wusb_dev->cdid)))
  182. return wusb_dev;
  183. /* We don't have it, create an entry, register it */
  184. wusb_dev = wusb_dev_alloc(wusbhc);
  185. if (wusb_dev == NULL)
  186. return NULL;
  187. wusb_dev_init(wusb_dev);
  188. wusb_dev->cdid = dnc->CDID;
  189. wusb_dev->port_idx = port_idx;
  190. /*
  191. * Devices are always available within the cluster reservation
  192. * and since the hardware will take the intersection of the
  193. * per-device availability and the cluster reservation, the
  194. * per-device availability can simply be set to always
  195. * available.
  196. */
  197. bitmap_fill(wusb_dev->availability.bm, UWB_NUM_MAS);
  198. /* FIXME: handle reconnects instead of assuming connects are
  199. always new. */
  200. if (1 && new_connection == 0)
  201. new_connection = 1;
  202. if (new_connection) {
  203. dev_addr = (port_idx + 2) | WUSB_DEV_ADDR_UNAUTH;
  204. dev_info(dev, "Connecting new WUSB device to address %u, "
  205. "port %u\n", dev_addr, port_idx);
  206. result = wusb_set_dev_addr(wusbhc, wusb_dev, dev_addr);
  207. if (result < 0)
  208. return NULL;
  209. }
  210. wusb_dev->entry_ts = jiffies;
  211. list_add_tail(&wusb_dev->cack_node, &wusbhc->cack_list);
  212. wusbhc->cack_count++;
  213. wusbhc_fill_cack_ie(wusbhc);
  214. return wusb_dev;
  215. }
  216. /*
  217. * Remove a Connect-Ack context entry from the HCs view
  218. *
  219. * @wusbhc->mutex must be taken
  220. */
  221. static void wusbhc_cack_rm(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
  222. {
  223. list_del_init(&wusb_dev->cack_node);
  224. wusbhc->cack_count--;
  225. wusbhc_fill_cack_ie(wusbhc);
  226. }
  227. /*
  228. * @wusbhc->mutex must be taken */
  229. static
  230. void wusbhc_devconnect_acked(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
  231. {
  232. wusbhc_cack_rm(wusbhc, wusb_dev);
  233. if (wusbhc->cack_count)
  234. wusbhc_mmcie_set(wusbhc, 0, 0, &wusbhc->cack_ie.hdr);
  235. else
  236. wusbhc_mmcie_rm(wusbhc, &wusbhc->cack_ie.hdr);
  237. }
  238. static void wusbhc_devconnect_acked_work(struct work_struct *work)
  239. {
  240. struct wusb_dev *wusb_dev = container_of(work, struct wusb_dev,
  241. devconnect_acked_work);
  242. struct wusbhc *wusbhc = wusb_dev->wusbhc;
  243. mutex_lock(&wusbhc->mutex);
  244. wusbhc_devconnect_acked(wusbhc, wusb_dev);
  245. mutex_unlock(&wusbhc->mutex);
  246. wusb_dev_put(wusb_dev);
  247. }
  248. /*
  249. * Ack a device for connection
  250. *
  251. * FIXME: docs
  252. *
  253. * @pr_cdid: Printable CDID...hex Use @dnc->cdid for the real deal.
  254. *
  255. * So we get the connect ack IE (may have been allocated already),
  256. * find an empty connect block, an empty virtual port, create an
  257. * address with it (see below), make it an unauth addr [bit 7 set] and
  258. * set the MMC.
  259. *
  260. * Addresses: because WUSB hosts have no downstream hubs, we can do a
  261. * 1:1 mapping between 'port number' and device
  262. * address. This simplifies many things, as during this
  263. * initial connect phase the USB stack has no knoledge of
  264. * the device and hasn't assigned an address yet--we know
  265. * USB's choose_address() will use the same euristics we
  266. * use here, so we can assume which address will be assigned.
  267. *
  268. * USB stack always assigns address 1 to the root hub, so
  269. * to the port number we add 2 (thus virtual port #0 is
  270. * addr #2).
  271. *
  272. * @wusbhc shall be referenced
  273. */
  274. static
  275. void wusbhc_devconnect_ack(struct wusbhc *wusbhc, struct wusb_dn_connect *dnc,
  276. const char *pr_cdid)
  277. {
  278. int result;
  279. struct device *dev = wusbhc->dev;
  280. struct wusb_dev *wusb_dev;
  281. struct wusb_port *port;
  282. unsigned idx, devnum;
  283. mutex_lock(&wusbhc->mutex);
  284. /* Check we are not handling it already */
  285. for (idx = 0; idx < wusbhc->ports_max; idx++) {
  286. port = wusb_port_by_idx(wusbhc, idx);
  287. if (port->wusb_dev
  288. && memcmp(&dnc->CDID, &port->wusb_dev->cdid, sizeof(dnc->CDID)) == 0)
  289. goto error_unlock;
  290. }
  291. /* Look up those fake ports we have for a free one */
  292. for (idx = 0; idx < wusbhc->ports_max; idx++) {
  293. port = wusb_port_by_idx(wusbhc, idx);
  294. if ((port->status & USB_PORT_STAT_POWER)
  295. && !(port->status & USB_PORT_STAT_CONNECTION))
  296. break;
  297. }
  298. if (idx >= wusbhc->ports_max) {
  299. dev_err(dev, "Host controller can't connect more devices "
  300. "(%u already connected); device %s rejected\n",
  301. wusbhc->ports_max, pr_cdid);
  302. /* NOTE: we could send a WUIE_Disconnect here, but we haven't
  303. * event acked, so the device will eventually timeout the
  304. * connection, right? */
  305. goto error_unlock;
  306. }
  307. devnum = idx + 2;
  308. /* Make sure we are using no crypto on that "virtual port" */
  309. wusbhc->set_ptk(wusbhc, idx, 0, NULL, 0);
  310. /* Grab a filled in Connect-Ack context, fill out the
  311. * Connect-Ack Wireless USB IE, set the MMC */
  312. wusb_dev = wusbhc_cack_add(wusbhc, dnc, pr_cdid, idx);
  313. if (wusb_dev == NULL)
  314. goto error_unlock;
  315. result = wusbhc_mmcie_set(wusbhc, 0, 0, &wusbhc->cack_ie.hdr);
  316. if (result < 0)
  317. goto error_unlock;
  318. /* Give the device at least 2ms (WUSB1.0[7.5.1p3]), let's do
  319. * three for a good measure */
  320. msleep(3);
  321. port->wusb_dev = wusb_dev;
  322. port->status |= USB_PORT_STAT_CONNECTION;
  323. port->change |= USB_PORT_STAT_C_CONNECTION;
  324. /* Now the port status changed to connected; khubd will
  325. * pick the change up and try to reset the port to bring it to
  326. * the enabled state--so this process returns up to the stack
  327. * and it calls back into wusbhc_rh_port_reset().
  328. */
  329. error_unlock:
  330. mutex_unlock(&wusbhc->mutex);
  331. return;
  332. }
  333. /*
  334. * Disconnect a Wireless USB device from its fake port
  335. *
  336. * Marks the port as disconnected so that khubd can pick up the change
  337. * and drops our knowledge about the device.
  338. *
  339. * Assumes there is a device connected
  340. *
  341. * @port_index: zero based port number
  342. *
  343. * NOTE: @wusbhc->mutex is locked
  344. *
  345. * WARNING: From here it is not very safe to access anything hanging off
  346. * wusb_dev
  347. */
  348. static void __wusbhc_dev_disconnect(struct wusbhc *wusbhc,
  349. struct wusb_port *port)
  350. {
  351. struct wusb_dev *wusb_dev = port->wusb_dev;
  352. port->status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE
  353. | USB_PORT_STAT_SUSPEND | USB_PORT_STAT_RESET
  354. | USB_PORT_STAT_LOW_SPEED | USB_PORT_STAT_HIGH_SPEED);
  355. port->change |= USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE;
  356. if (wusb_dev) {
  357. dev_dbg(wusbhc->dev, "disconnecting device from port %d\n", wusb_dev->port_idx);
  358. if (!list_empty(&wusb_dev->cack_node))
  359. list_del_init(&wusb_dev->cack_node);
  360. /* For the one in cack_add() */
  361. wusb_dev_put(wusb_dev);
  362. }
  363. port->wusb_dev = NULL;
  364. /* After a device disconnects, change the GTK (see [WUSB]
  365. * section 6.2.11.2). */
  366. if (wusbhc->active)
  367. wusbhc_gtk_rekey(wusbhc);
  368. /* The Wireless USB part has forgotten about the device already; now
  369. * khubd's timer will pick up the disconnection and remove the USB
  370. * device from the system
  371. */
  372. }
  373. /*
  374. * Refresh the list of keep alives to emit in the MMC
  375. *
  376. * We only publish the first four devices that have a coming timeout
  377. * condition. Then when we are done processing those, we go for the
  378. * next ones. We ignore the ones that have timed out already (they'll
  379. * be purged).
  380. *
  381. * This might cause the first devices to timeout the last devices in
  382. * the port array...FIXME: come up with a better algorithm?
  383. *
  384. * Note we can't do much about MMC's ops errors; we hope next refresh
  385. * will kind of handle it.
  386. *
  387. * NOTE: @wusbhc->mutex is locked
  388. */
  389. static void __wusbhc_keep_alive(struct wusbhc *wusbhc)
  390. {
  391. struct device *dev = wusbhc->dev;
  392. unsigned cnt;
  393. struct wusb_dev *wusb_dev;
  394. struct wusb_port *wusb_port;
  395. struct wuie_keep_alive *ie = &wusbhc->keep_alive_ie;
  396. unsigned keep_alives, old_keep_alives;
  397. old_keep_alives = ie->hdr.bLength - sizeof(ie->hdr);
  398. keep_alives = 0;
  399. for (cnt = 0;
  400. keep_alives < WUIE_ELT_MAX && cnt < wusbhc->ports_max;
  401. cnt++) {
  402. unsigned tt = msecs_to_jiffies(wusbhc->trust_timeout);
  403. wusb_port = wusb_port_by_idx(wusbhc, cnt);
  404. wusb_dev = wusb_port->wusb_dev;
  405. if (wusb_dev == NULL)
  406. continue;
  407. if (wusb_dev->usb_dev == NULL)
  408. continue;
  409. if (time_after(jiffies, wusb_dev->entry_ts + tt)) {
  410. dev_err(dev, "KEEPALIVE: device %u timed out\n",
  411. wusb_dev->addr);
  412. __wusbhc_dev_disconnect(wusbhc, wusb_port);
  413. } else if (time_after(jiffies, wusb_dev->entry_ts + tt/3)) {
  414. /* Approaching timeout cut off, need to refresh */
  415. ie->bDeviceAddress[keep_alives++] = wusb_dev->addr;
  416. }
  417. }
  418. if (keep_alives & 0x1) /* pad to even number ([WUSB] section 7.5.9) */
  419. ie->bDeviceAddress[keep_alives++] = 0x7f;
  420. ie->hdr.bLength = sizeof(ie->hdr) +
  421. keep_alives*sizeof(ie->bDeviceAddress[0]);
  422. if (keep_alives > 0)
  423. wusbhc_mmcie_set(wusbhc, 10, 5, &ie->hdr);
  424. else if (old_keep_alives != 0)
  425. wusbhc_mmcie_rm(wusbhc, &ie->hdr);
  426. }
  427. /*
  428. * Do a run through all devices checking for timeouts
  429. */
  430. static void wusbhc_keep_alive_run(struct work_struct *ws)
  431. {
  432. struct delayed_work *dw = to_delayed_work(ws);
  433. struct wusbhc *wusbhc = container_of(dw, struct wusbhc, keep_alive_timer);
  434. mutex_lock(&wusbhc->mutex);
  435. __wusbhc_keep_alive(wusbhc);
  436. mutex_unlock(&wusbhc->mutex);
  437. queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
  438. msecs_to_jiffies(wusbhc->trust_timeout / 2));
  439. }
  440. /*
  441. * Find the wusb_dev from its device address.
  442. *
  443. * The device can be found directly from the address (see
  444. * wusb_cack_add() for where the device address is set to port_idx
  445. * +2), except when the address is zero.
  446. */
  447. static struct wusb_dev *wusbhc_find_dev_by_addr(struct wusbhc *wusbhc, u8 addr)
  448. {
  449. int p;
  450. if (addr == 0xff) /* unconnected */
  451. return NULL;
  452. if (addr > 0) {
  453. int port = (addr & ~0x80) - 2;
  454. if (port < 0 || port >= wusbhc->ports_max)
  455. return NULL;
  456. return wusb_port_by_idx(wusbhc, port)->wusb_dev;
  457. }
  458. /* Look for the device with address 0. */
  459. for (p = 0; p < wusbhc->ports_max; p++) {
  460. struct wusb_dev *wusb_dev = wusb_port_by_idx(wusbhc, p)->wusb_dev;
  461. if (wusb_dev && wusb_dev->addr == addr)
  462. return wusb_dev;
  463. }
  464. return NULL;
  465. }
  466. /*
  467. * Handle a DN_Alive notification (WUSB1.0[7.6.1])
  468. *
  469. * This just updates the device activity timestamp and then refreshes
  470. * the keep alive IE.
  471. *
  472. * @wusbhc shall be referenced and unlocked
  473. */
  474. static void wusbhc_handle_dn_alive(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
  475. {
  476. mutex_lock(&wusbhc->mutex);
  477. wusb_dev->entry_ts = jiffies;
  478. __wusbhc_keep_alive(wusbhc);
  479. mutex_unlock(&wusbhc->mutex);
  480. }
  481. /*
  482. * Handle a DN_Connect notification (WUSB1.0[7.6.1])
  483. *
  484. * @wusbhc
  485. * @pkt_hdr
  486. * @size: Size of the buffer where the notification resides; if the
  487. * notification data suggests there should be more data than
  488. * available, an error will be signaled and the whole buffer
  489. * consumed.
  490. *
  491. * @wusbhc->mutex shall be held
  492. */
  493. static void wusbhc_handle_dn_connect(struct wusbhc *wusbhc,
  494. struct wusb_dn_hdr *dn_hdr,
  495. size_t size)
  496. {
  497. struct device *dev = wusbhc->dev;
  498. struct wusb_dn_connect *dnc;
  499. char pr_cdid[WUSB_CKHDID_STRSIZE];
  500. static const char *beacon_behaviour[] = {
  501. "reserved",
  502. "self-beacon",
  503. "directed-beacon",
  504. "no-beacon"
  505. };
  506. if (size < sizeof(*dnc)) {
  507. dev_err(dev, "DN CONNECT: short notification (%zu < %zu)\n",
  508. size, sizeof(*dnc));
  509. return;
  510. }
  511. dnc = container_of(dn_hdr, struct wusb_dn_connect, hdr);
  512. ckhdid_printf(pr_cdid, sizeof(pr_cdid), &dnc->CDID);
  513. dev_info(dev, "DN CONNECT: device %s @ %x (%s) wants to %s\n",
  514. pr_cdid,
  515. wusb_dn_connect_prev_dev_addr(dnc),
  516. beacon_behaviour[wusb_dn_connect_beacon_behavior(dnc)],
  517. wusb_dn_connect_new_connection(dnc) ? "connect" : "reconnect");
  518. /* ACK the connect */
  519. wusbhc_devconnect_ack(wusbhc, dnc, pr_cdid);
  520. }
  521. /*
  522. * Handle a DN_Disconnect notification (WUSB1.0[7.6.1])
  523. *
  524. * Device is going down -- do the disconnect.
  525. *
  526. * @wusbhc shall be referenced and unlocked
  527. */
  528. static void wusbhc_handle_dn_disconnect(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev)
  529. {
  530. struct device *dev = wusbhc->dev;
  531. dev_info(dev, "DN DISCONNECT: device 0x%02x going down\n", wusb_dev->addr);
  532. mutex_lock(&wusbhc->mutex);
  533. __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, wusb_dev->port_idx));
  534. mutex_unlock(&wusbhc->mutex);
  535. }
  536. /*
  537. * Handle a Device Notification coming a host
  538. *
  539. * The Device Notification comes from a host (HWA, DWA or WHCI)
  540. * wrapped in a set of headers. Somebody else has peeled off those
  541. * headers for us and we just get one Device Notifications.
  542. *
  543. * Invalid DNs (e.g., too short) are discarded.
  544. *
  545. * @wusbhc shall be referenced
  546. *
  547. * FIXMES:
  548. * - implement priorities as in WUSB1.0[Table 7-55]?
  549. */
  550. void wusbhc_handle_dn(struct wusbhc *wusbhc, u8 srcaddr,
  551. struct wusb_dn_hdr *dn_hdr, size_t size)
  552. {
  553. struct device *dev = wusbhc->dev;
  554. struct wusb_dev *wusb_dev;
  555. if (size < sizeof(struct wusb_dn_hdr)) {
  556. dev_err(dev, "DN data shorter than DN header (%d < %d)\n",
  557. (int)size, (int)sizeof(struct wusb_dn_hdr));
  558. return;
  559. }
  560. wusb_dev = wusbhc_find_dev_by_addr(wusbhc, srcaddr);
  561. if (wusb_dev == NULL && dn_hdr->bType != WUSB_DN_CONNECT) {
  562. dev_dbg(dev, "ignoring DN %d from unconnected device %02x\n",
  563. dn_hdr->bType, srcaddr);
  564. return;
  565. }
  566. switch (dn_hdr->bType) {
  567. case WUSB_DN_CONNECT:
  568. wusbhc_handle_dn_connect(wusbhc, dn_hdr, size);
  569. break;
  570. case WUSB_DN_ALIVE:
  571. wusbhc_handle_dn_alive(wusbhc, wusb_dev);
  572. break;
  573. case WUSB_DN_DISCONNECT:
  574. wusbhc_handle_dn_disconnect(wusbhc, wusb_dev);
  575. break;
  576. case WUSB_DN_MASAVAILCHANGED:
  577. case WUSB_DN_RWAKE:
  578. case WUSB_DN_SLEEP:
  579. /* FIXME: handle these DNs. */
  580. break;
  581. case WUSB_DN_EPRDY:
  582. /* The hardware handles these. */
  583. break;
  584. default:
  585. dev_warn(dev, "unknown DN %u (%d octets) from %u\n",
  586. dn_hdr->bType, (int)size, srcaddr);
  587. }
  588. }
  589. EXPORT_SYMBOL_GPL(wusbhc_handle_dn);
  590. /*
  591. * Disconnect a WUSB device from a the cluster
  592. *
  593. * @wusbhc
  594. * @port Fake port where the device is (wusbhc index, not USB port number).
  595. *
  596. * In Wireless USB, a disconnect is basically telling the device he is
  597. * being disconnected and forgetting about him.
  598. *
  599. * We send the device a Device Disconnect IE (WUSB1.0[7.5.11]) for 100
  600. * ms and then keep going.
  601. *
  602. * We don't do much in case of error; we always pretend we disabled
  603. * the port and disconnected the device. If physically the request
  604. * didn't get there (many things can fail in the way there), the stack
  605. * will reject the device's communication attempts.
  606. *
  607. * @wusbhc should be refcounted and locked
  608. */
  609. void __wusbhc_dev_disable(struct wusbhc *wusbhc, u8 port_idx)
  610. {
  611. int result;
  612. struct device *dev = wusbhc->dev;
  613. struct wusb_dev *wusb_dev;
  614. struct wuie_disconnect *ie;
  615. wusb_dev = wusb_port_by_idx(wusbhc, port_idx)->wusb_dev;
  616. if (wusb_dev == NULL) {
  617. /* reset no device? ignore */
  618. dev_dbg(dev, "DISCONNECT: no device at port %u, ignoring\n",
  619. port_idx);
  620. return;
  621. }
  622. __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, port_idx));
  623. ie = kzalloc(sizeof(*ie), GFP_KERNEL);
  624. if (ie == NULL)
  625. return;
  626. ie->hdr.bLength = sizeof(*ie);
  627. ie->hdr.bIEIdentifier = WUIE_ID_DEVICE_DISCONNECT;
  628. ie->bDeviceAddress = wusb_dev->addr;
  629. result = wusbhc_mmcie_set(wusbhc, 0, 0, &ie->hdr);
  630. if (result < 0)
  631. dev_err(dev, "DISCONNECT: can't set MMC: %d\n", result);
  632. else {
  633. /* At least 6 MMCs, assuming at least 1 MMC per zone. */
  634. msleep(7*4);
  635. wusbhc_mmcie_rm(wusbhc, &ie->hdr);
  636. }
  637. kfree(ie);
  638. }
  639. /*
  640. * Walk over the BOS descriptor, verify and grok it
  641. *
  642. * @usb_dev: referenced
  643. * @wusb_dev: referenced and unlocked
  644. *
  645. * The BOS descriptor is defined at WUSB1.0[7.4.1], and it defines a
  646. * "flexible" way to wrap all kinds of descriptors inside an standard
  647. * descriptor (wonder why they didn't use normal descriptors,
  648. * btw). Not like they lack code.
  649. *
  650. * At the end we go to look for the WUSB Device Capabilities
  651. * (WUSB1.0[7.4.1.1]) that is wrapped in a device capability descriptor
  652. * that is part of the BOS descriptor set. That tells us what does the
  653. * device support (dual role, beacon type, UWB PHY rates).
  654. */
  655. static int wusb_dev_bos_grok(struct usb_device *usb_dev,
  656. struct wusb_dev *wusb_dev,
  657. struct usb_bos_descriptor *bos, size_t desc_size)
  658. {
  659. ssize_t result;
  660. struct device *dev = &usb_dev->dev;
  661. void *itr, *top;
  662. /* Walk over BOS capabilities, verify them */
  663. itr = (void *)bos + sizeof(*bos);
  664. top = itr + desc_size - sizeof(*bos);
  665. while (itr < top) {
  666. struct usb_dev_cap_header *cap_hdr = itr;
  667. size_t cap_size;
  668. u8 cap_type;
  669. if (top - itr < sizeof(*cap_hdr)) {
  670. dev_err(dev, "Device BUG? premature end of BOS header "
  671. "data [offset 0x%02x]: only %zu bytes left\n",
  672. (int)(itr - (void *)bos), top - itr);
  673. result = -ENOSPC;
  674. goto error_bad_cap;
  675. }
  676. cap_size = cap_hdr->bLength;
  677. cap_type = cap_hdr->bDevCapabilityType;
  678. if (cap_size == 0)
  679. break;
  680. if (cap_size > top - itr) {
  681. dev_err(dev, "Device BUG? premature end of BOS data "
  682. "[offset 0x%02x cap %02x %zu bytes]: "
  683. "only %zu bytes left\n",
  684. (int)(itr - (void *)bos),
  685. cap_type, cap_size, top - itr);
  686. result = -EBADF;
  687. goto error_bad_cap;
  688. }
  689. switch (cap_type) {
  690. case USB_CAP_TYPE_WIRELESS_USB:
  691. if (cap_size != sizeof(*wusb_dev->wusb_cap_descr))
  692. dev_err(dev, "Device BUG? WUSB Capability "
  693. "descriptor is %zu bytes vs %zu "
  694. "needed\n", cap_size,
  695. sizeof(*wusb_dev->wusb_cap_descr));
  696. else
  697. wusb_dev->wusb_cap_descr = itr;
  698. break;
  699. default:
  700. dev_err(dev, "BUG? Unknown BOS capability 0x%02x "
  701. "(%zu bytes) at offset 0x%02x\n", cap_type,
  702. cap_size, (int)(itr - (void *)bos));
  703. }
  704. itr += cap_size;
  705. }
  706. result = 0;
  707. error_bad_cap:
  708. return result;
  709. }
  710. /*
  711. * Add information from the BOS descriptors to the device
  712. *
  713. * @usb_dev: referenced
  714. * @wusb_dev: referenced and unlocked
  715. *
  716. * So what we do is we alloc a space for the BOS descriptor of 64
  717. * bytes; read the first four bytes which include the wTotalLength
  718. * field (WUSB1.0[T7-26]) and if it fits in those 64 bytes, read the
  719. * whole thing. If not we realloc to that size.
  720. *
  721. * Then we call the groking function, that will fill up
  722. * wusb_dev->wusb_cap_descr, which is what we'll need later on.
  723. */
  724. static int wusb_dev_bos_add(struct usb_device *usb_dev,
  725. struct wusb_dev *wusb_dev)
  726. {
  727. ssize_t result;
  728. struct device *dev = &usb_dev->dev;
  729. struct usb_bos_descriptor *bos;
  730. size_t alloc_size = 32, desc_size = 4;
  731. bos = kmalloc(alloc_size, GFP_KERNEL);
  732. if (bos == NULL)
  733. return -ENOMEM;
  734. result = usb_get_descriptor(usb_dev, USB_DT_BOS, 0, bos, desc_size);
  735. if (result < 4) {
  736. dev_err(dev, "Can't get BOS descriptor or too short: %zd\n",
  737. result);
  738. goto error_get_descriptor;
  739. }
  740. desc_size = le16_to_cpu(bos->wTotalLength);
  741. if (desc_size >= alloc_size) {
  742. kfree(bos);
  743. alloc_size = desc_size;
  744. bos = kmalloc(alloc_size, GFP_KERNEL);
  745. if (bos == NULL)
  746. return -ENOMEM;
  747. }
  748. result = usb_get_descriptor(usb_dev, USB_DT_BOS, 0, bos, desc_size);
  749. if (result < 0 || result != desc_size) {
  750. dev_err(dev, "Can't get BOS descriptor or too short (need "
  751. "%zu bytes): %zd\n", desc_size, result);
  752. goto error_get_descriptor;
  753. }
  754. if (result < sizeof(*bos)
  755. || le16_to_cpu(bos->wTotalLength) != desc_size) {
  756. dev_err(dev, "Can't get BOS descriptor or too short (need "
  757. "%zu bytes): %zd\n", desc_size, result);
  758. goto error_get_descriptor;
  759. }
  760. result = wusb_dev_bos_grok(usb_dev, wusb_dev, bos, result);
  761. if (result < 0)
  762. goto error_bad_bos;
  763. wusb_dev->bos = bos;
  764. return 0;
  765. error_bad_bos:
  766. error_get_descriptor:
  767. kfree(bos);
  768. wusb_dev->wusb_cap_descr = NULL;
  769. return result;
  770. }
  771. static void wusb_dev_bos_rm(struct wusb_dev *wusb_dev)
  772. {
  773. kfree(wusb_dev->bos);
  774. wusb_dev->wusb_cap_descr = NULL;
  775. };
  776. /*
  777. * USB stack's device addition Notifier Callback
  778. *
  779. * Called from drivers/usb/core/hub.c when a new device is added; we
  780. * use this hook to perform certain WUSB specific setup work on the
  781. * new device. As well, it is the first time we can connect the
  782. * wusb_dev and the usb_dev. So we note it down in wusb_dev and take a
  783. * reference that we'll drop.
  784. *
  785. * First we need to determine if the device is a WUSB device (else we
  786. * ignore it). For that we use the speed setting (USB_SPEED_WIRELESS)
  787. * [FIXME: maybe we'd need something more definitive]. If so, we track
  788. * it's usb_busd and from there, the WUSB HC.
  789. *
  790. * Because all WUSB HCs are contained in a 'struct wusbhc', voila, we
  791. * get the wusbhc for the device.
  792. *
  793. * We have a reference on @usb_dev (as we are called at the end of its
  794. * enumeration).
  795. *
  796. * NOTE: @usb_dev locked
  797. */
  798. static void wusb_dev_add_ncb(struct usb_device *usb_dev)
  799. {
  800. int result = 0;
  801. struct wusb_dev *wusb_dev;
  802. struct wusbhc *wusbhc;
  803. struct device *dev = &usb_dev->dev;
  804. u8 port_idx;
  805. if (usb_dev->wusb == 0 || usb_dev->devnum == 1)
  806. return; /* skip non wusb and wusb RHs */
  807. usb_set_device_state(usb_dev, USB_STATE_UNAUTHENTICATED);
  808. wusbhc = wusbhc_get_by_usb_dev(usb_dev);
  809. if (wusbhc == NULL)
  810. goto error_nodev;
  811. mutex_lock(&wusbhc->mutex);
  812. wusb_dev = __wusb_dev_get_by_usb_dev(wusbhc, usb_dev);
  813. port_idx = wusb_port_no_to_idx(usb_dev->portnum);
  814. mutex_unlock(&wusbhc->mutex);
  815. if (wusb_dev == NULL)
  816. goto error_nodev;
  817. wusb_dev->usb_dev = usb_get_dev(usb_dev);
  818. usb_dev->wusb_dev = wusb_dev_get(wusb_dev);
  819. result = wusb_dev_sec_add(wusbhc, usb_dev, wusb_dev);
  820. if (result < 0) {
  821. dev_err(dev, "Cannot enable security: %d\n", result);
  822. goto error_sec_add;
  823. }
  824. /* Now query the device for it's BOS and attach it to wusb_dev */
  825. result = wusb_dev_bos_add(usb_dev, wusb_dev);
  826. if (result < 0) {
  827. dev_err(dev, "Cannot get BOS descriptors: %d\n", result);
  828. goto error_bos_add;
  829. }
  830. result = wusb_dev_sysfs_add(wusbhc, usb_dev, wusb_dev);
  831. if (result < 0)
  832. goto error_add_sysfs;
  833. out:
  834. wusb_dev_put(wusb_dev);
  835. wusbhc_put(wusbhc);
  836. error_nodev:
  837. return;
  838. wusb_dev_sysfs_rm(wusb_dev);
  839. error_add_sysfs:
  840. wusb_dev_bos_rm(wusb_dev);
  841. error_bos_add:
  842. wusb_dev_sec_rm(wusb_dev);
  843. error_sec_add:
  844. mutex_lock(&wusbhc->mutex);
  845. __wusbhc_dev_disconnect(wusbhc, wusb_port_by_idx(wusbhc, port_idx));
  846. mutex_unlock(&wusbhc->mutex);
  847. goto out;
  848. }
  849. /*
  850. * Undo all the steps done at connection by the notifier callback
  851. *
  852. * NOTE: @usb_dev locked
  853. */
  854. static void wusb_dev_rm_ncb(struct usb_device *usb_dev)
  855. {
  856. struct wusb_dev *wusb_dev = usb_dev->wusb_dev;
  857. if (usb_dev->wusb == 0 || usb_dev->devnum == 1)
  858. return; /* skip non wusb and wusb RHs */
  859. wusb_dev_sysfs_rm(wusb_dev);
  860. wusb_dev_bos_rm(wusb_dev);
  861. wusb_dev_sec_rm(wusb_dev);
  862. wusb_dev->usb_dev = NULL;
  863. usb_dev->wusb_dev = NULL;
  864. wusb_dev_put(wusb_dev);
  865. usb_put_dev(usb_dev);
  866. }
  867. /*
  868. * Handle notifications from the USB stack (notifier call back)
  869. *
  870. * This is called when the USB stack does a
  871. * usb_{bus,device}_{add,remove}() so we can do WUSB specific
  872. * handling. It is called with [for the case of
  873. * USB_DEVICE_{ADD,REMOVE} with the usb_dev locked.
  874. */
  875. int wusb_usb_ncb(struct notifier_block *nb, unsigned long val,
  876. void *priv)
  877. {
  878. int result = NOTIFY_OK;
  879. switch (val) {
  880. case USB_DEVICE_ADD:
  881. wusb_dev_add_ncb(priv);
  882. break;
  883. case USB_DEVICE_REMOVE:
  884. wusb_dev_rm_ncb(priv);
  885. break;
  886. case USB_BUS_ADD:
  887. /* ignore (for now) */
  888. case USB_BUS_REMOVE:
  889. break;
  890. default:
  891. WARN_ON(1);
  892. result = NOTIFY_BAD;
  893. }
  894. return result;
  895. }
  896. /*
  897. * Return a referenced wusb_dev given a @wusbhc and @usb_dev
  898. */
  899. struct wusb_dev *__wusb_dev_get_by_usb_dev(struct wusbhc *wusbhc,
  900. struct usb_device *usb_dev)
  901. {
  902. struct wusb_dev *wusb_dev;
  903. u8 port_idx;
  904. port_idx = wusb_port_no_to_idx(usb_dev->portnum);
  905. BUG_ON(port_idx > wusbhc->ports_max);
  906. wusb_dev = wusb_port_by_idx(wusbhc, port_idx)->wusb_dev;
  907. if (wusb_dev != NULL) /* ops, device is gone */
  908. wusb_dev_get(wusb_dev);
  909. return wusb_dev;
  910. }
  911. EXPORT_SYMBOL_GPL(__wusb_dev_get_by_usb_dev);
  912. void wusb_dev_destroy(struct kref *_wusb_dev)
  913. {
  914. struct wusb_dev *wusb_dev = container_of(_wusb_dev, struct wusb_dev, refcnt);
  915. list_del_init(&wusb_dev->cack_node);
  916. wusb_dev_free(wusb_dev);
  917. }
  918. EXPORT_SYMBOL_GPL(wusb_dev_destroy);
  919. /*
  920. * Create all the device connect handling infrastructure
  921. *
  922. * This is basically the device info array, Connect Acknowledgement
  923. * (cack) lists, keep-alive timers (and delayed work thread).
  924. */
  925. int wusbhc_devconnect_create(struct wusbhc *wusbhc)
  926. {
  927. wusbhc->keep_alive_ie.hdr.bIEIdentifier = WUIE_ID_KEEP_ALIVE;
  928. wusbhc->keep_alive_ie.hdr.bLength = sizeof(wusbhc->keep_alive_ie.hdr);
  929. INIT_DELAYED_WORK(&wusbhc->keep_alive_timer, wusbhc_keep_alive_run);
  930. wusbhc->cack_ie.hdr.bIEIdentifier = WUIE_ID_CONNECTACK;
  931. wusbhc->cack_ie.hdr.bLength = sizeof(wusbhc->cack_ie.hdr);
  932. INIT_LIST_HEAD(&wusbhc->cack_list);
  933. return 0;
  934. }
  935. /*
  936. * Release all resources taken by the devconnect stuff
  937. */
  938. void wusbhc_devconnect_destroy(struct wusbhc *wusbhc)
  939. {
  940. /* no op */
  941. }
  942. /*
  943. * wusbhc_devconnect_start - start accepting device connections
  944. * @wusbhc: the WUSB HC
  945. *
  946. * Sets the Host Info IE to accept all new connections.
  947. *
  948. * FIXME: This also enables the keep alives but this is not necessary
  949. * until there are connected and authenticated devices.
  950. */
  951. int wusbhc_devconnect_start(struct wusbhc *wusbhc)
  952. {
  953. struct device *dev = wusbhc->dev;
  954. struct wuie_host_info *hi;
  955. int result;
  956. hi = kzalloc(sizeof(*hi), GFP_KERNEL);
  957. if (hi == NULL)
  958. return -ENOMEM;
  959. hi->hdr.bLength = sizeof(*hi);
  960. hi->hdr.bIEIdentifier = WUIE_ID_HOST_INFO;
  961. hi->attributes = cpu_to_le16((wusbhc->rsv->stream << 3) | WUIE_HI_CAP_ALL);
  962. hi->CHID = wusbhc->chid;
  963. result = wusbhc_mmcie_set(wusbhc, 0, 0, &hi->hdr);
  964. if (result < 0) {
  965. dev_err(dev, "Cannot add Host Info MMCIE: %d\n", result);
  966. goto error_mmcie_set;
  967. }
  968. wusbhc->wuie_host_info = hi;
  969. queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,
  970. msecs_to_jiffies(wusbhc->trust_timeout / 2));
  971. return 0;
  972. error_mmcie_set:
  973. kfree(hi);
  974. return result;
  975. }
  976. /*
  977. * wusbhc_devconnect_stop - stop managing connected devices
  978. * @wusbhc: the WUSB HC
  979. *
  980. * Disconnects any devices still connected, stops the keep alives and
  981. * removes the Host Info IE.
  982. */
  983. void wusbhc_devconnect_stop(struct wusbhc *wusbhc)
  984. {
  985. int i;
  986. mutex_lock(&wusbhc->mutex);
  987. for (i = 0; i < wusbhc->ports_max; i++) {
  988. if (wusbhc->port[i].wusb_dev)
  989. __wusbhc_dev_disconnect(wusbhc, &wusbhc->port[i]);
  990. }
  991. mutex_unlock(&wusbhc->mutex);
  992. cancel_delayed_work_sync(&wusbhc->keep_alive_timer);
  993. wusbhc_mmcie_rm(wusbhc, &wusbhc->wuie_host_info->hdr);
  994. kfree(wusbhc->wuie_host_info);
  995. wusbhc->wuie_host_info = NULL;
  996. }
  997. /*
  998. * wusb_set_dev_addr - set the WUSB device address used by the host
  999. * @wusbhc: the WUSB HC the device is connect to
  1000. * @wusb_dev: the WUSB device
  1001. * @addr: new device address
  1002. */
  1003. int wusb_set_dev_addr(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev, u8 addr)
  1004. {
  1005. int result;
  1006. wusb_dev->addr = addr;
  1007. result = wusbhc->dev_info_set(wusbhc, wusb_dev);
  1008. if (result < 0)
  1009. dev_err(wusbhc->dev, "device %d: failed to set device "
  1010. "address\n", wusb_dev->port_idx);
  1011. else
  1012. dev_info(wusbhc->dev, "device %d: %s addr %u\n",
  1013. wusb_dev->port_idx,
  1014. (addr & WUSB_DEV_ADDR_UNAUTH) ? "unauth" : "auth",
  1015. wusb_dev->addr);
  1016. return result;
  1017. }