ALSA: firewire: extend snd_fw_transaction()
[sfrench/cifs-2.6.git] / sound / firewire / dice.c
index 1da1ddefd6a80b0816699d3080e0f5b142ef73be..59d5ca4438b26fbcca68fb0b917454ada4f65c14 100644 (file)
@@ -118,7 +118,7 @@ static int dice_owner_set(struct dice *dice)
 {
        struct fw_device *device = fw_parent_device(dice->unit);
        __be64 *buffer;
-       int rcode, err, errors = 0;
+       int err, errors = 0;
 
        buffer = kmalloc(2 * 8, GFP_KERNEL);
        if (!buffer)
@@ -132,31 +132,24 @@ static int dice_owner_set(struct dice *dice)
 
                dice->owner_generation = device->generation;
                smp_rmb(); /* node_id vs. generation */
-               rcode = fw_run_transaction(device->card,
-                                          TCODE_LOCK_COMPARE_SWAP,
-                                          device->node_id,
-                                          dice->owner_generation,
-                                          device->max_speed,
-                                          global_address(dice, GLOBAL_OWNER),
-                                          buffer, 2 * 8);
-
-               if (rcode == RCODE_COMPLETE) {
-                       if (buffer[0] == cpu_to_be64(OWNER_NO_OWNER)) {
-                               err = 0;
-                       } else {
+               err = snd_fw_transaction(dice->unit,
+                                        TCODE_LOCK_COMPARE_SWAP,
+                                        global_address(dice, GLOBAL_OWNER),
+                                        buffer, 2 * 8,
+                                        FW_FIXED_GENERATION |
+                                                       dice->owner_generation);
+
+               if (err == 0) {
+                       if (buffer[0] != cpu_to_be64(OWNER_NO_OWNER)) {
                                dev_err(&dice->unit->device,
                                        "device is already in use\n");
                                err = -EBUSY;
                        }
                        break;
                }
-               if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
-                       dev_err(&dice->unit->device,
-                               "setting device owner failed: %s\n",
-                               fw_rcode_string(rcode));
-                       err = -EIO;
+               if (err != -EAGAIN || ++errors >= 3)
                        break;
-               }
+
                msleep(20);
        }
 
@@ -169,7 +162,7 @@ static int dice_owner_update(struct dice *dice)
 {
        struct fw_device *device = fw_parent_device(dice->unit);
        __be64 *buffer;
-       int rcode, err, errors = 0;
+       int err;
 
        if (dice->owner_generation == -1)
                return 0;
@@ -178,44 +171,26 @@ static int dice_owner_update(struct dice *dice)
        if (!buffer)
                return -ENOMEM;
 
-       for (;;) {
-               buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
-               buffer[1] = cpu_to_be64(
-                       ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
-                       dice->notification_handler.offset);
+       buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
+       buffer[1] = cpu_to_be64(
+               ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
+               dice->notification_handler.offset);
 
-               dice->owner_generation = device->generation;
-               smp_rmb(); /* node_id vs. generation */
-               rcode = fw_run_transaction(device->card,
-                                          TCODE_LOCK_COMPARE_SWAP,
-                                          device->node_id,
-                                          dice->owner_generation,
-                                          device->max_speed,
-                                          global_address(dice, GLOBAL_OWNER),
-                                          buffer, 2 * 8);
-
-               if (rcode == RCODE_COMPLETE) {
-                       if (buffer[0] == cpu_to_be64(OWNER_NO_OWNER)) {
-                               err = 0;
-                       } else {
-                               dev_err(&dice->unit->device,
-                                       "device is already in use\n");
-                               err = -EBUSY;
-                       }
-                       break;
-               }
-               if (rcode == RCODE_GENERATION) {
-                       err = 0; /* try again later */
-                       break;
-               }
-               if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
+       dice->owner_generation = device->generation;
+       smp_rmb(); /* node_id vs. generation */
+       err = snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
+                                global_address(dice, GLOBAL_OWNER),
+                                buffer, 2 * 8,
+                                FW_FIXED_GENERATION | dice->owner_generation);
+
+       if (err == 0) {
+               if (buffer[0] != cpu_to_be64(OWNER_NO_OWNER)) {
                        dev_err(&dice->unit->device,
-                               "setting device owner failed: %s\n",
-                               fw_rcode_string(rcode));
-                       err = -EIO;
-                       break;
+                               "device is already in use\n");
+                       err = -EBUSY;
                }
-               msleep(20);
+       } else if (err == -EAGAIN) {
+               err = 0; /* try again later */
        }
 
        kfree(buffer);
@@ -230,38 +205,19 @@ static void dice_owner_clear(struct dice *dice)
 {
        struct fw_device *device = fw_parent_device(dice->unit);
        __be64 *buffer;
-       int rcode, errors = 0;
 
        buffer = kmalloc(2 * 8, GFP_KERNEL);
        if (!buffer)
                return;
 
-       for (;;) {
-               buffer[0] = cpu_to_be64(
-                       ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
-                       dice->notification_handler.offset);
-               buffer[1] = cpu_to_be64(OWNER_NO_OWNER);
-
-               rcode = fw_run_transaction(device->card,
-                                          TCODE_LOCK_COMPARE_SWAP,
-                                          device->node_id,
-                                          dice->owner_generation,
-                                          device->max_speed,
-                                          global_address(dice, GLOBAL_OWNER),
-                                          buffer, 2 * 8);
-
-               if (rcode == RCODE_COMPLETE)
-                       break;
-               if (rcode == RCODE_GENERATION)
-                       break;
-               if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
-                       dev_err(&dice->unit->device,
-                               "clearing device owner failed: %s\n",
-                               fw_rcode_string(rcode));
-                       break;
-               }
-               msleep(20);
-       }
+       buffer[0] = cpu_to_be64(
+               ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
+               dice->notification_handler.offset);
+       buffer[1] = cpu_to_be64(OWNER_NO_OWNER);
+       snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
+                          global_address(dice, GLOBAL_OWNER),
+                          buffer, 2 * 8, FW_QUIET |
+                          FW_FIXED_GENERATION | dice->owner_generation);
 
        kfree(buffer);
 
