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
15 static struct workqueue_struct
*btaddconn
;
16 static struct workqueue_struct
*btdelconn
;
18 static inline char *typetostr(int type
)
40 static ssize_t
show_type(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
42 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
43 return sprintf(buf
, "%s\n", typetostr(hdev
->type
));
46 static ssize_t
show_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
48 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
52 for (i
= 0; i
< 248; i
++)
53 name
[i
] = hdev
->dev_name
[i
];
56 return sprintf(buf
, "%s\n", name
);
59 static ssize_t
show_class(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
61 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
62 return sprintf(buf
, "0x%.2x%.2x%.2x\n",
63 hdev
->dev_class
[2], hdev
->dev_class
[1], hdev
->dev_class
[0]);
66 static ssize_t
show_address(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
68 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
70 baswap(&bdaddr
, &hdev
->bdaddr
);
71 return sprintf(buf
, "%s\n", batostr(&bdaddr
));
74 static ssize_t
show_features(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
76 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
78 return sprintf(buf
, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
79 hdev
->features
[0], hdev
->features
[1],
80 hdev
->features
[2], hdev
->features
[3],
81 hdev
->features
[4], hdev
->features
[5],
82 hdev
->features
[6], hdev
->features
[7]);
85 static ssize_t
show_manufacturer(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
87 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
88 return sprintf(buf
, "%d\n", hdev
->manufacturer
);
91 static ssize_t
show_hci_version(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
93 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
94 return sprintf(buf
, "%d\n", hdev
->hci_ver
);
97 static ssize_t
show_hci_revision(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
99 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
100 return sprintf(buf
, "%d\n", hdev
->hci_rev
);
103 static ssize_t
show_inquiry_cache(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
105 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
106 struct inquiry_cache
*cache
= &hdev
->inq_cache
;
107 struct inquiry_entry
*e
;
110 hci_dev_lock_bh(hdev
);
112 for (e
= cache
->list
; e
; e
= e
->next
) {
113 struct inquiry_data
*data
= &e
->data
;
115 baswap(&bdaddr
, &data
->bdaddr
);
116 n
+= sprintf(buf
+ n
, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %u\n",
118 data
->pscan_rep_mode
, data
->pscan_period_mode
, data
->pscan_mode
,
119 data
->dev_class
[2], data
->dev_class
[1], data
->dev_class
[0],
120 __le16_to_cpu(data
->clock_offset
), data
->rssi
, e
->timestamp
);
123 hci_dev_unlock_bh(hdev
);
127 static ssize_t
show_idle_timeout(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
->idle_timeout
);
133 static ssize_t
store_idle_timeout(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
!= 0 && (val
< 500 || val
> 3600000))
146 hdev
->idle_timeout
= val
;
151 static ssize_t
show_sniff_max_interval(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
153 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
154 return sprintf(buf
, "%d\n", hdev
->sniff_max_interval
);
157 static ssize_t
store_sniff_max_interval(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
159 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
163 val
= simple_strtoul(buf
, &ptr
, 10);
167 if (val
< 0x0002 || val
> 0xFFFE || val
% 2)
170 if (val
< hdev
->sniff_min_interval
)
173 hdev
->sniff_max_interval
= val
;
178 static ssize_t
show_sniff_min_interval(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
180 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
181 return sprintf(buf
, "%d\n", hdev
->sniff_min_interval
);
184 static ssize_t
store_sniff_min_interval(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
186 struct hci_dev
*hdev
= dev_get_drvdata(dev
);
190 val
= simple_strtoul(buf
, &ptr
, 10);
194 if (val
< 0x0002 || val
> 0xFFFE || val
% 2)
197 if (val
> hdev
->sniff_max_interval
)
200 hdev
->sniff_min_interval
= val
;
205 static DEVICE_ATTR(type
, S_IRUGO
, show_type
, NULL
);
206 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
207 static DEVICE_ATTR(class, S_IRUGO
, show_class
, NULL
);
208 static DEVICE_ATTR(address
, S_IRUGO
, show_address
, NULL
);
209 static DEVICE_ATTR(features
, S_IRUGO
, show_features
, NULL
);
210 static DEVICE_ATTR(manufacturer
, S_IRUGO
, show_manufacturer
, NULL
);
211 static DEVICE_ATTR(hci_version
, S_IRUGO
, show_hci_version
, NULL
);
212 static DEVICE_ATTR(hci_revision
, S_IRUGO
, show_hci_revision
, NULL
);
213 static DEVICE_ATTR(inquiry_cache
, S_IRUGO
, show_inquiry_cache
, NULL
);
215 static DEVICE_ATTR(idle_timeout
, S_IRUGO
| S_IWUSR
,
216 show_idle_timeout
, store_idle_timeout
);
217 static DEVICE_ATTR(sniff_max_interval
, S_IRUGO
| S_IWUSR
,
218 show_sniff_max_interval
, store_sniff_max_interval
);
219 static DEVICE_ATTR(sniff_min_interval
, S_IRUGO
| S_IWUSR
,
220 show_sniff_min_interval
, store_sniff_min_interval
);
222 static struct device_attribute
*bt_attrs
[] = {
228 &dev_attr_manufacturer
,
229 &dev_attr_hci_version
,
230 &dev_attr_hci_revision
,
231 &dev_attr_inquiry_cache
,
232 &dev_attr_idle_timeout
,
233 &dev_attr_sniff_max_interval
,
234 &dev_attr_sniff_min_interval
,
238 static ssize_t
show_conn_type(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
240 struct hci_conn
*conn
= dev_get_drvdata(dev
);
241 return sprintf(buf
, "%s\n", conn
->type
== ACL_LINK
? "ACL" : "SCO");
244 static ssize_t
show_conn_address(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
246 struct hci_conn
*conn
= dev_get_drvdata(dev
);
248 baswap(&bdaddr
, &conn
->dst
);
249 return sprintf(buf
, "%s\n", batostr(&bdaddr
));
252 #define CONN_ATTR(_name,_mode,_show,_store) \
253 struct device_attribute conn_attr_##_name = __ATTR(_name,_mode,_show,_store)
255 static CONN_ATTR(type
, S_IRUGO
, show_conn_type
, NULL
);
256 static CONN_ATTR(address
, S_IRUGO
, show_conn_address
, NULL
);
258 static struct device_attribute
*conn_attrs
[] = {
264 struct class *bt_class
= NULL
;
265 EXPORT_SYMBOL_GPL(bt_class
);
267 static struct bus_type bt_bus
= {
271 static struct platform_device
*bt_platform
;
273 static void bt_release(struct device
*dev
)
275 void *data
= dev_get_drvdata(dev
);
279 static void add_conn(struct work_struct
*work
)
281 struct hci_conn
*conn
= container_of(work
, struct hci_conn
, work
);
284 flush_workqueue(btdelconn
);
285 if (device_add(&conn
->dev
) < 0) {
286 BT_ERR("Failed to register connection device");
290 for (i
= 0; conn_attrs
[i
]; i
++)
291 if (device_create_file(&conn
->dev
, conn_attrs
[i
]) < 0)
292 BT_ERR("Failed to create connection attribute");
295 void hci_conn_add_sysfs(struct hci_conn
*conn
)
297 struct hci_dev
*hdev
= conn
->hdev
;
298 bdaddr_t
*ba
= &conn
->dst
;
300 BT_DBG("conn %p", conn
);
302 conn
->dev
.bus
= &bt_bus
;
303 conn
->dev
.parent
= &hdev
->dev
;
305 conn
->dev
.release
= bt_release
;
307 snprintf(conn
->dev
.bus_id
, BUS_ID_SIZE
,
308 "%s%2.2X%2.2X%2.2X%2.2X%2.2X%2.2X",
309 conn
->type
== ACL_LINK
? "acl" : "sco",
310 ba
->b
[5], ba
->b
[4], ba
->b
[3],
311 ba
->b
[2], ba
->b
[1], ba
->b
[0]);
313 dev_set_drvdata(&conn
->dev
, conn
);
315 device_initialize(&conn
->dev
);
317 INIT_WORK(&conn
->work
, add_conn
);
319 queue_work(btaddconn
, &conn
->work
);
320 schedule_work(&conn
->work
);
323 static int __match_tty(struct device
*dev
, void *data
)
325 /* The rfcomm tty device will possibly retain even when conn
326 * is down, and sysfs doesn't support move zombie device,
327 * so we should move the device before conn device is destroyed.
328 * Due to the only child device of hci_conn dev is rfcomm
329 * tty_dev, here just return 1
334 static void del_conn(struct work_struct
*work
)
337 struct hci_conn
*conn
= container_of(work
, struct hci_conn
, work
);
339 while (dev
= device_find_child(&conn
->dev
, NULL
, __match_tty
)) {
340 device_move(dev
, NULL
);
343 device_del(&conn
->dev
);
344 put_device(&conn
->dev
);
347 void hci_conn_del_sysfs(struct hci_conn
*conn
)
349 BT_DBG("conn %p", conn
);
351 if (!device_is_registered(&conn
->dev
))
354 INIT_WORK(&conn
->work
, del_conn
);
356 queue_work(btdelconn
, &conn
->work
);
357 schedule_work(&conn
->work
);
360 int hci_register_sysfs(struct hci_dev
*hdev
)
362 struct device
*dev
= &hdev
->dev
;
366 BT_DBG("%p name %s type %d", hdev
, hdev
->name
, hdev
->type
);
369 dev
->parent
= hdev
->parent
;
371 strlcpy(dev
->bus_id
, hdev
->name
, BUS_ID_SIZE
);
373 dev
->release
= bt_release
;
375 dev_set_drvdata(dev
, hdev
);
377 err
= device_register(dev
);
381 for (i
= 0; bt_attrs
[i
]; i
++)
382 if (device_create_file(dev
, bt_attrs
[i
]) < 0)
383 BT_ERR("Failed to create device attribute");
385 if (sysfs_create_link(&bt_class
->subsys
.kobj
,
386 &dev
->kobj
, kobject_name(&dev
->kobj
)) < 0)
387 BT_ERR("Failed to create class symlink");
392 void hci_unregister_sysfs(struct hci_dev
*hdev
)
394 BT_DBG("%p name %s type %d", hdev
, hdev
->name
, hdev
->type
);
396 sysfs_remove_link(&bt_class
->subsys
.kobj
,
397 kobject_name(&hdev
->dev
.kobj
));
399 device_del(&hdev
->dev
);
402 int __init
bt_sysfs_init(void)
406 btaddconn
= create_singlethread_workqueue("btaddconn");
411 btdelconn
= create_singlethread_workqueue("btdelconn");
417 bt_platform
= platform_device_register_simple("bluetooth", -1, NULL
, 0);
418 if (IS_ERR(bt_platform
)) {
419 err
= PTR_ERR(bt_platform
);
423 err
= bus_register(&bt_bus
);
427 bt_class
= class_create(THIS_MODULE
, "bluetooth");
428 if (IS_ERR(bt_class
)) {
429 err
= PTR_ERR(bt_class
);
436 bus_unregister(&bt_bus
);
438 platform_device_unregister(bt_platform
);
440 destroy_workqueue(btdelconn
);
442 destroy_workqueue(btaddconn
);
447 void bt_sysfs_cleanup(void)
449 destroy_workqueue(btaddconn
);
450 destroy_workqueue(btdelconn
);
451 class_destroy(bt_class
);
452 bus_unregister(&bt_bus
);
453 platform_device_unregister(bt_platform
);