blob: 430a4c0bf7c2dc3dce79c0aa5e8eae7e04598f3e [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>
38
39#include "smd_private.h"
40#ifdef CONFIG_ARCH_FSM9XXX
41#define NUM_SMD_PKT_PORTS 4
42#else
Jeff Hugodf5ee322012-04-16 11:56:12 -060043#define NUM_SMD_PKT_PORTS 14
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070044#endif
45
46#define LOOPBACK_INX (NUM_SMD_PKT_PORTS - 1)
47
48#define DEVICE_NAME "smdpkt"
Eric Holmbergc3c5cd92012-02-07 18:19:49 -070049#define WAKELOCK_TIMEOUT (2*HZ)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050
51struct smd_pkt_dev {
52 struct cdev cdev;
53 struct device *devicep;
54 void *pil;
55 struct platform_driver driver;
56
57 struct smd_channel *ch;
58 struct mutex ch_lock;
59 struct mutex rx_lock;
60 struct mutex tx_lock;
61 wait_queue_head_t ch_read_wait_queue;
62 wait_queue_head_t ch_write_wait_queue;
63 wait_queue_head_t ch_opened_wait_queue;
64
65 int i;
66
67 int blocking_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068 int is_open;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -070069 int poll_mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070070 unsigned ch_size;
71 uint open_modem_wait;
72
73 int has_reset;
74 int do_reset_notification;
75 struct completion ch_allocated;
Eric Holmbergc3c5cd92012-02-07 18:19:49 -070076 struct wake_lock pa_wake_lock; /* Packet Arrival Wake lock*/
77 struct work_struct packet_arrival_work;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -070078 struct spinlock pa_spinlock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079} *smd_pkt_devp[NUM_SMD_PKT_PORTS];
80
81struct class *smd_pkt_classp;
82static dev_t smd_pkt_number;
83static struct delayed_work loopback_work;
84static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp);
85static void check_and_wakeup_writer(struct smd_pkt_dev *smd_pkt_devp);
86static uint32_t is_modem_smsm_inited(void);
87
88static int msm_smd_pkt_debug_mask;
89module_param_named(debug_mask, msm_smd_pkt_debug_mask,
90 int, S_IRUGO | S_IWUSR | S_IWGRP);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -060091
92enum {
93 SMD_PKT_STATUS = 1U << 0,
94 SMD_PKT_READ = 1U << 1,
95 SMD_PKT_WRITE = 1U << 2,
96 SMD_PKT_READ_DUMP_BUFFER = 1U << 3,
97 SMD_PKT_WRITE_DUMP_BUFFER = 1U << 4,
98 SMD_PKT_POLL = 1U << 5,
99};
100
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101#define DEBUG
102
103#ifdef DEBUG
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600104#define D_STATUS(x...) \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105do { \
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600106 if (msm_smd_pkt_debug_mask & SMD_PKT_STATUS) \
107 pr_info("Status: "x); \
108} while (0)
109
110#define D_READ(x...) \
111do { \
112 if (msm_smd_pkt_debug_mask & SMD_PKT_READ) \
113 pr_info("Read: "x); \
114} while (0)
115
116#define D_WRITE(x...) \
117do { \
118 if (msm_smd_pkt_debug_mask & SMD_PKT_WRITE) \
119 pr_info("Write: "x); \
120} while (0)
121
122#define D_READ_DUMP_BUFFER(prestr, cnt, buf) \
123do { \
124 if (msm_smd_pkt_debug_mask & SMD_PKT_READ_DUMP_BUFFER) \
125 print_hex_dump(KERN_INFO, prestr, \
126 DUMP_PREFIX_NONE, 16, 1, \
127 buf, cnt, 1); \
128} while (0)
129
130#define D_WRITE_DUMP_BUFFER(prestr, cnt, buf) \
131do { \
132 if (msm_smd_pkt_debug_mask & SMD_PKT_WRITE_DUMP_BUFFER) \
133 print_hex_dump(KERN_INFO, prestr, \
134 DUMP_PREFIX_NONE, 16, 1, \
135 buf, cnt, 1); \
136} while (0)
137
138#define D_POLL(x...) \
139do { \
140 if (msm_smd_pkt_debug_mask & SMD_PKT_POLL) \
141 pr_info("Poll: "x); \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142} while (0)
143#else
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600144#define D_STATUS(x...) do {} while (0)
145#define D_READ(x...) do {} while (0)
146#define D_WRITE(x...) do {} while (0)
147#define D_READ_DUMP_BUFFER(prestr, cnt, buf) do {} while (0)
148#define D_WRITE_DUMP_BUFFER(prestr, cnt, buf) do {} while (0)
149#define D_POLL(x...) do {} while (0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150#endif
151
152static ssize_t open_timeout_store(struct device *d,
153 struct device_attribute *attr,
154 const char *buf,
155 size_t n)
156{
157 int i;
158 unsigned long tmp;
159 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
160 if (smd_pkt_devp[i]->devicep == d)
161 break;
162 }
Jeff Hugo31f83b42012-01-25 15:15:26 -0700163 if (i >= NUM_SMD_PKT_PORTS) {
164 pr_err("%s: unable to match device to valid smd_pkt port\n",
165 __func__);
166 return -EINVAL;
167 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168 if (!strict_strtoul(buf, 10, &tmp)) {
169 smd_pkt_devp[i]->open_modem_wait = tmp;
170 return n;
171 } else {
172 pr_err("%s: unable to convert: %s to an int\n", __func__,
173 buf);
174 return -EINVAL;
175 }
176}
177
178static ssize_t open_timeout_show(struct device *d,
179 struct device_attribute *attr,
180 char *buf)
181{
182 int i;
183 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
184 if (smd_pkt_devp[i]->devicep == d)
185 break;
186 }
Jeff Hugo31f83b42012-01-25 15:15:26 -0700187 if (i >= NUM_SMD_PKT_PORTS) {
188 pr_err("%s: unable to match device to valid smd_pkt port\n",
189 __func__);
190 return -EINVAL;
191 }
Karthikeyan Ramasubramanian63fa3d32011-09-29 17:06:26 -0600192 return snprintf(buf, PAGE_SIZE, "%d\n",
193 smd_pkt_devp[i]->open_modem_wait);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194}
195
196static DEVICE_ATTR(open_timeout, 0664, open_timeout_show, open_timeout_store);
197
198static int notify_reset(struct smd_pkt_dev *smd_pkt_devp)
199{
200 smd_pkt_devp->do_reset_notification = 0;
201
202 return -ENETRESET;
203}
204
205static void clean_and_signal(struct smd_pkt_dev *smd_pkt_devp)
206{
207 smd_pkt_devp->do_reset_notification = 1;
208 smd_pkt_devp->has_reset = 1;
209
210 smd_pkt_devp->is_open = 0;
211
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700212 wake_up(&smd_pkt_devp->ch_read_wait_queue);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700213 wake_up(&smd_pkt_devp->ch_write_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214 wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600215 D_STATUS("%s smd_pkt_dev id:%d\n", __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216}
217
218static void loopback_probe_worker(struct work_struct *work)
219{
220
221 /* Wait for the modem SMSM to be inited for the SMD
222 ** Loopback channel to be allocated at the modem. Since
223 ** the wait need to be done atmost once, using msleep
224 ** doesn't degrade the performance. */
225 if (!is_modem_smsm_inited())
226 schedule_delayed_work(&loopback_work, msecs_to_jiffies(1000));
227 else
228 smsm_change_state(SMSM_APPS_STATE,
229 0, SMSM_SMD_LOOPBACK);
230
231}
232
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700233static void packet_arrival_worker(struct work_struct *work)
234{
235 struct smd_pkt_dev *smd_pkt_devp;
236
237 smd_pkt_devp = container_of(work, struct smd_pkt_dev,
238 packet_arrival_work);
239 mutex_lock(&smd_pkt_devp->ch_lock);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600240 if (smd_pkt_devp->ch) {
241 D_READ("%s locking smd_pkt_dev id:%d wakelock\n",
242 __func__, smd_pkt_devp->i);
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700243 wake_lock_timeout(&smd_pkt_devp->pa_wake_lock,
244 WAKELOCK_TIMEOUT);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600245 }
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700246 mutex_unlock(&smd_pkt_devp->ch_lock);
247}
248
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249static long smd_pkt_ioctl(struct file *file, unsigned int cmd,
250 unsigned long arg)
251{
252 int ret;
253 struct smd_pkt_dev *smd_pkt_devp;
254
255 smd_pkt_devp = file->private_data;
256 if (!smd_pkt_devp)
257 return -EINVAL;
258
259 switch (cmd) {
260 case TIOCMGET:
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600261 D_STATUS("%s TIOCMGET command on smd_pkt_dev id:%d\n",
262 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263 ret = smd_tiocmget(smd_pkt_devp->ch);
264 break;
265 case TIOCMSET:
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600266 D_STATUS("%s TIOCSET command on smd_pkt_dev id:%d\n",
267 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268 ret = smd_tiocmset(smd_pkt_devp->ch, arg, ~arg);
269 break;
270 case SMD_PKT_IOCTL_BLOCKING_WRITE:
271 ret = get_user(smd_pkt_devp->blocking_write, (int *)arg);
272 break;
273 default:
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600274 pr_err("%s: Unrecognized ioctl command %d\n", __func__, cmd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275 ret = -1;
276 }
277
278 return ret;
279}
280
281ssize_t smd_pkt_read(struct file *file,
282 char __user *buf,
283 size_t count,
284 loff_t *ppos)
285{
286 int r;
287 int bytes_read;
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700288 int pkt_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289 struct smd_pkt_dev *smd_pkt_devp;
290 struct smd_channel *chl;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700291 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293 smd_pkt_devp = file->private_data;
294
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600295 if (!smd_pkt_devp) {
296 pr_err("%s on NULL smd_pkt_dev\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297 return -EINVAL;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600298 }
299
300 if (!smd_pkt_devp->ch) {
301 pr_err("%s on a closed smd_pkt_dev id:%d\n",
302 __func__, smd_pkt_devp->i);
303 return -EINVAL;
304 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305
306 if (smd_pkt_devp->do_reset_notification) {
307 /* notify client that a reset occurred */
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600308 pr_err("%s notifying reset for smd_pkt_dev id:%d\n",
309 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 return notify_reset(smd_pkt_devp);
311 }
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600312 D_READ("Begin %s on smd_pkt_dev id:%d buffer_size %d\n",
313 __func__, smd_pkt_devp->i, count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700314
315 chl = smd_pkt_devp->ch;
316wait_for_packet:
317 r = wait_event_interruptible(smd_pkt_devp->ch_read_wait_queue,
318 (smd_cur_packet_size(chl) > 0 &&
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700319 smd_read_avail(chl)) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320 smd_pkt_devp->has_reset);
321
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600322 if (smd_pkt_devp->has_reset) {
323 pr_err("%s notifying reset for smd_pkt_dev id:%d\n",
324 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325 return notify_reset(smd_pkt_devp);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600326 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327
328 if (r < 0) {
329 /* qualify error message */
330 if (r != -ERESTARTSYS) {
331 /* we get this anytime a signal comes in */
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600332 pr_err("%s: wait_event_interruptible on smd_pkt_dev"
333 " id:%d ret %i\n",
334 __func__, smd_pkt_devp->i, r);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335 }
336 return r;
337 }
338
339 /* Here we have a whole packet waiting for us */
340
341 mutex_lock(&smd_pkt_devp->rx_lock);
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700342 pkt_size = smd_cur_packet_size(smd_pkt_devp->ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700344 if (!pkt_size) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600345 pr_err("%s: No data on smd_pkt_dev id:%d, False wakeup\n",
346 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700347 mutex_unlock(&smd_pkt_devp->rx_lock);
348 goto wait_for_packet;
349 }
350
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700351 if (pkt_size > count) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600352 pr_err("%s: failure on smd_pkt_dev id: %d - packet size %d"
353 " > buffer size %d,", __func__, smd_pkt_devp->i,
354 pkt_size, count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700355 mutex_unlock(&smd_pkt_devp->rx_lock);
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700356 return -ETOOSMALL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700357 }
358
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700359 bytes_read = 0;
360 do {
361 r = smd_read_user_buffer(smd_pkt_devp->ch,
362 (buf + bytes_read),
363 (pkt_size - bytes_read));
364 if (r < 0) {
365 mutex_unlock(&smd_pkt_devp->rx_lock);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600366 if (smd_pkt_devp->has_reset) {
367 pr_err("%s notifying reset for smd_pkt_dev"
368 " id:%d\n", __func__, smd_pkt_devp->i);
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700369 return notify_reset(smd_pkt_devp);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600370 }
371 pr_err("%s Error while reading %d\n", __func__, r);
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700372 return r;
373 }
374 bytes_read += r;
375 if (pkt_size != bytes_read)
376 wait_event(smd_pkt_devp->ch_read_wait_queue,
377 smd_read_avail(smd_pkt_devp->ch) ||
378 smd_pkt_devp->has_reset);
379 if (smd_pkt_devp->has_reset) {
380 mutex_unlock(&smd_pkt_devp->rx_lock);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600381 pr_err("%s notifying reset for smd_pkt_dev id:%d\n",
382 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700383 return notify_reset(smd_pkt_devp);
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700384 }
385 } while (pkt_size != bytes_read);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600386 D_READ_DUMP_BUFFER("Read: ", (bytes_read > 16 ? 16 : bytes_read), buf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387 mutex_unlock(&smd_pkt_devp->rx_lock);
388
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700389 mutex_lock(&smd_pkt_devp->ch_lock);
390 spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags);
391 if (smd_pkt_devp->poll_mode &&
392 !smd_cur_packet_size(smd_pkt_devp->ch)) {
393 wake_unlock(&smd_pkt_devp->pa_wake_lock);
394 smd_pkt_devp->poll_mode = 0;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600395 D_READ("%s unlocked smd_pkt_dev id:%d wakelock\n",
396 __func__, smd_pkt_devp->i);
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700397 }
398 spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags);
399 mutex_unlock(&smd_pkt_devp->ch_lock);
400
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600401 D_READ("Finished %s on smd_pkt_dev id:%d %d bytes\n",
402 __func__, smd_pkt_devp->i, bytes_read);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700403
404 /* check and wakeup read threads waiting on this device */
405 check_and_wakeup_reader(smd_pkt_devp);
406
407 return bytes_read;
408}
409
410ssize_t smd_pkt_write(struct file *file,
411 const char __user *buf,
412 size_t count,
413 loff_t *ppos)
414{
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700415 int r = 0, bytes_written;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416 struct smd_pkt_dev *smd_pkt_devp;
417 DEFINE_WAIT(write_wait);
418
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700419 smd_pkt_devp = file->private_data;
420
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600421 if (!smd_pkt_devp) {
422 pr_err("%s on NULL smd_pkt_dev\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423 return -EINVAL;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600424 }
425
426 if (!smd_pkt_devp->ch) {
427 pr_err("%s on a closed smd_pkt_dev id:%d\n",
428 __func__, smd_pkt_devp->i);
429 return -EINVAL;
430 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432 if (smd_pkt_devp->do_reset_notification) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600433 pr_err("%s notifying reset for smd_pkt_dev id:%d\n",
434 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435 /* notify client that a reset occurred */
436 return notify_reset(smd_pkt_devp);
437 }
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600438 D_WRITE("Begin %s on smd_pkt_dev id:%d data_size %d\n",
439 __func__, smd_pkt_devp->i, count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700441 mutex_lock(&smd_pkt_devp->tx_lock);
442 if (!smd_pkt_devp->blocking_write) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443 if (smd_write_avail(smd_pkt_devp->ch) < count) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600444 pr_err("%s: Not enough space in smd_pkt_dev id:%d\n",
445 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446 mutex_unlock(&smd_pkt_devp->tx_lock);
447 return -ENOMEM;
448 }
449 }
450
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700451 r = smd_write_start(smd_pkt_devp->ch, count);
452 if (r < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 mutex_unlock(&smd_pkt_devp->tx_lock);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600454 pr_err("%s: Error:%d in smd_pkt_dev id:%d @ smd_write_start\n",
455 __func__, r, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456 return r;
457 }
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700458
459 bytes_written = 0;
460 do {
461 prepare_to_wait(&smd_pkt_devp->ch_write_wait_queue,
462 &write_wait, TASK_UNINTERRUPTIBLE);
463 if (!smd_write_avail(smd_pkt_devp->ch) &&
464 !smd_pkt_devp->has_reset) {
465 smd_enable_read_intr(smd_pkt_devp->ch);
466 schedule();
467 }
468 finish_wait(&smd_pkt_devp->ch_write_wait_queue, &write_wait);
469 smd_disable_read_intr(smd_pkt_devp->ch);
470
471 if (smd_pkt_devp->has_reset) {
472 mutex_unlock(&smd_pkt_devp->tx_lock);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600473 pr_err("%s notifying reset for smd_pkt_dev id:%d\n",
474 __func__, smd_pkt_devp->i);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700475 return notify_reset(smd_pkt_devp);
476 } else {
477 r = smd_write_segment(smd_pkt_devp->ch,
478 (void *)(buf + bytes_written),
479 (count - bytes_written), 1);
480 if (r < 0) {
481 mutex_unlock(&smd_pkt_devp->tx_lock);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600482 if (smd_pkt_devp->has_reset) {
483 pr_err("%s notifying reset for"
484 " smd_pkt_dev id:%d\n",
485 __func__, smd_pkt_devp->i);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700486 return notify_reset(smd_pkt_devp);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600487 }
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700488 }
489 bytes_written += r;
490 }
491 } while (bytes_written != count);
492 smd_write_end(smd_pkt_devp->ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 mutex_unlock(&smd_pkt_devp->tx_lock);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600494 D_WRITE_DUMP_BUFFER("Write: ",
495 (bytes_written > 16 ? 16 : bytes_written), buf);
496 D_WRITE("Finished %s on smd_pkt_dev id:%d %d bytes\n",
497 __func__, smd_pkt_devp->i, count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498
499 return count;
500}
501
502static unsigned int smd_pkt_poll(struct file *file, poll_table *wait)
503{
504 struct smd_pkt_dev *smd_pkt_devp;
505 unsigned int mask = 0;
506
507 smd_pkt_devp = file->private_data;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600508 if (!smd_pkt_devp) {
509 pr_err("%s on a NULL device\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510 return POLLERR;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600511 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700512
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700513 smd_pkt_devp->poll_mode = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514 poll_wait(file, &smd_pkt_devp->ch_read_wait_queue, wait);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600515 if (smd_read_avail(smd_pkt_devp->ch)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516 mask |= POLLIN | POLLRDNORM;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600517 D_POLL("%s sets POLLIN for smd_pkt_dev id: %d\n",
518 __func__, smd_pkt_devp->i);
519 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520
521 return mask;
522}
523
524static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp)
525{
526 int sz;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700527 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600529 if (!smd_pkt_devp) {
530 pr_err("%s on a NULL device\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531 return;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600532 }
533
534 if (!smd_pkt_devp->ch) {
535 pr_err("%s on a closed smd_pkt_dev id:%d\n",
536 __func__, smd_pkt_devp->i);
537 return;
538 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539
540 sz = smd_cur_packet_size(smd_pkt_devp->ch);
541 if (sz == 0) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600542 D_READ("%s: No packet in smd_pkt_dev id:%d\n",
543 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544 return;
545 }
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700546 if (!smd_read_avail(smd_pkt_devp->ch)) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600547 D_READ("%s: packet size is %d in smd_pkt_dev id:%d -"
548 " but the data isn't here\n",
549 __func__, sz, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550 return;
551 }
552
553 /* here we have a packet of size sz ready */
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700554 wake_up(&smd_pkt_devp->ch_read_wait_queue);
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700555 spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags);
556 wake_lock(&smd_pkt_devp->pa_wake_lock);
557 spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags);
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700558 schedule_work(&smd_pkt_devp->packet_arrival_work);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600559 D_READ("%s: wake_up smd_pkt_dev id:%d\n", __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560}
561
562static void check_and_wakeup_writer(struct smd_pkt_dev *smd_pkt_devp)
563{
564 int sz;
565
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600566 if (!smd_pkt_devp) {
567 pr_err("%s on a NULL device\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568 return;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600569 }
570
571 if (!smd_pkt_devp->ch) {
572 pr_err("%s on a closed smd_pkt_dev id:%d\n",
573 __func__, smd_pkt_devp->i);
574 return;
575 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576
577 sz = smd_write_avail(smd_pkt_devp->ch);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700578 if (sz) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600579 D_WRITE("%s: %d bytes write space in smd_pkt_dev id:%d\n",
580 __func__, sz, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581 smd_disable_read_intr(smd_pkt_devp->ch);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700582 wake_up(&smd_pkt_devp->ch_write_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583 }
584}
585
586static void ch_notify(void *priv, unsigned event)
587{
588 struct smd_pkt_dev *smd_pkt_devp = priv;
589
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600590 if (smd_pkt_devp->ch == 0) {
591 pr_err("%s on a closed smd_pkt_dev id:%d\n",
592 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593 return;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600594 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595
596 switch (event) {
597 case SMD_EVENT_DATA: {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600598 D_STATUS("%s: DATA event in smd_pkt_dev id:%d\n",
599 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 check_and_wakeup_reader(smd_pkt_devp);
601 if (smd_pkt_devp->blocking_write)
602 check_and_wakeup_writer(smd_pkt_devp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603 break;
604 }
605 case SMD_EVENT_OPEN:
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600606 D_STATUS("%s: OPEN event in smd_pkt_dev id:%d\n",
607 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608 smd_pkt_devp->has_reset = 0;
609 smd_pkt_devp->is_open = 1;
610 wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue);
611 break;
612 case SMD_EVENT_CLOSE:
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600613 D_STATUS("%s: CLOSE event in smd_pkt_dev id:%d\n",
614 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700615 smd_pkt_devp->is_open = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616 /* put port into reset state */
617 clean_and_signal(smd_pkt_devp);
618 if (smd_pkt_devp->i == LOOPBACK_INX)
619 schedule_delayed_work(&loopback_work,
620 msecs_to_jiffies(1000));
621 break;
622 }
623}
624
625#ifdef CONFIG_ARCH_FSM9XXX
626static char *smd_pkt_dev_name[] = {
627 "smdcntl1",
628 "smdcntl2",
629 "smd22",
630 "smd_pkt_loopback",
631};
632
633static char *smd_ch_name[] = {
634 "DATA6_CNTL",
635 "DATA7_CNTL",
636 "DATA22",
637 "LOOPBACK",
638};
639
640static uint32_t smd_ch_edge[] = {
641 SMD_APPS_QDSP,
642 SMD_APPS_QDSP,
643 SMD_APPS_QDSP,
644 SMD_APPS_QDSP
645};
646#else
647static char *smd_pkt_dev_name[] = {
648 "smdcntl0",
649 "smdcntl1",
650 "smdcntl2",
651 "smdcntl3",
652 "smdcntl4",
653 "smdcntl5",
654 "smdcntl6",
655 "smdcntl7",
656 "smd22",
657 "smd_sns_dsps",
Angshuman Sarkara18a2722011-07-29 13:41:13 +0530658 "apr_apps2",
Karthikeyan Ramasubramaniand08e59a2012-03-01 17:35:34 -0700659 "smdcntl8",
Jeff Hugodf5ee322012-04-16 11:56:12 -0600660 "smd_sns_adsp",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661 "smd_pkt_loopback",
662};
663
664static char *smd_ch_name[] = {
665 "DATA5_CNTL",
666 "DATA6_CNTL",
667 "DATA7_CNTL",
668 "DATA8_CNTL",
669 "DATA9_CNTL",
670 "DATA12_CNTL",
671 "DATA13_CNTL",
672 "DATA14_CNTL",
673 "DATA22",
674 "SENSOR",
Angshuman Sarkara18a2722011-07-29 13:41:13 +0530675 "apr_apps2",
Karthikeyan Ramasubramaniand08e59a2012-03-01 17:35:34 -0700676 "DATA40_CNTL",
Jeff Hugodf5ee322012-04-16 11:56:12 -0600677 "SENSOR",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678 "LOOPBACK",
679};
680
681static uint32_t smd_ch_edge[] = {
682 SMD_APPS_MODEM,
683 SMD_APPS_MODEM,
684 SMD_APPS_MODEM,
685 SMD_APPS_MODEM,
686 SMD_APPS_MODEM,
687 SMD_APPS_MODEM,
688 SMD_APPS_MODEM,
689 SMD_APPS_MODEM,
690 SMD_APPS_MODEM,
691 SMD_APPS_DSPS,
692 SMD_APPS_QDSP,
693 SMD_APPS_MODEM,
Jeff Hugodf5ee322012-04-16 11:56:12 -0600694 SMD_APPS_QDSP,
Karthikeyan Ramasubramaniand08e59a2012-03-01 17:35:34 -0700695 SMD_APPS_MODEM,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700696};
697#endif
698
699static int smd_pkt_dummy_probe(struct platform_device *pdev)
700{
701 int i;
702
703 for (i = 0; i < NUM_SMD_PKT_PORTS; i++) {
Jeff Hugoa5ee4362011-07-15 13:48:48 -0600704 if (!strncmp(pdev->name, smd_ch_name[i], SMD_MAX_CH_NAME_LEN)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700705 complete_all(&smd_pkt_devp[i]->ch_allocated);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600706 D_STATUS("%s allocated SMD ch for smd_pkt_dev id:%d\n",
707 __func__, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708 break;
709 }
710 }
711 return 0;
712}
713
714static uint32_t is_modem_smsm_inited(void)
715{
716 uint32_t modem_state;
717 uint32_t ready_state = (SMSM_INIT | SMSM_SMDINIT);
718
719 modem_state = smsm_get_state(SMSM_MODEM_STATE);
720 return (modem_state & ready_state) == ready_state;
721}
722
723int smd_pkt_open(struct inode *inode, struct file *file)
724{
725 int r = 0;
726 struct smd_pkt_dev *smd_pkt_devp;
Eric Holmbergcce6daf2012-02-27 14:34:02 -0700727 const char *peripheral = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700728
729 smd_pkt_devp = container_of(inode->i_cdev, struct smd_pkt_dev, cdev);
730
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600731 if (!smd_pkt_devp) {
732 pr_err("%s on a NULL device\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700733 return -EINVAL;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600734 }
735 D_STATUS("Begin %s on smd_pkt_dev id:%d\n", __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700736
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700737 wake_lock_init(&smd_pkt_devp->pa_wake_lock, WAKE_LOCK_SUSPEND,
738 smd_pkt_dev_name[smd_pkt_devp->i]);
739 INIT_WORK(&smd_pkt_devp->packet_arrival_work, packet_arrival_worker);
740
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700741 file->private_data = smd_pkt_devp;
742
743 mutex_lock(&smd_pkt_devp->ch_lock);
744 if (smd_pkt_devp->ch == 0) {
Karthikeyan Ramasubramanian63a913c2012-03-06 15:43:48 -0700745 init_completion(&smd_pkt_devp->ch_allocated);
746 smd_pkt_devp->driver.probe = smd_pkt_dummy_probe;
747 smd_pkt_devp->driver.driver.name =
748 smd_ch_name[smd_pkt_devp->i];
749 smd_pkt_devp->driver.driver.owner = THIS_MODULE;
750 r = platform_driver_register(&smd_pkt_devp->driver);
751 if (r) {
752 pr_err("%s: %s Platform driver reg. failed\n",
753 __func__, smd_ch_name[smd_pkt_devp->i]);
754 goto out;
755 }
756
Eric Holmbergcce6daf2012-02-27 14:34:02 -0700757 peripheral = smd_edge_to_subsystem(
758 smd_ch_edge[smd_pkt_devp->i]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759 if (peripheral) {
760 smd_pkt_devp->pil = pil_get(peripheral);
761 if (IS_ERR(smd_pkt_devp->pil)) {
762 r = PTR_ERR(smd_pkt_devp->pil);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600763 pr_err("%s failed on smd_pkt_dev id:%d -"
764 " pil_get failed for %s\n", __func__,
765 smd_pkt_devp->i, peripheral);
Karthikeyan Ramasubramanian63a913c2012-03-06 15:43:48 -0700766 goto release_pd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767 }
768
769 /* Wait for the modem SMSM to be inited for the SMD
770 ** Loopback channel to be allocated at the modem. Since
771 ** the wait need to be done atmost once, using msleep
772 ** doesn't degrade the performance. */
Jeff Hugoa5ee4362011-07-15 13:48:48 -0600773 if (!strncmp(smd_ch_name[smd_pkt_devp->i], "LOOPBACK",
774 SMD_MAX_CH_NAME_LEN)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775 if (!is_modem_smsm_inited())
776 msleep(5000);
777 smsm_change_state(SMSM_APPS_STATE,
778 0, SMSM_SMD_LOOPBACK);
779 msleep(100);
780 }
781
782 /*
783 * Wait for a packet channel to be allocated so we know
784 * the modem is ready enough.
785 */
786 if (smd_pkt_devp->open_modem_wait) {
787 r = wait_for_completion_interruptible_timeout(
788 &smd_pkt_devp->ch_allocated,
789 msecs_to_jiffies(
790 smd_pkt_devp->open_modem_wait
791 * 1000));
792 if (r == 0)
793 r = -ETIMEDOUT;
794 if (r < 0) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600795 pr_err("%s: wait on smd_pkt_dev id:%d"
796 " allocation failed rc:%d\n",
797 __func__, smd_pkt_devp->i, r);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798 goto release_pil;
799 }
800 }
801 }
802
803 r = smd_named_open_on_edge(smd_ch_name[smd_pkt_devp->i],
804 smd_ch_edge[smd_pkt_devp->i],
805 &smd_pkt_devp->ch,
806 smd_pkt_devp,
807 ch_notify);
808 if (r < 0) {
809 pr_err("%s: %s open failed %d\n", __func__,
810 smd_ch_name[smd_pkt_devp->i], r);
811 goto release_pil;
812 }
813
814 r = wait_event_interruptible_timeout(
815 smd_pkt_devp->ch_opened_wait_queue,
816 smd_pkt_devp->is_open, (2 * HZ));
817 if (r == 0)
818 r = -ETIMEDOUT;
819
820 if (r < 0) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600821 pr_err("%s: wait on smd_pkt_dev id:%d OPEN event failed"
822 " rc:%d\n", __func__, smd_pkt_devp->i, r);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 } else if (!smd_pkt_devp->is_open) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600824 pr_err("%s: Invalid OPEN event on smd_pkt_dev id:%d\n",
825 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700826 r = -ENODEV;
827 } else {
828 smd_disable_read_intr(smd_pkt_devp->ch);
829 smd_pkt_devp->ch_size =
830 smd_write_avail(smd_pkt_devp->ch);
831 r = 0;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600832 D_STATUS("Finished %s on smd_pkt_dev id:%d\n",
833 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700834 }
835 }
836release_pil:
837 if (peripheral && (r < 0))
838 pil_put(smd_pkt_devp->pil);
Karthikeyan Ramasubramanian63a913c2012-03-06 15:43:48 -0700839
840release_pd:
841 if (r < 0)
842 platform_driver_unregister(&smd_pkt_devp->driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843out:
844 mutex_unlock(&smd_pkt_devp->ch_lock);
845
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700846 if (r < 0)
847 wake_lock_destroy(&smd_pkt_devp->pa_wake_lock);
848
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700849 return r;
850}
851
852int smd_pkt_release(struct inode *inode, struct file *file)
853{
854 int r = 0;
855 struct smd_pkt_dev *smd_pkt_devp = file->private_data;
856
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600857 if (!smd_pkt_devp) {
858 pr_err("%s on a NULL device\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700859 return -EINVAL;
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600860 }
861 D_STATUS("Begin %s on smd_pkt_dev id:%d\n",
862 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700863
864 clean_and_signal(smd_pkt_devp);
865
866 mutex_lock(&smd_pkt_devp->ch_lock);
867 if (smd_pkt_devp->ch != 0) {
868 r = smd_close(smd_pkt_devp->ch);
869 smd_pkt_devp->ch = 0;
870 smd_pkt_devp->blocking_write = 0;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700871 smd_pkt_devp->poll_mode = 0;
Karthikeyan Ramasubramanian63a913c2012-03-06 15:43:48 -0700872 platform_driver_unregister(&smd_pkt_devp->driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700873 if (smd_pkt_devp->pil)
874 pil_put(smd_pkt_devp->pil);
875 }
876 mutex_unlock(&smd_pkt_devp->ch_lock);
877
878 smd_pkt_devp->has_reset = 0;
879 smd_pkt_devp->do_reset_notification = 0;
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700880 wake_lock_destroy(&smd_pkt_devp->pa_wake_lock);
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600881 D_STATUS("Finished %s on smd_pkt_dev id:%d\n",
882 __func__, smd_pkt_devp->i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700883
884 return r;
885}
886
887static const struct file_operations smd_pkt_fops = {
888 .owner = THIS_MODULE,
889 .open = smd_pkt_open,
890 .release = smd_pkt_release,
891 .read = smd_pkt_read,
892 .write = smd_pkt_write,
893 .poll = smd_pkt_poll,
894 .unlocked_ioctl = smd_pkt_ioctl,
895};
896
897static int __init smd_pkt_init(void)
898{
899 int i;
900 int r;
901
902 r = alloc_chrdev_region(&smd_pkt_number,
903 0,
904 NUM_SMD_PKT_PORTS,
905 DEVICE_NAME);
906 if (IS_ERR_VALUE(r)) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600907 pr_err("%s: alloc_chrdev_region() failed ret:%i\n",
908 __func__, r);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909 goto error0;
910 }
911
912 smd_pkt_classp = class_create(THIS_MODULE, DEVICE_NAME);
913 if (IS_ERR(smd_pkt_classp)) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600914 pr_err("%s: class_create() failed ENOMEM\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915 r = -ENOMEM;
916 goto error1;
917 }
918
919 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
920 smd_pkt_devp[i] = kzalloc(sizeof(struct smd_pkt_dev),
921 GFP_KERNEL);
922 if (IS_ERR(smd_pkt_devp[i])) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600923 pr_err("%s: kzalloc() failed for smd_pkt_dev id:%d\n",
924 __func__, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 r = -ENOMEM;
926 goto error2;
927 }
928
929 smd_pkt_devp[i]->i = i;
930
931 init_waitqueue_head(&smd_pkt_devp[i]->ch_read_wait_queue);
932 init_waitqueue_head(&smd_pkt_devp[i]->ch_write_wait_queue);
933 smd_pkt_devp[i]->is_open = 0;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700934 smd_pkt_devp[i]->poll_mode = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700935 init_waitqueue_head(&smd_pkt_devp[i]->ch_opened_wait_queue);
936
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700937 spin_lock_init(&smd_pkt_devp[i]->pa_spinlock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700938 mutex_init(&smd_pkt_devp[i]->ch_lock);
939 mutex_init(&smd_pkt_devp[i]->rx_lock);
940 mutex_init(&smd_pkt_devp[i]->tx_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700941
942 cdev_init(&smd_pkt_devp[i]->cdev, &smd_pkt_fops);
943 smd_pkt_devp[i]->cdev.owner = THIS_MODULE;
944
945 r = cdev_add(&smd_pkt_devp[i]->cdev,
946 (smd_pkt_number + i),
947 1);
948
949 if (IS_ERR_VALUE(r)) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600950 pr_err("%s: cdev_add() failed for smd_pkt_dev id:%d"
951 " ret:%i\n", __func__, i, r);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 kfree(smd_pkt_devp[i]);
953 goto error2;
954 }
955
956 smd_pkt_devp[i]->devicep =
957 device_create(smd_pkt_classp,
958 NULL,
959 (smd_pkt_number + i),
960 NULL,
961 smd_pkt_dev_name[i]);
962
963 if (IS_ERR(smd_pkt_devp[i]->devicep)) {
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600964 pr_err("%s: device_create() failed for smd_pkt_dev"
965 " id:%d\n", __func__, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700966 r = -ENOMEM;
967 cdev_del(&smd_pkt_devp[i]->cdev);
968 kfree(smd_pkt_devp[i]);
969 goto error2;
970 }
971 if (device_create_file(smd_pkt_devp[i]->devicep,
972 &dev_attr_open_timeout))
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600973 pr_err("%s: unable to create device attr for"
974 " smd_pkt_dev id:%d\n", __func__, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700975 }
976
977 INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker);
978
Karthikeyan Ramasubramanian05142ff2012-03-30 17:39:24 -0600979 D_STATUS("SMD Packet Port Driver Initialized.\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980 return 0;
981
982 error2:
983 if (i > 0) {
984 while (--i >= 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700985 cdev_del(&smd_pkt_devp[i]->cdev);
986 kfree(smd_pkt_devp[i]);
987 device_destroy(smd_pkt_classp,
988 MKDEV(MAJOR(smd_pkt_number), i));
989 }
990 }
991
992 class_destroy(smd_pkt_classp);
993 error1:
994 unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
995 error0:
996 return r;
997}
998
999static void __exit smd_pkt_cleanup(void)
1000{
1001 int i;
1002
1003 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001004 cdev_del(&smd_pkt_devp[i]->cdev);
1005 kfree(smd_pkt_devp[i]);
1006 device_destroy(smd_pkt_classp,
1007 MKDEV(MAJOR(smd_pkt_number), i));
1008 }
1009
1010 class_destroy(smd_pkt_classp);
1011
1012 unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
1013}
1014
1015module_init(smd_pkt_init);
1016module_exit(smd_pkt_cleanup);
1017
1018MODULE_DESCRIPTION("MSM Shared Memory Packet Port");
1019MODULE_LICENSE("GPL v2");