]> err.no Git - linux-2.6/blobdiff - drivers/firewire/fw-device-cdev.c
firewire: Add a bus reset event type for fw-device-cdev.
[linux-2.6] / drivers / firewire / fw-device-cdev.c
index c10e3326abf3c5488cc603582095a3c041b85569..9f3c96c7af29d0b144b8e0cccc8b195d27d35fae 100644 (file)
@@ -50,6 +50,11 @@ struct event {
        struct list_head link;
 };
 
+struct bus_reset {
+       struct event event;
+       struct fw_cdev_event_bus_reset reset;
+};
+
 struct response {
        struct event event;
        struct fw_transaction transaction;
@@ -71,8 +76,12 @@ struct client {
        struct list_head event_list;
        struct semaphore event_list_sem;
        wait_queue_head_t wait;
-       unsigned long vm_start;
+
        struct fw_iso_context *iso_context;
+       struct fw_iso_buffer buffer;
+       unsigned long vm_start;
+
+       struct list_head link;
 };
 
 static inline void __user *
@@ -91,6 +100,7 @@ static int fw_device_op_open(struct inode *inode, struct file *file)
 {
        struct fw_device *device;
        struct client *client;
+       unsigned long flags;
 
        device = container_of(inode->i_cdev, struct fw_device, cdev);
 
@@ -108,6 +118,10 @@ static int fw_device_op_open(struct inode *inode, struct file *file)
 
        file->private_data = client;
 
+       spin_lock_irqsave(&device->card->lock, flags);
+       list_add_tail(&client->link, &device->client_list);
+       spin_unlock_irqrestore(&device->card->lock, flags);
+
        return 0;
 }
 
@@ -175,6 +189,45 @@ fw_device_op_read(struct file *file,
        return dequeue_event(client, buffer, count);
 }
 
+static void
+queue_bus_reset_event(struct client *client)
+{
+       struct bus_reset *bus_reset;
+       struct fw_device *device = client->device;
+       struct fw_card *card = device->card;
+
+       bus_reset = kzalloc(sizeof *bus_reset, GFP_ATOMIC);
+       if (bus_reset == NULL) {
+               fw_notify("Out of memory when allocating bus reset event\n");
+               return;
+       }
+
+       bus_reset->reset.type          = FW_CDEV_EVENT_BUS_RESET;
+       bus_reset->reset.node_id       = device->node_id;
+       bus_reset->reset.local_node_id = card->local_node->node_id;
+       bus_reset->reset.bm_node_id    = 0; /* FIXME: We don't track the BM. */
+       bus_reset->reset.irm_node_id   = card->irm_node->node_id;
+       bus_reset->reset.root_node_id  = card->root_node->node_id;
+       bus_reset->reset.generation    = card->generation;
+
+       queue_event(client, &bus_reset->event,
+                   &bus_reset->reset, sizeof bus_reset->reset, NULL, 0);
+}
+
+void fw_device_cdev_update(struct fw_device *device)
+{
+       struct fw_card *card = device->card;
+       struct client *c;
+       unsigned long flags;
+
+       spin_lock_irqsave(&card->lock, flags);
+
+       list_for_each_entry(c, &device->client_list, link)
+               queue_bus_reset_event(c);
+
+       spin_unlock_irqrestore(&card->lock, flags);
+}
+
 static int ioctl_config_rom(struct client *client, void __user *arg)
 {
        struct fw_cdev_get_config_rom rom;
@@ -238,7 +291,7 @@ static ssize_t ioctl_send_request(struct client *client, void __user *arg)
 
        fw_send_request(device->card, &response->transaction,
                        request.tcode,
-                       device->node->node_id | LOCAL_BUS,
+                       device->node->node_id,
                        device->card->generation,
                        device->node->max_speed,
                        request.offset,
@@ -381,20 +434,24 @@ static int ioctl_send_response(struct client *client, void __user *arg)
 }
 
 static void
-iso_callback(struct fw_iso_context *context, int status, u32 cycle, void *data)
+iso_callback(struct fw_iso_context *context, u32 cycle,
+            size_t header_length, void *header, void *data)
 {
        struct client *client = data;
        struct iso_interrupt *interrupt;
 
-       interrupt = kzalloc(sizeof *interrupt, GFP_ATOMIC);
+       interrupt = kzalloc(sizeof *interrupt + header_length, GFP_ATOMIC);
        if (interrupt == NULL)
                return;
 
        interrupt->interrupt.type      = FW_CDEV_EVENT_ISO_INTERRUPT;
        interrupt->interrupt.closure   = 0;
        interrupt->interrupt.cycle     = cycle;
+       interrupt->interrupt.header_length = header_length;
+       memcpy(interrupt->interrupt.header, header, header_length);
        queue_event(client, &interrupt->event,
-                   &interrupt->interrupt, sizeof interrupt->interrupt, NULL, 0);
+                   &interrupt->interrupt,
+                   sizeof interrupt->interrupt + header_length, NULL, 0);
 }
 
 static int ioctl_create_iso_context(struct client *client, void __user *arg)
@@ -404,9 +461,28 @@ static int ioctl_create_iso_context(struct client *client, void __user *arg)
        if (copy_from_user(&request, arg, sizeof request))
                return -EFAULT;
 
+       if (request.type > FW_ISO_CONTEXT_RECEIVE)
+               return -EINVAL;
+
+       if (request.channel > 63)
+               return -EINVAL;
+
+       if (request.sync > 15)
+               return -EINVAL;
+
+       if (request.tags == 0 || request.tags > 15)
+               return -EINVAL;
+
+       if (request.speed > SCODE_3200)
+               return -EINVAL;
+
        client->iso_context = fw_iso_context_create(client->device->card,
-                                                   FW_ISO_CONTEXT_TRANSMIT,
-                                                   request.buffer_size,
+                                                   request.type,
+                                                   request.channel,
+                                                   request.speed,
+                                                   request.header_size,
+                                                   request.sync,
+                                                   request.tags,
                                                    iso_callback, client);
        if (IS_ERR(client->iso_context))
                return PTR_ERR(client->iso_context);
@@ -418,15 +494,15 @@ static int ioctl_queue_iso(struct client *client, void __user *arg)
 {
        struct fw_cdev_queue_iso request;
        struct fw_cdev_iso_packet __user *p, *end, *next;
-       void *payload, *payload_end;
-       unsigned long index;
+       struct fw_iso_context *ctx = client->iso_context;
+       unsigned long payload, payload_end, header_length;
        int count;
        struct {
                struct fw_iso_packet packet;
                u8 header[256];
        } u;
 
-       if (client->iso_context == NULL)
+       if (ctx == NULL)
                return -EINVAL;
        if (copy_from_user(&request, arg, sizeof request))
                return -EFAULT;
@@ -434,20 +510,17 @@ static int ioctl_queue_iso(struct client *client, void __user *arg)
        /* If the user passes a non-NULL data pointer, has mmap()'ed
         * the iso buffer, and the pointer points inside the buffer,
         * we setup the payload pointers accordingly.  Otherwise we
-        * set them both to NULL, which will still let packets with
+        * set them both to 0, which will still let packets with
         * payload_length == 0 through.  In other words, if no packets
         * use the indirect payload, the iso buffer need not be mapped
         * and the request.data pointer is ignored.*/
 
-       index = (unsigned long)request.data - client->vm_start;
-       if (request.data != 0 && client->vm_start != 0 &&
-           index <= client->iso_context->buffer_size) {
-               payload = client->iso_context->buffer + index;
-               payload_end = client->iso_context->buffer +
-                       client->iso_context->buffer_size;
-       } else {
-               payload = NULL;
-               payload_end = NULL;
+       payload = (unsigned long)request.data - client->vm_start;
+       payload_end = payload + (client->buffer.page_count << PAGE_SHIFT);
+       if (request.data == 0 || client->buffer.pages == NULL ||
+           payload >= payload_end) {
+               payload = 0;
+               payload_end = 0;
        }
 
        if (!access_ok(VERIFY_READ, request.packets, request.size))
@@ -459,21 +532,36 @@ static int ioctl_queue_iso(struct client *client, void __user *arg)
        while (p < end) {
                if (__copy_from_user(&u.packet, p, sizeof *p))
                        return -EFAULT;
+
+               if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
+                       header_length = u.packet.header_length;
+               } else {
+                       /* We require that header_length is a multiple of
+                        * the fixed header size, ctx->header_size */
+                       if (ctx->header_size == 0) {
+                               if (u.packet.header_length > 0)
+                                       return -EINVAL;
+                       } else if (u.packet.header_length % ctx->header_size != 0) {
+                               return -EINVAL;
+                       }
+                       header_length = 0;
+               }
+
                next = (struct fw_cdev_iso_packet __user *)
-                       &p->header[u.packet.header_length / 4];
+                       &p->header[header_length / 4];
                if (next > end)
                        return -EINVAL;
                if (__copy_from_user
-                   (u.packet.header, p->header, u.packet.header_length))
+                   (u.packet.header, p->header, header_length))
                        return -EFAULT;
-               if (u.packet.skip &&
+               if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
                    u.packet.header_length + u.packet.payload_length > 0)
                        return -EINVAL;
                if (payload + u.packet.payload_length > payload_end)
                        return -EINVAL;
 
-               if (fw_iso_context_queue(client->iso_context,
-                                        &u.packet, payload))
+               if (fw_iso_context_queue(ctx, &u.packet,
+                                        &client->buffer, payload))
                        break;
 
                p = next;
@@ -483,8 +571,7 @@ static int ioctl_queue_iso(struct client *client, void __user *arg)
 
        request.size    -= uptr_to_u64(p) - request.packets;
        request.packets  = uptr_to_u64(p);
-       request.data     =
-               client->vm_start + (payload - client->iso_context->buffer);
+       request.data     = client->vm_start + payload;
 
        if (copy_to_user(arg, &request, sizeof request))
                return -EFAULT;
@@ -492,15 +579,19 @@ static int ioctl_queue_iso(struct client *client, void __user *arg)
        return count;
 }
 
-static int ioctl_send_iso(struct client *client, void __user *arg)
+static int ioctl_start_iso(struct client *client, void __user *arg)
 {
-       struct fw_cdev_send_iso request;
+       struct fw_cdev_start_iso request;
 
        if (copy_from_user(&request, arg, sizeof request))
                return -EFAULT;
 
-       return fw_iso_context_send(client->iso_context, request.channel,
-                                  request.speed, request.cycle);
+       return fw_iso_context_start(client->iso_context, request.cycle);
+}
+
+static int ioctl_stop_iso(struct client *client, void __user *arg)
+{
+       return fw_iso_context_stop(client->iso_context);
 }
 
 static int
@@ -519,8 +610,10 @@ dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
                return ioctl_create_iso_context(client, arg);
        case FW_CDEV_IOC_QUEUE_ISO:
                return ioctl_queue_iso(client, arg);
-       case FW_CDEV_IOC_SEND_ISO:
-               return ioctl_send_iso(client, arg);
+       case FW_CDEV_IOC_START_ISO:
+               return ioctl_start_iso(client, arg);
+       case FW_CDEV_IOC_STOP_ISO:
+               return ioctl_stop_iso(client, arg);
        default:
                return -EINVAL;
        }
