Skip to content

Commit

Permalink
Revert some changes.
Browse files Browse the repository at this point in the history
Makefile and code refactor/cleanup.
  • Loading branch information
Wolf3s committed Dec 3, 2024
1 parent da77f00 commit e406dfe
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 87 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ clean: download_lwNBD
echo " -ds34bt"
$(MAKE) -C modules/ds34bt clean
echo " -pademu"
$(MAKE) -C modules/pademu USE_DS3USB=1 USE_DS3BT=1 USE_DS4USB=1 USE_DS4BT=1 clean
$(MAKE) -C modules/pademu USE_DS3=1 USE_DS4=1 clean
echo "-pc tools"
$(MAKE) -C pc clean

Expand Down Expand Up @@ -521,7 +521,7 @@ $(EE_ASM_DIR)ds34usb.c: modules/ds34usb/iop/ds34usb.irx | $(EE_ASM_DIR)
$(BIN2C) $< $@ $(*F)_irx

modules/pademu/pademu.irx: modules/pademu
$(MAKE) USE_DS3USB=1 USE_DS3BT=1 USE_DS4USB=1 USE_DS4BT=1 -C $< all
$(MAKE) USE_DS3=1 USE_DS4=1 -C $< all

$(EE_ASM_DIR)pademu.c: modules/pademu/pademu.irx
$(BIN2C) $< $@ $(*F)_irx
Expand Down
18 changes: 4 additions & 14 deletions modules/pademu/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,13 @@ IOP_OBJS = pademu_common.o pademu.o sys_utils.o imports.o exports.o padmacro.o
IOP_BIN = pademu.irx

ifeq ($(USE_DS3USB),1)
IOP_CFLAGS += -DDS3USB
IOP_OBJS += ds3usb.o
endif

ifeq ($(USE_DS3BT),1)
IOP_CFLAGS += -DDS3BT
IOP_OBJS += ds3bt.o
IOP_CFLAGS += -DUSE_DS3
IOP_OBJS += ds3usb.o ds3bt.o
endif

ifeq ($(USE_DS4USB),1)
IOP_CFLAGS += -DDS4USB
IOP_OBJS += ds4usb.o
endif

ifeq ($(USE_DS4BT),1)
IOP_CFLAGS += -DDS4BT
IOP_OBJS += ds4bt.o
IOP_CFLAGS += -DUSE_DS4
IOP_OBJS += ds4usb.o ds4bt.o
endif

ifeq ($(VMC),1)
Expand Down
97 changes: 78 additions & 19 deletions modules/pademu/ds3bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ static void bt_config_set(int result, int count, void *arg);
static UsbDriver bt_driver = {NULL, NULL, "ds3bt", bt_probe, bt_connect, bt_disconnect};
static bt_device bt_dev = {-1, -1, -1, -1, -1, -1, DS3BT_STATE_USB_DISCONNECTED};

static int chrg_probe(int devId);
static int chrg_connect(int devId);
static int chrg_disconnect(int devId);
static int chrg_dev = -1;
static UsbDriver chrg_driver = {NULL, NULL, "ds3chrg", chrg_probe, chrg_connect, chrg_disconnect};


static void ds3pad_clear(int pad);
static void ds3pad_init();
Expand Down Expand Up @@ -187,6 +193,42 @@ static int bt_disconnect(int devId)
return 0;
}

int chrg_probe(int devId)
{
UsbDeviceDescriptor *device = NULL;
DPRINTF("chrg_probe: devId=%i\n", devId);
device = (UsbDeviceDescriptor *)UsbGetDeviceStaticDescriptor(devId, NULL, USB_DT_DEVICE);
if (device == NULL) {
DPRINTF("chrg_probe Error - Couldn't get device descriptor\n");
return 0;
}
if (device->idVendor == DS_VID && (device->idProduct == DS3_PID))
return 1;
return 0;
}
int chrg_connect(int devId)
{
int chrg_end;
UsbDeviceDescriptor *device;
UsbConfigDescriptor *config;
DPRINTF("chrg_connect connect: devId=%i\n", devId);
if (chrg_dev != -1) {
DPRINTF("chrg_connect Error - only one device allowed !\n");
return 1;
}
chrg_dev = devId;
chrg_end = UsbOpenEndpoint(devId, NULL);
device = (UsbDeviceDescriptor *)UsbGetDeviceStaticDescriptor(devId, NULL, USB_DT_DEVICE);
config = (UsbConfigDescriptor *)UsbGetDeviceStaticDescriptor(devId, device, USB_DT_CONFIG);
UsbSetDeviceConfiguration(chrg_end, config->bConfigurationValue, NULL, NULL);
return 0;
}
int chrg_disconnect(int devId)
{
DPRINTF("chrg_disconnect devId=%i\n", devId);
chrg_dev = -1;
return 0;
}

