blob: a227853aa1e2d9cd851dd9b1ed530a9c54e0c422 [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>
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +010027#include <linux/jiffies.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050028#include <linux/kernel.h>
Stefan Richterfb443032009-01-04 16:23:29 +010029#include <linux/kref.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010030#include <linux/mm.h>
31#include <linux/module.h>
Stefan Richterd67cfb92008-10-05 10:37:11 +020032#include <linux/mutex.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050033#include <linux/poll.h>
Stefan Richtera64408b2007-09-29 10:41:58 +020034#include <linux/preempt.h>
Jay Fenlasoncf417e542008-10-03 11:19:09 -040035#include <linux/spinlock.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010036#include <linux/time.h>
37#include <linux/vmalloc.h>
38#include <linux/wait.h>
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +010039#include <linux/workqueue.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010040
Stefan Richtera64408b2007-09-29 10:41:58 +020041#include <asm/system.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050042#include <asm/uaccess.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010043
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050044#include "fw-device.h"
Stefan Richterbe5bbd62009-01-04 16:23:29 +010045#include "fw-topology.h"
46#include "fw-transaction.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050047
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050048struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050049 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050050 struct fw_device *device;
Jay Fenlason45ee3192008-12-21 16:47:17 +010051
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050052 spinlock_t lock;
Jay Fenlason45ee3192008-12-21 16:47:17 +010053 bool in_shutdown;
54 struct idr resource_idr;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050055 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050056 wait_queue_head_t wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040057 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050058
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050059 struct fw_iso_context *iso_context;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -040060 u64 iso_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050061 struct fw_iso_buffer buffer;
62 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050063
64 struct list_head link;
Stefan Richterfb443032009-01-04 16:23:29 +010065 struct kref kref;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050066};
67
Stefan Richterfb443032009-01-04 16:23:29 +010068static inline void client_get(struct client *client)
69{
70 kref_get(&client->kref);
71}
72
73static void client_release(struct kref *kref)
74{
75 struct client *client = container_of(kref, struct client, kref);
76
77 fw_device_put(client->device);
78 kfree(client);
79}
80
81static void client_put(struct client *client)
82{
83 kref_put(&client->kref, client_release);
84}
85
Stefan Richter97c18b72009-01-04 16:23:29 +010086struct client_resource;
87typedef void (*client_resource_release_fn_t)(struct client *,
88 struct client_resource *);
89struct client_resource {
90 client_resource_release_fn_t release;
91 int handle;
92};
93
94struct address_handler_resource {
95 struct client_resource resource;
96 struct fw_address_handler handler;
97 __u64 closure;
98 struct client *client;
99};
100
101struct outbound_transaction_resource {
102 struct client_resource resource;
103 struct fw_transaction transaction;
104};
105
106struct inbound_transaction_resource {
107 struct client_resource resource;
108 struct fw_request *request;
109 void *data;
110 size_t length;
111};
112
113struct descriptor_resource {
114 struct client_resource resource;
115 struct fw_descriptor descriptor;
116 u32 data[0];
117};
118
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100119struct iso_resource {
120 struct client_resource resource;
121 struct client *client;
122 /* Schedule work and access todo only with client->lock held. */
123 struct delayed_work work;
124 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,} todo;
125 int generation;
126 u64 channels;
127 s32 bandwidth;
128 struct iso_resource_event *e_alloc, *e_dealloc;
129};
130
131static void schedule_iso_resource(struct iso_resource *);
132static void release_iso_resource(struct client *, struct client_resource *);
133
Stefan Richter97c18b72009-01-04 16:23:29 +0100134/*
135 * dequeue_event() just kfree()'s the event, so the event has to be
136 * the first field in a struct XYZ_event.
137 */
138struct event {
139 struct { void *data; size_t size; } v[2];
140 struct list_head link;
141};
142
143struct bus_reset_event {
144 struct event event;
145 struct fw_cdev_event_bus_reset reset;
146};
147
148struct outbound_transaction_event {
149 struct event event;
150 struct client *client;
151 struct outbound_transaction_resource r;
152 struct fw_cdev_event_response response;
153};
154
155struct inbound_transaction_event {
156 struct event event;
157 struct fw_cdev_event_request request;
158};
159
160struct iso_interrupt_event {
161 struct event event;
162 struct fw_cdev_event_iso_interrupt interrupt;
163};
164
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100165struct iso_resource_event {
166 struct event event;
167 struct fw_cdev_event_iso_resource resource;
168};
169
Stefan Richter53dca512008-12-14 21:47:04 +0100170static inline void __user *u64_to_uptr(__u64 value)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500171{
172 return (void __user *)(unsigned long)value;
173}
174
Stefan Richter53dca512008-12-14 21:47:04 +0100175static inline __u64 uptr_to_u64(void __user *ptr)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500176{
177 return (__u64)(unsigned long)ptr;
178}
179
180static int fw_device_op_open(struct inode *inode, struct file *file)
181{
182 struct fw_device *device;
183 struct client *client;
184
Stefan Richter96b19062008-02-02 15:01:09 +0100185 device = fw_device_get_by_devt(inode->i_rdev);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500186 if (device == NULL)
187 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500188
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400189 if (fw_device_is_shutdown(device)) {
190 fw_device_put(device);
191 return -ENODEV;
192 }
193
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400194 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100195 if (client == NULL) {
196 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500197 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100198 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500199
Stefan Richter96b19062008-02-02 15:01:09 +0100200 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500201 spin_lock_init(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100202 idr_init(&client->resource_idr);
203 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500204 init_waitqueue_head(&client->wait);
Stefan Richterfb443032009-01-04 16:23:29 +0100205 kref_init(&client->kref);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500206
207 file->private_data = client;
208
Stefan Richterd67cfb92008-10-05 10:37:11 +0200209 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500210 list_add_tail(&client->link, &device->client_list);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200211 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500212
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500213 return 0;
214}
215
216static void queue_event(struct client *client, struct event *event,
217 void *data0, size_t size0, void *data1, size_t size1)
218{
219 unsigned long flags;
220
221 event->v[0].data = data0;
222 event->v[0].size = size0;
223 event->v[1].data = data1;
224 event->v[1].size = size1;
225
226 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100227 if (client->in_shutdown)
228 kfree(event);
229 else
230 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500231 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400232
233 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500234}
235
Stefan Richter53dca512008-12-14 21:47:04 +0100236static int dequeue_event(struct client *client,
237 char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500238{
239 unsigned long flags;
240 struct event *event;
241 size_t size, total;
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100242 int i, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500243
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100244 ret = wait_event_interruptible(client->wait,
245 !list_empty(&client->event_list) ||
246 fw_device_is_shutdown(client->device));
247 if (ret < 0)
248 return ret;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500249
250 if (list_empty(&client->event_list) &&
251 fw_device_is_shutdown(client->device))
252 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500253
254 spin_lock_irqsave(&client->lock, flags);
Stefan Richtera459b8a2008-12-21 16:49:57 +0100255 event = list_first_entry(&client->event_list, struct event, link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500256 list_del(&event->link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500257 spin_unlock_irqrestore(&client->lock, flags);
258
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500259 total = 0;
260 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
261 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500262 if (copy_to_user(buffer + total, event->v[i].data, size)) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100263 ret = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500264 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500265 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500266 total += size;
267 }
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100268 ret = total;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500269
270 out:
271 kfree(event);
272
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100273 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500274}
275
Stefan Richter53dca512008-12-14 21:47:04 +0100276static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
277 size_t count, loff_t *offset)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500278{
279 struct client *client = file->private_data;
280
281 return dequeue_event(client, buffer, count);
282}
283
Stefan Richter53dca512008-12-14 21:47:04 +0100284static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
285 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500286{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400287 struct fw_card *card = client->device->card;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400288 unsigned long flags;
289
290 spin_lock_irqsave(&card->lock, flags);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500291
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400292 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500293 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100294 event->generation = client->device->generation;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400295 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500296 event->local_node_id = card->local_node->node_id;
297 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
298 event->irm_node_id = card->irm_node->node_id;
299 event->root_node_id = card->root_node->node_id;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400300
301 spin_unlock_irqrestore(&card->lock, flags);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500302}
303
Stefan Richter53dca512008-12-14 21:47:04 +0100304static void for_each_client(struct fw_device *device,
305 void (*callback)(struct client *client))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500306{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500307 struct client *c;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500308
Stefan Richterd67cfb92008-10-05 10:37:11 +0200309 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500310 list_for_each_entry(c, &device->client_list, link)
311 callback(c);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200312 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500313}
314
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100315static int schedule_reallocations(int id, void *p, void *data)
316{
317 struct client_resource *r = p;
318
319 if (r->release == release_iso_resource)
320 schedule_iso_resource(container_of(r,
321 struct iso_resource, resource));
322 return 0;
323}
324
Stefan Richter53dca512008-12-14 21:47:04 +0100325static void queue_bus_reset_event(struct client *client)
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500326{
Stefan Richter97c18b72009-01-04 16:23:29 +0100327 struct bus_reset_event *e;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500328
Stefan Richter97c18b72009-01-04 16:23:29 +0100329 e = kzalloc(sizeof(*e), GFP_KERNEL);
330 if (e == NULL) {
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500331 fw_notify("Out of memory when allocating bus reset event\n");
332 return;
333 }
334
Stefan Richter97c18b72009-01-04 16:23:29 +0100335 fill_bus_reset_event(&e->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500336
Stefan Richter97c18b72009-01-04 16:23:29 +0100337 queue_event(client, &e->event,
338 &e->reset, sizeof(e->reset), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100339
340 spin_lock_irq(&client->lock);
341 idr_for_each(&client->resource_idr, schedule_reallocations, client);
342 spin_unlock_irq(&client->lock);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500343}
344
345void fw_device_cdev_update(struct fw_device *device)
346{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500347 for_each_client(device, queue_bus_reset_event);
348}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500349
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500350static void wake_up_client(struct client *client)
351{
352 wake_up_interruptible(&client->wait);
353}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500354
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500355void fw_device_cdev_remove(struct fw_device *device)
356{
357 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500358}
359
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400360static int ioctl_get_info(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500361{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400362 struct fw_cdev_get_info *get_info = buffer;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500363 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100364 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500365
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400366 client->version = get_info->version;
367 get_info->version = FW_CDEV_VERSION;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400368 get_info->card = client->device->card->index;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500369
Stefan Richterc9755e12008-03-24 20:54:28 +0100370 down_read(&fw_device_rwsem);
371
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400372 if (get_info->rom != 0) {
373 void __user *uptr = u64_to_uptr(get_info->rom);
374 size_t want = get_info->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100375 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500376
Stefan Richterc9755e12008-03-24 20:54:28 +0100377 ret = copy_to_user(uptr, client->device->config_rom,
378 min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500379 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400380 get_info->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500381
Stefan Richterc9755e12008-03-24 20:54:28 +0100382 up_read(&fw_device_rwsem);
383
384 if (ret != 0)
385 return -EFAULT;
386
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400387 client->bus_reset_closure = get_info->bus_reset_closure;
388 if (get_info->bus_reset != 0) {
389 void __user *uptr = u64_to_uptr(get_info->bus_reset);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500390
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400391 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400392 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500393 return -EFAULT;
394 }
395
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500396 return 0;
397}
398
Stefan Richter53dca512008-12-14 21:47:04 +0100399static int add_client_resource(struct client *client,
400 struct client_resource *resource, gfp_t gfp_mask)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400401{
402 unsigned long flags;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100403 int ret;
404
405 retry:
406 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
407 return -ENOMEM;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400408
409 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100410 if (client->in_shutdown)
411 ret = -ECANCELED;
412 else
413 ret = idr_get_new(&client->resource_idr, resource,
414 &resource->handle);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100415 if (ret >= 0) {
Stefan Richterfb443032009-01-04 16:23:29 +0100416 client_get(client);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100417 if (resource->release == release_iso_resource)
418 schedule_iso_resource(container_of(resource,
419 struct iso_resource, resource));
420 }
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400421 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100422
423 if (ret == -EAGAIN)
424 goto retry;
425
426 return ret < 0 ? ret : 0;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400427}
428
Stefan Richter53dca512008-12-14 21:47:04 +0100429static int release_client_resource(struct client *client, u32 handle,
430 client_resource_release_fn_t release,
431 struct client_resource **resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400432{
433 struct client_resource *r;
434 unsigned long flags;
435
436 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100437 if (client->in_shutdown)
438 r = NULL;
439 else
440 r = idr_find(&client->resource_idr, handle);
441 if (r && r->release == release)
442 idr_remove(&client->resource_idr, handle);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400443 spin_unlock_irqrestore(&client->lock, flags);
444
Jay Fenlason45ee3192008-12-21 16:47:17 +0100445 if (!(r && r->release == release))
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400446 return -EINVAL;
447
448 if (resource)
449 *resource = r;
450 else
451 r->release(client, r);
452
Stefan Richterfb443032009-01-04 16:23:29 +0100453 client_put(client);
454
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400455 return 0;
456}
457
Stefan Richter53dca512008-12-14 21:47:04 +0100458static void release_transaction(struct client *client,
459 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400460{
Stefan Richter97c18b72009-01-04 16:23:29 +0100461 struct outbound_transaction_resource *r = container_of(resource,
462 struct outbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400463
Stefan Richter97c18b72009-01-04 16:23:29 +0100464 fw_cancel_transaction(client->device->card, &r->transaction);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400465}
466
Stefan Richter53dca512008-12-14 21:47:04 +0100467static void complete_transaction(struct fw_card *card, int rcode,
468 void *payload, size_t length, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500469{
Stefan Richter97c18b72009-01-04 16:23:29 +0100470 struct outbound_transaction_event *e = data;
471 struct fw_cdev_event_response *rsp = &e->response;
472 struct client *client = e->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500473 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500474
Stefan Richter97c18b72009-01-04 16:23:29 +0100475 if (length < rsp->length)
476 rsp->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500477 if (rcode == RCODE_COMPLETE)
Stefan Richter97c18b72009-01-04 16:23:29 +0100478 memcpy(rsp->data, payload, rsp->length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500479
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500480 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100481 /*
Stefan Richterfb443032009-01-04 16:23:29 +0100482 * 1. If called while in shutdown, the idr tree must be left untouched.
483 * The idr handle will be removed and the client reference will be
484 * dropped later.
485 * 2. If the call chain was release_client_resource ->
486 * release_transaction -> complete_transaction (instead of a normal
487 * conclusion of the transaction), i.e. if this resource was already
488 * unregistered from the idr, the client reference will be dropped
489 * by release_client_resource and we must not drop it here.
Jay Fenlason45ee3192008-12-21 16:47:17 +0100490 */
Stefan Richterfb443032009-01-04 16:23:29 +0100491 if (!client->in_shutdown &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100492 idr_find(&client->resource_idr, e->r.resource.handle)) {
493 idr_remove(&client->resource_idr, e->r.resource.handle);
Stefan Richterfb443032009-01-04 16:23:29 +0100494 /* Drop the idr's reference */
495 client_put(client);
496 }
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500497 spin_unlock_irqrestore(&client->lock, flags);
498
Stefan Richter97c18b72009-01-04 16:23:29 +0100499 rsp->type = FW_CDEV_EVENT_RESPONSE;
500 rsp->rcode = rcode;
David Moore8401d922008-07-29 23:46:25 -0700501
502 /*
Stefan Richter97c18b72009-01-04 16:23:29 +0100503 * In the case that sizeof(*rsp) doesn't align with the position of the
David Moore8401d922008-07-29 23:46:25 -0700504 * data, and the read is short, preserve an extra copy of the data
505 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
506 * for short reads and some apps depended on it, this is both safe
507 * and prudent for compatibility.
508 */
Stefan Richter97c18b72009-01-04 16:23:29 +0100509 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
510 queue_event(client, &e->event, rsp, sizeof(*rsp),
511 rsp->data, rsp->length);
David Moore8401d922008-07-29 23:46:25 -0700512 else
Stefan Richter97c18b72009-01-04 16:23:29 +0100513 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
David Moore8401d922008-07-29 23:46:25 -0700514 NULL, 0);
Stefan Richterfb443032009-01-04 16:23:29 +0100515
516 /* Drop the transaction callback's reference */
517 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500518}
519
Jeff Garzik350958f2007-05-27 07:09:18 -0400520static int ioctl_send_request(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500521{
522 struct fw_device *device = client->device;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400523 struct fw_cdev_send_request *request = buffer;
Stefan Richter97c18b72009-01-04 16:23:29 +0100524 struct outbound_transaction_event *e;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100525 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500526
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500527 /* What is the biggest size we'll accept, really? */
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400528 if (request->length > 4096)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500529 return -EINVAL;
530
Stefan Richter97c18b72009-01-04 16:23:29 +0100531 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
532 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500533 return -ENOMEM;
534
Stefan Richter97c18b72009-01-04 16:23:29 +0100535 e->client = client;
536 e->response.length = request->length;
537 e->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500538
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400539 if (request->data &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100540 copy_from_user(e->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400541 u64_to_uptr(request->data), request->length)) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100542 ret = -EFAULT;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100543 goto failed;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100544 }
545
546 switch (request->tcode) {
547 case TCODE_WRITE_QUADLET_REQUEST:
548 case TCODE_WRITE_BLOCK_REQUEST:
549 case TCODE_READ_QUADLET_REQUEST:
550 case TCODE_READ_BLOCK_REQUEST:
551 case TCODE_LOCK_MASK_SWAP:
552 case TCODE_LOCK_COMPARE_SWAP:
553 case TCODE_LOCK_FETCH_ADD:
554 case TCODE_LOCK_LITTLE_ADD:
555 case TCODE_LOCK_BOUNDED_ADD:
556 case TCODE_LOCK_WRAP_ADD:
557 case TCODE_LOCK_VENDOR_DEPENDENT:
558 break;
559 default:
560 ret = -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100561 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500562 }
563
Stefan Richter97c18b72009-01-04 16:23:29 +0100564 e->r.resource.release = release_transaction;
565 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100566 if (ret < 0)
567 goto failed;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500568
Stefan Richterfb443032009-01-04 16:23:29 +0100569 /* Get a reference for the transaction callback */
570 client_get(client);
571
Stefan Richter97c18b72009-01-04 16:23:29 +0100572 fw_send_request(device->card, &e->r.transaction,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400573 request->tcode & 0x1f,
Stefan Richter907293d2007-01-23 21:11:43 +0100574 device->node->node_id,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400575 request->generation,
Stefan Richterf1397492007-06-10 21:31:36 +0200576 device->max_speed,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400577 request->offset,
Stefan Richter97c18b72009-01-04 16:23:29 +0100578 e->response.data, request->length,
579 complete_transaction, e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500580
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400581 if (request->data)
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400582 return sizeof(request) + request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500583 else
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400584 return sizeof(request);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100585 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100586 kfree(e);
Stefan Richter1f3125a2008-12-05 22:44:42 +0100587
588 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500589}
590
Stefan Richter53dca512008-12-14 21:47:04 +0100591static void release_request(struct client *client,
592 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400593{
Stefan Richter97c18b72009-01-04 16:23:29 +0100594 struct inbound_transaction_resource *r = container_of(resource,
595 struct inbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400596
Stefan Richter97c18b72009-01-04 16:23:29 +0100597 fw_send_response(client->device->card, r->request,
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400598 RCODE_CONFLICT_ERROR);
Stefan Richter97c18b72009-01-04 16:23:29 +0100599 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400600}
601
Stefan Richter97c18b72009-01-04 16:23:29 +0100602static void handle_request(struct fw_card *card, struct fw_request *request,
Stefan Richter53dca512008-12-14 21:47:04 +0100603 int tcode, int destination, int source,
604 int generation, int speed,
605 unsigned long long offset,
606 void *payload, size_t length, void *callback_data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500607{
Stefan Richter97c18b72009-01-04 16:23:29 +0100608 struct address_handler_resource *handler = callback_data;
609 struct inbound_transaction_resource *r;
610 struct inbound_transaction_event *e;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100611 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500612
Stefan Richter97c18b72009-01-04 16:23:29 +0100613 r = kmalloc(sizeof(*r), GFP_ATOMIC);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400614 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Stefan Richter97c18b72009-01-04 16:23:29 +0100615 if (r == NULL || e == NULL)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100616 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500617
Stefan Richter97c18b72009-01-04 16:23:29 +0100618 r->request = request;
619 r->data = payload;
620 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500621
Stefan Richter97c18b72009-01-04 16:23:29 +0100622 r->resource.release = release_request;
623 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100624 if (ret < 0)
625 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500626
627 e->request.type = FW_CDEV_EVENT_REQUEST;
628 e->request.tcode = tcode;
629 e->request.offset = offset;
630 e->request.length = length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100631 e->request.handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500632 e->request.closure = handler->closure;
633
Stefan Richter97c18b72009-01-04 16:23:29 +0100634 queue_event(handler->client, &e->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400635 &e->request, sizeof(e->request), payload, length);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100636 return;
637
638 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100639 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100640 kfree(e);
Stefan Richter97c18b72009-01-04 16:23:29 +0100641 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500642}
643
Stefan Richter53dca512008-12-14 21:47:04 +0100644static void release_address_handler(struct client *client,
645 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400646{
Stefan Richter97c18b72009-01-04 16:23:29 +0100647 struct address_handler_resource *r =
648 container_of(resource, struct address_handler_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400649
Stefan Richter97c18b72009-01-04 16:23:29 +0100650 fw_core_remove_address_handler(&r->handler);
651 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400652}
653
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400654static int ioctl_allocate(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500655{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400656 struct fw_cdev_allocate *request = buffer;
Stefan Richter97c18b72009-01-04 16:23:29 +0100657 struct address_handler_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500658 struct fw_address_region region;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100659 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500660
Stefan Richter97c18b72009-01-04 16:23:29 +0100661 r = kmalloc(sizeof(*r), GFP_KERNEL);
662 if (r == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500663 return -ENOMEM;
664
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400665 region.start = request->offset;
666 region.end = request->offset + request->length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100667 r->handler.length = request->length;
668 r->handler.address_callback = handle_request;
669 r->handler.callback_data = r;
670 r->closure = request->closure;
671 r->client = client;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500672
Stefan Richter97c18b72009-01-04 16:23:29 +0100673 ret = fw_core_add_address_handler(&r->handler, &region);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100674 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100675 kfree(r);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100676 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500677 }
678
Stefan Richter97c18b72009-01-04 16:23:29 +0100679 r->resource.release = release_address_handler;
680 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100681 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100682 release_address_handler(client, &r->resource);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100683 return ret;
684 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100685 request->handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500686
687 return 0;
688}
689
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400690static int ioctl_deallocate(struct client *client, void *buffer)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400691{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400692 struct fw_cdev_deallocate *request = buffer;
Kristian Høgsberg94723162007-03-14 17:34:55 -0400693
Jay Fenlason45ee3192008-12-21 16:47:17 +0100694 return release_client_resource(client, request->handle,
695 release_address_handler, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400696}
697
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400698static int ioctl_send_response(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500699{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400700 struct fw_cdev_send_response *request = buffer;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400701 struct client_resource *resource;
Stefan Richter97c18b72009-01-04 16:23:29 +0100702 struct inbound_transaction_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500703
Jay Fenlason45ee3192008-12-21 16:47:17 +0100704 if (release_client_resource(client, request->handle,
705 release_request, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500706 return -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100707
Stefan Richter97c18b72009-01-04 16:23:29 +0100708 r = container_of(resource, struct inbound_transaction_resource,
709 resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400710 if (request->length < r->length)
711 r->length = request->length;
712 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500713 return -EFAULT;
714
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400715 fw_send_response(client->device->card, r->request, request->rcode);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500716 kfree(r);
717
718 return 0;
719}
720
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400721static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500722{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400723 struct fw_cdev_initiate_bus_reset *request = buffer;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500724 int short_reset;
725
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400726 short_reset = (request->type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500727
728 return fw_core_initiate_bus_reset(client->device->card, short_reset);
729}
730
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400731static void release_descriptor(struct client *client,
732 struct client_resource *resource)
733{
Stefan Richter97c18b72009-01-04 16:23:29 +0100734 struct descriptor_resource *r =
735 container_of(resource, struct descriptor_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400736
Stefan Richter97c18b72009-01-04 16:23:29 +0100737 fw_core_remove_descriptor(&r->descriptor);
738 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400739}
740
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400741static int ioctl_add_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_add_descriptor *request = buffer;
Stefan Richter97c18b72009-01-04 16:23:29 +0100744 struct descriptor_resource *r;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100745 int ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200746
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400747 if (request->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200748 return -EINVAL;
749
Stefan Richter97c18b72009-01-04 16:23:29 +0100750 r = kmalloc(sizeof(*r) + request->length * 4, GFP_KERNEL);
751 if (r == NULL)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200752 return -ENOMEM;
753
Stefan Richter97c18b72009-01-04 16:23:29 +0100754 if (copy_from_user(r->data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400755 u64_to_uptr(request->data), request->length * 4)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100756 ret = -EFAULT;
757 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200758 }
759
Stefan Richter97c18b72009-01-04 16:23:29 +0100760 r->descriptor.length = request->length;
761 r->descriptor.immediate = request->immediate;
762 r->descriptor.key = request->key;
763 r->descriptor.data = r->data;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200764
Stefan Richter97c18b72009-01-04 16:23:29 +0100765 ret = fw_core_add_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100766 if (ret < 0)
767 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200768
Stefan Richter97c18b72009-01-04 16:23:29 +0100769 r->resource.release = release_descriptor;
770 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100771 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100772 fw_core_remove_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100773 goto failed;
774 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100775 request->handle = r->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200776
777 return 0;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100778 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100779 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100780
781 return ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200782}
783
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400784static int ioctl_remove_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200785{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400786 struct fw_cdev_remove_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200787
Jay Fenlason45ee3192008-12-21 16:47:17 +0100788 return release_client_resource(client, request->handle,
789 release_descriptor, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200790}
791
Stefan Richter53dca512008-12-14 21:47:04 +0100792static void iso_callback(struct fw_iso_context *context, u32 cycle,
793 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500794{
795 struct client *client = data;
Stefan Richter97c18b72009-01-04 16:23:29 +0100796 struct iso_interrupt_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500797
Stefan Richter97c18b72009-01-04 16:23:29 +0100798 e = kzalloc(sizeof(*e) + header_length, GFP_ATOMIC);
799 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500800 return;
801
Stefan Richter97c18b72009-01-04 16:23:29 +0100802 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
803 e->interrupt.closure = client->iso_closure;
804 e->interrupt.cycle = cycle;
805 e->interrupt.header_length = header_length;
806 memcpy(e->interrupt.header, header, header_length);
807 queue_event(client, &e->event, &e->interrupt,
808 sizeof(e->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500809}
810
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400811static int ioctl_create_iso_context(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500812{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400813 struct fw_cdev_create_iso_context *request = buffer;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400814 struct fw_iso_context *context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500815
Stefan Richterfae60312008-02-20 21:10:06 +0100816 /* We only support one context at this time. */
817 if (client->iso_context != NULL)
818 return -EBUSY;
819
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400820 if (request->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500821 return -EINVAL;
822
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400823 switch (request->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400824 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400825 if (request->header_size < 4 || (request->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400826 return -EINVAL;
827
828 break;
829
830 case FW_ISO_CONTEXT_TRANSMIT:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400831 if (request->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400832 return -EINVAL;
833
834 break;
835
836 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500837 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400838 }
839
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400840 context = fw_iso_context_create(client->device->card,
841 request->type,
842 request->channel,
843 request->speed,
844 request->header_size,
845 iso_callback, client);
846 if (IS_ERR(context))
847 return PTR_ERR(context);
848
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400849 client->iso_closure = request->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400850 client->iso_context = context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500851
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400852 /* We only support one context at this time. */
853 request->handle = 0;
854
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500855 return 0;
856}
857
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400858/* Macros for decoding the iso packet control header. */
859#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
860#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
861#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +0200862#define GET_TAG(v) (((v) >> 18) & 0x03)
863#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400864#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
865
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400866static int ioctl_queue_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500867{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400868 struct fw_cdev_queue_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500869 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500870 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200871 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400872 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500873 int count;
874 struct {
875 struct fw_iso_packet packet;
876 u8 header[256];
877 } u;
878
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400879 if (ctx == NULL || request->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500880 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500881
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400882 /*
883 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500884 * the iso buffer, and the pointer points inside the buffer,
885 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500886 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500887 * payload_length == 0 through. In other words, if no packets
888 * use the indirect payload, the iso buffer need not be mapped
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400889 * and the request->data pointer is ignored.
890 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500891
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400892 payload = (unsigned long)request->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200893 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400894 if (request->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200895 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500896 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200897 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500898 }
899
Al Viro1ccc9142007-10-14 19:34:40 +0100900 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
901
902 if (!access_ok(VERIFY_READ, p, request->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500903 return -EFAULT;
904
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400905 end = (void __user *)p + request->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500906 count = 0;
907 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400908 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500909 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400910 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
911 u.packet.interrupt = GET_INTERRUPT(control);
912 u.packet.skip = GET_SKIP(control);
913 u.packet.tag = GET_TAG(control);
914 u.packet.sy = GET_SY(control);
915 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500916
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500917 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500918 header_length = u.packet.header_length;
919 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400920 /*
921 * We require that header_length is a multiple of
922 * the fixed header size, ctx->header_size.
923 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500924 if (ctx->header_size == 0) {
925 if (u.packet.header_length > 0)
926 return -EINVAL;
927 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500928 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500929 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500930 header_length = 0;
931 }
932
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500933 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500934 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500935 if (next > end)
936 return -EINVAL;
937 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500938 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500939 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500940 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500941 u.packet.header_length + u.packet.payload_length > 0)
942 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200943 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500944 return -EINVAL;
945
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500946 if (fw_iso_context_queue(ctx, &u.packet,
947 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500948 break;
949
950 p = next;
951 payload += u.packet.payload_length;
952 count++;
953 }
954
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400955 request->size -= uptr_to_u64(p) - request->packets;
956 request->packets = uptr_to_u64(p);
957 request->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500958
959 return count;
960}
961
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400962static int ioctl_start_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500963{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400964 struct fw_cdev_start_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500965
Stefan Richterfae60312008-02-20 21:10:06 +0100966 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400967 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +0100968
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400969 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400970 if (request->tags == 0 || request->tags > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400971 return -EINVAL;
972
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400973 if (request->sync > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400974 return -EINVAL;
975 }
976
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400977 return fw_iso_context_start(client->iso_context, request->cycle,
978 request->sync, request->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500979}
980
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400981static int ioctl_stop_iso(struct client *client, void *buffer)
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500982{
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400983 struct fw_cdev_stop_iso *request = buffer;
984
Stefan Richterfae60312008-02-20 21:10:06 +0100985 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400986 return -EINVAL;
987
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500988 return fw_iso_context_stop(client->iso_context);
989}
990
Stefan Richtera64408b2007-09-29 10:41:58 +0200991static int ioctl_get_cycle_timer(struct client *client, void *buffer)
992{
993 struct fw_cdev_get_cycle_timer *request = buffer;
994 struct fw_card *card = client->device->card;
995 unsigned long long bus_time;
996 struct timeval tv;
997 unsigned long flags;
998
999 preempt_disable();
1000 local_irq_save(flags);
1001
1002 bus_time = card->driver->get_bus_time(card);
1003 do_gettimeofday(&tv);
1004
1005 local_irq_restore(flags);
1006 preempt_enable();
1007
1008 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
1009 request->cycle_timer = bus_time & 0xffffffff;
1010 return 0;
1011}
1012
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001013static void iso_resource_work(struct work_struct *work)
1014{
1015 struct iso_resource_event *e;
1016 struct iso_resource *r =
1017 container_of(work, struct iso_resource, work.work);
1018 struct client *client = r->client;
1019 int generation, channel, bandwidth, todo;
1020 bool skip, free, success;
1021
1022 spin_lock_irq(&client->lock);
1023 generation = client->device->generation;
1024 todo = r->todo;
1025 /* Allow 1000ms grace period for other reallocations. */
1026 if (todo == ISO_RES_ALLOC &&
1027 time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
1028 if (schedule_delayed_work(&r->work, DIV_ROUND_UP(HZ, 3)))
1029 client_get(client);
1030 skip = true;
1031 } else {
1032 /* We could be called twice within the same generation. */
1033 skip = todo == ISO_RES_REALLOC &&
1034 r->generation == generation;
1035 }
1036 free = todo == ISO_RES_DEALLOC;
1037 r->generation = generation;
1038 spin_unlock_irq(&client->lock);
1039
1040 if (skip)
1041 goto out;
1042
1043 bandwidth = r->bandwidth;
1044
1045 fw_iso_resource_manage(client->device->card, generation,
1046 r->channels, &channel, &bandwidth,
1047 todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC);
1048 /*
1049 * Is this generation outdated already? As long as this resource sticks
1050 * in the idr, it will be scheduled again for a newer generation or at
1051 * shutdown.
1052 */
1053 if (channel == -EAGAIN &&
1054 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1055 goto out;
1056
1057 success = channel >= 0 || bandwidth > 0;
1058
1059 spin_lock_irq(&client->lock);
1060 /*
1061 * Transit from allocation to reallocation, except if the client
1062 * requested deallocation in the meantime.
1063 */
1064 if (r->todo == ISO_RES_ALLOC)
1065 r->todo = ISO_RES_REALLOC;
1066 /*
1067 * Allocation or reallocation failure? Pull this resource out of the
1068 * idr and prepare for deletion, unless the client is shutting down.
1069 */
1070 if (r->todo == ISO_RES_REALLOC && !success &&
1071 !client->in_shutdown &&
1072 idr_find(&client->resource_idr, r->resource.handle)) {
1073 idr_remove(&client->resource_idr, r->resource.handle);
1074 client_put(client);
1075 free = true;
1076 }
1077 spin_unlock_irq(&client->lock);
1078
1079 if (todo == ISO_RES_ALLOC && channel >= 0)
1080 r->channels = 1ULL << (63 - channel);
1081
1082 if (todo == ISO_RES_REALLOC && success)
1083 goto out;
1084
1085 if (todo == ISO_RES_ALLOC) {
1086 e = r->e_alloc;
1087 r->e_alloc = NULL;
1088 } else {
1089 e = r->e_dealloc;
1090 r->e_dealloc = NULL;
1091 }
1092 e->resource.handle = r->resource.handle;
1093 e->resource.channel = channel;
1094 e->resource.bandwidth = bandwidth;
1095
1096 queue_event(client, &e->event,
1097 &e->resource, sizeof(e->resource), NULL, 0);
1098
1099 if (free) {
1100 cancel_delayed_work(&r->work);
1101 kfree(r->e_alloc);
1102 kfree(r->e_dealloc);
1103 kfree(r);
1104 }
1105 out:
1106 client_put(client);
1107}
1108
1109static void schedule_iso_resource(struct iso_resource *r)
1110{
1111 if (schedule_delayed_work(&r->work, 0))
1112 client_get(r->client);
1113}
1114
1115static void release_iso_resource(struct client *client,
1116 struct client_resource *resource)
1117{
1118 struct iso_resource *r =
1119 container_of(resource, struct iso_resource, resource);
1120
1121 spin_lock_irq(&client->lock);
1122 r->todo = ISO_RES_DEALLOC;
1123 schedule_iso_resource(r);
1124 spin_unlock_irq(&client->lock);
1125}
1126
1127static int ioctl_allocate_iso_resource(struct client *client, void *buffer)
1128{
1129 struct fw_cdev_allocate_iso_resource *request = buffer;
1130 struct iso_resource_event *e1, *e2;
1131 struct iso_resource *r;
1132 int ret;
1133
1134 if ((request->channels == 0 && request->bandwidth == 0) ||
1135 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1136 request->bandwidth < 0)
1137 return -EINVAL;
1138
1139 r = kmalloc(sizeof(*r), GFP_KERNEL);
1140 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1141 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1142 if (r == NULL || e1 == NULL || e2 == NULL) {
1143 ret = -ENOMEM;
1144 goto fail;
1145 }
1146
1147 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1148 r->client = client;
1149 r->todo = ISO_RES_ALLOC;
1150 r->generation = -1;
1151 r->channels = request->channels;
1152 r->bandwidth = request->bandwidth;
1153 r->e_alloc = e1;
1154 r->e_dealloc = e2;
1155
1156 e1->resource.closure = request->closure;
1157 e1->resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1158 e2->resource.closure = request->closure;
1159 e2->resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
1160
1161 r->resource.release = release_iso_resource;
1162 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
1163 if (ret < 0)
1164 goto fail;
1165 request->handle = r->resource.handle;
1166
1167 return 0;
1168 fail:
1169 kfree(r);
1170 kfree(e1);
1171 kfree(e2);
1172
1173 return ret;
1174}
1175
1176static int ioctl_deallocate_iso_resource(struct client *client, void *buffer)
1177{
1178 struct fw_cdev_deallocate *request = buffer;
1179
1180 return release_client_resource(client, request->handle,
1181 release_iso_resource, NULL);
1182}
1183
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001184static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
1185 ioctl_get_info,
1186 ioctl_send_request,
1187 ioctl_allocate,
1188 ioctl_deallocate,
1189 ioctl_send_response,
1190 ioctl_initiate_bus_reset,
1191 ioctl_add_descriptor,
1192 ioctl_remove_descriptor,
1193 ioctl_create_iso_context,
1194 ioctl_queue_iso,
1195 ioctl_start_iso,
1196 ioctl_stop_iso,
Stefan Richtera64408b2007-09-29 10:41:58 +02001197 ioctl_get_cycle_timer,
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001198 ioctl_allocate_iso_resource,
1199 ioctl_deallocate_iso_resource,
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001200};
1201
Stefan Richter53dca512008-12-14 21:47:04 +01001202static int dispatch_ioctl(struct client *client,
1203 unsigned int cmd, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001204{
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001205 char buffer[256];
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001206 int ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001207
1208 if (_IOC_TYPE(cmd) != '#' ||
1209 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001210 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001211
1212 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001213 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001214 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
1215 return -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001216 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001217
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001218 ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
1219 if (ret < 0)
1220 return ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001221
1222 if (_IOC_DIR(cmd) & _IOC_READ) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001223 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001224 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
1225 return -EFAULT;
1226 }
1227
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001228 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001229}
1230
Stefan Richter53dca512008-12-14 21:47:04 +01001231static long fw_device_op_ioctl(struct file *file,
1232 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001233{
1234 struct client *client = file->private_data;
1235
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001236 if (fw_device_is_shutdown(client->device))
1237 return -ENODEV;
1238
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001239 return dispatch_ioctl(client, cmd, (void __user *) arg);
1240}
1241
1242#ifdef CONFIG_COMPAT
Stefan Richter53dca512008-12-14 21:47:04 +01001243static long fw_device_op_compat_ioctl(struct file *file,
1244 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001245{
1246 struct client *client = file->private_data;
1247
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001248 if (fw_device_is_shutdown(client->device))
1249 return -ENODEV;
1250
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001251 return dispatch_ioctl(client, cmd, compat_ptr(arg));
1252}
1253#endif
1254
1255static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1256{
1257 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001258 enum dma_data_direction direction;
1259 unsigned long size;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001260 int page_count, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001261
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001262 if (fw_device_is_shutdown(client->device))
1263 return -ENODEV;
1264
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001265 /* FIXME: We could support multiple buffers, but we don't. */
1266 if (client->buffer.pages != NULL)
1267 return -EBUSY;
1268
1269 if (!(vma->vm_flags & VM_SHARED))
1270 return -EINVAL;
1271
1272 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001273 return -EINVAL;
1274
1275 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001276 size = vma->vm_end - vma->vm_start;
1277 page_count = size >> PAGE_SHIFT;
1278 if (size & ~PAGE_MASK)
1279 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001280
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001281 if (vma->vm_flags & VM_WRITE)
1282 direction = DMA_TO_DEVICE;
1283 else
1284 direction = DMA_FROM_DEVICE;
1285
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001286 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1287 page_count, direction);
1288 if (ret < 0)
1289 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001290
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001291 ret = fw_iso_buffer_map(&client->buffer, vma);
1292 if (ret < 0)
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001293 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1294
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001295 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001296}
1297
Jay Fenlason45ee3192008-12-21 16:47:17 +01001298static int shutdown_resource(int id, void *p, void *data)
1299{
1300 struct client_resource *r = p;
1301 struct client *client = data;
1302
1303 r->release(client, r);
Stefan Richterfb443032009-01-04 16:23:29 +01001304 client_put(client);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001305
1306 return 0;
1307}
1308
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001309static int fw_device_op_release(struct inode *inode, struct file *file)
1310{
1311 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001312 struct event *e, *next_e;
Jay Fenlason45ee3192008-12-21 16:47:17 +01001313 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001314
Stefan Richter97811e32008-12-14 19:19:23 +01001315 mutex_lock(&client->device->client_list_mutex);
1316 list_del(&client->link);
1317 mutex_unlock(&client->device->client_list_mutex);
1318
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001319 if (client->buffer.pages)
1320 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1321
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001322 if (client->iso_context)
1323 fw_iso_context_destroy(client->iso_context);
1324
Jay Fenlason45ee3192008-12-21 16:47:17 +01001325 /* Freeze client->resource_idr and client->event_list */
1326 spin_lock_irqsave(&client->lock, flags);
1327 client->in_shutdown = true;
1328 spin_unlock_irqrestore(&client->lock, flags);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001329
Jay Fenlason45ee3192008-12-21 16:47:17 +01001330 idr_for_each(&client->resource_idr, shutdown_resource, client);
1331 idr_remove_all(&client->resource_idr);
1332 idr_destroy(&client->resource_idr);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001333
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001334 list_for_each_entry_safe(e, next_e, &client->event_list, link)
1335 kfree(e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001336
Stefan Richterfb443032009-01-04 16:23:29 +01001337 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001338
1339 return 0;
1340}
1341
1342static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1343{
1344 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001345 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001346
1347 poll_wait(file, &client->wait, pt);
1348
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001349 if (fw_device_is_shutdown(client->device))
1350 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001351 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001352 mask |= POLLIN | POLLRDNORM;
1353
1354 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001355}
1356
Stefan Richter21ebcd12007-01-14 15:29:07 +01001357const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001358 .owner = THIS_MODULE,
1359 .open = fw_device_op_open,
1360 .read = fw_device_op_read,
1361 .unlocked_ioctl = fw_device_op_ioctl,
1362 .poll = fw_device_op_poll,
1363 .release = fw_device_op_release,
1364 .mmap = fw_device_op_mmap,
1365
1366#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001367 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001368#endif
1369};