fw-cdev.c 28.4 KB
Newer Older
1 2
/*
 * Char device for device raw access
3
 *
4
 * Copyright (C) 2005-2007  Kristian Hoegsberg <krh@bitplanet.net>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

S
Stefan Richter 已提交
21 22 23 24 25 26
#include <linux/compat.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/firewire-cdev.h>
#include <linux/idr.h>
27
#include <linux/kernel.h>
28
#include <linux/kref.h>
S
Stefan Richter 已提交
29 30
#include <linux/mm.h>
#include <linux/module.h>
31
#include <linux/mutex.h>
32
#include <linux/poll.h>
33
#include <linux/preempt.h>
J
Jay Fenlason 已提交
34
#include <linux/spinlock.h>
S
Stefan Richter 已提交
35 36 37 38
#include <linux/time.h>
#include <linux/vmalloc.h>
#include <linux/wait.h>

39
#include <asm/system.h>
40
#include <asm/uaccess.h>
S
Stefan Richter 已提交
41

42
#include "fw-device.h"
S
Stefan Richter 已提交
43 44
#include "fw-topology.h"
#include "fw-transaction.h"
45 46

struct client {
47
	u32 version;
48
	struct fw_device *device;
49

50
	spinlock_t lock;
51 52
	bool in_shutdown;
	struct idr resource_idr;
53 54
	struct list_head event_list;
	wait_queue_head_t wait;
55
	u64 bus_reset_closure;
56

57
	struct fw_iso_context *iso_context;
58
	u64 iso_closure;
59 60
	struct fw_iso_buffer buffer;
	unsigned long vm_start;
61 62

	struct list_head link;
63
	struct kref kref;
64 65
};

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
static inline void client_get(struct client *client)
{
	kref_get(&client->kref);
}

static void client_release(struct kref *kref)
{
	struct client *client = container_of(kref, struct client, kref);

	fw_device_put(client->device);
	kfree(client);
}

static void client_put(struct client *client)
{
	kref_put(&client->kref, client_release);
}

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
struct client_resource;
typedef void (*client_resource_release_fn_t)(struct client *,
					     struct client_resource *);
struct client_resource {
	client_resource_release_fn_t release;
	int handle;
};

struct address_handler_resource {
	struct client_resource resource;
	struct fw_address_handler handler;
	__u64 closure;
	struct client *client;
};

struct outbound_transaction_resource {
	struct client_resource resource;
	struct fw_transaction transaction;
};

struct inbound_transaction_resource {
	struct client_resource resource;
	struct fw_request *request;
	void *data;
	size_t length;
};

struct descriptor_resource {
	struct client_resource resource;
	struct fw_descriptor descriptor;
	u32 data[0];
};

/*
 * dequeue_event() just kfree()'s the event, so the event has to be
 * the first field in a struct XYZ_event.
 */
struct event {
	struct { void *data; size_t size; } v[2];
	struct list_head link;
};

struct bus_reset_event {
	struct event event;
	struct fw_cdev_event_bus_reset reset;
};

struct outbound_transaction_event {
	struct event event;
	struct client *client;
	struct outbound_transaction_resource r;
	struct fw_cdev_event_response response;
};

struct inbound_transaction_event {
	struct event event;
	struct fw_cdev_event_request request;
};

struct iso_interrupt_event {
	struct event event;
	struct fw_cdev_event_iso_interrupt interrupt;
};

148
static inline void __user *u64_to_uptr(__u64 value)
149 150 151 152
{
	return (void __user *)(unsigned long)value;
}

153
static inline __u64 uptr_to_u64(void __user *ptr)
154 155 156 157 158 159 160 161 162
{
	return (__u64)(unsigned long)ptr;
}

static int fw_device_op_open(struct inode *inode, struct file *file)
{
	struct fw_device *device;
	struct client *client;

163
	device = fw_device_get_by_devt(inode->i_rdev);
164 165
	if (device == NULL)
		return -ENODEV;
166

167 168 169 170 171
	if (fw_device_is_shutdown(device)) {
		fw_device_put(device);
		return -ENODEV;
	}

172
	client = kzalloc(sizeof(*client), GFP_KERNEL);
173 174
	if (client == NULL) {
		fw_device_put(device);
175
		return -ENOMEM;
176
	}
177

178
	client->device = device;
179
	spin_lock_init(&client->lock);
180 181
	idr_init(&client->resource_idr);
	INIT_LIST_HEAD(&client->event_list);
182
	init_waitqueue_head(&client->wait);
183
	kref_init(&client->kref);
184 185 186

	file->private_data = client;

187
	mutex_lock(&device->client_list_mutex);
188
	list_add_tail(&client->link, &device->client_list);
189
	mutex_unlock(&device->client_list_mutex);
190

191 192 193 194 195 196 197 198 199 200 201 202 203 204
	return 0;
}

