xref: /OK3568_Linux_fs/kernel/sound/firewire/fireface/ff.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * ff.c - a part of driver for RME Fireface series
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2015-2017 Takashi Sakamoto
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include "ff.h"
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #define OUI_RME	0x000a35
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun MODULE_DESCRIPTION("RME Fireface series Driver");
13*4882a593Smuzhiyun MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
14*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
15*4882a593Smuzhiyun 
name_card(struct snd_ff * ff)16*4882a593Smuzhiyun static void name_card(struct snd_ff *ff)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun 	struct fw_device *fw_dev = fw_parent_device(ff->unit);
19*4882a593Smuzhiyun 	const char *const names[] = {
20*4882a593Smuzhiyun 		[SND_FF_UNIT_VERSION_FF800]	= "Fireface800",
21*4882a593Smuzhiyun 		[SND_FF_UNIT_VERSION_FF400]	= "Fireface400",
22*4882a593Smuzhiyun 		[SND_FF_UNIT_VERSION_UFX]	= "FirefaceUFX",
23*4882a593Smuzhiyun 		[SND_FF_UNIT_VERSION_UCX]	= "FirefaceUCX",
24*4882a593Smuzhiyun 		[SND_FF_UNIT_VERSION_802]	= "Fireface802",
25*4882a593Smuzhiyun 	};
26*4882a593Smuzhiyun 	const char *name;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	name = names[ff->unit_version];
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	strcpy(ff->card->driver, "Fireface");
31*4882a593Smuzhiyun 	strcpy(ff->card->shortname, name);
32*4882a593Smuzhiyun 	strcpy(ff->card->mixername, name);
33*4882a593Smuzhiyun 	snprintf(ff->card->longname, sizeof(ff->card->longname),
34*4882a593Smuzhiyun 		 "RME %s, GUID %08x%08x at %s, S%d", name,
35*4882a593Smuzhiyun 		 fw_dev->config_rom[3], fw_dev->config_rom[4],
36*4882a593Smuzhiyun 		 dev_name(&ff->unit->device), 100 << fw_dev->max_speed);
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun 
ff_card_free(struct snd_card * card)39*4882a593Smuzhiyun static void ff_card_free(struct snd_card *card)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun 	struct snd_ff *ff = card->private_data;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	snd_ff_stream_destroy_duplex(ff);
44*4882a593Smuzhiyun 	snd_ff_transaction_unregister(ff);
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun 
do_registration(struct work_struct * work)47*4882a593Smuzhiyun static void do_registration(struct work_struct *work)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun 	struct snd_ff *ff = container_of(work, struct snd_ff, dwork.work);
50*4882a593Smuzhiyun 	int err;
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	if (ff->registered)
53*4882a593Smuzhiyun 		return;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	err = snd_card_new(&ff->unit->device, -1, NULL, THIS_MODULE, 0,
56*4882a593Smuzhiyun 			   &ff->card);
57*4882a593Smuzhiyun 	if (err < 0)
58*4882a593Smuzhiyun 		return;
59*4882a593Smuzhiyun 	ff->card->private_free = ff_card_free;
60*4882a593Smuzhiyun 	ff->card->private_data = ff;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	err = snd_ff_transaction_register(ff);
63*4882a593Smuzhiyun 	if (err < 0)
64*4882a593Smuzhiyun 		goto error;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	name_card(ff);
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	err = snd_ff_stream_init_duplex(ff);
69*4882a593Smuzhiyun 	if (err < 0)
70*4882a593Smuzhiyun 		goto error;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	snd_ff_proc_init(ff);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	err = snd_ff_create_midi_devices(ff);
75*4882a593Smuzhiyun 	if (err < 0)
76*4882a593Smuzhiyun 		goto error;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	err = snd_ff_create_pcm_devices(ff);
79*4882a593Smuzhiyun 	if (err < 0)
80*4882a593Smuzhiyun 		goto error;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	err = snd_ff_create_hwdep_devices(ff);
83*4882a593Smuzhiyun 	if (err < 0)
84*4882a593Smuzhiyun 		goto error;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	err = snd_card_register(ff->card);
87*4882a593Smuzhiyun 	if (err < 0)
88*4882a593Smuzhiyun 		goto error;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	ff->registered = true;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	return;
93*4882a593Smuzhiyun error:
94*4882a593Smuzhiyun 	snd_card_free(ff->card);
95*4882a593Smuzhiyun 	dev_info(&ff->unit->device,
96*4882a593Smuzhiyun 		 "Sound card registration failed: %d\n", err);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
snd_ff_probe(struct fw_unit * unit,const struct ieee1394_device_id * entry)99*4882a593Smuzhiyun static int snd_ff_probe(struct fw_unit *unit,
100*4882a593Smuzhiyun 			   const struct ieee1394_device_id *entry)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun 	struct snd_ff *ff;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	ff = devm_kzalloc(&unit->device, sizeof(struct snd_ff), GFP_KERNEL);
105*4882a593Smuzhiyun 	if (!ff)
106*4882a593Smuzhiyun 		return -ENOMEM;
107*4882a593Smuzhiyun 	ff->unit = fw_unit_get(unit);
108*4882a593Smuzhiyun 	dev_set_drvdata(&unit->device, ff);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	mutex_init(&ff->mutex);
111*4882a593Smuzhiyun 	spin_lock_init(&ff->lock);
112*4882a593Smuzhiyun 	init_waitqueue_head(&ff->hwdep_wait);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	ff->unit_version = entry->version;
115*4882a593Smuzhiyun 	ff->spec = (const struct snd_ff_spec *)entry->driver_data;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	/* Register this sound card later. */
118*4882a593Smuzhiyun 	INIT_DEFERRABLE_WORK(&ff->dwork, do_registration);
119*4882a593Smuzhiyun 	snd_fw_schedule_registration(unit, &ff->dwork);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	return 0;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
snd_ff_update(struct fw_unit * unit)124*4882a593Smuzhiyun static void snd_ff_update(struct fw_unit *unit)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	struct snd_ff *ff = dev_get_drvdata(&unit->device);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	/* Postpone a workqueue for deferred registration. */
129*4882a593Smuzhiyun 	if (!ff->registered)
130*4882a593Smuzhiyun 		snd_fw_schedule_registration(unit, &ff->dwork);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	snd_ff_transaction_reregister(ff);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	if (ff->registered)
135*4882a593Smuzhiyun 		snd_ff_stream_update_duplex(ff);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
snd_ff_remove(struct fw_unit * unit)138*4882a593Smuzhiyun static void snd_ff_remove(struct fw_unit *unit)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	struct snd_ff *ff = dev_get_drvdata(&unit->device);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	/*
143*4882a593Smuzhiyun 	 * Confirm to stop the work for registration before the sound card is
144*4882a593Smuzhiyun 	 * going to be released. The work is not scheduled again because bus
145*4882a593Smuzhiyun 	 * reset handler is not called anymore.
146*4882a593Smuzhiyun 	 */
147*4882a593Smuzhiyun 	cancel_work_sync(&ff->dwork.work);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	if (ff->registered) {
150*4882a593Smuzhiyun 		// Block till all of ALSA character devices are released.
151*4882a593Smuzhiyun 		snd_card_free(ff->card);
152*4882a593Smuzhiyun 	}
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	mutex_destroy(&ff->mutex);
155*4882a593Smuzhiyun 	fw_unit_put(ff->unit);
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun static const struct snd_ff_spec spec_ff800 = {
159*4882a593Smuzhiyun 	.pcm_capture_channels = {28, 20, 12},
160*4882a593Smuzhiyun 	.pcm_playback_channels = {28, 20, 12},
161*4882a593Smuzhiyun 	.midi_in_ports = 1,
162*4882a593Smuzhiyun 	.midi_out_ports = 1,
163*4882a593Smuzhiyun 	.protocol = &snd_ff_protocol_ff800,
164*4882a593Smuzhiyun 	.midi_high_addr = 0x000200000320ull,
165*4882a593Smuzhiyun 	.midi_addr_range = 12,
166*4882a593Smuzhiyun 	.midi_rx_addrs = {0x000080180000ull, 0},
167*4882a593Smuzhiyun };
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun static const struct snd_ff_spec spec_ff400 = {
170*4882a593Smuzhiyun 	.pcm_capture_channels = {18, 14, 10},
171*4882a593Smuzhiyun 	.pcm_playback_channels = {18, 14, 10},
172*4882a593Smuzhiyun 	.midi_in_ports = 2,
173*4882a593Smuzhiyun 	.midi_out_ports = 2,
174*4882a593Smuzhiyun 	.protocol = &snd_ff_protocol_ff400,
175*4882a593Smuzhiyun 	.midi_high_addr = 0x0000801003f4ull,
176*4882a593Smuzhiyun 	.midi_addr_range = SND_FF_MAXIMIM_MIDI_QUADS * 4,
177*4882a593Smuzhiyun 	.midi_rx_addrs = {0x000080180000ull, 0x000080190000ull},
178*4882a593Smuzhiyun };
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun static const struct snd_ff_spec spec_ucx = {
181*4882a593Smuzhiyun 	.pcm_capture_channels = {18, 14, 12},
182*4882a593Smuzhiyun 	.pcm_playback_channels = {18, 14, 12},
183*4882a593Smuzhiyun 	.midi_in_ports = 2,
184*4882a593Smuzhiyun 	.midi_out_ports = 2,
185*4882a593Smuzhiyun 	.protocol = &snd_ff_protocol_latter,
186*4882a593Smuzhiyun 	.midi_high_addr = 0xffff00000034ull,
187*4882a593Smuzhiyun 	.midi_addr_range = 0x80,
188*4882a593Smuzhiyun 	.midi_rx_addrs = {0xffff00000030ull, 0xffff00000030ull},
189*4882a593Smuzhiyun };
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun static const struct snd_ff_spec spec_ufx_802 = {
192*4882a593Smuzhiyun 	.pcm_capture_channels = {30, 22, 14},
193*4882a593Smuzhiyun 	.pcm_playback_channels = {30, 22, 14},
194*4882a593Smuzhiyun 	.midi_in_ports = 1,
195*4882a593Smuzhiyun 	.midi_out_ports = 1,
196*4882a593Smuzhiyun 	.protocol = &snd_ff_protocol_latter,
197*4882a593Smuzhiyun 	.midi_high_addr = 0xffff00000034ull,
198*4882a593Smuzhiyun 	.midi_addr_range = 0x80,
199*4882a593Smuzhiyun 	.midi_rx_addrs = {0xffff00000030ull, 0xffff00000030ull},
200*4882a593Smuzhiyun };
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun static const struct ieee1394_device_id snd_ff_id_table[] = {
203*4882a593Smuzhiyun 	/* Fireface 800 */
204*4882a593Smuzhiyun 	{
205*4882a593Smuzhiyun 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
206*4882a593Smuzhiyun 				  IEEE1394_MATCH_SPECIFIER_ID |
207*4882a593Smuzhiyun 				  IEEE1394_MATCH_VERSION |
208*4882a593Smuzhiyun 				  IEEE1394_MATCH_MODEL_ID,
209*4882a593Smuzhiyun 		.vendor_id	= OUI_RME,
210*4882a593Smuzhiyun 		.specifier_id	= OUI_RME,
211*4882a593Smuzhiyun 		.version	= SND_FF_UNIT_VERSION_FF800,
212*4882a593Smuzhiyun 		.model_id	= 0x101800,
213*4882a593Smuzhiyun 		.driver_data	= (kernel_ulong_t)&spec_ff800,
214*4882a593Smuzhiyun 	},
215*4882a593Smuzhiyun 	/* Fireface 400 */
216*4882a593Smuzhiyun 	{
217*4882a593Smuzhiyun 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
218*4882a593Smuzhiyun 				  IEEE1394_MATCH_SPECIFIER_ID |
219*4882a593Smuzhiyun 				  IEEE1394_MATCH_VERSION |
220*4882a593Smuzhiyun 				  IEEE1394_MATCH_MODEL_ID,
221*4882a593Smuzhiyun 		.vendor_id	= OUI_RME,
222*4882a593Smuzhiyun 		.specifier_id	= OUI_RME,
223*4882a593Smuzhiyun 		.version	= SND_FF_UNIT_VERSION_FF400,
224*4882a593Smuzhiyun 		.model_id	= 0x101800,
225*4882a593Smuzhiyun 		.driver_data	= (kernel_ulong_t)&spec_ff400,
226*4882a593Smuzhiyun 	},
227*4882a593Smuzhiyun 	// Fireface UFX.
228*4882a593Smuzhiyun 	{
229*4882a593Smuzhiyun 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
230*4882a593Smuzhiyun 				  IEEE1394_MATCH_SPECIFIER_ID |
231*4882a593Smuzhiyun 				  IEEE1394_MATCH_VERSION |
232*4882a593Smuzhiyun 				  IEEE1394_MATCH_MODEL_ID,
233*4882a593Smuzhiyun 		.vendor_id	= OUI_RME,
234*4882a593Smuzhiyun 		.specifier_id	= OUI_RME,
235*4882a593Smuzhiyun 		.version	= SND_FF_UNIT_VERSION_UFX,
236*4882a593Smuzhiyun 		.model_id	= 0x101800,
237*4882a593Smuzhiyun 		.driver_data	= (kernel_ulong_t)&spec_ufx_802,
238*4882a593Smuzhiyun 	},
239*4882a593Smuzhiyun 	// Fireface UCX.
240*4882a593Smuzhiyun 	{
241*4882a593Smuzhiyun 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
242*4882a593Smuzhiyun 				  IEEE1394_MATCH_SPECIFIER_ID |
243*4882a593Smuzhiyun 				  IEEE1394_MATCH_VERSION |
244*4882a593Smuzhiyun 				  IEEE1394_MATCH_MODEL_ID,
245*4882a593Smuzhiyun 		.vendor_id	= OUI_RME,
246*4882a593Smuzhiyun 		.specifier_id	= OUI_RME,
247*4882a593Smuzhiyun 		.version	= SND_FF_UNIT_VERSION_UCX,
248*4882a593Smuzhiyun 		.model_id	= 0x101800,
249*4882a593Smuzhiyun 		.driver_data	= (kernel_ulong_t)&spec_ucx,
250*4882a593Smuzhiyun 	},
251*4882a593Smuzhiyun 	// Fireface 802.
252*4882a593Smuzhiyun 	{
253*4882a593Smuzhiyun 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
254*4882a593Smuzhiyun 				  IEEE1394_MATCH_SPECIFIER_ID |
255*4882a593Smuzhiyun 				  IEEE1394_MATCH_VERSION |
256*4882a593Smuzhiyun 				  IEEE1394_MATCH_MODEL_ID,
257*4882a593Smuzhiyun 		.vendor_id	= OUI_RME,
258*4882a593Smuzhiyun 		.specifier_id	= OUI_RME,
259*4882a593Smuzhiyun 		.version	= SND_FF_UNIT_VERSION_802,
260*4882a593Smuzhiyun 		.model_id	= 0x101800,
261*4882a593Smuzhiyun 		.driver_data	= (kernel_ulong_t)&spec_ufx_802,
262*4882a593Smuzhiyun 	},
263*4882a593Smuzhiyun 	{}
264*4882a593Smuzhiyun };
265*4882a593Smuzhiyun MODULE_DEVICE_TABLE(ieee1394, snd_ff_id_table);
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun static struct fw_driver ff_driver = {
268*4882a593Smuzhiyun 	.driver = {
269*4882a593Smuzhiyun 		.owner	= THIS_MODULE,
270*4882a593Smuzhiyun 		.name	= KBUILD_MODNAME,
271*4882a593Smuzhiyun 		.bus	= &fw_bus_type,
272*4882a593Smuzhiyun 	},
273*4882a593Smuzhiyun 	.probe    = snd_ff_probe,
274*4882a593Smuzhiyun 	.update   = snd_ff_update,
275*4882a593Smuzhiyun 	.remove   = snd_ff_remove,
276*4882a593Smuzhiyun 	.id_table = snd_ff_id_table,
277*4882a593Smuzhiyun };
278*4882a593Smuzhiyun 
snd_ff_init(void)279*4882a593Smuzhiyun static int __init snd_ff_init(void)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun 	return driver_register(&ff_driver.driver);
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun 
snd_ff_exit(void)284*4882a593Smuzhiyun static void __exit snd_ff_exit(void)
285*4882a593Smuzhiyun {
286*4882a593Smuzhiyun 	driver_unregister(&ff_driver.driver);
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun module_init(snd_ff_init);
290*4882a593Smuzhiyun module_exit(snd_ff_exit);
291