blob: 40cc9732dc2870ea4ce042ac7455ee0bc5eebe23 [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>
23#include <linux/wait.h>
24#include <linux/errno.h>
25#include <linux/device.h>
26#include <linux/vmalloc.h>
27#include <linux/poll.h>
Stefan Richtera64408b2007-09-29 10:41:58 +020028#include <linux/preempt.h>
29#include <linux/time.h>
Jay Fenlasoncf417e542008-10-03 11:19:09 -040030#include <linux/spinlock.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050031#include <linux/delay.h>
32#include <linux/mm.h>
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -050033#include <linux/idr.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050034#include <linux/compat.h>
Kristian Høgsberg9640d3d2007-04-30 15:03:15 -040035#include <linux/firewire-cdev.h>
Stefan Richtera64408b2007-09-29 10:41:58 +020036#include <asm/system.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050037#include <asm/uaccess.h>
38#include "fw-transaction.h"
39#include "fw-topology.h"
40#include "fw-device.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050041
Kristian Høgsberg3964a442007-03-27 01:43:41 -040042struct client;
43struct client_resource {
44 struct list_head link;
45 void (*release)(struct client *client, struct client_resource *r);
46 u32 handle;
47};
48
Kristian Høgsbergc781c062007-05-07 20:33:32 -040049/*
50 * dequeue_event() just kfree()'s the event, so the event has to be
51 * the first field in the struct.
52 */
53
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050054struct event {
55 struct { void *data; size_t size; } v[2];
56 struct list_head link;
57};
58
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050059struct bus_reset {
60 struct event event;
61 struct fw_cdev_event_bus_reset reset;
62};
63
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050064struct response {
65 struct event event;
66 struct fw_transaction transaction;
67 struct client *client;
Kristian Høgsberg3964a442007-03-27 01:43:41 -040068 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050069 struct fw_cdev_event_response response;
70};
71
72struct iso_interrupt {
73 struct event event;
74 struct fw_cdev_event_iso_interrupt interrupt;
75};
76
77struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050078 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050079 struct fw_device *device;
80 spinlock_t lock;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +020081 u32 resource_handle;
Kristian Høgsberg3964a442007-03-27 01:43:41 -040082 struct list_head resource_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050083 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050084 wait_queue_head_t wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040085 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050086
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050087 struct fw_iso_context *iso_context;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -040088 u64 iso_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050089 struct fw_iso_buffer buffer;
90 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050091
92 struct list_head link;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050093};
94
95static inline void __user *
96u64_to_uptr(__u64 value)
97{
98 return (void __user *)(unsigned long)value;
99}
100
101static inline __u64
102uptr_to_u64(void __user *ptr)
103{
104 return (__u64)(unsigned long)ptr;
105}
106
107static int fw_device_op_open(struct inode *inode, struct file *file)
108{
109 struct fw_device *device;
110 struct client *client;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500111 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500112
Stefan Richter96b19062008-02-02 15:01:09 +0100113 device = fw_device_get_by_devt(inode->i_rdev);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500114 if (device == NULL)
115 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500116
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400117 if (fw_device_is_shutdown(device)) {
118 fw_device_put(device);
119 return -ENODEV;
120 }
121
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400122 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100123 if (client == NULL) {
124 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500125 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100126 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500127
Stefan Richter96b19062008-02-02 15:01:09 +0100128 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500129 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400130 INIT_LIST_HEAD(&client->resource_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500131 spin_lock_init(&client->lock);
132 init_waitqueue_head(&client->wait);
133
134 file->private_data = client;
135
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400136 spin_lock_irqsave(&device->client_list_lock, flags);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500137 list_add_tail(&client->link, &device->client_list);
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400138 spin_unlock_irqrestore(&device->client_list_lock, flags);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500139
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500140 return 0;
141}
142
143static void queue_event(struct client *client, struct event *event,
144 void *data0, size_t size0, void *data1, size_t size1)
145{
146 unsigned long flags;
147
148 event->v[0].data = data0;
149 event->v[0].size = size0;
150 event->v[1].data = data1;
151 event->v[1].size = size1;
152
153 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500154 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500155 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400156
157 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500158}
159
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500160static int
161dequeue_event(struct client *client, char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500162{
163 unsigned long flags;
164 struct event *event;
165 size_t size, total;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500166 int i, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500167
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500168 retval = wait_event_interruptible(client->wait,
169 !list_empty(&client->event_list) ||
170 fw_device_is_shutdown(client->device));
171 if (retval < 0)
172 return retval;
173
174 if (list_empty(&client->event_list) &&
175 fw_device_is_shutdown(client->device))
176 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500177
178 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500179 event = container_of(client->event_list.next, struct event, link);
180 list_del(&event->link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500181 spin_unlock_irqrestore(&client->lock, flags);
182
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500183 total = 0;
184 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
185 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500186 if (copy_to_user(buffer + total, event->v[i].data, size)) {
187 retval = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500188 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500189 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500190 total += size;
191 }
192 retval = total;
193
194 out:
195 kfree(event);
196
197 return retval;
198}
199
200static ssize_t
201fw_device_op_read(struct file *file,
202 char __user *buffer, size_t count, loff_t *offset)
203{
204 struct client *client = file->private_data;
205
206 return dequeue_event(client, buffer, count);
207}
208
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500209static void
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500210fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400211 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500212{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400213 struct fw_card *card = client->device->card;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400214 unsigned long flags;
215
216 spin_lock_irqsave(&card->lock, flags);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500217
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400218 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500219 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100220 event->generation = client->device->generation;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400221 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500222 event->local_node_id = card->local_node->node_id;
223 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
224 event->irm_node_id = card->irm_node->node_id;
225 event->root_node_id = card->root_node->node_id;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400226
227 spin_unlock_irqrestore(&card->lock, flags);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500228}
229
230static void
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500231for_each_client(struct fw_device *device,
232 void (*callback)(struct client *client))
233{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500234 struct client *c;
235 unsigned long flags;
236
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400237 spin_lock_irqsave(&device->client_list_lock, flags);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500238
239 list_for_each_entry(c, &device->client_list, link)
240 callback(c);
241
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400242 spin_unlock_irqrestore(&device->client_list_lock, flags);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500243}
244
245static void
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500246queue_bus_reset_event(struct client *client)
247{
248 struct bus_reset *bus_reset;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500249
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400250 bus_reset = kzalloc(sizeof(*bus_reset), GFP_ATOMIC);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500251 if (bus_reset == NULL) {
252 fw_notify("Out of memory when allocating bus reset event\n");
253 return;
254 }
255
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400256 fill_bus_reset_event(&bus_reset->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500257
258 queue_event(client, &bus_reset->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400259 &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500260}
261
262void fw_device_cdev_update(struct fw_device *device)
263{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500264 for_each_client(device, queue_bus_reset_event);
265}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500266
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500267static void wake_up_client(struct client *client)
268{
269 wake_up_interruptible(&client->wait);
270}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500271
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500272void fw_device_cdev_remove(struct fw_device *device)
273{
274 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500275}
276
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400277static int ioctl_get_info(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500278{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400279 struct fw_cdev_get_info *get_info = buffer;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500280 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100281 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500282
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400283 client->version = get_info->version;
284 get_info->version = FW_CDEV_VERSION;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400285 get_info->card = client->device->card->index;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500286
Stefan Richterc9755e12008-03-24 20:54:28 +0100287 down_read(&fw_device_rwsem);
288
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400289 if (get_info->rom != 0) {
290 void __user *uptr = u64_to_uptr(get_info->rom);
291 size_t want = get_info->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100292 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500293
Stefan Richterc9755e12008-03-24 20:54:28 +0100294 ret = copy_to_user(uptr, client->device->config_rom,
295 min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500296 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400297 get_info->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500298
Stefan Richterc9755e12008-03-24 20:54:28 +0100299 up_read(&fw_device_rwsem);
300
301 if (ret != 0)
302 return -EFAULT;
303
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400304 client->bus_reset_closure = get_info->bus_reset_closure;
305 if (get_info->bus_reset != 0) {
306 void __user *uptr = u64_to_uptr(get_info->bus_reset);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500307
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400308 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400309 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500310 return -EFAULT;
311 }
312
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500313 return 0;
314}
315
316static void
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400317add_client_resource(struct client *client, struct client_resource *resource)
318{
319 unsigned long flags;
320
321 spin_lock_irqsave(&client->lock, flags);
322 list_add_tail(&resource->link, &client->resource_list);
323 resource->handle = client->resource_handle++;
324 spin_unlock_irqrestore(&client->lock, flags);
325}
326
327static int
328release_client_resource(struct client *client, u32 handle,
329 struct client_resource **resource)
330{
331 struct client_resource *r;
332 unsigned long flags;
333
334 spin_lock_irqsave(&client->lock, flags);
335 list_for_each_entry(r, &client->resource_list, link) {
336 if (r->handle == handle) {
337 list_del(&r->link);
338 break;
339 }
340 }
341 spin_unlock_irqrestore(&client->lock, flags);
342
343 if (&r->link == &client->resource_list)
344 return -EINVAL;
345
346 if (resource)
347 *resource = r;
348 else
349 r->release(client, r);
350
351 return 0;
352}
353
354static void
355release_transaction(struct client *client, struct client_resource *resource)
356{
357 struct response *response =
358 container_of(resource, struct response, resource);
359
360 fw_cancel_transaction(client->device->card, &response->transaction);
361}
362
363static void
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500364complete_transaction(struct fw_card *card, int rcode,
365 void *payload, size_t length, void *data)
366{
367 struct response *response = data;
368 struct client *client = response->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500369 unsigned long flags;
David Moore8401d922008-07-29 23:46:25 -0700370 struct fw_cdev_event_response *r = &response->response;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500371
David Moore8401d922008-07-29 23:46:25 -0700372 if (length < r->length)
373 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500374 if (rcode == RCODE_COMPLETE)
David Moore8401d922008-07-29 23:46:25 -0700375 memcpy(r->data, payload, r->length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500376
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500377 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400378 list_del(&response->resource.link);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500379 spin_unlock_irqrestore(&client->lock, flags);
380
David Moore8401d922008-07-29 23:46:25 -0700381 r->type = FW_CDEV_EVENT_RESPONSE;
382 r->rcode = rcode;
383
384 /*
385 * In the case that sizeof(*r) doesn't align with the position of the
386 * data, and the read is short, preserve an extra copy of the data
387 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
388 * for short reads and some apps depended on it, this is both safe
389 * and prudent for compatibility.
390 */
391 if (r->length <= sizeof(*r) - offsetof(typeof(*r), data))
392 queue_event(client, &response->event, r, sizeof(*r),
393 r->data, r->length);
394 else
395 queue_event(client, &response->event, r, sizeof(*r) + r->length,
396 NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500397}
398
Jeff Garzik350958f2007-05-27 07:09:18 -0400399static int ioctl_send_request(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500400{
401 struct fw_device *device = client->device;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400402 struct fw_cdev_send_request *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500403 struct response *response;
404
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500405 /* What is the biggest size we'll accept, really? */
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400406 if (request->length > 4096)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500407 return -EINVAL;
408
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400409 response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500410 if (response == NULL)
411 return -ENOMEM;
412
413 response->client = client;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400414 response->response.length = request->length;
415 response->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500416
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400417 if (request->data &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500418 copy_from_user(response->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400419 u64_to_uptr(request->data), request->length)) {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500420 kfree(response);
421 return -EFAULT;
422 }
423
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400424 response->resource.release = release_transaction;
425 add_client_resource(client, &response->resource);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500426
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500427 fw_send_request(device->card, &response->transaction,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400428 request->tcode & 0x1f,
Stefan Richter907293d2007-01-23 21:11:43 +0100429 device->node->node_id,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400430 request->generation,
Stefan Richterf1397492007-06-10 21:31:36 +0200431 device->max_speed,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400432 request->offset,
433 response->response.data, request->length,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500434 complete_transaction, response);
435
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400436 if (request->data)
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400437 return sizeof(request) + request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500438 else
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400439 return sizeof(request);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500440}
441
442struct address_handler {
443 struct fw_address_handler handler;
444 __u64 closure;
445 struct client *client;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400446 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500447};
448
449struct request {
450 struct fw_request *request;
451 void *data;
452 size_t length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400453 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500454};
455
456struct request_event {
457 struct event event;
458 struct fw_cdev_event_request request;
459};
460
461static void
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400462release_request(struct client *client, struct client_resource *resource)
463{
464 struct request *request =
465 container_of(resource, struct request, resource);
466
467 fw_send_response(client->device->card, request->request,
468 RCODE_CONFLICT_ERROR);
469 kfree(request);
470}
471
472static void
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500473handle_request(struct fw_card *card, struct fw_request *r,
474 int tcode, int destination, int source,
475 int generation, int speed,
476 unsigned long long offset,
477 void *payload, size_t length, void *callback_data)
478{
479 struct address_handler *handler = callback_data;
480 struct request *request;
481 struct request_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500482 struct client *client = handler->client;
483
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400484 request = kmalloc(sizeof(*request), GFP_ATOMIC);
485 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500486 if (request == NULL || e == NULL) {
487 kfree(request);
488 kfree(e);
489 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
490 return;
491 }
492
493 request->request = r;
494 request->data = payload;
495 request->length = length;
496
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400497 request->resource.release = release_request;
498 add_client_resource(client, &request->resource);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500499
500 e->request.type = FW_CDEV_EVENT_REQUEST;
501 e->request.tcode = tcode;
502 e->request.offset = offset;
503 e->request.length = length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400504 e->request.handle = request->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500505 e->request.closure = handler->closure;
506
507 queue_event(client, &e->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400508 &e->request, sizeof(e->request), payload, length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500509}
510
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400511static void
512release_address_handler(struct client *client,
513 struct client_resource *resource)
514{
515 struct address_handler *handler =
516 container_of(resource, struct address_handler, resource);
517
518 fw_core_remove_address_handler(&handler->handler);
519 kfree(handler);
520}
521
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400522static int ioctl_allocate(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500523{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400524 struct fw_cdev_allocate *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500525 struct address_handler *handler;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500526 struct fw_address_region region;
527
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400528 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500529 if (handler == NULL)
530 return -ENOMEM;
531
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400532 region.start = request->offset;
533 region.end = request->offset + request->length;
534 handler->handler.length = request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500535 handler->handler.address_callback = handle_request;
536 handler->handler.callback_data = handler;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400537 handler->closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500538 handler->client = client;
539
540 if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
541 kfree(handler);
542 return -EBUSY;
543 }
544
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400545 handler->resource.release = release_address_handler;
546 add_client_resource(client, &handler->resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400547 request->handle = handler->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500548
549 return 0;
550}
551
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400552static int ioctl_deallocate(struct client *client, void *buffer)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400553{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400554 struct fw_cdev_deallocate *request = buffer;
Kristian Høgsberg94723162007-03-14 17:34:55 -0400555
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400556 return release_client_resource(client, request->handle, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400557}
558
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400559static int ioctl_send_response(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500560{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400561 struct fw_cdev_send_response *request = buffer;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400562 struct client_resource *resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500563 struct request *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500564
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400565 if (release_client_resource(client, request->handle, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500566 return -EINVAL;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400567 r = container_of(resource, struct request, resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400568 if (request->length < r->length)
569 r->length = request->length;
570 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500571 return -EFAULT;
572
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400573 fw_send_response(client->device->card, r->request, request->rcode);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500574 kfree(r);
575
576 return 0;
577}
578
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400579static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500580{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400581 struct fw_cdev_initiate_bus_reset *request = buffer;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500582 int short_reset;
583
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400584 short_reset = (request->type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500585
586 return fw_core_initiate_bus_reset(client->device->card, short_reset);
587}
588
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200589struct descriptor {
590 struct fw_descriptor d;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400591 struct client_resource resource;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200592 u32 data[0];
593};
594
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400595static void release_descriptor(struct client *client,
596 struct client_resource *resource)
597{
598 struct descriptor *descriptor =
599 container_of(resource, struct descriptor, resource);
600
601 fw_core_remove_descriptor(&descriptor->d);
602 kfree(descriptor);
603}
604
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400605static int ioctl_add_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200606{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400607 struct fw_cdev_add_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200608 struct descriptor *descriptor;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200609 int retval;
610
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400611 if (request->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200612 return -EINVAL;
613
614 descriptor =
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400615 kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200616 if (descriptor == NULL)
617 return -ENOMEM;
618
619 if (copy_from_user(descriptor->data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400620 u64_to_uptr(request->data), request->length * 4)) {
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200621 kfree(descriptor);
622 return -EFAULT;
623 }
624
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400625 descriptor->d.length = request->length;
626 descriptor->d.immediate = request->immediate;
627 descriptor->d.key = request->key;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200628 descriptor->d.data = descriptor->data;
629
630 retval = fw_core_add_descriptor(&descriptor->d);
631 if (retval < 0) {
632 kfree(descriptor);
633 return retval;
634 }
635
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400636 descriptor->resource.release = release_descriptor;
637 add_client_resource(client, &descriptor->resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400638 request->handle = descriptor->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200639
640 return 0;
641}
642
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400643static int ioctl_remove_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200644{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400645 struct fw_cdev_remove_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200646
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400647 return release_client_resource(client, request->handle, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200648}
649
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500650static void
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500651iso_callback(struct fw_iso_context *context, u32 cycle,
652 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500653{
654 struct client *client = data;
Stefan Richter930e4b72007-08-03 20:56:31 +0200655 struct iso_interrupt *irq;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500656
Stefan Richter930e4b72007-08-03 20:56:31 +0200657 irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
658 if (irq == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500659 return;
660
Stefan Richter930e4b72007-08-03 20:56:31 +0200661 irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
662 irq->interrupt.closure = client->iso_closure;
663 irq->interrupt.cycle = cycle;
664 irq->interrupt.header_length = header_length;
665 memcpy(irq->interrupt.header, header, header_length);
666 queue_event(client, &irq->event, &irq->interrupt,
667 sizeof(irq->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500668}
669
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400670static int ioctl_create_iso_context(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500671{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400672 struct fw_cdev_create_iso_context *request = buffer;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400673 struct fw_iso_context *context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500674
Stefan Richterfae60312008-02-20 21:10:06 +0100675 /* We only support one context at this time. */
676 if (client->iso_context != NULL)
677 return -EBUSY;
678
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400679 if (request->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500680 return -EINVAL;
681
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400682 switch (request->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400683 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400684 if (request->header_size < 4 || (request->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400685 return -EINVAL;
686
687 break;
688
689 case FW_ISO_CONTEXT_TRANSMIT:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400690 if (request->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400691 return -EINVAL;
692
693 break;
694
695 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500696 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400697 }
698
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400699 context = fw_iso_context_create(client->device->card,
700 request->type,
701 request->channel,
702 request->speed,
703 request->header_size,
704 iso_callback, client);
705 if (IS_ERR(context))
706 return PTR_ERR(context);
707
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400708 client->iso_closure = request->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400709 client->iso_context = context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500710
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400711 /* We only support one context at this time. */
712 request->handle = 0;
713
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500714 return 0;
715}
716
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400717/* Macros for decoding the iso packet control header. */
718#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
719#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
720#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +0200721#define GET_TAG(v) (((v) >> 18) & 0x03)
722#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400723#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
724
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400725static int ioctl_queue_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500726{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400727 struct fw_cdev_queue_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500728 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500729 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200730 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400731 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500732 int count;
733 struct {
734 struct fw_iso_packet packet;
735 u8 header[256];
736 } u;
737
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400738 if (ctx == NULL || request->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500739 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500740
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400741 /*
742 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500743 * the iso buffer, and the pointer points inside the buffer,
744 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500745 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500746 * payload_length == 0 through. In other words, if no packets
747 * use the indirect payload, the iso buffer need not be mapped
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400748 * and the request->data pointer is ignored.
749 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500750
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400751 payload = (unsigned long)request->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200752 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400753 if (request->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200754 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500755 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200756 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500757 }
758
Al Viro1ccc9142007-10-14 19:34:40 +0100759 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
760
761 if (!access_ok(VERIFY_READ, p, request->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500762 return -EFAULT;
763
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400764 end = (void __user *)p + request->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500765 count = 0;
766 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400767 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500768 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400769 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
770 u.packet.interrupt = GET_INTERRUPT(control);
771 u.packet.skip = GET_SKIP(control);
772 u.packet.tag = GET_TAG(control);
773 u.packet.sy = GET_SY(control);
774 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500775
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500776 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500777 header_length = u.packet.header_length;
778 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400779 /*
780 * We require that header_length is a multiple of
781 * the fixed header size, ctx->header_size.
782 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500783 if (ctx->header_size == 0) {
784 if (u.packet.header_length > 0)
785 return -EINVAL;
786 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500787 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500788 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500789 header_length = 0;
790 }
791
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500792 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500793 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500794 if (next > end)
795 return -EINVAL;
796 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500797 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500798 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500799 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500800 u.packet.header_length + u.packet.payload_length > 0)
801 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200802 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500803 return -EINVAL;
804
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500805 if (fw_iso_context_queue(ctx, &u.packet,
806 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500807 break;
808
809 p = next;
810 payload += u.packet.payload_length;
811 count++;
812 }
813
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400814 request->size -= uptr_to_u64(p) - request->packets;
815 request->packets = uptr_to_u64(p);
816 request->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500817
818 return count;
819}
820
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400821static int ioctl_start_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500822{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400823 struct fw_cdev_start_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500824
Stefan Richterfae60312008-02-20 21:10:06 +0100825 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400826 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +0100827
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400828 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400829 if (request->tags == 0 || request->tags > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400830 return -EINVAL;
831
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400832 if (request->sync > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400833 return -EINVAL;
834 }
835
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400836 return fw_iso_context_start(client->iso_context, request->cycle,
837 request->sync, request->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500838}
839
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400840static int ioctl_stop_iso(struct client *client, void *buffer)
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500841{
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400842 struct fw_cdev_stop_iso *request = buffer;
843
Stefan Richterfae60312008-02-20 21:10:06 +0100844 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400845 return -EINVAL;
846
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500847 return fw_iso_context_stop(client->iso_context);
848}
849
Stefan Richtera64408b2007-09-29 10:41:58 +0200850static int ioctl_get_cycle_timer(struct client *client, void *buffer)
851{
852 struct fw_cdev_get_cycle_timer *request = buffer;
853 struct fw_card *card = client->device->card;
854 unsigned long long bus_time;
855 struct timeval tv;
856 unsigned long flags;
857
858 preempt_disable();
859 local_irq_save(flags);
860
861 bus_time = card->driver->get_bus_time(card);
862 do_gettimeofday(&tv);
863
864 local_irq_restore(flags);
865 preempt_enable();
866
867 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
868 request->cycle_timer = bus_time & 0xffffffff;
869 return 0;
870}
871
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400872static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
873 ioctl_get_info,
874 ioctl_send_request,
875 ioctl_allocate,
876 ioctl_deallocate,
877 ioctl_send_response,
878 ioctl_initiate_bus_reset,
879 ioctl_add_descriptor,
880 ioctl_remove_descriptor,
881 ioctl_create_iso_context,
882 ioctl_queue_iso,
883 ioctl_start_iso,
884 ioctl_stop_iso,
Stefan Richtera64408b2007-09-29 10:41:58 +0200885 ioctl_get_cycle_timer,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400886};
887
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500888static int
889dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
890{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400891 char buffer[256];
892 int retval;
893
894 if (_IOC_TYPE(cmd) != '#' ||
895 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500896 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400897
898 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400899 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400900 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
901 return -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500902 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400903
904 retval = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
905 if (retval < 0)
906 return retval;
907
908 if (_IOC_DIR(cmd) & _IOC_READ) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400909 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400910 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
911 return -EFAULT;
912 }
913
Stefan Richter99692f72008-09-12 18:20:16 +0200914 return retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500915}
916
917static long
918fw_device_op_ioctl(struct file *file,
919 unsigned int cmd, unsigned long arg)
920{
921 struct client *client = file->private_data;
922
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400923 if (fw_device_is_shutdown(client->device))
924 return -ENODEV;
925
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500926 return dispatch_ioctl(client, cmd, (void __user *) arg);
927}
928
929#ifdef CONFIG_COMPAT
930static long
931fw_device_op_compat_ioctl(struct file *file,
932 unsigned int cmd, unsigned long arg)
933{
934 struct client *client = file->private_data;
935
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400936 if (fw_device_is_shutdown(client->device))
937 return -ENODEV;
938
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500939 return dispatch_ioctl(client, cmd, compat_ptr(arg));
940}
941#endif
942
943static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
944{
945 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500946 enum dma_data_direction direction;
947 unsigned long size;
948 int page_count, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500949
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400950 if (fw_device_is_shutdown(client->device))
951 return -ENODEV;
952
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500953 /* FIXME: We could support multiple buffers, but we don't. */
954 if (client->buffer.pages != NULL)
955 return -EBUSY;
956
957 if (!(vma->vm_flags & VM_SHARED))
958 return -EINVAL;
959
960 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500961 return -EINVAL;
962
963 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500964 size = vma->vm_end - vma->vm_start;
965 page_count = size >> PAGE_SHIFT;
966 if (size & ~PAGE_MASK)
967 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500968
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500969 if (vma->vm_flags & VM_WRITE)
970 direction = DMA_TO_DEVICE;
971 else
972 direction = DMA_FROM_DEVICE;
973
974 retval = fw_iso_buffer_init(&client->buffer, client->device->card,
975 page_count, direction);
976 if (retval < 0)
977 return retval;
978
979 retval = fw_iso_buffer_map(&client->buffer, vma);
980 if (retval < 0)
981 fw_iso_buffer_destroy(&client->buffer, client->device->card);
982
983 return retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500984}
985
986static int fw_device_op_release(struct inode *inode, struct file *file)
987{
988 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500989 struct event *e, *next_e;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400990 struct client_resource *r, *next_r;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500991 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500992
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500993 if (client->buffer.pages)
994 fw_iso_buffer_destroy(&client->buffer, client->device->card);
995
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500996 if (client->iso_context)
997 fw_iso_context_destroy(client->iso_context);
998
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400999 list_for_each_entry_safe(r, next_r, &client->resource_list, link)
1000 r->release(client, r);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001001
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001002 /*
1003 * FIXME: We should wait for the async tasklets to stop
1004 * running before freeing the memory.
1005 */
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001006
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001007 list_for_each_entry_safe(e, next_e, &client->event_list, link)
1008 kfree(e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001009
Jay Fenlasoncf417e542008-10-03 11:19:09 -04001010 spin_lock_irqsave(&client->device->client_list_lock, flags);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -05001011 list_del(&client->link);
Jay Fenlasoncf417e542008-10-03 11:19:09 -04001012 spin_unlock_irqrestore(&client->device->client_list_lock, flags);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -05001013
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001014 fw_device_put(client->device);
1015 kfree(client);
1016
1017 return 0;
1018}
1019
1020static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1021{
1022 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001023 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001024
1025 poll_wait(file, &client->wait, pt);
1026
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001027 if (fw_device_is_shutdown(client->device))
1028 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001029 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001030 mask |= POLLIN | POLLRDNORM;
1031
1032 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001033}
1034
Stefan Richter21ebcd12007-01-14 15:29:07 +01001035const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001036 .owner = THIS_MODULE,
1037 .open = fw_device_op_open,
1038 .read = fw_device_op_read,
1039 .unlocked_ioctl = fw_device_op_ioctl,
1040 .poll = fw_device_op_poll,
1041 .release = fw_device_op_release,
1042 .mmap = fw_device_op_mmap,
1043
1044#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001045 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001046#endif
1047};