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/slab.h>
14 #include <linux/sched.h>
15 #include <linux/spinlock.h>
16 #include <linux/wait.h>
17 #include <linux/delay.h>
20 #include "amdtp-stream.h"
24 #define ERROR_RETRIES 3
25 #define ERROR_DELAY_MS 5
26 #define FCP_TIMEOUT_MS 125
28 int avc_general_set_sig_fmt(struct fw_unit
*unit
, unsigned int rate
,
29 enum avc_general_plug_dir dir
,
38 for (sfc
= 0; sfc
< CIP_SFC_COUNT
; sfc
++) {
39 if (amdtp_rate_table
[sfc
] == rate
) {
47 buf
= kzalloc(8, GFP_KERNEL
);
51 buf
[0] = 0x00; /* AV/C CONTROL */
52 buf
[1] = 0xff; /* UNIT */
53 if (dir
== AVC_GENERAL_PLUG_DIR_IN
)
54 buf
[2] = 0x19; /* INPUT PLUG SIGNAL FORMAT */
56 buf
[2] = 0x18; /* OUTPUT PLUG SIGNAL FORMAT */
57 buf
[3] = 0xff & pid
; /* plug id */
58 buf
[4] = 0x90; /* EOH_1, Form_1, FMT. AM824 */
59 buf
[5] = 0x07 & sfc
; /* FDF-hi. AM824, frequency */
60 buf
[6] = 0xff; /* FDF-mid. AM824, SYT hi (not used)*/
61 buf
[7] = 0xff; /* FDF-low. AM824, SYT lo (not used) */
63 /* do transaction and check buf[1-5] are the same against command */
64 err
= fcp_avc_transaction(unit
, buf
, 8, buf
, 8,
65 BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5));
66 if (err
>= 0 && err
< 8)
68 else if (buf
[0] == 0x08) /* NOT IMPLEMENTED */
70 else if (buf
[0] == 0x0a) /* REJECTED */
80 EXPORT_SYMBOL(avc_general_set_sig_fmt
);
82 int avc_general_get_sig_fmt(struct fw_unit
*unit
, unsigned int *rate
,
83 enum avc_general_plug_dir dir
,
90 buf
= kzalloc(8, GFP_KERNEL
);
94 buf
[0] = 0x01; /* AV/C STATUS */
95 buf
[1] = 0xff; /* Unit */
96 if (dir
== AVC_GENERAL_PLUG_DIR_IN
)
97 buf
[2] = 0x19; /* INPUT PLUG SIGNAL FORMAT */
99 buf
[2] = 0x18; /* OUTPUT PLUG SIGNAL FORMAT */
100 buf
[3] = 0xff & pid
; /* plug id */
101 buf
[4] = 0x90; /* EOH_1, Form_1, FMT. AM824 */
102 buf
[5] = 0xff; /* FDF-hi. AM824, frequency */
103 buf
[6] = 0xff; /* FDF-mid. AM824, SYT hi (not used) */
104 buf
[7] = 0xff; /* FDF-low. AM824, SYT lo (not used) */
106 /* do transaction and check buf[1-4] are the same against command */
107 err
= fcp_avc_transaction(unit
, buf
, 8, buf
, 8,
108 BIT(1) | BIT(2) | BIT(3) | BIT(4));
109 if (err
>= 0 && err
< 8)
111 else if (buf
[0] == 0x08) /* NOT IMPLEMENTED */
113 else if (buf
[0] == 0x0a) /* REJECTED */
115 else if (buf
[0] == 0x0b) /* IN TRANSITION */
120 /* check sfc field and pick up rate */
122 if (sfc
>= CIP_SFC_COUNT
) {
123 err
= -EAGAIN
; /* also in transition */
127 *rate
= amdtp_rate_table
[sfc
];
133 EXPORT_SYMBOL(avc_general_get_sig_fmt
);
135 int avc_general_get_plug_info(struct fw_unit
*unit
, unsigned int subunit_type
,
136 unsigned int subunit_id
, unsigned int subfunction
,
137 u8 info
[AVC_PLUG_INFO_BUF_BYTES
])
142 /* extended subunit in spec.4.2 is not supported */
143 if ((subunit_type
== 0x1E) || (subunit_id
== 5))
146 buf
= kzalloc(8, GFP_KERNEL
);
150 buf
[0] = 0x01; /* AV/C STATUS */
151 /* UNIT or Subunit, Functionblock */
152 buf
[1] = ((subunit_type
& 0x1f) << 3) | (subunit_id
& 0x7);
153 buf
[2] = 0x02; /* PLUG INFO */
154 buf
[3] = 0xff & subfunction
;
156 err
= fcp_avc_transaction(unit
, buf
, 8, buf
, 8, BIT(1) | BIT(2));
157 if (err
>= 0 && err
< 8)
159 else if (buf
[0] == 0x08) /* NOT IMPLEMENTED */
161 else if (buf
[0] == 0x0a) /* REJECTED */
163 else if (buf
[0] == 0x0b) /* IN TRANSITION */
178 EXPORT_SYMBOL(avc_general_get_plug_info
);
180 static DEFINE_SPINLOCK(transactions_lock
);
181 static LIST_HEAD(transactions
);
190 struct fcp_transaction
{
191 struct list_head list
;
192 struct fw_unit
*unit
;
193 void *response_buffer
;
194 unsigned int response_size
;
195 unsigned int response_match_bytes
;
196 enum fcp_state state
;
197 wait_queue_head_t wait
;
202 * fcp_avc_transaction - send an AV/C command and wait for its response
203 * @unit: a unit on the target device
204 * @command: a buffer containing the command frame; must be DMA-able
205 * @command_size: the size of @command
206 * @response: a buffer for the response frame
207 * @response_size: the maximum size of @response
208 * @response_match_bytes: a bitmap specifying the bytes used to detect the
209 * correct response frame
211 * This function sends a FCP command frame to the target and waits for the
212 * corresponding response frame to be returned.
214 * Because it is possible for multiple FCP transactions to be active at the
215 * same time, the correct response frame is detected by the value of certain
216 * bytes. These bytes must be set in @response before calling this function,
217 * and the corresponding bits must be set in @response_match_bytes.
219 * @command and @response can point to the same buffer.
221 * Returns the actual size of the response frame, or a negative error code.
223 int fcp_avc_transaction(struct fw_unit
*unit
,
224 const void *command
, unsigned int command_size
,
225 void *response
, unsigned int response_size
,
226 unsigned int response_match_bytes
)
228 struct fcp_transaction t
;
229 int tcode
, ret
, tries
= 0;
232 t
.response_buffer
= response
;
233 t
.response_size
= response_size
;
234 t
.response_match_bytes
= response_match_bytes
;
235 t
.state
= STATE_PENDING
;
236 init_waitqueue_head(&t
.wait
);
238 if (*(const u8
*)command
== 0x00 || *(const u8
*)command
== 0x03)
241 spin_lock_irq(&transactions_lock
);
242 list_add_tail(&t
.list
, &transactions
);
243 spin_unlock_irq(&transactions_lock
);
246 tcode
= command_size
== 4 ? TCODE_WRITE_QUADLET_REQUEST
247 : TCODE_WRITE_BLOCK_REQUEST
;
248 ret
= snd_fw_transaction(t
.unit
, tcode
,
249 CSR_REGISTER_BASE
+ CSR_FCP_COMMAND
,
250 (void *)command
, command_size
, 0);
254 wait_event_timeout(t
.wait
, t
.state
!= STATE_PENDING
,
255 msecs_to_jiffies(FCP_TIMEOUT_MS
));
257 if (t
.state
== STATE_DEFERRED
) {
259 * 'AV/C General Specification' define no time limit
260 * on command completion once an INTERIM response has
261 * been sent. but we promise to finish this function
262 * for a caller. Here we use FCP_TIMEOUT_MS for next
263 * interval. This is not in the specification.
265 t
.state
= STATE_PENDING
;
267 } else if (t
.state
== STATE_COMPLETE
) {
268 ret
= t
.response_size
;
270 } else if (t
.state
== STATE_BUS_RESET
) {
271 msleep(ERROR_DELAY_MS
);
272 } else if (++tries
>= ERROR_RETRIES
) {
273 dev_err(&t
.unit
->device
, "FCP command timed out\n");
279 spin_lock_irq(&transactions_lock
);
281 spin_unlock_irq(&transactions_lock
);
285 EXPORT_SYMBOL(fcp_avc_transaction
);
288 * fcp_bus_reset - inform the target handler about a bus reset
289 * @unit: the unit that might be used by fcp_avc_transaction()
291 * This function must be called from the driver's .update handler to inform
292 * the FCP transaction handler that a bus reset has happened. Any pending FCP
293 * transactions are retried.
295 void fcp_bus_reset(struct fw_unit
*unit
)
297 struct fcp_transaction
*t
;
299 spin_lock_irq(&transactions_lock
);
300 list_for_each_entry(t
, &transactions
, list
) {
301 if (t
->unit
== unit
&&
302 (t
->state
== STATE_PENDING
||
303 t
->state
== STATE_DEFERRED
)) {
304 t
->state
= STATE_BUS_RESET
;
308 spin_unlock_irq(&transactions_lock
);
310 EXPORT_SYMBOL(fcp_bus_reset
);
312 /* checks whether the response matches the masked bytes in response_buffer */
313 static bool is_matching_response(struct fcp_transaction
*transaction
,
314 const void *response
, size_t length
)
317 unsigned int mask
, i
;
320 p2
= transaction
->response_buffer
;
321 mask
= transaction
->response_match_bytes
;
324 if ((mask
& 1) && p1
[i
] != p2
[i
])
334 static void fcp_response(struct fw_card
*card
, struct fw_request
*request
,
335 int tcode
, int destination
, int source
,
336 int generation
, unsigned long long offset
,
337 void *data
, size_t length
, void *callback_data
)
339 struct fcp_transaction
*t
;
342 if (length
< 1 || (*(const u8
*)data
& 0xf0) != CTS_AVC
)
345 spin_lock_irqsave(&transactions_lock
, flags
);
346 list_for_each_entry(t
, &transactions
, list
) {
347 struct fw_device
*device
= fw_parent_device(t
->unit
);
348 if (device
->card
!= card
||
349 device
->generation
!= generation
)
351 smp_rmb(); /* node_id vs. generation */
352 if (device
->node_id
!= source
)
355 if (t
->state
== STATE_PENDING
&&
356 is_matching_response(t
, data
, length
)) {
357 if (t
->deferrable
&& *(const u8
*)data
== 0x0f) {
358 t
->state
= STATE_DEFERRED
;
360 t
->state
= STATE_COMPLETE
;
361 t
->response_size
= min_t(unsigned int, length
,
363 memcpy(t
->response_buffer
, data
,
369 spin_unlock_irqrestore(&transactions_lock
, flags
);
372 static struct fw_address_handler response_register_handler
= {
374 .address_callback
= fcp_response
,
377 static int __init
fcp_module_init(void)
379 static const struct fw_address_region response_register_region
= {
380 .start
= CSR_REGISTER_BASE
+ CSR_FCP_RESPONSE
,
381 .end
= CSR_REGISTER_BASE
+ CSR_FCP_END
,
384 fw_core_add_address_handler(&response_register_handler
,
385 &response_register_region
);
390 static void __exit
fcp_module_exit(void)
392 WARN_ON(!list_empty(&transactions
));
393 fw_core_remove_address_handler(&response_register_handler
);
396 module_init(fcp_module_init
);
397 module_exit(fcp_module_exit
);