blob: e3c4a52a44a90a5b728560971a9925bca2711fd2 [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
39/*
40 * todo
41 *
42 * - bus resets sends a new packet with new generation and node id
43 *
44 */
45
46/* dequeue_event() just kfree()'s the event, so the event has to be
47 * the first field in the struct. */
48
49struct 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øgsberg28cf6a02007-03-07 12:12:50 -050063 struct list_head link;
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øgsberg19a15b92006-12-19 19:58:31 -050077 struct list_head handler_list;
78 struct list_head request_list;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -050079 struct list_head transaction_list;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +020080 struct list_head descriptor_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050081 u32 request_serial;
82 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øgsberg9aad8122007-02-16 17:34:38 -050087 struct fw_iso_buffer buffer;
88 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050089
90 struct list_head link;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050091};
92
93static inline void __user *
94u64_to_uptr(__u64 value)
95{
96 return (void __user *)(unsigned long)value;
97}
98
99static inline __u64
100uptr_to_u64(void __user *ptr)
101{
102 return (__u64)(unsigned long)ptr;
103}
104
105static int fw_device_op_open(struct inode *inode, struct file *file)
106{
107 struct fw_device *device;
108 struct client *client;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500109 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500110
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500111 device = fw_device_from_devt(inode->i_rdev);
112 if (device == NULL)
113 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500114
115 client = kzalloc(sizeof *client, GFP_KERNEL);
116 if (client == NULL)
117 return -ENOMEM;
118
119 client->device = fw_device_get(device);
120 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500121 INIT_LIST_HEAD(&client->handler_list);
122 INIT_LIST_HEAD(&client->request_list);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500123 INIT_LIST_HEAD(&client->transaction_list);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200124 INIT_LIST_HEAD(&client->descriptor_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500125 spin_lock_init(&client->lock);
126 init_waitqueue_head(&client->wait);
127
128 file->private_data = client;
129
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500130 spin_lock_irqsave(&device->card->lock, flags);
131 list_add_tail(&client->link, &device->client_list);
132 spin_unlock_irqrestore(&device->card->lock, flags);
133
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500134 return 0;
135}
136
137static void queue_event(struct client *client, struct event *event,
138 void *data0, size_t size0, void *data1, size_t size1)
139{
140 unsigned long flags;
141
142 event->v[0].data = data0;
143 event->v[0].size = size0;
144 event->v[1].data = data1;
145 event->v[1].size = size1;
146
147 spin_lock_irqsave(&client->lock, flags);
148
149 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500150 wake_up_interruptible(&client->wait);
151
152 spin_unlock_irqrestore(&client->lock, flags);
153}
154
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500155static int
156dequeue_event(struct client *client, char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500157{
158 unsigned long flags;
159 struct event *event;
160 size_t size, total;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500161 int i, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500162
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500163 retval = wait_event_interruptible(client->wait,
164 !list_empty(&client->event_list) ||
165 fw_device_is_shutdown(client->device));
166 if (retval < 0)
167 return retval;
168
169 if (list_empty(&client->event_list) &&
170 fw_device_is_shutdown(client->device))
171 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500172
173 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500174 event = container_of(client->event_list.next, struct event, link);
175 list_del(&event->link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500176 spin_unlock_irqrestore(&client->lock, flags);
177
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500178 total = 0;
179 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
180 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500181 if (copy_to_user(buffer + total, event->v[i].data, size)) {
182 retval = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500183 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500184 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500185 total += size;
186 }
187 retval = total;
188
189 out:
190 kfree(event);
191
192 return retval;
193}
194
195static ssize_t
196fw_device_op_read(struct file *file,
197 char __user *buffer, size_t count, loff_t *offset)
198{
199 struct client *client = file->private_data;
200
201 return dequeue_event(client, buffer, count);
202}
203
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500204static void
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500205fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400206 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500207{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400208 struct fw_card *card = client->device->card;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500209
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400210 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500211 event->type = FW_CDEV_EVENT_BUS_RESET;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400212 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500213 event->local_node_id = card->local_node->node_id;
214 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
215 event->irm_node_id = card->irm_node->node_id;
216 event->root_node_id = card->root_node->node_id;
217 event->generation = card->generation;
218}
219
220static void
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500221for_each_client(struct fw_device *device,
222 void (*callback)(struct client *client))
223{
224 struct fw_card *card = device->card;
225 struct client *c;
226 unsigned long flags;
227
228 spin_lock_irqsave(&card->lock, flags);
229
230 list_for_each_entry(c, &device->client_list, link)
231 callback(c);
232
233 spin_unlock_irqrestore(&card->lock, flags);
234}
235
236static void
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500237queue_bus_reset_event(struct client *client)
238{
239 struct bus_reset *bus_reset;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500240
241 bus_reset = kzalloc(sizeof *bus_reset, GFP_ATOMIC);
242 if (bus_reset == NULL) {
243 fw_notify("Out of memory when allocating bus reset event\n");
244 return;
245 }
246
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400247 fill_bus_reset_event(&bus_reset->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500248
249 queue_event(client, &bus_reset->event,
250 &bus_reset->reset, sizeof bus_reset->reset, NULL, 0);
251}
252
253void fw_device_cdev_update(struct fw_device *device)
254{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500255 for_each_client(device, queue_bus_reset_event);
256}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500257
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500258static void wake_up_client(struct client *client)
259{
260 wake_up_interruptible(&client->wait);
261}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500262
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500263void fw_device_cdev_remove(struct fw_device *device)
264{
265 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500266}
267
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500268static int ioctl_get_info(struct client *client, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500269{
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500270 struct fw_cdev_get_info get_info;
271 struct fw_cdev_event_bus_reset bus_reset;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500272
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500273 if (copy_from_user(&get_info, arg, sizeof get_info))
274 return -EFAULT;
275
276 client->version = get_info.version;
277 get_info.version = FW_CDEV_VERSION;
278
279 if (get_info.rom != 0) {
280 void __user *uptr = u64_to_uptr(get_info.rom);
Stefan Richterd84702a2007-03-20 19:42:15 +0100281 size_t want = get_info.rom_length;
282 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500283
Stefan Richterd84702a2007-03-20 19:42:15 +0100284 if (copy_to_user(uptr, client->device->config_rom,
285 min(want, have)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500286 return -EFAULT;
287 }
288 get_info.rom_length = client->device->config_rom_length * 4;
289
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400290 client->bus_reset_closure = get_info.bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500291 if (get_info.bus_reset != 0) {
292 void __user *uptr = u64_to_uptr(get_info.bus_reset);
293
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400294 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500295 if (copy_to_user(uptr, &bus_reset, sizeof bus_reset))
296 return -EFAULT;
297 }
298
Kristian Høgsberge7533502007-03-07 12:12:52 -0500299 get_info.card = client->device->card->index;
300
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500301 if (copy_to_user(arg, &get_info, sizeof get_info))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500302 return -EFAULT;
303
304 return 0;
305}
306
307static void
308complete_transaction(struct fw_card *card, int rcode,
309 void *payload, size_t length, void *data)
310{
311 struct response *response = data;
312 struct client *client = response->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500313 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500314
315 if (length < response->response.length)
316 response->response.length = length;
317 if (rcode == RCODE_COMPLETE)
318 memcpy(response->response.data, payload,
319 response->response.length);
320
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500321 spin_lock_irqsave(&client->lock, flags);
322 list_del(&response->link);
323 spin_unlock_irqrestore(&client->lock, flags);
324
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500325 response->response.type = FW_CDEV_EVENT_RESPONSE;
326 response->response.rcode = rcode;
327 queue_event(client, &response->event,
328 &response->response, sizeof response->response,
329 response->response.data, response->response.length);
330}
331
332static ssize_t ioctl_send_request(struct client *client, void __user *arg)
333{
334 struct fw_device *device = client->device;
335 struct fw_cdev_send_request request;
336 struct response *response;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500337 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500338
339 if (copy_from_user(&request, arg, sizeof request))
340 return -EFAULT;
341
342 /* What is the biggest size we'll accept, really? */
343 if (request.length > 4096)
344 return -EINVAL;
345
346 response = kmalloc(sizeof *response + request.length, GFP_KERNEL);
347 if (response == NULL)
348 return -ENOMEM;
349
350 response->client = client;
351 response->response.length = request.length;
352 response->response.closure = request.closure;
353
354 if (request.data &&
355 copy_from_user(response->response.data,
356 u64_to_uptr(request.data), request.length)) {
357 kfree(response);
358 return -EFAULT;
359 }
360
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500361 spin_lock_irqsave(&client->lock, flags);
362 list_add_tail(&response->link, &client->transaction_list);
363 spin_unlock_irqrestore(&client->lock, flags);
364
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500365 fw_send_request(device->card, &response->transaction,
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500366 request.tcode & 0x1f,
Stefan Richter907293d2007-01-23 21:11:43 +0100367 device->node->node_id,
Kristian Høgsberg97e35272007-03-07 12:12:53 -0500368 request.generation,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500369 device->node->max_speed,
370 request.offset,
371 response->response.data, request.length,
372 complete_transaction, response);
373
374 if (request.data)
375 return sizeof request + request.length;
376 else
377 return sizeof request;
378}
379
380struct address_handler {
381 struct fw_address_handler handler;
382 __u64 closure;
383 struct client *client;
384 struct list_head link;
385};
386
387struct request {
388 struct fw_request *request;
389 void *data;
390 size_t length;
391 u32 serial;
392 struct list_head link;
393};
394
395struct request_event {
396 struct event event;
397 struct fw_cdev_event_request request;
398};
399
400static void
401handle_request(struct fw_card *card, struct fw_request *r,
402 int tcode, int destination, int source,
403 int generation, int speed,
404 unsigned long long offset,
405 void *payload, size_t length, void *callback_data)
406{
407 struct address_handler *handler = callback_data;
408 struct request *request;
409 struct request_event *e;
410 unsigned long flags;
411 struct client *client = handler->client;
412
413 request = kmalloc(sizeof *request, GFP_ATOMIC);
414 e = kmalloc(sizeof *e, GFP_ATOMIC);
415 if (request == NULL || e == NULL) {
416 kfree(request);
417 kfree(e);
418 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
419 return;
420 }
421
422 request->request = r;
423 request->data = payload;
424 request->length = length;
425
426 spin_lock_irqsave(&client->lock, flags);
427 request->serial = client->request_serial++;
428 list_add_tail(&request->link, &client->request_list);
429 spin_unlock_irqrestore(&client->lock, flags);
430
431 e->request.type = FW_CDEV_EVENT_REQUEST;
432 e->request.tcode = tcode;
433 e->request.offset = offset;
434 e->request.length = length;
435 e->request.serial = request->serial;
436 e->request.closure = handler->closure;
437
438 queue_event(client, &e->event,
439 &e->request, sizeof e->request, payload, length);
440}
441
442static int ioctl_allocate(struct client *client, void __user *arg)
443{
444 struct fw_cdev_allocate request;
445 struct address_handler *handler;
446 unsigned long flags;
447 struct fw_address_region region;
448
449 if (copy_from_user(&request, arg, sizeof request))
450 return -EFAULT;
451
452 handler = kmalloc(sizeof *handler, GFP_KERNEL);
453 if (handler == NULL)
454 return -ENOMEM;
455
456 region.start = request.offset;
457 region.end = request.offset + request.length;
458 handler->handler.length = request.length;
459 handler->handler.address_callback = handle_request;
460 handler->handler.callback_data = handler;
461 handler->closure = request.closure;
462 handler->client = client;
463
464 if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
465 kfree(handler);
466 return -EBUSY;
467 }
468
469 spin_lock_irqsave(&client->lock, flags);
470 list_add_tail(&handler->link, &client->handler_list);
471 spin_unlock_irqrestore(&client->lock, flags);
472
473 return 0;
474}
475
Kristian Høgsberg94723162007-03-14 17:34:55 -0400476static int ioctl_deallocate(struct client *client, void __user *arg)
477{
478 struct fw_cdev_deallocate request;
479 struct address_handler *handler;
480 unsigned long flags;
481
482 if (copy_from_user(&request, arg, sizeof request))
483 return -EFAULT;
484
485 spin_lock_irqsave(&client->lock, flags);
486 list_for_each_entry(handler, &client->handler_list, link) {
487 if (handler->handler.offset == request.offset) {
488 list_del(&handler->link);
489 break;
490 }
491 }
492 spin_unlock_irqrestore(&client->lock, flags);
493
494 if (&handler->link == &client->handler_list)
495 return -EINVAL;
496
497 fw_core_remove_address_handler(&handler->handler);
498
499 return 0;
500}
501
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500502static int ioctl_send_response(struct client *client, void __user *arg)
503{
504 struct fw_cdev_send_response request;
505 struct request *r;
506 unsigned long flags;
507
508 if (copy_from_user(&request, arg, sizeof request))
509 return -EFAULT;
510
511 spin_lock_irqsave(&client->lock, flags);
512 list_for_each_entry(r, &client->request_list, link) {
513 if (r->serial == request.serial) {
514 list_del(&r->link);
515 break;
516 }
517 }
518 spin_unlock_irqrestore(&client->lock, flags);
519
520 if (&r->link == &client->request_list)
521 return -EINVAL;
522
523 if (request.length < r->length)
524 r->length = request.length;
525 if (copy_from_user(r->data, u64_to_uptr(request.data), r->length))
526 return -EFAULT;
527
528 fw_send_response(client->device->card, r->request, request.rcode);
529
530 kfree(r);
531
532 return 0;
533}
534
Kristian Høgsberg53718422007-03-07 12:12:42 -0500535static int ioctl_initiate_bus_reset(struct client *client, void __user *arg)
536{
537 struct fw_cdev_initiate_bus_reset request;
538 int short_reset;
539
540 if (copy_from_user(&request, arg, sizeof request))
541 return -EFAULT;
542
543 short_reset = (request.type == FW_CDEV_SHORT_RESET);
544
545 return fw_core_initiate_bus_reset(client->device->card, short_reset);
546}
547
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200548struct descriptor {
549 struct fw_descriptor d;
550 struct list_head link;
551 u32 handle;
552 u32 data[0];
553};
554
555static int ioctl_add_descriptor(struct client *client, void __user *arg)
556{
557 struct fw_cdev_add_descriptor request;
558 struct descriptor *descriptor;
559 unsigned long flags;
560 int retval;
561
562 if (copy_from_user(&request, arg, sizeof request))
563 return -EFAULT;
564
565 if (request.length > 256)
566 return -EINVAL;
567
568 descriptor =
569 kmalloc(sizeof *descriptor + request.length * 4, GFP_KERNEL);
570 if (descriptor == NULL)
571 return -ENOMEM;
572
573 if (copy_from_user(descriptor->data,
574 u64_to_uptr(request.data), request.length * 4)) {
575 kfree(descriptor);
576 return -EFAULT;
577 }
578
579 descriptor->d.length = request.length;
580 descriptor->d.immediate = request.immediate;
581 descriptor->d.key = request.key;
582 descriptor->d.data = descriptor->data;
583
584 retval = fw_core_add_descriptor(&descriptor->d);
585 if (retval < 0) {
586 kfree(descriptor);
587 return retval;
588 }
589
590 spin_lock_irqsave(&client->lock, flags);
591 list_add_tail(&descriptor->link, &client->descriptor_list);
592 descriptor->handle = client->resource_handle++;
593 spin_unlock_irqrestore(&client->lock, flags);
594
595 request.handle = descriptor->handle;
596 if (copy_to_user(arg, &request, sizeof request))
597 return -EFAULT;
598
599 return 0;
600}
601
602static int ioctl_remove_descriptor(struct client *client, void __user *arg)
603{
604 struct fw_cdev_remove_descriptor request;
605 struct descriptor *d;
606 unsigned long flags;
607
608 if (copy_from_user(&request, arg, sizeof request))
609 return -EFAULT;
610
611 spin_lock_irqsave(&client->lock, flags);
612 list_for_each_entry(d, &client->descriptor_list, link) {
613 if (d->handle == request.handle) {
614 list_del(&d->link);
615 break;
616 }
617 }
618 spin_unlock_irqrestore(&client->lock, flags);
619
620 if (&d->link == &client->descriptor_list)
621 return -EINVAL;
622
623 fw_core_remove_descriptor(&d->d);
624 kfree(d);
625
626 return 0;
627}
628
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500629static void
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500630iso_callback(struct fw_iso_context *context, u32 cycle,
631 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500632{
633 struct client *client = data;
634 struct iso_interrupt *interrupt;
635
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500636 interrupt = kzalloc(sizeof *interrupt + header_length, GFP_ATOMIC);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500637 if (interrupt == NULL)
638 return;
639
640 interrupt->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
641 interrupt->interrupt.closure = 0;
642 interrupt->interrupt.cycle = cycle;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500643 interrupt->interrupt.header_length = header_length;
644 memcpy(interrupt->interrupt.header, header, header_length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500645 queue_event(client, &interrupt->event,
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500646 &interrupt->interrupt,
647 sizeof interrupt->interrupt + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500648}
649
650static int ioctl_create_iso_context(struct client *client, void __user *arg)
651{
652 struct fw_cdev_create_iso_context request;
653
654 if (copy_from_user(&request, arg, sizeof request))
655 return -EFAULT;
656
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500657 if (request.channel > 63)
658 return -EINVAL;
659
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400660 switch (request.type) {
661 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400662 if (request.header_size < 4 || (request.header_size & 3))
663 return -EINVAL;
664
665 break;
666
667 case FW_ISO_CONTEXT_TRANSMIT:
668 if (request.speed > SCODE_3200)
669 return -EINVAL;
670
671 break;
672
673 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500674 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400675 }
676
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500677 client->iso_context = fw_iso_context_create(client->device->card,
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500678 request.type,
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500679 request.channel,
680 request.speed,
Kristian Høgsberg8fbdbb32007-03-07 12:12:51 -0500681 request.header_size,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500682 iso_callback, client);
683 if (IS_ERR(client->iso_context))
684 return PTR_ERR(client->iso_context);
685
686 return 0;
687}
688
689static int ioctl_queue_iso(struct client *client, void __user *arg)
690{
691 struct fw_cdev_queue_iso request;
692 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500693 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500694 unsigned long payload, payload_end, header_length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500695 int count;
696 struct {
697 struct fw_iso_packet packet;
698 u8 header[256];
699 } u;
700
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500701 if (ctx == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500702 return -EINVAL;
703 if (copy_from_user(&request, arg, sizeof request))
704 return -EFAULT;
705
706 /* If the user passes a non-NULL data pointer, has mmap()'ed
707 * the iso buffer, and the pointer points inside the buffer,
708 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500709 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500710 * payload_length == 0 through. In other words, if no packets
711 * use the indirect payload, the iso buffer need not be mapped
712 * and the request.data pointer is ignored.*/
713
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500714 payload = (unsigned long)request.data - client->vm_start;
715 payload_end = payload + (client->buffer.page_count << PAGE_SHIFT);
716 if (request.data == 0 || client->buffer.pages == NULL ||
717 payload >= payload_end) {
718 payload = 0;
719 payload_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500720 }
721
722 if (!access_ok(VERIFY_READ, request.packets, request.size))
723 return -EFAULT;
724
725 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request.packets);
726 end = (void __user *)p + request.size;
727 count = 0;
728 while (p < end) {
729 if (__copy_from_user(&u.packet, p, sizeof *p))
730 return -EFAULT;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500731
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500732 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500733 header_length = u.packet.header_length;
734 } else {
735 /* We require that header_length is a multiple of
736 * the fixed header size, ctx->header_size */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500737 if (ctx->header_size == 0) {
738 if (u.packet.header_length > 0)
739 return -EINVAL;
740 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500741 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500742 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500743 header_length = 0;
744 }
745
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500746 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500747 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500748 if (next > end)
749 return -EINVAL;
750 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500751 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500752 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500753 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500754 u.packet.header_length + u.packet.payload_length > 0)
755 return -EINVAL;
756 if (payload + u.packet.payload_length > payload_end)
757 return -EINVAL;
758
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500759 if (fw_iso_context_queue(ctx, &u.packet,
760 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500761 break;
762
763 p = next;
764 payload += u.packet.payload_length;
765 count++;
766 }
767
768 request.size -= uptr_to_u64(p) - request.packets;
769 request.packets = uptr_to_u64(p);
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500770 request.data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500771
772 if (copy_to_user(arg, &request, sizeof request))
773 return -EFAULT;
774
775 return count;
776}
777
Kristian Høgsberg69cdb722007-02-16 17:34:41 -0500778static int ioctl_start_iso(struct client *client, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500779{
Kristian Høgsberg69cdb722007-02-16 17:34:41 -0500780 struct fw_cdev_start_iso request;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500781
782 if (copy_from_user(&request, arg, sizeof request))
783 return -EFAULT;
784
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400785 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
786 if (request.tags == 0 || request.tags > 15)
787 return -EINVAL;
788
789 if (request.sync > 15)
790 return -EINVAL;
791 }
792
793 return fw_iso_context_start(client->iso_context,
794 request.cycle, request.sync, request.tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500795}
796
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500797static int ioctl_stop_iso(struct client *client, void __user *arg)
798{
799 return fw_iso_context_stop(client->iso_context);
800}
801
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500802static int
803dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
804{
805 switch (cmd) {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500806 case FW_CDEV_IOC_GET_INFO:
807 return ioctl_get_info(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500808 case FW_CDEV_IOC_SEND_REQUEST:
809 return ioctl_send_request(client, arg);
810 case FW_CDEV_IOC_ALLOCATE:
811 return ioctl_allocate(client, arg);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400812 case FW_CDEV_IOC_DEALLOCATE:
813 return ioctl_deallocate(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500814 case FW_CDEV_IOC_SEND_RESPONSE:
815 return ioctl_send_response(client, arg);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500816 case FW_CDEV_IOC_INITIATE_BUS_RESET:
817 return ioctl_initiate_bus_reset(client, arg);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200818 case FW_CDEV_IOC_ADD_DESCRIPTOR:
819 return ioctl_add_descriptor(client, arg);
820 case FW_CDEV_IOC_REMOVE_DESCRIPTOR:
821 return ioctl_remove_descriptor(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500822 case FW_CDEV_IOC_CREATE_ISO_CONTEXT:
823 return ioctl_create_iso_context(client, arg);
824 case FW_CDEV_IOC_QUEUE_ISO:
825 return ioctl_queue_iso(client, arg);
Kristian Høgsberg69cdb722007-02-16 17:34:41 -0500826 case FW_CDEV_IOC_START_ISO:
827 return ioctl_start_iso(client, arg);
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500828 case FW_CDEV_IOC_STOP_ISO:
829 return ioctl_stop_iso(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500830 default:
831 return -EINVAL;
832 }
833}
834
835static long
836fw_device_op_ioctl(struct file *file,
837 unsigned int cmd, unsigned long arg)
838{
839 struct client *client = file->private_data;
840
841 return dispatch_ioctl(client, cmd, (void __user *) arg);
842}
843
844#ifdef CONFIG_COMPAT
845static long
846fw_device_op_compat_ioctl(struct file *file,
847 unsigned int cmd, unsigned long arg)
848{
849 struct client *client = file->private_data;
850
851 return dispatch_ioctl(client, cmd, compat_ptr(arg));
852}
853#endif
854
855static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
856{
857 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500858 enum dma_data_direction direction;
859 unsigned long size;
860 int page_count, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500861
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500862 /* FIXME: We could support multiple buffers, but we don't. */
863 if (client->buffer.pages != NULL)
864 return -EBUSY;
865
866 if (!(vma->vm_flags & VM_SHARED))
867 return -EINVAL;
868
869 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500870 return -EINVAL;
871
872 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500873 size = vma->vm_end - vma->vm_start;
874 page_count = size >> PAGE_SHIFT;
875 if (size & ~PAGE_MASK)
876 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500877
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500878 if (vma->vm_flags & VM_WRITE)
879 direction = DMA_TO_DEVICE;
880 else
881 direction = DMA_FROM_DEVICE;
882
883 retval = fw_iso_buffer_init(&client->buffer, client->device->card,
884 page_count, direction);
885 if (retval < 0)
886 return retval;
887
888 retval = fw_iso_buffer_map(&client->buffer, vma);
889 if (retval < 0)
890 fw_iso_buffer_destroy(&client->buffer, client->device->card);
891
892 return retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500893}
894
895static int fw_device_op_release(struct inode *inode, struct file *file)
896{
897 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500898 struct address_handler *h, *next_h;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500899 struct request *r, *next_r;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500900 struct event *e, *next_e;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500901 struct response *t, *next_t;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200902 struct descriptor *d, *next_d;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500903 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500904
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500905 if (client->buffer.pages)
906 fw_iso_buffer_destroy(&client->buffer, client->device->card);
907
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500908 if (client->iso_context)
909 fw_iso_context_destroy(client->iso_context);
910
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500911 list_for_each_entry_safe(h, next_h, &client->handler_list, link) {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500912 fw_core_remove_address_handler(&h->handler);
913 kfree(h);
914 }
915
916 list_for_each_entry_safe(r, next_r, &client->request_list, link) {
917 fw_send_response(client->device->card, r->request,
918 RCODE_CONFLICT_ERROR);
919 kfree(r);
920 }
921
Kristian Høgsberg7e35f7f2007-03-14 17:34:57 -0400922 list_for_each_entry_safe(t, next_t, &client->transaction_list, link) {
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500923 fw_cancel_transaction(client->device->card, &t->transaction);
Kristian Høgsberg7e35f7f2007-03-14 17:34:57 -0400924 kfree(t);
925 }
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500926
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200927 list_for_each_entry_safe(d, next_d, &client->descriptor_list, link) {
928 fw_core_remove_descriptor(&d->d);
929 kfree(d);
930 }
931
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500932 /* FIXME: We should wait for the async tasklets to stop
933 * running before freeing the memory. */
934
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500935 list_for_each_entry_safe(e, next_e, &client->event_list, link)
936 kfree(e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500937
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500938 spin_lock_irqsave(&client->device->card->lock, flags);
939 list_del(&client->link);
940 spin_unlock_irqrestore(&client->device->card->lock, flags);
941
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500942 fw_device_put(client->device);
943 kfree(client);
944
945 return 0;
946}
947
948static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
949{
950 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500951 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500952
953 poll_wait(file, &client->wait, pt);
954
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500955 if (fw_device_is_shutdown(client->device))
956 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500957 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500958 mask |= POLLIN | POLLRDNORM;
959
960 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500961}
962
Stefan Richter21ebcd12007-01-14 15:29:07 +0100963const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500964 .owner = THIS_MODULE,
965 .open = fw_device_op_open,
966 .read = fw_device_op_read,
967 .unlocked_ioctl = fw_device_op_ioctl,
968 .poll = fw_device_op_poll,
969 .release = fw_device_op_release,
970 .mmap = fw_device_op_mmap,
971
972#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +0100973 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500974#endif
975};