Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / firewire / fw-cdev.c
blob7dffdacf8c4b1267885ea16d08c94e9aeec4d8c7
1 /*
2 * Char device for device raw access
4 * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/wait.h>
24 #include <linux/errno.h>
25 #include <linux/device.h>
26 #include <linux/vmalloc.h>
27 #include <linux/poll.h>
28 #include <linux/preempt.h>
29 #include <linux/time.h>
30 #include <linux/delay.h>
31 #include <linux/mm.h>
32 #include <linux/idr.h>
33 #include <linux/compat.h>
34 #include <linux/firewire-cdev.h>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37 #include "fw-transaction.h"
38 #include "fw-topology.h"
39 #include "fw-device.h"
41 struct client;
42 struct client_resource {
43 struct list_head link;
44 void (*release)(struct client *client, struct client_resource *r);
45 u32 handle;
49 * dequeue_event() just kfree()'s the event, so the event has to be
50 * the first field in the struct.
53 struct event {
54 struct { void *data; size_t size; } v[2];
55 struct list_head link;
58 struct bus_reset {
59 struct event event;
60 struct fw_cdev_event_bus_reset reset;
63 struct response {
64 struct event event;
65 struct fw_transaction transaction;
66 struct client *client;
67 struct client_resource resource;
68 struct fw_cdev_event_response response;
71 struct iso_interrupt {
72 struct event event;
73 struct fw_cdev_event_iso_interrupt interrupt;
76 struct client {
77 u32 version;
78 struct fw_device *device;
79 spinlock_t lock;
80 u32 resource_handle;
81 struct list_head resource_list;
82 struct list_head event_list;
83 wait_queue_head_t wait;
84 u64 bus_reset_closure;
86 struct fw_iso_context *iso_context;
87 u64 iso_closure;
88 struct fw_iso_buffer buffer;
89 unsigned long vm_start;
91 struct list_head link;
94 static inline void __user *
95 u64_to_uptr(__u64 value)
97 return (void __user *)(unsigned long)value;
100 static inline __u64
101 uptr_to_u64(void __user *ptr)
103 return (__u64)(unsigned long)ptr;
106 static int fw_device_op_open(struct inode *inode, struct file *file)
108 struct fw_device *device;
109 struct client *client;
110 unsigned long flags;
112 <<<<<<< HEAD:drivers/firewire/fw-cdev.c
113 device = fw_device_from_devt(inode->i_rdev);
114 =======
115 device = fw_device_get_by_devt(inode->i_rdev);
116 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-cdev.c
117 if (device == NULL)
118 return -ENODEV;
120 client = kzalloc(sizeof(*client), GFP_KERNEL);
121 <<<<<<< HEAD:drivers/firewire/fw-cdev.c
122 if (client == NULL)
123 =======
124 if (client == NULL) {
125 fw_device_put(device);
126 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-cdev.c
127 return -ENOMEM;
128 <<<<<<< HEAD:drivers/firewire/fw-cdev.c
129 =======
131 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-cdev.c
133 <<<<<<< HEAD:drivers/firewire/fw-cdev.c
134 client->device = fw_device_get(device);
135 =======
136 client->device = device;
137 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-cdev.c
138 INIT_LIST_HEAD(&client->event_list);
139 INIT_LIST_HEAD(&client->resource_list);
140 spin_lock_init(&client->lock);
141 init_waitqueue_head(&client->wait);
143 file->private_data = client;
145 spin_lock_irqsave(&device->card->lock, flags);
146 list_add_tail(&client->link, &device->client_list);
147 spin_unlock_irqrestore(&device->card->lock, flags);
149 return 0;
152 static void queue_event(struct client *client, struct event *event,
153 void *data0, size_t size0, void *data1, size_t size1)
155 unsigned long flags;
157 event->v[0].data = data0;
158 event->v[0].size = size0;
159 event->v[1].data = data1;
160 event->v[1].size = size1;
162 spin_lock_irqsave(&client->lock, flags);
163 list_add_tail(&event->link, &client->event_list);
164 spin_unlock_irqrestore(&client->lock, flags);
166 wake_up_interruptible(&client->wait);
169 static int
170 dequeue_event(struct client *client, char __user *buffer, size_t count)
172 unsigned long flags;
173 struct event *event;
174 size_t size, total;
175 int i, retval;
177 retval = wait_event_interruptible(client->wait,
178 !list_empty(&client->event_list) ||
179 fw_device_is_shutdown(client->device));
180 if (retval < 0)
181 return retval;
183 if (list_empty(&client->event_list) &&
184 fw_device_is_shutdown(client->device))
185 return -ENODEV;
187 spin_lock_irqsave(&client->lock, flags);
188 event = container_of(client->event_list.next, struct event, link);
189 list_del(&event->link);
190 spin_unlock_irqrestore(&client->lock, flags);
192 total = 0;
193 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
194 size = min(event->v[i].size, count - total);
195 if (copy_to_user(buffer + total, event->v[i].data, size)) {
196 retval = -EFAULT;
197 goto out;
199 total += size;
201 retval = total;
203 out:
204 kfree(event);
206 return retval;
209 static ssize_t
210 fw_device_op_read(struct file *file,
211 char __user *buffer, size_t count, loff_t *offset)
213 struct client *client = file->private_data;
215 return dequeue_event(client, buffer, count);
218 static void
219 fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
220 struct client *client)
222 struct fw_card *card = client->device->card;
224 event->closure = client->bus_reset_closure;
225 event->type = FW_CDEV_EVENT_BUS_RESET;
226 event->generation = client->device->generation;
227 smp_rmb(); /* node_id must not be older than generation */
228 event->node_id = client->device->node_id;
229 event->local_node_id = card->local_node->node_id;
230 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
231 event->irm_node_id = card->irm_node->node_id;
232 event->root_node_id = card->root_node->node_id;
235 static void
236 for_each_client(struct fw_device *device,
237 void (*callback)(struct client *client))
239 struct fw_card *card = device->card;
240 struct client *c;
241 unsigned long flags;
243 spin_lock_irqsave(&card->lock, flags);
245 list_for_each_entry(c, &device->client_list, link)
246 callback(c);
248 spin_unlock_irqrestore(&card->lock, flags);
251 static void
252 queue_bus_reset_event(struct client *client)
254 struct bus_reset *bus_reset;
256 bus_reset = kzalloc(sizeof(*bus_reset), GFP_ATOMIC);
257 if (bus_reset == NULL) {
258 fw_notify("Out of memory when allocating bus reset event\n");
259 return;
262 fill_bus_reset_event(&bus_reset->reset, client);
264 queue_event(client, &bus_reset->event,
265 &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
268 void fw_device_cdev_update(struct fw_device *device)
270 for_each_client(device, queue_bus_reset_event);
273 static void wake_up_client(struct client *client)
275 wake_up_interruptible(&client->wait);
278 void fw_device_cdev_remove(struct fw_device *device)
280 for_each_client(device, wake_up_client);
283 static int ioctl_get_info(struct client *client, void *buffer)
285 struct fw_cdev_get_info *get_info = buffer;
286 struct fw_cdev_event_bus_reset bus_reset;
288 client->version = get_info->version;
289 get_info->version = FW_CDEV_VERSION;
291 if (get_info->rom != 0) {
292 void __user *uptr = u64_to_uptr(get_info->rom);
293 size_t want = get_info->rom_length;
294 size_t have = client->device->config_rom_length * 4;
296 if (copy_to_user(uptr, client->device->config_rom,
297 min(want, have)))
298 return -EFAULT;
300 get_info->rom_length = client->device->config_rom_length * 4;
302 client->bus_reset_closure = get_info->bus_reset_closure;
303 if (get_info->bus_reset != 0) {
304 void __user *uptr = u64_to_uptr(get_info->bus_reset);
306 fill_bus_reset_event(&bus_reset, client);
307 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
308 return -EFAULT;
311 get_info->card = client->device->card->index;
313 return 0;
316 static void
317 add_client_resource(struct client *client, struct client_resource *resource)
319 unsigned long flags;
321 spin_lock_irqsave(&client->lock, flags);
322 list_add_tail(&resource->link, &client->resource_list);
323 resource->handle = client->resource_handle++;
324 spin_unlock_irqrestore(&client->lock, flags);
327 static int
328 release_client_resource(struct client *client, u32 handle,
329 struct client_resource **resource)
331 struct client_resource *r;
332 unsigned long flags;
334 spin_lock_irqsave(&client->lock, flags);
335 list_for_each_entry(r, &client->resource_list, link) {
336 if (r->handle == handle) {
337 list_del(&r->link);
338 break;
341 spin_unlock_irqrestore(&client->lock, flags);
343 if (&r->link == &client->resource_list)
344 return -EINVAL;
346 if (resource)
347 *resource = r;
348 else
349 r->release(client, r);
351 return 0;
354 static void
355 release_transaction(struct client *client, struct client_resource *resource)
357 struct response *response =
358 container_of(resource, struct response, resource);
360 fw_cancel_transaction(client->device->card, &response->transaction);
363 static void
364 complete_transaction(struct fw_card *card, int rcode,
365 void *payload, size_t length, void *data)
367 struct response *response = data;
368 struct client *client = response->client;
369 unsigned long flags;
371 if (length < response->response.length)
372 response->response.length = length;
373 if (rcode == RCODE_COMPLETE)
374 memcpy(response->response.data, payload,
375 response->response.length);
377 spin_lock_irqsave(&client->lock, flags);
378 list_del(&response->resource.link);
379 spin_unlock_irqrestore(&client->lock, flags);
381 response->response.type = FW_CDEV_EVENT_RESPONSE;
382 response->response.rcode = rcode;
383 queue_event(client, &response->event,
384 &response->response, sizeof(response->response),
385 response->response.data, response->response.length);
388 static int ioctl_send_request(struct client *client, void *buffer)
390 struct fw_device *device = client->device;
391 struct fw_cdev_send_request *request = buffer;
392 struct response *response;
394 /* What is the biggest size we'll accept, really? */
395 if (request->length > 4096)
396 return -EINVAL;
398 response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
399 if (response == NULL)
400 return -ENOMEM;
402 response->client = client;
403 response->response.length = request->length;
404 response->response.closure = request->closure;
406 if (request->data &&
407 copy_from_user(response->response.data,
408 u64_to_uptr(request->data), request->length)) {
409 kfree(response);
410 return -EFAULT;
413 response->resource.release = release_transaction;
414 add_client_resource(client, &response->resource);
416 fw_send_request(device->card, &response->transaction,
417 request->tcode & 0x1f,
418 device->node->node_id,
419 request->generation,
420 device->max_speed,
421 request->offset,
422 response->response.data, request->length,
423 complete_transaction, response);
425 if (request->data)
426 return sizeof(request) + request->length;
427 else
428 return sizeof(request);
431 struct address_handler {
432 struct fw_address_handler handler;
433 __u64 closure;
434 struct client *client;
435 struct client_resource resource;
438 struct request {
439 struct fw_request *request;
440 void *data;
441 size_t length;
442 struct client_resource resource;
445 struct request_event {
446 struct event event;
447 struct fw_cdev_event_request request;
450 static void
451 release_request(struct client *client, struct client_resource *resource)
453 struct request *request =
454 container_of(resource, struct request, resource);
456 fw_send_response(client->device->card, request->request,
457 RCODE_CONFLICT_ERROR);
458 kfree(request);
461 static void
462 handle_request(struct fw_card *card, struct fw_request *r,
463 int tcode, int destination, int source,
464 int generation, int speed,
465 unsigned long long offset,
466 void *payload, size_t length, void *callback_data)
468 struct address_handler *handler = callback_data;
469 struct request *request;
470 struct request_event *e;
471 struct client *client = handler->client;
473 request = kmalloc(sizeof(*request), GFP_ATOMIC);
474 e = kmalloc(sizeof(*e), GFP_ATOMIC);
475 if (request == NULL || e == NULL) {
476 kfree(request);
477 kfree(e);
478 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
479 return;
482 request->request = r;
483 request->data = payload;
484 request->length = length;
486 request->resource.release = release_request;
487 add_client_resource(client, &request->resource);
489 e->request.type = FW_CDEV_EVENT_REQUEST;
490 e->request.tcode = tcode;
491 e->request.offset = offset;
492 e->request.length = length;
493 e->request.handle = request->resource.handle;
494 e->request.closure = handler->closure;
496 queue_event(client, &e->event,
497 &e->request, sizeof(e->request), payload, length);
500 static void
501 release_address_handler(struct client *client,
502 struct client_resource *resource)
504 struct address_handler *handler =
505 container_of(resource, struct address_handler, resource);
507 fw_core_remove_address_handler(&handler->handler);
508 kfree(handler);
511 static int ioctl_allocate(struct client *client, void *buffer)
513 struct fw_cdev_allocate *request = buffer;
514 struct address_handler *handler;
515 struct fw_address_region region;
517 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
518 if (handler == NULL)
519 return -ENOMEM;
521 region.start = request->offset;
522 region.end = request->offset + request->length;
523 handler->handler.length = request->length;
524 handler->handler.address_callback = handle_request;
525 handler->handler.callback_data = handler;
526 handler->closure = request->closure;
527 handler->client = client;
529 if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
530 kfree(handler);
531 return -EBUSY;
534 handler->resource.release = release_address_handler;
535 add_client_resource(client, &handler->resource);
536 request->handle = handler->resource.handle;
538 return 0;
541 static int ioctl_deallocate(struct client *client, void *buffer)
543 struct fw_cdev_deallocate *request = buffer;
545 return release_client_resource(client, request->handle, NULL);
548 static int ioctl_send_response(struct client *client, void *buffer)
550 struct fw_cdev_send_response *request = buffer;
551 struct client_resource *resource;
552 struct request *r;
554 if (release_client_resource(client, request->handle, &resource) < 0)
555 return -EINVAL;
556 r = container_of(resource, struct request, resource);
557 if (request->length < r->length)
558 r->length = request->length;
559 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
560 return -EFAULT;
562 fw_send_response(client->device->card, r->request, request->rcode);
563 kfree(r);
565 return 0;
568 static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
570 struct fw_cdev_initiate_bus_reset *request = buffer;
571 int short_reset;
573 short_reset = (request->type == FW_CDEV_SHORT_RESET);
575 return fw_core_initiate_bus_reset(client->device->card, short_reset);
578 struct descriptor {
579 struct fw_descriptor d;
580 struct client_resource resource;
581 u32 data[0];
584 static void release_descriptor(struct client *client,
585 struct client_resource *resource)
587 struct descriptor *descriptor =
588 container_of(resource, struct descriptor, resource);
590 fw_core_remove_descriptor(&descriptor->d);
591 kfree(descriptor);
594 static int ioctl_add_descriptor(struct client *client, void *buffer)
596 struct fw_cdev_add_descriptor *request = buffer;
597 struct descriptor *descriptor;
598 int retval;
600 if (request->length > 256)
601 return -EINVAL;
603 descriptor =
604 kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
605 if (descriptor == NULL)
606 return -ENOMEM;
608 if (copy_from_user(descriptor->data,
609 u64_to_uptr(request->data), request->length * 4)) {
610 kfree(descriptor);
611 return -EFAULT;
614 descriptor->d.length = request->length;
615 descriptor->d.immediate = request->immediate;
616 descriptor->d.key = request->key;
617 descriptor->d.data = descriptor->data;
619 retval = fw_core_add_descriptor(&descriptor->d);
620 if (retval < 0) {
621 kfree(descriptor);
622 return retval;
625 descriptor->resource.release = release_descriptor;
626 add_client_resource(client, &descriptor->resource);
627 request->handle = descriptor->resource.handle;
629 return 0;
632 static int ioctl_remove_descriptor(struct client *client, void *buffer)
634 struct fw_cdev_remove_descriptor *request = buffer;
636 return release_client_resource(client, request->handle, NULL);
639 static void
640 iso_callback(struct fw_iso_context *context, u32 cycle,
641 size_t header_length, void *header, void *data)
643 struct client *client = data;
644 struct iso_interrupt *irq;
646 irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
647 if (irq == NULL)
648 return;
650 irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
651 irq->interrupt.closure = client->iso_closure;
652 irq->interrupt.cycle = cycle;
653 irq->interrupt.header_length = header_length;
654 memcpy(irq->interrupt.header, header, header_length);
655 queue_event(client, &irq->event, &irq->interrupt,
656 sizeof(irq->interrupt) + header_length, NULL, 0);
659 static int ioctl_create_iso_context(struct client *client, void *buffer)
661 struct fw_cdev_create_iso_context *request = buffer;
662 struct fw_iso_context *context;
664 <<<<<<< HEAD:drivers/firewire/fw-cdev.c
665 =======
666 /* We only support one context at this time. */
667 if (client->iso_context != NULL)
668 return -EBUSY;
670 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-cdev.c
671 if (request->channel > 63)
672 return -EINVAL;
674 switch (request->type) {
675 case FW_ISO_CONTEXT_RECEIVE:
676 if (request->header_size < 4 || (request->header_size & 3))
677 return -EINVAL;
679 break;
681 case FW_ISO_CONTEXT_TRANSMIT:
682 if (request->speed > SCODE_3200)
683 return -EINVAL;
685 break;
687 default:
688 return -EINVAL;
691 context = fw_iso_context_create(client->device->card,
692 request->type,
693 request->channel,
694 request->speed,
695 request->header_size,
696 iso_callback, client);
697 if (IS_ERR(context))
698 return PTR_ERR(context);
700 client->iso_closure = request->closure;
701 client->iso_context = context;
703 /* We only support one context at this time. */
704 request->handle = 0;
706 return 0;
709 /* Macros for decoding the iso packet control header. */
710 #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
711 #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
712 #define GET_SKIP(v) (((v) >> 17) & 0x01)
713 #define GET_TAG(v) (((v) >> 18) & 0x02)
714 #define GET_SY(v) (((v) >> 20) & 0x04)
715 #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
717 static int ioctl_queue_iso(struct client *client, void *buffer)
719 struct fw_cdev_queue_iso *request = buffer;
720 struct fw_cdev_iso_packet __user *p, *end, *next;
721 struct fw_iso_context *ctx = client->iso_context;
722 unsigned long payload, buffer_end, header_length;
723 u32 control;
724 int count;
725 struct {
726 struct fw_iso_packet packet;
727 u8 header[256];
728 } u;
730 if (ctx == NULL || request->handle != 0)
731 return -EINVAL;
734 * If the user passes a non-NULL data pointer, has mmap()'ed
735 * the iso buffer, and the pointer points inside the buffer,
736 * we setup the payload pointers accordingly. Otherwise we
737 * set them both to 0, which will still let packets with
738 * payload_length == 0 through. In other words, if no packets
739 * use the indirect payload, the iso buffer need not be mapped
740 * and the request->data pointer is ignored.
743 payload = (unsigned long)request->data - client->vm_start;
744 buffer_end = client->buffer.page_count << PAGE_SHIFT;
745 if (request->data == 0 || client->buffer.pages == NULL ||
746 payload >= buffer_end) {
747 payload = 0;
748 buffer_end = 0;
751 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
753 if (!access_ok(VERIFY_READ, p, request->size))
754 return -EFAULT;
756 end = (void __user *)p + request->size;
757 count = 0;
758 while (p < end) {
759 if (get_user(control, &p->control))
760 return -EFAULT;
761 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
762 u.packet.interrupt = GET_INTERRUPT(control);
763 u.packet.skip = GET_SKIP(control);
764 u.packet.tag = GET_TAG(control);
765 u.packet.sy = GET_SY(control);
766 u.packet.header_length = GET_HEADER_LENGTH(control);
768 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
769 header_length = u.packet.header_length;
770 } else {
772 * We require that header_length is a multiple of
773 * the fixed header size, ctx->header_size.
775 if (ctx->header_size == 0) {
776 if (u.packet.header_length > 0)
777 return -EINVAL;
778 } else if (u.packet.header_length % ctx->header_size != 0) {
779 return -EINVAL;
781 header_length = 0;
784 next = (struct fw_cdev_iso_packet __user *)
785 &p->header[header_length / 4];
786 if (next > end)
787 return -EINVAL;
788 if (__copy_from_user
789 (u.packet.header, p->header, header_length))
790 return -EFAULT;
791 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
792 u.packet.header_length + u.packet.payload_length > 0)
793 return -EINVAL;
794 if (payload + u.packet.payload_length > buffer_end)
795 return -EINVAL;
797 if (fw_iso_context_queue(ctx, &u.packet,
798 &client->buffer, payload))
799 break;
801 p = next;
802 payload += u.packet.payload_length;
803 count++;
806 request->size -= uptr_to_u64(p) - request->packets;
807 request->packets = uptr_to_u64(p);
808 request->data = client->vm_start + payload;
810 return count;
813 static int ioctl_start_iso(struct client *client, void *buffer)
815 struct fw_cdev_start_iso *request = buffer;
817 <<<<<<< HEAD:drivers/firewire/fw-cdev.c
818 if (request->handle != 0)
819 =======
820 if (client->iso_context == NULL || request->handle != 0)
821 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-cdev.c
822 return -EINVAL;
823 <<<<<<< HEAD:drivers/firewire/fw-cdev.c
824 =======
826 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-cdev.c
827 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
828 if (request->tags == 0 || request->tags > 15)
829 return -EINVAL;
831 if (request->sync > 15)
832 return -EINVAL;
835 return fw_iso_context_start(client->iso_context, request->cycle,
836 request->sync, request->tags);
839 static int ioctl_stop_iso(struct client *client, void *buffer)
841 struct fw_cdev_stop_iso *request = buffer;
843 <<<<<<< HEAD:drivers/firewire/fw-cdev.c
844 if (request->handle != 0)
845 =======
846 if (client->iso_context == NULL || request->handle != 0)
847 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/firewire/fw-cdev.c
848 return -EINVAL;
850 return fw_iso_context_stop(client->iso_context);
853 static int ioctl_get_cycle_timer(struct client *client, void *buffer)
855 struct fw_cdev_get_cycle_timer *request = buffer;
856 struct fw_card *card = client->device->card;
857 unsigned long long bus_time;
858 struct timeval tv;
859 unsigned long flags;
861 preempt_disable();
862 local_irq_save(flags);
864 bus_time = card->driver->get_bus_time(card);
865 do_gettimeofday(&tv);
867 local_irq_restore(flags);
868 preempt_enable();
870 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
871 request->cycle_timer = bus_time & 0xffffffff;
872 return 0;
875 static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
876 ioctl_get_info,
877 ioctl_send_request,
878 ioctl_allocate,
879 ioctl_deallocate,
880 ioctl_send_response,
881 ioctl_initiate_bus_reset,
882 ioctl_add_descriptor,
883 ioctl_remove_descriptor,
884 ioctl_create_iso_context,
885 ioctl_queue_iso,
886 ioctl_start_iso,
887 ioctl_stop_iso,
888 ioctl_get_cycle_timer,
891 static int
892 dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
894 char buffer[256];
895 int retval;
897 if (_IOC_TYPE(cmd) != '#' ||
898 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
899 return -EINVAL;
901 if (_IOC_DIR(cmd) & _IOC_WRITE) {
902 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
903 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
904 return -EFAULT;
907 retval = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
908 if (retval < 0)
909 return retval;
911 if (_IOC_DIR(cmd) & _IOC_READ) {
912 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
913 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
914 return -EFAULT;
917 return 0;
920 static long
921 fw_device_op_ioctl(struct file *file,
922 unsigned int cmd, unsigned long arg)
924 struct client *client = file->private_data;
926 return dispatch_ioctl(client, cmd, (void __user *) arg);
929 #ifdef CONFIG_COMPAT
930 static long
931 fw_device_op_compat_ioctl(struct file *file,
932 unsigned int cmd, unsigned long arg)
934 struct client *client = file->private_data;
936 return dispatch_ioctl(client, cmd, compat_ptr(arg));
938 #endif
940 static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
942 struct client *client = file->private_data;
943 enum dma_data_direction direction;
944 unsigned long size;
945 int page_count, retval;
947 /* FIXME: We could support multiple buffers, but we don't. */
948 if (client->buffer.pages != NULL)
949 return -EBUSY;
951 if (!(vma->vm_flags & VM_SHARED))
952 return -EINVAL;
954 if (vma->vm_start & ~PAGE_MASK)
955 return -EINVAL;
957 client->vm_start = vma->vm_start;
958 size = vma->vm_end - vma->vm_start;
959 page_count = size >> PAGE_SHIFT;
960 if (size & ~PAGE_MASK)
961 return -EINVAL;
963 if (vma->vm_flags & VM_WRITE)
964 direction = DMA_TO_DEVICE;
965 else
966 direction = DMA_FROM_DEVICE;
968 retval = fw_iso_buffer_init(&client->buffer, client->device->card,
969 page_count, direction);
970 if (retval < 0)
971 return retval;
973 retval = fw_iso_buffer_map(&client->buffer, vma);
974 if (retval < 0)
975 fw_iso_buffer_destroy(&client->buffer, client->device->card);
977 return retval;
980 static int fw_device_op_release(struct inode *inode, struct file *file)
982 struct client *client = file->private_data;
983 struct event *e, *next_e;
984 struct client_resource *r, *next_r;
985 unsigned long flags;
987 if (client->buffer.pages)
988 fw_iso_buffer_destroy(&client->buffer, client->device->card);
990 if (client->iso_context)
991 fw_iso_context_destroy(client->iso_context);
993 list_for_each_entry_safe(r, next_r, &client->resource_list, link)
994 r->release(client, r);
997 * FIXME: We should wait for the async tasklets to stop
998 * running before freeing the memory.
1001 list_for_each_entry_safe(e, next_e, &client->event_list, link)
1002 kfree(e);
1004 spin_lock_irqsave(&client->device->card->lock, flags);
1005 list_del(&client->link);
1006 spin_unlock_irqrestore(&client->device->card->lock, flags);
1008 fw_device_put(client->device);
1009 kfree(client);
1011 return 0;
1014 static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1016 struct client *client = file->private_data;
1017 unsigned int mask = 0;
1019 poll_wait(file, &client->wait, pt);
1021 if (fw_device_is_shutdown(client->device))
1022 mask |= POLLHUP | POLLERR;
1023 if (!list_empty(&client->event_list))
1024 mask |= POLLIN | POLLRDNORM;
1026 return mask;
1029 const struct file_operations fw_device_ops = {
1030 .owner = THIS_MODULE,
1031 .open = fw_device_op_open,
1032 .read = fw_device_op_read,
1033 .unlocked_ioctl = fw_device_op_ioctl,
1034 .poll = fw_device_op_poll,
1035 .release = fw_device_op_release,
1036 .mmap = fw_device_op_mmap,
1038 #ifdef CONFIG_COMPAT
1039 .compat_ioctl = fw_device_op_compat_ioctl,
1040 #endif