blob: 81362c14ef44ed7a22ee74e126256947400250ab [file] [log] [blame]
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001/*
2 * Char device for device raw access
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05003 *
Kristian Høgsbergc781c062007-05-07 20:33:32 -04004 * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05005 *
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>
Stefan Richterfb443032009-01-04 16:23:29 +010023#include <linux/kref.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050024#include <linux/wait.h>
25#include <linux/errno.h>
26#include <linux/device.h>
27#include <linux/vmalloc.h>
Stefan Richterd67cfb92008-10-05 10:37:11 +020028#include <linux/mutex.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050029#include <linux/poll.h>
Stefan Richtera64408b2007-09-29 10:41:58 +020030#include <linux/preempt.h>
31#include <linux/time.h>
Jay Fenlasoncf417e542008-10-03 11:19:09 -040032#include <linux/spinlock.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050033#include <linux/delay.h>
34#include <linux/mm.h>
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -050035#include <linux/idr.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050036#include <linux/compat.h>
Kristian Høgsberg9640d3d2007-04-30 15:03:15 -040037#include <linux/firewire-cdev.h>
Stefan Richtera64408b2007-09-29 10:41:58 +020038#include <asm/system.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050039#include <asm/uaccess.h>
40#include "fw-transaction.h"
41#include "fw-topology.h"
42#include "fw-device.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050043
Kristian Høgsberg3964a442007-03-27 01:43:41 -040044struct client;
Jay Fenlason45ee3192008-12-21 16:47:17 +010045struct client_resource;
46typedef void (*client_resource_release_fn_t)(struct client *,
47 struct client_resource *);
Kristian Høgsberg3964a442007-03-27 01:43:41 -040048struct client_resource {
Jay Fenlason45ee3192008-12-21 16:47:17 +010049 client_resource_release_fn_t release;
50 int handle;
Kristian Høgsberg3964a442007-03-27 01:43:41 -040051};
52
Kristian Høgsbergc781c062007-05-07 20:33:32 -040053/*
54 * dequeue_event() just kfree()'s the event, so the event has to be
55 * the first field in the struct.
56 */
57
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050058struct event {
59 struct { void *data; size_t size; } v[2];
60 struct list_head link;
61};
62
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050063struct bus_reset {
64 struct event event;
65 struct fw_cdev_event_bus_reset reset;
66};
67
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050068struct response {
69 struct event event;
70 struct fw_transaction transaction;
71 struct client *client;
Kristian Høgsberg3964a442007-03-27 01:43:41 -040072 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050073 struct fw_cdev_event_response response;
74};
75
76struct iso_interrupt {
77 struct event event;
78 struct fw_cdev_event_iso_interrupt interrupt;
79};
80
81struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050082 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050083 struct fw_device *device;
Jay Fenlason45ee3192008-12-21 16:47:17 +010084
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050085 spinlock_t lock;
Jay Fenlason45ee3192008-12-21 16:47:17 +010086 bool in_shutdown;
87 struct idr resource_idr;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050088 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050089 wait_queue_head_t wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040090 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050091
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050092 struct fw_iso_context *iso_context;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -040093 u64 iso_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050094 struct fw_iso_buffer buffer;
95 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050096
97 struct list_head link;
Stefan Richterfb443032009-01-04 16:23:29 +010098 struct kref kref;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050099};
100
Stefan Richterfb443032009-01-04 16:23:29 +0100101static inline void client_get(struct client *client)
102{
103 kref_get(&client->kref);
104}
105
106static void client_release(struct kref *kref)
107{
108 struct client *client = container_of(kref, struct client, kref);
109
110 fw_device_put(client->device);
111 kfree(client);
112}
113
114static void client_put(struct client *client)
115{
116 kref_put(&client->kref, client_release);
117}
118
Stefan Richter53dca512008-12-14 21:47:04 +0100119static inline void __user *u64_to_uptr(__u64 value)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500120{
121 return (void __user *)(unsigned long)value;
122}
123
Stefan Richter53dca512008-12-14 21:47:04 +0100124static inline __u64 uptr_to_u64(void __user *ptr)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500125{
126 return (__u64)(unsigned long)ptr;
127}
128
129static int fw_device_op_open(struct inode *inode, struct file *file)
130{
131 struct fw_device *device;
132 struct client *client;
133
Stefan Richter96b19062008-02-02 15:01:09 +0100134 device = fw_device_get_by_devt(inode->i_rdev);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500135 if (device == NULL)
136 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500137
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400138 if (fw_device_is_shutdown(device)) {
139 fw_device_put(device);
140 return -ENODEV;
141 }
142
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400143 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100144 if (client == NULL) {
145 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500146 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100147 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500148
Stefan Richter96b19062008-02-02 15:01:09 +0100149 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500150 spin_lock_init(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100151 idr_init(&client->resource_idr);
152 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500153 init_waitqueue_head(&client->wait);
Stefan Richterfb443032009-01-04 16:23:29 +0100154 kref_init(&client->kref);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500155
156 file->private_data = client;
157
Stefan Richterd67cfb92008-10-05 10:37:11 +0200158 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500159 list_add_tail(&client->link, &device->client_list);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200160 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500161
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500162 return 0;
163}
164
165static void queue_event(struct client *client, struct event *event,
166 void *data0, size_t size0, void *data1, size_t size1)
167{
168 unsigned long flags;
169
170 event->v[0].data = data0;
171 event->v[0].size = size0;
172 event->v[1].data = data1;
173 event->v[1].size = size1;
174
175 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100176 if (client->in_shutdown)
177 kfree(event);
178 else
179 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500180 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400181
182 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500183}
184
Stefan Richter53dca512008-12-14 21:47:04 +0100185static int dequeue_event(struct client *client,
186 char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500187{
188 unsigned long flags;
189 struct event *event;
190 size_t size, total;
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100191 int i, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500192
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100193 ret = wait_event_interruptible(client->wait,
194 !list_empty(&client->event_list) ||
195 fw_device_is_shutdown(client->device));
196 if (ret < 0)
197 return ret;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500198
199 if (list_empty(&client->event_list) &&
200 fw_device_is_shutdown(client->device))
201 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500202
203 spin_lock_irqsave(&client->lock, flags);
Stefan Richtera459b8a2008-12-21 16:49:57 +0100204 event = list_first_entry(&client->event_list, struct event, link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500205 list_del(&event->link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500206 spin_unlock_irqrestore(&client->lock, flags);
207
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500208 total = 0;
209 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
210 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500211 if (copy_to_user(buffer + total, event->v[i].data, size)) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100212 ret = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500213 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500214 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500215 total += size;
216 }
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100217 ret = total;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500218
219 out:
220 kfree(event);
221
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100222 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500223}
224
Stefan Richter53dca512008-12-14 21:47:04 +0100225static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
226 size_t count, loff_t *offset)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500227{
228 struct client *client = file->private_data;
229
230 return dequeue_event(client, buffer, count);
231}
232
Stefan Richter53dca512008-12-14 21:47:04 +0100233static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
234 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500235{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400236 struct fw_card *card = client->device->card;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400237 unsigned long flags;
238
239 spin_lock_irqsave(&card->lock, flags);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500240
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400241 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500242 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100243 event->generation = client->device->generation;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400244 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500245 event->local_node_id = card->local_node->node_id;
246 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
247 event->irm_node_id = card->irm_node->node_id;
248 event->root_node_id = card->root_node->node_id;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400249
250 spin_unlock_irqrestore(&card->lock, flags);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500251}
252
Stefan Richter53dca512008-12-14 21:47:04 +0100253static void for_each_client(struct fw_device *device,
254 void (*callback)(struct client *client))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500255{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500256 struct client *c;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500257
Stefan Richterd67cfb92008-10-05 10:37:11 +0200258 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500259 list_for_each_entry(c, &device->client_list, link)
260 callback(c);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200261 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500262}
263
Stefan Richter53dca512008-12-14 21:47:04 +0100264static void queue_bus_reset_event(struct client *client)
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500265{
266 struct bus_reset *bus_reset;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500267
Stefan Richterd67cfb92008-10-05 10:37:11 +0200268 bus_reset = kzalloc(sizeof(*bus_reset), GFP_KERNEL);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500269 if (bus_reset == NULL) {
270 fw_notify("Out of memory when allocating bus reset event\n");
271 return;
272 }
273
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400274 fill_bus_reset_event(&bus_reset->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500275
276 queue_event(client, &bus_reset->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400277 &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500278}
279
280void fw_device_cdev_update(struct fw_device *device)
281{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500282 for_each_client(device, queue_bus_reset_event);
283}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500284
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500285static void wake_up_client(struct client *client)
286{
287 wake_up_interruptible(&client->wait);
288}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500289
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500290void fw_device_cdev_remove(struct fw_device *device)
291{
292 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500293}
294
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400295static int ioctl_get_info(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500296{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400297 struct fw_cdev_get_info *get_info = buffer;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500298 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100299 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500300
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400301 client->version = get_info->version;
302 get_info->version = FW_CDEV_VERSION;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400303 get_info->card = client->device->card->index;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500304
Stefan Richterc9755e12008-03-24 20:54:28 +0100305 down_read(&fw_device_rwsem);
306
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400307 if (get_info->rom != 0) {
308 void __user *uptr = u64_to_uptr(get_info->rom);
309 size_t want = get_info->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100310 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500311
Stefan Richterc9755e12008-03-24 20:54:28 +0100312 ret = copy_to_user(uptr, client->device->config_rom,
313 min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500314 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400315 get_info->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500316
Stefan Richterc9755e12008-03-24 20:54:28 +0100317 up_read(&fw_device_rwsem);
318
319 if (ret != 0)
320 return -EFAULT;
321
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400322 client->bus_reset_closure = get_info->bus_reset_closure;
323 if (get_info->bus_reset != 0) {
324 void __user *uptr = u64_to_uptr(get_info->bus_reset);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500325
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400326 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400327 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500328 return -EFAULT;
329 }
330
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500331 return 0;
332}
333
Stefan Richter53dca512008-12-14 21:47:04 +0100334static int add_client_resource(struct client *client,
335 struct client_resource *resource, gfp_t gfp_mask)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400336{
337 unsigned long flags;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100338 int ret;
339
340 retry:
341 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
342 return -ENOMEM;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400343
344 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100345 if (client->in_shutdown)
346 ret = -ECANCELED;
347 else
348 ret = idr_get_new(&client->resource_idr, resource,
349 &resource->handle);
Stefan Richterfb443032009-01-04 16:23:29 +0100350 if (ret >= 0)
351 client_get(client);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400352 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100353
354 if (ret == -EAGAIN)
355 goto retry;
356
357 return ret < 0 ? ret : 0;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400358}
359
Stefan Richter53dca512008-12-14 21:47:04 +0100360static int release_client_resource(struct client *client, u32 handle,
361 client_resource_release_fn_t release,
362 struct client_resource **resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400363{
364 struct client_resource *r;
365 unsigned long flags;
366
367 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100368 if (client->in_shutdown)
369 r = NULL;
370 else
371 r = idr_find(&client->resource_idr, handle);
372 if (r && r->release == release)
373 idr_remove(&client->resource_idr, handle);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400374 spin_unlock_irqrestore(&client->lock, flags);
375
Jay Fenlason45ee3192008-12-21 16:47:17 +0100376 if (!(r && r->release == release))
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400377 return -EINVAL;
378
379 if (resource)
380 *resource = r;
381 else
382 r->release(client, r);
383
Stefan Richterfb443032009-01-04 16:23:29 +0100384 client_put(client);
385
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400386 return 0;
387}
388
Stefan Richter53dca512008-12-14 21:47:04 +0100389static void release_transaction(struct client *client,
390 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400391{
392 struct response *response =
393 container_of(resource, struct response, resource);
394
395 fw_cancel_transaction(client->device->card, &response->transaction);
396}
397
Stefan Richter53dca512008-12-14 21:47:04 +0100398static void complete_transaction(struct fw_card *card, int rcode,
399 void *payload, size_t length, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500400{
401 struct response *response = data;
402 struct client *client = response->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500403 unsigned long flags;
David Moore8401d922008-07-29 23:46:25 -0700404 struct fw_cdev_event_response *r = &response->response;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500405
David Moore8401d922008-07-29 23:46:25 -0700406 if (length < r->length)
407 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500408 if (rcode == RCODE_COMPLETE)
David Moore8401d922008-07-29 23:46:25 -0700409 memcpy(r->data, payload, r->length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500410
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500411 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100412 /*
Stefan Richterfb443032009-01-04 16:23:29 +0100413 * 1. If called while in shutdown, the idr tree must be left untouched.
414 * The idr handle will be removed and the client reference will be
415 * dropped later.
416 * 2. If the call chain was release_client_resource ->
417 * release_transaction -> complete_transaction (instead of a normal
418 * conclusion of the transaction), i.e. if this resource was already
419 * unregistered from the idr, the client reference will be dropped
420 * by release_client_resource and we must not drop it here.
Jay Fenlason45ee3192008-12-21 16:47:17 +0100421 */
Stefan Richterfb443032009-01-04 16:23:29 +0100422 if (!client->in_shutdown &&
423 idr_find(&client->resource_idr, response->resource.handle)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100424 idr_remove(&client->resource_idr, response->resource.handle);
Stefan Richterfb443032009-01-04 16:23:29 +0100425 /* Drop the idr's reference */
426 client_put(client);
427 }
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500428 spin_unlock_irqrestore(&client->lock, flags);
429
David Moore8401d922008-07-29 23:46:25 -0700430 r->type = FW_CDEV_EVENT_RESPONSE;
431 r->rcode = rcode;
432
433 /*
434 * In the case that sizeof(*r) doesn't align with the position of the
435 * data, and the read is short, preserve an extra copy of the data
436 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
437 * for short reads and some apps depended on it, this is both safe
438 * and prudent for compatibility.
439 */
440 if (r->length <= sizeof(*r) - offsetof(typeof(*r), data))
441 queue_event(client, &response->event, r, sizeof(*r),
442 r->data, r->length);
443 else
444 queue_event(client, &response->event, r, sizeof(*r) + r->length,
445 NULL, 0);
Stefan Richterfb443032009-01-04 16:23:29 +0100446
447 /* Drop the transaction callback's reference */
448 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500449}
450
Jeff Garzik350958f2007-05-27 07:09:18 -0400451static int ioctl_send_request(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500452{
453 struct fw_device *device = client->device;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400454 struct fw_cdev_send_request *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500455 struct response *response;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100456 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500457
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500458 /* What is the biggest size we'll accept, really? */
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400459 if (request->length > 4096)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500460 return -EINVAL;
461
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400462 response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500463 if (response == NULL)
464 return -ENOMEM;
465
466 response->client = client;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400467 response->response.length = request->length;
468 response->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500469
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400470 if (request->data &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500471 copy_from_user(response->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400472 u64_to_uptr(request->data), request->length)) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100473 ret = -EFAULT;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100474 goto failed;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100475 }
476
477 switch (request->tcode) {
478 case TCODE_WRITE_QUADLET_REQUEST:
479 case TCODE_WRITE_BLOCK_REQUEST:
480 case TCODE_READ_QUADLET_REQUEST:
481 case TCODE_READ_BLOCK_REQUEST:
482 case TCODE_LOCK_MASK_SWAP:
483 case TCODE_LOCK_COMPARE_SWAP:
484 case TCODE_LOCK_FETCH_ADD:
485 case TCODE_LOCK_LITTLE_ADD:
486 case TCODE_LOCK_BOUNDED_ADD:
487 case TCODE_LOCK_WRAP_ADD:
488 case TCODE_LOCK_VENDOR_DEPENDENT:
489 break;
490 default:
491 ret = -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100492 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500493 }
494
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400495 response->resource.release = release_transaction;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100496 ret = add_client_resource(client, &response->resource, GFP_KERNEL);
497 if (ret < 0)
498 goto failed;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500499
Stefan Richterfb443032009-01-04 16:23:29 +0100500 /* Get a reference for the transaction callback */
501 client_get(client);
502
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500503 fw_send_request(device->card, &response->transaction,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400504 request->tcode & 0x1f,
Stefan Richter907293d2007-01-23 21:11:43 +0100505 device->node->node_id,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400506 request->generation,
Stefan Richterf1397492007-06-10 21:31:36 +0200507 device->max_speed,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400508 request->offset,
509 response->response.data, request->length,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500510 complete_transaction, response);
511
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400512 if (request->data)
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400513 return sizeof(request) + request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500514 else
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400515 return sizeof(request);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100516 failed:
Stefan Richter1f3125a2008-12-05 22:44:42 +0100517 kfree(response);
518
519 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500520}
521
522struct address_handler {
523 struct fw_address_handler handler;
524 __u64 closure;
525 struct client *client;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400526 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500527};
528
529struct request {
530 struct fw_request *request;
531 void *data;
532 size_t length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400533 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500534};
535
536struct request_event {
537 struct event event;
538 struct fw_cdev_event_request request;
539};
540
Stefan Richter53dca512008-12-14 21:47:04 +0100541static void release_request(struct client *client,
542 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400543{
544 struct request *request =
545 container_of(resource, struct request, resource);
546
547 fw_send_response(client->device->card, request->request,
548 RCODE_CONFLICT_ERROR);
549 kfree(request);
550}
551
Stefan Richter53dca512008-12-14 21:47:04 +0100552static void handle_request(struct fw_card *card, struct fw_request *r,
553 int tcode, int destination, int source,
554 int generation, int speed,
555 unsigned long long offset,
556 void *payload, size_t length, void *callback_data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500557{
558 struct address_handler *handler = callback_data;
559 struct request *request;
560 struct request_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500561 struct client *client = handler->client;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100562 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500563
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400564 request = kmalloc(sizeof(*request), GFP_ATOMIC);
565 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100566 if (request == NULL || e == NULL)
567 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500568
569 request->request = r;
570 request->data = payload;
571 request->length = length;
572
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400573 request->resource.release = release_request;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100574 ret = add_client_resource(client, &request->resource, GFP_ATOMIC);
575 if (ret < 0)
576 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500577
578 e->request.type = FW_CDEV_EVENT_REQUEST;
579 e->request.tcode = tcode;
580 e->request.offset = offset;
581 e->request.length = length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400582 e->request.handle = request->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500583 e->request.closure = handler->closure;
584
585 queue_event(client, &e->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400586 &e->request, sizeof(e->request), payload, length);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100587 return;
588
589 failed:
590 kfree(request);
591 kfree(e);
592 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500593}
594
Stefan Richter53dca512008-12-14 21:47:04 +0100595static void release_address_handler(struct client *client,
596 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400597{
598 struct address_handler *handler =
599 container_of(resource, struct address_handler, resource);
600
601 fw_core_remove_address_handler(&handler->handler);
602 kfree(handler);
603}
604
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400605static int ioctl_allocate(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500606{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400607 struct fw_cdev_allocate *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500608 struct address_handler *handler;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500609 struct fw_address_region region;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100610 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500611
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400612 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500613 if (handler == NULL)
614 return -ENOMEM;
615
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400616 region.start = request->offset;
617 region.end = request->offset + request->length;
618 handler->handler.length = request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500619 handler->handler.address_callback = handle_request;
620 handler->handler.callback_data = handler;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400621 handler->closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500622 handler->client = client;
623
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100624 ret = fw_core_add_address_handler(&handler->handler, &region);
625 if (ret < 0) {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500626 kfree(handler);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100627 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500628 }
629
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400630 handler->resource.release = release_address_handler;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100631 ret = add_client_resource(client, &handler->resource, GFP_KERNEL);
632 if (ret < 0) {
633 release_address_handler(client, &handler->resource);
634 return ret;
635 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400636 request->handle = handler->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500637
638 return 0;
639}
640
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400641static int ioctl_deallocate(struct client *client, void *buffer)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400642{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400643 struct fw_cdev_deallocate *request = buffer;
Kristian Høgsberg94723162007-03-14 17:34:55 -0400644
Jay Fenlason45ee3192008-12-21 16:47:17 +0100645 return release_client_resource(client, request->handle,
646 release_address_handler, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400647}
648
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400649static int ioctl_send_response(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500650{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400651 struct fw_cdev_send_response *request = buffer;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400652 struct client_resource *resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500653 struct request *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500654
Jay Fenlason45ee3192008-12-21 16:47:17 +0100655 if (release_client_resource(client, request->handle,
656 release_request, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500657 return -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100658
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400659 r = container_of(resource, struct request, resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400660 if (request->length < r->length)
661 r->length = request->length;
662 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500663 return -EFAULT;
664
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400665 fw_send_response(client->device->card, r->request, request->rcode);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500666 kfree(r);
667
668 return 0;
669}
670
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400671static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500672{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400673 struct fw_cdev_initiate_bus_reset *request = buffer;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500674 int short_reset;
675
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400676 short_reset = (request->type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500677
678 return fw_core_initiate_bus_reset(client->device->card, short_reset);
679}
680
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200681struct descriptor {
682 struct fw_descriptor d;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400683 struct client_resource resource;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200684 u32 data[0];
685};
686
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400687static void release_descriptor(struct client *client,
688 struct client_resource *resource)
689{
690 struct descriptor *descriptor =
691 container_of(resource, struct descriptor, resource);
692
693 fw_core_remove_descriptor(&descriptor->d);
694 kfree(descriptor);
695}
696
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400697static int ioctl_add_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200698{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400699 struct fw_cdev_add_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200700 struct descriptor *descriptor;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100701 int ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200702
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400703 if (request->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200704 return -EINVAL;
705
706 descriptor =
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400707 kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200708 if (descriptor == NULL)
709 return -ENOMEM;
710
711 if (copy_from_user(descriptor->data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400712 u64_to_uptr(request->data), request->length * 4)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100713 ret = -EFAULT;
714 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200715 }
716
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400717 descriptor->d.length = request->length;
718 descriptor->d.immediate = request->immediate;
719 descriptor->d.key = request->key;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200720 descriptor->d.data = descriptor->data;
721
Jay Fenlason45ee3192008-12-21 16:47:17 +0100722 ret = fw_core_add_descriptor(&descriptor->d);
723 if (ret < 0)
724 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200725
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400726 descriptor->resource.release = release_descriptor;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100727 ret = add_client_resource(client, &descriptor->resource, GFP_KERNEL);
728 if (ret < 0) {
729 fw_core_remove_descriptor(&descriptor->d);
730 goto failed;
731 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400732 request->handle = descriptor->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200733
734 return 0;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100735 failed:
736 kfree(descriptor);
737
738 return ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200739}
740
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400741static int ioctl_remove_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200742{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400743 struct fw_cdev_remove_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200744
Jay Fenlason45ee3192008-12-21 16:47:17 +0100745 return release_client_resource(client, request->handle,
746 release_descriptor, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200747}
748
Stefan Richter53dca512008-12-14 21:47:04 +0100749static void iso_callback(struct fw_iso_context *context, u32 cycle,
750 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500751{
752 struct client *client = data;
Stefan Richter930e4b72007-08-03 20:56:31 +0200753 struct iso_interrupt *irq;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500754
Stefan Richter930e4b72007-08-03 20:56:31 +0200755 irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
756 if (irq == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500757 return;
758
Stefan Richter930e4b72007-08-03 20:56:31 +0200759 irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
760 irq->interrupt.closure = client->iso_closure;
761 irq->interrupt.cycle = cycle;
762 irq->interrupt.header_length = header_length;
763 memcpy(irq->interrupt.header, header, header_length);
764 queue_event(client, &irq->event, &irq->interrupt,
765 sizeof(irq->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500766}
767
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400768static int ioctl_create_iso_context(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500769{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400770 struct fw_cdev_create_iso_context *request = buffer;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400771 struct fw_iso_context *context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500772
Stefan Richterfae60312008-02-20 21:10:06 +0100773 /* We only support one context at this time. */
774 if (client->iso_context != NULL)
775 return -EBUSY;
776
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400777 if (request->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500778 return -EINVAL;
779
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400780 switch (request->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400781 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400782 if (request->header_size < 4 || (request->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400783 return -EINVAL;
784
785 break;
786
787 case FW_ISO_CONTEXT_TRANSMIT:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400788 if (request->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400789 return -EINVAL;
790
791 break;
792
793 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500794 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400795 }
796
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400797 context = fw_iso_context_create(client->device->card,
798 request->type,
799 request->channel,
800 request->speed,
801 request->header_size,
802 iso_callback, client);
803 if (IS_ERR(context))
804 return PTR_ERR(context);
805
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400806 client->iso_closure = request->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400807 client->iso_context = context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500808
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400809 /* We only support one context at this time. */
810 request->handle = 0;
811
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500812 return 0;
813}
814
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400815/* Macros for decoding the iso packet control header. */
816#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
817#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
818#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +0200819#define GET_TAG(v) (((v) >> 18) & 0x03)
820#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400821#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
822
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400823static int ioctl_queue_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500824{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400825 struct fw_cdev_queue_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500826 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500827 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200828 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400829 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500830 int count;
831 struct {
832 struct fw_iso_packet packet;
833 u8 header[256];
834 } u;
835
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400836 if (ctx == NULL || request->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500837 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500838
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400839 /*
840 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500841 * the iso buffer, and the pointer points inside the buffer,
842 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500843 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500844 * payload_length == 0 through. In other words, if no packets
845 * use the indirect payload, the iso buffer need not be mapped
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400846 * and the request->data pointer is ignored.
847 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500848
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400849 payload = (unsigned long)request->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200850 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400851 if (request->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200852 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500853 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200854 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500855 }
856
Al Viro1ccc9142007-10-14 19:34:40 +0100857 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
858
859 if (!access_ok(VERIFY_READ, p, request->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500860 return -EFAULT;
861
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400862 end = (void __user *)p + request->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500863 count = 0;
864 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400865 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500866 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400867 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
868 u.packet.interrupt = GET_INTERRUPT(control);
869 u.packet.skip = GET_SKIP(control);
870 u.packet.tag = GET_TAG(control);
871 u.packet.sy = GET_SY(control);
872 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500873
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500874 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500875 header_length = u.packet.header_length;
876 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400877 /*
878 * We require that header_length is a multiple of
879 * the fixed header size, ctx->header_size.
880 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500881 if (ctx->header_size == 0) {
882 if (u.packet.header_length > 0)
883 return -EINVAL;
884 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500885 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500886 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500887 header_length = 0;
888 }
889
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500890 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500891 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500892 if (next > end)
893 return -EINVAL;
894 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500895 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500896 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500897 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500898 u.packet.header_length + u.packet.payload_length > 0)
899 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200900 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500901 return -EINVAL;
902
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500903 if (fw_iso_context_queue(ctx, &u.packet,
904 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500905 break;
906
907 p = next;
908 payload += u.packet.payload_length;
909 count++;
910 }
911
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400912 request->size -= uptr_to_u64(p) - request->packets;
913 request->packets = uptr_to_u64(p);
914 request->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500915
916 return count;
917}
918
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400919static int ioctl_start_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500920{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400921 struct fw_cdev_start_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500922
Stefan Richterfae60312008-02-20 21:10:06 +0100923 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400924 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +0100925
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400926 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400927 if (request->tags == 0 || request->tags > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400928 return -EINVAL;
929
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400930 if (request->sync > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400931 return -EINVAL;
932 }
933
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400934 return fw_iso_context_start(client->iso_context, request->cycle,
935 request->sync, request->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500936}
937
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400938static int ioctl_stop_iso(struct client *client, void *buffer)
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500939{
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400940 struct fw_cdev_stop_iso *request = buffer;
941
Stefan Richterfae60312008-02-20 21:10:06 +0100942 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400943 return -EINVAL;
944
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500945 return fw_iso_context_stop(client->iso_context);
946}
947
Stefan Richtera64408b2007-09-29 10:41:58 +0200948static int ioctl_get_cycle_timer(struct client *client, void *buffer)
949{
950 struct fw_cdev_get_cycle_timer *request = buffer;
951 struct fw_card *card = client->device->card;
952 unsigned long long bus_time;
953 struct timeval tv;
954 unsigned long flags;
955
956 preempt_disable();
957 local_irq_save(flags);
958
959 bus_time = card->driver->get_bus_time(card);
960 do_gettimeofday(&tv);
961
962 local_irq_restore(flags);
963 preempt_enable();
964
965 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
966 request->cycle_timer = bus_time & 0xffffffff;
967 return 0;
968}
969
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400970static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
971 ioctl_get_info,
972 ioctl_send_request,
973 ioctl_allocate,
974 ioctl_deallocate,
975 ioctl_send_response,
976 ioctl_initiate_bus_reset,
977 ioctl_add_descriptor,
978 ioctl_remove_descriptor,
979 ioctl_create_iso_context,
980 ioctl_queue_iso,
981 ioctl_start_iso,
982 ioctl_stop_iso,
Stefan Richtera64408b2007-09-29 10:41:58 +0200983 ioctl_get_cycle_timer,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400984};
985
Stefan Richter53dca512008-12-14 21:47:04 +0100986static int dispatch_ioctl(struct client *client,
987 unsigned int cmd, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500988{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400989 char buffer[256];
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100990 int ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400991
992 if (_IOC_TYPE(cmd) != '#' ||
993 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500994 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400995
996 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400997 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400998 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
999 return -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001000 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001001
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001002 ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
1003 if (ret < 0)
1004 return ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001005
1006 if (_IOC_DIR(cmd) & _IOC_READ) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001007 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001008 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
1009 return -EFAULT;
1010 }
1011
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001012 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001013}
1014
Stefan Richter53dca512008-12-14 21:47:04 +01001015static long fw_device_op_ioctl(struct file *file,
1016 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001017{
1018 struct client *client = file->private_data;
1019
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001020 if (fw_device_is_shutdown(client->device))
1021 return -ENODEV;
1022
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001023 return dispatch_ioctl(client, cmd, (void __user *) arg);
1024}
1025
1026#ifdef CONFIG_COMPAT
Stefan Richter53dca512008-12-14 21:47:04 +01001027static long fw_device_op_compat_ioctl(struct file *file,
1028 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001029{
1030 struct client *client = file->private_data;
1031
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001032 if (fw_device_is_shutdown(client->device))
1033 return -ENODEV;
1034
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001035 return dispatch_ioctl(client, cmd, compat_ptr(arg));
1036}
1037#endif
1038
1039static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1040{
1041 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001042 enum dma_data_direction direction;
1043 unsigned long size;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001044 int page_count, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001045
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001046 if (fw_device_is_shutdown(client->device))
1047 return -ENODEV;
1048
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001049 /* FIXME: We could support multiple buffers, but we don't. */
1050 if (client->buffer.pages != NULL)
1051 return -EBUSY;
1052
1053 if (!(vma->vm_flags & VM_SHARED))
1054 return -EINVAL;
1055
1056 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001057 return -EINVAL;
1058
1059 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001060 size = vma->vm_end - vma->vm_start;
1061 page_count = size >> PAGE_SHIFT;
1062 if (size & ~PAGE_MASK)
1063 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001064
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001065 if (vma->vm_flags & VM_WRITE)
1066 direction = DMA_TO_DEVICE;
1067 else
1068 direction = DMA_FROM_DEVICE;
1069
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001070 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1071 page_count, direction);
1072 if (ret < 0)
1073 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001074
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001075 ret = fw_iso_buffer_map(&client->buffer, vma);
1076 if (ret < 0)
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001077 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1078
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001079 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001080}
1081
Jay Fenlason45ee3192008-12-21 16:47:17 +01001082static int shutdown_resource(int id, void *p, void *data)
1083{
1084 struct client_resource *r = p;
1085 struct client *client = data;
1086
1087 r->release(client, r);
Stefan Richterfb443032009-01-04 16:23:29 +01001088 client_put(client);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001089
1090 return 0;
1091}
1092
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001093static int fw_device_op_release(struct inode *inode, struct file *file)
1094{
1095 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001096 struct event *e, *next_e;
Jay Fenlason45ee3192008-12-21 16:47:17 +01001097 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001098
Stefan Richter97811e32008-12-14 19:19:23 +01001099 mutex_lock(&client->device->client_list_mutex);
1100 list_del(&client->link);
1101 mutex_unlock(&client->device->client_list_mutex);
1102
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001103 if (client->buffer.pages)
1104 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1105
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001106 if (client->iso_context)
1107 fw_iso_context_destroy(client->iso_context);
1108
Jay Fenlason45ee3192008-12-21 16:47:17 +01001109 /* Freeze client->resource_idr and client->event_list */
1110 spin_lock_irqsave(&client->lock, flags);
1111 client->in_shutdown = true;
1112 spin_unlock_irqrestore(&client->lock, flags);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001113
Jay Fenlason45ee3192008-12-21 16:47:17 +01001114 idr_for_each(&client->resource_idr, shutdown_resource, client);
1115 idr_remove_all(&client->resource_idr);
1116 idr_destroy(&client->resource_idr);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001117
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001118 list_for_each_entry_safe(e, next_e, &client->event_list, link)
1119 kfree(e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001120
Stefan Richterfb443032009-01-04 16:23:29 +01001121 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001122
1123 return 0;
1124}
1125
1126static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1127{
1128 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001129 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001130
1131 poll_wait(file, &client->wait, pt);
1132
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001133 if (fw_device_is_shutdown(client->device))
1134 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001135 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001136 mask |= POLLIN | POLLRDNORM;
1137
1138 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001139}
1140
Stefan Richter21ebcd12007-01-14 15:29:07 +01001141const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001142 .owner = THIS_MODULE,
1143 .open = fw_device_op_open,
1144 .read = fw_device_op_read,
1145 .unlocked_ioctl = fw_device_op_ioctl,
1146 .poll = fw_device_op_poll,
1147 .release = fw_device_op_release,
1148 .mmap = fw_device_op_mmap,
1149
1150#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001151 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001152#endif
1153};