static struct cyasblkdev_blk_data *cyasblkdev_blk_alloc(void)
{
	struct cyasblkdev_blk_data *bd;
	int ret = 0;
	cy_as_return_status_t stat = -1;
	int bus_num = 0;
	int total_media_count = 0;
	int devidx = 0;
	DBGPRN_FUNC_NAME;

	total_media_count = 0;
	devidx = find_first_zero_bit(dev_use, CYASBLKDEV_NUM_MINORS);
	if (devidx >= CYASBLKDEV_NUM_MINORS)
		return ERR_PTR(-ENOSPC);

	__set_bit(devidx, dev_use);
	__set_bit(devidx + 1, dev_use);

	bd = kzalloc(sizeof(struct cyasblkdev_blk_data), GFP_KERNEL);
	if (bd) {
		gl_bd = bd;

		spin_lock_init(&bd->lock);
		bd->usage = 1;

		/* setup the block_dev_ops pointer*/
		bd->blkops = &cyasblkdev_bdops;

		/* Get the device handle */
		bd->dev_handle = cyasdevice_getdevhandle();
		if (0 == bd->dev_handle) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message(
				"%s: get device failed\n", __func__);
			#endif
			ret = ENODEV;
			goto out;
		}

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s west bridge device handle:%x\n",
			__func__, (uint32_t)bd->dev_handle);
		#endif

		/* start the storage api and get a handle to the
		 * device we are interested in. */

		/* Error code to use if the conditions are not satisfied. */
		ret = ENOMEDIUM;

		stat = cy_as_misc_release_resource(bd->dev_handle, cy_as_bus_0);
		if ((stat != CY_AS_ERROR_SUCCESS) &&
		(stat != CY_AS_ERROR_RESOURCE_NOT_OWNED)) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message("%s: cannot release "
				"resource bus 0 - reason code %d\n",
				__func__, stat);
			#endif
		}

		stat = cy_as_misc_release_resource(bd->dev_handle, cy_as_bus_1);
		if ((stat != CY_AS_ERROR_SUCCESS) &&
		(stat != CY_AS_ERROR_RESOURCE_NOT_OWNED)) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message("%s: cannot release "
				"resource bus 0 - reason code %d\n",
				__func__, stat);
			#endif
		}

		/* start storage stack*/
		stat = cy_as_storage_start(bd->dev_handle, 0, 0x101);
		if (stat != CY_AS_ERROR_SUCCESS) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message("%s: cannot start storage "
				"stack - reason code %d\n", __func__, stat);
			#endif
			goto out;
		}

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s: storage started:%d ok\n",
			__func__, stat);
		#endif

		stat = cy_as_storage_register_callback(bd->dev_handle,
			cyasblkdev_storage_callback);
		if (stat != CY_AS_ERROR_SUCCESS) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message("%s: cannot register callback "
				"- reason code %d\n", __func__, stat);
			#endif
			goto out;
		}

		for (bus_num = 0; bus_num < 2; bus_num++) {
			stat = cy_as_storage_query_bus(bd->dev_handle,
				bus_num, &bd->media_count[bus_num],  0, 0);
			if (stat == CY_AS_ERROR_SUCCESS) {
				total_media_count = total_media_count +
					bd->media_count[bus_num];
			} else {
				#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message("%s: cannot query %d, "
					"reason code: %d\n",
					__func__, bus_num, stat);
				#endif
				goto out;
			}
		}

		if (total_media_count == 0) {
			#ifndef WESTBRIDGE_NDEBUG
			cy_as_hal_print_message(
				"%s: no storage media was found\n", __func__);
			#endif
			goto out;
		} else if (total_media_count >= 1) {
			if (bd->user_disk_0 == NULL) {

				bd->user_disk_0 =
					alloc_disk(8);
				if (bd->user_disk_0 == NULL) {
					kfree(bd);
					bd = ERR_PTR(-ENOMEM);
					return bd;
				}
			}
			#ifndef WESTBRIDGE_NDEBUG
			else {
				cy_as_hal_print_message("%s: no available "
					"gen_disk for disk 0, "
					"physically inconsistent\n", __func__);
			}
			#endif
		}

		if (total_media_count == 2) {
			if (bd->user_disk_1 == NULL) {
				bd->user_disk_1 =
					alloc_disk(8);
				if (bd->user_disk_1 == NULL) {
					kfree(bd);
					bd = ERR_PTR(-ENOMEM);
					return bd;
				}
			}
			#ifndef WESTBRIDGE_NDEBUG
			else {
				cy_as_hal_print_message("%s: no available "
					"gen_disk for media, "
					"physically inconsistent\n", __func__);
			}
			#endif
		}
		#ifndef WESTBRIDGE_NDEBUG
		else if (total_media_count > 2) {
			cy_as_hal_print_message("%s: count corrupted = 0x%d\n",
				__func__, total_media_count);
		}
		#endif

		#ifndef WESTBRIDGE_NDEBUG
		cy_as_hal_print_message("%s: %d device(s) found\n",
			__func__, total_media_count);
		#endif

		for (bus_num = 0; bus_num <= 1; bus_num++) {
			/*claim storage for cpu */
			stat = cy_as_storage_claim(bd->dev_handle,
				bus_num, 0, 0, 0);
			if (stat != CY_AS_ERROR_SUCCESS) {
				cy_as_hal_print_message("%s: cannot claim "
					"%d bus - reason code %d\n",
					__func__, bus_num, stat);
				goto out;
			}

			dev_data.bus = bus_num;
			dev_data.device = 0;

			stat = cy_as_storage_query_device(bd->dev_handle,
				&dev_data, 0, 0);
			if (stat == CY_AS_ERROR_SUCCESS) {
				cyasblkdev_add_disks(bus_num, bd,
					total_media_count, devidx);
			} else if (stat == CY_AS_ERROR_NO_SUCH_DEVICE) {
				#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message(
					"%s: no device on bus %d\n",
					__func__, bus_num);
				#endif
			} else {
				#ifndef WESTBRIDGE_NDEBUG
				cy_as_hal_print_message(
					"%s: cannot query %d device "
					"- reason code %d\n",
					__func__, bus_num, stat);
				#endif
				goto out;
			}
		} /* end for (bus_num = 0; bus_num <= 1; bus_num++)*/

		return bd;
	}
