firewire: cdev: count references of cards during inbound transactions
[firewire-audio.git] / drivers / firewire / core-cdev.c
blob8f8c8eeaf046eb8080f2118cd723fd2dd02ab688
1 /*
2 * Char device for device raw access
4 * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
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.
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.
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.
21 #include <linux/compat.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/errno.h>
25 #include <linux/firewire.h>
26 #include <linux/firewire-cdev.h>
27 #include <linux/idr.h>
28 #include <linux/irqflags.h>
29 #include <linux/jiffies.h>
30 #include <linux/kernel.h>
31 #include <linux/kref.h>
32 #include <linux/mm.h>
33 #include <linux/module.h>
34 #include <linux/mutex.h>
35 #include <linux/poll.h>
36 #include <linux/sched.h>
37 #include <linux/spinlock.h>
38 #include <linux/string.h>
39 #include <linux/time.h>
40 #include <linux/uaccess.h>
41 #include <linux/vmalloc.h>
42 #include <linux/wait.h>
43 #include <linux/workqueue.h>
45 #include <asm/system.h>
47 #include "core.h"
49 struct client {
50 u32 version;
51 struct fw_device *device;
53 spinlock_t lock;
54 bool in_shutdown;
55 struct idr resource_idr;
56 struct list_head event_list;
57 wait_queue_head_t wait;
58 u64 bus_reset_closure;
60 struct fw_iso_context *iso_context;
61 u64 iso_closure;
62 struct fw_iso_buffer buffer;
63 unsigned long vm_start;
65 struct list_head link;
66 struct kref kref;
69 static inline void client_get(struct client *client)
71 kref_get(&client->kref);
74 static void client_release(struct kref *kref)
76 struct client *client = container_of(kref, struct client, kref);
78 fw_device_put(client->device);
79 kfree(client);
82 static void client_put(struct client *client)
84 kref_put(&client->kref, client_release);
87 struct client_resource;
88 typedef void (*client_resource_release_fn_t)(struct client *,
89 struct client_resource *);
90 struct client_resource {
91 client_resource_release_fn_t release;
92 int handle;
95 struct address_handler_resource {
96 struct client_resource resource;
97 struct fw_address_handler handler;
98 __u64 closure;
99 struct client *client;
102 struct outbound_transaction_resource {
103 struct client_resource resource;
104 struct fw_transaction transaction;
107 struct inbound_transaction_resource {
108 struct client_resource resource;
109 struct fw_card *card;
110 struct fw_request *request;
111 void *data;
112 size_t length;
115 struct descriptor_resource {
116 struct client_resource resource;
117 struct fw_descriptor descriptor;
118 u32 data[0];
121 struct iso_resource {
122 struct client_resource resource;
123 struct client *client;
124 /* Schedule work and access todo only with client->lock held. */
125 struct delayed_work work;
126 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
127 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
128 int generation;
129 u64 channels;
130 s32 bandwidth;
131 __be32 transaction_data[2];
132 struct iso_resource_event *e_alloc, *e_dealloc;
135 static void release_iso_resource(struct client *, struct client_resource *);
137 static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
139 client_get(r->client);
140 if (!schedule_delayed_work(&r->work, delay))
141 client_put(r->client);
144 static void schedule_if_iso_resource(struct client_resource *resource)
146 if (resource->release == release_iso_resource)
147 schedule_iso_resource(container_of(resource,
148 struct iso_resource, resource), 0);
152 * dequeue_event() just kfree()'s the event, so the event has to be
153 * the first field in a struct XYZ_event.
155 struct event {
156 struct { void *data; size_t size; } v[2];
157 struct list_head link;
160 struct bus_reset_event {
161 struct event event;
162 struct fw_cdev_event_bus_reset reset;
165 struct outbound_transaction_event {
166 struct event event;
167 struct client *client;
168 struct outbound_transaction_resource r;
169 struct fw_cdev_event_response response;
172 struct inbound_transaction_event {
173 struct event event;
174 struct fw_cdev_event_request request;
177 struct iso_interrupt_event {
178 struct event event;
179 struct fw_cdev_event_iso_interrupt interrupt;
182 struct iso_resource_event {
183 struct event event;
184 struct fw_cdev_event_iso_resource iso_resource;
187 static inline void __user *u64_to_uptr(__u64 value)
189 return (void __user *)(unsigned long)value;
192 static inline __u64 uptr_to_u64(void __user *ptr)
194 return (__u64)(unsigned long)ptr;
197 static int fw_device_op_open(struct inode *inode, struct file *file)
199 struct fw_device *device;
200 struct client *client;
202 device = fw_device_get_by_devt(inode->i_rdev);
203 if (device == NULL)
204 return -ENODEV;
206 if (fw_device_is_shutdown(device)) {
207 fw_device_put(device);
208 return -ENODEV;
211 client = kzalloc(sizeof(*client), GFP_KERNEL);
212 if (client == NULL) {
213 fw_device_put(device);
214 return -ENOMEM;
217 client->device = device;
218 spin_lock_init(&client->lock);
219 idr_init(&client->resource_idr);
220 INIT_LIST_HEAD(&client->event_list);
221 init_waitqueue_head(&client->wait);
222 kref_init(&client->kref);
224 file->private_data = client;
226 mutex_lock(&device->client_list_mutex);
227 list_add_tail(&client->link, &device->client_list);
228 mutex_unlock(&device->client_list_mutex);
230 return nonseekable_open(inode, file);
233 static void queue_event(struct client *client, struct event *event,
234 void *data0, size_t size0, void *data1, size_t size1)
236 unsigned long flags;
238 event->v[0].data = data0;
239 event->v[0].size = size0;
240 event->v[1].data = data1;
241 event->v[1].size = size1;
243 spin_lock_irqsave(&client->lock, flags);
244 if (client->in_shutdown)
245 kfree(event);
246 else
247 list_add_tail(&event->link, &client->event_list);
248 spin_unlock_irqrestore(&client->lock, flags);
250 wake_up_interruptible(&client->wait);
253 static int dequeue_event(struct client *client,
254 char __user *buffer, size_t count)
256 struct event *event;
257 size_t size, total;
258 int i, ret;
260 ret = wait_event_interruptible(client->wait,
261 !list_empty(&client->event_list) ||
262 fw_device_is_shutdown(client->device));
263 if (ret < 0)
264 return ret;
266 if (list_empty(&client->event_list) &&
267 fw_device_is_shutdown(client->device))
268 return -ENODEV;
270 spin_lock_irq(&client->lock);
271 event = list_first_entry(&client->event_list, struct event, link);
272 list_del(&event->link);
273 spin_unlock_irq(&client->lock);
275 total = 0;
276 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
277 size = min(event->v[i].size, count - total);
278 if (copy_to_user(buffer + total, event->v[i].data, size)) {
279 ret = -EFAULT;
280 goto out;
282 total += size;
284 ret = total;
286 out:
287 kfree(event);
289 return ret;
292 static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
293 size_t count, loff_t *offset)
295 struct client *client = file->private_data;
297 return dequeue_event(client, buffer, count);
300 static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
301 struct client *client)
303 struct fw_card *card = client->device->card;
305 spin_lock_irq(&card->lock);
307 event->closure = client->bus_reset_closure;
308 event->type = FW_CDEV_EVENT_BUS_RESET;
309 event->generation = client->device->generation;
310 event->node_id = client->device->node_id;
311 event->local_node_id = card->local_node->node_id;
312 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
313 event->irm_node_id = card->irm_node->node_id;
314 event->root_node_id = card->root_node->node_id;
316 spin_unlock_irq(&card->lock);
319 static void for_each_client(struct fw_device *device,
320 void (*callback)(struct client *client))
322 struct client *c;
324 mutex_lock(&device->client_list_mutex);
325 list_for_each_entry(c, &device->client_list, link)
326 callback(c);
327 mutex_unlock(&device->client_list_mutex);
330 static int schedule_reallocations(int id, void *p, void *data)
332 schedule_if_iso_resource(p);
334 return 0;
337 static void queue_bus_reset_event(struct client *client)
339 struct bus_reset_event *e;
341 e = kzalloc(sizeof(*e), GFP_KERNEL);
342 if (e == NULL) {
343 fw_notify("Out of memory when allocating bus reset event\n");
344 return;
347 fill_bus_reset_event(&e->reset, client);
349 queue_event(client, &e->event,
350 &e->reset, sizeof(e->reset), NULL, 0);
352 spin_lock_irq(&client->lock);
353 idr_for_each(&client->resource_idr, schedule_reallocations, client);
354 spin_unlock_irq(&client->lock);
357 void fw_device_cdev_update(struct fw_device *device)
359 for_each_client(device, queue_bus_reset_event);
362 static void wake_up_client(struct client *client)
364 wake_up_interruptible(&client->wait);
367 void fw_device_cdev_remove(struct fw_device *device)
369 for_each_client(device, wake_up_client);
372 union ioctl_arg {
373 struct fw_cdev_get_info get_info;
374 struct fw_cdev_send_request send_request;
375 struct fw_cdev_allocate allocate;
376 struct fw_cdev_deallocate deallocate;
377 struct fw_cdev_send_response send_response;
378 struct fw_cdev_initiate_bus_reset initiate_bus_reset;
379 struct fw_cdev_add_descriptor add_descriptor;
380 struct fw_cdev_remove_descriptor remove_descriptor;
381 struct fw_cdev_create_iso_context create_iso_context;
382 struct fw_cdev_queue_iso queue_iso;
383 struct fw_cdev_start_iso start_iso;
384 struct fw_cdev_stop_iso stop_iso;
385 struct fw_cdev_get_cycle_timer get_cycle_timer;
386 struct fw_cdev_allocate_iso_resource allocate_iso_resource;
387 struct fw_cdev_send_stream_packet send_stream_packet;
388 struct fw_cdev_get_cycle_timer2 get_cycle_timer2;
391 static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
393 struct fw_cdev_get_info *a = &arg->get_info;
394 struct fw_cdev_event_bus_reset bus_reset;
395 unsigned long ret = 0;
397 client->version = a->version;
398 a->version = FW_CDEV_VERSION;
399 a->card = client->device->card->index;
401 down_read(&fw_device_rwsem);
403 if (a->rom != 0) {
404 size_t want = a->rom_length;
405 size_t have = client->device->config_rom_length * 4;
407 ret = copy_to_user(u64_to_uptr(a->rom),
408 client->device->config_rom, min(want, have));
410 a->rom_length = client->device->config_rom_length * 4;
412 up_read(&fw_device_rwsem);
414 if (ret != 0)
415 return -EFAULT;
417 client->bus_reset_closure = a->bus_reset_closure;
418 if (a->bus_reset != 0) {
419 fill_bus_reset_event(&bus_reset, client);
420 if (copy_to_user(u64_to_uptr(a->bus_reset),
421 &bus_reset, sizeof(bus_reset)))
422 return -EFAULT;
425 return 0;
428 static int add_client_resource(struct client *client,
429 struct client_resource *resource, gfp_t gfp_mask)
431 unsigned long flags;
432 int ret;
434 retry:
435 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
436 return -ENOMEM;
438 spin_lock_irqsave(&client->lock, flags);
439 if (client->in_shutdown)
440 ret = -ECANCELED;
441 else
442 ret = idr_get_new(&client->resource_idr, resource,
443 &resource->handle);
444 if (ret >= 0) {
445 client_get(client);
446 schedule_if_iso_resource(resource);
448 spin_unlock_irqrestore(&client->lock, flags);
450 if (ret == -EAGAIN)
451 goto retry;
453 return ret < 0 ? ret : 0;
456 static int release_client_resource(struct client *client, u32 handle,
457 client_resource_release_fn_t release,
458 struct client_resource **return_resource)
460 struct client_resource *resource;
462 spin_lock_irq(&client->lock);
463 if (client->in_shutdown)
464 resource = NULL;
465 else
466 resource = idr_find(&client->resource_idr, handle);
467 if (resource && resource->release == release)
468 idr_remove(&client->resource_idr, handle);
469 spin_unlock_irq(&client->lock);
471 if (!(resource && resource->release == release))
472 return -EINVAL;
474 if (return_resource)
475 *return_resource = resource;
476 else
477 resource->release(client, resource);
479 client_put(client);
481 return 0;
484 static void release_transaction(struct client *client,
485 struct client_resource *resource)
487 struct outbound_transaction_resource *r = container_of(resource,
488 struct outbound_transaction_resource, resource);
490 fw_cancel_transaction(client->device->card, &r->transaction);
493 static void complete_transaction(struct fw_card *card, int rcode,
494 void *payload, size_t length, void *data)
496 struct outbound_transaction_event *e = data;
497 struct fw_cdev_event_response *rsp = &e->response;
498 struct client *client = e->client;
499 unsigned long flags;
501 if (length < rsp->length)
502 rsp->length = length;
503 if (rcode == RCODE_COMPLETE)
504 memcpy(rsp->data, payload, rsp->length);
506 spin_lock_irqsave(&client->lock, flags);
508 * 1. If called while in shutdown, the idr tree must be left untouched.
509 * The idr handle will be removed and the client reference will be
510 * dropped later.
511 * 2. If the call chain was release_client_resource ->
512 * release_transaction -> complete_transaction (instead of a normal
513 * conclusion of the transaction), i.e. if this resource was already
514 * unregistered from the idr, the client reference will be dropped
515 * by release_client_resource and we must not drop it here.
517 if (!client->in_shutdown &&
518 idr_find(&client->resource_idr, e->r.resource.handle)) {
519 idr_remove(&client->resource_idr, e->r.resource.handle);
520 /* Drop the idr's reference */
521 client_put(client);
523 spin_unlock_irqrestore(&client->lock, flags);
525 rsp->type = FW_CDEV_EVENT_RESPONSE;
526 rsp->rcode = rcode;
529 * In the case that sizeof(*rsp) doesn't align with the position of the
530 * data, and the read is short, preserve an extra copy of the data
531 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
532 * for short reads and some apps depended on it, this is both safe
533 * and prudent for compatibility.
535 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
536 queue_event(client, &e->event, rsp, sizeof(*rsp),
537 rsp->data, rsp->length);
538 else
539 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
540 NULL, 0);
542 /* Drop the transaction callback's reference */
543 client_put(client);
546 static int init_request(struct client *client,
547 struct fw_cdev_send_request *request,
548 int destination_id, int speed)
550 struct outbound_transaction_event *e;
551 int ret;
553 if (request->tcode != TCODE_STREAM_DATA &&
554 (request->length > 4096 || request->length > 512 << speed))
555 return -EIO;
557 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
558 if (e == NULL)
559 return -ENOMEM;
561 e->client = client;
562 e->response.length = request->length;
563 e->response.closure = request->closure;
565 if (request->data &&
566 copy_from_user(e->response.data,
567 u64_to_uptr(request->data), request->length)) {
568 ret = -EFAULT;
569 goto failed;
572 e->r.resource.release = release_transaction;
573 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
574 if (ret < 0)
575 goto failed;
577 /* Get a reference for the transaction callback */
578 client_get(client);
580 fw_send_request(client->device->card, &e->r.transaction,
581 request->tcode, destination_id, request->generation,
582 speed, request->offset, e->response.data,
583 request->length, complete_transaction, e);
584 return 0;
586 failed:
587 kfree(e);
589 return ret;
592 static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
594 switch (arg->send_request.tcode) {
595 case TCODE_WRITE_QUADLET_REQUEST:
596 case TCODE_WRITE_BLOCK_REQUEST:
597 case TCODE_READ_QUADLET_REQUEST:
598 case TCODE_READ_BLOCK_REQUEST:
599 case TCODE_LOCK_MASK_SWAP:
600 case TCODE_LOCK_COMPARE_SWAP:
601 case TCODE_LOCK_FETCH_ADD:
602 case TCODE_LOCK_LITTLE_ADD:
603 case TCODE_LOCK_BOUNDED_ADD:
604 case TCODE_LOCK_WRAP_ADD:
605 case TCODE_LOCK_VENDOR_DEPENDENT:
606 break;
607 default:
608 return -EINVAL;
611 return init_request(client, &arg->send_request, client->device->node_id,
612 client->device->max_speed);
615 static inline bool is_fcp_request(struct fw_request *request)
617 return request == NULL;
620 static void release_request(struct client *client,
621 struct client_resource *resource)
623 struct inbound_transaction_resource *r = container_of(resource,
624 struct inbound_transaction_resource, resource);
626 if (is_fcp_request(r->request))
627 kfree(r->data);
628 else
629 fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
631 fw_card_put(r->card);
632 kfree(r);
635 static void handle_request(struct fw_card *card, struct fw_request *request,
636 int tcode, int destination, int source,
637 int generation, unsigned long long offset,
638 void *payload, size_t length, void *callback_data)
640 struct address_handler_resource *handler = callback_data;
641 struct inbound_transaction_resource *r;
642 struct inbound_transaction_event *e;
643 void *fcp_frame = NULL;
644 int ret;
646 /* card may be different from handler->client->device->card */
647 fw_card_get(card);
649 r = kmalloc(sizeof(*r), GFP_ATOMIC);
650 e = kmalloc(sizeof(*e), GFP_ATOMIC);
651 if (r == NULL || e == NULL)
652 goto failed;
654 r->card = card;
655 r->request = request;
656 r->data = payload;
657 r->length = length;
659 if (is_fcp_request(request)) {
661 * FIXME: Let core-transaction.c manage a
662 * single reference-counted copy?
664 fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
665 if (fcp_frame == NULL)
666 goto failed;
668 r->data = fcp_frame;
671 r->resource.release = release_request;
672 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
673 if (ret < 0)
674 goto failed;
676 e->request.type = FW_CDEV_EVENT_REQUEST;
677 e->request.tcode = tcode;
678 e->request.offset = offset;
679 e->request.length = length;
680 e->request.handle = r->resource.handle;
681 e->request.closure = handler->closure;
683 queue_event(handler->client, &e->event,
684 &e->request, sizeof(e->request), r->data, length);
685 return;
687 failed:
688 kfree(r);
689 kfree(e);
690 kfree(fcp_frame);
692 if (!is_fcp_request(request))
693 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
695 fw_card_put(card);
698 static void release_address_handler(struct client *client,
699 struct client_resource *resource)
701 struct address_handler_resource *r =
702 container_of(resource, struct address_handler_resource, resource);
704 fw_core_remove_address_handler(&r->handler);
705 kfree(r);
708 static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
710 struct fw_cdev_allocate *a = &arg->allocate;
711 struct address_handler_resource *r;
712 struct fw_address_region region;
713 int ret;
715 r = kmalloc(sizeof(*r), GFP_KERNEL);
716 if (r == NULL)
717 return -ENOMEM;
719 region.start = a->offset;
720 region.end = a->offset + a->length;
721 r->handler.length = a->length;
722 r->handler.address_callback = handle_request;
723 r->handler.callback_data = r;
724 r->closure = a->closure;
725 r->client = client;
727 ret = fw_core_add_address_handler(&r->handler, &region);
728 if (ret < 0) {
729 kfree(r);
730 return ret;
733 r->resource.release = release_address_handler;
734 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
735 if (ret < 0) {
736 release_address_handler(client, &r->resource);
737 return ret;
739 a->handle = r->resource.handle;
741 return 0;
744 static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
746 return release_client_resource(client, arg->deallocate.handle,
747 release_address_handler, NULL);
750 static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
752 struct fw_cdev_send_response *a = &arg->send_response;
753 struct client_resource *resource;
754 struct inbound_transaction_resource *r;
755 int ret = 0;
757 if (release_client_resource(client, a->handle,
758 release_request, &resource) < 0)
759 return -EINVAL;
761 r = container_of(resource, struct inbound_transaction_resource,
762 resource);
763 if (is_fcp_request(r->request))
764 goto out;
766 if (a->length != fw_get_response_length(r->request)) {
767 ret = -EINVAL;
768 kfree(r->request);
769 goto out;
771 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
772 ret = -EFAULT;
773 kfree(r->request);
774 goto out;
776 fw_send_response(r->card, r->request, a->rcode);
777 out:
778 fw_card_put(r->card);
779 kfree(r);
781 return ret;
784 static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
786 return fw_core_initiate_bus_reset(client->device->card,
787 arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
790 static void release_descriptor(struct client *client,
791 struct client_resource *resource)
793 struct descriptor_resource *r =
794 container_of(resource, struct descriptor_resource, resource);
796 fw_core_remove_descriptor(&r->descriptor);
797 kfree(r);
800 static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
802 struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
803 struct descriptor_resource *r;
804 int ret;
806 /* Access policy: Allow this ioctl only on local nodes' device files. */
807 if (!client->device->is_local)
808 return -ENOSYS;
810 if (a->length > 256)
811 return -EINVAL;
813 r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
814 if (r == NULL)
815 return -ENOMEM;
817 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
818 ret = -EFAULT;
819 goto failed;
822 r->descriptor.length = a->length;
823 r->descriptor.immediate = a->immediate;
824 r->descriptor.key = a->key;
825 r->descriptor.data = r->data;
827 ret = fw_core_add_descriptor(&r->descriptor);
828 if (ret < 0)
829 goto failed;
831 r->resource.release = release_descriptor;
832 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
833 if (ret < 0) {
834 fw_core_remove_descriptor(&r->descriptor);
835 goto failed;
837 a->handle = r->resource.handle;
839 return 0;
840 failed:
841 kfree(r);
843 return ret;
846 static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
848 return release_client_resource(client, arg->remove_descriptor.handle,
849 release_descriptor, NULL);
852 static void iso_callback(struct fw_iso_context *context, u32 cycle,
853 size_t header_length, void *header, void *data)
855 struct client *client = data;
856 struct iso_interrupt_event *e;
858 e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
859 if (e == NULL)
860 return;
862 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
863 e->interrupt.closure = client->iso_closure;
864 e->interrupt.cycle = cycle;
865 e->interrupt.header_length = header_length;
866 memcpy(e->interrupt.header, header, header_length);
867 queue_event(client, &e->event, &e->interrupt,
868 sizeof(e->interrupt) + header_length, NULL, 0);
871 static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
873 struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
874 struct fw_iso_context *context;
876 if (a->channel > 63)
877 return -EINVAL;
879 switch (a->type) {
880 case FW_ISO_CONTEXT_RECEIVE:
881 if (a->header_size < 4 || (a->header_size & 3))
882 return -EINVAL;
883 break;
885 case FW_ISO_CONTEXT_TRANSMIT:
886 if (a->speed > SCODE_3200)
887 return -EINVAL;
888 break;
890 default:
891 return -EINVAL;
894 context = fw_iso_context_create(client->device->card, a->type,
895 a->channel, a->speed, a->header_size,
896 iso_callback, client);
897 if (IS_ERR(context))
898 return PTR_ERR(context);
900 /* We only support one context at this time. */
901 spin_lock_irq(&client->lock);
902 if (client->iso_context != NULL) {
903 spin_unlock_irq(&client->lock);
904 fw_iso_context_destroy(context);
905 return -EBUSY;
907 client->iso_closure = a->closure;
908 client->iso_context = context;
909 spin_unlock_irq(&client->lock);
911 a->handle = 0;
913 return 0;
916 /* Macros for decoding the iso packet control header. */
917 #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
918 #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
919 #define GET_SKIP(v) (((v) >> 17) & 0x01)
920 #define GET_TAG(v) (((v) >> 18) & 0x03)
921 #define GET_SY(v) (((v) >> 20) & 0x0f)
922 #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
924 static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
926 struct fw_cdev_queue_iso *a = &arg->queue_iso;
927 struct fw_cdev_iso_packet __user *p, *end, *next;
928 struct fw_iso_context *ctx = client->iso_context;
929 unsigned long payload, buffer_end, header_length;
930 u32 control;
931 int count;
932 struct {
933 struct fw_iso_packet packet;
934 u8 header[256];
935 } u;
937 if (ctx == NULL || a->handle != 0)
938 return -EINVAL;
941 * If the user passes a non-NULL data pointer, has mmap()'ed
942 * the iso buffer, and the pointer points inside the buffer,
943 * we setup the payload pointers accordingly. Otherwise we
944 * set them both to 0, which will still let packets with
945 * payload_length == 0 through. In other words, if no packets
946 * use the indirect payload, the iso buffer need not be mapped
947 * and the a->data pointer is ignored.
950 payload = (unsigned long)a->data - client->vm_start;
951 buffer_end = client->buffer.page_count << PAGE_SHIFT;
952 if (a->data == 0 || client->buffer.pages == NULL ||
953 payload >= buffer_end) {
954 payload = 0;
955 buffer_end = 0;
958 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
960 if (!access_ok(VERIFY_READ, p, a->size))
961 return -EFAULT;
963 end = (void __user *)p + a->size;
964 count = 0;
965 while (p < end) {
966 if (get_user(control, &p->control))
967 return -EFAULT;
968 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
969 u.packet.interrupt = GET_INTERRUPT(control);
970 u.packet.skip = GET_SKIP(control);
971 u.packet.tag = GET_TAG(control);
972 u.packet.sy = GET_SY(control);
973 u.packet.header_length = GET_HEADER_LENGTH(control);
975 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
976 if (u.packet.header_length % 4 != 0)
977 return -EINVAL;
978 header_length = u.packet.header_length;
979 } else {
981 * We require that header_length is a multiple of
982 * the fixed header size, ctx->header_size.
984 if (ctx->header_size == 0) {
985 if (u.packet.header_length > 0)
986 return -EINVAL;
987 } else if (u.packet.header_length == 0 ||
988 u.packet.header_length % ctx->header_size != 0) {
989 return -EINVAL;
991 header_length = 0;
994 next = (struct fw_cdev_iso_packet __user *)
995 &p->header[header_length / 4];
996 if (next > end)
997 return -EINVAL;
998 if (__copy_from_user
999 (u.packet.header, p->header, header_length))
1000 return -EFAULT;
1001 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
1002 u.packet.header_length + u.packet.payload_length > 0)
1003 return -EINVAL;
1004 if (payload + u.packet.payload_length > buffer_end)
1005 return -EINVAL;
1007 if (fw_iso_context_queue(ctx, &u.packet,
1008 &client->buffer, payload))
1009 break;
1011 p = next;
1012 payload += u.packet.payload_length;
1013 count++;
1016 a->size -= uptr_to_u64(p) - a->packets;
1017 a->packets = uptr_to_u64(p);
1018 a->data = client->vm_start + payload;
1020 return count;
1023 static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
1025 struct fw_cdev_start_iso *a = &arg->start_iso;
1027 if (client->iso_context == NULL || a->handle != 0)
1028 return -EINVAL;
1030 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
1031 (a->tags == 0 || a->tags > 15 || a->sync > 15))
1032 return -EINVAL;
1034 return fw_iso_context_start(client->iso_context,
1035 a->cycle, a->sync, a->tags);
1038 static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
1040 struct fw_cdev_stop_iso *a = &arg->stop_iso;
1042 if (client->iso_context == NULL || a->handle != 0)
1043 return -EINVAL;
1045 return fw_iso_context_stop(client->iso_context);
1048 static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
1050 struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
1051 struct fw_card *card = client->device->card;
1052 struct timespec ts = {0, 0};
1053 u32 cycle_time;
1054 int ret = 0;
1056 local_irq_disable();
1058 cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
1060 switch (a->clk_id) {
1061 case CLOCK_REALTIME: getnstimeofday(&ts); break;
1062 case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
1063 case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
1064 default:
1065 ret = -EINVAL;
1068 local_irq_enable();
1070 a->tv_sec = ts.tv_sec;
1071 a->tv_nsec = ts.tv_nsec;
1072 a->cycle_timer = cycle_time;
1074 return ret;
1077 static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
1079 struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
1080 struct fw_cdev_get_cycle_timer2 ct2;
1082 ct2.clk_id = CLOCK_REALTIME;
1083 ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
1085 a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
1086 a->cycle_timer = ct2.cycle_timer;
1088 return 0;
1091 static void iso_resource_work(struct work_struct *work)
1093 struct iso_resource_event *e;
1094 struct iso_resource *r =
1095 container_of(work, struct iso_resource, work.work);
1096 struct client *client = r->client;
1097 int generation, channel, bandwidth, todo;
1098 bool skip, free, success;
1100 spin_lock_irq(&client->lock);
1101 generation = client->device->generation;
1102 todo = r->todo;
1103 /* Allow 1000ms grace period for other reallocations. */
1104 if (todo == ISO_RES_ALLOC &&
1105 time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
1106 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
1107 skip = true;
1108 } else {
1109 /* We could be called twice within the same generation. */
1110 skip = todo == ISO_RES_REALLOC &&
1111 r->generation == generation;
1113 free = todo == ISO_RES_DEALLOC ||
1114 todo == ISO_RES_ALLOC_ONCE ||
1115 todo == ISO_RES_DEALLOC_ONCE;
1116 r->generation = generation;
1117 spin_unlock_irq(&client->lock);
1119 if (skip)
1120 goto out;
1122 bandwidth = r->bandwidth;
1124 fw_iso_resource_manage(client->device->card, generation,
1125 r->channels, &channel, &bandwidth,
1126 todo == ISO_RES_ALLOC ||
1127 todo == ISO_RES_REALLOC ||
1128 todo == ISO_RES_ALLOC_ONCE,
1129 r->transaction_data);
1131 * Is this generation outdated already? As long as this resource sticks
1132 * in the idr, it will be scheduled again for a newer generation or at
1133 * shutdown.
1135 if (channel == -EAGAIN &&
1136 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1137 goto out;
1139 success = channel >= 0 || bandwidth > 0;
1141 spin_lock_irq(&client->lock);
1143 * Transit from allocation to reallocation, except if the client
1144 * requested deallocation in the meantime.
1146 if (r->todo == ISO_RES_ALLOC)
1147 r->todo = ISO_RES_REALLOC;
1149 * Allocation or reallocation failure? Pull this resource out of the
1150 * idr and prepare for deletion, unless the client is shutting down.
1152 if (r->todo == ISO_RES_REALLOC && !success &&
1153 !client->in_shutdown &&
1154 idr_find(&client->resource_idr, r->resource.handle)) {
1155 idr_remove(&client->resource_idr, r->resource.handle);
1156 client_put(client);
1157 free = true;
1159 spin_unlock_irq(&client->lock);
1161 if (todo == ISO_RES_ALLOC && channel >= 0)
1162 r->channels = 1ULL << channel;
1164 if (todo == ISO_RES_REALLOC && success)
1165 goto out;
1167 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
1168 e = r->e_alloc;
1169 r->e_alloc = NULL;
1170 } else {
1171 e = r->e_dealloc;
1172 r->e_dealloc = NULL;
1174 e->iso_resource.handle = r->resource.handle;
1175 e->iso_resource.channel = channel;
1176 e->iso_resource.bandwidth = bandwidth;
1178 queue_event(client, &e->event,
1179 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
1181 if (free) {
1182 cancel_delayed_work(&r->work);
1183 kfree(r->e_alloc);
1184 kfree(r->e_dealloc);
1185 kfree(r);
1187 out:
1188 client_put(client);
1191 static void release_iso_resource(struct client *client,
1192 struct client_resource *resource)
1194 struct iso_resource *r =
1195 container_of(resource, struct iso_resource, resource);
1197 spin_lock_irq(&client->lock);
1198 r->todo = ISO_RES_DEALLOC;
1199 schedule_iso_resource(r, 0);
1200 spin_unlock_irq(&client->lock);
1203 static int init_iso_resource(struct client *client,
1204 struct fw_cdev_allocate_iso_resource *request, int todo)
1206 struct iso_resource_event *e1, *e2;
1207 struct iso_resource *r;
1208 int ret;
1210 if ((request->channels == 0 && request->bandwidth == 0) ||
1211 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1212 request->bandwidth < 0)
1213 return -EINVAL;
1215 r = kmalloc(sizeof(*r), GFP_KERNEL);
1216 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1217 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1218 if (r == NULL || e1 == NULL || e2 == NULL) {
1219 ret = -ENOMEM;
1220 goto fail;
1223 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1224 r->client = client;
1225 r->todo = todo;
1226 r->generation = -1;
1227 r->channels = request->channels;
1228 r->bandwidth = request->bandwidth;
1229 r->e_alloc = e1;
1230 r->e_dealloc = e2;
1232 e1->iso_resource.closure = request->closure;
1233 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1234 e2->iso_resource.closure = request->closure;
1235 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
1237 if (todo == ISO_RES_ALLOC) {
1238 r->resource.release = release_iso_resource;
1239 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
1240 if (ret < 0)
1241 goto fail;
1242 } else {
1243 r->resource.release = NULL;
1244 r->resource.handle = -1;
1245 schedule_iso_resource(r, 0);
1247 request->handle = r->resource.handle;
1249 return 0;
1250 fail:
1251 kfree(r);
1252 kfree(e1);
1253 kfree(e2);
1255 return ret;
1258 static int ioctl_allocate_iso_resource(struct client *client,
1259 union ioctl_arg *arg)
1261 return init_iso_resource(client,
1262 &arg->allocate_iso_resource, ISO_RES_ALLOC);
1265 static int ioctl_deallocate_iso_resource(struct client *client,
1266 union ioctl_arg *arg)
1268 return release_client_resource(client,
1269 arg->deallocate.handle, release_iso_resource, NULL);
1272 static int ioctl_allocate_iso_resource_once(struct client *client,
1273 union ioctl_arg *arg)
1275 return init_iso_resource(client,
1276 &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
1279 static int ioctl_deallocate_iso_resource_once(struct client *client,
1280 union ioctl_arg *arg)
1282 return init_iso_resource(client,
1283 &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
1287 * Returns a speed code: Maximum speed to or from this device,
1288 * limited by the device's link speed, the local node's link speed,
1289 * and all PHY port speeds between the two links.
1291 static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
1293 return client->device->max_speed;
1296 static int ioctl_send_broadcast_request(struct client *client,
1297 union ioctl_arg *arg)
1299 struct fw_cdev_send_request *a = &arg->send_request;
1301 switch (a->tcode) {
1302 case TCODE_WRITE_QUADLET_REQUEST:
1303 case TCODE_WRITE_BLOCK_REQUEST:
1304 break;
1305 default:
1306 return -EINVAL;
1309 /* Security policy: Only allow accesses to Units Space. */
1310 if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
1311 return -EACCES;
1313 return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
1316 static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
1318 struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
1319 struct fw_cdev_send_request request;
1320 int dest;
1322 if (a->speed > client->device->card->link_speed ||
1323 a->length > 1024 << a->speed)
1324 return -EIO;
1326 if (a->tag > 3 || a->channel > 63 || a->sy > 15)
1327 return -EINVAL;
1329 dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
1330 request.tcode = TCODE_STREAM_DATA;
1331 request.length = a->length;
1332 request.closure = a->closure;
1333 request.data = a->data;
1334 request.generation = a->generation;
1336 return init_request(client, &request, dest, a->speed);
1339 static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
1340 ioctl_get_info,
1341 ioctl_send_request,
1342 ioctl_allocate,
1343 ioctl_deallocate,
1344 ioctl_send_response,
1345 ioctl_initiate_bus_reset,
1346 ioctl_add_descriptor,
1347 ioctl_remove_descriptor,
1348 ioctl_create_iso_context,
1349 ioctl_queue_iso,
1350 ioctl_start_iso,
1351 ioctl_stop_iso,
1352 ioctl_get_cycle_timer,
1353 ioctl_allocate_iso_resource,
1354 ioctl_deallocate_iso_resource,
1355 ioctl_allocate_iso_resource_once,
1356 ioctl_deallocate_iso_resource_once,
1357 ioctl_get_speed,
1358 ioctl_send_broadcast_request,
1359 ioctl_send_stream_packet,
1360 ioctl_get_cycle_timer2,
1363 static int dispatch_ioctl(struct client *client,
1364 unsigned int cmd, void __user *arg)
1366 union ioctl_arg buffer;
1367 int ret;
1369 if (fw_device_is_shutdown(client->device))
1370 return -ENODEV;
1372 if (_IOC_TYPE(cmd) != '#' ||
1373 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
1374 _IOC_SIZE(cmd) > sizeof(buffer))
1375 return -EINVAL;
1377 if (_IOC_DIR(cmd) == _IOC_READ)
1378 memset(&buffer, 0, _IOC_SIZE(cmd));
1380 if (_IOC_DIR(cmd) & _IOC_WRITE)
1381 if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
1382 return -EFAULT;
1384 ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
1385 if (ret < 0)
1386 return ret;
1388 if (_IOC_DIR(cmd) & _IOC_READ)
1389 if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
1390 return -EFAULT;
1392 return ret;
1395 static long fw_device_op_ioctl(struct file *file,
1396 unsigned int cmd, unsigned long arg)
1398 return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
1401 #ifdef CONFIG_COMPAT
1402 static long fw_device_op_compat_ioctl(struct file *file,
1403 unsigned int cmd, unsigned long arg)
1405 return dispatch_ioctl(file->private_data, cmd, compat_ptr(arg));
1407 #endif
1409 static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1411 struct client *client = file->private_data;
1412 enum dma_data_direction direction;
1413 unsigned long size;
1414 int page_count, ret;
1416 if (fw_device_is_shutdown(client->device))
1417 return -ENODEV;
1419 /* FIXME: We could support multiple buffers, but we don't. */
1420 if (client->buffer.pages != NULL)
1421 return -EBUSY;
1423 if (!(vma->vm_flags & VM_SHARED))
1424 return -EINVAL;
1426 if (vma->vm_start & ~PAGE_MASK)
1427 return -EINVAL;
1429 client->vm_start = vma->vm_start;
1430 size = vma->vm_end - vma->vm_start;
1431 page_count = size >> PAGE_SHIFT;
1432 if (size & ~PAGE_MASK)
1433 return -EINVAL;
1435 if (vma->vm_flags & VM_WRITE)
1436 direction = DMA_TO_DEVICE;
1437 else
1438 direction = DMA_FROM_DEVICE;
1440 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1441 page_count, direction);
1442 if (ret < 0)
1443 return ret;
1445 ret = fw_iso_buffer_map(&client->buffer, vma);
1446 if (ret < 0)
1447 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1449 return ret;
1452 static int shutdown_resource(int id, void *p, void *data)
1454 struct client_resource *resource = p;
1455 struct client *client = data;
1457 resource->release(client, resource);
1458 client_put(client);
1460 return 0;
1463 static int fw_device_op_release(struct inode *inode, struct file *file)
1465 struct client *client = file->private_data;
1466 struct event *event, *next_event;
1468 mutex_lock(&client->device->client_list_mutex);
1469 list_del(&client->link);
1470 mutex_unlock(&client->device->client_list_mutex);
1472 if (client->iso_context)
1473 fw_iso_context_destroy(client->iso_context);
1475 if (client->buffer.pages)
1476 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1478 /* Freeze client->resource_idr and client->event_list */
1479 spin_lock_irq(&client->lock);
1480 client->in_shutdown = true;
1481 spin_unlock_irq(&client->lock);
1483 idr_for_each(&client->resource_idr, shutdown_resource, client);
1484 idr_remove_all(&client->resource_idr);
1485 idr_destroy(&client->resource_idr);
1487 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1488 kfree(event);
1490 client_put(client);
1492 return 0;
1495 static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1497 struct client *client = file->private_data;
1498 unsigned int mask = 0;
1500 poll_wait(file, &client->wait, pt);
1502 if (fw_device_is_shutdown(client->device))
1503 mask |= POLLHUP | POLLERR;
1504 if (!list_empty(&client->event_list))
1505 mask |= POLLIN | POLLRDNORM;
1507 return mask;
1510 const struct file_operations fw_device_ops = {
1511 .owner = THIS_MODULE,
1512 .llseek = no_llseek,
1513 .open = fw_device_op_open,
1514 .read = fw_device_op_read,
1515 .unlocked_ioctl = fw_device_op_ioctl,
1516 .mmap = fw_device_op_mmap,
1517 .release = fw_device_op_release,
1518 .poll = fw_device_op_poll,
1519 #ifdef CONFIG_COMPAT
1520 .compat_ioctl = fw_device_op_compat_ioctl,
1521 #endif