Commit | Line | Data |
---|---|---|
31ef9134 | 1 | /* |
8832c5a7 | 2 | * oxfw.c - a part of driver for OXFW970/971 based devices |
31ef9134 CL |
3 | * |
4 | * Copyright (c) Clemens Ladisch <clemens@ladisch.de> | |
5 | * Licensed under the terms of the GNU General Public License, version 2. | |
6 | */ | |
7 | ||
e2786ca6 | 8 | #include "oxfw.h" |
31ef9134 CL |
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_GRIFFIN 0x001292 | |
18 | #define VENDOR_LACIE 0x00d04b | |
19 | ||
20 | #define SPECIFIER_1394TA 0x00a02d | |
21 | #define VERSION_AVC 0x010001 | |
22 | ||
8832c5a7 | 23 | MODULE_DESCRIPTION("Oxford Semiconductor FW970/971 driver"); |
31ef9134 CL |
24 | MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); |
25 | MODULE_LICENSE("GPL v2"); | |
8832c5a7 | 26 | MODULE_ALIAS("snd-firewire-speakers"); |
31ef9134 | 27 | |
31ef9134 CL |
28 | enum control_action { CTL_READ, CTL_WRITE }; |
29 | enum control_attribute { | |
30 | CTL_MIN = 0x02, | |
31 | CTL_MAX = 0x03, | |
32 | CTL_CURRENT = 0x10, | |
33 | }; | |
34 | ||
8832c5a7 | 35 | static int oxfw_mute_command(struct snd_oxfw *oxfw, bool *value, |
31ef9134 CL |
36 | enum control_action action) |
37 | { | |
38 | u8 *buf; | |
39 | u8 response_ok; | |
40 | int err; | |
41 | ||
42 | buf = kmalloc(11, GFP_KERNEL); | |
43 | if (!buf) | |
44 | return -ENOMEM; | |
45 | ||
46 | if (action == CTL_READ) { | |
47 | buf[0] = 0x01; /* AV/C, STATUS */ | |
48 | response_ok = 0x0c; /* STABLE */ | |
49 | } else { | |
50 | buf[0] = 0x00; /* AV/C, CONTROL */ | |
51 | response_ok = 0x09; /* ACCEPTED */ | |
52 | } | |
53 | buf[1] = 0x08; /* audio unit 0 */ | |
54 | buf[2] = 0xb8; /* FUNCTION BLOCK */ | |
55 | buf[3] = 0x81; /* function block type: feature */ | |
8832c5a7 | 56 | buf[4] = oxfw->device_info->mute_fb_id; /* function block ID */ |
31ef9134 CL |
57 | buf[5] = 0x10; /* control attribute: current */ |
58 | buf[6] = 0x02; /* selector length */ | |
59 | buf[7] = 0x00; /* audio channel number */ | |
60 | buf[8] = 0x01; /* control selector: mute */ | |
61 | buf[9] = 0x01; /* control data length */ | |
62 | if (action == CTL_READ) | |
63 | buf[10] = 0xff; | |
64 | else | |
65 | buf[10] = *value ? 0x70 : 0x60; | |
66 | ||
8832c5a7 | 67 | err = fcp_avc_transaction(oxfw->unit, buf, 11, buf, 11, 0x3fe); |
31ef9134 CL |
68 | if (err < 0) |
69 | goto error; | |
70 | if (err < 11) { | |
8832c5a7 | 71 | dev_err(&oxfw->unit->device, "short FCP response\n"); |
31ef9134 CL |
72 | err = -EIO; |
73 | goto error; | |
74 | } | |
75 | if (buf[0] != response_ok) { | |
8832c5a7 | 76 | dev_err(&oxfw->unit->device, "mute command failed\n"); |
31ef9134 CL |
77 | err = -EIO; |
78 | goto error; | |
79 | } | |
80 | if (action == CTL_READ) | |
81 | *value = buf[10] == 0x70; | |
82 | ||
83 | err = 0; | |
84 | ||
85 | error: | |
86 | kfree(buf); | |
87 | ||
88 | return err; | |
89 | } | |
90 | ||
8832c5a7 | 91 | static int oxfw_volume_command(struct snd_oxfw *oxfw, s16 *value, |
31ef9134 CL |
92 | unsigned int channel, |
93 | enum control_attribute attribute, | |
94 | enum control_action action) | |
95 | { | |
96 | u8 *buf; | |
97 | u8 response_ok; | |
98 | int err; | |
99 | ||
100 | buf = kmalloc(12, GFP_KERNEL); | |
101 | if (!buf) | |
102 | return -ENOMEM; | |
103 | ||
104 | if (action == CTL_READ) { | |
105 | buf[0] = 0x01; /* AV/C, STATUS */ | |
106 | response_ok = 0x0c; /* STABLE */ | |
107 | } else { | |
108 | buf[0] = 0x00; /* AV/C, CONTROL */ | |
109 | response_ok = 0x09; /* ACCEPTED */ | |
110 | } | |
111 | buf[1] = 0x08; /* audio unit 0 */ | |
112 | buf[2] = 0xb8; /* FUNCTION BLOCK */ | |
113 | buf[3] = 0x81; /* function block type: feature */ | |
8832c5a7 | 114 | buf[4] = oxfw->device_info->volume_fb_id; /* function block ID */ |
31ef9134 CL |
115 | buf[5] = attribute; /* control attribute */ |
116 | buf[6] = 0x02; /* selector length */ | |
117 | buf[7] = channel; /* audio channel number */ | |
118 | buf[8] = 0x02; /* control selector: volume */ | |
119 | buf[9] = 0x02; /* control data length */ | |
120 | if (action == CTL_READ) { | |
121 | buf[10] = 0xff; | |
122 | buf[11] = 0xff; | |
123 | } else { | |
124 | buf[10] = *value >> 8; | |
125 | buf[11] = *value; | |
126 | } | |
127 | ||
8832c5a7 | 128 | err = fcp_avc_transaction(oxfw->unit, buf, 12, buf, 12, 0x3fe); |
31ef9134 CL |
129 | if (err < 0) |
130 | goto error; | |
131 | if (err < 12) { | |
8832c5a7 | 132 | dev_err(&oxfw->unit->device, "short FCP response\n"); |
31ef9134 CL |
133 | err = -EIO; |
134 | goto error; | |
135 | } | |
136 | if (buf[0] != response_ok) { | |
8832c5a7 | 137 | dev_err(&oxfw->unit->device, "volume command failed\n"); |
31ef9134 CL |
138 | err = -EIO; |
139 | goto error; | |
140 | } | |
141 | if (action == CTL_READ) | |
142 | *value = (buf[10] << 8) | buf[11]; | |
143 | ||
144 | err = 0; | |
145 | ||
146 | error: | |
147 | kfree(buf); | |
148 | ||
149 | return err; | |
150 | } | |
151 | ||
8832c5a7 | 152 | static int oxfw_mute_get(struct snd_kcontrol *control, |
31ef9134 CL |
153 | struct snd_ctl_elem_value *value) |
154 | { | |
8832c5a7 | 155 | struct snd_oxfw *oxfw = control->private_data; |
31ef9134 | 156 | |
8832c5a7 | 157 | value->value.integer.value[0] = !oxfw->mute; |
31ef9134 CL |
158 | |
159 | return 0; | |
160 | } | |
161 | ||
8832c5a7 | 162 | static int oxfw_mute_put(struct snd_kcontrol *control, |
31ef9134 CL |
163 | struct snd_ctl_elem_value *value) |
164 | { | |
8832c5a7 | 165 | struct snd_oxfw *oxfw = control->private_data; |
31ef9134 CL |
166 | bool mute; |
167 | int err; | |
168 | ||
169 | mute = !value->value.integer.value[0]; | |
170 | ||
8832c5a7 | 171 | if (mute == oxfw->mute) |
31ef9134 CL |
172 | return 0; |
173 | ||
8832c5a7 | 174 | err = oxfw_mute_command(oxfw, &mute, CTL_WRITE); |
31ef9134 CL |
175 | if (err < 0) |
176 | return err; | |
8832c5a7 | 177 | oxfw->mute = mute; |
31ef9134 CL |
178 | |
179 | return 1; | |
180 | } | |
181 | ||
8832c5a7 | 182 | static int oxfw_volume_info(struct snd_kcontrol *control, |
31ef9134 CL |
183 | struct snd_ctl_elem_info *info) |
184 | { | |
8832c5a7 | 185 | struct snd_oxfw *oxfw = control->private_data; |
31ef9134 CL |
186 | |
187 | info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
8832c5a7 TS |
188 | info->count = oxfw->device_info->mixer_channels; |
189 | info->value.integer.min = oxfw->volume_min; | |
190 | info->value.integer.max = oxfw->volume_max; | |
31ef9134 CL |
191 | |
192 | return 0; | |
193 | } | |
194 | ||
195 | static const u8 channel_map[6] = { 0, 1, 4, 5, 2, 3 }; | |
196 | ||
8832c5a7 | 197 | static int oxfw_volume_get(struct snd_kcontrol *control, |
31ef9134 CL |
198 | struct snd_ctl_elem_value *value) |
199 | { | |
8832c5a7 | 200 | struct snd_oxfw *oxfw = control->private_data; |
31ef9134 CL |
201 | unsigned int i; |
202 | ||
8832c5a7 TS |
203 | for (i = 0; i < oxfw->device_info->mixer_channels; ++i) |
204 | value->value.integer.value[channel_map[i]] = oxfw->volume[i]; | |
31ef9134 CL |
205 | |
206 | return 0; | |
207 | } | |
208 | ||
8832c5a7 | 209 | static int oxfw_volume_put(struct snd_kcontrol *control, |
31ef9134 CL |
210 | struct snd_ctl_elem_value *value) |
211 | { | |
8832c5a7 | 212 | struct snd_oxfw *oxfw = control->private_data; |
31ef9134 CL |
213 | unsigned int i, changed_channels; |
214 | bool equal_values = true; | |
215 | s16 volume; | |
216 | int err; | |
217 | ||
8832c5a7 TS |
218 | for (i = 0; i < oxfw->device_info->mixer_channels; ++i) { |
219 | if (value->value.integer.value[i] < oxfw->volume_min || | |
220 | value->value.integer.value[i] > oxfw->volume_max) | |
31ef9134 CL |
221 | return -EINVAL; |
222 | if (value->value.integer.value[i] != | |
223 | value->value.integer.value[0]) | |
224 | equal_values = false; | |
225 | } | |
226 | ||
227 | changed_channels = 0; | |
8832c5a7 | 228 | for (i = 0; i < oxfw->device_info->mixer_channels; ++i) |
31ef9134 | 229 | if (value->value.integer.value[channel_map[i]] != |
8832c5a7 | 230 | oxfw->volume[i]) |
31ef9134 CL |
231 | changed_channels |= 1 << (i + 1); |
232 | ||
233 | if (equal_values && changed_channels != 0) | |
234 | changed_channels = 1 << 0; | |
235 | ||
8832c5a7 | 236 | for (i = 0; i <= oxfw->device_info->mixer_channels; ++i) { |
31ef9134 CL |
237 | volume = value->value.integer.value[channel_map[i ? i - 1 : 0]]; |
238 | if (changed_channels & (1 << i)) { | |
8832c5a7 | 239 | err = oxfw_volume_command(oxfw, &volume, i, |
31ef9134 CL |
240 | CTL_CURRENT, CTL_WRITE); |
241 | if (err < 0) | |
242 | return err; | |
243 | } | |
244 | if (i > 0) | |
8832c5a7 | 245 | oxfw->volume[i - 1] = volume; |
31ef9134 CL |
246 | } |
247 | ||
248 | return changed_channels != 0; | |
249 | } | |
250 | ||
8832c5a7 | 251 | static int oxfw_create_mixer(struct snd_oxfw *oxfw) |
31ef9134 CL |
252 | { |
253 | static const struct snd_kcontrol_new controls[] = { | |
254 | { | |
255 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
256 | .name = "PCM Playback Switch", | |
257 | .info = snd_ctl_boolean_mono_info, | |
8832c5a7 TS |
258 | .get = oxfw_mute_get, |
259 | .put = oxfw_mute_put, | |
31ef9134 CL |
260 | }, |
261 | { | |
262 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
263 | .name = "PCM Playback Volume", | |
8832c5a7 TS |
264 | .info = oxfw_volume_info, |
265 | .get = oxfw_volume_get, | |
266 | .put = oxfw_volume_put, | |
31ef9134 CL |
267 | }, |
268 | }; | |
269 | unsigned int i, first_ch; | |
270 | int err; | |
271 | ||
8832c5a7 | 272 | err = oxfw_volume_command(oxfw, &oxfw->volume_min, |
31ef9134 CL |
273 | 0, CTL_MIN, CTL_READ); |
274 | if (err < 0) | |
275 | return err; | |
8832c5a7 | 276 | err = oxfw_volume_command(oxfw, &oxfw->volume_max, |
31ef9134 CL |
277 | 0, CTL_MAX, CTL_READ); |
278 | if (err < 0) | |
279 | return err; | |
280 | ||
8832c5a7 | 281 | err = oxfw_mute_command(oxfw, &oxfw->mute, CTL_READ); |
31ef9134 CL |
282 | if (err < 0) |
283 | return err; | |
284 | ||
8832c5a7 TS |
285 | first_ch = oxfw->device_info->mixer_channels == 1 ? 0 : 1; |
286 | for (i = 0; i < oxfw->device_info->mixer_channels; ++i) { | |
287 | err = oxfw_volume_command(oxfw, &oxfw->volume[i], | |
31ef9134 CL |
288 | first_ch + i, CTL_CURRENT, CTL_READ); |
289 | if (err < 0) | |
290 | return err; | |
291 | } | |
292 | ||
293 | for (i = 0; i < ARRAY_SIZE(controls); ++i) { | |
8832c5a7 TS |
294 | err = snd_ctl_add(oxfw->card, |
295 | snd_ctl_new1(&controls[i], oxfw)); | |
31ef9134 CL |
296 | if (err < 0) |
297 | return err; | |
298 | } | |
299 | ||
300 | return 0; | |
301 | } | |
302 | ||
8832c5a7 | 303 | static u32 oxfw_read_firmware_version(struct fw_unit *unit) |
31ef9134 CL |
304 | { |
305 | __be32 data; | |
306 | int err; | |
307 | ||
308 | err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST, | |
1b70485f | 309 | OXFORD_FIRMWARE_ID_ADDRESS, &data, 4, 0); |
31ef9134 CL |
310 | return err >= 0 ? be32_to_cpu(data) : 0; |
311 | } | |
312 | ||
8832c5a7 | 313 | static void oxfw_card_free(struct snd_card *card) |
31ef9134 | 314 | { |
8832c5a7 | 315 | struct snd_oxfw *oxfw = card->private_data; |
31ef9134 | 316 | |
8832c5a7 | 317 | mutex_destroy(&oxfw->mutex); |
31ef9134 CL |
318 | } |
319 | ||
8832c5a7 | 320 | static int oxfw_probe(struct fw_unit *unit, |
94a87157 | 321 | const struct ieee1394_device_id *id) |
31ef9134 | 322 | { |
31ef9134 CL |
323 | struct fw_device *fw_dev = fw_parent_device(unit); |
324 | struct snd_card *card; | |
8832c5a7 | 325 | struct snd_oxfw *oxfw; |
31ef9134 CL |
326 | u32 firmware; |
327 | int err; | |
328 | ||
06b45f00 | 329 | err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE, |
8832c5a7 | 330 | sizeof(*oxfw), &card); |
31ef9134 CL |
331 | if (err < 0) |
332 | return err; | |
31ef9134 | 333 | |
e2786ca6 | 334 | card->private_free = oxfw_card_free; |
8832c5a7 TS |
335 | oxfw = card->private_data; |
336 | oxfw->card = card; | |
337 | mutex_init(&oxfw->mutex); | |
e2786ca6 | 338 | oxfw->unit = unit; |
8832c5a7 | 339 | oxfw->device_info = (const struct device_info *)id->driver_data; |
31ef9134 | 340 | |
8832c5a7 TS |
341 | strcpy(card->driver, oxfw->device_info->driver_name); |
342 | strcpy(card->shortname, oxfw->device_info->short_name); | |
343 | firmware = oxfw_read_firmware_version(unit); | |
31ef9134 CL |
344 | snprintf(card->longname, sizeof(card->longname), |
345 | "%s (OXFW%x %04x), GUID %08x%08x at %s, S%d", | |
8832c5a7 | 346 | oxfw->device_info->long_name, |
31ef9134 CL |
347 | firmware >> 20, firmware & 0xffff, |
348 | fw_dev->config_rom[3], fw_dev->config_rom[4], | |
349 | dev_name(&unit->device), 100 << fw_dev->max_speed); | |
8832c5a7 | 350 | strcpy(card->mixername, "OXFW"); |
31ef9134 | 351 | |
3713d93a | 352 | err = snd_oxfw_create_pcm(oxfw); |
31ef9134 CL |
353 | if (err < 0) |
354 | goto error; | |
355 | ||
8832c5a7 | 356 | err = oxfw_create_mixer(oxfw); |
31ef9134 CL |
357 | if (err < 0) |
358 | goto error; | |
359 | ||
e2786ca6 | 360 | err = snd_oxfw_stream_init_simplex(oxfw); |
31ef9134 CL |
361 | if (err < 0) |
362 | goto error; | |
363 | ||
e2786ca6 TS |
364 | err = snd_card_register(card); |
365 | if (err < 0) { | |
366 | snd_oxfw_stream_destroy_simplex(oxfw); | |
367 | goto error; | |
368 | } | |
8832c5a7 | 369 | dev_set_drvdata(&unit->device, oxfw); |
31ef9134 CL |
370 | |
371 | return 0; | |
31ef9134 CL |
372 | error: |
373 | snd_card_free(card); | |
374 | return err; | |
375 | } | |
376 | ||
8832c5a7 | 377 | static void oxfw_bus_reset(struct fw_unit *unit) |
31ef9134 | 378 | { |
8832c5a7 | 379 | struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device); |
31ef9134 | 380 | |
8832c5a7 | 381 | fcp_bus_reset(oxfw->unit); |
31ef9134 | 382 | |
e2786ca6 TS |
383 | mutex_lock(&oxfw->mutex); |
384 | snd_oxfw_stream_update_simplex(oxfw); | |
385 | mutex_unlock(&oxfw->mutex); | |
31ef9134 CL |
386 | } |
387 | ||
8832c5a7 | 388 | static void oxfw_remove(struct fw_unit *unit) |
94a87157 | 389 | { |
8832c5a7 | 390 | struct snd_oxfw *oxfw = dev_get_drvdata(&unit->device); |
94a87157 | 391 | |
8832c5a7 | 392 | snd_card_disconnect(oxfw->card); |
94a87157 | 393 | |
e2786ca6 | 394 | snd_oxfw_stream_destroy_simplex(oxfw); |
94a87157 | 395 | |
8832c5a7 | 396 | snd_card_free_when_closed(oxfw->card); |
94a87157 SR |
397 | } |
398 | ||
399 | static const struct device_info griffin_firewave = { | |
400 | .driver_name = "FireWave", | |
401 | .short_name = "FireWave", | |
402 | .long_name = "Griffin FireWave Surround", | |
403 | .pcm_constraints = firewave_constraints, | |
404 | .mixer_channels = 6, | |
405 | .mute_fb_id = 0x01, | |
406 | .volume_fb_id = 0x02, | |
407 | }; | |
408 | ||
409 | static const struct device_info lacie_speakers = { | |
410 | .driver_name = "FWSpeakers", | |
411 | .short_name = "FireWire Speakers", | |
412 | .long_name = "LaCie FireWire Speakers", | |
413 | .pcm_constraints = lacie_speakers_constraints, | |
414 | .mixer_channels = 1, | |
415 | .mute_fb_id = 0x01, | |
416 | .volume_fb_id = 0x01, | |
417 | }; | |
418 | ||
8832c5a7 | 419 | static const struct ieee1394_device_id oxfw_id_table[] = { |
31ef9134 CL |
420 | { |
421 | .match_flags = IEEE1394_MATCH_VENDOR_ID | | |
422 | IEEE1394_MATCH_MODEL_ID | | |
423 | IEEE1394_MATCH_SPECIFIER_ID | | |
424 | IEEE1394_MATCH_VERSION, | |
425 | .vendor_id = VENDOR_GRIFFIN, | |
426 | .model_id = 0x00f970, | |
427 | .specifier_id = SPECIFIER_1394TA, | |
428 | .version = VERSION_AVC, | |
94a87157 | 429 | .driver_data = (kernel_ulong_t)&griffin_firewave, |
31ef9134 CL |
430 | }, |
431 | { | |
432 | .match_flags = IEEE1394_MATCH_VENDOR_ID | | |
433 | IEEE1394_MATCH_MODEL_ID | | |
434 | IEEE1394_MATCH_SPECIFIER_ID | | |
435 | IEEE1394_MATCH_VERSION, | |
436 | .vendor_id = VENDOR_LACIE, | |
437 | .model_id = 0x00f970, | |
438 | .specifier_id = SPECIFIER_1394TA, | |
439 | .version = VERSION_AVC, | |
94a87157 | 440 | .driver_data = (kernel_ulong_t)&lacie_speakers, |
31ef9134 CL |
441 | }, |
442 | { } | |
443 | }; | |
8832c5a7 | 444 | MODULE_DEVICE_TABLE(ieee1394, oxfw_id_table); |
31ef9134 | 445 | |
8832c5a7 | 446 | static struct fw_driver oxfw_driver = { |
31ef9134 CL |
447 | .driver = { |
448 | .owner = THIS_MODULE, | |
449 | .name = KBUILD_MODNAME, | |
450 | .bus = &fw_bus_type, | |
31ef9134 | 451 | }, |
8832c5a7 TS |
452 | .probe = oxfw_probe, |
453 | .update = oxfw_bus_reset, | |
454 | .remove = oxfw_remove, | |
455 | .id_table = oxfw_id_table, | |
31ef9134 CL |
456 | }; |
457 | ||
8832c5a7 | 458 | static int __init snd_oxfw_init(void) |
31ef9134 | 459 | { |
8832c5a7 | 460 | return driver_register(&oxfw_driver.driver); |
31ef9134 CL |
461 | } |
462 | ||
8832c5a7 | 463 | static void __exit snd_oxfw_exit(void) |
31ef9134 | 464 | { |
8832c5a7 | 465 | driver_unregister(&oxfw_driver.driver); |
31ef9134 CL |
466 | } |
467 | ||
8832c5a7 TS |
468 | module_init(snd_oxfw_init); |
469 | module_exit(snd_oxfw_exit); |