]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/firewire/core-cdev.c
firewire: cdev: increment ABI version number
[mirror_ubuntu-bionic-kernel.git] / drivers / firewire / core-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>
77c9a5da 25#include <linux/firewire.h>
be5bbd67
SR
26#include <linux/firewire-cdev.h>
27#include <linux/idr.h>
4a9bde9b 28#include <linux/irqflags.h>
b1bda4cd 29#include <linux/jiffies.h>
19a15b93 30#include <linux/kernel.h>
fb443036 31#include <linux/kref.h>
be5bbd67
SR
32#include <linux/mm.h>
33#include <linux/module.h>
d67cfb96 34#include <linux/mutex.h>
19a15b93 35#include <linux/poll.h>
a99bbaf5 36#include <linux/sched.h>
cf417e54 37#include <linux/spinlock.h>
281e2032 38#include <linux/string.h>
be5bbd67 39#include <linux/time.h>
e034d242 40#include <linux/uaccess.h>
be5bbd67
SR
41#include <linux/vmalloc.h>
42#include <linux/wait.h>
b1bda4cd 43#include <linux/workqueue.h>
be5bbd67 44
a64408b9 45#include <asm/system.h>
be5bbd67 46
77c9a5da 47#include "core.h"
19a15b93 48
19a15b93 49struct client {
344bbc4d 50 u32 version;
19a15b93 51 struct fw_device *device;
45ee3199 52
19a15b93 53 spinlock_t lock;
45ee3199
JF
54 bool in_shutdown;
55 struct idr resource_idr;
19a15b93 56 struct list_head event_list;
19a15b93 57 wait_queue_head_t wait;
da8ecffa 58 u64 bus_reset_closure;
9aad8125 59
19a15b93 60 struct fw_iso_context *iso_context;
abaa5743 61 u64 iso_closure;
9aad8125
KH
62 struct fw_iso_buffer buffer;
63 unsigned long vm_start;
97bd9efa
KH
64
65 struct list_head link;
fb443036 66 struct kref kref;
19a15b93
KH
67};
68
fb443036
SR
69static inline void client_get(struct client *client)
70{
71 kref_get(&client->kref);
72}
73
74static void client_release(struct kref *kref)
75{
76 struct client *client = container_of(kref, struct client, kref);
77
78 fw_device_put(client->device);
79 kfree(client);
80}
81
82static void client_put(struct client *client)
83{
84 kref_put(&client->kref, client_release);
85}
86
97c18b7f
SR
87struct client_resource;
88typedef void (*client_resource_release_fn_t)(struct client *,
89 struct client_resource *);
90struct client_resource {
91 client_resource_release_fn_t release;
92 int handle;
93};
94
95struct address_handler_resource {
96 struct client_resource resource;
97 struct fw_address_handler handler;
98 __u64 closure;
99 struct client *client;
100};
101
102struct outbound_transaction_resource {
103 struct client_resource resource;
104 struct fw_transaction transaction;
105};
106
107struct inbound_transaction_resource {
108 struct client_resource resource;
109 struct fw_request *request;
110 void *data;
111 size_t length;
112};
113
114struct descriptor_resource {
115 struct client_resource resource;
116 struct fw_descriptor descriptor;
117 u32 data[0];
118};
119
b1bda4cd
JFSR
120struct iso_resource {
121 struct client_resource resource;
122 struct client *client;
123 /* Schedule work and access todo only with client->lock held. */
124 struct delayed_work work;
1ec3c026
SR
125 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
126 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
b1bda4cd
JFSR
127 int generation;
128 u64 channels;
129 s32 bandwidth;
6fdc0370 130 __be32 transaction_data[2];
b1bda4cd
JFSR
131 struct iso_resource_event *e_alloc, *e_dealloc;
132};
133
b1bda4cd
JFSR
134static void release_iso_resource(struct client *, struct client_resource *);
135
9fb551bf
SR
136static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
137{
138 client_get(r->client);
139 if (!schedule_delayed_work(&r->work, delay))
140 client_put(r->client);
141}
142
143static void schedule_if_iso_resource(struct client_resource *resource)
144{
145 if (resource->release == release_iso_resource)
146 schedule_iso_resource(container_of(resource,
147 struct iso_resource, resource), 0);
148}
149
97c18b7f
SR
150/*
151 * dequeue_event() just kfree()'s the event, so the event has to be
152 * the first field in a struct XYZ_event.
153 */
154struct event {
155 struct { void *data; size_t size; } v[2];
156 struct list_head link;
157};
158
159struct bus_reset_event {
160 struct event event;
161 struct fw_cdev_event_bus_reset reset;
162};
163
164struct outbound_transaction_event {
165 struct event event;
166 struct client *client;
167 struct outbound_transaction_resource r;
168 struct fw_cdev_event_response response;
169};
170
171struct inbound_transaction_event {
172 struct event event;
173 struct fw_cdev_event_request request;
174};
175
176struct iso_interrupt_event {
177 struct event event;
178 struct fw_cdev_event_iso_interrupt interrupt;
179};
180
b1bda4cd
JFSR
181struct iso_resource_event {
182 struct event event;
e21fcf79 183 struct fw_cdev_event_iso_resource iso_resource;
b1bda4cd
JFSR
184};
185
53dca511 186static inline void __user *u64_to_uptr(__u64 value)
19a15b93
KH
187{
188 return (void __user *)(unsigned long)value;
189}
190
53dca511 191static inline __u64 uptr_to_u64(void __user *ptr)
19a15b93
KH
192{
193 return (__u64)(unsigned long)ptr;
194}
195
196static int fw_device_op_open(struct inode *inode, struct file *file)
197{
198 struct fw_device *device;
199 struct client *client;
200
96b19062 201 device = fw_device_get_by_devt(inode->i_rdev);
a3aca3da
KH
202 if (device == NULL)
203 return -ENODEV;
19a15b93 204
551f4cb9
JF
205 if (fw_device_is_shutdown(device)) {
206 fw_device_put(device);
207 return -ENODEV;
208 }
209
2d826cc5 210 client = kzalloc(sizeof(*client), GFP_KERNEL);
96b19062
SR
211 if (client == NULL) {
212 fw_device_put(device);
19a15b93 213 return -ENOMEM;
96b19062 214 }
19a15b93 215
96b19062 216 client->device = device;
19a15b93 217 spin_lock_init(&client->lock);
45ee3199
JF
218 idr_init(&client->resource_idr);
219 INIT_LIST_HEAD(&client->event_list);
19a15b93 220 init_waitqueue_head(&client->wait);
fb443036 221 kref_init(&client->kref);
19a15b93
KH
222
223 file->private_data = client;
224
d67cfb96 225 mutex_lock(&device->client_list_mutex);
97bd9efa 226 list_add_tail(&client->link, &device->client_list);
d67cfb96 227 mutex_unlock(&device->client_list_mutex);
97bd9efa 228
19a15b93
KH
229 return 0;
230}
231
232static void queue_event(struct client *client, struct event *event,
233 void *data0, size_t size0, void *data1, size_t size1)
234{
235 unsigned long flags;
236
237 event->v[0].data = data0;
238 event->v[0].size = size0;
239 event->v[1].data = data1;
240 event->v[1].size = size1;
241
242 spin_lock_irqsave(&client->lock, flags);
45ee3199
JF
243 if (client->in_shutdown)
244 kfree(event);
245 else
246 list_add_tail(&event->link, &client->event_list);
19a15b93 247 spin_unlock_irqrestore(&client->lock, flags);
83431cba
JF
248
249 wake_up_interruptible(&client->wait);
19a15b93
KH
250}
251
53dca511
SR
252static int dequeue_event(struct client *client,
253 char __user *buffer, size_t count)
19a15b93 254{
19a15b93
KH
255 struct event *event;
256 size_t size, total;
2dbd7d7e 257 int i, ret;
19a15b93 258
2dbd7d7e
SR
259 ret = wait_event_interruptible(client->wait,
260 !list_empty(&client->event_list) ||
261 fw_device_is_shutdown(client->device));
262 if (ret < 0)
263 return ret;
19a15b93 264
2603bf21
KH
265 if (list_empty(&client->event_list) &&
266 fw_device_is_shutdown(client->device))
267 return -ENODEV;
19a15b93 268
3ba94986 269 spin_lock_irq(&client->lock);
a459b8ab 270 event = list_first_entry(&client->event_list, struct event, link);
19a15b93 271 list_del(&event->link);
3ba94986 272 spin_unlock_irq(&client->lock);
19a15b93 273
19a15b93
KH
274 total = 0;
275 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
276 size = min(event->v[i].size, count - total);
2603bf21 277 if (copy_to_user(buffer + total, event->v[i].data, size)) {
2dbd7d7e 278 ret = -EFAULT;
19a15b93 279 goto out;
2603bf21 280 }
19a15b93
KH
281 total += size;
282 }
2dbd7d7e 283 ret = total;
19a15b93
KH
284
285 out:
286 kfree(event);
287
2dbd7d7e 288 return ret;
19a15b93
KH
289}
290
53dca511
SR
291static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
292 size_t count, loff_t *offset)
19a15b93
KH
293{
294 struct client *client = file->private_data;
295
296 return dequeue_event(client, buffer, count);
297}
298
53dca511
SR
299static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
300 struct client *client)
344bbc4d 301{
da8ecffa 302 struct fw_card *card = client->device->card;
cf417e54 303
3ba94986 304 spin_lock_irq(&card->lock);
344bbc4d 305
da8ecffa 306 event->closure = client->bus_reset_closure;
344bbc4d 307 event->type = FW_CDEV_EVENT_BUS_RESET;
cf5a56ac 308 event->generation = client->device->generation;
da8ecffa 309 event->node_id = client->device->node_id;
344bbc4d
KH
310 event->local_node_id = card->local_node->node_id;
311 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
312 event->irm_node_id = card->irm_node->node_id;
313 event->root_node_id = card->root_node->node_id;
cf417e54 314
3ba94986 315 spin_unlock_irq(&card->lock);
344bbc4d
KH
316}
317
53dca511
SR
318static void for_each_client(struct fw_device *device,
319 void (*callback)(struct client *client))
2603bf21 320{
2603bf21 321 struct client *c;
2603bf21 322
d67cfb96 323 mutex_lock(&device->client_list_mutex);
2603bf21
KH
324 list_for_each_entry(c, &device->client_list, link)
325 callback(c);
d67cfb96 326 mutex_unlock(&device->client_list_mutex);
2603bf21
KH
327}
328
b1bda4cd
JFSR
329static int schedule_reallocations(int id, void *p, void *data)
330{
9fb551bf 331 schedule_if_iso_resource(p);
b1bda4cd 332
b1bda4cd
JFSR
333 return 0;
334}
335
53dca511 336static void queue_bus_reset_event(struct client *client)
97bd9efa 337{
97c18b7f 338 struct bus_reset_event *e;
97bd9efa 339
97c18b7f
SR
340 e = kzalloc(sizeof(*e), GFP_KERNEL);
341 if (e == NULL) {
97bd9efa
KH
342 fw_notify("Out of memory when allocating bus reset event\n");
343 return;
344 }
345
97c18b7f 346 fill_bus_reset_event(&e->reset, client);
97bd9efa 347
97c18b7f
SR
348 queue_event(client, &e->event,
349 &e->reset, sizeof(e->reset), NULL, 0);
b1bda4cd
JFSR
350
351 spin_lock_irq(&client->lock);
352 idr_for_each(&client->resource_idr, schedule_reallocations, client);
353 spin_unlock_irq(&client->lock);
97bd9efa
KH
354}
355
356void fw_device_cdev_update(struct fw_device *device)
357{
2603bf21
KH
358 for_each_client(device, queue_bus_reset_event);
359}
97bd9efa 360
2603bf21
KH
361static void wake_up_client(struct client *client)
362{
363 wake_up_interruptible(&client->wait);
364}
97bd9efa 365
2603bf21
KH
366void fw_device_cdev_remove(struct fw_device *device)
367{
368 for_each_client(device, wake_up_client);
97bd9efa
KH
369}
370
4f259223 371static int ioctl_get_info(struct client *client, void *buffer)
19a15b93 372{
4f259223 373 struct fw_cdev_get_info *get_info = buffer;
344bbc4d 374 struct fw_cdev_event_bus_reset bus_reset;
c9755e14 375 unsigned long ret = 0;
344bbc4d 376
4f259223
KH
377 client->version = get_info->version;
378 get_info->version = FW_CDEV_VERSION;
cf417e54 379 get_info->card = client->device->card->index;
344bbc4d 380
c9755e14
SR
381 down_read(&fw_device_rwsem);
382
4f259223
KH
383 if (get_info->rom != 0) {
384 void __user *uptr = u64_to_uptr(get_info->rom);
385 size_t want = get_info->rom_length;
d84702a5 386 size_t have = client->device->config_rom_length * 4;
344bbc4d 387
c9755e14
SR
388 ret = copy_to_user(uptr, client->device->config_rom,
389 min(want, have));
344bbc4d 390 }
4f259223 391 get_info->rom_length = client->device->config_rom_length * 4;
344bbc4d 392
c9755e14
SR
393 up_read(&fw_device_rwsem);
394
395 if (ret != 0)
396 return -EFAULT;
397
4f259223
KH
398 client->bus_reset_closure = get_info->bus_reset_closure;
399 if (get_info->bus_reset != 0) {
400 void __user *uptr = u64_to_uptr(get_info->bus_reset);
344bbc4d 401
da8ecffa 402 fill_bus_reset_event(&bus_reset, client);
2d826cc5 403 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
344bbc4d
KH
404 return -EFAULT;
405 }
19a15b93 406
19a15b93
KH
407 return 0;
408}
409
53dca511
SR
410static int add_client_resource(struct client *client,
411 struct client_resource *resource, gfp_t gfp_mask)
3964a449
KH
412{
413 unsigned long flags;
45ee3199
JF
414 int ret;
415
416 retry:
417 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
418 return -ENOMEM;
3964a449
KH
419
420 spin_lock_irqsave(&client->lock, flags);
45ee3199
JF
421 if (client->in_shutdown)
422 ret = -ECANCELED;
423 else
424 ret = idr_get_new(&client->resource_idr, resource,
425 &resource->handle);
b1bda4cd 426 if (ret >= 0) {
fb443036 427 client_get(client);
9fb551bf 428 schedule_if_iso_resource(resource);
b1bda4cd 429 }
3964a449 430 spin_unlock_irqrestore(&client->lock, flags);
45ee3199
JF
431
432 if (ret == -EAGAIN)
433 goto retry;
434
435 return ret < 0 ? ret : 0;
3964a449
KH
436}
437
53dca511
SR
438static int release_client_resource(struct client *client, u32 handle,
439 client_resource_release_fn_t release,
e21fcf79 440 struct client_resource **return_resource)
3964a449 441{
e21fcf79 442 struct client_resource *resource;
3964a449 443
3ba94986 444 spin_lock_irq(&client->lock);
45ee3199 445 if (client->in_shutdown)
e21fcf79 446 resource = NULL;
45ee3199 447 else
e21fcf79
SR
448 resource = idr_find(&client->resource_idr, handle);
449 if (resource && resource->release == release)
45ee3199 450 idr_remove(&client->resource_idr, handle);
3ba94986 451 spin_unlock_irq(&client->lock);
3964a449 452
e21fcf79 453 if (!(resource && resource->release == release))
3964a449
KH
454 return -EINVAL;
455
e21fcf79
SR
456 if (return_resource)
457 *return_resource = resource;
3964a449 458 else
e21fcf79 459 resource->release(client, resource);
3964a449 460
fb443036
SR
461 client_put(client);
462
3964a449
KH
463 return 0;
464}
465
53dca511
SR
466static void release_transaction(struct client *client,
467 struct client_resource *resource)
3964a449 468{
97c18b7f
SR
469 struct outbound_transaction_resource *r = container_of(resource,
470 struct outbound_transaction_resource, resource);
3964a449 471
97c18b7f 472 fw_cancel_transaction(client->device->card, &r->transaction);
3964a449
KH
473}
474
53dca511
SR
475static void complete_transaction(struct fw_card *card, int rcode,
476 void *payload, size_t length, void *data)
19a15b93 477{
97c18b7f
SR
478 struct outbound_transaction_event *e = data;
479 struct fw_cdev_event_response *rsp = &e->response;
480 struct client *client = e->client;
28cf6a04 481 unsigned long flags;
19a15b93 482
97c18b7f
SR
483 if (length < rsp->length)
484 rsp->length = length;
19a15b93 485 if (rcode == RCODE_COMPLETE)
97c18b7f 486 memcpy(rsp->data, payload, rsp->length);
19a15b93 487
28cf6a04 488 spin_lock_irqsave(&client->lock, flags);
45ee3199 489 /*
fb443036
SR
490 * 1. If called while in shutdown, the idr tree must be left untouched.
491 * The idr handle will be removed and the client reference will be
492 * dropped later.
493 * 2. If the call chain was release_client_resource ->
494 * release_transaction -> complete_transaction (instead of a normal
495 * conclusion of the transaction), i.e. if this resource was already
496 * unregistered from the idr, the client reference will be dropped
497 * by release_client_resource and we must not drop it here.
45ee3199 498 */
fb443036 499 if (!client->in_shutdown &&
97c18b7f
SR
500 idr_find(&client->resource_idr, e->r.resource.handle)) {
501 idr_remove(&client->resource_idr, e->r.resource.handle);
fb443036
SR
502 /* Drop the idr's reference */
503 client_put(client);
504 }
28cf6a04
KH
505 spin_unlock_irqrestore(&client->lock, flags);
506
97c18b7f
SR
507 rsp->type = FW_CDEV_EVENT_RESPONSE;
508 rsp->rcode = rcode;
8401d92b
DM
509
510 /*
97c18b7f 511 * In the case that sizeof(*rsp) doesn't align with the position of the
8401d92b
DM
512 * data, and the read is short, preserve an extra copy of the data
513 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
514 * for short reads and some apps depended on it, this is both safe
515 * and prudent for compatibility.
516 */
97c18b7f
SR
517 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
518 queue_event(client, &e->event, rsp, sizeof(*rsp),
519 rsp->data, rsp->length);
8401d92b 520 else
97c18b7f 521 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
8401d92b 522 NULL, 0);
fb443036
SR
523
524 /* Drop the transaction callback's reference */
525 client_put(client);
19a15b93
KH
526}
527
acfe8333
JFSR
528static int init_request(struct client *client,
529 struct fw_cdev_send_request *request,
530 int destination_id, int speed)
19a15b93 531{
97c18b7f 532 struct outbound_transaction_event *e;
1f3125af 533 int ret;
19a15b93 534
18e9b10f
SR
535 if (request->tcode != TCODE_STREAM_DATA &&
536 (request->length > 4096 || request->length > 512 << speed))
5d3fd692 537 return -EIO;
19a15b93 538
97c18b7f
SR
539 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
540 if (e == NULL)
19a15b93
KH
541 return -ENOMEM;
542
97c18b7f
SR
543 e->client = client;
544 e->response.length = request->length;
545 e->response.closure = request->closure;
19a15b93 546
4f259223 547 if (request->data &&
97c18b7f 548 copy_from_user(e->response.data,
4f259223 549 u64_to_uptr(request->data), request->length)) {
1f3125af 550 ret = -EFAULT;
45ee3199 551 goto failed;
1f3125af
SR
552 }
553
97c18b7f
SR
554 e->r.resource.release = release_transaction;
555 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
45ee3199
JF
556 if (ret < 0)
557 goto failed;
28cf6a04 558
fb443036
SR
559 /* Get a reference for the transaction callback */
560 client_get(client);
561
acfe8333 562 fw_send_request(client->device->card, &e->r.transaction,
664d8010
SR
563 request->tcode, destination_id, request->generation,
564 speed, request->offset, e->response.data,
565 request->length, complete_transaction, e);
566 return 0;
19a15b93 567
45ee3199 568 failed:
97c18b7f 569 kfree(e);
1f3125af
SR
570
571 return ret;
19a15b93
KH
572}
573
acfe8333
JFSR
574static int ioctl_send_request(struct client *client, void *buffer)
575{
576 struct fw_cdev_send_request *request = buffer;
577
578 switch (request->tcode) {
579 case TCODE_WRITE_QUADLET_REQUEST:
580 case TCODE_WRITE_BLOCK_REQUEST:
581 case TCODE_READ_QUADLET_REQUEST:
582 case TCODE_READ_BLOCK_REQUEST:
583 case TCODE_LOCK_MASK_SWAP:
584 case TCODE_LOCK_COMPARE_SWAP:
585 case TCODE_LOCK_FETCH_ADD:
586 case TCODE_LOCK_LITTLE_ADD:
587 case TCODE_LOCK_BOUNDED_ADD:
588 case TCODE_LOCK_WRAP_ADD:
589 case TCODE_LOCK_VENDOR_DEPENDENT:
590 break;
591 default:
592 return -EINVAL;
593 }
594
207fbefb 595 return init_request(client, request, client->device->node_id,
acfe8333
JFSR
596 client->device->max_speed);
597}
598
281e2032
SR
599static inline bool is_fcp_request(struct fw_request *request)
600{
601 return request == NULL;
602}
603
53dca511
SR
604static void release_request(struct client *client,
605 struct client_resource *resource)
3964a449 606{
97c18b7f
SR
607 struct inbound_transaction_resource *r = container_of(resource,
608 struct inbound_transaction_resource, resource);
3964a449 609
281e2032
SR
610 if (is_fcp_request(r->request))
611 kfree(r->data);
612 else
db5d247a
CL
613 fw_send_response(client->device->card, r->request,
614 RCODE_CONFLICT_ERROR);
97c18b7f 615 kfree(r);
3964a449
KH
616}
617
97c18b7f 618static void handle_request(struct fw_card *card, struct fw_request *request,
53dca511
SR
619 int tcode, int destination, int source,
620 int generation, int speed,
621 unsigned long long offset,
622 void *payload, size_t length, void *callback_data)
19a15b93 623{
97c18b7f
SR
624 struct address_handler_resource *handler = callback_data;
625 struct inbound_transaction_resource *r;
626 struct inbound_transaction_event *e;
281e2032 627 void *fcp_frame = NULL;
45ee3199 628 int ret;
19a15b93 629
97c18b7f 630 r = kmalloc(sizeof(*r), GFP_ATOMIC);
2d826cc5 631 e = kmalloc(sizeof(*e), GFP_ATOMIC);
97c18b7f 632 if (r == NULL || e == NULL)
45ee3199 633 goto failed;
19a15b93 634
97c18b7f
SR
635 r->request = request;
636 r->data = payload;
637 r->length = length;
19a15b93 638
281e2032
SR
639 if (is_fcp_request(request)) {
640 /*
641 * FIXME: Let core-transaction.c manage a
642 * single reference-counted copy?
643 */
644 fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
645 if (fcp_frame == NULL)
646 goto failed;
647
648 r->data = fcp_frame;
649 }
650
97c18b7f
SR
651 r->resource.release = release_request;
652 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
45ee3199
JF
653 if (ret < 0)
654 goto failed;
19a15b93
KH
655
656 e->request.type = FW_CDEV_EVENT_REQUEST;
657 e->request.tcode = tcode;
658 e->request.offset = offset;
659 e->request.length = length;
97c18b7f 660 e->request.handle = r->resource.handle;
19a15b93
KH
661 e->request.closure = handler->closure;
662
97c18b7f 663 queue_event(handler->client, &e->event,
281e2032 664 &e->request, sizeof(e->request), r->data, length);
45ee3199
JF
665 return;
666
667 failed:
97c18b7f 668 kfree(r);
45ee3199 669 kfree(e);
281e2032
SR
670 kfree(fcp_frame);
671
672 if (!is_fcp_request(request))
db5d247a 673 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
19a15b93
KH
674}
675
53dca511
SR
676static void release_address_handler(struct client *client,
677 struct client_resource *resource)
3964a449 678{
97c18b7f
SR
679 struct address_handler_resource *r =
680 container_of(resource, struct address_handler_resource, resource);
3964a449 681
97c18b7f
SR
682 fw_core_remove_address_handler(&r->handler);
683 kfree(r);
3964a449
KH
684}
685
4f259223 686static int ioctl_allocate(struct client *client, void *buffer)
19a15b93 687{
4f259223 688 struct fw_cdev_allocate *request = buffer;
97c18b7f 689 struct address_handler_resource *r;
19a15b93 690 struct fw_address_region region;
45ee3199 691 int ret;
19a15b93 692
97c18b7f
SR
693 r = kmalloc(sizeof(*r), GFP_KERNEL);
694 if (r == NULL)
19a15b93
KH
695 return -ENOMEM;
696
4f259223
KH
697 region.start = request->offset;
698 region.end = request->offset + request->length;
97c18b7f
SR
699 r->handler.length = request->length;
700 r->handler.address_callback = handle_request;
701 r->handler.callback_data = r;
702 r->closure = request->closure;
703 r->client = client;
19a15b93 704
97c18b7f 705 ret = fw_core_add_address_handler(&r->handler, &region);
3e0b5f0d 706 if (ret < 0) {
97c18b7f 707 kfree(r);
3e0b5f0d 708 return ret;
19a15b93
KH
709 }
710
97c18b7f
SR
711 r->resource.release = release_address_handler;
712 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
45ee3199 713 if (ret < 0) {
97c18b7f 714 release_address_handler(client, &r->resource);
45ee3199
JF
715 return ret;
716 }
97c18b7f 717 request->handle = r->resource.handle;
19a15b93
KH
718
719 return 0;
720}
721
4f259223 722static int ioctl_deallocate(struct client *client, void *buffer)
9472316b 723{
4f259223 724 struct fw_cdev_deallocate *request = buffer;
9472316b 725
45ee3199
JF
726 return release_client_resource(client, request->handle,
727 release_address_handler, NULL);
9472316b
KH
728}
729
4f259223 730static int ioctl_send_response(struct client *client, void *buffer)
19a15b93 731{
4f259223 732 struct fw_cdev_send_response *request = buffer;
3964a449 733 struct client_resource *resource;
97c18b7f 734 struct inbound_transaction_resource *r;
7e44c0b5 735 int ret = 0;
19a15b93 736
45ee3199
JF
737 if (release_client_resource(client, request->handle,
738 release_request, &resource) < 0)
19a15b93 739 return -EINVAL;
45ee3199 740
97c18b7f
SR
741 r = container_of(resource, struct inbound_transaction_resource,
742 resource);
281e2032
SR
743 if (is_fcp_request(r->request))
744 goto out;
745
746 if (request->length < r->length)
747 r->length = request->length;
748 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length)) {
749 ret = -EFAULT;
750 kfree(r->request);
751 goto out;
7e44c0b5 752 }
281e2032 753 fw_send_response(client->device->card, r->request, request->rcode);
7e44c0b5 754 out:
19a15b93
KH
755 kfree(r);
756
7e44c0b5 757 return ret;
19a15b93
KH
758}
759
4f259223 760static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
5371842b 761{
4f259223 762 struct fw_cdev_initiate_bus_reset *request = buffer;
5371842b
KH
763 int short_reset;
764
4f259223 765 short_reset = (request->type == FW_CDEV_SHORT_RESET);
5371842b
KH
766
767 return fw_core_initiate_bus_reset(client->device->card, short_reset);
768}
769
3964a449
KH
770static void release_descriptor(struct client *client,
771 struct client_resource *resource)
772{
97c18b7f
SR
773 struct descriptor_resource *r =
774 container_of(resource, struct descriptor_resource, resource);
3964a449 775
97c18b7f
SR
776 fw_core_remove_descriptor(&r->descriptor);
777 kfree(r);
3964a449
KH
778}
779
4f259223 780static int ioctl_add_descriptor(struct client *client, void *buffer)
66dea3e5 781{
4f259223 782 struct fw_cdev_add_descriptor *request = buffer;
97c18b7f 783 struct descriptor_resource *r;
45ee3199 784 int ret;
66dea3e5 785
de487da8 786 /* Access policy: Allow this ioctl only on local nodes' device files. */
92368890 787 if (!client->device->is_local)
de487da8
SR
788 return -ENOSYS;
789
4f259223 790 if (request->length > 256)
66dea3e5
KH
791 return -EINVAL;
792
97c18b7f
SR
793 r = kmalloc(sizeof(*r) + request->length * 4, GFP_KERNEL);
794 if (r == NULL)
66dea3e5
KH
795 return -ENOMEM;
796
97c18b7f 797 if (copy_from_user(r->data,
4f259223 798 u64_to_uptr(request->data), request->length * 4)) {
45ee3199
JF
799 ret = -EFAULT;
800 goto failed;
66dea3e5
KH
801 }
802
97c18b7f
SR
803 r->descriptor.length = request->length;
804 r->descriptor.immediate = request->immediate;
805 r->descriptor.key = request->key;
806 r->descriptor.data = r->data;
66dea3e5 807
97c18b7f 808 ret = fw_core_add_descriptor(&r->descriptor);
45ee3199
JF
809 if (ret < 0)
810 goto failed;
66dea3e5 811
97c18b7f
SR
812 r->resource.release = release_descriptor;
813 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
45ee3199 814 if (ret < 0) {
97c18b7f 815 fw_core_remove_descriptor(&r->descriptor);
45ee3199
JF
816 goto failed;
817 }
97c18b7f 818 request->handle = r->resource.handle;
66dea3e5
KH
819
820 return 0;
45ee3199 821 failed:
97c18b7f 822 kfree(r);
45ee3199
JF
823
824 return ret;
66dea3e5
KH
825}
826
4f259223 827static int ioctl_remove_descriptor(struct client *client, void *buffer)
66dea3e5 828{
4f259223 829 struct fw_cdev_remove_descriptor *request = buffer;
66dea3e5 830
45ee3199
JF
831 return release_client_resource(client, request->handle,
832 release_descriptor, NULL);
66dea3e5
KH
833}
834
53dca511
SR
835static void iso_callback(struct fw_iso_context *context, u32 cycle,
836 size_t header_length, void *header, void *data)
19a15b93
KH
837{
838 struct client *client = data;
97c18b7f 839 struct iso_interrupt_event *e;
19a15b93 840
97c18b7f
SR
841 e = kzalloc(sizeof(*e) + header_length, GFP_ATOMIC);
842 if (e == NULL)
19a15b93
KH
843 return;
844
97c18b7f
SR
845 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
846 e->interrupt.closure = client->iso_closure;
847 e->interrupt.cycle = cycle;
848 e->interrupt.header_length = header_length;
849 memcpy(e->interrupt.header, header, header_length);
850 queue_event(client, &e->event, &e->interrupt,
851 sizeof(e->interrupt) + header_length, NULL, 0);
19a15b93
KH
852}
853
4f259223 854static int ioctl_create_iso_context(struct client *client, void *buffer)
19a15b93 855{
4f259223 856 struct fw_cdev_create_iso_context *request = buffer;
24315c5e 857 struct fw_iso_context *context;
19a15b93 858
fae60312
SR
859 /* We only support one context at this time. */
860 if (client->iso_context != NULL)
861 return -EBUSY;
862
4f259223 863 if (request->channel > 63)
21efb3cf
KH
864 return -EINVAL;
865
4f259223 866 switch (request->type) {
c70dc788 867 case FW_ISO_CONTEXT_RECEIVE:
4f259223 868 if (request->header_size < 4 || (request->header_size & 3))
c70dc788 869 return -EINVAL;
98b6cbe8 870
c70dc788
KH
871 break;
872
873 case FW_ISO_CONTEXT_TRANSMIT:
4f259223 874 if (request->speed > SCODE_3200)
c70dc788
KH
875 return -EINVAL;
876
877 break;
878
879 default:
21efb3cf 880 return -EINVAL;
c70dc788
KH
881 }
882
24315c5e
KH
883 context = fw_iso_context_create(client->device->card,
884 request->type,
885 request->channel,
886 request->speed,
887 request->header_size,
888 iso_callback, client);
889 if (IS_ERR(context))
890 return PTR_ERR(context);
891
abaa5743 892 client->iso_closure = request->closure;
24315c5e 893 client->iso_context = context;
19a15b93 894
abaa5743
KH
895 /* We only support one context at this time. */
896 request->handle = 0;
897
19a15b93
KH
898 return 0;
899}
900
1ca31ae7
KH
901/* Macros for decoding the iso packet control header. */
902#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
903#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
904#define GET_SKIP(v) (((v) >> 17) & 0x01)
7a100344
SR
905#define GET_TAG(v) (((v) >> 18) & 0x03)
906#define GET_SY(v) (((v) >> 20) & 0x0f)
1ca31ae7
KH
907#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
908
4f259223 909static int ioctl_queue_iso(struct client *client, void *buffer)
19a15b93 910{
4f259223 911 struct fw_cdev_queue_iso *request = buffer;
19a15b93 912 struct fw_cdev_iso_packet __user *p, *end, *next;
9b32d5f3 913 struct fw_iso_context *ctx = client->iso_context;
ef370ee7 914 unsigned long payload, buffer_end, header_length;
1ca31ae7 915 u32 control;
19a15b93
KH
916 int count;
917 struct {
918 struct fw_iso_packet packet;
919 u8 header[256];
920 } u;
921
abaa5743 922 if (ctx == NULL || request->handle != 0)
19a15b93 923 return -EINVAL;
19a15b93 924
c781c06d
KH
925 /*
926 * If the user passes a non-NULL data pointer, has mmap()'ed
19a15b93
KH
927 * the iso buffer, and the pointer points inside the buffer,
928 * we setup the payload pointers accordingly. Otherwise we
9aad8125 929 * set them both to 0, which will still let packets with
19a15b93
KH
930 * payload_length == 0 through. In other words, if no packets
931 * use the indirect payload, the iso buffer need not be mapped
c781c06d
KH
932 * and the request->data pointer is ignored.
933 */
19a15b93 934
4f259223 935 payload = (unsigned long)request->data - client->vm_start;
ef370ee7 936 buffer_end = client->buffer.page_count << PAGE_SHIFT;
4f259223 937 if (request->data == 0 || client->buffer.pages == NULL ||
ef370ee7 938 payload >= buffer_end) {
9aad8125 939 payload = 0;
ef370ee7 940 buffer_end = 0;
19a15b93
KH
941 }
942
1ccc9147
AV
943 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
944
945 if (!access_ok(VERIFY_READ, p, request->size))
19a15b93
KH
946 return -EFAULT;
947
4f259223 948 end = (void __user *)p + request->size;
19a15b93
KH
949 count = 0;
950 while (p < end) {
1ca31ae7 951 if (get_user(control, &p->control))
19a15b93 952 return -EFAULT;
1ca31ae7
KH
953 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
954 u.packet.interrupt = GET_INTERRUPT(control);
955 u.packet.skip = GET_SKIP(control);
956 u.packet.tag = GET_TAG(control);
957 u.packet.sy = GET_SY(control);
958 u.packet.header_length = GET_HEADER_LENGTH(control);
295e3feb 959
9b32d5f3 960 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
295e3feb
KH
961 header_length = u.packet.header_length;
962 } else {
c781c06d
KH
963 /*
964 * We require that header_length is a multiple of
965 * the fixed header size, ctx->header_size.
966 */
9b32d5f3
KH
967 if (ctx->header_size == 0) {
968 if (u.packet.header_length > 0)
969 return -EINVAL;
970 } else if (u.packet.header_length % ctx->header_size != 0) {
295e3feb 971 return -EINVAL;
9b32d5f3 972 }
295e3feb
KH
973 header_length = 0;
974 }
975
19a15b93 976 next = (struct fw_cdev_iso_packet __user *)
295e3feb 977 &p->header[header_length / 4];
19a15b93
KH
978 if (next > end)
979 return -EINVAL;
980 if (__copy_from_user
295e3feb 981 (u.packet.header, p->header, header_length))
19a15b93 982 return -EFAULT;
98b6cbe8 983 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
19a15b93
KH
984 u.packet.header_length + u.packet.payload_length > 0)
985 return -EINVAL;
ef370ee7 986 if (payload + u.packet.payload_length > buffer_end)
19a15b93
KH
987 return -EINVAL;
988
9b32d5f3
KH
989 if (fw_iso_context_queue(ctx, &u.packet,
990 &client->buffer, payload))
19a15b93
KH
991 break;
992
993 p = next;
994 payload += u.packet.payload_length;
995 count++;
996 }
997
4f259223
KH
998 request->size -= uptr_to_u64(p) - request->packets;
999 request->packets = uptr_to_u64(p);
1000 request->data = client->vm_start + payload;
19a15b93
KH
1001
1002 return count;
1003}
1004
4f259223 1005static int ioctl_start_iso(struct client *client, void *buffer)
19a15b93 1006{
4f259223 1007 struct fw_cdev_start_iso *request = buffer;
19a15b93 1008
fae60312 1009 if (client->iso_context == NULL || request->handle != 0)
abaa5743 1010 return -EINVAL;
fae60312 1011
eb0306ea 1012 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
4f259223 1013 if (request->tags == 0 || request->tags > 15)
eb0306ea
KH
1014 return -EINVAL;
1015
4f259223 1016 if (request->sync > 15)
eb0306ea
KH
1017 return -EINVAL;
1018 }
1019
4f259223
KH
1020 return fw_iso_context_start(client->iso_context, request->cycle,
1021 request->sync, request->tags);
19a15b93
KH
1022}
1023
4f259223 1024static int ioctl_stop_iso(struct client *client, void *buffer)
b8295668 1025{
abaa5743
KH
1026 struct fw_cdev_stop_iso *request = buffer;
1027
fae60312 1028 if (client->iso_context == NULL || request->handle != 0)
abaa5743
KH
1029 return -EINVAL;
1030
b8295668
KH
1031 return fw_iso_context_stop(client->iso_context);
1032}
1033
abfe5a01 1034static int ioctl_get_cycle_timer2(struct client *client, void *buffer)
a64408b9 1035{
abfe5a01 1036 struct fw_cdev_get_cycle_timer2 *request = buffer;
a64408b9 1037 struct fw_card *card = client->device->card;
abfe5a01 1038 struct timespec ts = {0, 0};
4a9bde9b 1039 u32 cycle_time;
abfe5a01 1040 int ret = 0;
a64408b9 1041
4a9bde9b 1042 local_irq_disable();
a64408b9 1043
168cf9af 1044 cycle_time = card->driver->get_cycle_time(card);
abfe5a01
SR
1045
1046 switch (request->clk_id) {
1047 case CLOCK_REALTIME: getnstimeofday(&ts); break;
1048 case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
1049 case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
1050 default:
1051 ret = -EINVAL;
1052 }
a64408b9 1053
4a9bde9b 1054 local_irq_enable();
a64408b9 1055
abfe5a01
SR
1056 request->tv_sec = ts.tv_sec;
1057 request->tv_nsec = ts.tv_nsec;
1058 request->cycle_timer = cycle_time;
1059
1060 return ret;
1061}
1062
1063static int ioctl_get_cycle_timer(struct client *client, void *buffer)
1064{
1065 struct fw_cdev_get_cycle_timer *request = buffer;
1066 struct fw_cdev_get_cycle_timer2 ct2;
1067
1068 ct2.clk_id = CLOCK_REALTIME;
1069 ioctl_get_cycle_timer2(client, &ct2);
1070
1071 request->local_time = ct2.tv_sec * USEC_PER_SEC +
1072 ct2.tv_nsec / NSEC_PER_USEC;
1073 request->cycle_timer = ct2.cycle_timer;
4a9bde9b 1074
a64408b9
SR
1075 return 0;
1076}
1077
b1bda4cd
JFSR
1078static void iso_resource_work(struct work_struct *work)
1079{
1080 struct iso_resource_event *e;
1081 struct iso_resource *r =
1082 container_of(work, struct iso_resource, work.work);
1083 struct client *client = r->client;
1084 int generation, channel, bandwidth, todo;
1085 bool skip, free, success;
1086
1087 spin_lock_irq(&client->lock);
1088 generation = client->device->generation;
1089 todo = r->todo;
1090 /* Allow 1000ms grace period for other reallocations. */
1091 if (todo == ISO_RES_ALLOC &&
1092 time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
9fb551bf 1093 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
b1bda4cd
JFSR
1094 skip = true;
1095 } else {
1096 /* We could be called twice within the same generation. */
1097 skip = todo == ISO_RES_REALLOC &&
1098 r->generation == generation;
1099 }
1ec3c026
SR
1100 free = todo == ISO_RES_DEALLOC ||
1101 todo == ISO_RES_ALLOC_ONCE ||
1102 todo == ISO_RES_DEALLOC_ONCE;
b1bda4cd
JFSR
1103 r->generation = generation;
1104 spin_unlock_irq(&client->lock);
1105
1106 if (skip)
1107 goto out;
1108
1109 bandwidth = r->bandwidth;
1110
1111 fw_iso_resource_manage(client->device->card, generation,
1112 r->channels, &channel, &bandwidth,
1ec3c026
SR
1113 todo == ISO_RES_ALLOC ||
1114 todo == ISO_RES_REALLOC ||
6fdc0370
SR
1115 todo == ISO_RES_ALLOC_ONCE,
1116 r->transaction_data);
b1bda4cd
JFSR
1117 /*
1118 * Is this generation outdated already? As long as this resource sticks
1119 * in the idr, it will be scheduled again for a newer generation or at
1120 * shutdown.
1121 */
1122 if (channel == -EAGAIN &&
1123 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1124 goto out;
1125
1126 success = channel >= 0 || bandwidth > 0;
1127
1128 spin_lock_irq(&client->lock);
1129 /*
1130 * Transit from allocation to reallocation, except if the client
1131 * requested deallocation in the meantime.
1132 */
1133 if (r->todo == ISO_RES_ALLOC)
1134 r->todo = ISO_RES_REALLOC;
1135 /*
1136 * Allocation or reallocation failure? Pull this resource out of the
1137 * idr and prepare for deletion, unless the client is shutting down.
1138 */
1139 if (r->todo == ISO_RES_REALLOC && !success &&
1140 !client->in_shutdown &&
1141 idr_find(&client->resource_idr, r->resource.handle)) {
1142 idr_remove(&client->resource_idr, r->resource.handle);
1143 client_put(client);
1144 free = true;
1145 }
1146 spin_unlock_irq(&client->lock);
1147
1148 if (todo == ISO_RES_ALLOC && channel >= 0)
5d9cb7d2 1149 r->channels = 1ULL << channel;
b1bda4cd
JFSR
1150
1151 if (todo == ISO_RES_REALLOC && success)
1152 goto out;
1153
1ec3c026 1154 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
b1bda4cd
JFSR
1155 e = r->e_alloc;
1156 r->e_alloc = NULL;
1157 } else {
1158 e = r->e_dealloc;
1159 r->e_dealloc = NULL;
1160 }
e21fcf79
SR
1161 e->iso_resource.handle = r->resource.handle;
1162 e->iso_resource.channel = channel;
1163 e->iso_resource.bandwidth = bandwidth;
b1bda4cd
JFSR
1164
1165 queue_event(client, &e->event,
e21fcf79 1166 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
b1bda4cd
JFSR
1167
1168 if (free) {
1169 cancel_delayed_work(&r->work);
1170 kfree(r->e_alloc);
1171 kfree(r->e_dealloc);
1172 kfree(r);
1173 }
1174 out:
1175 client_put(client);
1176}
1177
b1bda4cd
JFSR
1178static void release_iso_resource(struct client *client,
1179 struct client_resource *resource)
1180{
1181 struct iso_resource *r =
1182 container_of(resource, struct iso_resource, resource);
1183
1184 spin_lock_irq(&client->lock);
1185 r->todo = ISO_RES_DEALLOC;
9fb551bf 1186 schedule_iso_resource(r, 0);
b1bda4cd
JFSR
1187 spin_unlock_irq(&client->lock);
1188}
1189
1ec3c026
SR
1190static int init_iso_resource(struct client *client,
1191 struct fw_cdev_allocate_iso_resource *request, int todo)
b1bda4cd 1192{
b1bda4cd
JFSR
1193 struct iso_resource_event *e1, *e2;
1194 struct iso_resource *r;
1195 int ret;
1196
1197 if ((request->channels == 0 && request->bandwidth == 0) ||
1198 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1199 request->bandwidth < 0)
1200 return -EINVAL;
1201
1202 r = kmalloc(sizeof(*r), GFP_KERNEL);
1203 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1204 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1205 if (r == NULL || e1 == NULL || e2 == NULL) {
1206 ret = -ENOMEM;
1207 goto fail;
1208 }
1209
1210 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1211 r->client = client;
1ec3c026 1212 r->todo = todo;
b1bda4cd
JFSR
1213 r->generation = -1;
1214 r->channels = request->channels;
1215 r->bandwidth = request->bandwidth;
1216 r->e_alloc = e1;
1217 r->e_dealloc = e2;
1218
e21fcf79
SR
1219 e1->iso_resource.closure = request->closure;
1220 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1221 e2->iso_resource.closure = request->closure;
1222 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
b1bda4cd 1223
1ec3c026
SR
1224 if (todo == ISO_RES_ALLOC) {
1225 r->resource.release = release_iso_resource;
1226 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
81610b8f
SR
1227 if (ret < 0)
1228 goto fail;
1ec3c026
SR
1229 } else {
1230 r->resource.release = NULL;
1231 r->resource.handle = -1;
9fb551bf 1232 schedule_iso_resource(r, 0);
1ec3c026 1233 }
b1bda4cd
JFSR
1234 request->handle = r->resource.handle;
1235
1236 return 0;
1237 fail:
1238 kfree(r);
1239 kfree(e1);
1240 kfree(e2);
1241
1242 return ret;
1243}
1244
1ec3c026
SR
1245static int ioctl_allocate_iso_resource(struct client *client, void *buffer)
1246{
1247 struct fw_cdev_allocate_iso_resource *request = buffer;
1248
1249 return init_iso_resource(client, request, ISO_RES_ALLOC);
1250}
1251
b1bda4cd
JFSR
1252static int ioctl_deallocate_iso_resource(struct client *client, void *buffer)
1253{
1254 struct fw_cdev_deallocate *request = buffer;
1255
1256 return release_client_resource(client, request->handle,
1257 release_iso_resource, NULL);
1258}
1259
1ec3c026
SR
1260static int ioctl_allocate_iso_resource_once(struct client *client, void *buffer)
1261{
1262 struct fw_cdev_allocate_iso_resource *request = buffer;
1263
1264 return init_iso_resource(client, request, ISO_RES_ALLOC_ONCE);
1265}
1266
1267static int ioctl_deallocate_iso_resource_once(struct client *client, void *buffer)
1268{
1269 struct fw_cdev_allocate_iso_resource *request = buffer;
1270
1271 return init_iso_resource(client, request, ISO_RES_DEALLOC_ONCE);
1272}
1273
c8a25900
SR
1274/*
1275 * Returns a speed code: Maximum speed to or from this device,
1276 * limited by the device's link speed, the local node's link speed,
1277 * and all PHY port speeds between the two links.
1278 */
33580a3e
SR
1279static int ioctl_get_speed(struct client *client, void *buffer)
1280{
c8a25900 1281 return client->device->max_speed;
33580a3e
SR
1282}
1283
acfe8333
JFSR
1284static int ioctl_send_broadcast_request(struct client *client, void *buffer)
1285{
1286 struct fw_cdev_send_request *request = buffer;
1287
1288 switch (request->tcode) {
1289 case TCODE_WRITE_QUADLET_REQUEST:
1290 case TCODE_WRITE_BLOCK_REQUEST:
1291 break;
1292 default:
1293 return -EINVAL;
1294 }
1295
1566f3dc
SR
1296 /* Security policy: Only allow accesses to Units Space. */
1297 if (request->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
1298 return -EACCES;
1299
acfe8333
JFSR
1300 return init_request(client, request, LOCAL_BUS | 0x3f, SCODE_100);
1301}
1302
f8c2287c
JF
1303static int ioctl_send_stream_packet(struct client *client, void *buffer)
1304{
18e9b10f
SR
1305 struct fw_cdev_send_stream_packet *p = buffer;
1306 struct fw_cdev_send_request request;
1307 int dest;
f8c2287c 1308
18e9b10f
SR
1309 if (p->speed > client->device->card->link_speed ||
1310 p->length > 1024 << p->speed)
1311 return -EIO;
f8c2287c 1312
18e9b10f
SR
1313 if (p->tag > 3 || p->channel > 63 || p->sy > 15)
1314 return -EINVAL;
1315
1316 dest = fw_stream_packet_destination_id(p->tag, p->channel, p->sy);
1317 request.tcode = TCODE_STREAM_DATA;
1318 request.length = p->length;
1319 request.closure = p->closure;
1320 request.data = p->data;
1321 request.generation = p->generation;
1322
1323 return init_request(client, &request, dest, p->speed);
f8c2287c
JF
1324}
1325
4f259223
KH
1326static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
1327 ioctl_get_info,
1328 ioctl_send_request,
1329 ioctl_allocate,
1330 ioctl_deallocate,
1331 ioctl_send_response,
1332 ioctl_initiate_bus_reset,
1333 ioctl_add_descriptor,
1334 ioctl_remove_descriptor,
1335 ioctl_create_iso_context,
1336 ioctl_queue_iso,
1337 ioctl_start_iso,
1338 ioctl_stop_iso,
a64408b9 1339 ioctl_get_cycle_timer,
b1bda4cd
JFSR
1340 ioctl_allocate_iso_resource,
1341 ioctl_deallocate_iso_resource,
1ec3c026
SR
1342 ioctl_allocate_iso_resource_once,
1343 ioctl_deallocate_iso_resource_once,
33580a3e 1344 ioctl_get_speed,
acfe8333 1345 ioctl_send_broadcast_request,
f8c2287c 1346 ioctl_send_stream_packet,
abfe5a01 1347 ioctl_get_cycle_timer2,
4f259223
KH
1348};
1349
53dca511
SR
1350static int dispatch_ioctl(struct client *client,
1351 unsigned int cmd, void __user *arg)
19a15b93 1352{
b2c0a2ac
SR
1353 char buffer[sizeof(union {
1354 struct fw_cdev_get_info _00;
1355 struct fw_cdev_send_request _01;
1356 struct fw_cdev_allocate _02;
1357 struct fw_cdev_deallocate _03;
1358 struct fw_cdev_send_response _04;
1359 struct fw_cdev_initiate_bus_reset _05;
1360 struct fw_cdev_add_descriptor _06;
1361 struct fw_cdev_remove_descriptor _07;
1362 struct fw_cdev_create_iso_context _08;
1363 struct fw_cdev_queue_iso _09;
1364 struct fw_cdev_start_iso _0a;
1365 struct fw_cdev_stop_iso _0b;
1366 struct fw_cdev_get_cycle_timer _0c;
1367 struct fw_cdev_allocate_iso_resource _0d;
1368 struct fw_cdev_send_stream_packet _13;
abfe5a01 1369 struct fw_cdev_get_cycle_timer2 _14;
b2c0a2ac 1370 })];
2dbd7d7e 1371 int ret;
4f259223
KH
1372
1373 if (_IOC_TYPE(cmd) != '#' ||
1374 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
19a15b93 1375 return -EINVAL;
4f259223
KH
1376
1377 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2d826cc5 1378 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
4f259223
KH
1379 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
1380 return -EFAULT;
1381 }
1382
2dbd7d7e
SR
1383 ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
1384 if (ret < 0)
1385 return ret;
4f259223
KH
1386
1387 if (_IOC_DIR(cmd) & _IOC_READ) {
2d826cc5 1388 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
4f259223
KH
1389 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
1390 return -EFAULT;
19a15b93 1391 }
4f259223 1392
2dbd7d7e 1393 return ret;
19a15b93
KH
1394}
1395
53dca511
SR
1396static long fw_device_op_ioctl(struct file *file,
1397 unsigned int cmd, unsigned long arg)
19a15b93
KH
1398{
1399 struct client *client = file->private_data;
1400
551f4cb9
JF
1401 if (fw_device_is_shutdown(client->device))
1402 return -ENODEV;
1403
19a15b93
KH
1404 return dispatch_ioctl(client, cmd, (void __user *) arg);
1405}
1406
1407#ifdef CONFIG_COMPAT
53dca511
SR
1408static long fw_device_op_compat_ioctl(struct file *file,
1409 unsigned int cmd, unsigned long arg)
19a15b93
KH
1410{
1411 struct client *client = file->private_data;
1412
551f4cb9
JF
1413 if (fw_device_is_shutdown(client->device))
1414 return -ENODEV;
1415
19a15b93
KH
1416 return dispatch_ioctl(client, cmd, compat_ptr(arg));
1417}
1418#endif
1419
1420static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1421{
1422 struct client *client = file->private_data;
9aad8125
KH
1423 enum dma_data_direction direction;
1424 unsigned long size;
2dbd7d7e 1425 int page_count, ret;
9aad8125 1426
551f4cb9
JF
1427 if (fw_device_is_shutdown(client->device))
1428 return -ENODEV;
1429
9aad8125
KH
1430 /* FIXME: We could support multiple buffers, but we don't. */
1431 if (client->buffer.pages != NULL)
1432 return -EBUSY;
1433
1434 if (!(vma->vm_flags & VM_SHARED))
1435 return -EINVAL;
19a15b93 1436
9aad8125 1437 if (vma->vm_start & ~PAGE_MASK)
19a15b93
KH
1438 return -EINVAL;
1439
1440 client->vm_start = vma->vm_start;
9aad8125
KH
1441 size = vma->vm_end - vma->vm_start;
1442 page_count = size >> PAGE_SHIFT;
1443 if (size & ~PAGE_MASK)
1444 return -EINVAL;
1445
1446 if (vma->vm_flags & VM_WRITE)
1447 direction = DMA_TO_DEVICE;
1448 else
1449 direction = DMA_FROM_DEVICE;
1450
2dbd7d7e
SR
1451 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1452 page_count, direction);
1453 if (ret < 0)
1454 return ret;
19a15b93 1455
2dbd7d7e
SR
1456 ret = fw_iso_buffer_map(&client->buffer, vma);
1457 if (ret < 0)
9aad8125
KH
1458 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1459
2dbd7d7e 1460 return ret;
19a15b93
KH
1461}
1462
45ee3199
JF
1463static int shutdown_resource(int id, void *p, void *data)
1464{
e21fcf79 1465 struct client_resource *resource = p;
45ee3199
JF
1466 struct client *client = data;
1467
e21fcf79 1468 resource->release(client, resource);
fb443036 1469 client_put(client);
45ee3199
JF
1470
1471 return 0;
1472}
1473
19a15b93
KH
1474static int fw_device_op_release(struct inode *inode, struct file *file)
1475{
1476 struct client *client = file->private_data;
e21fcf79 1477 struct event *event, *next_event;
19a15b93 1478
97811e34
SR
1479 mutex_lock(&client->device->client_list_mutex);
1480 list_del(&client->link);
1481 mutex_unlock(&client->device->client_list_mutex);
1482
19a15b93
KH
1483 if (client->iso_context)
1484 fw_iso_context_destroy(client->iso_context);
1485
36a755cf
SR
1486 if (client->buffer.pages)
1487 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1488
45ee3199 1489 /* Freeze client->resource_idr and client->event_list */
3ba94986 1490 spin_lock_irq(&client->lock);
45ee3199 1491 client->in_shutdown = true;
3ba94986 1492 spin_unlock_irq(&client->lock);
66dea3e5 1493
45ee3199
JF
1494 idr_for_each(&client->resource_idr, shutdown_resource, client);
1495 idr_remove_all(&client->resource_idr);
1496 idr_destroy(&client->resource_idr);
28cf6a04 1497
e21fcf79
SR
1498 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1499 kfree(event);
19a15b93 1500
fb443036 1501 client_put(client);
19a15b93
KH
1502
1503 return 0;
1504}
1505
1506static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1507{
1508 struct client *client = file->private_data;
2603bf21 1509 unsigned int mask = 0;
19a15b93
KH
1510
1511 poll_wait(file, &client->wait, pt);
1512
2603bf21
KH
1513 if (fw_device_is_shutdown(client->device))
1514 mask |= POLLHUP | POLLERR;
19a15b93 1515 if (!list_empty(&client->event_list))
2603bf21
KH
1516 mask |= POLLIN | POLLRDNORM;
1517
1518 return mask;
19a15b93
KH
1519}
1520
21ebcd12 1521const struct file_operations fw_device_ops = {
19a15b93
KH
1522 .owner = THIS_MODULE,
1523 .open = fw_device_op_open,
1524 .read = fw_device_op_read,
1525 .unlocked_ioctl = fw_device_op_ioctl,
1526 .poll = fw_device_op_poll,
1527 .release = fw_device_op_release,
1528 .mmap = fw_device_op_mmap,
1529
1530#ifdef CONFIG_COMPAT
5af4e5ea 1531 .compat_ioctl = fw_device_op_compat_ioctl,
19a15b93
KH
1532#endif
1533};