2 * Function Control Protocol (IEC 61883-1) helper functions
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Licensed under the terms of the GNU General Public License, version 2.
8 #include <linux/device.h>
9 #include <linux/firewire.h>
10 #include <linux/firewire-constants.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/spinlock.h>
15 #include <linux/wait.h>
21 #define ERROR_RETRIES 3
22 #define ERROR_DELAY_MS 5
23 #define FCP_TIMEOUT_MS 125
25 static DEFINE_SPINLOCK(transactions_lock
);
26 static LIST_HEAD(transactions
);
34 struct fcp_transaction
{
35 struct list_head list
;
37 void *response_buffer
;
38 unsigned int response_size
;
39 unsigned int response_match_bytes
;
41 wait_queue_head_t wait
;
45 * fcp_avc_transaction - send an AV/C command and wait for its response
46 * @unit: a unit on the target device
47 * @command: a buffer containing the command frame; must be DMA-able
48 * @command_size: the size of @command
49 * @response: a buffer for the response frame
50 * @response_size: the maximum size of @response
51 * @response_match_bytes: a bitmap specifying the bytes used to detect the
52 * correct response frame
54 * This function sends a FCP command frame to the target and waits for the
55 * corresponding response frame to be returned.
57 * Because it is possible for multiple FCP transactions to be active at the
58 * same time, the correct response frame is detected by the value of certain
59 * bytes. These bytes must be set in @response before calling this function,
60 * and the corresponding bits must be set in @response_match_bytes.
62 * @command and @response can point to the same buffer.
64 * Asynchronous operation (INTERIM, NOTIFY) is not supported at the moment.
66 * Returns the actual size of the response frame, or a negative error code.
68 int fcp_avc_transaction(struct fw_unit
*unit
,
69 const void *command
, unsigned int command_size
,
70 void *response
, unsigned int response_size
,
71 unsigned int response_match_bytes
)
73 struct fcp_transaction t
;
74 int tcode
, ret
, tries
= 0;
77 t
.response_buffer
= response
;
78 t
.response_size
= response_size
;
79 t
.response_match_bytes
= response_match_bytes
;
80 t
.state
= STATE_PENDING
;
81 init_waitqueue_head(&t
.wait
);
83 spin_lock_irq(&transactions_lock
);
84 list_add_tail(&t
.list
, &transactions
);
85 spin_unlock_irq(&transactions_lock
);
88 tcode
= command_size
== 4 ? TCODE_WRITE_QUADLET_REQUEST
89 : TCODE_WRITE_BLOCK_REQUEST
;
90 ret
= snd_fw_transaction(t
.unit
, tcode
,
91 CSR_REGISTER_BASE
+ CSR_FCP_COMMAND
,
92 (void *)command
, command_size
);
96 wait_event_timeout(t
.wait
, t
.state
!= STATE_PENDING
,
97 msecs_to_jiffies(FCP_TIMEOUT_MS
));
99 if (t
.state
== STATE_COMPLETE
) {
100 ret
= t
.response_size
;
102 } else if (t
.state
== STATE_BUS_RESET
) {
103 msleep(ERROR_DELAY_MS
);
104 } else if (++tries
>= ERROR_RETRIES
) {
105 dev_err(&t
.unit
->device
, "FCP command timed out\n");
111 spin_lock_irq(&transactions_lock
);
113 spin_unlock_irq(&transactions_lock
);
117 EXPORT_SYMBOL(fcp_avc_transaction
);
120 * fcp_bus_reset - inform the target handler about a bus reset
121 * @unit: the unit that might be used by fcp_avc_transaction()
123 * This function must be called from the driver's .update handler to inform
124 * the FCP transaction handler that a bus reset has happened. Any pending FCP
125 * transactions are retried.
127 void fcp_bus_reset(struct fw_unit
*unit
)
129 struct fcp_transaction
*t
;
131 spin_lock_irq(&transactions_lock
);
132 list_for_each_entry(t
, &transactions
, list
) {
133 if (t
->unit
== unit
&&
134 t
->state
== STATE_PENDING
) {
135 t
->state
= STATE_BUS_RESET
;
139 spin_unlock_irq(&transactions_lock
);
141 EXPORT_SYMBOL(fcp_bus_reset
);
143 /* checks whether the response matches the masked bytes in response_buffer */
144 static bool is_matching_response(struct fcp_transaction
*transaction
,
145 const void *response
, size_t length
)
148 unsigned int mask
, i
;
151 p2
= transaction
->response_buffer
;
152 mask
= transaction
->response_match_bytes
;
155 if ((mask
& 1) && p1
[i
] != p2
[i
])
165 static void fcp_response(struct fw_card
*card
, struct fw_request
*request
,
166 int tcode
, int destination
, int source
,
167 int generation
, unsigned long long offset
,
168 void *data
, size_t length
, void *callback_data
)
170 struct fcp_transaction
*t
;
173 if (length
< 1 || (*(const u8
*)data
& 0xf0) != CTS_AVC
)
176 spin_lock_irqsave(&transactions_lock
, flags
);
177 list_for_each_entry(t
, &transactions
, list
) {
178 struct fw_device
*device
= fw_parent_device(t
->unit
);
179 if (device
->card
!= card
||
180 device
->generation
!= generation
)
182 smp_rmb(); /* node_id vs. generation */
183 if (device
->node_id
!= source
)
186 if (t
->state
== STATE_PENDING
&&
187 is_matching_response(t
, data
, length
)) {
188 t
->state
= STATE_COMPLETE
;
189 t
->response_size
= min((unsigned int)length
,
191 memcpy(t
->response_buffer
, data
, t
->response_size
);
195 spin_unlock_irqrestore(&transactions_lock
, flags
);
198 static struct fw_address_handler response_register_handler
= {
200 .address_callback
= fcp_response
,
203 static int __init
fcp_module_init(void)
205 static const struct fw_address_region response_register_region
= {
206 .start
= CSR_REGISTER_BASE
+ CSR_FCP_RESPONSE
,
207 .end
= CSR_REGISTER_BASE
+ CSR_FCP_END
,
210 fw_core_add_address_handler(&response_register_handler
,
211 &response_register_region
);
216 static void __exit
fcp_module_exit(void)
218 WARN_ON(!list_empty(&transactions
));
219 fw_core_remove_address_handler(&response_register_handler
);
222 module_init(fcp_module_init
);
223 module_exit(fcp_module_exit
);