tizen 2.4 release
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / net / bluetooth / hci_sysfs.c
1 /* Bluetooth HCI driver model support. */
2
3 #include <linux/module.h>
4
5 #include <net/bluetooth/bluetooth.h>
6 #include <net/bluetooth/hci_core.h>
7
8 static struct class *bt_class;
9
10 static inline char *link_typetostr(int type)
11 {
12         switch (type) {
13         case ACL_LINK:
14                 return "ACL";
15         case SCO_LINK:
16                 return "SCO";
17         case ESCO_LINK:
18                 return "eSCO";
19         case LE_LINK:
20                 return "LE";
21         default:
22                 return "UNKNOWN";
23         }
24 }
25
26 static ssize_t show_link_type(struct device *dev,
27                               struct device_attribute *attr, char *buf)
28 {
29         struct hci_conn *conn = to_hci_conn(dev);
30         return sprintf(buf, "%s\n", link_typetostr(conn->type));
31 }
32
33 static ssize_t show_link_address(struct device *dev,
34                                  struct device_attribute *attr, char *buf)
35 {
36         struct hci_conn *conn = to_hci_conn(dev);
37         return sprintf(buf, "%pMR\n", &conn->dst);
38 }
39
40 #define LINK_ATTR(_name, _mode, _show, _store) \
41 struct device_attribute link_attr_##_name = __ATTR(_name, _mode, _show, _store)
42
43 static LINK_ATTR(type, S_IRUGO, show_link_type, NULL);
44 static LINK_ATTR(address, S_IRUGO, show_link_address, NULL);
45
46 static struct attribute *bt_link_attrs[] = {
47         &link_attr_type.attr,
48         &link_attr_address.attr,
49         NULL
50 };
51
52 #ifdef CONFIG_TIZEN_WIP
53 static struct attribute_group bt_link_group = {
54         .attrs = bt_link_attrs,
55 };
56
57 static const struct attribute_group *bt_link_groups[] = {
58         &bt_link_group,
59         NULL
60 };
61 #else
62 ATTRIBUTE_GROUPS(bt_link);
63 #endif
64
65 static void bt_link_release(struct device *dev)
66 {
67         struct hci_conn *conn = to_hci_conn(dev);
68         kfree(conn);
69 }
70
71 static struct device_type bt_link = {
72         .name    = "link",
73         .groups  = bt_link_groups,
74         .release = bt_link_release,
75 };
76
77 /*
78  * The rfcomm tty device will possibly retain even when conn
79  * is down, and sysfs doesn't support move zombie device,
80  * so we should move the device before conn device is destroyed.
81  */
82 static int __match_tty(struct device *dev, void *data)
83 {
84         return !strncmp(dev_name(dev), "rfcomm", 6);
85 }
86
87 void hci_conn_init_sysfs(struct hci_conn *conn)
88 {
89         struct hci_dev *hdev = conn->hdev;
90
91         BT_DBG("conn %p", conn);
92
93         conn->dev.type = &bt_link;
94         conn->dev.class = bt_class;
95         conn->dev.parent = &hdev->dev;
96
97         device_initialize(&conn->dev);
98 }
99
100 void hci_conn_add_sysfs(struct hci_conn *conn)
101 {
102         struct hci_dev *hdev = conn->hdev;
103
104         BT_DBG("conn %p", conn);
105
106         dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle);
107
108         if (device_add(&conn->dev) < 0) {
109                 BT_ERR("Failed to register connection device");
110                 return;
111         }
112
113         hci_dev_hold(hdev);
114 }
115
116 void hci_conn_del_sysfs(struct hci_conn *conn)
117 {
118         struct hci_dev *hdev = conn->hdev;
119
120         if (!device_is_registered(&conn->dev))
121                 return;
122
123         while (1) {
124                 struct device *dev;
125
126                 dev = device_find_child(&conn->dev, NULL, __match_tty);
127                 if (!dev)
128                         break;
129                 device_move(dev, NULL, DPM_ORDER_DEV_LAST);
130                 put_device(dev);
131         }
132
133         device_del(&conn->dev);
134
135         hci_dev_put(hdev);
136 }
137
138 static inline char *host_typetostr(int type)
139 {
140         switch (type) {
141         case HCI_BREDR:
142                 return "BR/EDR";
143         case HCI_AMP:
144                 return "AMP";
145         default:
146                 return "UNKNOWN";
147         }
148 }
149
150 static ssize_t show_type(struct device *dev,
151                          struct device_attribute *attr, char *buf)
152 {
153         struct hci_dev *hdev = to_hci_dev(dev);
154         return sprintf(buf, "%s\n", host_typetostr(hdev->dev_type));
155 }
156
157 static ssize_t show_name(struct device *dev,
158                          struct device_attribute *attr, char *buf)
159 {
160         struct hci_dev *hdev = to_hci_dev(dev);
161         char name[HCI_MAX_NAME_LENGTH + 1];
162         int i;
163
164         for (i = 0; i < HCI_MAX_NAME_LENGTH; i++)
165                 name[i] = hdev->dev_name[i];
166
167         name[HCI_MAX_NAME_LENGTH] = '\0';
168         return sprintf(buf, "%s\n", name);
169 }
170
171 static ssize_t show_address(struct device *dev,
172                             struct device_attribute *attr, char *buf)
173 {
174         struct hci_dev *hdev = to_hci_dev(dev);
175         return sprintf(buf, "%pMR\n", &hdev->bdaddr);
176 }
177
178 static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
179 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
180 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
181
182 static struct attribute *bt_host_attrs[] = {
183         &dev_attr_type.attr,
184         &dev_attr_name.attr,
185         &dev_attr_address.attr,
186         NULL
187 };
188
189 #ifdef CONFIG_TIZEN_WIP
190 static struct attribute_group bt_host_group = {
191         .attrs = bt_host_attrs,
192 };
193
194 static const struct attribute_group *bt_host_groups[] = {
195         &bt_host_group,
196         NULL
197 };
198 #else
199 ATTRIBUTE_GROUPS(bt_host);
200 #endif
201
202 static void bt_host_release(struct device *dev)
203 {
204         struct hci_dev *hdev = to_hci_dev(dev);
205         kfree(hdev);
206         module_put(THIS_MODULE);
207 }
208
209 static struct device_type bt_host = {
210         .name    = "host",
211         .groups  = bt_host_groups,
212         .release = bt_host_release,
213 };
214
215 void hci_init_sysfs(struct hci_dev *hdev)
216 {
217         struct device *dev = &hdev->dev;
218
219         dev->type = &bt_host;
220         dev->class = bt_class;
221
222         __module_get(THIS_MODULE);
223         device_initialize(dev);
224 }
225
226 int __init bt_sysfs_init(void)
227 {
228         bt_class = class_create(THIS_MODULE, "bluetooth");
229 #ifdef CONFIG_TIZEN_WIP
230         return PTR_RET(bt_class);
231 #else
232         return PTR_ERR_OR_ZERO(bt_class);
233 #endif
234
235 }
236
237 void bt_sysfs_cleanup(void)
238 {
239         class_destroy(bt_class);
240 }