1 /* Bluetooth HCI driver model support. */
3 #include <linux/kernel.h>
4 #include <linux/init.h>
6 #include <linux/platform_device.h>
8 #include <net/bluetooth/bluetooth.h>
9 #include <net/bluetooth/hci_core.h>
11 #ifndef CONFIG_BT_HCI_CORE_DEBUG
16 static inline char *typetostr(int type)
38 static ssize_t show_type(struct device *dev, struct device_attribute *attr, char *buf)
40 struct hci_dev *hdev = dev_get_drvdata(dev);
41 return sprintf(buf, "%s\n", typetostr(hdev->type));
44 static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
46 struct hci_dev *hdev = dev_get_drvdata(dev);
48 baswap(&bdaddr, &hdev->bdaddr);
49 return sprintf(buf, "%s\n", batostr(&bdaddr));
52 static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
54 struct hci_dev *hdev = dev_get_drvdata(dev);
55 return sprintf(buf, "%d\n", hdev->manufacturer);
58 static ssize_t show_hci_version(struct device *dev, struct device_attribute *attr, char *buf)
60 struct hci_dev *hdev = dev_get_drvdata(dev);
61 return sprintf(buf, "%d\n", hdev->hci_ver);
64 static ssize_t show_hci_revision(struct device *dev, struct device_attribute *attr, char *buf)
66 struct hci_dev *hdev = dev_get_drvdata(dev);
67 return sprintf(buf, "%d\n", hdev->hci_rev);
70 static ssize_t show_inquiry_cache(struct device *dev, struct device_attribute *attr, char *buf)
72 struct hci_dev *hdev = dev_get_drvdata(dev);
73 struct inquiry_cache *cache = &hdev->inq_cache;
74 struct inquiry_entry *e;
77 hci_dev_lock_bh(hdev);
79 for (e = cache->list; e; e = e->next) {
80 struct inquiry_data *data = &e->data;
82 baswap(&bdaddr, &data->bdaddr);
83 n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %u\n",
85 data->pscan_rep_mode, data->pscan_period_mode, data->pscan_mode,
86 data->dev_class[2], data->dev_class[1], data->dev_class[0],
87 __le16_to_cpu(data->clock_offset), data->rssi, e->timestamp);
90 hci_dev_unlock_bh(hdev);
94 static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
96 struct hci_dev *hdev = dev_get_drvdata(dev);
97 return sprintf(buf, "%d\n", hdev->idle_timeout);
100 static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
102 struct hci_dev *hdev = dev_get_drvdata(dev);
106 val = simple_strtoul(buf, &ptr, 10);
110 if (val != 0 && (val < 500 || val > 3600000))
113 hdev->idle_timeout = val;
118 static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
120 struct hci_dev *hdev = dev_get_drvdata(dev);
121 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
124 static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
126 struct hci_dev *hdev = dev_get_drvdata(dev);
130 val = simple_strtoul(buf, &ptr, 10);
134 if (val < 0x0002 || val > 0xFFFE || val % 2)
137 if (val < hdev->sniff_min_interval)
140 hdev->sniff_max_interval = val;
145 static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
147 struct hci_dev *hdev = dev_get_drvdata(dev);
148 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
151 static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
153 struct hci_dev *hdev = dev_get_drvdata(dev);
157 val = simple_strtoul(buf, &ptr, 10);
161 if (val < 0x0002 || val > 0xFFFE || val % 2)
164 if (val > hdev->sniff_max_interval)
167 hdev->sniff_min_interval = val;
172 static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
173 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
174 static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
175 static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
176 static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
177 static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL);
179 static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
180 show_idle_timeout, store_idle_timeout);
181 static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
182 show_sniff_max_interval, store_sniff_max_interval);
183 static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
184 show_sniff_min_interval, store_sniff_min_interval);
186 static struct device_attribute *bt_attrs[] = {
189 &dev_attr_manufacturer,
190 &dev_attr_hci_version,
191 &dev_attr_hci_revision,
192 &dev_attr_inquiry_cache,
193 &dev_attr_idle_timeout,
194 &dev_attr_sniff_max_interval,
195 &dev_attr_sniff_min_interval,
199 static ssize_t show_conn_type(struct device *dev, struct device_attribute *attr, char *buf)
201 struct hci_conn *conn = dev_get_drvdata(dev);
202 return sprintf(buf, "%s\n", conn->type == ACL_LINK ? "ACL" : "SCO");
205 static ssize_t show_conn_address(struct device *dev, struct device_attribute *attr, char *buf)
207 struct hci_conn *conn = dev_get_drvdata(dev);
209 baswap(&bdaddr, &conn->dst);
210 return sprintf(buf, "%s\n", batostr(&bdaddr));
213 #define CONN_ATTR(_name,_mode,_show,_store) \
214 struct device_attribute conn_attr_##_name = __ATTR(_name,_mode,_show,_store)
216 static CONN_ATTR(type, S_IRUGO, show_conn_type, NULL);
217 static CONN_ATTR(address, S_IRUGO, show_conn_address, NULL);
219 static struct device_attribute *conn_attrs[] = {
225 struct class *bt_class = NULL;
226 EXPORT_SYMBOL_GPL(bt_class);
228 static struct bus_type bt_bus = {
232 static struct platform_device *bt_platform;
234 static void bt_release(struct device *dev)
236 void *data = dev_get_drvdata(dev);
240 static void add_conn(void *data)
242 struct hci_conn *conn = data;
245 device_register(&conn->dev);
247 for (i = 0; conn_attrs[i]; i++)
248 device_create_file(&conn->dev, conn_attrs[i]);
251 void hci_conn_add_sysfs(struct hci_conn *conn)
253 struct hci_dev *hdev = conn->hdev;
254 bdaddr_t *ba = &conn->dst;
256 BT_DBG("conn %p", conn);
258 conn->dev.parent = &hdev->dev;
259 conn->dev.release = bt_release;
261 snprintf(conn->dev.bus_id, BUS_ID_SIZE,
262 "%s%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
263 conn->type == ACL_LINK ? "acl" : "sco",
264 ba->b[5], ba->b[4], ba->b[3],
265 ba->b[2], ba->b[1], ba->b[0]);
267 dev_set_drvdata(&conn->dev, conn);
269 INIT_WORK(&conn->work, add_conn, (void *) conn);
271 schedule_work(&conn->work);
274 static void del_conn(void *data)
276 struct hci_conn *conn = data;
277 device_del(&conn->dev);
280 void hci_conn_del_sysfs(struct hci_conn *conn)
282 BT_DBG("conn %p", conn);
284 INIT_WORK(&conn->work, del_conn, (void *) conn);
286 schedule_work(&conn->work);
289 int hci_register_sysfs(struct hci_dev *hdev)
291 struct device *dev = &hdev->dev;
295 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
297 dev->class = bt_class;
300 dev->parent = hdev->parent;
302 dev->parent = &bt_platform->dev;
304 strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE);
306 dev->release = bt_release;
308 dev_set_drvdata(dev, hdev);
310 err = device_register(dev);
314 for (i = 0; bt_attrs[i]; i++)
315 device_create_file(dev, bt_attrs[i]);
320 void hci_unregister_sysfs(struct hci_dev *hdev)
322 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
324 device_del(&hdev->dev);
327 int __init bt_sysfs_init(void)
331 bt_platform = platform_device_register_simple("bluetooth", -1, NULL, 0);
332 if (IS_ERR(bt_platform))
333 return PTR_ERR(bt_platform);
335 err = bus_register(&bt_bus);
337 platform_device_unregister(bt_platform);
341 bt_class = class_create(THIS_MODULE, "bluetooth");
342 if (IS_ERR(bt_class)) {
343 bus_unregister(&bt_bus);
344 platform_device_unregister(bt_platform);
345 return PTR_ERR(bt_class);
351 void bt_sysfs_cleanup(void)
353 class_destroy(bt_class);
355 bus_unregister(&bt_bus);
357 platform_device_unregister(bt_platform);