1/*
2 * Derived from drm_pci.c
3 *
4 * Copyright 2003 José Fonseca.
5 * Copyright 2003 Leif Delgass.
6 * Copyright (c) 2009, Code Aurora Forum.
7 * All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
23 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#include <linux/export.h>
29#include "drmP.h"
30
31/**
32 * Register.
33 *
34 * \param platdev - Platform device struture
35 * \return zero on success or a negative number on failure.
36 *
37 * Attempt to gets inter module "drm" information. If we are first
38 * then register the character device and inter module information.
39 * Try and register, if we fail to register, backout previous work.
40 */
41
42int drm_get_platform_dev(struct platform_device *platdev,
43			 struct drm_driver *driver)
44{
45	struct drm_device *dev;
46	int ret;
47
48	DRM_DEBUG("\n");
49
50	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
51	if (!dev)
52		return -ENOMEM;
53
54	dev->platformdev = platdev;
55	dev->dev = &platdev->dev;
56
57	mutex_lock(&drm_global_mutex);
58
59	ret = drm_fill_in_dev(dev, NULL, driver);
60
61	if (ret) {
62		printk(KERN_ERR "DRM: Fill_in_dev failed.\n");
63		goto err_g1;
64	}
65
66	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
67		dev_set_drvdata(&platdev->dev, dev);
68		ret = drm_get_minor(dev, &dev->control, DRM_MINOR_CONTROL);
69		if (ret)
70			goto err_g1;
71	}
72
73	ret = drm_get_minor(dev, &dev->primary, DRM_MINOR_LEGACY);
74	if (ret)
75		goto err_g2;
76
77	if (dev->driver->load) {
78		ret = dev->driver->load(dev, 0);
79		if (ret)
80			goto err_g3;
81	}
82
83	/* setup the grouping for the legacy output */
84	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
85		ret = drm_mode_group_init_legacy_group(dev,
86				&dev->primary->mode_group);
87		if (ret)
88			goto err_g3;
89	}
90
91	list_add_tail(&dev->driver_item, &driver->device_list);
92
93	mutex_unlock(&drm_global_mutex);
94
95	DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
96		 driver->name, driver->major, driver->minor, driver->patchlevel,
97		 driver->date, dev->primary->index);
98
99	return 0;
100
101err_g3:
102	drm_put_minor(&dev->primary);
103err_g2:
104	if (drm_core_check_feature(dev, DRIVER_MODESET))
105		drm_put_minor(&dev->control);
106err_g1:
107	kfree(dev);
108	mutex_unlock(&drm_global_mutex);
109	return ret;
110}
111EXPORT_SYMBOL(drm_get_platform_dev);
112
113static int drm_platform_get_irq(struct drm_device *dev)
114{
115	return platform_get_irq(dev->platformdev, 0);
116}
117
118static const char *drm_platform_get_name(struct drm_device *dev)
119{
120	return dev->platformdev->name;
121}
122
123static int drm_platform_set_busid(struct drm_device *dev, struct drm_master *master)
124{
125	int len, ret, id;
126
127	master->unique_len = 13 + strlen(dev->platformdev->name);
128	master->unique_size = master->unique_len;
129	master->unique = kmalloc(master->unique_len + 1, GFP_KERNEL);
130
131	if (master->unique == NULL)
132		return -ENOMEM;
133
134	id = dev->platformdev->id;
135
136	/* if only a single instance of the platform device, id will be
137	 * set to -1.. use 0 instead to avoid a funny looking bus-id:
138	 */
139	if (id == -1)
140		id = 0;
141
142	len = snprintf(master->unique, master->unique_len,
143			"platform:%s:%02d", dev->platformdev->name, id);
144
145	if (len > master->unique_len) {
146		DRM_ERROR("Unique buffer overflowed\n");
147		ret = -EINVAL;
148		goto err;
149	}
150
151	dev->devname =
152		kmalloc(strlen(dev->platformdev->name) +
153			master->unique_len + 2, GFP_KERNEL);
154
155	if (dev->devname == NULL) {
156		ret = -ENOMEM;
157		goto err;
158	}
159
160	sprintf(dev->devname, "%s@%s", dev->platformdev->name,
161		master->unique);
162	return 0;
163err:
164	return ret;
165}
166
167static struct drm_bus drm_platform_bus = {
168	.bus_type = DRIVER_BUS_PLATFORM,
169	.get_irq = drm_platform_get_irq,
170	.get_name = drm_platform_get_name,
171	.set_busid = drm_platform_set_busid,
172};
173
174/**
175 * Platform device initialization. Called direct from modules.
176 *
177 * \return zero on success or a negative number on failure.
178 *
179 * Initializes a drm_device structures,registering the
180 * stubs
181 *
182 * Expands the \c DRIVER_PREINIT and \c DRIVER_POST_INIT macros before and
183 * after the initialization for driver customization.
184 */
185
186int drm_platform_init(struct drm_driver *driver, struct platform_device *platform_device)
187{
188	DRM_DEBUG("\n");
189
190	driver->kdriver.platform_device = platform_device;
191	driver->bus = &drm_platform_bus;
192	INIT_LIST_HEAD(&driver->device_list);
193	return drm_get_platform_dev(platform_device, driver);
194}
195EXPORT_SYMBOL(drm_platform_init);
196
197void drm_platform_exit(struct drm_driver *driver, struct platform_device *platform_device)
198{
199	struct drm_device *dev, *tmp;
200	DRM_DEBUG("\n");
201
202	list_for_each_entry_safe(dev, tmp, &driver->device_list, driver_item)
203		drm_put_dev(dev);
204	DRM_INFO("Module unloaded\n");
205}
206EXPORT_SYMBOL(drm_platform_exit);
207