2017-11-07 06:58:41 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-12-14 18:18:06 -07:00
|
|
|
/*
|
|
|
|
* Greybus Camera protocol driver.
|
|
|
|
*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
* Copyright 2015 Linaro Ltd.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/debugfs.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
|
2016-01-13 12:52:38 -07:00
|
|
|
#include "gb-camera.h"
|
2015-12-14 18:18:06 -07:00
|
|
|
#include "greybus.h"
|
|
|
|
#include "greybus_protocols.h"
|
|
|
|
|
|
|
|
enum gb_camera_debugs_buffer_id {
|
|
|
|
GB_CAMERA_DEBUGFS_BUFFER_CAPABILITIES,
|
|
|
|
GB_CAMERA_DEBUGFS_BUFFER_STREAMS,
|
|
|
|
GB_CAMERA_DEBUGFS_BUFFER_CAPTURE,
|
|
|
|
GB_CAMERA_DEBUGFS_BUFFER_FLUSH,
|
|
|
|
GB_CAMERA_DEBUGFS_BUFFER_MAX,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct gb_camera_debugfs_buffer {
|
|
|
|
char data[PAGE_SIZE];
|
|
|
|
size_t length;
|
|
|
|
};
|
|
|
|
|
2016-06-14 06:59:24 -06:00
|
|
|
enum gb_camera_state {
|
|
|
|
GB_CAMERA_STATE_UNCONFIGURED,
|
|
|
|
GB_CAMERA_STATE_CONFIGURED,
|
|
|
|
};
|
|
|
|
|
2015-12-14 18:18:06 -07:00
|
|
|
/**
|
|
|
|
* struct gb_camera - A Greybus Camera Device
|
2016-05-31 02:33:11 -06:00
|
|
|
* @connection: the greybus connection for camera management
|
|
|
|
* @data_connection: the greybus connection for camera data
|
2016-06-14 06:59:25 -06:00
|
|
|
* @data_cport_id: the data CPort ID on the module side
|
2016-06-14 06:59:24 -06:00
|
|
|
* @mutex: protects the connection and state fields
|
|
|
|
* @state: the current module state
|
2015-12-14 18:18:06 -07:00
|
|
|
* @debugfs: debugfs entries for camera protocol operations testing
|
2016-03-14 10:44:53 -06:00
|
|
|
* @module: Greybus camera module registered to HOST processor.
|
2015-12-14 18:18:06 -07:00
|
|
|
*/
|
|
|
|
struct gb_camera {
|
2016-05-20 05:33:35 -06:00
|
|
|
struct gb_bundle *bundle;
|
2015-12-14 18:18:06 -07:00
|
|
|
struct gb_connection *connection;
|
2016-05-11 02:18:06 -06:00
|
|
|
struct gb_connection *data_connection;
|
2016-06-14 06:59:25 -06:00
|
|
|
u16 data_cport_id;
|
2016-06-14 06:59:24 -06:00
|
|
|
|
2016-05-31 02:33:11 -06:00
|
|
|
struct mutex mutex;
|
2016-06-14 06:59:24 -06:00
|
|
|
enum gb_camera_state state;
|
2015-12-14 18:18:06 -07:00
|
|
|
|
|
|
|
struct {
|
|
|
|
struct dentry *root;
|
|
|
|
struct gb_camera_debugfs_buffer *buffers;
|
|
|
|
} debugfs;
|
2016-03-14 10:44:53 -06:00
|
|
|
|
|
|
|
struct gb_camera_module module;
|
2015-12-14 18:18:06 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct gb_camera_stream_config {
|
|
|
|
unsigned int width;
|
|
|
|
unsigned int height;
|
|
|
|
unsigned int format;
|
|
|
|
unsigned int vc;
|
|
|
|
unsigned int dt[2];
|
|
|
|
unsigned int max_size;
|
|
|
|
};
|
|
|
|
|
2016-07-15 03:03:41 -06:00
|
|
|
struct gb_camera_fmt_info {
|
2016-01-13 12:52:38 -07:00
|
|
|
enum v4l2_mbus_pixelcode mbus_code;
|
|
|
|
unsigned int gb_format;
|
2016-07-15 03:03:41 -06:00
|
|
|
unsigned int bpp;
|
2016-01-13 12:52:38 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/* GB format to media code map */
|
2016-07-15 03:03:41 -06:00
|
|
|
static const struct gb_camera_fmt_info gb_fmt_info[] = {
|
2016-01-13 12:52:38 -07:00
|
|
|
{
|
|
|
|
.mbus_code = V4L2_MBUS_FMT_UYVY8_1X16,
|
|
|
|
.gb_format = 0x01,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 16,
|
2016-01-13 12:52:38 -07:00
|
|
|
},
|
|
|
|
{
|
2016-01-22 08:59:44 -07:00
|
|
|
.mbus_code = V4L2_MBUS_FMT_NV12_1x8,
|
|
|
|
.gb_format = 0x12,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 12,
|
2016-01-22 08:59:44 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.mbus_code = V4L2_MBUS_FMT_NV21_1x8,
|
|
|
|
.gb_format = 0x13,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 12,
|
2016-01-22 08:59:44 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.mbus_code = V4L2_MBUS_FMT_YU12_1x8,
|
2016-01-13 12:52:38 -07:00
|
|
|
.gb_format = 0x16,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 12,
|
2016-01-13 12:52:38 -07:00
|
|
|
},
|
|
|
|
{
|
2016-01-22 08:59:44 -07:00
|
|
|
.mbus_code = V4L2_MBUS_FMT_YV12_1x8,
|
2016-01-13 12:52:38 -07:00
|
|
|
.gb_format = 0x17,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 12,
|
2016-01-13 12:52:38 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.mbus_code = V4L2_MBUS_FMT_JPEG_1X8,
|
|
|
|
.gb_format = 0x40,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 0,
|
2016-04-01 08:32:43 -06:00
|
|
|
},
|
|
|
|
{
|
2016-07-27 12:47:01 -06:00
|
|
|
.mbus_code = V4L2_MBUS_FMT_GB_CAM_METADATA_1X8,
|
2016-04-01 08:32:43 -06:00
|
|
|
.gb_format = 0x41,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 0,
|
2016-06-09 02:53:15 -06:00
|
|
|
},
|
|
|
|
{
|
2016-07-27 12:47:01 -06:00
|
|
|
.mbus_code = V4L2_MBUS_FMT_GB_CAM_DEBUG_DATA_1X8,
|
2016-06-09 02:53:15 -06:00
|
|
|
.gb_format = 0x42,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 0,
|
2016-06-09 02:53:15 -06:00
|
|
|
},
|
2016-06-23 03:39:18 -06:00
|
|
|
{
|
|
|
|
.mbus_code = V4L2_MBUS_FMT_SBGGR10_1X10,
|
|
|
|
.gb_format = 0x80,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 10,
|
2016-06-23 03:39:18 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.mbus_code = V4L2_MBUS_FMT_SGBRG10_1X10,
|
|
|
|
.gb_format = 0x81,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 10,
|
2016-06-23 03:39:18 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.mbus_code = V4L2_MBUS_FMT_SGRBG10_1X10,
|
|
|
|
.gb_format = 0x82,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 10,
|
2016-06-23 03:39:18 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.mbus_code = V4L2_MBUS_FMT_SRGGB10_1X10,
|
|
|
|
.gb_format = 0x83,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 10,
|
2016-06-23 03:39:18 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.mbus_code = V4L2_MBUS_FMT_SBGGR12_1X12,
|
|
|
|
.gb_format = 0x84,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 12,
|
2016-06-23 03:39:18 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.mbus_code = V4L2_MBUS_FMT_SGBRG12_1X12,
|
|
|
|
.gb_format = 0x85,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 12,
|
2016-06-23 03:39:18 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.mbus_code = V4L2_MBUS_FMT_SGRBG12_1X12,
|
|
|
|
.gb_format = 0x86,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 12,
|
2016-06-23 03:39:18 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.mbus_code = V4L2_MBUS_FMT_SRGGB12_1X12,
|
|
|
|
.gb_format = 0x87,
|
2016-07-15 03:03:41 -06:00
|
|
|
.bpp = 12,
|
2016-06-23 03:39:18 -06:00
|
|
|
},
|
2016-01-13 12:52:38 -07:00
|
|
|
};
|
|
|
|
|
2016-07-15 03:03:44 -06:00
|
|
|
static const struct gb_camera_fmt_info *gb_camera_get_format_info(u16 gb_fmt)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(gb_fmt_info); i++) {
|
|
|
|
if (gb_fmt_info[i].gb_format == gb_fmt)
|
|
|
|
return &gb_fmt_info[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-12-14 18:18:06 -07:00
|
|
|
#define ES2_APB_CDSI0_CPORT 16
|
|
|
|
#define ES2_APB_CDSI1_CPORT 17
|
|
|
|
|
|
|
|
#define GB_CAMERA_MAX_SETTINGS_SIZE 8192
|
|
|
|
|
2016-05-20 05:33:35 -06:00
|
|
|
#define gcam_dbg(gcam, format...) dev_dbg(&gcam->bundle->dev, format)
|
|
|
|
#define gcam_info(gcam, format...) dev_info(&gcam->bundle->dev, format)
|
|
|
|
#define gcam_err(gcam, format...) dev_err(&gcam->bundle->dev, format)
|
2015-12-14 18:18:06 -07:00
|
|
|
|
2016-07-15 03:03:42 -06:00
|
|
|
static int gb_camera_operation_sync_flags(struct gb_connection *connection,
|
|
|
|
int type, unsigned int flags,
|
|
|
|
void *request, size_t request_size,
|
|
|
|
void *response, size_t *response_size)
|
|
|
|
{
|
|
|
|
struct gb_operation *operation;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
operation = gb_operation_create_flags(connection, type, request_size,
|
|
|
|
*response_size, flags,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!operation)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
if (request_size)
|
|
|
|
memcpy(operation->request->payload, request, request_size);
|
|
|
|
|
|
|
|
ret = gb_operation_request_send_sync(operation);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&connection->hd->dev,
|
|
|
|
"%s: synchronous operation of type 0x%02x failed: %d\n",
|
|
|
|
connection->name, type, ret);
|
|
|
|
} else {
|
|
|
|
*response_size = operation->response->payload_size;
|
|
|
|
|
|
|
|
if (operation->response->payload_size)
|
|
|
|
memcpy(response, operation->response->payload,
|
|
|
|
operation->response->payload_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
gb_operation_put(operation);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-07-15 03:03:45 -06:00
|
|
|
static int gb_camera_get_max_pkt_size(struct gb_camera *gcam,
|
|
|
|
struct gb_camera_configure_streams_response *resp)
|
|
|
|
{
|
|
|
|
unsigned int max_pkt_size = 0;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < resp->num_streams; i++) {
|
|
|
|
struct gb_camera_stream_config_response *cfg = &resp->config[i];
|
|
|
|
const struct gb_camera_fmt_info *fmt_info;
|
|
|
|
unsigned int pkt_size;
|
|
|
|
|
|
|
|
fmt_info = gb_camera_get_format_info(cfg->format);
|
|
|
|
if (!fmt_info) {
|
|
|
|
gcam_err(gcam, "unsupported greybus image format: %d\n",
|
|
|
|
cfg->format);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fmt_info->bpp == 0) {
|
|
|
|
pkt_size = le32_to_cpu(cfg->max_pkt_size);
|
|
|
|
|
|
|
|
if (pkt_size == 0) {
|
|
|
|
gcam_err(gcam,
|
|
|
|
"Stream %u: invalid zero maximum packet size\n",
|
|
|
|
i);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pkt_size = le16_to_cpu(cfg->width) * fmt_info->bpp / 8;
|
|
|
|
|
|
|
|
if (pkt_size != le32_to_cpu(cfg->max_pkt_size)) {
|
|
|
|
gcam_err(gcam,
|
|
|
|
"Stream %u: maximum packet size mismatch (%u/%u)\n",
|
|
|
|
i, pkt_size, cfg->max_pkt_size);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
max_pkt_size = max(pkt_size, max_pkt_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return max_pkt_size;
|
|
|
|
}
|
|
|
|
|
2016-07-15 03:03:44 -06:00
|
|
|
/*
|
|
|
|
* Validate the stream configuration response verifying padding is correctly
|
|
|
|
* set and the returned number of streams is supported
|
|
|
|
*/
|
2016-08-12 02:05:41 -06:00
|
|
|
static const int gb_camera_configure_streams_validate_response(
|
2016-07-15 03:03:44 -06:00
|
|
|
struct gb_camera *gcam,
|
2016-08-12 02:05:41 -06:00
|
|
|
struct gb_camera_configure_streams_response *resp,
|
2016-07-15 03:03:44 -06:00
|
|
|
unsigned int nstreams)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* Validate the returned response structure */
|
2016-08-12 02:05:41 -06:00
|
|
|
if (resp->padding[0] || resp->padding[1]) {
|
2016-07-15 03:03:44 -06:00
|
|
|
gcam_err(gcam, "response padding != 0\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2016-08-12 02:05:41 -06:00
|
|
|
if (resp->num_streams > nstreams) {
|
2016-07-15 03:03:44 -06:00
|
|
|
gcam_err(gcam, "got #streams %u > request %u\n",
|
|
|
|
resp->num_streams, nstreams);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2016-08-12 02:05:41 -06:00
|
|
|
for (i = 0; i < resp->num_streams; i++) {
|
2016-07-15 03:03:44 -06:00
|
|
|
struct gb_camera_stream_config_response *cfg = &resp->config[i];
|
2016-10-12 11:11:31 -06:00
|
|
|
|
2016-07-15 03:03:44 -06:00
|
|
|
if (cfg->padding) {
|
|
|
|
gcam_err(gcam, "stream #%u padding != 0\n", i);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-14 18:18:06 -07:00
|
|
|
/* -----------------------------------------------------------------------------
|
2016-06-14 06:59:23 -06:00
|
|
|
* Hardware Configuration
|
2015-12-14 18:18:06 -07:00
|
|
|
*/
|
|
|
|
|
2016-02-13 17:33:04 -07:00
|
|
|
static int gb_camera_set_intf_power_mode(struct gb_camera *gcam, u8 intf_id,
|
|
|
|
bool hs)
|
|
|
|
{
|
|
|
|
struct gb_svc *svc = gcam->connection->hd->svc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (hs)
|
|
|
|
ret = gb_svc_intf_set_power_mode(svc, intf_id,
|
|
|
|
GB_SVC_UNIPRO_HS_SERIES_A,
|
|
|
|
GB_SVC_UNIPRO_FAST_MODE, 2, 2,
|
2016-06-03 09:24:44 -06:00
|
|
|
GB_SVC_SMALL_AMPLITUDE,
|
|
|
|
GB_SVC_NO_DE_EMPHASIS,
|
2016-02-13 17:33:04 -07:00
|
|
|
GB_SVC_UNIPRO_FAST_MODE, 2, 2,
|
|
|
|
GB_SVC_PWRM_RXTERMINATION |
|
2016-06-03 09:24:44 -06:00
|
|
|
GB_SVC_PWRM_TXTERMINATION, 0,
|
|
|
|
NULL, NULL);
|
2016-02-13 17:33:04 -07:00
|
|
|
else
|
|
|
|
ret = gb_svc_intf_set_power_mode(svc, intf_id,
|
|
|
|
GB_SVC_UNIPRO_HS_SERIES_A,
|
|
|
|
GB_SVC_UNIPRO_SLOW_AUTO_MODE,
|
greybus: svc: reconfig APBridgeA-Switch link to handle required load
SW-4894, SW-4389, and share a common root cause, namely that
the power-on reset configuration of the APBridgeA-Switch link of PWM
Gear 1, 1 Lane, Slow Auto, is insufficient to handle some required
traffic loads, such as 3 audio streams plus boot-over-UniPro or 4 audio
streams.
The correct long-term solution is to implement a UniPro Power Mode
Manager as in that considers the demands placed on the network,
and adjusts power modes accordingly.
The present commit implements a short-term, brute-force hack to allow
continued system testing:
- Upon receiving an SVC HELLO request, schedule deferred work to
reconfigure the APB1-Switch link to PWM G2, 1 lane, Slow Auto
- When the Camera driver transitions a White Camera module from active to
inactive, return the APB1-Switch link to PWM G2, 1 lane, Slow Auto
The Camera driver already steps up the APBridgeA-Camera link speed while a
camera module is active, which affords sufficient margin for simultaneous
audio and hotplug activity, and the Camera driver already steps down the
link speed thereafter: the change made by the present patch is simply to
tweak the stepped-down power mode to match the new baseline configuration.
Signed-off-by: Mitchell Tasman <tasman@leaflabs.com>
Tested-by: Mark Greer <mgreer@animalcreek.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
2016-05-04 15:30:23 -06:00
|
|
|
2, 1,
|
2016-06-03 09:24:44 -06:00
|
|
|
GB_SVC_SMALL_AMPLITUDE,
|
|
|
|
GB_SVC_NO_DE_EMPHASIS,
|
2016-02-13 17:33:04 -07:00
|
|
|
GB_SVC_UNIPRO_SLOW_AUTO_MODE,
|
greybus: svc: reconfig APBridgeA-Switch link to handle required load
SW-4894, SW-4389, and share a common root cause, namely that
the power-on reset configuration of the APBridgeA-Switch link of PWM
Gear 1, 1 Lane, Slow Auto, is insufficient to handle some required
traffic loads, such as 3 audio streams plus boot-over-UniPro or 4 audio
streams.
The correct long-term solution is to implement a UniPro Power Mode
Manager as in that considers the demands placed on the network,
and adjusts power modes accordingly.
The present commit implements a short-term, brute-force hack to allow
continued system testing:
- Upon receiving an SVC HELLO request, schedule deferred work to
reconfigure the APB1-Switch link to PWM G2, 1 lane, Slow Auto
- When the Camera driver transitions a White Camera module from active to
inactive, return the APB1-Switch link to PWM G2, 1 lane, Slow Auto
The Camera driver already steps up the APBridgeA-Camera link speed while a
camera module is active, which affords sufficient margin for simultaneous
audio and hotplug activity, and the Camera driver already steps down the
link speed thereafter: the change made by the present patch is simply to
tweak the stepped-down power mode to match the new baseline configuration.
Signed-off-by: Mitchell Tasman <tasman@leaflabs.com>
Tested-by: Mark Greer <mgreer@animalcreek.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
2016-05-04 15:30:23 -06:00
|
|
|
2, 1,
|
2016-06-03 09:24:44 -06:00
|
|
|
0, 0,
|
|
|
|
NULL, NULL);
|
2016-02-13 17:33:04 -07:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_camera_set_power_mode(struct gb_camera *gcam, bool hs)
|
|
|
|
{
|
|
|
|
struct gb_interface *intf = gcam->connection->intf;
|
|
|
|
struct gb_svc *svc = gcam->connection->hd->svc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gb_camera_set_intf_power_mode(gcam, intf->interface_id, hs);
|
|
|
|
if (ret < 0) {
|
|
|
|
gcam_err(gcam, "failed to set module interface to %s (%d)\n",
|
|
|
|
hs ? "HS" : "PWM", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gb_camera_set_intf_power_mode(gcam, svc->ap_intf_id, hs);
|
|
|
|
if (ret < 0) {
|
2016-02-13 17:33:05 -07:00
|
|
|
gb_camera_set_intf_power_mode(gcam, intf->interface_id, !hs);
|
2016-02-13 17:33:04 -07:00
|
|
|
gcam_err(gcam, "failed to set AP interface to %s (%d)\n",
|
|
|
|
hs ? "HS" : "PWM", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-14 06:59:23 -06:00
|
|
|
struct ap_csi_config_request {
|
|
|
|
__u8 csi_id;
|
|
|
|
__u8 flags;
|
|
|
|
#define GB_CAMERA_CSI_FLAG_CLOCK_CONTINUOUS 0x01
|
|
|
|
__u8 num_lanes;
|
|
|
|
__u8 padding;
|
2016-07-15 03:03:45 -06:00
|
|
|
__le32 csi_clk_freq;
|
|
|
|
__le32 max_pkt_size;
|
2016-06-14 06:59:23 -06:00
|
|
|
} __packed;
|
|
|
|
|
2016-07-15 03:03:43 -06:00
|
|
|
/*
|
|
|
|
* TODO: Compute the number of lanes dynamically based on bandwidth
|
|
|
|
* requirements.
|
|
|
|
*/
|
|
|
|
#define GB_CAMERA_CSI_NUM_DATA_LANES 4
|
2016-07-15 03:03:45 -06:00
|
|
|
|
|
|
|
#define GB_CAMERA_CSI_CLK_FREQ_MAX 999000000U
|
|
|
|
#define GB_CAMERA_CSI_CLK_FREQ_MIN 100000000U
|
|
|
|
#define GB_CAMERA_CSI_CLK_FREQ_MARGIN 150000000U
|
2016-07-15 03:03:43 -06:00
|
|
|
|
2016-06-14 06:59:23 -06:00
|
|
|
static int gb_camera_setup_data_connection(struct gb_camera *gcam,
|
2016-07-15 03:03:45 -06:00
|
|
|
struct gb_camera_configure_streams_response *resp,
|
2016-06-14 06:59:23 -06:00
|
|
|
struct gb_camera_csi_params *csi_params)
|
|
|
|
{
|
|
|
|
struct ap_csi_config_request csi_cfg;
|
2016-06-14 06:59:25 -06:00
|
|
|
struct gb_connection *conn;
|
2016-07-15 03:03:45 -06:00
|
|
|
unsigned int clk_freq;
|
2016-06-14 06:59:23 -06:00
|
|
|
int ret;
|
|
|
|
|
2016-06-14 06:59:25 -06:00
|
|
|
/*
|
|
|
|
* Create the data connection between the camera module data CPort and
|
|
|
|
* APB CDSI1. The CDSI1 CPort ID is hardcoded by the ES2 bridge.
|
|
|
|
*/
|
|
|
|
conn = gb_connection_create_offloaded(gcam->bundle, gcam->data_cport_id,
|
|
|
|
GB_CONNECTION_FLAG_NO_FLOWCTRL |
|
|
|
|
GB_CONNECTION_FLAG_CDSI1);
|
|
|
|
if (IS_ERR(conn))
|
|
|
|
return PTR_ERR(conn);
|
|
|
|
|
|
|
|
gcam->data_connection = conn;
|
|
|
|
gb_connection_set_data(conn, gcam);
|
|
|
|
|
|
|
|
ret = gb_connection_enable(conn);
|
|
|
|
if (ret)
|
|
|
|
goto error_conn_destroy;
|
|
|
|
|
2016-06-14 06:59:23 -06:00
|
|
|
/* Set the UniPro link to high speed mode. */
|
|
|
|
ret = gb_camera_set_power_mode(gcam, true);
|
|
|
|
if (ret < 0)
|
2016-06-14 06:59:25 -06:00
|
|
|
goto error_conn_disable;
|
2016-06-14 06:59:23 -06:00
|
|
|
|
|
|
|
/*
|
2016-07-15 03:03:45 -06:00
|
|
|
* Configure the APB-A CSI-2 transmitter.
|
2016-06-14 06:59:23 -06:00
|
|
|
*
|
2016-07-15 03:03:45 -06:00
|
|
|
* Hardcode the number of lanes to 4 and compute the bus clock frequency
|
|
|
|
* based on the module bandwidth requirements with a safety margin.
|
2016-06-14 06:59:23 -06:00
|
|
|
*/
|
|
|
|
memset(&csi_cfg, 0, sizeof(csi_cfg));
|
|
|
|
csi_cfg.csi_id = 1;
|
|
|
|
csi_cfg.flags = 0;
|
2016-07-15 03:03:43 -06:00
|
|
|
csi_cfg.num_lanes = GB_CAMERA_CSI_NUM_DATA_LANES;
|
2016-07-15 03:03:45 -06:00
|
|
|
|
|
|
|
clk_freq = resp->data_rate / 2 / GB_CAMERA_CSI_NUM_DATA_LANES;
|
|
|
|
clk_freq = clamp(clk_freq + GB_CAMERA_CSI_CLK_FREQ_MARGIN,
|
|
|
|
GB_CAMERA_CSI_CLK_FREQ_MIN,
|
|
|
|
GB_CAMERA_CSI_CLK_FREQ_MAX);
|
|
|
|
csi_cfg.csi_clk_freq = clk_freq;
|
|
|
|
|
|
|
|
ret = gb_camera_get_max_pkt_size(gcam, resp);
|
|
|
|
if (ret < 0) {
|
|
|
|
ret = -EIO;
|
|
|
|
goto error_power;
|
|
|
|
}
|
|
|
|
csi_cfg.max_pkt_size = ret;
|
2016-06-14 06:59:23 -06:00
|
|
|
|
|
|
|
ret = gb_hd_output(gcam->connection->hd, &csi_cfg,
|
|
|
|
sizeof(csi_cfg),
|
|
|
|
GB_APB_REQUEST_CSI_TX_CONTROL, false);
|
|
|
|
if (ret < 0) {
|
|
|
|
gcam_err(gcam, "failed to start the CSI transmitter\n");
|
2016-06-14 06:59:25 -06:00
|
|
|
goto error_power;
|
2016-06-14 06:59:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (csi_params) {
|
2016-07-15 03:03:45 -06:00
|
|
|
csi_params->clk_freq = csi_cfg.csi_clk_freq;
|
2016-06-14 06:59:23 -06:00
|
|
|
csi_params->num_lanes = csi_cfg.num_lanes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2016-06-14 06:59:25 -06:00
|
|
|
|
|
|
|
error_power:
|
|
|
|
gb_camera_set_power_mode(gcam, false);
|
|
|
|
error_conn_disable:
|
|
|
|
gb_connection_disable(gcam->data_connection);
|
|
|
|
error_conn_destroy:
|
|
|
|
gb_connection_destroy(gcam->data_connection);
|
|
|
|
gcam->data_connection = NULL;
|
|
|
|
return ret;
|
2016-06-14 06:59:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_camera_teardown_data_connection(struct gb_camera *gcam)
|
|
|
|
{
|
|
|
|
struct ap_csi_config_request csi_cfg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Stop the APB1 CSI transmitter. */
|
|
|
|
memset(&csi_cfg, 0, sizeof(csi_cfg));
|
|
|
|
csi_cfg.csi_id = 1;
|
|
|
|
|
|
|
|
ret = gb_hd_output(gcam->connection->hd, &csi_cfg,
|
|
|
|
sizeof(csi_cfg),
|
|
|
|
GB_APB_REQUEST_CSI_TX_CONTROL, false);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
gcam_err(gcam, "failed to stop the CSI transmitter\n");
|
|
|
|
|
|
|
|
/* Set the UniPro link to low speed mode. */
|
|
|
|
gb_camera_set_power_mode(gcam, false);
|
2016-06-14 06:59:25 -06:00
|
|
|
|
|
|
|
/* Destroy the data connection. */
|
|
|
|
gb_connection_disable(gcam->data_connection);
|
|
|
|
gb_connection_destroy(gcam->data_connection);
|
|
|
|
gcam->data_connection = NULL;
|
2016-06-14 06:59:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
|
|
* Camera Protocol Operations
|
|
|
|
*/
|
|
|
|
|
2016-03-17 04:02:02 -06:00
|
|
|
static int gb_camera_capabilities(struct gb_camera *gcam,
|
|
|
|
u8 *capabilities, size_t *size)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2016-07-14 14:13:00 -06:00
|
|
|
ret = gb_pm_runtime_get_sync(gcam->bundle);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2016-05-31 02:33:11 -06:00
|
|
|
mutex_lock(&gcam->mutex);
|
|
|
|
|
|
|
|
if (!gcam->connection) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2016-07-15 03:03:42 -06:00
|
|
|
ret = gb_camera_operation_sync_flags(gcam->connection,
|
|
|
|
GB_CAMERA_TYPE_CAPABILITIES,
|
|
|
|
GB_OPERATION_FLAG_SHORT_RESPONSE,
|
|
|
|
NULL, 0,
|
|
|
|
(void *)capabilities, size);
|
|
|
|
if (ret)
|
2016-03-17 04:02:02 -06:00
|
|
|
gcam_err(gcam, "failed to retrieve capabilities: %d\n", ret);
|
|
|
|
|
|
|
|
done:
|
2016-05-31 02:33:11 -06:00
|
|
|
mutex_unlock(&gcam->mutex);
|
2016-07-14 14:13:00 -06:00
|
|
|
|
|
|
|
gb_pm_runtime_put_autosuspend(gcam->bundle);
|
|
|
|
|
2016-03-17 04:02:02 -06:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-12-14 18:18:06 -07:00
|
|
|
static int gb_camera_configure_streams(struct gb_camera *gcam,
|
2016-01-14 16:33:55 -07:00
|
|
|
unsigned int *num_streams,
|
|
|
|
unsigned int *flags,
|
2016-04-06 06:22:45 -06:00
|
|
|
struct gb_camera_stream_config *streams,
|
|
|
|
struct gb_camera_csi_params *csi_params)
|
2015-12-14 18:18:06 -07:00
|
|
|
{
|
|
|
|
struct gb_camera_configure_streams_request *req;
|
|
|
|
struct gb_camera_configure_streams_response *resp;
|
2016-01-14 16:33:55 -07:00
|
|
|
unsigned int nstreams = *num_streams;
|
2015-12-14 18:18:06 -07:00
|
|
|
unsigned int i;
|
|
|
|
size_t req_size;
|
|
|
|
size_t resp_size;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (nstreams > GB_CAMERA_MAX_STREAMS)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
req_size = sizeof(*req) + nstreams * sizeof(req->config[0]);
|
|
|
|
resp_size = sizeof(*resp) + nstreams * sizeof(resp->config[0]);
|
|
|
|
|
|
|
|
req = kmalloc(req_size, GFP_KERNEL);
|
|
|
|
resp = kmalloc(resp_size, GFP_KERNEL);
|
2016-08-12 02:05:41 -06:00
|
|
|
if (!req || !resp) {
|
2016-05-31 02:33:11 -06:00
|
|
|
kfree(req);
|
|
|
|
kfree(resp);
|
|
|
|
return -ENOMEM;
|
2015-12-14 18:18:06 -07:00
|
|
|
}
|
|
|
|
|
2016-01-08 09:13:20 -07:00
|
|
|
req->num_streams = nstreams;
|
2016-01-14 16:33:55 -07:00
|
|
|
req->flags = *flags;
|
2015-12-14 18:18:06 -07:00
|
|
|
req->padding = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < nstreams; ++i) {
|
|
|
|
struct gb_camera_stream_config_request *cfg = &req->config[i];
|
|
|
|
|
2015-12-18 12:23:21 -07:00
|
|
|
cfg->width = cpu_to_le16(streams[i].width);
|
|
|
|
cfg->height = cpu_to_le16(streams[i].height);
|
|
|
|
cfg->format = cpu_to_le16(streams[i].format);
|
2015-12-14 18:18:06 -07:00
|
|
|
cfg->padding = 0;
|
|
|
|
}
|
|
|
|
|
2016-05-31 02:33:11 -06:00
|
|
|
mutex_lock(&gcam->mutex);
|
|
|
|
|
2016-07-14 14:13:00 -06:00
|
|
|
ret = gb_pm_runtime_get_sync(gcam->bundle);
|
|
|
|
if (ret)
|
|
|
|
goto done_skip_pm_put;
|
|
|
|
|
2016-05-31 02:33:11 -06:00
|
|
|
if (!gcam->connection) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2016-07-15 03:03:44 -06:00
|
|
|
ret = gb_camera_operation_sync_flags(gcam->connection,
|
|
|
|
GB_CAMERA_TYPE_CONFIGURE_STREAMS,
|
|
|
|
GB_OPERATION_FLAG_SHORT_RESPONSE,
|
|
|
|
req, req_size,
|
2016-08-12 02:05:41 -06:00
|
|
|
resp, &resp_size);
|
2015-12-14 18:18:06 -07:00
|
|
|
if (ret < 0)
|
2016-02-13 17:33:06 -07:00
|
|
|
goto done;
|
2015-12-14 18:18:06 -07:00
|
|
|
|
2016-08-12 02:05:41 -06:00
|
|
|
ret = gb_camera_configure_streams_validate_response(gcam, resp,
|
|
|
|
nstreams);
|
2016-07-15 03:03:44 -06:00
|
|
|
if (ret < 0)
|
2016-02-13 17:33:06 -07:00
|
|
|
goto done;
|
2015-12-14 18:18:06 -07:00
|
|
|
|
2016-06-30 08:18:00 -06:00
|
|
|
*flags = resp->flags;
|
|
|
|
*num_streams = resp->num_streams;
|
|
|
|
|
2016-06-30 08:18:00 -06:00
|
|
|
for (i = 0; i < resp->num_streams; ++i) {
|
2015-12-14 18:18:06 -07:00
|
|
|
struct gb_camera_stream_config_response *cfg = &resp->config[i];
|
|
|
|
|
2015-12-18 12:23:21 -07:00
|
|
|
streams[i].width = le16_to_cpu(cfg->width);
|
|
|
|
streams[i].height = le16_to_cpu(cfg->height);
|
|
|
|
streams[i].format = le16_to_cpu(cfg->format);
|
2015-12-14 18:18:06 -07:00
|
|
|
streams[i].vc = cfg->virtual_channel;
|
|
|
|
streams[i].dt[0] = cfg->data_type[0];
|
|
|
|
streams[i].dt[1] = cfg->data_type[1];
|
2015-12-18 12:23:21 -07:00
|
|
|
streams[i].max_size = le32_to_cpu(cfg->max_size);
|
2015-12-14 18:18:06 -07:00
|
|
|
}
|
|
|
|
|
2016-02-13 17:33:07 -07:00
|
|
|
if ((resp->flags & GB_CAMERA_CONFIGURE_STREAMS_ADJUSTED) ||
|
2016-06-30 08:18:00 -06:00
|
|
|
(req->flags & GB_CAMERA_CONFIGURE_STREAMS_TEST_ONLY))
|
2016-02-13 17:33:06 -07:00
|
|
|
goto done;
|
|
|
|
|
2016-06-14 06:59:24 -06:00
|
|
|
if (gcam->state == GB_CAMERA_STATE_CONFIGURED) {
|
|
|
|
gb_camera_teardown_data_connection(gcam);
|
|
|
|
gcam->state = GB_CAMERA_STATE_UNCONFIGURED;
|
2016-07-14 14:13:00 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* When unconfiguring streams release the PM runtime reference
|
|
|
|
* that was acquired when streams were configured. The bundle
|
|
|
|
* won't be suspended until the PM runtime reference acquired at
|
|
|
|
* the beginning of this function gets released right before
|
|
|
|
* returning.
|
|
|
|
*/
|
|
|
|
gb_pm_runtime_put_noidle(gcam->bundle);
|
2016-06-14 06:59:24 -06:00
|
|
|
}
|
|
|
|
|
2016-07-15 03:03:45 -06:00
|
|
|
if (resp->num_streams == 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure the bundle won't be suspended until streams get
|
|
|
|
* unconfigured after the stream is configured successfully
|
|
|
|
*/
|
|
|
|
gb_pm_runtime_get_noresume(gcam->bundle);
|
2016-06-14 06:59:24 -06:00
|
|
|
|
2016-07-15 03:03:45 -06:00
|
|
|
/* Setup CSI-2 connection from APB-A to AP */
|
|
|
|
ret = gb_camera_setup_data_connection(gcam, resp, csi_params);
|
|
|
|
if (ret < 0) {
|
|
|
|
memset(req, 0, sizeof(*req));
|
|
|
|
gb_operation_sync(gcam->connection,
|
|
|
|
GB_CAMERA_TYPE_CONFIGURE_STREAMS,
|
|
|
|
req, sizeof(*req),
|
|
|
|
resp, sizeof(*resp));
|
|
|
|
*flags = 0;
|
|
|
|
*num_streams = 0;
|
|
|
|
gb_pm_runtime_put_noidle(gcam->bundle);
|
|
|
|
goto done;
|
2015-12-18 12:23:25 -07:00
|
|
|
}
|
|
|
|
|
2016-07-15 03:03:45 -06:00
|
|
|
gcam->state = GB_CAMERA_STATE_CONFIGURED;
|
|
|
|
|
2015-12-14 18:18:06 -07:00
|
|
|
done:
|
2016-07-14 14:13:00 -06:00
|
|
|
gb_pm_runtime_put_autosuspend(gcam->bundle);
|
|
|
|
|
|
|
|
done_skip_pm_put:
|
2016-05-31 02:33:11 -06:00
|
|
|
mutex_unlock(&gcam->mutex);
|
2015-12-14 18:18:06 -07:00
|
|
|
kfree(req);
|
|
|
|
kfree(resp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_camera_capture(struct gb_camera *gcam, u32 request_id,
|
|
|
|
unsigned int streams, unsigned int num_frames,
|
|
|
|
size_t settings_size, const void *settings)
|
|
|
|
{
|
|
|
|
struct gb_camera_capture_request *req;
|
|
|
|
size_t req_size;
|
2015-12-30 03:08:01 -07:00
|
|
|
int ret;
|
2015-12-14 18:18:06 -07:00
|
|
|
|
|
|
|
if (settings_size > GB_CAMERA_MAX_SETTINGS_SIZE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
req_size = sizeof(*req) + settings_size;
|
|
|
|
req = kmalloc(req_size, GFP_KERNEL);
|
|
|
|
if (!req)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2015-12-18 12:23:21 -07:00
|
|
|
req->request_id = cpu_to_le32(request_id);
|
2015-12-14 18:18:06 -07:00
|
|
|
req->streams = streams;
|
|
|
|
req->padding = 0;
|
2015-12-18 12:23:21 -07:00
|
|
|
req->num_frames = cpu_to_le16(num_frames);
|
2015-12-14 18:18:06 -07:00
|
|
|
memcpy(req->settings, settings, settings_size);
|
|
|
|
|
2016-05-31 02:33:11 -06:00
|
|
|
mutex_lock(&gcam->mutex);
|
|
|
|
|
|
|
|
if (!gcam->connection) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2015-12-30 03:08:01 -07:00
|
|
|
ret = gb_operation_sync(gcam->connection, GB_CAMERA_TYPE_CAPTURE,
|
2016-05-31 02:33:11 -06:00
|
|
|
req, req_size, NULL, 0);
|
|
|
|
done:
|
|
|
|
mutex_unlock(&gcam->mutex);
|
2015-12-30 03:08:01 -07:00
|
|
|
|
|
|
|
kfree(req);
|
|
|
|
|
|
|
|
return ret;
|
2015-12-14 18:18:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_camera_flush(struct gb_camera *gcam, u32 *request_id)
|
|
|
|
{
|
|
|
|
struct gb_camera_flush_response resp;
|
|
|
|
int ret;
|
|
|
|
|
2016-05-31 02:33:11 -06:00
|
|
|
mutex_lock(&gcam->mutex);
|
|
|
|
|
|
|
|
if (!gcam->connection) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2015-12-14 18:18:06 -07:00
|
|
|
ret = gb_operation_sync(gcam->connection, GB_CAMERA_TYPE_FLUSH, NULL, 0,
|
|
|
|
&resp, sizeof(resp));
|
2016-05-31 02:33:11 -06:00
|
|
|
|
2015-12-14 18:18:06 -07:00
|
|
|
if (ret < 0)
|
2016-05-31 02:33:11 -06:00
|
|
|
goto done;
|
2015-12-14 18:18:06 -07:00
|
|
|
|
|
|
|
if (request_id)
|
2015-12-18 12:23:21 -07:00
|
|
|
*request_id = le32_to_cpu(resp.request_id);
|
2015-12-14 18:18:06 -07:00
|
|
|
|
2016-05-31 02:33:11 -06:00
|
|
|
done:
|
|
|
|
mutex_unlock(&gcam->mutex);
|
|
|
|
|
|
|
|
return ret;
|
2015-12-14 18:18:06 -07:00
|
|
|
}
|
|
|
|
|
2016-05-20 05:33:35 -06:00
|
|
|
static int gb_camera_request_handler(struct gb_operation *op)
|
2015-12-14 18:18:06 -07:00
|
|
|
{
|
2016-03-22 12:30:35 -06:00
|
|
|
struct gb_camera *gcam = gb_connection_get_data(op->connection);
|
2015-12-14 18:18:06 -07:00
|
|
|
struct gb_camera_metadata_request *payload;
|
|
|
|
struct gb_message *request;
|
|
|
|
|
2016-05-20 05:33:35 -06:00
|
|
|
if (op->type != GB_CAMERA_TYPE_METADATA) {
|
|
|
|
gcam_err(gcam, "Unsupported unsolicited event: %u\n", op->type);
|
2015-12-14 18:18:06 -07:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
request = op->request;
|
|
|
|
|
|
|
|
if (request->payload_size < sizeof(*payload)) {
|
|
|
|
gcam_err(gcam, "Wrong event size received (%zu < %zu)\n",
|
|
|
|
request->payload_size, sizeof(*payload));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
payload = request->payload;
|
|
|
|
|
|
|
|
gcam_dbg(gcam, "received metadata for request %u, frame %u, stream %u\n",
|
|
|
|
payload->request_id, payload->frame_number, payload->stream);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-13 12:52:38 -07:00
|
|
|
/* -----------------------------------------------------------------------------
|
2016-08-10 01:08:19 -06:00
|
|
|
* Interface with HOST gmp camera.
|
2016-01-13 12:52:38 -07:00
|
|
|
*/
|
|
|
|
static unsigned int gb_camera_mbus_to_gb(enum v4l2_mbus_pixelcode mbus_code)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2016-07-15 03:03:41 -06:00
|
|
|
for (i = 0; i < ARRAY_SIZE(gb_fmt_info); i++) {
|
|
|
|
if (gb_fmt_info[i].mbus_code == mbus_code)
|
|
|
|
return gb_fmt_info[i].gb_format;
|
2016-01-13 12:52:38 -07:00
|
|
|
}
|
2016-07-15 03:03:41 -06:00
|
|
|
return gb_fmt_info[0].gb_format;
|
2016-01-13 12:52:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static enum v4l2_mbus_pixelcode gb_camera_gb_to_mbus(u16 gb_fmt)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2016-07-15 03:03:41 -06:00
|
|
|
for (i = 0; i < ARRAY_SIZE(gb_fmt_info); i++) {
|
|
|
|
if (gb_fmt_info[i].gb_format == gb_fmt)
|
|
|
|
return gb_fmt_info[i].mbus_code;
|
2016-01-13 12:52:38 -07:00
|
|
|
}
|
2016-07-15 03:03:41 -06:00
|
|
|
return gb_fmt_info[0].mbus_code;
|
2016-01-13 12:52:38 -07:00
|
|
|
}
|
|
|
|
|
2016-03-17 04:02:03 -06:00
|
|
|
static ssize_t gb_camera_op_capabilities(void *priv, char *data, size_t len)
|
|
|
|
{
|
|
|
|
struct gb_camera *gcam = priv;
|
|
|
|
size_t capabilities_len = len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gb_camera_capabilities(gcam, data, &capabilities_len);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return capabilities_len;
|
|
|
|
}
|
|
|
|
|
2016-01-17 10:52:21 -07:00
|
|
|
static int gb_camera_op_configure_streams(void *priv, unsigned int *nstreams,
|
2016-04-06 06:22:45 -06:00
|
|
|
unsigned int *flags, struct gb_camera_stream *streams,
|
|
|
|
struct gb_camera_csi_params *csi_params)
|
2016-01-13 12:52:38 -07:00
|
|
|
{
|
|
|
|
struct gb_camera *gcam = priv;
|
|
|
|
struct gb_camera_stream_config *gb_streams;
|
2016-01-17 10:52:21 -07:00
|
|
|
unsigned int gb_flags = 0;
|
|
|
|
unsigned int gb_nstreams = *nstreams;
|
2016-01-13 12:52:38 -07:00
|
|
|
unsigned int i;
|
|
|
|
int ret;
|
|
|
|
|
2016-01-17 10:52:21 -07:00
|
|
|
if (gb_nstreams > GB_CAMERA_MAX_STREAMS)
|
2016-01-13 12:52:38 -07:00
|
|
|
return -EINVAL;
|
|
|
|
|
2016-10-12 11:14:14 -06:00
|
|
|
gb_streams = kcalloc(gb_nstreams, sizeof(*gb_streams), GFP_KERNEL);
|
2016-01-13 12:52:38 -07:00
|
|
|
if (!gb_streams)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2016-01-17 10:52:21 -07:00
|
|
|
for (i = 0; i < gb_nstreams; i++) {
|
2016-01-13 12:52:38 -07:00
|
|
|
gb_streams[i].width = streams[i].width;
|
|
|
|
gb_streams[i].height = streams[i].height;
|
|
|
|
gb_streams[i].format =
|
|
|
|
gb_camera_mbus_to_gb(streams[i].pixel_code);
|
|
|
|
}
|
|
|
|
|
2016-01-17 10:52:21 -07:00
|
|
|
if (*flags & GB_CAMERA_IN_FLAG_TEST)
|
|
|
|
gb_flags |= GB_CAMERA_CONFIGURE_STREAMS_TEST_ONLY;
|
|
|
|
|
|
|
|
ret = gb_camera_configure_streams(gcam, &gb_nstreams,
|
2016-04-06 06:22:45 -06:00
|
|
|
&gb_flags, gb_streams, csi_params);
|
2016-01-13 12:52:38 -07:00
|
|
|
if (ret < 0)
|
|
|
|
goto done;
|
2016-01-17 10:52:21 -07:00
|
|
|
if (gb_nstreams > *nstreams) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
*flags = 0;
|
|
|
|
if (gb_flags & GB_CAMERA_CONFIGURE_STREAMS_ADJUSTED)
|
|
|
|
*flags |= GB_CAMERA_OUT_FLAG_ADJUSTED;
|
2016-01-13 12:52:38 -07:00
|
|
|
|
2016-01-17 10:52:21 -07:00
|
|
|
for (i = 0; i < gb_nstreams; i++) {
|
2016-01-13 12:52:38 -07:00
|
|
|
streams[i].width = gb_streams[i].width;
|
|
|
|
streams[i].height = gb_streams[i].height;
|
|
|
|
streams[i].vc = gb_streams[i].vc;
|
|
|
|
streams[i].dt[0] = gb_streams[i].dt[0];
|
|
|
|
streams[i].dt[1] = gb_streams[i].dt[1];
|
|
|
|
streams[i].max_size = gb_streams[i].max_size;
|
|
|
|
streams[i].pixel_code =
|
|
|
|
gb_camera_gb_to_mbus(gb_streams[i].format);
|
|
|
|
}
|
2016-01-17 10:52:21 -07:00
|
|
|
*nstreams = gb_nstreams;
|
2016-01-13 12:52:38 -07:00
|
|
|
|
|
|
|
done:
|
|
|
|
kfree(gb_streams);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_camera_op_capture(void *priv, u32 request_id,
|
|
|
|
unsigned int streams, unsigned int num_frames,
|
|
|
|
size_t settings_size, const void *settings)
|
|
|
|
{
|
2016-03-31 05:12:45 -06:00
|
|
|
struct gb_camera *gcam = priv;
|
|
|
|
|
|
|
|
return gb_camera_capture(gcam, request_id, streams, num_frames,
|
2016-01-13 12:52:38 -07:00
|
|
|
settings_size, settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_camera_op_flush(void *priv, u32 *request_id)
|
|
|
|
{
|
2016-03-31 05:12:45 -06:00
|
|
|
struct gb_camera *gcam = priv;
|
|
|
|
|
|
|
|
return gb_camera_flush(gcam, request_id);
|
2016-01-13 12:52:38 -07:00
|
|
|
}
|
|
|
|
|
2016-03-31 05:12:45 -06:00
|
|
|
static const struct gb_camera_ops gb_cam_ops = {
|
|
|
|
.capabilities = gb_camera_op_capabilities,
|
|
|
|
.configure_streams = gb_camera_op_configure_streams,
|
|
|
|
.capture = gb_camera_op_capture,
|
|
|
|
.flush = gb_camera_op_flush,
|
|
|
|
};
|
|
|
|
|
2015-12-14 18:18:06 -07:00
|
|
|
/* -----------------------------------------------------------------------------
|
|
|
|
* DebugFS
|
|
|
|
*/
|
2016-03-17 04:02:02 -06:00
|
|
|
|
2015-12-14 18:18:06 -07:00
|
|
|
static ssize_t gb_camera_debugfs_capabilities(struct gb_camera *gcam,
|
|
|
|
char *buf, size_t len)
|
|
|
|
{
|
2016-03-17 04:02:02 -06:00
|
|
|
struct gb_camera_debugfs_buffer *buffer =
|
|
|
|
&gcam->debugfs.buffers[GB_CAMERA_DEBUGFS_BUFFER_CAPABILITIES];
|
|
|
|
size_t size = 1024;
|
|
|
|
unsigned int i;
|
|
|
|
u8 *caps;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
caps = kmalloc(size, GFP_KERNEL);
|
|
|
|
if (!caps)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ret = gb_camera_capabilities(gcam, caps, &size);
|
|
|
|
if (ret < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hex_dump_to_buffer() doesn't return the number of bytes dumped prior
|
|
|
|
* to v4.0, we need our own implementation :-(
|
|
|
|
*/
|
|
|
|
buffer->length = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < size; i += 16) {
|
|
|
|
unsigned int nbytes = min_t(unsigned int, size - i, 16);
|
|
|
|
|
|
|
|
buffer->length += sprintf(buffer->data + buffer->length,
|
|
|
|
"%*ph\n", nbytes, caps + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
kfree(caps);
|
|
|
|
return ret;
|
2015-12-14 18:18:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t gb_camera_debugfs_configure_streams(struct gb_camera *gcam,
|
|
|
|
char *buf, size_t len)
|
|
|
|
{
|
|
|
|
struct gb_camera_debugfs_buffer *buffer =
|
|
|
|
&gcam->debugfs.buffers[GB_CAMERA_DEBUGFS_BUFFER_STREAMS];
|
|
|
|
struct gb_camera_stream_config *streams;
|
|
|
|
unsigned int nstreams;
|
2016-01-08 09:13:20 -07:00
|
|
|
unsigned int flags;
|
2015-12-14 18:18:06 -07:00
|
|
|
unsigned int i;
|
|
|
|
char *token;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Retrieve number of streams to configure */
|
2016-01-08 09:13:20 -07:00
|
|
|
token = strsep(&buf, ";");
|
2018-01-08 09:50:15 -07:00
|
|
|
if (!token)
|
2015-12-14 18:18:06 -07:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ret = kstrtouint(token, 10, &nstreams);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (nstreams > GB_CAMERA_MAX_STREAMS)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2016-01-08 09:13:20 -07:00
|
|
|
token = strsep(&buf, ";");
|
2018-01-08 09:50:15 -07:00
|
|
|
if (!token)
|
2016-01-08 09:13:20 -07:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ret = kstrtouint(token, 10, &flags);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2015-12-14 18:18:06 -07:00
|
|
|
/* For each stream to configure parse width, height and format */
|
2016-10-12 11:14:14 -06:00
|
|
|
streams = kcalloc(nstreams, sizeof(*streams), GFP_KERNEL);
|
2015-12-14 18:18:06 -07:00
|
|
|
if (!streams)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = 0; i < nstreams; ++i) {
|
|
|
|
struct gb_camera_stream_config *stream = &streams[i];
|
|
|
|
|
|
|
|
/* width */
|
|
|
|
token = strsep(&buf, ";");
|
2018-01-08 09:50:15 -07:00
|
|
|
if (!token) {
|
2015-12-14 18:18:06 -07:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
ret = kstrtouint(token, 10, &stream->width);
|
|
|
|
if (ret < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* height */
|
|
|
|
token = strsep(&buf, ";");
|
2018-01-08 09:50:15 -07:00
|
|
|
if (!token)
|
2015-12-14 18:18:06 -07:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
ret = kstrtouint(token, 10, &stream->height);
|
|
|
|
if (ret < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Image format code */
|
|
|
|
token = strsep(&buf, ";");
|
2018-01-08 09:50:15 -07:00
|
|
|
if (!token)
|
2015-12-14 18:18:06 -07:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
ret = kstrtouint(token, 16, &stream->format);
|
|
|
|
if (ret < 0)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2016-04-06 06:22:45 -06:00
|
|
|
ret = gb_camera_configure_streams(gcam, &nstreams, &flags, streams,
|
|
|
|
NULL);
|
2015-12-14 18:18:06 -07:00
|
|
|
if (ret < 0)
|
|
|
|
goto done;
|
|
|
|
|
2016-01-14 16:33:55 -07:00
|
|
|
buffer->length = sprintf(buffer->data, "%u;%u;", nstreams, flags);
|
2015-12-14 18:18:06 -07:00
|
|
|
|
|
|
|
for (i = 0; i < nstreams; ++i) {
|
|
|
|
struct gb_camera_stream_config *stream = &streams[i];
|
|
|
|
|
|
|
|
buffer->length += sprintf(buffer->data + buffer->length,
|
|
|
|
"%u;%u;%u;%u;%u;%u;%u;",
|
|
|
|
stream->width, stream->height,
|
|
|
|
stream->format, stream->vc,
|
|
|
|
stream->dt[0], stream->dt[1],
|
|
|
|
stream->max_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = len;
|
|
|
|
|
|
|
|
done:
|
|
|
|
kfree(streams);
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
static ssize_t gb_camera_debugfs_capture(struct gb_camera *gcam,
|
|
|
|
char *buf, size_t len)
|
|
|
|
{
|
|
|
|
unsigned int request_id;
|
|
|
|
unsigned int streams_mask;
|
|
|
|
unsigned int num_frames;
|
|
|
|
char *token;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Request id */
|
|
|
|
token = strsep(&buf, ";");
|
2018-01-08 09:50:15 -07:00
|
|
|
if (!token)
|
2015-12-14 18:18:06 -07:00
|
|
|
return -EINVAL;
|
|
|
|
ret = kstrtouint(token, 10, &request_id);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Stream mask */
|
|
|
|
token = strsep(&buf, ";");
|
2018-01-08 09:50:15 -07:00
|
|
|
if (!token)
|
2015-12-14 18:18:06 -07:00
|
|
|
return -EINVAL;
|
|
|
|
ret = kstrtouint(token, 16, &streams_mask);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* number of frames */
|
|
|
|
token = strsep(&buf, ";");
|
2018-01-08 09:50:15 -07:00
|
|
|
if (!token)
|
2015-12-14 18:18:06 -07:00
|
|
|
return -EINVAL;
|
|
|
|
ret = kstrtouint(token, 10, &num_frames);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = gb_camera_capture(gcam, request_id, streams_mask, num_frames, 0,
|
|
|
|
NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t gb_camera_debugfs_flush(struct gb_camera *gcam,
|
|
|
|
char *buf, size_t len)
|
|
|
|
{
|
|
|
|
struct gb_camera_debugfs_buffer *buffer =
|
|
|
|
&gcam->debugfs.buffers[GB_CAMERA_DEBUGFS_BUFFER_FLUSH];
|
|
|
|
unsigned int req_id;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gb_camera_flush(gcam, &req_id);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
buffer->length = sprintf(buffer->data, "%u", req_id);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct gb_camera_debugfs_entry {
|
|
|
|
const char *name;
|
|
|
|
unsigned int mask;
|
|
|
|
unsigned int buffer;
|
|
|
|
ssize_t (*execute)(struct gb_camera *gcam, char *buf, size_t len);
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct gb_camera_debugfs_entry gb_camera_debugfs_entries[] = {
|
|
|
|
{
|
|
|
|
.name = "capabilities",
|
2017-01-11 21:59:40 -07:00
|
|
|
.mask = S_IFREG | 0444,
|
2015-12-14 18:18:06 -07:00
|
|
|
.buffer = GB_CAMERA_DEBUGFS_BUFFER_CAPABILITIES,
|
|
|
|
.execute = gb_camera_debugfs_capabilities,
|
|
|
|
}, {
|
|
|
|
.name = "configure_streams",
|
2017-01-11 21:59:40 -07:00
|
|
|
.mask = S_IFREG | 0666,
|
2015-12-14 18:18:06 -07:00
|
|
|
.buffer = GB_CAMERA_DEBUGFS_BUFFER_STREAMS,
|
|
|
|
.execute = gb_camera_debugfs_configure_streams,
|
|
|
|
}, {
|
|
|
|
.name = "capture",
|
2017-01-11 21:59:40 -07:00
|
|
|
.mask = S_IFREG | 0666,
|
2015-12-14 18:18:06 -07:00
|
|
|
.buffer = GB_CAMERA_DEBUGFS_BUFFER_CAPTURE,
|
|
|
|
.execute = gb_camera_debugfs_capture,
|
|
|
|
}, {
|
|
|
|
.name = "flush",
|
2017-01-11 21:59:40 -07:00
|
|
|
.mask = S_IFREG | 0666,
|
2015-12-14 18:18:06 -07:00
|
|
|
.buffer = GB_CAMERA_DEBUGFS_BUFFER_FLUSH,
|
|
|
|
.execute = gb_camera_debugfs_flush,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static ssize_t gb_camera_debugfs_read(struct file *file, char __user *buf,
|
|
|
|
size_t len, loff_t *offset)
|
|
|
|
{
|
|
|
|
const struct gb_camera_debugfs_entry *op = file->private_data;
|
2016-12-04 16:24:56 -07:00
|
|
|
struct gb_camera *gcam = file_inode(file)->i_private;
|
2015-12-14 18:18:06 -07:00
|
|
|
struct gb_camera_debugfs_buffer *buffer;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
/* For read-only entries the operation is triggered by a read. */
|
2017-01-11 21:59:40 -07:00
|
|
|
if (!(op->mask & 0222)) {
|
2015-12-14 18:18:06 -07:00
|
|
|
ret = op->execute(gcam, NULL, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = &gcam->debugfs.buffers[op->buffer];
|
|
|
|
|
|
|
|
return simple_read_from_buffer(buf, len, offset, buffer->data,
|
|
|
|
buffer->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t gb_camera_debugfs_write(struct file *file,
|
|
|
|
const char __user *buf, size_t len,
|
|
|
|
loff_t *offset)
|
|
|
|
{
|
|
|
|
const struct gb_camera_debugfs_entry *op = file->private_data;
|
2016-12-04 16:24:56 -07:00
|
|
|
struct gb_camera *gcam = file_inode(file)->i_private;
|
2015-12-14 18:18:06 -07:00
|
|
|
ssize_t ret;
|
|
|
|
char *kbuf;
|
|
|
|
|
|
|
|
if (len > 1024)
|
2016-10-12 11:13:01 -06:00
|
|
|
return -EINVAL;
|
2015-12-14 18:18:06 -07:00
|
|
|
|
|
|
|
kbuf = kmalloc(len + 1, GFP_KERNEL);
|
2016-09-20 22:59:29 -06:00
|
|
|
if (!kbuf)
|
2015-12-14 18:18:06 -07:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
if (copy_from_user(kbuf, buf, len)) {
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
kbuf[len] = '\0';
|
|
|
|
|
|
|
|
ret = op->execute(gcam, kbuf, len);
|
|
|
|
|
|
|
|
done:
|
|
|
|
kfree(kbuf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_camera_debugfs_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(gb_camera_debugfs_entries); ++i) {
|
|
|
|
const struct gb_camera_debugfs_entry *entry =
|
|
|
|
&gb_camera_debugfs_entries[i];
|
|
|
|
|
2015-12-14 19:33:19 -07:00
|
|
|
if (!strcmp(file->f_path.dentry->d_iname, entry->name)) {
|
2015-12-14 18:18:06 -07:00
|
|
|
file->private_data = (void *)entry;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations gb_camera_debugfs_ops = {
|
|
|
|
.open = gb_camera_debugfs_open,
|
|
|
|
.read = gb_camera_debugfs_read,
|
|
|
|
.write = gb_camera_debugfs_write,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int gb_camera_debugfs_init(struct gb_camera *gcam)
|
|
|
|
{
|
|
|
|
struct gb_connection *connection = gcam->connection;
|
|
|
|
char dirname[27];
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create root debugfs entry and a file entry for each camera operation.
|
|
|
|
*/
|
|
|
|
snprintf(dirname, 27, "camera-%u.%u", connection->intf->interface_id,
|
2016-05-20 05:33:35 -06:00
|
|
|
gcam->bundle->id);
|
2015-12-14 18:18:06 -07:00
|
|
|
|
|
|
|
gcam->debugfs.root = debugfs_create_dir(dirname, gb_debugfs_get());
|
|
|
|
|
treewide: Use array_size() in vmalloc()
The vmalloc() function has no 2-factor argument form, so multiplication
factors need to be wrapped in array_size(). This patch replaces cases of:
vmalloc(a * b)
with:
vmalloc(array_size(a, b))
as well as handling cases of:
vmalloc(a * b * c)
with:
vmalloc(array3_size(a, b, c))
This does, however, attempt to ignore constant size factors like:
vmalloc(4 * 1024)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
vmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
vmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
vmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_ID
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_ID
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
vmalloc(
- SIZE * COUNT
+ array_size(COUNT, SIZE)
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
vmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
vmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
vmalloc(C1 * C2 * C3, ...)
|
vmalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants.
@@
expression E1, E2;
constant C1, C2;
@@
(
vmalloc(C1 * C2, ...)
|
vmalloc(
- E1 * E2
+ array_size(E1, E2)
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 15:27:11 -06:00
|
|
|
gcam->debugfs.buffers =
|
|
|
|
vmalloc(array_size(GB_CAMERA_DEBUGFS_BUFFER_MAX,
|
|
|
|
sizeof(*gcam->debugfs.buffers)));
|
2015-12-14 18:18:06 -07:00
|
|
|
if (!gcam->debugfs.buffers)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(gb_camera_debugfs_entries); ++i) {
|
|
|
|
const struct gb_camera_debugfs_entry *entry =
|
|
|
|
&gb_camera_debugfs_entries[i];
|
|
|
|
|
|
|
|
gcam->debugfs.buffers[i].length = 0;
|
|
|
|
|
2018-05-29 08:29:35 -06:00
|
|
|
debugfs_create_file(entry->name, entry->mask,
|
|
|
|
gcam->debugfs.root, gcam,
|
|
|
|
&gb_camera_debugfs_ops);
|
2015-12-14 18:18:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_camera_debugfs_cleanup(struct gb_camera *gcam)
|
|
|
|
{
|
2016-05-24 12:34:49 -06:00
|
|
|
debugfs_remove_recursive(gcam->debugfs.root);
|
2015-12-14 18:18:06 -07:00
|
|
|
|
|
|
|
vfree(gcam->debugfs.buffers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
|
|
* Init & Cleanup
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void gb_camera_cleanup(struct gb_camera *gcam)
|
|
|
|
{
|
|
|
|
gb_camera_debugfs_cleanup(gcam);
|
|
|
|
|
2016-06-14 06:59:25 -06:00
|
|
|
mutex_lock(&gcam->mutex);
|
2016-05-11 02:18:06 -06:00
|
|
|
if (gcam->data_connection) {
|
|
|
|
gb_connection_disable(gcam->data_connection);
|
|
|
|
gb_connection_destroy(gcam->data_connection);
|
2016-05-31 02:33:11 -06:00
|
|
|
gcam->data_connection = NULL;
|
2015-12-14 18:18:06 -07:00
|
|
|
}
|
|
|
|
|
2016-05-20 05:33:35 -06:00
|
|
|
if (gcam->connection) {
|
|
|
|
gb_connection_disable(gcam->connection);
|
|
|
|
gb_connection_destroy(gcam->connection);
|
2016-05-31 02:33:11 -06:00
|
|
|
gcam->connection = NULL;
|
2016-05-20 05:33:35 -06:00
|
|
|
}
|
2016-05-31 02:33:11 -06:00
|
|
|
mutex_unlock(&gcam->mutex);
|
|
|
|
}
|
2016-05-20 05:33:35 -06:00
|
|
|
|
2016-05-31 02:33:11 -06:00
|
|
|
static void gb_camera_release_module(struct kref *ref)
|
|
|
|
{
|
|
|
|
struct gb_camera_module *cam_mod =
|
|
|
|
container_of(ref, struct gb_camera_module, refcount);
|
|
|
|
kfree(cam_mod->priv);
|
2015-12-14 18:18:06 -07:00
|
|
|
}
|
|
|
|
|
2016-05-20 05:33:35 -06:00
|
|
|
static int gb_camera_probe(struct gb_bundle *bundle,
|
|
|
|
const struct greybus_bundle_id *id)
|
2015-12-14 18:18:06 -07:00
|
|
|
{
|
2016-05-20 05:33:35 -06:00
|
|
|
struct gb_connection *conn;
|
2015-12-14 18:18:06 -07:00
|
|
|
struct gb_camera *gcam;
|
2016-05-20 05:33:35 -06:00
|
|
|
u16 mgmt_cport_id = 0;
|
|
|
|
u16 data_cport_id = 0;
|
|
|
|
unsigned int i;
|
2015-12-14 18:18:06 -07:00
|
|
|
int ret;
|
|
|
|
|
2016-05-20 05:33:35 -06:00
|
|
|
/*
|
|
|
|
* The camera bundle must contain exactly two CPorts, one for the
|
|
|
|
* camera management protocol and one for the camera data protocol.
|
|
|
|
*/
|
|
|
|
if (bundle->num_cports != 2)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
for (i = 0; i < bundle->num_cports; ++i) {
|
|
|
|
struct greybus_descriptor_cport *desc = &bundle->cport_desc[i];
|
|
|
|
|
|
|
|
switch (desc->protocol_id) {
|
|
|
|
case GREYBUS_PROTOCOL_CAMERA_MGMT:
|
|
|
|
mgmt_cport_id = le16_to_cpu(desc->id);
|
|
|
|
break;
|
|
|
|
case GREYBUS_PROTOCOL_CAMERA_DATA:
|
|
|
|
data_cport_id = le16_to_cpu(desc->id);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mgmt_cport_id || !data_cport_id)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2015-12-14 18:18:06 -07:00
|
|
|
gcam = kzalloc(sizeof(*gcam), GFP_KERNEL);
|
|
|
|
if (!gcam)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2016-06-13 03:38:01 -06:00
|
|
|
mutex_init(&gcam->mutex);
|
2016-05-20 05:33:35 -06:00
|
|
|
|
2016-06-14 06:59:24 -06:00
|
|
|
gcam->bundle = bundle;
|
|
|
|
gcam->state = GB_CAMERA_STATE_UNCONFIGURED;
|
2016-06-14 06:59:25 -06:00
|
|
|
gcam->data_cport_id = data_cport_id;
|
2016-06-14 06:59:24 -06:00
|
|
|
|
2016-05-20 05:33:35 -06:00
|
|
|
conn = gb_connection_create(bundle, mgmt_cport_id,
|
|
|
|
gb_camera_request_handler);
|
|
|
|
if (IS_ERR(conn)) {
|
|
|
|
ret = PTR_ERR(conn);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
gcam->connection = conn;
|
|
|
|
gb_connection_set_data(conn, gcam);
|
|
|
|
|
|
|
|
ret = gb_connection_enable(conn);
|
2016-05-11 02:18:06 -06:00
|
|
|
if (ret)
|
|
|
|
goto error;
|
2015-12-30 03:23:51 -07:00
|
|
|
|
2015-12-14 18:18:06 -07:00
|
|
|
ret = gb_camera_debugfs_init(gcam);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
2016-05-31 02:33:11 -06:00
|
|
|
gcam->module.priv = gcam;
|
|
|
|
gcam->module.ops = &gb_cam_ops;
|
|
|
|
gcam->module.interface_id = gcam->connection->intf->interface_id;
|
|
|
|
gcam->module.release = gb_camera_release_module;
|
|
|
|
ret = gb_camera_register(&gcam->module);
|
2016-01-13 12:52:38 -07:00
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
2016-05-20 05:33:35 -06:00
|
|
|
greybus_set_drvdata(bundle, gcam);
|
|
|
|
|
2016-07-14 14:13:00 -06:00
|
|
|
gb_pm_runtime_put_autosuspend(gcam->bundle);
|
|
|
|
|
2015-12-14 18:18:06 -07:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
gb_camera_cleanup(gcam);
|
2016-05-31 02:33:11 -06:00
|
|
|
kfree(gcam);
|
2015-12-14 18:18:06 -07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-20 05:33:35 -06:00
|
|
|
static void gb_camera_disconnect(struct gb_bundle *bundle)
|
2015-12-14 18:18:06 -07:00
|
|
|
{
|
2016-05-20 05:33:35 -06:00
|
|
|
struct gb_camera *gcam = greybus_get_drvdata(bundle);
|
2016-07-14 14:13:00 -06:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gb_pm_runtime_get_sync(bundle);
|
|
|
|
if (ret)
|
|
|
|
gb_pm_runtime_get_noresume(bundle);
|
2015-12-14 18:18:06 -07:00
|
|
|
|
|
|
|
gb_camera_cleanup(gcam);
|
2016-05-31 02:33:11 -06:00
|
|
|
gb_camera_unregister(&gcam->module);
|
2015-12-14 18:18:06 -07:00
|
|
|
}
|
|
|
|
|
2016-05-20 05:33:35 -06:00
|
|
|
static const struct greybus_bundle_id gb_camera_id_table[] = {
|
|
|
|
{ GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_CAMERA) },
|
|
|
|
{ },
|
|
|
|
};
|
|
|
|
|
2016-09-09 01:47:01 -06:00
|
|
|
#ifdef CONFIG_PM
|
2016-07-14 14:13:00 -06:00
|
|
|
static int gb_camera_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
|
|
|
struct gb_camera *gcam = greybus_get_drvdata(bundle);
|
|
|
|
|
|
|
|
if (gcam->data_connection)
|
|
|
|
gb_connection_disable(gcam->data_connection);
|
|
|
|
|
|
|
|
gb_connection_disable(gcam->connection);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_camera_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
|
|
|
struct gb_camera *gcam = greybus_get_drvdata(bundle);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gb_connection_enable(gcam->connection);
|
|
|
|
if (ret) {
|
|
|
|
gcam_err(gcam, "failed to enable connection: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gcam->data_connection) {
|
|
|
|
ret = gb_connection_enable(gcam->data_connection);
|
|
|
|
if (ret) {
|
|
|
|
gcam_err(gcam,
|
|
|
|
"failed to enable data connection: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const struct dev_pm_ops gb_camera_pm_ops = {
|
|
|
|
SET_RUNTIME_PM_OPS(gb_camera_suspend, gb_camera_resume, NULL)
|
|
|
|
};
|
|
|
|
|
2016-05-20 05:33:35 -06:00
|
|
|
static struct greybus_driver gb_camera_driver = {
|
|
|
|
.name = "camera",
|
|
|
|
.probe = gb_camera_probe,
|
|
|
|
.disconnect = gb_camera_disconnect,
|
|
|
|
.id_table = gb_camera_id_table,
|
2016-07-14 14:13:00 -06:00
|
|
|
.driver.pm = &gb_camera_pm_ops,
|
2015-12-14 18:18:06 -07:00
|
|
|
};
|
|
|
|
|
2016-05-20 05:33:35 -06:00
|
|
|
module_greybus_driver(gb_camera_driver);
|
2015-12-14 18:18:06 -07:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL v2");
|