static void queue_event(struct client *client, struct event *event,
			void *data0, size_t size0, void *data1, size_t size1)
{
	unsigned long flags;

	event->v[0].data = data0;
	event->v[0].size = size0;
	event->v[1].data = data1;
	event->v[1].size = size1;

	spin_lock_irqsave(&client->lock, flags);
205 206 207 208
	if (client->in_shutdown)
		kfree(event);
	else
		list_add_tail(&event->link, &client->event_list);
209
	spin_unlock_irqrestore(&client->lock, flags);
210 211

	wake_up_interruptible(&client->wait);
212 213
}

214 215
static int dequeue_event(struct client *client,
			 char __user *buffer, size_t count)
216 217 218 219
{
	unsigned long flags;
	struct event *event;
	size_t size, total;
220
	int i, ret;
221

222 223 224 225 226
	ret = wait_event_interruptible(client->wait,
			!list_empty(&client->event_list) ||
			fw_device_is_shutdown(client->device));
	if (ret < 0)
		return ret;
227

228 229 230
	if (list_empty(&client->event_list) &&
		       fw_device_is_shutdown(client->device))
		return -ENODEV;
231

232
	spin_lock_irqsave(&client->lock, flags);
233
	event = list_first_entry(&client->event_list, struct event, link);
234 235 236 237 238 239
	list_del(&event->link);
	spin_unlock_irqrestore(&client->lock, flags);

	total = 0;
	for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
		size = min(event->v[i].size, count - total);
240
		if (copy_to_user(buffer + total, event->v[i].data, size)) {
241
			ret = -EFAULT;
242
			goto out;
243
		}
244 245
		total += size;
	}
246
	ret = total;
247 248 249 250

 out:
	kfree(event);

251
	return ret;
252 253
}

254 255
static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
				 size_t count, loff_t *offset)
256 257 258 259 260 261
{
	struct client *client = file->private_data;

	return dequeue_event(client, buffer, count);
}

262 263
static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
				 struct client *client)
264
{
265
	struct fw_card *card = client->device->card;
J
Jay Fenlason 已提交
266 267 268
	unsigned long flags;

	spin_lock_irqsave(&card->lock, flags);
269

270
	event->closure	     = client->bus_reset_closure;
271
	event->type          = FW_CDEV_EVENT_BUS_RESET;
272
	event->generation    = client->device->generation;
273
	event->node_id       = client->device->node_id;
274 275 276 277
	event->local_node_id = card->local_node->node_id;
	event->bm_node_id    = 0; /* FIXME: We don't track the BM. */
	event->irm_node_id   = card->irm_node->node_id;
	event->root_node_id  = card->root_node->node_id;
J
Jay Fenlason 已提交
278 279

	spin_unlock_irqrestore(&card->lock, flags);
280 281
}

282 283
static void for_each_client(struct fw_device *device,
			    void (*callback)(struct client *client))
284 285 286
{
	struct client *c;

287
	mutex_lock(&device->client_list_mutex);
288 289
	list_for_each_entry(c, &device->client_list, link)
		callback(c);
290
	mutex_unlock(&device->client_list_mutex);
291 292
}

293
static void queue_bus_reset_event(struct client *client)
294
{
295
	struct bus_reset_event *e;
296

297 298
	e = kzalloc(sizeof(*e), GFP_KERNEL);
	if (e == NULL) {
299 300 301 302
		fw_notify("Out of memory when allocating bus reset event\n");
		return;
	}

303
	fill_bus_reset_event(&e->reset, client);
304

305 306
	queue_event(client, &e->event,
		    &e->reset, sizeof(e->reset), NULL, 0);
307 308 309 310
}

void fw_device_cdev_update(struct fw_device *device)
{
311 312
	for_each_client(device, queue_bus_reset_event);
}
313

314 315 316 317
static void wake_up_client(struct client *client)
{
	wake_up_interruptible(&client->wait);
}
318

319 320 321
void fw_device_cdev_remove(struct fw_device *device)
{
	for_each_client(device, wake_up_client);
322 323
}

