From e2786ca648d780d106bd8abca06746eb30d15ee7 Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Sat, 29 Nov 2014 00:59:27 +0900 Subject: ALSA: oxfw: Split stream functionality to a new file and add a header file This is a help for works in followed patches. And this commit remove 'fw_unit_get()/fw_unit_put()' because these are called by helper functions in 'snd-firewire-lib'. Signed-off-by: Takashi Sakamoto Signed-off-by: Takashi Iwai --- sound/firewire/oxfw/oxfw-stream.c | 80 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 sound/firewire/oxfw/oxfw-stream.c (limited to 'sound/firewire/oxfw/oxfw-stream.c') diff --git a/sound/firewire/oxfw/oxfw-stream.c b/sound/firewire/oxfw/oxfw-stream.c new file mode 100644 index 000000000000..ebd156f3e29d --- /dev/null +++ b/sound/firewire/oxfw/oxfw-stream.c @@ -0,0 +1,80 @@ +/* + * oxfw_stream.c - a part of driver for OXFW970/971 based devices + * + * Copyright (c) 2014 Takashi Sakamoto + * + * Licensed under the terms of the GNU General Public License, version 2. + */ + +#include "oxfw.h" + +int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw) +{ + int err; + + err = cmp_connection_init(&oxfw->in_conn, oxfw->unit, + CMP_INPUT, 0); + if (err < 0) + goto end; + + err = amdtp_stream_init(&oxfw->rx_stream, oxfw->unit, + AMDTP_OUT_STREAM, CIP_NONBLOCKING); + if (err < 0) { + amdtp_stream_destroy(&oxfw->rx_stream); + cmp_connection_destroy(&oxfw->in_conn); + } +end: + return err; +} + +static void stop_stream(struct snd_oxfw *oxfw) +{ + amdtp_stream_pcm_abort(&oxfw->rx_stream); + amdtp_stream_stop(&oxfw->rx_stream); + cmp_connection_break(&oxfw->in_conn); +} + +int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw) +{ + int err = 0; + + if (amdtp_streaming_error(&oxfw->rx_stream)) + stop_stream(oxfw); + + if (amdtp_stream_running(&oxfw->rx_stream)) + goto end; + + err = cmp_connection_establish(&oxfw->in_conn, + amdtp_stream_get_max_payload(&oxfw->rx_stream)); + if (err < 0) + goto end; + + err = amdtp_stream_start(&oxfw->rx_stream, + oxfw->in_conn.resources.channel, + oxfw->in_conn.speed); + if (err < 0) + stop_stream(oxfw); +end: + return err; +} + +void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw) +{ + stop_stream(oxfw); +} + +void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw) +{ + stop_stream(oxfw); + + amdtp_stream_destroy(&oxfw->rx_stream); + cmp_connection_destroy(&oxfw->in_conn); +} + +void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw) +{ + if (cmp_connection_update(&oxfw->in_conn) < 0) + stop_stream(oxfw); + else + amdtp_stream_update(&oxfw->rx_stream); +} -- cgit v1.2.3 From 5cd1d3f47a6321612a51ab88ffe8ef65120fcbe0 Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Tue, 9 Dec 2014 00:10:42 +0900 Subject: ALSA: oxfw: Change the way to make PCM rules/constraints In previous commit, this driver can get to know stream formations at each supported sampling rates. This commit uses it to make PCM rules/constraints and obsoletes hard-coded rules/constraints. For this purpose, this commit adds 'struct snd_oxfw_stream_formation' and snd_oxfw_stream_parse_format() to parse data channel formation of data block. According to datasheet of OXFW970/971, they support 32.0kHz to 196.0kHz. As long as developers investigate, some devices are confirmed to have several formats for the same sampling rate. Signed-off-by: Takashi Sakamoto Acked-by: Clemens Ladisch Signed-off-by: Takashi Iwai --- sound/firewire/oxfw/oxfw-pcm.c | 197 ++++++++++++++++------------ sound/firewire/oxfw/oxfw-stream.c | 268 ++++++++++++++++++++++++++++++++++++++ sound/firewire/oxfw/oxfw.c | 10 +- sound/firewire/oxfw/oxfw.h | 21 ++- 4 files changed, 410 insertions(+), 86 deletions(-) (limited to 'sound/firewire/oxfw/oxfw-stream.c') diff --git a/sound/firewire/oxfw/oxfw-pcm.c b/sound/firewire/oxfw/oxfw-pcm.c index d39f17a8f8c0..0c0be984edce 100644 --- a/sound/firewire/oxfw/oxfw-pcm.c +++ b/sound/firewire/oxfw/oxfw-pcm.c @@ -7,117 +7,152 @@ #include "oxfw.h" -static int firewave_rate_constraint(struct snd_pcm_hw_params *params, - struct snd_pcm_hw_rule *rule) +static int hw_rule_rate(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) { - static unsigned int stereo_rates[] = { 48000, 96000 }; - struct snd_interval *channels = - hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); - struct snd_interval *rate = - hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); - - /* two channels work only at 48/96 kHz */ - if (snd_interval_max(channels) < 6) - return snd_interval_list(rate, 2, stereo_rates, 0); - return 0; + u8 **formats = rule->private; + struct snd_interval *r = + hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); + const struct snd_interval *c = + hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS); + struct snd_interval t = { + .min = UINT_MAX, .max = 0, .integer = 1 + }; + struct snd_oxfw_stream_formation formation; + unsigned int i, err; + + for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { + if (formats[i] == NULL) + continue; + + err = snd_oxfw_stream_parse_format(formats[i], &formation); + if (err < 0) + continue; + if (!snd_interval_test(c, formation.pcm)) + continue; + + t.min = min(t.min, formation.rate); + t.max = max(t.max, formation.rate); + + } + return snd_interval_refine(r, &t); } -static int firewave_channels_constraint(struct snd_pcm_hw_params *params, - struct snd_pcm_hw_rule *rule) +static int hw_rule_channels(struct snd_pcm_hw_params *params, + struct snd_pcm_hw_rule *rule) { - static const struct snd_interval all_channels = { .min = 6, .max = 6 }; - struct snd_interval *rate = - hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); - struct snd_interval *channels = - hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); - - /* 32/44.1 kHz work only with all six channels */ - if (snd_interval_max(rate) < 48000) - return snd_interval_refine(channels, &all_channels); - return 0; + u8 **formats = rule->private; + struct snd_interval *c = + hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); + const struct snd_interval *r = + hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE); + struct snd_oxfw_stream_formation formation; + unsigned int i, j, err; + unsigned int count, list[SND_OXFW_STREAM_FORMAT_ENTRIES] = {0}; + + count = 0; + for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { + if (formats[i] == NULL) + break; + + err = snd_oxfw_stream_parse_format(formats[i], &formation); + if (err < 0) + continue; + if (!snd_interval_test(r, formation.rate)) + continue; + if (list[count] == formation.pcm) + continue; + + for (j = 0; j < ARRAY_SIZE(list); j++) { + if (list[j] == formation.pcm) + break; + } + if (j == ARRAY_SIZE(list)) { + list[count] = formation.pcm; + if (++count == ARRAY_SIZE(list)) + break; + } + } + + return snd_interval_list(c, count, list, 0); } -int firewave_constraints(struct snd_pcm_runtime *runtime) +static void limit_channels_and_rates(struct snd_pcm_hardware *hw, u8 **formats) { - static unsigned int channels_list[] = { 2, 6 }; - static struct snd_pcm_hw_constraint_list channels_list_constraint = { - .count = 2, - .list = channels_list, - }; - int err; + struct snd_oxfw_stream_formation formation; + unsigned int i, err; - runtime->hw.rates = SNDRV_PCM_RATE_32000 | - SNDRV_PCM_RATE_44100 | - SNDRV_PCM_RATE_48000 | - SNDRV_PCM_RATE_96000; - runtime->hw.channels_max = 6; + hw->channels_min = UINT_MAX; + hw->channels_max = 0; - err = snd_pcm_hw_constraint_list(runtime, 0, - SNDRV_PCM_HW_PARAM_CHANNELS, - &channels_list_constraint); - if (err < 0) - return err; - err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, - firewave_rate_constraint, NULL, - SNDRV_PCM_HW_PARAM_CHANNELS, -1); - if (err < 0) - return err; - err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, - firewave_channels_constraint, NULL, - SNDRV_PCM_HW_PARAM_RATE, -1); - if (err < 0) - return err; + hw->rate_min = UINT_MAX; + hw->rate_max = 0; + hw->rates = 0; - return 0; + for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { + if (formats[i] == NULL) + break; + + err = snd_oxfw_stream_parse_format(formats[i], &formation); + if (err < 0) + continue; + + hw->channels_min = min(hw->channels_min, formation.pcm); + hw->channels_max = max(hw->channels_max, formation.pcm); + + hw->rate_min = min(hw->rate_min, formation.rate); + hw->rate_max = max(hw->rate_max, formation.rate); + hw->rates |= snd_pcm_rate_to_rate_bit(formation.rate); + } } -int lacie_speakers_constraints(struct snd_pcm_runtime *runtime) +static void limit_period_and_buffer(struct snd_pcm_hardware *hw) { - runtime->hw.rates = SNDRV_PCM_RATE_32000 | - SNDRV_PCM_RATE_44100 | - SNDRV_PCM_RATE_48000 | - SNDRV_PCM_RATE_88200 | - SNDRV_PCM_RATE_96000; + hw->periods_min = 2; /* SNDRV_PCM_INFO_BATCH */ + hw->periods_max = UINT_MAX; - return 0; + hw->period_bytes_min = 4 * hw->channels_max; /* bytes for a frame */ + + /* Just to prevent from allocating much pages. */ + hw->period_bytes_max = hw->period_bytes_min * 2048; + hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min; } static int pcm_open(struct snd_pcm_substream *substream) { - static const struct snd_pcm_hardware hardware = { - .info = SNDRV_PCM_INFO_MMAP | - SNDRV_PCM_INFO_MMAP_VALID | - SNDRV_PCM_INFO_BATCH | - SNDRV_PCM_INFO_INTERLEAVED | - SNDRV_PCM_INFO_BLOCK_TRANSFER, - .formats = AMDTP_OUT_PCM_FORMAT_BITS, - .channels_min = 2, - .channels_max = 2, - .buffer_bytes_max = 4 * 1024 * 1024, - .period_bytes_min = 1, - .period_bytes_max = UINT_MAX, - .periods_min = 1, - .periods_max = UINT_MAX, - }; struct snd_oxfw *oxfw = substream->private_data; struct snd_pcm_runtime *runtime = substream->runtime; - bool used; + u8 **formats; int err; - err = cmp_connection_check_used(&oxfw->in_conn, &used); - if ((err < 0) || used) - goto end; + formats = oxfw->rx_stream_formats; + + runtime->hw.info = SNDRV_PCM_INFO_BATCH | + SNDRV_PCM_INFO_BLOCK_TRANSFER | + SNDRV_PCM_INFO_INTERLEAVED | + SNDRV_PCM_INFO_MMAP | + SNDRV_PCM_INFO_MMAP_VALID; - runtime->hw = hardware; + limit_channels_and_rates(&runtime->hw, formats); + limit_period_and_buffer(&runtime->hw); - err = oxfw->device_info->pcm_constraints(runtime); + err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, + hw_rule_channels, formats, + SNDRV_PCM_HW_PARAM_RATE, -1); if (err < 0) goto end; - err = snd_pcm_limit_hw_rates(runtime); + + err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, + hw_rule_rate, formats, + SNDRV_PCM_HW_PARAM_CHANNELS, -1); if (err < 0) goto end; err = amdtp_stream_add_pcm_hw_constraints(&oxfw->rx_stream, runtime); + if (err < 0) + goto end; + + snd_pcm_set_sync(substream); end: return err; } diff --git a/sound/firewire/oxfw/oxfw-stream.c b/sound/firewire/oxfw/oxfw-stream.c index ebd156f3e29d..17e3802e6ac2 100644 --- a/sound/firewire/oxfw/oxfw-stream.c +++ b/sound/firewire/oxfw/oxfw-stream.c @@ -8,6 +8,35 @@ #include "oxfw.h" +#define AVC_GENERIC_FRAME_MAXIMUM_BYTES 512 + +/* + * According to datasheet of Oxford Semiconductor: + * OXFW970: 32.0/44.1/48.0/96.0 Khz, 8 audio channels I/O + * OXFW971: 32.0/44.1/48.0/88.2/96.0/192.0 kHz, 16 audio channels I/O, MIDI I/O + */ +static const unsigned int oxfw_rate_table[] = { + [0] = 32000, + [1] = 44100, + [2] = 48000, + [3] = 88200, + [4] = 96000, + [5] = 192000, +}; + +/* + * See Table 5.7 – Sampling frequency for Multi-bit Audio + * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA) + */ +static const unsigned int avc_stream_rate_table[] = { + [0] = 0x02, + [1] = 0x03, + [2] = 0x04, + [3] = 0x0a, + [4] = 0x05, + [5] = 0x07, +}; + int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw) { int err; @@ -78,3 +107,242 @@ void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw) else amdtp_stream_update(&oxfw->rx_stream); } + +/* + * See Table 6.16 - AM824 Stream Format + * Figure 6.19 - format_information field for AM824 Compound + * in AV/C Stream Format Information Specification 1.1 (Apr 2005, 1394TA) + * Also 'Clause 12 AM824 sequence adaption layers' in IEC 61883-6:2005 + */ +int snd_oxfw_stream_parse_format(u8 *format, + struct snd_oxfw_stream_formation *formation) +{ + unsigned int i, e, channels, type; + + memset(formation, 0, sizeof(struct snd_oxfw_stream_formation)); + + /* + * this module can support a hierarchy combination that: + * Root: Audio and Music (0x90) + * Level 1: AM824 Compound (0x40) + */ + if ((format[0] != 0x90) || (format[1] != 0x40)) + return -ENOSYS; + + /* check the sampling rate */ + for (i = 0; i < ARRAY_SIZE(avc_stream_rate_table); i++) { + if (format[2] == avc_stream_rate_table[i]) + break; + } + if (i == ARRAY_SIZE(avc_stream_rate_table)) + return -ENOSYS; + + formation->rate = oxfw_rate_table[i]; + + for (e = 0; e < format[4]; e++) { + channels = format[5 + e * 2]; + type = format[6 + e * 2]; + + switch (type) { + /* IEC 60958 Conformant, currently handled as MBLA */ + case 0x00: + /* Multi Bit Linear Audio (Raw) */ + case 0x06: + formation->pcm += channels; + break; + /* MIDI Conformant */ + case 0x0d: + formation->midi = channels; + break; + /* IEC 61937-3 to 7 */ + case 0x01: + case 0x02: + case 0x03: + case 0x04: + case 0x05: + /* Multi Bit Linear Audio */ + case 0x07: /* DVD-Audio */ + case 0x0c: /* High Precision */ + /* One Bit Audio */ + case 0x08: /* (Plain) Raw */ + case 0x09: /* (Plain) SACD */ + case 0x0a: /* (Encoded) Raw */ + case 0x0b: /* (Encoded) SACD */ + /* SMPTE Time-Code conformant */ + case 0x0e: + /* Sample Count */ + case 0x0f: + /* Anciliary Data */ + case 0x10: + /* Synchronization Stream (Stereo Raw audio) */ + case 0x40: + /* Don't care */ + case 0xff: + default: + return -ENOSYS; /* not supported */ + } + } + + if (formation->pcm > AMDTP_MAX_CHANNELS_FOR_PCM || + formation->midi > AMDTP_MAX_CHANNELS_FOR_MIDI) + return -ENOSYS; + + return 0; +} + +static int +assume_stream_formats(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir, + unsigned int pid, u8 *buf, unsigned int *len, + u8 **formats) +{ + struct snd_oxfw_stream_formation formation; + unsigned int i, eid; + int err; + + /* get format at current sampling rate */ + err = avc_stream_get_format_single(oxfw->unit, dir, pid, buf, len); + if (err < 0) { + dev_err(&oxfw->unit->device, + "fail to get current stream format for isoc %s plug %d:%d\n", + (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out", + pid, err); + goto end; + } + + /* parse and set stream format */ + eid = 0; + err = snd_oxfw_stream_parse_format(buf, &formation); + if (err < 0) + goto end; + + formats[eid] = kmalloc(*len, GFP_KERNEL); + if (formats[eid] == NULL) { + err = -ENOMEM; + goto end; + } + memcpy(formats[eid], buf, *len); + + /* apply the format for each available sampling rate */ + for (i = 0; i < ARRAY_SIZE(oxfw_rate_table); i++) { + if (formation.rate == oxfw_rate_table[i]) + continue; + + err = avc_general_inquiry_sig_fmt(oxfw->unit, + oxfw_rate_table[i], + dir, pid); + if (err < 0) + continue; + + eid++; + formats[eid] = kmalloc(*len, GFP_KERNEL); + if (formats[eid] == NULL) { + err = -ENOMEM; + goto end; + } + memcpy(formats[eid], buf, *len); + formats[eid][2] = avc_stream_rate_table[i]; + } + + err = 0; + oxfw->assumed = true; +end: + return err; +} + +static int fill_stream_formats(struct snd_oxfw *oxfw, + enum avc_general_plug_dir dir, + unsigned short pid) +{ + u8 *buf, **formats; + unsigned int len, eid = 0; + struct snd_oxfw_stream_formation dummy; + int err; + + buf = kmalloc(AVC_GENERIC_FRAME_MAXIMUM_BYTES, GFP_KERNEL); + if (buf == NULL) + return -ENOMEM; + + formats = oxfw->rx_stream_formats; + + /* get first entry */ + len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; + err = avc_stream_get_format_list(oxfw->unit, dir, 0, buf, &len, 0); + if (err == -ENOSYS) { + /* LIST subfunction is not implemented */ + len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; + err = assume_stream_formats(oxfw, dir, pid, buf, &len, + formats); + goto end; + } else if (err < 0) { + dev_err(&oxfw->unit->device, + "fail to get stream format %d for isoc %s plug %d:%d\n", + eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : "out", + pid, err); + goto end; + } + + /* LIST subfunction is implemented */ + while (eid < SND_OXFW_STREAM_FORMAT_ENTRIES) { + /* The format is too short. */ + if (len < 3) { + err = -EIO; + break; + } + + /* parse and set stream format */ + err = snd_oxfw_stream_parse_format(buf, &dummy); + if (err < 0) + break; + + formats[eid] = kmalloc(len, GFP_KERNEL); + if (formats[eid] == NULL) { + err = -ENOMEM; + break; + } + memcpy(formats[eid], buf, len); + + /* get next entry */ + len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; + err = avc_stream_get_format_list(oxfw->unit, dir, 0, + buf, &len, ++eid); + /* No entries remained. */ + if (err == -EINVAL) { + err = 0; + break; + } else if (err < 0) { + dev_err(&oxfw->unit->device, + "fail to get stream format %d for isoc %s plug %d:%d\n", + eid, (dir == AVC_GENERAL_PLUG_DIR_IN) ? "in" : + "out", + pid, err); + break; + } + } +end: + kfree(buf); + return err; +} + +int snd_oxfw_stream_discover(struct snd_oxfw *oxfw) +{ + u8 plugs[AVC_PLUG_INFO_BUF_BYTES]; + int err; + + /* the number of plugs for isoc in/out, ext in/out */ + err = avc_general_get_plug_info(oxfw->unit, 0x1f, 0x07, 0x00, plugs); + if (err < 0) { + dev_err(&oxfw->unit->device, + "fail to get info for isoc/external in/out plugs: %d\n", + err); + goto end; + } else if (plugs[0] == 0) { + err = -ENOSYS; + goto end; + } + + /* use iPCR[0] if exists */ + if (plugs[0] > 0) + err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0); +end: + return err; +} diff --git a/sound/firewire/oxfw/oxfw.c b/sound/firewire/oxfw/oxfw.c index dd576bf61c37..a8f9062b2884 100644 --- a/sound/firewire/oxfw/oxfw.c +++ b/sound/firewire/oxfw/oxfw.c @@ -58,6 +58,10 @@ end: static void oxfw_card_free(struct snd_card *card) { struct snd_oxfw *oxfw = card->private_data; + unsigned int i; + + for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) + kfree(oxfw->rx_stream_formats[i]); mutex_destroy(&oxfw->mutex); } @@ -81,6 +85,10 @@ static int oxfw_probe(struct fw_unit *unit, oxfw->unit = unit; oxfw->device_info = (const struct device_info *)id->driver_data; + err = snd_oxfw_stream_discover(oxfw); + if (err < 0) + goto error; + err = name_card(oxfw); if (err < 0) goto error; @@ -136,7 +144,6 @@ static const struct device_info griffin_firewave = { .driver_name = "FireWave", .vendor_name = "Griffin", .model_name = "FireWave", - .pcm_constraints = firewave_constraints, .mixer_channels = 6, .mute_fb_id = 0x01, .volume_fb_id = 0x02, @@ -146,7 +153,6 @@ static const struct device_info lacie_speakers = { .driver_name = "FWSpeakers", .vendor_name = "LaCie", .model_name = "FireWire Speakers", - .pcm_constraints = lacie_speakers_constraints, .mixer_channels = 1, .mute_fb_id = 0x01, .volume_fb_id = 0x01, diff --git a/sound/firewire/oxfw/oxfw.h b/sound/firewire/oxfw/oxfw.h index a7031d414441..9c3d3e352665 100644 --- a/sound/firewire/oxfw/oxfw.h +++ b/sound/firewire/oxfw/oxfw.h @@ -30,19 +30,24 @@ struct device_info { const char *driver_name; const char *vendor_name; const char *model_name; - int (*pcm_constraints)(struct snd_pcm_runtime *runtime); unsigned int mixer_channels; u8 mute_fb_id; u8 volume_fb_id; }; +/* This is an arbitrary number for convinience. */ +#define SND_OXFW_STREAM_FORMAT_ENTRIES 10 struct snd_oxfw { struct snd_card *card; struct fw_unit *unit; const struct device_info *device_info; struct mutex mutex; + + u8 *rx_stream_formats[SND_OXFW_STREAM_FORMAT_ENTRIES]; + bool assumed; struct cmp_connection in_conn; struct amdtp_stream rx_stream; + bool mute; s16 volume[6]; s16 volume_min; @@ -88,8 +93,18 @@ void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw); void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw); void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw); -int firewave_constraints(struct snd_pcm_runtime *runtime); -int lacie_speakers_constraints(struct snd_pcm_runtime *runtime); +struct snd_oxfw_stream_formation { + unsigned int rate; + unsigned int pcm; + unsigned int midi; +}; +int snd_oxfw_stream_parse_format(u8 *format, + struct snd_oxfw_stream_formation *formation); +int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw, + enum avc_general_plug_dir dir, + struct snd_oxfw_stream_formation *formation); +int snd_oxfw_stream_discover(struct snd_oxfw *oxfw); + int snd_oxfw_create_pcm(struct snd_oxfw *oxfw); int snd_oxfw_create_mixer(struct snd_oxfw *oxfw); -- cgit v1.2.3 From 3c96101f190020e91d413c5835f7a722fc007923 Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Tue, 9 Dec 2014 00:10:43 +0900 Subject: ALSA: oxfw: Add proc interface for debugging purpose This commit adds proc interface to get information about stream formation. This commit also adds snd_oxfw_stream_get_current_formation() to get current stream formation. Signed-off-by: Takashi Sakamoto Acked-by: Clemens Ladisch Signed-off-by: Takashi Iwai --- sound/firewire/oxfw/Makefile | 3 +- sound/firewire/oxfw/oxfw-proc.c | 84 +++++++++++++++++++++++++++++++++++++++ sound/firewire/oxfw/oxfw-stream.c | 27 +++++++++++++ sound/firewire/oxfw/oxfw.c | 2 + sound/firewire/oxfw/oxfw.h | 3 ++ 5 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 sound/firewire/oxfw/oxfw-proc.c (limited to 'sound/firewire/oxfw/oxfw-stream.c') diff --git a/sound/firewire/oxfw/Makefile b/sound/firewire/oxfw/Makefile index b107134d11d5..e9297c638aa1 100644 --- a/sound/firewire/oxfw/Makefile +++ b/sound/firewire/oxfw/Makefile @@ -1,2 +1,3 @@ -snd-oxfw-objs := oxfw-command.o oxfw-stream.o oxfw-control.o oxfw-pcm.o oxfw.o +snd-oxfw-objs := oxfw-command.o oxfw-stream.o oxfw-control.o oxfw-pcm.o \ + oxfw-proc.o oxfw.o obj-m += snd-oxfw.o diff --git a/sound/firewire/oxfw/oxfw-proc.c b/sound/firewire/oxfw/oxfw-proc.c new file mode 100644 index 000000000000..18e030572708 --- /dev/null +++ b/sound/firewire/oxfw/oxfw-proc.c @@ -0,0 +1,84 @@ +/* + * oxfw_proc.c - a part of driver for OXFW970/971 based devices + * + * Copyright (c) 2014 Takashi Sakamoto + * + * Licensed under the terms of the GNU General Public License, version 2. + */ + +#include "./oxfw.h" + +static void proc_read_formation(struct snd_info_entry *entry, + struct snd_info_buffer *buffer) +{ + struct snd_oxfw *oxfw = entry->private_data; + struct snd_oxfw_stream_formation formation, curr; + u8 *format; + char flag; + unsigned int i, err; + + /* Show input. */ + err = snd_oxfw_stream_get_current_formation(oxfw, + AVC_GENERAL_PLUG_DIR_IN, + &curr); + if (err < 0) + return; + + snd_iprintf(buffer, "Input Stream to device:\n"); + snd_iprintf(buffer, "\tRate\tPCM\tMIDI\n"); + for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { + format = oxfw->rx_stream_formats[i]; + if (format == NULL) + continue; + + err = snd_oxfw_stream_parse_format(format, &formation); + if (err < 0) + continue; + + if (memcmp(&formation, &curr, sizeof(curr)) == 0) + flag = '*'; + else + flag = ' '; + + snd_iprintf(buffer, "%c\t%d\t%d\t%d\n", flag, + formation.rate, formation.pcm, formation.midi); + } + +} + +static void add_node(struct snd_oxfw *oxfw, struct snd_info_entry *root, + const char *name, + void (*op)(struct snd_info_entry *e, + struct snd_info_buffer *b)) +{ + struct snd_info_entry *entry; + + entry = snd_info_create_card_entry(oxfw->card, name, root); + if (entry == NULL) + return; + + snd_info_set_text_ops(entry, oxfw, op); + if (snd_info_register(entry) < 0) + snd_info_free_entry(entry); +} + +void snd_oxfw_proc_init(struct snd_oxfw *oxfw) +{ + struct snd_info_entry *root; + + /* + * All nodes are automatically removed at snd_card_disconnect(), + * by following to link list. + */ + root = snd_info_create_card_entry(oxfw->card, "firewire", + oxfw->card->proc_root); + if (root == NULL) + return; + root->mode = S_IFDIR | S_IRUGO | S_IXUGO; + if (snd_info_register(root) < 0) { + snd_info_free_entry(root); + return; + } + + add_node(oxfw, root, "formation", proc_read_formation); +} diff --git a/sound/firewire/oxfw/oxfw-stream.c b/sound/firewire/oxfw/oxfw-stream.c index 17e3802e6ac2..210bf5a2f56e 100644 --- a/sound/firewire/oxfw/oxfw-stream.c +++ b/sound/firewire/oxfw/oxfw-stream.c @@ -108,6 +108,33 @@ void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw) amdtp_stream_update(&oxfw->rx_stream); } +int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw, + enum avc_general_plug_dir dir, + struct snd_oxfw_stream_formation *formation) +{ + u8 *format; + unsigned int len; + int err; + + len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; + format = kmalloc(len, GFP_KERNEL); + if (format == NULL) + return -ENOMEM; + + err = avc_stream_get_format_single(oxfw->unit, dir, 0, format, &len); + if (err < 0) + goto end; + if (len < 3) { + err = -EIO; + goto end; + } + + err = snd_oxfw_stream_parse_format(format, formation); +end: + kfree(format); + return err; +} + /* * See Table 6.16 - AM824 Stream Format * Figure 6.19 - format_information field for AM824 Compound diff --git a/sound/firewire/oxfw/oxfw.c b/sound/firewire/oxfw/oxfw.c index a8f9062b2884..a70149ab511b 100644 --- a/sound/firewire/oxfw/oxfw.c +++ b/sound/firewire/oxfw/oxfw.c @@ -101,6 +101,8 @@ static int oxfw_probe(struct fw_unit *unit, if (err < 0) goto error; + snd_oxfw_proc_init(oxfw); + err = snd_oxfw_stream_init_simplex(oxfw); if (err < 0) goto error; diff --git a/sound/firewire/oxfw/oxfw.h b/sound/firewire/oxfw/oxfw.h index 9c3d3e352665..8c832ea6aae0 100644 --- a/sound/firewire/oxfw/oxfw.h +++ b/sound/firewire/oxfw/oxfw.h @@ -18,6 +18,7 @@ #include #include #include +#include #include "../lib.h" #include "../fcp.h" @@ -108,3 +109,5 @@ int snd_oxfw_stream_discover(struct snd_oxfw *oxfw); int snd_oxfw_create_pcm(struct snd_oxfw *oxfw); int snd_oxfw_create_mixer(struct snd_oxfw *oxfw); + +void snd_oxfw_proc_init(struct snd_oxfw *oxfw); -- cgit v1.2.3 From f3699e2c77455a6cccc977b391c553f2c816f639 Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Tue, 9 Dec 2014 00:10:44 +0900 Subject: ALSA: oxfw: Change the way to start stream In past commit, this driver can keep stream formations for each sampling rate. So its stream functionality can decide stream formations with given some parameters. This commit moves related codes from PCM functionality to stream functionality. Furthermore, to set stream format correctly, this commit uses AV/C Stream Format Information command instead of AV/C Input/Output Plug Signal Format command. Signed-off-by: Takashi Sakamoto Acked-by: Clemens Ladisch Signed-off-by: Takashi Iwai --- sound/firewire/oxfw/oxfw-pcm.c | 44 ++---------- sound/firewire/oxfw/oxfw-stream.c | 139 +++++++++++++++++++++++++++++++++++--- sound/firewire/oxfw/oxfw.h | 3 +- 3 files changed, 138 insertions(+), 48 deletions(-) (limited to 'sound/firewire/oxfw/oxfw-stream.c') diff --git a/sound/firewire/oxfw/oxfw-pcm.c b/sound/firewire/oxfw/oxfw-pcm.c index 0c0be984edce..ea2b439253cf 100644 --- a/sound/firewire/oxfw/oxfw-pcm.c +++ b/sound/firewire/oxfw/oxfw-pcm.c @@ -166,39 +166,10 @@ static int pcm_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params) { struct snd_oxfw *oxfw = substream->private_data; - int err; - - mutex_lock(&oxfw->mutex); - - snd_oxfw_stream_stop_simplex(oxfw); - - err = snd_pcm_lib_alloc_vmalloc_buffer(substream, - params_buffer_bytes(hw_params)); - if (err < 0) - goto error; - - amdtp_stream_set_parameters(&oxfw->rx_stream, - params_rate(hw_params), - params_channels(hw_params), - 0); - - amdtp_stream_set_pcm_format(&oxfw->rx_stream, - params_format(hw_params)); - - err = avc_general_set_sig_fmt(oxfw->unit, params_rate(hw_params), - AVC_GENERAL_PLUG_DIR_IN, 0); - if (err < 0) { - dev_err(&oxfw->unit->device, "failed to set sample rate\n"); - goto err_buffer; - } - return 0; - -err_buffer: - snd_pcm_lib_free_vmalloc_buffer(substream); -error: - mutex_unlock(&oxfw->mutex); - return err; + amdtp_stream_set_pcm_format(&oxfw->rx_stream, params_format(hw_params)); + return snd_pcm_lib_alloc_vmalloc_buffer(substream, + params_buffer_bytes(hw_params)); } static int pcm_hw_free(struct snd_pcm_substream *substream) @@ -215,19 +186,18 @@ static int pcm_hw_free(struct snd_pcm_substream *substream) static int pcm_prepare(struct snd_pcm_substream *substream) { struct snd_oxfw *oxfw = substream->private_data; + struct snd_pcm_runtime *runtime = substream->runtime; int err; mutex_lock(&oxfw->mutex); - - snd_oxfw_stream_stop_simplex(oxfw); - - err = snd_oxfw_stream_start_simplex(oxfw); + err = snd_oxfw_stream_start_simplex(oxfw, runtime->rate, + runtime->channels); + mutex_unlock(&oxfw->mutex); if (err < 0) goto end; amdtp_stream_pcm_prepare(&oxfw->rx_stream); end: - mutex_unlock(&oxfw->mutex); return err; } diff --git a/sound/firewire/oxfw/oxfw-stream.c b/sound/firewire/oxfw/oxfw-stream.c index 210bf5a2f56e..1820497f4bbf 100644 --- a/sound/firewire/oxfw/oxfw-stream.c +++ b/sound/firewire/oxfw/oxfw-stream.c @@ -7,8 +7,10 @@ */ #include "oxfw.h" +#include #define AVC_GENERIC_FRAME_MAXIMUM_BYTES 512 +#define CALLBACK_TIMEOUT 200 /* * According to datasheet of Oxford Semiconductor: @@ -37,6 +39,47 @@ static const unsigned int avc_stream_rate_table[] = { [5] = 0x07, }; +static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s, + unsigned int rate, unsigned int pcm_channels) +{ + u8 **formats; + struct snd_oxfw_stream_formation formation; + enum avc_general_plug_dir dir; + unsigned int i, err, len; + + formats = oxfw->rx_stream_formats; + dir = AVC_GENERAL_PLUG_DIR_IN; + + /* Seek stream format for requirements. */ + for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { + err = snd_oxfw_stream_parse_format(formats[i], &formation); + if (err < 0) + return err; + + if ((formation.rate == rate) && (formation.pcm == pcm_channels)) + break; + } + if (i == SND_OXFW_STREAM_FORMAT_ENTRIES) + return -EINVAL; + + /* If assumed, just change rate. */ + if (oxfw->assumed) + return avc_general_set_sig_fmt(oxfw->unit, rate, + AVC_GENERAL_PLUG_DIR_IN, 0); + + /* Calculate format length. */ + len = 5 + formats[i][4] * 2; + + err = avc_stream_set_format(oxfw->unit, dir, 0, formats[i], len); + if (err < 0) + return err; + + /* Some requests just after changing format causes freezing. */ + msleep(100); + + return 0; +} + int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw) { int err; @@ -63,30 +106,106 @@ static void stop_stream(struct snd_oxfw *oxfw) cmp_connection_break(&oxfw->in_conn); } -int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw) +static int start_stream(struct snd_oxfw *oxfw, unsigned int rate, + unsigned int pcm_channels) { - int err = 0; + u8 **formats; + struct cmp_connection *conn; + struct snd_oxfw_stream_formation formation; + unsigned int i, midi_ports; + struct amdtp_stream *stream; + int err; - if (amdtp_streaming_error(&oxfw->rx_stream)) - stop_stream(oxfw); + stream = &oxfw->rx_stream; + formats = oxfw->rx_stream_formats; + conn = &oxfw->in_conn; - if (amdtp_stream_running(&oxfw->rx_stream)) + /* Get stream formation */ + for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { + if (formats[i] == NULL) + break; + + err = snd_oxfw_stream_parse_format(formats[i], &formation); + if (err < 0) + goto end; + if (rate != formation.rate) + continue; + if (pcm_channels == 0 || pcm_channels == formation.pcm) + break; + } + if (i == SND_OXFW_STREAM_FORMAT_ENTRIES) { + err = -EINVAL; goto end; + } - err = cmp_connection_establish(&oxfw->in_conn, - amdtp_stream_get_max_payload(&oxfw->rx_stream)); + pcm_channels = formation.pcm; + midi_ports = DIV_ROUND_UP(formation.midi, 8); + + /* The stream should have one pcm channels at least */ + if (pcm_channels == 0) { + err = -EINVAL; + goto end; + } + amdtp_stream_set_parameters(stream, rate, pcm_channels, midi_ports); + + err = cmp_connection_establish(conn, + amdtp_stream_get_max_payload(stream)); if (err < 0) goto end; - err = amdtp_stream_start(&oxfw->rx_stream, - oxfw->in_conn.resources.channel, - oxfw->in_conn.speed); + err = amdtp_stream_start(stream, + conn->resources.channel, + conn->speed); + if (err < 0) { + cmp_connection_break(conn); + goto end; + } + + /* Wait first packet */ + err = amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT); if (err < 0) stop_stream(oxfw); end: return err; } +int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw, unsigned int rate, + unsigned int pcm_channels) +{ + struct snd_oxfw_stream_formation formation; + int err = 0; + + /* packet queueing error */ + if (amdtp_streaming_error(&oxfw->rx_stream)) + stop_stream(oxfw); + + err = snd_oxfw_stream_get_current_formation(oxfw, + AVC_GENERAL_PLUG_DIR_IN, + &formation); + if (err < 0) + goto end; + + if ((formation.rate != rate) || (formation.pcm != pcm_channels)) { + stop_stream(oxfw); + + /* arrange sampling rate */ + err = set_stream_format(oxfw, &oxfw->rx_stream, rate, + pcm_channels); + if (err < 0) { + dev_err(&oxfw->unit->device, + "fail to set stream format: %d\n", err); + goto end; + } + } + + err = start_stream(oxfw, rate, pcm_channels); + if (err < 0) + dev_err(&oxfw->unit->device, + "fail to start stream: %d\n", err); +end: + return err; +} + void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw) { stop_stream(oxfw); diff --git a/sound/firewire/oxfw/oxfw.h b/sound/firewire/oxfw/oxfw.h index 8c832ea6aae0..c09ef38c22ba 100644 --- a/sound/firewire/oxfw/oxfw.h +++ b/sound/firewire/oxfw/oxfw.h @@ -89,7 +89,8 @@ int avc_general_inquiry_sig_fmt(struct fw_unit *unit, unsigned int rate, unsigned short pid); int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw); -int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw); +int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw, unsigned int rate, + unsigned int pcm_channels); void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw); void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw); void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw); -- cgit v1.2.3 From b0ac00095fe1485f60bb8ea7326426d3d02a1aec Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Tue, 9 Dec 2014 00:10:46 +0900 Subject: ALSA: oxfw: Add support AMDTP in-stream Previous commit adds support for some devices which can capture PCM samples. These devices transmit AMDTP stream in non-blocking mode. This commit adds functionality to handle AMDTP incoming stream. OXFW seems to have two quirks: - Transmits packets with non-zero dbc in its beginning - Transmits packets with wrong values in syt field For the first quirk, this commit adds CIP_SKIP_INIT_DBC_CHECK flag for incoming stream to skip first check of dbc. For the second quirk, this commit doesn't add duplex stream which Fireworks/BeBoB drivers use. So OXFW driver generates syt value for outgoing stream. Here are examples of a sequence of packets transmitted by Behringer F-Control Audio 202. There are differences between sequences of syt value when OXFW driver transfers outgoing stream or not. When driver gives no outgoing stream: Index Payload CIP_Header_0 CIP_Header_1 38 14 00020092 900103D1 39 12 00020098 900102FF 40 12 0002009D 9001027F 41 14 000200A2 90010396 42 14 000200A8 900102E8 43 12 000200AE 90010219 44 14 000200B3 90010331 45 12 000200B9 9001025F 46 14 000200BE 90010376 47 12 000200C4 900102A1 00 12 000200C9 9001023E 01 14 000200CE 90010358 02 12 000200D4 90010289 03 16 000200D9 900103A3 04 12 000200E0 900102DD 05 14 000200E5 900103F1 06 12 000200EB 90010335 07 12 000200F0 90010263 08 14 000200F5 9001037C 09 12 000200FB 900102AE When driver gives outgoing stream: Index Payload CIP_Header_0 CIP_Header_1 38 12 000200BD 900104A8 39 14 000200C2 900104A8 40 12 000200C8 900104AC 41 14 000200CD 900104A9 42 12 000200D3 900104B1 43 14 000200D8 900104A8 44 12 000200DE 900104AA 45 14 000200E3 900104A9 46 14 000200E9 900104AE 47 12 000200EF 900104A8 00 14 000200F4 900104AD 01 12 000200FA 900104A7 02 14 000200FF 900104A9 03 12 00020005 900104A9 04 14 0002000A 900104B1 05 12 00020010 900104AA 06 14 00020015 900104AD 07 12 0002001B 900104A7 08 14 00020020 900104AC 09 12 00020026 900104A7 Signed-off-by: Takashi Sakamoto Acked-by: Clemens Ladisch Signed-off-by: Takashi Iwai --- sound/firewire/oxfw/oxfw-pcm.c | 6 +- sound/firewire/oxfw/oxfw-proc.c | 29 ++++ sound/firewire/oxfw/oxfw-stream.c | 270 +++++++++++++++++++++++++++++--------- sound/firewire/oxfw/oxfw.c | 25 +++- sound/firewire/oxfw/oxfw.h | 24 +++- 5 files changed, 279 insertions(+), 75 deletions(-) (limited to 'sound/firewire/oxfw/oxfw-stream.c') diff --git a/sound/firewire/oxfw/oxfw-pcm.c b/sound/firewire/oxfw/oxfw-pcm.c index ea2b439253cf..a78339c81de8 100644 --- a/sound/firewire/oxfw/oxfw-pcm.c +++ b/sound/firewire/oxfw/oxfw-pcm.c @@ -177,7 +177,7 @@ static int pcm_hw_free(struct snd_pcm_substream *substream) struct snd_oxfw *oxfw = substream->private_data; mutex_lock(&oxfw->mutex); - snd_oxfw_stream_stop_simplex(oxfw); + snd_oxfw_stream_stop_simplex(oxfw, &oxfw->rx_stream); mutex_unlock(&oxfw->mutex); return snd_pcm_lib_free_vmalloc_buffer(substream); @@ -190,8 +190,8 @@ static int pcm_prepare(struct snd_pcm_substream *substream) int err; mutex_lock(&oxfw->mutex); - err = snd_oxfw_stream_start_simplex(oxfw, runtime->rate, - runtime->channels); + err = snd_oxfw_stream_start_simplex(oxfw, &oxfw->rx_stream, + runtime->rate, runtime->channels); mutex_unlock(&oxfw->mutex); if (err < 0) goto end; diff --git a/sound/firewire/oxfw/oxfw-proc.c b/sound/firewire/oxfw/oxfw-proc.c index 18e030572708..604808e5526d 100644 --- a/sound/firewire/oxfw/oxfw-proc.c +++ b/sound/firewire/oxfw/oxfw-proc.c @@ -44,6 +44,35 @@ static void proc_read_formation(struct snd_info_entry *entry, formation.rate, formation.pcm, formation.midi); } + if (!oxfw->has_output) + return; + + /* Show output. */ + err = snd_oxfw_stream_get_current_formation(oxfw, + AVC_GENERAL_PLUG_DIR_OUT, + &curr); + if (err < 0) + return; + + snd_iprintf(buffer, "Output Stream from device:\n"); + snd_iprintf(buffer, "\tRate\tPCM\tMIDI\n"); + for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { + format = oxfw->tx_stream_formats[i]; + if (format == NULL) + continue; + + err = snd_oxfw_stream_parse_format(format, &formation); + if (err < 0) + continue; + + if (memcmp(&formation, &curr, sizeof(curr)) == 0) + flag = '*'; + else + flag = ' '; + + snd_iprintf(buffer, "%c\t%d\t%d\t%d\n", flag, + formation.rate, formation.pcm, formation.midi); + } } static void add_node(struct snd_oxfw *oxfw, struct snd_info_entry *root, diff --git a/sound/firewire/oxfw/oxfw-stream.c b/sound/firewire/oxfw/oxfw-stream.c index 1820497f4bbf..1d154284873e 100644 --- a/sound/firewire/oxfw/oxfw-stream.c +++ b/sound/firewire/oxfw/oxfw-stream.c @@ -39,6 +39,22 @@ static const unsigned int avc_stream_rate_table[] = { [5] = 0x07, }; +static int set_rate(struct snd_oxfw *oxfw, unsigned int rate) +{ + int err; + + err = avc_general_set_sig_fmt(oxfw->unit, rate, + AVC_GENERAL_PLUG_DIR_IN, 0); + if (err < 0) + goto end; + + if (oxfw->has_output) + err = avc_general_set_sig_fmt(oxfw->unit, rate, + AVC_GENERAL_PLUG_DIR_OUT, 0); +end: + return err; +} + static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s, unsigned int rate, unsigned int pcm_channels) { @@ -47,8 +63,13 @@ static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s, enum avc_general_plug_dir dir; unsigned int i, err, len; - formats = oxfw->rx_stream_formats; - dir = AVC_GENERAL_PLUG_DIR_IN; + if (s == &oxfw->tx_stream) { + formats = oxfw->tx_stream_formats; + dir = AVC_GENERAL_PLUG_DIR_OUT; + } else { + formats = oxfw->rx_stream_formats; + dir = AVC_GENERAL_PLUG_DIR_IN; + } /* Seek stream format for requirements. */ for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { @@ -64,8 +85,7 @@ static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s, /* If assumed, just change rate. */ if (oxfw->assumed) - return avc_general_set_sig_fmt(oxfw->unit, rate, - AVC_GENERAL_PLUG_DIR_IN, 0); + return set_rate(oxfw, rate); /* Calculate format length. */ len = 5 + formats[i][4] * 2; @@ -80,47 +100,35 @@ static int set_stream_format(struct snd_oxfw *oxfw, struct amdtp_stream *s, return 0; } -int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw) +static void stop_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream) { - int err; - - err = cmp_connection_init(&oxfw->in_conn, oxfw->unit, - CMP_INPUT, 0); - if (err < 0) - goto end; + amdtp_stream_pcm_abort(stream); + amdtp_stream_stop(stream); - err = amdtp_stream_init(&oxfw->rx_stream, oxfw->unit, - AMDTP_OUT_STREAM, CIP_NONBLOCKING); - if (err < 0) { - amdtp_stream_destroy(&oxfw->rx_stream); - cmp_connection_destroy(&oxfw->in_conn); - } -end: - return err; -} - -static void stop_stream(struct snd_oxfw *oxfw) -{ - amdtp_stream_pcm_abort(&oxfw->rx_stream); - amdtp_stream_stop(&oxfw->rx_stream); - cmp_connection_break(&oxfw->in_conn); + if (stream == &oxfw->tx_stream) + cmp_connection_break(&oxfw->out_conn); + else + cmp_connection_break(&oxfw->in_conn); } -static int start_stream(struct snd_oxfw *oxfw, unsigned int rate, - unsigned int pcm_channels) +static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream, + unsigned int rate, unsigned int pcm_channels) { u8 **formats; struct cmp_connection *conn; struct snd_oxfw_stream_formation formation; unsigned int i, midi_ports; - struct amdtp_stream *stream; int err; - stream = &oxfw->rx_stream; - formats = oxfw->rx_stream_formats; - conn = &oxfw->in_conn; + if (stream == &oxfw->rx_stream) { + formats = oxfw->rx_stream_formats; + conn = &oxfw->in_conn; + } else { + formats = oxfw->tx_stream_formats; + conn = &oxfw->out_conn; + } - /* Get stream formation */ + /* Get stream format */ for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { if (formats[i] == NULL) break; @@ -164,67 +172,196 @@ static int start_stream(struct snd_oxfw *oxfw, unsigned int rate, /* Wait first packet */ err = amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT); if (err < 0) - stop_stream(oxfw); + stop_stream(oxfw, stream); end: return err; } -int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw, unsigned int rate, - unsigned int pcm_channels) +static int check_connection_used_by_others(struct snd_oxfw *oxfw, + struct amdtp_stream *stream) { + struct cmp_connection *conn; + bool used; + int err; + + if (stream == &oxfw->tx_stream) + conn = &oxfw->out_conn; + else + conn = &oxfw->in_conn; + + err = cmp_connection_check_used(conn, &used); + if ((err >= 0) && used && !amdtp_stream_running(stream)) { + dev_err(&oxfw->unit->device, + "Connection established by others: %cPCR[%d]\n", + (conn->direction == CMP_OUTPUT) ? 'o' : 'i', + conn->pcr_index); + err = -EBUSY; + } + + return err; +} + +int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw, + struct amdtp_stream *stream) +{ + struct cmp_connection *conn; + enum cmp_direction c_dir; + enum amdtp_stream_direction s_dir; + int err; + + if (stream == &oxfw->tx_stream) { + conn = &oxfw->out_conn; + c_dir = CMP_OUTPUT; + s_dir = AMDTP_IN_STREAM; + } else { + conn = &oxfw->in_conn; + c_dir = CMP_INPUT; + s_dir = AMDTP_OUT_STREAM; + } + + err = cmp_connection_init(conn, oxfw->unit, c_dir, 0); + if (err < 0) + goto end; + + err = amdtp_stream_init(stream, oxfw->unit, s_dir, CIP_NONBLOCKING); + if (err < 0) { + amdtp_stream_destroy(stream); + cmp_connection_destroy(conn); + goto end; + } + + /* OXFW starts to transmit packets with non-zero dbc. */ + if (stream == &oxfw->tx_stream) + oxfw->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK; +end: + return err; +} + +int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw, + struct amdtp_stream *stream, + unsigned int rate, unsigned int pcm_channels) +{ + struct amdtp_stream *opposite; struct snd_oxfw_stream_formation formation; + enum avc_general_plug_dir dir; + unsigned int substreams, opposite_substreams; int err = 0; + if (stream == &oxfw->tx_stream) { + substreams = oxfw->capture_substreams; + opposite = &oxfw->rx_stream; + opposite_substreams = oxfw->playback_substreams; + dir = AVC_GENERAL_PLUG_DIR_OUT; + } else { + substreams = oxfw->playback_substreams; + opposite_substreams = oxfw->capture_substreams; + + if (oxfw->has_output) + opposite = &oxfw->rx_stream; + else + opposite = NULL; + + dir = AVC_GENERAL_PLUG_DIR_IN; + } + + if (substreams == 0) + goto end; + + /* + * Considering JACK/FFADO streaming: + * TODO: This can be removed hwdep functionality becomes popular. + */ + err = check_connection_used_by_others(oxfw, stream); + if (err < 0) + goto end; + /* packet queueing error */ - if (amdtp_streaming_error(&oxfw->rx_stream)) - stop_stream(oxfw); + if (amdtp_streaming_error(stream)) + stop_stream(oxfw, stream); - err = snd_oxfw_stream_get_current_formation(oxfw, - AVC_GENERAL_PLUG_DIR_IN, - &formation); + err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation); if (err < 0) goto end; if ((formation.rate != rate) || (formation.pcm != pcm_channels)) { - stop_stream(oxfw); + if (opposite != NULL) { + err = check_connection_used_by_others(oxfw, opposite); + if (err < 0) + goto end; + stop_stream(oxfw, opposite); + } + stop_stream(oxfw, stream); - /* arrange sampling rate */ - err = set_stream_format(oxfw, &oxfw->rx_stream, rate, - pcm_channels); + err = set_stream_format(oxfw, stream, rate, pcm_channels); if (err < 0) { dev_err(&oxfw->unit->device, "fail to set stream format: %d\n", err); goto end; } + + /* Start opposite stream if needed. */ + if (opposite && !amdtp_stream_running(opposite) && + (opposite_substreams > 0)) { + err = start_stream(oxfw, opposite, rate, 0); + if (err < 0) { + dev_err(&oxfw->unit->device, + "fail to restart stream: %d\n", err); + goto end; + } + } } - err = start_stream(oxfw, rate, pcm_channels); - if (err < 0) - dev_err(&oxfw->unit->device, - "fail to start stream: %d\n", err); + /* Start requested stream. */ + if (!amdtp_stream_running(stream)) { + err = start_stream(oxfw, stream, rate, pcm_channels); + if (err < 0) + dev_err(&oxfw->unit->device, + "fail to start stream: %d\n", err); + } end: return err; } -void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw) +void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw, + struct amdtp_stream *stream) { - stop_stream(oxfw); + if (((stream == &oxfw->tx_stream) && (oxfw->capture_substreams > 0)) || + ((stream == &oxfw->rx_stream) && (oxfw->playback_substreams > 0))) + return; + + stop_stream(oxfw, stream); } -void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw) +void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw, + struct amdtp_stream *stream) { - stop_stream(oxfw); + struct cmp_connection *conn; + + if (stream == &oxfw->tx_stream) + conn = &oxfw->out_conn; + else + conn = &oxfw->in_conn; - amdtp_stream_destroy(&oxfw->rx_stream); - cmp_connection_destroy(&oxfw->in_conn); + stop_stream(oxfw, stream); + + amdtp_stream_destroy(stream); + cmp_connection_destroy(conn); } -void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw) +void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw, + struct amdtp_stream *stream) { - if (cmp_connection_update(&oxfw->in_conn) < 0) - stop_stream(oxfw); + struct cmp_connection *conn; + + if (stream == &oxfw->tx_stream) + conn = &oxfw->out_conn; + else + conn = &oxfw->in_conn; + + if (cmp_connection_update(conn) < 0) + stop_stream(oxfw, stream); else - amdtp_stream_update(&oxfw->rx_stream); + amdtp_stream_update(stream); } int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw, @@ -408,7 +545,10 @@ static int fill_stream_formats(struct snd_oxfw *oxfw, if (buf == NULL) return -ENOMEM; - formats = oxfw->rx_stream_formats; + if (dir == AVC_GENERAL_PLUG_DIR_OUT) + formats = oxfw->tx_stream_formats; + else + formats = oxfw->rx_stream_formats; /* get first entry */ len = AVC_GENERIC_FRAME_MAXIMUM_BYTES; @@ -481,11 +621,19 @@ int snd_oxfw_stream_discover(struct snd_oxfw *oxfw) "fail to get info for isoc/external in/out plugs: %d\n", err); goto end; - } else if (plugs[0] == 0) { + } else if ((plugs[0] == 0) && (plugs[1] == 0)) { err = -ENOSYS; goto end; } + /* use oPCR[0] if exists */ + if (plugs[1] > 0) { + err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_OUT, 0); + if (err < 0) + goto end; + oxfw->has_output = true; + } + /* use iPCR[0] if exists */ if (plugs[0] > 0) err = fill_stream_formats(oxfw, AVC_GENERAL_PLUG_DIR_IN, 0); diff --git a/sound/firewire/oxfw/oxfw.c b/sound/firewire/oxfw/oxfw.c index 797af33c7bcb..23c00a2bb7d3 100644 --- a/sound/firewire/oxfw/oxfw.c +++ b/sound/firewire/oxfw/oxfw.c @@ -109,8 +109,10 @@ static void oxfw_card_free(struct snd_card *card) struct snd_oxfw *oxfw = card->private_data; unsigned int i; - for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) + for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { + kfree(oxfw->tx_stream_formats[i]); kfree(oxfw->rx_stream_formats[i]); + } mutex_destroy(&oxfw->mutex); } @@ -157,13 +159,20 @@ static int oxfw_probe(struct fw_unit *unit, snd_oxfw_proc_init(oxfw); - err = snd_oxfw_stream_init_simplex(oxfw); + err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->rx_stream); if (err < 0) goto error; + if (oxfw->has_output) { + err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->tx_stream); + if (err < 0) + goto error; + } err = snd_card_register(card); if (err < 0) { - snd_oxfw_stream_destroy_simplex(oxfw); + snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->rx_stream); + if (oxfw->has_output) + snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->tx_stream); goto error; } dev_set_drvdata(&unit->device, oxfw); @@ -181,7 +190,11 @@ static void oxfw_bus_reset(struct fw_unit *unit) fcp_bus_reset(oxfw->unit); mutex_lock(&oxfw->mutex); - snd_oxfw_stream_update_simplex(oxfw); + + snd_oxfw_stream_update_simplex(oxfw, &oxfw->rx_stream); + if (oxfw->has_output) + snd_oxfw_stream_update_simplex(oxfw, &oxfw->tx_stream); + mutex_unlock(&oxfw->mutex); } @@ -191,7 +204,9 @@ static void oxfw_remove(struct fw_unit *unit) snd_card_disconnect(oxfw->card); - snd_oxfw_stream_destroy_simplex(oxfw); + snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->rx_stream); + if (oxfw->has_output) + snd_oxfw_stream_destroy_simplex(oxfw, &oxfw->tx_stream); snd_card_free_when_closed(oxfw->card); } diff --git a/sound/firewire/oxfw/oxfw.h b/sound/firewire/oxfw/oxfw.h index c09ef38c22ba..2211d11a79e1 100644 --- a/sound/firewire/oxfw/oxfw.h +++ b/sound/firewire/oxfw/oxfw.h @@ -44,10 +44,16 @@ struct snd_oxfw { const struct device_info *device_info; struct mutex mutex; + bool has_output; + u8 *tx_stream_formats[SND_OXFW_STREAM_FORMAT_ENTRIES]; u8 *rx_stream_formats[SND_OXFW_STREAM_FORMAT_ENTRIES]; bool assumed; + struct cmp_connection out_conn; struct cmp_connection in_conn; + struct amdtp_stream tx_stream; struct amdtp_stream rx_stream; + unsigned int capture_substreams; + unsigned int playback_substreams; bool mute; s16 volume[6]; @@ -88,12 +94,17 @@ int avc_general_inquiry_sig_fmt(struct fw_unit *unit, unsigned int rate, enum avc_general_plug_dir dir, unsigned short pid); -int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw); -int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw, unsigned int rate, - unsigned int pcm_channels); -void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw); -void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw); -void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw); +int snd_oxfw_stream_init_simplex(struct snd_oxfw *oxfw, + struct amdtp_stream *stream); +int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw, + struct amdtp_stream *stream, + unsigned int rate, unsigned int pcm_channels); +void snd_oxfw_stream_stop_simplex(struct snd_oxfw *oxfw, + struct amdtp_stream *stream); +void snd_oxfw_stream_destroy_simplex(struct snd_oxfw *oxfw, + struct amdtp_stream *stream); +void snd_oxfw_stream_update_simplex(struct snd_oxfw *oxfw, + struct amdtp_stream *stream); struct snd_oxfw_stream_formation { unsigned int rate; @@ -105,6 +116,7 @@ int snd_oxfw_stream_parse_format(u8 *format, int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw, enum avc_general_plug_dir dir, struct snd_oxfw_stream_formation *formation); + int snd_oxfw_stream_discover(struct snd_oxfw *oxfw); int snd_oxfw_create_pcm(struct snd_oxfw *oxfw); -- cgit v1.2.3 From 05588d340a128ff5c7b768c517150e31842a78aa Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Tue, 9 Dec 2014 00:10:48 +0900 Subject: ALSA: oxfw: Add support for capture/playback MIDI messages This commit adds MIDI functionality with an assumption of 'if the device has MIDI comformant data channels in its stream formation, the device has one MIDI port'. When no streams have already started, MIDI functionality starts stream with current sampling rate. When MIDI functionality has already starts some streams and PCM functionality is going to start streams at different sampling rate, this driver stops streams once and changes sampling rate, then restarts streams for both PCM/MIDI substreams. Signed-off-by: Takashi Sakamoto Acked-by: Clemens Ladisch Signed-off-by: Takashi Iwai --- sound/firewire/oxfw/Makefile | 2 +- sound/firewire/oxfw/oxfw-midi.c | 191 ++++++++++++++++++++++++++++++++++++++ sound/firewire/oxfw/oxfw-stream.c | 4 + sound/firewire/oxfw/oxfw.c | 5 + sound/firewire/oxfw/oxfw.h | 7 ++ 5 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 sound/firewire/oxfw/oxfw-midi.c (limited to 'sound/firewire/oxfw/oxfw-stream.c') diff --git a/sound/firewire/oxfw/Makefile b/sound/firewire/oxfw/Makefile index e9297c638aa1..3904a302d48e 100644 --- a/sound/firewire/oxfw/Makefile +++ b/sound/firewire/oxfw/Makefile @@ -1,3 +1,3 @@ snd-oxfw-objs := oxfw-command.o oxfw-stream.o oxfw-control.o oxfw-pcm.o \ - oxfw-proc.o oxfw.o + oxfw-proc.o oxfw-midi.o oxfw.o obj-m += snd-oxfw.o diff --git a/sound/firewire/oxfw/oxfw-midi.c b/sound/firewire/oxfw/oxfw-midi.c new file mode 100644 index 000000000000..334b11d1a422 --- /dev/null +++ b/sound/firewire/oxfw/oxfw-midi.c @@ -0,0 +1,191 @@ +/* + * oxfw_midi.c - a part of driver for OXFW970/971 based devices + * + * Copyright (c) 2014 Takashi Sakamoto + * + * Licensed under the terms of the GNU General Public License, version 2. + */ + +#include "oxfw.h" + +static int midi_capture_open(struct snd_rawmidi_substream *substream) +{ + struct snd_oxfw *oxfw = substream->rmidi->private_data; + int err; + + mutex_lock(&oxfw->mutex); + + oxfw->capture_substreams++; + err = snd_oxfw_stream_start_simplex(oxfw, &oxfw->tx_stream, 0, 0); + + mutex_unlock(&oxfw->mutex); + + return err; +} + +static int midi_playback_open(struct snd_rawmidi_substream *substream) +{ + struct snd_oxfw *oxfw = substream->rmidi->private_data; + int err; + + mutex_lock(&oxfw->mutex); + + oxfw->playback_substreams++; + err = snd_oxfw_stream_start_simplex(oxfw, &oxfw->rx_stream, 0, 0); + + mutex_unlock(&oxfw->mutex); + + return err; +} + +static int midi_capture_close(struct snd_rawmidi_substream *substream) +{ + struct snd_oxfw *oxfw = substream->rmidi->private_data; + + mutex_lock(&oxfw->mutex); + + oxfw->capture_substreams--; + snd_oxfw_stream_stop_simplex(oxfw, &oxfw->tx_stream); + + mutex_unlock(&oxfw->mutex); + + return 0; +} + +static int midi_playback_close(struct snd_rawmidi_substream *substream) +{ + struct snd_oxfw *oxfw = substream->rmidi->private_data; + + mutex_lock(&oxfw->mutex); + + oxfw->playback_substreams--; + snd_oxfw_stream_stop_simplex(oxfw, &oxfw->rx_stream); + + mutex_unlock(&oxfw->mutex); + + return 0; +} + +static void midi_capture_trigger(struct snd_rawmidi_substream *substrm, int up) +{ + struct snd_oxfw *oxfw = substrm->rmidi->private_data; + unsigned long flags; + + spin_lock_irqsave(&oxfw->lock, flags); + + if (up) + amdtp_stream_midi_trigger(&oxfw->tx_stream, + substrm->number, substrm); + else + amdtp_stream_midi_trigger(&oxfw->tx_stream, + substrm->number, NULL); + + spin_unlock_irqrestore(&oxfw->lock, flags); +} + +static void midi_playback_trigger(struct snd_rawmidi_substream *substrm, int up) +{ + struct snd_oxfw *oxfw = substrm->rmidi->private_data; + unsigned long flags; + + spin_lock_irqsave(&oxfw->lock, flags); + + if (up) + amdtp_stream_midi_trigger(&oxfw->rx_stream, + substrm->number, substrm); + else + amdtp_stream_midi_trigger(&oxfw->rx_stream, + substrm->number, NULL); + + spin_unlock_irqrestore(&oxfw->lock, flags); +} + +static struct snd_rawmidi_ops midi_capture_ops = { + .open = midi_capture_open, + .close = midi_capture_close, + .trigger = midi_capture_trigger, +}; + +static struct snd_rawmidi_ops midi_playback_ops = { + .open = midi_playback_open, + .close = midi_playback_close, + .trigger = midi_playback_trigger, +}; + +static void set_midi_substream_names(struct snd_oxfw *oxfw, + struct snd_rawmidi_str *str) +{ + struct snd_rawmidi_substream *subs; + + list_for_each_entry(subs, &str->substreams, list) { + snprintf(subs->name, sizeof(subs->name), + "%s MIDI %d", + oxfw->card->shortname, subs->number + 1); + } +} + +int snd_oxfw_create_midi(struct snd_oxfw *oxfw) +{ + struct snd_oxfw_stream_formation formation; + struct snd_rawmidi *rmidi; + struct snd_rawmidi_str *str; + u8 *format; + int i, err; + + /* If its stream has MIDI conformant data channel, add one MIDI port */ + for (i = 0; i < SND_OXFW_STREAM_FORMAT_ENTRIES; i++) { + format = oxfw->tx_stream_formats[i]; + if (format != NULL) { + err = snd_oxfw_stream_parse_format(format, &formation); + if (err >= 0 && formation.midi > 0) + oxfw->midi_input_ports = 1; + } + + format = oxfw->rx_stream_formats[i]; + if (format != NULL) { + err = snd_oxfw_stream_parse_format(format, &formation); + if (err >= 0 && formation.midi > 0) + oxfw->midi_output_ports = 1; + } + } + if ((oxfw->midi_input_ports == 0) && (oxfw->midi_output_ports == 0)) + return 0; + + /* create midi ports */ + err = snd_rawmidi_new(oxfw->card, oxfw->card->driver, 0, + oxfw->midi_output_ports, oxfw->midi_input_ports, + &rmidi); + if (err < 0) + return err; + + snprintf(rmidi->name, sizeof(rmidi->name), + "%s MIDI", oxfw->card->shortname); + rmidi->private_data = oxfw; + + if (oxfw->midi_input_ports > 0) { + rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT; + + snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, + &midi_capture_ops); + + str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]; + + set_midi_substream_names(oxfw, str); + } + + if (oxfw->midi_output_ports > 0) { + rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT; + + snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, + &midi_playback_ops); + + str = &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]; + + set_midi_substream_names(oxfw, str); + } + + if ((oxfw->midi_output_ports > 0) && (oxfw->midi_input_ports > 0)) + rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX; + + return 0; +} diff --git a/sound/firewire/oxfw/oxfw-stream.c b/sound/firewire/oxfw/oxfw-stream.c index 1d154284873e..a38b3c36faca 100644 --- a/sound/firewire/oxfw/oxfw-stream.c +++ b/sound/firewire/oxfw/oxfw-stream.c @@ -282,6 +282,10 @@ int snd_oxfw_stream_start_simplex(struct snd_oxfw *oxfw, err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation); if (err < 0) goto end; + if (rate == 0) + rate = formation.rate; + if (pcm_channels == 0) + pcm_channels = formation.pcm; if ((formation.rate != rate) || (formation.pcm != pcm_channels)) { if (opposite != NULL) { diff --git a/sound/firewire/oxfw/oxfw.c b/sound/firewire/oxfw/oxfw.c index 23c00a2bb7d3..9cfbfb168dac 100644 --- a/sound/firewire/oxfw/oxfw.c +++ b/sound/firewire/oxfw/oxfw.c @@ -138,6 +138,7 @@ static int oxfw_probe(struct fw_unit *unit, mutex_init(&oxfw->mutex); oxfw->unit = unit; oxfw->device_info = (const struct device_info *)id->driver_data; + spin_lock_init(&oxfw->lock); err = snd_oxfw_stream_discover(oxfw); if (err < 0) @@ -159,6 +160,10 @@ static int oxfw_probe(struct fw_unit *unit, snd_oxfw_proc_init(oxfw); + err = snd_oxfw_create_midi(oxfw); + if (err < 0) + goto error; + err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->rx_stream); if (err < 0) goto error; diff --git a/sound/firewire/oxfw/oxfw.h b/sound/firewire/oxfw/oxfw.h index 2211d11a79e1..83a54fc73a11 100644 --- a/sound/firewire/oxfw/oxfw.h +++ b/sound/firewire/oxfw/oxfw.h @@ -19,6 +19,7 @@ #include #include #include +#include #include "../lib.h" #include "../fcp.h" @@ -43,6 +44,7 @@ struct snd_oxfw { struct fw_unit *unit; const struct device_info *device_info; struct mutex mutex; + spinlock_t lock; bool has_output; u8 *tx_stream_formats[SND_OXFW_STREAM_FORMAT_ENTRIES]; @@ -55,6 +57,9 @@ struct snd_oxfw { unsigned int capture_substreams; unsigned int playback_substreams; + unsigned int midi_input_ports; + unsigned int midi_output_ports; + bool mute; s16 volume[6]; s16 volume_min; @@ -124,3 +129,5 @@ int snd_oxfw_create_pcm(struct snd_oxfw *oxfw); int snd_oxfw_create_mixer(struct snd_oxfw *oxfw); void snd_oxfw_proc_init(struct snd_oxfw *oxfw); + +int snd_oxfw_create_midi(struct snd_oxfw *oxfw); -- cgit v1.2.3 From 8985f4ac1c42bd25799f294f4e87fa73064673c7 Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Tue, 9 Dec 2014 00:10:49 +0900 Subject: ALSA: oxfw: Add hwdep interface This interface is designed for mixer/control application. By using this interface, an application can get information about firewire node, can lock/unlock kernel streaming and can get notification at starting/stopping kernel streaming. Signed-off-by: Takashi Sakamoto Acked-by: Clemens Ladisch Signed-off-by: Takashi Iwai --- include/uapi/sound/asound.h | 3 +- include/uapi/sound/firewire.h | 3 +- sound/firewire/Kconfig | 1 + sound/firewire/oxfw/Makefile | 2 +- sound/firewire/oxfw/oxfw-hwdep.c | 190 ++++++++++++++++++++++++++++++++++++++ sound/firewire/oxfw/oxfw-midi.c | 16 ++++ sound/firewire/oxfw/oxfw-pcm.c | 12 ++- sound/firewire/oxfw/oxfw-stream.c | 39 ++++++++ sound/firewire/oxfw/oxfw.c | 5 + sound/firewire/oxfw/oxfw.h | 13 +++ 10 files changed, 280 insertions(+), 4 deletions(-) create mode 100644 sound/firewire/oxfw/oxfw-hwdep.c (limited to 'sound/firewire/oxfw/oxfw-stream.c') diff --git a/include/uapi/sound/asound.h b/include/uapi/sound/asound.h index 941d32f007dc..1f23cd635957 100644 --- a/include/uapi/sound/asound.h +++ b/include/uapi/sound/asound.h @@ -96,9 +96,10 @@ enum { SNDRV_HWDEP_IFACE_FW_DICE, /* TC DICE FireWire device */ SNDRV_HWDEP_IFACE_FW_FIREWORKS, /* Echo Audio Fireworks based device */ SNDRV_HWDEP_IFACE_FW_BEBOB, /* BridgeCo BeBoB based device */ + SNDRV_HWDEP_IFACE_FW_OXFW, /* Oxford OXFW970/971 based device */ /* Don't forget to change the following: */ - SNDRV_HWDEP_IFACE_LAST = SNDRV_HWDEP_IFACE_FW_BEBOB + SNDRV_HWDEP_IFACE_LAST = SNDRV_HWDEP_IFACE_FW_OXFW }; struct snd_hwdep_info { diff --git a/include/uapi/sound/firewire.h b/include/uapi/sound/firewire.h index af4bd136c75d..49122df3b56b 100644 --- a/include/uapi/sound/firewire.h +++ b/include/uapi/sound/firewire.h @@ -55,7 +55,8 @@ union snd_firewire_event { #define SNDRV_FIREWIRE_TYPE_DICE 1 #define SNDRV_FIREWIRE_TYPE_FIREWORKS 2 #define SNDRV_FIREWIRE_TYPE_BEBOB 3 -/* AV/C, RME, MOTU, ... */ +#define SNDRV_FIREWIRE_TYPE_OXFW 4 +/* RME, MOTU, ... */ struct snd_firewire_get_info { unsigned int type; /* SNDRV_FIREWIRE_TYPE_xxx */ diff --git a/sound/firewire/Kconfig b/sound/firewire/Kconfig index 6364e5b90a00..ecec547782b2 100644 --- a/sound/firewire/Kconfig +++ b/sound/firewire/Kconfig @@ -26,6 +26,7 @@ config SND_DICE config SND_OXFW tristate "Oxford Semiconductor FW970/971 chipset support" select SND_FIREWIRE_LIB + select SND_HWDEP help Say Y here to include support for FireWire devices based on Oxford Semiconductor FW970/971 chipset. diff --git a/sound/firewire/oxfw/Makefile b/sound/firewire/oxfw/Makefile index 3904a302d48e..a926850864f6 100644 --- a/sound/firewire/oxfw/Makefile +++ b/sound/firewire/oxfw/Makefile @@ -1,3 +1,3 @@ snd-oxfw-objs := oxfw-command.o oxfw-stream.o oxfw-control.o oxfw-pcm.o \ - oxfw-proc.o oxfw-midi.o oxfw.o + oxfw-proc.o oxfw-midi.o oxfw-hwdep.o oxfw.o obj-m += snd-oxfw.o diff --git a/sound/firewire/oxfw/oxfw-hwdep.c b/sound/firewire/oxfw/oxfw-hwdep.c new file mode 100644 index 000000000000..ff2687ad0460 --- /dev/null +++ b/sound/firewire/oxfw/oxfw-hwdep.c @@ -0,0 +1,190 @@ +/* + * oxfw_hwdep.c - a part of driver for OXFW970/971 based devices + * + * Copyright (c) 2014 Takashi Sakamoto + * + * Licensed under the terms of the GNU General Public License, version 2. + */ + +/* + * This codes give three functionality. + * + * 1.get firewire node information + * 2.get notification about starting/stopping stream + * 3.lock/unlock stream + */ + +#include "oxfw.h" + +static long hwdep_read(struct snd_hwdep *hwdep, char __user *buf, long count, + loff_t *offset) +{ + struct snd_oxfw *oxfw = hwdep->private_data; + DEFINE_WAIT(wait); + union snd_firewire_event event; + + spin_lock_irq(&oxfw->lock); + + while (!oxfw->dev_lock_changed) { + prepare_to_wait(&oxfw->hwdep_wait, &wait, TASK_INTERRUPTIBLE); + spin_unlock_irq(&oxfw->lock); + schedule(); + finish_wait(&oxfw->hwdep_wait, &wait); + if (signal_pending(current)) + return -ERESTARTSYS; + spin_lock_irq(&oxfw->lock); + } + + memset(&event, 0, sizeof(event)); + if (oxfw->dev_lock_changed) { + event.lock_status.type = SNDRV_FIREWIRE_EVENT_LOCK_STATUS; + event.lock_status.status = (oxfw->dev_lock_count > 0); + oxfw->dev_lock_changed = false; + + count = min_t(long, count, sizeof(event.lock_status)); + } + + spin_unlock_irq(&oxfw->lock); + + if (copy_to_user(buf, &event, count)) + return -EFAULT; + + return count; +} + +static unsigned int hwdep_poll(struct snd_hwdep *hwdep, struct file *file, + poll_table *wait) +{ + struct snd_oxfw *oxfw = hwdep->private_data; + unsigned int events; + + poll_wait(file, &oxfw->hwdep_wait, wait); + + spin_lock_irq(&oxfw->lock); + if (oxfw->dev_lock_changed) + events = POLLIN | POLLRDNORM; + else + events = 0; + spin_unlock_irq(&oxfw->lock); + + return events; +} + +static int hwdep_get_info(struct snd_oxfw *oxfw, void __user *arg) +{ + struct fw_device *dev = fw_parent_device(oxfw->unit); + struct snd_firewire_get_info info; + + memset(&info, 0, sizeof(info)); + info.type = SNDRV_FIREWIRE_TYPE_OXFW; + info.card = dev->card->index; + *(__be32 *)&info.guid[0] = cpu_to_be32(dev->config_rom[3]); + *(__be32 *)&info.guid[4] = cpu_to_be32(dev->config_rom[4]); + strlcpy(info.device_name, dev_name(&dev->device), + sizeof(info.device_name)); + + if (copy_to_user(arg, &info, sizeof(info))) + return -EFAULT; + + return 0; +} + +static int hwdep_lock(struct snd_oxfw *oxfw) +{ + int err; + + spin_lock_irq(&oxfw->lock); + + if (oxfw->dev_lock_count == 0) { + oxfw->dev_lock_count = -1; + err = 0; + } else { + err = -EBUSY; + } + + spin_unlock_irq(&oxfw->lock); + + return err; +} + +static int hwdep_unlock(struct snd_oxfw *oxfw) +{ + int err; + + spin_lock_irq(&oxfw->lock); + + if (oxfw->dev_lock_count == -1) { + oxfw->dev_lock_count = 0; + err = 0; + } else { + err = -EBADFD; + } + + spin_unlock_irq(&oxfw->lock); + + return err; +} + +static int hwdep_release(struct snd_hwdep *hwdep, struct file *file) +{ + struct snd_oxfw *oxfw = hwdep->private_data; + + spin_lock_irq(&oxfw->lock); + if (oxfw->dev_lock_count == -1) + oxfw->dev_lock_count = 0; + spin_unlock_irq(&oxfw->lock); + + return 0; +} + +static int hwdep_ioctl(struct snd_hwdep *hwdep, struct file *file, + unsigned int cmd, unsigned long arg) +{ + struct snd_oxfw *oxfw = hwdep->private_data; + + switch (cmd) { + case SNDRV_FIREWIRE_IOCTL_GET_INFO: + return hwdep_get_info(oxfw, (void __user *)arg); + case SNDRV_FIREWIRE_IOCTL_LOCK: + return hwdep_lock(oxfw); + case SNDRV_FIREWIRE_IOCTL_UNLOCK: + return hwdep_unlock(oxfw); + default: + return -ENOIOCTLCMD; + } +} + +#ifdef CONFIG_COMPAT +static int hwdep_compat_ioctl(struct snd_hwdep *hwdep, struct file *file, + unsigned int cmd, unsigned long arg) +{ + return hwdep_ioctl(hwdep, file, cmd, + (unsigned long)compat_ptr(arg)); +} +#else +#define hwdep_compat_ioctl NULL +#endif + +int snd_oxfw_create_hwdep(struct snd_oxfw *oxfw) +{ + static const struct snd_hwdep_ops hwdep_ops = { + .read = hwdep_read, + .release = hwdep_release, + .poll = hwdep_poll, + .ioctl = hwdep_ioctl, + .ioctl_compat = hwdep_compat_ioctl, + }; + struct snd_hwdep *hwdep; + int err; + + err = snd_hwdep_new(oxfw->card, oxfw->card->driver, 0, &hwdep); + if (err < 0) + goto end; + strcpy(hwdep->name, oxfw->card->driver); + hwdep->iface = SNDRV_HWDEP_IFACE_FW_OXFW; + hwdep->ops = hwdep_ops; + hwdep->private_data = oxfw; + hwdep->exclusive = true; +end: + return err; +} diff --git a/sound/firewire/oxfw/oxfw-midi.c b/sound/firewire/oxfw/oxfw-midi.c index 334b11d1a422..540a30338516 100644 --- a/sound/firewire/oxfw/oxfw-midi.c +++ b/sound/firewire/oxfw/oxfw-midi.c @@ -13,6 +13,10 @@ static int midi_capture_open(struct snd_rawmidi_substream *substream) struct snd_oxfw *oxfw = substream->rmidi->private_data; int err; + err = snd_oxfw_stream_lock_try(oxfw); + if (err < 0) + return err; + mutex_lock(&oxfw->mutex); oxfw->capture_substreams++; @@ -20,6 +24,9 @@ static int midi_capture_open(struct snd_rawmidi_substream *substream) mutex_unlock(&oxfw->mutex); + if (err < 0) + snd_oxfw_stream_lock_release(oxfw); + return err; } @@ -28,6 +35,10 @@ static int midi_playback_open(struct snd_rawmidi_substream *substream) struct snd_oxfw *oxfw = substream->rmidi->private_data; int err; + err = snd_oxfw_stream_lock_try(oxfw); + if (err < 0) + return err; + mutex_lock(&oxfw->mutex); oxfw->playback_substreams++; @@ -35,6 +46,9 @@ static int midi_playback_open(struct snd_rawmidi_substream *substream) mutex_unlock(&oxfw->mutex); + if (err < 0) + snd_oxfw_stream_lock_release(oxfw); + return err; } @@ -49,6 +63,7 @@ static int midi_capture_close(struct snd_rawmidi_substream *substream) mutex_unlock(&oxfw->mutex); + snd_oxfw_stream_lock_release(oxfw); return 0; } @@ -63,6 +78,7 @@ static int midi_playback_close(struct snd_rawmidi_substream *substream) mutex_unlock(&oxfw->mutex); + snd_oxfw_stream_lock_release(oxfw); return 0; } diff --git a/sound/firewire/oxfw/oxfw-pcm.c b/sound/firewire/oxfw/oxfw-pcm.c index e84fc9c4bfd1..9bc556b15a92 100644 --- a/sound/firewire/oxfw/oxfw-pcm.c +++ b/sound/firewire/oxfw/oxfw-pcm.c @@ -192,10 +192,14 @@ static int pcm_open(struct snd_pcm_substream *substream) struct snd_oxfw *oxfw = substream->private_data; int err; - err = init_hw_params(oxfw, substream); + err = snd_oxfw_stream_lock_try(oxfw); if (err < 0) goto end; + err = init_hw_params(oxfw, substream); + if (err < 0) + goto err_locked; + /* * When any PCM streams are already running, the available sampling * rate is limited at current value. @@ -210,10 +214,16 @@ static int pcm_open(struct snd_pcm_substream *substream) snd_pcm_set_sync(substream); end: return err; +err_locked: + snd_oxfw_stream_lock_release(oxfw); + return err; } static int pcm_close(struct snd_pcm_substream *substream) { + struct snd_oxfw *oxfw = substream->private_data; + + snd_oxfw_stream_lock_release(oxfw); return 0; } diff --git a/sound/firewire/oxfw/oxfw-stream.c b/sound/firewire/oxfw/oxfw-stream.c index a38b3c36faca..b77cf80f1678 100644 --- a/sound/firewire/oxfw/oxfw-stream.c +++ b/sound/firewire/oxfw/oxfw-stream.c @@ -644,3 +644,42 @@ int snd_oxfw_stream_discover(struct snd_oxfw *oxfw) end: return err; } + +void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw) +{ + oxfw->dev_lock_changed = true; + wake_up(&oxfw->hwdep_wait); +} + +int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw) +{ + int err; + + spin_lock_irq(&oxfw->lock); + + /* user land lock this */ + if (oxfw->dev_lock_count < 0) { + err = -EBUSY; + goto end; + } + + /* this is the first time */ + if (oxfw->dev_lock_count++ == 0) + snd_oxfw_stream_lock_changed(oxfw); + err = 0; +end: + spin_unlock_irq(&oxfw->lock); + return err; +} + +void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw) +{ + spin_lock_irq(&oxfw->lock); + + if (WARN_ON(oxfw->dev_lock_count <= 0)) + goto end; + if (--oxfw->dev_lock_count == 0) + snd_oxfw_stream_lock_changed(oxfw); +end: + spin_unlock_irq(&oxfw->lock); +} diff --git a/sound/firewire/oxfw/oxfw.c b/sound/firewire/oxfw/oxfw.c index 9cfbfb168dac..cf1d0b55e827 100644 --- a/sound/firewire/oxfw/oxfw.c +++ b/sound/firewire/oxfw/oxfw.c @@ -139,6 +139,7 @@ static int oxfw_probe(struct fw_unit *unit, oxfw->unit = unit; oxfw->device_info = (const struct device_info *)id->driver_data; spin_lock_init(&oxfw->lock); + init_waitqueue_head(&oxfw->hwdep_wait); err = snd_oxfw_stream_discover(oxfw); if (err < 0) @@ -164,6 +165,10 @@ static int oxfw_probe(struct fw_unit *unit, if (err < 0) goto error; + err = snd_oxfw_create_hwdep(oxfw); + if (err < 0) + goto error; + err = snd_oxfw_stream_init_simplex(oxfw, &oxfw->rx_stream); if (err < 0) goto error; diff --git a/sound/firewire/oxfw/oxfw.h b/sound/firewire/oxfw/oxfw.h index 83a54fc73a11..cace5ad4fe76 100644 --- a/sound/firewire/oxfw/oxfw.h +++ b/sound/firewire/oxfw/oxfw.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -20,6 +21,8 @@ #include #include #include +#include +#include #include "../lib.h" #include "../fcp.h" @@ -64,6 +67,10 @@ struct snd_oxfw { s16 volume[6]; s16 volume_min; s16 volume_max; + + int dev_lock_count; + bool dev_lock_changed; + wait_queue_head_t hwdep_wait; }; /* @@ -124,6 +131,10 @@ int snd_oxfw_stream_get_current_formation(struct snd_oxfw *oxfw, int snd_oxfw_stream_discover(struct snd_oxfw *oxfw); +void snd_oxfw_stream_lock_changed(struct snd_oxfw *oxfw); +int snd_oxfw_stream_lock_try(struct snd_oxfw *oxfw); +void snd_oxfw_stream_lock_release(struct snd_oxfw *oxfw); + int snd_oxfw_create_pcm(struct snd_oxfw *oxfw); int snd_oxfw_create_mixer(struct snd_oxfw *oxfw); @@ -131,3 +142,5 @@ int snd_oxfw_create_mixer(struct snd_oxfw *oxfw); void snd_oxfw_proc_init(struct snd_oxfw *oxfw); int snd_oxfw_create_midi(struct snd_oxfw *oxfw); + +int snd_oxfw_create_hwdep(struct snd_oxfw *oxfw); -- cgit v1.2.3