]>
Commit | Line | Data |
---|---|---|
060b293b SW |
1 | /* |
2 | * visorbus_main.c | |
3703987c | 3 | * |
6f14cc18 | 4 | * Copyright � 2010 - 2015 UNISYS CORPORATION |
3703987c EA |
5 | * All rights reserved. |
6 | * | |
6f14cc18 BR |
7 | * This program is free software; you can redistribute it and/or modify it |
8 | * under the terms and conditions of the GNU General Public License, | |
9 | * version 2, as published by the Free Software Foundation. | |
3703987c EA |
10 | * |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or | |
14 | * NON INFRINGEMENT. See the GNU General Public License for more | |
15 | * details. | |
16 | */ | |
17 | ||
8217becc | 18 | #include <linux/debugfs.h> |
3703987c EA |
19 | #include <linux/uuid.h> |
20 | ||
4b78000e | 21 | #include "visorbus.h" |
c79b28f7 | 22 | #include "visorbus_private.h" |
4b78000e | 23 | |
060b293b | 24 | /* Display string that is guaranteed to be no longer the 99 characters */ |
38d56c2f EA |
25 | #define LINESIZE 99 |
26 | ||
3703987c | 27 | #define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c |
ffcdb101 | 28 | #define POLLJIFFIES_NORMALCHANNEL 10 |
3703987c | 29 | |
060b293b SW |
30 | /* stores whether bus_registration was successful */ |
31 | static bool initialized; | |
8217becc | 32 | static struct dentry *visorbus_debugfs_dir; |
6155a3cf | 33 | |
59fd2c8b PB |
34 | /* |
35 | * DEVICE type attributes | |
36 | * | |
37 | * The modalias file will contain the guid of the device. | |
38 | */ | |
39 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, | |
40 | char *buf) | |
41 | { | |
42 | struct visor_device *vdev; | |
43 | uuid_le guid; | |
44 | ||
45 | vdev = to_visor_device(dev); | |
46 | guid = visorchannel_get_uuid(vdev->visorchannel); | |
7a41385e | 47 | return sprintf(buf, "visorbus:%pUl\n", &guid); |
59fd2c8b PB |
48 | } |
49 | static DEVICE_ATTR_RO(modalias); | |
50 | ||
51 | static struct attribute *visorbus_dev_attrs[] = { | |
52 | &dev_attr_modalias.attr, | |
53 | NULL, | |
54 | }; | |
55 | ||
56 | /* sysfs example for bridge-only sysfs files using device_type's */ | |
57 | static const struct attribute_group visorbus_dev_group = { | |
58 | .attrs = visorbus_dev_attrs, | |
59 | }; | |
60 | ||
cf7281c9 | 61 | static const struct attribute_group *visorbus_dev_groups[] = { |
59fd2c8b PB |
62 | &visorbus_dev_group, |
63 | NULL, | |
64 | }; | |
65 | ||
3703987c | 66 | /* filled in with info about parent chipset driver when we register with it */ |
55c71eba | 67 | static struct visor_vbus_deviceinfo chipset_driverinfo; |
3703987c | 68 | /* filled in with info about this driver, wrt it servicing client busses */ |
55c71eba | 69 | static struct visor_vbus_deviceinfo clientbus_driverinfo; |
3703987c | 70 | |
3fd1b3b6 | 71 | /* list of visor_device structs, linked via .list_all */ |
3703987c | 72 | static LIST_HEAD(list_all_bus_instances); |
3fd1b3b6 | 73 | /* list of visor_device structs, linked via .list_all */ |
3703987c EA |
74 | static LIST_HEAD(list_all_device_instances); |
75 | ||
76 | static int | |
77 | visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env) | |
78 | { | |
59fd2c8b PB |
79 | struct visor_device *dev; |
80 | uuid_le guid; | |
81 | ||
82 | dev = to_visor_device(xdev); | |
83 | guid = visorchannel_get_uuid(dev->visorchannel); | |
84 | ||
f894c88a | 85 | return add_uevent_var(env, "MODALIAS=visorbus:%pUl", &guid); |
3703987c EA |
86 | } |
87 | ||
b84be59a | 88 | /* |
3fd1b3b6 DB |
89 | * visorbus_match() - called automatically upon adding a visor_device |
90 | * (device_add), or adding a visor_driver | |
91 | * (visorbus_register_visor_driver) | |
92 | * @xdev: struct device for the device being matched | |
93 | * @xdrv: struct device_driver for driver to match device against | |
94 | * | |
95 | * Return: 1 iff the provided driver can control the specified device | |
3703987c EA |
96 | */ |
97 | static int | |
98 | visorbus_match(struct device *xdev, struct device_driver *xdrv) | |
99 | { | |
100 | uuid_le channel_type; | |
3703987c EA |
101 | int i; |
102 | struct visor_device *dev; | |
103 | struct visor_driver *drv; | |
104 | ||
105 | dev = to_visor_device(xdev); | |
3703987c | 106 | channel_type = visorchannel_get_uuid(dev->visorchannel); |
d4e8a22e | 107 | drv = to_visor_driver(xdrv); |
3703987c | 108 | if (!drv->channel_types) |
8e33f48c DK |
109 | return 0; |
110 | ||
3703987c EA |
111 | for (i = 0; |
112 | (uuid_le_cmp(drv->channel_types[i].guid, NULL_UUID_LE) != 0) || | |
113 | (drv->channel_types[i].name); | |
114 | i++) | |
115 | if (uuid_le_cmp(drv->channel_types[i].guid, | |
8e33f48c DK |
116 | channel_type) == 0) |
117 | return i + 1; | |
118 | ||
119 | return 0; | |
3703987c EA |
120 | } |
121 | ||
7993b40c DK |
122 | /* |
123 | * This describes the TYPE of bus. | |
060b293b | 124 | * (Don't confuse this with an INSTANCE of the bus.) |
7993b40c DK |
125 | */ |
126 | struct bus_type visorbus_type = { | |
127 | .name = "visorbus", | |
128 | .match = visorbus_match, | |
129 | .uevent = visorbus_uevent, | |
130 | .dev_groups = visorbus_dev_groups, | |
7993b40c DK |
131 | }; |
132 | ||
b84be59a | 133 | /* |
ce4617ef DB |
134 | * visorbus_release_busdevice() - called when device_unregister() is called for |
135 | * the bus device instance, after all other tasks | |
136 | * involved with destroying the dev are complete | |
3fd1b3b6 | 137 | * @xdev: struct device for the bus being released |
3703987c EA |
138 | */ |
139 | static void | |
140 | visorbus_release_busdevice(struct device *xdev) | |
141 | { | |
343506bf | 142 | struct visor_device *dev = dev_get_drvdata(xdev); |
3703987c | 143 | |
8217becc TS |
144 | debugfs_remove(dev->debugfs_client_bus_info); |
145 | debugfs_remove_recursive(dev->debugfs_dir); | |
343506bf | 146 | kfree(dev); |
3703987c EA |
147 | } |
148 | ||
b84be59a | 149 | /* |
3fd1b3b6 DB |
150 | * visorbus_release_device() - called when device_unregister() is called for |
151 | * each child device instance | |
152 | * @xdev: struct device for the visor device being released | |
3703987c EA |
153 | */ |
154 | static void | |
155 | visorbus_release_device(struct device *xdev) | |
156 | { | |
157 | struct visor_device *dev = to_visor_device(xdev); | |
158 | ||
3703987c EA |
159 | if (dev->visorchannel) { |
160 | visorchannel_destroy(dev->visorchannel); | |
161 | dev->visorchannel = NULL; | |
162 | } | |
163 | kfree(dev); | |
164 | } | |
165 | ||
3fd1b3b6 DB |
166 | /* |
167 | * begin implementation of specific channel attributes to appear under | |
168 | * /sys/bus/visorbus<x>/dev<y>/channel | |
169 | */ | |
170 | ||
79573162 DZ |
171 | static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr, |
172 | char *buf) | |
826b6a0f | 173 | { |
79573162 DZ |
174 | struct visor_device *vdev = to_visor_device(dev); |
175 | ||
7a41385e DB |
176 | return sprintf(buf, "0x%llx\n", |
177 | visorchannel_get_physaddr(vdev->visorchannel)); | |
826b6a0f | 178 | } |
a25280b3 | 179 | static DEVICE_ATTR_RO(physaddr); |
826b6a0f | 180 | |
79573162 DZ |
181 | static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr, |
182 | char *buf) | |
826b6a0f | 183 | { |
79573162 DZ |
184 | struct visor_device *vdev = to_visor_device(dev); |
185 | ||
7a41385e | 186 | return sprintf(buf, "0x%lx\n", |
79573162 | 187 | visorchannel_get_nbytes(vdev->visorchannel)); |
826b6a0f | 188 | } |
a25280b3 | 189 | static DEVICE_ATTR_RO(nbytes); |
826b6a0f | 190 | |
79573162 DZ |
191 | static ssize_t clientpartition_show(struct device *dev, |
192 | struct device_attribute *attr, char *buf) | |
193 | { | |
194 | struct visor_device *vdev = to_visor_device(dev); | |
195 | ||
7a41385e DB |
196 | return sprintf(buf, "0x%llx\n", |
197 | visorchannel_get_clientpartition(vdev->visorchannel)); | |
826b6a0f | 198 | } |
a25280b3 | 199 | static DEVICE_ATTR_RO(clientpartition); |
826b6a0f | 200 | |
79573162 DZ |
201 | static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr, |
202 | char *buf) | |
826b6a0f | 203 | { |
79573162 | 204 | struct visor_device *vdev = to_visor_device(dev); |
38d56c2f | 205 | char typeid[LINESIZE]; |
826b6a0f | 206 | |
7a41385e DB |
207 | return sprintf(buf, "%s\n", |
208 | visorchannel_id(vdev->visorchannel, typeid)); | |
826b6a0f | 209 | } |
a25280b3 | 210 | static DEVICE_ATTR_RO(typeguid); |
826b6a0f | 211 | |
79573162 DZ |
212 | static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr, |
213 | char *buf) | |
826b6a0f | 214 | { |
79573162 | 215 | struct visor_device *vdev = to_visor_device(dev); |
38d56c2f | 216 | char zoneid[LINESIZE]; |
826b6a0f | 217 | |
7a41385e DB |
218 | return sprintf(buf, "%s\n", |
219 | visorchannel_zoneid(vdev->visorchannel, zoneid)); | |
826b6a0f | 220 | } |
a25280b3 | 221 | static DEVICE_ATTR_RO(zoneguid); |
826b6a0f | 222 | |
79573162 DZ |
223 | static ssize_t typename_show(struct device *dev, struct device_attribute *attr, |
224 | char *buf) | |
826b6a0f PB |
225 | { |
226 | int i = 0; | |
79573162 DZ |
227 | struct bus_type *xbus = dev->bus; |
228 | struct device_driver *xdrv = dev->driver; | |
826b6a0f PB |
229 | struct visor_driver *drv = NULL; |
230 | ||
bf8d0e94 | 231 | if (!xbus || !xdrv) |
826b6a0f | 232 | return 0; |
79573162 | 233 | i = xbus->match(dev, xdrv); |
826b6a0f PB |
234 | if (!i) |
235 | return 0; | |
236 | drv = to_visor_driver(xdrv); | |
7a41385e | 237 | return sprintf(buf, "%s\n", drv->channel_types[i - 1].name); |
826b6a0f | 238 | } |
79573162 | 239 | static DEVICE_ATTR_RO(typename); |
79573162 DZ |
240 | |
241 | static struct attribute *channel_attrs[] = { | |
b372fee1 ZD |
242 | &dev_attr_physaddr.attr, |
243 | &dev_attr_nbytes.attr, | |
244 | &dev_attr_clientpartition.attr, | |
245 | &dev_attr_typeguid.attr, | |
246 | &dev_attr_zoneguid.attr, | |
247 | &dev_attr_typename.attr, | |
248 | NULL | |
826b6a0f PB |
249 | }; |
250 | ||
79573162 | 251 | static struct attribute_group channel_attr_grp = { |
b372fee1 ZD |
252 | .name = "channel", |
253 | .attrs = channel_attrs, | |
826b6a0f PB |
254 | }; |
255 | ||
59fd2c8b | 256 | static const struct attribute_group *visorbus_channel_groups[] = { |
b372fee1 ZD |
257 | &channel_attr_grp, |
258 | NULL | |
826b6a0f PB |
259 | }; |
260 | ||
79573162 | 261 | /* end implementation of specific channel attributes */ |
826b6a0f | 262 | |
3fd1b3b6 DB |
263 | /* |
264 | * BUS instance attributes | |
3703987c EA |
265 | * |
266 | * define & implement display of bus attributes under | |
3fd1b3b6 | 267 | * /sys/bus/visorbus/devices/visorbus<n>. |
3703987c EA |
268 | */ |
269 | ||
d181dd03 DZ |
270 | static ssize_t partition_handle_show(struct device *dev, |
271 | struct device_attribute *attr, | |
7b9de71b EA |
272 | char *buf) |
273 | { | |
5ecbd5d4 DZ |
274 | struct visor_device *vdev = to_visor_device(dev); |
275 | u64 handle = visorchannel_get_clientpartition(vdev->visorchannel); | |
3703987c | 276 | |
7a41385e | 277 | return sprintf(buf, "0x%llx\n", handle); |
3703987c | 278 | } |
a25280b3 | 279 | static DEVICE_ATTR_RO(partition_handle); |
3703987c | 280 | |
d181dd03 DZ |
281 | static ssize_t partition_guid_show(struct device *dev, |
282 | struct device_attribute *attr, | |
7b9de71b EA |
283 | char *buf) |
284 | { | |
5ecbd5d4 | 285 | struct visor_device *vdev = to_visor_device(dev); |
3703987c | 286 | |
7a41385e | 287 | return sprintf(buf, "{%pUb}\n", &vdev->partition_uuid); |
3703987c | 288 | } |
a25280b3 | 289 | static DEVICE_ATTR_RO(partition_guid); |
3703987c | 290 | |
d181dd03 DZ |
291 | static ssize_t partition_name_show(struct device *dev, |
292 | struct device_attribute *attr, | |
7b9de71b EA |
293 | char *buf) |
294 | { | |
5ecbd5d4 | 295 | struct visor_device *vdev = to_visor_device(dev); |
3703987c | 296 | |
7a41385e | 297 | return sprintf(buf, "%s\n", vdev->name); |
3703987c | 298 | } |
a25280b3 | 299 | static DEVICE_ATTR_RO(partition_name); |
3703987c | 300 | |
d181dd03 DZ |
301 | static ssize_t channel_addr_show(struct device *dev, |
302 | struct device_attribute *attr, | |
7b9de71b EA |
303 | char *buf) |
304 | { | |
5ecbd5d4 DZ |
305 | struct visor_device *vdev = to_visor_device(dev); |
306 | u64 addr = visorchannel_get_physaddr(vdev->visorchannel); | |
3703987c | 307 | |
7a41385e | 308 | return sprintf(buf, "0x%llx\n", addr); |
3703987c | 309 | } |
a25280b3 | 310 | static DEVICE_ATTR_RO(channel_addr); |
3703987c | 311 | |
d181dd03 DZ |
312 | static ssize_t channel_bytes_show(struct device *dev, |
313 | struct device_attribute *attr, | |
7b9de71b EA |
314 | char *buf) |
315 | { | |
5ecbd5d4 DZ |
316 | struct visor_device *vdev = to_visor_device(dev); |
317 | u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel); | |
3703987c | 318 | |
7a41385e | 319 | return sprintf(buf, "0x%llx\n", nbytes); |
3703987c | 320 | } |
a25280b3 | 321 | static DEVICE_ATTR_RO(channel_bytes); |
3703987c | 322 | |
d181dd03 DZ |
323 | static ssize_t channel_id_show(struct device *dev, |
324 | struct device_attribute *attr, | |
7b9de71b EA |
325 | char *buf) |
326 | { | |
5ecbd5d4 | 327 | struct visor_device *vdev = to_visor_device(dev); |
3703987c EA |
328 | int len = 0; |
329 | ||
bf8d0e94 DB |
330 | visorchannel_id(vdev->visorchannel, buf); |
331 | len = strlen(buf); | |
332 | buf[len++] = '\n'; | |
333 | ||
3703987c EA |
334 | return len; |
335 | } | |
a25280b3 | 336 | static DEVICE_ATTR_RO(channel_id); |
3703987c | 337 | |
d181dd03 | 338 | static struct attribute *dev_attrs[] = { |
b372fee1 ZD |
339 | &dev_attr_partition_handle.attr, |
340 | &dev_attr_partition_guid.attr, | |
341 | &dev_attr_partition_name.attr, | |
342 | &dev_attr_channel_addr.attr, | |
343 | &dev_attr_channel_bytes.attr, | |
344 | &dev_attr_channel_id.attr, | |
345 | NULL | |
d181dd03 | 346 | }; |
3703987c | 347 | |
d181dd03 | 348 | static struct attribute_group dev_attr_grp = { |
b372fee1 | 349 | .attrs = dev_attrs, |
d181dd03 | 350 | }; |
3703987c | 351 | |
d181dd03 | 352 | static const struct attribute_group *visorbus_groups[] = { |
b372fee1 ZD |
353 | &dev_attr_grp, |
354 | NULL | |
d181dd03 | 355 | }; |
3703987c | 356 | |
8217becc TS |
357 | /* |
358 | * BUS debugfs entries | |
359 | * | |
360 | * define & implement display of debugfs attributes under | |
361 | * /sys/kernel/debug/visorbus/visorbus<n>. | |
362 | */ | |
d4e8a22e | 363 | |
738561a8 | 364 | /* |
55c71eba | 365 | * vbuschannel_print_devinfo() - format a struct visor_vbus_deviceinfo |
738561a8 | 366 | * and write it to a seq_file |
55c71eba | 367 | * @devinfo: the struct visor_vbus_deviceinfo to format |
738561a8 DK |
368 | * @seq: seq_file to write to |
369 | * @devix: the device index to be included in the output data, or -1 if no | |
370 | * device index is to be included | |
371 | * | |
372 | * Reads @devInfo, and writes it in human-readable notation to @seq. | |
373 | */ | |
374 | static void | |
55c71eba | 375 | vbuschannel_print_devinfo(struct visor_vbus_deviceinfo *devinfo, |
738561a8 DK |
376 | struct seq_file *seq, int devix) |
377 | { | |
060b293b | 378 | /* uninitialized vbus device entry */ |
738561a8 | 379 | if (!isprint(devinfo->devtype[0])) |
060b293b | 380 | return; |
738561a8 DK |
381 | |
382 | if (devix >= 0) | |
383 | seq_printf(seq, "[%d]", devix); | |
384 | else | |
385 | /* vbus device entry is for bus or chipset */ | |
386 | seq_puts(seq, " "); | |
387 | ||
388 | /* | |
389 | * Note: because the s-Par back-end is free to scribble in this area, | |
390 | * we never assume '\0'-termination. | |
391 | */ | |
392 | seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->devtype), | |
393 | (int)sizeof(devinfo->devtype), devinfo->devtype); | |
394 | seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->drvname), | |
395 | (int)sizeof(devinfo->drvname), devinfo->drvname); | |
396 | seq_printf(seq, "%.*s\n", (int)sizeof(devinfo->infostrs), | |
397 | devinfo->infostrs); | |
398 | } | |
8217becc TS |
399 | |
400 | static int client_bus_info_debugfs_show(struct seq_file *seq, void *v) | |
401 | { | |
d4e8a22e | 402 | int i = 0; |
8217becc | 403 | unsigned long off; |
55c71eba | 404 | struct visor_vbus_deviceinfo dev_info; |
d4e8a22e DB |
405 | struct visor_device *vdev = seq->private; |
406 | struct visorchannel *channel = vdev->visorchannel; | |
8217becc TS |
407 | |
408 | if (!channel) | |
409 | return 0; | |
410 | ||
411 | seq_printf(seq, | |
412 | "Client device / client driver info for %s partition (vbus #%u):\n", | |
413 | ((vdev->name) ? (char *)(vdev->name) : ""), | |
414 | vdev->chipset_bus_no); | |
d4e8a22e | 415 | |
8217becc | 416 | if (visorchannel_read(channel, |
55c71eba | 417 | offsetof(struct visor_vbus_channel, chp_info), |
8217becc TS |
418 | &dev_info, sizeof(dev_info)) >= 0) |
419 | vbuschannel_print_devinfo(&dev_info, seq, -1); | |
420 | if (visorchannel_read(channel, | |
55c71eba | 421 | offsetof(struct visor_vbus_channel, bus_info), |
8217becc TS |
422 | &dev_info, sizeof(dev_info)) >= 0) |
423 | vbuschannel_print_devinfo(&dev_info, seq, -1); | |
d4e8a22e | 424 | |
55c71eba | 425 | off = offsetof(struct visor_vbus_channel, dev_info); |
8217becc TS |
426 | while (off + sizeof(dev_info) <= visorchannel_get_nbytes(channel)) { |
427 | if (visorchannel_read(channel, off, &dev_info, | |
428 | sizeof(dev_info)) >= 0) | |
429 | vbuschannel_print_devinfo(&dev_info, seq, i); | |
430 | off += sizeof(dev_info); | |
431 | i++; | |
432 | } | |
433 | ||
434 | return 0; | |
435 | } | |
436 | ||
437 | static int client_bus_info_debugfs_open(struct inode *inode, struct file *file) | |
438 | { | |
439 | return single_open(file, client_bus_info_debugfs_show, | |
440 | inode->i_private); | |
441 | } | |
442 | ||
443 | static const struct file_operations client_bus_info_debugfs_fops = { | |
444 | .owner = THIS_MODULE, | |
445 | .open = client_bus_info_debugfs_open, | |
446 | .read = seq_read, | |
447 | .llseek = seq_lseek, | |
448 | .release = single_release, | |
449 | }; | |
450 | ||
3703987c | 451 | static void |
9ebab649 | 452 | dev_periodic_work(unsigned long __opaque) |
3703987c | 453 | { |
9ebab649 | 454 | struct visor_device *dev = (struct visor_device *)__opaque; |
3703987c EA |
455 | struct visor_driver *drv = to_visor_driver(dev->device.driver); |
456 | ||
396e36c9 | 457 | drv->channel_interrupt(dev); |
9ebab649 | 458 | mod_timer(&dev->timer, jiffies + POLLJIFFIES_NORMALCHANNEL); |
3703987c EA |
459 | } |
460 | ||
b90194d9 | 461 | static int |
3703987c EA |
462 | dev_start_periodic_work(struct visor_device *dev) |
463 | { | |
9ebab649 | 464 | if (dev->being_removed || dev->timer_active) |
b90194d9 | 465 | return -EINVAL; |
3703987c EA |
466 | /* now up by at least 2 */ |
467 | get_device(&dev->device); | |
9ebab649 TS |
468 | dev->timer.expires = jiffies + POLLJIFFIES_NORMALCHANNEL; |
469 | add_timer(&dev->timer); | |
470 | dev->timer_active = true; | |
b90194d9 | 471 | return 0; |
3703987c EA |
472 | } |
473 | ||
474 | static void | |
475 | dev_stop_periodic_work(struct visor_device *dev) | |
476 | { | |
9ebab649 TS |
477 | if (!dev->timer_active) |
478 | return; | |
479 | del_timer_sync(&dev->timer); | |
480 | dev->timer_active = false; | |
481 | put_device(&dev->device); | |
3703987c EA |
482 | } |
483 | ||
b84be59a | 484 | /* |
3fd1b3b6 DB |
485 | * visordriver_remove_device() - handle visor device going away |
486 | * @xdev: struct device for the visor device being removed | |
487 | * | |
488 | * This is called when device_unregister() is called for each child device | |
489 | * instance, to notify the appropriate visorbus function driver that the device | |
490 | * is going away, and to decrease the reference count of the device. | |
491 | * | |
492 | * Return: 0 iff successful | |
3703987c EA |
493 | */ |
494 | static int | |
495 | visordriver_remove_device(struct device *xdev) | |
496 | { | |
3703987c EA |
497 | struct visor_device *dev; |
498 | struct visor_driver *drv; | |
499 | ||
500 | dev = to_visor_device(xdev); | |
501 | drv = to_visor_driver(xdev->driver); | |
d4e8a22e | 502 | |
d505855e | 503 | mutex_lock(&dev->visordriver_callback_lock); |
779d0752 | 504 | dev->being_removed = true; |
64938182 DK |
505 | if (drv->remove) |
506 | drv->remove(dev); | |
d505855e | 507 | mutex_unlock(&dev->visordriver_callback_lock); |
3703987c | 508 | |
d4e8a22e | 509 | dev_stop_periodic_work(dev); |
3703987c | 510 | put_device(&dev->device); |
d4e8a22e | 511 | |
df7f46e8 | 512 | return 0; |
3703987c EA |
513 | } |
514 | ||
060b293b | 515 | /* |
3fd1b3b6 DB |
516 | * visorbus_unregister_visor_driver() - unregisters the provided driver |
517 | * @drv: the driver to unregister | |
518 | * | |
519 | * A visor function driver calls this function to unregister the driver, | |
520 | * i.e., within its module_exit function. | |
3703987c EA |
521 | */ |
522 | void | |
523 | visorbus_unregister_visor_driver(struct visor_driver *drv) | |
524 | { | |
3703987c EA |
525 | driver_unregister(&drv->driver); |
526 | } | |
527 | EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver); | |
528 | ||
060b293b | 529 | /* |
3fd1b3b6 DB |
530 | * visorbus_read_channel() - reads from the designated channel into |
531 | * the provided buffer | |
532 | * @dev: the device whose channel is read from | |
533 | * @offset: the offset into the channel at which reading starts | |
534 | * @dest: the destination buffer that is written into from the channel | |
535 | * @nbytes: the number of bytes to read from the channel | |
536 | * | |
537 | * If receiving a message, use the visorchannel_signalremove() | |
538 | * function instead. | |
539 | * | |
540 | * Return: integer indicating success (zero) or failure (non-zero) | |
541 | */ | |
3703987c EA |
542 | int |
543 | visorbus_read_channel(struct visor_device *dev, unsigned long offset, | |
544 | void *dest, unsigned long nbytes) | |
545 | { | |
546 | return visorchannel_read(dev->visorchannel, offset, dest, nbytes); | |
547 | } | |
548 | EXPORT_SYMBOL_GPL(visorbus_read_channel); | |
549 | ||
060b293b | 550 | /* |
3fd1b3b6 DB |
551 | * visorbus_write_channel() - writes the provided buffer into the designated |
552 | * channel | |
553 | * @dev: the device whose channel is written to | |
554 | * @offset: the offset into the channel at which writing starts | |
555 | * @src: the source buffer that is written into the channel | |
556 | * @nbytes: the number of bytes to write into the channel | |
557 | * | |
558 | * If sending a message, use the visorchannel_signalinsert() | |
559 | * function instead. | |
560 | * | |
561 | * Return: integer indicating success (zero) or failure (non-zero) | |
562 | */ | |
3703987c EA |
563 | int |
564 | visorbus_write_channel(struct visor_device *dev, unsigned long offset, | |
565 | void *src, unsigned long nbytes) | |
566 | { | |
567 | return visorchannel_write(dev->visorchannel, offset, src, nbytes); | |
568 | } | |
569 | EXPORT_SYMBOL_GPL(visorbus_write_channel); | |
570 | ||
060b293b | 571 | /* |
3fd1b3b6 DB |
572 | * visorbus_enable_channel_interrupts() - enables interrupts on the |
573 | * designated device | |
574 | * @dev: the device on which to enable interrupts | |
575 | * | |
576 | * Currently we don't yet have a real interrupt, so for now we just call the | |
577 | * interrupt function periodically via a timer. | |
3703987c | 578 | */ |
5dca9b29 | 579 | int |
3703987c EA |
580 | visorbus_enable_channel_interrupts(struct visor_device *dev) |
581 | { | |
396e36c9 TS |
582 | struct visor_driver *drv = to_visor_driver(dev->device.driver); |
583 | ||
584 | if (!drv->channel_interrupt) { | |
585 | dev_err(&dev->device, "%s no interrupt function!\n", __func__); | |
5dca9b29 | 586 | return -ENOENT; |
396e36c9 TS |
587 | } |
588 | ||
5dca9b29 | 589 | return dev_start_periodic_work(dev); |
3703987c EA |
590 | } |
591 | EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts); | |
592 | ||
060b293b | 593 | /* |
3fd1b3b6 DB |
594 | * visorbus_disable_channel_interrupts() - disables interrupts on the |
595 | * designated device | |
596 | * @dev: the device on which to disable interrupts | |
597 | */ | |
3703987c EA |
598 | void |
599 | visorbus_disable_channel_interrupts(struct visor_device *dev) | |
600 | { | |
601 | dev_stop_periodic_work(dev); | |
602 | } | |
603 | EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts); | |
604 | ||
b84be59a | 605 | /* |
3fd1b3b6 DB |
606 | * create_visor_device() - create visor device as a result of receiving the |
607 | * controlvm device_create message for a new device | |
608 | * @dev: a freshly-zeroed struct visor_device, containing only filled-in values | |
609 | * for chipset_bus_no and chipset_dev_no, that will be initialized | |
610 | * | |
611 | * This is how everything starts from the device end. | |
612 | * This function is called when a channel first appears via a ControlVM | |
613 | * message. In response, this function allocates a visor_device to | |
614 | * correspond to the new channel, and attempts to connect it the appropriate | |
615 | * driver. If the appropriate driver is found, the visor_driver.probe() | |
616 | * function for that driver will be called, and will be passed the new | |
617 | * visor_device that we just created. | |
3703987c | 618 | * |
3fd1b3b6 DB |
619 | * It's ok if the appropriate driver is not yet loaded, because in that case |
620 | * the new device struct will just stick around in the bus' list of devices. | |
621 | * When the appropriate driver calls visorbus_register_visor_driver(), the | |
622 | * visor_driver.probe() for the new driver will be called with the new | |
623 | * device. | |
624 | * | |
625 | * Return: 0 if successful, otherwise the negative value returned by | |
626 | * device_add() indicating the reason for failure | |
3703987c EA |
627 | */ |
628 | static int | |
a298bc0b | 629 | create_visor_device(struct visor_device *dev) |
3703987c | 630 | { |
0b2acf34 | 631 | int err; |
a298bc0b DZ |
632 | u32 chipset_bus_no = dev->chipset_bus_no; |
633 | u32 chipset_dev_no = dev->chipset_dev_no; | |
3703987c | 634 | |
d505855e | 635 | mutex_init(&dev->visordriver_callback_lock); |
3703987c | 636 | dev->device.bus = &visorbus_type; |
59fd2c8b | 637 | dev->device.groups = visorbus_channel_groups; |
3703987c EA |
638 | device_initialize(&dev->device); |
639 | dev->device.release = visorbus_release_device; | |
640 | /* keep a reference just for us (now 2) */ | |
641 | get_device(&dev->device); | |
614dd644 | 642 | setup_timer(&dev->timer, dev_periodic_work, (unsigned long)dev); |
3703987c | 643 | |
3fd1b3b6 DB |
644 | /* |
645 | * bus_id must be a unique name with respect to this bus TYPE | |
3703987c EA |
646 | * (NOT bus instance). That's why we need to include the bus |
647 | * number within the name. | |
648 | */ | |
e0d210ae DK |
649 | err = dev_set_name(&dev->device, "vbus%u:dev%u", |
650 | chipset_bus_no, chipset_dev_no); | |
651 | if (err) | |
652 | goto err_put; | |
3703987c | 653 | |
3fd1b3b6 DB |
654 | /* |
655 | * device_add does this: | |
3703987c EA |
656 | * bus_add_device(dev) |
657 | * ->device_attach(dev) | |
658 | * ->for each driver drv registered on the bus that dev is on | |
659 | * if (dev.drv) ** device already has a driver ** | |
660 | * ** not sure we could ever get here... ** | |
661 | * else | |
662 | * if (bus.match(dev,drv)) [visorbus_match] | |
663 | * dev.drv = drv | |
664 | * if (!drv.probe(dev)) [visordriver_probe_device] | |
665 | * dev.drv = NULL | |
666 | * | |
667 | * Note that device_add does NOT fail if no driver failed to | |
668 | * claim the device. The device will be linked onto | |
669 | * bus_type.klist_devices regardless (use bus_for_each_dev). | |
670 | */ | |
0b2acf34 | 671 | err = device_add(&dev->device); |
ebdc1b88 | 672 | if (err < 0) |
0b2acf34 | 673 | goto err_put; |
3703987c | 674 | |
a298bc0b | 675 | list_add_tail(&dev->list_all, &list_all_device_instances); |
060b293b SW |
676 | /* success: reference kept via unmatched get_device() */ |
677 | return 0; | |
3703987c | 678 | |
0b2acf34 | 679 | err_put: |
a298bc0b | 680 | put_device(&dev->device); |
ebdc1b88 | 681 | dev_err(&dev->device, "Creating visor device failed. %d\n", err); |
0b2acf34 | 682 | return err; |
3703987c EA |
683 | } |
684 | ||
685 | static void | |
686 | remove_visor_device(struct visor_device *dev) | |
687 | { | |
688 | list_del(&dev->list_all); | |
3703987c EA |
689 | put_device(&dev->device); |
690 | device_unregister(&dev->device); | |
691 | } | |
692 | ||
3703987c EA |
693 | static int |
694 | get_vbus_header_info(struct visorchannel *chan, | |
55c71eba | 695 | struct visor_vbus_headerinfo *hdr_info) |
3703987c | 696 | { |
711c67f4 DK |
697 | int err; |
698 | ||
315dfc84 SW |
699 | if (!visor_check_channel(visorchannel_get_header(chan), |
700 | visor_vbus_channel_uuid, | |
701 | "vbus", | |
702 | sizeof(struct visor_vbus_channel), | |
703 | VISOR_VBUS_CHANNEL_VERSIONID, | |
704 | VISOR_VBUS_CHANNEL_SIGNATURE)) | |
f748f64f BR |
705 | return -EINVAL; |
706 | ||
711c67f4 DK |
707 | err = visorchannel_read(chan, sizeof(struct channel_header), hdr_info, |
708 | sizeof(*hdr_info)); | |
709 | if (err < 0) | |
710 | return err; | |
711 | ||
55c71eba | 712 | if (hdr_info->struct_bytes < sizeof(struct visor_vbus_headerinfo)) |
f748f64f BR |
713 | return -EINVAL; |
714 | ||
3703987c | 715 | if (hdr_info->device_info_struct_bytes < |
55c71eba | 716 | sizeof(struct visor_vbus_deviceinfo)) |
f748f64f | 717 | return -EINVAL; |
493d1597 | 718 | |
f748f64f | 719 | return 0; |
3703987c EA |
720 | } |
721 | ||
b84be59a | 722 | /* |
3fd1b3b6 | 723 | * write_vbus_chp_info() - write the contents of <info> to the struct |
55c71eba | 724 | * visor_vbus_channel.chp_info |
3fd1b3b6 DB |
725 | * @chan: indentifies the s-Par channel that will be updated |
726 | * @hdr_info: used to find appropriate channel offset to write data | |
727 | * @info: contains the information to write | |
728 | * | |
729 | * Writes chipset info into the channel memory to be used for diagnostic | |
730 | * purposes. | |
ff13cf37 | 731 | * |
3fd1b3b6 | 732 | * Returns no value since this is debug information and not needed for |
ff13cf37 | 733 | * device functionality. |
48117895 | 734 | */ |
ff13cf37 | 735 | static void |
3703987c | 736 | write_vbus_chp_info(struct visorchannel *chan, |
55c71eba SW |
737 | struct visor_vbus_headerinfo *hdr_info, |
738 | struct visor_vbus_deviceinfo *info) | |
3703987c EA |
739 | { |
740 | int off = sizeof(struct channel_header) + hdr_info->chp_info_offset; | |
741 | ||
742 | if (hdr_info->chp_info_offset == 0) | |
ff13cf37 | 743 | return; |
3703987c | 744 | |
ff13cf37 | 745 | visorchannel_write(chan, off, info, sizeof(*info)); |
3703987c EA |
746 | } |
747 | ||
b84be59a | 748 | /* |
3fd1b3b6 | 749 | * write_vbus_bus_info() - write the contents of <info> to the struct |
55c71eba | 750 | * visor_vbus_channel.bus_info |
3fd1b3b6 DB |
751 | * @chan: indentifies the s-Par channel that will be updated |
752 | * @hdr_info: used to find appropriate channel offset to write data | |
753 | * @info: contains the information to write | |
754 | * | |
755 | * Writes bus info into the channel memory to be used for diagnostic | |
756 | * purposes. | |
ff13cf37 | 757 | * |
3fd1b3b6 | 758 | * Returns no value since this is debug information and not needed for |
ff13cf37 | 759 | * device functionality. |
48117895 | 760 | */ |
ff13cf37 | 761 | static void |
3703987c | 762 | write_vbus_bus_info(struct visorchannel *chan, |
55c71eba SW |
763 | struct visor_vbus_headerinfo *hdr_info, |
764 | struct visor_vbus_deviceinfo *info) | |
3703987c EA |
765 | { |
766 | int off = sizeof(struct channel_header) + hdr_info->bus_info_offset; | |
767 | ||
768 | if (hdr_info->bus_info_offset == 0) | |
ff13cf37 | 769 | return; |
3703987c | 770 | |
ff13cf37 | 771 | visorchannel_write(chan, off, info, sizeof(*info)); |
3703987c EA |
772 | } |
773 | ||
b84be59a | 774 | /* |
3fd1b3b6 | 775 | * write_vbus_dev_info() - write the contents of <info> to the struct |
55c71eba | 776 | * visor_vbus_channel.dev_info[<devix>] |
3fd1b3b6 DB |
777 | * @chan: indentifies the s-Par channel that will be updated |
778 | * @hdr_info: used to find appropriate channel offset to write data | |
779 | * @info: contains the information to write | |
780 | * @devix: the relative device number (0..n-1) of the device on the bus | |
ff13cf37 | 781 | * |
3fd1b3b6 DB |
782 | * Writes device info into the channel memory to be used for diagnostic |
783 | * purposes. | |
784 | * | |
785 | * Returns no value since this is debug information and not needed for | |
ff13cf37 | 786 | * device functionality. |
3703987c | 787 | */ |
ff13cf37 | 788 | static void |
3703987c | 789 | write_vbus_dev_info(struct visorchannel *chan, |
55c71eba SW |
790 | struct visor_vbus_headerinfo *hdr_info, |
791 | struct visor_vbus_deviceinfo *info, unsigned int devix) | |
3703987c EA |
792 | { |
793 | int off = | |
794 | (sizeof(struct channel_header) + hdr_info->dev_info_offset) + | |
795 | (hdr_info->device_info_struct_bytes * devix); | |
796 | ||
797 | if (hdr_info->dev_info_offset == 0) | |
ff13cf37 | 798 | return; |
3703987c | 799 | |
ff13cf37 | 800 | visorchannel_write(chan, off, info, sizeof(*info)); |
3703987c EA |
801 | } |
802 | ||
b5577d79 | 803 | static void bus_device_info_init( |
55c71eba | 804 | struct visor_vbus_deviceinfo *bus_device_info_ptr, |
b5577d79 DK |
805 | const char *dev_type, const char *drv_name) |
806 | { | |
55c71eba | 807 | memset(bus_device_info_ptr, 0, sizeof(struct visor_vbus_deviceinfo)); |
b5577d79 DK |
808 | snprintf(bus_device_info_ptr->devtype, |
809 | sizeof(bus_device_info_ptr->devtype), | |
810 | "%s", (dev_type) ? dev_type : "unknownType"); | |
811 | snprintf(bus_device_info_ptr->drvname, | |
812 | sizeof(bus_device_info_ptr->drvname), | |
813 | "%s", (drv_name) ? drv_name : "unknownDriver"); | |
814 | snprintf(bus_device_info_ptr->infostrs, | |
815 | sizeof(bus_device_info_ptr->infostrs), "kernel ver. %s", | |
816 | utsname()->release); | |
817 | } | |
818 | ||
b84be59a | 819 | /* |
3fd1b3b6 DB |
820 | * fix_vbus_dev_info() - for a child device just created on a client bus, fill |
821 | * in information about the driver that is controlling | |
bffa320f | 822 | * this device into the appropriate slot within the |
3fd1b3b6 DB |
823 | * vbus channel of the bus instance |
824 | * @visordev: struct visor_device for the desired device | |
3703987c EA |
825 | */ |
826 | static void | |
827 | fix_vbus_dev_info(struct visor_device *visordev) | |
828 | { | |
829 | int i; | |
d32517e3 | 830 | struct visor_device *bdev; |
3703987c | 831 | struct visor_driver *visordrv; |
f7a34ff7 TS |
832 | u32 bus_no = visordev->chipset_bus_no; |
833 | u32 dev_no = visordev->chipset_dev_no; | |
55c71eba | 834 | struct visor_vbus_deviceinfo dev_info; |
3703987c | 835 | const char *chan_type_name = NULL; |
55c71eba | 836 | struct visor_vbus_headerinfo *hdr_info; |
3703987c EA |
837 | |
838 | if (!visordev->device.driver) | |
216c3e2c | 839 | return; |
3703987c | 840 | |
d32517e3 DZ |
841 | bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL); |
842 | if (!bdev) | |
843 | return; | |
55c71eba | 844 | hdr_info = (struct visor_vbus_headerinfo *)bdev->vbus_hdr_info; |
5990b39e TS |
845 | if (!hdr_info) |
846 | return; | |
d32517e3 | 847 | visordrv = to_visor_driver(visordev->device.driver); |
3703987c | 848 | |
3fd1b3b6 DB |
849 | /* |
850 | * Within the list of device types (by GUID) that the driver | |
3703987c EA |
851 | * says it supports, find out which one of those types matches |
852 | * the type of this device, so that we can include the device | |
853 | * type name | |
854 | */ | |
855 | for (i = 0; visordrv->channel_types[i].name; i++) { | |
d5b3f1dc EA |
856 | if (memcmp(&visordrv->channel_types[i].guid, |
857 | &visordev->channel_type_guid, | |
858 | sizeof(visordrv->channel_types[i].guid)) == 0) { | |
3703987c EA |
859 | chan_type_name = visordrv->channel_types[i].name; |
860 | break; | |
861 | } | |
862 | } | |
863 | ||
e82ed633 | 864 | bus_device_info_init(&dev_info, chan_type_name, visordrv->name); |
d32517e3 | 865 | write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no); |
d32517e3 DZ |
866 | write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo); |
867 | write_vbus_bus_info(bdev->visorchannel, hdr_info, | |
868 | &clientbus_driverinfo); | |
3703987c EA |
869 | } |
870 | ||
b84be59a | 871 | /* |
7a0ee694 DK |
872 | * visordriver_probe_device() - handle new visor device coming online |
873 | * @xdev: struct device for the visor device being probed | |
874 | * | |
875 | * This is called automatically upon adding a visor_device (device_add), or | |
876 | * adding a visor_driver (visorbus_register_visor_driver), but only after | |
877 | * visorbus_match() has returned 1 to indicate a successful match between | |
878 | * driver and device. | |
879 | * | |
880 | * If successful, a reference to the device will be held onto via get_device(). | |
881 | * | |
882 | * Return: 0 if successful, meaning the function driver's probe() function | |
883 | * was successful with this device, otherwise a negative errno | |
884 | * value indicating failure reason | |
885 | */ | |
886 | static int | |
887 | visordriver_probe_device(struct device *xdev) | |
888 | { | |
889 | int res; | |
890 | struct visor_driver *drv; | |
891 | struct visor_device *dev; | |
892 | ||
7a0ee694 | 893 | dev = to_visor_device(xdev); |
d4e8a22e | 894 | drv = to_visor_driver(xdev->driver); |
7a0ee694 DK |
895 | if (!drv->probe) |
896 | return -ENODEV; | |
897 | ||
898 | mutex_lock(&dev->visordriver_callback_lock); | |
899 | dev->being_removed = false; | |
900 | ||
901 | res = drv->probe(dev); | |
902 | if (res >= 0) { | |
903 | /* success: reference kept via unmatched get_device() */ | |
904 | get_device(&dev->device); | |
905 | fix_vbus_dev_info(dev); | |
906 | } | |
907 | ||
908 | mutex_unlock(&dev->visordriver_callback_lock); | |
909 | return res; | |
910 | } | |
911 | ||
060b293b | 912 | /* |
7a0ee694 DK |
913 | * visorbus_register_visor_driver() - registers the provided visor driver |
914 | * for handling one or more visor device | |
915 | * types (channel_types) | |
916 | * @drv: the driver to register | |
917 | * | |
918 | * A visor function driver calls this function to register | |
919 | * the driver. The caller MUST fill in the following fields within the | |
920 | * #drv structure: | |
921 | * name, version, owner, channel_types, probe, remove | |
922 | * | |
923 | * Here's how the whole Linux bus / driver / device model works. | |
924 | * | |
925 | * At system start-up, the visorbus kernel module is loaded, which registers | |
926 | * visorbus_type as a bus type, using bus_register(). | |
927 | * | |
928 | * All kernel modules that support particular device types on a | |
929 | * visorbus bus are loaded. Each of these kernel modules calls | |
930 | * visorbus_register_visor_driver() in their init functions, passing a | |
931 | * visor_driver struct. visorbus_register_visor_driver() in turn calls | |
932 | * register_driver(&visor_driver.driver). This .driver member is | |
933 | * initialized with generic methods (like probe), whose sole responsibility | |
934 | * is to act as a broker for the real methods, which are within the | |
935 | * visor_driver struct. (This is the way the subclass behavior is | |
936 | * implemented, since visor_driver is essentially a subclass of the | |
937 | * generic driver.) Whenever a driver_register() happens, core bus code in | |
938 | * the kernel does (see device_attach() in drivers/base/dd.c): | |
939 | * | |
940 | * for each dev associated with the bus (the bus that driver is on) that | |
941 | * does not yet have a driver | |
942 | * if bus.match(dev,newdriver) == yes_matched ** .match specified | |
943 | * ** during bus_register(). | |
944 | * newdriver.probe(dev) ** for visor drivers, this will call | |
945 | * ** the generic driver.probe implemented in visorbus.c, | |
946 | * ** which in turn calls the probe specified within the | |
947 | * ** struct visor_driver (which was specified by the | |
948 | * ** actual device driver as part of | |
949 | * ** visorbus_register_visor_driver()). | |
950 | * | |
951 | * The above dance also happens when a new device appears. | |
952 | * So the question is, how are devices created within the system? | |
953 | * Basically, just call device_add(dev). See pci_bus_add_devices(). | |
954 | * pci_scan_device() shows an example of how to build a device struct. It | |
955 | * returns the newly-created struct to pci_scan_single_device(), who adds it | |
956 | * to the list of devices at PCIBUS.devices. That list of devices is what | |
957 | * is traversed by pci_bus_add_devices(). | |
958 | * | |
959 | * Return: integer indicating success (zero) or failure (non-zero) | |
960 | */ | |
961 | int visorbus_register_visor_driver(struct visor_driver *drv) | |
962 | { | |
060b293b | 963 | /* can't register on a nonexistent bus */ |
b0512faf | 964 | if (!initialized) |
060b293b | 965 | return -ENODEV; |
b0512faf | 966 | |
7a0ee694 DK |
967 | drv->driver.name = drv->name; |
968 | drv->driver.bus = &visorbus_type; | |
969 | drv->driver.probe = visordriver_probe_device; | |
970 | drv->driver.remove = visordriver_remove_device; | |
971 | drv->driver.owner = drv->owner; | |
972 | ||
973 | /* | |
974 | * driver_register does this: | |
975 | * bus_add_driver(drv) | |
976 | * ->if (drv.bus) ** (bus_type) ** | |
977 | * driver_attach(drv) | |
978 | * for each dev with bus type of drv.bus | |
979 | * if (!dev.drv) ** no driver assigned yet ** | |
980 | * if (bus.match(dev,drv)) [visorbus_match] | |
981 | * dev.drv = drv | |
982 | * if (!drv.probe(dev)) [visordriver_probe_device] | |
983 | * dev.drv = NULL | |
984 | */ | |
985 | ||
293deb2c | 986 | return driver_register(&drv->driver); |
7a0ee694 DK |
987 | } |
988 | EXPORT_SYMBOL_GPL(visorbus_register_visor_driver); | |
989 | ||
b84be59a | 990 | /* |
9e78fd35 | 991 | * visorbus_create_instance() - create a device instance for the visorbus itself |
3fd1b3b6 DB |
992 | * @dev: struct visor_device indicating the bus instance |
993 | * | |
994 | * Return: 0 for success, otherwise negative errno value indicating reason for | |
995 | * failure | |
3703987c | 996 | */ |
d32517e3 | 997 | static int |
9e78fd35 | 998 | visorbus_create_instance(struct visor_device *dev) |
3703987c | 999 | { |
d32517e3 | 1000 | int id = dev->chipset_bus_no; |
8217becc | 1001 | int err; |
55c71eba | 1002 | struct visor_vbus_headerinfo *hdr_info; |
3703987c | 1003 | |
7726f813 | 1004 | hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL); |
c2c667d6 BR |
1005 | if (!hdr_info) |
1006 | return -ENOMEM; | |
7726f813 | 1007 | |
343506bf DZ |
1008 | dev_set_name(&dev->device, "visorbus%d", id); |
1009 | dev->device.bus = &visorbus_type; | |
1010 | dev->device.groups = visorbus_groups; | |
1011 | dev->device.release = visorbus_release_busdevice; | |
d32517e3 | 1012 | |
8217becc TS |
1013 | dev->debugfs_dir = debugfs_create_dir(dev_name(&dev->device), |
1014 | visorbus_debugfs_dir); | |
8217becc | 1015 | dev->debugfs_client_bus_info = |
b3d76e80 | 1016 | debugfs_create_file("client_bus_info", 0440, |
8217becc TS |
1017 | dev->debugfs_dir, dev, |
1018 | &client_bus_info_debugfs_fops); | |
362f87f3 DK |
1019 | |
1020 | dev_set_drvdata(&dev->device, dev); | |
1021 | err = get_vbus_header_info(dev->visorchannel, hdr_info); | |
1022 | if (err < 0) | |
8217becc | 1023 | goto err_debugfs_dir; |
8217becc | 1024 | |
362f87f3 | 1025 | err = device_register(&dev->device); |
450333f1 | 1026 | if (err < 0) |
362f87f3 | 1027 | goto err_debugfs_dir; |
d32517e3 | 1028 | |
343506bf | 1029 | list_add_tail(&dev->list_all, &list_all_bus_instances); |
8217becc | 1030 | |
362f87f3 DK |
1031 | dev->vbus_hdr_info = (void *)hdr_info; |
1032 | write_vbus_chp_info(dev->visorchannel, hdr_info, | |
1033 | &chipset_driverinfo); | |
1034 | write_vbus_bus_info(dev->visorchannel, hdr_info, | |
1035 | &clientbus_driverinfo); | |
1036 | ||
1037 | return 0; | |
8217becc TS |
1038 | |
1039 | err_debugfs_dir: | |
1040 | debugfs_remove_recursive(dev->debugfs_dir); | |
8217becc | 1041 | kfree(hdr_info); |
23d01c42 | 1042 | dev_err(&dev->device, "%s failed: %d\n", __func__, err); |
8217becc | 1043 | return err; |
3703987c EA |
1044 | } |
1045 | ||
b84be59a | 1046 | /* |
9e78fd35 | 1047 | * visorbus_remove_instance() - remove a device instance for the visorbus itself |
3fd1b3b6 | 1048 | * @dev: struct visor_device indentifying the bus to remove |
3703987c EA |
1049 | */ |
1050 | static void | |
9e78fd35 | 1051 | visorbus_remove_instance(struct visor_device *dev) |
3703987c | 1052 | { |
3fd1b3b6 DB |
1053 | /* |
1054 | * Note that this will result in the release method for | |
343506bf | 1055 | * dev->dev being called, which will call |
3703987c EA |
1056 | * visorbus_release_busdevice(). This has something to do with |
1057 | * the put_device() done in device_unregister(), but I have never | |
1058 | * successfully been able to trace thru the code to see where/how | |
1059 | * release() gets called. But I know it does. | |
1060 | */ | |
343506bf DZ |
1061 | if (dev->visorchannel) { |
1062 | visorchannel_destroy(dev->visorchannel); | |
1063 | dev->visorchannel = NULL; | |
3703987c | 1064 | } |
343506bf DZ |
1065 | kfree(dev->vbus_hdr_info); |
1066 | list_del(&dev->list_all); | |
1067 | device_unregister(&dev->device); | |
3703987c EA |
1068 | } |
1069 | ||
b84be59a | 1070 | /* |
9e78fd35 | 1071 | * remove_all_visor_devices() - remove all child visorbus device instances |
3703987c EA |
1072 | */ |
1073 | static void | |
1074 | remove_all_visor_devices(void) | |
1075 | { | |
1076 | struct list_head *listentry, *listtmp; | |
1077 | ||
1078 | list_for_each_safe(listentry, listtmp, &list_all_device_instances) { | |
1079 | struct visor_device *dev = list_entry(listentry, | |
1080 | struct visor_device, | |
1081 | list_all); | |
1082 | remove_visor_device(dev); | |
1083 | } | |
1084 | } | |
1085 | ||
621f5e18 | 1086 | int |
4f96c730 | 1087 | visorchipset_bus_create(struct visor_device *dev) |
3703987c | 1088 | { |
621f5e18 | 1089 | int err; |
3703987c | 1090 | |
9e78fd35 | 1091 | err = visorbus_create_instance(dev); |
646c7eb5 | 1092 | if (err < 0) |
621f5e18 | 1093 | return err; |
d32517e3 | 1094 | |
63847f17 | 1095 | visorbus_create_response(dev, err); |
621f5e18 DK |
1096 | |
1097 | return 0; | |
3703987c EA |
1098 | } |
1099 | ||
87241ab8 | 1100 | void |
4f96c730 | 1101 | visorchipset_bus_destroy(struct visor_device *dev) |
3703987c | 1102 | { |
9e78fd35 | 1103 | visorbus_remove_instance(dev); |
63847f17 | 1104 | visorbus_destroy_response(dev, 0); |
3703987c EA |
1105 | } |
1106 | ||
3f49a21d | 1107 | int |
c0b44136 | 1108 | visorchipset_device_create(struct visor_device *dev_info) |
3703987c | 1109 | { |
3f49a21d | 1110 | int err; |
3703987c | 1111 | |
3f49a21d | 1112 | err = create_visor_device(dev_info); |
646c7eb5 | 1113 | if (err < 0) |
3f49a21d | 1114 | return err; |
3f49a21d | 1115 | |
f8b5a21f | 1116 | visorbus_device_create_response(dev_info, err); |
3f49a21d DK |
1117 | |
1118 | return 0; | |
3703987c EA |
1119 | } |
1120 | ||
87241ab8 | 1121 | void |
c0b44136 | 1122 | visorchipset_device_destroy(struct visor_device *dev_info) |
3703987c | 1123 | { |
a298bc0b | 1124 | remove_visor_device(dev_info); |
f8b5a21f | 1125 | visorbus_device_destroy_response(dev_info, 0); |
3703987c EA |
1126 | } |
1127 | ||
b84be59a | 1128 | /* |
3fd1b3b6 DB |
1129 | * pause_state_change_complete() - the callback function to be called by a |
1130 | * visorbus function driver when a | |
1131 | * pending "pause device" operation has | |
1132 | * completed | |
1133 | * @dev: struct visor_device identifying the paused device | |
1134 | * @status: 0 iff the pause state change completed successfully, otherwise | |
1135 | * a negative errno value indicating the reason for failure | |
3703987c EA |
1136 | */ |
1137 | static void | |
a298bc0b | 1138 | pause_state_change_complete(struct visor_device *dev, int status) |
3703987c EA |
1139 | { |
1140 | if (!dev->pausing) | |
216c3e2c | 1141 | return; |
3703987c | 1142 | |
779d0752 | 1143 | dev->pausing = false; |
f8b5a21f | 1144 | visorbus_device_pause_response(dev, status); |
3703987c EA |
1145 | } |
1146 | ||
b84be59a | 1147 | /* |
3fd1b3b6 DB |
1148 | * resume_state_change_complete() - the callback function to be called by a |
1149 | * visorbus function driver when a | |
1150 | * pending "resume device" operation has | |
1151 | * completed | |
1152 | * @dev: struct visor_device identifying the resumed device | |
1153 | * @status: 0 iff the resume state change completed successfully, otherwise | |
1154 | * a negative errno value indicating the reason for failure | |
3703987c EA |
1155 | */ |
1156 | static void | |
a298bc0b | 1157 | resume_state_change_complete(struct visor_device *dev, int status) |
3703987c EA |
1158 | { |
1159 | if (!dev->resuming) | |
216c3e2c | 1160 | return; |
3703987c | 1161 | |
779d0752 | 1162 | dev->resuming = false; |
3703987c | 1163 | |
3fd1b3b6 DB |
1164 | /* |
1165 | * Notify the chipset driver that the resume is complete, | |
3703987c | 1166 | * which will presumably want to send some sort of response to |
48117895 GL |
1167 | * the initiator. |
1168 | */ | |
f8b5a21f | 1169 | visorbus_device_resume_response(dev, status); |
3703987c EA |
1170 | } |
1171 | ||
b84be59a | 1172 | /* |
451072e3 SW |
1173 | * visorchipset_initiate_device_pause_resume() - start a pause or resume |
1174 | * operation for a visor device | |
3fd1b3b6 DB |
1175 | * @dev: struct visor_device identifying the device being paused or resumed |
1176 | * @is_pause: true to indicate pause operation, false to indicate resume | |
1177 | * | |
1178 | * Tell the subordinate function driver for a specific device to pause | |
1179 | * or resume that device. Success/failure result is returned asynchronously | |
1180 | * via a callback function; see pause_state_change_complete() and | |
1181 | * resume_state_change_complete(). | |
3703987c | 1182 | */ |
15d9ce9d | 1183 | static int |
451072e3 SW |
1184 | visorchipset_initiate_device_pause_resume(struct visor_device *dev, |
1185 | bool is_pause) | |
3703987c | 1186 | { |
15d9ce9d | 1187 | int err; |
3703987c | 1188 | struct visor_driver *drv = NULL; |
3703987c | 1189 | |
3703987c | 1190 | drv = to_visor_driver(dev->device.driver); |
15d9ce9d DK |
1191 | if (!drv) |
1192 | return -ENODEV; | |
3703987c | 1193 | |
15d9ce9d DK |
1194 | if (dev->pausing || dev->resuming) |
1195 | return -EBUSY; | |
3703987c | 1196 | |
3703987c | 1197 | if (is_pause) { |
15d9ce9d DK |
1198 | if (!drv->pause) |
1199 | return -EINVAL; | |
3703987c | 1200 | |
779d0752 | 1201 | dev->pausing = true; |
15d9ce9d | 1202 | err = drv->pause(dev, pause_state_change_complete); |
3703987c | 1203 | } else { |
060b293b SW |
1204 | /* |
1205 | * The vbus_dev_info structure in the channel was been cleared, | |
1206 | * make sure it is valid. | |
48117895 | 1207 | */ |
3703987c | 1208 | fix_vbus_dev_info(dev); |
15d9ce9d DK |
1209 | if (!drv->resume) |
1210 | return -EINVAL; | |
3703987c | 1211 | |
779d0752 | 1212 | dev->resuming = true; |
15d9ce9d | 1213 | err = drv->resume(dev, resume_state_change_complete); |
3703987c | 1214 | } |
15d9ce9d DK |
1215 | |
1216 | return err; | |
3703987c EA |
1217 | } |
1218 | ||
060b293b | 1219 | /* |
c0b44136 | 1220 | * visorchipset_device_pause() - start a pause operation for a visor device |
3fd1b3b6 DB |
1221 | * @dev_info: struct visor_device identifying the device being paused |
1222 | * | |
1223 | * Tell the subordinate function driver for a specific device to pause | |
1224 | * that device. Success/failure result is returned asynchronously | |
1225 | * via a callback function; see pause_state_change_complete(). | |
1226 | */ | |
b4a8e6ae | 1227 | int |
c0b44136 | 1228 | visorchipset_device_pause(struct visor_device *dev_info) |
3703987c | 1229 | { |
15d9ce9d DK |
1230 | int err; |
1231 | ||
451072e3 | 1232 | err = visorchipset_initiate_device_pause_resume(dev_info, true); |
15d9ce9d DK |
1233 | if (err < 0) { |
1234 | dev_info->pausing = false; | |
b4a8e6ae | 1235 | return err; |
15d9ce9d | 1236 | } |
b4a8e6ae DK |
1237 | |
1238 | return 0; | |
3703987c EA |
1239 | } |
1240 | ||
060b293b | 1241 | /* |
c0b44136 | 1242 | * visorchipset_device_resume() - start a resume operation for a visor device |
3fd1b3b6 DB |
1243 | * @dev_info: struct visor_device identifying the device being resumed |
1244 | * | |
1245 | * Tell the subordinate function driver for a specific device to resume | |
1246 | * that device. Success/failure result is returned asynchronously | |
1247 | * via a callback function; see resume_state_change_complete(). | |
1248 | */ | |
b4a8e6ae | 1249 | int |
c0b44136 | 1250 | visorchipset_device_resume(struct visor_device *dev_info) |
3703987c | 1251 | { |
15d9ce9d DK |
1252 | int err; |
1253 | ||
451072e3 | 1254 | err = visorchipset_initiate_device_pause_resume(dev_info, false); |
15d9ce9d | 1255 | if (err < 0) { |
15d9ce9d | 1256 | dev_info->resuming = false; |
b4a8e6ae | 1257 | return err; |
15d9ce9d | 1258 | } |
b4a8e6ae DK |
1259 | |
1260 | return 0; | |
3703987c EA |
1261 | } |
1262 | ||
55c67dca | 1263 | int |
3703987c EA |
1264 | visorbus_init(void) |
1265 | { | |
78af1aef | 1266 | int err; |
3703987c | 1267 | |
8217becc TS |
1268 | visorbus_debugfs_dir = debugfs_create_dir("visorbus", NULL); |
1269 | if (!visorbus_debugfs_dir) | |
1270 | return -ENOMEM; | |
1271 | ||
e82ed633 | 1272 | bus_device_info_init(&clientbus_driverinfo, "clientbus", "visorbus"); |
3703987c | 1273 | |
5b6f9b95 | 1274 | err = bus_register(&visorbus_type); |
804aab3b DK |
1275 | if (err < 0) |
1276 | return err; | |
1277 | ||
b0512faf | 1278 | initialized = true; |
e82ed633 | 1279 | bus_device_info_init(&chipset_driverinfo, "chipset", "visorchipset"); |
3703987c | 1280 | |
78af1aef | 1281 | return 0; |
3703987c EA |
1282 | } |
1283 | ||
c79b28f7 | 1284 | void |
3703987c EA |
1285 | visorbus_exit(void) |
1286 | { | |
1287 | struct list_head *listentry, *listtmp; | |
1288 | ||
3703987c EA |
1289 | remove_all_visor_devices(); |
1290 | ||
3703987c | 1291 | list_for_each_safe(listentry, listtmp, &list_all_bus_instances) { |
343506bf | 1292 | struct visor_device *dev = list_entry(listentry, |
216c3e2c CJ |
1293 | struct visor_device, |
1294 | list_all); | |
9e78fd35 | 1295 | visorbus_remove_instance(dev); |
3703987c | 1296 | } |
5b6f9b95 DK |
1297 | |
1298 | bus_unregister(&visorbus_type); | |
b0512faf | 1299 | initialized = false; |
8217becc | 1300 | debugfs_remove_recursive(visorbus_debugfs_dir); |
3703987c | 1301 | } |