blob: cb1bc7abc1a9078bef0ccfc29f6b9aefd98b45e9 [file] [log] [blame]
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/net.h>
16#include <linux/socket.h>
17#include <linux/errno.h>
18#include <linux/mm.h>
19#include <linux/poll.h>
20#include <linux/fcntl.h>
21#include <linux/gfp.h>
22#include <linux/msm_ipc.h>
Zaheerulla Meerf0707d52013-02-21 17:56:18 +053023#include <linux/sched.h>
24#include <linux/thread_info.h>
25#include <linux/qmi_encdec.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026
27#include <asm/string.h>
28#include <asm/atomic.h>
29
30#include <net/sock.h>
31
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -060032#include <mach/msm_ipc_router.h>
Zaheerulla Meerf0707d52013-02-21 17:56:18 +053033#include <mach/msm_ipc_logging.h>
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -060034
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035#include "ipc_router.h"
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -060036#include "msm_ipc_router_security.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037
38#define msm_ipc_sk(sk) ((struct msm_ipc_sock *)(sk))
39#define msm_ipc_sk_port(sk) ((struct msm_ipc_port *)(msm_ipc_sk(sk)->port))
Zaheerulla Meerf0707d52013-02-21 17:56:18 +053040#define REQ_RESP_IPC_LOG_PAGES 5
41#define IND_IPC_LOG_PAGES 5
42#define IPC_SEND 1
43#define IPC_RECV 2
44#define IPC_REQ_RESP_LOG(level, buf...) \
45do { \
46 if (ipc_req_resp_log_txt) { \
47 ipc_log_string(ipc_req_resp_log_txt, buf); \
48 } \
49} while (0) \
50
51#define IPC_IND_LOG(level, buf...) \
52do { \
53 if (ipc_ind_log_txt) { \
54 ipc_log_string(ipc_ind_log_txt, buf); \
55 } \
56} while (0) \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070057
58static int sockets_enabled;
59static struct proto msm_ipc_proto;
60static const struct proto_ops msm_ipc_proto_ops;
Zaheerulla Meerf0707d52013-02-21 17:56:18 +053061static void *ipc_req_resp_log_txt;
62static void *ipc_ind_log_txt;
63
64/**
65 * msm_ipc_router_ipc_log() - Pass log data to IPC logging framework
66 * @tran: Identifies the data to be a receive or send.
67 * @ipc_buf: Buffer to extract the log data.
68 * @port_ptr: IPC Router port corresponding to the current log data.
69 *
70 * This function builds the data the would be passed on to the IPC logging
71 * framework. The data that would be passed corresponds to the information
72 * that is exchanged between the IPC Router and user space modules during
73 * request/response/indication transactions.
74 */
75
76static void msm_ipc_router_ipc_log(uint8_t tran,
77 struct sk_buff *ipc_buf, struct msm_ipc_port *port_ptr)
78{
79 struct qmi_header *hdr = (struct qmi_header *)ipc_buf->data;
80
81 /*
82 * IPC Logging format is as below:-
83 * <Name>(Name of the User Space Process):
84 * <PID> (PID of the user space process) :
85 * <TID> (TID of the user space thread) :
86 * <User Space Module>(CLNT or SERV) :
87 * <Opertaion Type> (Transmit) :
88 * <Control Flag> (Req/Resp/Ind) :
89 * <Transaction ID> :
90 * <Message ID> :
91 * <Message Length> :
92 */
93 if (ipc_req_resp_log_txt &&
94 (((uint8_t) hdr->cntl_flag == QMI_REQUEST_CONTROL_FLAG) ||
95 ((uint8_t) hdr->cntl_flag == QMI_RESPONSE_CONTROL_FLAG)) &&
96 (port_ptr->type == CLIENT_PORT ||
97 port_ptr->type == SERVER_PORT)) {
98 IPC_REQ_RESP_LOG(KERN_DEBUG,
99 "%s %d %d %s %s CF:%x TI:%x MI:%x ML:%x",
100 current->comm, current->tgid, current->pid,
101 (port_ptr->type == CLIENT_PORT ? "QCCI" : "QCSI"),
102 (tran == IPC_RECV ? "RX" :
103 (tran == IPC_SEND ? "TX" : "ERR")),
104 (uint8_t)hdr->cntl_flag, hdr->txn_id, hdr->msg_id,
105 hdr->msg_len);
106 } else if (ipc_ind_log_txt &&
107 ((uint8_t)hdr->cntl_flag == QMI_INDICATION_CONTROL_FLAG) &&
108 (port_ptr->type == CLIENT_PORT ||
109 port_ptr->type == SERVER_PORT)) {
110 IPC_IND_LOG(KERN_DEBUG,
111 "%s %d %d %s %s CF:%x TI:%x MI:%x ML:%x",
112 current->comm, current->tgid, current->pid,
113 (port_ptr->type == CLIENT_PORT ? "QCCI" : "QCSI"),
114 (tran == IPC_RECV ? "RX" :
115 (tran == IPC_SEND ? "TX" : "ERR")),
116 (uint8_t)hdr->cntl_flag, hdr->txn_id, hdr->msg_id,
117 hdr->msg_len);
118 }
119}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700121static struct sk_buff_head *msm_ipc_router_build_msg(unsigned int num_sect,
122 struct iovec const *msg_sect,
123 size_t total_len)
124{
125 struct sk_buff_head *msg_head;
126 struct sk_buff *msg;
127 int i, copied, first = 1;
128 int data_size = 0, request_size, offset;
129 void *data;
130
131 for (i = 0; i < num_sect; i++)
132 data_size += msg_sect[i].iov_len;
133
134 if (!data_size)
135 return NULL;
136
137 msg_head = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL);
138 if (!msg_head) {
139 pr_err("%s: cannot allocate skb_head\n", __func__);
140 return NULL;
141 }
142 skb_queue_head_init(msg_head);
143
144 for (copied = 1, i = 0; copied && (i < num_sect); i++) {
145 data_size = msg_sect[i].iov_len;
146 offset = 0;
147 while (offset != msg_sect[i].iov_len) {
148 request_size = data_size;
149 if (first)
150 request_size += IPC_ROUTER_HDR_SIZE;
151
152 msg = alloc_skb(request_size, GFP_KERNEL);
153 if (!msg) {
154 if (request_size <= (PAGE_SIZE/2)) {
155 pr_err("%s: cannot allocated skb\n",
156 __func__);
157 goto msg_build_failure;
158 }
159 data_size = data_size / 2;
160 continue;
161 }
162
163 if (first) {
164 skb_reserve(msg, IPC_ROUTER_HDR_SIZE);
165 first = 0;
166 }
167
168 data = skb_put(msg, data_size);
169 copied = !copy_from_user(msg->data,
170 msg_sect[i].iov_base + offset,
171 data_size);
172 if (!copied) {
173 pr_err("%s: copy_from_user failed\n",
174 __func__);
175 kfree_skb(msg);
176 goto msg_build_failure;
177 }
178 skb_queue_tail(msg_head, msg);
179 offset += data_size;
180 data_size = msg_sect[i].iov_len - offset;
181 }
182 }
183 return msg_head;
184
185msg_build_failure:
186 while (!skb_queue_empty(msg_head)) {
187 msg = skb_dequeue(msg_head);
188 kfree_skb(msg);
189 }
190 kfree(msg_head);
191 return NULL;
192}
193
194static int msm_ipc_router_extract_msg(struct msghdr *m,
195 struct sk_buff_head *msg_head)
196{
Karthikeyan Ramasubramanian2272b882013-04-05 11:30:53 -0600197 struct sockaddr_msm_ipc *addr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198 struct rr_header *hdr;
199 struct sk_buff *temp;
200 int offset = 0, data_len = 0, copy_len;
201
202 if (!m || !msg_head) {
203 pr_err("%s: Invalid pointers passed\n", __func__);
204 return -EINVAL;
205 }
Karthikeyan Ramasubramanian2272b882013-04-05 11:30:53 -0600206 addr = (struct sockaddr_msm_ipc *)m->msg_name;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207
208 temp = skb_peek(msg_head);
209 hdr = (struct rr_header *)(temp->data);
Karthikeyan Ramasubramanian2272b882013-04-05 11:30:53 -0600210 if (addr && (hdr->src_port_id != IPC_ROUTER_ADDRESS)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 addr->family = AF_MSM_IPC;
212 addr->address.addrtype = MSM_IPC_ADDR_ID;
213 addr->address.addr.port_addr.node_id = hdr->src_node_id;
214 addr->address.addr.port_addr.port_id = hdr->src_port_id;
215 m->msg_namelen = sizeof(struct sockaddr_msm_ipc);
216 }
217
218 data_len = hdr->size;
219 skb_pull(temp, IPC_ROUTER_HDR_SIZE);
220 skb_queue_walk(msg_head, temp) {
221 copy_len = data_len < temp->len ? data_len : temp->len;
222 if (copy_to_user(m->msg_iov->iov_base + offset, temp->data,
223 copy_len)) {
224 pr_err("%s: Copy to user failed\n", __func__);
225 return -EFAULT;
226 }
227 offset += copy_len;
228 data_len -= copy_len;
229 }
230 return offset;
231}
232
233static void msm_ipc_router_release_msg(struct sk_buff_head *msg_head)
234{
235 struct sk_buff *temp;
236
237 if (!msg_head) {
238 pr_err("%s: Invalid msg pointer\n", __func__);
239 return;
240 }
241
242 while (!skb_queue_empty(msg_head)) {
243 temp = skb_dequeue(msg_head);
244 kfree_skb(temp);
245 }
246 kfree(msg_head);
247}
248
249static int msm_ipc_router_create(struct net *net,
250 struct socket *sock,
251 int protocol,
252 int kern)
253{
254 struct sock *sk;
255 struct msm_ipc_port *port_ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256
257 if (unlikely(protocol != 0)) {
258 pr_err("%s: Protocol not supported\n", __func__);
259 return -EPROTONOSUPPORT;
260 }
261
262 switch (sock->type) {
263 case SOCK_DGRAM:
264 break;
265 default:
266 pr_err("%s: Protocol type not supported\n", __func__);
267 return -EPROTOTYPE;
268 }
269
270 sk = sk_alloc(net, AF_MSM_IPC, GFP_KERNEL, &msm_ipc_proto);
271 if (!sk) {
272 pr_err("%s: sk_alloc failed\n", __func__);
273 return -ENOMEM;
274 }
275
276 port_ptr = msm_ipc_router_create_raw_port(sk, NULL, NULL);
277 if (!port_ptr) {
278 pr_err("%s: port_ptr alloc failed\n", __func__);
279 sk_free(sk);
280 return -ENOMEM;
281 }
282
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -0600283 port_ptr->check_send_permissions = msm_ipc_check_send_permissions;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700284 sock->ops = &msm_ipc_proto_ops;
285 sock_init_data(sock, sk);
286 sk->sk_rcvtimeo = DEFAULT_RCV_TIMEO;
287
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288 msm_ipc_sk(sk)->port = port_ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289
290 return 0;
291}
292
293int msm_ipc_router_bind(struct socket *sock, struct sockaddr *uaddr,
294 int uaddr_len)
295{
296 struct sockaddr_msm_ipc *addr = (struct sockaddr_msm_ipc *)uaddr;
297 struct sock *sk = sock->sk;
298 struct msm_ipc_port *port_ptr;
299 int ret;
Karthikeyan Ramasubramanianaf90ba82013-06-03 12:05:50 -0600300 void *pil;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700301
302 if (!sk)
303 return -EINVAL;
304
Karthikeyan Ramasubramanian54426332013-01-24 11:45:41 -0700305 if (!check_permissions()) {
306 pr_err("%s: %s Do not have permissions\n",
307 __func__, current->comm);
308 return -EPERM;
309 }
310
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700311 if (!uaddr_len) {
312 pr_err("%s: Invalid address length\n", __func__);
313 return -EINVAL;
314 }
315
316 if (addr->family != AF_MSM_IPC) {
317 pr_err("%s: Address family is incorrect\n", __func__);
318 return -EAFNOSUPPORT;
319 }
320
321 if (addr->address.addrtype != MSM_IPC_ADDR_NAME) {
322 pr_err("%s: Address type is incorrect\n", __func__);
323 return -EINVAL;
324 }
325
326 port_ptr = msm_ipc_sk_port(sk);
327 if (!port_ptr)
328 return -ENODEV;
329
Karthikeyan Ramasubramanianaf90ba82013-06-03 12:05:50 -0600330 pil = msm_ipc_load_default_node();
331 msm_ipc_sk(sk)->default_pil = pil;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332 lock_sock(sk);
333
334 ret = msm_ipc_router_register_server(port_ptr, &addr->address);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335
336 release_sock(sk);
337 return ret;
338}
339
340static int msm_ipc_router_sendmsg(struct kiocb *iocb, struct socket *sock,
341 struct msghdr *m, size_t total_len)
342{
343 struct sock *sk = sock->sk;
344 struct msm_ipc_port *port_ptr = msm_ipc_sk_port(sk);
345 struct sockaddr_msm_ipc *dest = (struct sockaddr_msm_ipc *)m->msg_name;
346 struct sk_buff_head *msg;
Zaheerulla Meerf0707d52013-02-21 17:56:18 +0530347 struct sk_buff *ipc_buf;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348 int ret;
349
350 if (!dest)
351 return -EDESTADDRREQ;
352
353 if (m->msg_namelen < sizeof(*dest) || dest->family != AF_MSM_IPC)
354 return -EINVAL;
355
356 if (total_len > MAX_IPC_PKT_SIZE)
357 return -EINVAL;
358
359 lock_sock(sk);
360 msg = msm_ipc_router_build_msg(m->msg_iovlen, m->msg_iov, total_len);
361 if (!msg) {
362 pr_err("%s: Msg build failure\n", __func__);
363 ret = -ENOMEM;
364 goto out_sendmsg;
365 }
366
Karthikeyan Ramasubramaniand6dbfae2013-01-16 09:00:28 -0700367 if (port_ptr->type == CLIENT_PORT)
368 wait_for_irsc_completion();
Zaheerulla Meerf0707d52013-02-21 17:56:18 +0530369 ipc_buf = skb_peek(msg);
370 msm_ipc_router_ipc_log(IPC_SEND, ipc_buf, port_ptr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371 ret = msm_ipc_router_send_to(port_ptr, msg, &dest->address);
372 if (ret == (IPC_ROUTER_HDR_SIZE + total_len))
373 ret = total_len;
374
375out_sendmsg:
376 release_sock(sk);
377 return ret;
378}
379
380static int msm_ipc_router_recvmsg(struct kiocb *iocb, struct socket *sock,
381 struct msghdr *m, size_t buf_len, int flags)
382{
383 struct sock *sk = sock->sk;
384 struct msm_ipc_port *port_ptr = msm_ipc_sk_port(sk);
385 struct sk_buff_head *msg;
Zaheerulla Meerf0707d52013-02-21 17:56:18 +0530386 struct sk_buff *ipc_buf;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387 long timeout;
388 int ret;
389
390 if (m->msg_iovlen != 1)
391 return -EOPNOTSUPP;
392
393 if (!buf_len)
394 return -EINVAL;
395
396 lock_sock(sk);
397 timeout = sk->sk_rcvtimeo;
398 mutex_lock(&port_ptr->port_rx_q_lock);
399 while (list_empty(&port_ptr->port_rx_q)) {
400 mutex_unlock(&port_ptr->port_rx_q_lock);
401 release_sock(sk);
402 if (timeout < 0) {
403 ret = wait_event_interruptible(
404 port_ptr->port_rx_wait_q,
405 !list_empty(&port_ptr->port_rx_q));
406 if (ret)
407 return ret;
408 } else if (timeout > 0) {
409 timeout = wait_event_interruptible_timeout(
410 port_ptr->port_rx_wait_q,
411 !list_empty(&port_ptr->port_rx_q),
412 timeout);
413 if (timeout < 0)
414 return -EFAULT;
415 }
416
417 if (timeout == 0)
418 return -ETIMEDOUT;
419 lock_sock(sk);
420 mutex_lock(&port_ptr->port_rx_q_lock);
421 }
422 mutex_unlock(&port_ptr->port_rx_q_lock);
423
424 ret = msm_ipc_router_read(port_ptr, &msg, buf_len);
425 if (ret <= 0 || !msg) {
426 release_sock(sk);
427 return ret;
428 }
429
430 ret = msm_ipc_router_extract_msg(m, msg);
Zaheerulla Meerf0707d52013-02-21 17:56:18 +0530431 ipc_buf = skb_peek(msg);
432 msm_ipc_router_ipc_log(IPC_RECV, ipc_buf, port_ptr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433 msm_ipc_router_release_msg(msg);
434 msg = NULL;
435 release_sock(sk);
436 return ret;
437}
438
439static int msm_ipc_router_ioctl(struct socket *sock,
440 unsigned int cmd, unsigned long arg)
441{
442 struct sock *sk = sock->sk;
443 struct msm_ipc_port *port_ptr;
444 struct server_lookup_args server_arg;
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -0600445 struct msm_ipc_server_info *srv_info = NULL;
446 unsigned int n, srv_info_sz = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447 int ret;
Karthikeyan Ramasubramanianaf90ba82013-06-03 12:05:50 -0600448 void *pil;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449
450 if (!sk)
451 return -EINVAL;
452
453 lock_sock(sk);
454 port_ptr = msm_ipc_sk_port(sock->sk);
455 if (!port_ptr) {
456 release_sock(sk);
457 return -EINVAL;
458 }
459
460 switch (cmd) {
461 case IPC_ROUTER_IOCTL_GET_VERSION:
462 n = IPC_ROUTER_VERSION;
463 ret = put_user(n, (unsigned int *)arg);
464 break;
465
466 case IPC_ROUTER_IOCTL_GET_MTU:
467 n = (MAX_IPC_PKT_SIZE - IPC_ROUTER_HDR_SIZE);
468 ret = put_user(n, (unsigned int *)arg);
469 break;
470
471 case IPC_ROUTER_IOCTL_GET_CURR_PKT_SIZE:
472 ret = msm_ipc_router_get_curr_pkt_size(port_ptr);
473 break;
474
475 case IPC_ROUTER_IOCTL_LOOKUP_SERVER:
Karthikeyan Ramasubramanianaf90ba82013-06-03 12:05:50 -0600476 pil = msm_ipc_load_default_node();
477 msm_ipc_sk(sk)->default_pil = pil;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478 ret = copy_from_user(&server_arg, (void *)arg,
479 sizeof(server_arg));
480 if (ret) {
481 ret = -EFAULT;
482 break;
483 }
484
485 if (server_arg.num_entries_in_array < 0) {
486 ret = -EINVAL;
487 break;
488 }
489 if (server_arg.num_entries_in_array) {
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -0600490 srv_info_sz = server_arg.num_entries_in_array *
491 sizeof(*srv_info);
Karthikeyan Ramasubramanianabe0b062013-02-26 16:37:50 -0700492 if ((srv_info_sz / sizeof(*srv_info)) !=
493 server_arg.num_entries_in_array) {
494 pr_err("%s: Integer Overflow %d * %d\n",
495 __func__, sizeof(*srv_info),
496 server_arg.num_entries_in_array);
497 ret = -EINVAL;
498 break;
499 }
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -0600500 srv_info = kmalloc(srv_info_sz, GFP_KERNEL);
501 if (!srv_info) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 ret = -ENOMEM;
503 break;
504 }
505 }
506 ret = msm_ipc_router_lookup_server_name(&server_arg.port_name,
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -0600507 srv_info, server_arg.num_entries_in_array,
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -0600508 server_arg.lookup_mask);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700509 if (ret < 0) {
510 pr_err("%s: Server not found\n", __func__);
511 ret = -ENODEV;
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -0600512 kfree(srv_info);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513 break;
514 }
515 server_arg.num_entries_found = ret;
516
517 ret = copy_to_user((void *)arg, &server_arg,
518 sizeof(server_arg));
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -0600519 if (srv_info_sz) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520 ret = copy_to_user((void *)(arg + sizeof(server_arg)),
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -0600521 srv_info, srv_info_sz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700522 if (ret)
523 ret = -EFAULT;
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -0600524 kfree(srv_info);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700525 }
526 break;
527
528 case IPC_ROUTER_IOCTL_BIND_CONTROL_PORT:
529 ret = msm_ipc_router_bind_control_port(port_ptr);
530 break;
531
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -0600532 case IPC_ROUTER_IOCTL_CONFIG_SEC_RULES:
533 ret = msm_ipc_config_sec_rules((void *)arg);
Karthikeyan Ramasubramaniand6dbfae2013-01-16 09:00:28 -0700534 if (ret != -EPERM)
535 port_ptr->type = IRSC_PORT;
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -0600536 break;
537
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538 default:
539 ret = -EINVAL;
540 }
541 release_sock(sk);
542 return ret;
543}
544
545static unsigned int msm_ipc_router_poll(struct file *file,
546 struct socket *sock, poll_table *wait)
547{
548 struct sock *sk = sock->sk;
549 struct msm_ipc_port *port_ptr;
550 uint32_t mask = 0;
551
552 if (!sk)
553 return -EINVAL;
554
555 port_ptr = msm_ipc_sk_port(sk);
556 if (!port_ptr)
557 return -EINVAL;
558
559 poll_wait(file, &port_ptr->port_rx_wait_q, wait);
560
561 if (!list_empty(&port_ptr->port_rx_q))
562 mask |= (POLLRDNORM | POLLIN);
563
564 return mask;
565}
566
567static int msm_ipc_router_close(struct socket *sock)
568{
569 struct sock *sk = sock->sk;
570 struct msm_ipc_port *port_ptr = msm_ipc_sk_port(sk);
Karthikeyan Ramasubramanianaf90ba82013-06-03 12:05:50 -0600571 void *pil = msm_ipc_sk(sk)->default_pil;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572 int ret;
573
574 lock_sock(sk);
575 ret = msm_ipc_router_close_port(port_ptr);
Karthikeyan Ramasubramanianaf90ba82013-06-03 12:05:50 -0600576 if (pil)
577 msm_ipc_unload_default_node(pil);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578 release_sock(sk);
579 sock_put(sk);
580 sock->sk = NULL;
581
582 return ret;
583}
584
585static const struct net_proto_family msm_ipc_family_ops = {
586 .owner = THIS_MODULE,
587 .family = AF_MSM_IPC,
588 .create = msm_ipc_router_create
589};
590
591static const struct proto_ops msm_ipc_proto_ops = {
592 .owner = THIS_MODULE,
593 .family = AF_MSM_IPC,
594 .bind = msm_ipc_router_bind,
595 .connect = sock_no_connect,
596 .sendmsg = msm_ipc_router_sendmsg,
597 .recvmsg = msm_ipc_router_recvmsg,
598 .ioctl = msm_ipc_router_ioctl,
599 .poll = msm_ipc_router_poll,
600 .setsockopt = sock_no_setsockopt,
601 .getsockopt = sock_no_getsockopt,
602 .release = msm_ipc_router_close,
603};
604
605static struct proto msm_ipc_proto = {
606 .name = "MSM_IPC",
607 .owner = THIS_MODULE,
608 .obj_size = sizeof(struct msm_ipc_sock),
609};
610
Zaheerulla Meerf0707d52013-02-21 17:56:18 +0530611/**
612 * msm_ipc_router_ipc_log_init() - Init function for IPC Logging
613 *
614 * Initialize the buffers to be used to provide the log information
615 * pertaining to the request, response and indication data flow that
616 * happens between user and kernel spaces.
617 */
618void msm_ipc_router_ipc_log_init(void)
619{
620 ipc_req_resp_log_txt =
Zaheerulla Meercbd869b2013-03-08 17:31:23 +0530621 ipc_log_context_create(REQ_RESP_IPC_LOG_PAGES,
622 "ipc_rtr_req_resp");
Zaheerulla Meerf0707d52013-02-21 17:56:18 +0530623 if (!ipc_req_resp_log_txt) {
624 pr_err("%s: Unable to create IPC logging for Req/Resp",
625 __func__);
626 }
627 ipc_ind_log_txt =
Zaheerulla Meercbd869b2013-03-08 17:31:23 +0530628 ipc_log_context_create(IND_IPC_LOG_PAGES, "ipc_rtr_ind");
Zaheerulla Meerf0707d52013-02-21 17:56:18 +0530629 if (!ipc_ind_log_txt) {
630 pr_err("%s: Unable to create IPC logging for Indications",
631 __func__);
632 }
633}
634
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635int msm_ipc_router_init_sockets(void)
636{
637 int ret;
638
639 ret = proto_register(&msm_ipc_proto, 1);
640 if (ret) {
641 pr_err("Failed to register MSM_IPC protocol type\n");
642 goto out_init_sockets;
643 }
644
645 ret = sock_register(&msm_ipc_family_ops);
646 if (ret) {
647 pr_err("Failed to register MSM_IPC socket type\n");
648 proto_unregister(&msm_ipc_proto);
649 goto out_init_sockets;
650 }
651
652 sockets_enabled = 1;
Zaheerulla Meerf0707d52013-02-21 17:56:18 +0530653 msm_ipc_router_ipc_log_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654out_init_sockets:
655 return ret;
656}
657
658void msm_ipc_router_exit_sockets(void)
659{
660 if (!sockets_enabled)
661 return;
662
663 sockets_enabled = 0;
664 sock_unregister(msm_ipc_family_ops.family);
665 proto_unregister(&msm_ipc_proto);
666}