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;
135 root = ibmasmfs_make_inode (sb, S_IFDIR | 0500);
139 root->i_op = &simple_dir_inode_operations;
140 root->i_fop = ibmasmfs_dir_ops;
142 root_dentry = d_alloc_root(root);
147 sb->s_root = root_dentry;
149 ibmasmfs_create_files(sb, root_dentry);
153 static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode)
155 struct inode *ret = new_inode(sb);
159 ret->i_uid = ret->i_gid = 0;
160 ret->i_blksize = PAGE_CACHE_SIZE;
162 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
167 static struct dentry *ibmasmfs_create_file (struct super_block *sb,
168 struct dentry *parent,
170 struct file_operations *fops,
174 struct dentry *dentry;
179 qname.len = strlen (name);
180 qname.hash = full_name_hash(name, qname.len);
182 dentry = d_alloc(parent, &qname);
186 inode = ibmasmfs_make_inode(sb, S_IFREG | mode);
193 inode->u.generic_ip = data;
195 d_add(dentry, inode);
199 static struct dentry *ibmasmfs_create_dir (struct super_block *sb,
200 struct dentry *parent,
203 struct dentry *dentry;
208 qname.len = strlen (name);
209 qname.hash = full_name_hash(name, qname.len);
210 dentry = d_alloc(parent, &qname);
214 inode = ibmasmfs_make_inode(sb, S_IFDIR | 0500);
220 inode->i_op = &simple_dir_inode_operations;
221 inode->i_fop = ibmasmfs_dir_ops;
223 d_add(dentry, inode);
227 int ibmasmfs_register()
229 return register_filesystem(&ibmasmfs_type);
232 void ibmasmfs_unregister()
234 unregister_filesystem(&ibmasmfs_type);
237 void ibmasmfs_add_sp(struct service_processor *sp)
239 list_add(&sp->node, &service_processors);
242 /* struct to save state between command file operations */
243 struct ibmasmfs_command_data {
244 struct service_processor *sp;
245 struct command *command;
248 /* struct to save state between event file operations */
249 struct ibmasmfs_event_data {
250 struct service_processor *sp;
251 struct event_reader reader;
255 /* struct to save state between reverse heartbeat file operations */
256 struct ibmasmfs_heartbeat_data {
257 struct service_processor *sp;
258 struct reverse_heartbeat heartbeat;
262 static int command_file_open(struct inode *inode, struct file *file)
264 struct ibmasmfs_command_data *command_data;
266 if (!inode->u.generic_ip)
269 command_data = kmalloc(sizeof(struct ibmasmfs_command_data), GFP_KERNEL);
273 command_data->command = NULL;
274 command_data->sp = inode->u.generic_ip;
275 file->private_data = command_data;
279 static int command_file_close(struct inode *inode, struct file *file)
281 struct ibmasmfs_command_data *command_data = file->private_data;
283 if (command_data->command)
284 command_put(command_data->command);
290 static ssize_t command_file_read(struct file *file, char *buf, size_t count, loff_t *offset)
292 struct ibmasmfs_command_data *command_data = file->private_data;
299 if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
304 spin_lock_irqsave(&command_data->sp->lock, flags);
305 cmd = command_data->command;
307 spin_unlock_irqrestore(&command_data->sp->lock, flags);
310 command_data->command = NULL;
311 spin_unlock_irqrestore(&command_data->sp->lock, flags);
313 if (cmd->status != IBMASM_CMD_COMPLETE) {
317 len = min(count, cmd->buffer_size);
318 if (copy_to_user(buf, cmd->buffer, len)) {
327 static ssize_t command_file_write(struct file *file, const char *ubuff, size_t count, loff_t *offset)
329 struct ibmasmfs_command_data *command_data = file->private_data;
335 if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
340 /* commands are executed sequentially, only one command at a time */
341 if (command_data->command)
344 cmd = ibmasm_new_command(count);
348 if (copy_from_user((void *)cmd->buffer, (void *)ubuff, count)) {
353 spin_lock_irqsave(&command_data->sp->lock, flags);
354 if (command_data->command) {
355 spin_unlock_irqrestore(&command_data->sp->lock, flags);
359 command_data->command = cmd;
360 spin_unlock_irqrestore(&command_data->sp->lock, flags);
362 ibmasm_exec_command(command_data->sp, cmd);
363 ibmasm_wait_for_response(cmd, get_dot_command_timeout(cmd->buffer));
368 static int event_file_open(struct inode *inode, struct file *file)
370 struct ibmasmfs_event_data *event_data;
371 struct service_processor *sp;
373 if (!inode->u.generic_ip)
376 sp = inode->u.generic_ip;
378 event_data = kmalloc(sizeof(struct ibmasmfs_event_data), GFP_KERNEL);
382 ibmasm_event_reader_register(sp, &event_data->reader);
385 file->private_data = event_data;
389 static int event_file_close(struct inode *inode, struct file *file)
391 struct ibmasmfs_event_data *event_data = file->private_data;
393 ibmasm_event_reader_unregister(event_data->sp, &event_data->reader);
398 static ssize_t event_file_read(struct file *file, char *buf, size_t count, loff_t *offset)
400 struct ibmasmfs_event_data *event_data = file->private_data;
401 struct event_reader *reader = &event_data->reader;
406 if (count == 0 || count > IBMASM_EVENT_MAX_SIZE)
411 ret = ibmasm_get_next_event(event_data->sp, reader);
415 if (count < reader->data_size)
418 if (copy_to_user(buf, reader->data, reader->data_size))
421 return reader->data_size;
424 static ssize_t event_file_write(struct file *file, const char *buf, size_t count, loff_t *offset)
426 struct ibmasmfs_event_data *event_data = file->private_data;
435 wake_up_interruptible(&event_data->reader.wait);
439 static int r_heartbeat_file_open(struct inode *inode, struct file *file)
441 struct ibmasmfs_heartbeat_data *rhbeat;
443 if (!inode->u.generic_ip)
446 rhbeat = kmalloc(sizeof(struct ibmasmfs_heartbeat_data), GFP_KERNEL);
450 rhbeat->sp = (struct service_processor *)inode->u.generic_ip;
452 ibmasm_init_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
453 file->private_data = rhbeat;
457 static int r_heartbeat_file_close(struct inode *inode, struct file *file)
459 struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
465 static ssize_t r_heartbeat_file_read(struct file *file, char *buf, size_t count, loff_t *offset)
467 struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
473 if (count == 0 || count > 1024)
478 /* allow only one reverse heartbeat per process */
479 spin_lock_irqsave(&rhbeat->sp->lock, flags);
480 if (rhbeat->active) {
481 spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
485 spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
487 result = ibmasm_start_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
493 static ssize_t r_heartbeat_file_write(struct file *file, const char *buf, size_t count, loff_t *offset)
495 struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
505 ibmasm_stop_reverse_heartbeat(&rhbeat->heartbeat);
510 static int remote_settings_file_open(struct inode *inode, struct file *file)
512 file->private_data = inode->u.generic_ip;
516 static int remote_settings_file_close(struct inode *inode, struct file *file)
521 static ssize_t remote_settings_file_read(struct file *file, char *buf, size_t count, loff_t *offset)
523 unsigned long address = (unsigned long)file->private_data;
531 if (count == 0 || count > 1024)
536 page = (unsigned char *)__get_free_page(GFP_KERNEL);
540 value = readl(address);
541 len = sprintf(page, "%d\n", value);
543 if (copy_to_user(buf, page, len)) {
551 free_page((unsigned long)page);
555 static ssize_t remote_settings_file_write(struct file *file, const char *ubuff, size_t count, loff_t *offset)
557 unsigned long address = (unsigned long)file->private_data;
563 if (count == 0 || count > 1024)
568 buff = kmalloc (count + 1, GFP_KERNEL);
572 memset(buff, 0x0, count + 1);
574 if (copy_from_user((void *)buff, (void *)ubuff, count)) {
579 value = simple_strtoul(buff, NULL, 10);
580 writel(value, address);
586 static int remote_event_file_open(struct inode *inode, struct file *file)
588 struct service_processor *sp;
590 struct remote_queue *q;
592 file->private_data = inode->u.generic_ip;
593 sp = file->private_data;
594 q = &sp->remote_queue;
596 /* allow only one event reader */
597 spin_lock_irqsave(&sp->lock, flags);
599 spin_unlock_irqrestore(&sp->lock, flags);
603 spin_unlock_irqrestore(&sp->lock, flags);
605 enable_mouse_interrupts(sp);
610 static int remote_event_file_close(struct inode *inode, struct file *file)
612 struct service_processor *sp = file->private_data;
614 disable_mouse_interrupts(sp);
615 wake_up_interruptible(&sp->remote_queue.wait);
616 sp->remote_queue.open = 0;
621 static ssize_t remote_event_file_read(struct file *file, char *buf, size_t count, loff_t *offset)
623 struct service_processor *sp = file->private_data;
624 struct remote_queue *q = &sp->remote_queue;
626 struct remote_event *reader = q->reader;
631 if (count == 0 || count > 1024)
636 if (wait_event_interruptible(q->wait, q->reader != q->writer))
639 /* only get multiples of struct remote_event */
640 num_events = min((count/sizeof(struct remote_event)), ibmasm_events_available(q));
644 data_size = num_events * sizeof(struct remote_event);
646 if (copy_to_user(buf, reader, data_size))
649 ibmasm_advance_reader(q, num_events);
655 static struct file_operations command_fops = {
656 .open = command_file_open,
657 .release = command_file_close,
658 .read = command_file_read,
659 .write = command_file_write,
662 static struct file_operations event_fops = {
663 .open = event_file_open,
664 .release = event_file_close,
665 .read = event_file_read,
666 .write event_file_write,
669 static struct file_operations r_heartbeat_fops = {
670 .open = r_heartbeat_file_open,
671 .release = r_heartbeat_file_close,
672 .read = r_heartbeat_file_read,
673 .write = r_heartbeat_file_write,
676 static struct file_operations remote_settings_fops = {
677 .open = remote_settings_file_open,
678 .release = remote_settings_file_close,
679 .read = remote_settings_file_read,
680 .write = remote_settings_file_write,
683 static struct file_operations remote_event_fops = {
684 .open = remote_event_file_open,
685 .release = remote_event_file_close,
686 .read = remote_event_file_read,
690 static void ibmasmfs_create_files (struct super_block *sb, struct dentry *root)
692 struct list_head *entry;
693 struct service_processor *sp;
695 list_for_each(entry, &service_processors) {
697 struct dentry *remote_dir;
698 sp = list_entry(entry, struct service_processor, node);
699 dir = ibmasmfs_create_dir(sb, root, sp->dirname);
703 ibmasmfs_create_file(sb, dir, "command", &command_fops, sp, S_IRUSR|S_IWUSR);
704 ibmasmfs_create_file(sb, dir, "event", &event_fops, sp, S_IRUSR|S_IWUSR);
705 ibmasmfs_create_file(sb, dir, "reverse_heartbeat", &r_heartbeat_fops, sp, S_IRUSR|S_IWUSR);
707 remote_dir = ibmasmfs_create_dir(sb, dir, "remote_video");
711 ibmasmfs_create_file(sb, remote_dir, "width", &remote_settings_fops, (void *)display_width(sp), S_IRUSR|S_IWUSR);
712 ibmasmfs_create_file(sb, remote_dir, "height", &remote_settings_fops, (void *)display_height(sp), S_IRUSR|S_IWUSR);
713 ibmasmfs_create_file(sb, remote_dir, "depth", &remote_settings_fops, (void *)display_depth(sp), S_IRUSR|S_IWUSR);
714 ibmasmfs_create_file(sb, remote_dir, "connected", &remote_settings_fops, (void *)vnc_status(sp), S_IRUSR);
715 ibmasmfs_create_file(sb, remote_dir, "events", &remote_event_fops, (void *)sp, S_IRUSR);