ia64/linux-2.6.18-xen.hg

diff drivers/ieee1394/iso.c @ 0:831230e53067

Import 2.6.18 from kernel.org tarball.
author Ian Campbell <ian.campbell@xensource.com>
date Wed Apr 11 14:15:44 2007 +0100 (2007-04-11)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/drivers/ieee1394/iso.c	Wed Apr 11 14:15:44 2007 +0100
     1.3 @@ -0,0 +1,484 @@
     1.4 +/*
     1.5 + * IEEE 1394 for Linux
     1.6 + *
     1.7 + * kernel ISO transmission/reception
     1.8 + *
     1.9 + * Copyright (C) 2002 Maas Digital LLC
    1.10 + *
    1.11 + * This code is licensed under the GPL.  See the file COPYING in the root
    1.12 + * directory of the kernel sources for details.
    1.13 + */
    1.14 +
    1.15 +#include <linux/slab.h>
    1.16 +#include <linux/sched.h>
    1.17 +#include "iso.h"
    1.18 +
    1.19 +void hpsb_iso_stop(struct hpsb_iso *iso)
    1.20 +{
    1.21 +	if (!(iso->flags & HPSB_ISO_DRIVER_STARTED))
    1.22 +		return;
    1.23 +
    1.24 +	iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
    1.25 +				  XMIT_STOP : RECV_STOP, 0);
    1.26 +	iso->flags &= ~HPSB_ISO_DRIVER_STARTED;
    1.27 +}
    1.28 +
    1.29 +void hpsb_iso_shutdown(struct hpsb_iso *iso)
    1.30 +{
    1.31 +	if (iso->flags & HPSB_ISO_DRIVER_INIT) {
    1.32 +		hpsb_iso_stop(iso);
    1.33 +		iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
    1.34 +					  XMIT_SHUTDOWN : RECV_SHUTDOWN, 0);
    1.35 +		iso->flags &= ~HPSB_ISO_DRIVER_INIT;
    1.36 +	}
    1.37 +
    1.38 +	dma_region_free(&iso->data_buf);
    1.39 +	kfree(iso);
    1.40 +}
    1.41 +
    1.42 +static struct hpsb_iso *hpsb_iso_common_init(struct hpsb_host *host,
    1.43 +					     enum hpsb_iso_type type,
    1.44 +					     unsigned int data_buf_size,
    1.45 +					     unsigned int buf_packets,
    1.46 +					     int channel, int dma_mode,
    1.47 +					     int irq_interval,
    1.48 +					     void (*callback) (struct hpsb_iso
    1.49 +							       *))
    1.50 +{
    1.51 +	struct hpsb_iso *iso;
    1.52 +	int dma_direction;
    1.53 +
    1.54 +	/* make sure driver supports the ISO API */
    1.55 +	if (!host->driver->isoctl) {
    1.56 +		printk(KERN_INFO
    1.57 +		       "ieee1394: host driver '%s' does not support the rawiso API\n",
    1.58 +		       host->driver->name);
    1.59 +		return NULL;
    1.60 +	}
    1.61 +
    1.62 +	/* sanitize parameters */
    1.63 +
    1.64 +	if (buf_packets < 2)
    1.65 +		buf_packets = 2;
    1.66 +
    1.67 +	if ((dma_mode < HPSB_ISO_DMA_DEFAULT)
    1.68 +	    || (dma_mode > HPSB_ISO_DMA_PACKET_PER_BUFFER))
    1.69 +		dma_mode = HPSB_ISO_DMA_DEFAULT;
    1.70 +
    1.71 +	if ((irq_interval < 0) || (irq_interval > buf_packets / 4))
    1.72 +		irq_interval = buf_packets / 4;
    1.73 +	if (irq_interval == 0)	/* really interrupt for each packet */
    1.74 +		irq_interval = 1;
    1.75 +
    1.76 +	if (channel < -1 || channel >= 64)
    1.77 +		return NULL;
    1.78 +
    1.79 +	/* channel = -1 is OK for multi-channel recv but not for xmit */
    1.80 +	if (type == HPSB_ISO_XMIT && channel < 0)
    1.81 +		return NULL;
    1.82 +
    1.83 +	/* allocate and write the struct hpsb_iso */
    1.84 +
    1.85 +	iso =
    1.86 +	    kmalloc(sizeof(*iso) +
    1.87 +		    buf_packets * sizeof(struct hpsb_iso_packet_info),
    1.88 +		    GFP_KERNEL);
    1.89 +	if (!iso)
    1.90 +		return NULL;
    1.91 +
    1.92 +	iso->infos = (struct hpsb_iso_packet_info *)(iso + 1);
    1.93 +
    1.94 +	iso->type = type;
    1.95 +	iso->host = host;
    1.96 +	iso->hostdata = NULL;
    1.97 +	iso->callback = callback;
    1.98 +	init_waitqueue_head(&iso->waitq);
    1.99 +	iso->channel = channel;
   1.100 +	iso->irq_interval = irq_interval;
   1.101 +	iso->dma_mode = dma_mode;
   1.102 +	dma_region_init(&iso->data_buf);
   1.103 +	iso->buf_size = PAGE_ALIGN(data_buf_size);
   1.104 +	iso->buf_packets = buf_packets;
   1.105 +	iso->pkt_dma = 0;
   1.106 +	iso->first_packet = 0;
   1.107 +	spin_lock_init(&iso->lock);
   1.108 +
   1.109 +	if (iso->type == HPSB_ISO_XMIT) {
   1.110 +		iso->n_ready_packets = iso->buf_packets;
   1.111 +		dma_direction = PCI_DMA_TODEVICE;
   1.112 +	} else {
   1.113 +		iso->n_ready_packets = 0;
   1.114 +		dma_direction = PCI_DMA_FROMDEVICE;
   1.115 +	}
   1.116 +
   1.117 +	atomic_set(&iso->overflows, 0);
   1.118 +	iso->bytes_discarded = 0;
   1.119 +	iso->flags = 0;
   1.120 +	iso->prebuffer = 0;
   1.121 +
   1.122 +	/* allocate the packet buffer */
   1.123 +	if (dma_region_alloc
   1.124 +	    (&iso->data_buf, iso->buf_size, host->pdev, dma_direction))
   1.125 +		goto err;
   1.126 +
   1.127 +	return iso;
   1.128 +
   1.129 +      err:
   1.130 +	hpsb_iso_shutdown(iso);
   1.131 +	return NULL;
   1.132 +}
   1.133 +
   1.134 +int hpsb_iso_n_ready(struct hpsb_iso *iso)
   1.135 +{
   1.136 +	unsigned long flags;
   1.137 +	int val;
   1.138 +
   1.139 +	spin_lock_irqsave(&iso->lock, flags);
   1.140 +	val = iso->n_ready_packets;
   1.141 +	spin_unlock_irqrestore(&iso->lock, flags);
   1.142 +
   1.143 +	return val;
   1.144 +}
   1.145 +
   1.146 +struct hpsb_iso *hpsb_iso_xmit_init(struct hpsb_host *host,
   1.147 +				    unsigned int data_buf_size,
   1.148 +				    unsigned int buf_packets,
   1.149 +				    int channel,
   1.150 +				    int speed,
   1.151 +				    int irq_interval,
   1.152 +				    void (*callback) (struct hpsb_iso *))
   1.153 +{
   1.154 +	struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT,
   1.155 +						    data_buf_size, buf_packets,
   1.156 +						    channel,
   1.157 +						    HPSB_ISO_DMA_DEFAULT,
   1.158 +						    irq_interval, callback);
   1.159 +	if (!iso)
   1.160 +		return NULL;
   1.161 +
   1.162 +	iso->speed = speed;
   1.163 +
   1.164 +	/* tell the driver to start working */
   1.165 +	if (host->driver->isoctl(iso, XMIT_INIT, 0))
   1.166 +		goto err;
   1.167 +
   1.168 +	iso->flags |= HPSB_ISO_DRIVER_INIT;
   1.169 +	return iso;
   1.170 +
   1.171 +      err:
   1.172 +	hpsb_iso_shutdown(iso);
   1.173 +	return NULL;
   1.174 +}
   1.175 +
   1.176 +struct hpsb_iso *hpsb_iso_recv_init(struct hpsb_host *host,
   1.177 +				    unsigned int data_buf_size,
   1.178 +				    unsigned int buf_packets,
   1.179 +				    int channel,
   1.180 +				    int dma_mode,
   1.181 +				    int irq_interval,
   1.182 +				    void (*callback) (struct hpsb_iso *))
   1.183 +{
   1.184 +	struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV,
   1.185 +						    data_buf_size, buf_packets,
   1.186 +						    channel, dma_mode,
   1.187 +						    irq_interval, callback);
   1.188 +	if (!iso)
   1.189 +		return NULL;
   1.190 +
   1.191 +	/* tell the driver to start working */
   1.192 +	if (host->driver->isoctl(iso, RECV_INIT, 0))
   1.193 +		goto err;
   1.194 +
   1.195 +	iso->flags |= HPSB_ISO_DRIVER_INIT;
   1.196 +	return iso;
   1.197 +
   1.198 +      err:
   1.199 +	hpsb_iso_shutdown(iso);
   1.200 +	return NULL;
   1.201 +}
   1.202 +
   1.203 +int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel)
   1.204 +{
   1.205 +	if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
   1.206 +		return -EINVAL;
   1.207 +	return iso->host->driver->isoctl(iso, RECV_LISTEN_CHANNEL, channel);
   1.208 +}
   1.209 +
   1.210 +int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel)
   1.211 +{
   1.212 +	if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
   1.213 +		return -EINVAL;
   1.214 +	return iso->host->driver->isoctl(iso, RECV_UNLISTEN_CHANNEL, channel);
   1.215 +}
   1.216 +
   1.217 +int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask)
   1.218 +{
   1.219 +	if (iso->type != HPSB_ISO_RECV || iso->channel != -1)
   1.220 +		return -EINVAL;
   1.221 +	return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK,
   1.222 +					 (unsigned long)&mask);
   1.223 +}
   1.224 +
   1.225 +int hpsb_iso_recv_flush(struct hpsb_iso *iso)
   1.226 +{
   1.227 +	if (iso->type != HPSB_ISO_RECV)
   1.228 +		return -EINVAL;
   1.229 +	return iso->host->driver->isoctl(iso, RECV_FLUSH, 0);
   1.230 +}
   1.231 +
   1.232 +static int do_iso_xmit_start(struct hpsb_iso *iso, int cycle)
   1.233 +{
   1.234 +	int retval = iso->host->driver->isoctl(iso, XMIT_START, cycle);
   1.235 +	if (retval)
   1.236 +		return retval;
   1.237 +
   1.238 +	iso->flags |= HPSB_ISO_DRIVER_STARTED;
   1.239 +	return retval;
   1.240 +}
   1.241 +
   1.242 +int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer)
   1.243 +{
   1.244 +	if (iso->type != HPSB_ISO_XMIT)
   1.245 +		return -1;
   1.246 +
   1.247 +	if (iso->flags & HPSB_ISO_DRIVER_STARTED)
   1.248 +		return 0;
   1.249 +
   1.250 +	if (cycle < -1)
   1.251 +		cycle = -1;
   1.252 +	else if (cycle >= 8000)
   1.253 +		cycle %= 8000;
   1.254 +
   1.255 +	iso->xmit_cycle = cycle;
   1.256 +
   1.257 +	if (prebuffer < 0)
   1.258 +		prebuffer = iso->buf_packets - 1;
   1.259 +	else if (prebuffer == 0)
   1.260 +		prebuffer = 1;
   1.261 +
   1.262 +	if (prebuffer >= iso->buf_packets)
   1.263 +		prebuffer = iso->buf_packets - 1;
   1.264 +
   1.265 +	iso->prebuffer = prebuffer;
   1.266 +
   1.267 +	/* remember the starting cycle; DMA will commence from xmit_queue_packets()
   1.268 +	   once enough packets have been buffered */
   1.269 +	iso->start_cycle = cycle;
   1.270 +
   1.271 +	return 0;
   1.272 +}
   1.273 +
   1.274 +int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync)
   1.275 +{
   1.276 +	int retval = 0;
   1.277 +	int isoctl_args[3];
   1.278 +
   1.279 +	if (iso->type != HPSB_ISO_RECV)
   1.280 +		return -1;
   1.281 +
   1.282 +	if (iso->flags & HPSB_ISO_DRIVER_STARTED)
   1.283 +		return 0;
   1.284 +
   1.285 +	if (cycle < -1)
   1.286 +		cycle = -1;
   1.287 +	else if (cycle >= 8000)
   1.288 +		cycle %= 8000;
   1.289 +
   1.290 +	isoctl_args[0] = cycle;
   1.291 +
   1.292 +	if (tag_mask < 0)
   1.293 +		/* match all tags */
   1.294 +		tag_mask = 0xF;
   1.295 +	isoctl_args[1] = tag_mask;
   1.296 +
   1.297 +	isoctl_args[2] = sync;
   1.298 +
   1.299 +	retval =
   1.300 +	    iso->host->driver->isoctl(iso, RECV_START,
   1.301 +				      (unsigned long)&isoctl_args[0]);
   1.302 +	if (retval)
   1.303 +		return retval;
   1.304 +
   1.305 +	iso->flags |= HPSB_ISO_DRIVER_STARTED;
   1.306 +	return retval;
   1.307 +}
   1.308 +
   1.309 +/* check to make sure the user has not supplied bogus values of offset/len
   1.310 +   that would cause the kernel to access memory outside the buffer */
   1.311 +
   1.312 +static int hpsb_iso_check_offset_len(struct hpsb_iso *iso,
   1.313 +				     unsigned int offset, unsigned short len,
   1.314 +				     unsigned int *out_offset,
   1.315 +				     unsigned short *out_len)
   1.316 +{
   1.317 +	if (offset >= iso->buf_size)
   1.318 +		return -EFAULT;
   1.319 +
   1.320 +	/* make sure the packet does not go beyond the end of the buffer */
   1.321 +	if (offset + len > iso->buf_size)
   1.322 +		return -EFAULT;
   1.323 +
   1.324 +	/* check for wrap-around */
   1.325 +	if (offset + len < offset)
   1.326 +		return -EFAULT;
   1.327 +
   1.328 +	/* now we can trust 'offset' and 'length' */
   1.329 +	*out_offset = offset;
   1.330 +	*out_len = len;
   1.331 +
   1.332 +	return 0;
   1.333 +}
   1.334 +
   1.335 +int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len,
   1.336 +			       u8 tag, u8 sy)
   1.337 +{
   1.338 +	struct hpsb_iso_packet_info *info;
   1.339 +	unsigned long flags;
   1.340 +	int rv;
   1.341 +
   1.342 +	if (iso->type != HPSB_ISO_XMIT)
   1.343 +		return -EINVAL;
   1.344 +
   1.345 +	/* is there space in the buffer? */
   1.346 +	if (iso->n_ready_packets <= 0) {
   1.347 +		return -EBUSY;
   1.348 +	}
   1.349 +
   1.350 +	info = &iso->infos[iso->first_packet];
   1.351 +
   1.352 +	/* check for bogus offset/length */
   1.353 +	if (hpsb_iso_check_offset_len
   1.354 +	    (iso, offset, len, &info->offset, &info->len))
   1.355 +		return -EFAULT;
   1.356 +
   1.357 +	info->tag = tag;
   1.358 +	info->sy = sy;
   1.359 +
   1.360 +	spin_lock_irqsave(&iso->lock, flags);
   1.361 +
   1.362 +	rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long)info);
   1.363 +	if (rv)
   1.364 +		goto out;
   1.365 +
   1.366 +	/* increment cursors */
   1.367 +	iso->first_packet = (iso->first_packet + 1) % iso->buf_packets;
   1.368 +	iso->xmit_cycle = (iso->xmit_cycle + 1) % 8000;
   1.369 +	iso->n_ready_packets--;
   1.370 +
   1.371 +	if (iso->prebuffer != 0) {
   1.372 +		iso->prebuffer--;
   1.373 +		if (iso->prebuffer <= 0) {
   1.374 +			iso->prebuffer = 0;
   1.375 +			rv = do_iso_xmit_start(iso, iso->start_cycle);
   1.376 +		}
   1.377 +	}
   1.378 +
   1.379 +      out:
   1.380 +	spin_unlock_irqrestore(&iso->lock, flags);
   1.381 +	return rv;
   1.382 +}
   1.383 +
   1.384 +int hpsb_iso_xmit_sync(struct hpsb_iso *iso)
   1.385 +{
   1.386 +	if (iso->type != HPSB_ISO_XMIT)
   1.387 +		return -EINVAL;
   1.388 +
   1.389 +	return wait_event_interruptible(iso->waitq,
   1.390 +					hpsb_iso_n_ready(iso) ==
   1.391 +					iso->buf_packets);
   1.392 +}
   1.393 +
   1.394 +void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error)
   1.395 +{
   1.396 +	unsigned long flags;
   1.397 +	spin_lock_irqsave(&iso->lock, flags);
   1.398 +
   1.399 +	/* predict the cycle of the next packet to be queued */
   1.400 +
   1.401 +	/* jump ahead by the number of packets that are already buffered */
   1.402 +	cycle += iso->buf_packets - iso->n_ready_packets;
   1.403 +	cycle %= 8000;
   1.404 +
   1.405 +	iso->xmit_cycle = cycle;
   1.406 +	iso->n_ready_packets++;
   1.407 +	iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
   1.408 +
   1.409 +	if (iso->n_ready_packets == iso->buf_packets || error != 0) {
   1.410 +		/* the buffer has run empty! */
   1.411 +		atomic_inc(&iso->overflows);
   1.412 +	}
   1.413 +
   1.414 +	spin_unlock_irqrestore(&iso->lock, flags);
   1.415 +}
   1.416 +
   1.417 +void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len,
   1.418 +			      u16 total_len, u16 cycle, u8 channel, u8 tag,
   1.419 +			      u8 sy)
   1.420 +{
   1.421 +	unsigned long flags;
   1.422 +	spin_lock_irqsave(&iso->lock, flags);
   1.423 +
   1.424 +	if (iso->n_ready_packets == iso->buf_packets) {
   1.425 +		/* overflow! */
   1.426 +		atomic_inc(&iso->overflows);
   1.427 +		/* Record size of this discarded packet */
   1.428 +		iso->bytes_discarded += total_len;
   1.429 +	} else {
   1.430 +		struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma];
   1.431 +		info->offset = offset;
   1.432 +		info->len = len;
   1.433 +		info->total_len = total_len;
   1.434 +		info->cycle = cycle;
   1.435 +		info->channel = channel;
   1.436 +		info->tag = tag;
   1.437 +		info->sy = sy;
   1.438 +
   1.439 +		iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
   1.440 +		iso->n_ready_packets++;
   1.441 +	}
   1.442 +
   1.443 +	spin_unlock_irqrestore(&iso->lock, flags);
   1.444 +}
   1.445 +
   1.446 +int hpsb_iso_recv_release_packets(struct hpsb_iso *iso, unsigned int n_packets)
   1.447 +{
   1.448 +	unsigned long flags;
   1.449 +	unsigned int i;
   1.450 +	int rv = 0;
   1.451 +
   1.452 +	if (iso->type != HPSB_ISO_RECV)
   1.453 +		return -1;
   1.454 +
   1.455 +	spin_lock_irqsave(&iso->lock, flags);
   1.456 +	for (i = 0; i < n_packets; i++) {
   1.457 +		rv = iso->host->driver->isoctl(iso, RECV_RELEASE,
   1.458 +					       (unsigned long)&iso->infos[iso->
   1.459 +									  first_packet]);
   1.460 +		if (rv)
   1.461 +			break;
   1.462 +
   1.463 +		iso->first_packet = (iso->first_packet + 1) % iso->buf_packets;
   1.464 +		iso->n_ready_packets--;
   1.465 +
   1.466 +		/* release memory from packets discarded when queue was full  */
   1.467 +		if (iso->n_ready_packets == 0) {	/* Release only after all prior packets handled */
   1.468 +			if (iso->bytes_discarded != 0) {
   1.469 +				struct hpsb_iso_packet_info inf;
   1.470 +				inf.total_len = iso->bytes_discarded;
   1.471 +				iso->host->driver->isoctl(iso, RECV_RELEASE,
   1.472 +							  (unsigned long)&inf);
   1.473 +				iso->bytes_discarded = 0;
   1.474 +			}
   1.475 +		}
   1.476 +	}
   1.477 +	spin_unlock_irqrestore(&iso->lock, flags);
   1.478 +	return rv;
   1.479 +}
   1.480 +
   1.481 +void hpsb_iso_wake(struct hpsb_iso *iso)
   1.482 +{
   1.483 +	wake_up_interruptible(&iso->waitq);
   1.484 +
   1.485 +	if (iso->callback)
   1.486 +		iso->callback(iso);
   1.487 +}