1 /*
2  * fireworks.c - a part of driver for Fireworks based devices
3  *
4  * Copyright (c) 2009-2010 Clemens Ladisch
5  * Copyright (c) 2013-2014 Takashi Sakamoto
6  *
7  * Licensed under the terms of the GNU General Public License, version 2.
8  */
9 
10 /*
11  * Fireworks is a board module which Echo Audio produced. This module consists
12  * of three chipsets:
13  *  - Communication chipset for IEEE1394 PHY/Link and IEC 61883-1/6
14  *  - DSP or/and FPGA for signal processing
15  *  - Flash Memory to store firmwares
16  */
17 
18 #include "fireworks.h"
19 
20 MODULE_DESCRIPTION("Echo Fireworks driver");
21 MODULE_AUTHOR("Takashi Sakamoto <o-takashi@sakamocchi.jp>");
22 MODULE_LICENSE("GPL v2");
23 
24 static int index[SNDRV_CARDS]	= SNDRV_DEFAULT_IDX;
25 static char *id[SNDRV_CARDS]	= SNDRV_DEFAULT_STR;
26 static bool enable[SNDRV_CARDS]	= SNDRV_DEFAULT_ENABLE_PNP;
27 unsigned int snd_efw_resp_buf_size	= 1024;
28 bool snd_efw_resp_buf_debug		= false;
29 
30 module_param_array(index, int, NULL, 0444);
31 MODULE_PARM_DESC(index, "card index");
32 module_param_array(id, charp, NULL, 0444);
33 MODULE_PARM_DESC(id, "ID string");
34 module_param_array(enable, bool, NULL, 0444);
35 MODULE_PARM_DESC(enable, "enable Fireworks sound card");
36 module_param_named(resp_buf_size, snd_efw_resp_buf_size, uint, 0444);
37 MODULE_PARM_DESC(resp_buf_size,
38 		 "response buffer size (max 4096, default 1024)");
39 module_param_named(resp_buf_debug, snd_efw_resp_buf_debug, bool, 0444);
40 MODULE_PARM_DESC(resp_buf_debug, "store all responses to buffer");
41 
42 static DEFINE_MUTEX(devices_mutex);
43 static DECLARE_BITMAP(devices_used, SNDRV_CARDS);
44 
45 #define VENDOR_LOUD			0x000ff2
46 #define  MODEL_MACKIE_400F		0x00400f
47 #define  MODEL_MACKIE_1200F		0x01200f
48 
49 #define VENDOR_ECHO			0x001486
50 #define  MODEL_ECHO_AUDIOFIRE_12	0x00af12
51 #define  MODEL_ECHO_AUDIOFIRE_12HD	0x0af12d
52 #define  MODEL_ECHO_AUDIOFIRE_12_APPLE	0x0af12a
53 /* This is applied for AudioFire8 (until 2009 July) */
54 #define  MODEL_ECHO_AUDIOFIRE_8		0x000af8
55 #define  MODEL_ECHO_AUDIOFIRE_2		0x000af2
56 #define  MODEL_ECHO_AUDIOFIRE_4		0x000af4
57 /* AudioFire9 is applied for AudioFire8(since 2009 July) and AudioFirePre8 */
58 #define  MODEL_ECHO_AUDIOFIRE_9		0x000af9
59 /* unknown as product */
60 #define  MODEL_ECHO_FIREWORKS_8		0x0000f8
61 #define  MODEL_ECHO_FIREWORKS_HDMI	0x00afd1
62 
63 #define VENDOR_GIBSON			0x00075b
64 /* for Robot Interface Pack of Dark Fire, Dusk Tiger, Les Paul Standard 2010 */
65 #define  MODEL_GIBSON_RIP		0x00afb2
66 /* unknown as product */
67 #define  MODEL_GIBSON_GOLDTOP		0x00afb9
68 
69 /* part of hardware capability flags */
70 #define FLAG_RESP_ADDR_CHANGABLE	0
71 
72 static int
73 get_hardware_info(struct snd_efw *efw)
74 {
75 	struct fw_device *fw_dev = fw_parent_device(efw->unit);
76 	struct snd_efw_hwinfo *hwinfo;
77 	char version[12] = {0};
78 	int err;
79 
80 	hwinfo = kzalloc(sizeof(struct snd_efw_hwinfo), GFP_KERNEL);
81 	if (hwinfo == NULL)
82 		return -ENOMEM;
83 
84 	err = snd_efw_command_get_hwinfo(efw, hwinfo);
85 	if (err < 0)
86 		goto end;
87 
88 	/* firmware version for communication chipset */
89 	snprintf(version, sizeof(version), "%u.%u",
90 		 (hwinfo->arm_version >> 24) & 0xff,
91 		 (hwinfo->arm_version >> 16) & 0xff);
92 	efw->firmware_version = hwinfo->arm_version;
93 
94 	strcpy(efw->card->driver, "Fireworks");
95 	strcpy(efw->card->shortname, hwinfo->model_name);
96 	strcpy(efw->card->mixername, hwinfo->model_name);
97 	snprintf(efw->card->longname, sizeof(efw->card->longname),
98 		 "%s %s v%s, GUID %08x%08x at %s, S%d",
99 		 hwinfo->vendor_name, hwinfo->model_name, version,
100 		 hwinfo->guid_hi, hwinfo->guid_lo,
101 		 dev_name(&efw->unit->device), 100 << fw_dev->max_speed);
102 
103 	if (hwinfo->flags & BIT(FLAG_RESP_ADDR_CHANGABLE))
104 		efw->resp_addr_changable = true;
105 
106 	efw->supported_sampling_rate = 0;
107 	if ((hwinfo->min_sample_rate <= 22050)
108 	 && (22050 <= hwinfo->max_sample_rate))
109 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_22050;
110 	if ((hwinfo->min_sample_rate <= 32000)
111 	 && (32000 <= hwinfo->max_sample_rate))
112 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_32000;
113 	if ((hwinfo->min_sample_rate <= 44100)
114 	 && (44100 <= hwinfo->max_sample_rate))
115 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_44100;
116 	if ((hwinfo->min_sample_rate <= 48000)
117 	 && (48000 <= hwinfo->max_sample_rate))
118 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_48000;
119 	if ((hwinfo->min_sample_rate <= 88200)
120 	 && (88200 <= hwinfo->max_sample_rate))
121 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_88200;
122 	if ((hwinfo->min_sample_rate <= 96000)
123 	 && (96000 <= hwinfo->max_sample_rate))
124 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_96000;
125 	if ((hwinfo->min_sample_rate <= 176400)
126 	 && (176400 <= hwinfo->max_sample_rate))
127 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_176400;
128 	if ((hwinfo->min_sample_rate <= 192000)
129 	 && (192000 <= hwinfo->max_sample_rate))
130 		efw->supported_sampling_rate |= SNDRV_PCM_RATE_192000;
131 
132 	/* the number of MIDI ports, not of MIDI conformant data channels */
133 	if (hwinfo->midi_out_ports > SND_EFW_MAX_MIDI_OUT_PORTS ||
134 	    hwinfo->midi_in_ports > SND_EFW_MAX_MIDI_IN_PORTS) {
135 		err = -EIO;
136 		goto end;
137 	}
138 	efw->midi_out_ports = hwinfo->midi_out_ports;
139 	efw->midi_in_ports = hwinfo->midi_in_ports;
140 
141 	if (hwinfo->amdtp_tx_pcm_channels    > AMDTP_MAX_CHANNELS_FOR_PCM ||
142 	    hwinfo->amdtp_tx_pcm_channels_2x > AMDTP_MAX_CHANNELS_FOR_PCM ||
143 	    hwinfo->amdtp_tx_pcm_channels_4x > AMDTP_MAX_CHANNELS_FOR_PCM ||
144 	    hwinfo->amdtp_rx_pcm_channels    > AMDTP_MAX_CHANNELS_FOR_PCM ||
145 	    hwinfo->amdtp_rx_pcm_channels_2x > AMDTP_MAX_CHANNELS_FOR_PCM ||
146 	    hwinfo->amdtp_rx_pcm_channels_4x > AMDTP_MAX_CHANNELS_FOR_PCM) {
147 		err = -ENOSYS;
148 		goto end;
149 	}
150 	efw->pcm_capture_channels[0] = hwinfo->amdtp_tx_pcm_channels;
151 	efw->pcm_capture_channels[1] = hwinfo->amdtp_tx_pcm_channels_2x;
152 	efw->pcm_capture_channels[2] = hwinfo->amdtp_tx_pcm_channels_4x;
153 	efw->pcm_playback_channels[0] = hwinfo->amdtp_rx_pcm_channels;
154 	efw->pcm_playback_channels[1] = hwinfo->amdtp_rx_pcm_channels_2x;
155 	efw->pcm_playback_channels[2] = hwinfo->amdtp_rx_pcm_channels_4x;
156 
157 	/* Hardware metering. */
158 	if (hwinfo->phys_in_grp_count  > HWINFO_MAX_CAPS_GROUPS ||
159 	    hwinfo->phys_out_grp_count > HWINFO_MAX_CAPS_GROUPS) {
160 		err = -EIO;
161 		goto end;
162 	}
163 	efw->phys_in = hwinfo->phys_in;
164 	efw->phys_out = hwinfo->phys_out;
165 	efw->phys_in_grp_count = hwinfo->phys_in_grp_count;
166 	efw->phys_out_grp_count = hwinfo->phys_out_grp_count;
167 	memcpy(&efw->phys_in_grps, hwinfo->phys_in_grps,
168 	       sizeof(struct snd_efw_phys_grp) * hwinfo->phys_in_grp_count);
169 	memcpy(&efw->phys_out_grps, hwinfo->phys_out_grps,
170 	       sizeof(struct snd_efw_phys_grp) * hwinfo->phys_out_grp_count);
171 end:
172 	kfree(hwinfo);
173 	return err;
174 }
175 
176 static void
177 efw_card_free(struct snd_card *card)
178 {
179 	struct snd_efw *efw = card->private_data;
180 
181 	if (efw->card_index >= 0) {
182 		mutex_lock(&devices_mutex);
183 		clear_bit(efw->card_index, devices_used);
184 		mutex_unlock(&devices_mutex);
185 	}
186 
187 	mutex_destroy(&efw->mutex);
188 	kfree(efw->resp_buf);
189 }
190 
191 static int
192 efw_probe(struct fw_unit *unit,
193 	  const struct ieee1394_device_id *entry)
194 {
195 	struct snd_card *card;
196 	struct snd_efw *efw;
197 	int card_index, err;
198 
199 	mutex_lock(&devices_mutex);
200 
201 	/* check registered cards */
202 	for (card_index = 0; card_index < SNDRV_CARDS; ++card_index) {
203 		if (!test_bit(card_index, devices_used) && enable[card_index])
204 			break;
205 	}
206 	if (card_index >= SNDRV_CARDS) {
207 		err = -ENOENT;
208 		goto end;
209 	}
210 
211 	err = snd_card_new(&unit->device, index[card_index], id[card_index],
212 			   THIS_MODULE, sizeof(struct snd_efw), &card);
213 	if (err < 0)
214 		goto end;
215 	efw = card->private_data;
216 	efw->card_index = card_index;
217 	set_bit(card_index, devices_used);
218 	card->private_free = efw_card_free;
219 
220 	efw->card = card;
221 	efw->unit = unit;
222 	mutex_init(&efw->mutex);
223 	spin_lock_init(&efw->lock);
224 	init_waitqueue_head(&efw->hwdep_wait);
225 
226 	/* prepare response buffer */
227 	snd_efw_resp_buf_size = clamp(snd_efw_resp_buf_size,
228 				      SND_EFW_RESPONSE_MAXIMUM_BYTES, 4096U);
229 	efw->resp_buf = kzalloc(snd_efw_resp_buf_size, GFP_KERNEL);
230 	if (efw->resp_buf == NULL) {
231 		err = -ENOMEM;
232 		goto error;
233 	}
234 	efw->pull_ptr = efw->push_ptr = efw->resp_buf;
235 	snd_efw_transaction_add_instance(efw);
236 
237 	err = get_hardware_info(efw);
238 	if (err < 0)
239 		goto error;
240 	if (entry->model_id == MODEL_ECHO_AUDIOFIRE_9)
241 		efw->is_af9 = true;
242 
243 	snd_efw_proc_init(efw);
244 
245 	if (efw->midi_out_ports || efw->midi_in_ports) {
246 		err = snd_efw_create_midi_devices(efw);
247 		if (err < 0)
248 			goto error;
249 	}
250 
251 	err = snd_efw_create_pcm_devices(efw);
252 	if (err < 0)
253 		goto error;
254 
255 	err = snd_efw_create_hwdep_device(efw);
256 	if (err < 0)
257 		goto error;
258 
259 	err = snd_efw_stream_init_duplex(efw);
260 	if (err < 0)
261 		goto error;
262 
263 	err = snd_card_register(card);
264 	if (err < 0) {
265 		snd_efw_stream_destroy_duplex(efw);
266 		goto error;
267 	}
268 
269 	dev_set_drvdata(&unit->device, efw);
270 end:
271 	mutex_unlock(&devices_mutex);
272 	return err;
273 error:
274 	snd_efw_transaction_remove_instance(efw);
275 	mutex_unlock(&devices_mutex);
276 	snd_card_free(card);
277 	return err;
278 }
279 
280 static void efw_update(struct fw_unit *unit)
281 {
282 	struct snd_efw *efw = dev_get_drvdata(&unit->device);
283 
284 	snd_efw_transaction_bus_reset(efw->unit);
285 	snd_efw_stream_update_duplex(efw);
286 }
287 
288 static void efw_remove(struct fw_unit *unit)
289 {
290 	struct snd_efw *efw = dev_get_drvdata(&unit->device);
291 
292 	snd_efw_stream_destroy_duplex(efw);
293 	snd_efw_transaction_remove_instance(efw);
294 
295 	snd_card_disconnect(efw->card);
296 	snd_card_free_when_closed(efw->card);
297 }
298 
299 static const struct ieee1394_device_id efw_id_table[] = {
300 	SND_EFW_DEV_ENTRY(VENDOR_LOUD, MODEL_MACKIE_400F),
301 	SND_EFW_DEV_ENTRY(VENDOR_LOUD, MODEL_MACKIE_1200F),
302 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_8),
303 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_12),
304 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_12HD),
305 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_12_APPLE),
306 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_2),
307 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_4),
308 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_AUDIOFIRE_9),
309 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_FIREWORKS_8),
310 	SND_EFW_DEV_ENTRY(VENDOR_ECHO, MODEL_ECHO_FIREWORKS_HDMI),
311 	SND_EFW_DEV_ENTRY(VENDOR_GIBSON, MODEL_GIBSON_RIP),
312 	SND_EFW_DEV_ENTRY(VENDOR_GIBSON, MODEL_GIBSON_GOLDTOP),
313 	{}
314 };
315 MODULE_DEVICE_TABLE(ieee1394, efw_id_table);
316 
317 static struct fw_driver efw_driver = {
318 	.driver = {
319 		.owner = THIS_MODULE,
320 		.name = "snd-fireworks",
321 		.bus = &fw_bus_type,
322 	},
323 	.probe    = efw_probe,
324 	.update   = efw_update,
325 	.remove   = efw_remove,
326 	.id_table = efw_id_table,
327 };
328 
329 static int __init snd_efw_init(void)
330 {
331 	int err;
332 
333 	err = snd_efw_transaction_register();
334 	if (err < 0)
335 		goto end;
336 
337 	err = driver_register(&efw_driver.driver);
338 	if (err < 0)
339 		snd_efw_transaction_unregister();
340 
341 end:
342 	return err;
343 }
344 
345 static void __exit snd_efw_exit(void)
346 {
347 	snd_efw_transaction_unregister();
348 	driver_unregister(&efw_driver.driver);
349 }
350 
351 module_init(snd_efw_init);
352 module_exit(snd_efw_exit);
353