blob: 4744f1fb73d706b0ff36d736dae88dc0b6367b81 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2008-2011, Code Aurora Forum. All rights reserved.
2 *
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>
34
35#include <mach/msm_smd.h>
36#include <mach/peripheral-loader.h>
37
38#include "smd_private.h"
39#ifdef CONFIG_ARCH_FSM9XXX
40#define NUM_SMD_PKT_PORTS 4
41#else
42#define NUM_SMD_PKT_PORTS 12
43#endif
44
45#define LOOPBACK_INX (NUM_SMD_PKT_PORTS - 1)
46
47#define DEVICE_NAME "smdpkt"
48
49struct smd_pkt_dev {
50 struct cdev cdev;
51 struct device *devicep;
52 void *pil;
53 struct platform_driver driver;
54
55 struct smd_channel *ch;
56 struct mutex ch_lock;
57 struct mutex rx_lock;
58 struct mutex tx_lock;
59 wait_queue_head_t ch_read_wait_queue;
60 wait_queue_head_t ch_write_wait_queue;
61 wait_queue_head_t ch_opened_wait_queue;
62
63 int i;
64
65 int blocking_write;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066 int is_open;
67 unsigned ch_size;
68 uint open_modem_wait;
69
70 int has_reset;
71 int do_reset_notification;
72 struct completion ch_allocated;
73
74} *smd_pkt_devp[NUM_SMD_PKT_PORTS];
75
76struct class *smd_pkt_classp;
77static dev_t smd_pkt_number;
78static struct delayed_work loopback_work;
79static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp);
80static void check_and_wakeup_writer(struct smd_pkt_dev *smd_pkt_devp);
81static uint32_t is_modem_smsm_inited(void);
82
83static int msm_smd_pkt_debug_mask;
84module_param_named(debug_mask, msm_smd_pkt_debug_mask,
85 int, S_IRUGO | S_IWUSR | S_IWGRP);
86#define DEBUG
87
88#ifdef DEBUG
89#define D_DUMP_BUFFER(prestr, cnt, buf) \
90do { \
91 if (msm_smd_pkt_debug_mask) \
92 print_hex_dump(KERN_DEBUG, prestr, \
93 DUMP_PREFIX_NONE, 16, 1, \
94 buf, cnt, 1); \
95} while (0)
96#else
97#define D_DUMP_BUFFER(prestr, cnt, buf) do {} while (0)
98#endif
99
100#ifdef DEBUG
101#define D(x...) if (msm_smd_pkt_debug_mask) printk(x)
102#else
103#define D(x...) do {} while (0)
104#endif
105
106static ssize_t open_timeout_store(struct device *d,
107 struct device_attribute *attr,
108 const char *buf,
109 size_t n)
110{
111 int i;
112 unsigned long tmp;
113 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
114 if (smd_pkt_devp[i]->devicep == d)
115 break;
116 }
117 if (!strict_strtoul(buf, 10, &tmp)) {
118 smd_pkt_devp[i]->open_modem_wait = tmp;
119 return n;
120 } else {
121 pr_err("%s: unable to convert: %s to an int\n", __func__,
122 buf);
123 return -EINVAL;
124 }
125}
126
127static ssize_t open_timeout_show(struct device *d,
128 struct device_attribute *attr,
129 char *buf)
130{
131 int i;
132 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
133 if (smd_pkt_devp[i]->devicep == d)
134 break;
135 }
Karthikeyan Ramasubramanian63fa3d32011-09-29 17:06:26 -0600136 return snprintf(buf, PAGE_SIZE, "%d\n",
137 smd_pkt_devp[i]->open_modem_wait);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138}
139
140static DEVICE_ATTR(open_timeout, 0664, open_timeout_show, open_timeout_store);
141
142static int notify_reset(struct smd_pkt_dev *smd_pkt_devp)
143{
144 smd_pkt_devp->do_reset_notification = 0;
145
146 return -ENETRESET;
147}
148
149static void clean_and_signal(struct smd_pkt_dev *smd_pkt_devp)
150{
151 smd_pkt_devp->do_reset_notification = 1;
152 smd_pkt_devp->has_reset = 1;
153
154 smd_pkt_devp->is_open = 0;
155
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700156 wake_up(&smd_pkt_devp->ch_read_wait_queue);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700157 wake_up(&smd_pkt_devp->ch_write_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158 wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue);
159}
160
161static void loopback_probe_worker(struct work_struct *work)
162{
163
164 /* Wait for the modem SMSM to be inited for the SMD
165 ** Loopback channel to be allocated at the modem. Since
166 ** the wait need to be done atmost once, using msleep
167 ** doesn't degrade the performance. */
168 if (!is_modem_smsm_inited())
169 schedule_delayed_work(&loopback_work, msecs_to_jiffies(1000));
170 else
171 smsm_change_state(SMSM_APPS_STATE,
172 0, SMSM_SMD_LOOPBACK);
173
174}
175
176static long smd_pkt_ioctl(struct file *file, unsigned int cmd,
177 unsigned long arg)
178{
179 int ret;
180 struct smd_pkt_dev *smd_pkt_devp;
181
182 smd_pkt_devp = file->private_data;
183 if (!smd_pkt_devp)
184 return -EINVAL;
185
186 switch (cmd) {
187 case TIOCMGET:
188 ret = smd_tiocmget(smd_pkt_devp->ch);
189 break;
190 case TIOCMSET:
191 ret = smd_tiocmset(smd_pkt_devp->ch, arg, ~arg);
192 break;
193 case SMD_PKT_IOCTL_BLOCKING_WRITE:
194 ret = get_user(smd_pkt_devp->blocking_write, (int *)arg);
195 break;
196 default:
197 ret = -1;
198 }
199
200 return ret;
201}
202
203ssize_t smd_pkt_read(struct file *file,
204 char __user *buf,
205 size_t count,
206 loff_t *ppos)
207{
208 int r;
209 int bytes_read;
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700210 int pkt_size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 struct smd_pkt_dev *smd_pkt_devp;
212 struct smd_channel *chl;
213
214 D(KERN_ERR "%s: read %i bytes\n",
215 __func__, count);
216
217 smd_pkt_devp = file->private_data;
218
219 if (!smd_pkt_devp || !smd_pkt_devp->ch)
220 return -EINVAL;
221
222 if (smd_pkt_devp->do_reset_notification) {
223 /* notify client that a reset occurred */
224 return notify_reset(smd_pkt_devp);
225 }
226
227 chl = smd_pkt_devp->ch;
228wait_for_packet:
229 r = wait_event_interruptible(smd_pkt_devp->ch_read_wait_queue,
230 (smd_cur_packet_size(chl) > 0 &&
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700231 smd_read_avail(chl)) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232 smd_pkt_devp->has_reset);
233
234 if (smd_pkt_devp->has_reset)
235 return notify_reset(smd_pkt_devp);
236
237 if (r < 0) {
238 /* qualify error message */
239 if (r != -ERESTARTSYS) {
240 /* we get this anytime a signal comes in */
241 printk(KERN_ERR "ERROR:%s:%i:%s: "
242 "wait_event_interruptible ret %i\n",
243 __FILE__,
244 __LINE__,
245 __func__,
246 r
247 );
248 }
249 return r;
250 }
251
252 /* Here we have a whole packet waiting for us */
253
254 mutex_lock(&smd_pkt_devp->rx_lock);
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700255 pkt_size = smd_cur_packet_size(smd_pkt_devp->ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700257 if (!pkt_size) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258 D(KERN_ERR "%s: Nothing to read\n", __func__);
259 mutex_unlock(&smd_pkt_devp->rx_lock);
260 goto wait_for_packet;
261 }
262
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700263 if (pkt_size > count) {
264 pr_err("packet size %i > buffer size %i,", pkt_size, count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265 mutex_unlock(&smd_pkt_devp->rx_lock);
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700266 return -ETOOSMALL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267 }
268
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700269 bytes_read = 0;
270 do {
271 r = smd_read_user_buffer(smd_pkt_devp->ch,
272 (buf + bytes_read),
273 (pkt_size - bytes_read));
274 if (r < 0) {
275 mutex_unlock(&smd_pkt_devp->rx_lock);
276 if (smd_pkt_devp->has_reset)
277 return notify_reset(smd_pkt_devp);
278 return r;
279 }
280 bytes_read += r;
281 if (pkt_size != bytes_read)
282 wait_event(smd_pkt_devp->ch_read_wait_queue,
283 smd_read_avail(smd_pkt_devp->ch) ||
284 smd_pkt_devp->has_reset);
285 if (smd_pkt_devp->has_reset) {
286 mutex_unlock(&smd_pkt_devp->rx_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 return notify_reset(smd_pkt_devp);
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700288 }
289 } while (pkt_size != bytes_read);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290 D_DUMP_BUFFER("read: ", bytes_read, buf);
291 mutex_unlock(&smd_pkt_devp->rx_lock);
292
293 D(KERN_ERR "%s: just read %i bytes\n",
294 __func__, bytes_read);
295
296 /* check and wakeup read threads waiting on this device */
297 check_and_wakeup_reader(smd_pkt_devp);
298
299 return bytes_read;
300}
301
302ssize_t smd_pkt_write(struct file *file,
303 const char __user *buf,
304 size_t count,
305 loff_t *ppos)
306{
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700307 int r = 0, bytes_written;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308 struct smd_pkt_dev *smd_pkt_devp;
309 DEFINE_WAIT(write_wait);
310
311 D(KERN_ERR "%s: writting %i bytes\n",
312 __func__, count);
313
314 smd_pkt_devp = file->private_data;
315
316 if (!smd_pkt_devp || !smd_pkt_devp->ch)
317 return -EINVAL;
318
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319 if (smd_pkt_devp->do_reset_notification) {
320 /* notify client that a reset occurred */
321 return notify_reset(smd_pkt_devp);
322 }
323
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700324 mutex_lock(&smd_pkt_devp->tx_lock);
325 if (!smd_pkt_devp->blocking_write) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700326 if (smd_write_avail(smd_pkt_devp->ch) < count) {
327 D(KERN_ERR "%s: Not enough space to write\n",
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700328 __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700329 mutex_unlock(&smd_pkt_devp->tx_lock);
330 return -ENOMEM;
331 }
332 }
333
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700334 r = smd_write_start(smd_pkt_devp->ch, count);
335 if (r < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700336 mutex_unlock(&smd_pkt_devp->tx_lock);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700337 pr_err("%s: Error %d @ smd_write_start\n", __func__, r);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700338 return r;
339 }
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700340
341 bytes_written = 0;
342 do {
343 prepare_to_wait(&smd_pkt_devp->ch_write_wait_queue,
344 &write_wait, TASK_UNINTERRUPTIBLE);
345 if (!smd_write_avail(smd_pkt_devp->ch) &&
346 !smd_pkt_devp->has_reset) {
347 smd_enable_read_intr(smd_pkt_devp->ch);
348 schedule();
349 }
350 finish_wait(&smd_pkt_devp->ch_write_wait_queue, &write_wait);
351 smd_disable_read_intr(smd_pkt_devp->ch);
352
353 if (smd_pkt_devp->has_reset) {
354 mutex_unlock(&smd_pkt_devp->tx_lock);
355 return notify_reset(smd_pkt_devp);
356 } else {
357 r = smd_write_segment(smd_pkt_devp->ch,
358 (void *)(buf + bytes_written),
359 (count - bytes_written), 1);
360 if (r < 0) {
361 mutex_unlock(&smd_pkt_devp->tx_lock);
362 if (smd_pkt_devp->has_reset)
363 return notify_reset(smd_pkt_devp);
364 }
365 bytes_written += r;
366 }
367 } while (bytes_written != count);
368 smd_write_end(smd_pkt_devp->ch);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700369 mutex_unlock(&smd_pkt_devp->tx_lock);
370
371 D(KERN_ERR "%s: just wrote %i bytes\n",
372 __func__, count);
373
374 return count;
375}
376
377static unsigned int smd_pkt_poll(struct file *file, poll_table *wait)
378{
379 struct smd_pkt_dev *smd_pkt_devp;
380 unsigned int mask = 0;
381
382 smd_pkt_devp = file->private_data;
383 if (!smd_pkt_devp)
384 return POLLERR;
385
386 poll_wait(file, &smd_pkt_devp->ch_read_wait_queue, wait);
387 if (smd_read_avail(smd_pkt_devp->ch))
388 mask |= POLLIN | POLLRDNORM;
389
390 return mask;
391}
392
393static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp)
394{
395 int sz;
396
397 if (!smd_pkt_devp || !smd_pkt_devp->ch)
398 return;
399
400 sz = smd_cur_packet_size(smd_pkt_devp->ch);
401 if (sz == 0) {
402 D(KERN_ERR "%s: packet size is 0\n", __func__);
403 return;
404 }
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700405 if (!smd_read_avail(smd_pkt_devp->ch)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406 D(KERN_ERR "%s: packet size is %i - "
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700407 "but the data isn't here\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408 __func__, sz);
409 return;
410 }
411
412 /* here we have a packet of size sz ready */
Karthikeyan Ramasubramanian703e8a12011-11-08 16:50:06 -0700413 wake_up(&smd_pkt_devp->ch_read_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700414 D(KERN_ERR "%s: after wake_up\n", __func__);
415}
416
417static void check_and_wakeup_writer(struct smd_pkt_dev *smd_pkt_devp)
418{
419 int sz;
420
421 if (!smd_pkt_devp || !smd_pkt_devp->ch)
422 return;
423
424 sz = smd_write_avail(smd_pkt_devp->ch);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700425 if (sz) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426 D(KERN_ERR "%s: %d bytes Write Space available\n",
427 __func__, sz);
428 smd_disable_read_intr(smd_pkt_devp->ch);
Karthikeyan Ramasubramaniance2c1152011-11-07 14:17:41 -0700429 wake_up(&smd_pkt_devp->ch_write_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700430 }
431}
432
433static void ch_notify(void *priv, unsigned event)
434{
435 struct smd_pkt_dev *smd_pkt_devp = priv;
436
437 if (smd_pkt_devp->ch == 0)
438 return;
439
440 switch (event) {
441 case SMD_EVENT_DATA: {
442 D(KERN_ERR "%s: data\n", __func__);
443 check_and_wakeup_reader(smd_pkt_devp);
444 if (smd_pkt_devp->blocking_write)
445 check_and_wakeup_writer(smd_pkt_devp);
446 D(KERN_ERR "%s: data after check_and_wakeup\n", __func__);
447 break;
448 }
449 case SMD_EVENT_OPEN:
450 D(KERN_ERR "%s: smd opened\n",
451 __func__);
452
453 smd_pkt_devp->has_reset = 0;
454 smd_pkt_devp->is_open = 1;
455 wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue);
456 break;
457 case SMD_EVENT_CLOSE:
458 smd_pkt_devp->is_open = 0;
459 printk(KERN_ERR "%s: smd closed\n",
460 __func__);
461
462 /* put port into reset state */
463 clean_and_signal(smd_pkt_devp);
464 if (smd_pkt_devp->i == LOOPBACK_INX)
465 schedule_delayed_work(&loopback_work,
466 msecs_to_jiffies(1000));
467 break;
468 }
469}
470
471#ifdef CONFIG_ARCH_FSM9XXX
472static char *smd_pkt_dev_name[] = {
473 "smdcntl1",
474 "smdcntl2",
475 "smd22",
476 "smd_pkt_loopback",
477};
478
479static char *smd_ch_name[] = {
480 "DATA6_CNTL",
481 "DATA7_CNTL",
482 "DATA22",
483 "LOOPBACK",
484};
485
486static uint32_t smd_ch_edge[] = {
487 SMD_APPS_QDSP,
488 SMD_APPS_QDSP,
489 SMD_APPS_QDSP,
490 SMD_APPS_QDSP
491};
492#else
493static char *smd_pkt_dev_name[] = {
494 "smdcntl0",
495 "smdcntl1",
496 "smdcntl2",
497 "smdcntl3",
498 "smdcntl4",
499 "smdcntl5",
500 "smdcntl6",
501 "smdcntl7",
502 "smd22",
503 "smd_sns_dsps",
Angshuman Sarkara18a2722011-07-29 13:41:13 +0530504 "apr_apps2",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 "smd_pkt_loopback",
506};
507
508static char *smd_ch_name[] = {
509 "DATA5_CNTL",
510 "DATA6_CNTL",
511 "DATA7_CNTL",
512 "DATA8_CNTL",
513 "DATA9_CNTL",
514 "DATA12_CNTL",
515 "DATA13_CNTL",
516 "DATA14_CNTL",
517 "DATA22",
518 "SENSOR",
Angshuman Sarkara18a2722011-07-29 13:41:13 +0530519 "apr_apps2",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520 "LOOPBACK",
521};
522
523static uint32_t smd_ch_edge[] = {
524 SMD_APPS_MODEM,
525 SMD_APPS_MODEM,
526 SMD_APPS_MODEM,
527 SMD_APPS_MODEM,
528 SMD_APPS_MODEM,
529 SMD_APPS_MODEM,
530 SMD_APPS_MODEM,
531 SMD_APPS_MODEM,
532 SMD_APPS_MODEM,
533 SMD_APPS_DSPS,
534 SMD_APPS_QDSP,
535 SMD_APPS_MODEM,
536};
537#endif
538
539static int smd_pkt_dummy_probe(struct platform_device *pdev)
540{
541 int i;
542
543 for (i = 0; i < NUM_SMD_PKT_PORTS; i++) {
Jeff Hugoa5ee4362011-07-15 13:48:48 -0600544 if (!strncmp(pdev->name, smd_ch_name[i], SMD_MAX_CH_NAME_LEN)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545 complete_all(&smd_pkt_devp[i]->ch_allocated);
546 break;
547 }
548 }
549 return 0;
550}
551
552static uint32_t is_modem_smsm_inited(void)
553{
554 uint32_t modem_state;
555 uint32_t ready_state = (SMSM_INIT | SMSM_SMDINIT);
556
557 modem_state = smsm_get_state(SMSM_MODEM_STATE);
558 return (modem_state & ready_state) == ready_state;
559}
560
561int smd_pkt_open(struct inode *inode, struct file *file)
562{
563 int r = 0;
564 struct smd_pkt_dev *smd_pkt_devp;
565 char *peripheral = NULL;
566
567 smd_pkt_devp = container_of(inode->i_cdev, struct smd_pkt_dev, cdev);
568
569 if (!smd_pkt_devp)
570 return -EINVAL;
571
572 file->private_data = smd_pkt_devp;
573
574 mutex_lock(&smd_pkt_devp->ch_lock);
575 if (smd_pkt_devp->ch == 0) {
576
577 if (smd_ch_edge[smd_pkt_devp->i] == SMD_APPS_MODEM)
578 peripheral = "modem";
579 else if (smd_ch_edge[smd_pkt_devp->i] == SMD_APPS_QDSP)
580 peripheral = "q6";
581
582 if (peripheral) {
583 smd_pkt_devp->pil = pil_get(peripheral);
584 if (IS_ERR(smd_pkt_devp->pil)) {
585 r = PTR_ERR(smd_pkt_devp->pil);
586 goto out;
587 }
588
589 /* Wait for the modem SMSM to be inited for the SMD
590 ** Loopback channel to be allocated at the modem. Since
591 ** the wait need to be done atmost once, using msleep
592 ** doesn't degrade the performance. */
Jeff Hugoa5ee4362011-07-15 13:48:48 -0600593 if (!strncmp(smd_ch_name[smd_pkt_devp->i], "LOOPBACK",
594 SMD_MAX_CH_NAME_LEN)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595 if (!is_modem_smsm_inited())
596 msleep(5000);
597 smsm_change_state(SMSM_APPS_STATE,
598 0, SMSM_SMD_LOOPBACK);
599 msleep(100);
600 }
601
602 /*
603 * Wait for a packet channel to be allocated so we know
604 * the modem is ready enough.
605 */
606 if (smd_pkt_devp->open_modem_wait) {
607 r = wait_for_completion_interruptible_timeout(
608 &smd_pkt_devp->ch_allocated,
609 msecs_to_jiffies(
610 smd_pkt_devp->open_modem_wait
611 * 1000));
612 if (r == 0)
613 r = -ETIMEDOUT;
614 if (r < 0) {
615 pr_err("%s: wait failed for smd port:"
616 " %d\n", __func__, r);
617 goto release_pil;
618 }
619 }
620 }
621
622 r = smd_named_open_on_edge(smd_ch_name[smd_pkt_devp->i],
623 smd_ch_edge[smd_pkt_devp->i],
624 &smd_pkt_devp->ch,
625 smd_pkt_devp,
626 ch_notify);
627 if (r < 0) {
628 pr_err("%s: %s open failed %d\n", __func__,
629 smd_ch_name[smd_pkt_devp->i], r);
630 goto release_pil;
631 }
632
633 r = wait_event_interruptible_timeout(
634 smd_pkt_devp->ch_opened_wait_queue,
635 smd_pkt_devp->is_open, (2 * HZ));
636 if (r == 0)
637 r = -ETIMEDOUT;
638
639 if (r < 0) {
640 pr_err("%s: wait failed for smd open: %d\n",
641 __func__, r);
642 } else if (!smd_pkt_devp->is_open) {
643 pr_err("%s: Invalid open notification\n", __func__);
644 r = -ENODEV;
645 } else {
646 smd_disable_read_intr(smd_pkt_devp->ch);
647 smd_pkt_devp->ch_size =
648 smd_write_avail(smd_pkt_devp->ch);
649 r = 0;
650 }
651 }
652release_pil:
653 if (peripheral && (r < 0))
654 pil_put(smd_pkt_devp->pil);
655out:
656 mutex_unlock(&smd_pkt_devp->ch_lock);
657
658 return r;
659}
660
661int smd_pkt_release(struct inode *inode, struct file *file)
662{
663 int r = 0;
664 struct smd_pkt_dev *smd_pkt_devp = file->private_data;
665
666 if (!smd_pkt_devp)
667 return -EINVAL;
668
669 clean_and_signal(smd_pkt_devp);
670
671 mutex_lock(&smd_pkt_devp->ch_lock);
672 if (smd_pkt_devp->ch != 0) {
673 r = smd_close(smd_pkt_devp->ch);
674 smd_pkt_devp->ch = 0;
675 smd_pkt_devp->blocking_write = 0;
676 if (smd_pkt_devp->pil)
677 pil_put(smd_pkt_devp->pil);
678 }
679 mutex_unlock(&smd_pkt_devp->ch_lock);
680
681 smd_pkt_devp->has_reset = 0;
682 smd_pkt_devp->do_reset_notification = 0;
683
684 return r;
685}
686
687static const struct file_operations smd_pkt_fops = {
688 .owner = THIS_MODULE,
689 .open = smd_pkt_open,
690 .release = smd_pkt_release,
691 .read = smd_pkt_read,
692 .write = smd_pkt_write,
693 .poll = smd_pkt_poll,
694 .unlocked_ioctl = smd_pkt_ioctl,
695};
696
697static int __init smd_pkt_init(void)
698{
699 int i;
700 int r;
701
702 r = alloc_chrdev_region(&smd_pkt_number,
703 0,
704 NUM_SMD_PKT_PORTS,
705 DEVICE_NAME);
706 if (IS_ERR_VALUE(r)) {
707 printk(KERN_ERR "ERROR:%s:%i:%s: "
708 "alloc_chrdev_region() ret %i.\n",
709 __FILE__,
710 __LINE__,
711 __func__,
712 r);
713 goto error0;
714 }
715
716 smd_pkt_classp = class_create(THIS_MODULE, DEVICE_NAME);
717 if (IS_ERR(smd_pkt_classp)) {
718 printk(KERN_ERR "ERROR:%s:%i:%s: "
719 "class_create() ENOMEM\n",
720 __FILE__,
721 __LINE__,
722 __func__);
723 r = -ENOMEM;
724 goto error1;
725 }
726
727 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
728 smd_pkt_devp[i] = kzalloc(sizeof(struct smd_pkt_dev),
729 GFP_KERNEL);
730 if (IS_ERR(smd_pkt_devp[i])) {
731 printk(KERN_ERR "ERROR:%s:%i:%s kmalloc() ENOMEM\n",
732 __FILE__,
733 __LINE__,
734 __func__);
735 r = -ENOMEM;
736 goto error2;
737 }
738
739 smd_pkt_devp[i]->i = i;
740
741 init_waitqueue_head(&smd_pkt_devp[i]->ch_read_wait_queue);
742 init_waitqueue_head(&smd_pkt_devp[i]->ch_write_wait_queue);
743 smd_pkt_devp[i]->is_open = 0;
744 init_waitqueue_head(&smd_pkt_devp[i]->ch_opened_wait_queue);
745
746 mutex_init(&smd_pkt_devp[i]->ch_lock);
747 mutex_init(&smd_pkt_devp[i]->rx_lock);
748 mutex_init(&smd_pkt_devp[i]->tx_lock);
749 init_completion(&smd_pkt_devp[i]->ch_allocated);
750
751 cdev_init(&smd_pkt_devp[i]->cdev, &smd_pkt_fops);
752 smd_pkt_devp[i]->cdev.owner = THIS_MODULE;
753
754 r = cdev_add(&smd_pkt_devp[i]->cdev,
755 (smd_pkt_number + i),
756 1);
757
758 if (IS_ERR_VALUE(r)) {
759 printk(KERN_ERR "%s:%i:%s: cdev_add() ret %i\n",
760 __FILE__,
761 __LINE__,
762 __func__,
763 r);
764 kfree(smd_pkt_devp[i]);
765 goto error2;
766 }
767
768 smd_pkt_devp[i]->devicep =
769 device_create(smd_pkt_classp,
770 NULL,
771 (smd_pkt_number + i),
772 NULL,
773 smd_pkt_dev_name[i]);
774
775 if (IS_ERR(smd_pkt_devp[i]->devicep)) {
776 printk(KERN_ERR "%s:%i:%s: "
777 "device_create() ENOMEM\n",
778 __FILE__,
779 __LINE__,
780 __func__);
781 r = -ENOMEM;
782 cdev_del(&smd_pkt_devp[i]->cdev);
783 kfree(smd_pkt_devp[i]);
784 goto error2;
785 }
786 if (device_create_file(smd_pkt_devp[i]->devicep,
787 &dev_attr_open_timeout))
788 pr_err("%s: unable to create device attr on #%d\n",
789 __func__, i);
790
791 smd_pkt_devp[i]->driver.probe = smd_pkt_dummy_probe;
792 smd_pkt_devp[i]->driver.driver.name = smd_ch_name[i];
793 smd_pkt_devp[i]->driver.driver.owner = THIS_MODULE;
794 r = platform_driver_register(&smd_pkt_devp[i]->driver);
795 if (r)
796 goto error2;
797 }
798
799 INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker);
800
801 D(KERN_INFO "SMD Packet Port Driver Initialized.\n");
802 return 0;
803
804 error2:
805 if (i > 0) {
806 while (--i >= 0) {
807 platform_driver_unregister(&smd_pkt_devp[i]->driver);
808 cdev_del(&smd_pkt_devp[i]->cdev);
809 kfree(smd_pkt_devp[i]);
810 device_destroy(smd_pkt_classp,
811 MKDEV(MAJOR(smd_pkt_number), i));
812 }
813 }
814
815 class_destroy(smd_pkt_classp);
816 error1:
817 unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
818 error0:
819 return r;
820}
821
822static void __exit smd_pkt_cleanup(void)
823{
824 int i;
825
826 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
827 platform_driver_unregister(&smd_pkt_devp[i]->driver);
828 cdev_del(&smd_pkt_devp[i]->cdev);
829 kfree(smd_pkt_devp[i]);
830 device_destroy(smd_pkt_classp,
831 MKDEV(MAJOR(smd_pkt_number), i));
832 }
833
834 class_destroy(smd_pkt_classp);
835
836 unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
837}
838
839module_init(smd_pkt_init);
840module_exit(smd_pkt_cleanup);
841
842MODULE_DESCRIPTION("MSM Shared Memory Packet Port");
843MODULE_LICENSE("GPL v2");