UBIFS: make debugging messages light again
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / firewire / core-cdev.c
blobe6ad3bb6c1a6b5efad00df79995f733be9a9c1f7
1 /*
2 * Char device for device raw access
4 * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <linux/bug.h>
22 #include <linux/compat.h>
23 #include <linux/delay.h>
24 #include <linux/device.h>
25 #include <linux/errno.h>
26 #include <linux/firewire.h>
27 #include <linux/firewire-cdev.h>
28 #include <linux/idr.h>
29 #include <linux/irqflags.h>
30 #include <linux/jiffies.h>
31 #include <linux/kernel.h>
32 #include <linux/kref.h>
33 #include <linux/mm.h>
34 #include <linux/module.h>
35 #include <linux/mutex.h>
36 #include <linux/poll.h>
37 #include <linux/sched.h> /* required for linux/wait.h */
38 #include <linux/slab.h>
39 #include <linux/spinlock.h>
40 #include <linux/string.h>
41 #include <linux/time.h>
42 #include <linux/uaccess.h>
43 #include <linux/vmalloc.h>
44 #include <linux/wait.h>
45 #include <linux/workqueue.h>
47 #include <asm/system.h>
49 #include "core.h"
52 * ABI version history is documented in linux/firewire-cdev.h.
54 #define FW_CDEV_KERNEL_VERSION 4
55 #define FW_CDEV_VERSION_EVENT_REQUEST2 4
56 #define FW_CDEV_VERSION_ALLOCATE_REGION_END 4
58 struct client {
59 u32 version;
60 struct fw_device *device;
62 spinlock_t lock;
63 bool in_shutdown;
64 struct idr resource_idr;
65 struct list_head event_list;
66 wait_queue_head_t wait;
67 wait_queue_head_t tx_flush_wait;
68 u64 bus_reset_closure;
70 struct fw_iso_context *iso_context;
71 u64 iso_closure;
72 struct fw_iso_buffer buffer;
73 unsigned long vm_start;
75 struct list_head phy_receiver_link;
76 u64 phy_receiver_closure;
78 struct list_head link;
79 struct kref kref;
82 static inline void client_get(struct client *client)
84 kref_get(&client->kref);
87 static void client_release(struct kref *kref)
89 struct client *client = container_of(kref, struct client, kref);
91 fw_device_put(client->device);
92 kfree(client);
95 static void client_put(struct client *client)
97 kref_put(&client->kref, client_release);
100 struct client_resource;
101 typedef void (*client_resource_release_fn_t)(struct client *,
102 struct client_resource *);
103 struct client_resource {
104 client_resource_release_fn_t release;
105 int handle;
108 struct address_handler_resource {
109 struct client_resource resource;
110 struct fw_address_handler handler;
111 __u64 closure;
112 struct client *client;
115 struct outbound_transaction_resource {
116 struct client_resource resource;
117 struct fw_transaction transaction;
120 struct inbound_transaction_resource {
121 struct client_resource resource;
122 struct fw_card *card;
123 struct fw_request *request;
124 void *data;
125 size_t length;
128 struct descriptor_resource {
129 struct client_resource resource;
130 struct fw_descriptor descriptor;
131 u32 data[0];
134 struct iso_resource {
135 struct client_resource resource;
136 struct client *client;
137 /* Schedule work and access todo only with client->lock held. */
138 struct delayed_work work;
139 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
140 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
141 int generation;
142 u64 channels;
143 s32 bandwidth;
144 struct iso_resource_event *e_alloc, *e_dealloc;
147 static void release_iso_resource(struct client *, struct client_resource *);
149 static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
151 client_get(r->client);
152 if (!queue_delayed_work(fw_workqueue, &r->work, delay))
153 client_put(r->client);
156 static void schedule_if_iso_resource(struct client_resource *resource)
158 if (resource->release == release_iso_resource)
159 schedule_iso_resource(container_of(resource,
160 struct iso_resource, resource), 0);
164 * dequeue_event() just kfree()'s the event, so the event has to be
165 * the first field in a struct XYZ_event.
167 struct event {
168 struct { void *data; size_t size; } v[2];
169 struct list_head link;
172 struct bus_reset_event {
173 struct event event;
174 struct fw_cdev_event_bus_reset reset;
177 struct outbound_transaction_event {
178 struct event event;
179 struct client *client;
180 struct outbound_transaction_resource r;
181 struct fw_cdev_event_response response;
184 struct inbound_transaction_event {
185 struct event event;
186 union {
187 struct fw_cdev_event_request request;
188 struct fw_cdev_event_request2 request2;
189 } req;
192 struct iso_interrupt_event {
193 struct event event;
194 struct fw_cdev_event_iso_interrupt interrupt;
197 struct iso_interrupt_mc_event {
198 struct event event;
199 struct fw_cdev_event_iso_interrupt_mc interrupt;
202 struct iso_resource_event {
203 struct event event;
204 struct fw_cdev_event_iso_resource iso_resource;
207 struct outbound_phy_packet_event {
208 struct event event;
209 struct client *client;
210 struct fw_packet p;
211 struct fw_cdev_event_phy_packet phy_packet;
214 struct inbound_phy_packet_event {
215 struct event event;
216 struct fw_cdev_event_phy_packet phy_packet;
219 static inline void __user *u64_to_uptr(__u64 value)
221 return (void __user *)(unsigned long)value;
224 static inline __u64 uptr_to_u64(void __user *ptr)
226 return (__u64)(unsigned long)ptr;
229 static int fw_device_op_open(struct inode *inode, struct file *file)
231 struct fw_device *device;
232 struct client *client;
234 device = fw_device_get_by_devt(inode->i_rdev);
235 if (device == NULL)
236 return -ENODEV;
238 if (fw_device_is_shutdown(device)) {
239 fw_device_put(device);
240 return -ENODEV;
243 client = kzalloc(sizeof(*client), GFP_KERNEL);
244 if (client == NULL) {
245 fw_device_put(device);
246 return -ENOMEM;
249 client->device = device;
250 spin_lock_init(&client->lock);
251 idr_init(&client->resource_idr);
252 INIT_LIST_HEAD(&client->event_list);
253 init_waitqueue_head(&client->wait);
254 init_waitqueue_head(&client->tx_flush_wait);
255 INIT_LIST_HEAD(&client->phy_receiver_link);
256 INIT_LIST_HEAD(&client->link);
257 kref_init(&client->kref);
259 file->private_data = client;
261 return nonseekable_open(inode, file);
264 static void queue_event(struct client *client, struct event *event,
265 void *data0, size_t size0, void *data1, size_t size1)
267 unsigned long flags;
269 event->v[0].data = data0;
270 event->v[0].size = size0;
271 event->v[1].data = data1;
272 event->v[1].size = size1;
274 spin_lock_irqsave(&client->lock, flags);
275 if (client->in_shutdown)
276 kfree(event);
277 else
278 list_add_tail(&event->link, &client->event_list);
279 spin_unlock_irqrestore(&client->lock, flags);
281 wake_up_interruptible(&client->wait);
284 static int dequeue_event(struct client *client,
285 char __user *buffer, size_t count)
287 struct event *event;
288 size_t size, total;
289 int i, ret;
291 ret = wait_event_interruptible(client->wait,
292 !list_empty(&client->event_list) ||
293 fw_device_is_shutdown(client->device));
294 if (ret < 0)
295 return ret;
297 if (list_empty(&client->event_list) &&
298 fw_device_is_shutdown(client->device))
299 return -ENODEV;
301 spin_lock_irq(&client->lock);
302 event = list_first_entry(&client->event_list, struct event, link);
303 list_del(&event->link);
304 spin_unlock_irq(&client->lock);
306 total = 0;
307 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
308 size = min(event->v[i].size, count - total);
309 if (copy_to_user(buffer + total, event->v[i].data, size)) {
310 ret = -EFAULT;
311 goto out;
313 total += size;
315 ret = total;
317 out:
318 kfree(event);
320 return ret;
323 static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
324 size_t count, loff_t *offset)
326 struct client *client = file->private_data;
328 return dequeue_event(client, buffer, count);
331 static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
332 struct client *client)
334 struct fw_card *card = client->device->card;
336 spin_lock_irq(&card->lock);
338 event->closure = client->bus_reset_closure;
339 event->type = FW_CDEV_EVENT_BUS_RESET;
340 event->generation = client->device->generation;
341 event->node_id = client->device->node_id;
342 event->local_node_id = card->local_node->node_id;
343 event->bm_node_id = card->bm_node_id;
344 event->irm_node_id = card->irm_node->node_id;
345 event->root_node_id = card->root_node->node_id;
347 spin_unlock_irq(&card->lock);
350 static void for_each_client(struct fw_device *device,
351 void (*callback)(struct client *client))
353 struct client *c;
355 mutex_lock(&device->client_list_mutex);
356 list_for_each_entry(c, &device->client_list, link)
357 callback(c);
358 mutex_unlock(&device->client_list_mutex);
361 static int schedule_reallocations(int id, void *p, void *data)
363 schedule_if_iso_resource(p);
365 return 0;
368 static void queue_bus_reset_event(struct client *client)
370 struct bus_reset_event *e;
372 e = kzalloc(sizeof(*e), GFP_KERNEL);
373 if (e == NULL) {
374 fw_notify("Out of memory when allocating event\n");
375 return;
378 fill_bus_reset_event(&e->reset, client);
380 queue_event(client, &e->event,
381 &e->reset, sizeof(e->reset), NULL, 0);
383 spin_lock_irq(&client->lock);
384 idr_for_each(&client->resource_idr, schedule_reallocations, client);
385 spin_unlock_irq(&client->lock);
388 void fw_device_cdev_update(struct fw_device *device)
390 for_each_client(device, queue_bus_reset_event);
393 static void wake_up_client(struct client *client)
395 wake_up_interruptible(&client->wait);
398 void fw_device_cdev_remove(struct fw_device *device)
400 for_each_client(device, wake_up_client);
403 union ioctl_arg {
404 struct fw_cdev_get_info get_info;
405 struct fw_cdev_send_request send_request;
406 struct fw_cdev_allocate allocate;
407 struct fw_cdev_deallocate deallocate;
408 struct fw_cdev_send_response send_response;
409 struct fw_cdev_initiate_bus_reset initiate_bus_reset;
410 struct fw_cdev_add_descriptor add_descriptor;
411 struct fw_cdev_remove_descriptor remove_descriptor;
412 struct fw_cdev_create_iso_context create_iso_context;
413 struct fw_cdev_queue_iso queue_iso;
414 struct fw_cdev_start_iso start_iso;
415 struct fw_cdev_stop_iso stop_iso;
416 struct fw_cdev_get_cycle_timer get_cycle_timer;
417 struct fw_cdev_allocate_iso_resource allocate_iso_resource;
418 struct fw_cdev_send_stream_packet send_stream_packet;
419 struct fw_cdev_get_cycle_timer2 get_cycle_timer2;
420 struct fw_cdev_send_phy_packet send_phy_packet;
421 struct fw_cdev_receive_phy_packets receive_phy_packets;
422 struct fw_cdev_set_iso_channels set_iso_channels;
425 static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
427 struct fw_cdev_get_info *a = &arg->get_info;
428 struct fw_cdev_event_bus_reset bus_reset;
429 unsigned long ret = 0;
431 client->version = a->version;
432 a->version = FW_CDEV_KERNEL_VERSION;
433 a->card = client->device->card->index;
435 down_read(&fw_device_rwsem);
437 if (a->rom != 0) {
438 size_t want = a->rom_length;
439 size_t have = client->device->config_rom_length * 4;
441 ret = copy_to_user(u64_to_uptr(a->rom),
442 client->device->config_rom, min(want, have));
444 a->rom_length = client->device->config_rom_length * 4;
446 up_read(&fw_device_rwsem);
448 if (ret != 0)
449 return -EFAULT;
451 mutex_lock(&client->device->client_list_mutex);
453 client->bus_reset_closure = a->bus_reset_closure;
454 if (a->bus_reset != 0) {
455 fill_bus_reset_event(&bus_reset, client);
456 ret = copy_to_user(u64_to_uptr(a->bus_reset),
457 &bus_reset, sizeof(bus_reset));
459 if (ret == 0 && list_empty(&client->link))
460 list_add_tail(&client->link, &client->device->client_list);
462 mutex_unlock(&client->device->client_list_mutex);
464 return ret ? -EFAULT : 0;
467 static int add_client_resource(struct client *client,
468 struct client_resource *resource, gfp_t gfp_mask)
470 unsigned long flags;
471 int ret;
473 retry:
474 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
475 return -ENOMEM;
477 spin_lock_irqsave(&client->lock, flags);
478 if (client->in_shutdown)
479 ret = -ECANCELED;
480 else
481 ret = idr_get_new(&client->resource_idr, resource,
482 &resource->handle);
483 if (ret >= 0) {
484 client_get(client);
485 schedule_if_iso_resource(resource);
487 spin_unlock_irqrestore(&client->lock, flags);
489 if (ret == -EAGAIN)
490 goto retry;
492 return ret < 0 ? ret : 0;
495 static int release_client_resource(struct client *client, u32 handle,
496 client_resource_release_fn_t release,
497 struct client_resource **return_resource)
499 struct client_resource *resource;
501 spin_lock_irq(&client->lock);
502 if (client->in_shutdown)
503 resource = NULL;
504 else
505 resource = idr_find(&client->resource_idr, handle);
506 if (resource && resource->release == release)
507 idr_remove(&client->resource_idr, handle);
508 spin_unlock_irq(&client->lock);
510 if (!(resource && resource->release == release))
511 return -EINVAL;
513 if (return_resource)
514 *return_resource = resource;
515 else
516 resource->release(client, resource);
518 client_put(client);
520 return 0;
523 static void release_transaction(struct client *client,
524 struct client_resource *resource)
528 static void complete_transaction(struct fw_card *card, int rcode,
529 void *payload, size_t length, void *data)
531 struct outbound_transaction_event *e = data;
532 struct fw_cdev_event_response *rsp = &e->response;
533 struct client *client = e->client;
534 unsigned long flags;
536 if (length < rsp->length)
537 rsp->length = length;
538 if (rcode == RCODE_COMPLETE)
539 memcpy(rsp->data, payload, rsp->length);
541 spin_lock_irqsave(&client->lock, flags);
542 idr_remove(&client->resource_idr, e->r.resource.handle);
543 if (client->in_shutdown)
544 wake_up(&client->tx_flush_wait);
545 spin_unlock_irqrestore(&client->lock, flags);
547 rsp->type = FW_CDEV_EVENT_RESPONSE;
548 rsp->rcode = rcode;
551 * In the case that sizeof(*rsp) doesn't align with the position of the
552 * data, and the read is short, preserve an extra copy of the data
553 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
554 * for short reads and some apps depended on it, this is both safe
555 * and prudent for compatibility.
557 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
558 queue_event(client, &e->event, rsp, sizeof(*rsp),
559 rsp->data, rsp->length);
560 else
561 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
562 NULL, 0);
564 /* Drop the idr's reference */
565 client_put(client);
568 static int init_request(struct client *client,
569 struct fw_cdev_send_request *request,
570 int destination_id, int speed)
572 struct outbound_transaction_event *e;
573 int ret;
575 if (request->tcode != TCODE_STREAM_DATA &&
576 (request->length > 4096 || request->length > 512 << speed))
577 return -EIO;
579 if (request->tcode == TCODE_WRITE_QUADLET_REQUEST &&
580 request->length < 4)
581 return -EINVAL;
583 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
584 if (e == NULL)
585 return -ENOMEM;
587 e->client = client;
588 e->response.length = request->length;
589 e->response.closure = request->closure;
591 if (request->data &&
592 copy_from_user(e->response.data,
593 u64_to_uptr(request->data), request->length)) {
594 ret = -EFAULT;
595 goto failed;
598 e->r.resource.release = release_transaction;
599 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
600 if (ret < 0)
601 goto failed;
603 fw_send_request(client->device->card, &e->r.transaction,
604 request->tcode, destination_id, request->generation,
605 speed, request->offset, e->response.data,
606 request->length, complete_transaction, e);
607 return 0;
609 failed:
610 kfree(e);
612 return ret;
615 static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
617 switch (arg->send_request.tcode) {
618 case TCODE_WRITE_QUADLET_REQUEST:
619 case TCODE_WRITE_BLOCK_REQUEST:
620 case TCODE_READ_QUADLET_REQUEST:
621 case TCODE_READ_BLOCK_REQUEST:
622 case TCODE_LOCK_MASK_SWAP:
623 case TCODE_LOCK_COMPARE_SWAP:
624 case TCODE_LOCK_FETCH_ADD:
625 case TCODE_LOCK_LITTLE_ADD:
626 case TCODE_LOCK_BOUNDED_ADD:
627 case TCODE_LOCK_WRAP_ADD:
628 case TCODE_LOCK_VENDOR_DEPENDENT:
629 break;
630 default:
631 return -EINVAL;
634 return init_request(client, &arg->send_request, client->device->node_id,
635 client->device->max_speed);
638 static inline bool is_fcp_request(struct fw_request *request)
640 return request == NULL;
643 static void release_request(struct client *client,
644 struct client_resource *resource)
646 struct inbound_transaction_resource *r = container_of(resource,
647 struct inbound_transaction_resource, resource);
649 if (is_fcp_request(r->request))
650 kfree(r->data);
651 else
652 fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
654 fw_card_put(r->card);
655 kfree(r);
658 static void handle_request(struct fw_card *card, struct fw_request *request,
659 int tcode, int destination, int source,
660 int generation, unsigned long long offset,
661 void *payload, size_t length, void *callback_data)
663 struct address_handler_resource *handler = callback_data;
664 struct inbound_transaction_resource *r;
665 struct inbound_transaction_event *e;
666 size_t event_size0;
667 void *fcp_frame = NULL;
668 int ret;
670 /* card may be different from handler->client->device->card */
671 fw_card_get(card);
673 r = kmalloc(sizeof(*r), GFP_ATOMIC);
674 e = kmalloc(sizeof(*e), GFP_ATOMIC);
675 if (r == NULL || e == NULL) {
676 fw_notify("Out of memory when allocating event\n");
677 goto failed;
679 r->card = card;
680 r->request = request;
681 r->data = payload;
682 r->length = length;
684 if (is_fcp_request(request)) {
686 * FIXME: Let core-transaction.c manage a
687 * single reference-counted copy?
689 fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
690 if (fcp_frame == NULL)
691 goto failed;
693 r->data = fcp_frame;
696 r->resource.release = release_request;
697 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
698 if (ret < 0)
699 goto failed;
701 if (handler->client->version < FW_CDEV_VERSION_EVENT_REQUEST2) {
702 struct fw_cdev_event_request *req = &e->req.request;
704 if (tcode & 0x10)
705 tcode = TCODE_LOCK_REQUEST;
707 req->type = FW_CDEV_EVENT_REQUEST;
708 req->tcode = tcode;
709 req->offset = offset;
710 req->length = length;
711 req->handle = r->resource.handle;
712 req->closure = handler->closure;
713 event_size0 = sizeof(*req);
714 } else {
715 struct fw_cdev_event_request2 *req = &e->req.request2;
717 req->type = FW_CDEV_EVENT_REQUEST2;
718 req->tcode = tcode;
719 req->offset = offset;
720 req->source_node_id = source;
721 req->destination_node_id = destination;
722 req->card = card->index;
723 req->generation = generation;
724 req->length = length;
725 req->handle = r->resource.handle;
726 req->closure = handler->closure;
727 event_size0 = sizeof(*req);
730 queue_event(handler->client, &e->event,
731 &e->req, event_size0, r->data, length);
732 return;
734 failed:
735 kfree(r);
736 kfree(e);
737 kfree(fcp_frame);
739 if (!is_fcp_request(request))
740 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
742 fw_card_put(card);
745 static void release_address_handler(struct client *client,
746 struct client_resource *resource)
748 struct address_handler_resource *r =
749 container_of(resource, struct address_handler_resource, resource);
751 fw_core_remove_address_handler(&r->handler);
752 kfree(r);
755 static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
757 struct fw_cdev_allocate *a = &arg->allocate;
758 struct address_handler_resource *r;
759 struct fw_address_region region;
760 int ret;
762 r = kmalloc(sizeof(*r), GFP_KERNEL);
763 if (r == NULL)
764 return -ENOMEM;
766 region.start = a->offset;
767 if (client->version < FW_CDEV_VERSION_ALLOCATE_REGION_END)
768 region.end = a->offset + a->length;
769 else
770 region.end = a->region_end;
772 r->handler.length = a->length;
773 r->handler.address_callback = handle_request;
774 r->handler.callback_data = r;
775 r->closure = a->closure;
776 r->client = client;
778 ret = fw_core_add_address_handler(&r->handler, &region);
779 if (ret < 0) {
780 kfree(r);
781 return ret;
783 a->offset = r->handler.offset;
785 r->resource.release = release_address_handler;
786 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
787 if (ret < 0) {
788 release_address_handler(client, &r->resource);
789 return ret;
791 a->handle = r->resource.handle;
793 return 0;
796 static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
798 return release_client_resource(client, arg->deallocate.handle,
799 release_address_handler, NULL);
802 static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
804 struct fw_cdev_send_response *a = &arg->send_response;
805 struct client_resource *resource;
806 struct inbound_transaction_resource *r;
807 int ret = 0;
809 if (release_client_resource(client, a->handle,
810 release_request, &resource) < 0)
811 return -EINVAL;
813 r = container_of(resource, struct inbound_transaction_resource,
814 resource);
815 if (is_fcp_request(r->request))
816 goto out;
818 if (a->length != fw_get_response_length(r->request)) {
819 ret = -EINVAL;
820 kfree(r->request);
821 goto out;
823 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
824 ret = -EFAULT;
825 kfree(r->request);
826 goto out;
828 fw_send_response(r->card, r->request, a->rcode);
829 out:
830 fw_card_put(r->card);
831 kfree(r);
833 return ret;
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);
840 return 0;
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);
850 kfree(r);
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;
857 int ret;
859 /* Access policy: Allow this ioctl only on local nodes' device files. */
860 if (!client->device->is_local)
861 return -ENOSYS;
863 if (a->length > 256)
864 return -EINVAL;
866 r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
867 if (r == NULL)
868 return -ENOMEM;
870 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
871 ret = -EFAULT;
872 goto failed;
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);
881 if (ret < 0)
882 goto failed;
884 r->resource.release = release_descriptor;
885 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
886 if (ret < 0) {
887 fw_core_remove_descriptor(&r->descriptor);
888 goto failed;
890 a->handle = r->resource.handle;
892 return 0;
893 failed:
894 kfree(r);
896 return ret;
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);
912 if (e == NULL) {
913 fw_notify("Out of memory when allocating event\n");
914 return;
916 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
917 e->interrupt.closure = client->iso_closure;
918 e->interrupt.cycle = cycle;
919 e->interrupt.header_length = header_length;
920 memcpy(e->interrupt.header, header, header_length);
921 queue_event(client, &e->event, &e->interrupt,
922 sizeof(e->interrupt) + header_length, NULL, 0);
925 static void iso_mc_callback(struct fw_iso_context *context,
926 dma_addr_t completed, void *data)
928 struct client *client = data;
929 struct iso_interrupt_mc_event *e;
931 e = kmalloc(sizeof(*e), GFP_ATOMIC);
932 if (e == NULL) {
933 fw_notify("Out of memory when allocating event\n");
934 return;
936 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT_MULTICHANNEL;
937 e->interrupt.closure = client->iso_closure;
938 e->interrupt.completed = fw_iso_buffer_lookup(&client->buffer,
939 completed);
940 queue_event(client, &e->event, &e->interrupt,
941 sizeof(e->interrupt), NULL, 0);
944 static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
946 struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
947 struct fw_iso_context *context;
948 fw_iso_callback_t cb;
950 BUILD_BUG_ON(FW_CDEV_ISO_CONTEXT_TRANSMIT != FW_ISO_CONTEXT_TRANSMIT ||
951 FW_CDEV_ISO_CONTEXT_RECEIVE != FW_ISO_CONTEXT_RECEIVE ||
952 FW_CDEV_ISO_CONTEXT_RECEIVE_MULTICHANNEL !=
953 FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL);
955 switch (a->type) {
956 case FW_ISO_CONTEXT_TRANSMIT:
957 if (a->speed > SCODE_3200 || a->channel > 63)
958 return -EINVAL;
960 cb = iso_callback;
961 break;
963 case FW_ISO_CONTEXT_RECEIVE:
964 if (a->header_size < 4 || (a->header_size & 3) ||
965 a->channel > 63)
966 return -EINVAL;
968 cb = iso_callback;
969 break;
971 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
972 cb = (fw_iso_callback_t)iso_mc_callback;
973 break;
975 default:
976 return -EINVAL;
979 context = fw_iso_context_create(client->device->card, a->type,
980 a->channel, a->speed, a->header_size, cb, client);
981 if (IS_ERR(context))
982 return PTR_ERR(context);
984 /* We only support one context at this time. */
985 spin_lock_irq(&client->lock);
986 if (client->iso_context != NULL) {
987 spin_unlock_irq(&client->lock);
988 fw_iso_context_destroy(context);
989 return -EBUSY;
991 client->iso_closure = a->closure;
992 client->iso_context = context;
993 spin_unlock_irq(&client->lock);
995 a->handle = 0;
997 return 0;
1000 static int ioctl_set_iso_channels(struct client *client, union ioctl_arg *arg)
1002 struct fw_cdev_set_iso_channels *a = &arg->set_iso_channels;
1003 struct fw_iso_context *ctx = client->iso_context;
1005 if (ctx == NULL || a->handle != 0)
1006 return -EINVAL;
1008 return fw_iso_context_set_channels(ctx, &a->channels);
1011 /* Macros for decoding the iso packet control header. */
1012 #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
1013 #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
1014 #define GET_SKIP(v) (((v) >> 17) & 0x01)
1015 #define GET_TAG(v) (((v) >> 18) & 0x03)
1016 #define GET_SY(v) (((v) >> 20) & 0x0f)
1017 #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
1019 static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
1021 struct fw_cdev_queue_iso *a = &arg->queue_iso;
1022 struct fw_cdev_iso_packet __user *p, *end, *next;
1023 struct fw_iso_context *ctx = client->iso_context;
1024 unsigned long payload, buffer_end, transmit_header_bytes = 0;
1025 u32 control;
1026 int count;
1027 struct {
1028 struct fw_iso_packet packet;
1029 u8 header[256];
1030 } u;
1032 if (ctx == NULL || a->handle != 0)
1033 return -EINVAL;
1036 * If the user passes a non-NULL data pointer, has mmap()'ed
1037 * the iso buffer, and the pointer points inside the buffer,
1038 * we setup the payload pointers accordingly. Otherwise we
1039 * set them both to 0, which will still let packets with
1040 * payload_length == 0 through. In other words, if no packets
1041 * use the indirect payload, the iso buffer need not be mapped
1042 * and the a->data pointer is ignored.
1044 payload = (unsigned long)a->data - client->vm_start;
1045 buffer_end = client->buffer.page_count << PAGE_SHIFT;
1046 if (a->data == 0 || client->buffer.pages == NULL ||
1047 payload >= buffer_end) {
1048 payload = 0;
1049 buffer_end = 0;
1052 if (ctx->type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL && payload & 3)
1053 return -EINVAL;
1055 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
1056 if (!access_ok(VERIFY_READ, p, a->size))
1057 return -EFAULT;
1059 end = (void __user *)p + a->size;
1060 count = 0;
1061 while (p < end) {
1062 if (get_user(control, &p->control))
1063 return -EFAULT;
1064 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
1065 u.packet.interrupt = GET_INTERRUPT(control);
1066 u.packet.skip = GET_SKIP(control);
1067 u.packet.tag = GET_TAG(control);
1068 u.packet.sy = GET_SY(control);
1069 u.packet.header_length = GET_HEADER_LENGTH(control);
1071 switch (ctx->type) {
1072 case FW_ISO_CONTEXT_TRANSMIT:
1073 if (u.packet.header_length & 3)
1074 return -EINVAL;
1075 transmit_header_bytes = u.packet.header_length;
1076 break;
1078 case FW_ISO_CONTEXT_RECEIVE:
1079 if (u.packet.header_length == 0 ||
1080 u.packet.header_length % ctx->header_size != 0)
1081 return -EINVAL;
1082 break;
1084 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
1085 if (u.packet.payload_length == 0 ||
1086 u.packet.payload_length & 3)
1087 return -EINVAL;
1088 break;
1091 next = (struct fw_cdev_iso_packet __user *)
1092 &p->header[transmit_header_bytes / 4];
1093 if (next > end)
1094 return -EINVAL;
1095 if (__copy_from_user
1096 (u.packet.header, p->header, transmit_header_bytes))
1097 return -EFAULT;
1098 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
1099 u.packet.header_length + u.packet.payload_length > 0)
1100 return -EINVAL;
1101 if (payload + u.packet.payload_length > buffer_end)
1102 return -EINVAL;
1104 if (fw_iso_context_queue(ctx, &u.packet,
1105 &client->buffer, payload))
1106 break;
1108 p = next;
1109 payload += u.packet.payload_length;
1110 count++;
1112 fw_iso_context_queue_flush(ctx);
1114 a->size -= uptr_to_u64(p) - a->packets;
1115 a->packets = uptr_to_u64(p);
1116 a->data = client->vm_start + payload;
1118 return count;
1121 static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
1123 struct fw_cdev_start_iso *a = &arg->start_iso;
1125 BUILD_BUG_ON(
1126 FW_CDEV_ISO_CONTEXT_MATCH_TAG0 != FW_ISO_CONTEXT_MATCH_TAG0 ||
1127 FW_CDEV_ISO_CONTEXT_MATCH_TAG1 != FW_ISO_CONTEXT_MATCH_TAG1 ||
1128 FW_CDEV_ISO_CONTEXT_MATCH_TAG2 != FW_ISO_CONTEXT_MATCH_TAG2 ||
1129 FW_CDEV_ISO_CONTEXT_MATCH_TAG3 != FW_ISO_CONTEXT_MATCH_TAG3 ||
1130 FW_CDEV_ISO_CONTEXT_MATCH_ALL_TAGS != FW_ISO_CONTEXT_MATCH_ALL_TAGS);
1132 if (client->iso_context == NULL || a->handle != 0)
1133 return -EINVAL;
1135 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
1136 (a->tags == 0 || a->tags > 15 || a->sync > 15))
1137 return -EINVAL;
1139 return fw_iso_context_start(client->iso_context,
1140 a->cycle, a->sync, a->tags);
1143 static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
1145 struct fw_cdev_stop_iso *a = &arg->stop_iso;
1147 if (client->iso_context == NULL || a->handle != 0)
1148 return -EINVAL;
1150 return fw_iso_context_stop(client->iso_context);
1153 static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
1155 struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
1156 struct fw_card *card = client->device->card;
1157 struct timespec ts = {0, 0};
1158 u32 cycle_time;
1159 int ret = 0;
1161 local_irq_disable();
1163 cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
1165 switch (a->clk_id) {
1166 case CLOCK_REALTIME: getnstimeofday(&ts); break;
1167 case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
1168 case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
1169 default:
1170 ret = -EINVAL;
1173 local_irq_enable();
1175 a->tv_sec = ts.tv_sec;
1176 a->tv_nsec = ts.tv_nsec;
1177 a->cycle_timer = cycle_time;
1179 return ret;
1182 static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
1184 struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
1185 struct fw_cdev_get_cycle_timer2 ct2;
1187 ct2.clk_id = CLOCK_REALTIME;
1188 ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
1190 a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
1191 a->cycle_timer = ct2.cycle_timer;
1193 return 0;
1196 static void iso_resource_work(struct work_struct *work)
1198 struct iso_resource_event *e;
1199 struct iso_resource *r =
1200 container_of(work, struct iso_resource, work.work);
1201 struct client *client = r->client;
1202 int generation, channel, bandwidth, todo;
1203 bool skip, free, success;
1205 spin_lock_irq(&client->lock);
1206 generation = client->device->generation;
1207 todo = r->todo;
1208 /* Allow 1000ms grace period for other reallocations. */
1209 if (todo == ISO_RES_ALLOC &&
1210 time_before64(get_jiffies_64(),
1211 client->device->card->reset_jiffies + HZ)) {
1212 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
1213 skip = true;
1214 } else {
1215 /* We could be called twice within the same generation. */
1216 skip = todo == ISO_RES_REALLOC &&
1217 r->generation == generation;
1219 free = todo == ISO_RES_DEALLOC ||
1220 todo == ISO_RES_ALLOC_ONCE ||
1221 todo == ISO_RES_DEALLOC_ONCE;
1222 r->generation = generation;
1223 spin_unlock_irq(&client->lock);
1225 if (skip)
1226 goto out;
1228 bandwidth = r->bandwidth;
1230 fw_iso_resource_manage(client->device->card, generation,
1231 r->channels, &channel, &bandwidth,
1232 todo == ISO_RES_ALLOC ||
1233 todo == ISO_RES_REALLOC ||
1234 todo == ISO_RES_ALLOC_ONCE);
1236 * Is this generation outdated already? As long as this resource sticks
1237 * in the idr, it will be scheduled again for a newer generation or at
1238 * shutdown.
1240 if (channel == -EAGAIN &&
1241 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1242 goto out;
1244 success = channel >= 0 || bandwidth > 0;
1246 spin_lock_irq(&client->lock);
1248 * Transit from allocation to reallocation, except if the client
1249 * requested deallocation in the meantime.
1251 if (r->todo == ISO_RES_ALLOC)
1252 r->todo = ISO_RES_REALLOC;
1254 * Allocation or reallocation failure? Pull this resource out of the
1255 * idr and prepare for deletion, unless the client is shutting down.
1257 if (r->todo == ISO_RES_REALLOC && !success &&
1258 !client->in_shutdown &&
1259 idr_find(&client->resource_idr, r->resource.handle)) {
1260 idr_remove(&client->resource_idr, r->resource.handle);
1261 client_put(client);
1262 free = true;
1264 spin_unlock_irq(&client->lock);
1266 if (todo == ISO_RES_ALLOC && channel >= 0)
1267 r->channels = 1ULL << channel;
1269 if (todo == ISO_RES_REALLOC && success)
1270 goto out;
1272 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
1273 e = r->e_alloc;
1274 r->e_alloc = NULL;
1275 } else {
1276 e = r->e_dealloc;
1277 r->e_dealloc = NULL;
1279 e->iso_resource.handle = r->resource.handle;
1280 e->iso_resource.channel = channel;
1281 e->iso_resource.bandwidth = bandwidth;
1283 queue_event(client, &e->event,
1284 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
1286 if (free) {
1287 cancel_delayed_work(&r->work);
1288 kfree(r->e_alloc);
1289 kfree(r->e_dealloc);
1290 kfree(r);
1292 out:
1293 client_put(client);
1296 static void release_iso_resource(struct client *client,
1297 struct client_resource *resource)
1299 struct iso_resource *r =
1300 container_of(resource, struct iso_resource, resource);
1302 spin_lock_irq(&client->lock);
1303 r->todo = ISO_RES_DEALLOC;
1304 schedule_iso_resource(r, 0);
1305 spin_unlock_irq(&client->lock);
1308 static int init_iso_resource(struct client *client,
1309 struct fw_cdev_allocate_iso_resource *request, int todo)
1311 struct iso_resource_event *e1, *e2;
1312 struct iso_resource *r;
1313 int ret;
1315 if ((request->channels == 0 && request->bandwidth == 0) ||
1316 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1317 request->bandwidth < 0)
1318 return -EINVAL;
1320 r = kmalloc(sizeof(*r), GFP_KERNEL);
1321 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1322 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1323 if (r == NULL || e1 == NULL || e2 == NULL) {
1324 ret = -ENOMEM;
1325 goto fail;
1328 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1329 r->client = client;
1330 r->todo = todo;
1331 r->generation = -1;
1332 r->channels = request->channels;
1333 r->bandwidth = request->bandwidth;
1334 r->e_alloc = e1;
1335 r->e_dealloc = e2;
1337 e1->iso_resource.closure = request->closure;
1338 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1339 e2->iso_resource.closure = request->closure;
1340 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
1342 if (todo == ISO_RES_ALLOC) {
1343 r->resource.release = release_iso_resource;
1344 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
1345 if (ret < 0)
1346 goto fail;
1347 } else {
1348 r->resource.release = NULL;
1349 r->resource.handle = -1;
1350 schedule_iso_resource(r, 0);
1352 request->handle = r->resource.handle;
1354 return 0;
1355 fail:
1356 kfree(r);
1357 kfree(e1);
1358 kfree(e2);
1360 return ret;
1363 static int ioctl_allocate_iso_resource(struct client *client,
1364 union ioctl_arg *arg)
1366 return init_iso_resource(client,
1367 &arg->allocate_iso_resource, ISO_RES_ALLOC);
1370 static int ioctl_deallocate_iso_resource(struct client *client,
1371 union ioctl_arg *arg)
1373 return release_client_resource(client,
1374 arg->deallocate.handle, release_iso_resource, NULL);
1377 static int ioctl_allocate_iso_resource_once(struct client *client,
1378 union ioctl_arg *arg)
1380 return init_iso_resource(client,
1381 &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
1384 static int ioctl_deallocate_iso_resource_once(struct client *client,
1385 union ioctl_arg *arg)
1387 return init_iso_resource(client,
1388 &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
1392 * Returns a speed code: Maximum speed to or from this device,
1393 * limited by the device's link speed, the local node's link speed,
1394 * and all PHY port speeds between the two links.
1396 static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
1398 return client->device->max_speed;
1401 static int ioctl_send_broadcast_request(struct client *client,
1402 union ioctl_arg *arg)
1404 struct fw_cdev_send_request *a = &arg->send_request;
1406 switch (a->tcode) {
1407 case TCODE_WRITE_QUADLET_REQUEST:
1408 case TCODE_WRITE_BLOCK_REQUEST:
1409 break;
1410 default:
1411 return -EINVAL;
1414 /* Security policy: Only allow accesses to Units Space. */
1415 if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
1416 return -EACCES;
1418 return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
1421 static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
1423 struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
1424 struct fw_cdev_send_request request;
1425 int dest;
1427 if (a->speed > client->device->card->link_speed ||
1428 a->length > 1024 << a->speed)
1429 return -EIO;
1431 if (a->tag > 3 || a->channel > 63 || a->sy > 15)
1432 return -EINVAL;
1434 dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
1435 request.tcode = TCODE_STREAM_DATA;
1436 request.length = a->length;
1437 request.closure = a->closure;
1438 request.data = a->data;
1439 request.generation = a->generation;
1441 return init_request(client, &request, dest, a->speed);
1444 static void outbound_phy_packet_callback(struct fw_packet *packet,
1445 struct fw_card *card, int status)
1447 struct outbound_phy_packet_event *e =
1448 container_of(packet, struct outbound_phy_packet_event, p);
1450 switch (status) {
1451 /* expected: */
1452 case ACK_COMPLETE: e->phy_packet.rcode = RCODE_COMPLETE; break;
1453 /* should never happen with PHY packets: */
1454 case ACK_PENDING: e->phy_packet.rcode = RCODE_COMPLETE; break;
1455 case ACK_BUSY_X:
1456 case ACK_BUSY_A:
1457 case ACK_BUSY_B: e->phy_packet.rcode = RCODE_BUSY; break;
1458 case ACK_DATA_ERROR: e->phy_packet.rcode = RCODE_DATA_ERROR; break;
1459 case ACK_TYPE_ERROR: e->phy_packet.rcode = RCODE_TYPE_ERROR; break;
1460 /* stale generation; cancelled; on certain controllers: no ack */
1461 default: e->phy_packet.rcode = status; break;
1463 e->phy_packet.data[0] = packet->timestamp;
1465 queue_event(e->client, &e->event, &e->phy_packet,
1466 sizeof(e->phy_packet) + e->phy_packet.length, NULL, 0);
1467 client_put(e->client);
1470 static int ioctl_send_phy_packet(struct client *client, union ioctl_arg *arg)
1472 struct fw_cdev_send_phy_packet *a = &arg->send_phy_packet;
1473 struct fw_card *card = client->device->card;
1474 struct outbound_phy_packet_event *e;
1476 /* Access policy: Allow this ioctl only on local nodes' device files. */
1477 if (!client->device->is_local)
1478 return -ENOSYS;
1480 e = kzalloc(sizeof(*e) + 4, GFP_KERNEL);
1481 if (e == NULL)
1482 return -ENOMEM;
1484 client_get(client);
1485 e->client = client;
1486 e->p.speed = SCODE_100;
1487 e->p.generation = a->generation;
1488 e->p.header[0] = TCODE_LINK_INTERNAL << 4;
1489 e->p.header[1] = a->data[0];
1490 e->p.header[2] = a->data[1];
1491 e->p.header_length = 12;
1492 e->p.callback = outbound_phy_packet_callback;
1493 e->phy_packet.closure = a->closure;
1494 e->phy_packet.type = FW_CDEV_EVENT_PHY_PACKET_SENT;
1495 if (is_ping_packet(a->data))
1496 e->phy_packet.length = 4;
1498 card->driver->send_request(card, &e->p);
1500 return 0;
1503 static int ioctl_receive_phy_packets(struct client *client, union ioctl_arg *arg)
1505 struct fw_cdev_receive_phy_packets *a = &arg->receive_phy_packets;
1506 struct fw_card *card = client->device->card;
1508 /* Access policy: Allow this ioctl only on local nodes' device files. */
1509 if (!client->device->is_local)
1510 return -ENOSYS;
1512 spin_lock_irq(&card->lock);
1514 list_move_tail(&client->phy_receiver_link, &card->phy_receiver_list);
1515 client->phy_receiver_closure = a->closure;
1517 spin_unlock_irq(&card->lock);
1519 return 0;
1522 void fw_cdev_handle_phy_packet(struct fw_card *card, struct fw_packet *p)
1524 struct client *client;
1525 struct inbound_phy_packet_event *e;
1526 unsigned long flags;
1528 spin_lock_irqsave(&card->lock, flags);
1530 list_for_each_entry(client, &card->phy_receiver_list, phy_receiver_link) {
1531 e = kmalloc(sizeof(*e) + 8, GFP_ATOMIC);
1532 if (e == NULL) {
1533 fw_notify("Out of memory when allocating event\n");
1534 break;
1536 e->phy_packet.closure = client->phy_receiver_closure;
1537 e->phy_packet.type = FW_CDEV_EVENT_PHY_PACKET_RECEIVED;
1538 e->phy_packet.rcode = RCODE_COMPLETE;
1539 e->phy_packet.length = 8;
1540 e->phy_packet.data[0] = p->header[1];
1541 e->phy_packet.data[1] = p->header[2];
1542 queue_event(client, &e->event,
1543 &e->phy_packet, sizeof(e->phy_packet) + 8, NULL, 0);
1546 spin_unlock_irqrestore(&card->lock, flags);
1549 static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
1550 [0x00] = ioctl_get_info,
1551 [0x01] = ioctl_send_request,
1552 [0x02] = ioctl_allocate,
1553 [0x03] = ioctl_deallocate,
1554 [0x04] = ioctl_send_response,
1555 [0x05] = ioctl_initiate_bus_reset,
1556 [0x06] = ioctl_add_descriptor,
1557 [0x07] = ioctl_remove_descriptor,
1558 [0x08] = ioctl_create_iso_context,
1559 [0x09] = ioctl_queue_iso,
1560 [0x0a] = ioctl_start_iso,
1561 [0x0b] = ioctl_stop_iso,
1562 [0x0c] = ioctl_get_cycle_timer,
1563 [0x0d] = ioctl_allocate_iso_resource,
1564 [0x0e] = ioctl_deallocate_iso_resource,
1565 [0x0f] = ioctl_allocate_iso_resource_once,
1566 [0x10] = ioctl_deallocate_iso_resource_once,
1567 [0x11] = ioctl_get_speed,
1568 [0x12] = ioctl_send_broadcast_request,
1569 [0x13] = ioctl_send_stream_packet,
1570 [0x14] = ioctl_get_cycle_timer2,
1571 [0x15] = ioctl_send_phy_packet,
1572 [0x16] = ioctl_receive_phy_packets,
1573 [0x17] = ioctl_set_iso_channels,
1576 static int dispatch_ioctl(struct client *client,
1577 unsigned int cmd, void __user *arg)
1579 union ioctl_arg buffer;
1580 int ret;
1582 if (fw_device_is_shutdown(client->device))
1583 return -ENODEV;
1585 if (_IOC_TYPE(cmd) != '#' ||
1586 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
1587 _IOC_SIZE(cmd) > sizeof(buffer))
1588 return -ENOTTY;
1590 if (_IOC_DIR(cmd) == _IOC_READ)
1591 memset(&buffer, 0, _IOC_SIZE(cmd));
1593 if (_IOC_DIR(cmd) & _IOC_WRITE)
1594 if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
1595 return -EFAULT;
1597 ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
1598 if (ret < 0)
1599 return ret;
1601 if (_IOC_DIR(cmd) & _IOC_READ)
1602 if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
1603 return -EFAULT;
1605 return ret;
1608 static long fw_device_op_ioctl(struct file *file,
1609 unsigned int cmd, unsigned long arg)
1611 return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
1614 #ifdef CONFIG_COMPAT
1615 static long fw_device_op_compat_ioctl(struct file *file,
1616 unsigned int cmd, unsigned long arg)
1618 return dispatch_ioctl(file->private_data, cmd, compat_ptr(arg));
1620 #endif
1622 static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1624 struct client *client = file->private_data;
1625 enum dma_data_direction direction;
1626 unsigned long size;
1627 int page_count, ret;
1629 if (fw_device_is_shutdown(client->device))
1630 return -ENODEV;
1632 /* FIXME: We could support multiple buffers, but we don't. */
1633 if (client->buffer.pages != NULL)
1634 return -EBUSY;
1636 if (!(vma->vm_flags & VM_SHARED))
1637 return -EINVAL;
1639 if (vma->vm_start & ~PAGE_MASK)
1640 return -EINVAL;
1642 client->vm_start = vma->vm_start;
1643 size = vma->vm_end - vma->vm_start;
1644 page_count = size >> PAGE_SHIFT;
1645 if (size & ~PAGE_MASK)
1646 return -EINVAL;
1648 if (vma->vm_flags & VM_WRITE)
1649 direction = DMA_TO_DEVICE;
1650 else
1651 direction = DMA_FROM_DEVICE;
1653 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1654 page_count, direction);
1655 if (ret < 0)
1656 return ret;
1658 ret = fw_iso_buffer_map(&client->buffer, vma);
1659 if (ret < 0)
1660 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1662 return ret;
1665 static int is_outbound_transaction_resource(int id, void *p, void *data)
1667 struct client_resource *resource = p;
1669 return resource->release == release_transaction;
1672 static int has_outbound_transactions(struct client *client)
1674 int ret;
1676 spin_lock_irq(&client->lock);
1677 ret = idr_for_each(&client->resource_idr,
1678 is_outbound_transaction_resource, NULL);
1679 spin_unlock_irq(&client->lock);
1681 return ret;
1684 static int shutdown_resource(int id, void *p, void *data)
1686 struct client_resource *resource = p;
1687 struct client *client = data;
1689 resource->release(client, resource);
1690 client_put(client);
1692 return 0;
1695 static int fw_device_op_release(struct inode *inode, struct file *file)
1697 struct client *client = file->private_data;
1698 struct event *event, *next_event;
1700 spin_lock_irq(&client->device->card->lock);
1701 list_del(&client->phy_receiver_link);
1702 spin_unlock_irq(&client->device->card->lock);
1704 mutex_lock(&client->device->client_list_mutex);
1705 list_del(&client->link);
1706 mutex_unlock(&client->device->client_list_mutex);
1708 if (client->iso_context)
1709 fw_iso_context_destroy(client->iso_context);
1711 if (client->buffer.pages)
1712 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1714 /* Freeze client->resource_idr and client->event_list */
1715 spin_lock_irq(&client->lock);
1716 client->in_shutdown = true;
1717 spin_unlock_irq(&client->lock);
1719 wait_event(client->tx_flush_wait, !has_outbound_transactions(client));
1721 idr_for_each(&client->resource_idr, shutdown_resource, client);
1722 idr_remove_all(&client->resource_idr);
1723 idr_destroy(&client->resource_idr);
1725 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1726 kfree(event);
1728 client_put(client);
1730 return 0;
1733 static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1735 struct client *client = file->private_data;
1736 unsigned int mask = 0;
1738 poll_wait(file, &client->wait, pt);
1740 if (fw_device_is_shutdown(client->device))
1741 mask |= POLLHUP | POLLERR;
1742 if (!list_empty(&client->event_list))
1743 mask |= POLLIN | POLLRDNORM;
1745 return mask;
1748 const struct file_operations fw_device_ops = {
1749 .owner = THIS_MODULE,
1750 .llseek = no_llseek,
1751 .open = fw_device_op_open,
1752 .read = fw_device_op_read,
1753 .unlocked_ioctl = fw_device_op_ioctl,
1754 .mmap = fw_device_op_mmap,
1755 .release = fw_device_op_release,
1756 .poll = fw_device_op_poll,
1757 #ifdef CONFIG_COMPAT
1758 .compat_ioctl = fw_device_op_compat_ioctl,
1759 #endif