blob: dda14015e873dce10c4bfbd796b03952d079b9b3 [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>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050030#include <linux/delay.h>
31#include <linux/mm.h>
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -050032#include <linux/idr.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050033#include <linux/compat.h>
Kristian Høgsberg9640d3d2007-04-30 15:03:15 -040034#include <linux/firewire-cdev.h>
Stefan Richtera64408b2007-09-29 10:41:58 +020035#include <asm/system.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050036#include <asm/uaccess.h>
37#include "fw-transaction.h"
38#include "fw-topology.h"
39#include "fw-device.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050040
Kristian Høgsberg3964a442007-03-27 01:43:41 -040041struct client;
42struct client_resource {
43 struct list_head link;
44 void (*release)(struct client *client, struct client_resource *r);
45 u32 handle;
46};
47
Kristian Høgsbergc781c062007-05-07 20:33:32 -040048/*
49 * dequeue_event() just kfree()'s the event, so the event has to be
50 * the first field in the struct.
51 */
52
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050053struct event {
54 struct { void *data; size_t size; } v[2];
55 struct list_head link;
56};
57
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050058struct bus_reset {
59 struct event event;
60 struct fw_cdev_event_bus_reset reset;
61};
62
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050063struct response {
64 struct event event;
65 struct fw_transaction transaction;
66 struct client *client;
Kristian Høgsberg3964a442007-03-27 01:43:41 -040067 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050068 struct fw_cdev_event_response response;
69};
70
71struct iso_interrupt {
72 struct event event;
73 struct fw_cdev_event_iso_interrupt interrupt;
74};
75
76struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050077 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050078 struct fw_device *device;
79 spinlock_t lock;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +020080 u32 resource_handle;
Kristian Høgsberg3964a442007-03-27 01:43:41 -040081 struct list_head resource_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050082 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050083 wait_queue_head_t wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040084 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050085
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050086 struct fw_iso_context *iso_context;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -040087 u64 iso_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050088 struct fw_iso_buffer buffer;
89 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050090
91 struct list_head link;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050092};
93
94static inline void __user *
95u64_to_uptr(__u64 value)
96{
97 return (void __user *)(unsigned long)value;
98}
99
100static inline __u64
101uptr_to_u64(void __user *ptr)
102{
103 return (__u64)(unsigned long)ptr;
104}
105
106static int fw_device_op_open(struct inode *inode, struct file *file)
107{
108 struct fw_device *device;
109 struct client *client;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500110 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500111
Stefan Richter96b19062008-02-02 15:01:09 +0100112 device = fw_device_get_by_devt(inode->i_rdev);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500113 if (device == NULL)
114 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500115
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400116 if (fw_device_is_shutdown(device)) {
117 fw_device_put(device);
118 return -ENODEV;
119 }
120
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400121 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100122 if (client == NULL) {
123 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500124 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100125 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500126
Stefan Richter96b19062008-02-02 15:01:09 +0100127 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500128 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400129 INIT_LIST_HEAD(&client->resource_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500130 spin_lock_init(&client->lock);
131 init_waitqueue_head(&client->wait);
132
133 file->private_data = client;
134
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500135 spin_lock_irqsave(&device->card->lock, flags);
136 list_add_tail(&client->link, &device->client_list);
137 spin_unlock_irqrestore(&device->card->lock, flags);
138
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500139 return 0;
140}
141
142static void queue_event(struct client *client, struct event *event,
143 void *data0, size_t size0, void *data1, size_t size1)
144{
145 unsigned long flags;
146
147 event->v[0].data = data0;
148 event->v[0].size = size0;
149 event->v[1].data = data1;
150 event->v[1].size = size1;
151
152 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500153 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500154 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400155
156 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500157}
158
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500159static int
160dequeue_event(struct client *client, char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500161{
162 unsigned long flags;
163 struct event *event;
164 size_t size, total;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500165 int i, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500166
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500167 retval = wait_event_interruptible(client->wait,
168 !list_empty(&client->event_list) ||
169 fw_device_is_shutdown(client->device));
170 if (retval < 0)
171 return retval;
172
173 if (list_empty(&client->event_list) &&
174 fw_device_is_shutdown(client->device))
175 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500176
177 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500178 event = container_of(client->event_list.next, struct event, link);
179 list_del(&event->link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500180 spin_unlock_irqrestore(&client->lock, flags);
181
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500182 total = 0;
183 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
184 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500185 if (copy_to_user(buffer + total, event->v[i].data, size)) {
186 retval = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500187 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500188 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500189 total += size;
190 }
191 retval = total;
192
193 out:
194 kfree(event);
195
196 return retval;
197}
198
199static ssize_t
200fw_device_op_read(struct file *file,
201 char __user *buffer, size_t count, loff_t *offset)
202{
203 struct client *client = file->private_data;
204
205 return dequeue_event(client, buffer, count);
206}
207
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500208static void
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500209fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400210 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500211{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400212 struct fw_card *card = client->device->card;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500213
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400214 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500215 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100216 event->generation = client->device->generation;
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100217 smp_rmb(); /* node_id must not be older than generation */
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400218 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500219 event->local_node_id = card->local_node->node_id;
220 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
221 event->irm_node_id = card->irm_node->node_id;
222 event->root_node_id = card->root_node->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500223}
224
225static void
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500226for_each_client(struct fw_device *device,
227 void (*callback)(struct client *client))
228{
229 struct fw_card *card = device->card;
230 struct client *c;
231 unsigned long flags;
232
233 spin_lock_irqsave(&card->lock, flags);
234
235 list_for_each_entry(c, &device->client_list, link)
236 callback(c);
237
238 spin_unlock_irqrestore(&card->lock, flags);
239}
240
241static void
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500242queue_bus_reset_event(struct client *client)
243{
244 struct bus_reset *bus_reset;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500245
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400246 bus_reset = kzalloc(sizeof(*bus_reset), GFP_ATOMIC);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500247 if (bus_reset == NULL) {
248 fw_notify("Out of memory when allocating bus reset event\n");
249 return;
250 }
251
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400252 fill_bus_reset_event(&bus_reset->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500253
254 queue_event(client, &bus_reset->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400255 &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500256}
257
258void fw_device_cdev_update(struct fw_device *device)
259{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500260 for_each_client(device, queue_bus_reset_event);
261}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500262
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500263static void wake_up_client(struct client *client)
264{
265 wake_up_interruptible(&client->wait);
266}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500267
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500268void fw_device_cdev_remove(struct fw_device *device)
269{
270 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500271}
272
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400273static int ioctl_get_info(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500274{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400275 struct fw_cdev_get_info *get_info = buffer;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500276 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100277 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500278
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400279 client->version = get_info->version;
280 get_info->version = FW_CDEV_VERSION;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500281
Stefan Richterc9755e12008-03-24 20:54:28 +0100282 down_read(&fw_device_rwsem);
283
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400284 if (get_info->rom != 0) {
285 void __user *uptr = u64_to_uptr(get_info->rom);
286 size_t want = get_info->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100287 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500288
Stefan Richterc9755e12008-03-24 20:54:28 +0100289 ret = copy_to_user(uptr, client->device->config_rom,
290 min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500291 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400292 get_info->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500293
Stefan Richterc9755e12008-03-24 20:54:28 +0100294 up_read(&fw_device_rwsem);
295
296 if (ret != 0)
297 return -EFAULT;
298
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400299 client->bus_reset_closure = get_info->bus_reset_closure;
300 if (get_info->bus_reset != 0) {
301 void __user *uptr = u64_to_uptr(get_info->bus_reset);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500302
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400303 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400304 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500305 return -EFAULT;
306 }
307
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400308 get_info->card = client->device->card->index;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500309
310 return 0;
311}
312
313static void
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400314add_client_resource(struct client *client, struct client_resource *resource)
315{
316 unsigned long flags;
317
318 spin_lock_irqsave(&client->lock, flags);
319 list_add_tail(&resource->link, &client->resource_list);
320 resource->handle = client->resource_handle++;
321 spin_unlock_irqrestore(&client->lock, flags);
322}
323
324static int
325release_client_resource(struct client *client, u32 handle,
326 struct client_resource **resource)
327{
328 struct client_resource *r;
329 unsigned long flags;
330
331 spin_lock_irqsave(&client->lock, flags);
332 list_for_each_entry(r, &client->resource_list, link) {
333 if (r->handle == handle) {
334 list_del(&r->link);
335 break;
336 }
337 }
338 spin_unlock_irqrestore(&client->lock, flags);
339
340 if (&r->link == &client->resource_list)
341 return -EINVAL;
342
343 if (resource)
344 *resource = r;
345 else
346 r->release(client, r);
347
348 return 0;
349}
350
351static void
352release_transaction(struct client *client, struct client_resource *resource)
353{
354 struct response *response =
355 container_of(resource, struct response, resource);
356
357 fw_cancel_transaction(client->device->card, &response->transaction);
358}
359
360static void
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500361complete_transaction(struct fw_card *card, int rcode,
362 void *payload, size_t length, void *data)
363{
364 struct response *response = data;
365 struct client *client = response->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500366 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500367
368 if (length < response->response.length)
369 response->response.length = length;
370 if (rcode == RCODE_COMPLETE)
371 memcpy(response->response.data, payload,
372 response->response.length);
373
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500374 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400375 list_del(&response->resource.link);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500376 spin_unlock_irqrestore(&client->lock, flags);
377
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500378 response->response.type = FW_CDEV_EVENT_RESPONSE;
379 response->response.rcode = rcode;
380 queue_event(client, &response->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400381 &response->response, sizeof(response->response),
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500382 response->response.data, response->response.length);
383}
384
Jeff Garzik350958f2007-05-27 07:09:18 -0400385static int ioctl_send_request(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500386{
387 struct fw_device *device = client->device;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400388 struct fw_cdev_send_request *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500389 struct response *response;
390
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500391 /* What is the biggest size we'll accept, really? */
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400392 if (request->length > 4096)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500393 return -EINVAL;
394
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400395 response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500396 if (response == NULL)
397 return -ENOMEM;
398
399 response->client = client;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400400 response->response.length = request->length;
401 response->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500402
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400403 if (request->data &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500404 copy_from_user(response->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400405 u64_to_uptr(request->data), request->length)) {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500406 kfree(response);
407 return -EFAULT;
408 }
409
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400410 response->resource.release = release_transaction;
411 add_client_resource(client, &response->resource);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500412
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500413 fw_send_request(device->card, &response->transaction,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400414 request->tcode & 0x1f,
Stefan Richter907293d2007-01-23 21:11:43 +0100415 device->node->node_id,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400416 request->generation,
Stefan Richterf1397492007-06-10 21:31:36 +0200417 device->max_speed,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400418 request->offset,
419 response->response.data, request->length,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500420 complete_transaction, response);
421
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400422 if (request->data)
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400423 return sizeof(request) + request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500424 else
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400425 return sizeof(request);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500426}
427
428struct address_handler {
429 struct fw_address_handler handler;
430 __u64 closure;
431 struct client *client;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400432 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500433};
434
435struct request {
436 struct fw_request *request;
437 void *data;
438 size_t length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400439 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500440};
441
442struct request_event {
443 struct event event;
444 struct fw_cdev_event_request request;
445};
446
447static void
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400448release_request(struct client *client, struct client_resource *resource)
449{
450 struct request *request =
451 container_of(resource, struct request, resource);
452
453 fw_send_response(client->device->card, request->request,
454 RCODE_CONFLICT_ERROR);
455 kfree(request);
456}
457
458static void
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500459handle_request(struct fw_card *card, struct fw_request *r,
460 int tcode, int destination, int source,
461 int generation, int speed,
462 unsigned long long offset,
463 void *payload, size_t length, void *callback_data)
464{
465 struct address_handler *handler = callback_data;
466 struct request *request;
467 struct request_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500468 struct client *client = handler->client;
469
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400470 request = kmalloc(sizeof(*request), GFP_ATOMIC);
471 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500472 if (request == NULL || e == NULL) {
473 kfree(request);
474 kfree(e);
475 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
476 return;
477 }
478
479 request->request = r;
480 request->data = payload;
481 request->length = length;
482
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400483 request->resource.release = release_request;
484 add_client_resource(client, &request->resource);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500485
486 e->request.type = FW_CDEV_EVENT_REQUEST;
487 e->request.tcode = tcode;
488 e->request.offset = offset;
489 e->request.length = length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400490 e->request.handle = request->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500491 e->request.closure = handler->closure;
492
493 queue_event(client, &e->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400494 &e->request, sizeof(e->request), payload, length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500495}
496
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400497static void
498release_address_handler(struct client *client,
499 struct client_resource *resource)
500{
501 struct address_handler *handler =
502 container_of(resource, struct address_handler, resource);
503
504 fw_core_remove_address_handler(&handler->handler);
505 kfree(handler);
506}
507
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400508static int ioctl_allocate(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500509{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400510 struct fw_cdev_allocate *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500511 struct address_handler *handler;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500512 struct fw_address_region region;
513
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400514 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500515 if (handler == NULL)
516 return -ENOMEM;
517
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400518 region.start = request->offset;
519 region.end = request->offset + request->length;
520 handler->handler.length = request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500521 handler->handler.address_callback = handle_request;
522 handler->handler.callback_data = handler;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400523 handler->closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500524 handler->client = client;
525
526 if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
527 kfree(handler);
528 return -EBUSY;
529 }
530
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400531 handler->resource.release = release_address_handler;
532 add_client_resource(client, &handler->resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400533 request->handle = handler->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500534
535 return 0;
536}
537
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400538static int ioctl_deallocate(struct client *client, void *buffer)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400539{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400540 struct fw_cdev_deallocate *request = buffer;
Kristian Høgsberg94723162007-03-14 17:34:55 -0400541
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400542 return release_client_resource(client, request->handle, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400543}
544
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400545static int ioctl_send_response(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500546{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400547 struct fw_cdev_send_response *request = buffer;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400548 struct client_resource *resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500549 struct request *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500550
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400551 if (release_client_resource(client, request->handle, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500552 return -EINVAL;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400553 r = container_of(resource, struct request, resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400554 if (request->length < r->length)
555 r->length = request->length;
556 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500557 return -EFAULT;
558
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400559 fw_send_response(client->device->card, r->request, request->rcode);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500560 kfree(r);
561
562 return 0;
563}
564
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400565static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500566{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400567 struct fw_cdev_initiate_bus_reset *request = buffer;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500568 int short_reset;
569
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400570 short_reset = (request->type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500571
572 return fw_core_initiate_bus_reset(client->device->card, short_reset);
573}
574
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200575struct descriptor {
576 struct fw_descriptor d;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400577 struct client_resource resource;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200578 u32 data[0];
579};
580
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400581static void release_descriptor(struct client *client,
582 struct client_resource *resource)
583{
584 struct descriptor *descriptor =
585 container_of(resource, struct descriptor, resource);
586
587 fw_core_remove_descriptor(&descriptor->d);
588 kfree(descriptor);
589}
590
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400591static int ioctl_add_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200592{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400593 struct fw_cdev_add_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200594 struct descriptor *descriptor;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200595 int retval;
596
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400597 if (request->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200598 return -EINVAL;
599
600 descriptor =
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400601 kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200602 if (descriptor == NULL)
603 return -ENOMEM;
604
605 if (copy_from_user(descriptor->data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400606 u64_to_uptr(request->data), request->length * 4)) {
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200607 kfree(descriptor);
608 return -EFAULT;
609 }
610
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400611 descriptor->d.length = request->length;
612 descriptor->d.immediate = request->immediate;
613 descriptor->d.key = request->key;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200614 descriptor->d.data = descriptor->data;
615
616 retval = fw_core_add_descriptor(&descriptor->d);
617 if (retval < 0) {
618 kfree(descriptor);
619 return retval;
620 }
621
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400622 descriptor->resource.release = release_descriptor;
623 add_client_resource(client, &descriptor->resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400624 request->handle = descriptor->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200625
626 return 0;
627}
628
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400629static int ioctl_remove_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200630{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400631 struct fw_cdev_remove_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200632
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400633 return release_client_resource(client, request->handle, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200634}
635
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500636static void
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500637iso_callback(struct fw_iso_context *context, u32 cycle,
638 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500639{
640 struct client *client = data;
Stefan Richter930e4b72007-08-03 20:56:31 +0200641 struct iso_interrupt *irq;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500642
Stefan Richter930e4b72007-08-03 20:56:31 +0200643 irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
644 if (irq == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500645 return;
646
Stefan Richter930e4b72007-08-03 20:56:31 +0200647 irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
648 irq->interrupt.closure = client->iso_closure;
649 irq->interrupt.cycle = cycle;
650 irq->interrupt.header_length = header_length;
651 memcpy(irq->interrupt.header, header, header_length);
652 queue_event(client, &irq->event, &irq->interrupt,
653 sizeof(irq->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500654}
655
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400656static int ioctl_create_iso_context(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_create_iso_context *request = buffer;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400659 struct fw_iso_context *context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500660
Stefan Richterfae60312008-02-20 21:10:06 +0100661 /* We only support one context at this time. */
662 if (client->iso_context != NULL)
663 return -EBUSY;
664
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400665 if (request->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500666 return -EINVAL;
667
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400668 switch (request->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400669 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400670 if (request->header_size < 4 || (request->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400671 return -EINVAL;
672
673 break;
674
675 case FW_ISO_CONTEXT_TRANSMIT:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400676 if (request->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400677 return -EINVAL;
678
679 break;
680
681 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500682 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400683 }
684
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400685 context = fw_iso_context_create(client->device->card,
686 request->type,
687 request->channel,
688 request->speed,
689 request->header_size,
690 iso_callback, client);
691 if (IS_ERR(context))
692 return PTR_ERR(context);
693
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400694 client->iso_closure = request->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400695 client->iso_context = context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500696
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400697 /* We only support one context at this time. */
698 request->handle = 0;
699
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500700 return 0;
701}
702
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400703/* Macros for decoding the iso packet control header. */
704#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
705#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
706#define GET_SKIP(v) (((v) >> 17) & 0x01)
707#define GET_TAG(v) (((v) >> 18) & 0x02)
708#define GET_SY(v) (((v) >> 20) & 0x04)
709#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
710
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400711static int ioctl_queue_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500712{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400713 struct fw_cdev_queue_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500714 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500715 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200716 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400717 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500718 int count;
719 struct {
720 struct fw_iso_packet packet;
721 u8 header[256];
722 } u;
723
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400724 if (ctx == NULL || request->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500725 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500726
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400727 /*
728 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500729 * the iso buffer, and the pointer points inside the buffer,
730 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500731 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500732 * payload_length == 0 through. In other words, if no packets
733 * use the indirect payload, the iso buffer need not be mapped
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400734 * and the request->data pointer is ignored.
735 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500736
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400737 payload = (unsigned long)request->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200738 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400739 if (request->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200740 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500741 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200742 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500743 }
744
Al Viro1ccc9142007-10-14 19:34:40 +0100745 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
746
747 if (!access_ok(VERIFY_READ, p, request->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500748 return -EFAULT;
749
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400750 end = (void __user *)p + request->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500751 count = 0;
752 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400753 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500754 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400755 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
756 u.packet.interrupt = GET_INTERRUPT(control);
757 u.packet.skip = GET_SKIP(control);
758 u.packet.tag = GET_TAG(control);
759 u.packet.sy = GET_SY(control);
760 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500761
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500762 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500763 header_length = u.packet.header_length;
764 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400765 /*
766 * We require that header_length is a multiple of
767 * the fixed header size, ctx->header_size.
768 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500769 if (ctx->header_size == 0) {
770 if (u.packet.header_length > 0)
771 return -EINVAL;
772 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500773 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500774 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500775 header_length = 0;
776 }
777
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500778 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500779 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500780 if (next > end)
781 return -EINVAL;
782 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500783 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500784 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500785 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500786 u.packet.header_length + u.packet.payload_length > 0)
787 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200788 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500789 return -EINVAL;
790
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500791 if (fw_iso_context_queue(ctx, &u.packet,
792 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500793 break;
794
795 p = next;
796 payload += u.packet.payload_length;
797 count++;
798 }
799
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400800 request->size -= uptr_to_u64(p) - request->packets;
801 request->packets = uptr_to_u64(p);
802 request->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500803
804 return count;
805}
806
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400807static int ioctl_start_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500808{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400809 struct fw_cdev_start_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500810
Stefan Richterfae60312008-02-20 21:10:06 +0100811 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400812 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +0100813
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400814 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400815 if (request->tags == 0 || request->tags > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400816 return -EINVAL;
817
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400818 if (request->sync > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400819 return -EINVAL;
820 }
821
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400822 return fw_iso_context_start(client->iso_context, request->cycle,
823 request->sync, request->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500824}
825
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400826static int ioctl_stop_iso(struct client *client, void *buffer)
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500827{
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400828 struct fw_cdev_stop_iso *request = buffer;
829
Stefan Richterfae60312008-02-20 21:10:06 +0100830 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400831 return -EINVAL;
832
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500833 return fw_iso_context_stop(client->iso_context);
834}
835
Stefan Richtera64408b2007-09-29 10:41:58 +0200836static int ioctl_get_cycle_timer(struct client *client, void *buffer)
837{
838 struct fw_cdev_get_cycle_timer *request = buffer;
839 struct fw_card *card = client->device->card;
840 unsigned long long bus_time;
841 struct timeval tv;
842 unsigned long flags;
843
844 preempt_disable();
845 local_irq_save(flags);
846
847 bus_time = card->driver->get_bus_time(card);
848 do_gettimeofday(&tv);
849
850 local_irq_restore(flags);
851 preempt_enable();
852
853 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
854 request->cycle_timer = bus_time & 0xffffffff;
855 return 0;
856}
857
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400858static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
859 ioctl_get_info,
860 ioctl_send_request,
861 ioctl_allocate,
862 ioctl_deallocate,
863 ioctl_send_response,
864 ioctl_initiate_bus_reset,
865 ioctl_add_descriptor,
866 ioctl_remove_descriptor,
867 ioctl_create_iso_context,
868 ioctl_queue_iso,
869 ioctl_start_iso,
870 ioctl_stop_iso,
Stefan Richtera64408b2007-09-29 10:41:58 +0200871 ioctl_get_cycle_timer,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400872};
873
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500874static int
875dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
876{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400877 char buffer[256];
878 int retval;
879
880 if (_IOC_TYPE(cmd) != '#' ||
881 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500882 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400883
884 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400885 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400886 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
887 return -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500888 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400889
890 retval = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
891 if (retval < 0)
892 return retval;
893
894 if (_IOC_DIR(cmd) & _IOC_READ) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400895 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400896 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
897 return -EFAULT;
898 }
899
900 return 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500901}
902
903static long
904fw_device_op_ioctl(struct file *file,
905 unsigned int cmd, unsigned long arg)
906{
907 struct client *client = file->private_data;
908
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400909 if (fw_device_is_shutdown(client->device))
910 return -ENODEV;
911
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500912 return dispatch_ioctl(client, cmd, (void __user *) arg);
913}
914
915#ifdef CONFIG_COMPAT
916static long
917fw_device_op_compat_ioctl(struct file *file,
918 unsigned int cmd, unsigned long arg)
919{
920 struct client *client = file->private_data;
921
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400922 if (fw_device_is_shutdown(client->device))
923 return -ENODEV;
924
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500925 return dispatch_ioctl(client, cmd, compat_ptr(arg));
926}
927#endif
928
929static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
930{
931 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500932 enum dma_data_direction direction;
933 unsigned long size;
934 int page_count, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500935
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400936 if (fw_device_is_shutdown(client->device))
937 return -ENODEV;
938
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500939 /* FIXME: We could support multiple buffers, but we don't. */
940 if (client->buffer.pages != NULL)
941 return -EBUSY;
942
943 if (!(vma->vm_flags & VM_SHARED))
944 return -EINVAL;
945
946 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500947 return -EINVAL;
948
949 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500950 size = vma->vm_end - vma->vm_start;
951 page_count = size >> PAGE_SHIFT;
952 if (size & ~PAGE_MASK)
953 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500954
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500955 if (vma->vm_flags & VM_WRITE)
956 direction = DMA_TO_DEVICE;
957 else
958 direction = DMA_FROM_DEVICE;
959
960 retval = fw_iso_buffer_init(&client->buffer, client->device->card,
961 page_count, direction);
962 if (retval < 0)
963 return retval;
964
965 retval = fw_iso_buffer_map(&client->buffer, vma);
966 if (retval < 0)
967 fw_iso_buffer_destroy(&client->buffer, client->device->card);
968
969 return retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500970}
971
972static int fw_device_op_release(struct inode *inode, struct file *file)
973{
974 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500975 struct event *e, *next_e;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400976 struct client_resource *r, *next_r;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500977 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500978
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500979 if (client->buffer.pages)
980 fw_iso_buffer_destroy(&client->buffer, client->device->card);
981
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500982 if (client->iso_context)
983 fw_iso_context_destroy(client->iso_context);
984
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400985 list_for_each_entry_safe(r, next_r, &client->resource_list, link)
986 r->release(client, r);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200987
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400988 /*
989 * FIXME: We should wait for the async tasklets to stop
990 * running before freeing the memory.
991 */
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500992
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500993 list_for_each_entry_safe(e, next_e, &client->event_list, link)
994 kfree(e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500995
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500996 spin_lock_irqsave(&client->device->card->lock, flags);
997 list_del(&client->link);
998 spin_unlock_irqrestore(&client->device->card->lock, flags);
999
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001000 fw_device_put(client->device);
1001 kfree(client);
1002
1003 return 0;
1004}
1005
1006static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1007{
1008 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001009 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001010
1011 poll_wait(file, &client->wait, pt);
1012
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001013 if (fw_device_is_shutdown(client->device))
1014 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001015 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001016 mask |= POLLIN | POLLRDNORM;
1017
1018 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001019}
1020
Stefan Richter21ebcd12007-01-14 15:29:07 +01001021const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001022 .owner = THIS_MODULE,
1023 .open = fw_device_op_open,
1024 .read = fw_device_op_read,
1025 .unlocked_ioctl = fw_device_op_ioctl,
1026 .poll = fw_device_op_poll,
1027 .release = fw_device_op_release,
1028 .mmap = fw_device_op_mmap,
1029
1030#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001031 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001032#endif
1033};