]> asedeno.scripts.mit.edu Git - linux.git/blob - sound/firewire/fireface/ff-protocol-ff400.c
d2fbb0382223e291518d7172f37b92f2a0bb1b85
[linux.git] / sound / firewire / fireface / ff-protocol-ff400.c
1 /*
2  * ff-protocol-ff400.c - a part of driver for RME Fireface series
3  *
4  * Copyright (c) 2015-2017 Takashi Sakamoto
5  *
6  * Licensed under the terms of the GNU General Public License, version 2.
7  */
8
9 #include <linux/delay.h>
10 #include "ff.h"
11
12 #define FF400_STF               0x000080100500ull
13 #define FF400_RX_PACKET_FORMAT  0x000080100504ull
14 #define FF400_ISOC_COMM_START   0x000080100508ull
15 #define FF400_TX_PACKET_FORMAT  0x00008010050cull
16 #define FF400_ISOC_COMM_STOP    0x000080100510ull
17
18 #define FF400_MIDI_HIGH_ADDR    0x0000801003f4ull
19 #define FF400_MIDI_RX_PORT_0    0x000080180000ull
20 #define FF400_MIDI_RX_PORT_1    0x000080190000ull
21
22 static int ff400_begin_session(struct snd_ff *ff, unsigned int rate)
23 {
24         __le32 reg;
25         int i, err;
26
27         /* Check whether the given value is supported or not. */
28         for (i = 0; i < CIP_SFC_COUNT; i++) {
29                 if (amdtp_rate_table[i] == rate)
30                         break;
31         }
32         if (i == CIP_SFC_COUNT)
33                 return -EINVAL;
34
35         /* Set the number of data blocks transferred in a second. */
36         reg = cpu_to_le32(rate);
37         err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
38                                  FF400_STF, &reg, sizeof(reg), 0);
39         if (err < 0)
40                 return err;
41
42         msleep(100);
43
44         /*
45          * Set isochronous channel and the number of quadlets of received
46          * packets.
47          */
48         reg = cpu_to_le32(((ff->rx_stream.data_block_quadlets << 3) << 8) |
49                           ff->rx_resources.channel);
50         err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
51                                  FF400_RX_PACKET_FORMAT, &reg, sizeof(reg), 0);
52         if (err < 0)
53                 return err;
54
55         /*
56          * Set isochronous channel and the number of quadlets of transmitted
57          * packet.
58          */
59         /* TODO: investigate the purpose of this 0x80. */
60         reg = cpu_to_le32((0x80 << 24) |
61                           (ff->tx_resources.channel << 5) |
62                           (ff->tx_stream.data_block_quadlets));
63         err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
64                                  FF400_TX_PACKET_FORMAT, &reg, sizeof(reg), 0);
65         if (err < 0)
66                 return err;
67
68         /* Allow to transmit packets. */
69         reg = cpu_to_le32(0x00000001);
70         return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
71                                  FF400_ISOC_COMM_START, &reg, sizeof(reg), 0);
72 }
73
74 static void ff400_finish_session(struct snd_ff *ff)
75 {
76         __le32 reg;
77
78         reg = cpu_to_le32(0x80000000);
79         snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
80                            FF400_ISOC_COMM_STOP, &reg, sizeof(reg), 0);
81 }
82
83 static int ff400_switch_fetching_mode(struct snd_ff *ff, bool enable)
84 {
85         __le32 *reg;
86         int i;
87         int err;
88
89         reg = kcalloc(18, sizeof(__le32), GFP_KERNEL);
90         if (reg == NULL)
91                 return -ENOMEM;
92
93         if (enable) {
94                 /*
95                  * Each quadlet is corresponding to data channels in a data
96                  * blocks in reverse order. Precisely, quadlets for available
97                  * data channels should be enabled. Here, I take second best
98                  * to fetch PCM frames from all of data channels regardless of
99                  * stf.
100                  */
101                 for (i = 0; i < 18; ++i)
102                         reg[i] = cpu_to_le32(0x00000001);
103         }
104
105         err = snd_fw_transaction(ff->unit, TCODE_WRITE_BLOCK_REQUEST,
106                                  SND_FF_REG_FETCH_PCM_FRAMES, reg,
107                                  sizeof(__le32) * 18, 0);
108         kfree(reg);
109         return err;
110 }
111
112 const struct snd_ff_protocol snd_ff_protocol_ff400 = {
113         .begin_session          = ff400_begin_session,
114         .finish_session         = ff400_finish_session,
115         .switch_fetching_mode   = ff400_switch_fetching_mode,
116
117         .midi_high_addr_reg     = FF400_MIDI_HIGH_ADDR,
118         .midi_rx_port_0_reg     = FF400_MIDI_RX_PORT_0,
119         .midi_rx_port_1_reg     = FF400_MIDI_RX_PORT_1,
120 };