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