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-cdev.h>
26 #include <linux/idr.h>
27 #include <linux/kernel.h>
28 #include <linux/kref.h>
30 #include <linux/module.h>
31 #include <linux/mutex.h>
32 #include <linux/poll.h>
33 #include <linux/preempt.h>
34 #include <linux/spinlock.h>
35 #include <linux/time.h>
36 #include <linux/vmalloc.h>
37 #include <linux/wait.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
42 #include "fw-device.h"
43 #include "fw-topology.h"
44 #include "fw-transaction.h"
48 struct fw_device
*device
;
52 struct idr resource_idr
;
53 struct list_head event_list
;
54 wait_queue_head_t wait
;
55 u64 bus_reset_closure
;
57 struct fw_iso_context
*iso_context
;
59 struct fw_iso_buffer buffer
;
60 unsigned long vm_start
;
62 struct list_head link
;
66 static inline void client_get(struct client
*client
)
68 kref_get(&client
->kref
);
71 static void client_release(struct kref
*kref
)
73 struct client
*client
= container_of(kref
, struct client
, kref
);
75 fw_device_put(client
->device
);
79 static void client_put(struct client
*client
)
81 kref_put(&client
->kref
, client_release
);
84 struct client_resource
;
85 typedef void (*client_resource_release_fn_t
)(struct client
*,
86 struct client_resource
*);
87 struct client_resource
{
88 client_resource_release_fn_t release
;
92 struct address_handler_resource
{
93 struct client_resource resource
;
94 struct fw_address_handler handler
;
96 struct client
*client
;
99 struct outbound_transaction_resource
{
100 struct client_resource resource
;
101 struct fw_transaction transaction
;
104 struct inbound_transaction_resource
{
105 struct client_resource resource
;
106 struct fw_request
*request
;
111 struct descriptor_resource
{
112 struct client_resource resource
;
113 struct fw_descriptor descriptor
;
118 * dequeue_event() just kfree()'s the event, so the event has to be
119 * the first field in a struct XYZ_event.
122 struct { void *data
; size_t size
; } v
[2];
123 struct list_head link
;
126 struct bus_reset_event
{
128 struct fw_cdev_event_bus_reset reset
;
131 struct outbound_transaction_event
{
133 struct client
*client
;
134 struct outbound_transaction_resource r
;
135 struct fw_cdev_event_response response
;
138 struct inbound_transaction_event
{
140 struct fw_cdev_event_request request
;
143 struct iso_interrupt_event
{
145 struct fw_cdev_event_iso_interrupt interrupt
;
148 static inline void __user
*u64_to_uptr(__u64 value
)
150 return (void __user
*)(unsigned long)value
;
153 static inline __u64
uptr_to_u64(void __user
*ptr
)
155 return (__u64
)(unsigned long)ptr
;
158 static int fw_device_op_open(struct inode
*inode
, struct file
*file
)
160 struct fw_device
*device
;
161 struct client
*client
;
163 device
= fw_device_get_by_devt(inode
->i_rdev
);
167 if (fw_device_is_shutdown(device
)) {
168 fw_device_put(device
);
172 client
= kzalloc(sizeof(*client
), GFP_KERNEL
);
173 if (client
== NULL
) {
174 fw_device_put(device
);
178 client
->device
= device
;
179 spin_lock_init(&client
->lock
);
180 idr_init(&client
->resource_idr
);
181 INIT_LIST_HEAD(&client
->event_list
);
182 init_waitqueue_head(&client
->wait
);
183 kref_init(&client
->kref
);
185 file
->private_data
= client
;
187 mutex_lock(&device
->client_list_mutex
);
188 list_add_tail(&client
->link
, &device
->client_list
);
189 mutex_unlock(&device
->client_list_mutex
);
194 static void queue_event(struct client
*client
, struct event
*event
,
195 void *data0
, size_t size0
, void *data1
, size_t size1
)
199 event
->v
[0].data
= data0
;
200 event
->v
[0].size
= size0
;
201 event
->v
[1].data
= data1
;
202 event
->v
[1].size
= size1
;
204 spin_lock_irqsave(&client
->lock
, flags
);
205 if (client
->in_shutdown
)
208 list_add_tail(&event
->link
, &client
->event_list
);
209 spin_unlock_irqrestore(&client
->lock
, flags
);
211 wake_up_interruptible(&client
->wait
);
214 static int dequeue_event(struct client
*client
,
215 char __user
*buffer
, size_t count
)
222 ret
= wait_event_interruptible(client
->wait
,
223 !list_empty(&client
->event_list
) ||
224 fw_device_is_shutdown(client
->device
));
228 if (list_empty(&client
->event_list
) &&
229 fw_device_is_shutdown(client
->device
))
232 spin_lock_irqsave(&client
->lock
, flags
);
233 event
= list_first_entry(&client
->event_list
, struct event
, link
);
234 list_del(&event
->link
);
235 spin_unlock_irqrestore(&client
->lock
, flags
);
238 for (i
= 0; i
< ARRAY_SIZE(event
->v
) && total
< count
; i
++) {
239 size
= min(event
->v
[i
].size
, count
- total
);
240 if (copy_to_user(buffer
+ total
, event
->v
[i
].data
, size
)) {
254 static ssize_t
fw_device_op_read(struct file
*file
, char __user
*buffer
,
255 size_t count
, loff_t
*offset
)
257 struct client
*client
= file
->private_data
;
259 return dequeue_event(client
, buffer
, count
);
262 static void fill_bus_reset_event(struct fw_cdev_event_bus_reset
*event
,
263 struct client
*client
)
265 struct fw_card
*card
= client
->device
->card
;
268 spin_lock_irqsave(&card
->lock
, flags
);
270 event
->closure
= client
->bus_reset_closure
;
271 event
->type
= FW_CDEV_EVENT_BUS_RESET
;
272 event
->generation
= client
->device
->generation
;
273 event
->node_id
= client
->device
->node_id
;
274 event
->local_node_id
= card
->local_node
->node_id
;
275 event
->bm_node_id
= 0; /* FIXME: We don't track the BM. */
276 event
->irm_node_id
= card
->irm_node
->node_id
;
277 event
->root_node_id
= card
->root_node
->node_id
;
279 spin_unlock_irqrestore(&card
->lock
, flags
);
282 static void for_each_client(struct fw_device
*device
,
283 void (*callback
)(struct client
*client
))
287 mutex_lock(&device
->client_list_mutex
);
288 list_for_each_entry(c
, &device
->client_list
, link
)
290 mutex_unlock(&device
->client_list_mutex
);
293 static void queue_bus_reset_event(struct client
*client
)
295 struct bus_reset_event
*e
;
297 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
299 fw_notify("Out of memory when allocating bus reset event\n");
303 fill_bus_reset_event(&e
->reset
, client
);
305 queue_event(client
, &e
->event
,
306 &e
->reset
, sizeof(e
->reset
), NULL
, 0);
309 void fw_device_cdev_update(struct fw_device
*device
)
311 for_each_client(device
, queue_bus_reset_event
);
314 static void wake_up_client(struct client
*client
)
316 wake_up_interruptible(&client
->wait
);
319 void fw_device_cdev_remove(struct fw_device
*device
)
321 for_each_client(device
, wake_up_client
);
324 static int ioctl_get_info(struct client
*client
, void *buffer
)
326 struct fw_cdev_get_info
*get_info
= buffer
;
327 struct fw_cdev_event_bus_reset bus_reset
;
328 unsigned long ret
= 0;
330 client
->version
= get_info
->version
;
331 get_info
->version
= FW_CDEV_VERSION
;
332 get_info
->card
= client
->device
->card
->index
;
334 down_read(&fw_device_rwsem
);
336 if (get_info
->rom
!= 0) {
337 void __user
*uptr
= u64_to_uptr(get_info
->rom
);
338 size_t want
= get_info
->rom_length
;
339 size_t have
= client
->device
->config_rom_length
* 4;
341 ret
= copy_to_user(uptr
, client
->device
->config_rom
,
344 get_info
->rom_length
= client
->device
->config_rom_length
* 4;
346 up_read(&fw_device_rwsem
);
351 client
->bus_reset_closure
= get_info
->bus_reset_closure
;
352 if (get_info
->bus_reset
!= 0) {
353 void __user
*uptr
= u64_to_uptr(get_info
->bus_reset
);
355 fill_bus_reset_event(&bus_reset
, client
);
356 if (copy_to_user(uptr
, &bus_reset
, sizeof(bus_reset
)))
363 static int add_client_resource(struct client
*client
,
364 struct client_resource
*resource
, gfp_t gfp_mask
)
370 if (idr_pre_get(&client
->resource_idr
, gfp_mask
) == 0)
373 spin_lock_irqsave(&client
->lock
, flags
);
374 if (client
->in_shutdown
)
377 ret
= idr_get_new(&client
->resource_idr
, resource
,
381 spin_unlock_irqrestore(&client
->lock
, flags
);
386 return ret
< 0 ? ret
: 0;
389 static int release_client_resource(struct client
*client
, u32 handle
,
390 client_resource_release_fn_t release
,
391 struct client_resource
**resource
)
393 struct client_resource
*r
;
396 spin_lock_irqsave(&client
->lock
, flags
);
397 if (client
->in_shutdown
)
400 r
= idr_find(&client
->resource_idr
, handle
);
401 if (r
&& r
->release
== release
)
402 idr_remove(&client
->resource_idr
, handle
);
403 spin_unlock_irqrestore(&client
->lock
, flags
);
405 if (!(r
&& r
->release
== release
))
411 r
->release(client
, r
);
418 static void release_transaction(struct client
*client
,
419 struct client_resource
*resource
)
421 struct outbound_transaction_resource
*r
= container_of(resource
,
422 struct outbound_transaction_resource
, resource
);
424 fw_cancel_transaction(client
->device
->card
, &r
->transaction
);
427 static void complete_transaction(struct fw_card
*card
, int rcode
,
428 void *payload
, size_t length
, void *data
)
430 struct outbound_transaction_event
*e
= data
;
431 struct fw_cdev_event_response
*rsp
= &e
->response
;
432 struct client
*client
= e
->client
;
435 if (length
< rsp
->length
)
436 rsp
->length
= length
;
437 if (rcode
== RCODE_COMPLETE
)
438 memcpy(rsp
->data
, payload
, rsp
->length
);
440 spin_lock_irqsave(&client
->lock
, flags
);
442 * 1. If called while in shutdown, the idr tree must be left untouched.
443 * The idr handle will be removed and the client reference will be
445 * 2. If the call chain was release_client_resource ->
446 * release_transaction -> complete_transaction (instead of a normal
447 * conclusion of the transaction), i.e. if this resource was already
448 * unregistered from the idr, the client reference will be dropped
449 * by release_client_resource and we must not drop it here.
451 if (!client
->in_shutdown
&&
452 idr_find(&client
->resource_idr
, e
->r
.resource
.handle
)) {
453 idr_remove(&client
->resource_idr
, e
->r
.resource
.handle
);
454 /* Drop the idr's reference */
457 spin_unlock_irqrestore(&client
->lock
, flags
);
459 rsp
->type
= FW_CDEV_EVENT_RESPONSE
;
463 * In the case that sizeof(*rsp) doesn't align with the position of the
464 * data, and the read is short, preserve an extra copy of the data
465 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
466 * for short reads and some apps depended on it, this is both safe
467 * and prudent for compatibility.
469 if (rsp
->length
<= sizeof(*rsp
) - offsetof(typeof(*rsp
), data
))
470 queue_event(client
, &e
->event
, rsp
, sizeof(*rsp
),
471 rsp
->data
, rsp
->length
);
473 queue_event(client
, &e
->event
, rsp
, sizeof(*rsp
) + rsp
->length
,
476 /* Drop the transaction callback's reference */
480 static int ioctl_send_request(struct client
*client
, void *buffer
)
482 struct fw_device
*device
= client
->device
;
483 struct fw_cdev_send_request
*request
= buffer
;
484 struct outbound_transaction_event
*e
;
487 /* What is the biggest size we'll accept, really? */
488 if (request
->length
> 4096)
491 e
= kmalloc(sizeof(*e
) + request
->length
, GFP_KERNEL
);
496 e
->response
.length
= request
->length
;
497 e
->response
.closure
= request
->closure
;
500 copy_from_user(e
->response
.data
,
501 u64_to_uptr(request
->data
), request
->length
)) {
506 switch (request
->tcode
) {
507 case TCODE_WRITE_QUADLET_REQUEST
:
508 case TCODE_WRITE_BLOCK_REQUEST
:
509 case TCODE_READ_QUADLET_REQUEST
:
510 case TCODE_READ_BLOCK_REQUEST
:
511 case TCODE_LOCK_MASK_SWAP
:
512 case TCODE_LOCK_COMPARE_SWAP
:
513 case TCODE_LOCK_FETCH_ADD
:
514 case TCODE_LOCK_LITTLE_ADD
:
515 case TCODE_LOCK_BOUNDED_ADD
:
516 case TCODE_LOCK_WRAP_ADD
:
517 case TCODE_LOCK_VENDOR_DEPENDENT
:
524 e
->r
.resource
.release
= release_transaction
;
525 ret
= add_client_resource(client
, &e
->r
.resource
, GFP_KERNEL
);
529 /* Get a reference for the transaction callback */
532 fw_send_request(device
->card
, &e
->r
.transaction
,
533 request
->tcode
& 0x1f,
534 device
->node
->node_id
,
538 e
->response
.data
, request
->length
,
539 complete_transaction
, e
);
542 return sizeof(request
) + request
->length
;
544 return sizeof(request
);
551 static void release_request(struct client
*client
,
552 struct client_resource
*resource
)
554 struct inbound_transaction_resource
*r
= container_of(resource
,
555 struct inbound_transaction_resource
, resource
);
557 fw_send_response(client
->device
->card
, r
->request
,
558 RCODE_CONFLICT_ERROR
);
562 static void handle_request(struct fw_card
*card
, struct fw_request
*request
,
563 int tcode
, int destination
, int source
,
564 int generation
, int speed
,
565 unsigned long long offset
,
566 void *payload
, size_t length
, void *callback_data
)
568 struct address_handler_resource
*handler
= callback_data
;
569 struct inbound_transaction_resource
*r
;
570 struct inbound_transaction_event
*e
;
573 r
= kmalloc(sizeof(*r
), GFP_ATOMIC
);
574 e
= kmalloc(sizeof(*e
), GFP_ATOMIC
);
575 if (r
== NULL
|| e
== NULL
)
578 r
->request
= request
;
582 r
->resource
.release
= release_request
;
583 ret
= add_client_resource(handler
->client
, &r
->resource
, GFP_ATOMIC
);
587 e
->request
.type
= FW_CDEV_EVENT_REQUEST
;
588 e
->request
.tcode
= tcode
;
589 e
->request
.offset
= offset
;
590 e
->request
.length
= length
;
591 e
->request
.handle
= r
->resource
.handle
;
592 e
->request
.closure
= handler
->closure
;
594 queue_event(handler
->client
, &e
->event
,
595 &e
->request
, sizeof(e
->request
), payload
, length
);
601 fw_send_response(card
, request
, RCODE_CONFLICT_ERROR
);
604 static void release_address_handler(struct client
*client
,
605 struct client_resource
*resource
)
607 struct address_handler_resource
*r
=
608 container_of(resource
, struct address_handler_resource
, resource
);
610 fw_core_remove_address_handler(&r
->handler
);
614 static int ioctl_allocate(struct client
*client
, void *buffer
)
616 struct fw_cdev_allocate
*request
= buffer
;
617 struct address_handler_resource
*r
;
618 struct fw_address_region region
;
621 r
= kmalloc(sizeof(*r
), GFP_KERNEL
);
625 region
.start
= request
->offset
;
626 region
.end
= request
->offset
+ request
->length
;
627 r
->handler
.length
= request
->length
;
628 r
->handler
.address_callback
= handle_request
;
629 r
->handler
.callback_data
= r
;
630 r
->closure
= request
->closure
;
633 ret
= fw_core_add_address_handler(&r
->handler
, ®ion
);
639 r
->resource
.release
= release_address_handler
;
640 ret
= add_client_resource(client
, &r
->resource
, GFP_KERNEL
);
642 release_address_handler(client
, &r
->resource
);
645 request
->handle
= r
->resource
.handle
;
650 static int ioctl_deallocate(struct client
*client
, void *buffer
)
652 struct fw_cdev_deallocate
*request
= buffer
;
654 return release_client_resource(client
, request
->handle
,
655 release_address_handler
, NULL
);
658 static int ioctl_send_response(struct client
*client
, void *buffer
)
660 struct fw_cdev_send_response
*request
= buffer
;
661 struct client_resource
*resource
;
662 struct inbound_transaction_resource
*r
;
664 if (release_client_resource(client
, request
->handle
,
665 release_request
, &resource
) < 0)
668 r
= container_of(resource
, struct inbound_transaction_resource
,
670 if (request
->length
< r
->length
)
671 r
->length
= request
->length
;
672 if (copy_from_user(r
->data
, u64_to_uptr(request
->data
), r
->length
))
675 fw_send_response(client
->device
->card
, r
->request
, request
->rcode
);
681 static int ioctl_initiate_bus_reset(struct client
*client
, void *buffer
)
683 struct fw_cdev_initiate_bus_reset
*request
= buffer
;
686 short_reset
= (request
->type
== FW_CDEV_SHORT_RESET
);
688 return fw_core_initiate_bus_reset(client
->device
->card
, short_reset
);
691 static void release_descriptor(struct client
*client
,
692 struct client_resource
*resource
)
694 struct descriptor_resource
*r
=
695 container_of(resource
, struct descriptor_resource
, resource
);
697 fw_core_remove_descriptor(&r
->descriptor
);
701 static int ioctl_add_descriptor(struct client
*client
, void *buffer
)
703 struct fw_cdev_add_descriptor
*request
= buffer
;
704 struct descriptor_resource
*r
;
707 if (request
->length
> 256)
710 r
= kmalloc(sizeof(*r
) + request
->length
* 4, GFP_KERNEL
);
714 if (copy_from_user(r
->data
,
715 u64_to_uptr(request
->data
), request
->length
* 4)) {
720 r
->descriptor
.length
= request
->length
;
721 r
->descriptor
.immediate
= request
->immediate
;
722 r
->descriptor
.key
= request
->key
;
723 r
->descriptor
.data
= r
->data
;
725 ret
= fw_core_add_descriptor(&r
->descriptor
);
729 r
->resource
.release
= release_descriptor
;
730 ret
= add_client_resource(client
, &r
->resource
, GFP_KERNEL
);
732 fw_core_remove_descriptor(&r
->descriptor
);
735 request
->handle
= r
->resource
.handle
;
744 static int ioctl_remove_descriptor(struct client
*client
, void *buffer
)
746 struct fw_cdev_remove_descriptor
*request
= buffer
;
748 return release_client_resource(client
, request
->handle
,
749 release_descriptor
, NULL
);
752 static void iso_callback(struct fw_iso_context
*context
, u32 cycle
,
753 size_t header_length
, void *header
, void *data
)
755 struct client
*client
= data
;
756 struct iso_interrupt_event
*e
;
758 e
= kzalloc(sizeof(*e
) + header_length
, GFP_ATOMIC
);
762 e
->interrupt
.type
= FW_CDEV_EVENT_ISO_INTERRUPT
;
763 e
->interrupt
.closure
= client
->iso_closure
;
764 e
->interrupt
.cycle
= cycle
;
765 e
->interrupt
.header_length
= header_length
;
766 memcpy(e
->interrupt
.header
, header
, header_length
);
767 queue_event(client
, &e
->event
, &e
->interrupt
,
768 sizeof(e
->interrupt
) + header_length
, NULL
, 0);
771 static int ioctl_create_iso_context(struct client
*client
, void *buffer
)
773 struct fw_cdev_create_iso_context
*request
= buffer
;
774 struct fw_iso_context
*context
;
776 /* We only support one context at this time. */
777 if (client
->iso_context
!= NULL
)
780 if (request
->channel
> 63)
783 switch (request
->type
) {
784 case FW_ISO_CONTEXT_RECEIVE
:
785 if (request
->header_size
< 4 || (request
->header_size
& 3))
790 case FW_ISO_CONTEXT_TRANSMIT
:
791 if (request
->speed
> SCODE_3200
)
800 context
= fw_iso_context_create(client
->device
->card
,
804 request
->header_size
,
805 iso_callback
, client
);
807 return PTR_ERR(context
);
809 client
->iso_closure
= request
->closure
;
810 client
->iso_context
= context
;
812 /* We only support one context at this time. */
818 /* Macros for decoding the iso packet control header. */
819 #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
820 #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
821 #define GET_SKIP(v) (((v) >> 17) & 0x01)
822 #define GET_TAG(v) (((v) >> 18) & 0x03)
823 #define GET_SY(v) (((v) >> 20) & 0x0f)
824 #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
826 static int ioctl_queue_iso(struct client
*client
, void *buffer
)
828 struct fw_cdev_queue_iso
*request
= buffer
;
829 struct fw_cdev_iso_packet __user
*p
, *end
, *next
;
830 struct fw_iso_context
*ctx
= client
->iso_context
;
831 unsigned long payload
, buffer_end
, header_length
;
835 struct fw_iso_packet packet
;
839 if (ctx
== NULL
|| request
->handle
!= 0)
843 * If the user passes a non-NULL data pointer, has mmap()'ed
844 * the iso buffer, and the pointer points inside the buffer,
845 * we setup the payload pointers accordingly. Otherwise we
846 * set them both to 0, which will still let packets with
847 * payload_length == 0 through. In other words, if no packets
848 * use the indirect payload, the iso buffer need not be mapped
849 * and the request->data pointer is ignored.
852 payload
= (unsigned long)request
->data
- client
->vm_start
;
853 buffer_end
= client
->buffer
.page_count
<< PAGE_SHIFT
;
854 if (request
->data
== 0 || client
->buffer
.pages
== NULL
||
855 payload
>= buffer_end
) {
860 p
= (struct fw_cdev_iso_packet __user
*)u64_to_uptr(request
->packets
);
862 if (!access_ok(VERIFY_READ
, p
, request
->size
))
865 end
= (void __user
*)p
+ request
->size
;
868 if (get_user(control
, &p
->control
))
870 u
.packet
.payload_length
= GET_PAYLOAD_LENGTH(control
);
871 u
.packet
.interrupt
= GET_INTERRUPT(control
);
872 u
.packet
.skip
= GET_SKIP(control
);
873 u
.packet
.tag
= GET_TAG(control
);
874 u
.packet
.sy
= GET_SY(control
);
875 u
.packet
.header_length
= GET_HEADER_LENGTH(control
);
877 if (ctx
->type
== FW_ISO_CONTEXT_TRANSMIT
) {
878 header_length
= u
.packet
.header_length
;
881 * We require that header_length is a multiple of
882 * the fixed header size, ctx->header_size.
884 if (ctx
->header_size
== 0) {
885 if (u
.packet
.header_length
> 0)
887 } else if (u
.packet
.header_length
% ctx
->header_size
!= 0) {
893 next
= (struct fw_cdev_iso_packet __user
*)
894 &p
->header
[header_length
/ 4];
898 (u
.packet
.header
, p
->header
, header_length
))
900 if (u
.packet
.skip
&& ctx
->type
== FW_ISO_CONTEXT_TRANSMIT
&&
901 u
.packet
.header_length
+ u
.packet
.payload_length
> 0)
903 if (payload
+ u
.packet
.payload_length
> buffer_end
)
906 if (fw_iso_context_queue(ctx
, &u
.packet
,
907 &client
->buffer
, payload
))
911 payload
+= u
.packet
.payload_length
;
915 request
->size
-= uptr_to_u64(p
) - request
->packets
;
916 request
->packets
= uptr_to_u64(p
);
917 request
->data
= client
->vm_start
+ payload
;
922 static int ioctl_start_iso(struct client
*client
, void *buffer
)
924 struct fw_cdev_start_iso
*request
= buffer
;
926 if (client
->iso_context
== NULL
|| request
->handle
!= 0)
929 if (client
->iso_context
->type
== FW_ISO_CONTEXT_RECEIVE
) {
930 if (request
->tags
== 0 || request
->tags
> 15)
933 if (request
->sync
> 15)
937 return fw_iso_context_start(client
->iso_context
, request
->cycle
,
938 request
->sync
, request
->tags
);
941 static int ioctl_stop_iso(struct client
*client
, void *buffer
)
943 struct fw_cdev_stop_iso
*request
= buffer
;
945 if (client
->iso_context
== NULL
|| request
->handle
!= 0)
948 return fw_iso_context_stop(client
->iso_context
);
951 static int ioctl_get_cycle_timer(struct client
*client
, void *buffer
)
953 struct fw_cdev_get_cycle_timer
*request
= buffer
;
954 struct fw_card
*card
= client
->device
->card
;
955 unsigned long long bus_time
;
960 local_irq_save(flags
);
962 bus_time
= card
->driver
->get_bus_time(card
);
963 do_gettimeofday(&tv
);
965 local_irq_restore(flags
);
968 request
->local_time
= tv
.tv_sec
* 1000000ULL + tv
.tv_usec
;
969 request
->cycle_timer
= bus_time
& 0xffffffff;
973 static int (* const ioctl_handlers
[])(struct client
*client
, void *buffer
) = {
979 ioctl_initiate_bus_reset
,
980 ioctl_add_descriptor
,
981 ioctl_remove_descriptor
,
982 ioctl_create_iso_context
,
986 ioctl_get_cycle_timer
,
989 static int dispatch_ioctl(struct client
*client
,
990 unsigned int cmd
, void __user
*arg
)
995 if (_IOC_TYPE(cmd
) != '#' ||
996 _IOC_NR(cmd
) >= ARRAY_SIZE(ioctl_handlers
))
999 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1000 if (_IOC_SIZE(cmd
) > sizeof(buffer
) ||
1001 copy_from_user(buffer
, arg
, _IOC_SIZE(cmd
)))
1005 ret
= ioctl_handlers
[_IOC_NR(cmd
)](client
, buffer
);
1009 if (_IOC_DIR(cmd
) & _IOC_READ
) {
1010 if (_IOC_SIZE(cmd
) > sizeof(buffer
) ||
1011 copy_to_user(arg
, buffer
, _IOC_SIZE(cmd
)))
1018 static long fw_device_op_ioctl(struct file
*file
,
1019 unsigned int cmd
, unsigned long arg
)
1021 struct client
*client
= file
->private_data
;
1023 if (fw_device_is_shutdown(client
->device
))
1026 return dispatch_ioctl(client
, cmd
, (void __user
*) arg
);
1029 #ifdef CONFIG_COMPAT
1030 static long fw_device_op_compat_ioctl(struct file
*file
,
1031 unsigned int cmd
, unsigned long arg
)
1033 struct client
*client
= file
->private_data
;
1035 if (fw_device_is_shutdown(client
->device
))
1038 return dispatch_ioctl(client
, cmd
, compat_ptr(arg
));
1042 static int fw_device_op_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1044 struct client
*client
= file
->private_data
;
1045 enum dma_data_direction direction
;
1047 int page_count
, ret
;
1049 if (fw_device_is_shutdown(client
->device
))
1052 /* FIXME: We could support multiple buffers, but we don't. */
1053 if (client
->buffer
.pages
!= NULL
)
1056 if (!(vma
->vm_flags
& VM_SHARED
))
1059 if (vma
->vm_start
& ~PAGE_MASK
)
1062 client
->vm_start
= vma
->vm_start
;
1063 size
= vma
->vm_end
- vma
->vm_start
;
1064 page_count
= size
>> PAGE_SHIFT
;
1065 if (size
& ~PAGE_MASK
)
1068 if (vma
->vm_flags
& VM_WRITE
)
1069 direction
= DMA_TO_DEVICE
;
1071 direction
= DMA_FROM_DEVICE
;
1073 ret
= fw_iso_buffer_init(&client
->buffer
, client
->device
->card
,
1074 page_count
, direction
);
1078 ret
= fw_iso_buffer_map(&client
->buffer
, vma
);
1080 fw_iso_buffer_destroy(&client
->buffer
, client
->device
->card
);
1085 static int shutdown_resource(int id
, void *p
, void *data
)
1087 struct client_resource
*r
= p
;
1088 struct client
*client
= data
;
1090 r
->release(client
, r
);
1096 static int fw_device_op_release(struct inode
*inode
, struct file
*file
)
1098 struct client
*client
= file
->private_data
;
1099 struct event
*e
, *next_e
;
1100 unsigned long flags
;
1102 mutex_lock(&client
->device
->client_list_mutex
);
1103 list_del(&client
->link
);
1104 mutex_unlock(&client
->device
->client_list_mutex
);
1106 if (client
->buffer
.pages
)
1107 fw_iso_buffer_destroy(&client
->buffer
, client
->device
->card
);
1109 if (client
->iso_context
)
1110 fw_iso_context_destroy(client
->iso_context
);
1112 /* Freeze client->resource_idr and client->event_list */
1113 spin_lock_irqsave(&client
->lock
, flags
);
1114 client
->in_shutdown
= true;
1115 spin_unlock_irqrestore(&client
->lock
, flags
);
1117 idr_for_each(&client
->resource_idr
, shutdown_resource
, client
);
1118 idr_remove_all(&client
->resource_idr
);
1119 idr_destroy(&client
->resource_idr
);
1121 list_for_each_entry_safe(e
, next_e
, &client
->event_list
, link
)
1129 static unsigned int fw_device_op_poll(struct file
*file
, poll_table
* pt
)
1131 struct client
*client
= file
->private_data
;
1132 unsigned int mask
= 0;
1134 poll_wait(file
, &client
->wait
, pt
);
1136 if (fw_device_is_shutdown(client
->device
))
1137 mask
|= POLLHUP
| POLLERR
;
1138 if (!list_empty(&client
->event_list
))
1139 mask
|= POLLIN
| POLLRDNORM
;
1144 const struct file_operations fw_device_ops
= {
1145 .owner
= THIS_MODULE
,
1146 .open
= fw_device_op_open
,
1147 .read
= fw_device_op_read
,
1148 .unlocked_ioctl
= fw_device_op_ioctl
,
1149 .poll
= fw_device_op_poll
,
1150 .release
= fw_device_op_release
,
1151 .mmap
= fw_device_op_mmap
,
1153 #ifdef CONFIG_COMPAT
1154 .compat_ioctl
= fw_device_op_compat_ioctl
,