blob: d9f3bb2be1c2a55155c147cfae367a1e9974496e [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>
31#include <linux/compat.h>
32#include <asm/uaccess.h>
33#include "fw-transaction.h"
34#include "fw-topology.h"
35#include "fw-device.h"
36#include "fw-device-cdev.h"
37
38/*
39 * todo
40 *
41 * - bus resets sends a new packet with new generation and node id
42 *
43 */
44
45/* dequeue_event() just kfree()'s the event, so the event has to be
46 * the first field in the struct. */
47
48struct event {
49 struct { void *data; size_t size; } v[2];
50 struct list_head link;
51};
52
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050053struct bus_reset {
54 struct event event;
55 struct fw_cdev_event_bus_reset reset;
56};
57
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050058struct response {
59 struct event event;
60 struct fw_transaction transaction;
61 struct client *client;
62 struct fw_cdev_event_response response;
63};
64
65struct iso_interrupt {
66 struct event event;
67 struct fw_cdev_event_iso_interrupt interrupt;
68};
69
70struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050071 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050072 struct fw_device *device;
73 spinlock_t lock;
74 struct list_head handler_list;
75 struct list_head request_list;
76 u32 request_serial;
77 struct list_head event_list;
78 struct semaphore event_list_sem;
79 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
106 device = container_of(inode->i_cdev, struct fw_device, cdev);
107
108 client = kzalloc(sizeof *client, GFP_KERNEL);
109 if (client == NULL)
110 return -ENOMEM;
111
112 client->device = fw_device_get(device);
113 INIT_LIST_HEAD(&client->event_list);
114 sema_init(&client->event_list_sem, 0);
115 INIT_LIST_HEAD(&client->handler_list);
116 INIT_LIST_HEAD(&client->request_list);
117 spin_lock_init(&client->lock);
118 init_waitqueue_head(&client->wait);
119
120 file->private_data = client;
121
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500122 spin_lock_irqsave(&device->card->lock, flags);
123 list_add_tail(&client->link, &device->client_list);
124 spin_unlock_irqrestore(&device->card->lock, flags);
125
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500126 return 0;
127}
128
129static void queue_event(struct client *client, struct event *event,
130 void *data0, size_t size0, void *data1, size_t size1)
131{
132 unsigned long flags;
133
134 event->v[0].data = data0;
135 event->v[0].size = size0;
136 event->v[1].data = data1;
137 event->v[1].size = size1;
138
139 spin_lock_irqsave(&client->lock, flags);
140
141 list_add_tail(&event->link, &client->event_list);
142
143 up(&client->event_list_sem);
144 wake_up_interruptible(&client->wait);
145
146 spin_unlock_irqrestore(&client->lock, flags);
147}
148
149static int dequeue_event(struct client *client, char __user *buffer, size_t count)
150{
151 unsigned long flags;
152 struct event *event;
153 size_t size, total;
154 int i, retval = -EFAULT;
155
156 if (down_interruptible(&client->event_list_sem) < 0)
157 return -EINTR;
158
159 spin_lock_irqsave(&client->lock, flags);
160
161 event = container_of(client->event_list.next, struct event, link);
162 list_del(&event->link);
163
164 spin_unlock_irqrestore(&client->lock, flags);
165
166 if (buffer == NULL)
167 goto out;
168
169 total = 0;
170 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
171 size = min(event->v[i].size, count - total);
172 if (copy_to_user(buffer + total, event->v[i].data, size))
173 goto out;
174 total += size;
175 }
176 retval = total;
177
178 out:
179 kfree(event);
180
181 return retval;
182}
183
184static ssize_t
185fw_device_op_read(struct file *file,
186 char __user *buffer, size_t count, loff_t *offset)
187{
188 struct client *client = file->private_data;
189
190 return dequeue_event(client, buffer, count);
191}
192
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500193static void
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500194fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
195 struct fw_device *device)
196{
197 struct fw_card *card = device->card;
198
199 event->type = FW_CDEV_EVENT_BUS_RESET;
200 event->node_id = device->node_id;
201 event->local_node_id = card->local_node->node_id;
202 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
203 event->irm_node_id = card->irm_node->node_id;
204 event->root_node_id = card->root_node->node_id;
205 event->generation = card->generation;
206}
207
208static void
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500209queue_bus_reset_event(struct client *client)
210{
211 struct bus_reset *bus_reset;
212 struct fw_device *device = client->device;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500213
214 bus_reset = kzalloc(sizeof *bus_reset, GFP_ATOMIC);
215 if (bus_reset == NULL) {
216 fw_notify("Out of memory when allocating bus reset event\n");
217 return;
218 }
219
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500220 fill_bus_reset_event(&bus_reset->reset, device);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500221
222 queue_event(client, &bus_reset->event,
223 &bus_reset->reset, sizeof bus_reset->reset, NULL, 0);
224}
225
226void fw_device_cdev_update(struct fw_device *device)
227{
228 struct fw_card *card = device->card;
229 struct client *c;
230 unsigned long flags;
231
232 spin_lock_irqsave(&card->lock, flags);
233
234 list_for_each_entry(c, &device->client_list, link)
235 queue_bus_reset_event(c);
236
237 spin_unlock_irqrestore(&card->lock, flags);
238}
239
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500240static int ioctl_get_info(struct client *client, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500241{
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500242 struct fw_cdev_get_info get_info;
243 struct fw_cdev_event_bus_reset bus_reset;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500244
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500245 if (copy_from_user(&get_info, arg, sizeof get_info))
246 return -EFAULT;
247
248 client->version = get_info.version;
249 get_info.version = FW_CDEV_VERSION;
250
251 if (get_info.rom != 0) {
252 void __user *uptr = u64_to_uptr(get_info.rom);
253 size_t length = min(get_info.rom_length,
254 client->device->config_rom_length * 4);
255
256 if (copy_to_user(uptr, client->device->config_rom, length))
257 return -EFAULT;
258 }
259 get_info.rom_length = client->device->config_rom_length * 4;
260
261 if (get_info.bus_reset != 0) {
262 void __user *uptr = u64_to_uptr(get_info.bus_reset);
263
264 fill_bus_reset_event(&bus_reset, client->device);
265 if (copy_to_user(uptr, &bus_reset, sizeof bus_reset))
266 return -EFAULT;
267 }
268
269 if (copy_to_user(arg, &get_info, sizeof get_info))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500270 return -EFAULT;
271
272 return 0;
273}
274
275static void
276complete_transaction(struct fw_card *card, int rcode,
277 void *payload, size_t length, void *data)
278{
279 struct response *response = data;
280 struct client *client = response->client;
281
282 if (length < response->response.length)
283 response->response.length = length;
284 if (rcode == RCODE_COMPLETE)
285 memcpy(response->response.data, payload,
286 response->response.length);
287
288 response->response.type = FW_CDEV_EVENT_RESPONSE;
289 response->response.rcode = rcode;
290 queue_event(client, &response->event,
291 &response->response, sizeof response->response,
292 response->response.data, response->response.length);
293}
294
295static ssize_t ioctl_send_request(struct client *client, void __user *arg)
296{
297 struct fw_device *device = client->device;
298 struct fw_cdev_send_request request;
299 struct response *response;
300
301 if (copy_from_user(&request, arg, sizeof request))
302 return -EFAULT;
303
304 /* What is the biggest size we'll accept, really? */
305 if (request.length > 4096)
306 return -EINVAL;
307
308 response = kmalloc(sizeof *response + request.length, GFP_KERNEL);
309 if (response == NULL)
310 return -ENOMEM;
311
312 response->client = client;
313 response->response.length = request.length;
314 response->response.closure = request.closure;
315
316 if (request.data &&
317 copy_from_user(response->response.data,
318 u64_to_uptr(request.data), request.length)) {
319 kfree(response);
320 return -EFAULT;
321 }
322
323 fw_send_request(device->card, &response->transaction,
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500324 request.tcode & 0x1f,
Stefan Richter907293d2007-01-23 21:11:43 +0100325 device->node->node_id,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500326 device->card->generation,
327 device->node->max_speed,
328 request.offset,
329 response->response.data, request.length,
330 complete_transaction, response);
331
332 if (request.data)
333 return sizeof request + request.length;
334 else
335 return sizeof request;
336}
337
338struct address_handler {
339 struct fw_address_handler handler;
340 __u64 closure;
341 struct client *client;
342 struct list_head link;
343};
344
345struct request {
346 struct fw_request *request;
347 void *data;
348 size_t length;
349 u32 serial;
350 struct list_head link;
351};
352
353struct request_event {
354 struct event event;
355 struct fw_cdev_event_request request;
356};
357
358static void
359handle_request(struct fw_card *card, struct fw_request *r,
360 int tcode, int destination, int source,
361 int generation, int speed,
362 unsigned long long offset,
363 void *payload, size_t length, void *callback_data)
364{
365 struct address_handler *handler = callback_data;
366 struct request *request;
367 struct request_event *e;
368 unsigned long flags;
369 struct client *client = handler->client;
370
371 request = kmalloc(sizeof *request, GFP_ATOMIC);
372 e = kmalloc(sizeof *e, GFP_ATOMIC);
373 if (request == NULL || e == NULL) {
374 kfree(request);
375 kfree(e);
376 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
377 return;
378 }
379
380 request->request = r;
381 request->data = payload;
382 request->length = length;
383
384 spin_lock_irqsave(&client->lock, flags);
385 request->serial = client->request_serial++;
386 list_add_tail(&request->link, &client->request_list);
387 spin_unlock_irqrestore(&client->lock, flags);
388
389 e->request.type = FW_CDEV_EVENT_REQUEST;
390 e->request.tcode = tcode;
391 e->request.offset = offset;
392 e->request.length = length;
393 e->request.serial = request->serial;
394 e->request.closure = handler->closure;
395
396 queue_event(client, &e->event,
397 &e->request, sizeof e->request, payload, length);
398}
399
400static int ioctl_allocate(struct client *client, void __user *arg)
401{
402 struct fw_cdev_allocate request;
403 struct address_handler *handler;
404 unsigned long flags;
405 struct fw_address_region region;
406
407 if (copy_from_user(&request, arg, sizeof request))
408 return -EFAULT;
409
410 handler = kmalloc(sizeof *handler, GFP_KERNEL);
411 if (handler == NULL)
412 return -ENOMEM;
413
414 region.start = request.offset;
415 region.end = request.offset + request.length;
416 handler->handler.length = request.length;
417 handler->handler.address_callback = handle_request;
418 handler->handler.callback_data = handler;
419 handler->closure = request.closure;
420 handler->client = client;
421
422 if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
423 kfree(handler);
424 return -EBUSY;
425 }
426
427 spin_lock_irqsave(&client->lock, flags);
428 list_add_tail(&handler->link, &client->handler_list);
429 spin_unlock_irqrestore(&client->lock, flags);
430
431 return 0;
432}
433
434static int ioctl_send_response(struct client *client, void __user *arg)
435{
436 struct fw_cdev_send_response request;
437 struct request *r;
438 unsigned long flags;
439
440 if (copy_from_user(&request, arg, sizeof request))
441 return -EFAULT;
442
443 spin_lock_irqsave(&client->lock, flags);
444 list_for_each_entry(r, &client->request_list, link) {
445 if (r->serial == request.serial) {
446 list_del(&r->link);
447 break;
448 }
449 }
450 spin_unlock_irqrestore(&client->lock, flags);
451
452 if (&r->link == &client->request_list)
453 return -EINVAL;
454
455 if (request.length < r->length)
456 r->length = request.length;
457 if (copy_from_user(r->data, u64_to_uptr(request.data), r->length))
458 return -EFAULT;
459
460 fw_send_response(client->device->card, r->request, request.rcode);
461
462 kfree(r);
463
464 return 0;
465}
466
Kristian Høgsberg53718422007-03-07 12:12:42 -0500467static int ioctl_initiate_bus_reset(struct client *client, void __user *arg)
468{
469 struct fw_cdev_initiate_bus_reset request;
470 int short_reset;
471
472 if (copy_from_user(&request, arg, sizeof request))
473 return -EFAULT;
474
475 short_reset = (request.type == FW_CDEV_SHORT_RESET);
476
477 return fw_core_initiate_bus_reset(client->device->card, short_reset);
478}
479
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500480static void
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500481iso_callback(struct fw_iso_context *context, u32 cycle,
482 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500483{
484 struct client *client = data;
485 struct iso_interrupt *interrupt;
486
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500487 interrupt = kzalloc(sizeof *interrupt + header_length, GFP_ATOMIC);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500488 if (interrupt == NULL)
489 return;
490
491 interrupt->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
492 interrupt->interrupt.closure = 0;
493 interrupt->interrupt.cycle = cycle;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500494 interrupt->interrupt.header_length = header_length;
495 memcpy(interrupt->interrupt.header, header, header_length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500496 queue_event(client, &interrupt->event,
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500497 &interrupt->interrupt,
498 sizeof interrupt->interrupt + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500499}
500
501static int ioctl_create_iso_context(struct client *client, void __user *arg)
502{
503 struct fw_cdev_create_iso_context request;
504
505 if (copy_from_user(&request, arg, sizeof request))
506 return -EFAULT;
507
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500508 if (request.type > FW_ISO_CONTEXT_RECEIVE)
509 return -EINVAL;
510
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500511 if (request.channel > 63)
512 return -EINVAL;
513
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500514 if (request.sync > 15)
515 return -EINVAL;
516
517 if (request.tags == 0 || request.tags > 15)
518 return -EINVAL;
519
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500520 if (request.speed > SCODE_3200)
521 return -EINVAL;
522
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500523 client->iso_context = fw_iso_context_create(client->device->card,
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500524 request.type,
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500525 request.channel,
526 request.speed,
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500527 request.header_size,
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500528 request.sync,
529 request.tags,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500530 iso_callback, client);
531 if (IS_ERR(client->iso_context))
532 return PTR_ERR(client->iso_context);
533
534 return 0;
535}
536
537static int ioctl_queue_iso(struct client *client, void __user *arg)
538{
539 struct fw_cdev_queue_iso request;
540 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500541 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500542 unsigned long payload, payload_end, header_length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500543 int count;
544 struct {
545 struct fw_iso_packet packet;
546 u8 header[256];
547 } u;
548
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500549 if (ctx == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500550 return -EINVAL;
551 if (copy_from_user(&request, arg, sizeof request))
552 return -EFAULT;
553
554 /* If the user passes a non-NULL data pointer, has mmap()'ed
555 * the iso buffer, and the pointer points inside the buffer,
556 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500557 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500558 * payload_length == 0 through. In other words, if no packets
559 * use the indirect payload, the iso buffer need not be mapped
560 * and the request.data pointer is ignored.*/
561
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500562 payload = (unsigned long)request.data - client->vm_start;
563 payload_end = payload + (client->buffer.page_count << PAGE_SHIFT);
564 if (request.data == 0 || client->buffer.pages == NULL ||
565 payload >= payload_end) {
566 payload = 0;
567 payload_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500568 }
569
570 if (!access_ok(VERIFY_READ, request.packets, request.size))
571 return -EFAULT;
572
573 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request.packets);
574 end = (void __user *)p + request.size;
575 count = 0;
576 while (p < end) {
577 if (__copy_from_user(&u.packet, p, sizeof *p))
578 return -EFAULT;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500579
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500580 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500581 header_length = u.packet.header_length;
582 } else {
583 /* We require that header_length is a multiple of
584 * the fixed header size, ctx->header_size */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500585 if (ctx->header_size == 0) {
586 if (u.packet.header_length > 0)
587 return -EINVAL;
588 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500589 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500590 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500591 header_length = 0;
592 }
593
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500594 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500595 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500596 if (next > end)
597 return -EINVAL;
598 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500599 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500600 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500601 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500602 u.packet.header_length + u.packet.payload_length > 0)
603 return -EINVAL;
604 if (payload + u.packet.payload_length > payload_end)
605 return -EINVAL;
606
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500607 if (fw_iso_context_queue(ctx, &u.packet,
608 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500609 break;
610
611 p = next;
612 payload += u.packet.payload_length;
613 count++;
614 }
615
616 request.size -= uptr_to_u64(p) - request.packets;
617 request.packets = uptr_to_u64(p);
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500618 request.data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500619
620 if (copy_to_user(arg, &request, sizeof request))
621 return -EFAULT;
622
623 return count;
624}
625
Kristian Høgsberg69cdb722007-02-16 17:34:41 -0500626static int ioctl_start_iso(struct client *client, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500627{
Kristian Høgsberg69cdb722007-02-16 17:34:41 -0500628 struct fw_cdev_start_iso request;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500629
630 if (copy_from_user(&request, arg, sizeof request))
631 return -EFAULT;
632
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500633 return fw_iso_context_start(client->iso_context, request.cycle);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500634}
635
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500636static int ioctl_stop_iso(struct client *client, void __user *arg)
637{
638 return fw_iso_context_stop(client->iso_context);
639}
640
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500641static int
642dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
643{
644 switch (cmd) {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500645 case FW_CDEV_IOC_GET_INFO:
646 return ioctl_get_info(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500647 case FW_CDEV_IOC_SEND_REQUEST:
648 return ioctl_send_request(client, arg);
649 case FW_CDEV_IOC_ALLOCATE:
650 return ioctl_allocate(client, arg);
651 case FW_CDEV_IOC_SEND_RESPONSE:
652 return ioctl_send_response(client, arg);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500653 case FW_CDEV_IOC_INITIATE_BUS_RESET:
654 return ioctl_initiate_bus_reset(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500655 case FW_CDEV_IOC_CREATE_ISO_CONTEXT:
656 return ioctl_create_iso_context(client, arg);
657 case FW_CDEV_IOC_QUEUE_ISO:
658 return ioctl_queue_iso(client, arg);
Kristian Høgsberg69cdb722007-02-16 17:34:41 -0500659 case FW_CDEV_IOC_START_ISO:
660 return ioctl_start_iso(client, arg);
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500661 case FW_CDEV_IOC_STOP_ISO:
662 return ioctl_stop_iso(client, arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500663 default:
664 return -EINVAL;
665 }
666}
667
668static long
669fw_device_op_ioctl(struct file *file,
670 unsigned int cmd, unsigned long arg)
671{
672 struct client *client = file->private_data;
673
674 return dispatch_ioctl(client, cmd, (void __user *) arg);
675}
676
677#ifdef CONFIG_COMPAT
678static long
679fw_device_op_compat_ioctl(struct file *file,
680 unsigned int cmd, unsigned long arg)
681{
682 struct client *client = file->private_data;
683
684 return dispatch_ioctl(client, cmd, compat_ptr(arg));
685}
686#endif
687
688static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
689{
690 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500691 enum dma_data_direction direction;
692 unsigned long size;
693 int page_count, retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500694
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500695 /* FIXME: We could support multiple buffers, but we don't. */
696 if (client->buffer.pages != NULL)
697 return -EBUSY;
698
699 if (!(vma->vm_flags & VM_SHARED))
700 return -EINVAL;
701
702 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500703 return -EINVAL;
704
705 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500706 size = vma->vm_end - vma->vm_start;
707 page_count = size >> PAGE_SHIFT;
708 if (size & ~PAGE_MASK)
709 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500710
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500711 if (vma->vm_flags & VM_WRITE)
712 direction = DMA_TO_DEVICE;
713 else
714 direction = DMA_FROM_DEVICE;
715
716 retval = fw_iso_buffer_init(&client->buffer, client->device->card,
717 page_count, direction);
718 if (retval < 0)
719 return retval;
720
721 retval = fw_iso_buffer_map(&client->buffer, vma);
722 if (retval < 0)
723 fw_iso_buffer_destroy(&client->buffer, client->device->card);
724
725 return retval;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500726}
727
728static int fw_device_op_release(struct inode *inode, struct file *file)
729{
730 struct client *client = file->private_data;
731 struct address_handler *h, *next;
732 struct request *r, *next_r;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500733 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500734
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500735 if (client->buffer.pages)
736 fw_iso_buffer_destroy(&client->buffer, client->device->card);
737
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500738 if (client->iso_context)
739 fw_iso_context_destroy(client->iso_context);
740
741 list_for_each_entry_safe(h, next, &client->handler_list, link) {
742 fw_core_remove_address_handler(&h->handler);
743 kfree(h);
744 }
745
746 list_for_each_entry_safe(r, next_r, &client->request_list, link) {
747 fw_send_response(client->device->card, r->request,
748 RCODE_CONFLICT_ERROR);
749 kfree(r);
750 }
751
752 /* TODO: wait for all transactions to finish so
753 * complete_transaction doesn't try to queue up responses
754 * after we free client. */
755 while (!list_empty(&client->event_list))
756 dequeue_event(client, NULL, 0);
757
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500758 spin_lock_irqsave(&client->device->card->lock, flags);
759 list_del(&client->link);
760 spin_unlock_irqrestore(&client->device->card->lock, flags);
761
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500762 fw_device_put(client->device);
763 kfree(client);
764
765 return 0;
766}
767
768static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
769{
770 struct client *client = file->private_data;
771
772 poll_wait(file, &client->wait, pt);
773
774 if (!list_empty(&client->event_list))
775 return POLLIN | POLLRDNORM;
776 else
777 return 0;
778}
779
Stefan Richter21ebcd12007-01-14 15:29:07 +0100780const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500781 .owner = THIS_MODULE,
782 .open = fw_device_op_open,
783 .read = fw_device_op_read,
784 .unlocked_ioctl = fw_device_op_ioctl,
785 .poll = fw_device_op_poll,
786 .release = fw_device_op_release,
787 .mmap = fw_device_op_mmap,
788
789#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +0100790 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500791#endif
792};