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_name(struct device *dev, struct device_attribute *attr, char *buf)
46 struct hci_dev *hdev = dev_get_drvdata(dev);
50 for (i = 0; i < 248; i++)
51 name[i] = hdev->dev_name[i];
54 return sprintf(buf, "%s\n", name);
57 static ssize_t show_class(struct device *dev, struct device_attribute *attr, char *buf)
59 struct hci_dev *hdev = dev_get_drvdata(dev);
60 return sprintf(buf, "0x%.2x%.2x%.2x\n",
61 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
64 static ssize_t show_address(struct device *dev, struct device_attribute *attr, char *buf)
66 struct hci_dev *hdev = dev_get_drvdata(dev);
68 baswap(&bdaddr, &hdev->bdaddr);
69 return sprintf(buf, "%s\n", batostr(&bdaddr));
72 static ssize_t show_features(struct device *dev, struct device_attribute *attr, char *buf)
74 struct hci_dev *hdev = dev_get_drvdata(dev);
76 return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
77 hdev->features[0], hdev->features[1],
78 hdev->features[2], hdev->features[3],
79 hdev->features[4], hdev->features[5],
80 hdev->features[6], hdev->features[7]);
83 static ssize_t show_manufacturer(struct device *dev, struct device_attribute *attr, char *buf)
85 struct hci_dev *hdev = dev_get_drvdata(dev);
86 return sprintf(buf, "%d\n", hdev->manufacturer);
89 static ssize_t show_hci_version(struct device *dev, struct device_attribute *attr, char *buf)
91 struct hci_dev *hdev = dev_get_drvdata(dev);
92 return sprintf(buf, "%d\n", hdev->hci_ver);
95 static ssize_t show_hci_revision(struct device *dev, struct device_attribute *attr, char *buf)
97 struct hci_dev *hdev = dev_get_drvdata(dev);
98 return sprintf(buf, "%d\n", hdev->hci_rev);
101 static ssize_t show_inquiry_cache(struct device *dev, struct device_attribute *attr, char *buf)
103 struct hci_dev *hdev = dev_get_drvdata(dev);
104 struct inquiry_cache *cache = &hdev->inq_cache;
105 struct inquiry_entry *e;
108 hci_dev_lock_bh(hdev);
110 for (e = cache->list; e; e = e->next) {
111 struct inquiry_data *data = &e->data;
113 baswap(&bdaddr, &data->bdaddr);
114 n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %u\n",
116 data->pscan_rep_mode, data->pscan_period_mode, data->pscan_mode,
117 data->dev_class[2], data->dev_class[1], data->dev_class[0],
118 __le16_to_cpu(data->clock_offset), data->rssi, e->timestamp);
121 hci_dev_unlock_bh(hdev);
125 static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
127 struct hci_dev *hdev = dev_get_drvdata(dev);
128 return sprintf(buf, "%d\n", hdev->idle_timeout);
131 static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
133 struct hci_dev *hdev = dev_get_drvdata(dev);
137 val = simple_strtoul(buf, &ptr, 10);
141 if (val != 0 && (val < 500 || val > 3600000))
144 hdev->idle_timeout = val;
149 static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
151 struct hci_dev *hdev = dev_get_drvdata(dev);
152 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
155 static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
157 struct hci_dev *hdev = dev_get_drvdata(dev);
161 val = simple_strtoul(buf, &ptr, 10);
165 if (val < 0x0002 || val > 0xFFFE || val % 2)
168 if (val < hdev->sniff_min_interval)
171 hdev->sniff_max_interval = val;
176 static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
178 struct hci_dev *hdev = dev_get_drvdata(dev);
179 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
182 static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
184 struct hci_dev *hdev = dev_get_drvdata(dev);
188 val = simple_strtoul(buf, &ptr, 10);
192 if (val < 0x0002 || val > 0xFFFE || val % 2)
195 if (val > hdev->sniff_max_interval)
198 hdev->sniff_min_interval = val;
203 static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
204 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
205 static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
206 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
207 static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
208 static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
209 static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
210 static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
211 static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL);
213 static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
214 show_idle_timeout, store_idle_timeout);
215 static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
216 show_sniff_max_interval, store_sniff_max_interval);
217 static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
218 show_sniff_min_interval, store_sniff_min_interval);
220 static struct device_attribute *bt_attrs[] = {
226 &dev_attr_manufacturer,
227 &dev_attr_hci_version,
228 &dev_attr_hci_revision,
229 &dev_attr_inquiry_cache,
230 &dev_attr_idle_timeout,
231 &dev_attr_sniff_max_interval,
232 &dev_attr_sniff_min_interval,
236 static ssize_t show_conn_type(struct device *dev, struct device_attribute *attr, char *buf)
238 struct hci_conn *conn = dev_get_drvdata(dev);
239 return sprintf(buf, "%s\n", conn->type == ACL_LINK ? "ACL" : "SCO");
242 static ssize_t show_conn_address(struct device *dev, struct device_attribute *attr, char *buf)
244 struct hci_conn *conn = dev_get_drvdata(dev);
246 baswap(&bdaddr, &conn->dst);
247 return sprintf(buf, "%s\n", batostr(&bdaddr));
250 #define CONN_ATTR(_name,_mode,_show,_store) \
251 struct device_attribute conn_attr_##_name = __ATTR(_name,_mode,_show,_store)
253 static CONN_ATTR(type, S_IRUGO, show_conn_type, NULL);
254 static CONN_ATTR(address, S_IRUGO, show_conn_address, NULL);
256 static struct device_attribute *conn_attrs[] = {
262 struct class *bt_class = NULL;
263 EXPORT_SYMBOL_GPL(bt_class);
265 static struct bus_type bt_bus = {
269 static struct platform_device *bt_platform;
271 static void bt_release(struct device *dev)
273 void *data = dev_get_drvdata(dev);
277 static void add_conn(struct work_struct *work)
279 struct hci_conn *conn = container_of(work, struct hci_conn, work);
282 if (device_add(&conn->dev) < 0) {
283 BT_ERR("Failed to register connection device");
287 for (i = 0; conn_attrs[i]; i++)
288 if (device_create_file(&conn->dev, conn_attrs[i]) < 0)
289 BT_ERR("Failed to create connection attribute");
292 void hci_conn_add_sysfs(struct hci_conn *conn)
294 struct hci_dev *hdev = conn->hdev;
295 bdaddr_t *ba = &conn->dst;
297 BT_DBG("conn %p", conn);
299 conn->dev.bus = &bt_bus;
300 conn->dev.parent = &hdev->dev;
302 conn->dev.release = bt_release;
304 snprintf(conn->dev.bus_id, BUS_ID_SIZE,
305 "%s%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
306 conn->type == ACL_LINK ? "acl" : "sco",
307 ba->b[5], ba->b[4], ba->b[3],
308 ba->b[2], ba->b[1], ba->b[0]);
310 dev_set_drvdata(&conn->dev, conn);
312 device_initialize(&conn->dev);
314 INIT_WORK(&conn->work, add_conn);
316 schedule_work(&conn->work);
319 static void del_conn(struct work_struct *work)
321 struct hci_conn *conn = container_of(work, struct hci_conn, work);
322 device_del(&conn->dev);
323 put_device(&conn->dev);
326 void hci_conn_del_sysfs(struct hci_conn *conn)
328 BT_DBG("conn %p", conn);
330 if (!device_is_registered(&conn->dev))
333 INIT_WORK(&conn->work, del_conn);
335 schedule_work(&conn->work);
338 int hci_register_sysfs(struct hci_dev *hdev)
340 struct device *dev = &hdev->dev;
344 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
347 dev->parent = hdev->parent;
349 strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE);
351 dev->release = bt_release;
353 dev_set_drvdata(dev, hdev);
355 err = device_register(dev);
359 for (i = 0; bt_attrs[i]; i++)
360 if (device_create_file(dev, bt_attrs[i]) < 0)
361 BT_ERR("Failed to create device attribute");
363 if (sysfs_create_link(&bt_class->subsys.kobj,
364 &dev->kobj, kobject_name(&dev->kobj)) < 0)
365 BT_ERR("Failed to create class symlink");
370 void hci_unregister_sysfs(struct hci_dev *hdev)
372 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
374 sysfs_remove_link(&bt_class->subsys.kobj,
375 kobject_name(&hdev->dev.kobj));
377 device_del(&hdev->dev);
380 int __init bt_sysfs_init(void)
384 bt_platform = platform_device_register_simple("bluetooth", -1, NULL, 0);
385 if (IS_ERR(bt_platform))
386 return PTR_ERR(bt_platform);
388 err = bus_register(&bt_bus);
390 platform_device_unregister(bt_platform);
394 bt_class = class_create(THIS_MODULE, "bluetooth");
395 if (IS_ERR(bt_class)) {
396 bus_unregister(&bt_bus);
397 platform_device_unregister(bt_platform);
398 return PTR_ERR(bt_class);
404 void bt_sysfs_cleanup(void)
406 class_destroy(bt_class);
408 bus_unregister(&bt_bus);
410 platform_device_unregister(bt_platform);