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>
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"
42 struct client_resource {
43 struct list_head link;
44 void (*release)(struct client *client, struct client_resource *r);
49 * dequeue_event() just kfree()'s the event, so the event has to be
50 * the first field in the struct.
54 struct { void *data; size_t size; } v[2];
55 struct list_head link;
60 struct fw_cdev_event_bus_reset reset;
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 {
73 struct fw_cdev_event_iso_interrupt interrupt;
78 struct fw_device *device;
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;
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;
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;
112 device = fw_device_get_by_devt(inode->i_rdev);
116 client = kzalloc(sizeof(*client), GFP_KERNEL);
117 if (client == NULL) {
118 fw_device_put(device);
122 client->device = device;
123 INIT_LIST_HEAD(&client->event_list);
124 INIT_LIST_HEAD(&client->resource_list);
125 spin_lock_init(&client->lock);
126 init_waitqueue_head(&client->wait);
128 file->private_data = client;
130 spin_lock_irqsave(&device->card->lock, flags);
131 list_add_tail(&client->link, &device->client_list);
132 spin_unlock_irqrestore(&device->card->lock, flags);
137 static void queue_event(struct client *client, struct event *event,
138 void *data0, size_t size0, void *data1, size_t size1)
142 event->v[0].data = data0;
143 event->v[0].size = size0;
144 event->v[1].data = data1;
145 event->v[1].size = size1;
147 spin_lock_irqsave(&client->lock, flags);
148 list_add_tail(&event->link, &client->event_list);
149 spin_unlock_irqrestore(&client->lock, flags);
151 wake_up_interruptible(&client->wait);
155 dequeue_event(struct client *client, char __user *buffer, size_t count)
162 retval = wait_event_interruptible(client->wait,
163 !list_empty(&client->event_list) ||
164 fw_device_is_shutdown(client->device));
168 if (list_empty(&client->event_list) &&
169 fw_device_is_shutdown(client->device))
172 spin_lock_irqsave(&client->lock, flags);
173 event = container_of(client->event_list.next, struct event, link);
174 list_del(&event->link);
175 spin_unlock_irqrestore(&client->lock, flags);
178 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
179 size = min(event->v[i].size, count - total);
180 if (copy_to_user(buffer + total, event->v[i].data, size)) {
195 fw_device_op_read(struct file *file,
196 char __user *buffer, size_t count, loff_t *offset)
198 struct client *client = file->private_data;
200 return dequeue_event(client, buffer, count);
204 fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
205 struct client *client)
207 struct fw_card *card = client->device->card;
209 event->closure = client->bus_reset_closure;
210 event->type = FW_CDEV_EVENT_BUS_RESET;
211 event->generation = client->device->generation;
212 smp_rmb(); /* node_id must not be older than generation */
213 event->node_id = client->device->node_id;
214 event->local_node_id = card->local_node->node_id;
215 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
216 event->irm_node_id = card->irm_node->node_id;
217 event->root_node_id = card->root_node->node_id;
221 for_each_client(struct fw_device *device,
222 void (*callback)(struct client *client))
224 struct fw_card *card = device->card;
228 spin_lock_irqsave(&card->lock, flags);
230 list_for_each_entry(c, &device->client_list, link)
233 spin_unlock_irqrestore(&card->lock, flags);
237 queue_bus_reset_event(struct client *client)
239 struct bus_reset *bus_reset;
241 bus_reset = kzalloc(sizeof(*bus_reset), GFP_ATOMIC);
242 if (bus_reset == NULL) {
243 fw_notify("Out of memory when allocating bus reset event\n");
247 fill_bus_reset_event(&bus_reset->reset, client);
249 queue_event(client, &bus_reset->event,
250 &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
253 void fw_device_cdev_update(struct fw_device *device)
255 for_each_client(device, queue_bus_reset_event);
258 static void wake_up_client(struct client *client)
260 wake_up_interruptible(&client->wait);
263 void fw_device_cdev_remove(struct fw_device *device)
265 for_each_client(device, wake_up_client);
268 static int ioctl_get_info(struct client *client, void *buffer)
270 struct fw_cdev_get_info *get_info = buffer;
271 struct fw_cdev_event_bus_reset bus_reset;
273 client->version = get_info->version;
274 get_info->version = FW_CDEV_VERSION;
276 if (get_info->rom != 0) {
277 void __user *uptr = u64_to_uptr(get_info->rom);
278 size_t want = get_info->rom_length;
279 size_t have = client->device->config_rom_length * 4;
281 if (copy_to_user(uptr, client->device->config_rom,
285 get_info->rom_length = client->device->config_rom_length * 4;
287 client->bus_reset_closure = get_info->bus_reset_closure;
288 if (get_info->bus_reset != 0) {
289 void __user *uptr = u64_to_uptr(get_info->bus_reset);
291 fill_bus_reset_event(&bus_reset, client);
292 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
296 get_info->card = client->device->card->index;
302 add_client_resource(struct client *client, struct client_resource *resource)
306 spin_lock_irqsave(&client->lock, flags);
307 list_add_tail(&resource->link, &client->resource_list);
308 resource->handle = client->resource_handle++;
309 spin_unlock_irqrestore(&client->lock, flags);
313 release_client_resource(struct client *client, u32 handle,
314 struct client_resource **resource)
316 struct client_resource *r;
319 spin_lock_irqsave(&client->lock, flags);
320 list_for_each_entry(r, &client->resource_list, link) {
321 if (r->handle == handle) {
326 spin_unlock_irqrestore(&client->lock, flags);
328 if (&r->link == &client->resource_list)
334 r->release(client, r);
340 release_transaction(struct client *client, struct client_resource *resource)
342 struct response *response =
343 container_of(resource, struct response, resource);
345 fw_cancel_transaction(client->device->card, &response->transaction);
349 complete_transaction(struct fw_card *card, int rcode,
350 void *payload, size_t length, void *data)
352 struct response *response = data;
353 struct client *client = response->client;
356 if (length < response->response.length)
357 response->response.length = length;
358 if (rcode == RCODE_COMPLETE)
359 memcpy(response->response.data, payload,
360 response->response.length);
362 spin_lock_irqsave(&client->lock, flags);
363 list_del(&response->resource.link);
364 spin_unlock_irqrestore(&client->lock, flags);
366 response->response.type = FW_CDEV_EVENT_RESPONSE;
367 response->response.rcode = rcode;
368 queue_event(client, &response->event,
369 &response->response, sizeof(response->response),
370 response->response.data, response->response.length);
373 static int ioctl_send_request(struct client *client, void *buffer)
375 struct fw_device *device = client->device;
376 struct fw_cdev_send_request *request = buffer;
377 struct response *response;
379 /* What is the biggest size we'll accept, really? */
380 if (request->length > 4096)
383 response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
384 if (response == NULL)
387 response->client = client;
388 response->response.length = request->length;
389 response->response.closure = request->closure;
392 copy_from_user(response->response.data,
393 u64_to_uptr(request->data), request->length)) {
398 response->resource.release = release_transaction;
399 add_client_resource(client, &response->resource);
401 fw_send_request(device->card, &response->transaction,
402 request->tcode & 0x1f,
403 device->node->node_id,
407 response->response.data, request->length,
408 complete_transaction, response);
411 return sizeof(request) + request->length;
413 return sizeof(request);
416 struct address_handler {
417 struct fw_address_handler handler;
419 struct client *client;
420 struct client_resource resource;
424 struct fw_request *request;
427 struct client_resource resource;
430 struct request_event {
432 struct fw_cdev_event_request request;
436 release_request(struct client *client, struct client_resource *resource)
438 struct request *request =
439 container_of(resource, struct request, resource);
441 fw_send_response(client->device->card, request->request,
442 RCODE_CONFLICT_ERROR);
447 handle_request(struct fw_card *card, struct fw_request *r,
448 int tcode, int destination, int source,
449 int generation, int speed,
450 unsigned long long offset,
451 void *payload, size_t length, void *callback_data)
453 struct address_handler *handler = callback_data;
454 struct request *request;
455 struct request_event *e;
456 struct client *client = handler->client;
458 request = kmalloc(sizeof(*request), GFP_ATOMIC);
459 e = kmalloc(sizeof(*e), GFP_ATOMIC);
460 if (request == NULL || e == NULL) {
463 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
467 request->request = r;
468 request->data = payload;
469 request->length = length;
471 request->resource.release = release_request;
472 add_client_resource(client, &request->resource);
474 e->request.type = FW_CDEV_EVENT_REQUEST;
475 e->request.tcode = tcode;
476 e->request.offset = offset;
477 e->request.length = length;
478 e->request.handle = request->resource.handle;
479 e->request.closure = handler->closure;
481 queue_event(client, &e->event,
482 &e->request, sizeof(e->request), payload, length);
486 release_address_handler(struct client *client,
487 struct client_resource *resource)
489 struct address_handler *handler =
490 container_of(resource, struct address_handler, resource);
492 fw_core_remove_address_handler(&handler->handler);
496 static int ioctl_allocate(struct client *client, void *buffer)
498 struct fw_cdev_allocate *request = buffer;
499 struct address_handler *handler;
500 struct fw_address_region region;
502 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
506 region.start = request->offset;
507 region.end = request->offset + request->length;
508 handler->handler.length = request->length;
509 handler->handler.address_callback = handle_request;
510 handler->handler.callback_data = handler;
511 handler->closure = request->closure;
512 handler->client = client;
514 if (fw_core_add_address_handler(&handler->handler, ®ion) < 0) {
519 handler->resource.release = release_address_handler;
520 add_client_resource(client, &handler->resource);
521 request->handle = handler->resource.handle;
526 static int ioctl_deallocate(struct client *client, void *buffer)
528 struct fw_cdev_deallocate *request = buffer;
530 return release_client_resource(client, request->handle, NULL);
533 static int ioctl_send_response(struct client *client, void *buffer)
535 struct fw_cdev_send_response *request = buffer;
536 struct client_resource *resource;
539 if (release_client_resource(client, request->handle, &resource) < 0)
541 r = container_of(resource, struct request, resource);
542 if (request->length < r->length)
543 r->length = request->length;
544 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
547 fw_send_response(client->device->card, r->request, request->rcode);
553 static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
555 struct fw_cdev_initiate_bus_reset *request = buffer;
558 short_reset = (request->type == FW_CDEV_SHORT_RESET);
560 return fw_core_initiate_bus_reset(client->device->card, short_reset);
564 struct fw_descriptor d;
565 struct client_resource resource;
569 static void release_descriptor(struct client *client,
570 struct client_resource *resource)
572 struct descriptor *descriptor =
573 container_of(resource, struct descriptor, resource);
575 fw_core_remove_descriptor(&descriptor->d);
579 static int ioctl_add_descriptor(struct client *client, void *buffer)
581 struct fw_cdev_add_descriptor *request = buffer;
582 struct descriptor *descriptor;
585 if (request->length > 256)
589 kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
590 if (descriptor == NULL)
593 if (copy_from_user(descriptor->data,
594 u64_to_uptr(request->data), request->length * 4)) {
599 descriptor->d.length = request->length;
600 descriptor->d.immediate = request->immediate;
601 descriptor->d.key = request->key;
602 descriptor->d.data = descriptor->data;
604 retval = fw_core_add_descriptor(&descriptor->d);
610 descriptor->resource.release = release_descriptor;
611 add_client_resource(client, &descriptor->resource);
612 request->handle = descriptor->resource.handle;
617 static int ioctl_remove_descriptor(struct client *client, void *buffer)
619 struct fw_cdev_remove_descriptor *request = buffer;
621 return release_client_resource(client, request->handle, NULL);
625 iso_callback(struct fw_iso_context *context, u32 cycle,
626 size_t header_length, void *header, void *data)
628 struct client *client = data;
629 struct iso_interrupt *irq;
631 irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
635 irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
636 irq->interrupt.closure = client->iso_closure;
637 irq->interrupt.cycle = cycle;
638 irq->interrupt.header_length = header_length;
639 memcpy(irq->interrupt.header, header, header_length);
640 queue_event(client, &irq->event, &irq->interrupt,
641 sizeof(irq->interrupt) + header_length, NULL, 0);
644 static int ioctl_create_iso_context(struct client *client, void *buffer)
646 struct fw_cdev_create_iso_context *request = buffer;
647 struct fw_iso_context *context;
649 /* We only support one context at this time. */
650 if (client->iso_context != NULL)
653 if (request->channel > 63)
656 switch (request->type) {
657 case FW_ISO_CONTEXT_RECEIVE:
658 if (request->header_size < 4 || (request->header_size & 3))
663 case FW_ISO_CONTEXT_TRANSMIT:
664 if (request->speed > SCODE_3200)
673 context = fw_iso_context_create(client->device->card,
677 request->header_size,
678 iso_callback, client);
680 return PTR_ERR(context);
682 client->iso_closure = request->closure;
683 client->iso_context = context;
685 /* We only support one context at this time. */
691 /* Macros for decoding the iso packet control header. */
692 #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
693 #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
694 #define GET_SKIP(v) (((v) >> 17) & 0x01)
695 #define GET_TAG(v) (((v) >> 18) & 0x02)
696 #define GET_SY(v) (((v) >> 20) & 0x04)
697 #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
699 static int ioctl_queue_iso(struct client *client, void *buffer)
701 struct fw_cdev_queue_iso *request = buffer;
702 struct fw_cdev_iso_packet __user *p, *end, *next;
703 struct fw_iso_context *ctx = client->iso_context;
704 unsigned long payload, buffer_end, header_length;
708 struct fw_iso_packet packet;
712 if (ctx == NULL || request->handle != 0)
716 * If the user passes a non-NULL data pointer, has mmap()'ed
717 * the iso buffer, and the pointer points inside the buffer,
718 * we setup the payload pointers accordingly. Otherwise we
719 * set them both to 0, which will still let packets with
720 * payload_length == 0 through. In other words, if no packets
721 * use the indirect payload, the iso buffer need not be mapped
722 * and the request->data pointer is ignored.
725 payload = (unsigned long)request->data - client->vm_start;
726 buffer_end = client->buffer.page_count << PAGE_SHIFT;
727 if (request->data == 0 || client->buffer.pages == NULL ||
728 payload >= buffer_end) {
733 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
735 if (!access_ok(VERIFY_READ, p, request->size))
738 end = (void __user *)p + request->size;
741 if (get_user(control, &p->control))
743 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
744 u.packet.interrupt = GET_INTERRUPT(control);
745 u.packet.skip = GET_SKIP(control);
746 u.packet.tag = GET_TAG(control);
747 u.packet.sy = GET_SY(control);
748 u.packet.header_length = GET_HEADER_LENGTH(control);
750 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
751 header_length = u.packet.header_length;
754 * We require that header_length is a multiple of
755 * the fixed header size, ctx->header_size.
757 if (ctx->header_size == 0) {
758 if (u.packet.header_length > 0)
760 } else if (u.packet.header_length % ctx->header_size != 0) {
766 next = (struct fw_cdev_iso_packet __user *)
767 &p->header[header_length / 4];
771 (u.packet.header, p->header, header_length))
773 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
774 u.packet.header_length + u.packet.payload_length > 0)
776 if (payload + u.packet.payload_length > buffer_end)
779 if (fw_iso_context_queue(ctx, &u.packet,
780 &client->buffer, payload))
784 payload += u.packet.payload_length;
788 request->size -= uptr_to_u64(p) - request->packets;
789 request->packets = uptr_to_u64(p);
790 request->data = client->vm_start + payload;
795 static int ioctl_start_iso(struct client *client, void *buffer)
797 struct fw_cdev_start_iso *request = buffer;
799 if (client->iso_context == NULL || request->handle != 0)
802 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
803 if (request->tags == 0 || request->tags > 15)
806 if (request->sync > 15)
810 return fw_iso_context_start(client->iso_context, request->cycle,
811 request->sync, request->tags);
814 static int ioctl_stop_iso(struct client *client, void *buffer)
816 struct fw_cdev_stop_iso *request = buffer;
818 if (client->iso_context == NULL || request->handle != 0)
821 return fw_iso_context_stop(client->iso_context);
824 static int ioctl_get_cycle_timer(struct client *client, void *buffer)
826 struct fw_cdev_get_cycle_timer *request = buffer;
827 struct fw_card *card = client->device->card;
828 unsigned long long bus_time;
833 local_irq_save(flags);
835 bus_time = card->driver->get_bus_time(card);
836 do_gettimeofday(&tv);
838 local_irq_restore(flags);
841 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
842 request->cycle_timer = bus_time & 0xffffffff;
846 static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
852 ioctl_initiate_bus_reset,
853 ioctl_add_descriptor,
854 ioctl_remove_descriptor,
855 ioctl_create_iso_context,
859 ioctl_get_cycle_timer,
863 dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
868 if (_IOC_TYPE(cmd) != '#' ||
869 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
872 if (_IOC_DIR(cmd) & _IOC_WRITE) {
873 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
874 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
878 retval = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
882 if (_IOC_DIR(cmd) & _IOC_READ) {
883 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
884 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
892 fw_device_op_ioctl(struct file *file,
893 unsigned int cmd, unsigned long arg)
895 struct client *client = file->private_data;
897 return dispatch_ioctl(client, cmd, (void __user *) arg);
902 fw_device_op_compat_ioctl(struct file *file,
903 unsigned int cmd, unsigned long arg)
905 struct client *client = file->private_data;
907 return dispatch_ioctl(client, cmd, compat_ptr(arg));
911 static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
913 struct client *client = file->private_data;
914 enum dma_data_direction direction;
916 int page_count, retval;
918 /* FIXME: We could support multiple buffers, but we don't. */
919 if (client->buffer.pages != NULL)
922 if (!(vma->vm_flags & VM_SHARED))
925 if (vma->vm_start & ~PAGE_MASK)
928 client->vm_start = vma->vm_start;
929 size = vma->vm_end - vma->vm_start;
930 page_count = size >> PAGE_SHIFT;
931 if (size & ~PAGE_MASK)
934 if (vma->vm_flags & VM_WRITE)
935 direction = DMA_TO_DEVICE;
937 direction = DMA_FROM_DEVICE;
939 retval = fw_iso_buffer_init(&client->buffer, client->device->card,
940 page_count, direction);
944 retval = fw_iso_buffer_map(&client->buffer, vma);
946 fw_iso_buffer_destroy(&client->buffer, client->device->card);
951 static int fw_device_op_release(struct inode *inode, struct file *file)
953 struct client *client = file->private_data;
954 struct event *e, *next_e;
955 struct client_resource *r, *next_r;
958 if (client->buffer.pages)
959 fw_iso_buffer_destroy(&client->buffer, client->device->card);
961 if (client->iso_context)
962 fw_iso_context_destroy(client->iso_context);
964 list_for_each_entry_safe(r, next_r, &client->resource_list, link)
965 r->release(client, r);
968 * FIXME: We should wait for the async tasklets to stop
969 * running before freeing the memory.
972 list_for_each_entry_safe(e, next_e, &client->event_list, link)
975 spin_lock_irqsave(&client->device->card->lock, flags);
976 list_del(&client->link);
977 spin_unlock_irqrestore(&client->device->card->lock, flags);
979 fw_device_put(client->device);
985 static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
987 struct client *client = file->private_data;
988 unsigned int mask = 0;
990 poll_wait(file, &client->wait, pt);
992 if (fw_device_is_shutdown(client->device))
993 mask |= POLLHUP | POLLERR;
994 if (!list_empty(&client->event_list))
995 mask |= POLLIN | POLLRDNORM;
1000 const struct file_operations fw_device_ops = {
1001 .owner = THIS_MODULE,
1002 .open = fw_device_op_open,
1003 .read = fw_device_op_read,
1004 .unlocked_ioctl = fw_device_op_ioctl,
1005 .poll = fw_device_op_poll,
1006 .release = fw_device_op_release,
1007 .mmap = fw_device_op_mmap,
1009 #ifdef CONFIG_COMPAT
1010 .compat_ioctl = fw_device_op_compat_ioctl,