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