Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Bluetooth HCI driver model support. */ |
| 2 | |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 3 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 5 | #include <net/bluetooth/bluetooth.h> |
| 6 | #include <net/bluetooth/hci_core.h> |
| 7 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 8 | static struct class *bt_class; |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 9 | |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 10 | static void bt_link_release(struct device *dev) |
| 11 | { |
David Herrmann | 2dd1068 | 2012-02-09 21:58:34 +0100 | [diff] [blame] | 12 | struct hci_conn *conn = to_hci_conn(dev); |
| 13 | kfree(conn); |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | static struct device_type bt_link = { |
| 17 | .name = "link", |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 18 | .release = bt_link_release, |
| 19 | }; |
| 20 | |
Gustavo F. Padovan | 6d438e3 | 2011-12-17 18:53:02 -0200 | [diff] [blame] | 21 | /* |
| 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 | */ |
| 26 | static int __match_tty(struct device *dev, void *data) |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 27 | { |
Gustavo F. Padovan | 6d438e3 | 2011-12-17 18:53:02 -0200 | [diff] [blame] | 28 | return !strncmp(dev_name(dev), "rfcomm", 6); |
| 29 | } |
| 30 | |
| 31 | void hci_conn_init_sysfs(struct hci_conn *conn) |
| 32 | { |
Marcel Holtmann | 457ca7b | 2009-05-05 13:09:01 -0700 | [diff] [blame] | 33 | struct hci_dev *hdev = conn->hdev; |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 34 | |
Gustavo F. Padovan | 6d438e3 | 2011-12-17 18:53:02 -0200 | [diff] [blame] | 35 | 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 | |
| 44 | void 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 Holtmann | 457ca7b | 2009-05-05 13:09:01 -0700 | [diff] [blame] | 50 | dev_set_name(&conn->dev, "%s:%d", hdev->name, conn->handle); |
| 51 | |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 52 | if (device_add(&conn->dev) < 0) { |
| 53 | BT_ERR("Failed to register connection device"); |
| 54 | return; |
| 55 | } |
Marcel Holtmann | 384943e | 2009-05-08 18:20:43 -0700 | [diff] [blame] | 56 | |
| 57 | hci_dev_hold(hdev); |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Gustavo F. Padovan | 6d438e3 | 2011-12-17 18:53:02 -0200 | [diff] [blame] | 60 | void hci_conn_del_sysfs(struct hci_conn *conn) |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 61 | { |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 62 | struct hci_dev *hdev = conn->hdev; |
| 63 | |
Marcel Holtmann | a67e899 | 2009-05-02 18:24:06 -0700 | [diff] [blame] | 64 | if (!device_is_registered(&conn->dev)) |
| 65 | return; |
Roger Quadros | f3784d8 | 2009-04-23 14:50:54 +0300 | [diff] [blame] | 66 | |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 67 | while (1) { |
| 68 | struct device *dev; |
| 69 | |
| 70 | dev = device_find_child(&conn->dev, NULL, __match_tty); |
| 71 | if (!dev) |
| 72 | break; |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 73 | device_move(dev, NULL, DPM_ORDER_DEV_LAST); |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 74 | put_device(dev); |
| 75 | } |
| 76 | |
| 77 | device_del(&conn->dev); |
Marcel Holtmann | 384943e | 2009-05-08 18:20:43 -0700 | [diff] [blame] | 78 | |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 79 | hci_dev_put(hdev); |
| 80 | } |
| 81 | |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 82 | static void bt_host_release(struct device *dev) |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 83 | { |
David Herrmann | 2dd1068 | 2012-02-09 21:58:34 +0100 | [diff] [blame] | 84 | struct hci_dev *hdev = to_hci_dev(dev); |
| 85 | kfree(hdev); |
David Herrmann | 46e0653 | 2012-01-07 15:47:21 +0100 | [diff] [blame] | 86 | module_put(THIS_MODULE); |
Marcel Holtmann | b219e3a | 2006-07-06 12:38:46 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 89 | static struct device_type bt_host = { |
| 90 | .name = "host", |
Marcel Holtmann | 90855d7 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 91 | .release = bt_host_release, |
| 92 | }; |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 93 | |
David Herrmann | 0ac7e70 | 2011-10-08 14:58:47 +0200 | [diff] [blame] | 94 | void 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 Herrmann | 46e0653 | 2012-01-07 15:47:21 +0100 | [diff] [blame] | 101 | __module_get(THIS_MODULE); |
David Herrmann | 0ac7e70 | 2011-10-08 14:58:47 +0200 | [diff] [blame] | 102 | device_initialize(dev); |
| 103 | } |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | int __init bt_sysfs_init(void) |
| 106 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 107 | bt_class = class_create(THIS_MODULE, "bluetooth"); |
Marcel Holtmann | 27d3528 | 2006-07-03 10:02:37 +0200 | [diff] [blame] | 108 | |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 109 | return PTR_ERR_OR_ZERO(bt_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Arnaud Patard | 860e13b | 2006-09-28 15:29:37 -0700 | [diff] [blame] | 112 | void bt_sysfs_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
Marcel Holtmann | a91f2e3 | 2006-07-03 10:02:41 +0200 | [diff] [blame] | 114 | class_destroy(bt_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |