xref: /openbmc/linux/sound/firewire/fireface/ff.c (revision 4aea96f4)
1 /*
2  * ff.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 "ff.h"
10 
11 #define OUI_RME	0x000a35
12 
13 MODULE_DESCRIPTION("RME Fireface series Driver");
14 MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
15 MODULE_LICENSE("GPL v2");
16 
17 static void name_card(struct snd_ff *ff)
18 {
19 	struct fw_device *fw_dev = fw_parent_device(ff->unit);
20 
21 	strcpy(ff->card->driver, "Fireface");
22 	strcpy(ff->card->shortname, ff->spec->name);
23 	strcpy(ff->card->mixername, ff->spec->name);
24 	snprintf(ff->card->longname, sizeof(ff->card->longname),
25 		 "RME %s, GUID %08x%08x at %s, S%d", ff->spec->name,
26 		 fw_dev->config_rom[3], fw_dev->config_rom[4],
27 		 dev_name(&ff->unit->device), 100 << fw_dev->max_speed);
28 }
29 
30 static void ff_card_free(struct snd_card *card)
31 {
32 	struct snd_ff *ff = card->private_data;
33 
34 	if (ff->spec->protocol->begin_session)
35 		snd_ff_stream_destroy_duplex(ff);
36 	snd_ff_transaction_unregister(ff);
37 }
38 
39 static void do_registration(struct work_struct *work)
40 {
41 	struct snd_ff *ff = container_of(work, struct snd_ff, dwork.work);
42 	int err;
43 
44 	if (ff->registered)
45 		return;
46 
47 	err = snd_card_new(&ff->unit->device, -1, NULL, THIS_MODULE, 0,
48 			   &ff->card);
49 	if (err < 0)
50 		return;
51 	ff->card->private_free = ff_card_free;
52 	ff->card->private_data = ff;
53 
54 	err = snd_ff_transaction_register(ff);
55 	if (err < 0)
56 		goto error;
57 
58 	name_card(ff);
59 
60 	if (ff->spec->protocol->begin_session) {
61 		err = snd_ff_stream_init_duplex(ff);
62 		if (err < 0)
63 			goto error;
64 	}
65 
66 	snd_ff_proc_init(ff);
67 
68 	err = snd_ff_create_midi_devices(ff);
69 	if (err < 0)
70 		goto error;
71 
72 	if (ff->spec->protocol->begin_session) {
73 		err = snd_ff_create_pcm_devices(ff);
74 		if (err < 0)
75 			goto error;
76 
77 		err = snd_ff_create_hwdep_devices(ff);
78 		if (err < 0)
79 			goto error;
80 	}
81 
82 	err = snd_card_register(ff->card);
83 	if (err < 0)
84 		goto error;
85 
86 	ff->registered = true;
87 
88 	return;
89 error:
90 	snd_card_free(ff->card);
91 	dev_info(&ff->unit->device,
92 		 "Sound card registration failed: %d\n", err);
93 }
94 
95 static int snd_ff_probe(struct fw_unit *unit,
96 			   const struct ieee1394_device_id *entry)
97 {
98 	struct snd_ff *ff;
99 
100 	ff = devm_kzalloc(&unit->device, sizeof(struct snd_ff), GFP_KERNEL);
101 	if (!ff)
102 		return -ENOMEM;
103 	ff->unit = fw_unit_get(unit);
104 	dev_set_drvdata(&unit->device, ff);
105 
106 	mutex_init(&ff->mutex);
107 	spin_lock_init(&ff->lock);
108 	init_waitqueue_head(&ff->hwdep_wait);
109 
110 	ff->spec = (const struct snd_ff_spec *)entry->driver_data;
111 
112 	/* Register this sound card later. */
113 	INIT_DEFERRABLE_WORK(&ff->dwork, do_registration);
114 	snd_fw_schedule_registration(unit, &ff->dwork);
115 
116 	return 0;
117 }
118 
119 static void snd_ff_update(struct fw_unit *unit)
120 {
121 	struct snd_ff *ff = dev_get_drvdata(&unit->device);
122 
123 	/* Postpone a workqueue for deferred registration. */
124 	if (!ff->registered)
125 		snd_fw_schedule_registration(unit, &ff->dwork);
126 
127 	snd_ff_transaction_reregister(ff);
128 
129 	if (ff->registered && ff->spec->protocol->begin_session)
130 		snd_ff_stream_update_duplex(ff);
131 }
132 
133 static void snd_ff_remove(struct fw_unit *unit)
134 {
135 	struct snd_ff *ff = dev_get_drvdata(&unit->device);
136 
137 	/*
138 	 * Confirm to stop the work for registration before the sound card is
139 	 * going to be released. The work is not scheduled again because bus
140 	 * reset handler is not called anymore.
141 	 */
142 	cancel_work_sync(&ff->dwork.work);
143 
144 	if (ff->registered) {
145 		// Block till all of ALSA character devices are released.
146 		snd_card_free(ff->card);
147 	}
148 
149 	mutex_destroy(&ff->mutex);
150 	fw_unit_put(ff->unit);
151 }
152 
153 static const struct snd_ff_spec spec_ff800 = {
154 	.name = "Fireface800",
155 	.midi_in_ports = 1,
156 	.midi_out_ports = 1,
157 	.protocol = &snd_ff_protocol_ff800,
158 	.regs = {
159 		[SND_FF_REG_TYPE_MIDI_HIGH_ADDR] = 0x000200000320ull,
160 	},
161 };
162 
163 static const struct snd_ff_spec spec_ff400 = {
164 	.name = "Fireface400",
165 	.pcm_capture_channels = {18, 14, 10},
166 	.pcm_playback_channels = {18, 14, 10},
167 	.midi_in_ports = 2,
168 	.midi_out_ports = 2,
169 	.protocol = &snd_ff_protocol_ff400,
170 	.regs = {
171 		[SND_FF_REG_TYPE_MIDI_HIGH_ADDR] = 0x0000801003f4ull,
172 	},
173 };
174 
175 static const struct ieee1394_device_id snd_ff_id_table[] = {
176 	/* Fireface 800 */
177 	{
178 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
179 				  IEEE1394_MATCH_SPECIFIER_ID |
180 				  IEEE1394_MATCH_VERSION |
181 				  IEEE1394_MATCH_MODEL_ID,
182 		.vendor_id	= OUI_RME,
183 		.specifier_id	= OUI_RME,
184 		.version	= 0x000001,
185 		.model_id	= 0x101800,
186 		.driver_data	= (kernel_ulong_t)&spec_ff800,
187 	},
188 	/* Fireface 400 */
189 	{
190 		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
191 				  IEEE1394_MATCH_SPECIFIER_ID |
192 				  IEEE1394_MATCH_VERSION |
193 				  IEEE1394_MATCH_MODEL_ID,
194 		.vendor_id	= OUI_RME,
195 		.specifier_id	= OUI_RME,
196 		.version	= 0x000002,
197 		.model_id	= 0x101800,
198 		.driver_data	= (kernel_ulong_t)&spec_ff400,
199 	},
200 	{}
201 };
202 MODULE_DEVICE_TABLE(ieee1394, snd_ff_id_table);
203 
204 static struct fw_driver ff_driver = {
205 	.driver = {
206 		.owner	= THIS_MODULE,
207 		.name	= "snd-fireface",
208 		.bus	= &fw_bus_type,
209 	},
210 	.probe    = snd_ff_probe,
211 	.update   = snd_ff_update,
212 	.remove   = snd_ff_remove,
213 	.id_table = snd_ff_id_table,
214 };
215 
216 static int __init snd_ff_init(void)
217 {
218 	return driver_register(&ff_driver.driver);
219 }
220 
221 static void __exit snd_ff_exit(void)
222 {
223 	driver_unregister(&ff_driver.driver);
224 }
225 
226 module_init(snd_ff_init);
227 module_exit(snd_ff_exit);
228