12 #include <arpa/inet.h>
17 #include <sys/mount.h>
23 /* Adjust bdflush parameters.
24 * Do this here, because Tomato doesn't have the sysctl command.
25 * With these values, a disk block should be written to disk within 2 seconds.
28 void tune_bdflush(void)
30 f_write_string("/proc/sys/vm/dirty_expire_centisecs", "200", 0, 0);
31 f_write_string("/proc/sys/vm/dirty_writeback_centisecs", "200", 0, 0);
34 #include <sys/kdaemon.h>
35 #define SET_PARM(n) (n * 2 | 1)
36 void tune_bdflush(void)
38 bdflush(SET_PARM(5), 100);
39 bdflush(SET_PARM(6), 100);
40 bdflush(SET_PARM(8), 0);
44 #define USBCORE_MOD "usbcore"
45 #define USB20_MOD "ehci-hcd"
46 #define USBSTORAGE_MOD "usb-storage"
47 #define SCSI_MOD "scsi_mod"
48 #define SD_MOD "sd_mod"
50 #define USBOHCI_MOD "ohci-hcd"
51 #define USBUHCI_MOD "uhci-hcd"
52 #define USBPRINTER_MOD "usblp"
53 #define SCSI_WAIT_MOD "scsi_wait_scan"
56 #define USBOHCI_MOD "usb-ohci"
57 #define USBUHCI_MOD "usb-uhci"
58 #define USBPRINTER_MOD "printer"
59 #define USBFS "usbdevfs"
62 static int p9100d_sig(int sig
)
64 const char p910pid
[] = "/var/run/p9100d.pid";
68 if (f_read_string(p910pid
, s
, sizeof(s
)) > 0) {
69 if ((pid
= atoi(s
)) > 1) {
70 if (kill(pid
, sig
) == 0) {
87 _dprintf("%s\n", __FUNCTION__
);
90 if (nvram_get_int("usb_enable")) {
91 modprobe(USBCORE_MOD
);
93 /* mount usb device filesystem */
94 mount(USBFS
, "/proc/bus/usb", USBFS
, MS_MGC_VAL
, NULL
);
97 i
= do_led(LED_USB
, LED_PROBE
);
99 modprobe("ledtrig-usbdev");
100 modprobe("leds-usb");
101 sprintf(param
, "%d", i
);
102 f_write_string("/proc/leds-usb/gpio_pin", param
, 0, 0);
105 if (nvram_get_int("usb_storage")) {
106 /* insert scsi and storage modules before usb drivers */
109 modprobe(SCSI_WAIT_MOD
);
112 modprobe(USBSTORAGE_MOD
);
114 if (nvram_get_int("usb_fs_ext3")) {
116 modprobe("mbcache"); // used by ext2/ext3
118 /* insert ext3 first so that lazy mount tries ext3 before ext2 */
124 if (nvram_get_int("usb_fs_fat")) {
129 if (nvram_get_int("usb_fs_hfs")) {
133 if (nvram_get_int("usb_fs_hfsplus")) {
137 #if defined(LINUX26) && defined(TCONFIG_USB_EXTRAS)
138 if (nvram_get_int("usb_mmc") == 1) {
139 /* insert SD/MMC modules if present */
140 modprobe("mmc_core");
141 modprobe("mmc_block");
147 /* if enabled, force USB2 before USB1.1 */
148 if (nvram_get_int("usb_usb2") == 1) {
149 i
= nvram_get_int("usb_irq_thresh");
150 if ((i
< 0) || (i
> 6))
152 sprintf(param
, "log2_irq_thresh=%d", i
);
153 modprobe(USB20_MOD
, param
);
156 if (nvram_get_int("usb_uhci") == 1) {
157 modprobe(USBUHCI_MOD
);
160 if (nvram_get_int("usb_ohci") == 1) {
161 modprobe(USBOHCI_MOD
);
164 if (nvram_get_int("usb_printer")) {
165 symlink("/dev/usb", "/dev/printers");
166 modprobe(USBPRINTER_MOD
);
168 /* start printer server only if not already running */
169 if (p9100d_sig(0) != 0) {
171 nvram_get_int("usb_printer_bidirect") ? "-b" : "", //bidirectional
172 "-f", "/dev/usb/lp0", // device
179 if (nvram_get_int("idle_enable") == 1) {
188 int disabled
= !nvram_get_int("usb_enable");
190 // only find and kill the printer server we started (port 0)
192 modprobe_r(USBPRINTER_MOD
);
194 // only stop storage services if disabled
195 if (disabled
|| !nvram_get_int("usb_storage")) {
196 // Unmount all partitions
197 remove_storage_main(0);
199 // Stop storage services
204 modprobe_r("mbcache");
210 modprobe_r("hfsplus");
212 #ifdef TCONFIG_SAMBASRV
213 modprobe_r("nls_cp437");
214 modprobe_r("nls_cp850");
215 modprobe_r("nls_cp852");
216 modprobe_r("nls_cp866");
218 modprobe_r("nls_cp932");
219 modprobe_r("nls_cp936");
220 modprobe_r("nls_cp949");
221 modprobe_r("nls_cp950");
224 modprobe_r(USBSTORAGE_MOD
);
227 modprobe_r(SCSI_WAIT_MOD
);
229 modprobe_r(SCSI_MOD
);
232 #if defined(LINUX26) && defined(TCONFIG_USB_EXTRAS)
233 if (disabled
|| !nvram_get_int("usb_storage") || nvram_get_int("usb_mmc") != 1) {
235 modprobe_r("mmc_block");
236 modprobe_r("mmc_core");
240 if (disabled
|| nvram_get_int("usb_ohci") != 1) modprobe_r(USBOHCI_MOD
);
241 if (disabled
|| nvram_get_int("usb_uhci") != 1) modprobe_r(USBUHCI_MOD
);
242 if (disabled
|| nvram_get_int("usb_usb2") != 1) modprobe_r(USB20_MOD
);
245 modprobe_r("leds-usb");
246 modprobe_r("ledtrig-usbdev");
247 led(LED_USB
, LED_OFF
);
250 // only unload core modules if usb is disabled
252 umount("/proc/bus/usb"); // unmount usb device filesystem
253 modprobe_r(USBOHCI_MOD
);
254 modprobe_r(USBUHCI_MOD
);
255 modprobe_r(USB20_MOD
);
256 modprobe_r(USBCORE_MOD
);
260 if (nvram_get_int("idle_enable") == 0) {
261 killall("sd-idle", SIGTERM
);
264 if (nvram_match("3g_usb", "0") ) {
265 if (nvram_match("3g_module", "sierra") ) {
266 modprobe_r("sierra");
267 modprobe_r("usbserial");
269 if (nvram_match("3g_module", "option") ) {
270 modprobe_r("option");
271 modprobe_r("usbserial");
274 // when modem use usbserial module and we will try remove module, module will crash
275 // the only solution at the moment is rebbot router
282 #define MOUNT_VAL_FAIL 0
283 #define MOUNT_VAL_RONLY 1
284 #define MOUNT_VAL_RW 2
285 #define MOUNT_VAL_EXIST 3
287 int mount_r(char *mnt_dev
, char *mnt_dir
, char *type
)
295 if ((mnt
= findmntents(mnt_dev
, 0, NULL
, 0))) {
296 syslog(LOG_INFO
, "USB partition at %s already mounted on %s",
297 mnt_dev
, mnt
->mnt_dir
);
298 return MOUNT_VAL_EXIST
;
304 unsigned long flags
= MS_NOATIME
| MS_NODEV
;
306 if (strcmp(type
, "swap") == 0 || strcmp(type
, "mbr") == 0) {
307 /* not a mountable partition */
310 else if (strcmp(type
, "ext2") == 0 || strcmp(type
, "ext3") == 0) {
311 if (nvram_invmatch("usb_ext_opt", ""))
312 sprintf(options
, nvram_safe_get("usb_ext_opt"));
314 else if (strcmp(type
, "vfat") == 0) {
315 if (nvram_invmatch("smbd_cset", ""))
316 sprintf(options
, "iocharset=%s%s",
317 isdigit(nvram_get("smbd_cset")[0]) ? "cp" : "",
318 nvram_get("smbd_cset"));
319 if (nvram_invmatch("smbd_cpage", "")) {
320 char *cp
= nvram_safe_get("smbd_cpage");
321 sprintf(options
+ strlen(options
), ",codepage=%s" + (options
[0] ? 0 : 1), cp
);
322 sprintf(flagfn
, "nls_cp%s", cp
);
324 cp
= nvram_get("smbd_nlsmod");
325 if ((cp
) && (*cp
!= 0) && (strcmp(cp
, flagfn
) != 0))
329 nvram_set("smbd_nlsmod", flagfn
);
331 sprintf(options
+ strlen(options
), ",shortname=winnt" + (options
[0] ? 0 : 1));
333 sprintf(options
+ strlen(options
), ",flush" + (options
[0] ? 0 : 1));
335 if (nvram_invmatch("usb_fat_opt", ""))
336 sprintf(options
+ strlen(options
), "%s%s", options
[0] ? "," : "", nvram_safe_get("usb_fat_opt"));
338 else if (strncmp(type
, "ntfs", 4) == 0) {
339 if (nvram_invmatch("smbd_cset", ""))
340 sprintf(options
, "iocharset=%s%s",
341 isdigit(nvram_get("smbd_cset")[0]) ? "cp" : "",
342 nvram_get("smbd_cset"));
343 if (nvram_invmatch("usb_ntfs_opt", ""))
344 sprintf(options
+ strlen(options
), "%s%s", options
[0] ? "," : "", nvram_safe_get("usb_ntfs_opt"));
348 if ((dir_made
= mkdir_if_none(mnt_dir
))) {
349 /* Create the flag file for remove the directory on dismount. */
350 sprintf(flagfn
, "%s/.autocreated-dir", mnt_dir
);
351 f_write(flagfn
, NULL
, 0, 0, 0);
354 ret
= mount(mnt_dev
, mnt_dir
, type
, flags
, options
[0] ? options
: "");
356 /* try ntfs-3g in case it's installed */
357 if (ret
!= 0 && strncmp(type
, "ntfs", 4) == 0) {
358 sprintf(options
+ strlen(options
), ",noatime,nodev" + (options
[0] ? 0 : 1));
360 if (nvram_get_int("usb_fs_ntfs"))
362 ret
= eval("ntfs-3g", "-o", options
, mnt_dev
, mnt_dir
);
365 if (ret
!= 0 && strncmp(type
, "hfs", "") == 0) {
366 ret
= eval("mount", "-o", "noatime,nodev", mnt_dev
, mnt_dir
);
369 if (ret
!= 0 && strncmp(type
, "hfsplus", "") == 0) {
370 ret
= eval("mount", "-o", "noatime,nodev", mnt_dev
, mnt_dir
);
373 if (ret
!= 0) /* give it another try - guess fs */
374 ret
= eval("mount", "-o", "noatime,nodev", mnt_dev
, mnt_dir
);
377 syslog(LOG_INFO
, "USB %s%s fs at %s mounted on %s",
378 type
, (flags
& MS_RDONLY
) ? " (ro)" : "", mnt_dev
, mnt_dir
);
379 return (flags
& MS_RDONLY
) ? MOUNT_VAL_RONLY
: MOUNT_VAL_RW
;
388 return MOUNT_VAL_FAIL
;
392 struct mntent
*mount_fstab(char *dev_name
, char *type
, char *label
, char *uuid
)
394 struct mntent
*mnt
= NULL
;
396 if (eval("mount", "-a") == 0)
397 mnt
= findmntents(dev_name
, 0, NULL
, 0);
399 char spec
[PATH_MAX
+1];
401 if (label
&& *label
) {
402 sprintf(spec
, "LABEL=%s", label
);
403 if (eval("mount", spec
) == 0)
404 mnt
= findmntents(dev_name
, 0, NULL
, 0);
407 if (!mnt
&& uuid
&& *uuid
) {
408 sprintf(spec
, "UUID=%s", uuid
);
409 if (eval("mount", spec
) == 0)
410 mnt
= findmntents(dev_name
, 0, NULL
, 0);
414 if (eval("mount", dev_name
) == 0)
415 mnt
= findmntents(dev_name
, 0, NULL
, 0);
419 /* Still did not find what we are looking for, try absolute path */
420 if (realpath(dev_name
, spec
)) {
421 if (eval("mount", spec
) == 0)
422 mnt
= findmntents(dev_name
, 0, NULL
, 0);
428 syslog(LOG_INFO
, "USB %s fs at %s mounted on %s", type
, dev_name
, mnt
->mnt_dir
);
433 /* Check if the UFD is still connected because the links created in /dev/discs
434 * are not removed when the UFD is unplugged.
435 * The file to read is: /proc/scsi/usb-storage-#/#, where # is the host no.
436 * We are looking for "Attached: Yes".
438 static int usb_ufd_connected(int host_no
)
446 sprintf(proc_file
, "%s/%s-%d/%d", PROC_SCSI_ROOT
, USB_STORAGE
, host_no
, host_no
);
447 fp
= fopen(proc_file
, "r");
450 /* try the way it's implemented in newer kernels: /proc/scsi/usb-storage/[host] */
451 sprintf(proc_file
, "%s/%s/%d", PROC_SCSI_ROOT
, USB_STORAGE
, host_no
);
452 fp
= fopen(proc_file
, "r");
460 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
461 if (strstr(line
, "Attached: Yes")) {
475 #define MNT_DETACH 0x00000002 /* from linux/fs.h - just detach from the tree */
477 int umount_mountpoint(struct mntent
*mnt
, uint flags
);
478 int uswap_mountpoint(struct mntent
*mnt
, uint flags
);
480 /* Unmount this partition from all its mountpoints. Note that it may
481 * actually be mounted several times, either with different names or
482 * with "-o bind" flag.
483 * If the special flagfile is now revealed, delete it and [attempt to] delete
486 int umount_partition(char *dev_name
, int host_num
, char *dsc_name
, char *pt_name
, uint flags
)
488 sync(); /* This won't matter if the device is unplugged, though. */
490 if (flags
& EFH_HUNKNOWN
) {
491 /* EFH_HUNKNOWN flag is passed if the host was unknown.
492 * Only unmount disconnected drives in this case.
494 if (usb_ufd_connected(host_num
))
498 /* Find all the active swaps that are on this device and stop them. */
499 findmntents(dev_name
, 1, uswap_mountpoint
, flags
);
501 /* Find all the mountpoints that are for this device and unmount them. */
502 findmntents(dev_name
, 0, umount_mountpoint
, flags
);
506 int uswap_mountpoint(struct mntent
*mnt
, uint flags
)
508 swapoff(mnt
->mnt_fsname
);
512 int umount_mountpoint(struct mntent
*mnt
, uint flags
)
517 sprintf(flagfn
, "%s/.autocreated-dir", mnt
->mnt_dir
);
519 /* Run user pre-unmount scripts if any. It might be too late if
520 * the drive has been disconnected, but we'll try it anyway.
522 if (nvram_get_int("usb_automount"))
523 run_nvscript("script_usbumount", mnt
->mnt_dir
, 3);
524 /* Run *.autostop scripts located in the root of the partition being unmounted if any. */
525 run_userfile(mnt
->mnt_dir
, ".autostop", mnt
->mnt_dir
, 5);
526 run_nvscript("script_autostop", mnt
->mnt_dir
, 5);
529 while ((ret
= umount(mnt
->mnt_dir
)) && (count
< 2)) {
531 /* If we could not unmount the drive on the 1st try,
532 * kill all NAS applications so they are not keeping the device busy -
533 * unless it's an unmount request from the Web GUI.
535 if ((count
== 1) && ((flags
& EFH_USER
) == 0))
536 restart_nas_services(1, 0);
541 syslog(LOG_INFO
, "USB partition unmounted from %s", mnt
->mnt_dir
);
543 if (ret
&& ((flags
& EFH_SHUTDN
) != 0)) {
544 /* If system is stopping (not restarting), and we couldn't unmount the
545 * partition, try to remount it as read-only. Ignore the return code -
546 * we can still try to do a lazy unmount.
548 eval("mount", "-o", "remount,ro", mnt
->mnt_dir
);
551 if (ret
&& ((flags
& EFH_USER
) == 0)) {
552 /* Make one more try to do a lazy unmount unless it's an unmount
553 * request from the Web GUI.
554 * MNT_DETACH will expose the underlying mountpoint directory to all
555 * except whatever has cd'ed to the mountpoint (thereby making it busy).
556 * So the unmount can't actually fail. It disappears from the ken of
557 * everyone else immediately, and from the ken of whomever is keeping it
558 * busy when they move away from it. And then it disappears for real.
560 ret
= umount2(mnt
->mnt_dir
, MNT_DETACH
);
561 syslog(LOG_INFO
, "USB partition busy - will unmount ASAP from %s", mnt
->mnt_dir
);
565 if ((unlink(flagfn
) == 0)) {
566 // Only delete the directory if it was auto-created
574 /* Mount this partition on this disc.
575 * If the device is already mounted on any mountpoint, don't mount it again.
576 * If this is a swap partition, try swapon -a.
577 * If this is a regular partition, try mount -a.
579 * Before we mount any partitions:
580 * If the type is swap and /etc/fstab exists, do "swapon -a"
581 * If /etc/fstab exists, try mounting using fstab.
582 * We delay invoking mount because mount will probe all the partitions
583 * to read the labels, and we don't want it to do that early on.
584 * We don't invoke swapon until we actually find a swap partition.
586 * If the mount succeeds, execute the *.autorun scripts in the top
587 * directory of the newly mounted partition.
588 * Returns NZ for success, 0 if we did not mount anything.
590 int mount_partition(char *dev_name
, int host_num
, char *dsc_name
, char *pt_name
, uint flags
)
592 char the_label
[128], mountpoint
[128], uuid
[40];
595 static char *swp_argv
[] = { "swapon", "-a", NULL
};
598 if ((type
= find_label_or_uuid(dev_name
, the_label
, uuid
)) == NULL
)
601 if (f_exists("/etc/fstab")) {
602 if (strcmp(type
, "swap") == 0) {
603 _eval(swp_argv
, NULL
, 0, NULL
);
607 if (mount_r(dev_name
, NULL
, NULL
) == MOUNT_VAL_EXIST
)
610 if ((mnt
= mount_fstab(dev_name
, type
, the_label
, uuid
))) {
611 strcpy(mountpoint
, mnt
->mnt_dir
);
617 if (*the_label
!= 0) {
618 for (p
= the_label
; *p
; p
++) {
619 if (!isalnum(*p
) && !strchr("+-&.@", *p
))
622 sprintf(mountpoint
, "%s/%s", MOUNT_ROOT
, the_label
);
623 if ((ret
= mount_r(dev_name
, mountpoint
, type
)))
627 /* Can't mount to /mnt/LABEL, so try mounting to /mnt/discDN_PN */
628 sprintf(mountpoint
, "%s/%s", MOUNT_ROOT
, pt_name
);
629 ret
= mount_r(dev_name
, mountpoint
, type
);
631 if (ret
== MOUNT_VAL_RONLY
|| ret
== MOUNT_VAL_RW
)
633 /* Run user *.autorun and post-mount scripts if any. */
634 run_userfile(mountpoint
, ".autorun", mountpoint
, 3);
635 if (nvram_get_int("usb_automount"))
636 run_nvscript("script_usbmount", mountpoint
, 3);
638 return (ret
== MOUNT_VAL_RONLY
|| ret
== MOUNT_VAL_RW
);
645 * Finds SCSI Host number. Returns the host number >=0 if found, or (-1) otherwise.
646 * The name and host number of scsi block device in kernel 2.6 (for attached devices) can be found as
647 * /sys($DEVPATH)/host<host_no>/target<*>/<id>/block:[sda|sdb|...]
648 * where $DEVPATH is passed to hotplug events, and looks like
649 * /devices/pci0000:00/0000:00:04.1/usb1/1-1/1-1:1.2
651 * For printers this function finds a minor assigned to a printer
652 * /sys($DEVPATH)/usb:lp[0|1|2|...]
654 int find_dev_host(const char *devpath
)
659 int host
= -1; /* Scsi Host */
661 sprintf(buf
, "/sys%s", devpath
);
662 if ((usb_devpath
= opendir(buf
))) {
663 while ((dp
= readdir(usb_devpath
))) {
665 if (strncmp(dp
->d_name
, "host", 4) == 0) {
666 host
= strtol(dp
->d_name
+ 4, (char **)NULL
, 10);
672 else if (strncmp(dp
->d_name
, "usb:lp", 6) == 0) {
673 host
= strtol(dp
->d_name
+ 6, (char **)NULL
, 10);
682 closedir(usb_devpath
);
689 int dir_is_mountpoint(const char *root
, const char *dir
)
695 snprintf(path
, sizeof(path
), "%s%s%s", root
? : "", root
? "/" : "", dir
);
697 /* Check if this is a directory */
698 sb
.st_mode
= S_IFDIR
; /* failsafe */
701 if (S_ISDIR(sb
.st_mode
)) {
703 /* If this dir & its parent dir are on the same device, it is not a mountpoint */
708 ++sb
.st_dev
; /* failsafe */
711 return (thisdev
!= sb
.st_dev
);
717 /* Mount or unmount all partitions on this controller.
718 * Parameter: action_add:
720 * >0 = mount only if automount config option is enabled.
721 * <0 = mount regardless of config option.
723 void hotplug_usb_storage_device(int host_no
, int action_add
, uint flags
)
725 if (!nvram_get_int("usb_enable"))
727 _dprintf("%s: host %d action: %d\n", __FUNCTION__
, host_no
, action_add
);
730 if (nvram_get_int("usb_storage") && (nvram_get_int("usb_automount") || action_add
< 0)) {
731 /* Do not probe the device here. It's either initiated by user,
732 * or hotplug_usb() already did.
734 if (exec_for_host(host_no
, 0x00, flags
, mount_partition
)) {
735 restart_nas_services(0, 1); // restart all NAS applications
740 if (nvram_get_int("usb_storage") || ((flags
& EFH_USER
) == 0)) {
741 /* When unplugged, unmount the device even if
742 * usb storage is disabled in the GUI.
744 exec_for_host(host_no
, (flags
& EFH_USER
) ? 0x00 : 0x02, flags
, umount_partition
);
745 /* Restart NAS applications (they could be killed by umount_mountpoint),
746 * or just re-read the configuration.
748 restart_nas_services(0, 1);
754 /* This gets called at reboot or upgrade. The system is stopping. */
755 void remove_storage_main(int shutdn
)
758 restart_nas_services(1, 0);
759 /* Unmount all partitions */
760 exec_for_host(-1, 0x02, shutdn
? EFH_SHUTDN
: 0, umount_partition
);
765 * All the complex locking & checking code was removed when the kernel USB-storage
767 * The crash bug was with overlapped I/O to different USB drives, not specifically
768 * with mount processing.
770 * And for USB devices that are slow to come up. The kernel now waits until the
771 * USB drive has settled, and it correctly reads the partition table before calling
774 * The kernel patch was cleaning up data structures on an unplug. It
775 * needs to wait until the disk is unmounted. We have 20 seconds to do
780 static inline void usbled_proc(char *device
, int add
)
785 if (do_led(LED_USB
, LED_PROBE
) != 255) {
786 strncpy(param
, device
, sizeof(param
));
787 if ((p
= strchr(param
, ':')) != NULL
)
790 /* verify if we need to ignore this device (i.e. an internal SD/MMC slot ) */
791 p
= nvram_safe_get("usb_noled");
792 if (strcmp(p
, param
) == 0)
795 f_write_string(add
? "/proc/leds-usb/add" : "/proc/leds-usb/remove", param
, 0, 0);
800 /* Plugging or removing usb device
802 * On an occurrance, multiple hotplug events may be fired off.
803 * For example, if a hub is plugged or unplugged, an event
804 * will be generated for everything downstream of it, plus one for
805 * the hub itself. These are fired off simultaneously, not serially.
806 * This means that many many hotplug processes will be running at
809 * The hotplug event generated by the kernel gives us several pieces
811 * PRODUCT is vendorid/productid/rev#.
812 * DEVICE is /proc/bus/usb/bus#/dev#
813 * ACTION is add or remove
814 * SCSI_HOST is the host (controller) number (this relies on the custom kernel patch)
816 * Note that when we get a hotplug add event, the USB susbsystem may
817 * or may not have yet tried to read the partition table of the
818 * device. For a new controller that has never been seen before,
819 * generally yes. For a re-plug of a controller that has been seen
820 * before, generally no.
822 * On a remove, the partition info has not yet been expunged. The
823 * partitions show up as /dev/discs/disc#/part#, and /proc/partitions.
824 * It appears that doing a "stat" for a non-existant partition will
825 * causes the kernel to re-validate the device and update the
826 * partition table info. However, it won't re-validate if the disc is
827 * mounted--you'll get a "Device busy for revalidation (usage=%d)" in
830 * The $INTERFACE is "class/subclass/protocol"
831 * Some interesting classes:
834 * 3 = HID. 3/1/2 = mouse.
835 * 6 = still image (6/1/1 = Digital camera Camera)
837 * 255 = scanner (255/255/255)
840 * Hub seems to have no INTERFACE (null), and TYPE of "9/0/0"
841 * Flash disk seems to have INTERFACE of "8/6/80", and TYPE of "0/0/0"
843 * When a hub is unplugged, a hotplug event is generated for it and everything
844 * downstream from it. You cannot depend on getting these events in any
845 * particular order, since there will be many hotplug programs all fired off
846 * at almost the same time.
847 * On a remove, don't try to access the downstream devices right away, give the
848 * kernel time to finish cleaning up all the data structures, which will be
849 * in the process of being torn down.
851 * On the initial plugin, the first time the kernel usb-storage subsystem sees
852 * the host (identified by GUID), it automatically reads the partition table.
853 * On subsequent plugins, it does not.
855 * Special values for Web Administration to unmount or remount
856 * all partitions of the host:
857 * INTERFACE=TOMATO/...
859 * SCSI_HOST=<host_no>
860 * If host_no is negative, we unmount all partions of *all* hosts.
862 void hotplug_usb(void)
866 char *interface
= getenv("INTERFACE");
867 char *action
= getenv("ACTION");
868 char *product
= getenv("PRODUCT");
870 char *device
= getenv("DEVICENAME");
871 int is_block
= strcmp(getenv("SUBSYSTEM") ? : "", "block") == 0;
873 char *device
= getenv("DEVICE");
875 char *scsi_host
= getenv("SCSI_HOST");
877 _dprintf("%s hotplug INTERFACE=%s ACTION=%s PRODUCT=%s HOST=%s DEVICE=%s\n",
878 getenv("SUBSYSTEM") ? : "USB", interface
, action
, product
, scsi_host
, device
);
880 if (!nvram_get_int("usb_enable")) return;
882 if (!action
|| ((!interface
|| !product
) && !is_block
))
884 if (!interface
|| !action
|| !product
) /* Hubs bail out here. */
889 host
= atoi(scsi_host
);
891 if (!wait_action_idle(10)) return;
893 add
= (strcmp(action
, "add") == 0);
894 if (add
&& (strncmp(interface
? : "", "TOMATO/", 7) != 0)) {
896 if (!is_block
&& device
)
898 syslog(LOG_DEBUG
, "Attached USB device %s [INTERFACE=%s PRODUCT=%s]",
899 device
, interface
, product
);
901 /* To allow automount to be blocked on startup.
902 * In kernel 2.6 we still need to serialize mount/umount calls -
903 * so the lock is down below in the "block" hotplug processing.
905 file_unlock(file_lock("usb"));
909 if (strncmp(interface
? : "", "TOMATO/", 7) == 0) { /* web admin */
910 if (scsi_host
== NULL
)
911 host
= atoi(product
); // for backward compatibility
912 /* If host is negative, unmount all partitions of *all* hosts.
913 * If host == -1, execute "soft" unmount (do not kill NAS apps, no "lazy" umount).
914 * If host == -2, run "hard" unmount, as if the drive is unplugged.
915 * This feature can be used in custom scripts as following:
917 * # INTERFACE=TOMATO/1 ACTION=remove PRODUCT=-1 SCSI_HOST=-1 hotplug usb
919 * PRODUCT is required to pass the env variables verification.
921 /* Unmount or remount all partitions of the host. */
922 hotplug_usb_storage_device(host
< 0 ? -1 : host
, add
? -1 : 0,
923 host
== -2 ? 0 : EFH_USER
);
926 else if (is_block
&& strcmp(getenv("MAJOR") ? : "", "8") == 0 && strcmp(getenv("PHYSDEVBUS") ? : "", "scsi") == 0) {
931 sprintf(devname
, "/dev/%s", device
);
932 lock
= file_lock("usb");
934 if (nvram_get_int("usb_storage") && nvram_get_int("usb_automount")) {
935 int minor
= atoi(getenv("MINOR") ? : "0");
936 if ((minor
% 16) == 0 && !is_no_partition(device
)) {
937 /* This is a disc, and not a "no-partition" device,
938 * like APPLE iPOD shuffle. We can't mount it.
942 if (mount_partition(devname
, host
, NULL
, device
, EFH_HP_ADD
)) {
943 restart_nas_services(0, 1); // restart all NAS applications
948 /* When unplugged, unmount the device even if usb storage is disabled in the GUI */
949 umount_partition(devname
, host
, NULL
, device
, EFH_HP_REMOVE
);
950 /* Restart NAS applications (they could be killed by umount_mountpoint),
951 * or just re-read the configuration.
953 restart_nas_services(0, 1);
958 else if (strncmp(interface
? : "", "8/", 2) == 0) { /* usb storage */
960 usbled_proc(device
, add
);
962 run_nvscript("script_usbhotplug", NULL
, 2);
964 hotplug_usb_storage_device(host
, add
, (add
? EFH_HP_ADD
: EFH_HP_REMOVE
) | (host
< 0 ? EFH_HUNKNOWN
: 0));
967 else { /* It's some other type of USB device, not storage. */
969 if (is_block
) return;
972 if (strncmp(interface
? : "", "7/", 2) == 0) /* printer */
973 usbled_proc(device
, add
);
975 /* Do nothing. The user's hotplug script must do it all. */
976 run_nvscript("script_usbhotplug", NULL
, 2);