Skip to content

Commit 5eba014

Browse files
committed
platform/x86: x86-android-tablets: Add support for instantiating platform-devs
Add support for instantiating platform-devs, note this also makes some small changes to the i2c_client instantiating code to make the 2 flows identical. Specifically for the pdevs flow pdev_count must only be set after allocating the pdevs array, to avoid a NULL ptr deref in x86_android_tablet_cleanup() and the i2c_clients flow is updated to work the same way. Signed-off-by: Hans de Goede <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent cd26465 commit 5eba014

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

drivers/platform/x86/x86-android-tablets.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/irqdomain.h>
2121
#include <linux/module.h>
2222
#include <linux/mod_devicetable.h>
23+
#include <linux/platform_device.h>
2324
#include <linux/string.h>
2425
/* For gpio_get_desc() which is EXPORT_SYMBOL_GPL() */
2526
#include "../../gpio/gpiolib.h"
@@ -128,7 +129,9 @@ struct x86_i2c_client_info {
128129

129130
struct x86_dev_info {
130131
const struct x86_i2c_client_info *i2c_client_info;
132+
const struct platform_device_info *pdev_info;
131133
int i2c_client_count;
134+
int pdev_count;
132135
};
133136

134137
/*
@@ -269,7 +272,9 @@ static const struct dmi_system_id x86_android_tablet_ids[] __initconst = {
269272
MODULE_DEVICE_TABLE(dmi, x86_android_tablet_ids);
270273

271274
static int i2c_client_count;
275+
static int pdev_count;
272276
static struct i2c_client **i2c_clients;
277+
static struct platform_device **pdevs;
273278

274279
static __init int x86_instantiate_i2c_client(const struct x86_dev_info *dev_info,
275280
int idx)
@@ -309,6 +314,11 @@ static void x86_android_tablet_cleanup(void)
309314
{
310315
int i;
311316

317+
for (i = 0; i < pdev_count; i++)
318+
platform_device_unregister(pdevs[i]);
319+
320+
kfree(pdevs);
321+
312322
for (i = 0; i < i2c_client_count; i++)
313323
i2c_unregister_device(i2c_clients[i]);
314324

@@ -327,21 +337,35 @@ static __init int x86_android_tablet_init(void)
327337

328338
dev_info = id->driver_data;
329339

330-
i2c_client_count = dev_info->i2c_client_count;
331-
332-
i2c_clients = kcalloc(i2c_client_count, sizeof(*i2c_clients), GFP_KERNEL);
340+
i2c_clients = kcalloc(dev_info->i2c_client_count, sizeof(*i2c_clients), GFP_KERNEL);
333341
if (!i2c_clients)
334342
return -ENOMEM;
335343

336-
for (i = 0; i < dev_info->i2c_client_count; i++) {
344+
i2c_client_count = dev_info->i2c_client_count;
345+
for (i = 0; i < i2c_client_count; i++) {
337346
ret = x86_instantiate_i2c_client(dev_info, i);
338347
if (ret < 0) {
339348
x86_android_tablet_cleanup();
340-
break;
349+
return ret;
350+
}
351+
}
352+
353+
pdevs = kcalloc(dev_info->pdev_count, sizeof(*pdevs), GFP_KERNEL);
354+
if (!pdevs) {
355+
x86_android_tablet_cleanup();
356+
return -ENOMEM;
357+
}
358+
359+
pdev_count = dev_info->pdev_count;
360+
for (i = 0; i < pdev_count; i++) {
361+
pdevs[i] = platform_device_register_full(&dev_info->pdev_info[i]);
362+
if (IS_ERR(pdevs[i])) {
363+
x86_android_tablet_cleanup();
364+
return PTR_ERR(pdevs[i]);
341365
}
342366
}
343367

344-
return ret;
368+
return 0;
345369
}
346370

347371
module_init(x86_android_tablet_init);

0 commit comments

Comments
 (0)