iso.c 9.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * IEEE 1394 for Linux
 *
 * kernel ISO transmission/reception
 *
 * Copyright (C) 2002 Maas Digital LLC
 *
 * This code is licensed under the GPL.  See the file COPYING in the root
 * directory of the kernel sources for details.
 */

#include <linux/slab.h>
Ben Collins's avatar
Ben Collins committed
13
#include <linux/sched.h>
14 15 16 17
#include "iso.h"

void hpsb_iso_stop(struct hpsb_iso *iso)
{
18
	if (!(iso->flags & HPSB_ISO_DRIVER_STARTED))
19 20
		return;

Ben Collins's avatar
Ben Collins committed
21 22
	iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
				  XMIT_STOP : RECV_STOP, 0);
23 24 25 26 27 28 29
	iso->flags &= ~HPSB_ISO_DRIVER_STARTED;
}

void hpsb_iso_shutdown(struct hpsb_iso *iso)
{
	if(iso->flags & HPSB_ISO_DRIVER_INIT) {
		hpsb_iso_stop(iso);
Ben Collins's avatar
Ben Collins committed
30 31
		iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
					  XMIT_SHUTDOWN : RECV_SHUTDOWN, 0);
32 33
		iso->flags &= ~HPSB_ISO_DRIVER_INIT;
	}
Ben Collins's avatar
Ben Collins committed
34 35

	dma_region_free(&iso->data_buf);
36 37 38 39
	kfree(iso);
}

static struct hpsb_iso* hpsb_iso_common_init(struct hpsb_host *host, enum hpsb_iso_type type,
Ben Collins's avatar
Ben Collins committed
40
					     unsigned int data_buf_size,
41 42 43 44 45 46 47
					     unsigned int buf_packets,
					     int channel,
					     int irq_interval,
					     void (*callback)(struct hpsb_iso*))
{
	struct hpsb_iso *iso;
	int dma_direction;
Ben Collins's avatar
Ben Collins committed
48

49
	/* make sure driver supports the ISO API */
Ben Collins's avatar
Ben Collins committed
50 51 52
	if(!host->driver->isoctl) {
		printk(KERN_INFO "ieee1394: host driver '%s' does not support the rawiso API\n",
		       host->driver->name);
53
		return NULL;
Ben Collins's avatar
Ben Collins committed
54
	}
55 56

	/* sanitize parameters */
Ben Collins's avatar
Ben Collins committed
57

58 59
	if(buf_packets < 2)
		buf_packets = 2;
Ben Collins's avatar
Ben Collins committed
60

61 62
	if(irq_interval < 1 || irq_interval > buf_packets / 2)
		irq_interval = buf_packets / 2;
Ben Collins's avatar
Ben Collins committed
63 64 65 66 67 68

	if(channel < -1 || channel >= 64)
		return NULL;

	/* channel = -1 is OK for multi-channel recv but not for xmit */
	if(type == HPSB_ISO_XMIT && channel < 0)
69 70 71
		return NULL;

	/* allocate and write the struct hpsb_iso */
Ben Collins's avatar
Ben Collins committed
72

Ben Collins's avatar
Ben Collins committed
73
	iso = kmalloc(sizeof(*iso) + buf_packets * sizeof(struct hpsb_iso_packet_info), GFP_KERNEL);
74 75
	if(!iso)
		return NULL;
Ben Collins's avatar
Ben Collins committed
76

Ben Collins's avatar
Ben Collins committed
77
	iso->infos = (struct hpsb_iso_packet_info *)(iso + 1);
Ben Collins's avatar
Ben Collins committed
78

79 80 81 82
	iso->type = type;
	iso->host = host;
	iso->hostdata = NULL;
	iso->callback = callback;
Ben Collins's avatar
Ben Collins committed
83
	init_waitqueue_head(&iso->waitq);
84 85
	iso->channel = channel;
	iso->irq_interval = irq_interval;
Ben Collins's avatar
Ben Collins committed
86 87
	dma_region_init(&iso->data_buf);
	iso->buf_size = round_up_to_page(data_buf_size);
88
	iso->buf_packets = buf_packets;
Ben Collins's avatar
Ben Collins committed
89
	iso->pkt_dma = 0;
90
	iso->first_packet = 0;
Ben Collins's avatar
Ben Collins committed
91
	spin_lock_init(&iso->lock);
92 93

	if(iso->type == HPSB_ISO_XMIT) {
Ben Collins's avatar
Ben Collins committed
94
		iso->n_ready_packets = iso->buf_packets;
95 96
		dma_direction = PCI_DMA_TODEVICE;
	} else {
Ben Collins's avatar
Ben Collins committed
97
		iso->n_ready_packets = 0;
98 99
		dma_direction = PCI_DMA_FROMDEVICE;
	}
Ben Collins's avatar
Ben Collins committed
100

101 102 103
	atomic_set(&iso->overflows, 0);
	iso->flags = 0;
	iso->prebuffer = 0;
Ben Collins's avatar
Ben Collins committed
104

105
	/* allocate the packet buffer */
Ben Collins's avatar
Ben Collins committed
106
	if(dma_region_alloc(&iso->data_buf, iso->buf_size, host->pdev, dma_direction))
107 108 109 110 111 112 113 114 115 116 117
		goto err;

	return iso;

err:
	hpsb_iso_shutdown(iso);
	return NULL;
}

