kernel.org linux-2.6.10
[linux-2.6.git] / drivers / misc / ibmasm / ibmasmfs.c
1 /*
2  * IBM ASM Service Processor Device Driver
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Copyright (C) IBM Corporation, 2004
19  *
20  * Author: Max Asböck <amax@us.ibm.com> 
21  *
22  */
23
24 /*
25  * Parts of this code are based on an article by Jonathan Corbet 
26  * that appeared in Linux Weekly News.
27  */
28
29
30 /*
31  * The IBMASM file virtual filesystem. It creates the following hierarchy
32  * dymamically when mounted from user space:
33  *
34  *    /ibmasm
35  *    |-- 0
36  *    |   |-- command
37  *    |   |-- event
38  *    |   |-- reverse_heartbeat
39  *    |   `-- remote_video
40  *    |       |-- connected
41  *    |       |-- depth
42  *    |       |-- events
43  *    |       |-- height
44  *    |       `-- width
45  *    .
46  *    .
47  *    .
48  *    `-- n
49  *        |-- command
50  *        |-- event
51  *        |-- reverse_heartbeat
52  *        `-- remote_video
53  *            |-- connected
54  *            |-- depth
55  *            |-- events
56  *            |-- height
57  *            `-- width
58  *
59  * For each service processor the following files are created:
60  *
61  * command: execute dot commands
62  *      write: execute a dot command on the service processor
63  *      read: return the result of a previously executed dot command
64  *
65  * events: listen for service processor events
66  *      read: sleep (interruptible) until an event occurs
67  *      write: wakeup sleeping event listener
68  *
69  * reverse_heartbeat: send a heartbeat to the service processor
70  *      read: sleep (interruptible) until the reverse heartbeat fails
71  *      write: wakeup sleeping heartbeat listener
72  *
73  * remote_video/width
74  * remote_video/height
75  * remote_video/width: control remote display settings
76  *      write: set value
77  *      read: read value
78  *
79  * remote_video/connected
80  *      read: return "1" if web browser VNC java applet is connected, 
81  *              "0" otherwise
82  *
83  * remote_video/events
84  *      read: sleep until a remote mouse or keyboard event occurs, then return
85  *              then event.
86  */
87
88 #include <linux/fs.h>
89 #include <linux/pagemap.h>
90 #include <asm/uaccess.h>
91 #include <asm/io.h>
92 #include "ibmasm.h"
93 #include "remote.h"
94 #include "dot_command.h"
95
96 #define IBMASMFS_MAGIC 0x66726f67
97
98 static LIST_HEAD(service_processors);
99
100 static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode);
101 static void ibmasmfs_create_files (struct super_block *sb, struct dentry *root);
102 static int ibmasmfs_fill_super (struct super_block *sb, void *data, int silent);
103
104
105 static struct super_block *ibmasmfs_get_super(struct file_system_type *fst,
106                         int flags, const char *name, void *data)
107 {
108         return get_sb_single(fst, flags, data, ibmasmfs_fill_super);
109 }
110
111 static struct super_operations ibmasmfs_s_ops = {
112         .statfs         = simple_statfs,
113         .drop_inode     = generic_delete_inode,
114 };
115
116 static struct file_operations *ibmasmfs_dir_ops = &simple_dir_operations;
117
118 static struct file_system_type ibmasmfs_type = {
119         .owner          = THIS_MODULE,
120         .name           = "ibmasmfs",
121         .get_sb         = ibmasmfs_get_super,
122         .kill_sb        = kill_litter_super,
123 };
124
125 static int ibmasmfs_fill_super (struct super_block *sb, void *data, int silent)
126 {
127         struct inode *root;
128         struct dentry *root_dentry;
129
130         sb->s_blocksize = PAGE_CACHE_SIZE;
131         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
132         sb->s_magic = IBMASMFS_MAGIC;
133         sb->s_op = &ibmasmfs_s_ops;
134
135         root = ibmasmfs_make_inode (sb, S_IFDIR | 0500);
136         if (!root)
137                 return -ENOMEM;
138
139         root->i_op = &simple_dir_inode_operations;
140         root->i_fop = ibmasmfs_dir_ops;
141
142         root_dentry = d_alloc_root(root);
143         if (!root_dentry) {
144                 iput(root);
145                 return -ENOMEM;
146         }
147         sb->s_root = root_dentry;
148
149         ibmasmfs_create_files(sb, root_dentry);
150         return 0;
151 }
152
153 static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode)
154 {
155         struct inode *ret = new_inode(sb);
156
157         if (ret) {
158                 ret->i_mode = mode;
159                 ret->i_uid = ret->i_gid = 0;
160                 ret->i_blksize = PAGE_CACHE_SIZE;
161                 ret->i_blocks = 0;
162                 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
163         }
164         return ret;
165 }
166
167 static struct dentry *ibmasmfs_create_file (struct super_block *sb,
168                         struct dentry *parent,
169                         const char *name,
170                         struct file_operations *fops,
171                         void *data,
172                         int mode)
173 {
174         struct dentry *dentry;
175         struct inode *inode;
176
177         dentry = d_alloc_name(parent, name);
178         if (!dentry)
179                 return NULL;
180
181         inode = ibmasmfs_make_inode(sb, S_IFREG | mode);
182         if (!inode) {
183                 dput(dentry);
184                 return NULL;
185         }
186
187         inode->i_fop = fops;
188         inode->u.generic_ip = data;
189
190         d_add(dentry, inode);
191         return dentry;
192 }
193
194 static struct dentry *ibmasmfs_create_dir (struct super_block *sb,
195                                 struct dentry *parent,
196                                 const char *name)
197 {
198         struct dentry *dentry;
199         struct inode *inode;
200
201         dentry = d_alloc_name(parent, name);
202         if (!dentry)
203                 return NULL;
204
205         inode = ibmasmfs_make_inode(sb, S_IFDIR | 0500);
206         if (!inode) {
207                 dput(dentry);
208                 return NULL;
209         }
210
211         inode->i_op = &simple_dir_inode_operations;
212         inode->i_fop = ibmasmfs_dir_ops;
213
214         d_add(dentry, inode);
215         return dentry;
216 }
217
218 int ibmasmfs_register(void)
219 {
220         return register_filesystem(&ibmasmfs_type);
221 }
222
223 void ibmasmfs_unregister(void)
224 {
225         unregister_filesystem(&ibmasmfs_type);
226 }
227
228 void ibmasmfs_add_sp(struct service_processor *sp)
229 {
230         list_add(&sp->node, &service_processors);
231 }
232
233 /* struct to save state between command file operations */
234 struct ibmasmfs_command_data {
235         struct service_processor        *sp;
236         struct command                  *command;
237 };
238
239 /* struct to save state between event file operations */
240 struct ibmasmfs_event_data {
241         struct service_processor        *sp;
242         struct event_reader             reader;
243         int                             active;
244 };
245
246 /* struct to save state between reverse heartbeat file operations */
247 struct ibmasmfs_heartbeat_data {
248         struct service_processor        *sp;
249         struct reverse_heartbeat        heartbeat;
250         int                             active;
251 };
252
253 static int command_file_open(struct inode *inode, struct file *file)
254 {
255         struct ibmasmfs_command_data *command_data;
256
257         if (!inode->u.generic_ip)
258                 return -ENODEV;
259
260         command_data = kmalloc(sizeof(struct ibmasmfs_command_data), GFP_KERNEL);
261         if (!command_data)
262                 return -ENOMEM;
263
264         command_data->command = NULL;
265         command_data->sp = inode->u.generic_ip;
266         file->private_data = command_data;
267         return 0;
268 }
269
270 static int command_file_close(struct inode *inode, struct file *file)
271 {
272         struct ibmasmfs_command_data *command_data = file->private_data;
273
274         if (command_data->command)
275                 command_put(command_data->command);     
276
277         kfree(command_data);
278         return 0;
279 }
280
281 static ssize_t command_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
282 {
283         struct ibmasmfs_command_data *command_data = file->private_data;
284         struct command *cmd;
285         int len;
286         unsigned long flags;
287
288         if (*offset < 0)
289                 return -EINVAL;
290         if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
291                 return 0;
292         if (*offset != 0)
293                 return 0;
294
295         spin_lock_irqsave(&command_data->sp->lock, flags);
296         cmd = command_data->command;
297         if (cmd == NULL) {
298                 spin_unlock_irqrestore(&command_data->sp->lock, flags);
299                 return 0;
300         }
301         command_data->command = NULL;
302         spin_unlock_irqrestore(&command_data->sp->lock, flags);
303
304         if (cmd->status != IBMASM_CMD_COMPLETE) {
305                 command_put(cmd);
306                 return -EIO;
307         }
308         len = min(count, cmd->buffer_size);
309         if (copy_to_user(buf, cmd->buffer, len)) {
310                 command_put(cmd);
311                 return -EFAULT;
312         }
313         command_put(cmd);
314
315         return len;
316 }
317
318 static ssize_t command_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
319 {
320         struct ibmasmfs_command_data *command_data = file->private_data;
321         struct command *cmd;
322         unsigned long flags;
323
324         if (*offset < 0)
325                 return -EINVAL;
326         if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
327                 return 0;
328         if (*offset != 0)
329                 return 0;
330
331         /* commands are executed sequentially, only one command at a time */
332         if (command_data->command)
333                 return -EAGAIN;
334
335         cmd = ibmasm_new_command(count);
336         if (!cmd)
337                 return -ENOMEM;
338
339         if (copy_from_user(cmd->buffer, ubuff, count)) {
340                 command_put(cmd);
341                 return -EFAULT;
342         }
343
344         spin_lock_irqsave(&command_data->sp->lock, flags);
345         if (command_data->command) {
346                 spin_unlock_irqrestore(&command_data->sp->lock, flags);
347                 command_put(cmd);
348                 return -EAGAIN;
349         }
350         command_data->command = cmd;
351         spin_unlock_irqrestore(&command_data->sp->lock, flags);
352
353         ibmasm_exec_command(command_data->sp, cmd);
354         ibmasm_wait_for_response(cmd, get_dot_command_timeout(cmd->buffer));
355
356         return count;
357 }
358
359 static int event_file_open(struct inode *inode, struct file *file)
360 {
361         struct ibmasmfs_event_data *event_data;
362         struct service_processor *sp; 
363
364         if (!inode->u.generic_ip)
365                 return -ENODEV;
366
367         sp = inode->u.generic_ip;
368
369         event_data = kmalloc(sizeof(struct ibmasmfs_event_data), GFP_KERNEL);
370         if (!event_data)
371                 return -ENOMEM;
372
373         ibmasm_event_reader_register(sp, &event_data->reader);
374
375         event_data->sp = sp;
376         file->private_data = event_data;
377         return 0;
378 }
379
380 static int event_file_close(struct inode *inode, struct file *file)
381 {
382         struct ibmasmfs_event_data *event_data = file->private_data;
383
384         ibmasm_event_reader_unregister(event_data->sp, &event_data->reader);
385         kfree(event_data);
386         return 0;
387 }
388
389 static ssize_t event_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
390 {
391         struct ibmasmfs_event_data *event_data = file->private_data;
392         struct event_reader *reader = &event_data->reader;
393         int ret;
394
395         if (*offset < 0)
396                 return -EINVAL;
397         if (count == 0 || count > IBMASM_EVENT_MAX_SIZE)
398                 return 0;
399         if (*offset != 0)
400                 return 0;
401
402         ret = ibmasm_get_next_event(event_data->sp, reader);
403         if (ret <= 0)
404                 return ret;
405
406         if (count < reader->data_size)
407                 return -EINVAL;
408
409         if (copy_to_user(buf, reader->data, reader->data_size))
410                 return -EFAULT;
411
412         return reader->data_size;
413 }
414
415 static ssize_t event_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
416 {
417         struct ibmasmfs_event_data *event_data = file->private_data;
418
419         if (*offset < 0)
420                 return -EINVAL;
421         if (count != 1)
422                 return 0;
423         if (*offset != 0)
424                 return 0;
425
426         wake_up_interruptible(&event_data->reader.wait);
427         return 0;
428 }
429
430 static int r_heartbeat_file_open(struct inode *inode, struct file *file)
431 {
432         struct ibmasmfs_heartbeat_data *rhbeat;
433
434         if (!inode->u.generic_ip)
435                 return -ENODEV;
436
437         rhbeat = kmalloc(sizeof(struct ibmasmfs_heartbeat_data), GFP_KERNEL);
438         if (!rhbeat)
439                 return -ENOMEM;
440
441         rhbeat->sp = (struct service_processor *)inode->u.generic_ip;
442         rhbeat->active = 0;
443         ibmasm_init_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
444         file->private_data = rhbeat;
445         return 0;
446 }
447
448 static int r_heartbeat_file_close(struct inode *inode, struct file *file)
449 {
450         struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
451
452         kfree(rhbeat);
453         return 0;
454 }
455
456 static ssize_t r_heartbeat_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
457 {
458         struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
459         unsigned long flags;
460         int result;
461
462         if (*offset < 0)
463                 return -EINVAL;
464         if (count == 0 || count > 1024)
465                 return 0;
466         if (*offset != 0)
467                 return 0;
468
469         /* allow only one reverse heartbeat per process */
470         spin_lock_irqsave(&rhbeat->sp->lock, flags);
471         if (rhbeat->active) {
472                 spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
473                 return -EBUSY;
474         }
475         rhbeat->active = 1;
476         spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
477
478         result = ibmasm_start_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
479         rhbeat->active = 0;
480
481         return result;
482 }
483
484 static ssize_t r_heartbeat_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
485 {
486         struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
487
488         if (*offset < 0)
489                 return -EINVAL;
490         if (count != 1)
491                 return 0;
492         if (*offset != 0)
493                 return 0;
494
495         if (rhbeat->active)
496                 ibmasm_stop_reverse_heartbeat(&rhbeat->heartbeat);
497
498         return 1;
499 }
500
501 static int remote_settings_file_open(struct inode *inode, struct file *file)
502 {
503         file->private_data = inode->u.generic_ip;
504         return 0;
505 }
506
507 static int remote_settings_file_close(struct inode *inode, struct file *file)
508 {
509         return 0;
510 }
511
512 static ssize_t remote_settings_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
513 {
514         void __iomem *address = (void __iomem *)file->private_data;
515         unsigned char *page;
516         int retval;
517         int len = 0;
518         unsigned int value;
519
520         if (*offset < 0)
521                 return -EINVAL;
522         if (count == 0 || count > 1024)
523                 return 0;
524         if (*offset != 0)
525                 return 0;
526
527         page = (unsigned char *)__get_free_page(GFP_KERNEL);
528         if (!page)
529                 return -ENOMEM;
530
531         value = readl(address);
532         len = sprintf(page, "%d\n", value);
533
534         if (copy_to_user(buf, page, len)) {
535                 retval = -EFAULT;
536                 goto exit;
537         }
538         *offset += len;
539         retval = len;
540
541 exit:
542         free_page((unsigned long)page);
543         return retval;
544 }
545
546 static ssize_t remote_settings_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
547 {
548         void __iomem *address = (void __iomem *)file->private_data;
549         char *buff;
550         unsigned int value;
551
552         if (*offset < 0)
553                 return -EINVAL;
554         if (count == 0 || count > 1024)
555                 return 0;
556         if (*offset != 0)
557                 return 0;
558
559         buff = kmalloc (count + 1, GFP_KERNEL);
560         if (!buff)
561                 return -ENOMEM;
562
563         memset(buff, 0x0, count + 1);
564
565         if (copy_from_user(buff, ubuff, count)) {
566                 kfree(buff);
567                 return -EFAULT;
568         }
569         
570         value = simple_strtoul(buff, NULL, 10);
571         writel(value, address);
572         kfree(buff);
573
574         return count;
575 }
576
577 static int remote_event_file_open(struct inode *inode, struct file *file)
578 {
579         struct service_processor *sp;
580         unsigned long flags;
581         struct remote_queue *q;
582         
583         file->private_data = inode->u.generic_ip;
584         sp = file->private_data;
585         q = &sp->remote_queue;
586
587         /* allow only one event reader */
588         spin_lock_irqsave(&sp->lock, flags);
589         if (q->open) {
590                 spin_unlock_irqrestore(&sp->lock, flags);
591                 return -EBUSY;
592         }
593         q->open = 1;
594         spin_unlock_irqrestore(&sp->lock, flags);
595
596         enable_mouse_interrupts(sp);
597         
598         return 0;
599 }
600
601 static int remote_event_file_close(struct inode *inode, struct file *file)
602 {
603         struct service_processor *sp = file->private_data;
604
605         disable_mouse_interrupts(sp);
606         wake_up_interruptible(&sp->remote_queue.wait);
607         sp->remote_queue.open = 0;
608
609         return 0;
610 }
611
612 static ssize_t remote_event_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
613 {
614         struct service_processor *sp = file->private_data;
615         struct remote_queue *q = &sp->remote_queue;
616         size_t data_size;
617         struct remote_event *reader = q->reader;
618         size_t num_events;
619
620         if (*offset < 0)
621                 return -EINVAL;
622         if (count == 0 || count > 1024)
623                 return 0;
624         if (*offset != 0)
625                 return 0;
626
627         if (wait_event_interruptible(q->wait, q->reader != q->writer))
628                 return -ERESTARTSYS;
629
630         /* only get multiples of struct remote_event */
631         num_events = min((count/sizeof(struct remote_event)), ibmasm_events_available(q));
632         if (!num_events)
633                 return 0;
634
635         data_size = num_events * sizeof(struct remote_event);
636
637         if (copy_to_user(buf, reader, data_size))
638                 return -EFAULT;
639
640         ibmasm_advance_reader(q, num_events);
641
642         return data_size;
643 }
644
645
646 static struct file_operations command_fops = {
647         .open =         command_file_open,
648         .release =      command_file_close,
649         .read =         command_file_read,
650         .write =        command_file_write,
651 };
652
653 static struct file_operations event_fops = {
654         .open =         event_file_open,
655         .release =      event_file_close,
656         .read =         event_file_read,
657         .write =        event_file_write,
658 };
659
660 static struct file_operations r_heartbeat_fops = {
661         .open =         r_heartbeat_file_open,
662         .release =      r_heartbeat_file_close,
663         .read =         r_heartbeat_file_read,
664         .write =        r_heartbeat_file_write,
665 };
666
667 static struct file_operations remote_settings_fops = {
668         .open =         remote_settings_file_open,
669         .release =      remote_settings_file_close,
670         .read =         remote_settings_file_read,
671         .write =        remote_settings_file_write,
672 };
673
674 static struct file_operations remote_event_fops = {
675         .open =         remote_event_file_open,
676         .release =      remote_event_file_close,
677         .read =         remote_event_file_read,
678 };
679
680
681 static void ibmasmfs_create_files (struct super_block *sb, struct dentry *root)
682 {
683         struct list_head *entry;
684         struct service_processor *sp;
685
686         list_for_each(entry, &service_processors) {
687                 struct dentry *dir;
688                 struct dentry *remote_dir;
689                 sp = list_entry(entry, struct service_processor, node);
690                 dir = ibmasmfs_create_dir(sb, root, sp->dirname);
691                 if (!dir)
692                         continue;
693
694                 ibmasmfs_create_file(sb, dir, "command", &command_fops, sp, S_IRUSR|S_IWUSR);
695                 ibmasmfs_create_file(sb, dir, "event", &event_fops, sp, S_IRUSR|S_IWUSR);
696                 ibmasmfs_create_file(sb, dir, "reverse_heartbeat", &r_heartbeat_fops, sp, S_IRUSR|S_IWUSR);
697
698                 remote_dir = ibmasmfs_create_dir(sb, dir, "remote_video");
699                 if (!remote_dir)
700                         continue;
701
702                 ibmasmfs_create_file(sb, remote_dir, "width", &remote_settings_fops, (void *)display_width(sp), S_IRUSR|S_IWUSR);
703                 ibmasmfs_create_file(sb, remote_dir, "height", &remote_settings_fops, (void *)display_height(sp), S_IRUSR|S_IWUSR);
704                 ibmasmfs_create_file(sb, remote_dir, "depth", &remote_settings_fops, (void *)display_depth(sp), S_IRUSR|S_IWUSR);
705                 ibmasmfs_create_file(sb, remote_dir, "connected", &remote_settings_fops, (void *)vnc_status(sp), S_IRUSR);
706                 ibmasmfs_create_file(sb, remote_dir, "events", &remote_event_fops, (void *)sp, S_IRUSR);
707         }
708 }