xref: /linux/drivers/bluetooth/bpa10x.c (revision a50eba1e778ad4da5b6f9ddbbf57dabbea59bc05)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  *  Digianswer Bluetooth USB driver
5  *
6  *  Copyright (C) 2004-2007  Marcel Holtmann <marcel@holtmann.org>
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/types.h>
14 #include <linux/sched.h>
15 #include <linux/errno.h>
16 #include <linux/skbuff.h>
17 
18 #include <linux/usb.h>
19 
20 #include <net/bluetooth/bluetooth.h>
21 #include <net/bluetooth/hci_core.h>
22 
23 #include "hci_uart.h"
24 
25 #define VERSION "0.11"
26 
27 static const struct usb_device_id bpa10x_table[] = {
28 	/* Tektronix BPA 100/105 (Digianswer) */
29 	{ USB_DEVICE(0x08fd, 0x0002) },
30 
31 	{ }	/* Terminating entry */
32 };
33 
34 MODULE_DEVICE_TABLE(usb, bpa10x_table);
35 
36 struct bpa10x_data {
37 	struct hci_dev    *hdev;
38 	struct usb_device *udev;
39 
40 	struct usb_anchor tx_anchor;
41 	struct usb_anchor rx_anchor;
42 
43 	struct sk_buff *rx_skb[2];
44 	struct hci_uart hu;
45 };
46 
47 static void bpa10x_tx_complete(struct urb *urb)
48 {
49 	struct sk_buff *skb = urb->context;
50 	struct hci_dev *hdev = (struct hci_dev *) skb->dev;
51 
52 	BT_DBG("%s urb %p status %d count %d", hdev->name,
53 					urb, urb->status, urb->actual_length);
54 
55 	if (!test_bit(HCI_RUNNING, &hdev->flags))
56 		goto done;
57 
58 	if (!urb->status)
59 		hdev->stat.byte_tx += urb->transfer_buffer_length;
60 	else
61 		hdev->stat.err_tx++;
62 
63 done:
64 	kfree(urb->setup_packet);
65 
66 	kfree_skb(skb);
67 }
68 
69 #define HCI_VENDOR_HDR_SIZE 5
70 
71 #define HCI_RECV_VENDOR \
72 	.type = HCI_VENDOR_PKT, \
73 	.hlen = HCI_VENDOR_HDR_SIZE, \
74 	.loff = 3, \
75 	.lsize = 2, \
76 	.maxlen = HCI_MAX_FRAME_SIZE
77 
78 static const struct h4_recv_pkt bpa10x_recv_pkts[] = {
79 	{ H4_RECV_ACL,     .recv = hci_recv_frame },
80 	{ H4_RECV_SCO,     .recv = hci_recv_frame },
81 	{ H4_RECV_EVENT,   .recv = hci_recv_frame },
82 	{ HCI_RECV_VENDOR, .recv = hci_recv_diag  },
83 };
84 
85 static void bpa10x_rx_complete(struct urb *urb)
86 {
87 	struct hci_dev *hdev = urb->context;
88 	struct bpa10x_data *data = hci_get_drvdata(hdev);
89 	int err;
90 
91 	BT_DBG("%s urb %p status %d count %d", hdev->name,
92 					urb, urb->status, urb->actual_length);
93 
94 	if (!test_bit(HCI_RUNNING, &hdev->flags))
95 		return;
96 
97 	if (urb->status == 0) {
98 		bool idx = usb_pipebulk(urb->pipe);
99 
100 		data->rx_skb[idx] = h4_recv_buf(&data->hu, data->rx_skb[idx],
101 						urb->transfer_buffer,
102 						urb->actual_length,
103 						bpa10x_recv_pkts,
104 						ARRAY_SIZE(bpa10x_recv_pkts));
105 		if (IS_ERR(data->rx_skb[idx])) {
106 			bt_dev_err(hdev, "corrupted event packet");
107 			hdev->stat.err_rx++;
108 			data->rx_skb[idx] = NULL;
109 		}
110 	}
111 
112 	usb_anchor_urb(urb, &data->rx_anchor);
113 
114 	err = usb_submit_urb(urb, GFP_ATOMIC);
115 	if (err < 0) {
116 		bt_dev_err(hdev, "urb %p failed to resubmit (%d)", urb, -err);
117 		usb_unanchor_urb(urb);
118 	}
119 }
120 
121 static inline int bpa10x_submit_intr_urb(struct hci_dev *hdev)
122 {
123 	struct bpa10x_data *data = hci_get_drvdata(hdev);
124 	struct urb *urb;
125 	unsigned char *buf;
126 	unsigned int pipe;
127 	int err, size = 16;
128 
129 	BT_DBG("%s", hdev->name);
130 
131 	urb = usb_alloc_urb(0, GFP_KERNEL);
132 	if (!urb)
133 		return -ENOMEM;
134 
135 	buf = kmalloc(size, GFP_KERNEL);
136 	if (!buf) {
137 		usb_free_urb(urb);
138 		return -ENOMEM;
139 	}
140 
141 	pipe = usb_rcvintpipe(data->udev, 0x81);
142 
143 	usb_fill_int_urb(urb, data->udev, pipe, buf, size,
144 						bpa10x_rx_complete, hdev, 1);
145 
146 	urb->transfer_flags |= URB_FREE_BUFFER;
147 
148 	usb_anchor_urb(urb, &data->rx_anchor);
149 
150 	err = usb_submit_urb(urb, GFP_KERNEL);
151 	if (err < 0) {
152 		bt_dev_err(hdev, "urb %p submission failed (%d)", urb, -err);
153 		usb_unanchor_urb(urb);
154 	}
155 
156 	usb_free_urb(urb);
157 
158 	return err;
159 }
160 
161 static inline int bpa10x_submit_bulk_urb(struct hci_dev *hdev)
162 {
163 	struct bpa10x_data *data = hci_get_drvdata(hdev);
164 	struct urb *urb;
165 	unsigned char *buf;
166 	unsigned int pipe;
167 	int err, size = 64;
168 
169 	BT_DBG("%s", hdev->name);
170 
171 	urb = usb_alloc_urb(0, GFP_KERNEL);
172 	if (!urb)
173 		return -ENOMEM;
174 
175 	buf = kmalloc(size, GFP_KERNEL);
176 	if (!buf) {
177 		usb_free_urb(urb);
178 		return -ENOMEM;
179 	}
180 
181 	pipe = usb_rcvbulkpipe(data->udev, 0x82);
182 
183 	usb_fill_bulk_urb(urb, data->udev, pipe,
184 					buf, size, bpa10x_rx_complete, hdev);
185 
186 	urb->transfer_flags |= URB_FREE_BUFFER;
187 
188 	usb_anchor_urb(urb, &data->rx_anchor);
189 
190 	err = usb_submit_urb(urb, GFP_KERNEL);
191 	if (err < 0) {
192 		bt_dev_err(hdev, "urb %p submission failed (%d)", urb, -err);
193 		usb_unanchor_urb(urb);
194 	}
195 
196 	usb_free_urb(urb);
197 
198 	return err;
199 }
200 
201 static int bpa10x_open(struct hci_dev *hdev)
202 {
203 	struct bpa10x_data *data = hci_get_drvdata(hdev);
204 	int err;
205 
206 	BT_DBG("%s", hdev->name);
207 
208 	err = bpa10x_submit_intr_urb(hdev);
209 	if (err < 0)
210 		goto error;
211 
212 	err = bpa10x_submit_bulk_urb(hdev);
213 	if (err < 0)
214 		goto error;
215 
216 	return 0;
217 
218 error:
219 	usb_kill_anchored_urbs(&data->rx_anchor);
220 
221 	return err;
222 }
223 
224 static int bpa10x_close(struct hci_dev *hdev)
225 {
226 	struct bpa10x_data *data = hci_get_drvdata(hdev);
227 
228 	BT_DBG("%s", hdev->name);
229 
230 	usb_kill_anchored_urbs(&data->rx_anchor);
231 
232 	return 0;
233 }
234 
235 static int bpa10x_flush(struct hci_dev *hdev)
236 {
237 	struct bpa10x_data *data = hci_get_drvdata(hdev);
238 
239 	BT_DBG("%s", hdev->name);
240 
241 	usb_kill_anchored_urbs(&data->tx_anchor);
242 
243 	return 0;
244 }
245 
246 static int bpa10x_setup(struct hci_dev *hdev)
247 {
248 	static const u8 req[] = { 0x07 };
249 	struct sk_buff *skb;
250 
251 	BT_DBG("%s", hdev->name);
252 
253 	/* Read revision string */
254 	skb = __hci_cmd_sync(hdev, 0xfc0e, sizeof(req), req, HCI_INIT_TIMEOUT);
255 	if (IS_ERR(skb))
256 		return PTR_ERR(skb);
257 
258 	/* Bounded print: the device controls skb->len. */
259 	if (skb->len > 1) {
260 		int len = skb->len - 1;
261 
262 		bt_dev_info(hdev, "%.*s", len, (char *)(skb->data + 1));
263 		hci_set_fw_info(hdev, "%.*s", len, skb->data + 1);
264 	}
265 
266 	kfree_skb(skb);
267 	return 0;
268 }
269 
270 static int bpa10x_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
271 {
272 	struct bpa10x_data *data = hci_get_drvdata(hdev);
273 	struct usb_ctrlrequest *dr;
274 	struct urb *urb;
275 	unsigned int pipe;
276 	int err;
277 
278 	BT_DBG("%s", hdev->name);
279 
280 	skb->dev = (void *) hdev;
281 
282 	urb = usb_alloc_urb(0, GFP_KERNEL);
283 	if (!urb)
284 		return -ENOMEM;
285 
286 	/* Prepend skb with frame type */
287 	*(u8 *)skb_push(skb, 1) = hci_skb_pkt_type(skb);
288 
289 	switch (hci_skb_pkt_type(skb)) {
290 	case HCI_COMMAND_PKT:
291 		dr = kmalloc_obj(*dr);
292 		if (!dr) {
293 			usb_free_urb(urb);
294 			return -ENOMEM;
295 		}
296 
297 		dr->bRequestType = USB_TYPE_VENDOR;
298 		dr->bRequest     = 0;
299 		dr->wIndex       = 0;
300 		dr->wValue       = 0;
301 		dr->wLength      = __cpu_to_le16(skb->len);
302 
303 		pipe = usb_sndctrlpipe(data->udev, 0x00);
304 
305 		usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
306 				skb->data, skb->len, bpa10x_tx_complete, skb);
307 
308 		hdev->stat.cmd_tx++;
309 		break;
310 
311 	case HCI_ACLDATA_PKT:
312 		pipe = usb_sndbulkpipe(data->udev, 0x02);
313 
314 		usb_fill_bulk_urb(urb, data->udev, pipe,
315 				skb->data, skb->len, bpa10x_tx_complete, skb);
316 
317 		hdev->stat.acl_tx++;
318 		break;
319 
320 	case HCI_SCODATA_PKT:
321 		pipe = usb_sndbulkpipe(data->udev, 0x02);
322 
323 		usb_fill_bulk_urb(urb, data->udev, pipe,
324 				skb->data, skb->len, bpa10x_tx_complete, skb);
325 
326 		hdev->stat.sco_tx++;
327 		break;
328 
329 	default:
330 		usb_free_urb(urb);
331 		return -EILSEQ;
332 	}
333 
334 	usb_anchor_urb(urb, &data->tx_anchor);
335 
336 	err = usb_submit_urb(urb, GFP_KERNEL);
337 	if (err < 0) {
338 		bt_dev_err(hdev, "urb %p submission failed", urb);
339 		kfree(urb->setup_packet);
340 		usb_unanchor_urb(urb);
341 	}
342 
343 	usb_free_urb(urb);
344 
345 	return err;
346 }
347 
348 static int bpa10x_set_diag(struct hci_dev *hdev, bool enable)
349 {
350 	const u8 req[] = { 0x00, enable };
351 	struct sk_buff *skb;
352 
353 	BT_DBG("%s", hdev->name);
354 
355 	if (!test_bit(HCI_RUNNING, &hdev->flags))
356 		return -ENETDOWN;
357 
358 	/* Enable sniffer operation */
359 	skb = __hci_cmd_sync(hdev, 0xfc0e, sizeof(req), req, HCI_INIT_TIMEOUT);
360 	if (IS_ERR(skb))
361 		return PTR_ERR(skb);
362 
363 	kfree_skb(skb);
364 	return 0;
365 }
366 
367 static int bpa10x_probe(struct usb_interface *intf,
368 			const struct usb_device_id *id)
369 {
370 	struct bpa10x_data *data;
371 	struct hci_dev *hdev;
372 	int err;
373 
374 	BT_DBG("intf %p id %p", intf, id);
375 
376 	if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
377 		return -ENODEV;
378 
379 	data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
380 	if (!data)
381 		return -ENOMEM;
382 
383 	data->udev = interface_to_usbdev(intf);
384 
385 	init_usb_anchor(&data->tx_anchor);
386 	init_usb_anchor(&data->rx_anchor);
387 
388 	hdev = hci_alloc_dev();
389 	if (!hdev)
390 		return -ENOMEM;
391 
392 	hdev->bus = HCI_USB;
393 	hci_set_drvdata(hdev, data);
394 
395 	data->hdev = hdev;
396 	data->hu.hdev = hdev;
397 
398 	SET_HCIDEV_DEV(hdev, &intf->dev);
399 
400 	hdev->open     = bpa10x_open;
401 	hdev->close    = bpa10x_close;
402 	hdev->flush    = bpa10x_flush;
403 	hdev->setup    = bpa10x_setup;
404 	hdev->send     = bpa10x_send_frame;
405 	hdev->set_diag = bpa10x_set_diag;
406 
407 	hci_set_quirk(hdev, HCI_QUIRK_RESET_ON_CLOSE);
408 
409 	err = hci_register_dev(hdev);
410 	if (err < 0) {
411 		hci_free_dev(hdev);
412 		return err;
413 	}
414 
415 	usb_set_intfdata(intf, data);
416 
417 	return 0;
418 }
419 
420 static void bpa10x_disconnect(struct usb_interface *intf)
421 {
422 	struct bpa10x_data *data = usb_get_intfdata(intf);
423 
424 	BT_DBG("intf %p", intf);
425 
426 	if (!data)
427 		return;
428 
429 	usb_set_intfdata(intf, NULL);
430 
431 	hci_unregister_dev(data->hdev);
432 
433 	hci_free_dev(data->hdev);
434 	kfree_skb(data->rx_skb[0]);
435 	kfree_skb(data->rx_skb[1]);
436 }
437 
438 static struct usb_driver bpa10x_driver = {
439 	.name		= "bpa10x",
440 	.probe		= bpa10x_probe,
441 	.disconnect	= bpa10x_disconnect,
442 	.id_table	= bpa10x_table,
443 	.disable_hub_initiated_lpm = 1,
444 };
445 
446 module_usb_driver(bpa10x_driver);
447 
448 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
449 MODULE_DESCRIPTION("Digianswer Bluetooth USB driver ver " VERSION);
450 MODULE_VERSION(VERSION);
451 MODULE_LICENSE("GPL");
452