blob: 12471444f1bd9feea4a5c112912d3b63bf98458c [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;
76 struct list_head handler_list;
77 struct list_head request_list;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -050078 struct list_head transaction_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050079 u32 request_serial;
80 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050081 wait_queue_head_t wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040082 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050083
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050084 struct fw_iso_context *iso_context;
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
113 client = kzalloc(sizeof *client, GFP_KERNEL);
114 if (client == NULL)
115 return -ENOMEM;
116
117 client->device = fw_device_get(device);
118 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500119 INIT_LIST_HEAD(&client->handler_list);
120 INIT_LIST_HEAD(&client->request_list);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500121 INIT_LIST_HEAD(&client->transaction_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500122 spin_lock_init(&client->lock);
123 init_waitqueue_head(&client->wait);
124
125 file->private_data = client;
126
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500127 spin_lock_irqsave(&device->card->lock, flags);
128 list_add_tail(&client->link, &device->client_list);
129 spin_unlock_irqrestore(&device->card->lock, flags);
130
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500131 return 0;
132}
133
134static void queue_event(struct client *client, struct event *event,
135 void *data0, size_t size0, void *data1, size_t size1)
136{
137 unsigned long flags;
138
139 event->v[0].data = data0;
140 event->v[0].size = size0;
141 event->v[1].data = data1;
142 event->v[1].size = size1;
143
144 spin_lock_irqsave(&client->lock, flags);
145
146 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500147 wake_up_interruptible(&client->wait);
148
149 spin_unlock_irqrestore(&client->lock, flags);
150}
151
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500152static int
153dequeue_event(struct client *client, char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500154{
155 unsigned long flags;
156 struct event *event;
157 size_t size, total;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500158 int i, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500159
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500160 retval = wait_event_interruptible(client->wait,
161 !list_empty(&client->event_list) ||
162 fw_device_is_shutdown(client->device));
163 if (retval < 0)
164 return retval;
165
166 if (list_empty(&client->event_list) &&
167 fw_device_is_shutdown(client->device))
168 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500169
170 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500171 event = container_of(client->event_list.next, struct event, link);
172 list_del(&event->link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500173 spin_unlock_irqrestore(&client->lock, flags);
174
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500175 total = 0;
176 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
177 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500178 if (copy_to_user(buffer + total, event->v[i].data, size)) {
179 retval = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500180 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500181 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500182 total += size;
183 }
184 retval = total;
185
186 out:
187 kfree(event);
188
189 return retval;
190}
191
192static ssize_t
193fw_device_op_read(struct file *file,
194 char __user *buffer, size_t count, loff_t *offset)
195{
196 struct client *client = file->private_data;
197
198 return dequeue_event(client, buffer, count);
199}
200
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500201static void
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500202fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400203 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500204{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400205 struct fw_card *card = client->device->card;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500206
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400207 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500208 event->type = FW_CDEV_EVENT_BUS_RESET;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400209 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500210 event->local_node_id = card->local_node->node_id;
211 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
212 event->irm_node_id = card->irm_node->node_id;
213 event->root_node_id = card->root_node->node_id;
214 event->generation = card->generation;
215}
216
217static void
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500218for_each_client(struct fw_device *device,
219 void (*callback)(struct client *client))
220{
221 struct fw_card *card = device->card;
222 struct client *c;
223 unsigned long flags;
224
225 spin_lock_irqsave(&card->lock, flags);
226
227 list_for_each_entry(c, &device->client_list, link)
228 callback(c);
229
230 spin_unlock_irqrestore(&card->lock, flags);
231}
232
233static void
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500234queue_bus_reset_event(struct client *client)
235{
236 struct bus_reset *bus_reset;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500237
238 bus_reset = kzalloc(sizeof *bus_reset, GFP_ATOMIC);
239 if (bus_reset == NULL) {
240 fw_notify("Out of memory when allocating bus reset event\n");
241 return;
242 }
243
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400244 fill_bus_reset_event(&bus_reset->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500245
246 queue_event(client, &bus_reset->event,
247 &bus_reset->reset, sizeof bus_reset->reset, NULL, 0);
248}
249
250void fw_device_cdev_update(struct fw_device *device)
251{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500252 for_each_client(device, queue_bus_reset_event);
253}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500254
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500255static void wake_up_client(struct client *client)
256{
257 wake_up_interruptible(&client->wait);
258}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500259
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500260void fw_device_cdev_remove(struct fw_device *device)
261{
262 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500263}
264
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500265static int ioctl_get_info(struct client *client, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500266{
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500267 struct fw_cdev_get_info get_info;
268 struct fw_cdev_event_bus_reset bus_reset;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500269
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500270 if (copy_from_user(&get_info, arg, sizeof get_info))
271 return -EFAULT;
272
273 client->version = get_info.version;
274 get_info.version = FW_CDEV_VERSION;
275
276 if (get_info.rom != 0) {
277 void __user *uptr = u64_to_uptr(get_info.rom);
Stefan Richterd84702a2007-03-20 19:42:15 +0100278 size_t want = get_info.rom_length;
279 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500280
Stefan Richterd84702a2007-03-20 19:42:15 +0100281 if (copy_to_user(uptr, client->device->config_rom,
282 min(want, have)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500283 return -EFAULT;
284 }
285 get_info.rom_length = client->device->config_rom_length * 4;
286
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400287 client->bus_reset_closure = get_info.bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500288 if (get_info.bus_reset != 0) {
289 void __user *uptr = u64_to_uptr(get_info.bus_reset);
290
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400291 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500292 if (copy_to_user(uptr, &bus_reset, sizeof bus_reset))
293 return -EFAULT;
294 }
295
Kristian Høgsberge7533502007-03-07 12:12:52 -0500296 get_info.card = client->device->card->index;
297
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500298 if (copy_to_user(arg, &get_info, sizeof get_info))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500299 return -EFAULT;
300
301 return 0;
302}
303
304static void
305complete_transaction(struct fw_card *card, int rcode,
306 void *payload, size_t length, void *data)
307{
308 struct response *response = data;
309 struct client *client = response->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500310 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500311
312 if (length < response->response.length)
313 response->response.length = length;
314 if (rcode == RCODE_COMPLETE)
315 memcpy(response->response.data, payload,
316 response->response.length);
317
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500318 spin_lock_irqsave(&client->lock, flags);
319 list_del(&response->link);
320 spin_unlock_irqrestore(&client->lock, flags);
321
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500322 response->response.type = FW_CDEV_EVENT_RESPONSE;
323 response->response.rcode = rcode;
324 queue_event(client, &response->event,
325 &response->response, sizeof response->response,
326 response->response.data, response->response.length);
327}
328
329static ssize_t ioctl_send_request(struct client *client, void __user *arg)
330{
331 struct fw_device *device = client->device;
332 struct fw_cdev_send_request request;
333 struct response *response;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500334 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500335
336 if (copy_from_user(&request, arg, sizeof request))
337 return -EFAULT;
338
339 /* What is the biggest size we'll accept, really? */
340 if (request.length > 4096)
341 return -EINVAL;
342
343 response = kmalloc(sizeof *response + request.length, GFP_KERNEL);
344 if (response == NULL)
345 return -ENOMEM;
346
347 response->client = client;
348 response->response.length = request.length;
349 response->response.closure = request.closure;
350
351 if (request.data &&
352 copy_from_user(response->response.data,
353 u64_to_uptr(request.data), request.length)) {
354 kfree(response);
355 return -EFAULT;
356 }
357
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500358 spin_lock_irqsave(&client->lock, flags);
359 list_add_tail(&response->link, &client->transaction_list);
360 spin_unlock_irqrestore(&client->lock, flags);
361
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500362 fw_send_request(device->card, &response->transaction,
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500363 request.tcode & 0x1f,
Stefan Richter907293d2007-01-23 21:11:43 +0100364 device->node->node_id,
Kristian Høgsberg97e35272007-03-07 12:12:53 -0500365 request.generation,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500366 device->node->max_speed,
367 request.offset,
368 response->response.data, request.length,
369 complete_transaction, response);
370
371 if (request.data)
372 return sizeof request + request.length;
373 else
374 return sizeof request;
375}
376
377struct address_handler {
378 struct fw_address_handler handler;
379 __u64 closure;
380 struct client *client;
381 struct list_head link;
382};
383
384struct request {
385 struct fw_request *request;
386 void *data;
387 size_t length;
388 u32 serial;
389 struct list_head link;
390};
391
392struct request_event {
393 struct event event;
394 struct fw_cdev_event_request request;
395};
396
397static void
398handle_request(struct fw_card *card, struct fw_request *r,
399 int tcode, int destination, int source,
400 int generation, int speed,
401 unsigned long long offset,
402 void *payload, size_t length, void *callback_data)
403{
404 struct address_handler *handler = callback_data;
405 struct request *request;
406 struct request_event *e;
407 unsigned long flags;
408 struct client *client = handler->client;
409
410 request = kmalloc(sizeof *request, GFP_ATOMIC);
411 e = kmalloc(sizeof *e, GFP_ATOMIC);
412 if (request == NULL || e == NULL) {
413 kfree(request);
414 kfree(e);
415 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
416 return;
417 }
418
419 request->request = r;
420 request->data = payload;
421 request->length = length;
422
423 spin_lock_irqsave(&client->lock, flags);
424 request->serial = client->request_serial++;
425 list_add_tail(&request->link, &client->request_list);
426 spin_unlock_irqrestore(&client->lock, flags);
427
428 e->request.type = FW_CDEV_EVENT_REQUEST;
429 e->request.tcode = tcode;
430 e->request.offset = offset;
431 e->request.length = length;
432 e->request.serial = request->serial;
433 e->request.closure = handler->closure;
434
435 queue_event(client, &e->event,
436 &e->request, sizeof e->request, payload, length);
437}
438
439static int ioctl_allocate(struct client *client, void __user *arg)
440{
441 struct fw_cdev_allocate request;
442 struct address_handler *handler;
443 unsigned long flags;
444 struct fw_address_region region;
445
446 if (copy_from_user(&request, arg, sizeof request))
447 return -EFAULT;
448
449 handler = kmalloc(sizeof *handler, GFP_KERNEL);
450 if (handler == NULL)
451 return -ENOMEM;
452
453 region.start = request.offset;
454 region.end = request.offset + request.length;
455 handler->handler.length = request.length;
456 handler->handler.address_callback = handle_request;
457 handler->handler.callback_data = handler;
458 handler->closure = request.closure;
459 handler->client = client;
460
461 if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
462 kfree(handler);
463 return -EBUSY;
464 }
465
466 spin_lock_irqsave(&client->lock, flags);
467 list_add_tail(&handler->link, &client->handler_list);
468 spin_unlock_irqrestore(&client->lock, flags);
469
470 return 0;
471}
472
Kristian Høgsberg9472316b2007-03-14 17:34:55 -0400473static int ioctl_deallocate(struct client *client, void __user *arg)
474{
475 struct fw_cdev_deallocate request;
476 struct address_handler *handler;
477 unsigned long flags;
478
479 if (copy_from_user(&request, arg, sizeof request))
480 return -EFAULT;
481
482 spin_lock_irqsave(&client->lock, flags);
483 list_for_each_entry(handler, &client->handler_list, link) {
484 if (handler->handler.offset == request.offset) {
485 list_del(&handler->link);
486 break;
487 }
488 }
489 spin_unlock_irqrestore(&client->lock, flags);
490
491 if (&handler->link == &client->handler_list)
492 return -EINVAL;
493
494 fw_core_remove_address_handler(&handler->handler);
495
496 return 0;
497}
498
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500499static int ioctl_send_response(struct client *client, void __user *arg)
500{
501 struct fw_cdev_send_response request;
502 struct request *r;
503 unsigned long flags;
504
505 if (copy_from_user(&request, arg, sizeof request))
506 return -EFAULT;
507
508 spin_lock_irqsave(&client->lock, flags);
509 list_for_each_entry(r, &client->request_list, link) {
510 if (r->serial == request.serial) {
511 list_del(&r->link);
512 break;
513 }
514 }
515 spin_unlock_irqrestore(&client->lock, flags);
516
517 if (&r->link == &client->request_list)
518 return -EINVAL;
519
520 if (request.length < r->length)
521 r->length = request.length;
522 if (copy_from_user(r->data, u64_to_uptr(request.data), r->length))
523 return -EFAULT;
524
525 fw_send_response(client->device->card, r->request, request.rcode);
526
527 kfree(r);
528
529 return 0;
530}
531
Kristian Høgsberg53718422007-03-07 12:12:42 -0500532static int ioctl_initiate_bus_reset(struct client *client, void __user *arg)
533{
534 struct fw_cdev_initiate_bus_reset request;
535 int short_reset;
536
537 if (copy_from_user(&request, arg, sizeof request))
538 return -EFAULT;
539
540 short_reset = (request.type == FW_CDEV_SHORT_RESET);
541
542 return fw_core_initiate_bus_reset(client->device->card, short_reset);
543}
544
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500545static void
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500546iso_callback(struct fw_iso_context *context, u32 cycle,
547 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500548{
549 struct client *client = data;
550 struct iso_interrupt *interrupt;
551
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500552 interrupt = kzalloc(sizeof *interrupt + header_length, GFP_ATOMIC);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500553 if (interrupt == NULL)
554 return;
555
556 interrupt->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
557 interrupt->interrupt.closure = 0;
558 interrupt->interrupt.cycle = cycle;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500559 interrupt->interrupt.header_length = header_length;
560 memcpy(interrupt->interrupt.header, header, header_length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500561 queue_event(client, &interrupt->event,
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500562 &interrupt->interrupt,
563 sizeof interrupt->interrupt + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500564}
565
566static int ioctl_create_iso_context(struct client *client, void __user *arg)
567{
568 struct fw_cdev_create_iso_context request;
569
570 if (copy_from_user(&request, arg, sizeof request))
571 return -EFAULT;
572
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500573 if (request.channel > 63)
574 return -EINVAL;
575
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400576 switch (request.type) {
577 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400578 if (request.header_size < 4 || (request.header_size & 3))
579 return -EINVAL;
580
581 break;
582
583 case FW_ISO_CONTEXT_TRANSMIT:
584 if (request.speed > SCODE_3200)
585 return -EINVAL;
586
587 break;
588
589 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500590 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400591 }
592
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500593 client->iso_context = fw_iso_context_create(client->device->card,
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500594 request.type,
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500595 request.channel,
596 request.speed,
Kristian Høgsberg8fbdbb32007-03-07 12:12:51 -0500597 request.header_size,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500598 iso_callback, client);
599 if (IS_ERR(client->iso_context))
600 return PTR_ERR(client->iso_context);
601
602 return 0;
603}
604
605static int ioctl_queue_iso(struct client *client, void __user *arg)
606{
607 struct fw_cdev_queue_iso request;
608 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500609 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500610 unsigned long payload, payload_end, header_length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500611 int count;
612 struct {
613 struct fw_iso_packet packet;
614 u8 header[256];
615 } u;
616
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500617 if (ctx == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500618 return -EINVAL;
619 if (copy_from_user(&request, arg, sizeof request))
620 return -EFAULT;
621
622 /* If the user passes a non-NULL data pointer, has mmap()'ed
623 * the iso buffer, and the pointer points inside the buffer,
624 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500625 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500626 * payload_length == 0 through. In other words, if no packets
627 * use the indirect payload, the iso buffer need not be mapped
628 * and the request.data pointer is ignored.*/
629
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500630 payload = (unsigned long)request.data - client->vm_start;
631 payload_end = payload + (client->buffer.page_count << PAGE_SHIFT);
632 if (request.data == 0 || client->buffer.pages == NULL ||
633 payload >= payload_end) {
634 payload = 0;
635 payload_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500636 }
637
638 if (!access_ok(VERIFY_READ, request.packets, request.size))
639 return -EFAULT;
640
641 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request.packets);
642 end = (void __user *)p + request.size;
643 count = 0;
644 while (p < end) {
645 if (__copy_from_user(&u.packet, p, sizeof *p))
646 return -EFAULT;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500647
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500648 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500649 header_length = u.packet.header_length;
650 } else {
651 /* We require that header_length is a multiple of
652 * the fixed header size, ctx->header_size */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500653 if (ctx->header_size == 0) {
654 if (u.packet.header_length > 0)
655 return -EINVAL;
656 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500657 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500658 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500659 header_length = 0;
660 }
661
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500662 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500663 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500664 if (next > end)
665 return -EINVAL;
666 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500667 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500668 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500669 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500670 u.packet.header_length + u.packet.payload_length > 0)
671 return -EINVAL;
672 if (payload + u.packet.payload_length > payload_end)
673 return -EINVAL;
674
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500675 if (fw_iso_context_queue(ctx, &u.packet,
676 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500677 break;
678
679 p = next;
680 payload += u.packet.payload_length;
681 count++;
682 }
683
684 request.size -= uptr_to_u64(p) - request.packets;
685 request.packets = uptr_to_u64(p);
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500686 request.data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500687
688 if (copy_to_user(arg, &request, sizeof request))
689 return -EFAULT;
690
691 return count;
692}
693
Kristian Høgsberg69cdb722007-02-16 17:34:41 -0500694static int ioctl_start_iso(struct client *client, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500695{
Kristian Høgsberg69cdb722007-02-16 17:34:41 -0500696 struct fw_cdev_start_iso request;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500697
698 if (copy_from_user(&request, arg, sizeof request))
699 return -EFAULT;
700
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400701 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
702 if (request.tags == 0 || request.tags > 15)
703 return -EINVAL;
704
705 if (request.sync > 15)
706 return -EINVAL;
707 }
708
709 return fw_iso_context_start(client->iso_context,
710 request.cycle, request.sync, request.tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500711}
712
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500713static int ioctl_stop_iso(struct client *client, void __user *arg)
714{
715 return fw_iso_context_stop(client->iso_context);
716}
717
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500718static int
719dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
720{
721 switch (cmd) {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500722 case FW_CDEV_IOC_GET_INFO:
723 return ioctl_get_info(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500724 case FW_CDEV_IOC_SEND_REQUEST:
725 return ioctl_send_request(client, arg);
726 case FW_CDEV_IOC_ALLOCATE:
727 return ioctl_allocate(client, arg);
Kristian Høgsberg9472316b2007-03-14 17:34:55 -0400728 case FW_CDEV_IOC_DEALLOCATE:
729 return ioctl_deallocate(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500730 case FW_CDEV_IOC_SEND_RESPONSE:
731 return ioctl_send_response(client, arg);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500732 case FW_CDEV_IOC_INITIATE_BUS_RESET:
733 return ioctl_initiate_bus_reset(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500734 case FW_CDEV_IOC_CREATE_ISO_CONTEXT:
735 return ioctl_create_iso_context(client, arg);
736 case FW_CDEV_IOC_QUEUE_ISO:
737 return ioctl_queue_iso(client, arg);
Kristian Høgsberg69cdb722007-02-16 17:34:41 -0500738 case FW_CDEV_IOC_START_ISO:
739 return ioctl_start_iso(client, arg);
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500740 case FW_CDEV_IOC_STOP_ISO:
741 return ioctl_stop_iso(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500742 default:
743 return -EINVAL;
744 }
745}
746
747static long
748fw_device_op_ioctl(struct file *file,
749 unsigned int cmd, unsigned long arg)
750{
751 struct client *client = file->private_data;
752
753 return dispatch_ioctl(client, cmd, (void __user *) arg);
754}
755
756#ifdef CONFIG_COMPAT
757static long
758fw_device_op_compat_ioctl(struct file *file,
759 unsigned int cmd, unsigned long arg)
760{
761 struct client *client = file->private_data;
762
763 return dispatch_ioctl(client, cmd, compat_ptr(arg));
764}
765#endif
766
767static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
768{
769 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500770 enum dma_data_direction direction;
771 unsigned long size;
772 int page_count, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500773
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500774 /* FIXME: We could support multiple buffers, but we don't. */
775 if (client->buffer.pages != NULL)
776 return -EBUSY;
777
778 if (!(vma->vm_flags & VM_SHARED))
779 return -EINVAL;
780
781 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500782 return -EINVAL;
783
784 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500785 size = vma->vm_end - vma->vm_start;
786 page_count = size >> PAGE_SHIFT;
787 if (size & ~PAGE_MASK)
788 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500789
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500790 if (vma->vm_flags & VM_WRITE)
791 direction = DMA_TO_DEVICE;
792 else
793 direction = DMA_FROM_DEVICE;
794
795 retval = fw_iso_buffer_init(&client->buffer, client->device->card,
796 page_count, direction);
797 if (retval < 0)
798 return retval;
799
800 retval = fw_iso_buffer_map(&client->buffer, vma);
801 if (retval < 0)
802 fw_iso_buffer_destroy(&client->buffer, client->device->card);
803
804 return retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500805}
806
807static int fw_device_op_release(struct inode *inode, struct file *file)
808{
809 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500810 struct address_handler *h, *next_h;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500811 struct request *r, *next_r;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500812 struct event *e, *next_e;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500813 struct response *t, *next_t;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500814 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500815
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500816 if (client->buffer.pages)
817 fw_iso_buffer_destroy(&client->buffer, client->device->card);
818
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500819 if (client->iso_context)
820 fw_iso_context_destroy(client->iso_context);
821
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500822 list_for_each_entry_safe(h, next_h, &client->handler_list, link) {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500823 fw_core_remove_address_handler(&h->handler);
824 kfree(h);
825 }
826
827 list_for_each_entry_safe(r, next_r, &client->request_list, link) {
828 fw_send_response(client->device->card, r->request,
829 RCODE_CONFLICT_ERROR);
830 kfree(r);
831 }
832
Kristian Høgsberg7e35f7f2007-03-14 17:34:57 -0400833 list_for_each_entry_safe(t, next_t, &client->transaction_list, link) {
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500834 fw_cancel_transaction(client->device->card, &t->transaction);
Kristian Høgsberg7e35f7f2007-03-14 17:34:57 -0400835 kfree(t);
836 }
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500837
838 /* FIXME: We should wait for the async tasklets to stop
839 * running before freeing the memory. */
840
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500841 list_for_each_entry_safe(e, next_e, &client->event_list, link)
842 kfree(e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500843
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500844 spin_lock_irqsave(&client->device->card->lock, flags);
845 list_del(&client->link);
846 spin_unlock_irqrestore(&client->device->card->lock, flags);
847
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500848 fw_device_put(client->device);
849 kfree(client);
850
851 return 0;
852}
853
854static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
855{
856 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500857 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500858
859 poll_wait(file, &client->wait, pt);
860
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500861 if (fw_device_is_shutdown(client->device))
862 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500863 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500864 mask |= POLLIN | POLLRDNORM;
865
866 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500867}
868
Stefan Richter21ebcd12007-01-14 15:29:07 +0100869const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500870 .owner = THIS_MODULE,
871 .open = fw_device_op_open,
872 .read = fw_device_op_read,
873 .unlocked_ioctl = fw_device_op_ioctl,
874 .poll = fw_device_op_poll,
875 .release = fw_device_op_release,
876 .mmap = fw_device_op_mmap,
877
878#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +0100879 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500880#endif
881};