devconnect.c 32 KB

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