xref: /openbmc/linux/sound/firewire/tascam/tascam.c (revision 32daa5d7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * tascam.c - a part of driver for TASCAM FireWire series
4  *
5  * Copyright (c) 2015 Takashi Sakamoto
6  */
7 
8 #include "tascam.h"
9 
10 MODULE_DESCRIPTION("TASCAM FireWire series Driver");
11 MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
12 MODULE_LICENSE("GPL v2");
13 
14 static const struct snd_tscm_spec model_specs[] = {
15 	{
16 		.name = "FW-1884",
17 		.has_adat = true,
18 		.has_spdif = true,
19 		.pcm_capture_analog_channels = 8,
20 		.pcm_playback_analog_channels = 8,
21 		.midi_capture_ports = 4,
22 		.midi_playback_ports = 4,
23 	},
24 	{
25 		.name = "FW-1082",
26 		.has_adat = false,
27 		.has_spdif = true,
28 		.pcm_capture_analog_channels = 8,
29 		.pcm_playback_analog_channels = 2,
30 		.midi_capture_ports = 2,
31 		.midi_playback_ports = 2,
32 	},
33 	{
34 		.name = "FW-1804",
35 		.has_adat = true,
36 		.has_spdif = true,
37 		.pcm_capture_analog_channels = 8,
38 		.pcm_playback_analog_channels = 2,
39 		.midi_capture_ports = 2,
40 		.midi_playback_ports = 4,
41 	},
42 };
43 
44 static int identify_model(struct snd_tscm *tscm)
45 {
46 	struct fw_device *fw_dev = fw_parent_device(tscm->unit);
47 	const u32 *config_rom = fw_dev->config_rom;
48 	char model[9];
49 	unsigned int i;
50 	u8 c;
51 
52 	if (fw_dev->config_rom_length < 30) {
53 		dev_err(&tscm->unit->device,
54 			"Configuration ROM is too short.\n");
55 		return -ENODEV;
56 	}
57 
58 	/* Pick up model name from certain addresses. */
59 	for (i = 0; i < 8; i++) {
60 		c = config_rom[28 + i / 4] >> (24 - 8 * (i % 4));
61 		if (c == '\0')
62 			break;
63 		model[i] = c;
64 	}
65 	model[i] = '\0';
66 
67 	for (i = 0; i < ARRAY_SIZE(model_specs); i++) {
68 		if (strcmp(model, model_specs[i].name) == 0) {
69 			tscm->spec = &model_specs[i];
70 			break;
71 		}
72 	}
73 	if (tscm->spec == NULL)
74 		return -ENODEV;
75 
76 	strcpy(tscm->card->driver, "FW-TASCAM");
77 	strcpy(tscm->card->shortname, model);
78 	strcpy(tscm->card->mixername, model);
79 	snprintf(tscm->card->longname, sizeof(tscm->card->longname),
80 		 "TASCAM %s, GUID %08x%08x at %s, S%d", model,
81 		 fw_dev->config_rom[3], fw_dev->config_rom[4],
82 		 dev_name(&tscm->unit->device), 100 << fw_dev->max_speed);
83 
84 	return 0;
85 }
86 
87 static void tscm_card_free(struct snd_card *card)
88 {
89 	struct snd_tscm *tscm = card->private_data;
90 
91 	snd_tscm_transaction_unregister(tscm);
92 	snd_tscm_stream_destroy_duplex(tscm);
93 }
94 
95 static void do_registration(struct work_struct *work)
96 {
97 	struct snd_tscm *tscm = container_of(work, struct snd_tscm, dwork.work);
98 	int err;
99 
100 	err = snd_card_new(&tscm->unit->device, -1, NULL, THIS_MODULE, 0,
101 			   &tscm->card);
102 	if (err < 0)
103 		return;
104 	tscm->card->private_free = tscm_card_free;
105 	tscm->card->private_data = tscm;
106 
107 	err = identify_model(tscm);
108 	if (err < 0)
109 		goto error;
110 
111 	err = snd_tscm_transaction_register(tscm);
112 	if (err < 0)
113 		goto error;
114 
115 	err = snd_tscm_stream_init_duplex(tscm);
116 	if (err < 0)
117 		goto error;
118 
119 	snd_tscm_proc_init(tscm);
120 
121 	err = snd_tscm_create_pcm_devices(tscm);
122 	if (err < 0)
123 		goto error;
124 
125 	err = snd_tscm_create_midi_devices(tscm);
126 	if (err < 0)
127 		goto error;
128 
129 	err = snd_tscm_create_hwdep_device(tscm);
130 	if (err < 0)
131 		goto error;
132 
133 	err = snd_card_register(tscm->card);
134 	if (err < 0)
135 		goto error;
136 
137 	tscm->registered = true;
138 
139 	return;
140 error:
141 	snd_card_free(tscm->card);
142 	dev_info(&tscm->unit->device,
143 		 "Sound card registration failed: %d\n", err);
144 }
145 
146 static int snd_tscm_probe(struct fw_unit *unit,
147 			   const struct ieee1394_device_id *entry)
148 {
149 	struct snd_tscm *tscm;
150 
151 	/* Allocate this independent of sound card instance. */
152 	tscm = devm_kzalloc(&unit->device, sizeof(struct snd_tscm), GFP_KERNEL);
153 	if (!tscm)
154 		return -ENOMEM;
155 	tscm->unit = fw_unit_get(unit);
156 	dev_set_drvdata(&unit->device, tscm);
157 
158 	mutex_init(&tscm->mutex);
159 	spin_lock_init(&tscm->lock);
160 	init_waitqueue_head(&tscm->hwdep_wait);
161 
162 	/* Allocate and register this sound card later. */
163 	INIT_DEFERRABLE_WORK(&tscm->dwork, do_registration);
164 	snd_fw_schedule_registration(unit, &tscm->dwork);
165 
166 	return 0;
167 }
168 
169 static void snd_tscm_update(struct fw_unit *unit)
170 {
171 	struct snd_tscm *tscm = dev_get_drvdata(&unit->device);
172 
173 	/* Postpone a workqueue for deferred registration. */
174 	if (!tscm->registered)
175 		snd_fw_schedule_registration(unit, &tscm->dwork);
176 
177 	snd_tscm_transaction_reregister(tscm);
178 
179 	/*
180 	 * After registration, userspace can start packet streaming, then this
181 	 * code block works fine.
182 	 */
183 	if (tscm->registered) {
184 		mutex_lock(&tscm->mutex);
185 		snd_tscm_stream_update_duplex(tscm);
186 		mutex_unlock(&tscm->mutex);
187 	}
188 }
189 
190 static void snd_tscm_remove(struct fw_unit *unit)
191 {
192 	struct snd_tscm *tscm = dev_get_drvdata(&unit->device);
193 
194 	/*
195 	 * Confirm to stop the work for registration before the sound card is
196 	 * going to be released. The work is not scheduled again because bus
197 	 * reset handler is not called anymore.
198 	 */
199 	cancel_delayed_work_sync(&tscm->dwork);
200 
201 	if (tscm->registered) {
202 		// Block till all of ALSA character devices are released.
203 		snd_card_free(tscm->card);
204 	}
205 
206 	mutex_destroy(&tscm->mutex);
207 	fw_unit_put(tscm->unit);
208 }
209 
210 static const struct ieee1394_device_id snd_tscm_id_table[] = {
211 	// Tascam, FW-1884.
212 	{
213 		.match_flags = IEEE1394_MATCH_VENDOR_ID |
214 			       IEEE1394_MATCH_SPECIFIER_ID |
215 			       IEEE1394_MATCH_VERSION,
216 		.vendor_id = 0x00022e,
217 		.specifier_id = 0x00022e,
218 		.version = 0x800000,
219 	},
220 	// Tascam, FE-8 (.version = 0x800001)
221 	// This kernel module doesn't support FE-8 because the most of features
222 	// can be implemented in userspace without any specific support of this
223 	// module.
224 	//
225 	// .version = 0x800002 is unknown.
226 	//
227 	// Tascam, FW-1082.
228 	{
229 		.match_flags = IEEE1394_MATCH_VENDOR_ID |
230 			       IEEE1394_MATCH_SPECIFIER_ID |
231 			       IEEE1394_MATCH_VERSION,
232 		.vendor_id = 0x00022e,
233 		.specifier_id = 0x00022e,
234 		.version = 0x800003,
235 	},
236 	// Tascam, FW-1804.
237 	{
238 		.match_flags = IEEE1394_MATCH_VENDOR_ID |
239 			       IEEE1394_MATCH_SPECIFIER_ID |
240 			       IEEE1394_MATCH_VERSION,
241 		.vendor_id = 0x00022e,
242 		.specifier_id = 0x00022e,
243 		.version = 0x800004,
244 	},
245 	{}
246 };
247 MODULE_DEVICE_TABLE(ieee1394, snd_tscm_id_table);
248 
249 static struct fw_driver tscm_driver = {
250 	.driver = {
251 		.owner = THIS_MODULE,
252 		.name = KBUILD_MODNAME,
253 		.bus = &fw_bus_type,
254 	},
255 	.probe    = snd_tscm_probe,
256 	.update   = snd_tscm_update,
257 	.remove   = snd_tscm_remove,
258 	.id_table = snd_tscm_id_table,
259 };
260 
261 static int __init snd_tscm_init(void)
262 {
263 	return driver_register(&tscm_driver.driver);
264 }
265 
266 static void __exit snd_tscm_exit(void)
267 {
268 	driver_unregister(&tscm_driver.driver);
269 }
270 
271 module_init(snd_tscm_init);
272 module_exit(snd_tscm_exit);
273