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 if (fw_device_is_shutdown(device)) {
117 fw_device_put(device);
121 client = kzalloc(sizeof(*client), GFP_KERNEL);
122 if (client == NULL) {
123 fw_device_put(device);
127 client->device = device;
128 INIT_LIST_HEAD(&client->event_list);
129 INIT_LIST_HEAD(&client->resource_list);
130 spin_lock_init(&client->lock);
131 init_waitqueue_head(&client->wait);
133 file->private_data = client;
135 spin_lock_irqsave(&device->card->lock, flags);
136 list_add_tail(&client->link, &device->client_list);
137 spin_unlock_irqrestore(&device->card->lock, flags);
142 static void queue_event(struct client *client, struct event *event,
143 void *data0, size_t size0, void *data1, size_t size1)
147 event->v[0].data = data0;
148 event->v[0].size = size0;
149 event->v[1].data = data1;
150 event->v[1].size = size1;
152 spin_lock_irqsave(&client->lock, flags);
153 list_add_tail(&event->link, &client->event_list);
154 spin_unlock_irqrestore(&client->lock, flags);
156 wake_up_interruptible(&client->wait);
160 dequeue_event(struct client *client, char __user *buffer, size_t count)
167 retval = wait_event_interruptible(client->wait,
168 !list_empty(&client->event_list) ||
169 fw_device_is_shutdown(client->device));
173 if (list_empty(&client->event_list) &&
174 fw_device_is_shutdown(client->device))
177 spin_lock_irqsave(&client->lock, flags);
178 event = container_of(client->event_list.next, struct event, link);
179 list_del(&event->link);
180 spin_unlock_irqrestore(&client->lock, flags);
183 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
184 size = min(event->v[i].size, count - total);
185 if (copy_to_user(buffer + total, event->v[i].data, size)) {
200 fw_device_op_read(struct file *file,
201 char __user *buffer, size_t count, loff_t *offset)
203 struct client *client = file->private_data;
205 return dequeue_event(client, buffer, count);
209 fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
210 struct client *client)
212 struct fw_card *card = client->device->card;
214 event->closure = client->bus_reset_closure;
215 event->type = FW_CDEV_EVENT_BUS_RESET;
216 event->generation = client->device->generation;
217 smp_rmb(); /* node_id must not be older than generation */
218 event->node_id = client->device->node_id;
219 event->local_node_id = card->local_node->node_id;
220 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
221 event->irm_node_id = card->irm_node->node_id;
222 event->root_node_id = card->root_node->node_id;
226 for_each_client(struct fw_device *device,
227 void (*callback)(struct client *client))
229 struct fw_card *card = device->card;
233 spin_lock_irqsave(&card->lock, flags);
235 list_for_each_entry(c, &device->client_list, link)
238 spin_unlock_irqrestore(&card->lock, flags);
242 queue_bus_reset_event(struct client *client)
244 struct bus_reset *bus_reset;
246 bus_reset = kzalloc(sizeof(*bus_reset), GFP_ATOMIC);
247 if (bus_reset == NULL) {
248 fw_notify("Out of memory when allocating bus reset event\n");
252 fill_bus_reset_event(&bus_reset->reset, client);
254 queue_event(client, &bus_reset->event,
255 &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
258 void fw_device_cdev_update(struct fw_device *device)
260 for_each_client(device, queue_bus_reset_event);
263 static void wake_up_client(struct client *client)
265 wake_up_interruptible(&client->wait);
268 void fw_device_cdev_remove(struct fw_device *device)
270 for_each_client(device, wake_up_client);
273 static int ioctl_get_info(struct client *client, void *buffer)
275 struct fw_cdev_get_info *get_info = buffer;
276 struct fw_cdev_event_bus_reset bus_reset;
277 unsigned long ret = 0;
279 client->version = get_info->version;
280 get_info->version = FW_CDEV_VERSION;
282 down_read(&fw_device_rwsem);
284 if (get_info->rom != 0) {
285 void __user *uptr = u64_to_uptr(get_info->rom);
286 size_t want = get_info->rom_length;
287 size_t have = client->device->config_rom_length * 4;
289 ret = copy_to_user(uptr, client->device->config_rom,
292 get_info->rom_length = client->device->config_rom_length * 4;
294 up_read(&fw_device_rwsem);
299 client->bus_reset_closure = get_info->bus_reset_closure;
300 if (get_info->bus_reset != 0) {
301 void __user *uptr = u64_to_uptr(get_info->bus_reset);
303 fill_bus_reset_event(&bus_reset, client);
304 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
308 get_info->card = client->device->card->index;
314 add_client_resource(struct client *client, struct client_resource *resource)
318 spin_lock_irqsave(&client->lock, flags);
319 list_add_tail(&resource->link, &client->resource_list);
320 resource->handle = client->resource_handle++;
321 spin_unlock_irqrestore(&client->lock, flags);
325 release_client_resource(struct client *client, u32 handle,
326 struct client_resource **resource)
328 struct client_resource *r;
331 spin_lock_irqsave(&client->lock, flags);
332 list_for_each_entry(r, &client->resource_list, link) {
333 if (r->handle == handle) {
338 spin_unlock_irqrestore(&client->lock, flags);
340 if (&r->link == &client->resource_list)
346 r->release(client, r);
352 release_transaction(struct client *client, struct client_resource *resource)
354 struct response *response =
355 container_of(resource, struct response, resource);
357 fw_cancel_transaction(client->device->card, &response->transaction);
361 complete_transaction(struct fw_card *card, int rcode,
362 void *payload, size_t length, void *data)
364 struct response *response = data;
365 struct client *client = response->client;
368 if (length < response->response.length)
369 response->response.length = length;
370 if (rcode == RCODE_COMPLETE)
371 memcpy(response->response.data, payload,
372 response->response.length);
374 spin_lock_irqsave(&client->lock, flags);
375 list_del(&response->resource.link);
376 spin_unlock_irqrestore(&client->lock, flags);
378 response->response.type = FW_CDEV_EVENT_RESPONSE;
379 response->response.rcode = rcode;
380 queue_event(client, &response->event,
381 &response->response, sizeof(response->response),
382 response->response.data, response->response.length);
385 static int ioctl_send_request(struct client *client, void *buffer)
387 struct fw_device *device = client->device;
388 struct fw_cdev_send_request *request = buffer;
389 struct response *response;
391 /* What is the biggest size we'll accept, really? */
392 if (request->length > 4096)
395 response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
396 if (response == NULL)
399 response->client = client;
400 response->response.length = request->length;
401 response->response.closure = request->closure;
404 copy_from_user(response->response.data,
405 u64_to_uptr(request->data), request->length)) {
410 response->resource.release = release_transaction;
411 add_client_resource(client, &response->resource);
413 fw_send_request(device->card, &response->transaction,
414 request->tcode & 0x1f,
415 device->node->node_id,
419 response->response.data, request->length,
420 complete_transaction, response);
423 return sizeof(request) + request->length;
425 return sizeof(request);
428 struct address_handler {
429 struct fw_address_handler handler;
431 struct client *client;
432 struct client_resource resource;
436 struct fw_request *request;
439 struct client_resource resource;
442 struct request_event {
444 struct fw_cdev_event_request request;
448 release_request(struct client *client, struct client_resource *resource)
450 struct request *request =
451 container_of(resource, struct request, resource);
453 fw_send_response(client->device->card, request->request,
454 RCODE_CONFLICT_ERROR);
459 handle_request(struct fw_card *card, struct fw_request *r,
460 int tcode, int destination, int source,
461 int generation, int speed,
462 unsigned long long offset,
463 void *payload, size_t length, void *callback_data)
465 struct address_handler *handler = callback_data;
466 struct request *request;
467 struct request_event *e;
468 struct client *client = handler->client;
470 request = kmalloc(sizeof(*request), GFP_ATOMIC);
471 e = kmalloc(sizeof(*e), GFP_ATOMIC);
472 if (request == NULL || e == NULL) {
475 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
479 request->request = r;
480 request->data = payload;
481 request->length = length;
483 request->resource.release = release_request;
484 add_client_resource(client, &request->resource);
486 e->request.type = FW_CDEV_EVENT_REQUEST;
487 e->request.tcode = tcode;
488 e->request.offset = offset;
489 e->request.length = length;
490 e->request.handle = request->resource.handle;
491 e->request.closure = handler->closure;
493 queue_event(client, &e->event,
494 &e->request, sizeof(e->request), payload, length);
498 release_address_handler(struct client *client,
499 struct client_resource *resource)
501 struct address_handler *handler =
502 container_of(resource, struct address_handler, resource);
504 fw_core_remove_address_handler(&handler->handler);
508 static int ioctl_allocate(struct client *client, void *buffer)
510 struct fw_cdev_allocate *request = buffer;
511 struct address_handler *handler;
512 struct fw_address_region region;
514 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
518 region.start = request->offset;
519 region.end = request->offset + request->length;
520 handler->handler.length = request->length;
521 handler->handler.address_callback = handle_request;
522 handler->handler.callback_data = handler;
523 handler->closure = request->closure;
524 handler->client = client;
526 if (fw_core_add_address_handler(&handler->handler, ®ion) < 0) {
531 handler->resource.release = release_address_handler;
532 add_client_resource(client, &handler->resource);
533 request->handle = handler->resource.handle;
538 static int ioctl_deallocate(struct client *client, void *buffer)
540 struct fw_cdev_deallocate *request = buffer;
542 return release_client_resource(client, request->handle, NULL);
545 static int ioctl_send_response(struct client *client, void *buffer)
547 struct fw_cdev_send_response *request = buffer;
548 struct client_resource *resource;
551 if (release_client_resource(client, request->handle, &resource) < 0)
553 r = container_of(resource, struct request, resource);
554 if (request->length < r->length)
555 r->length = request->length;
556 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
559 fw_send_response(client->device->card, r->request, request->rcode);
565 static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
567 struct fw_cdev_initiate_bus_reset *request = buffer;
570 short_reset = (request->type == FW_CDEV_SHORT_RESET);
572 return fw_core_initiate_bus_reset(client->device->card, short_reset);
576 struct fw_descriptor d;
577 struct client_resource resource;
581 static void release_descriptor(struct client *client,
582 struct client_resource *resource)
584 struct descriptor *descriptor =
585 container_of(resource, struct descriptor, resource);
587 fw_core_remove_descriptor(&descriptor->d);
591 static int ioctl_add_descriptor(struct client *client, void *buffer)
593 struct fw_cdev_add_descriptor *request = buffer;
594 struct descriptor *descriptor;
597 if (request->length > 256)
601 kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
602 if (descriptor == NULL)
605 if (copy_from_user(descriptor->data,
606 u64_to_uptr(request->data), request->length * 4)) {
611 descriptor->d.length = request->length;
612 descriptor->d.immediate = request->immediate;
613 descriptor->d.key = request->key;
614 descriptor->d.data = descriptor->data;
616 retval = fw_core_add_descriptor(&descriptor->d);
622 descriptor->resource.release = release_descriptor;
623 add_client_resource(client, &descriptor->resource);
624 request->handle = descriptor->resource.handle;
629 static int ioctl_remove_descriptor(struct client *client, void *buffer)
631 struct fw_cdev_remove_descriptor *request = buffer;
633 return release_client_resource(client, request->handle, NULL);
637 iso_callback(struct fw_iso_context *context, u32 cycle,
638 size_t header_length, void *header, void *data)
640 struct client *client = data;
641 struct iso_interrupt *irq;
643 irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
647 irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
648 irq->interrupt.closure = client->iso_closure;
649 irq->interrupt.cycle = cycle;
650 irq->interrupt.header_length = header_length;
651 memcpy(irq->interrupt.header, header, header_length);
652 queue_event(client, &irq->event, &irq->interrupt,
653 sizeof(irq->interrupt) + header_length, NULL, 0);
656 static int ioctl_create_iso_context(struct client *client, void *buffer)
658 struct fw_cdev_create_iso_context *request = buffer;
659 struct fw_iso_context *context;
661 /* We only support one context at this time. */
662 if (client->iso_context != NULL)
665 if (request->channel > 63)
668 switch (request->type) {
669 case FW_ISO_CONTEXT_RECEIVE:
670 if (request->header_size < 4 || (request->header_size & 3))
675 case FW_ISO_CONTEXT_TRANSMIT:
676 if (request->speed > SCODE_3200)
685 context = fw_iso_context_create(client->device->card,
689 request->header_size,
690 iso_callback, client);
692 return PTR_ERR(context);
694 client->iso_closure = request->closure;
695 client->iso_context = context;
697 /* We only support one context at this time. */
703 /* Macros for decoding the iso packet control header. */
704 #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
705 #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
706 #define GET_SKIP(v) (((v) >> 17) & 0x01)
707 #define GET_TAG(v) (((v) >> 18) & 0x02)
708 #define GET_SY(v) (((v) >> 20) & 0x04)
709 #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
711 static int ioctl_queue_iso(struct client *client, void *buffer)
713 struct fw_cdev_queue_iso *request = buffer;
714 struct fw_cdev_iso_packet __user *p, *end, *next;
715 struct fw_iso_context *ctx = client->iso_context;
716 unsigned long payload, buffer_end, header_length;
720 struct fw_iso_packet packet;
724 if (ctx == NULL || request->handle != 0)
728 * If the user passes a non-NULL data pointer, has mmap()'ed
729 * the iso buffer, and the pointer points inside the buffer,
730 * we setup the payload pointers accordingly. Otherwise we
731 * set them both to 0, which will still let packets with
732 * payload_length == 0 through. In other words, if no packets
733 * use the indirect payload, the iso buffer need not be mapped
734 * and the request->data pointer is ignored.
737 payload = (unsigned long)request->data - client->vm_start;
738 buffer_end = client->buffer.page_count << PAGE_SHIFT;
739 if (request->data == 0 || client->buffer.pages == NULL ||
740 payload >= buffer_end) {
745 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
747 if (!access_ok(VERIFY_READ, p, request->size))
750 end = (void __user *)p + request->size;
753 if (get_user(control, &p->control))
755 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
756 u.packet.interrupt = GET_INTERRUPT(control);
757 u.packet.skip = GET_SKIP(control);
758 u.packet.tag = GET_TAG(control);
759 u.packet.sy = GET_SY(control);
760 u.packet.header_length = GET_HEADER_LENGTH(control);
762 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
763 header_length = u.packet.header_length;
766 * We require that header_length is a multiple of
767 * the fixed header size, ctx->header_size.
769 if (ctx->header_size == 0) {
770 if (u.packet.header_length > 0)
772 } else if (u.packet.header_length % ctx->header_size != 0) {
778 next = (struct fw_cdev_iso_packet __user *)
779 &p->header[header_length / 4];
783 (u.packet.header, p->header, header_length))
785 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
786 u.packet.header_length + u.packet.payload_length > 0)
788 if (payload + u.packet.payload_length > buffer_end)
791 if (fw_iso_context_queue(ctx, &u.packet,
792 &client->buffer, payload))
796 payload += u.packet.payload_length;
800 request->size -= uptr_to_u64(p) - request->packets;
801 request->packets = uptr_to_u64(p);
802 request->data = client->vm_start + payload;
807 static int ioctl_start_iso(struct client *client, void *buffer)
809 struct fw_cdev_start_iso *request = buffer;
811 if (client->iso_context == NULL || request->handle != 0)
814 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
815 if (request->tags == 0 || request->tags > 15)
818 if (request->sync > 15)
822 return fw_iso_context_start(client->iso_context, request->cycle,
823 request->sync, request->tags);
826 static int ioctl_stop_iso(struct client *client, void *buffer)
828 struct fw_cdev_stop_iso *request = buffer;
830 if (client->iso_context == NULL || request->handle != 0)
833 return fw_iso_context_stop(client->iso_context);
836 static int ioctl_get_cycle_timer(struct client *client, void *buffer)
838 struct fw_cdev_get_cycle_timer *request = buffer;
839 struct fw_card *card = client->device->card;
840 unsigned long long bus_time;
845 local_irq_save(flags);
847 bus_time = card->driver->get_bus_time(card);
848 do_gettimeofday(&tv);
850 local_irq_restore(flags);
853 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
854 request->cycle_timer = bus_time & 0xffffffff;
858 static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
864 ioctl_initiate_bus_reset,
865 ioctl_add_descriptor,
866 ioctl_remove_descriptor,
867 ioctl_create_iso_context,
871 ioctl_get_cycle_timer,
875 dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
880 if (_IOC_TYPE(cmd) != '#' ||
881 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
884 if (_IOC_DIR(cmd) & _IOC_WRITE) {
885 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
886 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
890 retval = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
894 if (_IOC_DIR(cmd) & _IOC_READ) {
895 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
896 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
904 fw_device_op_ioctl(struct file *file,
905 unsigned int cmd, unsigned long arg)
907 struct client *client = file->private_data;
909 if (fw_device_is_shutdown(client->device))
912 return dispatch_ioctl(client, cmd, (void __user *) arg);
917 fw_device_op_compat_ioctl(struct file *file,
918 unsigned int cmd, unsigned long arg)
920 struct client *client = file->private_data;
922 if (fw_device_is_shutdown(client->device))
925 return dispatch_ioctl(client, cmd, compat_ptr(arg));
929 static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
931 struct client *client = file->private_data;
932 enum dma_data_direction direction;
934 int page_count, retval;
936 if (fw_device_is_shutdown(client->device))
939 /* FIXME: We could support multiple buffers, but we don't. */
940 if (client->buffer.pages != NULL)
943 if (!(vma->vm_flags & VM_SHARED))
946 if (vma->vm_start & ~PAGE_MASK)
949 client->vm_start = vma->vm_start;
950 size = vma->vm_end - vma->vm_start;
951 page_count = size >> PAGE_SHIFT;
952 if (size & ~PAGE_MASK)
955 if (vma->vm_flags & VM_WRITE)
956 direction = DMA_TO_DEVICE;
958 direction = DMA_FROM_DEVICE;
960 retval = fw_iso_buffer_init(&client->buffer, client->device->card,
961 page_count, direction);
965 retval = fw_iso_buffer_map(&client->buffer, vma);
967 fw_iso_buffer_destroy(&client->buffer, client->device->card);
972 static int fw_device_op_release(struct inode *inode, struct file *file)
974 struct client *client = file->private_data;
975 struct event *e, *next_e;
976 struct client_resource *r, *next_r;
979 if (client->buffer.pages)
980 fw_iso_buffer_destroy(&client->buffer, client->device->card);
982 if (client->iso_context)
983 fw_iso_context_destroy(client->iso_context);
985 list_for_each_entry_safe(r, next_r, &client->resource_list, link)
986 r->release(client, r);
989 * FIXME: We should wait for the async tasklets to stop
990 * running before freeing the memory.
993 list_for_each_entry_safe(e, next_e, &client->event_list, link)
996 spin_lock_irqsave(&client->device->card->lock, flags);
997 list_del(&client->link);
998 spin_unlock_irqrestore(&client->device->card->lock, flags);
1000 fw_device_put(client->device);
1006 static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1008 struct client *client = file->private_data;
1009 unsigned int mask = 0;
1011 poll_wait(file, &client->wait, pt);
1013 if (fw_device_is_shutdown(client->device))
1014 mask |= POLLHUP | POLLERR;
1015 if (!list_empty(&client->event_list))
1016 mask |= POLLIN | POLLRDNORM;
1021 const struct file_operations fw_device_ops = {
1022 .owner = THIS_MODULE,
1023 .open = fw_device_op_open,
1024 .read = fw_device_op_read,
1025 .unlocked_ioctl = fw_device_op_ioctl,
1026 .poll = fw_device_op_poll,
1027 .release = fw_device_op_release,
1028 .mmap = fw_device_op_mmap,
1030 #ifdef CONFIG_COMPAT
1031 .compat_ioctl = fw_device_op_compat_ioctl,