devconnect.c 35 KB

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