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