blob: 4a541921a14a26e2a24c8c814b92f17035098de3 [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
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400116 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100117 if (client == NULL) {
118 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500119 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100120 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500121
Stefan Richter96b19062008-02-02 15:01:09 +0100122 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500123 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400124 INIT_LIST_HEAD(&client->resource_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500125 spin_lock_init(&client->lock);
126 init_waitqueue_head(&client->wait);
127
128 file->private_data = client;
129
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500130 spin_lock_irqsave(&device->card->lock, flags);
131 list_add_tail(&client->link, &device->client_list);
132 spin_unlock_irqrestore(&device->card->lock, flags);
133
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500134 return 0;
135}
136
137static void queue_event(struct client *client, struct event *event,
138 void *data0, size_t size0, void *data1, size_t size1)
139{
140 unsigned long flags;
141
142 event->v[0].data = data0;
143 event->v[0].size = size0;
144 event->v[1].data = data1;
145 event->v[1].size = size1;
146
147 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500148 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500149 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400150
151 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500152}
153
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500154static int
155dequeue_event(struct client *client, char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500156{
157 unsigned long flags;
158 struct event *event;
159 size_t size, total;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500160 int i, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500161
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500162 retval = wait_event_interruptible(client->wait,
163 !list_empty(&client->event_list) ||
164 fw_device_is_shutdown(client->device));
165 if (retval < 0)
166 return retval;
167
168 if (list_empty(&client->event_list) &&
169 fw_device_is_shutdown(client->device))
170 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500171
172 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500173 event = container_of(client->event_list.next, struct event, link);
174 list_del(&event->link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500175 spin_unlock_irqrestore(&client->lock, flags);
176
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500177 total = 0;
178 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
179 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500180 if (copy_to_user(buffer + total, event->v[i].data, size)) {
181 retval = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500182 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500183 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500184 total += size;
185 }
186 retval = total;
187
188 out:
189 kfree(event);
190
191 return retval;
192}
193
194static ssize_t
195fw_device_op_read(struct file *file,
196 char __user *buffer, size_t count, loff_t *offset)
197{
198 struct client *client = file->private_data;
199
200 return dequeue_event(client, buffer, count);
201}
202
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500203static void
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500204fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400205 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500206{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400207 struct fw_card *card = client->device->card;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500208
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400209 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500210 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100211 event->generation = client->device->generation;
Stefan Richterb5d2a5e2008-01-25 18:57:41 +0100212 smp_rmb(); /* node_id must not be older than generation */
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400213 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500214 event->local_node_id = card->local_node->node_id;
215 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
216 event->irm_node_id = card->irm_node->node_id;
217 event->root_node_id = card->root_node->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500218}
219
220static void
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500221for_each_client(struct fw_device *device,
222 void (*callback)(struct client *client))
223{
224 struct fw_card *card = device->card;
225 struct client *c;
226 unsigned long flags;
227
228 spin_lock_irqsave(&card->lock, flags);
229
230 list_for_each_entry(c, &device->client_list, link)
231 callback(c);
232
233 spin_unlock_irqrestore(&card->lock, flags);
234}
235
236static void
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500237queue_bus_reset_event(struct client *client)
238{
239 struct bus_reset *bus_reset;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500240
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400241 bus_reset = kzalloc(sizeof(*bus_reset), GFP_ATOMIC);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500242 if (bus_reset == NULL) {
243 fw_notify("Out of memory when allocating bus reset event\n");
244 return;
245 }
246
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400247 fill_bus_reset_event(&bus_reset->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500248
249 queue_event(client, &bus_reset->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400250 &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500251}
252
253void fw_device_cdev_update(struct fw_device *device)
254{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500255 for_each_client(device, queue_bus_reset_event);
256}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500257
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500258static void wake_up_client(struct client *client)
259{
260 wake_up_interruptible(&client->wait);
261}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500262
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500263void fw_device_cdev_remove(struct fw_device *device)
264{
265 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500266}
267
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400268static int ioctl_get_info(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500269{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400270 struct fw_cdev_get_info *get_info = buffer;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500271 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100272 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500273
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400274 client->version = get_info->version;
275 get_info->version = FW_CDEV_VERSION;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500276
Stefan Richterc9755e12008-03-24 20:54:28 +0100277 down_read(&fw_device_rwsem);
278
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400279 if (get_info->rom != 0) {
280 void __user *uptr = u64_to_uptr(get_info->rom);
281 size_t want = get_info->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100282 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500283
Stefan Richterc9755e12008-03-24 20:54:28 +0100284 ret = copy_to_user(uptr, client->device->config_rom,
285 min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500286 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400287 get_info->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500288
Stefan Richterc9755e12008-03-24 20:54:28 +0100289 up_read(&fw_device_rwsem);
290
291 if (ret != 0)
292 return -EFAULT;
293
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400294 client->bus_reset_closure = get_info->bus_reset_closure;
295 if (get_info->bus_reset != 0) {
296 void __user *uptr = u64_to_uptr(get_info->bus_reset);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500297
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400298 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400299 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500300 return -EFAULT;
301 }
302
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400303 get_info->card = client->device->card->index;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500304
305 return 0;
306}
307
308static void
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400309add_client_resource(struct client *client, struct client_resource *resource)
310{
311 unsigned long flags;
312
313 spin_lock_irqsave(&client->lock, flags);
314 list_add_tail(&resource->link, &client->resource_list);
315 resource->handle = client->resource_handle++;
316 spin_unlock_irqrestore(&client->lock, flags);
317}
318
319static int
320release_client_resource(struct client *client, u32 handle,
321 struct client_resource **resource)
322{
323 struct client_resource *r;
324 unsigned long flags;
325
326 spin_lock_irqsave(&client->lock, flags);
327 list_for_each_entry(r, &client->resource_list, link) {
328 if (r->handle == handle) {
329 list_del(&r->link);
330 break;
331 }
332 }
333 spin_unlock_irqrestore(&client->lock, flags);
334
335 if (&r->link == &client->resource_list)
336 return -EINVAL;
337
338 if (resource)
339 *resource = r;
340 else
341 r->release(client, r);
342
343 return 0;
344}
345
346static void
347release_transaction(struct client *client, struct client_resource *resource)
348{
349 struct response *response =
350 container_of(resource, struct response, resource);
351
352 fw_cancel_transaction(client->device->card, &response->transaction);
353}
354
355static void
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500356complete_transaction(struct fw_card *card, int rcode,
357 void *payload, size_t length, void *data)
358{
359 struct response *response = data;
360 struct client *client = response->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500361 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500362
363 if (length < response->response.length)
364 response->response.length = length;
365 if (rcode == RCODE_COMPLETE)
366 memcpy(response->response.data, payload,
367 response->response.length);
368
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500369 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400370 list_del(&response->resource.link);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500371 spin_unlock_irqrestore(&client->lock, flags);
372
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500373 response->response.type = FW_CDEV_EVENT_RESPONSE;
374 response->response.rcode = rcode;
375 queue_event(client, &response->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400376 &response->response, sizeof(response->response),
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500377 response->response.data, response->response.length);
378}
379
Jeff Garzik350958f2007-05-27 07:09:18 -0400380static int ioctl_send_request(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500381{
382 struct fw_device *device = client->device;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400383 struct fw_cdev_send_request *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500384 struct response *response;
385
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500386 /* What is the biggest size we'll accept, really? */
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400387 if (request->length > 4096)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500388 return -EINVAL;
389
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400390 response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500391 if (response == NULL)
392 return -ENOMEM;
393
394 response->client = client;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400395 response->response.length = request->length;
396 response->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500397
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400398 if (request->data &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500399 copy_from_user(response->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400400 u64_to_uptr(request->data), request->length)) {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500401 kfree(response);
402 return -EFAULT;
403 }
404
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400405 response->resource.release = release_transaction;
406 add_client_resource(client, &response->resource);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500407
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500408 fw_send_request(device->card, &response->transaction,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400409 request->tcode & 0x1f,
Stefan Richter907293d2007-01-23 21:11:43 +0100410 device->node->node_id,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400411 request->generation,
Stefan Richterf1397492007-06-10 21:31:36 +0200412 device->max_speed,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400413 request->offset,
414 response->response.data, request->length,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500415 complete_transaction, response);
416
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400417 if (request->data)
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400418 return sizeof(request) + request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500419 else
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400420 return sizeof(request);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500421}
422
423struct address_handler {
424 struct fw_address_handler handler;
425 __u64 closure;
426 struct client *client;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400427 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500428};
429
430struct request {
431 struct fw_request *request;
432 void *data;
433 size_t length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400434 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500435};
436
437struct request_event {
438 struct event event;
439 struct fw_cdev_event_request request;
440};
441
442static void
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400443release_request(struct client *client, struct client_resource *resource)
444{
445 struct request *request =
446 container_of(resource, struct request, resource);
447
448 fw_send_response(client->device->card, request->request,
449 RCODE_CONFLICT_ERROR);
450 kfree(request);
451}
452
453static void
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500454handle_request(struct fw_card *card, struct fw_request *r,
455 int tcode, int destination, int source,
456 int generation, int speed,
457 unsigned long long offset,
458 void *payload, size_t length, void *callback_data)
459{
460 struct address_handler *handler = callback_data;
461 struct request *request;
462 struct request_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500463 struct client *client = handler->client;
464
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400465 request = kmalloc(sizeof(*request), GFP_ATOMIC);
466 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500467 if (request == NULL || e == NULL) {
468 kfree(request);
469 kfree(e);
470 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
471 return;
472 }
473
474 request->request = r;
475 request->data = payload;
476 request->length = length;
477
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400478 request->resource.release = release_request;
479 add_client_resource(client, &request->resource);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500480
481 e->request.type = FW_CDEV_EVENT_REQUEST;
482 e->request.tcode = tcode;
483 e->request.offset = offset;
484 e->request.length = length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400485 e->request.handle = request->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500486 e->request.closure = handler->closure;
487
488 queue_event(client, &e->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400489 &e->request, sizeof(e->request), payload, length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500490}
491
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400492static void
493release_address_handler(struct client *client,
494 struct client_resource *resource)
495{
496 struct address_handler *handler =
497 container_of(resource, struct address_handler, resource);
498
499 fw_core_remove_address_handler(&handler->handler);
500 kfree(handler);
501}
502
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400503static int ioctl_allocate(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500504{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400505 struct fw_cdev_allocate *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500506 struct address_handler *handler;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500507 struct fw_address_region region;
508
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400509 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500510 if (handler == NULL)
511 return -ENOMEM;
512
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400513 region.start = request->offset;
514 region.end = request->offset + request->length;
515 handler->handler.length = request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500516 handler->handler.address_callback = handle_request;
517 handler->handler.callback_data = handler;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400518 handler->closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500519 handler->client = client;
520
521 if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
522 kfree(handler);
523 return -EBUSY;
524 }
525
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400526 handler->resource.release = release_address_handler;
527 add_client_resource(client, &handler->resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400528 request->handle = handler->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500529
530 return 0;
531}
532
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400533static int ioctl_deallocate(struct client *client, void *buffer)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400534{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400535 struct fw_cdev_deallocate *request = buffer;
Kristian Høgsberg94723162007-03-14 17:34:55 -0400536
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400537 return release_client_resource(client, request->handle, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400538}
539
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400540static int ioctl_send_response(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500541{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400542 struct fw_cdev_send_response *request = buffer;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400543 struct client_resource *resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500544 struct request *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500545
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400546 if (release_client_resource(client, request->handle, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500547 return -EINVAL;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400548 r = container_of(resource, struct request, resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400549 if (request->length < r->length)
550 r->length = request->length;
551 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500552 return -EFAULT;
553
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400554 fw_send_response(client->device->card, r->request, request->rcode);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500555 kfree(r);
556
557 return 0;
558}
559
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400560static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500561{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400562 struct fw_cdev_initiate_bus_reset *request = buffer;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500563 int short_reset;
564
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400565 short_reset = (request->type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500566
567 return fw_core_initiate_bus_reset(client->device->card, short_reset);
568}
569
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200570struct descriptor {
571 struct fw_descriptor d;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400572 struct client_resource resource;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200573 u32 data[0];
574};
575
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400576static void release_descriptor(struct client *client,
577 struct client_resource *resource)
578{
579 struct descriptor *descriptor =
580 container_of(resource, struct descriptor, resource);
581
582 fw_core_remove_descriptor(&descriptor->d);
583 kfree(descriptor);
584}
585
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400586static int ioctl_add_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200587{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400588 struct fw_cdev_add_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200589 struct descriptor *descriptor;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200590 int retval;
591
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400592 if (request->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200593 return -EINVAL;
594
595 descriptor =
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400596 kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200597 if (descriptor == NULL)
598 return -ENOMEM;
599
600 if (copy_from_user(descriptor->data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400601 u64_to_uptr(request->data), request->length * 4)) {
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200602 kfree(descriptor);
603 return -EFAULT;
604 }
605
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400606 descriptor->d.length = request->length;
607 descriptor->d.immediate = request->immediate;
608 descriptor->d.key = request->key;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200609 descriptor->d.data = descriptor->data;
610
611 retval = fw_core_add_descriptor(&descriptor->d);
612 if (retval < 0) {
613 kfree(descriptor);
614 return retval;
615 }
616
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400617 descriptor->resource.release = release_descriptor;
618 add_client_resource(client, &descriptor->resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400619 request->handle = descriptor->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200620
621 return 0;
622}
623
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400624static int ioctl_remove_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200625{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400626 struct fw_cdev_remove_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200627
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400628 return release_client_resource(client, request->handle, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200629}
630
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500631static void
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500632iso_callback(struct fw_iso_context *context, u32 cycle,
633 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500634{
635 struct client *client = data;
Stefan Richter930e4b72007-08-03 20:56:31 +0200636 struct iso_interrupt *irq;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500637
Stefan Richter930e4b72007-08-03 20:56:31 +0200638 irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
639 if (irq == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500640 return;
641
Stefan Richter930e4b72007-08-03 20:56:31 +0200642 irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
643 irq->interrupt.closure = client->iso_closure;
644 irq->interrupt.cycle = cycle;
645 irq->interrupt.header_length = header_length;
646 memcpy(irq->interrupt.header, header, header_length);
647 queue_event(client, &irq->event, &irq->interrupt,
648 sizeof(irq->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500649}
650
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400651static int ioctl_create_iso_context(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500652{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400653 struct fw_cdev_create_iso_context *request = buffer;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400654 struct fw_iso_context *context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500655
Stefan Richterfae60312008-02-20 21:10:06 +0100656 /* We only support one context at this time. */
657 if (client->iso_context != NULL)
658 return -EBUSY;
659
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400660 if (request->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500661 return -EINVAL;
662
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400663 switch (request->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400664 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400665 if (request->header_size < 4 || (request->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400666 return -EINVAL;
667
668 break;
669
670 case FW_ISO_CONTEXT_TRANSMIT:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400671 if (request->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400672 return -EINVAL;
673
674 break;
675
676 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500677 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400678 }
679
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400680 context = fw_iso_context_create(client->device->card,
681 request->type,
682 request->channel,
683 request->speed,
684 request->header_size,
685 iso_callback, client);
686 if (IS_ERR(context))
687 return PTR_ERR(context);
688
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400689 client->iso_closure = request->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400690 client->iso_context = context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500691
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400692 /* We only support one context at this time. */
693 request->handle = 0;
694
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500695 return 0;
696}
697
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400698/* Macros for decoding the iso packet control header. */
699#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
700#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
701#define GET_SKIP(v) (((v) >> 17) & 0x01)
702#define GET_TAG(v) (((v) >> 18) & 0x02)
703#define GET_SY(v) (((v) >> 20) & 0x04)
704#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
705
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400706static int ioctl_queue_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500707{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400708 struct fw_cdev_queue_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500709 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500710 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200711 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400712 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500713 int count;
714 struct {
715 struct fw_iso_packet packet;
716 u8 header[256];
717 } u;
718
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400719 if (ctx == NULL || request->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500720 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500721
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400722 /*
723 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500724 * the iso buffer, and the pointer points inside the buffer,
725 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500726 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500727 * payload_length == 0 through. In other words, if no packets
728 * use the indirect payload, the iso buffer need not be mapped
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400729 * and the request->data pointer is ignored.
730 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500731
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400732 payload = (unsigned long)request->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200733 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400734 if (request->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200735 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500736 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200737 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500738 }
739
Al Viro1ccc9142007-10-14 19:34:40 +0100740 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
741
742 if (!access_ok(VERIFY_READ, p, request->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500743 return -EFAULT;
744
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400745 end = (void __user *)p + request->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500746 count = 0;
747 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400748 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500749 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400750 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
751 u.packet.interrupt = GET_INTERRUPT(control);
752 u.packet.skip = GET_SKIP(control);
753 u.packet.tag = GET_TAG(control);
754 u.packet.sy = GET_SY(control);
755 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500756
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500757 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500758 header_length = u.packet.header_length;
759 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400760 /*
761 * We require that header_length is a multiple of
762 * the fixed header size, ctx->header_size.
763 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500764 if (ctx->header_size == 0) {
765 if (u.packet.header_length > 0)
766 return -EINVAL;
767 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500768 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500769 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500770 header_length = 0;
771 }
772
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500773 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500774 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500775 if (next > end)
776 return -EINVAL;
777 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500778 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500779 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500780 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500781 u.packet.header_length + u.packet.payload_length > 0)
782 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200783 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500784 return -EINVAL;
785
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500786 if (fw_iso_context_queue(ctx, &u.packet,
787 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500788 break;
789
790 p = next;
791 payload += u.packet.payload_length;
792 count++;
793 }
794
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400795 request->size -= uptr_to_u64(p) - request->packets;
796 request->packets = uptr_to_u64(p);
797 request->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500798
799 return count;
800}
801
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400802static int ioctl_start_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500803{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400804 struct fw_cdev_start_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500805
Stefan Richterfae60312008-02-20 21:10:06 +0100806 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400807 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +0100808
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400809 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400810 if (request->tags == 0 || request->tags > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400811 return -EINVAL;
812
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400813 if (request->sync > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400814 return -EINVAL;
815 }
816
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400817 return fw_iso_context_start(client->iso_context, request->cycle,
818 request->sync, request->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500819}
820
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400821static int ioctl_stop_iso(struct client *client, void *buffer)
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500822{
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400823 struct fw_cdev_stop_iso *request = buffer;
824
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;
827
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500828 return fw_iso_context_stop(client->iso_context);
829}
830
Stefan Richtera64408b2007-09-29 10:41:58 +0200831static int ioctl_get_cycle_timer(struct client *client, void *buffer)
832{
833 struct fw_cdev_get_cycle_timer *request = buffer;
834 struct fw_card *card = client->device->card;
835 unsigned long long bus_time;
836 struct timeval tv;
837 unsigned long flags;
838
839 preempt_disable();
840 local_irq_save(flags);
841
842 bus_time = card->driver->get_bus_time(card);
843 do_gettimeofday(&tv);
844
845 local_irq_restore(flags);
846 preempt_enable();
847
848 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
849 request->cycle_timer = bus_time & 0xffffffff;
850 return 0;
851}
852
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400853static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
854 ioctl_get_info,
855 ioctl_send_request,
856 ioctl_allocate,
857 ioctl_deallocate,
858 ioctl_send_response,
859 ioctl_initiate_bus_reset,
860 ioctl_add_descriptor,
861 ioctl_remove_descriptor,
862 ioctl_create_iso_context,
863 ioctl_queue_iso,
864 ioctl_start_iso,
865 ioctl_stop_iso,
Stefan Richtera64408b2007-09-29 10:41:58 +0200866 ioctl_get_cycle_timer,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400867};
868
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500869static int
870dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
871{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400872 char buffer[256];
873 int retval;
874
875 if (_IOC_TYPE(cmd) != '#' ||
876 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500877 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400878
879 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400880 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400881 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
882 return -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500883 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400884
885 retval = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
886 if (retval < 0)
887 return retval;
888
889 if (_IOC_DIR(cmd) & _IOC_READ) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400890 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400891 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
892 return -EFAULT;
893 }
894
895 return 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500896}
897
898static long
899fw_device_op_ioctl(struct file *file,
900 unsigned int cmd, unsigned long arg)
901{
902 struct client *client = file->private_data;
903
904 return dispatch_ioctl(client, cmd, (void __user *) arg);
905}
906
907#ifdef CONFIG_COMPAT
908static long
909fw_device_op_compat_ioctl(struct file *file,
910 unsigned int cmd, unsigned long arg)
911{
912 struct client *client = file->private_data;
913
914 return dispatch_ioctl(client, cmd, compat_ptr(arg));
915}
916#endif
917
918static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
919{
920 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500921 enum dma_data_direction direction;
922 unsigned long size;
923 int page_count, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500924
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500925 /* FIXME: We could support multiple buffers, but we don't. */
926 if (client->buffer.pages != NULL)
927 return -EBUSY;
928
929 if (!(vma->vm_flags & VM_SHARED))
930 return -EINVAL;
931
932 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500933 return -EINVAL;
934
935 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500936 size = vma->vm_end - vma->vm_start;
937 page_count = size >> PAGE_SHIFT;
938 if (size & ~PAGE_MASK)
939 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500940
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500941 if (vma->vm_flags & VM_WRITE)
942 direction = DMA_TO_DEVICE;
943 else
944 direction = DMA_FROM_DEVICE;
945
946 retval = fw_iso_buffer_init(&client->buffer, client->device->card,
947 page_count, direction);
948 if (retval < 0)
949 return retval;
950
951 retval = fw_iso_buffer_map(&client->buffer, vma);
952 if (retval < 0)
953 fw_iso_buffer_destroy(&client->buffer, client->device->card);
954
955 return retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500956}
957
958static int fw_device_op_release(struct inode *inode, struct file *file)
959{
960 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500961 struct event *e, *next_e;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400962 struct client_resource *r, *next_r;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500963 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500964
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500965 if (client->buffer.pages)
966 fw_iso_buffer_destroy(&client->buffer, client->device->card);
967
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500968 if (client->iso_context)
969 fw_iso_context_destroy(client->iso_context);
970
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400971 list_for_each_entry_safe(r, next_r, &client->resource_list, link)
972 r->release(client, r);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200973
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400974 /*
975 * FIXME: We should wait for the async tasklets to stop
976 * running before freeing the memory.
977 */
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500978
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500979 list_for_each_entry_safe(e, next_e, &client->event_list, link)
980 kfree(e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500981
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500982 spin_lock_irqsave(&client->device->card->lock, flags);
983 list_del(&client->link);
984 spin_unlock_irqrestore(&client->device->card->lock, flags);
985
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500986 fw_device_put(client->device);
987 kfree(client);
988
989 return 0;
990}
991
992static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
993{
994 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500995 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500996
997 poll_wait(file, &client->wait, pt);
998
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500999 if (fw_device_is_shutdown(client->device))
1000 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001001 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001002 mask |= POLLIN | POLLRDNORM;
1003
1004 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001005}
1006
Stefan Richter21ebcd12007-01-14 15:29:07 +01001007const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001008 .owner = THIS_MODULE,
1009 .open = fw_device_op_open,
1010 .read = fw_device_op_read,
1011 .unlocked_ioctl = fw_device_op_ioctl,
1012 .poll = fw_device_op_poll,
1013 .release = fw_device_op_release,
1014 .mmap = fw_device_op_mmap,
1015
1016#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001017 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001018#endif
1019};