blob: 85e866e28a272dfd1573b735739c91973d511686 [file] [log] [blame]
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001/*
2 * Char device for device raw access
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05003 *
Kristian Høgsbergc781c062007-05-07 20:33:32 -04004 * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/module.h>
22#include <linux/kernel.h>
Stefan Richterfb443032009-01-04 16:23:29 +010023#include <linux/kref.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050024#include <linux/wait.h>
25#include <linux/errno.h>
26#include <linux/device.h>
27#include <linux/vmalloc.h>
Stefan Richterd67cfb92008-10-05 10:37:11 +020028#include <linux/mutex.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050029#include <linux/poll.h>
Stefan Richtera64408b2007-09-29 10:41:58 +020030#include <linux/preempt.h>
31#include <linux/time.h>
Jay Fenlasoncf417e542008-10-03 11:19:09 -040032#include <linux/spinlock.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050033#include <linux/delay.h>
34#include <linux/mm.h>
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -050035#include <linux/idr.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050036#include <linux/compat.h>
Kristian Høgsberg9640d3d2007-04-30 15:03:15 -040037#include <linux/firewire-cdev.h>
Stefan Richtera64408b2007-09-29 10:41:58 +020038#include <asm/system.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050039#include <asm/uaccess.h>
40#include "fw-transaction.h"
41#include "fw-topology.h"
42#include "fw-device.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050043
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050044struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050045 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050046 struct fw_device *device;
Jay Fenlason45ee3192008-12-21 16:47:17 +010047
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050048 spinlock_t lock;
Jay Fenlason45ee3192008-12-21 16:47:17 +010049 bool in_shutdown;
50 struct idr resource_idr;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050051 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050052 wait_queue_head_t wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040053 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050054
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050055 struct fw_iso_context *iso_context;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -040056 u64 iso_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050057 struct fw_iso_buffer buffer;
58 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050059
60 struct list_head link;
Stefan Richterfb443032009-01-04 16:23:29 +010061 struct kref kref;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050062};
63
Stefan Richterfb443032009-01-04 16:23:29 +010064static inline void client_get(struct client *client)
65{
66 kref_get(&client->kref);
67}
68
69static void client_release(struct kref *kref)
70{
71 struct client *client = container_of(kref, struct client, kref);
72
73 fw_device_put(client->device);
74 kfree(client);
75}
76
77static void client_put(struct client *client)
78{
79 kref_put(&client->kref, client_release);
80}
81
Stefan Richter97c18b72009-01-04 16:23:29 +010082struct client_resource;
83typedef void (*client_resource_release_fn_t)(struct client *,
84 struct client_resource *);
85struct client_resource {
86 client_resource_release_fn_t release;
87 int handle;
88};
89
90struct address_handler_resource {
91 struct client_resource resource;
92 struct fw_address_handler handler;
93 __u64 closure;
94 struct client *client;
95};
96
97struct outbound_transaction_resource {
98 struct client_resource resource;
99 struct fw_transaction transaction;
100};
101
102struct inbound_transaction_resource {
103 struct client_resource resource;
104 struct fw_request *request;
105 void *data;
106 size_t length;
107};
108
109struct descriptor_resource {
110 struct client_resource resource;
111 struct fw_descriptor descriptor;
112 u32 data[0];
113};
114
115/*
116 * dequeue_event() just kfree()'s the event, so the event has to be
117 * the first field in a struct XYZ_event.
118 */
119struct event {
120 struct { void *data; size_t size; } v[2];
121 struct list_head link;
122};
123
124struct bus_reset_event {
125 struct event event;
126 struct fw_cdev_event_bus_reset reset;
127};
128
129struct outbound_transaction_event {
130 struct event event;
131 struct client *client;
132 struct outbound_transaction_resource r;
133 struct fw_cdev_event_response response;
134};
135
136struct inbound_transaction_event {
137 struct event event;
138 struct fw_cdev_event_request request;
139};
140
141struct iso_interrupt_event {
142 struct event event;
143 struct fw_cdev_event_iso_interrupt interrupt;
144};
145
Stefan Richter53dca512008-12-14 21:47:04 +0100146static inline void __user *u64_to_uptr(__u64 value)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500147{
148 return (void __user *)(unsigned long)value;
149}
150
Stefan Richter53dca512008-12-14 21:47:04 +0100151static inline __u64 uptr_to_u64(void __user *ptr)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500152{
153 return (__u64)(unsigned long)ptr;
154}
155
156static int fw_device_op_open(struct inode *inode, struct file *file)
157{
158 struct fw_device *device;
159 struct client *client;
160
Stefan Richter96b19062008-02-02 15:01:09 +0100161 device = fw_device_get_by_devt(inode->i_rdev);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500162 if (device == NULL)
163 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500164
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400165 if (fw_device_is_shutdown(device)) {
166 fw_device_put(device);
167 return -ENODEV;
168 }
169
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400170 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100171 if (client == NULL) {
172 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500173 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100174 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500175
Stefan Richter96b19062008-02-02 15:01:09 +0100176 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500177 spin_lock_init(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100178 idr_init(&client->resource_idr);
179 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500180 init_waitqueue_head(&client->wait);
Stefan Richterfb443032009-01-04 16:23:29 +0100181 kref_init(&client->kref);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500182
183 file->private_data = client;
184
Stefan Richterd67cfb92008-10-05 10:37:11 +0200185 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500186 list_add_tail(&client->link, &device->client_list);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200187 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500188
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500189 return 0;
190}
191
192static void queue_event(struct client *client, struct event *event,
193 void *data0, size_t size0, void *data1, size_t size1)
194{
195 unsigned long flags;
196
197 event->v[0].data = data0;
198 event->v[0].size = size0;
199 event->v[1].data = data1;
200 event->v[1].size = size1;
201
202 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100203 if (client->in_shutdown)
204 kfree(event);
205 else
206 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500207 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400208
209 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500210}
211
Stefan Richter53dca512008-12-14 21:47:04 +0100212static int dequeue_event(struct client *client,
213 char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500214{
215 unsigned long flags;
216 struct event *event;
217 size_t size, total;
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100218 int i, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500219
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100220 ret = wait_event_interruptible(client->wait,
221 !list_empty(&client->event_list) ||
222 fw_device_is_shutdown(client->device));
223 if (ret < 0)
224 return ret;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500225
226 if (list_empty(&client->event_list) &&
227 fw_device_is_shutdown(client->device))
228 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500229
230 spin_lock_irqsave(&client->lock, flags);
Stefan Richtera459b8a2008-12-21 16:49:57 +0100231 event = list_first_entry(&client->event_list, struct event, link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500232 list_del(&event->link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500233 spin_unlock_irqrestore(&client->lock, flags);
234
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500235 total = 0;
236 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
237 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500238 if (copy_to_user(buffer + total, event->v[i].data, size)) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100239 ret = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500240 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500241 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500242 total += size;
243 }
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100244 ret = total;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500245
246 out:
247 kfree(event);
248
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100249 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500250}
251
Stefan Richter53dca512008-12-14 21:47:04 +0100252static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
253 size_t count, loff_t *offset)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500254{
255 struct client *client = file->private_data;
256
257 return dequeue_event(client, buffer, count);
258}
259
Stefan Richter53dca512008-12-14 21:47:04 +0100260static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
261 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500262{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400263 struct fw_card *card = client->device->card;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400264 unsigned long flags;
265
266 spin_lock_irqsave(&card->lock, flags);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500267
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400268 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500269 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100270 event->generation = client->device->generation;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400271 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500272 event->local_node_id = card->local_node->node_id;
273 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
274 event->irm_node_id = card->irm_node->node_id;
275 event->root_node_id = card->root_node->node_id;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400276
277 spin_unlock_irqrestore(&card->lock, flags);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500278}
279
Stefan Richter53dca512008-12-14 21:47:04 +0100280static void for_each_client(struct fw_device *device,
281 void (*callback)(struct client *client))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500282{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500283 struct client *c;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500284
Stefan Richterd67cfb92008-10-05 10:37:11 +0200285 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500286 list_for_each_entry(c, &device->client_list, link)
287 callback(c);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200288 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500289}
290
Stefan Richter53dca512008-12-14 21:47:04 +0100291static void queue_bus_reset_event(struct client *client)
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500292{
Stefan Richter97c18b72009-01-04 16:23:29 +0100293 struct bus_reset_event *e;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500294
Stefan Richter97c18b72009-01-04 16:23:29 +0100295 e = kzalloc(sizeof(*e), GFP_KERNEL);
296 if (e == NULL) {
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500297 fw_notify("Out of memory when allocating bus reset event\n");
298 return;
299 }
300
Stefan Richter97c18b72009-01-04 16:23:29 +0100301 fill_bus_reset_event(&e->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500302
Stefan Richter97c18b72009-01-04 16:23:29 +0100303 queue_event(client, &e->event,
304 &e->reset, sizeof(e->reset), NULL, 0);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500305}
306
307void fw_device_cdev_update(struct fw_device *device)
308{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500309 for_each_client(device, queue_bus_reset_event);
310}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500311
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500312static void wake_up_client(struct client *client)
313{
314 wake_up_interruptible(&client->wait);
315}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500316
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500317void fw_device_cdev_remove(struct fw_device *device)
318{
319 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500320}
321
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400322static int ioctl_get_info(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500323{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400324 struct fw_cdev_get_info *get_info = buffer;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500325 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100326 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500327
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400328 client->version = get_info->version;
329 get_info->version = FW_CDEV_VERSION;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400330 get_info->card = client->device->card->index;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500331
Stefan Richterc9755e12008-03-24 20:54:28 +0100332 down_read(&fw_device_rwsem);
333
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400334 if (get_info->rom != 0) {
335 void __user *uptr = u64_to_uptr(get_info->rom);
336 size_t want = get_info->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100337 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500338
Stefan Richterc9755e12008-03-24 20:54:28 +0100339 ret = copy_to_user(uptr, client->device->config_rom,
340 min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500341 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400342 get_info->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500343
Stefan Richterc9755e12008-03-24 20:54:28 +0100344 up_read(&fw_device_rwsem);
345
346 if (ret != 0)
347 return -EFAULT;
348
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400349 client->bus_reset_closure = get_info->bus_reset_closure;
350 if (get_info->bus_reset != 0) {
351 void __user *uptr = u64_to_uptr(get_info->bus_reset);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500352
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400353 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400354 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500355 return -EFAULT;
356 }
357
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500358 return 0;
359}
360
Stefan Richter53dca512008-12-14 21:47:04 +0100361static int add_client_resource(struct client *client,
362 struct client_resource *resource, gfp_t gfp_mask)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400363{
364 unsigned long flags;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100365 int ret;
366
367 retry:
368 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
369 return -ENOMEM;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400370
371 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100372 if (client->in_shutdown)
373 ret = -ECANCELED;
374 else
375 ret = idr_get_new(&client->resource_idr, resource,
376 &resource->handle);
Stefan Richterfb443032009-01-04 16:23:29 +0100377 if (ret >= 0)
378 client_get(client);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400379 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100380
381 if (ret == -EAGAIN)
382 goto retry;
383
384 return ret < 0 ? ret : 0;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400385}
386
Stefan Richter53dca512008-12-14 21:47:04 +0100387static int release_client_resource(struct client *client, u32 handle,
388 client_resource_release_fn_t release,
389 struct client_resource **resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400390{
391 struct client_resource *r;
392 unsigned long flags;
393
394 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100395 if (client->in_shutdown)
396 r = NULL;
397 else
398 r = idr_find(&client->resource_idr, handle);
399 if (r && r->release == release)
400 idr_remove(&client->resource_idr, handle);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400401 spin_unlock_irqrestore(&client->lock, flags);
402
Jay Fenlason45ee3192008-12-21 16:47:17 +0100403 if (!(r && r->release == release))
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400404 return -EINVAL;
405
406 if (resource)
407 *resource = r;
408 else
409 r->release(client, r);
410
Stefan Richterfb443032009-01-04 16:23:29 +0100411 client_put(client);
412
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400413 return 0;
414}
415
Stefan Richter53dca512008-12-14 21:47:04 +0100416static void release_transaction(struct client *client,
417 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400418{
Stefan Richter97c18b72009-01-04 16:23:29 +0100419 struct outbound_transaction_resource *r = container_of(resource,
420 struct outbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400421
Stefan Richter97c18b72009-01-04 16:23:29 +0100422 fw_cancel_transaction(client->device->card, &r->transaction);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400423}
424
Stefan Richter53dca512008-12-14 21:47:04 +0100425static void complete_transaction(struct fw_card *card, int rcode,
426 void *payload, size_t length, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500427{
Stefan Richter97c18b72009-01-04 16:23:29 +0100428 struct outbound_transaction_event *e = data;
429 struct fw_cdev_event_response *rsp = &e->response;
430 struct client *client = e->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500431 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500432
Stefan Richter97c18b72009-01-04 16:23:29 +0100433 if (length < rsp->length)
434 rsp->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500435 if (rcode == RCODE_COMPLETE)
Stefan Richter97c18b72009-01-04 16:23:29 +0100436 memcpy(rsp->data, payload, rsp->length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500437
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500438 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100439 /*
Stefan Richterfb443032009-01-04 16:23:29 +0100440 * 1. If called while in shutdown, the idr tree must be left untouched.
441 * The idr handle will be removed and the client reference will be
442 * dropped later.
443 * 2. If the call chain was release_client_resource ->
444 * release_transaction -> complete_transaction (instead of a normal
445 * conclusion of the transaction), i.e. if this resource was already
446 * unregistered from the idr, the client reference will be dropped
447 * by release_client_resource and we must not drop it here.
Jay Fenlason45ee3192008-12-21 16:47:17 +0100448 */
Stefan Richterfb443032009-01-04 16:23:29 +0100449 if (!client->in_shutdown &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100450 idr_find(&client->resource_idr, e->r.resource.handle)) {
451 idr_remove(&client->resource_idr, e->r.resource.handle);
Stefan Richterfb443032009-01-04 16:23:29 +0100452 /* Drop the idr's reference */
453 client_put(client);
454 }
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500455 spin_unlock_irqrestore(&client->lock, flags);
456
Stefan Richter97c18b72009-01-04 16:23:29 +0100457 rsp->type = FW_CDEV_EVENT_RESPONSE;
458 rsp->rcode = rcode;
David Moore8401d922008-07-29 23:46:25 -0700459
460 /*
Stefan Richter97c18b72009-01-04 16:23:29 +0100461 * In the case that sizeof(*rsp) doesn't align with the position of the
David Moore8401d922008-07-29 23:46:25 -0700462 * data, and the read is short, preserve an extra copy of the data
463 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
464 * for short reads and some apps depended on it, this is both safe
465 * and prudent for compatibility.
466 */
Stefan Richter97c18b72009-01-04 16:23:29 +0100467 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
468 queue_event(client, &e->event, rsp, sizeof(*rsp),
469 rsp->data, rsp->length);
David Moore8401d922008-07-29 23:46:25 -0700470 else
Stefan Richter97c18b72009-01-04 16:23:29 +0100471 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
David Moore8401d922008-07-29 23:46:25 -0700472 NULL, 0);
Stefan Richterfb443032009-01-04 16:23:29 +0100473
474 /* Drop the transaction callback's reference */
475 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500476}
477
Jeff Garzik350958f2007-05-27 07:09:18 -0400478static int ioctl_send_request(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500479{
480 struct fw_device *device = client->device;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400481 struct fw_cdev_send_request *request = buffer;
Stefan Richter97c18b72009-01-04 16:23:29 +0100482 struct outbound_transaction_event *e;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100483 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500484
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500485 /* What is the biggest size we'll accept, really? */
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400486 if (request->length > 4096)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500487 return -EINVAL;
488
Stefan Richter97c18b72009-01-04 16:23:29 +0100489 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
490 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500491 return -ENOMEM;
492
Stefan Richter97c18b72009-01-04 16:23:29 +0100493 e->client = client;
494 e->response.length = request->length;
495 e->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500496
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400497 if (request->data &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100498 copy_from_user(e->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400499 u64_to_uptr(request->data), request->length)) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100500 ret = -EFAULT;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100501 goto failed;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100502 }
503
504 switch (request->tcode) {
505 case TCODE_WRITE_QUADLET_REQUEST:
506 case TCODE_WRITE_BLOCK_REQUEST:
507 case TCODE_READ_QUADLET_REQUEST:
508 case TCODE_READ_BLOCK_REQUEST:
509 case TCODE_LOCK_MASK_SWAP:
510 case TCODE_LOCK_COMPARE_SWAP:
511 case TCODE_LOCK_FETCH_ADD:
512 case TCODE_LOCK_LITTLE_ADD:
513 case TCODE_LOCK_BOUNDED_ADD:
514 case TCODE_LOCK_WRAP_ADD:
515 case TCODE_LOCK_VENDOR_DEPENDENT:
516 break;
517 default:
518 ret = -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100519 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500520 }
521
Stefan Richter97c18b72009-01-04 16:23:29 +0100522 e->r.resource.release = release_transaction;
523 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100524 if (ret < 0)
525 goto failed;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500526
Stefan Richterfb443032009-01-04 16:23:29 +0100527 /* Get a reference for the transaction callback */
528 client_get(client);
529
Stefan Richter97c18b72009-01-04 16:23:29 +0100530 fw_send_request(device->card, &e->r.transaction,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400531 request->tcode & 0x1f,
Stefan Richter907293d2007-01-23 21:11:43 +0100532 device->node->node_id,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400533 request->generation,
Stefan Richterf1397492007-06-10 21:31:36 +0200534 device->max_speed,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400535 request->offset,
Stefan Richter97c18b72009-01-04 16:23:29 +0100536 e->response.data, request->length,
537 complete_transaction, e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500538
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400539 if (request->data)
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400540 return sizeof(request) + request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500541 else
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400542 return sizeof(request);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100543 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100544 kfree(e);
Stefan Richter1f3125a2008-12-05 22:44:42 +0100545
546 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500547}
548
Stefan Richter53dca512008-12-14 21:47:04 +0100549static void release_request(struct client *client,
550 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400551{
Stefan Richter97c18b72009-01-04 16:23:29 +0100552 struct inbound_transaction_resource *r = container_of(resource,
553 struct inbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400554
Stefan Richter97c18b72009-01-04 16:23:29 +0100555 fw_send_response(client->device->card, r->request,
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400556 RCODE_CONFLICT_ERROR);
Stefan Richter97c18b72009-01-04 16:23:29 +0100557 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400558}
559
Stefan Richter97c18b72009-01-04 16:23:29 +0100560static void handle_request(struct fw_card *card, struct fw_request *request,
Stefan Richter53dca512008-12-14 21:47:04 +0100561 int tcode, int destination, int source,
562 int generation, int speed,
563 unsigned long long offset,
564 void *payload, size_t length, void *callback_data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500565{
Stefan Richter97c18b72009-01-04 16:23:29 +0100566 struct address_handler_resource *handler = callback_data;
567 struct inbound_transaction_resource *r;
568 struct inbound_transaction_event *e;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100569 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500570
Stefan Richter97c18b72009-01-04 16:23:29 +0100571 r = kmalloc(sizeof(*r), GFP_ATOMIC);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400572 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Stefan Richter97c18b72009-01-04 16:23:29 +0100573 if (r == NULL || e == NULL)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100574 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500575
Stefan Richter97c18b72009-01-04 16:23:29 +0100576 r->request = request;
577 r->data = payload;
578 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500579
Stefan Richter97c18b72009-01-04 16:23:29 +0100580 r->resource.release = release_request;
581 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100582 if (ret < 0)
583 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500584
585 e->request.type = FW_CDEV_EVENT_REQUEST;
586 e->request.tcode = tcode;
587 e->request.offset = offset;
588 e->request.length = length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100589 e->request.handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500590 e->request.closure = handler->closure;
591
Stefan Richter97c18b72009-01-04 16:23:29 +0100592 queue_event(handler->client, &e->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400593 &e->request, sizeof(e->request), payload, length);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100594 return;
595
596 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100597 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100598 kfree(e);
Stefan Richter97c18b72009-01-04 16:23:29 +0100599 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500600}
601
Stefan Richter53dca512008-12-14 21:47:04 +0100602static void release_address_handler(struct client *client,
603 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400604{
Stefan Richter97c18b72009-01-04 16:23:29 +0100605 struct address_handler_resource *r =
606 container_of(resource, struct address_handler_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400607
Stefan Richter97c18b72009-01-04 16:23:29 +0100608 fw_core_remove_address_handler(&r->handler);
609 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400610}
611
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400612static int ioctl_allocate(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500613{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400614 struct fw_cdev_allocate *request = buffer;
Stefan Richter97c18b72009-01-04 16:23:29 +0100615 struct address_handler_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500616 struct fw_address_region region;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100617 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500618
Stefan Richter97c18b72009-01-04 16:23:29 +0100619 r = kmalloc(sizeof(*r), GFP_KERNEL);
620 if (r == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500621 return -ENOMEM;
622
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400623 region.start = request->offset;
624 region.end = request->offset + request->length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100625 r->handler.length = request->length;
626 r->handler.address_callback = handle_request;
627 r->handler.callback_data = r;
628 r->closure = request->closure;
629 r->client = client;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500630
Stefan Richter97c18b72009-01-04 16:23:29 +0100631 ret = fw_core_add_address_handler(&r->handler, &region);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100632 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100633 kfree(r);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100634 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500635 }
636
Stefan Richter97c18b72009-01-04 16:23:29 +0100637 r->resource.release = release_address_handler;
638 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100639 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100640 release_address_handler(client, &r->resource);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100641 return ret;
642 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100643 request->handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500644
645 return 0;
646}
647
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400648static int ioctl_deallocate(struct client *client, void *buffer)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400649{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400650 struct fw_cdev_deallocate *request = buffer;
Kristian Høgsberg94723162007-03-14 17:34:55 -0400651
Jay Fenlason45ee3192008-12-21 16:47:17 +0100652 return release_client_resource(client, request->handle,
653 release_address_handler, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400654}
655
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400656static int ioctl_send_response(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500657{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400658 struct fw_cdev_send_response *request = buffer;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400659 struct client_resource *resource;
Stefan Richter97c18b72009-01-04 16:23:29 +0100660 struct inbound_transaction_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500661
Jay Fenlason45ee3192008-12-21 16:47:17 +0100662 if (release_client_resource(client, request->handle,
663 release_request, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500664 return -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100665
Stefan Richter97c18b72009-01-04 16:23:29 +0100666 r = container_of(resource, struct inbound_transaction_resource,
667 resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400668 if (request->length < r->length)
669 r->length = request->length;
670 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500671 return -EFAULT;
672
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400673 fw_send_response(client->device->card, r->request, request->rcode);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500674 kfree(r);
675
676 return 0;
677}
678
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400679static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500680{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400681 struct fw_cdev_initiate_bus_reset *request = buffer;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500682 int short_reset;
683
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400684 short_reset = (request->type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500685
686 return fw_core_initiate_bus_reset(client->device->card, short_reset);
687}
688
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400689static void release_descriptor(struct client *client,
690 struct client_resource *resource)
691{
Stefan Richter97c18b72009-01-04 16:23:29 +0100692 struct descriptor_resource *r =
693 container_of(resource, struct descriptor_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400694
Stefan Richter97c18b72009-01-04 16:23:29 +0100695 fw_core_remove_descriptor(&r->descriptor);
696 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400697}
698
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400699static int ioctl_add_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200700{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400701 struct fw_cdev_add_descriptor *request = buffer;
Stefan Richter97c18b72009-01-04 16:23:29 +0100702 struct descriptor_resource *r;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100703 int ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200704
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400705 if (request->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200706 return -EINVAL;
707
Stefan Richter97c18b72009-01-04 16:23:29 +0100708 r = kmalloc(sizeof(*r) + request->length * 4, GFP_KERNEL);
709 if (r == NULL)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200710 return -ENOMEM;
711
Stefan Richter97c18b72009-01-04 16:23:29 +0100712 if (copy_from_user(r->data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400713 u64_to_uptr(request->data), request->length * 4)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100714 ret = -EFAULT;
715 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200716 }
717
Stefan Richter97c18b72009-01-04 16:23:29 +0100718 r->descriptor.length = request->length;
719 r->descriptor.immediate = request->immediate;
720 r->descriptor.key = request->key;
721 r->descriptor.data = r->data;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200722
Stefan Richter97c18b72009-01-04 16:23:29 +0100723 ret = fw_core_add_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100724 if (ret < 0)
725 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200726
Stefan Richter97c18b72009-01-04 16:23:29 +0100727 r->resource.release = release_descriptor;
728 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100729 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100730 fw_core_remove_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100731 goto failed;
732 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100733 request->handle = r->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200734
735 return 0;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100736 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100737 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100738
739 return ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200740}
741
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400742static int ioctl_remove_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200743{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400744 struct fw_cdev_remove_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200745
Jay Fenlason45ee3192008-12-21 16:47:17 +0100746 return release_client_resource(client, request->handle,
747 release_descriptor, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200748}
749
Stefan Richter53dca512008-12-14 21:47:04 +0100750static void iso_callback(struct fw_iso_context *context, u32 cycle,
751 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500752{
753 struct client *client = data;
Stefan Richter97c18b72009-01-04 16:23:29 +0100754 struct iso_interrupt_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500755
Stefan Richter97c18b72009-01-04 16:23:29 +0100756 e = kzalloc(sizeof(*e) + header_length, GFP_ATOMIC);
757 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500758 return;
759
Stefan Richter97c18b72009-01-04 16:23:29 +0100760 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
761 e->interrupt.closure = client->iso_closure;
762 e->interrupt.cycle = cycle;
763 e->interrupt.header_length = header_length;
764 memcpy(e->interrupt.header, header, header_length);
765 queue_event(client, &e->event, &e->interrupt,
766 sizeof(e->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500767}
768
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400769static int ioctl_create_iso_context(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500770{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400771 struct fw_cdev_create_iso_context *request = buffer;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400772 struct fw_iso_context *context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500773
Stefan Richterfae60312008-02-20 21:10:06 +0100774 /* We only support one context at this time. */
775 if (client->iso_context != NULL)
776 return -EBUSY;
777
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400778 if (request->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500779 return -EINVAL;
780
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400781 switch (request->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400782 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400783 if (request->header_size < 4 || (request->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400784 return -EINVAL;
785
786 break;
787
788 case FW_ISO_CONTEXT_TRANSMIT:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400789 if (request->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400790 return -EINVAL;
791
792 break;
793
794 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500795 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400796 }
797
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400798 context = fw_iso_context_create(client->device->card,
799 request->type,
800 request->channel,
801 request->speed,
802 request->header_size,
803 iso_callback, client);
804 if (IS_ERR(context))
805 return PTR_ERR(context);
806
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400807 client->iso_closure = request->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400808 client->iso_context = context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500809
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400810 /* We only support one context at this time. */
811 request->handle = 0;
812
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500813 return 0;
814}
815
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400816/* Macros for decoding the iso packet control header. */
817#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
818#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
819#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +0200820#define GET_TAG(v) (((v) >> 18) & 0x03)
821#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400822#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
823
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400824static int ioctl_queue_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500825{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400826 struct fw_cdev_queue_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500827 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500828 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200829 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400830 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500831 int count;
832 struct {
833 struct fw_iso_packet packet;
834 u8 header[256];
835 } u;
836
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400837 if (ctx == NULL || request->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500838 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500839
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400840 /*
841 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500842 * the iso buffer, and the pointer points inside the buffer,
843 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500844 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500845 * payload_length == 0 through. In other words, if no packets
846 * use the indirect payload, the iso buffer need not be mapped
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400847 * and the request->data pointer is ignored.
848 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500849
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400850 payload = (unsigned long)request->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200851 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400852 if (request->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200853 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500854 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200855 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500856 }
857
Al Viro1ccc9142007-10-14 19:34:40 +0100858 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
859
860 if (!access_ok(VERIFY_READ, p, request->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500861 return -EFAULT;
862
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400863 end = (void __user *)p + request->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500864 count = 0;
865 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400866 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500867 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400868 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
869 u.packet.interrupt = GET_INTERRUPT(control);
870 u.packet.skip = GET_SKIP(control);
871 u.packet.tag = GET_TAG(control);
872 u.packet.sy = GET_SY(control);
873 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500874
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500875 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500876 header_length = u.packet.header_length;
877 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400878 /*
879 * We require that header_length is a multiple of
880 * the fixed header size, ctx->header_size.
881 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500882 if (ctx->header_size == 0) {
883 if (u.packet.header_length > 0)
884 return -EINVAL;
885 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500886 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500887 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500888 header_length = 0;
889 }
890
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500891 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500892 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500893 if (next > end)
894 return -EINVAL;
895 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500896 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500897 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500898 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500899 u.packet.header_length + u.packet.payload_length > 0)
900 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200901 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500902 return -EINVAL;
903
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500904 if (fw_iso_context_queue(ctx, &u.packet,
905 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500906 break;
907
908 p = next;
909 payload += u.packet.payload_length;
910 count++;
911 }
912
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400913 request->size -= uptr_to_u64(p) - request->packets;
914 request->packets = uptr_to_u64(p);
915 request->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500916
917 return count;
918}
919
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400920static int ioctl_start_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500921{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400922 struct fw_cdev_start_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500923
Stefan Richterfae60312008-02-20 21:10:06 +0100924 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400925 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +0100926
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400927 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400928 if (request->tags == 0 || request->tags > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400929 return -EINVAL;
930
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400931 if (request->sync > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400932 return -EINVAL;
933 }
934
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400935 return fw_iso_context_start(client->iso_context, request->cycle,
936 request->sync, request->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500937}
938
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400939static int ioctl_stop_iso(struct client *client, void *buffer)
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500940{
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400941 struct fw_cdev_stop_iso *request = buffer;
942
Stefan Richterfae60312008-02-20 21:10:06 +0100943 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400944 return -EINVAL;
945
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500946 return fw_iso_context_stop(client->iso_context);
947}
948
Stefan Richtera64408b2007-09-29 10:41:58 +0200949static int ioctl_get_cycle_timer(struct client *client, void *buffer)
950{
951 struct fw_cdev_get_cycle_timer *request = buffer;
952 struct fw_card *card = client->device->card;
953 unsigned long long bus_time;
954 struct timeval tv;
955 unsigned long flags;
956
957 preempt_disable();
958 local_irq_save(flags);
959
960 bus_time = card->driver->get_bus_time(card);
961 do_gettimeofday(&tv);
962
963 local_irq_restore(flags);
964 preempt_enable();
965
966 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
967 request->cycle_timer = bus_time & 0xffffffff;
968 return 0;
969}
970
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400971static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
972 ioctl_get_info,
973 ioctl_send_request,
974 ioctl_allocate,
975 ioctl_deallocate,
976 ioctl_send_response,
977 ioctl_initiate_bus_reset,
978 ioctl_add_descriptor,
979 ioctl_remove_descriptor,
980 ioctl_create_iso_context,
981 ioctl_queue_iso,
982 ioctl_start_iso,
983 ioctl_stop_iso,
Stefan Richtera64408b2007-09-29 10:41:58 +0200984 ioctl_get_cycle_timer,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400985};
986
Stefan Richter53dca512008-12-14 21:47:04 +0100987static int dispatch_ioctl(struct client *client,
988 unsigned int cmd, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500989{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400990 char buffer[256];
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100991 int ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400992
993 if (_IOC_TYPE(cmd) != '#' ||
994 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500995 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400996
997 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400998 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400999 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
1000 return -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001001 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001002
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001003 ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
1004 if (ret < 0)
1005 return ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001006
1007 if (_IOC_DIR(cmd) & _IOC_READ) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001008 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001009 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
1010 return -EFAULT;
1011 }
1012
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001013 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001014}
1015
Stefan Richter53dca512008-12-14 21:47:04 +01001016static long fw_device_op_ioctl(struct file *file,
1017 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001018{
1019 struct client *client = file->private_data;
1020
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001021 if (fw_device_is_shutdown(client->device))
1022 return -ENODEV;
1023
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001024 return dispatch_ioctl(client, cmd, (void __user *) arg);
1025}
1026
1027#ifdef CONFIG_COMPAT
Stefan Richter53dca512008-12-14 21:47:04 +01001028static long fw_device_op_compat_ioctl(struct file *file,
1029 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001030{
1031 struct client *client = file->private_data;
1032
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001033 if (fw_device_is_shutdown(client->device))
1034 return -ENODEV;
1035
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001036 return dispatch_ioctl(client, cmd, compat_ptr(arg));
1037}
1038#endif
1039
1040static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1041{
1042 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001043 enum dma_data_direction direction;
1044 unsigned long size;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001045 int page_count, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001046
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001047 if (fw_device_is_shutdown(client->device))
1048 return -ENODEV;
1049
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001050 /* FIXME: We could support multiple buffers, but we don't. */
1051 if (client->buffer.pages != NULL)
1052 return -EBUSY;
1053
1054 if (!(vma->vm_flags & VM_SHARED))
1055 return -EINVAL;
1056
1057 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001058 return -EINVAL;
1059
1060 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001061 size = vma->vm_end - vma->vm_start;
1062 page_count = size >> PAGE_SHIFT;
1063 if (size & ~PAGE_MASK)
1064 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001065
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001066 if (vma->vm_flags & VM_WRITE)
1067 direction = DMA_TO_DEVICE;
1068 else
1069 direction = DMA_FROM_DEVICE;
1070
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001071 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1072 page_count, direction);
1073 if (ret < 0)
1074 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001075
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001076 ret = fw_iso_buffer_map(&client->buffer, vma);
1077 if (ret < 0)
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001078 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1079
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001080 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001081}
1082
Jay Fenlason45ee3192008-12-21 16:47:17 +01001083static int shutdown_resource(int id, void *p, void *data)
1084{
1085 struct client_resource *r = p;
1086 struct client *client = data;
1087
1088 r->release(client, r);
Stefan Richterfb443032009-01-04 16:23:29 +01001089 client_put(client);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001090
1091 return 0;
1092}
1093
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001094static int fw_device_op_release(struct inode *inode, struct file *file)
1095{
1096 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001097 struct event *e, *next_e;
Jay Fenlason45ee3192008-12-21 16:47:17 +01001098 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001099
Stefan Richter97811e32008-12-14 19:19:23 +01001100 mutex_lock(&client->device->client_list_mutex);
1101 list_del(&client->link);
1102 mutex_unlock(&client->device->client_list_mutex);
1103
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001104 if (client->buffer.pages)
1105 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1106
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001107 if (client->iso_context)
1108 fw_iso_context_destroy(client->iso_context);
1109
Jay Fenlason45ee3192008-12-21 16:47:17 +01001110 /* Freeze client->resource_idr and client->event_list */
1111 spin_lock_irqsave(&client->lock, flags);
1112 client->in_shutdown = true;
1113 spin_unlock_irqrestore(&client->lock, flags);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001114
Jay Fenlason45ee3192008-12-21 16:47:17 +01001115 idr_for_each(&client->resource_idr, shutdown_resource, client);
1116 idr_remove_all(&client->resource_idr);
1117 idr_destroy(&client->resource_idr);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001118
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001119 list_for_each_entry_safe(e, next_e, &client->event_list, link)
1120 kfree(e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001121
Stefan Richterfb443032009-01-04 16:23:29 +01001122 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001123
1124 return 0;
1125}
1126
1127static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1128{
1129 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001130 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001131
1132 poll_wait(file, &client->wait, pt);
1133
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001134 if (fw_device_is_shutdown(client->device))
1135 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001136 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001137 mask |= POLLIN | POLLRDNORM;
1138
1139 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001140}
1141
Stefan Richter21ebcd12007-01-14 15:29:07 +01001142const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001143 .owner = THIS_MODULE,
1144 .open = fw_device_op_open,
1145 .read = fw_device_op_read,
1146 .unlocked_ioctl = fw_device_op_ioctl,
1147 .poll = fw_device_op_poll,
1148 .release = fw_device_op_release,
1149 .mmap = fw_device_op_mmap,
1150
1151#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001152 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001153#endif
1154};