int hpsb_iso_n_ready(struct hpsb_iso* iso)
{
Ben Collins's avatar
Ben Collins committed
118 119 120 121 122 123 124 125
	unsigned long flags;
	int val;

	spin_lock_irqsave(&iso->lock, flags);
	val = iso->n_ready_packets;
	spin_unlock_irqrestore(&iso->lock, flags);

	return val;
126
}
Ben Collins's avatar
Ben Collins committed
127

128 129

struct hpsb_iso* hpsb_iso_xmit_init(struct hpsb_host *host,
Ben Collins's avatar
Ben Collins committed
130
				    unsigned int data_buf_size,
131 132 133 134 135 136 137
				    unsigned int buf_packets,
				    int channel,
				    int speed,
				    int irq_interval,
				    void (*callback)(struct hpsb_iso*))
{
	struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT,
Ben Collins's avatar
Ben Collins committed
138
						    data_buf_size, buf_packets,
139 140 141 142 143
						    channel, irq_interval, callback);
	if(!iso)
		return NULL;

	iso->speed = speed;
Ben Collins's avatar
Ben Collins committed
144

145 146 147 148 149 150 151 152 153 154 155 156 157
	/* tell the driver to start working */
	if(host->driver->isoctl(iso, XMIT_INIT, 0))
		goto err;

	iso->flags |= HPSB_ISO_DRIVER_INIT;
	return iso;

err:
	hpsb_iso_shutdown(iso);
	return NULL;
}

struct hpsb_iso* hpsb_iso_recv_init(struct hpsb_host *host,
Ben Collins's avatar
Ben Collins committed
158
				    unsigned int data_buf_size,
159 160 161 162 163 164
				    unsigned int buf_packets,
				    int channel,
				    int irq_interval,
				    void (*callback)(struct hpsb_iso*))
{
	struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV,
Ben Collins's avatar
Ben Collins committed
165
						    data_buf_size, buf_packets,
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
						    channel, irq_interval, callback);
	if(!iso)
		return NULL;

	/* tell the driver to start working */
	if(host->driver->isoctl(iso, RECV_INIT, 0))
		goto err;

	iso->flags |= HPSB_ISO_DRIVER_INIT;
	return iso;

err:
	hpsb_iso_shutdown(iso);
	return NULL;
}

