]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - drivers/input/joystick/xpad.c
Input: xpad - add support for leds on xbox 360 pad
[karo-tx-linux.git] / drivers / input / joystick / xpad.c
index f21f84a2dfffdf65fad3af3f555d01ef559a8a0a..28080395899c21aacbc08accda1631c98637a286 100644 (file)
@@ -191,7 +191,18 @@ struct usb_xpad {
        unsigned char *idata;           /* input data */
        dma_addr_t idata_dma;
 
-       char phys[65];                  /* physical device path */
+#if defined(CONFIG_JOYSTICK_XPAD_FF) || defined(CONFIG_JOYSTICK_XPAD_LEDS)
+       struct urb *irq_out;            /* urb for interrupt out report */
+       unsigned char *odata;           /* output data */
+       dma_addr_t odata_dma;
+       struct mutex odata_mutex;
+#endif
+
+#if defined(CONFIG_JOYSTICK_XPAD_LEDS)
+       struct xpad_led *led;
+#endif
+
+       char phys[64];                  /* physical device path */
 
        int dpad_mapping;               /* map d-pad to buttons or to axes */
        int xtype;                      /* type of xbox device */
@@ -230,8 +241,8 @@ static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *d
        } else /* xpad->dpad_mapping == MAP_DPAD_TO_BUTTONS */ {
                input_report_key(dev, BTN_LEFT,  data[2] & 0x04);
                input_report_key(dev, BTN_RIGHT, data[2] & 0x08);
-               input_report_key(dev, BTN_0,     data[2] & 0x01); // up
-               input_report_key(dev, BTN_1,     data[2] & 0x02); // down
+               input_report_key(dev, BTN_0,     data[2] & 0x01); /* up */
+               input_report_key(dev, BTN_1,     data[2] & 0x02); /* down */
        }
 
        /* start/back buttons and stick press left/right */
@@ -269,40 +280,40 @@ static void xpad360_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char
 
        /* digital pad */
        if (xpad->dpad_mapping == MAP_DPAD_TO_AXES) {
-               input_report_abs(dev, ABS_HAT0X, !!(data[2] & 0x01) - !!((data[2] & 0x08) >> 3));
-               input_report_abs(dev, ABS_HAT0Y, !!((data[2] & 0x02) >> 1) - !!((data[2] & 0x04) >> 2));
-       } else if ( xpad->dpad_mapping == MAP_DPAD_TO_BUTTONS ) {
+               input_report_abs(dev, ABS_HAT0X, !!(data[2] & 0x08) - !!(data[2] & 0x04));
+               input_report_abs(dev, ABS_HAT0Y, !!(data[2] & 0x02) - !!(data[2] & 0x01));
+       } else if (xpad->dpad_mapping == MAP_DPAD_TO_BUTTONS) {
                /* dpad as buttons (right, left, down, up) */
-               input_report_key(dev, BTN_RIGHT, (data[2] & 0x01));
-               input_report_key(dev, BTN_LEFT, (data[2] & 0x08) >> 3);
-               input_report_key(dev, BTN_0, (data[2] & 0x02) >> 1);
-               input_report_key(dev, BTN_1, (data[2] & 0x04) >> 2);
+               input_report_key(dev, BTN_LEFT, data[2] & 0x04);
+               input_report_key(dev, BTN_RIGHT, data[2] & 0x08);
+               input_report_key(dev, BTN_0, data[2] & 0x01);   /* up */
+               input_report_key(dev, BTN_1, data[2] & 0x02);   /* down */
        }
 
        /* start/back buttons */
-       input_report_key(dev, BTN_START,  (data[2] & 0x10) >> 4);
-       input_report_key(dev, BTN_BACK,   (data[2] & 0x20) >> 5);
+       input_report_key(dev, BTN_START,  data[2] & 0x10);
+       input_report_key(dev, BTN_BACK,   data[2] & 0x20);
 
        /* stick press left/right */
        input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
        input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
 
        /* buttons A,B,X,Y,TL,TR and MODE */
-       input_report_key(dev, BTN_A, (data[3] & 0x10) >> 4);
-       input_report_key(dev, BTN_B, (data[3] & 0x20) >> 5);
-       input_report_key(dev, BTN_X, (data[3] & 0x40) >> 6);
-       input_report_key(dev, BTN_Y, (data[3] & 0x80) >> 7);
-       input_report_key(dev, BTN_TL, data[3] & 0x01 );
-       input_report_key(dev, BTN_TR, (data[3] & 0x02) >> 1);
-       input_report_key(dev, BTN_MODE, (data[3] & 0x04) >> 2);
+       input_report_key(dev, BTN_A,    data[3] & 0x10);
+       input_report_key(dev, BTN_B,    data[3] & 0x20);
+       input_report_key(dev, BTN_X,    data[3] & 0x40);
+       input_report_key(dev, BTN_Y,    data[3] & 0x80);
+       input_report_key(dev, BTN_TL,   data[3] & 0x01);
+       input_report_key(dev, BTN_TR,   data[3] & 0x02);
+       input_report_key(dev, BTN_MODE, data[3] & 0x04);
 
        /* left stick */
        input_report_abs(dev, ABS_X, (__s16) (((__s16)data[7] << 8) | (__s16)data[6]));
