libaudio-qsd8k: update a1026.h header and improve firmware-loading code

-- new a1026.h kernel header that contains just the user-space API and is
   sanitized through bionic/libc/kernel/tools/clean_header.py
-- AudioHardware.cpp:
	-- replace vr_mode with vr_mode_enabled, and use 0 and 1 instead of
	   A1026_VR_MODE_DISABLED/ENABLED, which is not defined in the kernel
	   header;
	-- in doA1026_init(), replace a fread() of 1 byte with a read that as
	   much as possible from the firmware file in as few as possible passes
	   before we call into the kernel
	-- Replace the size of the on-stack f/w buffer with A1026_MAX_FW_SIZE,
	   which now comes from the kernel header
	-- Fix up some signed-unsigned comparison warnings on mBluetoothIdTx

Signed-off-by: Iliyan Malchev <malchev@google.com>
diff --git a/libaudio-qsd8k/AudioHardware.cpp b/libaudio-qsd8k/AudioHardware.cpp
index b8d2596..473cf4d 100644
--- a/libaudio-qsd8k/AudioHardware.cpp
+++ b/libaudio-qsd8k/AudioHardware.cpp
@@ -71,7 +71,7 @@
 static int fd_fm_device = -1;
 static int stream_volume = -300;
 // use VR mode on inputs: 1 == VR mode enabled when selected, 0 = VR mode disabled when selected
-static int vr_mode = A1026_VR_MODE_DISABLED;
+static int vr_mode_enabled;
 static bool vr_mode_change = false;
 static int vr_uses_ns = 0;
 static int enable1026 = 1;
@@ -150,7 +150,7 @@
     }
 
     vr_mode_change = false;
-    vr_mode = A1026_VR_MODE_DISABLED;
+    vr_mode_enabled = 0;
     enable1026 = 1;
     char value[PROPERTY_VALUE_MAX];
     // Check the system property to enable or not the special recording modes
