firewire: Add sysfs attributes for config rom directory values.
[linux-2.6] / drivers / firewire / fw-device-cdev.c
1 /*                                              -*- c-basic-offset: 8 -*-
2  *
3  * fw-device-cdev.c - Char device for device raw access
4  *
5  * Copyright (C) 2005-2006  Kristian Hoegsberg <krh@bitplanet.net>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/wait.h>
25 #include <linux/errno.h>
26 #include <linux/device.h>
27 #include <linux/vmalloc.h>
28 #include <linux/poll.h>
29 #include <linux/delay.h>
30 #include <linux/mm.h>
31 #include <linux/idr.h>
32 #include <linux/compat.h>
33 #include <asm/uaccess.h>
34 #include "fw-transaction.h"
35 #include "fw-topology.h"
36 #include "fw-device.h"
37 #include "fw-device-cdev.h"
38
39 /*
40  * todo
41  *
42  * - bus resets sends a new packet with new generation and node id
43  *
44  */
45
46 /* dequeue_event() just kfree()'s the event, so the event has to be
47  * the first field in the struct. */
48
49 struct event {
50         struct { void *data; size_t size; } v[2];
51         struct list_head link;
52 };
53
54 struct bus_reset {
55         struct event event;
56         struct fw_cdev_event_bus_reset reset;
57 };
58
59 struct response {
60         struct event event;
61         struct fw_transaction transaction;
62         struct client *client;
63         struct list_head link;
64         struct fw_cdev_event_response response;
65 };
66
67 struct iso_interrupt {
68         struct event event;
69         struct fw_cdev_event_iso_interrupt interrupt;
70 };
71
72 struct client {
73         u32 version;
74         struct fw_device *device;
75         spinlock_t lock;
76         struct list_head handler_list;
77         struct list_head request_list;
78         struct list_head transaction_list;
79         u32 request_serial;
80         struct list_head event_list;
81         wait_queue_head_t wait;
82
83         struct fw_iso_context *iso_context;
84         struct fw_iso_buffer buffer;
85         unsigned long vm_start;
86
87         struct list_head link;
88 };
89
90 static inline void __user *
91 u64_to_uptr(__u64 value)
92 {
93         return (void __user *)(unsigned long)value;
94 }
95
96 static inline __u64
97 uptr_to_u64(void __user *ptr)
98 {
99         return (__u64)(unsigned long)ptr;
100 }
101
102 static int fw_device_op_open(struct inode *inode, struct file *file)
103 {
104         struct fw_device *device;
105         struct client *client;
106         unsigned long flags;
107
108         device = fw_device_from_devt(inode->i_rdev);
109         if (device == NULL)
110                 return -ENODEV;
111
112         client = kzalloc(sizeof *client, GFP_KERNEL);
113         if (client == NULL)
114                 return -ENOMEM;
115
116         client->device = fw_device_get(device);
117         INIT_LIST_HEAD(&client->event_list);
118         INIT_LIST_HEAD(&client->handler_list);
119         INIT_LIST_HEAD(&client->request_list);
120         INIT_LIST_HEAD(&client->transaction_list);
121         spin_lock_init(&client->lock);
122         init_waitqueue_head(&client->wait);
123
124         file->private_data = client;
125
126         spin_lock_irqsave(&device->card->lock, flags);
127         list_add_tail(&client->link, &device->client_list);
128         spin_unlock_irqrestore(&device->card->lock, flags);
129
130         return 0;
131 }
132
133 static void queue_event(struct client *client, struct event *event,
134                         void *data0, size_t size0, void *data1, size_t size1)
135 {
136         unsigned long flags;
137
138         event->v[0].data = data0;
139         event->v[0].size = size0;
140         event->v[1].data = data1;
141         event->v[1].size = size1;
142
143         spin_lock_irqsave(&client->lock, flags);
144
145         list_add_tail(&event->link, &client->event_list);
146         wake_up_interruptible(&client->wait);
147
148         spin_unlock_irqrestore(&client->lock, flags);
149 }
150
151 static int
152 dequeue_event(struct client *client, char __user *buffer, size_t count)
153 {
154         unsigned long flags;
155         struct event *event;
156         size_t size, total;
157         int i, retval;
158
159         retval = wait_event_interruptible(client->wait,
160                                           !list_empty(&client->event_list) ||
161                                           fw_device_is_shutdown(client->device));
162         if (retval < 0)
163                 return retval;
164
165         if (list_empty(&client->event_list) &&
166                        fw_device_is_shutdown(client->device))
167                 return -ENODEV;
168
169         spin_lock_irqsave(&client->lock, flags);
170         event = container_of(client->event_list.next, struct event, link);
171         list_del(&event->link);
172         spin_unlock_irqrestore(&client->lock, flags);
173
174         total = 0;
175         for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
176                 size = min(event->v[i].size, count - total);
177                 if (copy_to_user(buffer + total, event->v[i].data, size)) {
178                         retval = -EFAULT;
179                         goto out;
180                 }
181                 total += size;
182         }
183         retval = total;
184
185  out:
186         kfree(event);
187
188         return retval;
189 }
190
191 static ssize_t
192 fw_device_op_read(struct file *file,
193                   char __user *buffer, size_t count, loff_t *offset)
194 {
195         struct client *client = file->private_data;
196
197         return dequeue_event(client, buffer, count);
198 }
199
200 static void
201 fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
202                      struct fw_device *device)
203 {
204         struct fw_card *card = device->card;
205
206         event->type          = FW_CDEV_EVENT_BUS_RESET;
207         event->node_id       = device->node_id;
208         event->local_node_id = card->local_node->node_id;
209         event->bm_node_id    = 0; /* FIXME: We don't track the BM. */
210         event->irm_node_id   = card->irm_node->node_id;
211         event->root_node_id  = card->root_node->node_id;
212         event->generation    = card->generation;
213 }
214
215 static void
216 for_each_client(struct fw_device *device,
217                 void (*callback)(struct client *client))
218 {
219         struct fw_card *card = device->card;
220         struct client *c;
221         unsigned long flags;
222
223         spin_lock_irqsave(&card->lock, flags);
224
225         list_for_each_entry(c, &device->client_list, link)
226                 callback(c);
227
228         spin_unlock_irqrestore(&card->lock, flags);
229 }
230
231 static void
232 queue_bus_reset_event(struct client *client)
233 {
234         struct bus_reset *bus_reset;
235         struct fw_device *device = client->device;
236
237         bus_reset = kzalloc(sizeof *bus_reset, GFP_ATOMIC);
238         if (bus_reset == NULL) {
239                 fw_notify("Out of memory when allocating bus reset event\n");
240                 return;
241         }
242
243         fill_bus_reset_event(&bus_reset->reset, device);
244
245         queue_event(client, &bus_reset->event,
246                     &bus_reset->reset, sizeof bus_reset->reset, NULL, 0);
247 }
248
249 void fw_device_cdev_update(struct fw_device *device)
250 {
251         for_each_client(device, queue_bus_reset_event);
252 }
253
254 static void wake_up_client(struct client *client)
255 {
256         wake_up_interruptible(&client->wait);
257 }
258
259 void fw_device_cdev_remove(struct fw_device *device)
260 {
261         for_each_client(device, wake_up_client);
262 }
263
264 static int ioctl_get_info(struct client *client, void __user *arg)
265 {
266         struct fw_cdev_get_info get_info;
267         struct fw_cdev_event_bus_reset bus_reset;
268
269         if (copy_from_user(&get_info, arg, sizeof get_info))
270                 return -EFAULT;
271
272         client->version = get_info.version;
273         get_info.version = FW_CDEV_VERSION;
274
275         if (get_info.rom != 0) {
276                 void __user *uptr = u64_to_uptr(get_info.rom);
277                 size_t want = get_info.rom_length;
278                 size_t have = client->device->config_rom_length * 4;
279
280                 if (copy_to_user(uptr, client->device->config_rom,
281                                  min(want, have)))
282                         return -EFAULT;
283         }
284         get_info.rom_length = client->device->config_rom_length * 4;
285
286         if (get_info.bus_reset != 0) {
287                 void __user *uptr = u64_to_uptr(get_info.bus_reset);
288
289                 fill_bus_reset_event(&bus_reset, client->device);
290                 if (copy_to_user(uptr, &bus_reset, sizeof bus_reset))
291                         return -EFAULT;
292         }
293
294         get_info.card = client->device->card->index;
295
296         if (copy_to_user(arg, &get_info, sizeof get_info))
297                 return -EFAULT;
298
299         return 0;
300 }
301
302 static void
303 complete_transaction(struct fw_card *card, int rcode,
304                      void *payload, size_t length, void *data)
305 {
306         struct response *response = data;
307         struct client *client = response->client;
308         unsigned long flags;
309
310         if (length < response->response.length)
311                 response->response.length = length;
312         if (rcode == RCODE_COMPLETE)
313                 memcpy(response->response.data, payload,
314                        response->response.length);
315
316         spin_lock_irqsave(&client->lock, flags);
317         list_del(&response->link);
318         spin_unlock_irqrestore(&client->lock, flags);
319
320         response->response.type   = FW_CDEV_EVENT_RESPONSE;
321         response->response.rcode  = rcode;
322         queue_event(client, &response->event,
323                     &response->response, sizeof response->response,
324                     response->response.data, response->response.length);
325 }
326
327 static ssize_t ioctl_send_request(struct client *client, void __user *arg)
328 {
329         struct fw_device *device = client->device;
330         struct fw_cdev_send_request request;
331         struct response *response;
332         unsigned long flags;
333
334         if (copy_from_user(&request, arg, sizeof request))
335                 return -EFAULT;
336
337         /* What is the biggest size we'll accept, really? */
338         if (request.length > 4096)
339                 return -EINVAL;
340
341         response = kmalloc(sizeof *response + request.length, GFP_KERNEL);
342         if (response == NULL)
343                 return -ENOMEM;
344
345         response->client = client;
346         response->response.length = request.length;
347         response->response.closure = request.closure;
348
349         if (request.data &&
350             copy_from_user(response->response.data,
351                            u64_to_uptr(request.data), request.length)) {
352                 kfree(response);
353                 return -EFAULT;
354         }
355
356         spin_lock_irqsave(&client->lock, flags);
357         list_add_tail(&response->link, &client->transaction_list);
358         spin_unlock_irqrestore(&client->lock, flags);
359
360         fw_send_request(device->card, &response->transaction,
361                         request.tcode & 0x1f,
362                         device->node->node_id,
363                         request.generation,
364                         device->node->max_speed,
365                         request.offset,
366                         response->response.data, request.length,
367                         complete_transaction, response);
368
369         if (request.data)
370                 return sizeof request + request.length;
371         else
372                 return sizeof request;
373 }
374
375 struct address_handler {
376         struct fw_address_handler handler;
377         __u64 closure;
378         struct client *client;
379         struct list_head link;
380 };
381
382 struct request {
383         struct fw_request *request;
384         void *data;
385         size_t length;
386         u32 serial;
387         struct list_head link;
388 };
389
390 struct request_event {
391         struct event event;
392         struct fw_cdev_event_request request;
393 };
394
395 static void
396 handle_request(struct fw_card *card, struct fw_request *r,
397                int tcode, int destination, int source,
398                int generation, int speed,
399                unsigned long long offset,
400                void *payload, size_t length, void *callback_data)
401 {
402         struct address_handler *handler = callback_data;
403         struct request *request;
404         struct request_event *e;
405         unsigned long flags;
406         struct client *client = handler->client;
407
408         request = kmalloc(sizeof *request, GFP_ATOMIC);
409         e = kmalloc(sizeof *e, GFP_ATOMIC);
410         if (request == NULL || e == NULL) {
411                 kfree(request);
412                 kfree(e);
413                 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
414                 return;
415         }
416
417         request->request = r;
418         request->data    = payload;
419         request->length  = length;
420
421         spin_lock_irqsave(&client->lock, flags);
422         request->serial = client->request_serial++;
423         list_add_tail(&request->link, &client->request_list);
424         spin_unlock_irqrestore(&client->lock, flags);
425
426         e->request.type    = FW_CDEV_EVENT_REQUEST;
427         e->request.tcode   = tcode;
428         e->request.offset  = offset;
429         e->request.length  = length;
430         e->request.serial  = request->serial;
431         e->request.closure = handler->closure;
432
433         queue_event(client, &e->event,
434                     &e->request, sizeof e->request, payload, length);
435 }
436
437 static int ioctl_allocate(struct client *client, void __user *arg)
438 {
439         struct fw_cdev_allocate request;
440         struct address_handler *handler;
441         unsigned long flags;
442         struct fw_address_region region;
443
444         if (copy_from_user(&request, arg, sizeof request))
445                 return -EFAULT;
446
447         handler = kmalloc(sizeof *handler, GFP_KERNEL);
448         if (handler == NULL)
449                 return -ENOMEM;
450
451         region.start = request.offset;
452         region.end = request.offset + request.length;
453         handler->handler.length = request.length;
454         handler->handler.address_callback = handle_request;
455         handler->handler.callback_data = handler;
456         handler->closure = request.closure;
457         handler->client = client;
458
459         if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
460                 kfree(handler);
461                 return -EBUSY;
462         }
463
464         spin_lock_irqsave(&client->lock, flags);
465         list_add_tail(&handler->link, &client->handler_list);
466         spin_unlock_irqrestore(&client->lock, flags);
467
468         return 0;
469 }
470
471 static int ioctl_deallocate(struct client *client, void __user *arg)
472 {
473         struct fw_cdev_deallocate request;
474         struct address_handler *handler;
475         unsigned long flags;
476
477         if (copy_from_user(&request, arg, sizeof request))
478                 return -EFAULT;
479
480         spin_lock_irqsave(&client->lock, flags);
481         list_for_each_entry(handler, &client->handler_list, link) {
482                 if (handler->handler.offset == request.offset) {
483                         list_del(&handler->link);
484                         break;
485                 }
486         }
487         spin_unlock_irqrestore(&client->lock, flags);
488
489         if (&handler->link == &client->handler_list)
490                 return -EINVAL;
491
492         fw_core_remove_address_handler(&handler->handler);
493
494         return 0;
495 }
496
497 static int ioctl_send_response(struct client *client, void __user *arg)
498 {
499         struct fw_cdev_send_response request;
500         struct request *r;
501         unsigned long flags;
502
503         if (copy_from_user(&request, arg, sizeof request))
504                 return -EFAULT;
505
506         spin_lock_irqsave(&client->lock, flags);
507         list_for_each_entry(r, &client->request_list, link) {
508                 if (r->serial == request.serial) {
509                         list_del(&r->link);
510                         break;
511                 }
512         }
513         spin_unlock_irqrestore(&client->lock, flags);
514
515         if (&r->link == &client->request_list)
516                 return -EINVAL;
517
518         if (request.length < r->length)
519                 r->length = request.length;
520         if (copy_from_user(r->data, u64_to_uptr(request.data), r->length))
521                 return -EFAULT;
522
523         fw_send_response(client->device->card, r->request, request.rcode);
524
525         kfree(r);
526
527         return 0;
528 }
529
530 static int ioctl_initiate_bus_reset(struct client *client, void __user *arg)
531 {
532         struct fw_cdev_initiate_bus_reset request;
533         int short_reset;
534
535         if (copy_from_user(&request, arg, sizeof request))
536                 return -EFAULT;
537
538         short_reset = (request.type == FW_CDEV_SHORT_RESET);
539
540         return fw_core_initiate_bus_reset(client->device->card, short_reset);
541 }
542
543 static void
544 iso_callback(struct fw_iso_context *context, u32 cycle,
545              size_t header_length, void *header, void *data)
546 {
547         struct client *client = data;
548         struct iso_interrupt *interrupt;
549
550         interrupt = kzalloc(sizeof *interrupt + header_length, GFP_ATOMIC);
551         if (interrupt == NULL)
552                 return;
553
554         interrupt->interrupt.type      = FW_CDEV_EVENT_ISO_INTERRUPT;
555         interrupt->interrupt.closure   = 0;
556         interrupt->interrupt.cycle     = cycle;
557         interrupt->interrupt.header_length = header_length;
558         memcpy(interrupt->interrupt.header, header, header_length);
559         queue_event(client, &interrupt->event,
560                     &interrupt->interrupt,
561                     sizeof interrupt->interrupt + header_length, NULL, 0);
562 }
563
564 static int ioctl_create_iso_context(struct client *client, void __user *arg)
565 {
566         struct fw_cdev_create_iso_context request;
567
568         if (copy_from_user(&request, arg, sizeof request))
569                 return -EFAULT;
570
571         if (request.channel > 63)
572                 return -EINVAL;
573
574         switch (request.type) {
575         case FW_ISO_CONTEXT_RECEIVE:
576                 if (request.header_size < 4 || (request.header_size & 3))
577                         return -EINVAL;
578
579                 break;
580
581         case FW_ISO_CONTEXT_TRANSMIT:
582                 if (request.speed > SCODE_3200)
583                         return -EINVAL;
584
585                 break;
586
587         default:
588                 return -EINVAL;
589         }
590
591         client->iso_context = fw_iso_context_create(client->device->card,
592                                                     request.type,
593                                                     request.channel,
594                                                     request.speed,
595                                                     request.header_size,
596                                                     iso_callback, client);
597         if (IS_ERR(client->iso_context))
598                 return PTR_ERR(client->iso_context);
599
600         return 0;
601 }
602
603 static int ioctl_queue_iso(struct client *client, void __user *arg)
604 {
605         struct fw_cdev_queue_iso request;
606         struct fw_cdev_iso_packet __user *p, *end, *next;
607         struct fw_iso_context *ctx = client->iso_context;
608         unsigned long payload, payload_end, header_length;
609         int count;
610         struct {
611                 struct fw_iso_packet packet;
612                 u8 header[256];
613         } u;
614
615         if (ctx == NULL)
616                 return -EINVAL;
617         if (copy_from_user(&request, arg, sizeof request))
618                 return -EFAULT;
619
620         /* If the user passes a non-NULL data pointer, has mmap()'ed
621          * the iso buffer, and the pointer points inside the buffer,
622          * we setup the payload pointers accordingly.  Otherwise we
623          * set them both to 0, which will still let packets with
624          * payload_length == 0 through.  In other words, if no packets
625          * use the indirect payload, the iso buffer need not be mapped
626          * and the request.data pointer is ignored.*/
627
628         payload = (unsigned long)request.data - client->vm_start;
629         payload_end = payload + (client->buffer.page_count << PAGE_SHIFT);
630         if (request.data == 0 || client->buffer.pages == NULL ||
631             payload >= payload_end) {
632                 payload = 0;
633                 payload_end = 0;
634         }
635
636         if (!access_ok(VERIFY_READ, request.packets, request.size))
637                 return -EFAULT;
638
639         p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request.packets);
640         end = (void __user *)p + request.size;
641         count = 0;
642         while (p < end) {
643                 if (__copy_from_user(&u.packet, p, sizeof *p))
644                         return -EFAULT;
645
646                 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
647                         header_length = u.packet.header_length;
648                 } else {
649                         /* We require that header_length is a multiple of
650                          * the fixed header size, ctx->header_size */
651                         if (ctx->header_size == 0) {
652                                 if (u.packet.header_length > 0)
653                                         return -EINVAL;
654                         } else if (u.packet.header_length % ctx->header_size != 0) {
655                                 return -EINVAL;
656                         }
657                         header_length = 0;
658                 }
659
660                 next = (struct fw_cdev_iso_packet __user *)
661                         &p->header[header_length / 4];
662                 if (next > end)
663                         return -EINVAL;
664                 if (__copy_from_user
665                     (u.packet.header, p->header, header_length))
666                         return -EFAULT;
667                 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
668                     u.packet.header_length + u.packet.payload_length > 0)
669                         return -EINVAL;
670                 if (payload + u.packet.payload_length > payload_end)
671                         return -EINVAL;
672
673                 if (fw_iso_context_queue(ctx, &u.packet,
674                                          &client->buffer, payload))
675                         break;
676
677                 p = next;
678                 payload += u.packet.payload_length;
679                 count++;
680         }
681
682         request.size    -= uptr_to_u64(p) - request.packets;
683         request.packets  = uptr_to_u64(p);
684         request.data     = client->vm_start + payload;
685
686         if (copy_to_user(arg, &request, sizeof request))
687                 return -EFAULT;
688
689         return count;
690 }
691
692 static int ioctl_start_iso(struct client *client, void __user *arg)
693 {
694         struct fw_cdev_start_iso request;
695
696         if (copy_from_user(&request, arg, sizeof request))
697                 return -EFAULT;
698
699         if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
700                 if (request.tags == 0 || request.tags > 15)
701                         return -EINVAL;
702
703                 if (request.sync > 15)
704                         return -EINVAL;
705         }
706
707         return fw_iso_context_start(client->iso_context,
708                                     request.cycle, request.sync, request.tags);
709 }
710
711 static int ioctl_stop_iso(struct client *client, void __user *arg)
712 {
713         return fw_iso_context_stop(client->iso_context);
714 }
715
716 static int
717 dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
718 {
719         switch (cmd) {
720         case FW_CDEV_IOC_GET_INFO:
721                 return ioctl_get_info(client, arg);
722         case FW_CDEV_IOC_SEND_REQUEST:
723                 return ioctl_send_request(client, arg);
724         case FW_CDEV_IOC_ALLOCATE:
725                 return ioctl_allocate(client, arg);
726         case FW_CDEV_IOC_DEALLOCATE:
727                 return ioctl_deallocate(client, arg);
728         case FW_CDEV_IOC_SEND_RESPONSE:
729                 return ioctl_send_response(client, arg);
730         case FW_CDEV_IOC_INITIATE_BUS_RESET:
731                 return ioctl_initiate_bus_reset(client, arg);
732         case FW_CDEV_IOC_CREATE_ISO_CONTEXT:
733                 return ioctl_create_iso_context(client, arg);
734         case FW_CDEV_IOC_QUEUE_ISO:
735                 return ioctl_queue_iso(client, arg);
736         case FW_CDEV_IOC_START_ISO:
737                 return ioctl_start_iso(client, arg);
738         case FW_CDEV_IOC_STOP_ISO:
739                 return ioctl_stop_iso(client, arg);
740         default:
741                 return -EINVAL;
742         }
743 }
744
745 static long
746 fw_device_op_ioctl(struct file *file,
747                    unsigned int cmd, unsigned long arg)
748 {
749         struct client *client = file->private_data;
750
751         return dispatch_ioctl(client, cmd, (void __user *) arg);
752 }
753
754 #ifdef CONFIG_COMPAT
755 static long
756 fw_device_op_compat_ioctl(struct file *file,
757                           unsigned int cmd, unsigned long arg)
758 {
759         struct client *client = file->private_data;
760
761         return dispatch_ioctl(client, cmd, compat_ptr(arg));
762 }
763 #endif
764
765 static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
766 {
767         struct client *client = file->private_data;
768         enum dma_data_direction direction;
769         unsigned long size;
770         int page_count, retval;
771
772         /* FIXME: We could support multiple buffers, but we don't. */
773         if (client->buffer.pages != NULL)
774                 return -EBUSY;
775
776         if (!(vma->vm_flags & VM_SHARED))
777                 return -EINVAL;
778
779         if (vma->vm_start & ~PAGE_MASK)
780                 return -EINVAL;
781
782         client->vm_start = vma->vm_start;
783         size = vma->vm_end - vma->vm_start;
784         page_count = size >> PAGE_SHIFT;
785         if (size & ~PAGE_MASK)
786                 return -EINVAL;
787
788         if (vma->vm_flags & VM_WRITE)
789                 direction = DMA_TO_DEVICE;
790         else
791                 direction = DMA_FROM_DEVICE;
792
793         retval = fw_iso_buffer_init(&client->buffer, client->device->card,
794                                     page_count, direction);
795         if (retval < 0)
796                 return retval;
797
798         retval = fw_iso_buffer_map(&client->buffer, vma);
799         if (retval < 0)
800                 fw_iso_buffer_destroy(&client->buffer, client->device->card);
801
802         return retval;
803 }
804
805 static int fw_device_op_release(struct inode *inode, struct file *file)
806 {
807         struct client *client = file->private_data;
808         struct address_handler *h, *next_h;
809         struct request *r, *next_r;
810         struct event *e, *next_e;
811         struct response *t, *next_t;
812         unsigned long flags;
813
814         if (client->buffer.pages)
815                 fw_iso_buffer_destroy(&client->buffer, client->device->card);
816
817         if (client->iso_context)
818                 fw_iso_context_destroy(client->iso_context);
819
820         list_for_each_entry_safe(h, next_h, &client->handler_list, link) {
821                 fw_core_remove_address_handler(&h->handler);
822                 kfree(h);
823         }
824
825         list_for_each_entry_safe(r, next_r, &client->request_list, link) {
826                 fw_send_response(client->device->card, r->request,
827                                  RCODE_CONFLICT_ERROR);
828                 kfree(r);
829         }
830
831         list_for_each_entry_safe(t, next_t, &client->transaction_list, link) {
832                 fw_cancel_transaction(client->device->card, &t->transaction);
833                 kfree(t);
834         }
835
836         /* FIXME: We should wait for the async tasklets to stop
837          * running before freeing the memory. */
838
839         list_for_each_entry_safe(e, next_e, &client->event_list, link)
840                 kfree(e);
841
842         spin_lock_irqsave(&client->device->card->lock, flags);
843         list_del(&client->link);
844         spin_unlock_irqrestore(&client->device->card->lock, flags);
845
846         fw_device_put(client->device);
847         kfree(client);
848
849         return 0;
850 }
851
852 static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
853 {
854         struct client *client = file->private_data;
855         unsigned int mask = 0;
856
857         poll_wait(file, &client->wait, pt);
858
859         if (fw_device_is_shutdown(client->device))
860                 mask |= POLLHUP | POLLERR;
861         if (!list_empty(&client->event_list))
862                 mask |= POLLIN | POLLRDNORM;
863
864         return mask;
865 }
866
867 const struct file_operations fw_device_ops = {
868         .owner          = THIS_MODULE,
869         .open           = fw_device_op_open,
870         .read           = fw_device_op_read,
871         .unlocked_ioctl = fw_device_op_ioctl,
872         .poll           = fw_device_op_poll,
873         .release        = fw_device_op_release,
874         .mmap           = fw_device_op_mmap,
875
876 #ifdef CONFIG_COMPAT
877         .compat_ioctl   = fw_device_op_compat_ioctl,
878 #endif
879 };