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; }
int is_disk_name(const char *device_name){ if(get_device_type_by_device(device_name) != DEVICE_TYPE_DISK) return 0; if(isdigit(device_name[strlen(device_name)-1])) return 0; return 1; }
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); if(device_type == DEVICE_TYPE_UNKNOWN) return NULL; memset(buf, 0, buf_size); strcpy(buf, "1-1"); return buf; }
int mdev_sg_main(int argc, char **argv) { int isLock; const char *device_name, *action; if(argc < 3){ printf("Usage: %s [device_name] [action]\n", argv[0]); return 0; } device_name = argv[1]; action = argv[2]; usb_dbg("(%s): action=%s.\n", device_name, action); if(get_device_type_by_device(device_name) != DEVICE_TYPE_SG) return 0; // If remove the device? if(!check_hotplug_action(action)){ usb_dbg("(%s): Remove sg device.\n", device_name); return 0; } // Check Lock. if((isLock = file_lock((char *)device_name)) == -1){ usb_dbg("(%s): Can't set the file lock!\n", device_name); return 0; } if (nvram_get_int("modem_zcd") != 0) { if (module_smart_load("sr_mod")) sleep(1); } else { char vid[8] = {0}, pid[8] = {0}; char usb_port_id[64] = {0}; if (get_usb_port_by_device(device_name, usb_port_id, sizeof(usb_port_id))) { if (get_usb_vid(usb_port_id, vid, sizeof(vid)) && get_usb_pid(usb_port_id, pid, sizeof(pid))) perform_usb_modeswitch(vid, pid); } } usb_dbg("(%s): Success!\n", device_name); file_unlock(isLock); return 1; }
int mdev_lp_main(int argc, char **argv) { int isLock; const char *device_name, *action; if(argc != 3){ printf("Usage: %s [device_name] [action]\n", argv[0]); return 0; } device_name = argv[1]; action = argv[2]; usb_dbg("(%s): action=%s.\n", device_name, action); if(get_device_type_by_device(device_name) != DEVICE_TYPE_PRINTER){ usb_dbg("(%s): The device is not a printer.\n", device_name); return 0; } // Check Lock. if((isLock = file_lock((char *)device_name)) == -1){ usb_dbg("(%s): Can't set the file lock!\n", device_name); return 0; } // If remove the device? if(!check_hotplug_action(action)){ int ports_used_usblp = usb_port_module_used("usblp"); if (!ports_used_usblp) stop_usb_printer_spoolers(); file_unlock(isLock); return 0; } notify_rc("on_hotplug_usb_printer"); usb_dbg("(%s): Success!\n", device_name); file_unlock(isLock); return 1; }
int mdev_sr_main(int argc, char **argv) { int isLock; const char *device_name, *action; if(argc != 3){ printf("Usage: %s [device_name] [action]\n", argv[0]); return 0; } device_name = argv[1]; action = argv[2]; usb_dbg("(%s): action=%s.\n", device_name, action); if(get_device_type_by_device(device_name) != DEVICE_TYPE_CD){ usb_dbg("(%s): The device is not a CD one.\n", device_name); return 0; } // If remove the device? if(!check_hotplug_action(action)){ usb_dbg("(%s): Remove CD device.\n", device_name); return 0; } // Check Lock. if((isLock = file_lock((char *)device_name)) == -1){ usb_dbg("(%s): Can't set the file lock!\n", device_name); return 0; } doSystem("eject -s /dev/%s", device_name); sleep(1); module_smart_unload("sr_mod", 1); usb_dbg("(%s): Success!\n", device_name); file_unlock(isLock); return 1; }
int is_partition_name(const char *device_name, u32 *partition_order){ int order; u32 partition_number; if(partition_order != NULL) *partition_order = 0; if(get_device_type_by_device(device_name) != DEVICE_TYPE_DISK) return 0; // get the partition number in the device_name order = (u32)strtol(device_name+3, NULL, 10); if(order <= 0 || order == LONG_MIN || order == LONG_MAX) return 0; if(!get_disk_partitionnumber(device_name, &partition_number, NULL)) return 0; if(partition_order != NULL) *partition_order = order; return 1; }
char *get_interface_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_interface_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; }
partition_info_t *create_partition(const char *device_name, partition_info_t **new_part_info) { partition_info_t *follow_part_info; u32 partition_order; u64 size_in_kilobytes = 0, total_kilobytes = 0, used_kilobytes = 0; char buf1[PATH_MAX], buf2[64], buf3[PATH_MAX]; // options of mount info needs more buffer size. int len; if(new_part_info == NULL) return NULL; *new_part_info = NULL; // initial value. if(device_name == NULL || get_device_type_by_device(device_name) != DEVICE_TYPE_DISK) return NULL; if(!is_disk_name(device_name) && !is_partition_name(device_name, &partition_order)) return NULL; if(initial_part_data(&follow_part_info) == NULL) return NULL; len = strlen(device_name); follow_part_info->device = (char *)malloc(len+1); if(!follow_part_info->device){ free_partition_data(&follow_part_info); return NULL; } strncpy(follow_part_info->device, device_name, len); follow_part_info->device[len] = 0; follow_part_info->partition_order = partition_order; if(read_mount_data(device_name, buf1, buf2, buf3)){ len = strlen(buf1); follow_part_info->mount_point = (char *)malloc(len+1); if(!follow_part_info->mount_point){ free_partition_data(&follow_part_info); return NULL; } strncpy(follow_part_info->mount_point, buf1, len); follow_part_info->mount_point[len] = 0; len = strlen(buf2); follow_part_info->file_system = (char *)malloc(len+1); if(!follow_part_info->file_system){ free_partition_data(&follow_part_info); return NULL; } strncpy(follow_part_info->file_system, buf2, len); follow_part_info->file_system[len] = 0; len = strlen(buf3); follow_part_info->permission = (char *)malloc(len+1); if(!follow_part_info->permission){ free_partition_data(&follow_part_info); return NULL; } strncpy(follow_part_info->permission, buf3, len); follow_part_info->permission[len] = 0; if(get_mount_size(follow_part_info->mount_point, &total_kilobytes, &used_kilobytes)){ follow_part_info->size_in_kilobytes = total_kilobytes; follow_part_info->used_kilobytes = used_kilobytes; } } else{ if(is_disk_name(device_name)){ // Disk free_partition_data(&follow_part_info); return NULL; } else{ len = strlen(PARTITION_TYPE_UNKNOWN); follow_part_info->file_system = (char *)malloc(len+1); if(!follow_part_info->file_system){ free_partition_data(&follow_part_info); return NULL; } strncpy(follow_part_info->file_system, PARTITION_TYPE_UNKNOWN, len); follow_part_info->file_system[len] = 0; get_partition_size(device_name, &size_in_kilobytes); follow_part_info->size_in_kilobytes = size_in_kilobytes; } } *new_part_info = follow_part_info; return *new_part_info; }
partition_info_t *create_partition(const char *device_name, partition_info_t **new_part_info){ partition_info_t *follow_part_info; char label[128]; u32 partition_order = 0; u64 size_in_kilobytes = 0, total_kilobytes = 0, used_kilobytes = 0; char buf1[PATH_MAX], buf2[64], buf3[PATH_MAX]; // options of mount info needs more buffer size. int len; if(new_part_info == NULL){ usb_dbg("Bad input!!\n"); return NULL; } *new_part_info = NULL; // initial value. if(device_name == NULL || get_device_type_by_device(device_name) != DEVICE_TYPE_DISK) return NULL; if(!is_disk_name(device_name) && !is_partition_name(device_name, &partition_order)) return NULL; if(initial_part_data(&follow_part_info) == NULL){ usb_dbg("No memory!!(follow_part_info)\n"); return NULL; } len = strlen(device_name); follow_part_info->device = (char *)malloc(len+1); if(follow_part_info->device == NULL){ usb_dbg("No memory!!(follow_part_info->device)\n"); free_partition_data(&follow_part_info); return NULL; } strncpy(follow_part_info->device, device_name, len); follow_part_info->device[len] = 0; if(find_partition_label(device_name, label)){ strntrim(label); len = strlen(label); follow_part_info->label = (char *)malloc(len+1); if(follow_part_info->label == NULL){ usb_dbg("No memory!!(follow_part_info->label)\n"); free_partition_data(&follow_part_info); return NULL; } strncpy(follow_part_info->label, label, len); follow_part_info->label[len] = 0; } follow_part_info->partition_order = partition_order; if(read_mount_data(device_name, buf1, PATH_MAX, buf2, 64, buf3, PATH_MAX)){ len = strlen(buf1); follow_part_info->mount_point = (char *)malloc(len+1); if(follow_part_info->mount_point == NULL){ usb_dbg("No memory!!(follow_part_info->mount_point)\n"); free_partition_data(&follow_part_info); return NULL; } strncpy(follow_part_info->mount_point, buf1, len); follow_part_info->mount_point[len] = 0; len = strlen(buf2); follow_part_info->file_system = (char *)malloc(len+1); if(follow_part_info->file_system == NULL){ usb_dbg("No memory!!(follow_part_info->file_system)\n"); free_partition_data(&follow_part_info); return NULL; } strncpy(follow_part_info->file_system, buf2, len); follow_part_info->file_system[len] = 0; len = strlen(buf3); follow_part_info->permission = (char *)malloc(len+1); if(follow_part_info->permission == NULL){ usb_dbg("No memory!!(follow_part_info->permission)\n"); free_partition_data(&follow_part_info); return NULL; } strncpy(follow_part_info->permission, buf3, len); follow_part_info->permission[len] = 0; if(get_mount_size(follow_part_info->mount_point, &total_kilobytes, &used_kilobytes)){ follow_part_info->size_in_kilobytes = total_kilobytes; follow_part_info->used_kilobytes = used_kilobytes; } } else{ /*if(is_disk_name(device_name)){ // Disk free_partition_data(&follow_part_info); return NULL; } else{//*/ len = strlen(PARTITION_TYPE_UNKNOWN); follow_part_info->file_system = (char *)malloc(len+1); if(follow_part_info->file_system == NULL){ usb_dbg("No memory!!(follow_part_info->file_system)\n"); free_partition_data(&follow_part_info); return NULL; } strncpy(follow_part_info->file_system, PARTITION_TYPE_UNKNOWN, len); follow_part_info->file_system[len] = 0; get_partition_size(device_name, &size_in_kilobytes); follow_part_info->size_in_kilobytes = size_in_kilobytes; //} } *new_part_info = follow_part_info; return *new_part_info; }
int mdev_sd_main(int argc, char **argv) { char aidisk_cmd[64]; int isLock, mount_result; const char *device_name, *action; if(argc != 3){ printf("Usage: %s [device_name] [action]\n", argv[0]); return 0; } device_name = argv[1]; action = argv[2]; usb_dbg("(%s): action=%s.\n", device_name, action); if(get_device_type_by_device(device_name) != DEVICE_TYPE_DISK){ usb_dbg("(%s): The device is not a sd device.\n", device_name); return 0; } // Check Lock. if((isLock = file_lock((char *)device_name)) == -1){ usb_dbg("(%s): Can't set the file lock!\n", device_name); return 0; } // If remove the device? if(!check_hotplug_action(action)){ if (device_name[3] != '\0') detach_swap_partition((char*)device_name); else notify_rc("on_unplug_usb_storage"); file_unlock(isLock); return 0; } memset(aidisk_cmd, 0, sizeof(aidisk_cmd)); if (device_name[3] == '\0') // sda, sdb, sdc... { system("/sbin/hddtune.sh $MDEV"); if (!check_root_partition(device_name)) { sprintf(aidisk_cmd, "/sbin/automount.sh $MDEV AiDisk_%c%c", device_name[2], '1'); } else goto No_Need_To_Mount; } else { sprintf(aidisk_cmd, "/sbin/automount.sh $MDEV AiDisk_%c%c", device_name[2], device_name[3]); } umask(0000); mount_result = system(aidisk_cmd); if (mount_result == 0) { notify_rc("on_hotplug_usb_storage"); } usb_dbg("(%s): Success!\n", device_name); file_unlock(isLock); return 1; No_Need_To_Mount: usb_dbg("(%s): No need to mount!\n", device_name); file_unlock(isLock); return 0; }