drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / i2c / busses / i2c-virtio.c
blob2a351f961b89932f770db00d22fc78ee35c2f655
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Virtio I2C Bus Driver
5 * The Virtio I2C Specification:
6 * https://raw.githubusercontent.com/oasis-tcs/virtio-spec/master/virtio-i2c.tex
8 * Copyright (c) 2021 Intel Corporation. All rights reserved.
9 */
11 #include <linux/acpi.h>
12 #include <linux/completion.h>
13 #include <linux/err.h>
14 #include <linux/i2c.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/virtio.h>
18 #include <linux/virtio_ids.h>
19 #include <linux/virtio_config.h>
20 #include <linux/virtio_i2c.h>
22 /**
23 * struct virtio_i2c - virtio I2C data
24 * @vdev: virtio device for this controller
25 * @adap: I2C adapter for this controller
26 * @vq: the virtio virtqueue for communication
28 struct virtio_i2c {
29 struct virtio_device *vdev;
30 struct i2c_adapter adap;
31 struct virtqueue *vq;
34 /**
35 * struct virtio_i2c_req - the virtio I2C request structure
36 * @completion: completion of virtio I2C message
37 * @out_hdr: the OUT header of the virtio I2C message
38 * @buf: the buffer into which data is read, or from which it's written
39 * @in_hdr: the IN header of the virtio I2C message
41 struct virtio_i2c_req {
42 struct completion completion;
43 struct virtio_i2c_out_hdr out_hdr ____cacheline_aligned;
44 uint8_t *buf ____cacheline_aligned;
45 struct virtio_i2c_in_hdr in_hdr ____cacheline_aligned;
48 static void virtio_i2c_msg_done(struct virtqueue *vq)
50 struct virtio_i2c_req *req;
51 unsigned int len;
53 while ((req = virtqueue_get_buf(vq, &len)))
54 complete(&req->completion);
57 static int virtio_i2c_prepare_reqs(struct virtqueue *vq,
58 struct virtio_i2c_req *reqs,
59 struct i2c_msg *msgs, int num)
61 struct scatterlist *sgs[3], out_hdr, msg_buf, in_hdr;
62 int i;
64 for (i = 0; i < num; i++) {
65 int outcnt = 0, incnt = 0;
67 init_completion(&reqs[i].completion);
70 * Only 7-bit mode supported for this moment. For the address
71 * format, Please check the Virtio I2C Specification.
73 reqs[i].out_hdr.addr = cpu_to_le16(msgs[i].addr << 1);
75 if (msgs[i].flags & I2C_M_RD)
76 reqs[i].out_hdr.flags |= cpu_to_le32(VIRTIO_I2C_FLAGS_M_RD);
78 if (i != num - 1)
79 reqs[i].out_hdr.flags |= cpu_to_le32(VIRTIO_I2C_FLAGS_FAIL_NEXT);
81 sg_init_one(&out_hdr, &reqs[i].out_hdr, sizeof(reqs[i].out_hdr));
82 sgs[outcnt++] = &out_hdr;
84 if (msgs[i].len) {
85 reqs[i].buf = i2c_get_dma_safe_msg_buf(&msgs[i], 1);
86 if (!reqs[i].buf)
87 break;
89 sg_init_one(&msg_buf, reqs[i].buf, msgs[i].len);
91 if (msgs[i].flags & I2C_M_RD)
92 sgs[outcnt + incnt++] = &msg_buf;
93 else
94 sgs[outcnt++] = &msg_buf;
97 sg_init_one(&in_hdr, &reqs[i].in_hdr, sizeof(reqs[i].in_hdr));
98 sgs[outcnt + incnt++] = &in_hdr;
100 if (virtqueue_add_sgs(vq, sgs, outcnt, incnt, &reqs[i], GFP_KERNEL)) {
101 i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], false);
102 break;
106 return i;
109 static int virtio_i2c_complete_reqs(struct virtqueue *vq,
110 struct virtio_i2c_req *reqs,
111 struct i2c_msg *msgs, int num)
113 bool failed = false;
114 int i, j = 0;
116 for (i = 0; i < num; i++) {
117 struct virtio_i2c_req *req = &reqs[i];
119 wait_for_completion(&req->completion);
121 if (!failed && req->in_hdr.status != VIRTIO_I2C_MSG_OK)
122 failed = true;
124 i2c_put_dma_safe_msg_buf(reqs[i].buf, &msgs[i], !failed);
126 if (!failed)
127 j++;
130 return j;
133 static int virtio_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
134 int num)
136 struct virtio_i2c *vi = i2c_get_adapdata(adap);
137 struct virtqueue *vq = vi->vq;
138 struct virtio_i2c_req *reqs;
139 int count;
141 reqs = kcalloc(num, sizeof(*reqs), GFP_KERNEL);
142 if (!reqs)
143 return -ENOMEM;
145 count = virtio_i2c_prepare_reqs(vq, reqs, msgs, num);
146 if (!count)
147 goto err_free;
150 * For the case where count < num, i.e. we weren't able to queue all the
151 * msgs, ideally we should abort right away and return early, but some
152 * of the messages are already sent to the remote I2C controller and the
153 * virtqueue will be left in undefined state in that case. We kick the
154 * remote here to clear the virtqueue, so we can try another set of
155 * messages later on.
157 virtqueue_kick(vq);
159 count = virtio_i2c_complete_reqs(vq, reqs, msgs, count);
161 err_free:
162 kfree(reqs);
163 return count;
166 static void virtio_i2c_del_vqs(struct virtio_device *vdev)
168 virtio_reset_device(vdev);
169 vdev->config->del_vqs(vdev);
172 static int virtio_i2c_setup_vqs(struct virtio_i2c *vi)
174 struct virtio_device *vdev = vi->vdev;
176 vi->vq = virtio_find_single_vq(vdev, virtio_i2c_msg_done, "msg");
177 return PTR_ERR_OR_ZERO(vi->vq);
180 static u32 virtio_i2c_func(struct i2c_adapter *adap)
182 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
185 static const struct i2c_algorithm virtio_algorithm = {
186 .xfer = virtio_i2c_xfer,
187 .functionality = virtio_i2c_func,
190 static int virtio_i2c_probe(struct virtio_device *vdev)
192 struct virtio_i2c *vi;
193 int ret;
195 if (!virtio_has_feature(vdev, VIRTIO_I2C_F_ZERO_LENGTH_REQUEST)) {
196 dev_err(&vdev->dev, "Zero-length request feature is mandatory\n");
197 return -EINVAL;
200 vi = devm_kzalloc(&vdev->dev, sizeof(*vi), GFP_KERNEL);
201 if (!vi)
202 return -ENOMEM;
204 vdev->priv = vi;
205 vi->vdev = vdev;
207 ret = virtio_i2c_setup_vqs(vi);
208 if (ret)
209 return ret;
211 vi->adap.owner = THIS_MODULE;
212 snprintf(vi->adap.name, sizeof(vi->adap.name),
213 "i2c_virtio at virtio bus %d", vdev->index);
214 vi->adap.algo = &virtio_algorithm;
215 vi->adap.dev.parent = &vdev->dev;
216 vi->adap.dev.of_node = vdev->dev.of_node;
217 i2c_set_adapdata(&vi->adap, vi);
220 * Setup ACPI node for controlled devices which will be probed through
221 * ACPI.
223 ACPI_COMPANION_SET(&vi->adap.dev, ACPI_COMPANION(vdev->dev.parent));
225 ret = i2c_add_adapter(&vi->adap);
226 if (ret)
227 virtio_i2c_del_vqs(vdev);
229 return ret;
232 static void virtio_i2c_remove(struct virtio_device *vdev)
234 struct virtio_i2c *vi = vdev->priv;
236 i2c_del_adapter(&vi->adap);
237 virtio_i2c_del_vqs(vdev);
240 static const struct virtio_device_id id_table[] = {
241 { VIRTIO_ID_I2C_ADAPTER, VIRTIO_DEV_ANY_ID },
244 MODULE_DEVICE_TABLE(virtio, id_table);
246 static int virtio_i2c_freeze(struct virtio_device *vdev)
248 virtio_i2c_del_vqs(vdev);
249 return 0;
252 static int virtio_i2c_restore(struct virtio_device *vdev)
254 return virtio_i2c_setup_vqs(vdev->priv);
257 static const unsigned int features[] = {
258 VIRTIO_I2C_F_ZERO_LENGTH_REQUEST,
261 static struct virtio_driver virtio_i2c_driver = {
262 .feature_table = features,
263 .feature_table_size = ARRAY_SIZE(features),
264 .id_table = id_table,
265 .probe = virtio_i2c_probe,
266 .remove = virtio_i2c_remove,
267 .driver = {
268 .name = "i2c_virtio",
270 .freeze = pm_sleep_ptr(virtio_i2c_freeze),
271 .restore = pm_sleep_ptr(virtio_i2c_restore),
273 module_virtio_driver(virtio_i2c_driver);
275 MODULE_AUTHOR("Jie Deng <jie.deng@intel.com>");
276 MODULE_AUTHOR("Conghui Chen <conghui.chen@intel.com>");
277 MODULE_DESCRIPTION("Virtio i2c bus driver");
278 MODULE_LICENSE("GPL");