Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * OXFW970-based speakers driver |
| 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 <linux/device.h> |
| 9 | #include <linux/firewire.h> |
| 10 | #include <linux/firewire-constants.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/mod_devicetable.h> |
| 13 | #include <linux/mutex.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <sound/control.h> |
| 16 | #include <sound/core.h> |
| 17 | #include <sound/initval.h> |
| 18 | #include <sound/pcm.h> |
| 19 | #include <sound/pcm_params.h> |
| 20 | #include "cmp.h" |
| 21 | #include "fcp.h" |
| 22 | #include "amdtp.h" |
| 23 | #include "lib.h" |
| 24 | |
| 25 | #define OXFORD_FIRMWARE_ID_ADDRESS (CSR_REGISTER_BASE + 0x50000) |
| 26 | /* 0x970?vvvv or 0x971?vvvv, where vvvv = firmware version */ |
| 27 | |
| 28 | #define OXFORD_HARDWARE_ID_ADDRESS (CSR_REGISTER_BASE + 0x90020) |
| 29 | #define OXFORD_HARDWARE_ID_OXFW970 0x39443841 |
| 30 | #define OXFORD_HARDWARE_ID_OXFW971 0x39373100 |
| 31 | |
| 32 | #define VENDOR_GRIFFIN 0x001292 |
| 33 | #define VENDOR_LACIE 0x00d04b |
| 34 | |
| 35 | #define SPECIFIER_1394TA 0x00a02d |
| 36 | #define VERSION_AVC 0x010001 |
| 37 | |
| 38 | struct device_info { |
| 39 | const char *driver_name; |
| 40 | const char *short_name; |
| 41 | const char *long_name; |
| 42 | int (*pcm_constraints)(struct snd_pcm_runtime *runtime); |
| 43 | unsigned int mixer_channels; |
| 44 | u8 mute_fb_id; |
| 45 | u8 volume_fb_id; |
| 46 | }; |
| 47 | |
| 48 | struct fwspk { |
| 49 | struct snd_card *card; |
| 50 | struct fw_unit *unit; |
| 51 | const struct device_info *device_info; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 52 | struct mutex mutex; |
| 53 | struct cmp_connection connection; |
| 54 | struct amdtp_out_stream stream; |
| 55 | bool stream_running; |
| 56 | bool mute; |
| 57 | s16 volume[6]; |
| 58 | s16 volume_min; |
| 59 | s16 volume_max; |
| 60 | }; |
| 61 | |
| 62 | MODULE_DESCRIPTION("FireWire speakers driver"); |
| 63 | MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); |
| 64 | MODULE_LICENSE("GPL v2"); |
| 65 | |
| 66 | static int firewave_rate_constraint(struct snd_pcm_hw_params *params, |
| 67 | struct snd_pcm_hw_rule *rule) |
| 68 | { |
| 69 | static unsigned int stereo_rates[] = { 48000, 96000 }; |
| 70 | struct snd_interval *channels = |
| 71 | hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
| 72 | struct snd_interval *rate = |
| 73 | hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
| 74 | |
| 75 | /* two channels work only at 48/96 kHz */ |
| 76 | if (snd_interval_max(channels) < 6) |
| 77 | return snd_interval_list(rate, 2, stereo_rates, 0); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int firewave_channels_constraint(struct snd_pcm_hw_params *params, |
| 82 | struct snd_pcm_hw_rule *rule) |
| 83 | { |
| 84 | static const struct snd_interval all_channels = { .min = 6, .max = 6 }; |
| 85 | struct snd_interval *rate = |
| 86 | hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
| 87 | struct snd_interval *channels = |
| 88 | hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
| 89 | |
| 90 | /* 32/44.1 kHz work only with all six channels */ |
| 91 | if (snd_interval_max(rate) < 48000) |
| 92 | return snd_interval_refine(channels, &all_channels); |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int firewave_constraints(struct snd_pcm_runtime *runtime) |
| 97 | { |
| 98 | static unsigned int channels_list[] = { 2, 6 }; |
| 99 | static struct snd_pcm_hw_constraint_list channels_list_constraint = { |
| 100 | .count = 2, |
| 101 | .list = channels_list, |
| 102 | }; |
| 103 | int err; |
| 104 | |
| 105 | runtime->hw.rates = SNDRV_PCM_RATE_32000 | |
| 106 | SNDRV_PCM_RATE_44100 | |
| 107 | SNDRV_PCM_RATE_48000 | |
| 108 | SNDRV_PCM_RATE_96000; |
| 109 | runtime->hw.channels_max = 6; |
| 110 | |
| 111 | err = snd_pcm_hw_constraint_list(runtime, 0, |
| 112 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 113 | &channels_list_constraint); |
| 114 | if (err < 0) |
| 115 | return err; |
| 116 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
| 117 | firewave_rate_constraint, NULL, |
| 118 | SNDRV_PCM_HW_PARAM_CHANNELS, -1); |
| 119 | if (err < 0) |
| 120 | return err; |
| 121 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 122 | firewave_channels_constraint, NULL, |
| 123 | SNDRV_PCM_HW_PARAM_RATE, -1); |
| 124 | if (err < 0) |
| 125 | return err; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int lacie_speakers_constraints(struct snd_pcm_runtime *runtime) |
| 131 | { |
| 132 | runtime->hw.rates = SNDRV_PCM_RATE_32000 | |
| 133 | SNDRV_PCM_RATE_44100 | |
| 134 | SNDRV_PCM_RATE_48000 | |
| 135 | SNDRV_PCM_RATE_88200 | |
| 136 | SNDRV_PCM_RATE_96000; |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static int fwspk_open(struct snd_pcm_substream *substream) |
| 142 | { |
| 143 | static const struct snd_pcm_hardware hardware = { |
| 144 | .info = SNDRV_PCM_INFO_MMAP | |
| 145 | SNDRV_PCM_INFO_MMAP_VALID | |
| 146 | SNDRV_PCM_INFO_BATCH | |
| 147 | SNDRV_PCM_INFO_INTERLEAVED | |
| 148 | SNDRV_PCM_INFO_BLOCK_TRANSFER, |
| 149 | .formats = AMDTP_OUT_PCM_FORMAT_BITS, |
| 150 | .channels_min = 2, |
| 151 | .channels_max = 2, |
| 152 | .buffer_bytes_max = 4 * 1024 * 1024, |
| 153 | .period_bytes_min = 1, |
| 154 | .period_bytes_max = UINT_MAX, |
| 155 | .periods_min = 1, |
| 156 | .periods_max = UINT_MAX, |
| 157 | }; |
| 158 | struct fwspk *fwspk = substream->private_data; |
| 159 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 160 | int err; |
| 161 | |
| 162 | runtime->hw = hardware; |
| 163 | |
| 164 | err = fwspk->device_info->pcm_constraints(runtime); |
| 165 | if (err < 0) |
| 166 | return err; |
| 167 | err = snd_pcm_limit_hw_rates(runtime); |
| 168 | if (err < 0) |
| 169 | return err; |
| 170 | |
| 171 | err = snd_pcm_hw_constraint_minmax(runtime, |
| 172 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
Clemens Ladisch | f4b1e98 | 2011-06-17 08:17:56 +0200 | [diff] [blame] | 173 | 5000, UINT_MAX); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 174 | if (err < 0) |
| 175 | return err; |
| 176 | |
| 177 | err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24); |
| 178 | if (err < 0) |
| 179 | return err; |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int fwspk_close(struct snd_pcm_substream *substream) |
| 185 | { |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static void fwspk_stop_stream(struct fwspk *fwspk) |
| 190 | { |
| 191 | if (fwspk->stream_running) { |
| 192 | amdtp_out_stream_stop(&fwspk->stream); |
| 193 | cmp_connection_break(&fwspk->connection); |
| 194 | fwspk->stream_running = false; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | static int fwspk_set_rate(struct fwspk *fwspk, unsigned int sfc) |
| 199 | { |
| 200 | u8 *buf; |
| 201 | int err; |
| 202 | |
| 203 | buf = kmalloc(8, GFP_KERNEL); |
| 204 | if (!buf) |
| 205 | return -ENOMEM; |
| 206 | |
| 207 | buf[0] = 0x00; /* AV/C, CONTROL */ |
| 208 | buf[1] = 0xff; /* unit */ |
| 209 | buf[2] = 0x19; /* INPUT PLUG SIGNAL FORMAT */ |
| 210 | buf[3] = 0x00; /* plug 0 */ |
| 211 | buf[4] = 0x90; /* format: audio */ |
| 212 | buf[5] = 0x00 | sfc; /* AM824, frequency */ |
| 213 | buf[6] = 0xff; /* SYT (not used) */ |
| 214 | buf[7] = 0xff; |
| 215 | |
| 216 | err = fcp_avc_transaction(fwspk->unit, buf, 8, buf, 8, |
| 217 | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5)); |
| 218 | if (err < 0) |
| 219 | goto error; |
| 220 | if (err < 6 || buf[0] != 0x09 /* ACCEPTED */) { |
| 221 | dev_err(&fwspk->unit->device, "failed to set sample rate\n"); |
| 222 | err = -EIO; |
| 223 | goto error; |
| 224 | } |
| 225 | |
| 226 | err = 0; |
| 227 | |
| 228 | error: |
| 229 | kfree(buf); |
| 230 | |
| 231 | return err; |
| 232 | } |
| 233 | |
| 234 | static int fwspk_hw_params(struct snd_pcm_substream *substream, |
| 235 | struct snd_pcm_hw_params *hw_params) |
| 236 | { |
| 237 | struct fwspk *fwspk = substream->private_data; |
| 238 | int err; |
| 239 | |
| 240 | mutex_lock(&fwspk->mutex); |
| 241 | fwspk_stop_stream(fwspk); |
| 242 | mutex_unlock(&fwspk->mutex); |
| 243 | |
| 244 | err = snd_pcm_lib_alloc_vmalloc_buffer(substream, |
| 245 | params_buffer_bytes(hw_params)); |
| 246 | if (err < 0) |
| 247 | goto error; |
| 248 | |
| 249 | amdtp_out_stream_set_rate(&fwspk->stream, params_rate(hw_params)); |
| 250 | amdtp_out_stream_set_pcm(&fwspk->stream, params_channels(hw_params)); |
| 251 | |
| 252 | amdtp_out_stream_set_pcm_format(&fwspk->stream, |
| 253 | params_format(hw_params)); |
| 254 | |
| 255 | err = fwspk_set_rate(fwspk, fwspk->stream.sfc); |
| 256 | if (err < 0) |
| 257 | goto err_buffer; |
| 258 | |
| 259 | return 0; |
| 260 | |
| 261 | err_buffer: |
| 262 | snd_pcm_lib_free_vmalloc_buffer(substream); |
| 263 | error: |
| 264 | return err; |
| 265 | } |
| 266 | |
| 267 | static int fwspk_hw_free(struct snd_pcm_substream *substream) |
| 268 | { |
| 269 | struct fwspk *fwspk = substream->private_data; |
| 270 | |
| 271 | mutex_lock(&fwspk->mutex); |
| 272 | fwspk_stop_stream(fwspk); |
| 273 | mutex_unlock(&fwspk->mutex); |
| 274 | |
| 275 | return snd_pcm_lib_free_vmalloc_buffer(substream); |
| 276 | } |
| 277 | |
| 278 | static int fwspk_prepare(struct snd_pcm_substream *substream) |
| 279 | { |
| 280 | struct fwspk *fwspk = substream->private_data; |
| 281 | int err; |
| 282 | |
| 283 | mutex_lock(&fwspk->mutex); |
| 284 | |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 285 | if (amdtp_out_streaming_error(&fwspk->stream)) |
| 286 | fwspk_stop_stream(fwspk); |
| 287 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 288 | if (!fwspk->stream_running) { |
| 289 | err = cmp_connection_establish(&fwspk->connection, |
| 290 | amdtp_out_stream_get_max_payload(&fwspk->stream)); |
| 291 | if (err < 0) |
| 292 | goto err_mutex; |
| 293 | |
| 294 | err = amdtp_out_stream_start(&fwspk->stream, |
| 295 | fwspk->connection.resources.channel, |
| 296 | fwspk->connection.speed); |
| 297 | if (err < 0) |
| 298 | goto err_connection; |
| 299 | |
| 300 | fwspk->stream_running = true; |
| 301 | } |
| 302 | |
| 303 | mutex_unlock(&fwspk->mutex); |
| 304 | |
| 305 | amdtp_out_stream_pcm_prepare(&fwspk->stream); |
| 306 | |
| 307 | return 0; |
| 308 | |
| 309 | err_connection: |
| 310 | cmp_connection_break(&fwspk->connection); |
| 311 | err_mutex: |
| 312 | mutex_unlock(&fwspk->mutex); |
| 313 | |
| 314 | return err; |
| 315 | } |
| 316 | |
| 317 | static int fwspk_trigger(struct snd_pcm_substream *substream, int cmd) |
| 318 | { |
| 319 | struct fwspk *fwspk = substream->private_data; |
| 320 | struct snd_pcm_substream *pcm; |
| 321 | |
| 322 | switch (cmd) { |
| 323 | case SNDRV_PCM_TRIGGER_START: |
| 324 | pcm = substream; |
| 325 | break; |
| 326 | case SNDRV_PCM_TRIGGER_STOP: |
| 327 | pcm = NULL; |
| 328 | break; |
| 329 | default: |
| 330 | return -EINVAL; |
| 331 | } |
| 332 | amdtp_out_stream_pcm_trigger(&fwspk->stream, pcm); |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static snd_pcm_uframes_t fwspk_pointer(struct snd_pcm_substream *substream) |
| 337 | { |
| 338 | struct fwspk *fwspk = substream->private_data; |
| 339 | |
| 340 | return amdtp_out_stream_pcm_pointer(&fwspk->stream); |
| 341 | } |
| 342 | |
| 343 | static int fwspk_create_pcm(struct fwspk *fwspk) |
| 344 | { |
| 345 | static struct snd_pcm_ops ops = { |
| 346 | .open = fwspk_open, |
| 347 | .close = fwspk_close, |
| 348 | .ioctl = snd_pcm_lib_ioctl, |
| 349 | .hw_params = fwspk_hw_params, |
| 350 | .hw_free = fwspk_hw_free, |
| 351 | .prepare = fwspk_prepare, |
| 352 | .trigger = fwspk_trigger, |
| 353 | .pointer = fwspk_pointer, |
| 354 | .page = snd_pcm_lib_get_vmalloc_page, |
| 355 | .mmap = snd_pcm_lib_mmap_vmalloc, |
| 356 | }; |
| 357 | struct snd_pcm *pcm; |
| 358 | int err; |
| 359 | |
| 360 | err = snd_pcm_new(fwspk->card, "OXFW970", 0, 1, 0, &pcm); |
| 361 | if (err < 0) |
| 362 | return err; |
| 363 | pcm->private_data = fwspk; |
| 364 | strcpy(pcm->name, fwspk->device_info->short_name); |
Takashi Sakamoto | b6c44f4 | 2013-07-10 00:22:46 +0900 | [diff] [blame] | 365 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &ops); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | enum control_action { CTL_READ, CTL_WRITE }; |
| 370 | enum control_attribute { |
| 371 | CTL_MIN = 0x02, |
| 372 | CTL_MAX = 0x03, |
| 373 | CTL_CURRENT = 0x10, |
| 374 | }; |
| 375 | |
| 376 | static int fwspk_mute_command(struct fwspk *fwspk, bool *value, |
| 377 | enum control_action action) |
| 378 | { |
| 379 | u8 *buf; |
| 380 | u8 response_ok; |
| 381 | int err; |
| 382 | |
| 383 | buf = kmalloc(11, GFP_KERNEL); |
| 384 | if (!buf) |
| 385 | return -ENOMEM; |
| 386 | |
| 387 | if (action == CTL_READ) { |
| 388 | buf[0] = 0x01; /* AV/C, STATUS */ |
| 389 | response_ok = 0x0c; /* STABLE */ |
| 390 | } else { |
| 391 | buf[0] = 0x00; /* AV/C, CONTROL */ |
| 392 | response_ok = 0x09; /* ACCEPTED */ |
| 393 | } |
| 394 | buf[1] = 0x08; /* audio unit 0 */ |
| 395 | buf[2] = 0xb8; /* FUNCTION BLOCK */ |
| 396 | buf[3] = 0x81; /* function block type: feature */ |
| 397 | buf[4] = fwspk->device_info->mute_fb_id; /* function block ID */ |
| 398 | buf[5] = 0x10; /* control attribute: current */ |
| 399 | buf[6] = 0x02; /* selector length */ |
| 400 | buf[7] = 0x00; /* audio channel number */ |
| 401 | buf[8] = 0x01; /* control selector: mute */ |
| 402 | buf[9] = 0x01; /* control data length */ |
| 403 | if (action == CTL_READ) |
| 404 | buf[10] = 0xff; |
| 405 | else |
| 406 | buf[10] = *value ? 0x70 : 0x60; |
| 407 | |
| 408 | err = fcp_avc_transaction(fwspk->unit, buf, 11, buf, 11, 0x3fe); |
| 409 | if (err < 0) |
| 410 | goto error; |
| 411 | if (err < 11) { |
| 412 | dev_err(&fwspk->unit->device, "short FCP response\n"); |
| 413 | err = -EIO; |
| 414 | goto error; |
| 415 | } |
| 416 | if (buf[0] != response_ok) { |
| 417 | dev_err(&fwspk->unit->device, "mute command failed\n"); |
| 418 | err = -EIO; |
| 419 | goto error; |
| 420 | } |
| 421 | if (action == CTL_READ) |
| 422 | *value = buf[10] == 0x70; |
| 423 | |
| 424 | err = 0; |
| 425 | |
| 426 | error: |
| 427 | kfree(buf); |
| 428 | |
| 429 | return err; |
| 430 | } |
| 431 | |
| 432 | static int fwspk_volume_command(struct fwspk *fwspk, s16 *value, |
| 433 | unsigned int channel, |
| 434 | enum control_attribute attribute, |
| 435 | enum control_action action) |
| 436 | { |
| 437 | u8 *buf; |
| 438 | u8 response_ok; |
| 439 | int err; |
| 440 | |
| 441 | buf = kmalloc(12, GFP_KERNEL); |
| 442 | if (!buf) |
| 443 | return -ENOMEM; |
| 444 | |
| 445 | if (action == CTL_READ) { |
| 446 | buf[0] = 0x01; /* AV/C, STATUS */ |
| 447 | response_ok = 0x0c; /* STABLE */ |
| 448 | } else { |
| 449 | buf[0] = 0x00; /* AV/C, CONTROL */ |
| 450 | response_ok = 0x09; /* ACCEPTED */ |
| 451 | } |
| 452 | buf[1] = 0x08; /* audio unit 0 */ |
| 453 | buf[2] = 0xb8; /* FUNCTION BLOCK */ |
| 454 | buf[3] = 0x81; /* function block type: feature */ |
| 455 | buf[4] = fwspk->device_info->volume_fb_id; /* function block ID */ |
| 456 | buf[5] = attribute; /* control attribute */ |
| 457 | buf[6] = 0x02; /* selector length */ |
| 458 | buf[7] = channel; /* audio channel number */ |
| 459 | buf[8] = 0x02; /* control selector: volume */ |
| 460 | buf[9] = 0x02; /* control data length */ |
| 461 | if (action == CTL_READ) { |
| 462 | buf[10] = 0xff; |
| 463 | buf[11] = 0xff; |
| 464 | } else { |
| 465 | buf[10] = *value >> 8; |
| 466 | buf[11] = *value; |
| 467 | } |
| 468 | |
| 469 | err = fcp_avc_transaction(fwspk->unit, buf, 12, buf, 12, 0x3fe); |
| 470 | if (err < 0) |
| 471 | goto error; |
| 472 | if (err < 12) { |
| 473 | dev_err(&fwspk->unit->device, "short FCP response\n"); |
| 474 | err = -EIO; |
| 475 | goto error; |
| 476 | } |
| 477 | if (buf[0] != response_ok) { |
| 478 | dev_err(&fwspk->unit->device, "volume command failed\n"); |
| 479 | err = -EIO; |
| 480 | goto error; |
| 481 | } |
| 482 | if (action == CTL_READ) |
| 483 | *value = (buf[10] << 8) | buf[11]; |
| 484 | |
| 485 | err = 0; |
| 486 | |
| 487 | error: |
| 488 | kfree(buf); |
| 489 | |
| 490 | return err; |
| 491 | } |
| 492 | |
| 493 | static int fwspk_mute_get(struct snd_kcontrol *control, |
| 494 | struct snd_ctl_elem_value *value) |
| 495 | { |
| 496 | struct fwspk *fwspk = control->private_data; |
| 497 | |
| 498 | value->value.integer.value[0] = !fwspk->mute; |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static int fwspk_mute_put(struct snd_kcontrol *control, |
| 504 | struct snd_ctl_elem_value *value) |
| 505 | { |
| 506 | struct fwspk *fwspk = control->private_data; |
| 507 | bool mute; |
| 508 | int err; |
| 509 | |
| 510 | mute = !value->value.integer.value[0]; |
| 511 | |
| 512 | if (mute == fwspk->mute) |
| 513 | return 0; |
| 514 | |
| 515 | err = fwspk_mute_command(fwspk, &mute, CTL_WRITE); |
| 516 | if (err < 0) |
| 517 | return err; |
| 518 | fwspk->mute = mute; |
| 519 | |
| 520 | return 1; |
| 521 | } |
| 522 | |
| 523 | static int fwspk_volume_info(struct snd_kcontrol *control, |
| 524 | struct snd_ctl_elem_info *info) |
| 525 | { |
| 526 | struct fwspk *fwspk = control->private_data; |
| 527 | |
| 528 | info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 529 | info->count = fwspk->device_info->mixer_channels; |
| 530 | info->value.integer.min = fwspk->volume_min; |
| 531 | info->value.integer.max = fwspk->volume_max; |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static const u8 channel_map[6] = { 0, 1, 4, 5, 2, 3 }; |
| 537 | |
| 538 | static int fwspk_volume_get(struct snd_kcontrol *control, |
| 539 | struct snd_ctl_elem_value *value) |
| 540 | { |
| 541 | struct fwspk *fwspk = control->private_data; |
| 542 | unsigned int i; |
| 543 | |
| 544 | for (i = 0; i < fwspk->device_info->mixer_channels; ++i) |
| 545 | value->value.integer.value[channel_map[i]] = fwspk->volume[i]; |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | static int fwspk_volume_put(struct snd_kcontrol *control, |
| 551 | struct snd_ctl_elem_value *value) |
| 552 | { |
| 553 | struct fwspk *fwspk = control->private_data; |
| 554 | unsigned int i, changed_channels; |
| 555 | bool equal_values = true; |
| 556 | s16 volume; |
| 557 | int err; |
| 558 | |
| 559 | for (i = 0; i < fwspk->device_info->mixer_channels; ++i) { |
| 560 | if (value->value.integer.value[i] < fwspk->volume_min || |
| 561 | value->value.integer.value[i] > fwspk->volume_max) |
| 562 | return -EINVAL; |
| 563 | if (value->value.integer.value[i] != |
| 564 | value->value.integer.value[0]) |
| 565 | equal_values = false; |
| 566 | } |
| 567 | |
| 568 | changed_channels = 0; |
| 569 | for (i = 0; i < fwspk->device_info->mixer_channels; ++i) |
| 570 | if (value->value.integer.value[channel_map[i]] != |
| 571 | fwspk->volume[i]) |
| 572 | changed_channels |= 1 << (i + 1); |
| 573 | |
| 574 | if (equal_values && changed_channels != 0) |
| 575 | changed_channels = 1 << 0; |
| 576 | |
| 577 | for (i = 0; i <= fwspk->device_info->mixer_channels; ++i) { |
| 578 | volume = value->value.integer.value[channel_map[i ? i - 1 : 0]]; |
| 579 | if (changed_channels & (1 << i)) { |
| 580 | err = fwspk_volume_command(fwspk, &volume, i, |
| 581 | CTL_CURRENT, CTL_WRITE); |
| 582 | if (err < 0) |
| 583 | return err; |
| 584 | } |
| 585 | if (i > 0) |
| 586 | fwspk->volume[i - 1] = volume; |
| 587 | } |
| 588 | |
| 589 | return changed_channels != 0; |
| 590 | } |
| 591 | |
| 592 | static int fwspk_create_mixer(struct fwspk *fwspk) |
| 593 | { |
| 594 | static const struct snd_kcontrol_new controls[] = { |
| 595 | { |
| 596 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 597 | .name = "PCM Playback Switch", |
| 598 | .info = snd_ctl_boolean_mono_info, |
| 599 | .get = fwspk_mute_get, |
| 600 | .put = fwspk_mute_put, |
| 601 | }, |
| 602 | { |
| 603 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 604 | .name = "PCM Playback Volume", |
| 605 | .info = fwspk_volume_info, |
| 606 | .get = fwspk_volume_get, |
| 607 | .put = fwspk_volume_put, |
| 608 | }, |
| 609 | }; |
| 610 | unsigned int i, first_ch; |
| 611 | int err; |
| 612 | |
| 613 | err = fwspk_volume_command(fwspk, &fwspk->volume_min, |
| 614 | 0, CTL_MIN, CTL_READ); |
| 615 | if (err < 0) |
| 616 | return err; |
| 617 | err = fwspk_volume_command(fwspk, &fwspk->volume_max, |
| 618 | 0, CTL_MAX, CTL_READ); |
| 619 | if (err < 0) |
| 620 | return err; |
| 621 | |
| 622 | err = fwspk_mute_command(fwspk, &fwspk->mute, CTL_READ); |
| 623 | if (err < 0) |
| 624 | return err; |
| 625 | |
| 626 | first_ch = fwspk->device_info->mixer_channels == 1 ? 0 : 1; |
| 627 | for (i = 0; i < fwspk->device_info->mixer_channels; ++i) { |
| 628 | err = fwspk_volume_command(fwspk, &fwspk->volume[i], |
| 629 | first_ch + i, CTL_CURRENT, CTL_READ); |
| 630 | if (err < 0) |
| 631 | return err; |
| 632 | } |
| 633 | |
| 634 | for (i = 0; i < ARRAY_SIZE(controls); ++i) { |
| 635 | err = snd_ctl_add(fwspk->card, |
| 636 | snd_ctl_new1(&controls[i], fwspk)); |
| 637 | if (err < 0) |
| 638 | return err; |
| 639 | } |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | static u32 fwspk_read_firmware_version(struct fw_unit *unit) |
| 645 | { |
| 646 | __be32 data; |
| 647 | int err; |
| 648 | |
| 649 | err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST, |
| 650 | OXFORD_FIRMWARE_ID_ADDRESS, &data, 4); |
| 651 | return err >= 0 ? be32_to_cpu(data) : 0; |
| 652 | } |
| 653 | |
| 654 | static void fwspk_card_free(struct snd_card *card) |
| 655 | { |
| 656 | struct fwspk *fwspk = card->private_data; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 657 | |
| 658 | amdtp_out_stream_destroy(&fwspk->stream); |
| 659 | cmp_connection_destroy(&fwspk->connection); |
| 660 | fw_unit_put(fwspk->unit); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 661 | mutex_destroy(&fwspk->mutex); |
| 662 | } |
| 663 | |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 664 | static int fwspk_probe(struct fw_unit *unit, |
| 665 | const struct ieee1394_device_id *id) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 666 | { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 667 | struct fw_device *fw_dev = fw_parent_device(unit); |
| 668 | struct snd_card *card; |
| 669 | struct fwspk *fwspk; |
| 670 | u32 firmware; |
| 671 | int err; |
| 672 | |
| 673 | err = snd_card_create(-1, NULL, THIS_MODULE, sizeof(*fwspk), &card); |
| 674 | if (err < 0) |
| 675 | return err; |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 676 | snd_card_set_dev(card, &unit->device); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 677 | |
| 678 | fwspk = card->private_data; |
| 679 | fwspk->card = card; |
| 680 | mutex_init(&fwspk->mutex); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 681 | fwspk->unit = fw_unit_get(unit); |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 682 | fwspk->device_info = (const struct device_info *)id->driver_data; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 683 | |
| 684 | err = cmp_connection_init(&fwspk->connection, unit, 0); |
| 685 | if (err < 0) |
| 686 | goto err_unit; |
| 687 | |
| 688 | err = amdtp_out_stream_init(&fwspk->stream, unit, CIP_NONBLOCKING); |
| 689 | if (err < 0) |
| 690 | goto err_connection; |
| 691 | |
| 692 | card->private_free = fwspk_card_free; |
| 693 | |
| 694 | strcpy(card->driver, fwspk->device_info->driver_name); |
| 695 | strcpy(card->shortname, fwspk->device_info->short_name); |
| 696 | firmware = fwspk_read_firmware_version(unit); |
| 697 | snprintf(card->longname, sizeof(card->longname), |
| 698 | "%s (OXFW%x %04x), GUID %08x%08x at %s, S%d", |
| 699 | fwspk->device_info->long_name, |
| 700 | firmware >> 20, firmware & 0xffff, |
| 701 | fw_dev->config_rom[3], fw_dev->config_rom[4], |
| 702 | dev_name(&unit->device), 100 << fw_dev->max_speed); |
| 703 | strcpy(card->mixername, "OXFW970"); |
| 704 | |
| 705 | err = fwspk_create_pcm(fwspk); |
| 706 | if (err < 0) |
| 707 | goto error; |
| 708 | |
| 709 | err = fwspk_create_mixer(fwspk); |
| 710 | if (err < 0) |
| 711 | goto error; |
| 712 | |
| 713 | err = snd_card_register(card); |
| 714 | if (err < 0) |
| 715 | goto error; |
| 716 | |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 717 | dev_set_drvdata(&unit->device, fwspk); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 718 | |
| 719 | return 0; |
| 720 | |
| 721 | err_connection: |
| 722 | cmp_connection_destroy(&fwspk->connection); |
| 723 | err_unit: |
| 724 | fw_unit_put(fwspk->unit); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 725 | mutex_destroy(&fwspk->mutex); |
| 726 | error: |
| 727 | snd_card_free(card); |
| 728 | return err; |
| 729 | } |
| 730 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 731 | static void fwspk_bus_reset(struct fw_unit *unit) |
| 732 | { |
| 733 | struct fwspk *fwspk = dev_get_drvdata(&unit->device); |
| 734 | |
| 735 | fcp_bus_reset(fwspk->unit); |
| 736 | |
| 737 | if (cmp_connection_update(&fwspk->connection) < 0) { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 738 | amdtp_out_stream_pcm_abort(&fwspk->stream); |
Stefan Richter | a0978e8 | 2011-08-27 16:45:28 +0200 | [diff] [blame] | 739 | mutex_lock(&fwspk->mutex); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 740 | fwspk_stop_stream(fwspk); |
| 741 | mutex_unlock(&fwspk->mutex); |
| 742 | return; |
| 743 | } |
| 744 | |
| 745 | amdtp_out_stream_update(&fwspk->stream); |
| 746 | } |
| 747 | |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 748 | static void fwspk_remove(struct fw_unit *unit) |
| 749 | { |
| 750 | struct fwspk *fwspk = dev_get_drvdata(&unit->device); |
| 751 | |
| 752 | amdtp_out_stream_pcm_abort(&fwspk->stream); |
| 753 | snd_card_disconnect(fwspk->card); |
| 754 | |
| 755 | mutex_lock(&fwspk->mutex); |
| 756 | fwspk_stop_stream(fwspk); |
| 757 | mutex_unlock(&fwspk->mutex); |
| 758 | |
| 759 | snd_card_free_when_closed(fwspk->card); |
| 760 | } |
| 761 | |
| 762 | static const struct device_info griffin_firewave = { |
| 763 | .driver_name = "FireWave", |
| 764 | .short_name = "FireWave", |
| 765 | .long_name = "Griffin FireWave Surround", |
| 766 | .pcm_constraints = firewave_constraints, |
| 767 | .mixer_channels = 6, |
| 768 | .mute_fb_id = 0x01, |
| 769 | .volume_fb_id = 0x02, |
| 770 | }; |
| 771 | |
| 772 | static const struct device_info lacie_speakers = { |
| 773 | .driver_name = "FWSpeakers", |
| 774 | .short_name = "FireWire Speakers", |
| 775 | .long_name = "LaCie FireWire Speakers", |
| 776 | .pcm_constraints = lacie_speakers_constraints, |
| 777 | .mixer_channels = 1, |
| 778 | .mute_fb_id = 0x01, |
| 779 | .volume_fb_id = 0x01, |
| 780 | }; |
| 781 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 782 | static const struct ieee1394_device_id fwspk_id_table[] = { |
| 783 | { |
| 784 | .match_flags = IEEE1394_MATCH_VENDOR_ID | |
| 785 | IEEE1394_MATCH_MODEL_ID | |
| 786 | IEEE1394_MATCH_SPECIFIER_ID | |
| 787 | IEEE1394_MATCH_VERSION, |
| 788 | .vendor_id = VENDOR_GRIFFIN, |
| 789 | .model_id = 0x00f970, |
| 790 | .specifier_id = SPECIFIER_1394TA, |
| 791 | .version = VERSION_AVC, |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 792 | .driver_data = (kernel_ulong_t)&griffin_firewave, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 793 | }, |
| 794 | { |
| 795 | .match_flags = IEEE1394_MATCH_VENDOR_ID | |
| 796 | IEEE1394_MATCH_MODEL_ID | |
| 797 | IEEE1394_MATCH_SPECIFIER_ID | |
| 798 | IEEE1394_MATCH_VERSION, |
| 799 | .vendor_id = VENDOR_LACIE, |
| 800 | .model_id = 0x00f970, |
| 801 | .specifier_id = SPECIFIER_1394TA, |
| 802 | .version = VERSION_AVC, |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 803 | .driver_data = (kernel_ulong_t)&lacie_speakers, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 804 | }, |
| 805 | { } |
| 806 | }; |
| 807 | MODULE_DEVICE_TABLE(ieee1394, fwspk_id_table); |
| 808 | |
| 809 | static struct fw_driver fwspk_driver = { |
| 810 | .driver = { |
| 811 | .owner = THIS_MODULE, |
| 812 | .name = KBUILD_MODNAME, |
| 813 | .bus = &fw_bus_type, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 814 | }, |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 815 | .probe = fwspk_probe, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 816 | .update = fwspk_bus_reset, |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 817 | .remove = fwspk_remove, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 818 | .id_table = fwspk_id_table, |
| 819 | }; |
| 820 | |
| 821 | static int __init alsa_fwspk_init(void) |
| 822 | { |
| 823 | return driver_register(&fwspk_driver.driver); |
| 824 | } |
| 825 | |
| 826 | static void __exit alsa_fwspk_exit(void) |
| 827 | { |
| 828 | driver_unregister(&fwspk_driver.driver); |
| 829 | } |
| 830 | |
| 831 | module_init(alsa_fwspk_init); |
| 832 | module_exit(alsa_fwspk_exit); |