xref: /plan9/sys/src/cmd/usb/ether/main.c (revision bfb6eab9346d861b5f68a2b1af55a1768a8fe25b)
1 /*
2  * usb/ether - usb ethernet adapter.
3  * BUG: This should use /dev/etherfile to
4  * use the kernel ether device code.
5  */
6 #include <u.h>
7 #include <libc.h>
8 #include <fcall.h>
9 #include <thread.h>
10 #include "usb.h"
11 #include "usbfs.h"
12 #include "ether.h"
13 
14 enum
15 {
16 	Arglen = 80,
17 };
18 
19 static void
20 usage(void)
21 {
22 	fprint(2, "usage: %s [-Dd] [-m mnt] [-s srv] [dev...]\n", argv0);
23 	threadexitsall("usage");
24 }
25 
26 /*
27  * Ether devices may be weird.
28  * Be optimistic and try to use any communication
29  * device or one of the `vendor specific class' devices
30  * that we know are ethernets.
31  */
32 static int
33 matchether(char *info, void*)
34 {
35 	Cinfo *ip;
36 	char buf[50];
37 
38 	/*
39 	 * I have an ether reporting comms.0.0
40 	 */
41 	if(strstr(info, "comms") != nil)
42 		return 0;
43 	for(ip = cinfo; ip->vid != 0; ip++){
44 		snprint(buf, sizeof(buf), "vid %#06x did %#06x", ip->vid, ip->did);
45 		if(strstr(info, buf) != nil)
46 			return 0;
47 	}
48 	return -1;
49 }
50 
51 void
52 threadmain(int argc, char **argv)
53 {
54 	char args[Arglen];
55 	char *as;
56 	char *ae;
57 	char *srv;
58 	char *mnt;
59 
60 	srv = nil;
61 	mnt = "/net";
62 
63 	quotefmtinstall();
64 	ae = args+sizeof(args);
65 	as = seprint(args, ae, "ether");
66 	ARGBEGIN{
67 	case 'D':
68 		usbfsdebug++;
69 		break;
70 	case 'd':
71 		usbdebug++;
72 		as = seprint(as, ae, " -d");
73 		break;
74 	case 'm':
75 		mnt = EARGF(usage());
76 		break;
77 	case 's':
78 		srv = EARGF(usage());
79 	default:
80 		usage();
81 	}ARGEND
82 
83 	rfork(RFNOTEG);
84 	threadsetgrp(threadid());
85 	fmtinstall('U', Ufmt);
86 	usbfsinit(srv, mnt, &usbdirfs, MAFTER|MCREATE);
87 	startdevs(args, argv, argc, matchether, nil, ethermain);
88 	threadexits(nil);
89 }
90