1 /* Bluetooth HCI driver model support. */
3 #include <linux/kernel.h>
4 #include <linux/init.h>
6 #include <net/bluetooth/bluetooth.h>
7 #include <net/bluetooth/hci_core.h>
9 #ifndef CONFIG_BT_HCI_CORE_DEBUG
14 struct class *bt_class
= NULL
;
15 EXPORT_SYMBOL_GPL(bt_class
);
17 static struct workqueue_struct
*btaddconn
;
18 static struct workqueue_struct
*btdelconn
;
20 static inline char *link_typetostr(int type
)
34 static ssize_t
show_link_type(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
36 struct hci_conn
*conn
= dev_get_drvdata(dev
);
37 return sprintf(buf
, "%s\n", link_typetostr(conn
->type
));
40 static ssize_t
show_link_address(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
42 struct hci_conn
*conn
= dev_get_drvdata(dev
);
44 baswap(&bdaddr
, &conn
->dst
);
45 return sprintf(buf
, "%s\n", batostr(&bdaddr
));
48 static ssize_t
show_link_features(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
50 struct hci_conn
*conn
= dev_get_drvdata(dev
);
52 return sprintf(buf
, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
53 conn
->features
[0], conn
->features
[1],
54 conn
->features
[2], conn
->features
[3],
55 conn
->features
[4], conn
->features
[5],
56 conn
->features
[6], conn
->features
[7]);
59 #define LINK_ATTR(_name,_mode,_show,_store) \
60 struct device_attribute link_attr_##_name = __ATTR(_name,_mode,_show,_store)
62 static LINK_ATTR(type
, S_IRUGO
, show_link_type
, NULL
);
63 static LINK_ATTR(address
, S_IRUGO
, show_link_address
, NULL
);
64 static LINK_ATTR(features
, S_IRUGO
, show_link_features
, NULL
);
66 static struct attribute
*bt_link_attrs
[] = {
68 &link_attr_address
.attr
,
69 &link_attr_features
.attr
,
73 static struct attribute_group bt_link_group
= {
74 .attrs
= bt_link_attrs
,
77 static struct attribute_group
*bt_link_groups
[] = {
82 static void bt_link_release(struct device
*dev
)
84 void *data
= dev_get_drvdata(dev
);
88 static struct device_type bt_link
= {
90 .groups
= bt_link_groups
,
91 .release
= bt_link_release
,
94 static void add_conn(struct work_struct
*work
)
96 struct hci_conn
*conn
= container_of(work
, struct hci_conn
, work
);
98 flush_workqueue(btdelconn
);
100 if (device_add(&conn
->dev
) < 0) {
101 BT_ERR("Failed to register connection device");
106 void hci_conn_add_sysfs(struct hci_conn
*conn
)
108 struct hci_dev
*hdev
= conn
->hdev
;
110 BT_DBG("conn %p", conn
);
112 conn
->dev
.type
= &bt_link
;
113 conn
->dev
.class = bt_class
;
114 conn
->dev
.parent
= &hdev
->dev
;
116 snprintf(conn
->dev
.bus_id
, BUS_ID_SIZE
, "%s:%d",
117 hdev
->name
, conn
->handle
);
119 dev_set_drvdata(&conn
->dev
, conn
);
121 device_initialize(&conn
->dev
);
123 INIT_WORK(&conn
->work
, add_conn
);
125 queue_work(btaddconn
, &conn
->work
);
129 * The rfcomm tty device will possibly retain even when conn
130 * is down, and sysfs doesn't support move zombie device,
131 * so we should move the device before conn device is destroyed.
133 static int __match_tty(struct device
*dev
, void *data
)
135 return !strncmp(dev
->bus_id
, "rfcomm", 6);
138 static void del_conn(struct work_struct
*work
)
140 struct hci_conn
*conn
= container_of(work
, struct hci_conn
, work
);
141 struct hci_dev
*hdev
= conn
->hdev
;
146 dev
= device_find_child(&conn
->dev
, NULL
, __match_tty
);
149 device_move(dev
, NULL
);
153 device_del(&conn
->dev
);
154 put_device(&conn
->dev
);
158 void hci_conn_del_sysfs(struct hci_conn
*conn
)
160 BT_DBG("conn %p", conn
);
162 if (!device_is_registered(&conn
->dev
))
165 INIT_WORK(&conn
->work
, del_conn
);
167 queue_work(btdelconn
, &conn
->work
);
170 static inline char *host_typetostr(int type
)
192 static ssize_t
show_type(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
194 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
195 return sprintf(buf
, "%s\n", host_typetostr(hdev
->type
));
198 static ssize_t
show_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
200 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
204 for (i
= 0; i
< 248; i
++)
205 name
[i
] = hdev
->dev_name
[i
];
208 return sprintf(buf
, "%s\n", name
);
211 static ssize_t
show_class(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
213 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
214 return sprintf(buf
, "0x%.2x%.2x%.2x\n",
215 hdev
->dev_class
[2], hdev
->dev_class
[1], hdev
->dev_class
[0]);
218 static ssize_t
show_address(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
220 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
222 baswap(&bdaddr
, &hdev
->bdaddr
);
223 return sprintf(buf
, "%s\n", batostr(&bdaddr
));
226 static ssize_t
show_features(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
228 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
230 return sprintf(buf
, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
231 hdev
->features
[0], hdev
->features
[1],
232 hdev
->features
[2], hdev
->features
[3],
233 hdev
->features
[4], hdev
->features
[5],
234 hdev
->features
[6], hdev
->features
[7]);
237 static ssize_t
show_manufacturer(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
239 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
240 return sprintf(buf
, "%d\n", hdev
->manufacturer
);
243 static ssize_t
show_hci_version(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
245 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
246 return sprintf(buf
, "%d\n", hdev
->hci_ver
);
249 static ssize_t
show_hci_revision(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
251 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
252 return sprintf(buf
, "%d\n", hdev
->hci_rev
);
255 static ssize_t
show_inquiry_cache(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
257 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
258 struct inquiry_cache
*cache
= &hdev
->inq_cache
;
259 struct inquiry_entry
*e
;
262 hci_dev_lock_bh(hdev
);
264 for (e
= cache
->list
; e
; e
= e
->next
) {
265 struct inquiry_data
*data
= &e
->data
;
267 baswap(&bdaddr
, &data
->bdaddr
);
268 n
+= sprintf(buf
+ n
, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
270 data
->pscan_rep_mode
, data
->pscan_period_mode
,
271 data
->pscan_mode
, data
->dev_class
[2],
272 data
->dev_class
[1], data
->dev_class
[0],
273 __le16_to_cpu(data
->clock_offset
),
274 data
->rssi
, data
->ssp_mode
, e
->timestamp
);
277 hci_dev_unlock_bh(hdev
);
281 static ssize_t
show_idle_timeout(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
283 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
284 return sprintf(buf
, "%d\n", hdev
->idle_timeout
);
287 static ssize_t
store_idle_timeout(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
289 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
293 val
= simple_strtoul(buf
, &ptr
, 10);
297 if (val
!= 0 && (val
< 500 || val
> 3600000))
300 hdev
->idle_timeout
= val
;
305 static ssize_t
show_sniff_max_interval(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
307 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
308 return sprintf(buf
, "%d\n", hdev
->sniff_max_interval
);
311 static ssize_t
store_sniff_max_interval(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
313 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
317 val
= simple_strtoul(buf
, &ptr
, 10);
321 if (val
< 0x0002 || val
> 0xFFFE || val
% 2)
324 if (val
< hdev
->sniff_min_interval
)
327 hdev
->sniff_max_interval
= val
;
332 static ssize_t
show_sniff_min_interval(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
334 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
335 return sprintf(buf
, "%d\n", hdev
->sniff_min_interval
);
338 static ssize_t
store_sniff_min_interval(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
340 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
344 val
= simple_strtoul(buf
, &ptr
, 10);
348 if (val
< 0x0002 || val
> 0xFFFE || val
% 2)
351 if (val
> hdev
->sniff_max_interval
)
354 hdev
->sniff_min_interval
= val
;
359 static DEVICE_ATTR(type
, S_IRUGO
, show_type
, NULL
);
360 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
361 static DEVICE_ATTR(class, S_IRUGO
, show_class
, NULL
);
362 static DEVICE_ATTR(address
, S_IRUGO
, show_address
, NULL
);
363 static DEVICE_ATTR(features
, S_IRUGO
, show_features
, NULL
);
364 static DEVICE_ATTR(manufacturer
, S_IRUGO
, show_manufacturer
, NULL
);
365 static DEVICE_ATTR(hci_version
, S_IRUGO
, show_hci_version
, NULL
);
366 static DEVICE_ATTR(hci_revision
, S_IRUGO
, show_hci_revision
, NULL
);
367 static DEVICE_ATTR(inquiry_cache
, S_IRUGO
, show_inquiry_cache
, NULL
);
369 static DEVICE_ATTR(idle_timeout
, S_IRUGO
| S_IWUSR
,
370 show_idle_timeout
, store_idle_timeout
);
371 static DEVICE_ATTR(sniff_max_interval
, S_IRUGO
| S_IWUSR
,
372 show_sniff_max_interval
, store_sniff_max_interval
);
373 static DEVICE_ATTR(sniff_min_interval
, S_IRUGO
| S_IWUSR
,
374 show_sniff_min_interval
, store_sniff_min_interval
);
376 static struct attribute
*bt_host_attrs
[] = {
379 &dev_attr_class
.attr
,
380 &dev_attr_address
.attr
,
381 &dev_attr_features
.attr
,
382 &dev_attr_manufacturer
.attr
,
383 &dev_attr_hci_version
.attr
,
384 &dev_attr_hci_revision
.attr
,
385 &dev_attr_inquiry_cache
.attr
,
386 &dev_attr_idle_timeout
.attr
,
387 &dev_attr_sniff_max_interval
.attr
,
388 &dev_attr_sniff_min_interval
.attr
,
392 static struct attribute_group bt_host_group
= {
393 .attrs
= bt_host_attrs
,
396 static struct attribute_group
*bt_host_groups
[] = {
401 static void bt_host_release(struct device
*dev
)
403 void *data
= dev_get_drvdata(dev
);
407 static struct device_type bt_host
= {
409 .groups
= bt_host_groups
,
410 .release
= bt_host_release
,
413 int hci_register_sysfs(struct hci_dev
*hdev
)
415 struct device
*dev
= &hdev
->dev
;
418 BT_DBG("%p name %s type %d", hdev
, hdev
->name
, hdev
->type
);
420 dev
->type
= &bt_host
;
421 dev
->class = bt_class
;
422 dev
->parent
= hdev
->parent
;
424 strlcpy(dev
->bus_id
, hdev
->name
, BUS_ID_SIZE
);
426 dev_set_drvdata(dev
, hdev
);
428 err
= device_register(dev
);
435 void hci_unregister_sysfs(struct hci_dev
*hdev
)
437 BT_DBG("%p name %s type %d", hdev
, hdev
->name
, hdev
->type
);
439 device_del(&hdev
->dev
);
442 int __init
bt_sysfs_init(void)
444 btaddconn
= create_singlethread_workqueue("btaddconn");
448 btdelconn
= create_singlethread_workqueue("btdelconn");
450 destroy_workqueue(btaddconn
);
454 bt_class
= class_create(THIS_MODULE
, "bluetooth");
455 if (IS_ERR(bt_class
)) {
456 destroy_workqueue(btdelconn
);
457 destroy_workqueue(btaddconn
);
458 return PTR_ERR(bt_class
);
464 void bt_sysfs_cleanup(void)
466 destroy_workqueue(btaddconn
);
467 destroy_workqueue(btdelconn
);
469 class_destroy(bt_class
);