blob: 928e59b098594ae5316133aa7add7a36ff742edb [file] [log] [blame]
Jeff Hugo31f83b42012-01-25 15:15:26 -07001/* Copyright (c) 2008-2012, Code Aurora Forum. 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/*
14 * SMD Packet Driver -- Provides a binary SMD non-muxed packet port
15 * interface.
16 */
17
18#include <linux/slab.h>
19#include <linux/cdev.h>
20#include <linux/module.h>
21#include <linux/fs.h>
22#include <linux/device.h>
23#include <linux/sched.h>
24#include <linux/spinlock.h>
25#include <linux/mutex.h>
26#include <linux/delay.h>
27#include <linux/uaccess.h>
28#include <linux/workqueue.h>
29#include <linux/platform_device.h>
30#include <linux/completion.h>
31#include <linux/msm_smd_pkt.h>
32#include <linux/poll.h>
33#include <asm/ioctls.h>
Eric Holmbergc3c5cd92012-02-07 18:19:49 -070034#include <linux/wakelock.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035
36#include <mach/msm_smd.h>
37#include <mach/peripheral-loader.h>
Karthikeyan Ramasubramanian085bd622012-09-10 21:45:00 -060038#include <mach/msm_ipc_logging.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039
40#include "smd_private.h"
41#ifdef CONFIG_ARCH_FSM9XXX
42#define NUM_SMD_PKT_PORTS 4
43#else
Karthikeyan Ramasubramanianb7e72732012-05-22 12:31:31 -060044#define NUM_SMD_PKT_PORTS 15
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045#endif
46
Karthikeyan Ramasubramanian5fa88252012-04-19 23:45:08 -060047#define PDRIVER_NAME_MAX_SIZE 32
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048#define LOOPBACK_INX (NUM_SMD_PKT_PORTS - 1)
49
50#define DEVICE_NAME "smdpkt"
Eric Holmbergc3c5cd92012-02-07 18:19:49 -070051#define WAKELOCK_TIMEOUT (2*HZ)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052
53struct smd_pkt_dev {
54 struct cdev cdev;
55 struct device *devicep;
56 void *pil;
Karthikeyan Ramasubramanian5fa88252012-04-19 23:45:08 -060057 char pdriver_name[PDRIVER_NAME_MAX_SIZE];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070058 struct platform_driver driver;
59
60 struct smd_channel *ch;
61 struct mutex ch_lock;
62 struct mutex rx_lock;
63 struct mutex tx_lock;
64 wait_queue_head_t ch_read_wait_queue;
65 wait_queue_head_t ch_write_wait_queue;
66 wait_queue_head_t ch_opened_wait_queue;
67
68 int i;
Karthikeyan Ramasubramanian540dbf32012-09-14 13:16:50 -060069 int ref_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070070
71 int blocking_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072 int is_open;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -070073 int poll_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074 unsigned ch_size;
75 uint open_modem_wait;
76
77 int has_reset;
78 int do_reset_notification;
79 struct completion ch_allocated;
Eric Holmbergc3c5cd92012-02-07 18:19:49 -070080 struct wake_lock pa_wake_lock; /* Packet Arrival Wake lock*/
81 struct work_struct packet_arrival_work;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -070082 struct spinlock pa_spinlock;
Jeff Hugo93476a32012-06-01 14:32:49 -060083 int wakelock_locked;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084} *smd_pkt_devp[NUM_SMD_PKT_PORTS];
85
86struct class *smd_pkt_classp;
87static dev_t smd_pkt_number;
88static struct delayed_work loopback_work;
89static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp);
90static void check_and_wakeup_writer(struct smd_pkt_dev *smd_pkt_devp);
91static uint32_t is_modem_smsm_inited(void);
92
Karthikeyan Ramasubramanian085bd622012-09-10 21:45:00 -060093#define SMD_PKT_IPC_LOG_PAGE_CNT 2
94static void *smd_pkt_ilctxt;
95
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096static int msm_smd_pkt_debug_mask;
97module_param_named(debug_mask, msm_smd_pkt_debug_mask,
98 int, S_IRUGO | S_IWUSR | S_IWGRP);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -060099
100enum {
101 SMD_PKT_STATUS = 1U << 0,
102 SMD_PKT_READ = 1U << 1,
103 SMD_PKT_WRITE = 1U << 2,
104 SMD_PKT_READ_DUMP_BUFFER = 1U << 3,
105 SMD_PKT_WRITE_DUMP_BUFFER = 1U << 4,
106 SMD_PKT_POLL = 1U << 5,
107};
108
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109#define DEBUG
110
111#ifdef DEBUG
Karthikeyan Ramasubramanian085bd622012-09-10 21:45:00 -0600112
113#define SMD_PKT_LOG_STRING(x...) \
114do { \
115 if (smd_pkt_ilctxt) \
116 ipc_log_string(smd_pkt_ilctxt, "<SMD_PKT>: "x); \
117} while (0)
118
119#define SMD_PKT_LOG_BUF(buf, cnt) \
120do { \
121 char log_buf[128]; \
122 int i; \
123 if (smd_pkt_ilctxt) { \
124 i = cnt < 16 ? cnt : 16; \
125 hex_dump_to_buffer(buf, i, 16, 1, log_buf, \
126 sizeof(log_buf), false); \
127 ipc_log_string(smd_pkt_ilctxt, "<SMD_PKT>: %s", log_buf); \
128 } \
129} while (0)
130
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600131#define D_STATUS(x...) \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132do { \
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600133 if (msm_smd_pkt_debug_mask & SMD_PKT_STATUS) \
134 pr_info("Status: "x); \
Karthikeyan Ramasubramanian085bd622012-09-10 21:45:00 -0600135 SMD_PKT_LOG_STRING(x); \
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600136} while (0)
137
138#define D_READ(x...) \
139do { \
140 if (msm_smd_pkt_debug_mask & SMD_PKT_READ) \
141 pr_info("Read: "x); \
Karthikeyan Ramasubramanian085bd622012-09-10 21:45:00 -0600142 SMD_PKT_LOG_STRING(x); \
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600143} while (0)
144
145#define D_WRITE(x...) \
146do { \
147 if (msm_smd_pkt_debug_mask & SMD_PKT_WRITE) \
148 pr_info("Write: "x); \
Karthikeyan Ramasubramanian085bd622012-09-10 21:45:00 -0600149 SMD_PKT_LOG_STRING(x); \
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600150} while (0)
151
152#define D_READ_DUMP_BUFFER(prestr, cnt, buf) \
153do { \
154 if (msm_smd_pkt_debug_mask & SMD_PKT_READ_DUMP_BUFFER) \
155 print_hex_dump(KERN_INFO, prestr, \
156 DUMP_PREFIX_NONE, 16, 1, \
157 buf, cnt, 1); \
Karthikeyan Ramasubramanian085bd622012-09-10 21:45:00 -0600158 SMD_PKT_LOG_BUF(buf, cnt); \
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600159} while (0)
160
161#define D_WRITE_DUMP_BUFFER(prestr, cnt, buf) \
162do { \
163 if (msm_smd_pkt_debug_mask & SMD_PKT_WRITE_DUMP_BUFFER) \
164 print_hex_dump(KERN_INFO, prestr, \
165 DUMP_PREFIX_NONE, 16, 1, \
166 buf, cnt, 1); \
Karthikeyan Ramasubramanian085bd622012-09-10 21:45:00 -0600167 SMD_PKT_LOG_BUF(buf, cnt); \
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600168} while (0)
169
170#define D_POLL(x...) \
171do { \
172 if (msm_smd_pkt_debug_mask & SMD_PKT_POLL) \
173 pr_info("Poll: "x); \
Karthikeyan Ramasubramanian085bd622012-09-10 21:45:00 -0600174 SMD_PKT_LOG_STRING(x); \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700175} while (0)
Karthikeyan Ramasubramaniana6dc4b02012-09-07 14:20:06 -0600176
177#define E_SMD_PKT_SSR(x) \
178do { \
179 if (x->do_reset_notification) \
180 pr_err("%s notifying reset for smd_pkt_dev id:%d\n", \
181 __func__, x->i); \
182} while (0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700183#else
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600184#define D_STATUS(x...) do {} while (0)
185#define D_READ(x...) do {} while (0)
186#define D_WRITE(x...) do {} while (0)
187#define D_READ_DUMP_BUFFER(prestr, cnt, buf) do {} while (0)
188#define D_WRITE_DUMP_BUFFER(prestr, cnt, buf) do {} while (0)
189#define D_POLL(x...) do {} while (0)
Karthikeyan Ramasubramaniana6dc4b02012-09-07 14:20:06 -0600190#define E_SMD_PKT_SSR(x) do {} while (0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191#endif
192
193static ssize_t open_timeout_store(struct device *d,
194 struct device_attribute *attr,
195 const char *buf,
196 size_t n)
197{
198 int i;
199 unsigned long tmp;
200 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
201 if (smd_pkt_devp[i]->devicep == d)
202 break;
203 }
Jeff Hugo31f83b42012-01-25 15:15:26 -0700204 if (i >= NUM_SMD_PKT_PORTS) {
205 pr_err("%s: unable to match device to valid smd_pkt port\n",
206 __func__);
207 return -EINVAL;
208 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700209 if (!strict_strtoul(buf, 10, &tmp)) {
210 smd_pkt_devp[i]->open_modem_wait = tmp;
211 return n;
212 } else {
213 pr_err("%s: unable to convert: %s to an int\n", __func__,
214 buf);
215 return -EINVAL;
216 }
217}
218
219static ssize_t open_timeout_show(struct device *d,
220 struct device_attribute *attr,
221 char *buf)
222{
223 int i;
224 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
225 if (smd_pkt_devp[i]->devicep == d)
226 break;
227 }
Jeff Hugo31f83b42012-01-25 15:15:26 -0700228 if (i >= NUM_SMD_PKT_PORTS) {
229 pr_err("%s: unable to match device to valid smd_pkt port\n",
230 __func__);
231 return -EINVAL;
232 }
Karthikeyan Ramasubramanian63fa3d32011-09-29 17:06:26 -0600233 return snprintf(buf, PAGE_SIZE, "%d\n",
234 smd_pkt_devp[i]->open_modem_wait);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235}
236
237static DEVICE_ATTR(open_timeout, 0664, open_timeout_show, open_timeout_store);
238
239static int notify_reset(struct smd_pkt_dev *smd_pkt_devp)
240{
241 smd_pkt_devp->do_reset_notification = 0;
242
243 return -ENETRESET;
244}
245
246static void clean_and_signal(struct smd_pkt_dev *smd_pkt_devp)
247{
248 smd_pkt_devp->do_reset_notification = 1;
249 smd_pkt_devp->has_reset = 1;
250
251 smd_pkt_devp->is_open = 0;
252
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700253 wake_up(&smd_pkt_devp->ch_read_wait_queue);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700254 wake_up(&smd_pkt_devp->ch_write_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600256 D_STATUS("%s smd_pkt_dev id:%d\n", __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257}
258
259static void loopback_probe_worker(struct work_struct *work)
260{
261
262 /* Wait for the modem SMSM to be inited for the SMD
263 ** Loopback channel to be allocated at the modem. Since
264 ** the wait need to be done atmost once, using msleep
265 ** doesn't degrade the performance. */
266 if (!is_modem_smsm_inited())
267 schedule_delayed_work(&loopback_work, msecs_to_jiffies(1000));
268 else
269 smsm_change_state(SMSM_APPS_STATE,
270 0, SMSM_SMD_LOOPBACK);
271
272}
273
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700274static void packet_arrival_worker(struct work_struct *work)
275{
276 struct smd_pkt_dev *smd_pkt_devp;
Jeff Hugo93476a32012-06-01 14:32:49 -0600277 unsigned long flags;
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700278
279 smd_pkt_devp = container_of(work, struct smd_pkt_dev,
280 packet_arrival_work);
281 mutex_lock(&smd_pkt_devp->ch_lock);
Jeff Hugo93476a32012-06-01 14:32:49 -0600282 spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags);
283 if (smd_pkt_devp->ch && smd_pkt_devp->wakelock_locked) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600284 D_READ("%s locking smd_pkt_dev id:%d wakelock\n",
285 __func__, smd_pkt_devp->i);
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700286 wake_lock_timeout(&smd_pkt_devp->pa_wake_lock,
287 WAKELOCK_TIMEOUT);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600288 }
Jeff Hugo93476a32012-06-01 14:32:49 -0600289 spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags);
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700290 mutex_unlock(&smd_pkt_devp->ch_lock);
291}
292
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293static long smd_pkt_ioctl(struct file *file, unsigned int cmd,
294 unsigned long arg)
295{
296 int ret;
297 struct smd_pkt_dev *smd_pkt_devp;
298
299 smd_pkt_devp = file->private_data;
300 if (!smd_pkt_devp)
301 return -EINVAL;
302
Karthikeyan Ramasubramanian540dbf32012-09-14 13:16:50 -0600303 mutex_lock(&smd_pkt_devp->ch_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700304 switch (cmd) {
305 case TIOCMGET:
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600306 D_STATUS("%s TIOCMGET command on smd_pkt_dev id:%d\n",
307 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308 ret = smd_tiocmget(smd_pkt_devp->ch);
309 break;
310 case TIOCMSET:
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600311 D_STATUS("%s TIOCSET command on smd_pkt_dev id:%d\n",
312 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313 ret = smd_tiocmset(smd_pkt_devp->ch, arg, ~arg);
314 break;
315 case SMD_PKT_IOCTL_BLOCKING_WRITE:
316 ret = get_user(smd_pkt_devp->blocking_write, (int *)arg);
317 break;
318 default:
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600319 pr_err("%s: Unrecognized ioctl command %d\n", __func__, cmd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320 ret = -1;
321 }
Karthikeyan Ramasubramanian540dbf32012-09-14 13:16:50 -0600322 mutex_unlock(&smd_pkt_devp->ch_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700323
324 return ret;
325}
326
327ssize_t smd_pkt_read(struct file *file,
328 char __user *buf,
329 size_t count,
330 loff_t *ppos)
331{
332 int r;
333 int bytes_read;
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700334 int pkt_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335 struct smd_pkt_dev *smd_pkt_devp;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700336 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700337
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 smd_pkt_devp = file->private_data;
339
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600340 if (!smd_pkt_devp) {
341 pr_err("%s on NULL smd_pkt_dev\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342 return -EINVAL;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600343 }
344
345 if (!smd_pkt_devp->ch) {
346 pr_err("%s on a closed smd_pkt_dev id:%d\n",
347 __func__, smd_pkt_devp->i);
348 return -EINVAL;
349 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350
351 if (smd_pkt_devp->do_reset_notification) {
352 /* notify client that a reset occurred */
Karthikeyan Ramasubramaniana6dc4b02012-09-07 14:20:06 -0600353 E_SMD_PKT_SSR(smd_pkt_devp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700354 return notify_reset(smd_pkt_devp);
355 }
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600356 D_READ("Begin %s on smd_pkt_dev id:%d buffer_size %d\n",
357 __func__, smd_pkt_devp->i, count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700358
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700359wait_for_packet:
360 r = wait_event_interruptible(smd_pkt_devp->ch_read_wait_queue,
Karthikeyan Ramasubramanian96a14fb2012-04-30 12:04:35 -0600361 !smd_pkt_devp->ch ||
362 (smd_cur_packet_size(smd_pkt_devp->ch) > 0
363 && smd_read_avail(smd_pkt_devp->ch)) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364 smd_pkt_devp->has_reset);
365
Karthikeyan Ramasubramanian96a14fb2012-04-30 12:04:35 -0600366 mutex_lock(&smd_pkt_devp->rx_lock);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600367 if (smd_pkt_devp->has_reset) {
Karthikeyan Ramasubramanian96a14fb2012-04-30 12:04:35 -0600368 mutex_unlock(&smd_pkt_devp->rx_lock);
Karthikeyan Ramasubramaniana6dc4b02012-09-07 14:20:06 -0600369 E_SMD_PKT_SSR(smd_pkt_devp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700370 return notify_reset(smd_pkt_devp);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600371 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372
Karthikeyan Ramasubramanian96a14fb2012-04-30 12:04:35 -0600373 if (!smd_pkt_devp->ch) {
374 mutex_unlock(&smd_pkt_devp->rx_lock);
375 pr_err("%s on a closed smd_pkt_dev id:%d\n",
376 __func__, smd_pkt_devp->i);
377 return -EINVAL;
378 }
379
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700380 if (r < 0) {
Karthikeyan Ramasubramanian96a14fb2012-04-30 12:04:35 -0600381 mutex_unlock(&smd_pkt_devp->rx_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700382 /* qualify error message */
383 if (r != -ERESTARTSYS) {
384 /* we get this anytime a signal comes in */
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600385 pr_err("%s: wait_event_interruptible on smd_pkt_dev"
386 " id:%d ret %i\n",
387 __func__, smd_pkt_devp->i, r);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388 }
389 return r;
390 }
391
392 /* Here we have a whole packet waiting for us */
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700393 pkt_size = smd_cur_packet_size(smd_pkt_devp->ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700395 if (!pkt_size) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600396 pr_err("%s: No data on smd_pkt_dev id:%d, False wakeup\n",
397 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700398 mutex_unlock(&smd_pkt_devp->rx_lock);
399 goto wait_for_packet;
400 }
401
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700402 if (pkt_size > count) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600403 pr_err("%s: failure on smd_pkt_dev id: %d - packet size %d"
404 " > buffer size %d,", __func__, smd_pkt_devp->i,
405 pkt_size, count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406 mutex_unlock(&smd_pkt_devp->rx_lock);
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700407 return -ETOOSMALL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408 }
409
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700410 bytes_read = 0;
411 do {
412 r = smd_read_user_buffer(smd_pkt_devp->ch,
413 (buf + bytes_read),
414 (pkt_size - bytes_read));
415 if (r < 0) {
416 mutex_unlock(&smd_pkt_devp->rx_lock);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600417 if (smd_pkt_devp->has_reset) {
Karthikeyan Ramasubramaniana6dc4b02012-09-07 14:20:06 -0600418 E_SMD_PKT_SSR(smd_pkt_devp);
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700419 return notify_reset(smd_pkt_devp);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600420 }
421 pr_err("%s Error while reading %d\n", __func__, r);
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700422 return r;
423 }
424 bytes_read += r;
425 if (pkt_size != bytes_read)
426 wait_event(smd_pkt_devp->ch_read_wait_queue,
427 smd_read_avail(smd_pkt_devp->ch) ||
428 smd_pkt_devp->has_reset);
429 if (smd_pkt_devp->has_reset) {
430 mutex_unlock(&smd_pkt_devp->rx_lock);
Karthikeyan Ramasubramaniana6dc4b02012-09-07 14:20:06 -0600431 E_SMD_PKT_SSR(smd_pkt_devp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432 return notify_reset(smd_pkt_devp);
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700433 }
434 } while (pkt_size != bytes_read);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600435 D_READ_DUMP_BUFFER("Read: ", (bytes_read > 16 ? 16 : bytes_read), buf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436 mutex_unlock(&smd_pkt_devp->rx_lock);
437
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700438 mutex_lock(&smd_pkt_devp->ch_lock);
439 spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags);
440 if (smd_pkt_devp->poll_mode &&
441 !smd_cur_packet_size(smd_pkt_devp->ch)) {
442 wake_unlock(&smd_pkt_devp->pa_wake_lock);
Jeff Hugo93476a32012-06-01 14:32:49 -0600443 smd_pkt_devp->wakelock_locked = 0;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700444 smd_pkt_devp->poll_mode = 0;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600445 D_READ("%s unlocked smd_pkt_dev id:%d wakelock\n",
446 __func__, smd_pkt_devp->i);
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700447 }
448 spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags);
449 mutex_unlock(&smd_pkt_devp->ch_lock);
450
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600451 D_READ("Finished %s on smd_pkt_dev id:%d %d bytes\n",
452 __func__, smd_pkt_devp->i, bytes_read);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453
454 /* check and wakeup read threads waiting on this device */
455 check_and_wakeup_reader(smd_pkt_devp);
456
457 return bytes_read;
458}
459
460ssize_t smd_pkt_write(struct file *file,
461 const char __user *buf,
462 size_t count,
463 loff_t *ppos)
464{
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700465 int r = 0, bytes_written;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466 struct smd_pkt_dev *smd_pkt_devp;
467 DEFINE_WAIT(write_wait);
468
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469 smd_pkt_devp = file->private_data;
470
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600471 if (!smd_pkt_devp) {
472 pr_err("%s on NULL smd_pkt_dev\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700473 return -EINVAL;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600474 }
475
476 if (!smd_pkt_devp->ch) {
477 pr_err("%s on a closed smd_pkt_dev id:%d\n",
478 __func__, smd_pkt_devp->i);
479 return -EINVAL;
480 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481
Karthikeyan Ramasubramanian762e52a2012-04-30 12:52:14 -0600482 if (smd_pkt_devp->do_reset_notification || smd_pkt_devp->has_reset) {
Karthikeyan Ramasubramaniana6dc4b02012-09-07 14:20:06 -0600483 E_SMD_PKT_SSR(smd_pkt_devp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484 /* notify client that a reset occurred */
485 return notify_reset(smd_pkt_devp);
486 }
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600487 D_WRITE("Begin %s on smd_pkt_dev id:%d data_size %d\n",
488 __func__, smd_pkt_devp->i, count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700490 mutex_lock(&smd_pkt_devp->tx_lock);
491 if (!smd_pkt_devp->blocking_write) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 if (smd_write_avail(smd_pkt_devp->ch) < count) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600493 pr_err("%s: Not enough space in smd_pkt_dev id:%d\n",
494 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495 mutex_unlock(&smd_pkt_devp->tx_lock);
496 return -ENOMEM;
497 }
498 }
499
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700500 r = smd_write_start(smd_pkt_devp->ch, count);
501 if (r < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 mutex_unlock(&smd_pkt_devp->tx_lock);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600503 pr_err("%s: Error:%d in smd_pkt_dev id:%d @ smd_write_start\n",
504 __func__, r, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 return r;
506 }
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700507
508 bytes_written = 0;
509 do {
510 prepare_to_wait(&smd_pkt_devp->ch_write_wait_queue,
511 &write_wait, TASK_UNINTERRUPTIBLE);
512 if (!smd_write_avail(smd_pkt_devp->ch) &&
513 !smd_pkt_devp->has_reset) {
514 smd_enable_read_intr(smd_pkt_devp->ch);
515 schedule();
516 }
517 finish_wait(&smd_pkt_devp->ch_write_wait_queue, &write_wait);
518 smd_disable_read_intr(smd_pkt_devp->ch);
519
520 if (smd_pkt_devp->has_reset) {
521 mutex_unlock(&smd_pkt_devp->tx_lock);
Karthikeyan Ramasubramaniana6dc4b02012-09-07 14:20:06 -0600522 E_SMD_PKT_SSR(smd_pkt_devp);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700523 return notify_reset(smd_pkt_devp);
524 } else {
525 r = smd_write_segment(smd_pkt_devp->ch,
526 (void *)(buf + bytes_written),
527 (count - bytes_written), 1);
528 if (r < 0) {
529 mutex_unlock(&smd_pkt_devp->tx_lock);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600530 if (smd_pkt_devp->has_reset) {
Karthikeyan Ramasubramaniana6dc4b02012-09-07 14:20:06 -0600531 E_SMD_PKT_SSR(smd_pkt_devp);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700532 return notify_reset(smd_pkt_devp);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600533 }
Karthikeyan Ramasubramanian96a14fb2012-04-30 12:04:35 -0600534 pr_err("%s on smd_pkt_dev id:%d failed r:%d\n",
535 __func__, smd_pkt_devp->i, r);
536 return r;
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700537 }
538 bytes_written += r;
539 }
540 } while (bytes_written != count);
541 smd_write_end(smd_pkt_devp->ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542 mutex_unlock(&smd_pkt_devp->tx_lock);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600543 D_WRITE_DUMP_BUFFER("Write: ",
544 (bytes_written > 16 ? 16 : bytes_written), buf);
545 D_WRITE("Finished %s on smd_pkt_dev id:%d %d bytes\n",
546 __func__, smd_pkt_devp->i, count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547
548 return count;
549}
550
551static unsigned int smd_pkt_poll(struct file *file, poll_table *wait)
552{
553 struct smd_pkt_dev *smd_pkt_devp;
554 unsigned int mask = 0;
555
556 smd_pkt_devp = file->private_data;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600557 if (!smd_pkt_devp) {
558 pr_err("%s on a NULL device\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 return POLLERR;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600560 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700562 smd_pkt_devp->poll_mode = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563 poll_wait(file, &smd_pkt_devp->ch_read_wait_queue, wait);
Karthikeyan Ramasubramanian762e52a2012-04-30 12:52:14 -0600564 mutex_lock(&smd_pkt_devp->ch_lock);
565 if (smd_pkt_devp->has_reset || !smd_pkt_devp->ch) {
566 mutex_unlock(&smd_pkt_devp->ch_lock);
Karthikeyan Ramasubramanian762e52a2012-04-30 12:52:14 -0600567 return POLLERR;
568 }
569
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600570 if (smd_read_avail(smd_pkt_devp->ch)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571 mask |= POLLIN | POLLRDNORM;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600572 D_POLL("%s sets POLLIN for smd_pkt_dev id: %d\n",
573 __func__, smd_pkt_devp->i);
574 }
Karthikeyan Ramasubramanian762e52a2012-04-30 12:52:14 -0600575 mutex_unlock(&smd_pkt_devp->ch_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576
577 return mask;
578}
579
580static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp)
581{
582 int sz;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700583 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700584
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600585 if (!smd_pkt_devp) {
586 pr_err("%s on a NULL device\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587 return;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600588 }
589
590 if (!smd_pkt_devp->ch) {
591 pr_err("%s on a closed smd_pkt_dev id:%d\n",
592 __func__, smd_pkt_devp->i);
593 return;
594 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595
596 sz = smd_cur_packet_size(smd_pkt_devp->ch);
597 if (sz == 0) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600598 D_READ("%s: No packet in smd_pkt_dev id:%d\n",
599 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 return;
601 }
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700602 if (!smd_read_avail(smd_pkt_devp->ch)) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600603 D_READ("%s: packet size is %d in smd_pkt_dev id:%d -"
604 " but the data isn't here\n",
605 __func__, sz, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700606 return;
607 }
608
609 /* here we have a packet of size sz ready */
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700610 spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags);
611 wake_lock(&smd_pkt_devp->pa_wake_lock);
Jeff Hugo93476a32012-06-01 14:32:49 -0600612 smd_pkt_devp->wakelock_locked = 1;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700613 spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags);
Jeff Hugo93476a32012-06-01 14:32:49 -0600614 wake_up(&smd_pkt_devp->ch_read_wait_queue);
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700615 schedule_work(&smd_pkt_devp->packet_arrival_work);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600616 D_READ("%s: wake_up smd_pkt_dev id:%d\n", __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617}
618
619static void check_and_wakeup_writer(struct smd_pkt_dev *smd_pkt_devp)
620{
621 int sz;
622
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600623 if (!smd_pkt_devp) {
624 pr_err("%s on a NULL device\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 return;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600626 }
627
628 if (!smd_pkt_devp->ch) {
629 pr_err("%s on a closed smd_pkt_dev id:%d\n",
630 __func__, smd_pkt_devp->i);
631 return;
632 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633
634 sz = smd_write_avail(smd_pkt_devp->ch);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700635 if (sz) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600636 D_WRITE("%s: %d bytes write space in smd_pkt_dev id:%d\n",
637 __func__, sz, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638 smd_disable_read_intr(smd_pkt_devp->ch);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700639 wake_up(&smd_pkt_devp->ch_write_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640 }
641}
642
643static void ch_notify(void *priv, unsigned event)
644{
645 struct smd_pkt_dev *smd_pkt_devp = priv;
646
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600647 if (smd_pkt_devp->ch == 0) {
648 pr_err("%s on a closed smd_pkt_dev id:%d\n",
649 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650 return;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600651 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700652
653 switch (event) {
654 case SMD_EVENT_DATA: {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600655 D_STATUS("%s: DATA event in smd_pkt_dev id:%d\n",
656 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657 check_and_wakeup_reader(smd_pkt_devp);
658 if (smd_pkt_devp->blocking_write)
659 check_and_wakeup_writer(smd_pkt_devp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660 break;
661 }
662 case SMD_EVENT_OPEN:
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600663 D_STATUS("%s: OPEN event in smd_pkt_dev id:%d\n",
664 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700665 smd_pkt_devp->has_reset = 0;
666 smd_pkt_devp->is_open = 1;
667 wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue);
668 break;
669 case SMD_EVENT_CLOSE:
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600670 D_STATUS("%s: CLOSE event in smd_pkt_dev id:%d\n",
671 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700672 smd_pkt_devp->is_open = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700673 /* put port into reset state */
674 clean_and_signal(smd_pkt_devp);
675 if (smd_pkt_devp->i == LOOPBACK_INX)
676 schedule_delayed_work(&loopback_work,
677 msecs_to_jiffies(1000));
678 break;
679 }
680}
681
682#ifdef CONFIG_ARCH_FSM9XXX
683static char *smd_pkt_dev_name[] = {
684 "smdcntl1",
685 "smdcntl2",
686 "smd22",
687 "smd_pkt_loopback",
688};
689
690static char *smd_ch_name[] = {
691 "DATA6_CNTL",
692 "DATA7_CNTL",
693 "DATA22",
694 "LOOPBACK",
695};
696
697static uint32_t smd_ch_edge[] = {
698 SMD_APPS_QDSP,
699 SMD_APPS_QDSP,
700 SMD_APPS_QDSP,
701 SMD_APPS_QDSP
702};
703#else
704static char *smd_pkt_dev_name[] = {
705 "smdcntl0",
706 "smdcntl1",
707 "smdcntl2",
708 "smdcntl3",
709 "smdcntl4",
710 "smdcntl5",
711 "smdcntl6",
712 "smdcntl7",
713 "smd22",
714 "smd_sns_dsps",
Angshuman Sarkara18a2722011-07-29 13:41:13 +0530715 "apr_apps2",
Karthikeyan Ramasubramaniand08e59a2012-03-01 17:35:34 -0700716 "smdcntl8",
Jeff Hugodf5ee322012-04-16 11:56:12 -0600717 "smd_sns_adsp",
Karthikeyan Ramasubramanianb7e72732012-05-22 12:31:31 -0600718 "smd_cxm_qmi",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700719 "smd_pkt_loopback",
720};
721
722static char *smd_ch_name[] = {
723 "DATA5_CNTL",
724 "DATA6_CNTL",
725 "DATA7_CNTL",
726 "DATA8_CNTL",
727 "DATA9_CNTL",
728 "DATA12_CNTL",
729 "DATA13_CNTL",
730 "DATA14_CNTL",
731 "DATA22",
732 "SENSOR",
Angshuman Sarkara18a2722011-07-29 13:41:13 +0530733 "apr_apps2",
Karthikeyan Ramasubramaniand08e59a2012-03-01 17:35:34 -0700734 "DATA40_CNTL",
Jeff Hugodf5ee322012-04-16 11:56:12 -0600735 "SENSOR",
Karthikeyan Ramasubramanianb7e72732012-05-22 12:31:31 -0600736 "CXM_QMI_PORT_8064",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737 "LOOPBACK",
738};
739
740static uint32_t smd_ch_edge[] = {
741 SMD_APPS_MODEM,
742 SMD_APPS_MODEM,
743 SMD_APPS_MODEM,
744 SMD_APPS_MODEM,
745 SMD_APPS_MODEM,
746 SMD_APPS_MODEM,
747 SMD_APPS_MODEM,
748 SMD_APPS_MODEM,
749 SMD_APPS_MODEM,
750 SMD_APPS_DSPS,
751 SMD_APPS_QDSP,
752 SMD_APPS_MODEM,
Jeff Hugodf5ee322012-04-16 11:56:12 -0600753 SMD_APPS_QDSP,
Karthikeyan Ramasubramanianb7e72732012-05-22 12:31:31 -0600754 SMD_APPS_WCNSS,
Karthikeyan Ramasubramaniand08e59a2012-03-01 17:35:34 -0700755 SMD_APPS_MODEM,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700756};
757#endif
Jeff Hugo223fd452012-07-18 14:35:45 -0600758module_param_named(loopback_edge, smd_ch_edge[LOOPBACK_INX],
759 int, S_IRUGO | S_IWUSR | S_IWGRP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700760
761static int smd_pkt_dummy_probe(struct platform_device *pdev)
762{
763 int i;
764
765 for (i = 0; i < NUM_SMD_PKT_PORTS; i++) {
Karthikeyan Ramasubramanian5fa88252012-04-19 23:45:08 -0600766 if (smd_ch_edge[i] == pdev->id
767 && !strncmp(pdev->name, smd_ch_name[i],
768 SMD_MAX_CH_NAME_LEN)
769 && smd_pkt_devp[i]->driver.probe) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770 complete_all(&smd_pkt_devp[i]->ch_allocated);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600771 D_STATUS("%s allocated SMD ch for smd_pkt_dev id:%d\n",
772 __func__, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773 break;
774 }
775 }
776 return 0;
777}
778
779static uint32_t is_modem_smsm_inited(void)
780{
781 uint32_t modem_state;
782 uint32_t ready_state = (SMSM_INIT | SMSM_SMDINIT);
783
784 modem_state = smsm_get_state(SMSM_MODEM_STATE);
785 return (modem_state & ready_state) == ready_state;
786}
787
788int smd_pkt_open(struct inode *inode, struct file *file)
789{
790 int r = 0;
791 struct smd_pkt_dev *smd_pkt_devp;
Eric Holmbergcce6daf2012-02-27 14:34:02 -0700792 const char *peripheral = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793
794 smd_pkt_devp = container_of(inode->i_cdev, struct smd_pkt_dev, cdev);
795
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600796 if (!smd_pkt_devp) {
797 pr_err("%s on a NULL device\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798 return -EINVAL;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600799 }
800 D_STATUS("Begin %s on smd_pkt_dev id:%d\n", __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801
802 file->private_data = smd_pkt_devp;
803
804 mutex_lock(&smd_pkt_devp->ch_lock);
805 if (smd_pkt_devp->ch == 0) {
Eric Holmbergb399a4f2012-07-06 11:42:24 -0600806 wake_lock_init(&smd_pkt_devp->pa_wake_lock, WAKE_LOCK_SUSPEND,
807 smd_pkt_dev_name[smd_pkt_devp->i]);
808 INIT_WORK(&smd_pkt_devp->packet_arrival_work,
809 packet_arrival_worker);
Karthikeyan Ramasubramanian63a913c2012-03-06 15:43:48 -0700810 init_completion(&smd_pkt_devp->ch_allocated);
811 smd_pkt_devp->driver.probe = smd_pkt_dummy_probe;
Karthikeyan Ramasubramanian5fa88252012-04-19 23:45:08 -0600812 scnprintf(smd_pkt_devp->pdriver_name, PDRIVER_NAME_MAX_SIZE,
Karthikeyan Ramasubramanian61df1932012-06-28 12:10:41 -0600813 "%s", smd_ch_name[smd_pkt_devp->i]);
Karthikeyan Ramasubramanian5fa88252012-04-19 23:45:08 -0600814 smd_pkt_devp->driver.driver.name = smd_pkt_devp->pdriver_name;
Karthikeyan Ramasubramanian63a913c2012-03-06 15:43:48 -0700815 smd_pkt_devp->driver.driver.owner = THIS_MODULE;
816 r = platform_driver_register(&smd_pkt_devp->driver);
817 if (r) {
818 pr_err("%s: %s Platform driver reg. failed\n",
819 __func__, smd_ch_name[smd_pkt_devp->i]);
820 goto out;
821 }
822
Eric Holmbergcce6daf2012-02-27 14:34:02 -0700823 peripheral = smd_edge_to_subsystem(
824 smd_ch_edge[smd_pkt_devp->i]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825 if (peripheral) {
826 smd_pkt_devp->pil = pil_get(peripheral);
827 if (IS_ERR(smd_pkt_devp->pil)) {
828 r = PTR_ERR(smd_pkt_devp->pil);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600829 pr_err("%s failed on smd_pkt_dev id:%d -"
830 " pil_get failed for %s\n", __func__,
831 smd_pkt_devp->i, peripheral);
Karthikeyan Ramasubramanian63a913c2012-03-06 15:43:48 -0700832 goto release_pd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 }
834
835 /* Wait for the modem SMSM to be inited for the SMD
836 ** Loopback channel to be allocated at the modem. Since
837 ** the wait need to be done atmost once, using msleep
838 ** doesn't degrade the performance. */
Jeff Hugoa5ee4362011-07-15 13:48:48 -0600839 if (!strncmp(smd_ch_name[smd_pkt_devp->i], "LOOPBACK",
840 SMD_MAX_CH_NAME_LEN)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841 if (!is_modem_smsm_inited())
842 msleep(5000);
843 smsm_change_state(SMSM_APPS_STATE,
844 0, SMSM_SMD_LOOPBACK);
845 msleep(100);
846 }
847
848 /*
849 * Wait for a packet channel to be allocated so we know
850 * the modem is ready enough.
851 */
852 if (smd_pkt_devp->open_modem_wait) {
853 r = wait_for_completion_interruptible_timeout(
854 &smd_pkt_devp->ch_allocated,
855 msecs_to_jiffies(
856 smd_pkt_devp->open_modem_wait
857 * 1000));
858 if (r == 0)
859 r = -ETIMEDOUT;
860 if (r < 0) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600861 pr_err("%s: wait on smd_pkt_dev id:%d"
862 " allocation failed rc:%d\n",
863 __func__, smd_pkt_devp->i, r);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700864 goto release_pil;
865 }
866 }
867 }
868
869 r = smd_named_open_on_edge(smd_ch_name[smd_pkt_devp->i],
870 smd_ch_edge[smd_pkt_devp->i],
871 &smd_pkt_devp->ch,
872 smd_pkt_devp,
873 ch_notify);
874 if (r < 0) {
875 pr_err("%s: %s open failed %d\n", __func__,
876 smd_ch_name[smd_pkt_devp->i], r);
877 goto release_pil;
878 }
879
880 r = wait_event_interruptible_timeout(
881 smd_pkt_devp->ch_opened_wait_queue,
882 smd_pkt_devp->is_open, (2 * HZ));
Jeff Hugo4d9fdf32012-04-30 11:10:54 -0600883 if (r == 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700884 r = -ETIMEDOUT;
Jeff Hugo4d9fdf32012-04-30 11:10:54 -0600885 /* close the ch to sync smd's state with smd_pkt */
886 smd_close(smd_pkt_devp->ch);
887 smd_pkt_devp->ch = NULL;
888 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889
890 if (r < 0) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600891 pr_err("%s: wait on smd_pkt_dev id:%d OPEN event failed"
892 " rc:%d\n", __func__, smd_pkt_devp->i, r);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893 } else if (!smd_pkt_devp->is_open) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600894 pr_err("%s: Invalid OPEN event on smd_pkt_dev id:%d\n",
895 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700896 r = -ENODEV;
897 } else {
898 smd_disable_read_intr(smd_pkt_devp->ch);
899 smd_pkt_devp->ch_size =
900 smd_write_avail(smd_pkt_devp->ch);
901 r = 0;
Karthikeyan Ramasubramanian540dbf32012-09-14 13:16:50 -0600902 smd_pkt_devp->ref_cnt++;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600903 D_STATUS("Finished %s on smd_pkt_dev id:%d\n",
904 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905 }
Karthikeyan Ramasubramanian540dbf32012-09-14 13:16:50 -0600906 } else {
907 smd_pkt_devp->ref_cnt++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908 }
909release_pil:
910 if (peripheral && (r < 0))
911 pil_put(smd_pkt_devp->pil);
Karthikeyan Ramasubramanian63a913c2012-03-06 15:43:48 -0700912
913release_pd:
Karthikeyan Ramasubramanian5fa88252012-04-19 23:45:08 -0600914 if (r < 0) {
Karthikeyan Ramasubramanian63a913c2012-03-06 15:43:48 -0700915 platform_driver_unregister(&smd_pkt_devp->driver);
Karthikeyan Ramasubramanian5fa88252012-04-19 23:45:08 -0600916 smd_pkt_devp->driver.probe = NULL;
917 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700918out:
Eric Holmbergb399a4f2012-07-06 11:42:24 -0600919 if (!smd_pkt_devp->ch)
920 wake_lock_destroy(&smd_pkt_devp->pa_wake_lock);
921
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922 mutex_unlock(&smd_pkt_devp->ch_lock);
923
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700924
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 return r;
926}
927
928int smd_pkt_release(struct inode *inode, struct file *file)
929{
930 int r = 0;
931 struct smd_pkt_dev *smd_pkt_devp = file->private_data;
932
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600933 if (!smd_pkt_devp) {
934 pr_err("%s on a NULL device\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700935 return -EINVAL;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600936 }
937 D_STATUS("Begin %s on smd_pkt_dev id:%d\n",
938 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940 mutex_lock(&smd_pkt_devp->ch_lock);
Karthikeyan Ramasubramanian96a14fb2012-04-30 12:04:35 -0600941 mutex_lock(&smd_pkt_devp->rx_lock);
942 mutex_lock(&smd_pkt_devp->tx_lock);
Karthikeyan Ramasubramanian540dbf32012-09-14 13:16:50 -0600943 if (smd_pkt_devp->ref_cnt > 0)
944 smd_pkt_devp->ref_cnt--;
945
946 if (smd_pkt_devp->ch != 0 && smd_pkt_devp->ref_cnt == 0) {
947 clean_and_signal(smd_pkt_devp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948 r = smd_close(smd_pkt_devp->ch);
949 smd_pkt_devp->ch = 0;
950 smd_pkt_devp->blocking_write = 0;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700951 smd_pkt_devp->poll_mode = 0;
Karthikeyan Ramasubramanian63a913c2012-03-06 15:43:48 -0700952 platform_driver_unregister(&smd_pkt_devp->driver);
Karthikeyan Ramasubramanian5fa88252012-04-19 23:45:08 -0600953 smd_pkt_devp->driver.probe = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954 if (smd_pkt_devp->pil)
955 pil_put(smd_pkt_devp->pil);
Karthikeyan Ramasubramanian540dbf32012-09-14 13:16:50 -0600956 smd_pkt_devp->has_reset = 0;
957 smd_pkt_devp->do_reset_notification = 0;
958 smd_pkt_devp->wakelock_locked = 0;
959 wake_lock_destroy(&smd_pkt_devp->pa_wake_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700960 }
Karthikeyan Ramasubramanian96a14fb2012-04-30 12:04:35 -0600961 mutex_unlock(&smd_pkt_devp->tx_lock);
962 mutex_unlock(&smd_pkt_devp->rx_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700963 mutex_unlock(&smd_pkt_devp->ch_lock);
964
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600965 D_STATUS("Finished %s on smd_pkt_dev id:%d\n",
966 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700967
968 return r;
969}
970
971static const struct file_operations smd_pkt_fops = {
972 .owner = THIS_MODULE,
973 .open = smd_pkt_open,
974 .release = smd_pkt_release,
975 .read = smd_pkt_read,
976 .write = smd_pkt_write,
977 .poll = smd_pkt_poll,
978 .unlocked_ioctl = smd_pkt_ioctl,
979};
980
981static int __init smd_pkt_init(void)
982{
983 int i;
984 int r;
985
986 r = alloc_chrdev_region(&smd_pkt_number,
987 0,
988 NUM_SMD_PKT_PORTS,
989 DEVICE_NAME);
990 if (IS_ERR_VALUE(r)) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600991 pr_err("%s: alloc_chrdev_region() failed ret:%i\n",
992 __func__, r);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993 goto error0;
994 }
995
996 smd_pkt_classp = class_create(THIS_MODULE, DEVICE_NAME);
997 if (IS_ERR(smd_pkt_classp)) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600998 pr_err("%s: class_create() failed ENOMEM\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700999 r = -ENOMEM;
1000 goto error1;
1001 }
1002
1003 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
1004 smd_pkt_devp[i] = kzalloc(sizeof(struct smd_pkt_dev),
1005 GFP_KERNEL);
1006 if (IS_ERR(smd_pkt_devp[i])) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -06001007 pr_err("%s: kzalloc() failed for smd_pkt_dev id:%d\n",
1008 __func__, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001009 r = -ENOMEM;
1010 goto error2;
1011 }
1012
1013 smd_pkt_devp[i]->i = i;
1014
1015 init_waitqueue_head(&smd_pkt_devp[i]->ch_read_wait_queue);
1016 init_waitqueue_head(&smd_pkt_devp[i]->ch_write_wait_queue);
1017 smd_pkt_devp[i]->is_open = 0;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -07001018 smd_pkt_devp[i]->poll_mode = 0;
Jeff Hugo93476a32012-06-01 14:32:49 -06001019 smd_pkt_devp[i]->wakelock_locked = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001020 init_waitqueue_head(&smd_pkt_devp[i]->ch_opened_wait_queue);
1021
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -07001022 spin_lock_init(&smd_pkt_devp[i]->pa_spinlock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001023 mutex_init(&smd_pkt_devp[i]->ch_lock);
1024 mutex_init(&smd_pkt_devp[i]->rx_lock);
1025 mutex_init(&smd_pkt_devp[i]->tx_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001026
1027 cdev_init(&smd_pkt_devp[i]->cdev, &smd_pkt_fops);
1028 smd_pkt_devp[i]->cdev.owner = THIS_MODULE;
1029
1030 r = cdev_add(&smd_pkt_devp[i]->cdev,
1031 (smd_pkt_number + i),
1032 1);
1033
1034 if (IS_ERR_VALUE(r)) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -06001035 pr_err("%s: cdev_add() failed for smd_pkt_dev id:%d"
1036 " ret:%i\n", __func__, i, r);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037 kfree(smd_pkt_devp[i]);
1038 goto error2;
1039 }
1040
1041 smd_pkt_devp[i]->devicep =
1042 device_create(smd_pkt_classp,
1043 NULL,
1044 (smd_pkt_number + i),
1045 NULL,
1046 smd_pkt_dev_name[i]);
1047
1048 if (IS_ERR(smd_pkt_devp[i]->devicep)) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -06001049 pr_err("%s: device_create() failed for smd_pkt_dev"
1050 " id:%d\n", __func__, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001051 r = -ENOMEM;
1052 cdev_del(&smd_pkt_devp[i]->cdev);
1053 kfree(smd_pkt_devp[i]);
1054 goto error2;
1055 }
1056 if (device_create_file(smd_pkt_devp[i]->devicep,
1057 &dev_attr_open_timeout))
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -06001058 pr_err("%s: unable to create device attr for"
1059 " smd_pkt_dev id:%d\n", __func__, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001060 }
1061
1062 INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker);
1063
Karthikeyan Ramasubramanian085bd622012-09-10 21:45:00 -06001064 smd_pkt_ilctxt = ipc_log_context_create(SMD_PKT_IPC_LOG_PAGE_CNT,
1065 "smd_pkt");
1066
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -06001067 D_STATUS("SMD Packet Port Driver Initialized.\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068 return 0;
1069
1070 error2:
1071 if (i > 0) {
1072 while (--i >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001073 cdev_del(&smd_pkt_devp[i]->cdev);
1074 kfree(smd_pkt_devp[i]);
1075 device_destroy(smd_pkt_classp,
1076 MKDEV(MAJOR(smd_pkt_number), i));
1077 }
1078 }
1079
1080 class_destroy(smd_pkt_classp);
1081 error1:
1082 unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
1083 error0:
1084 return r;
1085}
1086
1087static void __exit smd_pkt_cleanup(void)
1088{
1089 int i;
1090
1091 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 cdev_del(&smd_pkt_devp[i]->cdev);
1093 kfree(smd_pkt_devp[i]);
1094 device_destroy(smd_pkt_classp,
1095 MKDEV(MAJOR(smd_pkt_number), i));
1096 }
1097
1098 class_destroy(smd_pkt_classp);
1099
1100 unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
1101}
1102
1103module_init(smd_pkt_init);
1104module_exit(smd_pkt_cleanup);
1105
1106MODULE_DESCRIPTION("MSM Shared Memory Packet Port");
1107MODULE_LICENSE("GPL v2");