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