Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

staging: vc04_services: Move global g_state to vchiq_state

The patch intended to drop the g_state pointer.
g_state is supposed to be a central place to track the state
via vchiq_state. This is now moved to be contained in the
struct vchiq_drv_mgmt. As a result vchiq_get_state() is also removed.

In order to have access to vchiq_drv_mgmt, vchiq_initialise()
and vchiq_mmal_init() are modified to receive a struct device pointer
as one of their function parameter

The vchiq_state pointer is now passed directly to
vchiq_dump_platform_instances() to get access to the state instead
getting it via vchiq_get_state().

For the char device, struct miscdevice is retrieved by struct file's
private data in vchiq_open and struct vchiq_drv_mgmt is retrieved
thereafter.

Removal of global variable members is now addressed hence, drop
the corresponding item from the TODO list.

Signed-off-by: Umang Jain <umang.jain@ideasonboard.com>
Link: https://lore.kernel.org/r/20240412075743.60712-11-umang.jain@ideasonboard.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Umang Jain and committed by
Greg Kroah-Hartman
42a2f666 7f56c601

+37 -55
+4 -1
drivers/staging/vc04_services/bcm2835-audio/bcm2835-vchiq.c
··· 7 7 #include "bcm2835.h" 8 8 #include "vc_vchi_audioserv_defs.h" 9 9 10 + #include "../interface/vchiq_arm/vchiq_arm.h" 11 + 10 12 struct bcm2835_audio_instance { 11 13 struct device *dev; 12 14 unsigned int service_handle; ··· 177 175 178 176 int bcm2835_new_vchi_ctx(struct device *dev, struct bcm2835_vchi_ctx *vchi_ctx) 179 177 { 178 + struct vchiq_drv_mgmt *mgmt = dev_get_drvdata(dev->parent); 180 179 int ret; 181 180 182 181 /* Initialize and create a VCHI connection */ 183 - ret = vchiq_initialise(&vchi_ctx->instance); 182 + ret = vchiq_initialise(&mgmt->state, &vchi_ctx->instance); 184 183 if (ret) { 185 184 dev_err(dev, "failed to initialise VCHI instance (ret=%d)\n", 186 185 ret);
+2 -2
drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
··· 1555 1555 u32 param_size; 1556 1556 struct vchiq_mmal_component *camera; 1557 1557 1558 - ret = vchiq_mmal_init(&dev->instance); 1558 + ret = vchiq_mmal_init(dev->v4l2_dev.dev, &dev->instance); 1559 1559 if (ret < 0) { 1560 1560 v4l2_err(&dev->v4l2_dev, "%s: vchiq mmal init failed %d\n", 1561 1561 __func__, ret); ··· 1854 1854 return ret; 1855 1855 } 1856 1856 1857 - ret = vchiq_mmal_init(&instance); 1857 + ret = vchiq_mmal_init(&device->dev, &instance); 1858 1858 if (ret < 0) 1859 1859 return ret; 1860 1860
+3 -1
drivers/staging/vc04_services/include/linux/raspberrypi/vchiq.h
··· 48 48 }; 49 49 50 50 struct vchiq_instance; 51 + struct vchiq_state; 51 52 52 53 struct vchiq_service_base { 53 54 int fourcc; ··· 79 78 short version_min; /* Update for incompatible changes */ 80 79 }; 81 80 82 - extern int vchiq_initialise(struct vchiq_instance **pinstance); 81 + extern int vchiq_initialise(struct vchiq_state *state, 82 + struct vchiq_instance **pinstance); 83 83 extern int vchiq_shutdown(struct vchiq_instance *instance); 84 84 extern int vchiq_connect(struct vchiq_instance *instance); 85 85 extern int vchiq_open_service(struct vchiq_instance *instance,
-8
drivers/staging/vc04_services/interface/TODO
··· 41 41 indentation deep making it very unpleasant to read. This is specially relevant 42 42 in the character driver ioctl code and in the core thread functions. 43 43 44 - * Get rid of all non essential global structures and create a proper per 45 - device structure 46 - 47 - The first thing one generally sees in a probe function is a memory allocation 48 - for all the device specific data. This structure is then passed all over the 49 - driver. This is good practice since it makes the driver work regardless of the 50 - number of devices probed. 51 - 52 44 * Clean up Sparse warnings from __user annotations. See 53 45 vchiq_irq_queue_bulk_tx_rx(). Ensure that the address of "&waiter->bulk_waiter" 54 46 is never disclosed to userspace.
+8 -28
drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
··· 59 59 #define KEEPALIVE_VER 1 60 60 #define KEEPALIVE_VER_MIN KEEPALIVE_VER 61 61 62 - struct vchiq_state g_state; 63 - 64 62 /* 65 63 * The devices implemented in the VCHIQ firmware are not discoverable, 66 64 * so we need to maintain a list of them in order to register them with ··· 696 698 } 697 699 698 700 #define VCHIQ_INIT_RETRIES 10 699 - int vchiq_initialise(struct vchiq_instance **instance_out) 701 + int vchiq_initialise(struct vchiq_state *state, struct vchiq_instance **instance_out) 700 702 { 701 - struct vchiq_state *state; 702 703 struct vchiq_instance *instance = NULL; 703 704 int i, ret; 704 705 ··· 707 710 * block forever. 708 711 */ 709 712 for (i = 0; i < VCHIQ_INIT_RETRIES; i++) { 710 - state = vchiq_get_state(); 711 713 if (state) 712 714 break; 713 715 usleep_range(500, 600); ··· 1022 1026 void *bulk_userdata) 1023 1027 { 1024 1028 struct vchiq_completion_data_kernel *completion; 1029 + struct vchiq_drv_mgmt *mgmt = dev_get_drvdata(instance->state->dev); 1025 1030 int insert; 1026 1031 1027 - DEBUG_INITIALISE(g_state.local); 1032 + DEBUG_INITIALISE(mgmt->state.local); 1028 1033 1029 1034 insert = instance->completion_insert; 1030 1035 while ((insert - instance->completion_remove) >= MAX_COMPLETIONS) { ··· 1088 1091 * containing the original callback and the user state structure, which 1089 1092 * contains a circular buffer for completion records. 1090 1093 */ 1094 + struct vchiq_drv_mgmt *mgmt = dev_get_drvdata(instance->state->dev); 1091 1095 struct user_service *user_service; 1092 1096 struct vchiq_service *service; 1093 1097 bool skip_completion = false; 1094 1098 1095 - DEBUG_INITIALISE(g_state.local); 1099 + DEBUG_INITIALISE(mgmt->state.local); 1096 1100 1097 1101 DEBUG_TRACE(SERVICE_CALLBACK_LINE); 1098 1102 ··· 1198 1200 bulk_userdata); 1199 1201 } 1200 1202 1201 - void vchiq_dump_platform_instances(struct seq_file *f) 1203 + void vchiq_dump_platform_instances(struct vchiq_state *state, struct seq_file *f) 1202 1204 { 1203 - struct vchiq_state *state = vchiq_get_state(); 1204 1205 int i; 1205 1206 1206 1207 if (!state) ··· 1274 1277 seq_puts(f, "\n"); 1275 1278 } 1276 1279 1277 - struct vchiq_state * 1278 - vchiq_get_state(void) 1279 - { 1280 - if (!g_state.remote) { 1281 - pr_err("%s: g_state.remote == NULL\n", __func__); 1282 - return NULL; 1283 - } 1284 - 1285 - if (g_state.remote->initialised != 1) { 1286 - pr_notice("%s: g_state.remote->initialised != 1 (%d)\n", 1287 - __func__, g_state.remote->initialised); 1288 - return NULL; 1289 - } 1290 - 1291 - return &g_state; 1292 - } 1293 - 1294 1280 /* 1295 1281 * Autosuspend related functionality 1296 1282 */ ··· 1307 1327 .version_min = KEEPALIVE_VER_MIN 1308 1328 }; 1309 1329 1310 - ret = vchiq_initialise(&instance); 1330 + ret = vchiq_initialise(state, &instance); 1311 1331 if (ret) { 1312 1332 dev_err(state->dev, "suspend: %s: vchiq_initialise failed %d\n", __func__, ret); 1313 1333 goto exit; ··· 1755 1775 mgmt->info = info; 1756 1776 platform_set_drvdata(pdev, mgmt); 1757 1777 1758 - err = vchiq_platform_init(pdev, &g_state); 1778 + err = vchiq_platform_init(pdev, &mgmt->state); 1759 1779 if (err) 1760 1780 goto failed_platform_init; 1761 1781
+2 -5
drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
··· 52 52 unsigned int fragments_size; 53 53 54 54 void __iomem *regs; 55 + 56 + struct vchiq_state state; 55 57 }; 56 58 57 59 struct user_service { ··· 99 97 100 98 struct vchiq_debugfs_node debugfs_node; 101 99 }; 102 - 103 - extern struct vchiq_state g_state; 104 - 105 - extern struct vchiq_state * 106 - vchiq_get_state(void); 107 100 108 101 int 109 102 vchiq_use_service(struct vchiq_instance *instance, unsigned int handle);
+1 -1
drivers/staging/vc04_services/interface/vchiq_arm/vchiq_core.c
··· 3505 3505 3506 3506 vchiq_dump_shared_state(f, state, state->remote, "Remote"); 3507 3507 3508 - vchiq_dump_platform_instances(f); 3508 + vchiq_dump_platform_instances(state, f); 3509 3509 3510 3510 for (i = 0; i < state->unused_service; i++) { 3511 3511 struct vchiq_service *service = find_service_by_port(state, i);
+1 -1
drivers/staging/vc04_services/interface/vchiq_arm/vchiq_core.h
··· 527 527 528 528 void vchiq_dump_platform_state(struct seq_file *f); 529 529 530 - void vchiq_dump_platform_instances(struct seq_file *f); 530 + void vchiq_dump_platform_instances(struct vchiq_state *state, struct seq_file *f); 531 531 532 532 void vchiq_dump_platform_service_state(struct seq_file *f, struct vchiq_service *service); 533 533
+4 -1
drivers/staging/vc04_services/interface/vchiq_arm/vchiq_debugfs.c
··· 42 42 43 43 static int vchiq_dump_show(struct seq_file *f, void *offset) 44 44 { 45 - vchiq_dump_state(f, &g_state); 45 + struct vchiq_instance *instance = f->private; 46 + 47 + vchiq_dump_state(f, instance->state); 48 + 46 49 return 0; 47 50 } 48 51 DEFINE_SHOW_ATTRIBUTE(vchiq_dump);
+6 -4
drivers/staging/vc04_services/interface/vchiq_arm/vchiq_dev.c
··· 208 208 struct vchiq_header *header; 209 209 int ret; 210 210 211 - DEBUG_INITIALISE(g_state.local); 211 + DEBUG_INITIALISE(instance->state->local); 212 212 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE); 213 213 service = find_service_for_instance(instance, args->handle); 214 214 if (!service) ··· 435 435 int remove; 436 436 int ret; 437 437 438 - DEBUG_INITIALISE(g_state.local); 438 + DEBUG_INITIALISE(instance->state->local); 439 439 440 440 DEBUG_TRACE(AWAIT_COMPLETION_LINE); 441 441 if (!instance->connected) ··· 1163 1163 1164 1164 static int vchiq_open(struct inode *inode, struct file *file) 1165 1165 { 1166 - struct vchiq_state *state = vchiq_get_state(); 1166 + struct miscdevice *vchiq_miscdev = file->private_data; 1167 + struct vchiq_drv_mgmt *mgmt = dev_get_drvdata(vchiq_miscdev->parent); 1168 + struct vchiq_state *state = &mgmt->state; 1167 1169 struct vchiq_instance *instance; 1168 1170 1169 1171 dev_dbg(state->dev, "arm: vchiq open\n"); ··· 1198 1196 static int vchiq_release(struct inode *inode, struct file *file) 1199 1197 { 1200 1198 struct vchiq_instance *instance = file->private_data; 1201 - struct vchiq_state *state = vchiq_get_state(); 1199 + struct vchiq_state *state = instance->state; 1202 1200 struct vchiq_service *service; 1203 1201 int ret = 0; 1204 1202 int i;
+4 -2
drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c
··· 26 26 #include <media/videobuf2-vmalloc.h> 27 27 28 28 #include "../include/linux/raspberrypi/vchiq.h" 29 + #include "../interface/vchiq_arm/vchiq_arm.h" 29 30 #include "mmal-common.h" 30 31 #include "mmal-vchiq.h" 31 32 #include "mmal-msg.h" ··· 1853 1852 } 1854 1853 EXPORT_SYMBOL_GPL(vchiq_mmal_finalise); 1855 1854 1856 - int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance) 1855 + int vchiq_mmal_init(struct device *dev, struct vchiq_mmal_instance **out_instance) 1857 1856 { 1858 1857 int status; 1859 1858 int err = -ENODEV; ··· 1866 1865 .callback = mmal_service_callback, 1867 1866 .userdata = NULL, 1868 1867 }; 1868 + struct vchiq_drv_mgmt *mgmt = dev_get_drvdata(dev->parent); 1869 1869 1870 1870 /* compile time checks to ensure structure size as they are 1871 1871 * directly (de)serialised from memory. ··· 1882 1880 BUILD_BUG_ON(sizeof(struct mmal_port) != 64); 1883 1881 1884 1882 /* create a vchi instance */ 1885 - status = vchiq_initialise(&vchiq_instance); 1883 + status = vchiq_initialise(&mgmt->state, &vchiq_instance); 1886 1884 if (status) { 1887 1885 pr_err("Failed to initialise VCHI instance (status=%d)\n", 1888 1886 status);
+2 -1
drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.h
··· 25 25 #define MMAL_FORMAT_EXTRADATA_MAX_SIZE 128 26 26 27 27 struct vchiq_mmal_instance; 28 + struct device; 28 29 29 30 enum vchiq_mmal_es_type { 30 31 MMAL_ES_TYPE_UNKNOWN, /**< Unknown elementary stream type */ ··· 95 94 u32 client_component; /* Used to ref back to client struct */ 96 95 }; 97 96 98 - int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance); 97 + int vchiq_mmal_init(struct device *dev, struct vchiq_mmal_instance **out_instance); 99 98 int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance); 100 99 101 100 /* Initialise a mmal component and its ports