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