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; |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 54 | struct amdtp_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 | |
Takashi Sakamoto | 7b2d99f | 2014-04-25 22:44:52 +0900 | [diff] [blame] | 170 | err = amdtp_stream_add_pcm_hw_constraints(&fwspk->stream, runtime); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 171 | if (err < 0) |
| 172 | return err; |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int fwspk_close(struct snd_pcm_substream *substream) |
| 178 | { |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static void fwspk_stop_stream(struct fwspk *fwspk) |
| 183 | { |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 184 | if (amdtp_stream_running(&fwspk->stream)) { |
| 185 | amdtp_stream_stop(&fwspk->stream); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 186 | cmp_connection_break(&fwspk->connection); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 190 | static int fwspk_hw_params(struct snd_pcm_substream *substream, |
| 191 | struct snd_pcm_hw_params *hw_params) |
| 192 | { |
| 193 | struct fwspk *fwspk = substream->private_data; |
| 194 | int err; |
| 195 | |
| 196 | mutex_lock(&fwspk->mutex); |
| 197 | fwspk_stop_stream(fwspk); |
| 198 | mutex_unlock(&fwspk->mutex); |
| 199 | |
| 200 | err = snd_pcm_lib_alloc_vmalloc_buffer(substream, |
| 201 | params_buffer_bytes(hw_params)); |
| 202 | if (err < 0) |
| 203 | goto error; |
| 204 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 205 | amdtp_stream_set_parameters(&fwspk->stream, |
| 206 | params_rate(hw_params), |
| 207 | params_channels(hw_params), |
| 208 | 0); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 209 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 210 | amdtp_stream_set_pcm_format(&fwspk->stream, |
| 211 | params_format(hw_params)); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 212 | |
Takashi Sakamoto | 1017abe | 2014-04-25 22:44:59 +0900 | [diff] [blame] | 213 | err = avc_general_set_sig_fmt(fwspk->unit, params_rate(hw_params), |
| 214 | AVC_GENERAL_PLUG_DIR_IN, 0); |
| 215 | if (err < 0) { |
| 216 | dev_err(&fwspk->unit->device, "failed to set sample rate\n"); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 217 | goto err_buffer; |
Takashi Sakamoto | 1017abe | 2014-04-25 22:44:59 +0900 | [diff] [blame] | 218 | } |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 219 | |
| 220 | return 0; |
| 221 | |
| 222 | err_buffer: |
| 223 | snd_pcm_lib_free_vmalloc_buffer(substream); |
| 224 | error: |
| 225 | return err; |
| 226 | } |
| 227 | |
| 228 | static int fwspk_hw_free(struct snd_pcm_substream *substream) |
| 229 | { |
| 230 | struct fwspk *fwspk = substream->private_data; |
| 231 | |
| 232 | mutex_lock(&fwspk->mutex); |
| 233 | fwspk_stop_stream(fwspk); |
| 234 | mutex_unlock(&fwspk->mutex); |
| 235 | |
| 236 | return snd_pcm_lib_free_vmalloc_buffer(substream); |
| 237 | } |
| 238 | |
| 239 | static int fwspk_prepare(struct snd_pcm_substream *substream) |
| 240 | { |
| 241 | struct fwspk *fwspk = substream->private_data; |
| 242 | int err; |
| 243 | |
| 244 | mutex_lock(&fwspk->mutex); |
| 245 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 246 | if (amdtp_streaming_error(&fwspk->stream)) |
Clemens Ladisch | ec00f5e | 2011-03-15 07:57:24 +0100 | [diff] [blame] | 247 | fwspk_stop_stream(fwspk); |
| 248 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 249 | if (!amdtp_stream_running(&fwspk->stream)) { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 250 | err = cmp_connection_establish(&fwspk->connection, |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 251 | amdtp_stream_get_max_payload(&fwspk->stream)); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 252 | if (err < 0) |
| 253 | goto err_mutex; |
| 254 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 255 | err = amdtp_stream_start(&fwspk->stream, |
| 256 | fwspk->connection.resources.channel, |
| 257 | fwspk->connection.speed); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 258 | if (err < 0) |
| 259 | goto err_connection; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | mutex_unlock(&fwspk->mutex); |
| 263 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 264 | amdtp_stream_pcm_prepare(&fwspk->stream); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 265 | |
| 266 | return 0; |
| 267 | |
| 268 | err_connection: |
| 269 | cmp_connection_break(&fwspk->connection); |
| 270 | err_mutex: |
| 271 | mutex_unlock(&fwspk->mutex); |
| 272 | |
| 273 | return err; |
| 274 | } |
| 275 | |
| 276 | static int fwspk_trigger(struct snd_pcm_substream *substream, int cmd) |
| 277 | { |
| 278 | struct fwspk *fwspk = substream->private_data; |
| 279 | struct snd_pcm_substream *pcm; |
| 280 | |
| 281 | switch (cmd) { |
| 282 | case SNDRV_PCM_TRIGGER_START: |
| 283 | pcm = substream; |
| 284 | break; |
| 285 | case SNDRV_PCM_TRIGGER_STOP: |
| 286 | pcm = NULL; |
| 287 | break; |
| 288 | default: |
| 289 | return -EINVAL; |
| 290 | } |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 291 | amdtp_stream_pcm_trigger(&fwspk->stream, pcm); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static snd_pcm_uframes_t fwspk_pointer(struct snd_pcm_substream *substream) |
| 296 | { |
| 297 | struct fwspk *fwspk = substream->private_data; |
| 298 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 299 | return amdtp_stream_pcm_pointer(&fwspk->stream); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static int fwspk_create_pcm(struct fwspk *fwspk) |
| 303 | { |
| 304 | static struct snd_pcm_ops ops = { |
| 305 | .open = fwspk_open, |
| 306 | .close = fwspk_close, |
| 307 | .ioctl = snd_pcm_lib_ioctl, |
| 308 | .hw_params = fwspk_hw_params, |
| 309 | .hw_free = fwspk_hw_free, |
| 310 | .prepare = fwspk_prepare, |
| 311 | .trigger = fwspk_trigger, |
| 312 | .pointer = fwspk_pointer, |
| 313 | .page = snd_pcm_lib_get_vmalloc_page, |
| 314 | .mmap = snd_pcm_lib_mmap_vmalloc, |
| 315 | }; |
| 316 | struct snd_pcm *pcm; |
| 317 | int err; |
| 318 | |
| 319 | err = snd_pcm_new(fwspk->card, "OXFW970", 0, 1, 0, &pcm); |
| 320 | if (err < 0) |
| 321 | return err; |
| 322 | pcm->private_data = fwspk; |
| 323 | strcpy(pcm->name, fwspk->device_info->short_name); |
Takashi Sakamoto | b6c44f4 | 2013-07-10 00:22:46 +0900 | [diff] [blame] | 324 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &ops); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | enum control_action { CTL_READ, CTL_WRITE }; |
| 329 | enum control_attribute { |
| 330 | CTL_MIN = 0x02, |
| 331 | CTL_MAX = 0x03, |
| 332 | CTL_CURRENT = 0x10, |
| 333 | }; |
| 334 | |
| 335 | static int fwspk_mute_command(struct fwspk *fwspk, bool *value, |
| 336 | enum control_action action) |
| 337 | { |
| 338 | u8 *buf; |
| 339 | u8 response_ok; |
| 340 | int err; |
| 341 | |
| 342 | buf = kmalloc(11, GFP_KERNEL); |
| 343 | if (!buf) |
| 344 | return -ENOMEM; |
| 345 | |
| 346 | if (action == CTL_READ) { |
| 347 | buf[0] = 0x01; /* AV/C, STATUS */ |
| 348 | response_ok = 0x0c; /* STABLE */ |
| 349 | } else { |
| 350 | buf[0] = 0x00; /* AV/C, CONTROL */ |
| 351 | response_ok = 0x09; /* ACCEPTED */ |
| 352 | } |
| 353 | buf[1] = 0x08; /* audio unit 0 */ |
| 354 | buf[2] = 0xb8; /* FUNCTION BLOCK */ |
| 355 | buf[3] = 0x81; /* function block type: feature */ |
| 356 | buf[4] = fwspk->device_info->mute_fb_id; /* function block ID */ |
| 357 | buf[5] = 0x10; /* control attribute: current */ |
| 358 | buf[6] = 0x02; /* selector length */ |
| 359 | buf[7] = 0x00; /* audio channel number */ |
| 360 | buf[8] = 0x01; /* control selector: mute */ |
| 361 | buf[9] = 0x01; /* control data length */ |
| 362 | if (action == CTL_READ) |
| 363 | buf[10] = 0xff; |
| 364 | else |
| 365 | buf[10] = *value ? 0x70 : 0x60; |
| 366 | |
| 367 | err = fcp_avc_transaction(fwspk->unit, buf, 11, buf, 11, 0x3fe); |
| 368 | if (err < 0) |
| 369 | goto error; |
| 370 | if (err < 11) { |
| 371 | dev_err(&fwspk->unit->device, "short FCP response\n"); |
| 372 | err = -EIO; |
| 373 | goto error; |
| 374 | } |
| 375 | if (buf[0] != response_ok) { |
| 376 | dev_err(&fwspk->unit->device, "mute command failed\n"); |
| 377 | err = -EIO; |
| 378 | goto error; |
| 379 | } |
| 380 | if (action == CTL_READ) |
| 381 | *value = buf[10] == 0x70; |
| 382 | |
| 383 | err = 0; |
| 384 | |
| 385 | error: |
| 386 | kfree(buf); |
| 387 | |
| 388 | return err; |
| 389 | } |
| 390 | |
| 391 | static int fwspk_volume_command(struct fwspk *fwspk, s16 *value, |
| 392 | unsigned int channel, |
| 393 | enum control_attribute attribute, |
| 394 | enum control_action action) |
| 395 | { |
| 396 | u8 *buf; |
| 397 | u8 response_ok; |
| 398 | int err; |
| 399 | |
| 400 | buf = kmalloc(12, GFP_KERNEL); |
| 401 | if (!buf) |
| 402 | return -ENOMEM; |
| 403 | |
| 404 | if (action == CTL_READ) { |
| 405 | buf[0] = 0x01; /* AV/C, STATUS */ |
| 406 | response_ok = 0x0c; /* STABLE */ |
| 407 | } else { |
| 408 | buf[0] = 0x00; /* AV/C, CONTROL */ |
| 409 | response_ok = 0x09; /* ACCEPTED */ |
| 410 | } |
| 411 | buf[1] = 0x08; /* audio unit 0 */ |
| 412 | buf[2] = 0xb8; /* FUNCTION BLOCK */ |
| 413 | buf[3] = 0x81; /* function block type: feature */ |
| 414 | buf[4] = fwspk->device_info->volume_fb_id; /* function block ID */ |
| 415 | buf[5] = attribute; /* control attribute */ |
| 416 | buf[6] = 0x02; /* selector length */ |
| 417 | buf[7] = channel; /* audio channel number */ |
| 418 | buf[8] = 0x02; /* control selector: volume */ |
| 419 | buf[9] = 0x02; /* control data length */ |
| 420 | if (action == CTL_READ) { |
| 421 | buf[10] = 0xff; |
| 422 | buf[11] = 0xff; |
| 423 | } else { |
| 424 | buf[10] = *value >> 8; |
| 425 | buf[11] = *value; |
| 426 | } |
| 427 | |
| 428 | err = fcp_avc_transaction(fwspk->unit, buf, 12, buf, 12, 0x3fe); |
| 429 | if (err < 0) |
| 430 | goto error; |
| 431 | if (err < 12) { |
| 432 | dev_err(&fwspk->unit->device, "short FCP response\n"); |
| 433 | err = -EIO; |
| 434 | goto error; |
| 435 | } |
| 436 | if (buf[0] != response_ok) { |
| 437 | dev_err(&fwspk->unit->device, "volume command failed\n"); |
| 438 | err = -EIO; |
| 439 | goto error; |
| 440 | } |
| 441 | if (action == CTL_READ) |
| 442 | *value = (buf[10] << 8) | buf[11]; |
| 443 | |
| 444 | err = 0; |
| 445 | |
| 446 | error: |
| 447 | kfree(buf); |
| 448 | |
| 449 | return err; |
| 450 | } |
| 451 | |
| 452 | static int fwspk_mute_get(struct snd_kcontrol *control, |
| 453 | struct snd_ctl_elem_value *value) |
| 454 | { |
| 455 | struct fwspk *fwspk = control->private_data; |
| 456 | |
| 457 | value->value.integer.value[0] = !fwspk->mute; |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static int fwspk_mute_put(struct snd_kcontrol *control, |
| 463 | struct snd_ctl_elem_value *value) |
| 464 | { |
| 465 | struct fwspk *fwspk = control->private_data; |
| 466 | bool mute; |
| 467 | int err; |
| 468 | |
| 469 | mute = !value->value.integer.value[0]; |
| 470 | |
| 471 | if (mute == fwspk->mute) |
| 472 | return 0; |
| 473 | |
| 474 | err = fwspk_mute_command(fwspk, &mute, CTL_WRITE); |
| 475 | if (err < 0) |
| 476 | return err; |
| 477 | fwspk->mute = mute; |
| 478 | |
| 479 | return 1; |
| 480 | } |
| 481 | |
| 482 | static int fwspk_volume_info(struct snd_kcontrol *control, |
| 483 | struct snd_ctl_elem_info *info) |
| 484 | { |
| 485 | struct fwspk *fwspk = control->private_data; |
| 486 | |
| 487 | info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 488 | info->count = fwspk->device_info->mixer_channels; |
| 489 | info->value.integer.min = fwspk->volume_min; |
| 490 | info->value.integer.max = fwspk->volume_max; |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static const u8 channel_map[6] = { 0, 1, 4, 5, 2, 3 }; |
| 496 | |
| 497 | static int fwspk_volume_get(struct snd_kcontrol *control, |
| 498 | struct snd_ctl_elem_value *value) |
| 499 | { |
| 500 | struct fwspk *fwspk = control->private_data; |
| 501 | unsigned int i; |
| 502 | |
| 503 | for (i = 0; i < fwspk->device_info->mixer_channels; ++i) |
| 504 | value->value.integer.value[channel_map[i]] = fwspk->volume[i]; |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static int fwspk_volume_put(struct snd_kcontrol *control, |
| 510 | struct snd_ctl_elem_value *value) |
| 511 | { |
| 512 | struct fwspk *fwspk = control->private_data; |
| 513 | unsigned int i, changed_channels; |
| 514 | bool equal_values = true; |
| 515 | s16 volume; |
| 516 | int err; |
| 517 | |
| 518 | for (i = 0; i < fwspk->device_info->mixer_channels; ++i) { |
| 519 | if (value->value.integer.value[i] < fwspk->volume_min || |
| 520 | value->value.integer.value[i] > fwspk->volume_max) |
| 521 | return -EINVAL; |
| 522 | if (value->value.integer.value[i] != |
| 523 | value->value.integer.value[0]) |
| 524 | equal_values = false; |
| 525 | } |
| 526 | |
| 527 | changed_channels = 0; |
| 528 | for (i = 0; i < fwspk->device_info->mixer_channels; ++i) |
| 529 | if (value->value.integer.value[channel_map[i]] != |
| 530 | fwspk->volume[i]) |
| 531 | changed_channels |= 1 << (i + 1); |
| 532 | |
| 533 | if (equal_values && changed_channels != 0) |
| 534 | changed_channels = 1 << 0; |
| 535 | |
| 536 | for (i = 0; i <= fwspk->device_info->mixer_channels; ++i) { |
| 537 | volume = value->value.integer.value[channel_map[i ? i - 1 : 0]]; |
| 538 | if (changed_channels & (1 << i)) { |
| 539 | err = fwspk_volume_command(fwspk, &volume, i, |
| 540 | CTL_CURRENT, CTL_WRITE); |
| 541 | if (err < 0) |
| 542 | return err; |
| 543 | } |
| 544 | if (i > 0) |
| 545 | fwspk->volume[i - 1] = volume; |
| 546 | } |
| 547 | |
| 548 | return changed_channels != 0; |
| 549 | } |
| 550 | |
| 551 | static int fwspk_create_mixer(struct fwspk *fwspk) |
| 552 | { |
| 553 | static const struct snd_kcontrol_new controls[] = { |
| 554 | { |
| 555 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 556 | .name = "PCM Playback Switch", |
| 557 | .info = snd_ctl_boolean_mono_info, |
| 558 | .get = fwspk_mute_get, |
| 559 | .put = fwspk_mute_put, |
| 560 | }, |
| 561 | { |
| 562 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 563 | .name = "PCM Playback Volume", |
| 564 | .info = fwspk_volume_info, |
| 565 | .get = fwspk_volume_get, |
| 566 | .put = fwspk_volume_put, |
| 567 | }, |
| 568 | }; |
| 569 | unsigned int i, first_ch; |
| 570 | int err; |
| 571 | |
| 572 | err = fwspk_volume_command(fwspk, &fwspk->volume_min, |
| 573 | 0, CTL_MIN, CTL_READ); |
| 574 | if (err < 0) |
| 575 | return err; |
| 576 | err = fwspk_volume_command(fwspk, &fwspk->volume_max, |
| 577 | 0, CTL_MAX, CTL_READ); |
| 578 | if (err < 0) |
| 579 | return err; |
| 580 | |
| 581 | err = fwspk_mute_command(fwspk, &fwspk->mute, CTL_READ); |
| 582 | if (err < 0) |
| 583 | return err; |
| 584 | |
| 585 | first_ch = fwspk->device_info->mixer_channels == 1 ? 0 : 1; |
| 586 | for (i = 0; i < fwspk->device_info->mixer_channels; ++i) { |
| 587 | err = fwspk_volume_command(fwspk, &fwspk->volume[i], |
| 588 | first_ch + i, CTL_CURRENT, CTL_READ); |
| 589 | if (err < 0) |
| 590 | return err; |
| 591 | } |
| 592 | |
| 593 | for (i = 0; i < ARRAY_SIZE(controls); ++i) { |
| 594 | err = snd_ctl_add(fwspk->card, |
| 595 | snd_ctl_new1(&controls[i], fwspk)); |
| 596 | if (err < 0) |
| 597 | return err; |
| 598 | } |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | static u32 fwspk_read_firmware_version(struct fw_unit *unit) |
| 604 | { |
| 605 | __be32 data; |
| 606 | int err; |
| 607 | |
| 608 | err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST, |
Clemens Ladisch | 1b70485 | 2011-09-04 22:17:38 +0200 | [diff] [blame] | 609 | OXFORD_FIRMWARE_ID_ADDRESS, &data, 4, 0); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 610 | return err >= 0 ? be32_to_cpu(data) : 0; |
| 611 | } |
| 612 | |
| 613 | static void fwspk_card_free(struct snd_card *card) |
| 614 | { |
| 615 | struct fwspk *fwspk = card->private_data; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 616 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 617 | amdtp_stream_destroy(&fwspk->stream); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 618 | cmp_connection_destroy(&fwspk->connection); |
| 619 | fw_unit_put(fwspk->unit); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 620 | mutex_destroy(&fwspk->mutex); |
| 621 | } |
| 622 | |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 623 | static int fwspk_probe(struct fw_unit *unit, |
| 624 | const struct ieee1394_device_id *id) |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 625 | { |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 626 | struct fw_device *fw_dev = fw_parent_device(unit); |
| 627 | struct snd_card *card; |
| 628 | struct fwspk *fwspk; |
| 629 | u32 firmware; |
| 630 | int err; |
| 631 | |
Takashi Iwai | 06b45f0 | 2014-01-29 14:23:55 +0100 | [diff] [blame] | 632 | err = snd_card_new(&unit->device, -1, NULL, THIS_MODULE, |
| 633 | sizeof(*fwspk), &card); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 634 | if (err < 0) |
| 635 | return err; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 636 | |
| 637 | fwspk = card->private_data; |
| 638 | fwspk->card = card; |
| 639 | mutex_init(&fwspk->mutex); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 640 | fwspk->unit = fw_unit_get(unit); |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 641 | fwspk->device_info = (const struct device_info *)id->driver_data; |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 642 | |
Takashi Sakamoto | c68a1c6 | 2014-04-25 22:44:55 +0900 | [diff] [blame] | 643 | err = cmp_connection_init(&fwspk->connection, unit, CMP_INPUT, 0); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 644 | if (err < 0) |
| 645 | goto err_unit; |
| 646 | |
Takashi Sakamoto | 3ff7e8f | 2014-04-25 22:44:44 +0900 | [diff] [blame] | 647 | err = amdtp_stream_init(&fwspk->stream, unit, AMDTP_OUT_STREAM, |
| 648 | CIP_NONBLOCKING); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 649 | if (err < 0) |
| 650 | goto err_connection; |
| 651 | |
| 652 | card->private_free = fwspk_card_free; |
| 653 | |
| 654 | strcpy(card->driver, fwspk->device_info->driver_name); |
| 655 | strcpy(card->shortname, fwspk->device_info->short_name); |
| 656 | firmware = fwspk_read_firmware_version(unit); |
| 657 | snprintf(card->longname, sizeof(card->longname), |
| 658 | "%s (OXFW%x %04x), GUID %08x%08x at %s, S%d", |
| 659 | fwspk->device_info->long_name, |
| 660 | firmware >> 20, firmware & 0xffff, |
| 661 | fw_dev->config_rom[3], fw_dev->config_rom[4], |
| 662 | dev_name(&unit->device), 100 << fw_dev->max_speed); |
| 663 | strcpy(card->mixername, "OXFW970"); |
| 664 | |
| 665 | err = fwspk_create_pcm(fwspk); |
| 666 | if (err < 0) |
| 667 | goto error; |
| 668 | |
| 669 | err = fwspk_create_mixer(fwspk); |
| 670 | if (err < 0) |
| 671 | goto error; |
| 672 | |
| 673 | err = snd_card_register(card); |
| 674 | if (err < 0) |
| 675 | goto error; |
| 676 | |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 677 | dev_set_drvdata(&unit->device, fwspk); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 678 | |
| 679 | return 0; |
| 680 | |
| 681 | err_connection: |
| 682 | cmp_connection_destroy(&fwspk->connection); |
| 683 | err_unit: |
| 684 | fw_unit_put(fwspk->unit); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 685 | mutex_destroy(&fwspk->mutex); |
| 686 | error: |
| 687 | snd_card_free(card); |
| 688 | return err; |
| 689 | } |
| 690 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 691 | static void fwspk_bus_reset(struct fw_unit *unit) |
| 692 | { |
| 693 | struct fwspk *fwspk = dev_get_drvdata(&unit->device); |
| 694 | |
| 695 | fcp_bus_reset(fwspk->unit); |
| 696 | |
| 697 | if (cmp_connection_update(&fwspk->connection) < 0) { |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 698 | amdtp_stream_pcm_abort(&fwspk->stream); |
Stefan Richter | a0978e8 | 2011-08-27 16:45:28 +0200 | [diff] [blame] | 699 | mutex_lock(&fwspk->mutex); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 700 | fwspk_stop_stream(fwspk); |
| 701 | mutex_unlock(&fwspk->mutex); |
| 702 | return; |
| 703 | } |
| 704 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 705 | amdtp_stream_update(&fwspk->stream); |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 706 | } |
| 707 | |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 708 | static void fwspk_remove(struct fw_unit *unit) |
| 709 | { |
| 710 | struct fwspk *fwspk = dev_get_drvdata(&unit->device); |
| 711 | |
Takashi Sakamoto | be4a289 | 2014-04-25 22:44:42 +0900 | [diff] [blame] | 712 | amdtp_stream_pcm_abort(&fwspk->stream); |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 713 | snd_card_disconnect(fwspk->card); |
| 714 | |
| 715 | mutex_lock(&fwspk->mutex); |
| 716 | fwspk_stop_stream(fwspk); |
| 717 | mutex_unlock(&fwspk->mutex); |
| 718 | |
| 719 | snd_card_free_when_closed(fwspk->card); |
| 720 | } |
| 721 | |
| 722 | static const struct device_info griffin_firewave = { |
| 723 | .driver_name = "FireWave", |
| 724 | .short_name = "FireWave", |
| 725 | .long_name = "Griffin FireWave Surround", |
| 726 | .pcm_constraints = firewave_constraints, |
| 727 | .mixer_channels = 6, |
| 728 | .mute_fb_id = 0x01, |
| 729 | .volume_fb_id = 0x02, |
| 730 | }; |
| 731 | |
| 732 | static const struct device_info lacie_speakers = { |
| 733 | .driver_name = "FWSpeakers", |
| 734 | .short_name = "FireWire Speakers", |
| 735 | .long_name = "LaCie FireWire Speakers", |
| 736 | .pcm_constraints = lacie_speakers_constraints, |
| 737 | .mixer_channels = 1, |
| 738 | .mute_fb_id = 0x01, |
| 739 | .volume_fb_id = 0x01, |
| 740 | }; |
| 741 | |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 742 | static const struct ieee1394_device_id fwspk_id_table[] = { |
| 743 | { |
| 744 | .match_flags = IEEE1394_MATCH_VENDOR_ID | |
| 745 | IEEE1394_MATCH_MODEL_ID | |
| 746 | IEEE1394_MATCH_SPECIFIER_ID | |
| 747 | IEEE1394_MATCH_VERSION, |
| 748 | .vendor_id = VENDOR_GRIFFIN, |
| 749 | .model_id = 0x00f970, |
| 750 | .specifier_id = SPECIFIER_1394TA, |
| 751 | .version = VERSION_AVC, |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 752 | .driver_data = (kernel_ulong_t)&griffin_firewave, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 753 | }, |
| 754 | { |
| 755 | .match_flags = IEEE1394_MATCH_VENDOR_ID | |
| 756 | IEEE1394_MATCH_MODEL_ID | |
| 757 | IEEE1394_MATCH_SPECIFIER_ID | |
| 758 | IEEE1394_MATCH_VERSION, |
| 759 | .vendor_id = VENDOR_LACIE, |
| 760 | .model_id = 0x00f970, |
| 761 | .specifier_id = SPECIFIER_1394TA, |
| 762 | .version = VERSION_AVC, |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 763 | .driver_data = (kernel_ulong_t)&lacie_speakers, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 764 | }, |
| 765 | { } |
| 766 | }; |
| 767 | MODULE_DEVICE_TABLE(ieee1394, fwspk_id_table); |
| 768 | |
| 769 | static struct fw_driver fwspk_driver = { |
| 770 | .driver = { |
| 771 | .owner = THIS_MODULE, |
| 772 | .name = KBUILD_MODNAME, |
| 773 | .bus = &fw_bus_type, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 774 | }, |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 775 | .probe = fwspk_probe, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 776 | .update = fwspk_bus_reset, |
Stefan Richter | 94a8715 | 2013-06-09 18:15:00 +0200 | [diff] [blame] | 777 | .remove = fwspk_remove, |
Clemens Ladisch | 31ef913 | 2011-03-15 07:53:21 +0100 | [diff] [blame] | 778 | .id_table = fwspk_id_table, |
| 779 | }; |
| 780 | |
| 781 | static int __init alsa_fwspk_init(void) |
| 782 | { |
| 783 | return driver_register(&fwspk_driver.driver); |
| 784 | } |
| 785 | |
| 786 | static void __exit alsa_fwspk_exit(void) |
| 787 | { |
| 788 | driver_unregister(&fwspk_driver.driver); |
| 789 | } |
| 790 | |
| 791 | module_init(alsa_fwspk_init); |
| 792 | module_exit(alsa_fwspk_exit); |