out:
	#ifndef WESTBRIDGE_NDEBUG
	cy_as_hal_print_message(
		"%s: bd failed to initialize\n", __func__);
	#endif

	kfree(bd);
	bd = ERR_PTR(-ret);
	return bd;
}
int cy_as_diagnostics(cy_as_diag_cmd_type mode, char *result)
{
	uint32_t retVal = 0;
	
#ifdef __CYAS_SYSFS_FOR_DIAGNOSTICS__
	cy_as_device_handle cyas_hd = cyasdevice_getdevhandle();

	switch( mode )
	{
		case CY_AS_DIAG_GET_VERSION:
			{
				cy_as_get_firmware_version_data ver_data = {0};
				const char *str = "" ;

				cyasdevice_leave_standby();

				retVal = cy_as_misc_get_firmware_version(cyas_hd, &ver_data, 0, 0) ;
				if (retVal != CY_AS_ERROR_SUCCESS) {
					cy_as_hal_print_message("cy_as_diagnostics: cannot get firmware version. reason code: %d\n", retVal) ;
					sprintf( result, "fail - %d", retVal );
					return retVal;
				}

				if ((ver_data.media_type & 0x01) && (ver_data.media_type & 0x06))
					str = "nand and SD/MMC." ;
				else if ((ver_data.media_type & 0x01) && (ver_data.media_type & 0x08))
					str = "nand and CEATA." ;
				else if (ver_data.media_type & 0x01)
					str = "nand." ;
				else if (ver_data.media_type & 0x08)
					str = "CEATA." ;
				else
					str = "SD/MMC." ;

				cyasdevice_enter_standby();
				
				cy_as_hal_print_message("<1> cy_as_device:_firmware version: %s "
										"major=%d minor=%d build=%d,\n_media types supported:%s\n",
										((ver_data.is_debug_mode) ? "debug" : "release"),
										ver_data.major, ver_data.minor, ver_data.build, str) ;
				
				sprintf( result, "%d.%d.%d", ver_data.major, ver_data.minor, ver_data.build );
			}
			break;
			
		case CY_AS_DIAG_DISABLE_MSM_SDIO:
			break;
			
		case CY_AS_DIAG_ENABLE_MSM_SDIO:
			break;

		case CY_AS_DIAG_ENTER_STANDBY:
			cyasdevice_enter_standby();
			break;
			
		case CY_AS_DIAG_LEAVE_STANDBY:
			cyasdevice_leave_standby();
			break;
			
		case CY_AS_DIAG_CREATE_BLKDEV:
			retVal = cyasblkdev_blk_init(0, 0);
			break;
			
		case CY_AS_DIAG_DESTROY_BLKDEV:
			cyasblkdev_blk_exit();
			break;

		case CY_AS_DIAG_SD_MOUNT:
			{
				int	i;
		        uint32_t count = 0 ;
		        int bus = 1 ;
				cy_as_storage_query_device_data dev_data ;
				cy_as_storage_query_unit_data unit_data = {0} ;
		
				cyasdevice_leave_standby();

				actdata = (uint8_t *)cy_as_hal_alloc(CYASSTORAGE_MAX_XFER_SIZE);
				expdata = (uint8_t *)cy_as_hal_alloc(CYASSTORAGE_MAX_XFER_SIZE);
		        cy_as_hal_mem_set(actdata, 0, CYASSTORAGE_MAX_XFER_SIZE);
		        cy_as_hal_mem_set(expdata, 0, CYASSTORAGE_MAX_XFER_SIZE);

		        retVal = cy_as_storage_device_control(cyas_hd, bus, 0, cy_true, cy_false, cy_as_storage_detect_SDAT_3, 0, 0) ;
		        if (retVal != CY_AS_ERROR_SUCCESS)
		        {
		                cy_as_hal_print_message(KERN_ERR"ERROR: Cannot set Device control - Reason code %d\n", retVal) ;
		                return retVal;
		        }

		        // Start the storage API and get a handle to the device we are interested in.
		        retVal = cy_as_storage_start(cyas_hd,0,0) ;
		        if (retVal != CY_AS_ERROR_SUCCESS)
		        {
		                cy_as_hal_print_message(KERN_ERR"ERROR: Cannot start storage stack - Reason code %d\n", retVal) ;
		                return retVal;
		        }

		        retVal = cy_as_storage_query_media(cyas_hd, cy_as_media_sd_flash, &count, 0, 0) ;
		        if (retVal != CY_AS_ERROR_SUCCESS)
		        {
		                cy_as_hal_print_message(KERN_ERR"ERROR: Cannot query SD device count - Reason code %d\n", retVal) ;
		                return retVal;
		        }
				if (!count)
		        {
		                cy_as_hal_print_message(KERN_ERR"ERROR: SD storage media was not found\n") ;
		                return retVal;
		        }
		        else
		        {
		                cy_as_hal_print_message(KERN_ERR"SUCCESS: %d SD device(s) found. SD_CLK, SD_CMD, and SD_D0 connected\n", count) ;


		                dev_data.bus = 1 ;
		                dev_data.device = 0 ;

		                retVal = cy_as_storage_query_device(cyas_hd, &dev_data, 0, 0 );
		                if(retVal != CY_AS_ERROR_SUCCESS)
		                {
		                        cy_as_hal_print_message(KERN_ERR"ERROR: Cannot query SD device count - Reason code %d\n", retVal) ;
		                        return retVal;
		                }
		                else
		                {
			 				#if 1 //skkm
							retVal = cy_as_storage_change_sd_frequency(cyas_hd, bus, 0x11, 24, 0, 0);
							if(retVal != CY_AS_ERROR_SUCCESS) {
								cy_as_hal_print_message("%s: Cannot control cy_as_storage_change_sd_frequency - reason [%d]\n", __func__, retVal) ;
							}
							#endif
							cy_as_hal_print_message(KERN_ERR"Checking for SD_WP Connectivity:\n");
							
							if(dev_data.desc_p.writeable)
							{
								cy_as_hal_print_message(KERN_ERR" SD media is not write protected \n") ;
							}
							else
							{
								cy_as_hal_print_message(KERN_ERR" SD media is write protected %d\n", retVal) ;
							}
							
							unit_data.device = 0 ;
							unit_data.unit   = 0 ;
							unit_data.bus    = bus;
							retVal = cy_as_storage_query_unit(cyas_hd,&unit_data, 0, 0) ;
							if (retVal != CY_AS_ERROR_SUCCESS) {
								#ifndef WESTBRIDGE_NDEBUG
								cy_as_hal_print_message(KERN_INFO"%s: cannot query %d device unit - reason code %d\n",
																	__func__, unit_data.bus, retVal) ;
								#endif
								return retVal;
							}

		                }
		        }
				
				cy_as_hal_set_ep_dma_mode(4, false);  
				cy_as_hal_set_ep_dma_mode(8, false); 

				start_unit = unit_data.desc_p.unit_size - MAX_DRQ_LOOPS_IN_ISR*2;
				
				for ( i = 0 ; i < CYASSTORAGE_MAX_XFER_SIZE ; i++ )
				{
					expdata[i] = i;
				}
			}
		break;
		
		case CY_AS_DIAG_SD_READ:
			{
				int i;
				int bus = 1 ;
				struct timespec mStartTime, mEndTime;
				long second,nano;
				long mDelta;


	    		mStartTime = CURRENT_TIME;
			    retVal = cy_as_storage_read(cyas_hd, bus, 0, 0, start_unit, actdata, MAX_DRQ_LOOPS_IN_ISR) ;
			    if (retVal != CY_AS_ERROR_SUCCESS)
			    {
			    	cy_as_hal_print_message(KERN_ERR "ERROR: cannot read from block device - code %d\n", retVal) ;
			    	break;
			    }

	    		mEndTime = CURRENT_TIME;
	        	second = mEndTime.tv_sec - mStartTime.tv_sec;
	        	nano = mEndTime.tv_nsec - mStartTime.tv_nsec;

				mDelta = (second*1000000) + nano/1000;
				
				cy_as_hal_print_message("<1>%s: reading speed = %d KByte/s\n", __func__, (int)((CYASSTORAGE_MAX_XFER_SIZE*1000)/mDelta) ) ;

			    if ( memcmp(expdata, actdata, 2048) != 0 )
			    {
				   int errCnt = 0 ;
		           cy_as_hal_print_message(KERN_ERR "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n") ;
					for ( i = 0 ; i < 2048 ; i++ )
					{
						if ( expdata[i] != actdata[i] )
						{
							cy_as_hal_print_message(KERN_ERR "EXP[%d]: 0x%02x\n",i, expdata[i]);
							cy_as_hal_print_message(KERN_ERR "ACT[%d]: 0x%02x\n",i, actdata[i]);
							errCnt++ ;
							if ( errCnt > 10 )
							{
								break;
							}
						}
					}
		           cy_as_hal_print_message(KERN_ERR "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n") ;
					retVal = CY_AS_ERROR_INVALID_RESPONSE;
					break;
			    }
				else
				{
					 cy_as_hal_print_message(KERN_ERR "success : storage test\n") ;
				}

			}
			break;
			
		case CY_AS_DIAG_SD_WRITE:
			{
				int	i;
				int bus = 1 ;
				struct timespec mStartTime, mEndTime;
				long second,nano;
				long mDelta;
				
				for ( i = 0 ; i < CYASSTORAGE_MAX_XFER_SIZE ; i++ )
				{
					expdata[i] = i;
				}

	    		mStartTime = CURRENT_TIME;
				
				retVal = cy_as_storage_write(cyas_hd, bus, 0, 0, start_unit, expdata, MAX_DRQ_LOOPS_IN_ISR) ;
		        if (retVal != CY_AS_ERROR_SUCCESS)
		        {
	                cy_as_hal_print_message(KERN_ERR "ERROR: cannot write to block device - code %d\n", retVal) ;
	                break;
		        }
				
	    		mEndTime = CURRENT_TIME;
	        	second = mEndTime.tv_sec - mStartTime.tv_sec;
	        	nano = mEndTime.tv_nsec - mStartTime.tv_nsec;

				mDelta = (second*1000000) + nano/1000;
				
				cy_as_hal_print_message("<1>%s: writing speed = %d KByte/s\n", __func__, (int)((CYASSTORAGE_MAX_XFER_SIZE*1000)/mDelta) ) ;
			}
			break;
			
		case CY_AS_DIAG_SD_UNMOUNT:
			cy_as_hal_set_ep_dma_mode(4, true);  
			cy_as_hal_set_ep_dma_mode(8, true); 

	        // Start the storage API and get a handle to the device we are interested in.
	        retVal = cy_as_storage_stop(cyas_hd,0,0) ;
	        if (retVal != CY_AS_ERROR_SUCCESS)
	        {
	                cy_as_hal_print_message(KERN_ERR"ERROR: Cannot stop storage stack - Reason code %d\n", retVal) ;
	                return retVal;
	        }

			cy_as_hal_free(actdata);
			cy_as_hal_free(expdata);
			
			cyasdevice_enter_standby();
			break;
#if 0
		case CY_AS_DIAG_CONNECT_UMS:
			cyasdevice_leave_standby();
			//cyasblkdev_blk_exit();

			cyasdevice_reload_firmware(0);
		
			//CyAsHalSelUSBSwitch(1);	
			retVal = CyAsAPIUsbInit();
			if (retVal)
			{
				cy_as_hal_print_message("%s: USB test failed.\n", __func__) ;
				return 0;	
			}
			else
			{
				msleep(1000);
				cy_as_hal_print_message("%s: USB connected.\n", __func__) ;
			}
			
			cy_as_hal_print_message("%s: UMS MODE init done\n", __func__) ;
		break;

	    case CY_AS_DIAG_DISCONNECT_UMS:

			CyAsAPIUsbExit();
			//CyAsHalSelUSBSwitch(0);	
			cy_as_hal_print_message("%s: UMS mode - close done\n", __func__) ;

			cyasdevice_reload_firmware(1);
			//cyasblkdev_blk_init(0, 0);
			cyasdevice_enter_standby();
			cy_as_hal_print_message("%s: reload F/W - close done\n", __func__) ;
		break;
		
	    case CY_AS_DIAG_CONNECT_MTP:
			cyasdevice_leave_standby();
			//CyAsHalSelUSBSwitch(1);	
			cyasblkdev_blk_init(0, 0);
			retVal = cy_as_gadget_init(1);
			if (retVal)
			{
				cy_as_hal_print_message("%s: cy_as_gadget_init failed.\n", __func__) ;
			}
			else
			{
				cy_as_hal_print_message("%s: cy_as_gadget_init success\n", __func__) ;
			}
			cy_as_hal_print_message("%s: Start cy_as_gadget driver -  init done\n", __func__) ;
		break;

	    case CY_AS_DIAG_DISCONNECT_MTP:
			cyasblkdev_blk_exit();
			cy_as_gadget_cleanup();
			//CyAsHalSelUSBSwitch(0);
			cyasdevice_enter_standby();
			cy_as_hal_print_message("%s: cy_as_gadget driver - close done\n", __func__) ;
		break;
#endif
		case CY_AS_DIAG_TEST_RESET_LOW:
			//cy_as_hal_set_reset_pin(0);
			cy_as_hal_print_message("%s: cy_as_hal_set_reset_pin - set LOW\n", __func__) ;
		break;

		case CY_AS_DIAG_TEST_RESET_HIGH:
			//cy_as_hal_set_reset_pin(1);
			cy_as_hal_print_message("%s: cy_as_hal_set_reset_pin - set HIGH\n", __func__) ;
		break;
		
		default:
			cy_as_hal_print_message("%s: unkown mode \n", __func__) ;
		break;

	}
#endif 
	return retVal;
}