Merge branch 'sched/for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip...
[pandora-kernel.git] / drivers / s390 / char / vmlogrdr.c
1 /*
2  * drivers/s390/char/vmlogrdr.c
3  *      character device driver for reading z/VM system service records
4  *
5  *
6  *      Copyright 2004 IBM Corporation
7  *      character device driver for reading z/VM system service records,
8  *      Version 1.0
9  *      Author(s): Xenia Tkatschow <xenia@us.ibm.com>
10  *                 Stefan Weinhuber <wein@de.ibm.com>
11  *
12  */
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 #include <linux/interrupt.h>
18 #include <linux/spinlock.h>
19 #include <asm/atomic.h>
20 #include <asm/uaccess.h>
21 #include <asm/cpcmd.h>
22 #include <asm/debug.h>
23 #include <asm/ebcdic.h>
24 #include <net/iucv/iucv.h>
25 #include <linux/kmod.h>
26 #include <linux/cdev.h>
27 #include <linux/device.h>
28 #include <linux/string.h>
29
30
31
32 MODULE_AUTHOR
33         ("(C) 2004 IBM Corporation by Xenia Tkatschow (xenia@us.ibm.com)\n"
34          "                            Stefan Weinhuber (wein@de.ibm.com)");
35 MODULE_DESCRIPTION ("Character device driver for reading z/VM "
36                     "system service records.");
37 MODULE_LICENSE("GPL");
38
39
40 /*
41  * The size of the buffer for iucv data transfer is one page,
42  * but in addition to the data we read from iucv we also
43  * place an integer and some characters into that buffer,
44  * so the maximum size for record data is a little less then
45  * one page.
46  */
47 #define NET_BUFFER_SIZE (PAGE_SIZE - sizeof(int) - sizeof(FENCE))
48
49 /*
50  * The elements that are concurrently accessed by bottom halves are
51  * connection_established, iucv_path_severed, local_interrupt_buffer
52  * and receive_ready. The first three can be protected by
53  * priv_lock.  receive_ready is atomic, so it can be incremented and
54  * decremented without holding a lock.
55  * The variable dev_in_use needs to be protected by the lock, since
56  * it's a flag used by open to make sure that the device is opened only
57  * by one user at the same time.
58  */
59 struct vmlogrdr_priv_t {
60         char system_service[8];
61         char internal_name[8];
62         char recording_name[8];
63         struct iucv_path *path;
64         int connection_established;
65         int iucv_path_severed;
66         struct iucv_message local_interrupt_buffer;
67         atomic_t receive_ready;
68         int minor_num;
69         char * buffer;
70         char * current_position;
71         int remaining;
72         ulong residual_length;
73         int buffer_free;
74         int dev_in_use; /* 1: already opened, 0: not opened*/
75         spinlock_t priv_lock;
76         struct device  *device;
77         struct device  *class_device;
78         int autorecording;
79         int autopurge;
80 };
81
82
83 /*
84  * File operation structure for vmlogrdr devices
85  */
86 static int vmlogrdr_open(struct inode *, struct file *);
87 static int vmlogrdr_release(struct inode *, struct file *);
88 static ssize_t vmlogrdr_read (struct file *filp, char __user *data,
89                               size_t count, loff_t * ppos);
90
91 static const struct file_operations vmlogrdr_fops = {
92         .owner   = THIS_MODULE,
93         .open    = vmlogrdr_open,
94         .release = vmlogrdr_release,
95         .read    = vmlogrdr_read,
96 };
97
98
99 static void vmlogrdr_iucv_path_complete(struct iucv_path *, u8 ipuser[16]);
100 static void vmlogrdr_iucv_path_severed(struct iucv_path *, u8 ipuser[16]);
101 static void vmlogrdr_iucv_message_pending(struct iucv_path *,
102                                           struct iucv_message *);
103
104
105 static struct iucv_handler vmlogrdr_iucv_handler = {
106         .path_complete   = vmlogrdr_iucv_path_complete,
107         .path_severed    = vmlogrdr_iucv_path_severed,
108         .message_pending = vmlogrdr_iucv_message_pending,
109 };
110
111
112 static DECLARE_WAIT_QUEUE_HEAD(conn_wait_queue);
113 static DECLARE_WAIT_QUEUE_HEAD(read_wait_queue);
114
115 /*
116  * pointer to system service private structure
117  * minor number 0 --> logrec
118  * minor number 1 --> account
119  * minor number 2 --> symptom
120  */
121
122 static struct vmlogrdr_priv_t sys_ser[] = {
123         { .system_service = "*LOGREC ",
124           .internal_name  = "logrec",
125           .recording_name = "EREP",
126           .minor_num      = 0,
127           .buffer_free    = 1,
128           .priv_lock      = __SPIN_LOCK_UNLOCKED(sys_ser[0].priv_lock),
129           .autorecording  = 1,
130           .autopurge      = 1,
131         },
132         { .system_service = "*ACCOUNT",
133           .internal_name  = "account",
134           .recording_name = "ACCOUNT",
135           .minor_num      = 1,
136           .buffer_free    = 1,
137           .priv_lock      = __SPIN_LOCK_UNLOCKED(sys_ser[1].priv_lock),
138           .autorecording  = 1,
139           .autopurge      = 1,
140         },
141         { .system_service = "*SYMPTOM",
142           .internal_name  = "symptom",
143           .recording_name = "SYMPTOM",
144           .minor_num      = 2,
145           .buffer_free    = 1,
146           .priv_lock      = __SPIN_LOCK_UNLOCKED(sys_ser[2].priv_lock),
147           .autorecording  = 1,
148           .autopurge      = 1,
149         }
150 };
151
152 #define MAXMINOR  (sizeof(sys_ser)/sizeof(struct vmlogrdr_priv_t))
153
154 static char FENCE[] = {"EOR"};
155 static int vmlogrdr_major = 0;
156 static struct cdev  *vmlogrdr_cdev = NULL;
157 static int recording_class_AB;
158
159
160 static void vmlogrdr_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
161 {
162         struct vmlogrdr_priv_t * logptr = path->private;
163
164         spin_lock(&logptr->priv_lock);
165         logptr->connection_established = 1;
166         spin_unlock(&logptr->priv_lock);
167         wake_up(&conn_wait_queue);
168 }
169
170
171 static void vmlogrdr_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
172 {
173         struct vmlogrdr_priv_t * logptr = path->private;
174         u8 reason = (u8) ipuser[8];
175
176         printk (KERN_ERR "vmlogrdr: connection severed with"
177                 " reason %i\n", reason);
178
179         iucv_path_sever(path, NULL);
180         kfree(path);
181         logptr->path = NULL;
182
183         spin_lock(&logptr->priv_lock);
184         logptr->connection_established = 0;
185         logptr->iucv_path_severed = 1;
186         spin_unlock(&logptr->priv_lock);
187
188         wake_up(&conn_wait_queue);
189         /* just in case we're sleeping waiting for a record */
190         wake_up_interruptible(&read_wait_queue);
191 }
192
193
194 static void vmlogrdr_iucv_message_pending(struct iucv_path *path,
195                                           struct iucv_message *msg)
196 {
197         struct vmlogrdr_priv_t * logptr = path->private;
198
199         /*
200          * This function is the bottom half so it should be quick.
201          * Copy the external interrupt data into our local eib and increment
202          * the usage count
203          */
204         spin_lock(&logptr->priv_lock);
205         memcpy(&logptr->local_interrupt_buffer, msg, sizeof(*msg));
206         atomic_inc(&logptr->receive_ready);
207         spin_unlock(&logptr->priv_lock);
208         wake_up_interruptible(&read_wait_queue);
209 }
210
211
212 static int vmlogrdr_get_recording_class_AB(void)
213 {
214         char cp_command[]="QUERY COMMAND RECORDING ";
215         char cp_response[80];
216         char *tail;
217         int len,i;
218
219         cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
220         len = strnlen(cp_response,sizeof(cp_response));
221         // now the parsing
222         tail=strnchr(cp_response,len,'=');
223         if (!tail)
224                 return 0;
225         tail++;
226         if (!strncmp("ANY",tail,3))
227                 return 1;
228         if (!strncmp("NONE",tail,4))
229                 return 0;
230         /*
231          * expect comma separated list of classes here, if one of them
232          * is A or B return 1 otherwise 0
233          */
234         for (i=tail-cp_response; i<len; i++)
235                 if ( cp_response[i]=='A' || cp_response[i]=='B' )
236                         return 1;
237         return 0;
238 }
239
240
241 static int vmlogrdr_recording(struct vmlogrdr_priv_t * logptr,
242                               int action, int purge)
243 {
244
245         char cp_command[80];
246         char cp_response[160];
247         char *onoff, *qid_string;
248
249         memset(cp_command, 0x00, sizeof(cp_command));
250         memset(cp_response, 0x00, sizeof(cp_response));
251
252         onoff = ((action == 1) ? "ON" : "OFF");
253         qid_string = ((recording_class_AB == 1) ? " QID * " : "");
254
255         /*
256          * The recording commands needs to be called with option QID
257          * for guests that have previlege classes A or B.
258          * Purging has to be done as separate step, because recording
259          * can't be switched on as long as records are on the queue.
260          * Doing both at the same time doesn't work.
261          */
262
263         if (purge) {
264                 snprintf(cp_command, sizeof(cp_command),
265                          "RECORDING %s PURGE %s",
266                          logptr->recording_name,
267                          qid_string);
268
269                 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
270         }
271
272         memset(cp_command, 0x00, sizeof(cp_command));
273         memset(cp_response, 0x00, sizeof(cp_response));
274         snprintf(cp_command, sizeof(cp_command), "RECORDING %s %s %s",
275                 logptr->recording_name,
276                 onoff,
277                 qid_string);
278
279         cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
280         /* The recording command will usually answer with 'Command complete'
281          * on success, but when the specific service was never connected
282          * before then there might be an additional informational message
283          * 'HCPCRC8072I Recording entry not found' before the
284          * 'Command complete'. So I use strstr rather then the strncmp.
285          */
286         if (strstr(cp_response,"Command complete"))
287                 return 0;
288         else
289                 return -EIO;
290
291 }
292
293
294 static int vmlogrdr_open (struct inode *inode, struct file *filp)
295 {
296         int dev_num = 0;
297         struct vmlogrdr_priv_t * logptr = NULL;
298         int connect_rc = 0;
299         int ret;
300
301         dev_num = iminor(inode);
302         if (dev_num > MAXMINOR)
303                 return -ENODEV;
304         logptr = &sys_ser[dev_num];
305
306         /*
307          * only allow for blocking reads to be open
308          */
309         if (filp->f_flags & O_NONBLOCK)
310                 return -ENOSYS;
311
312         /* Besure this device hasn't already been opened */
313         spin_lock_bh(&logptr->priv_lock);
314         if (logptr->dev_in_use) {
315                 spin_unlock_bh(&logptr->priv_lock);
316                 return -EBUSY;
317         }
318         logptr->dev_in_use = 1;
319         logptr->connection_established = 0;
320         logptr->iucv_path_severed = 0;
321         atomic_set(&logptr->receive_ready, 0);
322         logptr->buffer_free = 1;
323         spin_unlock_bh(&logptr->priv_lock);
324
325         /* set the file options */
326         filp->private_data = logptr;
327         filp->f_op = &vmlogrdr_fops;
328
329         /* start recording for this service*/
330         if (logptr->autorecording) {
331                 ret = vmlogrdr_recording(logptr,1,logptr->autopurge);
332                 if (ret)
333                         printk (KERN_WARNING "vmlogrdr: failed to start "
334                                 "recording automatically\n");
335         }
336
337         /* create connection to the system service */
338         logptr->path = iucv_path_alloc(10, 0, GFP_KERNEL);
339         if (!logptr->path)
340                 goto out_dev;
341         connect_rc = iucv_path_connect(logptr->path, &vmlogrdr_iucv_handler,
342                                        logptr->system_service, NULL, NULL,
343                                        logptr);
344         if (connect_rc) {
345                 printk (KERN_ERR "vmlogrdr: iucv connection to %s "
346                         "failed with rc %i \n", logptr->system_service,
347                         connect_rc);
348                 goto out_path;
349         }
350
351         /* We've issued the connect and now we must wait for a
352          * ConnectionComplete or ConnectinSevered Interrupt
353          * before we can continue to process.
354          */
355         wait_event(conn_wait_queue, (logptr->connection_established)
356                    || (logptr->iucv_path_severed));
357         if (logptr->iucv_path_severed)
358                 goto out_record;
359         return nonseekable_open(inode, filp);
360
361 out_record:
362         if (logptr->autorecording)
363                 vmlogrdr_recording(logptr,0,logptr->autopurge);
364 out_path:
365         kfree(logptr->path);    /* kfree(NULL) is ok. */
366         logptr->path = NULL;
367 out_dev:
368         logptr->dev_in_use = 0;
369         return -EIO;
370 }
371
372
373 static int vmlogrdr_release (struct inode *inode, struct file *filp)
374 {
375         int ret;
376
377         struct vmlogrdr_priv_t * logptr = filp->private_data;
378
379         iucv_path_sever(logptr->path, NULL);
380         kfree(logptr->path);
381         logptr->path = NULL;
382         if (logptr->autorecording) {
383                 ret = vmlogrdr_recording(logptr,0,logptr->autopurge);
384                 if (ret)
385                         printk (KERN_WARNING "vmlogrdr: failed to stop "
386                                 "recording automatically\n");
387         }
388         logptr->dev_in_use = 0;
389
390         return 0;
391 }
392
393
394 static int vmlogrdr_receive_data(struct vmlogrdr_priv_t *priv)
395 {
396         int rc, *temp;
397         /* we need to keep track of two data sizes here:
398          * The number of bytes we need to receive from iucv and
399          * the total number of bytes we actually write into the buffer.
400          */
401         int user_data_count, iucv_data_count;
402         char * buffer;
403
404         if (atomic_read(&priv->receive_ready)) {
405                 spin_lock_bh(&priv->priv_lock);
406                 if (priv->residual_length){
407                         /* receive second half of a record */
408                         iucv_data_count = priv->residual_length;
409                         user_data_count = 0;
410                         buffer = priv->buffer;
411                 } else {
412                         /* receive a new record:
413                          * We need to return the total length of the record
414                          * + size of FENCE in the first 4 bytes of the buffer.
415                          */
416                         iucv_data_count = priv->local_interrupt_buffer.length;
417                         user_data_count = sizeof(int);
418                         temp = (int*)priv->buffer;
419                         *temp= iucv_data_count + sizeof(FENCE);
420                         buffer = priv->buffer + sizeof(int);
421                 }
422                 /*
423                  * If the record is bigger then our buffer, we receive only
424                  * a part of it. We can get the rest later.
425                  */
426                 if (iucv_data_count > NET_BUFFER_SIZE)
427                         iucv_data_count = NET_BUFFER_SIZE;
428                 rc = iucv_message_receive(priv->path,
429                                           &priv->local_interrupt_buffer,
430                                           0, buffer, iucv_data_count,
431                                           &priv->residual_length);
432                 spin_unlock_bh(&priv->priv_lock);
433                 /* An rc of 5 indicates that the record was bigger then
434                  * the buffer, which is OK for us. A 9 indicates that the
435                  * record was purged befor we could receive it.
436                  */
437                 if (rc == 5)
438                         rc = 0;
439                 if (rc == 9)
440                         atomic_set(&priv->receive_ready, 0);
441         } else {
442                 rc = 1;
443         }
444         if (!rc) {
445                 priv->buffer_free = 0;
446                 user_data_count += iucv_data_count;
447                 priv->current_position = priv->buffer;
448                 if (priv->residual_length == 0){
449                         /* the whole record has been captured,
450                          * now add the fence */
451                         atomic_dec(&priv->receive_ready);
452                         buffer = priv->buffer + user_data_count;
453                         memcpy(buffer, FENCE, sizeof(FENCE));
454                         user_data_count += sizeof(FENCE);
455                 }
456                 priv->remaining = user_data_count;
457         }
458
459         return rc;
460 }
461
462
463 static ssize_t vmlogrdr_read(struct file *filp, char __user *data,
464                              size_t count, loff_t * ppos)
465 {
466         int rc;
467         struct vmlogrdr_priv_t * priv = filp->private_data;
468
469         while (priv->buffer_free) {
470                 rc = vmlogrdr_receive_data(priv);
471                 if (rc) {
472                         rc = wait_event_interruptible(read_wait_queue,
473                                         atomic_read(&priv->receive_ready));
474                         if (rc)
475                                 return rc;
476                 }
477         }
478         /* copy only up to end of record */
479         if (count > priv->remaining)
480                 count = priv->remaining;
481
482         if (copy_to_user(data, priv->current_position, count))
483                 return -EFAULT;
484
485         *ppos += count;
486         priv->current_position += count;
487         priv->remaining -= count;
488
489         /* if all data has been transferred, set buffer free */
490         if (priv->remaining == 0)
491                 priv->buffer_free = 1;
492
493         return count;
494 }
495
496 static ssize_t vmlogrdr_autopurge_store(struct device * dev,
497                                         struct device_attribute *attr,
498                                         const char * buf, size_t count)
499 {
500         struct vmlogrdr_priv_t *priv = dev->driver_data;
501         ssize_t ret = count;
502
503         switch (buf[0]) {
504         case '0':
505                 priv->autopurge=0;
506                 break;
507         case '1':
508                 priv->autopurge=1;
509                 break;
510         default:
511                 ret = -EINVAL;
512         }
513         return ret;
514 }
515
516
517 static ssize_t vmlogrdr_autopurge_show(struct device *dev,
518                                        struct device_attribute *attr,
519                                        char *buf)
520 {
521         struct vmlogrdr_priv_t *priv = dev->driver_data;
522         return sprintf(buf, "%u\n", priv->autopurge);
523 }
524
525
526 static DEVICE_ATTR(autopurge, 0644, vmlogrdr_autopurge_show,
527                    vmlogrdr_autopurge_store);
528
529
530 static ssize_t vmlogrdr_purge_store(struct device * dev,
531                                     struct device_attribute *attr,
532                                     const char * buf, size_t count)
533 {
534
535         char cp_command[80];
536         char cp_response[80];
537         struct vmlogrdr_priv_t *priv = dev->driver_data;
538
539         if (buf[0] != '1')
540                 return -EINVAL;
541
542         memset(cp_command, 0x00, sizeof(cp_command));
543         memset(cp_response, 0x00, sizeof(cp_response));
544
545         /*
546          * The recording command needs to be called with option QID
547          * for guests that have previlege classes A or B.
548          * Other guests will not recognize the command and we have to
549          * issue the same command without the QID parameter.
550          */
551
552         if (recording_class_AB)
553                 snprintf(cp_command, sizeof(cp_command),
554                          "RECORDING %s PURGE QID * ",
555                          priv->recording_name);
556         else
557                 snprintf(cp_command, sizeof(cp_command),
558                          "RECORDING %s PURGE ",
559                          priv->recording_name);
560
561         cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
562
563         return count;
564 }
565
566
567 static DEVICE_ATTR(purge, 0200, NULL, vmlogrdr_purge_store);
568
569
570 static ssize_t vmlogrdr_autorecording_store(struct device *dev,
571                                             struct device_attribute *attr,
572                                             const char *buf, size_t count)
573 {
574         struct vmlogrdr_priv_t *priv = dev->driver_data;
575         ssize_t ret = count;
576
577         switch (buf[0]) {
578         case '0':
579                 priv->autorecording=0;
580                 break;
581         case '1':
582                 priv->autorecording=1;
583                 break;
584         default:
585                 ret = -EINVAL;
586         }
587         return ret;
588 }
589
590
591 static ssize_t vmlogrdr_autorecording_show(struct device *dev,
592                                            struct device_attribute *attr,
593                                            char *buf)
594 {
595         struct vmlogrdr_priv_t *priv = dev->driver_data;
596         return sprintf(buf, "%u\n", priv->autorecording);
597 }
598
599
600 static DEVICE_ATTR(autorecording, 0644, vmlogrdr_autorecording_show,
601                    vmlogrdr_autorecording_store);
602
603
604 static ssize_t vmlogrdr_recording_store(struct device * dev,
605                                         struct device_attribute *attr,
606                                         const char * buf, size_t count)
607 {
608         struct vmlogrdr_priv_t *priv = dev->driver_data;
609         ssize_t ret;
610
611         switch (buf[0]) {
612         case '0':
613                 ret = vmlogrdr_recording(priv,0,0);
614                 break;
615         case '1':
616                 ret = vmlogrdr_recording(priv,1,0);
617                 break;
618         default:
619                 ret = -EINVAL;
620         }
621         if (ret)
622                 return ret;
623         else
624                 return count;
625
626 }
627
628
629 static DEVICE_ATTR(recording, 0200, NULL, vmlogrdr_recording_store);
630
631
632 static ssize_t vmlogrdr_recording_status_show(struct device_driver *driver,
633                                               char *buf)
634 {
635
636         char cp_command[] = "QUERY RECORDING ";
637         int len;
638
639         cpcmd(cp_command, buf, 4096, NULL);
640         len = strlen(buf);
641         return len;
642 }
643
644
645 static DRIVER_ATTR(recording_status, 0444, vmlogrdr_recording_status_show,
646                    NULL);
647
648 static struct attribute *vmlogrdr_attrs[] = {
649         &dev_attr_autopurge.attr,
650         &dev_attr_purge.attr,
651         &dev_attr_autorecording.attr,
652         &dev_attr_recording.attr,
653         NULL,
654 };
655
656 static struct attribute_group vmlogrdr_attr_group = {
657         .attrs = vmlogrdr_attrs,
658 };
659
660 static struct class *vmlogrdr_class;
661 static struct device_driver vmlogrdr_driver = {
662         .name = "vmlogrdr",
663         .bus  = &iucv_bus,
664 };
665
666
667 static int vmlogrdr_register_driver(void)
668 {
669         int ret;
670
671         /* Register with iucv driver */
672         ret = iucv_register(&vmlogrdr_iucv_handler, 1);
673         if (ret)
674                 goto out;
675
676         ret = driver_register(&vmlogrdr_driver);
677         if (ret)
678                 goto out_iucv;
679
680         ret = driver_create_file(&vmlogrdr_driver,
681                                  &driver_attr_recording_status);
682         if (ret)
683                 goto out_driver;
684
685         vmlogrdr_class = class_create(THIS_MODULE, "vmlogrdr");
686         if (IS_ERR(vmlogrdr_class)) {
687                 ret = PTR_ERR(vmlogrdr_class);
688                 vmlogrdr_class = NULL;
689                 goto out_attr;
690         }
691         return 0;
692
693 out_attr:
694         driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
695 out_driver:
696         driver_unregister(&vmlogrdr_driver);
697 out_iucv:
698         iucv_unregister(&vmlogrdr_iucv_handler, 1);
699 out:
700         return ret;
701 }
702
703
704 static void vmlogrdr_unregister_driver(void)
705 {
706         class_destroy(vmlogrdr_class);
707         vmlogrdr_class = NULL;
708         driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
709         driver_unregister(&vmlogrdr_driver);
710         iucv_unregister(&vmlogrdr_iucv_handler, 1);
711 }
712
713
714 static int vmlogrdr_register_device(struct vmlogrdr_priv_t *priv)
715 {
716         struct device *dev;
717         int ret;
718
719         dev = kzalloc(sizeof(struct device), GFP_KERNEL);
720         if (dev) {
721                 snprintf(dev->bus_id, BUS_ID_SIZE, "%s",
722                          priv->internal_name);
723                 dev->bus = &iucv_bus;
724                 dev->parent = iucv_root;
725                 dev->driver = &vmlogrdr_driver;
726                 /*
727                  * The release function could be called after the
728                  * module has been unloaded. It's _only_ task is to
729                  * free the struct. Therefore, we specify kfree()
730                  * directly here. (Probably a little bit obfuscating
731                  * but legitime ...).
732                  */
733                 dev->release = (void (*)(struct device *))kfree;
734         } else
735                 return -ENOMEM;
736         ret = device_register(dev);
737         if (ret)
738                 return ret;
739
740         ret = sysfs_create_group(&dev->kobj, &vmlogrdr_attr_group);
741         if (ret) {
742                 device_unregister(dev);
743                 return ret;
744         }
745         priv->class_device = device_create_drvdata(vmlogrdr_class, dev,
746                                                    MKDEV(vmlogrdr_major,
747                                                          priv->minor_num),
748                                                    priv, "%s", dev->bus_id);
749         if (IS_ERR(priv->class_device)) {
750                 ret = PTR_ERR(priv->class_device);
751                 priv->class_device=NULL;
752                 sysfs_remove_group(&dev->kobj, &vmlogrdr_attr_group);
753                 device_unregister(dev);
754                 return ret;
755         }
756         priv->device = dev;
757         return 0;
758 }
759
760
761 static int vmlogrdr_unregister_device(struct vmlogrdr_priv_t *priv)
762 {
763         device_destroy(vmlogrdr_class, MKDEV(vmlogrdr_major, priv->minor_num));
764         if (priv->device != NULL) {
765                 sysfs_remove_group(&priv->device->kobj, &vmlogrdr_attr_group);
766                 device_unregister(priv->device);
767                 priv->device=NULL;
768         }
769         return 0;
770 }
771
772
773 static int vmlogrdr_register_cdev(dev_t dev)
774 {
775         int rc = 0;
776         vmlogrdr_cdev = cdev_alloc();
777         if (!vmlogrdr_cdev) {
778                 return -ENOMEM;
779         }
780         vmlogrdr_cdev->owner = THIS_MODULE;
781         vmlogrdr_cdev->ops = &vmlogrdr_fops;
782         vmlogrdr_cdev->dev = dev;
783         rc = cdev_add(vmlogrdr_cdev, vmlogrdr_cdev->dev, MAXMINOR);
784         if (!rc)
785                 return 0;
786
787         // cleanup: cdev is not fully registered, no cdev_del here!
788         kobject_put(&vmlogrdr_cdev->kobj);
789         vmlogrdr_cdev=NULL;
790         return rc;
791 }
792
793
794 static void vmlogrdr_cleanup(void)
795 {
796         int i;
797
798         if (vmlogrdr_cdev) {
799                 cdev_del(vmlogrdr_cdev);
800                 vmlogrdr_cdev=NULL;
801         }
802         for (i=0; i < MAXMINOR; ++i ) {
803                 vmlogrdr_unregister_device(&sys_ser[i]);
804                 free_page((unsigned long)sys_ser[i].buffer);
805         }
806         vmlogrdr_unregister_driver();
807         if (vmlogrdr_major) {
808                 unregister_chrdev_region(MKDEV(vmlogrdr_major, 0), MAXMINOR);
809                 vmlogrdr_major=0;
810         }
811 }
812
813
814 static int __init vmlogrdr_init(void)
815 {
816         int rc;
817         int i;
818         dev_t dev;
819
820         if (! MACHINE_IS_VM) {
821                 printk (KERN_ERR "vmlogrdr: not running under VM, "
822                                 "driver not loaded.\n");
823                 return -ENODEV;
824         }
825
826         recording_class_AB = vmlogrdr_get_recording_class_AB();
827
828         rc = alloc_chrdev_region(&dev, 0, MAXMINOR, "vmlogrdr");
829         if (rc)
830                 return rc;
831         vmlogrdr_major = MAJOR(dev);
832
833         rc=vmlogrdr_register_driver();
834         if (rc)
835                 goto cleanup;
836
837         for (i=0; i < MAXMINOR; ++i ) {
838                 sys_ser[i].buffer = (char *) get_zeroed_page(GFP_KERNEL);
839                 if (!sys_ser[i].buffer) {
840                         rc = -ENOMEM;
841                         break;
842                 }
843                 sys_ser[i].current_position = sys_ser[i].buffer;
844                 rc=vmlogrdr_register_device(&sys_ser[i]);
845                 if (rc)
846                         break;
847         }
848         if (rc)
849                 goto cleanup;
850
851         rc = vmlogrdr_register_cdev(dev);
852         if (rc)
853                 goto cleanup;
854         return 0;
855
856 cleanup:
857         vmlogrdr_cleanup();
858         return rc;
859 }
860
861
862 static void __exit vmlogrdr_exit(void)
863 {
864         vmlogrdr_cleanup();
865         return;
866 }
867
868
869 module_init(vmlogrdr_init);
870 module_exit(vmlogrdr_exit);