2014-10-04 03:18:36 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2015-01-07 16:46:50 +00:00
|
|
|
* Copyright (C) 2014-2015 - Mike Robinson
|
2014-10-04 03:18:36 +00:00
|
|
|
*
|
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2015-09-05 18:53:56 +00:00
|
|
|
#include <unistd.h>
|
2014-10-04 03:18:36 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
2015-09-05 18:53:56 +00:00
|
|
|
|
2014-10-04 03:18:36 +00:00
|
|
|
#include <linux/parport.h>
|
|
|
|
#include <linux/ppdev.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2015-09-05 18:53:56 +00:00
|
|
|
#include "../input_autodetect.h"
|
|
|
|
#include "../../general.h"
|
2015-11-23 11:03:38 +00:00
|
|
|
#include "../../verbosity.h"
|
2015-09-05 18:53:56 +00:00
|
|
|
|
2014-10-04 03:18:36 +00:00
|
|
|
/* Linux parport driver does not support reading the control register
|
|
|
|
Other platforms may support up to 17 buttons */
|
2014-10-27 14:45:18 +00:00
|
|
|
#define PARPORT_NUM_BUTTONS 13
|
2014-10-04 03:18:36 +00:00
|
|
|
|
|
|
|
struct parport_joypad
|
|
|
|
{
|
|
|
|
int fd;
|
2015-02-15 00:57:29 +00:00
|
|
|
uint64_t buttons;
|
2014-10-27 14:45:18 +00:00
|
|
|
bool button_enable[PARPORT_NUM_BUTTONS];
|
2014-10-04 16:26:28 +00:00
|
|
|
char saved_data;
|
|
|
|
char saved_control;
|
2014-10-04 03:18:36 +00:00
|
|
|
char *ident;
|
|
|
|
};
|
|
|
|
|
2015-01-05 00:58:00 +00:00
|
|
|
static struct parport_joypad parport_pads[MAX_USERS];
|
2014-10-04 03:18:36 +00:00
|
|
|
|
2014-10-27 14:45:18 +00:00
|
|
|
static void parport_poll_pad(struct parport_joypad *pad)
|
2014-10-04 03:18:36 +00:00
|
|
|
{
|
|
|
|
/* RetroArch uses an extended version of the Linux
|
|
|
|
* Multisystem 2-button joystick protocol for parallel port
|
|
|
|
* joypads:
|
|
|
|
*
|
|
|
|
* | Function | Pin | Register | Bit | Active |
|
|
|
|
* |-------------|-----|----------|-----|--------|
|
|
|
|
* | Up | 2 | Data | 0 | Low |
|
|
|
|
* | Down | 3 | Data | 1 | Low |
|
|
|
|
* | Left | 4 | Data | 2 | Low |
|
|
|
|
* | Right | 5 | Data | 3 | Low |
|
|
|
|
* | A | 6 | Data | 4 | Low |
|
|
|
|
* | B | 7 | Data | 5 | Low |
|
|
|
|
* | Start | 8 | Data | 6 | Low |
|
|
|
|
* | Select | 9 | Data | 7 | Low |
|
|
|
|
* | Menu toggle | 10 | Status | 6 | Low |
|
|
|
|
* | X | 11 | Status | 7 | Low* |
|
|
|
|
* | Y | 12 | Status | 5 | Low |
|
|
|
|
* | L1 | 13 | Status | 4 | Low |
|
|
|
|
* | R1 | 15 | Status | 3 | Low |
|
|
|
|
*
|
|
|
|
* (*) Pin is hardware inverted, but RetroArch inverts it
|
|
|
|
* back again so the same pullup scheme may be used for
|
|
|
|
* all pins.
|
|
|
|
*
|
2014-10-04 17:03:00 +00:00
|
|
|
* Pin 1 is set high so it can be used for pullups.
|
|
|
|
*
|
2014-10-04 03:18:36 +00:00
|
|
|
* RetroArch does not perform debouncing, and so long as
|
|
|
|
* the button settling time is less than the frame time
|
|
|
|
* no bouncing will be observed. This replicates the latching
|
|
|
|
* behavior common in old games consoles. For optimum latency
|
|
|
|
* and jitter a high performance debouncing routine should be
|
|
|
|
* implemented in the controller hardware.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int i;
|
|
|
|
char data;
|
|
|
|
char status;
|
|
|
|
|
|
|
|
ioctl(pad->fd, PPRDATA, &data);
|
|
|
|
ioctl(pad->fd, PPRSTATUS, &status);
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
2014-10-17 05:41:43 +00:00
|
|
|
if (!(data & UINT8_C(1 << i)) && pad->button_enable[i])
|
2015-02-15 00:57:29 +00:00
|
|
|
BIT64_SET(pad->buttons, i);
|
2014-10-17 05:41:43 +00:00
|
|
|
else
|
2015-02-15 00:57:29 +00:00
|
|
|
BIT64_CLEAR(pad->buttons, i);
|
2014-10-04 03:18:36 +00:00
|
|
|
}
|
|
|
|
for (i = 3; i < 8; i++)
|
|
|
|
{
|
2014-10-17 05:41:43 +00:00
|
|
|
if (!(status & UINT8_C(1 << i)) && pad->button_enable[i + 5])
|
2015-02-15 00:57:29 +00:00
|
|
|
BIT64_SET(pad->buttons, i + 5);
|
2014-10-17 05:41:43 +00:00
|
|
|
else
|
2015-02-15 00:57:29 +00:00
|
|
|
BIT64_CLEAR(pad->buttons, i + 5);
|
2014-10-04 03:18:36 +00:00
|
|
|
}
|
2014-10-17 05:41:43 +00:00
|
|
|
|
2015-02-15 00:57:29 +00:00
|
|
|
if (BIT64_GET(pad->buttons, 12) && pad->button_enable[12])
|
|
|
|
BIT64_CLEAR(pad->buttons, 12);
|
2014-10-17 05:41:43 +00:00
|
|
|
else
|
2015-02-15 00:57:29 +00:00
|
|
|
BIT64_SET(pad->buttons, 12);
|
2014-10-04 03:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool parport_joypad_init_pad(const char *path, struct parport_joypad *pad)
|
|
|
|
{
|
2015-03-20 21:41:15 +00:00
|
|
|
int i;
|
2014-10-04 03:18:36 +00:00
|
|
|
char data;
|
2014-10-04 16:26:28 +00:00
|
|
|
struct ppdev_frob_struct frob;
|
2015-04-02 23:19:51 +00:00
|
|
|
int datadir = 1; /* read */
|
|
|
|
bool set_control = false;
|
|
|
|
int mode = IEEE1284_MODE_BYTE;
|
2015-03-20 21:41:15 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
2014-10-04 03:18:36 +00:00
|
|
|
|
|
|
|
if (pad->fd >= 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (access(path, R_OK | W_OK) < 0)
|
|
|
|
return false;
|
|
|
|
|
2015-04-02 23:19:51 +00:00
|
|
|
pad->fd = open(path, O_RDWR | O_NONBLOCK);
|
2014-10-04 03:18:36 +00:00
|
|
|
*pad->ident = '\0';
|
2015-04-02 23:19:51 +00:00
|
|
|
|
2014-10-04 03:18:36 +00:00
|
|
|
if (pad->fd >= 0)
|
|
|
|
{
|
|
|
|
RARCH_LOG("[Joypad]: Found parallel port: %s\n", path);
|
|
|
|
|
|
|
|
/* Parport driver does not log failures with RARCH_ERR because they could be
|
|
|
|
* a normal result of connected non-joypad devices. */
|
|
|
|
if (ioctl(pad->fd, PPCLAIM) < 0)
|
|
|
|
{
|
|
|
|
RARCH_WARN("[Joypad]: Failed to claim %s\n", path);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (ioctl(pad->fd, PPSETMODE, &mode) < 0)
|
|
|
|
{
|
|
|
|
RARCH_WARN("[Joypad]: Failed to set byte mode on %s\n", path);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (ioctl(pad->fd, PPDATADIR, &datadir) < 0)
|
|
|
|
{
|
|
|
|
RARCH_WARN("[Joypad]: Failed to set data direction to input on %s\n", path);
|
|
|
|
goto error;
|
|
|
|
}
|
2014-10-04 16:26:28 +00:00
|
|
|
|
|
|
|
if (ioctl(pad->fd, PPRDATA, &data) < 0)
|
|
|
|
{
|
|
|
|
RARCH_WARN("[Joypad]: Failed to save original data register on %s\n", path);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
pad->saved_data = data;
|
|
|
|
|
2014-10-04 03:18:36 +00:00
|
|
|
if (ioctl(pad->fd, PPRCONTROL, &data) == 0)
|
|
|
|
{
|
2014-10-04 16:26:28 +00:00
|
|
|
pad->saved_control = data;
|
|
|
|
/* Clear strobe bit to set strobe high for pullup +V */
|
|
|
|
/* Clear control bit 4 to disable interrupts */
|
|
|
|
frob.mask = PARPORT_CONTROL_STROBE | (UINT8_C(1 << 4));
|
|
|
|
frob.val = 0;
|
|
|
|
if (ioctl(pad->fd, PPFCONTROL, &frob) == 0)
|
|
|
|
set_control = true;
|
2014-10-04 03:18:36 +00:00
|
|
|
}
|
2014-10-04 16:26:28 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
data = pad->saved_data;
|
|
|
|
if (ioctl(pad->fd, PPWDATA, &data) < 0)
|
|
|
|
RARCH_WARN("[Joypad]: Failed to restore original data register on %s\n", path);
|
|
|
|
RARCH_WARN("[Joypad]: Failed to save original control register on %s\n", path);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2014-10-04 17:03:00 +00:00
|
|
|
/* Failure to enable strobe can break Linux Multisystem style controllers.
|
2014-10-04 16:26:28 +00:00
|
|
|
* Controllers using an alternative power source will still work.
|
|
|
|
* Failure to disable interrupts slightly increases CPU usage. */
|
|
|
|
if (!set_control)
|
|
|
|
RARCH_WARN("[Joypad]: Failed to clear nStrobe and nIRQ bits on %s\n", path);
|
2014-10-04 03:18:36 +00:00
|
|
|
|
2015-03-20 21:41:15 +00:00
|
|
|
strlcpy(pad->ident, path, sizeof(settings->input.device_names[0]));
|
2014-10-04 17:03:00 +00:00
|
|
|
|
2014-10-27 14:45:18 +00:00
|
|
|
for (i = 0; i < PARPORT_NUM_BUTTONS; i++)
|
2014-10-04 17:03:00 +00:00
|
|
|
pad->button_enable[i] = true;
|
|
|
|
|
2014-10-04 03:18:36 +00:00
|
|
|
return true;
|
|
|
|
error:
|
|
|
|
close(pad->fd);
|
|
|
|
pad->fd = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RARCH_WARN("[Joypad]: Failed to open parallel port %s (error: %s).\n", path, strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parport_joypad_poll(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-01-05 00:58:00 +00:00
|
|
|
for (i = 0; i < MAX_USERS; i++)
|
2014-10-04 03:18:36 +00:00
|
|
|
{
|
2014-10-27 14:45:18 +00:00
|
|
|
if (parport_pads[i].fd >= 0)
|
|
|
|
parport_poll_pad(&parport_pads[i]);
|
2014-10-04 03:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 23:37:20 +00:00
|
|
|
static void parport_free_pad(struct parport_joypad *pad)
|
2014-10-04 17:03:00 +00:00
|
|
|
{
|
2015-04-02 23:19:51 +00:00
|
|
|
char data = pad->saved_data;
|
|
|
|
|
2014-10-04 17:03:00 +00:00
|
|
|
if (ioctl(pad->fd, PPWDATA, &data) < 0)
|
|
|
|
RARCH_ERR("[Joypad]: Failed to restore original data register on %s\n", pad->ident);
|
|
|
|
|
|
|
|
data = pad->saved_control;
|
|
|
|
if (ioctl(pad->fd, PPWDATA, &data) < 0)
|
|
|
|
RARCH_ERR("[Joypad]: Failed to restore original control register on %s\n", pad->ident);
|
|
|
|
|
|
|
|
if (ioctl(pad->fd, PPRELEASE) < 0)
|
|
|
|
RARCH_ERR("[Joypad]: Failed to release parallel port %s\n", pad->ident);
|
|
|
|
|
|
|
|
close(pad->fd);
|
|
|
|
pad->fd = -1;
|
|
|
|
}
|
|
|
|
|
2015-06-03 16:22:54 +00:00
|
|
|
static bool parport_joypad_init(void *data)
|
2014-10-04 03:18:36 +00:00
|
|
|
{
|
2014-10-04 17:03:00 +00:00
|
|
|
unsigned i, j;
|
2015-06-12 22:02:28 +00:00
|
|
|
bool found_enabled_button = false;
|
|
|
|
bool found_disabled_button = false;
|
|
|
|
char buf[PARPORT_NUM_BUTTONS * 3 + 1] = {0};
|
|
|
|
char pin[3 + 1] = {0};
|
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
autoconfig_params_t params = {{0}};
|
2014-10-04 03:18:36 +00:00
|
|
|
|
2015-06-03 16:22:54 +00:00
|
|
|
(void)data;
|
|
|
|
|
2014-10-27 14:45:18 +00:00
|
|
|
memset(buf, 0, PARPORT_NUM_BUTTONS * 3 + 1);
|
2014-10-09 23:12:44 +00:00
|
|
|
|
2015-01-05 00:58:00 +00:00
|
|
|
for (i = 0; i < MAX_USERS; i++)
|
2014-10-04 03:18:36 +00:00
|
|
|
{
|
2015-06-12 22:02:28 +00:00
|
|
|
char path[PATH_MAX_LENGTH] = {0};
|
2014-10-27 14:45:18 +00:00
|
|
|
struct parport_joypad *pad = &parport_pads[i];
|
2015-01-07 19:42:36 +00:00
|
|
|
|
2015-03-20 21:41:15 +00:00
|
|
|
pad->fd = -1;
|
|
|
|
pad->ident = settings->input.device_names[i];
|
2014-10-04 03:18:36 +00:00
|
|
|
|
|
|
|
snprintf(path, sizeof(path), "/dev/parport%u", i);
|
|
|
|
|
2015-03-27 15:57:58 +00:00
|
|
|
params.idx = i;
|
|
|
|
|
2014-10-04 03:18:36 +00:00
|
|
|
if (parport_joypad_init_pad(path, pad))
|
2014-10-04 17:03:00 +00:00
|
|
|
{
|
|
|
|
/* If a pin is low on initialization it can either mean
|
|
|
|
* a button is pressed or that nothing is connected.
|
|
|
|
* Polling non-connected pins can render the menu unusable
|
|
|
|
* so assume the user is not holding any button on startup
|
|
|
|
* and disable any low pins.
|
|
|
|
*/
|
2014-10-27 14:45:18 +00:00
|
|
|
parport_poll_pad(pad);
|
2014-10-04 17:59:31 +00:00
|
|
|
found_enabled_button = false;
|
|
|
|
found_disabled_button = false;
|
2014-10-04 17:03:00 +00:00
|
|
|
|
2014-10-27 14:45:18 +00:00
|
|
|
for (j = 0; j < PARPORT_NUM_BUTTONS; j++)
|
2014-10-04 17:03:00 +00:00
|
|
|
{
|
2015-02-15 00:57:29 +00:00
|
|
|
if (!(BIT64_GET(pad->buttons, j)))
|
2014-10-04 17:59:31 +00:00
|
|
|
{
|
|
|
|
pad->button_enable[j] = true;
|
|
|
|
found_enabled_button = true;
|
|
|
|
}
|
2014-10-04 17:03:00 +00:00
|
|
|
else
|
2014-10-04 17:59:31 +00:00
|
|
|
{
|
2014-10-04 17:03:00 +00:00
|
|
|
pad->button_enable[j] = false;
|
2014-10-04 17:59:31 +00:00
|
|
|
found_disabled_button = true;
|
|
|
|
}
|
2014-10-04 17:03:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-04 17:59:31 +00:00
|
|
|
if (found_enabled_button)
|
|
|
|
{
|
|
|
|
if (found_disabled_button)
|
|
|
|
{
|
|
|
|
buf[0] = '\0';
|
2014-10-27 14:45:18 +00:00
|
|
|
for (j = 0; j < PARPORT_NUM_BUTTONS; j++)
|
2014-10-04 17:59:31 +00:00
|
|
|
{
|
|
|
|
if (!pad->button_enable[j])
|
|
|
|
{
|
|
|
|
snprintf(pin, sizeof(pin), "%d ", j);
|
|
|
|
strlcat(buf, pin, sizeof(buf));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RARCH_WARN("[Joypad]: Pin(s) %son %s were low on init, assuming not connected\n", \
|
2015-03-27 15:57:58 +00:00
|
|
|
buf, path);
|
2014-10-04 17:59:31 +00:00
|
|
|
}
|
2015-03-27 15:57:58 +00:00
|
|
|
strlcpy(params.name, "Generic Parallel Port device", sizeof(params.name));
|
|
|
|
strlcpy(params.driver, "parport", sizeof(params.driver));
|
2014-10-04 17:59:31 +00:00
|
|
|
}
|
2014-10-04 17:03:00 +00:00
|
|
|
else
|
|
|
|
{
|
2014-10-04 17:59:31 +00:00
|
|
|
RARCH_WARN("[Joypad]: All pins low on %s, assuming nothing connected\n", path);
|
2015-04-02 23:37:20 +00:00
|
|
|
parport_free_pad(pad);
|
2014-10-04 17:03:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-27 15:57:58 +00:00
|
|
|
input_config_autoconfigure_joypad(¶ms);
|
|
|
|
}
|
2014-10-04 03:18:36 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parport_joypad_destroy(void)
|
|
|
|
{
|
|
|
|
unsigned i;
|
2014-10-04 16:26:28 +00:00
|
|
|
|
2015-01-05 00:58:00 +00:00
|
|
|
for (i = 0; i < MAX_USERS; i++)
|
2014-10-04 03:18:36 +00:00
|
|
|
{
|
2015-09-28 21:00:22 +00:00
|
|
|
struct parport_joypad *pad = (struct parport_joypad*)&parport_pads[i];
|
2014-10-04 16:26:28 +00:00
|
|
|
if (pad->fd >= 0)
|
2015-04-02 23:37:20 +00:00
|
|
|
parport_free_pad(pad);
|
2014-10-04 03:18:36 +00:00
|
|
|
}
|
2015-09-28 21:00:22 +00:00
|
|
|
|
2014-10-27 14:45:18 +00:00
|
|
|
memset(parport_pads, 0, sizeof(parport_pads));
|
2015-01-05 00:58:00 +00:00
|
|
|
for (i = 0; i < MAX_USERS; i++)
|
2014-10-27 14:45:18 +00:00
|
|
|
parport_pads[i].fd = -1;
|
2014-10-04 03:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool parport_joypad_button(unsigned port, uint16_t joykey)
|
|
|
|
{
|
2014-10-27 14:45:18 +00:00
|
|
|
const struct parport_joypad *pad = (const struct parport_joypad*)&parport_pads[port];
|
2015-02-15 00:57:29 +00:00
|
|
|
if (!pad)
|
|
|
|
return false;
|
|
|
|
return joykey < PARPORT_NUM_BUTTONS && BIT64_GET(pad->buttons, joykey);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t parport_joypad_get_buttons(unsigned port)
|
|
|
|
{
|
|
|
|
const struct parport_joypad *pad = (const struct parport_joypad*)&parport_pads[port];
|
|
|
|
if (!pad)
|
|
|
|
return false;
|
|
|
|
return pad->buttons;
|
2014-10-04 03:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int16_t parport_joypad_axis(unsigned port, uint32_t joyaxis)
|
|
|
|
{
|
|
|
|
/* Parport does not support analog sticks */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool parport_joypad_query_pad(unsigned pad)
|
|
|
|
{
|
2015-01-05 00:58:00 +00:00
|
|
|
return pad < MAX_USERS && parport_pads[pad].fd >= 0;
|
2014-10-04 03:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *parport_joypad_name(unsigned pad)
|
|
|
|
{
|
2015-01-05 00:58:00 +00:00
|
|
|
if (pad >= MAX_USERS)
|
2014-10-04 03:18:36 +00:00
|
|
|
return NULL;
|
|
|
|
|
2014-10-27 14:45:18 +00:00
|
|
|
return *parport_pads[pad].ident ? parport_pads[pad].ident : NULL;
|
2014-10-04 03:18:36 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 14:37:59 +00:00
|
|
|
input_device_driver_t parport_joypad = {
|
2014-10-04 03:18:36 +00:00
|
|
|
parport_joypad_init,
|
|
|
|
parport_joypad_query_pad,
|
|
|
|
parport_joypad_destroy,
|
|
|
|
parport_joypad_button,
|
2015-02-15 00:57:29 +00:00
|
|
|
parport_joypad_get_buttons,
|
2014-10-04 03:18:36 +00:00
|
|
|
parport_joypad_axis,
|
|
|
|
parport_joypad_poll,
|
|
|
|
NULL,
|
|
|
|
parport_joypad_name,
|
|
|
|
"parport",
|
|
|
|
};
|