324
static int ioctl_get_info(struct client *client, void *buffer)
325
{
326
	struct fw_cdev_get_info *get_info = buffer;
327
	struct fw_cdev_event_bus_reset bus_reset;
328
	unsigned long ret = 0;
329

330 331
	client->version = get_info->version;
	get_info->version = FW_CDEV_VERSION;
J
Jay Fenlason 已提交
332
	get_info->card = client->device->card->index;
333

334 335
	down_read(&fw_device_rwsem);

336 337 338
	if (get_info->rom != 0) {
		void __user *uptr = u64_to_uptr(get_info->rom);
		size_t want = get_info->rom_length;
339
		size_t have = client->device->config_rom_length * 4;
340

341 342
		ret = copy_to_user(uptr, client->device->config_rom,
				   min(want, have));
343
	}
344
	get_info->rom_length = client->device->config_rom_length * 4;
345

346 347 348 349 350
	up_read(&fw_device_rwsem);

	if (ret != 0)
		return -EFAULT;

351 352 353
	client->bus_reset_closure = get_info->bus_reset_closure;
	if (get_info->bus_reset != 0) {
		void __user *uptr = u64_to_uptr(get_info->bus_reset);
354

355
		fill_bus_reset_event(&bus_reset, client);
356
		if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
357 358
			return -EFAULT;
	}
359 360 361 362

	return 0;
}

363 364
static int add_client_resource(struct client *client,
			       struct client_resource *resource, gfp_t gfp_mask)
365 366
{
	unsigned long flags;
367 368 369 370 371
	int ret;

 retry:
	if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
		return -ENOMEM;
372 373

	spin_lock_irqsave(&client->lock, flags);
374 375 376 377 378
	if (client->in_shutdown)
		ret = -ECANCELED;
	else
		ret = idr_get_new(&client->resource_idr, resource,
				  &resource->handle);
379 380
	if (ret >= 0)
		client_get(client);
381
	spin_unlock_irqrestore(&client->lock, flags);
382 383 384 385 386

	if (ret == -EAGAIN)
		goto retry;

	return ret < 0 ? ret : 0;
387 388
}

389 390 391
static int release_client_resource(struct client *client, u32 handle,
				   client_resource_release_fn_t release,
				   struct client_resource **resource)
392 393 394 395 396
{
	struct client_resource *r;
	unsigned long flags;

	spin_lock_irqsave(&client->lock, flags);
397 398 399 400 401 402
	if (client->in_shutdown)
		r = NULL;
	else
		r = idr_find(&client->resource_idr, handle);
	if (r && r->release == release)
		idr_remove(&client->resource_idr, handle);
403 404
	spin_unlock_irqrestore(&client->lock, flags);

405
	if (!(r && r->release == release))
406 407 408 409 410 411 412
		return -EINVAL;

	if (resource)
		*resource = r;
	else
		r->release(client, r);

413 414
	client_put(client);

415 416 417
	return 0;
}

418 419
static void release_transaction(struct client *client,
				struct client_resource *resource)
420
{
421 422
	struct outbound_transaction_resource *r = container_of(resource,
			struct outbound_transaction_resource, resource);
423

424
	fw_cancel_transaction(client->device->card, &r->transaction);
425 426
}

427 428
static void complete_transaction(struct fw_card *card, int rcode,
				 void *payload, size_t length, void *data)
429
{
430 431 432
	struct outbound_transaction_event *e = data;
	struct fw_cdev_event_response *rsp = &e->response;
	struct client *client = e->client;
433
	unsigned long flags;
434

435 436
	if (length < rsp->length)
		rsp->length = length;
437
	if (rcode == RCODE_COMPLETE)
438
		memcpy(rsp->data, payload, rsp->length);
439

440
	spin_lock_irqsave(&client->lock, flags);
441
	/*
442 443 444 445 446 447 448 449
	 * 1. If called while in shutdown, the idr tree must be left untouched.
	 *    The idr handle will be removed and the client reference will be
	 *    dropped later.
	 * 2. If the call chain was release_client_resource ->
	 *    release_transaction -> complete_transaction (instead of a normal
	 *    conclusion of the transaction), i.e. if this resource was already
	 *    unregistered from the idr, the client reference will be dropped
	 *    by release_client_resource and we must not drop it here.
450
	 */
451
	if (!client->in_shutdown &&
452 453
	    idr_find(&client->resource_idr, e->r.resource.handle)) {
		idr_remove(&client->resource_idr, e->r.resource.handle);
454 455 456
		/* Drop the idr's reference */
		client_put(client);
	}
457 458
	spin_unlock_irqrestore(&client->lock, flags);

459 460
	rsp->type = FW_CDEV_EVENT_RESPONSE;
	rsp->rcode = rcode;
461 462

	/*
463
	 * In the case that sizeof(*rsp) doesn't align with the position of the
464 465 466 467 468
	 * data, and the read is short, preserve an extra copy of the data
	 * to stay compatible with a pre-2.6.27 bug.  Since the bug is harmless
	 * for short reads and some apps depended on it, this is both safe
	 * and prudent for compatibility.
	 */
469 470 471
	if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
		queue_event(client, &e->event, rsp, sizeof(*rsp),
			    rsp->data, rsp->length);
472
	else
473
		queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
474
			    NULL, 0);
475 476 477

	/* Drop the transaction callback's reference */
	client_put(client);
478 479
}

J
Jeff Garzik 已提交
480
static int ioctl_send_request(struct client *client, void *buffer)
481 482
{
	struct fw_device *device = client->device;
483
	struct fw_cdev_send_request *request = buffer;
484
	struct outbound_transaction_event *e;
485
	int ret;
486 487

	/* What is the biggest size we'll accept, really? */
488
	if (request->length > 4096)
489 490
		return -EINVAL;

491 492
	e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
	if (e == NULL)
493 494
		return -ENOMEM;

495 496 497
	e->client = client;
	e->response.length = request->length;
	e->response.closure = request->closure;
498

499
	if (request->data &&
500
	    copy_from_user(e->response.data,
501
			   u64_to_uptr(request->data), request->length)) {
502
		ret = -EFAULT;
503
		goto failed;
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
	}

	switch (request->tcode) {
	case TCODE_WRITE_QUADLET_REQUEST:
	case TCODE_WRITE_BLOCK_REQUEST:
	case TCODE_READ_QUADLET_REQUEST:
	case TCODE_READ_BLOCK_REQUEST:
	case TCODE_LOCK_MASK_SWAP:
	case TCODE_LOCK_COMPARE_SWAP:
	case TCODE_LOCK_FETCH_ADD:
	case TCODE_LOCK_LITTLE_ADD:
	case TCODE_LOCK_BOUNDED_ADD:
	case TCODE_LOCK_WRAP_ADD:
	case TCODE_LOCK_VENDOR_DEPENDENT:
		break;
	default:
		ret = -EINVAL;
521
		goto failed;
522 523
	}

524 525
	e->r.resource.release = release_transaction;
	ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
526 527
	if (ret < 0)
		goto failed;
528

529 530 531
	/* Get a reference for the transaction callback */
	client_get(client);

532
	fw_send_request(device->card, &e->r.transaction,
533
			request->tcode & 0x1f,
534
			device->node->node_id,
535
			request->generation,
536
			device->max_speed,
537
			request->offset,
538 539
			e->response.data, request->length,
			complete_transaction, e);
540

541
	if (request->data)
542
		return sizeof(request) + request->length;
543
	else
544
		return sizeof(request);
545
 failed:
546
	kfree(e);
547 548

	return ret;
549 550
}

551 552
static void release_request(struct client *client,
			    struct client_resource *resource)
553
{
554 555
	struct inbound_transaction_resource *r = container_of(resource,
			struct inbound_transaction_resource, resource);
556

557
	fw_send_response(client->device->card, r->request,
558
			 RCODE_CONFLICT_ERROR);
559
	kfree(r);
560 561
}

562
static void handle_request(struct fw_card *card, struct fw_request *request,
563 564 565 566
			   int tcode, int destination, int source,
			   int generation, int speed,
			   unsigned long long offset,
			   void *payload, size_t length, void *callback_data)
567
{
568 569 570
	struct address_handler_resource *handler = callback_data;
	struct inbound_transaction_resource *r;
	struct inbound_transaction_event *e;
571
	int ret;
572

573
	r = kmalloc(sizeof(*r), GFP_ATOMIC);
574
	e = kmalloc(sizeof(*e), GFP_ATOMIC);
575
	if (r == NULL || e == NULL)
576
		goto failed;
577

578 579 580
	r->request = request;
	r->data    = payload;
	r->length  = length;
581

582 583
	r->resource.release = release_request;
	ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
584 585
	if (ret < 0)
		goto failed;
586 587 588 589 590

	e->request.type    = FW_CDEV_EVENT_REQUEST;
	e->request.tcode   = tcode;
	e->request.offset  = offset;
	e->request.length  = length;
591
	e->request.handle  = r->resource.handle;
592 593
	e->request.closure = handler->closure;

594
	queue_event(handler->client, &e->event,
595
		    &e->request, sizeof(e->request), payload, length);
596 597 598
	return;

 failed:
599
	kfree(r);
600
	kfree(e);
601
	fw_send_response(card, request, RCODE_CONFLICT_ERROR);
602 603
}

