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