1 /*
2  * Copyright (C) 2011  Intel Corporation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19 
20 enum llcp_state {
21 	LLCP_CONNECTED = 1, /* wait_for_packet() wants that */
22 	LLCP_CLOSED,
23 	LLCP_BOUND,
24 	LLCP_LISTEN,
25 };
26 
27 #define LLCP_DEFAULT_LTO 100
28 #define LLCP_DEFAULT_RW  1
29 #define LLCP_DEFAULT_MIU 128
30 
31 #define LLCP_WKS_NUM_SAP   16
32 #define LLCP_SDP_NUM_SAP   16
33 #define LLCP_LOCAL_NUM_SAP 32
34 #define LLCP_LOCAL_SAP_OFFSET (LLCP_WKS_NUM_SAP + LLCP_SDP_NUM_SAP)
35 #define LLCP_MAX_SAP (LLCP_WKS_NUM_SAP + LLCP_SDP_NUM_SAP + LLCP_LOCAL_NUM_SAP)
36 
37 struct nfc_llcp_sock;
38 
39 struct nfc_llcp_local {
40 	struct list_head list;
41 	struct nfc_dev *dev;
42 
43 	struct mutex sdp_lock;
44 	struct mutex socket_lock;
45 
46 	struct timer_list link_timer;
47 	struct sk_buff_head tx_queue;
48 	struct workqueue_struct	*tx_wq;
49 	struct work_struct	 tx_work;
50 	struct workqueue_struct	*rx_wq;
51 	struct work_struct	 rx_work;
52 	struct sk_buff *rx_pending;
53 	struct workqueue_struct	*timeout_wq;
54 	struct work_struct	 timeout_work;
55 
56 	u32 target_idx;
57 	u8 rf_mode;
58 	u8 comm_mode;
59 	unsigned long local_wks;      /* Well known services */
60 	unsigned long local_sdp;      /* Local services  */
61 	unsigned long local_sap; /* Local SAPs, not available for discovery */
62 
63 	/* local */
64 	u8 gb[NFC_MAX_GT_LEN];
65 	u8 gb_len;
66 
67 	/* remote */
68 	u8 remote_gb[NFC_MAX_GT_LEN];
69 	u8 remote_gb_len;
70 
71 	u8  remote_version;
72 	u16 remote_miu;
73 	u16 remote_lto;
74 	u8  remote_opt;
75 	u16 remote_wks;
76 	u8  remote_rw;
77 
78 	/* sockets array */
79 	struct nfc_llcp_sock *sockets[LLCP_MAX_SAP];
80 };
81 
82 struct nfc_llcp_sock {
83 	struct sock sk;
84 	struct list_head list;
85 	struct nfc_dev *dev;
86 	struct nfc_llcp_local *local;
87 	u32 target_idx;
88 	u32 nfc_protocol;
89 
90 	u8 ssap;
91 	u8 dsap;
92 	char *service_name;
93 	size_t service_name_len;
94 
95 	/* Link variables */
96 	u8 send_n;
97 	u8 send_ack_n;
98 	u8 recv_n;
99 	u8 recv_ack_n;
100 
101 	/* Is the remote peer ready to receive */
102 	u8 remote_ready;
103 
104 	struct sk_buff_head tx_queue;
105 	struct sk_buff_head tx_pending_queue;
106 	struct sk_buff_head tx_backlog_queue;
107 
108 	struct list_head accept_queue;
109 	struct sock *parent;
110 };
111 
112 #define nfc_llcp_sock(sk) ((struct nfc_llcp_sock *) (sk))
113 #define nfc_llcp_dev(sk)  (nfc_llcp_sock((sk))->dev)
114 
115 #define LLCP_HEADER_SIZE   2
116 #define LLCP_SEQUENCE_SIZE 1
117 
118 /* LLCP versions: 1.1 is 1.0 plus SDP */
119 #define LLCP_VERSION_10 0x10
120 #define LLCP_VERSION_11 0x11
121 
122 /* LLCP PDU types */
123 #define LLCP_PDU_SYMM     0x0
124 #define LLCP_PDU_PAX      0x1
125 #define LLCP_PDU_AGF      0x2
126 #define LLCP_PDU_UI       0x3
127 #define LLCP_PDU_CONNECT  0x4
128 #define LLCP_PDU_DISC     0x5
129 #define LLCP_PDU_CC       0x6
130 #define LLCP_PDU_DM       0x7
131 #define LLCP_PDU_FRMR     0x8
132 #define LLCP_PDU_SNL      0x9
133 #define LLCP_PDU_I        0xc
134 #define LLCP_PDU_RR       0xd
135 #define LLCP_PDU_RNR      0xe
136 
137 /* Parameters TLV types */
138 #define LLCP_TLV_VERSION 0x1
139 #define LLCP_TLV_MIUX    0x2
140 #define LLCP_TLV_WKS     0x3
141 #define LLCP_TLV_LTO     0x4
142 #define LLCP_TLV_RW      0x5
143 #define LLCP_TLV_SN      0x6
144 #define LLCP_TLV_OPT     0x7
145 #define LLCP_TLV_SDREQ   0x8
146 #define LLCP_TLV_SDRES   0x9
147 #define LLCP_TLV_MAX     0xa
148 
149 /* Well known LLCP SAP */
150 #define LLCP_SAP_SDP   0x1
151 #define LLCP_SAP_IP    0x2
152 #define LLCP_SAP_OBEX  0x3
153 #define LLCP_SAP_SNEP  0x4
154 #define LLCP_SAP_MAX   0xff
155 
156 /* Disconnection reason code */
157 #define LLCP_DM_DISC    0x00
158 #define LLCP_DM_NOCONN  0x01
159 #define LLCP_DM_NOBOUND 0x02
160 #define LLCP_DM_REJ     0x03
161 
162 
163 struct nfc_llcp_local *nfc_llcp_find_local(struct nfc_dev *dev);
164 u8 nfc_llcp_get_sdp_ssap(struct nfc_llcp_local *local,
165 				struct nfc_llcp_sock *sock);
166 u8 nfc_llcp_get_local_ssap(struct nfc_llcp_local *local);
167 void nfc_llcp_put_ssap(struct nfc_llcp_local *local, u8 ssap);
168 
169 /* Sock API */
170 struct sock *nfc_llcp_sock_alloc(struct socket *sock, int type, gfp_t gfp);
171 void nfc_llcp_sock_free(struct nfc_llcp_sock *sock);
172 void nfc_llcp_accept_unlink(struct sock *sk);
173 void nfc_llcp_accept_enqueue(struct sock *parent, struct sock *sk);
174 struct sock *nfc_llcp_accept_dequeue(struct sock *sk, struct socket *newsock);
175 
176 /* TLV API */
177 int nfc_llcp_parse_tlv(struct nfc_llcp_local *local,
178 			u8 *tlv_array, u16 tlv_array_len);
179 
180 /* Commands API */
181 void nfc_llcp_recv(void *data, struct sk_buff *skb, int err);
182 u8 *nfc_llcp_build_tlv(u8 type, u8 *value, u8 value_length, u8 *tlv_length);
183 void nfc_llcp_recv(void *data, struct sk_buff *skb, int err);
184 int nfc_llcp_disconnect(struct nfc_llcp_sock *sock);
185 int nfc_llcp_send_symm(struct nfc_dev *dev);
186 int nfc_llcp_send_connect(struct nfc_llcp_sock *sock);
187 int nfc_llcp_send_cc(struct nfc_llcp_sock *sock);
188 int nfc_llcp_send_dm(struct nfc_llcp_local *local, u8 ssap, u8 dsap, u8 reason);
189 int nfc_llcp_send_disconnect(struct nfc_llcp_sock *sock);
190 
191 /* Socket API */
192 int __init nfc_llcp_sock_init(void);
193 void nfc_llcp_sock_exit(void);
194