Commit | Line | Data |
---|---|---|
82f5169b RH |
1 | /* |
2 | * Bluetooth HCI serdev driver lib | |
3 | * | |
4 | * Copyright (C) 2017 Linaro, Ltd., Rob Herring <robh@kernel.org> | |
5 | * | |
6 | * Based on hci_ldisc.c: | |
7 | * | |
8 | * Copyright (C) 2000-2001 Qualcomm Incorporated | |
9 | * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com> | |
10 | * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org> | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
21 | * | |
22 | */ | |
23 | ||
24 | #include <linux/kernel.h> | |
25 | #include <linux/types.h> | |
26 | #include <linux/serdev.h> | |
27 | #include <linux/skbuff.h> | |
28 | ||
29 | #include <net/bluetooth/bluetooth.h> | |
30 | #include <net/bluetooth/hci_core.h> | |
31 | ||
32 | #include "hci_uart.h" | |
33 | ||
640e32c9 | 34 | static struct serdev_device_ops hci_serdev_client_ops; |
82f5169b RH |
35 | |
36 | static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type) | |
37 | { | |
38 | struct hci_dev *hdev = hu->hdev; | |
39 | ||
40 | /* Update HCI stat counters */ | |
41 | switch (pkt_type) { | |
42 | case HCI_COMMAND_PKT: | |
43 | hdev->stat.cmd_tx++; | |
44 | break; | |
45 | ||
46 | case HCI_ACLDATA_PKT: | |
47 | hdev->stat.acl_tx++; | |
48 | break; | |
49 | ||
50 | case HCI_SCODATA_PKT: | |
51 | hdev->stat.sco_tx++; | |
52 | break; | |
53 | } | |
54 | } | |
55 | ||
56 | static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu) | |
57 | { | |
58 | struct sk_buff *skb = hu->tx_skb; | |
59 | ||
5a637751 BG |
60 | if (!skb) { |
61 | if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) | |
62 | skb = hu->proto->dequeue(hu); | |
63 | } else | |
82f5169b RH |
64 | hu->tx_skb = NULL; |
65 | ||
66 | return skb; | |
67 | } | |
68 | ||
69 | static void hci_uart_write_work(struct work_struct *work) | |
70 | { | |
71 | struct hci_uart *hu = container_of(work, struct hci_uart, write_work); | |
72 | struct serdev_device *serdev = hu->serdev; | |
73 | struct hci_dev *hdev = hu->hdev; | |
74 | struct sk_buff *skb; | |
75 | ||
76 | /* REVISIT: | |
77 | * should we cope with bad skbs or ->write() returning an error value? | |
78 | */ | |
79 | do { | |
80 | clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state); | |
81 | ||
82 | while ((skb = hci_uart_dequeue(hu))) { | |
83 | int len; | |
84 | ||
85 | len = serdev_device_write_buf(serdev, | |
86 | skb->data, skb->len); | |
87 | hdev->stat.byte_tx += len; | |
88 | ||
89 | skb_pull(skb, len); | |
90 | if (skb->len) { | |
91 | hu->tx_skb = skb; | |
92 | break; | |
93 | } | |
94 | ||
95 | hci_uart_tx_complete(hu, hci_skb_pkt_type(skb)); | |
96 | kfree_skb(skb); | |
97 | } | |
ea9ed991 | 98 | } while (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state)); |
82f5169b RH |
99 | |
100 | clear_bit(HCI_UART_SENDING, &hu->tx_state); | |
101 | } | |
102 | ||
103 | /* ------- Interface to HCI layer ------ */ | |
104 | ||
82f5169b RH |
105 | /* Reset device */ |
106 | static int hci_uart_flush(struct hci_dev *hdev) | |
107 | { | |
108 | struct hci_uart *hu = hci_get_drvdata(hdev); | |
109 | ||
110 | BT_DBG("hdev %p serdev %p", hdev, hu->serdev); | |
111 | ||
112 | if (hu->tx_skb) { | |
113 | kfree_skb(hu->tx_skb); hu->tx_skb = NULL; | |
114 | } | |
115 | ||
116 | /* Flush any pending characters in the driver and discipline. */ | |
117 | serdev_device_write_flush(hu->serdev); | |
118 | ||
119 | if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) | |
120 | hu->proto->flush(hu); | |
121 | ||
122 | return 0; | |
123 | } | |
124 | ||
412fe29f HG |
125 | /* Initialize device */ |
126 | static int hci_uart_open(struct hci_dev *hdev) | |
127 | { | |
128 | BT_DBG("%s %p", hdev->name, hdev); | |
129 | ||
130 | /* Undo clearing this from hci_uart_close() */ | |
131 | hdev->flush = hci_uart_flush; | |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
82f5169b RH |
136 | /* Close device */ |
137 | static int hci_uart_close(struct hci_dev *hdev) | |
138 | { | |
82f5169b RH |
139 | BT_DBG("hdev %p", hdev); |
140 | ||
141 | hci_uart_flush(hdev); | |
142 | hdev->flush = NULL; | |
143 | ||
82f5169b RH |
144 | return 0; |
145 | } | |
146 | ||
147 | /* Send frames from HCI layer */ | |
148 | static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb) | |
149 | { | |
150 | struct hci_uart *hu = hci_get_drvdata(hdev); | |
151 | ||
152 | BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb), | |
153 | skb->len); | |
154 | ||
155 | hu->proto->enqueue(hu, skb); | |
156 | ||
157 | hci_uart_tx_wakeup(hu); | |
158 | ||
159 | return 0; | |
160 | } | |
161 | ||
162 | static int hci_uart_setup(struct hci_dev *hdev) | |
163 | { | |
164 | struct hci_uart *hu = hci_get_drvdata(hdev); | |
165 | struct hci_rp_read_local_version *ver; | |
166 | struct sk_buff *skb; | |
167 | unsigned int speed; | |
168 | int err; | |
169 | ||
170 | /* Init speed if any */ | |
171 | if (hu->init_speed) | |
172 | speed = hu->init_speed; | |
173 | else if (hu->proto->init_speed) | |
174 | speed = hu->proto->init_speed; | |
175 | else | |
176 | speed = 0; | |
177 | ||
178 | if (speed) | |
179 | serdev_device_set_baudrate(hu->serdev, speed); | |
180 | ||
181 | /* Operational speed if any */ | |
182 | if (hu->oper_speed) | |
183 | speed = hu->oper_speed; | |
184 | else if (hu->proto->oper_speed) | |
185 | speed = hu->proto->oper_speed; | |
186 | else | |
187 | speed = 0; | |
188 | ||
189 | if (hu->proto->set_baudrate && speed) { | |
190 | err = hu->proto->set_baudrate(hu, speed); | |
191 | if (err) | |
2064ee33 | 192 | bt_dev_err(hdev, "Failed to set baudrate"); |
82f5169b RH |
193 | else |
194 | serdev_device_set_baudrate(hu->serdev, speed); | |
195 | } | |
196 | ||
197 | if (hu->proto->setup) | |
198 | return hu->proto->setup(hu); | |
199 | ||
200 | if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags)) | |
201 | return 0; | |
202 | ||
203 | skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, | |
204 | HCI_INIT_TIMEOUT); | |
205 | if (IS_ERR(skb)) { | |
2064ee33 MH |
206 | bt_dev_err(hdev, "Reading local version info failed (%ld)", |
207 | PTR_ERR(skb)); | |
82f5169b RH |
208 | return 0; |
209 | } | |
210 | ||
0c0c09ff | 211 | if (skb->len != sizeof(*ver)) |
2064ee33 | 212 | bt_dev_err(hdev, "Event length mismatch for version info"); |
82f5169b RH |
213 | |
214 | kfree_skb(skb); | |
215 | return 0; | |
216 | } | |
217 | ||
218 | /** hci_uart_write_wakeup - transmit buffer wakeup | |
219 | * @serdev: serial device | |
220 | * | |
221 | * This function is called by the serdev framework when it accepts | |
222 | * more data being sent. | |
223 | */ | |
224 | static void hci_uart_write_wakeup(struct serdev_device *serdev) | |
225 | { | |
226 | struct hci_uart *hu = serdev_device_get_drvdata(serdev); | |
227 | ||
228 | BT_DBG(""); | |
229 | ||
230 | if (!hu || serdev != hu->serdev) { | |
231 | WARN_ON(1); | |
232 | return; | |
233 | } | |
234 | ||
235 | if (test_bit(HCI_UART_PROTO_READY, &hu->flags)) | |
236 | hci_uart_tx_wakeup(hu); | |
237 | } | |
238 | ||
239 | /** hci_uart_receive_buf - receive buffer wakeup | |
240 | * @serdev: serial device | |
241 | * @data: pointer to received data | |
242 | * @count: count of received data in bytes | |
243 | * | |
244 | * This function is called by the serdev framework when it received data | |
245 | * in the RX buffer. | |
246 | * | |
247 | * Return: number of processed bytes | |
248 | */ | |
249 | static int hci_uart_receive_buf(struct serdev_device *serdev, const u8 *data, | |
250 | size_t count) | |
251 | { | |
252 | struct hci_uart *hu = serdev_device_get_drvdata(serdev); | |
253 | ||
254 | if (!hu || serdev != hu->serdev) { | |
255 | WARN_ON(1); | |
256 | return 0; | |
257 | } | |
258 | ||
259 | if (!test_bit(HCI_UART_PROTO_READY, &hu->flags)) | |
260 | return 0; | |
261 | ||
262 | /* It does not need a lock here as it is already protected by a mutex in | |
263 | * tty caller | |
264 | */ | |
265 | hu->proto->recv(hu, data, count); | |
266 | ||
267 | if (hu->hdev) | |
268 | hu->hdev->stat.byte_rx += count; | |
269 | ||
270 | return count; | |
271 | } | |
272 | ||
640e32c9 | 273 | static struct serdev_device_ops hci_serdev_client_ops = { |
82f5169b RH |
274 | .receive_buf = hci_uart_receive_buf, |
275 | .write_wakeup = hci_uart_write_wakeup, | |
276 | }; | |
277 | ||
278 | int hci_uart_register_device(struct hci_uart *hu, | |
279 | const struct hci_uart_proto *p) | |
280 | { | |
281 | int err; | |
282 | struct hci_dev *hdev; | |
283 | ||
284 | BT_DBG(""); | |
285 | ||
52b318e6 SR |
286 | serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops); |
287 | ||
e9ca0807 | 288 | err = serdev_device_open(hu->serdev); |
82f5169b RH |
289 | if (err) |
290 | return err; | |
291 | ||
e9ca0807 HG |
292 | err = p->open(hu); |
293 | if (err) | |
294 | goto err_open; | |
295 | ||
82f5169b RH |
296 | hu->proto = p; |
297 | set_bit(HCI_UART_PROTO_READY, &hu->flags); | |
298 | ||
299 | /* Initialize and register HCI device */ | |
300 | hdev = hci_alloc_dev(); | |
301 | if (!hdev) { | |
302 | BT_ERR("Can't allocate HCI device"); | |
303 | err = -ENOMEM; | |
304 | goto err_alloc; | |
305 | } | |
306 | ||
307 | hu->hdev = hdev; | |
308 | ||
309 | hdev->bus = HCI_UART; | |
310 | hci_set_drvdata(hdev, hu); | |
311 | ||
fdee6d8f | 312 | INIT_WORK(&hu->init_ready, hci_uart_init_work); |
82f5169b | 313 | INIT_WORK(&hu->write_work, hci_uart_write_work); |
d73e1728 | 314 | percpu_init_rwsem(&hu->proto_lock); |
82f5169b RH |
315 | |
316 | /* Only when vendor specific setup callback is provided, consider | |
317 | * the manufacturer information valid. This avoids filling in the | |
318 | * value for Ericsson when nothing is specified. | |
319 | */ | |
320 | if (hu->proto->setup) | |
321 | hdev->manufacturer = hu->proto->manufacturer; | |
322 | ||
323 | hdev->open = hci_uart_open; | |
324 | hdev->close = hci_uart_close; | |
325 | hdev->flush = hci_uart_flush; | |
326 | hdev->send = hci_uart_send_frame; | |
327 | hdev->setup = hci_uart_setup; | |
328 | SET_HCIDEV_DEV(hdev, &hu->serdev->dev); | |
329 | ||
330 | if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags)) | |
331 | set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks); | |
332 | ||
333 | if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags)) | |
334 | set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks); | |
335 | ||
82f5169b RH |
336 | if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags)) |
337 | hdev->dev_type = HCI_AMP; | |
338 | else | |
339 | hdev->dev_type = HCI_PRIMARY; | |
340 | ||
341 | if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags)) | |
342 | return 0; | |
343 | ||
344 | if (hci_register_dev(hdev) < 0) { | |
345 | BT_ERR("Can't register HCI device"); | |
346 | err = -ENODEV; | |
347 | goto err_register; | |
348 | } | |
349 | ||
350 | set_bit(HCI_UART_REGISTERED, &hu->flags); | |
351 | ||
352 | return 0; | |
353 | ||
354 | err_register: | |
355 | hci_free_dev(hdev); | |
356 | err_alloc: | |
357 | clear_bit(HCI_UART_PROTO_READY, &hu->flags); | |
358 | p->close(hu); | |
e9ca0807 HG |
359 | err_open: |
360 | serdev_device_close(hu->serdev); | |
82f5169b RH |
361 | return err; |
362 | } | |
081f36a8 | 363 | EXPORT_SYMBOL_GPL(hci_uart_register_device); |
c34dc3bf IM |
364 | |
365 | void hci_uart_unregister_device(struct hci_uart *hu) | |
366 | { | |
367 | struct hci_dev *hdev = hu->hdev; | |
368 | ||
7cf7846d | 369 | clear_bit(HCI_UART_PROTO_READY, &hu->flags); |
c34dc3bf IM |
370 | hci_unregister_dev(hdev); |
371 | hci_free_dev(hdev); | |
372 | ||
373 | cancel_work_sync(&hu->write_work); | |
374 | ||
375 | hu->proto->close(hu); | |
e9ca0807 | 376 | serdev_device_close(hu->serdev); |
c34dc3bf IM |
377 | } |
378 | EXPORT_SYMBOL_GPL(hci_uart_unregister_device); |