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