devconnect.c 38 KB

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