aboutsummaryrefslogtreecommitdiff
path: root/drivers/usb/susb/lm.c
blob: 7f86c395b0a92557f8df2ade4faa653dbc26d177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 *  linux/arch/arm/mach-k3v2/lm.c
 *
 *  Copyright (C) 2003 Deep Blue Solutions Ltd, All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/slab.h>
#include "lm.h"
#include <linux/string.h>

#define to_lm_device(d)	container_of(d, struct lm_device, dev)
#define to_lm_driver(d)	container_of(d, struct lm_driver, drv)

static int lm_match(struct device *dev, struct device_driver *drv)
{
	if (!dev_name(dev) || !drv->name) {
		WARN(!dev_name(dev), "logic model device no name!\n");
		WARN(!drv->name, "logic model driver no name!\n");
		return 0;
	}
	if (0 == strcmp(dev_name(dev), drv->name))
		return 1;
	else
		return 0;
}

static int lm_bus_probe(struct device *dev)
{
	struct lm_device *lmdev = to_lm_device(dev);
	struct lm_driver *lmdrv = to_lm_driver(dev->driver);

	return lmdrv->probe(lmdev);
}

static int lm_bus_remove(struct device *dev)
{
	struct lm_device *lmdev = to_lm_device(dev);
	struct lm_driver *lmdrv = to_lm_driver(dev->driver);

	if (lmdrv->remove)
		lmdrv->remove(lmdev);
	return 0;
}


static int lm_bus_suspend(struct device *dev, pm_message_t state)
{
	struct lm_device *lmdev = to_lm_device(dev);
	struct lm_driver *lmdrv = to_lm_driver(dev->driver);

	if (lmdrv->suspend)
		return lmdrv->suspend(lmdev, state);
	return 0;
}

static int lm_bus_resume(struct device *dev)
{
	struct lm_device *lmdev = to_lm_device(dev);
	struct lm_driver *lmdrv = to_lm_driver(dev->driver);

	if (lmdrv->resume)
		return lmdrv->resume(lmdev);
	return 0;
}

static struct bus_type lm_bustype = {
	.name		= "logicmodule",
	.match		= lm_match,
	.probe		= lm_bus_probe,
	.remove		= lm_bus_remove,
	.suspend	= lm_bus_suspend,
	.resume		= lm_bus_resume,
};

static int __init lm_init(void)
{
	return bus_register(&lm_bustype);
}
postcore_initcall(lm_init);

int lm_driver_register(struct lm_driver *drv)
{
	drv->drv.bus = &lm_bustype;
	return driver_register(&drv->drv);
}

void lm_driver_unregister(struct lm_driver *drv)
{
	driver_unregister(&drv->drv);
}

static void lm_device_release(struct device *dev)
{
	struct lm_device *d = to_lm_device(dev);

	kfree(d);
}

int lm_device_register(struct lm_device *dev)
{
	int ret;

	dev->dev.release = lm_device_release;
	dev->dev.bus = &lm_bustype;

	if (dev->dev.init_name) {
		ret = dev_set_name(&dev->dev, dev->dev.init_name, dev->id);
	} else {
		ret = dev_set_name(&dev->dev, "lm%d", dev->id);
	}
	if (ret) {
		WARN(1, "lm device set name fail\n");
		return ret;
	}

	dev->resource.name = dev_name(&dev->dev);

	ret = request_resource(&iomem_resource, &dev->resource);
	if (ret == 0) {
		ret = device_register(&dev->dev);
		if (ret) {
			release_resource(&dev->resource);
			WARN(1, "lm device register device fail\n");
		}
	} else
		WARN(1, "lm device request resource fail\n");

	return ret;
}

EXPORT_SYMBOL(lm_driver_register);
EXPORT_SYMBOL(lm_driver_unregister);