604 605
static void release_address_handler(struct client *client,
				    struct client_resource *resource)
606
{
607 608
	struct address_handler_resource *r =
	    container_of(resource, struct address_handler_resource, resource);
609

610 611
	fw_core_remove_address_handler(&r->handler);
	kfree(r);
612 613
}

614
static int ioctl_allocate(struct client *client, void *buffer)
615
{
616
	struct fw_cdev_allocate *request = buffer;
617
	struct address_handler_resource *r;
618
	struct fw_address_region region;
619
	int ret;
620

621 622
	r = kmalloc(sizeof(*r), GFP_KERNEL);
	if (r == NULL)
623 624
		return -ENOMEM;

625 626
	region.start = request->offset;
	region.end = request->offset + request->length;
627 628 629 630 631
	r->handler.length = request->length;
	r->handler.address_callback = handle_request;
	r->handler.callback_data = r;
	r->closure = request->closure;
	r->client = client;
632

633
	ret = fw_core_add_address_handler(&r->handler, &region);
634
	if (ret < 0) {
635
		kfree(r);
636
		return ret;
637 638
	}

639 640
	r->resource.release = release_address_handler;
	ret = add_client_resource(client, &r->resource, GFP_KERNEL);
641
	if (ret < 0) {
642
		release_address_handler(client, &r->resource);
643 644
		return ret;
	}
645
	request->handle = r->resource.handle;
646 647 648 649

	return 0;
}

650
static int ioctl_deallocate(struct client *client, void *buffer)
651
{
652
	struct fw_cdev_deallocate *request = buffer;
653

654 655
	return release_client_resource(client, request->handle,
				       release_address_handler, NULL);
656 657
}

658
static int ioctl_send_response(struct client *client, void *buffer)
659
{
660
	struct fw_cdev_send_response *request = buffer;
661
	struct client_resource *resource;
662
	struct inbound_transaction_resource *r;
663

664 665
	if (release_client_resource(client, request->handle,
				    release_request, &resource) < 0)
666
		return -EINVAL;
667

668 669
	r = container_of(resource, struct inbound_transaction_resource,
			 resource);
670 671 672
	if (request->length < r->length)
		r->length = request->length;
	if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
673 674
		return -EFAULT;

675
	fw_send_response(client->device->card, r->request, request->rcode);
676 677 678 679 680
	kfree(r);

	return 0;
}

681
static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
682
{
683
	struct fw_cdev_initiate_bus_reset *request = buffer;
684 685
	int short_reset;

686
	short_reset = (request->type == FW_CDEV_SHORT_RESET);
687 688 689 690

	return fw_core_initiate_bus_reset(client->device->card, short_reset);
}

691 692 693
static void release_descriptor(struct client *client,
			       struct client_resource *resource)
{
694 695
	struct descriptor_resource *r =
		container_of(resource, struct descriptor_resource, resource);
696

697 698
	fw_core_remove_descriptor(&r->descriptor);
	kfree(r);
699 700
}

701
static int ioctl_add_descriptor(struct client *client, void *buffer)
702
{
703
	struct fw_cdev_add_descriptor *request = buffer;
704
	struct descriptor_resource *r;
705
	int ret;
706

707
	if (request->length > 256)
708 709
		return -EINVAL;

710 711
	r = kmalloc(sizeof(*r) + request->length * 4, GFP_KERNEL);
	if (r == NULL)
712 713
		return -ENOMEM;

714
	if (copy_from_user(r->data,
715
			   u64_to_uptr(request->data), request->length * 4)) {
716 717
		ret = -EFAULT;
		goto failed;
718 719
	}

720 721 722 723
	r->descriptor.length    = request->length;
	r->descriptor.immediate = request->immediate;
	r->descriptor.key       = request->key;
	r->descriptor.data      = r->data;
724

725
	ret = fw_core_add_descriptor(&r->descriptor);
726 727
	if (ret < 0)
		goto failed;
728

729 730
	r->resource.release = release_descriptor;
	ret = add_client_resource(client, &r->resource, GFP_KERNEL);
731
	if (ret < 0) {
732
		fw_core_remove_descriptor(&r->descriptor);
733 734
		goto failed;
	}
735
	request->handle = r->resource.handle;
736 737

	return 0;
738
 failed:
739
	kfree(r);
740 741

	return ret;
742 743
}

744
static int ioctl_remove_descriptor(struct client *client, void *buffer)
745
{
746
	struct fw_cdev_remove_descriptor *request = buffer;
747

748 749
	return release_client_resource(client, request->handle,
				       release_descriptor, NULL);
750 751
}

