Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Al Viro | d36b691 | 2011-12-29 17:09:01 -0500 | [diff] [blame] | 20 | * Author: Max Asböck <amax@us.ibm.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | /* |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 25 | * Parts of this code are based on an article by Jonathan Corbet |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | * that appeared in Linux Weekly News. |
| 27 | */ |
| 28 | |
| 29 | |
| 30 | /* |
| 31 | * The IBMASM file virtual filesystem. It creates the following hierarchy |
Justin P. Mattock | 5ecd602e | 2011-04-08 08:23:23 -0700 | [diff] [blame] | 32 | * dynamically when mounted from user space: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | * |
| 34 | * /ibmasm |
| 35 | * |-- 0 |
| 36 | * | |-- command |
| 37 | * | |-- event |
| 38 | * | |-- reverse_heartbeat |
| 39 | * | `-- remote_video |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | * | |-- depth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | * | |-- height |
| 42 | * | `-- width |
| 43 | * . |
| 44 | * . |
| 45 | * . |
| 46 | * `-- n |
| 47 | * |-- command |
| 48 | * |-- event |
| 49 | * |-- reverse_heartbeat |
| 50 | * `-- remote_video |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | * |-- depth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | * |-- height |
| 53 | * `-- width |
| 54 | * |
| 55 | * For each service processor the following files are created: |
| 56 | * |
| 57 | * command: execute dot commands |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 58 | * write: execute a dot command on the service processor |
| 59 | * read: return the result of a previously executed dot command |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | * |
| 61 | * events: listen for service processor events |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 62 | * read: sleep (interruptible) until an event occurs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * write: wakeup sleeping event listener |
| 64 | * |
| 65 | * reverse_heartbeat: send a heartbeat to the service processor |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 66 | * read: sleep (interruptible) until the reverse heartbeat fails |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | * write: wakeup sleeping heartbeat listener |
| 68 | * |
| 69 | * remote_video/width |
| 70 | * remote_video/height |
| 71 | * remote_video/width: control remote display settings |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 72 | * write: set value |
| 73 | * read: read value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | */ |
| 75 | |
| 76 | #include <linux/fs.h> |
| 77 | #include <linux/pagemap.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 78 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | #include <asm/uaccess.h> |
| 80 | #include <asm/io.h> |
| 81 | #include "ibmasm.h" |
| 82 | #include "remote.h" |
| 83 | #include "dot_command.h" |
| 84 | |
| 85 | #define IBMASMFS_MAGIC 0x66726f67 |
| 86 | |
| 87 | static LIST_HEAD(service_processors); |
| 88 | |
| 89 | static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode); |
Al Viro | 318ceed | 2012-02-12 22:08:01 -0500 | [diff] [blame] | 90 | static void ibmasmfs_create_files (struct super_block *sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | static int ibmasmfs_fill_super (struct super_block *sb, void *data, int silent); |
| 92 | |
| 93 | |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 94 | static struct dentry *ibmasmfs_mount(struct file_system_type *fst, |
| 95 | int flags, const char *name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 97 | return mount_single(fst, flags, data, ibmasmfs_fill_super); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 100 | static const struct super_operations ibmasmfs_s_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | .statfs = simple_statfs, |
| 102 | .drop_inode = generic_delete_inode, |
| 103 | }; |
| 104 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 105 | static const struct file_operations *ibmasmfs_dir_ops = &simple_dir_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | static struct file_system_type ibmasmfs_type = { |
| 108 | .owner = THIS_MODULE, |
| 109 | .name = "ibmasmfs", |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 110 | .mount = ibmasmfs_mount, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | .kill_sb = kill_litter_super, |
| 112 | }; |
Eric W. Biederman | 7f78e03 | 2013-03-02 19:39:14 -0800 | [diff] [blame] | 113 | MODULE_ALIAS_FS("ibmasmfs"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | static int ibmasmfs_fill_super (struct super_block *sb, void *data, int silent) |
| 116 | { |
| 117 | struct inode *root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 120 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 121 | sb->s_magic = IBMASMFS_MAGIC; |
| 122 | sb->s_op = &ibmasmfs_s_ops; |
| 123 | sb->s_time_gran = 1; |
| 124 | |
| 125 | root = ibmasmfs_make_inode (sb, S_IFDIR | 0500); |
| 126 | if (!root) |
| 127 | return -ENOMEM; |
| 128 | |
| 129 | root->i_op = &simple_dir_inode_operations; |
| 130 | root->i_fop = ibmasmfs_dir_ops; |
| 131 | |
Al Viro | 318ceed | 2012-02-12 22:08:01 -0500 | [diff] [blame] | 132 | sb->s_root = d_make_root(root); |
| 133 | if (!sb->s_root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Al Viro | 318ceed | 2012-02-12 22:08:01 -0500 | [diff] [blame] | 136 | ibmasmfs_create_files(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode) |
| 141 | { |
| 142 | struct inode *ret = new_inode(sb); |
| 143 | |
| 144 | if (ret) { |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 145 | ret->i_ino = get_next_ino(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | ret->i_mode = mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME; |
| 148 | } |
| 149 | return ret; |
| 150 | } |
| 151 | |
Al Viro | 0507c78 | 2013-07-19 18:09:56 +0400 | [diff] [blame] | 152 | static struct dentry *ibmasmfs_create_file(struct dentry *parent, |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 153 | const char *name, |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 154 | const struct file_operations *fops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | void *data, |
| 156 | int mode) |
| 157 | { |
| 158 | struct dentry *dentry; |
| 159 | struct inode *inode; |
| 160 | |
| 161 | dentry = d_alloc_name(parent, name); |
| 162 | if (!dentry) |
| 163 | return NULL; |
| 164 | |
Al Viro | 0507c78 | 2013-07-19 18:09:56 +0400 | [diff] [blame] | 165 | inode = ibmasmfs_make_inode(parent->d_sb, S_IFREG | mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | if (!inode) { |
| 167 | dput(dentry); |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | inode->i_fop = fops; |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 172 | inode->i_private = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | d_add(dentry, inode); |
| 175 | return dentry; |
| 176 | } |
| 177 | |
Al Viro | 0507c78 | 2013-07-19 18:09:56 +0400 | [diff] [blame] | 178 | static struct dentry *ibmasmfs_create_dir(struct dentry *parent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | const char *name) |
| 180 | { |
| 181 | struct dentry *dentry; |
| 182 | struct inode *inode; |
| 183 | |
| 184 | dentry = d_alloc_name(parent, name); |
| 185 | if (!dentry) |
| 186 | return NULL; |
| 187 | |
Al Viro | 0507c78 | 2013-07-19 18:09:56 +0400 | [diff] [blame] | 188 | inode = ibmasmfs_make_inode(parent->d_sb, S_IFDIR | 0500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | if (!inode) { |
| 190 | dput(dentry); |
| 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | inode->i_op = &simple_dir_inode_operations; |
| 195 | inode->i_fop = ibmasmfs_dir_ops; |
| 196 | |
| 197 | d_add(dentry, inode); |
| 198 | return dentry; |
| 199 | } |
| 200 | |
| 201 | int ibmasmfs_register(void) |
| 202 | { |
| 203 | return register_filesystem(&ibmasmfs_type); |
| 204 | } |
| 205 | |
| 206 | void ibmasmfs_unregister(void) |
| 207 | { |
| 208 | unregister_filesystem(&ibmasmfs_type); |
| 209 | } |
| 210 | |
| 211 | void ibmasmfs_add_sp(struct service_processor *sp) |
| 212 | { |
| 213 | list_add(&sp->node, &service_processors); |
| 214 | } |
| 215 | |
| 216 | /* struct to save state between command file operations */ |
| 217 | struct ibmasmfs_command_data { |
| 218 | struct service_processor *sp; |
| 219 | struct command *command; |
| 220 | }; |
| 221 | |
| 222 | /* struct to save state between event file operations */ |
| 223 | struct ibmasmfs_event_data { |
| 224 | struct service_processor *sp; |
| 225 | struct event_reader reader; |
| 226 | int active; |
| 227 | }; |
| 228 | |
| 229 | /* struct to save state between reverse heartbeat file operations */ |
| 230 | struct ibmasmfs_heartbeat_data { |
| 231 | struct service_processor *sp; |
| 232 | struct reverse_heartbeat heartbeat; |
| 233 | int active; |
| 234 | }; |
| 235 | |
| 236 | static int command_file_open(struct inode *inode, struct file *file) |
| 237 | { |
| 238 | struct ibmasmfs_command_data *command_data; |
| 239 | |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 240 | if (!inode->i_private) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | return -ENODEV; |
| 242 | |
| 243 | command_data = kmalloc(sizeof(struct ibmasmfs_command_data), GFP_KERNEL); |
| 244 | if (!command_data) |
| 245 | return -ENOMEM; |
| 246 | |
| 247 | command_data->command = NULL; |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 248 | command_data->sp = inode->i_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | file->private_data = command_data; |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int command_file_close(struct inode *inode, struct file *file) |
| 254 | { |
| 255 | struct ibmasmfs_command_data *command_data = file->private_data; |
| 256 | |
| 257 | if (command_data->command) |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 258 | command_put(command_data->command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | kfree(command_data); |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static ssize_t command_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset) |
| 265 | { |
| 266 | struct ibmasmfs_command_data *command_data = file->private_data; |
| 267 | struct command *cmd; |
| 268 | int len; |
| 269 | unsigned long flags; |
| 270 | |
| 271 | if (*offset < 0) |
| 272 | return -EINVAL; |
| 273 | if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE) |
| 274 | return 0; |
| 275 | if (*offset != 0) |
| 276 | return 0; |
| 277 | |
| 278 | spin_lock_irqsave(&command_data->sp->lock, flags); |
| 279 | cmd = command_data->command; |
| 280 | if (cmd == NULL) { |
| 281 | spin_unlock_irqrestore(&command_data->sp->lock, flags); |
| 282 | return 0; |
| 283 | } |
| 284 | command_data->command = NULL; |
| 285 | spin_unlock_irqrestore(&command_data->sp->lock, flags); |
| 286 | |
| 287 | if (cmd->status != IBMASM_CMD_COMPLETE) { |
| 288 | command_put(cmd); |
| 289 | return -EIO; |
| 290 | } |
| 291 | len = min(count, cmd->buffer_size); |
| 292 | if (copy_to_user(buf, cmd->buffer, len)) { |
| 293 | command_put(cmd); |
| 294 | return -EFAULT; |
| 295 | } |
| 296 | command_put(cmd); |
| 297 | |
| 298 | return len; |
| 299 | } |
| 300 | |
| 301 | static ssize_t command_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset) |
| 302 | { |
| 303 | struct ibmasmfs_command_data *command_data = file->private_data; |
| 304 | struct command *cmd; |
| 305 | unsigned long flags; |
| 306 | |
| 307 | if (*offset < 0) |
| 308 | return -EINVAL; |
| 309 | if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE) |
| 310 | return 0; |
| 311 | if (*offset != 0) |
| 312 | return 0; |
| 313 | |
| 314 | /* commands are executed sequentially, only one command at a time */ |
| 315 | if (command_data->command) |
| 316 | return -EAGAIN; |
| 317 | |
Max Asbock | 8818760 | 2005-06-21 17:16:36 -0700 | [diff] [blame] | 318 | cmd = ibmasm_new_command(command_data->sp, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | if (!cmd) |
| 320 | return -ENOMEM; |
| 321 | |
| 322 | if (copy_from_user(cmd->buffer, ubuff, count)) { |
| 323 | command_put(cmd); |
| 324 | return -EFAULT; |
| 325 | } |
| 326 | |
| 327 | spin_lock_irqsave(&command_data->sp->lock, flags); |
| 328 | if (command_data->command) { |
| 329 | spin_unlock_irqrestore(&command_data->sp->lock, flags); |
| 330 | command_put(cmd); |
| 331 | return -EAGAIN; |
| 332 | } |
| 333 | command_data->command = cmd; |
| 334 | spin_unlock_irqrestore(&command_data->sp->lock, flags); |
| 335 | |
| 336 | ibmasm_exec_command(command_data->sp, cmd); |
| 337 | ibmasm_wait_for_response(cmd, get_dot_command_timeout(cmd->buffer)); |
| 338 | |
| 339 | return count; |
| 340 | } |
| 341 | |
| 342 | static int event_file_open(struct inode *inode, struct file *file) |
| 343 | { |
| 344 | struct ibmasmfs_event_data *event_data; |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 345 | struct service_processor *sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 347 | if (!inode->i_private) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | return -ENODEV; |
| 349 | |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 350 | sp = inode->i_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | event_data = kmalloc(sizeof(struct ibmasmfs_event_data), GFP_KERNEL); |
| 353 | if (!event_data) |
| 354 | return -ENOMEM; |
| 355 | |
| 356 | ibmasm_event_reader_register(sp, &event_data->reader); |
| 357 | |
| 358 | event_data->sp = sp; |
Max Asbock | b8acb80 | 2005-06-21 17:16:33 -0700 | [diff] [blame] | 359 | event_data->active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | file->private_data = event_data; |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int event_file_close(struct inode *inode, struct file *file) |
| 365 | { |
| 366 | struct ibmasmfs_event_data *event_data = file->private_data; |
| 367 | |
| 368 | ibmasm_event_reader_unregister(event_data->sp, &event_data->reader); |
| 369 | kfree(event_data); |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static ssize_t event_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset) |
| 374 | { |
| 375 | struct ibmasmfs_event_data *event_data = file->private_data; |
| 376 | struct event_reader *reader = &event_data->reader; |
Max Asbock | b8acb80 | 2005-06-21 17:16:33 -0700 | [diff] [blame] | 377 | struct service_processor *sp = event_data->sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | int ret; |
Max Asbock | b8acb80 | 2005-06-21 17:16:33 -0700 | [diff] [blame] | 379 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
| 381 | if (*offset < 0) |
| 382 | return -EINVAL; |
| 383 | if (count == 0 || count > IBMASM_EVENT_MAX_SIZE) |
| 384 | return 0; |
| 385 | if (*offset != 0) |
| 386 | return 0; |
| 387 | |
Max Asbock | b8acb80 | 2005-06-21 17:16:33 -0700 | [diff] [blame] | 388 | spin_lock_irqsave(&sp->lock, flags); |
| 389 | if (event_data->active) { |
| 390 | spin_unlock_irqrestore(&sp->lock, flags); |
| 391 | return -EBUSY; |
| 392 | } |
| 393 | event_data->active = 1; |
| 394 | spin_unlock_irqrestore(&sp->lock, flags); |
| 395 | |
| 396 | ret = ibmasm_get_next_event(sp, reader); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | if (ret <= 0) |
Max Asbock | b8acb80 | 2005-06-21 17:16:33 -0700 | [diff] [blame] | 398 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Max Asbock | b8acb80 | 2005-06-21 17:16:33 -0700 | [diff] [blame] | 400 | if (count < reader->data_size) { |
| 401 | ret = -EINVAL; |
| 402 | goto out; |
| 403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
Max Asbock | b8acb80 | 2005-06-21 17:16:33 -0700 | [diff] [blame] | 405 | if (copy_to_user(buf, reader->data, reader->data_size)) { |
| 406 | ret = -EFAULT; |
| 407 | goto out; |
| 408 | } |
| 409 | ret = reader->data_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Max Asbock | b8acb80 | 2005-06-21 17:16:33 -0700 | [diff] [blame] | 411 | out: |
| 412 | event_data->active = 0; |
| 413 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static ssize_t event_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset) |
| 417 | { |
| 418 | struct ibmasmfs_event_data *event_data = file->private_data; |
| 419 | |
| 420 | if (*offset < 0) |
| 421 | return -EINVAL; |
| 422 | if (count != 1) |
| 423 | return 0; |
| 424 | if (*offset != 0) |
| 425 | return 0; |
| 426 | |
Max Asbock | b8acb80 | 2005-06-21 17:16:33 -0700 | [diff] [blame] | 427 | ibmasm_cancel_next_event(&event_data->reader); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static int r_heartbeat_file_open(struct inode *inode, struct file *file) |
| 432 | { |
| 433 | struct ibmasmfs_heartbeat_data *rhbeat; |
| 434 | |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 435 | if (!inode->i_private) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | return -ENODEV; |
| 437 | |
| 438 | rhbeat = kmalloc(sizeof(struct ibmasmfs_heartbeat_data), GFP_KERNEL); |
| 439 | if (!rhbeat) |
| 440 | return -ENOMEM; |
| 441 | |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 442 | rhbeat->sp = inode->i_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | rhbeat->active = 0; |
| 444 | ibmasm_init_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat); |
| 445 | file->private_data = rhbeat; |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static int r_heartbeat_file_close(struct inode *inode, struct file *file) |
| 450 | { |
| 451 | struct ibmasmfs_heartbeat_data *rhbeat = file->private_data; |
| 452 | |
| 453 | kfree(rhbeat); |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | static ssize_t r_heartbeat_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset) |
| 458 | { |
| 459 | struct ibmasmfs_heartbeat_data *rhbeat = file->private_data; |
| 460 | unsigned long flags; |
| 461 | int result; |
| 462 | |
| 463 | if (*offset < 0) |
| 464 | return -EINVAL; |
| 465 | if (count == 0 || count > 1024) |
| 466 | return 0; |
| 467 | if (*offset != 0) |
| 468 | return 0; |
| 469 | |
| 470 | /* allow only one reverse heartbeat per process */ |
| 471 | spin_lock_irqsave(&rhbeat->sp->lock, flags); |
| 472 | if (rhbeat->active) { |
| 473 | spin_unlock_irqrestore(&rhbeat->sp->lock, flags); |
| 474 | return -EBUSY; |
| 475 | } |
| 476 | rhbeat->active = 1; |
| 477 | spin_unlock_irqrestore(&rhbeat->sp->lock, flags); |
| 478 | |
| 479 | result = ibmasm_start_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat); |
| 480 | rhbeat->active = 0; |
| 481 | |
| 482 | return result; |
| 483 | } |
| 484 | |
| 485 | static ssize_t r_heartbeat_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset) |
| 486 | { |
| 487 | struct ibmasmfs_heartbeat_data *rhbeat = file->private_data; |
| 488 | |
| 489 | if (*offset < 0) |
| 490 | return -EINVAL; |
| 491 | if (count != 1) |
| 492 | return 0; |
| 493 | if (*offset != 0) |
| 494 | return 0; |
| 495 | |
| 496 | if (rhbeat->active) |
| 497 | ibmasm_stop_reverse_heartbeat(&rhbeat->heartbeat); |
| 498 | |
| 499 | return 1; |
| 500 | } |
| 501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | static int remote_settings_file_close(struct inode *inode, struct file *file) |
| 503 | { |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static ssize_t remote_settings_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset) |
| 508 | { |
| 509 | void __iomem *address = (void __iomem *)file->private_data; |
| 510 | unsigned char *page; |
| 511 | int retval; |
| 512 | int len = 0; |
| 513 | unsigned int value; |
| 514 | |
| 515 | if (*offset < 0) |
| 516 | return -EINVAL; |
| 517 | if (count == 0 || count > 1024) |
| 518 | return 0; |
| 519 | if (*offset != 0) |
| 520 | return 0; |
| 521 | |
| 522 | page = (unsigned char *)__get_free_page(GFP_KERNEL); |
| 523 | if (!page) |
| 524 | return -ENOMEM; |
| 525 | |
| 526 | value = readl(address); |
| 527 | len = sprintf(page, "%d\n", value); |
| 528 | |
| 529 | if (copy_to_user(buf, page, len)) { |
| 530 | retval = -EFAULT; |
| 531 | goto exit; |
| 532 | } |
| 533 | *offset += len; |
| 534 | retval = len; |
| 535 | |
| 536 | exit: |
| 537 | free_page((unsigned long)page); |
| 538 | return retval; |
| 539 | } |
| 540 | |
| 541 | static ssize_t remote_settings_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset) |
| 542 | { |
| 543 | void __iomem *address = (void __iomem *)file->private_data; |
| 544 | char *buff; |
| 545 | unsigned int value; |
| 546 | |
| 547 | if (*offset < 0) |
| 548 | return -EINVAL; |
| 549 | if (count == 0 || count > 1024) |
| 550 | return 0; |
| 551 | if (*offset != 0) |
| 552 | return 0; |
| 553 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 554 | buff = kzalloc (count + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | if (!buff) |
| 556 | return -ENOMEM; |
| 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 559 | if (copy_from_user(buff, ubuff, count)) { |
| 560 | kfree(buff); |
| 561 | return -EFAULT; |
| 562 | } |
Dmitry Torokhov | 3110dc7 | 2007-07-17 04:03:58 -0700 | [diff] [blame] | 563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | value = simple_strtoul(buff, NULL, 10); |
| 565 | writel(value, address); |
| 566 | kfree(buff); |
| 567 | |
| 568 | return count; |
| 569 | } |
| 570 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 571 | static const struct file_operations command_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | .open = command_file_open, |
| 573 | .release = command_file_close, |
| 574 | .read = command_file_read, |
| 575 | .write = command_file_write, |
Arnd Bergmann | 275bd41 | 2010-07-06 22:54:51 +0200 | [diff] [blame] | 576 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | }; |
| 578 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 579 | static const struct file_operations event_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | .open = event_file_open, |
| 581 | .release = event_file_close, |
| 582 | .read = event_file_read, |
| 583 | .write = event_file_write, |
Arnd Bergmann | 275bd41 | 2010-07-06 22:54:51 +0200 | [diff] [blame] | 584 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | }; |
| 586 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 587 | static const struct file_operations r_heartbeat_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | .open = r_heartbeat_file_open, |
| 589 | .release = r_heartbeat_file_close, |
| 590 | .read = r_heartbeat_file_read, |
| 591 | .write = r_heartbeat_file_write, |
Arnd Bergmann | 275bd41 | 2010-07-06 22:54:51 +0200 | [diff] [blame] | 592 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | }; |
| 594 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 595 | static const struct file_operations remote_settings_fops = { |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 596 | .open = simple_open, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | .release = remote_settings_file_close, |
| 598 | .read = remote_settings_file_read, |
| 599 | .write = remote_settings_file_write, |
Arnd Bergmann | 275bd41 | 2010-07-06 22:54:51 +0200 | [diff] [blame] | 600 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | }; |
| 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
Al Viro | 318ceed | 2012-02-12 22:08:01 -0500 | [diff] [blame] | 604 | static void ibmasmfs_create_files (struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | { |
| 606 | struct list_head *entry; |
| 607 | struct service_processor *sp; |
| 608 | |
| 609 | list_for_each(entry, &service_processors) { |
| 610 | struct dentry *dir; |
| 611 | struct dentry *remote_dir; |
| 612 | sp = list_entry(entry, struct service_processor, node); |
Al Viro | 0507c78 | 2013-07-19 18:09:56 +0400 | [diff] [blame] | 613 | dir = ibmasmfs_create_dir(sb->s_root, sp->dirname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | if (!dir) |
| 615 | continue; |
| 616 | |
Al Viro | 0507c78 | 2013-07-19 18:09:56 +0400 | [diff] [blame] | 617 | ibmasmfs_create_file(dir, "command", &command_fops, sp, S_IRUSR|S_IWUSR); |
| 618 | ibmasmfs_create_file(dir, "event", &event_fops, sp, S_IRUSR|S_IWUSR); |
| 619 | ibmasmfs_create_file(dir, "reverse_heartbeat", &r_heartbeat_fops, sp, S_IRUSR|S_IWUSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
Al Viro | 0507c78 | 2013-07-19 18:09:56 +0400 | [diff] [blame] | 621 | remote_dir = ibmasmfs_create_dir(dir, "remote_video"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | if (!remote_dir) |
| 623 | continue; |
| 624 | |
Al Viro | 0507c78 | 2013-07-19 18:09:56 +0400 | [diff] [blame] | 625 | ibmasmfs_create_file(remote_dir, "width", &remote_settings_fops, (void *)display_width(sp), S_IRUSR|S_IWUSR); |
| 626 | ibmasmfs_create_file(remote_dir, "height", &remote_settings_fops, (void *)display_height(sp), S_IRUSR|S_IWUSR); |
| 627 | ibmasmfs_create_file(remote_dir, "depth", &remote_settings_fops, (void *)display_depth(sp), S_IRUSR|S_IWUSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } |
| 629 | } |