162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * dice.h - a part of driver for Dice based devices
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) Clemens Ladisch
662306a36Sopenharmony_ci * Copyright (c) 2014 Takashi Sakamoto
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#ifndef SOUND_DICE_H_INCLUDED
1062306a36Sopenharmony_ci#define SOUND_DICE_H_INCLUDED
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/compat.h>
1362306a36Sopenharmony_ci#include <linux/completion.h>
1462306a36Sopenharmony_ci#include <linux/delay.h>
1562306a36Sopenharmony_ci#include <linux/device.h>
1662306a36Sopenharmony_ci#include <linux/firewire.h>
1762306a36Sopenharmony_ci#include <linux/firewire-constants.h>
1862306a36Sopenharmony_ci#include <linux/jiffies.h>
1962306a36Sopenharmony_ci#include <linux/module.h>
2062306a36Sopenharmony_ci#include <linux/mod_devicetable.h>
2162306a36Sopenharmony_ci#include <linux/mutex.h>
2262306a36Sopenharmony_ci#include <linux/slab.h>
2362306a36Sopenharmony_ci#include <linux/spinlock.h>
2462306a36Sopenharmony_ci#include <linux/wait.h>
2562306a36Sopenharmony_ci#include <linux/sched/signal.h>
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#include <sound/control.h>
2862306a36Sopenharmony_ci#include <sound/core.h>
2962306a36Sopenharmony_ci#include <sound/firewire.h>
3062306a36Sopenharmony_ci#include <sound/hwdep.h>
3162306a36Sopenharmony_ci#include <sound/info.h>
3262306a36Sopenharmony_ci#include <sound/initval.h>
3362306a36Sopenharmony_ci#include <sound/pcm.h>
3462306a36Sopenharmony_ci#include <sound/pcm_params.h>
3562306a36Sopenharmony_ci#include <sound/rawmidi.h>
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#include "../amdtp-am824.h"
3862306a36Sopenharmony_ci#include "../iso-resources.h"
3962306a36Sopenharmony_ci#include "../lib.h"
4062306a36Sopenharmony_ci#include "dice-interface.h"
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci/*
4362306a36Sopenharmony_ci * This module support maximum 2 pairs of tx/rx isochronous streams for
4462306a36Sopenharmony_ci * our convinience.
4562306a36Sopenharmony_ci *
4662306a36Sopenharmony_ci * In documents for ASICs called with a name of 'DICE':
4762306a36Sopenharmony_ci *  - ASIC for DICE II:
4862306a36Sopenharmony_ci *   - Maximum 2 tx and 4 rx are supported.
4962306a36Sopenharmony_ci *   - A packet supports maximum 16 data channels.
5062306a36Sopenharmony_ci *  - TCD2210/2210-E (so-called 'Dice Mini'):
5162306a36Sopenharmony_ci *   - Maximum 2 tx and 2 rx are supported.
5262306a36Sopenharmony_ci *   - A packet supports maximum 16 data channels.
5362306a36Sopenharmony_ci *  - TCD2220/2220-E (so-called 'Dice Jr.')
5462306a36Sopenharmony_ci *   - 2 tx and 2 rx are supported.
5562306a36Sopenharmony_ci *   - A packet supports maximum 16 data channels.
5662306a36Sopenharmony_ci *  - TCD3070-CH (so-called 'Dice III')
5762306a36Sopenharmony_ci *   - Maximum 2 tx and 2 rx are supported.
5862306a36Sopenharmony_ci *   - A packet supports maximum 32 data channels.
5962306a36Sopenharmony_ci *
6062306a36Sopenharmony_ci * For the above, MIDI conformant data channel is just on the first isochronous
6162306a36Sopenharmony_ci * stream.
6262306a36Sopenharmony_ci */
6362306a36Sopenharmony_ci#define MAX_STREAMS	2
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_cienum snd_dice_rate_mode {
6662306a36Sopenharmony_ci	SND_DICE_RATE_MODE_LOW = 0,
6762306a36Sopenharmony_ci	SND_DICE_RATE_MODE_MIDDLE,
6862306a36Sopenharmony_ci	SND_DICE_RATE_MODE_HIGH,
6962306a36Sopenharmony_ci	SND_DICE_RATE_MODE_COUNT,
7062306a36Sopenharmony_ci};
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_cistruct snd_dice;
7362306a36Sopenharmony_citypedef int (*snd_dice_detect_formats_t)(struct snd_dice *dice);
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cistruct snd_dice {
7662306a36Sopenharmony_ci	struct snd_card *card;
7762306a36Sopenharmony_ci	struct fw_unit *unit;
7862306a36Sopenharmony_ci	spinlock_t lock;
7962306a36Sopenharmony_ci	struct mutex mutex;
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	/* Offsets for sub-addresses */
8262306a36Sopenharmony_ci	unsigned int global_offset;
8362306a36Sopenharmony_ci	unsigned int rx_offset;
8462306a36Sopenharmony_ci	unsigned int tx_offset;
8562306a36Sopenharmony_ci	unsigned int sync_offset;
8662306a36Sopenharmony_ci	unsigned int rsrv_offset;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	unsigned int clock_caps;
8962306a36Sopenharmony_ci	unsigned int tx_pcm_chs[MAX_STREAMS][SND_DICE_RATE_MODE_COUNT];
9062306a36Sopenharmony_ci	unsigned int rx_pcm_chs[MAX_STREAMS][SND_DICE_RATE_MODE_COUNT];
9162306a36Sopenharmony_ci	unsigned int tx_midi_ports[MAX_STREAMS];
9262306a36Sopenharmony_ci	unsigned int rx_midi_ports[MAX_STREAMS];
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	struct fw_address_handler notification_handler;
9562306a36Sopenharmony_ci	int owner_generation;
9662306a36Sopenharmony_ci	u32 notification_bits;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	/* For uapi */
9962306a36Sopenharmony_ci	int dev_lock_count; /* > 0 driver, < 0 userspace */
10062306a36Sopenharmony_ci	bool dev_lock_changed;
10162306a36Sopenharmony_ci	wait_queue_head_t hwdep_wait;
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	/* For streaming */
10462306a36Sopenharmony_ci	struct fw_iso_resources tx_resources[MAX_STREAMS];
10562306a36Sopenharmony_ci	struct fw_iso_resources rx_resources[MAX_STREAMS];
10662306a36Sopenharmony_ci	struct amdtp_stream tx_stream[MAX_STREAMS];
10762306a36Sopenharmony_ci	struct amdtp_stream rx_stream[MAX_STREAMS];
10862306a36Sopenharmony_ci	bool global_enabled:1;
10962306a36Sopenharmony_ci	bool disable_double_pcm_frames:1;
11062306a36Sopenharmony_ci	struct completion clock_accepted;
11162306a36Sopenharmony_ci	unsigned int substreams_counter;
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	struct amdtp_domain domain;
11462306a36Sopenharmony_ci};
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cienum snd_dice_addr_type {
11762306a36Sopenharmony_ci	SND_DICE_ADDR_TYPE_PRIVATE,
11862306a36Sopenharmony_ci	SND_DICE_ADDR_TYPE_GLOBAL,
11962306a36Sopenharmony_ci	SND_DICE_ADDR_TYPE_TX,
12062306a36Sopenharmony_ci	SND_DICE_ADDR_TYPE_RX,
12162306a36Sopenharmony_ci	SND_DICE_ADDR_TYPE_SYNC,
12262306a36Sopenharmony_ci	SND_DICE_ADDR_TYPE_RSRV,
12362306a36Sopenharmony_ci};
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ciint snd_dice_transaction_write(struct snd_dice *dice,
12662306a36Sopenharmony_ci			       enum snd_dice_addr_type type,
12762306a36Sopenharmony_ci			       unsigned int offset,
12862306a36Sopenharmony_ci			       void *buf, unsigned int len);
12962306a36Sopenharmony_ciint snd_dice_transaction_read(struct snd_dice *dice,
13062306a36Sopenharmony_ci			      enum snd_dice_addr_type type, unsigned int offset,
13162306a36Sopenharmony_ci			      void *buf, unsigned int len);
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_cistatic inline int snd_dice_transaction_write_global(struct snd_dice *dice,
13462306a36Sopenharmony_ci						    unsigned int offset,
13562306a36Sopenharmony_ci						    void *buf, unsigned int len)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci	return snd_dice_transaction_write(dice,
13862306a36Sopenharmony_ci					  SND_DICE_ADDR_TYPE_GLOBAL, offset,
13962306a36Sopenharmony_ci					  buf, len);
14062306a36Sopenharmony_ci}
14162306a36Sopenharmony_cistatic inline int snd_dice_transaction_read_global(struct snd_dice *dice,
14262306a36Sopenharmony_ci						   unsigned int offset,
14362306a36Sopenharmony_ci						   void *buf, unsigned int len)
14462306a36Sopenharmony_ci{
14562306a36Sopenharmony_ci	return snd_dice_transaction_read(dice,
14662306a36Sopenharmony_ci					 SND_DICE_ADDR_TYPE_GLOBAL, offset,
14762306a36Sopenharmony_ci					 buf, len);
14862306a36Sopenharmony_ci}
14962306a36Sopenharmony_cistatic inline int snd_dice_transaction_write_tx(struct snd_dice *dice,
15062306a36Sopenharmony_ci						unsigned int offset,
15162306a36Sopenharmony_ci						void *buf, unsigned int len)
15262306a36Sopenharmony_ci{
15362306a36Sopenharmony_ci	return snd_dice_transaction_write(dice, SND_DICE_ADDR_TYPE_TX, offset,
15462306a36Sopenharmony_ci					  buf, len);
15562306a36Sopenharmony_ci}
15662306a36Sopenharmony_cistatic inline int snd_dice_transaction_read_tx(struct snd_dice *dice,
15762306a36Sopenharmony_ci					       unsigned int offset,
15862306a36Sopenharmony_ci					       void *buf, unsigned int len)
15962306a36Sopenharmony_ci{
16062306a36Sopenharmony_ci	return snd_dice_transaction_read(dice, SND_DICE_ADDR_TYPE_TX, offset,
16162306a36Sopenharmony_ci					 buf, len);
16262306a36Sopenharmony_ci}
16362306a36Sopenharmony_cistatic inline int snd_dice_transaction_write_rx(struct snd_dice *dice,
16462306a36Sopenharmony_ci						unsigned int offset,
16562306a36Sopenharmony_ci						void *buf, unsigned int len)
16662306a36Sopenharmony_ci{
16762306a36Sopenharmony_ci	return snd_dice_transaction_write(dice, SND_DICE_ADDR_TYPE_RX, offset,
16862306a36Sopenharmony_ci					  buf, len);
16962306a36Sopenharmony_ci}
17062306a36Sopenharmony_cistatic inline int snd_dice_transaction_read_rx(struct snd_dice *dice,
17162306a36Sopenharmony_ci					       unsigned int offset,
17262306a36Sopenharmony_ci					       void *buf, unsigned int len)
17362306a36Sopenharmony_ci{
17462306a36Sopenharmony_ci	return snd_dice_transaction_read(dice, SND_DICE_ADDR_TYPE_RX, offset,
17562306a36Sopenharmony_ci					 buf, len);
17662306a36Sopenharmony_ci}
17762306a36Sopenharmony_cistatic inline int snd_dice_transaction_write_sync(struct snd_dice *dice,
17862306a36Sopenharmony_ci						  unsigned int offset,
17962306a36Sopenharmony_ci						  void *buf, unsigned int len)
18062306a36Sopenharmony_ci{
18162306a36Sopenharmony_ci	return snd_dice_transaction_write(dice, SND_DICE_ADDR_TYPE_SYNC, offset,
18262306a36Sopenharmony_ci					  buf, len);
18362306a36Sopenharmony_ci}
18462306a36Sopenharmony_cistatic inline int snd_dice_transaction_read_sync(struct snd_dice *dice,
18562306a36Sopenharmony_ci						 unsigned int offset,
18662306a36Sopenharmony_ci						 void *buf, unsigned int len)
18762306a36Sopenharmony_ci{
18862306a36Sopenharmony_ci	return snd_dice_transaction_read(dice, SND_DICE_ADDR_TYPE_SYNC, offset,
18962306a36Sopenharmony_ci					 buf, len);
19062306a36Sopenharmony_ci}
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ciint snd_dice_transaction_get_clock_source(struct snd_dice *dice,
19362306a36Sopenharmony_ci					  unsigned int *source);
19462306a36Sopenharmony_ciint snd_dice_transaction_get_rate(struct snd_dice *dice, unsigned int *rate);
19562306a36Sopenharmony_ciint snd_dice_transaction_set_enable(struct snd_dice *dice);
19662306a36Sopenharmony_civoid snd_dice_transaction_clear_enable(struct snd_dice *dice);
19762306a36Sopenharmony_ciint snd_dice_transaction_init(struct snd_dice *dice);
19862306a36Sopenharmony_ciint snd_dice_transaction_reinit(struct snd_dice *dice);
19962306a36Sopenharmony_civoid snd_dice_transaction_destroy(struct snd_dice *dice);
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci#define SND_DICE_RATES_COUNT	7
20262306a36Sopenharmony_ciextern const unsigned int snd_dice_rates[SND_DICE_RATES_COUNT];
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ciint snd_dice_stream_get_rate_mode(struct snd_dice *dice, unsigned int rate,
20562306a36Sopenharmony_ci				  enum snd_dice_rate_mode *mode);
20662306a36Sopenharmony_ciint snd_dice_stream_start_duplex(struct snd_dice *dice);
20762306a36Sopenharmony_civoid snd_dice_stream_stop_duplex(struct snd_dice *dice);
20862306a36Sopenharmony_ciint snd_dice_stream_init_duplex(struct snd_dice *dice);
20962306a36Sopenharmony_civoid snd_dice_stream_destroy_duplex(struct snd_dice *dice);
21062306a36Sopenharmony_ciint snd_dice_stream_reserve_duplex(struct snd_dice *dice, unsigned int rate,
21162306a36Sopenharmony_ci				   unsigned int events_per_period,
21262306a36Sopenharmony_ci				   unsigned int events_per_buffer);
21362306a36Sopenharmony_civoid snd_dice_stream_update_duplex(struct snd_dice *dice);
21462306a36Sopenharmony_ciint snd_dice_stream_detect_current_formats(struct snd_dice *dice);
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ciint snd_dice_stream_lock_try(struct snd_dice *dice);
21762306a36Sopenharmony_civoid snd_dice_stream_lock_release(struct snd_dice *dice);
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ciint snd_dice_create_pcm(struct snd_dice *dice);
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ciint snd_dice_create_hwdep(struct snd_dice *dice);
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_civoid snd_dice_create_proc(struct snd_dice *dice);
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ciint snd_dice_create_midi(struct snd_dice *dice);
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ciint snd_dice_detect_tcelectronic_formats(struct snd_dice *dice);
22862306a36Sopenharmony_ciint snd_dice_detect_alesis_formats(struct snd_dice *dice);
22962306a36Sopenharmony_ciint snd_dice_detect_alesis_mastercontrol_formats(struct snd_dice *dice);
23062306a36Sopenharmony_ciint snd_dice_detect_extension_formats(struct snd_dice *dice);
23162306a36Sopenharmony_ciint snd_dice_detect_mytek_formats(struct snd_dice *dice);
23262306a36Sopenharmony_ciint snd_dice_detect_presonus_formats(struct snd_dice *dice);
23362306a36Sopenharmony_ciint snd_dice_detect_harman_formats(struct snd_dice *dice);
23462306a36Sopenharmony_ciint snd_dice_detect_focusrite_pro40_tcd3070_formats(struct snd_dice *dice);
23562306a36Sopenharmony_ciint snd_dice_detect_weiss_formats(struct snd_dice *dice);
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci#endif
238