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