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