blob: 60f1a8924a953ac14c37068d286ebe2cf45959a5 [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
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500112 device = fw_device_from_devt(inode->i_rdev);
113 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);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500117 if (client == NULL)
118 return -ENOMEM;
119
120 client->device = fw_device_get(device);
121 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400122 INIT_LIST_HEAD(&client->resource_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500123 spin_lock_init(&client->lock);
124 init_waitqueue_head(&client->wait);
125
126 file->private_data = client;
127
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500128 spin_lock_irqsave(&device->card->lock, flags);
129 list_add_tail(&client->link, &device->client_list);
130 spin_unlock_irqrestore(&device->card->lock, flags);
131
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500132 return 0;
133}
134
135static void queue_event(struct client *client, struct event *event,
136 void *data0, size_t size0, void *data1, size_t size1)
137{
138 unsigned long flags;
139
140 event->v[0].data = data0;
141 event->v[0].size = size0;
142 event->v[1].data = data1;
143 event->v[1].size = size1;
144
145 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500146 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500147 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400148
149 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500150}
151
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500152static int
153dequeue_event(struct client *client, char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500154{
155 unsigned long flags;
156 struct event *event;
157 size_t size, total;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500158 int i, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500159
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500160 retval = wait_event_interruptible(client->wait,
161 !list_empty(&client->event_list) ||
162 fw_device_is_shutdown(client->device));
163 if (retval < 0)
164 return retval;
165
166 if (list_empty(&client->event_list) &&
167 fw_device_is_shutdown(client->device))
168 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500169
170 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500171 event = container_of(client->event_list.next, struct event, link);
172 list_del(&event->link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500173 spin_unlock_irqrestore(&client->lock, flags);
174
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500175 total = 0;
176 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
177 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500178 if (copy_to_user(buffer + total, event->v[i].data, size)) {
179 retval = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500180 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500181 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500182 total += size;
183 }
184 retval = total;
185
186 out:
187 kfree(event);
188
189 return retval;
190}
191
192static ssize_t
193fw_device_op_read(struct file *file,
194 char __user *buffer, size_t count, loff_t *offset)
195{
196 struct client *client = file->private_data;
197
198 return dequeue_event(client, buffer, count);
199}
200
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500201static void
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500202fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400203 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500204{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400205 struct fw_card *card = client->device->card;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500206
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400207 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500208 event->type = FW_CDEV_EVENT_BUS_RESET;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400209 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500210 event->local_node_id = card->local_node->node_id;
211 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
212 event->irm_node_id = card->irm_node->node_id;
213 event->root_node_id = card->root_node->node_id;
214 event->generation = card->generation;
215}
216
217static void
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500218for_each_client(struct fw_device *device,
219 void (*callback)(struct client *client))
220{
221 struct fw_card *card = device->card;
222 struct client *c;
223 unsigned long flags;
224
225 spin_lock_irqsave(&card->lock, flags);
226
227 list_for_each_entry(c, &device->client_list, link)
228 callback(c);
229
230 spin_unlock_irqrestore(&card->lock, flags);
231}
232
233static void
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500234queue_bus_reset_event(struct client *client)
235{
236 struct bus_reset *bus_reset;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500237
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400238 bus_reset = kzalloc(sizeof(*bus_reset), GFP_ATOMIC);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500239 if (bus_reset == NULL) {
240 fw_notify("Out of memory when allocating bus reset event\n");
241 return;
242 }
243
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400244 fill_bus_reset_event(&bus_reset->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500245
246 queue_event(client, &bus_reset->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400247 &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500248}
249
250void fw_device_cdev_update(struct fw_device *device)
251{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500252 for_each_client(device, queue_bus_reset_event);
253}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500254
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500255static void wake_up_client(struct client *client)
256{
257 wake_up_interruptible(&client->wait);
258}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500259
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500260void fw_device_cdev_remove(struct fw_device *device)
261{
262 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500263}
264
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400265static int ioctl_get_info(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500266{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400267 struct fw_cdev_get_info *get_info = buffer;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500268 struct fw_cdev_event_bus_reset bus_reset;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500269
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400270 client->version = get_info->version;
271 get_info->version = FW_CDEV_VERSION;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500272
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400273 if (get_info->rom != 0) {
274 void __user *uptr = u64_to_uptr(get_info->rom);
275 size_t want = get_info->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100276 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500277
Stefan Richterd84702a2007-03-20 19:42:15 +0100278 if (copy_to_user(uptr, client->device->config_rom,
279 min(want, have)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500280 return -EFAULT;
281 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400282 get_info->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500283
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400284 client->bus_reset_closure = get_info->bus_reset_closure;
285 if (get_info->bus_reset != 0) {
286 void __user *uptr = u64_to_uptr(get_info->bus_reset);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500287
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400288 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400289 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500290 return -EFAULT;
291 }
292
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400293 get_info->card = client->device->card->index;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500294
295 return 0;
296}
297
298static void
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400299add_client_resource(struct client *client, struct client_resource *resource)
300{
301 unsigned long flags;
302
303 spin_lock_irqsave(&client->lock, flags);
304 list_add_tail(&resource->link, &client->resource_list);
305 resource->handle = client->resource_handle++;
306 spin_unlock_irqrestore(&client->lock, flags);
307}
308
309static int
310release_client_resource(struct client *client, u32 handle,
311 struct client_resource **resource)
312{
313 struct client_resource *r;
314 unsigned long flags;
315
316 spin_lock_irqsave(&client->lock, flags);
317 list_for_each_entry(r, &client->resource_list, link) {
318 if (r->handle == handle) {
319 list_del(&r->link);
320 break;
321 }
322 }
323 spin_unlock_irqrestore(&client->lock, flags);
324
325 if (&r->link == &client->resource_list)
326 return -EINVAL;
327
328 if (resource)
329 *resource = r;
330 else
331 r->release(client, r);
332
333 return 0;
334}
335
336static void
337release_transaction(struct client *client, struct client_resource *resource)
338{
339 struct response *response =
340 container_of(resource, struct response, resource);
341
342 fw_cancel_transaction(client->device->card, &response->transaction);
343}
344
345static void
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500346complete_transaction(struct fw_card *card, int rcode,
347 void *payload, size_t length, void *data)
348{
349 struct response *response = data;
350 struct client *client = response->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500351 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500352
353 if (length < response->response.length)
354 response->response.length = length;
355 if (rcode == RCODE_COMPLETE)
356 memcpy(response->response.data, payload,
357 response->response.length);
358
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500359 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400360 list_del(&response->resource.link);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500361 spin_unlock_irqrestore(&client->lock, flags);
362
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500363 response->response.type = FW_CDEV_EVENT_RESPONSE;
364 response->response.rcode = rcode;
365 queue_event(client, &response->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400366 &response->response, sizeof(response->response),
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500367 response->response.data, response->response.length);
368}
369
Jeff Garzik350958f2007-05-27 07:09:18 -0400370static int ioctl_send_request(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500371{
372 struct fw_device *device = client->device;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400373 struct fw_cdev_send_request *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500374 struct response *response;
375
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500376 /* What is the biggest size we'll accept, really? */
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400377 if (request->length > 4096)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500378 return -EINVAL;
379
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400380 response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500381 if (response == NULL)
382 return -ENOMEM;
383
384 response->client = client;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400385 response->response.length = request->length;
386 response->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500387
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400388 if (request->data &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500389 copy_from_user(response->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400390 u64_to_uptr(request->data), request->length)) {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500391 kfree(response);
392 return -EFAULT;
393 }
394
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400395 response->resource.release = release_transaction;
396 add_client_resource(client, &response->resource);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500397
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500398 fw_send_request(device->card, &response->transaction,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400399 request->tcode & 0x1f,
Stefan Richter907293d2007-01-23 21:11:43 +0100400 device->node->node_id,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400401 request->generation,
Stefan Richterf1397492007-06-10 21:31:36 +0200402 device->max_speed,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400403 request->offset,
404 response->response.data, request->length,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500405 complete_transaction, response);
406
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400407 if (request->data)
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400408 return sizeof(request) + request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500409 else
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400410 return sizeof(request);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500411}
412
413struct address_handler {
414 struct fw_address_handler handler;
415 __u64 closure;
416 struct client *client;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400417 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500418};
419
420struct request {
421 struct fw_request *request;
422 void *data;
423 size_t length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400424 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500425};
426
427struct request_event {
428 struct event event;
429 struct fw_cdev_event_request request;
430};
431
432static void
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400433release_request(struct client *client, struct client_resource *resource)
434{
435 struct request *request =
436 container_of(resource, struct request, resource);
437
438 fw_send_response(client->device->card, request->request,
439 RCODE_CONFLICT_ERROR);
440 kfree(request);
441}
442
443static void
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500444handle_request(struct fw_card *card, struct fw_request *r,
445 int tcode, int destination, int source,
446 int generation, int speed,
447 unsigned long long offset,
448 void *payload, size_t length, void *callback_data)
449{
450 struct address_handler *handler = callback_data;
451 struct request *request;
452 struct request_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500453 struct client *client = handler->client;
454
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400455 request = kmalloc(sizeof(*request), GFP_ATOMIC);
456 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500457 if (request == NULL || e == NULL) {
458 kfree(request);
459 kfree(e);
460 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
461 return;
462 }
463
464 request->request = r;
465 request->data = payload;
466 request->length = length;
467
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400468 request->resource.release = release_request;
469 add_client_resource(client, &request->resource);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500470
471 e->request.type = FW_CDEV_EVENT_REQUEST;
472 e->request.tcode = tcode;
473 e->request.offset = offset;
474 e->request.length = length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400475 e->request.handle = request->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500476 e->request.closure = handler->closure;
477
478 queue_event(client, &e->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400479 &e->request, sizeof(e->request), payload, length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500480}
481
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400482static void
483release_address_handler(struct client *client,
484 struct client_resource *resource)
485{
486 struct address_handler *handler =
487 container_of(resource, struct address_handler, resource);
488
489 fw_core_remove_address_handler(&handler->handler);
490 kfree(handler);
491}
492
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400493static int ioctl_allocate(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500494{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400495 struct fw_cdev_allocate *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500496 struct address_handler *handler;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500497 struct fw_address_region region;
498
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400499 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500500 if (handler == NULL)
501 return -ENOMEM;
502
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400503 region.start = request->offset;
504 region.end = request->offset + request->length;
505 handler->handler.length = request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500506 handler->handler.address_callback = handle_request;
507 handler->handler.callback_data = handler;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400508 handler->closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500509 handler->client = client;
510
511 if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
512 kfree(handler);
513 return -EBUSY;
514 }
515
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400516 handler->resource.release = release_address_handler;
517 add_client_resource(client, &handler->resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400518 request->handle = handler->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500519
520 return 0;
521}
522
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400523static int ioctl_deallocate(struct client *client, void *buffer)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400524{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400525 struct fw_cdev_deallocate *request = buffer;
Kristian Høgsberg94723162007-03-14 17:34:55 -0400526
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400527 return release_client_resource(client, request->handle, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400528}
529
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400530static int ioctl_send_response(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500531{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400532 struct fw_cdev_send_response *request = buffer;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400533 struct client_resource *resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500534 struct request *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500535
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400536 if (release_client_resource(client, request->handle, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500537 return -EINVAL;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400538 r = container_of(resource, struct request, resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400539 if (request->length < r->length)
540 r->length = request->length;
541 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500542 return -EFAULT;
543
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400544 fw_send_response(client->device->card, r->request, request->rcode);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500545 kfree(r);
546
547 return 0;
548}
549
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400550static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500551{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400552 struct fw_cdev_initiate_bus_reset *request = buffer;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500553 int short_reset;
554
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400555 short_reset = (request->type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500556
557 return fw_core_initiate_bus_reset(client->device->card, short_reset);
558}
559
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200560struct descriptor {
561 struct fw_descriptor d;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400562 struct client_resource resource;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200563 u32 data[0];
564};
565
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400566static void release_descriptor(struct client *client,
567 struct client_resource *resource)
568{
569 struct descriptor *descriptor =
570 container_of(resource, struct descriptor, resource);
571
572 fw_core_remove_descriptor(&descriptor->d);
573 kfree(descriptor);
574}
575
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400576static int ioctl_add_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200577{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400578 struct fw_cdev_add_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200579 struct descriptor *descriptor;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200580 int retval;
581
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400582 if (request->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200583 return -EINVAL;
584
585 descriptor =
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400586 kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200587 if (descriptor == NULL)
588 return -ENOMEM;
589
590 if (copy_from_user(descriptor->data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400591 u64_to_uptr(request->data), request->length * 4)) {
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200592 kfree(descriptor);
593 return -EFAULT;
594 }
595
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400596 descriptor->d.length = request->length;
597 descriptor->d.immediate = request->immediate;
598 descriptor->d.key = request->key;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200599 descriptor->d.data = descriptor->data;
600
601 retval = fw_core_add_descriptor(&descriptor->d);
602 if (retval < 0) {
603 kfree(descriptor);
604 return retval;
605 }
606
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400607 descriptor->resource.release = release_descriptor;
608 add_client_resource(client, &descriptor->resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400609 request->handle = descriptor->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200610
611 return 0;
612}
613
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400614static int ioctl_remove_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200615{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400616 struct fw_cdev_remove_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200617
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400618 return release_client_resource(client, request->handle, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200619}
620
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500621static void
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500622iso_callback(struct fw_iso_context *context, u32 cycle,
623 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500624{
625 struct client *client = data;
Stefan Richter930e4b72007-08-03 20:56:31 +0200626 struct iso_interrupt *irq;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500627
Stefan Richter930e4b72007-08-03 20:56:31 +0200628 irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
629 if (irq == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500630 return;
631
Stefan Richter930e4b72007-08-03 20:56:31 +0200632 irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
633 irq->interrupt.closure = client->iso_closure;
634 irq->interrupt.cycle = cycle;
635 irq->interrupt.header_length = header_length;
636 memcpy(irq->interrupt.header, header, header_length);
637 queue_event(client, &irq->event, &irq->interrupt,
638 sizeof(irq->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500639}
640
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400641static int ioctl_create_iso_context(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500642{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400643 struct fw_cdev_create_iso_context *request = buffer;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400644 struct fw_iso_context *context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500645
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400646 if (request->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500647 return -EINVAL;
648
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400649 switch (request->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400650 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400651 if (request->header_size < 4 || (request->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400652 return -EINVAL;
653
654 break;
655
656 case FW_ISO_CONTEXT_TRANSMIT:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400657 if (request->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400658 return -EINVAL;
659
660 break;
661
662 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500663 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400664 }
665
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400666 context = fw_iso_context_create(client->device->card,
667 request->type,
668 request->channel,
669 request->speed,
670 request->header_size,
671 iso_callback, client);
672 if (IS_ERR(context))
673 return PTR_ERR(context);
674
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400675 client->iso_closure = request->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400676 client->iso_context = context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500677
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400678 /* We only support one context at this time. */
679 request->handle = 0;
680
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500681 return 0;
682}
683
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400684/* Macros for decoding the iso packet control header. */
685#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
686#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
687#define GET_SKIP(v) (((v) >> 17) & 0x01)
688#define GET_TAG(v) (((v) >> 18) & 0x02)
689#define GET_SY(v) (((v) >> 20) & 0x04)
690#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
691
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400692static int ioctl_queue_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500693{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400694 struct fw_cdev_queue_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500695 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500696 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200697 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400698 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500699 int count;
700 struct {
701 struct fw_iso_packet packet;
702 u8 header[256];
703 } u;
704
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400705 if (ctx == NULL || request->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500706 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500707
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400708 /*
709 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500710 * the iso buffer, and the pointer points inside the buffer,
711 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500712 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500713 * payload_length == 0 through. In other words, if no packets
714 * use the indirect payload, the iso buffer need not be mapped
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400715 * and the request->data pointer is ignored.
716 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500717
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400718 payload = (unsigned long)request->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200719 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400720 if (request->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200721 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500722 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200723 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500724 }
725
Al Viro1ccc9142007-10-14 19:34:40 +0100726 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
727
728 if (!access_ok(VERIFY_READ, p, request->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500729 return -EFAULT;
730
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400731 end = (void __user *)p + request->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500732 count = 0;
733 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400734 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500735 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400736 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
737 u.packet.interrupt = GET_INTERRUPT(control);
738 u.packet.skip = GET_SKIP(control);
739 u.packet.tag = GET_TAG(control);
740 u.packet.sy = GET_SY(control);
741 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500742
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500743 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500744 header_length = u.packet.header_length;
745 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400746 /*
747 * We require that header_length is a multiple of
748 * the fixed header size, ctx->header_size.
749 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500750 if (ctx->header_size == 0) {
751 if (u.packet.header_length > 0)
752 return -EINVAL;
753 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500754 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500755 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500756 header_length = 0;
757 }
758
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500759 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500760 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500761 if (next > end)
762 return -EINVAL;
763 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500764 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500765 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500766 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500767 u.packet.header_length + u.packet.payload_length > 0)
768 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200769 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500770 return -EINVAL;
771
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500772 if (fw_iso_context_queue(ctx, &u.packet,
773 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500774 break;
775
776 p = next;
777 payload += u.packet.payload_length;
778 count++;
779 }
780
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400781 request->size -= uptr_to_u64(p) - request->packets;
782 request->packets = uptr_to_u64(p);
783 request->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500784
785 return count;
786}
787
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400788static int ioctl_start_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500789{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400790 struct fw_cdev_start_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500791
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400792 if (request->handle != 0)
793 return -EINVAL;
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400794 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400795 if (request->tags == 0 || request->tags > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400796 return -EINVAL;
797
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400798 if (request->sync > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400799 return -EINVAL;
800 }
801
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400802 return fw_iso_context_start(client->iso_context, request->cycle,
803 request->sync, request->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500804}
805
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400806static int ioctl_stop_iso(struct client *client, void *buffer)
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500807{
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400808 struct fw_cdev_stop_iso *request = buffer;
809
810 if (request->handle != 0)
811 return -EINVAL;
812
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500813 return fw_iso_context_stop(client->iso_context);
814}
815
Stefan Richtera64408b2007-09-29 10:41:58 +0200816static int ioctl_get_cycle_timer(struct client *client, void *buffer)
817{
818 struct fw_cdev_get_cycle_timer *request = buffer;
819 struct fw_card *card = client->device->card;
820 unsigned long long bus_time;
821 struct timeval tv;
822 unsigned long flags;
823
824 preempt_disable();
825 local_irq_save(flags);
826
827 bus_time = card->driver->get_bus_time(card);
828 do_gettimeofday(&tv);
829
830 local_irq_restore(flags);
831 preempt_enable();
832
833 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
834 request->cycle_timer = bus_time & 0xffffffff;
835 return 0;
836}
837
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400838static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
839 ioctl_get_info,
840 ioctl_send_request,
841 ioctl_allocate,
842 ioctl_deallocate,
843 ioctl_send_response,
844 ioctl_initiate_bus_reset,
845 ioctl_add_descriptor,
846 ioctl_remove_descriptor,
847 ioctl_create_iso_context,
848 ioctl_queue_iso,
849 ioctl_start_iso,
850 ioctl_stop_iso,
Stefan Richtera64408b2007-09-29 10:41:58 +0200851 ioctl_get_cycle_timer,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400852};
853
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500854static int
855dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
856{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400857 char buffer[256];
858 int retval;
859
860 if (_IOC_TYPE(cmd) != '#' ||
861 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500862 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400863
864 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400865 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400866 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
867 return -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500868 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400869
870 retval = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
871 if (retval < 0)
872 return retval;
873
874 if (_IOC_DIR(cmd) & _IOC_READ) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400875 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400876 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
877 return -EFAULT;
878 }
879
880 return 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500881}
882
883static long
884fw_device_op_ioctl(struct file *file,
885 unsigned int cmd, unsigned long arg)
886{
887 struct client *client = file->private_data;
888
889 return dispatch_ioctl(client, cmd, (void __user *) arg);
890}
891
892#ifdef CONFIG_COMPAT
893static long
894fw_device_op_compat_ioctl(struct file *file,
895 unsigned int cmd, unsigned long arg)
896{
897 struct client *client = file->private_data;
898
899 return dispatch_ioctl(client, cmd, compat_ptr(arg));
900}
901#endif
902
903static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
904{
905 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500906 enum dma_data_direction direction;
907 unsigned long size;
908 int page_count, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500909
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500910 /* FIXME: We could support multiple buffers, but we don't. */
911 if (client->buffer.pages != NULL)
912 return -EBUSY;
913
914 if (!(vma->vm_flags & VM_SHARED))
915 return -EINVAL;
916
917 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500918 return -EINVAL;
919
920 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500921 size = vma->vm_end - vma->vm_start;
922 page_count = size >> PAGE_SHIFT;
923 if (size & ~PAGE_MASK)
924 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500925
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500926 if (vma->vm_flags & VM_WRITE)
927 direction = DMA_TO_DEVICE;
928 else
929 direction = DMA_FROM_DEVICE;
930
931 retval = fw_iso_buffer_init(&client->buffer, client->device->card,
932 page_count, direction);
933 if (retval < 0)
934 return retval;
935
936 retval = fw_iso_buffer_map(&client->buffer, vma);
937 if (retval < 0)
938 fw_iso_buffer_destroy(&client->buffer, client->device->card);
939
940 return retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500941}
942
943static int fw_device_op_release(struct inode *inode, struct file *file)
944{
945 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500946 struct event *e, *next_e;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400947 struct client_resource *r, *next_r;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500948 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500949
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500950 if (client->buffer.pages)
951 fw_iso_buffer_destroy(&client->buffer, client->device->card);
952
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500953 if (client->iso_context)
954 fw_iso_context_destroy(client->iso_context);
955
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400956 list_for_each_entry_safe(r, next_r, &client->resource_list, link)
957 r->release(client, r);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200958
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400959 /*
960 * FIXME: We should wait for the async tasklets to stop
961 * running before freeing the memory.
962 */
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500963
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500964 list_for_each_entry_safe(e, next_e, &client->event_list, link)
965 kfree(e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500966
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500967 spin_lock_irqsave(&client->device->card->lock, flags);
968 list_del(&client->link);
969 spin_unlock_irqrestore(&client->device->card->lock, flags);
970
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500971 fw_device_put(client->device);
972 kfree(client);
973
974 return 0;
975}
976
977static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
978{
979 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500980 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500981
982 poll_wait(file, &client->wait, pt);
983
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500984 if (fw_device_is_shutdown(client->device))
985 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500986 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500987 mask |= POLLIN | POLLRDNORM;
988
989 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500990}
991
Stefan Richter21ebcd12007-01-14 15:29:07 +0100992const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500993 .owner = THIS_MODULE,
994 .open = fw_device_op_open,
995 .read = fw_device_op_read,
996 .unlocked_ioctl = fw_device_op_ioctl,
997 .poll = fw_device_op_poll,
998 .release = fw_device_op_release,
999 .mmap = fw_device_op_mmap,
1000
1001#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001002 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001003#endif
1004};