1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Char device for device raw access
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>
40 * ABI version history is documented in linux/firewire-cdev.h.
42 #define FW_CDEV_KERNEL_VERSION 5
43 #define FW_CDEV_VERSION_EVENT_REQUEST2 4
44 #define FW_CDEV_VERSION_ALLOCATE_REGION_END 4
45 #define FW_CDEV_VERSION_AUTO_FLUSH_ISO_OVERFLOW 5
49 struct fw_device *device;
53 struct idr resource_idr;
54 struct list_head event_list;
55 wait_queue_head_t wait;
56 wait_queue_head_t tx_flush_wait;
57 u64 bus_reset_closure;
59 struct fw_iso_context *iso_context;
61 struct fw_iso_buffer buffer;
62 unsigned long vm_start;
63 bool buffer_is_mapped;
65 struct list_head phy_receiver_link;
66 u64 phy_receiver_closure;
68 struct list_head link;
72 static inline void client_get(struct client *client)
74 kref_get(&client->kref);
77 static void client_release(struct kref *kref)
79 struct client *client = container_of(kref, struct client, kref);
81 fw_device_put(client->device);
85 static void client_put(struct client *client)
87 kref_put(&client->kref, client_release);
90 struct client_resource;
91 typedef void (*client_resource_release_fn_t)(struct client *,
92 struct client_resource *);
93 struct client_resource {
94 client_resource_release_fn_t release;
98 struct address_handler_resource {
99 struct client_resource resource;
100 struct fw_address_handler handler;
102 struct client *client;
105 struct outbound_transaction_resource {
106 struct client_resource resource;
107 struct fw_transaction transaction;
110 struct inbound_transaction_resource {
111 struct client_resource resource;
112 struct fw_card *card;
113 struct fw_request *request;
119 struct descriptor_resource {
120 struct client_resource resource;
121 struct fw_descriptor descriptor;
125 struct iso_resource {
126 struct client_resource resource;
127 struct client *client;
128 /* Schedule work and access todo only with client->lock held. */
129 struct delayed_work work;
130 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
131 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
135 struct iso_resource_event *e_alloc, *e_dealloc;
138 static void release_iso_resource(struct client *, struct client_resource *);
140 static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
142 client_get(r->client);
143 if (!queue_delayed_work(fw_workqueue, &r->work, delay))
144 client_put(r->client);
147 static void schedule_if_iso_resource(struct client_resource *resource)
149 if (resource->release == release_iso_resource)
150 schedule_iso_resource(container_of(resource,
151 struct iso_resource, resource), 0);
155 * dequeue_event() just kfree()'s the event, so the event has to be
156 * the first field in a struct XYZ_event.
159 struct { void *data; size_t size; } v[2];
160 struct list_head link;
163 struct bus_reset_event {
165 struct fw_cdev_event_bus_reset reset;
168 struct outbound_transaction_event {
170 struct client *client;
171 struct outbound_transaction_resource r;
172 struct fw_cdev_event_response response;
175 struct inbound_transaction_event {
178 struct fw_cdev_event_request request;
179 struct fw_cdev_event_request2 request2;
183 struct iso_interrupt_event {
185 struct fw_cdev_event_iso_interrupt interrupt;
188 struct iso_interrupt_mc_event {
190 struct fw_cdev_event_iso_interrupt_mc interrupt;
193 struct iso_resource_event {
195 struct fw_cdev_event_iso_resource iso_resource;
198 struct outbound_phy_packet_event {
200 struct client *client;
202 struct fw_cdev_event_phy_packet phy_packet;
205 struct inbound_phy_packet_event {
207 struct fw_cdev_event_phy_packet phy_packet;
211 static void __user *u64_to_uptr(u64 value)
213 if (in_compat_syscall())
214 return compat_ptr(value);
216 return (void __user *)(unsigned long)value;
219 static u64 uptr_to_u64(void __user *ptr)
221 if (in_compat_syscall())
222 return ptr_to_compat(ptr);
224 return (u64)(unsigned long)ptr;
227 static inline void __user *u64_to_uptr(u64 value)
229 return (void __user *)(unsigned long)value;
232 static inline u64 uptr_to_u64(void __user *ptr)
234 return (u64)(unsigned long)ptr;
236 #endif /* CONFIG_COMPAT */
238 static int fw_device_op_open(struct inode *inode, struct file *file)
240 struct fw_device *device;
241 struct client *client;
243 device = fw_device_get_by_devt(inode->i_rdev);
247 if (fw_device_is_shutdown(device)) {
248 fw_device_put(device);
252 client = kzalloc(sizeof(*client), GFP_KERNEL);
253 if (client == NULL) {
254 fw_device_put(device);
258 client->device = device;
259 spin_lock_init(&client->lock);
260 idr_init(&client->resource_idr);
261 INIT_LIST_HEAD(&client->event_list);
262 init_waitqueue_head(&client->wait);
263 init_waitqueue_head(&client->tx_flush_wait);
264 INIT_LIST_HEAD(&client->phy_receiver_link);
265 INIT_LIST_HEAD(&client->link);
266 kref_init(&client->kref);
268 file->private_data = client;
270 return nonseekable_open(inode, file);
273 static void queue_event(struct client *client, struct event *event,
274 void *data0, size_t size0, void *data1, size_t size1)
278 event->v[0].data = data0;
279 event->v[0].size = size0;
280 event->v[1].data = data1;
281 event->v[1].size = size1;
283 spin_lock_irqsave(&client->lock, flags);
284 if (client->in_shutdown)
287 list_add_tail(&event->link, &client->event_list);
288 spin_unlock_irqrestore(&client->lock, flags);
290 wake_up_interruptible(&client->wait);
293 static int dequeue_event(struct client *client,
294 char __user *buffer, size_t count)
300 ret = wait_event_interruptible(client->wait,
301 !list_empty(&client->event_list) ||
302 fw_device_is_shutdown(client->device));
306 if (list_empty(&client->event_list) &&
307 fw_device_is_shutdown(client->device))
310 spin_lock_irq(&client->lock);
311 event = list_first_entry(&client->event_list, struct event, link);
312 list_del(&event->link);
313 spin_unlock_irq(&client->lock);
316 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
317 size = min(event->v[i].size, count - total);
318 if (copy_to_user(buffer + total, event->v[i].data, size)) {
332 static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
333 size_t count, loff_t *offset)
335 struct client *client = file->private_data;
337 return dequeue_event(client, buffer, count);
340 static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
341 struct client *client)
343 struct fw_card *card = client->device->card;
345 spin_lock_irq(&card->lock);
347 event->closure = client->bus_reset_closure;
348 event->type = FW_CDEV_EVENT_BUS_RESET;
349 event->generation = client->device->generation;
350 event->node_id = client->device->node_id;
351 event->local_node_id = card->local_node->node_id;
352 event->bm_node_id = card->bm_node_id;
353 event->irm_node_id = card->irm_node->node_id;
354 event->root_node_id = card->root_node->node_id;
356 spin_unlock_irq(&card->lock);
359 static void for_each_client(struct fw_device *device,
360 void (*callback)(struct client *client))
364 mutex_lock(&device->client_list_mutex);
365 list_for_each_entry(c, &device->client_list, link)
367 mutex_unlock(&device->client_list_mutex);
370 static int schedule_reallocations(int id, void *p, void *data)
372 schedule_if_iso_resource(p);
377 static void queue_bus_reset_event(struct client *client)
379 struct bus_reset_event *e;
381 e = kzalloc(sizeof(*e), GFP_KERNEL);
385 fill_bus_reset_event(&e->reset, client);
387 queue_event(client, &e->event,
388 &e->reset, sizeof(e->reset), NULL, 0);
390 spin_lock_irq(&client->lock);
391 idr_for_each(&client->resource_idr, schedule_reallocations, client);
392 spin_unlock_irq(&client->lock);
395 void fw_device_cdev_update(struct fw_device *device)
397 for_each_client(device, queue_bus_reset_event);
400 static void wake_up_client(struct client *client)
402 wake_up_interruptible(&client->wait);
405 void fw_device_cdev_remove(struct fw_device *device)
407 for_each_client(device, wake_up_client);
411 struct fw_cdev_get_info get_info;
412 struct fw_cdev_send_request send_request;
413 struct fw_cdev_allocate allocate;
414 struct fw_cdev_deallocate deallocate;
415 struct fw_cdev_send_response send_response;
416 struct fw_cdev_initiate_bus_reset initiate_bus_reset;
417 struct fw_cdev_add_descriptor add_descriptor;
418 struct fw_cdev_remove_descriptor remove_descriptor;
419 struct fw_cdev_create_iso_context create_iso_context;
420 struct fw_cdev_queue_iso queue_iso;
421 struct fw_cdev_start_iso start_iso;
422 struct fw_cdev_stop_iso stop_iso;
423 struct fw_cdev_get_cycle_timer get_cycle_timer;
424 struct fw_cdev_allocate_iso_resource allocate_iso_resource;
425 struct fw_cdev_send_stream_packet send_stream_packet;
426 struct fw_cdev_get_cycle_timer2 get_cycle_timer2;
427 struct fw_cdev_send_phy_packet send_phy_packet;
428 struct fw_cdev_receive_phy_packets receive_phy_packets;
429 struct fw_cdev_set_iso_channels set_iso_channels;
430 struct fw_cdev_flush_iso flush_iso;
433 static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
435 struct fw_cdev_get_info *a = &arg->get_info;
436 struct fw_cdev_event_bus_reset bus_reset;
437 unsigned long ret = 0;
439 client->version = a->version;
440 a->version = FW_CDEV_KERNEL_VERSION;
441 a->card = client->device->card->index;
443 down_read(&fw_device_rwsem);
446 size_t want = a->rom_length;
447 size_t have = client->device->config_rom_length * 4;
449 ret = copy_to_user(u64_to_uptr(a->rom),
450 client->device->config_rom, min(want, have));
452 a->rom_length = client->device->config_rom_length * 4;
454 up_read(&fw_device_rwsem);
459 mutex_lock(&client->device->client_list_mutex);
461 client->bus_reset_closure = a->bus_reset_closure;
462 if (a->bus_reset != 0) {
463 fill_bus_reset_event(&bus_reset, client);
464 /* unaligned size of bus_reset is 36 bytes */
465 ret = copy_to_user(u64_to_uptr(a->bus_reset), &bus_reset, 36);
467 if (ret == 0 && list_empty(&client->link))
468 list_add_tail(&client->link, &client->device->client_list);
470 mutex_unlock(&client->device->client_list_mutex);
472 return ret ? -EFAULT : 0;
475 static int add_client_resource(struct client *client,
476 struct client_resource *resource, gfp_t gfp_mask)
478 bool preload = gfpflags_allow_blocking(gfp_mask);
483 idr_preload(gfp_mask);
484 spin_lock_irqsave(&client->lock, flags);
486 if (client->in_shutdown)
489 ret = idr_alloc(&client->resource_idr, resource, 0, 0,
492 resource->handle = ret;
494 schedule_if_iso_resource(resource);
497 spin_unlock_irqrestore(&client->lock, flags);
501 return ret < 0 ? ret : 0;
504 static int release_client_resource(struct client *client, u32 handle,
505 client_resource_release_fn_t release,
506 struct client_resource **return_resource)
508 struct client_resource *resource;
510 spin_lock_irq(&client->lock);
511 if (client->in_shutdown)
514 resource = idr_find(&client->resource_idr, handle);
515 if (resource && resource->release == release)
516 idr_remove(&client->resource_idr, handle);
517 spin_unlock_irq(&client->lock);
519 if (!(resource && resource->release == release))
523 *return_resource = resource;
525 resource->release(client, resource);
532 static void release_transaction(struct client *client,
533 struct client_resource *resource)
537 static void complete_transaction(struct fw_card *card, int rcode,
538 void *payload, size_t length, void *data)
540 struct outbound_transaction_event *e = data;
541 struct fw_cdev_event_response *rsp = &e->response;
542 struct client *client = e->client;
545 if (length < rsp->length)
546 rsp->length = length;
547 if (rcode == RCODE_COMPLETE)
548 memcpy(rsp->data, payload, rsp->length);
550 spin_lock_irqsave(&client->lock, flags);
551 idr_remove(&client->resource_idr, e->r.resource.handle);
552 if (client->in_shutdown)
553 wake_up(&client->tx_flush_wait);
554 spin_unlock_irqrestore(&client->lock, flags);
556 rsp->type = FW_CDEV_EVENT_RESPONSE;
560 * In the case that sizeof(*rsp) doesn't align with the position of the
561 * data, and the read is short, preserve an extra copy of the data
562 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
563 * for short reads and some apps depended on it, this is both safe
564 * and prudent for compatibility.
566 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
567 queue_event(client, &e->event, rsp, sizeof(*rsp),
568 rsp->data, rsp->length);
570 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
573 /* Drop the idr's reference */
577 static int init_request(struct client *client,
578 struct fw_cdev_send_request *request,
579 int destination_id, int speed)
581 struct outbound_transaction_event *e;
584 if (request->tcode != TCODE_STREAM_DATA &&
585 (request->length > 4096 || request->length > 512 << speed))
588 if (request->tcode == TCODE_WRITE_QUADLET_REQUEST &&
592 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
597 e->response.length = request->length;
598 e->response.closure = request->closure;
601 copy_from_user(e->response.data,
602 u64_to_uptr(request->data), request->length)) {
607 e->r.resource.release = release_transaction;
608 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
612 fw_send_request(client->device->card, &e->r.transaction,
613 request->tcode, destination_id, request->generation,
614 speed, request->offset, e->response.data,
615 request->length, complete_transaction, e);
624 static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
626 switch (arg->send_request.tcode) {
627 case TCODE_WRITE_QUADLET_REQUEST:
628 case TCODE_WRITE_BLOCK_REQUEST:
629 case TCODE_READ_QUADLET_REQUEST:
630 case TCODE_READ_BLOCK_REQUEST:
631 case TCODE_LOCK_MASK_SWAP:
632 case TCODE_LOCK_COMPARE_SWAP:
633 case TCODE_LOCK_FETCH_ADD:
634 case TCODE_LOCK_LITTLE_ADD:
635 case TCODE_LOCK_BOUNDED_ADD:
636 case TCODE_LOCK_WRAP_ADD:
637 case TCODE_LOCK_VENDOR_DEPENDENT:
643 return init_request(client, &arg->send_request, client->device->node_id,
644 client->device->max_speed);
647 static void release_request(struct client *client,
648 struct client_resource *resource)
650 struct inbound_transaction_resource *r = container_of(resource,
651 struct inbound_transaction_resource, resource);
654 fw_request_put(r->request);
656 fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
658 fw_card_put(r->card);
662 static void handle_request(struct fw_card *card, struct fw_request *request,
663 int tcode, int destination, int source,
664 int generation, unsigned long long offset,
665 void *payload, size_t length, void *callback_data)
667 struct address_handler_resource *handler = callback_data;
668 bool is_fcp = is_in_fcp_region(offset, length);
669 struct inbound_transaction_resource *r;
670 struct inbound_transaction_event *e;
674 /* card may be different from handler->client->device->card */
677 // Extend the lifetime of data for request so that its payload is safely accessible in
678 // the process context for the client.
680 fw_request_get(request);
682 r = kmalloc(sizeof(*r), GFP_ATOMIC);
683 e = kmalloc(sizeof(*e), GFP_ATOMIC);
684 if (r == NULL || e == NULL)
688 r->request = request;
693 r->resource.release = release_request;
694 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
698 if (handler->client->version < FW_CDEV_VERSION_EVENT_REQUEST2) {
699 struct fw_cdev_event_request *req = &e->req.request;
702 tcode = TCODE_LOCK_REQUEST;
704 req->type = FW_CDEV_EVENT_REQUEST;
706 req->offset = offset;
707 req->length = length;
708 req->handle = r->resource.handle;
709 req->closure = handler->closure;
710 event_size0 = sizeof(*req);
712 struct fw_cdev_event_request2 *req = &e->req.request2;
714 req->type = FW_CDEV_EVENT_REQUEST2;
716 req->offset = offset;
717 req->source_node_id = source;
718 req->destination_node_id = destination;
719 req->card = card->index;
720 req->generation = generation;
721 req->length = length;
722 req->handle = r->resource.handle;
723 req->closure = handler->closure;
724 event_size0 = sizeof(*req);
727 queue_event(handler->client, &e->event,
728 &e->req, event_size0, r->data, length);
736 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
738 fw_request_put(request);
743 static void release_address_handler(struct client *client,
744 struct client_resource *resource)
746 struct address_handler_resource *r =
747 container_of(resource, struct address_handler_resource, resource);
749 fw_core_remove_address_handler(&r->handler);
753 static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
755 struct fw_cdev_allocate *a = &arg->allocate;
756 struct address_handler_resource *r;
757 struct fw_address_region region;
760 r = kmalloc(sizeof(*r), GFP_KERNEL);
764 region.start = a->offset;
765 if (client->version < FW_CDEV_VERSION_ALLOCATE_REGION_END)
766 region.end = a->offset + a->length;
768 region.end = a->region_end;
770 r->handler.length = a->length;
771 r->handler.address_callback = handle_request;
772 r->handler.callback_data = r;
773 r->closure = a->closure;
776 ret = fw_core_add_address_handler(&r->handler, ®ion);
781 a->offset = r->handler.offset;
783 r->resource.release = release_address_handler;
784 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
786 release_address_handler(client, &r->resource);
789 a->handle = r->resource.handle;
794 static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
796 return release_client_resource(client, arg->deallocate.handle,
797 release_address_handler, NULL);
800 static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
802 struct fw_cdev_send_response *a = &arg->send_response;
803 struct client_resource *resource;
804 struct inbound_transaction_resource *r;
807 if (release_client_resource(client, a->handle,
808 release_request, &resource) < 0)
811 r = container_of(resource, struct inbound_transaction_resource,
814 fw_request_put(r->request);
818 if (a->length != fw_get_response_length(r->request)) {
820 fw_request_put(r->request);
823 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
825 fw_request_put(r->request);
828 fw_send_response(r->card, r->request, a->rcode);
830 fw_card_put(r->card);
836 static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
838 fw_schedule_bus_reset(client->device->card, true,
839 arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
843 static void release_descriptor(struct client *client,
844 struct client_resource *resource)
846 struct descriptor_resource *r =
847 container_of(resource, struct descriptor_resource, resource);
849 fw_core_remove_descriptor(&r->descriptor);
853 static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
855 struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
856 struct descriptor_resource *r;
859 /* Access policy: Allow this ioctl only on local nodes' device files. */
860 if (!client->device->is_local)
866 r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
870 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
875 r->descriptor.length = a->length;
876 r->descriptor.immediate = a->immediate;
877 r->descriptor.key = a->key;
878 r->descriptor.data = r->data;
880 ret = fw_core_add_descriptor(&r->descriptor);
884 r->resource.release = release_descriptor;
885 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
887 fw_core_remove_descriptor(&r->descriptor);
890 a->handle = r->resource.handle;
899 static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
901 return release_client_resource(client, arg->remove_descriptor.handle,
902 release_descriptor, NULL);
905 static void iso_callback(struct fw_iso_context *context, u32 cycle,
906 size_t header_length, void *header, void *data)
908 struct client *client = data;
909 struct iso_interrupt_event *e;
911 e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
915 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
916 e->interrupt.closure = client->iso_closure;
917 e->interrupt.cycle = cycle;
918 e->interrupt.header_length = header_length;
919 memcpy(e->interrupt.header, header, header_length);
920 queue_event(client, &e->event, &e->interrupt,
921 sizeof(e->interrupt) + header_length, NULL, 0);
924 static void iso_mc_callback(struct fw_iso_context *context,
925 dma_addr_t completed, void *data)
927 struct client *client = data;
928 struct iso_interrupt_mc_event *e;
930 e = kmalloc(sizeof(*e), GFP_ATOMIC);
934 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT_MULTICHANNEL;
935 e->interrupt.closure = client->iso_closure;
936 e->interrupt.completed = fw_iso_buffer_lookup(&client->buffer,
938 queue_event(client, &e->event, &e->interrupt,
939 sizeof(e->interrupt), NULL, 0);
942 static enum dma_data_direction iso_dma_direction(struct fw_iso_context *context)
944 if (context->type == FW_ISO_CONTEXT_TRANSMIT)
945 return DMA_TO_DEVICE;
947 return DMA_FROM_DEVICE;
950 static struct fw_iso_context *fw_iso_mc_context_create(struct fw_card *card,
951 fw_iso_mc_callback_t callback,
954 struct fw_iso_context *ctx;
956 ctx = fw_iso_context_create(card, FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL,
957 0, 0, 0, NULL, callback_data);
959 ctx->callback.mc = callback;
964 static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
966 struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
967 struct fw_iso_context *context;
968 union fw_iso_callback cb;
971 BUILD_BUG_ON(FW_CDEV_ISO_CONTEXT_TRANSMIT != FW_ISO_CONTEXT_TRANSMIT ||
972 FW_CDEV_ISO_CONTEXT_RECEIVE != FW_ISO_CONTEXT_RECEIVE ||
973 FW_CDEV_ISO_CONTEXT_RECEIVE_MULTICHANNEL !=
974 FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL);
977 case FW_ISO_CONTEXT_TRANSMIT:
978 if (a->speed > SCODE_3200 || a->channel > 63)
981 cb.sc = iso_callback;
984 case FW_ISO_CONTEXT_RECEIVE:
985 if (a->header_size < 4 || (a->header_size & 3) ||
989 cb.sc = iso_callback;
992 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
993 cb.mc = iso_mc_callback;
1000 if (a->type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL)
1001 context = fw_iso_mc_context_create(client->device->card, cb.mc,
1004 context = fw_iso_context_create(client->device->card, a->type,
1005 a->channel, a->speed,
1006 a->header_size, cb.sc, client);
1007 if (IS_ERR(context))
1008 return PTR_ERR(context);
1009 if (client->version < FW_CDEV_VERSION_AUTO_FLUSH_ISO_OVERFLOW)
1010 context->drop_overflow_headers = true;
1012 /* We only support one context at this time. */
1013 spin_lock_irq(&client->lock);
1014 if (client->iso_context != NULL) {
1015 spin_unlock_irq(&client->lock);
1016 fw_iso_context_destroy(context);
1020 if (!client->buffer_is_mapped) {
1021 ret = fw_iso_buffer_map_dma(&client->buffer,
1022 client->device->card,
1023 iso_dma_direction(context));
1025 spin_unlock_irq(&client->lock);
1026 fw_iso_context_destroy(context);
1030 client->buffer_is_mapped = true;
1032 client->iso_closure = a->closure;
1033 client->iso_context = context;
1034 spin_unlock_irq(&client->lock);
1041 static int ioctl_set_iso_channels(struct client *client, union ioctl_arg *arg)
1043 struct fw_cdev_set_iso_channels *a = &arg->set_iso_channels;
1044 struct fw_iso_context *ctx = client->iso_context;
1046 if (ctx == NULL || a->handle != 0)
1049 return fw_iso_context_set_channels(ctx, &a->channels);
1052 /* Macros for decoding the iso packet control header. */
1053 #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
1054 #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
1055 #define GET_SKIP(v) (((v) >> 17) & 0x01)
1056 #define GET_TAG(v) (((v) >> 18) & 0x03)
1057 #define GET_SY(v) (((v) >> 20) & 0x0f)
1058 #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
1060 static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
1062 struct fw_cdev_queue_iso *a = &arg->queue_iso;
1063 struct fw_cdev_iso_packet __user *p, *end, *next;
1064 struct fw_iso_context *ctx = client->iso_context;
1065 unsigned long payload, buffer_end, transmit_header_bytes = 0;
1069 struct fw_iso_packet packet;
1073 if (ctx == NULL || a->handle != 0)
1077 * If the user passes a non-NULL data pointer, has mmap()'ed
1078 * the iso buffer, and the pointer points inside the buffer,
1079 * we setup the payload pointers accordingly. Otherwise we
1080 * set them both to 0, which will still let packets with
1081 * payload_length == 0 through. In other words, if no packets
1082 * use the indirect payload, the iso buffer need not be mapped
1083 * and the a->data pointer is ignored.
1085 payload = (unsigned long)a->data - client->vm_start;
1086 buffer_end = client->buffer.page_count << PAGE_SHIFT;
1087 if (a->data == 0 || client->buffer.pages == NULL ||
1088 payload >= buffer_end) {
1093 if (ctx->type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL && payload & 3)
1096 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
1098 end = (void __user *)p + a->size;
1101 if (get_user(control, &p->control))
1103 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
1104 u.packet.interrupt = GET_INTERRUPT(control);
1105 u.packet.skip = GET_SKIP(control);
1106 u.packet.tag = GET_TAG(control);
1107 u.packet.sy = GET_SY(control);
1108 u.packet.header_length = GET_HEADER_LENGTH(control);
1110 switch (ctx->type) {
1111 case FW_ISO_CONTEXT_TRANSMIT:
1112 if (u.packet.header_length & 3)
1114 transmit_header_bytes = u.packet.header_length;
1117 case FW_ISO_CONTEXT_RECEIVE:
1118 if (u.packet.header_length == 0 ||
1119 u.packet.header_length % ctx->header_size != 0)
1123 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
1124 if (u.packet.payload_length == 0 ||
1125 u.packet.payload_length & 3)
1130 next = (struct fw_cdev_iso_packet __user *)
1131 &p->header[transmit_header_bytes / 4];
1135 (u.packet.header, p->header, transmit_header_bytes))
1137 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
1138 u.packet.header_length + u.packet.payload_length > 0)
1140 if (payload + u.packet.payload_length > buffer_end)
1143 if (fw_iso_context_queue(ctx, &u.packet,
1144 &client->buffer, payload))
1148 payload += u.packet.payload_length;
1151 fw_iso_context_queue_flush(ctx);
1153 a->size -= uptr_to_u64(p) - a->packets;
1154 a->packets = uptr_to_u64(p);
1155 a->data = client->vm_start + payload;
1160 static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
1162 struct fw_cdev_start_iso *a = &arg->start_iso;
1165 FW_CDEV_ISO_CONTEXT_MATCH_TAG0 != FW_ISO_CONTEXT_MATCH_TAG0 ||
1166 FW_CDEV_ISO_CONTEXT_MATCH_TAG1 != FW_ISO_CONTEXT_MATCH_TAG1 ||
1167 FW_CDEV_ISO_CONTEXT_MATCH_TAG2 != FW_ISO_CONTEXT_MATCH_TAG2 ||
1168 FW_CDEV_ISO_CONTEXT_MATCH_TAG3 != FW_ISO_CONTEXT_MATCH_TAG3 ||
1169 FW_CDEV_ISO_CONTEXT_MATCH_ALL_TAGS != FW_ISO_CONTEXT_MATCH_ALL_TAGS);
1171 if (client->iso_context == NULL || a->handle != 0)
1174 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
1175 (a->tags == 0 || a->tags > 15 || a->sync > 15))
1178 return fw_iso_context_start(client->iso_context,
1179 a->cycle, a->sync, a->tags);
1182 static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
1184 struct fw_cdev_stop_iso *a = &arg->stop_iso;
1186 if (client->iso_context == NULL || a->handle != 0)
1189 return fw_iso_context_stop(client->iso_context);
1192 static int ioctl_flush_iso(struct client *client, union ioctl_arg *arg)
1194 struct fw_cdev_flush_iso *a = &arg->flush_iso;
1196 if (client->iso_context == NULL || a->handle != 0)
1199 return fw_iso_context_flush_completions(client->iso_context);
1202 static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
1204 struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
1205 struct fw_card *card = client->device->card;
1206 struct timespec64 ts = {0, 0};
1210 local_irq_disable();
1212 ret = fw_card_read_cycle_time(card, &cycle_time);
1216 switch (a->clk_id) {
1217 case CLOCK_REALTIME: ktime_get_real_ts64(&ts); break;
1218 case CLOCK_MONOTONIC: ktime_get_ts64(&ts); break;
1219 case CLOCK_MONOTONIC_RAW: ktime_get_raw_ts64(&ts); break;
1226 a->tv_sec = ts.tv_sec;
1227 a->tv_nsec = ts.tv_nsec;
1228 a->cycle_timer = cycle_time;
1233 static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
1235 struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
1236 struct fw_cdev_get_cycle_timer2 ct2;
1238 ct2.clk_id = CLOCK_REALTIME;
1239 ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
1241 a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
1242 a->cycle_timer = ct2.cycle_timer;
1247 static void iso_resource_work(struct work_struct *work)
1249 struct iso_resource_event *e;
1250 struct iso_resource *r =
1251 container_of(work, struct iso_resource, work.work);
1252 struct client *client = r->client;
1253 int generation, channel, bandwidth, todo;
1254 bool skip, free, success;
1256 spin_lock_irq(&client->lock);
1257 generation = client->device->generation;
1259 /* Allow 1000ms grace period for other reallocations. */
1260 if (todo == ISO_RES_ALLOC &&
1261 time_before64(get_jiffies_64(),
1262 client->device->card->reset_jiffies + HZ)) {
1263 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
1266 /* We could be called twice within the same generation. */
1267 skip = todo == ISO_RES_REALLOC &&
1268 r->generation == generation;
1270 free = todo == ISO_RES_DEALLOC ||
1271 todo == ISO_RES_ALLOC_ONCE ||
1272 todo == ISO_RES_DEALLOC_ONCE;
1273 r->generation = generation;
1274 spin_unlock_irq(&client->lock);
1279 bandwidth = r->bandwidth;
1281 fw_iso_resource_manage(client->device->card, generation,
1282 r->channels, &channel, &bandwidth,
1283 todo == ISO_RES_ALLOC ||
1284 todo == ISO_RES_REALLOC ||
1285 todo == ISO_RES_ALLOC_ONCE);
1287 * Is this generation outdated already? As long as this resource sticks
1288 * in the idr, it will be scheduled again for a newer generation or at
1291 if (channel == -EAGAIN &&
1292 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1295 success = channel >= 0 || bandwidth > 0;
1297 spin_lock_irq(&client->lock);
1299 * Transit from allocation to reallocation, except if the client
1300 * requested deallocation in the meantime.
1302 if (r->todo == ISO_RES_ALLOC)
1303 r->todo = ISO_RES_REALLOC;
1305 * Allocation or reallocation failure? Pull this resource out of the
1306 * idr and prepare for deletion, unless the client is shutting down.
1308 if (r->todo == ISO_RES_REALLOC && !success &&
1309 !client->in_shutdown &&
1310 idr_remove(&client->resource_idr, r->resource.handle)) {
1314 spin_unlock_irq(&client->lock);
1316 if (todo == ISO_RES_ALLOC && channel >= 0)
1317 r->channels = 1ULL << channel;
1319 if (todo == ISO_RES_REALLOC && success)
1322 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
1327 r->e_dealloc = NULL;
1329 e->iso_resource.handle = r->resource.handle;
1330 e->iso_resource.channel = channel;
1331 e->iso_resource.bandwidth = bandwidth;
1333 queue_event(client, &e->event,
1334 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
1337 cancel_delayed_work(&r->work);
1339 kfree(r->e_dealloc);
1346 static void release_iso_resource(struct client *client,
1347 struct client_resource *resource)
1349 struct iso_resource *r =
1350 container_of(resource, struct iso_resource, resource);
1352 spin_lock_irq(&client->lock);
1353 r->todo = ISO_RES_DEALLOC;
1354 schedule_iso_resource(r, 0);
1355 spin_unlock_irq(&client->lock);
1358 static int init_iso_resource(struct client *client,
1359 struct fw_cdev_allocate_iso_resource *request, int todo)
1361 struct iso_resource_event *e1, *e2;
1362 struct iso_resource *r;
1365 if ((request->channels == 0 && request->bandwidth == 0) ||
1366 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL)
1369 r = kmalloc(sizeof(*r), GFP_KERNEL);
1370 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1371 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1372 if (r == NULL || e1 == NULL || e2 == NULL) {
1377 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1381 r->channels = request->channels;
1382 r->bandwidth = request->bandwidth;
1386 e1->iso_resource.closure = request->closure;
1387 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1388 e2->iso_resource.closure = request->closure;
1389 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
1391 if (todo == ISO_RES_ALLOC) {
1392 r->resource.release = release_iso_resource;
1393 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
1397 r->resource.release = NULL;
1398 r->resource.handle = -1;
1399 schedule_iso_resource(r, 0);
1401 request->handle = r->resource.handle;
1412 static int ioctl_allocate_iso_resource(struct client *client,
1413 union ioctl_arg *arg)
1415 return init_iso_resource(client,
1416 &arg->allocate_iso_resource, ISO_RES_ALLOC);
1419 static int ioctl_deallocate_iso_resource(struct client *client,
1420 union ioctl_arg *arg)
1422 return release_client_resource(client,
1423 arg->deallocate.handle, release_iso_resource, NULL);
1426 static int ioctl_allocate_iso_resource_once(struct client *client,
1427 union ioctl_arg *arg)
1429 return init_iso_resource(client,
1430 &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
1433 static int ioctl_deallocate_iso_resource_once(struct client *client,
1434 union ioctl_arg *arg)
1436 return init_iso_resource(client,
1437 &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
1441 * Returns a speed code: Maximum speed to or from this device,
1442 * limited by the device's link speed, the local node's link speed,
1443 * and all PHY port speeds between the two links.
1445 static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
1447 return client->device->max_speed;
1450 static int ioctl_send_broadcast_request(struct client *client,
1451 union ioctl_arg *arg)
1453 struct fw_cdev_send_request *a = &arg->send_request;
1456 case TCODE_WRITE_QUADLET_REQUEST:
1457 case TCODE_WRITE_BLOCK_REQUEST:
1463 /* Security policy: Only allow accesses to Units Space. */
1464 if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
1467 return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
1470 static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
1472 struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
1473 struct fw_cdev_send_request request;
1476 if (a->speed > client->device->card->link_speed ||
1477 a->length > 1024 << a->speed)
1480 if (a->tag > 3 || a->channel > 63 || a->sy > 15)
1483 dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
1484 request.tcode = TCODE_STREAM_DATA;
1485 request.length = a->length;
1486 request.closure = a->closure;
1487 request.data = a->data;
1488 request.generation = a->generation;
1490 return init_request(client, &request, dest, a->speed);
1493 static void outbound_phy_packet_callback(struct fw_packet *packet,
1494 struct fw_card *card, int status)
1496 struct outbound_phy_packet_event *e =
1497 container_of(packet, struct outbound_phy_packet_event, p);
1498 struct client *e_client;
1502 case ACK_COMPLETE: e->phy_packet.rcode = RCODE_COMPLETE; break;
1503 /* should never happen with PHY packets: */
1504 case ACK_PENDING: e->phy_packet.rcode = RCODE_COMPLETE; break;
1507 case ACK_BUSY_B: e->phy_packet.rcode = RCODE_BUSY; break;
1508 case ACK_DATA_ERROR: e->phy_packet.rcode = RCODE_DATA_ERROR; break;
1509 case ACK_TYPE_ERROR: e->phy_packet.rcode = RCODE_TYPE_ERROR; break;
1510 /* stale generation; cancelled; on certain controllers: no ack */
1511 default: e->phy_packet.rcode = status; break;
1513 e->phy_packet.data[0] = packet->timestamp;
1515 e_client = e->client;
1516 queue_event(e->client, &e->event, &e->phy_packet,
1517 sizeof(e->phy_packet) + e->phy_packet.length, NULL, 0);
1518 client_put(e_client);
1521 static int ioctl_send_phy_packet(struct client *client, union ioctl_arg *arg)
1523 struct fw_cdev_send_phy_packet *a = &arg->send_phy_packet;
1524 struct fw_card *card = client->device->card;
1525 struct outbound_phy_packet_event *e;
1527 /* Access policy: Allow this ioctl only on local nodes' device files. */
1528 if (!client->device->is_local)
1531 e = kzalloc(sizeof(*e) + 4, GFP_KERNEL);
1537 e->p.speed = SCODE_100;
1538 e->p.generation = a->generation;
1539 e->p.header[0] = TCODE_LINK_INTERNAL << 4;
1540 e->p.header[1] = a->data[0];
1541 e->p.header[2] = a->data[1];
1542 e->p.header_length = 12;
1543 e->p.callback = outbound_phy_packet_callback;
1544 e->phy_packet.closure = a->closure;
1545 e->phy_packet.type = FW_CDEV_EVENT_PHY_PACKET_SENT;
1546 if (is_ping_packet(a->data))
1547 e->phy_packet.length = 4;
1549 card->driver->send_request(card, &e->p);
1554 static int ioctl_receive_phy_packets(struct client *client, union ioctl_arg *arg)
1556 struct fw_cdev_receive_phy_packets *a = &arg->receive_phy_packets;
1557 struct fw_card *card = client->device->card;
1559 /* Access policy: Allow this ioctl only on local nodes' device files. */
1560 if (!client->device->is_local)
1563 spin_lock_irq(&card->lock);
1565 list_move_tail(&client->phy_receiver_link, &card->phy_receiver_list);
1566 client->phy_receiver_closure = a->closure;
1568 spin_unlock_irq(&card->lock);
1573 void fw_cdev_handle_phy_packet(struct fw_card *card, struct fw_packet *p)
1575 struct client *client;
1576 struct inbound_phy_packet_event *e;
1577 unsigned long flags;
1579 spin_lock_irqsave(&card->lock, flags);
1581 list_for_each_entry(client, &card->phy_receiver_list, phy_receiver_link) {
1582 e = kmalloc(sizeof(*e) + 8, GFP_ATOMIC);
1586 e->phy_packet.closure = client->phy_receiver_closure;
1587 e->phy_packet.type = FW_CDEV_EVENT_PHY_PACKET_RECEIVED;
1588 e->phy_packet.rcode = RCODE_COMPLETE;
1589 e->phy_packet.length = 8;
1590 e->phy_packet.data[0] = p->header[1];
1591 e->phy_packet.data[1] = p->header[2];
1592 queue_event(client, &e->event,
1593 &e->phy_packet, sizeof(e->phy_packet) + 8, NULL, 0);
1596 spin_unlock_irqrestore(&card->lock, flags);
1599 static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
1600 [0x00] = ioctl_get_info,
1601 [0x01] = ioctl_send_request,
1602 [0x02] = ioctl_allocate,
1603 [0x03] = ioctl_deallocate,
1604 [0x04] = ioctl_send_response,
1605 [0x05] = ioctl_initiate_bus_reset,
1606 [0x06] = ioctl_add_descriptor,
1607 [0x07] = ioctl_remove_descriptor,
1608 [0x08] = ioctl_create_iso_context,
1609 [0x09] = ioctl_queue_iso,
1610 [0x0a] = ioctl_start_iso,
1611 [0x0b] = ioctl_stop_iso,
1612 [0x0c] = ioctl_get_cycle_timer,
1613 [0x0d] = ioctl_allocate_iso_resource,
1614 [0x0e] = ioctl_deallocate_iso_resource,
1615 [0x0f] = ioctl_allocate_iso_resource_once,
1616 [0x10] = ioctl_deallocate_iso_resource_once,
1617 [0x11] = ioctl_get_speed,
1618 [0x12] = ioctl_send_broadcast_request,
1619 [0x13] = ioctl_send_stream_packet,
1620 [0x14] = ioctl_get_cycle_timer2,
1621 [0x15] = ioctl_send_phy_packet,
1622 [0x16] = ioctl_receive_phy_packets,
1623 [0x17] = ioctl_set_iso_channels,
1624 [0x18] = ioctl_flush_iso,
1627 static int dispatch_ioctl(struct client *client,
1628 unsigned int cmd, void __user *arg)
1630 union ioctl_arg buffer;
1633 if (fw_device_is_shutdown(client->device))
1636 if (_IOC_TYPE(cmd) != '#' ||
1637 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
1638 _IOC_SIZE(cmd) > sizeof(buffer))
1641 memset(&buffer, 0, sizeof(buffer));
1643 if (_IOC_DIR(cmd) & _IOC_WRITE)
1644 if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
1647 ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
1651 if (_IOC_DIR(cmd) & _IOC_READ)
1652 if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
1658 static long fw_device_op_ioctl(struct file *file,
1659 unsigned int cmd, unsigned long arg)
1661 return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
1664 static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1666 struct client *client = file->private_data;
1668 int page_count, ret;
1670 if (fw_device_is_shutdown(client->device))
1673 /* FIXME: We could support multiple buffers, but we don't. */
1674 if (client->buffer.pages != NULL)
1677 if (!(vma->vm_flags & VM_SHARED))
1680 if (vma->vm_start & ~PAGE_MASK)
1683 client->vm_start = vma->vm_start;
1684 size = vma->vm_end - vma->vm_start;
1685 page_count = size >> PAGE_SHIFT;
1686 if (size & ~PAGE_MASK)
1689 ret = fw_iso_buffer_alloc(&client->buffer, page_count);
1693 spin_lock_irq(&client->lock);
1694 if (client->iso_context) {
1695 ret = fw_iso_buffer_map_dma(&client->buffer,
1696 client->device->card,
1697 iso_dma_direction(client->iso_context));
1698 client->buffer_is_mapped = (ret == 0);
1700 spin_unlock_irq(&client->lock);
1704 ret = vm_map_pages_zero(vma, client->buffer.pages,
1705 client->buffer.page_count);
1711 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1715 static int is_outbound_transaction_resource(int id, void *p, void *data)
1717 struct client_resource *resource = p;
1719 return resource->release == release_transaction;
1722 static int has_outbound_transactions(struct client *client)
1726 spin_lock_irq(&client->lock);
1727 ret = idr_for_each(&client->resource_idr,
1728 is_outbound_transaction_resource, NULL);
1729 spin_unlock_irq(&client->lock);
1734 static int shutdown_resource(int id, void *p, void *data)
1736 struct client_resource *resource = p;
1737 struct client *client = data;
1739 resource->release(client, resource);
1745 static int fw_device_op_release(struct inode *inode, struct file *file)
1747 struct client *client = file->private_data;
1748 struct event *event, *next_event;
1750 spin_lock_irq(&client->device->card->lock);
1751 list_del(&client->phy_receiver_link);
1752 spin_unlock_irq(&client->device->card->lock);
1754 mutex_lock(&client->device->client_list_mutex);
1755 list_del(&client->link);
1756 mutex_unlock(&client->device->client_list_mutex);
1758 if (client->iso_context)
1759 fw_iso_context_destroy(client->iso_context);
1761 if (client->buffer.pages)
1762 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1764 /* Freeze client->resource_idr and client->event_list */
1765 spin_lock_irq(&client->lock);
1766 client->in_shutdown = true;
1767 spin_unlock_irq(&client->lock);
1769 wait_event(client->tx_flush_wait, !has_outbound_transactions(client));
1771 idr_for_each(&client->resource_idr, shutdown_resource, client);
1772 idr_destroy(&client->resource_idr);
1774 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1782 static __poll_t fw_device_op_poll(struct file *file, poll_table * pt)
1784 struct client *client = file->private_data;
1787 poll_wait(file, &client->wait, pt);
1789 if (fw_device_is_shutdown(client->device))
1790 mask |= EPOLLHUP | EPOLLERR;
1791 if (!list_empty(&client->event_list))
1792 mask |= EPOLLIN | EPOLLRDNORM;
1797 const struct file_operations fw_device_ops = {
1798 .owner = THIS_MODULE,
1799 .llseek = no_llseek,
1800 .open = fw_device_op_open,
1801 .read = fw_device_op_read,
1802 .unlocked_ioctl = fw_device_op_ioctl,
1803 .mmap = fw_device_op_mmap,
1804 .release = fw_device_op_release,
1805 .poll = fw_device_op_poll,
1806 .compat_ioctl = compat_ptr_ioctl,