18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * bebob.h - a part of driver for BeBoB based devices
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2013-2014 Takashi Sakamoto
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef SOUND_BEBOB_H_INCLUDED
98c2ecf20Sopenharmony_ci#define SOUND_BEBOB_H_INCLUDED
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/compat.h>
128c2ecf20Sopenharmony_ci#include <linux/device.h>
138c2ecf20Sopenharmony_ci#include <linux/firewire.h>
148c2ecf20Sopenharmony_ci#include <linux/firewire-constants.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h>
178c2ecf20Sopenharmony_ci#include <linux/delay.h>
188c2ecf20Sopenharmony_ci#include <linux/slab.h>
198c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <sound/core.h>
228c2ecf20Sopenharmony_ci#include <sound/initval.h>
238c2ecf20Sopenharmony_ci#include <sound/info.h>
248c2ecf20Sopenharmony_ci#include <sound/rawmidi.h>
258c2ecf20Sopenharmony_ci#include <sound/pcm.h>
268c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
278c2ecf20Sopenharmony_ci#include <sound/firewire.h>
288c2ecf20Sopenharmony_ci#include <sound/hwdep.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include "../lib.h"
318c2ecf20Sopenharmony_ci#include "../fcp.h"
328c2ecf20Sopenharmony_ci#include "../packets-buffer.h"
338c2ecf20Sopenharmony_ci#include "../iso-resources.h"
348c2ecf20Sopenharmony_ci#include "../amdtp-am824.h"
358c2ecf20Sopenharmony_ci#include "../cmp.h"
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* basic register addresses on DM1000/DM1100/DM1500 */
388c2ecf20Sopenharmony_ci#define BEBOB_ADDR_REG_INFO	0xffffc8020000ULL
398c2ecf20Sopenharmony_ci#define BEBOB_ADDR_REG_REQ	0xffffc8021000ULL
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistruct snd_bebob;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define SND_BEBOB_STRM_FMT_ENTRIES	7
448c2ecf20Sopenharmony_cistruct snd_bebob_stream_formation {
458c2ecf20Sopenharmony_ci	unsigned int pcm;
468c2ecf20Sopenharmony_ci	unsigned int midi;
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci/* this is a lookup table for index of stream formations */
498c2ecf20Sopenharmony_ciextern const unsigned int snd_bebob_rate_table[SND_BEBOB_STRM_FMT_ENTRIES];
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* device specific operations */
528c2ecf20Sopenharmony_cienum snd_bebob_clock_type {
538c2ecf20Sopenharmony_ci	SND_BEBOB_CLOCK_TYPE_INTERNAL = 0,
548c2ecf20Sopenharmony_ci	SND_BEBOB_CLOCK_TYPE_EXTERNAL,
558c2ecf20Sopenharmony_ci	SND_BEBOB_CLOCK_TYPE_SYT,
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_cistruct snd_bebob_clock_spec {
588c2ecf20Sopenharmony_ci	unsigned int num;
598c2ecf20Sopenharmony_ci	const char *const *labels;
608c2ecf20Sopenharmony_ci	const enum snd_bebob_clock_type *types;
618c2ecf20Sopenharmony_ci	int (*get)(struct snd_bebob *bebob, unsigned int *id);
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_cistruct snd_bebob_rate_spec {
648c2ecf20Sopenharmony_ci	int (*get)(struct snd_bebob *bebob, unsigned int *rate);
658c2ecf20Sopenharmony_ci	int (*set)(struct snd_bebob *bebob, unsigned int rate);
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_cistruct snd_bebob_meter_spec {
688c2ecf20Sopenharmony_ci	unsigned int num;
698c2ecf20Sopenharmony_ci	const char *const *labels;
708c2ecf20Sopenharmony_ci	int (*get)(struct snd_bebob *bebob, u32 *target, unsigned int size);
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_cistruct snd_bebob_spec {
738c2ecf20Sopenharmony_ci	const struct snd_bebob_clock_spec *clock;
748c2ecf20Sopenharmony_ci	const struct snd_bebob_rate_spec *rate;
758c2ecf20Sopenharmony_ci	const struct snd_bebob_meter_spec *meter;
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistruct snd_bebob {
798c2ecf20Sopenharmony_ci	struct snd_card *card;
808c2ecf20Sopenharmony_ci	struct fw_unit *unit;
818c2ecf20Sopenharmony_ci	int card_index;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	struct mutex mutex;
848c2ecf20Sopenharmony_ci	spinlock_t lock;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	bool registered;
878c2ecf20Sopenharmony_ci	struct delayed_work dwork;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	const struct ieee1394_device_id *entry;
908c2ecf20Sopenharmony_ci	const struct snd_bebob_spec *spec;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	unsigned int midi_input_ports;
938c2ecf20Sopenharmony_ci	unsigned int midi_output_ports;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	struct amdtp_stream tx_stream;
968c2ecf20Sopenharmony_ci	struct amdtp_stream rx_stream;
978c2ecf20Sopenharmony_ci	struct cmp_connection out_conn;
988c2ecf20Sopenharmony_ci	struct cmp_connection in_conn;
998c2ecf20Sopenharmony_ci	unsigned int substreams_counter;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	struct snd_bebob_stream_formation
1028c2ecf20Sopenharmony_ci		tx_stream_formations[SND_BEBOB_STRM_FMT_ENTRIES];
1038c2ecf20Sopenharmony_ci	struct snd_bebob_stream_formation
1048c2ecf20Sopenharmony_ci		rx_stream_formations[SND_BEBOB_STRM_FMT_ENTRIES];
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	int sync_input_plug;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	/* for uapi */
1098c2ecf20Sopenharmony_ci	int dev_lock_count;
1108c2ecf20Sopenharmony_ci	bool dev_lock_changed;
1118c2ecf20Sopenharmony_ci	wait_queue_head_t hwdep_wait;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/* for M-Audio special devices */
1148c2ecf20Sopenharmony_ci	void *maudio_special_quirk;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	/* For BeBoB version quirk. */
1178c2ecf20Sopenharmony_ci	unsigned int version;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	struct amdtp_domain domain;
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic inline int
1238c2ecf20Sopenharmony_cisnd_bebob_read_block(struct fw_unit *unit, u64 addr, void *buf, int size)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	return snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
1268c2ecf20Sopenharmony_ci				  BEBOB_ADDR_REG_INFO + addr,
1278c2ecf20Sopenharmony_ci				  buf, size, 0);
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic inline int
1318c2ecf20Sopenharmony_cisnd_bebob_read_quad(struct fw_unit *unit, u64 addr, u32 *buf)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	return snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
1348c2ecf20Sopenharmony_ci				  BEBOB_ADDR_REG_INFO + addr,
1358c2ecf20Sopenharmony_ci				  (void *)buf, sizeof(u32), 0);
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci/* AV/C Audio Subunit Specification 1.0 (Oct 2000, 1394TA) */
1398c2ecf20Sopenharmony_ciint avc_audio_set_selector(struct fw_unit *unit, unsigned int subunit_id,
1408c2ecf20Sopenharmony_ci			   unsigned int fb_id, unsigned int num);
1418c2ecf20Sopenharmony_ciint avc_audio_get_selector(struct fw_unit *unit, unsigned  int subunit_id,
1428c2ecf20Sopenharmony_ci			   unsigned int fb_id, unsigned int *num);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci/*
1458c2ecf20Sopenharmony_ci * AVC command extensions, AV/C Unit and Subunit, Revision 17
1468c2ecf20Sopenharmony_ci * (Nov 2003, BridgeCo)
1478c2ecf20Sopenharmony_ci */
1488c2ecf20Sopenharmony_ci#define	AVC_BRIDGECO_ADDR_BYTES	6
1498c2ecf20Sopenharmony_cienum avc_bridgeco_plug_dir {
1508c2ecf20Sopenharmony_ci	AVC_BRIDGECO_PLUG_DIR_IN	= 0x00,
1518c2ecf20Sopenharmony_ci	AVC_BRIDGECO_PLUG_DIR_OUT	= 0x01
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_cienum avc_bridgeco_plug_mode {
1548c2ecf20Sopenharmony_ci	AVC_BRIDGECO_PLUG_MODE_UNIT		= 0x00,
1558c2ecf20Sopenharmony_ci	AVC_BRIDGECO_PLUG_MODE_SUBUNIT		= 0x01,
1568c2ecf20Sopenharmony_ci	AVC_BRIDGECO_PLUG_MODE_FUNCTION_BLOCK	= 0x02
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_cienum avc_bridgeco_plug_unit {
1598c2ecf20Sopenharmony_ci	AVC_BRIDGECO_PLUG_UNIT_ISOC	= 0x00,
1608c2ecf20Sopenharmony_ci	AVC_BRIDGECO_PLUG_UNIT_EXT	= 0x01,
1618c2ecf20Sopenharmony_ci	AVC_BRIDGECO_PLUG_UNIT_ASYNC	= 0x02
1628c2ecf20Sopenharmony_ci};
1638c2ecf20Sopenharmony_cienum avc_bridgeco_plug_type {
1648c2ecf20Sopenharmony_ci	AVC_BRIDGECO_PLUG_TYPE_ISOC	= 0x00,
1658c2ecf20Sopenharmony_ci	AVC_BRIDGECO_PLUG_TYPE_ASYNC	= 0x01,
1668c2ecf20Sopenharmony_ci	AVC_BRIDGECO_PLUG_TYPE_MIDI	= 0x02,
1678c2ecf20Sopenharmony_ci	AVC_BRIDGECO_PLUG_TYPE_SYNC	= 0x03,
1688c2ecf20Sopenharmony_ci	AVC_BRIDGECO_PLUG_TYPE_ANA	= 0x04,
1698c2ecf20Sopenharmony_ci	AVC_BRIDGECO_PLUG_TYPE_DIG	= 0x05,
1708c2ecf20Sopenharmony_ci	AVC_BRIDGECO_PLUG_TYPE_ADDITION	= 0x06
1718c2ecf20Sopenharmony_ci};
1728c2ecf20Sopenharmony_cistatic inline void
1738c2ecf20Sopenharmony_ciavc_bridgeco_fill_unit_addr(u8 buf[AVC_BRIDGECO_ADDR_BYTES],
1748c2ecf20Sopenharmony_ci			    enum avc_bridgeco_plug_dir dir,
1758c2ecf20Sopenharmony_ci			    enum avc_bridgeco_plug_unit unit,
1768c2ecf20Sopenharmony_ci			    unsigned int pid)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	buf[0] = 0xff;	/* Unit */
1798c2ecf20Sopenharmony_ci	buf[1] = dir;
1808c2ecf20Sopenharmony_ci	buf[2] = AVC_BRIDGECO_PLUG_MODE_UNIT;
1818c2ecf20Sopenharmony_ci	buf[3] = unit;
1828c2ecf20Sopenharmony_ci	buf[4] = 0xff & pid;
1838c2ecf20Sopenharmony_ci	buf[5] = 0xff;	/* reserved */
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_cistatic inline void
1868c2ecf20Sopenharmony_ciavc_bridgeco_fill_msu_addr(u8 buf[AVC_BRIDGECO_ADDR_BYTES],
1878c2ecf20Sopenharmony_ci			   enum avc_bridgeco_plug_dir dir,
1888c2ecf20Sopenharmony_ci			   unsigned int pid)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	buf[0] = 0x60;	/* Music subunit */
1918c2ecf20Sopenharmony_ci	buf[1] = dir;
1928c2ecf20Sopenharmony_ci	buf[2] = AVC_BRIDGECO_PLUG_MODE_SUBUNIT;
1938c2ecf20Sopenharmony_ci	buf[3] = 0xff & pid;
1948c2ecf20Sopenharmony_ci	buf[4] = 0xff;	/* reserved */
1958c2ecf20Sopenharmony_ci	buf[5] = 0xff;	/* reserved */
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ciint avc_bridgeco_get_plug_ch_pos(struct fw_unit *unit,
1988c2ecf20Sopenharmony_ci				 u8 addr[AVC_BRIDGECO_ADDR_BYTES],
1998c2ecf20Sopenharmony_ci				 u8 *buf, unsigned int len);
2008c2ecf20Sopenharmony_ciint avc_bridgeco_get_plug_type(struct fw_unit *unit,
2018c2ecf20Sopenharmony_ci			       u8 addr[AVC_BRIDGECO_ADDR_BYTES],
2028c2ecf20Sopenharmony_ci			       enum avc_bridgeco_plug_type *type);
2038c2ecf20Sopenharmony_ciint avc_bridgeco_get_plug_section_type(struct fw_unit *unit,
2048c2ecf20Sopenharmony_ci				       u8 addr[AVC_BRIDGECO_ADDR_BYTES],
2058c2ecf20Sopenharmony_ci				       unsigned int id, u8 *type);
2068c2ecf20Sopenharmony_ciint avc_bridgeco_get_plug_input(struct fw_unit *unit,
2078c2ecf20Sopenharmony_ci				u8 addr[AVC_BRIDGECO_ADDR_BYTES],
2088c2ecf20Sopenharmony_ci				u8 input[7]);
2098c2ecf20Sopenharmony_ciint avc_bridgeco_get_plug_strm_fmt(struct fw_unit *unit,
2108c2ecf20Sopenharmony_ci				   u8 addr[AVC_BRIDGECO_ADDR_BYTES], u8 *buf,
2118c2ecf20Sopenharmony_ci				   unsigned int *len, unsigned int eid);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci/* for AMDTP streaming */
2148c2ecf20Sopenharmony_ciint snd_bebob_stream_get_rate(struct snd_bebob *bebob, unsigned int *rate);
2158c2ecf20Sopenharmony_ciint snd_bebob_stream_set_rate(struct snd_bebob *bebob, unsigned int rate);
2168c2ecf20Sopenharmony_ciint snd_bebob_stream_get_clock_src(struct snd_bebob *bebob,
2178c2ecf20Sopenharmony_ci				   enum snd_bebob_clock_type *src);
2188c2ecf20Sopenharmony_ciint snd_bebob_stream_discover(struct snd_bebob *bebob);
2198c2ecf20Sopenharmony_ciint snd_bebob_stream_init_duplex(struct snd_bebob *bebob);
2208c2ecf20Sopenharmony_ciint snd_bebob_stream_reserve_duplex(struct snd_bebob *bebob, unsigned int rate,
2218c2ecf20Sopenharmony_ci				    unsigned int frames_per_period,
2228c2ecf20Sopenharmony_ci				    unsigned int frames_per_buffer);
2238c2ecf20Sopenharmony_ciint snd_bebob_stream_start_duplex(struct snd_bebob *bebob);
2248c2ecf20Sopenharmony_civoid snd_bebob_stream_stop_duplex(struct snd_bebob *bebob);
2258c2ecf20Sopenharmony_civoid snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_civoid snd_bebob_stream_lock_changed(struct snd_bebob *bebob);
2288c2ecf20Sopenharmony_ciint snd_bebob_stream_lock_try(struct snd_bebob *bebob);
2298c2ecf20Sopenharmony_civoid snd_bebob_stream_lock_release(struct snd_bebob *bebob);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_civoid snd_bebob_proc_init(struct snd_bebob *bebob);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ciint snd_bebob_create_midi_devices(struct snd_bebob *bebob);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ciint snd_bebob_create_pcm_devices(struct snd_bebob *bebob);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ciint snd_bebob_create_hwdep_device(struct snd_bebob *bebob);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci/* model specific operations */
2408c2ecf20Sopenharmony_ciextern const struct snd_bebob_spec phase88_rack_spec;
2418c2ecf20Sopenharmony_ciextern const struct snd_bebob_spec yamaha_terratec_spec;
2428c2ecf20Sopenharmony_ciextern const struct snd_bebob_spec saffirepro_26_spec;
2438c2ecf20Sopenharmony_ciextern const struct snd_bebob_spec saffirepro_10_spec;
2448c2ecf20Sopenharmony_ciextern const struct snd_bebob_spec saffire_le_spec;
2458c2ecf20Sopenharmony_ciextern const struct snd_bebob_spec saffire_spec;
2468c2ecf20Sopenharmony_ciextern const struct snd_bebob_spec maudio_fw410_spec;
2478c2ecf20Sopenharmony_ciextern const struct snd_bebob_spec maudio_audiophile_spec;
2488c2ecf20Sopenharmony_ciextern const struct snd_bebob_spec maudio_solo_spec;
2498c2ecf20Sopenharmony_ciextern const struct snd_bebob_spec maudio_ozonic_spec;
2508c2ecf20Sopenharmony_ciextern const struct snd_bebob_spec maudio_nrv10_spec;
2518c2ecf20Sopenharmony_ciextern const struct snd_bebob_spec maudio_special_spec;
2528c2ecf20Sopenharmony_ciint snd_bebob_maudio_special_discover(struct snd_bebob *bebob, bool is1814);
2538c2ecf20Sopenharmony_ciint snd_bebob_maudio_load_firmware(struct fw_unit *unit);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci#define SND_BEBOB_DEV_ENTRY(vendor, model, data) \
2568c2ecf20Sopenharmony_ci{ \
2578c2ecf20Sopenharmony_ci	.match_flags	= IEEE1394_MATCH_VENDOR_ID | \
2588c2ecf20Sopenharmony_ci			  IEEE1394_MATCH_MODEL_ID, \
2598c2ecf20Sopenharmony_ci	.vendor_id	= vendor, \
2608c2ecf20Sopenharmony_ci	.model_id	= model, \
2618c2ecf20Sopenharmony_ci	.driver_data	= (kernel_ulong_t)data \
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci#endif
265