1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <errno.h>
30
31 #include <bluetooth/bluetooth.h>
32
33 #include "plugin.h"
34 #include "adapter.h"
35 #include "log.h"
36
37 #define DMI_CHASSIS_FILE "/sys/class/dmi/id/chassis_type"
38 #define DMI_CHASSIS_FILE_FALLBACK "/sys/devices/virtual/dmi/id/chassis_type"
39
40 /* Map the chassis type from chassis_type to a sensible type used in hal
41 *
42 * See also 3.3.4.1 of the "System Management BIOS Reference Specification,
43 * Version 2.6.1" (Preliminary Standard) document, available from
44 * http://www.dmtf.org/standards/smbios.
45 *
46 * TODO: figure out WTF the mapping should be; "Lunch Box"? Give me a break :-)
47 *
48 * Copied from hal/hald/linux/osspec.c
49 */
50 static const char *chassis_map[] = {
51 "Other", "unknown", /* 0x01 */
52 "Unknown", "unknown",
53 "Desktop", "desktop",
54 "Low Profile Desktop", "desktop",
55 "Pizza Box", "server",
56 "Mini Tower", "desktop",
57 "Tower", "desktop",
58 "Portable", "laptop",
59 "Laptop", "laptop",
60 "Notebook", "laptop",
61 "Hand Held", "handheld",
62 "Docking Station", "laptop",
63 "All In One", "unknown",
64 "Sub Notebook", "laptop",
65 "Space-saving", "desktop",
66 "Lunch Box", "unknown",
67 "Main Server Chassis", "server",
68 "Expansion Chassis", "unknown",
69 "Sub Chassis", "unknown",
70 "Bus Expansion Chassis", "unknown",
71 "Peripheral Chassis", "unknown",
72 "RAID Chassis", "unknown",
73 "Rack Mount Chassis", "unknown",
74 "Sealed-case PC", "unknown",
75 "Multi-system", "unknown",
76 "CompactPCI", "unknonw",
77 "AdvancedTCA", "unknown",
78 "Blade", "server",
79 "Blade Enclosure" "unknown", /* 0x1D */
80 NULL
81 };
82
formfactor_probe(struct btd_adapter * adapter)83 static int formfactor_probe(struct btd_adapter *adapter)
84 {
85 int chassis_type;
86 uint8_t minor = 0;
87 const char *formfactor;
88 char *contents;
89
90 if (g_file_get_contents(DMI_CHASSIS_FILE,
91 &contents, NULL, NULL) == FALSE) {
92 if (g_file_get_contents(DMI_CHASSIS_FILE_FALLBACK,
93 &contents, NULL, NULL) == FALSE) {
94 error("Could not get the contents of DMI chassis type");
95 return 0;
96 }
97 }
98
99 chassis_type = atoi(contents);
100 g_free (contents);
101
102 if (chassis_type > 0x1D || chassis_type <= 0) {
103 error ("Chassis type is not a known chassis type");
104 return 0;
105 }
106
107 formfactor = chassis_map[chassis_type * 2];
108 if (formfactor != NULL) {
109 if (g_str_equal(formfactor, "laptop") == TRUE)
110 minor |= (1 << 2) | (1 << 3);
111 else if (g_str_equal(formfactor, "desktop") == TRUE)
112 minor |= 1 << 2;
113 else if (g_str_equal(formfactor, "server") == TRUE)
114 minor |= 1 << 3;
115 else if (g_str_equal(formfactor, "handheld") == TRUE)
116 minor += 1 << 4;
117 }
118
119 /* Computer major class */
120 DBG("Setting 0x%06x for major/minor device class", (1 << 8) | minor);
121
122 btd_adapter_set_class(adapter, 0x01, minor);
123
124 return 0;
125 }
126
formfactor_remove(struct btd_adapter * adapter)127 static void formfactor_remove(struct btd_adapter *adapter)
128 {
129 }
130
131 static struct btd_adapter_driver formfactor_driver = {
132 .name = "formfactor",
133 .probe = formfactor_probe,
134 .remove = formfactor_remove,
135 };
136
formfactor_init(void)137 static int formfactor_init(void)
138 {
139 return btd_register_adapter_driver(&formfactor_driver);
140 }
141
formfactor_exit(void)142 static void formfactor_exit(void)
143 {
144 btd_unregister_adapter_driver(&formfactor_driver);
145 }
146
147 BLUETOOTH_PLUGIN_DEFINE(formfactor, VERSION,
148 BLUETOOTH_PLUGIN_PRIORITY_LOW, formfactor_init, formfactor_exit)
149