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/poll.h>
28 #include <linux/preempt.h>
29 #include <linux/time.h>
30 #include <linux/delay.h>
32 #include <linux/idr.h>
33 #include <linux/compat.h>
34 #include <linux/firewire-cdev.h>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
37 #include "fw-transaction.h"
38 #include "fw-topology.h"
39 #include "fw-device.h"
42 struct client_resource
{
43 struct list_head link
;
44 void (*release
)(struct client
*client
, struct client_resource
*r
);
49 * dequeue_event() just kfree()'s the event, so the event has to be
50 * the first field in the struct.
54 struct { void *data
; size_t size
; } v
[2];
55 struct list_head link
;
60 struct fw_cdev_event_bus_reset reset
;
65 struct fw_transaction transaction
;
66 struct client
*client
;
67 struct client_resource resource
;
68 struct fw_cdev_event_response response
;
71 struct iso_interrupt
{
73 struct fw_cdev_event_iso_interrupt interrupt
;
78 struct fw_device
*device
;
81 struct list_head resource_list
;
82 struct list_head event_list
;
83 wait_queue_head_t wait
;
84 u64 bus_reset_closure
;
86 struct fw_iso_context
*iso_context
;
88 struct fw_iso_buffer buffer
;
89 unsigned long vm_start
;
91 struct list_head link
;
94 static inline void __user
*
95 u64_to_uptr(__u64 value
)
97 return (void __user
*)(unsigned long)value
;
101 uptr_to_u64(void __user
*ptr
)
103 return (__u64
)(unsigned long)ptr
;
106 static int fw_device_op_open(struct inode
*inode
, struct file
*file
)
108 struct fw_device
*device
;
109 struct client
*client
;
112 <<<<<<< HEAD
:drivers
/firewire
/fw
-cdev
.c
113 device
= fw_device_from_devt(inode
->i_rdev
);
115 device
= fw_device_get_by_devt(inode
->i_rdev
);
116 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/firewire
/fw
-cdev
.c
120 client
= kzalloc(sizeof(*client
), GFP_KERNEL
);
121 <<<<<<< HEAD
:drivers
/firewire
/fw
-cdev
.c
124 if (client
== NULL
) {
125 fw_device_put(device
);
126 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/firewire
/fw
-cdev
.c
128 <<<<<<< HEAD
:drivers
/firewire
/fw
-cdev
.c
131 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/firewire
/fw
-cdev
.c
133 <<<<<<< HEAD
:drivers
/firewire
/fw
-cdev
.c
134 client
->device
= fw_device_get(device
);
136 client
->device
= device
;
137 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/firewire
/fw
-cdev
.c
138 INIT_LIST_HEAD(&client
->event_list
);
139 INIT_LIST_HEAD(&client
->resource_list
);
140 spin_lock_init(&client
->lock
);
141 init_waitqueue_head(&client
->wait
);
143 file
->private_data
= client
;
145 spin_lock_irqsave(&device
->card
->lock
, flags
);
146 list_add_tail(&client
->link
, &device
->client_list
);
147 spin_unlock_irqrestore(&device
->card
->lock
, flags
);
152 static void queue_event(struct client
*client
, struct event
*event
,
153 void *data0
, size_t size0
, void *data1
, size_t size1
)
157 event
->v
[0].data
= data0
;
158 event
->v
[0].size
= size0
;
159 event
->v
[1].data
= data1
;
160 event
->v
[1].size
= size1
;
162 spin_lock_irqsave(&client
->lock
, flags
);
163 list_add_tail(&event
->link
, &client
->event_list
);
164 spin_unlock_irqrestore(&client
->lock
, flags
);
166 wake_up_interruptible(&client
->wait
);
170 dequeue_event(struct client
*client
, char __user
*buffer
, size_t count
)
177 retval
= wait_event_interruptible(client
->wait
,
178 !list_empty(&client
->event_list
) ||
179 fw_device_is_shutdown(client
->device
));
183 if (list_empty(&client
->event_list
) &&
184 fw_device_is_shutdown(client
->device
))
187 spin_lock_irqsave(&client
->lock
, flags
);
188 event
= container_of(client
->event_list
.next
, struct event
, link
);
189 list_del(&event
->link
);
190 spin_unlock_irqrestore(&client
->lock
, flags
);
193 for (i
= 0; i
< ARRAY_SIZE(event
->v
) && total
< count
; i
++) {
194 size
= min(event
->v
[i
].size
, count
- total
);
195 if (copy_to_user(buffer
+ total
, event
->v
[i
].data
, size
)) {
210 fw_device_op_read(struct file
*file
,
211 char __user
*buffer
, size_t count
, loff_t
*offset
)
213 struct client
*client
= file
->private_data
;
215 return dequeue_event(client
, buffer
, count
);
219 fill_bus_reset_event(struct fw_cdev_event_bus_reset
*event
,
220 struct client
*client
)
222 struct fw_card
*card
= client
->device
->card
;
224 event
->closure
= client
->bus_reset_closure
;
225 event
->type
= FW_CDEV_EVENT_BUS_RESET
;
226 event
->generation
= client
->device
->generation
;
227 smp_rmb(); /* node_id must not be older than generation */
228 event
->node_id
= client
->device
->node_id
;
229 event
->local_node_id
= card
->local_node
->node_id
;
230 event
->bm_node_id
= 0; /* FIXME: We don't track the BM. */
231 event
->irm_node_id
= card
->irm_node
->node_id
;
232 event
->root_node_id
= card
->root_node
->node_id
;
236 for_each_client(struct fw_device
*device
,
237 void (*callback
)(struct client
*client
))
239 struct fw_card
*card
= device
->card
;
243 spin_lock_irqsave(&card
->lock
, flags
);
245 list_for_each_entry(c
, &device
->client_list
, link
)
248 spin_unlock_irqrestore(&card
->lock
, flags
);
252 queue_bus_reset_event(struct client
*client
)
254 struct bus_reset
*bus_reset
;
256 bus_reset
= kzalloc(sizeof(*bus_reset
), GFP_ATOMIC
);
257 if (bus_reset
== NULL
) {
258 fw_notify("Out of memory when allocating bus reset event\n");
262 fill_bus_reset_event(&bus_reset
->reset
, client
);
264 queue_event(client
, &bus_reset
->event
,
265 &bus_reset
->reset
, sizeof(bus_reset
->reset
), NULL
, 0);
268 void fw_device_cdev_update(struct fw_device
*device
)
270 for_each_client(device
, queue_bus_reset_event
);
273 static void wake_up_client(struct client
*client
)
275 wake_up_interruptible(&client
->wait
);
278 void fw_device_cdev_remove(struct fw_device
*device
)
280 for_each_client(device
, wake_up_client
);
283 static int ioctl_get_info(struct client
*client
, void *buffer
)
285 struct fw_cdev_get_info
*get_info
= buffer
;
286 struct fw_cdev_event_bus_reset bus_reset
;
288 client
->version
= get_info
->version
;
289 get_info
->version
= FW_CDEV_VERSION
;
291 if (get_info
->rom
!= 0) {
292 void __user
*uptr
= u64_to_uptr(get_info
->rom
);
293 size_t want
= get_info
->rom_length
;
294 size_t have
= client
->device
->config_rom_length
* 4;
296 if (copy_to_user(uptr
, client
->device
->config_rom
,
300 get_info
->rom_length
= client
->device
->config_rom_length
* 4;
302 client
->bus_reset_closure
= get_info
->bus_reset_closure
;
303 if (get_info
->bus_reset
!= 0) {
304 void __user
*uptr
= u64_to_uptr(get_info
->bus_reset
);
306 fill_bus_reset_event(&bus_reset
, client
);
307 if (copy_to_user(uptr
, &bus_reset
, sizeof(bus_reset
)))
311 get_info
->card
= client
->device
->card
->index
;
317 add_client_resource(struct client
*client
, struct client_resource
*resource
)
321 spin_lock_irqsave(&client
->lock
, flags
);
322 list_add_tail(&resource
->link
, &client
->resource_list
);
323 resource
->handle
= client
->resource_handle
++;
324 spin_unlock_irqrestore(&client
->lock
, flags
);
328 release_client_resource(struct client
*client
, u32 handle
,
329 struct client_resource
**resource
)
331 struct client_resource
*r
;
334 spin_lock_irqsave(&client
->lock
, flags
);
335 list_for_each_entry(r
, &client
->resource_list
, link
) {
336 if (r
->handle
== handle
) {
341 spin_unlock_irqrestore(&client
->lock
, flags
);
343 if (&r
->link
== &client
->resource_list
)
349 r
->release(client
, r
);
355 release_transaction(struct client
*client
, struct client_resource
*resource
)
357 struct response
*response
=
358 container_of(resource
, struct response
, resource
);
360 fw_cancel_transaction(client
->device
->card
, &response
->transaction
);
364 complete_transaction(struct fw_card
*card
, int rcode
,
365 void *payload
, size_t length
, void *data
)
367 struct response
*response
= data
;
368 struct client
*client
= response
->client
;
371 if (length
< response
->response
.length
)
372 response
->response
.length
= length
;
373 if (rcode
== RCODE_COMPLETE
)
374 memcpy(response
->response
.data
, payload
,
375 response
->response
.length
);
377 spin_lock_irqsave(&client
->lock
, flags
);
378 list_del(&response
->resource
.link
);
379 spin_unlock_irqrestore(&client
->lock
, flags
);
381 response
->response
.type
= FW_CDEV_EVENT_RESPONSE
;
382 response
->response
.rcode
= rcode
;
383 queue_event(client
, &response
->event
,
384 &response
->response
, sizeof(response
->response
),
385 response
->response
.data
, response
->response
.length
);
388 static int ioctl_send_request(struct client
*client
, void *buffer
)
390 struct fw_device
*device
= client
->device
;
391 struct fw_cdev_send_request
*request
= buffer
;
392 struct response
*response
;
394 /* What is the biggest size we'll accept, really? */
395 if (request
->length
> 4096)
398 response
= kmalloc(sizeof(*response
) + request
->length
, GFP_KERNEL
);
399 if (response
== NULL
)
402 response
->client
= client
;
403 response
->response
.length
= request
->length
;
404 response
->response
.closure
= request
->closure
;
407 copy_from_user(response
->response
.data
,
408 u64_to_uptr(request
->data
), request
->length
)) {
413 response
->resource
.release
= release_transaction
;
414 add_client_resource(client
, &response
->resource
);
416 fw_send_request(device
->card
, &response
->transaction
,
417 request
->tcode
& 0x1f,
418 device
->node
->node_id
,
422 response
->response
.data
, request
->length
,
423 complete_transaction
, response
);
426 return sizeof(request
) + request
->length
;
428 return sizeof(request
);
431 struct address_handler
{
432 struct fw_address_handler handler
;
434 struct client
*client
;
435 struct client_resource resource
;
439 struct fw_request
*request
;
442 struct client_resource resource
;
445 struct request_event
{
447 struct fw_cdev_event_request request
;
451 release_request(struct client
*client
, struct client_resource
*resource
)
453 struct request
*request
=
454 container_of(resource
, struct request
, resource
);
456 fw_send_response(client
->device
->card
, request
->request
,
457 RCODE_CONFLICT_ERROR
);
462 handle_request(struct fw_card
*card
, struct fw_request
*r
,
463 int tcode
, int destination
, int source
,
464 int generation
, int speed
,
465 unsigned long long offset
,
466 void *payload
, size_t length
, void *callback_data
)
468 struct address_handler
*handler
= callback_data
;
469 struct request
*request
;
470 struct request_event
*e
;
471 struct client
*client
= handler
->client
;
473 request
= kmalloc(sizeof(*request
), GFP_ATOMIC
);
474 e
= kmalloc(sizeof(*e
), GFP_ATOMIC
);
475 if (request
== NULL
|| e
== NULL
) {
478 fw_send_response(card
, r
, RCODE_CONFLICT_ERROR
);
482 request
->request
= r
;
483 request
->data
= payload
;
484 request
->length
= length
;
486 request
->resource
.release
= release_request
;
487 add_client_resource(client
, &request
->resource
);
489 e
->request
.type
= FW_CDEV_EVENT_REQUEST
;
490 e
->request
.tcode
= tcode
;
491 e
->request
.offset
= offset
;
492 e
->request
.length
= length
;
493 e
->request
.handle
= request
->resource
.handle
;
494 e
->request
.closure
= handler
->closure
;
496 queue_event(client
, &e
->event
,
497 &e
->request
, sizeof(e
->request
), payload
, length
);
501 release_address_handler(struct client
*client
,
502 struct client_resource
*resource
)
504 struct address_handler
*handler
=
505 container_of(resource
, struct address_handler
, resource
);
507 fw_core_remove_address_handler(&handler
->handler
);
511 static int ioctl_allocate(struct client
*client
, void *buffer
)
513 struct fw_cdev_allocate
*request
= buffer
;
514 struct address_handler
*handler
;
515 struct fw_address_region region
;
517 handler
= kmalloc(sizeof(*handler
), GFP_KERNEL
);
521 region
.start
= request
->offset
;
522 region
.end
= request
->offset
+ request
->length
;
523 handler
->handler
.length
= request
->length
;
524 handler
->handler
.address_callback
= handle_request
;
525 handler
->handler
.callback_data
= handler
;
526 handler
->closure
= request
->closure
;
527 handler
->client
= client
;
529 if (fw_core_add_address_handler(&handler
->handler
, ®ion
) < 0) {
534 handler
->resource
.release
= release_address_handler
;
535 add_client_resource(client
, &handler
->resource
);
536 request
->handle
= handler
->resource
.handle
;
541 static int ioctl_deallocate(struct client
*client
, void *buffer
)
543 struct fw_cdev_deallocate
*request
= buffer
;
545 return release_client_resource(client
, request
->handle
, NULL
);
548 static int ioctl_send_response(struct client
*client
, void *buffer
)
550 struct fw_cdev_send_response
*request
= buffer
;
551 struct client_resource
*resource
;
554 if (release_client_resource(client
, request
->handle
, &resource
) < 0)
556 r
= container_of(resource
, struct request
, resource
);
557 if (request
->length
< r
->length
)
558 r
->length
= request
->length
;
559 if (copy_from_user(r
->data
, u64_to_uptr(request
->data
), r
->length
))
562 fw_send_response(client
->device
->card
, r
->request
, request
->rcode
);
568 static int ioctl_initiate_bus_reset(struct client
*client
, void *buffer
)
570 struct fw_cdev_initiate_bus_reset
*request
= buffer
;
573 short_reset
= (request
->type
== FW_CDEV_SHORT_RESET
);
575 return fw_core_initiate_bus_reset(client
->device
->card
, short_reset
);
579 struct fw_descriptor d
;
580 struct client_resource resource
;
584 static void release_descriptor(struct client
*client
,
585 struct client_resource
*resource
)
587 struct descriptor
*descriptor
=
588 container_of(resource
, struct descriptor
, resource
);
590 fw_core_remove_descriptor(&descriptor
->d
);
594 static int ioctl_add_descriptor(struct client
*client
, void *buffer
)
596 struct fw_cdev_add_descriptor
*request
= buffer
;
597 struct descriptor
*descriptor
;
600 if (request
->length
> 256)
604 kmalloc(sizeof(*descriptor
) + request
->length
* 4, GFP_KERNEL
);
605 if (descriptor
== NULL
)
608 if (copy_from_user(descriptor
->data
,
609 u64_to_uptr(request
->data
), request
->length
* 4)) {
614 descriptor
->d
.length
= request
->length
;
615 descriptor
->d
.immediate
= request
->immediate
;
616 descriptor
->d
.key
= request
->key
;
617 descriptor
->d
.data
= descriptor
->data
;
619 retval
= fw_core_add_descriptor(&descriptor
->d
);
625 descriptor
->resource
.release
= release_descriptor
;
626 add_client_resource(client
, &descriptor
->resource
);
627 request
->handle
= descriptor
->resource
.handle
;
632 static int ioctl_remove_descriptor(struct client
*client
, void *buffer
)
634 struct fw_cdev_remove_descriptor
*request
= buffer
;
636 return release_client_resource(client
, request
->handle
, NULL
);
640 iso_callback(struct fw_iso_context
*context
, u32 cycle
,
641 size_t header_length
, void *header
, void *data
)
643 struct client
*client
= data
;
644 struct iso_interrupt
*irq
;
646 irq
= kzalloc(sizeof(*irq
) + header_length
, GFP_ATOMIC
);
650 irq
->interrupt
.type
= FW_CDEV_EVENT_ISO_INTERRUPT
;
651 irq
->interrupt
.closure
= client
->iso_closure
;
652 irq
->interrupt
.cycle
= cycle
;
653 irq
->interrupt
.header_length
= header_length
;
654 memcpy(irq
->interrupt
.header
, header
, header_length
);
655 queue_event(client
, &irq
->event
, &irq
->interrupt
,
656 sizeof(irq
->interrupt
) + header_length
, NULL
, 0);
659 static int ioctl_create_iso_context(struct client
*client
, void *buffer
)
661 struct fw_cdev_create_iso_context
*request
= buffer
;
662 struct fw_iso_context
*context
;
664 <<<<<<< HEAD
:drivers
/firewire
/fw
-cdev
.c
666 /* We only support one context at this time. */
667 if (client
->iso_context
!= NULL
)
670 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/firewire
/fw
-cdev
.c
671 if (request
->channel
> 63)
674 switch (request
->type
) {
675 case FW_ISO_CONTEXT_RECEIVE
:
676 if (request
->header_size
< 4 || (request
->header_size
& 3))
681 case FW_ISO_CONTEXT_TRANSMIT
:
682 if (request
->speed
> SCODE_3200
)
691 context
= fw_iso_context_create(client
->device
->card
,
695 request
->header_size
,
696 iso_callback
, client
);
698 return PTR_ERR(context
);
700 client
->iso_closure
= request
->closure
;
701 client
->iso_context
= context
;
703 /* We only support one context at this time. */
709 /* Macros for decoding the iso packet control header. */
710 #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
711 #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
712 #define GET_SKIP(v) (((v) >> 17) & 0x01)
713 #define GET_TAG(v) (((v) >> 18) & 0x02)
714 #define GET_SY(v) (((v) >> 20) & 0x04)
715 #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
717 static int ioctl_queue_iso(struct client
*client
, void *buffer
)
719 struct fw_cdev_queue_iso
*request
= buffer
;
720 struct fw_cdev_iso_packet __user
*p
, *end
, *next
;
721 struct fw_iso_context
*ctx
= client
->iso_context
;
722 unsigned long payload
, buffer_end
, header_length
;
726 struct fw_iso_packet packet
;
730 if (ctx
== NULL
|| request
->handle
!= 0)
734 * If the user passes a non-NULL data pointer, has mmap()'ed
735 * the iso buffer, and the pointer points inside the buffer,
736 * we setup the payload pointers accordingly. Otherwise we
737 * set them both to 0, which will still let packets with
738 * payload_length == 0 through. In other words, if no packets
739 * use the indirect payload, the iso buffer need not be mapped
740 * and the request->data pointer is ignored.
743 payload
= (unsigned long)request
->data
- client
->vm_start
;
744 buffer_end
= client
->buffer
.page_count
<< PAGE_SHIFT
;
745 if (request
->data
== 0 || client
->buffer
.pages
== NULL
||
746 payload
>= buffer_end
) {
751 p
= (struct fw_cdev_iso_packet __user
*)u64_to_uptr(request
->packets
);
753 if (!access_ok(VERIFY_READ
, p
, request
->size
))
756 end
= (void __user
*)p
+ request
->size
;
759 if (get_user(control
, &p
->control
))
761 u
.packet
.payload_length
= GET_PAYLOAD_LENGTH(control
);
762 u
.packet
.interrupt
= GET_INTERRUPT(control
);
763 u
.packet
.skip
= GET_SKIP(control
);
764 u
.packet
.tag
= GET_TAG(control
);
765 u
.packet
.sy
= GET_SY(control
);
766 u
.packet
.header_length
= GET_HEADER_LENGTH(control
);
768 if (ctx
->type
== FW_ISO_CONTEXT_TRANSMIT
) {
769 header_length
= u
.packet
.header_length
;
772 * We require that header_length is a multiple of
773 * the fixed header size, ctx->header_size.
775 if (ctx
->header_size
== 0) {
776 if (u
.packet
.header_length
> 0)
778 } else if (u
.packet
.header_length
% ctx
->header_size
!= 0) {
784 next
= (struct fw_cdev_iso_packet __user
*)
785 &p
->header
[header_length
/ 4];
789 (u
.packet
.header
, p
->header
, header_length
))
791 if (u
.packet
.skip
&& ctx
->type
== FW_ISO_CONTEXT_TRANSMIT
&&
792 u
.packet
.header_length
+ u
.packet
.payload_length
> 0)
794 if (payload
+ u
.packet
.payload_length
> buffer_end
)
797 if (fw_iso_context_queue(ctx
, &u
.packet
,
798 &client
->buffer
, payload
))
802 payload
+= u
.packet
.payload_length
;
806 request
->size
-= uptr_to_u64(p
) - request
->packets
;
807 request
->packets
= uptr_to_u64(p
);
808 request
->data
= client
->vm_start
+ payload
;
813 static int ioctl_start_iso(struct client
*client
, void *buffer
)
815 struct fw_cdev_start_iso
*request
= buffer
;
817 <<<<<<< HEAD
:drivers
/firewire
/fw
-cdev
.c
818 if (request
->handle
!= 0)
820 if (client
->iso_context
== NULL
|| request
->handle
!= 0)
821 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/firewire
/fw
-cdev
.c
823 <<<<<<< HEAD
:drivers
/firewire
/fw
-cdev
.c
826 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/firewire
/fw
-cdev
.c
827 if (client
->iso_context
->type
== FW_ISO_CONTEXT_RECEIVE
) {
828 if (request
->tags
== 0 || request
->tags
> 15)
831 if (request
->sync
> 15)
835 return fw_iso_context_start(client
->iso_context
, request
->cycle
,
836 request
->sync
, request
->tags
);
839 static int ioctl_stop_iso(struct client
*client
, void *buffer
)
841 struct fw_cdev_stop_iso
*request
= buffer
;
843 <<<<<<< HEAD
:drivers
/firewire
/fw
-cdev
.c
844 if (request
->handle
!= 0)
846 if (client
->iso_context
== NULL
|| request
->handle
!= 0)
847 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/firewire
/fw
-cdev
.c
850 return fw_iso_context_stop(client
->iso_context
);
853 static int ioctl_get_cycle_timer(struct client
*client
, void *buffer
)
855 struct fw_cdev_get_cycle_timer
*request
= buffer
;
856 struct fw_card
*card
= client
->device
->card
;
857 unsigned long long bus_time
;
862 local_irq_save(flags
);
864 bus_time
= card
->driver
->get_bus_time(card
);
865 do_gettimeofday(&tv
);
867 local_irq_restore(flags
);
870 request
->local_time
= tv
.tv_sec
* 1000000ULL + tv
.tv_usec
;
871 request
->cycle_timer
= bus_time
& 0xffffffff;
875 static int (* const ioctl_handlers
[])(struct client
*client
, void *buffer
) = {
881 ioctl_initiate_bus_reset
,
882 ioctl_add_descriptor
,
883 ioctl_remove_descriptor
,
884 ioctl_create_iso_context
,
888 ioctl_get_cycle_timer
,
892 dispatch_ioctl(struct client
*client
, unsigned int cmd
, void __user
*arg
)
897 if (_IOC_TYPE(cmd
) != '#' ||
898 _IOC_NR(cmd
) >= ARRAY_SIZE(ioctl_handlers
))
901 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
902 if (_IOC_SIZE(cmd
) > sizeof(buffer
) ||
903 copy_from_user(buffer
, arg
, _IOC_SIZE(cmd
)))
907 retval
= ioctl_handlers
[_IOC_NR(cmd
)](client
, buffer
);
911 if (_IOC_DIR(cmd
) & _IOC_READ
) {
912 if (_IOC_SIZE(cmd
) > sizeof(buffer
) ||
913 copy_to_user(arg
, buffer
, _IOC_SIZE(cmd
)))
921 fw_device_op_ioctl(struct file
*file
,
922 unsigned int cmd
, unsigned long arg
)
924 struct client
*client
= file
->private_data
;
926 return dispatch_ioctl(client
, cmd
, (void __user
*) arg
);
931 fw_device_op_compat_ioctl(struct file
*file
,
932 unsigned int cmd
, unsigned long arg
)
934 struct client
*client
= file
->private_data
;
936 return dispatch_ioctl(client
, cmd
, compat_ptr(arg
));
940 static int fw_device_op_mmap(struct file
*file
, struct vm_area_struct
*vma
)
942 struct client
*client
= file
->private_data
;
943 enum dma_data_direction direction
;
945 int page_count
, retval
;
947 /* FIXME: We could support multiple buffers, but we don't. */
948 if (client
->buffer
.pages
!= NULL
)
951 if (!(vma
->vm_flags
& VM_SHARED
))
954 if (vma
->vm_start
& ~PAGE_MASK
)
957 client
->vm_start
= vma
->vm_start
;
958 size
= vma
->vm_end
- vma
->vm_start
;
959 page_count
= size
>> PAGE_SHIFT
;
960 if (size
& ~PAGE_MASK
)
963 if (vma
->vm_flags
& VM_WRITE
)
964 direction
= DMA_TO_DEVICE
;
966 direction
= DMA_FROM_DEVICE
;
968 retval
= fw_iso_buffer_init(&client
->buffer
, client
->device
->card
,
969 page_count
, direction
);
973 retval
= fw_iso_buffer_map(&client
->buffer
, vma
);
975 fw_iso_buffer_destroy(&client
->buffer
, client
->device
->card
);
980 static int fw_device_op_release(struct inode
*inode
, struct file
*file
)
982 struct client
*client
= file
->private_data
;
983 struct event
*e
, *next_e
;
984 struct client_resource
*r
, *next_r
;
987 if (client
->buffer
.pages
)
988 fw_iso_buffer_destroy(&client
->buffer
, client
->device
->card
);
990 if (client
->iso_context
)
991 fw_iso_context_destroy(client
->iso_context
);
993 list_for_each_entry_safe(r
, next_r
, &client
->resource_list
, link
)
994 r
->release(client
, r
);
997 * FIXME: We should wait for the async tasklets to stop
998 * running before freeing the memory.
1001 list_for_each_entry_safe(e
, next_e
, &client
->event_list
, link
)
1004 spin_lock_irqsave(&client
->device
->card
->lock
, flags
);
1005 list_del(&client
->link
);
1006 spin_unlock_irqrestore(&client
->device
->card
->lock
, flags
);
1008 fw_device_put(client
->device
);
1014 static unsigned int fw_device_op_poll(struct file
*file
, poll_table
* pt
)
1016 struct client
*client
= file
->private_data
;
1017 unsigned int mask
= 0;
1019 poll_wait(file
, &client
->wait
, pt
);
1021 if (fw_device_is_shutdown(client
->device
))
1022 mask
|= POLLHUP
| POLLERR
;
1023 if (!list_empty(&client
->event_list
))
1024 mask
|= POLLIN
| POLLRDNORM
;
1029 const struct file_operations fw_device_ops
= {
1030 .owner
= THIS_MODULE
,
1031 .open
= fw_device_op_open
,
1032 .read
= fw_device_op_read
,
1033 .unlocked_ioctl
= fw_device_op_ioctl
,
1034 .poll
= fw_device_op_poll
,
1035 .release
= fw_device_op_release
,
1036 .mmap
= fw_device_op_mmap
,
1038 #ifdef CONFIG_COMPAT
1039 .compat_ioctl
= fw_device_op_compat_ioctl
,