6bde4a8e9c856a68e01edc6f8e8a21e93b84702b
[linux-flexiantxendom0-3.2.10.git] / arch / um / drivers / port_kern.c
1 /* 
2  * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include "linux/list.h"
7 #include "linux/sched.h"
8 #include "linux/slab.h"
9 #include "linux/interrupt.h"
10 #include "linux/irq.h"
11 #include "linux/spinlock.h"
12 #include "linux/errno.h"
13 #include "asm/semaphore.h"
14 #include "asm/errno.h"
15 #include "kern_util.h"
16 #include "kern.h"
17 #include "irq_user.h"
18 #include "irq_kern.h"
19 #include "port.h"
20 #include "init.h"
21 #include "os.h"
22
23 struct port_list {
24         struct list_head list;
25         int has_connection;
26         struct semaphore sem;
27         int port;
28         int fd;
29         spinlock_t lock;
30         struct list_head pending;
31         struct list_head connections;
32 };
33
34 struct port_dev {
35         struct port_list *port;
36         int helper_pid;
37         int telnetd_pid;
38 };
39
40 struct connection {
41         struct list_head list;
42         int fd;
43         int helper_pid;
44         int socket[2];
45         int telnetd_pid;
46         struct port_list *port;
47 };
48
49 static irqreturn_t pipe_interrupt(int irq, void *data, struct pt_regs *regs)
50 {
51         struct connection *conn = data;
52         int fd;
53
54         fd = os_rcv_fd(conn->socket[0], &conn->helper_pid);
55         if(fd < 0){
56                 if(fd == -EAGAIN)
57                         return(IRQ_NONE);
58
59                 printk(KERN_ERR "pipe_interrupt : os_rcv_fd returned %d\n", 
60                        -fd);
61                 os_close_file(conn->fd);
62         }
63
64         list_del(&conn->list);
65
66         conn->fd = fd;
67         list_add(&conn->list, &conn->port->connections);
68
69         up(&conn->port->sem);
70         return(IRQ_HANDLED);
71 }
72
73 static int port_accept(struct port_list *port)
74 {
75         struct connection *conn;
76         int fd, socket[2], pid, ret = 0;
77
78         fd = port_connection(port->fd, socket, &pid);
79         if(fd < 0){
80                 if(fd != -EAGAIN)
81                         printk(KERN_ERR "port_accept : port_connection "
82                                "returned %d\n", -fd);
83                 goto out;
84         }
85
86         conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
87         if(conn == NULL){
88                 printk(KERN_ERR "port_accept : failed to allocate "
89                        "connection\n");
90                 goto out_close;
91         }
92         *conn = ((struct connection) 
93                 { .list         = LIST_HEAD_INIT(conn->list),
94                   .fd           = fd,
95                   .socket       = { socket[0], socket[1] },
96                   .telnetd_pid  = pid,
97                   .port         = port });
98
99         if(um_request_irq(TELNETD_IRQ, socket[0], IRQ_READ, pipe_interrupt, 
100                           SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM, 
101                           "telnetd", conn)){
102                 printk(KERN_ERR "port_accept : failed to get IRQ for "
103                        "telnetd\n");
104                 goto out_free;
105         }
106
107         list_add(&conn->list, &port->pending);
108         ret = 1;
109         goto out;
110
111  out_free:
112         kfree(conn);
113  out_close:
114         os_close_file(fd);
115         if(pid != -1) 
116                 os_kill_process(pid, 1);
117  out:
118         return(ret);
119
120
121 DECLARE_MUTEX(ports_sem);
122 struct list_head ports = LIST_HEAD_INIT(ports);
123
124 void port_work_proc(void *unused)
125 {
126         struct port_list *port;
127         struct list_head *ele;
128         unsigned long flags;
129
130         local_irq_save(flags);
131         list_for_each(ele, &ports){
132                 port = list_entry(ele, struct port_list, list);
133                 if(!port->has_connection)
134                         continue;
135                 reactivate_fd(port->fd, ACCEPT_IRQ);
136                 while(port_accept(port)) ;
137                 port->has_connection = 0;
138         }
139         local_irq_restore(flags);
140 }
141
142 DECLARE_WORK(port_work, port_work_proc, NULL);
143
144 static irqreturn_t port_interrupt(int irq, void *data, struct pt_regs *regs)
145 {
146         struct port_list *port = data;
147
148         port->has_connection = 1;
149         schedule_work(&port_work);
150         return(IRQ_HANDLED);
151
152
153 void *port_data(int port_num)
154 {
155         struct list_head *ele;
156         struct port_list *port;
157         struct port_dev *dev = NULL;
158         int fd;
159
160         down(&ports_sem);
161         list_for_each(ele, &ports){
162                 port = list_entry(ele, struct port_list, list);
163                 if(port->port == port_num) goto found;
164         }
165         port = kmalloc(sizeof(struct port_list), GFP_KERNEL);
166         if(port == NULL){
167                 printk(KERN_ERR "Allocation of port list failed\n");
168                 goto out;
169         }
170
171         fd = port_listen_fd(port_num);
172         if(fd < 0){
173                 printk(KERN_ERR "binding to port %d failed, errno = %d\n",
174                        port_num, -fd);
175                 goto out_free;
176         }
177         if(um_request_irq(ACCEPT_IRQ, fd, IRQ_READ, port_interrupt, 
178                           SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM, "port",
179                           port)){
180                 printk(KERN_ERR "Failed to get IRQ for port %d\n", port_num);
181                 goto out_close;
182         }
183
184         *port = ((struct port_list) 
185                 { .list                 = LIST_HEAD_INIT(port->list),
186                   .has_connection       = 0,
187                   .sem                  = __SEMAPHORE_INITIALIZER(port->sem, 
188                                                                   0),
189                   .lock                 = SPIN_LOCK_UNLOCKED,
190                   .port                 = port_num,
191                   .fd                   = fd,
192                   .pending              = LIST_HEAD_INIT(port->pending),
193                   .connections          = LIST_HEAD_INIT(port->connections) });
194         list_add(&port->list, &ports);
195
196  found:
197         dev = kmalloc(sizeof(struct port_dev), GFP_KERNEL);
198         if(dev == NULL){
199                 printk(KERN_ERR "Allocation of port device entry failed\n");
200                 goto out;
201         }
202
203         *dev = ((struct port_dev) { .port               = port,
204                                     .helper_pid         = -1,
205                                     .telnetd_pid        = -1 });
206         goto out;
207
208  out_free:
209         kfree(port);
210  out_close:
211         os_close_file(fd);
212  out:
213         up(&ports_sem);
214         return(dev);
215 }
216
217 int port_wait(void *data)
218 {
219         struct port_dev *dev = data;
220         struct connection *conn;
221         struct port_list *port = dev->port;
222         int fd;
223
224         while(1){
225                 if(down_interruptible(&port->sem))
226                         return(-ERESTARTSYS);
227
228                 spin_lock(&port->lock);
229
230                 conn = list_entry(port->connections.next, struct connection, 
231                                   list);
232                 list_del(&conn->list);
233                 spin_unlock(&port->lock);
234
235                 os_shutdown_socket(conn->socket[0], 1, 1);
236                 os_close_file(conn->socket[0]);
237                 os_shutdown_socket(conn->socket[1], 1, 1);
238                 os_close_file(conn->socket[1]); 
239
240                 /* This is done here because freeing an IRQ can't be done
241                  * within the IRQ handler.  So, pipe_interrupt always ups
242                  * the semaphore regardless of whether it got a successful
243                  * connection.  Then we loop here throwing out failed 
244                  * connections until a good one is found.
245                  */
246                 free_irq(TELNETD_IRQ, conn);
247
248                 if(conn->fd >= 0) break;
249                 os_close_file(conn->fd);
250                 kfree(conn);
251         }
252
253         fd = conn->fd;
254         dev->helper_pid = conn->helper_pid;
255         dev->telnetd_pid = conn->telnetd_pid;
256         kfree(conn);
257
258         return(fd);
259 }
260
261 void port_remove_dev(void *d)
262 {
263         struct port_dev *dev = d;
264
265         if(dev->helper_pid != -1)
266                 os_kill_process(dev->helper_pid, 0);
267         if(dev->telnetd_pid != -1)
268                 os_kill_process(dev->telnetd_pid, 1);
269         dev->helper_pid = -1;
270         dev->telnetd_pid = -1;
271 }
272
273 void port_kern_free(void *d)
274 {
275         struct port_dev *dev = d;
276
277         port_remove_dev(dev);
278         kfree(dev);
279 }
280
281 static void free_port(void)
282 {
283         struct list_head *ele;
284         struct port_list *port;
285
286         list_for_each(ele, &ports){
287                 port = list_entry(ele, struct port_list, list);
288                 free_irq_by_fd(port->fd);
289                 os_close_file(port->fd);
290         }
291 }
292
293 __uml_exitcall(free_port);
294
295 /*
296  * Overrides for Emacs so that we follow Linus's tabbing style.
297  * Emacs will notice this stuff at the end of the file and automatically
298  * adjust the settings for this buffer only.  This must remain at the end
299  * of the file.
300  * ---------------------------------------------------------------------------
301  * Local variables:
302  * c-file-style: "linux"
303  * End:
304  */