firewire: add a client_list_lock
[safe/jmp/linux-2.6] / drivers / firewire / fw-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/module.h>
22 #include <linux/kernel.h>
23 #include <linux/wait.h>
24 #include <linux/errno.h>
25 #include <linux/device.h>
26 #include <linux/vmalloc.h>
27 #include <linux/poll.h>
28 #include <linux/preempt.h>
29 #include <linux/time.h>
30 #include <linux/spinlock.h>
31 #include <linux/delay.h>
32 #include <linux/mm.h>
33 #include <linux/idr.h>
34 #include <linux/compat.h>
35 #include <linux/firewire-cdev.h>
36 #include <asm/system.h>
37 #include <asm/uaccess.h>
38 #include "fw-transaction.h"
39 #include "fw-topology.h"
40 #include "fw-device.h"
41
42 struct client;
43 struct client_resource {
44         struct list_head link;
45         void (*release)(struct client *client, struct client_resource *r);
46         u32 handle;
47 };
48
49 /*
50  * dequeue_event() just kfree()'s the event, so the event has to be
51  * the first field in the struct.
52  */
53
54 struct event {
55         struct { void *data; size_t size; } v[2];
56         struct list_head link;
57 };
58
59 struct bus_reset {
60         struct event event;
61         struct fw_cdev_event_bus_reset reset;
62 };
63
64 struct response {
65         struct event event;
66         struct fw_transaction transaction;
67         struct client *client;
68         struct client_resource resource;
69         struct fw_cdev_event_response response;
70 };
71
72 struct iso_interrupt {
73         struct event event;
74         struct fw_cdev_event_iso_interrupt interrupt;
75 };
76
77 struct client {
78         u32 version;
79         struct fw_device *device;
80         spinlock_t lock;
81         u32 resource_handle;
82         struct list_head resource_list;
83         struct list_head event_list;
84         wait_queue_head_t wait;
85         u64 bus_reset_closure;
86
87         struct fw_iso_context *iso_context;
88         u64 iso_closure;
89         struct fw_iso_buffer buffer;
90         unsigned long vm_start;
91
92         struct list_head link;
93 };
94
95 static inline void __user *
96 u64_to_uptr(__u64 value)
97 {
98         return (void __user *)(unsigned long)value;
99 }
100
101 static inline __u64
102 uptr_to_u64(void __user *ptr)
103 {
104         return (__u64)(unsigned long)ptr;
105 }
106
107 static int fw_device_op_open(struct inode *inode, struct file *file)
108 {
109         struct fw_device *device;
110         struct client *client;
111         unsigned long flags;
112
113         device = fw_device_get_by_devt(inode->i_rdev);
114         if (device == NULL)
115                 return -ENODEV;
116
117         if (fw_device_is_shutdown(device)) {
118                 fw_device_put(device);
119                 return -ENODEV;
120         }
121
122         client = kzalloc(sizeof(*client), GFP_KERNEL);
123         if (client == NULL) {
124                 fw_device_put(device);
125                 return -ENOMEM;
126         }
127
128         client->device = device;
129         INIT_LIST_HEAD(&client->event_list);
130         INIT_LIST_HEAD(&client->resource_list);
131         spin_lock_init(&client->lock);
132         init_waitqueue_head(&client->wait);
133
134         file->private_data = client;
135
136         spin_lock_irqsave(&device->client_list_lock, flags);
137         list_add_tail(&client->link, &device->client_list);
138         spin_unlock_irqrestore(&device->client_list_lock, flags);
139
140         return 0;
141 }
142
143 static void queue_event(struct client *client, struct event *event,
144                         void *data0, size_t size0, void *data1, size_t size1)
145 {
146         unsigned long flags;
147
148         event->v[0].data = data0;
149         event->v[0].size = size0;
150         event->v[1].data = data1;
151         event->v[1].size = size1;
152
153         spin_lock_irqsave(&client->lock, flags);
154         list_add_tail(&event->link, &client->event_list);
155         spin_unlock_irqrestore(&client->lock, flags);
156
157         wake_up_interruptible(&client->wait);
158 }
159
160 static int
161 dequeue_event(struct client *client, char __user *buffer, size_t count)
162 {
163         unsigned long flags;
164         struct event *event;
165         size_t size, total;
166         int i, retval;
167
168         retval = wait_event_interruptible(client->wait,
169                                           !list_empty(&client->event_list) ||
170                                           fw_device_is_shutdown(client->device));
171         if (retval < 0)
172                 return retval;
173
174         if (list_empty(&client->event_list) &&
175                        fw_device_is_shutdown(client->device))
176                 return -ENODEV;
177
178         spin_lock_irqsave(&client->lock, flags);
179         event = container_of(client->event_list.next, struct event, link);
180         list_del(&event->link);
181         spin_unlock_irqrestore(&client->lock, flags);
182
183         total = 0;
184         for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
185                 size = min(event->v[i].size, count - total);
186                 if (copy_to_user(buffer + total, event->v[i].data, size)) {
187                         retval = -EFAULT;
188                         goto out;
189                 }
190                 total += size;
191         }
192         retval = total;
193
194  out:
195         kfree(event);
196
197         return retval;
198 }
199
200 static ssize_t
201 fw_device_op_read(struct file *file,
202                   char __user *buffer, size_t count, loff_t *offset)
203 {
204         struct client *client = file->private_data;
205
206         return dequeue_event(client, buffer, count);
207 }
208
209 static void
210 fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
211                      struct client *client)
212 {
213         struct fw_card *card = client->device->card;
214         unsigned long flags;
215
216         spin_lock_irqsave(&card->lock, flags);
217
218         event->closure       = client->bus_reset_closure;
219         event->type          = FW_CDEV_EVENT_BUS_RESET;
220         event->generation    = client->device->generation;
221         event->node_id       = client->device->node_id;
222         event->local_node_id = card->local_node->node_id;
223         event->bm_node_id    = 0; /* FIXME: We don't track the BM. */
224         event->irm_node_id   = card->irm_node->node_id;
225         event->root_node_id  = card->root_node->node_id;
226
227         spin_unlock_irqrestore(&card->lock, flags);
228 }
229
230 static void
231 for_each_client(struct fw_device *device,
232                 void (*callback)(struct client *client))
233 {
234         struct client *c;
235         unsigned long flags;
236
237         spin_lock_irqsave(&device->client_list_lock, flags);
238
239         list_for_each_entry(c, &device->client_list, link)
240                 callback(c);
241
242         spin_unlock_irqrestore(&device->client_list_lock, flags);
243 }
244
245 static void
246 queue_bus_reset_event(struct client *client)
247 {
248         struct bus_reset *bus_reset;
249
250         bus_reset = kzalloc(sizeof(*bus_reset), GFP_ATOMIC);
251         if (bus_reset == NULL) {
252                 fw_notify("Out of memory when allocating bus reset event\n");
253                 return;
254         }
255
256         fill_bus_reset_event(&bus_reset->reset, client);
257
258         queue_event(client, &bus_reset->event,
259                     &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
260 }
261
262 void fw_device_cdev_update(struct fw_device *device)
263 {
264         for_each_client(device, queue_bus_reset_event);
265 }
266
267 static void wake_up_client(struct client *client)
268 {
269         wake_up_interruptible(&client->wait);
270 }
271
272 void fw_device_cdev_remove(struct fw_device *device)
273 {
274         for_each_client(device, wake_up_client);
275 }
276
277 static int ioctl_get_info(struct client *client, void *buffer)
278 {
279         struct fw_cdev_get_info *get_info = buffer;
280         struct fw_cdev_event_bus_reset bus_reset;
281         unsigned long ret = 0;
282
283         client->version = get_info->version;
284         get_info->version = FW_CDEV_VERSION;
285         get_info->card = client->device->card->index;
286
287         down_read(&fw_device_rwsem);
288
289         if (get_info->rom != 0) {
290                 void __user *uptr = u64_to_uptr(get_info->rom);
291                 size_t want = get_info->rom_length;
292                 size_t have = client->device->config_rom_length * 4;
293
294                 ret = copy_to_user(uptr, client->device->config_rom,
295                                    min(want, have));
296         }
297         get_info->rom_length = client->device->config_rom_length * 4;
298
299         up_read(&fw_device_rwsem);
300
301         if (ret != 0)
302                 return -EFAULT;
303
304         client->bus_reset_closure = get_info->bus_reset_closure;
305         if (get_info->bus_reset != 0) {
306                 void __user *uptr = u64_to_uptr(get_info->bus_reset);
307
308                 fill_bus_reset_event(&bus_reset, client);
309                 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
310                         return -EFAULT;
311         }
312
313         return 0;
314 }
315
316 static void
317 add_client_resource(struct client *client, struct client_resource *resource)
318 {
319         unsigned long flags;
320
321         spin_lock_irqsave(&client->lock, flags);
322         list_add_tail(&resource->link, &client->resource_list);
323         resource->handle = client->resource_handle++;
324         spin_unlock_irqrestore(&client->lock, flags);
325 }
326
327 static int
328 release_client_resource(struct client *client, u32 handle,
329                         struct client_resource **resource)
330 {
331         struct client_resource *r;
332         unsigned long flags;
333
334         spin_lock_irqsave(&client->lock, flags);
335         list_for_each_entry(r, &client->resource_list, link) {
336                 if (r->handle == handle) {
337                         list_del(&r->link);
338                         break;
339                 }
340         }
341         spin_unlock_irqrestore(&client->lock, flags);
342
343         if (&r->link == &client->resource_list)
344                 return -EINVAL;
345
346         if (resource)
347                 *resource = r;
348         else
349                 r->release(client, r);
350
351         return 0;
352 }
353
354 static void
355 release_transaction(struct client *client, struct client_resource *resource)
356 {
357         struct response *response =
358                 container_of(resource, struct response, resource);
359
360         fw_cancel_transaction(client->device->card, &response->transaction);
361 }
362
363 static void
364 complete_transaction(struct fw_card *card, int rcode,
365                      void *payload, size_t length, void *data)
366 {
367         struct response *response = data;
368         struct client *client = response->client;
369         unsigned long flags;
370         struct fw_cdev_event_response *r = &response->response;
371
372         if (length < r->length)
373                 r->length = length;
374         if (rcode == RCODE_COMPLETE)
375                 memcpy(r->data, payload, r->length);
376
377         spin_lock_irqsave(&client->lock, flags);
378         list_del(&response->resource.link);
379         spin_unlock_irqrestore(&client->lock, flags);
380
381         r->type   = FW_CDEV_EVENT_RESPONSE;
382         r->rcode  = rcode;
383
384         /*
385          * In the case that sizeof(*r) doesn't align with the position of the
386          * data, and the read is short, preserve an extra copy of the data
387          * to stay compatible with a pre-2.6.27 bug.  Since the bug is harmless
388          * for short reads and some apps depended on it, this is both safe
389          * and prudent for compatibility.
390          */
391         if (r->length <= sizeof(*r) - offsetof(typeof(*r), data))
392                 queue_event(client, &response->event, r, sizeof(*r),
393                             r->data, r->length);
394         else
395                 queue_event(client, &response->event, r, sizeof(*r) + r->length,
396                             NULL, 0);
397 }
398
399 static int ioctl_send_request(struct client *client, void *buffer)
400 {
401         struct fw_device *device = client->device;
402         struct fw_cdev_send_request *request = buffer;
403         struct response *response;
404
405         /* What is the biggest size we'll accept, really? */
406         if (request->length > 4096)
407                 return -EINVAL;
408
409         response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
410         if (response == NULL)
411                 return -ENOMEM;
412
413         response->client = client;
414         response->response.length = request->length;
415         response->response.closure = request->closure;
416
417         if (request->data &&
418             copy_from_user(response->response.data,
419                            u64_to_uptr(request->data), request->length)) {
420                 kfree(response);
421                 return -EFAULT;
422         }
423
424         response->resource.release = release_transaction;
425         add_client_resource(client, &response->resource);
426
427         fw_send_request(device->card, &response->transaction,
428                         request->tcode & 0x1f,
429                         device->node->node_id,
430                         request->generation,
431                         device->max_speed,
432                         request->offset,
433                         response->response.data, request->length,
434                         complete_transaction, response);
435
436         if (request->data)
437                 return sizeof(request) + request->length;
438         else
439                 return sizeof(request);
440 }
441
442 struct address_handler {
443         struct fw_address_handler handler;
444         __u64 closure;
445         struct client *client;
446         struct client_resource resource;
447 };
448
449 struct request {
450         struct fw_request *request;
451         void *data;
452         size_t length;
453         struct client_resource resource;
454 };
455
456 struct request_event {
457         struct event event;
458         struct fw_cdev_event_request request;
459 };
460
461 static void
462 release_request(struct client *client, struct client_resource *resource)
463 {
464         struct request *request =
465                 container_of(resource, struct request, resource);
466
467         fw_send_response(client->device->card, request->request,
468                          RCODE_CONFLICT_ERROR);
469         kfree(request);
470 }
471
472 static void
473 handle_request(struct fw_card *card, struct fw_request *r,
474                int tcode, int destination, int source,
475                int generation, int speed,
476                unsigned long long offset,
477                void *payload, size_t length, void *callback_data)
478 {
479         struct address_handler *handler = callback_data;
480         struct request *request;
481         struct request_event *e;
482         struct client *client = handler->client;
483
484         request = kmalloc(sizeof(*request), GFP_ATOMIC);
485         e = kmalloc(sizeof(*e), GFP_ATOMIC);
486         if (request == NULL || e == NULL) {
487                 kfree(request);
488                 kfree(e);
489                 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
490                 return;
491         }
492
493         request->request = r;
494         request->data    = payload;
495         request->length  = length;
496
497         request->resource.release = release_request;
498         add_client_resource(client, &request->resource);
499
500         e->request.type    = FW_CDEV_EVENT_REQUEST;
501         e->request.tcode   = tcode;
502         e->request.offset  = offset;
503         e->request.length  = length;
504         e->request.handle  = request->resource.handle;
505         e->request.closure = handler->closure;
506
507         queue_event(client, &e->event,
508                     &e->request, sizeof(e->request), payload, length);
509 }
510
511 static void
512 release_address_handler(struct client *client,
513                         struct client_resource *resource)
514 {
515         struct address_handler *handler =
516                 container_of(resource, struct address_handler, resource);
517
518         fw_core_remove_address_handler(&handler->handler);
519         kfree(handler);
520 }
521
522 static int ioctl_allocate(struct client *client, void *buffer)
523 {
524         struct fw_cdev_allocate *request = buffer;
525         struct address_handler *handler;
526         struct fw_address_region region;
527
528         handler = kmalloc(sizeof(*handler), GFP_KERNEL);
529         if (handler == NULL)
530                 return -ENOMEM;
531
532         region.start = request->offset;
533         region.end = request->offset + request->length;
534         handler->handler.length = request->length;
535         handler->handler.address_callback = handle_request;
536         handler->handler.callback_data = handler;
537         handler->closure = request->closure;
538         handler->client = client;
539
540         if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
541                 kfree(handler);
542                 return -EBUSY;
543         }
544
545         handler->resource.release = release_address_handler;
546         add_client_resource(client, &handler->resource);
547         request->handle = handler->resource.handle;
548
549         return 0;
550 }
551
552 static int ioctl_deallocate(struct client *client, void *buffer)
553 {
554         struct fw_cdev_deallocate *request = buffer;
555
556         return release_client_resource(client, request->handle, NULL);
557 }
558
559 static int ioctl_send_response(struct client *client, void *buffer)
560 {
561         struct fw_cdev_send_response *request = buffer;
562         struct client_resource *resource;
563         struct request *r;
564
565         if (release_client_resource(client, request->handle, &resource) < 0)
566                 return -EINVAL;
567         r = container_of(resource, struct request, resource);
568         if (request->length < r->length)
569                 r->length = request->length;
570         if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
571                 return -EFAULT;
572
573         fw_send_response(client->device->card, r->request, request->rcode);
574         kfree(r);
575
576         return 0;
577 }
578
579 static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
580 {
581         struct fw_cdev_initiate_bus_reset *request = buffer;
582         int short_reset;
583
584         short_reset = (request->type == FW_CDEV_SHORT_RESET);
585
586         return fw_core_initiate_bus_reset(client->device->card, short_reset);
587 }
588
589 struct descriptor {
590         struct fw_descriptor d;
591         struct client_resource resource;
592         u32 data[0];
593 };
594
595 static void release_descriptor(struct client *client,
596                                struct client_resource *resource)
597 {
598         struct descriptor *descriptor =
599                 container_of(resource, struct descriptor, resource);
600
601         fw_core_remove_descriptor(&descriptor->d);
602         kfree(descriptor);
603 }
604
605 static int ioctl_add_descriptor(struct client *client, void *buffer)
606 {
607         struct fw_cdev_add_descriptor *request = buffer;
608         struct descriptor *descriptor;
609         int retval;
610
611         if (request->length > 256)
612                 return -EINVAL;
613
614         descriptor =
615                 kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
616         if (descriptor == NULL)
617                 return -ENOMEM;
618
619         if (copy_from_user(descriptor->data,
620                            u64_to_uptr(request->data), request->length * 4)) {
621                 kfree(descriptor);
622                 return -EFAULT;
623         }
624
625         descriptor->d.length = request->length;
626         descriptor->d.immediate = request->immediate;
627         descriptor->d.key = request->key;
628         descriptor->d.data = descriptor->data;
629
630         retval = fw_core_add_descriptor(&descriptor->d);
631         if (retval < 0) {
632                 kfree(descriptor);
633                 return retval;
634         }
635
636         descriptor->resource.release = release_descriptor;
637         add_client_resource(client, &descriptor->resource);
638         request->handle = descriptor->resource.handle;
639
640         return 0;
641 }
642
643 static int ioctl_remove_descriptor(struct client *client, void *buffer)
644 {
645         struct fw_cdev_remove_descriptor *request = buffer;
646
647         return release_client_resource(client, request->handle, NULL);
648 }
649
650 static void
651 iso_callback(struct fw_iso_context *context, u32 cycle,
652              size_t header_length, void *header, void *data)
653 {
654         struct client *client = data;
655         struct iso_interrupt *irq;
656
657         irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
658         if (irq == NULL)
659                 return;
660
661         irq->interrupt.type      = FW_CDEV_EVENT_ISO_INTERRUPT;
662         irq->interrupt.closure   = client->iso_closure;
663         irq->interrupt.cycle     = cycle;
664         irq->interrupt.header_length = header_length;
665         memcpy(irq->interrupt.header, header, header_length);
666         queue_event(client, &irq->event, &irq->interrupt,
667                     sizeof(irq->interrupt) + header_length, NULL, 0);
668 }
669
670 static int ioctl_create_iso_context(struct client *client, void *buffer)
671 {
672         struct fw_cdev_create_iso_context *request = buffer;
673         struct fw_iso_context *context;
674
675         /* We only support one context at this time. */
676         if (client->iso_context != NULL)
677                 return -EBUSY;
678
679         if (request->channel > 63)
680                 return -EINVAL;
681
682         switch (request->type) {
683         case FW_ISO_CONTEXT_RECEIVE:
684                 if (request->header_size < 4 || (request->header_size & 3))
685                         return -EINVAL;
686
687                 break;
688
689         case FW_ISO_CONTEXT_TRANSMIT:
690                 if (request->speed > SCODE_3200)
691                         return -EINVAL;
692
693                 break;
694
695         default:
696                 return -EINVAL;
697         }
698
699         context =  fw_iso_context_create(client->device->card,
700                                          request->type,
701                                          request->channel,
702                                          request->speed,
703                                          request->header_size,
704                                          iso_callback, client);
705         if (IS_ERR(context))
706                 return PTR_ERR(context);
707
708         client->iso_closure = request->closure;
709         client->iso_context = context;
710
711         /* We only support one context at this time. */
712         request->handle = 0;
713
714         return 0;
715 }
716
717 /* Macros for decoding the iso packet control header. */
718 #define GET_PAYLOAD_LENGTH(v)   ((v) & 0xffff)
719 #define GET_INTERRUPT(v)        (((v) >> 16) & 0x01)
720 #define GET_SKIP(v)             (((v) >> 17) & 0x01)
721 #define GET_TAG(v)              (((v) >> 18) & 0x03)
722 #define GET_SY(v)               (((v) >> 20) & 0x0f)
723 #define GET_HEADER_LENGTH(v)    (((v) >> 24) & 0xff)
724
725 static int ioctl_queue_iso(struct client *client, void *buffer)
726 {
727         struct fw_cdev_queue_iso *request = buffer;
728         struct fw_cdev_iso_packet __user *p, *end, *next;
729         struct fw_iso_context *ctx = client->iso_context;
730         unsigned long payload, buffer_end, header_length;
731         u32 control;
732         int count;
733         struct {
734                 struct fw_iso_packet packet;
735                 u8 header[256];
736         } u;
737
738         if (ctx == NULL || request->handle != 0)
739                 return -EINVAL;
740
741         /*
742          * If the user passes a non-NULL data pointer, has mmap()'ed
743          * the iso buffer, and the pointer points inside the buffer,
744          * we setup the payload pointers accordingly.  Otherwise we
745          * set them both to 0, which will still let packets with
746          * payload_length == 0 through.  In other words, if no packets
747          * use the indirect payload, the iso buffer need not be mapped
748          * and the request->data pointer is ignored.
749          */
750
751         payload = (unsigned long)request->data - client->vm_start;
752         buffer_end = client->buffer.page_count << PAGE_SHIFT;
753         if (request->data == 0 || client->buffer.pages == NULL ||
754             payload >= buffer_end) {
755                 payload = 0;
756                 buffer_end = 0;
757         }
758
759         p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
760
761         if (!access_ok(VERIFY_READ, p, request->size))
762                 return -EFAULT;
763
764         end = (void __user *)p + request->size;
765         count = 0;
766         while (p < end) {
767                 if (get_user(control, &p->control))
768                         return -EFAULT;
769                 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
770                 u.packet.interrupt = GET_INTERRUPT(control);
771                 u.packet.skip = GET_SKIP(control);
772                 u.packet.tag = GET_TAG(control);
773                 u.packet.sy = GET_SY(control);
774                 u.packet.header_length = GET_HEADER_LENGTH(control);
775
776                 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
777                         header_length = u.packet.header_length;
778                 } else {
779                         /*
780                          * We require that header_length is a multiple of
781                          * the fixed header size, ctx->header_size.
782                          */
783                         if (ctx->header_size == 0) {
784                                 if (u.packet.header_length > 0)
785                                         return -EINVAL;
786                         } else if (u.packet.header_length % ctx->header_size != 0) {
787                                 return -EINVAL;
788                         }
789                         header_length = 0;
790                 }
791
792                 next = (struct fw_cdev_iso_packet __user *)
793                         &p->header[header_length / 4];
794                 if (next > end)
795                         return -EINVAL;
796                 if (__copy_from_user
797                     (u.packet.header, p->header, header_length))
798                         return -EFAULT;
799                 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
800                     u.packet.header_length + u.packet.payload_length > 0)
801                         return -EINVAL;
802                 if (payload + u.packet.payload_length > buffer_end)
803                         return -EINVAL;
804
805                 if (fw_iso_context_queue(ctx, &u.packet,
806                                          &client->buffer, payload))
807                         break;
808
809                 p = next;
810                 payload += u.packet.payload_length;
811                 count++;
812         }
813
814         request->size    -= uptr_to_u64(p) - request->packets;
815         request->packets  = uptr_to_u64(p);
816         request->data     = client->vm_start + payload;
817
818         return count;
819 }
820
821 static int ioctl_start_iso(struct client *client, void *buffer)
822 {
823         struct fw_cdev_start_iso *request = buffer;
824
825         if (client->iso_context == NULL || request->handle != 0)
826                 return -EINVAL;
827
828         if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
829                 if (request->tags == 0 || request->tags > 15)
830                         return -EINVAL;
831
832                 if (request->sync > 15)
833                         return -EINVAL;
834         }
835
836         return fw_iso_context_start(client->iso_context, request->cycle,
837                                     request->sync, request->tags);
838 }
839
840 static int ioctl_stop_iso(struct client *client, void *buffer)
841 {
842         struct fw_cdev_stop_iso *request = buffer;
843
844         if (client->iso_context == NULL || request->handle != 0)
845                 return -EINVAL;
846
847         return fw_iso_context_stop(client->iso_context);
848 }
849
850 static int ioctl_get_cycle_timer(struct client *client, void *buffer)
851 {
852         struct fw_cdev_get_cycle_timer *request = buffer;
853         struct fw_card *card = client->device->card;
854         unsigned long long bus_time;
855         struct timeval tv;
856         unsigned long flags;
857
858         preempt_disable();
859         local_irq_save(flags);
860
861         bus_time = card->driver->get_bus_time(card);
862         do_gettimeofday(&tv);
863
864         local_irq_restore(flags);
865         preempt_enable();
866
867         request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
868         request->cycle_timer = bus_time & 0xffffffff;
869         return 0;
870 }
871
872 static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
873         ioctl_get_info,
874         ioctl_send_request,
875         ioctl_allocate,
876         ioctl_deallocate,
877         ioctl_send_response,
878         ioctl_initiate_bus_reset,
879         ioctl_add_descriptor,
880         ioctl_remove_descriptor,
881         ioctl_create_iso_context,
882         ioctl_queue_iso,
883         ioctl_start_iso,
884         ioctl_stop_iso,
885         ioctl_get_cycle_timer,
886 };
887
888 static int
889 dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
890 {
891         char buffer[256];
892         int retval;
893
894         if (_IOC_TYPE(cmd) != '#' ||
895             _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
896                 return -EINVAL;
897
898         if (_IOC_DIR(cmd) & _IOC_WRITE) {
899                 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
900                     copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
901                         return -EFAULT;
902         }
903
904         retval = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
905         if (retval < 0)
906                 return retval;
907
908         if (_IOC_DIR(cmd) & _IOC_READ) {
909                 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
910                     copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
911                         return -EFAULT;
912         }
913
914         return retval;
915 }
916
917 static long
918 fw_device_op_ioctl(struct file *file,
919                    unsigned int cmd, unsigned long arg)
920 {
921         struct client *client = file->private_data;
922
923         if (fw_device_is_shutdown(client->device))
924                 return -ENODEV;
925
926         return dispatch_ioctl(client, cmd, (void __user *) arg);
927 }
928
929 #ifdef CONFIG_COMPAT
930 static long
931 fw_device_op_compat_ioctl(struct file *file,
932                           unsigned int cmd, unsigned long arg)
933 {
934         struct client *client = file->private_data;
935
936         if (fw_device_is_shutdown(client->device))
937                 return -ENODEV;
938
939         return dispatch_ioctl(client, cmd, compat_ptr(arg));
940 }
941 #endif
942
943 static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
944 {
945         struct client *client = file->private_data;
946         enum dma_data_direction direction;
947         unsigned long size;
948         int page_count, retval;
949
950         if (fw_device_is_shutdown(client->device))
951                 return -ENODEV;
952
953         /* FIXME: We could support multiple buffers, but we don't. */
954         if (client->buffer.pages != NULL)
955                 return -EBUSY;
956
957         if (!(vma->vm_flags & VM_SHARED))
958                 return -EINVAL;
959
960         if (vma->vm_start & ~PAGE_MASK)
961                 return -EINVAL;
962
963         client->vm_start = vma->vm_start;
964         size = vma->vm_end - vma->vm_start;
965         page_count = size >> PAGE_SHIFT;
966         if (size & ~PAGE_MASK)
967                 return -EINVAL;
968
969         if (vma->vm_flags & VM_WRITE)
970                 direction = DMA_TO_DEVICE;
971         else
972                 direction = DMA_FROM_DEVICE;
973
974         retval = fw_iso_buffer_init(&client->buffer, client->device->card,
975                                     page_count, direction);
976         if (retval < 0)
977                 return retval;
978
979         retval = fw_iso_buffer_map(&client->buffer, vma);
980         if (retval < 0)
981                 fw_iso_buffer_destroy(&client->buffer, client->device->card);
982
983         return retval;
984 }
985
986 static int fw_device_op_release(struct inode *inode, struct file *file)
987 {
988         struct client *client = file->private_data;
989         struct event *e, *next_e;
990         struct client_resource *r, *next_r;
991         unsigned long flags;
992
993         if (client->buffer.pages)
994                 fw_iso_buffer_destroy(&client->buffer, client->device->card);
995
996         if (client->iso_context)
997                 fw_iso_context_destroy(client->iso_context);
998
999         list_for_each_entry_safe(r, next_r, &client->resource_list, link)
1000                 r->release(client, r);
1001
1002         /*
1003          * FIXME: We should wait for the async tasklets to stop
1004          * running before freeing the memory.
1005          */
1006
1007         list_for_each_entry_safe(e, next_e, &client->event_list, link)
1008                 kfree(e);
1009
1010         spin_lock_irqsave(&client->device->client_list_lock, flags);
1011         list_del(&client->link);
1012         spin_unlock_irqrestore(&client->device->client_list_lock, flags);
1013
1014         fw_device_put(client->device);
1015         kfree(client);
1016
1017         return 0;
1018 }
1019
1020 static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1021 {
1022         struct client *client = file->private_data;
1023         unsigned int mask = 0;
1024
1025         poll_wait(file, &client->wait, pt);
1026
1027         if (fw_device_is_shutdown(client->device))
1028                 mask |= POLLHUP | POLLERR;
1029         if (!list_empty(&client->event_list))
1030                 mask |= POLLIN | POLLRDNORM;
1031
1032         return mask;
1033 }
1034
1035 const struct file_operations fw_device_ops = {
1036         .owner          = THIS_MODULE,
1037         .open           = fw_device_op_open,
1038         .read           = fw_device_op_read,
1039         .unlocked_ioctl = fw_device_op_ioctl,
1040         .poll           = fw_device_op_poll,
1041         .release        = fw_device_op_release,
1042         .mmap           = fw_device_op_mmap,
1043
1044 #ifdef CONFIG_COMPAT
1045         .compat_ioctl   = fw_device_op_compat_ioctl,
1046 #endif
1047 };