blob: 781d4fcbdd83851d0cca99da2cbae62a789c7ce0 [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;
66 int needed_space;
67 int is_open;
68 unsigned ch_size;
69 uint open_modem_wait;
70
71 int has_reset;
72 int do_reset_notification;
73 struct completion ch_allocated;
74
75} *smd_pkt_devp[NUM_SMD_PKT_PORTS];
76
77struct class *smd_pkt_classp;
78static dev_t smd_pkt_number;
79static struct delayed_work loopback_work;
80static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp);
81static void check_and_wakeup_writer(struct smd_pkt_dev *smd_pkt_devp);
82static uint32_t is_modem_smsm_inited(void);
83
84static int msm_smd_pkt_debug_mask;
85module_param_named(debug_mask, msm_smd_pkt_debug_mask,
86 int, S_IRUGO | S_IWUSR | S_IWGRP);
87#define DEBUG
88
89#ifdef DEBUG
90#define D_DUMP_BUFFER(prestr, cnt, buf) \
91do { \
92 if (msm_smd_pkt_debug_mask) \
93 print_hex_dump(KERN_DEBUG, prestr, \
94 DUMP_PREFIX_NONE, 16, 1, \
95 buf, cnt, 1); \
96} while (0)
97#else
98#define D_DUMP_BUFFER(prestr, cnt, buf) do {} while (0)
99#endif
100
101#ifdef DEBUG
102#define D(x...) if (msm_smd_pkt_debug_mask) printk(x)
103#else
104#define D(x...) do {} while (0)
105#endif
106
107static ssize_t open_timeout_store(struct device *d,
108 struct device_attribute *attr,
109 const char *buf,
110 size_t n)
111{
112 int i;
113 unsigned long tmp;
114 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
115 if (smd_pkt_devp[i]->devicep == d)
116 break;
117 }
118 if (!strict_strtoul(buf, 10, &tmp)) {
119 smd_pkt_devp[i]->open_modem_wait = tmp;
120 return n;
121 } else {
122 pr_err("%s: unable to convert: %s to an int\n", __func__,
123 buf);
124 return -EINVAL;
125 }
126}
127
128static ssize_t open_timeout_show(struct device *d,
129 struct device_attribute *attr,
130 char *buf)
131{
132 int i;
133 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
134 if (smd_pkt_devp[i]->devicep == d)
135 break;
136 }
137 return sprintf(buf, "%d\n", smd_pkt_devp[i]->open_modem_wait);
138}
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
156 wake_up_interruptible(&smd_pkt_devp->ch_read_wait_queue);
157 wake_up_interruptible(&smd_pkt_devp->ch_write_wait_queue);
158 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;
210 struct smd_pkt_dev *smd_pkt_devp;
211 struct smd_channel *chl;
212
213 D(KERN_ERR "%s: read %i bytes\n",
214 __func__, count);
215
216 smd_pkt_devp = file->private_data;
217
218 if (!smd_pkt_devp || !smd_pkt_devp->ch)
219 return -EINVAL;
220
221 if (smd_pkt_devp->do_reset_notification) {
222 /* notify client that a reset occurred */
223 return notify_reset(smd_pkt_devp);
224 }
225
226 chl = smd_pkt_devp->ch;
227wait_for_packet:
228 r = wait_event_interruptible(smd_pkt_devp->ch_read_wait_queue,
229 (smd_cur_packet_size(chl) > 0 &&
230 smd_read_avail(chl) >=
231 smd_cur_packet_size(chl)) ||
232 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);
255 bytes_read = smd_cur_packet_size(smd_pkt_devp->ch);
256
257 D(KERN_ERR "%s: after wait_event_interruptible bytes_read = %i\n",
258 __func__, bytes_read);
259
260 if (bytes_read == 0 ||
261 bytes_read < smd_read_avail(smd_pkt_devp->ch)) {
262 D(KERN_ERR "%s: Nothing to read\n", __func__);
263 mutex_unlock(&smd_pkt_devp->rx_lock);
264 goto wait_for_packet;
265 }
266
267 if (bytes_read > count) {
268 printk(KERN_ERR "packet size %i > buffer size %i, "
269 "dropping packet!", bytes_read, count);
270 smd_read(smd_pkt_devp->ch, 0, bytes_read);
271 mutex_unlock(&smd_pkt_devp->rx_lock);
272 return -EINVAL;
273 }
274
275 if (smd_read_user_buffer(smd_pkt_devp->ch, buf, bytes_read)
276 != bytes_read) {
277 mutex_unlock(&smd_pkt_devp->rx_lock);
278 if (smd_pkt_devp->has_reset)
279 return notify_reset(smd_pkt_devp);
280
281 printk(KERN_ERR "user read: not enough data?!\n");
282 return -EINVAL;
283 }
284 D_DUMP_BUFFER("read: ", bytes_read, buf);
285 mutex_unlock(&smd_pkt_devp->rx_lock);
286
287 D(KERN_ERR "%s: just read %i bytes\n",
288 __func__, bytes_read);
289
290 /* check and wakeup read threads waiting on this device */
291 check_and_wakeup_reader(smd_pkt_devp);
292
293 return bytes_read;
294}
295
296ssize_t smd_pkt_write(struct file *file,
297 const char __user *buf,
298 size_t count,
299 loff_t *ppos)
300{
301 int r = 0;
302 struct smd_pkt_dev *smd_pkt_devp;
303 DEFINE_WAIT(write_wait);
304
305 D(KERN_ERR "%s: writting %i bytes\n",
306 __func__, count);
307
308 smd_pkt_devp = file->private_data;
309
310 if (!smd_pkt_devp || !smd_pkt_devp->ch)
311 return -EINVAL;
312
313 if (count > smd_pkt_devp->ch_size)
314 return -EINVAL;
315
316 if (smd_pkt_devp->do_reset_notification) {
317 /* notify client that a reset occurred */
318 return notify_reset(smd_pkt_devp);
319 }
320
321 if (smd_pkt_devp->blocking_write) {
322 for (;;) {
323 mutex_lock(&smd_pkt_devp->tx_lock);
324 if (smd_pkt_devp->has_reset) {
325 smd_disable_read_intr(smd_pkt_devp->ch);
326 mutex_unlock(&smd_pkt_devp->tx_lock);
327 return notify_reset(smd_pkt_devp);
328 }
329 if (signal_pending(current)) {
330 smd_disable_read_intr(smd_pkt_devp->ch);
331 mutex_unlock(&smd_pkt_devp->tx_lock);
332 return -ERESTARTSYS;
333 }
334
335 prepare_to_wait(&smd_pkt_devp->ch_write_wait_queue,
336 &write_wait, TASK_INTERRUPTIBLE);
337 smd_enable_read_intr(smd_pkt_devp->ch);
338 if (smd_write_avail(smd_pkt_devp->ch) < count) {
339 if (!smd_pkt_devp->needed_space ||
340 count < smd_pkt_devp->needed_space)
341 smd_pkt_devp->needed_space = count;
342 mutex_unlock(&smd_pkt_devp->tx_lock);
343 schedule();
344 } else
345 break;
346 }
347 finish_wait(&smd_pkt_devp->ch_write_wait_queue, &write_wait);
348 smd_disable_read_intr(smd_pkt_devp->ch);
349 if (smd_pkt_devp->has_reset) {
350 mutex_unlock(&smd_pkt_devp->tx_lock);
351 return notify_reset(smd_pkt_devp);
352 }
353 if (signal_pending(current)) {
354 mutex_unlock(&smd_pkt_devp->tx_lock);
355 return -ERESTARTSYS;
356 }
357 } else {
358 if (smd_pkt_devp->has_reset)
359 return notify_reset(smd_pkt_devp);
360 if (signal_pending(current))
361 return -ERESTARTSYS;
362
363 mutex_lock(&smd_pkt_devp->tx_lock);
364 if (smd_write_avail(smd_pkt_devp->ch) < count) {
365 D(KERN_ERR "%s: Not enough space to write\n",
366 __func__);
367 mutex_unlock(&smd_pkt_devp->tx_lock);
368 return -ENOMEM;
369 }
370 }
371
372 D_DUMP_BUFFER("write: ", count, buf);
373
374 smd_pkt_devp->needed_space = 0;
375
376 r = smd_write_user_buffer(smd_pkt_devp->ch, buf, count);
377 if (r != count) {
378 mutex_unlock(&smd_pkt_devp->tx_lock);
379 if (smd_pkt_devp->has_reset)
380 return notify_reset(smd_pkt_devp);
381
382 printk(KERN_ERR "ERROR:%s:%i:%s: "
383 "smd_write(ch,buf,count = %i) ret %i.\n",
384 __FILE__,
385 __LINE__,
386 __func__,
387 count,
388 r);
389 return r;
390 }
391 mutex_unlock(&smd_pkt_devp->tx_lock);
392
393 D(KERN_ERR "%s: just wrote %i bytes\n",
394 __func__, count);
395
396 return count;
397}
398
399static unsigned int smd_pkt_poll(struct file *file, poll_table *wait)
400{
401 struct smd_pkt_dev *smd_pkt_devp;
402 unsigned int mask = 0;
403
404 smd_pkt_devp = file->private_data;
405 if (!smd_pkt_devp)
406 return POLLERR;
407
408 poll_wait(file, &smd_pkt_devp->ch_read_wait_queue, wait);
409 if (smd_read_avail(smd_pkt_devp->ch))
410 mask |= POLLIN | POLLRDNORM;
411
412 return mask;
413}
414
415static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp)
416{
417 int sz;
418
419 if (!smd_pkt_devp || !smd_pkt_devp->ch)
420 return;
421
422 sz = smd_cur_packet_size(smd_pkt_devp->ch);
423 if (sz == 0) {
424 D(KERN_ERR "%s: packet size is 0\n", __func__);
425 return;
426 }
427 if (sz > smd_read_avail(smd_pkt_devp->ch)) {
428 D(KERN_ERR "%s: packet size is %i - "
429 "the whole packet isn't here\n",
430 __func__, sz);
431 return;
432 }
433
434 /* here we have a packet of size sz ready */
435 wake_up_interruptible(&smd_pkt_devp->ch_read_wait_queue);
436 D(KERN_ERR "%s: after wake_up\n", __func__);
437}
438
439static void check_and_wakeup_writer(struct smd_pkt_dev *smd_pkt_devp)
440{
441 int sz;
442
443 if (!smd_pkt_devp || !smd_pkt_devp->ch)
444 return;
445
446 sz = smd_write_avail(smd_pkt_devp->ch);
447 if (sz >= smd_pkt_devp->needed_space) {
448 D(KERN_ERR "%s: %d bytes Write Space available\n",
449 __func__, sz);
450 smd_disable_read_intr(smd_pkt_devp->ch);
451 wake_up_interruptible(&smd_pkt_devp->ch_write_wait_queue);
452 }
453}
454
455static void ch_notify(void *priv, unsigned event)
456{
457 struct smd_pkt_dev *smd_pkt_devp = priv;
458
459 if (smd_pkt_devp->ch == 0)
460 return;
461
462 switch (event) {
463 case SMD_EVENT_DATA: {
464 D(KERN_ERR "%s: data\n", __func__);
465 check_and_wakeup_reader(smd_pkt_devp);
466 if (smd_pkt_devp->blocking_write)
467 check_and_wakeup_writer(smd_pkt_devp);
468 D(KERN_ERR "%s: data after check_and_wakeup\n", __func__);
469 break;
470 }
471 case SMD_EVENT_OPEN:
472 D(KERN_ERR "%s: smd opened\n",
473 __func__);
474
475 smd_pkt_devp->has_reset = 0;
476 smd_pkt_devp->is_open = 1;
477 wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue);
478 break;
479 case SMD_EVENT_CLOSE:
480 smd_pkt_devp->is_open = 0;
481 printk(KERN_ERR "%s: smd closed\n",
482 __func__);
483
484 /* put port into reset state */
485 clean_and_signal(smd_pkt_devp);
486 if (smd_pkt_devp->i == LOOPBACK_INX)
487 schedule_delayed_work(&loopback_work,
488 msecs_to_jiffies(1000));
489 break;
490 }
491}
492
493#ifdef CONFIG_ARCH_FSM9XXX
494static char *smd_pkt_dev_name[] = {
495 "smdcntl1",
496 "smdcntl2",
497 "smd22",
498 "smd_pkt_loopback",
499};
500
501static char *smd_ch_name[] = {
502 "DATA6_CNTL",
503 "DATA7_CNTL",
504 "DATA22",
505 "LOOPBACK",
506};
507
508static uint32_t smd_ch_edge[] = {
509 SMD_APPS_QDSP,
510 SMD_APPS_QDSP,
511 SMD_APPS_QDSP,
512 SMD_APPS_QDSP
513};
514#else
515static char *smd_pkt_dev_name[] = {
516 "smdcntl0",
517 "smdcntl1",
518 "smdcntl2",
519 "smdcntl3",
520 "smdcntl4",
521 "smdcntl5",
522 "smdcntl6",
523 "smdcntl7",
524 "smd22",
525 "smd_sns_dsps",
526 "apr_apps_user",
527 "smd_pkt_loopback",
528};
529
530static char *smd_ch_name[] = {
531 "DATA5_CNTL",
532 "DATA6_CNTL",
533 "DATA7_CNTL",
534 "DATA8_CNTL",
535 "DATA9_CNTL",
536 "DATA12_CNTL",
537 "DATA13_CNTL",
538 "DATA14_CNTL",
539 "DATA22",
540 "SENSOR",
541 "apr_apps_user",
542 "LOOPBACK",
543};
544
545static uint32_t smd_ch_edge[] = {
546 SMD_APPS_MODEM,
547 SMD_APPS_MODEM,
548 SMD_APPS_MODEM,
549 SMD_APPS_MODEM,
550 SMD_APPS_MODEM,
551 SMD_APPS_MODEM,
552 SMD_APPS_MODEM,
553 SMD_APPS_MODEM,
554 SMD_APPS_MODEM,
555 SMD_APPS_DSPS,
556 SMD_APPS_QDSP,
557 SMD_APPS_MODEM,
558};
559#endif
560
561static int smd_pkt_dummy_probe(struct platform_device *pdev)
562{
563 int i;
564
565 for (i = 0; i < NUM_SMD_PKT_PORTS; i++) {
566 if (!strcmp(pdev->name, smd_ch_name[i])) {
567 complete_all(&smd_pkt_devp[i]->ch_allocated);
568 break;
569 }
570 }
571 return 0;
572}
573
574static uint32_t is_modem_smsm_inited(void)
575{
576 uint32_t modem_state;
577 uint32_t ready_state = (SMSM_INIT | SMSM_SMDINIT);
578
579 modem_state = smsm_get_state(SMSM_MODEM_STATE);
580 return (modem_state & ready_state) == ready_state;
581}
582
583int smd_pkt_open(struct inode *inode, struct file *file)
584{
585 int r = 0;
586 struct smd_pkt_dev *smd_pkt_devp;
587 char *peripheral = NULL;
588
589 smd_pkt_devp = container_of(inode->i_cdev, struct smd_pkt_dev, cdev);
590
591 if (!smd_pkt_devp)
592 return -EINVAL;
593
594 file->private_data = smd_pkt_devp;
595
596 mutex_lock(&smd_pkt_devp->ch_lock);
597 if (smd_pkt_devp->ch == 0) {
598
599 if (smd_ch_edge[smd_pkt_devp->i] == SMD_APPS_MODEM)
600 peripheral = "modem";
601 else if (smd_ch_edge[smd_pkt_devp->i] == SMD_APPS_QDSP)
602 peripheral = "q6";
603
604 if (peripheral) {
605 smd_pkt_devp->pil = pil_get(peripheral);
606 if (IS_ERR(smd_pkt_devp->pil)) {
607 r = PTR_ERR(smd_pkt_devp->pil);
608 goto out;
609 }
610
611 /* Wait for the modem SMSM to be inited for the SMD
612 ** Loopback channel to be allocated at the modem. Since
613 ** the wait need to be done atmost once, using msleep
614 ** doesn't degrade the performance. */
615 if (!strcmp(smd_ch_name[smd_pkt_devp->i], "LOOPBACK")) {
616 if (!is_modem_smsm_inited())
617 msleep(5000);
618 smsm_change_state(SMSM_APPS_STATE,
619 0, SMSM_SMD_LOOPBACK);
620 msleep(100);
621 }
622
623 /*
624 * Wait for a packet channel to be allocated so we know
625 * the modem is ready enough.
626 */
627 if (smd_pkt_devp->open_modem_wait) {
628 r = wait_for_completion_interruptible_timeout(
629 &smd_pkt_devp->ch_allocated,
630 msecs_to_jiffies(
631 smd_pkt_devp->open_modem_wait
632 * 1000));
633 if (r == 0)
634 r = -ETIMEDOUT;
635 if (r < 0) {
636 pr_err("%s: wait failed for smd port:"
637 " %d\n", __func__, r);
638 goto release_pil;
639 }
640 }
641 }
642
643 r = smd_named_open_on_edge(smd_ch_name[smd_pkt_devp->i],
644 smd_ch_edge[smd_pkt_devp->i],
645 &smd_pkt_devp->ch,
646 smd_pkt_devp,
647 ch_notify);
648 if (r < 0) {
649 pr_err("%s: %s open failed %d\n", __func__,
650 smd_ch_name[smd_pkt_devp->i], r);
651 goto release_pil;
652 }
653
654 r = wait_event_interruptible_timeout(
655 smd_pkt_devp->ch_opened_wait_queue,
656 smd_pkt_devp->is_open, (2 * HZ));
657 if (r == 0)
658 r = -ETIMEDOUT;
659
660 if (r < 0) {
661 pr_err("%s: wait failed for smd open: %d\n",
662 __func__, r);
663 } else if (!smd_pkt_devp->is_open) {
664 pr_err("%s: Invalid open notification\n", __func__);
665 r = -ENODEV;
666 } else {
667 smd_disable_read_intr(smd_pkt_devp->ch);
668 smd_pkt_devp->ch_size =
669 smd_write_avail(smd_pkt_devp->ch);
670 r = 0;
671 }
672 }
673release_pil:
674 if (peripheral && (r < 0))
675 pil_put(smd_pkt_devp->pil);
676out:
677 mutex_unlock(&smd_pkt_devp->ch_lock);
678
679 return r;
680}
681
682int smd_pkt_release(struct inode *inode, struct file *file)
683{
684 int r = 0;
685 struct smd_pkt_dev *smd_pkt_devp = file->private_data;
686
687 if (!smd_pkt_devp)
688 return -EINVAL;
689
690 clean_and_signal(smd_pkt_devp);
691
692 mutex_lock(&smd_pkt_devp->ch_lock);
693 if (smd_pkt_devp->ch != 0) {
694 r = smd_close(smd_pkt_devp->ch);
695 smd_pkt_devp->ch = 0;
696 smd_pkt_devp->blocking_write = 0;
697 if (smd_pkt_devp->pil)
698 pil_put(smd_pkt_devp->pil);
699 }
700 mutex_unlock(&smd_pkt_devp->ch_lock);
701
702 smd_pkt_devp->has_reset = 0;
703 smd_pkt_devp->do_reset_notification = 0;
704
705 return r;
706}
707
708static const struct file_operations smd_pkt_fops = {
709 .owner = THIS_MODULE,
710 .open = smd_pkt_open,
711 .release = smd_pkt_release,
712 .read = smd_pkt_read,
713 .write = smd_pkt_write,
714 .poll = smd_pkt_poll,
715 .unlocked_ioctl = smd_pkt_ioctl,
716};
717
718static int __init smd_pkt_init(void)
719{
720 int i;
721 int r;
722
723 r = alloc_chrdev_region(&smd_pkt_number,
724 0,
725 NUM_SMD_PKT_PORTS,
726 DEVICE_NAME);
727 if (IS_ERR_VALUE(r)) {
728 printk(KERN_ERR "ERROR:%s:%i:%s: "
729 "alloc_chrdev_region() ret %i.\n",
730 __FILE__,
731 __LINE__,
732 __func__,
733 r);
734 goto error0;
735 }
736
737 smd_pkt_classp = class_create(THIS_MODULE, DEVICE_NAME);
738 if (IS_ERR(smd_pkt_classp)) {
739 printk(KERN_ERR "ERROR:%s:%i:%s: "
740 "class_create() ENOMEM\n",
741 __FILE__,
742 __LINE__,
743 __func__);
744 r = -ENOMEM;
745 goto error1;
746 }
747
748 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
749 smd_pkt_devp[i] = kzalloc(sizeof(struct smd_pkt_dev),
750 GFP_KERNEL);
751 if (IS_ERR(smd_pkt_devp[i])) {
752 printk(KERN_ERR "ERROR:%s:%i:%s kmalloc() ENOMEM\n",
753 __FILE__,
754 __LINE__,
755 __func__);
756 r = -ENOMEM;
757 goto error2;
758 }
759
760 smd_pkt_devp[i]->i = i;
761
762 init_waitqueue_head(&smd_pkt_devp[i]->ch_read_wait_queue);
763 init_waitqueue_head(&smd_pkt_devp[i]->ch_write_wait_queue);
764 smd_pkt_devp[i]->is_open = 0;
765 init_waitqueue_head(&smd_pkt_devp[i]->ch_opened_wait_queue);
766
767 mutex_init(&smd_pkt_devp[i]->ch_lock);
768 mutex_init(&smd_pkt_devp[i]->rx_lock);
769 mutex_init(&smd_pkt_devp[i]->tx_lock);
770 init_completion(&smd_pkt_devp[i]->ch_allocated);
771
772 cdev_init(&smd_pkt_devp[i]->cdev, &smd_pkt_fops);
773 smd_pkt_devp[i]->cdev.owner = THIS_MODULE;
774
775 r = cdev_add(&smd_pkt_devp[i]->cdev,
776 (smd_pkt_number + i),
777 1);
778
779 if (IS_ERR_VALUE(r)) {
780 printk(KERN_ERR "%s:%i:%s: cdev_add() ret %i\n",
781 __FILE__,
782 __LINE__,
783 __func__,
784 r);
785 kfree(smd_pkt_devp[i]);
786 goto error2;
787 }
788
789 smd_pkt_devp[i]->devicep =
790 device_create(smd_pkt_classp,
791 NULL,
792 (smd_pkt_number + i),
793 NULL,
794 smd_pkt_dev_name[i]);
795
796 if (IS_ERR(smd_pkt_devp[i]->devicep)) {
797 printk(KERN_ERR "%s:%i:%s: "
798 "device_create() ENOMEM\n",
799 __FILE__,
800 __LINE__,
801 __func__);
802 r = -ENOMEM;
803 cdev_del(&smd_pkt_devp[i]->cdev);
804 kfree(smd_pkt_devp[i]);
805 goto error2;
806 }
807 if (device_create_file(smd_pkt_devp[i]->devicep,
808 &dev_attr_open_timeout))
809 pr_err("%s: unable to create device attr on #%d\n",
810 __func__, i);
811
812 smd_pkt_devp[i]->driver.probe = smd_pkt_dummy_probe;
813 smd_pkt_devp[i]->driver.driver.name = smd_ch_name[i];
814 smd_pkt_devp[i]->driver.driver.owner = THIS_MODULE;
815 r = platform_driver_register(&smd_pkt_devp[i]->driver);
816 if (r)
817 goto error2;
818 }
819
820 INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker);
821
822 D(KERN_INFO "SMD Packet Port Driver Initialized.\n");
823 return 0;
824
825 error2:
826 if (i > 0) {
827 while (--i >= 0) {
828 platform_driver_unregister(&smd_pkt_devp[i]->driver);
829 cdev_del(&smd_pkt_devp[i]->cdev);
830 kfree(smd_pkt_devp[i]);
831 device_destroy(smd_pkt_classp,
832 MKDEV(MAJOR(smd_pkt_number), i));
833 }
834 }
835
836 class_destroy(smd_pkt_classp);
837 error1:
838 unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
839 error0:
840 return r;
841}
842
843static void __exit smd_pkt_cleanup(void)
844{
845 int i;
846
847 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
848 platform_driver_unregister(&smd_pkt_devp[i]->driver);
849 cdev_del(&smd_pkt_devp[i]->cdev);
850 kfree(smd_pkt_devp[i]);
851 device_destroy(smd_pkt_classp,
852 MKDEV(MAJOR(smd_pkt_number), i));
853 }
854
855 class_destroy(smd_pkt_classp);
856
857 unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
858}
859
860module_init(smd_pkt_init);
861module_exit(smd_pkt_cleanup);
862
863MODULE_DESCRIPTION("MSM Shared Memory Packet Port");
864MODULE_LICENSE("GPL v2");