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