blob: 231e6ee5ba4397c6aa511ad8ff2df4f54129799b [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>
Stefan Richter77c9a5d2009-06-05 16:26:18 +020025#include <linux/firewire.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010026#include <linux/firewire-cdev.h>
27#include <linux/idr.h>
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +010028#include <linux/jiffies.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050029#include <linux/kernel.h>
Stefan Richterfb443032009-01-04 16:23:29 +010030#include <linux/kref.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010031#include <linux/mm.h>
32#include <linux/module.h>
Stefan Richterd67cfb92008-10-05 10:37:11 +020033#include <linux/mutex.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050034#include <linux/poll.h>
Stefan Richtera64408b2007-09-29 10:41:58 +020035#include <linux/preempt.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040036#include <linux/sched.h>
Jay Fenlasoncf417e542008-10-03 11:19:09 -040037#include <linux/spinlock.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010038#include <linux/time.h>
Stefan Richtere034d242009-06-06 18:36:24 +020039#include <linux/uaccess.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010040#include <linux/vmalloc.h>
41#include <linux/wait.h>
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +010042#include <linux/workqueue.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010043
Stefan Richtera64408b2007-09-29 10:41:58 +020044#include <asm/system.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010045
Stefan Richter77c9a5d2009-06-05 16:26:18 +020046#include "core.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;
Stefan Richter1ec3c022009-01-04 16:23:29 +0100124 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
125 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100126 int generation;
127 u64 channels;
128 s32 bandwidth;
Stefan Richter6fdc0372009-06-20 13:23:59 +0200129 __be32 transaction_data[2];
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100130 struct iso_resource_event *e_alloc, *e_dealloc;
131};
132
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100133static void release_iso_resource(struct client *, struct client_resource *);
134
Stefan Richter9fb551b2009-10-08 00:41:10 +0200135static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
136{
137 client_get(r->client);
138 if (!schedule_delayed_work(&r->work, delay))
139 client_put(r->client);
140}
141
142static void schedule_if_iso_resource(struct client_resource *resource)
143{
144 if (resource->release == release_iso_resource)
145 schedule_iso_resource(container_of(resource,
146 struct iso_resource, resource), 0);
147}
148
Stefan Richter97c18b72009-01-04 16:23:29 +0100149/*
150 * dequeue_event() just kfree()'s the event, so the event has to be
151 * the first field in a struct XYZ_event.
152 */
153struct event {
154 struct { void *data; size_t size; } v[2];
155 struct list_head link;
156};
157
158struct bus_reset_event {
159 struct event event;
160 struct fw_cdev_event_bus_reset reset;
161};
162
163struct outbound_transaction_event {
164 struct event event;
165 struct client *client;
166 struct outbound_transaction_resource r;
167 struct fw_cdev_event_response response;
168};
169
170struct inbound_transaction_event {
171 struct event event;
172 struct fw_cdev_event_request request;
173};
174
175struct iso_interrupt_event {
176 struct event event;
177 struct fw_cdev_event_iso_interrupt interrupt;
178};
179
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100180struct iso_resource_event {
181 struct event event;
Stefan Richtere21fcf72009-10-08 00:41:38 +0200182 struct fw_cdev_event_iso_resource iso_resource;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100183};
184
Stefan Richter53dca512008-12-14 21:47:04 +0100185static inline void __user *u64_to_uptr(__u64 value)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500186{
187 return (void __user *)(unsigned long)value;
188}
189
Stefan Richter53dca512008-12-14 21:47:04 +0100190static inline __u64 uptr_to_u64(void __user *ptr)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500191{
192 return (__u64)(unsigned long)ptr;
193}
194
195static int fw_device_op_open(struct inode *inode, struct file *file)
196{
197 struct fw_device *device;
198 struct client *client;
199
Stefan Richter96b19062008-02-02 15:01:09 +0100200 device = fw_device_get_by_devt(inode->i_rdev);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500201 if (device == NULL)
202 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500203
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400204 if (fw_device_is_shutdown(device)) {
205 fw_device_put(device);
206 return -ENODEV;
207 }
208
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400209 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100210 if (client == NULL) {
211 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500212 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100213 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500214
Stefan Richter96b19062008-02-02 15:01:09 +0100215 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500216 spin_lock_init(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100217 idr_init(&client->resource_idr);
218 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500219 init_waitqueue_head(&client->wait);
Stefan Richterfb443032009-01-04 16:23:29 +0100220 kref_init(&client->kref);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500221
222 file->private_data = client;
223
Stefan Richterd67cfb92008-10-05 10:37:11 +0200224 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500225 list_add_tail(&client->link, &device->client_list);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200226 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500227
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500228 return 0;
229}
230
231static void queue_event(struct client *client, struct event *event,
232 void *data0, size_t size0, void *data1, size_t size1)
233{
234 unsigned long flags;
235
236 event->v[0].data = data0;
237 event->v[0].size = size0;
238 event->v[1].data = data1;
239 event->v[1].size = size1;
240
241 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100242 if (client->in_shutdown)
243 kfree(event);
244 else
245 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500246 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400247
248 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500249}
250
Stefan Richter53dca512008-12-14 21:47:04 +0100251static int dequeue_event(struct client *client,
252 char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500253{
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500254 struct event *event;
255 size_t size, total;
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100256 int i, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500257
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100258 ret = wait_event_interruptible(client->wait,
259 !list_empty(&client->event_list) ||
260 fw_device_is_shutdown(client->device));
261 if (ret < 0)
262 return ret;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500263
264 if (list_empty(&client->event_list) &&
265 fw_device_is_shutdown(client->device))
266 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500267
Stefan Richter3ba94982009-01-04 16:23:29 +0100268 spin_lock_irq(&client->lock);
Stefan Richtera459b8a2008-12-21 16:49:57 +0100269 event = list_first_entry(&client->event_list, struct event, link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500270 list_del(&event->link);
Stefan Richter3ba94982009-01-04 16:23:29 +0100271 spin_unlock_irq(&client->lock);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500272
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500273 total = 0;
274 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
275 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500276 if (copy_to_user(buffer + total, event->v[i].data, size)) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100277 ret = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500278 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500279 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500280 total += size;
281 }
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100282 ret = total;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500283
284 out:
285 kfree(event);
286
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100287 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500288}
289
Stefan Richter53dca512008-12-14 21:47:04 +0100290static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
291 size_t count, loff_t *offset)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500292{
293 struct client *client = file->private_data;
294
295 return dequeue_event(client, buffer, count);
296}
297
Stefan Richter53dca512008-12-14 21:47:04 +0100298static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
299 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500300{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400301 struct fw_card *card = client->device->card;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400302
Stefan Richter3ba94982009-01-04 16:23:29 +0100303 spin_lock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500304
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400305 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500306 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100307 event->generation = client->device->generation;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400308 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500309 event->local_node_id = card->local_node->node_id;
310 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
311 event->irm_node_id = card->irm_node->node_id;
312 event->root_node_id = card->root_node->node_id;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400313
Stefan Richter3ba94982009-01-04 16:23:29 +0100314 spin_unlock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500315}
316
Stefan Richter53dca512008-12-14 21:47:04 +0100317static void for_each_client(struct fw_device *device,
318 void (*callback)(struct client *client))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500319{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500320 struct client *c;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500321
Stefan Richterd67cfb92008-10-05 10:37:11 +0200322 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500323 list_for_each_entry(c, &device->client_list, link)
324 callback(c);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200325 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500326}
327
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100328static int schedule_reallocations(int id, void *p, void *data)
329{
Stefan Richter9fb551b2009-10-08 00:41:10 +0200330 schedule_if_iso_resource(p);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100331
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100332 return 0;
333}
334
Stefan Richter53dca512008-12-14 21:47:04 +0100335static void queue_bus_reset_event(struct client *client)
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500336{
Stefan Richter97c18b72009-01-04 16:23:29 +0100337 struct bus_reset_event *e;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500338
Stefan Richter97c18b72009-01-04 16:23:29 +0100339 e = kzalloc(sizeof(*e), GFP_KERNEL);
340 if (e == NULL) {
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500341 fw_notify("Out of memory when allocating bus reset event\n");
342 return;
343 }
344
Stefan Richter97c18b72009-01-04 16:23:29 +0100345 fill_bus_reset_event(&e->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500346
Stefan Richter97c18b72009-01-04 16:23:29 +0100347 queue_event(client, &e->event,
348 &e->reset, sizeof(e->reset), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100349
350 spin_lock_irq(&client->lock);
351 idr_for_each(&client->resource_idr, schedule_reallocations, client);
352 spin_unlock_irq(&client->lock);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500353}
354
355void fw_device_cdev_update(struct fw_device *device)
356{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500357 for_each_client(device, queue_bus_reset_event);
358}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500359
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500360static void wake_up_client(struct client *client)
361{
362 wake_up_interruptible(&client->wait);
363}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500364
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500365void fw_device_cdev_remove(struct fw_device *device)
366{
367 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500368}
369
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400370static int ioctl_get_info(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500371{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400372 struct fw_cdev_get_info *get_info = buffer;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500373 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100374 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500375
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400376 client->version = get_info->version;
377 get_info->version = FW_CDEV_VERSION;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400378 get_info->card = client->device->card->index;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500379
Stefan Richterc9755e12008-03-24 20:54:28 +0100380 down_read(&fw_device_rwsem);
381
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400382 if (get_info->rom != 0) {
383 void __user *uptr = u64_to_uptr(get_info->rom);
384 size_t want = get_info->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100385 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500386
Stefan Richterc9755e12008-03-24 20:54:28 +0100387 ret = copy_to_user(uptr, client->device->config_rom,
388 min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500389 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400390 get_info->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500391
Stefan Richterc9755e12008-03-24 20:54:28 +0100392 up_read(&fw_device_rwsem);
393
394 if (ret != 0)
395 return -EFAULT;
396
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400397 client->bus_reset_closure = get_info->bus_reset_closure;
398 if (get_info->bus_reset != 0) {
399 void __user *uptr = u64_to_uptr(get_info->bus_reset);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500400
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400401 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400402 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500403 return -EFAULT;
404 }
405
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500406 return 0;
407}
408
Stefan Richter53dca512008-12-14 21:47:04 +0100409static int add_client_resource(struct client *client,
410 struct client_resource *resource, gfp_t gfp_mask)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400411{
412 unsigned long flags;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100413 int ret;
414
415 retry:
416 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
417 return -ENOMEM;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400418
419 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100420 if (client->in_shutdown)
421 ret = -ECANCELED;
422 else
423 ret = idr_get_new(&client->resource_idr, resource,
424 &resource->handle);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100425 if (ret >= 0) {
Stefan Richterfb443032009-01-04 16:23:29 +0100426 client_get(client);
Stefan Richter9fb551b2009-10-08 00:41:10 +0200427 schedule_if_iso_resource(resource);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100428 }
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400429 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100430
431 if (ret == -EAGAIN)
432 goto retry;
433
434 return ret < 0 ? ret : 0;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400435}
436
Stefan Richter53dca512008-12-14 21:47:04 +0100437static int release_client_resource(struct client *client, u32 handle,
438 client_resource_release_fn_t release,
Stefan Richtere21fcf72009-10-08 00:41:38 +0200439 struct client_resource **return_resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400440{
Stefan Richtere21fcf72009-10-08 00:41:38 +0200441 struct client_resource *resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400442
Stefan Richter3ba94982009-01-04 16:23:29 +0100443 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100444 if (client->in_shutdown)
Stefan Richtere21fcf72009-10-08 00:41:38 +0200445 resource = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100446 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200447 resource = idr_find(&client->resource_idr, handle);
448 if (resource && resource->release == release)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100449 idr_remove(&client->resource_idr, handle);
Stefan Richter3ba94982009-01-04 16:23:29 +0100450 spin_unlock_irq(&client->lock);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400451
Stefan Richtere21fcf72009-10-08 00:41:38 +0200452 if (!(resource && resource->release == release))
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400453 return -EINVAL;
454
Stefan Richtere21fcf72009-10-08 00:41:38 +0200455 if (return_resource)
456 *return_resource = resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400457 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200458 resource->release(client, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400459
Stefan Richterfb443032009-01-04 16:23:29 +0100460 client_put(client);
461
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400462 return 0;
463}
464
Stefan Richter53dca512008-12-14 21:47:04 +0100465static void release_transaction(struct client *client,
466 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400467{
Stefan Richter97c18b72009-01-04 16:23:29 +0100468 struct outbound_transaction_resource *r = container_of(resource,
469 struct outbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400470
Stefan Richter97c18b72009-01-04 16:23:29 +0100471 fw_cancel_transaction(client->device->card, &r->transaction);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400472}
473
Stefan Richter53dca512008-12-14 21:47:04 +0100474static void complete_transaction(struct fw_card *card, int rcode,
475 void *payload, size_t length, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500476{
Stefan Richter97c18b72009-01-04 16:23:29 +0100477 struct outbound_transaction_event *e = data;
478 struct fw_cdev_event_response *rsp = &e->response;
479 struct client *client = e->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500480 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500481
Stefan Richter97c18b72009-01-04 16:23:29 +0100482 if (length < rsp->length)
483 rsp->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500484 if (rcode == RCODE_COMPLETE)
Stefan Richter97c18b72009-01-04 16:23:29 +0100485 memcpy(rsp->data, payload, rsp->length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500486
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500487 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100488 /*
Stefan Richterfb443032009-01-04 16:23:29 +0100489 * 1. If called while in shutdown, the idr tree must be left untouched.
490 * The idr handle will be removed and the client reference will be
491 * dropped later.
492 * 2. If the call chain was release_client_resource ->
493 * release_transaction -> complete_transaction (instead of a normal
494 * conclusion of the transaction), i.e. if this resource was already
495 * unregistered from the idr, the client reference will be dropped
496 * by release_client_resource and we must not drop it here.
Jay Fenlason45ee3192008-12-21 16:47:17 +0100497 */
Stefan Richterfb443032009-01-04 16:23:29 +0100498 if (!client->in_shutdown &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100499 idr_find(&client->resource_idr, e->r.resource.handle)) {
500 idr_remove(&client->resource_idr, e->r.resource.handle);
Stefan Richterfb443032009-01-04 16:23:29 +0100501 /* Drop the idr's reference */
502 client_put(client);
503 }
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500504 spin_unlock_irqrestore(&client->lock, flags);
505
Stefan Richter97c18b72009-01-04 16:23:29 +0100506 rsp->type = FW_CDEV_EVENT_RESPONSE;
507 rsp->rcode = rcode;
David Moore8401d922008-07-29 23:46:25 -0700508
509 /*
Stefan Richter97c18b72009-01-04 16:23:29 +0100510 * In the case that sizeof(*rsp) doesn't align with the position of the
David Moore8401d922008-07-29 23:46:25 -0700511 * data, and the read is short, preserve an extra copy of the data
512 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
513 * for short reads and some apps depended on it, this is both safe
514 * and prudent for compatibility.
515 */
Stefan Richter97c18b72009-01-04 16:23:29 +0100516 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
517 queue_event(client, &e->event, rsp, sizeof(*rsp),
518 rsp->data, rsp->length);
David Moore8401d922008-07-29 23:46:25 -0700519 else
Stefan Richter97c18b72009-01-04 16:23:29 +0100520 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
David Moore8401d922008-07-29 23:46:25 -0700521 NULL, 0);
Stefan Richterfb443032009-01-04 16:23:29 +0100522
523 /* Drop the transaction callback's reference */
524 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500525}
526
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100527static int init_request(struct client *client,
528 struct fw_cdev_send_request *request,
529 int destination_id, int speed)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500530{
Stefan Richter97c18b72009-01-04 16:23:29 +0100531 struct outbound_transaction_event *e;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100532 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500533
Stefan Richter18e9b102009-03-10 21:02:21 +0100534 if (request->tcode != TCODE_STREAM_DATA &&
535 (request->length > 4096 || request->length > 512 << speed))
Stefan Richter5d3fd692009-01-04 16:23:29 +0100536 return -EIO;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500537
Stefan Richter97c18b72009-01-04 16:23:29 +0100538 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
539 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500540 return -ENOMEM;
541
Stefan Richter97c18b72009-01-04 16:23:29 +0100542 e->client = client;
543 e->response.length = request->length;
544 e->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500545
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400546 if (request->data &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100547 copy_from_user(e->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400548 u64_to_uptr(request->data), request->length)) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100549 ret = -EFAULT;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100550 goto failed;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100551 }
552
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100553 e->r.resource.release = release_transaction;
554 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
555 if (ret < 0)
556 goto failed;
557
558 /* Get a reference for the transaction callback */
559 client_get(client);
560
561 fw_send_request(client->device->card, &e->r.transaction,
Stefan Richter664d8012009-03-10 21:01:54 +0100562 request->tcode, destination_id, request->generation,
563 speed, request->offset, e->response.data,
564 request->length, complete_transaction, e);
565 return 0;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100566
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100567 failed:
568 kfree(e);
569
570 return ret;
571}
572
573static int ioctl_send_request(struct client *client, void *buffer)
574{
575 struct fw_cdev_send_request *request = buffer;
576
Stefan Richter1f3125a2008-12-05 22:44:42 +0100577 switch (request->tcode) {
578 case TCODE_WRITE_QUADLET_REQUEST:
579 case TCODE_WRITE_BLOCK_REQUEST:
580 case TCODE_READ_QUADLET_REQUEST:
581 case TCODE_READ_BLOCK_REQUEST:
582 case TCODE_LOCK_MASK_SWAP:
583 case TCODE_LOCK_COMPARE_SWAP:
584 case TCODE_LOCK_FETCH_ADD:
585 case TCODE_LOCK_LITTLE_ADD:
586 case TCODE_LOCK_BOUNDED_ADD:
587 case TCODE_LOCK_WRAP_ADD:
588 case TCODE_LOCK_VENDOR_DEPENDENT:
589 break;
590 default:
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100591 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500592 }
593
Stefan Richter207fbef2009-03-10 21:01:08 +0100594 return init_request(client, request, client->device->node_id,
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100595 client->device->max_speed);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500596}
597
Stefan Richter53dca512008-12-14 21:47:04 +0100598static void release_request(struct client *client,
599 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400600{
Stefan Richter97c18b72009-01-04 16:23:29 +0100601 struct inbound_transaction_resource *r = container_of(resource,
602 struct inbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400603
Stefan Richter97c18b72009-01-04 16:23:29 +0100604 fw_send_response(client->device->card, r->request,
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400605 RCODE_CONFLICT_ERROR);
Stefan Richter97c18b72009-01-04 16:23:29 +0100606 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400607}
608
Stefan Richter97c18b72009-01-04 16:23:29 +0100609static void handle_request(struct fw_card *card, struct fw_request *request,
Stefan Richter53dca512008-12-14 21:47:04 +0100610 int tcode, int destination, int source,
611 int generation, int speed,
612 unsigned long long offset,
613 void *payload, size_t length, void *callback_data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500614{
Stefan Richter97c18b72009-01-04 16:23:29 +0100615 struct address_handler_resource *handler = callback_data;
616 struct inbound_transaction_resource *r;
617 struct inbound_transaction_event *e;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100618 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500619
Stefan Richter97c18b72009-01-04 16:23:29 +0100620 r = kmalloc(sizeof(*r), GFP_ATOMIC);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400621 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Stefan Richter97c18b72009-01-04 16:23:29 +0100622 if (r == NULL || e == NULL)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100623 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500624
Stefan Richter97c18b72009-01-04 16:23:29 +0100625 r->request = request;
626 r->data = payload;
627 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500628
Stefan Richter97c18b72009-01-04 16:23:29 +0100629 r->resource.release = release_request;
630 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100631 if (ret < 0)
632 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500633
634 e->request.type = FW_CDEV_EVENT_REQUEST;
635 e->request.tcode = tcode;
636 e->request.offset = offset;
637 e->request.length = length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100638 e->request.handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500639 e->request.closure = handler->closure;
640
Stefan Richter97c18b72009-01-04 16:23:29 +0100641 queue_event(handler->client, &e->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400642 &e->request, sizeof(e->request), payload, length);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100643 return;
644
645 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100646 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100647 kfree(e);
Stefan Richter97c18b72009-01-04 16:23:29 +0100648 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500649}
650
Stefan Richter53dca512008-12-14 21:47:04 +0100651static void release_address_handler(struct client *client,
652 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400653{
Stefan Richter97c18b72009-01-04 16:23:29 +0100654 struct address_handler_resource *r =
655 container_of(resource, struct address_handler_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400656
Stefan Richter97c18b72009-01-04 16:23:29 +0100657 fw_core_remove_address_handler(&r->handler);
658 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400659}
660
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400661static int ioctl_allocate(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500662{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400663 struct fw_cdev_allocate *request = buffer;
Stefan Richter97c18b72009-01-04 16:23:29 +0100664 struct address_handler_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500665 struct fw_address_region region;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100666 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500667
Stefan Richter97c18b72009-01-04 16:23:29 +0100668 r = kmalloc(sizeof(*r), GFP_KERNEL);
669 if (r == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500670 return -ENOMEM;
671
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400672 region.start = request->offset;
673 region.end = request->offset + request->length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100674 r->handler.length = request->length;
675 r->handler.address_callback = handle_request;
676 r->handler.callback_data = r;
677 r->closure = request->closure;
678 r->client = client;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500679
Stefan Richter97c18b72009-01-04 16:23:29 +0100680 ret = fw_core_add_address_handler(&r->handler, &region);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100681 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100682 kfree(r);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100683 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500684 }
685
Stefan Richter97c18b72009-01-04 16:23:29 +0100686 r->resource.release = release_address_handler;
687 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100688 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100689 release_address_handler(client, &r->resource);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100690 return ret;
691 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100692 request->handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500693
694 return 0;
695}
696
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400697static int ioctl_deallocate(struct client *client, void *buffer)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400698{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400699 struct fw_cdev_deallocate *request = buffer;
Kristian Høgsberg94723162007-03-14 17:34:55 -0400700
Jay Fenlason45ee3192008-12-21 16:47:17 +0100701 return release_client_resource(client, request->handle,
702 release_address_handler, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400703}
704
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400705static int ioctl_send_response(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500706{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400707 struct fw_cdev_send_response *request = buffer;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400708 struct client_resource *resource;
Stefan Richter97c18b72009-01-04 16:23:29 +0100709 struct inbound_transaction_resource *r;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200710 int ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500711
Jay Fenlason45ee3192008-12-21 16:47:17 +0100712 if (release_client_resource(client, request->handle,
713 release_request, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500714 return -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100715
Stefan Richter97c18b72009-01-04 16:23:29 +0100716 r = container_of(resource, struct inbound_transaction_resource,
717 resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400718 if (request->length < r->length)
719 r->length = request->length;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200720
721 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length)) {
722 ret = -EFAULT;
723 goto out;
724 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500725
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400726 fw_send_response(client->device->card, r->request, request->rcode);
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200727 out:
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500728 kfree(r);
729
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200730 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500731}
732
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400733static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500734{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400735 struct fw_cdev_initiate_bus_reset *request = buffer;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500736 int short_reset;
737
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400738 short_reset = (request->type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500739
740 return fw_core_initiate_bus_reset(client->device->card, short_reset);
741}
742
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400743static void release_descriptor(struct client *client,
744 struct client_resource *resource)
745{
Stefan Richter97c18b72009-01-04 16:23:29 +0100746 struct descriptor_resource *r =
747 container_of(resource, struct descriptor_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400748
Stefan Richter97c18b72009-01-04 16:23:29 +0100749 fw_core_remove_descriptor(&r->descriptor);
750 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400751}
752
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400753static int ioctl_add_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200754{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400755 struct fw_cdev_add_descriptor *request = buffer;
Stefan Richter97c18b72009-01-04 16:23:29 +0100756 struct descriptor_resource *r;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100757 int ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200758
Stefan Richterde487da2009-03-10 21:00:23 +0100759 /* Access policy: Allow this ioctl only on local nodes' device files. */
Stefan Richter92368892009-05-13 21:42:14 +0200760 if (!client->device->is_local)
Stefan Richterde487da2009-03-10 21:00:23 +0100761 return -ENOSYS;
762
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400763 if (request->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200764 return -EINVAL;
765
Stefan Richter97c18b72009-01-04 16:23:29 +0100766 r = kmalloc(sizeof(*r) + request->length * 4, GFP_KERNEL);
767 if (r == NULL)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200768 return -ENOMEM;
769
Stefan Richter97c18b72009-01-04 16:23:29 +0100770 if (copy_from_user(r->data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400771 u64_to_uptr(request->data), request->length * 4)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100772 ret = -EFAULT;
773 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200774 }
775
Stefan Richter97c18b72009-01-04 16:23:29 +0100776 r->descriptor.length = request->length;
777 r->descriptor.immediate = request->immediate;
778 r->descriptor.key = request->key;
779 r->descriptor.data = r->data;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200780
Stefan Richter97c18b72009-01-04 16:23:29 +0100781 ret = fw_core_add_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100782 if (ret < 0)
783 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200784
Stefan Richter97c18b72009-01-04 16:23:29 +0100785 r->resource.release = release_descriptor;
786 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100787 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100788 fw_core_remove_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100789 goto failed;
790 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100791 request->handle = r->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200792
793 return 0;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100794 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100795 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100796
797 return ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200798}
799
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400800static int ioctl_remove_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200801{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400802 struct fw_cdev_remove_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200803
Jay Fenlason45ee3192008-12-21 16:47:17 +0100804 return release_client_resource(client, request->handle,
805 release_descriptor, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200806}
807
Stefan Richter53dca512008-12-14 21:47:04 +0100808static void iso_callback(struct fw_iso_context *context, u32 cycle,
809 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500810{
811 struct client *client = data;
Stefan Richter97c18b72009-01-04 16:23:29 +0100812 struct iso_interrupt_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500813
Stefan Richter97c18b72009-01-04 16:23:29 +0100814 e = kzalloc(sizeof(*e) + header_length, GFP_ATOMIC);
815 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500816 return;
817
Stefan Richter97c18b72009-01-04 16:23:29 +0100818 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
819 e->interrupt.closure = client->iso_closure;
820 e->interrupt.cycle = cycle;
821 e->interrupt.header_length = header_length;
822 memcpy(e->interrupt.header, header, header_length);
823 queue_event(client, &e->event, &e->interrupt,
824 sizeof(e->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500825}
826
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400827static int ioctl_create_iso_context(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500828{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400829 struct fw_cdev_create_iso_context *request = buffer;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400830 struct fw_iso_context *context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500831
Stefan Richterfae60312008-02-20 21:10:06 +0100832 /* We only support one context at this time. */
833 if (client->iso_context != NULL)
834 return -EBUSY;
835
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400836 if (request->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500837 return -EINVAL;
838
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400839 switch (request->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400840 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400841 if (request->header_size < 4 || (request->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400842 return -EINVAL;
843
844 break;
845
846 case FW_ISO_CONTEXT_TRANSMIT:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400847 if (request->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400848 return -EINVAL;
849
850 break;
851
852 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500853 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400854 }
855
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400856 context = fw_iso_context_create(client->device->card,
857 request->type,
858 request->channel,
859 request->speed,
860 request->header_size,
861 iso_callback, client);
862 if (IS_ERR(context))
863 return PTR_ERR(context);
864
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400865 client->iso_closure = request->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400866 client->iso_context = context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500867
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400868 /* We only support one context at this time. */
869 request->handle = 0;
870
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500871 return 0;
872}
873
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400874/* Macros for decoding the iso packet control header. */
875#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
876#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
877#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +0200878#define GET_TAG(v) (((v) >> 18) & 0x03)
879#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400880#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
881
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400882static int ioctl_queue_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500883{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400884 struct fw_cdev_queue_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500885 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500886 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200887 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400888 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500889 int count;
890 struct {
891 struct fw_iso_packet packet;
892 u8 header[256];
893 } u;
894
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400895 if (ctx == NULL || request->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500896 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500897
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400898 /*
899 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500900 * the iso buffer, and the pointer points inside the buffer,
901 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500902 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500903 * payload_length == 0 through. In other words, if no packets
904 * use the indirect payload, the iso buffer need not be mapped
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400905 * and the request->data pointer is ignored.
906 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500907
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400908 payload = (unsigned long)request->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200909 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400910 if (request->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200911 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500912 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200913 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500914 }
915
Al Viro1ccc9142007-10-14 19:34:40 +0100916 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
917
918 if (!access_ok(VERIFY_READ, p, request->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500919 return -EFAULT;
920
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400921 end = (void __user *)p + request->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500922 count = 0;
923 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400924 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500925 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400926 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
927 u.packet.interrupt = GET_INTERRUPT(control);
928 u.packet.skip = GET_SKIP(control);
929 u.packet.tag = GET_TAG(control);
930 u.packet.sy = GET_SY(control);
931 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500932
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500933 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500934 header_length = u.packet.header_length;
935 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400936 /*
937 * We require that header_length is a multiple of
938 * the fixed header size, ctx->header_size.
939 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500940 if (ctx->header_size == 0) {
941 if (u.packet.header_length > 0)
942 return -EINVAL;
943 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500944 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500945 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500946 header_length = 0;
947 }
948
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500949 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500950 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500951 if (next > end)
952 return -EINVAL;
953 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500954 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500955 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500956 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500957 u.packet.header_length + u.packet.payload_length > 0)
958 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200959 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500960 return -EINVAL;
961
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500962 if (fw_iso_context_queue(ctx, &u.packet,
963 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500964 break;
965
966 p = next;
967 payload += u.packet.payload_length;
968 count++;
969 }
970
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400971 request->size -= uptr_to_u64(p) - request->packets;
972 request->packets = uptr_to_u64(p);
973 request->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500974
975 return count;
976}
977
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400978static int ioctl_start_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500979{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400980 struct fw_cdev_start_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500981
Stefan Richterfae60312008-02-20 21:10:06 +0100982 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400983 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +0100984
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400985 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400986 if (request->tags == 0 || request->tags > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400987 return -EINVAL;
988
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400989 if (request->sync > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400990 return -EINVAL;
991 }
992
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400993 return fw_iso_context_start(client->iso_context, request->cycle,
994 request->sync, request->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500995}
996
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400997static int ioctl_stop_iso(struct client *client, void *buffer)
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500998{
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400999 struct fw_cdev_stop_iso *request = buffer;
1000
Stefan Richterfae60312008-02-20 21:10:06 +01001001 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001002 return -EINVAL;
1003
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001004 return fw_iso_context_stop(client->iso_context);
1005}
1006
Stefan Richtera64408b2007-09-29 10:41:58 +02001007static int ioctl_get_cycle_timer(struct client *client, void *buffer)
1008{
1009 struct fw_cdev_get_cycle_timer *request = buffer;
1010 struct fw_card *card = client->device->card;
1011 unsigned long long bus_time;
1012 struct timeval tv;
1013 unsigned long flags;
1014
1015 preempt_disable();
1016 local_irq_save(flags);
1017
1018 bus_time = card->driver->get_bus_time(card);
1019 do_gettimeofday(&tv);
1020
1021 local_irq_restore(flags);
1022 preempt_enable();
1023
1024 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
1025 request->cycle_timer = bus_time & 0xffffffff;
1026 return 0;
1027}
1028
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001029static void iso_resource_work(struct work_struct *work)
1030{
1031 struct iso_resource_event *e;
1032 struct iso_resource *r =
1033 container_of(work, struct iso_resource, work.work);
1034 struct client *client = r->client;
1035 int generation, channel, bandwidth, todo;
1036 bool skip, free, success;
1037
1038 spin_lock_irq(&client->lock);
1039 generation = client->device->generation;
1040 todo = r->todo;
1041 /* Allow 1000ms grace period for other reallocations. */
1042 if (todo == ISO_RES_ALLOC &&
1043 time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
Stefan Richter9fb551b2009-10-08 00:41:10 +02001044 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001045 skip = true;
1046 } else {
1047 /* We could be called twice within the same generation. */
1048 skip = todo == ISO_RES_REALLOC &&
1049 r->generation == generation;
1050 }
Stefan Richter1ec3c022009-01-04 16:23:29 +01001051 free = todo == ISO_RES_DEALLOC ||
1052 todo == ISO_RES_ALLOC_ONCE ||
1053 todo == ISO_RES_DEALLOC_ONCE;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001054 r->generation = generation;
1055 spin_unlock_irq(&client->lock);
1056
1057 if (skip)
1058 goto out;
1059
1060 bandwidth = r->bandwidth;
1061
1062 fw_iso_resource_manage(client->device->card, generation,
1063 r->channels, &channel, &bandwidth,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001064 todo == ISO_RES_ALLOC ||
1065 todo == ISO_RES_REALLOC ||
Stefan Richter6fdc0372009-06-20 13:23:59 +02001066 todo == ISO_RES_ALLOC_ONCE,
1067 r->transaction_data);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001068 /*
1069 * Is this generation outdated already? As long as this resource sticks
1070 * in the idr, it will be scheduled again for a newer generation or at
1071 * shutdown.
1072 */
1073 if (channel == -EAGAIN &&
1074 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1075 goto out;
1076
1077 success = channel >= 0 || bandwidth > 0;
1078
1079 spin_lock_irq(&client->lock);
1080 /*
1081 * Transit from allocation to reallocation, except if the client
1082 * requested deallocation in the meantime.
1083 */
1084 if (r->todo == ISO_RES_ALLOC)
1085 r->todo = ISO_RES_REALLOC;
1086 /*
1087 * Allocation or reallocation failure? Pull this resource out of the
1088 * idr and prepare for deletion, unless the client is shutting down.
1089 */
1090 if (r->todo == ISO_RES_REALLOC && !success &&
1091 !client->in_shutdown &&
1092 idr_find(&client->resource_idr, r->resource.handle)) {
1093 idr_remove(&client->resource_idr, r->resource.handle);
1094 client_put(client);
1095 free = true;
1096 }
1097 spin_unlock_irq(&client->lock);
1098
1099 if (todo == ISO_RES_ALLOC && channel >= 0)
Stefan Richter5d9cb7d2009-01-08 23:07:40 +01001100 r->channels = 1ULL << channel;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001101
1102 if (todo == ISO_RES_REALLOC && success)
1103 goto out;
1104
Stefan Richter1ec3c022009-01-04 16:23:29 +01001105 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001106 e = r->e_alloc;
1107 r->e_alloc = NULL;
1108 } else {
1109 e = r->e_dealloc;
1110 r->e_dealloc = NULL;
1111 }
Stefan Richtere21fcf72009-10-08 00:41:38 +02001112 e->iso_resource.handle = r->resource.handle;
1113 e->iso_resource.channel = channel;
1114 e->iso_resource.bandwidth = bandwidth;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001115
1116 queue_event(client, &e->event,
Stefan Richtere21fcf72009-10-08 00:41:38 +02001117 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001118
1119 if (free) {
1120 cancel_delayed_work(&r->work);
1121 kfree(r->e_alloc);
1122 kfree(r->e_dealloc);
1123 kfree(r);
1124 }
1125 out:
1126 client_put(client);
1127}
1128
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001129static void release_iso_resource(struct client *client,
1130 struct client_resource *resource)
1131{
1132 struct iso_resource *r =
1133 container_of(resource, struct iso_resource, resource);
1134
1135 spin_lock_irq(&client->lock);
1136 r->todo = ISO_RES_DEALLOC;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001137 schedule_iso_resource(r, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001138 spin_unlock_irq(&client->lock);
1139}
1140
Stefan Richter1ec3c022009-01-04 16:23:29 +01001141static int init_iso_resource(struct client *client,
1142 struct fw_cdev_allocate_iso_resource *request, int todo)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001143{
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001144 struct iso_resource_event *e1, *e2;
1145 struct iso_resource *r;
1146 int ret;
1147
1148 if ((request->channels == 0 && request->bandwidth == 0) ||
1149 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1150 request->bandwidth < 0)
1151 return -EINVAL;
1152
1153 r = kmalloc(sizeof(*r), GFP_KERNEL);
1154 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1155 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1156 if (r == NULL || e1 == NULL || e2 == NULL) {
1157 ret = -ENOMEM;
1158 goto fail;
1159 }
1160
1161 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1162 r->client = client;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001163 r->todo = todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001164 r->generation = -1;
1165 r->channels = request->channels;
1166 r->bandwidth = request->bandwidth;
1167 r->e_alloc = e1;
1168 r->e_dealloc = e2;
1169
Stefan Richtere21fcf72009-10-08 00:41:38 +02001170 e1->iso_resource.closure = request->closure;
1171 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1172 e2->iso_resource.closure = request->closure;
1173 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001174
Stefan Richter1ec3c022009-01-04 16:23:29 +01001175 if (todo == ISO_RES_ALLOC) {
1176 r->resource.release = release_iso_resource;
1177 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Stefan Richter81610b82009-01-11 13:44:46 +01001178 if (ret < 0)
1179 goto fail;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001180 } else {
1181 r->resource.release = NULL;
1182 r->resource.handle = -1;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001183 schedule_iso_resource(r, 0);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001184 }
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001185 request->handle = r->resource.handle;
1186
1187 return 0;
1188 fail:
1189 kfree(r);
1190 kfree(e1);
1191 kfree(e2);
1192
1193 return ret;
1194}
1195
Stefan Richter1ec3c022009-01-04 16:23:29 +01001196static int ioctl_allocate_iso_resource(struct client *client, void *buffer)
1197{
1198 struct fw_cdev_allocate_iso_resource *request = buffer;
1199
1200 return init_iso_resource(client, request, ISO_RES_ALLOC);
1201}
1202
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001203static int ioctl_deallocate_iso_resource(struct client *client, void *buffer)
1204{
1205 struct fw_cdev_deallocate *request = buffer;
1206
1207 return release_client_resource(client, request->handle,
1208 release_iso_resource, NULL);
1209}
1210
Stefan Richter1ec3c022009-01-04 16:23:29 +01001211static int ioctl_allocate_iso_resource_once(struct client *client, void *buffer)
1212{
1213 struct fw_cdev_allocate_iso_resource *request = buffer;
1214
1215 return init_iso_resource(client, request, ISO_RES_ALLOC_ONCE);
1216}
1217
1218static int ioctl_deallocate_iso_resource_once(struct client *client, void *buffer)
1219{
1220 struct fw_cdev_allocate_iso_resource *request = buffer;
1221
1222 return init_iso_resource(client, request, ISO_RES_DEALLOC_ONCE);
1223}
1224
Stefan Richterc8a25902009-03-10 20:59:16 +01001225/*
1226 * Returns a speed code: Maximum speed to or from this device,
1227 * limited by the device's link speed, the local node's link speed,
1228 * and all PHY port speeds between the two links.
1229 */
Stefan Richter33580a32009-01-04 16:23:29 +01001230static int ioctl_get_speed(struct client *client, void *buffer)
1231{
Stefan Richterc8a25902009-03-10 20:59:16 +01001232 return client->device->max_speed;
Stefan Richter33580a32009-01-04 16:23:29 +01001233}
1234
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001235static int ioctl_send_broadcast_request(struct client *client, void *buffer)
1236{
1237 struct fw_cdev_send_request *request = buffer;
1238
1239 switch (request->tcode) {
1240 case TCODE_WRITE_QUADLET_REQUEST:
1241 case TCODE_WRITE_BLOCK_REQUEST:
1242 break;
1243 default:
1244 return -EINVAL;
1245 }
1246
Stefan Richter1566f3d2009-01-04 16:23:29 +01001247 /* Security policy: Only allow accesses to Units Space. */
1248 if (request->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
1249 return -EACCES;
1250
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001251 return init_request(client, request, LOCAL_BUS | 0x3f, SCODE_100);
1252}
1253
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001254static int ioctl_send_stream_packet(struct client *client, void *buffer)
1255{
Stefan Richter18e9b102009-03-10 21:02:21 +01001256 struct fw_cdev_send_stream_packet *p = buffer;
1257 struct fw_cdev_send_request request;
1258 int dest;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001259
Stefan Richter18e9b102009-03-10 21:02:21 +01001260 if (p->speed > client->device->card->link_speed ||
1261 p->length > 1024 << p->speed)
1262 return -EIO;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001263
Stefan Richter18e9b102009-03-10 21:02:21 +01001264 if (p->tag > 3 || p->channel > 63 || p->sy > 15)
1265 return -EINVAL;
1266
1267 dest = fw_stream_packet_destination_id(p->tag, p->channel, p->sy);
1268 request.tcode = TCODE_STREAM_DATA;
1269 request.length = p->length;
1270 request.closure = p->closure;
1271 request.data = p->data;
1272 request.generation = p->generation;
1273
1274 return init_request(client, &request, dest, p->speed);
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001275}
1276
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001277static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
1278 ioctl_get_info,
1279 ioctl_send_request,
1280 ioctl_allocate,
1281 ioctl_deallocate,
1282 ioctl_send_response,
1283 ioctl_initiate_bus_reset,
1284 ioctl_add_descriptor,
1285 ioctl_remove_descriptor,
1286 ioctl_create_iso_context,
1287 ioctl_queue_iso,
1288 ioctl_start_iso,
1289 ioctl_stop_iso,
Stefan Richtera64408b2007-09-29 10:41:58 +02001290 ioctl_get_cycle_timer,
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001291 ioctl_allocate_iso_resource,
1292 ioctl_deallocate_iso_resource,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001293 ioctl_allocate_iso_resource_once,
1294 ioctl_deallocate_iso_resource_once,
Stefan Richter33580a32009-01-04 16:23:29 +01001295 ioctl_get_speed,
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001296 ioctl_send_broadcast_request,
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001297 ioctl_send_stream_packet,
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001298};
1299
Stefan Richter53dca512008-12-14 21:47:04 +01001300static int dispatch_ioctl(struct client *client,
1301 unsigned int cmd, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001302{
Stefan Richterb2c0a2a2009-10-15 21:16:53 +02001303 char buffer[sizeof(union {
1304 struct fw_cdev_get_info _00;
1305 struct fw_cdev_send_request _01;
1306 struct fw_cdev_allocate _02;
1307 struct fw_cdev_deallocate _03;
1308 struct fw_cdev_send_response _04;
1309 struct fw_cdev_initiate_bus_reset _05;
1310 struct fw_cdev_add_descriptor _06;
1311 struct fw_cdev_remove_descriptor _07;
1312 struct fw_cdev_create_iso_context _08;
1313 struct fw_cdev_queue_iso _09;
1314 struct fw_cdev_start_iso _0a;
1315 struct fw_cdev_stop_iso _0b;
1316 struct fw_cdev_get_cycle_timer _0c;
1317 struct fw_cdev_allocate_iso_resource _0d;
1318 struct fw_cdev_send_stream_packet _13;
1319 })];
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001320 int ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001321
1322 if (_IOC_TYPE(cmd) != '#' ||
1323 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001324 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001325
1326 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001327 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001328 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
1329 return -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001330 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001331
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001332 ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
1333 if (ret < 0)
1334 return ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001335
1336 if (_IOC_DIR(cmd) & _IOC_READ) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001337 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001338 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
1339 return -EFAULT;
1340 }
1341
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001342 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001343}
1344
Stefan Richter53dca512008-12-14 21:47:04 +01001345static long fw_device_op_ioctl(struct file *file,
1346 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001347{
1348 struct client *client = file->private_data;
1349
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001350 if (fw_device_is_shutdown(client->device))
1351 return -ENODEV;
1352
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001353 return dispatch_ioctl(client, cmd, (void __user *) arg);
1354}
1355
1356#ifdef CONFIG_COMPAT
Stefan Richter53dca512008-12-14 21:47:04 +01001357static long fw_device_op_compat_ioctl(struct file *file,
1358 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001359{
1360 struct client *client = file->private_data;
1361
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001362 if (fw_device_is_shutdown(client->device))
1363 return -ENODEV;
1364
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001365 return dispatch_ioctl(client, cmd, compat_ptr(arg));
1366}
1367#endif
1368
1369static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1370{
1371 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001372 enum dma_data_direction direction;
1373 unsigned long size;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001374 int page_count, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001375
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001376 if (fw_device_is_shutdown(client->device))
1377 return -ENODEV;
1378
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001379 /* FIXME: We could support multiple buffers, but we don't. */
1380 if (client->buffer.pages != NULL)
1381 return -EBUSY;
1382
1383 if (!(vma->vm_flags & VM_SHARED))
1384 return -EINVAL;
1385
1386 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001387 return -EINVAL;
1388
1389 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001390 size = vma->vm_end - vma->vm_start;
1391 page_count = size >> PAGE_SHIFT;
1392 if (size & ~PAGE_MASK)
1393 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001394
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001395 if (vma->vm_flags & VM_WRITE)
1396 direction = DMA_TO_DEVICE;
1397 else
1398 direction = DMA_FROM_DEVICE;
1399
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001400 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1401 page_count, direction);
1402 if (ret < 0)
1403 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001404
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001405 ret = fw_iso_buffer_map(&client->buffer, vma);
1406 if (ret < 0)
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001407 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1408
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001409 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001410}
1411
Jay Fenlason45ee3192008-12-21 16:47:17 +01001412static int shutdown_resource(int id, void *p, void *data)
1413{
Stefan Richtere21fcf72009-10-08 00:41:38 +02001414 struct client_resource *resource = p;
Jay Fenlason45ee3192008-12-21 16:47:17 +01001415 struct client *client = data;
1416
Stefan Richtere21fcf72009-10-08 00:41:38 +02001417 resource->release(client, resource);
Stefan Richterfb443032009-01-04 16:23:29 +01001418 client_put(client);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001419
1420 return 0;
1421}
1422
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001423static int fw_device_op_release(struct inode *inode, struct file *file)
1424{
1425 struct client *client = file->private_data;
Stefan Richtere21fcf72009-10-08 00:41:38 +02001426 struct event *event, *next_event;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001427
Stefan Richter97811e32008-12-14 19:19:23 +01001428 mutex_lock(&client->device->client_list_mutex);
1429 list_del(&client->link);
1430 mutex_unlock(&client->device->client_list_mutex);
1431
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001432 if (client->iso_context)
1433 fw_iso_context_destroy(client->iso_context);
1434
Stefan Richter36a755c2009-01-05 20:28:10 +01001435 if (client->buffer.pages)
1436 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1437
Jay Fenlason45ee3192008-12-21 16:47:17 +01001438 /* Freeze client->resource_idr and client->event_list */
Stefan Richter3ba94982009-01-04 16:23:29 +01001439 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001440 client->in_shutdown = true;
Stefan Richter3ba94982009-01-04 16:23:29 +01001441 spin_unlock_irq(&client->lock);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001442
Jay Fenlason45ee3192008-12-21 16:47:17 +01001443 idr_for_each(&client->resource_idr, shutdown_resource, client);
1444 idr_remove_all(&client->resource_idr);
1445 idr_destroy(&client->resource_idr);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001446
Stefan Richtere21fcf72009-10-08 00:41:38 +02001447 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1448 kfree(event);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001449
Stefan Richterfb443032009-01-04 16:23:29 +01001450 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001451
1452 return 0;
1453}
1454
1455static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1456{
1457 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001458 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001459
1460 poll_wait(file, &client->wait, pt);
1461
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001462 if (fw_device_is_shutdown(client->device))
1463 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001464 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001465 mask |= POLLIN | POLLRDNORM;
1466
1467 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001468}
1469
Stefan Richter21ebcd12007-01-14 15:29:07 +01001470const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001471 .owner = THIS_MODULE,
1472 .open = fw_device_op_open,
1473 .read = fw_device_op_read,
1474 .unlocked_ioctl = fw_device_op_ioctl,
1475 .poll = fw_device_op_poll,
1476 .release = fw_device_op_release,
1477 .mmap = fw_device_op_mmap,
1478
1479#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001480 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001481#endif
1482};