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