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