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