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