Staging: silicom: C99 cleanup of libbp_sd.h
[deliverable/linux.git] / drivers / staging / ipack / ipack.c
CommitLineData
d3465872
SIG
1/*
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
416289b1 9 * Software Foundation; version 2 of the License.
d3465872
SIG
10 */
11
d3465872 12#include <linux/module.h>
ec440335 13#include <linux/slab.h>
3b86bb2e 14#include <linux/idr.h>
b9f789fb 15#include <asm/io.h>
d3465872
SIG
16#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
3b86bb2e 21static DEFINE_IDA(ipack_ida);
d3465872 22
ec440335
SIG
23static void ipack_device_release(struct device *dev)
24{
25 struct ipack_device *device = to_ipack_dev(dev);
187e4782 26 kfree(device->id);
ec440335
SIG
27 kfree(device);
28}
29
4aa09d47
JT
30static inline const struct ipack_device_id *
31ipack_match_one_device(const struct ipack_device_id *id,
32 const struct ipack_device *device)
d3465872 33{
5948ae27
JT
34 if ((id->format == IPACK_ANY_FORMAT ||
35 id->format == device->id_format) &&
4aa09d47
JT
36 (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}
d3465872 41
4aa09d47
JT
42static 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}
54
55static 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;
d3465872 60
4aa09d47
JT
61 found_id = ipack_match_id(idrv->id_table, idev);
62 if (found_id) {
63 idev->driver = idrv;
64 return 1;
65 }
d3465872 66
4aa09d47 67 return 0;
d3465872
SIG
68}
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
35eb97bb
JT
91#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
e8ed3276
JT
116#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
5d72c848
JT
125static 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
e8ed3276
JT
155static 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
35eb97bb
JT
183static 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
e8ed3276
JT
192ipack_device_attr(id_format, "0x%hhu\n");
193
194static struct device_attribute ipack_dev_attrs[] = {
5d72c848 195 __ATTR_RO(id),
e8ed3276
JT
196 __ATTR_RO(id_device),
197 __ATTR_RO(id_format),
198 __ATTR_RO(id_vendor),
35eb97bb 199 __ATTR_RO(modalias),
e8ed3276
JT
200};
201
d3465872 202static struct bus_type ipack_bus_type = {
e8ed3276
JT
203 .name = "ipack",
204 .probe = ipack_bus_probe,
205 .match = ipack_bus_match,
206 .remove = ipack_bus_remove,
207 .dev_attrs = ipack_dev_attrs,
35eb97bb 208 .uevent = ipack_uevent,
d3465872
SIG
209};
210
ec440335
SIG
211struct ipack_bus_device *ipack_bus_register(struct device *parent, int slots,
212 struct ipack_bus_ops *ops)
d3465872
SIG
213{
214 int bus_nr;
ec440335
SIG
215 struct ipack_bus_device *bus;
216
217 bus = kzalloc(sizeof(struct ipack_bus_device), GFP_KERNEL);
218 if (!bus)
219 return NULL;
d3465872 220
3b86bb2e 221 bus_nr = ida_simple_get(&ipack_ida, 0, 0, GFP_KERNEL);
ec440335
SIG
222 if (bus_nr < 0) {
223 kfree(bus);
224 return NULL;
225 }
d3465872
SIG
226
227 bus->bus_nr = bus_nr;
ec440335
SIG
228 bus->parent = parent;
229 bus->slots = slots;
230 bus->ops = ops;
231 return bus;
d3465872
SIG
232}
233EXPORT_SYMBOL_GPL(ipack_bus_register);
234
235int ipack_bus_unregister(struct ipack_bus_device *bus)
236{
3b86bb2e 237 ida_simple_remove(&ipack_ida, bus->bus_nr);
ec440335 238 kfree(bus);
d3465872
SIG
239 return 0;
240}
241EXPORT_SYMBOL_GPL(ipack_bus_unregister);
242
ec440335
SIG
243int ipack_driver_register(struct ipack_driver *edrv, struct module *owner,
244 char *name)
d3465872 245{
ec440335
SIG
246 edrv->driver.owner = owner;
247 edrv->driver.name = name;
d3465872
SIG
248 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
e8ed3276
JT
259static 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
187e4782
JT
276static 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
e8ed3276
JT
341 /* 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
187e4782
JT
351out:
352 dev->bus->ops->unmap_space(dev, IPACK_ID_SPACE);
353
354 return ret;
355}
356
ec440335
SIG
357struct ipack_device *ipack_device_register(struct ipack_bus_device *bus,
358 int slot, int irqv)
d3465872
SIG
359{
360 int ret;
ec440335
SIG
361 struct ipack_device *dev;
362
363 dev = kzalloc(sizeof(struct ipack_device), GFP_KERNEL);
364 if (!dev)
365 return NULL;
d3465872
SIG
366
367 dev->dev.bus = &ipack_bus_type;
368 dev->dev.release = ipack_device_release;
ec440335
SIG
369 dev->dev.parent = bus->parent;
370 dev->slot = slot;
371 dev->bus_nr = bus->bus_nr;
372 dev->irq = irqv;
373 dev->bus = bus;
d3465872 374 dev_set_name(&dev->dev,
ec440335 375 "ipack-dev.%u.%u", dev->bus_nr, dev->slot);
d3465872 376
187e4782
JT
377 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
d3465872
SIG
384 ret = device_register(&dev->dev);
385 if (ret < 0) {
187e4782 386 kfree(dev->id);
ec440335
SIG
387 kfree(dev);
388 return NULL;
d3465872
SIG
389 }
390
ec440335 391 return dev;
d3465872
SIG
392}
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{
3b86bb2e 403 ida_init(&ipack_ida);
d3465872
SIG
404 return bus_register(&ipack_bus_type);
405}
406
407static void __exit ipack_exit(void)
408{
409 bus_unregister(&ipack_bus_type);
3b86bb2e 410 ida_destroy(&ipack_ida);
d3465872
SIG
411}
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");
This page took 0.729069 seconds and 5 git commands to generate.