]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - sound/firewire/oxfw/oxfw-scs1x.c
ALSA: oxfw: copy handlers of asynchronous transaction for MIDI capture
[mirror_ubuntu-focal-kernel.git] / sound / firewire / oxfw / oxfw-scs1x.c
CommitLineData
3f47152a
TS
1/*
2 * oxfw-scs1x.c - a part of driver for OXFW970/971 based devices
3 *
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Copyright (c) 2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6 *
7 * Licensed under the terms of the GNU General Public License, version 2.
8 */
9
10#include "oxfw.h"
11
e3315b43
TS
12#define HSS1394_ADDRESS 0xc007dedadadaULL
13#define HSS1394_MAX_PACKET_SIZE 64
13b8b78c 14#define HSS1394_TAG_USER_DATA 0x00
e3315b43
TS
15#define HSS1394_TAG_CHANGE_ADDRESS 0xf1
16
17struct fw_scs1x {
18 struct fw_address_handler hss_handler;
13b8b78c
TS
19 u8 input_escape_count;
20 struct snd_rawmidi_substream *input;
e3315b43
TS
21};
22
13b8b78c
TS
23static const u8 sysex_escape_prefix[] = {
24 0xf0, /* SysEx begin */
25 0x00, 0x01, 0x60, /* Stanton DJ */
26 0x48, 0x53, 0x53, /* "HSS" */
27};
28
29static void midi_input_escaped_byte(struct snd_rawmidi_substream *stream,
30 u8 byte)
31{
32 u8 nibbles[2];
33
34 nibbles[0] = byte >> 4;
35 nibbles[1] = byte & 0x0f;
36 snd_rawmidi_receive(stream, nibbles, 2);
37}
38
39static void midi_input_byte(struct fw_scs1x *scs,
40 struct snd_rawmidi_substream *stream, u8 byte)
41{
42 const u8 eox = 0xf7;
43
44 if (scs->input_escape_count > 0) {
45 midi_input_escaped_byte(stream, byte);
46 scs->input_escape_count--;
47 if (scs->input_escape_count == 0)
48 snd_rawmidi_receive(stream, &eox, sizeof(eox));
49 } else if (byte == 0xf9) {
50 snd_rawmidi_receive(stream, sysex_escape_prefix,
51 ARRAY_SIZE(sysex_escape_prefix));
52 midi_input_escaped_byte(stream, 0x00);
53 midi_input_escaped_byte(stream, 0xf9);
54 scs->input_escape_count = 3;
55 } else {
56 snd_rawmidi_receive(stream, &byte, 1);
57 }
58}
59
60static void midi_input_packet(struct fw_scs1x *scs,
61 struct snd_rawmidi_substream *stream,
62 const u8 *data, unsigned int bytes)
63{
64 unsigned int i;
65 const u8 eox = 0xf7;
66
67 if (data[0] == HSS1394_TAG_USER_DATA) {
68 for (i = 1; i < bytes; ++i)
69 midi_input_byte(scs, stream, data[i]);
70 } else {
71 snd_rawmidi_receive(stream, sysex_escape_prefix,
72 ARRAY_SIZE(sysex_escape_prefix));
73 for (i = 0; i < bytes; ++i)
74 midi_input_escaped_byte(stream, data[i]);
75 snd_rawmidi_receive(stream, &eox, sizeof(eox));
76 }
77}
78
e3315b43
TS
79static void handle_hss(struct fw_card *card, struct fw_request *request,
80 int tcode, int destination, int source, int generation,
81 unsigned long long offset, void *data, size_t length,
82 void *callback_data)
83{
13b8b78c
TS
84 struct fw_scs1x *scs = callback_data;
85 struct snd_rawmidi_substream *stream;
86 int rcode;
87
88 if (offset != scs->hss_handler.offset) {
89 rcode = RCODE_ADDRESS_ERROR;
90 goto end;
91 }
92 if (tcode != TCODE_WRITE_QUADLET_REQUEST &&
93 tcode != TCODE_WRITE_BLOCK_REQUEST) {
94 rcode = RCODE_TYPE_ERROR;
95 goto end;
96 }
97
98 if (length >= 1) {
99 stream = ACCESS_ONCE(scs->input);
100 if (stream)
101 midi_input_packet(scs, stream, data, length);
102 }
103
104 rcode = RCODE_COMPLETE;
105end:
106 fw_send_response(card, request, rcode);
e3315b43
TS
107}
108
109static int register_address(struct snd_oxfw *oxfw)
110{
111 struct fw_scs1x *scs = oxfw->spec;
112 __be64 data;
113
114 data = cpu_to_be64(((u64)HSS1394_TAG_CHANGE_ADDRESS << 56) |
115 scs->hss_handler.offset);
116 return snd_fw_transaction(oxfw->unit, TCODE_WRITE_BLOCK_REQUEST,
117 HSS1394_ADDRESS, &data, sizeof(data), 0);
118}
119
120static void remove_scs1x(struct snd_rawmidi *rmidi)
121{
122 struct fw_scs1x *scs = rmidi->private_data;
123
124 fw_core_remove_address_handler(&scs->hss_handler);
125}
126
127void snd_oxfw_scs1x_update(struct snd_oxfw *oxfw)
128{
129 register_address(oxfw);
130}
131
3f47152a
TS
132int snd_oxfw_scs1x_add(struct snd_oxfw *oxfw)
133{
134 struct snd_rawmidi *rmidi;
e3315b43 135 struct fw_scs1x *scs;
3f47152a
TS
136 int err;
137
e3315b43
TS
138 scs = kzalloc(sizeof(struct fw_scs1x), GFP_KERNEL);
139 if (scs == NULL)
140 return -ENOMEM;
141 oxfw->spec = scs;
142
143 /* Allocate own handler for imcoming asynchronous transaction. */
144 scs->hss_handler.length = HSS1394_MAX_PACKET_SIZE;
145 scs->hss_handler.address_callback = handle_hss;
146 scs->hss_handler.callback_data = scs;
147 err = fw_core_add_address_handler(&scs->hss_handler,
148 &fw_high_memory_region);
149 if (err < 0)
150 return err;
151
152 err = register_address(oxfw);
153 if (err < 0)
154 goto err_allocated;
155
3f47152a
TS
156 /* Use unique name for backward compatibility to scs1x module. */
157 err = snd_rawmidi_new(oxfw->card, "SCS.1x", 0, 0, 0, &rmidi);
158 if (err < 0)
e3315b43
TS
159 goto err_allocated;
160 rmidi->private_data = scs;
161 rmidi->private_free = remove_scs1x;
3f47152a
TS
162
163 snprintf(rmidi->name, sizeof(rmidi->name),
164 "%s MIDI", oxfw->card->shortname);
165
e3315b43
TS
166 return 0;
167err_allocated:
168 fw_core_remove_address_handler(&scs->hss_handler);
3f47152a
TS
169 return err;
170}