Include wl_high.ko module
[tomato.git] / release / src / router / rc / usb.c
blob642f553af3bd993a9831266fc243983b43891178
1 /*
3 USB Support
5 */
6 #include "rc.h"
8 #include <sys/types.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <arpa/inet.h>
13 #include <time.h>
14 #include <sys/time.h>
15 #include <errno.h>
17 #include <sys/mount.h>
18 #include <mntent.h>
19 #include <dirent.h>
20 #include <sys/file.h>
21 #include <sys/swap.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.
27 #ifdef LINUX26
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);
33 #else
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);
42 #endif // LINUX26
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"
49 #ifdef LINUX26
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"
54 #define USBFS "usbfs"
55 #else
56 #define USBOHCI_MOD "usb-ohci"
57 #define USBUHCI_MOD "usb-uhci"
58 #define USBPRINTER_MOD "printer"
59 #define USBFS "usbdevfs"
60 #endif
62 static int p9100d_sig(int sig)
64 const char p910pid[] = "/var/run/p9100d.pid";
65 char s[32];
66 int pid;
68 if (f_read_string(p910pid, s, sizeof(s)) > 0) {
69 if ((pid = atoi(s)) > 1) {
70 if (kill(pid, sig) == 0) {
71 if (sig == SIGTERM) {
72 sleep(1);
73 unlink(p910pid);
75 return 0;
79 return -1;
82 void start_usb(void)
84 char param[32];
85 int i;
87 if (nvram_match("boardtype", "0x052b")) { // Netgear WNR3500L v2 - initialize USB port
88 xstart("gpio", "enable", "20");
91 _dprintf("%s\n", __FUNCTION__);
92 tune_bdflush();
94 if (nvram_get_int("usb_enable")) {
95 modprobe(USBCORE_MOD);
97 /* mount usb device filesystem */
98 mount(USBFS, "/proc/bus/usb", USBFS, MS_MGC_VAL, NULL);
100 #ifdef LINUX26
101 i = do_led(LED_USB, LED_PROBE);
102 if (i != 255) {
103 modprobe("ledtrig-usbdev");
104 modprobe("leds-usb");
105 sprintf(param, "%d", i);
106 f_write_string("/proc/leds-usb/gpio_pin", param, 0, 0);
108 #endif
110 #ifdef TCONFIG_USBAP
111 char instance[20];
112 sprintf(instance, "instance_base=1");
113 modprobe("wl_high", instance );
114 #endif
116 if (nvram_get_int("usb_storage")) {
117 /* insert scsi and storage modules before usb drivers */
118 modprobe(SCSI_MOD);
119 #ifdef LINUX26
120 modprobe(SCSI_WAIT_MOD);
121 #endif
122 modprobe(SD_MOD);
123 modprobe(USBSTORAGE_MOD);
125 if (nvram_get_int("usb_fs_ext3")) {
126 #ifdef LINUX26
127 modprobe("mbcache"); // used by ext2/ext3
128 #endif
129 /* insert ext3 first so that lazy mount tries ext3 before ext2 */
130 modprobe("jbd");
131 modprobe("ext3");
132 modprobe("ext2");
135 if (nvram_get_int("usb_fs_fat")) {
136 modprobe("fat");
137 modprobe("vfat");
139 #ifdef TCONFIG_HFS
140 if (nvram_get_int("usb_fs_hfs")) {
141 modprobe("hfs");
142 modprobe("hfsplus");
144 #endif
146 #if defined(LINUX26) && defined(TCONFIG_MICROSD)
147 if (nvram_get_int("usb_mmc") == 1) {
148 /* insert SD/MMC modules if present */
149 modprobe("mmc_core");
150 modprobe("mmc_block");
151 modprobe("sdhci");
153 #endif
156 /* if enabled, force USB2 before USB1.1 */
157 if (nvram_get_int("usb_usb2") == 1) {
158 i = nvram_get_int("usb_irq_thresh");
159 if ((i < 0) || (i > 6))
160 i = 0;
161 sprintf(param, "log2_irq_thresh=%d", i);
162 modprobe(USB20_MOD, param);
165 if (nvram_get_int("usb_uhci") == 1) {
166 modprobe(USBUHCI_MOD);
169 if (nvram_get_int("usb_ohci") == 1) {
170 modprobe(USBOHCI_MOD);
173 if (nvram_get_int("usb_printer")) {
174 symlink("/dev/usb", "/dev/printers");
175 modprobe(USBPRINTER_MOD);
177 /* start printer server only if not already running */
178 if (p9100d_sig(0) != 0) {
179 eval("p910nd",
180 nvram_get_int("usb_printer_bidirect") ? "-b" : "", //bidirectional
181 "-f", "/dev/usb/lp0", // device
182 "0" // listen port
186 #ifdef LINUX26
187 if (nvram_get_int("idle_enable") == 1) {
188 xstart( "sd-idle" );
190 #endif
192 #ifdef TCONFIG_UPS
193 start_ups();
194 #endif
199 void stop_usb(void)
201 int disabled = !nvram_get_int("usb_enable");
203 // only find and kill the printer server we started (port 0)
204 p9100d_sig(SIGTERM);
205 modprobe_r(USBPRINTER_MOD);
207 // only stop storage services if disabled
208 if (disabled || !nvram_get_int("usb_storage")) {
209 // Unmount all partitions
210 remove_storage_main(0);
212 // Stop storage services
213 modprobe_r("ext2");
214 modprobe_r("ext3");
215 modprobe_r("jbd");
216 #ifdef LINUX26
217 modprobe_r("mbcache");
218 #endif
219 modprobe_r("vfat");
220 modprobe_r("fat");
221 modprobe_r("fuse");
222 #ifdef TCONFIG_HFS
223 modprobe_r("hfs");
224 modprobe_r("hfsplus");
225 #endif
226 sleep(1);
228 #ifdef TCONFIG_SAMBASRV
229 modprobe_r("nls_cp437");
230 modprobe_r("nls_cp850");
231 modprobe_r("nls_cp852");
232 modprobe_r("nls_cp866");
233 #ifdef LINUX26
234 modprobe_r("nls_cp932");
235 modprobe_r("nls_cp936");
236 modprobe_r("nls_cp949");
237 modprobe_r("nls_cp950");
238 #endif
239 #endif
240 modprobe_r(USBSTORAGE_MOD);
241 modprobe_r(SD_MOD);
242 #ifdef LINUX26
243 modprobe_r(SCSI_WAIT_MOD);
244 #endif
245 modprobe_r(SCSI_MOD);
248 #if defined(LINUX26) && defined(TCONFIG_MICROSD)
249 if (disabled || !nvram_get_int("usb_storage") || nvram_get_int("usb_mmc") != 1) {
250 modprobe_r("sdhci");
251 modprobe_r("mmc_block");
252 modprobe_r("mmc_core");
254 #endif
256 if (disabled || nvram_get_int("usb_ohci") != 1) modprobe_r(USBOHCI_MOD);
257 if (disabled || nvram_get_int("usb_uhci") != 1) modprobe_r(USBUHCI_MOD);
258 if (disabled || nvram_get_int("usb_usb2") != 1) modprobe_r(USB20_MOD);
260 #ifdef LINUX26
261 modprobe_r("leds-usb");
262 modprobe_r("ledtrig-usbdev");
263 led(LED_USB, LED_OFF);
264 #endif
266 // only unload core modules if usb is disabled
267 if (disabled) {
268 umount("/proc/bus/usb"); // unmount usb device filesystem
269 modprobe_r(USBOHCI_MOD);
270 modprobe_r(USBUHCI_MOD);
271 modprobe_r(USB20_MOD);
272 modprobe_r(USBCORE_MOD);
275 #ifdef LINUX26
276 if (nvram_get_int("idle_enable") == 0) {
277 killall("sd-idle", SIGTERM);
280 #ifdef TCONFIG_UPS
281 stop_ups();
282 #endif
284 if (nvram_match("3g_usb", "0") ) {
285 if (nvram_match("3g_module", "sierra") ) {
286 modprobe_r("sierra");
287 modprobe_r("usbserial");
290 if (nvram_match("3g_module", "option") ) {
291 modprobe_r("option");
292 modprobe_r("usbserial");
295 // shibby
296 // when modem use usbserial module and we will try remove module, module will crash
297 // the only solution at the moment is reboot router
298 // FIX THIS
299 if (nvram_match("3g_module", "usbserial") ) {
300 modprobe_r("usbserial");
304 if (nvram_match("boardtype", "0x052b")) { // Netgear WNR3500L v2 - disable USB port
305 xstart("gpio", "disable", "20");
309 #endif
314 #define MOUNT_VAL_FAIL 0
315 #define MOUNT_VAL_RONLY 1
316 #define MOUNT_VAL_RW 2
317 #define MOUNT_VAL_EXIST 3
319 int mount_r(char *mnt_dev, char *mnt_dir, char *type)
321 struct mntent *mnt;
322 int ret;
323 char options[140];
324 char flagfn[128];
325 int dir_made;
327 if ((mnt = findmntents(mnt_dev, 0, NULL, 0))) {
328 syslog(LOG_INFO, "USB partition at %s already mounted on %s",
329 mnt_dev, mnt->mnt_dir);
330 return MOUNT_VAL_EXIST;
333 options[0] = 0;
335 if (type) {
336 unsigned long flags = MS_NOATIME | MS_NODEV;
338 if (strcmp(type, "swap") == 0 || strcmp(type, "mbr") == 0) {
339 /* not a mountable partition */
340 flags = 0;
342 else if (strcmp(type, "ext2") == 0 || strcmp(type, "ext3") == 0) {
343 if (nvram_invmatch("usb_ext_opt", ""))
344 sprintf(options, nvram_safe_get("usb_ext_opt"));
346 else if (strcmp(type, "vfat") == 0) {
347 if (nvram_invmatch("smbd_cset", ""))
348 sprintf(options, "iocharset=%s%s",
349 isdigit(nvram_get("smbd_cset")[0]) ? "cp" : "",
350 nvram_get("smbd_cset"));
351 if (nvram_invmatch("smbd_cpage", "")) {
352 char *cp = nvram_safe_get("smbd_cpage");
353 sprintf(options + strlen(options), ",codepage=%s" + (options[0] ? 0 : 1), cp);
354 sprintf(flagfn, "nls_cp%s", cp);
356 cp = nvram_get("smbd_nlsmod");
357 if ((cp) && (*cp != 0) && (strcmp(cp, flagfn) != 0))
358 modprobe_r(cp);
360 modprobe(flagfn);
361 nvram_set("smbd_nlsmod", flagfn);
363 sprintf(options + strlen(options), ",shortname=winnt" + (options[0] ? 0 : 1));
364 #ifdef LINUX26
365 sprintf(options + strlen(options), ",flush" + (options[0] ? 0 : 1));
366 #endif
367 if (nvram_invmatch("usb_fat_opt", ""))
368 sprintf(options + strlen(options), "%s%s", options[0] ? "," : "", nvram_safe_get("usb_fat_opt"));
370 else if (strncmp(type, "ntfs", 4) == 0) {
371 if (nvram_invmatch("smbd_cset", ""))
372 sprintf(options, "iocharset=%s%s",
373 isdigit(nvram_get("smbd_cset")[0]) ? "cp" : "",
374 nvram_get("smbd_cset"));
375 if (nvram_invmatch("usb_ntfs_opt", ""))
376 sprintf(options + strlen(options), "%s%s", options[0] ? "," : "", nvram_safe_get("usb_ntfs_opt"));
379 if (flags) {
380 if ((dir_made = mkdir_if_none(mnt_dir))) {
381 /* Create the flag file for remove the directory on dismount. */
382 sprintf(flagfn, "%s/.autocreated-dir", mnt_dir);
383 f_write(flagfn, NULL, 0, 0, 0);
386 ret = mount(mnt_dev, mnt_dir, type, flags, options[0] ? options : "");
388 /* try ntfs-3g in case it's installed */
389 if (ret != 0 && strncmp(type, "ntfs", 4) == 0) {
390 sprintf(options + strlen(options), ",noatime,nodev" + (options[0] ? 0 : 1));
391 #ifdef TCONFIG_NTFS
392 if (nvram_get_int("usb_fs_ntfs"))
393 #endif
394 ret = eval("ntfs-3g", "-o", options, mnt_dev, mnt_dir);
397 #ifdef TCONFIG_HFS
398 if (ret != 0 && strncmp(type, "hfs", "") == 0) {
399 ret = eval("mount", "-o", "noatime,nodev", mnt_dev, mnt_dir);
402 if (ret != 0 && strncmp(type, "hfsplus", "") == 0) {
403 ret = eval("mount", "-o", "noatime,nodev", mnt_dev, mnt_dir);
405 #endif
407 if (ret != 0) /* give it another try - guess fs */
408 ret = eval("mount", "-o", "noatime,nodev", mnt_dev, mnt_dir);
410 if (ret == 0) {
411 syslog(LOG_INFO, "USB %s%s fs at %s mounted on %s",
412 type, (flags & MS_RDONLY) ? " (ro)" : "", mnt_dev, mnt_dir);
413 return (flags & MS_RDONLY) ? MOUNT_VAL_RONLY : MOUNT_VAL_RW;
416 if (dir_made) {
417 unlink(flagfn);
418 rmdir(mnt_dir);
422 return MOUNT_VAL_FAIL;
426 struct mntent *mount_fstab(char *dev_name, char *type, char *label, char *uuid)
428 struct mntent *mnt = NULL;
429 #if 0
430 if (eval("mount", "-a") == 0)
431 mnt = findmntents(dev_name, 0, NULL, 0);
432 #else
433 char spec[PATH_MAX+1];
435 if (label && *label) {
436 sprintf(spec, "LABEL=%s", label);
437 if (eval("mount", spec) == 0)
438 mnt = findmntents(dev_name, 0, NULL, 0);
441 if (!mnt && uuid && *uuid) {
442 sprintf(spec, "UUID=%s", uuid);
443 if (eval("mount", spec) == 0)
444 mnt = findmntents(dev_name, 0, NULL, 0);
447 if (!mnt) {
448 if (eval("mount", dev_name) == 0)
449 mnt = findmntents(dev_name, 0, NULL, 0);
452 if (!mnt) {
453 /* Still did not find what we are looking for, try absolute path */
454 if (realpath(dev_name, spec)) {
455 if (eval("mount", spec) == 0)
456 mnt = findmntents(dev_name, 0, NULL, 0);
459 #endif
461 if (mnt)
462 syslog(LOG_INFO, "USB %s fs at %s mounted on %s", type, dev_name, mnt->mnt_dir);
463 return (mnt);
467 /* Check if the UFD is still connected because the links created in /dev/discs
468 * are not removed when the UFD is unplugged.
469 * The file to read is: /proc/scsi/usb-storage-#/#, where # is the host no.
470 * We are looking for "Attached: Yes".
472 static int usb_ufd_connected(int host_no)
474 char proc_file[128];
475 #ifndef LINUX26
476 char line[256];
477 #endif
478 FILE *fp;
480 sprintf(proc_file, "%s/%s-%d/%d", PROC_SCSI_ROOT, USB_STORAGE, host_no, host_no);
481 fp = fopen(proc_file, "r");
483 if (!fp) {
484 /* try the way it's implemented in newer kernels: /proc/scsi/usb-storage/[host] */
485 sprintf(proc_file, "%s/%s/%d", PROC_SCSI_ROOT, USB_STORAGE, host_no);
486 fp = fopen(proc_file, "r");
489 if (fp) {
490 #ifdef LINUX26
491 fclose(fp);
492 return 1;
493 #else
494 while (fgets(line, sizeof(line), fp) != NULL) {
495 if (strstr(line, "Attached: Yes")) {
496 fclose(fp);
497 return 1;
500 fclose(fp);
501 #endif
504 return 0;
508 #ifndef MNT_DETACH
509 #define MNT_DETACH 0x00000002 /* from linux/fs.h - just detach from the tree */
510 #endif
511 int umount_mountpoint(struct mntent *mnt, uint flags);
512 int uswap_mountpoint(struct mntent *mnt, uint flags);
514 /* Unmount this partition from all its mountpoints. Note that it may
515 * actually be mounted several times, either with different names or
516 * with "-o bind" flag.
517 * If the special flagfile is now revealed, delete it and [attempt to] delete
518 * the directory.
520 int umount_partition(char *dev_name, int host_num, char *dsc_name, char *pt_name, uint flags)
522 sync(); /* This won't matter if the device is unplugged, though. */
524 if (flags & EFH_HUNKNOWN) {
525 /* EFH_HUNKNOWN flag is passed if the host was unknown.
526 * Only unmount disconnected drives in this case.
528 if (usb_ufd_connected(host_num))
529 return 0;
532 /* Find all the active swaps that are on this device and stop them. */
533 findmntents(dev_name, 1, uswap_mountpoint, flags);
535 /* Find all the mountpoints that are for this device and unmount them. */
536 findmntents(dev_name, 0, umount_mountpoint, flags);
537 return 0;
540 int uswap_mountpoint(struct mntent *mnt, uint flags)
542 swapoff(mnt->mnt_fsname);
543 return 0;
546 int umount_mountpoint(struct mntent *mnt, uint flags)
548 int ret = 1, count;
549 char flagfn[128];
551 sprintf(flagfn, "%s/.autocreated-dir", mnt->mnt_dir);
553 /* Run user pre-unmount scripts if any. It might be too late if
554 * the drive has been disconnected, but we'll try it anyway.
556 if (nvram_get_int("usb_automount"))
557 run_nvscript("script_usbumount", mnt->mnt_dir, 3);
558 /* Run *.autostop scripts located in the root of the partition being unmounted if any. */
559 run_userfile(mnt->mnt_dir, ".autostop", mnt->mnt_dir, 5);
560 run_nvscript("script_autostop", mnt->mnt_dir, 5);
562 count = 0;
563 while ((ret = umount(mnt->mnt_dir)) && (count < 2)) {
564 count++;
565 /* If we could not unmount the drive on the 1st try,
566 * kill all NAS applications so they are not keeping the device busy -
567 * unless it's an unmount request from the Web GUI.
569 if ((count == 1) && ((flags & EFH_USER) == 0))
570 restart_nas_services(1, 0);
571 sleep(1);
574 if (ret == 0)
575 syslog(LOG_INFO, "USB partition unmounted from %s", mnt->mnt_dir);
577 if (ret && ((flags & EFH_SHUTDN) != 0)) {
578 /* If system is stopping (not restarting), and we couldn't unmount the
579 * partition, try to remount it as read-only. Ignore the return code -
580 * we can still try to do a lazy unmount.
582 eval("mount", "-o", "remount,ro", mnt->mnt_dir);
585 if (ret && ((flags & EFH_USER) == 0)) {
586 /* Make one more try to do a lazy unmount unless it's an unmount
587 * request from the Web GUI.
588 * MNT_DETACH will expose the underlying mountpoint directory to all
589 * except whatever has cd'ed to the mountpoint (thereby making it busy).
590 * So the unmount can't actually fail. It disappears from the ken of
591 * everyone else immediately, and from the ken of whomever is keeping it
592 * busy when they move away from it. And then it disappears for real.
594 ret = umount2(mnt->mnt_dir, MNT_DETACH);
595 syslog(LOG_INFO, "USB partition busy - will unmount ASAP from %s", mnt->mnt_dir);
598 if (ret == 0) {
599 if ((unlink(flagfn) == 0)) {
600 // Only delete the directory if it was auto-created
601 rmdir(mnt->mnt_dir);
604 return (ret == 0);
608 /* Mount this partition on this disc.
609 * If the device is already mounted on any mountpoint, don't mount it again.
610 * If this is a swap partition, try swapon -a.
611 * If this is a regular partition, try mount -a.
613 * Before we mount any partitions:
614 * If the type is swap and /etc/fstab exists, do "swapon -a"
615 * If /etc/fstab exists, try mounting using fstab.
616 * We delay invoking mount because mount will probe all the partitions
617 * to read the labels, and we don't want it to do that early on.
618 * We don't invoke swapon until we actually find a swap partition.
620 * If the mount succeeds, execute the *.autorun scripts in the top
621 * directory of the newly mounted partition.
622 * Returns NZ for success, 0 if we did not mount anything.
624 int mount_partition(char *dev_name, int host_num, char *dsc_name, char *pt_name, uint flags)
626 char the_label[128], mountpoint[128], uuid[40];
627 int ret;
628 char *type, *p;
629 static char *swp_argv[] = { "swapon", "-a", NULL };
630 struct mntent *mnt;
632 if ((type = find_label_or_uuid(dev_name, the_label, uuid)) == NULL)
633 return 0;
635 if (f_exists("/etc/fstab")) {
636 if (strcmp(type, "swap") == 0) {
637 _eval(swp_argv, NULL, 0, NULL);
638 return 0;
641 if (mount_r(dev_name, NULL, NULL) == MOUNT_VAL_EXIST)
642 return 0;
644 if ((mnt = mount_fstab(dev_name, type, the_label, uuid))) {
645 strcpy(mountpoint, mnt->mnt_dir);
646 ret = MOUNT_VAL_RW;
647 goto done;
651 if (*the_label != 0) {
652 for (p = the_label; *p; p++) {
653 if (!isalnum(*p) && !strchr("+-&.@", *p))
654 *p = '_';
656 sprintf(mountpoint, "%s/%s", MOUNT_ROOT, the_label);
657 if ((ret = mount_r(dev_name, mountpoint, type)))
658 goto done;
661 /* Can't mount to /mnt/LABEL, so try mounting to /mnt/discDN_PN */
662 sprintf(mountpoint, "%s/%s", MOUNT_ROOT, pt_name);
663 ret = mount_r(dev_name, mountpoint, type);
664 done:
665 if (ret == MOUNT_VAL_RONLY || ret == MOUNT_VAL_RW)
667 /* Run user *.autorun and post-mount scripts if any. */
668 run_userfile(mountpoint, ".autorun", mountpoint, 3);
669 if (nvram_get_int("usb_automount"))
670 run_nvscript("script_usbmount", mountpoint, 3);
672 return (ret == MOUNT_VAL_RONLY || ret == MOUNT_VAL_RW);
676 #if 0 /* LINUX26 */
679 * Finds SCSI Host number. Returns the host number >=0 if found, or (-1) otherwise.
680 * The name and host number of scsi block device in kernel 2.6 (for attached devices) can be found as
681 * /sys($DEVPATH)/host<host_no>/target<*>/<id>/block:[sda|sdb|...]
682 * where $DEVPATH is passed to hotplug events, and looks like
683 * /devices/pci0000:00/0000:00:04.1/usb1/1-1/1-1:1.2
685 * For printers this function finds a minor assigned to a printer
686 * /sys($DEVPATH)/usb:lp[0|1|2|...]
688 int find_dev_host(const char *devpath)
690 DIR *usb_devpath;
691 struct dirent *dp;
692 char buf[256];
693 int host = -1; /* Scsi Host */
695 sprintf(buf, "/sys%s", devpath);
696 if ((usb_devpath = opendir(buf))) {
697 while ((dp = readdir(usb_devpath))) {
698 errno = 0;
699 if (strncmp(dp->d_name, "host", 4) == 0) {
700 host = strtol(dp->d_name + 4, (char **)NULL, 10);
701 if (errno)
702 host = -1;
703 else
704 break;
706 else if (strncmp(dp->d_name, "usb:lp", 6) == 0) {
707 host = strtol(dp->d_name + 6, (char **)NULL, 10);
708 if (errno)
709 host = -1;
710 else
711 break;
713 else
714 continue;
716 closedir(usb_devpath);
718 return (host);
721 #endif /* LINUX26 */
723 int dir_is_mountpoint(const char *root, const char *dir)
725 char path[256];
726 struct stat sb;
727 int thisdev;
729 snprintf(path, sizeof(path), "%s%s%s", root ? : "", root ? "/" : "", dir);
731 /* Check if this is a directory */
732 sb.st_mode = S_IFDIR; /* failsafe */
733 stat(path, &sb);
735 if (S_ISDIR(sb.st_mode)) {
737 /* If this dir & its parent dir are on the same device, it is not a mountpoint */
738 strcat(path, "/.");
739 stat(path, &sb);
740 thisdev = sb.st_dev;
741 strcat(path, ".");
742 ++sb.st_dev; /* failsafe */
743 stat(path, &sb);
745 return (thisdev != sb.st_dev);
748 return 0;
751 /* Mount or unmount all partitions on this controller.
752 * Parameter: action_add:
753 * 0 = unmount
754 * >0 = mount only if automount config option is enabled.
755 * <0 = mount regardless of config option.
757 void hotplug_usb_storage_device(int host_no, int action_add, uint flags)
759 if (!nvram_get_int("usb_enable"))
760 return;
761 _dprintf("%s: host %d action: %d\n", __FUNCTION__, host_no, action_add);
763 if (action_add) {
764 if (nvram_get_int("usb_storage") && (nvram_get_int("usb_automount") || action_add < 0)) {
765 /* Do not probe the device here. It's either initiated by user,
766 * or hotplug_usb() already did.
768 if (exec_for_host(host_no, 0x00, flags, mount_partition)) {
769 restart_nas_services(0, 1); // restart all NAS applications
773 else {
774 if (nvram_get_int("usb_storage") || ((flags & EFH_USER) == 0)) {
775 /* When unplugged, unmount the device even if
776 * usb storage is disabled in the GUI.
778 exec_for_host(host_no, (flags & EFH_USER) ? 0x00 : 0x02, flags, umount_partition);
779 /* Restart NAS applications (they could be killed by umount_mountpoint),
780 * or just re-read the configuration.
782 restart_nas_services(0, 1);
788 /* This gets called at reboot or upgrade. The system is stopping. */
789 void remove_storage_main(int shutdn)
791 if (shutdn)
792 restart_nas_services(1, 0);
793 /* Unmount all partitions */
794 exec_for_host(-1, 0x02, shutdn ? EFH_SHUTDN : 0, umount_partition);
798 /*******
799 * All the complex locking & checking code was removed when the kernel USB-storage
800 * bugs were fixed.
801 * The crash bug was with overlapped I/O to different USB drives, not specifically
802 * with mount processing.
804 * And for USB devices that are slow to come up. The kernel now waits until the
805 * USB drive has settled, and it correctly reads the partition table before calling
806 * the hotplug agent.
808 * The kernel patch was cleaning up data structures on an unplug. It
809 * needs to wait until the disk is unmounted. We have 20 seconds to do
810 * the unmounts.
811 *******/
813 #ifdef LINUX26
814 static inline void usbled_proc(char *device, int add)
816 char *p;
817 char param[32];
819 if (do_led(LED_USB, LED_PROBE) != 255) {
820 strncpy(param, device, sizeof(param));
821 if ((p = strchr(param, ':')) != NULL)
822 *p = 0;
824 /* verify if we need to ignore this device (i.e. an internal SD/MMC slot ) */
825 p = nvram_safe_get("usb_noled");
826 if (strcmp(p, param) == 0)
827 return;
829 f_write_string(add ? "/proc/leds-usb/add" : "/proc/leds-usb/remove", param, 0, 0);
832 #endif
834 /* Plugging or removing usb device
836 * On an occurrance, multiple hotplug events may be fired off.
837 * For example, if a hub is plugged or unplugged, an event
838 * will be generated for everything downstream of it, plus one for
839 * the hub itself. These are fired off simultaneously, not serially.
840 * This means that many many hotplug processes will be running at
841 * the same time.
843 * The hotplug event generated by the kernel gives us several pieces
844 * of information:
845 * PRODUCT is vendorid/productid/rev#.
846 * DEVICE is /proc/bus/usb/bus#/dev#
847 * ACTION is add or remove
848 * SCSI_HOST is the host (controller) number (this relies on the custom kernel patch)
850 * Note that when we get a hotplug add event, the USB susbsystem may
851 * or may not have yet tried to read the partition table of the
852 * device. For a new controller that has never been seen before,
853 * generally yes. For a re-plug of a controller that has been seen
854 * before, generally no.
856 * On a remove, the partition info has not yet been expunged. The
857 * partitions show up as /dev/discs/disc#/part#, and /proc/partitions.
858 * It appears that doing a "stat" for a non-existant partition will
859 * causes the kernel to re-validate the device and update the
860 * partition table info. However, it won't re-validate if the disc is
861 * mounted--you'll get a "Device busy for revalidation (usage=%d)" in
862 * syslog.
864 * The $INTERFACE is "class/subclass/protocol"
865 * Some interesting classes:
866 * 8 = mass storage
867 * 7 = printer
868 * 3 = HID. 3/1/2 = mouse.
869 * 6 = still image (6/1/1 = Digital camera Camera)
870 * 9 = Hub
871 * 255 = scanner (255/255/255)
873 * Observed:
874 * Hub seems to have no INTERFACE (null), and TYPE of "9/0/0"
875 * Flash disk seems to have INTERFACE of "8/6/80", and TYPE of "0/0/0"
877 * When a hub is unplugged, a hotplug event is generated for it and everything
878 * downstream from it. You cannot depend on getting these events in any
879 * particular order, since there will be many hotplug programs all fired off
880 * at almost the same time.
881 * On a remove, don't try to access the downstream devices right away, give the
882 * kernel time to finish cleaning up all the data structures, which will be
883 * in the process of being torn down.
885 * On the initial plugin, the first time the kernel usb-storage subsystem sees
886 * the host (identified by GUID), it automatically reads the partition table.
887 * On subsequent plugins, it does not.
889 * Special values for Web Administration to unmount or remount
890 * all partitions of the host:
891 * INTERFACE=TOMATO/...
892 * ACTION=add/remove
893 * SCSI_HOST=<host_no>
894 * If host_no is negative, we unmount all partions of *all* hosts.
896 void hotplug_usb(void)
898 int add;
899 int host = -1;
900 char *interface = getenv("INTERFACE");
901 char *action = getenv("ACTION");
902 char *product = getenv("PRODUCT");
903 #ifdef LINUX26
904 char *device = getenv("DEVICENAME");
905 int is_block = strcmp(getenv("SUBSYSTEM") ? : "", "block") == 0;
906 #else
907 char *device = getenv("DEVICE");
908 #endif
909 char *scsi_host = getenv("SCSI_HOST");
911 _dprintf("%s hotplug INTERFACE=%s ACTION=%s PRODUCT=%s HOST=%s DEVICE=%s\n",
912 getenv("SUBSYSTEM") ? : "USB", interface, action, product, scsi_host, device);
914 if (!nvram_get_int("usb_enable")) return;
915 #ifdef LINUX26
916 if (!action || ((!interface || !product) && !is_block))
917 #else
918 if (!interface || !action || !product) /* Hubs bail out here. */
919 #endif
920 return;
922 if (scsi_host)
923 host = atoi(scsi_host);
925 if (!wait_action_idle(10)) return;
927 add = (strcmp(action, "add") == 0);
928 if (add && (strncmp(interface ? : "", "TOMATO/", 7) != 0)) {
929 #ifdef LINUX26
930 if (!is_block && device)
931 #endif
932 syslog(LOG_DEBUG, "Attached USB device %s [INTERFACE=%s PRODUCT=%s]",
933 device, interface, product);
934 #ifndef LINUX26
935 /* To allow automount to be blocked on startup.
936 * In kernel 2.6 we still need to serialize mount/umount calls -
937 * so the lock is down below in the "block" hotplug processing.
939 file_unlock(file_lock("usb"));
940 #endif
943 if (strncmp(interface ? : "", "TOMATO/", 7) == 0) { /* web admin */
944 if (scsi_host == NULL)
945 host = atoi(product); // for backward compatibility
946 /* If host is negative, unmount all partitions of *all* hosts.
947 * If host == -1, execute "soft" unmount (do not kill NAS apps, no "lazy" umount).
948 * If host == -2, run "hard" unmount, as if the drive is unplugged.
949 * This feature can be used in custom scripts as following:
951 * # INTERFACE=TOMATO/1 ACTION=remove PRODUCT=-1 SCSI_HOST=-1 hotplug usb
953 * PRODUCT is required to pass the env variables verification.
955 /* Unmount or remount all partitions of the host. */
956 hotplug_usb_storage_device(host < 0 ? -1 : host, add ? -1 : 0,
957 host == -2 ? 0 : EFH_USER);
959 #ifdef LINUX26
960 else if (is_block && strcmp(getenv("MAJOR") ? : "", "8") == 0 && strcmp(getenv("PHYSDEVBUS") ? : "", "scsi") == 0) {
961 /* scsi partition */
962 char devname[64];
963 int lock;
965 sprintf(devname, "/dev/%s", device);
966 lock = file_lock("usb");
967 if (add) {
968 if (nvram_get_int("usb_storage") && nvram_get_int("usb_automount")) {
969 int minor = atoi(getenv("MINOR") ? : "0");
970 if ((minor % 16) == 0 && !is_no_partition(device)) {
971 /* This is a disc, and not a "no-partition" device,
972 * like APPLE iPOD shuffle. We can't mount it.
974 return;
976 if (mount_partition(devname, host, NULL, device, EFH_HP_ADD)) {
977 restart_nas_services(0, 1); // restart all NAS applications
981 else {
982 /* When unplugged, unmount the device even if usb storage is disabled in the GUI */
983 umount_partition(devname, host, NULL, device, EFH_HP_REMOVE);
984 /* Restart NAS applications (they could be killed by umount_mountpoint),
985 * or just re-read the configuration.
987 restart_nas_services(0, 1);
989 file_unlock(lock);
991 #endif
992 else if (strncmp(interface ? : "", "8/", 2) == 0) { /* usb storage */
993 #ifdef LINUX26
994 usbled_proc(device, add);
995 #endif
996 run_nvscript("script_usbhotplug", NULL, 2);
997 #ifndef LINUX26
998 hotplug_usb_storage_device(host, add, (add ? EFH_HP_ADD : EFH_HP_REMOVE) | (host < 0 ? EFH_HUNKNOWN : 0));
999 #endif
1001 else { /* It's some other type of USB device, not storage. */
1002 #ifdef LINUX26
1003 if (is_block) return;
1004 #endif
1005 #ifdef LINUX26
1006 if (strncmp(interface ? : "", "7/", 2) == 0) /* printer */
1007 usbled_proc(device, add);
1008 #endif
1009 /* Do nothing. The user's hotplug script must do it all. */
1010 run_nvscript("script_usbhotplug", NULL, 2);