Fixed USB LED GPIO Writes
[tomato.git] / release / src / router / rc / usb.c
blob70e050dadfbdb3b2fa36342c7afdd08dff48a0b4
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 // Remove legacy approach in the code here - rather, use do_led() function, which is designed to do this
102 // The reason for changing this ... some HW (like Netgear WNDR4000) don't work with direct GPIO write -> use do_led()!
103 i = do_led(LED_USB, LED_PROBE);
104 if (i != 255) {
105 /*modprobe("ledtrig-usbdev");
106 modprobe("leds-usb");
107 sprintf(param, "%d", i);
108 f_write_string("/proc/leds-usb/gpio_pin", param, 0, 0);*/
109 do_led(LED_USB, LED_OFF);
111 #endif
112 #ifdef TCONFIG_USBAP
113 char instance[20];
114 /* From Asus QTD cache params */
115 char arg1[20] = {0};
116 char arg2[20] = {0};
117 char arg3[20] = {0};
118 char arg4[20] = {0};
119 char arg5[20] = {0};
120 char arg6[20] = {0};
121 char arg7[20] = {0};
122 /* Save QTD cache params in nvram */
123 sprintf(arg1, "log2_irq_thresh=%d", nvram_get_int("ehciirqt"));
124 sprintf(arg2, "qtdc_pid=%d", nvram_get_int("qtdc_pid"));
125 sprintf(arg3, "qtdc_vid=%d", nvram_get_int("qtdc_vid"));
126 sprintf(arg4, "qtdc0_ep=%d", nvram_get_int("qtdc0_ep"));
127 sprintf(arg5, "qtdc0_sz=%d", nvram_get_int("qtdc0_sz"));
128 sprintf(arg6, "qtdc1_ep=%d", nvram_get_int("qtdc1_ep"));
129 sprintf(arg7, "qtdc1_sz=%d", nvram_get_int("qtdc1_sz"));
131 modprobe("ehci-hcd", arg1, arg2, arg3, arg4, arg5, arg6, arg7);
133 sprintf(instance, "instance_base=1");
134 modprobe("wl_high", instance );
135 #endif
137 if (nvram_get_int("usb_storage")) {
138 /* insert scsi and storage modules before usb drivers */
139 modprobe(SCSI_MOD);
140 #ifdef LINUX26
141 modprobe(SCSI_WAIT_MOD);
142 #endif
143 modprobe(SD_MOD);
144 modprobe(USBSTORAGE_MOD);
146 if (nvram_get_int("usb_fs_ext3")) {
147 #ifdef LINUX26
148 modprobe("mbcache"); // used by ext2/ext3
149 #endif
150 /* insert ext3 first so that lazy mount tries ext3 before ext2 */
151 modprobe("jbd");
152 modprobe("ext3");
153 modprobe("ext2");
156 if (nvram_get_int("usb_fs_fat")) {
157 modprobe("fat");
158 modprobe("vfat");
161 #ifdef TCONFIG_UFSD
162 if (nvram_get_int("usb_fs_ntfs")) {
163 modprobe("ufsd");
165 #endif
167 #ifdef TCONFIG_HFS
168 if (nvram_get_int("usb_fs_hfs")) {
169 modprobe("hfs");
170 modprobe("hfsplus");
172 #endif
174 #if defined(LINUX26) && defined(TCONFIG_MICROSD)
175 if (nvram_get_int("usb_mmc") == 1) {
176 /* insert SD/MMC modules if present */
177 modprobe("mmc_core");
178 modprobe("mmc_block");
179 modprobe("sdhci");
181 #endif
184 /* if enabled, force USB2 before USB1.1 */
185 if (nvram_get_int("usb_usb2") == 1) {
186 i = nvram_get_int("usb_irq_thresh");
187 if ((i < 0) || (i > 6))
188 i = 0;
189 sprintf(param, "log2_irq_thresh=%d", i);
190 modprobe(USB20_MOD, param);
193 if (nvram_get_int("usb_uhci") == 1) {
194 modprobe(USBUHCI_MOD);
197 if (nvram_get_int("usb_ohci") == 1) {
198 modprobe(USBOHCI_MOD);
201 if (nvram_get_int("usb_printer")) {
202 symlink("/dev/usb", "/dev/printers");
203 modprobe(USBPRINTER_MOD);
205 /* start printer server only if not already running */
206 if (p9100d_sig(0) != 0) {
207 eval("p910nd",
208 nvram_get_int("usb_printer_bidirect") ? "-b" : "", //bidirectional
209 "-f", "/dev/usb/lp0", // device
210 "0" // listen port
214 #ifdef LINUX26
215 if (nvram_get_int("idle_enable") == 1) {
216 xstart( "sd-idle" );
218 #endif
220 #ifdef TCONFIG_UPS
221 start_ups();
222 #endif
224 #ifdef TCONFIG_USBAP
225 //enable eth2 after detect new iface by wl_high module
226 sleep(5);
227 xstart("service", "wireless", "restart");
228 #endif
230 // shibby
231 // If we want restore backup of webmon from USB device,
232 // we have to wait for mount USB devices by hotplug
233 // and then reboot firewall service (webmon iptables rules) one more time.
234 if( nvram_match( "log_wm", "1" ) && nvram_match( "webmon_bkp", "1" ) )
235 xstart( "service", "firewall", "restart" );
240 void stop_usb(void)
242 int disabled = !nvram_get_int("usb_enable");
244 // only find and kill the printer server we started (port 0)
245 p9100d_sig(SIGTERM);
246 modprobe_r(USBPRINTER_MOD);
248 // only stop storage services if disabled
249 if (disabled || !nvram_get_int("usb_storage")) {
250 // Unmount all partitions
251 remove_storage_main(0);
253 // Stop storage services
254 modprobe_r("ext2");
255 modprobe_r("ext3");
256 modprobe_r("jbd");
257 #ifdef LINUX26
258 modprobe_r("mbcache");
259 #endif
260 modprobe_r("vfat");
261 modprobe_r("fat");
262 modprobe_r("fuse");
263 #ifdef TCONFIG_UFSD
264 modprobe_r("ufsd");
265 #endif
266 #ifdef TCONFIG_HFS
267 modprobe_r("hfs");
268 modprobe_r("hfsplus");
269 #endif
270 sleep(1);
272 #ifdef TCONFIG_SAMBASRV
273 modprobe_r("nls_cp437");
274 modprobe_r("nls_cp850");
275 modprobe_r("nls_cp852");
276 modprobe_r("nls_cp866");
277 #ifdef LINUX26
278 modprobe_r("nls_cp932");
279 modprobe_r("nls_cp936");
280 modprobe_r("nls_cp949");
281 modprobe_r("nls_cp950");
282 #endif
283 #endif
284 modprobe_r(USBSTORAGE_MOD);
285 modprobe_r(SD_MOD);
286 #ifdef LINUX26
287 modprobe_r(SCSI_WAIT_MOD);
288 #endif
289 modprobe_r(SCSI_MOD);
292 #if defined(LINUX26) && defined(TCONFIG_MICROSD)
293 if (disabled || !nvram_get_int("usb_storage") || nvram_get_int("usb_mmc") != 1) {
294 modprobe_r("sdhci");
295 modprobe_r("mmc_block");
296 modprobe_r("mmc_core");
298 #endif
300 if (disabled || nvram_get_int("usb_ohci") != 1) modprobe_r(USBOHCI_MOD);
301 if (disabled || nvram_get_int("usb_uhci") != 1) modprobe_r(USBUHCI_MOD);
302 if (disabled || nvram_get_int("usb_usb2") != 1) modprobe_r(USB20_MOD);
304 #ifdef LINUX26
305 //modprobe_r("leds-usb");
306 //modprobe_r("ledtrig-usbdev");
307 led(LED_USB, LED_OFF);
308 #endif
310 // only unload core modules if usb is disabled
311 if (disabled) {
312 umount("/proc/bus/usb"); // unmount usb device filesystem
313 modprobe_r(USBOHCI_MOD);
314 modprobe_r(USBUHCI_MOD);
315 modprobe_r(USB20_MOD);
316 modprobe_r(USBCORE_MOD);
319 #ifdef LINUX26
320 if (nvram_get_int("idle_enable") == 0) {
321 killall("sd-idle", SIGTERM);
324 #ifdef TCONFIG_UPS
325 stop_ups();
326 #endif
328 if (nvram_match("3g_usb", "0") ) {
329 if (nvram_match("3g_module", "sierra") ) {
330 modprobe_r("sierra");
331 modprobe_r("usbserial");
334 if (nvram_match("3g_module", "option") ) {
335 modprobe_r("option");
336 modprobe_r("usbserial");
339 // shibby
340 // when modem use usbserial module and we will try remove module, module will crash
341 // the only solution at the moment is reboot router
342 // FIX THIS
343 if (nvram_match("3g_module", "usbserial") ) {
344 modprobe_r("usbserial");
348 if (nvram_match("boardtype", "0x052b")) { // Netgear WNR3500L v2 - disable USB port
349 xstart("gpio", "disable", "20");
353 #endif
358 #define MOUNT_VAL_FAIL 0
359 #define MOUNT_VAL_RONLY 1
360 #define MOUNT_VAL_RW 2
361 #define MOUNT_VAL_EXIST 3
363 int mount_r(char *mnt_dev, char *mnt_dir, char *type)
365 struct mntent *mnt;
366 int ret;
367 char options[140];
368 char flagfn[128];
369 int dir_made;
371 if ((mnt = findmntents(mnt_dev, 0, NULL, 0))) {
372 syslog(LOG_INFO, "USB partition at %s already mounted on %s",
373 mnt_dev, mnt->mnt_dir);
374 return MOUNT_VAL_EXIST;
377 options[0] = 0;
379 if (type) {
380 unsigned long flags = MS_NOATIME | MS_NODEV;
382 if (strcmp(type, "swap") == 0 || strcmp(type, "mbr") == 0) {
383 /* not a mountable partition */
384 flags = 0;
386 else if (strcmp(type, "ext2") == 0 || strcmp(type, "ext3") == 0) {
387 if (nvram_invmatch("usb_ext_opt", ""))
388 sprintf(options, nvram_safe_get("usb_ext_opt"));
390 else if (strcmp(type, "vfat") == 0) {
391 if (nvram_invmatch("smbd_cset", ""))
392 sprintf(options, "iocharset=%s%s",
393 isdigit(nvram_get("smbd_cset")[0]) ? "cp" : "",
394 nvram_get("smbd_cset"));
395 if (nvram_invmatch("smbd_cpage", "")) {
396 char *cp = nvram_safe_get("smbd_cpage");
397 sprintf(options + strlen(options), ",codepage=%s" + (options[0] ? 0 : 1), cp);
398 sprintf(flagfn, "nls_cp%s", cp);
400 cp = nvram_get("smbd_nlsmod");
401 if ((cp) && (*cp != 0) && (strcmp(cp, flagfn) != 0))
402 modprobe_r(cp);
404 modprobe(flagfn);
405 nvram_set("smbd_nlsmod", flagfn);
407 sprintf(options + strlen(options), ",shortname=winnt" + (options[0] ? 0 : 1));
408 #ifdef LINUX26
409 sprintf(options + strlen(options), ",flush" + (options[0] ? 0 : 1));
410 #endif
411 if (nvram_invmatch("usb_fat_opt", ""))
412 sprintf(options + strlen(options), "%s%s", options[0] ? "," : "", nvram_safe_get("usb_fat_opt"));
414 else if (strncmp(type, "ntfs", 4) == 0) {
415 if (nvram_invmatch("smbd_cset", ""))
416 sprintf(options, "iocharset=%s%s",
417 isdigit(nvram_get("smbd_cset")[0]) ? "cp" : "",
418 nvram_get("smbd_cset"));
419 if (nvram_invmatch("usb_ntfs_opt", ""))
420 sprintf(options + strlen(options), "%s%s", options[0] ? "," : "", nvram_safe_get("usb_ntfs_opt"));
423 if (flags) {
424 if ((dir_made = mkdir_if_none(mnt_dir))) {
425 /* Create the flag file for remove the directory on dismount. */
426 sprintf(flagfn, "%s/.autocreated-dir", mnt_dir);
427 f_write(flagfn, NULL, 0, 0, 0);
430 ret = mount(mnt_dev, mnt_dir, type, flags, options[0] ? options : "");
432 #ifdef TCONFIG_NTFS
433 /* try ntfs-3g in case it's installed */
434 if (ret != 0 && strncmp(type, "ntfs", 4) == 0) {
435 sprintf(options + strlen(options), ",noatime,nodev" + (options[0] ? 0 : 1));
436 if (nvram_get_int("usb_fs_ntfs"))
437 #ifdef TCONFIG_UFSD
438 ret = eval("mount", "-t", "ufsd", "-o", options, "-o", "force", mnt_dev, mnt_dir);
439 #else
440 ret = eval("ntfs-3g", "-o", options, mnt_dev, mnt_dir);
441 #endif
443 #endif
445 #ifdef TCONFIG_HFS
446 if (ret != 0 && strncmp(type, "hfs", "") == 0) {
447 ret = eval("mount", "-o", "noatime,nodev", mnt_dev, mnt_dir);
450 if (ret != 0 && strncmp(type, "hfsplus", "") == 0) {
451 ret = eval("mount", "-o", "noatime,nodev", mnt_dev, mnt_dir);
453 #endif
455 if (ret != 0) /* give it another try - guess fs */
456 ret = eval("mount", "-o", "noatime,nodev", mnt_dev, mnt_dir);
458 if (ret == 0) {
459 syslog(LOG_INFO, "USB %s%s fs at %s mounted on %s",
460 type, (flags & MS_RDONLY) ? " (ro)" : "", mnt_dev, mnt_dir);
461 return (flags & MS_RDONLY) ? MOUNT_VAL_RONLY : MOUNT_VAL_RW;
464 if (dir_made) {
465 unlink(flagfn);
466 rmdir(mnt_dir);
470 return MOUNT_VAL_FAIL;
474 struct mntent *mount_fstab(char *dev_name, char *type, char *label, char *uuid)
476 struct mntent *mnt = NULL;
477 #if 0
478 if (eval("mount", "-a") == 0)
479 mnt = findmntents(dev_name, 0, NULL, 0);
480 #else
481 char spec[PATH_MAX+1];
483 if (label && *label) {
484 sprintf(spec, "LABEL=%s", label);
485 if (eval("mount", spec) == 0)
486 mnt = findmntents(dev_name, 0, NULL, 0);
489 if (!mnt && uuid && *uuid) {
490 sprintf(spec, "UUID=%s", uuid);
491 if (eval("mount", spec) == 0)
492 mnt = findmntents(dev_name, 0, NULL, 0);
495 if (!mnt) {
496 if (eval("mount", dev_name) == 0)
497 mnt = findmntents(dev_name, 0, NULL, 0);
500 if (!mnt) {
501 /* Still did not find what we are looking for, try absolute path */
502 if (realpath(dev_name, spec)) {
503 if (eval("mount", spec) == 0)
504 mnt = findmntents(dev_name, 0, NULL, 0);
507 #endif
509 if (mnt)
510 syslog(LOG_INFO, "USB %s fs at %s mounted on %s", type, dev_name, mnt->mnt_dir);
511 return (mnt);
515 /* Check if the UFD is still connected because the links created in /dev/discs
516 * are not removed when the UFD is unplugged.
517 * The file to read is: /proc/scsi/usb-storage-#/#, where # is the host no.
518 * We are looking for "Attached: Yes".
520 static int usb_ufd_connected(int host_no)
522 char proc_file[128];
523 #ifndef LINUX26
524 char line[256];
525 #endif
526 FILE *fp;
528 sprintf(proc_file, "%s/%s-%d/%d", PROC_SCSI_ROOT, USB_STORAGE, host_no, host_no);
529 fp = fopen(proc_file, "r");
531 if (!fp) {
532 /* try the way it's implemented in newer kernels: /proc/scsi/usb-storage/[host] */
533 sprintf(proc_file, "%s/%s/%d", PROC_SCSI_ROOT, USB_STORAGE, host_no);
534 fp = fopen(proc_file, "r");
537 if (fp) {
538 #ifdef LINUX26
539 fclose(fp);
540 return 1;
541 #else
542 while (fgets(line, sizeof(line), fp) != NULL) {
543 if (strstr(line, "Attached: Yes")) {
544 fclose(fp);
545 return 1;
548 fclose(fp);
549 #endif
552 return 0;
556 #ifndef MNT_DETACH
557 #define MNT_DETACH 0x00000002 /* from linux/fs.h - just detach from the tree */
558 #endif
559 int umount_mountpoint(struct mntent *mnt, uint flags);
560 int uswap_mountpoint(struct mntent *mnt, uint flags);
562 /* Unmount this partition from all its mountpoints. Note that it may
563 * actually be mounted several times, either with different names or
564 * with "-o bind" flag.
565 * If the special flagfile is now revealed, delete it and [attempt to] delete
566 * the directory.
568 int umount_partition(char *dev_name, int host_num, char *dsc_name, char *pt_name, uint flags)
570 sync(); /* This won't matter if the device is unplugged, though. */
572 if (flags & EFH_HUNKNOWN) {
573 /* EFH_HUNKNOWN flag is passed if the host was unknown.
574 * Only unmount disconnected drives in this case.
576 if (usb_ufd_connected(host_num))
577 return 0;
580 /* Find all the active swaps that are on this device and stop them. */
581 findmntents(dev_name, 1, uswap_mountpoint, flags);
583 /* Find all the mountpoints that are for this device and unmount them. */
584 findmntents(dev_name, 0, umount_mountpoint, flags);
585 return 0;
588 int uswap_mountpoint(struct mntent *mnt, uint flags)
590 swapoff(mnt->mnt_fsname);
591 return 0;
594 int umount_mountpoint(struct mntent *mnt, uint flags)
596 int ret = 1, count;
597 char flagfn[128];
599 sprintf(flagfn, "%s/.autocreated-dir", mnt->mnt_dir);
601 /* Run user pre-unmount scripts if any. It might be too late if
602 * the drive has been disconnected, but we'll try it anyway.
604 if (nvram_get_int("usb_automount"))
605 run_nvscript("script_usbumount", mnt->mnt_dir, 3);
606 /* Run *.autostop scripts located in the root of the partition being unmounted if any. */
607 run_userfile(mnt->mnt_dir, ".autostop", mnt->mnt_dir, 5);
608 run_nvscript("script_autostop", mnt->mnt_dir, 5);
610 count = 0;
611 while ((ret = umount(mnt->mnt_dir)) && (count < 2)) {
612 count++;
613 /* If we could not unmount the drive on the 1st try,
614 * kill all NAS applications so they are not keeping the device busy -
615 * unless it's an unmount request from the Web GUI.
617 if ((count == 1) && ((flags & EFH_USER) == 0))
618 restart_nas_services(1, 0);
619 sleep(1);
622 if (ret == 0)
623 syslog(LOG_INFO, "USB partition unmounted from %s", mnt->mnt_dir);
625 if (ret && ((flags & EFH_SHUTDN) != 0)) {
626 /* If system is stopping (not restarting), and we couldn't unmount the
627 * partition, try to remount it as read-only. Ignore the return code -
628 * we can still try to do a lazy unmount.
630 eval("mount", "-o", "remount,ro", mnt->mnt_dir);
633 if (ret && ((flags & EFH_USER) == 0)) {
634 /* Make one more try to do a lazy unmount unless it's an unmount
635 * request from the Web GUI.
636 * MNT_DETACH will expose the underlying mountpoint directory to all
637 * except whatever has cd'ed to the mountpoint (thereby making it busy).
638 * So the unmount can't actually fail. It disappears from the ken of
639 * everyone else immediately, and from the ken of whomever is keeping it
640 * busy when they move away from it. And then it disappears for real.
642 ret = umount2(mnt->mnt_dir, MNT_DETACH);
643 syslog(LOG_INFO, "USB partition busy - will unmount ASAP from %s", mnt->mnt_dir);
646 if (ret == 0) {
647 if ((unlink(flagfn) == 0)) {
648 // Only delete the directory if it was auto-created
649 rmdir(mnt->mnt_dir);
652 return (ret == 0);
656 /* Mount this partition on this disc.
657 * If the device is already mounted on any mountpoint, don't mount it again.
658 * If this is a swap partition, try swapon -a.
659 * If this is a regular partition, try mount -a.
661 * Before we mount any partitions:
662 * If the type is swap and /etc/fstab exists, do "swapon -a"
663 * If /etc/fstab exists, try mounting using fstab.
664 * We delay invoking mount because mount will probe all the partitions
665 * to read the labels, and we don't want it to do that early on.
666 * We don't invoke swapon until we actually find a swap partition.
668 * If the mount succeeds, execute the *.autorun scripts in the top
669 * directory of the newly mounted partition.
670 * Returns NZ for success, 0 if we did not mount anything.
672 int mount_partition(char *dev_name, int host_num, char *dsc_name, char *pt_name, uint flags)
674 char the_label[128], mountpoint[128], uuid[40];
675 int ret;
676 char *type, *p;
677 static char *swp_argv[] = { "swapon", "-a", NULL };
678 struct mntent *mnt;
680 if ((type = find_label_or_uuid(dev_name, the_label, uuid)) == NULL)
681 return 0;
683 if (f_exists("/etc/fstab")) {
684 if (strcmp(type, "swap") == 0) {
685 _eval(swp_argv, NULL, 0, NULL);
686 return 0;
689 if (mount_r(dev_name, NULL, NULL) == MOUNT_VAL_EXIST)
690 return 0;
692 if ((mnt = mount_fstab(dev_name, type, the_label, uuid))) {
693 strcpy(mountpoint, mnt->mnt_dir);
694 ret = MOUNT_VAL_RW;
695 goto done;
699 if (*the_label != 0) {
700 for (p = the_label; *p; p++) {
701 if (!isalnum(*p) && !strchr("+-&.@", *p))
702 *p = '_';
704 sprintf(mountpoint, "%s/%s", MOUNT_ROOT, the_label);
705 if ((ret = mount_r(dev_name, mountpoint, type)))
706 goto done;
709 /* Can't mount to /mnt/LABEL, so try mounting to /mnt/discDN_PN */
710 sprintf(mountpoint, "%s/%s", MOUNT_ROOT, pt_name);
711 ret = mount_r(dev_name, mountpoint, type);
712 done:
713 if (ret == MOUNT_VAL_RONLY || ret == MOUNT_VAL_RW)
715 /* Run user *.autorun and post-mount scripts if any. */
716 run_userfile(mountpoint, ".autorun", mountpoint, 3);
717 if (nvram_get_int("usb_automount"))
718 run_nvscript("script_usbmount", mountpoint, 3);
720 return (ret == MOUNT_VAL_RONLY || ret == MOUNT_VAL_RW);
724 #if 0 /* LINUX26 */
727 * Finds SCSI Host number. Returns the host number >=0 if found, or (-1) otherwise.
728 * The name and host number of scsi block device in kernel 2.6 (for attached devices) can be found as
729 * /sys($DEVPATH)/host<host_no>/target<*>/<id>/block:[sda|sdb|...]
730 * where $DEVPATH is passed to hotplug events, and looks like
731 * /devices/pci0000:00/0000:00:04.1/usb1/1-1/1-1:1.2
733 * For printers this function finds a minor assigned to a printer
734 * /sys($DEVPATH)/usb:lp[0|1|2|...]
736 int find_dev_host(const char *devpath)
738 DIR *usb_devpath;
739 struct dirent *dp;
740 char buf[256];
741 int host = -1; /* Scsi Host */
743 sprintf(buf, "/sys%s", devpath);
744 if ((usb_devpath = opendir(buf))) {
745 while ((dp = readdir(usb_devpath))) {
746 errno = 0;
747 if (strncmp(dp->d_name, "host", 4) == 0) {
748 host = strtol(dp->d_name + 4, (char **)NULL, 10);
749 if (errno)
750 host = -1;
751 else
752 break;
754 else if (strncmp(dp->d_name, "usb:lp", 6) == 0) {
755 host = strtol(dp->d_name + 6, (char **)NULL, 10);
756 if (errno)
757 host = -1;
758 else
759 break;
761 else
762 continue;
764 closedir(usb_devpath);
766 return (host);
769 #endif /* LINUX26 */
771 int dir_is_mountpoint(const char *root, const char *dir)
773 char path[256];
774 struct stat sb;
775 int thisdev;
777 snprintf(path, sizeof(path), "%s%s%s", root ? : "", root ? "/" : "", dir);
779 /* Check if this is a directory */
780 sb.st_mode = S_IFDIR; /* failsafe */
781 stat(path, &sb);
783 if (S_ISDIR(sb.st_mode)) {
785 /* If this dir & its parent dir are on the same device, it is not a mountpoint */
786 strcat(path, "/.");
787 stat(path, &sb);
788 thisdev = sb.st_dev;
789 strcat(path, ".");
790 ++sb.st_dev; /* failsafe */
791 stat(path, &sb);
793 return (thisdev != sb.st_dev);
796 return 0;
799 /* Mount or unmount all partitions on this controller.
800 * Parameter: action_add:
801 * 0 = unmount
802 * >0 = mount only if automount config option is enabled.
803 * <0 = mount regardless of config option.
805 void hotplug_usb_storage_device(int host_no, int action_add, uint flags)
807 if (!nvram_get_int("usb_enable"))
808 return;
809 _dprintf("%s: host %d action: %d\n", __FUNCTION__, host_no, action_add);
811 if (action_add) {
812 if (nvram_get_int("usb_storage") && (nvram_get_int("usb_automount") || action_add < 0)) {
813 /* Do not probe the device here. It's either initiated by user,
814 * or hotplug_usb() already did.
816 if (exec_for_host(host_no, 0x00, flags, mount_partition)) {
817 restart_nas_services(0, 1); // restart all NAS applications
821 else {
822 if (nvram_get_int("usb_storage") || ((flags & EFH_USER) == 0)) {
823 /* When unplugged, unmount the device even if
824 * usb storage is disabled in the GUI.
826 exec_for_host(host_no, (flags & EFH_USER) ? 0x00 : 0x02, flags, umount_partition);
827 /* Restart NAS applications (they could be killed by umount_mountpoint),
828 * or just re-read the configuration.
830 restart_nas_services(0, 1);
836 /* This gets called at reboot or upgrade. The system is stopping. */
837 void remove_storage_main(int shutdn)
839 if (shutdn)
840 restart_nas_services(1, 0);
841 /* Unmount all partitions */
842 exec_for_host(-1, 0x02, shutdn ? EFH_SHUTDN : 0, umount_partition);
846 /*******
847 * All the complex locking & checking code was removed when the kernel USB-storage
848 * bugs were fixed.
849 * The crash bug was with overlapped I/O to different USB drives, not specifically
850 * with mount processing.
852 * And for USB devices that are slow to come up. The kernel now waits until the
853 * USB drive has settled, and it correctly reads the partition table before calling
854 * the hotplug agent.
856 * The kernel patch was cleaning up data structures on an unplug. It
857 * needs to wait until the disk is unmounted. We have 20 seconds to do
858 * the unmounts.
859 *******/
861 #ifdef LINUX26
862 static inline void usbled_proc(char *device, int add)
864 char *p;
865 char param[32];
867 if (do_led(LED_USB, LED_PROBE) != 255) {
868 strncpy(param, device, sizeof(param));
869 if ((p = strchr(param, ':')) != NULL)
870 *p = 0;
872 /* verify if we need to ignore this device (i.e. an internal SD/MMC slot ) */
873 p = nvram_safe_get("usb_noled");
874 if (strcmp(p, param) == 0)
875 return;
877 // Remove legacy approach in the code here - rather, use do_led() function, which is designed to do this
878 // The reason for changing this ... some HW (like Netgear WNDR4000) don't work with direct GPIO write -> use do_led()!
879 //f_write_string(add ? "/proc/leds-usb/add" : "/proc/leds-usb/remove", param, 0, 0);
880 if (add)
881 do_led(LED_USB, LED_ON);
882 else
883 do_led(LED_USB, LED_OFF);
886 #endif
888 /* Plugging or removing usb device
890 * On an occurrance, multiple hotplug events may be fired off.
891 * For example, if a hub is plugged or unplugged, an event
892 * will be generated for everything downstream of it, plus one for
893 * the hub itself. These are fired off simultaneously, not serially.
894 * This means that many many hotplug processes will be running at
895 * the same time.
897 * The hotplug event generated by the kernel gives us several pieces
898 * of information:
899 * PRODUCT is vendorid/productid/rev#.
900 * DEVICE is /proc/bus/usb/bus#/dev#
901 * ACTION is add or remove
902 * SCSI_HOST is the host (controller) number (this relies on the custom kernel patch)
904 * Note that when we get a hotplug add event, the USB susbsystem may
905 * or may not have yet tried to read the partition table of the
906 * device. For a new controller that has never been seen before,
907 * generally yes. For a re-plug of a controller that has been seen
908 * before, generally no.
910 * On a remove, the partition info has not yet been expunged. The
911 * partitions show up as /dev/discs/disc#/part#, and /proc/partitions.
912 * It appears that doing a "stat" for a non-existant partition will
913 * causes the kernel to re-validate the device and update the
914 * partition table info. However, it won't re-validate if the disc is
915 * mounted--you'll get a "Device busy for revalidation (usage=%d)" in
916 * syslog.
918 * The $INTERFACE is "class/subclass/protocol"
919 * Some interesting classes:
920 * 8 = mass storage
921 * 7 = printer
922 * 3 = HID. 3/1/2 = mouse.
923 * 6 = still image (6/1/1 = Digital camera Camera)
924 * 9 = Hub
925 * 255 = scanner (255/255/255)
927 * Observed:
928 * Hub seems to have no INTERFACE (null), and TYPE of "9/0/0"
929 * Flash disk seems to have INTERFACE of "8/6/80", and TYPE of "0/0/0"
931 * When a hub is unplugged, a hotplug event is generated for it and everything
932 * downstream from it. You cannot depend on getting these events in any
933 * particular order, since there will be many hotplug programs all fired off
934 * at almost the same time.
935 * On a remove, don't try to access the downstream devices right away, give the
936 * kernel time to finish cleaning up all the data structures, which will be
937 * in the process of being torn down.
939 * On the initial plugin, the first time the kernel usb-storage subsystem sees
940 * the host (identified by GUID), it automatically reads the partition table.
941 * On subsequent plugins, it does not.
943 * Special values for Web Administration to unmount or remount
944 * all partitions of the host:
945 * INTERFACE=TOMATO/...
946 * ACTION=add/remove
947 * SCSI_HOST=<host_no>
948 * If host_no is negative, we unmount all partions of *all* hosts.
950 void hotplug_usb(void)
952 int add;
953 int host = -1;
954 char *interface = getenv("INTERFACE");
955 char *action = getenv("ACTION");
956 char *product = getenv("PRODUCT");
957 #ifdef LINUX26
958 char *device = getenv("DEVICENAME");
959 int is_block = strcmp(getenv("SUBSYSTEM") ? : "", "block") == 0;
960 #else
961 char *device = getenv("DEVICE");
962 #endif
963 char *scsi_host = getenv("SCSI_HOST");
965 _dprintf("%s hotplug INTERFACE=%s ACTION=%s PRODUCT=%s HOST=%s DEVICE=%s\n",
966 getenv("SUBSYSTEM") ? : "USB", interface, action, product, scsi_host, device);
968 if (!nvram_get_int("usb_enable")) return;
969 #ifdef LINUX26
970 if (!action || ((!interface || !product) && !is_block))
971 #else
972 if (!interface || !action || !product) /* Hubs bail out here. */
973 #endif
974 return;
976 if (scsi_host)
977 host = atoi(scsi_host);
979 if (!wait_action_idle(10)) return;
981 add = (strcmp(action, "add") == 0);
982 if (add && (strncmp(interface ? : "", "TOMATO/", 7) != 0)) {
983 #ifdef LINUX26
984 if (!is_block && device)
985 #endif
986 syslog(LOG_DEBUG, "Attached USB device %s [INTERFACE=%s PRODUCT=%s]",
987 device, interface, product);
988 #ifndef LINUX26
989 /* To allow automount to be blocked on startup.
990 * In kernel 2.6 we still need to serialize mount/umount calls -
991 * so the lock is down below in the "block" hotplug processing.
993 file_unlock(file_lock("usb"));
994 #endif
997 if (strncmp(interface ? : "", "TOMATO/", 7) == 0) { /* web admin */
998 if (scsi_host == NULL)
999 host = atoi(product); // for backward compatibility
1000 /* If host is negative, unmount all partitions of *all* hosts.
1001 * If host == -1, execute "soft" unmount (do not kill NAS apps, no "lazy" umount).
1002 * If host == -2, run "hard" unmount, as if the drive is unplugged.
1003 * This feature can be used in custom scripts as following:
1005 * # INTERFACE=TOMATO/1 ACTION=remove PRODUCT=-1 SCSI_HOST=-1 hotplug usb
1007 * PRODUCT is required to pass the env variables verification.
1009 /* Unmount or remount all partitions of the host. */
1010 hotplug_usb_storage_device(host < 0 ? -1 : host, add ? -1 : 0,
1011 host == -2 ? 0 : EFH_USER);
1013 #ifdef LINUX26
1014 else if (is_block && strcmp(getenv("MAJOR") ? : "", "8") == 0 && strcmp(getenv("PHYSDEVBUS") ? : "", "scsi") == 0) {
1015 /* scsi partition */
1016 char devname[64];
1017 int lock;
1019 sprintf(devname, "/dev/%s", device);
1020 lock = file_lock("usb");
1021 if (add) {
1022 if (nvram_get_int("usb_storage") && nvram_get_int("usb_automount")) {
1023 int minor = atoi(getenv("MINOR") ? : "0");
1024 if ((minor % 16) == 0 && !is_no_partition(device)) {
1025 /* This is a disc, and not a "no-partition" device,
1026 * like APPLE iPOD shuffle. We can't mount it.
1028 return;
1030 if (mount_partition(devname, host, NULL, device, EFH_HP_ADD)) {
1031 restart_nas_services(0, 1); // restart all NAS applications
1035 else {
1036 /* When unplugged, unmount the device even if usb storage is disabled in the GUI */
1037 umount_partition(devname, host, NULL, device, EFH_HP_REMOVE);
1038 /* Restart NAS applications (they could be killed by umount_mountpoint),
1039 * or just re-read the configuration.
1041 restart_nas_services(0, 1);
1043 file_unlock(lock);
1045 #endif
1046 else if (strncmp(interface ? : "", "8/", 2) == 0) { /* usb storage */
1047 #ifdef LINUX26
1048 usbled_proc(device, add);
1049 #endif
1050 run_nvscript("script_usbhotplug", NULL, 2);
1051 #ifndef LINUX26
1052 hotplug_usb_storage_device(host, add, (add ? EFH_HP_ADD : EFH_HP_REMOVE) | (host < 0 ? EFH_HUNKNOWN : 0));
1053 #endif
1055 else { /* It's some other type of USB device, not storage. */
1056 #ifdef LINUX26
1057 if (is_block) return;
1058 #endif
1059 #ifdef LINUX26
1060 if (strncmp(interface ? : "", "7/", 2) == 0) /* printer */
1061 usbled_proc(device, add);
1062 #endif
1063 /* Do nothing. The user's hotplug script must do it all. */
1064 run_nvscript("script_usbhotplug", NULL, 2);