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