blob: aeffbfd4054bd1636b2edc2c20e1af36150e0b0c [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;
610 char *peripheral = NULL;
611
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) {
625
626 if (smd_ch_edge[smd_pkt_devp->i] == SMD_APPS_MODEM)
627 peripheral = "modem";
628 else if (smd_ch_edge[smd_pkt_devp->i] == SMD_APPS_QDSP)
629 peripheral = "q6";
630
631 if (peripheral) {
632 smd_pkt_devp->pil = pil_get(peripheral);
633 if (IS_ERR(smd_pkt_devp->pil)) {
634 r = PTR_ERR(smd_pkt_devp->pil);
635 goto out;
636 }
637
638 /* Wait for the modem SMSM to be inited for the SMD
639 ** Loopback channel to be allocated at the modem. Since
640 ** the wait need to be done atmost once, using msleep
641 ** doesn't degrade the performance. */
Jeff Hugoa5ee4362011-07-15 13:48:48 -0600642 if (!strncmp(smd_ch_name[smd_pkt_devp->i], "LOOPBACK",
643 SMD_MAX_CH_NAME_LEN)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644 if (!is_modem_smsm_inited())
645 msleep(5000);
646 smsm_change_state(SMSM_APPS_STATE,
647 0, SMSM_SMD_LOOPBACK);
648 msleep(100);
649 }
650
651 /*
652 * Wait for a packet channel to be allocated so we know
653 * the modem is ready enough.
654 */
655 if (smd_pkt_devp->open_modem_wait) {
656 r = wait_for_completion_interruptible_timeout(
657 &smd_pkt_devp->ch_allocated,
658 msecs_to_jiffies(
659 smd_pkt_devp->open_modem_wait
660 * 1000));
661 if (r == 0)
662 r = -ETIMEDOUT;
663 if (r < 0) {
664 pr_err("%s: wait failed for smd port:"
665 " %d\n", __func__, r);
666 goto release_pil;
667 }
668 }
669 }
670
671 r = smd_named_open_on_edge(smd_ch_name[smd_pkt_devp->i],
672 smd_ch_edge[smd_pkt_devp->i],
673 &smd_pkt_devp->ch,
674 smd_pkt_devp,
675 ch_notify);
676 if (r < 0) {
677 pr_err("%s: %s open failed %d\n", __func__,
678 smd_ch_name[smd_pkt_devp->i], r);
679 goto release_pil;
680 }
681
682 r = wait_event_interruptible_timeout(
683 smd_pkt_devp->ch_opened_wait_queue,
684 smd_pkt_devp->is_open, (2 * HZ));
685 if (r == 0)
686 r = -ETIMEDOUT;
687
688 if (r < 0) {
689 pr_err("%s: wait failed for smd open: %d\n",
690 __func__, r);
691 } else if (!smd_pkt_devp->is_open) {
692 pr_err("%s: Invalid open notification\n", __func__);
693 r = -ENODEV;
694 } else {
695 smd_disable_read_intr(smd_pkt_devp->ch);
696 smd_pkt_devp->ch_size =
697 smd_write_avail(smd_pkt_devp->ch);
698 r = 0;
699 }
700 }
701release_pil:
702 if (peripheral && (r < 0))
703 pil_put(smd_pkt_devp->pil);
704out:
705 mutex_unlock(&smd_pkt_devp->ch_lock);
706
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700707 if (r < 0)
708 wake_lock_destroy(&smd_pkt_devp->pa_wake_lock);
709
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710 return r;
711}
712
713int smd_pkt_release(struct inode *inode, struct file *file)
714{
715 int r = 0;
716 struct smd_pkt_dev *smd_pkt_devp = file->private_data;
717
718 if (!smd_pkt_devp)
719 return -EINVAL;
720
721 clean_and_signal(smd_pkt_devp);
722
723 mutex_lock(&smd_pkt_devp->ch_lock);
724 if (smd_pkt_devp->ch != 0) {
725 r = smd_close(smd_pkt_devp->ch);
726 smd_pkt_devp->ch = 0;
727 smd_pkt_devp->blocking_write = 0;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700728 smd_pkt_devp->poll_mode = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700729 if (smd_pkt_devp->pil)
730 pil_put(smd_pkt_devp->pil);
731 }
732 mutex_unlock(&smd_pkt_devp->ch_lock);
733
734 smd_pkt_devp->has_reset = 0;
735 smd_pkt_devp->do_reset_notification = 0;
Eric Holmbergc3c5cd92012-02-07 18:19:49 -0700736 wake_lock_destroy(&smd_pkt_devp->pa_wake_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737
738 return r;
739}
740
741static const struct file_operations smd_pkt_fops = {
742 .owner = THIS_MODULE,
743 .open = smd_pkt_open,
744 .release = smd_pkt_release,
745 .read = smd_pkt_read,
746 .write = smd_pkt_write,
747 .poll = smd_pkt_poll,
748 .unlocked_ioctl = smd_pkt_ioctl,
749};
750
751static int __init smd_pkt_init(void)
752{
753 int i;
754 int r;
755
756 r = alloc_chrdev_region(&smd_pkt_number,
757 0,
758 NUM_SMD_PKT_PORTS,
759 DEVICE_NAME);
760 if (IS_ERR_VALUE(r)) {
761 printk(KERN_ERR "ERROR:%s:%i:%s: "
762 "alloc_chrdev_region() ret %i.\n",
763 __FILE__,
764 __LINE__,
765 __func__,
766 r);
767 goto error0;
768 }
769
770 smd_pkt_classp = class_create(THIS_MODULE, DEVICE_NAME);
771 if (IS_ERR(smd_pkt_classp)) {
772 printk(KERN_ERR "ERROR:%s:%i:%s: "
773 "class_create() ENOMEM\n",
774 __FILE__,
775 __LINE__,
776 __func__);
777 r = -ENOMEM;
778 goto error1;
779 }
780
781 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
782 smd_pkt_devp[i] = kzalloc(sizeof(struct smd_pkt_dev),
783 GFP_KERNEL);
784 if (IS_ERR(smd_pkt_devp[i])) {
785 printk(KERN_ERR "ERROR:%s:%i:%s kmalloc() ENOMEM\n",
786 __FILE__,
787 __LINE__,
788 __func__);
789 r = -ENOMEM;
790 goto error2;
791 }
792
793 smd_pkt_devp[i]->i = i;
794
795 init_waitqueue_head(&smd_pkt_devp[i]->ch_read_wait_queue);
796 init_waitqueue_head(&smd_pkt_devp[i]->ch_write_wait_queue);
797 smd_pkt_devp[i]->is_open = 0;
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700798 smd_pkt_devp[i]->poll_mode = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799 init_waitqueue_head(&smd_pkt_devp[i]->ch_opened_wait_queue);
800
Karthikeyan Ramasubramanian048ef382012-02-15 10:49:10 -0700801 spin_lock_init(&smd_pkt_devp[i]->pa_spinlock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700802 mutex_init(&smd_pkt_devp[i]->ch_lock);
803 mutex_init(&smd_pkt_devp[i]->rx_lock);
804 mutex_init(&smd_pkt_devp[i]->tx_lock);
805 init_completion(&smd_pkt_devp[i]->ch_allocated);
806
807 cdev_init(&smd_pkt_devp[i]->cdev, &smd_pkt_fops);
808 smd_pkt_devp[i]->cdev.owner = THIS_MODULE;
809
810 r = cdev_add(&smd_pkt_devp[i]->cdev,
811 (smd_pkt_number + i),
812 1);
813
814 if (IS_ERR_VALUE(r)) {
815 printk(KERN_ERR "%s:%i:%s: cdev_add() ret %i\n",
816 __FILE__,
817 __LINE__,
818 __func__,
819 r);
820 kfree(smd_pkt_devp[i]);
821 goto error2;
822 }
823
824 smd_pkt_devp[i]->devicep =
825 device_create(smd_pkt_classp,
826 NULL,
827 (smd_pkt_number + i),
828 NULL,
829 smd_pkt_dev_name[i]);
830
831 if (IS_ERR(smd_pkt_devp[i]->devicep)) {
832 printk(KERN_ERR "%s:%i:%s: "
833 "device_create() ENOMEM\n",
834 __FILE__,
835 __LINE__,
836 __func__);
837 r = -ENOMEM;
838 cdev_del(&smd_pkt_devp[i]->cdev);
839 kfree(smd_pkt_devp[i]);
840 goto error2;
841 }
842 if (device_create_file(smd_pkt_devp[i]->devicep,
843 &dev_attr_open_timeout))
844 pr_err("%s: unable to create device attr on #%d\n",
845 __func__, i);
846
847 smd_pkt_devp[i]->driver.probe = smd_pkt_dummy_probe;
848 smd_pkt_devp[i]->driver.driver.name = smd_ch_name[i];
849 smd_pkt_devp[i]->driver.driver.owner = THIS_MODULE;
850 r = platform_driver_register(&smd_pkt_devp[i]->driver);
851 if (r)
852 goto error2;
853 }
854
855 INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker);
856
857 D(KERN_INFO "SMD Packet Port Driver Initialized.\n");
858 return 0;
859
860 error2:
861 if (i > 0) {
862 while (--i >= 0) {
863 platform_driver_unregister(&smd_pkt_devp[i]->driver);
864 cdev_del(&smd_pkt_devp[i]->cdev);
865 kfree(smd_pkt_devp[i]);
866 device_destroy(smd_pkt_classp,
867 MKDEV(MAJOR(smd_pkt_number), i));
868 }
869 }
870
871 class_destroy(smd_pkt_classp);
872 error1:
873 unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
874 error0:
875 return r;
876}
877
878static void __exit smd_pkt_cleanup(void)
879{
880 int i;
881
882 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
883 platform_driver_unregister(&smd_pkt_devp[i]->driver);
884 cdev_del(&smd_pkt_devp[i]->cdev);
885 kfree(smd_pkt_devp[i]);
886 device_destroy(smd_pkt_classp,
887 MKDEV(MAJOR(smd_pkt_number), i));
888 }
889
890 class_destroy(smd_pkt_classp);
891
892 unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
893}
894
895module_init(smd_pkt_init);
896module_exit(smd_pkt_cleanup);
897
898MODULE_DESCRIPTION("MSM Shared Memory Packet Port");
899MODULE_LICENSE("GPL v2");