Linux-libre 4.13.7-gnu
[librecmc/linux-libre.git] / sound / firewire / dice / dice.h
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>
26 #include <linux/sched/signal.h>
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>
36 #include <sound/rawmidi.h>
37
38 #include "../amdtp-am824.h"
39 #include "../iso-resources.h"
40 #include "../lib.h"
41 #include "dice-interface.h"
42
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
66 struct snd_dice {
67         struct snd_card *card;
68         struct fw_unit *unit;
69         spinlock_t lock;
70         struct mutex mutex;
71
72         bool registered;
73         struct delayed_work dwork;
74
75         /* Offsets for sub-addresses */
76         unsigned int global_offset;
77         unsigned int rx_offset;
78         unsigned int tx_offset;
79         unsigned int sync_offset;
80         unsigned int rsrv_offset;
81
82         unsigned int clock_caps;
83
84         struct fw_address_handler notification_handler;
85         int owner_generation;
86         u32 notification_bits;
87
88         /* For uapi */
89         int dev_lock_count; /* > 0 driver, < 0 userspace */
90         bool dev_lock_changed;
91         wait_queue_head_t hwdep_wait;
92
93         /* For streaming */
94         struct fw_iso_resources tx_resources[MAX_STREAMS];
95         struct fw_iso_resources rx_resources[MAX_STREAMS];
96         struct amdtp_stream tx_stream[MAX_STREAMS];
97         struct amdtp_stream rx_stream[MAX_STREAMS];
98         bool global_enabled;
99         struct completion clock_accepted;
100         unsigned int substreams_counter;
101
102         bool force_two_pcms;
103 };
104
105 enum snd_dice_addr_type {
106         SND_DICE_ADDR_TYPE_PRIVATE,
107         SND_DICE_ADDR_TYPE_GLOBAL,
108         SND_DICE_ADDR_TYPE_TX,
109         SND_DICE_ADDR_TYPE_RX,
110         SND_DICE_ADDR_TYPE_SYNC,
111         SND_DICE_ADDR_TYPE_RSRV,
112 };
113
114 int snd_dice_transaction_write(struct snd_dice *dice,
115                                enum snd_dice_addr_type type,
116                                unsigned int offset,
117                                void *buf, unsigned int len);
118 int snd_dice_transaction_read(struct snd_dice *dice,
119                               enum snd_dice_addr_type type, unsigned int offset,
120                               void *buf, unsigned int len);
121
122 static inline int snd_dice_transaction_write_global(struct snd_dice *dice,
123                                                     unsigned int offset,
124                                                     void *buf, unsigned int len)
125 {
126         return snd_dice_transaction_write(dice,
127                                           SND_DICE_ADDR_TYPE_GLOBAL, offset,
128                                           buf, len);
129 }
130 static inline int snd_dice_transaction_read_global(struct snd_dice *dice,
131                                                    unsigned int offset,
132                                                    void *buf, unsigned int len)
133 {
134         return snd_dice_transaction_read(dice,
135                                          SND_DICE_ADDR_TYPE_GLOBAL, offset,
136                                          buf, len);
137 }
138 static inline int snd_dice_transaction_write_tx(struct snd_dice *dice,
139                                                 unsigned int offset,
140                                                 void *buf, unsigned int len)
141 {
142         return snd_dice_transaction_write(dice, SND_DICE_ADDR_TYPE_TX, offset,
143                                           buf, len);
144 }
145 static inline int snd_dice_transaction_read_tx(struct snd_dice *dice,
146                                                unsigned int offset,
147                                                void *buf, unsigned int len)
148 {
149         return snd_dice_transaction_read(dice, SND_DICE_ADDR_TYPE_TX, offset,
150                                          buf, len);
151 }
152 static inline int snd_dice_transaction_write_rx(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_RX, offset,
157                                           buf, len);
158 }
159 static inline int snd_dice_transaction_read_rx(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_RX, offset,
164                                          buf, len);
165 }
166 static inline int snd_dice_transaction_write_sync(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_SYNC, offset,
171                                           buf, len);
172 }
173 static inline int snd_dice_transaction_read_sync(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_SYNC, offset,
178                                          buf, len);
179 }
180
181 int snd_dice_transaction_get_clock_source(struct snd_dice *dice,
182                                           unsigned int *source);
183 int snd_dice_transaction_get_rate(struct snd_dice *dice, unsigned int *rate);
184 int snd_dice_transaction_set_enable(struct snd_dice *dice);
185 void snd_dice_transaction_clear_enable(struct snd_dice *dice);
186 int snd_dice_transaction_init(struct snd_dice *dice);
187 int snd_dice_transaction_reinit(struct snd_dice *dice);
188 void snd_dice_transaction_destroy(struct snd_dice *dice);
189
190 #define SND_DICE_RATES_COUNT    7
191 extern const unsigned int snd_dice_rates[SND_DICE_RATES_COUNT];
192
193 int snd_dice_stream_start_duplex(struct snd_dice *dice, unsigned int rate);
194 void snd_dice_stream_stop_duplex(struct snd_dice *dice);
195 int snd_dice_stream_init_duplex(struct snd_dice *dice);
196 void snd_dice_stream_destroy_duplex(struct snd_dice *dice);
197 void snd_dice_stream_update_duplex(struct snd_dice *dice);
198
199 int snd_dice_stream_lock_try(struct snd_dice *dice);
200 void snd_dice_stream_lock_release(struct snd_dice *dice);
201
202 int snd_dice_create_pcm(struct snd_dice *dice);
203
204 int snd_dice_create_hwdep(struct snd_dice *dice);
205
206 void snd_dice_create_proc(struct snd_dice *dice);
207
208 int snd_dice_create_midi(struct snd_dice *dice);
209
210 #endif