752 753
static void iso_callback(struct fw_iso_context *context, u32 cycle,
			 size_t header_length, void *header, void *data)
754 755
{
	struct client *client = data;
756
	struct iso_interrupt_event *e;
757

758 759
	e = kzalloc(sizeof(*e) + header_length, GFP_ATOMIC);
	if (e == NULL)
760 761
		return;

762 763 764 765 766 767 768
	e->interrupt.type      = FW_CDEV_EVENT_ISO_INTERRUPT;
	e->interrupt.closure   = client->iso_closure;
	e->interrupt.cycle     = cycle;
	e->interrupt.header_length = header_length;
	memcpy(e->interrupt.header, header, header_length);
	queue_event(client, &e->event, &e->interrupt,
		    sizeof(e->interrupt) + header_length, NULL, 0);
769 770
}

771
static int ioctl_create_iso_context(struct client *client, void *buffer)
772
{
773
	struct fw_cdev_create_iso_context *request = buffer;
774
	struct fw_iso_context *context;
775

776 777 778 779
	/* We only support one context at this time. */
	if (client->iso_context != NULL)
		return -EBUSY;

780
	if (request->channel > 63)
781 782
		return -EINVAL;

783
	switch (request->type) {
784
	case FW_ISO_CONTEXT_RECEIVE:
785
		if (request->header_size < 4 || (request->header_size & 3))
786
			return -EINVAL;
787

788 789 790
		break;

	case FW_ISO_CONTEXT_TRANSMIT:
791
		if (request->speed > SCODE_3200)
792 793 794 795 796
			return -EINVAL;

		break;

	default:
797
		return -EINVAL;
798 799
	}

800 801 802 803 804 805 806 807 808
	context =  fw_iso_context_create(client->device->card,
					 request->type,
					 request->channel,
					 request->speed,
					 request->header_size,
					 iso_callback, client);
	if (IS_ERR(context))
		return PTR_ERR(context);

809
	client->iso_closure = request->closure;
810
	client->iso_context = context;
811

812 813 814
	/* We only support one context at this time. */
	request->handle = 0;

815 816 817
	return 0;
}

818 819 820 821
/* Macros for decoding the iso packet control header. */
#define GET_PAYLOAD_LENGTH(v)	((v) & 0xffff)
#define GET_INTERRUPT(v)	(((v) >> 16) & 0x01)
#define GET_SKIP(v)		(((v) >> 17) & 0x01)
822 823
#define GET_TAG(v)		(((v) >> 18) & 0x03)
#define GET_SY(v)		(((v) >> 20) & 0x0f)
824 825
#define GET_HEADER_LENGTH(v)	(((v) >> 24) & 0xff)

826
static int ioctl_queue_iso(struct client *client, void *buffer)
827
{
828
	struct fw_cdev_queue_iso *request = buffer;
829
	struct fw_cdev_iso_packet __user *p, *end, *next;
830
	struct fw_iso_context *ctx = client->iso_context;
831
	unsigned long payload, buffer_end, header_length;
832
	u32 control;
833 834 835 836 837 838
	int count;
	struct {
		struct fw_iso_packet packet;
		u8 header[256];
	} u;

839
	if (ctx == NULL || request->handle != 0)
840 841
		return -EINVAL;

842 843
	/*
	 * If the user passes a non-NULL data pointer, has mmap()'ed
844 845
	 * the iso buffer, and the pointer points inside the buffer,
	 * we setup the payload pointers accordingly.  Otherwise we
846
	 * set them both to 0, which will still let packets with
847 848
	 * payload_length == 0 through.  In other words, if no packets
	 * use the indirect payload, the iso buffer need not be mapped
849 850
	 * and the request->data pointer is ignored.
	 */
851

852
	payload = (unsigned long)request->data - client->vm_start;
853
	buffer_end = client->buffer.page_count << PAGE_SHIFT;
854
	if (request->data == 0 || client->buffer.pages == NULL ||
855
	    payload >= buffer_end) {
856
		payload = 0;
857
		buffer_end = 0;
858 859
	}

A
Al Viro 已提交
860 861 862
	p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);

	if (!access_ok(VERIFY_READ, p, request->size))
863 864
		return -EFAULT;

865
	end = (void __user *)p + request->size;
