Takashi Sakamoto | 7c2d4c0c | 2014-11-29 00:59:13 +0900 | [diff] [blame] | 1 | /* |
| 2 | * dice.h - a part of driver for Dice based devices |
| 3 | * |
| 4 | * Copyright (c) Clemens Ladisch |
| 5 | * Copyright (c) 2014 Takashi Sakamoto |
| 6 | * |
| 7 | * Licensed under the terms of the GNU General Public License, version 2. |
| 8 | */ |
| 9 | |
| 10 | #ifndef SOUND_DICE_H_INCLUDED |
| 11 | #define SOUND_DICE_H_INCLUDED |
| 12 | |
| 13 | #include <linux/compat.h> |
| 14 | #include <linux/completion.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/firewire.h> |
| 18 | #include <linux/firewire-constants.h> |
| 19 | #include <linux/jiffies.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/mod_devicetable.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/wait.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 26 | #include <linux/sched/signal.h> |
Takashi Sakamoto | 7c2d4c0c | 2014-11-29 00:59:13 +0900 | [diff] [blame] | 27 | |
| 28 | #include <sound/control.h> |
| 29 | #include <sound/core.h> |
| 30 | #include <sound/firewire.h> |
| 31 | #include <sound/hwdep.h> |
| 32 | #include <sound/info.h> |
| 33 | #include <sound/initval.h> |
| 34 | #include <sound/pcm.h> |
| 35 | #include <sound/pcm_params.h> |
Takashi Sakamoto | a113ff8 | 2014-12-09 00:10:39 +0900 | [diff] [blame] | 36 | #include <sound/rawmidi.h> |
Takashi Sakamoto | 7c2d4c0c | 2014-11-29 00:59:13 +0900 | [diff] [blame] | 37 | |
Takashi Sakamoto | 5955815 | 2015-09-19 11:21:55 +0900 | [diff] [blame] | 38 | #include "../amdtp-am824.h" |
Takashi Sakamoto | 7c2d4c0c | 2014-11-29 00:59:13 +0900 | [diff] [blame] | 39 | #include "../iso-resources.h" |
| 40 | #include "../lib.h" |
| 41 | #include "dice-interface.h" |
| 42 | |
Takashi Sakamoto | 8ae25b76 | 2016-03-07 22:35:42 +0900 | [diff] [blame] | 43 | /* |
| 44 | * This module support maximum 2 pairs of tx/rx isochronous streams for |
| 45 | * our convinience. |
| 46 | * |
| 47 | * In documents for ASICs called with a name of 'DICE': |
| 48 | * - ASIC for DICE II: |
| 49 | * - Maximum 2 tx and 4 rx are supported. |
| 50 | * - A packet supports maximum 16 data channels. |
| 51 | * - TCD2210/2210-E (so-called 'Dice Mini'): |
| 52 | * - Maximum 2 tx and 2 rx are supported. |
| 53 | * - A packet supports maximum 16 data channels. |
| 54 | * - TCD2220/2220-E (so-called 'Dice Jr.') |
| 55 | * - 2 tx and 2 rx are supported. |
| 56 | * - A packet supports maximum 16 data channels. |
| 57 | * - TCD3070-CH (so-called 'Dice III') |
| 58 | * - Maximum 2 tx and 2 rx are supported. |
| 59 | * - A packet supports maximum 32 data channels. |
| 60 | * |
| 61 | * For the above, MIDI conformant data channel is just on the first isochronous |
| 62 | * stream. |
| 63 | */ |
| 64 | #define MAX_STREAMS 2 |
| 65 | |
Takashi Sakamoto | 8feda7d | 2018-05-02 19:16:39 +0900 | [diff] [blame] | 66 | enum snd_dice_rate_mode { |
| 67 | SND_DICE_RATE_MODE_LOW = 0, |
| 68 | SND_DICE_RATE_MODE_MIDDLE, |
| 69 | SND_DICE_RATE_MODE_HIGH, |
| 70 | SND_DICE_RATE_MODE_COUNT, |
| 71 | }; |
| 72 | |
Takashi Sakamoto | f1f0f33 | 2018-05-02 19:16:43 +0900 | [diff] [blame] | 73 | struct snd_dice; |
| 74 | typedef int (*snd_dice_detect_formats_t)(struct snd_dice *dice); |
| 75 | |
Takashi Sakamoto | 7c2d4c0c | 2014-11-29 00:59:13 +0900 | [diff] [blame] | 76 | struct snd_dice { |
| 77 | struct snd_card *card; |
| 78 | struct fw_unit *unit; |
| 79 | spinlock_t lock; |
| 80 | struct mutex mutex; |
| 81 | |
Takashi Sakamoto | b59fb19 | 2015-12-31 13:58:12 +0900 | [diff] [blame] | 82 | bool registered; |
| 83 | struct delayed_work dwork; |
| 84 | |
Takashi Sakamoto | 7c2d4c0c | 2014-11-29 00:59:13 +0900 | [diff] [blame] | 85 | /* Offsets for sub-addresses */ |
| 86 | unsigned int global_offset; |
| 87 | unsigned int rx_offset; |
| 88 | unsigned int tx_offset; |
| 89 | unsigned int sync_offset; |
| 90 | unsigned int rsrv_offset; |
| 91 | |
| 92 | unsigned int clock_caps; |
Takashi Sakamoto | 8feda7d | 2018-05-02 19:16:39 +0900 | [diff] [blame] | 93 | unsigned int tx_pcm_chs[MAX_STREAMS][SND_DICE_RATE_MODE_COUNT]; |
| 94 | unsigned int rx_pcm_chs[MAX_STREAMS][SND_DICE_RATE_MODE_COUNT]; |
| 95 | unsigned int tx_midi_ports[MAX_STREAMS]; |
| 96 | unsigned int rx_midi_ports[MAX_STREAMS]; |
Takashi Sakamoto | f1f0f33 | 2018-05-02 19:16:43 +0900 | [diff] [blame] | 97 | snd_dice_detect_formats_t detect_formats; |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 98 | |
Takashi Sakamoto | 7c2d4c0c | 2014-11-29 00:59:13 +0900 | [diff] [blame] | 99 | struct fw_address_handler notification_handler; |
| 100 | int owner_generation; |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 101 | u32 notification_bits; |
| 102 | |
| 103 | /* For uapi */ |
Takashi Sakamoto | 7c2d4c0c | 2014-11-29 00:59:13 +0900 | [diff] [blame] | 104 | int dev_lock_count; /* > 0 driver, < 0 userspace */ |
| 105 | bool dev_lock_changed; |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 106 | wait_queue_head_t hwdep_wait; |
| 107 | |
| 108 | /* For streaming */ |
Takashi Sakamoto | 8ae25b76 | 2016-03-07 22:35:42 +0900 | [diff] [blame] | 109 | struct fw_iso_resources tx_resources[MAX_STREAMS]; |
| 110 | struct fw_iso_resources rx_resources[MAX_STREAMS]; |
| 111 | struct amdtp_stream tx_stream[MAX_STREAMS]; |
| 112 | struct amdtp_stream rx_stream[MAX_STREAMS]; |
Takashi Sakamoto | 7c2d4c0c | 2014-11-29 00:59:13 +0900 | [diff] [blame] | 113 | bool global_enabled; |
| 114 | struct completion clock_accepted; |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 115 | unsigned int substreams_counter; |
Takashi Sakamoto | 7c2d4c0c | 2014-11-29 00:59:13 +0900 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | enum snd_dice_addr_type { |
| 119 | SND_DICE_ADDR_TYPE_PRIVATE, |
| 120 | SND_DICE_ADDR_TYPE_GLOBAL, |
| 121 | SND_DICE_ADDR_TYPE_TX, |
| 122 | SND_DICE_ADDR_TYPE_RX, |
| 123 | SND_DICE_ADDR_TYPE_SYNC, |
| 124 | SND_DICE_ADDR_TYPE_RSRV, |
| 125 | }; |
| 126 | |
| 127 | int snd_dice_transaction_write(struct snd_dice *dice, |
| 128 | enum snd_dice_addr_type type, |
| 129 | unsigned int offset, |
| 130 | void *buf, unsigned int len); |
| 131 | int snd_dice_transaction_read(struct snd_dice *dice, |
| 132 | enum snd_dice_addr_type type, unsigned int offset, |
| 133 | void *buf, unsigned int len); |
| 134 | |
| 135 | static inline int snd_dice_transaction_write_global(struct snd_dice *dice, |
| 136 | unsigned int offset, |
| 137 | void *buf, unsigned int len) |
| 138 | { |
| 139 | return snd_dice_transaction_write(dice, |
| 140 | SND_DICE_ADDR_TYPE_GLOBAL, offset, |
| 141 | buf, len); |
| 142 | } |
| 143 | static inline int snd_dice_transaction_read_global(struct snd_dice *dice, |
| 144 | unsigned int offset, |
| 145 | void *buf, unsigned int len) |
| 146 | { |
| 147 | return snd_dice_transaction_read(dice, |
| 148 | SND_DICE_ADDR_TYPE_GLOBAL, offset, |
| 149 | buf, len); |
| 150 | } |
| 151 | static inline int snd_dice_transaction_write_tx(struct snd_dice *dice, |
| 152 | unsigned int offset, |
| 153 | void *buf, unsigned int len) |
| 154 | { |
| 155 | return snd_dice_transaction_write(dice, SND_DICE_ADDR_TYPE_TX, offset, |
| 156 | buf, len); |
| 157 | } |
| 158 | static inline int snd_dice_transaction_read_tx(struct snd_dice *dice, |
| 159 | unsigned int offset, |
| 160 | void *buf, unsigned int len) |
| 161 | { |
| 162 | return snd_dice_transaction_read(dice, SND_DICE_ADDR_TYPE_TX, offset, |
| 163 | buf, len); |
| 164 | } |
| 165 | static inline int snd_dice_transaction_write_rx(struct snd_dice *dice, |
| 166 | unsigned int offset, |
| 167 | void *buf, unsigned int len) |
| 168 | { |
| 169 | return snd_dice_transaction_write(dice, SND_DICE_ADDR_TYPE_RX, offset, |
| 170 | buf, len); |
| 171 | } |
| 172 | static inline int snd_dice_transaction_read_rx(struct snd_dice *dice, |
| 173 | unsigned int offset, |
| 174 | void *buf, unsigned int len) |
| 175 | { |
| 176 | return snd_dice_transaction_read(dice, SND_DICE_ADDR_TYPE_RX, offset, |
| 177 | buf, len); |
| 178 | } |
| 179 | static inline int snd_dice_transaction_write_sync(struct snd_dice *dice, |
| 180 | unsigned int offset, |
| 181 | void *buf, unsigned int len) |
| 182 | { |
| 183 | return snd_dice_transaction_write(dice, SND_DICE_ADDR_TYPE_SYNC, offset, |
| 184 | buf, len); |
| 185 | } |
| 186 | static inline int snd_dice_transaction_read_sync(struct snd_dice *dice, |
| 187 | unsigned int offset, |
| 188 | void *buf, unsigned int len) |
| 189 | { |
| 190 | return snd_dice_transaction_read(dice, SND_DICE_ADDR_TYPE_SYNC, offset, |
| 191 | buf, len); |
| 192 | } |
| 193 | |
Takashi Sakamoto | 7c2d4c0c | 2014-11-29 00:59:13 +0900 | [diff] [blame] | 194 | int snd_dice_transaction_get_clock_source(struct snd_dice *dice, |
| 195 | unsigned int *source); |
Takashi Sakamoto | 7c2d4c0c | 2014-11-29 00:59:13 +0900 | [diff] [blame] | 196 | int snd_dice_transaction_get_rate(struct snd_dice *dice, unsigned int *rate); |
| 197 | int snd_dice_transaction_set_enable(struct snd_dice *dice); |
| 198 | void snd_dice_transaction_clear_enable(struct snd_dice *dice); |
| 199 | int snd_dice_transaction_init(struct snd_dice *dice); |
| 200 | int snd_dice_transaction_reinit(struct snd_dice *dice); |
| 201 | void snd_dice_transaction_destroy(struct snd_dice *dice); |
| 202 | |
| 203 | #define SND_DICE_RATES_COUNT 7 |
| 204 | extern const unsigned int snd_dice_rates[SND_DICE_RATES_COUNT]; |
| 205 | |
Takashi Sakamoto | b60152f | 2018-05-02 19:16:42 +0900 | [diff] [blame] | 206 | int snd_dice_stream_get_rate_mode(struct snd_dice *dice, unsigned int rate, |
| 207 | enum snd_dice_rate_mode *mode); |
Takashi Sakamoto | 9a02843 | 2014-12-09 00:10:36 +0900 | [diff] [blame] | 208 | int snd_dice_stream_start_duplex(struct snd_dice *dice, unsigned int rate); |
| 209 | void snd_dice_stream_stop_duplex(struct snd_dice *dice); |
| 210 | int snd_dice_stream_init_duplex(struct snd_dice *dice); |
| 211 | void snd_dice_stream_destroy_duplex(struct snd_dice *dice); |
| 212 | void snd_dice_stream_update_duplex(struct snd_dice *dice); |
Takashi Sakamoto | b60152f | 2018-05-02 19:16:42 +0900 | [diff] [blame] | 213 | int snd_dice_stream_detect_current_formats(struct snd_dice *dice); |
Takashi Sakamoto | 6eb6c81 | 2014-11-29 00:59:14 +0900 | [diff] [blame] | 214 | |
| 215 | int snd_dice_stream_lock_try(struct snd_dice *dice); |
| 216 | void snd_dice_stream_lock_release(struct snd_dice *dice); |
| 217 | |
Takashi Sakamoto | c50fb91 | 2014-11-29 00:59:15 +0900 | [diff] [blame] | 218 | int snd_dice_create_pcm(struct snd_dice *dice); |
| 219 | |
Takashi Sakamoto | 19af57b | 2014-11-29 00:59:16 +0900 | [diff] [blame] | 220 | int snd_dice_create_hwdep(struct snd_dice *dice); |
| 221 | |
Takashi Sakamoto | 04d426a | 2014-11-29 00:59:17 +0900 | [diff] [blame] | 222 | void snd_dice_create_proc(struct snd_dice *dice); |
| 223 | |
Takashi Sakamoto | a113ff8 | 2014-12-09 00:10:39 +0900 | [diff] [blame] | 224 | int snd_dice_create_midi(struct snd_dice *dice); |
| 225 | |
Takashi Sakamoto | f1f0f33 | 2018-05-02 19:16:43 +0900 | [diff] [blame] | 226 | int snd_dice_detect_tcelectronic_formats(struct snd_dice *dice); |
Takashi Sakamoto | 28b208f | 2018-05-02 19:16:44 +0900 | [diff] [blame] | 227 | int snd_dice_detect_alesis_formats(struct snd_dice *dice); |
Takashi Sakamoto | 58579c0 | 2018-05-02 19:16:45 +0900 | [diff] [blame] | 228 | int snd_dice_detect_extension_formats(struct snd_dice *dice); |
Melvin Vermeeren | 7c1543f | 2018-05-17 21:00:00 +0200 | [diff] [blame] | 229 | int snd_dice_detect_mytek_formats(struct snd_dice *dice); |
Takashi Sakamoto | c4580f2 | 2019-05-17 11:56:22 +0900 | [diff] [blame] | 230 | int snd_dice_detect_presonus_formats(struct snd_dice *dice); |
Takashi Sakamoto | f1f0f33 | 2018-05-02 19:16:43 +0900 | [diff] [blame] | 231 | |
Takashi Sakamoto | 7c2d4c0c | 2014-11-29 00:59:13 +0900 | [diff] [blame] | 232 | #endif |