2 * IBM ASM Service Processor Device Driver
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.
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.
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.
18 * Copyright (C) IBM Corporation, 2004
20 * Author: Max Asböck <amax@us.ibm.com>
25 * Parts of this code are based on an article by Jonathan Corbet
26 * that appeared in Linux Weekly News.
31 * The IBMASM file virtual filesystem. It creates the following hierarchy
32 * dymamically when mounted from user space:
38 * | |-- reverse_heartbeat
51 * |-- reverse_heartbeat
59 * For each service processor the following files are created:
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
65 * events: listen for service processor events
66 * read: sleep (interruptible) until an event occurs
67 * write: wakeup sleeping event listener
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
75 * remote_video/width: control remote display settings
79 * remote_video/connected
80 * read: return "1" if web browser VNC java applet is connected,
84 * read: sleep until a remote mouse or keyboard event occurs, then return
89 #include <linux/pagemap.h>
90 #include <asm/uaccess.h>
94 #include "dot_command.h"
96 #define IBMASMFS_MAGIC 0x66726f67
98 static LIST_HEAD(service_processors);
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);
105 static struct super_block *ibmasmfs_get_super(struct file_system_type *fst,
106 int flags, const char *name, void *data)
108 return get_sb_single(fst, flags, data, ibmasmfs_fill_super);
111 static struct super_operations ibmasmfs_s_ops = {
112 .statfs = simple_statfs,
113 .drop_inode = generic_delete_inode,
116 static struct file_operations *ibmasmfs_dir_ops = &simple_dir_operations;
118 static struct file_system_type ibmasmfs_type = {
119 .owner = THIS_MODULE,
121 .get_sb = ibmasmfs_get_super,
122 .kill_sb = kill_litter_super,
125 static int ibmasmfs_fill_super (struct super_block *sb, void *data, int silent)
128 struct dentry *root_dentry;
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;
136 root = ibmasmfs_make_inode (sb, S_IFDIR | 0500);
140 root->i_op = &simple_dir_inode_operations;
141 root->i_fop = ibmasmfs_dir_ops;
143 root_dentry = d_alloc_root(root);
148 sb->s_root = root_dentry;
150 ibmasmfs_create_files(sb, root_dentry);
154 static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode)
156 struct inode *ret = new_inode(sb);
160 ret->i_uid = ret->i_gid = 0;
161 ret->i_blksize = PAGE_CACHE_SIZE;
163 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
168 static struct dentry *ibmasmfs_create_file (struct super_block *sb,
169 struct dentry *parent,
171 struct file_operations *fops,
175 struct dentry *dentry;
178 dentry = d_alloc_name(parent, name);
182 inode = ibmasmfs_make_inode(sb, S_IFREG | mode);
189 inode->u.generic_ip = data;
191 d_add(dentry, inode);
195 static struct dentry *ibmasmfs_create_dir (struct super_block *sb,
196 struct dentry *parent,
199 struct dentry *dentry;
202 dentry = d_alloc_name(parent, name);
206 inode = ibmasmfs_make_inode(sb, S_IFDIR | 0500);
212 inode->i_op = &simple_dir_inode_operations;
213 inode->i_fop = ibmasmfs_dir_ops;
215 d_add(dentry, inode);
219 int ibmasmfs_register(void)
221 return register_filesystem(&ibmasmfs_type);
224 void ibmasmfs_unregister(void)
226 unregister_filesystem(&ibmasmfs_type);
229 void ibmasmfs_add_sp(struct service_processor *sp)
231 list_add(&sp->node, &service_processors);
234 /* struct to save state between command file operations */
235 struct ibmasmfs_command_data {
236 struct service_processor *sp;
237 struct command *command;
240 /* struct to save state between event file operations */
241 struct ibmasmfs_event_data {
242 struct service_processor *sp;
243 struct event_reader reader;
247 /* struct to save state between reverse heartbeat file operations */
248 struct ibmasmfs_heartbeat_data {
249 struct service_processor *sp;
250 struct reverse_heartbeat heartbeat;
254 static int command_file_open(struct inode *inode, struct file *file)
256 struct ibmasmfs_command_data *command_data;
258 if (!inode->u.generic_ip)
261 command_data = kmalloc(sizeof(struct ibmasmfs_command_data), GFP_KERNEL);
265 command_data->command = NULL;
266 command_data->sp = inode->u.generic_ip;
267 file->private_data = command_data;
271 static int command_file_close(struct inode *inode, struct file *file)
273 struct ibmasmfs_command_data *command_data = file->private_data;
275 if (command_data->command)
276 command_put(command_data->command);
282 static ssize_t command_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
284 struct ibmasmfs_command_data *command_data = file->private_data;
291 if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
296 spin_lock_irqsave(&command_data->sp->lock, flags);
297 cmd = command_data->command;
299 spin_unlock_irqrestore(&command_data->sp->lock, flags);
302 command_data->command = NULL;
303 spin_unlock_irqrestore(&command_data->sp->lock, flags);
305 if (cmd->status != IBMASM_CMD_COMPLETE) {
309 len = min(count, cmd->buffer_size);
310 if (copy_to_user(buf, cmd->buffer, len)) {
319 static ssize_t command_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
321 struct ibmasmfs_command_data *command_data = file->private_data;
327 if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
332 /* commands are executed sequentially, only one command at a time */
333 if (command_data->command)
336 cmd = ibmasm_new_command(count);
340 if (copy_from_user(cmd->buffer, ubuff, count)) {
345 spin_lock_irqsave(&command_data->sp->lock, flags);
346 if (command_data->command) {
347 spin_unlock_irqrestore(&command_data->sp->lock, flags);
351 command_data->command = cmd;
352 spin_unlock_irqrestore(&command_data->sp->lock, flags);
354 ibmasm_exec_command(command_data->sp, cmd);
355 ibmasm_wait_for_response(cmd, get_dot_command_timeout(cmd->buffer));
360 static int event_file_open(struct inode *inode, struct file *file)
362 struct ibmasmfs_event_data *event_data;
363 struct service_processor *sp;
365 if (!inode->u.generic_ip)
368 sp = inode->u.generic_ip;
370 event_data = kmalloc(sizeof(struct ibmasmfs_event_data), GFP_KERNEL);
374 ibmasm_event_reader_register(sp, &event_data->reader);
377 event_data->active = 0;
378 file->private_data = event_data;
382 static int event_file_close(struct inode *inode, struct file *file)
384 struct ibmasmfs_event_data *event_data = file->private_data;
386 ibmasm_event_reader_unregister(event_data->sp, &event_data->reader);
391 static ssize_t event_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
393 struct ibmasmfs_event_data *event_data = file->private_data;
394 struct event_reader *reader = &event_data->reader;
395 struct service_processor *sp = event_data->sp;
401 if (count == 0 || count > IBMASM_EVENT_MAX_SIZE)
406 spin_lock_irqsave(&sp->lock, flags);
407 if (event_data->active) {
408 spin_unlock_irqrestore(&sp->lock, flags);
411 event_data->active = 1;
412 spin_unlock_irqrestore(&sp->lock, flags);
414 ret = ibmasm_get_next_event(sp, reader);
418 if (count < reader->data_size) {
423 if (copy_to_user(buf, reader->data, reader->data_size)) {
427 ret = reader->data_size;
430 event_data->active = 0;
434 static ssize_t event_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
436 struct ibmasmfs_event_data *event_data = file->private_data;
445 ibmasm_cancel_next_event(&event_data->reader);
449 static int r_heartbeat_file_open(struct inode *inode, struct file *file)
451 struct ibmasmfs_heartbeat_data *rhbeat;
453 if (!inode->u.generic_ip)
456 rhbeat = kmalloc(sizeof(struct ibmasmfs_heartbeat_data), GFP_KERNEL);
460 rhbeat->sp = (struct service_processor *)inode->u.generic_ip;
462 ibmasm_init_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
463 file->private_data = rhbeat;
467 static int r_heartbeat_file_close(struct inode *inode, struct file *file)
469 struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
475 static ssize_t r_heartbeat_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
477 struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
483 if (count == 0 || count > 1024)
488 /* allow only one reverse heartbeat per process */
489 spin_lock_irqsave(&rhbeat->sp->lock, flags);
490 if (rhbeat->active) {
491 spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
495 spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
497 result = ibmasm_start_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
503 static ssize_t r_heartbeat_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
505 struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
515 ibmasm_stop_reverse_heartbeat(&rhbeat->heartbeat);
520 static int remote_settings_file_open(struct inode *inode, struct file *file)
522 file->private_data = inode->u.generic_ip;
526 static int remote_settings_file_close(struct inode *inode, struct file *file)
531 static ssize_t remote_settings_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
533 void __iomem *address = (void __iomem *)file->private_data;
541 if (count == 0 || count > 1024)
546 page = (unsigned char *)__get_free_page(GFP_KERNEL);
550 value = readl(address);
551 len = sprintf(page, "%d\n", value);
553 if (copy_to_user(buf, page, len)) {
561 free_page((unsigned long)page);
565 static ssize_t remote_settings_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
567 void __iomem *address = (void __iomem *)file->private_data;
573 if (count == 0 || count > 1024)
578 buff = kmalloc (count + 1, GFP_KERNEL);
582 memset(buff, 0x0, count + 1);
584 if (copy_from_user(buff, ubuff, count)) {
589 value = simple_strtoul(buff, NULL, 10);
590 writel(value, address);
596 static int remote_event_file_open(struct inode *inode, struct file *file)
598 struct service_processor *sp;
600 struct remote_queue *q;
602 file->private_data = inode->u.generic_ip;
603 sp = file->private_data;
604 q = &sp->remote_queue;
606 /* allow only one event reader */
607 spin_lock_irqsave(&sp->lock, flags);
609 spin_unlock_irqrestore(&sp->lock, flags);
613 spin_unlock_irqrestore(&sp->lock, flags);
615 enable_mouse_interrupts(sp);
620 static int remote_event_file_close(struct inode *inode, struct file *file)
622 struct service_processor *sp = file->private_data;
624 disable_mouse_interrupts(sp);
625 wake_up_interruptible(&sp->remote_queue.wait);
626 sp->remote_queue.open = 0;
631 static ssize_t remote_event_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
633 struct service_processor *sp = file->private_data;
634 struct remote_queue *q = &sp->remote_queue;
636 struct remote_event *reader = q->reader;
641 if (count == 0 || count > 1024)
646 if (wait_event_interruptible(q->wait, q->reader != q->writer))
649 /* only get multiples of struct remote_event */
650 num_events = min((count/sizeof(struct remote_event)), ibmasm_events_available(q));
654 data_size = num_events * sizeof(struct remote_event);
656 if (copy_to_user(buf, reader, data_size))
659 ibmasm_advance_reader(q, num_events);
665 static struct file_operations command_fops = {
666 .open = command_file_open,
667 .release = command_file_close,
668 .read = command_file_read,
669 .write = command_file_write,
672 static struct file_operations event_fops = {
673 .open = event_file_open,
674 .release = event_file_close,
675 .read = event_file_read,
676 .write = event_file_write,
679 static struct file_operations r_heartbeat_fops = {
680 .open = r_heartbeat_file_open,
681 .release = r_heartbeat_file_close,
682 .read = r_heartbeat_file_read,
683 .write = r_heartbeat_file_write,
686 static struct file_operations remote_settings_fops = {
687 .open = remote_settings_file_open,
688 .release = remote_settings_file_close,
689 .read = remote_settings_file_read,
690 .write = remote_settings_file_write,
693 static struct file_operations remote_event_fops = {
694 .open = remote_event_file_open,
695 .release = remote_event_file_close,
696 .read = remote_event_file_read,
700 static void ibmasmfs_create_files (struct super_block *sb, struct dentry *root)
702 struct list_head *entry;
703 struct service_processor *sp;
705 list_for_each(entry, &service_processors) {
707 struct dentry *remote_dir;
708 sp = list_entry(entry, struct service_processor, node);
709 dir = ibmasmfs_create_dir(sb, root, sp->dirname);
713 ibmasmfs_create_file(sb, dir, "command", &command_fops, sp, S_IRUSR|S_IWUSR);
714 ibmasmfs_create_file(sb, dir, "event", &event_fops, sp, S_IRUSR|S_IWUSR);
715 ibmasmfs_create_file(sb, dir, "reverse_heartbeat", &r_heartbeat_fops, sp, S_IRUSR|S_IWUSR);
717 remote_dir = ibmasmfs_create_dir(sb, dir, "remote_video");
721 ibmasmfs_create_file(sb, remote_dir, "width", &remote_settings_fops, (void *)display_width(sp), S_IRUSR|S_IWUSR);
722 ibmasmfs_create_file(sb, remote_dir, "height", &remote_settings_fops, (void *)display_height(sp), S_IRUSR|S_IWUSR);
723 ibmasmfs_create_file(sb, remote_dir, "depth", &remote_settings_fops, (void *)display_depth(sp), S_IRUSR|S_IWUSR);
724 ibmasmfs_create_file(sb, remote_dir, "connected", &remote_settings_fops, (void *)vnc_status(sp), S_IRUSR);
725 ibmasmfs_create_file(sb, remote_dir, "events", &remote_event_fops, (void *)sp, S_IRUSR);