firewire: cdev: freeze FW_CDEV_VERSION due to libraw1394 bug
[pandora-kernel.git] / drivers / firewire / core-cdev.c
1 /*
2  * Char device for device raw access
3  *
4  * Copyright (C) 2005-2007  Kristian Hoegsberg <krh@bitplanet.net>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include <linux/compat.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/errno.h>
25 #include <linux/firewire.h>
26 #include <linux/firewire-cdev.h>
27 #include <linux/idr.h>
28 #include <linux/irqflags.h>
29 #include <linux/jiffies.h>
30 #include <linux/kernel.h>
31 #include <linux/kref.h>
32 #include <linux/mm.h>
33 #include <linux/module.h>
34 #include <linux/mutex.h>
35 #include <linux/poll.h>
36 #include <linux/sched.h>
37 #include <linux/spinlock.h>
38 #include <linux/string.h>
39 #include <linux/time.h>
40 #include <linux/uaccess.h>
41 #include <linux/vmalloc.h>
42 #include <linux/wait.h>
43 #include <linux/workqueue.h>
44
45 #include <asm/system.h>
46
47 #include "core.h"
48
49 /*
50  * ABI version history is documented in linux/firewire-cdev.h.
51  */
52 #define FW_CDEV_KERNEL_VERSION 3
53
54 struct client {
55         u32 version;
56         struct fw_device *device;
57
58         spinlock_t lock;
59         bool in_shutdown;
60         struct idr resource_idr;
61         struct list_head event_list;
62         wait_queue_head_t wait;
63         u64 bus_reset_closure;
64
65         struct fw_iso_context *iso_context;
66         u64 iso_closure;
67         struct fw_iso_buffer buffer;
68         unsigned long vm_start;
69
70         struct list_head link;
71         struct kref kref;
72 };
73
74 static inline void client_get(struct client *client)
75 {
76         kref_get(&client->kref);
77 }
78
79 static void client_release(struct kref *kref)
80 {
81         struct client *client = container_of(kref, struct client, kref);
82
83         fw_device_put(client->device);
84         kfree(client);
85 }
86
87 static void client_put(struct client *client)
88 {
89         kref_put(&client->kref, client_release);
90 }
91
92 struct client_resource;
93 typedef void (*client_resource_release_fn_t)(struct client *,
94                                              struct client_resource *);
95 struct client_resource {
96         client_resource_release_fn_t release;
97         int handle;
98 };
99
100 struct address_handler_resource {
101         struct client_resource resource;
102         struct fw_address_handler handler;
103         __u64 closure;
104         struct client *client;
105 };
106
107 struct outbound_transaction_resource {
108         struct client_resource resource;
109         struct fw_transaction transaction;
110 };
111
112 struct inbound_transaction_resource {
113         struct client_resource resource;
114         struct fw_card *card;
115         struct fw_request *request;
116         void *data;
117         size_t length;
118 };
119
120 struct descriptor_resource {
121         struct client_resource resource;
122         struct fw_descriptor descriptor;
123         u32 data[0];
124 };
125
126 struct iso_resource {
127         struct client_resource resource;
128         struct client *client;
129         /* Schedule work and access todo only with client->lock held. */
130         struct delayed_work work;
131         enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
132               ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
133         int generation;
134         u64 channels;
135         s32 bandwidth;
136         __be32 transaction_data[2];
137         struct iso_resource_event *e_alloc, *e_dealloc;
138 };
139
140 static void release_iso_resource(struct client *, struct client_resource *);
141
142 static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
143 {
144         client_get(r->client);
145         if (!schedule_delayed_work(&r->work, delay))
146                 client_put(r->client);
147 }
148
149 static void schedule_if_iso_resource(struct client_resource *resource)
150 {
151         if (resource->release == release_iso_resource)
152                 schedule_iso_resource(container_of(resource,
153                                         struct iso_resource, resource), 0);
154 }
155
156 /*
157  * dequeue_event() just kfree()'s the event, so the event has to be
158  * the first field in a struct XYZ_event.
159  */
160 struct event {
161         struct { void *data; size_t size; } v[2];
162         struct list_head link;
163 };
164
165 struct bus_reset_event {
166         struct event event;
167         struct fw_cdev_event_bus_reset reset;
168 };
169
170 struct outbound_transaction_event {
171         struct event event;
172         struct client *client;
173         struct outbound_transaction_resource r;
174         struct fw_cdev_event_response response;
175 };
176
177 struct inbound_transaction_event {
178         struct event event;
179         struct fw_cdev_event_request request;
180 };
181
182 struct iso_interrupt_event {
183         struct event event;
184         struct fw_cdev_event_iso_interrupt interrupt;
185 };
186
187 struct iso_resource_event {
188         struct event event;
189         struct fw_cdev_event_iso_resource iso_resource;
190 };
191
192 static inline void __user *u64_to_uptr(__u64 value)
193 {
194         return (void __user *)(unsigned long)value;
195 }
196
197 static inline __u64 uptr_to_u64(void __user *ptr)
198 {
199         return (__u64)(unsigned long)ptr;
200 }
201
202 static int fw_device_op_open(struct inode *inode, struct file *file)
203 {
204         struct fw_device *device;
205         struct client *client;
206
207         device = fw_device_get_by_devt(inode->i_rdev);
208         if (device == NULL)
209                 return -ENODEV;
210
211         if (fw_device_is_shutdown(device)) {
212                 fw_device_put(device);
213                 return -ENODEV;
214         }
215
216         client = kzalloc(sizeof(*client), GFP_KERNEL);
217         if (client == NULL) {
218                 fw_device_put(device);
219                 return -ENOMEM;
220         }
221
222         client->device = device;
223         spin_lock_init(&client->lock);
224         idr_init(&client->resource_idr);
225         INIT_LIST_HEAD(&client->event_list);
226         init_waitqueue_head(&client->wait);
227         kref_init(&client->kref);
228
229         file->private_data = client;
230
231         mutex_lock(&device->client_list_mutex);
232         list_add_tail(&client->link, &device->client_list);
233         mutex_unlock(&device->client_list_mutex);
234
235         return nonseekable_open(inode, file);
236 }
237
238 static void queue_event(struct client *client, struct event *event,
239                         void *data0, size_t size0, void *data1, size_t size1)
240 {
241         unsigned long flags;
242
243         event->v[0].data = data0;
244         event->v[0].size = size0;
245         event->v[1].data = data1;
246         event->v[1].size = size1;
247
248         spin_lock_irqsave(&client->lock, flags);
249         if (client->in_shutdown)
250                 kfree(event);
251         else
252                 list_add_tail(&event->link, &client->event_list);
253         spin_unlock_irqrestore(&client->lock, flags);
254
255         wake_up_interruptible(&client->wait);
256 }
257
258 static int dequeue_event(struct client *client,
259                          char __user *buffer, size_t count)
260 {
261         struct event *event;
262         size_t size, total;
263         int i, ret;
264
265         ret = wait_event_interruptible(client->wait,
266                         !list_empty(&client->event_list) ||
267                         fw_device_is_shutdown(client->device));
268         if (ret < 0)
269                 return ret;
270
271         if (list_empty(&client->event_list) &&
272                        fw_device_is_shutdown(client->device))
273                 return -ENODEV;
274
275         spin_lock_irq(&client->lock);
276         event = list_first_entry(&client->event_list, struct event, link);
277         list_del(&event->link);
278         spin_unlock_irq(&client->lock);
279
280         total = 0;
281         for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
282                 size = min(event->v[i].size, count - total);
283                 if (copy_to_user(buffer + total, event->v[i].data, size)) {
284                         ret = -EFAULT;
285                         goto out;
286                 }
287                 total += size;
288         }
289         ret = total;
290
291  out:
292         kfree(event);
293
294         return ret;
295 }
296
297 static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
298                                  size_t count, loff_t *offset)
299 {
300         struct client *client = file->private_data;
301
302         return dequeue_event(client, buffer, count);
303 }
304
305 static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
306                                  struct client *client)
307 {
308         struct fw_card *card = client->device->card;
309
310         spin_lock_irq(&card->lock);
311
312         event->closure       = client->bus_reset_closure;
313         event->type          = FW_CDEV_EVENT_BUS_RESET;
314         event->generation    = client->device->generation;
315         event->node_id       = client->device->node_id;
316         event->local_node_id = card->local_node->node_id;
317         event->bm_node_id    = 0; /* FIXME: We don't track the BM. */
318         event->irm_node_id   = card->irm_node->node_id;
319         event->root_node_id  = card->root_node->node_id;
320
321         spin_unlock_irq(&card->lock);
322 }
323
324 static void for_each_client(struct fw_device *device,
325                             void (*callback)(struct client *client))
326 {
327         struct client *c;
328
329         mutex_lock(&device->client_list_mutex);
330         list_for_each_entry(c, &device->client_list, link)
331                 callback(c);
332         mutex_unlock(&device->client_list_mutex);
333 }
334
335 static int schedule_reallocations(int id, void *p, void *data)
336 {
337         schedule_if_iso_resource(p);
338
339         return 0;
340 }
341
342 static void queue_bus_reset_event(struct client *client)
343 {
344         struct bus_reset_event *e;
345
346         e = kzalloc(sizeof(*e), GFP_KERNEL);
347         if (e == NULL) {
348                 fw_notify("Out of memory when allocating bus reset event\n");
349                 return;
350         }
351
352         fill_bus_reset_event(&e->reset, client);
353
354         queue_event(client, &e->event,
355                     &e->reset, sizeof(e->reset), NULL, 0);
356
357         spin_lock_irq(&client->lock);
358         idr_for_each(&client->resource_idr, schedule_reallocations, client);
359         spin_unlock_irq(&client->lock);
360 }
361
362 void fw_device_cdev_update(struct fw_device *device)
363 {
364         for_each_client(device, queue_bus_reset_event);
365 }
366
367 static void wake_up_client(struct client *client)
368 {
369         wake_up_interruptible(&client->wait);
370 }
371
372 void fw_device_cdev_remove(struct fw_device *device)
373 {
374         for_each_client(device, wake_up_client);
375 }
376
377 union ioctl_arg {
378         struct fw_cdev_get_info                 get_info;
379         struct fw_cdev_send_request             send_request;
380         struct fw_cdev_allocate                 allocate;
381         struct fw_cdev_deallocate               deallocate;
382         struct fw_cdev_send_response            send_response;
383         struct fw_cdev_initiate_bus_reset       initiate_bus_reset;
384         struct fw_cdev_add_descriptor           add_descriptor;
385         struct fw_cdev_remove_descriptor        remove_descriptor;
386         struct fw_cdev_create_iso_context       create_iso_context;
387         struct fw_cdev_queue_iso                queue_iso;
388         struct fw_cdev_start_iso                start_iso;
389         struct fw_cdev_stop_iso                 stop_iso;
390         struct fw_cdev_get_cycle_timer          get_cycle_timer;
391         struct fw_cdev_allocate_iso_resource    allocate_iso_resource;
392         struct fw_cdev_send_stream_packet       send_stream_packet;
393         struct fw_cdev_get_cycle_timer2         get_cycle_timer2;
394 };
395
396 static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
397 {
398         struct fw_cdev_get_info *a = &arg->get_info;
399         struct fw_cdev_event_bus_reset bus_reset;
400         unsigned long ret = 0;
401
402         client->version = a->version;
403         a->version = FW_CDEV_KERNEL_VERSION;
404         a->card = client->device->card->index;
405
406         down_read(&fw_device_rwsem);
407
408         if (a->rom != 0) {
409                 size_t want = a->rom_length;
410                 size_t have = client->device->config_rom_length * 4;
411
412                 ret = copy_to_user(u64_to_uptr(a->rom),
413                                    client->device->config_rom, min(want, have));
414         }
415         a->rom_length = client->device->config_rom_length * 4;
416
417         up_read(&fw_device_rwsem);
418
419         if (ret != 0)
420                 return -EFAULT;
421
422         client->bus_reset_closure = a->bus_reset_closure;
423         if (a->bus_reset != 0) {
424                 fill_bus_reset_event(&bus_reset, client);
425                 if (copy_to_user(u64_to_uptr(a->bus_reset),
426                                  &bus_reset, sizeof(bus_reset)))
427                         return -EFAULT;
428         }
429
430         return 0;
431 }
432
433 static int add_client_resource(struct client *client,
434                                struct client_resource *resource, gfp_t gfp_mask)
435 {
436         unsigned long flags;
437         int ret;
438
439  retry:
440         if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
441                 return -ENOMEM;
442
443         spin_lock_irqsave(&client->lock, flags);
444         if (client->in_shutdown)
445                 ret = -ECANCELED;
446         else
447                 ret = idr_get_new(&client->resource_idr, resource,
448                                   &resource->handle);
449         if (ret >= 0) {
450                 client_get(client);
451                 schedule_if_iso_resource(resource);
452         }
453         spin_unlock_irqrestore(&client->lock, flags);
454
455         if (ret == -EAGAIN)
456                 goto retry;
457
458         return ret < 0 ? ret : 0;
459 }
460
461 static int release_client_resource(struct client *client, u32 handle,
462                                    client_resource_release_fn_t release,
463                                    struct client_resource **return_resource)
464 {
465         struct client_resource *resource;
466
467         spin_lock_irq(&client->lock);
468         if (client->in_shutdown)
469                 resource = NULL;
470         else
471                 resource = idr_find(&client->resource_idr, handle);
472         if (resource && resource->release == release)
473                 idr_remove(&client->resource_idr, handle);
474         spin_unlock_irq(&client->lock);
475
476         if (!(resource && resource->release == release))
477                 return -EINVAL;
478
479         if (return_resource)
480                 *return_resource = resource;
481         else
482                 resource->release(client, resource);
483
484         client_put(client);
485
486         return 0;
487 }
488
489 static void release_transaction(struct client *client,
490                                 struct client_resource *resource)
491 {
492         struct outbound_transaction_resource *r = container_of(resource,
493                         struct outbound_transaction_resource, resource);
494
495         fw_cancel_transaction(client->device->card, &r->transaction);
496 }
497
498 static void complete_transaction(struct fw_card *card, int rcode,
499                                  void *payload, size_t length, void *data)
500 {
501         struct outbound_transaction_event *e = data;
502         struct fw_cdev_event_response *rsp = &e->response;
503         struct client *client = e->client;
504         unsigned long flags;
505
506         if (length < rsp->length)
507                 rsp->length = length;
508         if (rcode == RCODE_COMPLETE)
509                 memcpy(rsp->data, payload, rsp->length);
510
511         spin_lock_irqsave(&client->lock, flags);
512         /*
513          * 1. If called while in shutdown, the idr tree must be left untouched.
514          *    The idr handle will be removed and the client reference will be
515          *    dropped later.
516          * 2. If the call chain was release_client_resource ->
517          *    release_transaction -> complete_transaction (instead of a normal
518          *    conclusion of the transaction), i.e. if this resource was already
519          *    unregistered from the idr, the client reference will be dropped
520          *    by release_client_resource and we must not drop it here.
521          */
522         if (!client->in_shutdown &&
523             idr_find(&client->resource_idr, e->r.resource.handle)) {
524                 idr_remove(&client->resource_idr, e->r.resource.handle);
525                 /* Drop the idr's reference */
526                 client_put(client);
527         }
528         spin_unlock_irqrestore(&client->lock, flags);
529
530         rsp->type = FW_CDEV_EVENT_RESPONSE;
531         rsp->rcode = rcode;
532
533         /*
534          * In the case that sizeof(*rsp) doesn't align with the position of the
535          * data, and the read is short, preserve an extra copy of the data
536          * to stay compatible with a pre-2.6.27 bug.  Since the bug is harmless
537          * for short reads and some apps depended on it, this is both safe
538          * and prudent for compatibility.
539          */
540         if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
541                 queue_event(client, &e->event, rsp, sizeof(*rsp),
542                             rsp->data, rsp->length);
543         else
544                 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
545                             NULL, 0);
546
547         /* Drop the transaction callback's reference */
548         client_put(client);
549 }
550
551 static int init_request(struct client *client,
552                         struct fw_cdev_send_request *request,
553                         int destination_id, int speed)
554 {
555         struct outbound_transaction_event *e;
556         int ret;
557
558         if (request->tcode != TCODE_STREAM_DATA &&
559             (request->length > 4096 || request->length > 512 << speed))
560                 return -EIO;
561
562         e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
563         if (e == NULL)
564                 return -ENOMEM;
565
566         e->client = client;
567         e->response.length = request->length;
568         e->response.closure = request->closure;
569
570         if (request->data &&
571             copy_from_user(e->response.data,
572                            u64_to_uptr(request->data), request->length)) {
573                 ret = -EFAULT;
574                 goto failed;
575         }
576
577         e->r.resource.release = release_transaction;
578         ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
579         if (ret < 0)
580                 goto failed;
581
582         /* Get a reference for the transaction callback */
583         client_get(client);
584
585         fw_send_request(client->device->card, &e->r.transaction,
586                         request->tcode, destination_id, request->generation,
587                         speed, request->offset, e->response.data,
588                         request->length, complete_transaction, e);
589         return 0;
590
591  failed:
592         kfree(e);
593
594         return ret;
595 }
596
597 static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
598 {
599         switch (arg->send_request.tcode) {
600         case TCODE_WRITE_QUADLET_REQUEST:
601         case TCODE_WRITE_BLOCK_REQUEST:
602         case TCODE_READ_QUADLET_REQUEST:
603         case TCODE_READ_BLOCK_REQUEST:
604         case TCODE_LOCK_MASK_SWAP:
605         case TCODE_LOCK_COMPARE_SWAP:
606         case TCODE_LOCK_FETCH_ADD:
607         case TCODE_LOCK_LITTLE_ADD:
608         case TCODE_LOCK_BOUNDED_ADD:
609         case TCODE_LOCK_WRAP_ADD:
610         case TCODE_LOCK_VENDOR_DEPENDENT:
611                 break;
612         default:
613                 return -EINVAL;
614         }
615
616         return init_request(client, &arg->send_request, client->device->node_id,
617                             client->device->max_speed);
618 }
619
620 static inline bool is_fcp_request(struct fw_request *request)
621 {
622         return request == NULL;
623 }
624
625 static void release_request(struct client *client,
626                             struct client_resource *resource)
627 {
628         struct inbound_transaction_resource *r = container_of(resource,
629                         struct inbound_transaction_resource, resource);
630
631         if (is_fcp_request(r->request))
632                 kfree(r->data);
633         else
634                 fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
635
636         fw_card_put(r->card);
637         kfree(r);
638 }
639
640 static void handle_request(struct fw_card *card, struct fw_request *request,
641                            int tcode, int destination, int source,
642                            int generation, unsigned long long offset,
643                            void *payload, size_t length, void *callback_data)
644 {
645         struct address_handler_resource *handler = callback_data;
646         struct inbound_transaction_resource *r;
647         struct inbound_transaction_event *e;
648         void *fcp_frame = NULL;
649         int ret;
650
651         /* card may be different from handler->client->device->card */
652         fw_card_get(card);
653
654         r = kmalloc(sizeof(*r), GFP_ATOMIC);
655         e = kmalloc(sizeof(*e), GFP_ATOMIC);
656         if (r == NULL || e == NULL)
657                 goto failed;
658
659         r->card    = card;
660         r->request = request;
661         r->data    = payload;
662         r->length  = length;
663
664         if (is_fcp_request(request)) {
665                 /*
666                  * FIXME: Let core-transaction.c manage a
667                  * single reference-counted copy?
668                  */
669                 fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
670                 if (fcp_frame == NULL)
671                         goto failed;
672
673                 r->data = fcp_frame;
674         }
675
676         r->resource.release = release_request;
677         ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
678         if (ret < 0)
679                 goto failed;
680
681         e->request.type    = FW_CDEV_EVENT_REQUEST;
682         e->request.tcode   = tcode;
683         e->request.offset  = offset;
684         e->request.length  = length;
685         e->request.handle  = r->resource.handle;
686         e->request.closure = handler->closure;
687
688         queue_event(handler->client, &e->event,
689                     &e->request, sizeof(e->request), r->data, length);
690         return;
691
692  failed:
693         kfree(r);
694         kfree(e);
695         kfree(fcp_frame);
696
697         if (!is_fcp_request(request))
698                 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
699
700         fw_card_put(card);
701 }
702
703 static void release_address_handler(struct client *client,
704                                     struct client_resource *resource)
705 {
706         struct address_handler_resource *r =
707             container_of(resource, struct address_handler_resource, resource);
708
709         fw_core_remove_address_handler(&r->handler);
710         kfree(r);
711 }
712
713 static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
714 {
715         struct fw_cdev_allocate *a = &arg->allocate;
716         struct address_handler_resource *r;
717         struct fw_address_region region;
718         int ret;
719
720         r = kmalloc(sizeof(*r), GFP_KERNEL);
721         if (r == NULL)
722                 return -ENOMEM;
723
724         region.start = a->offset;
725         region.end   = a->offset + a->length;
726         r->handler.length           = a->length;
727         r->handler.address_callback = handle_request;
728         r->handler.callback_data    = r;
729         r->closure   = a->closure;
730         r->client    = client;
731
732         ret = fw_core_add_address_handler(&r->handler, &region);
733         if (ret < 0) {
734                 kfree(r);
735                 return ret;
736         }
737
738         r->resource.release = release_address_handler;
739         ret = add_client_resource(client, &r->resource, GFP_KERNEL);
740         if (ret < 0) {
741                 release_address_handler(client, &r->resource);
742                 return ret;
743         }
744         a->handle = r->resource.handle;
745
746         return 0;
747 }
748
749 static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
750 {
751         return release_client_resource(client, arg->deallocate.handle,
752                                        release_address_handler, NULL);
753 }
754
755 static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
756 {
757         struct fw_cdev_send_response *a = &arg->send_response;
758         struct client_resource *resource;
759         struct inbound_transaction_resource *r;
760         int ret = 0;
761
762         if (release_client_resource(client, a->handle,
763                                     release_request, &resource) < 0)
764                 return -EINVAL;
765
766         r = container_of(resource, struct inbound_transaction_resource,
767                          resource);
768         if (is_fcp_request(r->request))
769                 goto out;
770
771         if (a->length != fw_get_response_length(r->request)) {
772                 ret = -EINVAL;
773                 kfree(r->request);
774                 goto out;
775         }
776         if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
777                 ret = -EFAULT;
778                 kfree(r->request);
779                 goto out;
780         }
781         fw_send_response(r->card, r->request, a->rcode);
782  out:
783         fw_card_put(r->card);
784         kfree(r);
785
786         return ret;
787 }
788
789 static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
790 {
791         return fw_core_initiate_bus_reset(client->device->card,
792                         arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
793 }
794
795 static void release_descriptor(struct client *client,
796                                struct client_resource *resource)
797 {
798         struct descriptor_resource *r =
799                 container_of(resource, struct descriptor_resource, resource);
800
801         fw_core_remove_descriptor(&r->descriptor);
802         kfree(r);
803 }
804
805 static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
806 {
807         struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
808         struct descriptor_resource *r;
809         int ret;
810
811         /* Access policy: Allow this ioctl only on local nodes' device files. */
812         if (!client->device->is_local)
813                 return -ENOSYS;
814
815         if (a->length > 256)
816                 return -EINVAL;
817
818         r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
819         if (r == NULL)
820                 return -ENOMEM;
821
822         if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
823                 ret = -EFAULT;
824                 goto failed;
825         }
826
827         r->descriptor.length    = a->length;
828         r->descriptor.immediate = a->immediate;
829         r->descriptor.key       = a->key;
830         r->descriptor.data      = r->data;
831
832         ret = fw_core_add_descriptor(&r->descriptor);
833         if (ret < 0)
834                 goto failed;
835
836         r->resource.release = release_descriptor;
837         ret = add_client_resource(client, &r->resource, GFP_KERNEL);
838         if (ret < 0) {
839                 fw_core_remove_descriptor(&r->descriptor);
840                 goto failed;
841         }
842         a->handle = r->resource.handle;
843
844         return 0;
845  failed:
846         kfree(r);
847
848         return ret;
849 }
850
851 static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
852 {
853         return release_client_resource(client, arg->remove_descriptor.handle,
854                                        release_descriptor, NULL);
855 }
856
857 static void iso_callback(struct fw_iso_context *context, u32 cycle,
858                          size_t header_length, void *header, void *data)
859 {
860         struct client *client = data;
861         struct iso_interrupt_event *e;
862
863         e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
864         if (e == NULL)
865                 return;
866
867         e->interrupt.type      = FW_CDEV_EVENT_ISO_INTERRUPT;
868         e->interrupt.closure   = client->iso_closure;
869         e->interrupt.cycle     = cycle;
870         e->interrupt.header_length = header_length;
871         memcpy(e->interrupt.header, header, header_length);
872         queue_event(client, &e->event, &e->interrupt,
873                     sizeof(e->interrupt) + header_length, NULL, 0);
874 }
875
876 static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
877 {
878         struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
879         struct fw_iso_context *context;
880
881         if (a->channel > 63)
882                 return -EINVAL;
883
884         switch (a->type) {
885         case FW_ISO_CONTEXT_RECEIVE:
886                 if (a->header_size < 4 || (a->header_size & 3))
887                         return -EINVAL;
888                 break;
889
890         case FW_ISO_CONTEXT_TRANSMIT:
891                 if (a->speed > SCODE_3200)
892                         return -EINVAL;
893                 break;
894
895         default:
896                 return -EINVAL;
897         }
898
899         context = fw_iso_context_create(client->device->card, a->type,
900                                         a->channel, a->speed, a->header_size,
901                                         iso_callback, client);
902         if (IS_ERR(context))
903                 return PTR_ERR(context);
904
905         /* We only support one context at this time. */
906         spin_lock_irq(&client->lock);
907         if (client->iso_context != NULL) {
908                 spin_unlock_irq(&client->lock);
909                 fw_iso_context_destroy(context);
910                 return -EBUSY;
911         }
912         client->iso_closure = a->closure;
913         client->iso_context = context;
914         spin_unlock_irq(&client->lock);
915
916         a->handle = 0;
917
918         return 0;
919 }
920
921 /* Macros for decoding the iso packet control header. */
922 #define GET_PAYLOAD_LENGTH(v)   ((v) & 0xffff)
923 #define GET_INTERRUPT(v)        (((v) >> 16) & 0x01)
924 #define GET_SKIP(v)             (((v) >> 17) & 0x01)
925 #define GET_TAG(v)              (((v) >> 18) & 0x03)
926 #define GET_SY(v)               (((v) >> 20) & 0x0f)
927 #define GET_HEADER_LENGTH(v)    (((v) >> 24) & 0xff)
928
929 static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
930 {
931         struct fw_cdev_queue_iso *a = &arg->queue_iso;
932         struct fw_cdev_iso_packet __user *p, *end, *next;
933         struct fw_iso_context *ctx = client->iso_context;
934         unsigned long payload, buffer_end, header_length;
935         u32 control;
936         int count;
937         struct {
938                 struct fw_iso_packet packet;
939                 u8 header[256];
940         } u;
941
942         if (ctx == NULL || a->handle != 0)
943                 return -EINVAL;
944
945         /*
946          * If the user passes a non-NULL data pointer, has mmap()'ed
947          * the iso buffer, and the pointer points inside the buffer,
948          * we setup the payload pointers accordingly.  Otherwise we
949          * set them both to 0, which will still let packets with
950          * payload_length == 0 through.  In other words, if no packets
951          * use the indirect payload, the iso buffer need not be mapped
952          * and the a->data pointer is ignored.
953          */
954
955         payload = (unsigned long)a->data - client->vm_start;
956         buffer_end = client->buffer.page_count << PAGE_SHIFT;
957         if (a->data == 0 || client->buffer.pages == NULL ||
958             payload >= buffer_end) {
959                 payload = 0;
960                 buffer_end = 0;
961         }
962
963         p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
964
965         if (!access_ok(VERIFY_READ, p, a->size))
966                 return -EFAULT;
967
968         end = (void __user *)p + a->size;
969         count = 0;
970         while (p < end) {
971                 if (get_user(control, &p->control))
972                         return -EFAULT;
973                 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
974                 u.packet.interrupt = GET_INTERRUPT(control);
975                 u.packet.skip = GET_SKIP(control);
976                 u.packet.tag = GET_TAG(control);
977                 u.packet.sy = GET_SY(control);
978                 u.packet.header_length = GET_HEADER_LENGTH(control);
979
980                 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
981                         if (u.packet.header_length % 4 != 0)
982                                 return -EINVAL;
983                         header_length = u.packet.header_length;
984                 } else {
985                         /*
986                          * We require that header_length is a multiple of
987                          * the fixed header size, ctx->header_size.
988                          */
989                         if (ctx->header_size == 0) {
990                                 if (u.packet.header_length > 0)
991                                         return -EINVAL;
992                         } else if (u.packet.header_length == 0 ||
993                                    u.packet.header_length % ctx->header_size != 0) {
994                                 return -EINVAL;
995                         }
996                         header_length = 0;
997                 }
998
999                 next = (struct fw_cdev_iso_packet __user *)
1000                         &p->header[header_length / 4];
1001                 if (next > end)
1002                         return -EINVAL;
1003                 if (__copy_from_user
1004                     (u.packet.header, p->header, header_length))
1005                         return -EFAULT;
1006                 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
1007                     u.packet.header_length + u.packet.payload_length > 0)
1008                         return -EINVAL;
1009                 if (payload + u.packet.payload_length > buffer_end)
1010                         return -EINVAL;
1011
1012                 if (fw_iso_context_queue(ctx, &u.packet,
1013                                          &client->buffer, payload))
1014                         break;
1015
1016                 p = next;
1017                 payload += u.packet.payload_length;
1018                 count++;
1019         }
1020
1021         a->size    -= uptr_to_u64(p) - a->packets;
1022         a->packets  = uptr_to_u64(p);
1023         a->data     = client->vm_start + payload;
1024
1025         return count;
1026 }
1027
1028 static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
1029 {
1030         struct fw_cdev_start_iso *a = &arg->start_iso;
1031
1032         if (client->iso_context == NULL || a->handle != 0)
1033                 return -EINVAL;
1034
1035         if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
1036             (a->tags == 0 || a->tags > 15 || a->sync > 15))
1037                 return -EINVAL;
1038
1039         return fw_iso_context_start(client->iso_context,
1040                                     a->cycle, a->sync, a->tags);
1041 }
1042
1043 static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
1044 {
1045         struct fw_cdev_stop_iso *a = &arg->stop_iso;
1046
1047         if (client->iso_context == NULL || a->handle != 0)
1048                 return -EINVAL;
1049
1050         return fw_iso_context_stop(client->iso_context);
1051 }
1052
1053 static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
1054 {
1055         struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
1056         struct fw_card *card = client->device->card;
1057         struct timespec ts = {0, 0};
1058         u32 cycle_time;
1059         int ret = 0;
1060
1061         local_irq_disable();
1062
1063         cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
1064
1065         switch (a->clk_id) {
1066         case CLOCK_REALTIME:      getnstimeofday(&ts);                   break;
1067         case CLOCK_MONOTONIC:     do_posix_clock_monotonic_gettime(&ts); break;
1068         case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts);                  break;
1069         default:
1070                 ret = -EINVAL;
1071         }
1072
1073         local_irq_enable();
1074
1075         a->tv_sec      = ts.tv_sec;
1076         a->tv_nsec     = ts.tv_nsec;
1077         a->cycle_timer = cycle_time;
1078
1079         return ret;
1080 }
1081
1082 static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
1083 {
1084         struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
1085         struct fw_cdev_get_cycle_timer2 ct2;
1086
1087         ct2.clk_id = CLOCK_REALTIME;
1088         ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
1089
1090         a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
1091         a->cycle_timer = ct2.cycle_timer;
1092
1093         return 0;
1094 }
1095
1096 static void iso_resource_work(struct work_struct *work)
1097 {
1098         struct iso_resource_event *e;
1099         struct iso_resource *r =
1100                         container_of(work, struct iso_resource, work.work);
1101         struct client *client = r->client;
1102         int generation, channel, bandwidth, todo;
1103         bool skip, free, success;
1104
1105         spin_lock_irq(&client->lock);
1106         generation = client->device->generation;
1107         todo = r->todo;
1108         /* Allow 1000ms grace period for other reallocations. */
1109         if (todo == ISO_RES_ALLOC &&
1110             time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
1111                 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
1112                 skip = true;
1113         } else {
1114                 /* We could be called twice within the same generation. */
1115                 skip = todo == ISO_RES_REALLOC &&
1116                        r->generation == generation;
1117         }
1118         free = todo == ISO_RES_DEALLOC ||
1119                todo == ISO_RES_ALLOC_ONCE ||
1120                todo == ISO_RES_DEALLOC_ONCE;
1121         r->generation = generation;
1122         spin_unlock_irq(&client->lock);
1123
1124         if (skip)
1125                 goto out;
1126
1127         bandwidth = r->bandwidth;
1128
1129         fw_iso_resource_manage(client->device->card, generation,
1130                         r->channels, &channel, &bandwidth,
1131                         todo == ISO_RES_ALLOC ||
1132                         todo == ISO_RES_REALLOC ||
1133                         todo == ISO_RES_ALLOC_ONCE,
1134                         r->transaction_data);
1135         /*
1136          * Is this generation outdated already?  As long as this resource sticks
1137          * in the idr, it will be scheduled again for a newer generation or at
1138          * shutdown.
1139          */
1140         if (channel == -EAGAIN &&
1141             (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1142                 goto out;
1143
1144         success = channel >= 0 || bandwidth > 0;
1145
1146         spin_lock_irq(&client->lock);
1147         /*
1148          * Transit from allocation to reallocation, except if the client
1149          * requested deallocation in the meantime.
1150          */
1151         if (r->todo == ISO_RES_ALLOC)
1152                 r->todo = ISO_RES_REALLOC;
1153         /*
1154          * Allocation or reallocation failure?  Pull this resource out of the
1155          * idr and prepare for deletion, unless the client is shutting down.
1156          */
1157         if (r->todo == ISO_RES_REALLOC && !success &&
1158             !client->in_shutdown &&
1159             idr_find(&client->resource_idr, r->resource.handle)) {
1160                 idr_remove(&client->resource_idr, r->resource.handle);
1161                 client_put(client);
1162                 free = true;
1163         }
1164         spin_unlock_irq(&client->lock);
1165
1166         if (todo == ISO_RES_ALLOC && channel >= 0)
1167                 r->channels = 1ULL << channel;
1168
1169         if (todo == ISO_RES_REALLOC && success)
1170                 goto out;
1171
1172         if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
1173                 e = r->e_alloc;
1174                 r->e_alloc = NULL;
1175         } else {
1176                 e = r->e_dealloc;
1177                 r->e_dealloc = NULL;
1178         }
1179         e->iso_resource.handle    = r->resource.handle;
1180         e->iso_resource.channel   = channel;
1181         e->iso_resource.bandwidth = bandwidth;
1182
1183         queue_event(client, &e->event,
1184                     &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
1185
1186         if (free) {
1187                 cancel_delayed_work(&r->work);
1188                 kfree(r->e_alloc);
1189                 kfree(r->e_dealloc);
1190                 kfree(r);
1191         }
1192  out:
1193         client_put(client);
1194 }
1195
1196 static void release_iso_resource(struct client *client,
1197                                  struct client_resource *resource)
1198 {
1199         struct iso_resource *r =
1200                 container_of(resource, struct iso_resource, resource);
1201
1202         spin_lock_irq(&client->lock);
1203         r->todo = ISO_RES_DEALLOC;
1204         schedule_iso_resource(r, 0);
1205         spin_unlock_irq(&client->lock);
1206 }
1207
1208 static int init_iso_resource(struct client *client,
1209                 struct fw_cdev_allocate_iso_resource *request, int todo)
1210 {
1211         struct iso_resource_event *e1, *e2;
1212         struct iso_resource *r;
1213         int ret;
1214
1215         if ((request->channels == 0 && request->bandwidth == 0) ||
1216             request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1217             request->bandwidth < 0)
1218                 return -EINVAL;
1219
1220         r  = kmalloc(sizeof(*r), GFP_KERNEL);
1221         e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1222         e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1223         if (r == NULL || e1 == NULL || e2 == NULL) {
1224                 ret = -ENOMEM;
1225                 goto fail;
1226         }
1227
1228         INIT_DELAYED_WORK(&r->work, iso_resource_work);
1229         r->client       = client;
1230         r->todo         = todo;
1231         r->generation   = -1;
1232         r->channels     = request->channels;
1233         r->bandwidth    = request->bandwidth;
1234         r->e_alloc      = e1;
1235         r->e_dealloc    = e2;
1236
1237         e1->iso_resource.closure = request->closure;
1238         e1->iso_resource.type    = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1239         e2->iso_resource.closure = request->closure;
1240         e2->iso_resource.type    = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
1241
1242         if (todo == ISO_RES_ALLOC) {
1243                 r->resource.release = release_iso_resource;
1244                 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
1245                 if (ret < 0)
1246                         goto fail;
1247         } else {
1248                 r->resource.release = NULL;
1249                 r->resource.handle = -1;
1250                 schedule_iso_resource(r, 0);
1251         }
1252         request->handle = r->resource.handle;
1253
1254         return 0;
1255  fail:
1256         kfree(r);
1257         kfree(e1);
1258         kfree(e2);
1259
1260         return ret;
1261 }
1262
1263 static int ioctl_allocate_iso_resource(struct client *client,
1264                                        union ioctl_arg *arg)
1265 {
1266         return init_iso_resource(client,
1267                         &arg->allocate_iso_resource, ISO_RES_ALLOC);
1268 }
1269
1270 static int ioctl_deallocate_iso_resource(struct client *client,
1271                                          union ioctl_arg *arg)
1272 {
1273         return release_client_resource(client,
1274                         arg->deallocate.handle, release_iso_resource, NULL);
1275 }
1276
1277 static int ioctl_allocate_iso_resource_once(struct client *client,
1278                                             union ioctl_arg *arg)
1279 {
1280         return init_iso_resource(client,
1281                         &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
1282 }
1283
1284 static int ioctl_deallocate_iso_resource_once(struct client *client,
1285                                               union ioctl_arg *arg)
1286 {
1287         return init_iso_resource(client,
1288                         &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
1289 }
1290
1291 /*
1292  * Returns a speed code:  Maximum speed to or from this device,
1293  * limited by the device's link speed, the local node's link speed,
1294  * and all PHY port speeds between the two links.
1295  */
1296 static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
1297 {
1298         return client->device->max_speed;
1299 }
1300
1301 static int ioctl_send_broadcast_request(struct client *client,
1302                                         union ioctl_arg *arg)
1303 {
1304         struct fw_cdev_send_request *a = &arg->send_request;
1305
1306         switch (a->tcode) {
1307         case TCODE_WRITE_QUADLET_REQUEST:
1308         case TCODE_WRITE_BLOCK_REQUEST:
1309                 break;
1310         default:
1311                 return -EINVAL;
1312         }
1313
1314         /* Security policy: Only allow accesses to Units Space. */
1315         if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
1316                 return -EACCES;
1317
1318         return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
1319 }
1320
1321 static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
1322 {
1323         struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
1324         struct fw_cdev_send_request request;
1325         int dest;
1326
1327         if (a->speed > client->device->card->link_speed ||
1328             a->length > 1024 << a->speed)
1329                 return -EIO;
1330
1331         if (a->tag > 3 || a->channel > 63 || a->sy > 15)
1332                 return -EINVAL;
1333
1334         dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
1335         request.tcode           = TCODE_STREAM_DATA;
1336         request.length          = a->length;
1337         request.closure         = a->closure;
1338         request.data            = a->data;
1339         request.generation      = a->generation;
1340
1341         return init_request(client, &request, dest, a->speed);
1342 }
1343
1344 static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
1345         ioctl_get_info,
1346         ioctl_send_request,
1347         ioctl_allocate,
1348         ioctl_deallocate,
1349         ioctl_send_response,
1350         ioctl_initiate_bus_reset,
1351         ioctl_add_descriptor,
1352         ioctl_remove_descriptor,
1353         ioctl_create_iso_context,
1354         ioctl_queue_iso,
1355         ioctl_start_iso,
1356         ioctl_stop_iso,
1357         ioctl_get_cycle_timer,
1358         ioctl_allocate_iso_resource,
1359         ioctl_deallocate_iso_resource,
1360         ioctl_allocate_iso_resource_once,
1361         ioctl_deallocate_iso_resource_once,
1362         ioctl_get_speed,
1363         ioctl_send_broadcast_request,
1364         ioctl_send_stream_packet,
1365         ioctl_get_cycle_timer2,
1366 };
1367
1368 static int dispatch_ioctl(struct client *client,
1369                           unsigned int cmd, void __user *arg)
1370 {
1371         union ioctl_arg buffer;
1372         int ret;
1373
1374         if (fw_device_is_shutdown(client->device))
1375                 return -ENODEV;
1376
1377         if (_IOC_TYPE(cmd) != '#' ||
1378             _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
1379             _IOC_SIZE(cmd) > sizeof(buffer))
1380                 return -EINVAL;
1381
1382         if (_IOC_DIR(cmd) == _IOC_READ)
1383                 memset(&buffer, 0, _IOC_SIZE(cmd));
1384
1385         if (_IOC_DIR(cmd) & _IOC_WRITE)
1386                 if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
1387                         return -EFAULT;
1388
1389         ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
1390         if (ret < 0)
1391                 return ret;
1392
1393         if (_IOC_DIR(cmd) & _IOC_READ)
1394                 if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
1395                         return -EFAULT;
1396
1397         return ret;
1398 }
1399
1400 static long fw_device_op_ioctl(struct file *file,
1401                                unsigned int cmd, unsigned long arg)
1402 {
1403         return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
1404 }
1405
1406 #ifdef CONFIG_COMPAT
1407 static long fw_device_op_compat_ioctl(struct file *file,
1408                                       unsigned int cmd, unsigned long arg)
1409 {
1410         return dispatch_ioctl(file->private_data, cmd, compat_ptr(arg));
1411 }
1412 #endif
1413
1414 static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1415 {
1416         struct client *client = file->private_data;
1417         enum dma_data_direction direction;
1418         unsigned long size;
1419         int page_count, ret;
1420
1421         if (fw_device_is_shutdown(client->device))
1422                 return -ENODEV;
1423
1424         /* FIXME: We could support multiple buffers, but we don't. */
1425         if (client->buffer.pages != NULL)
1426                 return -EBUSY;
1427
1428         if (!(vma->vm_flags & VM_SHARED))
1429                 return -EINVAL;
1430
1431         if (vma->vm_start & ~PAGE_MASK)
1432                 return -EINVAL;
1433
1434         client->vm_start = vma->vm_start;
1435         size = vma->vm_end - vma->vm_start;
1436         page_count = size >> PAGE_SHIFT;
1437         if (size & ~PAGE_MASK)
1438                 return -EINVAL;
1439
1440         if (vma->vm_flags & VM_WRITE)
1441                 direction = DMA_TO_DEVICE;
1442         else
1443                 direction = DMA_FROM_DEVICE;
1444
1445         ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1446                                  page_count, direction);
1447         if (ret < 0)
1448                 return ret;
1449
1450         ret = fw_iso_buffer_map(&client->buffer, vma);
1451         if (ret < 0)
1452                 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1453
1454         return ret;
1455 }
1456
1457 static int shutdown_resource(int id, void *p, void *data)
1458 {
1459         struct client_resource *resource = p;
1460         struct client *client = data;
1461
1462         resource->release(client, resource);
1463         client_put(client);
1464
1465         return 0;
1466 }
1467
1468 static int fw_device_op_release(struct inode *inode, struct file *file)
1469 {
1470         struct client *client = file->private_data;
1471         struct event *event, *next_event;
1472
1473         mutex_lock(&client->device->client_list_mutex);
1474         list_del(&client->link);
1475         mutex_unlock(&client->device->client_list_mutex);
1476
1477         if (client->iso_context)
1478                 fw_iso_context_destroy(client->iso_context);
1479
1480         if (client->buffer.pages)
1481                 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1482
1483         /* Freeze client->resource_idr and client->event_list */
1484         spin_lock_irq(&client->lock);
1485         client->in_shutdown = true;
1486         spin_unlock_irq(&client->lock);
1487
1488         idr_for_each(&client->resource_idr, shutdown_resource, client);
1489         idr_remove_all(&client->resource_idr);
1490         idr_destroy(&client->resource_idr);
1491
1492         list_for_each_entry_safe(event, next_event, &client->event_list, link)
1493                 kfree(event);
1494
1495         client_put(client);
1496
1497         return 0;
1498 }
1499
1500 static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1501 {
1502         struct client *client = file->private_data;
1503         unsigned int mask = 0;
1504
1505         poll_wait(file, &client->wait, pt);
1506
1507         if (fw_device_is_shutdown(client->device))
1508                 mask |= POLLHUP | POLLERR;
1509         if (!list_empty(&client->event_list))
1510                 mask |= POLLIN | POLLRDNORM;
1511
1512         return mask;
1513 }
1514
1515 const struct file_operations fw_device_ops = {
1516         .owner          = THIS_MODULE,
1517         .llseek         = no_llseek,
1518         .open           = fw_device_op_open,
1519         .read           = fw_device_op_read,
1520         .unlocked_ioctl = fw_device_op_ioctl,
1521         .mmap           = fw_device_op_mmap,
1522         .release        = fw_device_op_release,
1523         .poll           = fw_device_op_poll,
1524 #ifdef CONFIG_COMPAT
1525         .compat_ioctl   = fw_device_op_compat_ioctl,
1526 #endif
1527 };