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