blob: 3ab3585d36012608b8fa180b647033c74bc5ccc1 [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>
28#include <linux/delay.h>
29#include <linux/mm.h>
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -050030#include <linux/idr.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050031#include <linux/compat.h>
Kristian Høgsberg9640d3d2007-04-30 15:03:15 -040032#include <linux/firewire-cdev.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050033#include <asm/uaccess.h>
34#include "fw-transaction.h"
35#include "fw-topology.h"
36#include "fw-device.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050037
Kristian Høgsberg3964a442007-03-27 01:43:41 -040038struct client;
39struct client_resource {
40 struct list_head link;
41 void (*release)(struct client *client, struct client_resource *r);
42 u32 handle;
43};
44
Kristian Høgsbergc781c062007-05-07 20:33:32 -040045/*
46 * dequeue_event() just kfree()'s the event, so the event has to be
47 * the first field in the struct.
48 */
49
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050050struct event {
51 struct { void *data; size_t size; } v[2];
52 struct list_head link;
53};
54
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050055struct bus_reset {
56 struct event event;
57 struct fw_cdev_event_bus_reset reset;
58};
59
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050060struct response {
61 struct event event;
62 struct fw_transaction transaction;
63 struct client *client;
Kristian Høgsberg3964a442007-03-27 01:43:41 -040064 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050065 struct fw_cdev_event_response response;
66};
67
68struct iso_interrupt {
69 struct event event;
70 struct fw_cdev_event_iso_interrupt interrupt;
71};
72
73struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050074 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050075 struct fw_device *device;
76 spinlock_t lock;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +020077 u32 resource_handle;
Kristian Høgsberg3964a442007-03-27 01:43:41 -040078 struct list_head resource_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050079 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050080 wait_queue_head_t wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040081 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050082
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050083 struct fw_iso_context *iso_context;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -040084 u64 iso_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050085 struct fw_iso_buffer buffer;
86 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050087
88 struct list_head link;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050089};
90
91static inline void __user *
92u64_to_uptr(__u64 value)
93{
94 return (void __user *)(unsigned long)value;
95}
96
97static inline __u64
98uptr_to_u64(void __user *ptr)
99{
100 return (__u64)(unsigned long)ptr;
101}
102
103static int fw_device_op_open(struct inode *inode, struct file *file)
104{
105 struct fw_device *device;
106 struct client *client;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500107 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500108
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500109 device = fw_device_from_devt(inode->i_rdev);
110 if (device == NULL)
111 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500112
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400113 client = kzalloc(sizeof(*client), GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500114 if (client == NULL)
115 return -ENOMEM;
116
117 client->device = fw_device_get(device);
118 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400119 INIT_LIST_HEAD(&client->resource_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500120 spin_lock_init(&client->lock);
121 init_waitqueue_head(&client->wait);
122
123 file->private_data = client;
124
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500125 spin_lock_irqsave(&device->card->lock, flags);
126 list_add_tail(&client->link, &device->client_list);
127 spin_unlock_irqrestore(&device->card->lock, flags);
128
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500129 return 0;
130}
131
132static void queue_event(struct client *client, struct event *event,
133 void *data0, size_t size0, void *data1, size_t size1)
134{
135 unsigned long flags;
136
137 event->v[0].data = data0;
138 event->v[0].size = size0;
139 event->v[1].data = data1;
140 event->v[1].size = size1;
141
142 spin_lock_irqsave(&client->lock, flags);
143
144 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500145 wake_up_interruptible(&client->wait);
146
147 spin_unlock_irqrestore(&client->lock, flags);
148}
149
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500150static int
151dequeue_event(struct client *client, char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500152{
153 unsigned long flags;
154 struct event *event;
155 size_t size, total;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500156 int i, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500157
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500158 retval = wait_event_interruptible(client->wait,
159 !list_empty(&client->event_list) ||
160 fw_device_is_shutdown(client->device));
161 if (retval < 0)
162 return retval;
163
164 if (list_empty(&client->event_list) &&
165 fw_device_is_shutdown(client->device))
166 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500167
168 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500169 event = container_of(client->event_list.next, struct event, link);
170 list_del(&event->link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500171 spin_unlock_irqrestore(&client->lock, flags);
172
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500173 total = 0;
174 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
175 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500176 if (copy_to_user(buffer + total, event->v[i].data, size)) {
177 retval = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500178 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500179 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500180 total += size;
181 }
182 retval = total;
183
184 out:
185 kfree(event);
186
187 return retval;
188}
189
190static ssize_t
191fw_device_op_read(struct file *file,
192 char __user *buffer, size_t count, loff_t *offset)
193{
194 struct client *client = file->private_data;
195
196 return dequeue_event(client, buffer, count);
197}
198
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500199static void
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500200fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400201 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500202{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400203 struct fw_card *card = client->device->card;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500204
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400205 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500206 event->type = FW_CDEV_EVENT_BUS_RESET;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400207 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500208 event->local_node_id = card->local_node->node_id;
209 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
210 event->irm_node_id = card->irm_node->node_id;
211 event->root_node_id = card->root_node->node_id;
212 event->generation = card->generation;
213}
214
215static void
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500216for_each_client(struct fw_device *device,
217 void (*callback)(struct client *client))
218{
219 struct fw_card *card = device->card;
220 struct client *c;
221 unsigned long flags;
222
223 spin_lock_irqsave(&card->lock, flags);
224
225 list_for_each_entry(c, &device->client_list, link)
226 callback(c);
227
228 spin_unlock_irqrestore(&card->lock, flags);
229}
230
231static void
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500232queue_bus_reset_event(struct client *client)
233{
234 struct bus_reset *bus_reset;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500235
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400236 bus_reset = kzalloc(sizeof(*bus_reset), GFP_ATOMIC);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500237 if (bus_reset == NULL) {
238 fw_notify("Out of memory when allocating bus reset event\n");
239 return;
240 }
241
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400242 fill_bus_reset_event(&bus_reset->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500243
244 queue_event(client, &bus_reset->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400245 &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500246}
247
248void fw_device_cdev_update(struct fw_device *device)
249{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500250 for_each_client(device, queue_bus_reset_event);
251}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500252
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500253static void wake_up_client(struct client *client)
254{
255 wake_up_interruptible(&client->wait);
256}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500257
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500258void fw_device_cdev_remove(struct fw_device *device)
259{
260 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500261}
262
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400263static int ioctl_get_info(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500264{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400265 struct fw_cdev_get_info *get_info = buffer;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500266 struct fw_cdev_event_bus_reset bus_reset;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500267
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400268 client->version = get_info->version;
269 get_info->version = FW_CDEV_VERSION;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500270
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400271 if (get_info->rom != 0) {
272 void __user *uptr = u64_to_uptr(get_info->rom);
273 size_t want = get_info->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100274 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500275
Stefan Richterd84702a2007-03-20 19:42:15 +0100276 if (copy_to_user(uptr, client->device->config_rom,
277 min(want, have)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500278 return -EFAULT;
279 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400280 get_info->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500281
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400282 client->bus_reset_closure = get_info->bus_reset_closure;
283 if (get_info->bus_reset != 0) {
284 void __user *uptr = u64_to_uptr(get_info->bus_reset);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500285
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400286 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400287 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500288 return -EFAULT;
289 }
290
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400291 get_info->card = client->device->card->index;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500292
293 return 0;
294}
295
296static void
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400297add_client_resource(struct client *client, struct client_resource *resource)
298{
299 unsigned long flags;
300
301 spin_lock_irqsave(&client->lock, flags);
302 list_add_tail(&resource->link, &client->resource_list);
303 resource->handle = client->resource_handle++;
304 spin_unlock_irqrestore(&client->lock, flags);
305}
306
307static int
308release_client_resource(struct client *client, u32 handle,
309 struct client_resource **resource)
310{
311 struct client_resource *r;
312 unsigned long flags;
313
314 spin_lock_irqsave(&client->lock, flags);
315 list_for_each_entry(r, &client->resource_list, link) {
316 if (r->handle == handle) {
317 list_del(&r->link);
318 break;
319 }
320 }
321 spin_unlock_irqrestore(&client->lock, flags);
322
323 if (&r->link == &client->resource_list)
324 return -EINVAL;
325
326 if (resource)
327 *resource = r;
328 else
329 r->release(client, r);
330
331 return 0;
332}
333
334static void
335release_transaction(struct client *client, struct client_resource *resource)
336{
337 struct response *response =
338 container_of(resource, struct response, resource);
339
340 fw_cancel_transaction(client->device->card, &response->transaction);
341}
342
343static void
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500344complete_transaction(struct fw_card *card, int rcode,
345 void *payload, size_t length, void *data)
346{
347 struct response *response = data;
348 struct client *client = response->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500349 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500350
351 if (length < response->response.length)
352 response->response.length = length;
353 if (rcode == RCODE_COMPLETE)
354 memcpy(response->response.data, payload,
355 response->response.length);
356
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500357 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400358 list_del(&response->resource.link);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500359 spin_unlock_irqrestore(&client->lock, flags);
360
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500361 response->response.type = FW_CDEV_EVENT_RESPONSE;
362 response->response.rcode = rcode;
363 queue_event(client, &response->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400364 &response->response, sizeof(response->response),
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500365 response->response.data, response->response.length);
366}
367
Jeff Garzik350958f2007-05-27 07:09:18 -0400368static int ioctl_send_request(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500369{
370 struct fw_device *device = client->device;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400371 struct fw_cdev_send_request *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500372 struct response *response;
373
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500374 /* What is the biggest size we'll accept, really? */
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400375 if (request->length > 4096)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500376 return -EINVAL;
377
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400378 response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500379 if (response == NULL)
380 return -ENOMEM;
381
382 response->client = client;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400383 response->response.length = request->length;
384 response->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500385
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400386 if (request->data &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500387 copy_from_user(response->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400388 u64_to_uptr(request->data), request->length)) {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500389 kfree(response);
390 return -EFAULT;
391 }
392
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400393 response->resource.release = release_transaction;
394 add_client_resource(client, &response->resource);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500395
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500396 fw_send_request(device->card, &response->transaction,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400397 request->tcode & 0x1f,
Stefan Richter907293d2007-01-23 21:11:43 +0100398 device->node->node_id,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400399 request->generation,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500400 device->node->max_speed,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400401 request->offset,
402 response->response.data, request->length,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500403 complete_transaction, response);
404
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400405 if (request->data)
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400406 return sizeof(request) + request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500407 else
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400408 return sizeof(request);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500409}
410
411struct address_handler {
412 struct fw_address_handler handler;
413 __u64 closure;
414 struct client *client;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400415 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500416};
417
418struct request {
419 struct fw_request *request;
420 void *data;
421 size_t length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400422 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500423};
424
425struct request_event {
426 struct event event;
427 struct fw_cdev_event_request request;
428};
429
430static void
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400431release_request(struct client *client, struct client_resource *resource)
432{
433 struct request *request =
434 container_of(resource, struct request, resource);
435
436 fw_send_response(client->device->card, request->request,
437 RCODE_CONFLICT_ERROR);
438 kfree(request);
439}
440
441static void
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500442handle_request(struct fw_card *card, struct fw_request *r,
443 int tcode, int destination, int source,
444 int generation, int speed,
445 unsigned long long offset,
446 void *payload, size_t length, void *callback_data)
447{
448 struct address_handler *handler = callback_data;
449 struct request *request;
450 struct request_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500451 struct client *client = handler->client;
452
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400453 request = kmalloc(sizeof(*request), GFP_ATOMIC);
454 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500455 if (request == NULL || e == NULL) {
456 kfree(request);
457 kfree(e);
458 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
459 return;
460 }
461
462 request->request = r;
463 request->data = payload;
464 request->length = length;
465
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400466 request->resource.release = release_request;
467 add_client_resource(client, &request->resource);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500468
469 e->request.type = FW_CDEV_EVENT_REQUEST;
470 e->request.tcode = tcode;
471 e->request.offset = offset;
472 e->request.length = length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400473 e->request.handle = request->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500474 e->request.closure = handler->closure;
475
476 queue_event(client, &e->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400477 &e->request, sizeof(e->request), payload, length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500478}
479
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400480static void
481release_address_handler(struct client *client,
482 struct client_resource *resource)
483{
484 struct address_handler *handler =
485 container_of(resource, struct address_handler, resource);
486
487 fw_core_remove_address_handler(&handler->handler);
488 kfree(handler);
489}
490
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400491static int ioctl_allocate(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500492{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400493 struct fw_cdev_allocate *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500494 struct address_handler *handler;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500495 struct fw_address_region region;
496
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400497 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500498 if (handler == NULL)
499 return -ENOMEM;
500
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400501 region.start = request->offset;
502 region.end = request->offset + request->length;
503 handler->handler.length = request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500504 handler->handler.address_callback = handle_request;
505 handler->handler.callback_data = handler;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400506 handler->closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500507 handler->client = client;
508
509 if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
510 kfree(handler);
511 return -EBUSY;
512 }
513
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400514 handler->resource.release = release_address_handler;
515 add_client_resource(client, &handler->resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400516 request->handle = handler->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500517
518 return 0;
519}
520
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400521static int ioctl_deallocate(struct client *client, void *buffer)
Kristian Høgsberg9472316b2007-03-14 17:34:55 -0400522{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400523 struct fw_cdev_deallocate *request = buffer;
Kristian Høgsberg9472316b2007-03-14 17:34:55 -0400524
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400525 return release_client_resource(client, request->handle, NULL);
Kristian Høgsberg9472316b2007-03-14 17:34:55 -0400526}
527
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400528static int ioctl_send_response(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500529{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400530 struct fw_cdev_send_response *request = buffer;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400531 struct client_resource *resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500532 struct request *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500533
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400534 if (release_client_resource(client, request->handle, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500535 return -EINVAL;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400536 r = container_of(resource, struct request, resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400537 if (request->length < r->length)
538 r->length = request->length;
539 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500540 return -EFAULT;
541
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400542 fw_send_response(client->device->card, r->request, request->rcode);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500543 kfree(r);
544
545 return 0;
546}
547
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400548static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500549{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400550 struct fw_cdev_initiate_bus_reset *request = buffer;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500551 int short_reset;
552
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400553 short_reset = (request->type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500554
555 return fw_core_initiate_bus_reset(client->device->card, short_reset);
556}
557
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200558struct descriptor {
559 struct fw_descriptor d;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400560 struct client_resource resource;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200561 u32 data[0];
562};
563
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400564static void release_descriptor(struct client *client,
565 struct client_resource *resource)
566{
567 struct descriptor *descriptor =
568 container_of(resource, struct descriptor, resource);
569
570 fw_core_remove_descriptor(&descriptor->d);
571 kfree(descriptor);
572}
573
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400574static int ioctl_add_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200575{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400576 struct fw_cdev_add_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200577 struct descriptor *descriptor;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200578 int retval;
579
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400580 if (request->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200581 return -EINVAL;
582
583 descriptor =
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400584 kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200585 if (descriptor == NULL)
586 return -ENOMEM;
587
588 if (copy_from_user(descriptor->data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400589 u64_to_uptr(request->data), request->length * 4)) {
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200590 kfree(descriptor);
591 return -EFAULT;
592 }
593
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400594 descriptor->d.length = request->length;
595 descriptor->d.immediate = request->immediate;
596 descriptor->d.key = request->key;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200597 descriptor->d.data = descriptor->data;
598
599 retval = fw_core_add_descriptor(&descriptor->d);
600 if (retval < 0) {
601 kfree(descriptor);
602 return retval;
603 }
604
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400605 descriptor->resource.release = release_descriptor;
606 add_client_resource(client, &descriptor->resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400607 request->handle = descriptor->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200608
609 return 0;
610}
611
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400612static int ioctl_remove_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200613{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400614 struct fw_cdev_remove_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200615
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400616 return release_client_resource(client, request->handle, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200617}
618
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500619static void
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500620iso_callback(struct fw_iso_context *context, u32 cycle,
621 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500622{
623 struct client *client = data;
624 struct iso_interrupt *interrupt;
625
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400626 interrupt = kzalloc(sizeof(*interrupt) + header_length, GFP_ATOMIC);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500627 if (interrupt == NULL)
628 return;
629
630 interrupt->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400631 interrupt->interrupt.closure = client->iso_closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500632 interrupt->interrupt.cycle = cycle;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500633 interrupt->interrupt.header_length = header_length;
634 memcpy(interrupt->interrupt.header, header, header_length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500635 queue_event(client, &interrupt->event,
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500636 &interrupt->interrupt,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400637 sizeof(interrupt->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500638}
639
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400640static int ioctl_create_iso_context(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500641{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400642 struct fw_cdev_create_iso_context *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500643
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400644 if (request->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500645 return -EINVAL;
646
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400647 switch (request->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400648 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400649 if (request->header_size < 4 || (request->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400650 return -EINVAL;
651
652 break;
653
654 case FW_ISO_CONTEXT_TRANSMIT:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400655 if (request->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400656 return -EINVAL;
657
658 break;
659
660 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500661 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400662 }
663
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400664 client->iso_closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500665 client->iso_context = fw_iso_context_create(client->device->card,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400666 request->type,
667 request->channel,
668 request->speed,
669 request->header_size,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500670 iso_callback, client);
671 if (IS_ERR(client->iso_context))
672 return PTR_ERR(client->iso_context);
673
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400674 /* We only support one context at this time. */
675 request->handle = 0;
676
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500677 return 0;
678}
679
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400680static int ioctl_queue_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500681{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400682 struct fw_cdev_queue_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500683 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500684 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200685 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500686 int count;
687 struct {
688 struct fw_iso_packet packet;
689 u8 header[256];
690 } u;
691
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400692 if (ctx == NULL || request->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500693 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500694
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400695 /*
696 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500697 * the iso buffer, and the pointer points inside the buffer,
698 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500699 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500700 * payload_length == 0 through. In other words, if no packets
701 * use the indirect payload, the iso buffer need not be mapped
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400702 * and the request->data pointer is ignored.
703 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500704
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400705 payload = (unsigned long)request->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200706 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400707 if (request->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200708 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500709 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200710 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500711 }
712
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400713 if (!access_ok(VERIFY_READ, request->packets, request->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500714 return -EFAULT;
715
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400716 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
717 end = (void __user *)p + request->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500718 count = 0;
719 while (p < end) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400720 if (__copy_from_user(&u.packet, p, sizeof(*p)))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500721 return -EFAULT;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500722
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500723 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500724 header_length = u.packet.header_length;
725 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400726 /*
727 * We require that header_length is a multiple of
728 * the fixed header size, ctx->header_size.
729 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500730 if (ctx->header_size == 0) {
731 if (u.packet.header_length > 0)
732 return -EINVAL;
733 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500734 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500735 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500736 header_length = 0;
737 }
738
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500739 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500740 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500741 if (next > end)
742 return -EINVAL;
743 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500744 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500745 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500746 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500747 u.packet.header_length + u.packet.payload_length > 0)
748 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200749 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500750 return -EINVAL;
751
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500752 if (fw_iso_context_queue(ctx, &u.packet,
753 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500754 break;
755
756 p = next;
757 payload += u.packet.payload_length;
758 count++;
759 }
760
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400761 request->size -= uptr_to_u64(p) - request->packets;
762 request->packets = uptr_to_u64(p);
763 request->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500764
765 return count;
766}
767
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400768static int ioctl_start_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500769{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400770 struct fw_cdev_start_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500771
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400772 if (request->handle != 0)
773 return -EINVAL;
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400774 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400775 if (request->tags == 0 || request->tags > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400776 return -EINVAL;
777
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400778 if (request->sync > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400779 return -EINVAL;
780 }
781
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400782 return fw_iso_context_start(client->iso_context, request->cycle,
783 request->sync, request->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500784}
785
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400786static int ioctl_stop_iso(struct client *client, void *buffer)
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500787{
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400788 struct fw_cdev_stop_iso *request = buffer;
789
790 if (request->handle != 0)
791 return -EINVAL;
792
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500793 return fw_iso_context_stop(client->iso_context);
794}
795
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400796static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
797 ioctl_get_info,
798 ioctl_send_request,
799 ioctl_allocate,
800 ioctl_deallocate,
801 ioctl_send_response,
802 ioctl_initiate_bus_reset,
803 ioctl_add_descriptor,
804 ioctl_remove_descriptor,
805 ioctl_create_iso_context,
806 ioctl_queue_iso,
807 ioctl_start_iso,
808 ioctl_stop_iso,
809};
810
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500811static int
812dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
813{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400814 char buffer[256];
815 int retval;
816
817 if (_IOC_TYPE(cmd) != '#' ||
818 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500819 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400820
821 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400822 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400823 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
824 return -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500825 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400826
827 retval = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
828 if (retval < 0)
829 return retval;
830
831 if (_IOC_DIR(cmd) & _IOC_READ) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400832 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400833 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
834 return -EFAULT;
835 }
836
837 return 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500838}
839
840static long
841fw_device_op_ioctl(struct file *file,
842 unsigned int cmd, unsigned long arg)
843{
844 struct client *client = file->private_data;
845
846 return dispatch_ioctl(client, cmd, (void __user *) arg);
847}
848
849#ifdef CONFIG_COMPAT
850static long
851fw_device_op_compat_ioctl(struct file *file,
852 unsigned int cmd, unsigned long arg)
853{
854 struct client *client = file->private_data;
855
856 return dispatch_ioctl(client, cmd, compat_ptr(arg));
857}
858#endif
859
860static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
861{
862 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500863 enum dma_data_direction direction;
864 unsigned long size;
865 int page_count, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500866
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500867 /* FIXME: We could support multiple buffers, but we don't. */
868 if (client->buffer.pages != NULL)
869 return -EBUSY;
870
871 if (!(vma->vm_flags & VM_SHARED))
872 return -EINVAL;
873
874 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500875 return -EINVAL;
876
877 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500878 size = vma->vm_end - vma->vm_start;
879 page_count = size >> PAGE_SHIFT;
880 if (size & ~PAGE_MASK)
881 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500882
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500883 if (vma->vm_flags & VM_WRITE)
884 direction = DMA_TO_DEVICE;
885 else
886 direction = DMA_FROM_DEVICE;
887
888 retval = fw_iso_buffer_init(&client->buffer, client->device->card,
889 page_count, direction);
890 if (retval < 0)
891 return retval;
892
893 retval = fw_iso_buffer_map(&client->buffer, vma);
894 if (retval < 0)
895 fw_iso_buffer_destroy(&client->buffer, client->device->card);
896
897 return retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500898}
899
900static int fw_device_op_release(struct inode *inode, struct file *file)
901{
902 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500903 struct event *e, *next_e;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400904 struct client_resource *r, *next_r;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500905 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500906
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500907 if (client->buffer.pages)
908 fw_iso_buffer_destroy(&client->buffer, client->device->card);
909
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500910 if (client->iso_context)
911 fw_iso_context_destroy(client->iso_context);
912
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400913 list_for_each_entry_safe(r, next_r, &client->resource_list, link)
914 r->release(client, r);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200915
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400916 /*
917 * FIXME: We should wait for the async tasklets to stop
918 * running before freeing the memory.
919 */
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500920
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500921 list_for_each_entry_safe(e, next_e, &client->event_list, link)
922 kfree(e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500923
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500924 spin_lock_irqsave(&client->device->card->lock, flags);
925 list_del(&client->link);
926 spin_unlock_irqrestore(&client->device->card->lock, flags);
927
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500928 fw_device_put(client->device);
929 kfree(client);
930
931 return 0;
932}
933
934static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
935{
936 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500937 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500938
939 poll_wait(file, &client->wait, pt);
940
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500941 if (fw_device_is_shutdown(client->device))
942 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500943 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500944 mask |= POLLIN | POLLRDNORM;
945
946 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500947}
948
Stefan Richter21ebcd12007-01-14 15:29:07 +0100949const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500950 .owner = THIS_MODULE,
951 .open = fw_device_op_open,
952 .read = fw_device_op_read,
953 .unlocked_ioctl = fw_device_op_ioctl,
954 .poll = fw_device_op_poll,
955 .release = fw_device_op_release,
956 .mmap = fw_device_op_mmap,
957
958#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +0100959 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500960#endif
961};