866 867
	count = 0;
	while (p < end) {
868
		if (get_user(control, &p->control))
869
			return -EFAULT;
870 871 872 873 874 875
		u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
		u.packet.interrupt = GET_INTERRUPT(control);
		u.packet.skip = GET_SKIP(control);
		u.packet.tag = GET_TAG(control);
		u.packet.sy = GET_SY(control);
		u.packet.header_length = GET_HEADER_LENGTH(control);
876

877
		if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
878 879
			header_length = u.packet.header_length;
		} else {
880 881 882 883
			/*
			 * We require that header_length is a multiple of
			 * the fixed header size, ctx->header_size.
			 */
884 885 886 887
			if (ctx->header_size == 0) {
				if (u.packet.header_length > 0)
					return -EINVAL;
			} else if (u.packet.header_length % ctx->header_size != 0) {
888
				return -EINVAL;
889
			}
890 891 892
			header_length = 0;
		}

893
		next = (struct fw_cdev_iso_packet __user *)
894
			&p->header[header_length / 4];
895 896 897
		if (next > end)
			return -EINVAL;
		if (__copy_from_user
898
		    (u.packet.header, p->header, header_length))
899
			return -EFAULT;
900
		if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
901 902
		    u.packet.header_length + u.packet.payload_length > 0)
			return -EINVAL;
903
		if (payload + u.packet.payload_length > buffer_end)
904 905
			return -EINVAL;

906 907
		if (fw_iso_context_queue(ctx, &u.packet,
					 &client->buffer, payload))
908 909 910 911 912 913 914
			break;

		p = next;
		payload += u.packet.payload_length;
		count++;
	}

915 916 917
	request->size    -= uptr_to_u64(p) - request->packets;
	request->packets  = uptr_to_u64(p);
	request->data     = client->vm_start + payload;
918 919 920 921

	return count;
}

922
static int ioctl_start_iso(struct client *client, void *buffer)
923
{
924
	struct fw_cdev_start_iso *request = buffer;
925

926
	if (client->iso_context == NULL || request->handle != 0)
927
		return -EINVAL;
928

929
	if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
930
		if (request->tags == 0 || request->tags > 15)
931 932
			return -EINVAL;

933
		if (request->sync > 15)
934 935 936
			return -EINVAL;
	}

937 938
	return fw_iso_context_start(client->iso_context, request->cycle,
				    request->sync, request->tags);
939 940
}

941
static int ioctl_stop_iso(struct client *client, void *buffer)
942
{
943 944
	struct fw_cdev_stop_iso *request = buffer;

945
	if (client->iso_context == NULL || request->handle != 0)
946 947
		return -EINVAL;

948 949 950
	return fw_iso_context_stop(client->iso_context);
}

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
static int ioctl_get_cycle_timer(struct client *client, void *buffer)
{
	struct fw_cdev_get_cycle_timer *request = buffer;
	struct fw_card *card = client->device->card;
	unsigned long long bus_time;
	struct timeval tv;
	unsigned long flags;

	preempt_disable();
	local_irq_save(flags);

	bus_time = card->driver->get_bus_time(card);
	do_gettimeofday(&tv);

	local_irq_restore(flags);
	preempt_enable();

	request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
	request->cycle_timer = bus_time & 0xffffffff;
	return 0;
}

973 974 975 976 977 978 979 980 981 982 983 984 985
static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
	ioctl_get_info,
	ioctl_send_request,
	ioctl_allocate,
	ioctl_deallocate,
	ioctl_send_response,
	ioctl_initiate_bus_reset,
	ioctl_add_descriptor,
	ioctl_remove_descriptor,
	ioctl_create_iso_context,
	ioctl_queue_iso,
	ioctl_start_iso,
	ioctl_stop_iso,
986
	ioctl_get_cycle_timer,
987 988
};

989 990
static int dispatch_ioctl(struct client *client,
			  unsigned int cmd, void __user *arg)