-       input_report_abs(dev, ABS_Y, ~(__s16) (((__s16)data[9] << 8) | (__s16)data[8]));
+       input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[9] << 8) | (__s16)data[8]));
 
        /* right stick */
-       input_report_abs(dev, ABS_RY, ~(__s16) (((__s16)data[13] << 8) | (__s16)data[12]));
        input_report_abs(dev, ABS_RX, (__s16) (((__s16)data[11] << 8) | (__s16)data[10]));
+       input_report_abs(dev, ABS_RY, (__s16) (((__s16)data[13] << 8) | (__s16)data[12]));
 
        /* triggers left/right */
        input_report_abs(dev, ABS_Z, data[4]);
@@ -343,7 +354,205 @@ exit:
                     __FUNCTION__, retval);
 }
 
-static int xpad_open (struct input_dev *dev)
+#if defined(CONFIG_JOYSTICK_XPAD_FF) || defined(CONFIG_JOYSTICK_XPAD_LEDS)
+static void xpad_irq_out(struct urb *urb)
+{
+       int retval;
+
+       switch (urb->status) {
+               case 0:
+               /* success */
+               break;
+               case -ECONNRESET:
+               case -ENOENT:
+               case -ESHUTDOWN:
+                       /* this urb is terminated, clean up */
+                       dbg("%s - urb shutting down with status: %d",  __FUNCTION__, urb->status);
+                       return;
+               default:
+                       dbg("%s - nonzero urb status received: %d",  __FUNCTION__, urb->status);
+                       goto exit;
+       }
+
+exit:
+       retval = usb_submit_urb(urb, GFP_ATOMIC);
+       if (retval)
+               err("%s - usb_submit_urb failed with result %d",
+                  __FUNCTION__, retval);
+}
+
+static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
+{
+       struct usb_endpoint_descriptor *ep_irq_out;
+       int error = -ENOMEM;
+
+       if (xpad->xtype != XTYPE_XBOX360)
+               return 0;
+
+       xpad->odata = usb_buffer_alloc(xpad->udev, XPAD_PKT_LEN,
+                                      GFP_ATOMIC, &xpad->odata_dma );
+       if (!xpad->odata)
+               goto fail1;
+
+       mutex_init(&xpad->odata_mutex);
+
+       xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
+       if (!xpad->irq_out)
+               goto fail2;
+
+       ep_irq_out = &intf->cur_altsetting->endpoint[1].desc;
+       usb_fill_int_urb(xpad->irq_out, xpad->udev,
+                        usb_sndintpipe(xpad->udev, ep_irq_out->bEndpointAddress),
+                        xpad->odata, XPAD_PKT_LEN,
+                        xpad_irq_out, xpad, ep_irq_out->bInterval);
+       xpad->irq_out->transfer_dma = xpad->odata_dma;
+       xpad->irq_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
+
+       return 0;
+
+ fail2:        usb_buffer_free(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma);
+ fail1:        return error;
+}
+
+static void xpad_stop_output(struct usb_xpad *xpad)
+{
+       if (xpad->xtype == XTYPE_XBOX360)
+               usb_kill_urb(xpad->irq_out);
+}
+
+static void xpad_deinit_output(struct usb_xpad *xpad)
+{
+       if (xpad->xtype == XTYPE_XBOX360) {
+               usb_free_urb(xpad->irq_out);
+               usb_buffer_free(xpad->udev, XPAD_PKT_LEN,
+                               xpad->odata, xpad->odata_dma);
+       }
+}
+#else
+static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad) { return 0; }
+static void xpad_deinit_output(struct usb_xpad *xpad) {}
+static void xpad_stop_output(struct usb_xpad *xpad) {}
+#endif
+
+#ifdef CONFIG_JOYSTICK_XPAD_FF
+static int xpad_play_effect(struct input_dev *dev, void *data,
+                           struct ff_effect *effect)
+{
+       struct usb_xpad *xpad = input_get_drvdata(dev);
+
+       if (effect->type == FF_RUMBLE) {
+               __u16 strong = effect->u.rumble.strong_magnitude;
+               __u16 weak = effect->u.rumble.weak_magnitude;
+               xpad->odata[0] = 0x00;
+               xpad->odata[1] = 0x08;
+               xpad->odata[2] = 0x00;
+               xpad->odata[3] = strong / 256;
+               xpad->odata[4] = weak / 256;
+               xpad->odata[5] = 0x00;
+               xpad->odata[6] = 0x00;
+               xpad->odata[7] = 0x00;
+               usb_submit_urb(xpad->irq_out, GFP_KERNEL);
+       }
+
+       return 0;
+}
+
+static int xpad_init_ff(struct usb_xpad *xpad)
+{
+       input_set_capability(xpad->dev, EV_FF, FF_RUMBLE);
+
+       return input_ff_create_memless(xpad->dev, NULL, xpad_play_effect);
+}
+
+#else
+static int xpad_init_ff(struct usb_xpad *xpad) { return 0; }
+#endif
+
+#if defined(CONFIG_JOYSTICK_XPAD_LEDS)
+#include <linux/leds.h>
+
+struct xpad_led {
+       char name[16];
+       struct led_classdev led_cdev;
+       struct usb_xpad *xpad;
+};
+
+static void xpad_send_led_command(struct usb_xpad *xpad, int command)
+{
+       if (command >= 0 && command < 14) {
+               mutex_lock(&xpad->odata_mutex);
+               xpad->odata[0] = 0x01;
+               xpad->odata[1] = 0x03;
+               xpad->odata[2] = command;
+               usb_submit_urb(xpad->irq_out, GFP_KERNEL);
+               mutex_unlock(&xpad->odata_mutex);
+       }
+}
+
+static void xpad_led_set(struct led_classdev *led_cdev,
+                        enum led_brightness value)
+{
+       struct xpad_led *xpad_led = container_of(led_cdev,
+                                                struct xpad_led, led_cdev);
+
+       xpad_send_led_command(xpad_led->xpad, value);
+}
+
+static int xpad_led_probe(struct usb_xpad *xpad)
+{
+       static atomic_t led_seq = ATOMIC_INIT(0);
+       long led_no;
+       struct xpad_led *led;
+       struct led_classdev *led_cdev;
+       int error;
+
+       if (xpad->xtype != XTYPE_XBOX360)
+               return 0;
+
+       xpad->led = led = kzalloc(sizeof(struct xpad_led), GFP_KERNEL);
+       if (!led)
+               return -ENOMEM;
+
+       led_no = (long)atomic_inc_return(&led_seq) - 1;
+
+       snprintf(led->name, sizeof(led->name), "xpad%ld", led_no);
+       led->xpad = xpad;
+
+       led_cdev = &led->led_cdev;
+       led_cdev->name = led->name;
+       led_cdev->brightness_set = xpad_led_set;
+
+       error = led_classdev_register(&xpad->udev->dev, led_cdev);
+       if (error) {
+               kfree(led);
+               xpad->led = NULL;
+               return error;
+       }
+
+       /*
+        * Light up the segment corresponding to controller number
+        */
+       xpad_send_led_command(xpad, (led_no % 4) + 2);
+
+       return 0;
+}
+
+static void xpad_led_disconnect(struct usb_xpad *xpad)
+{
+       struct xpad_led *xpad_led = xpad->led;
+
+       if (xpad_led) {
+               led_classdev_unregister(&xpad_led->led_cdev);
+               kfree(xpad_led->name);
+       }
+}
+#else
+static int xpad_led_probe(struct usb_xpad *xpad) { return 0; }
+static void xpad_led_disconnect(struct usb_xpad *xpad) { }
+#endif
+
+
+static int xpad_open(struct input_dev *dev)
 {
        struct usb_xpad *xpad = input_get_drvdata(dev);
 
@@ -354,11 +563,12 @@ static int xpad_open (struct input_dev *dev)
        return 0;
 }
 
