blob: ca7a35ebaefb61095f278106adc8d5529a247d31 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Bluetooth HCI driver model support. */
2
Paul Gortmaker3a9a2312011-05-27 09:12:25 -04003#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5#include <net/bluetooth/bluetooth.h>
6#include <net/bluetooth/hci_core.h>
7
Marcel Holtmannaef7d972010-03-21 05:27:45 +01008static struct class *bt_class;
Marcel Holtmann90855d72008-08-18 13:23:53 +02009
Marcel Holtmann90855d72008-08-18 13:23:53 +020010static void bt_link_release(struct device *dev)
11{
David Herrmann2dd10682012-02-09 21:58:34 +010012 struct hci_conn *conn = to_hci_conn(dev);
13 kfree(conn);
Marcel Holtmann90855d72008-08-18 13:23:53 +020014}
15
16static struct device_type bt_link = {
17 .name = "link",
Marcel Holtmann90855d72008-08-18 13:23:53 +020018 .release = bt_link_release,
19};
20
Gustavo F. Padovan6d438e32011-12-17 18:53:02 -020021/*
22 * The rfcomm tty device will possibly retain even when conn
23 * is down, and sysfs doesn't support move zombie device,
24 * so we should move the device before conn device is destroyed.
25 */
26static int __match_tty(struct device *dev, void *data)
Marcel Holtmann90855d72008-08-18 13:23:53 +020027{
Gustavo F. Padovan6d438e32011-12-17 18:53:02 -020028 return !strncmp(dev_name(dev), "rfcomm", 6);
29}
30
31void hci_conn_init_sysfs(struct hci_conn *conn)
32{
Marcel Holtmann457ca7b2009-05-05 13:09:01 -070033 struct hci_dev *hdev = conn->hdev;
Marcel Holtmann90855d72008-08-18 13:23:53 +020034
Gustavo F. Padovan6d438e32011-12-17 18:53:02 -020035 BT_DBG("conn %p", conn);
36
37 conn->dev.type = &bt_link;
38 conn->dev.class = bt_class;
39 conn->dev.parent = &hdev->dev;
40
41 device_initialize(&conn->dev);
42}
43
44void hci_conn_add_sysfs(struct hci_conn *conn)
45{
46 struct hci_dev *hdev = conn->hdev;
47
48 BT_DBG("conn %p", conn);
49
Marcel Holtmann457ca7b2009-05-05 13:09:01 -070050 dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
51
Marcel Holtmann90855d72008-08-18 13:23:53 +020052 if (device_add(&conn->dev) < 0) {
53 BT_ERR("Failed to register connection device");
54 return;
55 }
Marcel Holtmann384943e2009-05-08 18:20:43 -070056
57 hci_dev_hold(hdev);
Marcel Holtmann90855d72008-08-18 13:23:53 +020058}
59
Gustavo F. Padovan6d438e32011-12-17 18:53:02 -020060void hci_conn_del_sysfs(struct hci_conn *conn)
Marcel Holtmann90855d72008-08-18 13:23:53 +020061{
Marcel Holtmann90855d72008-08-18 13:23:53 +020062 struct hci_dev *hdev = conn->hdev;
63
Marcel Holtmanna67e8992009-05-02 18:24:06 -070064 if (!device_is_registered(&conn->dev))
65 return;
Roger Quadrosf3784d82009-04-23 14:50:54 +030066
Marcel Holtmann90855d72008-08-18 13:23:53 +020067 while (1) {
68 struct device *dev;
69
70 dev = device_find_child(&conn->dev, NULL, __match_tty);
71 if (!dev)
72 break;
Cornelia Huckffa6a702009-03-04 12:44:00 +010073 device_move(dev, NULL, DPM_ORDER_DEV_LAST);
Marcel Holtmann90855d72008-08-18 13:23:53 +020074 put_device(dev);
75 }
76
77 device_del(&conn->dev);
Marcel Holtmann384943e2009-05-08 18:20:43 -070078
Marcel Holtmann90855d72008-08-18 13:23:53 +020079 hci_dev_put(hdev);
80}
81
Marcel Holtmann90855d72008-08-18 13:23:53 +020082static void bt_host_release(struct device *dev)
Marcel Holtmanna91f2e32006-07-03 10:02:41 +020083{
David Herrmann2dd10682012-02-09 21:58:34 +010084 struct hci_dev *hdev = to_hci_dev(dev);
85 kfree(hdev);
David Herrmann46e06532012-01-07 15:47:21 +010086 module_put(THIS_MODULE);
Marcel Holtmannb219e3a2006-07-06 12:38:46 +020087}
88
Marcel Holtmann90855d72008-08-18 13:23:53 +020089static struct device_type bt_host = {
90 .name = "host",
Marcel Holtmann90855d72008-08-18 13:23:53 +020091 .release = bt_host_release,
92};
Marcel Holtmanna91f2e32006-07-03 10:02:41 +020093
David Herrmann0ac7e702011-10-08 14:58:47 +020094void hci_init_sysfs(struct hci_dev *hdev)
95{
96 struct device *dev = &hdev->dev;
97
98 dev->type = &bt_host;
99 dev->class = bt_class;
100
David Herrmann46e06532012-01-07 15:47:21 +0100101 __module_get(THIS_MODULE);
David Herrmann0ac7e702011-10-08 14:58:47 +0200102 device_initialize(dev);
103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105int __init bt_sysfs_init(void)
106{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200107 bt_class = class_create(THIS_MODULE, "bluetooth");
Marcel Holtmann27d35282006-07-03 10:02:37 +0200108
Rusty Russell8c6ffba2013-07-15 11:20:32 +0930109 return PTR_ERR_OR_ZERO(bt_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Arnaud Patard860e13b2006-09-28 15:29:37 -0700112void bt_sysfs_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200114 class_destroy(bt_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}