1/*
2 * oxfw.c - a part of driver for OXFW970/971 based devices
3 *
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Licensed under the terms of the GNU General Public License, version 2.
6 */
7
8#include "oxfw.h"
9
10#define OXFORD_FIRMWARE_ID_ADDRESS	(CSR_REGISTER_BASE + 0x50000)
11/* 0x970?vvvv or 0x971?vvvv, where vvvv = firmware version */
12
13#define OXFORD_HARDWARE_ID_ADDRESS	(CSR_REGISTER_BASE + 0x90020)
14#define OXFORD_HARDWARE_ID_OXFW970	0x39443841
15#define OXFORD_HARDWARE_ID_OXFW971	0x39373100
16
17#define VENDOR_LOUD		0x000ff2
18#define VENDOR_GRIFFIN		0x001292
19#define VENDOR_BEHRINGER	0x001564
20#define VENDOR_LACIE		0x00d04b
21#define VENDOR_TASCAM		0x00022e
22
23#define MODEL_SATELLITE		0x00200f
24
25#define SPECIFIER_1394TA	0x00a02d
26#define VERSION_AVC		0x010001
27
28MODULE_DESCRIPTION("Oxford Semiconductor FW970/971 driver");
29MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
30MODULE_LICENSE("GPL v2");
31MODULE_ALIAS("snd-firewire-speakers");
32
33static bool detect_loud_models(struct fw_unit *unit)
34{
35	const char *const models[] = {
36		"Onyxi",
37		"Onyx-i",
38		"d.Pro",
39		"Mackie Onyx Satellite",
40		"Tapco LINK.firewire 4x6",
41		"U.420"};
42	char model[32];
43	unsigned int i;
44	int err;
45
46	err = fw_csr_string(unit->directory, CSR_MODEL,
47			    model, sizeof(model));
48	if (err < 0)
49		return false;
50
51	for (i = 0; i < ARRAY_SIZE(models); i++) {
52		if (strcmp(models[i], model) == 0)
53			break;
54	}
55
56	return (i < ARRAY_SIZE(models));
57}
58
59static int name_card(struct snd_oxfw *oxfw)
60{
61	struct fw_device *fw_dev = fw_parent_device(oxfw->unit);
62	char vendor[24];
63	char model[32];
64	const char *d, *v, *m;
65	u32 firmware;
66	int err;
67
68	/* get vendor name from root directory */
69	err = fw_csr_string(fw_dev->config_rom + 5, CSR_VENDOR,
70			    vendor, sizeof(vendor));
71	if (err < 0)
72		goto end;
73
74	/* get model name from unit directory */
75	err = fw_csr_string(oxfw->unit->directory, CSR_MODEL,
76			    model, sizeof(model));
77	if (err < 0)
78		goto end;
79
80	err = snd_fw_transaction(oxfw->unit, TCODE_READ_QUADLET_REQUEST,
81				 OXFORD_FIRMWARE_ID_ADDRESS, &firmware, 4, 0);
82	if (err < 0)
83		goto end;
84	be32_to_cpus(&firmware);
85
86	/* to apply card definitions */
87	if (oxfw->device_info) {
88		d = oxfw->device_info->driver_name;
89		v = oxfw->device_info->vendor_name;
90		m = oxfw->device_info->model_name;
91	} else {
92		d = "OXFW";
93		v = vendor;
94		m = model;
95	}
96
97	strcpy(oxfw->card->driver, d);
98	strcpy(oxfw->card->mixername, m);
99	strcpy(oxfw->card->shortname, m);
100
101	snprintf(oxfw->card->longname, sizeof(oxfw->card->longname),
102		 "%s %s (OXFW%x %04x), GUID %08x%08x at %s, S%d",
103		 v, m, firmware >> 20, firmware & 0xffff,
104		 fw_dev->config_rom[3], fw_dev->config_rom[4],
105		 dev_name(&oxfw->unit->device), 100 << fw_dev->max_speed);
106end:
107	return err;
108}
109
110/*
111 * This module releases the FireWire unit data after all ALSA character devices
112 * are released by applications. This is for releasing stream data or finishing
113 * transactions safely. Thus at returning from .remove(), this module still keep
114 * references for the unit.
115 */
116static void oxfw_card_free(struct snd_card *card)
117{
118	struct snd_oxfw *oxfw = card->private_data;
119	unsigned int i;
120
121	snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->rx_stream);
122	if (oxfw->has_output)
123		snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->tx_stream);
124
125	fw_unit_put(oxfw->unit);
126
127	for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) {
128		kfree(oxfw->tx_stream_formats[i]);
129		kfree(oxfw->rx_stream_formats[i]);
130	}
131
132	mutex_destroy(&oxfw->mutex);
133}
134
135static void detect_quirks(struct snd_oxfw *oxfw)
136{
137	struct fw_device *fw_dev = fw_parent_device(oxfw->unit);
138	struct fw_csr_iterator it;
139	int key, val;
140	int vendor, model;
141
142	/* Seek from Root Directory of Config ROM. */
143	vendor = model = 0;
144	fw_csr_iterator_init(&it, fw_dev->config_rom + 5);
145	while (fw_csr_iterator_next(&it, &key, &val)) {
146		if (key == CSR_VENDOR)
147			vendor = val;
148		else if (key == CSR_MODEL)
149			model = val;
150	}
151
152	/*
153	 * Mackie Onyx Satellite with base station has a quirk to report a wrong
154	 * value in 'dbs' field of CIP header against its format information.
155	 */
156	if (vendor == VENDOR_LOUD && model == MODEL_SATELLITE)
157		oxfw->wrong_dbs = true;
158
159	/*
160	 * TASCAM FireOne has physical control and requires a pair of additional
161	 * MIDI ports.
162	 */
163	if (vendor == VENDOR_TASCAM) {
164		oxfw->midi_input_ports++;
165		oxfw->midi_output_ports++;
166	}
167}
168
169static int oxfw_probe(struct fw_unit *unit,
170		       const struct ieee1394_device_id *id)
171{
172	struct snd_card *card;
173	struct snd_oxfw *oxfw;
174	int err;
175
176	if ((id->vendor_id == VENDOR_LOUD) && !detect_loud_models(unit))
177		return -ENODEV;
178
179	err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE,
180			   sizeof(*oxfw), &card);
181	if (err < 0)
182		return err;
183
184	card->private_free = oxfw_card_free;
185	oxfw = card->private_data;
186	oxfw->card = card;
187	mutex_init(&oxfw->mutex);
188	oxfw->unit = fw_unit_get(unit);
189	oxfw->device_info = (const struct device_info *)id->driver_data;
190	spin_lock_init(&oxfw->lock);
191	init_waitqueue_head(&oxfw->hwdep_wait);
192
193	err = snd_oxfw_stream_discover(oxfw);
194	if (err < 0)
195		goto error;
196
197	detect_quirks(oxfw);
198
199	err = name_card(oxfw);
200	if (err < 0)
201		goto error;
202
203	err = snd_oxfw_create_pcm(oxfw);
204	if (err < 0)
205		goto error;
206
207	if (oxfw->device_info) {
208		err = snd_oxfw_create_mixer(oxfw);
209		if (err < 0)
210			goto error;
211	}
212
213	snd_oxfw_proc_init(oxfw);
214
215	err = snd_oxfw_create_midi(oxfw);
216	if (err < 0)
217		goto error;
218
219	err = snd_oxfw_create_hwdep(oxfw);
220	if (err < 0)
221		goto error;
222
223	err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->rx_stream);
224	if (err < 0)
225		goto error;
226	if (oxfw->has_output) {
227		err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->tx_stream);
228		if (err < 0)
229			goto error;
230	}
231
232	err = snd_card_register(card);
233	if (err < 0) {
234		snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->rx_stream);
235		if (oxfw->has_output)
236			snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->tx_stream);
237		goto error;
238	}
239	dev_set_drvdata(&unit->device, oxfw);
240
241	return 0;
242error:
243	snd_card_free(card);
244	return err;
245}
246
247static void oxfw_bus_reset(struct fw_unit *unit)
248{
249	struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
250
251	fcp_bus_reset(oxfw->unit);
252
253	mutex_lock(&oxfw->mutex);
254
255	snd_oxfw_stream_update_simplex(oxfw, &oxfw->rx_stream);
256	if (oxfw->has_output)
257		snd_oxfw_stream_update_simplex(oxfw, &oxfw->tx_stream);
258
259	mutex_unlock(&oxfw->mutex);
260}
261
262static void oxfw_remove(struct fw_unit *unit)
263{
264	struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device);
265
266	/* No need to wait for releasing card object in this context. */
267	snd_card_free_when_closed(oxfw->card);
268}
269
270static const struct device_info griffin_firewave = {
271	.driver_name = "FireWave",
272	.vendor_name = "Griffin",
273	.model_name = "FireWave",
274	.mixer_channels = 6,
275	.mute_fb_id   = 0x01,
276	.volume_fb_id = 0x02,
277};
278
279static const struct device_info lacie_speakers = {
280	.driver_name = "FWSpeakers",
281	.vendor_name = "LaCie",
282	.model_name = "FireWire Speakers",
283	.mixer_channels = 1,
284	.mute_fb_id   = 0x01,
285	.volume_fb_id = 0x01,
286};
287
288static const struct ieee1394_device_id oxfw_id_table[] = {
289	{
290		.match_flags  = IEEE1394_MATCH_VENDOR_ID |
291				IEEE1394_MATCH_MODEL_ID |
292				IEEE1394_MATCH_SPECIFIER_ID |
293				IEEE1394_MATCH_VERSION,
294		.vendor_id    = VENDOR_GRIFFIN,
295		.model_id     = 0x00f970,
296		.specifier_id = SPECIFIER_1394TA,
297		.version      = VERSION_AVC,
298		.driver_data  = (kernel_ulong_t)&griffin_firewave,
299	},
300	{
301		.match_flags  = IEEE1394_MATCH_VENDOR_ID |
302				IEEE1394_MATCH_MODEL_ID |
303				IEEE1394_MATCH_SPECIFIER_ID |
304				IEEE1394_MATCH_VERSION,
305		.vendor_id    = VENDOR_LACIE,
306		.model_id     = 0x00f970,
307		.specifier_id = SPECIFIER_1394TA,
308		.version      = VERSION_AVC,
309		.driver_data  = (kernel_ulong_t)&lacie_speakers,
310	},
311	/* Behringer,F-Control Audio 202 */
312	{
313		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
314				  IEEE1394_MATCH_MODEL_ID,
315		.vendor_id	= VENDOR_BEHRINGER,
316		.model_id	= 0x00fc22,
317	},
318	/*
319	 * Any Mackie(Loud) models (name string/model id):
320	 *  Onyx-i series (former models):	0x081216
321	 *  Mackie Onyx Satellite:		0x00200f
322	 *  Tapco LINK.firewire 4x6:		0x000460
323	 *  d.2 pro:				Unknown
324	 *  d.4 pro:				Unknown
325	 *  U.420:				Unknown
326	 *  U.420d:				Unknown
327	 */
328	{
329		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
330				  IEEE1394_MATCH_SPECIFIER_ID |
331				  IEEE1394_MATCH_VERSION,
332		.vendor_id	= VENDOR_LOUD,
333		.specifier_id	= SPECIFIER_1394TA,
334		.version	= VERSION_AVC,
335	},
336	/* TASCAM, FireOne */
337	{
338		.match_flags	= IEEE1394_MATCH_VENDOR_ID |
339				  IEEE1394_MATCH_MODEL_ID,
340		.vendor_id	= VENDOR_TASCAM,
341		.model_id	= 0x800007,
342	},
343	{ }
344};
345MODULE_DEVICE_TABLE(ieee1394, oxfw_id_table);
346
347static struct fw_driver oxfw_driver = {
348	.driver   = {
349		.owner	= THIS_MODULE,
350		.name	= KBUILD_MODNAME,
351		.bus	= &fw_bus_type,
352	},
353	.probe    = oxfw_probe,
354	.update   = oxfw_bus_reset,
355	.remove   = oxfw_remove,
356	.id_table = oxfw_id_table,
357};
358
359static int __init snd_oxfw_init(void)
360{
361	return driver_register(&oxfw_driver.driver);
362}
363
364static void __exit snd_oxfw_exit(void)
365{
366	driver_unregister(&oxfw_driver.driver);
367}
368
369module_init(snd_oxfw_init);
370module_exit(snd_oxfw_exit);
371