Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Char device for device raw access |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 3 | * |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 4 | * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net> |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software Foundation, |
| 18 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/wait.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/device.h> |
| 26 | #include <linux/vmalloc.h> |
| 27 | #include <linux/poll.h> |
Stefan Richter | a64408b | 2007-09-29 10:41:58 +0200 | [diff] [blame] | 28 | #include <linux/preempt.h> |
| 29 | #include <linux/time.h> |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 30 | #include <linux/delay.h> |
| 31 | #include <linux/mm.h> |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 32 | #include <linux/idr.h> |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 33 | #include <linux/compat.h> |
Kristian Høgsberg | 9640d3d | 2007-04-30 15:03:15 -0400 | [diff] [blame] | 34 | #include <linux/firewire-cdev.h> |
Stefan Richter | a64408b | 2007-09-29 10:41:58 +0200 | [diff] [blame] | 35 | #include <asm/system.h> |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 36 | #include <asm/uaccess.h> |
| 37 | #include "fw-transaction.h" |
| 38 | #include "fw-topology.h" |
| 39 | #include "fw-device.h" |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 40 | |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 41 | struct client; |
| 42 | struct client_resource { |
| 43 | struct list_head link; |
| 44 | void (*release)(struct client *client, struct client_resource *r); |
| 45 | u32 handle; |
| 46 | }; |
| 47 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 48 | /* |
| 49 | * dequeue_event() just kfree()'s the event, so the event has to be |
| 50 | * the first field in the struct. |
| 51 | */ |
| 52 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 53 | struct event { |
| 54 | struct { void *data; size_t size; } v[2]; |
| 55 | struct list_head link; |
| 56 | }; |
| 57 | |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 58 | struct bus_reset { |
| 59 | struct event event; |
| 60 | struct fw_cdev_event_bus_reset reset; |
| 61 | }; |
| 62 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 63 | struct response { |
| 64 | struct event event; |
| 65 | struct fw_transaction transaction; |
| 66 | struct client *client; |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 67 | struct client_resource resource; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 68 | struct fw_cdev_event_response response; |
| 69 | }; |
| 70 | |
| 71 | struct iso_interrupt { |
| 72 | struct event event; |
| 73 | struct fw_cdev_event_iso_interrupt interrupt; |
| 74 | }; |
| 75 | |
| 76 | struct client { |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 77 | u32 version; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 78 | struct fw_device *device; |
| 79 | spinlock_t lock; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 80 | u32 resource_handle; |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 81 | struct list_head resource_list; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 82 | struct list_head event_list; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 83 | wait_queue_head_t wait; |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 84 | u64 bus_reset_closure; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 85 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 86 | struct fw_iso_context *iso_context; |
Kristian Høgsberg | abaa574 | 2007-04-30 15:03:14 -0400 | [diff] [blame] | 87 | u64 iso_closure; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 88 | struct fw_iso_buffer buffer; |
| 89 | unsigned long vm_start; |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 90 | |
| 91 | struct list_head link; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | static inline void __user * |
| 95 | u64_to_uptr(__u64 value) |
| 96 | { |
| 97 | return (void __user *)(unsigned long)value; |
| 98 | } |
| 99 | |
| 100 | static inline __u64 |
| 101 | uptr_to_u64(void __user *ptr) |
| 102 | { |
| 103 | return (__u64)(unsigned long)ptr; |
| 104 | } |
| 105 | |
| 106 | static int fw_device_op_open(struct inode *inode, struct file *file) |
| 107 | { |
| 108 | struct fw_device *device; |
| 109 | struct client *client; |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 110 | unsigned long flags; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 111 | |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 112 | device = fw_device_get_by_devt(inode->i_rdev); |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 113 | if (device == NULL) |
| 114 | return -ENODEV; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 115 | |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 116 | client = kzalloc(sizeof(*client), GFP_KERNEL); |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 117 | if (client == NULL) { |
| 118 | fw_device_put(device); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 119 | return -ENOMEM; |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 120 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 121 | |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 122 | client->device = device; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 123 | INIT_LIST_HEAD(&client->event_list); |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 124 | INIT_LIST_HEAD(&client->resource_list); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 125 | spin_lock_init(&client->lock); |
| 126 | init_waitqueue_head(&client->wait); |
| 127 | |
| 128 | file->private_data = client; |
| 129 | |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 130 | spin_lock_irqsave(&device->card->lock, flags); |
| 131 | list_add_tail(&client->link, &device->client_list); |
| 132 | spin_unlock_irqrestore(&device->card->lock, flags); |
| 133 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static void queue_event(struct client *client, struct event *event, |
| 138 | void *data0, size_t size0, void *data1, size_t size1) |
| 139 | { |
| 140 | unsigned long flags; |
| 141 | |
| 142 | event->v[0].data = data0; |
| 143 | event->v[0].size = size0; |
| 144 | event->v[1].data = data1; |
| 145 | event->v[1].size = size1; |
| 146 | |
| 147 | spin_lock_irqsave(&client->lock, flags); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 148 | list_add_tail(&event->link, &client->event_list); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 149 | spin_unlock_irqrestore(&client->lock, flags); |
Jay Fenlason | 83431cb | 2007-10-08 17:00:29 -0400 | [diff] [blame] | 150 | |
| 151 | wake_up_interruptible(&client->wait); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 154 | static int |
| 155 | dequeue_event(struct client *client, char __user *buffer, size_t count) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 156 | { |
| 157 | unsigned long flags; |
| 158 | struct event *event; |
| 159 | size_t size, total; |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 160 | int i, retval; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 161 | |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 162 | retval = wait_event_interruptible(client->wait, |
| 163 | !list_empty(&client->event_list) || |
| 164 | fw_device_is_shutdown(client->device)); |
| 165 | if (retval < 0) |
| 166 | return retval; |
| 167 | |
| 168 | if (list_empty(&client->event_list) && |
| 169 | fw_device_is_shutdown(client->device)) |
| 170 | return -ENODEV; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 171 | |
| 172 | spin_lock_irqsave(&client->lock, flags); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 173 | event = container_of(client->event_list.next, struct event, link); |
| 174 | list_del(&event->link); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 175 | spin_unlock_irqrestore(&client->lock, flags); |
| 176 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 177 | total = 0; |
| 178 | for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) { |
| 179 | size = min(event->v[i].size, count - total); |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 180 | if (copy_to_user(buffer + total, event->v[i].data, size)) { |
| 181 | retval = -EFAULT; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 182 | goto out; |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 183 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 184 | total += size; |
| 185 | } |
| 186 | retval = total; |
| 187 | |
| 188 | out: |
| 189 | kfree(event); |
| 190 | |
| 191 | return retval; |
| 192 | } |
| 193 | |
| 194 | static ssize_t |
| 195 | fw_device_op_read(struct file *file, |
| 196 | char __user *buffer, size_t count, loff_t *offset) |
| 197 | { |
| 198 | struct client *client = file->private_data; |
| 199 | |
| 200 | return dequeue_event(client, buffer, count); |
| 201 | } |
| 202 | |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 203 | static void |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 204 | fill_bus_reset_event(struct fw_cdev_event_bus_reset *event, |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 205 | struct client *client) |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 206 | { |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 207 | struct fw_card *card = client->device->card; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 208 | |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 209 | event->closure = client->bus_reset_closure; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 210 | event->type = FW_CDEV_EVENT_BUS_RESET; |
Stefan Richter | cf5a56a | 2008-01-24 01:53:51 +0100 | [diff] [blame] | 211 | event->generation = client->device->generation; |
Stefan Richter | b5d2a5e | 2008-01-25 18:57:41 +0100 | [diff] [blame] | 212 | smp_rmb(); /* node_id must not be older than generation */ |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 213 | event->node_id = client->device->node_id; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 214 | event->local_node_id = card->local_node->node_id; |
| 215 | event->bm_node_id = 0; /* FIXME: We don't track the BM. */ |
| 216 | event->irm_node_id = card->irm_node->node_id; |
| 217 | event->root_node_id = card->root_node->node_id; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static void |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 221 | for_each_client(struct fw_device *device, |
| 222 | void (*callback)(struct client *client)) |
| 223 | { |
| 224 | struct fw_card *card = device->card; |
| 225 | struct client *c; |
| 226 | unsigned long flags; |
| 227 | |
| 228 | spin_lock_irqsave(&card->lock, flags); |
| 229 | |
| 230 | list_for_each_entry(c, &device->client_list, link) |
| 231 | callback(c); |
| 232 | |
| 233 | spin_unlock_irqrestore(&card->lock, flags); |
| 234 | } |
| 235 | |
| 236 | static void |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 237 | queue_bus_reset_event(struct client *client) |
| 238 | { |
| 239 | struct bus_reset *bus_reset; |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 240 | |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 241 | bus_reset = kzalloc(sizeof(*bus_reset), GFP_ATOMIC); |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 242 | if (bus_reset == NULL) { |
| 243 | fw_notify("Out of memory when allocating bus reset event\n"); |
| 244 | return; |
| 245 | } |
| 246 | |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 247 | fill_bus_reset_event(&bus_reset->reset, client); |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 248 | |
| 249 | queue_event(client, &bus_reset->event, |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 250 | &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0); |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void fw_device_cdev_update(struct fw_device *device) |
| 254 | { |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 255 | for_each_client(device, queue_bus_reset_event); |
| 256 | } |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 257 | |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 258 | static void wake_up_client(struct client *client) |
| 259 | { |
| 260 | wake_up_interruptible(&client->wait); |
| 261 | } |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 262 | |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 263 | void fw_device_cdev_remove(struct fw_device *device) |
| 264 | { |
| 265 | for_each_client(device, wake_up_client); |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 266 | } |
| 267 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 268 | static int ioctl_get_info(struct client *client, void *buffer) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 269 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 270 | struct fw_cdev_get_info *get_info = buffer; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 271 | struct fw_cdev_event_bus_reset bus_reset; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 272 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 273 | client->version = get_info->version; |
| 274 | get_info->version = FW_CDEV_VERSION; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 275 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 276 | if (get_info->rom != 0) { |
| 277 | void __user *uptr = u64_to_uptr(get_info->rom); |
| 278 | size_t want = get_info->rom_length; |
Stefan Richter | d84702a | 2007-03-20 19:42:15 +0100 | [diff] [blame] | 279 | size_t have = client->device->config_rom_length * 4; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 280 | |
Stefan Richter | d84702a | 2007-03-20 19:42:15 +0100 | [diff] [blame] | 281 | if (copy_to_user(uptr, client->device->config_rom, |
| 282 | min(want, have))) |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 283 | return -EFAULT; |
| 284 | } |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 285 | get_info->rom_length = client->device->config_rom_length * 4; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 286 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 287 | client->bus_reset_closure = get_info->bus_reset_closure; |
| 288 | if (get_info->bus_reset != 0) { |
| 289 | void __user *uptr = u64_to_uptr(get_info->bus_reset); |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 290 | |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 291 | fill_bus_reset_event(&bus_reset, client); |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 292 | if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset))) |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 293 | return -EFAULT; |
| 294 | } |
| 295 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 296 | get_info->card = client->device->card->index; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static void |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 302 | add_client_resource(struct client *client, struct client_resource *resource) |
| 303 | { |
| 304 | unsigned long flags; |
| 305 | |
| 306 | spin_lock_irqsave(&client->lock, flags); |
| 307 | list_add_tail(&resource->link, &client->resource_list); |
| 308 | resource->handle = client->resource_handle++; |
| 309 | spin_unlock_irqrestore(&client->lock, flags); |
| 310 | } |
| 311 | |
| 312 | static int |
| 313 | release_client_resource(struct client *client, u32 handle, |
| 314 | struct client_resource **resource) |
| 315 | { |
| 316 | struct client_resource *r; |
| 317 | unsigned long flags; |
| 318 | |
| 319 | spin_lock_irqsave(&client->lock, flags); |
| 320 | list_for_each_entry(r, &client->resource_list, link) { |
| 321 | if (r->handle == handle) { |
| 322 | list_del(&r->link); |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | spin_unlock_irqrestore(&client->lock, flags); |
| 327 | |
| 328 | if (&r->link == &client->resource_list) |
| 329 | return -EINVAL; |
| 330 | |
| 331 | if (resource) |
| 332 | *resource = r; |
| 333 | else |
| 334 | r->release(client, r); |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static void |
| 340 | release_transaction(struct client *client, struct client_resource *resource) |
| 341 | { |
| 342 | struct response *response = |
| 343 | container_of(resource, struct response, resource); |
| 344 | |
| 345 | fw_cancel_transaction(client->device->card, &response->transaction); |
| 346 | } |
| 347 | |
| 348 | static void |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 349 | complete_transaction(struct fw_card *card, int rcode, |
| 350 | void *payload, size_t length, void *data) |
| 351 | { |
| 352 | struct response *response = data; |
| 353 | struct client *client = response->client; |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 354 | unsigned long flags; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 355 | |
| 356 | if (length < response->response.length) |
| 357 | response->response.length = length; |
| 358 | if (rcode == RCODE_COMPLETE) |
| 359 | memcpy(response->response.data, payload, |
| 360 | response->response.length); |
| 361 | |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 362 | spin_lock_irqsave(&client->lock, flags); |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 363 | list_del(&response->resource.link); |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 364 | spin_unlock_irqrestore(&client->lock, flags); |
| 365 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 366 | response->response.type = FW_CDEV_EVENT_RESPONSE; |
| 367 | response->response.rcode = rcode; |
| 368 | queue_event(client, &response->event, |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 369 | &response->response, sizeof(response->response), |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 370 | response->response.data, response->response.length); |
| 371 | } |
| 372 | |
Jeff Garzik | 350958f | 2007-05-27 07:09:18 -0400 | [diff] [blame] | 373 | static int ioctl_send_request(struct client *client, void *buffer) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 374 | { |
| 375 | struct fw_device *device = client->device; |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 376 | struct fw_cdev_send_request *request = buffer; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 377 | struct response *response; |
| 378 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 379 | /* What is the biggest size we'll accept, really? */ |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 380 | if (request->length > 4096) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 381 | return -EINVAL; |
| 382 | |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 383 | response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 384 | if (response == NULL) |
| 385 | return -ENOMEM; |
| 386 | |
| 387 | response->client = client; |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 388 | response->response.length = request->length; |
| 389 | response->response.closure = request->closure; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 390 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 391 | if (request->data && |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 392 | copy_from_user(response->response.data, |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 393 | u64_to_uptr(request->data), request->length)) { |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 394 | kfree(response); |
| 395 | return -EFAULT; |
| 396 | } |
| 397 | |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 398 | response->resource.release = release_transaction; |
| 399 | add_client_resource(client, &response->resource); |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 400 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 401 | fw_send_request(device->card, &response->transaction, |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 402 | request->tcode & 0x1f, |
Stefan Richter | 907293d | 2007-01-23 21:11:43 +0100 | [diff] [blame] | 403 | device->node->node_id, |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 404 | request->generation, |
Stefan Richter | f139749 | 2007-06-10 21:31:36 +0200 | [diff] [blame] | 405 | device->max_speed, |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 406 | request->offset, |
| 407 | response->response.data, request->length, |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 408 | complete_transaction, response); |
| 409 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 410 | if (request->data) |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 411 | return sizeof(request) + request->length; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 412 | else |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 413 | return sizeof(request); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | struct address_handler { |
| 417 | struct fw_address_handler handler; |
| 418 | __u64 closure; |
| 419 | struct client *client; |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 420 | struct client_resource resource; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 421 | }; |
| 422 | |
| 423 | struct request { |
| 424 | struct fw_request *request; |
| 425 | void *data; |
| 426 | size_t length; |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 427 | struct client_resource resource; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 428 | }; |
| 429 | |
| 430 | struct request_event { |
| 431 | struct event event; |
| 432 | struct fw_cdev_event_request request; |
| 433 | }; |
| 434 | |
| 435 | static void |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 436 | release_request(struct client *client, struct client_resource *resource) |
| 437 | { |
| 438 | struct request *request = |
| 439 | container_of(resource, struct request, resource); |
| 440 | |
| 441 | fw_send_response(client->device->card, request->request, |
| 442 | RCODE_CONFLICT_ERROR); |
| 443 | kfree(request); |
| 444 | } |
| 445 | |
| 446 | static void |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 447 | handle_request(struct fw_card *card, struct fw_request *r, |
| 448 | int tcode, int destination, int source, |
| 449 | int generation, int speed, |
| 450 | unsigned long long offset, |
| 451 | void *payload, size_t length, void *callback_data) |
| 452 | { |
| 453 | struct address_handler *handler = callback_data; |
| 454 | struct request *request; |
| 455 | struct request_event *e; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 456 | struct client *client = handler->client; |
| 457 | |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 458 | request = kmalloc(sizeof(*request), GFP_ATOMIC); |
| 459 | e = kmalloc(sizeof(*e), GFP_ATOMIC); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 460 | if (request == NULL || e == NULL) { |
| 461 | kfree(request); |
| 462 | kfree(e); |
| 463 | fw_send_response(card, r, RCODE_CONFLICT_ERROR); |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | request->request = r; |
| 468 | request->data = payload; |
| 469 | request->length = length; |
| 470 | |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 471 | request->resource.release = release_request; |
| 472 | add_client_resource(client, &request->resource); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 473 | |
| 474 | e->request.type = FW_CDEV_EVENT_REQUEST; |
| 475 | e->request.tcode = tcode; |
| 476 | e->request.offset = offset; |
| 477 | e->request.length = length; |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 478 | e->request.handle = request->resource.handle; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 479 | e->request.closure = handler->closure; |
| 480 | |
| 481 | queue_event(client, &e->event, |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 482 | &e->request, sizeof(e->request), payload, length); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 483 | } |
| 484 | |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 485 | static void |
| 486 | release_address_handler(struct client *client, |
| 487 | struct client_resource *resource) |
| 488 | { |
| 489 | struct address_handler *handler = |
| 490 | container_of(resource, struct address_handler, resource); |
| 491 | |
| 492 | fw_core_remove_address_handler(&handler->handler); |
| 493 | kfree(handler); |
| 494 | } |
| 495 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 496 | static int ioctl_allocate(struct client *client, void *buffer) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 497 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 498 | struct fw_cdev_allocate *request = buffer; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 499 | struct address_handler *handler; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 500 | struct fw_address_region region; |
| 501 | |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 502 | handler = kmalloc(sizeof(*handler), GFP_KERNEL); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 503 | if (handler == NULL) |
| 504 | return -ENOMEM; |
| 505 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 506 | region.start = request->offset; |
| 507 | region.end = request->offset + request->length; |
| 508 | handler->handler.length = request->length; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 509 | handler->handler.address_callback = handle_request; |
| 510 | handler->handler.callback_data = handler; |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 511 | handler->closure = request->closure; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 512 | handler->client = client; |
| 513 | |
| 514 | if (fw_core_add_address_handler(&handler->handler, ®ion) < 0) { |
| 515 | kfree(handler); |
| 516 | return -EBUSY; |
| 517 | } |
| 518 | |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 519 | handler->resource.release = release_address_handler; |
| 520 | add_client_resource(client, &handler->resource); |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 521 | request->handle = handler->resource.handle; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 526 | static int ioctl_deallocate(struct client *client, void *buffer) |
Kristian Høgsberg | 9472316 | 2007-03-14 17:34:55 -0400 | [diff] [blame] | 527 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 528 | struct fw_cdev_deallocate *request = buffer; |
Kristian Høgsberg | 9472316 | 2007-03-14 17:34:55 -0400 | [diff] [blame] | 529 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 530 | return release_client_resource(client, request->handle, NULL); |
Kristian Høgsberg | 9472316 | 2007-03-14 17:34:55 -0400 | [diff] [blame] | 531 | } |
| 532 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 533 | static int ioctl_send_response(struct client *client, void *buffer) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 534 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 535 | struct fw_cdev_send_response *request = buffer; |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 536 | struct client_resource *resource; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 537 | struct request *r; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 538 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 539 | if (release_client_resource(client, request->handle, &resource) < 0) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 540 | return -EINVAL; |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 541 | r = container_of(resource, struct request, resource); |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 542 | if (request->length < r->length) |
| 543 | r->length = request->length; |
| 544 | if (copy_from_user(r->data, u64_to_uptr(request->data), r->length)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 545 | return -EFAULT; |
| 546 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 547 | fw_send_response(client->device->card, r->request, request->rcode); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 548 | kfree(r); |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 553 | static int ioctl_initiate_bus_reset(struct client *client, void *buffer) |
Kristian Høgsberg | 5371842 | 2007-03-07 12:12:42 -0500 | [diff] [blame] | 554 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 555 | struct fw_cdev_initiate_bus_reset *request = buffer; |
Kristian Høgsberg | 5371842 | 2007-03-07 12:12:42 -0500 | [diff] [blame] | 556 | int short_reset; |
| 557 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 558 | short_reset = (request->type == FW_CDEV_SHORT_RESET); |
Kristian Høgsberg | 5371842 | 2007-03-07 12:12:42 -0500 | [diff] [blame] | 559 | |
| 560 | return fw_core_initiate_bus_reset(client->device->card, short_reset); |
| 561 | } |
| 562 | |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 563 | struct descriptor { |
| 564 | struct fw_descriptor d; |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 565 | struct client_resource resource; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 566 | u32 data[0]; |
| 567 | }; |
| 568 | |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 569 | static void release_descriptor(struct client *client, |
| 570 | struct client_resource *resource) |
| 571 | { |
| 572 | struct descriptor *descriptor = |
| 573 | container_of(resource, struct descriptor, resource); |
| 574 | |
| 575 | fw_core_remove_descriptor(&descriptor->d); |
| 576 | kfree(descriptor); |
| 577 | } |
| 578 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 579 | static int ioctl_add_descriptor(struct client *client, void *buffer) |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 580 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 581 | struct fw_cdev_add_descriptor *request = buffer; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 582 | struct descriptor *descriptor; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 583 | int retval; |
| 584 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 585 | if (request->length > 256) |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 586 | return -EINVAL; |
| 587 | |
| 588 | descriptor = |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 589 | kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL); |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 590 | if (descriptor == NULL) |
| 591 | return -ENOMEM; |
| 592 | |
| 593 | if (copy_from_user(descriptor->data, |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 594 | u64_to_uptr(request->data), request->length * 4)) { |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 595 | kfree(descriptor); |
| 596 | return -EFAULT; |
| 597 | } |
| 598 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 599 | descriptor->d.length = request->length; |
| 600 | descriptor->d.immediate = request->immediate; |
| 601 | descriptor->d.key = request->key; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 602 | descriptor->d.data = descriptor->data; |
| 603 | |
| 604 | retval = fw_core_add_descriptor(&descriptor->d); |
| 605 | if (retval < 0) { |
| 606 | kfree(descriptor); |
| 607 | return retval; |
| 608 | } |
| 609 | |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 610 | descriptor->resource.release = release_descriptor; |
| 611 | add_client_resource(client, &descriptor->resource); |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 612 | request->handle = descriptor->resource.handle; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 617 | static int ioctl_remove_descriptor(struct client *client, void *buffer) |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 618 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 619 | struct fw_cdev_remove_descriptor *request = buffer; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 620 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 621 | return release_client_resource(client, request->handle, NULL); |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 622 | } |
| 623 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 624 | static void |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 625 | iso_callback(struct fw_iso_context *context, u32 cycle, |
| 626 | size_t header_length, void *header, void *data) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 627 | { |
| 628 | struct client *client = data; |
Stefan Richter | 930e4b7 | 2007-08-03 20:56:31 +0200 | [diff] [blame] | 629 | struct iso_interrupt *irq; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 630 | |
Stefan Richter | 930e4b7 | 2007-08-03 20:56:31 +0200 | [diff] [blame] | 631 | irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC); |
| 632 | if (irq == NULL) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 633 | return; |
| 634 | |
Stefan Richter | 930e4b7 | 2007-08-03 20:56:31 +0200 | [diff] [blame] | 635 | irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT; |
| 636 | irq->interrupt.closure = client->iso_closure; |
| 637 | irq->interrupt.cycle = cycle; |
| 638 | irq->interrupt.header_length = header_length; |
| 639 | memcpy(irq->interrupt.header, header, header_length); |
| 640 | queue_event(client, &irq->event, &irq->interrupt, |
| 641 | sizeof(irq->interrupt) + header_length, NULL, 0); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 642 | } |
| 643 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 644 | static int ioctl_create_iso_context(struct client *client, void *buffer) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 645 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 646 | struct fw_cdev_create_iso_context *request = buffer; |
Kristian Høgsberg | 24315c5 | 2007-06-20 17:48:07 -0400 | [diff] [blame] | 647 | struct fw_iso_context *context; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 648 | |
Stefan Richter | fae6031 | 2008-02-20 21:10:06 +0100 | [diff] [blame] | 649 | /* We only support one context at this time. */ |
| 650 | if (client->iso_context != NULL) |
| 651 | return -EBUSY; |
| 652 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 653 | if (request->channel > 63) |
Kristian Høgsberg | 21efb3c | 2007-02-16 17:34:50 -0500 | [diff] [blame] | 654 | return -EINVAL; |
| 655 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 656 | switch (request->type) { |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 657 | case FW_ISO_CONTEXT_RECEIVE: |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 658 | if (request->header_size < 4 || (request->header_size & 3)) |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 659 | return -EINVAL; |
| 660 | |
| 661 | break; |
| 662 | |
| 663 | case FW_ISO_CONTEXT_TRANSMIT: |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 664 | if (request->speed > SCODE_3200) |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 665 | return -EINVAL; |
| 666 | |
| 667 | break; |
| 668 | |
| 669 | default: |
Kristian Høgsberg | 21efb3c | 2007-02-16 17:34:50 -0500 | [diff] [blame] | 670 | return -EINVAL; |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 671 | } |
| 672 | |
Kristian Høgsberg | 24315c5 | 2007-06-20 17:48:07 -0400 | [diff] [blame] | 673 | context = fw_iso_context_create(client->device->card, |
| 674 | request->type, |
| 675 | request->channel, |
| 676 | request->speed, |
| 677 | request->header_size, |
| 678 | iso_callback, client); |
| 679 | if (IS_ERR(context)) |
| 680 | return PTR_ERR(context); |
| 681 | |
Kristian Høgsberg | abaa574 | 2007-04-30 15:03:14 -0400 | [diff] [blame] | 682 | client->iso_closure = request->closure; |
Kristian Høgsberg | 24315c5 | 2007-06-20 17:48:07 -0400 | [diff] [blame] | 683 | client->iso_context = context; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 684 | |
Kristian Høgsberg | abaa574 | 2007-04-30 15:03:14 -0400 | [diff] [blame] | 685 | /* We only support one context at this time. */ |
| 686 | request->handle = 0; |
| 687 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 688 | return 0; |
| 689 | } |
| 690 | |
Kristian Høgsberg | 1ca31ae | 2007-05-31 11:16:43 -0400 | [diff] [blame] | 691 | /* Macros for decoding the iso packet control header. */ |
| 692 | #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff) |
| 693 | #define GET_INTERRUPT(v) (((v) >> 16) & 0x01) |
| 694 | #define GET_SKIP(v) (((v) >> 17) & 0x01) |
| 695 | #define GET_TAG(v) (((v) >> 18) & 0x02) |
| 696 | #define GET_SY(v) (((v) >> 20) & 0x04) |
| 697 | #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff) |
| 698 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 699 | static int ioctl_queue_iso(struct client *client, void *buffer) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 700 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 701 | struct fw_cdev_queue_iso *request = buffer; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 702 | struct fw_cdev_iso_packet __user *p, *end, *next; |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 703 | struct fw_iso_context *ctx = client->iso_context; |
Kristian Høgsberg | ef370ee | 2007-03-28 20:46:23 +0200 | [diff] [blame] | 704 | unsigned long payload, buffer_end, header_length; |
Kristian Høgsberg | 1ca31ae | 2007-05-31 11:16:43 -0400 | [diff] [blame] | 705 | u32 control; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 706 | int count; |
| 707 | struct { |
| 708 | struct fw_iso_packet packet; |
| 709 | u8 header[256]; |
| 710 | } u; |
| 711 | |
Kristian Høgsberg | abaa574 | 2007-04-30 15:03:14 -0400 | [diff] [blame] | 712 | if (ctx == NULL || request->handle != 0) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 713 | return -EINVAL; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 714 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 715 | /* |
| 716 | * If the user passes a non-NULL data pointer, has mmap()'ed |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 717 | * the iso buffer, and the pointer points inside the buffer, |
| 718 | * we setup the payload pointers accordingly. Otherwise we |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 719 | * set them both to 0, which will still let packets with |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 720 | * payload_length == 0 through. In other words, if no packets |
| 721 | * use the indirect payload, the iso buffer need not be mapped |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 722 | * and the request->data pointer is ignored. |
| 723 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 724 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 725 | payload = (unsigned long)request->data - client->vm_start; |
Kristian Høgsberg | ef370ee | 2007-03-28 20:46:23 +0200 | [diff] [blame] | 726 | buffer_end = client->buffer.page_count << PAGE_SHIFT; |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 727 | if (request->data == 0 || client->buffer.pages == NULL || |
Kristian Høgsberg | ef370ee | 2007-03-28 20:46:23 +0200 | [diff] [blame] | 728 | payload >= buffer_end) { |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 729 | payload = 0; |
Kristian Høgsberg | ef370ee | 2007-03-28 20:46:23 +0200 | [diff] [blame] | 730 | buffer_end = 0; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 731 | } |
| 732 | |
Al Viro | 1ccc914 | 2007-10-14 19:34:40 +0100 | [diff] [blame] | 733 | p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets); |
| 734 | |
| 735 | if (!access_ok(VERIFY_READ, p, request->size)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 736 | return -EFAULT; |
| 737 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 738 | end = (void __user *)p + request->size; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 739 | count = 0; |
| 740 | while (p < end) { |
Kristian Høgsberg | 1ca31ae | 2007-05-31 11:16:43 -0400 | [diff] [blame] | 741 | if (get_user(control, &p->control)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 742 | return -EFAULT; |
Kristian Høgsberg | 1ca31ae | 2007-05-31 11:16:43 -0400 | [diff] [blame] | 743 | u.packet.payload_length = GET_PAYLOAD_LENGTH(control); |
| 744 | u.packet.interrupt = GET_INTERRUPT(control); |
| 745 | u.packet.skip = GET_SKIP(control); |
| 746 | u.packet.tag = GET_TAG(control); |
| 747 | u.packet.sy = GET_SY(control); |
| 748 | u.packet.header_length = GET_HEADER_LENGTH(control); |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 749 | |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 750 | if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) { |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 751 | header_length = u.packet.header_length; |
| 752 | } else { |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 753 | /* |
| 754 | * We require that header_length is a multiple of |
| 755 | * the fixed header size, ctx->header_size. |
| 756 | */ |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 757 | if (ctx->header_size == 0) { |
| 758 | if (u.packet.header_length > 0) |
| 759 | return -EINVAL; |
| 760 | } else if (u.packet.header_length % ctx->header_size != 0) { |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 761 | return -EINVAL; |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 762 | } |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 763 | header_length = 0; |
| 764 | } |
| 765 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 766 | next = (struct fw_cdev_iso_packet __user *) |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 767 | &p->header[header_length / 4]; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 768 | if (next > end) |
| 769 | return -EINVAL; |
| 770 | if (__copy_from_user |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 771 | (u.packet.header, p->header, header_length)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 772 | return -EFAULT; |
Kristian Høgsberg | 98b6cbe | 2007-02-16 17:34:51 -0500 | [diff] [blame] | 773 | if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT && |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 774 | u.packet.header_length + u.packet.payload_length > 0) |
| 775 | return -EINVAL; |
Kristian Høgsberg | ef370ee | 2007-03-28 20:46:23 +0200 | [diff] [blame] | 776 | if (payload + u.packet.payload_length > buffer_end) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 777 | return -EINVAL; |
| 778 | |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 779 | if (fw_iso_context_queue(ctx, &u.packet, |
| 780 | &client->buffer, payload)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 781 | break; |
| 782 | |
| 783 | p = next; |
| 784 | payload += u.packet.payload_length; |
| 785 | count++; |
| 786 | } |
| 787 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 788 | request->size -= uptr_to_u64(p) - request->packets; |
| 789 | request->packets = uptr_to_u64(p); |
| 790 | request->data = client->vm_start + payload; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 791 | |
| 792 | return count; |
| 793 | } |
| 794 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 795 | static int ioctl_start_iso(struct client *client, void *buffer) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 796 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 797 | struct fw_cdev_start_iso *request = buffer; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 798 | |
Stefan Richter | fae6031 | 2008-02-20 21:10:06 +0100 | [diff] [blame] | 799 | if (client->iso_context == NULL || request->handle != 0) |
Kristian Høgsberg | abaa574 | 2007-04-30 15:03:14 -0400 | [diff] [blame] | 800 | return -EINVAL; |
Stefan Richter | fae6031 | 2008-02-20 21:10:06 +0100 | [diff] [blame] | 801 | |
Kristian Høgsberg | eb0306e | 2007-03-14 17:34:54 -0400 | [diff] [blame] | 802 | if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 803 | if (request->tags == 0 || request->tags > 15) |
Kristian Høgsberg | eb0306e | 2007-03-14 17:34:54 -0400 | [diff] [blame] | 804 | return -EINVAL; |
| 805 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 806 | if (request->sync > 15) |
Kristian Høgsberg | eb0306e | 2007-03-14 17:34:54 -0400 | [diff] [blame] | 807 | return -EINVAL; |
| 808 | } |
| 809 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 810 | return fw_iso_context_start(client->iso_context, request->cycle, |
| 811 | request->sync, request->tags); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 812 | } |
| 813 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 814 | static int ioctl_stop_iso(struct client *client, void *buffer) |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 815 | { |
Kristian Høgsberg | abaa574 | 2007-04-30 15:03:14 -0400 | [diff] [blame] | 816 | struct fw_cdev_stop_iso *request = buffer; |
| 817 | |
Stefan Richter | fae6031 | 2008-02-20 21:10:06 +0100 | [diff] [blame] | 818 | if (client->iso_context == NULL || request->handle != 0) |
Kristian Høgsberg | abaa574 | 2007-04-30 15:03:14 -0400 | [diff] [blame] | 819 | return -EINVAL; |
| 820 | |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 821 | return fw_iso_context_stop(client->iso_context); |
| 822 | } |
| 823 | |
Stefan Richter | a64408b | 2007-09-29 10:41:58 +0200 | [diff] [blame] | 824 | static int ioctl_get_cycle_timer(struct client *client, void *buffer) |
| 825 | { |
| 826 | struct fw_cdev_get_cycle_timer *request = buffer; |
| 827 | struct fw_card *card = client->device->card; |
| 828 | unsigned long long bus_time; |
| 829 | struct timeval tv; |
| 830 | unsigned long flags; |
| 831 | |
| 832 | preempt_disable(); |
| 833 | local_irq_save(flags); |
| 834 | |
| 835 | bus_time = card->driver->get_bus_time(card); |
| 836 | do_gettimeofday(&tv); |
| 837 | |
| 838 | local_irq_restore(flags); |
| 839 | preempt_enable(); |
| 840 | |
| 841 | request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec; |
| 842 | request->cycle_timer = bus_time & 0xffffffff; |
| 843 | return 0; |
| 844 | } |
| 845 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 846 | static int (* const ioctl_handlers[])(struct client *client, void *buffer) = { |
| 847 | ioctl_get_info, |
| 848 | ioctl_send_request, |
| 849 | ioctl_allocate, |
| 850 | ioctl_deallocate, |
| 851 | ioctl_send_response, |
| 852 | ioctl_initiate_bus_reset, |
| 853 | ioctl_add_descriptor, |
| 854 | ioctl_remove_descriptor, |
| 855 | ioctl_create_iso_context, |
| 856 | ioctl_queue_iso, |
| 857 | ioctl_start_iso, |
| 858 | ioctl_stop_iso, |
Stefan Richter | a64408b | 2007-09-29 10:41:58 +0200 | [diff] [blame] | 859 | ioctl_get_cycle_timer, |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 860 | }; |
| 861 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 862 | static int |
| 863 | dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg) |
| 864 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 865 | char buffer[256]; |
| 866 | int retval; |
| 867 | |
| 868 | if (_IOC_TYPE(cmd) != '#' || |
| 869 | _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 870 | return -EINVAL; |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 871 | |
| 872 | if (_IOC_DIR(cmd) & _IOC_WRITE) { |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 873 | if (_IOC_SIZE(cmd) > sizeof(buffer) || |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 874 | copy_from_user(buffer, arg, _IOC_SIZE(cmd))) |
| 875 | return -EFAULT; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 876 | } |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 877 | |
| 878 | retval = ioctl_handlers[_IOC_NR(cmd)](client, buffer); |
| 879 | if (retval < 0) |
| 880 | return retval; |
| 881 | |
| 882 | if (_IOC_DIR(cmd) & _IOC_READ) { |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 883 | if (_IOC_SIZE(cmd) > sizeof(buffer) || |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 884 | copy_to_user(arg, buffer, _IOC_SIZE(cmd))) |
| 885 | return -EFAULT; |
| 886 | } |
| 887 | |
| 888 | return 0; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | static long |
| 892 | fw_device_op_ioctl(struct file *file, |
| 893 | unsigned int cmd, unsigned long arg) |
| 894 | { |
| 895 | struct client *client = file->private_data; |
| 896 | |
| 897 | return dispatch_ioctl(client, cmd, (void __user *) arg); |
| 898 | } |
| 899 | |
| 900 | #ifdef CONFIG_COMPAT |
| 901 | static long |
| 902 | fw_device_op_compat_ioctl(struct file *file, |
| 903 | unsigned int cmd, unsigned long arg) |
| 904 | { |
| 905 | struct client *client = file->private_data; |
| 906 | |
| 907 | return dispatch_ioctl(client, cmd, compat_ptr(arg)); |
| 908 | } |
| 909 | #endif |
| 910 | |
| 911 | static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma) |
| 912 | { |
| 913 | struct client *client = file->private_data; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 914 | enum dma_data_direction direction; |
| 915 | unsigned long size; |
| 916 | int page_count, retval; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 917 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 918 | /* FIXME: We could support multiple buffers, but we don't. */ |
| 919 | if (client->buffer.pages != NULL) |
| 920 | return -EBUSY; |
| 921 | |
| 922 | if (!(vma->vm_flags & VM_SHARED)) |
| 923 | return -EINVAL; |
| 924 | |
| 925 | if (vma->vm_start & ~PAGE_MASK) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 926 | return -EINVAL; |
| 927 | |
| 928 | client->vm_start = vma->vm_start; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 929 | size = vma->vm_end - vma->vm_start; |
| 930 | page_count = size >> PAGE_SHIFT; |
| 931 | if (size & ~PAGE_MASK) |
| 932 | return -EINVAL; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 933 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 934 | if (vma->vm_flags & VM_WRITE) |
| 935 | direction = DMA_TO_DEVICE; |
| 936 | else |
| 937 | direction = DMA_FROM_DEVICE; |
| 938 | |
| 939 | retval = fw_iso_buffer_init(&client->buffer, client->device->card, |
| 940 | page_count, direction); |
| 941 | if (retval < 0) |
| 942 | return retval; |
| 943 | |
| 944 | retval = fw_iso_buffer_map(&client->buffer, vma); |
| 945 | if (retval < 0) |
| 946 | fw_iso_buffer_destroy(&client->buffer, client->device->card); |
| 947 | |
| 948 | return retval; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | static int fw_device_op_release(struct inode *inode, struct file *file) |
| 952 | { |
| 953 | struct client *client = file->private_data; |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 954 | struct event *e, *next_e; |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 955 | struct client_resource *r, *next_r; |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 956 | unsigned long flags; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 957 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 958 | if (client->buffer.pages) |
| 959 | fw_iso_buffer_destroy(&client->buffer, client->device->card); |
| 960 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 961 | if (client->iso_context) |
| 962 | fw_iso_context_destroy(client->iso_context); |
| 963 | |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 964 | list_for_each_entry_safe(r, next_r, &client->resource_list, link) |
| 965 | r->release(client, r); |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 966 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 967 | /* |
| 968 | * FIXME: We should wait for the async tasklets to stop |
| 969 | * running before freeing the memory. |
| 970 | */ |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 971 | |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 972 | list_for_each_entry_safe(e, next_e, &client->event_list, link) |
| 973 | kfree(e); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 974 | |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 975 | spin_lock_irqsave(&client->device->card->lock, flags); |
| 976 | list_del(&client->link); |
| 977 | spin_unlock_irqrestore(&client->device->card->lock, flags); |
| 978 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 979 | fw_device_put(client->device); |
| 980 | kfree(client); |
| 981 | |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | static unsigned int fw_device_op_poll(struct file *file, poll_table * pt) |
| 986 | { |
| 987 | struct client *client = file->private_data; |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 988 | unsigned int mask = 0; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 989 | |
| 990 | poll_wait(file, &client->wait, pt); |
| 991 | |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 992 | if (fw_device_is_shutdown(client->device)) |
| 993 | mask |= POLLHUP | POLLERR; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 994 | if (!list_empty(&client->event_list)) |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 995 | mask |= POLLIN | POLLRDNORM; |
| 996 | |
| 997 | return mask; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 998 | } |
| 999 | |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 1000 | const struct file_operations fw_device_ops = { |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1001 | .owner = THIS_MODULE, |
| 1002 | .open = fw_device_op_open, |
| 1003 | .read = fw_device_op_read, |
| 1004 | .unlocked_ioctl = fw_device_op_ioctl, |
| 1005 | .poll = fw_device_op_poll, |
| 1006 | .release = fw_device_op_release, |
| 1007 | .mmap = fw_device_op_mmap, |
| 1008 | |
| 1009 | #ifdef CONFIG_COMPAT |
Stefan Richter | 5af4e5e | 2007-01-21 20:45:32 +0100 | [diff] [blame] | 1010 | .compat_ioctl = fw_device_op_compat_ioctl, |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1011 | #endif |
| 1012 | }; |