ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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         struct qstr qname;
177
178         qname.name = name;
179         qname.len = strlen (name);
180         qname.hash = full_name_hash(name, qname.len);
181
182         dentry = d_alloc(parent, &qname);
183         if (!dentry)
184                 return NULL;
185
186         inode = ibmasmfs_make_inode(sb, S_IFREG | mode);
187         if (!inode) {
188                 dput(dentry);
189                 return NULL;
190         }
191
192         inode->i_fop = fops;
193         inode->u.generic_ip = data;
194
195         d_add(dentry, inode);
196         return dentry;
197 }
198
199 static struct dentry *ibmasmfs_create_dir (struct super_block *sb,
200                                 struct dentry *parent,
201                                 const char *name)
202 {
203         struct dentry *dentry;
204         struct inode *inode;
205         struct qstr qname;
206
207         qname.name = name;
208         qname.len = strlen (name);
209         qname.hash = full_name_hash(name, qname.len);
210         dentry = d_alloc(parent, &qname);
211         if (!dentry)
212                 return NULL;
213
214         inode = ibmasmfs_make_inode(sb, S_IFDIR | 0500);
215         if (!inode) {
216                 dput(dentry);
217                 return NULL;
218         }
219
220         inode->i_op = &simple_dir_inode_operations;
221         inode->i_fop = ibmasmfs_dir_ops;
222
223         d_add(dentry, inode);
224         return dentry;
225 }
226
227 int ibmasmfs_register()
228 {
229         return register_filesystem(&ibmasmfs_type);
230 }
231
232 void ibmasmfs_unregister()
233 {
234         unregister_filesystem(&ibmasmfs_type);
235 }
236
237 void ibmasmfs_add_sp(struct service_processor *sp)
238 {
239         list_add(&sp->node, &service_processors);
240 }
241
242 /* struct to save state between command file operations */
243 struct ibmasmfs_command_data {
244         struct service_processor        *sp;
245         struct command                  *command;
246 };
247
248 /* struct to save state between event file operations */
249 struct ibmasmfs_event_data {
250         struct service_processor        *sp;
251         struct event_reader             reader;
252         int                             active;
253 };
254
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;
259         int                             active;
260 };
261
262 static int command_file_open(struct inode *inode, struct file *file)
263 {
264         struct ibmasmfs_command_data *command_data;
265
266         if (!inode->u.generic_ip)
267                 return -ENODEV;
268
269         command_data = kmalloc(sizeof(struct ibmasmfs_command_data), GFP_KERNEL);
270         if (!command_data)
271                 return -ENOMEM;
272
273         command_data->command = NULL;
274         command_data->sp = inode->u.generic_ip;
275         file->private_data = command_data;
276         return 0;
277 }
278
279 static int command_file_close(struct inode *inode, struct file *file)
280 {
281         struct ibmasmfs_command_data *command_data = file->private_data;
282
283         if (command_data->command)
284                 command_put(command_data->command);     
285
286         kfree(command_data);
287         return 0;
288 }
289
290 static ssize_t command_file_read(struct file *file, char *buf, size_t count, loff_t *offset)
291 {
292         struct ibmasmfs_command_data *command_data = file->private_data;
293         struct command *cmd;
294         int len;
295         unsigned long flags;
296
297         if (*offset < 0)
298                 return -EINVAL;
299         if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
300                 return 0;
301         if (*offset != 0)
302                 return 0;
303
304         spin_lock_irqsave(&command_data->sp->lock, flags);
305         cmd = command_data->command;
306         if (cmd == NULL) {
307                 spin_unlock_irqrestore(&command_data->sp->lock, flags);
308                 return 0;
309         }
310         command_data->command = NULL;
311         spin_unlock_irqrestore(&command_data->sp->lock, flags);
312
313         if (cmd->status != IBMASM_CMD_COMPLETE) {
314                 command_put(cmd);
315                 return -EIO;
316         }
317         len = min(count, cmd->buffer_size);
318         if (copy_to_user(buf, cmd->buffer, len)) {
319                 command_put(cmd);
320                 return -EFAULT;
321         }
322         command_put(cmd);
323
324         return len;
325 }
326
327 static ssize_t command_file_write(struct file *file, const char *ubuff, size_t count, loff_t *offset)
328 {
329         struct ibmasmfs_command_data *command_data = file->private_data;
330         struct command *cmd;
331         unsigned long flags;
332
333         if (*offset < 0)
334                 return -EINVAL;
335         if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
336                 return 0;
337         if (*offset != 0)
338                 return 0;
339
340         /* commands are executed sequentially, only one command at a time */
341         if (command_data->command)
342                 return -EAGAIN;
343
344         cmd = ibmasm_new_command(count);
345         if (!cmd)
346                 return -ENOMEM;
347
348         if (copy_from_user((void *)cmd->buffer, (void *)ubuff, count)) {
349                 command_put(cmd);
350                 return -EFAULT;
351         }
352
353         spin_lock_irqsave(&command_data->sp->lock, flags);
354         if (command_data->command) {
355                 spin_unlock_irqrestore(&command_data->sp->lock, flags);
356                 command_put(cmd);
357                 return -EAGAIN;
358         }
359         command_data->command = cmd;
360         spin_unlock_irqrestore(&command_data->sp->lock, flags);
361
362         ibmasm_exec_command(command_data->sp, cmd);
363         ibmasm_wait_for_response(cmd, get_dot_command_timeout(cmd->buffer));
364
365         return count;
366 }
367
368 static int event_file_open(struct inode *inode, struct file *file)
369 {
370         struct ibmasmfs_event_data *event_data;
371         struct service_processor *sp; 
372
373         if (!inode->u.generic_ip)
374                 return -ENODEV;
375
376         sp = inode->u.generic_ip;
377
378         event_data = kmalloc(sizeof(struct ibmasmfs_event_data), GFP_KERNEL);
379         if (!event_data)
380                 return -ENOMEM;
381
382         ibmasm_event_reader_register(sp, &event_data->reader);
383
384         event_data->sp = sp;
385         file->private_data = event_data;
386         return 0;
387 }
388
389 static int event_file_close(struct inode *inode, struct file *file)
390 {
391         struct ibmasmfs_event_data *event_data = file->private_data;
392
393         ibmasm_event_reader_unregister(event_data->sp, &event_data->reader);
394         kfree(event_data);
395         return 0;
396 }
397
398 static ssize_t event_file_read(struct file *file, char *buf, size_t count, loff_t *offset)
399 {
400         struct ibmasmfs_event_data *event_data = file->private_data;
401         struct event_reader *reader = &event_data->reader;
402         int ret;
403
404         if (*offset < 0)
405                 return -EINVAL;
406         if (count == 0 || count > IBMASM_EVENT_MAX_SIZE)
407                 return 0;
408         if (*offset != 0)
409                 return 0;
410
411         ret = ibmasm_get_next_event(event_data->sp, reader);
412         if (ret <= 0)
413                 return ret;
414
415         if (count < reader->data_size)
416                 return -EINVAL;
417
418         if (copy_to_user(buf, reader->data, reader->data_size))
419                 return -EFAULT;
420
421         return reader->data_size;
422 }
423
424 static ssize_t event_file_write(struct file *file, const char *buf, size_t count, loff_t *offset)
425 {
426         struct ibmasmfs_event_data *event_data = file->private_data;
427
428         if (*offset < 0)
429                 return -EINVAL;
430         if (count != 1)
431                 return 0;
432         if (*offset != 0)
433                 return 0;
434
435         wake_up_interruptible(&event_data->reader.wait);
436         return 0;
437 }
438
439 static int r_heartbeat_file_open(struct inode *inode, struct file *file)
440 {
441         struct ibmasmfs_heartbeat_data *rhbeat;
442
443         if (!inode->u.generic_ip)
444                 return -ENODEV;
445
446         rhbeat = kmalloc(sizeof(struct ibmasmfs_heartbeat_data), GFP_KERNEL);
447         if (!rhbeat)
448                 return -ENOMEM;
449
450         rhbeat->sp = (struct service_processor *)inode->u.generic_ip;
451         rhbeat->active = 0;
452         ibmasm_init_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
453         file->private_data = rhbeat;
454         return 0;
455 }
456
457 static int r_heartbeat_file_close(struct inode *inode, struct file *file)
458 {
459         struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
460
461         kfree(rhbeat);
462         return 0;
463 }
464
465 static ssize_t r_heartbeat_file_read(struct file *file, char *buf, size_t count, loff_t *offset)
466 {
467         struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
468         unsigned long flags;
469         int result;
470
471         if (*offset < 0)
472                 return -EINVAL;
473         if (count == 0 || count > 1024)
474                 return 0;
475         if (*offset != 0)
476                 return 0;
477
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);
482                 return -EBUSY;
483         }
484         rhbeat->active = 1;
485         spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
486
487         result = ibmasm_start_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
488         rhbeat->active = 0;
489
490         return result;
491 }
492
493 static ssize_t r_heartbeat_file_write(struct file *file, const char *buf, size_t count, loff_t *offset)
494 {
495         struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
496
497         if (*offset < 0)
498                 return -EINVAL;
499         if (count != 1)
500                 return 0;
501         if (*offset != 0)
502                 return 0;
503
504         if (rhbeat->active)
505                 ibmasm_stop_reverse_heartbeat(&rhbeat->heartbeat);
506
507         return 1;
508 }
509
510 static int remote_settings_file_open(struct inode *inode, struct file *file)
511 {
512         file->private_data = inode->u.generic_ip;
513         return 0;
514 }
515
516 static int remote_settings_file_close(struct inode *inode, struct file *file)
517 {
518         return 0;
519 }
520
521 static ssize_t remote_settings_file_read(struct file *file, char *buf, size_t count, loff_t *offset)
522 {
523         unsigned long address = (unsigned long)file->private_data;
524         unsigned char *page;
525         int retval;
526         int len = 0;
527         unsigned int value;
528
529         if (*offset < 0)
530                 return -EINVAL;
531         if (count == 0 || count > 1024)
532                 return 0;
533         if (*offset != 0)
534                 return 0;
535
536         page = (unsigned char *)__get_free_page(GFP_KERNEL);
537         if (!page)
538                 return -ENOMEM;
539
540         value = readl(address);
541         len = sprintf(page, "%d\n", value);
542
543         if (copy_to_user(buf, page, len)) {
544                 retval = -EFAULT;
545                 goto exit;
546         }
547         *offset += len;
548         retval = len;
549
550 exit:
551         free_page((unsigned long)page);
552         return retval;
553 }
554
555 static ssize_t remote_settings_file_write(struct file *file, const char *ubuff, size_t count, loff_t *offset)
556 {
557         unsigned long address = (unsigned long)file->private_data;
558         char *buff;
559         unsigned int value;
560
561         if (*offset < 0)
562                 return -EINVAL;
563         if (count == 0 || count > 1024)
564                 return 0;
565         if (*offset != 0)
566                 return 0;
567
568         buff = kmalloc (count + 1, GFP_KERNEL);
569         if (!buff)
570                 return -ENOMEM;
571
572         memset(buff, 0x0, count + 1);
573
574         if (copy_from_user((void *)buff, (void *)ubuff, count)) {
575                 kfree(buff);
576                 return -EFAULT;
577         }
578         
579         value = simple_strtoul(buff, NULL, 10);
580         writel(value, address);
581         kfree(buff);
582
583         return count;
584 }
585
586 static int remote_event_file_open(struct inode *inode, struct file *file)
587 {
588         struct service_processor *sp;
589         unsigned long flags;
590         struct remote_queue *q;
591         
592         file->private_data = inode->u.generic_ip;
593         sp = file->private_data;
594         q = &sp->remote_queue;
595
596         /* allow only one event reader */
597         spin_lock_irqsave(&sp->lock, flags);
598         if (q->open) {
599                 spin_unlock_irqrestore(&sp->lock, flags);
600                 return -EBUSY;
601         }
602         q->open = 1;
603         spin_unlock_irqrestore(&sp->lock, flags);
604
605         enable_mouse_interrupts(sp);
606         
607         return 0;
608 }
609
610 static int remote_event_file_close(struct inode *inode, struct file *file)
611 {
612         struct service_processor *sp = file->private_data;
613
614         disable_mouse_interrupts(sp);
615         wake_up_interruptible(&sp->remote_queue.wait);
616         sp->remote_queue.open = 0;
617
618         return 0;
619 }
620
621 static ssize_t remote_event_file_read(struct file *file, char *buf, size_t count, loff_t *offset)
622 {
623         struct service_processor *sp = file->private_data;
624         struct remote_queue *q = &sp->remote_queue;
625         size_t data_size;
626         struct remote_event *reader = q->reader;
627         size_t num_events;
628
629         if (*offset < 0)
630                 return -EINVAL;
631         if (count == 0 || count > 1024)
632                 return 0;
633         if (*offset != 0)
634                 return 0;
635
636         if (wait_event_interruptible(q->wait, q->reader != q->writer))
637                 return -ERESTARTSYS;
638
639         /* only get multiples of struct remote_event */
640         num_events = min((count/sizeof(struct remote_event)), ibmasm_events_available(q));
641         if (!num_events)
642                 return 0;
643
644         data_size = num_events * sizeof(struct remote_event);
645
646         if (copy_to_user(buf, reader, data_size))
647                 return -EFAULT;
648
649         ibmasm_advance_reader(q, num_events);
650
651         return data_size;
652 }
653
654
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,
660 };
661
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,
667 };
668
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,
674 };
675
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,
681 };
682
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,
687 };
688
689
690 static void ibmasmfs_create_files (struct super_block *sb, struct dentry *root)
691 {
692         struct list_head *entry;
693         struct service_processor *sp;
694
695         list_for_each(entry, &service_processors) {
696                 struct dentry *dir;
697                 struct dentry *remote_dir;
698                 sp = list_entry(entry, struct service_processor, node);
699                 dir = ibmasmfs_create_dir(sb, root, sp->dirname);
700                 if (!dir)
701                         continue;
702
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);
706
707                 remote_dir = ibmasmfs_create_dir(sb, dir, "remote_video");
708                 if (!remote_dir)
709                         continue;
710
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);
716         }
717 }