Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 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> |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 31 | #include <linux/idr.h> |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 32 | #include <linux/compat.h> |
| 33 | #include <asm/uaccess.h> |
| 34 | #include "fw-transaction.h" |
| 35 | #include "fw-topology.h" |
| 36 | #include "fw-device.h" |
| 37 | #include "fw-device-cdev.h" |
| 38 | |
| 39 | /* |
| 40 | * todo |
| 41 | * |
| 42 | * - bus resets sends a new packet with new generation and node id |
| 43 | * |
| 44 | */ |
| 45 | |
| 46 | /* dequeue_event() just kfree()'s the event, so the event has to be |
| 47 | * the first field in the struct. */ |
| 48 | |
| 49 | struct event { |
| 50 | struct { void *data; size_t size; } v[2]; |
| 51 | struct list_head link; |
| 52 | }; |
| 53 | |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 54 | struct bus_reset { |
| 55 | struct event event; |
| 56 | struct fw_cdev_event_bus_reset reset; |
| 57 | }; |
| 58 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 59 | struct response { |
| 60 | struct event event; |
| 61 | struct fw_transaction transaction; |
| 62 | struct client *client; |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 63 | struct list_head link; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 64 | struct fw_cdev_event_response response; |
| 65 | }; |
| 66 | |
| 67 | struct iso_interrupt { |
| 68 | struct event event; |
| 69 | struct fw_cdev_event_iso_interrupt interrupt; |
| 70 | }; |
| 71 | |
| 72 | struct client { |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 73 | u32 version; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 74 | struct fw_device *device; |
| 75 | spinlock_t lock; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame^] | 76 | u32 resource_handle; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 77 | struct list_head handler_list; |
| 78 | struct list_head request_list; |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 79 | struct list_head transaction_list; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame^] | 80 | struct list_head descriptor_list; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 81 | u32 request_serial; |
| 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 | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 87 | struct fw_iso_buffer buffer; |
| 88 | unsigned long vm_start; |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 89 | |
| 90 | struct list_head link; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static inline void __user * |
| 94 | u64_to_uptr(__u64 value) |
| 95 | { |
| 96 | return (void __user *)(unsigned long)value; |
| 97 | } |
| 98 | |
| 99 | static inline __u64 |
| 100 | uptr_to_u64(void __user *ptr) |
| 101 | { |
| 102 | return (__u64)(unsigned long)ptr; |
| 103 | } |
| 104 | |
| 105 | static int fw_device_op_open(struct inode *inode, struct file *file) |
| 106 | { |
| 107 | struct fw_device *device; |
| 108 | struct client *client; |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 109 | unsigned long flags; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 110 | |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 111 | device = fw_device_from_devt(inode->i_rdev); |
| 112 | if (device == NULL) |
| 113 | return -ENODEV; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 114 | |
| 115 | client = kzalloc(sizeof *client, GFP_KERNEL); |
| 116 | if (client == NULL) |
| 117 | return -ENOMEM; |
| 118 | |
| 119 | client->device = fw_device_get(device); |
| 120 | INIT_LIST_HEAD(&client->event_list); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 121 | INIT_LIST_HEAD(&client->handler_list); |
| 122 | INIT_LIST_HEAD(&client->request_list); |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 123 | INIT_LIST_HEAD(&client->transaction_list); |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame^] | 124 | INIT_LIST_HEAD(&client->descriptor_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); |
| 148 | |
| 149 | list_add_tail(&event->link, &client->event_list); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 150 | wake_up_interruptible(&client->wait); |
| 151 | |
| 152 | spin_unlock_irqrestore(&client->lock, flags); |
| 153 | } |
| 154 | |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 155 | static int |
| 156 | dequeue_event(struct client *client, char __user *buffer, size_t count) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 157 | { |
| 158 | unsigned long flags; |
| 159 | struct event *event; |
| 160 | size_t size, total; |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 161 | int i, retval; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 162 | |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 163 | retval = wait_event_interruptible(client->wait, |
| 164 | !list_empty(&client->event_list) || |
| 165 | fw_device_is_shutdown(client->device)); |
| 166 | if (retval < 0) |
| 167 | return retval; |
| 168 | |
| 169 | if (list_empty(&client->event_list) && |
| 170 | fw_device_is_shutdown(client->device)) |
| 171 | return -ENODEV; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 172 | |
| 173 | spin_lock_irqsave(&client->lock, flags); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 174 | event = container_of(client->event_list.next, struct event, link); |
| 175 | list_del(&event->link); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 176 | spin_unlock_irqrestore(&client->lock, flags); |
| 177 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 178 | total = 0; |
| 179 | for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) { |
| 180 | size = min(event->v[i].size, count - total); |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 181 | if (copy_to_user(buffer + total, event->v[i].data, size)) { |
| 182 | retval = -EFAULT; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 183 | goto out; |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 184 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 185 | total += size; |
| 186 | } |
| 187 | retval = total; |
| 188 | |
| 189 | out: |
| 190 | kfree(event); |
| 191 | |
| 192 | return retval; |
| 193 | } |
| 194 | |
| 195 | static ssize_t |
| 196 | fw_device_op_read(struct file *file, |
| 197 | char __user *buffer, size_t count, loff_t *offset) |
| 198 | { |
| 199 | struct client *client = file->private_data; |
| 200 | |
| 201 | return dequeue_event(client, buffer, count); |
| 202 | } |
| 203 | |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 204 | static void |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 205 | fill_bus_reset_event(struct fw_cdev_event_bus_reset *event, |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 206 | struct client *client) |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 207 | { |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 208 | struct fw_card *card = client->device->card; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 209 | |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 210 | event->closure = client->bus_reset_closure; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 211 | event->type = FW_CDEV_EVENT_BUS_RESET; |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 212 | event->node_id = client->device->node_id; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 213 | event->local_node_id = card->local_node->node_id; |
| 214 | event->bm_node_id = 0; /* FIXME: We don't track the BM. */ |
| 215 | event->irm_node_id = card->irm_node->node_id; |
| 216 | event->root_node_id = card->root_node->node_id; |
| 217 | event->generation = card->generation; |
| 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 | |
| 241 | bus_reset = kzalloc(sizeof *bus_reset, GFP_ATOMIC); |
| 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, |
| 250 | &bus_reset->reset, sizeof bus_reset->reset, NULL, 0); |
| 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 | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 268 | static int ioctl_get_info(struct client *client, void __user *arg) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 269 | { |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 270 | struct fw_cdev_get_info get_info; |
| 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 | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 273 | if (copy_from_user(&get_info, arg, sizeof get_info)) |
| 274 | return -EFAULT; |
| 275 | |
| 276 | client->version = get_info.version; |
| 277 | get_info.version = FW_CDEV_VERSION; |
| 278 | |
| 279 | if (get_info.rom != 0) { |
| 280 | void __user *uptr = u64_to_uptr(get_info.rom); |
Stefan Richter | d84702a | 2007-03-20 19:42:15 +0100 | [diff] [blame] | 281 | size_t want = get_info.rom_length; |
| 282 | size_t have = client->device->config_rom_length * 4; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 283 | |
Stefan Richter | d84702a | 2007-03-20 19:42:15 +0100 | [diff] [blame] | 284 | if (copy_to_user(uptr, client->device->config_rom, |
| 285 | min(want, have))) |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 286 | return -EFAULT; |
| 287 | } |
| 288 | get_info.rom_length = client->device->config_rom_length * 4; |
| 289 | |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 290 | client->bus_reset_closure = get_info.bus_reset_closure; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 291 | if (get_info.bus_reset != 0) { |
| 292 | void __user *uptr = u64_to_uptr(get_info.bus_reset); |
| 293 | |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 294 | fill_bus_reset_event(&bus_reset, client); |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 295 | if (copy_to_user(uptr, &bus_reset, sizeof bus_reset)) |
| 296 | return -EFAULT; |
| 297 | } |
| 298 | |
Kristian Høgsberg | e753350 | 2007-03-07 12:12:52 -0500 | [diff] [blame] | 299 | get_info.card = client->device->card->index; |
| 300 | |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 301 | if (copy_to_user(arg, &get_info, sizeof get_info)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 302 | return -EFAULT; |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static void |
| 308 | complete_transaction(struct fw_card *card, int rcode, |
| 309 | void *payload, size_t length, void *data) |
| 310 | { |
| 311 | struct response *response = data; |
| 312 | struct client *client = response->client; |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 313 | unsigned long flags; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 314 | |
| 315 | if (length < response->response.length) |
| 316 | response->response.length = length; |
| 317 | if (rcode == RCODE_COMPLETE) |
| 318 | memcpy(response->response.data, payload, |
| 319 | response->response.length); |
| 320 | |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 321 | spin_lock_irqsave(&client->lock, flags); |
| 322 | list_del(&response->link); |
| 323 | spin_unlock_irqrestore(&client->lock, flags); |
| 324 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 325 | response->response.type = FW_CDEV_EVENT_RESPONSE; |
| 326 | response->response.rcode = rcode; |
| 327 | queue_event(client, &response->event, |
| 328 | &response->response, sizeof response->response, |
| 329 | response->response.data, response->response.length); |
| 330 | } |
| 331 | |
| 332 | static ssize_t ioctl_send_request(struct client *client, void __user *arg) |
| 333 | { |
| 334 | struct fw_device *device = client->device; |
| 335 | struct fw_cdev_send_request request; |
| 336 | struct response *response; |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 337 | unsigned long flags; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 338 | |
| 339 | if (copy_from_user(&request, arg, sizeof request)) |
| 340 | return -EFAULT; |
| 341 | |
| 342 | /* What is the biggest size we'll accept, really? */ |
| 343 | if (request.length > 4096) |
| 344 | return -EINVAL; |
| 345 | |
| 346 | response = kmalloc(sizeof *response + request.length, GFP_KERNEL); |
| 347 | if (response == NULL) |
| 348 | return -ENOMEM; |
| 349 | |
| 350 | response->client = client; |
| 351 | response->response.length = request.length; |
| 352 | response->response.closure = request.closure; |
| 353 | |
| 354 | if (request.data && |
| 355 | copy_from_user(response->response.data, |
| 356 | u64_to_uptr(request.data), request.length)) { |
| 357 | kfree(response); |
| 358 | return -EFAULT; |
| 359 | } |
| 360 | |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 361 | spin_lock_irqsave(&client->lock, flags); |
| 362 | list_add_tail(&response->link, &client->transaction_list); |
| 363 | spin_unlock_irqrestore(&client->lock, flags); |
| 364 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 365 | fw_send_request(device->card, &response->transaction, |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 366 | request.tcode & 0x1f, |
Stefan Richter | 907293d | 2007-01-23 21:11:43 +0100 | [diff] [blame] | 367 | device->node->node_id, |
Kristian Høgsberg | 97e3527 | 2007-03-07 12:12:53 -0500 | [diff] [blame] | 368 | request.generation, |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 369 | device->node->max_speed, |
| 370 | request.offset, |
| 371 | response->response.data, request.length, |
| 372 | complete_transaction, response); |
| 373 | |
| 374 | if (request.data) |
| 375 | return sizeof request + request.length; |
| 376 | else |
| 377 | return sizeof request; |
| 378 | } |
| 379 | |
| 380 | struct address_handler { |
| 381 | struct fw_address_handler handler; |
| 382 | __u64 closure; |
| 383 | struct client *client; |
| 384 | struct list_head link; |
| 385 | }; |
| 386 | |
| 387 | struct request { |
| 388 | struct fw_request *request; |
| 389 | void *data; |
| 390 | size_t length; |
| 391 | u32 serial; |
| 392 | struct list_head link; |
| 393 | }; |
| 394 | |
| 395 | struct request_event { |
| 396 | struct event event; |
| 397 | struct fw_cdev_event_request request; |
| 398 | }; |
| 399 | |
| 400 | static void |
| 401 | handle_request(struct fw_card *card, struct fw_request *r, |
| 402 | int tcode, int destination, int source, |
| 403 | int generation, int speed, |
| 404 | unsigned long long offset, |
| 405 | void *payload, size_t length, void *callback_data) |
| 406 | { |
| 407 | struct address_handler *handler = callback_data; |
| 408 | struct request *request; |
| 409 | struct request_event *e; |
| 410 | unsigned long flags; |
| 411 | struct client *client = handler->client; |
| 412 | |
| 413 | request = kmalloc(sizeof *request, GFP_ATOMIC); |
| 414 | e = kmalloc(sizeof *e, GFP_ATOMIC); |
| 415 | if (request == NULL || e == NULL) { |
| 416 | kfree(request); |
| 417 | kfree(e); |
| 418 | fw_send_response(card, r, RCODE_CONFLICT_ERROR); |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | request->request = r; |
| 423 | request->data = payload; |
| 424 | request->length = length; |
| 425 | |
| 426 | spin_lock_irqsave(&client->lock, flags); |
| 427 | request->serial = client->request_serial++; |
| 428 | list_add_tail(&request->link, &client->request_list); |
| 429 | spin_unlock_irqrestore(&client->lock, flags); |
| 430 | |
| 431 | e->request.type = FW_CDEV_EVENT_REQUEST; |
| 432 | e->request.tcode = tcode; |
| 433 | e->request.offset = offset; |
| 434 | e->request.length = length; |
| 435 | e->request.serial = request->serial; |
| 436 | e->request.closure = handler->closure; |
| 437 | |
| 438 | queue_event(client, &e->event, |
| 439 | &e->request, sizeof e->request, payload, length); |
| 440 | } |
| 441 | |
| 442 | static int ioctl_allocate(struct client *client, void __user *arg) |
| 443 | { |
| 444 | struct fw_cdev_allocate request; |
| 445 | struct address_handler *handler; |
| 446 | unsigned long flags; |
| 447 | struct fw_address_region region; |
| 448 | |
| 449 | if (copy_from_user(&request, arg, sizeof request)) |
| 450 | return -EFAULT; |
| 451 | |
| 452 | handler = kmalloc(sizeof *handler, GFP_KERNEL); |
| 453 | if (handler == NULL) |
| 454 | return -ENOMEM; |
| 455 | |
| 456 | region.start = request.offset; |
| 457 | region.end = request.offset + request.length; |
| 458 | handler->handler.length = request.length; |
| 459 | handler->handler.address_callback = handle_request; |
| 460 | handler->handler.callback_data = handler; |
| 461 | handler->closure = request.closure; |
| 462 | handler->client = client; |
| 463 | |
| 464 | if (fw_core_add_address_handler(&handler->handler, ®ion) < 0) { |
| 465 | kfree(handler); |
| 466 | return -EBUSY; |
| 467 | } |
| 468 | |
| 469 | spin_lock_irqsave(&client->lock, flags); |
| 470 | list_add_tail(&handler->link, &client->handler_list); |
| 471 | spin_unlock_irqrestore(&client->lock, flags); |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
Kristian Høgsberg | 9472316 | 2007-03-14 17:34:55 -0400 | [diff] [blame] | 476 | static int ioctl_deallocate(struct client *client, void __user *arg) |
| 477 | { |
| 478 | struct fw_cdev_deallocate request; |
| 479 | struct address_handler *handler; |
| 480 | unsigned long flags; |
| 481 | |
| 482 | if (copy_from_user(&request, arg, sizeof request)) |
| 483 | return -EFAULT; |
| 484 | |
| 485 | spin_lock_irqsave(&client->lock, flags); |
| 486 | list_for_each_entry(handler, &client->handler_list, link) { |
| 487 | if (handler->handler.offset == request.offset) { |
| 488 | list_del(&handler->link); |
| 489 | break; |
| 490 | } |
| 491 | } |
| 492 | spin_unlock_irqrestore(&client->lock, flags); |
| 493 | |
| 494 | if (&handler->link == &client->handler_list) |
| 495 | return -EINVAL; |
| 496 | |
| 497 | fw_core_remove_address_handler(&handler->handler); |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 502 | static int ioctl_send_response(struct client *client, void __user *arg) |
| 503 | { |
| 504 | struct fw_cdev_send_response request; |
| 505 | struct request *r; |
| 506 | unsigned long flags; |
| 507 | |
| 508 | if (copy_from_user(&request, arg, sizeof request)) |
| 509 | return -EFAULT; |
| 510 | |
| 511 | spin_lock_irqsave(&client->lock, flags); |
| 512 | list_for_each_entry(r, &client->request_list, link) { |
| 513 | if (r->serial == request.serial) { |
| 514 | list_del(&r->link); |
| 515 | break; |
| 516 | } |
| 517 | } |
| 518 | spin_unlock_irqrestore(&client->lock, flags); |
| 519 | |
| 520 | if (&r->link == &client->request_list) |
| 521 | return -EINVAL; |
| 522 | |
| 523 | if (request.length < r->length) |
| 524 | r->length = request.length; |
| 525 | if (copy_from_user(r->data, u64_to_uptr(request.data), r->length)) |
| 526 | return -EFAULT; |
| 527 | |
| 528 | fw_send_response(client->device->card, r->request, request.rcode); |
| 529 | |
| 530 | kfree(r); |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
Kristian Høgsberg | 5371842 | 2007-03-07 12:12:42 -0500 | [diff] [blame] | 535 | static int ioctl_initiate_bus_reset(struct client *client, void __user *arg) |
| 536 | { |
| 537 | struct fw_cdev_initiate_bus_reset request; |
| 538 | int short_reset; |
| 539 | |
| 540 | if (copy_from_user(&request, arg, sizeof request)) |
| 541 | return -EFAULT; |
| 542 | |
| 543 | short_reset = (request.type == FW_CDEV_SHORT_RESET); |
| 544 | |
| 545 | return fw_core_initiate_bus_reset(client->device->card, short_reset); |
| 546 | } |
| 547 | |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame^] | 548 | struct descriptor { |
| 549 | struct fw_descriptor d; |
| 550 | struct list_head link; |
| 551 | u32 handle; |
| 552 | u32 data[0]; |
| 553 | }; |
| 554 | |
| 555 | static int ioctl_add_descriptor(struct client *client, void __user *arg) |
| 556 | { |
| 557 | struct fw_cdev_add_descriptor request; |
| 558 | struct descriptor *descriptor; |
| 559 | unsigned long flags; |
| 560 | int retval; |
| 561 | |
| 562 | if (copy_from_user(&request, arg, sizeof request)) |
| 563 | return -EFAULT; |
| 564 | |
| 565 | if (request.length > 256) |
| 566 | return -EINVAL; |
| 567 | |
| 568 | descriptor = |
| 569 | kmalloc(sizeof *descriptor + request.length * 4, GFP_KERNEL); |
| 570 | if (descriptor == NULL) |
| 571 | return -ENOMEM; |
| 572 | |
| 573 | if (copy_from_user(descriptor->data, |
| 574 | u64_to_uptr(request.data), request.length * 4)) { |
| 575 | kfree(descriptor); |
| 576 | return -EFAULT; |
| 577 | } |
| 578 | |
| 579 | descriptor->d.length = request.length; |
| 580 | descriptor->d.immediate = request.immediate; |
| 581 | descriptor->d.key = request.key; |
| 582 | descriptor->d.data = descriptor->data; |
| 583 | |
| 584 | retval = fw_core_add_descriptor(&descriptor->d); |
| 585 | if (retval < 0) { |
| 586 | kfree(descriptor); |
| 587 | return retval; |
| 588 | } |
| 589 | |
| 590 | spin_lock_irqsave(&client->lock, flags); |
| 591 | list_add_tail(&descriptor->link, &client->descriptor_list); |
| 592 | descriptor->handle = client->resource_handle++; |
| 593 | spin_unlock_irqrestore(&client->lock, flags); |
| 594 | |
| 595 | request.handle = descriptor->handle; |
| 596 | if (copy_to_user(arg, &request, sizeof request)) |
| 597 | return -EFAULT; |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | static int ioctl_remove_descriptor(struct client *client, void __user *arg) |
| 603 | { |
| 604 | struct fw_cdev_remove_descriptor request; |
| 605 | struct descriptor *d; |
| 606 | unsigned long flags; |
| 607 | |
| 608 | if (copy_from_user(&request, arg, sizeof request)) |
| 609 | return -EFAULT; |
| 610 | |
| 611 | spin_lock_irqsave(&client->lock, flags); |
| 612 | list_for_each_entry(d, &client->descriptor_list, link) { |
| 613 | if (d->handle == request.handle) { |
| 614 | list_del(&d->link); |
| 615 | break; |
| 616 | } |
| 617 | } |
| 618 | spin_unlock_irqrestore(&client->lock, flags); |
| 619 | |
| 620 | if (&d->link == &client->descriptor_list) |
| 621 | return -EINVAL; |
| 622 | |
| 623 | fw_core_remove_descriptor(&d->d); |
| 624 | kfree(d); |
| 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 629 | static void |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 630 | iso_callback(struct fw_iso_context *context, u32 cycle, |
| 631 | size_t header_length, void *header, void *data) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 632 | { |
| 633 | struct client *client = data; |
| 634 | struct iso_interrupt *interrupt; |
| 635 | |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 636 | interrupt = kzalloc(sizeof *interrupt + header_length, GFP_ATOMIC); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 637 | if (interrupt == NULL) |
| 638 | return; |
| 639 | |
| 640 | interrupt->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT; |
| 641 | interrupt->interrupt.closure = 0; |
| 642 | interrupt->interrupt.cycle = cycle; |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 643 | interrupt->interrupt.header_length = header_length; |
| 644 | memcpy(interrupt->interrupt.header, header, header_length); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 645 | queue_event(client, &interrupt->event, |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 646 | &interrupt->interrupt, |
| 647 | sizeof interrupt->interrupt + header_length, NULL, 0); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static int ioctl_create_iso_context(struct client *client, void __user *arg) |
| 651 | { |
| 652 | struct fw_cdev_create_iso_context request; |
| 653 | |
| 654 | if (copy_from_user(&request, arg, sizeof request)) |
| 655 | return -EFAULT; |
| 656 | |
Kristian Høgsberg | 21efb3c | 2007-02-16 17:34:50 -0500 | [diff] [blame] | 657 | if (request.channel > 63) |
| 658 | return -EINVAL; |
| 659 | |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 660 | switch (request.type) { |
| 661 | case FW_ISO_CONTEXT_RECEIVE: |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 662 | if (request.header_size < 4 || (request.header_size & 3)) |
| 663 | return -EINVAL; |
| 664 | |
| 665 | break; |
| 666 | |
| 667 | case FW_ISO_CONTEXT_TRANSMIT: |
| 668 | if (request.speed > SCODE_3200) |
| 669 | return -EINVAL; |
| 670 | |
| 671 | break; |
| 672 | |
| 673 | default: |
Kristian Høgsberg | 21efb3c | 2007-02-16 17:34:50 -0500 | [diff] [blame] | 674 | return -EINVAL; |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 675 | } |
| 676 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 677 | client->iso_context = fw_iso_context_create(client->device->card, |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 678 | request.type, |
Kristian Høgsberg | 21efb3c | 2007-02-16 17:34:50 -0500 | [diff] [blame] | 679 | request.channel, |
| 680 | request.speed, |
Kristian Høgsberg | 8fbdbb3 | 2007-03-07 12:12:51 -0500 | [diff] [blame] | 681 | request.header_size, |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 682 | iso_callback, client); |
| 683 | if (IS_ERR(client->iso_context)) |
| 684 | return PTR_ERR(client->iso_context); |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | static int ioctl_queue_iso(struct client *client, void __user *arg) |
| 690 | { |
| 691 | struct fw_cdev_queue_iso request; |
| 692 | struct fw_cdev_iso_packet __user *p, *end, *next; |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 693 | struct fw_iso_context *ctx = client->iso_context; |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 694 | unsigned long payload, payload_end, header_length; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 695 | int count; |
| 696 | struct { |
| 697 | struct fw_iso_packet packet; |
| 698 | u8 header[256]; |
| 699 | } u; |
| 700 | |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 701 | if (ctx == NULL) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 702 | return -EINVAL; |
| 703 | if (copy_from_user(&request, arg, sizeof request)) |
| 704 | return -EFAULT; |
| 705 | |
| 706 | /* If the user passes a non-NULL data pointer, has mmap()'ed |
| 707 | * the iso buffer, and the pointer points inside the buffer, |
| 708 | * we setup the payload pointers accordingly. Otherwise we |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 709 | * set them both to 0, which will still let packets with |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 710 | * payload_length == 0 through. In other words, if no packets |
| 711 | * use the indirect payload, the iso buffer need not be mapped |
| 712 | * and the request.data pointer is ignored.*/ |
| 713 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 714 | payload = (unsigned long)request.data - client->vm_start; |
| 715 | payload_end = payload + (client->buffer.page_count << PAGE_SHIFT); |
| 716 | if (request.data == 0 || client->buffer.pages == NULL || |
| 717 | payload >= payload_end) { |
| 718 | payload = 0; |
| 719 | payload_end = 0; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | if (!access_ok(VERIFY_READ, request.packets, request.size)) |
| 723 | return -EFAULT; |
| 724 | |
| 725 | p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request.packets); |
| 726 | end = (void __user *)p + request.size; |
| 727 | count = 0; |
| 728 | while (p < end) { |
| 729 | if (__copy_from_user(&u.packet, p, sizeof *p)) |
| 730 | return -EFAULT; |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 731 | |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 732 | if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) { |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 733 | header_length = u.packet.header_length; |
| 734 | } else { |
| 735 | /* We require that header_length is a multiple of |
| 736 | * the fixed header size, ctx->header_size */ |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 737 | if (ctx->header_size == 0) { |
| 738 | if (u.packet.header_length > 0) |
| 739 | return -EINVAL; |
| 740 | } else if (u.packet.header_length % ctx->header_size != 0) { |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 741 | return -EINVAL; |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 742 | } |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 743 | header_length = 0; |
| 744 | } |
| 745 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 746 | next = (struct fw_cdev_iso_packet __user *) |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 747 | &p->header[header_length / 4]; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 748 | if (next > end) |
| 749 | return -EINVAL; |
| 750 | if (__copy_from_user |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 751 | (u.packet.header, p->header, header_length)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 752 | return -EFAULT; |
Kristian Høgsberg | 98b6cbe | 2007-02-16 17:34:51 -0500 | [diff] [blame] | 753 | if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT && |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 754 | u.packet.header_length + u.packet.payload_length > 0) |
| 755 | return -EINVAL; |
| 756 | if (payload + u.packet.payload_length > payload_end) |
| 757 | return -EINVAL; |
| 758 | |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 759 | if (fw_iso_context_queue(ctx, &u.packet, |
| 760 | &client->buffer, payload)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 761 | break; |
| 762 | |
| 763 | p = next; |
| 764 | payload += u.packet.payload_length; |
| 765 | count++; |
| 766 | } |
| 767 | |
| 768 | request.size -= uptr_to_u64(p) - request.packets; |
| 769 | request.packets = uptr_to_u64(p); |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 770 | request.data = client->vm_start + payload; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 771 | |
| 772 | if (copy_to_user(arg, &request, sizeof request)) |
| 773 | return -EFAULT; |
| 774 | |
| 775 | return count; |
| 776 | } |
| 777 | |
Kristian Høgsberg | 69cdb72 | 2007-02-16 17:34:41 -0500 | [diff] [blame] | 778 | static int ioctl_start_iso(struct client *client, void __user *arg) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 779 | { |
Kristian Høgsberg | 69cdb72 | 2007-02-16 17:34:41 -0500 | [diff] [blame] | 780 | struct fw_cdev_start_iso request; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 781 | |
| 782 | if (copy_from_user(&request, arg, sizeof request)) |
| 783 | return -EFAULT; |
| 784 | |
Kristian Høgsberg | eb0306e | 2007-03-14 17:34:54 -0400 | [diff] [blame] | 785 | if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) { |
| 786 | if (request.tags == 0 || request.tags > 15) |
| 787 | return -EINVAL; |
| 788 | |
| 789 | if (request.sync > 15) |
| 790 | return -EINVAL; |
| 791 | } |
| 792 | |
| 793 | return fw_iso_context_start(client->iso_context, |
| 794 | request.cycle, request.sync, request.tags); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 795 | } |
| 796 | |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 797 | static int ioctl_stop_iso(struct client *client, void __user *arg) |
| 798 | { |
| 799 | return fw_iso_context_stop(client->iso_context); |
| 800 | } |
| 801 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 802 | static int |
| 803 | dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg) |
| 804 | { |
| 805 | switch (cmd) { |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 806 | case FW_CDEV_IOC_GET_INFO: |
| 807 | return ioctl_get_info(client, arg); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 808 | case FW_CDEV_IOC_SEND_REQUEST: |
| 809 | return ioctl_send_request(client, arg); |
| 810 | case FW_CDEV_IOC_ALLOCATE: |
| 811 | return ioctl_allocate(client, arg); |
Kristian Høgsberg | 9472316 | 2007-03-14 17:34:55 -0400 | [diff] [blame] | 812 | case FW_CDEV_IOC_DEALLOCATE: |
| 813 | return ioctl_deallocate(client, arg); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 814 | case FW_CDEV_IOC_SEND_RESPONSE: |
| 815 | return ioctl_send_response(client, arg); |
Kristian Høgsberg | 5371842 | 2007-03-07 12:12:42 -0500 | [diff] [blame] | 816 | case FW_CDEV_IOC_INITIATE_BUS_RESET: |
| 817 | return ioctl_initiate_bus_reset(client, arg); |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame^] | 818 | case FW_CDEV_IOC_ADD_DESCRIPTOR: |
| 819 | return ioctl_add_descriptor(client, arg); |
| 820 | case FW_CDEV_IOC_REMOVE_DESCRIPTOR: |
| 821 | return ioctl_remove_descriptor(client, arg); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 822 | case FW_CDEV_IOC_CREATE_ISO_CONTEXT: |
| 823 | return ioctl_create_iso_context(client, arg); |
| 824 | case FW_CDEV_IOC_QUEUE_ISO: |
| 825 | return ioctl_queue_iso(client, arg); |
Kristian Høgsberg | 69cdb72 | 2007-02-16 17:34:41 -0500 | [diff] [blame] | 826 | case FW_CDEV_IOC_START_ISO: |
| 827 | return ioctl_start_iso(client, arg); |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 828 | case FW_CDEV_IOC_STOP_ISO: |
| 829 | return ioctl_stop_iso(client, arg); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 830 | default: |
| 831 | return -EINVAL; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | static long |
| 836 | fw_device_op_ioctl(struct file *file, |
| 837 | unsigned int cmd, unsigned long arg) |
| 838 | { |
| 839 | struct client *client = file->private_data; |
| 840 | |
| 841 | return dispatch_ioctl(client, cmd, (void __user *) arg); |
| 842 | } |
| 843 | |
| 844 | #ifdef CONFIG_COMPAT |
| 845 | static long |
| 846 | fw_device_op_compat_ioctl(struct file *file, |
| 847 | unsigned int cmd, unsigned long arg) |
| 848 | { |
| 849 | struct client *client = file->private_data; |
| 850 | |
| 851 | return dispatch_ioctl(client, cmd, compat_ptr(arg)); |
| 852 | } |
| 853 | #endif |
| 854 | |
| 855 | static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma) |
| 856 | { |
| 857 | struct client *client = file->private_data; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 858 | enum dma_data_direction direction; |
| 859 | unsigned long size; |
| 860 | int page_count, retval; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 861 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 862 | /* FIXME: We could support multiple buffers, but we don't. */ |
| 863 | if (client->buffer.pages != NULL) |
| 864 | return -EBUSY; |
| 865 | |
| 866 | if (!(vma->vm_flags & VM_SHARED)) |
| 867 | return -EINVAL; |
| 868 | |
| 869 | if (vma->vm_start & ~PAGE_MASK) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 870 | return -EINVAL; |
| 871 | |
| 872 | client->vm_start = vma->vm_start; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 873 | size = vma->vm_end - vma->vm_start; |
| 874 | page_count = size >> PAGE_SHIFT; |
| 875 | if (size & ~PAGE_MASK) |
| 876 | return -EINVAL; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 877 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 878 | if (vma->vm_flags & VM_WRITE) |
| 879 | direction = DMA_TO_DEVICE; |
| 880 | else |
| 881 | direction = DMA_FROM_DEVICE; |
| 882 | |
| 883 | retval = fw_iso_buffer_init(&client->buffer, client->device->card, |
| 884 | page_count, direction); |
| 885 | if (retval < 0) |
| 886 | return retval; |
| 887 | |
| 888 | retval = fw_iso_buffer_map(&client->buffer, vma); |
| 889 | if (retval < 0) |
| 890 | fw_iso_buffer_destroy(&client->buffer, client->device->card); |
| 891 | |
| 892 | return retval; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | static int fw_device_op_release(struct inode *inode, struct file *file) |
| 896 | { |
| 897 | struct client *client = file->private_data; |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 898 | struct address_handler *h, *next_h; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 899 | struct request *r, *next_r; |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 900 | struct event *e, *next_e; |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 901 | struct response *t, *next_t; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame^] | 902 | struct descriptor *d, *next_d; |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 903 | unsigned long flags; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 904 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 905 | if (client->buffer.pages) |
| 906 | fw_iso_buffer_destroy(&client->buffer, client->device->card); |
| 907 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 908 | if (client->iso_context) |
| 909 | fw_iso_context_destroy(client->iso_context); |
| 910 | |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 911 | list_for_each_entry_safe(h, next_h, &client->handler_list, link) { |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 912 | fw_core_remove_address_handler(&h->handler); |
| 913 | kfree(h); |
| 914 | } |
| 915 | |
| 916 | list_for_each_entry_safe(r, next_r, &client->request_list, link) { |
| 917 | fw_send_response(client->device->card, r->request, |
| 918 | RCODE_CONFLICT_ERROR); |
| 919 | kfree(r); |
| 920 | } |
| 921 | |
Kristian Høgsberg | 7e35f7f | 2007-03-14 17:34:57 -0400 | [diff] [blame] | 922 | list_for_each_entry_safe(t, next_t, &client->transaction_list, link) { |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 923 | fw_cancel_transaction(client->device->card, &t->transaction); |
Kristian Høgsberg | 7e35f7f | 2007-03-14 17:34:57 -0400 | [diff] [blame] | 924 | kfree(t); |
| 925 | } |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 926 | |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame^] | 927 | list_for_each_entry_safe(d, next_d, &client->descriptor_list, link) { |
| 928 | fw_core_remove_descriptor(&d->d); |
| 929 | kfree(d); |
| 930 | } |
| 931 | |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 932 | /* FIXME: We should wait for the async tasklets to stop |
| 933 | * running before freeing the memory. */ |
| 934 | |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 935 | list_for_each_entry_safe(e, next_e, &client->event_list, link) |
| 936 | kfree(e); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 937 | |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 938 | spin_lock_irqsave(&client->device->card->lock, flags); |
| 939 | list_del(&client->link); |
| 940 | spin_unlock_irqrestore(&client->device->card->lock, flags); |
| 941 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 942 | fw_device_put(client->device); |
| 943 | kfree(client); |
| 944 | |
| 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | static unsigned int fw_device_op_poll(struct file *file, poll_table * pt) |
| 949 | { |
| 950 | struct client *client = file->private_data; |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 951 | unsigned int mask = 0; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 952 | |
| 953 | poll_wait(file, &client->wait, pt); |
| 954 | |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 955 | if (fw_device_is_shutdown(client->device)) |
| 956 | mask |= POLLHUP | POLLERR; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 957 | if (!list_empty(&client->event_list)) |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 958 | mask |= POLLIN | POLLRDNORM; |
| 959 | |
| 960 | return mask; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 961 | } |
| 962 | |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 963 | const struct file_operations fw_device_ops = { |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 964 | .owner = THIS_MODULE, |
| 965 | .open = fw_device_op_open, |
| 966 | .read = fw_device_op_read, |
| 967 | .unlocked_ioctl = fw_device_op_ioctl, |
| 968 | .poll = fw_device_op_poll, |
| 969 | .release = fw_device_op_release, |
| 970 | .mmap = fw_device_op_mmap, |
| 971 | |
| 972 | #ifdef CONFIG_COMPAT |
Stefan Richter | 5af4e5e | 2007-01-21 20:45:32 +0100 | [diff] [blame] | 973 | .compat_ioctl = fw_device_op_compat_ioctl, |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 974 | #endif |
| 975 | }; |