1102 lines · 31.6 KB · C
RawDownload
/* * WUSB Wire Adapter: Control/Data Streaming Interface (WUSB[8]) * Device Connect handling * * Copyright (C) 2006 Intel Corporation * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301, USA. * * * FIXME: docs * FIXME: this file needs to be broken up, it's grown too big * * * WUSB1.0[7.1, 7.5.1, ] * * WUSB device connection is kind of messy. Some background: * *     When a device wants to connect it scans the UWB radio channels *     looking for a WUSB Channel; a WUSB channel is defined by MMCs *     (Micro Managed Commands or something like that) [see *     Design-overview for more on this] . * * So, device scans the radio, finds MMCs and thus a host and checks * when the next DNTS is. It sends a Device Notification Connect * (DN_Connect); the host picks it up (through nep.c and notif.c, ends * up in wusb_devconnect_ack(), which creates a wusb_dev structure in * wusbhc->port[port_number].wusb_dev), assigns an unauth address * to the device (this means from 0x80 to 0xfe) and sends, in the MMC * a Connect Ack Information Element (ConnAck IE). * * So now the device now has a WUSB address. From now on, we use * that to talk to it in the RPipes. * * ASSUMPTIONS: * *  - We use the the as device address the port number where it is *    connected (port 0 doesn't exist). For unauth, it is 128 + that. * * ROADMAP: * *   This file contains the logic for doing that--entry points: * *   wusb_devconnect_ack()      Ack a device until _acked() called. *                              Called by notif.c:wusb_handle_dn_connect() *                              when a DN_Connect is received. * *     wusb_devconnect_acked()  Ack done, release resources. * *   wusb_handle_dn_alive()     Called by notif.c:wusb_handle_dn() *                              for processing a DN_Alive pong from a device. * *   wusb_handle_dn_disconnect()Called by notif.c:wusb_handle_dn() to *                              process a disconenct request from a *                              device. * *   __wusb_dev_disable()       Called by rh.c:wusbhc_rh_clear_port_feat() when *                              disabling a port. * *   wusb_devconnect_create()   Called when creating the host by *                              lc.c:wusbhc_create(). * *   wusb_devconnect_destroy()  Cleanup called removing the host. Called *                              by lc.c:wusbhc_destroy(). * *   Each Wireless USB host maintains a list of DN_Connect requests *   (actually we maintain a list of pending Connect Acks, the *   wusbhc->ca_list). * * LIFE CYCLE OF port->wusb_dev * *   Before the @wusbhc structure put()s the reference it owns for *   port->wusb_dev [and clean the wusb_dev pointer], it needs to *   lock @wusbhc->mutex. */ #include <linux/jiffies.h>#include <linux/ctype.h>#include <linux/slab.h>#include <linux/workqueue.h>#include <linux/export.h>#include "wusbhc.h" static void wusbhc_devconnect_acked_work(struct work_struct *work); static void wusb_dev_free(struct wusb_dev *wusb_dev){	kfree(wusb_dev);} static struct wusb_dev *wusb_dev_alloc(struct wusbhc *wusbhc){	struct wusb_dev *wusb_dev; 	wusb_dev = kzalloc(sizeof(*wusb_dev), GFP_KERNEL);	if (wusb_dev == NULL)		goto err; 	wusb_dev->wusbhc = wusbhc; 	INIT_WORK(&wusb_dev->devconnect_acked_work, wusbhc_devconnect_acked_work); 	return wusb_dev;err:	wusb_dev_free(wusb_dev);	return NULL;}  /* * Using the Connect-Ack list, fill out the @wusbhc Connect-Ack WUSB IE * properly so that it can be added to the MMC. * * We just get the @wusbhc->ca_list and fill out the first four ones or * less (per-spec WUSB1.0[7.5, before T7-38). If the ConnectAck WUSB * IE is not allocated, we alloc it. * * @wusbhc->mutex must be taken */static void wusbhc_fill_cack_ie(struct wusbhc *wusbhc){	unsigned cnt;	struct wusb_dev *dev_itr;	struct wuie_connect_ack *cack_ie; 	cack_ie = &wusbhc->cack_ie;	cnt = 0;	list_for_each_entry(dev_itr, &wusbhc->cack_list, cack_node) {		cack_ie->blk[cnt].CDID = dev_itr->cdid;		cack_ie->blk[cnt].bDeviceAddress = dev_itr->addr;		if (++cnt >= WUIE_ELT_MAX)			break;	}	cack_ie->hdr.bLength = sizeof(cack_ie->hdr)		+ cnt * sizeof(cack_ie->blk[0]);} /* * Register a new device that wants to connect * * A new device wants to connect, so we add it to the Connect-Ack * list. We give it an address in the unauthorized range (bit 8 set); * user space will have to drive authorization further on. * * @dev_addr: address to use for the device (which is also the port *            number). * * @wusbhc->mutex must be taken */static struct wusb_dev *wusbhc_cack_add(struct wusbhc *wusbhc,					struct wusb_dn_connect *dnc,					const char *pr_cdid, u8 port_idx){	struct device *dev = wusbhc->dev;	struct wusb_dev *wusb_dev;	int new_connection = wusb_dn_connect_new_connection(dnc);	u8 dev_addr;	int result; 	/* Is it registered already? */	list_for_each_entry(wusb_dev, &wusbhc->cack_list, cack_node)		if (!memcmp(&wusb_dev->cdid, &dnc->CDID,			    sizeof(wusb_dev->cdid)))			return wusb_dev;	/* We don't have it, create an entry, register it */	wusb_dev = wusb_dev_alloc(wusbhc);	if (wusb_dev == NULL)		return NULL;	wusb_dev_init(wusb_dev);	wusb_dev->cdid = dnc->CDID;	wusb_dev->port_idx = port_idx; 	/*	 * Devices are always available within the cluster reservation	 * and since the hardware will take the intersection of the	 * per-device availability and the cluster reservation, the	 * per-device availability can simply be set to always	 * available.	 */	bitmap_fill(wusb_dev->availability.bm, UWB_NUM_MAS); 	/* FIXME: handle reconnects instead of assuming connects are	   always new. */	if (1 && new_connection == 0)		new_connection = 1;	if (new_connection) {		dev_addr = (port_idx + 2) | WUSB_DEV_ADDR_UNAUTH; 		dev_info(dev, "Connecting new WUSB device to address %u, "			"port %u\n", dev_addr, port_idx); 		result = wusb_set_dev_addr(wusbhc, wusb_dev, dev_addr);		if (result < 0)			return NULL;	}	wusb_dev->entry_ts = jiffies;	list_add_tail(&wusb_dev->cack_node, &wusbhc->cack_list);	wusbhc->cack_count++;	wusbhc_fill_cack_ie(wusbhc); 	return wusb_dev;} /* * Remove a Connect-Ack context entry from the HCs view * * @wusbhc->mutex must be taken */static void wusbhc_cack_rm(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev){	list_del_init(&wusb_dev->cack_node);	wusbhc->cack_count--;	wusbhc_fill_cack_ie(wusbhc);} /* * @wusbhc->mutex must be taken */staticvoid wusbhc_devconnect_acked(struct wusbhc *wusbhc, struct wusb_dev *wusb_dev){	wusbhc_cack_rm(wusbhc, wusb_dev);	if (wusbhc->cack_count)		wusbhc_mmcie_set(wusbhc, 0, 0, &wusbhc->cack_ie.hdr);	else		wusbhc_mmcie_rm(wusbhc, &wusbhc->cack_ie.hdr);} static void wusbhc_devconnect_acked_work(struct work_struct *work){	struct wusb_dev *wusb_dev = container_of(work, struct wusb_dev,						 devconnect_acked_work);	struct wusbhc *wusbhc = wusb_dev->wusbhc; 	mutex_lock(&wusbhc->mutex);	wusbhc_devconnect_acked(wusbhc, wusb_dev);	mutex_unlock(&wusbhc->mutex); 	wusb_dev_put(wusb_dev);} /* * Ack a device for connection * * FIXME: docs * * @pr_cdid:	Printable CDID...hex Use @dnc->cdid for the real deal. * * So we get the connect ack IE (may have been allocated already), * find an empty connect block, an empty virtual port, create an * address with it (see below), make it an unauth addr [bit 7 set] and * set the MMC. * * Addresses: because WUSB hosts have no downstream hubs, we can do a *            1:1 mapping between 'port number' and device *            address. This simplifies many things, as during this *            initial connect phase the USB stack has no knoledge of *            the device and hasn't assigned an address yet--we know *            USB's choose_address() will use the same euristics we *            use here, so we can assume which address will be assigned. * *            USB stack always assigns address 1 to the root hub, so *            to the port number we add 2 (thus virtual port #0 is *            addr #2). * * @wusbhc shall be referenced */staticvoid wusbhc_devconnect_ack(struct wusbhc *wusbhc, struct wusb_dn_connect *dnc,			   const char *pr_cdid){	int result;	struct device *dev = wusbhc->dev;	struct wusb_dev *wusb_dev;	struct wusb_port *port;	unsigned idx, devnum; 	mutex_lock(&wusbhc->mutex); 	/* Check we are not handling it already */	for (idx = 0; idx < wusbhc->ports_max; idx++) {		port = wusb_port_by_idx(wusbhc, idx);		if (port->wusb_dev		    && memcmp(&dnc->CDID, &port->wusb_dev->cdid, sizeof(dnc->CDID)) == 0)			goto error_unlock;	}	/* Look up those fake ports we have for a free one */	for (idx = 0; idx < wusbhc->ports_max; idx++) {		port = wusb_port_by_idx(wusbhc, idx);		if ((port->status & USB_PORT_STAT_POWER)		    && !(port->status & USB_PORT_STAT_CONNECTION))			break;	}	if (idx >= wusbhc->ports_max) {		dev_err(dev, "Host controller can't connect more devices "			"(%u already connected); device %s rejected\n",			wusbhc->ports_max, pr_cdid);		/* NOTE: we could send a WUIE_Disconnect here, but we haven't		 *       event acked, so the device will eventually timeout the		 *       connection, right? */		goto error_unlock;	} 	devnum = idx + 2; 	/* Make sure we are using no crypto on that "virtual port" */	wusbhc->set_ptk(wusbhc, idx, 0, NULL, 0); 	/* Grab a filled in Connect-Ack context, fill out the	 * Connect-Ack Wireless USB IE, set the MMC */	wusb_dev = wusbhc_cack_add(wusbhc, dnc, pr_cdid, idx);	if (wusb_dev == NULL)		goto error_unlock;	result = wusbhc_mmcie_set(wusbhc, 0, 0, &wusbhc->cack_ie.hdr);	if (result < 0)		goto error_unlock;	/* Give the device at least 2ms (WUSB1.0[7.5.1p3]), let's do	 * three for a good measure */	msleep(3);	port->wusb_dev = wusb_dev;	port->status |= USB_PORT_STAT_CONNECTION;	port->change |= USB_PORT_STAT_C_CONNECTION;	/* Now the port status changed to connected; khubd will	 * pick the change up and try to reset the port to bring it to	 * the enabled state--so this process returns up to the stack	 * and it calls back into wusbhc_rh_port_reset().	 */error_unlock:	mutex_unlock(&wusbhc->mutex);	return; } /* * Disconnect a Wireless USB device from its fake port * * Marks the port as disconnected so that khubd can pick up the change * and drops our knowledge about the device. * * Assumes there is a device connected * * @port_index: zero based port number * * NOTE: @wusbhc->mutex is locked * * WARNING: From here it is not very safe to access anything hanging off *	    wusb_dev */static void __wusbhc_dev_disconnect(struct wusbhc *wusbhc,				    struct wusb_port *port){	struct wusb_dev *wusb_dev = port->wusb_dev; 	port->status &= ~(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE			  | USB_PORT_STAT_SUSPEND | USB_PORT_STAT_RESET			  | USB_PORT_STAT_LOW_SPEED | USB_PORT_STAT_HIGH_SPEED);	port->change |= USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE;	if (wusb_dev) {		dev_dbg(wusbhc->dev, "disconnecting device from port %d\n", wusb_dev->port_idx);		if (!list_empty(&wusb_dev->cack_node))			list_del_init(&wusb_dev->cack_node);		/* For the one in cack_add() */		wusb_dev_put(wusb_dev);	}	port->wusb_dev = NULL; 	/* After a device disconnects, change the GTK (see [WUSB]	 * section 6.2.11.2). */	if (wusbhc->active)		wusbhc_gtk_rekey(wusbhc); 	/* The Wireless USB part has forgotten about the device already; now	 * khubd's timer will pick up the disconnection and remove the USB	 * device from the system	 */} /* * Refresh the list of keep alives to emit in the MMC * * We only publish the first four devices that have a coming timeout * condition. Then when we are done processing those, we go for the * next ones. We ignore the ones that have timed out already (they'll * be purged). * * This might cause the first devices to timeout the last devices in * the port array...FIXME: come up with a better algorithm? * * Note we can't do much about MMC's ops errors; we hope next refresh * will kind of handle it. * * NOTE: @wusbhc->mutex is locked */static void __wusbhc_keep_alive(struct wusbhc *wusbhc){	struct device *dev = wusbhc->dev;	unsigned cnt;	struct wusb_dev *wusb_dev;	struct wusb_port *wusb_port;	struct wuie_keep_alive *ie = &wusbhc->keep_alive_ie;	unsigned keep_alives, old_keep_alives; 	old_keep_alives = ie->hdr.bLength - sizeof(ie->hdr);	keep_alives = 0;	for (cnt = 0;	     keep_alives < WUIE_ELT_MAX && cnt < wusbhc->ports_max;	     cnt++) {		unsigned tt = msecs_to_jiffies(wusbhc->trust_timeout); 		wusb_port = wusb_port_by_idx(wusbhc, cnt);		wusb_dev = wusb_port->wusb_dev; 		if (wusb_dev == NULL)			continue;		if (wusb_dev->usb_dev == NULL)			continue; 		if (time_after(jiffies, wusb_dev->entry_ts + tt)) {			dev_err(dev, "KEEPALIVE: device %u timed out\n",				wusb_dev->addr);			__wusbhc_dev_disconnect(wusbhc, wusb_port);		} else if (time_after(jiffies, wusb_dev->entry_ts + tt/3)) {			/* Approaching timeout cut off, need to refresh */			ie->bDeviceAddress[keep_alives++] = wusb_dev->addr;		}	}	if (keep_alives & 0x1)	/* pad to even number ([WUSB] section 7.5.9) */		ie->bDeviceAddress[keep_alives++] = 0x7f;	ie->hdr.bLength = sizeof(ie->hdr) +		keep_alives*sizeof(ie->bDeviceAddress[0]);	if (keep_alives > 0)		wusbhc_mmcie_set(wusbhc, 10, 5, &ie->hdr);	else if (old_keep_alives != 0)		wusbhc_mmcie_rm(wusbhc, &ie->hdr);} /* * Do a run through all devices checking for timeouts */static void wusbhc_keep_alive_run(struct work_struct *ws){	struct delayed_work *dw = to_delayed_work(ws);	struct wusbhc *wusbhc =	container_of(dw, struct wusbhc, keep_alive_timer); 	mutex_lock(&wusbhc->mutex);	__wusbhc_keep_alive(wusbhc);	mutex_unlock(&wusbhc->mutex); 	queue_delayed_work(wusbd, &wusbhc->keep_alive_timer,			   msecs_to_jiffies(wusbhc->trust_timeout / 2));} /* * Find the wusb_dev from its device address. * * The device can be found directly from the address (see * wusb_cack_add() for where the device address is set to port_idx * +2), except when the address is zero. */static struct wusb_dev *wusbhc_find_dev_by_addr(struct wusbhc *wusbhc, u8 addr){	int p; 	if (addr == 0xff) /* unconnected */		return NULL; 	if (addr > 0) {		int port = (addr & ~0x80) - 2;		if (port < 0 || port >= wusbhc->ports_max)			return NULL;		return wusb_port_by_idx(wusbhc, port)->wusb_dev;	} 	/* Look for the device with address 0. */	for (p = 0; p < wusbhc->ports_max; p++) {		struct wusb_dev *wusb_dev = wusb_port_by_idx(wusbhc, p)->wusb_dev;		if (wusb_dev && wusb_dev->addr == addr)			return wusb_dev;	}	return NULL;} /* * Handle a DN_Alive notification (WUSB1.0[7.6.1]) * * This just updates the device activity timestamp and then refreshes * the keep alive IE. * * @wusbhc shall be referenced and unlocked

Lines 1–500 of 1102.