ia64/linux-2.6.18-xen.hg

diff drivers/xen/usbback/interface.c @ 829:f799db0570f2

PVUSB: backend driver

Signed-off-by: Noboru Iwamatsu <n_iwamatsu@jp.fujitsu.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Mar 18 11:43:24 2009 +0000 (2009-03-18)
parents
children 4c7eb2e71e9d
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/drivers/xen/usbback/interface.c	Wed Mar 18 11:43:24 2009 +0000
     1.3 @@ -0,0 +1,208 @@
     1.4 +/*
     1.5 + * interface.c
     1.6 + *
     1.7 + * Xen USB backend interface management.
     1.8 + *
     1.9 + * Copyright (C) 2009, FUJITSU LABORATORIES LTD.
    1.10 + * Author: Noboru Iwamatsu <n_iwamatsu@jp.fujitsu.com>
    1.11 + *
    1.12 + * This program is free software; you can redistribute it and/or modify
    1.13 + * it under the terms of the GNU General Public License as published by
    1.14 + * the Free Software Foundation; either version 2 of the License, or
    1.15 + * (at your option) any later version.
    1.16 + *
    1.17 + * This program is distributed in the hope that it will be useful,
    1.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.20 + * GNU General Public License for more details.
    1.21 + *
    1.22 + * You should have received a copy of the GNU General Public License
    1.23 + * along with this program; if not, see <http://www.gnu.org/licenses/>.
    1.24 + *
    1.25 + * or,
    1.26 + *
    1.27 + * When distributed separately from the Linux kernel or incorporated into
    1.28 + * other software packages, subject to the following license:
    1.29 + *
    1.30 + * Permission is hereby granted, free of charge, to any person obtaining a copy
    1.31 + * of this software and associated documentation files (the "Software"), to
    1.32 + * deal in the Software without restriction, including without limitation the
    1.33 + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    1.34 + * sell copies of the Software, and to permit persons to whom the Software is
    1.35 + * furnished to do so, subject to the following conditions:
    1.36 + *
    1.37 + * The above copyright notice and this permission notice shall be included in
    1.38 + * all copies or substantial portions of the Software.
    1.39 + *
    1.40 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    1.41 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    1.42 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    1.43 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    1.44 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    1.45 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    1.46 + * DEALINGS IN THE SOFTWARE.
    1.47 + */
    1.48 +
    1.49 +#include "usbback.h"
    1.50 +
    1.51 +static LIST_HEAD(usbif_list);
    1.52 +static DEFINE_SPINLOCK(usbif_list_lock);
    1.53 +
    1.54 +usbif_t *find_usbif(int dom_id, int dev_id)
    1.55 +{
    1.56 +	usbif_t *usbif;
    1.57 +	int found = 0;
    1.58 +	unsigned long flags;
    1.59 +
    1.60 +	spin_lock_irqsave(&usbif_list_lock, flags);
    1.61 +	list_for_each_entry(usbif, &usbif_list, usbif_list) {
    1.62 +		if (usbif->domid == dom_id
    1.63 +			&& usbif->handle == dev_id) {
    1.64 +			found = 1;
    1.65 +			break;
    1.66 +		}
    1.67 +	}
    1.68 +	spin_unlock_irqrestore(&usbif_list_lock, flags);
    1.69 +
    1.70 +	if (found)
    1.71 +		return usbif;
    1.72 +
    1.73 +	return NULL;
    1.74 +}
    1.75 +
    1.76 +usbif_t *usbif_alloc(domid_t domid, unsigned int handle)
    1.77 +{
    1.78 +	usbif_t *usbif;
    1.79 +	unsigned long flags;
    1.80 +	int i;
    1.81 +
    1.82 +	usbif = kzalloc(sizeof(usbif_t), GFP_KERNEL);
    1.83 +	if (!usbif)
    1.84 +		return NULL;
    1.85 +
    1.86 +	usbif->domid = domid;
    1.87 +	usbif->handle = handle;
    1.88 +	spin_lock_init(&usbif->ring_lock);
    1.89 +	atomic_set(&usbif->refcnt, 0);
    1.90 +	init_waitqueue_head(&usbif->wq);
    1.91 +	init_waitqueue_head(&usbif->waiting_to_free);
    1.92 +	spin_lock_init(&usbif->plug_lock);
    1.93 +	INIT_LIST_HEAD(&usbif->plugged_devices);
    1.94 +	spin_lock_init(&usbif->addr_lock);
    1.95 +	for (i = 0; i < USB_DEV_ADDR_SIZE; i++) {
    1.96 +		usbif->addr_table[i] = NULL;
    1.97 +	}
    1.98 +
    1.99 +	spin_lock_irqsave(&usbif_list_lock, flags);
   1.100 +	list_add(&usbif->usbif_list, &usbif_list);
   1.101 +	spin_unlock_irqrestore(&usbif_list_lock, flags);
   1.102 +
   1.103 +	return usbif;
   1.104 +}
   1.105 +
   1.106 +static int map_frontend_page(usbif_t *usbif, unsigned long shared_page)
   1.107 +{
   1.108 +	struct gnttab_map_grant_ref op;
   1.109 +
   1.110 +	gnttab_set_map_op(&op, (unsigned long)usbif->ring_area->addr,
   1.111 +			  GNTMAP_host_map, shared_page, usbif->domid);
   1.112 +
   1.113 +	if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
   1.114 +		BUG();
   1.115 +
   1.116 +	if (op.status) {
   1.117 +		printk(KERN_ERR "grant table operation failure\n");
   1.118 +		return op.status;
   1.119 +	}
   1.120 +
   1.121 +	usbif->shmem_ref = shared_page;
   1.122 +	usbif->shmem_handle = op.handle;
   1.123 +
   1.124 +	return 0;
   1.125 +}
   1.126 +
   1.127 +static void unmap_frontend_page(usbif_t *usbif)
   1.128 +{
   1.129 +	struct gnttab_unmap_grant_ref op;
   1.130 +
   1.131 +	gnttab_set_unmap_op(&op, (unsigned long)usbif->ring_area->addr,
   1.132 +			    GNTMAP_host_map, usbif->shmem_handle);
   1.133 +
   1.134 +	if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
   1.135 +		BUG();
   1.136 +}
   1.137 +
   1.138 +int usbif_map(usbif_t *usbif, unsigned long shared_page, unsigned int evtchn)
   1.139 +{
   1.140 +	int err;
   1.141 +	usbif_sring_t *sring;
   1.142 +
   1.143 +	if (usbif->irq)
   1.144 +		return 0;
   1.145 +
   1.146 +	if ((usbif->ring_area = alloc_vm_area(PAGE_SIZE)) == NULL)
   1.147 +		return -ENOMEM;
   1.148 +
   1.149 +	err = map_frontend_page(usbif, shared_page);
   1.150 +	if (err) {
   1.151 +		free_vm_area(usbif->ring_area);
   1.152 +		return err;
   1.153 +	}
   1.154 +
   1.155 +	sring = (usbif_sring_t *) usbif->ring_area->addr;
   1.156 +	BACK_RING_INIT(&usbif->ring, sring, PAGE_SIZE);
   1.157 +
   1.158 +	err = bind_interdomain_evtchn_to_irqhandler(
   1.159 +			usbif->domid, evtchn, usbbk_be_int, 0, "usbif-backend", usbif);
   1.160 +	if (err < 0)
   1.161 +	{
   1.162 +		unmap_frontend_page(usbif);
   1.163 +		free_vm_area(usbif->ring_area);
   1.164 +		usbif->ring.sring = NULL;
   1.165 +		return err;
   1.166 +	}
   1.167 +	usbif->irq = err;
   1.168 +
   1.169 +	return 0;
   1.170 +}
   1.171 +
   1.172 +void usbif_disconnect(usbif_t *usbif)
   1.173 +{
   1.174 +	struct usbstub *stub, *tmp;
   1.175 +	unsigned long flags;
   1.176 +
   1.177 +	if (usbif->xenusbd) {
   1.178 +		kthread_stop(usbif->xenusbd);
   1.179 +		usbif->xenusbd = NULL;
   1.180 +	}
   1.181 +
   1.182 +	spin_lock_irqsave(&usbif->plug_lock, flags);
   1.183 +	list_for_each_entry_safe(stub, tmp, &usbif->plugged_devices, plugged_list) {
   1.184 +		usbbk_unlink_urbs(stub);
   1.185 +		detach_device_without_lock(usbif, stub);
   1.186 +	}
   1.187 +	spin_unlock_irqrestore(&usbif->plug_lock, flags);
   1.188 +
   1.189 +	wait_event(usbif->waiting_to_free, atomic_read(&usbif->refcnt) == 0);
   1.190 +
   1.191 +	if (usbif->irq) {
   1.192 +		unbind_from_irqhandler(usbif->irq, usbif);
   1.193 +		usbif->irq = 0;
   1.194 +	}
   1.195 +
   1.196 +	if (usbif->ring.sring) {
   1.197 +		unmap_frontend_page(usbif);
   1.198 +		free_vm_area(usbif->ring_area);
   1.199 +		usbif->ring.sring = NULL;
   1.200 +	}
   1.201 +}
   1.202 +
   1.203 +void usbif_free(usbif_t *usbif)
   1.204 +{
   1.205 +	unsigned long flags;
   1.206 +
   1.207 +	spin_lock_irqsave(&usbif_list_lock, flags);
   1.208 +	list_del(&usbif->usbif_list);
   1.209 +	spin_unlock_irqrestore(&usbif_list_lock, flags);
   1.210 +	kfree(usbif);
   1.211 +}