devconnect.c 39 KB

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