Ben Collins's avatar
Ben Collins committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel)
{
	if(iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
		return -EINVAL;
	return iso->host->driver->isoctl(iso, RECV_LISTEN_CHANNEL, channel);
}

int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel)
{
       if(iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
               return -EINVAL;
       return iso->host->driver->isoctl(iso, RECV_UNLISTEN_CHANNEL, channel);
}

int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask)
{
	if(iso->type != HPSB_ISO_RECV || iso->channel != -1)
		return -EINVAL;
	return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK, (unsigned long) &mask);
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216
static int do_iso_xmit_start(struct hpsb_iso *iso, int cycle)
{
	int retval = iso->host->driver->isoctl(iso, XMIT_START, cycle);
	if(retval)
		return retval;

	iso->flags |= HPSB_ISO_DRIVER_STARTED;
	return retval;
}

int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer)
{
	if(iso->type != HPSB_ISO_XMIT)
		return -1;
Ben Collins's avatar
Ben Collins committed
217

218 219 220
	if(iso->flags & HPSB_ISO_DRIVER_STARTED)
		return 0;

Ben Collins's avatar
Ben Collins committed
221 222 223 224 225 226 227 228 229 230
	if(cycle < -1)
		cycle = -1;
	else if(cycle >= 8000)
		cycle %= 8000;

	iso->xmit_cycle = cycle;

	if(prebuffer < 0)
		prebuffer = iso->buf_packets;
	else if(prebuffer == 0)
231 232 233 234 235 236 237
		prebuffer = 1;

	if(prebuffer > iso->buf_packets)
		prebuffer = iso->buf_packets;

	iso->prebuffer = prebuffer;

Ben Collins's avatar
Ben Collins committed
238 239
	/* remember the starting cycle; DMA will commence from xmit_queue_packets()
	   once enough packets have been buffered */
240 241 242 243 244
	iso->start_cycle = cycle;

	return 0;
}

Ben Collins's avatar
Ben Collins committed
245
int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync)
246 247
{
	int retval = 0;
Ben Collins's avatar
Ben Collins committed
248
	int isoctl_args[3];
249 250 251

	if(iso->type != HPSB_ISO_RECV)
		return -1;
Ben Collins's avatar
Ben Collins committed
252

253 254 255
	if(iso->flags & HPSB_ISO_DRIVER_STARTED)
		return 0;

Ben Collins's avatar
Ben Collins committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	if(cycle < -1)
		cycle = -1;
	else if(cycle >= 8000)
		cycle %= 8000;

	isoctl_args[0] = cycle;
	
	if(tag_mask < 0)
		/* match all tags */
		tag_mask = 0xF;
	isoctl_args[1] = tag_mask;

	isoctl_args[2] = sync;

	retval = iso->host->driver->isoctl(iso, RECV_START, (unsigned long) &isoctl_args[0]);
271 272 273 274 275 276 277
	if(retval)
		return retval;

	iso->flags |= HPSB_ISO_DRIVER_STARTED;
	return retval;
}

Ben Collins's avatar
Ben Collins committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
/* check to make sure the user has not supplied bogus values of offset/len
   that would cause the kernel to access memory outside the buffer */

static int hpsb_iso_check_offset_len(struct hpsb_iso *iso,
				     unsigned int offset, unsigned short len,
				     unsigned int *out_offset, unsigned short *out_len)
{
	if(offset >= iso->buf_size)
		return -EFAULT;

	/* make sure the packet does not go beyond the end of the buffer */
	if(offset + len > iso->buf_size)
		return -EFAULT;

	/* check for wrap-around */
	if(offset + len < offset)
		return -EFAULT;

	/* now we can trust 'offset' and 'length' */
	*out_offset = offset;
	*out_len = len;

	return 0;
}


