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/mutex.h>
28 #include <linux/poll.h>
29 #include <linux/preempt.h>
30 #include <linux/time.h>
31 #include <linux/spinlock.h>
32 #include <linux/delay.h>
34 #include <linux/idr.h>
35 #include <linux/compat.h>
36 #include <linux/firewire-cdev.h>
37 #include <asm/system.h>
38 #include <asm/uaccess.h>
39 #include "fw-transaction.h"
40 #include "fw-topology.h"
41 #include "fw-device.h"
44 struct client_resource;
45 typedef void (*client_resource_release_fn_t)(struct client *,
46 struct client_resource *);
47 struct client_resource {
48 client_resource_release_fn_t release;
53 * dequeue_event() just kfree()'s the event, so the event has to be
54 * the first field in the struct.
58 struct { void *data; size_t size; } v[2];
59 struct list_head link;
64 struct fw_cdev_event_bus_reset reset;
69 struct fw_transaction transaction;
70 struct client *client;
71 struct client_resource resource;
72 struct fw_cdev_event_response response;
75 struct iso_interrupt {
77 struct fw_cdev_event_iso_interrupt interrupt;
82 struct fw_device *device;
86 struct idr resource_idr;
87 struct list_head event_list;
88 wait_queue_head_t wait;
89 u64 bus_reset_closure;
91 struct fw_iso_context *iso_context;
93 struct fw_iso_buffer buffer;
94 unsigned long vm_start;
96 struct list_head link;
99 static inline void __user *u64_to_uptr(__u64 value)
101 return (void __user *)(unsigned long)value;
104 static inline __u64 uptr_to_u64(void __user *ptr)
106 return (__u64)(unsigned long)ptr;
109 static int fw_device_op_open(struct inode *inode, struct file *file)
111 struct fw_device *device;
112 struct client *client;
114 device = fw_device_get_by_devt(inode->i_rdev);
118 if (fw_device_is_shutdown(device)) {
119 fw_device_put(device);
123 client = kzalloc(sizeof(*client), GFP_KERNEL);
124 if (client == NULL) {
125 fw_device_put(device);
129 client->device = device;
130 spin_lock_init(&client->lock);
131 idr_init(&client->resource_idr);
132 INIT_LIST_HEAD(&client->event_list);
133 init_waitqueue_head(&client->wait);
135 file->private_data = client;
137 mutex_lock(&device->client_list_mutex);
138 list_add_tail(&client->link, &device->client_list);
139 mutex_unlock(&device->client_list_mutex);
144 static void queue_event(struct client *client, struct event *event,
145 void *data0, size_t size0, void *data1, size_t size1)
149 event->v[0].data = data0;
150 event->v[0].size = size0;
151 event->v[1].data = data1;
152 event->v[1].size = size1;
154 spin_lock_irqsave(&client->lock, flags);
155 if (client->in_shutdown)
158 list_add_tail(&event->link, &client->event_list);
159 spin_unlock_irqrestore(&client->lock, flags);
161 wake_up_interruptible(&client->wait);
164 static int dequeue_event(struct client *client,
165 char __user *buffer, size_t count)
172 ret = wait_event_interruptible(client->wait,
173 !list_empty(&client->event_list) ||
174 fw_device_is_shutdown(client->device));
178 if (list_empty(&client->event_list) &&
179 fw_device_is_shutdown(client->device))
182 spin_lock_irqsave(&client->lock, flags);
183 event = container_of(client->event_list.next, struct event, link);
184 list_del(&event->link);
185 spin_unlock_irqrestore(&client->lock, flags);
188 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
189 size = min(event->v[i].size, count - total);
190 if (copy_to_user(buffer + total, event->v[i].data, size)) {
204 static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
205 size_t count, loff_t *offset)
207 struct client *client = file->private_data;
209 return dequeue_event(client, buffer, count);
212 static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
213 struct client *client)
215 struct fw_card *card = client->device->card;
218 spin_lock_irqsave(&card->lock, flags);
220 event->closure = client->bus_reset_closure;
221 event->type = FW_CDEV_EVENT_BUS_RESET;
222 event->generation = client->device->generation;
223 event->node_id = client->device->node_id;
224 event->local_node_id = card->local_node->node_id;
225 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
226 event->irm_node_id = card->irm_node->node_id;
227 event->root_node_id = card->root_node->node_id;
229 spin_unlock_irqrestore(&card->lock, flags);
232 static void for_each_client(struct fw_device *device,
233 void (*callback)(struct client *client))
237 mutex_lock(&device->client_list_mutex);
238 list_for_each_entry(c, &device->client_list, link)
240 mutex_unlock(&device->client_list_mutex);
243 static void queue_bus_reset_event(struct client *client)
245 struct bus_reset *bus_reset;
247 bus_reset = kzalloc(sizeof(*bus_reset), GFP_KERNEL);
248 if (bus_reset == NULL) {
249 fw_notify("Out of memory when allocating bus reset event\n");
253 fill_bus_reset_event(&bus_reset->reset, client);
255 queue_event(client, &bus_reset->event,
256 &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
259 void fw_device_cdev_update(struct fw_device *device)
261 for_each_client(device, queue_bus_reset_event);
264 static void wake_up_client(struct client *client)
266 wake_up_interruptible(&client->wait);
269 void fw_device_cdev_remove(struct fw_device *device)
271 for_each_client(device, wake_up_client);
274 static int ioctl_get_info(struct client *client, void *buffer)
276 struct fw_cdev_get_info *get_info = buffer;
277 struct fw_cdev_event_bus_reset bus_reset;
278 unsigned long ret = 0;
280 client->version = get_info->version;
281 get_info->version = FW_CDEV_VERSION;
282 get_info->card = client->device->card->index;
284 down_read(&fw_device_rwsem);
286 if (get_info->rom != 0) {
287 void __user *uptr = u64_to_uptr(get_info->rom);
288 size_t want = get_info->rom_length;
289 size_t have = client->device->config_rom_length * 4;
291 ret = copy_to_user(uptr, client->device->config_rom,
294 get_info->rom_length = client->device->config_rom_length * 4;
296 up_read(&fw_device_rwsem);
301 client->bus_reset_closure = get_info->bus_reset_closure;
302 if (get_info->bus_reset != 0) {
303 void __user *uptr = u64_to_uptr(get_info->bus_reset);
305 fill_bus_reset_event(&bus_reset, client);
306 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
313 static int add_client_resource(struct client *client,
314 struct client_resource *resource, gfp_t gfp_mask)
320 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
323 spin_lock_irqsave(&client->lock, flags);
324 if (client->in_shutdown)
327 ret = idr_get_new(&client->resource_idr, resource,
329 spin_unlock_irqrestore(&client->lock, flags);
334 return ret < 0 ? ret : 0;
337 static int release_client_resource(struct client *client, u32 handle,
338 client_resource_release_fn_t release,
339 struct client_resource **resource)
341 struct client_resource *r;
344 spin_lock_irqsave(&client->lock, flags);
345 if (client->in_shutdown)
348 r = idr_find(&client->resource_idr, handle);
349 if (r && r->release == release)
350 idr_remove(&client->resource_idr, handle);
351 spin_unlock_irqrestore(&client->lock, flags);
353 if (!(r && r->release == release))
359 r->release(client, r);
364 static void release_transaction(struct client *client,
365 struct client_resource *resource)
367 struct response *response =
368 container_of(resource, struct response, resource);
370 fw_cancel_transaction(client->device->card, &response->transaction);
373 static void complete_transaction(struct fw_card *card, int rcode,
374 void *payload, size_t length, void *data)
376 struct response *response = data;
377 struct client *client = response->client;
379 struct fw_cdev_event_response *r = &response->response;
381 if (length < r->length)
383 if (rcode == RCODE_COMPLETE)
384 memcpy(r->data, payload, r->length);
386 spin_lock_irqsave(&client->lock, flags);
388 * If called while in shutdown, the idr tree must be left untouched.
389 * The idr handle will be removed later.
391 if (!client->in_shutdown)
392 idr_remove(&client->resource_idr, response->resource.handle);
393 spin_unlock_irqrestore(&client->lock, flags);
395 r->type = FW_CDEV_EVENT_RESPONSE;
399 * In the case that sizeof(*r) doesn't align with the position of the
400 * data, and the read is short, preserve an extra copy of the data
401 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
402 * for short reads and some apps depended on it, this is both safe
403 * and prudent for compatibility.
405 if (r->length <= sizeof(*r) - offsetof(typeof(*r), data))
406 queue_event(client, &response->event, r, sizeof(*r),
409 queue_event(client, &response->event, r, sizeof(*r) + r->length,
413 static int ioctl_send_request(struct client *client, void *buffer)
415 struct fw_device *device = client->device;
416 struct fw_cdev_send_request *request = buffer;
417 struct response *response;
420 /* What is the biggest size we'll accept, really? */
421 if (request->length > 4096)
424 response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
425 if (response == NULL)
428 response->client = client;
429 response->response.length = request->length;
430 response->response.closure = request->closure;
433 copy_from_user(response->response.data,
434 u64_to_uptr(request->data), request->length)) {
439 switch (request->tcode) {
440 case TCODE_WRITE_QUADLET_REQUEST:
441 case TCODE_WRITE_BLOCK_REQUEST:
442 case TCODE_READ_QUADLET_REQUEST:
443 case TCODE_READ_BLOCK_REQUEST:
444 case TCODE_LOCK_MASK_SWAP:
445 case TCODE_LOCK_COMPARE_SWAP:
446 case TCODE_LOCK_FETCH_ADD:
447 case TCODE_LOCK_LITTLE_ADD:
448 case TCODE_LOCK_BOUNDED_ADD:
449 case TCODE_LOCK_WRAP_ADD:
450 case TCODE_LOCK_VENDOR_DEPENDENT:
457 response->resource.release = release_transaction;
458 ret = add_client_resource(client, &response->resource, GFP_KERNEL);
462 fw_send_request(device->card, &response->transaction,
463 request->tcode & 0x1f,
464 device->node->node_id,
468 response->response.data, request->length,
469 complete_transaction, response);
472 return sizeof(request) + request->length;
474 return sizeof(request);
481 struct address_handler {
482 struct fw_address_handler handler;
484 struct client *client;
485 struct client_resource resource;
489 struct fw_request *request;
492 struct client_resource resource;
495 struct request_event {
497 struct fw_cdev_event_request request;
500 static void release_request(struct client *client,
501 struct client_resource *resource)
503 struct request *request =
504 container_of(resource, struct request, resource);
506 fw_send_response(client->device->card, request->request,
507 RCODE_CONFLICT_ERROR);
511 static void handle_request(struct fw_card *card, struct fw_request *r,
512 int tcode, int destination, int source,
513 int generation, int speed,
514 unsigned long long offset,
515 void *payload, size_t length, void *callback_data)
517 struct address_handler *handler = callback_data;
518 struct request *request;
519 struct request_event *e;
520 struct client *client = handler->client;
523 request = kmalloc(sizeof(*request), GFP_ATOMIC);
524 e = kmalloc(sizeof(*e), GFP_ATOMIC);
525 if (request == NULL || e == NULL)
528 request->request = r;
529 request->data = payload;
530 request->length = length;
532 request->resource.release = release_request;
533 ret = add_client_resource(client, &request->resource, GFP_ATOMIC);
537 e->request.type = FW_CDEV_EVENT_REQUEST;
538 e->request.tcode = tcode;
539 e->request.offset = offset;
540 e->request.length = length;
541 e->request.handle = request->resource.handle;
542 e->request.closure = handler->closure;
544 queue_event(client, &e->event,
545 &e->request, sizeof(e->request), payload, length);
551 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
554 static void release_address_handler(struct client *client,
555 struct client_resource *resource)
557 struct address_handler *handler =
558 container_of(resource, struct address_handler, resource);
560 fw_core_remove_address_handler(&handler->handler);
564 static int ioctl_allocate(struct client *client, void *buffer)
566 struct fw_cdev_allocate *request = buffer;
567 struct address_handler *handler;
568 struct fw_address_region region;
571 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
575 region.start = request->offset;
576 region.end = request->offset + request->length;
577 handler->handler.length = request->length;
578 handler->handler.address_callback = handle_request;
579 handler->handler.callback_data = handler;
580 handler->closure = request->closure;
581 handler->client = client;
583 ret = fw_core_add_address_handler(&handler->handler, ®ion);
589 handler->resource.release = release_address_handler;
590 ret = add_client_resource(client, &handler->resource, GFP_KERNEL);
592 release_address_handler(client, &handler->resource);
595 request->handle = handler->resource.handle;
600 static int ioctl_deallocate(struct client *client, void *buffer)
602 struct fw_cdev_deallocate *request = buffer;
604 return release_client_resource(client, request->handle,
605 release_address_handler, NULL);
608 static int ioctl_send_response(struct client *client, void *buffer)
610 struct fw_cdev_send_response *request = buffer;
611 struct client_resource *resource;
614 if (release_client_resource(client, request->handle,
615 release_request, &resource) < 0)
618 r = container_of(resource, struct request, resource);
619 if (request->length < r->length)
620 r->length = request->length;
621 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
624 fw_send_response(client->device->card, r->request, request->rcode);
630 static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
632 struct fw_cdev_initiate_bus_reset *request = buffer;
635 short_reset = (request->type == FW_CDEV_SHORT_RESET);
637 return fw_core_initiate_bus_reset(client->device->card, short_reset);
641 struct fw_descriptor d;
642 struct client_resource resource;
646 static void release_descriptor(struct client *client,
647 struct client_resource *resource)
649 struct descriptor *descriptor =
650 container_of(resource, struct descriptor, resource);
652 fw_core_remove_descriptor(&descriptor->d);
656 static int ioctl_add_descriptor(struct client *client, void *buffer)
658 struct fw_cdev_add_descriptor *request = buffer;
659 struct descriptor *descriptor;
662 if (request->length > 256)
666 kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
667 if (descriptor == NULL)
670 if (copy_from_user(descriptor->data,
671 u64_to_uptr(request->data), request->length * 4)) {
676 descriptor->d.length = request->length;
677 descriptor->d.immediate = request->immediate;
678 descriptor->d.key = request->key;
679 descriptor->d.data = descriptor->data;
681 ret = fw_core_add_descriptor(&descriptor->d);
685 descriptor->resource.release = release_descriptor;
686 ret = add_client_resource(client, &descriptor->resource, GFP_KERNEL);
688 fw_core_remove_descriptor(&descriptor->d);
691 request->handle = descriptor->resource.handle;
700 static int ioctl_remove_descriptor(struct client *client, void *buffer)
702 struct fw_cdev_remove_descriptor *request = buffer;
704 return release_client_resource(client, request->handle,
705 release_descriptor, NULL);
708 static void iso_callback(struct fw_iso_context *context, u32 cycle,
709 size_t header_length, void *header, void *data)
711 struct client *client = data;
712 struct iso_interrupt *irq;
714 irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
718 irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
719 irq->interrupt.closure = client->iso_closure;
720 irq->interrupt.cycle = cycle;
721 irq->interrupt.header_length = header_length;
722 memcpy(irq->interrupt.header, header, header_length);
723 queue_event(client, &irq->event, &irq->interrupt,
724 sizeof(irq->interrupt) + header_length, NULL, 0);
727 static int ioctl_create_iso_context(struct client *client, void *buffer)
729 struct fw_cdev_create_iso_context *request = buffer;
730 struct fw_iso_context *context;
732 /* We only support one context at this time. */
733 if (client->iso_context != NULL)
736 if (request->channel > 63)
739 switch (request->type) {
740 case FW_ISO_CONTEXT_RECEIVE:
741 if (request->header_size < 4 || (request->header_size & 3))
746 case FW_ISO_CONTEXT_TRANSMIT:
747 if (request->speed > SCODE_3200)
756 context = fw_iso_context_create(client->device->card,
760 request->header_size,
761 iso_callback, client);
763 return PTR_ERR(context);
765 client->iso_closure = request->closure;
766 client->iso_context = context;
768 /* We only support one context at this time. */
774 /* Macros for decoding the iso packet control header. */
775 #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
776 #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
777 #define GET_SKIP(v) (((v) >> 17) & 0x01)
778 #define GET_TAG(v) (((v) >> 18) & 0x03)
779 #define GET_SY(v) (((v) >> 20) & 0x0f)
780 #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
782 static int ioctl_queue_iso(struct client *client, void *buffer)
784 struct fw_cdev_queue_iso *request = buffer;
785 struct fw_cdev_iso_packet __user *p, *end, *next;
786 struct fw_iso_context *ctx = client->iso_context;
787 unsigned long payload, buffer_end, header_length;
791 struct fw_iso_packet packet;
795 if (ctx == NULL || request->handle != 0)
799 * If the user passes a non-NULL data pointer, has mmap()'ed
800 * the iso buffer, and the pointer points inside the buffer,
801 * we setup the payload pointers accordingly. Otherwise we
802 * set them both to 0, which will still let packets with
803 * payload_length == 0 through. In other words, if no packets
804 * use the indirect payload, the iso buffer need not be mapped
805 * and the request->data pointer is ignored.
808 payload = (unsigned long)request->data - client->vm_start;
809 buffer_end = client->buffer.page_count << PAGE_SHIFT;
810 if (request->data == 0 || client->buffer.pages == NULL ||
811 payload >= buffer_end) {
816 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
818 if (!access_ok(VERIFY_READ, p, request->size))
821 end = (void __user *)p + request->size;
824 if (get_user(control, &p->control))
826 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
827 u.packet.interrupt = GET_INTERRUPT(control);
828 u.packet.skip = GET_SKIP(control);
829 u.packet.tag = GET_TAG(control);
830 u.packet.sy = GET_SY(control);
831 u.packet.header_length = GET_HEADER_LENGTH(control);
833 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
834 header_length = u.packet.header_length;
837 * We require that header_length is a multiple of
838 * the fixed header size, ctx->header_size.
840 if (ctx->header_size == 0) {
841 if (u.packet.header_length > 0)
843 } else if (u.packet.header_length % ctx->header_size != 0) {
849 next = (struct fw_cdev_iso_packet __user *)
850 &p->header[header_length / 4];
854 (u.packet.header, p->header, header_length))
856 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
857 u.packet.header_length + u.packet.payload_length > 0)
859 if (payload + u.packet.payload_length > buffer_end)
862 if (fw_iso_context_queue(ctx, &u.packet,
863 &client->buffer, payload))
867 payload += u.packet.payload_length;
871 request->size -= uptr_to_u64(p) - request->packets;
872 request->packets = uptr_to_u64(p);
873 request->data = client->vm_start + payload;
878 static int ioctl_start_iso(struct client *client, void *buffer)
880 struct fw_cdev_start_iso *request = buffer;
882 if (client->iso_context == NULL || request->handle != 0)
885 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
886 if (request->tags == 0 || request->tags > 15)
889 if (request->sync > 15)
893 return fw_iso_context_start(client->iso_context, request->cycle,
894 request->sync, request->tags);
897 static int ioctl_stop_iso(struct client *client, void *buffer)
899 struct fw_cdev_stop_iso *request = buffer;
901 if (client->iso_context == NULL || request->handle != 0)
904 return fw_iso_context_stop(client->iso_context);
907 static int ioctl_get_cycle_timer(struct client *client, void *buffer)
909 struct fw_cdev_get_cycle_timer *request = buffer;
910 struct fw_card *card = client->device->card;
911 unsigned long long bus_time;
916 local_irq_save(flags);
918 bus_time = card->driver->get_bus_time(card);
919 do_gettimeofday(&tv);
921 local_irq_restore(flags);
924 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
925 request->cycle_timer = bus_time & 0xffffffff;
929 static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
935 ioctl_initiate_bus_reset,
936 ioctl_add_descriptor,
937 ioctl_remove_descriptor,
938 ioctl_create_iso_context,
942 ioctl_get_cycle_timer,
945 static int dispatch_ioctl(struct client *client,
946 unsigned int cmd, void __user *arg)
951 if (_IOC_TYPE(cmd) != '#' ||
952 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
955 if (_IOC_DIR(cmd) & _IOC_WRITE) {
956 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
957 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
961 ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
965 if (_IOC_DIR(cmd) & _IOC_READ) {
966 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
967 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
974 static long fw_device_op_ioctl(struct file *file,
975 unsigned int cmd, unsigned long arg)
977 struct client *client = file->private_data;
979 if (fw_device_is_shutdown(client->device))
982 return dispatch_ioctl(client, cmd, (void __user *) arg);
986 static long fw_device_op_compat_ioctl(struct file *file,
987 unsigned int cmd, unsigned long arg)
989 struct client *client = file->private_data;
991 if (fw_device_is_shutdown(client->device))
994 return dispatch_ioctl(client, cmd, compat_ptr(arg));
998 static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1000 struct client *client = file->private_data;
1001 enum dma_data_direction direction;
1003 int page_count, ret;
1005 if (fw_device_is_shutdown(client->device))
1008 /* FIXME: We could support multiple buffers, but we don't. */
1009 if (client->buffer.pages != NULL)
1012 if (!(vma->vm_flags & VM_SHARED))
1015 if (vma->vm_start & ~PAGE_MASK)
1018 client->vm_start = vma->vm_start;
1019 size = vma->vm_end - vma->vm_start;
1020 page_count = size >> PAGE_SHIFT;
1021 if (size & ~PAGE_MASK)
1024 if (vma->vm_flags & VM_WRITE)
1025 direction = DMA_TO_DEVICE;
1027 direction = DMA_FROM_DEVICE;
1029 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1030 page_count, direction);
1034 ret = fw_iso_buffer_map(&client->buffer, vma);
1036 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1041 static int shutdown_resource(int id, void *p, void *data)
1043 struct client_resource *r = p;
1044 struct client *client = data;
1046 r->release(client, r);
1051 static int fw_device_op_release(struct inode *inode, struct file *file)
1053 struct client *client = file->private_data;
1054 struct event *e, *next_e;
1055 unsigned long flags;
1057 mutex_lock(&client->device->client_list_mutex);
1058 list_del(&client->link);
1059 mutex_unlock(&client->device->client_list_mutex);
1061 if (client->buffer.pages)
1062 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1064 if (client->iso_context)
1065 fw_iso_context_destroy(client->iso_context);
1067 /* Freeze client->resource_idr and client->event_list */
1068 spin_lock_irqsave(&client->lock, flags);
1069 client->in_shutdown = true;
1070 spin_unlock_irqrestore(&client->lock, flags);
1072 idr_for_each(&client->resource_idr, shutdown_resource, client);
1073 idr_remove_all(&client->resource_idr);
1074 idr_destroy(&client->resource_idr);
1076 list_for_each_entry_safe(e, next_e, &client->event_list, link)
1080 * FIXME: client should be reference-counted. It's extremely unlikely
1081 * but there may still be transactions being completed at this point.
1083 fw_device_put(client->device);
1089 static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1091 struct client *client = file->private_data;
1092 unsigned int mask = 0;
1094 poll_wait(file, &client->wait, pt);
1096 if (fw_device_is_shutdown(client->device))
1097 mask |= POLLHUP | POLLERR;
1098 if (!list_empty(&client->event_list))
1099 mask |= POLLIN | POLLRDNORM;
1104 const struct file_operations fw_device_ops = {
1105 .owner = THIS_MODULE,
1106 .open = fw_device_op_open,
1107 .read = fw_device_op_read,
1108 .unlocked_ioctl = fw_device_op_ioctl,
1109 .poll = fw_device_op_poll,
1110 .release = fw_device_op_release,
1111 .mmap = fw_device_op_mmap,
1113 #ifdef CONFIG_COMPAT
1114 .compat_ioctl = fw_device_op_compat_ioctl,