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