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