1247cca04f63d940627410937626a4c18a99aab1
[firefly-linux-kernel-4.4.55.git] / sound / firewire / bebob / bebob.h
1 /*
2  * bebob.h - a part of driver for BeBoB based devices
3  *
4  * Copyright (c) 2013-2014 Takashi Sakamoto
5  *
6  * Licensed under the terms of the GNU General Public License, version 2.
7  */
8
9 #ifndef SOUND_BEBOB_H_INCLUDED
10 #define SOUND_BEBOB_H_INCLUDED
11
12 #include <linux/compat.h>
13 #include <linux/device.h>
14 #include <linux/firewire.h>
15 #include <linux/firewire-constants.h>
16 #include <linux/module.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/delay.h>
19 #include <linux/slab.h>
20
21 #include <sound/core.h>
22 #include <sound/initval.h>
23 #include <sound/info.h>
24
25 #include "../lib.h"
26 #include "../fcp.h"
27 #include "../packets-buffer.h"
28 #include "../iso-resources.h"
29 #include "../amdtp.h"
30 #include "../cmp.h"
31
32 /* basic register addresses on DM1000/DM1100/DM1500 */
33 #define BEBOB_ADDR_REG_INFO     0xffffc8020000
34 #define BEBOB_ADDR_REG_REQ      0xffffc8021000
35
36 struct snd_bebob;
37
38 #define SND_BEBOB_STRM_FMT_ENTRIES      7
39 struct snd_bebob_stream_formation {
40         unsigned int pcm;
41         unsigned int midi;
42 };
43 /* this is a lookup table for index of stream formations */
44 extern const unsigned int snd_bebob_rate_table[SND_BEBOB_STRM_FMT_ENTRIES];
45
46 struct snd_bebob {
47         struct snd_card *card;
48         struct fw_unit *unit;
49         int card_index;
50
51         struct mutex mutex;
52         spinlock_t lock;
53
54         unsigned int midi_input_ports;
55         unsigned int midi_output_ports;
56
57         /* for bus reset quirk */
58         struct completion bus_reset;
59         bool connected;
60
61         struct amdtp_stream *master;
62         struct amdtp_stream tx_stream;
63         struct amdtp_stream rx_stream;
64         struct cmp_connection out_conn;
65         struct cmp_connection in_conn;
66         atomic_t capture_substreams;
67         atomic_t playback_substreams;
68
69         struct snd_bebob_stream_formation
70                 tx_stream_formations[SND_BEBOB_STRM_FMT_ENTRIES];
71         struct snd_bebob_stream_formation
72                 rx_stream_formations[SND_BEBOB_STRM_FMT_ENTRIES];
73
74         int sync_input_plug;
75 };
76
77 static inline int
78 snd_bebob_read_block(struct fw_unit *unit, u64 addr, void *buf, int size)
79 {
80         return snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
81                                   BEBOB_ADDR_REG_INFO + addr,
82                                   buf, size, 0);
83 }
84
85 static inline int
86 snd_bebob_read_quad(struct fw_unit *unit, u64 addr, u32 *buf)
87 {
88         return snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
89                                   BEBOB_ADDR_REG_INFO + addr,
90                                   (void *)buf, sizeof(u32), 0);
91 }
92
93 /*
94  * AVC command extensions, AV/C Unit and Subunit, Revision 17
95  * (Nov 2003, BridgeCo)
96  */
97 #define AVC_BRIDGECO_ADDR_BYTES 6
98 enum avc_bridgeco_plug_dir {
99         AVC_BRIDGECO_PLUG_DIR_IN        = 0x00,
100         AVC_BRIDGECO_PLUG_DIR_OUT       = 0x01
101 };
102 enum avc_bridgeco_plug_mode {
103         AVC_BRIDGECO_PLUG_MODE_UNIT             = 0x00,
104         AVC_BRIDGECO_PLUG_MODE_SUBUNIT          = 0x01,
105         AVC_BRIDGECO_PLUG_MODE_FUNCTION_BLOCK   = 0x02
106 };
107 enum avc_bridgeco_plug_unit {
108         AVC_BRIDGECO_PLUG_UNIT_ISOC     = 0x00,
109         AVC_BRIDGECO_PLUG_UNIT_EXT      = 0x01,
110         AVC_BRIDGECO_PLUG_UNIT_ASYNC    = 0x02
111 };
112 enum avc_bridgeco_plug_type {
113         AVC_BRIDGECO_PLUG_TYPE_ISOC     = 0x00,
114         AVC_BRIDGECO_PLUG_TYPE_ASYNC    = 0x01,
115         AVC_BRIDGECO_PLUG_TYPE_MIDI     = 0x02,
116         AVC_BRIDGECO_PLUG_TYPE_SYNC     = 0x03,
117         AVC_BRIDGECO_PLUG_TYPE_ANA      = 0x04,
118         AVC_BRIDGECO_PLUG_TYPE_DIG      = 0x05
119 };
120 static inline void
121 avc_bridgeco_fill_unit_addr(u8 buf[AVC_BRIDGECO_ADDR_BYTES],
122                             enum avc_bridgeco_plug_dir dir,
123                             enum avc_bridgeco_plug_unit unit,
124                             unsigned int pid)
125 {
126         buf[0] = 0xff;  /* Unit */
127         buf[1] = dir;
128         buf[2] = AVC_BRIDGECO_PLUG_MODE_UNIT;
129         buf[3] = unit;
130         buf[4] = 0xff & pid;
131         buf[5] = 0xff;  /* reserved */
132 }
133 static inline void
134 avc_bridgeco_fill_msu_addr(u8 buf[AVC_BRIDGECO_ADDR_BYTES],
135                            enum avc_bridgeco_plug_dir dir,
136                            unsigned int pid)
137 {
138         buf[0] = 0x60;  /* Music subunit */
139         buf[1] = dir;
140         buf[2] = AVC_BRIDGECO_PLUG_MODE_SUBUNIT;
141         buf[3] = 0xff & pid;
142         buf[4] = 0xff;  /* reserved */
143         buf[5] = 0xff;  /* reserved */
144 }
145 int avc_bridgeco_get_plug_ch_pos(struct fw_unit *unit,
146                                  u8 addr[AVC_BRIDGECO_ADDR_BYTES],
147                                  u8 *buf, unsigned int len);
148 int avc_bridgeco_get_plug_type(struct fw_unit *unit,
149                                u8 addr[AVC_BRIDGECO_ADDR_BYTES],
150                                enum avc_bridgeco_plug_type *type);
151 int avc_bridgeco_get_plug_section_type(struct fw_unit *unit,
152                                        u8 addr[AVC_BRIDGECO_ADDR_BYTES],
153                                        unsigned int id, u8 *type);
154 int avc_bridgeco_get_plug_input(struct fw_unit *unit,
155                                 u8 addr[AVC_BRIDGECO_ADDR_BYTES],
156                                 u8 input[7]);
157 int avc_bridgeco_get_plug_strm_fmt(struct fw_unit *unit,
158                                    u8 addr[AVC_BRIDGECO_ADDR_BYTES], u8 *buf,
159                                    unsigned int *len, unsigned int eid);
160
161 /* for AMDTP streaming */
162 int snd_bebob_stream_get_rate(struct snd_bebob *bebob, unsigned int *rate);
163 int snd_bebob_stream_set_rate(struct snd_bebob *bebob, unsigned int rate);
164 int snd_bebob_stream_check_internal_clock(struct snd_bebob *bebob,
165                                           bool *internal);
166 int snd_bebob_stream_discover(struct snd_bebob *bebob);
167 int snd_bebob_stream_map(struct snd_bebob *bebob,
168                          struct amdtp_stream *stream);
169 int snd_bebob_stream_init_duplex(struct snd_bebob *bebob);
170 int snd_bebob_stream_start_duplex(struct snd_bebob *bebob, int rate);
171 void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob);
172 void snd_bebob_stream_update_duplex(struct snd_bebob *bebob);
173 void snd_bebob_stream_destroy_duplex(struct snd_bebob *bebob);
174
175 void snd_bebob_proc_init(struct snd_bebob *bebob);
176
177 #define SND_BEBOB_DEV_ENTRY(vendor, model) \
178 { \
179         .match_flags    = IEEE1394_MATCH_VENDOR_ID | \
180                           IEEE1394_MATCH_MODEL_ID, \
181         .vendor_id      = vendor, \
182         .model_id       = model, \
183 }
184
185 #endif