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