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>
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>
50 * ABI version history is documented in linux/firewire-cdev.h.
52 #define FW_CDEV_KERNEL_VERSION 3
56 struct fw_device *device;
60 struct idr resource_idr;
61 struct list_head event_list;
62 wait_queue_head_t wait;
63 u64 bus_reset_closure;
65 struct fw_iso_context *iso_context;
67 struct fw_iso_buffer buffer;
68 unsigned long vm_start;
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;
120 struct descriptor_resource {
121 struct client_resource resource;
122 struct fw_descriptor descriptor;
126 struct iso_resource {
127 struct client_resource resource;
128 struct client *client;
129 /* Schedule work and access todo only with client->lock held. */
130 struct delayed_work work;
131 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
132 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
136 __be32 transaction_data[2];
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 (!schedule_delayed_work(&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;
174 struct fw_cdev_event_response response;
177 struct inbound_transaction_event {
179 struct fw_cdev_event_request request;
182 struct iso_interrupt_event {
184 struct fw_cdev_event_iso_interrupt interrupt;
187 struct iso_resource_event {
189 struct fw_cdev_event_iso_resource iso_resource;
192 static inline void __user *u64_to_uptr(__u64 value)
194 return (void __user *)(unsigned long)value;
197 static inline __u64 uptr_to_u64(void __user *ptr)
199 return (__u64)(unsigned long)ptr;
202 static int fw_device_op_open(struct inode *inode, struct file *file)
204 struct fw_device *device;
205 struct client *client;
207 device = fw_device_get_by_devt(inode->i_rdev);
211 if (fw_device_is_shutdown(device)) {
212 fw_device_put(device);
216 client = kzalloc(sizeof(*client), GFP_KERNEL);
217 if (client == NULL) {
218 fw_device_put(device);
222 client->device = device;
223 spin_lock_init(&client->lock);
224 idr_init(&client->resource_idr);
225 INIT_LIST_HEAD(&client->event_list);
226 init_waitqueue_head(&client->wait);
227 kref_init(&client->kref);
229 file->private_data = client;
231 mutex_lock(&device->client_list_mutex);
232 list_add_tail(&client->link, &device->client_list);
233 mutex_unlock(&device->client_list_mutex);
235 return nonseekable_open(inode, file);
238 static void queue_event(struct client *client, struct event *event,
239 void *data0, size_t size0, void *data1, size_t size1)
243 event->v[0].data = data0;
244 event->v[0].size = size0;
245 event->v[1].data = data1;
246 event->v[1].size = size1;
248 spin_lock_irqsave(&client->lock, flags);
249 if (client->in_shutdown)
252 list_add_tail(&event->link, &client->event_list);
253 spin_unlock_irqrestore(&client->lock, flags);
255 wake_up_interruptible(&client->wait);
258 static int dequeue_event(struct client *client,
259 char __user *buffer, size_t count)
265 ret = wait_event_interruptible(client->wait,
266 !list_empty(&client->event_list) ||
267 fw_device_is_shutdown(client->device));
271 if (list_empty(&client->event_list) &&
272 fw_device_is_shutdown(client->device))
275 spin_lock_irq(&client->lock);
276 event = list_first_entry(&client->event_list, struct event, link);
277 list_del(&event->link);
278 spin_unlock_irq(&client->lock);
281 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
282 size = min(event->v[i].size, count - total);
283 if (copy_to_user(buffer + total, event->v[i].data, size)) {
297 static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
298 size_t count, loff_t *offset)
300 struct client *client = file->private_data;
302 return dequeue_event(client, buffer, count);
305 static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
306 struct client *client)
308 struct fw_card *card = client->device->card;
310 spin_lock_irq(&card->lock);
312 event->closure = client->bus_reset_closure;
313 event->type = FW_CDEV_EVENT_BUS_RESET;
314 event->generation = client->device->generation;
315 event->node_id = client->device->node_id;
316 event->local_node_id = card->local_node->node_id;
317 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
318 event->irm_node_id = card->irm_node->node_id;
319 event->root_node_id = card->root_node->node_id;
321 spin_unlock_irq(&card->lock);
324 static void for_each_client(struct fw_device *device,
325 void (*callback)(struct client *client))
329 mutex_lock(&device->client_list_mutex);
330 list_for_each_entry(c, &device->client_list, link)
332 mutex_unlock(&device->client_list_mutex);
335 static int schedule_reallocations(int id, void *p, void *data)
337 schedule_if_iso_resource(p);
342 static void queue_bus_reset_event(struct client *client)
344 struct bus_reset_event *e;
346 e = kzalloc(sizeof(*e), GFP_KERNEL);
348 fw_notify("Out of memory when allocating bus reset event\n");
352 fill_bus_reset_event(&e->reset, client);
354 queue_event(client, &e->event,
355 &e->reset, sizeof(e->reset), NULL, 0);
357 spin_lock_irq(&client->lock);
358 idr_for_each(&client->resource_idr, schedule_reallocations, client);
359 spin_unlock_irq(&client->lock);
362 void fw_device_cdev_update(struct fw_device *device)
364 for_each_client(device, queue_bus_reset_event);
367 static void wake_up_client(struct client *client)
369 wake_up_interruptible(&client->wait);
372 void fw_device_cdev_remove(struct fw_device *device)
374 for_each_client(device, wake_up_client);
378 struct fw_cdev_get_info get_info;
379 struct fw_cdev_send_request send_request;
380 struct fw_cdev_allocate allocate;
381 struct fw_cdev_deallocate deallocate;
382 struct fw_cdev_send_response send_response;
383 struct fw_cdev_initiate_bus_reset initiate_bus_reset;
384 struct fw_cdev_add_descriptor add_descriptor;
385 struct fw_cdev_remove_descriptor remove_descriptor;
386 struct fw_cdev_create_iso_context create_iso_context;
387 struct fw_cdev_queue_iso queue_iso;
388 struct fw_cdev_start_iso start_iso;
389 struct fw_cdev_stop_iso stop_iso;
390 struct fw_cdev_get_cycle_timer get_cycle_timer;
391 struct fw_cdev_allocate_iso_resource allocate_iso_resource;
392 struct fw_cdev_send_stream_packet send_stream_packet;
393 struct fw_cdev_get_cycle_timer2 get_cycle_timer2;
396 static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
398 struct fw_cdev_get_info *a = &arg->get_info;
399 struct fw_cdev_event_bus_reset bus_reset;
400 unsigned long ret = 0;
402 client->version = a->version;
403 a->version = FW_CDEV_KERNEL_VERSION;
404 a->card = client->device->card->index;
406 down_read(&fw_device_rwsem);
409 size_t want = a->rom_length;
410 size_t have = client->device->config_rom_length * 4;
412 ret = copy_to_user(u64_to_uptr(a->rom),
413 client->device->config_rom, min(want, have));
415 a->rom_length = client->device->config_rom_length * 4;
417 up_read(&fw_device_rwsem);
422 client->bus_reset_closure = a->bus_reset_closure;
423 if (a->bus_reset != 0) {
424 fill_bus_reset_event(&bus_reset, client);
425 if (copy_to_user(u64_to_uptr(a->bus_reset),
426 &bus_reset, sizeof(bus_reset)))
433 static int add_client_resource(struct client *client,
434 struct client_resource *resource, gfp_t gfp_mask)
440 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
443 spin_lock_irqsave(&client->lock, flags);
444 if (client->in_shutdown)
447 ret = idr_get_new(&client->resource_idr, resource,
451 schedule_if_iso_resource(resource);
453 spin_unlock_irqrestore(&client->lock, flags);
458 return ret < 0 ? ret : 0;
461 static int release_client_resource(struct client *client, u32 handle,
462 client_resource_release_fn_t release,
463 struct client_resource **return_resource)
465 struct client_resource *resource;
467 spin_lock_irq(&client->lock);
468 if (client->in_shutdown)
471 resource = idr_find(&client->resource_idr, handle);
472 if (resource && resource->release == release)
473 idr_remove(&client->resource_idr, handle);
474 spin_unlock_irq(&client->lock);
476 if (!(resource && resource->release == release))
480 *return_resource = resource;
482 resource->release(client, resource);
489 static void release_transaction(struct client *client,
490 struct client_resource *resource)
492 struct outbound_transaction_resource *r = container_of(resource,
493 struct outbound_transaction_resource, resource);
495 fw_cancel_transaction(client->device->card, &r->transaction);
498 static void complete_transaction(struct fw_card *card, int rcode,
499 void *payload, size_t length, void *data)
501 struct outbound_transaction_event *e = data;
502 struct fw_cdev_event_response *rsp = &e->response;
503 struct client *client = e->client;
506 if (length < rsp->length)
507 rsp->length = length;
508 if (rcode == RCODE_COMPLETE)
509 memcpy(rsp->data, payload, rsp->length);
511 spin_lock_irqsave(&client->lock, flags);
513 * 1. If called while in shutdown, the idr tree must be left untouched.
514 * The idr handle will be removed and the client reference will be
516 * 2. If the call chain was release_client_resource ->
517 * release_transaction -> complete_transaction (instead of a normal
518 * conclusion of the transaction), i.e. if this resource was already
519 * unregistered from the idr, the client reference will be dropped
520 * by release_client_resource and we must not drop it here.
522 if (!client->in_shutdown &&
523 idr_find(&client->resource_idr, e->r.resource.handle)) {
524 idr_remove(&client->resource_idr, e->r.resource.handle);
525 /* Drop the idr's reference */
528 spin_unlock_irqrestore(&client->lock, flags);
530 rsp->type = FW_CDEV_EVENT_RESPONSE;
534 * In the case that sizeof(*rsp) doesn't align with the position of the
535 * data, and the read is short, preserve an extra copy of the data
536 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
537 * for short reads and some apps depended on it, this is both safe
538 * and prudent for compatibility.
540 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
541 queue_event(client, &e->event, rsp, sizeof(*rsp),
542 rsp->data, rsp->length);
544 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
547 /* Drop the transaction callback's reference */
551 static int init_request(struct client *client,
552 struct fw_cdev_send_request *request,
553 int destination_id, int speed)
555 struct outbound_transaction_event *e;
558 if (request->tcode != TCODE_STREAM_DATA &&
559 (request->length > 4096 || request->length > 512 << speed))
562 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
567 e->response.length = request->length;
568 e->response.closure = request->closure;
571 copy_from_user(e->response.data,
572 u64_to_uptr(request->data), request->length)) {
577 e->r.resource.release = release_transaction;
578 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
582 /* Get a reference for the transaction callback */
585 fw_send_request(client->device->card, &e->r.transaction,
586 request->tcode, destination_id, request->generation,
587 speed, request->offset, e->response.data,
588 request->length, complete_transaction, e);
597 static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
599 switch (arg->send_request.tcode) {
600 case TCODE_WRITE_QUADLET_REQUEST:
601 case TCODE_WRITE_BLOCK_REQUEST:
602 case TCODE_READ_QUADLET_REQUEST:
603 case TCODE_READ_BLOCK_REQUEST:
604 case TCODE_LOCK_MASK_SWAP:
605 case TCODE_LOCK_COMPARE_SWAP:
606 case TCODE_LOCK_FETCH_ADD:
607 case TCODE_LOCK_LITTLE_ADD:
608 case TCODE_LOCK_BOUNDED_ADD:
609 case TCODE_LOCK_WRAP_ADD:
610 case TCODE_LOCK_VENDOR_DEPENDENT:
616 return init_request(client, &arg->send_request, client->device->node_id,
617 client->device->max_speed);
620 static inline bool is_fcp_request(struct fw_request *request)
622 return request == NULL;
625 static void release_request(struct client *client,
626 struct client_resource *resource)
628 struct inbound_transaction_resource *r = container_of(resource,
629 struct inbound_transaction_resource, resource);
631 if (is_fcp_request(r->request))
634 fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
636 fw_card_put(r->card);
640 static void handle_request(struct fw_card *card, struct fw_request *request,
641 int tcode, int destination, int source,
642 int generation, unsigned long long offset,
643 void *payload, size_t length, void *callback_data)
645 struct address_handler_resource *handler = callback_data;
646 struct inbound_transaction_resource *r;
647 struct inbound_transaction_event *e;
648 void *fcp_frame = NULL;
651 /* card may be different from handler->client->device->card */
654 r = kmalloc(sizeof(*r), GFP_ATOMIC);
655 e = kmalloc(sizeof(*e), GFP_ATOMIC);
656 if (r == NULL || e == NULL)
660 r->request = request;
664 if (is_fcp_request(request)) {
666 * FIXME: Let core-transaction.c manage a
667 * single reference-counted copy?
669 fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
670 if (fcp_frame == NULL)
676 r->resource.release = release_request;
677 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
681 e->request.type = FW_CDEV_EVENT_REQUEST;
682 e->request.tcode = tcode;
683 e->request.offset = offset;
684 e->request.length = length;
685 e->request.handle = r->resource.handle;
686 e->request.closure = handler->closure;
688 queue_event(handler->client, &e->event,
689 &e->request, sizeof(e->request), r->data, length);
697 if (!is_fcp_request(request))
698 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
703 static void release_address_handler(struct client *client,
704 struct client_resource *resource)
706 struct address_handler_resource *r =
707 container_of(resource, struct address_handler_resource, resource);
709 fw_core_remove_address_handler(&r->handler);
713 static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
715 struct fw_cdev_allocate *a = &arg->allocate;
716 struct address_handler_resource *r;
717 struct fw_address_region region;
720 r = kmalloc(sizeof(*r), GFP_KERNEL);
724 region.start = a->offset;
725 region.end = a->offset + a->length;
726 r->handler.length = a->length;
727 r->handler.address_callback = handle_request;
728 r->handler.callback_data = r;
729 r->closure = a->closure;
732 ret = fw_core_add_address_handler(&r->handler, ®ion);
738 r->resource.release = release_address_handler;
739 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
741 release_address_handler(client, &r->resource);
744 a->handle = r->resource.handle;
749 static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
751 return release_client_resource(client, arg->deallocate.handle,
752 release_address_handler, NULL);
755 static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
757 struct fw_cdev_send_response *a = &arg->send_response;
758 struct client_resource *resource;
759 struct inbound_transaction_resource *r;
762 if (release_client_resource(client, a->handle,
763 release_request, &resource) < 0)
766 r = container_of(resource, struct inbound_transaction_resource,
768 if (is_fcp_request(r->request))
771 if (a->length != fw_get_response_length(r->request)) {
776 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
781 fw_send_response(r->card, r->request, a->rcode);
783 fw_card_put(r->card);
789 static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
791 return fw_core_initiate_bus_reset(client->device->card,
792 arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
795 static void release_descriptor(struct client *client,
796 struct client_resource *resource)
798 struct descriptor_resource *r =
799 container_of(resource, struct descriptor_resource, resource);
801 fw_core_remove_descriptor(&r->descriptor);
805 static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
807 struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
808 struct descriptor_resource *r;
811 /* Access policy: Allow this ioctl only on local nodes' device files. */
812 if (!client->device->is_local)
818 r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
822 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
827 r->descriptor.length = a->length;
828 r->descriptor.immediate = a->immediate;
829 r->descriptor.key = a->key;
830 r->descriptor.data = r->data;
832 ret = fw_core_add_descriptor(&r->descriptor);
836 r->resource.release = release_descriptor;
837 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
839 fw_core_remove_descriptor(&r->descriptor);
842 a->handle = r->resource.handle;
851 static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
853 return release_client_resource(client, arg->remove_descriptor.handle,
854 release_descriptor, NULL);
857 static void iso_callback(struct fw_iso_context *context, u32 cycle,
858 size_t header_length, void *header, void *data)
860 struct client *client = data;
861 struct iso_interrupt_event *e;
863 e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
867 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
868 e->interrupt.closure = client->iso_closure;
869 e->interrupt.cycle = cycle;
870 e->interrupt.header_length = header_length;
871 memcpy(e->interrupt.header, header, header_length);
872 queue_event(client, &e->event, &e->interrupt,
873 sizeof(e->interrupt) + header_length, NULL, 0);
876 static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
878 struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
879 struct fw_iso_context *context;
885 case FW_ISO_CONTEXT_RECEIVE:
886 if (a->header_size < 4 || (a->header_size & 3))
890 case FW_ISO_CONTEXT_TRANSMIT:
891 if (a->speed > SCODE_3200)
899 context = fw_iso_context_create(client->device->card, a->type,
900 a->channel, a->speed, a->header_size,
901 iso_callback, client);
903 return PTR_ERR(context);
905 /* We only support one context at this time. */
906 spin_lock_irq(&client->lock);
907 if (client->iso_context != NULL) {
908 spin_unlock_irq(&client->lock);
909 fw_iso_context_destroy(context);
912 client->iso_closure = a->closure;
913 client->iso_context = context;
914 spin_unlock_irq(&client->lock);
921 /* Macros for decoding the iso packet control header. */
922 #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
923 #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
924 #define GET_SKIP(v) (((v) >> 17) & 0x01)
925 #define GET_TAG(v) (((v) >> 18) & 0x03)
926 #define GET_SY(v) (((v) >> 20) & 0x0f)
927 #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
929 static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
931 struct fw_cdev_queue_iso *a = &arg->queue_iso;
932 struct fw_cdev_iso_packet __user *p, *end, *next;
933 struct fw_iso_context *ctx = client->iso_context;
934 unsigned long payload, buffer_end, header_length;
938 struct fw_iso_packet packet;
942 if (ctx == NULL || a->handle != 0)
946 * If the user passes a non-NULL data pointer, has mmap()'ed
947 * the iso buffer, and the pointer points inside the buffer,
948 * we setup the payload pointers accordingly. Otherwise we
949 * set them both to 0, which will still let packets with
950 * payload_length == 0 through. In other words, if no packets
951 * use the indirect payload, the iso buffer need not be mapped
952 * and the a->data pointer is ignored.
955 payload = (unsigned long)a->data - client->vm_start;
956 buffer_end = client->buffer.page_count << PAGE_SHIFT;
957 if (a->data == 0 || client->buffer.pages == NULL ||
958 payload >= buffer_end) {
963 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
965 if (!access_ok(VERIFY_READ, p, a->size))
968 end = (void __user *)p + a->size;
971 if (get_user(control, &p->control))
973 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
974 u.packet.interrupt = GET_INTERRUPT(control);
975 u.packet.skip = GET_SKIP(control);
976 u.packet.tag = GET_TAG(control);
977 u.packet.sy = GET_SY(control);
978 u.packet.header_length = GET_HEADER_LENGTH(control);
980 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
981 if (u.packet.header_length % 4 != 0)
983 header_length = u.packet.header_length;
986 * We require that header_length is a multiple of
987 * the fixed header size, ctx->header_size.
989 if (ctx->header_size == 0) {
990 if (u.packet.header_length > 0)
992 } else if (u.packet.header_length == 0 ||
993 u.packet.header_length % ctx->header_size != 0) {
999 next = (struct fw_cdev_iso_packet __user *)
1000 &p->header[header_length / 4];
1003 if (__copy_from_user
1004 (u.packet.header, p->header, header_length))
1006 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
1007 u.packet.header_length + u.packet.payload_length > 0)
1009 if (payload + u.packet.payload_length > buffer_end)
1012 if (fw_iso_context_queue(ctx, &u.packet,
1013 &client->buffer, payload))
1017 payload += u.packet.payload_length;
1021 a->size -= uptr_to_u64(p) - a->packets;
1022 a->packets = uptr_to_u64(p);
1023 a->data = client->vm_start + payload;
1028 static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
1030 struct fw_cdev_start_iso *a = &arg->start_iso;
1032 if (client->iso_context == NULL || a->handle != 0)
1035 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
1036 (a->tags == 0 || a->tags > 15 || a->sync > 15))
1039 return fw_iso_context_start(client->iso_context,
1040 a->cycle, a->sync, a->tags);
1043 static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
1045 struct fw_cdev_stop_iso *a = &arg->stop_iso;
1047 if (client->iso_context == NULL || a->handle != 0)
1050 return fw_iso_context_stop(client->iso_context);
1053 static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
1055 struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
1056 struct fw_card *card = client->device->card;
1057 struct timespec ts = {0, 0};
1061 local_irq_disable();
1063 cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
1065 switch (a->clk_id) {
1066 case CLOCK_REALTIME: getnstimeofday(&ts); break;
1067 case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
1068 case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
1075 a->tv_sec = ts.tv_sec;
1076 a->tv_nsec = ts.tv_nsec;
1077 a->cycle_timer = cycle_time;
1082 static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
1084 struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
1085 struct fw_cdev_get_cycle_timer2 ct2;
1087 ct2.clk_id = CLOCK_REALTIME;
1088 ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
1090 a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
1091 a->cycle_timer = ct2.cycle_timer;
1096 static void iso_resource_work(struct work_struct *work)
1098 struct iso_resource_event *e;
1099 struct iso_resource *r =
1100 container_of(work, struct iso_resource, work.work);
1101 struct client *client = r->client;
1102 int generation, channel, bandwidth, todo;
1103 bool skip, free, success;
1105 spin_lock_irq(&client->lock);
1106 generation = client->device->generation;
1108 /* Allow 1000ms grace period for other reallocations. */
1109 if (todo == ISO_RES_ALLOC &&
1110 time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
1111 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
1114 /* We could be called twice within the same generation. */
1115 skip = todo == ISO_RES_REALLOC &&
1116 r->generation == generation;
1118 free = todo == ISO_RES_DEALLOC ||
1119 todo == ISO_RES_ALLOC_ONCE ||
1120 todo == ISO_RES_DEALLOC_ONCE;
1121 r->generation = generation;
1122 spin_unlock_irq(&client->lock);
1127 bandwidth = r->bandwidth;
1129 fw_iso_resource_manage(client->device->card, generation,
1130 r->channels, &channel, &bandwidth,
1131 todo == ISO_RES_ALLOC ||
1132 todo == ISO_RES_REALLOC ||
1133 todo == ISO_RES_ALLOC_ONCE,
1134 r->transaction_data);
1136 * Is this generation outdated already? As long as this resource sticks
1137 * in the idr, it will be scheduled again for a newer generation or at
1140 if (channel == -EAGAIN &&
1141 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1144 success = channel >= 0 || bandwidth > 0;
1146 spin_lock_irq(&client->lock);
1148 * Transit from allocation to reallocation, except if the client
1149 * requested deallocation in the meantime.
1151 if (r->todo == ISO_RES_ALLOC)
1152 r->todo = ISO_RES_REALLOC;
1154 * Allocation or reallocation failure? Pull this resource out of the
1155 * idr and prepare for deletion, unless the client is shutting down.
1157 if (r->todo == ISO_RES_REALLOC && !success &&
1158 !client->in_shutdown &&
1159 idr_find(&client->resource_idr, r->resource.handle)) {
1160 idr_remove(&client->resource_idr, r->resource.handle);
1164 spin_unlock_irq(&client->lock);
1166 if (todo == ISO_RES_ALLOC && channel >= 0)
1167 r->channels = 1ULL << channel;
1169 if (todo == ISO_RES_REALLOC && success)
1172 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
1177 r->e_dealloc = NULL;
1179 e->iso_resource.handle = r->resource.handle;
1180 e->iso_resource.channel = channel;
1181 e->iso_resource.bandwidth = bandwidth;
1183 queue_event(client, &e->event,
1184 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
1187 cancel_delayed_work(&r->work);
1189 kfree(r->e_dealloc);
1196 static void release_iso_resource(struct client *client,
1197 struct client_resource *resource)
1199 struct iso_resource *r =
1200 container_of(resource, struct iso_resource, resource);
1202 spin_lock_irq(&client->lock);
1203 r->todo = ISO_RES_DEALLOC;
1204 schedule_iso_resource(r, 0);
1205 spin_unlock_irq(&client->lock);
1208 static int init_iso_resource(struct client *client,
1209 struct fw_cdev_allocate_iso_resource *request, int todo)
1211 struct iso_resource_event *e1, *e2;
1212 struct iso_resource *r;
1215 if ((request->channels == 0 && request->bandwidth == 0) ||
1216 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1217 request->bandwidth < 0)
1220 r = kmalloc(sizeof(*r), GFP_KERNEL);
1221 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1222 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1223 if (r == NULL || e1 == NULL || e2 == NULL) {
1228 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1232 r->channels = request->channels;
1233 r->bandwidth = request->bandwidth;
1237 e1->iso_resource.closure = request->closure;
1238 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1239 e2->iso_resource.closure = request->closure;
1240 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
1242 if (todo == ISO_RES_ALLOC) {
1243 r->resource.release = release_iso_resource;
1244 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
1248 r->resource.release = NULL;
1249 r->resource.handle = -1;
1250 schedule_iso_resource(r, 0);
1252 request->handle = r->resource.handle;
1263 static int ioctl_allocate_iso_resource(struct client *client,
1264 union ioctl_arg *arg)
1266 return init_iso_resource(client,
1267 &arg->allocate_iso_resource, ISO_RES_ALLOC);
1270 static int ioctl_deallocate_iso_resource(struct client *client,
1271 union ioctl_arg *arg)
1273 return release_client_resource(client,
1274 arg->deallocate.handle, release_iso_resource, NULL);
1277 static int ioctl_allocate_iso_resource_once(struct client *client,
1278 union ioctl_arg *arg)
1280 return init_iso_resource(client,
1281 &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
1284 static int ioctl_deallocate_iso_resource_once(struct client *client,
1285 union ioctl_arg *arg)
1287 return init_iso_resource(client,
1288 &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
1292 * Returns a speed code: Maximum speed to or from this device,
1293 * limited by the device's link speed, the local node's link speed,
1294 * and all PHY port speeds between the two links.
1296 static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
1298 return client->device->max_speed;
1301 static int ioctl_send_broadcast_request(struct client *client,
1302 union ioctl_arg *arg)
1304 struct fw_cdev_send_request *a = &arg->send_request;
1307 case TCODE_WRITE_QUADLET_REQUEST:
1308 case TCODE_WRITE_BLOCK_REQUEST:
1314 /* Security policy: Only allow accesses to Units Space. */
1315 if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
1318 return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
1321 static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
1323 struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
1324 struct fw_cdev_send_request request;
1327 if (a->speed > client->device->card->link_speed ||
1328 a->length > 1024 << a->speed)
1331 if (a->tag > 3 || a->channel > 63 || a->sy > 15)
1334 dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
1335 request.tcode = TCODE_STREAM_DATA;
1336 request.length = a->length;
1337 request.closure = a->closure;
1338 request.data = a->data;
1339 request.generation = a->generation;
1341 return init_request(client, &request, dest, a->speed);
1344 static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
1349 ioctl_send_response,
1350 ioctl_initiate_bus_reset,
1351 ioctl_add_descriptor,
1352 ioctl_remove_descriptor,
1353 ioctl_create_iso_context,
1357 ioctl_get_cycle_timer,
1358 ioctl_allocate_iso_resource,
1359 ioctl_deallocate_iso_resource,
1360 ioctl_allocate_iso_resource_once,
1361 ioctl_deallocate_iso_resource_once,
1363 ioctl_send_broadcast_request,
1364 ioctl_send_stream_packet,
1365 ioctl_get_cycle_timer2,
1368 static int dispatch_ioctl(struct client *client,
1369 unsigned int cmd, void __user *arg)
1371 union ioctl_arg buffer;
1374 if (fw_device_is_shutdown(client->device))
1377 if (_IOC_TYPE(cmd) != '#' ||
1378 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
1379 _IOC_SIZE(cmd) > sizeof(buffer))
1382 if (_IOC_DIR(cmd) == _IOC_READ)
1383 memset(&buffer, 0, _IOC_SIZE(cmd));
1385 if (_IOC_DIR(cmd) & _IOC_WRITE)
1386 if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
1389 ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
1393 if (_IOC_DIR(cmd) & _IOC_READ)
1394 if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
1400 static long fw_device_op_ioctl(struct file *file,
1401 unsigned int cmd, unsigned long arg)
1403 return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
1406 #ifdef CONFIG_COMPAT
1407 static long fw_device_op_compat_ioctl(struct file *file,
1408 unsigned int cmd, unsigned long arg)
1410 return dispatch_ioctl(file->private_data, cmd, compat_ptr(arg));
1414 static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1416 struct client *client = file->private_data;
1417 enum dma_data_direction direction;
1419 int page_count, ret;
1421 if (fw_device_is_shutdown(client->device))
1424 /* FIXME: We could support multiple buffers, but we don't. */
1425 if (client->buffer.pages != NULL)
1428 if (!(vma->vm_flags & VM_SHARED))
1431 if (vma->vm_start & ~PAGE_MASK)
1434 client->vm_start = vma->vm_start;
1435 size = vma->vm_end - vma->vm_start;
1436 page_count = size >> PAGE_SHIFT;
1437 if (size & ~PAGE_MASK)
1440 if (vma->vm_flags & VM_WRITE)
1441 direction = DMA_TO_DEVICE;
1443 direction = DMA_FROM_DEVICE;
1445 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1446 page_count, direction);
1450 ret = fw_iso_buffer_map(&client->buffer, vma);
1452 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1457 static int shutdown_resource(int id, void *p, void *data)
1459 struct client_resource *resource = p;
1460 struct client *client = data;
1462 resource->release(client, resource);
1468 static int fw_device_op_release(struct inode *inode, struct file *file)
1470 struct client *client = file->private_data;
1471 struct event *event, *next_event;
1473 mutex_lock(&client->device->client_list_mutex);
1474 list_del(&client->link);
1475 mutex_unlock(&client->device->client_list_mutex);
1477 if (client->iso_context)
1478 fw_iso_context_destroy(client->iso_context);
1480 if (client->buffer.pages)
1481 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1483 /* Freeze client->resource_idr and client->event_list */
1484 spin_lock_irq(&client->lock);
1485 client->in_shutdown = true;
1486 spin_unlock_irq(&client->lock);
1488 idr_for_each(&client->resource_idr, shutdown_resource, client);
1489 idr_remove_all(&client->resource_idr);
1490 idr_destroy(&client->resource_idr);
1492 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1500 static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1502 struct client *client = file->private_data;
1503 unsigned int mask = 0;
1505 poll_wait(file, &client->wait, pt);
1507 if (fw_device_is_shutdown(client->device))
1508 mask |= POLLHUP | POLLERR;
1509 if (!list_empty(&client->event_list))
1510 mask |= POLLIN | POLLRDNORM;
1515 const struct file_operations fw_device_ops = {
1516 .owner = THIS_MODULE,
1517 .llseek = no_llseek,
1518 .open = fw_device_op_open,
1519 .read = fw_device_op_read,
1520 .unlocked_ioctl = fw_device_op_ioctl,
1521 .mmap = fw_device_op_mmap,
1522 .release = fw_device_op_release,
1523 .poll = fw_device_op_poll,
1524 #ifdef CONFIG_COMPAT
1525 .compat_ioctl = fw_device_op_compat_ioctl,