]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/mcb/mcb-core.c
mcb: export bus information via sysfs
[mirror_ubuntu-zesty-kernel.git] / drivers / mcb / mcb-core.c
CommitLineData
3764e82e
JT
1/*
2 * MEN Chameleon Bus.
3 *
4 * Copyright (C) 2013 MEN Mikroelektronik GmbH (www.men.de)
5 * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
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#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/types.h>
15#include <linux/idr.h>
16#include <linux/mcb.h>
17
18static DEFINE_IDA(mcb_ida);
19
20static const struct mcb_device_id *mcb_match_id(const struct mcb_device_id *ids,
21 struct mcb_device *dev)
22{
23 if (ids) {
24 while (ids->device) {
25 if (ids->device == dev->id)
26 return ids;
27 ids++;
28 }
29 }
30
31 return NULL;
32}
33
34static int mcb_match(struct device *dev, struct device_driver *drv)
35{
36 struct mcb_driver *mdrv = to_mcb_driver(drv);
37 struct mcb_device *mdev = to_mcb_device(dev);
38 const struct mcb_device_id *found_id;
39
40 found_id = mcb_match_id(mdrv->id_table, mdev);
41 if (found_id)
42 return 1;
43
44 return 0;
45}
46
47static int mcb_uevent(struct device *dev, struct kobj_uevent_env *env)
48{
49 struct mcb_device *mdev = to_mcb_device(dev);
50 int ret;
51
52 ret = add_uevent_var(env, "MODALIAS=mcb:16z%03d", mdev->id);
53 if (ret)
54 return -ENOMEM;
55
56 return 0;
57}
58
59static int mcb_probe(struct device *dev)
60{
61 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
62 struct mcb_device *mdev = to_mcb_device(dev);
63 const struct mcb_device_id *found_id;
64
65 found_id = mcb_match_id(mdrv->id_table, mdev);
66 if (!found_id)
67 return -ENODEV;
68
69 return mdrv->probe(mdev, found_id);
70}
71
72static int mcb_remove(struct device *dev)
73{
74 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
75 struct mcb_device *mdev = to_mcb_device(dev);
76
77 mdrv->remove(mdev);
78
79 put_device(&mdev->dev);
80
81 return 0;
82}
83
84static void mcb_shutdown(struct device *dev)
85{
86 struct mcb_device *mdev = to_mcb_device(dev);
87 struct mcb_driver *mdrv = mdev->driver;
88
89 if (mdrv && mdrv->shutdown)
90 mdrv->shutdown(mdev);
91}
92
803f1ca6
JT
93static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
94 char *buf)
95{
96 struct mcb_bus *bus = to_mcb_bus(dev);
97
98 return scnprintf(buf, PAGE_SIZE, "%d\n", bus->revision);
99}
100static DEVICE_ATTR_RO(revision);
101
102static ssize_t model_show(struct device *dev, struct device_attribute *attr,
103 char *buf)
104{
105 struct mcb_bus *bus = to_mcb_bus(dev);
106
107 return scnprintf(buf, PAGE_SIZE, "%c\n", bus->model);
108}
109static DEVICE_ATTR_RO(model);
110
111static ssize_t minor_show(struct device *dev, struct device_attribute *attr,
112 char *buf)
113{
114 struct mcb_bus *bus = to_mcb_bus(dev);
115
116 return scnprintf(buf, PAGE_SIZE, "%d\n", bus->minor);
117}
118static DEVICE_ATTR_RO(minor);
119
120static ssize_t name_show(struct device *dev, struct device_attribute *attr,
121 char *buf)
122{
123 struct mcb_bus *bus = to_mcb_bus(dev);
124
125 return scnprintf(buf, PAGE_SIZE, "%s\n", bus->name);
126}
127static DEVICE_ATTR_RO(name);
128
129static struct attribute *mcb_bus_attrs[] = {
130 &dev_attr_revision.attr,
131 &dev_attr_model.attr,
132 &dev_attr_minor.attr,
133 &dev_attr_name.attr,
134 NULL,
135};
136
137static const struct attribute_group mcb_carrier_group = {
138 .attrs = mcb_bus_attrs,
139};
140
141static const struct attribute_group *mcb_carrier_groups[] = {
142 &mcb_carrier_group,
143 NULL,
144};
145
146
3764e82e
JT
147static struct bus_type mcb_bus_type = {
148 .name = "mcb",
149 .match = mcb_match,
150 .uevent = mcb_uevent,
151 .probe = mcb_probe,
152 .remove = mcb_remove,
153 .shutdown = mcb_shutdown,
154};
155
803f1ca6
JT
156static struct device_type mcb_carrier_device_type = {
157 .name = "mcb-carrier",
158 .groups = mcb_carrier_groups,
159};
160
3764e82e
JT
161/**
162 * __mcb_register_driver() - Register a @mcb_driver at the system
163 * @drv: The @mcb_driver
164 * @owner: The @mcb_driver's module
165 * @mod_name: The name of the @mcb_driver's module
166 *
167 * Register a @mcb_driver at the system. Perform some sanity checks, if
168 * the .probe and .remove methods are provided by the driver.
169 */
170int __mcb_register_driver(struct mcb_driver *drv, struct module *owner,
171 const char *mod_name)
172{
173 if (!drv->probe || !drv->remove)
174 return -EINVAL;
175
176 drv->driver.owner = owner;
177 drv->driver.bus = &mcb_bus_type;
178 drv->driver.mod_name = mod_name;
179
180 return driver_register(&drv->driver);
181}
182EXPORT_SYMBOL_GPL(__mcb_register_driver);
183
184/**
185 * mcb_unregister_driver() - Unregister a @mcb_driver from the system
186 * @drv: The @mcb_driver
187 *
188 * Unregister a @mcb_driver from the system.
189 */
190void mcb_unregister_driver(struct mcb_driver *drv)
191{
192 driver_unregister(&drv->driver);
193}
194EXPORT_SYMBOL_GPL(mcb_unregister_driver);
195
196static void mcb_release_dev(struct device *dev)
197{
198 struct mcb_device *mdev = to_mcb_device(dev);
199
200 mcb_bus_put(mdev->bus);
201 kfree(mdev);
202}
203
204/**
205 * mcb_device_register() - Register a mcb_device
206 * @bus: The @mcb_bus of the device
207 * @dev: The @mcb_device
208 *
209 * Register a specific @mcb_device at a @mcb_bus and the system itself.
210 */
211int mcb_device_register(struct mcb_bus *bus, struct mcb_device *dev)
212{
213 int ret;
214 int device_id;
215
216 device_initialize(&dev->dev);
217 dev->dev.bus = &mcb_bus_type;
218 dev->dev.parent = bus->dev.parent;
219 dev->dev.release = mcb_release_dev;
220
221 device_id = dev->id;
222 dev_set_name(&dev->dev, "mcb%d-16z%03d-%d:%d:%d",
223 bus->bus_nr, device_id, dev->inst, dev->group, dev->var);
224
225 ret = device_add(&dev->dev);
226 if (ret < 0) {
227 pr_err("Failed registering device 16z%03d on bus mcb%d (%d)\n",
228 device_id, bus->bus_nr, ret);
229 goto out;
230 }
231
232 return 0;
233
234out:
235
236 return ret;
237}
238EXPORT_SYMBOL_GPL(mcb_device_register);
239
240/**
241 * mcb_alloc_bus() - Allocate a new @mcb_bus
242 *
243 * Allocate a new @mcb_bus.
244 */
4ec65b77 245struct mcb_bus *mcb_alloc_bus(struct device *carrier)
3764e82e
JT
246{
247 struct mcb_bus *bus;
248 int bus_nr;
18d28819 249 int rc;
3764e82e
JT
250
251 bus = kzalloc(sizeof(struct mcb_bus), GFP_KERNEL);
252 if (!bus)
4ec65b77 253 return ERR_PTR(-ENOMEM);
3764e82e
JT
254
255 bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL);
256 if (bus_nr < 0) {
18d28819
JT
257 rc = bus_nr;
258 goto err_free;
3764e82e
JT
259 }
260
3764e82e 261 bus->bus_nr = bus_nr;
4ec65b77 262 bus->carrier = carrier;
18d28819
JT
263
264 device_initialize(&bus->dev);
265 bus->dev.parent = carrier;
266 bus->dev.bus = &mcb_bus_type;
803f1ca6 267 bus->dev.type = &mcb_carrier_device_type;
18d28819
JT
268
269 dev_set_name(&bus->dev, "mcb:%d", bus_nr);
270 rc = device_add(&bus->dev);
271 if (rc)
272 goto err_free;
273
3764e82e 274 return bus;
18d28819
JT
275err_free:
276 kfree(bus);
277 return ERR_PTR(rc);
3764e82e
JT
278}
279EXPORT_SYMBOL_GPL(mcb_alloc_bus);
280
281static int __mcb_devices_unregister(struct device *dev, void *data)
282{
283 device_unregister(dev);
284 return 0;
285}
286
287static void mcb_devices_unregister(struct mcb_bus *bus)
288{
289 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_devices_unregister);
290}
291/**
292 * mcb_release_bus() - Free a @mcb_bus
293 * @bus: The @mcb_bus to release
294 *
295 * Release an allocated @mcb_bus from the system.
296 */
297void mcb_release_bus(struct mcb_bus *bus)
298{
299 mcb_devices_unregister(bus);
300
301 ida_simple_remove(&mcb_ida, bus->bus_nr);
302
303 kfree(bus);
304}
305EXPORT_SYMBOL_GPL(mcb_release_bus);
306
307/**
308 * mcb_bus_put() - Increment refcnt
309 * @bus: The @mcb_bus
310 *
311 * Get a @mcb_bus' ref
312 */
313struct mcb_bus *mcb_bus_get(struct mcb_bus *bus)
314{
315 if (bus)
316 get_device(&bus->dev);
317
318 return bus;
319}
320EXPORT_SYMBOL_GPL(mcb_bus_get);
321
322/**
323 * mcb_bus_put() - Decrement refcnt
324 * @bus: The @mcb_bus
325 *
326 * Release a @mcb_bus' ref
327 */
328void mcb_bus_put(struct mcb_bus *bus)
329{
330 if (bus)
331 put_device(&bus->dev);
332}
333EXPORT_SYMBOL_GPL(mcb_bus_put);
334
335/**
336 * mcb_alloc_dev() - Allocate a device
337 * @bus: The @mcb_bus the device is part of
338 *
339 * Allocate a @mcb_device and add bus.
340 */
341struct mcb_device *mcb_alloc_dev(struct mcb_bus *bus)
342{
343 struct mcb_device *dev;
344
345 dev = kzalloc(sizeof(struct mcb_device), GFP_KERNEL);
346 if (!dev)
347 return NULL;
348
349 INIT_LIST_HEAD(&dev->bus_list);
350 dev->bus = bus;
351
352 return dev;
353}
354EXPORT_SYMBOL_GPL(mcb_alloc_dev);
355
356/**
357 * mcb_free_dev() - Free @mcb_device
358 * @dev: The device to free
359 *
360 * Free a @mcb_device
361 */
362void mcb_free_dev(struct mcb_device *dev)
363{
364 kfree(dev);
365}
366EXPORT_SYMBOL_GPL(mcb_free_dev);
367
368static int __mcb_bus_add_devices(struct device *dev, void *data)
369{
370 struct mcb_device *mdev = to_mcb_device(dev);
371 int retval;
372
373 if (mdev->is_added)
374 return 0;
375
376 retval = device_attach(dev);
377 if (retval < 0)
378 dev_err(dev, "Error adding device (%d)\n", retval);
379
380 mdev->is_added = true;
381
382 return 0;
383}
384
385static int __mcb_bus_add_child(struct device *dev, void *data)
386{
387 struct mcb_device *mdev = to_mcb_device(dev);
388 struct mcb_bus *child;
389
390 BUG_ON(!mdev->is_added);
391 child = mdev->subordinate;
392
393 if (child)
394 mcb_bus_add_devices(child);
395
396 return 0;
397}
398
399/**
400 * mcb_bus_add_devices() - Add devices in the bus' internal device list
401 * @bus: The @mcb_bus we add the devices
402 *
403 * Add devices in the bus' internal device list to the system.
404 */
405void mcb_bus_add_devices(const struct mcb_bus *bus)
406{
407 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_devices);
408 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_child);
409
410}
411EXPORT_SYMBOL_GPL(mcb_bus_add_devices);
412
413/**
414 * mcb_request_mem() - Request memory
415 * @dev: The @mcb_device the memory is for
416 * @name: The name for the memory reference.
417 *
418 * Request memory for a @mcb_device. If @name is NULL the driver name will
419 * be used.
420 */
421struct resource *mcb_request_mem(struct mcb_device *dev, const char *name)
422{
423 struct resource *mem;
424 u32 size;
425
426 if (!name)
427 name = dev->dev.driver->name;
428
429 size = resource_size(&dev->mem);
430
431 mem = request_mem_region(dev->mem.start, size, name);
432 if (!mem)
433 return ERR_PTR(-EBUSY);
434
435 return mem;
436}
437EXPORT_SYMBOL_GPL(mcb_request_mem);
438
439/**
440 * mcb_release_mem() - Release memory requested by device
441 * @dev: The @mcb_device that requested the memory
442 *
443 * Release memory that was prior requested via @mcb_request_mem().
444 */
445void mcb_release_mem(struct resource *mem)
446{
447 u32 size;
448
449 size = resource_size(mem);
450 release_mem_region(mem->start, size);
451}
452EXPORT_SYMBOL_GPL(mcb_release_mem);
453
4ec65b77
JT
454static int __mcb_get_irq(struct mcb_device *dev)
455{
456 struct resource *irq = &dev->irq;
457
458 return irq->start;
459}
460
3764e82e
JT
461/**
462 * mcb_get_irq() - Get device's IRQ number
463 * @dev: The @mcb_device the IRQ is for
464 *
465 * Get the IRQ number of a given @mcb_device.
466 */
467int mcb_get_irq(struct mcb_device *dev)
468{
4ec65b77 469 struct mcb_bus *bus = dev->bus;
3764e82e 470
4ec65b77
JT
471 if (bus->get_irq)
472 return bus->get_irq(dev);
473
474 return __mcb_get_irq(dev);
3764e82e
JT
475}
476EXPORT_SYMBOL_GPL(mcb_get_irq);
477
478static int mcb_init(void)
479{
480 return bus_register(&mcb_bus_type);
481}
482
483static void mcb_exit(void)
484{
169883a6 485 ida_destroy(&mcb_ida);
3764e82e
JT
486 bus_unregister(&mcb_bus_type);
487}
488
489/* mcb must be initialized after PCI but before the chameleon drivers.
490 * That means we must use some initcall between subsys_initcall and
491 * device_initcall.
492 */
493fs_initcall(mcb_init);
494module_exit(mcb_exit);
495
496MODULE_DESCRIPTION("MEN Chameleon Bus Driver");
497MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
498MODULE_LICENSE("GPL v2");