Home | History | Annotate | Download | only in lib
      1 /*
      2  *
      3  *  BlueZ - Bluetooth protocol stack for Linux
      4  *
      5  *  Copyright (C) 2000-2001  Qualcomm Incorporated
      6  *  Copyright (C) 2002-2003  Maxim Krasnyansky <maxk (at) qualcomm.com>
      7  *  Copyright (C) 2002-2010  Marcel Holtmann <marcel (at) holtmann.org>
      8  *
      9  *
     10  *  This program is free software; you can redistribute it and/or modify
     11  *  it under the terms of the GNU General Public License as published by
     12  *  the Free Software Foundation; either version 2 of the License, or
     13  *  (at your option) any later version.
     14  *
     15  *  This program is distributed in the hope that it will be useful,
     16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18  *  GNU General Public License for more details.
     19  *
     20  *  You should have received a copy of the GNU General Public License
     21  *  along with this program; if not, write to the Free Software
     22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     23  *
     24  */
     25 
     26 #ifdef HAVE_CONFIG_H
     27 #include <config.h>
     28 #endif
     29 
     30 #include <stdio.h>
     31 #include <errno.h>
     32 #include <fcntl.h>
     33 #include <unistd.h>
     34 #include <stdlib.h>
     35 #include <string.h>
     36 
     37 #include <sys/param.h>
     38 #include <sys/uio.h>
     39 #include <sys/poll.h>
     40 #include <sys/types.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/socket.h>
     43 
     44 #include "bluetooth.h"
     45 #include "hci.h"
     46 #include "hci_lib.h"
     47 
     48 #ifndef MIN
     49 #define MIN(x, y) ((x) < (y) ? (x) : (y))
     50 #endif
     51 
     52 typedef struct {
     53 	char *str;
     54 	unsigned int val;
     55 } hci_map;
     56 
     57 static char *hci_bit2str(hci_map *m, unsigned int val)
     58 {
     59 	char *str = malloc(120);
     60 	char *ptr = str;
     61 
     62 	if (!str)
     63 		return NULL;
     64 
     65 	*ptr = 0;
     66 	while (m->str) {
     67 		if ((unsigned int) m->val & val)
     68 			ptr += sprintf(ptr, "%s ", m->str);
     69 		m++;
     70 	}
     71 	return str;
     72 }
     73 
     74 static int hci_str2bit(hci_map *map, char *str, unsigned int *val)
     75 {
     76 	char *t, *ptr;
     77 	hci_map *m;
     78 	int set;
     79 
     80 	if (!str || !(str = ptr = strdup(str)))
     81 		return 0;
     82 
     83 	*val = set = 0;
     84 
     85 	while ((t = strsep(&ptr, ","))) {
     86 		for (m = map; m->str; m++) {
     87 			if (!strcasecmp(m->str, t)) {
     88 				*val |= (unsigned int) m->val;
     89 				set = 1;
     90 			}
     91 		}
     92 	}
     93 	free(str);
     94 
     95 	return set;
     96 }
     97 
     98 static char *hci_uint2str(hci_map *m, unsigned int val)
     99 {
    100 	char *str = malloc(50);
    101 	char *ptr = str;
    102 
    103 	if (!str)
    104 		return NULL;
    105 
    106 	*ptr = 0;
    107 	while (m->str) {
    108 		if ((unsigned int) m->val == val) {
    109 			ptr += sprintf(ptr, "%s", m->str);
    110 			break;
    111 		}
    112 		m++;
    113 	}
    114 	return str;
    115 }
    116 
    117 static int hci_str2uint(hci_map *map, char *str, unsigned int *val)
    118 {
    119 	char *t, *ptr;
    120 	hci_map *m;
    121 	int set = 0;
    122 
    123 	if (!str)
    124 		return 0;
    125 
    126 	str = ptr = strdup(str);
    127 
    128 	while ((t = strsep(&ptr, ","))) {
    129 		for (m = map; m->str; m++) {
    130 			if (!strcasecmp(m->str,t)) {
    131 				*val = (unsigned int) m->val;
    132 				set = 1;
    133 				break;
    134 			}
    135 		}
    136 	}
    137 	free(str);
    138 
    139 	return set;
    140 }
    141 
    142 char *hci_bustostr(int bus)
    143 {
    144 	switch (bus) {
    145 	case HCI_VIRTUAL:
    146 		return "VIRTUAL";
    147 	case HCI_USB:
    148 		return "USB";
    149 	case HCI_PCCARD:
    150 		return "PCCARD";
    151 	case HCI_UART:
    152 		return "UART";
    153 	case HCI_RS232:
    154 		return "RS232";
    155 	case HCI_PCI:
    156 		return "PCI";
    157 	case HCI_SDIO:
    158 		return "SDIO";
    159 	default:
    160 		return "UNKNOWN";
    161 	}
    162 }
    163 
    164 char *hci_dtypetostr(int type)
    165 {
    166 	return hci_bustostr(type & 0x0f);
    167 }
    168 
    169 char *hci_typetostr(int type)
    170 {
    171 	switch (type) {
    172 	case HCI_BREDR:
    173 		return "BR/EDR";
    174 	case HCI_AMP:
    175 		return "AMP";
    176 	default:
    177 		return "UNKNOWN";
    178 	}
    179 }
    180 
    181 /* HCI dev flags mapping */
    182 static hci_map dev_flags_map[] = {
    183 	{ "UP",      HCI_UP      },
    184 	{ "INIT",    HCI_INIT    },
    185 	{ "RUNNING", HCI_RUNNING },
    186 	{ "RAW",     HCI_RAW     },
    187 	{ "PSCAN",   HCI_PSCAN   },
    188 	{ "ISCAN",   HCI_ISCAN   },
    189 	{ "INQUIRY", HCI_INQUIRY },
    190 	{ "AUTH",    HCI_AUTH    },
    191 	{ "ENCRYPT", HCI_ENCRYPT },
    192 	{ NULL }
    193 };
    194 
    195 char *hci_dflagstostr(uint32_t flags)
    196 {
    197 	char *str = bt_malloc(50);
    198 	char *ptr = str;
    199 	hci_map *m = dev_flags_map;
    200 
    201 	if (!str)
    202 		return NULL;
    203 
    204 	*ptr = 0;
    205 
    206 	if (!hci_test_bit(HCI_UP, &flags))
    207 		ptr += sprintf(ptr, "DOWN ");
    208 
    209 	while (m->str) {
    210 		if (hci_test_bit(m->val, &flags))
    211 			ptr += sprintf(ptr, "%s ", m->str);
    212 		m++;
    213 	}
    214 	return str;
    215 }
    216 
    217 /* HCI packet type mapping */
    218 static hci_map pkt_type_map[] = {
    219 	{ "DM1",   HCI_DM1  },
    220 	{ "DM3",   HCI_DM3  },
    221 	{ "DM5",   HCI_DM5  },
    222 	{ "DH1",   HCI_DH1  },
    223 	{ "DH3",   HCI_DH3  },
    224 	{ "DH5",   HCI_DH5  },
    225 	{ "HV1",   HCI_HV1  },
    226 	{ "HV2",   HCI_HV2  },
    227 	{ "HV3",   HCI_HV3  },
    228 	{ "2-DH1", HCI_2DH1 },
    229 	{ "2-DH3", HCI_2DH3 },
    230 	{ "2-DH5", HCI_2DH5 },
    231 	{ "3-DH1", HCI_3DH1 },
    232 	{ "3-DH3", HCI_3DH3 },
    233 	{ "3-DH5", HCI_3DH5 },
    234 	{ NULL }
    235 };
    236 
    237 static hci_map sco_ptype_map[] = {
    238 	{ "HV1",   0x0001   },
    239 	{ "HV2",   0x0002   },
    240 	{ "HV3",   0x0004   },
    241 	{ "EV3",   HCI_EV3  },
    242 	{ "EV4",   HCI_EV4  },
    243 	{ "EV5",   HCI_EV5  },
    244 	{ "2-EV3", HCI_2EV3 },
    245 	{ "2-EV5", HCI_2EV5 },
    246 	{ "3-EV3", HCI_3EV3 },
    247 	{ "3-EV5", HCI_3EV5 },
    248 	{ NULL }
    249 };
    250 
    251 char *hci_ptypetostr(unsigned int ptype)
    252 {
    253 	return hci_bit2str(pkt_type_map, ptype);
    254 }
    255 
    256 int hci_strtoptype(char *str, unsigned int *val)
    257 {
    258 	return hci_str2bit(pkt_type_map, str, val);
    259 }
    260 
    261 char *hci_scoptypetostr(unsigned int ptype)
    262 {
    263 	return hci_bit2str(sco_ptype_map, ptype);
    264 }
    265 
    266 int hci_strtoscoptype(char *str, unsigned int *val)
    267 {
    268 	return hci_str2bit(sco_ptype_map, str, val);
    269 }
    270 
    271 /* Link policy mapping */
    272 static hci_map link_policy_map[] = {
    273 	{ "NONE",	0		},
    274 	{ "RSWITCH",	HCI_LP_RSWITCH	},
    275 	{ "HOLD",	HCI_LP_HOLD	},
    276 	{ "SNIFF",	HCI_LP_SNIFF	},
    277 	{ "PARK",	HCI_LP_PARK	},
    278 	{ NULL }
    279 };
    280 
    281 char *hci_lptostr(unsigned int lp)
    282 {
    283 	return hci_bit2str(link_policy_map, lp);
    284 }
    285 
    286 int hci_strtolp(char *str, unsigned int *val)
    287 {
    288 	return hci_str2bit(link_policy_map, str, val);
    289 }
    290 
    291 /* Link mode mapping */
    292 static hci_map link_mode_map[] = {
    293 	{ "NONE",	0		},
    294 	{ "ACCEPT",	HCI_LM_ACCEPT	},
    295 	{ "MASTER",	HCI_LM_MASTER	},
    296 	{ "AUTH",	HCI_LM_AUTH	},
    297 	{ "ENCRYPT",	HCI_LM_ENCRYPT	},
    298 	{ "TRUSTED",	HCI_LM_TRUSTED	},
    299 	{ "RELIABLE",	HCI_LM_RELIABLE	},
    300 	{ "SECURE",	HCI_LM_SECURE	},
    301 	{ NULL }
    302 };
    303 
    304 char *hci_lmtostr(unsigned int lm)
    305 {
    306 	char *s, *str = bt_malloc(50);
    307 	if (!str)
    308 		return NULL;
    309 
    310 	*str = 0;
    311 	if (!(lm & HCI_LM_MASTER))
    312 		strcpy(str, "SLAVE ");
    313 
    314 	s = hci_bit2str(link_mode_map, lm);
    315 	if (!s) {
    316 		bt_free(str);
    317 		return NULL;
    318 	}
    319 
    320 	strcat(str, s);
    321 	free(s);
    322 	return str;
    323 }
    324 
    325 int hci_strtolm(char *str, unsigned int *val)
    326 {
    327 	return hci_str2bit(link_mode_map, str, val);
    328 }
    329 
    330 /* Command mapping */
    331 static hci_map commands_map[] = {
    332 	{ "Inquiry",					0   },
    333 	{ "Inquiry Cancel",				1   },
    334 	{ "Periodic Inquiry Mode",			2   },
    335 	{ "Exit Periodic Inquiry Mode",			3   },
    336 	{ "Create Connection",				4   },
    337 	{ "Disconnect",					5   },
    338 	{ "Add SCO Connection",				6   },
    339 	{ "Cancel Create Connection",			7   },
    340 
    341 	{ "Accept Connection Request",			8   },
    342 	{ "Reject Connection Request",			9   },
    343 	{ "Link Key Request Reply",			10  },
    344 	{ "Link Key Request Negative Reply",		11  },
    345 	{ "PIN Code Request Reply",			12  },
    346 	{ "PIN Code Request Negative Reply",		13  },
    347 	{ "Change Connection Packet Type",		14  },
    348 	{ "Authentication Requested",			15  },
    349 
    350 	{ "Set Connection Encryption",			16  },
    351 	{ "Change Connection Link Key",			17  },
    352 	{ "Master Link Key",				18  },
    353 	{ "Remote Name Request",			19  },
    354 	{ "Cancel Remote Name Request",			20  },
    355 	{ "Read Remote Supported Features",		21  },
    356 	{ "Read Remote Extended Features",		22  },
    357 	{ "Read Remote Version Information",		23  },
    358 
    359 	{ "Read Clock Offset",				24  },
    360 	{ "Read LMP Handle",				25  },
    361 	{ "Reserved",					26  },
    362 	{ "Reserved",					27  },
    363 	{ "Reserved",					28  },
    364 	{ "Reserved",					29  },
    365 	{ "Reserved",					30  },
    366 	{ "Reserved",					31  },
    367 
    368 	{ "Reserved",					32  },
    369 	{ "Hold Mode",					33  },
    370 	{ "Sniff Mode",					34  },
    371 	{ "Exit Sniff Mode",				35  },
    372 	{ "Park State",					36  },
    373 	{ "Exit Park State",				37  },
    374 	{ "QoS Setup",					38  },
    375 	{ "Role Discovery",				39  },
    376 
    377 	{ "Switch Role",				40  },
    378 	{ "Read Link Policy Settings",			41  },
    379 	{ "Write Link Policy Settings",			42  },
    380 	{ "Read Default Link Policy Settings",		43  },
    381 	{ "Write Default Link Policy Settings",		44  },
    382 	{ "Flow Specification",				45  },
    383 	{ "Set Event Mask",				46  },
    384 	{ "Reset",					47  },
    385 
    386 	{ "Set Event Filter",				48  },
    387 	{ "Flush",					49  },
    388 	{ "Read PIN Type",				50  },
    389 	{ "Write PIN Type",				51  },
    390 	{ "Create New Unit Key",			52  },
    391 	{ "Read Stored Link Key",			53  },
    392 	{ "Write Stored Link Key",			54  },
    393 	{ "Delete Stored Link Key",			55  },
    394 
    395 	{ "Write Local Name",				56  },
    396 	{ "Read Local Name",				57  },
    397 	{ "Read Connection Accept Timeout",		58  },
    398 	{ "Write Connection Accept Timeout",		59  },
    399 	{ "Read Page Timeout",				60  },
    400 	{ "Write Page Timeout",				61  },
    401 	{ "Read Scan Enable",				62  },
    402 	{ "Write Scan Enable",				63  },
    403 
    404 	{ "Read Page Scan Activity",			64  },
    405 	{ "Write Page Scan Activity",			65  },
    406 	{ "Read Inquiry Scan Activity",			66  },
    407 	{ "Write Inquiry Scan Activity",		67  },
    408 	{ "Read Authentication Enable",			68  },
    409 	{ "Write Authentication Enable",		69  },
    410 	{ "Read Encryption Mode",			70  },
    411 	{ "Write Encryption Mode",			71  },
    412 
    413 	{ "Read Class Of Device",			72  },
    414 	{ "Write Class Of Device",			73  },
    415 	{ "Read Voice Setting",				74  },
    416 	{ "Write Voice Setting",			75  },
    417 	{ "Read Automatic Flush Timeout",		76  },
    418 	{ "Write Automatic Flush Timeout",		77  },
    419 	{ "Read Num Broadcast Retransmissions",		78  },
    420 	{ "Write Num Broadcast Retransmissions",	79  },
    421 
    422 	{ "Read Hold Mode Activity",			80  },
    423 	{ "Write Hold Mode Activity",			81  },
    424 	{ "Read Transmit Power Level",			82  },
    425 	{ "Read Synchronous Flow Control Enable",	83  },
    426 	{ "Write Synchronous Flow Control Enable",	84  },
    427 	{ "Set Host Controller To Host Flow Control",	85  },
    428 	{ "Host Buffer Size",				86  },
    429 	{ "Host Number Of Completed Packets",		87  },
    430 
    431 	{ "Read Link Supervision Timeout",		88  },
    432 	{ "Write Link Supervision Timeout",		89  },
    433 	{ "Read Number of Supported IAC",		90  },
    434 	{ "Read Current IAC LAP",			91  },
    435 	{ "Write Current IAC LAP",			92  },
    436 	{ "Read Page Scan Period Mode",			93  },
    437 	{ "Write Page Scan Period Mode",		94  },
    438 	{ "Read Page Scan Mode",			95  },
    439 
    440 	{ "Write Page Scan Mode",			96  },
    441 	{ "Set AFH Channel Classification",		97  },
    442 	{ "Reserved",					98  },
    443 	{ "Reserved",					99  },
    444 	{ "Read Inquiry Scan Type",			100 },
    445 	{ "Write Inquiry Scan Type",			101 },
    446 	{ "Read Inquiry Mode",				102 },
    447 	{ "Write Inquiry Mode",				103 },
    448 
    449 	{ "Read Page Scan Type",			104 },
    450 	{ "Write Page Scan Type",			105 },
    451 	{ "Read AFH Channel Assessment Mode",		106 },
    452 	{ "Write AFH Channel Assessment Mode",		107 },
    453 	{ "Reserved",					108 },
    454 	{ "Reserved",					109 },
    455 	{ "Reserved",					110 },
    456 	{ "Reserved",					111 },
    457 
    458 	{ "Reserved",					112 },
    459 	{ "Reserved",					113 },
    460 	{ "Reserved",					114 },
    461 	{ "Read Local Version Information",		115 },
    462 	{ "Read Local Supported Commands",		116 },
    463 	{ "Read Local Supported Features",		117 },
    464 	{ "Read Local Extended Features",		118 },
    465 	{ "Read Buffer Size",				119 },
    466 
    467 	{ "Read Country Code",				120 },
    468 	{ "Read BD ADDR",				121 },
    469 	{ "Read Failed Contact Counter",		122 },
    470 	{ "Reset Failed Contact Counter",		123 },
    471 	{ "Get Link Quality",				124 },
    472 	{ "Read RSSI",					125 },
    473 	{ "Read AFH Channel Map",			126 },
    474 	{ "Read BD Clock",				127 },
    475 
    476 	{ "Read Loopback Mode",				128 },
    477 	{ "Write Loopback Mode",			129 },
    478 	{ "Enable Device Under Test Mode",		130 },
    479 	{ "Setup Synchronous Connection",		131 },
    480 	{ "Accept Synchronous Connection",		132 },
    481 	{ "Reject Synchronous Connection",		133 },
    482 	{ "Reserved",					134 },
    483 	{ "Reserved",					135 },
    484 
    485 	{ "Read Extended Inquiry Response",		136 },
    486 	{ "Write Extended Inquiry Response",		137 },
    487 	{ "Refresh Encryption Key",			138 },
    488 	{ "Reserved",					139 },
    489 	{ "Sniff Subrating",				140 },
    490 	{ "Read Simple Pairing Mode",			141 },
    491 	{ "Write Simple Pairing Mode",			142 },
    492 	{ "Read Local OOB Data",			143 },
    493 
    494 	{ "Read Inquiry Response Transmit Power Level",	144 },
    495 	{ "Write Inquiry Transmit Power Level",		145 },
    496 	{ "Read Default Erroneous Data Reporting",	146 },
    497 	{ "Write Default Erroneous Data Reporting",	147 },
    498 	{ "Reserved",					148 },
    499 	{ "Reserved",					149 },
    500 	{ "Reserved",					150 },
    501 	{ "IO Capability Request Reply",		151 },
    502 
    503 	{ "User Confirmation Request Reply",		152 },
    504 	{ "User Confirmation Request Negative Reply",	153 },
    505 	{ "User Passkey Request Reply",			154 },
    506 	{ "User Passkey Request Negative Reply",	155 },
    507 	{ "Remote OOB Data Request Reply",		156 },
    508 	{ "Write Simple Pairing Debug Mode",		157 },
    509 	{ "Enhanced Flush",				158 },
    510 	{ "Remote OOB Data Request Negative Reply",	159 },
    511 
    512 	{ "Reserved",					160 },
    513 	{ "Reserved",					161 },
    514 	{ "Send Keypress Notification",			162 },
    515 	{ "IO Capability Request Negative Reply",	163 },
    516 	{ "Read Encryption Key Size",			164 },
    517 	{ "Reserved",					165 },
    518 	{ "Reserved",					166 },
    519 	{ "Reserved",					167 },
    520 
    521 	{ "Create Physical Link",			168 },
    522 	{ "Accept Physical Link",			169 },
    523 	{ "Disconnect Physical Link",			170 },
    524 	{ "Create Logical Link",			171 },
    525 	{ "Accept Logical Link",			172 },
    526 	{ "Disconnect Logical Link",			173 },
    527 	{ "Logical Link Cancel",			174 },
    528 	{ "Flow Specification Modify",			175 },
    529 
    530 	{ "Read Logical Link Accept Timeout",		176 },
    531 	{ "Write Logical Link Accept Timeout",		177 },
    532 	{ "Set Event Mask Page 2",			178 },
    533 	{ "Read Location Data",				179 },
    534 	{ "Write Location Data",			180 },
    535 	{ "Read Local AMP Info",			181 },
    536 	{ "Read Local AMP_ASSOC",			182 },
    537 	{ "Write Remote AMP_ASSOC",			183 },
    538 
    539 	{ "Read Flow Control Mode",			184 },
    540 	{ "Write Flow Control Mode",			185 },
    541 	{ "Read Data Block Size",			186 },
    542 	{ "Reserved",					187 },
    543 	{ "Reserved",					188 },
    544 	{ "Enable AMP Receiver Reports",		189 },
    545 	{ "AMP Test End",				190 },
    546 	{ "AMP Test Command",				191 },
    547 
    548 	{ "Read Enhanced Transmit Power Level",		192 },
    549 	{ "Reserved",					193 },
    550 	{ "Read Best Effort Flush Timeout",		194 },
    551 	{ "Write Best Effort Flush Timeout",		195 },
    552 	{ "Short Range Mode",				196 },
    553 	{ "Read LE Host Support",			197 },
    554 	{ "Write LE Host Support",			198 },
    555 	{ "Reserved",					199 },
    556 
    557 	{ "LE Set Event Mask",				200 },
    558 	{ "LE Read Buffer Size",			201 },
    559 	{ "LE Read Local Supported Features",		202 },
    560 	{ "Reserved",					203 },
    561 	{ "LE Set Random Address",			204 },
    562 	{ "LE Set Advertising Parameters",		205 },
    563 	{ "LE Read Advertising Channel TX Power",	206 },
    564 	{ "LE Set Advertising Data",			207 },
    565 
    566 	{ "LE Set Scan Response Data",			208 },
    567 	{ "LE Set Advertise Enable",			209 },
    568 	{ "LE Set Scan Parameters",			210 },
    569 	{ "LE Set Scan Enable",				211 },
    570 	{ "LE Create Connection",			212 },
    571 	{ "LE Create Connection Cancel",		213 },
    572 	{ "LE Read White List Size",			214 },
    573 	{ "LE Clear White List",			215 },
    574 
    575 	{ "LE Add Device To White List",		216 },
    576 	{ "LE Remove Device From White List",		217 },
    577 	{ "LE Connection Update",			218 },
    578 	{ "LE Set Host Channel Classification",		219 },
    579 	{ "LE Read Channel Map",			220 },
    580 	{ "LE Read Remote Used Features",		221 },
    581 	{ "LE Encrypt",					222 },
    582 	{ "LE Rand",					223 },
    583 
    584 	{ "LE Start Encryption",			224 },
    585 	{ "LE Long Term Key Request Reply",		225 },
    586 	{ "LE Long Term Key Request Negative Reply",	226 },
    587 	{ "LE Read Supported States",			227 },
    588 	{ "LE Receiver Test",				228 },
    589 	{ "LE Transmitter Test",			229 },
    590 	{ "LE Test End",				230 },
    591 	{ "Reserved",					231 },
    592 
    593 	{ NULL }
    594 };
    595 
    596 char *hci_cmdtostr(unsigned int cmd)
    597 {
    598 	return hci_uint2str(commands_map, cmd);
    599 }
    600 
    601 char *hci_commandstostr(uint8_t *commands, char *pref, int width)
    602 {
    603 	unsigned int maxwidth = width - 3;
    604 	hci_map *m;
    605 	char *off, *ptr, *str;
    606 	int size = 10;
    607 
    608 	m = commands_map;
    609 
    610 	while (m->str) {
    611 		if (commands[m->val / 8] & (1 << (m->val % 8)))
    612 			size += strlen(m->str) + (pref ? strlen(pref) : 0) + 3;
    613 		m++;
    614 	}
    615 
    616 	str = bt_malloc(size);
    617 	if (!str)
    618 		return NULL;
    619 
    620 	ptr = str; *ptr = '\0';
    621 
    622 	if (pref)
    623 		ptr += sprintf(ptr, "%s", pref);
    624 
    625 	off = ptr;
    626 
    627 	m = commands_map;
    628 
    629 	while (m->str) {
    630 		if (commands[m->val / 8] & (1 << (m->val % 8))) {
    631 			if (strlen(off) + strlen(m->str) > maxwidth) {
    632 				ptr += sprintf(ptr, "\n%s", pref ? pref : "");
    633 				off = ptr;
    634 			}
    635 			ptr += sprintf(ptr, "'%s' ", m->str);
    636 		}
    637 		m++;
    638 	}
    639 
    640 	return str;
    641 }
    642 
    643 /* Version mapping */
    644 static hci_map ver_map[] = {
    645 	{ "1.0b",	0x00 },
    646 	{ "1.1",	0x01 },
    647 	{ "1.2",	0x02 },
    648 	{ "2.0",	0x03 },
    649 	{ "2.1",	0x04 },
    650 	{ "3.0",	0x05 },
    651 	{ "4.0",	0x06 },
    652 	{ NULL }
    653 };
    654 
    655 char *hci_vertostr(unsigned int ver)
    656 {
    657 	return hci_uint2str(ver_map, ver);
    658 }
    659 
    660 int hci_strtover(char *str, unsigned int *ver)
    661 {
    662 	return hci_str2uint(ver_map, str, ver);
    663 }
    664 
    665 char *lmp_vertostr(unsigned int ver)
    666 {
    667 	return hci_uint2str(ver_map, ver);
    668 }
    669 
    670 int lmp_strtover(char *str, unsigned int *ver)
    671 {
    672 	return hci_str2uint(ver_map, str, ver);
    673 }
    674 
    675 /* LMP features mapping */
    676 static hci_map lmp_features_map[8][9] = {
    677 	{	/* Byte 0 */
    678 		{ "<3-slot packets>",	LMP_3SLOT	},	/* Bit 0 */
    679 		{ "<5-slot packets>",	LMP_5SLOT	},	/* Bit 1 */
    680 		{ "<encryption>",	LMP_ENCRYPT	},	/* Bit 2 */
    681 		{ "<slot offset>",	LMP_SOFFSET	},	/* Bit 3 */
    682 		{ "<timing accuracy>",	LMP_TACCURACY	},	/* Bit 4 */
    683 		{ "<role switch>",	LMP_RSWITCH	},	/* Bit 5 */
    684 		{ "<hold mode>",	LMP_HOLD	},	/* Bit 6 */
    685 		{ "<sniff mode>",	LMP_SNIFF	},	/* Bit 7 */
    686 		{ NULL }
    687 	},
    688 	{	/* Byte 1 */
    689 		{ "<park state>",	LMP_PARK	},	/* Bit 0 */
    690 		{ "<RSSI>",		LMP_RSSI	},	/* Bit 1 */
    691 		{ "<channel quality>",	LMP_QUALITY	},	/* Bit 2 */
    692 		{ "<SCO link>",		LMP_SCO		},	/* Bit 3 */
    693 		{ "<HV2 packets>",	LMP_HV2		},	/* Bit 4 */
    694 		{ "<HV3 packets>",	LMP_HV3		},	/* Bit 5 */
    695 		{ "<u-law log>",	LMP_ULAW	},	/* Bit 6 */
    696 		{ "<A-law log>",	LMP_ALAW	},	/* Bit 7 */
    697 		{ NULL }
    698 	},
    699 	{	/* Byte 2 */
    700 		{ "<CVSD>",		LMP_CVSD	},	/* Bit 0 */
    701 		{ "<paging scheme>",	LMP_PSCHEME	},	/* Bit 1 */
    702 		{ "<power control>",	LMP_PCONTROL	},	/* Bit 2 */
    703 		{ "<transparent SCO>",	LMP_TRSP_SCO	},	/* Bit 3 */
    704 		{ "<broadcast encrypt>",LMP_BCAST_ENC	},	/* Bit 7 */
    705 		{ NULL }
    706 	},
    707 	{	/* Byte 3 */
    708 		{ "<no. 24>",		0x01		},	/* Bit 0 */
    709 		{ "<EDR ACL 2 Mbps>",	LMP_EDR_ACL_2M	},	/* Bit 1 */
    710 		{ "<EDR ACL 3 Mbps>",	LMP_EDR_ACL_3M	},	/* Bit 2 */
    711 		{ "<enhanced iscan>",	LMP_ENH_ISCAN	},	/* Bit 3 */
    712 		{ "<interlaced iscan>",	LMP_ILACE_ISCAN	},	/* Bit 4 */
    713 		{ "<interlaced pscan>",	LMP_ILACE_PSCAN	},	/* Bit 5 */
    714 		{ "<inquiry with RSSI>",LMP_RSSI_INQ	},	/* Bit 6 */
    715 		{ "<extended SCO>",	LMP_ESCO	},	/* Bit 7 */
    716 		{ NULL }
    717 	},
    718 	{	/* Byte 4 */
    719 		{ "<EV4 packets>",	LMP_EV4		},	/* Bit 0 */
    720 		{ "<EV5 packets>",	LMP_EV5		},	/* Bit 1 */
    721 		{ "<no. 34>",		0x04		},	/* Bit 2 */
    722 		{ "<AFH cap. slave>",	LMP_AFH_CAP_SLV	},	/* Bit 3 */
    723 		{ "<AFH class. slave>",	LMP_AFH_CLS_SLV	},	/* Bit 4 */
    724 		{ "<BR/EDR not supp.>",	LMP_NO_BREDR	},	/* Bit 5 */
    725 		{ "<LE support>",	LMP_LE		},	/* Bit 6 */
    726 		{ "<3-slot EDR ACL>",	LMP_EDR_3SLOT	},	/* Bit 7 */
    727 		{ NULL }
    728 	},
    729 	{	/* Byte 5 */
    730 		{ "<5-slot EDR ACL>",	LMP_EDR_5SLOT	},	/* Bit 0 */
    731 		{ "<sniff subrating>",	LMP_SNIFF_SUBR	},	/* Bit 1 */
    732 		{ "<pause encryption>",	LMP_PAUSE_ENC	},	/* Bit 2 */
    733 		{ "<AFH cap. master>",	LMP_AFH_CAP_MST	},	/* Bit 3 */
    734 		{ "<AFH class. master>",LMP_AFH_CLS_MST	},	/* Bit 4 */
    735 		{ "<EDR eSCO 2 Mbps>",	LMP_EDR_ESCO_2M	},	/* Bit 5 */
    736 		{ "<EDR eSCO 3 Mbps>",	LMP_EDR_ESCO_3M	},	/* Bit 6 */
    737 		{ "<3-slot EDR eSCO>",	LMP_EDR_3S_ESCO	},	/* Bit 7 */
    738 		{ NULL }
    739 	},
    740 	{	/* Byte 6 */
    741 		{ "<extended inquiry>",	LMP_EXT_INQ	},	/* Bit 0 */
    742 		{ "<LE and BR/EDR>",	LMP_LE_BREDR	},	/* Bit 1 */
    743 		{ "<no. 50>",		0x04		},	/* Bit 2 */
    744 		{ "<simple pairing>",	LMP_SIMPLE_PAIR	},	/* Bit 3 */
    745 		{ "<encapsulated PDU>",	LMP_ENCAPS_PDU	},	/* Bit 4 */
    746 		{ "<err. data report>",	LMP_ERR_DAT_REP	},	/* Bit 5 */
    747 		{ "<non-flush flag>",	LMP_NFLUSH_PKTS	},	/* Bit 6 */
    748 		{ "<no. 55>",		0x80		},	/* Bit 7 */
    749 		{ NULL }
    750 	},
    751 	{	/* Byte 7 */
    752 		{ "<LSTO>",		LMP_LSTO	},	/* Bit 1 */
    753 		{ "<inquiry TX power>",	LMP_INQ_TX_PWR	},	/* Bit 1 */
    754 		{ "<EPC>",		LMP_EPC		},	/* Bit 2 */
    755 		{ "<no. 59>",		0x08		},	/* Bit 3 */
    756 		{ "<no. 60>",		0x10		},	/* Bit 4 */
    757 		{ "<no. 61>",		0x20		},	/* Bit 5 */
    758 		{ "<no. 62>",		0x40		},	/* Bit 6 */
    759 		{ "<extended features>",LMP_EXT_FEAT	},	/* Bit 7 */
    760 		{ NULL }
    761 	},
    762 };
    763 
    764 char *lmp_featurestostr(uint8_t *features, char *pref, int width)
    765 {
    766 	unsigned int maxwidth = width - 1;
    767 	char *off, *ptr, *str;
    768 	int i, size = 10;
    769 
    770 	for (i = 0; i < 8; i++) {
    771 		hci_map *m = lmp_features_map[i];
    772 
    773 		while (m->str) {
    774 			if (m->val & features[i])
    775 				size += strlen(m->str) +
    776 						(pref ? strlen(pref) : 0) + 1;
    777 			m++;
    778 		}
    779 	}
    780 
    781 	str = bt_malloc(size);
    782 	if (!str)
    783 		return NULL;
    784 
    785 	ptr = str; *ptr = '\0';
    786 
    787 	if (pref)
    788 		ptr += sprintf(ptr, "%s", pref);
    789 
    790 	off = ptr;
    791 
    792 	for (i = 0; i < 8; i++) {
    793 		hci_map *m = lmp_features_map[i];
    794 
    795 		while (m->str) {
    796 			if (m->val & features[i]) {
    797 				if (strlen(off) + strlen(m->str) > maxwidth) {
    798 					ptr += sprintf(ptr, "\n%s",
    799 							pref ? pref : "");
    800 					off = ptr;
    801 				}
    802 				ptr += sprintf(ptr, "%s ", m->str);
    803 			}
    804 			m++;
    805 		}
    806 	}
    807 
    808 	return str;
    809 }
    810 
    811 /* HCI functions that do not require open device */
    812 int hci_for_each_dev(int flag, int (*func)(int dd, int dev_id, long arg),
    813 			long arg)
    814 {
    815 	struct hci_dev_list_req *dl;
    816 	struct hci_dev_req *dr;
    817 	int dev_id = -1;
    818 	int i, sk, err = 0;
    819 
    820 	sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
    821 	if (sk < 0)
    822 		return -1;
    823 
    824 	dl = malloc(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl));
    825 	if (!dl) {
    826 		err = errno;
    827 		goto done;
    828 	}
    829 
    830 	memset(dl, 0, HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl));
    831 
    832 	dl->dev_num = HCI_MAX_DEV;
    833 	dr = dl->dev_req;
    834 
    835 	if (ioctl(sk, HCIGETDEVLIST, (void *) dl) < 0) {
    836 		err = errno;
    837 		goto free;
    838 	}
    839 
    840 	for (i = 0; i < dl->dev_num; i++, dr++) {
    841 		if (hci_test_bit(flag, &dr->dev_opt))
    842 			if (!func || func(sk, dr->dev_id, arg)) {
    843 				dev_id = dr->dev_id;
    844 				break;
    845 			}
    846 	}
    847 
    848 	if (dev_id < 0)
    849 		err = ENODEV;
    850 
    851 free:
    852 	free(dl);
    853 
    854 done:
    855 	close(sk);
    856 	errno = err;
    857 
    858 	return dev_id;
    859 }
    860 
    861 static int __other_bdaddr(int dd, int dev_id, long arg)
    862 {
    863 	struct hci_dev_info di = { dev_id: dev_id };
    864 
    865 	if (ioctl(dd, HCIGETDEVINFO, (void *) &di))
    866 		return 0;
    867 
    868 	if (hci_test_bit(HCI_RAW, &di.flags))
    869 		return 0;
    870 
    871 	return bacmp((bdaddr_t *) arg, &di.bdaddr);
    872 }
    873 
    874 static int __same_bdaddr(int dd, int dev_id, long arg)
    875 {
    876 	struct hci_dev_info di = { dev_id: dev_id };
    877 
    878 	if (ioctl(dd, HCIGETDEVINFO, (void *) &di))
    879 		return 0;
    880 
    881 	return !bacmp((bdaddr_t *) arg, &di.bdaddr);
    882 }
    883 
    884 int hci_get_route(bdaddr_t *bdaddr)
    885 {
    886 	return hci_for_each_dev(HCI_UP, __other_bdaddr,
    887 				(long) (bdaddr ? bdaddr : BDADDR_ANY));
    888 }
    889 
    890 int hci_devid(const char *str)
    891 {
    892 	bdaddr_t ba;
    893 	int id = -1;
    894 
    895 	if (!strncmp(str, "hci", 3) && strlen(str) >= 4) {
    896 		id = atoi(str + 3);
    897 		if (hci_devba(id, &ba) < 0)
    898 			return -1;
    899 	} else {
    900 		errno = ENODEV;
    901 		str2ba(str, &ba);
    902 		id = hci_for_each_dev(HCI_UP, __same_bdaddr, (long) &ba);
    903 	}
    904 
    905 	return id;
    906 }
    907 
    908 int hci_devinfo(int dev_id, struct hci_dev_info *di)
    909 {
    910 	int dd, err, ret;
    911 
    912 	dd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
    913 	if (dd < 0)
    914 		return dd;
    915 
    916 	memset(di, 0, sizeof(struct hci_dev_info));
    917 
    918 	di->dev_id = dev_id;
    919 	ret = ioctl(dd, HCIGETDEVINFO, (void *) di);
    920 
    921 	err = errno;
    922 	close(dd);
    923 	errno = err;
    924 
    925 	return ret;
    926 }
    927 
    928 int hci_devba(int dev_id, bdaddr_t *bdaddr)
    929 {
    930 	struct hci_dev_info di;
    931 
    932 	memset(&di, 0, sizeof(di));
    933 
    934 	if (hci_devinfo(dev_id, &di))
    935 		return -1;
    936 
    937 	if (!hci_test_bit(HCI_UP, &di.flags)) {
    938 		errno = ENETDOWN;
    939 		return -1;
    940 	}
    941 
    942 	bacpy(bdaddr, &di.bdaddr);
    943 
    944 	return 0;
    945 }
    946 
    947 int hci_inquiry(int dev_id, int len, int nrsp, const uint8_t *lap,
    948 		inquiry_info **ii, long flags)
    949 {
    950 	struct hci_inquiry_req *ir;
    951 	uint8_t num_rsp = nrsp;
    952 	void *buf;
    953 	int dd, size, err, ret = -1;
    954 
    955 	if (nrsp <= 0) {
    956 		num_rsp = 0;
    957 		nrsp = 255;
    958 	}
    959 
    960 	if (dev_id < 0) {
    961 		dev_id = hci_get_route(NULL);
    962 		if (dev_id < 0) {
    963 			errno = ENODEV;
    964 			return -1;
    965 		}
    966 	}
    967 
    968 	dd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
    969 	if (dd < 0)
    970 		return dd;
    971 
    972 	buf = malloc(sizeof(*ir) + (sizeof(inquiry_info) * (nrsp)));
    973 	if (!buf)
    974 		goto done;
    975 
    976 	ir = buf;
    977 	ir->dev_id  = dev_id;
    978 	ir->num_rsp = num_rsp;
    979 	ir->length  = len;
    980 	ir->flags   = flags;
    981 
    982 	if (lap) {
    983 		memcpy(ir->lap, lap, 3);
    984 	} else {
    985 		ir->lap[0] = 0x33;
    986 		ir->lap[1] = 0x8b;
    987 		ir->lap[2] = 0x9e;
    988 	}
    989 
    990 	ret = ioctl(dd, HCIINQUIRY, (unsigned long) buf);
    991 	if (ret < 0)
    992 		goto free;
    993 
    994 	size = sizeof(inquiry_info) * ir->num_rsp;
    995 
    996 	if (!*ii)
    997 		*ii = malloc(size);
    998 
    999 	if (*ii) {
   1000 		memcpy((void *) *ii, buf + sizeof(*ir), size);
   1001 		ret = ir->num_rsp;
   1002 	} else
   1003 		ret = -1;
   1004 
   1005 free:
   1006 	free(buf);
   1007 
   1008 done:
   1009 	err = errno;
   1010 	close(dd);
   1011 	errno = err;
   1012 
   1013 	return ret;
   1014 }
   1015 
   1016 /* Open HCI device.
   1017  * Returns device descriptor (dd). */
   1018 int hci_open_dev(int dev_id)
   1019 {
   1020 	struct sockaddr_hci a;
   1021 	int dd, err;
   1022 
   1023 	/* Create HCI socket */
   1024 	dd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
   1025 	if (dd < 0)
   1026 		return dd;
   1027 
   1028 	/* Bind socket to the HCI device */
   1029 	memset(&a, 0, sizeof(a));
   1030 	a.hci_family = AF_BLUETOOTH;
   1031 	a.hci_dev = dev_id;
   1032 	if (bind(dd, (struct sockaddr *) &a, sizeof(a)) < 0)
   1033 		goto failed;
   1034 
   1035 	return dd;
   1036 
   1037 failed:
   1038 	err = errno;
   1039 	close(dd);
   1040 	errno = err;
   1041 
   1042 	return -1;
   1043 }
   1044 
   1045 int hci_close_dev(int dd)
   1046 {
   1047 	return close(dd);
   1048 }
   1049 
   1050 /* HCI functions that require open device
   1051  * dd - Device descriptor returned by hci_open_dev. */
   1052 
   1053 int hci_send_cmd(int dd, uint16_t ogf, uint16_t ocf, uint8_t plen, void *param)
   1054 {
   1055 	uint8_t type = HCI_COMMAND_PKT;
   1056 	hci_command_hdr hc;
   1057 	struct iovec iv[3];
   1058 	int ivn;
   1059 
   1060 	hc.opcode = htobs(cmd_opcode_pack(ogf, ocf));
   1061 	hc.plen= plen;
   1062 
   1063 	iv[0].iov_base = &type;
   1064 	iv[0].iov_len  = 1;
   1065 	iv[1].iov_base = &hc;
   1066 	iv[1].iov_len  = HCI_COMMAND_HDR_SIZE;
   1067 	ivn = 2;
   1068 
   1069 	if (plen) {
   1070 		iv[2].iov_base = param;
   1071 		iv[2].iov_len  = plen;
   1072 		ivn = 3;
   1073 	}
   1074 
   1075 	while (writev(dd, iv, ivn) < 0) {
   1076 		if (errno == EAGAIN || errno == EINTR)
   1077 			continue;
   1078 		return -1;
   1079 	}
   1080 	return 0;
   1081 }
   1082 
   1083 int hci_send_req(int dd, struct hci_request *r, int to)
   1084 {
   1085 	unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr;
   1086 	uint16_t opcode = htobs(cmd_opcode_pack(r->ogf, r->ocf));
   1087 	struct hci_filter nf, of;
   1088 	socklen_t olen;
   1089 	hci_event_hdr *hdr;
   1090 	int err, try;
   1091 
   1092 	olen = sizeof(of);
   1093 	if (getsockopt(dd, SOL_HCI, HCI_FILTER, &of, &olen) < 0)
   1094 		return -1;
   1095 
   1096 	hci_filter_clear(&nf);
   1097 	hci_filter_set_ptype(HCI_EVENT_PKT,  &nf);
   1098 	hci_filter_set_event(EVT_CMD_STATUS, &nf);
   1099 	hci_filter_set_event(EVT_CMD_COMPLETE, &nf);
   1100 	hci_filter_set_event(EVT_LE_META_EVENT, &nf);
   1101 	hci_filter_set_event(r->event, &nf);
   1102 	hci_filter_set_opcode(opcode, &nf);
   1103 	if (setsockopt(dd, SOL_HCI, HCI_FILTER, &nf, sizeof(nf)) < 0)
   1104 		return -1;
   1105 
   1106 	if (hci_send_cmd(dd, r->ogf, r->ocf, r->clen, r->cparam) < 0)
   1107 		goto failed;
   1108 
   1109 	try = 10;
   1110 	while (try--) {
   1111 		evt_cmd_complete *cc;
   1112 		evt_cmd_status *cs;
   1113 		evt_remote_name_req_complete *rn;
   1114 		evt_le_meta_event *me;
   1115 		remote_name_req_cp *cp;
   1116 		int len;
   1117 
   1118 		if (to) {
   1119 			struct pollfd p;
   1120 			int n;
   1121 
   1122 			p.fd = dd; p.events = POLLIN;
   1123 			while ((n = poll(&p, 1, to)) < 0) {
   1124 				if (errno == EAGAIN || errno == EINTR)
   1125 					continue;
   1126 				goto failed;
   1127 			}
   1128 
   1129 			if (!n) {
   1130 				errno = ETIMEDOUT;
   1131 				goto failed;
   1132 			}
   1133 
   1134 			to -= 10;
   1135 			if (to < 0)
   1136 				to = 0;
   1137 
   1138 		}
   1139 
   1140 		while ((len = read(dd, buf, sizeof(buf))) < 0) {
   1141 			if (errno == EAGAIN || errno == EINTR)
   1142 				continue;
   1143 			goto failed;
   1144 		}
   1145 
   1146 		hdr = (void *) (buf + 1);
   1147 		ptr = buf + (1 + HCI_EVENT_HDR_SIZE);
   1148 		len -= (1 + HCI_EVENT_HDR_SIZE);
   1149 
   1150 		switch (hdr->evt) {
   1151 		case EVT_CMD_STATUS:
   1152 			cs = (void *) ptr;
   1153 
   1154 			if (cs->opcode != opcode)
   1155 				continue;
   1156 
   1157 			if (r->event != EVT_CMD_STATUS) {
   1158 				if (cs->status) {
   1159 					errno = EIO;
   1160 					goto failed;
   1161 				}
   1162 				break;
   1163 			}
   1164 
   1165 			r->rlen = MIN(len, r->rlen);
   1166 			memcpy(r->rparam, ptr, r->rlen);
   1167 			goto done;
   1168 
   1169 		case EVT_CMD_COMPLETE:
   1170 			cc = (void *) ptr;
   1171 
   1172 			if (cc->opcode != opcode)
   1173 				continue;
   1174 
   1175 			ptr += EVT_CMD_COMPLETE_SIZE;
   1176 			len -= EVT_CMD_COMPLETE_SIZE;
   1177 
   1178 			r->rlen = MIN(len, r->rlen);
   1179 			memcpy(r->rparam, ptr, r->rlen);
   1180 			goto done;
   1181 
   1182 		case EVT_REMOTE_NAME_REQ_COMPLETE:
   1183 			if (hdr->evt != r->event)
   1184 				break;
   1185 
   1186 			rn = (void *) ptr;
   1187 			cp = r->cparam;
   1188 
   1189 			if (bacmp(&rn->bdaddr, &cp->bdaddr))
   1190 				continue;
   1191 
   1192 			r->rlen = MIN(len, r->rlen);
   1193 			memcpy(r->rparam, ptr, r->rlen);
   1194 			goto done;
   1195 
   1196 		case EVT_LE_META_EVENT:
   1197 			me = (void *) ptr;
   1198 
   1199 			if (me->subevent != r->event)
   1200 				continue;
   1201 
   1202 			len -= 1;
   1203 			r->rlen = MIN(len, r->rlen);
   1204 			memcpy(r->rparam, me->data, r->rlen);
   1205 			goto done;
   1206 
   1207 		default:
   1208 			if (hdr->evt != r->event)
   1209 				break;
   1210 
   1211 			r->rlen = MIN(len, r->rlen);
   1212 			memcpy(r->rparam, ptr, r->rlen);
   1213 			goto done;
   1214 		}
   1215 	}
   1216 	errno = ETIMEDOUT;
   1217 
   1218 failed:
   1219 	err = errno;
   1220 	setsockopt(dd, SOL_HCI, HCI_FILTER, &of, sizeof(of));
   1221 	errno = err;
   1222 	return -1;
   1223 
   1224 done:
   1225 	setsockopt(dd, SOL_HCI, HCI_FILTER, &of, sizeof(of));
   1226 	return 0;
   1227 }
   1228 
   1229 int hci_create_connection(int dd, const bdaddr_t *bdaddr, uint16_t ptype,
   1230 				uint16_t clkoffset, uint8_t rswitch,
   1231 				uint16_t *handle, int to)
   1232 {
   1233 	evt_conn_complete rp;
   1234 	create_conn_cp cp;
   1235 	struct hci_request rq;
   1236 
   1237 	memset(&cp, 0, sizeof(cp));
   1238 	bacpy(&cp.bdaddr, bdaddr);
   1239 	cp.pkt_type       = ptype;
   1240 	cp.pscan_rep_mode = 0x02;
   1241 	cp.clock_offset   = clkoffset;
   1242 	cp.role_switch    = rswitch;
   1243 
   1244 	memset(&rq, 0, sizeof(rq));
   1245 	rq.ogf    = OGF_LINK_CTL;
   1246 	rq.ocf    = OCF_CREATE_CONN;
   1247 	rq.event  = EVT_CONN_COMPLETE;
   1248 	rq.cparam = &cp;
   1249 	rq.clen   = CREATE_CONN_CP_SIZE;
   1250 	rq.rparam = &rp;
   1251 	rq.rlen   = EVT_CONN_COMPLETE_SIZE;
   1252 
   1253 	if (hci_send_req(dd, &rq, to) < 0)
   1254 		return -1;
   1255 
   1256 	if (rp.status) {
   1257 		errno = EIO;
   1258 		return -1;
   1259 	}
   1260 
   1261 	*handle = rp.handle;
   1262 	return 0;
   1263 }
   1264 
   1265 int hci_disconnect(int dd, uint16_t handle, uint8_t reason, int to)
   1266 {
   1267 	evt_disconn_complete rp;
   1268 	disconnect_cp cp;
   1269 	struct hci_request rq;
   1270 
   1271 	memset(&cp, 0, sizeof(cp));
   1272 	cp.handle = handle;
   1273 	cp.reason = reason;
   1274 
   1275 	memset(&rq, 0, sizeof(rq));
   1276 	rq.ogf    = OGF_LINK_CTL;
   1277 	rq.ocf    = OCF_DISCONNECT;
   1278 	rq.event  = EVT_DISCONN_COMPLETE;
   1279 	rq.cparam = &cp;
   1280 	rq.clen   = DISCONNECT_CP_SIZE;
   1281 	rq.rparam = &rp;
   1282 	rq.rlen   = EVT_DISCONN_COMPLETE_SIZE;
   1283 
   1284 	if (hci_send_req(dd, &rq, to) < 0)
   1285 		return -1;
   1286 
   1287 	if (rp.status) {
   1288 		errno = EIO;
   1289 		return -1;
   1290 	}
   1291 	return 0;
   1292 }
   1293 
   1294 int hci_le_add_white_list(int dd, const bdaddr_t *bdaddr, uint8_t type, int to)
   1295 {
   1296 	struct hci_request rq;
   1297 	le_add_device_to_white_list_cp cp;
   1298 	uint8_t status;
   1299 
   1300 	memset(&cp, 0, sizeof(cp));
   1301 	cp.bdaddr_type = type;
   1302 	bacpy(&cp.bdaddr, bdaddr);
   1303 
   1304 	memset(&rq, 0, sizeof(rq));
   1305 	rq.ogf = OGF_LE_CTL;
   1306 	rq.ocf = OCF_LE_ADD_DEVICE_TO_WHITE_LIST;
   1307 	rq.cparam = &cp;
   1308 	rq.clen = LE_ADD_DEVICE_TO_WHITE_LIST_CP_SIZE;
   1309 	rq.rparam = &status;
   1310 	rq.rlen = 1;
   1311 
   1312 	if (hci_send_req(dd, &rq, to) < 0)
   1313 		return -1;
   1314 
   1315 	if (status) {
   1316 		errno = EIO;
   1317 		return -1;
   1318 	}
   1319 
   1320 	return 0;
   1321 }
   1322 
   1323 int hci_le_rm_white_list(int dd, const bdaddr_t *bdaddr, uint8_t type, int to)
   1324 {
   1325 	struct hci_request rq;
   1326 	le_remove_device_from_white_list_cp cp;
   1327 	uint8_t status;
   1328 
   1329 	memset(&cp, 0, sizeof(cp));
   1330 	cp.bdaddr_type = type;
   1331 	bacpy(&cp.bdaddr, bdaddr);
   1332 
   1333 	memset(&rq, 0, sizeof(rq));
   1334 	rq.ogf = OGF_LE_CTL;
   1335 	rq.ocf = OCF_LE_REMOVE_DEVICE_FROM_WHITE_LIST;
   1336 	rq.cparam = &cp;
   1337 	rq.clen = LE_REMOVE_DEVICE_FROM_WHITE_LIST_CP_SIZE;
   1338 	rq.rparam = &status;
   1339 	rq.rlen = 1;
   1340 
   1341 	if (hci_send_req(dd, &rq, to) < 0)
   1342 		return -1;
   1343 
   1344 	if (status) {
   1345 		errno = EIO;
   1346 		return -1;
   1347 	}
   1348 
   1349 	return 0;
   1350 }
   1351 
   1352 int hci_le_read_white_list_size(int dd, uint8_t *size, int to)
   1353 {
   1354 	struct hci_request rq;
   1355 	le_read_white_list_size_rp rp;
   1356 
   1357 	memset(&rp, 0, sizeof(rp));
   1358 	memset(&rq, 0, sizeof(rq));
   1359 
   1360 	rq.ogf = OGF_LE_CTL;
   1361 	rq.ocf = OCF_LE_READ_WHITE_LIST_SIZE;
   1362 	rq.rparam = &rp;
   1363 	rq.rlen = LE_READ_WHITE_LIST_SIZE_RP_SIZE;
   1364 
   1365 	if (hci_send_req(dd, &rq, to) < 0)
   1366 		return -1;
   1367 
   1368 	if (rp.status) {
   1369 		errno = EIO;
   1370 		return -1;
   1371 	}
   1372 
   1373 	if (size)
   1374 		*size = rp.size;
   1375 
   1376 	return 0;
   1377 }
   1378 
   1379 int hci_le_clear_white_list(int dd, int to)
   1380 {
   1381 	struct hci_request rq;
   1382 	uint8_t status;
   1383 
   1384 	memset(&rq, 0, sizeof(rq));
   1385 	rq.ogf = OGF_LE_CTL;
   1386 	rq.ocf = OCF_LE_CLEAR_WHITE_LIST;
   1387 	rq.rparam = &status;
   1388 	rq.rlen = 1;
   1389 
   1390 	if (hci_send_req(dd, &rq, to) < 0)
   1391 		return -1;
   1392 
   1393 	if (status) {
   1394 		errno = EIO;
   1395 		return -1;
   1396 	}
   1397 
   1398 	return 0;
   1399 }
   1400 
   1401 int hci_read_local_name(int dd, int len, char *name, int to)
   1402 {
   1403 	read_local_name_rp rp;
   1404 	struct hci_request rq;
   1405 
   1406 	memset(&rq, 0, sizeof(rq));
   1407 	rq.ogf    = OGF_HOST_CTL;
   1408 	rq.ocf    = OCF_READ_LOCAL_NAME;
   1409 	rq.rparam = &rp;
   1410 	rq.rlen   = READ_LOCAL_NAME_RP_SIZE;
   1411 
   1412 	if (hci_send_req(dd, &rq, to) < 0)
   1413 		return -1;
   1414 
   1415 	if (rp.status) {
   1416 		errno = EIO;
   1417 		return -1;
   1418 	}
   1419 
   1420 	rp.name[247] = '\0';
   1421 	strncpy(name, (char *) rp.name, len);
   1422 	return 0;
   1423 }
   1424 
   1425 int hci_write_local_name(int dd, const char *name, int to)
   1426 {
   1427 	change_local_name_cp cp;
   1428 	struct hci_request rq;
   1429 
   1430 	memset(&cp, 0, sizeof(cp));
   1431 	strncpy((char *) cp.name, name, sizeof(cp.name));
   1432 
   1433 	memset(&rq, 0, sizeof(rq));
   1434 	rq.ogf    = OGF_HOST_CTL;
   1435 	rq.ocf    = OCF_CHANGE_LOCAL_NAME;
   1436 	rq.cparam = &cp;
   1437 	rq.clen   = CHANGE_LOCAL_NAME_CP_SIZE;
   1438 
   1439 	if (hci_send_req(dd, &rq, to) < 0)
   1440 		return -1;
   1441 
   1442 	return 0;
   1443 }
   1444 
   1445 int hci_read_remote_name_with_clock_offset(int dd, const bdaddr_t *bdaddr,
   1446 						uint8_t pscan_rep_mode,
   1447 						uint16_t clkoffset,
   1448 						int len, char *name, int to)
   1449 {
   1450 	evt_remote_name_req_complete rn;
   1451 	remote_name_req_cp cp;
   1452 	struct hci_request rq;
   1453 
   1454 	memset(&cp, 0, sizeof(cp));
   1455 	bacpy(&cp.bdaddr, bdaddr);
   1456 	cp.pscan_rep_mode = pscan_rep_mode;
   1457 	cp.clock_offset   = clkoffset;
   1458 
   1459 	memset(&rq, 0, sizeof(rq));
   1460 	rq.ogf    = OGF_LINK_CTL;
   1461 	rq.ocf    = OCF_REMOTE_NAME_REQ;
   1462 	rq.cparam = &cp;
   1463 	rq.clen   = REMOTE_NAME_REQ_CP_SIZE;
   1464 	rq.event  = EVT_REMOTE_NAME_REQ_COMPLETE;
   1465 	rq.rparam = &rn;
   1466 	rq.rlen   = EVT_REMOTE_NAME_REQ_COMPLETE_SIZE;
   1467 
   1468 	if (hci_send_req(dd, &rq, to) < 0)
   1469 		return -1;
   1470 
   1471 	if (rn.status) {
   1472 		errno = EIO;
   1473 		return -1;
   1474 	}
   1475 
   1476 	rn.name[247] = '\0';
   1477 	strncpy(name, (char *) rn.name, len);
   1478 	return 0;
   1479 }
   1480 
   1481 int hci_read_remote_name(int dd, const bdaddr_t *bdaddr, int len, char *name,
   1482 				int to)
   1483 {
   1484 	return hci_read_remote_name_with_clock_offset(dd, bdaddr, 0x02, 0x0000,
   1485 							len, name, to);
   1486 }
   1487 
   1488 int hci_read_remote_name_cancel(int dd, const bdaddr_t *bdaddr, int to)
   1489 {
   1490 	remote_name_req_cancel_cp cp;
   1491 	struct hci_request rq;
   1492 
   1493 	memset(&cp, 0, sizeof(cp));
   1494 	bacpy(&cp.bdaddr, bdaddr);
   1495 
   1496 	memset(&rq, 0, sizeof(rq));
   1497 	rq.ogf    = OGF_LINK_CTL;
   1498 	rq.ocf    = OCF_REMOTE_NAME_REQ_CANCEL;
   1499 	rq.cparam = &cp;
   1500 	rq.clen   = REMOTE_NAME_REQ_CANCEL_CP_SIZE;
   1501 
   1502 	if (hci_send_req(dd, &rq, to) < 0)
   1503 		return -1;
   1504 
   1505 	return 0;
   1506 }
   1507 
   1508 int hci_read_remote_version(int dd, uint16_t handle, struct hci_version *ver,
   1509 				int to)
   1510 {
   1511 	evt_read_remote_version_complete rp;
   1512 	read_remote_version_cp cp;
   1513 	struct hci_request rq;
   1514 
   1515 	memset(&cp, 0, sizeof(cp));
   1516 	cp.handle = handle;
   1517 
   1518 	memset(&rq, 0, sizeof(rq));
   1519 	rq.ogf    = OGF_LINK_CTL;
   1520 	rq.ocf    = OCF_READ_REMOTE_VERSION;
   1521 	rq.event  = EVT_READ_REMOTE_VERSION_COMPLETE;
   1522 	rq.cparam = &cp;
   1523 	rq.clen   = READ_REMOTE_VERSION_CP_SIZE;
   1524 	rq.rparam = &rp;
   1525 	rq.rlen   = EVT_READ_REMOTE_VERSION_COMPLETE_SIZE;
   1526 
   1527 	if (hci_send_req(dd, &rq, to) < 0)
   1528 		return -1;
   1529 
   1530 	if (rp.status) {
   1531 		errno = EIO;
   1532 		return -1;
   1533 	}
   1534 
   1535 	ver->manufacturer = btohs(rp.manufacturer);
   1536 	ver->lmp_ver      = rp.lmp_ver;
   1537 	ver->lmp_subver   = btohs(rp.lmp_subver);
   1538 	return 0;
   1539 }
   1540 
   1541 int hci_read_remote_features(int dd, uint16_t handle, uint8_t *features, int to)
   1542 {
   1543 	evt_read_remote_features_complete rp;
   1544 	read_remote_features_cp cp;
   1545 	struct hci_request rq;
   1546 
   1547 	memset(&cp, 0, sizeof(cp));
   1548 	cp.handle = handle;
   1549 
   1550 	memset(&rq, 0, sizeof(rq));
   1551 	rq.ogf    = OGF_LINK_CTL;
   1552 	rq.ocf    = OCF_READ_REMOTE_FEATURES;
   1553 	rq.event  = EVT_READ_REMOTE_FEATURES_COMPLETE;
   1554 	rq.cparam = &cp;
   1555 	rq.clen   = READ_REMOTE_FEATURES_CP_SIZE;
   1556 	rq.rparam = &rp;
   1557 	rq.rlen   = EVT_READ_REMOTE_FEATURES_COMPLETE_SIZE;
   1558 
   1559 	if (hci_send_req(dd, &rq, to) < 0)
   1560 		return -1;
   1561 
   1562 	if (rp.status) {
   1563 		errno = EIO;
   1564 		return -1;
   1565 	}
   1566 
   1567 	if (features)
   1568 		memcpy(features, rp.features, 8);
   1569 
   1570 	return 0;
   1571 }
   1572 
   1573 int hci_read_remote_ext_features(int dd, uint16_t handle, uint8_t page,
   1574 					uint8_t *max_page, uint8_t *features,
   1575 					int to)
   1576 {
   1577 	evt_read_remote_ext_features_complete rp;
   1578 	read_remote_ext_features_cp cp;
   1579 	struct hci_request rq;
   1580 
   1581 	memset(&cp, 0, sizeof(cp));
   1582 	cp.handle   = handle;
   1583 	cp.page_num = page;
   1584 
   1585 	memset(&rq, 0, sizeof(rq));
   1586 	rq.ogf    = OGF_LINK_CTL;
   1587 	rq.ocf    = OCF_READ_REMOTE_EXT_FEATURES;
   1588 	rq.event  = EVT_READ_REMOTE_EXT_FEATURES_COMPLETE;
   1589 	rq.cparam = &cp;
   1590 	rq.clen   = READ_REMOTE_EXT_FEATURES_CP_SIZE;
   1591 	rq.rparam = &rp;
   1592 	rq.rlen   = EVT_READ_REMOTE_EXT_FEATURES_COMPLETE_SIZE;
   1593 
   1594 	if (hci_send_req(dd, &rq, to) < 0)
   1595 		return -1;
   1596 
   1597 	if (rp.status) {
   1598 		errno = EIO;
   1599 		return -1;
   1600 	}
   1601 
   1602 	if (max_page)
   1603 		*max_page = rp.max_page_num;
   1604 
   1605 	if (features)
   1606 		memcpy(features, rp.features, 8);
   1607 
   1608 	return 0;
   1609 }
   1610 
   1611 int hci_read_clock_offset(int dd, uint16_t handle, uint16_t *clkoffset, int to)
   1612 {
   1613 	evt_read_clock_offset_complete rp;
   1614 	read_clock_offset_cp cp;
   1615 	struct hci_request rq;
   1616 
   1617 	memset(&cp, 0, sizeof(cp));
   1618 	cp.handle = handle;
   1619 
   1620 	memset(&rq, 0, sizeof(rq));
   1621 	rq.ogf    = OGF_LINK_CTL;
   1622 	rq.ocf    = OCF_READ_CLOCK_OFFSET;
   1623 	rq.event  = EVT_READ_CLOCK_OFFSET_COMPLETE;
   1624 	rq.cparam = &cp;
   1625 	rq.clen   = READ_CLOCK_OFFSET_CP_SIZE;
   1626 	rq.rparam = &rp;
   1627 	rq.rlen   = EVT_READ_CLOCK_OFFSET_COMPLETE_SIZE;
   1628 
   1629 	if (hci_send_req(dd, &rq, to) < 0)
   1630 		return -1;
   1631 
   1632 	if (rp.status) {
   1633 		errno = EIO;
   1634 		return -1;
   1635 	}
   1636 
   1637 	*clkoffset = rp.clock_offset;
   1638 	return 0;
   1639 }
   1640 
   1641 int hci_read_local_version(int dd, struct hci_version *ver, int to)
   1642 {
   1643 	read_local_version_rp rp;
   1644 	struct hci_request rq;
   1645 
   1646 	memset(&rq, 0, sizeof(rq));
   1647 	rq.ogf    = OGF_INFO_PARAM;
   1648 	rq.ocf    = OCF_READ_LOCAL_VERSION;
   1649 	rq.rparam = &rp;
   1650 	rq.rlen   = READ_LOCAL_VERSION_RP_SIZE;
   1651 
   1652 	if (hci_send_req(dd, &rq, to) < 0)
   1653 		return -1;
   1654 
   1655 	if (rp.status) {
   1656 		errno = EIO;
   1657 		return -1;
   1658 	}
   1659 
   1660 	ver->manufacturer = btohs(rp.manufacturer);
   1661 	ver->hci_ver      = rp.hci_ver;
   1662 	ver->hci_rev      = btohs(rp.hci_rev);
   1663 	ver->lmp_ver      = rp.lmp_ver;
   1664 	ver->lmp_subver   = btohs(rp.lmp_subver);
   1665 	return 0;
   1666 }
   1667 
   1668 int hci_read_local_commands(int dd, uint8_t *commands, int to)
   1669 {
   1670 	read_local_commands_rp rp;
   1671 	struct hci_request rq;
   1672 
   1673 	memset(&rq, 0, sizeof(rq));
   1674 	rq.ogf    = OGF_INFO_PARAM;
   1675 	rq.ocf    = OCF_READ_LOCAL_COMMANDS;
   1676 	rq.rparam = &rp;
   1677 	rq.rlen   = READ_LOCAL_COMMANDS_RP_SIZE;
   1678 
   1679 	if (hci_send_req(dd, &rq, to) < 0)
   1680 		return -1;
   1681 
   1682 	if (rp.status) {
   1683 		errno = EIO;
   1684 		return -1;
   1685 	}
   1686 
   1687 	if (commands)
   1688 		memcpy(commands, rp.commands, 64);
   1689 
   1690 	return 0;
   1691 }
   1692 
   1693 int hci_read_local_features(int dd, uint8_t *features, int to)
   1694 {
   1695 	read_local_features_rp rp;
   1696 	struct hci_request rq;
   1697 
   1698 	memset(&rq, 0, sizeof(rq));
   1699 	rq.ogf    = OGF_INFO_PARAM;
   1700 	rq.ocf    = OCF_READ_LOCAL_FEATURES;
   1701 	rq.rparam = &rp;
   1702 	rq.rlen   = READ_LOCAL_FEATURES_RP_SIZE;
   1703 
   1704 	if (hci_send_req(dd, &rq, to) < 0)
   1705 		return -1;
   1706 
   1707 	if (rp.status) {
   1708 		errno = EIO;
   1709 		return -1;
   1710 	}
   1711 
   1712 	if (features)
   1713 		memcpy(features, rp.features, 8);
   1714 
   1715 	return 0;
   1716 }
   1717 
   1718 int hci_read_local_ext_features(int dd, uint8_t page, uint8_t *max_page,
   1719 				uint8_t *features, int to)
   1720 {
   1721 	read_local_ext_features_cp cp;
   1722 	read_local_ext_features_rp rp;
   1723 	struct hci_request rq;
   1724 
   1725 	cp.page_num = page;
   1726 
   1727 	memset(&rq, 0, sizeof(rq));
   1728 	rq.ogf    = OGF_INFO_PARAM;
   1729 	rq.ocf    = OCF_READ_LOCAL_EXT_FEATURES;
   1730 	rq.cparam = &cp;
   1731 	rq.clen   = READ_LOCAL_EXT_FEATURES_CP_SIZE;
   1732 	rq.rparam = &rp;
   1733 	rq.rlen   = READ_LOCAL_EXT_FEATURES_RP_SIZE;
   1734 
   1735 	if (hci_send_req(dd, &rq, to) < 0)
   1736 		return -1;
   1737 
   1738 	if (rp.status) {
   1739 		errno = EIO;
   1740 		return -1;
   1741 	}
   1742 
   1743 	if (max_page)
   1744 		*max_page = rp.max_page_num;
   1745 
   1746 	if (features)
   1747 		memcpy(features, rp.features, 8);
   1748 
   1749 	return 0;
   1750 }
   1751 
   1752 int hci_read_bd_addr(int dd, bdaddr_t *bdaddr, int to)
   1753 {
   1754 	read_bd_addr_rp rp;
   1755 	struct hci_request rq;
   1756 
   1757 	memset(&rq, 0, sizeof(rq));
   1758 	rq.ogf    = OGF_INFO_PARAM;
   1759 	rq.ocf    = OCF_READ_BD_ADDR;
   1760 	rq.rparam = &rp;
   1761 	rq.rlen   = READ_BD_ADDR_RP_SIZE;
   1762 
   1763 	if (hci_send_req(dd, &rq, to) < 0)
   1764 		return -1;
   1765 
   1766 	if (rp.status) {
   1767 		errno = EIO;
   1768 		return -1;
   1769 	}
   1770 
   1771 	if (bdaddr)
   1772 		bacpy(bdaddr, &rp.bdaddr);
   1773 
   1774 	return 0;
   1775 }
   1776 
   1777 int hci_read_class_of_dev(int dd, uint8_t *cls, int to)
   1778 {
   1779 	read_class_of_dev_rp rp;
   1780 	struct hci_request rq;
   1781 
   1782 	memset(&rq, 0, sizeof(rq));
   1783 	rq.ogf    = OGF_HOST_CTL;
   1784 	rq.ocf    = OCF_READ_CLASS_OF_DEV;
   1785 	rq.rparam = &rp;
   1786 	rq.rlen   = READ_CLASS_OF_DEV_RP_SIZE;
   1787 
   1788 	if (hci_send_req(dd, &rq, to) < 0)
   1789 		return -1;
   1790 
   1791 	if (rp.status) {
   1792 		errno = EIO;
   1793 		return -1;
   1794 	}
   1795 
   1796 	memcpy(cls, rp.dev_class, 3);
   1797 	return 0;
   1798 }
   1799 
   1800 int hci_write_class_of_dev(int dd, uint32_t cls, int to)
   1801 {
   1802 	write_class_of_dev_cp cp;
   1803 	struct hci_request rq;
   1804 
   1805 	memset(&rq, 0, sizeof(rq));
   1806 	cp.dev_class[0] = cls & 0xff;
   1807 	cp.dev_class[1] = (cls >> 8) & 0xff;
   1808 	cp.dev_class[2] = (cls >> 16) & 0xff;
   1809 	rq.ogf    = OGF_HOST_CTL;
   1810 	rq.ocf    = OCF_WRITE_CLASS_OF_DEV;
   1811 	rq.cparam = &cp;
   1812 	rq.clen   = WRITE_CLASS_OF_DEV_CP_SIZE;
   1813 	return hci_send_req(dd, &rq, to);
   1814 }
   1815 
   1816 int hci_read_voice_setting(int dd, uint16_t *vs, int to)
   1817 {
   1818 	read_voice_setting_rp rp;
   1819 	struct hci_request rq;
   1820 
   1821 	memset(&rq, 0, sizeof(rq));
   1822 	rq.ogf    = OGF_HOST_CTL;
   1823 	rq.ocf    = OCF_READ_VOICE_SETTING;
   1824 	rq.rparam = &rp;
   1825 	rq.rlen   = READ_VOICE_SETTING_RP_SIZE;
   1826 
   1827 	if (hci_send_req(dd, &rq, to) < 0)
   1828 		return -1;
   1829 
   1830 	if (rp.status) {
   1831 		errno = EIO;
   1832 		return -1;
   1833 	}
   1834 
   1835 	*vs = rp.voice_setting;
   1836 	return 0;
   1837 }
   1838 
   1839 int hci_write_voice_setting(int dd, uint16_t vs, int to)
   1840 {
   1841 	write_voice_setting_cp cp;
   1842 	struct hci_request rq;
   1843 
   1844 	memset(&rq, 0, sizeof(rq));
   1845 	cp.voice_setting = vs;
   1846 	rq.ogf    = OGF_HOST_CTL;
   1847 	rq.ocf    = OCF_WRITE_VOICE_SETTING;
   1848 	rq.cparam = &cp;
   1849 	rq.clen   = WRITE_VOICE_SETTING_CP_SIZE;
   1850 
   1851 	return hci_send_req(dd, &rq, to);
   1852 }
   1853 
   1854 int hci_read_current_iac_lap(int dd, uint8_t *num_iac, uint8_t *lap, int to)
   1855 {
   1856 	read_current_iac_lap_rp rp;
   1857 	struct hci_request rq;
   1858 
   1859 	memset(&rq, 0, sizeof(rq));
   1860 	rq.ogf    = OGF_HOST_CTL;
   1861 	rq.ocf    = OCF_READ_CURRENT_IAC_LAP;
   1862 	rq.rparam = &rp;
   1863 	rq.rlen   = READ_CURRENT_IAC_LAP_RP_SIZE;
   1864 
   1865 	if (hci_send_req(dd, &rq, to) < 0)
   1866 		return -1;
   1867 
   1868 	if (rp.status) {
   1869 		errno = EIO;
   1870 		return -1;
   1871 	}
   1872 
   1873 	*num_iac = rp.num_current_iac;
   1874 	memcpy(lap, rp.lap, rp.num_current_iac * 3);
   1875 	return 0;
   1876 }
   1877 
   1878 int hci_write_current_iac_lap(int dd, uint8_t num_iac, uint8_t *lap, int to)
   1879 {
   1880 	write_current_iac_lap_cp cp;
   1881 	struct hci_request rq;
   1882 
   1883 	memset(&cp, 0, sizeof(cp));
   1884 	cp.num_current_iac = num_iac;
   1885 	memcpy(&cp.lap, lap, num_iac * 3);
   1886 
   1887 	memset(&rq, 0, sizeof(rq));
   1888 	rq.ogf    = OGF_HOST_CTL;
   1889 	rq.ocf    = OCF_WRITE_CURRENT_IAC_LAP;
   1890 	rq.cparam = &cp;
   1891 	rq.clen   = num_iac * 3 + 1;
   1892 
   1893 	return hci_send_req(dd, &rq, to);
   1894 }
   1895 
   1896 int hci_read_stored_link_key(int dd, bdaddr_t *bdaddr, uint8_t all, int to)
   1897 {
   1898 	read_stored_link_key_cp cp;
   1899 	struct hci_request rq;
   1900 
   1901 	memset(&cp, 0, sizeof(cp));
   1902 	bacpy(&cp.bdaddr, bdaddr);
   1903 	cp.read_all = all;
   1904 
   1905 	memset(&rq, 0, sizeof(rq));
   1906 	rq.ogf    = OGF_HOST_CTL;
   1907 	rq.ocf    = OCF_READ_STORED_LINK_KEY;
   1908 	rq.cparam = &cp;
   1909 	rq.clen   = READ_STORED_LINK_KEY_CP_SIZE;
   1910 
   1911 	return hci_send_req(dd, &rq, to);
   1912 }
   1913 
   1914 int hci_write_stored_link_key(int dd, bdaddr_t *bdaddr, uint8_t *key, int to)
   1915 {
   1916 	unsigned char cp[WRITE_STORED_LINK_KEY_CP_SIZE + 6 + 16];
   1917 	struct hci_request rq;
   1918 
   1919 	memset(&cp, 0, sizeof(cp));
   1920 	cp[0] = 1;
   1921 	bacpy((bdaddr_t *) (cp + 1), bdaddr);
   1922 	memcpy(cp + 7, key, 16);
   1923 
   1924 	memset(&rq, 0, sizeof(rq));
   1925 	rq.ogf    = OGF_HOST_CTL;
   1926 	rq.ocf    = OCF_WRITE_STORED_LINK_KEY;
   1927 	rq.cparam = &cp;
   1928 	rq.clen   = WRITE_STORED_LINK_KEY_CP_SIZE + 6 + 16;
   1929 
   1930 	return hci_send_req(dd, &rq, to);
   1931 }
   1932 
   1933 int hci_delete_stored_link_key(int dd, bdaddr_t *bdaddr, uint8_t all, int to)
   1934 {
   1935 	delete_stored_link_key_cp cp;
   1936 	struct hci_request rq;
   1937 
   1938 	memset(&cp, 0, sizeof(cp));
   1939 	bacpy(&cp.bdaddr, bdaddr);
   1940 	cp.delete_all = all;
   1941 
   1942 	memset(&rq, 0, sizeof(rq));
   1943 	rq.ogf    = OGF_HOST_CTL;
   1944 	rq.ocf    = OCF_DELETE_STORED_LINK_KEY;
   1945 	rq.cparam = &cp;
   1946 	rq.clen   = DELETE_STORED_LINK_KEY_CP_SIZE;
   1947 
   1948 	return hci_send_req(dd, &rq, to);
   1949 }
   1950 
   1951 int hci_authenticate_link(int dd, uint16_t handle, int to)
   1952 {
   1953 	auth_requested_cp cp;
   1954 	evt_auth_complete rp;
   1955 	struct hci_request rq;
   1956 
   1957 	cp.handle = handle;
   1958 
   1959 	rq.ogf    = OGF_LINK_CTL;
   1960 	rq.ocf    = OCF_AUTH_REQUESTED;
   1961 	rq.event  = EVT_AUTH_COMPLETE;
   1962 	rq.cparam = &cp;
   1963 	rq.clen   = AUTH_REQUESTED_CP_SIZE;
   1964 	rq.rparam = &rp;
   1965 	rq.rlen   = EVT_AUTH_COMPLETE_SIZE;
   1966 
   1967 	if (hci_send_req(dd, &rq, to) < 0)
   1968 		return -1;
   1969 
   1970 	if (rp.status) {
   1971 		errno = EIO;
   1972 		return -1;
   1973 	}
   1974 
   1975 	return 0;
   1976 }
   1977 
   1978 int hci_encrypt_link(int dd, uint16_t handle, uint8_t encrypt, int to)
   1979 {
   1980 	set_conn_encrypt_cp cp;
   1981 	evt_encrypt_change rp;
   1982 	struct hci_request rq;
   1983 
   1984 	cp.handle  = handle;
   1985 	cp.encrypt = encrypt;
   1986 
   1987 	rq.ogf     = OGF_LINK_CTL;
   1988 	rq.ocf     = OCF_SET_CONN_ENCRYPT;
   1989 	rq.event   = EVT_ENCRYPT_CHANGE;
   1990 	rq.cparam  = &cp;
   1991 	rq.clen    = SET_CONN_ENCRYPT_CP_SIZE;
   1992 	rq.rparam  = &rp;
   1993 	rq.rlen    = EVT_ENCRYPT_CHANGE_SIZE;
   1994 
   1995 	if (hci_send_req(dd, &rq, to) < 0)
   1996 		return -1;
   1997 
   1998 	if (rp.status) {
   1999 		errno = EIO;
   2000 		return -1;
   2001 	}
   2002 
   2003 	return 0;
   2004 }
   2005 
   2006 int hci_change_link_key(int dd, uint16_t handle, int to)
   2007 {
   2008 	change_conn_link_key_cp cp;
   2009 	evt_change_conn_link_key_complete rp;
   2010 	struct hci_request rq;
   2011 
   2012 	cp.handle = handle;
   2013 
   2014 	rq.ogf    = OGF_LINK_CTL;
   2015 	rq.ocf    = OCF_CHANGE_CONN_LINK_KEY;
   2016 	rq.event  = EVT_CHANGE_CONN_LINK_KEY_COMPLETE;
   2017 	rq.cparam = &cp;
   2018 	rq.clen   = CHANGE_CONN_LINK_KEY_CP_SIZE;
   2019 	rq.rparam = &rp;
   2020 	rq.rlen   = EVT_CHANGE_CONN_LINK_KEY_COMPLETE_SIZE;
   2021 
   2022 	if (hci_send_req(dd, &rq, to) < 0)
   2023 		return -1;
   2024 
   2025 	if (rp.status) {
   2026 		errno = EIO;
   2027 		return -1;
   2028 	}
   2029 
   2030 	return 0;
   2031 }
   2032 
   2033 int hci_switch_role(int dd, bdaddr_t *bdaddr, uint8_t role, int to)
   2034 {
   2035 	switch_role_cp cp;
   2036 	evt_role_change rp;
   2037 	struct hci_request rq;
   2038 
   2039 	bacpy(&cp.bdaddr, bdaddr);
   2040 	cp.role   = role;
   2041 	rq.ogf    = OGF_LINK_POLICY;
   2042 	rq.ocf    = OCF_SWITCH_ROLE;
   2043 	rq.cparam = &cp;
   2044 	rq.clen   = SWITCH_ROLE_CP_SIZE;
   2045 	rq.rparam = &rp;
   2046 	rq.rlen   = EVT_ROLE_CHANGE_SIZE;
   2047 	rq.event  = EVT_ROLE_CHANGE;
   2048 
   2049 	if (hci_send_req(dd, &rq, to) < 0)
   2050 		return -1;
   2051 
   2052 	if (rp.status) {
   2053 		errno = EIO;
   2054 		return -1;
   2055 	}
   2056 
   2057 	return 0;
   2058 }
   2059 
   2060 int hci_park_mode(int dd, uint16_t handle, uint16_t max_interval,
   2061 			uint16_t min_interval, int to)
   2062 {
   2063 	park_mode_cp cp;
   2064 	evt_mode_change rp;
   2065 	struct hci_request rq;
   2066 
   2067 	memset(&cp, 0, sizeof (cp));
   2068 	cp.handle       = handle;
   2069 	cp.max_interval = max_interval;
   2070 	cp.min_interval = min_interval;
   2071 
   2072 	memset(&rq, 0, sizeof (rq));
   2073 	rq.ogf    = OGF_LINK_POLICY;
   2074 	rq.ocf    = OCF_PARK_MODE;
   2075 	rq.event  = EVT_MODE_CHANGE;
   2076 	rq.cparam = &cp;
   2077 	rq.clen   = PARK_MODE_CP_SIZE;
   2078 	rq.rparam = &rp;
   2079 	rq.rlen   = EVT_MODE_CHANGE_SIZE;
   2080 
   2081 	if (hci_send_req(dd, &rq, to) < 0)
   2082 		return -1;
   2083 
   2084 	if (rp.status) {
   2085 		errno = EIO;
   2086 		return -1;
   2087 	}
   2088 
   2089 	return 0;
   2090 }
   2091 
   2092 int hci_exit_park_mode(int dd, uint16_t handle, int to)
   2093 {
   2094 	exit_park_mode_cp cp;
   2095 	evt_mode_change rp;
   2096 	struct hci_request rq;
   2097 
   2098 	memset(&cp, 0, sizeof (cp));
   2099 	cp.handle = handle;
   2100 
   2101 	memset (&rq, 0, sizeof (rq));
   2102 	rq.ogf    = OGF_LINK_POLICY;
   2103 	rq.ocf    = OCF_EXIT_PARK_MODE;
   2104 	rq.event  = EVT_MODE_CHANGE;
   2105 	rq.cparam = &cp;
   2106 	rq.clen   = EXIT_PARK_MODE_CP_SIZE;
   2107 	rq.rparam = &rp;
   2108 	rq.rlen   = EVT_MODE_CHANGE_SIZE;
   2109 
   2110 	if (hci_send_req(dd, &rq, to) < 0)
   2111 		return -1;
   2112 
   2113 	if (rp.status) {
   2114 		errno = EIO;
   2115 		return -1;
   2116 	}
   2117 
   2118 	return 0;
   2119 }
   2120 
   2121 int hci_read_inquiry_scan_type(int dd, uint8_t *type, int to)
   2122 {
   2123 	read_inquiry_scan_type_rp rp;
   2124 	struct hci_request rq;
   2125 
   2126 	memset(&rq, 0, sizeof(rq));
   2127 	rq.ogf    = OGF_HOST_CTL;
   2128 	rq.ocf    = OCF_READ_INQUIRY_SCAN_TYPE;
   2129 	rq.rparam = &rp;
   2130 	rq.rlen   = READ_INQUIRY_SCAN_TYPE_RP_SIZE;
   2131 
   2132 	if (hci_send_req(dd, &rq, to) < 0)
   2133 		return -1;
   2134 
   2135 	if (rp.status) {
   2136 		errno = EIO;
   2137 		return -1;
   2138 	}
   2139 
   2140 	*type = rp.type;
   2141 	return 0;
   2142 }
   2143 
   2144 int hci_write_inquiry_scan_type(int dd, uint8_t type, int to)
   2145 {
   2146 	write_inquiry_scan_type_cp cp;
   2147 	write_inquiry_scan_type_rp rp;
   2148 	struct hci_request rq;
   2149 
   2150 	memset(&cp, 0, sizeof(cp));
   2151 	cp.type = type;
   2152 
   2153 	memset(&rq, 0, sizeof(rq));
   2154 	rq.ogf    = OGF_HOST_CTL;
   2155 	rq.ocf    = OCF_WRITE_INQUIRY_SCAN_TYPE;
   2156 	rq.cparam = &cp;
   2157 	rq.clen   = WRITE_INQUIRY_SCAN_TYPE_CP_SIZE;
   2158 	rq.rparam = &rp;
   2159 	rq.rlen   = WRITE_INQUIRY_SCAN_TYPE_RP_SIZE;
   2160 
   2161 	if (hci_send_req(dd, &rq, to) < 0)
   2162 		return -1;
   2163 
   2164 	if (rp.status) {
   2165 		errno = EIO;
   2166 		return -1;
   2167 	}
   2168 
   2169 	return 0;
   2170 }
   2171 
   2172 int hci_read_inquiry_mode(int dd, uint8_t *mode, int to)
   2173 {
   2174 	read_inquiry_mode_rp rp;
   2175 	struct hci_request rq;
   2176 
   2177 	memset(&rq, 0, sizeof(rq));
   2178 	rq.ogf    = OGF_HOST_CTL;
   2179 	rq.ocf    = OCF_READ_INQUIRY_MODE;
   2180 	rq.rparam = &rp;
   2181 	rq.rlen   = READ_INQUIRY_MODE_RP_SIZE;
   2182 
   2183 	if (hci_send_req(dd, &rq, to) < 0)
   2184 		return -1;
   2185 
   2186 	if (rp.status) {
   2187 		errno = EIO;
   2188 		return -1;
   2189 	}
   2190 
   2191 	*mode = rp.mode;
   2192 	return 0;
   2193 }
   2194 
   2195 int hci_write_inquiry_mode(int dd, uint8_t mode, int to)
   2196 {
   2197 	write_inquiry_mode_cp cp;
   2198 	write_inquiry_mode_rp rp;
   2199 	struct hci_request rq;
   2200 
   2201 	memset(&cp, 0, sizeof(cp));
   2202 	cp.mode = mode;
   2203 
   2204 	memset(&rq, 0, sizeof(rq));
   2205 	rq.ogf    = OGF_HOST_CTL;
   2206 	rq.ocf    = OCF_WRITE_INQUIRY_MODE;
   2207 	rq.cparam = &cp;
   2208 	rq.clen   = WRITE_INQUIRY_MODE_CP_SIZE;
   2209 	rq.rparam = &rp;
   2210 	rq.rlen   = WRITE_INQUIRY_MODE_RP_SIZE;
   2211 
   2212 	if (hci_send_req(dd, &rq, to) < 0)
   2213 		return -1;
   2214 
   2215 	if (rp.status) {
   2216 		errno = EIO;
   2217 		return -1;
   2218 	}
   2219 
   2220 	return 0;
   2221 }
   2222 
   2223 int hci_read_afh_mode(int dd, uint8_t *mode, int to)
   2224 {
   2225 	read_afh_mode_rp rp;
   2226 	struct hci_request rq;
   2227 
   2228 	memset(&rq, 0, sizeof(rq));
   2229 	rq.ogf    = OGF_HOST_CTL;
   2230 	rq.ocf    = OCF_READ_AFH_MODE;
   2231 	rq.rparam = &rp;
   2232 	rq.rlen   = READ_AFH_MODE_RP_SIZE;
   2233 
   2234 	if (hci_send_req(dd, &rq, to) < 0)
   2235 		return -1;
   2236 
   2237 	if (rp.status) {
   2238 		errno = EIO;
   2239 		return -1;
   2240 	}
   2241 
   2242 	*mode = rp.mode;
   2243 	return 0;
   2244 }
   2245 
   2246 int hci_write_afh_mode(int dd, uint8_t mode, int to)
   2247 {
   2248 	write_afh_mode_cp cp;
   2249 	write_afh_mode_rp rp;
   2250 	struct hci_request rq;
   2251 
   2252 	memset(&cp, 0, sizeof(cp));
   2253 	cp.mode = mode;
   2254 
   2255 	memset(&rq, 0, sizeof(rq));
   2256 	rq.ogf    = OGF_HOST_CTL;
   2257 	rq.ocf    = OCF_WRITE_AFH_MODE;
   2258 	rq.cparam = &cp;
   2259 	rq.clen   = WRITE_AFH_MODE_CP_SIZE;
   2260 	rq.rparam = &rp;
   2261 	rq.rlen   = WRITE_AFH_MODE_RP_SIZE;
   2262 
   2263 	if (hci_send_req(dd, &rq, to) < 0)
   2264 		return -1;
   2265 
   2266 	if (rp.status) {
   2267 		errno = EIO;
   2268 		return -1;
   2269 	}
   2270 
   2271 	return 0;
   2272 }
   2273 
   2274 int hci_read_ext_inquiry_response(int dd, uint8_t *fec, uint8_t *data, int to)
   2275 {
   2276 	read_ext_inquiry_response_rp rp;
   2277 	struct hci_request rq;
   2278 
   2279 	memset(&rq, 0, sizeof(rq));
   2280 	rq.ogf    = OGF_HOST_CTL;
   2281 	rq.ocf    = OCF_READ_EXT_INQUIRY_RESPONSE;
   2282 	rq.rparam = &rp;
   2283 	rq.rlen   = READ_EXT_INQUIRY_RESPONSE_RP_SIZE;
   2284 
   2285 	if (hci_send_req(dd, &rq, to) < 0)
   2286 		return -1;
   2287 
   2288 	if (rp.status) {
   2289 		errno = EIO;
   2290 		return -1;
   2291 	}
   2292 
   2293 	*fec = rp.fec;
   2294 	memcpy(data, rp.data, HCI_MAX_EIR_LENGTH);
   2295 
   2296 	return 0;
   2297 }
   2298 
   2299 int hci_write_ext_inquiry_response(int dd, uint8_t fec, uint8_t *data, int to)
   2300 {
   2301 	write_ext_inquiry_response_cp cp;
   2302 	write_ext_inquiry_response_rp rp;
   2303 	struct hci_request rq;
   2304 
   2305 	memset(&cp, 0, sizeof(cp));
   2306 	cp.fec = fec;
   2307 	memcpy(cp.data, data, HCI_MAX_EIR_LENGTH);
   2308 
   2309 	memset(&rq, 0, sizeof(rq));
   2310 	rq.ogf    = OGF_HOST_CTL;
   2311 	rq.ocf    = OCF_WRITE_EXT_INQUIRY_RESPONSE;
   2312 	rq.cparam = &cp;
   2313 	rq.clen   = WRITE_EXT_INQUIRY_RESPONSE_CP_SIZE;
   2314 	rq.rparam = &rp;
   2315 	rq.rlen   = WRITE_EXT_INQUIRY_RESPONSE_RP_SIZE;
   2316 
   2317 	if (hci_send_req(dd, &rq, to) < 0)
   2318 		return -1;
   2319 
   2320 	if (rp.status) {
   2321 		errno = EIO;
   2322 		return -1;
   2323 	}
   2324 
   2325 	return 0;
   2326 }
   2327 
   2328 int hci_read_simple_pairing_mode(int dd, uint8_t *mode, int to)
   2329 {
   2330 	read_simple_pairing_mode_rp rp;
   2331 	struct hci_request rq;
   2332 
   2333 	memset(&rq, 0, sizeof(rq));
   2334 	rq.ogf    = OGF_HOST_CTL;
   2335 	rq.ocf    = OCF_READ_SIMPLE_PAIRING_MODE;
   2336 	rq.rparam = &rp;
   2337 	rq.rlen   = READ_SIMPLE_PAIRING_MODE_RP_SIZE;
   2338 
   2339 	if (hci_send_req(dd, &rq, to) < 0)
   2340 		return -1;
   2341 
   2342 	if (rp.status) {
   2343 		errno = EIO;
   2344 		return -1;
   2345 	}
   2346 
   2347 	*mode = rp.mode;
   2348 	return 0;
   2349 }
   2350 
   2351 int hci_write_simple_pairing_mode(int dd, uint8_t mode, int to)
   2352 {
   2353 	write_simple_pairing_mode_cp cp;
   2354 	write_simple_pairing_mode_rp rp;
   2355 	struct hci_request rq;
   2356 
   2357 	memset(&cp, 0, sizeof(cp));
   2358 	cp.mode = mode;
   2359 
   2360 	memset(&rq, 0, sizeof(rq));
   2361 	rq.ogf    = OGF_HOST_CTL;
   2362 	rq.ocf    = OCF_WRITE_SIMPLE_PAIRING_MODE;
   2363 	rq.cparam = &cp;
   2364 	rq.clen   = WRITE_SIMPLE_PAIRING_MODE_CP_SIZE;
   2365 	rq.rparam = &rp;
   2366 	rq.rlen   = WRITE_SIMPLE_PAIRING_MODE_RP_SIZE;
   2367 
   2368 	if (hci_send_req(dd, &rq, to) < 0)
   2369 		return -1;
   2370 
   2371 	if (rp.status) {
   2372 		errno = EIO;
   2373 		return -1;
   2374 	}
   2375 
   2376 	return 0;
   2377 }
   2378 
   2379 int hci_read_local_oob_data(int dd, uint8_t *hash, uint8_t *randomizer, int to)
   2380 {
   2381 	read_local_oob_data_rp rp;
   2382 	struct hci_request rq;
   2383 
   2384 	memset(&rq, 0, sizeof(rq));
   2385 	rq.ogf    = OGF_HOST_CTL;
   2386 	rq.ocf    = OCF_READ_LOCAL_OOB_DATA;
   2387 	rq.rparam = &rp;
   2388 	rq.rlen   = READ_LOCAL_OOB_DATA_RP_SIZE;
   2389 
   2390 	if (hci_send_req(dd, &rq, to) < 0)
   2391 		return -1;
   2392 
   2393 	if (rp.status) {
   2394 		errno = EIO;
   2395 		return -1;
   2396 	}
   2397 
   2398 	memcpy(hash, rp.hash, 16);
   2399 	memcpy(randomizer, rp.randomizer, 16);
   2400 	return 0;
   2401 }
   2402 
   2403 int hci_read_inq_response_tx_power_level(int dd, int8_t *level, int to)
   2404 {
   2405 	read_inq_response_tx_power_level_rp rp;
   2406 	struct hci_request rq;
   2407 
   2408 	memset(&rq, 0, sizeof(rq));
   2409 	rq.ogf    = OGF_HOST_CTL;
   2410 	rq.ocf    = OCF_READ_INQ_RESPONSE_TX_POWER_LEVEL;
   2411 	rq.rparam = &rp;
   2412 	rq.rlen   = READ_INQ_RESPONSE_TX_POWER_LEVEL_RP_SIZE;
   2413 
   2414 	if (hci_send_req(dd, &rq, to) < 0)
   2415 		return -1;
   2416 
   2417 	if (rp.status) {
   2418 		errno = EIO;
   2419 		return -1;
   2420 	}
   2421 
   2422 	*level = rp.level;
   2423 	return 0;
   2424 }
   2425 
   2426 int hci_read_inquiry_transmit_power_level(int dd, int8_t *level, int to)
   2427 {
   2428 	return hci_read_inq_response_tx_power_level(dd, level, to);
   2429 }
   2430 
   2431 int hci_write_inquiry_transmit_power_level(int dd, int8_t level, int to)
   2432 {
   2433 	write_inquiry_transmit_power_level_cp cp;
   2434 	write_inquiry_transmit_power_level_rp rp;
   2435 	struct hci_request rq;
   2436 
   2437 	memset(&cp, 0, sizeof(cp));
   2438 	cp.level = level;
   2439 
   2440 	memset(&rq, 0, sizeof(rq));
   2441 	rq.ogf    = OGF_HOST_CTL;
   2442 	rq.ocf    = OCF_WRITE_INQUIRY_TRANSMIT_POWER_LEVEL;
   2443 	rq.cparam = &cp;
   2444 	rq.clen   = WRITE_INQUIRY_TRANSMIT_POWER_LEVEL_CP_SIZE;
   2445 	rq.rparam = &rp;
   2446 	rq.rlen   = WRITE_INQUIRY_TRANSMIT_POWER_LEVEL_RP_SIZE;
   2447 
   2448 	if (hci_send_req(dd, &rq, to) < 0)
   2449 		return -1;
   2450 
   2451 	if (rp.status) {
   2452 		errno = EIO;
   2453 		return -1;
   2454 	}
   2455 
   2456 	return 0;
   2457 }
   2458 
   2459 int hci_read_transmit_power_level(int dd, uint16_t handle, uint8_t type,
   2460 					int8_t *level, int to)
   2461 {
   2462 	read_transmit_power_level_cp cp;
   2463 	read_transmit_power_level_rp rp;
   2464 	struct hci_request rq;
   2465 
   2466 	memset(&cp, 0, sizeof(cp));
   2467 	cp.handle = handle;
   2468 	cp.type   = type;
   2469 
   2470 	memset(&rq, 0, sizeof(rq));
   2471 	rq.ogf    = OGF_HOST_CTL;
   2472 	rq.ocf    = OCF_READ_TRANSMIT_POWER_LEVEL;
   2473 	rq.cparam = &cp;
   2474 	rq.clen   = READ_TRANSMIT_POWER_LEVEL_CP_SIZE;
   2475 	rq.rparam = &rp;
   2476 	rq.rlen   = READ_TRANSMIT_POWER_LEVEL_RP_SIZE;
   2477 
   2478 	if (hci_send_req(dd, &rq, to) < 0)
   2479 		return -1;
   2480 
   2481 	if (rp.status) {
   2482 		errno = EIO;
   2483 		return -1;
   2484 	}
   2485 
   2486 	*level = rp.level;
   2487 	return 0;
   2488 }
   2489 
   2490 int hci_read_link_policy(int dd, uint16_t handle, uint16_t *policy, int to)
   2491 {
   2492 	read_link_policy_rp rp;
   2493 	struct hci_request rq;
   2494 
   2495 	memset(&rq, 0, sizeof(rq));
   2496 	rq.ogf    = OGF_LINK_POLICY;
   2497 	rq.ocf    = OCF_READ_LINK_POLICY;
   2498 	rq.cparam = &handle;
   2499 	rq.clen   = 2;
   2500 	rq.rparam = &rp;
   2501 	rq.rlen   = READ_LINK_POLICY_RP_SIZE;
   2502 
   2503 	if (hci_send_req(dd, &rq, to) < 0)
   2504 		return -1;
   2505 
   2506 	if (rp.status) {
   2507 		errno = EIO;
   2508 		return -1;
   2509 	}
   2510 
   2511 	*policy = rp.policy;
   2512 	return 0;
   2513 }
   2514 
   2515 int hci_write_link_policy(int dd, uint16_t handle, uint16_t policy, int to)
   2516 {
   2517 	write_link_policy_cp cp;
   2518 	write_link_policy_rp rp;
   2519 	struct hci_request rq;
   2520 
   2521 	memset(&cp, 0, sizeof(cp));
   2522 	cp.handle = handle;
   2523 	cp.policy = policy;
   2524 
   2525 	memset(&rq, 0, sizeof(rq));
   2526 	rq.ogf    = OGF_LINK_POLICY;
   2527 	rq.ocf    = OCF_WRITE_LINK_POLICY;
   2528 	rq.cparam = &cp;
   2529 	rq.clen   = WRITE_LINK_POLICY_CP_SIZE;
   2530 	rq.rparam = &rp;
   2531 	rq.rlen   = WRITE_LINK_POLICY_RP_SIZE;
   2532 
   2533 	if (hci_send_req(dd, &rq, to) < 0)
   2534 		return -1;
   2535 
   2536 	if (rp.status) {
   2537 		errno = EIO;
   2538 		return -1;
   2539 	}
   2540 
   2541 	return 0;
   2542 }
   2543 
   2544 int hci_read_link_supervision_timeout(int dd, uint16_t handle,
   2545 					uint16_t *timeout, int to)
   2546 {
   2547 	read_link_supervision_timeout_rp rp;
   2548 	struct hci_request rq;
   2549 
   2550 	memset(&rq, 0, sizeof(rq));
   2551 	rq.ogf    = OGF_HOST_CTL;
   2552 	rq.ocf    = OCF_READ_LINK_SUPERVISION_TIMEOUT;
   2553 	rq.cparam = &handle;
   2554 	rq.clen   = 2;
   2555 	rq.rparam = &rp;
   2556 	rq.rlen   = READ_LINK_SUPERVISION_TIMEOUT_RP_SIZE;
   2557 
   2558 	if (hci_send_req(dd, &rq, to) < 0)
   2559 		return -1;
   2560 
   2561 	if (rp.status) {
   2562 		errno = EIO;
   2563 		return -1;
   2564 	}
   2565 
   2566 	*timeout = rp.timeout;
   2567 	return 0;
   2568 }
   2569 
   2570 int hci_write_link_supervision_timeout(int dd, uint16_t handle,
   2571 					uint16_t timeout, int to)
   2572 {
   2573 	write_link_supervision_timeout_cp cp;
   2574 	write_link_supervision_timeout_rp rp;
   2575 	struct hci_request rq;
   2576 
   2577 	memset(&cp, 0, sizeof(cp));
   2578 	cp.handle  = handle;
   2579 	cp.timeout = timeout;
   2580 
   2581 	memset(&rq, 0, sizeof(rq));
   2582 	rq.ogf    = OGF_HOST_CTL;
   2583 	rq.ocf    = OCF_WRITE_LINK_SUPERVISION_TIMEOUT;
   2584 	rq.cparam = &cp;
   2585 	rq.clen   = WRITE_LINK_SUPERVISION_TIMEOUT_CP_SIZE;
   2586 	rq.rparam = &rp;
   2587 	rq.rlen   = WRITE_LINK_SUPERVISION_TIMEOUT_RP_SIZE;
   2588 
   2589 	if (hci_send_req(dd, &rq, to) < 0)
   2590 		return -1;
   2591 
   2592 	if (rp.status) {
   2593 		errno = EIO;
   2594 		return -1;
   2595 	}
   2596 
   2597 	return 0;
   2598 }
   2599 
   2600 int hci_set_afh_classification(int dd, uint8_t *map, int to)
   2601 {
   2602 	set_afh_classification_cp cp;
   2603 	set_afh_classification_rp rp;
   2604 	struct hci_request rq;
   2605 
   2606 	memset(&cp, 0, sizeof(cp));
   2607 	memcpy(cp.map, map, 10);
   2608 
   2609 	memset(&rq, 0, sizeof(rq));
   2610 	rq.ogf    = OGF_HOST_CTL;
   2611 	rq.ocf    = OCF_SET_AFH_CLASSIFICATION;
   2612 	rq.cparam = &cp;
   2613 	rq.clen   = SET_AFH_CLASSIFICATION_CP_SIZE;
   2614 	rq.rparam = &rp;
   2615 	rq.rlen   = SET_AFH_CLASSIFICATION_RP_SIZE;
   2616 
   2617 	if (hci_send_req(dd, &rq, to) < 0)
   2618 		return -1;
   2619 
   2620 	if (rp.status) {
   2621 		errno = EIO;
   2622 		return -1;
   2623 	}
   2624 
   2625 	return 0;
   2626 }
   2627 
   2628 int hci_read_link_quality(int dd, uint16_t handle, uint8_t *link_quality,
   2629 				int to)
   2630 {
   2631 	read_link_quality_rp rp;
   2632 	struct hci_request rq;
   2633 
   2634 	memset(&rq, 0, sizeof(rq));
   2635 	rq.ogf    = OGF_STATUS_PARAM;
   2636 	rq.ocf    = OCF_READ_LINK_QUALITY;
   2637 	rq.cparam = &handle;
   2638 	rq.clen   = 2;
   2639 	rq.rparam = &rp;
   2640 	rq.rlen   = READ_LINK_QUALITY_RP_SIZE;
   2641 
   2642 	if (hci_send_req(dd, &rq, to) < 0)
   2643 		return -1;
   2644 
   2645 	if (rp.status) {
   2646 		errno = EIO;
   2647 		return -1;
   2648 	}
   2649 
   2650 	*link_quality = rp.link_quality;
   2651 	return 0;
   2652 }
   2653 
   2654 int hci_read_rssi(int dd, uint16_t handle, int8_t *rssi, int to)
   2655 {
   2656 	read_rssi_rp rp;
   2657 	struct hci_request rq;
   2658 
   2659 	memset(&rq, 0, sizeof(rq));
   2660 	rq.ogf    = OGF_STATUS_PARAM;
   2661 	rq.ocf    = OCF_READ_RSSI;
   2662 	rq.cparam = &handle;
   2663 	rq.clen   = 2;
   2664 	rq.rparam = &rp;
   2665 	rq.rlen   = READ_RSSI_RP_SIZE;
   2666 
   2667 	if (hci_send_req(dd, &rq, to) < 0)
   2668 		return -1;
   2669 
   2670 	if (rp.status) {
   2671 		errno = EIO;
   2672 		return -1;
   2673 	}
   2674 
   2675 	*rssi = rp.rssi;
   2676 	return 0;
   2677 }
   2678 
   2679 int hci_read_afh_map(int dd, uint16_t handle, uint8_t *mode, uint8_t *map,
   2680 			int to)
   2681 {
   2682 	read_afh_map_rp rp;
   2683 	struct hci_request rq;
   2684 
   2685 	memset(&rq, 0, sizeof(rq));
   2686 	rq.ogf    = OGF_STATUS_PARAM;
   2687 	rq.ocf    = OCF_READ_AFH_MAP;
   2688 	rq.cparam = &handle;
   2689 	rq.clen   = 2;
   2690 	rq.rparam = &rp;
   2691 	rq.rlen   = READ_AFH_MAP_RP_SIZE;
   2692 
   2693 	if (hci_send_req(dd, &rq, to) < 0)
   2694 		return -1;
   2695 
   2696 	if (rp.status) {
   2697 		errno = EIO;
   2698 		return -1;
   2699 	}
   2700 
   2701 	*mode = rp.mode;
   2702 	memcpy(map, rp.map, 10);
   2703 	return 0;
   2704 }
   2705 
   2706 int hci_read_clock(int dd, uint16_t handle, uint8_t which, uint32_t *clock,
   2707 			uint16_t *accuracy, int to)
   2708 {
   2709 	read_clock_cp cp;
   2710 	read_clock_rp rp;
   2711 	struct hci_request rq;
   2712 
   2713 	memset(&cp, 0, sizeof(cp));
   2714 	cp.handle      = handle;
   2715 	cp.which_clock = which;
   2716 
   2717 	memset(&rq, 0, sizeof(rq));
   2718 	rq.ogf    = OGF_STATUS_PARAM;
   2719 	rq.ocf    = OCF_READ_CLOCK;
   2720 	rq.cparam = &cp;
   2721 	rq.clen   = READ_CLOCK_CP_SIZE;
   2722 	rq.rparam = &rp;
   2723 	rq.rlen   = READ_CLOCK_RP_SIZE;
   2724 
   2725 	if (hci_send_req(dd, &rq, to) < 0)
   2726 		return -1;
   2727 
   2728 	if (rp.status) {
   2729 		errno = EIO;
   2730 		return -1;
   2731 	}
   2732 
   2733 	*clock    = rp.clock;
   2734 	*accuracy = rp.accuracy;
   2735 	return 0;
   2736 }
   2737 
   2738 int hci_le_set_scan_enable(int dd, uint8_t enable, uint8_t filter_dup, int to)
   2739 {
   2740 	struct hci_request rq;
   2741 	le_set_scan_enable_cp scan_cp;
   2742 	uint8_t status;
   2743 
   2744 	memset(&scan_cp, 0, sizeof(scan_cp));
   2745 	scan_cp.enable = enable;
   2746 	scan_cp.filter_dup = filter_dup;
   2747 
   2748 	memset(&rq, 0, sizeof(rq));
   2749 	rq.ogf = OGF_LE_CTL;
   2750 	rq.ocf = OCF_LE_SET_SCAN_ENABLE;
   2751 	rq.cparam = &scan_cp;
   2752 	rq.clen = LE_SET_SCAN_ENABLE_CP_SIZE;
   2753 	rq.rparam = &status;
   2754 	rq.rlen = 1;
   2755 
   2756 	if (hci_send_req(dd, &rq, to) < 0)
   2757 		return -1;
   2758 
   2759 	if (status) {
   2760 		errno = EIO;
   2761 		return -1;
   2762 	}
   2763 
   2764 	return 0;
   2765 }
   2766 
   2767 int hci_le_set_scan_parameters(int dd, uint8_t type,
   2768 					uint16_t interval, uint16_t window,
   2769 					uint8_t own_type, uint8_t filter, int to)
   2770 {
   2771 	struct hci_request rq;
   2772 	le_set_scan_parameters_cp param_cp;
   2773 	uint8_t status;
   2774 
   2775 	memset(&param_cp, 0, sizeof(param_cp));
   2776 	param_cp.type = type;
   2777 	param_cp.interval = interval;
   2778 	param_cp.window = window;
   2779 	param_cp.own_bdaddr_type = own_type;
   2780 	param_cp.filter = filter;
   2781 
   2782 	memset(&rq, 0, sizeof(rq));
   2783 	rq.ogf = OGF_LE_CTL;
   2784 	rq.ocf = OCF_LE_SET_SCAN_PARAMETERS;
   2785 	rq.cparam = &param_cp;
   2786 	rq.clen = LE_SET_SCAN_PARAMETERS_CP_SIZE;
   2787 	rq.rparam = &status;
   2788 	rq.rlen = 1;
   2789 
   2790 	if (hci_send_req(dd, &rq, to) < 0)
   2791 		return -1;
   2792 
   2793 	if (status) {
   2794 		errno = EIO;
   2795 		return -1;
   2796 	}
   2797 
   2798 	return 0;
   2799 }
   2800 
   2801 int hci_le_set_advertise_enable(int dd, uint8_t enable, int to)
   2802 {
   2803 	struct hci_request rq;
   2804 	le_set_advertise_enable_cp adv_cp;
   2805 	uint8_t status;
   2806 
   2807 	memset(&adv_cp, 0, sizeof(adv_cp));
   2808 	adv_cp.enable = enable;
   2809 
   2810 	memset(&rq, 0, sizeof(rq));
   2811 	rq.ogf = OGF_LE_CTL;
   2812 	rq.ocf = OCF_LE_SET_ADVERTISE_ENABLE;
   2813 	rq.cparam = &adv_cp;
   2814 	rq.clen = LE_SET_ADVERTISE_ENABLE_CP_SIZE;
   2815 	rq.rparam = &status;
   2816 	rq.rlen = 1;
   2817 
   2818 	if (hci_send_req(dd, &rq, to) < 0)
   2819 		return -1;
   2820 
   2821 	if (status) {
   2822 		errno = EIO;
   2823 		return -1;
   2824 	}
   2825 
   2826 	return 0;
   2827 }
   2828 
   2829 int hci_le_create_conn(int dd, uint16_t interval, uint16_t window,
   2830 		uint8_t initiator_filter, uint8_t peer_bdaddr_type,
   2831 		bdaddr_t peer_bdaddr, uint8_t own_bdaddr_type,
   2832 		uint16_t min_interval, 	uint16_t max_interval,
   2833 		uint16_t latency, uint16_t supervision_timeout,
   2834 		uint16_t min_ce_length, uint16_t max_ce_length,
   2835 		uint16_t *handle, int to)
   2836 {
   2837 	struct hci_request rq;
   2838 	le_create_connection_cp create_conn_cp;
   2839 	evt_le_connection_complete conn_complete_rp;
   2840 
   2841 	memset(&create_conn_cp, 0, sizeof(create_conn_cp));
   2842 	create_conn_cp.interval = interval;
   2843 	create_conn_cp.window = window;
   2844 	create_conn_cp.initiator_filter = initiator_filter;
   2845 	create_conn_cp.peer_bdaddr_type = peer_bdaddr_type;
   2846 	create_conn_cp.peer_bdaddr = peer_bdaddr;
   2847 	create_conn_cp.own_bdaddr_type = own_bdaddr_type;
   2848 	create_conn_cp.min_interval = min_interval;
   2849 	create_conn_cp.max_interval = max_interval;
   2850 	create_conn_cp.latency = latency;
   2851 	create_conn_cp.supervision_timeout = supervision_timeout;
   2852 	create_conn_cp.min_ce_length = min_ce_length;
   2853 	create_conn_cp.max_ce_length = max_ce_length;
   2854 
   2855 	memset(&rq, 0, sizeof(rq));
   2856 	rq.ogf = OGF_LE_CTL;
   2857 	rq.ocf = OCF_LE_CREATE_CONN;
   2858 	rq.event = EVT_LE_CONN_COMPLETE;
   2859 	rq.cparam = &create_conn_cp;
   2860 	rq.clen = LE_CREATE_CONN_CP_SIZE;
   2861 	rq.rparam = &conn_complete_rp;
   2862 	rq.rlen = EVT_CONN_COMPLETE_SIZE;
   2863 
   2864 	if (hci_send_req(dd, &rq, to) < 0)
   2865 		return -1;
   2866 
   2867 	if (conn_complete_rp.status) {
   2868 		errno = EIO;
   2869 		return -1;
   2870 	}
   2871 
   2872 	if (handle)
   2873 		*handle = conn_complete_rp.handle;
   2874 
   2875 	return 0;
   2876 }
   2877 
   2878 int hci_le_conn_update(int dd, uint16_t handle, uint16_t min_interval,
   2879 			uint16_t max_interval, uint16_t latency,
   2880 			uint16_t supervision_timeout, int to)
   2881 {
   2882 	evt_le_connection_update_complete evt;
   2883 	le_connection_update_cp cp;
   2884 	struct hci_request rq;
   2885 
   2886 	memset(&cp, 0, sizeof(cp));
   2887 	cp.handle = handle;
   2888 	cp.min_interval = min_interval;
   2889 	cp.max_interval = max_interval;
   2890 	cp.latency = latency;
   2891 	cp.supervision_timeout = supervision_timeout;
   2892 	cp.min_ce_length = htobs(0x0001);
   2893 	cp.max_ce_length = htobs(0x0001);
   2894 
   2895 	memset(&rq, 0, sizeof(rq));
   2896 	rq.ogf = OGF_LE_CTL;
   2897 	rq.ocf = OCF_LE_CONN_UPDATE;
   2898 	rq.cparam = &cp;
   2899 	rq.clen = LE_CONN_UPDATE_CP_SIZE;
   2900 	rq.event = EVT_LE_CONN_UPDATE_COMPLETE;
   2901 	rq.rparam = &evt;
   2902 	rq.rlen = sizeof(evt);
   2903 
   2904 	if (hci_send_req(dd, &rq, to) < 0)
   2905 		return -1;
   2906 
   2907 	if (evt.status) {
   2908 		errno = EIO;
   2909 		return -1;
   2910 	}
   2911 
   2912 	return 0;
   2913 }
   2914