blob: 2d84284f76058e959ff4236b673a366c41087b3f [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;
63 struct fw_cdev_event_response response;
64};
65
66struct iso_interrupt {
67 struct event event;
68 struct fw_cdev_event_iso_interrupt interrupt;
69};
70
71struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050072 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050073 struct fw_device *device;
74 spinlock_t lock;
75 struct list_head handler_list;
76 struct list_head request_list;
77 u32 request_serial;
78 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050079 wait_queue_head_t wait;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050080
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050081 struct fw_iso_context *iso_context;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050082 struct fw_iso_buffer buffer;
83 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050084
85 struct list_head link;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050086};
87
88static inline void __user *
89u64_to_uptr(__u64 value)
90{
91 return (void __user *)(unsigned long)value;
92}
93
94static inline __u64
95uptr_to_u64(void __user *ptr)
96{
97 return (__u64)(unsigned long)ptr;
98}
99
100static int fw_device_op_open(struct inode *inode, struct file *file)
101{
102 struct fw_device *device;
103 struct client *client;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500104 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500105
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500106 device = fw_device_from_devt(inode->i_rdev);
107 if (device == NULL)
108 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500109
110 client = kzalloc(sizeof *client, GFP_KERNEL);
111 if (client == NULL)
112 return -ENOMEM;
113
114 client->device = fw_device_get(device);
115 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500116 INIT_LIST_HEAD(&client->handler_list);
117 INIT_LIST_HEAD(&client->request_list);
118 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,
199 struct fw_device *device)
200{
201 struct fw_card *card = device->card;
202
203 event->type = FW_CDEV_EVENT_BUS_RESET;
204 event->node_id = device->node_id;
205 event->local_node_id = card->local_node->node_id;
206 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
207 event->irm_node_id = card->irm_node->node_id;
208 event->root_node_id = card->root_node->node_id;
209 event->generation = card->generation;
210}
211
212static void
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500213for_each_client(struct fw_device *device,
214 void (*callback)(struct client *client))
215{
216 struct fw_card *card = device->card;
217 struct client *c;
218 unsigned long flags;
219
220 spin_lock_irqsave(&card->lock, flags);
221
222 list_for_each_entry(c, &device->client_list, link)
223 callback(c);
224
225 spin_unlock_irqrestore(&card->lock, flags);
226}
227
228static void
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500229queue_bus_reset_event(struct client *client)
230{
231 struct bus_reset *bus_reset;
232 struct fw_device *device = client->device;
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øgsberg344bbc42007-03-07 12:12:43 -0500240 fill_bus_reset_event(&bus_reset->reset, device);
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);
274 size_t length = min(get_info.rom_length,
275 client->device->config_rom_length * 4);
276
277 if (copy_to_user(uptr, client->device->config_rom, length))
278 return -EFAULT;
279 }
280 get_info.rom_length = client->device->config_rom_length * 4;
281
282 if (get_info.bus_reset != 0) {
283 void __user *uptr = u64_to_uptr(get_info.bus_reset);
284
285 fill_bus_reset_event(&bus_reset, client->device);
286 if (copy_to_user(uptr, &bus_reset, sizeof bus_reset))
287 return -EFAULT;
288 }
289
290 if (copy_to_user(arg, &get_info, sizeof get_info))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500291 return -EFAULT;
292
293 return 0;
294}
295
296static void
297complete_transaction(struct fw_card *card, int rcode,
298 void *payload, size_t length, void *data)
299{
300 struct response *response = data;
301 struct client *client = response->client;
302
303 if (length < response->response.length)
304 response->response.length = length;
305 if (rcode == RCODE_COMPLETE)
306 memcpy(response->response.data, payload,
307 response->response.length);
308
309 response->response.type = FW_CDEV_EVENT_RESPONSE;
310 response->response.rcode = rcode;
311 queue_event(client, &response->event,
312 &response->response, sizeof response->response,
313 response->response.data, response->response.length);
314}
315
316static ssize_t ioctl_send_request(struct client *client, void __user *arg)
317{
318 struct fw_device *device = client->device;
319 struct fw_cdev_send_request request;
320 struct response *response;
321
322 if (copy_from_user(&request, arg, sizeof request))
323 return -EFAULT;
324
325 /* What is the biggest size we'll accept, really? */
326 if (request.length > 4096)
327 return -EINVAL;
328
329 response = kmalloc(sizeof *response + request.length, GFP_KERNEL);
330 if (response == NULL)
331 return -ENOMEM;
332
333 response->client = client;
334 response->response.length = request.length;
335 response->response.closure = request.closure;
336
337 if (request.data &&
338 copy_from_user(response->response.data,
339 u64_to_uptr(request.data), request.length)) {
340 kfree(response);
341 return -EFAULT;
342 }
343
344 fw_send_request(device->card, &response->transaction,
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500345 request.tcode & 0x1f,
Stefan Richter907293d2007-01-23 21:11:43 +0100346 device->node->node_id,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500347 device->card->generation,
348 device->node->max_speed,
349 request.offset,
350 response->response.data, request.length,
351 complete_transaction, response);
352
353 if (request.data)
354 return sizeof request + request.length;
355 else
356 return sizeof request;
357}
358
359struct address_handler {
360 struct fw_address_handler handler;
361 __u64 closure;
362 struct client *client;
363 struct list_head link;
364};
365
366struct request {
367 struct fw_request *request;
368 void *data;
369 size_t length;
370 u32 serial;
371 struct list_head link;
372};
373
374struct request_event {
375 struct event event;
376 struct fw_cdev_event_request request;
377};
378
379static void
380handle_request(struct fw_card *card, struct fw_request *r,
381 int tcode, int destination, int source,
382 int generation, int speed,
383 unsigned long long offset,
384 void *payload, size_t length, void *callback_data)
385{
386 struct address_handler *handler = callback_data;
387 struct request *request;
388 struct request_event *e;
389 unsigned long flags;
390 struct client *client = handler->client;
391
392 request = kmalloc(sizeof *request, GFP_ATOMIC);
393 e = kmalloc(sizeof *e, GFP_ATOMIC);
394 if (request == NULL || e == NULL) {
395 kfree(request);
396 kfree(e);
397 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
398 return;
399 }
400
401 request->request = r;
402 request->data = payload;
403 request->length = length;
404
405 spin_lock_irqsave(&client->lock, flags);
406 request->serial = client->request_serial++;
407 list_add_tail(&request->link, &client->request_list);
408 spin_unlock_irqrestore(&client->lock, flags);
409
410 e->request.type = FW_CDEV_EVENT_REQUEST;
411 e->request.tcode = tcode;
412 e->request.offset = offset;
413 e->request.length = length;
414 e->request.serial = request->serial;
415 e->request.closure = handler->closure;
416
417 queue_event(client, &e->event,
418 &e->request, sizeof e->request, payload, length);
419}
420
421static int ioctl_allocate(struct client *client, void __user *arg)
422{
423 struct fw_cdev_allocate request;
424 struct address_handler *handler;
425 unsigned long flags;
426 struct fw_address_region region;
427
428 if (copy_from_user(&request, arg, sizeof request))
429 return -EFAULT;
430
431 handler = kmalloc(sizeof *handler, GFP_KERNEL);
432 if (handler == NULL)
433 return -ENOMEM;
434
435 region.start = request.offset;
436 region.end = request.offset + request.length;
437 handler->handler.length = request.length;
438 handler->handler.address_callback = handle_request;
439 handler->handler.callback_data = handler;
440 handler->closure = request.closure;
441 handler->client = client;
442
443 if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
444 kfree(handler);
445 return -EBUSY;
446 }
447
448 spin_lock_irqsave(&client->lock, flags);
449 list_add_tail(&handler->link, &client->handler_list);
450 spin_unlock_irqrestore(&client->lock, flags);
451
452 return 0;
453}
454
455static int ioctl_send_response(struct client *client, void __user *arg)
456{
457 struct fw_cdev_send_response request;
458 struct request *r;
459 unsigned long flags;
460
461 if (copy_from_user(&request, arg, sizeof request))
462 return -EFAULT;
463
464 spin_lock_irqsave(&client->lock, flags);
465 list_for_each_entry(r, &client->request_list, link) {
466 if (r->serial == request.serial) {
467 list_del(&r->link);
468 break;
469 }
470 }
471 spin_unlock_irqrestore(&client->lock, flags);
472
473 if (&r->link == &client->request_list)
474 return -EINVAL;
475
476 if (request.length < r->length)
477 r->length = request.length;
478 if (copy_from_user(r->data, u64_to_uptr(request.data), r->length))
479 return -EFAULT;
480
481 fw_send_response(client->device->card, r->request, request.rcode);
482
483 kfree(r);
484
485 return 0;
486}
487
Kristian Høgsberg53718422007-03-07 12:12:42 -0500488static int ioctl_initiate_bus_reset(struct client *client, void __user *arg)
489{
490 struct fw_cdev_initiate_bus_reset request;
491 int short_reset;
492
493 if (copy_from_user(&request, arg, sizeof request))
494 return -EFAULT;
495
496 short_reset = (request.type == FW_CDEV_SHORT_RESET);
497
498 return fw_core_initiate_bus_reset(client->device->card, short_reset);
499}
500
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500501static void
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500502iso_callback(struct fw_iso_context *context, u32 cycle,
503 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500504{
505 struct client *client = data;
506 struct iso_interrupt *interrupt;
507
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500508 interrupt = kzalloc(sizeof *interrupt + header_length, GFP_ATOMIC);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500509 if (interrupt == NULL)
510 return;
511
512 interrupt->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
513 interrupt->interrupt.closure = 0;
514 interrupt->interrupt.cycle = cycle;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500515 interrupt->interrupt.header_length = header_length;
516 memcpy(interrupt->interrupt.header, header, header_length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500517 queue_event(client, &interrupt->event,
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500518 &interrupt->interrupt,
519 sizeof interrupt->interrupt + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500520}
521
522static int ioctl_create_iso_context(struct client *client, void __user *arg)
523{
524 struct fw_cdev_create_iso_context request;
525
526 if (copy_from_user(&request, arg, sizeof request))
527 return -EFAULT;
528
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500529 if (request.type > FW_ISO_CONTEXT_RECEIVE)
530 return -EINVAL;
531
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500532 if (request.channel > 63)
533 return -EINVAL;
534
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500535 if (request.sync > 15)
536 return -EINVAL;
537
538 if (request.tags == 0 || request.tags > 15)
539 return -EINVAL;
540
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500541 if (request.speed > SCODE_3200)
542 return -EINVAL;
543
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500544 client->iso_context = fw_iso_context_create(client->device->card,
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500545 request.type,
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500546 request.channel,
547 request.speed,
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500548 request.header_size,
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500549 request.sync,
550 request.tags,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500551 iso_callback, client);
552 if (IS_ERR(client->iso_context))
553 return PTR_ERR(client->iso_context);
554
555 return 0;
556}
557
558static int ioctl_queue_iso(struct client *client, void __user *arg)
559{
560 struct fw_cdev_queue_iso request;
561 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500562 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500563 unsigned long payload, payload_end, header_length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500564 int count;
565 struct {
566 struct fw_iso_packet packet;
567 u8 header[256];
568 } u;
569
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500570 if (ctx == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500571 return -EINVAL;
572 if (copy_from_user(&request, arg, sizeof request))
573 return -EFAULT;
574
575 /* If the user passes a non-NULL data pointer, has mmap()'ed
576 * the iso buffer, and the pointer points inside the buffer,
577 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500578 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500579 * payload_length == 0 through. In other words, if no packets
580 * use the indirect payload, the iso buffer need not be mapped
581 * and the request.data pointer is ignored.*/
582
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500583 payload = (unsigned long)request.data - client->vm_start;
584 payload_end = payload + (client->buffer.page_count << PAGE_SHIFT);
585 if (request.data == 0 || client->buffer.pages == NULL ||
586 payload >= payload_end) {
587 payload = 0;
588 payload_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500589 }
590
591 if (!access_ok(VERIFY_READ, request.packets, request.size))
592 return -EFAULT;
593
594 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request.packets);
595 end = (void __user *)p + request.size;
596 count = 0;
597 while (p < end) {
598 if (__copy_from_user(&u.packet, p, sizeof *p))
599 return -EFAULT;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500600
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500601 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500602 header_length = u.packet.header_length;
603 } else {
604 /* We require that header_length is a multiple of
605 * the fixed header size, ctx->header_size */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500606 if (ctx->header_size == 0) {
607 if (u.packet.header_length > 0)
608 return -EINVAL;
609 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500610 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500611 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500612 header_length = 0;
613 }
614
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500615 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500616 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500617 if (next > end)
618 return -EINVAL;
619 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500620 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500621 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500622 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500623 u.packet.header_length + u.packet.payload_length > 0)
624 return -EINVAL;
625 if (payload + u.packet.payload_length > payload_end)
626 return -EINVAL;
627
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500628 if (fw_iso_context_queue(ctx, &u.packet,
629 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500630 break;
631
632 p = next;
633 payload += u.packet.payload_length;
634 count++;
635 }
636
637 request.size -= uptr_to_u64(p) - request.packets;
638 request.packets = uptr_to_u64(p);
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500639 request.data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500640
641 if (copy_to_user(arg, &request, sizeof request))
642 return -EFAULT;
643
644 return count;
645}
646
Kristian Høgsberg69cdb722007-02-16 17:34:41 -0500647static int ioctl_start_iso(struct client *client, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500648{
Kristian Høgsberg69cdb722007-02-16 17:34:41 -0500649 struct fw_cdev_start_iso request;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500650
651 if (copy_from_user(&request, arg, sizeof request))
652 return -EFAULT;
653
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500654 return fw_iso_context_start(client->iso_context, request.cycle);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500655}
656
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500657static int ioctl_stop_iso(struct client *client, void __user *arg)
658{
659 return fw_iso_context_stop(client->iso_context);
660}
661
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500662static int
663dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
664{
665 switch (cmd) {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500666 case FW_CDEV_IOC_GET_INFO:
667 return ioctl_get_info(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500668 case FW_CDEV_IOC_SEND_REQUEST:
669 return ioctl_send_request(client, arg);
670 case FW_CDEV_IOC_ALLOCATE:
671 return ioctl_allocate(client, arg);
672 case FW_CDEV_IOC_SEND_RESPONSE:
673 return ioctl_send_response(client, arg);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500674 case FW_CDEV_IOC_INITIATE_BUS_RESET:
675 return ioctl_initiate_bus_reset(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500676 case FW_CDEV_IOC_CREATE_ISO_CONTEXT:
677 return ioctl_create_iso_context(client, arg);
678 case FW_CDEV_IOC_QUEUE_ISO:
679 return ioctl_queue_iso(client, arg);
Kristian Høgsberg69cdb722007-02-16 17:34:41 -0500680 case FW_CDEV_IOC_START_ISO:
681 return ioctl_start_iso(client, arg);
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500682 case FW_CDEV_IOC_STOP_ISO:
683 return ioctl_stop_iso(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500684 default:
685 return -EINVAL;
686 }
687}
688
689static long
690fw_device_op_ioctl(struct file *file,
691 unsigned int cmd, unsigned long arg)
692{
693 struct client *client = file->private_data;
694
695 return dispatch_ioctl(client, cmd, (void __user *) arg);
696}
697
698#ifdef CONFIG_COMPAT
699static long
700fw_device_op_compat_ioctl(struct file *file,
701 unsigned int cmd, unsigned long arg)
702{
703 struct client *client = file->private_data;
704
705 return dispatch_ioctl(client, cmd, compat_ptr(arg));
706}
707#endif
708
709static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
710{
711 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500712 enum dma_data_direction direction;
713 unsigned long size;
714 int page_count, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500715
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500716 /* FIXME: We could support multiple buffers, but we don't. */
717 if (client->buffer.pages != NULL)
718 return -EBUSY;
719
720 if (!(vma->vm_flags & VM_SHARED))
721 return -EINVAL;
722
723 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500724 return -EINVAL;
725
726 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500727 size = vma->vm_end - vma->vm_start;
728 page_count = size >> PAGE_SHIFT;
729 if (size & ~PAGE_MASK)
730 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500731
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500732 if (vma->vm_flags & VM_WRITE)
733 direction = DMA_TO_DEVICE;
734 else
735 direction = DMA_FROM_DEVICE;
736
737 retval = fw_iso_buffer_init(&client->buffer, client->device->card,
738 page_count, direction);
739 if (retval < 0)
740 return retval;
741
742 retval = fw_iso_buffer_map(&client->buffer, vma);
743 if (retval < 0)
744 fw_iso_buffer_destroy(&client->buffer, client->device->card);
745
746 return retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500747}
748
749static int fw_device_op_release(struct inode *inode, struct file *file)
750{
751 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500752 struct address_handler *h, *next_h;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500753 struct request *r, *next_r;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500754 struct event *e, *next_e;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500755 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500756
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500757 if (client->buffer.pages)
758 fw_iso_buffer_destroy(&client->buffer, client->device->card);
759
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500760 if (client->iso_context)
761 fw_iso_context_destroy(client->iso_context);
762
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500763 list_for_each_entry_safe(h, next_h, &client->handler_list, link) {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500764 fw_core_remove_address_handler(&h->handler);
765 kfree(h);
766 }
767
768 list_for_each_entry_safe(r, next_r, &client->request_list, link) {
769 fw_send_response(client->device->card, r->request,
770 RCODE_CONFLICT_ERROR);
771 kfree(r);
772 }
773
774 /* TODO: wait for all transactions to finish so
775 * complete_transaction doesn't try to queue up responses
776 * after we free client. */
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500777 list_for_each_entry_safe(e, next_e, &client->event_list, link)
778 kfree(e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500779
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500780 spin_lock_irqsave(&client->device->card->lock, flags);
781 list_del(&client->link);
782 spin_unlock_irqrestore(&client->device->card->lock, flags);
783
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500784 fw_device_put(client->device);
785 kfree(client);
786
787 return 0;
788}
789
790static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
791{
792 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500793 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500794
795 poll_wait(file, &client->wait, pt);
796
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500797 if (fw_device_is_shutdown(client->device))
798 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500799 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500800 mask |= POLLIN | POLLRDNORM;
801
802 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500803}
804
Stefan Richter21ebcd12007-01-14 15:29:07 +0100805const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500806 .owner = THIS_MODULE,
807 .open = fw_device_op_open,
808 .read = fw_device_op_read,
809 .unlocked_ioctl = fw_device_op_ioctl,
810 .poll = fw_device_op_poll,
811 .release = fw_device_op_release,
812 .mmap = fw_device_op_mmap,
813
814#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +0100815 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500816#endif
817};