diff options
author | Stefani Seibold <stefani@seibold.net> | 2009-12-21 17:37:26 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-12-22 17:17:55 -0500 |
commit | 45465487897a1c6d508b14b904dc5777f7ec7e04 (patch) | |
tree | 935c8dae68dc793ff2f795d57cf027531475cd53 /drivers/char | |
parent | 2ec91eec47f713e3d158ba5b28a24a85a2cf3650 (diff) |
kfifo: move struct kfifo in place
This is a new generic kernel FIFO implementation.
The current kernel fifo API is not very widely used, because it has to
many constrains. Only 17 files in the current 2.6.31-rc5 used it.
FIFO's are like list's a very basic thing and a kfifo API which handles
the most use case would save a lot of development time and memory
resources.
I think this are the reasons why kfifo is not in use:
- The API is to simple, important functions are missing
- A fifo can be only allocated dynamically
- There is a requirement of a spinlock whether you need it or not
- There is no support for data records inside a fifo
So I decided to extend the kfifo in a more generic way without blowing up
the API to much. The new API has the following benefits:
- Generic usage: For kernel internal use and/or device driver.
- Provide an API for the most use case.
- Slim API: The whole API provides 25 functions.
- Linux style habit.
- DECLARE_KFIFO, DEFINE_KFIFO and INIT_KFIFO Macros
- Direct copy_to_user from the fifo and copy_from_user into the fifo.
- The kfifo itself is an in place member of the using data structure, this save an
indirection access and does not waste the kernel allocator.
- Lockless access: if only one reader and one writer is active on the fifo,
which is the common use case, no additional locking is necessary.
- Remove spinlock - give the user the freedom of choice what kind of locking to use if
one is required.
- Ability to handle records. Three type of records are supported:
- Variable length records between 0-255 bytes, with a record size
field of 1 bytes.
- Variable length records between 0-65535 bytes, with a record size
field of 2 bytes.
- Fixed size records, which no record size field.
- Preserve memory resource.
- Performance!
- Easy to use!
This patch:
Since most users want to have the kfifo as part of another object,
reorganize the code to allow including struct kfifo in another data
structure. This requires changing the kfifo_alloc and kfifo_init
prototypes so that we pass an existing kfifo pointer into them. This
patch changes the implementation and all existing users.
[akpm@linux-foundation.org: fix warning]
Signed-off-by: Stefani Seibold <stefani@seibold.net>
Acked-by: Greg Kroah-Hartman <gregkh@suse.de>
Acked-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Acked-by: Andi Kleen <ak@linux.intel.com>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/char')
-rw-r--r-- | drivers/char/nozomi.c | 21 | ||||
-rw-r--r-- | drivers/char/sonypi.c | 40 |
2 files changed, 29 insertions, 32 deletions
diff --git a/drivers/char/nozomi.c b/drivers/char/nozomi.c index d3400b20444f..0f39bec28b45 100644 --- a/drivers/char/nozomi.c +++ b/drivers/char/nozomi.c | |||
@@ -358,7 +358,7 @@ struct port { | |||
358 | u8 update_flow_control; | 358 | u8 update_flow_control; |
359 | struct ctrl_ul ctrl_ul; | 359 | struct ctrl_ul ctrl_ul; |
360 | struct ctrl_dl ctrl_dl; | 360 | struct ctrl_dl ctrl_dl; |
361 | struct kfifo *fifo_ul; | 361 | struct kfifo fifo_ul; |
362 | void __iomem *dl_addr[2]; | 362 | void __iomem *dl_addr[2]; |
363 | u32 dl_size[2]; | 363 | u32 dl_size[2]; |
364 | u8 toggle_dl; | 364 | u8 toggle_dl; |
@@ -685,8 +685,8 @@ static int nozomi_read_config_table(struct nozomi *dc) | |||
685 | dump_table(dc); | 685 | dump_table(dc); |
686 | 686 | ||
687 | for (i = PORT_MDM; i < MAX_PORT; i++) { | 687 | for (i = PORT_MDM; i < MAX_PORT; i++) { |
688 | dc->port[i].fifo_ul = | 688 | kfifo_alloc(&dc->port[i].fifo_ul, |
689 | kfifo_alloc(FIFO_BUFFER_SIZE_UL, GFP_ATOMIC, NULL); | 689 | FIFO_BUFFER_SIZE_UL, GFP_ATOMIC, NULL); |
690 | memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl)); | 690 | memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl)); |
691 | memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul)); | 691 | memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul)); |
692 | } | 692 | } |
@@ -798,7 +798,7 @@ static int send_data(enum port_type index, struct nozomi *dc) | |||
798 | struct tty_struct *tty = tty_port_tty_get(&port->port); | 798 | struct tty_struct *tty = tty_port_tty_get(&port->port); |
799 | 799 | ||
800 | /* Get data from tty and place in buf for now */ | 800 | /* Get data from tty and place in buf for now */ |
801 | size = __kfifo_get(port->fifo_ul, dc->send_buf, | 801 | size = __kfifo_get(&port->fifo_ul, dc->send_buf, |
802 | ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX); | 802 | ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX); |
803 | 803 | ||
804 | if (size == 0) { | 804 | if (size == 0) { |
@@ -988,11 +988,11 @@ static int receive_flow_control(struct nozomi *dc) | |||
988 | 988 | ||
989 | } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) { | 989 | } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) { |
990 | 990 | ||
991 | if (__kfifo_len(dc->port[port].fifo_ul)) { | 991 | if (__kfifo_len(&dc->port[port].fifo_ul)) { |
992 | DBG1("Enable interrupt (0x%04X) on port: %d", | 992 | DBG1("Enable interrupt (0x%04X) on port: %d", |
993 | enable_ier, port); | 993 | enable_ier, port); |
994 | DBG1("Data in buffer [%d], enable transmit! ", | 994 | DBG1("Data in buffer [%d], enable transmit! ", |
995 | __kfifo_len(dc->port[port].fifo_ul)); | 995 | __kfifo_len(&dc->port[port].fifo_ul)); |
996 | enable_transmit_ul(port, dc); | 996 | enable_transmit_ul(port, dc); |
997 | } else { | 997 | } else { |
998 | DBG1("No data in buffer..."); | 998 | DBG1("No data in buffer..."); |
@@ -1536,8 +1536,7 @@ static void __devexit nozomi_card_exit(struct pci_dev *pdev) | |||
1536 | free_irq(pdev->irq, dc); | 1536 | free_irq(pdev->irq, dc); |
1537 | 1537 | ||
1538 | for (i = 0; i < MAX_PORT; i++) | 1538 | for (i = 0; i < MAX_PORT; i++) |
1539 | if (dc->port[i].fifo_ul) | 1539 | kfifo_free(&dc->port[i].fifo_ul); |
1540 | kfifo_free(dc->port[i].fifo_ul); | ||
1541 | 1540 | ||
1542 | kfree(dc->send_buf); | 1541 | kfree(dc->send_buf); |
1543 | 1542 | ||
@@ -1673,7 +1672,7 @@ static int ntty_write(struct tty_struct *tty, const unsigned char *buffer, | |||
1673 | goto exit; | 1672 | goto exit; |
1674 | } | 1673 | } |
1675 | 1674 | ||
1676 | rval = __kfifo_put(port->fifo_ul, (unsigned char *)buffer, count); | 1675 | rval = __kfifo_put(&port->fifo_ul, (unsigned char *)buffer, count); |
1677 | 1676 | ||
1678 | /* notify card */ | 1677 | /* notify card */ |
1679 | if (unlikely(dc == NULL)) { | 1678 | if (unlikely(dc == NULL)) { |
@@ -1721,7 +1720,7 @@ static int ntty_write_room(struct tty_struct *tty) | |||
1721 | if (!port->port.count) | 1720 | if (!port->port.count) |
1722 | goto exit; | 1721 | goto exit; |
1723 | 1722 | ||
1724 | room = port->fifo_ul->size - __kfifo_len(port->fifo_ul); | 1723 | room = port->fifo_ul.size - __kfifo_len(&port->fifo_ul); |
1725 | 1724 | ||
1726 | exit: | 1725 | exit: |
1727 | mutex_unlock(&port->tty_sem); | 1726 | mutex_unlock(&port->tty_sem); |
@@ -1878,7 +1877,7 @@ static s32 ntty_chars_in_buffer(struct tty_struct *tty) | |||
1878 | goto exit_in_buffer; | 1877 | goto exit_in_buffer; |
1879 | } | 1878 | } |
1880 | 1879 | ||
1881 | rval = __kfifo_len(port->fifo_ul); | 1880 | rval = __kfifo_len(&port->fifo_ul); |
1882 | 1881 | ||
1883 | exit_in_buffer: | 1882 | exit_in_buffer: |
1884 | return rval; | 1883 | return rval; |
diff --git a/drivers/char/sonypi.c b/drivers/char/sonypi.c index 8c262aaf7c26..9e6efb1f029f 100644 --- a/drivers/char/sonypi.c +++ b/drivers/char/sonypi.c | |||
@@ -487,7 +487,7 @@ static struct sonypi_device { | |||
487 | int camera_power; | 487 | int camera_power; |
488 | int bluetooth_power; | 488 | int bluetooth_power; |
489 | struct mutex lock; | 489 | struct mutex lock; |
490 | struct kfifo *fifo; | 490 | struct kfifo fifo; |
491 | spinlock_t fifo_lock; | 491 | spinlock_t fifo_lock; |
492 | wait_queue_head_t fifo_proc_list; | 492 | wait_queue_head_t fifo_proc_list; |
493 | struct fasync_struct *fifo_async; | 493 | struct fasync_struct *fifo_async; |
@@ -496,7 +496,7 @@ static struct sonypi_device { | |||
496 | struct input_dev *input_jog_dev; | 496 | struct input_dev *input_jog_dev; |
497 | struct input_dev *input_key_dev; | 497 | struct input_dev *input_key_dev; |
498 | struct work_struct input_work; | 498 | struct work_struct input_work; |
499 | struct kfifo *input_fifo; | 499 | struct kfifo input_fifo; |
500 | spinlock_t input_fifo_lock; | 500 | spinlock_t input_fifo_lock; |
501 | } sonypi_device; | 501 | } sonypi_device; |
502 | 502 | ||
@@ -777,7 +777,7 @@ static void input_keyrelease(struct work_struct *work) | |||
777 | { | 777 | { |
778 | struct sonypi_keypress kp; | 778 | struct sonypi_keypress kp; |
779 | 779 | ||
780 | while (kfifo_get(sonypi_device.input_fifo, (unsigned char *)&kp, | 780 | while (kfifo_get(&sonypi_device.input_fifo, (unsigned char *)&kp, |
781 | sizeof(kp)) == sizeof(kp)) { | 781 | sizeof(kp)) == sizeof(kp)) { |
782 | msleep(10); | 782 | msleep(10); |
783 | input_report_key(kp.dev, kp.key, 0); | 783 | input_report_key(kp.dev, kp.key, 0); |
@@ -827,7 +827,7 @@ static void sonypi_report_input_event(u8 event) | |||
827 | if (kp.dev) { | 827 | if (kp.dev) { |
828 | input_report_key(kp.dev, kp.key, 1); | 828 | input_report_key(kp.dev, kp.key, 1); |
829 | input_sync(kp.dev); | 829 | input_sync(kp.dev); |
830 | kfifo_put(sonypi_device.input_fifo, | 830 | kfifo_put(&sonypi_device.input_fifo, |
831 | (unsigned char *)&kp, sizeof(kp)); | 831 | (unsigned char *)&kp, sizeof(kp)); |
832 | schedule_work(&sonypi_device.input_work); | 832 | schedule_work(&sonypi_device.input_work); |
833 | } | 833 | } |
@@ -880,7 +880,7 @@ found: | |||
880 | acpi_bus_generate_proc_event(sonypi_acpi_device, 1, event); | 880 | acpi_bus_generate_proc_event(sonypi_acpi_device, 1, event); |
881 | #endif | 881 | #endif |
882 | 882 | ||
883 | kfifo_put(sonypi_device.fifo, (unsigned char *)&event, sizeof(event)); | 883 | kfifo_put(&sonypi_device.fifo, (unsigned char *)&event, sizeof(event)); |
884 | kill_fasync(&sonypi_device.fifo_async, SIGIO, POLL_IN); | 884 | kill_fasync(&sonypi_device.fifo_async, SIGIO, POLL_IN); |
885 | wake_up_interruptible(&sonypi_device.fifo_proc_list); | 885 | wake_up_interruptible(&sonypi_device.fifo_proc_list); |
886 | 886 | ||
@@ -906,7 +906,7 @@ static int sonypi_misc_open(struct inode *inode, struct file *file) | |||
906 | mutex_lock(&sonypi_device.lock); | 906 | mutex_lock(&sonypi_device.lock); |
907 | /* Flush input queue on first open */ | 907 | /* Flush input queue on first open */ |
908 | if (!sonypi_device.open_count) | 908 | if (!sonypi_device.open_count) |
909 | kfifo_reset(sonypi_device.fifo); | 909 | kfifo_reset(&sonypi_device.fifo); |
910 | sonypi_device.open_count++; | 910 | sonypi_device.open_count++; |
911 | mutex_unlock(&sonypi_device.lock); | 911 | mutex_unlock(&sonypi_device.lock); |
912 | unlock_kernel(); | 912 | unlock_kernel(); |
@@ -919,17 +919,17 @@ static ssize_t sonypi_misc_read(struct file *file, char __user *buf, | |||
919 | ssize_t ret; | 919 | ssize_t ret; |
920 | unsigned char c; | 920 | unsigned char c; |
921 | 921 | ||
922 | if ((kfifo_len(sonypi_device.fifo) == 0) && | 922 | if ((kfifo_len(&sonypi_device.fifo) == 0) && |
923 | (file->f_flags & O_NONBLOCK)) | 923 | (file->f_flags & O_NONBLOCK)) |
924 | return -EAGAIN; | 924 | return -EAGAIN; |
925 | 925 | ||
926 | ret = wait_event_interruptible(sonypi_device.fifo_proc_list, | 926 | ret = wait_event_interruptible(sonypi_device.fifo_proc_list, |
927 | kfifo_len(sonypi_device.fifo) != 0); | 927 | kfifo_len(&sonypi_device.fifo) != 0); |
928 | if (ret) | 928 | if (ret) |
929 | return ret; | 929 | return ret; |
930 | 930 | ||
931 | while (ret < count && | 931 | while (ret < count && |
932 | (kfifo_get(sonypi_device.fifo, &c, sizeof(c)) == sizeof(c))) { | 932 | (kfifo_get(&sonypi_device.fifo, &c, sizeof(c)) == sizeof(c))) { |
933 | if (put_user(c, buf++)) | 933 | if (put_user(c, buf++)) |
934 | return -EFAULT; | 934 | return -EFAULT; |
935 | ret++; | 935 | ret++; |
@@ -946,7 +946,7 @@ static ssize_t sonypi_misc_read(struct file *file, char __user *buf, | |||
946 | static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait) | 946 | static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait) |
947 | { | 947 | { |
948 | poll_wait(file, &sonypi_device.fifo_proc_list, wait); | 948 | poll_wait(file, &sonypi_device.fifo_proc_list, wait); |
949 | if (kfifo_len(sonypi_device.fifo)) | 949 | if (kfifo_len(&sonypi_device.fifo)) |
950 | return POLLIN | POLLRDNORM; | 950 | return POLLIN | POLLRDNORM; |
951 | return 0; | 951 | return 0; |
952 | } | 952 | } |
@@ -1313,11 +1313,11 @@ static int __devinit sonypi_probe(struct platform_device *dev) | |||
1313 | "http://www.linux.it/~malattia/wiki/index.php/Sony_drivers\n"); | 1313 | "http://www.linux.it/~malattia/wiki/index.php/Sony_drivers\n"); |
1314 | 1314 | ||
1315 | spin_lock_init(&sonypi_device.fifo_lock); | 1315 | spin_lock_init(&sonypi_device.fifo_lock); |
1316 | sonypi_device.fifo = kfifo_alloc(SONYPI_BUF_SIZE, GFP_KERNEL, | 1316 | error = kfifo_alloc(&sonypi_device.fifo, SONYPI_BUF_SIZE, GFP_KERNEL, |
1317 | &sonypi_device.fifo_lock); | 1317 | &sonypi_device.fifo_lock); |
1318 | if (IS_ERR(sonypi_device.fifo)) { | 1318 | if (error) { |
1319 | printk(KERN_ERR "sonypi: kfifo_alloc failed\n"); | 1319 | printk(KERN_ERR "sonypi: kfifo_alloc failed\n"); |
1320 | return PTR_ERR(sonypi_device.fifo); | 1320 | return error; |
1321 | } | 1321 | } |
1322 | 1322 | ||
1323 | init_waitqueue_head(&sonypi_device.fifo_proc_list); | 1323 | init_waitqueue_head(&sonypi_device.fifo_proc_list); |
@@ -1393,12 +1393,10 @@ static int __devinit sonypi_probe(struct platform_device *dev) | |||
1393 | } | 1393 | } |
1394 | 1394 | ||
1395 | spin_lock_init(&sonypi_device.input_fifo_lock); | 1395 | spin_lock_init(&sonypi_device.input_fifo_lock); |
1396 | sonypi_device.input_fifo = | 1396 | error = kfifo_alloc(&sonypi_device.input_fifo, SONYPI_BUF_SIZE, |
1397 | kfifo_alloc(SONYPI_BUF_SIZE, GFP_KERNEL, | 1397 | GFP_KERNEL, &sonypi_device.input_fifo_lock); |
1398 | &sonypi_device.input_fifo_lock); | 1398 | if (error) { |
1399 | if (IS_ERR(sonypi_device.input_fifo)) { | ||
1400 | printk(KERN_ERR "sonypi: kfifo_alloc failed\n"); | 1399 | printk(KERN_ERR "sonypi: kfifo_alloc failed\n"); |
1401 | error = PTR_ERR(sonypi_device.input_fifo); | ||
1402 | goto err_inpdev_unregister; | 1400 | goto err_inpdev_unregister; |
1403 | } | 1401 | } |
1404 | 1402 | ||
@@ -1423,7 +1421,7 @@ static int __devinit sonypi_probe(struct platform_device *dev) | |||
1423 | pci_disable_device(pcidev); | 1421 | pci_disable_device(pcidev); |
1424 | err_put_pcidev: | 1422 | err_put_pcidev: |
1425 | pci_dev_put(pcidev); | 1423 | pci_dev_put(pcidev); |
1426 | kfifo_free(sonypi_device.fifo); | 1424 | kfifo_free(&sonypi_device.fifo); |
1427 | 1425 | ||
1428 | return error; | 1426 | return error; |
1429 | } | 1427 | } |
@@ -1438,7 +1436,7 @@ static int __devexit sonypi_remove(struct platform_device *dev) | |||
1438 | if (useinput) { | 1436 | if (useinput) { |
1439 | input_unregister_device(sonypi_device.input_key_dev); | 1437 | input_unregister_device(sonypi_device.input_key_dev); |
1440 | input_unregister_device(sonypi_device.input_jog_dev); | 1438 | input_unregister_device(sonypi_device.input_jog_dev); |
1441 | kfifo_free(sonypi_device.input_fifo); | 1439 | kfifo_free(&sonypi_device.input_fifo); |
1442 | } | 1440 | } |
1443 | 1441 | ||
1444 | misc_deregister(&sonypi_misc_device); | 1442 | misc_deregister(&sonypi_misc_device); |
@@ -1451,7 +1449,7 @@ static int __devexit sonypi_remove(struct platform_device *dev) | |||
1451 | pci_dev_put(sonypi_device.dev); | 1449 | pci_dev_put(sonypi_device.dev); |
1452 | } | 1450 | } |
1453 | 1451 | ||
1454 | kfifo_free(sonypi_device.fifo); | 1452 | kfifo_free(&sonypi_device.fifo); |
1455 | 1453 | ||
1456 | return 0; | 1454 | return 0; |
1457 | } | 1455 | } |