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.h>
26 #include <linux/firewire-cdev.h>
27 #include <linux/idr.h>
28 #include <linux/irqflags.h>
29 #include <linux/jiffies.h>
30 #include <linux/kernel.h>
31 #include <linux/kref.h>
33 #include <linux/module.h>
34 #include <linux/mutex.h>
35 #include <linux/poll.h>
36 #include <linux/sched.h>
37 #include <linux/spinlock.h>
38 #include <linux/string.h>
39 #include <linux/time.h>
40 #include <linux/uaccess.h>
41 #include <linux/vmalloc.h>
42 #include <linux/wait.h>
43 #include <linux/workqueue.h>
45 #include <asm/system.h>
51 struct fw_device
*device
;
55 struct idr resource_idr
;
56 struct list_head event_list
;
57 wait_queue_head_t wait
;
58 u64 bus_reset_closure
;
60 struct fw_iso_context
*iso_context
;
62 struct fw_iso_buffer buffer
;
63 unsigned long vm_start
;
65 struct list_head link
;
69 static inline void client_get(struct client
*client
)
71 kref_get(&client
->kref
);
74 static void client_release(struct kref
*kref
)
76 struct client
*client
= container_of(kref
, struct client
, kref
);
78 fw_device_put(client
->device
);
82 static void client_put(struct client
*client
)
84 kref_put(&client
->kref
, client_release
);
87 struct client_resource
;
88 typedef void (*client_resource_release_fn_t
)(struct client
*,
89 struct client_resource
*);
90 struct client_resource
{
91 client_resource_release_fn_t release
;
95 struct address_handler_resource
{
96 struct client_resource resource
;
97 struct fw_address_handler handler
;
99 struct client
*client
;
102 struct outbound_transaction_resource
{
103 struct client_resource resource
;
104 struct fw_transaction transaction
;
107 struct inbound_transaction_resource
{
108 struct client_resource resource
;
109 struct fw_card
*card
;
110 struct fw_request
*request
;
115 struct descriptor_resource
{
116 struct client_resource resource
;
117 struct fw_descriptor descriptor
;
121 struct iso_resource
{
122 struct client_resource resource
;
123 struct client
*client
;
124 /* Schedule work and access todo only with client->lock held. */
125 struct delayed_work work
;
126 enum {ISO_RES_ALLOC
, ISO_RES_REALLOC
, ISO_RES_DEALLOC
,
127 ISO_RES_ALLOC_ONCE
, ISO_RES_DEALLOC_ONCE
,} todo
;
131 __be32 transaction_data
[2];
132 struct iso_resource_event
*e_alloc
, *e_dealloc
;
135 static void release_iso_resource(struct client
*, struct client_resource
*);
137 static void schedule_iso_resource(struct iso_resource
*r
, unsigned long delay
)
139 client_get(r
->client
);
140 if (!schedule_delayed_work(&r
->work
, delay
))
141 client_put(r
->client
);
144 static void schedule_if_iso_resource(struct client_resource
*resource
)
146 if (resource
->release
== release_iso_resource
)
147 schedule_iso_resource(container_of(resource
,
148 struct iso_resource
, resource
), 0);
152 * dequeue_event() just kfree()'s the event, so the event has to be
153 * the first field in a struct XYZ_event.
156 struct { void *data
; size_t size
; } v
[2];
157 struct list_head link
;
160 struct bus_reset_event
{
162 struct fw_cdev_event_bus_reset reset
;
165 struct outbound_transaction_event
{
167 struct client
*client
;
168 struct outbound_transaction_resource r
;
169 struct fw_cdev_event_response response
;
172 struct inbound_transaction_event
{
174 struct fw_cdev_event_request request
;
177 struct iso_interrupt_event
{
179 struct fw_cdev_event_iso_interrupt interrupt
;
182 struct iso_resource_event
{
184 struct fw_cdev_event_iso_resource iso_resource
;
187 static inline void __user
*u64_to_uptr(__u64 value
)
189 return (void __user
*)(unsigned long)value
;
192 static inline __u64
uptr_to_u64(void __user
*ptr
)
194 return (__u64
)(unsigned long)ptr
;
197 static int fw_device_op_open(struct inode
*inode
, struct file
*file
)
199 struct fw_device
*device
;
200 struct client
*client
;
202 device
= fw_device_get_by_devt(inode
->i_rdev
);
206 if (fw_device_is_shutdown(device
)) {
207 fw_device_put(device
);
211 client
= kzalloc(sizeof(*client
), GFP_KERNEL
);
212 if (client
== NULL
) {
213 fw_device_put(device
);
217 client
->device
= device
;
218 spin_lock_init(&client
->lock
);
219 idr_init(&client
->resource_idr
);
220 INIT_LIST_HEAD(&client
->event_list
);
221 init_waitqueue_head(&client
->wait
);
222 kref_init(&client
->kref
);
224 file
->private_data
= client
;
226 mutex_lock(&device
->client_list_mutex
);
227 list_add_tail(&client
->link
, &device
->client_list
);
228 mutex_unlock(&device
->client_list_mutex
);
230 return nonseekable_open(inode
, file
);
233 static void queue_event(struct client
*client
, struct event
*event
,
234 void *data0
, size_t size0
, void *data1
, size_t size1
)
238 event
->v
[0].data
= data0
;
239 event
->v
[0].size
= size0
;
240 event
->v
[1].data
= data1
;
241 event
->v
[1].size
= size1
;
243 spin_lock_irqsave(&client
->lock
, flags
);
244 if (client
->in_shutdown
)
247 list_add_tail(&event
->link
, &client
->event_list
);
248 spin_unlock_irqrestore(&client
->lock
, flags
);
250 wake_up_interruptible(&client
->wait
);
253 static int dequeue_event(struct client
*client
,
254 char __user
*buffer
, size_t count
)
260 ret
= wait_event_interruptible(client
->wait
,
261 !list_empty(&client
->event_list
) ||
262 fw_device_is_shutdown(client
->device
));
266 if (list_empty(&client
->event_list
) &&
267 fw_device_is_shutdown(client
->device
))
270 spin_lock_irq(&client
->lock
);
271 event
= list_first_entry(&client
->event_list
, struct event
, link
);
272 list_del(&event
->link
);
273 spin_unlock_irq(&client
->lock
);
276 for (i
= 0; i
< ARRAY_SIZE(event
->v
) && total
< count
; i
++) {
277 size
= min(event
->v
[i
].size
, count
- total
);
278 if (copy_to_user(buffer
+ total
, event
->v
[i
].data
, size
)) {
292 static ssize_t
fw_device_op_read(struct file
*file
, char __user
*buffer
,
293 size_t count
, loff_t
*offset
)
295 struct client
*client
= file
->private_data
;
297 return dequeue_event(client
, buffer
, count
);
300 static void fill_bus_reset_event(struct fw_cdev_event_bus_reset
*event
,
301 struct client
*client
)
303 struct fw_card
*card
= client
->device
->card
;
305 spin_lock_irq(&card
->lock
);
307 event
->closure
= client
->bus_reset_closure
;
308 event
->type
= FW_CDEV_EVENT_BUS_RESET
;
309 event
->generation
= client
->device
->generation
;
310 event
->node_id
= client
->device
->node_id
;
311 event
->local_node_id
= card
->local_node
->node_id
;
312 event
->bm_node_id
= 0; /* FIXME: We don't track the BM. */
313 event
->irm_node_id
= card
->irm_node
->node_id
;
314 event
->root_node_id
= card
->root_node
->node_id
;
316 spin_unlock_irq(&card
->lock
);
319 static void for_each_client(struct fw_device
*device
,
320 void (*callback
)(struct client
*client
))
324 mutex_lock(&device
->client_list_mutex
);
325 list_for_each_entry(c
, &device
->client_list
, link
)
327 mutex_unlock(&device
->client_list_mutex
);
330 static int schedule_reallocations(int id
, void *p
, void *data
)
332 schedule_if_iso_resource(p
);
337 static void queue_bus_reset_event(struct client
*client
)
339 struct bus_reset_event
*e
;
341 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
343 fw_notify("Out of memory when allocating bus reset event\n");
347 fill_bus_reset_event(&e
->reset
, client
);
349 queue_event(client
, &e
->event
,
350 &e
->reset
, sizeof(e
->reset
), NULL
, 0);
352 spin_lock_irq(&client
->lock
);
353 idr_for_each(&client
->resource_idr
, schedule_reallocations
, client
);
354 spin_unlock_irq(&client
->lock
);
357 void fw_device_cdev_update(struct fw_device
*device
)
359 for_each_client(device
, queue_bus_reset_event
);
362 static void wake_up_client(struct client
*client
)
364 wake_up_interruptible(&client
->wait
);
367 void fw_device_cdev_remove(struct fw_device
*device
)
369 for_each_client(device
, wake_up_client
);
373 struct fw_cdev_get_info get_info
;
374 struct fw_cdev_send_request send_request
;
375 struct fw_cdev_allocate allocate
;
376 struct fw_cdev_deallocate deallocate
;
377 struct fw_cdev_send_response send_response
;
378 struct fw_cdev_initiate_bus_reset initiate_bus_reset
;
379 struct fw_cdev_add_descriptor add_descriptor
;
380 struct fw_cdev_remove_descriptor remove_descriptor
;
381 struct fw_cdev_create_iso_context create_iso_context
;
382 struct fw_cdev_queue_iso queue_iso
;
383 struct fw_cdev_start_iso start_iso
;
384 struct fw_cdev_stop_iso stop_iso
;
385 struct fw_cdev_get_cycle_timer get_cycle_timer
;
386 struct fw_cdev_allocate_iso_resource allocate_iso_resource
;
387 struct fw_cdev_send_stream_packet send_stream_packet
;
388 struct fw_cdev_get_cycle_timer2 get_cycle_timer2
;
391 static int ioctl_get_info(struct client
*client
, union ioctl_arg
*arg
)
393 struct fw_cdev_get_info
*a
= &arg
->get_info
;
394 struct fw_cdev_event_bus_reset bus_reset
;
395 unsigned long ret
= 0;
397 client
->version
= a
->version
;
398 a
->version
= FW_CDEV_VERSION
;
399 a
->card
= client
->device
->card
->index
;
401 down_read(&fw_device_rwsem
);
404 size_t want
= a
->rom_length
;
405 size_t have
= client
->device
->config_rom_length
* 4;
407 ret
= copy_to_user(u64_to_uptr(a
->rom
),
408 client
->device
->config_rom
, min(want
, have
));
410 a
->rom_length
= client
->device
->config_rom_length
* 4;
412 up_read(&fw_device_rwsem
);
417 client
->bus_reset_closure
= a
->bus_reset_closure
;
418 if (a
->bus_reset
!= 0) {
419 fill_bus_reset_event(&bus_reset
, client
);
420 if (copy_to_user(u64_to_uptr(a
->bus_reset
),
421 &bus_reset
, sizeof(bus_reset
)))
428 static int add_client_resource(struct client
*client
,
429 struct client_resource
*resource
, gfp_t gfp_mask
)
435 if (idr_pre_get(&client
->resource_idr
, gfp_mask
) == 0)
438 spin_lock_irqsave(&client
->lock
, flags
);
439 if (client
->in_shutdown
)
442 ret
= idr_get_new(&client
->resource_idr
, resource
,
446 schedule_if_iso_resource(resource
);
448 spin_unlock_irqrestore(&client
->lock
, flags
);
453 return ret
< 0 ? ret
: 0;
456 static int release_client_resource(struct client
*client
, u32 handle
,
457 client_resource_release_fn_t release
,
458 struct client_resource
**return_resource
)
460 struct client_resource
*resource
;
462 spin_lock_irq(&client
->lock
);
463 if (client
->in_shutdown
)
466 resource
= idr_find(&client
->resource_idr
, handle
);
467 if (resource
&& resource
->release
== release
)
468 idr_remove(&client
->resource_idr
, handle
);
469 spin_unlock_irq(&client
->lock
);
471 if (!(resource
&& resource
->release
== release
))
475 *return_resource
= resource
;
477 resource
->release(client
, resource
);
484 static void release_transaction(struct client
*client
,
485 struct client_resource
*resource
)
487 struct outbound_transaction_resource
*r
= container_of(resource
,
488 struct outbound_transaction_resource
, resource
);
490 fw_cancel_transaction(client
->device
->card
, &r
->transaction
);
493 static void complete_transaction(struct fw_card
*card
, int rcode
,
494 void *payload
, size_t length
, void *data
)
496 struct outbound_transaction_event
*e
= data
;
497 struct fw_cdev_event_response
*rsp
= &e
->response
;
498 struct client
*client
= e
->client
;
501 if (length
< rsp
->length
)
502 rsp
->length
= length
;
503 if (rcode
== RCODE_COMPLETE
)
504 memcpy(rsp
->data
, payload
, rsp
->length
);
506 spin_lock_irqsave(&client
->lock
, flags
);
508 * 1. If called while in shutdown, the idr tree must be left untouched.
509 * The idr handle will be removed and the client reference will be
511 * 2. If the call chain was release_client_resource ->
512 * release_transaction -> complete_transaction (instead of a normal
513 * conclusion of the transaction), i.e. if this resource was already
514 * unregistered from the idr, the client reference will be dropped
515 * by release_client_resource and we must not drop it here.
517 if (!client
->in_shutdown
&&
518 idr_find(&client
->resource_idr
, e
->r
.resource
.handle
)) {
519 idr_remove(&client
->resource_idr
, e
->r
.resource
.handle
);
520 /* Drop the idr's reference */
523 spin_unlock_irqrestore(&client
->lock
, flags
);
525 rsp
->type
= FW_CDEV_EVENT_RESPONSE
;
529 * In the case that sizeof(*rsp) doesn't align with the position of the
530 * data, and the read is short, preserve an extra copy of the data
531 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
532 * for short reads and some apps depended on it, this is both safe
533 * and prudent for compatibility.
535 if (rsp
->length
<= sizeof(*rsp
) - offsetof(typeof(*rsp
), data
))
536 queue_event(client
, &e
->event
, rsp
, sizeof(*rsp
),
537 rsp
->data
, rsp
->length
);
539 queue_event(client
, &e
->event
, rsp
, sizeof(*rsp
) + rsp
->length
,
542 /* Drop the transaction callback's reference */
546 static int init_request(struct client
*client
,
547 struct fw_cdev_send_request
*request
,
548 int destination_id
, int speed
)
550 struct outbound_transaction_event
*e
;
553 if (request
->tcode
!= TCODE_STREAM_DATA
&&
554 (request
->length
> 4096 || request
->length
> 512 << speed
))
557 e
= kmalloc(sizeof(*e
) + request
->length
, GFP_KERNEL
);
562 e
->response
.length
= request
->length
;
563 e
->response
.closure
= request
->closure
;
566 copy_from_user(e
->response
.data
,
567 u64_to_uptr(request
->data
), request
->length
)) {
572 e
->r
.resource
.release
= release_transaction
;
573 ret
= add_client_resource(client
, &e
->r
.resource
, GFP_KERNEL
);
577 /* Get a reference for the transaction callback */
580 fw_send_request(client
->device
->card
, &e
->r
.transaction
,
581 request
->tcode
, destination_id
, request
->generation
,
582 speed
, request
->offset
, e
->response
.data
,
583 request
->length
, complete_transaction
, e
);
592 static int ioctl_send_request(struct client
*client
, union ioctl_arg
*arg
)
594 switch (arg
->send_request
.tcode
) {
595 case TCODE_WRITE_QUADLET_REQUEST
:
596 case TCODE_WRITE_BLOCK_REQUEST
:
597 case TCODE_READ_QUADLET_REQUEST
:
598 case TCODE_READ_BLOCK_REQUEST
:
599 case TCODE_LOCK_MASK_SWAP
:
600 case TCODE_LOCK_COMPARE_SWAP
:
601 case TCODE_LOCK_FETCH_ADD
:
602 case TCODE_LOCK_LITTLE_ADD
:
603 case TCODE_LOCK_BOUNDED_ADD
:
604 case TCODE_LOCK_WRAP_ADD
:
605 case TCODE_LOCK_VENDOR_DEPENDENT
:
611 return init_request(client
, &arg
->send_request
, client
->device
->node_id
,
612 client
->device
->max_speed
);
615 static inline bool is_fcp_request(struct fw_request
*request
)
617 return request
== NULL
;
620 static void release_request(struct client
*client
,
621 struct client_resource
*resource
)
623 struct inbound_transaction_resource
*r
= container_of(resource
,
624 struct inbound_transaction_resource
, resource
);
626 if (is_fcp_request(r
->request
))
629 fw_send_response(r
->card
, r
->request
, RCODE_CONFLICT_ERROR
);
631 fw_card_put(r
->card
);
635 static void handle_request(struct fw_card
*card
, struct fw_request
*request
,
636 int tcode
, int destination
, int source
,
637 int generation
, unsigned long long offset
,
638 void *payload
, size_t length
, void *callback_data
)
640 struct address_handler_resource
*handler
= callback_data
;
641 struct inbound_transaction_resource
*r
;
642 struct inbound_transaction_event
*e
;
643 void *fcp_frame
= NULL
;
646 /* card may be different from handler->client->device->card */
649 r
= kmalloc(sizeof(*r
), GFP_ATOMIC
);
650 e
= kmalloc(sizeof(*e
), GFP_ATOMIC
);
651 if (r
== NULL
|| e
== NULL
)
655 r
->request
= request
;
659 if (is_fcp_request(request
)) {
661 * FIXME: Let core-transaction.c manage a
662 * single reference-counted copy?
664 fcp_frame
= kmemdup(payload
, length
, GFP_ATOMIC
);
665 if (fcp_frame
== NULL
)
671 r
->resource
.release
= release_request
;
672 ret
= add_client_resource(handler
->client
, &r
->resource
, GFP_ATOMIC
);
676 e
->request
.type
= FW_CDEV_EVENT_REQUEST
;
677 e
->request
.tcode
= tcode
;
678 e
->request
.offset
= offset
;
679 e
->request
.length
= length
;
680 e
->request
.handle
= r
->resource
.handle
;
681 e
->request
.closure
= handler
->closure
;
683 queue_event(handler
->client
, &e
->event
,
684 &e
->request
, sizeof(e
->request
), r
->data
, length
);
692 if (!is_fcp_request(request
))
693 fw_send_response(card
, request
, RCODE_CONFLICT_ERROR
);
698 static void release_address_handler(struct client
*client
,
699 struct client_resource
*resource
)
701 struct address_handler_resource
*r
=
702 container_of(resource
, struct address_handler_resource
, resource
);
704 fw_core_remove_address_handler(&r
->handler
);
708 static int ioctl_allocate(struct client
*client
, union ioctl_arg
*arg
)
710 struct fw_cdev_allocate
*a
= &arg
->allocate
;
711 struct address_handler_resource
*r
;
712 struct fw_address_region region
;
715 r
= kmalloc(sizeof(*r
), GFP_KERNEL
);
719 region
.start
= a
->offset
;
720 region
.end
= a
->offset
+ a
->length
;
721 r
->handler
.length
= a
->length
;
722 r
->handler
.address_callback
= handle_request
;
723 r
->handler
.callback_data
= r
;
724 r
->closure
= a
->closure
;
727 ret
= fw_core_add_address_handler(&r
->handler
, ®ion
);
733 r
->resource
.release
= release_address_handler
;
734 ret
= add_client_resource(client
, &r
->resource
, GFP_KERNEL
);
736 release_address_handler(client
, &r
->resource
);
739 a
->handle
= r
->resource
.handle
;
744 static int ioctl_deallocate(struct client
*client
, union ioctl_arg
*arg
)
746 return release_client_resource(client
, arg
->deallocate
.handle
,
747 release_address_handler
, NULL
);
750 static int ioctl_send_response(struct client
*client
, union ioctl_arg
*arg
)
752 struct fw_cdev_send_response
*a
= &arg
->send_response
;
753 struct client_resource
*resource
;
754 struct inbound_transaction_resource
*r
;
757 if (release_client_resource(client
, a
->handle
,
758 release_request
, &resource
) < 0)
761 r
= container_of(resource
, struct inbound_transaction_resource
,
763 if (is_fcp_request(r
->request
))
766 if (a
->length
!= fw_get_response_length(r
->request
)) {
771 if (copy_from_user(r
->data
, u64_to_uptr(a
->data
), a
->length
)) {
776 fw_send_response(r
->card
, r
->request
, a
->rcode
);
778 fw_card_put(r
->card
);
784 static int ioctl_initiate_bus_reset(struct client
*client
, union ioctl_arg
*arg
)
786 return fw_core_initiate_bus_reset(client
->device
->card
,
787 arg
->initiate_bus_reset
.type
== FW_CDEV_SHORT_RESET
);
790 static void release_descriptor(struct client
*client
,
791 struct client_resource
*resource
)
793 struct descriptor_resource
*r
=
794 container_of(resource
, struct descriptor_resource
, resource
);
796 fw_core_remove_descriptor(&r
->descriptor
);
800 static int ioctl_add_descriptor(struct client
*client
, union ioctl_arg
*arg
)
802 struct fw_cdev_add_descriptor
*a
= &arg
->add_descriptor
;
803 struct descriptor_resource
*r
;
806 /* Access policy: Allow this ioctl only on local nodes' device files. */
807 if (!client
->device
->is_local
)
813 r
= kmalloc(sizeof(*r
) + a
->length
* 4, GFP_KERNEL
);
817 if (copy_from_user(r
->data
, u64_to_uptr(a
->data
), a
->length
* 4)) {
822 r
->descriptor
.length
= a
->length
;
823 r
->descriptor
.immediate
= a
->immediate
;
824 r
->descriptor
.key
= a
->key
;
825 r
->descriptor
.data
= r
->data
;
827 ret
= fw_core_add_descriptor(&r
->descriptor
);
831 r
->resource
.release
= release_descriptor
;
832 ret
= add_client_resource(client
, &r
->resource
, GFP_KERNEL
);
834 fw_core_remove_descriptor(&r
->descriptor
);
837 a
->handle
= r
->resource
.handle
;
846 static int ioctl_remove_descriptor(struct client
*client
, union ioctl_arg
*arg
)
848 return release_client_resource(client
, arg
->remove_descriptor
.handle
,
849 release_descriptor
, NULL
);
852 static void iso_callback(struct fw_iso_context
*context
, u32 cycle
,
853 size_t header_length
, void *header
, void *data
)
855 struct client
*client
= data
;
856 struct iso_interrupt_event
*e
;
858 e
= kmalloc(sizeof(*e
) + header_length
, GFP_ATOMIC
);
862 e
->interrupt
.type
= FW_CDEV_EVENT_ISO_INTERRUPT
;
863 e
->interrupt
.closure
= client
->iso_closure
;
864 e
->interrupt
.cycle
= cycle
;
865 e
->interrupt
.header_length
= header_length
;
866 memcpy(e
->interrupt
.header
, header
, header_length
);
867 queue_event(client
, &e
->event
, &e
->interrupt
,
868 sizeof(e
->interrupt
) + header_length
, NULL
, 0);
871 static int ioctl_create_iso_context(struct client
*client
, union ioctl_arg
*arg
)
873 struct fw_cdev_create_iso_context
*a
= &arg
->create_iso_context
;
874 struct fw_iso_context
*context
;
880 case FW_ISO_CONTEXT_RECEIVE
:
881 if (a
->header_size
< 4 || (a
->header_size
& 3))
885 case FW_ISO_CONTEXT_TRANSMIT
:
886 if (a
->speed
> SCODE_3200
)
894 context
= fw_iso_context_create(client
->device
->card
, a
->type
,
895 a
->channel
, a
->speed
, a
->header_size
,
896 iso_callback
, client
);
898 return PTR_ERR(context
);
900 /* We only support one context at this time. */
901 spin_lock_irq(&client
->lock
);
902 if (client
->iso_context
!= NULL
) {
903 spin_unlock_irq(&client
->lock
);
904 fw_iso_context_destroy(context
);
907 client
->iso_closure
= a
->closure
;
908 client
->iso_context
= context
;
909 spin_unlock_irq(&client
->lock
);
916 /* Macros for decoding the iso packet control header. */
917 #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
918 #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
919 #define GET_SKIP(v) (((v) >> 17) & 0x01)
920 #define GET_TAG(v) (((v) >> 18) & 0x03)
921 #define GET_SY(v) (((v) >> 20) & 0x0f)
922 #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
924 static int ioctl_queue_iso(struct client
*client
, union ioctl_arg
*arg
)
926 struct fw_cdev_queue_iso
*a
= &arg
->queue_iso
;
927 struct fw_cdev_iso_packet __user
*p
, *end
, *next
;
928 struct fw_iso_context
*ctx
= client
->iso_context
;
929 unsigned long payload
, buffer_end
, header_length
;
933 struct fw_iso_packet packet
;
937 if (ctx
== NULL
|| a
->handle
!= 0)
941 * If the user passes a non-NULL data pointer, has mmap()'ed
942 * the iso buffer, and the pointer points inside the buffer,
943 * we setup the payload pointers accordingly. Otherwise we
944 * set them both to 0, which will still let packets with
945 * payload_length == 0 through. In other words, if no packets
946 * use the indirect payload, the iso buffer need not be mapped
947 * and the a->data pointer is ignored.
950 payload
= (unsigned long)a
->data
- client
->vm_start
;
951 buffer_end
= client
->buffer
.page_count
<< PAGE_SHIFT
;
952 if (a
->data
== 0 || client
->buffer
.pages
== NULL
||
953 payload
>= buffer_end
) {
958 p
= (struct fw_cdev_iso_packet __user
*)u64_to_uptr(a
->packets
);
960 if (!access_ok(VERIFY_READ
, p
, a
->size
))
963 end
= (void __user
*)p
+ a
->size
;
966 if (get_user(control
, &p
->control
))
968 u
.packet
.payload_length
= GET_PAYLOAD_LENGTH(control
);
969 u
.packet
.interrupt
= GET_INTERRUPT(control
);
970 u
.packet
.skip
= GET_SKIP(control
);
971 u
.packet
.tag
= GET_TAG(control
);
972 u
.packet
.sy
= GET_SY(control
);
973 u
.packet
.header_length
= GET_HEADER_LENGTH(control
);
975 if (ctx
->type
== FW_ISO_CONTEXT_TRANSMIT
) {
976 if (u
.packet
.header_length
% 4 != 0)
978 header_length
= u
.packet
.header_length
;
981 * We require that header_length is a multiple of
982 * the fixed header size, ctx->header_size.
984 if (ctx
->header_size
== 0) {
985 if (u
.packet
.header_length
> 0)
987 } else if (u
.packet
.header_length
== 0 ||
988 u
.packet
.header_length
% ctx
->header_size
!= 0) {
994 next
= (struct fw_cdev_iso_packet __user
*)
995 &p
->header
[header_length
/ 4];
999 (u
.packet
.header
, p
->header
, header_length
))
1001 if (u
.packet
.skip
&& ctx
->type
== FW_ISO_CONTEXT_TRANSMIT
&&
1002 u
.packet
.header_length
+ u
.packet
.payload_length
> 0)
1004 if (payload
+ u
.packet
.payload_length
> buffer_end
)
1007 if (fw_iso_context_queue(ctx
, &u
.packet
,
1008 &client
->buffer
, payload
))
1012 payload
+= u
.packet
.payload_length
;
1016 a
->size
-= uptr_to_u64(p
) - a
->packets
;
1017 a
->packets
= uptr_to_u64(p
);
1018 a
->data
= client
->vm_start
+ payload
;
1023 static int ioctl_start_iso(struct client
*client
, union ioctl_arg
*arg
)
1025 struct fw_cdev_start_iso
*a
= &arg
->start_iso
;
1027 if (client
->iso_context
== NULL
|| a
->handle
!= 0)
1030 if (client
->iso_context
->type
== FW_ISO_CONTEXT_RECEIVE
&&
1031 (a
->tags
== 0 || a
->tags
> 15 || a
->sync
> 15))
1034 return fw_iso_context_start(client
->iso_context
,
1035 a
->cycle
, a
->sync
, a
->tags
);
1038 static int ioctl_stop_iso(struct client
*client
, union ioctl_arg
*arg
)
1040 struct fw_cdev_stop_iso
*a
= &arg
->stop_iso
;
1042 if (client
->iso_context
== NULL
|| a
->handle
!= 0)
1045 return fw_iso_context_stop(client
->iso_context
);
1048 static int ioctl_get_cycle_timer2(struct client
*client
, union ioctl_arg
*arg
)
1050 struct fw_cdev_get_cycle_timer2
*a
= &arg
->get_cycle_timer2
;
1051 struct fw_card
*card
= client
->device
->card
;
1052 struct timespec ts
= {0, 0};
1056 local_irq_disable();
1058 cycle_time
= card
->driver
->read_csr(card
, CSR_CYCLE_TIME
);
1060 switch (a
->clk_id
) {
1061 case CLOCK_REALTIME
: getnstimeofday(&ts
); break;
1062 case CLOCK_MONOTONIC
: do_posix_clock_monotonic_gettime(&ts
); break;
1063 case CLOCK_MONOTONIC_RAW
: getrawmonotonic(&ts
); break;
1070 a
->tv_sec
= ts
.tv_sec
;
1071 a
->tv_nsec
= ts
.tv_nsec
;
1072 a
->cycle_timer
= cycle_time
;
1077 static int ioctl_get_cycle_timer(struct client
*client
, union ioctl_arg
*arg
)
1079 struct fw_cdev_get_cycle_timer
*a
= &arg
->get_cycle_timer
;
1080 struct fw_cdev_get_cycle_timer2 ct2
;
1082 ct2
.clk_id
= CLOCK_REALTIME
;
1083 ioctl_get_cycle_timer2(client
, (union ioctl_arg
*)&ct2
);
1085 a
->local_time
= ct2
.tv_sec
* USEC_PER_SEC
+ ct2
.tv_nsec
/ NSEC_PER_USEC
;
1086 a
->cycle_timer
= ct2
.cycle_timer
;
1091 static void iso_resource_work(struct work_struct
*work
)
1093 struct iso_resource_event
*e
;
1094 struct iso_resource
*r
=
1095 container_of(work
, struct iso_resource
, work
.work
);
1096 struct client
*client
= r
->client
;
1097 int generation
, channel
, bandwidth
, todo
;
1098 bool skip
, free
, success
;
1100 spin_lock_irq(&client
->lock
);
1101 generation
= client
->device
->generation
;
1103 /* Allow 1000ms grace period for other reallocations. */
1104 if (todo
== ISO_RES_ALLOC
&&
1105 time_is_after_jiffies(client
->device
->card
->reset_jiffies
+ HZ
)) {
1106 schedule_iso_resource(r
, DIV_ROUND_UP(HZ
, 3));
1109 /* We could be called twice within the same generation. */
1110 skip
= todo
== ISO_RES_REALLOC
&&
1111 r
->generation
== generation
;
1113 free
= todo
== ISO_RES_DEALLOC
||
1114 todo
== ISO_RES_ALLOC_ONCE
||
1115 todo
== ISO_RES_DEALLOC_ONCE
;
1116 r
->generation
= generation
;
1117 spin_unlock_irq(&client
->lock
);
1122 bandwidth
= r
->bandwidth
;
1124 fw_iso_resource_manage(client
->device
->card
, generation
,
1125 r
->channels
, &channel
, &bandwidth
,
1126 todo
== ISO_RES_ALLOC
||
1127 todo
== ISO_RES_REALLOC
||
1128 todo
== ISO_RES_ALLOC_ONCE
,
1129 r
->transaction_data
);
1131 * Is this generation outdated already? As long as this resource sticks
1132 * in the idr, it will be scheduled again for a newer generation or at
1135 if (channel
== -EAGAIN
&&
1136 (todo
== ISO_RES_ALLOC
|| todo
== ISO_RES_REALLOC
))
1139 success
= channel
>= 0 || bandwidth
> 0;
1141 spin_lock_irq(&client
->lock
);
1143 * Transit from allocation to reallocation, except if the client
1144 * requested deallocation in the meantime.
1146 if (r
->todo
== ISO_RES_ALLOC
)
1147 r
->todo
= ISO_RES_REALLOC
;
1149 * Allocation or reallocation failure? Pull this resource out of the
1150 * idr and prepare for deletion, unless the client is shutting down.
1152 if (r
->todo
== ISO_RES_REALLOC
&& !success
&&
1153 !client
->in_shutdown
&&
1154 idr_find(&client
->resource_idr
, r
->resource
.handle
)) {
1155 idr_remove(&client
->resource_idr
, r
->resource
.handle
);
1159 spin_unlock_irq(&client
->lock
);
1161 if (todo
== ISO_RES_ALLOC
&& channel
>= 0)
1162 r
->channels
= 1ULL << channel
;
1164 if (todo
== ISO_RES_REALLOC
&& success
)
1167 if (todo
== ISO_RES_ALLOC
|| todo
== ISO_RES_ALLOC_ONCE
) {
1172 r
->e_dealloc
= NULL
;
1174 e
->iso_resource
.handle
= r
->resource
.handle
;
1175 e
->iso_resource
.channel
= channel
;
1176 e
->iso_resource
.bandwidth
= bandwidth
;
1178 queue_event(client
, &e
->event
,
1179 &e
->iso_resource
, sizeof(e
->iso_resource
), NULL
, 0);
1182 cancel_delayed_work(&r
->work
);
1184 kfree(r
->e_dealloc
);
1191 static void release_iso_resource(struct client
*client
,
1192 struct client_resource
*resource
)
1194 struct iso_resource
*r
=
1195 container_of(resource
, struct iso_resource
, resource
);
1197 spin_lock_irq(&client
->lock
);
1198 r
->todo
= ISO_RES_DEALLOC
;
1199 schedule_iso_resource(r
, 0);
1200 spin_unlock_irq(&client
->lock
);
1203 static int init_iso_resource(struct client
*client
,
1204 struct fw_cdev_allocate_iso_resource
*request
, int todo
)
1206 struct iso_resource_event
*e1
, *e2
;
1207 struct iso_resource
*r
;
1210 if ((request
->channels
== 0 && request
->bandwidth
== 0) ||
1211 request
->bandwidth
> BANDWIDTH_AVAILABLE_INITIAL
||
1212 request
->bandwidth
< 0)
1215 r
= kmalloc(sizeof(*r
), GFP_KERNEL
);
1216 e1
= kmalloc(sizeof(*e1
), GFP_KERNEL
);
1217 e2
= kmalloc(sizeof(*e2
), GFP_KERNEL
);
1218 if (r
== NULL
|| e1
== NULL
|| e2
== NULL
) {
1223 INIT_DELAYED_WORK(&r
->work
, iso_resource_work
);
1227 r
->channels
= request
->channels
;
1228 r
->bandwidth
= request
->bandwidth
;
1232 e1
->iso_resource
.closure
= request
->closure
;
1233 e1
->iso_resource
.type
= FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED
;
1234 e2
->iso_resource
.closure
= request
->closure
;
1235 e2
->iso_resource
.type
= FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED
;
1237 if (todo
== ISO_RES_ALLOC
) {
1238 r
->resource
.release
= release_iso_resource
;
1239 ret
= add_client_resource(client
, &r
->resource
, GFP_KERNEL
);
1243 r
->resource
.release
= NULL
;
1244 r
->resource
.handle
= -1;
1245 schedule_iso_resource(r
, 0);
1247 request
->handle
= r
->resource
.handle
;
1258 static int ioctl_allocate_iso_resource(struct client
*client
,
1259 union ioctl_arg
*arg
)
1261 return init_iso_resource(client
,
1262 &arg
->allocate_iso_resource
, ISO_RES_ALLOC
);
1265 static int ioctl_deallocate_iso_resource(struct client
*client
,
1266 union ioctl_arg
*arg
)
1268 return release_client_resource(client
,
1269 arg
->deallocate
.handle
, release_iso_resource
, NULL
);
1272 static int ioctl_allocate_iso_resource_once(struct client
*client
,
1273 union ioctl_arg
*arg
)
1275 return init_iso_resource(client
,
1276 &arg
->allocate_iso_resource
, ISO_RES_ALLOC_ONCE
);
1279 static int ioctl_deallocate_iso_resource_once(struct client
*client
,
1280 union ioctl_arg
*arg
)
1282 return init_iso_resource(client
,
1283 &arg
->allocate_iso_resource
, ISO_RES_DEALLOC_ONCE
);
1287 * Returns a speed code: Maximum speed to or from this device,
1288 * limited by the device's link speed, the local node's link speed,
1289 * and all PHY port speeds between the two links.
1291 static int ioctl_get_speed(struct client
*client
, union ioctl_arg
*arg
)
1293 return client
->device
->max_speed
;
1296 static int ioctl_send_broadcast_request(struct client
*client
,
1297 union ioctl_arg
*arg
)
1299 struct fw_cdev_send_request
*a
= &arg
->send_request
;
1302 case TCODE_WRITE_QUADLET_REQUEST
:
1303 case TCODE_WRITE_BLOCK_REQUEST
:
1309 /* Security policy: Only allow accesses to Units Space. */
1310 if (a
->offset
< CSR_REGISTER_BASE
+ CSR_CONFIG_ROM_END
)
1313 return init_request(client
, a
, LOCAL_BUS
| 0x3f, SCODE_100
);
1316 static int ioctl_send_stream_packet(struct client
*client
, union ioctl_arg
*arg
)
1318 struct fw_cdev_send_stream_packet
*a
= &arg
->send_stream_packet
;
1319 struct fw_cdev_send_request request
;
1322 if (a
->speed
> client
->device
->card
->link_speed
||
1323 a
->length
> 1024 << a
->speed
)
1326 if (a
->tag
> 3 || a
->channel
> 63 || a
->sy
> 15)
1329 dest
= fw_stream_packet_destination_id(a
->tag
, a
->channel
, a
->sy
);
1330 request
.tcode
= TCODE_STREAM_DATA
;
1331 request
.length
= a
->length
;
1332 request
.closure
= a
->closure
;
1333 request
.data
= a
->data
;
1334 request
.generation
= a
->generation
;
1336 return init_request(client
, &request
, dest
, a
->speed
);
1339 static int (* const ioctl_handlers
[])(struct client
*, union ioctl_arg
*) = {
1344 ioctl_send_response
,
1345 ioctl_initiate_bus_reset
,
1346 ioctl_add_descriptor
,
1347 ioctl_remove_descriptor
,
1348 ioctl_create_iso_context
,
1352 ioctl_get_cycle_timer
,
1353 ioctl_allocate_iso_resource
,
1354 ioctl_deallocate_iso_resource
,
1355 ioctl_allocate_iso_resource_once
,
1356 ioctl_deallocate_iso_resource_once
,
1358 ioctl_send_broadcast_request
,
1359 ioctl_send_stream_packet
,
1360 ioctl_get_cycle_timer2
,
1363 static int dispatch_ioctl(struct client
*client
,
1364 unsigned int cmd
, void __user
*arg
)
1366 union ioctl_arg buffer
;
1369 if (fw_device_is_shutdown(client
->device
))
1372 if (_IOC_TYPE(cmd
) != '#' ||
1373 _IOC_NR(cmd
) >= ARRAY_SIZE(ioctl_handlers
) ||
1374 _IOC_SIZE(cmd
) > sizeof(buffer
))
1377 if (_IOC_DIR(cmd
) == _IOC_READ
)
1378 memset(&buffer
, 0, _IOC_SIZE(cmd
));
1380 if (_IOC_DIR(cmd
) & _IOC_WRITE
)
1381 if (copy_from_user(&buffer
, arg
, _IOC_SIZE(cmd
)))
1384 ret
= ioctl_handlers
[_IOC_NR(cmd
)](client
, &buffer
);
1388 if (_IOC_DIR(cmd
) & _IOC_READ
)
1389 if (copy_to_user(arg
, &buffer
, _IOC_SIZE(cmd
)))
1395 static long fw_device_op_ioctl(struct file
*file
,
1396 unsigned int cmd
, unsigned long arg
)
1398 return dispatch_ioctl(file
->private_data
, cmd
, (void __user
*)arg
);
1401 #ifdef CONFIG_COMPAT
1402 static long fw_device_op_compat_ioctl(struct file
*file
,
1403 unsigned int cmd
, unsigned long arg
)
1405 return dispatch_ioctl(file
->private_data
, cmd
, compat_ptr(arg
));
1409 static int fw_device_op_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1411 struct client
*client
= file
->private_data
;
1412 enum dma_data_direction direction
;
1414 int page_count
, ret
;
1416 if (fw_device_is_shutdown(client
->device
))
1419 /* FIXME: We could support multiple buffers, but we don't. */
1420 if (client
->buffer
.pages
!= NULL
)
1423 if (!(vma
->vm_flags
& VM_SHARED
))
1426 if (vma
->vm_start
& ~PAGE_MASK
)
1429 client
->vm_start
= vma
->vm_start
;
1430 size
= vma
->vm_end
- vma
->vm_start
;
1431 page_count
= size
>> PAGE_SHIFT
;
1432 if (size
& ~PAGE_MASK
)
1435 if (vma
->vm_flags
& VM_WRITE
)
1436 direction
= DMA_TO_DEVICE
;
1438 direction
= DMA_FROM_DEVICE
;
1440 ret
= fw_iso_buffer_init(&client
->buffer
, client
->device
->card
,
1441 page_count
, direction
);
1445 ret
= fw_iso_buffer_map(&client
->buffer
, vma
);
1447 fw_iso_buffer_destroy(&client
->buffer
, client
->device
->card
);
1452 static int shutdown_resource(int id
, void *p
, void *data
)
1454 struct client_resource
*resource
= p
;
1455 struct client
*client
= data
;
1457 resource
->release(client
, resource
);
1463 static int fw_device_op_release(struct inode
*inode
, struct file
*file
)
1465 struct client
*client
= file
->private_data
;
1466 struct event
*event
, *next_event
;
1468 mutex_lock(&client
->device
->client_list_mutex
);
1469 list_del(&client
->link
);
1470 mutex_unlock(&client
->device
->client_list_mutex
);
1472 if (client
->iso_context
)
1473 fw_iso_context_destroy(client
->iso_context
);
1475 if (client
->buffer
.pages
)
1476 fw_iso_buffer_destroy(&client
->buffer
, client
->device
->card
);
1478 /* Freeze client->resource_idr and client->event_list */
1479 spin_lock_irq(&client
->lock
);
1480 client
->in_shutdown
= true;
1481 spin_unlock_irq(&client
->lock
);
1483 idr_for_each(&client
->resource_idr
, shutdown_resource
, client
);
1484 idr_remove_all(&client
->resource_idr
);
1485 idr_destroy(&client
->resource_idr
);
1487 list_for_each_entry_safe(event
, next_event
, &client
->event_list
, link
)
1495 static unsigned int fw_device_op_poll(struct file
*file
, poll_table
* pt
)
1497 struct client
*client
= file
->private_data
;
1498 unsigned int mask
= 0;
1500 poll_wait(file
, &client
->wait
, pt
);
1502 if (fw_device_is_shutdown(client
->device
))
1503 mask
|= POLLHUP
| POLLERR
;
1504 if (!list_empty(&client
->event_list
))
1505 mask
|= POLLIN
| POLLRDNORM
;
1510 const struct file_operations fw_device_ops
= {
1511 .owner
= THIS_MODULE
,
1512 .llseek
= no_llseek
,
1513 .open
= fw_device_op_open
,
1514 .read
= fw_device_op_read
,
1515 .unlocked_ioctl
= fw_device_op_ioctl
,
1516 .mmap
= fw_device_op_mmap
,
1517 .release
= fw_device_op_release
,
1518 .poll
= fw_device_op_poll
,
1519 #ifdef CONFIG_COMPAT
1520 .compat_ioctl
= fw_device_op_compat_ioctl
,