Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / pccard / pccardd / server.c
1 /*
2  *      pccardd UNIX-domain socket interface
3  *      Copyright (C) 1996 by Tatsumi Hosokawa <hosokawa@mt.cs.keio.ac.jp>
4  *
5  * $Id: server.c,v 1.3 1999/02/07 08:02:44 kuriyama Exp $
6  *
7  * $FreeBSD: src/usr.sbin/pccard/pccardd/server.c,v 1.2.2.1 2000/08/26 15:49:48 ume Exp $
8  * $DragonFly: src/usr.sbin/pccard/pccardd/Attic/server.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
9  */
10
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/ioctl.h>
15 #include <sys/socket.h>
16 #include <sys/un.h>
17 #include <signal.h>
18 #include <setjmp.h>
19
20 #include "cardd.h"
21
22 static void
23 cardnum(char *buf)
24 {
25         int     i = 0;
26         struct slot *sp;
27
28         for (sp = slots; sp; sp = sp->next)
29                 i++;
30         if (i > MAXSLOT)
31                 i = MAXSLOT;
32         sprintf(buf, "%2d", i);
33 }
34
35 static struct slot *
36 find_slot(int slot)
37 {
38         struct slot *sp;
39
40         /* Search the list until we find the slot or get to the end */
41         for (sp = slots; sp && sp->slot != slot; sp = sp->next)
42                 continue;
43
44         return ( sp );
45 }
46
47 static void
48 cardname(char *buf, int slot)
49 {
50         struct slot *sp;
51         char   *manuf, *vers, *drv, *stat;
52
53         /* Look for the slot */
54         if ( (sp = find_slot(slot)) == NULL)
55                 return;
56
57         /* Fill in the information in the buff */
58         if (sp->cis) {
59
60                 manuf = sp->cis->manuf;
61                 vers = sp->cis->vers;
62                 if (sp->config && sp->config->driver &&
63                     sp->config->driver->name)
64                         drv = sp->config->driver->name;
65                 else
66                         drv = "";
67         } else
68                 manuf = vers = drv = "";
69
70         switch (sp->state) {
71         case empty:
72                 stat = "0";
73                 break;
74         case filled:
75                 stat = "1";
76                 break;
77         case inactive:
78                 stat = "2";
79                 break;
80         default:
81                 stat = "9";
82         }
83         sprintf(buf, "%d~%s~%s~%s~%s", slot, manuf, vers, drv, stat);
84 }
85
86 static void
87 cardpwr(int slot, int pwon)
88 {
89         struct slot *sp;
90
91         /* Look for the slot */
92         if ( (sp = find_slot(slot)) == NULL)
93                 return;
94
95         if (ioctl(sp->fd, PIOCSVIR, &pwon) < 0)
96                 logerr("invaild arguments for cardpwr");
97 }
98
99 static int sock = 0;
100 static int slen = 0;
101 static struct sockaddr_un sun;
102
103 void
104 set_socket(int s)
105 {
106         sock = s;
107 }
108
109 void
110 stat_changed(struct slot *sp)
111 {
112         int     len;
113         char    buf[512];
114
115         if (!slen)
116                 return;
117
118         cardname(buf, sp->slot);
119         len = strlen(buf);
120         if (sendto(sock, buf, len, 0, (struct sockaddr *) & sun, slen) != len) {
121                 logerr("sendto failed");
122                 slen = 0;
123         }
124 }
125
126 void
127 process_client(void)
128 {
129         char    buf[512], obuf[512];
130         int     len;
131         int     snum;
132
133         if (!sock)
134                 return;
135         slen = sizeof(sun);
136         len = recvfrom(sock, buf, sizeof(buf),
137                        0, (struct sockaddr *)&sun, &slen);
138         if (len < 0)
139                 logerr("recvfrom failed");
140         buf[len] = '\0';
141         obuf[0] = '\0';
142         switch (buf[0]) {       /* Protocol implementation */
143         case 'S':       /* How many slots? */
144                 cardnum(obuf);
145                 break;
146         case 'N':       /* Card name request */
147                 sscanf(buf + 1, "%d", &snum);
148                 if (snum >= 0 && snum <= MAXSLOT)
149                         cardname(obuf, snum);
150                 else 
151                         logerr("Illegal slot requests for N command");
152                 break;
153         case 'P':       /* Virtual insertion request */
154                 sscanf(buf + 1, "%d", &snum);
155                 if (snum >= 0 && snum <= MAXSLOT) {
156                         logmsg("slot %d: spring has come", snum);
157                         cardpwr(snum, 1);
158                 } else
159                         logerr("Illegal slot requests for P command");
160                 break;
161         case 'Q':       /* Virtual removal request */
162                 sscanf(buf + 1, "%d", &snum);
163                 if (snum >= 0 && snum <= MAXSLOT) {
164                         logmsg("slot %d: hibernation", snum);
165                         cardpwr(snum, 0);
166                 } else
167                         logerr("Illegal slot requests for Q command");
168                 break;
169         default:
170                 logerr("Unknown control message from socket");
171                 break;
172         }
173         len = strlen(obuf);
174         if (len) {
175                 if (sendto(sock, obuf, len, 0, (struct sockaddr *)&sun, slen)
176                     != len) {
177                         logerr("sendto failed");
178                         slen = 0;
179                 }
180         } else if (sendto(sock, 0, 0, 0, (struct sockaddr *)&sun, slen)
181                    != len) {
182                         logerr("sendto failed");
183                         slen = 0;
184         }
185 }