blob: 158015abf844c093e6700b7bce4b34875448a68e [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
43#define NUM_SMD_PKT_PORTS 12
44#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);
91#define DEBUG
92
93#ifdef DEBUG
94#define D_DUMP_BUFFER(prestr, cnt, buf) \
95do { \
96 if (msm_smd_pkt_debug_mask) \
97 print_hex_dump(KERN_DEBUG, prestr, \
98 DUMP_PREFIX_NONE, 16, 1, \
99 buf, cnt, 1); \
100} while (0)
101#else
102#define D_DUMP_BUFFER(prestr, cnt, buf) do {} while (0)
103#endif
104
105#ifdef DEBUG
106#define D(x...) if (msm_smd_pkt_debug_mask) printk(x)
107#else
108#define D(x...) do {} while (0)
109#endif
110
111static ssize_t open_timeout_store(struct device *d,
112 struct device_attribute *attr,
113 const char *buf,
114 size_t n)
115{
116 int i;
117 unsigned long tmp;
118 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
119 if (smd_pkt_devp[i]->devicep == d)
120 break;
121 }
Jeff Hugo31f83b42012-01-25 15:15:26 -0700122 if (i >= NUM_SMD_PKT_PORTS) {
123 pr_err("%s: unable to match device to valid smd_pkt port\n",
124 __func__);
125 return -EINVAL;
126 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127 if (!strict_strtoul(buf, 10, &tmp)) {
128 smd_pkt_devp[i]->open_modem_wait = tmp;
129 return n;
130 } else {
131 pr_err("%s: unable to convert: %s to an int\n", __func__,
132 buf);
133 return -EINVAL;
134 }
135}
136
137static ssize_t open_timeout_show(struct device *d,
138 struct device_attribute *attr,
139 char *buf)
140{
141 int i;
142 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
143 if (smd_pkt_devp[i]->devicep == d)
144 break;
145 }
Jeff Hugo31f83b42012-01-25 15:15:26 -0700146 if (i >= NUM_SMD_PKT_PORTS) {
147 pr_err("%s: unable to match device to valid smd_pkt port\n",
148 __func__);
149 return -EINVAL;
150 }
Karthikeyan Ramasubramanian63fa3d32011-09-29 17:06:26 -0600151 return snprintf(buf, PAGE_SIZE, "%d\n",
152 smd_pkt_devp[i]->open_modem_wait);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700153}
154
155static DEVICE_ATTR(open_timeout, 0664, open_timeout_show, open_timeout_store);
156
157static int notify_reset(struct smd_pkt_dev *smd_pkt_devp)
158{
159 smd_pkt_devp->do_reset_notification = 0;
160
161 return -ENETRESET;
162}
163
164static void clean_and_signal(struct smd_pkt_dev *smd_pkt_devp)
165{
166 smd_pkt_devp->do_reset_notification = 1;
167 smd_pkt_devp->has_reset = 1;
168
169 smd_pkt_devp->is_open = 0;
170
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700171 wake_up(&smd_pkt_devp->ch_read_wait_queue);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700172 wake_up(&smd_pkt_devp->ch_write_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700173 wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue);
174}
175
176static void loopback_probe_worker(struct work_struct *work)
177{
178
179 /* Wait for the modem SMSM to be inited for the SMD
180 ** Loopback channel to be allocated at the modem. Since
181 ** the wait need to be done atmost once, using msleep
182 ** doesn't degrade the performance. */
183 if (!is_modem_smsm_inited())
184 schedule_delayed_work(&loopback_work, msecs_to_jiffies(1000));
185 else
186 smsm_change_state(SMSM_APPS_STATE,
187 0, SMSM_SMD_LOOPBACK);
188
189}
190
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700191static void packet_arrival_worker(struct work_struct *work)
192{
193 struct smd_pkt_dev *smd_pkt_devp;
194
195 smd_pkt_devp = container_of(work, struct smd_pkt_dev,
196 packet_arrival_work);
197 mutex_lock(&smd_pkt_devp->ch_lock);
198 if (smd_pkt_devp->ch)
199 wake_lock_timeout(&smd_pkt_devp->pa_wake_lock,
200 WAKELOCK_TIMEOUT);
201 mutex_unlock(&smd_pkt_devp->ch_lock);
202}
203
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204static long smd_pkt_ioctl(struct file *file, unsigned int cmd,
205 unsigned long arg)
206{
207 int ret;
208 struct smd_pkt_dev *smd_pkt_devp;
209
210 smd_pkt_devp = file->private_data;
211 if (!smd_pkt_devp)
212 return -EINVAL;
213
214 switch (cmd) {
215 case TIOCMGET:
216 ret = smd_tiocmget(smd_pkt_devp->ch);
217 break;
218 case TIOCMSET:
219 ret = smd_tiocmset(smd_pkt_devp->ch, arg, ~arg);
220 break;
221 case SMD_PKT_IOCTL_BLOCKING_WRITE:
222 ret = get_user(smd_pkt_devp->blocking_write, (int *)arg);
223 break;
224 default:
225 ret = -1;
226 }
227
228 return ret;
229}
230
231ssize_t smd_pkt_read(struct file *file,
232 char __user *buf,
233 size_t count,
234 loff_t *ppos)
235{
236 int r;
237 int bytes_read;
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700238 int pkt_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239 struct smd_pkt_dev *smd_pkt_devp;
240 struct smd_channel *chl;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700241 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700242
243 D(KERN_ERR "%s: read %i bytes\n",
244 __func__, count);
245
246 smd_pkt_devp = file->private_data;
247
248 if (!smd_pkt_devp || !smd_pkt_devp->ch)
249 return -EINVAL;
250
251 if (smd_pkt_devp->do_reset_notification) {
252 /* notify client that a reset occurred */
253 return notify_reset(smd_pkt_devp);
254 }
255
256 chl = smd_pkt_devp->ch;
257wait_for_packet:
258 r = wait_event_interruptible(smd_pkt_devp->ch_read_wait_queue,
259 (smd_cur_packet_size(chl) > 0 &&
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700260 smd_read_avail(chl)) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 smd_pkt_devp->has_reset);
262
263 if (smd_pkt_devp->has_reset)
264 return notify_reset(smd_pkt_devp);
265
266 if (r < 0) {
267 /* qualify error message */
268 if (r != -ERESTARTSYS) {
269 /* we get this anytime a signal comes in */
270 printk(KERN_ERR "ERROR:%s:%i:%s: "
271 "wait_event_interruptible ret %i\n",
272 __FILE__,
273 __LINE__,
274 __func__,
275 r
276 );
277 }
278 return r;
279 }
280
281 /* Here we have a whole packet waiting for us */
282
283 mutex_lock(&smd_pkt_devp->rx_lock);
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700284 pkt_size = smd_cur_packet_size(smd_pkt_devp->ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700285
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700286 if (!pkt_size) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 D(KERN_ERR "%s: Nothing to read\n", __func__);
288 mutex_unlock(&smd_pkt_devp->rx_lock);
289 goto wait_for_packet;
290 }
291
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700292 if (pkt_size > count) {
293 pr_err("packet size %i > buffer size %i,", pkt_size, count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294 mutex_unlock(&smd_pkt_devp->rx_lock);
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700295 return -ETOOSMALL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700296 }
297
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700298 bytes_read = 0;
299 do {
300 r = smd_read_user_buffer(smd_pkt_devp->ch,
301 (buf + bytes_read),
302 (pkt_size - bytes_read));
303 if (r < 0) {
304 mutex_unlock(&smd_pkt_devp->rx_lock);
305 if (smd_pkt_devp->has_reset)
306 return notify_reset(smd_pkt_devp);
307 return r;
308 }
309 bytes_read += r;
310 if (pkt_size != bytes_read)
311 wait_event(smd_pkt_devp->ch_read_wait_queue,
312 smd_read_avail(smd_pkt_devp->ch) ||
313 smd_pkt_devp->has_reset);
314 if (smd_pkt_devp->has_reset) {
315 mutex_unlock(&smd_pkt_devp->rx_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700316 return notify_reset(smd_pkt_devp);
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700317 }
318 } while (pkt_size != bytes_read);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319 D_DUMP_BUFFER("read: ", bytes_read, buf);
320 mutex_unlock(&smd_pkt_devp->rx_lock);
321
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700322 mutex_lock(&smd_pkt_devp->ch_lock);
323 spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags);
324 if (smd_pkt_devp->poll_mode &&
325 !smd_cur_packet_size(smd_pkt_devp->ch)) {
326 wake_unlock(&smd_pkt_devp->pa_wake_lock);
327 smd_pkt_devp->poll_mode = 0;
328 }
329 spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags);
330 mutex_unlock(&smd_pkt_devp->ch_lock);
331
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700332 D(KERN_ERR "%s: just read %i bytes\n",
333 __func__, bytes_read);
334
335 /* check and wakeup read threads waiting on this device */
336 check_and_wakeup_reader(smd_pkt_devp);
337
338 return bytes_read;
339}
340
341ssize_t smd_pkt_write(struct file *file,
342 const char __user *buf,
343 size_t count,
344 loff_t *ppos)
345{
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700346 int r = 0, bytes_written;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700347 struct smd_pkt_dev *smd_pkt_devp;
348 DEFINE_WAIT(write_wait);
349
350 D(KERN_ERR "%s: writting %i bytes\n",
351 __func__, count);
352
353 smd_pkt_devp = file->private_data;
354
355 if (!smd_pkt_devp || !smd_pkt_devp->ch)
356 return -EINVAL;
357
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700358 if (smd_pkt_devp->do_reset_notification) {
359 /* notify client that a reset occurred */
360 return notify_reset(smd_pkt_devp);
361 }
362
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700363 mutex_lock(&smd_pkt_devp->tx_lock);
364 if (!smd_pkt_devp->blocking_write) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700365 if (smd_write_avail(smd_pkt_devp->ch) < count) {
366 D(KERN_ERR "%s: Not enough space to write\n",
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700367 __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368 mutex_unlock(&smd_pkt_devp->tx_lock);
369 return -ENOMEM;
370 }
371 }
372
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700373 r = smd_write_start(smd_pkt_devp->ch, count);
374 if (r < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375 mutex_unlock(&smd_pkt_devp->tx_lock);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700376 pr_err("%s: Error %d @ smd_write_start\n", __func__, r);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377 return r;
378 }
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700379
380 bytes_written = 0;
381 do {
382 prepare_to_wait(&smd_pkt_devp->ch_write_wait_queue,
383 &write_wait, TASK_UNINTERRUPTIBLE);
384 if (!smd_write_avail(smd_pkt_devp->ch) &&
385 !smd_pkt_devp->has_reset) {
386 smd_enable_read_intr(smd_pkt_devp->ch);
387 schedule();
388 }
389 finish_wait(&smd_pkt_devp->ch_write_wait_queue, &write_wait);
390 smd_disable_read_intr(smd_pkt_devp->ch);
391
392 if (smd_pkt_devp->has_reset) {
393 mutex_unlock(&smd_pkt_devp->tx_lock);
394 return notify_reset(smd_pkt_devp);
395 } else {
396 r = smd_write_segment(smd_pkt_devp->ch,
397 (void *)(buf + bytes_written),
398 (count - bytes_written), 1);
399 if (r < 0) {
400 mutex_unlock(&smd_pkt_devp->tx_lock);
401 if (smd_pkt_devp->has_reset)
402 return notify_reset(smd_pkt_devp);
403 }
404 bytes_written += r;
405 }
406 } while (bytes_written != count);
407 smd_write_end(smd_pkt_devp->ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408 mutex_unlock(&smd_pkt_devp->tx_lock);
409
410 D(KERN_ERR "%s: just wrote %i bytes\n",
411 __func__, count);
412
413 return count;
414}
415
416static unsigned int smd_pkt_poll(struct file *file, poll_table *wait)
417{
418 struct smd_pkt_dev *smd_pkt_devp;
419 unsigned int mask = 0;
420
421 smd_pkt_devp = file->private_data;
422 if (!smd_pkt_devp)
423 return POLLERR;
424
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700425 smd_pkt_devp->poll_mode = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426 poll_wait(file, &smd_pkt_devp->ch_read_wait_queue, wait);
427 if (smd_read_avail(smd_pkt_devp->ch))
428 mask |= POLLIN | POLLRDNORM;
429
430 return mask;
431}
432
433static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp)
434{
435 int sz;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700436 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437
438 if (!smd_pkt_devp || !smd_pkt_devp->ch)
439 return;
440
441 sz = smd_cur_packet_size(smd_pkt_devp->ch);
442 if (sz == 0) {
443 D(KERN_ERR "%s: packet size is 0\n", __func__);
444 return;
445 }
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700446 if (!smd_read_avail(smd_pkt_devp->ch)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447 D(KERN_ERR "%s: packet size is %i - "
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700448 "but the data isn't here\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449 __func__, sz);
450 return;
451 }
452
453 /* here we have a packet of size sz ready */
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700454 wake_up(&smd_pkt_devp->ch_read_wait_queue);
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700455 spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags);
456 wake_lock(&smd_pkt_devp->pa_wake_lock);
457 spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags);
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700458 schedule_work(&smd_pkt_devp->packet_arrival_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 D(KERN_ERR "%s: after wake_up\n", __func__);
460}
461
462static void check_and_wakeup_writer(struct smd_pkt_dev *smd_pkt_devp)
463{
464 int sz;
465
466 if (!smd_pkt_devp || !smd_pkt_devp->ch)
467 return;
468
469 sz = smd_write_avail(smd_pkt_devp->ch);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700470 if (sz) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471 D(KERN_ERR "%s: %d bytes Write Space available\n",
472 __func__, sz);
473 smd_disable_read_intr(smd_pkt_devp->ch);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700474 wake_up(&smd_pkt_devp->ch_write_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475 }
476}
477
478static void ch_notify(void *priv, unsigned event)
479{
480 struct smd_pkt_dev *smd_pkt_devp = priv;
481
482 if (smd_pkt_devp->ch == 0)
483 return;
484
485 switch (event) {
486 case SMD_EVENT_DATA: {
487 D(KERN_ERR "%s: data\n", __func__);
488 check_and_wakeup_reader(smd_pkt_devp);
489 if (smd_pkt_devp->blocking_write)
490 check_and_wakeup_writer(smd_pkt_devp);
491 D(KERN_ERR "%s: data after check_and_wakeup\n", __func__);
492 break;
493 }
494 case SMD_EVENT_OPEN:
495 D(KERN_ERR "%s: smd opened\n",
496 __func__);
497
498 smd_pkt_devp->has_reset = 0;
499 smd_pkt_devp->is_open = 1;
500 wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue);
501 break;
502 case SMD_EVENT_CLOSE:
503 smd_pkt_devp->is_open = 0;
504 printk(KERN_ERR "%s: smd closed\n",
505 __func__);
506
507 /* put port into reset state */
508 clean_and_signal(smd_pkt_devp);
509 if (smd_pkt_devp->i == LOOPBACK_INX)
510 schedule_delayed_work(&loopback_work,
511 msecs_to_jiffies(1000));
512 break;
513 }
514}
515
516#ifdef CONFIG_ARCH_FSM9XXX
517static char *smd_pkt_dev_name[] = {
518 "smdcntl1",
519 "smdcntl2",
520 "smd22",
521 "smd_pkt_loopback",
522};
523
524static char *smd_ch_name[] = {
525 "DATA6_CNTL",
526 "DATA7_CNTL",
527 "DATA22",
528 "LOOPBACK",
529};
530
531static uint32_t smd_ch_edge[] = {
532 SMD_APPS_QDSP,
533 SMD_APPS_QDSP,
534 SMD_APPS_QDSP,
535 SMD_APPS_QDSP
536};
537#else
538static char *smd_pkt_dev_name[] = {
539 "smdcntl0",
540 "smdcntl1",
541 "smdcntl2",
542 "smdcntl3",
543 "smdcntl4",
544 "smdcntl5",
545 "smdcntl6",
546 "smdcntl7",
547 "smd22",
548 "smd_sns_dsps",
Angshuman Sarkara18a2722011-07-29 13:41:13 +0530549 "apr_apps2",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550 "smd_pkt_loopback",
551};
552
553static char *smd_ch_name[] = {
554 "DATA5_CNTL",
555 "DATA6_CNTL",
556 "DATA7_CNTL",
557 "DATA8_CNTL",
558 "DATA9_CNTL",
559 "DATA12_CNTL",
560 "DATA13_CNTL",
561 "DATA14_CNTL",
562 "DATA22",
563 "SENSOR",
Angshuman Sarkara18a2722011-07-29 13:41:13 +0530564 "apr_apps2",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565 "LOOPBACK",
566};
567
568static uint32_t smd_ch_edge[] = {
569 SMD_APPS_MODEM,
570 SMD_APPS_MODEM,
571 SMD_APPS_MODEM,
572 SMD_APPS_MODEM,
573 SMD_APPS_MODEM,
574 SMD_APPS_MODEM,
575 SMD_APPS_MODEM,
576 SMD_APPS_MODEM,
577 SMD_APPS_MODEM,
578 SMD_APPS_DSPS,
579 SMD_APPS_QDSP,
580 SMD_APPS_MODEM,
581};
582#endif
583
584static int smd_pkt_dummy_probe(struct platform_device *pdev)
585{
586 int i;
587
588 for (i = 0; i < NUM_SMD_PKT_PORTS; i++) {
Jeff Hugoa5ee4362011-07-15 13:48:48 -0600589 if (!strncmp(pdev->name, smd_ch_name[i], SMD_MAX_CH_NAME_LEN)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590 complete_all(&smd_pkt_devp[i]->ch_allocated);
591 break;
592 }
593 }
594 return 0;
595}
596
597static uint32_t is_modem_smsm_inited(void)
598{
599 uint32_t modem_state;
600 uint32_t ready_state = (SMSM_INIT | SMSM_SMDINIT);
601
602 modem_state = smsm_get_state(SMSM_MODEM_STATE);
603 return (modem_state & ready_state) == ready_state;
604}
605
606int smd_pkt_open(struct inode *inode, struct file *file)
607{
608 int r = 0;
609 struct smd_pkt_dev *smd_pkt_devp;
Eric Holmbergcce6daf2012-02-27 14:34:02 -0700610 const char *peripheral = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611
612 smd_pkt_devp = container_of(inode->i_cdev, struct smd_pkt_dev, cdev);
613
614 if (!smd_pkt_devp)
615 return -EINVAL;
616
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700617 wake_lock_init(&smd_pkt_devp->pa_wake_lock, WAKE_LOCK_SUSPEND,
618 smd_pkt_dev_name[smd_pkt_devp->i]);
619 INIT_WORK(&smd_pkt_devp->packet_arrival_work, packet_arrival_worker);
620
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621 file->private_data = smd_pkt_devp;
622
623 mutex_lock(&smd_pkt_devp->ch_lock);
624 if (smd_pkt_devp->ch == 0) {
Eric Holmbergcce6daf2012-02-27 14:34:02 -0700625 peripheral = smd_edge_to_subsystem(
626 smd_ch_edge[smd_pkt_devp->i]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627 if (peripheral) {
628 smd_pkt_devp->pil = pil_get(peripheral);
629 if (IS_ERR(smd_pkt_devp->pil)) {
630 r = PTR_ERR(smd_pkt_devp->pil);
631 goto out;
632 }
633
634 /* Wait for the modem SMSM to be inited for the SMD
635 ** Loopback channel to be allocated at the modem. Since
636 ** the wait need to be done atmost once, using msleep
637 ** doesn't degrade the performance. */
Jeff Hugoa5ee4362011-07-15 13:48:48 -0600638 if (!strncmp(smd_ch_name[smd_pkt_devp->i], "LOOPBACK",
639 SMD_MAX_CH_NAME_LEN)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640 if (!is_modem_smsm_inited())
641 msleep(5000);
642 smsm_change_state(SMSM_APPS_STATE,
643 0, SMSM_SMD_LOOPBACK);
644 msleep(100);
645 }
646
647 /*
648 * Wait for a packet channel to be allocated so we know
649 * the modem is ready enough.
650 */
651 if (smd_pkt_devp->open_modem_wait) {
652 r = wait_for_completion_interruptible_timeout(
653 &smd_pkt_devp->ch_allocated,
654 msecs_to_jiffies(
655 smd_pkt_devp->open_modem_wait
656 * 1000));
657 if (r == 0)
658 r = -ETIMEDOUT;
659 if (r < 0) {
660 pr_err("%s: wait failed for smd port:"
661 " %d\n", __func__, r);
662 goto release_pil;
663 }
664 }
665 }
666
667 r = smd_named_open_on_edge(smd_ch_name[smd_pkt_devp->i],
668 smd_ch_edge[smd_pkt_devp->i],
669 &smd_pkt_devp->ch,
670 smd_pkt_devp,
671 ch_notify);
672 if (r < 0) {
673 pr_err("%s: %s open failed %d\n", __func__,
674 smd_ch_name[smd_pkt_devp->i], r);
675 goto release_pil;
676 }
677
678 r = wait_event_interruptible_timeout(
679 smd_pkt_devp->ch_opened_wait_queue,
680 smd_pkt_devp->is_open, (2 * HZ));
681 if (r == 0)
682 r = -ETIMEDOUT;
683
684 if (r < 0) {
685 pr_err("%s: wait failed for smd open: %d\n",
686 __func__, r);
687 } else if (!smd_pkt_devp->is_open) {
688 pr_err("%s: Invalid open notification\n", __func__);
689 r = -ENODEV;
690 } else {
691 smd_disable_read_intr(smd_pkt_devp->ch);
692 smd_pkt_devp->ch_size =
693 smd_write_avail(smd_pkt_devp->ch);
694 r = 0;
695 }
696 }
697release_pil:
698 if (peripheral && (r < 0))
699 pil_put(smd_pkt_devp->pil);
700out:
701 mutex_unlock(&smd_pkt_devp->ch_lock);
702
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700703 if (r < 0)
704 wake_lock_destroy(&smd_pkt_devp->pa_wake_lock);
705
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700706 return r;
707}
708
709int smd_pkt_release(struct inode *inode, struct file *file)
710{
711 int r = 0;
712 struct smd_pkt_dev *smd_pkt_devp = file->private_data;
713
714 if (!smd_pkt_devp)
715 return -EINVAL;
716
717 clean_and_signal(smd_pkt_devp);
718
719 mutex_lock(&smd_pkt_devp->ch_lock);
720 if (smd_pkt_devp->ch != 0) {
721 r = smd_close(smd_pkt_devp->ch);
722 smd_pkt_devp->ch = 0;
723 smd_pkt_devp->blocking_write = 0;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700724 smd_pkt_devp->poll_mode = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725 if (smd_pkt_devp->pil)
726 pil_put(smd_pkt_devp->pil);
727 }
728 mutex_unlock(&smd_pkt_devp->ch_lock);
729
730 smd_pkt_devp->has_reset = 0;
731 smd_pkt_devp->do_reset_notification = 0;
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700732 wake_lock_destroy(&smd_pkt_devp->pa_wake_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700733
734 return r;
735}
736
737static const struct file_operations smd_pkt_fops = {
738 .owner = THIS_MODULE,
739 .open = smd_pkt_open,
740 .release = smd_pkt_release,
741 .read = smd_pkt_read,
742 .write = smd_pkt_write,
743 .poll = smd_pkt_poll,
744 .unlocked_ioctl = smd_pkt_ioctl,
745};
746
747static int __init smd_pkt_init(void)
748{
749 int i;
750 int r;
751
752 r = alloc_chrdev_region(&smd_pkt_number,
753 0,
754 NUM_SMD_PKT_PORTS,
755 DEVICE_NAME);
756 if (IS_ERR_VALUE(r)) {
757 printk(KERN_ERR "ERROR:%s:%i:%s: "
758 "alloc_chrdev_region() ret %i.\n",
759 __FILE__,
760 __LINE__,
761 __func__,
762 r);
763 goto error0;
764 }
765
766 smd_pkt_classp = class_create(THIS_MODULE, DEVICE_NAME);
767 if (IS_ERR(smd_pkt_classp)) {
768 printk(KERN_ERR "ERROR:%s:%i:%s: "
769 "class_create() ENOMEM\n",
770 __FILE__,
771 __LINE__,
772 __func__);
773 r = -ENOMEM;
774 goto error1;
775 }
776
777 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
778 smd_pkt_devp[i] = kzalloc(sizeof(struct smd_pkt_dev),
779 GFP_KERNEL);
780 if (IS_ERR(smd_pkt_devp[i])) {
781 printk(KERN_ERR "ERROR:%s:%i:%s kmalloc() ENOMEM\n",
782 __FILE__,
783 __LINE__,
784 __func__);
785 r = -ENOMEM;
786 goto error2;
787 }
788
789 smd_pkt_devp[i]->i = i;
790
791 init_waitqueue_head(&smd_pkt_devp[i]->ch_read_wait_queue);
792 init_waitqueue_head(&smd_pkt_devp[i]->ch_write_wait_queue);
793 smd_pkt_devp[i]->is_open = 0;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700794 smd_pkt_devp[i]->poll_mode = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795 init_waitqueue_head(&smd_pkt_devp[i]->ch_opened_wait_queue);
796
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700797 spin_lock_init(&smd_pkt_devp[i]->pa_spinlock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798 mutex_init(&smd_pkt_devp[i]->ch_lock);
799 mutex_init(&smd_pkt_devp[i]->rx_lock);
800 mutex_init(&smd_pkt_devp[i]->tx_lock);
801 init_completion(&smd_pkt_devp[i]->ch_allocated);
802
803 cdev_init(&smd_pkt_devp[i]->cdev, &smd_pkt_fops);
804 smd_pkt_devp[i]->cdev.owner = THIS_MODULE;
805
806 r = cdev_add(&smd_pkt_devp[i]->cdev,
807 (smd_pkt_number + i),
808 1);
809
810 if (IS_ERR_VALUE(r)) {
811 printk(KERN_ERR "%s:%i:%s: cdev_add() ret %i\n",
812 __FILE__,
813 __LINE__,
814 __func__,
815 r);
816 kfree(smd_pkt_devp[i]);
817 goto error2;
818 }
819
820 smd_pkt_devp[i]->devicep =
821 device_create(smd_pkt_classp,
822 NULL,
823 (smd_pkt_number + i),
824 NULL,
825 smd_pkt_dev_name[i]);
826
827 if (IS_ERR(smd_pkt_devp[i]->devicep)) {
828 printk(KERN_ERR "%s:%i:%s: "
829 "device_create() ENOMEM\n",
830 __FILE__,
831 __LINE__,
832 __func__);
833 r = -ENOMEM;
834 cdev_del(&smd_pkt_devp[i]->cdev);
835 kfree(smd_pkt_devp[i]);
836 goto error2;
837 }
838 if (device_create_file(smd_pkt_devp[i]->devicep,
839 &dev_attr_open_timeout))
840 pr_err("%s: unable to create device attr on #%d\n",
841 __func__, i);
842
843 smd_pkt_devp[i]->driver.probe = smd_pkt_dummy_probe;
844 smd_pkt_devp[i]->driver.driver.name = smd_ch_name[i];
845 smd_pkt_devp[i]->driver.driver.owner = THIS_MODULE;
846 r = platform_driver_register(&smd_pkt_devp[i]->driver);
847 if (r)
848 goto error2;
849 }
850
851 INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker);
852
853 D(KERN_INFO "SMD Packet Port Driver Initialized.\n");
854 return 0;
855
856 error2:
857 if (i > 0) {
858 while (--i >= 0) {
859 platform_driver_unregister(&smd_pkt_devp[i]->driver);
860 cdev_del(&smd_pkt_devp[i]->cdev);
861 kfree(smd_pkt_devp[i]);
862 device_destroy(smd_pkt_classp,
863 MKDEV(MAJOR(smd_pkt_number), i));
864 }
865 }
866
867 class_destroy(smd_pkt_classp);
868 error1:
869 unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
870 error0:
871 return r;
872}
873
874static void __exit smd_pkt_cleanup(void)
875{
876 int i;
877
878 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
879 platform_driver_unregister(&smd_pkt_devp[i]->driver);
880 cdev_del(&smd_pkt_devp[i]->cdev);
881 kfree(smd_pkt_devp[i]);
882 device_destroy(smd_pkt_classp,
883 MKDEV(MAJOR(smd_pkt_number), i));
884 }
885
886 class_destroy(smd_pkt_classp);
887
888 unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
889}
890
891module_init(smd_pkt_init);
892module_exit(smd_pkt_cleanup);
893
894MODULE_DESCRIPTION("MSM Shared Memory Packet Port");
895MODULE_LICENSE("GPL v2");