1 /* -*- c-basic-offset: 8 -*-
3 * fw-device-cdev.c - Char device for device raw access
5 * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/wait.h>
25 #include <linux/errno.h>
26 #include <linux/device.h>
27 #include <linux/vmalloc.h>
28 #include <linux/poll.h>
29 #include <linux/delay.h>
31 #include <linux/idr.h>
32 #include <linux/compat.h>
33 #include <asm/uaccess.h>
34 #include "fw-transaction.h"
35 #include "fw-topology.h"
36 #include "fw-device.h"
37 #include "fw-device-cdev.h"
42 * - bus resets sends a new packet with new generation and node id
46 /* dequeue_event() just kfree()'s the event, so the event has to be
47 * the first field in the struct. */
50 struct { void *data
; size_t size
; } v
[2];
51 struct list_head link
;
56 struct fw_cdev_event_bus_reset reset
;
61 struct fw_transaction transaction
;
62 struct client
*client
;
63 struct list_head link
;
64 struct fw_cdev_event_response response
;
67 struct iso_interrupt
{
69 struct fw_cdev_event_iso_interrupt interrupt
;
74 struct fw_device
*device
;
76 struct list_head handler_list
;
77 struct list_head request_list
;
78 struct list_head transaction_list
;
80 struct list_head event_list
;
81 wait_queue_head_t wait
;
83 struct fw_iso_context
*iso_context
;
84 struct fw_iso_buffer buffer
;
85 unsigned long vm_start
;
87 struct list_head link
;
90 static inline void __user
*
91 u64_to_uptr(__u64 value
)
93 return (void __user
*)(unsigned long)value
;
97 uptr_to_u64(void __user
*ptr
)
99 return (__u64
)(unsigned long)ptr
;
102 static int fw_device_op_open(struct inode
*inode
, struct file
*file
)
104 struct fw_device
*device
;
105 struct client
*client
;
108 device
= fw_device_from_devt(inode
->i_rdev
);
112 client
= kzalloc(sizeof *client
, GFP_KERNEL
);
116 client
->device
= fw_device_get(device
);
117 INIT_LIST_HEAD(&client
->event_list
);
118 INIT_LIST_HEAD(&client
->handler_list
);
119 INIT_LIST_HEAD(&client
->request_list
);
120 INIT_LIST_HEAD(&client
->transaction_list
);
121 spin_lock_init(&client
->lock
);
122 init_waitqueue_head(&client
->wait
);
124 file
->private_data
= client
;
126 spin_lock_irqsave(&device
->card
->lock
, flags
);
127 list_add_tail(&client
->link
, &device
->client_list
);
128 spin_unlock_irqrestore(&device
->card
->lock
, flags
);
133 static void queue_event(struct client
*client
, struct event
*event
,
134 void *data0
, size_t size0
, void *data1
, size_t size1
)
138 event
->v
[0].data
= data0
;
139 event
->v
[0].size
= size0
;
140 event
->v
[1].data
= data1
;
141 event
->v
[1].size
= size1
;
143 spin_lock_irqsave(&client
->lock
, flags
);
145 list_add_tail(&event
->link
, &client
->event_list
);
146 wake_up_interruptible(&client
->wait
);
148 spin_unlock_irqrestore(&client
->lock
, flags
);
152 dequeue_event(struct client
*client
, char __user
*buffer
, size_t count
)
159 retval
= wait_event_interruptible(client
->wait
,
160 !list_empty(&client
->event_list
) ||
161 fw_device_is_shutdown(client
->device
));
165 if (list_empty(&client
->event_list
) &&
166 fw_device_is_shutdown(client
->device
))
169 spin_lock_irqsave(&client
->lock
, flags
);
170 event
= container_of(client
->event_list
.next
, struct event
, link
);
171 list_del(&event
->link
);
172 spin_unlock_irqrestore(&client
->lock
, flags
);
175 for (i
= 0; i
< ARRAY_SIZE(event
->v
) && total
< count
; i
++) {
176 size
= min(event
->v
[i
].size
, count
- total
);
177 if (copy_to_user(buffer
+ total
, event
->v
[i
].data
, size
)) {
192 fw_device_op_read(struct file
*file
,
193 char __user
*buffer
, size_t count
, loff_t
*offset
)
195 struct client
*client
= file
->private_data
;
197 return dequeue_event(client
, buffer
, count
);
201 fill_bus_reset_event(struct fw_cdev_event_bus_reset
*event
,
202 struct fw_device
*device
)
204 struct fw_card
*card
= device
->card
;
206 event
->type
= FW_CDEV_EVENT_BUS_RESET
;
207 event
->node_id
= device
->node_id
;
208 event
->local_node_id
= card
->local_node
->node_id
;
209 event
->bm_node_id
= 0; /* FIXME: We don't track the BM. */
210 event
->irm_node_id
= card
->irm_node
->node_id
;
211 event
->root_node_id
= card
->root_node
->node_id
;
212 event
->generation
= card
->generation
;
216 for_each_client(struct fw_device
*device
,
217 void (*callback
)(struct client
*client
))
219 struct fw_card
*card
= device
->card
;
223 spin_lock_irqsave(&card
->lock
, flags
);
225 list_for_each_entry(c
, &device
->client_list
, link
)
228 spin_unlock_irqrestore(&card
->lock
, flags
);
232 queue_bus_reset_event(struct client
*client
)
234 struct bus_reset
*bus_reset
;
235 struct fw_device
*device
= client
->device
;
237 bus_reset
= kzalloc(sizeof *bus_reset
, GFP_ATOMIC
);
238 if (bus_reset
== NULL
) {
239 fw_notify("Out of memory when allocating bus reset event\n");
243 fill_bus_reset_event(&bus_reset
->reset
, device
);
245 queue_event(client
, &bus_reset
->event
,
246 &bus_reset
->reset
, sizeof bus_reset
->reset
, NULL
, 0);
249 void fw_device_cdev_update(struct fw_device
*device
)
251 for_each_client(device
, queue_bus_reset_event
);
254 static void wake_up_client(struct client
*client
)
256 wake_up_interruptible(&client
->wait
);
259 void fw_device_cdev_remove(struct fw_device
*device
)
261 for_each_client(device
, wake_up_client
);
264 static int ioctl_get_info(struct client
*client
, void __user
*arg
)
266 struct fw_cdev_get_info get_info
;
267 struct fw_cdev_event_bus_reset bus_reset
;
269 if (copy_from_user(&get_info
, arg
, sizeof get_info
))
272 client
->version
= get_info
.version
;
273 get_info
.version
= FW_CDEV_VERSION
;
275 if (get_info
.rom
!= 0) {
276 void __user
*uptr
= u64_to_uptr(get_info
.rom
);
277 size_t want
= get_info
.rom_length
;
278 size_t have
= client
->device
->config_rom_length
* 4;
280 if (copy_to_user(uptr
, client
->device
->config_rom
,
284 get_info
.rom_length
= client
->device
->config_rom_length
* 4;
286 if (get_info
.bus_reset
!= 0) {
287 void __user
*uptr
= u64_to_uptr(get_info
.bus_reset
);
289 fill_bus_reset_event(&bus_reset
, client
->device
);
290 if (copy_to_user(uptr
, &bus_reset
, sizeof bus_reset
))
294 get_info
.card
= client
->device
->card
->index
;
296 if (copy_to_user(arg
, &get_info
, sizeof get_info
))
303 complete_transaction(struct fw_card
*card
, int rcode
,
304 void *payload
, size_t length
, void *data
)
306 struct response
*response
= data
;
307 struct client
*client
= response
->client
;
310 if (length
< response
->response
.length
)
311 response
->response
.length
= length
;
312 if (rcode
== RCODE_COMPLETE
)
313 memcpy(response
->response
.data
, payload
,
314 response
->response
.length
);
316 spin_lock_irqsave(&client
->lock
, flags
);
317 list_del(&response
->link
);
318 spin_unlock_irqrestore(&client
->lock
, flags
);
320 response
->response
.type
= FW_CDEV_EVENT_RESPONSE
;
321 response
->response
.rcode
= rcode
;
322 queue_event(client
, &response
->event
,
323 &response
->response
, sizeof response
->response
,
324 response
->response
.data
, response
->response
.length
);
327 static ssize_t
ioctl_send_request(struct client
*client
, void __user
*arg
)
329 struct fw_device
*device
= client
->device
;
330 struct fw_cdev_send_request request
;
331 struct response
*response
;
334 if (copy_from_user(&request
, arg
, sizeof request
))
337 /* What is the biggest size we'll accept, really? */
338 if (request
.length
> 4096)
341 response
= kmalloc(sizeof *response
+ request
.length
, GFP_KERNEL
);
342 if (response
== NULL
)
345 response
->client
= client
;
346 response
->response
.length
= request
.length
;
347 response
->response
.closure
= request
.closure
;
350 copy_from_user(response
->response
.data
,
351 u64_to_uptr(request
.data
), request
.length
)) {
356 spin_lock_irqsave(&client
->lock
, flags
);
357 list_add_tail(&response
->link
, &client
->transaction_list
);
358 spin_unlock_irqrestore(&client
->lock
, flags
);
360 fw_send_request(device
->card
, &response
->transaction
,
361 request
.tcode
& 0x1f,
362 device
->node
->node_id
,
364 device
->node
->max_speed
,
366 response
->response
.data
, request
.length
,
367 complete_transaction
, response
);
370 return sizeof request
+ request
.length
;
372 return sizeof request
;
375 struct address_handler
{
376 struct fw_address_handler handler
;
378 struct client
*client
;
379 struct list_head link
;
383 struct fw_request
*request
;
387 struct list_head link
;
390 struct request_event
{
392 struct fw_cdev_event_request request
;
396 handle_request(struct fw_card
*card
, struct fw_request
*r
,
397 int tcode
, int destination
, int source
,
398 int generation
, int speed
,
399 unsigned long long offset
,
400 void *payload
, size_t length
, void *callback_data
)
402 struct address_handler
*handler
= callback_data
;
403 struct request
*request
;
404 struct request_event
*e
;
406 struct client
*client
= handler
->client
;
408 request
= kmalloc(sizeof *request
, GFP_ATOMIC
);
409 e
= kmalloc(sizeof *e
, GFP_ATOMIC
);
410 if (request
== NULL
|| e
== NULL
) {
413 fw_send_response(card
, r
, RCODE_CONFLICT_ERROR
);
417 request
->request
= r
;
418 request
->data
= payload
;
419 request
->length
= length
;
421 spin_lock_irqsave(&client
->lock
, flags
);
422 request
->serial
= client
->request_serial
++;
423 list_add_tail(&request
->link
, &client
->request_list
);
424 spin_unlock_irqrestore(&client
->lock
, flags
);
426 e
->request
.type
= FW_CDEV_EVENT_REQUEST
;
427 e
->request
.tcode
= tcode
;
428 e
->request
.offset
= offset
;
429 e
->request
.length
= length
;
430 e
->request
.serial
= request
->serial
;
431 e
->request
.closure
= handler
->closure
;
433 queue_event(client
, &e
->event
,
434 &e
->request
, sizeof e
->request
, payload
, length
);
437 static int ioctl_allocate(struct client
*client
, void __user
*arg
)
439 struct fw_cdev_allocate request
;
440 struct address_handler
*handler
;
442 struct fw_address_region region
;
444 if (copy_from_user(&request
, arg
, sizeof request
))
447 handler
= kmalloc(sizeof *handler
, GFP_KERNEL
);
451 region
.start
= request
.offset
;
452 region
.end
= request
.offset
+ request
.length
;
453 handler
->handler
.length
= request
.length
;
454 handler
->handler
.address_callback
= handle_request
;
455 handler
->handler
.callback_data
= handler
;
456 handler
->closure
= request
.closure
;
457 handler
->client
= client
;
459 if (fw_core_add_address_handler(&handler
->handler
, ®ion
) < 0) {
464 spin_lock_irqsave(&client
->lock
, flags
);
465 list_add_tail(&handler
->link
, &client
->handler_list
);
466 spin_unlock_irqrestore(&client
->lock
, flags
);
471 static int ioctl_deallocate(struct client
*client
, void __user
*arg
)
473 struct fw_cdev_deallocate request
;
474 struct address_handler
*handler
;
477 if (copy_from_user(&request
, arg
, sizeof request
))
480 spin_lock_irqsave(&client
->lock
, flags
);
481 list_for_each_entry(handler
, &client
->handler_list
, link
) {
482 if (handler
->handler
.offset
== request
.offset
) {
483 list_del(&handler
->link
);
487 spin_unlock_irqrestore(&client
->lock
, flags
);
489 if (&handler
->link
== &client
->handler_list
)
492 fw_core_remove_address_handler(&handler
->handler
);
497 static int ioctl_send_response(struct client
*client
, void __user
*arg
)
499 struct fw_cdev_send_response request
;
503 if (copy_from_user(&request
, arg
, sizeof request
))
506 spin_lock_irqsave(&client
->lock
, flags
);
507 list_for_each_entry(r
, &client
->request_list
, link
) {
508 if (r
->serial
== request
.serial
) {
513 spin_unlock_irqrestore(&client
->lock
, flags
);
515 if (&r
->link
== &client
->request_list
)
518 if (request
.length
< r
->length
)
519 r
->length
= request
.length
;
520 if (copy_from_user(r
->data
, u64_to_uptr(request
.data
), r
->length
))
523 fw_send_response(client
->device
->card
, r
->request
, request
.rcode
);
530 static int ioctl_initiate_bus_reset(struct client
*client
, void __user
*arg
)
532 struct fw_cdev_initiate_bus_reset request
;
535 if (copy_from_user(&request
, arg
, sizeof request
))
538 short_reset
= (request
.type
== FW_CDEV_SHORT_RESET
);
540 return fw_core_initiate_bus_reset(client
->device
->card
, short_reset
);
544 iso_callback(struct fw_iso_context
*context
, u32 cycle
,
545 size_t header_length
, void *header
, void *data
)
547 struct client
*client
= data
;
548 struct iso_interrupt
*interrupt
;
550 interrupt
= kzalloc(sizeof *interrupt
+ header_length
, GFP_ATOMIC
);
551 if (interrupt
== NULL
)
554 interrupt
->interrupt
.type
= FW_CDEV_EVENT_ISO_INTERRUPT
;
555 interrupt
->interrupt
.closure
= 0;
556 interrupt
->interrupt
.cycle
= cycle
;
557 interrupt
->interrupt
.header_length
= header_length
;
558 memcpy(interrupt
->interrupt
.header
, header
, header_length
);
559 queue_event(client
, &interrupt
->event
,
560 &interrupt
->interrupt
,
561 sizeof interrupt
->interrupt
+ header_length
, NULL
, 0);
564 static int ioctl_create_iso_context(struct client
*client
, void __user
*arg
)
566 struct fw_cdev_create_iso_context request
;
568 if (copy_from_user(&request
, arg
, sizeof request
))
571 if (request
.channel
> 63)
574 switch (request
.type
) {
575 case FW_ISO_CONTEXT_RECEIVE
:
576 if (request
.header_size
< 4 || (request
.header_size
& 3))
581 case FW_ISO_CONTEXT_TRANSMIT
:
582 if (request
.speed
> SCODE_3200
)
591 client
->iso_context
= fw_iso_context_create(client
->device
->card
,
596 iso_callback
, client
);
597 if (IS_ERR(client
->iso_context
))
598 return PTR_ERR(client
->iso_context
);
603 static int ioctl_queue_iso(struct client
*client
, void __user
*arg
)
605 struct fw_cdev_queue_iso request
;
606 struct fw_cdev_iso_packet __user
*p
, *end
, *next
;
607 struct fw_iso_context
*ctx
= client
->iso_context
;
608 unsigned long payload
, payload_end
, header_length
;
611 struct fw_iso_packet packet
;
617 if (copy_from_user(&request
, arg
, sizeof request
))
620 /* If the user passes a non-NULL data pointer, has mmap()'ed
621 * the iso buffer, and the pointer points inside the buffer,
622 * we setup the payload pointers accordingly. Otherwise we
623 * set them both to 0, which will still let packets with
624 * payload_length == 0 through. In other words, if no packets
625 * use the indirect payload, the iso buffer need not be mapped
626 * and the request.data pointer is ignored.*/
628 payload
= (unsigned long)request
.data
- client
->vm_start
;
629 payload_end
= payload
+ (client
->buffer
.page_count
<< PAGE_SHIFT
);
630 if (request
.data
== 0 || client
->buffer
.pages
== NULL
||
631 payload
>= payload_end
) {
636 if (!access_ok(VERIFY_READ
, request
.packets
, request
.size
))
639 p
= (struct fw_cdev_iso_packet __user
*)u64_to_uptr(request
.packets
);
640 end
= (void __user
*)p
+ request
.size
;
643 if (__copy_from_user(&u
.packet
, p
, sizeof *p
))
646 if (ctx
->type
== FW_ISO_CONTEXT_TRANSMIT
) {
647 header_length
= u
.packet
.header_length
;
649 /* We require that header_length is a multiple of
650 * the fixed header size, ctx->header_size */
651 if (ctx
->header_size
== 0) {
652 if (u
.packet
.header_length
> 0)
654 } else if (u
.packet
.header_length
% ctx
->header_size
!= 0) {
660 next
= (struct fw_cdev_iso_packet __user
*)
661 &p
->header
[header_length
/ 4];
665 (u
.packet
.header
, p
->header
, header_length
))
667 if (u
.packet
.skip
&& ctx
->type
== FW_ISO_CONTEXT_TRANSMIT
&&
668 u
.packet
.header_length
+ u
.packet
.payload_length
> 0)
670 if (payload
+ u
.packet
.payload_length
> payload_end
)
673 if (fw_iso_context_queue(ctx
, &u
.packet
,
674 &client
->buffer
, payload
))
678 payload
+= u
.packet
.payload_length
;
682 request
.size
-= uptr_to_u64(p
) - request
.packets
;
683 request
.packets
= uptr_to_u64(p
);
684 request
.data
= client
->vm_start
+ payload
;
686 if (copy_to_user(arg
, &request
, sizeof request
))
692 static int ioctl_start_iso(struct client
*client
, void __user
*arg
)
694 struct fw_cdev_start_iso request
;
696 if (copy_from_user(&request
, arg
, sizeof request
))
699 if (client
->iso_context
->type
== FW_ISO_CONTEXT_RECEIVE
) {
700 if (request
.tags
== 0 || request
.tags
> 15)
703 if (request
.sync
> 15)
707 return fw_iso_context_start(client
->iso_context
,
708 request
.cycle
, request
.sync
, request
.tags
);
711 static int ioctl_stop_iso(struct client
*client
, void __user
*arg
)
713 return fw_iso_context_stop(client
->iso_context
);
717 dispatch_ioctl(struct client
*client
, unsigned int cmd
, void __user
*arg
)
720 case FW_CDEV_IOC_GET_INFO
:
721 return ioctl_get_info(client
, arg
);
722 case FW_CDEV_IOC_SEND_REQUEST
:
723 return ioctl_send_request(client
, arg
);
724 case FW_CDEV_IOC_ALLOCATE
:
725 return ioctl_allocate(client
, arg
);
726 case FW_CDEV_IOC_DEALLOCATE
:
727 return ioctl_deallocate(client
, arg
);
728 case FW_CDEV_IOC_SEND_RESPONSE
:
729 return ioctl_send_response(client
, arg
);
730 case FW_CDEV_IOC_INITIATE_BUS_RESET
:
731 return ioctl_initiate_bus_reset(client
, arg
);
732 case FW_CDEV_IOC_CREATE_ISO_CONTEXT
:
733 return ioctl_create_iso_context(client
, arg
);
734 case FW_CDEV_IOC_QUEUE_ISO
:
735 return ioctl_queue_iso(client
, arg
);
736 case FW_CDEV_IOC_START_ISO
:
737 return ioctl_start_iso(client
, arg
);
738 case FW_CDEV_IOC_STOP_ISO
:
739 return ioctl_stop_iso(client
, arg
);
746 fw_device_op_ioctl(struct file
*file
,
747 unsigned int cmd
, unsigned long arg
)
749 struct client
*client
= file
->private_data
;
751 return dispatch_ioctl(client
, cmd
, (void __user
*) arg
);
756 fw_device_op_compat_ioctl(struct file
*file
,
757 unsigned int cmd
, unsigned long arg
)
759 struct client
*client
= file
->private_data
;
761 return dispatch_ioctl(client
, cmd
, compat_ptr(arg
));
765 static int fw_device_op_mmap(struct file
*file
, struct vm_area_struct
*vma
)
767 struct client
*client
= file
->private_data
;
768 enum dma_data_direction direction
;
770 int page_count
, retval
;
772 /* FIXME: We could support multiple buffers, but we don't. */
773 if (client
->buffer
.pages
!= NULL
)
776 if (!(vma
->vm_flags
& VM_SHARED
))
779 if (vma
->vm_start
& ~PAGE_MASK
)
782 client
->vm_start
= vma
->vm_start
;
783 size
= vma
->vm_end
- vma
->vm_start
;
784 page_count
= size
>> PAGE_SHIFT
;
785 if (size
& ~PAGE_MASK
)
788 if (vma
->vm_flags
& VM_WRITE
)
789 direction
= DMA_TO_DEVICE
;
791 direction
= DMA_FROM_DEVICE
;
793 retval
= fw_iso_buffer_init(&client
->buffer
, client
->device
->card
,
794 page_count
, direction
);
798 retval
= fw_iso_buffer_map(&client
->buffer
, vma
);
800 fw_iso_buffer_destroy(&client
->buffer
, client
->device
->card
);
805 static int fw_device_op_release(struct inode
*inode
, struct file
*file
)
807 struct client
*client
= file
->private_data
;
808 struct address_handler
*h
, *next_h
;
809 struct request
*r
, *next_r
;
810 struct event
*e
, *next_e
;
811 struct response
*t
, *next_t
;
814 if (client
->buffer
.pages
)
815 fw_iso_buffer_destroy(&client
->buffer
, client
->device
->card
);
817 if (client
->iso_context
)
818 fw_iso_context_destroy(client
->iso_context
);
820 list_for_each_entry_safe(h
, next_h
, &client
->handler_list
, link
) {
821 fw_core_remove_address_handler(&h
->handler
);
825 list_for_each_entry_safe(r
, next_r
, &client
->request_list
, link
) {
826 fw_send_response(client
->device
->card
, r
->request
,
827 RCODE_CONFLICT_ERROR
);
831 list_for_each_entry_safe(t
, next_t
, &client
->transaction_list
, link
) {
832 fw_cancel_transaction(client
->device
->card
, &t
->transaction
);
836 /* FIXME: We should wait for the async tasklets to stop
837 * running before freeing the memory. */
839 list_for_each_entry_safe(e
, next_e
, &client
->event_list
, link
)
842 spin_lock_irqsave(&client
->device
->card
->lock
, flags
);
843 list_del(&client
->link
);
844 spin_unlock_irqrestore(&client
->device
->card
->lock
, flags
);
846 fw_device_put(client
->device
);
852 static unsigned int fw_device_op_poll(struct file
*file
, poll_table
* pt
)
854 struct client
*client
= file
->private_data
;
855 unsigned int mask
= 0;
857 poll_wait(file
, &client
->wait
, pt
);
859 if (fw_device_is_shutdown(client
->device
))
860 mask
|= POLLHUP
| POLLERR
;
861 if (!list_empty(&client
->event_list
))
862 mask
|= POLLIN
| POLLRDNORM
;
867 const struct file_operations fw_device_ops
= {
868 .owner
= THIS_MODULE
,
869 .open
= fw_device_op_open
,
870 .read
= fw_device_op_read
,
871 .unlocked_ioctl
= fw_device_op_ioctl
,
872 .poll
= fw_device_op_poll
,
873 .release
= fw_device_op_release
,
874 .mmap
= fw_device_op_mmap
,
877 .compat_ioctl
= fw_device_op_compat_ioctl
,