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