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/module.h>
22 #include <linux/kernel.h>
23 #include <linux/wait.h>
24 #include <linux/errno.h>
25 #include <linux/device.h>
26 #include <linux/vmalloc.h>
27 #include <linux/mutex.h>
28 #include <linux/poll.h>
29 #include <linux/preempt.h>
30 #include <linux/time.h>
31 #include <linux/spinlock.h>
32 #include <linux/delay.h>
34 #include <linux/idr.h>
35 #include <linux/compat.h>
36 #include <linux/firewire-cdev.h>
37 #include <asm/system.h>
38 #include <asm/uaccess.h>
39 #include "fw-transaction.h"
40 #include "fw-topology.h"
41 #include "fw-device.h"
44 struct client_resource
{
45 struct list_head link
;
46 void (*release
)(struct client
*client
, struct client_resource
*r
);
51 * dequeue_event() just kfree()'s the event, so the event has to be
52 * the first field in the struct.
56 struct { void *data
; size_t size
; } v
[2];
57 struct list_head link
;
62 struct fw_cdev_event_bus_reset reset
;
67 struct fw_transaction transaction
;
68 struct client
*client
;
69 struct client_resource resource
;
70 struct fw_cdev_event_response response
;
73 struct iso_interrupt
{
75 struct fw_cdev_event_iso_interrupt interrupt
;
80 struct fw_device
*device
;
83 struct list_head resource_list
;
84 struct list_head event_list
;
85 wait_queue_head_t wait
;
86 u64 bus_reset_closure
;
88 struct fw_iso_context
*iso_context
;
90 struct fw_iso_buffer buffer
;
91 unsigned long vm_start
;
93 struct list_head link
;
96 static inline void __user
*
97 u64_to_uptr(__u64 value
)
99 return (void __user
*)(unsigned long)value
;
103 uptr_to_u64(void __user
*ptr
)
105 return (__u64
)(unsigned long)ptr
;
108 static int fw_device_op_open(struct inode
*inode
, struct file
*file
)
110 struct fw_device
*device
;
111 struct client
*client
;
113 device
= fw_device_get_by_devt(inode
->i_rdev
);
117 if (fw_device_is_shutdown(device
)) {
118 fw_device_put(device
);
122 client
= kzalloc(sizeof(*client
), GFP_KERNEL
);
123 if (client
== NULL
) {
124 fw_device_put(device
);
128 client
->device
= device
;
129 INIT_LIST_HEAD(&client
->event_list
);
130 INIT_LIST_HEAD(&client
->resource_list
);
131 spin_lock_init(&client
->lock
);
132 init_waitqueue_head(&client
->wait
);
134 file
->private_data
= client
;
136 mutex_lock(&device
->client_list_mutex
);
137 list_add_tail(&client
->link
, &device
->client_list
);
138 mutex_unlock(&device
->client_list_mutex
);
143 static void queue_event(struct client
*client
, struct event
*event
,
144 void *data0
, size_t size0
, void *data1
, size_t size1
)
148 event
->v
[0].data
= data0
;
149 event
->v
[0].size
= size0
;
150 event
->v
[1].data
= data1
;
151 event
->v
[1].size
= size1
;
153 spin_lock_irqsave(&client
->lock
, flags
);
154 list_add_tail(&event
->link
, &client
->event_list
);
155 spin_unlock_irqrestore(&client
->lock
, flags
);
157 wake_up_interruptible(&client
->wait
);
161 dequeue_event(struct client
*client
, char __user
*buffer
, size_t count
)
168 retval
= wait_event_interruptible(client
->wait
,
169 !list_empty(&client
->event_list
) ||
170 fw_device_is_shutdown(client
->device
));
174 if (list_empty(&client
->event_list
) &&
175 fw_device_is_shutdown(client
->device
))
178 spin_lock_irqsave(&client
->lock
, flags
);
179 event
= container_of(client
->event_list
.next
, struct event
, link
);
180 list_del(&event
->link
);
181 spin_unlock_irqrestore(&client
->lock
, flags
);
184 for (i
= 0; i
< ARRAY_SIZE(event
->v
) && total
< count
; i
++) {
185 size
= min(event
->v
[i
].size
, count
- total
);
186 if (copy_to_user(buffer
+ total
, event
->v
[i
].data
, size
)) {
201 fw_device_op_read(struct file
*file
,
202 char __user
*buffer
, size_t count
, loff_t
*offset
)
204 struct client
*client
= file
->private_data
;
206 return dequeue_event(client
, buffer
, count
);
210 fill_bus_reset_event(struct fw_cdev_event_bus_reset
*event
,
211 struct client
*client
)
213 struct fw_card
*card
= client
->device
->card
;
216 spin_lock_irqsave(&card
->lock
, flags
);
218 event
->closure
= client
->bus_reset_closure
;
219 event
->type
= FW_CDEV_EVENT_BUS_RESET
;
220 event
->generation
= client
->device
->generation
;
221 event
->node_id
= client
->device
->node_id
;
222 event
->local_node_id
= card
->local_node
->node_id
;
223 event
->bm_node_id
= 0; /* FIXME: We don't track the BM. */
224 event
->irm_node_id
= card
->irm_node
->node_id
;
225 event
->root_node_id
= card
->root_node
->node_id
;
227 spin_unlock_irqrestore(&card
->lock
, flags
);
231 for_each_client(struct fw_device
*device
,
232 void (*callback
)(struct client
*client
))
236 mutex_lock(&device
->client_list_mutex
);
237 list_for_each_entry(c
, &device
->client_list
, link
)
239 mutex_unlock(&device
->client_list_mutex
);
243 queue_bus_reset_event(struct client
*client
)
245 struct bus_reset
*bus_reset
;
247 bus_reset
= kzalloc(sizeof(*bus_reset
), GFP_KERNEL
);
248 if (bus_reset
== NULL
) {
249 fw_notify("Out of memory when allocating bus reset event\n");
253 fill_bus_reset_event(&bus_reset
->reset
, client
);
255 queue_event(client
, &bus_reset
->event
,
256 &bus_reset
->reset
, sizeof(bus_reset
->reset
), NULL
, 0);
259 void fw_device_cdev_update(struct fw_device
*device
)
261 for_each_client(device
, queue_bus_reset_event
);
264 static void wake_up_client(struct client
*client
)
266 wake_up_interruptible(&client
->wait
);
269 void fw_device_cdev_remove(struct fw_device
*device
)
271 for_each_client(device
, wake_up_client
);
274 static int ioctl_get_info(struct client
*client
, void *buffer
)
276 struct fw_cdev_get_info
*get_info
= buffer
;
277 struct fw_cdev_event_bus_reset bus_reset
;
278 unsigned long ret
= 0;
280 client
->version
= get_info
->version
;
281 get_info
->version
= FW_CDEV_VERSION
;
282 get_info
->card
= client
->device
->card
->index
;
284 down_read(&fw_device_rwsem
);
286 if (get_info
->rom
!= 0) {
287 void __user
*uptr
= u64_to_uptr(get_info
->rom
);
288 size_t want
= get_info
->rom_length
;
289 size_t have
= client
->device
->config_rom_length
* 4;
291 ret
= copy_to_user(uptr
, client
->device
->config_rom
,
294 get_info
->rom_length
= client
->device
->config_rom_length
* 4;
296 up_read(&fw_device_rwsem
);
301 client
->bus_reset_closure
= get_info
->bus_reset_closure
;
302 if (get_info
->bus_reset
!= 0) {
303 void __user
*uptr
= u64_to_uptr(get_info
->bus_reset
);
305 fill_bus_reset_event(&bus_reset
, client
);
306 if (copy_to_user(uptr
, &bus_reset
, sizeof(bus_reset
)))
314 add_client_resource(struct client
*client
, struct client_resource
*resource
)
318 spin_lock_irqsave(&client
->lock
, flags
);
319 list_add_tail(&resource
->link
, &client
->resource_list
);
320 resource
->handle
= client
->resource_handle
++;
321 spin_unlock_irqrestore(&client
->lock
, flags
);
325 release_client_resource(struct client
*client
, u32 handle
,
326 struct client_resource
**resource
)
328 struct client_resource
*r
;
331 spin_lock_irqsave(&client
->lock
, flags
);
332 list_for_each_entry(r
, &client
->resource_list
, link
) {
333 if (r
->handle
== handle
) {
338 spin_unlock_irqrestore(&client
->lock
, flags
);
340 if (&r
->link
== &client
->resource_list
)
346 r
->release(client
, r
);
352 release_transaction(struct client
*client
, struct client_resource
*resource
)
354 struct response
*response
=
355 container_of(resource
, struct response
, resource
);
357 fw_cancel_transaction(client
->device
->card
, &response
->transaction
);
361 complete_transaction(struct fw_card
*card
, int rcode
,
362 void *payload
, size_t length
, void *data
)
364 struct response
*response
= data
;
365 struct client
*client
= response
->client
;
367 struct fw_cdev_event_response
*r
= &response
->response
;
369 if (length
< r
->length
)
371 if (rcode
== RCODE_COMPLETE
)
372 memcpy(r
->data
, payload
, r
->length
);
374 spin_lock_irqsave(&client
->lock
, flags
);
375 list_del(&response
->resource
.link
);
376 spin_unlock_irqrestore(&client
->lock
, flags
);
378 r
->type
= FW_CDEV_EVENT_RESPONSE
;
382 * In the case that sizeof(*r) doesn't align with the position of the
383 * data, and the read is short, preserve an extra copy of the data
384 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
385 * for short reads and some apps depended on it, this is both safe
386 * and prudent for compatibility.
388 if (r
->length
<= sizeof(*r
) - offsetof(typeof(*r
), data
))
389 queue_event(client
, &response
->event
, r
, sizeof(*r
),
392 queue_event(client
, &response
->event
, r
, sizeof(*r
) + r
->length
,
396 static int ioctl_send_request(struct client
*client
, void *buffer
)
398 struct fw_device
*device
= client
->device
;
399 struct fw_cdev_send_request
*request
= buffer
;
400 struct response
*response
;
403 /* What is the biggest size we'll accept, really? */
404 if (request
->length
> 4096)
407 response
= kmalloc(sizeof(*response
) + request
->length
, GFP_KERNEL
);
408 if (response
== NULL
)
411 response
->client
= client
;
412 response
->response
.length
= request
->length
;
413 response
->response
.closure
= request
->closure
;
416 copy_from_user(response
->response
.data
,
417 u64_to_uptr(request
->data
), request
->length
)) {
422 switch (request
->tcode
) {
423 case TCODE_WRITE_QUADLET_REQUEST
:
424 case TCODE_WRITE_BLOCK_REQUEST
:
425 case TCODE_READ_QUADLET_REQUEST
:
426 case TCODE_READ_BLOCK_REQUEST
:
427 case TCODE_LOCK_MASK_SWAP
:
428 case TCODE_LOCK_COMPARE_SWAP
:
429 case TCODE_LOCK_FETCH_ADD
:
430 case TCODE_LOCK_LITTLE_ADD
:
431 case TCODE_LOCK_BOUNDED_ADD
:
432 case TCODE_LOCK_WRAP_ADD
:
433 case TCODE_LOCK_VENDOR_DEPENDENT
:
440 response
->resource
.release
= release_transaction
;
441 add_client_resource(client
, &response
->resource
);
443 fw_send_request(device
->card
, &response
->transaction
,
444 request
->tcode
& 0x1f,
445 device
->node
->node_id
,
449 response
->response
.data
, request
->length
,
450 complete_transaction
, response
);
453 return sizeof(request
) + request
->length
;
455 return sizeof(request
);
462 struct address_handler
{
463 struct fw_address_handler handler
;
465 struct client
*client
;
466 struct client_resource resource
;
470 struct fw_request
*request
;
473 struct client_resource resource
;
476 struct request_event
{
478 struct fw_cdev_event_request request
;
482 release_request(struct client
*client
, struct client_resource
*resource
)
484 struct request
*request
=
485 container_of(resource
, struct request
, resource
);
487 fw_send_response(client
->device
->card
, request
->request
,
488 RCODE_CONFLICT_ERROR
);
493 handle_request(struct fw_card
*card
, struct fw_request
*r
,
494 int tcode
, int destination
, int source
,
495 int generation
, int speed
,
496 unsigned long long offset
,
497 void *payload
, size_t length
, void *callback_data
)
499 struct address_handler
*handler
= callback_data
;
500 struct request
*request
;
501 struct request_event
*e
;
502 struct client
*client
= handler
->client
;
504 request
= kmalloc(sizeof(*request
), GFP_ATOMIC
);
505 e
= kmalloc(sizeof(*e
), GFP_ATOMIC
);
506 if (request
== NULL
|| e
== NULL
) {
509 fw_send_response(card
, r
, RCODE_CONFLICT_ERROR
);
513 request
->request
= r
;
514 request
->data
= payload
;
515 request
->length
= length
;
517 request
->resource
.release
= release_request
;
518 add_client_resource(client
, &request
->resource
);
520 e
->request
.type
= FW_CDEV_EVENT_REQUEST
;
521 e
->request
.tcode
= tcode
;
522 e
->request
.offset
= offset
;
523 e
->request
.length
= length
;
524 e
->request
.handle
= request
->resource
.handle
;
525 e
->request
.closure
= handler
->closure
;
527 queue_event(client
, &e
->event
,
528 &e
->request
, sizeof(e
->request
), payload
, length
);
532 release_address_handler(struct client
*client
,
533 struct client_resource
*resource
)
535 struct address_handler
*handler
=
536 container_of(resource
, struct address_handler
, resource
);
538 fw_core_remove_address_handler(&handler
->handler
);
542 static int ioctl_allocate(struct client
*client
, void *buffer
)
544 struct fw_cdev_allocate
*request
= buffer
;
545 struct address_handler
*handler
;
546 struct fw_address_region region
;
548 handler
= kmalloc(sizeof(*handler
), GFP_KERNEL
);
552 region
.start
= request
->offset
;
553 region
.end
= request
->offset
+ request
->length
;
554 handler
->handler
.length
= request
->length
;
555 handler
->handler
.address_callback
= handle_request
;
556 handler
->handler
.callback_data
= handler
;
557 handler
->closure
= request
->closure
;
558 handler
->client
= client
;
560 if (fw_core_add_address_handler(&handler
->handler
, ®ion
) < 0) {
565 handler
->resource
.release
= release_address_handler
;
566 add_client_resource(client
, &handler
->resource
);
567 request
->handle
= handler
->resource
.handle
;
572 static int ioctl_deallocate(struct client
*client
, void *buffer
)
574 struct fw_cdev_deallocate
*request
= buffer
;
576 return release_client_resource(client
, request
->handle
, NULL
);
579 static int ioctl_send_response(struct client
*client
, void *buffer
)
581 struct fw_cdev_send_response
*request
= buffer
;
582 struct client_resource
*resource
;
585 if (release_client_resource(client
, request
->handle
, &resource
) < 0)
587 r
= container_of(resource
, struct request
, resource
);
588 if (request
->length
< r
->length
)
589 r
->length
= request
->length
;
590 if (copy_from_user(r
->data
, u64_to_uptr(request
->data
), r
->length
))
593 fw_send_response(client
->device
->card
, r
->request
, request
->rcode
);
599 static int ioctl_initiate_bus_reset(struct client
*client
, void *buffer
)
601 struct fw_cdev_initiate_bus_reset
*request
= buffer
;
604 short_reset
= (request
->type
== FW_CDEV_SHORT_RESET
);
606 return fw_core_initiate_bus_reset(client
->device
->card
, short_reset
);
610 struct fw_descriptor d
;
611 struct client_resource resource
;
615 static void release_descriptor(struct client
*client
,
616 struct client_resource
*resource
)
618 struct descriptor
*descriptor
=
619 container_of(resource
, struct descriptor
, resource
);
621 fw_core_remove_descriptor(&descriptor
->d
);
625 static int ioctl_add_descriptor(struct client
*client
, void *buffer
)
627 struct fw_cdev_add_descriptor
*request
= buffer
;
628 struct descriptor
*descriptor
;
631 if (request
->length
> 256)
635 kmalloc(sizeof(*descriptor
) + request
->length
* 4, GFP_KERNEL
);
636 if (descriptor
== NULL
)
639 if (copy_from_user(descriptor
->data
,
640 u64_to_uptr(request
->data
), request
->length
* 4)) {
645 descriptor
->d
.length
= request
->length
;
646 descriptor
->d
.immediate
= request
->immediate
;
647 descriptor
->d
.key
= request
->key
;
648 descriptor
->d
.data
= descriptor
->data
;
650 retval
= fw_core_add_descriptor(&descriptor
->d
);
656 descriptor
->resource
.release
= release_descriptor
;
657 add_client_resource(client
, &descriptor
->resource
);
658 request
->handle
= descriptor
->resource
.handle
;
663 static int ioctl_remove_descriptor(struct client
*client
, void *buffer
)
665 struct fw_cdev_remove_descriptor
*request
= buffer
;
667 return release_client_resource(client
, request
->handle
, NULL
);
671 iso_callback(struct fw_iso_context
*context
, u32 cycle
,
672 size_t header_length
, void *header
, void *data
)
674 struct client
*client
= data
;
675 struct iso_interrupt
*irq
;
677 irq
= kzalloc(sizeof(*irq
) + header_length
, GFP_ATOMIC
);
681 irq
->interrupt
.type
= FW_CDEV_EVENT_ISO_INTERRUPT
;
682 irq
->interrupt
.closure
= client
->iso_closure
;
683 irq
->interrupt
.cycle
= cycle
;
684 irq
->interrupt
.header_length
= header_length
;
685 memcpy(irq
->interrupt
.header
, header
, header_length
);
686 queue_event(client
, &irq
->event
, &irq
->interrupt
,
687 sizeof(irq
->interrupt
) + header_length
, NULL
, 0);
690 static int ioctl_create_iso_context(struct client
*client
, void *buffer
)
692 struct fw_cdev_create_iso_context
*request
= buffer
;
693 struct fw_iso_context
*context
;
695 /* We only support one context at this time. */
696 if (client
->iso_context
!= NULL
)
699 if (request
->channel
> 63)
702 switch (request
->type
) {
703 case FW_ISO_CONTEXT_RECEIVE
:
704 if (request
->header_size
< 4 || (request
->header_size
& 3))
709 case FW_ISO_CONTEXT_TRANSMIT
:
710 if (request
->speed
> SCODE_3200
)
719 context
= fw_iso_context_create(client
->device
->card
,
723 request
->header_size
,
724 iso_callback
, client
);
726 return PTR_ERR(context
);
728 client
->iso_closure
= request
->closure
;
729 client
->iso_context
= context
;
731 /* We only support one context at this time. */
737 /* Macros for decoding the iso packet control header. */
738 #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
739 #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
740 #define GET_SKIP(v) (((v) >> 17) & 0x01)
741 #define GET_TAG(v) (((v) >> 18) & 0x03)
742 #define GET_SY(v) (((v) >> 20) & 0x0f)
743 #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
745 static int ioctl_queue_iso(struct client
*client
, void *buffer
)
747 struct fw_cdev_queue_iso
*request
= buffer
;
748 struct fw_cdev_iso_packet __user
*p
, *end
, *next
;
749 struct fw_iso_context
*ctx
= client
->iso_context
;
750 unsigned long payload
, buffer_end
, header_length
;
754 struct fw_iso_packet packet
;
758 if (ctx
== NULL
|| request
->handle
!= 0)
762 * If the user passes a non-NULL data pointer, has mmap()'ed
763 * the iso buffer, and the pointer points inside the buffer,
764 * we setup the payload pointers accordingly. Otherwise we
765 * set them both to 0, which will still let packets with
766 * payload_length == 0 through. In other words, if no packets
767 * use the indirect payload, the iso buffer need not be mapped
768 * and the request->data pointer is ignored.
771 payload
= (unsigned long)request
->data
- client
->vm_start
;
772 buffer_end
= client
->buffer
.page_count
<< PAGE_SHIFT
;
773 if (request
->data
== 0 || client
->buffer
.pages
== NULL
||
774 payload
>= buffer_end
) {
779 p
= (struct fw_cdev_iso_packet __user
*)u64_to_uptr(request
->packets
);
781 if (!access_ok(VERIFY_READ
, p
, request
->size
))
784 end
= (void __user
*)p
+ request
->size
;
787 if (get_user(control
, &p
->control
))
789 u
.packet
.payload_length
= GET_PAYLOAD_LENGTH(control
);
790 u
.packet
.interrupt
= GET_INTERRUPT(control
);
791 u
.packet
.skip
= GET_SKIP(control
);
792 u
.packet
.tag
= GET_TAG(control
);
793 u
.packet
.sy
= GET_SY(control
);
794 u
.packet
.header_length
= GET_HEADER_LENGTH(control
);
796 if (ctx
->type
== FW_ISO_CONTEXT_TRANSMIT
) {
797 header_length
= u
.packet
.header_length
;
800 * We require that header_length is a multiple of
801 * the fixed header size, ctx->header_size.
803 if (ctx
->header_size
== 0) {
804 if (u
.packet
.header_length
> 0)
806 } else if (u
.packet
.header_length
% ctx
->header_size
!= 0) {
812 next
= (struct fw_cdev_iso_packet __user
*)
813 &p
->header
[header_length
/ 4];
817 (u
.packet
.header
, p
->header
, header_length
))
819 if (u
.packet
.skip
&& ctx
->type
== FW_ISO_CONTEXT_TRANSMIT
&&
820 u
.packet
.header_length
+ u
.packet
.payload_length
> 0)
822 if (payload
+ u
.packet
.payload_length
> buffer_end
)
825 if (fw_iso_context_queue(ctx
, &u
.packet
,
826 &client
->buffer
, payload
))
830 payload
+= u
.packet
.payload_length
;
834 request
->size
-= uptr_to_u64(p
) - request
->packets
;
835 request
->packets
= uptr_to_u64(p
);
836 request
->data
= client
->vm_start
+ payload
;
841 static int ioctl_start_iso(struct client
*client
, void *buffer
)
843 struct fw_cdev_start_iso
*request
= buffer
;
845 if (client
->iso_context
== NULL
|| request
->handle
!= 0)
848 if (client
->iso_context
->type
== FW_ISO_CONTEXT_RECEIVE
) {
849 if (request
->tags
== 0 || request
->tags
> 15)
852 if (request
->sync
> 15)
856 return fw_iso_context_start(client
->iso_context
, request
->cycle
,
857 request
->sync
, request
->tags
);
860 static int ioctl_stop_iso(struct client
*client
, void *buffer
)
862 struct fw_cdev_stop_iso
*request
= buffer
;
864 if (client
->iso_context
== NULL
|| request
->handle
!= 0)
867 return fw_iso_context_stop(client
->iso_context
);
870 static int ioctl_get_cycle_timer(struct client
*client
, void *buffer
)
872 struct fw_cdev_get_cycle_timer
*request
= buffer
;
873 struct fw_card
*card
= client
->device
->card
;
874 unsigned long long bus_time
;
879 local_irq_save(flags
);
881 bus_time
= card
->driver
->get_bus_time(card
);
882 do_gettimeofday(&tv
);
884 local_irq_restore(flags
);
887 request
->local_time
= tv
.tv_sec
* 1000000ULL + tv
.tv_usec
;
888 request
->cycle_timer
= bus_time
& 0xffffffff;
892 static int (* const ioctl_handlers
[])(struct client
*client
, void *buffer
) = {
898 ioctl_initiate_bus_reset
,
899 ioctl_add_descriptor
,
900 ioctl_remove_descriptor
,
901 ioctl_create_iso_context
,
905 ioctl_get_cycle_timer
,
909 dispatch_ioctl(struct client
*client
, unsigned int cmd
, void __user
*arg
)
914 if (_IOC_TYPE(cmd
) != '#' ||
915 _IOC_NR(cmd
) >= ARRAY_SIZE(ioctl_handlers
))
918 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
919 if (_IOC_SIZE(cmd
) > sizeof(buffer
) ||
920 copy_from_user(buffer
, arg
, _IOC_SIZE(cmd
)))
924 retval
= ioctl_handlers
[_IOC_NR(cmd
)](client
, buffer
);
928 if (_IOC_DIR(cmd
) & _IOC_READ
) {
929 if (_IOC_SIZE(cmd
) > sizeof(buffer
) ||
930 copy_to_user(arg
, buffer
, _IOC_SIZE(cmd
)))
938 fw_device_op_ioctl(struct file
*file
,
939 unsigned int cmd
, unsigned long arg
)
941 struct client
*client
= file
->private_data
;
943 if (fw_device_is_shutdown(client
->device
))
946 return dispatch_ioctl(client
, cmd
, (void __user
*) arg
);
951 fw_device_op_compat_ioctl(struct file
*file
,
952 unsigned int cmd
, unsigned long arg
)
954 struct client
*client
= file
->private_data
;
956 if (fw_device_is_shutdown(client
->device
))
959 return dispatch_ioctl(client
, cmd
, compat_ptr(arg
));
963 static int fw_device_op_mmap(struct file
*file
, struct vm_area_struct
*vma
)
965 struct client
*client
= file
->private_data
;
966 enum dma_data_direction direction
;
968 int page_count
, retval
;
970 if (fw_device_is_shutdown(client
->device
))
973 /* FIXME: We could support multiple buffers, but we don't. */
974 if (client
->buffer
.pages
!= NULL
)
977 if (!(vma
->vm_flags
& VM_SHARED
))
980 if (vma
->vm_start
& ~PAGE_MASK
)
983 client
->vm_start
= vma
->vm_start
;
984 size
= vma
->vm_end
- vma
->vm_start
;
985 page_count
= size
>> PAGE_SHIFT
;
986 if (size
& ~PAGE_MASK
)
989 if (vma
->vm_flags
& VM_WRITE
)
990 direction
= DMA_TO_DEVICE
;
992 direction
= DMA_FROM_DEVICE
;
994 retval
= fw_iso_buffer_init(&client
->buffer
, client
->device
->card
,
995 page_count
, direction
);
999 retval
= fw_iso_buffer_map(&client
->buffer
, vma
);
1001 fw_iso_buffer_destroy(&client
->buffer
, client
->device
->card
);
1006 static int fw_device_op_release(struct inode
*inode
, struct file
*file
)
1008 struct client
*client
= file
->private_data
;
1009 struct event
*e
, *next_e
;
1010 struct client_resource
*r
, *next_r
;
1012 if (client
->buffer
.pages
)
1013 fw_iso_buffer_destroy(&client
->buffer
, client
->device
->card
);
1015 if (client
->iso_context
)
1016 fw_iso_context_destroy(client
->iso_context
);
1018 list_for_each_entry_safe(r
, next_r
, &client
->resource_list
, link
)
1019 r
->release(client
, r
);
1022 * FIXME: We should wait for the async tasklets to stop
1023 * running before freeing the memory.
1026 list_for_each_entry_safe(e
, next_e
, &client
->event_list
, link
)
1029 mutex_lock(&client
->device
->client_list_mutex
);
1030 list_del(&client
->link
);
1031 mutex_unlock(&client
->device
->client_list_mutex
);
1033 fw_device_put(client
->device
);
1039 static unsigned int fw_device_op_poll(struct file
*file
, poll_table
* pt
)
1041 struct client
*client
= file
->private_data
;
1042 unsigned int mask
= 0;
1044 poll_wait(file
, &client
->wait
, pt
);
1046 if (fw_device_is_shutdown(client
->device
))
1047 mask
|= POLLHUP
| POLLERR
;
1048 if (!list_empty(&client
->event_list
))
1049 mask
|= POLLIN
| POLLRDNORM
;
1054 const struct file_operations fw_device_ops
= {
1055 .owner
= THIS_MODULE
,
1056 .open
= fw_device_op_open
,
1057 .read
= fw_device_op_read
,
1058 .unlocked_ioctl
= fw_device_op_ioctl
,
1059 .poll
= fw_device_op_poll
,
1060 .release
= fw_device_op_release
,
1061 .mmap
= fw_device_op_mmap
,
1063 #ifdef CONFIG_COMPAT
1064 .compat_ioctl
= fw_device_op_compat_ioctl
,