blob: 6b33f15584cb16906b14c4402f58588cef55aaf8 [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;
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;
129 struct iso_resource_event *e_alloc, *e_dealloc;
130};
131
Stefan Richter1ec3c022009-01-04 16:23:29 +0100132static int schedule_iso_resource(struct iso_resource *);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100133static void release_iso_resource(struct client *, struct client_resource *);
134
Stefan Richter97c18b72009-01-04 16:23:29 +0100135/*
136 * dequeue_event() just kfree()'s the event, so the event has to be
137 * the first field in a struct XYZ_event.
138 */
139struct event {
140 struct { void *data; size_t size; } v[2];
141 struct list_head link;
142};
143
144struct bus_reset_event {
145 struct event event;
146 struct fw_cdev_event_bus_reset reset;
147};
148
149struct outbound_transaction_event {
150 struct event event;
151 struct client *client;
152 struct outbound_transaction_resource r;
153 struct fw_cdev_event_response response;
154};
155
156struct inbound_transaction_event {
157 struct event event;
158 struct fw_cdev_event_request request;
159};
160
161struct iso_interrupt_event {
162 struct event event;
163 struct fw_cdev_event_iso_interrupt interrupt;
164};
165
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100166struct iso_resource_event {
167 struct event event;
168 struct fw_cdev_event_iso_resource resource;
169};
170
Stefan Richter53dca512008-12-14 21:47:04 +0100171static inline void __user *u64_to_uptr(__u64 value)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500172{
173 return (void __user *)(unsigned long)value;
174}
175
Stefan Richter53dca512008-12-14 21:47:04 +0100176static inline __u64 uptr_to_u64(void __user *ptr)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500177{
178 return (__u64)(unsigned long)ptr;
179}
180
181static int fw_device_op_open(struct inode *inode, struct file *file)
182{
183 struct fw_device *device;
184 struct client *client;
185
Stefan Richter96b19062008-02-02 15:01:09 +0100186 device = fw_device_get_by_devt(inode->i_rdev);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500187 if (device == NULL)
188 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500189
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400190 if (fw_device_is_shutdown(device)) {
191 fw_device_put(device);
192 return -ENODEV;
193 }
194
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400195 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100196 if (client == NULL) {
197 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500198 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100199 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500200
Stefan Richter96b19062008-02-02 15:01:09 +0100201 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500202 spin_lock_init(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100203 idr_init(&client->resource_idr);
204 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500205 init_waitqueue_head(&client->wait);
Stefan Richterfb443032009-01-04 16:23:29 +0100206 kref_init(&client->kref);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500207
208 file->private_data = client;
209
Stefan Richterd67cfb92008-10-05 10:37:11 +0200210 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500211 list_add_tail(&client->link, &device->client_list);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200212 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500213
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500214 return 0;
215}
216
217static void queue_event(struct client *client, struct event *event,
218 void *data0, size_t size0, void *data1, size_t size1)
219{
220 unsigned long flags;
221
222 event->v[0].data = data0;
223 event->v[0].size = size0;
224 event->v[1].data = data1;
225 event->v[1].size = size1;
226
227 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100228 if (client->in_shutdown)
229 kfree(event);
230 else
231 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500232 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400233
234 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500235}
236
Stefan Richter53dca512008-12-14 21:47:04 +0100237static int dequeue_event(struct client *client,
238 char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500239{
240 unsigned long flags;
241 struct event *event;
242 size_t size, total;
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100243 int i, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500244
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100245 ret = wait_event_interruptible(client->wait,
246 !list_empty(&client->event_list) ||
247 fw_device_is_shutdown(client->device));
248 if (ret < 0)
249 return ret;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500250
251 if (list_empty(&client->event_list) &&
252 fw_device_is_shutdown(client->device))
253 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500254
255 spin_lock_irqsave(&client->lock, flags);
Stefan Richtera459b8a2008-12-21 16:49:57 +0100256 event = list_first_entry(&client->event_list, struct event, link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500257 list_del(&event->link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500258 spin_unlock_irqrestore(&client->lock, flags);
259
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500260 total = 0;
261 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
262 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500263 if (copy_to_user(buffer + total, event->v[i].data, size)) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100264 ret = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500265 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500266 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500267 total += size;
268 }
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100269 ret = total;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500270
271 out:
272 kfree(event);
273
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100274 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500275}
276
Stefan Richter53dca512008-12-14 21:47:04 +0100277static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
278 size_t count, loff_t *offset)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500279{
280 struct client *client = file->private_data;
281
282 return dequeue_event(client, buffer, count);
283}
284
Stefan Richter53dca512008-12-14 21:47:04 +0100285static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
286 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500287{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400288 struct fw_card *card = client->device->card;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400289 unsigned long flags;
290
291 spin_lock_irqsave(&card->lock, flags);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500292
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400293 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500294 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100295 event->generation = client->device->generation;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400296 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500297 event->local_node_id = card->local_node->node_id;
298 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
299 event->irm_node_id = card->irm_node->node_id;
300 event->root_node_id = card->root_node->node_id;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400301
302 spin_unlock_irqrestore(&card->lock, flags);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500303}
304
Stefan Richter53dca512008-12-14 21:47:04 +0100305static void for_each_client(struct fw_device *device,
306 void (*callback)(struct client *client))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500307{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500308 struct client *c;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500309
Stefan Richterd67cfb92008-10-05 10:37:11 +0200310 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500311 list_for_each_entry(c, &device->client_list, link)
312 callback(c);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200313 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500314}
315
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100316static int schedule_reallocations(int id, void *p, void *data)
317{
318 struct client_resource *r = p;
319
320 if (r->release == release_iso_resource)
321 schedule_iso_resource(container_of(r,
322 struct iso_resource, resource));
323 return 0;
324}
325
Stefan Richter53dca512008-12-14 21:47:04 +0100326static void queue_bus_reset_event(struct client *client)
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500327{
Stefan Richter97c18b72009-01-04 16:23:29 +0100328 struct bus_reset_event *e;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500329
Stefan Richter97c18b72009-01-04 16:23:29 +0100330 e = kzalloc(sizeof(*e), GFP_KERNEL);
331 if (e == NULL) {
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500332 fw_notify("Out of memory when allocating bus reset event\n");
333 return;
334 }
335
Stefan Richter97c18b72009-01-04 16:23:29 +0100336 fill_bus_reset_event(&e->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500337
Stefan Richter97c18b72009-01-04 16:23:29 +0100338 queue_event(client, &e->event,
339 &e->reset, sizeof(e->reset), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100340
341 spin_lock_irq(&client->lock);
342 idr_for_each(&client->resource_idr, schedule_reallocations, client);
343 spin_unlock_irq(&client->lock);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500344}
345
346void fw_device_cdev_update(struct fw_device *device)
347{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500348 for_each_client(device, queue_bus_reset_event);
349}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500350
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500351static void wake_up_client(struct client *client)
352{
353 wake_up_interruptible(&client->wait);
354}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500355
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500356void fw_device_cdev_remove(struct fw_device *device)
357{
358 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500359}
360
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400361static int ioctl_get_info(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500362{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400363 struct fw_cdev_get_info *get_info = buffer;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500364 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100365 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500366
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400367 client->version = get_info->version;
368 get_info->version = FW_CDEV_VERSION;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400369 get_info->card = client->device->card->index;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500370
Stefan Richterc9755e12008-03-24 20:54:28 +0100371 down_read(&fw_device_rwsem);
372
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400373 if (get_info->rom != 0) {
374 void __user *uptr = u64_to_uptr(get_info->rom);
375 size_t want = get_info->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100376 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500377
Stefan Richterc9755e12008-03-24 20:54:28 +0100378 ret = copy_to_user(uptr, client->device->config_rom,
379 min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500380 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400381 get_info->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500382
Stefan Richterc9755e12008-03-24 20:54:28 +0100383 up_read(&fw_device_rwsem);
384
385 if (ret != 0)
386 return -EFAULT;
387
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400388 client->bus_reset_closure = get_info->bus_reset_closure;
389 if (get_info->bus_reset != 0) {
390 void __user *uptr = u64_to_uptr(get_info->bus_reset);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500391
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400392 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400393 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500394 return -EFAULT;
395 }
396
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500397 return 0;
398}
399
Stefan Richter53dca512008-12-14 21:47:04 +0100400static int add_client_resource(struct client *client,
401 struct client_resource *resource, gfp_t gfp_mask)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400402{
403 unsigned long flags;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100404 int ret;
405
406 retry:
407 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
408 return -ENOMEM;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400409
410 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100411 if (client->in_shutdown)
412 ret = -ECANCELED;
413 else
414 ret = idr_get_new(&client->resource_idr, resource,
415 &resource->handle);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100416 if (ret >= 0) {
Stefan Richterfb443032009-01-04 16:23:29 +0100417 client_get(client);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100418 if (resource->release == release_iso_resource)
419 schedule_iso_resource(container_of(resource,
420 struct iso_resource, resource));
421 }
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400422 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100423
424 if (ret == -EAGAIN)
425 goto retry;
426
427 return ret < 0 ? ret : 0;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400428}
429
Stefan Richter53dca512008-12-14 21:47:04 +0100430static int release_client_resource(struct client *client, u32 handle,
431 client_resource_release_fn_t release,
432 struct client_resource **resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400433{
434 struct client_resource *r;
435 unsigned long flags;
436
437 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100438 if (client->in_shutdown)
439 r = NULL;
440 else
441 r = idr_find(&client->resource_idr, handle);
442 if (r && r->release == release)
443 idr_remove(&client->resource_idr, handle);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400444 spin_unlock_irqrestore(&client->lock, flags);
445
Jay Fenlason45ee3192008-12-21 16:47:17 +0100446 if (!(r && r->release == release))
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400447 return -EINVAL;
448
449 if (resource)
450 *resource = r;
451 else
452 r->release(client, r);
453
Stefan Richterfb443032009-01-04 16:23:29 +0100454 client_put(client);
455
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400456 return 0;
457}
458
Stefan Richter53dca512008-12-14 21:47:04 +0100459static void release_transaction(struct client *client,
460 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400461{
Stefan Richter97c18b72009-01-04 16:23:29 +0100462 struct outbound_transaction_resource *r = container_of(resource,
463 struct outbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400464
Stefan Richter97c18b72009-01-04 16:23:29 +0100465 fw_cancel_transaction(client->device->card, &r->transaction);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400466}
467
Stefan Richter53dca512008-12-14 21:47:04 +0100468static void complete_transaction(struct fw_card *card, int rcode,
469 void *payload, size_t length, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500470{
Stefan Richter97c18b72009-01-04 16:23:29 +0100471 struct outbound_transaction_event *e = data;
472 struct fw_cdev_event_response *rsp = &e->response;
473 struct client *client = e->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500474 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500475
Stefan Richter97c18b72009-01-04 16:23:29 +0100476 if (length < rsp->length)
477 rsp->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500478 if (rcode == RCODE_COMPLETE)
Stefan Richter97c18b72009-01-04 16:23:29 +0100479 memcpy(rsp->data, payload, rsp->length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500480
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500481 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100482 /*
Stefan Richterfb443032009-01-04 16:23:29 +0100483 * 1. If called while in shutdown, the idr tree must be left untouched.
484 * The idr handle will be removed and the client reference will be
485 * dropped later.
486 * 2. If the call chain was release_client_resource ->
487 * release_transaction -> complete_transaction (instead of a normal
488 * conclusion of the transaction), i.e. if this resource was already
489 * unregistered from the idr, the client reference will be dropped
490 * by release_client_resource and we must not drop it here.
Jay Fenlason45ee3192008-12-21 16:47:17 +0100491 */
Stefan Richterfb443032009-01-04 16:23:29 +0100492 if (!client->in_shutdown &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100493 idr_find(&client->resource_idr, e->r.resource.handle)) {
494 idr_remove(&client->resource_idr, e->r.resource.handle);
Stefan Richterfb443032009-01-04 16:23:29 +0100495 /* Drop the idr's reference */
496 client_put(client);
497 }
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500498 spin_unlock_irqrestore(&client->lock, flags);
499
Stefan Richter97c18b72009-01-04 16:23:29 +0100500 rsp->type = FW_CDEV_EVENT_RESPONSE;
501 rsp->rcode = rcode;
David Moore8401d922008-07-29 23:46:25 -0700502
503 /*
Stefan Richter97c18b72009-01-04 16:23:29 +0100504 * In the case that sizeof(*rsp) doesn't align with the position of the
David Moore8401d922008-07-29 23:46:25 -0700505 * data, and the read is short, preserve an extra copy of the data
506 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
507 * for short reads and some apps depended on it, this is both safe
508 * and prudent for compatibility.
509 */
Stefan Richter97c18b72009-01-04 16:23:29 +0100510 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
511 queue_event(client, &e->event, rsp, sizeof(*rsp),
512 rsp->data, rsp->length);
David Moore8401d922008-07-29 23:46:25 -0700513 else
Stefan Richter97c18b72009-01-04 16:23:29 +0100514 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
David Moore8401d922008-07-29 23:46:25 -0700515 NULL, 0);
Stefan Richterfb443032009-01-04 16:23:29 +0100516
517 /* Drop the transaction callback's reference */
518 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500519}
520
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100521static int init_request(struct client *client,
522 struct fw_cdev_send_request *request,
523 int destination_id, int speed)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500524{
Stefan Richter97c18b72009-01-04 16:23:29 +0100525 struct outbound_transaction_event *e;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100526 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500527
Stefan Richter5d3fd692009-01-04 16:23:29 +0100528 if (request->length > 4096 || request->length > 512 << speed)
529 return -EIO;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500530
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
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100546 e->r.resource.release = release_transaction;
547 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
548 if (ret < 0)
549 goto failed;
550
551 /* Get a reference for the transaction callback */
552 client_get(client);
553
554 fw_send_request(client->device->card, &e->r.transaction,
555 request->tcode & 0x1f, destination_id,
556 request->generation, speed, request->offset,
557 e->response.data, request->length,
558 complete_transaction, e);
559
560 if (request->data)
561 return sizeof(request) + request->length;
562 else
563 return sizeof(request);
564 failed:
565 kfree(e);
566
567 return ret;
568}
569
570static int ioctl_send_request(struct client *client, void *buffer)
571{
572 struct fw_cdev_send_request *request = buffer;
573
Stefan Richter1f3125a2008-12-05 22:44:42 +0100574 switch (request->tcode) {
575 case TCODE_WRITE_QUADLET_REQUEST:
576 case TCODE_WRITE_BLOCK_REQUEST:
577 case TCODE_READ_QUADLET_REQUEST:
578 case TCODE_READ_BLOCK_REQUEST:
579 case TCODE_LOCK_MASK_SWAP:
580 case TCODE_LOCK_COMPARE_SWAP:
581 case TCODE_LOCK_FETCH_ADD:
582 case TCODE_LOCK_LITTLE_ADD:
583 case TCODE_LOCK_BOUNDED_ADD:
584 case TCODE_LOCK_WRAP_ADD:
585 case TCODE_LOCK_VENDOR_DEPENDENT:
586 break;
587 default:
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100588 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500589 }
590
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100591 return init_request(client, request, client->device->node->node_id,
592 client->device->max_speed);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500593}
594
Stefan Richter53dca512008-12-14 21:47:04 +0100595static void release_request(struct client *client,
596 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400597{
Stefan Richter97c18b72009-01-04 16:23:29 +0100598 struct inbound_transaction_resource *r = container_of(resource,
599 struct inbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400600
Stefan Richter97c18b72009-01-04 16:23:29 +0100601 fw_send_response(client->device->card, r->request,
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400602 RCODE_CONFLICT_ERROR);
Stefan Richter97c18b72009-01-04 16:23:29 +0100603 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400604}
605
Stefan Richter97c18b72009-01-04 16:23:29 +0100606static void handle_request(struct fw_card *card, struct fw_request *request,
Stefan Richter53dca512008-12-14 21:47:04 +0100607 int tcode, int destination, int source,
608 int generation, int speed,
609 unsigned long long offset,
610 void *payload, size_t length, void *callback_data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500611{
Stefan Richter97c18b72009-01-04 16:23:29 +0100612 struct address_handler_resource *handler = callback_data;
613 struct inbound_transaction_resource *r;
614 struct inbound_transaction_event *e;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100615 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500616
Stefan Richter97c18b72009-01-04 16:23:29 +0100617 r = kmalloc(sizeof(*r), GFP_ATOMIC);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400618 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Stefan Richter97c18b72009-01-04 16:23:29 +0100619 if (r == NULL || e == NULL)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100620 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500621
Stefan Richter97c18b72009-01-04 16:23:29 +0100622 r->request = request;
623 r->data = payload;
624 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500625
Stefan Richter97c18b72009-01-04 16:23:29 +0100626 r->resource.release = release_request;
627 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100628 if (ret < 0)
629 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500630
631 e->request.type = FW_CDEV_EVENT_REQUEST;
632 e->request.tcode = tcode;
633 e->request.offset = offset;
634 e->request.length = length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100635 e->request.handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500636 e->request.closure = handler->closure;
637
Stefan Richter97c18b72009-01-04 16:23:29 +0100638 queue_event(handler->client, &e->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400639 &e->request, sizeof(e->request), payload, length);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100640 return;
641
642 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100643 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100644 kfree(e);
Stefan Richter97c18b72009-01-04 16:23:29 +0100645 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500646}
647
Stefan Richter53dca512008-12-14 21:47:04 +0100648static void release_address_handler(struct client *client,
649 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400650{
Stefan Richter97c18b72009-01-04 16:23:29 +0100651 struct address_handler_resource *r =
652 container_of(resource, struct address_handler_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400653
Stefan Richter97c18b72009-01-04 16:23:29 +0100654 fw_core_remove_address_handler(&r->handler);
655 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400656}
657
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400658static int ioctl_allocate(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500659{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400660 struct fw_cdev_allocate *request = buffer;
Stefan Richter97c18b72009-01-04 16:23:29 +0100661 struct address_handler_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500662 struct fw_address_region region;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100663 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500664
Stefan Richter97c18b72009-01-04 16:23:29 +0100665 r = kmalloc(sizeof(*r), GFP_KERNEL);
666 if (r == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500667 return -ENOMEM;
668
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400669 region.start = request->offset;
670 region.end = request->offset + request->length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100671 r->handler.length = request->length;
672 r->handler.address_callback = handle_request;
673 r->handler.callback_data = r;
674 r->closure = request->closure;
675 r->client = client;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500676
Stefan Richter97c18b72009-01-04 16:23:29 +0100677 ret = fw_core_add_address_handler(&r->handler, &region);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100678 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100679 kfree(r);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100680 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500681 }
682
Stefan Richter97c18b72009-01-04 16:23:29 +0100683 r->resource.release = release_address_handler;
684 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100685 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100686 release_address_handler(client, &r->resource);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100687 return ret;
688 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100689 request->handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500690
691 return 0;
692}
693
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400694static int ioctl_deallocate(struct client *client, void *buffer)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400695{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400696 struct fw_cdev_deallocate *request = buffer;
Kristian Høgsberg94723162007-03-14 17:34:55 -0400697
Jay Fenlason45ee3192008-12-21 16:47:17 +0100698 return release_client_resource(client, request->handle,
699 release_address_handler, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400700}
701
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400702static int ioctl_send_response(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500703{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400704 struct fw_cdev_send_response *request = buffer;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400705 struct client_resource *resource;
Stefan Richter97c18b72009-01-04 16:23:29 +0100706 struct inbound_transaction_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500707
Jay Fenlason45ee3192008-12-21 16:47:17 +0100708 if (release_client_resource(client, request->handle,
709 release_request, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500710 return -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100711
Stefan Richter97c18b72009-01-04 16:23:29 +0100712 r = container_of(resource, struct inbound_transaction_resource,
713 resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400714 if (request->length < r->length)
715 r->length = request->length;
716 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500717 return -EFAULT;
718
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400719 fw_send_response(client->device->card, r->request, request->rcode);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500720 kfree(r);
721
722 return 0;
723}
724
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400725static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500726{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400727 struct fw_cdev_initiate_bus_reset *request = buffer;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500728 int short_reset;
729
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400730 short_reset = (request->type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500731
732 return fw_core_initiate_bus_reset(client->device->card, short_reset);
733}
734
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400735static void release_descriptor(struct client *client,
736 struct client_resource *resource)
737{
Stefan Richter97c18b72009-01-04 16:23:29 +0100738 struct descriptor_resource *r =
739 container_of(resource, struct descriptor_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400740
Stefan Richter97c18b72009-01-04 16:23:29 +0100741 fw_core_remove_descriptor(&r->descriptor);
742 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400743}
744
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400745static int ioctl_add_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200746{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400747 struct fw_cdev_add_descriptor *request = buffer;
Stefan Richter97c18b72009-01-04 16:23:29 +0100748 struct descriptor_resource *r;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100749 int ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200750
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400751 if (request->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200752 return -EINVAL;
753
Stefan Richter97c18b72009-01-04 16:23:29 +0100754 r = kmalloc(sizeof(*r) + request->length * 4, GFP_KERNEL);
755 if (r == NULL)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200756 return -ENOMEM;
757
Stefan Richter97c18b72009-01-04 16:23:29 +0100758 if (copy_from_user(r->data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400759 u64_to_uptr(request->data), request->length * 4)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100760 ret = -EFAULT;
761 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200762 }
763
Stefan Richter97c18b72009-01-04 16:23:29 +0100764 r->descriptor.length = request->length;
765 r->descriptor.immediate = request->immediate;
766 r->descriptor.key = request->key;
767 r->descriptor.data = r->data;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200768
Stefan Richter97c18b72009-01-04 16:23:29 +0100769 ret = fw_core_add_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100770 if (ret < 0)
771 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200772
Stefan Richter97c18b72009-01-04 16:23:29 +0100773 r->resource.release = release_descriptor;
774 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100775 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100776 fw_core_remove_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100777 goto failed;
778 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100779 request->handle = r->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200780
781 return 0;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100782 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100783 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100784
785 return ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200786}
787
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400788static int ioctl_remove_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200789{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400790 struct fw_cdev_remove_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200791
Jay Fenlason45ee3192008-12-21 16:47:17 +0100792 return release_client_resource(client, request->handle,
793 release_descriptor, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200794}
795
Stefan Richter53dca512008-12-14 21:47:04 +0100796static void iso_callback(struct fw_iso_context *context, u32 cycle,
797 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500798{
799 struct client *client = data;
Stefan Richter97c18b72009-01-04 16:23:29 +0100800 struct iso_interrupt_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500801
Stefan Richter97c18b72009-01-04 16:23:29 +0100802 e = kzalloc(sizeof(*e) + header_length, GFP_ATOMIC);
803 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500804 return;
805
Stefan Richter97c18b72009-01-04 16:23:29 +0100806 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
807 e->interrupt.closure = client->iso_closure;
808 e->interrupt.cycle = cycle;
809 e->interrupt.header_length = header_length;
810 memcpy(e->interrupt.header, header, header_length);
811 queue_event(client, &e->event, &e->interrupt,
812 sizeof(e->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500813}
814
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400815static int ioctl_create_iso_context(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500816{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400817 struct fw_cdev_create_iso_context *request = buffer;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400818 struct fw_iso_context *context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500819
Stefan Richterfae60312008-02-20 21:10:06 +0100820 /* We only support one context at this time. */
821 if (client->iso_context != NULL)
822 return -EBUSY;
823
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400824 if (request->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500825 return -EINVAL;
826
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400827 switch (request->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400828 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400829 if (request->header_size < 4 || (request->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400830 return -EINVAL;
831
832 break;
833
834 case FW_ISO_CONTEXT_TRANSMIT:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400835 if (request->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400836 return -EINVAL;
837
838 break;
839
840 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500841 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400842 }
843
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400844 context = fw_iso_context_create(client->device->card,
845 request->type,
846 request->channel,
847 request->speed,
848 request->header_size,
849 iso_callback, client);
850 if (IS_ERR(context))
851 return PTR_ERR(context);
852
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400853 client->iso_closure = request->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400854 client->iso_context = context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500855
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400856 /* We only support one context at this time. */
857 request->handle = 0;
858
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500859 return 0;
860}
861
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400862/* Macros for decoding the iso packet control header. */
863#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
864#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
865#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +0200866#define GET_TAG(v) (((v) >> 18) & 0x03)
867#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400868#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
869
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400870static int ioctl_queue_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500871{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400872 struct fw_cdev_queue_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500873 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500874 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200875 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400876 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500877 int count;
878 struct {
879 struct fw_iso_packet packet;
880 u8 header[256];
881 } u;
882
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400883 if (ctx == NULL || request->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500884 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500885
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400886 /*
887 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500888 * the iso buffer, and the pointer points inside the buffer,
889 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500890 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500891 * payload_length == 0 through. In other words, if no packets
892 * use the indirect payload, the iso buffer need not be mapped
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400893 * and the request->data pointer is ignored.
894 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500895
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400896 payload = (unsigned long)request->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200897 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400898 if (request->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200899 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500900 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200901 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500902 }
903
Al Viro1ccc9142007-10-14 19:34:40 +0100904 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
905
906 if (!access_ok(VERIFY_READ, p, request->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500907 return -EFAULT;
908
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400909 end = (void __user *)p + request->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500910 count = 0;
911 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400912 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500913 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400914 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
915 u.packet.interrupt = GET_INTERRUPT(control);
916 u.packet.skip = GET_SKIP(control);
917 u.packet.tag = GET_TAG(control);
918 u.packet.sy = GET_SY(control);
919 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500920
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500921 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500922 header_length = u.packet.header_length;
923 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400924 /*
925 * We require that header_length is a multiple of
926 * the fixed header size, ctx->header_size.
927 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500928 if (ctx->header_size == 0) {
929 if (u.packet.header_length > 0)
930 return -EINVAL;
931 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500932 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500933 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500934 header_length = 0;
935 }
936
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500937 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500938 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500939 if (next > end)
940 return -EINVAL;
941 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500942 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500943 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500944 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500945 u.packet.header_length + u.packet.payload_length > 0)
946 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200947 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500948 return -EINVAL;
949
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500950 if (fw_iso_context_queue(ctx, &u.packet,
951 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500952 break;
953
954 p = next;
955 payload += u.packet.payload_length;
956 count++;
957 }
958
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400959 request->size -= uptr_to_u64(p) - request->packets;
960 request->packets = uptr_to_u64(p);
961 request->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500962
963 return count;
964}
965
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400966static int ioctl_start_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500967{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400968 struct fw_cdev_start_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500969
Stefan Richterfae60312008-02-20 21:10:06 +0100970 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400971 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +0100972
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400973 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400974 if (request->tags == 0 || request->tags > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400975 return -EINVAL;
976
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400977 if (request->sync > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400978 return -EINVAL;
979 }
980
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400981 return fw_iso_context_start(client->iso_context, request->cycle,
982 request->sync, request->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500983}
984
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400985static int ioctl_stop_iso(struct client *client, void *buffer)
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500986{
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400987 struct fw_cdev_stop_iso *request = buffer;
988
Stefan Richterfae60312008-02-20 21:10:06 +0100989 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400990 return -EINVAL;
991
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500992 return fw_iso_context_stop(client->iso_context);
993}
994
Stefan Richtera64408b2007-09-29 10:41:58 +0200995static int ioctl_get_cycle_timer(struct client *client, void *buffer)
996{
997 struct fw_cdev_get_cycle_timer *request = buffer;
998 struct fw_card *card = client->device->card;
999 unsigned long long bus_time;
1000 struct timeval tv;
1001 unsigned long flags;
1002
1003 preempt_disable();
1004 local_irq_save(flags);
1005
1006 bus_time = card->driver->get_bus_time(card);
1007 do_gettimeofday(&tv);
1008
1009 local_irq_restore(flags);
1010 preempt_enable();
1011
1012 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
1013 request->cycle_timer = bus_time & 0xffffffff;
1014 return 0;
1015}
1016
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001017static void iso_resource_work(struct work_struct *work)
1018{
1019 struct iso_resource_event *e;
1020 struct iso_resource *r =
1021 container_of(work, struct iso_resource, work.work);
1022 struct client *client = r->client;
1023 int generation, channel, bandwidth, todo;
1024 bool skip, free, success;
1025
1026 spin_lock_irq(&client->lock);
1027 generation = client->device->generation;
1028 todo = r->todo;
1029 /* Allow 1000ms grace period for other reallocations. */
1030 if (todo == ISO_RES_ALLOC &&
1031 time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
1032 if (schedule_delayed_work(&r->work, DIV_ROUND_UP(HZ, 3)))
1033 client_get(client);
1034 skip = true;
1035 } else {
1036 /* We could be called twice within the same generation. */
1037 skip = todo == ISO_RES_REALLOC &&
1038 r->generation == generation;
1039 }
Stefan Richter1ec3c022009-01-04 16:23:29 +01001040 free = todo == ISO_RES_DEALLOC ||
1041 todo == ISO_RES_ALLOC_ONCE ||
1042 todo == ISO_RES_DEALLOC_ONCE;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001043 r->generation = generation;
1044 spin_unlock_irq(&client->lock);
1045
1046 if (skip)
1047 goto out;
1048
1049 bandwidth = r->bandwidth;
1050
1051 fw_iso_resource_manage(client->device->card, generation,
1052 r->channels, &channel, &bandwidth,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001053 todo == ISO_RES_ALLOC ||
1054 todo == ISO_RES_REALLOC ||
1055 todo == ISO_RES_ALLOC_ONCE);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001056 /*
1057 * Is this generation outdated already? As long as this resource sticks
1058 * in the idr, it will be scheduled again for a newer generation or at
1059 * shutdown.
1060 */
1061 if (channel == -EAGAIN &&
1062 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1063 goto out;
1064
1065 success = channel >= 0 || bandwidth > 0;
1066
1067 spin_lock_irq(&client->lock);
1068 /*
1069 * Transit from allocation to reallocation, except if the client
1070 * requested deallocation in the meantime.
1071 */
1072 if (r->todo == ISO_RES_ALLOC)
1073 r->todo = ISO_RES_REALLOC;
1074 /*
1075 * Allocation or reallocation failure? Pull this resource out of the
1076 * idr and prepare for deletion, unless the client is shutting down.
1077 */
1078 if (r->todo == ISO_RES_REALLOC && !success &&
1079 !client->in_shutdown &&
1080 idr_find(&client->resource_idr, r->resource.handle)) {
1081 idr_remove(&client->resource_idr, r->resource.handle);
1082 client_put(client);
1083 free = true;
1084 }
1085 spin_unlock_irq(&client->lock);
1086
1087 if (todo == ISO_RES_ALLOC && channel >= 0)
1088 r->channels = 1ULL << (63 - channel);
1089
1090 if (todo == ISO_RES_REALLOC && success)
1091 goto out;
1092
Stefan Richter1ec3c022009-01-04 16:23:29 +01001093 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001094 e = r->e_alloc;
1095 r->e_alloc = NULL;
1096 } else {
1097 e = r->e_dealloc;
1098 r->e_dealloc = NULL;
1099 }
1100 e->resource.handle = r->resource.handle;
1101 e->resource.channel = channel;
1102 e->resource.bandwidth = bandwidth;
1103
1104 queue_event(client, &e->event,
1105 &e->resource, sizeof(e->resource), NULL, 0);
1106
1107 if (free) {
1108 cancel_delayed_work(&r->work);
1109 kfree(r->e_alloc);
1110 kfree(r->e_dealloc);
1111 kfree(r);
1112 }
1113 out:
1114 client_put(client);
1115}
1116
Stefan Richter1ec3c022009-01-04 16:23:29 +01001117static int schedule_iso_resource(struct iso_resource *r)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001118{
Stefan Richter1ec3c022009-01-04 16:23:29 +01001119 int scheduled;
1120
1121 client_get(r->client);
1122
1123 scheduled = schedule_delayed_work(&r->work, 0);
1124 if (!scheduled)
1125 client_put(r->client);
1126
1127 return scheduled;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001128}
1129
1130static void release_iso_resource(struct client *client,
1131 struct client_resource *resource)
1132{
1133 struct iso_resource *r =
1134 container_of(resource, struct iso_resource, resource);
1135
1136 spin_lock_irq(&client->lock);
1137 r->todo = ISO_RES_DEALLOC;
1138 schedule_iso_resource(r);
1139 spin_unlock_irq(&client->lock);
1140}
1141
Stefan Richter1ec3c022009-01-04 16:23:29 +01001142static int init_iso_resource(struct client *client,
1143 struct fw_cdev_allocate_iso_resource *request, int todo)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001144{
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001145 struct iso_resource_event *e1, *e2;
1146 struct iso_resource *r;
1147 int ret;
1148
1149 if ((request->channels == 0 && request->bandwidth == 0) ||
1150 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1151 request->bandwidth < 0)
1152 return -EINVAL;
1153
1154 r = kmalloc(sizeof(*r), GFP_KERNEL);
1155 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1156 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1157 if (r == NULL || e1 == NULL || e2 == NULL) {
1158 ret = -ENOMEM;
1159 goto fail;
1160 }
1161
1162 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1163 r->client = client;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001164 r->todo = todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001165 r->generation = -1;
1166 r->channels = request->channels;
1167 r->bandwidth = request->bandwidth;
1168 r->e_alloc = e1;
1169 r->e_dealloc = e2;
1170
1171 e1->resource.closure = request->closure;
1172 e1->resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1173 e2->resource.closure = request->closure;
1174 e2->resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
1175
Stefan Richter1ec3c022009-01-04 16:23:29 +01001176 if (todo == ISO_RES_ALLOC) {
1177 r->resource.release = release_iso_resource;
1178 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
1179 } else {
1180 r->resource.release = NULL;
1181 r->resource.handle = -1;
1182 ret = schedule_iso_resource(r) ? 0 : -ENOMEM;
1183 }
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001184 if (ret < 0)
1185 goto fail;
1186 request->handle = r->resource.handle;
1187
1188 return 0;
1189 fail:
1190 kfree(r);
1191 kfree(e1);
1192 kfree(e2);
1193
1194 return ret;
1195}
1196
Stefan Richter1ec3c022009-01-04 16:23:29 +01001197static int ioctl_allocate_iso_resource(struct client *client, void *buffer)
1198{
1199 struct fw_cdev_allocate_iso_resource *request = buffer;
1200
1201 return init_iso_resource(client, request, ISO_RES_ALLOC);
1202}
1203
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001204static int ioctl_deallocate_iso_resource(struct client *client, void *buffer)
1205{
1206 struct fw_cdev_deallocate *request = buffer;
1207
1208 return release_client_resource(client, request->handle,
1209 release_iso_resource, NULL);
1210}
1211
Stefan Richter1ec3c022009-01-04 16:23:29 +01001212static int ioctl_allocate_iso_resource_once(struct client *client, void *buffer)
1213{
1214 struct fw_cdev_allocate_iso_resource *request = buffer;
1215
1216 return init_iso_resource(client, request, ISO_RES_ALLOC_ONCE);
1217}
1218
1219static int ioctl_deallocate_iso_resource_once(struct client *client, void *buffer)
1220{
1221 struct fw_cdev_allocate_iso_resource *request = buffer;
1222
1223 return init_iso_resource(client, request, ISO_RES_DEALLOC_ONCE);
1224}
1225
Stefan Richter33580a32009-01-04 16:23:29 +01001226static int ioctl_get_speed(struct client *client, void *buffer)
1227{
1228 struct fw_cdev_get_speed *request = buffer;
1229
1230 request->max_speed = client->device->max_speed;
1231
1232 return 0;
1233}
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
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001254static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
1255 ioctl_get_info,
1256 ioctl_send_request,
1257 ioctl_allocate,
1258 ioctl_deallocate,
1259 ioctl_send_response,
1260 ioctl_initiate_bus_reset,
1261 ioctl_add_descriptor,
1262 ioctl_remove_descriptor,
1263 ioctl_create_iso_context,
1264 ioctl_queue_iso,
1265 ioctl_start_iso,
1266 ioctl_stop_iso,
Stefan Richtera64408b2007-09-29 10:41:58 +02001267 ioctl_get_cycle_timer,
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001268 ioctl_allocate_iso_resource,
1269 ioctl_deallocate_iso_resource,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001270 ioctl_allocate_iso_resource_once,
1271 ioctl_deallocate_iso_resource_once,
Stefan Richter33580a32009-01-04 16:23:29 +01001272 ioctl_get_speed,
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001273 ioctl_send_broadcast_request,
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001274};
1275
Stefan Richter53dca512008-12-14 21:47:04 +01001276static int dispatch_ioctl(struct client *client,
1277 unsigned int cmd, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001278{
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001279 char buffer[256];
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001280 int ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001281
1282 if (_IOC_TYPE(cmd) != '#' ||
1283 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001284 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001285
1286 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001287 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001288 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
1289 return -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001290 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001291
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001292 ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
1293 if (ret < 0)
1294 return ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001295
1296 if (_IOC_DIR(cmd) & _IOC_READ) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001297 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001298 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
1299 return -EFAULT;
1300 }
1301
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001302 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001303}
1304
Stefan Richter53dca512008-12-14 21:47:04 +01001305static long fw_device_op_ioctl(struct file *file,
1306 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001307{
1308 struct client *client = file->private_data;
1309
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001310 if (fw_device_is_shutdown(client->device))
1311 return -ENODEV;
1312
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001313 return dispatch_ioctl(client, cmd, (void __user *) arg);
1314}
1315
1316#ifdef CONFIG_COMPAT
Stefan Richter53dca512008-12-14 21:47:04 +01001317static long fw_device_op_compat_ioctl(struct file *file,
1318 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001319{
1320 struct client *client = file->private_data;
1321
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001322 if (fw_device_is_shutdown(client->device))
1323 return -ENODEV;
1324
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001325 return dispatch_ioctl(client, cmd, compat_ptr(arg));
1326}
1327#endif
1328
1329static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1330{
1331 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001332 enum dma_data_direction direction;
1333 unsigned long size;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001334 int page_count, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001335
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001336 if (fw_device_is_shutdown(client->device))
1337 return -ENODEV;
1338
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001339 /* FIXME: We could support multiple buffers, but we don't. */
1340 if (client->buffer.pages != NULL)
1341 return -EBUSY;
1342
1343 if (!(vma->vm_flags & VM_SHARED))
1344 return -EINVAL;
1345
1346 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001347 return -EINVAL;
1348
1349 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001350 size = vma->vm_end - vma->vm_start;
1351 page_count = size >> PAGE_SHIFT;
1352 if (size & ~PAGE_MASK)
1353 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001354
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001355 if (vma->vm_flags & VM_WRITE)
1356 direction = DMA_TO_DEVICE;
1357 else
1358 direction = DMA_FROM_DEVICE;
1359
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001360 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1361 page_count, direction);
1362 if (ret < 0)
1363 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001364
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001365 ret = fw_iso_buffer_map(&client->buffer, vma);
1366 if (ret < 0)
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001367 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1368
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001369 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001370}
1371
Jay Fenlason45ee3192008-12-21 16:47:17 +01001372static int shutdown_resource(int id, void *p, void *data)
1373{
1374 struct client_resource *r = p;
1375 struct client *client = data;
1376
1377 r->release(client, r);
Stefan Richterfb443032009-01-04 16:23:29 +01001378 client_put(client);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001379
1380 return 0;
1381}
1382
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001383static int fw_device_op_release(struct inode *inode, struct file *file)
1384{
1385 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001386 struct event *e, *next_e;
Jay Fenlason45ee3192008-12-21 16:47:17 +01001387 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001388
Stefan Richter97811e32008-12-14 19:19:23 +01001389 mutex_lock(&client->device->client_list_mutex);
1390 list_del(&client->link);
1391 mutex_unlock(&client->device->client_list_mutex);
1392
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001393 if (client->buffer.pages)
1394 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1395
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001396 if (client->iso_context)
1397 fw_iso_context_destroy(client->iso_context);
1398
Jay Fenlason45ee3192008-12-21 16:47:17 +01001399 /* Freeze client->resource_idr and client->event_list */
1400 spin_lock_irqsave(&client->lock, flags);
1401 client->in_shutdown = true;
1402 spin_unlock_irqrestore(&client->lock, flags);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001403
Jay Fenlason45ee3192008-12-21 16:47:17 +01001404 idr_for_each(&client->resource_idr, shutdown_resource, client);
1405 idr_remove_all(&client->resource_idr);
1406 idr_destroy(&client->resource_idr);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001407
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001408 list_for_each_entry_safe(e, next_e, &client->event_list, link)
1409 kfree(e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001410
Stefan Richterfb443032009-01-04 16:23:29 +01001411 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001412
1413 return 0;
1414}
1415
1416static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1417{
1418 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001419 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001420
1421 poll_wait(file, &client->wait, pt);
1422
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001423 if (fw_device_is_shutdown(client->device))
1424 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001425 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001426 mask |= POLLIN | POLLRDNORM;
1427
1428 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001429}
1430
Stefan Richter21ebcd12007-01-14 15:29:07 +01001431const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001432 .owner = THIS_MODULE,
1433 .open = fw_device_op_open,
1434 .read = fw_device_op_read,
1435 .unlocked_ioctl = fw_device_op_ioctl,
1436 .poll = fw_device_op_poll,
1437 .release = fw_device_op_release,
1438 .mmap = fw_device_op_mmap,
1439
1440#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001441 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001442#endif
1443};