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