xref: /linux/drivers/greybus/hd.c (revision 3bd29138)
18465def4SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
28465def4SGreg Kroah-Hartman /*
38465def4SGreg Kroah-Hartman  * Greybus Host Device
48465def4SGreg Kroah-Hartman  *
58465def4SGreg Kroah-Hartman  * Copyright 2014-2015 Google Inc.
68465def4SGreg Kroah-Hartman  * Copyright 2014-2015 Linaro Ltd.
78465def4SGreg Kroah-Hartman  */
88465def4SGreg Kroah-Hartman 
98465def4SGreg Kroah-Hartman #include <linux/kernel.h>
108465def4SGreg Kroah-Hartman #include <linux/slab.h>
118465def4SGreg Kroah-Hartman #include <linux/greybus.h>
128465def4SGreg Kroah-Hartman 
138465def4SGreg Kroah-Hartman #include "greybus_trace.h"
148465def4SGreg Kroah-Hartman 
158465def4SGreg Kroah-Hartman EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_create);
168465def4SGreg Kroah-Hartman EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_release);
178465def4SGreg Kroah-Hartman EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_add);
188465def4SGreg Kroah-Hartman EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_del);
198465def4SGreg Kroah-Hartman EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_in);
208465def4SGreg Kroah-Hartman EXPORT_TRACEPOINT_SYMBOL_GPL(gb_message_submit);
218465def4SGreg Kroah-Hartman 
228465def4SGreg Kroah-Hartman static struct ida gb_hd_bus_id_map;
238465def4SGreg Kroah-Hartman 
gb_hd_output(struct gb_host_device * hd,void * req,u16 size,u8 cmd,bool async)248465def4SGreg Kroah-Hartman int gb_hd_output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
258465def4SGreg Kroah-Hartman 		 bool async)
268465def4SGreg Kroah-Hartman {
278465def4SGreg Kroah-Hartman 	if (!hd || !hd->driver || !hd->driver->output)
288465def4SGreg Kroah-Hartman 		return -EINVAL;
298465def4SGreg Kroah-Hartman 	return hd->driver->output(hd, req, size, cmd, async);
308465def4SGreg Kroah-Hartman }
318465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_hd_output);
328465def4SGreg Kroah-Hartman 
bus_id_show(struct device * dev,struct device_attribute * attr,char * buf)338465def4SGreg Kroah-Hartman static ssize_t bus_id_show(struct device *dev,
348465def4SGreg Kroah-Hartman 			   struct device_attribute *attr, char *buf)
358465def4SGreg Kroah-Hartman {
368465def4SGreg Kroah-Hartman 	struct gb_host_device *hd = to_gb_host_device(dev);
378465def4SGreg Kroah-Hartman 
388465def4SGreg Kroah-Hartman 	return sprintf(buf, "%d\n", hd->bus_id);
398465def4SGreg Kroah-Hartman }
408465def4SGreg Kroah-Hartman static DEVICE_ATTR_RO(bus_id);
418465def4SGreg Kroah-Hartman 
428465def4SGreg Kroah-Hartman static struct attribute *bus_attrs[] = {
438465def4SGreg Kroah-Hartman 	&dev_attr_bus_id.attr,
448465def4SGreg Kroah-Hartman 	NULL
458465def4SGreg Kroah-Hartman };
468465def4SGreg Kroah-Hartman ATTRIBUTE_GROUPS(bus);
478465def4SGreg Kroah-Hartman 
gb_hd_cport_reserve(struct gb_host_device * hd,u16 cport_id)488465def4SGreg Kroah-Hartman int gb_hd_cport_reserve(struct gb_host_device *hd, u16 cport_id)
498465def4SGreg Kroah-Hartman {
508465def4SGreg Kroah-Hartman 	struct ida *id_map = &hd->cport_id_map;
518465def4SGreg Kroah-Hartman 	int ret;
528465def4SGreg Kroah-Hartman 
53*3bd29138SChristophe JAILLET 	ret = ida_alloc_range(id_map, cport_id, cport_id, GFP_KERNEL);
548465def4SGreg Kroah-Hartman 	if (ret < 0) {
558465def4SGreg Kroah-Hartman 		dev_err(&hd->dev, "failed to reserve cport %u\n", cport_id);
568465def4SGreg Kroah-Hartman 		return ret;
578465def4SGreg Kroah-Hartman 	}
588465def4SGreg Kroah-Hartman 
598465def4SGreg Kroah-Hartman 	return 0;
608465def4SGreg Kroah-Hartman }
618465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_hd_cport_reserve);
628465def4SGreg Kroah-Hartman 
gb_hd_cport_release_reserved(struct gb_host_device * hd,u16 cport_id)638465def4SGreg Kroah-Hartman void gb_hd_cport_release_reserved(struct gb_host_device *hd, u16 cport_id)
648465def4SGreg Kroah-Hartman {
658465def4SGreg Kroah-Hartman 	struct ida *id_map = &hd->cport_id_map;
668465def4SGreg Kroah-Hartman 
67*3bd29138SChristophe JAILLET 	ida_free(id_map, cport_id);
688465def4SGreg Kroah-Hartman }
698465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_hd_cport_release_reserved);
708465def4SGreg Kroah-Hartman 
718465def4SGreg Kroah-Hartman /* Locking: Caller guarantees serialisation */
gb_hd_cport_allocate(struct gb_host_device * hd,int cport_id,unsigned long flags)728465def4SGreg Kroah-Hartman int gb_hd_cport_allocate(struct gb_host_device *hd, int cport_id,
738465def4SGreg Kroah-Hartman 			 unsigned long flags)
748465def4SGreg Kroah-Hartman {
758465def4SGreg Kroah-Hartman 	struct ida *id_map = &hd->cport_id_map;
768465def4SGreg Kroah-Hartman 	int ida_start, ida_end;
778465def4SGreg Kroah-Hartman 
788465def4SGreg Kroah-Hartman 	if (hd->driver->cport_allocate)
798465def4SGreg Kroah-Hartman 		return hd->driver->cport_allocate(hd, cport_id, flags);
808465def4SGreg Kroah-Hartman 
818465def4SGreg Kroah-Hartman 	if (cport_id < 0) {
828465def4SGreg Kroah-Hartman 		ida_start = 0;
83*3bd29138SChristophe JAILLET 		ida_end = hd->num_cports - 1;
848465def4SGreg Kroah-Hartman 	} else if (cport_id < hd->num_cports) {
858465def4SGreg Kroah-Hartman 		ida_start = cport_id;
86*3bd29138SChristophe JAILLET 		ida_end = cport_id;
878465def4SGreg Kroah-Hartman 	} else {
888465def4SGreg Kroah-Hartman 		dev_err(&hd->dev, "cport %d not available\n", cport_id);
898465def4SGreg Kroah-Hartman 		return -EINVAL;
908465def4SGreg Kroah-Hartman 	}
918465def4SGreg Kroah-Hartman 
92*3bd29138SChristophe JAILLET 	return ida_alloc_range(id_map, ida_start, ida_end, GFP_KERNEL);
938465def4SGreg Kroah-Hartman }
948465def4SGreg Kroah-Hartman 
958465def4SGreg Kroah-Hartman /* Locking: Caller guarantees serialisation */
gb_hd_cport_release(struct gb_host_device * hd,u16 cport_id)968465def4SGreg Kroah-Hartman void gb_hd_cport_release(struct gb_host_device *hd, u16 cport_id)
978465def4SGreg Kroah-Hartman {
988465def4SGreg Kroah-Hartman 	if (hd->driver->cport_release) {
998465def4SGreg Kroah-Hartman 		hd->driver->cport_release(hd, cport_id);
1008465def4SGreg Kroah-Hartman 		return;
1018465def4SGreg Kroah-Hartman 	}
1028465def4SGreg Kroah-Hartman 
103*3bd29138SChristophe JAILLET 	ida_free(&hd->cport_id_map, cport_id);
1048465def4SGreg Kroah-Hartman }
1058465def4SGreg Kroah-Hartman 
gb_hd_release(struct device * dev)1068465def4SGreg Kroah-Hartman static void gb_hd_release(struct device *dev)
1078465def4SGreg Kroah-Hartman {
1088465def4SGreg Kroah-Hartman 	struct gb_host_device *hd = to_gb_host_device(dev);
1098465def4SGreg Kroah-Hartman 
1108465def4SGreg Kroah-Hartman 	trace_gb_hd_release(hd);
1118465def4SGreg Kroah-Hartman 
1128465def4SGreg Kroah-Hartman 	if (hd->svc)
1138465def4SGreg Kroah-Hartman 		gb_svc_put(hd->svc);
114*3bd29138SChristophe JAILLET 	ida_free(&gb_hd_bus_id_map, hd->bus_id);
1158465def4SGreg Kroah-Hartman 	ida_destroy(&hd->cport_id_map);
1168465def4SGreg Kroah-Hartman 	kfree(hd);
1178465def4SGreg Kroah-Hartman }
1188465def4SGreg Kroah-Hartman 
119e869b72bSRicardo B. Marliere const struct device_type greybus_hd_type = {
1208465def4SGreg Kroah-Hartman 	.name		= "greybus_host_device",
1218465def4SGreg Kroah-Hartman 	.release	= gb_hd_release,
1228465def4SGreg Kroah-Hartman };
1238465def4SGreg Kroah-Hartman 
gb_hd_create(struct gb_hd_driver * driver,struct device * parent,size_t buffer_size_max,size_t num_cports)1248465def4SGreg Kroah-Hartman struct gb_host_device *gb_hd_create(struct gb_hd_driver *driver,
1258465def4SGreg Kroah-Hartman 				    struct device *parent,
1268465def4SGreg Kroah-Hartman 				    size_t buffer_size_max,
1278465def4SGreg Kroah-Hartman 				    size_t num_cports)
1288465def4SGreg Kroah-Hartman {
1298465def4SGreg Kroah-Hartman 	struct gb_host_device *hd;
1308465def4SGreg Kroah-Hartman 	int ret;
1318465def4SGreg Kroah-Hartman 
1328465def4SGreg Kroah-Hartman 	/*
1338465def4SGreg Kroah-Hartman 	 * Validate that the driver implements all of the callbacks
1348465def4SGreg Kroah-Hartman 	 * so that we don't have to every time we make them.
1358465def4SGreg Kroah-Hartman 	 */
1368465def4SGreg Kroah-Hartman 	if ((!driver->message_send) || (!driver->message_cancel)) {
1378465def4SGreg Kroah-Hartman 		dev_err(parent, "mandatory hd-callbacks missing\n");
1388465def4SGreg Kroah-Hartman 		return ERR_PTR(-EINVAL);
1398465def4SGreg Kroah-Hartman 	}
1408465def4SGreg Kroah-Hartman 
1418465def4SGreg Kroah-Hartman 	if (buffer_size_max < GB_OPERATION_MESSAGE_SIZE_MIN) {
1428465def4SGreg Kroah-Hartman 		dev_err(parent, "greybus host-device buffers too small\n");
1438465def4SGreg Kroah-Hartman 		return ERR_PTR(-EINVAL);
1448465def4SGreg Kroah-Hartman 	}
1458465def4SGreg Kroah-Hartman 
1468465def4SGreg Kroah-Hartman 	if (num_cports == 0 || num_cports > CPORT_ID_MAX + 1) {
1478465def4SGreg Kroah-Hartman 		dev_err(parent, "Invalid number of CPorts: %zu\n", num_cports);
1488465def4SGreg Kroah-Hartman 		return ERR_PTR(-EINVAL);
1498465def4SGreg Kroah-Hartman 	}
1508465def4SGreg Kroah-Hartman 
1518465def4SGreg Kroah-Hartman 	/*
1528465def4SGreg Kroah-Hartman 	 * Make sure to never allocate messages larger than what the Greybus
1538465def4SGreg Kroah-Hartman 	 * protocol supports.
1548465def4SGreg Kroah-Hartman 	 */
1558465def4SGreg Kroah-Hartman 	if (buffer_size_max > GB_OPERATION_MESSAGE_SIZE_MAX) {
1568465def4SGreg Kroah-Hartman 		dev_warn(parent, "limiting buffer size to %u\n",
1578465def4SGreg Kroah-Hartman 			 GB_OPERATION_MESSAGE_SIZE_MAX);
1588465def4SGreg Kroah-Hartman 		buffer_size_max = GB_OPERATION_MESSAGE_SIZE_MAX;
1598465def4SGreg Kroah-Hartman 	}
1608465def4SGreg Kroah-Hartman 
1618465def4SGreg Kroah-Hartman 	hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
1628465def4SGreg Kroah-Hartman 	if (!hd)
1638465def4SGreg Kroah-Hartman 		return ERR_PTR(-ENOMEM);
1648465def4SGreg Kroah-Hartman 
165*3bd29138SChristophe JAILLET 	ret = ida_alloc_min(&gb_hd_bus_id_map, 1, GFP_KERNEL);
1668465def4SGreg Kroah-Hartman 	if (ret < 0) {
1678465def4SGreg Kroah-Hartman 		kfree(hd);
1688465def4SGreg Kroah-Hartman 		return ERR_PTR(ret);
1698465def4SGreg Kroah-Hartman 	}
1708465def4SGreg Kroah-Hartman 	hd->bus_id = ret;
1718465def4SGreg Kroah-Hartman 
1728465def4SGreg Kroah-Hartman 	hd->driver = driver;
1738465def4SGreg Kroah-Hartman 	INIT_LIST_HEAD(&hd->modules);
1748465def4SGreg Kroah-Hartman 	INIT_LIST_HEAD(&hd->connections);
1758465def4SGreg Kroah-Hartman 	ida_init(&hd->cport_id_map);
1768465def4SGreg Kroah-Hartman 	hd->buffer_size_max = buffer_size_max;
1778465def4SGreg Kroah-Hartman 	hd->num_cports = num_cports;
1788465def4SGreg Kroah-Hartman 
1798465def4SGreg Kroah-Hartman 	hd->dev.parent = parent;
1808465def4SGreg Kroah-Hartman 	hd->dev.bus = &greybus_bus_type;
1818465def4SGreg Kroah-Hartman 	hd->dev.type = &greybus_hd_type;
1828465def4SGreg Kroah-Hartman 	hd->dev.groups = bus_groups;
1838465def4SGreg Kroah-Hartman 	hd->dev.dma_mask = hd->dev.parent->dma_mask;
1848465def4SGreg Kroah-Hartman 	device_initialize(&hd->dev);
1858465def4SGreg Kroah-Hartman 	dev_set_name(&hd->dev, "greybus%d", hd->bus_id);
1868465def4SGreg Kroah-Hartman 
1878465def4SGreg Kroah-Hartman 	trace_gb_hd_create(hd);
1888465def4SGreg Kroah-Hartman 
1898465def4SGreg Kroah-Hartman 	hd->svc = gb_svc_create(hd);
1908465def4SGreg Kroah-Hartman 	if (!hd->svc) {
1918465def4SGreg Kroah-Hartman 		dev_err(&hd->dev, "failed to create svc\n");
1928465def4SGreg Kroah-Hartman 		put_device(&hd->dev);
1938465def4SGreg Kroah-Hartman 		return ERR_PTR(-ENOMEM);
1948465def4SGreg Kroah-Hartman 	}
1958465def4SGreg Kroah-Hartman 
1968465def4SGreg Kroah-Hartman 	return hd;
1978465def4SGreg Kroah-Hartman }
1988465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_hd_create);
1998465def4SGreg Kroah-Hartman 
gb_hd_add(struct gb_host_device * hd)2008465def4SGreg Kroah-Hartman int gb_hd_add(struct gb_host_device *hd)
2018465def4SGreg Kroah-Hartman {
2028465def4SGreg Kroah-Hartman 	int ret;
2038465def4SGreg Kroah-Hartman 
2048465def4SGreg Kroah-Hartman 	ret = device_add(&hd->dev);
2058465def4SGreg Kroah-Hartman 	if (ret)
2068465def4SGreg Kroah-Hartman 		return ret;
2078465def4SGreg Kroah-Hartman 
2088465def4SGreg Kroah-Hartman 	ret = gb_svc_add(hd->svc);
2098465def4SGreg Kroah-Hartman 	if (ret) {
2108465def4SGreg Kroah-Hartman 		device_del(&hd->dev);
2118465def4SGreg Kroah-Hartman 		return ret;
2128465def4SGreg Kroah-Hartman 	}
2138465def4SGreg Kroah-Hartman 
2148465def4SGreg Kroah-Hartman 	trace_gb_hd_add(hd);
2158465def4SGreg Kroah-Hartman 
2168465def4SGreg Kroah-Hartman 	return 0;
2178465def4SGreg Kroah-Hartman }
2188465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_hd_add);
2198465def4SGreg Kroah-Hartman 
gb_hd_del(struct gb_host_device * hd)2208465def4SGreg Kroah-Hartman void gb_hd_del(struct gb_host_device *hd)
2218465def4SGreg Kroah-Hartman {
2228465def4SGreg Kroah-Hartman 	trace_gb_hd_del(hd);
2238465def4SGreg Kroah-Hartman 
2248465def4SGreg Kroah-Hartman 	/*
2258465def4SGreg Kroah-Hartman 	 * Tear down the svc and flush any on-going hotplug processing before
2268465def4SGreg Kroah-Hartman 	 * removing the remaining interfaces.
2278465def4SGreg Kroah-Hartman 	 */
2288465def4SGreg Kroah-Hartman 	gb_svc_del(hd->svc);
2298465def4SGreg Kroah-Hartman 
2308465def4SGreg Kroah-Hartman 	device_del(&hd->dev);
2318465def4SGreg Kroah-Hartman }
2328465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_hd_del);
2338465def4SGreg Kroah-Hartman 
gb_hd_shutdown(struct gb_host_device * hd)2348465def4SGreg Kroah-Hartman void gb_hd_shutdown(struct gb_host_device *hd)
2358465def4SGreg Kroah-Hartman {
2368465def4SGreg Kroah-Hartman 	gb_svc_del(hd->svc);
2378465def4SGreg Kroah-Hartman }
2388465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_hd_shutdown);
2398465def4SGreg Kroah-Hartman 
gb_hd_put(struct gb_host_device * hd)2408465def4SGreg Kroah-Hartman void gb_hd_put(struct gb_host_device *hd)
2418465def4SGreg Kroah-Hartman {
2428465def4SGreg Kroah-Hartman 	put_device(&hd->dev);
2438465def4SGreg Kroah-Hartman }
2448465def4SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(gb_hd_put);
2458465def4SGreg Kroah-Hartman 
gb_hd_init(void)2468465def4SGreg Kroah-Hartman int __init gb_hd_init(void)
2478465def4SGreg Kroah-Hartman {
2488465def4SGreg Kroah-Hartman 	ida_init(&gb_hd_bus_id_map);
2498465def4SGreg Kroah-Hartman 
2508465def4SGreg Kroah-Hartman 	return 0;
2518465def4SGreg Kroah-Hartman }
2528465def4SGreg Kroah-Hartman 
gb_hd_exit(void)2538465def4SGreg Kroah-Hartman void gb_hd_exit(void)
2548465def4SGreg Kroah-Hartman {
2558465def4SGreg Kroah-Hartman 	ida_destroy(&gb_hd_bus_id_map);
2568465def4SGreg Kroah-Hartman }
257