bpf: Prevent memory disambiguation attack
[linux/fpc-iii.git] / drivers / tty / serdev / core.c
blobf439c72b9e3c165a7068a7ff26e4fd93f2d5266e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2016-2017 Linaro Ltd., Rob Herring <robh@kernel.org>
5 * Based on drivers/spmi/spmi.c:
6 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
7 */
9 #include <linux/acpi.h>
10 #include <linux/errno.h>
11 #include <linux/idr.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 #include <linux/serdev.h>
17 #include <linux/slab.h>
19 static bool is_registered;
20 static DEFINE_IDA(ctrl_ida);
22 static ssize_t modalias_show(struct device *dev,
23 struct device_attribute *attr, char *buf)
25 int len;
27 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
28 if (len != -ENODEV)
29 return len;
31 return of_device_modalias(dev, buf, PAGE_SIZE);
33 static DEVICE_ATTR_RO(modalias);
35 static struct attribute *serdev_device_attrs[] = {
36 &dev_attr_modalias.attr,
37 NULL,
39 ATTRIBUTE_GROUPS(serdev_device);
41 static int serdev_device_uevent(struct device *dev, struct kobj_uevent_env *env)
43 int rc;
45 /* TODO: platform modalias */
47 rc = acpi_device_uevent_modalias(dev, env);
48 if (rc != -ENODEV)
49 return rc;
51 return of_device_uevent_modalias(dev, env);
54 static void serdev_device_release(struct device *dev)
56 struct serdev_device *serdev = to_serdev_device(dev);
57 kfree(serdev);
60 static const struct device_type serdev_device_type = {
61 .groups = serdev_device_groups,
62 .uevent = serdev_device_uevent,
63 .release = serdev_device_release,
66 static bool is_serdev_device(const struct device *dev)
68 return dev->type == &serdev_device_type;
71 static void serdev_ctrl_release(struct device *dev)
73 struct serdev_controller *ctrl = to_serdev_controller(dev);
74 ida_simple_remove(&ctrl_ida, ctrl->nr);
75 kfree(ctrl);
78 static const struct device_type serdev_ctrl_type = {
79 .release = serdev_ctrl_release,
82 static int serdev_device_match(struct device *dev, struct device_driver *drv)
84 if (!is_serdev_device(dev))
85 return 0;
87 /* TODO: platform matching */
88 if (acpi_driver_match_device(dev, drv))
89 return 1;
91 return of_driver_match_device(dev, drv);
94 /**
95 * serdev_device_add() - add a device previously constructed via serdev_device_alloc()
96 * @serdev: serdev_device to be added
98 int serdev_device_add(struct serdev_device *serdev)
100 struct serdev_controller *ctrl = serdev->ctrl;
101 struct device *parent = serdev->dev.parent;
102 int err;
104 dev_set_name(&serdev->dev, "%s-%d", dev_name(parent), serdev->nr);
106 /* Only a single slave device is currently supported. */
107 if (ctrl->serdev) {
108 dev_err(&serdev->dev, "controller busy\n");
109 return -EBUSY;
111 ctrl->serdev = serdev;
113 err = device_add(&serdev->dev);
114 if (err < 0) {
115 dev_err(&serdev->dev, "Can't add %s, status %d\n",
116 dev_name(&serdev->dev), err);
117 goto err_clear_serdev;
120 dev_dbg(&serdev->dev, "device %s registered\n", dev_name(&serdev->dev));
122 return 0;
124 err_clear_serdev:
125 ctrl->serdev = NULL;
126 return err;
128 EXPORT_SYMBOL_GPL(serdev_device_add);
131 * serdev_device_remove(): remove an serdev device
132 * @serdev: serdev_device to be removed
134 void serdev_device_remove(struct serdev_device *serdev)
136 struct serdev_controller *ctrl = serdev->ctrl;
138 device_unregister(&serdev->dev);
139 ctrl->serdev = NULL;
141 EXPORT_SYMBOL_GPL(serdev_device_remove);
143 int serdev_device_open(struct serdev_device *serdev)
145 struct serdev_controller *ctrl = serdev->ctrl;
147 if (!ctrl || !ctrl->ops->open)
148 return -EINVAL;
150 return ctrl->ops->open(ctrl);
152 EXPORT_SYMBOL_GPL(serdev_device_open);
154 void serdev_device_close(struct serdev_device *serdev)
156 struct serdev_controller *ctrl = serdev->ctrl;
158 if (!ctrl || !ctrl->ops->close)
159 return;
161 ctrl->ops->close(ctrl);
163 EXPORT_SYMBOL_GPL(serdev_device_close);
165 static void devm_serdev_device_release(struct device *dev, void *dr)
167 serdev_device_close(*(struct serdev_device **)dr);
170 int devm_serdev_device_open(struct device *dev, struct serdev_device *serdev)
172 struct serdev_device **dr;
173 int ret;
175 dr = devres_alloc(devm_serdev_device_release, sizeof(*dr), GFP_KERNEL);
176 if (!dr)
177 return -ENOMEM;
179 ret = serdev_device_open(serdev);
180 if (ret) {
181 devres_free(dr);
182 return ret;
185 *dr = serdev;
186 devres_add(dev, dr);
188 return 0;
190 EXPORT_SYMBOL_GPL(devm_serdev_device_open);
192 void serdev_device_write_wakeup(struct serdev_device *serdev)
194 complete(&serdev->write_comp);
196 EXPORT_SYMBOL_GPL(serdev_device_write_wakeup);
198 int serdev_device_write_buf(struct serdev_device *serdev,
199 const unsigned char *buf, size_t count)
201 struct serdev_controller *ctrl = serdev->ctrl;
203 if (!ctrl || !ctrl->ops->write_buf)
204 return -EINVAL;
206 return ctrl->ops->write_buf(ctrl, buf, count);
208 EXPORT_SYMBOL_GPL(serdev_device_write_buf);
210 int serdev_device_write(struct serdev_device *serdev,
211 const unsigned char *buf, size_t count,
212 unsigned long timeout)
214 struct serdev_controller *ctrl = serdev->ctrl;
215 int ret;
217 if (!ctrl || !ctrl->ops->write_buf ||
218 (timeout && !serdev->ops->write_wakeup))
219 return -EINVAL;
221 mutex_lock(&serdev->write_lock);
222 do {
223 reinit_completion(&serdev->write_comp);
225 ret = ctrl->ops->write_buf(ctrl, buf, count);
226 if (ret < 0)
227 break;
229 buf += ret;
230 count -= ret;
232 } while (count &&
233 (timeout = wait_for_completion_timeout(&serdev->write_comp,
234 timeout)));
235 mutex_unlock(&serdev->write_lock);
236 return ret < 0 ? ret : (count ? -ETIMEDOUT : 0);
238 EXPORT_SYMBOL_GPL(serdev_device_write);
240 void serdev_device_write_flush(struct serdev_device *serdev)
242 struct serdev_controller *ctrl = serdev->ctrl;
244 if (!ctrl || !ctrl->ops->write_flush)
245 return;
247 ctrl->ops->write_flush(ctrl);
249 EXPORT_SYMBOL_GPL(serdev_device_write_flush);
251 int serdev_device_write_room(struct serdev_device *serdev)
253 struct serdev_controller *ctrl = serdev->ctrl;
255 if (!ctrl || !ctrl->ops->write_room)
256 return 0;
258 return serdev->ctrl->ops->write_room(ctrl);
260 EXPORT_SYMBOL_GPL(serdev_device_write_room);
262 unsigned int serdev_device_set_baudrate(struct serdev_device *serdev, unsigned int speed)
264 struct serdev_controller *ctrl = serdev->ctrl;
266 if (!ctrl || !ctrl->ops->set_baudrate)
267 return 0;
269 return ctrl->ops->set_baudrate(ctrl, speed);
272 EXPORT_SYMBOL_GPL(serdev_device_set_baudrate);
274 void serdev_device_set_flow_control(struct serdev_device *serdev, bool enable)
276 struct serdev_controller *ctrl = serdev->ctrl;
278 if (!ctrl || !ctrl->ops->set_flow_control)
279 return;
281 ctrl->ops->set_flow_control(ctrl, enable);
283 EXPORT_SYMBOL_GPL(serdev_device_set_flow_control);
285 int serdev_device_set_parity(struct serdev_device *serdev,
286 enum serdev_parity parity)
288 struct serdev_controller *ctrl = serdev->ctrl;
290 if (!ctrl || !ctrl->ops->set_parity)
291 return -ENOTSUPP;
293 return ctrl->ops->set_parity(ctrl, parity);
295 EXPORT_SYMBOL_GPL(serdev_device_set_parity);
297 void serdev_device_wait_until_sent(struct serdev_device *serdev, long timeout)
299 struct serdev_controller *ctrl = serdev->ctrl;
301 if (!ctrl || !ctrl->ops->wait_until_sent)
302 return;
304 ctrl->ops->wait_until_sent(ctrl, timeout);
306 EXPORT_SYMBOL_GPL(serdev_device_wait_until_sent);
308 int serdev_device_get_tiocm(struct serdev_device *serdev)
310 struct serdev_controller *ctrl = serdev->ctrl;
312 if (!ctrl || !ctrl->ops->get_tiocm)
313 return -ENOTSUPP;
315 return ctrl->ops->get_tiocm(ctrl);
317 EXPORT_SYMBOL_GPL(serdev_device_get_tiocm);
319 int serdev_device_set_tiocm(struct serdev_device *serdev, int set, int clear)
321 struct serdev_controller *ctrl = serdev->ctrl;
323 if (!ctrl || !ctrl->ops->set_tiocm)
324 return -ENOTSUPP;
326 return ctrl->ops->set_tiocm(ctrl, set, clear);
328 EXPORT_SYMBOL_GPL(serdev_device_set_tiocm);
330 static int serdev_drv_probe(struct device *dev)
332 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
334 return sdrv->probe(to_serdev_device(dev));
337 static int serdev_drv_remove(struct device *dev)
339 const struct serdev_device_driver *sdrv = to_serdev_device_driver(dev->driver);
340 if (sdrv->remove)
341 sdrv->remove(to_serdev_device(dev));
342 return 0;
345 static struct bus_type serdev_bus_type = {
346 .name = "serial",
347 .match = serdev_device_match,
348 .probe = serdev_drv_probe,
349 .remove = serdev_drv_remove,
353 * serdev_controller_alloc() - Allocate a new serdev device
354 * @ctrl: associated controller
356 * Caller is responsible for either calling serdev_device_add() to add the
357 * newly allocated controller, or calling serdev_device_put() to discard it.
359 struct serdev_device *serdev_device_alloc(struct serdev_controller *ctrl)
361 struct serdev_device *serdev;
363 serdev = kzalloc(sizeof(*serdev), GFP_KERNEL);
364 if (!serdev)
365 return NULL;
367 serdev->ctrl = ctrl;
368 device_initialize(&serdev->dev);
369 serdev->dev.parent = &ctrl->dev;
370 serdev->dev.bus = &serdev_bus_type;
371 serdev->dev.type = &serdev_device_type;
372 init_completion(&serdev->write_comp);
373 mutex_init(&serdev->write_lock);
374 return serdev;
376 EXPORT_SYMBOL_GPL(serdev_device_alloc);
379 * serdev_controller_alloc() - Allocate a new serdev controller
380 * @parent: parent device
381 * @size: size of private data
383 * Caller is responsible for either calling serdev_controller_add() to add the
384 * newly allocated controller, or calling serdev_controller_put() to discard it.
385 * The allocated private data region may be accessed via
386 * serdev_controller_get_drvdata()
388 struct serdev_controller *serdev_controller_alloc(struct device *parent,
389 size_t size)
391 struct serdev_controller *ctrl;
392 int id;
394 if (WARN_ON(!parent))
395 return NULL;
397 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
398 if (!ctrl)
399 return NULL;
401 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
402 if (id < 0) {
403 dev_err(parent,
404 "unable to allocate serdev controller identifier.\n");
405 goto err_free;
408 ctrl->nr = id;
410 device_initialize(&ctrl->dev);
411 ctrl->dev.type = &serdev_ctrl_type;
412 ctrl->dev.bus = &serdev_bus_type;
413 ctrl->dev.parent = parent;
414 ctrl->dev.of_node = parent->of_node;
415 serdev_controller_set_drvdata(ctrl, &ctrl[1]);
417 dev_set_name(&ctrl->dev, "serial%d", id);
419 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
420 return ctrl;
422 err_free:
423 kfree(ctrl);
425 return NULL;
427 EXPORT_SYMBOL_GPL(serdev_controller_alloc);
429 static int of_serdev_register_devices(struct serdev_controller *ctrl)
431 struct device_node *node;
432 struct serdev_device *serdev = NULL;
433 int err;
434 bool found = false;
436 for_each_available_child_of_node(ctrl->dev.of_node, node) {
437 if (!of_get_property(node, "compatible", NULL))
438 continue;
440 dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
442 serdev = serdev_device_alloc(ctrl);
443 if (!serdev)
444 continue;
446 serdev->dev.of_node = node;
448 err = serdev_device_add(serdev);
449 if (err) {
450 dev_err(&serdev->dev,
451 "failure adding device. status %d\n", err);
452 serdev_device_put(serdev);
453 } else
454 found = true;
456 if (!found)
457 return -ENODEV;
459 return 0;
462 #ifdef CONFIG_ACPI
463 static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
464 struct acpi_device *adev)
466 struct serdev_device *serdev = NULL;
467 int err;
469 if (acpi_bus_get_status(adev) || !adev->status.present ||
470 acpi_device_enumerated(adev))
471 return AE_OK;
473 serdev = serdev_device_alloc(ctrl);
474 if (!serdev) {
475 dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
476 dev_name(&adev->dev));
477 return AE_NO_MEMORY;
480 ACPI_COMPANION_SET(&serdev->dev, adev);
481 acpi_device_set_enumerated(adev);
483 err = serdev_device_add(serdev);
484 if (err) {
485 dev_err(&serdev->dev,
486 "failure adding ACPI serdev device. status %d\n", err);
487 serdev_device_put(serdev);
490 return AE_OK;
493 static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
494 void *data, void **return_value)
496 struct serdev_controller *ctrl = data;
497 struct acpi_device *adev;
499 if (acpi_bus_get_device(handle, &adev))
500 return AE_OK;
502 return acpi_serdev_register_device(ctrl, adev);
505 static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
507 acpi_status status;
508 acpi_handle handle;
510 handle = ACPI_HANDLE(ctrl->dev.parent);
511 if (!handle)
512 return -ENODEV;
514 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
515 acpi_serdev_add_device, NULL, ctrl, NULL);
516 if (ACPI_FAILURE(status))
517 dev_dbg(&ctrl->dev, "failed to enumerate serdev slaves\n");
519 if (!ctrl->serdev)
520 return -ENODEV;
522 return 0;
524 #else
525 static inline int acpi_serdev_register_devices(struct serdev_controller *ctrl)
527 return -ENODEV;
529 #endif /* CONFIG_ACPI */
532 * serdev_controller_add() - Add an serdev controller
533 * @ctrl: controller to be registered.
535 * Register a controller previously allocated via serdev_controller_alloc() with
536 * the serdev core.
538 int serdev_controller_add(struct serdev_controller *ctrl)
540 int ret_of, ret_acpi, ret;
542 /* Can't register until after driver model init */
543 if (WARN_ON(!is_registered))
544 return -EAGAIN;
546 ret = device_add(&ctrl->dev);
547 if (ret)
548 return ret;
550 ret_of = of_serdev_register_devices(ctrl);
551 ret_acpi = acpi_serdev_register_devices(ctrl);
552 if (ret_of && ret_acpi) {
553 dev_dbg(&ctrl->dev, "no devices registered: of:%d acpi:%d\n",
554 ret_of, ret_acpi);
555 ret = -ENODEV;
556 goto out_dev_del;
559 dev_dbg(&ctrl->dev, "serdev%d registered: dev:%p\n",
560 ctrl->nr, &ctrl->dev);
561 return 0;
563 out_dev_del:
564 device_del(&ctrl->dev);
565 return ret;
567 EXPORT_SYMBOL_GPL(serdev_controller_add);
569 /* Remove a device associated with a controller */
570 static int serdev_remove_device(struct device *dev, void *data)
572 struct serdev_device *serdev = to_serdev_device(dev);
573 if (dev->type == &serdev_device_type)
574 serdev_device_remove(serdev);
575 return 0;
579 * serdev_controller_remove(): remove an serdev controller
580 * @ctrl: controller to remove
582 * Remove a serdev controller. Caller is responsible for calling
583 * serdev_controller_put() to discard the allocated controller.
585 void serdev_controller_remove(struct serdev_controller *ctrl)
587 int dummy;
589 if (!ctrl)
590 return;
592 dummy = device_for_each_child(&ctrl->dev, NULL,
593 serdev_remove_device);
594 device_del(&ctrl->dev);
596 EXPORT_SYMBOL_GPL(serdev_controller_remove);
599 * serdev_driver_register() - Register client driver with serdev core
600 * @sdrv: client driver to be associated with client-device.
602 * This API will register the client driver with the serdev framework.
603 * It is typically called from the driver's module-init function.
605 int __serdev_device_driver_register(struct serdev_device_driver *sdrv, struct module *owner)
607 sdrv->driver.bus = &serdev_bus_type;
608 sdrv->driver.owner = owner;
610 /* force drivers to async probe so I/O is possible in probe */
611 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
613 return driver_register(&sdrv->driver);
615 EXPORT_SYMBOL_GPL(__serdev_device_driver_register);
617 static void __exit serdev_exit(void)
619 bus_unregister(&serdev_bus_type);
621 module_exit(serdev_exit);
623 static int __init serdev_init(void)
625 int ret;
627 ret = bus_register(&serdev_bus_type);
628 if (ret)
629 return ret;
631 is_registered = true;
632 return 0;
634 /* Must be before serial drivers register */
635 postcore_initcall(serdev_init);
637 MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
638 MODULE_LICENSE("GPL v2");
639 MODULE_DESCRIPTION("Serial attached device bus");