@@ -270,67 +226,32 @@ static void dice_owner_clear(struct dice *dice)
 
 static int dice_enable_set(struct dice *dice)
 {
-       struct fw_device *device = fw_parent_device(dice->unit);
        __be32 value;
-       int rcode, err, errors = 0;
+       int err;
 
        value = cpu_to_be32(1);
-       for (;;) {
-               rcode = fw_run_transaction(device->card,
-                                          TCODE_WRITE_QUADLET_REQUEST,
-                                          device->node_id,
-                                          dice->owner_generation,
-                                          device->max_speed,
-                                          global_address(dice, GLOBAL_ENABLE),
-                                          &value, 4);
-               if (rcode == RCODE_COMPLETE) {
-                       dice->global_enabled = true;
-                       err = 0;
-                       break;
-               }
-               if (rcode == RCODE_GENERATION) {
-                       err = -EAGAIN;
-                       break;
-               }
-               if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
-                       dev_err(&dice->unit->device,
-                               "device enabling failed: %s\n",
-                               fw_rcode_string(rcode));
-                       err = -EIO;
-                       break;
-               }
-               msleep(20);
-       }
+       err = snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
+                                global_address(dice, GLOBAL_ENABLE),
+                                &value, 4,
+                                FW_FIXED_GENERATION | dice->owner_generation);
+       if (err < 0)
+               return err;
 
-       return err;
+       dice->global_enabled = true;
+
+       return 0;
 }
 
 static void dice_enable_clear(struct dice *dice)
 {
-       struct fw_device *device = fw_parent_device(dice->unit);
        __be32 value;
-       int rcode, errors = 0;
 
        value = 0;
-       for (;;) {
-               rcode = fw_run_transaction(device->card,
-                                          TCODE_WRITE_QUADLET_REQUEST,
-                                          device->node_id,
-                                          dice->owner_generation,
-                                          device->max_speed,
-                                          global_address(dice, GLOBAL_ENABLE),
-                                          &value, 4);
-               if (rcode == RCODE_COMPLETE ||
-                   rcode == RCODE_GENERATION)
-                       break;
-               if (rcode_is_permanent_error(rcode) || ++errors >= 3) {
-                       dev_err(&dice->unit->device,
-                               "device disabling failed: %s\n",
-                               fw_rcode_string(rcode));
-                       break;
-               }
-               msleep(20);
-       }
+       snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
+                          global_address(dice, GLOBAL_ENABLE),
+                          &value, 4, FW_QUIET |
+                          FW_FIXED_GENERATION | dice->owner_generation);
+
        dice->global_enabled = false;
 }
 
