zram: remove zram_meta structure
[linux/fpc-iii.git] / drivers / bluetooth / hci_serdev.c
blob7de0edc0ff8cd73bca5d81bc1a96794c35234a17
1 /*
2 * Bluetooth HCI serdev driver lib
4 * Copyright (C) 2017 Linaro, Ltd., Rob Herring <robh@kernel.org>
6 * Based on hci_ldisc.c:
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>
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.
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.
24 #include <linux/kernel.h>
25 #include <linux/types.h>
26 #include <linux/serdev.h>
27 #include <linux/skbuff.h>
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
32 #include "hci_uart.h"
34 struct serdev_device_ops hci_serdev_client_ops;
36 static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
38 struct hci_dev *hdev = hu->hdev;
40 /* Update HCI stat counters */
41 switch (pkt_type) {
42 case HCI_COMMAND_PKT:
43 hdev->stat.cmd_tx++;
44 break;
46 case HCI_ACLDATA_PKT:
47 hdev->stat.acl_tx++;
48 break;
50 case HCI_SCODATA_PKT:
51 hdev->stat.sco_tx++;
52 break;
56 static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
58 struct sk_buff *skb = hu->tx_skb;
60 if (!skb)
61 skb = hu->proto->dequeue(hu);
62 else
63 hu->tx_skb = NULL;
65 return skb;
68 static void hci_uart_write_work(struct work_struct *work)
70 struct hci_uart *hu = container_of(work, struct hci_uart, write_work);
71 struct serdev_device *serdev = hu->serdev;
72 struct hci_dev *hdev = hu->hdev;
73 struct sk_buff *skb;
75 /* REVISIT:
76 * should we cope with bad skbs or ->write() returning an error value?
78 do {
79 clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
81 while ((skb = hci_uart_dequeue(hu))) {
82 int len;
84 len = serdev_device_write_buf(serdev,
85 skb->data, skb->len);
86 hdev->stat.byte_tx += len;
88 skb_pull(skb, len);
89 if (skb->len) {
90 hu->tx_skb = skb;
91 break;
94 hci_uart_tx_complete(hu, hci_skb_pkt_type(skb));
95 kfree_skb(skb);
97 } while(test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state));
99 clear_bit(HCI_UART_SENDING, &hu->tx_state);
102 /* ------- Interface to HCI layer ------ */
104 /* Initialize device */
105 static int hci_uart_open(struct hci_dev *hdev)
107 BT_DBG("%s %p", hdev->name, hdev);
109 return 0;
112 /* Reset device */
113 static int hci_uart_flush(struct hci_dev *hdev)
115 struct hci_uart *hu = hci_get_drvdata(hdev);
117 BT_DBG("hdev %p serdev %p", hdev, hu->serdev);
119 if (hu->tx_skb) {
120 kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
123 /* Flush any pending characters in the driver and discipline. */
124 serdev_device_write_flush(hu->serdev);
126 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
127 hu->proto->flush(hu);
129 return 0;
132 /* Close device */
133 static int hci_uart_close(struct hci_dev *hdev)
135 BT_DBG("hdev %p", hdev);
137 hci_uart_flush(hdev);
138 hdev->flush = NULL;
140 return 0;
143 /* Send frames from HCI layer */
144 static int hci_uart_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
146 struct hci_uart *hu = hci_get_drvdata(hdev);
148 BT_DBG("%s: type %d len %d", hdev->name, hci_skb_pkt_type(skb),
149 skb->len);
151 hu->proto->enqueue(hu, skb);
153 hci_uart_tx_wakeup(hu);
155 return 0;
158 static int hci_uart_setup(struct hci_dev *hdev)
160 struct hci_uart *hu = hci_get_drvdata(hdev);
161 struct hci_rp_read_local_version *ver;
162 struct sk_buff *skb;
163 unsigned int speed;
164 int err;
166 /* Init speed if any */
167 if (hu->init_speed)
168 speed = hu->init_speed;
169 else if (hu->proto->init_speed)
170 speed = hu->proto->init_speed;
171 else
172 speed = 0;
174 if (speed)
175 serdev_device_set_baudrate(hu->serdev, speed);
177 /* Operational speed if any */
178 if (hu->oper_speed)
179 speed = hu->oper_speed;
180 else if (hu->proto->oper_speed)
181 speed = hu->proto->oper_speed;
182 else
183 speed = 0;
185 if (hu->proto->set_baudrate && speed) {
186 err = hu->proto->set_baudrate(hu, speed);
187 if (err)
188 BT_ERR("%s: failed to set baudrate", hdev->name);
189 else
190 serdev_device_set_baudrate(hu->serdev, speed);
193 if (hu->proto->setup)
194 return hu->proto->setup(hu);
196 if (!test_bit(HCI_UART_VND_DETECT, &hu->hdev_flags))
197 return 0;
199 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
200 HCI_INIT_TIMEOUT);
201 if (IS_ERR(skb)) {
202 BT_ERR("%s: Reading local version information failed (%ld)",
203 hdev->name, PTR_ERR(skb));
204 return 0;
207 if (skb->len != sizeof(*ver)) {
208 BT_ERR("%s: Event length mismatch for version information",
209 hdev->name);
212 kfree_skb(skb);
213 return 0;
216 /** hci_uart_write_wakeup - transmit buffer wakeup
217 * @serdev: serial device
219 * This function is called by the serdev framework when it accepts
220 * more data being sent.
222 static void hci_uart_write_wakeup(struct serdev_device *serdev)
224 struct hci_uart *hu = serdev_device_get_drvdata(serdev);
226 BT_DBG("");
228 if (!hu || serdev != hu->serdev) {
229 WARN_ON(1);
230 return;
233 if (test_bit(HCI_UART_PROTO_READY, &hu->flags))
234 hci_uart_tx_wakeup(hu);
237 /** hci_uart_receive_buf - receive buffer wakeup
238 * @serdev: serial device
239 * @data: pointer to received data
240 * @count: count of received data in bytes
242 * This function is called by the serdev framework when it received data
243 * in the RX buffer.
245 * Return: number of processed bytes
247 static int hci_uart_receive_buf(struct serdev_device *serdev, const u8 *data,
248 size_t count)
250 struct hci_uart *hu = serdev_device_get_drvdata(serdev);
252 if (!hu || serdev != hu->serdev) {
253 WARN_ON(1);
254 return 0;
257 if (!test_bit(HCI_UART_PROTO_READY, &hu->flags))
258 return 0;
260 /* It does not need a lock here as it is already protected by a mutex in
261 * tty caller
263 hu->proto->recv(hu, data, count);
265 if (hu->hdev)
266 hu->hdev->stat.byte_rx += count;
268 return count;
271 struct serdev_device_ops hci_serdev_client_ops = {
272 .receive_buf = hci_uart_receive_buf,
273 .write_wakeup = hci_uart_write_wakeup,
276 int hci_uart_register_device(struct hci_uart *hu,
277 const struct hci_uart_proto *p)
279 int err;
280 struct hci_dev *hdev;
282 BT_DBG("");
284 serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops);
286 err = p->open(hu);
287 if (err)
288 return err;
290 hu->proto = p;
291 set_bit(HCI_UART_PROTO_READY, &hu->flags);
293 /* Initialize and register HCI device */
294 hdev = hci_alloc_dev();
295 if (!hdev) {
296 BT_ERR("Can't allocate HCI device");
297 err = -ENOMEM;
298 goto err_alloc;
301 hu->hdev = hdev;
303 hdev->bus = HCI_UART;
304 hci_set_drvdata(hdev, hu);
306 INIT_WORK(&hu->write_work, hci_uart_write_work);
308 /* Only when vendor specific setup callback is provided, consider
309 * the manufacturer information valid. This avoids filling in the
310 * value for Ericsson when nothing is specified.
312 if (hu->proto->setup)
313 hdev->manufacturer = hu->proto->manufacturer;
315 hdev->open = hci_uart_open;
316 hdev->close = hci_uart_close;
317 hdev->flush = hci_uart_flush;
318 hdev->send = hci_uart_send_frame;
319 hdev->setup = hci_uart_setup;
320 SET_HCIDEV_DEV(hdev, &hu->serdev->dev);
322 if (test_bit(HCI_UART_RAW_DEVICE, &hu->hdev_flags))
323 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
325 if (test_bit(HCI_UART_EXT_CONFIG, &hu->hdev_flags))
326 set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
328 if (!test_bit(HCI_UART_RESET_ON_INIT, &hu->hdev_flags))
329 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
331 if (test_bit(HCI_UART_CREATE_AMP, &hu->hdev_flags))
332 hdev->dev_type = HCI_AMP;
333 else
334 hdev->dev_type = HCI_PRIMARY;
336 if (test_bit(HCI_UART_INIT_PENDING, &hu->hdev_flags))
337 return 0;
339 if (hci_register_dev(hdev) < 0) {
340 BT_ERR("Can't register HCI device");
341 err = -ENODEV;
342 goto err_register;
345 set_bit(HCI_UART_REGISTERED, &hu->flags);
347 return 0;
349 err_register:
350 hci_free_dev(hdev);
351 err_alloc:
352 clear_bit(HCI_UART_PROTO_READY, &hu->flags);
353 p->close(hu);
354 return err;
356 EXPORT_SYMBOL_GPL(hci_uart_register_device);