blob: b73c6e7d28e4b9b0f337355aa75ce09d56c7e8f7 [file] [log] [blame]
Johannes Thumshirn3764e822014-02-26 17:29:05 +01001/*
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{
Johannes Thumshirn5d9e2ab2016-05-03 12:42:03 +020086 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
Johannes Thumshirn3764e822014-02-26 17:29:05 +010087 struct mcb_device *mdev = to_mcb_device(dev);
Johannes Thumshirn3764e822014-02-26 17:29:05 +010088
89 if (mdrv && mdrv->shutdown)
90 mdrv->shutdown(mdev);
91}
92
Johannes Thumshirn803f1ca2016-05-03 09:46:23 +020093static 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
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100147static 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
Johannes Thumshirn803f1ca2016-05-03 09:46:23 +0200156static struct device_type mcb_carrier_device_type = {
157 .name = "mcb-carrier",
158 .groups = mcb_carrier_groups,
159};
160
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100161/**
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);
Johannes Thumshirn5d9e2ab2016-05-03 12:42:03 +0200217 mcb_bus_get(bus);
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100218 dev->dev.bus = &mcb_bus_type;
219 dev->dev.parent = bus->dev.parent;
220 dev->dev.release = mcb_release_dev;
221
222 device_id = dev->id;
223 dev_set_name(&dev->dev, "mcb%d-16z%03d-%d:%d:%d",
224 bus->bus_nr, device_id, dev->inst, dev->group, dev->var);
225
226 ret = device_add(&dev->dev);
227 if (ret < 0) {
228 pr_err("Failed registering device 16z%03d on bus mcb%d (%d)\n",
229 device_id, bus->bus_nr, ret);
230 goto out;
231 }
232
233 return 0;
234
235out:
236
237 return ret;
238}
239EXPORT_SYMBOL_GPL(mcb_device_register);
240
Johannes Thumshirn5d9e2ab2016-05-03 12:42:03 +0200241static void mcb_free_bus(struct device *dev)
242{
243 struct mcb_bus *bus = to_mcb_bus(dev);
244
245 put_device(bus->carrier);
246 ida_simple_remove(&mcb_ida, bus->bus_nr);
247 kfree(bus);
248}
249
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100250/**
251 * mcb_alloc_bus() - Allocate a new @mcb_bus
252 *
253 * Allocate a new @mcb_bus.
254 */
Johannes Thumshirn4ec65b72014-04-24 14:35:25 +0200255struct mcb_bus *mcb_alloc_bus(struct device *carrier)
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100256{
257 struct mcb_bus *bus;
258 int bus_nr;
Johannes Thumshirn18d28812016-05-03 09:46:22 +0200259 int rc;
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100260
261 bus = kzalloc(sizeof(struct mcb_bus), GFP_KERNEL);
262 if (!bus)
Johannes Thumshirn4ec65b72014-04-24 14:35:25 +0200263 return ERR_PTR(-ENOMEM);
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100264
265 bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL);
266 if (bus_nr < 0) {
Johannes Thumshirn18d28812016-05-03 09:46:22 +0200267 rc = bus_nr;
268 goto err_free;
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100269 }
270
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100271 bus->bus_nr = bus_nr;
Johannes Thumshirn5d9e2ab2016-05-03 12:42:03 +0200272 bus->carrier = get_device(carrier);
Johannes Thumshirn18d28812016-05-03 09:46:22 +0200273
274 device_initialize(&bus->dev);
275 bus->dev.parent = carrier;
276 bus->dev.bus = &mcb_bus_type;
Johannes Thumshirn803f1ca2016-05-03 09:46:23 +0200277 bus->dev.type = &mcb_carrier_device_type;
Johannes Thumshirn5d9e2ab2016-05-03 12:42:03 +0200278 bus->dev.release = &mcb_free_bus;
Johannes Thumshirn18d28812016-05-03 09:46:22 +0200279
280 dev_set_name(&bus->dev, "mcb:%d", bus_nr);
281 rc = device_add(&bus->dev);
282 if (rc)
283 goto err_free;
284
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100285 return bus;
Johannes Thumshirn18d28812016-05-03 09:46:22 +0200286err_free:
Johannes Thumshirn5d9e2ab2016-05-03 12:42:03 +0200287 put_device(carrier);
Johannes Thumshirn18d28812016-05-03 09:46:22 +0200288 kfree(bus);
289 return ERR_PTR(rc);
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100290}
291EXPORT_SYMBOL_GPL(mcb_alloc_bus);
292
293static int __mcb_devices_unregister(struct device *dev, void *data)
294{
295 device_unregister(dev);
296 return 0;
297}
298
299static void mcb_devices_unregister(struct mcb_bus *bus)
300{
301 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_devices_unregister);
302}
303/**
304 * mcb_release_bus() - Free a @mcb_bus
305 * @bus: The @mcb_bus to release
306 *
307 * Release an allocated @mcb_bus from the system.
308 */
309void mcb_release_bus(struct mcb_bus *bus)
310{
311 mcb_devices_unregister(bus);
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100312}
313EXPORT_SYMBOL_GPL(mcb_release_bus);
314
315/**
316 * mcb_bus_put() - Increment refcnt
317 * @bus: The @mcb_bus
318 *
319 * Get a @mcb_bus' ref
320 */
321struct mcb_bus *mcb_bus_get(struct mcb_bus *bus)
322{
323 if (bus)
324 get_device(&bus->dev);
325
326 return bus;
327}
328EXPORT_SYMBOL_GPL(mcb_bus_get);
329
330/**
331 * mcb_bus_put() - Decrement refcnt
332 * @bus: The @mcb_bus
333 *
334 * Release a @mcb_bus' ref
335 */
336void mcb_bus_put(struct mcb_bus *bus)
337{
338 if (bus)
339 put_device(&bus->dev);
340}
341EXPORT_SYMBOL_GPL(mcb_bus_put);
342
343/**
344 * mcb_alloc_dev() - Allocate a device
345 * @bus: The @mcb_bus the device is part of
346 *
347 * Allocate a @mcb_device and add bus.
348 */
349struct mcb_device *mcb_alloc_dev(struct mcb_bus *bus)
350{
351 struct mcb_device *dev;
352
353 dev = kzalloc(sizeof(struct mcb_device), GFP_KERNEL);
354 if (!dev)
355 return NULL;
356
357 INIT_LIST_HEAD(&dev->bus_list);
358 dev->bus = bus;
359
360 return dev;
361}
362EXPORT_SYMBOL_GPL(mcb_alloc_dev);
363
364/**
365 * mcb_free_dev() - Free @mcb_device
366 * @dev: The device to free
367 *
368 * Free a @mcb_device
369 */
370void mcb_free_dev(struct mcb_device *dev)
371{
372 kfree(dev);
373}
374EXPORT_SYMBOL_GPL(mcb_free_dev);
375
376static int __mcb_bus_add_devices(struct device *dev, void *data)
377{
378 struct mcb_device *mdev = to_mcb_device(dev);
379 int retval;
380
381 if (mdev->is_added)
382 return 0;
383
384 retval = device_attach(dev);
385 if (retval < 0)
386 dev_err(dev, "Error adding device (%d)\n", retval);
387
388 mdev->is_added = true;
389
390 return 0;
391}
392
393static int __mcb_bus_add_child(struct device *dev, void *data)
394{
395 struct mcb_device *mdev = to_mcb_device(dev);
396 struct mcb_bus *child;
397
398 BUG_ON(!mdev->is_added);
399 child = mdev->subordinate;
400
401 if (child)
402 mcb_bus_add_devices(child);
403
404 return 0;
405}
406
407/**
408 * mcb_bus_add_devices() - Add devices in the bus' internal device list
409 * @bus: The @mcb_bus we add the devices
410 *
411 * Add devices in the bus' internal device list to the system.
412 */
413void mcb_bus_add_devices(const struct mcb_bus *bus)
414{
415 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_devices);
416 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_child);
417
418}
419EXPORT_SYMBOL_GPL(mcb_bus_add_devices);
420
421/**
422 * mcb_request_mem() - Request memory
423 * @dev: The @mcb_device the memory is for
424 * @name: The name for the memory reference.
425 *
426 * Request memory for a @mcb_device. If @name is NULL the driver name will
427 * be used.
428 */
429struct resource *mcb_request_mem(struct mcb_device *dev, const char *name)
430{
431 struct resource *mem;
432 u32 size;
433
434 if (!name)
435 name = dev->dev.driver->name;
436
437 size = resource_size(&dev->mem);
438
439 mem = request_mem_region(dev->mem.start, size, name);
440 if (!mem)
441 return ERR_PTR(-EBUSY);
442
443 return mem;
444}
445EXPORT_SYMBOL_GPL(mcb_request_mem);
446
447/**
448 * mcb_release_mem() - Release memory requested by device
449 * @dev: The @mcb_device that requested the memory
450 *
451 * Release memory that was prior requested via @mcb_request_mem().
452 */
453void mcb_release_mem(struct resource *mem)
454{
455 u32 size;
456
457 size = resource_size(mem);
458 release_mem_region(mem->start, size);
459}
460EXPORT_SYMBOL_GPL(mcb_release_mem);
461
Johannes Thumshirn4ec65b72014-04-24 14:35:25 +0200462static int __mcb_get_irq(struct mcb_device *dev)
463{
464 struct resource *irq = &dev->irq;
465
466 return irq->start;
467}
468
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100469/**
470 * mcb_get_irq() - Get device's IRQ number
471 * @dev: The @mcb_device the IRQ is for
472 *
473 * Get the IRQ number of a given @mcb_device.
474 */
475int mcb_get_irq(struct mcb_device *dev)
476{
Johannes Thumshirn4ec65b72014-04-24 14:35:25 +0200477 struct mcb_bus *bus = dev->bus;
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100478
Johannes Thumshirn4ec65b72014-04-24 14:35:25 +0200479 if (bus->get_irq)
480 return bus->get_irq(dev);
481
482 return __mcb_get_irq(dev);
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100483}
484EXPORT_SYMBOL_GPL(mcb_get_irq);
485
486static int mcb_init(void)
487{
488 return bus_register(&mcb_bus_type);
489}
490
491static void mcb_exit(void)
492{
Johannes Thumshirn169883a2015-10-28 08:22:16 +0100493 ida_destroy(&mcb_ida);
Johannes Thumshirn3764e822014-02-26 17:29:05 +0100494 bus_unregister(&mcb_bus_type);
495}
496
497/* mcb must be initialized after PCI but before the chameleon drivers.
498 * That means we must use some initcall between subsys_initcall and
499 * device_initcall.
500 */
501fs_initcall(mcb_init);
502module_exit(mcb_exit);
503
504MODULE_DESCRIPTION("MEN Chameleon Bus Driver");
505MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
506MODULE_LICENSE("GPL v2");