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 | |
Stefan Richter | be5bbd6 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 21 | #include <linux/compat.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/device.h> |
| 24 | #include <linux/errno.h> |
Stefan Richter | 77c9a5d | 2009-06-05 16:26:18 +0200 | [diff] [blame] | 25 | #include <linux/firewire.h> |
Stefan Richter | be5bbd6 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 26 | #include <linux/firewire-cdev.h> |
| 27 | #include <linux/idr.h> |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 28 | #include <linux/jiffies.h> |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 29 | #include <linux/kernel.h> |
Stefan Richter | fb44303 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 30 | #include <linux/kref.h> |
Stefan Richter | be5bbd6 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 31 | #include <linux/mm.h> |
| 32 | #include <linux/module.h> |
Stefan Richter | d67cfb9 | 2008-10-05 10:37:11 +0200 | [diff] [blame] | 33 | #include <linux/mutex.h> |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 34 | #include <linux/poll.h> |
Stefan Richter | a64408b | 2007-09-29 10:41:58 +0200 | [diff] [blame] | 35 | #include <linux/preempt.h> |
Alexey Dobriyan | a99bbaf | 2009-10-04 16:11:37 +0400 | [diff] [blame] | 36 | #include <linux/sched.h> |
Jay Fenlason | cf417e54 | 2008-10-03 11:19:09 -0400 | [diff] [blame] | 37 | #include <linux/spinlock.h> |
Stefan Richter | 281e203 | 2010-01-24 16:45:03 +0100 | [diff] [blame] | 38 | #include <linux/string.h> |
Stefan Richter | be5bbd6 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 39 | #include <linux/time.h> |
Stefan Richter | e034d24 | 2009-06-06 18:36:24 +0200 | [diff] [blame] | 40 | #include <linux/uaccess.h> |
Stefan Richter | be5bbd6 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 41 | #include <linux/vmalloc.h> |
| 42 | #include <linux/wait.h> |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 43 | #include <linux/workqueue.h> |
Stefan Richter | be5bbd6 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 44 | |
Stefan Richter | a64408b | 2007-09-29 10:41:58 +0200 | [diff] [blame] | 45 | #include <asm/system.h> |
Stefan Richter | be5bbd6 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 46 | |
Stefan Richter | 77c9a5d | 2009-06-05 16:26:18 +0200 | [diff] [blame] | 47 | #include "core.h" |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 48 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 49 | struct client { |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 50 | u32 version; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 51 | struct fw_device *device; |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 52 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 53 | spinlock_t lock; |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 54 | bool in_shutdown; |
| 55 | struct idr resource_idr; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 56 | struct list_head event_list; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 57 | wait_queue_head_t wait; |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 58 | u64 bus_reset_closure; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 59 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 60 | struct fw_iso_context *iso_context; |
Kristian Høgsberg | abaa574 | 2007-04-30 15:03:14 -0400 | [diff] [blame] | 61 | u64 iso_closure; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 62 | struct fw_iso_buffer buffer; |
| 63 | unsigned long vm_start; |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 64 | |
| 65 | struct list_head link; |
Stefan Richter | fb44303 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 66 | struct kref kref; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 67 | }; |
| 68 | |
Stefan Richter | fb44303 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 69 | static inline void client_get(struct client *client) |
| 70 | { |
| 71 | kref_get(&client->kref); |
| 72 | } |
| 73 | |
| 74 | static void client_release(struct kref *kref) |
| 75 | { |
| 76 | struct client *client = container_of(kref, struct client, kref); |
| 77 | |
| 78 | fw_device_put(client->device); |
| 79 | kfree(client); |
| 80 | } |
| 81 | |
| 82 | static void client_put(struct client *client) |
| 83 | { |
| 84 | kref_put(&client->kref, client_release); |
| 85 | } |
| 86 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 87 | struct client_resource; |
| 88 | typedef void (*client_resource_release_fn_t)(struct client *, |
| 89 | struct client_resource *); |
| 90 | struct client_resource { |
| 91 | client_resource_release_fn_t release; |
| 92 | int handle; |
| 93 | }; |
| 94 | |
| 95 | struct address_handler_resource { |
| 96 | struct client_resource resource; |
| 97 | struct fw_address_handler handler; |
| 98 | __u64 closure; |
| 99 | struct client *client; |
| 100 | }; |
| 101 | |
| 102 | struct outbound_transaction_resource { |
| 103 | struct client_resource resource; |
| 104 | struct fw_transaction transaction; |
| 105 | }; |
| 106 | |
| 107 | struct inbound_transaction_resource { |
| 108 | struct client_resource resource; |
| 109 | struct fw_request *request; |
| 110 | void *data; |
| 111 | size_t length; |
| 112 | }; |
| 113 | |
| 114 | struct descriptor_resource { |
| 115 | struct client_resource resource; |
| 116 | struct fw_descriptor descriptor; |
| 117 | u32 data[0]; |
| 118 | }; |
| 119 | |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 120 | struct iso_resource { |
| 121 | struct client_resource resource; |
| 122 | struct client *client; |
| 123 | /* Schedule work and access todo only with client->lock held. */ |
| 124 | struct delayed_work work; |
Stefan Richter | 1ec3c02 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 125 | enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC, |
| 126 | ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo; |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 127 | int generation; |
| 128 | u64 channels; |
| 129 | s32 bandwidth; |
Stefan Richter | 6fdc037 | 2009-06-20 13:23:59 +0200 | [diff] [blame] | 130 | __be32 transaction_data[2]; |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 131 | struct iso_resource_event *e_alloc, *e_dealloc; |
| 132 | }; |
| 133 | |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 134 | static void release_iso_resource(struct client *, struct client_resource *); |
| 135 | |
Stefan Richter | 9fb551b | 2009-10-08 00:41:10 +0200 | [diff] [blame] | 136 | static void schedule_iso_resource(struct iso_resource *r, unsigned long delay) |
| 137 | { |
| 138 | client_get(r->client); |
| 139 | if (!schedule_delayed_work(&r->work, delay)) |
| 140 | client_put(r->client); |
| 141 | } |
| 142 | |
| 143 | static void schedule_if_iso_resource(struct client_resource *resource) |
| 144 | { |
| 145 | if (resource->release == release_iso_resource) |
| 146 | schedule_iso_resource(container_of(resource, |
| 147 | struct iso_resource, resource), 0); |
| 148 | } |
| 149 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 150 | /* |
| 151 | * dequeue_event() just kfree()'s the event, so the event has to be |
| 152 | * the first field in a struct XYZ_event. |
| 153 | */ |
| 154 | struct event { |
| 155 | struct { void *data; size_t size; } v[2]; |
| 156 | struct list_head link; |
| 157 | }; |
| 158 | |
| 159 | struct bus_reset_event { |
| 160 | struct event event; |
| 161 | struct fw_cdev_event_bus_reset reset; |
| 162 | }; |
| 163 | |
| 164 | struct outbound_transaction_event { |
| 165 | struct event event; |
| 166 | struct client *client; |
| 167 | struct outbound_transaction_resource r; |
| 168 | struct fw_cdev_event_response response; |
| 169 | }; |
| 170 | |
| 171 | struct inbound_transaction_event { |
| 172 | struct event event; |
| 173 | struct fw_cdev_event_request request; |
| 174 | }; |
| 175 | |
| 176 | struct iso_interrupt_event { |
| 177 | struct event event; |
| 178 | struct fw_cdev_event_iso_interrupt interrupt; |
| 179 | }; |
| 180 | |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 181 | struct iso_resource_event { |
| 182 | struct event event; |
Stefan Richter | e21fcf7 | 2009-10-08 00:41:38 +0200 | [diff] [blame] | 183 | struct fw_cdev_event_iso_resource iso_resource; |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 184 | }; |
| 185 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 186 | static inline void __user *u64_to_uptr(__u64 value) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 187 | { |
| 188 | return (void __user *)(unsigned long)value; |
| 189 | } |
| 190 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 191 | static inline __u64 uptr_to_u64(void __user *ptr) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 192 | { |
| 193 | return (__u64)(unsigned long)ptr; |
| 194 | } |
| 195 | |
| 196 | static int fw_device_op_open(struct inode *inode, struct file *file) |
| 197 | { |
| 198 | struct fw_device *device; |
| 199 | struct client *client; |
| 200 | |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 201 | device = fw_device_get_by_devt(inode->i_rdev); |
Kristian Høgsberg | a3aca3d | 2007-03-07 12:12:44 -0500 | [diff] [blame] | 202 | if (device == NULL) |
| 203 | return -ENODEV; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 204 | |
Jay Fenlason | 551f4cb | 2008-05-16 11:15:23 -0400 | [diff] [blame] | 205 | if (fw_device_is_shutdown(device)) { |
| 206 | fw_device_put(device); |
| 207 | return -ENODEV; |
| 208 | } |
| 209 | |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 210 | client = kzalloc(sizeof(*client), GFP_KERNEL); |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 211 | if (client == NULL) { |
| 212 | fw_device_put(device); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 213 | return -ENOMEM; |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 214 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 215 | |
Stefan Richter | 96b1906 | 2008-02-02 15:01:09 +0100 | [diff] [blame] | 216 | client->device = device; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 217 | spin_lock_init(&client->lock); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 218 | idr_init(&client->resource_idr); |
| 219 | INIT_LIST_HEAD(&client->event_list); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 220 | init_waitqueue_head(&client->wait); |
Stefan Richter | fb44303 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 221 | kref_init(&client->kref); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 222 | |
| 223 | file->private_data = client; |
| 224 | |
Stefan Richter | d67cfb9 | 2008-10-05 10:37:11 +0200 | [diff] [blame] | 225 | mutex_lock(&device->client_list_mutex); |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 226 | list_add_tail(&client->link, &device->client_list); |
Stefan Richter | d67cfb9 | 2008-10-05 10:37:11 +0200 | [diff] [blame] | 227 | mutex_unlock(&device->client_list_mutex); |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 228 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static void queue_event(struct client *client, struct event *event, |
| 233 | void *data0, size_t size0, void *data1, size_t size1) |
| 234 | { |
| 235 | unsigned long flags; |
| 236 | |
| 237 | event->v[0].data = data0; |
| 238 | event->v[0].size = size0; |
| 239 | event->v[1].data = data1; |
| 240 | event->v[1].size = size1; |
| 241 | |
| 242 | spin_lock_irqsave(&client->lock, flags); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 243 | if (client->in_shutdown) |
| 244 | kfree(event); |
| 245 | else |
| 246 | list_add_tail(&event->link, &client->event_list); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 247 | spin_unlock_irqrestore(&client->lock, flags); |
Jay Fenlason | 83431cb | 2007-10-08 17:00:29 -0400 | [diff] [blame] | 248 | |
| 249 | wake_up_interruptible(&client->wait); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 250 | } |
| 251 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 252 | static int dequeue_event(struct client *client, |
| 253 | char __user *buffer, size_t count) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 254 | { |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 255 | struct event *event; |
| 256 | size_t size, total; |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 257 | int i, ret; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 258 | |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 259 | ret = wait_event_interruptible(client->wait, |
| 260 | !list_empty(&client->event_list) || |
| 261 | fw_device_is_shutdown(client->device)); |
| 262 | if (ret < 0) |
| 263 | return ret; |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 264 | |
| 265 | if (list_empty(&client->event_list) && |
| 266 | fw_device_is_shutdown(client->device)) |
| 267 | return -ENODEV; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 268 | |
Stefan Richter | 3ba9498 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 269 | spin_lock_irq(&client->lock); |
Stefan Richter | a459b8a | 2008-12-21 16:49:57 +0100 | [diff] [blame] | 270 | event = list_first_entry(&client->event_list, struct event, link); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 271 | list_del(&event->link); |
Stefan Richter | 3ba9498 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 272 | spin_unlock_irq(&client->lock); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 273 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 274 | total = 0; |
| 275 | for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) { |
| 276 | size = min(event->v[i].size, count - total); |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 277 | if (copy_to_user(buffer + total, event->v[i].data, size)) { |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 278 | ret = -EFAULT; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 279 | goto out; |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 280 | } |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 281 | total += size; |
| 282 | } |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 283 | ret = total; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 284 | |
| 285 | out: |
| 286 | kfree(event); |
| 287 | |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 288 | return ret; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 289 | } |
| 290 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 291 | static ssize_t fw_device_op_read(struct file *file, char __user *buffer, |
| 292 | size_t count, loff_t *offset) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 293 | { |
| 294 | struct client *client = file->private_data; |
| 295 | |
| 296 | return dequeue_event(client, buffer, count); |
| 297 | } |
| 298 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 299 | static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event, |
| 300 | struct client *client) |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 301 | { |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 302 | struct fw_card *card = client->device->card; |
Jay Fenlason | cf417e54 | 2008-10-03 11:19:09 -0400 | [diff] [blame] | 303 | |
Stefan Richter | 3ba9498 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 304 | spin_lock_irq(&card->lock); |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 305 | |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 306 | event->closure = client->bus_reset_closure; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 307 | event->type = FW_CDEV_EVENT_BUS_RESET; |
Stefan Richter | cf5a56a | 2008-01-24 01:53:51 +0100 | [diff] [blame] | 308 | event->generation = client->device->generation; |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 309 | event->node_id = client->device->node_id; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 310 | event->local_node_id = card->local_node->node_id; |
| 311 | event->bm_node_id = 0; /* FIXME: We don't track the BM. */ |
| 312 | event->irm_node_id = card->irm_node->node_id; |
| 313 | event->root_node_id = card->root_node->node_id; |
Jay Fenlason | cf417e54 | 2008-10-03 11:19:09 -0400 | [diff] [blame] | 314 | |
Stefan Richter | 3ba9498 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 315 | spin_unlock_irq(&card->lock); |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 316 | } |
| 317 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 318 | static void for_each_client(struct fw_device *device, |
| 319 | void (*callback)(struct client *client)) |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 320 | { |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 321 | struct client *c; |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 322 | |
Stefan Richter | d67cfb9 | 2008-10-05 10:37:11 +0200 | [diff] [blame] | 323 | mutex_lock(&device->client_list_mutex); |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 324 | list_for_each_entry(c, &device->client_list, link) |
| 325 | callback(c); |
Stefan Richter | d67cfb9 | 2008-10-05 10:37:11 +0200 | [diff] [blame] | 326 | mutex_unlock(&device->client_list_mutex); |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 327 | } |
| 328 | |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 329 | static int schedule_reallocations(int id, void *p, void *data) |
| 330 | { |
Stefan Richter | 9fb551b | 2009-10-08 00:41:10 +0200 | [diff] [blame] | 331 | schedule_if_iso_resource(p); |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 332 | |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 336 | static void queue_bus_reset_event(struct client *client) |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 337 | { |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 338 | struct bus_reset_event *e; |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 339 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 340 | e = kzalloc(sizeof(*e), GFP_KERNEL); |
| 341 | if (e == NULL) { |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 342 | fw_notify("Out of memory when allocating bus reset event\n"); |
| 343 | return; |
| 344 | } |
| 345 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 346 | fill_bus_reset_event(&e->reset, client); |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 347 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 348 | queue_event(client, &e->event, |
| 349 | &e->reset, sizeof(e->reset), NULL, 0); |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 350 | |
| 351 | spin_lock_irq(&client->lock); |
| 352 | idr_for_each(&client->resource_idr, schedule_reallocations, client); |
| 353 | spin_unlock_irq(&client->lock); |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void fw_device_cdev_update(struct fw_device *device) |
| 357 | { |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 358 | for_each_client(device, queue_bus_reset_event); |
| 359 | } |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 360 | |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 361 | static void wake_up_client(struct client *client) |
| 362 | { |
| 363 | wake_up_interruptible(&client->wait); |
| 364 | } |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 365 | |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 366 | void fw_device_cdev_remove(struct fw_device *device) |
| 367 | { |
| 368 | for_each_client(device, wake_up_client); |
Kristian Høgsberg | 97bd9ef | 2007-03-07 12:12:41 -0500 | [diff] [blame] | 369 | } |
| 370 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 371 | static int ioctl_get_info(struct client *client, void *buffer) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 372 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 373 | struct fw_cdev_get_info *get_info = buffer; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 374 | struct fw_cdev_event_bus_reset bus_reset; |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 375 | unsigned long ret = 0; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 376 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 377 | client->version = get_info->version; |
| 378 | get_info->version = FW_CDEV_VERSION; |
Jay Fenlason | cf417e54 | 2008-10-03 11:19:09 -0400 | [diff] [blame] | 379 | get_info->card = client->device->card->index; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 380 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 381 | down_read(&fw_device_rwsem); |
| 382 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 383 | if (get_info->rom != 0) { |
| 384 | void __user *uptr = u64_to_uptr(get_info->rom); |
| 385 | size_t want = get_info->rom_length; |
Stefan Richter | d84702a | 2007-03-20 19:42:15 +0100 | [diff] [blame] | 386 | size_t have = client->device->config_rom_length * 4; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 387 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 388 | ret = copy_to_user(uptr, client->device->config_rom, |
| 389 | min(want, have)); |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 390 | } |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 391 | get_info->rom_length = client->device->config_rom_length * 4; |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 392 | |
Stefan Richter | c9755e1 | 2008-03-24 20:54:28 +0100 | [diff] [blame] | 393 | up_read(&fw_device_rwsem); |
| 394 | |
| 395 | if (ret != 0) |
| 396 | return -EFAULT; |
| 397 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 398 | client->bus_reset_closure = get_info->bus_reset_closure; |
| 399 | if (get_info->bus_reset != 0) { |
| 400 | void __user *uptr = u64_to_uptr(get_info->bus_reset); |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 401 | |
Kristian Høgsberg | da8ecff | 2007-03-27 01:43:39 -0400 | [diff] [blame] | 402 | fill_bus_reset_event(&bus_reset, client); |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 403 | if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset))) |
Kristian Høgsberg | 344bbc4 | 2007-03-07 12:12:43 -0500 | [diff] [blame] | 404 | return -EFAULT; |
| 405 | } |
| 406 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 410 | static int add_client_resource(struct client *client, |
| 411 | struct client_resource *resource, gfp_t gfp_mask) |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 412 | { |
| 413 | unsigned long flags; |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 414 | int ret; |
| 415 | |
| 416 | retry: |
| 417 | if (idr_pre_get(&client->resource_idr, gfp_mask) == 0) |
| 418 | return -ENOMEM; |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 419 | |
| 420 | spin_lock_irqsave(&client->lock, flags); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 421 | if (client->in_shutdown) |
| 422 | ret = -ECANCELED; |
| 423 | else |
| 424 | ret = idr_get_new(&client->resource_idr, resource, |
| 425 | &resource->handle); |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 426 | if (ret >= 0) { |
Stefan Richter | fb44303 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 427 | client_get(client); |
Stefan Richter | 9fb551b | 2009-10-08 00:41:10 +0200 | [diff] [blame] | 428 | schedule_if_iso_resource(resource); |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 429 | } |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 430 | spin_unlock_irqrestore(&client->lock, flags); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 431 | |
| 432 | if (ret == -EAGAIN) |
| 433 | goto retry; |
| 434 | |
| 435 | return ret < 0 ? ret : 0; |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 436 | } |
| 437 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 438 | static int release_client_resource(struct client *client, u32 handle, |
| 439 | client_resource_release_fn_t release, |
Stefan Richter | e21fcf7 | 2009-10-08 00:41:38 +0200 | [diff] [blame] | 440 | struct client_resource **return_resource) |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 441 | { |
Stefan Richter | e21fcf7 | 2009-10-08 00:41:38 +0200 | [diff] [blame] | 442 | struct client_resource *resource; |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 443 | |
Stefan Richter | 3ba9498 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 444 | spin_lock_irq(&client->lock); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 445 | if (client->in_shutdown) |
Stefan Richter | e21fcf7 | 2009-10-08 00:41:38 +0200 | [diff] [blame] | 446 | resource = NULL; |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 447 | else |
Stefan Richter | e21fcf7 | 2009-10-08 00:41:38 +0200 | [diff] [blame] | 448 | resource = idr_find(&client->resource_idr, handle); |
| 449 | if (resource && resource->release == release) |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 450 | idr_remove(&client->resource_idr, handle); |
Stefan Richter | 3ba9498 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 451 | spin_unlock_irq(&client->lock); |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 452 | |
Stefan Richter | e21fcf7 | 2009-10-08 00:41:38 +0200 | [diff] [blame] | 453 | if (!(resource && resource->release == release)) |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 454 | return -EINVAL; |
| 455 | |
Stefan Richter | e21fcf7 | 2009-10-08 00:41:38 +0200 | [diff] [blame] | 456 | if (return_resource) |
| 457 | *return_resource = resource; |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 458 | else |
Stefan Richter | e21fcf7 | 2009-10-08 00:41:38 +0200 | [diff] [blame] | 459 | resource->release(client, resource); |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 460 | |
Stefan Richter | fb44303 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 461 | client_put(client); |
| 462 | |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 463 | return 0; |
| 464 | } |
| 465 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 466 | static void release_transaction(struct client *client, |
| 467 | struct client_resource *resource) |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 468 | { |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 469 | struct outbound_transaction_resource *r = container_of(resource, |
| 470 | struct outbound_transaction_resource, resource); |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 471 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 472 | fw_cancel_transaction(client->device->card, &r->transaction); |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 473 | } |
| 474 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 475 | static void complete_transaction(struct fw_card *card, int rcode, |
| 476 | void *payload, size_t length, void *data) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 477 | { |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 478 | struct outbound_transaction_event *e = data; |
| 479 | struct fw_cdev_event_response *rsp = &e->response; |
| 480 | struct client *client = e->client; |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 481 | unsigned long flags; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 482 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 483 | if (length < rsp->length) |
| 484 | rsp->length = length; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 485 | if (rcode == RCODE_COMPLETE) |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 486 | memcpy(rsp->data, payload, rsp->length); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 487 | |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 488 | spin_lock_irqsave(&client->lock, flags); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 489 | /* |
Stefan Richter | fb44303 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 490 | * 1. If called while in shutdown, the idr tree must be left untouched. |
| 491 | * The idr handle will be removed and the client reference will be |
| 492 | * dropped later. |
| 493 | * 2. If the call chain was release_client_resource -> |
| 494 | * release_transaction -> complete_transaction (instead of a normal |
| 495 | * conclusion of the transaction), i.e. if this resource was already |
| 496 | * unregistered from the idr, the client reference will be dropped |
| 497 | * by release_client_resource and we must not drop it here. |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 498 | */ |
Stefan Richter | fb44303 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 499 | if (!client->in_shutdown && |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 500 | idr_find(&client->resource_idr, e->r.resource.handle)) { |
| 501 | idr_remove(&client->resource_idr, e->r.resource.handle); |
Stefan Richter | fb44303 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 502 | /* Drop the idr's reference */ |
| 503 | client_put(client); |
| 504 | } |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 505 | spin_unlock_irqrestore(&client->lock, flags); |
| 506 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 507 | rsp->type = FW_CDEV_EVENT_RESPONSE; |
| 508 | rsp->rcode = rcode; |
David Moore | 8401d92 | 2008-07-29 23:46:25 -0700 | [diff] [blame] | 509 | |
| 510 | /* |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 511 | * In the case that sizeof(*rsp) doesn't align with the position of the |
David Moore | 8401d92 | 2008-07-29 23:46:25 -0700 | [diff] [blame] | 512 | * data, and the read is short, preserve an extra copy of the data |
| 513 | * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless |
| 514 | * for short reads and some apps depended on it, this is both safe |
| 515 | * and prudent for compatibility. |
| 516 | */ |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 517 | if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data)) |
| 518 | queue_event(client, &e->event, rsp, sizeof(*rsp), |
| 519 | rsp->data, rsp->length); |
David Moore | 8401d92 | 2008-07-29 23:46:25 -0700 | [diff] [blame] | 520 | else |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 521 | queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length, |
David Moore | 8401d92 | 2008-07-29 23:46:25 -0700 | [diff] [blame] | 522 | NULL, 0); |
Stefan Richter | fb44303 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 523 | |
| 524 | /* Drop the transaction callback's reference */ |
| 525 | client_put(client); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 526 | } |
| 527 | |
Jay Fenlason, Stefan Richter | acfe833 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 528 | static int init_request(struct client *client, |
| 529 | struct fw_cdev_send_request *request, |
| 530 | int destination_id, int speed) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 531 | { |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 532 | struct outbound_transaction_event *e; |
Stefan Richter | 1f3125a | 2008-12-05 22:44:42 +0100 | [diff] [blame] | 533 | int ret; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 534 | |
Stefan Richter | 18e9b10 | 2009-03-10 21:02:21 +0100 | [diff] [blame] | 535 | if (request->tcode != TCODE_STREAM_DATA && |
| 536 | (request->length > 4096 || request->length > 512 << speed)) |
Stefan Richter | 5d3fd69 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 537 | return -EIO; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 538 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 539 | e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL); |
| 540 | if (e == NULL) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 541 | return -ENOMEM; |
| 542 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 543 | e->client = client; |
| 544 | e->response.length = request->length; |
| 545 | e->response.closure = request->closure; |
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 | if (request->data && |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 548 | copy_from_user(e->response.data, |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 549 | u64_to_uptr(request->data), request->length)) { |
Stefan Richter | 1f3125a | 2008-12-05 22:44:42 +0100 | [diff] [blame] | 550 | ret = -EFAULT; |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 551 | goto failed; |
Stefan Richter | 1f3125a | 2008-12-05 22:44:42 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Jay Fenlason, Stefan Richter | acfe833 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 554 | e->r.resource.release = release_transaction; |
| 555 | ret = add_client_resource(client, &e->r.resource, GFP_KERNEL); |
| 556 | if (ret < 0) |
| 557 | goto failed; |
| 558 | |
| 559 | /* Get a reference for the transaction callback */ |
| 560 | client_get(client); |
| 561 | |
| 562 | fw_send_request(client->device->card, &e->r.transaction, |
Stefan Richter | 664d801 | 2009-03-10 21:01:54 +0100 | [diff] [blame] | 563 | request->tcode, destination_id, request->generation, |
| 564 | speed, request->offset, e->response.data, |
| 565 | request->length, complete_transaction, e); |
| 566 | return 0; |
Jay Fenlason, Stefan Richter | acfe833 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 567 | |
Jay Fenlason, Stefan Richter | acfe833 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 568 | failed: |
| 569 | kfree(e); |
| 570 | |
| 571 | return ret; |
| 572 | } |
| 573 | |
| 574 | static int ioctl_send_request(struct client *client, void *buffer) |
| 575 | { |
| 576 | struct fw_cdev_send_request *request = buffer; |
| 577 | |
Stefan Richter | 1f3125a | 2008-12-05 22:44:42 +0100 | [diff] [blame] | 578 | switch (request->tcode) { |
| 579 | case TCODE_WRITE_QUADLET_REQUEST: |
| 580 | case TCODE_WRITE_BLOCK_REQUEST: |
| 581 | case TCODE_READ_QUADLET_REQUEST: |
| 582 | case TCODE_READ_BLOCK_REQUEST: |
| 583 | case TCODE_LOCK_MASK_SWAP: |
| 584 | case TCODE_LOCK_COMPARE_SWAP: |
| 585 | case TCODE_LOCK_FETCH_ADD: |
| 586 | case TCODE_LOCK_LITTLE_ADD: |
| 587 | case TCODE_LOCK_BOUNDED_ADD: |
| 588 | case TCODE_LOCK_WRAP_ADD: |
| 589 | case TCODE_LOCK_VENDOR_DEPENDENT: |
| 590 | break; |
| 591 | default: |
Jay Fenlason, Stefan Richter | acfe833 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 592 | return -EINVAL; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 593 | } |
| 594 | |
Stefan Richter | 207fbef | 2009-03-10 21:01:08 +0100 | [diff] [blame] | 595 | return init_request(client, request, client->device->node_id, |
Jay Fenlason, Stefan Richter | acfe833 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 596 | client->device->max_speed); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 597 | } |
| 598 | |
Stefan Richter | 281e203 | 2010-01-24 16:45:03 +0100 | [diff] [blame] | 599 | static inline bool is_fcp_request(struct fw_request *request) |
| 600 | { |
| 601 | return request == NULL; |
| 602 | } |
| 603 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 604 | static void release_request(struct client *client, |
| 605 | struct client_resource *resource) |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 606 | { |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 607 | struct inbound_transaction_resource *r = container_of(resource, |
| 608 | struct inbound_transaction_resource, resource); |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 609 | |
Stefan Richter | 281e203 | 2010-01-24 16:45:03 +0100 | [diff] [blame] | 610 | if (is_fcp_request(r->request)) |
| 611 | kfree(r->data); |
| 612 | else |
Clemens Ladisch | db5d247 | 2009-12-24 12:05:58 +0100 | [diff] [blame] | 613 | fw_send_response(client->device->card, r->request, |
| 614 | RCODE_CONFLICT_ERROR); |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 615 | kfree(r); |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 616 | } |
| 617 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 618 | static void handle_request(struct fw_card *card, struct fw_request *request, |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 619 | int tcode, int destination, int source, |
| 620 | int generation, int speed, |
| 621 | unsigned long long offset, |
| 622 | void *payload, size_t length, void *callback_data) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 623 | { |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 624 | struct address_handler_resource *handler = callback_data; |
| 625 | struct inbound_transaction_resource *r; |
| 626 | struct inbound_transaction_event *e; |
Stefan Richter | 281e203 | 2010-01-24 16:45:03 +0100 | [diff] [blame] | 627 | void *fcp_frame = NULL; |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 628 | int ret; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 629 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 630 | r = kmalloc(sizeof(*r), GFP_ATOMIC); |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 631 | e = kmalloc(sizeof(*e), GFP_ATOMIC); |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 632 | if (r == NULL || e == NULL) |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 633 | goto failed; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 634 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 635 | r->request = request; |
| 636 | r->data = payload; |
| 637 | r->length = length; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 638 | |
Stefan Richter | 281e203 | 2010-01-24 16:45:03 +0100 | [diff] [blame] | 639 | if (is_fcp_request(request)) { |
| 640 | /* |
| 641 | * FIXME: Let core-transaction.c manage a |
| 642 | * single reference-counted copy? |
| 643 | */ |
| 644 | fcp_frame = kmemdup(payload, length, GFP_ATOMIC); |
| 645 | if (fcp_frame == NULL) |
| 646 | goto failed; |
| 647 | |
| 648 | r->data = fcp_frame; |
| 649 | } |
| 650 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 651 | r->resource.release = release_request; |
| 652 | ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 653 | if (ret < 0) |
| 654 | goto failed; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 655 | |
| 656 | e->request.type = FW_CDEV_EVENT_REQUEST; |
| 657 | e->request.tcode = tcode; |
| 658 | e->request.offset = offset; |
| 659 | e->request.length = length; |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 660 | e->request.handle = r->resource.handle; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 661 | e->request.closure = handler->closure; |
| 662 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 663 | queue_event(handler->client, &e->event, |
Stefan Richter | 281e203 | 2010-01-24 16:45:03 +0100 | [diff] [blame] | 664 | &e->request, sizeof(e->request), r->data, length); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 665 | return; |
| 666 | |
| 667 | failed: |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 668 | kfree(r); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 669 | kfree(e); |
Stefan Richter | 281e203 | 2010-01-24 16:45:03 +0100 | [diff] [blame] | 670 | kfree(fcp_frame); |
| 671 | |
| 672 | if (!is_fcp_request(request)) |
Clemens Ladisch | db5d247 | 2009-12-24 12:05:58 +0100 | [diff] [blame] | 673 | fw_send_response(card, request, RCODE_CONFLICT_ERROR); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 674 | } |
| 675 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 676 | static void release_address_handler(struct client *client, |
| 677 | struct client_resource *resource) |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 678 | { |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 679 | struct address_handler_resource *r = |
| 680 | container_of(resource, struct address_handler_resource, resource); |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 681 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 682 | fw_core_remove_address_handler(&r->handler); |
| 683 | kfree(r); |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 684 | } |
| 685 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 686 | static int ioctl_allocate(struct client *client, void *buffer) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 687 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 688 | struct fw_cdev_allocate *request = buffer; |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 689 | struct address_handler_resource *r; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 690 | struct fw_address_region region; |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 691 | int ret; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 692 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 693 | r = kmalloc(sizeof(*r), GFP_KERNEL); |
| 694 | if (r == NULL) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 695 | return -ENOMEM; |
| 696 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 697 | region.start = request->offset; |
| 698 | region.end = request->offset + request->length; |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 699 | r->handler.length = request->length; |
| 700 | r->handler.address_callback = handle_request; |
| 701 | r->handler.callback_data = r; |
| 702 | r->closure = request->closure; |
| 703 | r->client = client; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 704 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 705 | ret = fw_core_add_address_handler(&r->handler, ®ion); |
Stefan Richter | 3e0b5f0 | 2008-12-14 19:21:01 +0100 | [diff] [blame] | 706 | if (ret < 0) { |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 707 | kfree(r); |
Stefan Richter | 3e0b5f0 | 2008-12-14 19:21:01 +0100 | [diff] [blame] | 708 | return ret; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 709 | } |
| 710 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 711 | r->resource.release = release_address_handler; |
| 712 | ret = add_client_resource(client, &r->resource, GFP_KERNEL); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 713 | if (ret < 0) { |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 714 | release_address_handler(client, &r->resource); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 715 | return ret; |
| 716 | } |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 717 | request->handle = r->resource.handle; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 722 | static int ioctl_deallocate(struct client *client, void *buffer) |
Kristian Høgsberg | 9472316 | 2007-03-14 17:34:55 -0400 | [diff] [blame] | 723 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 724 | struct fw_cdev_deallocate *request = buffer; |
Kristian Høgsberg | 9472316 | 2007-03-14 17:34:55 -0400 | [diff] [blame] | 725 | |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 726 | return release_client_resource(client, request->handle, |
| 727 | release_address_handler, NULL); |
Kristian Høgsberg | 9472316 | 2007-03-14 17:34:55 -0400 | [diff] [blame] | 728 | } |
| 729 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 730 | static int ioctl_send_response(struct client *client, void *buffer) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 731 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 732 | struct fw_cdev_send_response *request = buffer; |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 733 | struct client_resource *resource; |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 734 | struct inbound_transaction_resource *r; |
Stefan Richter | 7e44c0b | 2009-10-08 00:39:56 +0200 | [diff] [blame] | 735 | int ret = 0; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 736 | |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 737 | if (release_client_resource(client, request->handle, |
| 738 | release_request, &resource) < 0) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 739 | return -EINVAL; |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 740 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 741 | r = container_of(resource, struct inbound_transaction_resource, |
| 742 | resource); |
Stefan Richter | 281e203 | 2010-01-24 16:45:03 +0100 | [diff] [blame] | 743 | if (is_fcp_request(r->request)) |
| 744 | goto out; |
| 745 | |
| 746 | if (request->length < r->length) |
| 747 | r->length = request->length; |
| 748 | if (copy_from_user(r->data, u64_to_uptr(request->data), r->length)) { |
| 749 | ret = -EFAULT; |
| 750 | kfree(r->request); |
| 751 | goto out; |
Stefan Richter | 7e44c0b | 2009-10-08 00:39:56 +0200 | [diff] [blame] | 752 | } |
Stefan Richter | 281e203 | 2010-01-24 16:45:03 +0100 | [diff] [blame] | 753 | fw_send_response(client->device->card, r->request, request->rcode); |
Stefan Richter | 7e44c0b | 2009-10-08 00:39:56 +0200 | [diff] [blame] | 754 | out: |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 755 | kfree(r); |
| 756 | |
Stefan Richter | 7e44c0b | 2009-10-08 00:39:56 +0200 | [diff] [blame] | 757 | return ret; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 758 | } |
| 759 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 760 | static int ioctl_initiate_bus_reset(struct client *client, void *buffer) |
Kristian Høgsberg | 5371842 | 2007-03-07 12:12:42 -0500 | [diff] [blame] | 761 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 762 | struct fw_cdev_initiate_bus_reset *request = buffer; |
Kristian Høgsberg | 5371842 | 2007-03-07 12:12:42 -0500 | [diff] [blame] | 763 | int short_reset; |
| 764 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 765 | short_reset = (request->type == FW_CDEV_SHORT_RESET); |
Kristian Høgsberg | 5371842 | 2007-03-07 12:12:42 -0500 | [diff] [blame] | 766 | |
| 767 | return fw_core_initiate_bus_reset(client->device->card, short_reset); |
| 768 | } |
| 769 | |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 770 | static void release_descriptor(struct client *client, |
| 771 | struct client_resource *resource) |
| 772 | { |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 773 | struct descriptor_resource *r = |
| 774 | container_of(resource, struct descriptor_resource, resource); |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 775 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 776 | fw_core_remove_descriptor(&r->descriptor); |
| 777 | kfree(r); |
Kristian Høgsberg | 3964a44 | 2007-03-27 01:43:41 -0400 | [diff] [blame] | 778 | } |
| 779 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 780 | static int ioctl_add_descriptor(struct client *client, void *buffer) |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 781 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 782 | struct fw_cdev_add_descriptor *request = buffer; |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 783 | struct descriptor_resource *r; |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 784 | int ret; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 785 | |
Stefan Richter | de487da | 2009-03-10 21:00:23 +0100 | [diff] [blame] | 786 | /* Access policy: Allow this ioctl only on local nodes' device files. */ |
Stefan Richter | 9236889 | 2009-05-13 21:42:14 +0200 | [diff] [blame] | 787 | if (!client->device->is_local) |
Stefan Richter | de487da | 2009-03-10 21:00:23 +0100 | [diff] [blame] | 788 | return -ENOSYS; |
| 789 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 790 | if (request->length > 256) |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 791 | return -EINVAL; |
| 792 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 793 | r = kmalloc(sizeof(*r) + request->length * 4, GFP_KERNEL); |
| 794 | if (r == NULL) |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 795 | return -ENOMEM; |
| 796 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 797 | if (copy_from_user(r->data, |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 798 | u64_to_uptr(request->data), request->length * 4)) { |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 799 | ret = -EFAULT; |
| 800 | goto failed; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 803 | r->descriptor.length = request->length; |
| 804 | r->descriptor.immediate = request->immediate; |
| 805 | r->descriptor.key = request->key; |
| 806 | r->descriptor.data = r->data; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 807 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 808 | ret = fw_core_add_descriptor(&r->descriptor); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 809 | if (ret < 0) |
| 810 | goto failed; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 811 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 812 | r->resource.release = release_descriptor; |
| 813 | ret = add_client_resource(client, &r->resource, GFP_KERNEL); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 814 | if (ret < 0) { |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 815 | fw_core_remove_descriptor(&r->descriptor); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 816 | goto failed; |
| 817 | } |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 818 | request->handle = r->resource.handle; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 819 | |
| 820 | return 0; |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 821 | failed: |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 822 | kfree(r); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 823 | |
| 824 | return ret; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 825 | } |
| 826 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 827 | static int ioctl_remove_descriptor(struct client *client, void *buffer) |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 828 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 829 | struct fw_cdev_remove_descriptor *request = buffer; |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 830 | |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 831 | return release_client_resource(client, request->handle, |
| 832 | release_descriptor, NULL); |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 833 | } |
| 834 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 835 | static void iso_callback(struct fw_iso_context *context, u32 cycle, |
| 836 | size_t header_length, void *header, void *data) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 837 | { |
| 838 | struct client *client = data; |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 839 | struct iso_interrupt_event *e; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 840 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 841 | e = kzalloc(sizeof(*e) + header_length, GFP_ATOMIC); |
| 842 | if (e == NULL) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 843 | return; |
| 844 | |
Stefan Richter | 97c18b7 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 845 | e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT; |
| 846 | e->interrupt.closure = client->iso_closure; |
| 847 | e->interrupt.cycle = cycle; |
| 848 | e->interrupt.header_length = header_length; |
| 849 | memcpy(e->interrupt.header, header, header_length); |
| 850 | queue_event(client, &e->event, &e->interrupt, |
| 851 | sizeof(e->interrupt) + header_length, NULL, 0); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 852 | } |
| 853 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 854 | static int ioctl_create_iso_context(struct client *client, void *buffer) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 855 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 856 | struct fw_cdev_create_iso_context *request = buffer; |
Kristian Høgsberg | 24315c5 | 2007-06-20 17:48:07 -0400 | [diff] [blame] | 857 | struct fw_iso_context *context; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 858 | |
Stefan Richter | fae6031 | 2008-02-20 21:10:06 +0100 | [diff] [blame] | 859 | /* We only support one context at this time. */ |
| 860 | if (client->iso_context != NULL) |
| 861 | return -EBUSY; |
| 862 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 863 | if (request->channel > 63) |
Kristian Høgsberg | 21efb3c | 2007-02-16 17:34:50 -0500 | [diff] [blame] | 864 | return -EINVAL; |
| 865 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 866 | switch (request->type) { |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 867 | case FW_ISO_CONTEXT_RECEIVE: |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 868 | if (request->header_size < 4 || (request->header_size & 3)) |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 869 | return -EINVAL; |
| 870 | |
| 871 | break; |
| 872 | |
| 873 | case FW_ISO_CONTEXT_TRANSMIT: |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 874 | if (request->speed > SCODE_3200) |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 875 | return -EINVAL; |
| 876 | |
| 877 | break; |
| 878 | |
| 879 | default: |
Kristian Høgsberg | 21efb3c | 2007-02-16 17:34:50 -0500 | [diff] [blame] | 880 | return -EINVAL; |
Kristian Høgsberg | c70dc78 | 2007-03-14 17:34:53 -0400 | [diff] [blame] | 881 | } |
| 882 | |
Kristian Høgsberg | 24315c5 | 2007-06-20 17:48:07 -0400 | [diff] [blame] | 883 | context = fw_iso_context_create(client->device->card, |
| 884 | request->type, |
| 885 | request->channel, |
| 886 | request->speed, |
| 887 | request->header_size, |
| 888 | iso_callback, client); |
| 889 | if (IS_ERR(context)) |
| 890 | return PTR_ERR(context); |
| 891 | |
Kristian Høgsberg | abaa574 | 2007-04-30 15:03:14 -0400 | [diff] [blame] | 892 | client->iso_closure = request->closure; |
Kristian Høgsberg | 24315c5 | 2007-06-20 17:48:07 -0400 | [diff] [blame] | 893 | client->iso_context = context; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 894 | |
Kristian Høgsberg | abaa574 | 2007-04-30 15:03:14 -0400 | [diff] [blame] | 895 | /* We only support one context at this time. */ |
| 896 | request->handle = 0; |
| 897 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 898 | return 0; |
| 899 | } |
| 900 | |
Kristian Høgsberg | 1ca31ae | 2007-05-31 11:16:43 -0400 | [diff] [blame] | 901 | /* Macros for decoding the iso packet control header. */ |
| 902 | #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff) |
| 903 | #define GET_INTERRUPT(v) (((v) >> 16) & 0x01) |
| 904 | #define GET_SKIP(v) (((v) >> 17) & 0x01) |
Stefan Richter | 7a10034 | 2008-09-12 18:09:55 +0200 | [diff] [blame] | 905 | #define GET_TAG(v) (((v) >> 18) & 0x03) |
| 906 | #define GET_SY(v) (((v) >> 20) & 0x0f) |
Kristian Høgsberg | 1ca31ae | 2007-05-31 11:16:43 -0400 | [diff] [blame] | 907 | #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff) |
| 908 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 909 | static int ioctl_queue_iso(struct client *client, void *buffer) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 910 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 911 | struct fw_cdev_queue_iso *request = buffer; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 912 | struct fw_cdev_iso_packet __user *p, *end, *next; |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 913 | struct fw_iso_context *ctx = client->iso_context; |
Kristian Høgsberg | ef370ee | 2007-03-28 20:46:23 +0200 | [diff] [blame] | 914 | unsigned long payload, buffer_end, header_length; |
Kristian Høgsberg | 1ca31ae | 2007-05-31 11:16:43 -0400 | [diff] [blame] | 915 | u32 control; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 916 | int count; |
| 917 | struct { |
| 918 | struct fw_iso_packet packet; |
| 919 | u8 header[256]; |
| 920 | } u; |
| 921 | |
Kristian Høgsberg | abaa574 | 2007-04-30 15:03:14 -0400 | [diff] [blame] | 922 | if (ctx == NULL || request->handle != 0) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 923 | return -EINVAL; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 924 | |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 925 | /* |
| 926 | * 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] | 927 | * the iso buffer, and the pointer points inside the buffer, |
| 928 | * we setup the payload pointers accordingly. Otherwise we |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 929 | * set them both to 0, which will still let packets with |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 930 | * payload_length == 0 through. In other words, if no packets |
| 931 | * use the indirect payload, the iso buffer need not be mapped |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 932 | * and the request->data pointer is ignored. |
| 933 | */ |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 934 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 935 | payload = (unsigned long)request->data - client->vm_start; |
Kristian Høgsberg | ef370ee | 2007-03-28 20:46:23 +0200 | [diff] [blame] | 936 | buffer_end = client->buffer.page_count << PAGE_SHIFT; |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 937 | if (request->data == 0 || client->buffer.pages == NULL || |
Kristian Høgsberg | ef370ee | 2007-03-28 20:46:23 +0200 | [diff] [blame] | 938 | payload >= buffer_end) { |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 939 | payload = 0; |
Kristian Høgsberg | ef370ee | 2007-03-28 20:46:23 +0200 | [diff] [blame] | 940 | buffer_end = 0; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 941 | } |
| 942 | |
Al Viro | 1ccc914 | 2007-10-14 19:34:40 +0100 | [diff] [blame] | 943 | p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets); |
| 944 | |
| 945 | if (!access_ok(VERIFY_READ, p, request->size)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 946 | return -EFAULT; |
| 947 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 948 | end = (void __user *)p + request->size; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 949 | count = 0; |
| 950 | while (p < end) { |
Kristian Høgsberg | 1ca31ae | 2007-05-31 11:16:43 -0400 | [diff] [blame] | 951 | if (get_user(control, &p->control)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 952 | return -EFAULT; |
Kristian Høgsberg | 1ca31ae | 2007-05-31 11:16:43 -0400 | [diff] [blame] | 953 | u.packet.payload_length = GET_PAYLOAD_LENGTH(control); |
| 954 | u.packet.interrupt = GET_INTERRUPT(control); |
| 955 | u.packet.skip = GET_SKIP(control); |
| 956 | u.packet.tag = GET_TAG(control); |
| 957 | u.packet.sy = GET_SY(control); |
| 958 | u.packet.header_length = GET_HEADER_LENGTH(control); |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 959 | |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 960 | if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) { |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 961 | header_length = u.packet.header_length; |
| 962 | } else { |
Kristian Høgsberg | c781c06 | 2007-05-07 20:33:32 -0400 | [diff] [blame] | 963 | /* |
| 964 | * We require that header_length is a multiple of |
| 965 | * the fixed header size, ctx->header_size. |
| 966 | */ |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 967 | if (ctx->header_size == 0) { |
| 968 | if (u.packet.header_length > 0) |
| 969 | return -EINVAL; |
| 970 | } else if (u.packet.header_length % ctx->header_size != 0) { |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 971 | return -EINVAL; |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 972 | } |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 973 | header_length = 0; |
| 974 | } |
| 975 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 976 | next = (struct fw_cdev_iso_packet __user *) |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 977 | &p->header[header_length / 4]; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 978 | if (next > end) |
| 979 | return -EINVAL; |
| 980 | if (__copy_from_user |
Kristian Høgsberg | 295e3fe | 2007-02-16 17:34:40 -0500 | [diff] [blame] | 981 | (u.packet.header, p->header, header_length)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 982 | return -EFAULT; |
Kristian Høgsberg | 98b6cbe | 2007-02-16 17:34:51 -0500 | [diff] [blame] | 983 | if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT && |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 984 | u.packet.header_length + u.packet.payload_length > 0) |
| 985 | return -EINVAL; |
Kristian Høgsberg | ef370ee | 2007-03-28 20:46:23 +0200 | [diff] [blame] | 986 | if (payload + u.packet.payload_length > buffer_end) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 987 | return -EINVAL; |
| 988 | |
Kristian Høgsberg | 9b32d5f | 2007-02-16 17:34:44 -0500 | [diff] [blame] | 989 | if (fw_iso_context_queue(ctx, &u.packet, |
| 990 | &client->buffer, payload)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 991 | break; |
| 992 | |
| 993 | p = next; |
| 994 | payload += u.packet.payload_length; |
| 995 | count++; |
| 996 | } |
| 997 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 998 | request->size -= uptr_to_u64(p) - request->packets; |
| 999 | request->packets = uptr_to_u64(p); |
| 1000 | request->data = client->vm_start + payload; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1001 | |
| 1002 | return count; |
| 1003 | } |
| 1004 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 1005 | static int ioctl_start_iso(struct client *client, void *buffer) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1006 | { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 1007 | struct fw_cdev_start_iso *request = buffer; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1008 | |
Stefan Richter | fae6031 | 2008-02-20 21:10:06 +0100 | [diff] [blame] | 1009 | if (client->iso_context == NULL || request->handle != 0) |
Kristian Høgsberg | abaa574 | 2007-04-30 15:03:14 -0400 | [diff] [blame] | 1010 | return -EINVAL; |
Stefan Richter | fae6031 | 2008-02-20 21:10:06 +0100 | [diff] [blame] | 1011 | |
Kristian Høgsberg | eb0306e | 2007-03-14 17:34:54 -0400 | [diff] [blame] | 1012 | if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) { |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 1013 | if (request->tags == 0 || request->tags > 15) |
Kristian Høgsberg | eb0306e | 2007-03-14 17:34:54 -0400 | [diff] [blame] | 1014 | return -EINVAL; |
| 1015 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 1016 | if (request->sync > 15) |
Kristian Høgsberg | eb0306e | 2007-03-14 17:34:54 -0400 | [diff] [blame] | 1017 | return -EINVAL; |
| 1018 | } |
| 1019 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 1020 | return fw_iso_context_start(client->iso_context, request->cycle, |
| 1021 | request->sync, request->tags); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1022 | } |
| 1023 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 1024 | static int ioctl_stop_iso(struct client *client, void *buffer) |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 1025 | { |
Kristian Høgsberg | abaa574 | 2007-04-30 15:03:14 -0400 | [diff] [blame] | 1026 | struct fw_cdev_stop_iso *request = buffer; |
| 1027 | |
Stefan Richter | fae6031 | 2008-02-20 21:10:06 +0100 | [diff] [blame] | 1028 | if (client->iso_context == NULL || request->handle != 0) |
Kristian Høgsberg | abaa574 | 2007-04-30 15:03:14 -0400 | [diff] [blame] | 1029 | return -EINVAL; |
| 1030 | |
Kristian Høgsberg | b829566 | 2007-02-16 17:34:42 -0500 | [diff] [blame] | 1031 | return fw_iso_context_stop(client->iso_context); |
| 1032 | } |
| 1033 | |
Stefan Richter | a64408b | 2007-09-29 10:41:58 +0200 | [diff] [blame] | 1034 | static int ioctl_get_cycle_timer(struct client *client, void *buffer) |
| 1035 | { |
| 1036 | struct fw_cdev_get_cycle_timer *request = buffer; |
| 1037 | struct fw_card *card = client->device->card; |
| 1038 | unsigned long long bus_time; |
| 1039 | struct timeval tv; |
| 1040 | unsigned long flags; |
| 1041 | |
| 1042 | preempt_disable(); |
| 1043 | local_irq_save(flags); |
| 1044 | |
| 1045 | bus_time = card->driver->get_bus_time(card); |
| 1046 | do_gettimeofday(&tv); |
| 1047 | |
| 1048 | local_irq_restore(flags); |
| 1049 | preempt_enable(); |
| 1050 | |
| 1051 | request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec; |
| 1052 | request->cycle_timer = bus_time & 0xffffffff; |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1056 | static void iso_resource_work(struct work_struct *work) |
| 1057 | { |
| 1058 | struct iso_resource_event *e; |
| 1059 | struct iso_resource *r = |
| 1060 | container_of(work, struct iso_resource, work.work); |
| 1061 | struct client *client = r->client; |
| 1062 | int generation, channel, bandwidth, todo; |
| 1063 | bool skip, free, success; |
| 1064 | |
| 1065 | spin_lock_irq(&client->lock); |
| 1066 | generation = client->device->generation; |
| 1067 | todo = r->todo; |
| 1068 | /* Allow 1000ms grace period for other reallocations. */ |
| 1069 | if (todo == ISO_RES_ALLOC && |
| 1070 | time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) { |
Stefan Richter | 9fb551b | 2009-10-08 00:41:10 +0200 | [diff] [blame] | 1071 | schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3)); |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1072 | skip = true; |
| 1073 | } else { |
| 1074 | /* We could be called twice within the same generation. */ |
| 1075 | skip = todo == ISO_RES_REALLOC && |
| 1076 | r->generation == generation; |
| 1077 | } |
Stefan Richter | 1ec3c02 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1078 | free = todo == ISO_RES_DEALLOC || |
| 1079 | todo == ISO_RES_ALLOC_ONCE || |
| 1080 | todo == ISO_RES_DEALLOC_ONCE; |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1081 | r->generation = generation; |
| 1082 | spin_unlock_irq(&client->lock); |
| 1083 | |
| 1084 | if (skip) |
| 1085 | goto out; |
| 1086 | |
| 1087 | bandwidth = r->bandwidth; |
| 1088 | |
| 1089 | fw_iso_resource_manage(client->device->card, generation, |
| 1090 | r->channels, &channel, &bandwidth, |
Stefan Richter | 1ec3c02 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1091 | todo == ISO_RES_ALLOC || |
| 1092 | todo == ISO_RES_REALLOC || |
Stefan Richter | 6fdc037 | 2009-06-20 13:23:59 +0200 | [diff] [blame] | 1093 | todo == ISO_RES_ALLOC_ONCE, |
| 1094 | r->transaction_data); |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1095 | /* |
| 1096 | * Is this generation outdated already? As long as this resource sticks |
| 1097 | * in the idr, it will be scheduled again for a newer generation or at |
| 1098 | * shutdown. |
| 1099 | */ |
| 1100 | if (channel == -EAGAIN && |
| 1101 | (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC)) |
| 1102 | goto out; |
| 1103 | |
| 1104 | success = channel >= 0 || bandwidth > 0; |
| 1105 | |
| 1106 | spin_lock_irq(&client->lock); |
| 1107 | /* |
| 1108 | * Transit from allocation to reallocation, except if the client |
| 1109 | * requested deallocation in the meantime. |
| 1110 | */ |
| 1111 | if (r->todo == ISO_RES_ALLOC) |
| 1112 | r->todo = ISO_RES_REALLOC; |
| 1113 | /* |
| 1114 | * Allocation or reallocation failure? Pull this resource out of the |
| 1115 | * idr and prepare for deletion, unless the client is shutting down. |
| 1116 | */ |
| 1117 | if (r->todo == ISO_RES_REALLOC && !success && |
| 1118 | !client->in_shutdown && |
| 1119 | idr_find(&client->resource_idr, r->resource.handle)) { |
| 1120 | idr_remove(&client->resource_idr, r->resource.handle); |
| 1121 | client_put(client); |
| 1122 | free = true; |
| 1123 | } |
| 1124 | spin_unlock_irq(&client->lock); |
| 1125 | |
| 1126 | if (todo == ISO_RES_ALLOC && channel >= 0) |
Stefan Richter | 5d9cb7d | 2009-01-08 23:07:40 +0100 | [diff] [blame] | 1127 | r->channels = 1ULL << channel; |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1128 | |
| 1129 | if (todo == ISO_RES_REALLOC && success) |
| 1130 | goto out; |
| 1131 | |
Stefan Richter | 1ec3c02 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1132 | if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) { |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1133 | e = r->e_alloc; |
| 1134 | r->e_alloc = NULL; |
| 1135 | } else { |
| 1136 | e = r->e_dealloc; |
| 1137 | r->e_dealloc = NULL; |
| 1138 | } |
Stefan Richter | e21fcf7 | 2009-10-08 00:41:38 +0200 | [diff] [blame] | 1139 | e->iso_resource.handle = r->resource.handle; |
| 1140 | e->iso_resource.channel = channel; |
| 1141 | e->iso_resource.bandwidth = bandwidth; |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1142 | |
| 1143 | queue_event(client, &e->event, |
Stefan Richter | e21fcf7 | 2009-10-08 00:41:38 +0200 | [diff] [blame] | 1144 | &e->iso_resource, sizeof(e->iso_resource), NULL, 0); |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1145 | |
| 1146 | if (free) { |
| 1147 | cancel_delayed_work(&r->work); |
| 1148 | kfree(r->e_alloc); |
| 1149 | kfree(r->e_dealloc); |
| 1150 | kfree(r); |
| 1151 | } |
| 1152 | out: |
| 1153 | client_put(client); |
| 1154 | } |
| 1155 | |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1156 | static void release_iso_resource(struct client *client, |
| 1157 | struct client_resource *resource) |
| 1158 | { |
| 1159 | struct iso_resource *r = |
| 1160 | container_of(resource, struct iso_resource, resource); |
| 1161 | |
| 1162 | spin_lock_irq(&client->lock); |
| 1163 | r->todo = ISO_RES_DEALLOC; |
Stefan Richter | 9fb551b | 2009-10-08 00:41:10 +0200 | [diff] [blame] | 1164 | schedule_iso_resource(r, 0); |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1165 | spin_unlock_irq(&client->lock); |
| 1166 | } |
| 1167 | |
Stefan Richter | 1ec3c02 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1168 | static int init_iso_resource(struct client *client, |
| 1169 | struct fw_cdev_allocate_iso_resource *request, int todo) |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1170 | { |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1171 | struct iso_resource_event *e1, *e2; |
| 1172 | struct iso_resource *r; |
| 1173 | int ret; |
| 1174 | |
| 1175 | if ((request->channels == 0 && request->bandwidth == 0) || |
| 1176 | request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL || |
| 1177 | request->bandwidth < 0) |
| 1178 | return -EINVAL; |
| 1179 | |
| 1180 | r = kmalloc(sizeof(*r), GFP_KERNEL); |
| 1181 | e1 = kmalloc(sizeof(*e1), GFP_KERNEL); |
| 1182 | e2 = kmalloc(sizeof(*e2), GFP_KERNEL); |
| 1183 | if (r == NULL || e1 == NULL || e2 == NULL) { |
| 1184 | ret = -ENOMEM; |
| 1185 | goto fail; |
| 1186 | } |
| 1187 | |
| 1188 | INIT_DELAYED_WORK(&r->work, iso_resource_work); |
| 1189 | r->client = client; |
Stefan Richter | 1ec3c02 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1190 | r->todo = todo; |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1191 | r->generation = -1; |
| 1192 | r->channels = request->channels; |
| 1193 | r->bandwidth = request->bandwidth; |
| 1194 | r->e_alloc = e1; |
| 1195 | r->e_dealloc = e2; |
| 1196 | |
Stefan Richter | e21fcf7 | 2009-10-08 00:41:38 +0200 | [diff] [blame] | 1197 | e1->iso_resource.closure = request->closure; |
| 1198 | e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED; |
| 1199 | e2->iso_resource.closure = request->closure; |
| 1200 | e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED; |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1201 | |
Stefan Richter | 1ec3c02 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1202 | if (todo == ISO_RES_ALLOC) { |
| 1203 | r->resource.release = release_iso_resource; |
| 1204 | ret = add_client_resource(client, &r->resource, GFP_KERNEL); |
Stefan Richter | 81610b8 | 2009-01-11 13:44:46 +0100 | [diff] [blame] | 1205 | if (ret < 0) |
| 1206 | goto fail; |
Stefan Richter | 1ec3c02 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1207 | } else { |
| 1208 | r->resource.release = NULL; |
| 1209 | r->resource.handle = -1; |
Stefan Richter | 9fb551b | 2009-10-08 00:41:10 +0200 | [diff] [blame] | 1210 | schedule_iso_resource(r, 0); |
Stefan Richter | 1ec3c02 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1211 | } |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1212 | request->handle = r->resource.handle; |
| 1213 | |
| 1214 | return 0; |
| 1215 | fail: |
| 1216 | kfree(r); |
| 1217 | kfree(e1); |
| 1218 | kfree(e2); |
| 1219 | |
| 1220 | return ret; |
| 1221 | } |
| 1222 | |
Stefan Richter | 1ec3c02 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1223 | static int ioctl_allocate_iso_resource(struct client *client, void *buffer) |
| 1224 | { |
| 1225 | struct fw_cdev_allocate_iso_resource *request = buffer; |
| 1226 | |
| 1227 | return init_iso_resource(client, request, ISO_RES_ALLOC); |
| 1228 | } |
| 1229 | |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1230 | static int ioctl_deallocate_iso_resource(struct client *client, void *buffer) |
| 1231 | { |
| 1232 | struct fw_cdev_deallocate *request = buffer; |
| 1233 | |
| 1234 | return release_client_resource(client, request->handle, |
| 1235 | release_iso_resource, NULL); |
| 1236 | } |
| 1237 | |
Stefan Richter | 1ec3c02 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1238 | static int ioctl_allocate_iso_resource_once(struct client *client, void *buffer) |
| 1239 | { |
| 1240 | struct fw_cdev_allocate_iso_resource *request = buffer; |
| 1241 | |
| 1242 | return init_iso_resource(client, request, ISO_RES_ALLOC_ONCE); |
| 1243 | } |
| 1244 | |
| 1245 | static int ioctl_deallocate_iso_resource_once(struct client *client, void *buffer) |
| 1246 | { |
| 1247 | struct fw_cdev_allocate_iso_resource *request = buffer; |
| 1248 | |
| 1249 | return init_iso_resource(client, request, ISO_RES_DEALLOC_ONCE); |
| 1250 | } |
| 1251 | |
Stefan Richter | c8a2590 | 2009-03-10 20:59:16 +0100 | [diff] [blame] | 1252 | /* |
| 1253 | * Returns a speed code: Maximum speed to or from this device, |
| 1254 | * limited by the device's link speed, the local node's link speed, |
| 1255 | * and all PHY port speeds between the two links. |
| 1256 | */ |
Stefan Richter | 33580a3 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1257 | static int ioctl_get_speed(struct client *client, void *buffer) |
| 1258 | { |
Stefan Richter | c8a2590 | 2009-03-10 20:59:16 +0100 | [diff] [blame] | 1259 | return client->device->max_speed; |
Stefan Richter | 33580a3 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1260 | } |
| 1261 | |
Jay Fenlason, Stefan Richter | acfe833 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1262 | static int ioctl_send_broadcast_request(struct client *client, void *buffer) |
| 1263 | { |
| 1264 | struct fw_cdev_send_request *request = buffer; |
| 1265 | |
| 1266 | switch (request->tcode) { |
| 1267 | case TCODE_WRITE_QUADLET_REQUEST: |
| 1268 | case TCODE_WRITE_BLOCK_REQUEST: |
| 1269 | break; |
| 1270 | default: |
| 1271 | return -EINVAL; |
| 1272 | } |
| 1273 | |
Stefan Richter | 1566f3d | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1274 | /* Security policy: Only allow accesses to Units Space. */ |
| 1275 | if (request->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END) |
| 1276 | return -EACCES; |
| 1277 | |
Jay Fenlason, Stefan Richter | acfe833 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1278 | return init_request(client, request, LOCAL_BUS | 0x3f, SCODE_100); |
| 1279 | } |
| 1280 | |
Jay Fenlason | f8c2287 | 2009-03-05 19:08:40 +0100 | [diff] [blame] | 1281 | static int ioctl_send_stream_packet(struct client *client, void *buffer) |
| 1282 | { |
Stefan Richter | 18e9b10 | 2009-03-10 21:02:21 +0100 | [diff] [blame] | 1283 | struct fw_cdev_send_stream_packet *p = buffer; |
| 1284 | struct fw_cdev_send_request request; |
| 1285 | int dest; |
Jay Fenlason | f8c2287 | 2009-03-05 19:08:40 +0100 | [diff] [blame] | 1286 | |
Stefan Richter | 18e9b10 | 2009-03-10 21:02:21 +0100 | [diff] [blame] | 1287 | if (p->speed > client->device->card->link_speed || |
| 1288 | p->length > 1024 << p->speed) |
| 1289 | return -EIO; |
Jay Fenlason | f8c2287 | 2009-03-05 19:08:40 +0100 | [diff] [blame] | 1290 | |
Stefan Richter | 18e9b10 | 2009-03-10 21:02:21 +0100 | [diff] [blame] | 1291 | if (p->tag > 3 || p->channel > 63 || p->sy > 15) |
| 1292 | return -EINVAL; |
| 1293 | |
| 1294 | dest = fw_stream_packet_destination_id(p->tag, p->channel, p->sy); |
| 1295 | request.tcode = TCODE_STREAM_DATA; |
| 1296 | request.length = p->length; |
| 1297 | request.closure = p->closure; |
| 1298 | request.data = p->data; |
| 1299 | request.generation = p->generation; |
| 1300 | |
| 1301 | return init_request(client, &request, dest, p->speed); |
Jay Fenlason | f8c2287 | 2009-03-05 19:08:40 +0100 | [diff] [blame] | 1302 | } |
| 1303 | |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 1304 | static int (* const ioctl_handlers[])(struct client *client, void *buffer) = { |
| 1305 | ioctl_get_info, |
| 1306 | ioctl_send_request, |
| 1307 | ioctl_allocate, |
| 1308 | ioctl_deallocate, |
| 1309 | ioctl_send_response, |
| 1310 | ioctl_initiate_bus_reset, |
| 1311 | ioctl_add_descriptor, |
| 1312 | ioctl_remove_descriptor, |
| 1313 | ioctl_create_iso_context, |
| 1314 | ioctl_queue_iso, |
| 1315 | ioctl_start_iso, |
| 1316 | ioctl_stop_iso, |
Stefan Richter | a64408b | 2007-09-29 10:41:58 +0200 | [diff] [blame] | 1317 | ioctl_get_cycle_timer, |
Jay Fenlason, Stefan Richter | b1bda4c | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1318 | ioctl_allocate_iso_resource, |
| 1319 | ioctl_deallocate_iso_resource, |
Stefan Richter | 1ec3c02 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1320 | ioctl_allocate_iso_resource_once, |
| 1321 | ioctl_deallocate_iso_resource_once, |
Stefan Richter | 33580a3 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1322 | ioctl_get_speed, |
Jay Fenlason, Stefan Richter | acfe833 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1323 | ioctl_send_broadcast_request, |
Jay Fenlason | f8c2287 | 2009-03-05 19:08:40 +0100 | [diff] [blame] | 1324 | ioctl_send_stream_packet, |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 1325 | }; |
| 1326 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 1327 | static int dispatch_ioctl(struct client *client, |
| 1328 | unsigned int cmd, void __user *arg) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1329 | { |
Stefan Richter | b2c0a2a | 2009-10-15 21:16:53 +0200 | [diff] [blame] | 1330 | char buffer[sizeof(union { |
| 1331 | struct fw_cdev_get_info _00; |
| 1332 | struct fw_cdev_send_request _01; |
| 1333 | struct fw_cdev_allocate _02; |
| 1334 | struct fw_cdev_deallocate _03; |
| 1335 | struct fw_cdev_send_response _04; |
| 1336 | struct fw_cdev_initiate_bus_reset _05; |
| 1337 | struct fw_cdev_add_descriptor _06; |
| 1338 | struct fw_cdev_remove_descriptor _07; |
| 1339 | struct fw_cdev_create_iso_context _08; |
| 1340 | struct fw_cdev_queue_iso _09; |
| 1341 | struct fw_cdev_start_iso _0a; |
| 1342 | struct fw_cdev_stop_iso _0b; |
| 1343 | struct fw_cdev_get_cycle_timer _0c; |
| 1344 | struct fw_cdev_allocate_iso_resource _0d; |
| 1345 | struct fw_cdev_send_stream_packet _13; |
| 1346 | })]; |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 1347 | int ret; |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 1348 | |
| 1349 | if (_IOC_TYPE(cmd) != '#' || |
| 1350 | _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers)) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1351 | return -EINVAL; |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 1352 | |
| 1353 | if (_IOC_DIR(cmd) & _IOC_WRITE) { |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 1354 | if (_IOC_SIZE(cmd) > sizeof(buffer) || |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 1355 | copy_from_user(buffer, arg, _IOC_SIZE(cmd))) |
| 1356 | return -EFAULT; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1357 | } |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 1358 | |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 1359 | ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer); |
| 1360 | if (ret < 0) |
| 1361 | return ret; |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 1362 | |
| 1363 | if (_IOC_DIR(cmd) & _IOC_READ) { |
Kristian Høgsberg | 2d826cc | 2007-05-09 19:23:14 -0400 | [diff] [blame] | 1364 | if (_IOC_SIZE(cmd) > sizeof(buffer) || |
Kristian Høgsberg | 4f25922 | 2007-04-30 15:03:13 -0400 | [diff] [blame] | 1365 | copy_to_user(arg, buffer, _IOC_SIZE(cmd))) |
| 1366 | return -EFAULT; |
| 1367 | } |
| 1368 | |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 1369 | return ret; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1370 | } |
| 1371 | |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 1372 | static long fw_device_op_ioctl(struct file *file, |
| 1373 | unsigned int cmd, unsigned long arg) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1374 | { |
| 1375 | struct client *client = file->private_data; |
| 1376 | |
Jay Fenlason | 551f4cb | 2008-05-16 11:15:23 -0400 | [diff] [blame] | 1377 | if (fw_device_is_shutdown(client->device)) |
| 1378 | return -ENODEV; |
| 1379 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1380 | return dispatch_ioctl(client, cmd, (void __user *) arg); |
| 1381 | } |
| 1382 | |
| 1383 | #ifdef CONFIG_COMPAT |
Stefan Richter | 53dca51 | 2008-12-14 21:47:04 +0100 | [diff] [blame] | 1384 | static long fw_device_op_compat_ioctl(struct file *file, |
| 1385 | unsigned int cmd, unsigned long arg) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1386 | { |
| 1387 | struct client *client = file->private_data; |
| 1388 | |
Jay Fenlason | 551f4cb | 2008-05-16 11:15:23 -0400 | [diff] [blame] | 1389 | if (fw_device_is_shutdown(client->device)) |
| 1390 | return -ENODEV; |
| 1391 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1392 | return dispatch_ioctl(client, cmd, compat_ptr(arg)); |
| 1393 | } |
| 1394 | #endif |
| 1395 | |
| 1396 | static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma) |
| 1397 | { |
| 1398 | struct client *client = file->private_data; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 1399 | enum dma_data_direction direction; |
| 1400 | unsigned long size; |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 1401 | int page_count, ret; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1402 | |
Jay Fenlason | 551f4cb | 2008-05-16 11:15:23 -0400 | [diff] [blame] | 1403 | if (fw_device_is_shutdown(client->device)) |
| 1404 | return -ENODEV; |
| 1405 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 1406 | /* FIXME: We could support multiple buffers, but we don't. */ |
| 1407 | if (client->buffer.pages != NULL) |
| 1408 | return -EBUSY; |
| 1409 | |
| 1410 | if (!(vma->vm_flags & VM_SHARED)) |
| 1411 | return -EINVAL; |
| 1412 | |
| 1413 | if (vma->vm_start & ~PAGE_MASK) |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1414 | return -EINVAL; |
| 1415 | |
| 1416 | client->vm_start = vma->vm_start; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 1417 | size = vma->vm_end - vma->vm_start; |
| 1418 | page_count = size >> PAGE_SHIFT; |
| 1419 | if (size & ~PAGE_MASK) |
| 1420 | return -EINVAL; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1421 | |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 1422 | if (vma->vm_flags & VM_WRITE) |
| 1423 | direction = DMA_TO_DEVICE; |
| 1424 | else |
| 1425 | direction = DMA_FROM_DEVICE; |
| 1426 | |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 1427 | ret = fw_iso_buffer_init(&client->buffer, client->device->card, |
| 1428 | page_count, direction); |
| 1429 | if (ret < 0) |
| 1430 | return ret; |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 1431 | |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 1432 | ret = fw_iso_buffer_map(&client->buffer, vma); |
| 1433 | if (ret < 0) |
Kristian Høgsberg | 9aad812 | 2007-02-16 17:34:38 -0500 | [diff] [blame] | 1434 | fw_iso_buffer_destroy(&client->buffer, client->device->card); |
| 1435 | |
Stefan Richter | 2dbd7d7 | 2008-12-14 21:45:45 +0100 | [diff] [blame] | 1436 | return ret; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1437 | } |
| 1438 | |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 1439 | static int shutdown_resource(int id, void *p, void *data) |
| 1440 | { |
Stefan Richter | e21fcf7 | 2009-10-08 00:41:38 +0200 | [diff] [blame] | 1441 | struct client_resource *resource = p; |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 1442 | struct client *client = data; |
| 1443 | |
Stefan Richter | e21fcf7 | 2009-10-08 00:41:38 +0200 | [diff] [blame] | 1444 | resource->release(client, resource); |
Stefan Richter | fb44303 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1445 | client_put(client); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 1446 | |
| 1447 | return 0; |
| 1448 | } |
| 1449 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1450 | static int fw_device_op_release(struct inode *inode, struct file *file) |
| 1451 | { |
| 1452 | struct client *client = file->private_data; |
Stefan Richter | e21fcf7 | 2009-10-08 00:41:38 +0200 | [diff] [blame] | 1453 | struct event *event, *next_event; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1454 | |
Stefan Richter | 97811e3 | 2008-12-14 19:19:23 +0100 | [diff] [blame] | 1455 | mutex_lock(&client->device->client_list_mutex); |
| 1456 | list_del(&client->link); |
| 1457 | mutex_unlock(&client->device->client_list_mutex); |
| 1458 | |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1459 | if (client->iso_context) |
| 1460 | fw_iso_context_destroy(client->iso_context); |
| 1461 | |
Stefan Richter | 36a755c | 2009-01-05 20:28:10 +0100 | [diff] [blame] | 1462 | if (client->buffer.pages) |
| 1463 | fw_iso_buffer_destroy(&client->buffer, client->device->card); |
| 1464 | |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 1465 | /* Freeze client->resource_idr and client->event_list */ |
Stefan Richter | 3ba9498 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1466 | spin_lock_irq(&client->lock); |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 1467 | client->in_shutdown = true; |
Stefan Richter | 3ba9498 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1468 | spin_unlock_irq(&client->lock); |
Kristian Høgsberg | 66dea3e | 2007-03-28 21:26:42 +0200 | [diff] [blame] | 1469 | |
Jay Fenlason | 45ee319 | 2008-12-21 16:47:17 +0100 | [diff] [blame] | 1470 | idr_for_each(&client->resource_idr, shutdown_resource, client); |
| 1471 | idr_remove_all(&client->resource_idr); |
| 1472 | idr_destroy(&client->resource_idr); |
Kristian Høgsberg | 28cf6a0 | 2007-03-07 12:12:50 -0500 | [diff] [blame] | 1473 | |
Stefan Richter | e21fcf7 | 2009-10-08 00:41:38 +0200 | [diff] [blame] | 1474 | list_for_each_entry_safe(event, next_event, &client->event_list, link) |
| 1475 | kfree(event); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1476 | |
Stefan Richter | fb44303 | 2009-01-04 16:23:29 +0100 | [diff] [blame] | 1477 | client_put(client); |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1478 | |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
| 1482 | static unsigned int fw_device_op_poll(struct file *file, poll_table * pt) |
| 1483 | { |
| 1484 | struct client *client = file->private_data; |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 1485 | unsigned int mask = 0; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1486 | |
| 1487 | poll_wait(file, &client->wait, pt); |
| 1488 | |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 1489 | if (fw_device_is_shutdown(client->device)) |
| 1490 | mask |= POLLHUP | POLLERR; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1491 | if (!list_empty(&client->event_list)) |
Kristian Høgsberg | 2603bf2 | 2007-03-07 12:12:48 -0500 | [diff] [blame] | 1492 | mask |= POLLIN | POLLRDNORM; |
| 1493 | |
| 1494 | return mask; |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1495 | } |
| 1496 | |
Stefan Richter | 21ebcd1 | 2007-01-14 15:29:07 +0100 | [diff] [blame] | 1497 | const struct file_operations fw_device_ops = { |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1498 | .owner = THIS_MODULE, |
| 1499 | .open = fw_device_op_open, |
| 1500 | .read = fw_device_op_read, |
| 1501 | .unlocked_ioctl = fw_device_op_ioctl, |
| 1502 | .poll = fw_device_op_poll, |
| 1503 | .release = fw_device_op_release, |
| 1504 | .mmap = fw_device_op_mmap, |
| 1505 | |
| 1506 | #ifdef CONFIG_COMPAT |
Stefan Richter | 5af4e5e | 2007-01-21 20:45:32 +0100 | [diff] [blame] | 1507 | .compat_ioctl = fw_device_op_compat_ioctl, |
Kristian Høgsberg | 19a15b9 | 2006-12-19 19:58:31 -0500 | [diff] [blame] | 1508 | #endif |
| 1509 | }; |