Skip to contentSkip to conversation

Input: psxpad-spi - Add PlayStation 1/2 joypads via SPI interface Driver

PATCH Superseded Tomohiro Yoshidomi 3 participants 4 messages Apr 30, 2017

Subsystems (2)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 Input: psxpad-spi - Add PlayStation 1/2 joypads via SPI interface Driver

PlayStation 1/2 joypads can be connected directly to the SPI interface.

---
 drivers/input/joystick/Kconfig      |  22 ++
 drivers/input/joystick/Makefile     |   1 +
 drivers/input/joystick/psxpad-spi.c | 433 ++++++++++++++++++++++++++++++++++++
 3 files changed, 456 insertions(+)
 create mode 100644 drivers/input/joystick/psxpad-spi.c
drivers/input/joystick/Kconfig +22 -0
index 4215b538..cbcb9a6e 100644
@@ -330,4 +330,26 @@ config JOYSTICK_MAPLE
 	  To compile this as a module choose M here: the module will be called
 	  maplecontrol.
 
+config JOYSTICK_PSXPAD_SPI
+	tristate "PlayStation 1/2 joypads via SPI interface"
+	depends on SPI
+	select INPUT_POLLDEV
+	help
+	  Say Y here if you wish to connect PlayStation 1/2 joypads
+	  via SPI interface.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called psxpad-spi.
+
+config JOYSTICK_PSXPAD_SPI_FF
+	bool "PlayStation 1/2 joypads force feedback (rumble) support"
+	depends on JOYSTICK_PSXPAD_SPI
+	select INPUT_FF_MEMLESS
+	help
+	  Say Y here if you want to take advantage of
+	  PlayStation 1/2 joypads rumble features.
+
+	  To drive rumble motor,
+	  a dedicated power supply is required.
+
 endif
drivers/input/joystick/Makefile +1 -0
index 92dc0de9..496fd56b 100644
@@ -21,6 +21,7 @@ obj-$(CONFIG_JOYSTICK_INTERACT)		+= interact.o
 obj-$(CONFIG_JOYSTICK_JOYDUMP)		+= joydump.o
 obj-$(CONFIG_JOYSTICK_MAGELLAN)		+= magellan.o
 obj-$(CONFIG_JOYSTICK_MAPLE)		+= maplecontrol.o
+obj-$(CONFIG_JOYSTICK_PSXPAD_SPI)	+= psxpad-spi.o
 obj-$(CONFIG_JOYSTICK_SIDEWINDER)	+= sidewinder.o
 obj-$(CONFIG_JOYSTICK_SPACEBALL)	+= spaceball.o
 obj-$(CONFIG_JOYSTICK_SPACEORB)		+= spaceorb.o
drivers/input/joystick/psxpad-spi.c new file +433 -0

433 lines not on this page · raw message

Signature · 1 line
2.11.0
#1 lore raw

Mr.Torokhov

I fixed source, and sent PATCH v6 to you.

As Mr.Nocera said, I fixed souces does not use 'PSX' in sentences. Also, I wrote that a separate power supply is required for the motor.

2017-04-30 3:16 GMT+09:00 Dmitry Torokhov <[email protected]>:

Quoted 26 lines

Hi,

Quoted 3 lines · Tomohiro Yoshidomi

PSX pads can be connected directly to the SPI bus.

Thank you very much for making requested changes and your patience. I think we just need a few finishing touches and we can commit this driver.

Quoted 15 lines
---
 drivers/input/joystick/Kconfig      |  17 ++
 drivers/input/joystick/Makefile     |   1 +
 drivers/input/joystick/psxpad-spi.c | 387 ++++++++++++++++++++++++++++++++++++
 3 files changed, 405 insertions(+)
 create mode 100644 drivers/input/joystick/psxpad-spi.c
drivers/input/joystick/Kconfig +0 -0
index 4215b538..bfbda643 100644
@@ -330,4 +330,21 @@ config JOYSTICK_MAPLE
        To compile this as a module choose M here: the module will be called
        maplecontrol.
 
Quoted 4 lines

drivers/input/joystick/Kconfig config JOYSTICK_MAPLE · line 334

Quoted 2 lines
drivers/input/joystick/Kconfig +2 -0
+config JOYSTICK_PSXPAD_SPI
+     tristate "PSX (Play Station 1/2) pad with SPI Bus Driver"

Let's call this "PSX (Play Station 1/2) gamepad on SPI bus"

Fixed.
"PlayStation 1/2 joypads via SPI interface"

Quoted 6 lines

drivers/input/joystick/Kconfig config JOYSTICK_MAPLE

Quoted 4 lines
drivers/input/joystick/Kconfig +4 -0
+     depends on SPI
+     select INPUT_POLLDEV
+     help
+       Say Y here if you connect PSX (PS1/2) pad with SPI Interface.

"Say Y here if you wish to connect PSX (PS1/2) pad via SPI interface."

