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