blob: 4c33b51b735a6150277939124cbbc417587529af [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
Stefan Richterbe5bbd62009-01-04 16:23:29 +010021#include <linux/compat.h>
22#include <linux/delay.h>
23#include <linux/device.h>
24#include <linux/errno.h>
25#include <linux/firewire-cdev.h>
26#include <linux/idr.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050027#include <linux/kernel.h>
Stefan Richterfb443032009-01-04 16:23:29 +010028#include <linux/kref.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010029#include <linux/mm.h>
30#include <linux/module.h>
Stefan Richterd67cfb92008-10-05 10:37:11 +020031#include <linux/mutex.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050032#include <linux/poll.h>
Stefan Richtera64408b2007-09-29 10:41:58 +020033#include <linux/preempt.h>
Jay Fenlasoncf417e542008-10-03 11:19:09 -040034#include <linux/spinlock.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010035#include <linux/time.h>
36#include <linux/vmalloc.h>
37#include <linux/wait.h>
38
Stefan Richtera64408b2007-09-29 10:41:58 +020039#include <asm/system.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050040#include <asm/uaccess.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010041
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050042#include "fw-device.h"
Stefan Richterbe5bbd62009-01-04 16:23:29 +010043#include "fw-topology.h"
44#include "fw-transaction.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050045
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050046struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050047 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050048 struct fw_device *device;
Jay Fenlason45ee3192008-12-21 16:47:17 +010049
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050050 spinlock_t lock;
Jay Fenlason45ee3192008-12-21 16:47:17 +010051 bool in_shutdown;
52 struct idr resource_idr;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050053 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050054 wait_queue_head_t wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040055 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050056
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050057 struct fw_iso_context *iso_context;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -040058 u64 iso_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050059 struct fw_iso_buffer buffer;
60 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050061
62 struct list_head link;
Stefan Richterfb443032009-01-04 16:23:29 +010063 struct kref kref;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050064};
65
Stefan Richterfb443032009-01-04 16:23:29 +010066static inline void client_get(struct client *client)
67{
68 kref_get(&client->kref);
69}
70
71static void client_release(struct kref *kref)
72{
73 struct client *client = container_of(kref, struct client, kref);
74
75 fw_device_put(client->device);
76 kfree(client);
77}
78
79static void client_put(struct client *client)
80{
81 kref_put(&client->kref, client_release);
82}
83
Stefan Richter97c18b72009-01-04 16:23:29 +010084struct client_resource;
85typedef void (*client_resource_release_fn_t)(struct client *,
86 struct client_resource *);
87struct client_resource {
88 client_resource_release_fn_t release;
89 int handle;
90};
91
92struct address_handler_resource {
93 struct client_resource resource;
94 struct fw_address_handler handler;
95 __u64 closure;
96 struct client *client;
97};
98
99struct outbound_transaction_resource {
100 struct client_resource resource;
101 struct fw_transaction transaction;
102};
103
104struct inbound_transaction_resource {
105 struct client_resource resource;
106 struct fw_request *request;
107 void *data;
108 size_t length;
109};
110
111struct descriptor_resource {
112 struct client_resource resource;
113 struct fw_descriptor descriptor;
114 u32 data[0];
115};
116
117/*
118 * dequeue_event() just kfree()'s the event, so the event has to be
119 * the first field in a struct XYZ_event.
120 */
121struct event {
122 struct { void *data; size_t size; } v[2];
123 struct list_head link;
124};
125
126struct bus_reset_event {
127 struct event event;
128 struct fw_cdev_event_bus_reset reset;
129};
130
131struct outbound_transaction_event {
132 struct event event;
133 struct client *client;
134 struct outbound_transaction_resource r;
135 struct fw_cdev_event_response response;
136};
137
138struct inbound_transaction_event {
139 struct event event;
140 struct fw_cdev_event_request request;
141};
142
143struct iso_interrupt_event {
144 struct event event;
145 struct fw_cdev_event_iso_interrupt interrupt;
146};
147
Stefan Richter53dca512008-12-14 21:47:04 +0100148static inline void __user *u64_to_uptr(__u64 value)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500149{
150 return (void __user *)(unsigned long)value;
151}
152
Stefan Richter53dca512008-12-14 21:47:04 +0100153static inline __u64 uptr_to_u64(void __user *ptr)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500154{
155 return (__u64)(unsigned long)ptr;
156}
157
158static int fw_device_op_open(struct inode *inode, struct file *file)
159{
160 struct fw_device *device;
161 struct client *client;
162
Stefan Richter96b19062008-02-02 15:01:09 +0100163 device = fw_device_get_by_devt(inode->i_rdev);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500164 if (device == NULL)
165 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500166
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400167 if (fw_device_is_shutdown(device)) {
168 fw_device_put(device);
169 return -ENODEV;
170 }
171
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400172 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100173 if (client == NULL) {
174 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500175 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100176 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500177
Stefan Richter96b19062008-02-02 15:01:09 +0100178 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500179 spin_lock_init(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100180 idr_init(&client->resource_idr);
181 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500182 init_waitqueue_head(&client->wait);
Stefan Richterfb443032009-01-04 16:23:29 +0100183 kref_init(&client->kref);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500184
185 file->private_data = client;
186
Stefan Richterd67cfb92008-10-05 10:37:11 +0200187 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500188 list_add_tail(&client->link, &device->client_list);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200189 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500190
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500191 return 0;
192}
193
194static void queue_event(struct client *client, struct event *event,
195 void *data0, size_t size0, void *data1, size_t size1)
196{
197 unsigned long flags;
198
199 event->v[0].data = data0;
200 event->v[0].size = size0;
201 event->v[1].data = data1;
202 event->v[1].size = size1;
203
204 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100205 if (client->in_shutdown)
206 kfree(event);
207 else
208 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500209 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400210
211 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500212}
213
Stefan Richter53dca512008-12-14 21:47:04 +0100214static int dequeue_event(struct client *client,
215 char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500216{
217 unsigned long flags;
218 struct event *event;
219 size_t size, total;
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100220 int i, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500221
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100222 ret = wait_event_interruptible(client->wait,
223 !list_empty(&client->event_list) ||
224 fw_device_is_shutdown(client->device));
225 if (ret < 0)
226 return ret;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500227
228 if (list_empty(&client->event_list) &&
229 fw_device_is_shutdown(client->device))
230 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500231
232 spin_lock_irqsave(&client->lock, flags);
Stefan Richtera459b8a2008-12-21 16:49:57 +0100233 event = list_first_entry(&client->event_list, struct event, link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500234 list_del(&event->link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500235 spin_unlock_irqrestore(&client->lock, flags);
236
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500237 total = 0;
238 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
239 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500240 if (copy_to_user(buffer + total, event->v[i].data, size)) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100241 ret = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500242 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500243 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500244 total += size;
245 }
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100246 ret = total;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500247
248 out:
249 kfree(event);
250
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100251 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500252}
253
Stefan Richter53dca512008-12-14 21:47:04 +0100254static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
255 size_t count, loff_t *offset)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500256{
257 struct client *client = file->private_data;
258
259 return dequeue_event(client, buffer, count);
260}
261
Stefan Richter53dca512008-12-14 21:47:04 +0100262static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
263 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500264{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400265 struct fw_card *card = client->device->card;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400266 unsigned long flags;
267
268 spin_lock_irqsave(&card->lock, flags);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500269
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400270 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500271 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100272 event->generation = client->device->generation;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400273 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500274 event->local_node_id = card->local_node->node_id;
275 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
276 event->irm_node_id = card->irm_node->node_id;
277 event->root_node_id = card->root_node->node_id;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400278
279 spin_unlock_irqrestore(&card->lock, flags);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500280}
281
Stefan Richter53dca512008-12-14 21:47:04 +0100282static void for_each_client(struct fw_device *device,
283 void (*callback)(struct client *client))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500284{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500285 struct client *c;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500286
Stefan Richterd67cfb92008-10-05 10:37:11 +0200287 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500288 list_for_each_entry(c, &device->client_list, link)
289 callback(c);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200290 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500291}
292
Stefan Richter53dca512008-12-14 21:47:04 +0100293static void queue_bus_reset_event(struct client *client)
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500294{
Stefan Richter97c18b72009-01-04 16:23:29 +0100295 struct bus_reset_event *e;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500296
Stefan Richter97c18b72009-01-04 16:23:29 +0100297 e = kzalloc(sizeof(*e), GFP_KERNEL);
298 if (e == NULL) {
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500299 fw_notify("Out of memory when allocating bus reset event\n");
300 return;
301 }
302
Stefan Richter97c18b72009-01-04 16:23:29 +0100303 fill_bus_reset_event(&e->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500304
Stefan Richter97c18b72009-01-04 16:23:29 +0100305 queue_event(client, &e->event,
306 &e->reset, sizeof(e->reset), NULL, 0);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500307}
308
309void fw_device_cdev_update(struct fw_device *device)
310{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500311 for_each_client(device, queue_bus_reset_event);
312}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500313
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500314static void wake_up_client(struct client *client)
315{
316 wake_up_interruptible(&client->wait);
317}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500318
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500319void fw_device_cdev_remove(struct fw_device *device)
320{
321 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500322}
323
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400324static int ioctl_get_info(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500325{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400326 struct fw_cdev_get_info *get_info = buffer;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500327 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100328 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500329
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400330 client->version = get_info->version;
331 get_info->version = FW_CDEV_VERSION;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400332 get_info->card = client->device->card->index;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500333
Stefan Richterc9755e12008-03-24 20:54:28 +0100334 down_read(&fw_device_rwsem);
335
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400336 if (get_info->rom != 0) {
337 void __user *uptr = u64_to_uptr(get_info->rom);
338 size_t want = get_info->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100339 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500340
Stefan Richterc9755e12008-03-24 20:54:28 +0100341 ret = copy_to_user(uptr, client->device->config_rom,
342 min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500343 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400344 get_info->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500345
Stefan Richterc9755e12008-03-24 20:54:28 +0100346 up_read(&fw_device_rwsem);
347
348 if (ret != 0)
349 return -EFAULT;
350
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400351 client->bus_reset_closure = get_info->bus_reset_closure;
352 if (get_info->bus_reset != 0) {
353 void __user *uptr = u64_to_uptr(get_info->bus_reset);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500354
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400355 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400356 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500357 return -EFAULT;
358 }
359
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500360 return 0;
361}
362
Stefan Richter53dca512008-12-14 21:47:04 +0100363static int add_client_resource(struct client *client,
364 struct client_resource *resource, gfp_t gfp_mask)
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400365{
366 unsigned long flags;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100367 int ret;
368
369 retry:
370 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
371 return -ENOMEM;
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400372
373 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100374 if (client->in_shutdown)
375 ret = -ECANCELED;
376 else
377 ret = idr_get_new(&client->resource_idr, resource,
378 &resource->handle);
Stefan Richterfb443032009-01-04 16:23:29 +0100379 if (ret >= 0)
380 client_get(client);
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400381 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100382
383 if (ret == -EAGAIN)
384 goto retry;
385
386 return ret < 0 ? ret : 0;
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400387}
388
Stefan Richter53dca512008-12-14 21:47:04 +0100389static int release_client_resource(struct client *client, u32 handle,
390 client_resource_release_fn_t release,
391 struct client_resource **resource)
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400392{
393 struct client_resource *r;
394 unsigned long flags;
395
396 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100397 if (client->in_shutdown)
398 r = NULL;
399 else
400 r = idr_find(&client->resource_idr, handle);
401 if (r && r->release == release)
402 idr_remove(&client->resource_idr, handle);
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400403 spin_unlock_irqrestore(&client->lock, flags);
404
Jay Fenlason45ee3192008-12-21 16:47:17 +0100405 if (!(r && r->release == release))
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400406 return -EINVAL;
407
408 if (resource)
409 *resource = r;
410 else
411 r->release(client, r);
412
Stefan Richterfb443032009-01-04 16:23:29 +0100413 client_put(client);
414
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400415 return 0;
416}
417
Stefan Richter53dca512008-12-14 21:47:04 +0100418static void release_transaction(struct client *client,
419 struct client_resource *resource)
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400420{
Stefan Richter97c18b72009-01-04 16:23:29 +0100421 struct outbound_transaction_resource *r = container_of(resource,
422 struct outbound_transaction_resource, resource);
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400423
Stefan Richter97c18b72009-01-04 16:23:29 +0100424 fw_cancel_transaction(client->device->card, &r->transaction);
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400425}
426
Stefan Richter53dca512008-12-14 21:47:04 +0100427static void complete_transaction(struct fw_card *card, int rcode,
428 void *payload, size_t length, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500429{
Stefan Richter97c18b72009-01-04 16:23:29 +0100430 struct outbound_transaction_event *e = data;
431 struct fw_cdev_event_response *rsp = &e->response;
432 struct client *client = e->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500433 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500434
Stefan Richter97c18b72009-01-04 16:23:29 +0100435 if (length < rsp->length)
436 rsp->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500437 if (rcode == RCODE_COMPLETE)
Stefan Richter97c18b72009-01-04 16:23:29 +0100438 memcpy(rsp->data, payload, rsp->length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500439
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500440 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100441 /*
Stefan Richterfb443032009-01-04 16:23:29 +0100442 * 1. If called while in shutdown, the idr tree must be left untouched.
443 * The idr handle will be removed and the client reference will be
444 * dropped later.
445 * 2. If the call chain was release_client_resource ->
446 * release_transaction -> complete_transaction (instead of a normal
447 * conclusion of the transaction), i.e. if this resource was already
448 * unregistered from the idr, the client reference will be dropped
449 * by release_client_resource and we must not drop it here.
Jay Fenlason45ee3192008-12-21 16:47:17 +0100450 */
Stefan Richterfb443032009-01-04 16:23:29 +0100451 if (!client->in_shutdown &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100452 idr_find(&client->resource_idr, e->r.resource.handle)) {
453 idr_remove(&client->resource_idr, e->r.resource.handle);
Stefan Richterfb443032009-01-04 16:23:29 +0100454 /* Drop the idr's reference */
455 client_put(client);
456 }
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500457 spin_unlock_irqrestore(&client->lock, flags);
458
Stefan Richter97c18b72009-01-04 16:23:29 +0100459 rsp->type = FW_CDEV_EVENT_RESPONSE;
460 rsp->rcode = rcode;
David Moore8401d922008-07-29 23:46:25 -0700461
462 /*
Stefan Richter97c18b72009-01-04 16:23:29 +0100463 * In the case that sizeof(*rsp) doesn't align with the position of the
David Moore8401d922008-07-29 23:46:25 -0700464 * data, and the read is short, preserve an extra copy of the data
465 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
466 * for short reads and some apps depended on it, this is both safe
467 * and prudent for compatibility.
468 */
Stefan Richter97c18b72009-01-04 16:23:29 +0100469 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
470 queue_event(client, &e->event, rsp, sizeof(*rsp),
471 rsp->data, rsp->length);
David Moore8401d922008-07-29 23:46:25 -0700472 else
Stefan Richter97c18b72009-01-04 16:23:29 +0100473 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
David Moore8401d922008-07-29 23:46:25 -0700474 NULL, 0);
Stefan Richterfb443032009-01-04 16:23:29 +0100475
476 /* Drop the transaction callback's reference */
477 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500478}
479
Jeff Garzik350958f2007-05-27 07:09:18 -0400480static int ioctl_send_request(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500481{
482 struct fw_device *device = client->device;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400483 struct fw_cdev_send_request *request = buffer;
Stefan Richter97c18b72009-01-04 16:23:29 +0100484 struct outbound_transaction_event *e;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100485 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500486
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500487 /* What is the biggest size we'll accept, really? */
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400488 if (request->length > 4096)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500489 return -EINVAL;
490
Stefan Richter97c18b72009-01-04 16:23:29 +0100491 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
492 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500493 return -ENOMEM;
494
Stefan Richter97c18b72009-01-04 16:23:29 +0100495 e->client = client;
496 e->response.length = request->length;
497 e->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500498
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400499 if (request->data &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100500 copy_from_user(e->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400501 u64_to_uptr(request->data), request->length)) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100502 ret = -EFAULT;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100503 goto failed;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100504 }
505
506 switch (request->tcode) {
507 case TCODE_WRITE_QUADLET_REQUEST:
508 case TCODE_WRITE_BLOCK_REQUEST:
509 case TCODE_READ_QUADLET_REQUEST:
510 case TCODE_READ_BLOCK_REQUEST:
511 case TCODE_LOCK_MASK_SWAP:
512 case TCODE_LOCK_COMPARE_SWAP:
513 case TCODE_LOCK_FETCH_ADD:
514 case TCODE_LOCK_LITTLE_ADD:
515 case TCODE_LOCK_BOUNDED_ADD:
516 case TCODE_LOCK_WRAP_ADD:
517 case TCODE_LOCK_VENDOR_DEPENDENT:
518 break;
519 default:
520 ret = -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100521 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500522 }
523
Stefan Richter97c18b72009-01-04 16:23:29 +0100524 e->r.resource.release = release_transaction;
525 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100526 if (ret < 0)
527 goto failed;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500528
Stefan Richterfb443032009-01-04 16:23:29 +0100529 /* Get a reference for the transaction callback */
530 client_get(client);
531
Stefan Richter97c18b72009-01-04 16:23:29 +0100532 fw_send_request(device->card, &e->r.transaction,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400533 request->tcode & 0x1f,
Stefan Richter907293d2007-01-23 21:11:43 +0100534 device->node->node_id,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400535 request->generation,
Stefan Richterf1397492007-06-10 21:31:36 +0200536 device->max_speed,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400537 request->offset,
Stefan Richter97c18b72009-01-04 16:23:29 +0100538 e->response.data, request->length,
539 complete_transaction, e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500540
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400541 if (request->data)
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400542 return sizeof(request) + request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500543 else
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400544 return sizeof(request);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100545 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100546 kfree(e);
Stefan Richter1f3125a2008-12-05 22:44:42 +0100547
548 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500549}
550
Stefan Richter53dca512008-12-14 21:47:04 +0100551static void release_request(struct client *client,
552 struct client_resource *resource)
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400553{
Stefan Richter97c18b72009-01-04 16:23:29 +0100554 struct inbound_transaction_resource *r = container_of(resource,
555 struct inbound_transaction_resource, resource);
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400556
Stefan Richter97c18b72009-01-04 16:23:29 +0100557 fw_send_response(client->device->card, r->request,
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400558 RCODE_CONFLICT_ERROR);
Stefan Richter97c18b72009-01-04 16:23:29 +0100559 kfree(r);
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400560}
561
Stefan Richter97c18b72009-01-04 16:23:29 +0100562static void handle_request(struct fw_card *card, struct fw_request *request,
Stefan Richter53dca512008-12-14 21:47:04 +0100563 int tcode, int destination, int source,
564 int generation, int speed,
565 unsigned long long offset,
566 void *payload, size_t length, void *callback_data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500567{
Stefan Richter97c18b72009-01-04 16:23:29 +0100568 struct address_handler_resource *handler = callback_data;
569 struct inbound_transaction_resource *r;
570 struct inbound_transaction_event *e;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100571 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500572
Stefan Richter97c18b72009-01-04 16:23:29 +0100573 r = kmalloc(sizeof(*r), GFP_ATOMIC);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400574 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Stefan Richter97c18b72009-01-04 16:23:29 +0100575 if (r == NULL || e == NULL)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100576 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500577
Stefan Richter97c18b72009-01-04 16:23:29 +0100578 r->request = request;
579 r->data = payload;
580 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500581
Stefan Richter97c18b72009-01-04 16:23:29 +0100582 r->resource.release = release_request;
583 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100584 if (ret < 0)
585 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500586
587 e->request.type = FW_CDEV_EVENT_REQUEST;
588 e->request.tcode = tcode;
589 e->request.offset = offset;
590 e->request.length = length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100591 e->request.handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500592 e->request.closure = handler->closure;
593
Stefan Richter97c18b72009-01-04 16:23:29 +0100594 queue_event(handler->client, &e->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400595 &e->request, sizeof(e->request), payload, length);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100596 return;
597
598 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100599 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100600 kfree(e);
Stefan Richter97c18b72009-01-04 16:23:29 +0100601 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500602}
603
Stefan Richter53dca512008-12-14 21:47:04 +0100604static void release_address_handler(struct client *client,
605 struct client_resource *resource)
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400606{
Stefan Richter97c18b72009-01-04 16:23:29 +0100607 struct address_handler_resource *r =
608 container_of(resource, struct address_handler_resource, resource);
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400609
Stefan Richter97c18b72009-01-04 16:23:29 +0100610 fw_core_remove_address_handler(&r->handler);
611 kfree(r);
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400612}
613
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400614static int ioctl_allocate(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500615{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400616 struct fw_cdev_allocate *request = buffer;
Stefan Richter97c18b72009-01-04 16:23:29 +0100617 struct address_handler_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500618 struct fw_address_region region;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100619 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500620
Stefan Richter97c18b72009-01-04 16:23:29 +0100621 r = kmalloc(sizeof(*r), GFP_KERNEL);
622 if (r == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500623 return -ENOMEM;
624
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400625 region.start = request->offset;
626 region.end = request->offset + request->length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100627 r->handler.length = request->length;
628 r->handler.address_callback = handle_request;
629 r->handler.callback_data = r;
630 r->closure = request->closure;
631 r->client = client;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500632
Stefan Richter97c18b72009-01-04 16:23:29 +0100633 ret = fw_core_add_address_handler(&r->handler, &region);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100634 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100635 kfree(r);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100636 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500637 }
638
Stefan Richter97c18b72009-01-04 16:23:29 +0100639 r->resource.release = release_address_handler;
640 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100641 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100642 release_address_handler(client, &r->resource);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100643 return ret;
644 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100645 request->handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500646
647 return 0;
648}
649
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400650static int ioctl_deallocate(struct client *client, void *buffer)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400651{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400652 struct fw_cdev_deallocate *request = buffer;
Kristian Høgsberg94723162007-03-14 17:34:55 -0400653
Jay Fenlason45ee3192008-12-21 16:47:17 +0100654 return release_client_resource(client, request->handle,
655 release_address_handler, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400656}
657
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400658static int ioctl_send_response(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500659{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400660 struct fw_cdev_send_response *request = buffer;
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400661 struct client_resource *resource;
Stefan Richter97c18b72009-01-04 16:23:29 +0100662 struct inbound_transaction_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500663
Jay Fenlason45ee3192008-12-21 16:47:17 +0100664 if (release_client_resource(client, request->handle,
665 release_request, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500666 return -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100667
Stefan Richter97c18b72009-01-04 16:23:29 +0100668 r = container_of(resource, struct inbound_transaction_resource,
669 resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400670 if (request->length < r->length)
671 r->length = request->length;
672 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500673 return -EFAULT;
674
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400675 fw_send_response(client->device->card, r->request, request->rcode);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500676 kfree(r);
677
678 return 0;
679}
680
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400681static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500682{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400683 struct fw_cdev_initiate_bus_reset *request = buffer;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500684 int short_reset;
685
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400686 short_reset = (request->type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500687
688 return fw_core_initiate_bus_reset(client->device->card, short_reset);
689}
690
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400691static void release_descriptor(struct client *client,
692 struct client_resource *resource)
693{
Stefan Richter97c18b72009-01-04 16:23:29 +0100694 struct descriptor_resource *r =
695 container_of(resource, struct descriptor_resource, resource);
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400696
Stefan Richter97c18b72009-01-04 16:23:29 +0100697 fw_core_remove_descriptor(&r->descriptor);
698 kfree(r);
Kristian Høgsberg3964a4492007-03-27 01:43:41 -0400699}
700
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400701static int ioctl_add_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200702{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400703 struct fw_cdev_add_descriptor *request = buffer;
Stefan Richter97c18b72009-01-04 16:23:29 +0100704 struct descriptor_resource *r;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100705 int ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200706
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400707 if (request->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200708 return -EINVAL;
709
Stefan Richter97c18b72009-01-04 16:23:29 +0100710 r = kmalloc(sizeof(*r) + request->length * 4, GFP_KERNEL);
711 if (r == NULL)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200712 return -ENOMEM;
713
Stefan Richter97c18b72009-01-04 16:23:29 +0100714 if (copy_from_user(r->data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400715 u64_to_uptr(request->data), request->length * 4)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100716 ret = -EFAULT;
717 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200718 }
719
Stefan Richter97c18b72009-01-04 16:23:29 +0100720 r->descriptor.length = request->length;
721 r->descriptor.immediate = request->immediate;
722 r->descriptor.key = request->key;
723 r->descriptor.data = r->data;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200724
Stefan Richter97c18b72009-01-04 16:23:29 +0100725 ret = fw_core_add_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100726 if (ret < 0)
727 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200728
Stefan Richter97c18b72009-01-04 16:23:29 +0100729 r->resource.release = release_descriptor;
730 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100731 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100732 fw_core_remove_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100733 goto failed;
734 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100735 request->handle = r->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200736
737 return 0;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100738 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100739 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100740
741 return ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200742}
743
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400744static int ioctl_remove_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200745{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400746 struct fw_cdev_remove_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200747
Jay Fenlason45ee3192008-12-21 16:47:17 +0100748 return release_client_resource(client, request->handle,
749 release_descriptor, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200750}
751
Stefan Richter53dca512008-12-14 21:47:04 +0100752static void iso_callback(struct fw_iso_context *context, u32 cycle,
753 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500754{
755 struct client *client = data;
Stefan Richter97c18b72009-01-04 16:23:29 +0100756 struct iso_interrupt_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500757
Stefan Richter97c18b72009-01-04 16:23:29 +0100758 e = kzalloc(sizeof(*e) + header_length, GFP_ATOMIC);
759 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500760 return;
761
Stefan Richter97c18b72009-01-04 16:23:29 +0100762 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
763 e->interrupt.closure = client->iso_closure;
764 e->interrupt.cycle = cycle;
765 e->interrupt.header_length = header_length;
766 memcpy(e->interrupt.header, header, header_length);
767 queue_event(client, &e->event, &e->interrupt,
768 sizeof(e->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500769}
770
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400771static int ioctl_create_iso_context(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500772{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400773 struct fw_cdev_create_iso_context *request = buffer;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400774 struct fw_iso_context *context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500775
Stefan Richterfae60312008-02-20 21:10:06 +0100776 /* We only support one context at this time. */
777 if (client->iso_context != NULL)
778 return -EBUSY;
779
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400780 if (request->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500781 return -EINVAL;
782
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400783 switch (request->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400784 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400785 if (request->header_size < 4 || (request->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400786 return -EINVAL;
787
788 break;
789
790 case FW_ISO_CONTEXT_TRANSMIT:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400791 if (request->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400792 return -EINVAL;
793
794 break;
795
796 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500797 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400798 }
799
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400800 context = fw_iso_context_create(client->device->card,
801 request->type,
802 request->channel,
803 request->speed,
804 request->header_size,
805 iso_callback, client);
806 if (IS_ERR(context))
807 return PTR_ERR(context);
808
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400809 client->iso_closure = request->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400810 client->iso_context = context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500811
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400812 /* We only support one context at this time. */
813 request->handle = 0;
814
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500815 return 0;
816}
817
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400818/* Macros for decoding the iso packet control header. */
819#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
820#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
821#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +0200822#define GET_TAG(v) (((v) >> 18) & 0x03)
823#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400824#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
825
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400826static int ioctl_queue_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500827{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400828 struct fw_cdev_queue_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500829 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500830 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200831 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400832 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500833 int count;
834 struct {
835 struct fw_iso_packet packet;
836 u8 header[256];
837 } u;
838
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400839 if (ctx == NULL || request->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500840 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500841
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400842 /*
843 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500844 * the iso buffer, and the pointer points inside the buffer,
845 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500846 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500847 * payload_length == 0 through. In other words, if no packets
848 * use the indirect payload, the iso buffer need not be mapped
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400849 * and the request->data pointer is ignored.
850 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500851
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400852 payload = (unsigned long)request->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200853 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400854 if (request->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200855 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500856 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200857 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500858 }
859
Al Viro1ccc9142007-10-14 19:34:40 +0100860 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
861
862 if (!access_ok(VERIFY_READ, p, request->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500863 return -EFAULT;
864
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400865 end = (void __user *)p + request->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500866 count = 0;
867 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400868 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500869 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400870 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
871 u.packet.interrupt = GET_INTERRUPT(control);
872 u.packet.skip = GET_SKIP(control);
873 u.packet.tag = GET_TAG(control);
874 u.packet.sy = GET_SY(control);
875 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500876
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500877 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500878 header_length = u.packet.header_length;
879 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400880 /*
881 * We require that header_length is a multiple of
882 * the fixed header size, ctx->header_size.
883 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500884 if (ctx->header_size == 0) {
885 if (u.packet.header_length > 0)
886 return -EINVAL;
887 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500888 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500889 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500890 header_length = 0;
891 }
892
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500893 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500894 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500895 if (next > end)
896 return -EINVAL;
897 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500898 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500899 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500900 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500901 u.packet.header_length + u.packet.payload_length > 0)
902 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200903 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500904 return -EINVAL;
905
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500906 if (fw_iso_context_queue(ctx, &u.packet,
907 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500908 break;
909
910 p = next;
911 payload += u.packet.payload_length;
912 count++;
913 }
914
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400915 request->size -= uptr_to_u64(p) - request->packets;
916 request->packets = uptr_to_u64(p);
917 request->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500918
919 return count;
920}
921
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400922static int ioctl_start_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500923{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400924 struct fw_cdev_start_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500925
Stefan Richterfae60312008-02-20 21:10:06 +0100926 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400927 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +0100928
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400929 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400930 if (request->tags == 0 || request->tags > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400931 return -EINVAL;
932
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400933 if (request->sync > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400934 return -EINVAL;
935 }
936
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400937 return fw_iso_context_start(client->iso_context, request->cycle,
938 request->sync, request->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500939}
940
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400941static int ioctl_stop_iso(struct client *client, void *buffer)
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500942{
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400943 struct fw_cdev_stop_iso *request = buffer;
944
Stefan Richterfae60312008-02-20 21:10:06 +0100945 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400946 return -EINVAL;
947
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500948 return fw_iso_context_stop(client->iso_context);
949}
950
Stefan Richtera64408b2007-09-29 10:41:58 +0200951static int ioctl_get_cycle_timer(struct client *client, void *buffer)
952{
953 struct fw_cdev_get_cycle_timer *request = buffer;
954 struct fw_card *card = client->device->card;
955 unsigned long long bus_time;
956 struct timeval tv;
957 unsigned long flags;
958
959 preempt_disable();
960 local_irq_save(flags);
961
962 bus_time = card->driver->get_bus_time(card);
963 do_gettimeofday(&tv);
964
965 local_irq_restore(flags);
966 preempt_enable();
967
968 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
969 request->cycle_timer = bus_time & 0xffffffff;
970 return 0;
971}
972
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400973static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
974 ioctl_get_info,
975 ioctl_send_request,
976 ioctl_allocate,
977 ioctl_deallocate,
978 ioctl_send_response,
979 ioctl_initiate_bus_reset,
980 ioctl_add_descriptor,
981 ioctl_remove_descriptor,
982 ioctl_create_iso_context,
983 ioctl_queue_iso,
984 ioctl_start_iso,
985 ioctl_stop_iso,
Stefan Richtera64408b2007-09-29 10:41:58 +0200986 ioctl_get_cycle_timer,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400987};
988
Stefan Richter53dca512008-12-14 21:47:04 +0100989static int dispatch_ioctl(struct client *client,
990 unsigned int cmd, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500991{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400992 char buffer[256];
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100993 int ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400994
995 if (_IOC_TYPE(cmd) != '#' ||
996 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500997 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400998
999 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001000 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001001 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
1002 return -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001003 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001004
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001005 ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
1006 if (ret < 0)
1007 return ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001008
1009 if (_IOC_DIR(cmd) & _IOC_READ) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001010 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001011 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
1012 return -EFAULT;
1013 }
1014
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001015 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001016}
1017
Stefan Richter53dca512008-12-14 21:47:04 +01001018static long fw_device_op_ioctl(struct file *file,
1019 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001020{
1021 struct client *client = file->private_data;
1022
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001023 if (fw_device_is_shutdown(client->device))
1024 return -ENODEV;
1025
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001026 return dispatch_ioctl(client, cmd, (void __user *) arg);
1027}
1028
1029#ifdef CONFIG_COMPAT
Stefan Richter53dca512008-12-14 21:47:04 +01001030static long fw_device_op_compat_ioctl(struct file *file,
1031 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001032{
1033 struct client *client = file->private_data;
1034
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001035 if (fw_device_is_shutdown(client->device))
1036 return -ENODEV;
1037
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001038 return dispatch_ioctl(client, cmd, compat_ptr(arg));
1039}
1040#endif
1041
1042static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1043{
1044 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001045 enum dma_data_direction direction;
1046 unsigned long size;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001047 int page_count, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001048
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001049 if (fw_device_is_shutdown(client->device))
1050 return -ENODEV;
1051
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001052 /* FIXME: We could support multiple buffers, but we don't. */
1053 if (client->buffer.pages != NULL)
1054 return -EBUSY;
1055
1056 if (!(vma->vm_flags & VM_SHARED))
1057 return -EINVAL;
1058
1059 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001060 return -EINVAL;
1061
1062 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001063 size = vma->vm_end - vma->vm_start;
1064 page_count = size >> PAGE_SHIFT;
1065 if (size & ~PAGE_MASK)
1066 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001067
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001068 if (vma->vm_flags & VM_WRITE)
1069 direction = DMA_TO_DEVICE;
1070 else
1071 direction = DMA_FROM_DEVICE;
1072
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001073 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1074 page_count, direction);
1075 if (ret < 0)
1076 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001077
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001078 ret = fw_iso_buffer_map(&client->buffer, vma);
1079 if (ret < 0)
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001080 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1081
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001082 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001083}
1084
Jay Fenlason45ee3192008-12-21 16:47:17 +01001085static int shutdown_resource(int id, void *p, void *data)
1086{
1087 struct client_resource *r = p;
1088 struct client *client = data;
1089
1090 r->release(client, r);
Stefan Richterfb443032009-01-04 16:23:29 +01001091 client_put(client);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001092
1093 return 0;
1094}
1095
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001096static int fw_device_op_release(struct inode *inode, struct file *file)
1097{
1098 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001099 struct event *e, *next_e;
Jay Fenlason45ee3192008-12-21 16:47:17 +01001100 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001101
Stefan Richter97811e32008-12-14 19:19:23 +01001102 mutex_lock(&client->device->client_list_mutex);
1103 list_del(&client->link);
1104 mutex_unlock(&client->device->client_list_mutex);
1105
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001106 if (client->buffer.pages)
1107 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1108
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001109 if (client->iso_context)
1110 fw_iso_context_destroy(client->iso_context);
1111
Jay Fenlason45ee3192008-12-21 16:47:17 +01001112 /* Freeze client->resource_idr and client->event_list */
1113 spin_lock_irqsave(&client->lock, flags);
1114 client->in_shutdown = true;
1115 spin_unlock_irqrestore(&client->lock, flags);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001116
Jay Fenlason45ee3192008-12-21 16:47:17 +01001117 idr_for_each(&client->resource_idr, shutdown_resource, client);
1118 idr_remove_all(&client->resource_idr);
1119 idr_destroy(&client->resource_idr);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001120
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001121 list_for_each_entry_safe(e, next_e, &client->event_list, link)
1122 kfree(e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001123
Stefan Richterfb443032009-01-04 16:23:29 +01001124 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001125
1126 return 0;
1127}
1128
1129static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1130{
1131 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001132 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001133
1134 poll_wait(file, &client->wait, pt);
1135
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001136 if (fw_device_is_shutdown(client->device))
1137 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001138 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001139 mask |= POLLIN | POLLRDNORM;
1140
1141 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001142}
1143
Stefan Richter21ebcd12007-01-14 15:29:07 +01001144const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001145 .owner = THIS_MODULE,
1146 .open = fw_device_op_open,
1147 .read = fw_device_op_read,
1148 .unlocked_ioctl = fw_device_op_ioctl,
1149 .poll = fw_device_op_poll,
1150 .release = fw_device_op_release,
1151 .mmap = fw_device_op_mmap,
1152
1153#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001154 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001155#endif
1156};