@@ -374,8 +295,8 @@ static int dice_open(struct snd_pcm_substream *substream)
        };
        struct dice *dice = substream->private_data;
        struct snd_pcm_runtime *runtime = substream->runtime;
-       __be32 clock_sel, number_audio, number_midi;
-       unsigned int rate;
+       __be32 clock_sel, data[2];
+       unsigned int rate_index, number_audio, number_midi;
        int err;
 
        err = dice_try_lock(dice);
@@ -384,38 +305,45 @@ static int dice_open(struct snd_pcm_substream *substream)
 
        err = snd_fw_transaction(dice->unit, TCODE_READ_QUADLET_REQUEST,
                                 global_address(dice, GLOBAL_CLOCK_SELECT),
-                                &clock_sel, 4);
+                                &clock_sel, 4, 0);
        if (err < 0)
                goto err_lock;
-       rate = (be32_to_cpu(clock_sel) & CLOCK_RATE_MASK) >> CLOCK_RATE_SHIFT;
-       if (rate >= ARRAY_SIZE(dice_rates)) {
+       rate_index = (be32_to_cpu(clock_sel) & CLOCK_RATE_MASK)
+                                                       >> CLOCK_RATE_SHIFT;
+       if (rate_index >= ARRAY_SIZE(dice_rates)) {
                err = -ENXIO;
                goto err_lock;
        }
-       rate = dice_rates[rate];
 
-       err = snd_fw_transaction(dice->unit, TCODE_READ_QUADLET_REQUEST,
+       err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
                                 rx_address(dice, RX_NUMBER_AUDIO),
-                                &number_audio, 4);
-       if (err < 0)
-               goto err_lock;
-       err = snd_fw_transaction(dice->unit, TCODE_READ_QUADLET_REQUEST,
-                                rx_address(dice, RX_NUMBER_MIDI),
-                                &number_midi, 4);
+                                data, 2 * 4, 0);
        if (err < 0)
                goto err_lock;
+       number_audio = be32_to_cpu(data[0]);
+       number_midi = be32_to_cpu(data[1]);
 
        runtime->hw = hardware;
 
-       runtime->hw.rates = snd_pcm_rate_to_rate_bit(rate);
+       runtime->hw.rates = snd_pcm_rate_to_rate_bit(dice_rates[rate_index]);
        snd_pcm_limit_hw_rates(runtime);
 
-       runtime->hw.channels_min = be32_to_cpu(number_audio);
-       runtime->hw.channels_max = be32_to_cpu(number_audio);
+       runtime->hw.channels_min = number_audio;
+       runtime->hw.channels_max = number_audio;
 
-       amdtp_out_stream_set_rate(&dice->stream, rate);
-       amdtp_out_stream_set_pcm(&dice->stream, be32_to_cpu(number_audio));
-       amdtp_out_stream_set_midi(&dice->stream, be32_to_cpu(number_midi));
+       amdtp_out_stream_set_parameters(&dice->stream, dice_rates[rate_index],
+                                       number_audio, number_midi);
+
+       err = snd_pcm_hw_constraint_step(runtime, 0,
+                                        SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
+                                        amdtp_syt_intervals[rate_index]);
+       if (err < 0)
+               goto err_lock;
+       err = snd_pcm_hw_constraint_step(runtime, 0,
+                                        SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
+                                        amdtp_syt_intervals[rate_index]);
+       if (err < 0)
+               goto err_lock;
 
        err = snd_pcm_hw_constraint_minmax(runtime,
                                           SNDRV_PCM_HW_PARAM_PERIOD_TIME,
@@ -481,7 +409,7 @@ static int dice_stream_start(struct dice *dice)
                err = snd_fw_transaction(dice->unit,
                                         TCODE_WRITE_QUADLET_REQUEST,
                                         rx_address(dice, RX_ISOCHRONOUS),
-                                        &channel, 4);
+                                        &channel, 4, 0);
                if (err < 0)
                        goto err_resources;
        }