-static void xpad_close (struct input_dev *dev)
+static void xpad_close(struct input_dev *dev)
 {
        struct usb_xpad *xpad = input_get_drvdata(dev);
 
        usb_kill_urb(xpad->irq_in);
+       xpad_stop_output(xpad);
 }
 
 static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
@@ -450,6 +660,18 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
                for (i = 0; xpad_abs_pad[i] >= 0; i++)
                    xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
 
+       error = xpad_init_output(intf, xpad);
+       if (error)
+               goto fail2;
+
+       error = xpad_init_ff(xpad);
+       if (error)
+               goto fail3;
+
+       error = xpad_led_probe(xpad);
+       if (error)
+               goto fail3;
+
        ep_irq_in = &intf->cur_altsetting->endpoint[0].desc;
        usb_fill_int_urb(xpad->irq_in, udev,
                         usb_rcvintpipe(udev, ep_irq_in->bEndpointAddress),
@@ -460,12 +682,13 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
 
        error = input_register_device(xpad->dev);
        if (error)
-               goto fail3;
+               goto fail4;
 
        usb_set_intfdata(intf, xpad);
        return 0;
 
- fail3:        usb_free_urb(xpad->irq_in);
+ fail4:        usb_free_urb(xpad->irq_in);
+ fail3:        xpad_deinit_output(xpad);
  fail2:        usb_buffer_free(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
  fail1:        input_free_device(input_dev);
        kfree(xpad);
@@ -479,10 +702,11 @@ static void xpad_disconnect(struct usb_interface *intf)
 
        usb_set_intfdata(intf, NULL);
        if (xpad) {
-               usb_kill_urb(xpad->irq_in);
+               xpad_led_disconnect(xpad);
                input_unregister_device(xpad->dev);
+               xpad_deinit_output(xpad);
                usb_free_urb(xpad->irq_in);
-               usb_buffer_free(interface_to_usbdev(intf), XPAD_PKT_LEN,
+               usb_buffer_free(xpad->udev, XPAD_PKT_LEN,
                                xpad->idata, xpad->idata_dma);
                kfree(xpad);
        }