Fixed.
"Say Y here if you wish to connect PlayStation 1/2 joypads via SPI interface."

Quoted 8 lines

drivers/input/joystick/Kconfig config JOYSTICK_MAPLE

Quoted 6 lines
drivers/input/joystick/Kconfig +6 -0
+
+       To compile this driver as a module, choose M here: the
+       module will be called psxpad-spi.
+
+config JOYSTICK_PSXPAD_SPI_FF
+     bool "PSX pad with SPI Bus rumble support"

Let's call it "PSX gamepad force feedback (rumble) support"

Fixed.
"PlayStation 1/2 joypads force feedback (rumble) support"

Quoted 72 lines
Quoted 72 lines
drivers/input/joystick/Kconfig +5 -0
+     depends on JOYSTICK_PSXPAD_SPI
+     select INPUT_FF_MEMLESS
+     help
+       Say Y here if you want to take advantage of PSX pad rumble features.
+
 endif
drivers/input/joystick/Makefile +1 -0
index 92dc0de9..496fd56b 100644
@@ -21,6 +21,7 @@ obj-$(CONFIG_JOYSTICK_INTERACT)             += interact.o
 obj-$(CONFIG_JOYSTICK_JOYDUMP)               += joydump.o
 obj-$(CONFIG_JOYSTICK_MAGELLAN)              += magellan.o
 obj-$(CONFIG_JOYSTICK_MAPLE)         += maplecontrol.o
+obj-$(CONFIG_JOYSTICK_PSXPAD_SPI)    += psxpad-spi.o
 obj-$(CONFIG_JOYSTICK_SIDEWINDER)    += sidewinder.o
 obj-$(CONFIG_JOYSTICK_SPACEBALL)     += spaceball.o
 obj-$(CONFIG_JOYSTICK_SPACEORB)              += spaceorb.o
