]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - drivers/i2c/i2c-core-base.c
i2c: add extra check to safe DMA buffer helper
[mirror_ubuntu-bionic-kernel.git] / drivers / i2c / i2c-core-base.c
index 706164b4c5be46bf2f98aa5a8ddde44800eeae0e..16072efec762f9e7a7596e478244fd7d48aa58f3 100644 (file)
@@ -821,8 +821,12 @@ void i2c_unregister_device(struct i2c_client *client)
 {
        if (!client)
                return;
-       if (client->dev.of_node)
+
+       if (client->dev.of_node) {
                of_node_clear_flag(client->dev.of_node, OF_POPULATED);
+               of_node_put(client->dev.of_node);
+       }
+
        if (ACPI_COMPANION(&client->dev))
                acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
        device_unregister(&client->dev);
@@ -2261,6 +2265,57 @@ void i2c_put_adapter(struct i2c_adapter *adap)
 }
 EXPORT_SYMBOL(i2c_put_adapter);
 
+/**
+ * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg
+ * @msg: the message to be checked
+ * @threshold: the minimum number of bytes for which using DMA makes sense.
+ *            Should at least be 1.
+ *
+ * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.
+ *        Or a valid pointer to be used with DMA. After use, release it by
+ *        calling i2c_release_dma_safe_msg_buf().
+ *
+ * This function must only be called from process context!
+ */
+u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)
+{
+       /* also skip 0-length msgs for bogus thresholds of 0 */
+       if (!threshold)
+               pr_debug("DMA buffer for addr=0x%02x with length 0 is bogus\n",
+                        msg->addr);
+       if (msg->len < threshold || msg->len == 0)
+               return NULL;
+
+       if (msg->flags & I2C_M_DMA_SAFE)
+               return msg->buf;
+
+       pr_debug("using bounce buffer for addr=0x%02x, len=%d\n",
+                msg->addr, msg->len);
+
+       if (msg->flags & I2C_M_RD)
+               return kzalloc(msg->len, GFP_KERNEL);
+       else
+               return kmemdup(msg->buf, msg->len, GFP_KERNEL);
+}
+EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf);
+
+/**
+ * i2c_release_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
+ * @msg: the message to be synced with
+ * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.
+ */
+void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf)
+{
+       if (!buf || buf == msg->buf)
+               return;
+
+       if (msg->flags & I2C_M_RD)
+               memcpy(msg->buf, buf, msg->len);
+
+       kfree(buf);
+}
+EXPORT_SYMBOL_GPL(i2c_release_dma_safe_msg_buf);
+
 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
 MODULE_DESCRIPTION("I2C-Bus main module");
 MODULE_LICENSE("GPL");