int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len, u8 tag, u8 sy)
305
{
Ben Collins's avatar
Ben Collins committed
306 307 308
	struct hpsb_iso_packet_info *info;
	unsigned long flags;
	int rv;
309 310

	if(iso->type != HPSB_ISO_XMIT)
Ben Collins's avatar
Ben Collins committed
311 312 313 314 315
		return -EINVAL;

	/* is there space in the buffer? */
	if(iso->n_ready_packets <= 0) {
		return -EBUSY;
316 317
	}

Ben Collins's avatar
Ben Collins committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
	info = &iso->infos[iso->first_packet];

	/* check for bogus offset/length */
	if(hpsb_iso_check_offset_len(iso, offset, len, &info->offset, &info->len))
		return -EFAULT;

	info->tag = tag;
	info->sy = sy;

	spin_lock_irqsave(&iso->lock, flags);

	rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long) info);
	if(rv)
		goto out;

	/* increment cursors */
	iso->first_packet = (iso->first_packet+1) % iso->buf_packets;
	iso->xmit_cycle = (iso->xmit_cycle+1) % 8000;
	iso->n_ready_packets--;
337 338

	if(iso->prebuffer != 0) {
Ben Collins's avatar
Ben Collins committed
339
		iso->prebuffer--;
340 341
		if(iso->prebuffer <= 0) {
			iso->prebuffer = 0;
Ben Collins's avatar
Ben Collins committed
342
			rv = do_iso_xmit_start(iso, iso->start_cycle);
343 344 345
		}
	}

Ben Collins's avatar
Ben Collins committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
out:	
	spin_unlock_irqrestore(&iso->lock, flags);
	return rv;
}

int hpsb_iso_xmit_sync(struct hpsb_iso *iso)
{
	if(iso->type != HPSB_ISO_XMIT)
		return -EINVAL;

	return wait_event_interruptible(iso->waitq, hpsb_iso_n_ready(iso) == iso->buf_packets);
}

void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error)
{
	unsigned long flags;
	spin_lock_irqsave(&iso->lock, flags);

	/* predict the cycle of the next packet to be queued */

	/* jump ahead by the number of packets that are already buffered */
	cycle += iso->buf_packets - iso->n_ready_packets;
	cycle %= 8000;

	iso->xmit_cycle = cycle;
	iso->n_ready_packets++;
	iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;

	if(iso->n_ready_packets == iso->buf_packets || error != 0) {
		/* the buffer has run empty! */
		atomic_inc(&iso->overflows);
	}

	spin_unlock_irqrestore(&iso->lock, flags);
}

void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len,
			      u16 cycle, u8 channel, u8 tag, u8 sy)
{
	unsigned long flags;
	spin_lock_irqsave(&iso->lock, flags);

	if(iso->n_ready_packets == iso->buf_packets) {
		/* overflow! */
		atomic_inc(&iso->overflows);
	} else {
		struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma];
		info->offset = offset;
		info->len = len;
		info->cycle = cycle;
		info->channel = channel;
		info->tag = tag;
		info->sy = sy;

		iso->pkt_dma = (iso->pkt_dma+1) % iso->buf_packets;
		iso->n_ready_packets++;
	}

	spin_unlock_irqrestore(&iso->lock, flags);
405 406 407 408
}

int hpsb_iso_recv_release_packets(struct hpsb_iso *iso, unsigned int n_packets)
{
Ben Collins's avatar
Ben Collins committed
409 410 411 412
	unsigned long flags;
	unsigned int i;
	int rv = 0;

413 414 415
	if(iso->type != HPSB_ISO_RECV)
		return -1;

Ben Collins's avatar
Ben Collins committed
416 417 418 419 420 421 422 423 424 425 426 427
	spin_lock_irqsave(&iso->lock, flags);
	for(i = 0; i < n_packets; i++) {
		rv = iso->host->driver->isoctl(iso, RECV_RELEASE,
					       (unsigned long) &iso->infos[iso->first_packet]);
		if(rv)
			break;

		iso->first_packet = (iso->first_packet+1) % iso->buf_packets;
		iso->n_ready_packets--;
	}
	spin_unlock_irqrestore(&iso->lock, flags);
	return rv;
428 429
}

Ben Collins's avatar
Ben Collins committed
430
void hpsb_iso_wake(struct hpsb_iso *iso)
431
{
Ben Collins's avatar
Ben Collins committed
432 433 434 435
	wake_up_interruptible(&iso->waitq);

	if(iso->callback)
		iso->callback(iso);
436
}