1 /*
2 * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <misc.h>
11
12 /*
13 * Implement a miscellaneous uclass for those do not fit other more
14 * general classes. A set of generic read, write and ioctl methods may
15 * be used to access the device.
16 */
17
misc_read(struct udevice * dev,int offset,void * buf,int size)18 int misc_read(struct udevice *dev, int offset, void *buf, int size)
19 {
20 const struct misc_ops *ops = device_get_ops(dev);
21
22 if (!ops->read)
23 return -ENOSYS;
24
25 return ops->read(dev, offset, buf, size);
26 }
27
misc_write(struct udevice * dev,int offset,void * buf,int size)28 int misc_write(struct udevice *dev, int offset, void *buf, int size)
29 {
30 const struct misc_ops *ops = device_get_ops(dev);
31
32 if (!ops->write)
33 return -ENOSYS;
34
35 return ops->write(dev, offset, buf, size);
36 }
37
misc_ioctl(struct udevice * dev,unsigned long request,void * buf)38 int misc_ioctl(struct udevice *dev, unsigned long request, void *buf)
39 {
40 const struct misc_ops *ops = device_get_ops(dev);
41
42 if (!ops->ioctl)
43 return -ENOSYS;
44
45 return ops->ioctl(dev, request, buf);
46 }
47
misc_call(struct udevice * dev,int msgid,void * tx_msg,int tx_size,void * rx_msg,int rx_size)48 int misc_call(struct udevice *dev, int msgid, void *tx_msg, int tx_size,
49 void *rx_msg, int rx_size)
50 {
51 const struct misc_ops *ops = device_get_ops(dev);
52
53 if (!ops->call)
54 return -ENOSYS;
55
56 return ops->call(dev, msgid, tx_msg, tx_size, rx_msg, rx_size);
57 }
58
misc_get_device_by_capability(u32 capability)59 struct udevice *misc_get_device_by_capability(u32 capability)
60 {
61 const struct misc_ops *ops;
62 struct udevice *dev;
63 struct uclass *uc;
64 int ret;
65 u32 cap;
66
67 ret = uclass_get(UCLASS_MISC, &uc);
68 if (ret)
69 return NULL;
70
71 for (uclass_first_device(UCLASS_MISC, &dev);
72 dev;
73 uclass_next_device(&dev)) {
74 ops = device_get_ops(dev);
75 if (!ops || !ops->ioctl)
76 continue;
77
78 ret = ops->ioctl(dev, IOCTL_REQ_CAPABILITY, &cap);
79 if (!ret && ((cap & capability) == capability))
80 return dev;
81 }
82
83 return NULL;
84 }
85
86 UCLASS_DRIVER(misc) = {
87 .id = UCLASS_MISC,
88 .name = "misc",
89 };
90