1 /* -*- c-basic-offset: 8 -*-
3 * fw-device-cdev.c - Char device for device raw access
5 * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net>
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.
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.
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.
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>
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"
41 * - bus resets sends a new packet with new generation and node id
45 /* dequeue_event() just kfree()'s the event, so the event has to be
46 * the first field in the struct. */
49 struct { void *data; size_t size; } v[2];
50 struct list_head link;
55 struct fw_transaction transaction;
56 struct client *client;
57 struct fw_cdev_event_response response;
60 struct iso_interrupt {
62 struct fw_cdev_event_iso_interrupt interrupt;
66 struct fw_device *device;
68 struct list_head handler_list;
69 struct list_head request_list;
71 struct list_head event_list;
72 struct semaphore event_list_sem;
73 wait_queue_head_t wait;
74 unsigned long vm_start;
75 struct fw_iso_context *iso_context;
78 static inline void __user *
79 u64_to_uptr(__u64 value)
81 return (void __user *)(unsigned long)value;
85 uptr_to_u64(void __user *ptr)
87 return (__u64)(unsigned long)ptr;
90 static int fw_device_op_open(struct inode *inode, struct file *file)
92 struct fw_device *device;
93 struct client *client;
95 device = container_of(inode->i_cdev, struct fw_device, cdev);
97 client = kzalloc(sizeof *client, GFP_KERNEL);
101 client->device = fw_device_get(device);
102 INIT_LIST_HEAD(&client->event_list);
103 sema_init(&client->event_list_sem, 0);
104 INIT_LIST_HEAD(&client->handler_list);
105 INIT_LIST_HEAD(&client->request_list);
106 spin_lock_init(&client->lock);
107 init_waitqueue_head(&client->wait);
109 file->private_data = client;
114 static void queue_event(struct client *client, struct event *event,
115 void *data0, size_t size0, void *data1, size_t size1)
119 event->v[0].data = data0;
120 event->v[0].size = size0;
121 event->v[1].data = data1;
122 event->v[1].size = size1;
124 spin_lock_irqsave(&client->lock, flags);
126 list_add_tail(&event->link, &client->event_list);
128 up(&client->event_list_sem);
129 wake_up_interruptible(&client->wait);
131 spin_unlock_irqrestore(&client->lock, flags);
134 static int dequeue_event(struct client *client, char __user *buffer, size_t count)
139 int i, retval = -EFAULT;
141 if (down_interruptible(&client->event_list_sem) < 0)
144 spin_lock_irqsave(&client->lock, flags);
146 event = container_of(client->event_list.next, struct event, link);
147 list_del(&event->link);
149 spin_unlock_irqrestore(&client->lock, flags);
155 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
156 size = min(event->v[i].size, count - total);
157 if (copy_to_user(buffer + total, event->v[i].data, size))
170 fw_device_op_read(struct file *file,
171 char __user *buffer, size_t count, loff_t *offset)
173 struct client *client = file->private_data;
175 return dequeue_event(client, buffer, count);
178 static int ioctl_config_rom(struct client *client, void __user *arg)
180 struct fw_cdev_get_config_rom rom;
182 rom.length = client->device->config_rom_length;
183 memcpy(rom.data, client->device->config_rom, rom.length * 4);
184 if (copy_to_user(arg, &rom,
185 (char *)&rom.data[rom.length] - (char *)&rom))
192 complete_transaction(struct fw_card *card, int rcode,
193 void *payload, size_t length, void *data)
195 struct response *response = data;
196 struct client *client = response->client;
198 if (length < response->response.length)
199 response->response.length = length;
200 if (rcode == RCODE_COMPLETE)
201 memcpy(response->response.data, payload,
202 response->response.length);
204 response->response.type = FW_CDEV_EVENT_RESPONSE;
205 response->response.rcode = rcode;
206 queue_event(client, &response->event,
207 &response->response, sizeof response->response,
208 response->response.data, response->response.length);
211 static ssize_t ioctl_send_request(struct client *client, void __user *arg)
213 struct fw_device *device = client->device;
214 struct fw_cdev_send_request request;
215 struct response *response;
217 if (copy_from_user(&request, arg, sizeof request))
220 /* What is the biggest size we'll accept, really? */
221 if (request.length > 4096)
224 response = kmalloc(sizeof *response + request.length, GFP_KERNEL);
225 if (response == NULL)
228 response->client = client;
229 response->response.length = request.length;
230 response->response.closure = request.closure;
233 copy_from_user(response->response.data,
234 u64_to_uptr(request.data), request.length)) {
239 fw_send_request(device->card, &response->transaction,
241 device->node->node_id,
242 device->card->generation,
243 device->node->max_speed,
245 response->response.data, request.length,
246 complete_transaction, response);
249 return sizeof request + request.length;
251 return sizeof request;
254 struct address_handler {
255 struct fw_address_handler handler;
257 struct client *client;
258 struct list_head link;
262 struct fw_request *request;
266 struct list_head link;
269 struct request_event {
271 struct fw_cdev_event_request request;
275 handle_request(struct fw_card *card, struct fw_request *r,
276 int tcode, int destination, int source,
277 int generation, int speed,
278 unsigned long long offset,
279 void *payload, size_t length, void *callback_data)
281 struct address_handler *handler = callback_data;
282 struct request *request;
283 struct request_event *e;
285 struct client *client = handler->client;
287 request = kmalloc(sizeof *request, GFP_ATOMIC);
288 e = kmalloc(sizeof *e, GFP_ATOMIC);
289 if (request == NULL || e == NULL) {
292 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
296 request->request = r;
297 request->data = payload;
298 request->length = length;
300 spin_lock_irqsave(&client->lock, flags);
301 request->serial = client->request_serial++;
302 list_add_tail(&request->link, &client->request_list);
303 spin_unlock_irqrestore(&client->lock, flags);
305 e->request.type = FW_CDEV_EVENT_REQUEST;
306 e->request.tcode = tcode;
307 e->request.offset = offset;
308 e->request.length = length;
309 e->request.serial = request->serial;
310 e->request.closure = handler->closure;
312 queue_event(client, &e->event,
313 &e->request, sizeof e->request, payload, length);
316 static int ioctl_allocate(struct client *client, void __user *arg)
318 struct fw_cdev_allocate request;
319 struct address_handler *handler;
321 struct fw_address_region region;
323 if (copy_from_user(&request, arg, sizeof request))
326 handler = kmalloc(sizeof *handler, GFP_KERNEL);
330 region.start = request.offset;
331 region.end = request.offset + request.length;
332 handler->handler.length = request.length;
333 handler->handler.address_callback = handle_request;
334 handler->handler.callback_data = handler;
335 handler->closure = request.closure;
336 handler->client = client;
338 if (fw_core_add_address_handler(&handler->handler, ®ion) < 0) {
343 spin_lock_irqsave(&client->lock, flags);
344 list_add_tail(&handler->link, &client->handler_list);
345 spin_unlock_irqrestore(&client->lock, flags);
350 static int ioctl_send_response(struct client *client, void __user *arg)
352 struct fw_cdev_send_response request;
356 if (copy_from_user(&request, arg, sizeof request))
359 spin_lock_irqsave(&client->lock, flags);
360 list_for_each_entry(r, &client->request_list, link) {
361 if (r->serial == request.serial) {
366 spin_unlock_irqrestore(&client->lock, flags);
368 if (&r->link == &client->request_list)
371 if (request.length < r->length)
372 r->length = request.length;
373 if (copy_from_user(r->data, u64_to_uptr(request.data), r->length))
376 fw_send_response(client->device->card, r->request, request.rcode);
384 iso_callback(struct fw_iso_context *context, int status, u32 cycle, void *data)
386 struct client *client = data;
387 struct iso_interrupt *interrupt;
389 interrupt = kzalloc(sizeof *interrupt, GFP_ATOMIC);
390 if (interrupt == NULL)
393 interrupt->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
394 interrupt->interrupt.closure = 0;
395 interrupt->interrupt.cycle = cycle;
396 queue_event(client, &interrupt->event,
397 &interrupt->interrupt, sizeof interrupt->interrupt, NULL, 0);
400 static int ioctl_create_iso_context(struct client *client, void __user *arg)
402 struct fw_cdev_create_iso_context request;
404 if (copy_from_user(&request, arg, sizeof request))
407 client->iso_context = fw_iso_context_create(client->device->card,
408 FW_ISO_CONTEXT_TRANSMIT,
410 iso_callback, client);
411 if (IS_ERR(client->iso_context))
412 return PTR_ERR(client->iso_context);
417 static int ioctl_queue_iso(struct client *client, void __user *arg)
419 struct fw_cdev_queue_iso request;
420 struct fw_cdev_iso_packet __user *p, *end, *next;
421 void *payload, *payload_end;
425 struct fw_iso_packet packet;
429 if (client->iso_context == NULL)
431 if (copy_from_user(&request, arg, sizeof request))
434 /* If the user passes a non-NULL data pointer, has mmap()'ed
435 * the iso buffer, and the pointer points inside the buffer,
436 * we setup the payload pointers accordingly. Otherwise we
437 * set them both to NULL, which will still let packets with
438 * payload_length == 0 through. In other words, if no packets
439 * use the indirect payload, the iso buffer need not be mapped
440 * and the request.data pointer is ignored.*/
442 index = (unsigned long)request.data - client->vm_start;
443 if (request.data != 0 && client->vm_start != 0 &&
444 index <= client->iso_context->buffer_size) {
445 payload = client->iso_context->buffer + index;
446 payload_end = client->iso_context->buffer +
447 client->iso_context->buffer_size;
453 if (!access_ok(VERIFY_READ, request.packets, request.size))
456 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request.packets);
457 end = (void __user *)p + request.size;
460 if (__copy_from_user(&u.packet, p, sizeof *p))
462 next = (struct fw_cdev_iso_packet __user *)
463 &p->header[u.packet.header_length / 4];
467 (u.packet.header, p->header, u.packet.header_length))
470 u.packet.header_length + u.packet.payload_length > 0)
472 if (payload + u.packet.payload_length > payload_end)
475 if (fw_iso_context_queue(client->iso_context,
480 payload += u.packet.payload_length;
484 request.size -= uptr_to_u64(p) - request.packets;
485 request.packets = uptr_to_u64(p);
487 client->vm_start + (payload - client->iso_context->buffer);
489 if (copy_to_user(arg, &request, sizeof request))
495 static int ioctl_send_iso(struct client *client, void __user *arg)
497 struct fw_cdev_send_iso request;
499 if (copy_from_user(&request, arg, sizeof request))
502 return fw_iso_context_send(client->iso_context, request.channel,
503 request.speed, request.cycle);
507 dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
510 case FW_CDEV_IOC_GET_CONFIG_ROM:
511 return ioctl_config_rom(client, arg);
512 case FW_CDEV_IOC_SEND_REQUEST:
513 return ioctl_send_request(client, arg);
514 case FW_CDEV_IOC_ALLOCATE:
515 return ioctl_allocate(client, arg);
516 case FW_CDEV_IOC_SEND_RESPONSE:
517 return ioctl_send_response(client, arg);
518 case FW_CDEV_IOC_CREATE_ISO_CONTEXT:
519 return ioctl_create_iso_context(client, arg);
520 case FW_CDEV_IOC_QUEUE_ISO:
521 return ioctl_queue_iso(client, arg);
522 case FW_CDEV_IOC_SEND_ISO:
523 return ioctl_send_iso(client, arg);
530 fw_device_op_ioctl(struct file *file,
531 unsigned int cmd, unsigned long arg)
533 struct client *client = file->private_data;
535 return dispatch_ioctl(client, cmd, (void __user *) arg);
540 fw_device_op_compat_ioctl(struct file *file,
541 unsigned int cmd, unsigned long arg)
543 struct client *client = file->private_data;
545 return dispatch_ioctl(client, cmd, compat_ptr(arg));
549 static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
551 struct client *client = file->private_data;
553 if (client->iso_context->buffer == NULL)
556 client->vm_start = vma->vm_start;
558 return remap_vmalloc_range(vma, client->iso_context->buffer, 0);
561 static int fw_device_op_release(struct inode *inode, struct file *file)
563 struct client *client = file->private_data;
564 struct address_handler *h, *next;
565 struct request *r, *next_r;
567 if (client->iso_context)
568 fw_iso_context_destroy(client->iso_context);
570 list_for_each_entry_safe(h, next, &client->handler_list, link) {
571 fw_core_remove_address_handler(&h->handler);
575 list_for_each_entry_safe(r, next_r, &client->request_list, link) {
576 fw_send_response(client->device->card, r->request,
577 RCODE_CONFLICT_ERROR);
581 /* TODO: wait for all transactions to finish so
582 * complete_transaction doesn't try to queue up responses
583 * after we free client. */
584 while (!list_empty(&client->event_list))
585 dequeue_event(client, NULL, 0);
587 fw_device_put(client->device);
593 static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
595 struct client *client = file->private_data;
597 poll_wait(file, &client->wait, pt);
599 if (!list_empty(&client->event_list))
600 return POLLIN | POLLRDNORM;
605 const struct file_operations fw_device_ops = {
606 .owner = THIS_MODULE,
607 .open = fw_device_op_open,
608 .read = fw_device_op_read,
609 .unlocked_ioctl = fw_device_op_ioctl,
610 .poll = fw_device_op_poll,
611 .release = fw_device_op_release,
612 .mmap = fw_device_op_mmap,
615 .compat_ioctl = fw_device_op_compat_ioctl,