fixes, fully translated tomato, with english dictionary and Polish translation
[tomato.git] / release / src / router / rc / usb.c
blob7e1c8ade2c0cf957fc151eb20a97e4d4494cc9bc
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 _dprintf("%s\n", __FUNCTION__);
88 tune_bdflush();
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);
96 #ifdef LINUX26
97 i = do_led(LED_USB, LED_PROBE);
98 if (i != 255) {
99 modprobe("ledtrig-usbdev");
100 modprobe("leds-usb");
101 sprintf(param, "%d", i);
102 f_write_string("/sys/class/leds/usb-led/gpio_pin", param, 0, 0);
103 f_write_string("/sys/class/leds/usb-led/device_name", "1-1", 0, 0);
105 #endif
106 if (nvram_get_int("usb_storage")) {
107 /* insert scsi and storage modules before usb drivers */
108 modprobe(SCSI_MOD);
109 #ifdef LINUX26
110 modprobe(SCSI_WAIT_MOD);
111 #endif
112 modprobe(SD_MOD);
113 modprobe(USBSTORAGE_MOD);
115 if (nvram_get_int("usb_fs_ext3")) {
116 #ifdef LINUX26
117 modprobe("mbcache"); // used by ext2/ext3
118 #endif
119 /* insert ext3 first so that lazy mount tries ext3 before ext2 */
120 modprobe("jbd");
121 modprobe("ext3");
122 modprobe("ext2");
125 if (nvram_get_int("usb_fs_fat")) {
126 modprobe("fat");
127 modprobe("vfat");
131 /* if enabled, force USB2 before USB1.1 */
132 if (nvram_get_int("usb_usb2") == 1) {
133 i = nvram_get_int("usb_irq_thresh");
134 if ((i < 0) || (i > 6))
135 i = 0;
136 sprintf(param, "log2_irq_thresh=%d", i);
137 modprobe(USB20_MOD, param);
140 if (nvram_get_int("usb_uhci") == 1) {
141 modprobe(USBUHCI_MOD);
144 if (nvram_get_int("usb_ohci") == 1) {
145 modprobe(USBOHCI_MOD);
148 if (nvram_get_int("usb_printer")) {
149 symlink("/dev/usb", "/dev/printers");
150 modprobe(USBPRINTER_MOD);
152 /* start printer server only if not already running */
153 if (p9100d_sig(0) != 0) {
154 eval("p910nd",
155 nvram_get_int("usb_printer_bidirect") ? "-b" : "", //bidirectional
156 "-f", "/dev/usb/lp0", // device
157 "0" // listen port
164 void stop_usb(void)
166 int disabled = !nvram_get_int("usb_enable");
168 // only find and kill the printer server we started (port 0)
169 p9100d_sig(SIGTERM);
170 modprobe_r(USBPRINTER_MOD);
172 // only stop storage services if disabled
173 if (disabled || !nvram_get_int("usb_storage")) {
174 // Unmount all partitions
175 remove_storage_main(0);
177 // Stop storage services
178 modprobe_r("ext2");
179 modprobe_r("ext3");
180 modprobe_r("jbd");
181 #ifdef LINUX26
182 modprobe_r("mbcache");
183 #endif
184 modprobe_r("vfat");
185 modprobe_r("fat");
186 modprobe_r("fuse");
187 sleep(1);
188 #ifdef TCONFIG_SAMBASRV
189 modprobe_r("nls_cp437");
190 modprobe_r("nls_cp850");
191 modprobe_r("nls_cp852");
192 modprobe_r("nls_cp866");
193 #ifdef LINUX26
194 modprobe_r("nls_cp932");
195 modprobe_r("nls_cp936");
196 modprobe_r("nls_cp949");
197 modprobe_r("nls_cp950");
198 #endif
199 #endif
200 modprobe_r(USBSTORAGE_MOD);
201 modprobe_r(SD_MOD);
202 #ifdef LINUX26
203 modprobe_r(SCSI_WAIT_MOD);
204 #endif
205 modprobe_r(SCSI_MOD);
208 if (disabled || nvram_get_int("usb_ohci") != 1) modprobe_r(USBOHCI_MOD);
209 if (disabled || nvram_get_int("usb_uhci") != 1) modprobe_r(USBUHCI_MOD);
210 if (disabled || nvram_get_int("usb_usb2") != 1) modprobe_r(USB20_MOD);
212 #ifdef LINUX26
213 modprobe_r("leds-usb");
214 modprobe_r("ledtrig-usbdev");
215 led(LED_USB, LED_OFF);
216 #endif
218 // only unload core modules if usb is disabled
219 if (disabled) {
220 umount("/proc/bus/usb"); // unmount usb device filesystem
221 modprobe_r(USBOHCI_MOD);
222 modprobe_r(USBUHCI_MOD);
223 modprobe_r(USB20_MOD);
224 modprobe_r(USBCORE_MOD);
229 #define MOUNT_VAL_FAIL 0
230 #define MOUNT_VAL_RONLY 1
231 #define MOUNT_VAL_RW 2
232 #define MOUNT_VAL_EXIST 3
234 int mount_r(char *mnt_dev, char *mnt_dir, char *type)
236 struct mntent *mnt;
237 int ret;
238 char options[140];
239 char flagfn[128];
240 int dir_made;
242 if ((mnt = findmntents(mnt_dev, 0, NULL, 0))) {
243 syslog(LOG_INFO, "USB partition at %s already mounted on %s",
244 mnt_dev, mnt->mnt_dir);
245 return MOUNT_VAL_EXIST;
248 options[0] = 0;
250 if (type) {
251 unsigned long flags = MS_NOATIME | MS_NODEV;
253 if (strcmp(type, "swap") == 0 || strcmp(type, "mbr") == 0) {
254 /* not a mountable partition */
255 flags = 0;
257 else if (strcmp(type, "ext2") == 0 || strcmp(type, "ext3") == 0) {
258 if (nvram_invmatch("usb_ext_opt", ""))
259 sprintf(options, nvram_safe_get("usb_ext_opt"));
261 else if (strcmp(type, "vfat") == 0) {
262 if (nvram_invmatch("smbd_cset", ""))
263 sprintf(options, "iocharset=%s%s",
264 isdigit(nvram_get("smbd_cset")[0]) ? "cp" : "",
265 nvram_get("smbd_cset"));
266 if (nvram_invmatch("smbd_cpage", "")) {
267 char *cp = nvram_safe_get("smbd_cpage");
268 sprintf(options + strlen(options), ",codepage=%s" + (options[0] ? 0 : 1), cp);
269 sprintf(flagfn, "nls_cp%s", cp);
271 cp = nvram_get("smbd_nlsmod");
272 if ((cp) && (*cp != 0) && (strcmp(cp, flagfn) != 0))
273 modprobe_r(cp);
275 modprobe(flagfn);
276 nvram_set("smbd_nlsmod", flagfn);
278 sprintf(options + strlen(options), ",shortname=winnt" + (options[0] ? 0 : 1));
279 #ifdef LINUX26
280 sprintf(options + strlen(options), ",flush" + (options[0] ? 0 : 1));
281 #endif
282 if (nvram_invmatch("usb_fat_opt", ""))
283 sprintf(options + strlen(options), "%s%s", options[0] ? "," : "", nvram_safe_get("usb_fat_opt"));
285 else if (strncmp(type, "ntfs", 4) == 0) {
286 if (nvram_invmatch("smbd_cset", ""))
287 sprintf(options, "iocharset=%s%s",
288 isdigit(nvram_get("smbd_cset")[0]) ? "cp" : "",
289 nvram_get("smbd_cset"));
290 if (nvram_invmatch("usb_ntfs_opt", ""))
291 sprintf(options + strlen(options), "%s%s", options[0] ? "," : "", nvram_safe_get("usb_ntfs_opt"));
294 if (flags) {
295 if ((dir_made = mkdir_if_none(mnt_dir))) {
296 /* Create the flag file for remove the directory on dismount. */
297 sprintf(flagfn, "%s/.autocreated-dir", mnt_dir);
298 f_write(flagfn, NULL, 0, 0, 0);
301 ret = mount(mnt_dev, mnt_dir, type, flags, options[0] ? options : "");
303 /* try ntfs-3g in case it's installed */
304 if (ret != 0 && strncmp(type, "ntfs", 4) == 0) {
305 sprintf(options + strlen(options), ",noatime,nodev" + (options[0] ? 0 : 1));
306 #ifdef TCONFIG_NTFS
307 if (nvram_get_int("usb_fs_ntfs"))
308 #endif
309 ret = eval("ntfs-3g", "-o", options, mnt_dev, mnt_dir);
311 if (ret != 0) /* give it another try - guess fs */
312 ret = eval("mount", "-o", "noatime,nodev", mnt_dev, mnt_dir);
314 if (ret == 0) {
315 syslog(LOG_INFO, "USB %s%s fs at %s mounted on %s",
316 type, (flags & MS_RDONLY) ? " (ro)" : "", mnt_dev, mnt_dir);
317 return (flags & MS_RDONLY) ? MOUNT_VAL_RONLY : MOUNT_VAL_RW;
320 if (dir_made) {
321 unlink(flagfn);
322 rmdir(mnt_dir);
326 return MOUNT_VAL_FAIL;
330 struct mntent *mount_fstab(char *dev_name, char *type, char *label, char *uuid)
332 struct mntent *mnt = NULL;
333 #if 0
334 if (eval("mount", "-a") == 0)
335 mnt = findmntents(dev_name, 0, NULL, 0);
336 #else
337 char spec[PATH_MAX+1];
339 if (label && *label) {
340 sprintf(spec, "LABEL=%s", label);
341 if (eval("mount", spec) == 0)
342 mnt = findmntents(dev_name, 0, NULL, 0);
345 if (!mnt && uuid && *uuid) {
346 sprintf(spec, "UUID=%s", uuid);
347 if (eval("mount", spec) == 0)
348 mnt = findmntents(dev_name, 0, NULL, 0);
351 if (!mnt) {
352 if (eval("mount", dev_name) == 0)
353 mnt = findmntents(dev_name, 0, NULL, 0);
356 if (!mnt) {
357 /* Still did not find what we are looking for, try absolute path */
358 if (realpath(dev_name, spec)) {
359 if (eval("mount", spec) == 0)
360 mnt = findmntents(dev_name, 0, NULL, 0);
363 #endif
365 if (mnt)
366 syslog(LOG_INFO, "USB %s fs at %s mounted on %s", type, dev_name, mnt->mnt_dir);
367 return (mnt);
371 /* Check if the UFD is still connected because the links created in /dev/discs
372 * are not removed when the UFD is unplugged.
373 * The file to read is: /proc/scsi/usb-storage-#/#, where # is the host no.
374 * We are looking for "Attached: Yes".
376 static int usb_ufd_connected(int host_no)
378 char proc_file[128];
379 #ifndef LINUX26
380 char line[256];
381 #endif
382 FILE *fp;
384 sprintf(proc_file, "%s/%s-%d/%d", PROC_SCSI_ROOT, USB_STORAGE, host_no, host_no);
385 fp = fopen(proc_file, "r");
387 if (!fp) {
388 /* try the way it's implemented in newer kernels: /proc/scsi/usb-storage/[host] */
389 sprintf(proc_file, "%s/%s/%d", PROC_SCSI_ROOT, USB_STORAGE, host_no);
390 fp = fopen(proc_file, "r");
393 if (fp) {
394 #ifdef LINUX26
395 fclose(fp);
396 return 1;
397 #else
398 while (fgets(line, sizeof(line), fp) != NULL) {
399 if (strstr(line, "Attached: Yes")) {
400 fclose(fp);
401 return 1;
404 fclose(fp);
405 #endif
408 return 0;
412 #ifndef MNT_DETACH
413 #define MNT_DETACH 0x00000002 /* from linux/fs.h - just detach from the tree */
414 #endif
415 int umount_mountpoint(struct mntent *mnt, uint flags);
416 int uswap_mountpoint(struct mntent *mnt, uint flags);
418 /* Unmount this partition from all its mountpoints. Note that it may
419 * actually be mounted several times, either with different names or
420 * with "-o bind" flag.
421 * If the special flagfile is now revealed, delete it and [attempt to] delete
422 * the directory.
424 int umount_partition(char *dev_name, int host_num, char *dsc_name, char *pt_name, uint flags)
426 sync(); /* This won't matter if the device is unplugged, though. */
428 if (flags & EFH_HUNKNOWN) {
429 /* EFH_HUNKNOWN flag is passed if the host was unknown.
430 * Only unmount disconnected drives in this case.
432 if (usb_ufd_connected(host_num))
433 return 0;
436 /* Find all the active swaps that are on this device and stop them. */
437 findmntents(dev_name, 1, uswap_mountpoint, flags);
439 /* Find all the mountpoints that are for this device and unmount them. */
440 findmntents(dev_name, 0, umount_mountpoint, flags);
441 return 0;
444 int uswap_mountpoint(struct mntent *mnt, uint flags)
446 swapoff(mnt->mnt_fsname);
447 return 0;
450 int umount_mountpoint(struct mntent *mnt, uint flags)
452 int ret = 1, count;
453 char flagfn[128];
455 sprintf(flagfn, "%s/.autocreated-dir", mnt->mnt_dir);
457 /* Run user pre-unmount scripts if any. It might be too late if
458 * the drive has been disconnected, but we'll try it anyway.
460 if (nvram_get_int("usb_automount"))
461 run_nvscript("script_usbumount", mnt->mnt_dir, 3);
462 /* Run *.autostop scripts located in the root of the partition being unmounted if any. */
463 run_userfile(mnt->mnt_dir, ".autostop", mnt->mnt_dir, 5);
464 run_nvscript("script_autostop", mnt->mnt_dir, 5);
466 count = 0;
467 while ((ret = umount(mnt->mnt_dir)) && (count < 2)) {
468 count++;
469 /* If we could not unmount the drive on the 1st try,
470 * kill all NAS applications so they are not keeping the device busy -
471 * unless it's an unmount request from the Web GUI.
473 if ((count == 1) && ((flags & EFH_USER) == 0))
474 restart_nas_services(1, 0);
475 sleep(1);
478 if (ret == 0)
479 syslog(LOG_INFO, "USB partition unmounted from %s", mnt->mnt_dir);
481 if (ret && ((flags & EFH_SHUTDN) != 0)) {
482 /* If system is stopping (not restarting), and we couldn't unmount the
483 * partition, try to remount it as read-only. Ignore the return code -
484 * we can still try to do a lazy unmount.
486 eval("mount", "-o", "remount,ro", mnt->mnt_dir);
489 if (ret && ((flags & EFH_USER) == 0)) {
490 /* Make one more try to do a lazy unmount unless it's an unmount
491 * request from the Web GUI.
492 * MNT_DETACH will expose the underlying mountpoint directory to all
493 * except whatever has cd'ed to the mountpoint (thereby making it busy).
494 * So the unmount can't actually fail. It disappears from the ken of
495 * everyone else immediately, and from the ken of whomever is keeping it
496 * busy when they move away from it. And then it disappears for real.
498 ret = umount2(mnt->mnt_dir, MNT_DETACH);
499 syslog(LOG_INFO, "USB partition busy - will unmount ASAP from %s", mnt->mnt_dir);
502 if (ret == 0) {
503 if ((unlink(flagfn) == 0)) {
504 // Only delete the directory if it was auto-created
505 rmdir(mnt->mnt_dir);
508 return (ret == 0);
512 /* Mount this partition on this disc.
513 * If the device is already mounted on any mountpoint, don't mount it again.
514 * If this is a swap partition, try swapon -a.
515 * If this is a regular partition, try mount -a.
517 * Before we mount any partitions:
518 * If the type is swap and /etc/fstab exists, do "swapon -a"
519 * If /etc/fstab exists, try mounting using fstab.
520 * We delay invoking mount because mount will probe all the partitions
521 * to read the labels, and we don't want it to do that early on.
522 * We don't invoke swapon until we actually find a swap partition.
524 * If the mount succeeds, execute the *.autorun scripts in the top
525 * directory of the newly mounted partition.
526 * Returns NZ for success, 0 if we did not mount anything.
528 int mount_partition(char *dev_name, int host_num, char *dsc_name, char *pt_name, uint flags)
530 char the_label[128], mountpoint[128], uuid[40];
531 int ret;
532 char *type, *p;
533 static char *swp_argv[] = { "swapon", "-a", NULL };
534 struct mntent *mnt;
536 if ((type = find_label_or_uuid(dev_name, the_label, uuid)) == NULL)
537 return 0;
539 if (f_exists("/etc/fstab")) {
540 if (strcmp(type, "swap") == 0) {
541 _eval(swp_argv, NULL, 0, NULL);
542 return 0;
545 if (mount_r(dev_name, NULL, NULL) == MOUNT_VAL_EXIST)
546 return 0;
548 if ((mnt = mount_fstab(dev_name, type, the_label, uuid))) {
549 strcpy(mountpoint, mnt->mnt_dir);
550 ret = MOUNT_VAL_RW;
551 goto done;
555 if (*the_label != 0) {
556 for (p = the_label; *p; p++) {
557 if (!isalnum(*p) && !strchr("+-&.@", *p))
558 *p = '_';
560 sprintf(mountpoint, "%s/%s", MOUNT_ROOT, the_label);
561 if ((ret = mount_r(dev_name, mountpoint, type)))
562 goto done;
565 /* Can't mount to /mnt/LABEL, so try mounting to /mnt/discDN_PN */
566 sprintf(mountpoint, "%s/%s", MOUNT_ROOT, pt_name);
567 ret = mount_r(dev_name, mountpoint, type);
568 done:
569 if (ret == MOUNT_VAL_RONLY || ret == MOUNT_VAL_RW)
571 /* Run user *.autorun and post-mount scripts if any. */
572 run_userfile(mountpoint, ".autorun", mountpoint, 3);
573 if (nvram_get_int("usb_automount"))
574 run_nvscript("script_usbmount", mountpoint, 3);
576 return (ret == MOUNT_VAL_RONLY || ret == MOUNT_VAL_RW);
580 #if 0 /* LINUX26 */
583 * Finds SCSI Host number. Returns the host number >=0 if found, or (-1) otherwise.
584 * The name and host number of scsi block device in kernel 2.6 (for attached devices) can be found as
585 * /sys($DEVPATH)/host<host_no>/target<*>/<id>/block:[sda|sdb|...]
586 * where $DEVPATH is passed to hotplug events, and looks like
587 * /devices/pci0000:00/0000:00:04.1/usb1/1-1/1-1:1.2
589 * For printers this function finds a minor assigned to a printer
590 * /sys($DEVPATH)/usb:lp[0|1|2|...]
592 int find_dev_host(const char *devpath)
594 DIR *usb_devpath;
595 struct dirent *dp;
596 char buf[256];
597 int host = -1; /* Scsi Host */
599 sprintf(buf, "/sys%s", devpath);
600 if ((usb_devpath = opendir(buf))) {
601 while ((dp = readdir(usb_devpath))) {
602 errno = 0;
603 if (strncmp(dp->d_name, "host", 4) == 0) {
604 host = strtol(dp->d_name + 4, (char **)NULL, 10);
605 if (errno)
606 host = -1;
607 else
608 break;
610 else if (strncmp(dp->d_name, "usb:lp", 6) == 0) {
611 host = strtol(dp->d_name + 6, (char **)NULL, 10);
612 if (errno)
613 host = -1;
614 else
615 break;
617 else
618 continue;
620 closedir(usb_devpath);
622 return (host);
625 #endif /* LINUX26 */
627 int dir_is_mountpoint(const char *root, const char *dir)
629 char path[256];
630 struct stat sb;
631 int thisdev;
633 snprintf(path, sizeof(path), "%s%s%s", root ? : "", root ? "/" : "", dir);
635 /* Check if this is a directory */
636 sb.st_mode = S_IFDIR; /* failsafe */
637 stat(path, &sb);
639 if (S_ISDIR(sb.st_mode)) {
641 /* If this dir & its parent dir are on the same device, it is not a mountpoint */
642 strcat(path, "/.");
643 stat(path, &sb);
644 thisdev = sb.st_dev;
645 strcat(path, ".");
646 ++sb.st_dev; /* failsafe */
647 stat(path, &sb);
649 return (thisdev != sb.st_dev);
652 return 0;
655 /* Mount or unmount all partitions on this controller.
656 * Parameter: action_add:
657 * 0 = unmount
658 * >0 = mount only if automount config option is enabled.
659 * <0 = mount regardless of config option.
661 void hotplug_usb_storage_device(int host_no, int action_add, uint flags)
663 if (!nvram_get_int("usb_enable"))
664 return;
665 _dprintf("%s: host %d action: %d\n", __FUNCTION__, host_no, action_add);
667 if (action_add) {
668 if (nvram_get_int("usb_storage") && (nvram_get_int("usb_automount") || action_add < 0)) {
669 /* Do not probe the device here. It's either initiated by user,
670 * or hotplug_usb() already did.
672 if (exec_for_host(host_no, 0x00, flags, mount_partition)) {
673 restart_nas_services(0, 1); // restart all NAS applications
677 else {
678 if (nvram_get_int("usb_storage") || ((flags & EFH_USER) == 0)) {
679 /* When unplugged, unmount the device even if
680 * usb storage is disabled in the GUI.
682 exec_for_host(host_no, (flags & EFH_USER) ? 0x00 : 0x02, flags, umount_partition);
683 /* Restart NAS applications (they could be killed by umount_mountpoint),
684 * or just re-read the configuration.
686 restart_nas_services(0, 1);
692 /* This gets called at reboot or upgrade. The system is stopping. */
693 void remove_storage_main(int shutdn)
695 if (shutdn)
696 restart_nas_services(1, 0);
697 /* Unmount all partitions */
698 exec_for_host(-1, 0x02, shutdn ? EFH_SHUTDN : 0, umount_partition);
702 /*******
703 * All the complex locking & checking code was removed when the kernel USB-storage
704 * bugs were fixed.
705 * The crash bug was with overlapped I/O to different USB drives, not specifically
706 * with mount processing.
708 * And for USB devices that are slow to come up. The kernel now waits until the
709 * USB drive has settled, and it correctly reads the partition table before calling
710 * the hotplug agent.
712 * The kernel patch was cleaning up data structures on an unplug. It
713 * needs to wait until the disk is unmounted. We have 20 seconds to do
714 * the unmounts.
715 *******/
718 /* Plugging or removing usb device
720 * On an occurrance, multiple hotplug events may be fired off.
721 * For example, if a hub is plugged or unplugged, an event
722 * will be generated for everything downstream of it, plus one for
723 * the hub itself. These are fired off simultaneously, not serially.
724 * This means that many many hotplug processes will be running at
725 * the same time.
727 * The hotplug event generated by the kernel gives us several pieces
728 * of information:
729 * PRODUCT is vendorid/productid/rev#.
730 * DEVICE is /proc/bus/usb/bus#/dev#
731 * ACTION is add or remove
732 * SCSI_HOST is the host (controller) number (this relies on the custom kernel patch)
734 * Note that when we get a hotplug add event, the USB susbsystem may
735 * or may not have yet tried to read the partition table of the
736 * device. For a new controller that has never been seen before,
737 * generally yes. For a re-plug of a controller that has been seen
738 * before, generally no.
740 * On a remove, the partition info has not yet been expunged. The
741 * partitions show up as /dev/discs/disc#/part#, and /proc/partitions.
742 * It appears that doing a "stat" for a non-existant partition will
743 * causes the kernel to re-validate the device and update the
744 * partition table info. However, it won't re-validate if the disc is
745 * mounted--you'll get a "Device busy for revalidation (usage=%d)" in
746 * syslog.
748 * The $INTERFACE is "class/subclass/protocol"
749 * Some interesting classes:
750 * 8 = mass storage
751 * 7 = printer
752 * 3 = HID. 3/1/2 = mouse.
753 * 6 = still image (6/1/1 = Digital camera Camera)
754 * 9 = Hub
755 * 255 = scanner (255/255/255)
757 * Observed:
758 * Hub seems to have no INTERFACE (null), and TYPE of "9/0/0"
759 * Flash disk seems to have INTERFACE of "8/6/80", and TYPE of "0/0/0"
761 * When a hub is unplugged, a hotplug event is generated for it and everything
762 * downstream from it. You cannot depend on getting these events in any
763 * particular order, since there will be many hotplug programs all fired off
764 * at almost the same time.
765 * On a remove, don't try to access the downstream devices right away, give the
766 * kernel time to finish cleaning up all the data structures, which will be
767 * in the process of being torn down.
769 * On the initial plugin, the first time the kernel usb-storage subsystem sees
770 * the host (identified by GUID), it automatically reads the partition table.
771 * On subsequent plugins, it does not.
773 * Special values for Web Administration to unmount or remount
774 * all partitions of the host:
775 * INTERFACE=TOMATO/...
776 * ACTION=add/remove
777 * SCSI_HOST=<host_no>
778 * If host_no is negative, we unmount all partions of *all* hosts.
780 void hotplug_usb(void)
782 int add;
783 int host = -1;
784 char *interface = getenv("INTERFACE");
785 char *action = getenv("ACTION");
786 char *product = getenv("PRODUCT");
787 #ifdef LINUX26
788 char *device = getenv("DEVICENAME");
789 int is_block = strcmp(getenv("SUBSYSTEM") ? : "", "block") == 0;
790 #else
791 char *device = getenv("DEVICE");
792 #endif
793 char *scsi_host = getenv("SCSI_HOST");
795 _dprintf("%s hotplug INTERFACE=%s ACTION=%s PRODUCT=%s HOST=%s DEVICE=%s\n",
796 getenv("SUBSYSTEM") ? : "USB", interface, action, product, scsi_host, device);
798 if (!nvram_get_int("usb_enable")) return;
799 #ifdef LINUX26
800 if (!action || ((!interface || !product) && !is_block))
801 #else
802 if (!interface || !action || !product) /* Hubs bail out here. */
803 #endif
804 return;
806 if (scsi_host)
807 host = atoi(scsi_host);
809 if (!wait_action_idle(10)) return;
811 add = (strcmp(action, "add") == 0);
812 if (add && (strncmp(interface ? : "", "TOMATO/", 7) != 0)) {
813 #ifdef LINUX26
814 if (!is_block && device)
815 #endif
816 syslog(LOG_DEBUG, "Attached USB device %s [INTERFACE=%s PRODUCT=%s]",
817 device, interface, product);
818 #ifndef LINUX26
819 /* To allow automount to be blocked on startup.
820 * In kernel 2.6 we still need to serialize mount/umount calls -
821 * so the lock is down below in the "block" hotplug processing.
823 file_unlock(file_lock("usb"));
824 #endif
827 if (strncmp(interface ? : "", "TOMATO/", 7) == 0) { /* web admin */
828 if (scsi_host == NULL)
829 host = atoi(product); // for backward compatibility
830 /* If host is negative, unmount all partitions of *all* hosts.
831 * If host == -1, execute "soft" unmount (do not kill NAS apps, no "lazy" umount).
832 * If host == -2, run "hard" unmount, as if the drive is unplugged.
833 * This feature can be used in custom scripts as following:
835 * # INTERFACE=TOMATO/1 ACTION=remove PRODUCT=-1 SCSI_HOST=-1 hotplug usb
837 * PRODUCT is required to pass the env variables verification.
839 /* Unmount or remount all partitions of the host. */
840 hotplug_usb_storage_device(host < 0 ? -1 : host, add ? -1 : 0,
841 host == -2 ? 0 : EFH_USER);
843 #ifdef LINUX26
844 else if (is_block && strcmp(getenv("MAJOR") ? : "", "8") == 0 && strcmp(getenv("PHYSDEVBUS") ? : "", "scsi") == 0) {
845 /* scsi partition */
846 char devname[64];
847 int lock;
849 sprintf(devname, "/dev/%s", device);
850 lock = file_lock("usb");
851 if (add) {
852 if (nvram_get_int("usb_storage") && nvram_get_int("usb_automount")) {
853 int minor = atoi(getenv("MINOR") ? : "0");
854 if ((minor % 16) == 0 && !is_no_partition(device)) {
855 /* This is a disc, and not a "no-partition" device,
856 * like APPLE iPOD shuffle. We can't mount it.
858 return;
860 if (mount_partition(devname, host, NULL, device, EFH_HP_ADD)) {
861 restart_nas_services(0, 1); // restart all NAS applications
865 else {
866 /* When unplugged, unmount the device even if usb storage is disabled in the GUI */
867 umount_partition(devname, host, NULL, device, EFH_HP_REMOVE);
868 /* Restart NAS applications (they could be killed by umount_mountpoint),
869 * or just re-read the configuration.
871 restart_nas_services(0, 1);
873 file_unlock(lock);
875 #endif
876 else if (strncmp(interface ? : "", "8/", 2) == 0) { /* usb storage */
877 run_nvscript("script_usbhotplug", NULL, 2);
878 #ifndef LINUX26
879 hotplug_usb_storage_device(host, add, (add ? EFH_HP_ADD : EFH_HP_REMOVE) | (host < 0 ? EFH_HUNKNOWN : 0));
880 #endif
882 else { /* It's some other type of USB device, not storage. */
883 #ifdef LINUX26
884 if (is_block) return;
885 #endif
886 /* Do nothing. The user's hotplug script must do it all. */
887 run_nvscript("script_usbhotplug", NULL, 2);