Ejemplo n.º 1
0
extern char *get_usb_port_by_device(const char *device_name, char *buf, const int buf_size){
	int device_type = get_device_type_by_device(device_name);
	char device_path[128], usb_path[PATH_MAX];
#ifdef RTCONFIG_USB
	char disk_name[4];
#endif

	if(device_type == DEVICE_TYPE_UNKNOWN)
		return NULL;

	memset(device_path, 0, 128);
	memset(usb_path, 0, PATH_MAX);

#ifdef RTCONFIG_USB
	if(device_type == DEVICE_TYPE_DISK){
		memset(disk_name, 0, 4);
		strncpy(disk_name, device_name, 3);
		sprintf(device_path, "%s/%s/device", SYS_BLOCK, disk_name);
		if(realpath(device_path, usb_path) == NULL){
			usb_dbg("(%s): Fail to get link: %s.\n", device_name, device_path);
			return NULL;
		}
	}
	else
#endif
		return NULL;

	if(get_usb_port_by_string(usb_path, buf, buf_size) == NULL){
		usb_dbg("(%s): Fail to get usb port: %s.\n", device_name, usb_path);
		return NULL;
	}

	return buf;
}
Ejemplo n.º 2
0
static FILE *open_usb_target(const char *usb_node, const char *target, const int wait)
{
	FILE *fp;
	char usb_port[32], target_file[128];
	int retry = wait;

	if(usb_node == NULL || target == NULL ||
	   get_usb_port_by_string(usb_node, usb_port, sizeof(usb_port)) == NULL)
		return NULL;

	snprintf(target_file, sizeof(target_file), "%s/%s/%s", USB_DEVICE_PATH, usb_node, target);

	while((fp = fopen(target_file, "r")) == NULL && retry-- > 0)
		sleep(1);

	return fp;
}
Ejemplo n.º 3
0
char *get_usb_node_by_string(const char *target_string, char *ret, const int ret_size)
{
	char usb_port[32], buf[16];
	char *ptr, *ptr2, *ptr3;
	int len;

	memset(usb_port, 0, sizeof(usb_port));
	if(get_usb_port_by_string(target_string, usb_port, sizeof(usb_port)) == NULL)
		return NULL;
	if((ptr = strstr(target_string, usb_port)) == NULL)
		return NULL;
	if(ptr != target_string)
		ptr += strlen(usb_port)+1;

	if((ptr2 = strchr(ptr, ':')) == NULL)
		return NULL;
	ptr3 = ptr2;
	*ptr3 = 0;

	if((ptr2 = strrchr(ptr, '/')) == NULL)
		ptr2 = ptr;
	else
		ptr = ptr2+1;

	len = strlen(ptr);
	if(len > 16)
		len = 16;

	memset(buf, 0, sizeof(buf));
	strncpy(buf, ptr, len);

	len = strlen(buf);
	if(len > ret_size)
		len = ret_size;

	memset(ret, 0, ret_size);
	strncpy(ret, buf, len);

	*ptr3 = ':';

	return ret;
}
Ejemplo n.º 4
0
char *get_usb_node_by_string(const char *target_string, char *ret, const int ret_size)
{
	char usb_port[8], buf[16];
	char *ptr, *ptr_end;
	int len;

	memset(usb_port, 0, 8);
	if(get_usb_port_by_string(target_string, usb_port, 8) == NULL)
		return NULL;

	if((ptr = strstr(target_string, usb_port)) == NULL)
		return NULL;
	if(ptr != target_string)
		ptr += strlen(usb_port)+1;

	if((ptr_end = strchr(ptr, ':')) == NULL)
		return NULL;

	len = strlen(ptr)-strlen(ptr_end);
	if(len > 16)
		len = 16;

	memset(buf, 0, 16);
	strncpy(buf, ptr, len);

	if((ptr = strrchr(buf, '/')) == NULL)
		ptr = buf;
	else
		++ptr;

	len = strlen(ptr);
	if(len > ret_size)
		len = ret_size;

	memset(ret, 0, ret_size);
	strncpy(ret, ptr, len);

	return ret;
}
Ejemplo n.º 5
0
char *get_path_by_node(const char *usb_node, char *buf, const int buf_size){
	char usb_port[32], *hub_path;
	int port_num = 0, len;

	if(usb_node == NULL || buf == NULL || buf_size <= 0)
		return NULL;

	// Get USB port.
	if(get_usb_port_by_string(usb_node, usb_port, sizeof(usb_port)) == NULL)
		return NULL;

	port_num = get_usb_port_number(usb_port);
	if(port_num == 0)
		return NULL;

	if(strlen(usb_node) > (len = strlen(usb_port))){
		hub_path = (char *)usb_node+len;
		snprintf(buf, buf_size, "%d%s", port_num, hub_path);
	}
	else
		snprintf(buf, buf_size, "%d", port_num);

	return buf;
}
Ejemplo n.º 6
0
int get_interface_Int_endpoint(const char *interface_name)
{
	FILE *fp;
	char interface_path[128], bmAttributes_file[128], buf[8], *ptr;
	DIR *interface_dir = NULL;
	struct dirent *end_name;
	int bNumEndpoints, end_count, got_Int = 0;

	if(interface_name == NULL || get_usb_port_by_string(interface_name, buf, sizeof(buf)) == NULL){
		usb_dbg("(%s): The device is not a interface.\n", interface_name);
		return 0;
	}

	memset(interface_path, 0, 128);
	sprintf(interface_path, "%s/%s", USB_DEVICE_PATH, interface_name);
	if((interface_dir = opendir(interface_path)) == NULL){
		usb_dbg("(%s): Fail to open dir: %s.\n", interface_name, interface_path);
		return 0;
	}

	// Get bNumEndpoints.
	bNumEndpoints = get_interface_numendpoints(interface_name);
	if(bNumEndpoints <= 0){
		usb_dbg("(%s): No endpoints: %d.\n", interface_name, bNumEndpoints);
		goto leave;
	}
	else if(bNumEndpoints == 1){ // ex: GL04P
		usb_dbg("(%s): It's a little impossible to be the control interface with a endpoint.\n", interface_name);
		goto leave;
	}

	end_count = 0;
	while((end_name = readdir(interface_dir)) != NULL){
		if(strncmp(end_name->d_name, "ep_", 3))
			continue;

		++end_count;

		memset(bmAttributes_file, 0, 128);
		sprintf(bmAttributes_file, "%s/%s/bmAttributes", interface_path, end_name->d_name);

		if((fp = fopen(bmAttributes_file, "r")) == NULL){
			usb_dbg("(%s): Fail to open file: %s.\n", interface_name, bmAttributes_file);
			continue;
		}

		memset(buf, 0, sizeof(buf));
		ptr = fgets(buf, sizeof(buf), fp);
		fclose(fp);
		if(ptr == NULL)
			goto leave;

		if(!strncmp(buf, "03", 2)){
			got_Int = 1;
			break;
		}
		else if(end_count == bNumEndpoints)
			break;
	}

leave:
	closedir(interface_dir);

	return got_Int;
}
Ejemplo n.º 7
0
char *get_usb_port_by_device(const char *device_name, char *buf, const int buf_size)
{
	int device_type = get_device_type_by_device(device_name);
	char device_path[128], usb_path[PATH_MAX];
	char disk_name[4];

	if(device_type == DEVICE_TYPE_UNKNOWN)
		return NULL;

	memset(device_path, 0, 128);
	memset(usb_path, 0, PATH_MAX);

#ifdef RTCONFIG_USB
	if(device_type == DEVICE_TYPE_DISK){
		memset(disk_name, 0, 4);
		strncpy(disk_name, device_name, 3);
		sprintf(device_path, "%s/%s/device", SYS_BLOCK, disk_name);
		if(realpath(device_path, usb_path) == NULL){
			usb_dbg("(%s): Fail to get link: %s.\n", device_name, device_path);
			return NULL;
		}
	}
	else
#endif
#ifdef RTCONFIG_USB_PRINTER
	if(device_type == DEVICE_TYPE_PRINTER){
		sprintf(device_path, "%s/%s/device", SYS_USB, device_name);
		if(realpath(device_path, usb_path) == NULL){
			usb_dbg("(%s): Fail to get link: %s.\n", device_name, device_path);
			return NULL;
		}
	}
	else
#endif
#ifdef RTCONFIG_USB_MODEM
	if(device_type == DEVICE_TYPE_SG){
		sprintf(device_path, "%s/%s/device", SYS_SG, device_name);
		if(realpath(device_path, usb_path) == NULL){
			usb_dbg("(%s): Fail to get link: %s.\n", device_name, device_path);
			return NULL;
		}
	}
	else
	if(device_type == DEVICE_TYPE_CD){
		sprintf(device_path, "%s/%s/device", SYS_BLOCK, device_name);
		if(realpath(device_path, usb_path) == NULL){
			usb_dbg("(%s): Fail to get link: %s.\n", device_name, device_path);
			return NULL;
		}
	}
	else
	if(device_type == DEVICE_TYPE_MODEM){
		sprintf(device_path, "%s/%s/device", SYS_TTY, device_name);
		if(realpath(device_path, usb_path) == NULL){
			sleep(1); // Sometimes link would be built slowly, so try again.

			if(realpath(device_path, usb_path) == NULL){
				usb_dbg("(%s)(2/2): Fail to get link: %s.\n", device_name, device_path);
				return NULL;
			}
		}
	}
	else
#endif
#ifdef RTCONFIG_USB_BECEEM
	if(device_type == DEVICE_TYPE_BECEEM){
		sprintf(device_path, "%s/%s/device", SYS_USB, device_name);
		if(realpath(device_path, usb_path) == NULL){
			if(realpath(device_path, usb_path) == NULL){
				usb_dbg("(%s)(2/2): Fail to get link: %s.\n", device_name, device_path);
				return NULL;
			}
		}
	}
	else
#endif
		return NULL;

	if(get_usb_port_by_string(usb_path, buf, buf_size) == NULL){
		usb_dbg("(%s): Fail to get usb port: %s.\n", device_name, usb_path);
		return NULL;
	}

	return buf;
}
Ejemplo n.º 8
0
disk_info_t *create_disk(const char *device_name, disk_info_t **new_disk_info){
	disk_info_t *follow_disk_info;
	u32 major, minor;
	u64 size_in_kilobytes = 0;
	int len;
	char usb_node[32], port_path[8];
	char buf[64], *port, *vendor = NULL, *model = NULL, *ptr;
	partition_info_t *new_partition_info, **follow_partition_list;

	if(new_disk_info == NULL){
		usb_dbg("Bad input!!\n");
		return NULL;
	}

	*new_disk_info = NULL; // initial value.

	if(device_name == NULL || !is_disk_name(device_name))
		return NULL;

	if(initial_disk_data(&follow_disk_info) == NULL){
		usb_dbg("No memory!!(follow_disk_info)\n");
		return NULL;
	}

	len = strlen(device_name);
	follow_disk_info->device = (char *)malloc(len+1);
	if(follow_disk_info->device == NULL){
		usb_dbg("No memory!!(follow_disk_info->device)\n");
		free_disk_data(&follow_disk_info);
		return NULL;
	}
	strcpy(follow_disk_info->device, device_name);
	follow_disk_info->device[len] = 0;

	if(!get_disk_major_minor(device_name, &major, &minor)){
		usb_dbg("Fail to get disk's major and minor: %s.\n", device_name);
		free_disk_data(&follow_disk_info);
		return NULL;
	}
	follow_disk_info->major = major;
	follow_disk_info->minor = minor;

	if(!get_disk_size(device_name, &size_in_kilobytes)){
		usb_dbg("Fail to get disk's size_in_kilobytes: %s.\n", device_name);
		free_disk_data(&follow_disk_info);
		return NULL;
	}
	follow_disk_info->size_in_kilobytes = size_in_kilobytes;

	if(!strncmp(device_name, "sd", 2)){
		// Get USB node.
		if(get_usb_node_by_device(device_name, usb_node, 32) == NULL){
			usb_dbg("(%s): Fail to get usb node.\n", device_name);
			free_disk_data(&follow_disk_info);
			return NULL;
		}

		if(get_path_by_node(usb_node, port_path, 8) == NULL){
			usb_dbg("(%s): Fail to get usb path.\n", usb_node);
			free_disk_data(&follow_disk_info);
			return NULL;
		}

		// Get USB port.
		if(get_usb_port_by_string(usb_node, buf, 64) == NULL){
			usb_dbg("Fail to get usb port: %s.\n", usb_node);
			free_disk_data(&follow_disk_info);
			return NULL;
		}

		len = strlen(buf);
		if(len > 0){
			port = (char *)malloc(8);
			if(port == NULL){
				usb_dbg("No memory!!(port)\n");
				free_disk_data(&follow_disk_info);
				return NULL;
			}
			memset(port, 0, 8);
			strncpy(port, port_path, 8);

			follow_disk_info->port = port;
		}

		// start get vendor.
		if(get_disk_vendor(device_name, buf, 64) == NULL){
			usb_dbg("Fail to get disk's vendor: %s.\n", device_name);
			free_disk_data(&follow_disk_info);
			return NULL;
		}

		len = strlen(buf);
		if(len > 0){
			vendor = (char *)malloc(len+1);
			if(vendor == NULL){
				usb_dbg("No memory!!(vendor)\n");
				free_disk_data(&follow_disk_info);
				return NULL;
			}
			strncpy(vendor, buf, len);
			vendor[len] = 0;
			strntrim(vendor);

			follow_disk_info->vendor = vendor;
		}

		// start get model.
		if(get_disk_model(device_name, buf, 64) == NULL){
			usb_dbg("Fail to get disk's model: %s.\n", device_name);
			free_disk_data(&follow_disk_info);
			return NULL;
		}

		len = strlen(buf);
		if(len > 0){
			model = (char *)malloc(len+1);
			if(model == NULL){
				usb_dbg("No memory!!(model)\n");
				free_disk_data(&follow_disk_info);
				return NULL;
			}
			strncpy(model, buf, len);
			model[len] = 0;
			strntrim(model);

			follow_disk_info->model = model;
		}

		// get USB's tag
		memset(buf, 0, 64);
		len = 0;
		ptr = buf;
		if(vendor != NULL){
			len += strlen(vendor);
			strcpy(ptr, vendor);
			ptr += len;
		}
		if(model != NULL){
			if(len > 0){
				++len; // Add a space between vendor and model.
				strcpy(ptr, " ");
				++ptr;
			}
			len += strlen(model);
			strcpy(ptr, model);
			ptr += len;
		}

		if(len > 0){
			follow_disk_info->tag = (char *)malloc(len+1);
			if(follow_disk_info->tag == NULL){
				usb_dbg("No memory!!(follow_disk_info->tag)\n");
				free_disk_data(&follow_disk_info);
				return NULL;
			}
			strcpy(follow_disk_info->tag, buf);
			follow_disk_info->tag[len] = 0;
		}
		else{
			len = strlen(DEFAULT_USB_TAG);

			follow_disk_info->tag = (char *)malloc(len+1);
			if(follow_disk_info->tag == NULL){
				usb_dbg("No memory!!(follow_disk_info->tag)\n");
				free_disk_data(&follow_disk_info);
				return NULL;
			}
			strcpy(follow_disk_info->tag, DEFAULT_USB_TAG);
			follow_disk_info->tag[len] = 0;
		}

		follow_partition_list = &(follow_disk_info->partitions);
		while(*follow_partition_list != NULL)
			follow_partition_list = &((*follow_partition_list)->next);

		new_partition_info = create_partition(device_name, follow_partition_list);
		if(new_partition_info != NULL){
			new_partition_info->disk = follow_disk_info;

			++(follow_disk_info->partition_number);
			++(follow_disk_info->mounted_number);
			if(!strcmp(new_partition_info->device, follow_disk_info->device))
				new_partition_info->size_in_kilobytes = follow_disk_info->size_in_kilobytes-4;
		}
	}

	if(!strcmp(follow_disk_info->device, follow_disk_info->partitions->device))
		get_disk_partitionnumber(device_name, &(follow_disk_info->partition_number), &(follow_disk_info->mounted_number));

	*new_disk_info = follow_disk_info;

	return *new_disk_info;
}