aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/input.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/input.c')
-rw-r--r--drivers/input/input.c192
1 files changed, 144 insertions, 48 deletions
diff --git a/drivers/input/input.c b/drivers/input/input.c
index acb3c8095c65..832771e73663 100644
--- a/drivers/input/input.c
+++ b/drivers/input/input.c
@@ -634,78 +634,141 @@ static void input_disconnect_device(struct input_dev *dev)
634 spin_unlock_irq(&dev->event_lock); 634 spin_unlock_irq(&dev->event_lock);
635} 635}
636 636
637static int input_fetch_keycode(struct input_dev *dev, int scancode) 637/**
638 * input_scancode_to_scalar() - converts scancode in &struct input_keymap_entry
639 * @ke: keymap entry containing scancode to be converted.
640 * @scancode: pointer to the location where converted scancode should
641 * be stored.
642 *
643 * This function is used to convert scancode stored in &struct keymap_entry
644 * into scalar form understood by legacy keymap handling methods. These
645 * methods expect scancodes to be represented as 'unsigned int'.
646 */
647int input_scancode_to_scalar(const struct input_keymap_entry *ke,
648 unsigned int *scancode)
649{
650 switch (ke->len) {
651 case 1:
652 *scancode = *((u8 *)ke->scancode);
653 break;
654
655 case 2:
656 *scancode = *((u16 *)ke->scancode);
657 break;
658
659 case 4:
660 *scancode = *((u32 *)ke->scancode);
661 break;
662
663 default:
664 return -EINVAL;
665 }
666
667 return 0;
668}
669EXPORT_SYMBOL(input_scancode_to_scalar);
670
671/*
672 * Those routines handle the default case where no [gs]etkeycode() is
673 * defined. In this case, an array indexed by the scancode is used.
674 */
675
676static unsigned int input_fetch_keycode(struct input_dev *dev,
677 unsigned int index)
638{ 678{
639 switch (dev->keycodesize) { 679 switch (dev->keycodesize) {
640 case 1: 680 case 1:
641 return ((u8 *)dev->keycode)[scancode]; 681 return ((u8 *)dev->keycode)[index];
642 682
643 case 2: 683 case 2:
644 return ((u16 *)dev->keycode)[scancode]; 684 return ((u16 *)dev->keycode)[index];
645 685
646 default: 686 default:
647 return ((u32 *)dev->keycode)[scancode]; 687 return ((u32 *)dev->keycode)[index];
648 } 688 }
649} 689}
650 690
651static int input_default_getkeycode(struct input_dev *dev, 691static int input_default_getkeycode(struct input_dev *dev,
652 unsigned int scancode, 692 struct input_keymap_entry *ke)
653 unsigned int *keycode)
654{ 693{
694 unsigned int index;
695 int error;
696
655 if (!dev->keycodesize) 697 if (!dev->keycodesize)
656 return -EINVAL; 698 return -EINVAL;
657 699
658 if (scancode >= dev->keycodemax) 700 if (ke->flags & INPUT_KEYMAP_BY_INDEX)
701 index = ke->index;
702 else {
703 error = input_scancode_to_scalar(ke, &index);
704 if (error)
705 return error;
706 }
707
708 if (index >= dev->keycodemax)
659 return -EINVAL; 709 return -EINVAL;
660 710
661 *keycode = input_fetch_keycode(dev, scancode); 711 ke->keycode = input_fetch_keycode(dev, index);
712 ke->index = index;
713 ke->len = sizeof(index);
714 memcpy(ke->scancode, &index, sizeof(index));
662 715
663 return 0; 716 return 0;
664} 717}
665 718
666static int input_default_setkeycode(struct input_dev *dev, 719static int input_default_setkeycode(struct input_dev *dev,
667 unsigned int scancode, 720 const struct input_keymap_entry *ke,
668 unsigned int keycode) 721 unsigned int *old_keycode)
669{ 722{
670 int old_keycode; 723 unsigned int index;
724 int error;
671 int i; 725 int i;
672 726
673 if (scancode >= dev->keycodemax) 727 if (!dev->keycodesize)
674 return -EINVAL; 728 return -EINVAL;
675 729
676 if (!dev->keycodesize) 730 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
731 index = ke->index;
732 } else {
733 error = input_scancode_to_scalar(ke, &index);
734 if (error)
735 return error;
736 }
737
738 if (index >= dev->keycodemax)
677 return -EINVAL; 739 return -EINVAL;
678 740
679 if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8))) 741 if (dev->keycodesize < sizeof(dev->keycode) &&
742 (ke->keycode >> (dev->keycodesize * 8)))
680 return -EINVAL; 743 return -EINVAL;
681 744
682 switch (dev->keycodesize) { 745 switch (dev->keycodesize) {
683 case 1: { 746 case 1: {
684 u8 *k = (u8 *)dev->keycode; 747 u8 *k = (u8 *)dev->keycode;
685 old_keycode = k[scancode]; 748 *old_keycode = k[index];
686 k[scancode] = keycode; 749 k[index] = ke->keycode;
687 break; 750 break;
688 } 751 }
689 case 2: { 752 case 2: {
690 u16 *k = (u16 *)dev->keycode; 753 u16 *k = (u16 *)dev->keycode;
691 old_keycode = k[scancode]; 754 *old_keycode = k[index];
692 k[scancode] = keycode; 755 k[index] = ke->keycode;
693 break; 756 break;
694 } 757 }
695 default: { 758 default: {
696 u32 *k = (u32 *)dev->keycode; 759 u32 *k = (u32 *)dev->keycode;
697 old_keycode = k[scancode]; 760 *old_keycode = k[index];
698 k[scancode] = keycode; 761 k[index] = ke->keycode;
699 break; 762 break;
700 } 763 }
701 } 764 }
702 765
703 __clear_bit(old_keycode, dev->keybit); 766 __clear_bit(*old_keycode, dev->keybit);
704 __set_bit(keycode, dev->keybit); 767 __set_bit(ke->keycode, dev->keybit);
705 768
706 for (i = 0; i < dev->keycodemax; i++) { 769 for (i = 0; i < dev->keycodemax; i++) {
707 if (input_fetch_keycode(dev, i) == old_keycode) { 770 if (input_fetch_keycode(dev, i) == *old_keycode) {
708 __set_bit(old_keycode, dev->keybit); 771 __set_bit(*old_keycode, dev->keybit);
709 break; /* Setting the bit twice is useless, so break */ 772 break; /* Setting the bit twice is useless, so break */
710 } 773 }
711 } 774 }
@@ -716,53 +779,86 @@ static int input_default_setkeycode(struct input_dev *dev,
716/** 779/**
717 * input_get_keycode - retrieve keycode currently mapped to a given scancode 780 * input_get_keycode - retrieve keycode currently mapped to a given scancode
718 * @dev: input device which keymap is being queried 781 * @dev: input device which keymap is being queried
719 * @scancode: scancode (or its equivalent for device in question) for which 782 * @ke: keymap entry
720 * keycode is needed
721 * @keycode: result
722 * 783 *
723 * This function should be called by anyone interested in retrieving current 784 * This function should be called by anyone interested in retrieving current
724 * keymap. Presently keyboard and evdev handlers use it. 785 * keymap. Presently evdev handlers use it.
725 */ 786 */
726int input_get_keycode(struct input_dev *dev, 787int input_get_keycode(struct input_dev *dev, struct input_keymap_entry *ke)
727 unsigned int scancode, unsigned int *keycode)
728{ 788{
729 unsigned long flags; 789 unsigned long flags;
730 int retval; 790 int retval;
731 791
732 spin_lock_irqsave(&dev->event_lock, flags); 792 spin_lock_irqsave(&dev->event_lock, flags);
733 retval = dev->getkeycode(dev, scancode, keycode);
734 spin_unlock_irqrestore(&dev->event_lock, flags);
735 793
794 if (dev->getkeycode) {
795 /*
796 * Support for legacy drivers, that don't implement the new
797 * ioctls
798 */
799 u32 scancode = ke->index;
800
801 memcpy(ke->scancode, &scancode, sizeof(scancode));
802 ke->len = sizeof(scancode);
803 retval = dev->getkeycode(dev, scancode, &ke->keycode);
804 } else {
805 retval = dev->getkeycode_new(dev, ke);
806 }
807
808 spin_unlock_irqrestore(&dev->event_lock, flags);
736 return retval; 809 return retval;
737} 810}
738EXPORT_SYMBOL(input_get_keycode); 811EXPORT_SYMBOL(input_get_keycode);
739 812
740/** 813/**
741 * input_get_keycode - assign new keycode to a given scancode 814 * input_set_keycode - attribute a keycode to a given scancode
742 * @dev: input device which keymap is being updated 815 * @dev: input device which keymap is being updated
743 * @scancode: scancode (or its equivalent for device in question) 816 * @ke: new keymap entry
744 * @keycode: new keycode to be assigned to the scancode
745 * 817 *
746 * This function should be called by anyone needing to update current 818 * This function should be called by anyone needing to update current
747 * keymap. Presently keyboard and evdev handlers use it. 819 * keymap. Presently keyboard and evdev handlers use it.
748 */ 820 */
749int input_set_keycode(struct input_dev *dev, 821int input_set_keycode(struct input_dev *dev,
750 unsigned int scancode, unsigned int keycode) 822 const struct input_keymap_entry *ke)
751{ 823{
752 unsigned long flags; 824 unsigned long flags;
753 unsigned int old_keycode; 825 unsigned int old_keycode;
754 int retval; 826 int retval;
755 827
756 if (keycode > KEY_MAX) 828 if (ke->keycode > KEY_MAX)
757 return -EINVAL; 829 return -EINVAL;
758 830
759 spin_lock_irqsave(&dev->event_lock, flags); 831 spin_lock_irqsave(&dev->event_lock, flags);
760 832
761 retval = dev->getkeycode(dev, scancode, &old_keycode); 833 if (dev->setkeycode) {
762 if (retval) 834 /*
763 goto out; 835 * Support for legacy drivers, that don't implement the new
836 * ioctls
837 */
838 unsigned int scancode;
839
840 retval = input_scancode_to_scalar(ke, &scancode);
841 if (retval)
842 goto out;
843
844 /*
845 * We need to know the old scancode, in order to generate a
846 * keyup effect, if the set operation happens successfully
847 */
848 if (!dev->getkeycode) {
849 retval = -EINVAL;
850 goto out;
851 }
852
853 retval = dev->getkeycode(dev, scancode, &old_keycode);
854 if (retval)
855 goto out;
856
857 retval = dev->setkeycode(dev, scancode, ke->keycode);
858 } else {
859 retval = dev->setkeycode_new(dev, ke, &old_keycode);
860 }
764 861
765 retval = dev->setkeycode(dev, scancode, keycode);
766 if (retval) 862 if (retval)
767 goto out; 863 goto out;
768 864
@@ -1759,11 +1855,11 @@ int input_register_device(struct input_dev *dev)
1759 dev->rep[REP_PERIOD] = 33; 1855 dev->rep[REP_PERIOD] = 33;
1760 } 1856 }
1761 1857
1762 if (!dev->getkeycode) 1858 if (!dev->getkeycode && !dev->getkeycode_new)
1763 dev->getkeycode = input_default_getkeycode; 1859 dev->getkeycode_new = input_default_getkeycode;
1764 1860
1765 if (!dev->setkeycode) 1861 if (!dev->setkeycode && !dev->setkeycode_new)
1766 dev->setkeycode = input_default_setkeycode; 1862 dev->setkeycode_new = input_default_setkeycode;
1767 1863
1768 dev_set_name(&dev->dev, "input%ld", 1864 dev_set_name(&dev->dev, "input%ld",
1769 (unsigned long) atomic_inc_return(&input_no) - 1); 1865 (unsigned long) atomic_inc_return(&input_no) - 1);