int fc8050_spi_dataread(HANDLE hDevice, u16 addr, u8 *data, u16 length)
{
	int res;
	u8 command = HPIC_READ | HPIC_BMODE | HPIC_ENDIAN;

	mutex_lock(&lock);
	res  = spi_bulkwrite(hDevice, BBM_COMMAND_REG, &command, 1);
	res |= spi_bulkwrite(hDevice, BBM_ADDRESS_REG, (u8 *)&addr, 2);
	res |= spi_dataread(hDevice, BBM_DATA_REG, data, length);
	mutex_unlock(&lock);

	return res;
}
int fc8050_spi_longwrite(HANDLE hDevice, u16 addr, u32 data)
{
	int res;
	u8 command = HPIC_WRITE | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;

	mutex_lock(&lock);
	res  = spi_bulkwrite(hDevice, BBM_COMMAND_REG, &command, 1);
	res |= spi_bulkwrite(hDevice, BBM_ADDRESS_REG, (u8 *)&addr, 2);
	res |= spi_bulkwrite(hDevice, BBM_DATA_REG, (u8 *)&data, 4);
	mutex_unlock(&lock);

	return res;
}
int fc8050_spi_bulkwrite(HANDLE hDevice, fci_u16 addr, fci_u8* data, fci_u16 length)
{
	int res;
	fci_u8 command = HPIC_WRITE | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;

	mutex_lock(&lock);

	res  = spi_bulkwrite(hDevice, BBM_COMMAND_REG, &command, 1);
	res |= spi_bulkwrite(hDevice, BBM_ADDRESS_REG, (fci_u8*)&addr, 2);
	res |= spi_bulkwrite(hDevice, BBM_DATA_REG, data, length);

	mutex_unlock(&lock);
	
	return res;
}
int fc8050_spi_longread(HANDLE hDevice, fci_u16 addr, fci_u32 *data)
{
	int res;
	fci_u8 command = HPIC_READ | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;

	mutex_lock(&lock);
	
	res  = spi_bulkwrite(hDevice, BBM_COMMAND_REG, &command, 1);
	res |= spi_bulkwrite(hDevice, BBM_ADDRESS_REG, (fci_u8*)&addr, 2);
	res |= spi_bulkread(hDevice, BBM_DATA_REG, (fci_u8*)data, 4);

	mutex_unlock(&lock);

	return res;
}
int fc8050_spi_wordwrite(HANDLE hDevice, u16 addr, u16 data)
{
	int res;
	u8 command = HPIC_WRITE | HPIC_AINC | HPIC_BMODE | HPIC_ENDIAN;

	if (BBM_SCI_DATA <= addr && BBM_SCI_SYNCRX >= addr)
		command = HPIC_WRITE | HPIC_WMODE | HPIC_ENDIAN;

	mutex_lock(&lock);
	res  = spi_bulkwrite(hDevice, BBM_COMMAND_REG, &command, 1);
	res |= spi_bulkwrite(hDevice, BBM_ADDRESS_REG, (u8 *)&addr, 2);
	res |= spi_bulkwrite(hDevice, BBM_DATA_REG, (u8 *)&data, 2);
	mutex_unlock(&lock);

	return res;
}
int fc8150_spib_bulkwrite(HANDLE hDevice, u16 addr, u8 *data, u16 length)
{
	int res;
	u8 command = SPI_WRITE | SPI_AINC;

	OAL_OBTAIN_SEMAPHORE();
	res = spi_bulkwrite(hDevice, addr, command, data, length);
	OAL_RELEASE_SEMAPHORE();
	return res;
}
int fc8150_spib_longwrite(HANDLE hDevice, u16 addr, u32 data)
{
	int res;
	u8 command = SPI_WRITE | SPI_AINC;

	OAL_OBTAIN_SEMAPHORE();
	res = spi_bulkwrite(hDevice, addr, command, (u8 *)&data, 4);
	OAL_RELEASE_SEMAPHORE();
	return res;
}
int fc8150_spib_bytewrite(HANDLE hDevice, u16 addr, u8 data)
{
	int res;
	u8 command = SPI_WRITE;

	OAL_OBTAIN_SEMAPHORE();
	res = spi_bulkwrite(hDevice, addr, command, (u8 *)&data, 1);
	OAL_RELEASE_SEMAPHORE();
	return res;
}