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