1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Char device for device raw access
5 * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
9 #include <linux/compat.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/firewire.h>
16 #include <linux/firewire-cdev.h>
17 #include <linux/idr.h>
18 #include <linux/irqflags.h>
19 #include <linux/jiffies.h>
20 #include <linux/kernel.h>
21 #include <linux/kref.h>
23 #include <linux/module.h>
24 #include <linux/mutex.h>
25 #include <linux/poll.h>
26 #include <linux/sched.h> /* required for linux/wait.h */
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/string.h>
30 #include <linux/time.h>
31 #include <linux/uaccess.h>
32 #include <linux/vmalloc.h>
33 #include <linux/wait.h>
34 #include <linux/workqueue.h>
38 #include <trace/events/firewire.h>
41 * ABI version history is documented in linux/firewire-cdev.h.
43 #define FW_CDEV_KERNEL_VERSION 5
44 #define FW_CDEV_VERSION_EVENT_REQUEST2 4
45 #define FW_CDEV_VERSION_ALLOCATE_REGION_END 4
46 #define FW_CDEV_VERSION_AUTO_FLUSH_ISO_OVERFLOW 5
47 #define FW_CDEV_VERSION_EVENT_ASYNC_TSTAMP 6
51 struct fw_device *device;
55 struct idr resource_idr;
56 struct list_head event_list;
57 wait_queue_head_t wait;
58 wait_queue_head_t tx_flush_wait;
59 u64 bus_reset_closure;
61 struct fw_iso_context *iso_context;
63 struct fw_iso_buffer buffer;
64 unsigned long vm_start;
65 bool buffer_is_mapped;
67 struct list_head phy_receiver_link;
68 u64 phy_receiver_closure;
70 struct list_head link;
74 static inline void client_get(struct client *client)
76 kref_get(&client->kref);
79 static void client_release(struct kref *kref)
81 struct client *client = container_of(kref, struct client, kref);
83 fw_device_put(client->device);
87 static void client_put(struct client *client)
89 kref_put(&client->kref, client_release);
92 struct client_resource;
93 typedef void (*client_resource_release_fn_t)(struct client *,
94 struct client_resource *);
95 struct client_resource {
96 client_resource_release_fn_t release;
100 struct address_handler_resource {
101 struct client_resource resource;
102 struct fw_address_handler handler;
104 struct client *client;
107 struct outbound_transaction_resource {
108 struct client_resource resource;
109 struct fw_transaction transaction;
112 struct inbound_transaction_resource {
113 struct client_resource resource;
114 struct fw_card *card;
115 struct fw_request *request;
121 struct descriptor_resource {
122 struct client_resource resource;
123 struct fw_descriptor descriptor;
127 struct iso_resource {
128 struct client_resource resource;
129 struct client *client;
130 /* Schedule work and access todo only with client->lock held. */
131 struct delayed_work work;
132 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
133 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
137 struct iso_resource_event *e_alloc, *e_dealloc;
140 static void release_iso_resource(struct client *, struct client_resource *);
142 static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
144 client_get(r->client);
145 if (!queue_delayed_work(fw_workqueue, &r->work, delay))
146 client_put(r->client);
149 static void schedule_if_iso_resource(struct client_resource *resource)
151 if (resource->release == release_iso_resource)
152 schedule_iso_resource(container_of(resource,
153 struct iso_resource, resource), 0);
157 * dequeue_event() just kfree()'s the event, so the event has to be
158 * the first field in a struct XYZ_event.
161 struct { void *data; size_t size; } v[2];
162 struct list_head link;
165 struct bus_reset_event {
167 struct fw_cdev_event_bus_reset reset;
170 struct outbound_transaction_event {
172 struct client *client;
173 struct outbound_transaction_resource r;
175 struct fw_cdev_event_response without_tstamp;
176 struct fw_cdev_event_response2 with_tstamp;
180 struct inbound_transaction_event {
183 struct fw_cdev_event_request request;
184 struct fw_cdev_event_request2 request2;
185 struct fw_cdev_event_request3 with_tstamp;
189 struct iso_interrupt_event {
191 struct fw_cdev_event_iso_interrupt interrupt;
194 struct iso_interrupt_mc_event {
196 struct fw_cdev_event_iso_interrupt_mc interrupt;
199 struct iso_resource_event {
201 struct fw_cdev_event_iso_resource iso_resource;
204 struct outbound_phy_packet_event {
206 struct client *client;
209 struct fw_cdev_event_phy_packet without_tstamp;
210 struct fw_cdev_event_phy_packet2 with_tstamp;
214 struct inbound_phy_packet_event {
217 struct fw_cdev_event_phy_packet without_tstamp;
218 struct fw_cdev_event_phy_packet2 with_tstamp;
223 static void __user *u64_to_uptr(u64 value)
225 if (in_compat_syscall())
226 return compat_ptr(value);
228 return (void __user *)(unsigned long)value;
231 static u64 uptr_to_u64(void __user *ptr)
233 if (in_compat_syscall())
234 return ptr_to_compat(ptr);
236 return (u64)(unsigned long)ptr;
239 static inline void __user *u64_to_uptr(u64 value)
241 return (void __user *)(unsigned long)value;
244 static inline u64 uptr_to_u64(void __user *ptr)
246 return (u64)(unsigned long)ptr;
248 #endif /* CONFIG_COMPAT */
250 static int fw_device_op_open(struct inode *inode, struct file *file)
252 struct fw_device *device;
253 struct client *client;
255 device = fw_device_get_by_devt(inode->i_rdev);
259 if (fw_device_is_shutdown(device)) {
260 fw_device_put(device);
264 client = kzalloc(sizeof(*client), GFP_KERNEL);
265 if (client == NULL) {
266 fw_device_put(device);
270 client->device = device;
271 spin_lock_init(&client->lock);
272 idr_init(&client->resource_idr);
273 INIT_LIST_HEAD(&client->event_list);
274 init_waitqueue_head(&client->wait);
275 init_waitqueue_head(&client->tx_flush_wait);
276 INIT_LIST_HEAD(&client->phy_receiver_link);
277 INIT_LIST_HEAD(&client->link);
278 kref_init(&client->kref);
280 file->private_data = client;
282 return nonseekable_open(inode, file);
285 static void queue_event(struct client *client, struct event *event,
286 void *data0, size_t size0, void *data1, size_t size1)
290 event->v[0].data = data0;
291 event->v[0].size = size0;
292 event->v[1].data = data1;
293 event->v[1].size = size1;
295 spin_lock_irqsave(&client->lock, flags);
296 if (client->in_shutdown)
299 list_add_tail(&event->link, &client->event_list);
300 spin_unlock_irqrestore(&client->lock, flags);
302 wake_up_interruptible(&client->wait);
305 static int dequeue_event(struct client *client,
306 char __user *buffer, size_t count)
312 ret = wait_event_interruptible(client->wait,
313 !list_empty(&client->event_list) ||
314 fw_device_is_shutdown(client->device));
318 if (list_empty(&client->event_list) &&
319 fw_device_is_shutdown(client->device))
322 spin_lock_irq(&client->lock);
323 event = list_first_entry(&client->event_list, struct event, link);
324 list_del(&event->link);
325 spin_unlock_irq(&client->lock);
328 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
329 size = min(event->v[i].size, count - total);
330 if (copy_to_user(buffer + total, event->v[i].data, size)) {
344 static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
345 size_t count, loff_t *offset)
347 struct client *client = file->private_data;
349 return dequeue_event(client, buffer, count);
352 static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
353 struct client *client)
355 struct fw_card *card = client->device->card;
357 spin_lock_irq(&card->lock);
359 event->closure = client->bus_reset_closure;
360 event->type = FW_CDEV_EVENT_BUS_RESET;
361 event->generation = client->device->generation;
362 event->node_id = client->device->node_id;
363 event->local_node_id = card->local_node->node_id;
364 event->bm_node_id = card->bm_node_id;
365 event->irm_node_id = card->irm_node->node_id;
366 event->root_node_id = card->root_node->node_id;
368 spin_unlock_irq(&card->lock);
371 static void for_each_client(struct fw_device *device,
372 void (*callback)(struct client *client))
376 mutex_lock(&device->client_list_mutex);
377 list_for_each_entry(c, &device->client_list, link)
379 mutex_unlock(&device->client_list_mutex);
382 static int schedule_reallocations(int id, void *p, void *data)
384 schedule_if_iso_resource(p);
389 static void queue_bus_reset_event(struct client *client)
391 struct bus_reset_event *e;
393 e = kzalloc(sizeof(*e), GFP_KERNEL);
397 fill_bus_reset_event(&e->reset, client);
399 queue_event(client, &e->event,
400 &e->reset, sizeof(e->reset), NULL, 0);
402 spin_lock_irq(&client->lock);
403 idr_for_each(&client->resource_idr, schedule_reallocations, client);
404 spin_unlock_irq(&client->lock);
407 void fw_device_cdev_update(struct fw_device *device)
409 for_each_client(device, queue_bus_reset_event);
412 static void wake_up_client(struct client *client)
414 wake_up_interruptible(&client->wait);
417 void fw_device_cdev_remove(struct fw_device *device)
419 for_each_client(device, wake_up_client);
423 struct fw_cdev_get_info get_info;
424 struct fw_cdev_send_request send_request;
425 struct fw_cdev_allocate allocate;
426 struct fw_cdev_deallocate deallocate;
427 struct fw_cdev_send_response send_response;
428 struct fw_cdev_initiate_bus_reset initiate_bus_reset;
429 struct fw_cdev_add_descriptor add_descriptor;
430 struct fw_cdev_remove_descriptor remove_descriptor;
431 struct fw_cdev_create_iso_context create_iso_context;
432 struct fw_cdev_queue_iso queue_iso;
433 struct fw_cdev_start_iso start_iso;
434 struct fw_cdev_stop_iso stop_iso;
435 struct fw_cdev_get_cycle_timer get_cycle_timer;
436 struct fw_cdev_allocate_iso_resource allocate_iso_resource;
437 struct fw_cdev_send_stream_packet send_stream_packet;
438 struct fw_cdev_get_cycle_timer2 get_cycle_timer2;
439 struct fw_cdev_send_phy_packet send_phy_packet;
440 struct fw_cdev_receive_phy_packets receive_phy_packets;
441 struct fw_cdev_set_iso_channels set_iso_channels;
442 struct fw_cdev_flush_iso flush_iso;
445 static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
447 struct fw_cdev_get_info *a = &arg->get_info;
448 struct fw_cdev_event_bus_reset bus_reset;
449 unsigned long ret = 0;
451 client->version = a->version;
452 a->version = FW_CDEV_KERNEL_VERSION;
453 a->card = client->device->card->index;
455 down_read(&fw_device_rwsem);
458 size_t want = a->rom_length;
459 size_t have = client->device->config_rom_length * 4;
461 ret = copy_to_user(u64_to_uptr(a->rom),
462 client->device->config_rom, min(want, have));
464 a->rom_length = client->device->config_rom_length * 4;
466 up_read(&fw_device_rwsem);
471 mutex_lock(&client->device->client_list_mutex);
473 client->bus_reset_closure = a->bus_reset_closure;
474 if (a->bus_reset != 0) {
475 fill_bus_reset_event(&bus_reset, client);
476 /* unaligned size of bus_reset is 36 bytes */
477 ret = copy_to_user(u64_to_uptr(a->bus_reset), &bus_reset, 36);
479 if (ret == 0 && list_empty(&client->link))
480 list_add_tail(&client->link, &client->device->client_list);
482 mutex_unlock(&client->device->client_list_mutex);
484 return ret ? -EFAULT : 0;
487 static int add_client_resource(struct client *client,
488 struct client_resource *resource, gfp_t gfp_mask)
490 bool preload = gfpflags_allow_blocking(gfp_mask);
495 idr_preload(gfp_mask);
496 spin_lock_irqsave(&client->lock, flags);
498 if (client->in_shutdown)
501 ret = idr_alloc(&client->resource_idr, resource, 0, 0,
504 resource->handle = ret;
506 schedule_if_iso_resource(resource);
509 spin_unlock_irqrestore(&client->lock, flags);
513 return ret < 0 ? ret : 0;
516 static int release_client_resource(struct client *client, u32 handle,
517 client_resource_release_fn_t release,
518 struct client_resource **return_resource)
520 struct client_resource *resource;
522 spin_lock_irq(&client->lock);
523 if (client->in_shutdown)
526 resource = idr_find(&client->resource_idr, handle);
527 if (resource && resource->release == release)
528 idr_remove(&client->resource_idr, handle);
529 spin_unlock_irq(&client->lock);
531 if (!(resource && resource->release == release))
535 *return_resource = resource;
537 resource->release(client, resource);
544 static void release_transaction(struct client *client,
545 struct client_resource *resource)
549 static void complete_transaction(struct fw_card *card, int rcode, u32 request_tstamp,
550 u32 response_tstamp, void *payload, size_t length, void *data)
552 struct outbound_transaction_event *e = data;
553 struct client *client = e->client;
556 spin_lock_irqsave(&client->lock, flags);
557 idr_remove(&client->resource_idr, e->r.resource.handle);
558 if (client->in_shutdown)
559 wake_up(&client->tx_flush_wait);
560 spin_unlock_irqrestore(&client->lock, flags);
562 switch (e->rsp.without_tstamp.type) {
563 case FW_CDEV_EVENT_RESPONSE:
565 struct fw_cdev_event_response *rsp = &e->rsp.without_tstamp;
567 if (length < rsp->length)
568 rsp->length = length;
569 if (rcode == RCODE_COMPLETE)
570 memcpy(rsp->data, payload, rsp->length);
574 // In the case that sizeof(*rsp) doesn't align with the position of the
575 // data, and the read is short, preserve an extra copy of the data
576 // to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
577 // for short reads and some apps depended on it, this is both safe
578 // and prudent for compatibility.
579 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
580 queue_event(client, &e->event, rsp, sizeof(*rsp), rsp->data, rsp->length);
582 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length, NULL, 0);
586 case FW_CDEV_EVENT_RESPONSE2:
588 struct fw_cdev_event_response2 *rsp = &e->rsp.with_tstamp;
590 if (length < rsp->length)
591 rsp->length = length;
592 if (rcode == RCODE_COMPLETE)
593 memcpy(rsp->data, payload, rsp->length);
596 rsp->request_tstamp = request_tstamp;
597 rsp->response_tstamp = response_tstamp;
599 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length, NULL, 0);
608 /* Drop the idr's reference */
612 static int init_request(struct client *client,
613 struct fw_cdev_send_request *request,
614 int destination_id, int speed)
616 struct outbound_transaction_event *e;
620 if (request->tcode != TCODE_STREAM_DATA &&
621 (request->length > 4096 || request->length > 512 << speed))
624 if (request->tcode == TCODE_WRITE_QUADLET_REQUEST &&
628 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
633 if (client->version < FW_CDEV_VERSION_EVENT_ASYNC_TSTAMP) {
634 struct fw_cdev_event_response *rsp = &e->rsp.without_tstamp;
636 rsp->type = FW_CDEV_EVENT_RESPONSE;
637 rsp->length = request->length;
638 rsp->closure = request->closure;
641 struct fw_cdev_event_response2 *rsp = &e->rsp.with_tstamp;
643 rsp->type = FW_CDEV_EVENT_RESPONSE2;
644 rsp->length = request->length;
645 rsp->closure = request->closure;
649 if (request->data && copy_from_user(payload, u64_to_uptr(request->data), request->length)) {
654 e->r.resource.release = release_transaction;
655 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
659 fw_send_request_with_tstamp(client->device->card, &e->r.transaction, request->tcode,
660 destination_id, request->generation, speed, request->offset,
661 payload, request->length, complete_transaction, e);
670 static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
672 switch (arg->send_request.tcode) {
673 case TCODE_WRITE_QUADLET_REQUEST:
674 case TCODE_WRITE_BLOCK_REQUEST:
675 case TCODE_READ_QUADLET_REQUEST:
676 case TCODE_READ_BLOCK_REQUEST:
677 case TCODE_LOCK_MASK_SWAP:
678 case TCODE_LOCK_COMPARE_SWAP:
679 case TCODE_LOCK_FETCH_ADD:
680 case TCODE_LOCK_LITTLE_ADD:
681 case TCODE_LOCK_BOUNDED_ADD:
682 case TCODE_LOCK_WRAP_ADD:
683 case TCODE_LOCK_VENDOR_DEPENDENT:
689 return init_request(client, &arg->send_request, client->device->node_id,
690 client->device->max_speed);
693 static void release_request(struct client *client,
694 struct client_resource *resource)
696 struct inbound_transaction_resource *r = container_of(resource,
697 struct inbound_transaction_resource, resource);
700 fw_request_put(r->request);
702 fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
704 fw_card_put(r->card);
708 static void handle_request(struct fw_card *card, struct fw_request *request,
709 int tcode, int destination, int source,
710 int generation, unsigned long long offset,
711 void *payload, size_t length, void *callback_data)
713 struct address_handler_resource *handler = callback_data;
714 bool is_fcp = is_in_fcp_region(offset, length);
715 struct inbound_transaction_resource *r;
716 struct inbound_transaction_event *e;
720 /* card may be different from handler->client->device->card */
723 // Extend the lifetime of data for request so that its payload is safely accessible in
724 // the process context for the client.
726 fw_request_get(request);
728 r = kmalloc(sizeof(*r), GFP_ATOMIC);
729 e = kmalloc(sizeof(*e), GFP_ATOMIC);
730 if (r == NULL || e == NULL)
734 r->request = request;
739 r->resource.release = release_request;
740 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
744 if (handler->client->version < FW_CDEV_VERSION_EVENT_REQUEST2) {
745 struct fw_cdev_event_request *req = &e->req.request;
748 tcode = TCODE_LOCK_REQUEST;
750 req->type = FW_CDEV_EVENT_REQUEST;
752 req->offset = offset;
753 req->length = length;
754 req->handle = r->resource.handle;
755 req->closure = handler->closure;
756 event_size0 = sizeof(*req);
757 } else if (handler->client->version < FW_CDEV_VERSION_EVENT_ASYNC_TSTAMP) {
758 struct fw_cdev_event_request2 *req = &e->req.request2;
760 req->type = FW_CDEV_EVENT_REQUEST2;
762 req->offset = offset;
763 req->source_node_id = source;
764 req->destination_node_id = destination;
765 req->card = card->index;
766 req->generation = generation;
767 req->length = length;
768 req->handle = r->resource.handle;
769 req->closure = handler->closure;
770 event_size0 = sizeof(*req);
772 struct fw_cdev_event_request3 *req = &e->req.with_tstamp;
774 req->type = FW_CDEV_EVENT_REQUEST3;
776 req->offset = offset;
777 req->source_node_id = source;
778 req->destination_node_id = destination;
779 req->card = card->index;
780 req->generation = generation;
781 req->length = length;
782 req->handle = r->resource.handle;
783 req->closure = handler->closure;
784 req->tstamp = fw_request_get_timestamp(request);
785 event_size0 = sizeof(*req);
788 queue_event(handler->client, &e->event,
789 &e->req, event_size0, r->data, length);
797 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
799 fw_request_put(request);
804 static void release_address_handler(struct client *client,
805 struct client_resource *resource)
807 struct address_handler_resource *r =
808 container_of(resource, struct address_handler_resource, resource);
810 fw_core_remove_address_handler(&r->handler);
814 static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
816 struct fw_cdev_allocate *a = &arg->allocate;
817 struct address_handler_resource *r;
818 struct fw_address_region region;
821 r = kmalloc(sizeof(*r), GFP_KERNEL);
825 region.start = a->offset;
826 if (client->version < FW_CDEV_VERSION_ALLOCATE_REGION_END)
827 region.end = a->offset + a->length;
829 region.end = a->region_end;
831 r->handler.length = a->length;
832 r->handler.address_callback = handle_request;
833 r->handler.callback_data = r;
834 r->closure = a->closure;
837 ret = fw_core_add_address_handler(&r->handler, ®ion);
842 a->offset = r->handler.offset;
844 r->resource.release = release_address_handler;
845 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
847 release_address_handler(client, &r->resource);
850 a->handle = r->resource.handle;
855 static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
857 return release_client_resource(client, arg->deallocate.handle,
858 release_address_handler, NULL);
861 static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
863 struct fw_cdev_send_response *a = &arg->send_response;
864 struct client_resource *resource;
865 struct inbound_transaction_resource *r;
868 if (release_client_resource(client, a->handle,
869 release_request, &resource) < 0)
872 r = container_of(resource, struct inbound_transaction_resource,
875 fw_request_put(r->request);
879 if (a->length != fw_get_response_length(r->request)) {
881 fw_request_put(r->request);
884 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
886 fw_request_put(r->request);
889 fw_send_response(r->card, r->request, a->rcode);
891 fw_card_put(r->card);
897 static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
899 fw_schedule_bus_reset(client->device->card, true,
900 arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
904 static void release_descriptor(struct client *client,
905 struct client_resource *resource)
907 struct descriptor_resource *r =
908 container_of(resource, struct descriptor_resource, resource);
910 fw_core_remove_descriptor(&r->descriptor);
914 static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
916 struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
917 struct descriptor_resource *r;
920 /* Access policy: Allow this ioctl only on local nodes' device files. */
921 if (!client->device->is_local)
927 r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
931 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
936 r->descriptor.length = a->length;
937 r->descriptor.immediate = a->immediate;
938 r->descriptor.key = a->key;
939 r->descriptor.data = r->data;
941 ret = fw_core_add_descriptor(&r->descriptor);
945 r->resource.release = release_descriptor;
946 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
948 fw_core_remove_descriptor(&r->descriptor);
951 a->handle = r->resource.handle;
960 static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
962 return release_client_resource(client, arg->remove_descriptor.handle,
963 release_descriptor, NULL);
966 static void iso_callback(struct fw_iso_context *context, u32 cycle,
967 size_t header_length, void *header, void *data)
969 struct client *client = data;
970 struct iso_interrupt_event *e;
972 e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
976 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
977 e->interrupt.closure = client->iso_closure;
978 e->interrupt.cycle = cycle;
979 e->interrupt.header_length = header_length;
980 memcpy(e->interrupt.header, header, header_length);
981 queue_event(client, &e->event, &e->interrupt,
982 sizeof(e->interrupt) + header_length, NULL, 0);
985 static void iso_mc_callback(struct fw_iso_context *context,
986 dma_addr_t completed, void *data)
988 struct client *client = data;
989 struct iso_interrupt_mc_event *e;
991 e = kmalloc(sizeof(*e), GFP_ATOMIC);
995 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT_MULTICHANNEL;
996 e->interrupt.closure = client->iso_closure;
997 e->interrupt.completed = fw_iso_buffer_lookup(&client->buffer,
999 queue_event(client, &e->event, &e->interrupt,
1000 sizeof(e->interrupt), NULL, 0);
1003 static enum dma_data_direction iso_dma_direction(struct fw_iso_context *context)
1005 if (context->type == FW_ISO_CONTEXT_TRANSMIT)
1006 return DMA_TO_DEVICE;
1008 return DMA_FROM_DEVICE;
1011 static struct fw_iso_context *fw_iso_mc_context_create(struct fw_card *card,
1012 fw_iso_mc_callback_t callback,
1013 void *callback_data)
1015 struct fw_iso_context *ctx;
1017 ctx = fw_iso_context_create(card, FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL,
1018 0, 0, 0, NULL, callback_data);
1020 ctx->callback.mc = callback;
1025 static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
1027 struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
1028 struct fw_iso_context *context;
1029 union fw_iso_callback cb;
1032 BUILD_BUG_ON(FW_CDEV_ISO_CONTEXT_TRANSMIT != FW_ISO_CONTEXT_TRANSMIT ||
1033 FW_CDEV_ISO_CONTEXT_RECEIVE != FW_ISO_CONTEXT_RECEIVE ||
1034 FW_CDEV_ISO_CONTEXT_RECEIVE_MULTICHANNEL !=
1035 FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL);
1038 case FW_ISO_CONTEXT_TRANSMIT:
1039 if (a->speed > SCODE_3200 || a->channel > 63)
1042 cb.sc = iso_callback;
1045 case FW_ISO_CONTEXT_RECEIVE:
1046 if (a->header_size < 4 || (a->header_size & 3) ||
1050 cb.sc = iso_callback;
1053 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
1054 cb.mc = iso_mc_callback;
1061 if (a->type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL)
1062 context = fw_iso_mc_context_create(client->device->card, cb.mc,
1065 context = fw_iso_context_create(client->device->card, a->type,
1066 a->channel, a->speed,
1067 a->header_size, cb.sc, client);
1068 if (IS_ERR(context))
1069 return PTR_ERR(context);
1070 if (client->version < FW_CDEV_VERSION_AUTO_FLUSH_ISO_OVERFLOW)
1071 context->drop_overflow_headers = true;
1073 /* We only support one context at this time. */
1074 spin_lock_irq(&client->lock);
1075 if (client->iso_context != NULL) {
1076 spin_unlock_irq(&client->lock);
1077 fw_iso_context_destroy(context);
1081 if (!client->buffer_is_mapped) {
1082 ret = fw_iso_buffer_map_dma(&client->buffer,
1083 client->device->card,
1084 iso_dma_direction(context));
1086 spin_unlock_irq(&client->lock);
1087 fw_iso_context_destroy(context);
1091 client->buffer_is_mapped = true;
1093 client->iso_closure = a->closure;
1094 client->iso_context = context;
1095 spin_unlock_irq(&client->lock);
1102 static int ioctl_set_iso_channels(struct client *client, union ioctl_arg *arg)
1104 struct fw_cdev_set_iso_channels *a = &arg->set_iso_channels;
1105 struct fw_iso_context *ctx = client->iso_context;
1107 if (ctx == NULL || a->handle != 0)
1110 return fw_iso_context_set_channels(ctx, &a->channels);
1113 /* Macros for decoding the iso packet control header. */
1114 #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
1115 #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
1116 #define GET_SKIP(v) (((v) >> 17) & 0x01)
1117 #define GET_TAG(v) (((v) >> 18) & 0x03)
1118 #define GET_SY(v) (((v) >> 20) & 0x0f)
1119 #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
1121 static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
1123 struct fw_cdev_queue_iso *a = &arg->queue_iso;
1124 struct fw_cdev_iso_packet __user *p, *end, *next;
1125 struct fw_iso_context *ctx = client->iso_context;
1126 unsigned long payload, buffer_end, transmit_header_bytes = 0;
1130 struct fw_iso_packet packet;
1134 if (ctx == NULL || a->handle != 0)
1138 * If the user passes a non-NULL data pointer, has mmap()'ed
1139 * the iso buffer, and the pointer points inside the buffer,
1140 * we setup the payload pointers accordingly. Otherwise we
1141 * set them both to 0, which will still let packets with
1142 * payload_length == 0 through. In other words, if no packets
1143 * use the indirect payload, the iso buffer need not be mapped
1144 * and the a->data pointer is ignored.
1146 payload = (unsigned long)a->data - client->vm_start;
1147 buffer_end = client->buffer.page_count << PAGE_SHIFT;
1148 if (a->data == 0 || client->buffer.pages == NULL ||
1149 payload >= buffer_end) {
1154 if (ctx->type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL && payload & 3)
1157 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
1159 end = (void __user *)p + a->size;
1162 if (get_user(control, &p->control))
1164 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
1165 u.packet.interrupt = GET_INTERRUPT(control);
1166 u.packet.skip = GET_SKIP(control);
1167 u.packet.tag = GET_TAG(control);
1168 u.packet.sy = GET_SY(control);
1169 u.packet.header_length = GET_HEADER_LENGTH(control);
1171 switch (ctx->type) {
1172 case FW_ISO_CONTEXT_TRANSMIT:
1173 if (u.packet.header_length & 3)
1175 transmit_header_bytes = u.packet.header_length;
1178 case FW_ISO_CONTEXT_RECEIVE:
1179 if (u.packet.header_length == 0 ||
1180 u.packet.header_length % ctx->header_size != 0)
1184 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
1185 if (u.packet.payload_length == 0 ||
1186 u.packet.payload_length & 3)
1191 next = (struct fw_cdev_iso_packet __user *)
1192 &p->header[transmit_header_bytes / 4];
1196 (u.packet.header, p->header, transmit_header_bytes))
1198 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
1199 u.packet.header_length + u.packet.payload_length > 0)
1201 if (payload + u.packet.payload_length > buffer_end)
1204 if (fw_iso_context_queue(ctx, &u.packet,
1205 &client->buffer, payload))
1209 payload += u.packet.payload_length;
1212 fw_iso_context_queue_flush(ctx);
1214 a->size -= uptr_to_u64(p) - a->packets;
1215 a->packets = uptr_to_u64(p);
1216 a->data = client->vm_start + payload;
1221 static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
1223 struct fw_cdev_start_iso *a = &arg->start_iso;
1226 FW_CDEV_ISO_CONTEXT_MATCH_TAG0 != FW_ISO_CONTEXT_MATCH_TAG0 ||
1227 FW_CDEV_ISO_CONTEXT_MATCH_TAG1 != FW_ISO_CONTEXT_MATCH_TAG1 ||
1228 FW_CDEV_ISO_CONTEXT_MATCH_TAG2 != FW_ISO_CONTEXT_MATCH_TAG2 ||
1229 FW_CDEV_ISO_CONTEXT_MATCH_TAG3 != FW_ISO_CONTEXT_MATCH_TAG3 ||
1230 FW_CDEV_ISO_CONTEXT_MATCH_ALL_TAGS != FW_ISO_CONTEXT_MATCH_ALL_TAGS);
1232 if (client->iso_context == NULL || a->handle != 0)
1235 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
1236 (a->tags == 0 || a->tags > 15 || a->sync > 15))
1239 return fw_iso_context_start(client->iso_context,
1240 a->cycle, a->sync, a->tags);
1243 static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
1245 struct fw_cdev_stop_iso *a = &arg->stop_iso;
1247 if (client->iso_context == NULL || a->handle != 0)
1250 return fw_iso_context_stop(client->iso_context);
1253 static int ioctl_flush_iso(struct client *client, union ioctl_arg *arg)
1255 struct fw_cdev_flush_iso *a = &arg->flush_iso;
1257 if (client->iso_context == NULL || a->handle != 0)
1260 return fw_iso_context_flush_completions(client->iso_context);
1263 static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
1265 struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
1266 struct fw_card *card = client->device->card;
1267 struct timespec64 ts = {0, 0};
1271 local_irq_disable();
1273 ret = fw_card_read_cycle_time(card, &cycle_time);
1277 switch (a->clk_id) {
1278 case CLOCK_REALTIME: ktime_get_real_ts64(&ts); break;
1279 case CLOCK_MONOTONIC: ktime_get_ts64(&ts); break;
1280 case CLOCK_MONOTONIC_RAW: ktime_get_raw_ts64(&ts); break;
1287 a->tv_sec = ts.tv_sec;
1288 a->tv_nsec = ts.tv_nsec;
1289 a->cycle_timer = cycle_time;
1294 static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
1296 struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
1297 struct fw_cdev_get_cycle_timer2 ct2;
1299 ct2.clk_id = CLOCK_REALTIME;
1300 ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
1302 a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
1303 a->cycle_timer = ct2.cycle_timer;
1308 static void iso_resource_work(struct work_struct *work)
1310 struct iso_resource_event *e;
1311 struct iso_resource *r =
1312 container_of(work, struct iso_resource, work.work);
1313 struct client *client = r->client;
1314 int generation, channel, bandwidth, todo;
1315 bool skip, free, success;
1317 spin_lock_irq(&client->lock);
1318 generation = client->device->generation;
1320 /* Allow 1000ms grace period for other reallocations. */
1321 if (todo == ISO_RES_ALLOC &&
1322 time_before64(get_jiffies_64(),
1323 client->device->card->reset_jiffies + HZ)) {
1324 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
1327 /* We could be called twice within the same generation. */
1328 skip = todo == ISO_RES_REALLOC &&
1329 r->generation == generation;
1331 free = todo == ISO_RES_DEALLOC ||
1332 todo == ISO_RES_ALLOC_ONCE ||
1333 todo == ISO_RES_DEALLOC_ONCE;
1334 r->generation = generation;
1335 spin_unlock_irq(&client->lock);
1340 bandwidth = r->bandwidth;
1342 fw_iso_resource_manage(client->device->card, generation,
1343 r->channels, &channel, &bandwidth,
1344 todo == ISO_RES_ALLOC ||
1345 todo == ISO_RES_REALLOC ||
1346 todo == ISO_RES_ALLOC_ONCE);
1348 * Is this generation outdated already? As long as this resource sticks
1349 * in the idr, it will be scheduled again for a newer generation or at
1352 if (channel == -EAGAIN &&
1353 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1356 success = channel >= 0 || bandwidth > 0;
1358 spin_lock_irq(&client->lock);
1360 * Transit from allocation to reallocation, except if the client
1361 * requested deallocation in the meantime.
1363 if (r->todo == ISO_RES_ALLOC)
1364 r->todo = ISO_RES_REALLOC;
1366 * Allocation or reallocation failure? Pull this resource out of the
1367 * idr and prepare for deletion, unless the client is shutting down.
1369 if (r->todo == ISO_RES_REALLOC && !success &&
1370 !client->in_shutdown &&
1371 idr_remove(&client->resource_idr, r->resource.handle)) {
1375 spin_unlock_irq(&client->lock);
1377 if (todo == ISO_RES_ALLOC && channel >= 0)
1378 r->channels = 1ULL << channel;
1380 if (todo == ISO_RES_REALLOC && success)
1383 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
1388 r->e_dealloc = NULL;
1390 e->iso_resource.handle = r->resource.handle;
1391 e->iso_resource.channel = channel;
1392 e->iso_resource.bandwidth = bandwidth;
1394 queue_event(client, &e->event,
1395 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
1398 cancel_delayed_work(&r->work);
1400 kfree(r->e_dealloc);
1407 static void release_iso_resource(struct client *client,
1408 struct client_resource *resource)
1410 struct iso_resource *r =
1411 container_of(resource, struct iso_resource, resource);
1413 spin_lock_irq(&client->lock);
1414 r->todo = ISO_RES_DEALLOC;
1415 schedule_iso_resource(r, 0);
1416 spin_unlock_irq(&client->lock);
1419 static int init_iso_resource(struct client *client,
1420 struct fw_cdev_allocate_iso_resource *request, int todo)
1422 struct iso_resource_event *e1, *e2;
1423 struct iso_resource *r;
1426 if ((request->channels == 0 && request->bandwidth == 0) ||
1427 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL)
1430 r = kmalloc(sizeof(*r), GFP_KERNEL);
1431 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1432 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1433 if (r == NULL || e1 == NULL || e2 == NULL) {
1438 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1442 r->channels = request->channels;
1443 r->bandwidth = request->bandwidth;
1447 e1->iso_resource.closure = request->closure;
1448 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1449 e2->iso_resource.closure = request->closure;
1450 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
1452 if (todo == ISO_RES_ALLOC) {
1453 r->resource.release = release_iso_resource;
1454 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
1458 r->resource.release = NULL;
1459 r->resource.handle = -1;
1460 schedule_iso_resource(r, 0);
1462 request->handle = r->resource.handle;
1473 static int ioctl_allocate_iso_resource(struct client *client,
1474 union ioctl_arg *arg)
1476 return init_iso_resource(client,
1477 &arg->allocate_iso_resource, ISO_RES_ALLOC);
1480 static int ioctl_deallocate_iso_resource(struct client *client,
1481 union ioctl_arg *arg)
1483 return release_client_resource(client,
1484 arg->deallocate.handle, release_iso_resource, NULL);
1487 static int ioctl_allocate_iso_resource_once(struct client *client,
1488 union ioctl_arg *arg)
1490 return init_iso_resource(client,
1491 &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
1494 static int ioctl_deallocate_iso_resource_once(struct client *client,
1495 union ioctl_arg *arg)
1497 return init_iso_resource(client,
1498 &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
1502 * Returns a speed code: Maximum speed to or from this device,
1503 * limited by the device's link speed, the local node's link speed,
1504 * and all PHY port speeds between the two links.
1506 static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
1508 return client->device->max_speed;
1511 static int ioctl_send_broadcast_request(struct client *client,
1512 union ioctl_arg *arg)
1514 struct fw_cdev_send_request *a = &arg->send_request;
1517 case TCODE_WRITE_QUADLET_REQUEST:
1518 case TCODE_WRITE_BLOCK_REQUEST:
1524 /* Security policy: Only allow accesses to Units Space. */
1525 if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
1528 return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
1531 static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
1533 struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
1534 struct fw_cdev_send_request request;
1537 if (a->speed > client->device->card->link_speed ||
1538 a->length > 1024 << a->speed)
1541 if (a->tag > 3 || a->channel > 63 || a->sy > 15)
1544 dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
1545 request.tcode = TCODE_STREAM_DATA;
1546 request.length = a->length;
1547 request.closure = a->closure;
1548 request.data = a->data;
1549 request.generation = a->generation;
1551 return init_request(client, &request, dest, a->speed);
1554 static void outbound_phy_packet_callback(struct fw_packet *packet,
1555 struct fw_card *card, int status)
1557 struct outbound_phy_packet_event *e =
1558 container_of(packet, struct outbound_phy_packet_event, p);
1559 struct client *e_client = e->client;
1562 trace_async_phy_outbound_complete((uintptr_t)packet, status, packet->generation,
1568 rcode = RCODE_COMPLETE;
1570 // should never happen with PHY packets:
1572 rcode = RCODE_COMPLETE;
1579 case ACK_DATA_ERROR:
1580 rcode = RCODE_DATA_ERROR;
1582 case ACK_TYPE_ERROR:
1583 rcode = RCODE_TYPE_ERROR;
1585 // stale generation; cancelled; on certain controllers: no ack
1591 switch (e->phy_packet.without_tstamp.type) {
1592 case FW_CDEV_EVENT_PHY_PACKET_SENT:
1594 struct fw_cdev_event_phy_packet *pp = &e->phy_packet.without_tstamp;
1597 pp->data[0] = packet->timestamp;
1598 queue_event(e->client, &e->event, &e->phy_packet, sizeof(*pp) + pp->length,
1602 case FW_CDEV_EVENT_PHY_PACKET_SENT2:
1604 struct fw_cdev_event_phy_packet2 *pp = &e->phy_packet.with_tstamp;
1607 pp->tstamp = packet->timestamp;
1608 queue_event(e->client, &e->event, &e->phy_packet, sizeof(*pp) + pp->length,
1617 client_put(e_client);
1620 static int ioctl_send_phy_packet(struct client *client, union ioctl_arg *arg)
1622 struct fw_cdev_send_phy_packet *a = &arg->send_phy_packet;
1623 struct fw_card *card = client->device->card;
1624 struct outbound_phy_packet_event *e;
1626 /* Access policy: Allow this ioctl only on local nodes' device files. */
1627 if (!client->device->is_local)
1630 e = kzalloc(sizeof(*e) + sizeof(a->data), GFP_KERNEL);
1636 e->p.speed = SCODE_100;
1637 e->p.generation = a->generation;
1638 e->p.header[0] = TCODE_LINK_INTERNAL << 4;
1639 e->p.header[1] = a->data[0];
1640 e->p.header[2] = a->data[1];
1641 e->p.header_length = 12;
1642 e->p.callback = outbound_phy_packet_callback;
1644 if (client->version < FW_CDEV_VERSION_EVENT_ASYNC_TSTAMP) {
1645 struct fw_cdev_event_phy_packet *pp = &e->phy_packet.without_tstamp;
1647 pp->closure = a->closure;
1648 pp->type = FW_CDEV_EVENT_PHY_PACKET_SENT;
1649 if (is_ping_packet(a->data))
1652 struct fw_cdev_event_phy_packet2 *pp = &e->phy_packet.with_tstamp;
1654 pp->closure = a->closure;
1655 pp->type = FW_CDEV_EVENT_PHY_PACKET_SENT2;
1656 // Keep the data field so that application can match the response event to the
1658 pp->length = sizeof(a->data);
1659 memcpy(pp->data, a->data, sizeof(a->data));
1662 trace_async_phy_outbound_initiate((uintptr_t)&e->p, e->p.generation, e->p.header[1],
1665 card->driver->send_request(card, &e->p);
1670 static int ioctl_receive_phy_packets(struct client *client, union ioctl_arg *arg)
1672 struct fw_cdev_receive_phy_packets *a = &arg->receive_phy_packets;
1673 struct fw_card *card = client->device->card;
1675 /* Access policy: Allow this ioctl only on local nodes' device files. */
1676 if (!client->device->is_local)
1679 spin_lock_irq(&card->lock);
1681 list_move_tail(&client->phy_receiver_link, &card->phy_receiver_list);
1682 client->phy_receiver_closure = a->closure;
1684 spin_unlock_irq(&card->lock);
1689 void fw_cdev_handle_phy_packet(struct fw_card *card, struct fw_packet *p)
1691 struct client *client;
1692 struct inbound_phy_packet_event *e;
1693 unsigned long flags;
1695 spin_lock_irqsave(&card->lock, flags);
1697 list_for_each_entry(client, &card->phy_receiver_list, phy_receiver_link) {
1698 e = kmalloc(sizeof(*e) + 8, GFP_ATOMIC);
1702 if (client->version < FW_CDEV_VERSION_EVENT_ASYNC_TSTAMP) {
1703 struct fw_cdev_event_phy_packet *pp = &e->phy_packet.without_tstamp;
1705 pp->closure = client->phy_receiver_closure;
1706 pp->type = FW_CDEV_EVENT_PHY_PACKET_RECEIVED;
1707 pp->rcode = RCODE_COMPLETE;
1709 pp->data[0] = p->header[1];
1710 pp->data[1] = p->header[2];
1711 queue_event(client, &e->event, &e->phy_packet, sizeof(*pp) + 8, NULL, 0);
1713 struct fw_cdev_event_phy_packet2 *pp = &e->phy_packet.with_tstamp;
1715 pp = &e->phy_packet.with_tstamp;
1716 pp->closure = client->phy_receiver_closure;
1717 pp->type = FW_CDEV_EVENT_PHY_PACKET_RECEIVED2;
1718 pp->rcode = RCODE_COMPLETE;
1720 pp->tstamp = p->timestamp;
1721 pp->data[0] = p->header[1];
1722 pp->data[1] = p->header[2];
1723 queue_event(client, &e->event, &e->phy_packet, sizeof(*pp) + 8, NULL, 0);
1727 spin_unlock_irqrestore(&card->lock, flags);
1730 static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
1731 [0x00] = ioctl_get_info,
1732 [0x01] = ioctl_send_request,
1733 [0x02] = ioctl_allocate,
1734 [0x03] = ioctl_deallocate,
1735 [0x04] = ioctl_send_response,
1736 [0x05] = ioctl_initiate_bus_reset,
1737 [0x06] = ioctl_add_descriptor,
1738 [0x07] = ioctl_remove_descriptor,
1739 [0x08] = ioctl_create_iso_context,
1740 [0x09] = ioctl_queue_iso,
1741 [0x0a] = ioctl_start_iso,
1742 [0x0b] = ioctl_stop_iso,
1743 [0x0c] = ioctl_get_cycle_timer,
1744 [0x0d] = ioctl_allocate_iso_resource,
1745 [0x0e] = ioctl_deallocate_iso_resource,
1746 [0x0f] = ioctl_allocate_iso_resource_once,
1747 [0x10] = ioctl_deallocate_iso_resource_once,
1748 [0x11] = ioctl_get_speed,
1749 [0x12] = ioctl_send_broadcast_request,
1750 [0x13] = ioctl_send_stream_packet,
1751 [0x14] = ioctl_get_cycle_timer2,
1752 [0x15] = ioctl_send_phy_packet,
1753 [0x16] = ioctl_receive_phy_packets,
1754 [0x17] = ioctl_set_iso_channels,
1755 [0x18] = ioctl_flush_iso,
1758 static int dispatch_ioctl(struct client *client,
1759 unsigned int cmd, void __user *arg)
1761 union ioctl_arg buffer;
1764 if (fw_device_is_shutdown(client->device))
1767 if (_IOC_TYPE(cmd) != '#' ||
1768 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
1769 _IOC_SIZE(cmd) > sizeof(buffer))
1772 memset(&buffer, 0, sizeof(buffer));
1774 if (_IOC_DIR(cmd) & _IOC_WRITE)
1775 if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
1778 ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
1782 if (_IOC_DIR(cmd) & _IOC_READ)
1783 if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
1789 static long fw_device_op_ioctl(struct file *file,
1790 unsigned int cmd, unsigned long arg)
1792 return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
1795 static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1797 struct client *client = file->private_data;
1799 int page_count, ret;
1801 if (fw_device_is_shutdown(client->device))
1804 /* FIXME: We could support multiple buffers, but we don't. */
1805 if (client->buffer.pages != NULL)
1808 if (!(vma->vm_flags & VM_SHARED))
1811 if (vma->vm_start & ~PAGE_MASK)
1814 client->vm_start = vma->vm_start;
1815 size = vma->vm_end - vma->vm_start;
1816 page_count = size >> PAGE_SHIFT;
1817 if (size & ~PAGE_MASK)
1820 ret = fw_iso_buffer_alloc(&client->buffer, page_count);
1824 spin_lock_irq(&client->lock);
1825 if (client->iso_context) {
1826 ret = fw_iso_buffer_map_dma(&client->buffer,
1827 client->device->card,
1828 iso_dma_direction(client->iso_context));
1829 client->buffer_is_mapped = (ret == 0);
1831 spin_unlock_irq(&client->lock);
1835 ret = vm_map_pages_zero(vma, client->buffer.pages,
1836 client->buffer.page_count);
1842 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1846 static int is_outbound_transaction_resource(int id, void *p, void *data)
1848 struct client_resource *resource = p;
1850 return resource->release == release_transaction;
1853 static int has_outbound_transactions(struct client *client)
1857 spin_lock_irq(&client->lock);
1858 ret = idr_for_each(&client->resource_idr,
1859 is_outbound_transaction_resource, NULL);
1860 spin_unlock_irq(&client->lock);
1865 static int shutdown_resource(int id, void *p, void *data)
1867 struct client_resource *resource = p;
1868 struct client *client = data;
1870 resource->release(client, resource);
1876 static int fw_device_op_release(struct inode *inode, struct file *file)
1878 struct client *client = file->private_data;
1879 struct event *event, *next_event;
1881 spin_lock_irq(&client->device->card->lock);
1882 list_del(&client->phy_receiver_link);
1883 spin_unlock_irq(&client->device->card->lock);
1885 mutex_lock(&client->device->client_list_mutex);
1886 list_del(&client->link);
1887 mutex_unlock(&client->device->client_list_mutex);
1889 if (client->iso_context)
1890 fw_iso_context_destroy(client->iso_context);
1892 if (client->buffer.pages)
1893 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1895 /* Freeze client->resource_idr and client->event_list */
1896 spin_lock_irq(&client->lock);
1897 client->in_shutdown = true;
1898 spin_unlock_irq(&client->lock);
1900 wait_event(client->tx_flush_wait, !has_outbound_transactions(client));
1902 idr_for_each(&client->resource_idr, shutdown_resource, client);
1903 idr_destroy(&client->resource_idr);
1905 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1913 static __poll_t fw_device_op_poll(struct file *file, poll_table * pt)
1915 struct client *client = file->private_data;
1918 poll_wait(file, &client->wait, pt);
1920 if (fw_device_is_shutdown(client->device))
1921 mask |= EPOLLHUP | EPOLLERR;
1922 if (!list_empty(&client->event_list))
1923 mask |= EPOLLIN | EPOLLRDNORM;
1928 const struct file_operations fw_device_ops = {
1929 .owner = THIS_MODULE,
1930 .llseek = no_llseek,
1931 .open = fw_device_op_open,
1932 .read = fw_device_op_read,
1933 .unlocked_ioctl = fw_device_op_ioctl,
1934 .mmap = fw_device_op_mmap,
1935 .release = fw_device_op_release,
1936 .poll = fw_device_op_poll,
1937 .compat_ioctl = compat_ptr_ioctl,