Documentation: correction to debugging-via-ohci1394
[deliverable/linux.git] / drivers / firewire / fw-cdev.c
CommitLineData
c781c06d
KH
1/*
2 * Char device for device raw access
19a15b93 3 *
c781c06d 4 * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
19a15b93
KH
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/wait.h>
24#include <linux/errno.h>
25#include <linux/device.h>
26#include <linux/vmalloc.h>
27#include <linux/poll.h>
a64408b9
SR
28#include <linux/preempt.h>
29#include <linux/time.h>
19a15b93
KH
30#include <linux/delay.h>
31#include <linux/mm.h>
a3aca3da 32#include <linux/idr.h>
19a15b93 33#include <linux/compat.h>
9640d3d7 34#include <linux/firewire-cdev.h>
a64408b9 35#include <asm/system.h>
19a15b93
KH
36#include <asm/uaccess.h>
37#include "fw-transaction.h"
38#include "fw-topology.h"
39#include "fw-device.h"
19a15b93 40
3964a449
KH
41struct client;
42struct client_resource {
43 struct list_head link;
44 void (*release)(struct client *client, struct client_resource *r);
45 u32 handle;
46};
47
c781c06d
KH
48/*
49 * dequeue_event() just kfree()'s the event, so the event has to be
50 * the first field in the struct.
51 */
52
19a15b93
KH
53struct event {
54 struct { void *data; size_t size; } v[2];
55 struct list_head link;
56};
57
97bd9efa
KH
58struct bus_reset {
59 struct event event;
60 struct fw_cdev_event_bus_reset reset;
61};
62
19a15b93
KH
63struct response {
64 struct event event;
65 struct fw_transaction transaction;
66 struct client *client;
3964a449 67 struct client_resource resource;
19a15b93
KH
68 struct fw_cdev_event_response response;
69};
70
71struct iso_interrupt {
72 struct event event;
73 struct fw_cdev_event_iso_interrupt interrupt;
74};
75
76struct client {
344bbc4d 77 u32 version;
19a15b93
KH
78 struct fw_device *device;
79 spinlock_t lock;
66dea3e5 80 u32 resource_handle;
3964a449 81 struct list_head resource_list;
19a15b93 82 struct list_head event_list;
19a15b93 83 wait_queue_head_t wait;
da8ecffa 84 u64 bus_reset_closure;
9aad8125 85
19a15b93 86 struct fw_iso_context *iso_context;
abaa5743 87 u64 iso_closure;
9aad8125
KH
88 struct fw_iso_buffer buffer;
89 unsigned long vm_start;
97bd9efa
KH
90
91 struct list_head link;
19a15b93
KH
92};
93
94static inline void __user *
95u64_to_uptr(__u64 value)
96{
97 return (void __user *)(unsigned long)value;
98}
99
100static inline __u64
101uptr_to_u64(void __user *ptr)
102{
103 return (__u64)(unsigned long)ptr;
104}
105
106static int fw_device_op_open(struct inode *inode, struct file *file)
107{
108 struct fw_device *device;
109 struct client *client;
97bd9efa 110 unsigned long flags;
19a15b93 111
96b19062 112 device = fw_device_get_by_devt(inode->i_rdev);
a3aca3da
KH
113 if (device == NULL)
114 return -ENODEV;
19a15b93 115
2d826cc5 116 client = kzalloc(sizeof(*client), GFP_KERNEL);
96b19062
SR
117 if (client == NULL) {
118 fw_device_put(device);
19a15b93 119 return -ENOMEM;
96b19062 120 }
19a15b93 121
96b19062 122 client->device = device;
19a15b93 123 INIT_LIST_HEAD(&client->event_list);
3964a449 124 INIT_LIST_HEAD(&client->resource_list);
19a15b93
KH
125 spin_lock_init(&client->lock);
126 init_waitqueue_head(&client->wait);
127
128 file->private_data = client;
129
97bd9efa
KH
130 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
19a15b93
KH
134 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);
19a15b93 148 list_add_tail(&event->link, &client->event_list);
19a15b93 149 spin_unlock_irqrestore(&client->lock, flags);
83431cba
JF
150
151 wake_up_interruptible(&client->wait);
19a15b93
KH
152}
153
2603bf21
KH
154static int
155dequeue_event(struct client *client, char __user *buffer, size_t count)
19a15b93
KH
156{
157 unsigned long flags;
158 struct event *event;
159 size_t size, total;
2603bf21 160 int i, retval;
19a15b93 161
2603bf21
KH
162 retval = wait_event_interruptible(client->wait,
163 !list_empty(&client->event_list) ||
164 fw_device_is_shutdown(client->device));
165 if (retval < 0)
166 return retval;
19a15b93 167
2603bf21
KH
168 if (list_empty(&client->event_list) &&
169 fw_device_is_shutdown(client->device))
170 return -ENODEV;
19a15b93 171
2603bf21 172 spin_lock_irqsave(&client->lock, flags);
19a15b93
KH
173 event = container_of(client->event_list.next, struct event, link);
174 list_del(&event->link);
19a15b93
KH
175 spin_unlock_irqrestore(&client->lock, flags);
176
19a15b93
KH
177 total = 0;
178 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
179 size = min(event->v[i].size, count - total);
2603bf21
KH
180 if (copy_to_user(buffer + total, event->v[i].data, size)) {
181 retval = -EFAULT;
19a15b93 182 goto out;
2603bf21 183 }
19a15b93
KH
184 total += size;
185 }
186 retval = total;
187
188 out:
189 kfree(event);
190
191 return retval;
192}
193
194static ssize_t
195fw_device_op_read(struct file *file,
196 char __user *buffer, size_t count, loff_t *offset)
197{
198 struct client *client = file->private_data;
199
200 return dequeue_event(client, buffer, count);
201}
202
344bbc4d
KH
203static void
204fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
da8ecffa 205 struct client *client)
344bbc4d 206{
da8ecffa 207 struct fw_card *card = client->device->card;
344bbc4d 208
da8ecffa 209 event->closure = client->bus_reset_closure;
344bbc4d 210 event->type = FW_CDEV_EVENT_BUS_RESET;
cf5a56ac 211 event->generation = client->device->generation;
b5d2a5e0 212 smp_rmb(); /* node_id must not be older than generation */
da8ecffa 213 event->node_id = client->device->node_id;
344bbc4d
KH
214 event->local_node_id = card->local_node->node_id;
215 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
216 event->irm_node_id = card->irm_node->node_id;
217 event->root_node_id = card->root_node->node_id;
344bbc4d
KH
218}
219
2603bf21
KH
220static void
221for_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
97bd9efa
KH
236static void
237queue_bus_reset_event(struct client *client)
238{
239 struct bus_reset *bus_reset;
97bd9efa 240
2d826cc5 241 bus_reset = kzalloc(sizeof(*bus_reset), GFP_ATOMIC);
97bd9efa
KH
242 if (bus_reset == NULL) {
243 fw_notify("Out of memory when allocating bus reset event\n");
244 return;
245 }
246
da8ecffa 247 fill_bus_reset_event(&bus_reset->reset, client);
97bd9efa
KH
248
249 queue_event(client, &bus_reset->event,
2d826cc5 250 &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
97bd9efa
KH
251}
252
253void fw_device_cdev_update(struct fw_device *device)
254{
2603bf21
KH
255 for_each_client(device, queue_bus_reset_event);
256}
97bd9efa 257
2603bf21
KH
258static void wake_up_client(struct client *client)
259{
260 wake_up_interruptible(&client->wait);
261}
97bd9efa 262
2603bf21
KH
263void fw_device_cdev_remove(struct fw_device *device)
264{
265 for_each_client(device, wake_up_client);
97bd9efa
KH
266}
267
4f259223 268static int ioctl_get_info(struct client *client, void *buffer)
19a15b93 269{
4f259223 270 struct fw_cdev_get_info *get_info = buffer;
344bbc4d
KH
271 struct fw_cdev_event_bus_reset bus_reset;
272
4f259223
KH
273 client->version = get_info->version;
274 get_info->version = FW_CDEV_VERSION;
344bbc4d 275
4f259223
KH
276 if (get_info->rom != 0) {
277 void __user *uptr = u64_to_uptr(get_info->rom);
278 size_t want = get_info->rom_length;
d84702a5 279 size_t have = client->device->config_rom_length * 4;
344bbc4d 280
d84702a5
SR
281 if (copy_to_user(uptr, client->device->config_rom,
282 min(want, have)))
344bbc4d
KH
283 return -EFAULT;
284 }
4f259223 285 get_info->rom_length = client->device->config_rom_length * 4;
344bbc4d 286
4f259223
KH
287 client->bus_reset_closure = get_info->bus_reset_closure;
288 if (get_info->bus_reset != 0) {
289 void __user *uptr = u64_to_uptr(get_info->bus_reset);
344bbc4d 290
da8ecffa 291 fill_bus_reset_event(&bus_reset, client);
2d826cc5 292 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
344bbc4d
KH
293 return -EFAULT;
294 }
19a15b93 295
4f259223 296 get_info->card = client->device->card->index;
19a15b93
KH
297
298 return 0;
299}
300
3964a449
KH
301static void
302add_client_resource(struct client *client, struct client_resource *resource)
303{
304 unsigned long flags;
305
306 spin_lock_irqsave(&client->lock, flags);
307 list_add_tail(&resource->link, &client->resource_list);
308 resource->handle = client->resource_handle++;
309 spin_unlock_irqrestore(&client->lock, flags);
310}
311
312static int
313release_client_resource(struct client *client, u32 handle,
314 struct client_resource **resource)
315{
316 struct client_resource *r;
317 unsigned long flags;
318
319 spin_lock_irqsave(&client->lock, flags);
320 list_for_each_entry(r, &client->resource_list, link) {
321 if (r->handle == handle) {
322 list_del(&r->link);
323 break;
324 }
325 }
326 spin_unlock_irqrestore(&client->lock, flags);
327
328 if (&r->link == &client->resource_list)
329 return -EINVAL;
330
331 if (resource)
332 *resource = r;
333 else
334 r->release(client, r);
335
336 return 0;
337}
338
339static void
340release_transaction(struct client *client, struct client_resource *resource)
341{
342 struct response *response =
343 container_of(resource, struct response, resource);
344
345 fw_cancel_transaction(client->device->card, &response->transaction);
346}
347
19a15b93
KH
348static void
349complete_transaction(struct fw_card *card, int rcode,
350 void *payload, size_t length, void *data)
351{
352 struct response *response = data;
353 struct client *client = response->client;
28cf6a04 354 unsigned long flags;
19a15b93
KH
355
356 if (length < response->response.length)
357 response->response.length = length;
358 if (rcode == RCODE_COMPLETE)
359 memcpy(response->response.data, payload,
360 response->response.length);
361
28cf6a04 362 spin_lock_irqsave(&client->lock, flags);
3964a449 363 list_del(&response->resource.link);
28cf6a04
KH
364 spin_unlock_irqrestore(&client->lock, flags);
365
19a15b93
KH
366 response->response.type = FW_CDEV_EVENT_RESPONSE;
367 response->response.rcode = rcode;
368 queue_event(client, &response->event,
2d826cc5 369 &response->response, sizeof(response->response),
19a15b93
KH
370 response->response.data, response->response.length);
371}
372
350958f9 373static int ioctl_send_request(struct client *client, void *buffer)
19a15b93
KH
374{
375 struct fw_device *device = client->device;
4f259223 376 struct fw_cdev_send_request *request = buffer;
19a15b93
KH
377 struct response *response;
378
19a15b93 379 /* What is the biggest size we'll accept, really? */
4f259223 380 if (request->length > 4096)
19a15b93
KH
381 return -EINVAL;
382
2d826cc5 383 response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
19a15b93
KH
384 if (response == NULL)
385 return -ENOMEM;
386
387 response->client = client;
4f259223
KH
388 response->response.length = request->length;
389 response->response.closure = request->closure;
19a15b93 390
4f259223 391 if (request->data &&
19a15b93 392 copy_from_user(response->response.data,
4f259223 393 u64_to_uptr(request->data), request->length)) {
19a15b93
KH
394 kfree(response);
395 return -EFAULT;
396 }
397
3964a449
KH
398 response->resource.release = release_transaction;
399 add_client_resource(client, &response->resource);
28cf6a04 400
19a15b93 401 fw_send_request(device->card, &response->transaction,
4f259223 402 request->tcode & 0x1f,
907293d7 403 device->node->node_id,
4f259223 404 request->generation,
f1397490 405 device->max_speed,
4f259223
KH
406 request->offset,
407 response->response.data, request->length,
19a15b93
KH
408 complete_transaction, response);
409
4f259223 410 if (request->data)
2d826cc5 411 return sizeof(request) + request->length;
19a15b93 412 else
2d826cc5 413 return sizeof(request);
19a15b93
KH
414}
415
416struct address_handler {
417 struct fw_address_handler handler;
418 __u64 closure;
419 struct client *client;
3964a449 420 struct client_resource resource;
19a15b93
KH
421};
422
423struct request {
424 struct fw_request *request;
425 void *data;
426 size_t length;
3964a449 427 struct client_resource resource;
19a15b93
KH
428};
429
430struct request_event {
431 struct event event;
432 struct fw_cdev_event_request request;
433};
434
3964a449
KH
435static void
436release_request(struct client *client, struct client_resource *resource)
437{
438 struct request *request =
439 container_of(resource, struct request, resource);
440
441 fw_send_response(client->device->card, request->request,
442 RCODE_CONFLICT_ERROR);
443 kfree(request);
444}
445
19a15b93
KH
446static void
447handle_request(struct fw_card *card, struct fw_request *r,
448 int tcode, int destination, int source,
449 int generation, int speed,
450 unsigned long long offset,
451 void *payload, size_t length, void *callback_data)
452{
453 struct address_handler *handler = callback_data;
454 struct request *request;
455 struct request_event *e;
19a15b93
KH
456 struct client *client = handler->client;
457
2d826cc5
KH
458 request = kmalloc(sizeof(*request), GFP_ATOMIC);
459 e = kmalloc(sizeof(*e), GFP_ATOMIC);
19a15b93
KH
460 if (request == NULL || e == NULL) {
461 kfree(request);
462 kfree(e);
463 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
464 return;
465 }
466
467 request->request = r;
468 request->data = payload;
469 request->length = length;
470
3964a449
KH
471 request->resource.release = release_request;
472 add_client_resource(client, &request->resource);
19a15b93
KH
473
474 e->request.type = FW_CDEV_EVENT_REQUEST;
475 e->request.tcode = tcode;
476 e->request.offset = offset;
477 e->request.length = length;
3964a449 478 e->request.handle = request->resource.handle;
19a15b93
KH
479 e->request.closure = handler->closure;
480
481 queue_event(client, &e->event,
2d826cc5 482 &e->request, sizeof(e->request), payload, length);
19a15b93
KH
483}
484
3964a449
KH
485static void
486release_address_handler(struct client *client,
487 struct client_resource *resource)
488{
489 struct address_handler *handler =
490 container_of(resource, struct address_handler, resource);
491
492 fw_core_remove_address_handler(&handler->handler);
493 kfree(handler);
494}
495
4f259223 496static int ioctl_allocate(struct client *client, void *buffer)
19a15b93 497{
4f259223 498 struct fw_cdev_allocate *request = buffer;
19a15b93 499 struct address_handler *handler;
19a15b93
KH
500 struct fw_address_region region;
501
2d826cc5 502 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
19a15b93
KH
503 if (handler == NULL)
504 return -ENOMEM;
505
4f259223
KH
506 region.start = request->offset;
507 region.end = request->offset + request->length;
508 handler->handler.length = request->length;
19a15b93
KH
509 handler->handler.address_callback = handle_request;
510 handler->handler.callback_data = handler;
4f259223 511 handler->closure = request->closure;
19a15b93
KH
512 handler->client = client;
513
514 if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
515 kfree(handler);
516 return -EBUSY;
517 }
518
3964a449
KH
519 handler->resource.release = release_address_handler;
520 add_client_resource(client, &handler->resource);
4f259223 521 request->handle = handler->resource.handle;
19a15b93
KH
522
523 return 0;
524}
525
4f259223 526static int ioctl_deallocate(struct client *client, void *buffer)
9472316b 527{
4f259223 528 struct fw_cdev_deallocate *request = buffer;
9472316b 529
4f259223 530 return release_client_resource(client, request->handle, NULL);
9472316b
KH
531}
532
4f259223 533static int ioctl_send_response(struct client *client, void *buffer)
19a15b93 534{
4f259223 535 struct fw_cdev_send_response *request = buffer;
3964a449 536 struct client_resource *resource;
19a15b93 537 struct request *r;
19a15b93 538
4f259223 539 if (release_client_resource(client, request->handle, &resource) < 0)
19a15b93 540 return -EINVAL;
3964a449 541 r = container_of(resource, struct request, resource);
4f259223
KH
542 if (request->length < r->length)
543 r->length = request->length;
544 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
19a15b93
KH
545 return -EFAULT;
546
4f259223 547 fw_send_response(client->device->card, r->request, request->rcode);
19a15b93
KH
548 kfree(r);
549
550 return 0;
551}
552
4f259223 553static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
5371842b 554{
4f259223 555 struct fw_cdev_initiate_bus_reset *request = buffer;
5371842b
KH
556 int short_reset;
557
4f259223 558 short_reset = (request->type == FW_CDEV_SHORT_RESET);
5371842b
KH
559
560 return fw_core_initiate_bus_reset(client->device->card, short_reset);
561}
562
66dea3e5
KH
563struct descriptor {
564 struct fw_descriptor d;
3964a449 565 struct client_resource resource;
66dea3e5
KH
566 u32 data[0];
567};
568
3964a449
KH
569static void release_descriptor(struct client *client,
570 struct client_resource *resource)
571{
572 struct descriptor *descriptor =
573 container_of(resource, struct descriptor, resource);
574
575 fw_core_remove_descriptor(&descriptor->d);
576 kfree(descriptor);
577}
578
4f259223 579static int ioctl_add_descriptor(struct client *client, void *buffer)
66dea3e5 580{
4f259223 581 struct fw_cdev_add_descriptor *request = buffer;
66dea3e5 582 struct descriptor *descriptor;
66dea3e5
KH
583 int retval;
584
4f259223 585 if (request->length > 256)
66dea3e5
KH
586 return -EINVAL;
587
588 descriptor =
2d826cc5 589 kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
66dea3e5
KH
590 if (descriptor == NULL)
591 return -ENOMEM;
592
593 if (copy_from_user(descriptor->data,
4f259223 594 u64_to_uptr(request->data), request->length * 4)) {
66dea3e5
KH
595 kfree(descriptor);
596 return -EFAULT;
597 }
598
4f259223
KH
599 descriptor->d.length = request->length;
600 descriptor->d.immediate = request->immediate;
601 descriptor->d.key = request->key;
66dea3e5
KH
602 descriptor->d.data = descriptor->data;
603
604 retval = fw_core_add_descriptor(&descriptor->d);
605 if (retval < 0) {
606 kfree(descriptor);
607 return retval;
608 }
609
3964a449
KH
610 descriptor->resource.release = release_descriptor;
611 add_client_resource(client, &descriptor->resource);
4f259223 612 request->handle = descriptor->resource.handle;
66dea3e5
KH
613
614 return 0;
615}
616
4f259223 617static int ioctl_remove_descriptor(struct client *client, void *buffer)
66dea3e5 618{
4f259223 619 struct fw_cdev_remove_descriptor *request = buffer;
66dea3e5 620
4f259223 621 return release_client_resource(client, request->handle, NULL);
66dea3e5
KH
622}
623
19a15b93 624static void
9b32d5f3
KH
625iso_callback(struct fw_iso_context *context, u32 cycle,
626 size_t header_length, void *header, void *data)
19a15b93
KH
627{
628 struct client *client = data;
930e4b7f 629 struct iso_interrupt *irq;
19a15b93 630
930e4b7f
SR
631 irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
632 if (irq == NULL)
19a15b93
KH
633 return;
634
930e4b7f
SR
635 irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
636 irq->interrupt.closure = client->iso_closure;
637 irq->interrupt.cycle = cycle;
638 irq->interrupt.header_length = header_length;
639 memcpy(irq->interrupt.header, header, header_length);
640 queue_event(client, &irq->event, &irq->interrupt,
641 sizeof(irq->interrupt) + header_length, NULL, 0);
19a15b93
KH
642}
643
4f259223 644static int ioctl_create_iso_context(struct client *client, void *buffer)
19a15b93 645{
4f259223 646 struct fw_cdev_create_iso_context *request = buffer;
24315c5e 647 struct fw_iso_context *context;
19a15b93 648
4f259223 649 if (request->channel > 63)
21efb3cf
KH
650 return -EINVAL;
651
4f259223 652 switch (request->type) {
c70dc788 653 case FW_ISO_CONTEXT_RECEIVE:
4f259223 654 if (request->header_size < 4 || (request->header_size & 3))
c70dc788 655 return -EINVAL;
98b6cbe8 656
c70dc788
KH
657 break;
658
659 case FW_ISO_CONTEXT_TRANSMIT:
4f259223 660 if (request->speed > SCODE_3200)
c70dc788
KH
661 return -EINVAL;
662
663 break;
664
665 default:
21efb3cf 666 return -EINVAL;
c70dc788
KH
667 }
668
24315c5e
KH
669 context = fw_iso_context_create(client->device->card,
670 request->type,
671 request->channel,
672 request->speed,
673 request->header_size,
674 iso_callback, client);
675 if (IS_ERR(context))
676 return PTR_ERR(context);
677
abaa5743 678 client->iso_closure = request->closure;
24315c5e 679 client->iso_context = context;
19a15b93 680
abaa5743
KH
681 /* We only support one context at this time. */
682 request->handle = 0;
683
19a15b93
KH
684 return 0;
685}
686
1ca31ae7
KH
687/* Macros for decoding the iso packet control header. */
688#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
689#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
690#define GET_SKIP(v) (((v) >> 17) & 0x01)
691#define GET_TAG(v) (((v) >> 18) & 0x02)
692#define GET_SY(v) (((v) >> 20) & 0x04)
693#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
694
4f259223 695static int ioctl_queue_iso(struct client *client, void *buffer)
19a15b93 696{
4f259223 697 struct fw_cdev_queue_iso *request = buffer;
19a15b93 698 struct fw_cdev_iso_packet __user *p, *end, *next;
9b32d5f3 699 struct fw_iso_context *ctx = client->iso_context;
ef370ee7 700 unsigned long payload, buffer_end, header_length;
1ca31ae7 701 u32 control;
19a15b93
KH
702 int count;
703 struct {
704 struct fw_iso_packet packet;
705 u8 header[256];
706 } u;
707
abaa5743 708 if (ctx == NULL || request->handle != 0)
19a15b93 709 return -EINVAL;
19a15b93 710
c781c06d
KH
711 /*
712 * If the user passes a non-NULL data pointer, has mmap()'ed
19a15b93
KH
713 * the iso buffer, and the pointer points inside the buffer,
714 * we setup the payload pointers accordingly. Otherwise we
9aad8125 715 * set them both to 0, which will still let packets with
19a15b93
KH
716 * payload_length == 0 through. In other words, if no packets
717 * use the indirect payload, the iso buffer need not be mapped
c781c06d
KH
718 * and the request->data pointer is ignored.
719 */
19a15b93 720
4f259223 721 payload = (unsigned long)request->data - client->vm_start;
ef370ee7 722 buffer_end = client->buffer.page_count << PAGE_SHIFT;
4f259223 723 if (request->data == 0 || client->buffer.pages == NULL ||
ef370ee7 724 payload >= buffer_end) {
9aad8125 725 payload = 0;
ef370ee7 726 buffer_end = 0;
19a15b93
KH
727 }
728
1ccc9147
AV
729 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
730
731 if (!access_ok(VERIFY_READ, p, request->size))
19a15b93
KH
732 return -EFAULT;
733
4f259223 734 end = (void __user *)p + request->size;
19a15b93
KH
735 count = 0;
736 while (p < end) {
1ca31ae7 737 if (get_user(control, &p->control))
19a15b93 738 return -EFAULT;
1ca31ae7
KH
739 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
740 u.packet.interrupt = GET_INTERRUPT(control);
741 u.packet.skip = GET_SKIP(control);
742 u.packet.tag = GET_TAG(control);
743 u.packet.sy = GET_SY(control);
744 u.packet.header_length = GET_HEADER_LENGTH(control);
295e3feb 745
9b32d5f3 746 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
295e3feb
KH
747 header_length = u.packet.header_length;
748 } else {
c781c06d
KH
749 /*
750 * We require that header_length is a multiple of
751 * the fixed header size, ctx->header_size.
752 */
9b32d5f3
KH
753 if (ctx->header_size == 0) {
754 if (u.packet.header_length > 0)
755 return -EINVAL;
756 } else if (u.packet.header_length % ctx->header_size != 0) {
295e3feb 757 return -EINVAL;
9b32d5f3 758 }
295e3feb
KH
759 header_length = 0;
760 }
761
19a15b93 762 next = (struct fw_cdev_iso_packet __user *)
295e3feb 763 &p->header[header_length / 4];
19a15b93
KH
764 if (next > end)
765 return -EINVAL;
766 if (__copy_from_user
295e3feb 767 (u.packet.header, p->header, header_length))
19a15b93 768 return -EFAULT;
98b6cbe8 769 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
19a15b93
KH
770 u.packet.header_length + u.packet.payload_length > 0)
771 return -EINVAL;
ef370ee7 772 if (payload + u.packet.payload_length > buffer_end)
19a15b93
KH
773 return -EINVAL;
774
9b32d5f3
KH
775 if (fw_iso_context_queue(ctx, &u.packet,
776 &client->buffer, payload))
19a15b93
KH
777 break;
778
779 p = next;
780 payload += u.packet.payload_length;
781 count++;
782 }
783
4f259223
KH
784 request->size -= uptr_to_u64(p) - request->packets;
785 request->packets = uptr_to_u64(p);
786 request->data = client->vm_start + payload;
19a15b93
KH
787
788 return count;
789}
790
4f259223 791static int ioctl_start_iso(struct client *client, void *buffer)
19a15b93 792{
4f259223 793 struct fw_cdev_start_iso *request = buffer;
19a15b93 794
abaa5743
KH
795 if (request->handle != 0)
796 return -EINVAL;
eb0306ea 797 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
4f259223 798 if (request->tags == 0 || request->tags > 15)
eb0306ea
KH
799 return -EINVAL;
800
4f259223 801 if (request->sync > 15)
eb0306ea
KH
802 return -EINVAL;
803 }
804
4f259223
KH
805 return fw_iso_context_start(client->iso_context, request->cycle,
806 request->sync, request->tags);
19a15b93
KH
807}
808
4f259223 809static int ioctl_stop_iso(struct client *client, void *buffer)
b8295668 810{
abaa5743
KH
811 struct fw_cdev_stop_iso *request = buffer;
812
813 if (request->handle != 0)
814 return -EINVAL;
815
b8295668
KH
816 return fw_iso_context_stop(client->iso_context);
817}
818
a64408b9
SR
819static int ioctl_get_cycle_timer(struct client *client, void *buffer)
820{
821 struct fw_cdev_get_cycle_timer *request = buffer;
822 struct fw_card *card = client->device->card;
823 unsigned long long bus_time;
824 struct timeval tv;
825 unsigned long flags;
826
827 preempt_disable();
828 local_irq_save(flags);
829
830 bus_time = card->driver->get_bus_time(card);
831 do_gettimeofday(&tv);
832
833 local_irq_restore(flags);
834 preempt_enable();
835
836 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
837 request->cycle_timer = bus_time & 0xffffffff;
838 return 0;
839}
840
4f259223
KH
841static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
842 ioctl_get_info,
843 ioctl_send_request,
844 ioctl_allocate,
845 ioctl_deallocate,
846 ioctl_send_response,
847 ioctl_initiate_bus_reset,
848 ioctl_add_descriptor,
849 ioctl_remove_descriptor,
850 ioctl_create_iso_context,
851 ioctl_queue_iso,
852 ioctl_start_iso,
853 ioctl_stop_iso,
a64408b9 854 ioctl_get_cycle_timer,
4f259223
KH
855};
856
19a15b93
KH
857static int
858dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
859{
4f259223
KH
860 char buffer[256];
861 int retval;
862
863 if (_IOC_TYPE(cmd) != '#' ||
864 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
19a15b93 865 return -EINVAL;
4f259223
KH
866
867 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2d826cc5 868 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
4f259223
KH
869 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
870 return -EFAULT;
871 }
872
873 retval = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
874 if (retval < 0)
875 return retval;
876
877 if (_IOC_DIR(cmd) & _IOC_READ) {
2d826cc5 878 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
4f259223
KH
879 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
880 return -EFAULT;
19a15b93 881 }
4f259223
KH
882
883 return 0;
19a15b93
KH
884}
885
886static long
887fw_device_op_ioctl(struct file *file,
888 unsigned int cmd, unsigned long arg)
889{
890 struct client *client = file->private_data;
891
892 return dispatch_ioctl(client, cmd, (void __user *) arg);
893}
894
895#ifdef CONFIG_COMPAT
896static long
897fw_device_op_compat_ioctl(struct file *file,
898 unsigned int cmd, unsigned long arg)
899{
900 struct client *client = file->private_data;
901
902 return dispatch_ioctl(client, cmd, compat_ptr(arg));
903}
904#endif
905
906static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
907{
908 struct client *client = file->private_data;
9aad8125
KH
909 enum dma_data_direction direction;
910 unsigned long size;
911 int page_count, retval;
912
913 /* FIXME: We could support multiple buffers, but we don't. */
914 if (client->buffer.pages != NULL)
915 return -EBUSY;
916
917 if (!(vma->vm_flags & VM_SHARED))
918 return -EINVAL;
19a15b93 919
9aad8125 920 if (vma->vm_start & ~PAGE_MASK)
19a15b93
KH
921 return -EINVAL;
922
923 client->vm_start = vma->vm_start;
9aad8125
KH
924 size = vma->vm_end - vma->vm_start;
925 page_count = size >> PAGE_SHIFT;
926 if (size & ~PAGE_MASK)
927 return -EINVAL;
928
929 if (vma->vm_flags & VM_WRITE)
930 direction = DMA_TO_DEVICE;
931 else
932 direction = DMA_FROM_DEVICE;
933
934 retval = fw_iso_buffer_init(&client->buffer, client->device->card,
935 page_count, direction);
936 if (retval < 0)
937 return retval;
19a15b93 938
9aad8125
KH
939 retval = fw_iso_buffer_map(&client->buffer, vma);
940 if (retval < 0)
941 fw_iso_buffer_destroy(&client->buffer, client->device->card);
942
943 return retval;
19a15b93
KH
944}
945
946static int fw_device_op_release(struct inode *inode, struct file *file)
947{
948 struct client *client = file->private_data;
2603bf21 949 struct event *e, *next_e;
3964a449 950 struct client_resource *r, *next_r;
97bd9efa 951 unsigned long flags;
19a15b93 952
9aad8125
KH
953 if (client->buffer.pages)
954 fw_iso_buffer_destroy(&client->buffer, client->device->card);
955
19a15b93
KH
956 if (client->iso_context)
957 fw_iso_context_destroy(client->iso_context);
958
3964a449
KH
959 list_for_each_entry_safe(r, next_r, &client->resource_list, link)
960 r->release(client, r);
66dea3e5 961
c781c06d
KH
962 /*
963 * FIXME: We should wait for the async tasklets to stop
964 * running before freeing the memory.
965 */
28cf6a04 966
2603bf21
KH
967 list_for_each_entry_safe(e, next_e, &client->event_list, link)
968 kfree(e);
19a15b93 969
97bd9efa
KH
970 spin_lock_irqsave(&client->device->card->lock, flags);
971 list_del(&client->link);
972 spin_unlock_irqrestore(&client->device->card->lock, flags);
973
19a15b93
KH
974 fw_device_put(client->device);
975 kfree(client);
976
977 return 0;
978}
979
980static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
981{
982 struct client *client = file->private_data;
2603bf21 983 unsigned int mask = 0;
19a15b93
KH
984
985 poll_wait(file, &client->wait, pt);
986
2603bf21
KH
987 if (fw_device_is_shutdown(client->device))
988 mask |= POLLHUP | POLLERR;
19a15b93 989 if (!list_empty(&client->event_list))
2603bf21
KH
990 mask |= POLLIN | POLLRDNORM;
991
992 return mask;
19a15b93
KH
993}
994
21ebcd12 995const struct file_operations fw_device_ops = {
19a15b93
KH
996 .owner = THIS_MODULE,
997 .open = fw_device_op_open,
998 .read = fw_device_op_read,
999 .unlocked_ioctl = fw_device_op_ioctl,
1000 .poll = fw_device_op_poll,
1001 .release = fw_device_op_release,
1002 .mmap = fw_device_op_mmap,
1003
1004#ifdef CONFIG_COMPAT
5af4e5ea 1005 .compat_ioctl = fw_device_op_compat_ioctl,
19a15b93
KH
1006#endif
1007};
This page took 0.251211 seconds and 5 git commands to generate.