@@ -266,7 +266,7 @@
 {
     // VR mode is never used in a call and must be cleared when entering the IN_CALL mode
     if (mode == AudioSystem::MODE_IN_CALL) {
-        vr_mode = A1026_VR_MODE_DISABLED;
+        vr_mode_enabled = 0;
     }
 
     status_t status = AudioHardwareBase::setMode(mode);
@@ -690,41 +690,69 @@
 status_t AudioHardware::doA1026_init(void)
 {
     struct a1026img fwimg;
-    char *fn = NULL, *path = NULL;
     char char_tmp = 0;
-    int rc = 0, count = 0;
-    unsigned char local_vpimg_buf[32*1024];
-    FILE *fp;
+    unsigned char local_vpimg_buf[A1026_MAX_FW_SIZE], *ptr = local_vpimg_buf;
+    int rc = 0, fw_fd = -1;
+    ssize_t nr;
+    size_t remaining;
+    struct stat fw_stat;
 
-    fn = "/system/etc/vpimg";
-    path = "/dev/audience_a1026";
+    static const char *const fn = "/system/etc/vpimg";
+    static const char *const path = "/dev/audience_a1026";
+
+    if (fd_a1026 < 0)
+        fd_a1026 = open(path, O_RDWR | O_NONBLOCK, 0);
 
     if (fd_a1026 < 0) {
-        fd_a1026 = open(path, O_RDWR|O_NONBLOCK, 0);
-	if (fd_a1026 < 0) {
-		LOGE("Cannot open %s %d\n", path, fd_a1026);
-		support_a1026 = 0;
-		goto open_drv_err;
-	}
+        LOGE("Cannot open %s %d\n", path, fd_a1026);
+        support_a1026 = 0;
+        goto open_drv_err;
     }
 
-    fp = fopen(fn, "rb");
-    if (!fp) {
+    fw_fd = open(fn, O_RDONLY);
+    if (fw_fd < 0) {
         LOGE("Fail to open %s\n", fn);
         goto ld_img_error;
     } else LOGI("open %s success\n", fn);
 
-    memset(local_vpimg_buf, 0, sizeof(local_vpimg_buf));
-
-    while(!feof(fp)) {
-        fread(&char_tmp, sizeof(unsigned char), 1, fp);
-        local_vpimg_buf[count++] = char_tmp;
+    rc = fstat(fw_fd, &fw_stat);
+    if (rc < 0) {
+        LOGE("Cannot stat file %s: %s\n", fn, strerror(errno));
+        goto ld_img_error;
     }
-    fclose(fp);
 
-    LOGI("Total %d bytes put to user space buffer.\n", --count);
+    remaining = (int)fw_stat.st_size;
+
+    LOGI("Firmware %s size %d\n", fn, remaining);
+
+    if (remaining > sizeof(local_vpimg_buf)) {
+        LOGE("File %s size %d exceeds internal limit %d\n",
+             fn, remaining, sizeof(local_vpimg_buf));
+        goto ld_img_error;
+    }
+
+    while (remaining) {
+        nr = read(fw_fd, ptr, remaining);
+        if (nr < 0) {
+            LOGE("Error reading firmware: %s\n", strerror(errno));
+            goto ld_img_error;
+        }
+        else if (!nr) {
+            if (remaining)
+                LOGW("EOF reading firmware %s while %d bytes remain\n",
+                     fn, remaining);
+            break;
+        }
+        remaining -= nr;
+        ptr += nr;
+    }
+
+    close (fw_fd);
+    fw_fd = -1;
+
     fwimg.buf = local_vpimg_buf;
-    fwimg.img_size = count;
+    fwimg.img_size = (int)(fw_stat.st_size - remaining);
+    LOGI("Total %d bytes put to user space buffer.\n", fwimg.img_size);
 
     rc = ioctl(fd_a1026, A1026_BOOTUP_INIT, &fwimg);
     if (!rc) {
@@ -734,6 +762,8 @@
         LOGE("audience_a1026 init failed\n");
 
 ld_img_error:
+    if (fw_fd >= 0)
+        close(fw_fd);
     close(fd_a1026);
 open_drv_err:
     fd_a1026 = -1;
@@ -741,7 +771,6 @@
 }
 
 status_t AudioHardware::get_snd_dev(void)
-
 {
     Mutex::Autolock lock(mLock);
     return mCurSndDevice;
@@ -758,7 +787,7 @@
        return -1;
     }
     int rc = 0;
-    if (mBluetoothIdRx != -1) {
+    if (mBluetoothIdRx != -1UL) {
         id[0] = mBluetoothIdRx;
         id[1] = curr_out_device;
         LOGE("(mBluetoothIdRx, curr_out_device) = (%d, %d)", mBluetoothIdRx, curr_out_device);
@@ -771,7 +800,7 @@
            LOGD("update TX ACDB %d success", mBluetoothIdRx);
     }
 
-    if (mBluetoothIdTx != -1) {
+    if (mBluetoothIdTx != -1UL) {
         id[0] = mBluetoothIdTx;
         id[1] = curr_mic_device;
         LOGE("(mBluetoothIdTx, curr_out_device) = (%d, %d)", mBluetoothIdTx, curr_out_device);
@@ -851,7 +880,7 @@
             case SND_DEVICE_HANDSET:
             case SND_DEVICE_NO_MIC_HEADSET:
             case SND_DEVICE_SPEAKER:
-                if (vr_mode == A1026_VR_MODE_DISABLED) {
+                if (vr_mode_enabled == 0) {
                     acdb_id = ACDB_ID_INT_MIC_REC;
                 } else {
                     acdb_id = ACDB_ID_INT_MIC_VR;
@@ -971,7 +1000,7 @@
             // default output is speaker, recording from phone mic, user RECEIVER configuration
         case SND_DEVICE_HANDSET:
         case SND_DEVICE_NO_MIC_HEADSET:
-	        if (vr_mode == A1026_VR_MODE_ENABLED) {
+	        if (vr_mode_enabled) {
 	            if (vr_uses_ns) {
 	                new_pathid = A1026_PATH_VR_NS_RECEIVER;
 	                LOGV("A1026 control: new path is A1026_PATH_VR_NS_RECEIVER");
@@ -988,7 +1017,7 @@
         case SND_DEVICE_HEADSET_AND_SPEAKER:
         case SND_DEVICE_FM_HEADSET:
         case SND_DEVICE_FM_SPEAKER:
-	        if (vr_mode == A1026_VR_MODE_ENABLED) {
+	        if (vr_mode_enabled) {
 	            if (vr_uses_ns) {
 	                new_pathid = A1026_PATH_VR_NS_HEADSET;
 	                LOGV("A1026 control: new path is A1026_PATH_VR_NS_HEADSET");
@@ -1008,7 +1037,7 @@
 	        break;
         case SND_DEVICE_BT:
         case SND_DEVICE_BT_EC_OFF:
-	        if (vr_mode == A1026_VR_MODE_ENABLED) {
+	        if (vr_mode_enabled) {
 	            if (vr_uses_ns) {
 	                new_pathid = A1026_PATH_VR_NS_BT;
 	                LOGV("A1026 control: new path is A1026_PATH_VR_NS_BT");
@@ -1167,7 +1196,7 @@
     if ((vr_mode_change) || (sndDevice != -1 && sndDevice != mCurSndDevice)) {
         ret = doAudioRouteOrMute(sndDevice);
         mCurSndDevice = sndDevice;
-        if (mMode == AudioSystem::MODE_IN_CALL &&  mBluetoothIdTx != 0
+        if (mMode == AudioSystem::MODE_IN_CALL && mBluetoothIdTx != 0
                 && sndDevice == SND_DEVICE_BT) {
             ret = updateBT();
         } else if (!ret) ret = updateACDB();
@@ -1670,12 +1699,12 @@
 
     // reading voice recognition mode parameter
     if (param.getInt(key, enabled) == NO_ERROR) {
-        LOGV("set vr_mode to %d", enabled);
-        vr_mode_change = (vr_mode != enabled);
+        LOGV("set vr_mode_enabled to %d", enabled);
+        vr_mode_change = (vr_mode_enabled != enabled);
         if (enable1026) {
-            vr_mode = enabled;
+            vr_mode_enabled = enabled;
         } else {
-            vr_mode = A1026_VR_MODE_DISABLED;
+            vr_mode_enabled = 0;
         }
         param.remove(key);
     }
diff --git a/libaudio-qsd8k/a1026.h b/libaudio-qsd8k/a1026.h
index 32df1ce..cd6b6a6 100644
--- a/libaudio-qsd8k/a1026.h
+++ b/libaudio-qsd8k/a1026.h
@@ -1,239 +1,66 @@
-/* include/linux/a1026.h - a1026 voice processor driver
- *
- * Copyright (C) 2009 HTC Corporation.
- *
- * This software is licensed under the terms of the GNU General Public
- * License version 2, as published by the Free Software Foundation, and
- * may be copied, distributed, and modified under those terms.
- *
- * 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.
- *
- */
-
-#ifndef A1026_H
-#define A1026_H
+/****************************************************************************
+ ****************************************************************************
+ ***
+ ***   This header was automatically generated from a Linux kernel header
+ ***   of the same name, to make information necessary for userspace to
+ ***   call into the kernel available to libc.  It contains only constants,
+ ***   structures, and macros generated from the original header, and thus,
+ ***   contains no copyrightable information.
+ ***
+ ****************************************************************************
+ ****************************************************************************/
+#ifndef __LINUX_A1026_H
+#define __LINUX_A1026_H
 
 #include <linux/ioctl.h>
 
-/* A1026 Command codes */
-#define A100_msg_Sync                   0x8000
-#define CtrlMode_LAL                    0x0001 /* Level Active Low  */
-#define CtrlMode_LAH                    0x0002 /* Level Active High */
-#define CtrlMode_FE                     0x0003 /* Falling Edge */
-#define CtrlMode_RE                     0x0004 /* Rising  Edge */
-#define A100_msg_Sync_Ack               0x80000000
-
-#define A100_msg_Reset                  0x8002
-#define RESET_IMMEDIATE                 0x0000
-#define RESET_DELAYED                   0x0001
-
-#define A100_msg_BootloadInitiate       0x8003
-#define A100_msg_GetDeviceParm          0x800B
-#define A100_msg_SetDeviceParmID        0x800C
-#define A100_msg_SetDeviceParm          0x800D
-
-/* Get/Set PCM Device Parameter ID List */
-/* PCM-0 */
-#define PCM0WordLength                  0x0100
-#define PCM0DelFromFsTx                 0x0101
-#define PCM0DelFromFsRx                 0x0102
-#define PCM0LatchEdge                   0x0103
-#define PCM0Endianness                  0x0105
-#define PCM0TristateEnable              0x0107
-
-/* PCM-1 */
-#define PCM1WordLength                  0x0200
-#define PCM1DelFromFsTx                 0x0201
-#define PCM1DelFromFsRx                 0x0202
-#define PCM1LatchEdge                   0x0203
-#define PCM1Endianness                  0x0205
-#define PCM1TristateEnable              0x0207
-
-/* Possible setting values for PCM I/F */
-#define PCMWordLength_16bit             0x10 /* Default */
-#define PCMWordLength_24bit             0x18
-#define PCMWordLength_32bit             0x20
-#define PCMLatchEdge_Tx_F_Rx_R          0x00 /* Tx on Falling Edge, Rx on Rising Edge */
-#define PCMLatchEdge_Tx_R_Rx_F          0x03 /* Tx on Falling Edge, Rx on Rising Edge (Default) */
-#define PCMEndianness_Little            0x00
-#define PCMEndianness_Big               0x01 /* Default */
-#define PCMTristate_Disable             0x00 /* Default */
-#define PCMTristate_Enable              0x01
-
-/* Get/Set ADC Device Parameter ID List */
-/* ADC-0 */
-#define ADC0Gain                        0x0300
-#define ADC0Rate                        0x0301
-#define ADC0CutoffFreq                  0x0302
-
-/* ADC-1 */
-#define ADC1Gain                        0x0400
-#define ADC1Rate                        0x0401
-#define ADC1CutoffFreq                  0x0402
-
-/* Possible setting values for ADC I/F */
-#define ADC_Gain_0db                    0x00
-#define ADC_Gain_6db                    0x01
-#define ADC_Gain_12db                   0x02
-#define ADC_Gain_18db                   0x03
-#define ADC_Gain_24db                   0x04 /* Default */
-#define ADC_Gain_30db                   0x05
-#define ADC_Rate_8kHz                   0x00 /* Default */
-#define ADC_Rate_16kHz                  0x01
-#define ADC_CutoffFreq_NO_DC_Filter     0x00
-#define ADC_CutoffFreq_59p68Hz          0x01 /* Default */
-#define ADC_CutoffFreq_7p46Hz           0x02
-#define ADC_CutoffFreq_3p73Hz           0x03
-
-/* Set Power State */
-#define A100_msg_Sleep                  0x80100001
-
-/* Get/Set Algorithm Parameter command codes list */
-#define A100_msg_GetAlgorithmParm       0x8016
-#define A100_msg_SetAlgorithmParmID     0x8017
-#define A100_msg_SetAlgorithmParm       0x8018
-
-/* Get/Set Algorithm Parameter ID List (Transmit Feature) */
-#define AIS_Global_Supression_Level     0x0000
-#define Mic_Config                      0x0002
-#define AEC_Mode                        0x0003
-#define AEC_CNG                         0x0023
-#define Output_AGC                      0x0004
-#define Output_AGC_Target_Level         0x0005
-#define Output_AGC_Noise_Floor          0x0006
-#define Output_AGC_SNR_Improvement      0x0007
-#define Comfort_Noise                   0x001A
-#define Comfort_Noise_Level             0x001B
-
-/* Get/Set Algorithm Parameter ID List (Receive Feature) */
-#define Speaker_Volume                  0x0012
-#define VEQ_Mode                        0x0009
-#define VEQ_Max_FarEnd_Limiter_Level    0x000D
-#define VEQ_Noise_Estimation_Adj        0x0025
-#define Receive_NS                      0x000E
-#define Receive_NS_Level                0x000F
-#define SideTone                        0x0015
-#define SideTone_Gain                   0x0016
-
-/* Audio Path Commands */
-/* Get/Set Transmit Digital Input Gain */
-#define A100_msg_GetTxDigitalInputGain  0x801A
-#define A100_msg_SetTxDigitalInputGain  0x801B
-
-/* Get/Set Receive Digital Input Gain */
-#define A100_msg_GetRcvDigitalInputGain 0x8022
-#define A100_msg_SetRcvDigitalInputGain 0x8023
-
-/* Get/Set Transmit Digital Output Gain */
-#define A100_msg_GetTxDigitalOutputGain 0x801D
-#define A100_msg_SetTxDigitalOutputGain 0x8015
-
-/* Bypass */
-#define A100_msg_Bypass                 0x801C /* 0ff = 0x0000; on = 0x0001 (Default) */
-#define A1026_msg_VP_ON                 0x801C0001
-#define A1026_msg_VP_OFF                0x801C0000
-
-/* Diagnostic API Commands */
-#define A100_msg_GetMicRMS              0x8013
-#define A100_msg_GetMicPeak             0x8014
-#define DiagPath_Pri_Input_Mic          0x0000
-#define DiagPath_Sec_Input_Mic          0x0001
-#define DiagPath_Output_Mic             0x0002
-#define DiagPath_Far_End_Input          0x0003
-#define DiagPath_Far_End_Output         0x0004
-#define A100_msg_SwapInputCh            0x8019
-#define A100_msg_OutputKnownSig         0x801E
-
-#define A1026_msg_BOOT                  0x0001
-#define A1026_msg_BOOT_ACK              0x01
-
-
-/* general definitions */
-#define TIMEOUT                         20	// 20ms
-#define RETRY_CNT                       5
-#define POLLING_RETRY_CNT               3
-#define A1026_ERROR_CODE                0xffff
-#define A1026_SLEEP                     0
-#define A1026_ACTIVE                    1
-#define ERROR                           0xffffffff
-
-/* IOCTLs for Audience A1026 */
-#define A1026_IOCTL_MAGIC 'u'
-
-#define A1026_BOOTUP_INIT               _IOW(A1026_IOCTL_MAGIC, 0x01, unsigned)
-#define A1026_SET_CONFIG                _IOW(A1026_IOCTL_MAGIC, 0x02, unsigned)
-#define A1026_SET_NS_STATE              _IOW(A1026_IOCTL_MAGIC, 0x03, unsigned)
-
-/* For Diag */
-#define A1026_SET_MIC_ONOFF             _IOW(A1026_IOCTL_MAGIC, 0x50, unsigned)
-#define A1026_SET_MICSEL_ONOFF          _IOW(A1026_IOCTL_MAGIC, 0x51, unsigned)
-#define A1026_READ_DATA                 _IOR(A1026_IOCTL_MAGIC, 0x52, unsigned)
-#define A1026_WRITE_MSG                 _IOW(A1026_IOCTL_MAGIC, 0x53, unsigned)
-#define A1026_SYNC_CMD                  _IO(A1026_IOCTL_MAGIC, 0x54)
-#define A1026_SET_CMD_FILE              _IOW(A1026_IOCTL_MAGIC, 0x55, unsigned)
+#define A1026_MAX_FW_SIZE (32*1024)
+struct a1026img {
+ unsigned char *buf;
+ unsigned img_size;
+};
 
 enum A1026_PathID {
-        A1026_PATH_SUSPEND,
-        A1026_PATH_INCALL_RECEIVER,
-        A1026_PATH_INCALL_HEADSET,
-        A1026_PATH_INCALL_SPEAKER,
-        A1026_PATH_INCALL_BT,
-        A1026_PATH_VR_NO_NS_RECEIVER,
-        A1026_PATH_VR_NO_NS_HEADSET,
-        A1026_PATH_VR_NO_NS_SPEAKER,
-        A1026_PATH_VR_NO_NS_BT,
-        A1026_PATH_VR_NS_RECEIVER,
-        A1026_PATH_VR_NS_HEADSET,
-        A1026_PATH_VR_NS_SPEAKER,
-        A1026_PATH_VR_NS_BT,
-        A1026_PATH_RECORD_RECEIVER,
-        A1026_PATH_RECORD_HEADSET,
-        A1026_PATH_RECORD_SPEAKER,
-        A1026_PATH_RECORD_BT,
-        A1026_PATH_CAMCORDER
+ A1026_PATH_SUSPEND,
+ A1026_PATH_INCALL_RECEIVER,
+ A1026_PATH_INCALL_HEADSET,
+ A1026_PATH_INCALL_SPEAKER,
+ A1026_PATH_INCALL_BT,
+ A1026_PATH_VR_NO_NS_RECEIVER,
+ A1026_PATH_VR_NO_NS_HEADSET,
+ A1026_PATH_VR_NO_NS_SPEAKER,
+ A1026_PATH_VR_NO_NS_BT,
+ A1026_PATH_VR_NS_RECEIVER,
+ A1026_PATH_VR_NS_HEADSET,
+ A1026_PATH_VR_NS_SPEAKER,
+ A1026_PATH_VR_NS_BT,
+ A1026_PATH_RECORD_RECEIVER,
+ A1026_PATH_RECORD_HEADSET,
+ A1026_PATH_RECORD_SPEAKER,
+ A1026_PATH_RECORD_BT,
+ A1026_PATH_CAMCORDER
 };
 
-enum A1026_VR_Mode {
-        A1026_VR_MODE_DISABLED = 0,
-        A1026_VR_MODE_ENABLED = 1
-};
-
-/* noise suppression states */
 enum A1026_NS_states {
-	A1026_NS_STATE_AUTO,	/* leave mode as selected by driver  */
-	A1026_NS_STATE_OFF,	/* disable noise suppression */
-	A1026_NS_STATE_CT,	/* force close talk mode */
-	A1026_NS_STATE_FT,	/* force far talk mode */
-	A1026_NS_NUM_STATES
+ A1026_NS_STATE_AUTO,
+ A1026_NS_STATE_OFF,
+ A1026_NS_STATE_CT,
+ A1026_NS_STATE_FT,
+ A1026_NS_NUM_STATES
 };
 
-/* indicates if a1026_set_config() performs a full configuration or only
- * a voice processing algorithm configuration */
-enum A1026_config_mode {
-        A1026_CONFIG_FULL,
-        A1026_CONFIG_VP
-};
+#define A1026_IOCTL_MAGIC 'u'
 
-struct a1026_platform_data {
-        uint32_t gpio_a1026_micsel;
-        uint32_t gpio_a1026_wakeup;
-        uint32_t gpio_a1026_reset;
-        uint32_t gpio_a1026_int;
-        uint32_t gpio_a1026_clk;
-};
+#define A1026_BOOTUP_INIT _IOW(A1026_IOCTL_MAGIC, 0x01, struct a1026img *)
+#define A1026_SET_CONFIG _IOW(A1026_IOCTL_MAGIC, 0x02, enum A1026_PathID)
+#define A1026_SET_NS_STATE _IOW(A1026_IOCTL_MAGIC, 0x03, enum A1026_NS_states)
 
-struct a1026img {
-        unsigned char *buf;
-        int     img_size;
-};
-
-struct cmd_list {
-	unsigned int *p;
-	unsigned int cnt;
-};
+#define A1026_SET_MIC_ONOFF _IOW(A1026_IOCTL_MAGIC, 0x50, unsigned)
+#define A1026_SET_MICSEL_ONOFF _IOW(A1026_IOCTL_MAGIC, 0x51, unsigned)
+#define A1026_READ_DATA _IOR(A1026_IOCTL_MAGIC, 0x52, unsigned)
+#define A1026_WRITE_MSG _IOW(A1026_IOCTL_MAGIC, 0x53, unsigned)
+#define A1026_SYNC_CMD _IO(A1026_IOCTL_MAGIC, 0x54)
+#define A1026_SET_CMD_FILE _IOW(A1026_IOCTL_MAGIC, 0x55, unsigned)
 
 #endif