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