aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPavel Rojtberg <rojtberg@gmail.com>2015-12-09 14:57:01 -0500
committerDmitry Torokhov <dmitry.torokhov@gmail.com>2016-01-04 14:39:38 -0500
commit7fc595f4c02636eadaeeecfe7bbc45b57c173004 (patch)
treeda2f8d034b5a494a6eaf962465bdbe97e72474b7
parent09c8b00ae3e16c8d0fd4beb2ca064502a76c0f17 (diff)
Input: xpad - correctly handle concurrent LED and FF requests
Track the status of the irq_out URB to prevent submission iof new requests while current one is active. Failure to do so results in the "URB submitted while active" warning/stack trace. Store pending brightness and FF effect in the driver structure and replace it with the latest requests until the device is ready to process next request. Alternate serving LED vs FF requests to make sure one does not starve another. See [1] for discussion. Inspired by patch of Sarah Bessmer [2]. [1]: http://www.spinics.net/lists/linux-input/msg40708.html [2]: http://www.spinics.net/lists/linux-input/msg31450.html Signed-off-by: Pavel Rojtberg <rojtberg@gmail.com> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
-rw-r--r--drivers/input/joystick/xpad.c322
1 files changed, 223 insertions, 99 deletions
diff --git a/drivers/input/joystick/xpad.c b/drivers/input/joystick/xpad.c
index 1d51d24c03d1..285f0b753c25 100644
--- a/drivers/input/joystick/xpad.c
+++ b/drivers/input/joystick/xpad.c
@@ -319,6 +319,19 @@ static struct usb_device_id xpad_table[] = {
319 319
320MODULE_DEVICE_TABLE(usb, xpad_table); 320MODULE_DEVICE_TABLE(usb, xpad_table);
321 321
322struct xpad_output_packet {
323 u8 data[XPAD_PKT_LEN];
324 u8 len;
325 bool pending;
326};
327
328#define XPAD_OUT_CMD_IDX 0
329#define XPAD_OUT_FF_IDX 1
330#define XPAD_OUT_LED_IDX (1 + IS_ENABLED(CONFIG_JOYSTICK_XPAD_FF))
331#define XPAD_NUM_OUT_PACKETS (1 + \
332 IS_ENABLED(CONFIG_JOYSTICK_XPAD_FF) + \
333 IS_ENABLED(CONFIG_JOYSTICK_XPAD_LEDS))
334
322struct usb_xpad { 335struct usb_xpad {
323 struct input_dev *dev; /* input device interface */ 336 struct input_dev *dev; /* input device interface */
324 struct input_dev __rcu *x360w_dev; 337 struct input_dev __rcu *x360w_dev;
@@ -333,9 +346,13 @@ struct usb_xpad {
333 dma_addr_t idata_dma; 346 dma_addr_t idata_dma;
334 347
335 struct urb *irq_out; /* urb for interrupt out report */ 348 struct urb *irq_out; /* urb for interrupt out report */
349 bool irq_out_active; /* we must not use an active URB */
336 unsigned char *odata; /* output data */ 350 unsigned char *odata; /* output data */
337 dma_addr_t odata_dma; 351 dma_addr_t odata_dma;
338 struct mutex odata_mutex; 352 spinlock_t odata_lock;
353
354 struct xpad_output_packet out_packets[XPAD_NUM_OUT_PACKETS];
355 int last_out_packet;
339 356
340#if defined(CONFIG_JOYSTICK_XPAD_LEDS) 357#if defined(CONFIG_JOYSTICK_XPAD_LEDS)
341 struct xpad_led *led; 358 struct xpad_led *led;
@@ -711,18 +728,71 @@ exit:
711 __func__, retval); 728 __func__, retval);
712} 729}
713 730
731/* Callers must hold xpad->odata_lock spinlock */
732static bool xpad_prepare_next_out_packet(struct usb_xpad *xpad)
733{
734 struct xpad_output_packet *pkt, *packet = NULL;
735 int i;
736
737 for (i = 0; i < XPAD_NUM_OUT_PACKETS; i++) {
738 if (++xpad->last_out_packet >= XPAD_NUM_OUT_PACKETS)
739 xpad->last_out_packet = 0;
740
741 pkt = &xpad->out_packets[xpad->last_out_packet];
742 if (pkt->pending) {
743 dev_dbg(&xpad->intf->dev,
744 "%s - found pending output packet %d\n",
745 __func__, xpad->last_out_packet);
746 packet = pkt;
747 break;
748 }
749 }
750
751 if (packet) {
752 memcpy(xpad->odata, packet->data, packet->len);
753 xpad->irq_out->transfer_buffer_length = packet->len;
754 return true;
755 }
756
757 return false;
758}
759
760/* Callers must hold xpad->odata_lock spinlock */
761static int xpad_try_sending_next_out_packet(struct usb_xpad *xpad)
762{
763 int error;
764
765 if (!xpad->irq_out_active && xpad_prepare_next_out_packet(xpad)) {
766 error = usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
767 if (error) {
768 dev_err(&xpad->intf->dev,
769 "%s - usb_submit_urb failed with result %d\n",
770 __func__, error);
771 return -EIO;
772 }
773
774 xpad->irq_out_active = true;
775 }
776
777 return 0;
778}
779
714static void xpad_irq_out(struct urb *urb) 780static void xpad_irq_out(struct urb *urb)
715{ 781{
716 struct usb_xpad *xpad = urb->context; 782 struct usb_xpad *xpad = urb->context;
717 struct device *dev = &xpad->intf->dev; 783 struct device *dev = &xpad->intf->dev;
718 int retval, status; 784 int status = urb->status;
785 int error;
786 unsigned long flags;
719 787
720 status = urb->status; 788 spin_lock_irqsave(&xpad->odata_lock, flags);
721 789
722 switch (status) { 790 switch (status) {
723 case 0: 791 case 0:
724 /* success */ 792 /* success */
725 return; 793 xpad->out_packets[xpad->last_out_packet].pending = false;
794 xpad->irq_out_active = xpad_prepare_next_out_packet(xpad);
795 break;
726 796
727 case -ECONNRESET: 797 case -ECONNRESET:
728 case -ENOENT: 798 case -ENOENT:
@@ -730,19 +800,26 @@ static void xpad_irq_out(struct urb *urb)
730 /* this urb is terminated, clean up */ 800 /* this urb is terminated, clean up */
731 dev_dbg(dev, "%s - urb shutting down with status: %d\n", 801 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
732 __func__, status); 802 __func__, status);
733 return; 803 xpad->irq_out_active = false;
804 break;
734 805
735 default: 806 default:
736 dev_dbg(dev, "%s - nonzero urb status received: %d\n", 807 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
737 __func__, status); 808 __func__, status);
738 goto exit; 809 break;
739 } 810 }
740 811
741exit: 812 if (xpad->irq_out_active) {
742 retval = usb_submit_urb(urb, GFP_ATOMIC); 813 error = usb_submit_urb(urb, GFP_ATOMIC);
743 if (retval) 814 if (error) {
744 dev_err(dev, "%s - usb_submit_urb failed with result %d\n", 815 dev_err(dev,
745 __func__, retval); 816 "%s - usb_submit_urb failed with result %d\n",
817 __func__, error);
818 xpad->irq_out_active = false;
819 }
820 }
821
822 spin_unlock_irqrestore(&xpad->odata_lock, flags);
746} 823}
747 824
748static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad) 825static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
@@ -761,7 +838,7 @@ static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
761 goto fail1; 838 goto fail1;
762 } 839 }
763 840
764 mutex_init(&xpad->odata_mutex); 841 spin_lock_init(&xpad->odata_lock);
765 842
766 xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL); 843 xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
767 if (!xpad->irq_out) { 844 if (!xpad->irq_out) {
@@ -803,27 +880,57 @@ static void xpad_deinit_output(struct usb_xpad *xpad)
803 880
804static int xpad_inquiry_pad_presence(struct usb_xpad *xpad) 881static int xpad_inquiry_pad_presence(struct usb_xpad *xpad)
805{ 882{
883 struct xpad_output_packet *packet =
884 &xpad->out_packets[XPAD_OUT_CMD_IDX];
885 unsigned long flags;
886 int retval;
887
888 spin_lock_irqsave(&xpad->odata_lock, flags);
889
890 packet->data[0] = 0x08;
891 packet->data[1] = 0x00;
892 packet->data[2] = 0x0F;
893 packet->data[3] = 0xC0;
894 packet->data[4] = 0x00;
895 packet->data[5] = 0x00;
896 packet->data[6] = 0x00;
897 packet->data[7] = 0x00;
898 packet->data[8] = 0x00;
899 packet->data[9] = 0x00;
900 packet->data[10] = 0x00;
901 packet->data[11] = 0x00;
902 packet->len = 12;
903 packet->pending = true;
904
905 /* Reset the sequence so we send out presence first */
906 xpad->last_out_packet = -1;
907 retval = xpad_try_sending_next_out_packet(xpad);
908
909 spin_unlock_irqrestore(&xpad->odata_lock, flags);
910
911 return retval;
912}
913
914static int xpad_start_xbox_one(struct usb_xpad *xpad)
915{
916 struct xpad_output_packet *packet =
917 &xpad->out_packets[XPAD_OUT_CMD_IDX];
918 unsigned long flags;
806 int retval; 919 int retval;
807 920
808 mutex_lock(&xpad->odata_mutex); 921 spin_lock_irqsave(&xpad->odata_lock, flags);
809 922
810 xpad->odata[0] = 0x08; 923 /* Xbox one controller needs to be initialized. */
811 xpad->odata[1] = 0x00; 924 packet->data[0] = 0x05;
812 xpad->odata[2] = 0x0F; 925 packet->data[1] = 0x20;
813 xpad->odata[3] = 0xC0; 926 packet->len = 2;
814 xpad->odata[4] = 0x00; 927 packet->pending = true;
815 xpad->odata[5] = 0x00;
816 xpad->odata[6] = 0x00;
817 xpad->odata[7] = 0x00;
818 xpad->odata[8] = 0x00;
819 xpad->odata[9] = 0x00;
820 xpad->odata[10] = 0x00;
821 xpad->odata[11] = 0x00;
822 xpad->irq_out->transfer_buffer_length = 12;
823 928
824 retval = usb_submit_urb(xpad->irq_out, GFP_KERNEL); 929 /* Reset the sequence so we send out start packet first */
930 xpad->last_out_packet = -1;
931 retval = xpad_try_sending_next_out_packet(xpad);
825 932
826 mutex_unlock(&xpad->odata_mutex); 933 spin_unlock_irqrestore(&xpad->odata_lock, flags);
827 934
828 return retval; 935 return retval;
829} 936}
@@ -832,8 +939,11 @@ static int xpad_inquiry_pad_presence(struct usb_xpad *xpad)
832static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect) 939static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
833{ 940{
834 struct usb_xpad *xpad = input_get_drvdata(dev); 941 struct usb_xpad *xpad = input_get_drvdata(dev);
942 struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_FF_IDX];
835 __u16 strong; 943 __u16 strong;
836 __u16 weak; 944 __u16 weak;
945 int retval;
946 unsigned long flags;
837 947
838 if (effect->type != FF_RUMBLE) 948 if (effect->type != FF_RUMBLE)
839 return 0; 949 return 0;
@@ -841,69 +951,80 @@ static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect
841 strong = effect->u.rumble.strong_magnitude; 951 strong = effect->u.rumble.strong_magnitude;
842 weak = effect->u.rumble.weak_magnitude; 952 weak = effect->u.rumble.weak_magnitude;
843 953
954 spin_lock_irqsave(&xpad->odata_lock, flags);
955
844 switch (xpad->xtype) { 956 switch (xpad->xtype) {
845 case XTYPE_XBOX: 957 case XTYPE_XBOX:
846 xpad->odata[0] = 0x00; 958 packet->data[0] = 0x00;
847 xpad->odata[1] = 0x06; 959 packet->data[1] = 0x06;
848 xpad->odata[2] = 0x00; 960 packet->data[2] = 0x00;
849 xpad->odata[3] = strong / 256; /* left actuator */ 961 packet->data[3] = strong / 256; /* left actuator */
850 xpad->odata[4] = 0x00; 962 packet->data[4] = 0x00;
851 xpad->odata[5] = weak / 256; /* right actuator */ 963 packet->data[5] = weak / 256; /* right actuator */
852 xpad->irq_out->transfer_buffer_length = 6; 964 packet->len = 6;
965 packet->pending = true;
853 break; 966 break;
854 967
855 case XTYPE_XBOX360: 968 case XTYPE_XBOX360:
856 xpad->odata[0] = 0x00; 969 packet->data[0] = 0x00;
857 xpad->odata[1] = 0x08; 970 packet->data[1] = 0x08;
858 xpad->odata[2] = 0x00; 971 packet->data[2] = 0x00;
859 xpad->odata[3] = strong / 256; /* left actuator? */ 972 packet->data[3] = strong / 256; /* left actuator? */
860 xpad->odata[4] = weak / 256; /* right actuator? */ 973 packet->data[4] = weak / 256; /* right actuator? */
861 xpad->odata[5] = 0x00; 974 packet->data[5] = 0x00;
862 xpad->odata[6] = 0x00; 975 packet->data[6] = 0x00;
863 xpad->odata[7] = 0x00; 976 packet->data[7] = 0x00;
864 xpad->irq_out->transfer_buffer_length = 8; 977 packet->len = 8;
978 packet->pending = true;
865 break; 979 break;
866 980
867 case XTYPE_XBOX360W: 981 case XTYPE_XBOX360W:
868 xpad->odata[0] = 0x00; 982 packet->data[0] = 0x00;
869 xpad->odata[1] = 0x01; 983 packet->data[1] = 0x01;
870 xpad->odata[2] = 0x0F; 984 packet->data[2] = 0x0F;
871 xpad->odata[3] = 0xC0; 985 packet->data[3] = 0xC0;
872 xpad->odata[4] = 0x00; 986 packet->data[4] = 0x00;
873 xpad->odata[5] = strong / 256; 987 packet->data[5] = strong / 256;
874 xpad->odata[6] = weak / 256; 988 packet->data[6] = weak / 256;
875 xpad->odata[7] = 0x00; 989 packet->data[7] = 0x00;
876 xpad->odata[8] = 0x00; 990 packet->data[8] = 0x00;
877 xpad->odata[9] = 0x00; 991 packet->data[9] = 0x00;
878 xpad->odata[10] = 0x00; 992 packet->data[10] = 0x00;
879 xpad->odata[11] = 0x00; 993 packet->data[11] = 0x00;
880 xpad->irq_out->transfer_buffer_length = 12; 994 packet->len = 12;
995 packet->pending = true;
881 break; 996 break;
882 997
883 case XTYPE_XBOXONE: 998 case XTYPE_XBOXONE:
884 xpad->odata[0] = 0x09; /* activate rumble */ 999 packet->data[0] = 0x09; /* activate rumble */
885 xpad->odata[1] = 0x08; 1000 packet->data[1] = 0x08;
886 xpad->odata[2] = 0x00; 1001 packet->data[2] = 0x00;
887 xpad->odata[3] = 0x08; /* continuous effect */ 1002 packet->data[3] = 0x08; /* continuous effect */
888 xpad->odata[4] = 0x00; /* simple rumble mode */ 1003 packet->data[4] = 0x00; /* simple rumble mode */
889 xpad->odata[5] = 0x03; /* L and R actuator only */ 1004 packet->data[5] = 0x03; /* L and R actuator only */
890 xpad->odata[6] = 0x00; /* TODO: LT actuator */ 1005 packet->data[6] = 0x00; /* TODO: LT actuator */
891 xpad->odata[7] = 0x00; /* TODO: RT actuator */ 1006 packet->data[7] = 0x00; /* TODO: RT actuator */
892 xpad->odata[8] = strong / 256; /* left actuator */ 1007 packet->data[8] = strong / 256; /* left actuator */
893 xpad->odata[9] = weak / 256; /* right actuator */ 1008 packet->data[9] = weak / 256; /* right actuator */
894 xpad->odata[10] = 0x80; /* length of pulse */ 1009 packet->data[10] = 0x80; /* length of pulse */
895 xpad->odata[11] = 0x00; /* stop period of pulse */ 1010 packet->data[11] = 0x00; /* stop period of pulse */
896 xpad->irq_out->transfer_buffer_length = 12; 1011 packet->len = 12;
1012 packet->pending = true;
897 break; 1013 break;
898 1014
899 default: 1015 default:
900 dev_dbg(&xpad->dev->dev, 1016 dev_dbg(&xpad->dev->dev,
901 "%s - rumble command sent to unsupported xpad type: %d\n", 1017 "%s - rumble command sent to unsupported xpad type: %d\n",
902 __func__, xpad->xtype); 1018 __func__, xpad->xtype);
903 return -EINVAL; 1019 retval = -EINVAL;
1020 goto out;
904 } 1021 }
905 1022
906 return usb_submit_urb(xpad->irq_out, GFP_ATOMIC); 1023 retval = xpad_try_sending_next_out_packet(xpad);
1024
1025out:
1026 spin_unlock_irqrestore(&xpad->odata_lock, flags);
1027 return retval;
907} 1028}
908 1029
909static int xpad_init_ff(struct usb_xpad *xpad) 1030static int xpad_init_ff(struct usb_xpad *xpad)
@@ -954,36 +1075,44 @@ struct xpad_led {
954 */ 1075 */
955static void xpad_send_led_command(struct usb_xpad *xpad, int command) 1076static void xpad_send_led_command(struct usb_xpad *xpad, int command)
956{ 1077{
1078 struct xpad_output_packet *packet =
1079 &xpad->out_packets[XPAD_OUT_LED_IDX];
1080 unsigned long flags;
1081
957 command %= 16; 1082 command %= 16;
958 1083
959 mutex_lock(&xpad->odata_mutex); 1084 spin_lock_irqsave(&xpad->odata_lock, flags);
960 1085
961 switch (xpad->xtype) { 1086 switch (xpad->xtype) {
962 case XTYPE_XBOX360: 1087 case XTYPE_XBOX360:
963 xpad->odata[0] = 0x01; 1088 packet->data[0] = 0x01;
964 xpad->odata[1] = 0x03; 1089 packet->data[1] = 0x03;
965 xpad->odata[2] = command; 1090 packet->data[2] = command;
966 xpad->irq_out->transfer_buffer_length = 3; 1091 packet->len = 3;
1092 packet->pending = true;
967 break; 1093 break;
1094
968 case XTYPE_XBOX360W: 1095 case XTYPE_XBOX360W:
969 xpad->odata[0] = 0x00; 1096 packet->data[0] = 0x00;
970 xpad->odata[1] = 0x00; 1097 packet->data[1] = 0x00;
971 xpad->odata[2] = 0x08; 1098 packet->data[2] = 0x08;
972 xpad->odata[3] = 0x40 + command; 1099 packet->data[3] = 0x40 + command;
973 xpad->odata[4] = 0x00; 1100 packet->data[4] = 0x00;
974 xpad->odata[5] = 0x00; 1101 packet->data[5] = 0x00;
975 xpad->odata[6] = 0x00; 1102 packet->data[6] = 0x00;
976 xpad->odata[7] = 0x00; 1103 packet->data[7] = 0x00;
977 xpad->odata[8] = 0x00; 1104 packet->data[8] = 0x00;
978 xpad->odata[9] = 0x00; 1105 packet->data[9] = 0x00;
979 xpad->odata[10] = 0x00; 1106 packet->data[10] = 0x00;
980 xpad->odata[11] = 0x00; 1107 packet->data[11] = 0x00;
981 xpad->irq_out->transfer_buffer_length = 12; 1108 packet->len = 12;
1109 packet->pending = true;
982 break; 1110 break;
983 } 1111 }
984 1112
985 usb_submit_urb(xpad->irq_out, GFP_KERNEL); 1113 xpad_try_sending_next_out_packet(xpad);
986 mutex_unlock(&xpad->odata_mutex); 1114
1115 spin_unlock_irqrestore(&xpad->odata_lock, flags);
987} 1116}
988 1117
989/* 1118/*
@@ -1074,13 +1203,8 @@ static int xpad_open(struct input_dev *dev)
1074 if (usb_submit_urb(xpad->irq_in, GFP_KERNEL)) 1203 if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
1075 return -EIO; 1204 return -EIO;
1076 1205
1077 if (xpad->xtype == XTYPE_XBOXONE) { 1206 if (xpad->xtype == XTYPE_XBOXONE)
1078 /* Xbox one controller needs to be initialized. */ 1207 return xpad_start_xbox_one(xpad);
1079 xpad->odata[0] = 0x05;
1080 xpad->odata[1] = 0x20;
1081 xpad->irq_out->transfer_buffer_length = 2;
1082 return usb_submit_urb(xpad->irq_out, GFP_KERNEL);
1083 }
1084 1208
1085 return 0; 1209 return 0;
1086} 1210}