Add support up to 2.6.31
[bcu1driver.git] / eib-common.c
blobd77c1b3f885bbf7e00952cab2ef9f35c836523d7
1 /*
2 EIB driver core
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>
26 #include <linux/fs.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
34 #endif
36 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
37 #define HAVE_CLASS_DEVICE
38 #else
39 #undef HAVE_CLASS_DEVICE
40 #endif
42 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
43 #else
44 #include <linux/platform_device.h>
45 #endif
47 #define MAX_DEVICE_COUNT 256
49 static struct platform_device *eib_device;
50 static struct device_driver eib_sysfs_driver = {
51 .name = "eib",
52 .bus = &platform_bus_type,
55 static struct class *eib_class;
57 typedef struct
59 struct device *dev;
60 eib_port *ops;
61 int used;
62 eib_create_serial_t create;
63 } eib_port_driver;
65 typedef struct _eib_driver
67 struct _eib_driver *next;
68 char name[15];
69 char devicename[17];
70 int major;
71 eib_protocol_ops *ops;
72 } eib_driver;
74 static eib_port_driver devs[MAX_DEVICE_COUNT];
75 static eib_driver *eib_drivers = NULL;
77 static DECLARE_RWSEM (register_lock);
79 static void
80 eib_device_release (struct device *dev)
82 return;
85 static void
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);
92 #else
93 class_device_create (eib_class, NULL, devid, dev, drv->devicename, minor);
94 #endif
95 #else
96 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
97 device_create (eib_class, dev, devid, drv->devicename, minor);
98 #else
99 device_create (eib_class, dev, devid, 0, drv->devicename, minor);
100 #endif
101 #endif
104 static void
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);
110 #else
111 device_destroy (eib_class, devid);
112 #endif
115 static void
116 eib_driver_add (eib_driver * drv)
118 int i;
119 for (i = 0; i < MAX_DEVICE_COUNT; i++)
120 if (devs[i].dev)
122 eib_create_device (drv, i, devs[i].dev);
126 static void
127 eib_driver_del (eib_driver * drv)
129 int i;
130 for (i = 0; i < MAX_DEVICE_COUNT; i++)
131 if (devs[i].dev)
133 eib_delete_device (drv, i);
137 static void
138 eib_port_add (int minor)
140 eib_driver *drv;
141 drv = eib_drivers;
142 while (drv)
144 eib_create_device (drv, minor, devs[minor].dev);
145 drv = drv->next;
149 static void
150 eib_port_del (int minor)
152 eib_driver *drv;
153 drv = eib_drivers;
154 while (drv)
156 eib_delete_device (drv, minor);
157 drv = drv->next;
162 eib_port_register (struct device *dev, eib_create_serial_t create,
163 eib_port * ops)
165 int retval = -ENFILE;
166 int i;
167 down_write (&register_lock);
168 for (i = 0; i < MAX_DEVICE_COUNT; i++)
169 if (!devs[i].dev)
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);
175 #else
176 dev_set_name (dev, "eib%d", i);
177 #endif
179 retval = device_register (dev);
180 if (!retval)
182 devs[i].ops = ops;
183 devs[i].dev = dev;
184 devs[i].create = create;
185 eib_port_add (i);
187 break;
189 up_write (&register_lock);
190 return retval;
193 void
194 eib_port_unregister (struct device *dev)
196 int i;
197 down_write (&register_lock);
198 for (i = 0; i < MAX_DEVICE_COUNT; i++)
199 if (devs[i].dev == dev)
201 BUG_ON (devs[i].used);
202 eib_port_del (i);
203 device_unregister (dev);
204 devs[i].dev = NULL;
205 devs[i].create = NULL;
206 devs[i].ops = NULL;
207 up_write (&register_lock);
208 return;
210 BUG_ON (1);
211 up_write (&register_lock);
214 eib_lowlevel *
215 eib_port_create (int minor, const char *name,
216 eib_handler_t handler, void *data, eib_port ** ops)
218 eib_lowlevel *res;
220 if (minor >= MAX_DEVICE_COUNT)
221 return ERR_PTR (-ENODEV);
223 down_read (&register_lock);
225 if (!devs[minor].dev)
227 up_read (&register_lock);
228 return ERR_PTR (-ENODEV);
231 if (devs[minor].used)
233 up_read (&register_lock);
234 return ERR_PTR (-EBUSY);
237 if (!try_module_get (devs[minor].ops->owner))
239 up_read (&register_lock);
240 return ERR_PTR (-ENODEV);
242 *ops = devs[minor].ops;
244 res = (*devs[minor].create) (devs[minor].dev, name, handler, data);
245 if (IS_ERR (res))
246 module_put (devs[minor].ops->owner);
247 else
248 devs[minor].used = 1;
250 up_read (&register_lock);
251 return res;
254 void
255 eib_port_free (struct device *dev)
257 int i;
258 down_write (&register_lock);
259 for (i = 0; i < MAX_DEVICE_COUNT; i++)
260 if (devs[i].dev == dev)
262 BUG_ON (!devs[i].used);
263 devs[i].used = 0;
264 module_put (devs[i].ops->owner);
265 up_write (&register_lock);
266 return;
268 BUG_ON (1);
269 up_write (&register_lock);
272 static int
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);
280 static ssize_t
281 eib_read (struct file *file, char __user * buf, size_t count, loff_t * offset)
283 eib_chardev *cdev = (eib_chardev *) file->private_data;
284 int rb_tail, len;
286 retry:
287 while (cdev->read_next == cdev->read_free)
289 if (file->f_flags & O_NONBLOCK) /* read buffer empty */
290 return -EAGAIN;
291 wait_event_interruptible (cdev->read_queue,
292 !(cdev->read_next == cdev->read_free));
294 if (signal_pending (current))
295 return -ERESTARTSYS;
298 spin_lock (&cdev->read_lock);
301 if (cdev->read_next == cdev->read_free)
303 spin_unlock (&cdev->read_lock);
304 goto retry;
307 rb_tail = cdev->read_next;
308 len = cdev->read_buffer[rb_tail][0];
310 if (count < len)
312 spin_unlock (&cdev->read_lock);
313 return -ENOMSG;
316 if (copy_to_user (buf, &cdev->read_buffer[rb_tail][1], len))
318 spin_unlock (&cdev->read_lock);
319 return -EFAULT;
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);
328 return len;
331 static ssize_t
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;
336 int temp, wb_head;
337 eib_buffer buffer;
339 if (count >= sizeof (eib_buffer) - 1)
340 return -EINVAL;
342 if (copy_from_user (buffer + 1, buf, count))
343 return -EFAULT;
345 retry:
346 while ((cdev->write_free + 1) % EIB_MAX_WRITE_BUFFER == cdev->write_head)
348 /* write buffer full */
350 if (file->f_flags & O_NONBLOCK)
351 return -EAGAIN;
352 wait_event_interruptible (cdev->write_queue,
353 !((cdev->write_free +
354 1) % EIB_MAX_WRITE_BUFFER ==
355 cdev->write_head));
357 if (signal_pending (current))
358 return -ERESTARTSYS;
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);
366 goto retry;
369 temp = cdev->ops->write_prepare (cdev->data, buffer, count);
370 if (temp < 0)
372 spin_unlock (&cdev->write_lock);
373 return temp;
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)
388 return count;
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))
398 return -EINTR;
401 if (cdev->write_result[wb_head] > 0)
402 return count;
403 else
404 return 0;
407 static unsigned int
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)
425 mask |= POLLPRI;
426 spin_unlock (&cdev->write_lock);
428 return mask;
431 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
432 static int
433 eib_flush (struct file *file)
434 #else
435 static int
436 eib_flush (struct file *file, fl_owner_t id)
437 #endif
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))
445 return -ERESTARTSYS;
448 return 0;
451 static int
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))
460 return -ERESTARTSYS;
463 return 0;
466 static int
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);
474 static int
475 eib_open (struct inode *inode, struct file *file)
477 eib_chardev *cdev;
478 int retval;
479 unsigned int minor = iminor (inode);
480 unsigned int major = imajor (inode);
481 eib_protocol_ops *ops;
482 eib_driver *drv;
484 down_read (&register_lock);
485 drv = eib_drivers;
486 while (drv)
488 if (major == drv->major)
489 break;
490 drv = drv->next;
492 if (!drv)
494 up_read (&register_lock);
495 return -ENODEV;
497 ops = drv->ops;
498 up_read (&register_lock);
500 cdev = (void *) kmalloc (sizeof (eib_chardev), GFP_KERNEL);
501 if (!cdev)
502 return -ENOMEM;
503 cdev->async = NULL;
505 cdev->read_head = 0;
506 cdev->read_free = 0;
507 cdev->read_next = 0;
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);
517 cdev->ops = ops;
518 if (!try_module_get (cdev->ops->owner))
520 retval = -ENODEV;
521 goto out;
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);
528 goto out;
531 return 0;
532 out:
533 kfree (cdev);
534 return retval;
537 static int
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 ();
546 synchronize_rcu ();
548 cdev->ops->free (cdev->data);
549 module_put (cdev->ops->owner);
551 kfree (cdev);
553 return 0;
556 static struct file_operations eib_fops = {
557 .owner = THIS_MODULE,
558 .read = eib_read,
559 .write = eib_write,
560 .poll = eib_poll,
561 .ioctl = eib_ioctl,
562 .open = eib_open,
563 .flush = eib_flush,
564 .fsync = eib_fsync,
565 .release = eib_release,
566 .fasync = eib_fasync,
570 eib_driver_register (const char *name, int major, eib_protocol_ops * ops)
572 int retval;
573 eib_driver *drv;
574 down_write (&register_lock);
575 drv = eib_drivers;
576 while (drv)
578 if (!strcmp (name, drv->name))
580 up_write (&register_lock);
581 return -EEXIST;
583 drv = drv->next;
585 drv = kmalloc (sizeof (eib_driver), GFP_KERNEL);
586 if (!drv)
588 up_write (&register_lock);
589 return -ENOMEM;
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");
595 drv->ops = ops;
596 retval = register_chrdev (major, drv->name, &eib_fops);
597 if (drv->major < 0)
599 kfree (drv);
600 up_write (&register_lock);
601 return retval;
603 if (major == 0)
604 drv->major = retval;
605 else
606 drv->major = major;
607 drv->next = eib_drivers;
608 eib_drivers = drv;
609 eib_driver_add (drv);
610 up_write (&register_lock);
611 return 0;
614 void
615 eib_driver_unregister (const char *name)
617 eib_driver *drv, **drv1;
618 down_write (&register_lock);
619 drv = eib_drivers;
620 drv1 = &eib_drivers;
621 while (drv)
623 if (!strcmp (name, drv->name))
625 *drv1 = drv->next;
626 eib_driver_del (drv);
627 kfree (drv);
628 up_write (&register_lock);
629 return;
631 drv1 = &drv->next;
632 drv = drv->next;
634 BUG_ON (1);
635 up_write (&register_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);
646 static int __init
647 eib_common_init (void)
649 int retval;
651 memset (devs, 0, sizeof (devs));
653 retval = driver_register (&eib_sysfs_driver);
654 if (retval != 0)
655 goto unreg1;
657 eib_device = platform_device_register_simple ("eib", -1, NULL, 0);
658 if (IS_ERR (eib_device))
660 retval = PTR_ERR (eib_device);
661 goto unreg2;
664 eib_class = class_create (THIS_MODULE, "eib");
666 if (IS_ERR (eib_class))
668 retval = PTR_ERR (eib_class);
669 goto unreg3;
672 printk ("EIB core loaded\n");
673 return 0;
675 unreg3:
676 platform_device_unregister (eib_device);
678 unreg2:
679 driver_unregister (&eib_sysfs_driver);
681 unreg1:
682 return retval;
685 static void __exit
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);