@@ -549,13 +642,41 @@ fw_device_op_compat_ioctl(struct file *file,
 static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
 {
        struct client *client = file->private_data;
+       enum dma_data_direction direction;
+       unsigned long size;
+       int page_count, retval;
+
+       /* FIXME: We could support multiple buffers, but we don't. */
+       if (client->buffer.pages != NULL)
+               return -EBUSY;
 
-       if (client->iso_context->buffer == NULL)
+       if (!(vma->vm_flags & VM_SHARED))
+               return -EINVAL;
+
+       if (vma->vm_start & ~PAGE_MASK)
                return -EINVAL;
 
        client->vm_start = vma->vm_start;
+       size = vma->vm_end - vma->vm_start;
+       page_count = size >> PAGE_SHIFT;
+       if (size & ~PAGE_MASK)
+               return -EINVAL;
+
+       if (vma->vm_flags & VM_WRITE)
+               direction = DMA_TO_DEVICE;
+       else
+               direction = DMA_FROM_DEVICE;
+
+       retval = fw_iso_buffer_init(&client->buffer, client->device->card,
+                                   page_count, direction);
+       if (retval < 0)
+               return retval;
 
-       return remap_vmalloc_range(vma, client->iso_context->buffer, 0);
+       retval = fw_iso_buffer_map(&client->buffer, vma);
+       if (retval < 0)
+               fw_iso_buffer_destroy(&client->buffer, client->device->card);
+
+       return retval;
 }
 
 static int fw_device_op_release(struct inode *inode, struct file *file)
@@ -563,6 +684,10 @@ static int fw_device_op_release(struct inode *inode, struct file *file)
        struct client *client = file->private_data;
        struct address_handler *h, *next;
        struct request *r, *next_r;
+       unsigned long flags;
+
+       if (client->buffer.pages)
+               fw_iso_buffer_destroy(&client->buffer, client->device->card);
 
        if (client->iso_context)
                fw_iso_context_destroy(client->iso_context);
@@ -584,6 +709,10 @@ static int fw_device_op_release(struct inode *inode, struct file *file)
        while (!list_empty(&client->event_list))
                dequeue_event(client, NULL, 0);
 
+       spin_lock_irqsave(&client->device->card->lock, flags);
+       list_del(&client->link);
+       spin_unlock_irqrestore(&client->device->card->lock, flags);
+
        fw_device_put(client->device);
        kfree(client);
 
@@ -602,7 +731,7 @@ static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
                return 0;
 }
 
-struct file_operations fw_device_ops = {
+const struct file_operations fw_device_ops = {
        .owner          = THIS_MODULE,
        .open           = fw_device_op_open,
        .read           = fw_device_op_read,
@@ -612,6 +741,6 @@ struct file_operations fw_device_ops = {
        .mmap           = fw_device_op_mmap,
 
 #ifdef CONFIG_COMPAT
-       .compat_ioctl   = fw_device_op_compat_ioctl
+       .compat_ioctl   = fw_device_op_compat_ioctl,
 #endif
 };