blob: d02dbc5af91e164c10cfb64d56258670981193d8 [file] [log] [blame]
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001/* -*- c-basic-offset: 8 -*-
2 *
3 * fw-device-cdev.c - Char device for device raw access
4 *
5 * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/wait.h>
25#include <linux/errno.h>
26#include <linux/device.h>
27#include <linux/vmalloc.h>
28#include <linux/poll.h>
29#include <linux/delay.h>
30#include <linux/mm.h>
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -050031#include <linux/idr.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050032#include <linux/compat.h>
33#include <asm/uaccess.h>
34#include "fw-transaction.h"
35#include "fw-topology.h"
36#include "fw-device.h"
37#include "fw-device-cdev.h"
38
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050039/* dequeue_event() just kfree()'s the event, so the event has to be
40 * the first field in the struct. */
41
Kristian Høgsberg3964a442007-03-27 01:43:41 -040042struct client;
43struct client_resource {
44 struct list_head link;
45 void (*release)(struct client *client, struct client_resource *r);
46 u32 handle;
47};
48
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050049struct event {
50 struct { void *data; size_t size; } v[2];
51 struct list_head link;
52};
53
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050054struct bus_reset {
55 struct event event;
56 struct fw_cdev_event_bus_reset reset;
57};
58
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050059struct response {
60 struct event event;
61 struct fw_transaction transaction;
62 struct client *client;
Kristian Høgsberg3964a442007-03-27 01:43:41 -040063 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050064 struct fw_cdev_event_response response;
65};
66
67struct iso_interrupt {
68 struct event event;
69 struct fw_cdev_event_iso_interrupt interrupt;
70};
71
72struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050073 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050074 struct fw_device *device;
75 spinlock_t lock;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +020076 u32 resource_handle;
Kristian Høgsberg3964a442007-03-27 01:43:41 -040077 struct list_head resource_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050078 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050079 wait_queue_head_t wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040080 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050081
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050082 struct fw_iso_context *iso_context;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050083 struct fw_iso_buffer buffer;
84 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050085
86 struct list_head link;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050087};
88
89static inline void __user *
90u64_to_uptr(__u64 value)
91{
92 return (void __user *)(unsigned long)value;
93}
94
95static inline __u64
96uptr_to_u64(void __user *ptr)
97{
98 return (__u64)(unsigned long)ptr;
99}
100
101static int fw_device_op_open(struct inode *inode, struct file *file)
102{
103 struct fw_device *device;
104 struct client *client;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500105 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500106
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500107 device = fw_device_from_devt(inode->i_rdev);
108 if (device == NULL)
109 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500110
111 client = kzalloc(sizeof *client, GFP_KERNEL);
112 if (client == NULL)
113 return -ENOMEM;
114
115 client->device = fw_device_get(device);
116 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400117 INIT_LIST_HEAD(&client->resource_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500118 spin_lock_init(&client->lock);
119 init_waitqueue_head(&client->wait);
120
121 file->private_data = client;
122
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500123 spin_lock_irqsave(&device->card->lock, flags);
124 list_add_tail(&client->link, &device->client_list);
125 spin_unlock_irqrestore(&device->card->lock, flags);
126
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500127 return 0;
128}
129
130static void queue_event(struct client *client, struct event *event,
131 void *data0, size_t size0, void *data1, size_t size1)
132{
133 unsigned long flags;
134
135 event->v[0].data = data0;
136 event->v[0].size = size0;
137 event->v[1].data = data1;
138 event->v[1].size = size1;
139
140 spin_lock_irqsave(&client->lock, flags);
141
142 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500143 wake_up_interruptible(&client->wait);
144
145 spin_unlock_irqrestore(&client->lock, flags);
146}
147
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500148static int
149dequeue_event(struct client *client, char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500150{
151 unsigned long flags;
152 struct event *event;
153 size_t size, total;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500154 int i, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500155
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500156 retval = wait_event_interruptible(client->wait,
157 !list_empty(&client->event_list) ||
158 fw_device_is_shutdown(client->device));
159 if (retval < 0)
160 return retval;
161
162 if (list_empty(&client->event_list) &&
163 fw_device_is_shutdown(client->device))
164 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500165
166 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500167 event = container_of(client->event_list.next, struct event, link);
168 list_del(&event->link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500169 spin_unlock_irqrestore(&client->lock, flags);
170
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500171 total = 0;
172 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
173 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500174 if (copy_to_user(buffer + total, event->v[i].data, size)) {
175 retval = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500176 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500177 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500178 total += size;
179 }
180 retval = total;
181
182 out:
183 kfree(event);
184
185 return retval;
186}
187
188static ssize_t
189fw_device_op_read(struct file *file,
190 char __user *buffer, size_t count, loff_t *offset)
191{
192 struct client *client = file->private_data;
193
194 return dequeue_event(client, buffer, count);
195}
196
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500197static void
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500198fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400199 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500200{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400201 struct fw_card *card = client->device->card;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500202
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400203 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500204 event->type = FW_CDEV_EVENT_BUS_RESET;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400205 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500206 event->local_node_id = card->local_node->node_id;
207 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
208 event->irm_node_id = card->irm_node->node_id;
209 event->root_node_id = card->root_node->node_id;
210 event->generation = card->generation;
211}
212
213static void
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500214for_each_client(struct fw_device *device,
215 void (*callback)(struct client *client))
216{
217 struct fw_card *card = device->card;
218 struct client *c;
219 unsigned long flags;
220
221 spin_lock_irqsave(&card->lock, flags);
222
223 list_for_each_entry(c, &device->client_list, link)
224 callback(c);
225
226 spin_unlock_irqrestore(&card->lock, flags);
227}
228
229static void
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500230queue_bus_reset_event(struct client *client)
231{
232 struct bus_reset *bus_reset;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500233
234 bus_reset = kzalloc(sizeof *bus_reset, GFP_ATOMIC);
235 if (bus_reset == NULL) {
236 fw_notify("Out of memory when allocating bus reset event\n");
237 return;
238 }
239
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400240 fill_bus_reset_event(&bus_reset->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500241
242 queue_event(client, &bus_reset->event,
243 &bus_reset->reset, sizeof bus_reset->reset, NULL, 0);
244}
245
246void fw_device_cdev_update(struct fw_device *device)
247{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500248 for_each_client(device, queue_bus_reset_event);
249}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500250
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500251static void wake_up_client(struct client *client)
252{
253 wake_up_interruptible(&client->wait);
254}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500255
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500256void fw_device_cdev_remove(struct fw_device *device)
257{
258 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500259}
260
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500261static int ioctl_get_info(struct client *client, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500262{
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500263 struct fw_cdev_get_info get_info;
264 struct fw_cdev_event_bus_reset bus_reset;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500265
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500266 if (copy_from_user(&get_info, arg, sizeof get_info))
267 return -EFAULT;
268
269 client->version = get_info.version;
270 get_info.version = FW_CDEV_VERSION;
271
272 if (get_info.rom != 0) {
273 void __user *uptr = u64_to_uptr(get_info.rom);
Stefan Richterd84702a2007-03-20 19:42:15 +0100274 size_t want = get_info.rom_length;
275 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500276
Stefan Richterd84702a2007-03-20 19:42:15 +0100277 if (copy_to_user(uptr, client->device->config_rom,
278 min(want, have)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500279 return -EFAULT;
280 }
281 get_info.rom_length = client->device->config_rom_length * 4;
282
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400283 client->bus_reset_closure = get_info.bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500284 if (get_info.bus_reset != 0) {
285 void __user *uptr = u64_to_uptr(get_info.bus_reset);
286
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400287 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500288 if (copy_to_user(uptr, &bus_reset, sizeof bus_reset))
289 return -EFAULT;
290 }
291
Kristian Høgsberge7533502007-03-07 12:12:52 -0500292 get_info.card = client->device->card->index;
293
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500294 if (copy_to_user(arg, &get_info, sizeof get_info))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500295 return -EFAULT;
296
297 return 0;
298}
299
300static void
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400301add_client_resource(struct client *client, struct client_resource *resource)
302{
303 unsigned long flags;
304
305 spin_lock_irqsave(&client->lock, flags);
306 list_add_tail(&resource->link, &client->resource_list);
307 resource->handle = client->resource_handle++;
308 spin_unlock_irqrestore(&client->lock, flags);
309}
310
311static int
312release_client_resource(struct client *client, u32 handle,
313 struct client_resource **resource)
314{
315 struct client_resource *r;
316 unsigned long flags;
317
318 spin_lock_irqsave(&client->lock, flags);
319 list_for_each_entry(r, &client->resource_list, link) {
320 if (r->handle == handle) {
321 list_del(&r->link);
322 break;
323 }
324 }
325 spin_unlock_irqrestore(&client->lock, flags);
326
327 if (&r->link == &client->resource_list)
328 return -EINVAL;
329
330 if (resource)
331 *resource = r;
332 else
333 r->release(client, r);
334
335 return 0;
336}
337
338static void
339release_transaction(struct client *client, struct client_resource *resource)
340{
341 struct response *response =
342 container_of(resource, struct response, resource);
343
344 fw_cancel_transaction(client->device->card, &response->transaction);
345}
346
347static void
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500348complete_transaction(struct fw_card *card, int rcode,
349 void *payload, size_t length, void *data)
350{
351 struct response *response = data;
352 struct client *client = response->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500353 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500354
355 if (length < response->response.length)
356 response->response.length = length;
357 if (rcode == RCODE_COMPLETE)
358 memcpy(response->response.data, payload,
359 response->response.length);
360
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500361 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400362 list_del(&response->resource.link);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500363 spin_unlock_irqrestore(&client->lock, flags);
364
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500365 response->response.type = FW_CDEV_EVENT_RESPONSE;
366 response->response.rcode = rcode;
367 queue_event(client, &response->event,
368 &response->response, sizeof response->response,
369 response->response.data, response->response.length);
370}
371
372static ssize_t ioctl_send_request(struct client *client, void __user *arg)
373{
374 struct fw_device *device = client->device;
375 struct fw_cdev_send_request request;
376 struct response *response;
377
378 if (copy_from_user(&request, arg, sizeof request))
379 return -EFAULT;
380
381 /* What is the biggest size we'll accept, really? */
382 if (request.length > 4096)
383 return -EINVAL;
384
385 response = kmalloc(sizeof *response + request.length, GFP_KERNEL);
386 if (response == NULL)
387 return -ENOMEM;
388
389 response->client = client;
390 response->response.length = request.length;
391 response->response.closure = request.closure;
392
393 if (request.data &&
394 copy_from_user(response->response.data,
395 u64_to_uptr(request.data), request.length)) {
396 kfree(response);
397 return -EFAULT;
398 }
399
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400400 response->resource.release = release_transaction;
401 add_client_resource(client, &response->resource);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500402
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500403 fw_send_request(device->card, &response->transaction,
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500404 request.tcode & 0x1f,
Stefan Richter907293d2007-01-23 21:11:43 +0100405 device->node->node_id,
Kristian Høgsberg97e35272007-03-07 12:12:53 -0500406 request.generation,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500407 device->node->max_speed,
408 request.offset,
409 response->response.data, request.length,
410 complete_transaction, response);
411
412 if (request.data)
413 return sizeof request + request.length;
414 else
415 return sizeof request;
416}
417
418struct address_handler {
419 struct fw_address_handler handler;
420 __u64 closure;
421 struct client *client;
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 {
426 struct fw_request *request;
427 void *data;
428 size_t length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400429 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500430};
431
432struct request_event {
433 struct event event;
434 struct fw_cdev_event_request request;
435};
436
437static void
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400438release_request(struct client *client, struct client_resource *resource)
439{
440 struct request *request =
441 container_of(resource, struct request, resource);
442
443 fw_send_response(client->device->card, request->request,
444 RCODE_CONFLICT_ERROR);
445 kfree(request);
446}
447
448static void
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500449handle_request(struct fw_card *card, struct fw_request *r,
450 int tcode, int destination, int source,
451 int generation, int speed,
452 unsigned long long offset,
453 void *payload, size_t length, void *callback_data)
454{
455 struct address_handler *handler = callback_data;
456 struct request *request;
457 struct request_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500458 struct client *client = handler->client;
459
460 request = kmalloc(sizeof *request, GFP_ATOMIC);
461 e = kmalloc(sizeof *e, GFP_ATOMIC);
462 if (request == NULL || e == NULL) {
463 kfree(request);
464 kfree(e);
465 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
466 return;
467 }
468
469 request->request = r;
470 request->data = payload;
471 request->length = length;
472
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400473 request->resource.release = release_request;
474 add_client_resource(client, &request->resource);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500475
476 e->request.type = FW_CDEV_EVENT_REQUEST;
477 e->request.tcode = tcode;
478 e->request.offset = offset;
479 e->request.length = length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400480 e->request.handle = request->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500481 e->request.closure = handler->closure;
482
483 queue_event(client, &e->event,
484 &e->request, sizeof e->request, payload, length);
485}
486
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400487static void
488release_address_handler(struct client *client,
489 struct client_resource *resource)
490{
491 struct address_handler *handler =
492 container_of(resource, struct address_handler, resource);
493
494 fw_core_remove_address_handler(&handler->handler);
495 kfree(handler);
496}
497
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500498static int ioctl_allocate(struct client *client, void __user *arg)
499{
500 struct fw_cdev_allocate request;
501 struct address_handler *handler;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500502 struct fw_address_region region;
503
504 if (copy_from_user(&request, arg, sizeof request))
505 return -EFAULT;
506
507 handler = kmalloc(sizeof *handler, GFP_KERNEL);
508 if (handler == NULL)
509 return -ENOMEM;
510
511 region.start = request.offset;
512 region.end = request.offset + request.length;
513 handler->handler.length = request.length;
514 handler->handler.address_callback = handle_request;
515 handler->handler.callback_data = handler;
516 handler->closure = request.closure;
517 handler->client = client;
518
519 if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
520 kfree(handler);
521 return -EBUSY;
522 }
523
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400524 handler->resource.release = release_address_handler;
525 add_client_resource(client, &handler->resource);
526 request.handle = handler->resource.handle;
527
528 if (copy_to_user(arg, &request, sizeof request))
529 return -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500530
531 return 0;
532}
533
Kristian Høgsberg94723162007-03-14 17:34:55 -0400534static int ioctl_deallocate(struct client *client, void __user *arg)
535{
536 struct fw_cdev_deallocate request;
Kristian Høgsberg94723162007-03-14 17:34:55 -0400537
538 if (copy_from_user(&request, arg, sizeof request))
539 return -EFAULT;
540
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400541 return release_client_resource(client, request.handle, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400542}
543
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500544static int ioctl_send_response(struct client *client, void __user *arg)
545{
546 struct fw_cdev_send_response request;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400547 struct client_resource *resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500548 struct request *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500549
550 if (copy_from_user(&request, arg, sizeof request))
551 return -EFAULT;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400552 if (release_client_resource(client, request.handle, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500553 return -EINVAL;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400554 r = container_of(resource, struct request, resource);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500555 if (request.length < r->length)
556 r->length = request.length;
557 if (copy_from_user(r->data, u64_to_uptr(request.data), r->length))
558 return -EFAULT;
559
560 fw_send_response(client->device->card, r->request, request.rcode);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500561 kfree(r);
562
563 return 0;
564}
565
Kristian Høgsberg53718422007-03-07 12:12:42 -0500566static int ioctl_initiate_bus_reset(struct client *client, void __user *arg)
567{
568 struct fw_cdev_initiate_bus_reset request;
569 int short_reset;
570
571 if (copy_from_user(&request, arg, sizeof request))
572 return -EFAULT;
573
574 short_reset = (request.type == FW_CDEV_SHORT_RESET);
575
576 return fw_core_initiate_bus_reset(client->device->card, short_reset);
577}
578
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200579struct descriptor {
580 struct fw_descriptor d;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400581 struct client_resource resource;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200582 u32 data[0];
583};
584
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400585static void release_descriptor(struct client *client,
586 struct client_resource *resource)
587{
588 struct descriptor *descriptor =
589 container_of(resource, struct descriptor, resource);
590
591 fw_core_remove_descriptor(&descriptor->d);
592 kfree(descriptor);
593}
594
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200595static int ioctl_add_descriptor(struct client *client, void __user *arg)
596{
597 struct fw_cdev_add_descriptor request;
598 struct descriptor *descriptor;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200599 int retval;
600
601 if (copy_from_user(&request, arg, sizeof request))
602 return -EFAULT;
603
604 if (request.length > 256)
605 return -EINVAL;
606
607 descriptor =
608 kmalloc(sizeof *descriptor + request.length * 4, GFP_KERNEL);
609 if (descriptor == NULL)
610 return -ENOMEM;
611
612 if (copy_from_user(descriptor->data,
613 u64_to_uptr(request.data), request.length * 4)) {
614 kfree(descriptor);
615 return -EFAULT;
616 }
617
618 descriptor->d.length = request.length;
619 descriptor->d.immediate = request.immediate;
620 descriptor->d.key = request.key;
621 descriptor->d.data = descriptor->data;
622
623 retval = fw_core_add_descriptor(&descriptor->d);
624 if (retval < 0) {
625 kfree(descriptor);
626 return retval;
627 }
628
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400629 descriptor->resource.release = release_descriptor;
630 add_client_resource(client, &descriptor->resource);
631 request.handle = descriptor->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200632
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200633 if (copy_to_user(arg, &request, sizeof request))
634 return -EFAULT;
635
636 return 0;
637}
638
639static int ioctl_remove_descriptor(struct client *client, void __user *arg)
640{
641 struct fw_cdev_remove_descriptor request;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200642
643 if (copy_from_user(&request, arg, sizeof request))
644 return -EFAULT;
645
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400646 return release_client_resource(client, request.handle, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200647}
648
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500649static void
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500650iso_callback(struct fw_iso_context *context, u32 cycle,
651 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500652{
653 struct client *client = data;
654 struct iso_interrupt *interrupt;
655
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500656 interrupt = kzalloc(sizeof *interrupt + header_length, GFP_ATOMIC);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500657 if (interrupt == NULL)
658 return;
659
660 interrupt->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
661 interrupt->interrupt.closure = 0;
662 interrupt->interrupt.cycle = cycle;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500663 interrupt->interrupt.header_length = header_length;
664 memcpy(interrupt->interrupt.header, header, header_length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500665 queue_event(client, &interrupt->event,
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500666 &interrupt->interrupt,
667 sizeof interrupt->interrupt + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500668}
669
670static int ioctl_create_iso_context(struct client *client, void __user *arg)
671{
672 struct fw_cdev_create_iso_context request;
673
674 if (copy_from_user(&request, arg, sizeof request))
675 return -EFAULT;
676
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500677 if (request.channel > 63)
678 return -EINVAL;
679
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400680 switch (request.type) {
681 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400682 if (request.header_size < 4 || (request.header_size & 3))
683 return -EINVAL;
684
685 break;
686
687 case FW_ISO_CONTEXT_TRANSMIT:
688 if (request.speed > SCODE_3200)
689 return -EINVAL;
690
691 break;
692
693 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500694 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400695 }
696
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500697 client->iso_context = fw_iso_context_create(client->device->card,
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500698 request.type,
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500699 request.channel,
700 request.speed,
Kristian Høgsberg8fbdbb32007-03-07 12:12:51 -0500701 request.header_size,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500702 iso_callback, client);
703 if (IS_ERR(client->iso_context))
704 return PTR_ERR(client->iso_context);
705
706 return 0;
707}
708
709static int ioctl_queue_iso(struct client *client, void __user *arg)
710{
711 struct fw_cdev_queue_iso request;
712 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500713 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500714 unsigned long payload, payload_end, header_length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500715 int count;
716 struct {
717 struct fw_iso_packet packet;
718 u8 header[256];
719 } u;
720
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500721 if (ctx == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500722 return -EINVAL;
723 if (copy_from_user(&request, arg, sizeof request))
724 return -EFAULT;
725
726 /* If the user passes a non-NULL data pointer, has mmap()'ed
727 * the iso buffer, and the pointer points inside the buffer,
728 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500729 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500730 * payload_length == 0 through. In other words, if no packets
731 * use the indirect payload, the iso buffer need not be mapped
732 * and the request.data pointer is ignored.*/
733
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500734 payload = (unsigned long)request.data - client->vm_start;
735 payload_end = payload + (client->buffer.page_count << PAGE_SHIFT);
736 if (request.data == 0 || client->buffer.pages == NULL ||
737 payload >= payload_end) {
738 payload = 0;
739 payload_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500740 }
741
742 if (!access_ok(VERIFY_READ, request.packets, request.size))
743 return -EFAULT;
744
745 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request.packets);
746 end = (void __user *)p + request.size;
747 count = 0;
748 while (p < end) {
749 if (__copy_from_user(&u.packet, p, sizeof *p))
750 return -EFAULT;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500751
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500752 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500753 header_length = u.packet.header_length;
754 } else {
755 /* We require that header_length is a multiple of
756 * the fixed header size, ctx->header_size */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500757 if (ctx->header_size == 0) {
758 if (u.packet.header_length > 0)
759 return -EINVAL;
760 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500761 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500762 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500763 header_length = 0;
764 }
765
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500766 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500767 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500768 if (next > end)
769 return -EINVAL;
770 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500771 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500772 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500773 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500774 u.packet.header_length + u.packet.payload_length > 0)
775 return -EINVAL;
776 if (payload + u.packet.payload_length > payload_end)
777 return -EINVAL;
778
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500779 if (fw_iso_context_queue(ctx, &u.packet,
780 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500781 break;
782
783 p = next;
784 payload += u.packet.payload_length;
785 count++;
786 }
787
788 request.size -= uptr_to_u64(p) - request.packets;
789 request.packets = uptr_to_u64(p);
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500790 request.data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500791
792 if (copy_to_user(arg, &request, sizeof request))
793 return -EFAULT;
794
795 return count;
796}
797
Kristian Høgsberg69cdb722007-02-16 17:34:41 -0500798static int ioctl_start_iso(struct client *client, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500799{
Kristian Høgsberg69cdb722007-02-16 17:34:41 -0500800 struct fw_cdev_start_iso request;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500801
802 if (copy_from_user(&request, arg, sizeof request))
803 return -EFAULT;
804
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400805 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
806 if (request.tags == 0 || request.tags > 15)
807 return -EINVAL;
808
809 if (request.sync > 15)
810 return -EINVAL;
811 }
812
813 return fw_iso_context_start(client->iso_context,
814 request.cycle, request.sync, request.tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500815}
816
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500817static int ioctl_stop_iso(struct client *client, void __user *arg)
818{
819 return fw_iso_context_stop(client->iso_context);
820}
821
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500822static int
823dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
824{
825 switch (cmd) {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500826 case FW_CDEV_IOC_GET_INFO:
827 return ioctl_get_info(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500828 case FW_CDEV_IOC_SEND_REQUEST:
829 return ioctl_send_request(client, arg);
830 case FW_CDEV_IOC_ALLOCATE:
831 return ioctl_allocate(client, arg);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400832 case FW_CDEV_IOC_DEALLOCATE:
833 return ioctl_deallocate(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500834 case FW_CDEV_IOC_SEND_RESPONSE:
835 return ioctl_send_response(client, arg);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500836 case FW_CDEV_IOC_INITIATE_BUS_RESET:
837 return ioctl_initiate_bus_reset(client, arg);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200838 case FW_CDEV_IOC_ADD_DESCRIPTOR:
839 return ioctl_add_descriptor(client, arg);
840 case FW_CDEV_IOC_REMOVE_DESCRIPTOR:
841 return ioctl_remove_descriptor(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500842 case FW_CDEV_IOC_CREATE_ISO_CONTEXT:
843 return ioctl_create_iso_context(client, arg);
844 case FW_CDEV_IOC_QUEUE_ISO:
845 return ioctl_queue_iso(client, arg);
Kristian Høgsberg69cdb722007-02-16 17:34:41 -0500846 case FW_CDEV_IOC_START_ISO:
847 return ioctl_start_iso(client, arg);
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500848 case FW_CDEV_IOC_STOP_ISO:
849 return ioctl_stop_iso(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500850 default:
851 return -EINVAL;
852 }
853}
854
855static long
856fw_device_op_ioctl(struct file *file,
857 unsigned int cmd, unsigned long arg)
858{
859 struct client *client = file->private_data;
860
861 return dispatch_ioctl(client, cmd, (void __user *) arg);
862}
863
864#ifdef CONFIG_COMPAT
865static long
866fw_device_op_compat_ioctl(struct file *file,
867 unsigned int cmd, unsigned long arg)
868{
869 struct client *client = file->private_data;
870
871 return dispatch_ioctl(client, cmd, compat_ptr(arg));
872}
873#endif
874
875static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
876{
877 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500878 enum dma_data_direction direction;
879 unsigned long size;
880 int page_count, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500881
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500882 /* FIXME: We could support multiple buffers, but we don't. */
883 if (client->buffer.pages != NULL)
884 return -EBUSY;
885
886 if (!(vma->vm_flags & VM_SHARED))
887 return -EINVAL;
888
889 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500890 return -EINVAL;
891
892 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500893 size = vma->vm_end - vma->vm_start;
894 page_count = size >> PAGE_SHIFT;
895 if (size & ~PAGE_MASK)
896 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500897
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500898 if (vma->vm_flags & VM_WRITE)
899 direction = DMA_TO_DEVICE;
900 else
901 direction = DMA_FROM_DEVICE;
902
903 retval = fw_iso_buffer_init(&client->buffer, client->device->card,
904 page_count, direction);
905 if (retval < 0)
906 return retval;
907
908 retval = fw_iso_buffer_map(&client->buffer, vma);
909 if (retval < 0)
910 fw_iso_buffer_destroy(&client->buffer, client->device->card);
911
912 return retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500913}
914
915static int fw_device_op_release(struct inode *inode, struct file *file)
916{
917 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500918 struct event *e, *next_e;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400919 struct client_resource *r, *next_r;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500920 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500921
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500922 if (client->buffer.pages)
923 fw_iso_buffer_destroy(&client->buffer, client->device->card);
924
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500925 if (client->iso_context)
926 fw_iso_context_destroy(client->iso_context);
927
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400928 list_for_each_entry_safe(r, next_r, &client->resource_list, link)
929 r->release(client, r);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200930
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500931 /* FIXME: We should wait for the async tasklets to stop
932 * running before freeing the memory. */
933
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500934 list_for_each_entry_safe(e, next_e, &client->event_list, link)
935 kfree(e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500936
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500937 spin_lock_irqsave(&client->device->card->lock, flags);
938 list_del(&client->link);
939 spin_unlock_irqrestore(&client->device->card->lock, flags);
940
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500941 fw_device_put(client->device);
942 kfree(client);
943
944 return 0;
945}
946
947static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
948{
949 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500950 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500951
952 poll_wait(file, &client->wait, pt);
953
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500954 if (fw_device_is_shutdown(client->device))
955 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500956 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500957 mask |= POLLIN | POLLRDNORM;
958
959 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500960}
961
Stefan Richter21ebcd12007-01-14 15:29:07 +0100962const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500963 .owner = THIS_MODULE,
964 .open = fw_device_op_open,
965 .read = fw_device_op_read,
966 .unlocked_ioctl = fw_device_op_ioctl,
967 .poll = fw_device_op_poll,
968 .release = fw_device_op_release,
969 .mmap = fw_device_op_mmap,
970
971#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +0100972 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500973#endif
974};