991
{
992
	char buffer[256];
993
	int ret;
994 995 996

	if (_IOC_TYPE(cmd) != '#' ||
	    _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
997
		return -EINVAL;
998 999

	if (_IOC_DIR(cmd) & _IOC_WRITE) {
1000
		if (_IOC_SIZE(cmd) > sizeof(buffer) ||
1001 1002 1003 1004
		    copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
			return -EFAULT;
	}

1005 1006 1007
	ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
	if (ret < 0)
		return ret;
1008 1009

	if (_IOC_DIR(cmd) & _IOC_READ) {
1010
		if (_IOC_SIZE(cmd) > sizeof(buffer) ||
1011 1012
		    copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
			return -EFAULT;
1013
	}
1014

1015
	return ret;
1016 1017
}

1018 1019
static long fw_device_op_ioctl(struct file *file,
			       unsigned int cmd, unsigned long arg)
1020 1021 1022
{
	struct client *client = file->private_data;

1023 1024 1025
	if (fw_device_is_shutdown(client->device))
		return -ENODEV;

1026 1027 1028 1029
	return dispatch_ioctl(client, cmd, (void __user *) arg);
}

#ifdef CONFIG_COMPAT
1030 1031
static long fw_device_op_compat_ioctl(struct file *file,
				      unsigned int cmd, unsigned long arg)
1032 1033 1034
{
	struct client *client = file->private_data;

1035 1036 1037
	if (fw_device_is_shutdown(client->device))
		return -ENODEV;

1038 1039 1040 1041 1042 1043 1044
	return dispatch_ioctl(client, cmd, compat_ptr(arg));
}
#endif

static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct client *client = file->private_data;
1045 1046
	enum dma_data_direction direction;
	unsigned long size;
1047
	int page_count, ret;
1048

1049 1050 1051
	if (fw_device_is_shutdown(client->device))
		return -ENODEV;

1052 1053 1054 1055 1056 1057
	/* FIXME: We could support multiple buffers, but we don't. */
	if (client->buffer.pages != NULL)
		return -EBUSY;

	if (!(vma->vm_flags & VM_SHARED))
		return -EINVAL;
1058

1059
	if (vma->vm_start & ~PAGE_MASK)
1060 1061 1062
		return -EINVAL;

	client->vm_start = vma->vm_start;
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	size = vma->vm_end - vma->vm_start;
	page_count = size >> PAGE_SHIFT;
	if (size & ~PAGE_MASK)
		return -EINVAL;

	if (vma->vm_flags & VM_WRITE)
		direction = DMA_TO_DEVICE;
	else
		direction = DMA_FROM_DEVICE;

1073 1074 1075 1076
	ret = fw_iso_buffer_init(&client->buffer, client->device->card,
				 page_count, direction);
	if (ret < 0)
		return ret;
1077

1078 1079
	ret = fw_iso_buffer_map(&client->buffer, vma);
	if (ret < 0)
1080 1081
		fw_iso_buffer_destroy(&client->buffer, client->device->card);

1082
	return ret;
1083 1084
}

1085 1086 1087 1088 1089 1090
static int shutdown_resource(int id, void *p, void *data)
{
	struct client_resource *r = p;
	struct client *client = data;

	r->release(client, r);
1091
	client_put(client);
1092 1093 1094 1095

	return 0;
}

1096 1097 1098
static int fw_device_op_release(struct inode *inode, struct file *file)
{
	struct client *client = file->private_data;
1099
	struct event *e, *next_e;
1100
	unsigned long flags;
1101

1102 1103 1104 1105
	mutex_lock(&client->device->client_list_mutex);
	list_del(&client->link);
	mutex_unlock(&client->device->client_list_mutex);

1106 1107 1108
	if (client->buffer.pages)
		fw_iso_buffer_destroy(&client->buffer, client->device->card);

1109 1110 1111
	if (client->iso_context)
		fw_iso_context_destroy(client->iso_context);

1112 1113 1114 1115
	/* Freeze client->resource_idr and client->event_list */
	spin_lock_irqsave(&client->lock, flags);
	client->in_shutdown = true;
	spin_unlock_irqrestore(&client->lock, flags);
1116

1117 1118 1119
	idr_for_each(&client->resource_idr, shutdown_resource, client);
	idr_remove_all(&client->resource_idr);
	idr_destroy(&client->resource_idr);
1120

1121 1122
	list_for_each_entry_safe(e, next_e, &client->event_list, link)
		kfree(e);
1123

1124
	client_put(client);
1125 1126 1127 1128 1129 1130 1131

	return 0;
}

static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
{
	struct client *client = file->private_data;
1132
	unsigned int mask = 0;
1133 1134 1135

	poll_wait(file, &client->wait, pt);

1136 1137
	if (fw_device_is_shutdown(client->device))
		mask |= POLLHUP | POLLERR;
1138
	if (!list_empty(&client->event_list))
1139 1140 1141
		mask |= POLLIN | POLLRDNORM;

	return mask;
1142 1143
}

1144
const struct file_operations fw_device_ops = {
1145 1146 1147 1148 1149 1150 1151 1152 1153
	.owner		= THIS_MODULE,
	.open		= fw_device_op_open,
	.read		= fw_device_op_read,
	.unlocked_ioctl	= fw_device_op_ioctl,
	.poll		= fw_device_op_poll,
	.release	= fw_device_op_release,
	.mmap		= fw_device_op_mmap,

#ifdef CONFIG_COMPAT
1154
	.compat_ioctl	= fw_device_op_compat_ioctl,
1155 1156
#endif
};