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