drivers/input/joystick/psxpad-spi.c new file +48 -0
new file mode 100644
index 00000000..9684839c
@@ -0,0 +1,387 @@
+/*
+ * PSX (Play Station 1/2) pad (SPI Interface)
+ *
+ * Copyright (C) 2017 Tomohiro Yoshidomi <[email protected]>
+ * Licensed under the GPL-2 or later.
+ *
+ * PSX pad plug (not socket)
+ *  123 456 789
+ * (...|...|...)
+ *
+ * 1: DAT -> MISO (pullup with 1k owm to 3.3V)
+ * 2: CMD -> MOSI
+ * 3: 9V (for motor, if not use N.C.)
+ * 4: GND
+ * 5: 3.3V
+ * 6: Attention -> CS(SS)
+ * 7: SCK -> SCK
+ * 8: N.C.
+ * 9: ACK -> N.C.
+ */
+
+#include <linux/kernel.h>
+#include <linux/device.h>
+#include <linux/input.h>
+#include <linux/input-polldev.h>
+#include <linux/module.h>
+#include <linux/spi/spi.h>
+#include <linux/types.h>
+#include <linux/pm.h>
+#include <linux/pm_runtime.h>
+
+#define REVERSE_BIT(x) ((((x) & 0x80) >> 7) | (((x) & 0x40) >> 5) | (((x) & 0x20) >> 3) | (((x) & 0x10) >> 1) | (((x) & 0x08) << 1) | (((x) & 0x04) << 3) | (((x) & 0x02) << 5) | (((x) & 0x01) << 7))
+
+static const u8 PSX_CMD_POLL[]               = {0x01, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+static const u8 PSX_CMD_ENTER_CFG[]  = {0x01, 0x43, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
+static const u8 PSX_CMD_EXIT_CFG[]   = {0x01, 0x43, 0x00, 0x00, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A};
+static const u8 PSX_CMD_ENABLE_MOTOR[]       = {0x01, 0x4D, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
+
+struct psxpad {
+     struct spi_device *spi;
+     struct input_polled_dev *pdev;
+     struct input_dev *idev;
+     char phys[0x20];
+     bool motor1enable;
+     bool motor2enable;
+     u8 motor1level;
+     u8 motor2level;
+     u8 pollcmd[sizeof(PSX_CMD_POLL)];
Quoted 8 lines

drivers/input/joystick/psxpad-spi.c

Quoted 6 lines
drivers/input/joystick/psxpad-spi.c new file +6 -0
+     u8 enablemotor[sizeof(PSX_CMD_ENABLE_MOTOR)];
+     u8 sendbuf[0x20] ____cacheline_aligned;
+     u8 response[sizeof(PSX_CMD_POLL)] ____cacheline_aligned;
+};
+
+static int psxpad_command(struct psxpad *pad, const u8 sendcmd[], u8 response[], const u8 sendcmdlen)

I wonder if we really need to pass "response" as a parameter here.

Fixed.

Quoted 10 lines

drivers/input/joystick/psxpad-spi.c

Quoted 9 lines
drivers/input/joystick/psxpad-spi.c new file +9 -0
+{
+     struct spi_transfer xfers = {
+             .tx_buf         = pad->sendbuf,
+             .rx_buf         = response,
+             .len            = sendcmdlen,
+     };
+     struct spi_message msg;
+     int err;
+     u8 loc;
Quoted 6 lines

Let's call it "int i" - the standard loop variable.

drivers/input/joystick/psxpad-spi.c line 65

Quoted 2 lines
drivers/input/joystick/psxpad-spi.c new file +2 -0
+
+     spi_message_init(&msg);

I do not think we need "msg" anymore.

Fixed.

Quoted 9 lines

drivers/input/joystick/psxpad-spi.c line 68

Quoted 3 lines
drivers/input/joystick/psxpad-spi.c new file +3 -0
+
+     for (loc = 0; loc < sendcmdlen; loc++)
+             pad->sendbuf[loc] = REVERSE_BIT(sendcmd[loc]);

I think I already asked this, but I do not recall the answer: can we supply commands with bits pre-reversed (i.e. specify PSX_CMD_POLL as 0x80, 0x42, 0x00, ... and PSX_CMD_ENTER_CFG as 0x80, 0xc2, 0x00, 0x80, 0x00, ... and so on, and reverse arguments when you supply them? See more in psxpad_setenablemotor().

Fixed.
I use MSBFIRST (not LSBFIRST) values.

Quoted 9 lines

drivers/input/joystick/psxpad-spi.c

Quoted 5 lines
drivers/input/joystick/psxpad-spi.c new file +5 -0
+
+     err = spi_sync_transfer(pad->spi, &xfers, 1);
+     if (err) {
+             dev_err(&pad->idev->dev, "psxpad-spi: failed SPI xfers!!\n");
+             goto err_end;

Just return directly here:

                return err;

Fixed.

Quoted 7 lines

drivers/input/joystick/psxpad-spi.c

Quoted 4 lines
drivers/input/joystick/psxpad-spi.c new file +4 -0
+     }
+
+     for (loc = 0; loc < sendcmdlen; loc++)
+             response[loc] = REVERSE_BIT(response[loc]);

We only use returned data when we poll the device. Maybe we should do this bit-reversing outsize of psxpad_command()?

Fixed.

Quoted 5 lines
Quoted 5 lines
drivers/input/joystick/psxpad-spi.c new file +5 -0
+
+     return 0;
+
+ err_end:
+
Quoted 8 lines

drivers/input/joystick/psxpad-spi.c

Quoted 5 lines
drivers/input/joystick/psxpad-spi.c new file +5 -0
+     return err;
+}
+
+#ifdef CONFIG_JOYSTICK_PSXPAD_SPI_FF
+static void psxpad_setenablemotor(struct psxpad *pad, const bool motor1enable, const bool motor2enable)

psxpad_control_motor() and there is really no need for "const" on scalar arguments.

Fixed.
psxpad_set_motor_level() was fixed too.

Quoted 24 lines

drivers/input/joystick/psxpad-spi.c

Quoted 8 lines
drivers/input/joystick/psxpad-spi.c new file +8 -0
+{
+     int err;
+
+     pad->motor1enable = motor1enable;
+     pad->motor2enable = motor2enable;
+
+     pad->enablemotor[3] = pad->motor1enable ? 0x00 : 0xFF;
+     pad->enablemotor[4] = pad->motor2enable ? 0x01 : 0xFF;

Assuming that the commands are "pre-revrsed":

        memcpy(pad->sendbuf, PSX_CMD_ENTER_CFG, sizeof(PSX_CMD_ENTER_CFG);
        err = psxpad_command(pad, sizeof(PSX_CMD_ENTER_CFG));
        if (err) {
                dev_err(&pad->spi->dev,
                        "%s: failed to enter config mode: %d\n",
                        __func__, err);
                return err;
        }

        memcpy(pad->sendbuf, PSX_CMD_ENABLE_MOTOR, sizeof(PSX_CMD_ENABLE_MOTOR);
        pad->sendbuf[3] = motor1enable ? 0x00 : 0xff;
        pad->sendbuf[4] = motor2enable ? 0x80 : 0xff;
        err = psxpad_command(pad, sizeof(PSX_CMD_ENABLE_MOTOR));
Quoted 8 lines
        if (err) {
                dev_err(&pad->spi->dev,
                        "%s: ENABLE_MOTOR command failed: %d\n",
                        __func__, err);
                return err;
        }

        ...

Fixed.

I wrote this code with reading otherone's PSX pad's analysis informaton. The LSBFIRST values are listed in them.
I implemented the value so that it is easy to read.

I commented LSBFIRST value, and change to use MSBFIRST value.

Quoted 13 lines
Quoted 13 lines
drivers/input/joystick/psxpad-spi.c new file +13 -0
+
+     err = psxpad_command(pad, PSX_CMD_ENTER_CFG, pad->response, sizeof(PSX_CMD_ENTER_CFG));
+     if (err) {
+             dev_err(&pad->idev->dev, "psxpad-spi: setenablemotor: enter cfg failed!!\n");
+             return;
+     }
+     err = psxpad_command(pad, pad->enablemotor, pad->response, sizeof(PSX_CMD_ENABLE_MOTOR));
+     if (err) {
+             dev_err(&pad->idev->dev, "psxpad-spi: setenablemotor: enable motor failed!!\n");
+             return;
+     }
+     err = psxpad_command(pad, PSX_CMD_EXIT_CFG, pad->response, sizeof(PSX_CMD_EXIT_CFG));
+     if (err) {
Quoted 8 lines

drivers/input/joystick/psxpad-spi.c

Quoted 6 lines
drivers/input/joystick/psxpad-spi.c new file +6 -0
+             dev_err(&pad->idev->dev, "psxpad-spi: setenablemotor: exit config failed!!\n");
+             return;
+     }
+}
+
+static void psxpad_setmotorlevel(struct psxpad *pad, const u8 motor1level, const u8 motor2level)

psxpad_set_motor_level.

Fixed.

Quoted 9 lines

drivers/input/joystick/psxpad-spi.c

Quoted 6 lines
drivers/input/joystick/psxpad-spi.c new file +6 -0
+{
+     pad->motor1level = motor1level ? 0xFF : 0x00;
+     pad->motor2level = motor2level;
+
+     pad->pollcmd[3] = pad->motor1level;
+     pad->pollcmd[4] = pad->motor2level;

So we need to control motors both while polling and explicitly at end of poll?

If the polling is interrupted at regular intervals, the pad will forget to set it.
So I'm enabling the motor in the polling.

And, I changed setting motors at first of polling.

Quoted 8 lines

drivers/input/joystick/psxpad-spi.c

Quoted 5 lines
drivers/input/joystick/psxpad-spi.c new file +5 -0
+}
+#else        /* CONFIG_JOYSTICK_PSXPAD_SPI_FF */
+static void psxpad_setenablemotor(struct psxpad *pad, const bool motor1enable, const bool motor2enable) { }
+
+static void psxpad_setmotorlevel(struct psxpad *pad, const u8 motor1level, const u8 motor2level) { }

Please try keeping within 80 columns.

Fixed.
I got it.

Quoted 19 lines
Quoted 19 lines
drivers/input/joystick/psxpad-spi.c new file +19 -0
+#endif       /* CONFIG_JOYSTICK_PSXPAD_SPI_FF */
+
+static void psxpad_spi_poll_open(struct input_polled_dev *pdev)
+{
+     struct psxpad *pad = pdev->private;
+
+     pm_runtime_get_sync(&pad->spi->dev);
+}
+
+static void psxpad_spi_poll_close(struct input_polled_dev *pdev)
+{
+     struct psxpad *pad = pdev->private;
+
+     pm_runtime_put_sync(&pad->spi->dev);
+}
+
+static void psxpad_spi_poll(struct input_polled_dev *pdev)
+{
+     struct psxpad *pad = pdev->private;
Quoted 8 lines

drivers/input/joystick/psxpad-spi.c

Quoted 5 lines
drivers/input/joystick/psxpad-spi.c new file +5 -0
+     int err;
+
+     err = psxpad_command(pad, pad->pollcmd, pad->response, sizeof(PSX_CMD_POLL));
+     if (err) {
+             dev_err(&pad->idev->dev, "psxpad-spi: poll: poll cmd failed!!\n");

Please use pad->spi->dev in error messages, so errors are associated with a physical device and not abstract one (inputNNN).

Fixed.

Quoted 15 lines

drivers/input/joystick/psxpad-spi.c line 160

Quoted 11 lines
drivers/input/joystick/psxpad-spi.c new file +11 -0
+             return;
+     }
+
+     switch (pad->response[1]) {
+     case 0x73:      /* analog 1 */
+             input_report_abs(pad->idev, ABS_X,              pad->response[7]);
+             input_report_abs(pad->idev, ABS_Y,              pad->response[8]);
+             input_report_abs(pad->idev, ABS_RX,             pad->response[5]);
+             input_report_abs(pad->idev, ABS_RY,             pad->response[6]);
+             input_report_key(pad->idev, BTN_DPAD_UP,        (pad->response[3] & 0x10U) ? false : true);
+             input_report_key(pad->idev, BTN_DPAD_DOWN,      (pad->response[3] & 0x40U) ? false : true);

Please use BIT() macro; there is also no need to convert to true/false as input_report_key also does it.

Quoted 6 lines
                input_report_key(pad->idev, BTN_DPAD_UP,
                                 pad->response[3] & BIT(4));
                input_report_key(pad->idev, BTN_DPAD_DOWN,
                                 pad->response[3] & BIT(7));

and so on.

Fixed.

Quoted 52 lines
Quoted 38 lines
drivers/input/joystick/psxpad-spi.c new file +38 -0
+             input_report_key(pad->idev, BTN_DPAD_LEFT,      (pad->response[3] & 0x80U) ? false : true);
+             input_report_key(pad->idev, BTN_DPAD_RIGHT,     (pad->response[3] & 0x20U) ? false : true);
+             input_report_key(pad->idev, BTN_X,              (pad->response[4] & 0x10U) ? false : true);
+             input_report_key(pad->idev, BTN_A,              (pad->response[4] & 0x20U) ? false : true);
+             input_report_key(pad->idev, BTN_B,              (pad->response[4] & 0x40U) ? false : true);
+             input_report_key(pad->idev, BTN_Y,              (pad->response[4] & 0x80U) ? false : true);
+             input_report_key(pad->idev, BTN_TL,             (pad->response[4] & 0x04U) ? false : true);
+             input_report_key(pad->idev, BTN_TR,             (pad->response[4] & 0x08U) ? false : true);
+             input_report_key(pad->idev, BTN_TL2,            (pad->response[4] & 0x01U) ? false : true);
+             input_report_key(pad->idev, BTN_TR2,            (pad->response[4] & 0x02U) ? false : true);
+             input_report_key(pad->idev, BTN_THUMBL,         (pad->response[3] & 0x02U) ? false : true);
+             input_report_key(pad->idev, BTN_THUMBR,         (pad->response[3] & 0x04U) ? false : true);
+             input_report_key(pad->idev, BTN_SELECT,         (pad->response[3] & 0x01U) ? false : true);
+             input_report_key(pad->idev, BTN_START,          (pad->response[3] & 0x08U) ? false : true);
+             break;
+
+     case 0x41:      /* digital */
+             input_report_abs(pad->idev, ABS_X,              0x80);
+             input_report_abs(pad->idev, ABS_Y,              0x80);
+             input_report_abs(pad->idev, ABS_RX,             0x80);
+             input_report_abs(pad->idev, ABS_RY,             0x80);
+             input_report_key(pad->idev, BTN_DPAD_UP,        (pad->response[3] & 0x10U) ? false : true);
+             input_report_key(pad->idev, BTN_DPAD_DOWN,      (pad->response[3] & 0x40U) ? false : true);
+             input_report_key(pad->idev, BTN_DPAD_LEFT,      (pad->response[3] & 0x80U) ? false : true);
+             input_report_key(pad->idev, BTN_DPAD_RIGHT,     (pad->response[3] & 0x20U) ? false : true);
+             input_report_key(pad->idev, BTN_X,              (pad->response[4] & 0x10U) ? false : true);
+             input_report_key(pad->idev, BTN_A,              (pad->response[4] & 0x20U) ? false : true);
+             input_report_key(pad->idev, BTN_B,              (pad->response[4] & 0x40U) ? false : true);
+             input_report_key(pad->idev, BTN_Y,              (pad->response[4] & 0x80U) ? false : true);
+             input_report_key(pad->idev, BTN_TL,             (pad->response[4] & 0x04U) ? false : true);
+             input_report_key(pad->idev, BTN_TR,             (pad->response[4] & 0x08U) ? false : true);
+             input_report_key(pad->idev, BTN_TL2,            (pad->response[4] & 0x01U) ? false : true);
+             input_report_key(pad->idev, BTN_TR2,            (pad->response[4] & 0x02U) ? false : true);
+             input_report_key(pad->idev, BTN_THUMBL,         false);
+             input_report_key(pad->idev, BTN_THUMBR,         false);
+             input_report_key(pad->idev, BTN_SELECT,         (pad->response[3] & 0x01U) ? false : true);
+             input_report_key(pad->idev, BTN_START,          (pad->response[3] & 0x08U) ? false : true);
+             break;

drivers/input/joystick/psxpad-spi.c line 208

Quoted 10 lines
drivers/input/joystick/psxpad-spi.c new file +10 -0
+     }
+
+     input_sync(pad->idev);
+     psxpad_setenablemotor(pad, true, true);
+}
+
+#ifdef CONFIG_JOYSTICK_PSXPAD_SPI_FF
+static int psxpad_spi_ff(struct input_dev *idev, void *data, struct ff_effect *effect)
+{
+     struct psxpad *pad = idev->dev.platform_data;

platform_data usually reserved for platform (board) code, not driver data.

Quoted 2 lines
        struct input_polled_dev *pdev = input_get_drvdata(input);
        struct psxpad *pad = pdev->private;

Fixed.

Quoted 14 lines
Quoted 14 lines
drivers/input/joystick/psxpad-spi.c new file +14 -0
+
+     switch (effect->type) {
+     case FF_RUMBLE:
+             psxpad_setmotorlevel(pad, (effect->u.rumble.weak_magnitude >> 8) & 0xFFU, (effect->u.rumble.strong_magnitude >> 8) & 0xFFU);
+             break;
+     }
+
+     return 0;
+}
+
+static int psxpad_spi_init_ff(struct psxpad *pad)
+{
+     int err;
+
Quoted 8 lines

drivers/input/joystick/psxpad-spi.c

Quoted 5 lines
drivers/input/joystick/psxpad-spi.c new file +5 -0
+     input_set_capability(pad->idev, EV_FF, FF_RUMBLE);
+     err = input_ff_create_memless(pad->idev, NULL, psxpad_spi_ff);
+     if (err) {
+             dev_err(&pad->idev->dev, "psxpad-spi: init_ff: ff alloc failed!!\n");
+             err = -ENOMEM;

Please do not clobber error value returned by input_ff_create_memless(), pass it on as is.

Fixed.

Quoted 24 lines
Quoted 12 lines
drivers/input/joystick/psxpad-spi.c new file +12 -0
+     }
+
+     return err;
+}
+#else        /* CONFIG_JOYSTICK_PSXPAD_SPI_FF */
+static inline int psxpad_spi_init_ff(struct psxpad *pad)
+{
+     return 0;
+}
+#endif       /* CONFIG_JOYSTICK_PSXPAD_SPI_FF */
+
+static int psxpad_spi_probe(struct spi_device *spi)

drivers/input/joystick/psxpad-spi.c

Quoted 10 lines
drivers/input/joystick/psxpad-spi.c new file +10 -0
+{
+     struct psxpad *pad;
+     struct input_polled_dev *pdev;
+     struct input_dev *idev;
+     int err, i;
+
+     pad = devm_kzalloc(&spi->dev, sizeof(struct psxpad), GFP_KERNEL);
+     if (!pad) {
+             err = -ENOMEM;
+             goto err_free_mem1;

Just

Quoted 1 line
                return -ENOMEM;

Fixed.

Quoted 9 lines

drivers/input/joystick/psxpad-spi.c

Quoted 4 lines
drivers/input/joystick/psxpad-spi.c new file +4 -0
+     }
+     pdev = input_allocate_polled_device();
+     if (!pdev) {
+             dev_err(&spi->dev, "psxpad-spi: probe: pdev alloc failed!!\n");

No need for "psxpad-spi: " prefix - dev_err adds driver name to error messages.

Quoted 1 line
Quoted 9 lines

drivers/input/joystick/psxpad-spi.c

Quoted 2 lines
drivers/input/joystick/psxpad-spi.c new file +2 -0
+             err = -ENOMEM;
+             goto err_free_mem1;

Just

                return -ENOMEM;

devm will take care of freeing "pad" memory. That is is point of devm_* API.

Fixed.

Quoted 9 lines

drivers/input/joystick/psxpad-spi.c

Quoted 5 lines
drivers/input/joystick/psxpad-spi.c new file +5 -0
+     }
+     for (i = 0; i < sizeof(PSX_CMD_POLL); i++)
+             pad->pollcmd[i] = PSX_CMD_POLL[i];
+     for (i = 0; i < sizeof(PSX_CMD_ENABLE_MOTOR); i++)
+             pad->enablemotor[i] = PSX_CMD_ENABLE_MOTOR[i];

I do not think you'll need this anymore if you do what I proposed with regard to command handling.

Fixed.

Quoted 15 lines
Quoted 15 lines
drivers/input/joystick/psxpad-spi.c new file +15 -0
+
+     /* input poll device settings */
+     pad->pdev = pdev;
+     pad->spi = spi;
+     pdev->private = pad;
+     pdev->open = psxpad_spi_poll_open;
+     pdev->close = psxpad_spi_poll_close;
+     pdev->poll = psxpad_spi_poll;
+     /* poll interval is about 60fps */
+     pdev->poll_interval = 16;
+     pdev->poll_interval_min = 8;
+     pdev->poll_interval_max = 32;
+
+     /* input device settings */
+     idev = pdev->input;
Quoted 8 lines

drivers/input/joystick/psxpad-spi.c

Quoted 5 lines
drivers/input/joystick/psxpad-spi.c new file +5 -0
+     pad->idev = idev;
+     idev->name = "PSX (PS1/2) pad";
+     snprintf(pad->phys, sizeof(pad->phys), "%s/input", dev_name(&spi->dev));
+     idev->id.bustype = BUS_SPI;
+     idev->dev.parent = &spi->dev;

Please drop - devm_input_allocate_polled_device should set it up for you.

Fixed.

Quoted 3 lines
Quoted 1 line

+ idev->dev.platform_data = pad;

Please drop.

Fixed.

Quoted 23 lines
Quoted 23 lines
drivers/input/joystick/psxpad-spi.c new file +23 -0
+
+     /* key/value map settings */
+     input_set_abs_params(idev, ABS_X,       0, 255, 0, 0);
+     input_set_abs_params(idev, ABS_Y,       0, 255, 0, 0);
+     input_set_abs_params(idev, ABS_RX,      0, 255, 0, 0);
+     input_set_abs_params(idev, ABS_RY,      0, 255, 0, 0);
+     input_set_capability(idev, EV_KEY, BTN_DPAD_UP);
+     input_set_capability(idev, EV_KEY, BTN_DPAD_DOWN);
+     input_set_capability(idev, EV_KEY, BTN_DPAD_LEFT);
+     input_set_capability(idev, EV_KEY, BTN_DPAD_RIGHT);
+     input_set_capability(idev, EV_KEY, BTN_A);
+     input_set_capability(idev, EV_KEY, BTN_B);
+     input_set_capability(idev, EV_KEY, BTN_X);
+     input_set_capability(idev, EV_KEY, BTN_Y);
+     input_set_capability(idev, EV_KEY, BTN_TL);
+     input_set_capability(idev, EV_KEY, BTN_TR);
+     input_set_capability(idev, EV_KEY, BTN_TL2);
+     input_set_capability(idev, EV_KEY, BTN_TR2);
+     input_set_capability(idev, EV_KEY, BTN_THUMBL);
+     input_set_capability(idev, EV_KEY, BTN_THUMBR);
+     input_set_capability(idev, EV_KEY, BTN_SELECT);
+     input_set_capability(idev, EV_KEY, BTN_START);
+
Quoted 8 lines

drivers/input/joystick/psxpad-spi.c

Quoted 6 lines
drivers/input/joystick/psxpad-spi.c new file +6 -0
+     /* force feedback */
+     err = psxpad_spi_init_ff(pad);
+     if (err) {
+             dev_err(&spi->dev, "psxpad-spi: probe: failed init ff!!\n");
+             err = -ENOMEM;
+             goto err_free_mem2;
                return err;

Fixed.

Quoted 12 lines
Quoted 12 lines
drivers/input/joystick/psxpad-spi.c new file +12 -0
+     }
+
+     /* SPI settings */
+     spi->mode = SPI_MODE_3;
+     spi->bits_per_word = 8;
+     /* (PSX pad might be possible works 250kHz/500kHz) */
+     spi->master->min_speed_hz = 125000;
+     spi->master->max_speed_hz = 125000;
+     spi_setup(spi);
+
+     /* pad settings */
+     psxpad_setmotorlevel(pad, 0, 0);
Quoted 8 lines

drivers/input/joystick/psxpad-spi.c

Quoted 6 lines
drivers/input/joystick/psxpad-spi.c new file +6 -0
+
+     /* register input poll device */
+     err = input_register_polled_device(pdev);
+     if (err) {
+             dev_err(&spi->dev, "psxpad-spi: probe: failed register!!\n");
+             goto err_free_mem2;
                return err;

Fixed.

Quoted 9 lines

drivers/input/joystick/psxpad-spi.c line 401

Quoted 6 lines
drivers/input/joystick/psxpad-spi.c new file +6 -0
+     }
+
+     pm_runtime_enable(&spi->dev);
+
+     return 0;
+

Code below is not needed.

Fixed.

Quoted 8 lines
Quoted 8 lines

+ err_free_mem2:

drivers/input/joystick/psxpad-spi.c new file +7 -0
+     input_free_polled_device(pdev);
+
+ err_free_mem1:
+     devm_kfree(&spi->dev, pad);
+
+     return err;
+}
Quoted 8 lines

drivers/input/joystick/psxpad-spi.c line 91

Quoted 6 lines
drivers/input/joystick/psxpad-spi.c new file +6 -0
+
+
+static int psxpad_spi_remove(struct spi_device *spi)
+{
+     return 0;
+}

You can remove this now empty stub.

Fixed.

Quoted 9 lines
Quoted 9 lines
drivers/input/joystick/psxpad-spi.c new file +9 -0
+
+static int __maybe_unused psxpad_spi_suspend(struct device *dev)
+{
+     struct spi_device *spi = to_spi_device(dev);
+     struct psxpad *pad = spi_get_drvdata(spi);
+
+     psxpad_setmotorlevel(pad, 0, 0);
+
+     return 0;
Quoted 8 lines

drivers/input/joystick/psxpad-spi.c line 284

Quoted 6 lines
drivers/input/joystick/psxpad-spi.c new file +6 -0
+}
+
+static int __maybe_unused psxpad_spi_resume(struct device *dev)
+{
+     return 0;
+}

You can remove this empty stub.

Fixed.

Quoted 30 lines
Quoted 25 lines
drivers/input/joystick/psxpad-spi.c new file +24 -1
+
+static SIMPLE_DEV_PM_OPS(psxpad_spi_pm, psxpad_spi_suspend, psxpad_spi_resume);
+
+static const struct spi_device_id psxpad_spi_id[] = {
+     { "psxpad-spi", 0 },
+     { }
+};
+MODULE_DEVICE_TABLE(spi, psxpad_spi_id);
+
+static struct spi_driver psxpad_spi_driver = {
+     .driver = {
+             .name = "psxpad-spi",
+             .pm = &psxpad_spi_pm,
+     },
+     .id_table = psxpad_spi_id,
+     .probe   = psxpad_spi_probe,
+     .remove  = psxpad_spi_remove,
+};
+
+module_spi_driver(psxpad_spi_driver);
+
+MODULE_AUTHOR("Tomohiro Yoshidomi <[email protected]>");
+MODULE_DESCRIPTION("PSX (Play Station 1/2) pad with SPI Bus Driver");
+MODULE_LICENSE("GPL");
--
Quoted 2 lines

2.11.0

Thanks!

Quoted 2 lines

--
Dmitry

Thank you!
If you have any problem, please mail me.

--
Tomohiro

#2 lore raw
In reply to Tomohiro Yoshidomi
reproduce: wget https://raw.githubusercontent.com/01org/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree make.cross…

Hi Tomohiro,

[auto build test WARNING on input/next]
[also build test WARNING on v4.11-rc8 next-20170428]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Tomohiro-Yoshidomi/Input-psxpad-spi-Add-PlayStation-1-2-joypads-via-SPI-interface-Driver/20170430-114033
base:   https://git.kernel.org/pub/scm/linux/kernel/git/dtor/input.git next
config: ia64-allmodconfig (attached as .config)
compiler: ia64-linux-gcc (GCC) 6.2.0

reproduce:
wget https://raw.githubusercontent.com/01org/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=ia64

Note: it may well be a FALSE warning. FWIW you are at least aware of it now.
http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings

All warnings (new ones prefixed by >>):

drivers/input/joystick/psxpad-spi.c: In function 'psxpad_spi_probe':

>> drivers/input/joystick/psxpad-spi.c:319:3: warning: 'err' may be used uninitialized in this function [-Wmaybe-uninitialized]
      dev_err(&spi->dev,
      ^~~~~~~~~~~~~~~~~~
       "%s: failed to pdev alloc mode: %d\n",
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       __func__, err);
       ~~~~~~~~~~~~~~

vim +/err +319 drivers/input/joystick/psxpad-spi.c

303 return 0;
304 }
305 #endif /* CONFIG_JOYSTICK_PSXPAD_SPI_FF */
306
307 static int psxpad_spi_probe(struct spi_device *spi)
308 {
309 struct psxpad *pad;
310 struct input_polled_dev *pdev;
311 struct input_dev *idev;
312 int err;
313
314 pad = devm_kzalloc(&spi->dev, sizeof(struct psxpad), GFP_KERNEL);
315 if (!pad)
316 return -ENOMEM;
317 pdev = input_allocate_polled_device();
318 if (!pdev) {

 > 319			dev_err(&spi->dev,

320 "%s: failed to pdev alloc mode: %d\n",
321 __func__, err);
322 return -ENOMEM;
323 }
324
325 /* input poll device settings */
326 pad->pdev = pdev;
327 pad->spi = spi;

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#3 lore raw
In reply to Tomohiro Yoshidomi
reproduce: wget https://raw.githubusercontent.com/01org/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree make.cross…

Hi Tomohiro,

[auto build test ERROR on input/next]
[also build test ERROR on v4.11-rc8 next-20170428]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Tomohiro-Yoshidomi/Input-psxpad-spi-Add-PlayStation-1-2-joypads-via-SPI-interface-Driver/20170430-114033
base:   https://git.kernel.org/pub/scm/linux/kernel/git/dtor/input.git next
config: blackfin-allmodconfig (attached as .config)
compiler: bfin-uclinux-gcc (GCC) 6.2.0

reproduce:
wget https://raw.githubusercontent.com/01org/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=blackfin

All error/warnings (new ones prefixed by >>):

In file included from include/linux/device.h:25:0,
from drivers/input/joystick/psxpad-spi.c:23:

>> drivers/input/joystick/psxpad-spi.c:412:61: error: 'psxpad_spi_resume' undeclared here (not in a function)
    static SIMPLE_DEV_PM_OPS(psxpad_spi_pm, psxpad_spi_suspend, psxpad_spi_resume);
                                                                ^
   include/linux/pm.h:319:12: note: in definition of macro 'SET_SYSTEM_SLEEP_PM_OPS'
     .resume = resume_fn, \
               ^~~~~~~~~
>> drivers/input/joystick/psxpad-spi.c:412:8: note: in expansion of macro 'SIMPLE_DEV_PM_OPS'
    static SIMPLE_DEV_PM_OPS(psxpad_spi_pm, psxpad_spi_suspend, psxpad_spi_resume);
           ^~~~~~~~~~~~~~~~~

vim +/psxpad_spi_resume +412 drivers/input/joystick/psxpad-spi.c

406
407 psxpad_set_motor_level(pad, 0, 0);
408
409 return 0;
410 }
411

 > 412	static SIMPLE_DEV_PM_OPS(psxpad_spi_pm, psxpad_spi_suspend, psxpad_spi_resume);

413
414 static const struct spi_device_id psxpad_spi_id[] = {
415 { "psxpad-spi", 0 },

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation