Skip to content

Commit

Permalink
[mtp] Fix gcc-4.9 compilation, fixes jb#44871
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko Kenttälä committed Feb 21, 2019
1 parent 9b609bd commit 3e6126f
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 112 deletions.
228 changes: 129 additions & 99 deletions mts/transport/usb/descriptor.c
@@ -1,104 +1,134 @@
#include <endian.h>
#include <stdlib.h>
#include <string.h>
#include "mtp1descriptors.h"

const struct mtp1_descriptors_s mtp1descriptors = {
.header = {
.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
.length = cpu_to_le32(sizeof mtp1descriptors),
.fs_count = 4,
.hs_count = 4,
},
.fs_descs = {
.intf = {
.bLength = sizeof mtp1descriptors.fs_descs.intf,
.bDescriptorType = USB_DT_INTERFACE,
.bAlternateSetting = 0,
.bNumEndpoints = 3,
.bInterfaceClass = USB_CLASS_STILL_IMAGE,
.bInterfaceSubClass = USB_SUBCLASS_PTP,
.bInterfaceProtocol = USB_PROTOCOL_PTP,
.iInterface = 1,
},
.mtp_ep_in = {
.bLength = sizeof mtp1descriptors.fs_descs.mtp_ep_in,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(PTP_FS_DATA_PKT_SIZE),
.bInterval = 0,
},
.mtp_ep_out = {
.bLength = sizeof mtp1descriptors.fs_descs.mtp_ep_out,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 2 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(PTP_FS_DATA_PKT_SIZE),
.bInterval = 0,
},
.mtp_ep_int = {
.bLength = sizeof mtp1descriptors.fs_descs.mtp_ep_int,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 3 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.wMaxPacketSize = cpu_to_le16(PTP_FS_EVENT_PKT_SIZE),
.bInterval = 16, // bInterval frames * 1 ms/frame
},
},
.hs_descs = {
.intf = {
.bLength = sizeof mtp1descriptors.hs_descs.intf,
.bDescriptorType = USB_DT_INTERFACE,
.bAlternateSetting = 0,
.bNumEndpoints = 3,
.bInterfaceClass = USB_CLASS_STILL_IMAGE,
.bInterfaceSubClass = USB_SUBCLASS_PTP,
.bInterfaceProtocol = USB_PROTOCOL_PTP,
.iInterface = 1,
},
.mtp_ep_in = {
.bLength = sizeof mtp1descriptors.hs_descs.mtp_ep_in,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(PTP_HS_DATA_PKT_SIZE),
.bInterval = 0,
},
.mtp_ep_out = {
.bLength = sizeof mtp1descriptors.hs_descs.mtp_ep_out,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 2 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(PTP_HS_DATA_PKT_SIZE),
.bInterval = 0,
},
.mtp_ep_int = {
.bLength = sizeof mtp1descriptors.hs_descs.mtp_ep_int,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 3 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.wMaxPacketSize = cpu_to_le16(PTP_HS_EVENT_PKT_SIZE),
.bInterval = 5, // 2**(bInterval-1) franes * 0.125 ms/frame
},
},
};
struct mtp1_descriptors_s* mtp1descriptors_ptr = NULL;
struct mtp1strings_s* mtp1strings_ptr = NULL;
struct usb_functionfs_descs_head_incompatible* mtp1descriptors_header_incompatible_ptr = NULL;

const struct mtp1strings_s mtp1strings = {
.header = {
.magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
.length = cpu_to_le32(sizeof mtp1strings),
.str_count = cpu_to_le32(1),
.lang_count = cpu_to_le32(1),
},
.lang0 = {
cpu_to_le16(ENGLISH_US),
MTP_STRING_DESCRIPTOR,
},
};
const struct mtp1_descriptors_s* mtp1descriptors()
{
if (!mtp1descriptors_ptr) {
const struct mtp1_descriptors_s mtp1descriptors = {
.header = {
.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
.length = cpu_to_le32(sizeof mtp1descriptors),
.fs_count = 4,
.hs_count = 4,
},
.fs_descs = {
.intf = {
.bLength = sizeof mtp1descriptors.fs_descs.intf,
.bDescriptorType = USB_DT_INTERFACE,
.bAlternateSetting = 0,
.bNumEndpoints = 3,
.bInterfaceClass = USB_CLASS_STILL_IMAGE,
.bInterfaceSubClass = USB_SUBCLASS_PTP,
.bInterfaceProtocol = USB_PROTOCOL_PTP,
.iInterface = 1,
},
.mtp_ep_in = {
.bLength = sizeof mtp1descriptors.fs_descs.mtp_ep_in,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(PTP_FS_DATA_PKT_SIZE),
.bInterval = 0,
},
.mtp_ep_out = {
.bLength = sizeof mtp1descriptors.fs_descs.mtp_ep_out,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 2 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(PTP_FS_DATA_PKT_SIZE),
.bInterval = 0,
},
.mtp_ep_int = {
.bLength = sizeof mtp1descriptors.fs_descs.mtp_ep_int,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 3 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.wMaxPacketSize = cpu_to_le16(PTP_FS_EVENT_PKT_SIZE),
.bInterval = 16, // bInterval frames * 1 ms/frame
},
},
.hs_descs = {
.intf = {
.bLength = sizeof mtp1descriptors.hs_descs.intf,
.bDescriptorType = USB_DT_INTERFACE,
.bAlternateSetting = 0,
.bNumEndpoints = 3,
.bInterfaceClass = USB_CLASS_STILL_IMAGE,
.bInterfaceSubClass = USB_SUBCLASS_PTP,
.bInterfaceProtocol = USB_PROTOCOL_PTP,
.iInterface = 1,
},
.mtp_ep_in = {
.bLength = sizeof mtp1descriptors.hs_descs.mtp_ep_in,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 1 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(PTP_HS_DATA_PKT_SIZE),
.bInterval = 0,
},
.mtp_ep_out = {
.bLength = sizeof mtp1descriptors.hs_descs.mtp_ep_out,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 2 | USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = cpu_to_le16(PTP_HS_DATA_PKT_SIZE),
.bInterval = 0,
},
.mtp_ep_int = {
.bLength = sizeof mtp1descriptors.hs_descs.mtp_ep_int,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 3 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.wMaxPacketSize = cpu_to_le16(PTP_HS_EVENT_PKT_SIZE),
.bInterval = 5, // 2**(bInterval-1) franes * 0.125 ms/frame
},
},
};
mtp1descriptors_ptr = malloc(sizeof(struct mtp1_descriptors_s));
memcpy(mtp1descriptors_ptr, &mtp1descriptors, sizeof(struct mtp1_descriptors_s));
}
return mtp1descriptors_ptr;
}

const struct usb_functionfs_descs_head_incompatible mtp1descriptors_header_incompatible = {
.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
.length = cpu_to_le32(sizeof(struct mtp1_descriptors_s_incompatible)),
.fs_count = 4,
.hs_count = 4,
.ss_count = 0
};
const struct mtp1strings_s* mtp1strings()
{
if (!mtp1strings_ptr) {
const struct mtp1strings_s mtp1strings = {
.header = {
.magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
.length = cpu_to_le32(sizeof mtp1strings),
.str_count = cpu_to_le32(1),
.lang_count = cpu_to_le32(1),
},
.lang0 = {
cpu_to_le16(ENGLISH_US),
MTP_STRING_DESCRIPTOR,
},
};
mtp1strings_ptr = malloc(sizeof(struct mtp1strings_s));
memcpy(mtp1strings_ptr, &mtp1strings, sizeof(struct mtp1strings_s));
}
return mtp1strings_ptr;
}

const struct usb_functionfs_descs_head_incompatible* mtp1descriptors_header_incompatible()
{
if (!mtp1descriptors_header_incompatible_ptr) {
const struct usb_functionfs_descs_head_incompatible mtp1descriptors_header_incompatible = {
.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
.length = cpu_to_le32(sizeof(struct mtp1_descriptors_s_incompatible)),
.fs_count = 4,
.hs_count = 4,
.ss_count = 0
};
mtp1descriptors_header_incompatible_ptr = malloc(sizeof(struct usb_functionfs_descs_head_incompatible));
memcpy(mtp1descriptors_header_incompatible_ptr, &mtp1descriptors_header_incompatible, sizeof(struct usb_functionfs_descs_head_incompatible));
}
return mtp1descriptors_header_incompatible_ptr;
}
10 changes: 4 additions & 6 deletions mts/transport/usb/mtp1descriptors.h
Expand Up @@ -30,8 +30,6 @@ struct mtp1_descriptors_s {
struct mtp1_descs_s hs_descs;
} __attribute__((packed));

extern const struct mtp1_descriptors_s mtp1descriptors;

struct mtp1strings_s {
struct usb_functionfs_strings_head header;
struct {
Expand All @@ -40,9 +38,6 @@ struct mtp1strings_s {
} __attribute__((packed)) lang0;
} __attribute__((packed));

extern const struct mtp1strings_s mtp1strings;


struct usb_functionfs_descs_head_incompatible {
__le32 magic;
__le32 length;
Expand All @@ -59,6 +54,9 @@ struct mtp1_descriptors_s_incompatible {
struct mtp1_descs_s hs_descs;
} __attribute__((packed));

extern const struct usb_functionfs_descs_head_incompatible mtp1descriptors_header_incompatible;
// Descriptor getters
const struct mtp1_descriptors_s* mtp1descriptors();
const struct mtp1strings_s* mtp1strings();
const struct usb_functionfs_descs_head_incompatible* mtp1descriptors_header_incompatible();

#endif
15 changes: 9 additions & 6 deletions mts/transport/usb/mtptransporterusb.cpp
Expand Up @@ -42,10 +42,13 @@
#include <linux/usb/functionfs.h>
#include "trace.h"
#include "threadio.h"
#include "mtp1descriptors.h"
#include <QMutex>
#include <QCoreApplication>

extern "C" {
#include "mtp1descriptors.h"
}

using namespace meegomtp1dot0;

MTPTransporterUSB::MTPTransporterUSB() : m_ioState(SUSPENDED), m_containerReadLen(0),
Expand Down Expand Up @@ -104,7 +107,7 @@ void MTPTransporterUSB::onCommandPending()

bool MTPTransporterUSB::writeMtpDescriptors()
{
if (write(m_ctrlFd, &mtp1descriptors, sizeof mtp1descriptors) >= 0)
if (write(m_ctrlFd, mtp1descriptors(), sizeof(struct mtp1_descriptors_s)) >= 0)
return true;

if (errno == EINVAL) {
Expand All @@ -113,9 +116,9 @@ bool MTPTransporterUSB::writeMtpDescriptors()
// Some android kernels changed the usb_functionfs_descs_head size
// by adding an ss_count member. Try it that way.
mtp1_descriptors_s_incompatible descs;
descs.header = mtp1descriptors_header_incompatible;
descs.fs_descs = mtp1descriptors.fs_descs;
descs.hs_descs = mtp1descriptors.hs_descs;
descs.header = *mtp1descriptors_header_incompatible();
descs.fs_descs = mtp1descriptors()->fs_descs;
descs.hs_descs = mtp1descriptors()->hs_descs;
if (write(m_ctrlFd, &descs, sizeof descs) >= 0)
return true;
}
Expand All @@ -127,7 +130,7 @@ bool MTPTransporterUSB::writeMtpDescriptors()

bool MTPTransporterUSB::writeMtpStrings()
{
if (write(m_ctrlFd, &mtp1strings, sizeof(mtp1strings)) >= 0)
if (write(m_ctrlFd, mtp1strings(), sizeof(struct mtp1strings_s)) >= 0)
return true;

MTP_LOG_CRITICAL("Couldn't write strings to control endpoint file"
Expand Down
1 change: 0 additions & 1 deletion mts/transport/usb/mtptransporterusb.h
Expand Up @@ -201,4 +201,3 @@ class MTPTransporterUSB : public MTPTransporter

}
#endif

0 comments on commit 3e6126f

Please sign in to comment.