18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * miscellaneous helper functions 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) Clemens Ladisch <clemens@ladisch.de> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/delay.h> 98c2ecf20Sopenharmony_ci#include <linux/device.h> 108c2ecf20Sopenharmony_ci#include <linux/firewire.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/slab.h> 138c2ecf20Sopenharmony_ci#include "lib.h" 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#define ERROR_RETRY_DELAY_MS 20 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci/** 188c2ecf20Sopenharmony_ci * snd_fw_transaction - send a request and wait for its completion 198c2ecf20Sopenharmony_ci * @unit: the driver's unit on the target device 208c2ecf20Sopenharmony_ci * @tcode: the transaction code 218c2ecf20Sopenharmony_ci * @offset: the address in the target's address space 228c2ecf20Sopenharmony_ci * @buffer: input/output data 238c2ecf20Sopenharmony_ci * @length: length of @buffer 248c2ecf20Sopenharmony_ci * @flags: use %FW_FIXED_GENERATION and add the generation value to attempt the 258c2ecf20Sopenharmony_ci * request only in that generation; use %FW_QUIET to suppress error 268c2ecf20Sopenharmony_ci * messages 278c2ecf20Sopenharmony_ci * 288c2ecf20Sopenharmony_ci * Submits an asynchronous request to the target device, and waits for the 298c2ecf20Sopenharmony_ci * response. The node ID and the current generation are derived from @unit. 308c2ecf20Sopenharmony_ci * On a bus reset or an error, the transaction is retried a few times. 318c2ecf20Sopenharmony_ci * Returns zero on success, or a negative error code. 328c2ecf20Sopenharmony_ci */ 338c2ecf20Sopenharmony_ciint snd_fw_transaction(struct fw_unit *unit, int tcode, 348c2ecf20Sopenharmony_ci u64 offset, void *buffer, size_t length, 358c2ecf20Sopenharmony_ci unsigned int flags) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci struct fw_device *device = fw_parent_device(unit); 388c2ecf20Sopenharmony_ci int generation, rcode, tries = 0; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci generation = flags & FW_GENERATION_MASK; 418c2ecf20Sopenharmony_ci for (;;) { 428c2ecf20Sopenharmony_ci if (!(flags & FW_FIXED_GENERATION)) { 438c2ecf20Sopenharmony_ci generation = device->generation; 448c2ecf20Sopenharmony_ci smp_rmb(); /* node_id vs. generation */ 458c2ecf20Sopenharmony_ci } 468c2ecf20Sopenharmony_ci rcode = fw_run_transaction(device->card, tcode, 478c2ecf20Sopenharmony_ci device->node_id, generation, 488c2ecf20Sopenharmony_ci device->max_speed, offset, 498c2ecf20Sopenharmony_ci buffer, length); 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci if (rcode == RCODE_COMPLETE) 528c2ecf20Sopenharmony_ci return 0; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci if (rcode == RCODE_GENERATION && (flags & FW_FIXED_GENERATION)) 558c2ecf20Sopenharmony_ci return -EAGAIN; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci if (rcode_is_permanent_error(rcode) || ++tries >= 3) { 588c2ecf20Sopenharmony_ci if (!(flags & FW_QUIET)) 598c2ecf20Sopenharmony_ci dev_err(&unit->device, 608c2ecf20Sopenharmony_ci "transaction failed: %s\n", 618c2ecf20Sopenharmony_ci fw_rcode_string(rcode)); 628c2ecf20Sopenharmony_ci return -EIO; 638c2ecf20Sopenharmony_ci } 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci msleep(ERROR_RETRY_DELAY_MS); 668c2ecf20Sopenharmony_ci } 678c2ecf20Sopenharmony_ci} 688c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_fw_transaction); 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci#define PROBE_DELAY_MS (2 * MSEC_PER_SEC) 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci/** 738c2ecf20Sopenharmony_ci * snd_fw_schedule_registration - schedule work for sound card registration 748c2ecf20Sopenharmony_ci * @unit: an instance for unit on IEEE 1394 bus 758c2ecf20Sopenharmony_ci * @dwork: delayed work with callback function 768c2ecf20Sopenharmony_ci * 778c2ecf20Sopenharmony_ci * This function is not designed for general purposes. When new unit is 788c2ecf20Sopenharmony_ci * connected to IEEE 1394 bus, the bus is under bus-reset state because of 798c2ecf20Sopenharmony_ci * topological change. In this state, units tend to fail both of asynchronous 808c2ecf20Sopenharmony_ci * and isochronous communication. To avoid this problem, this function is used 818c2ecf20Sopenharmony_ci * to postpone sound card registration after the state. The callers must 828c2ecf20Sopenharmony_ci * set up instance of delayed work in advance. 838c2ecf20Sopenharmony_ci */ 848c2ecf20Sopenharmony_civoid snd_fw_schedule_registration(struct fw_unit *unit, 858c2ecf20Sopenharmony_ci struct delayed_work *dwork) 868c2ecf20Sopenharmony_ci{ 878c2ecf20Sopenharmony_ci u64 now, delay; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci now = get_jiffies_64(); 908c2ecf20Sopenharmony_ci delay = fw_parent_device(unit)->card->reset_jiffies 918c2ecf20Sopenharmony_ci + msecs_to_jiffies(PROBE_DELAY_MS); 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci if (time_after64(delay, now)) 948c2ecf20Sopenharmony_ci delay -= now; 958c2ecf20Sopenharmony_ci else 968c2ecf20Sopenharmony_ci delay = 0; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci mod_delayed_work(system_wq, dwork, delay); 998c2ecf20Sopenharmony_ci} 1008c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_fw_schedule_registration); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("FireWire audio helper functions"); 1038c2ecf20Sopenharmony_ciMODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); 1048c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 105