/*
 *  Write out firmware images for Cirrus Logic's Sound Fusion CS46XX
 *  based soundcards
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 * Modified on 2008-04-06 by Kalle Olavi Niemitalo.
 * Modified on 2009-12-17 by Geoff Simmons.
 */

#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/stat.h>

/* The following two #defines were copied from
 * linux-2.6.24/sound/pci/cs46xx/cs46xx_lib.h,
 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> */
/* 3*1024 parameter, 3.5*1024 sample, 2*3.5*1024 code */
#define BA1_DWORD_SIZE		(13 * 1024 + 512)
#define BA1_MEMORY_COUNT	3

typedef uint32_t u32;
#include "cs46xx_image.h"

static int write_image(const char *filename, const void *data, size_t len)
{
  FILE *file = fopen(filename, "wb");
  if (file == NULL) {
    perror(filename);
    return 1;
  }
  if (fwrite(data, len, 1, file) != 1) {
    perror(filename);
    fclose(file);
    remove(filename);
    return 1;
  }
  if (fclose(file) != 0) {
    perror(filename);
    remove(filename);
    return 1;
  }
  return 0;
}

int main(void)
{
  mkdir("cs46xx", S_IRWXU | S_IRWXG | S_IRWXO);
  if (write_image("cs46xx/cs46xx-old.fw", &BA1Struct, sizeof BA1Struct))
    return 1;
  return 0;
}
