4 Copyright (C) 2005-2008 Martin Koegler <mkoegler@auto.tuwien.ac.at>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <linux/kernel.h>
22 #include <linux/version.h>
23 #include <linux/module.h>
24 #include <linux/device.h>
25 #include <linux/poll.h>
27 #include <linux/err.h>
28 #include <linux/rwsem.h>
30 #include "eib-common.h"
32 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
33 #error This Linux version is no longer supported
36 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
37 #define HAVE_CLASS_DEVICE
39 #undef HAVE_CLASS_DEVICE
42 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
44 #include <linux/platform_device.h>
47 #define MAX_DEVICE_COUNT 256
49 static struct platform_device
*eib_device
;
50 static struct device_driver eib_sysfs_driver
= {
52 .bus
= &platform_bus_type
,
55 static struct class *eib_class
;
62 eib_create_serial_t create
;
65 typedef struct _eib_driver
67 struct _eib_driver
*next
;
71 eib_protocol_ops
*ops
;
74 static eib_port_driver devs
[MAX_DEVICE_COUNT
];
75 static eib_driver
*eib_drivers
= NULL
;
77 static DECLARE_RWSEM (register_lock
);
80 eib_device_release (struct device
*dev
)
86 eib_create_device (eib_driver
* drv
, int minor
, struct device
*dev
)
88 dev_t devid
= MKDEV (drv
->major
, minor
);
89 #ifdef HAVE_CLASS_DEVICE
90 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
91 class_device_create (eib_class
, devid
, dev
, drv
->devicename
, minor
);
93 class_device_create (eib_class
, NULL
, devid
, dev
, drv
->devicename
, minor
);
96 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
97 device_create (eib_class
, dev
, devid
, drv
->devicename
, minor
);
99 device_create (eib_class
, dev
, devid
, 0, drv
->devicename
, minor
);
105 eib_delete_device (eib_driver
* drv
, int minor
)
107 dev_t devid
= MKDEV (drv
->major
, minor
);
108 #ifdef HAVE_CLASS_DEVICE
109 class_device_destroy (eib_class
, devid
);
111 device_destroy (eib_class
, devid
);
116 eib_driver_add (eib_driver
* drv
)
119 for (i
= 0; i
< MAX_DEVICE_COUNT
; i
++)
122 eib_create_device (drv
, i
, devs
[i
].dev
);
127 eib_driver_del (eib_driver
* drv
)
130 for (i
= 0; i
< MAX_DEVICE_COUNT
; i
++)
133 eib_delete_device (drv
, i
);
138 eib_port_add (int minor
)
144 eib_create_device (drv
, minor
, devs
[minor
].dev
);
150 eib_port_del (int minor
)
156 eib_delete_device (drv
, minor
);
162 eib_port_register (struct device
*dev
, eib_create_serial_t create
,
165 int retval
= -ENFILE
;
167 down_write (®ister_lock
);
168 for (i
= 0; i
< MAX_DEVICE_COUNT
; i
++)
171 dev
->parent
= &eib_device
->dev
;
172 dev
->release
= eib_device_release
;
173 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
174 snprintf (dev
->bus_id
, sizeof (dev
->bus_id
), "eib%d", i
);
176 dev_set_name (dev
, "eib%d", i
);
179 retval
= device_register (dev
);
184 devs
[i
].create
= create
;
189 up_write (®ister_lock
);
194 eib_port_unregister (struct device
*dev
)
197 down_write (®ister_lock
);
198 for (i
= 0; i
< MAX_DEVICE_COUNT
; i
++)
199 if (devs
[i
].dev
== dev
)
201 BUG_ON (devs
[i
].used
);
203 device_unregister (dev
);
205 devs
[i
].create
= NULL
;
207 up_write (®ister_lock
);
211 up_write (®ister_lock
);
215 eib_port_create (int minor
, const char *name
,
216 eib_handler_t handler
, void *data
, eib_port
** ops
)
220 if (minor
>= MAX_DEVICE_COUNT
)
221 return ERR_PTR (-ENODEV
);
223 down_read (®ister_lock
);
225 if (!devs
[minor
].dev
)
227 up_read (®ister_lock
);
228 return ERR_PTR (-ENODEV
);
231 if (devs
[minor
].used
)
233 up_read (®ister_lock
);
234 return ERR_PTR (-EBUSY
);
237 if (!try_module_get (devs
[minor
].ops
->owner
))
239 up_read (®ister_lock
);
240 return ERR_PTR (-ENODEV
);
242 *ops
= devs
[minor
].ops
;
244 res
= (*devs
[minor
].create
) (devs
[minor
].dev
, name
, handler
, data
);
246 module_put (devs
[minor
].ops
->owner
);
248 devs
[minor
].used
= 1;
250 up_read (®ister_lock
);
255 eib_port_free (struct device
*dev
)
258 down_write (®ister_lock
);
259 for (i
= 0; i
< MAX_DEVICE_COUNT
; i
++)
260 if (devs
[i
].dev
== dev
)
262 BUG_ON (!devs
[i
].used
);
264 module_put (devs
[i
].ops
->owner
);
265 up_write (®ister_lock
);
269 up_write (®ister_lock
);
273 eib_fasync (int fd
, struct file
*file
, int mode
)
275 eib_chardev
*cdev
= (eib_chardev
*) file
->private_data
;
277 return fasync_helper (fd
, file
, mode
, &cdev
->async
);
281 eib_read (struct file
*file
, char __user
* buf
, size_t count
, loff_t
* offset
)
283 eib_chardev
*cdev
= (eib_chardev
*) file
->private_data
;
287 while (cdev
->read_next
== cdev
->read_free
)
289 if (file
->f_flags
& O_NONBLOCK
) /* read buffer empty */
291 wait_event_interruptible (cdev
->read_queue
,
292 !(cdev
->read_next
== cdev
->read_free
));
294 if (signal_pending (current
))
298 spin_lock (&cdev
->read_lock
);
301 if (cdev
->read_next
== cdev
->read_free
)
303 spin_unlock (&cdev
->read_lock
);
307 rb_tail
= cdev
->read_next
;
308 len
= cdev
->read_buffer
[rb_tail
][0];
312 spin_unlock (&cdev
->read_lock
);
316 if (copy_to_user (buf
, &cdev
->read_buffer
[rb_tail
][1], len
))
318 spin_unlock (&cdev
->read_lock
);
322 cdev
->read_next
= (cdev
->read_next
+ 1) % EIB_MAX_READ_BUFFER
;
324 cdev
->read_head
= (cdev
->read_head
+ 1) % EIB_MAX_READ_BUFFER
;
326 spin_unlock (&cdev
->read_lock
);
332 eib_write (struct file
*file
,
333 const char __user
* buf
, size_t count
, loff_t
* offset
)
335 eib_chardev
*cdev
= (eib_chardev
*) file
->private_data
;
339 if (count
>= sizeof (eib_buffer
) - 1)
342 if (copy_from_user (buffer
+ 1, buf
, count
))
346 while ((cdev
->write_free
+ 1) % EIB_MAX_WRITE_BUFFER
== cdev
->write_head
)
348 /* write buffer full */
350 if (file
->f_flags
& O_NONBLOCK
)
352 wait_event_interruptible (cdev
->write_queue
,
353 !((cdev
->write_free
+
354 1) % EIB_MAX_WRITE_BUFFER
==
357 if (signal_pending (current
))
361 spin_lock (&cdev
->write_lock
);
363 if ((cdev
->write_free
+ 1) % EIB_MAX_WRITE_BUFFER
== cdev
->write_head
)
365 spin_unlock (&cdev
->write_lock
);
369 temp
= cdev
->ops
->write_prepare (cdev
->data
, buffer
, count
);
372 spin_unlock (&cdev
->write_lock
);
376 wb_head
= cdev
->write_free
;
377 cdev
->write_free
= (cdev
->write_free
+ 1) % EIB_MAX_WRITE_BUFFER
;
379 memcpy (cdev
->write_buffer
[wb_head
], buffer
, sizeof (buffer
));
380 cdev
->write_result
[wb_head
] = 0;
382 cdev
->write_next
= (cdev
->write_next
+ 1) % EIB_MAX_WRITE_BUFFER
;
383 spin_unlock (&cdev
->write_lock
);
385 cdev
->ops
->write_start (cdev
->data
);
387 if (file
->f_flags
& O_NONBLOCK
)
390 while (!cdev
->write_result
[wb_head
])
392 /* write buffer full */
394 wait_event_interruptible (cdev
->write_queue
,
395 cdev
->write_result
[wb_head
]);
397 if (signal_pending (current
))
401 if (cdev
->write_result
[wb_head
] > 0)
408 eib_poll (struct file
*file
, poll_table
* wait
)
410 eib_chardev
*cdev
= (eib_chardev
*) file
->private_data
;
411 unsigned int mask
= 0;
413 poll_wait (file
, &cdev
->read_queue
, wait
);
414 poll_wait (file
, &cdev
->write_queue
, wait
);
416 spin_lock (&cdev
->read_lock
);
417 if (cdev
->read_free
!= cdev
->read_next
)
418 mask
|= POLLIN
| POLLRDNORM
;
419 spin_unlock (&cdev
->read_lock
);
421 spin_lock (&cdev
->write_lock
);
422 if ((cdev
->write_free
+ 1) % EIB_MAX_WRITE_BUFFER
!= cdev
->write_head
)
423 mask
|= POLLOUT
| POLLWRNORM
;
424 if (cdev
->write_head
== cdev
->write_next
)
426 spin_unlock (&cdev
->write_lock
);
431 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
433 eib_flush (struct file
*file
)
436 eib_flush (struct file
*file
, fl_owner_t id
)
439 eib_chardev
*cdev
= (eib_chardev
*) file
->private_data
;
440 while (cdev
->write_head
!= cdev
->write_free
)
441 { // write buffer not empty
442 wait_event_interruptible (cdev
->write_queue
,
443 !(cdev
->write_head
!= cdev
->write_free
));
444 if (signal_pending (current
))
452 eib_fsync (struct file
*file
, struct dentry
*d
, int datasync
)
454 eib_chardev
*cdev
= (eib_chardev
*) file
->private_data
;
455 while (cdev
->write_head
!= cdev
->write_free
)
456 { // write buffer not empty
457 wait_event_interruptible (cdev
->write_queue
,
458 !(cdev
->write_head
!= cdev
->write_free
));
459 if (signal_pending (current
))
467 eib_ioctl (struct inode
*inode
,
468 struct file
*file
, unsigned int cmd
, unsigned long arg
)
470 eib_chardev
*cdev
= (eib_chardev
*) file
->private_data
;
471 return cdev
->ops
->ioctl (cdev
->data
, cmd
, arg
);
475 eib_open (struct inode
*inode
, struct file
*file
)
479 unsigned int minor
= iminor (inode
);
480 unsigned int major
= imajor (inode
);
481 eib_protocol_ops
*ops
;
484 down_read (®ister_lock
);
488 if (major
== drv
->major
)
494 up_read (®ister_lock
);
498 up_read (®ister_lock
);
500 cdev
= (void *) kmalloc (sizeof (eib_chardev
), GFP_KERNEL
);
508 spin_lock_init (&cdev
->read_lock
);
509 init_waitqueue_head (&cdev
->read_queue
);
511 cdev
->write_head
= 0;
512 cdev
->write_free
= 0;
513 cdev
->write_next
= 0;
514 spin_lock_init (&cdev
->write_lock
);
515 init_waitqueue_head (&cdev
->write_queue
);
518 if (!try_module_get (cdev
->ops
->owner
))
523 file
->private_data
= cdev
;
524 cdev
->data
= cdev
->ops
->create (minor
, cdev
);
525 if (IS_ERR (cdev
->data
))
527 retval
= PTR_ERR (cdev
->data
);
538 eib_release (struct inode
*inode
, struct file
*file
)
540 eib_chardev
*cdev
= file
->private_data
;
542 cdev
->ops
->shutdown (cdev
->data
);
543 eib_fasync (-1, file
, 0);
545 flush_scheduled_work ();
548 cdev
->ops
->free (cdev
->data
);
549 module_put (cdev
->ops
->owner
);
556 static struct file_operations eib_fops
= {
557 .owner
= THIS_MODULE
,
565 .release
= eib_release
,
566 .fasync
= eib_fasync
,
570 eib_driver_register (const char *name
, int major
, eib_protocol_ops
* ops
)
574 down_write (®ister_lock
);
578 if (!strcmp (name
, drv
->name
))
580 up_write (®ister_lock
);
585 drv
= kmalloc (sizeof (eib_driver
), GFP_KERNEL
);
588 up_write (®ister_lock
);
591 strncpy (drv
->name
, name
, sizeof (drv
->name
));
592 drv
->name
[sizeof (drv
->name
) - 1] = 0;
593 strcpy (drv
->devicename
, drv
->name
);
594 strcat (drv
->devicename
, "%d");
596 retval
= register_chrdev (major
, drv
->name
, &eib_fops
);
600 up_write (®ister_lock
);
607 drv
->next
= eib_drivers
;
609 eib_driver_add (drv
);
610 up_write (®ister_lock
);
615 eib_driver_unregister (const char *name
)
617 eib_driver
*drv
, **drv1
;
618 down_write (®ister_lock
);
623 if (!strcmp (name
, drv
->name
))
626 eib_driver_del (drv
);
628 up_write (®ister_lock
);
635 up_write (®ister_lock
);
639 EXPORT_SYMBOL_GPL (eib_port_register
);
640 EXPORT_SYMBOL_GPL (eib_port_unregister
);
641 EXPORT_SYMBOL_GPL (eib_port_create
);
642 EXPORT_SYMBOL_GPL (eib_port_free
);
643 EXPORT_SYMBOL_GPL (eib_driver_register
);
644 EXPORT_SYMBOL_GPL (eib_driver_unregister
);
647 eib_common_init (void)
651 memset (devs
, 0, sizeof (devs
));
653 retval
= driver_register (&eib_sysfs_driver
);
657 eib_device
= platform_device_register_simple ("eib", -1, NULL
, 0);
658 if (IS_ERR (eib_device
))
660 retval
= PTR_ERR (eib_device
);
664 eib_class
= class_create (THIS_MODULE
, "eib");
666 if (IS_ERR (eib_class
))
668 retval
= PTR_ERR (eib_class
);
672 printk ("EIB core loaded\n");
676 platform_device_unregister (eib_device
);
679 driver_unregister (&eib_sysfs_driver
);
686 eib_common_cleanup (void)
688 class_destroy (eib_class
);
689 platform_device_unregister (eib_device
);
690 driver_unregister (&eib_sysfs_driver
);
691 printk ("EIB core unloaded\n");
694 MODULE_LICENSE ("GPL");
695 MODULE_AUTHOR ("Martin Kögler <mkoegler@auto.tuwien.ac.at>");
696 MODULE_DESCRIPTION ("EIB driver core");
698 module_init (eib_common_init
);
699 module_exit (eib_common_cleanup
);