@@ -495,7 +423,7 @@ static int dice_stream_start(struct dice *dice)
 err_rx_channel:
        channel = cpu_to_be32((u32)-1);
        snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
-                          rx_address(dice, RX_ISOCHRONOUS), &channel, 4);
+                          rx_address(dice, RX_ISOCHRONOUS), &channel, 4, 0);
 err_resources:
        fw_iso_resources_free(&dice->resources);
 error:
@@ -521,7 +449,7 @@ static void dice_stream_stop(struct dice *dice)
 
        channel = cpu_to_be32((u32)-1);
        snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
-                          rx_address(dice, RX_ISOCHRONOUS), &channel, 4);
+                          rx_address(dice, RX_ISOCHRONOUS), &channel, 4, 0);
 
        fw_iso_resources_free(&dice->resources);
 }
@@ -827,28 +755,93 @@ static void dice_card_free(struct snd_card *card)
        mutex_destroy(&dice->mutex);
 }
 
+#define DICE_CATEGORY_ID 0x04
+
+static int dice_interface_check(struct fw_unit *unit)
+{
+       static const int min_values[10] = {
+               10, 0x64 / 4,
+               10, 0x18 / 4,
+               10, 0x18 / 4,
+               0, 0,
+               0, 0,
+       };
+       struct fw_device *device = fw_parent_device(unit);
+       struct fw_csr_iterator it;
+       int key, value, vendor = -1, model = -1, err;
+       unsigned int i;
+       __be32 pointers[ARRAY_SIZE(min_values)];
+       __be32 version;
+
+       /*
+        * Check that GUID and unit directory are constructed according to DICE
+        * rules, i.e., that the specifier ID is the GUID's OUI, and that the
+        * GUID chip ID consists of the 8-bit DICE category ID, the 10-bit
+        * product ID, and a 22-bit serial number.
+        */
+       fw_csr_iterator_init(&it, unit->directory);
+       while (fw_csr_iterator_next(&it, &key, &value)) {
+               switch (key) {
+               case CSR_SPECIFIER_ID:
+                       vendor = value;
+                       break;
+               case CSR_MODEL:
+                       model = value;
+                       break;
+               }
+       }
+       if (device->config_rom[3] != ((vendor << 8) | DICE_CATEGORY_ID) ||
+           device->config_rom[4] >> 22 != model)
+               return -ENODEV;
+
+       /*
+        * Check that the sub address spaces exist and are located inside the
+        * private address space.  The minimum values are chosen so that all
+        * minimally required registers are included.
+        */
+       err = snd_fw_transaction(unit, TCODE_READ_BLOCK_REQUEST,
+                                DICE_PRIVATE_SPACE,
+                                pointers, sizeof(pointers), 0);
+       if (err < 0)
+               return -ENODEV;
+       for (i = 0; i < ARRAY_SIZE(pointers); ++i) {
+               value = be32_to_cpu(pointers[i]);
+               if (value < min_values[i] || value >= 0x40000)
+                       return -ENODEV;
+       }
+
+       /*
+        * Check that the implemented DICE driver specification major version
+        * number matches.
+        */
+       err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
+                                DICE_PRIVATE_SPACE +
+                                be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION,
+                                &version, 4, 0);
+       if (err < 0)
+               return -ENODEV;
+       if ((version & cpu_to_be32(0xff000000)) != cpu_to_be32(0x01000000)) {
+               dev_err(&unit->device,
+                       "unknown DICE version: 0x%08x\n", be32_to_cpu(version));
+               return -ENODEV;
+       }
+
+       return 0;
+}
+
 static int dice_init_offsets(struct dice *dice)
 {
        __be32 pointers[6];
-       unsigned int global_size, rx_size;
        int err;
 
        err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
-                                DICE_PRIVATE_SPACE, &pointers, 6 * 4);
+                                DICE_PRIVATE_SPACE,
+                                pointers, sizeof(pointers), 0);
        if (err < 0)
                return err;
 
        dice->global_offset = be32_to_cpu(pointers[0]) * 4;