#define OUTPUT_01_REPORT_SIZE 48
static const u8 hid_cmd_payload_led_arguments[] = {0xff, 0x27, 0x10, 0x00, 0x32};
Expand Down Expand Up @@ -514,7 +556,7 @@ static void HCI_event_task(int result)
if (i >= MAX_PADS) {
break;
}
pad_status_set(DS3BT_STATE_CONNECTED, i);
ds3pad_status_set(DS3BT_STATE_CONNECTED, i);
hci_remote_name(ds3pad[i].bdaddr);
} else {
DPRINTF("\t Error 0x%02X \n", hci_buf[2]);
Expand Down Expand Up @@ -588,9 +630,9 @@ static void HCI_event_task(int result)
DPRINTF("\n\t Link = 0x%02X \n", hci_buf[11]);
DPRINTF("\t Class = 0x%02X 0x%02X 0x%02X \n", hci_buf[8], hci_buf[9], hci_buf[10]);
for (i = 0; i < MAX_PADS; i++) { // find free slot
if (!pad_status_check(DS3BT_STATE_RUNNING, i) && ds3pad[i].enabled) {
if (pad_status_check(DS3BT_STATE_CONNECTED, i)) {
if (pad_status_check(DS3BT_STATE_DISCONNECTING, i)) // if we're waiting for hci disconnect event
if (!ds3pad_status_check(DS3BT_STATE_RUNNING, i) && ds3pad[i].enabled) {
if (ds3pad_status_check(DS3BT_STATE_CONNECTED, i)) {
if (ds3pad_status_check(DS3BT_STATE_DISCONNECTING, i)) // if we're waiting for hci disconnect event
continue;
else
hci_disconnect(ds3pad[i].hci_handle); // try to disconnect
Expand All @@ -616,9 +658,9 @@ static void HCI_event_task(int result)
}
}
}
pad_status_clear(DS3BT_STATE_CONNECTED, pad);
pad_status_clear(DS3BT_STATE_RUNNING, pad);
pad_status_clear(DS3BT_STATE_DISCONNECTING, pad);
ds3pad_status_clear(DS3BT_STATE_CONNECTED, pad);
ds3pad_status_clear(DS3BT_STATE_RUNNING, pad);
ds3pad_status_clear(DS3BT_STATE_DISCONNECTING, pad);
hci_accept_connection(ds3pad[pad].bdaddr);
break;

Expand Down Expand Up @@ -923,7 +965,7 @@ static int L2CAP_event_task(int result, int bytes)
ds3pad[pad].oldled[3] = 0;
DelayThread(CMD_DELAY);
hid_LEDRumbleCommand(ds3pad[pad].oldled, 0, 0, pad);
pad_status_set(DS3BT_STATE_RUNNING, pad);
ds3pad_status_set(DS3BT_STATE_RUNNING, pad);
pademu_connect(&padf[pad]);
}
ds3pad[pad].btn_delay = 0xFF;
Expand All @@ -933,10 +975,10 @@ static int L2CAP_event_task(int result, int bytes)
DPRINTF("Disconnect Request SCID = 0x%04X \n", (l2cap_buf[12] | (l2cap_buf[13] << 8)));

if ((l2cap_buf[12] | (l2cap_buf[13] << 8)) == control_dcid) {
pad_status_set(DS3BT_STATE_DISCONNECTING, pad);
ds3pad_status_set(DS3BT_STATE_DISCONNECTING, pad);
l2cap_disconnection_response(ds3pad[pad].hci_handle, l2cap_buf[9], control_dcid, ds3pad[pad].control_scid);
} else if ((l2cap_buf[12] | (l2cap_buf[13] << 8)) == interrupt_dcid) {
pad_status_set(DS3BT_STATE_DISCONNECTING, pad);
ds3pad_status_set(DS3BT_STATE_DISCONNECTING, pad);
l2cap_disconnection_response(ds3pad[pad].hci_handle, l2cap_buf[9], interrupt_dcid, ds3pad[pad].interrupt_scid);
}
break;
Expand All @@ -945,10 +987,10 @@ static int L2CAP_event_task(int result, int bytes)
DPRINTF("Disconnect Response SCID = 0x%04X \n", (l2cap_buf[12] | (l2cap_buf[13] << 8)));

if ((l2cap_buf[12] | (l2cap_buf[13] << 8)) == ds3pad[pad].control_scid) {
pad_status_set(DS3BT_STATE_DISCONNECTING, pad);
ds3pad_status_set(DS3BT_STATE_DISCONNECTING, pad);
hci_disconnect(ds3pad[pad].hci_handle);
} else if ((l2cap_buf[12] | (l2cap_buf[13] << 8)) == ds3pad[pad].interrupt_scid) {
pad_status_set(DS3BT_STATE_DISCONNECTING, pad);
ds3pad_status_set(DS3BT_STATE_DISCONNECTING, pad);
identifier++;
l2cap_disconnection_request(ds3pad[pad].hci_handle, identifier, ds3pad[pad].control_scid, control_dcid);
}
Expand Down Expand Up @@ -979,15 +1021,15 @@ static void l2cap_event_cb(int resultCode, int bytes, void *arg)
ret = L2CAP_event_task(resultCode, bytes);

if (ret < MAX_PADS) {
if (pad_status_check(DS3BT_STATE_RUNNING, ret)) {
if (pad_status_check(DS3BT_STATE_DISCONNECT_REQUEST, ret)) {
if (ds3pad_status_check(DS3BT_STATE_RUNNING, ret)) {
if (ds3pad_status_check(DS3BT_STATE_DISCONNECT_REQUEST, ret)) {
if (!ds3pad[ret].isfake) {
identifier++;
l2cap_disconnection_request(ds3pad[ret].hci_handle, identifier, ds3pad[ret].interrupt_scid, interrupt_dcid);
} else {
hci_disconnect(ds3pad[ret].hci_handle);
}
pad_status_clear(DS3BT_STATE_DISCONNECT_REQUEST, ret);
ds3pad_status_clear(DS3BT_STATE_DISCONNECT_REQUEST, ret);
} else if (ds3pad[ret].update_rum) {
hid_LEDRumbleCommand(ds3pad[ret].oldled, ds3pad[ret].lrum, ds3pad[ret].rrum, ret);
ds3pad[ret].update_rum = 0;
Expand All @@ -1006,6 +1048,21 @@ static void l2cap_event_cb(int resultCode, int bytes, void *arg)
/* HID Commands */
/************************************************************/

static int HID_command(u16 handle, u16 scid, u8 *data, u8 length)
{
l2cap_cmd_buf[0] = (u8)(handle & 0xff); // HCI handle with PB,BC flag
l2cap_cmd_buf[1] = (u8)(((handle >> 8) & 0x0f) | 0x20);
l2cap_cmd_buf[2] = (u8)((4 + length) & 0xff); // HCI ACL total data length
l2cap_cmd_buf[3] = (u8)((4 + length) >> 8);
l2cap_cmd_buf[4] = (u8)(length & 0xff); // L2CAP header: Length
l2cap_cmd_buf[5] = (u8)(length >> 8);
l2cap_cmd_buf[6] = (u8)(scid & 0xff); // L2CAP header: Channel ID
l2cap_cmd_buf[7] = (u8)(scid >> 8);
mips_memcpy(&l2cap_cmd_buf[8], data, length);
// output on endpoint 2
return sceUsbdBulkTransfer(bt_dev.outEndp, l2cap_cmd_buf, (8 + length), NULL, NULL);
}

static int hid_initDS34(int pad)
{
u8 init_buf[PS3_F4_REPORT_LEN + 2];
Expand All @@ -1019,7 +1076,7 @@ static int hid_initDS34(int pad)
init_buf[5] = 0x00;
size += PS3_F4_REPORT_LEN;

return L2CAP_Command(ds3pad[pad].hci_handle, ds3pad[pad].control_scid, init_buf, size);
return HID_command(ds3pad[pad].hci_handle, ds3pad[pad].control_scid, init_buf, size);
}

/**
Expand Down Expand Up @@ -1071,7 +1128,7 @@ static int hid_LEDRumbleCommand(u8 *led, u8 lrum, u8 rrum, int pad)
ds3pad[pad].oldled[2] = led[2];
ds3pad[pad].oldled[3] = led[3];

return L2CAP_Command(ds3pad[pad].hci_handle, ds3pad[pad].control_scid, led_buf, size);
return HID_command(ds3pad[pad].hci_handle, ds3pad[pad].control_scid, led_buf, size);
}

static void hid_readReport(u8 *data, int bytes, int pad_idx)
Expand Down Expand Up @@ -1238,6 +1295,8 @@ int ds3bt_init(u8 pads, u8 options)
return 0;
}

sceUsbdRegisterLdd(&chrg_driver);

return 1;
}

Expand All @@ -1248,12 +1307,12 @@ void ds3bt_reset()
if (bt_dev.status & DS3BT_STATE_USB_AUTHORIZED) {
for (pad = 0; pad < MAX_PADS; pad++) {
WaitSema(bt_dev.hid_sema);
pad_status_set(DS3BT_STATE_DISCONNECT_REQUEST, pad);
ds3pad_status_set(DS3BT_STATE_DISCONNECT_REQUEST, pad);
SignalSema(bt_dev.hid_sema);
while (1) {
DelayThread(500);
WaitSema(bt_dev.hid_sema);
if (!pad_status_check(DS3BT_STATE_RUNNING, pad)) {
if (!ds3pad_status_check(DS3BT_STATE_RUNNING, pad)) {
SignalSema(bt_dev.hid_sema);
break;
}
Expand Down
6 changes: 3 additions & 3 deletions modules/pademu/ds3bt.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ enum eDS34BTStatus {
DS3BT_STATE_DISCONNECT_REQUEST = 0x20,
};

#define pad_status_clear(flag, pad) ds3pad[pad].status &= ~flag
#define pad_status_set(flag, pad) ds3pad[pad].status |= flag
#define pad_status_check(flag, pad) (ds3pad[pad].status & flag)
#define ds3pad_status_clear(flag, pad) ds3pad[pad].status &= ~flag
#define ds3pad_status_set(flag, pad) ds3pad[pad].status |= flag
#define ds3pad_status_check(flag, pad) (ds3pad[pad].status & flag)

#define hci_event_flag_clear(flag) hci_event_flag &= ~flag
#define hci_event_flag_set(flag) hci_event_flag |= flag
Expand Down
7 changes: 3 additions & 4 deletions modules/pademu/ds3usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static int usb_probe(int devId)
return 1;
}

if (device->idVendor == DS34_VID && (device->idProduct == DS3_PID))
if (device->idVendor == DS_VID && (device->idProduct == DS3_PID))
return 1;

return 0;
Expand Down Expand Up @@ -146,7 +146,7 @@ static int usb_connect(int devId)

endpoint = (UsbEndpointDescriptor *)sceUsbdScanStaticDescriptor(devId, NULL, USB_DT_ENDPOINT);

do {
while (epCount-- > 0) {
if (endpoint->bmAttributes == USB_ENDPOINT_XFER_INT) {
if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN && ds3pad[pad].interruptEndp < 0) {
ds3pad[pad].interruptEndp = sceUsbdOpenPipe(devId, endpoint);
Expand All @@ -159,8 +159,7 @@ static int usb_connect(int devId)
}

endpoint = (UsbEndpointDescriptor *)((char *)endpoint + endpoint->bLength);

} while (epCount--);
}

if (ds3pad[pad].interruptEndp < 0 || ds3pad[pad].outEndp < 0) {
usb_release(pad);
Expand Down
Loading

0 comments on commit e406dfe

Please sign in to comment.