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_inquiry_cache(struct device *dev, struct device_attribute *attr, char *buf)
54 struct hci_dev *hdev = dev_get_drvdata(dev);
55 struct inquiry_cache *cache = &hdev->inq_cache;
56 struct inquiry_entry *e;
59 hci_dev_lock_bh(hdev);
61 for (e = cache->list; e; e = e->next) {
62 struct inquiry_data *data = &e->data;
64 baswap(&bdaddr, &data->bdaddr);
65 n += sprintf(buf + n, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %u\n",
67 data->pscan_rep_mode, data->pscan_period_mode, data->pscan_mode,
68 data->dev_class[2], data->dev_class[1], data->dev_class[0],
69 __le16_to_cpu(data->clock_offset), data->rssi, e->timestamp);
72 hci_dev_unlock_bh(hdev);
76 static ssize_t show_idle_timeout(struct device *dev, struct device_attribute *attr, char *buf)
78 struct hci_dev *hdev = dev_get_drvdata(dev);
79 return sprintf(buf, "%d\n", hdev->idle_timeout);
82 static ssize_t store_idle_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
84 struct hci_dev *hdev = dev_get_drvdata(dev);
88 val = simple_strtoul(buf, &ptr, 10);
92 if (val != 0 && (val < 500 || val > 3600000))
95 hdev->idle_timeout = val;
100 static ssize_t show_sniff_max_interval(struct device *dev, struct device_attribute *attr, char *buf)
102 struct hci_dev *hdev = dev_get_drvdata(dev);
103 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
106 static ssize_t store_sniff_max_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
108 struct hci_dev *hdev = dev_get_drvdata(dev);
112 val = simple_strtoul(buf, &ptr, 10);
116 if (val < 0x0002 || val > 0xFFFE || val % 2)
119 if (val < hdev->sniff_min_interval)
122 hdev->sniff_max_interval = val;
127 static ssize_t show_sniff_min_interval(struct device *dev, struct device_attribute *attr, char *buf)
129 struct hci_dev *hdev = dev_get_drvdata(dev);
130 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
133 static ssize_t store_sniff_min_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
135 struct hci_dev *hdev = dev_get_drvdata(dev);
139 val = simple_strtoul(buf, &ptr, 10);
143 if (val < 0x0002 || val > 0xFFFE || val % 2)
146 if (val > hdev->sniff_max_interval)
149 hdev->sniff_min_interval = val;
154 static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
155 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
156 static DEVICE_ATTR(inquiry_cache, S_IRUGO, show_inquiry_cache, NULL);
158 static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
159 show_idle_timeout, store_idle_timeout);
160 static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
161 show_sniff_max_interval, store_sniff_max_interval);
162 static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
163 show_sniff_min_interval, store_sniff_min_interval);
165 static struct device_attribute *bt_attrs[] = {
168 &dev_attr_inquiry_cache,
169 &dev_attr_idle_timeout,
170 &dev_attr_sniff_max_interval,
171 &dev_attr_sniff_min_interval,
175 static ssize_t show_conn_type(struct device *dev, struct device_attribute *attr, char *buf)
177 struct hci_conn *conn = dev_get_drvdata(dev);
178 return sprintf(buf, "%s\n", conn->type == ACL_LINK ? "ACL" : "SCO");
181 static ssize_t show_conn_address(struct device *dev, struct device_attribute *attr, char *buf)
183 struct hci_conn *conn = dev_get_drvdata(dev);
185 baswap(&bdaddr, &conn->dst);
186 return sprintf(buf, "%s\n", batostr(&bdaddr));
189 #define CONN_ATTR(_name,_mode,_show,_store) \
190 struct device_attribute conn_attr_##_name = __ATTR(_name,_mode,_show,_store)
192 static CONN_ATTR(type, S_IRUGO, show_conn_type, NULL);
193 static CONN_ATTR(address, S_IRUGO, show_conn_address, NULL);
195 static struct device_attribute *conn_attrs[] = {
201 struct class *bt_class = NULL;
202 EXPORT_SYMBOL_GPL(bt_class);
204 static struct bus_type bt_bus = {
208 static struct platform_device *bt_platform;
210 static void bt_release(struct device *dev)
212 void *data = dev_get_drvdata(dev);
216 static void add_conn(void *data)
218 struct hci_conn *conn = data;
221 device_register(&conn->dev);
223 for (i = 0; conn_attrs[i]; i++)
224 device_create_file(&conn->dev, conn_attrs[i]);
227 void hci_conn_add_sysfs(struct hci_conn *conn)
229 struct hci_dev *hdev = conn->hdev;
230 bdaddr_t *ba = &conn->dst;
232 BT_DBG("conn %p", conn);
234 conn->dev.parent = &hdev->dev;
235 conn->dev.release = bt_release;
237 snprintf(conn->dev.bus_id, BUS_ID_SIZE,
238 "%s%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
239 conn->type == ACL_LINK ? "acl" : "sco",
240 ba->b[5], ba->b[4], ba->b[3],
241 ba->b[2], ba->b[1], ba->b[0]);
243 dev_set_drvdata(&conn->dev, conn);
245 INIT_WORK(&conn->work, add_conn, (void *) conn);
247 schedule_work(&conn->work);
250 static void del_conn(void *data)
252 struct hci_conn *conn = data;
253 device_del(&conn->dev);
256 void hci_conn_del_sysfs(struct hci_conn *conn)
258 BT_DBG("conn %p", conn);
260 INIT_WORK(&conn->work, del_conn, (void *) conn);
262 schedule_work(&conn->work);
265 int hci_register_sysfs(struct hci_dev *hdev)
267 struct device *dev = &hdev->dev;
271 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
273 dev->class = bt_class;
276 dev->parent = hdev->parent;
278 dev->parent = &bt_platform->dev;
280 strlcpy(dev->bus_id, hdev->name, BUS_ID_SIZE);
282 dev->release = bt_release;
284 dev_set_drvdata(dev, hdev);
286 err = device_register(dev);
290 for (i = 0; bt_attrs[i]; i++)
291 device_create_file(dev, bt_attrs[i]);
296 void hci_unregister_sysfs(struct hci_dev *hdev)
298 BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
300 device_del(&hdev->dev);
303 int __init bt_sysfs_init(void)
307 bt_platform = platform_device_register_simple("bluetooth", -1, NULL, 0);
308 if (IS_ERR(bt_platform))
309 return PTR_ERR(bt_platform);
311 err = bus_register(&bt_bus);
313 platform_device_unregister(bt_platform);
317 bt_class = class_create(THIS_MODULE, "bluetooth");
318 if (IS_ERR(bt_class)) {
319 bus_unregister(&bt_bus);
320 platform_device_unregister(bt_platform);
321 return PTR_ERR(bt_class);
327 void __exit bt_sysfs_cleanup(void)
329 class_destroy(bt_class);
331 bus_unregister(&bt_bus);
333 platform_device_unregister(bt_platform);