-       global_size = be32_to_cpu(pointers[1]);
        dice->rx_offset = be32_to_cpu(pointers[4]) * 4;
-       rx_size = be32_to_cpu(pointers[5]);
-
-       /* some sanity checks to ensure that we actually have a DICE */
-       if (dice->global_offset < 10 * 4 || global_size < 0x168 / 4 ||
-           dice->rx_offset < 10 * 4 || rx_size < 0x120 / 4) {
-               dev_err(&dice->unit->device, "invalid register pointers\n");
-               return -ENXIO;
-       }
 
        return 0;
 }
@@ -867,7 +860,7 @@ static void dice_card_strings(struct dice *dice)
        BUILD_BUG_ON(NICK_NAME_SIZE < sizeof(card->shortname));
        err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
                                 global_address(dice, GLOBAL_NICK_NAME),
-                                card->shortname, sizeof(card->shortname));
+                                card->shortname, sizeof(card->shortname), 0);
        if (err >= 0) {
                /* DICE strings are returned in "always-wrong" endianness */
                BUILD_BUG_ON(sizeof(card->shortname) % 4 != 0);
@@ -881,8 +874,8 @@ static void dice_card_strings(struct dice *dice)
        strcpy(model, "?");
        fw_csr_string(dice->unit->directory, CSR_MODEL, model, sizeof(model));
        snprintf(card->longname, sizeof(card->longname),
-                "%s %s, GUID %08x%08x at %s, S%d",
-                vendor, model, dev->config_rom[3], dev->config_rom[4],
+                "%s %s (serial %u) at %s, S%d",
+                vendor, model, dev->config_rom[4] & 0x3fffff,
                 dev_name(&dice->unit->device), 100 << dev->max_speed);
 
        strcpy(card->mixername, "DICE");
@@ -895,6 +888,10 @@ static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
        __be32 clock_sel;
        int err;
 
+       err = dice_interface_check(unit);
+       if (err < 0)
+               return err;
+
        err = snd_card_create(-1, NULL, THIS_MODULE, sizeof(*dice), &card);
        if (err < 0)
                return err;
@@ -924,7 +921,8 @@ static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
                goto err_notification_handler;
        dice->resources.channels_mask = 0x00000000ffffffffuLL;
 
-       err = amdtp_out_stream_init(&dice->stream, unit, CIP_BLOCKING);
+       err = amdtp_out_stream_init(&dice->stream, unit,
+                                   CIP_BLOCKING | CIP_HI_DUALWIRE);
        if (err < 0)
                goto err_resources;
 
@@ -938,14 +936,14 @@ static int dice_probe(struct fw_unit *unit, const struct ieee1394_device_id *id)
 
        err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
                                 global_address(dice, GLOBAL_CLOCK_SELECT),
-                                &clock_sel, 4);
+                                &clock_sel, 4, 0);
        if (err < 0)
                goto error;
        clock_sel &= cpu_to_be32(~CLOCK_SOURCE_MASK);
        clock_sel |= cpu_to_be32(CLOCK_SOURCE_ARX1);
        err = snd_fw_transaction(unit, TCODE_WRITE_QUADLET_REQUEST,
                                 global_address(dice, GLOBAL_CLOCK_SELECT),
-                                &clock_sel, 4);
+                                &clock_sel, 4, 0);
        if (err < 0)
                goto error;
 
@@ -1020,15 +1018,12 @@ static void dice_bus_reset(struct fw_unit *unit)
        mutex_unlock(&dice->mutex);
 }
 
-#define TC_OUI         0x000166
 #define DICE_INTERFACE 0x000001
 
 static const struct ieee1394_device_id dice_id_table[] = {
        {
-               .match_flags  = IEEE1394_MATCH_SPECIFIER_ID |
-                               IEEE1394_MATCH_VERSION,
-               .specifier_id = TC_OUI,
-               .version      = DICE_INTERFACE,
+               .match_flags = IEEE1394_MATCH_VERSION,
+               .version     = DICE_INTERFACE,
        },
        { }
 };