blob: af47772da5158b76dc6ff4f252c907611b76c365 [file] [log] [blame]
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +02001/*
2 * Industry-pack bus support functions.
3 *
4 * (C) 2011 Samuel Iglesias Gonsalvez <siglesia@cern.ch>, CERN
5 * (C) 2012 Samuel Iglesias Gonsalvez <siglesias@igalia.com>, Igalia
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
Samuel Iglesias Gonsalvez416289b2012-05-11 10:17:13 +02009 * Software Foundation; version 2 of the License.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020010 */
11
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020012#include <linux/module.h>
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020013#include <linux/slab.h>
Samuel Iglesias Gonsalvez3b86bb22012-05-25 10:03:01 +020014#include <linux/idr.h>
Samuel Iglesias Gonsálvezb9f789f2012-09-05 09:08:17 +020015#include <asm/io.h>
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020016#include "ipack.h"
17
18#define to_ipack_dev(device) container_of(device, struct ipack_device, dev)
19#define to_ipack_driver(drv) container_of(drv, struct ipack_driver, driver)
20
Samuel Iglesias Gonsalvez3b86bb22012-05-25 10:03:01 +020021static DEFINE_IDA(ipack_ida);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020022
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020023static void ipack_device_release(struct device *dev)
24{
25 struct ipack_device *device = to_ipack_dev(dev);
Jens Taprogge187e4782012-09-04 17:01:14 +020026 kfree(device->id);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020027 kfree(device);
28}
29
Jens Taprogge4aa09d42012-09-04 17:01:19 +020030static inline const struct ipack_device_id *
31ipack_match_one_device(const struct ipack_device_id *id,
32 const struct ipack_device *device)
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020033{
Jens Taprogge5948ae22012-09-07 10:29:19 +020034 if ((id->format == IPACK_ANY_FORMAT ||
35 id->format == device->id_format) &&
Jens Taprogge4aa09d42012-09-04 17:01:19 +020036 (id->vendor == IPACK_ANY_ID || id->vendor == device->id_vendor) &&
37 (id->device == IPACK_ANY_ID || id->device == device->id_device))
38 return id;
39 return NULL;
40}
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020041
Jens Taprogge4aa09d42012-09-04 17:01:19 +020042static const struct ipack_device_id *
43ipack_match_id(const struct ipack_device_id *ids, struct ipack_device *idev)
44{
45 if (ids) {
46 while (ids->vendor || ids->device) {
47 if (ipack_match_one_device(ids, idev))
48 return ids;
49 ids++;
50 }
51 }
52 return NULL;
53}
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020054
Jens Taprogge4aa09d42012-09-04 17:01:19 +020055static int ipack_bus_match(struct device *dev, struct device_driver *drv)
56{
57 struct ipack_device *idev = to_ipack_dev(dev);
58 struct ipack_driver *idrv = to_ipack_driver(drv);
59 const struct ipack_device_id *found_id;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020060
Jens Taprogge4aa09d42012-09-04 17:01:19 +020061 found_id = ipack_match_id(idrv->id_table, idev);
62 if (found_id) {
63 idev->driver = idrv;
64 return 1;
65 }
66
67 return 0;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020068}
69
70static int ipack_bus_probe(struct device *device)
71{
72 struct ipack_device *dev = to_ipack_dev(device);
73
74 if (!dev->driver->ops->probe)
75 return -EINVAL;
76
77 return dev->driver->ops->probe(dev);
78}
79
80static int ipack_bus_remove(struct device *device)
81{
82 struct ipack_device *dev = to_ipack_dev(device);
83
84 if (!dev->driver->ops->remove)
85 return -EINVAL;
86
87 dev->driver->ops->remove(dev);
88 return 0;
89}
90
Jens Taprogge35eb97b2012-09-04 17:01:20 +020091#ifdef CONFIG_HOTPLUG
92
93static int ipack_uevent(struct device *dev, struct kobj_uevent_env *env)
94{
95 struct ipack_device *idev;
96
97 if (!dev)
98 return -ENODEV;
99
100 idev = to_ipack_dev(dev);
101
102 if (add_uevent_var(env,
103 "MODALIAS=ipack:f%02Xv%08Xd%08X", idev->id_format,
104 idev->id_vendor, idev->id_device))
105 return -ENOMEM;
106
107 return 0;
108}
109
110#else /* !CONFIG_HOTPLUG */
111
112#define ipack_uevent NULL
113
114#endif /* !CONFIG_HOTPLUG */
115
Jens Taproggee8ed3272012-09-04 17:01:15 +0200116#define ipack_device_attr(field, format_string) \
117static ssize_t \
118field##_show(struct device *dev, struct device_attribute *attr, \
119 char *buf) \
120{ \
121 struct ipack_device *idev = to_ipack_dev(dev); \
122 return sprintf(buf, format_string, idev->field); \
123}
124
Jens Taprogge5d72c8482012-09-04 17:01:21 +0200125static ssize_t id_show(struct device *dev,
126 struct device_attribute *attr, char *buf)
127{
128 unsigned int i, c, l, s;
129 struct ipack_device *idev = to_ipack_dev(dev);
130
131
132 switch (idev->id_format) {
133 case IPACK_ID_VERSION_1:
134 l = 0x7; s = 1; break;
135 case IPACK_ID_VERSION_2:
136 l = 0xf; s = 2; break;
137 default:
138 return -EIO;
139 }
140 c = 0;
141 for (i = 0; i < idev->id_avail; i++) {
142 if (i > 0) {
143 if ((i & l) == 0)
144 buf[c++] = '\n';
145 else if ((i & s) == 0)
146 buf[c++] = ' ';
147 }
148 sprintf(&buf[c], "%02x", idev->id[i]);
149 c += 2;
150 }
151 buf[c++] = '\n';
152 return c;
153}
154
Jens Taproggee8ed3272012-09-04 17:01:15 +0200155static ssize_t
156id_vendor_show(struct device *dev, struct device_attribute *attr, char *buf)
157{
158 struct ipack_device *idev = to_ipack_dev(dev);
159 switch (idev->id_format) {
160 case IPACK_ID_VERSION_1:
161 return sprintf(buf, "0x%02x\n", idev->id_vendor);
162 case IPACK_ID_VERSION_2:
163 return sprintf(buf, "0x%06x\n", idev->id_vendor);
164 default:
165 return -EIO;
166 }
167}
168
169static ssize_t
170id_device_show(struct device *dev, struct device_attribute *attr, char *buf)
171{
172 struct ipack_device *idev = to_ipack_dev(dev);
173 switch (idev->id_format) {
174 case IPACK_ID_VERSION_1:
175 return sprintf(buf, "0x%02x\n", idev->id_device);
176 case IPACK_ID_VERSION_2:
177 return sprintf(buf, "0x%04x\n", idev->id_device);
178 default:
179 return -EIO;
180 }
181}
182
Jens Taprogge35eb97b2012-09-04 17:01:20 +0200183static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
184 char *buf)
185{
186 struct ipack_device *idev = to_ipack_dev(dev);
187
188 return sprintf(buf, "ipac:f%02Xv%08Xd%08X", idev->id_format,
189 idev->id_vendor, idev->id_device);
190}
191
Jens Taproggee8ed3272012-09-04 17:01:15 +0200192ipack_device_attr(id_format, "0x%hhu\n");
193
194static struct device_attribute ipack_dev_attrs[] = {
Jens Taprogge5d72c8482012-09-04 17:01:21 +0200195 __ATTR_RO(id),
Jens Taproggee8ed3272012-09-04 17:01:15 +0200196 __ATTR_RO(id_device),
197 __ATTR_RO(id_format),
198 __ATTR_RO(id_vendor),
Jens Taprogge35eb97b2012-09-04 17:01:20 +0200199 __ATTR_RO(modalias),
Jens Taproggee8ed3272012-09-04 17:01:15 +0200200};
201
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200202static struct bus_type ipack_bus_type = {
Jens Taproggee8ed3272012-09-04 17:01:15 +0200203 .name = "ipack",
204 .probe = ipack_bus_probe,
205 .match = ipack_bus_match,
206 .remove = ipack_bus_remove,
207 .dev_attrs = ipack_dev_attrs,
Jens Taprogge35eb97b2012-09-04 17:01:20 +0200208 .uevent = ipack_uevent,
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200209};
210
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200211struct ipack_bus_device *ipack_bus_register(struct device *parent, int slots,
Stephen Hemminger9869a9372012-09-10 11:14:01 -0700212 const struct ipack_bus_ops *ops)
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200213{
214 int bus_nr;
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200215 struct ipack_bus_device *bus;
216
217 bus = kzalloc(sizeof(struct ipack_bus_device), GFP_KERNEL);
218 if (!bus)
219 return NULL;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200220
Samuel Iglesias Gonsalvez3b86bb22012-05-25 10:03:01 +0200221 bus_nr = ida_simple_get(&ipack_ida, 0, 0, GFP_KERNEL);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200222 if (bus_nr < 0) {
223 kfree(bus);
224 return NULL;
225 }
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200226
227 bus->bus_nr = bus_nr;
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200228 bus->parent = parent;
229 bus->slots = slots;
230 bus->ops = ops;
231 return bus;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200232}
233EXPORT_SYMBOL_GPL(ipack_bus_register);
234
235int ipack_bus_unregister(struct ipack_bus_device *bus)
236{
Samuel Iglesias Gonsalvez3b86bb22012-05-25 10:03:01 +0200237 ida_simple_remove(&ipack_ida, bus->bus_nr);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200238 kfree(bus);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200239 return 0;
240}
241EXPORT_SYMBOL_GPL(ipack_bus_unregister);
242
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200243int ipack_driver_register(struct ipack_driver *edrv, struct module *owner,
Stephen Hemminger9869a9372012-09-10 11:14:01 -0700244 const char *name)
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200245{
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200246 edrv->driver.owner = owner;
247 edrv->driver.name = name;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200248 edrv->driver.bus = &ipack_bus_type;
249 return driver_register(&edrv->driver);
250}
251EXPORT_SYMBOL_GPL(ipack_driver_register);
252
253void ipack_driver_unregister(struct ipack_driver *edrv)
254{
255 driver_unregister(&edrv->driver);
256}
257EXPORT_SYMBOL_GPL(ipack_driver_unregister);
258
Jens Taproggee8ed3272012-09-04 17:01:15 +0200259static void ipack_parse_id1(struct ipack_device *dev)
260{
261 u8 *id = dev->id;
262
263 dev->id_vendor = id[4];
264 dev->id_device = id[5];
265}
266
267static void ipack_parse_id2(struct ipack_device *dev)
268{
269 __be16 *id = (__be16 *) dev->id;
270
271 dev->id_vendor = ((be16_to_cpu(id[3]) & 0xff) << 16)
272 + be16_to_cpu(id[4]);
273 dev->id_device = be16_to_cpu(id[5]);
274}
275
Jens Taprogge187e4782012-09-04 17:01:14 +0200276static int ipack_device_read_id(struct ipack_device *dev)
277{
278 u8 __iomem *idmem;
279 int i;
280 int ret = 0;
281
282 ret = dev->bus->ops->map_space(dev, 0, IPACK_ID_SPACE);
283 if (ret) {
284 dev_err(&dev->dev, "error mapping memory\n");
285 return ret;
286 }
287 idmem = dev->id_space.address;
288
289 /* Determine ID PROM Data Format. If we find the ids "IPAC" or "IPAH"
290 * we are dealing with a IndustryPack format 1 device. If we detect
291 * "VITA4 " (16 bit big endian formatted) we are dealing with a
292 * IndustryPack format 2 device */
293 if ((ioread8(idmem + 1) == 'I') &&
294 (ioread8(idmem + 3) == 'P') &&
295 (ioread8(idmem + 5) == 'A') &&
296 ((ioread8(idmem + 7) == 'C') ||
297 (ioread8(idmem + 7) == 'H'))) {
298 dev->id_format = IPACK_ID_VERSION_1;
299 dev->id_avail = ioread8(idmem + 0x15);
300 if ((dev->id_avail < 0x0c) || (dev->id_avail > 0x40)) {
301 dev_warn(&dev->dev, "invalid id size");
302 dev->id_avail = 0x0c;
303 }
304 } else if ((ioread8(idmem + 0) == 'I') &&
305 (ioread8(idmem + 1) == 'V') &&
306 (ioread8(idmem + 2) == 'A') &&
307 (ioread8(idmem + 3) == 'T') &&
308 (ioread8(idmem + 4) == ' ') &&
309 (ioread8(idmem + 5) == '4')) {
310 dev->id_format = IPACK_ID_VERSION_2;
311 dev->id_avail = ioread16be(idmem + 0x16);
312 if ((dev->id_avail < 0x1a) || (dev->id_avail > 0x40)) {
313 dev_warn(&dev->dev, "invalid id size");
314 dev->id_avail = 0x1a;
315 }
316 } else {
317 dev->id_format = IPACK_ID_VERSION_INVALID;
318 dev->id_avail = 0;
319 }
320
321 if (!dev->id_avail) {
322 ret = -ENODEV;
323 goto out;
324 }
325
326 /* Obtain the amount of memory required to store a copy of the complete
327 * ID ROM contents */
328 dev->id = kmalloc(dev->id_avail, GFP_KERNEL);
329 if (!dev->id) {
330 dev_err(&dev->dev, "dev->id alloc failed.\n");
331 ret = -ENOMEM;
332 goto out;
333 }
334 for (i = 0; i < dev->id_avail; i++) {
335 if (dev->id_format == IPACK_ID_VERSION_1)
336 dev->id[i] = ioread8(idmem + (i << 1) + 1);
337 else
338 dev->id[i] = ioread8(idmem + i);
339 }
340
Jens Taproggee8ed3272012-09-04 17:01:15 +0200341 /* now we can finally work with the copy */
342 switch (dev->id_format) {
343 case IPACK_ID_VERSION_1:
344 ipack_parse_id1(dev);
345 break;
346 case IPACK_ID_VERSION_2:
347 ipack_parse_id2(dev);
348 break;
349 }
350
Jens Taprogge187e4782012-09-04 17:01:14 +0200351out:
352 dev->bus->ops->unmap_space(dev, IPACK_ID_SPACE);
353
354 return ret;
355}
356
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200357struct ipack_device *ipack_device_register(struct ipack_bus_device *bus,
358 int slot, int irqv)
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200359{
360 int ret;
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200361 struct ipack_device *dev;
362
363 dev = kzalloc(sizeof(struct ipack_device), GFP_KERNEL);
364 if (!dev)
365 return NULL;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200366
367 dev->dev.bus = &ipack_bus_type;
368 dev->dev.release = ipack_device_release;
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200369 dev->dev.parent = bus->parent;
370 dev->slot = slot;
371 dev->bus_nr = bus->bus_nr;
372 dev->irq = irqv;
373 dev->bus = bus;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200374 dev_set_name(&dev->dev,
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200375 "ipack-dev.%u.%u", dev->bus_nr, dev->slot);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200376
Jens Taprogge187e4782012-09-04 17:01:14 +0200377 ret = ipack_device_read_id(dev);
378 if (ret < 0) {
379 dev_err(&dev->dev, "error reading device id section.\n");
380 kfree(dev);
381 return NULL;
382 }
383
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200384 ret = device_register(&dev->dev);
385 if (ret < 0) {
Jens Taprogge187e4782012-09-04 17:01:14 +0200386 kfree(dev->id);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200387 kfree(dev);
388 return NULL;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200389 }
390
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200391 return dev;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200392}
393EXPORT_SYMBOL_GPL(ipack_device_register);
394
395void ipack_device_unregister(struct ipack_device *dev)
396{
397 device_unregister(&dev->dev);
398}
399EXPORT_SYMBOL_GPL(ipack_device_unregister);
400
401static int __init ipack_init(void)
402{
Samuel Iglesias Gonsalvez3b86bb22012-05-25 10:03:01 +0200403 ida_init(&ipack_ida);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200404 return bus_register(&ipack_bus_type);
405}
406
407static void __exit ipack_exit(void)
408{
409 bus_unregister(&ipack_bus_type);
Samuel Iglesias Gonsalvez3b86bb22012-05-25 10:03:01 +0200410 ida_destroy(&ipack_ida);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200411}
412
413module_init(ipack_init);
414module_exit(ipack_exit);
415
416MODULE_AUTHOR("Samuel Iglesias Gonsalvez <siglesias@igalia.com>");
417MODULE_LICENSE("GPL");
418MODULE_DESCRIPTION("Industry-pack bus core");