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