Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Industry-pack bus support functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2011-2012 CERN (www.cern.ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/ipack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #define to_ipack_dev(device) container_of(device, struct ipack_device, dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define to_ipack_driver(drv) container_of(drv, struct ipack_driver, driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static DEFINE_IDA(ipack_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static void ipack_device_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	struct ipack_device *device = to_ipack_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	kfree(device->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	device->release(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static inline const struct ipack_device_id *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) ipack_match_one_device(const struct ipack_device_id *id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		       const struct ipack_device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	if ((id->format == IPACK_ANY_FORMAT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 				id->format == device->id_format) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	    (id->vendor == IPACK_ANY_ID || id->vendor == device->id_vendor) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	    (id->device == IPACK_ANY_ID || id->device == device->id_device))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static const struct ipack_device_id *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) ipack_match_id(const struct ipack_device_id *ids, struct ipack_device *idev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	if (ids) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		while (ids->vendor || ids->device) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			if (ipack_match_one_device(ids, idev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 				return ids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 			ids++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static int ipack_bus_match(struct device *dev, struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct ipack_device *idev = to_ipack_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct ipack_driver *idrv = to_ipack_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	const struct ipack_device_id *found_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	found_id = ipack_match_id(idrv->id_table, idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	return found_id ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static int ipack_bus_probe(struct device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	struct ipack_device *dev = to_ipack_dev(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	struct ipack_driver *drv = to_ipack_driver(device->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (!drv->ops->probe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	return drv->ops->probe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static int ipack_bus_remove(struct device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct ipack_device *dev = to_ipack_dev(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct ipack_driver *drv = to_ipack_driver(device->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (!drv->ops->remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	drv->ops->remove(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static int ipack_uevent(struct device *dev, struct kobj_uevent_env *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct ipack_device *idev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	idev = to_ipack_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	if (add_uevent_var(env,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			   "MODALIAS=ipack:f%02Xv%08Xd%08X", idev->id_format,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			   idev->id_vendor, idev->id_device))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #define ipack_device_attr(field, format_string)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static ssize_t								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) field##_show(struct device *dev, struct device_attribute *attr,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		char *buf)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct ipack_device *idev = to_ipack_dev(dev);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return sprintf(buf, format_string, idev->field);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static ssize_t id_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		       struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	unsigned int i, c, l, s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct ipack_device *idev = to_ipack_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	switch (idev->id_format) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	case IPACK_ID_VERSION_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		l = 0x7; s = 1; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	case IPACK_ID_VERSION_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		l = 0xf; s = 2; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	c = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	for (i = 0; i < idev->id_avail; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		if (i > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			if ((i & l) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 				buf[c++] = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			else if ((i & s) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 				buf[c++] = ' ';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		sprintf(&buf[c], "%02x", idev->id[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		c += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	buf[c++] = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	return c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) id_vendor_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	struct ipack_device *idev = to_ipack_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	switch (idev->id_format) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	case IPACK_ID_VERSION_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		return sprintf(buf, "0x%02x\n", idev->id_vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	case IPACK_ID_VERSION_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return sprintf(buf, "0x%06x\n", idev->id_vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) id_device_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	struct ipack_device *idev = to_ipack_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	switch (idev->id_format) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	case IPACK_ID_VERSION_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		return sprintf(buf, "0x%02x\n", idev->id_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	case IPACK_ID_VERSION_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		return sprintf(buf, "0x%04x\n", idev->id_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			     char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct ipack_device *idev = to_ipack_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	return sprintf(buf, "ipac:f%02Xv%08Xd%08X", idev->id_format,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		       idev->id_vendor, idev->id_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ipack_device_attr(id_format, "0x%hhx\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static DEVICE_ATTR_RO(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static DEVICE_ATTR_RO(id_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static DEVICE_ATTR_RO(id_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static DEVICE_ATTR_RO(id_vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static DEVICE_ATTR_RO(modalias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static struct attribute *ipack_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	&dev_attr_id.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	&dev_attr_id_device.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	&dev_attr_id_format.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	&dev_attr_id_vendor.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	&dev_attr_modalias.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ATTRIBUTE_GROUPS(ipack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static struct bus_type ipack_bus_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	.name      = "ipack",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	.probe     = ipack_bus_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	.match     = ipack_bus_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	.remove    = ipack_bus_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	.dev_groups = ipack_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	.uevent	   = ipack_uevent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct ipack_bus_device *ipack_bus_register(struct device *parent, int slots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 					    const struct ipack_bus_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 					    struct module *owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	int bus_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	struct ipack_bus_device *bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	bus = kzalloc(sizeof(*bus), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (!bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	bus_nr = ida_simple_get(&ipack_ida, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	if (bus_nr < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		kfree(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	bus->bus_nr = bus_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	bus->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	bus->slots = slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	bus->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	bus->owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	return bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) EXPORT_SYMBOL_GPL(ipack_bus_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static int ipack_unregister_bus_member(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	struct ipack_device *idev = to_ipack_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	struct ipack_bus_device *bus = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (idev->bus == bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		ipack_device_del(idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) int ipack_bus_unregister(struct ipack_bus_device *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	bus_for_each_dev(&ipack_bus_type, NULL, bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		ipack_unregister_bus_member);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	ida_simple_remove(&ipack_ida, bus->bus_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	kfree(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) EXPORT_SYMBOL_GPL(ipack_bus_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) int ipack_driver_register(struct ipack_driver *edrv, struct module *owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			  const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	edrv->driver.owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	edrv->driver.name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	edrv->driver.bus = &ipack_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	return driver_register(&edrv->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) EXPORT_SYMBOL_GPL(ipack_driver_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) void ipack_driver_unregister(struct ipack_driver *edrv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	driver_unregister(&edrv->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) EXPORT_SYMBOL_GPL(ipack_driver_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static u16 ipack_crc_byte(u16 crc, u8 c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	crc ^= c << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	for (i = 0; i < 8; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		crc = (crc << 1) ^ ((crc & 0x8000) ? 0x1021 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	return crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)  * The algorithm in lib/crc-ccitt.c does not seem to apply since it uses the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)  * opposite bit ordering.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static u8 ipack_calc_crc1(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	u8 c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	u16 crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	crc = 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	for (i = 0; i < dev->id_avail; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		c = (i != 11) ? dev->id[i] : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		crc = ipack_crc_byte(crc, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	crc = ~crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	return crc & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static u16 ipack_calc_crc2(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	u8 c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	u16 crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	crc = 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	for (i = 0; i < dev->id_avail; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		c = ((i != 0x18) && (i != 0x19)) ? dev->id[i] : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		crc = ipack_crc_byte(crc, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	crc = ~crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	return crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static void ipack_parse_id1(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	u8 *id = dev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	u8 crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	dev->id_vendor = id[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	dev->id_device = id[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	dev->speed_8mhz = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	dev->speed_32mhz = (id[7] == 'H');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	crc = ipack_calc_crc1(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	dev->id_crc_correct = (crc == id[11]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (!dev->id_crc_correct) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		dev_warn(&dev->dev, "ID CRC invalid found 0x%x, expected 0x%x.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 				id[11], crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static void ipack_parse_id2(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	__be16 *id = (__be16 *) dev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	u16 flags, crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	dev->id_vendor = ((be16_to_cpu(id[3]) & 0xff) << 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			 + be16_to_cpu(id[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	dev->id_device = be16_to_cpu(id[5]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	flags = be16_to_cpu(id[10]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	dev->speed_8mhz = !!(flags & 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	dev->speed_32mhz = !!(flags & 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	crc = ipack_calc_crc2(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	dev->id_crc_correct = (crc == be16_to_cpu(id[12]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (!dev->id_crc_correct) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		dev_warn(&dev->dev, "ID CRC invalid found 0x%x, expected 0x%x.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 				id[11], crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static int ipack_device_read_id(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	u8 __iomem *idmem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	idmem = ioremap(dev->region[IPACK_ID_SPACE].start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			dev->region[IPACK_ID_SPACE].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (!idmem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		dev_err(&dev->dev, "error mapping memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	/* Determine ID PROM Data Format.  If we find the ids "IPAC" or "IPAH"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	 * we are dealing with a IndustryPack  format 1 device.  If we detect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	 * "VITA4 " (16 bit big endian formatted) we are dealing with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	 * IndustryPack format 2 device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	if ((ioread8(idmem + 1) == 'I') &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			(ioread8(idmem + 3) == 'P') &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			(ioread8(idmem + 5) == 'A') &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			((ioread8(idmem + 7) == 'C') ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			 (ioread8(idmem + 7) == 'H'))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		dev->id_format = IPACK_ID_VERSION_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		dev->id_avail = ioread8(idmem + 0x15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		if ((dev->id_avail < 0x0c) || (dev->id_avail > 0x40)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 			dev_warn(&dev->dev, "invalid id size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			dev->id_avail = 0x0c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	} else if ((ioread8(idmem + 0) == 'I') &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 			(ioread8(idmem + 1) == 'V') &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 			(ioread8(idmem + 2) == 'A') &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 			(ioread8(idmem + 3) == 'T') &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			(ioread8(idmem + 4) == ' ') &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			(ioread8(idmem + 5) == '4')) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		dev->id_format = IPACK_ID_VERSION_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		dev->id_avail = ioread16be(idmem + 0x16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		if ((dev->id_avail < 0x1a) || (dev->id_avail > 0x40)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 			dev_warn(&dev->dev, "invalid id size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			dev->id_avail = 0x1a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		dev->id_format = IPACK_ID_VERSION_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		dev->id_avail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	if (!dev->id_avail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	/* Obtain the amount of memory required to store a copy of the complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	 * ID ROM contents */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	dev->id = kmalloc(dev->id_avail, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	if (!dev->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	for (i = 0; i < dev->id_avail; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		if (dev->id_format == IPACK_ID_VERSION_1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			dev->id[i] = ioread8(idmem + (i << 1) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			dev->id[i] = ioread8(idmem + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	/* now we can finally work with the copy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	switch (dev->id_format) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	case IPACK_ID_VERSION_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		ipack_parse_id1(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	case IPACK_ID_VERSION_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		ipack_parse_id2(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	iounmap(idmem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) int ipack_device_init(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	dev->dev.bus = &ipack_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	dev->dev.release = ipack_device_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	dev->dev.parent = dev->bus->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	dev_set_name(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		     "ipack-dev.%u.%u", dev->bus->bus_nr, dev->slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	device_initialize(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	if (dev->bus->ops->set_clockrate(dev, 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		dev_warn(&dev->dev, "failed to switch to 8 MHz operation for reading of device ID.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	if (dev->bus->ops->reset_timeout(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		dev_warn(&dev->dev, "failed to reset potential timeout.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	ret = ipack_device_read_id(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		dev_err(&dev->dev, "error reading device id section.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	/* if the device supports 32 MHz operation, use it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	if (dev->speed_32mhz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		ret = dev->bus->ops->set_clockrate(dev, 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			dev_err(&dev->dev, "failed to switch to 32 MHz operation.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) EXPORT_SYMBOL_GPL(ipack_device_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) int ipack_device_add(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	return device_add(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) EXPORT_SYMBOL_GPL(ipack_device_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) void ipack_device_del(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	device_del(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	ipack_put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) EXPORT_SYMBOL_GPL(ipack_device_del);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) void ipack_get_device(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	get_device(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) EXPORT_SYMBOL_GPL(ipack_get_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) void ipack_put_device(struct ipack_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	put_device(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) EXPORT_SYMBOL_GPL(ipack_put_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) static int __init ipack_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	ida_init(&ipack_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	return bus_register(&ipack_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static void __exit ipack_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	bus_unregister(&ipack_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	ida_destroy(&ipack_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) module_init(ipack_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) module_exit(ipack_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) MODULE_AUTHOR("Samuel Iglesias Gonsalvez <siglesias@igalia.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) MODULE_DESCRIPTION("Industry-pack bus core");