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