firewire: core: topology header fix
[GitHub/mt8127/android_kernel_alcatel_ttab.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
be5bbd67
SR
21#include <linux/compat.h>
22#include <linux/delay.h>
23#include <linux/device.h>
24#include <linux/errno.h>
25#include <linux/firewire-cdev.h>
26#include <linux/idr.h>
19a15b93 27#include <linux/kernel.h>
fb443036 28#include <linux/kref.h>
be5bbd67
SR
29#include <linux/mm.h>
30#include <linux/module.h>
d67cfb96 31#include <linux/mutex.h>
19a15b93 32#include <linux/poll.h>
a64408b9 33#include <linux/preempt.h>
cf417e54 34#include <linux/spinlock.h>
be5bbd67
SR
35#include <linux/time.h>
36#include <linux/vmalloc.h>
37#include <linux/wait.h>
38
a64408b9 39#include <asm/system.h>
19a15b93 40#include <asm/uaccess.h>
be5bbd67 41
19a15b93 42#include "fw-device.h"
be5bbd67
SR
43#include "fw-topology.h"
44#include "fw-transaction.h"
19a15b93 45
19a15b93 46struct client {
344bbc4d 47 u32 version;
19a15b93 48 struct fw_device *device;
45ee3199 49
19a15b93 50 spinlock_t lock;
45ee3199
JF
51 bool in_shutdown;
52 struct idr resource_idr;
19a15b93 53 struct list_head event_list;
19a15b93 54 wait_queue_head_t wait;
da8ecffa 55 u64 bus_reset_closure;
9aad8125 56
19a15b93 57 struct fw_iso_context *iso_context;
abaa5743 58 u64 iso_closure;
9aad8125
KH
59 struct fw_iso_buffer buffer;
60 unsigned long vm_start;
97bd9efa
KH
61
62 struct list_head link;
fb443036 63 struct kref kref;
19a15b93
KH
64};
65
fb443036
SR
66static inline void client_get(struct client *client)
67{
68 kref_get(&client->kref);
69}
70
71static void client_release(struct kref *kref)
72{
73 struct client *client = container_of(kref, struct client, kref);
74
75 fw_device_put(client->device);
76 kfree(client);
77}
78
79static void client_put(struct client *client)
80{
81 kref_put(&client->kref, client_release);
82}
83
97c18b7f
SR
84struct client_resource;
85typedef void (*client_resource_release_fn_t)(struct client *,
86 struct client_resource *);
87struct client_resource {
88 client_resource_release_fn_t release;
89 int handle;
90};
91
92struct address_handler_resource {
93 struct client_resource resource;
94 struct fw_address_handler handler;
95 __u64 closure;
96 struct client *client;
97};
98
99struct outbound_transaction_resource {
100 struct client_resource resource;
101 struct fw_transaction transaction;
102};
103
104struct inbound_transaction_resource {
105 struct client_resource resource;
106 struct fw_request *request;
107 void *data;
108 size_t length;
109};
110
111struct descriptor_resource {
112 struct client_resource resource;
113 struct fw_descriptor descriptor;
114 u32 data[0];
115};
116
117/*
118 * dequeue_event() just kfree()'s the event, so the event has to be
119 * the first field in a struct XYZ_event.
120 */
121struct event {
122 struct { void *data; size_t size; } v[2];
123 struct list_head link;
124};
125
126struct bus_reset_event {
127 struct event event;
128 struct fw_cdev_event_bus_reset reset;
129};
130
131struct outbound_transaction_event {
132 struct event event;
133 struct client *client;
134 struct outbound_transaction_resource r;
135 struct fw_cdev_event_response response;
136};
137
138struct inbound_transaction_event {
139 struct event event;
140 struct fw_cdev_event_request request;
141};
142
143struct iso_interrupt_event {
144 struct event event;
145 struct fw_cdev_event_iso_interrupt interrupt;
146};
147
53dca511 148static inline void __user *u64_to_uptr(__u64 value)
19a15b93
KH
149{
150 return (void __user *)(unsigned long)value;
151}
152
53dca511 153static inline __u64 uptr_to_u64(void __user *ptr)
19a15b93
KH
154{
155 return (__u64)(unsigned long)ptr;
156}
157
158static int fw_device_op_open(struct inode *inode, struct file *file)
159{
160 struct fw_device *device;
161 struct client *client;
162
96b19062 163 device = fw_device_get_by_devt(inode->i_rdev);
a3aca3da
KH
164 if (device == NULL)
165 return -ENODEV;
19a15b93 166
551f4cb9
JF
167 if (fw_device_is_shutdown(device)) {
168 fw_device_put(device);
169 return -ENODEV;
170 }
171
2d826cc5 172 client = kzalloc(sizeof(*client), GFP_KERNEL);
96b19062
SR
173 if (client == NULL) {
174 fw_device_put(device);
19a15b93 175 return -ENOMEM;
96b19062 176 }
19a15b93 177
96b19062 178 client->device = device;
19a15b93 179 spin_lock_init(&client->lock);
45ee3199
JF
180 idr_init(&client->resource_idr);
181 INIT_LIST_HEAD(&client->event_list);
19a15b93 182 init_waitqueue_head(&client->wait);
fb443036 183 kref_init(&client->kref);
19a15b93
KH
184
185 file->private_data = client;
186
d67cfb96 187 mutex_lock(&device->client_list_mutex);
97bd9efa 188 list_add_tail(&client->link, &device->client_list);
d67cfb96 189 mutex_unlock(&device->client_list_mutex);
97bd9efa 190
19a15b93
KH
191 return 0;
192}
193
194static void queue_event(struct client *client, struct event *event,
195 void *data0, size_t size0, void *data1, size_t size1)
196{
197 unsigned long flags;
198
199 event->v[0].data = data0;
200 event->v[0].size = size0;
201 event->v[1].data = data1;
202 event->v[1].size = size1;
203
204 spin_lock_irqsave(&client->lock, flags);
45ee3199
JF
205 if (client->in_shutdown)
206 kfree(event);
207 else
208 list_add_tail(&event->link, &client->event_list);
19a15b93 209 spin_unlock_irqrestore(&client->lock, flags);
83431cba
JF
210
211 wake_up_interruptible(&client->wait);
19a15b93
KH
212}
213
53dca511
SR
214static int dequeue_event(struct client *client,
215 char __user *buffer, size_t count)
19a15b93
KH
216{
217 unsigned long flags;
218 struct event *event;
219 size_t size, total;
2dbd7d7e 220 int i, ret;
19a15b93 221
2dbd7d7e
SR
222 ret = wait_event_interruptible(client->wait,
223 !list_empty(&client->event_list) ||
224 fw_device_is_shutdown(client->device));
225 if (ret < 0)
226 return ret;
19a15b93 227
2603bf21
KH
228 if (list_empty(&client->event_list) &&
229 fw_device_is_shutdown(client->device))
230 return -ENODEV;
19a15b93 231
2603bf21 232 spin_lock_irqsave(&client->lock, flags);
a459b8ab 233 event = list_first_entry(&client->event_list, struct event, link);
19a15b93 234 list_del(&event->link);
19a15b93
KH
235 spin_unlock_irqrestore(&client->lock, flags);
236
19a15b93
KH
237 total = 0;
238 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
239 size = min(event->v[i].size, count - total);
2603bf21 240 if (copy_to_user(buffer + total, event->v[i].data, size)) {
2dbd7d7e 241 ret = -EFAULT;
19a15b93 242 goto out;
2603bf21 243 }
19a15b93
KH
244 total += size;
245 }
2dbd7d7e 246 ret = total;
19a15b93
KH
247
248 out:
249 kfree(event);
250
2dbd7d7e 251 return ret;
19a15b93
KH
252}
253
53dca511
SR
254static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
255 size_t count, loff_t *offset)
19a15b93
KH
256{
257 struct client *client = file->private_data;
258
259 return dequeue_event(client, buffer, count);
260}
261
53dca511
SR
262static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
263 struct client *client)
344bbc4d 264{
da8ecffa 265 struct fw_card *card = client->device->card;
cf417e54
JF
266 unsigned long flags;
267
268 spin_lock_irqsave(&card->lock, flags);
344bbc4d 269
da8ecffa 270 event->closure = client->bus_reset_closure;
344bbc4d 271 event->type = FW_CDEV_EVENT_BUS_RESET;
cf5a56ac 272 event->generation = client->device->generation;
da8ecffa 273 event->node_id = client->device->node_id;
344bbc4d
KH
274 event->local_node_id = card->local_node->node_id;
275 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
276 event->irm_node_id = card->irm_node->node_id;
277 event->root_node_id = card->root_node->node_id;
cf417e54
JF
278
279 spin_unlock_irqrestore(&card->lock, flags);
344bbc4d
KH
280}
281
53dca511
SR
282static void for_each_client(struct fw_device *device,
283 void (*callback)(struct client *client))
2603bf21 284{
2603bf21 285 struct client *c;
2603bf21 286
d67cfb96 287 mutex_lock(&device->client_list_mutex);
2603bf21
KH
288 list_for_each_entry(c, &device->client_list, link)
289 callback(c);
d67cfb96 290 mutex_unlock(&device->client_list_mutex);
2603bf21
KH
291}
292
53dca511 293static void queue_bus_reset_event(struct client *client)
97bd9efa 294{
97c18b7f 295 struct bus_reset_event *e;
97bd9efa 296
97c18b7f
SR
297 e = kzalloc(sizeof(*e), GFP_KERNEL);
298 if (e == NULL) {
97bd9efa
KH
299 fw_notify("Out of memory when allocating bus reset event\n");
300 return;
301 }
302
97c18b7f 303 fill_bus_reset_event(&e->reset, client);
97bd9efa 304
97c18b7f
SR
305 queue_event(client, &e->event,
306 &e->reset, sizeof(e->reset), NULL, 0);
97bd9efa
KH
307}
308
309void fw_device_cdev_update(struct fw_device *device)
310{
2603bf21
KH
311 for_each_client(device, queue_bus_reset_event);
312}
97bd9efa 313
2603bf21
KH
314static void wake_up_client(struct client *client)
315{
316 wake_up_interruptible(&client->wait);
317}
97bd9efa 318
2603bf21
KH
319void fw_device_cdev_remove(struct fw_device *device)
320{
321 for_each_client(device, wake_up_client);
97bd9efa
KH
322}
323
4f259223 324static int ioctl_get_info(struct client *client, void *buffer)
19a15b93 325{
4f259223 326 struct fw_cdev_get_info *get_info = buffer;
344bbc4d 327 struct fw_cdev_event_bus_reset bus_reset;
c9755e14 328 unsigned long ret = 0;
344bbc4d 329
4f259223
KH
330 client->version = get_info->version;
331 get_info->version = FW_CDEV_VERSION;
cf417e54 332 get_info->card = client->device->card->index;
344bbc4d 333
c9755e14
SR
334 down_read(&fw_device_rwsem);
335
4f259223
KH
336 if (get_info->rom != 0) {
337 void __user *uptr = u64_to_uptr(get_info->rom);
338 size_t want = get_info->rom_length;
d84702a5 339 size_t have = client->device->config_rom_length * 4;
344bbc4d 340
c9755e14
SR
341 ret = copy_to_user(uptr, client->device->config_rom,
342 min(want, have));
344bbc4d 343 }
4f259223 344 get_info->rom_length = client->device->config_rom_length * 4;
344bbc4d 345
c9755e14
SR
346 up_read(&fw_device_rwsem);
347
348 if (ret != 0)
349 return -EFAULT;
350
4f259223
KH
351 client->bus_reset_closure = get_info->bus_reset_closure;
352 if (get_info->bus_reset != 0) {
353 void __user *uptr = u64_to_uptr(get_info->bus_reset);
344bbc4d 354
da8ecffa 355 fill_bus_reset_event(&bus_reset, client);
2d826cc5 356 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
344bbc4d
KH
357 return -EFAULT;
358 }
19a15b93 359
19a15b93
KH
360 return 0;
361}
362
53dca511
SR
363static int add_client_resource(struct client *client,
364 struct client_resource *resource, gfp_t gfp_mask)
3964a449
KH
365{
366 unsigned long flags;
45ee3199
JF
367 int ret;
368
369 retry:
370 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
371 return -ENOMEM;
3964a449
KH
372
373 spin_lock_irqsave(&client->lock, flags);
45ee3199
JF
374 if (client->in_shutdown)
375 ret = -ECANCELED;
376 else
377 ret = idr_get_new(&client->resource_idr, resource,
378 &resource->handle);
fb443036
SR
379 if (ret >= 0)
380 client_get(client);
3964a449 381 spin_unlock_irqrestore(&client->lock, flags);
45ee3199
JF
382
383 if (ret == -EAGAIN)
384 goto retry;
385
386 return ret < 0 ? ret : 0;
3964a449
KH
387}
388
53dca511
SR
389static int release_client_resource(struct client *client, u32 handle,
390 client_resource_release_fn_t release,
391 struct client_resource **resource)
3964a449
KH
392{
393 struct client_resource *r;
394 unsigned long flags;
395
396 spin_lock_irqsave(&client->lock, flags);
45ee3199
JF
397 if (client->in_shutdown)
398 r = NULL;
399 else
400 r = idr_find(&client->resource_idr, handle);
401 if (r && r->release == release)
402 idr_remove(&client->resource_idr, handle);
3964a449
KH
403 spin_unlock_irqrestore(&client->lock, flags);
404
45ee3199 405 if (!(r && r->release == release))
3964a449
KH
406 return -EINVAL;
407
408 if (resource)
409 *resource = r;
410 else
411 r->release(client, r);
412
fb443036
SR
413 client_put(client);
414
3964a449
KH
415 return 0;
416}
417
53dca511
SR
418static void release_transaction(struct client *client,
419 struct client_resource *resource)
3964a449 420{
97c18b7f
SR
421 struct outbound_transaction_resource *r = container_of(resource,
422 struct outbound_transaction_resource, resource);
3964a449 423
97c18b7f 424 fw_cancel_transaction(client->device->card, &r->transaction);
3964a449
KH
425}
426
53dca511
SR
427static void complete_transaction(struct fw_card *card, int rcode,
428 void *payload, size_t length, void *data)
19a15b93 429{
97c18b7f
SR
430 struct outbound_transaction_event *e = data;
431 struct fw_cdev_event_response *rsp = &e->response;
432 struct client *client = e->client;
28cf6a04 433 unsigned long flags;
19a15b93 434
97c18b7f
SR
435 if (length < rsp->length)
436 rsp->length = length;
19a15b93 437 if (rcode == RCODE_COMPLETE)
97c18b7f 438 memcpy(rsp->data, payload, rsp->length);
19a15b93 439
28cf6a04 440 spin_lock_irqsave(&client->lock, flags);
45ee3199 441 /*
fb443036
SR
442 * 1. If called while in shutdown, the idr tree must be left untouched.
443 * The idr handle will be removed and the client reference will be
444 * dropped later.
445 * 2. If the call chain was release_client_resource ->
446 * release_transaction -> complete_transaction (instead of a normal
447 * conclusion of the transaction), i.e. if this resource was already
448 * unregistered from the idr, the client reference will be dropped
449 * by release_client_resource and we must not drop it here.
45ee3199 450 */
fb443036 451 if (!client->in_shutdown &&
97c18b7f
SR
452 idr_find(&client->resource_idr, e->r.resource.handle)) {
453 idr_remove(&client->resource_idr, e->r.resource.handle);
fb443036
SR
454 /* Drop the idr's reference */
455 client_put(client);
456 }
28cf6a04
KH
457 spin_unlock_irqrestore(&client->lock, flags);
458
97c18b7f
SR
459 rsp->type = FW_CDEV_EVENT_RESPONSE;
460 rsp->rcode = rcode;
8401d92b
DM
461
462 /*
97c18b7f 463 * In the case that sizeof(*rsp) doesn't align with the position of the
8401d92b
DM
464 * data, and the read is short, preserve an extra copy of the data
465 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
466 * for short reads and some apps depended on it, this is both safe
467 * and prudent for compatibility.
468 */
97c18b7f
SR
469 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
470 queue_event(client, &e->event, rsp, sizeof(*rsp),
471 rsp->data, rsp->length);
8401d92b 472 else
97c18b7f 473 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
8401d92b 474 NULL, 0);
fb443036
SR
475
476 /* Drop the transaction callback's reference */
477 client_put(client);
19a15b93
KH
478}
479
350958f9 480static int ioctl_send_request(struct client *client, void *buffer)
19a15b93
KH
481{
482 struct fw_device *device = client->device;
4f259223 483 struct fw_cdev_send_request *request = buffer;
97c18b7f 484 struct outbound_transaction_event *e;
1f3125af 485 int ret;
19a15b93 486
19a15b93 487 /* What is the biggest size we'll accept, really? */
4f259223 488 if (request->length > 4096)
19a15b93
KH
489 return -EINVAL;
490
97c18b7f
SR
491 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
492 if (e == NULL)
19a15b93
KH
493 return -ENOMEM;
494
97c18b7f
SR
495 e->client = client;
496 e->response.length = request->length;
497 e->response.closure = request->closure;
19a15b93 498
4f259223 499 if (request->data &&
97c18b7f 500 copy_from_user(e->response.data,
4f259223 501 u64_to_uptr(request->data), request->length)) {
1f3125af 502 ret = -EFAULT;
45ee3199 503 goto failed;
1f3125af
SR
504 }
505
506 switch (request->tcode) {
507 case TCODE_WRITE_QUADLET_REQUEST:
508 case TCODE_WRITE_BLOCK_REQUEST:
509 case TCODE_READ_QUADLET_REQUEST:
510 case TCODE_READ_BLOCK_REQUEST:
511 case TCODE_LOCK_MASK_SWAP:
512 case TCODE_LOCK_COMPARE_SWAP:
513 case TCODE_LOCK_FETCH_ADD:
514 case TCODE_LOCK_LITTLE_ADD:
515 case TCODE_LOCK_BOUNDED_ADD:
516 case TCODE_LOCK_WRAP_ADD:
517 case TCODE_LOCK_VENDOR_DEPENDENT:
518 break;
519 default:
520 ret = -EINVAL;
45ee3199 521 goto failed;
19a15b93
KH
522 }
523
97c18b7f
SR
524 e->r.resource.release = release_transaction;
525 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
45ee3199
JF
526 if (ret < 0)
527 goto failed;
28cf6a04 528
fb443036
SR
529 /* Get a reference for the transaction callback */
530 client_get(client);
531
97c18b7f 532 fw_send_request(device->card, &e->r.transaction,
4f259223 533 request->tcode & 0x1f,
907293d7 534 device->node->node_id,
4f259223 535 request->generation,
f1397490 536 device->max_speed,
4f259223 537 request->offset,
97c18b7f
SR
538 e->response.data, request->length,
539 complete_transaction, e);
19a15b93 540
4f259223 541 if (request->data)
2d826cc5 542 return sizeof(request) + request->length;
19a15b93 543 else
2d826cc5 544 return sizeof(request);
45ee3199 545 failed:
97c18b7f 546 kfree(e);
1f3125af
SR
547
548 return ret;
19a15b93
KH
549}
550
53dca511
SR
551static void release_request(struct client *client,
552 struct client_resource *resource)
3964a449 553{
97c18b7f
SR
554 struct inbound_transaction_resource *r = container_of(resource,
555 struct inbound_transaction_resource, resource);
3964a449 556
97c18b7f 557 fw_send_response(client->device->card, r->request,
3964a449 558 RCODE_CONFLICT_ERROR);
97c18b7f 559 kfree(r);
3964a449
KH
560}
561
97c18b7f 562static void handle_request(struct fw_card *card, struct fw_request *request,
53dca511
SR
563 int tcode, int destination, int source,
564 int generation, int speed,
565 unsigned long long offset,
566 void *payload, size_t length, void *callback_data)
19a15b93 567{
97c18b7f
SR
568 struct address_handler_resource *handler = callback_data;
569 struct inbound_transaction_resource *r;
570 struct inbound_transaction_event *e;
45ee3199 571 int ret;
19a15b93 572
97c18b7f 573 r = kmalloc(sizeof(*r), GFP_ATOMIC);
2d826cc5 574 e = kmalloc(sizeof(*e), GFP_ATOMIC);
97c18b7f 575 if (r == NULL || e == NULL)
45ee3199 576 goto failed;
19a15b93 577
97c18b7f
SR
578 r->request = request;
579 r->data = payload;
580 r->length = length;
19a15b93 581
97c18b7f
SR
582 r->resource.release = release_request;
583 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
45ee3199
JF
584 if (ret < 0)
585 goto failed;
19a15b93
KH
586
587 e->request.type = FW_CDEV_EVENT_REQUEST;
588 e->request.tcode = tcode;
589 e->request.offset = offset;
590 e->request.length = length;
97c18b7f 591 e->request.handle = r->resource.handle;
19a15b93
KH
592 e->request.closure = handler->closure;
593
97c18b7f 594 queue_event(handler->client, &e->event,
2d826cc5 595 &e->request, sizeof(e->request), payload, length);
45ee3199
JF
596 return;
597
598 failed:
97c18b7f 599 kfree(r);
45ee3199 600 kfree(e);
97c18b7f 601 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
19a15b93
KH
602}
603
53dca511
SR
604static void release_address_handler(struct client *client,
605 struct client_resource *resource)
3964a449 606{
97c18b7f
SR
607 struct address_handler_resource *r =
608 container_of(resource, struct address_handler_resource, resource);
3964a449 609
97c18b7f
SR
610 fw_core_remove_address_handler(&r->handler);
611 kfree(r);
3964a449
KH
612}
613
4f259223 614static int ioctl_allocate(struct client *client, void *buffer)
19a15b93 615{
4f259223 616 struct fw_cdev_allocate *request = buffer;
97c18b7f 617 struct address_handler_resource *r;
19a15b93 618 struct fw_address_region region;
45ee3199 619 int ret;
19a15b93 620
97c18b7f
SR
621 r = kmalloc(sizeof(*r), GFP_KERNEL);
622 if (r == NULL)
19a15b93
KH
623 return -ENOMEM;
624
4f259223
KH
625 region.start = request->offset;
626 region.end = request->offset + request->length;
97c18b7f
SR
627 r->handler.length = request->length;
628 r->handler.address_callback = handle_request;
629 r->handler.callback_data = r;
630 r->closure = request->closure;
631 r->client = client;
19a15b93 632
97c18b7f 633 ret = fw_core_add_address_handler(&r->handler, &region);
3e0b5f0d 634 if (ret < 0) {
97c18b7f 635 kfree(r);
3e0b5f0d 636 return ret;
19a15b93
KH
637 }
638
97c18b7f
SR
639 r->resource.release = release_address_handler;
640 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
45ee3199 641 if (ret < 0) {
97c18b7f 642 release_address_handler(client, &r->resource);
45ee3199
JF
643 return ret;
644 }
97c18b7f 645 request->handle = r->resource.handle;
19a15b93
KH
646
647 return 0;
648}
649
4f259223 650static int ioctl_deallocate(struct client *client, void *buffer)
9472316b 651{
4f259223 652 struct fw_cdev_deallocate *request = buffer;
9472316b 653
45ee3199
JF
654 return release_client_resource(client, request->handle,
655 release_address_handler, NULL);
9472316b
KH
656}
657
4f259223 658static int ioctl_send_response(struct client *client, void *buffer)
19a15b93 659{
4f259223 660 struct fw_cdev_send_response *request = buffer;
3964a449 661 struct client_resource *resource;
97c18b7f 662 struct inbound_transaction_resource *r;
19a15b93 663
45ee3199
JF
664 if (release_client_resource(client, request->handle,
665 release_request, &resource) < 0)
19a15b93 666 return -EINVAL;
45ee3199 667
97c18b7f
SR
668 r = container_of(resource, struct inbound_transaction_resource,
669 resource);
4f259223
KH
670 if (request->length < r->length)
671 r->length = request->length;
672 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
19a15b93
KH
673 return -EFAULT;
674
4f259223 675 fw_send_response(client->device->card, r->request, request->rcode);
19a15b93
KH
676 kfree(r);
677
678 return 0;
679}
680
4f259223 681static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
5371842b 682{
4f259223 683 struct fw_cdev_initiate_bus_reset *request = buffer;
5371842b
KH
684 int short_reset;
685
4f259223 686 short_reset = (request->type == FW_CDEV_SHORT_RESET);
5371842b
KH
687
688 return fw_core_initiate_bus_reset(client->device->card, short_reset);
689}
690
3964a449
KH
691static void release_descriptor(struct client *client,
692 struct client_resource *resource)
693{
97c18b7f
SR
694 struct descriptor_resource *r =
695 container_of(resource, struct descriptor_resource, resource);
3964a449 696
97c18b7f
SR
697 fw_core_remove_descriptor(&r->descriptor);
698 kfree(r);
3964a449
KH
699}
700
4f259223 701static int ioctl_add_descriptor(struct client *client, void *buffer)
66dea3e5 702{
4f259223 703 struct fw_cdev_add_descriptor *request = buffer;
97c18b7f 704 struct descriptor_resource *r;
45ee3199 705 int ret;
66dea3e5 706
4f259223 707 if (request->length > 256)
66dea3e5
KH
708 return -EINVAL;
709
97c18b7f
SR
710 r = kmalloc(sizeof(*r) + request->length * 4, GFP_KERNEL);
711 if (r == NULL)
66dea3e5
KH
712 return -ENOMEM;
713
97c18b7f 714 if (copy_from_user(r->data,
4f259223 715 u64_to_uptr(request->data), request->length * 4)) {
45ee3199
JF
716 ret = -EFAULT;
717 goto failed;
66dea3e5
KH
718 }
719
97c18b7f
SR
720 r->descriptor.length = request->length;
721 r->descriptor.immediate = request->immediate;
722 r->descriptor.key = request->key;
723 r->descriptor.data = r->data;
66dea3e5 724
97c18b7f 725 ret = fw_core_add_descriptor(&r->descriptor);
45ee3199
JF
726 if (ret < 0)
727 goto failed;
66dea3e5 728
97c18b7f
SR
729 r->resource.release = release_descriptor;
730 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
45ee3199 731 if (ret < 0) {
97c18b7f 732 fw_core_remove_descriptor(&r->descriptor);
45ee3199
JF
733 goto failed;
734 }
97c18b7f 735 request->handle = r->resource.handle;
66dea3e5
KH
736
737 return 0;
45ee3199 738 failed:
97c18b7f 739 kfree(r);
45ee3199
JF
740
741 return ret;
66dea3e5
KH
742}
743
4f259223 744static int ioctl_remove_descriptor(struct client *client, void *buffer)
66dea3e5 745{
4f259223 746 struct fw_cdev_remove_descriptor *request = buffer;
66dea3e5 747
45ee3199
JF
748 return release_client_resource(client, request->handle,
749 release_descriptor, NULL);
66dea3e5
KH
750}
751
53dca511
SR
752static void iso_callback(struct fw_iso_context *context, u32 cycle,
753 size_t header_length, void *header, void *data)
19a15b93
KH
754{
755 struct client *client = data;
97c18b7f 756 struct iso_interrupt_event *e;
19a15b93 757
97c18b7f
SR
758 e = kzalloc(sizeof(*e) + header_length, GFP_ATOMIC);
759 if (e == NULL)
19a15b93
KH
760 return;
761
97c18b7f
SR
762 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
763 e->interrupt.closure = client->iso_closure;
764 e->interrupt.cycle = cycle;
765 e->interrupt.header_length = header_length;
766 memcpy(e->interrupt.header, header, header_length);
767 queue_event(client, &e->event, &e->interrupt,
768 sizeof(e->interrupt) + header_length, NULL, 0);
19a15b93
KH
769}
770
4f259223 771static int ioctl_create_iso_context(struct client *client, void *buffer)
19a15b93 772{
4f259223 773 struct fw_cdev_create_iso_context *request = buffer;
24315c5e 774 struct fw_iso_context *context;
19a15b93 775
fae60312
SR
776 /* We only support one context at this time. */
777 if (client->iso_context != NULL)
778 return -EBUSY;
779
4f259223 780 if (request->channel > 63)
21efb3cf
KH
781 return -EINVAL;
782
4f259223 783 switch (request->type) {
c70dc788 784 case FW_ISO_CONTEXT_RECEIVE:
4f259223 785 if (request->header_size < 4 || (request->header_size & 3))
c70dc788 786 return -EINVAL;
98b6cbe8 787
c70dc788
KH
788 break;
789
790 case FW_ISO_CONTEXT_TRANSMIT:
4f259223 791 if (request->speed > SCODE_3200)
c70dc788
KH
792 return -EINVAL;
793
794 break;
795
796 default:
21efb3cf 797 return -EINVAL;
c70dc788
KH
798 }
799
24315c5e
KH
800 context = fw_iso_context_create(client->device->card,
801 request->type,
802 request->channel,
803 request->speed,
804 request->header_size,
805 iso_callback, client);
806 if (IS_ERR(context))
807 return PTR_ERR(context);
808
abaa5743 809 client->iso_closure = request->closure;
24315c5e 810 client->iso_context = context;
19a15b93 811
abaa5743
KH
812 /* We only support one context at this time. */
813 request->handle = 0;
814
19a15b93
KH
815 return 0;
816}
817
1ca31ae7
KH
818/* Macros for decoding the iso packet control header. */
819#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
820#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
821#define GET_SKIP(v) (((v) >> 17) & 0x01)
7a100344
SR
822#define GET_TAG(v) (((v) >> 18) & 0x03)
823#define GET_SY(v) (((v) >> 20) & 0x0f)
1ca31ae7
KH
824#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
825
4f259223 826static int ioctl_queue_iso(struct client *client, void *buffer)
19a15b93 827{
4f259223 828 struct fw_cdev_queue_iso *request = buffer;
19a15b93 829 struct fw_cdev_iso_packet __user *p, *end, *next;
9b32d5f3 830 struct fw_iso_context *ctx = client->iso_context;
ef370ee7 831 unsigned long payload, buffer_end, header_length;
1ca31ae7 832 u32 control;
19a15b93
KH
833 int count;
834 struct {
835 struct fw_iso_packet packet;
836 u8 header[256];
837 } u;
838
abaa5743 839 if (ctx == NULL || request->handle != 0)
19a15b93 840 return -EINVAL;
19a15b93 841
c781c06d
KH
842 /*
843 * If the user passes a non-NULL data pointer, has mmap()'ed
19a15b93
KH
844 * the iso buffer, and the pointer points inside the buffer,
845 * we setup the payload pointers accordingly. Otherwise we
9aad8125 846 * set them both to 0, which will still let packets with
19a15b93
KH
847 * payload_length == 0 through. In other words, if no packets
848 * use the indirect payload, the iso buffer need not be mapped
c781c06d
KH
849 * and the request->data pointer is ignored.
850 */
19a15b93 851
4f259223 852 payload = (unsigned long)request->data - client->vm_start;
ef370ee7 853 buffer_end = client->buffer.page_count << PAGE_SHIFT;
4f259223 854 if (request->data == 0 || client->buffer.pages == NULL ||
ef370ee7 855 payload >= buffer_end) {
9aad8125 856 payload = 0;
ef370ee7 857 buffer_end = 0;
19a15b93
KH
858 }
859
1ccc9147
AV
860 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
861
862 if (!access_ok(VERIFY_READ, p, request->size))
19a15b93
KH
863 return -EFAULT;
864
4f259223 865 end = (void __user *)p + request->size;
19a15b93
KH
866 count = 0;
867 while (p < end) {
1ca31ae7 868 if (get_user(control, &p->control))
19a15b93 869 return -EFAULT;
1ca31ae7
KH
870 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
871 u.packet.interrupt = GET_INTERRUPT(control);
872 u.packet.skip = GET_SKIP(control);
873 u.packet.tag = GET_TAG(control);
874 u.packet.sy = GET_SY(control);
875 u.packet.header_length = GET_HEADER_LENGTH(control);
295e3feb 876
9b32d5f3 877 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
295e3feb
KH
878 header_length = u.packet.header_length;
879 } else {
c781c06d
KH
880 /*
881 * We require that header_length is a multiple of
882 * the fixed header size, ctx->header_size.
883 */
9b32d5f3
KH
884 if (ctx->header_size == 0) {
885 if (u.packet.header_length > 0)
886 return -EINVAL;
887 } else if (u.packet.header_length % ctx->header_size != 0) {
295e3feb 888 return -EINVAL;
9b32d5f3 889 }
295e3feb
KH
890 header_length = 0;
891 }
892
19a15b93 893 next = (struct fw_cdev_iso_packet __user *)
295e3feb 894 &p->header[header_length / 4];
19a15b93
KH
895 if (next > end)
896 return -EINVAL;
897 if (__copy_from_user
295e3feb 898 (u.packet.header, p->header, header_length))
19a15b93 899 return -EFAULT;
98b6cbe8 900 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
19a15b93
KH
901 u.packet.header_length + u.packet.payload_length > 0)
902 return -EINVAL;
ef370ee7 903 if (payload + u.packet.payload_length > buffer_end)
19a15b93
KH
904 return -EINVAL;
905
9b32d5f3
KH
906 if (fw_iso_context_queue(ctx, &u.packet,
907 &client->buffer, payload))
19a15b93
KH
908 break;
909
910 p = next;
911 payload += u.packet.payload_length;
912 count++;
913 }
914
4f259223
KH
915 request->size -= uptr_to_u64(p) - request->packets;
916 request->packets = uptr_to_u64(p);
917 request->data = client->vm_start + payload;
19a15b93
KH
918
919 return count;
920}
921
4f259223 922static int ioctl_start_iso(struct client *client, void *buffer)
19a15b93 923{
4f259223 924 struct fw_cdev_start_iso *request = buffer;
19a15b93 925
fae60312 926 if (client->iso_context == NULL || request->handle != 0)
abaa5743 927 return -EINVAL;
fae60312 928
eb0306ea 929 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
4f259223 930 if (request->tags == 0 || request->tags > 15)
eb0306ea
KH
931 return -EINVAL;
932
4f259223 933 if (request->sync > 15)
eb0306ea
KH
934 return -EINVAL;
935 }
936
4f259223
KH
937 return fw_iso_context_start(client->iso_context, request->cycle,
938 request->sync, request->tags);
19a15b93
KH
939}
940
4f259223 941static int ioctl_stop_iso(struct client *client, void *buffer)
b8295668 942{
abaa5743
KH
943 struct fw_cdev_stop_iso *request = buffer;
944
fae60312 945 if (client->iso_context == NULL || request->handle != 0)
abaa5743
KH
946 return -EINVAL;
947
b8295668
KH
948 return fw_iso_context_stop(client->iso_context);
949}
950
a64408b9
SR
951static int ioctl_get_cycle_timer(struct client *client, void *buffer)
952{
953 struct fw_cdev_get_cycle_timer *request = buffer;
954 struct fw_card *card = client->device->card;
955 unsigned long long bus_time;
956 struct timeval tv;
957 unsigned long flags;
958
959 preempt_disable();
960 local_irq_save(flags);
961
962 bus_time = card->driver->get_bus_time(card);
963 do_gettimeofday(&tv);
964
965 local_irq_restore(flags);
966 preempt_enable();
967
968 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
969 request->cycle_timer = bus_time & 0xffffffff;
970 return 0;
971}
972
4f259223
KH
973static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
974 ioctl_get_info,
975 ioctl_send_request,
976 ioctl_allocate,
977 ioctl_deallocate,
978 ioctl_send_response,
979 ioctl_initiate_bus_reset,
980 ioctl_add_descriptor,
981 ioctl_remove_descriptor,
982 ioctl_create_iso_context,
983 ioctl_queue_iso,
984 ioctl_start_iso,
985 ioctl_stop_iso,
a64408b9 986 ioctl_get_cycle_timer,
4f259223
KH
987};
988
53dca511
SR
989static int dispatch_ioctl(struct client *client,
990 unsigned int cmd, void __user *arg)
19a15b93 991{
4f259223 992 char buffer[256];
2dbd7d7e 993 int ret;
4f259223
KH
994
995 if (_IOC_TYPE(cmd) != '#' ||
996 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
19a15b93 997 return -EINVAL;
4f259223
KH
998
999 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2d826cc5 1000 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
4f259223
KH
1001 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
1002 return -EFAULT;
1003 }
1004
2dbd7d7e
SR
1005 ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
1006 if (ret < 0)
1007 return ret;
4f259223
KH
1008
1009 if (_IOC_DIR(cmd) & _IOC_READ) {
2d826cc5 1010 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
4f259223
KH
1011 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
1012 return -EFAULT;
19a15b93 1013 }
4f259223 1014
2dbd7d7e 1015 return ret;
19a15b93
KH
1016}
1017
53dca511
SR
1018static long fw_device_op_ioctl(struct file *file,
1019 unsigned int cmd, unsigned long arg)
19a15b93
KH
1020{
1021 struct client *client = file->private_data;
1022
551f4cb9
JF
1023 if (fw_device_is_shutdown(client->device))
1024 return -ENODEV;
1025
19a15b93
KH
1026 return dispatch_ioctl(client, cmd, (void __user *) arg);
1027}
1028
1029#ifdef CONFIG_COMPAT
53dca511
SR
1030static long fw_device_op_compat_ioctl(struct file *file,
1031 unsigned int cmd, unsigned long arg)
19a15b93
KH
1032{
1033 struct client *client = file->private_data;
1034
551f4cb9
JF
1035 if (fw_device_is_shutdown(client->device))
1036 return -ENODEV;
1037
19a15b93
KH
1038 return dispatch_ioctl(client, cmd, compat_ptr(arg));
1039}
1040#endif
1041
1042static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1043{
1044 struct client *client = file->private_data;
9aad8125
KH
1045 enum dma_data_direction direction;
1046 unsigned long size;
2dbd7d7e 1047 int page_count, ret;
9aad8125 1048
551f4cb9
JF
1049 if (fw_device_is_shutdown(client->device))
1050 return -ENODEV;
1051
9aad8125
KH
1052 /* FIXME: We could support multiple buffers, but we don't. */
1053 if (client->buffer.pages != NULL)
1054 return -EBUSY;
1055
1056 if (!(vma->vm_flags & VM_SHARED))
1057 return -EINVAL;
19a15b93 1058
9aad8125 1059 if (vma->vm_start & ~PAGE_MASK)
19a15b93
KH
1060 return -EINVAL;
1061
1062 client->vm_start = vma->vm_start;
9aad8125
KH
1063 size = vma->vm_end - vma->vm_start;
1064 page_count = size >> PAGE_SHIFT;
1065 if (size & ~PAGE_MASK)
1066 return -EINVAL;
1067
1068 if (vma->vm_flags & VM_WRITE)
1069 direction = DMA_TO_DEVICE;
1070 else
1071 direction = DMA_FROM_DEVICE;
1072
2dbd7d7e
SR
1073 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1074 page_count, direction);
1075 if (ret < 0)
1076 return ret;
19a15b93 1077
2dbd7d7e
SR
1078 ret = fw_iso_buffer_map(&client->buffer, vma);
1079 if (ret < 0)
9aad8125
KH
1080 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1081
2dbd7d7e 1082 return ret;
19a15b93
KH
1083}
1084
45ee3199
JF
1085static int shutdown_resource(int id, void *p, void *data)
1086{
1087 struct client_resource *r = p;
1088 struct client *client = data;
1089
1090 r->release(client, r);
fb443036 1091 client_put(client);
45ee3199
JF
1092
1093 return 0;
1094}
1095
19a15b93
KH
1096static int fw_device_op_release(struct inode *inode, struct file *file)
1097{
1098 struct client *client = file->private_data;
2603bf21 1099 struct event *e, *next_e;
45ee3199 1100 unsigned long flags;
19a15b93 1101
97811e34
SR
1102 mutex_lock(&client->device->client_list_mutex);
1103 list_del(&client->link);
1104 mutex_unlock(&client->device->client_list_mutex);
1105
9aad8125
KH
1106 if (client->buffer.pages)
1107 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1108
19a15b93
KH
1109 if (client->iso_context)
1110 fw_iso_context_destroy(client->iso_context);
1111
45ee3199
JF
1112 /* Freeze client->resource_idr and client->event_list */
1113 spin_lock_irqsave(&client->lock, flags);
1114 client->in_shutdown = true;
1115 spin_unlock_irqrestore(&client->lock, flags);
66dea3e5 1116
45ee3199
JF
1117 idr_for_each(&client->resource_idr, shutdown_resource, client);
1118 idr_remove_all(&client->resource_idr);
1119 idr_destroy(&client->resource_idr);
28cf6a04 1120
2603bf21
KH
1121 list_for_each_entry_safe(e, next_e, &client->event_list, link)
1122 kfree(e);
19a15b93 1123
fb443036 1124 client_put(client);
19a15b93
KH
1125
1126 return 0;
1127}
1128
1129static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1130{
1131 struct client *client = file->private_data;
2603bf21 1132 unsigned int mask = 0;
19a15b93
KH
1133
1134 poll_wait(file, &client->wait, pt);
1135
2603bf21
KH
1136 if (fw_device_is_shutdown(client->device))
1137 mask |= POLLHUP | POLLERR;
19a15b93 1138 if (!list_empty(&client->event_list))
2603bf21
KH
1139 mask |= POLLIN | POLLRDNORM;
1140
1141 return mask;
19a15b93
KH
1142}
1143
21ebcd12 1144const struct file_operations fw_device_ops = {
19a15b93
KH
1145 .owner = THIS_MODULE,
1146 .open = fw_device_op_open,
1147 .read = fw_device_op_read,
1148 .unlocked_ioctl = fw_device_op_ioctl,
1149 .poll = fw_device_op_poll,
1150 .release = fw_device_op_release,
1151 .mmap = fw_device_op_mmap,
1152
1153#ifdef CONFIG_COMPAT
5af4e5ea 1154 .compat_ioctl = fw_device_op_compat_ioctl,
19a15b93
KH
1155#endif
1156};