Initial commit
[kernel/linux-3.0.git] / drivers / misc / modem_if_na / modem_io_device.c
1 /* /linux/drivers/misc/modem_if/modem_io_device.c
2  *
3  * Copyright (C) 2010 Google, Inc.
4  * Copyright (C) 2010 Samsung Electronics.
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include <linux/init.h>
18 #include <linux/sched.h>
19 #include <linux/fs.h>
20 #include <linux/poll.h>
21 #include <linux/irq.h>
22 #include <linux/gpio.h>
23 #include <linux/if_arp.h>
24 #include <linux/ip.h>
25 #include <linux/if_ether.h>
26 #include <linux/etherdevice.h>
27 #include <linux/ratelimit.h>
28
29 #include <linux/platform_data/modem_na.h>
30 #include "modem_prj.h"
31
32
33 #define HDLC_START      0x7F
34 #define HDLC_END        0x7E
35 #define SIZE_OF_HDLC_START      1
36 #define SIZE_OF_HDLC_END        1
37 #define MAX_RXDATA_SIZE         (4096 - 512)
38
39 static const char hdlc_start[1] = { HDLC_START };
40 static const char hdlc_end[1] = { HDLC_END };
41
42 struct fmt_hdr {
43         u16 len;
44         u8 control;
45 } __packed;
46
47 struct raw_hdr {
48         u32 len;
49         u8 channel;
50         u8 control;
51 } __packed;
52
53 struct rfs_hdr {
54         u32 len;
55         u8 cmd;
56         u8 id;
57 } __packed;
58
59 static const char const *modem_state_name[] = {
60         [STATE_OFFLINE]         = "OFFLINE",
61         [STATE_CRASH_EXIT]      = "CRASH_EXIT",
62         [STATE_BOOTING]         = "BOOTING",
63         [STATE_ONLINE]          = "ONLINE",
64         [STATE_LOADER_DONE]     = "LOADER_DONE",
65         [STATE_NV_REBUILDING]   = "NV_REBUILDING",
66 };
67
68 static int rx_iodev_skb(struct io_device *iod);
69
70 static int get_header_size(struct io_device *iod)
71 {
72         switch (iod->format) {
73         case IPC_FMT:
74                 return sizeof(struct fmt_hdr);
75
76         case IPC_RAW:
77         case IPC_MULTI_RAW:
78                 return sizeof(struct raw_hdr);
79
80         case IPC_RFS:
81                 return sizeof(struct rfs_hdr);
82
83         case IPC_BOOT:
84                 /* minimum size for transaction align */
85                 return 4;
86
87         case IPC_RAMDUMP:
88         default:
89                 return 0;
90         }
91 }
92
93 static int get_hdlc_size(struct io_device *iod, char *buf)
94 {
95         struct fmt_hdr *fmt_header;
96         struct raw_hdr *raw_header;
97         struct rfs_hdr *rfs_header;
98
99         pr_debug("[MODEM_IF] buf : %02x %02x %02x (%d)\n", *buf, *(buf + 1),
100                                 *(buf + 2), __LINE__);
101
102         switch (iod->format) {
103         case IPC_FMT:
104                 fmt_header = (struct fmt_hdr *)buf;
105                 return fmt_header->len;
106         case IPC_RAW:
107         case IPC_MULTI_RAW:
108                 raw_header = (struct raw_hdr *)buf;
109                 return raw_header->len;
110         case IPC_RFS:
111                 rfs_header = (struct rfs_hdr *)buf;
112                 return rfs_header->len;
113         default:
114                 break;
115         }
116         return 0;
117 }
118
119 static void *get_header(struct io_device *iod, size_t count,
120                         char *frame_header_buf)
121 {
122         struct fmt_hdr *fmt_h;
123         struct raw_hdr *raw_h;
124         struct rfs_hdr *rfs_h;
125
126         switch (iod->format) {
127         case IPC_FMT:
128                 fmt_h = (struct fmt_hdr *)frame_header_buf;
129
130                 fmt_h->len = count + sizeof(struct fmt_hdr);
131                 fmt_h->control = 0;
132
133                 return (void *)frame_header_buf;
134
135         case IPC_RAW:
136         case IPC_MULTI_RAW:
137                 raw_h = (struct raw_hdr *)frame_header_buf;
138
139                 raw_h->len = count + sizeof(struct raw_hdr);
140                 raw_h->channel = iod->id & 0x1F;
141                 raw_h->control = 0;
142
143                 return (void *)frame_header_buf;
144
145         case IPC_RFS:
146                 rfs_h = (struct rfs_hdr *)frame_header_buf;
147
148                 rfs_h->len = count + sizeof(struct raw_hdr);
149                 rfs_h->id = iod->id;
150
151                 return (void *)frame_header_buf;
152
153         default:
154                 return 0;
155         }
156 }
157
158 static inline int rx_hdlc_head_start_check(char *buf)
159 {
160         /* check hdlc head and return size of start byte */
161         return (buf[0] == HDLC_START) ? SIZE_OF_HDLC_START : -EBADMSG;
162 }
163
164 static inline int rx_hdlc_tail_check(char *buf)
165 {
166         /* check hdlc tail and return size of tail byte */
167         return (buf[0] == HDLC_END) ? SIZE_OF_HDLC_END : -EBADMSG;
168 }
169
170 /* remove hdlc header and store IPC header */
171 static int rx_hdlc_head_check(struct io_device *iod, char *buf, unsigned rest)
172 {
173         struct header_data *hdr = &iod->h_data;
174         int head_size = get_header_size(iod);
175         int done_len = 0;
176         int len = 0;
177         struct modem_data *md = (struct modem_data *)\
178                                         iod->mc->dev->platform_data;
179
180         /* first frame, remove start header 7F */
181         if (!hdr->start) {
182                 len = rx_hdlc_head_start_check(buf);
183                 if (len < 0) {
184                         pr_err("[MODEM_IF] Wrong HDLC start: 0x%x(%s)\n",
185                                                 *buf, iod->name);
186                         return len; /*Wrong hdlc start*/
187                 }
188
189                 pr_debug("[MODEM_IF] check len : %d, rest : %d (%d)\n", len,
190                                         rest, __LINE__);
191
192                 /* set the start flag of current packet */
193                 hdr->start = HDLC_START;
194                 hdr->len = 0;
195
196                 buf += len;
197                 done_len += len;
198                 rest -= len; /* rest, call by value */
199         }
200
201         pr_debug("[MODEM_IF] check len : %d, rest : %d (%d)\n", len, rest,
202                                 __LINE__);
203
204         /* store the IPC header to iod priv */
205         if (hdr->len < head_size) {
206                 len = min(rest, head_size - hdr->len);
207                 memcpy(hdr->hdr + hdr->len, buf, len);
208
209                 /* Skip the dummy byte inserted for 2-byte alignment in header.
210                 RAW format header size is 6 bytes. Start + 6 + 1 (skip byte) */
211                 if (md->align == 1) {
212                         if ((iod->format == IPC_RAW
213                                 || iod->format == IPC_MULTI_RAW)
214                                 && (iod->net_typ == CDMA_NETWORK)
215                                 && !(len & 0x01))
216                                 len++;
217                 }
218                 hdr->len += len;
219                 done_len += len;
220         }
221
222         pr_debug("[MODEM_IF] check done_len : %d, rest : %d (%d)\n", done_len,
223                                 rest, __LINE__);
224         return done_len;
225 }
226
227 /* alloc skb and copy dat to skb */
228 static int rx_hdlc_data_check(struct io_device *iod, char *buf, unsigned rest)
229 {
230         struct header_data *hdr = &iod->h_data;
231         struct sk_buff *skb = iod->skb_recv;
232         int head_size = get_header_size(iod);
233         int data_size = get_hdlc_size(iod, hdr->hdr) - head_size;
234         int alloc_size = min(data_size, MAX_RXDATA_SIZE);
235         int len;
236         int done_len = 0;
237         int rest_len = data_size - hdr->flag_len;
238
239         /* first payload data - alloc skb */
240         if (!skb) {
241                 switch (iod->format) {
242                 case IPC_RFS:
243                         alloc_size = min(data_size + head_size, \
244                                                         MAX_RXDATA_SIZE);
245                         skb = alloc_skb(alloc_size, GFP_ATOMIC);
246                         if (unlikely(!skb))
247                                 return -ENOMEM;
248                         /* copy the RFS haeder to skb->data */
249                         memcpy(skb_put(skb, head_size), hdr->hdr, head_size);
250                         break;
251
252                 case IPC_MULTI_RAW:
253                         if (data_size > MAX_RXDATA_SIZE) { \
254                                 pr_err("%s: %s: packet size too large (%d)\n",\
255                                         __func__, iod->name, data_size);
256                                 return -EINVAL;
257                         }
258
259                         if (iod->net_typ == UMTS_NETWORK)
260                                 skb = alloc_skb(alloc_size, GFP_ATOMIC);
261                         else
262                                 skb = alloc_skb(alloc_size +
263                                         sizeof(struct ethhdr), GFP_ATOMIC);
264                         if (unlikely(!skb))
265                                 return -ENOMEM;
266
267                         if (iod->net_typ != UMTS_NETWORK)
268                                 skb_reserve(skb, sizeof(struct ethhdr));
269                         break;
270
271                 default:
272                         skb = alloc_skb(alloc_size, GFP_ATOMIC);
273                         if (unlikely(!skb))
274                                 return -ENOMEM;
275                         break;
276                 }
277                 iod->skb_recv = skb;
278         }
279
280         while (rest > 0) {
281                 len = min(rest,  alloc_size - skb->len);
282                 len = min(len, rest_len);
283                 memcpy(skb_put(skb, len), buf, len);
284                 buf += len;
285                 done_len += len;
286                 hdr->flag_len += len;
287                 rest -= len;
288                 rest_len -= len;
289
290                 if (!rest_len || !rest)
291                         break;
292
293                 rx_iodev_skb(iod);
294                 iod->skb_recv =  NULL;
295
296                 alloc_size = min(rest_len, MAX_RXDATA_SIZE);
297                 skb = alloc_skb(alloc_size, GFP_ATOMIC);
298                 if (unlikely(!skb))
299                         return -ENOMEM;
300                 iod->skb_recv = skb;
301         }
302
303         return done_len;
304 }
305
306 static int rx_iodev_skb_raw(struct io_device *iod)
307 {
308         int err;
309         struct sk_buff *skb = iod->skb_recv;
310         struct net_device *ndev;
311         struct iphdr *ip_header;
312         struct ethhdr *ehdr;
313         const char source[ETH_ALEN] = SOURCE_MAC_ADDR;
314
315         switch (iod->io_typ) {
316         case IODEV_MISC:
317                 skb_queue_tail(&iod->sk_rx_q, iod->skb_recv);
318                 wake_up(&iod->wq);
319                 return 0;
320
321         case IODEV_NET:
322                 ndev = iod->ndev;
323                 if (!ndev)
324                         return NET_RX_DROP;
325
326                 skb->dev = ndev;
327                 ndev->stats.rx_packets++;
328                 ndev->stats.rx_bytes += skb->len;
329
330                 /* check the version of IP */
331                 ip_header = (struct iphdr *)skb->data;
332                 if (ip_header->version == IP6VERSION)
333                         skb->protocol = htons(ETH_P_IPV6);
334                 else
335                         skb->protocol = htons(ETH_P_IP);
336
337                 if (iod->net_typ == UMTS_NETWORK) {
338                         skb_reset_mac_header(skb);
339                 } else {
340                         ehdr = (void *)skb_push(skb, sizeof(struct ethhdr));
341                         memcpy(ehdr->h_dest, ndev->dev_addr, ETH_ALEN);
342                         memcpy(ehdr->h_source, source, ETH_ALEN);
343                         ehdr->h_proto = skb->protocol;
344                         skb->ip_summed = CHECKSUM_NONE;
345                         skb_reset_mac_header(skb);
346
347                         skb_pull(skb, sizeof(struct ethhdr));
348                 }
349
350                 err = netif_rx_ni(skb);
351                 if (err != NET_RX_SUCCESS)
352                         dev_err(&ndev->dev, "rx error: %d\n", err);
353                 return err;
354
355         default:
356                 pr_err("[MODEM_IF] wrong io_type : %d\n", iod->io_typ);
357                 return -EINVAL;
358         }
359 }
360
361 static void rx_iodev_work(struct work_struct *work)
362 {
363         int ret;
364         struct sk_buff *skb;
365         struct io_device *real_iod;
366         struct io_device *iod = container_of(work, struct io_device,
367                                 rx_work.work);
368
369         skb = skb_dequeue(&iod->sk_rx_q);
370         while (skb) {
371                 real_iod = *((struct io_device **)skb->cb);
372                 real_iod->skb_recv = skb;
373
374                 ret = rx_iodev_skb_raw(real_iod);
375                 if (ret == NET_RX_DROP) {
376                         pr_err("[MODEM_IF] %s: queue delayed work!\n",
377                                                                 __func__);
378                         skb_queue_head(&iod->sk_rx_q, skb);
379                         schedule_delayed_work(&iod->rx_work,
380                                                 msecs_to_jiffies(20));
381                         break;
382                 } else if (ret < 0)
383                         dev_kfree_skb_any(skb);
384
385                 skb = skb_dequeue(&iod->sk_rx_q);
386         }
387 }
388
389
390 static int rx_multipdp(struct io_device *iod)
391 {
392         u8 ch;
393         struct raw_hdr *raw_header = (struct raw_hdr *)&iod->h_data.hdr;
394         struct io_raw_devices *io_raw_devs =
395                                 (struct io_raw_devices *)iod->private_data;
396         struct io_device *real_iod;
397
398         ch = raw_header->channel;
399         real_iod = io_raw_devs->raw_devices[ch];
400         if (!real_iod) {
401                 pr_err("[MODEM_IF] %s: wrong channel %d\n", __func__, ch);
402                 return -1;
403         }
404
405         *((struct io_device **)iod->skb_recv->cb) = real_iod;
406         skb_queue_tail(&iod->sk_rx_q, iod->skb_recv);
407         pr_debug("sk_rx_qlen:%d\n", iod->sk_rx_q.qlen);
408
409         schedule_delayed_work(&iod->rx_work, 0);
410         return 0;
411 }
412
413 /* de-mux function draft */
414 static int rx_iodev_skb(struct io_device *iod)
415 {
416         switch (iod->format) {
417         case IPC_MULTI_RAW:
418                 return rx_multipdp(iod);
419
420         case IPC_FMT:
421         case IPC_RFS:
422         default:
423                 skb_queue_tail(&iod->sk_rx_q, iod->skb_recv);
424
425                 pr_debug("[MODEM_IF] wake up fmt,rfs skb\n");
426                 wake_up(&iod->wq);
427                 return 0;
428         }
429 }
430
431 static int rx_hdlc_packet(struct io_device *iod, const char *data,
432                 unsigned recv_size)
433 {
434         unsigned rest = recv_size;
435         char *buf = (char *)data;
436         int err = 0;
437         int len;
438         struct modem_data *md = (struct modem_data *)\
439                                         iod->mc->dev->platform_data;
440
441
442         if (rest <= 0)
443                 goto exit;
444
445         pr_debug("[MODEM_IF] RX_SIZE=%d\n", rest);
446
447         if (iod->h_data.flag_len)
448                 goto data_check;
449
450 next_frame:
451         err = len = rx_hdlc_head_check(iod, buf, rest);
452         if (err < 0)
453                 goto exit; /* buf++; rest--; goto next_frame; */
454         pr_debug("[MODEM_IF] check len : %d, rest : %d (%d)\n", len, rest,
455                                 __LINE__);
456
457         buf += len;
458         rest -= len;
459         if (rest <= 0)
460                 goto exit;
461
462 data_check:
463         err = len = rx_hdlc_data_check(iod, buf, rest);
464         if (err < 0)
465                 goto exit;
466         pr_debug("[MODEM_IF] check len : %d, rest : %d (%d)\n", len, rest,
467                                 __LINE__);
468
469         /* If the lenght of actual data is odd. Skip the dummy bit*/
470         if (md->align == 1) {
471                 if ((iod->format == IPC_RAW || iod->format == IPC_MULTI_RAW)
472                         && (iod->net_typ == CDMA_NETWORK) && (len & 0x01))
473                         len++;
474         }
475         buf += len;
476         rest -= len;
477
478         if (!rest && iod->h_data.flag_len)
479                 return 0;
480         else if (rest <= 0)
481                 goto exit;
482
483         err = len = rx_hdlc_tail_check(buf);
484         if (err < 0) {
485                 pr_err("[MODEM_IF] Wrong HDLC end: 0x%x(%s)\n",
486                                         *buf, iod->name);
487                 goto exit;
488         }
489         pr_debug("[MODEM_IF] check len : %d, rest : %d (%d)\n", len, rest,
490                                 __LINE__);
491
492         /* Skip the dummy byte inserted for 2-byte alignment in header.
493            Ox7E 00.*/
494         if (md->align == 1) {
495                 if ((iod->format == IPC_RAW || iod->format == IPC_MULTI_RAW)
496                         && (iod->net_typ == CDMA_NETWORK))
497                         len++;
498         }
499         buf += len;
500         rest -= len;
501         if (rest < 0)
502                 goto exit;
503
504         err = rx_iodev_skb(iod);
505         if (err < 0)
506                 goto exit;
507
508         /* initialize header & skb */
509         iod->skb_recv = NULL;
510         memset(&iod->h_data, 0x00, sizeof(struct header_data));
511
512         if (rest)
513                 goto next_frame;
514
515 exit:
516         /* free buffers. mipi-hsi re-use recv buf */
517         if (rest < 0)
518                 err = -ERANGE;
519
520         if (err < 0) {
521                 /* clear headers */
522                 memset(&iod->h_data, 0x00, sizeof(struct header_data));
523
524                 if (iod->skb_recv) {
525                         dev_kfree_skb_any(iod->skb_recv);
526                         iod->skb_recv = NULL;
527                 }
528         }
529
530         return err;
531 }
532
533 /* called from link device when a packet arrives for this io device */
534 static int io_dev_recv_data_from_link_dev(struct io_device *iod,
535                         const char *data, unsigned int len)
536 {
537         struct sk_buff *skb;
538         int err;
539
540         switch (iod->format) {
541         case IPC_FMT:
542         case IPC_RAW:
543         case IPC_RFS:
544         case IPC_MULTI_RAW:
545                 err = rx_hdlc_packet(iod, data, len);
546                 if (err < 0)
547                         pr_err("[MODEM_IF] fail process hdlc fram\n");
548                 return err;
549
550         case IPC_CMD:
551                 /* TODO- handle flow control command from CP */
552                 return 0;
553
554         case IPC_BOOT:
555         case IPC_RAMDUMP:
556                 /* save packet to sk_buff */
557                 skb = alloc_skb(len, GFP_ATOMIC);
558                 if (!skb) {
559                         pr_err("[MODEM_IF] fail alloc skb (%d)\n", __LINE__);
560                         return -ENOMEM;
561                 }
562
563                 pr_debug("[MODEM_IF] boot len : %d\n", len);
564
565                 memcpy(skb_put(skb, len), data, len);
566                 skb_queue_tail(&iod->sk_rx_q, skb);
567                 pr_debug("[MODEM_IF] skb len : %d\n", skb->len);
568
569                 wake_up(&iod->wq);
570                 return len;
571
572         default:
573                 return -EINVAL;
574         }
575 }
576
577 /* inform the IO device that the modem is now online or offline or
578  * crashing or whatever...
579  */
580 static void io_dev_modem_state_changed(struct io_device *iod,
581                         enum modem_state state)
582 {
583         iod->mc->phone_state = state;
584         pr_info("[MODEM_IF] %s state changed: %s\n", \
585                                 iod->name, modem_state_name[state]);
586
587         if ((state == STATE_CRASH_EXIT) || (state == STATE_NV_REBUILDING))
588                 wake_up(&iod->wq);
589 }
590
591 static int misc_open(struct inode *inode, struct file *filp)
592 {
593         struct io_device *iod = to_io_device(filp->private_data);
594         filp->private_data = (void *)iod;
595
596         if (iod->format != IPC_BOOT && iod->format != IPC_RAMDUMP)
597                 pr_info("[MODEM_IF] misc_open : %s\n", iod->name);
598
599         if (iod->link->init_comm)
600                 return iod->link->init_comm(iod->link, iod);
601         return 0;
602 }
603
604 static int misc_release(struct inode *inode, struct file *filp)
605 {
606         struct io_device *iod = (struct io_device *)filp->private_data;
607
608         if (iod->format != IPC_BOOT && iod->format != IPC_RAMDUMP)
609                 pr_info("[MODEM_IF] misc_release : %s\n", iod->name);
610
611         if (iod->link->terminate_comm)
612                 iod->link->terminate_comm(iod->link, iod);
613
614         skb_queue_purge(&iod->sk_rx_q);
615         return 0;
616 }
617
618 static unsigned int misc_poll(struct file *filp,
619                         struct poll_table_struct *wait)
620 {
621         struct io_device *iod = (struct io_device *)filp->private_data;
622
623         poll_wait(filp, &iod->wq, wait);
624
625         if ((!skb_queue_empty(&iod->sk_rx_q))
626                                 && (iod->mc->phone_state != STATE_OFFLINE))
627                 return POLLIN | POLLRDNORM;
628         else if ((iod->mc->phone_state == STATE_CRASH_EXIT) ||
629                 (iod->mc->phone_state == STATE_NV_REBUILDING))
630                 return POLLHUP;
631         else
632                 return 0;
633 }
634
635 static long misc_ioctl(struct file *filp, unsigned int cmd, unsigned long _arg)
636 {
637         int p_state;
638         struct io_device *iod = (struct io_device *)filp->private_data;
639
640         pr_debug("[MODEM_IF] misc_ioctl : 0x%x\n", cmd);
641
642         switch (cmd) {
643         case IOCTL_MODEM_ON:
644                 pr_debug("[MODEM_IF] misc_ioctl : IOCTL_MODEM_ON\n");
645                 return iod->mc->ops.modem_on(iod->mc);
646
647         case IOCTL_MODEM_OFF:
648                 pr_debug("[MODEM_IF] misc_ioctl : IOCTL_MODEM_OFF\n");
649                 return iod->mc->ops.modem_off(iod->mc);
650
651         case IOCTL_MODEM_RESET:
652                 pr_debug("[MODEM_IF] misc_ioctl : IOCTL_MODEM_RESET\n");
653                 return iod->mc->ops.modem_reset(iod->mc);
654
655         case IOCTL_MODEM_FORCE_CRASH_EXIT:
656                 pr_debug("[MODEM_IF] misc_ioctl : IOCTL_MODEM_FORCE_CRASH_EXIT\n");
657                 return iod->mc->ops.modem_force_crash_exit(iod->mc);
658
659         case IOCTL_MODEM_DUMP_RESET:
660                 pr_debug("[MODEM_IF] misc_ioctl : IOCTL_MODEM_FORCE_CRASH_EXIT\n");
661                 return iod->mc->ops.modem_dump_reset(iod->mc);
662
663         case IOCTL_MODEM_BOOT_ON:
664                 pr_debug("[MODEM_IF] misc_ioctl : IOCTL_MODEM_BOOT_ON\n");
665                 return iod->mc->ops.modem_boot_on(iod->mc);
666
667         case IOCTL_MODEM_BOOT_OFF:
668                 pr_debug("[MODEM_IF] misc_ioctl : IOCTL_MODEM_BOOT_OFF\n");
669                 return iod->mc->ops.modem_boot_off(iod->mc);
670
671         /* TODO - will remove this command after ril updated */
672         case IOCTL_MODEM_START:
673                 pr_debug("[MODEM_IF] misc_ioctl : IOCTL_MODEM_START\n");
674                 return 0;
675
676         case IOCTL_MODEM_STATUS:
677                 pr_debug("[MODEM_IF] misc_ioctl : IOCTL_MODEM_STATUS\n");
678                 p_state = iod->mc->phone_state;
679                 if (p_state == STATE_NV_REBUILDING) {
680                         pr_info("[MODEM_IF] nv rebuild state : %d\n", p_state);
681                         iod->mc->phone_state = STATE_ONLINE;
682                 }
683                 return p_state;
684
685         case IOCTL_MODEM_DUMP_START:
686                 pr_debug("[MODEM_IF] misc_ioctl : IOCTL_MODEM_DUMP_START\n");
687                 return iod->link->dump_start(iod->link, iod);
688
689         case IOCTL_MODEM_DUMP_UPDATE:
690                 pr_debug("[MODEM_IF] misc_ioctl : IOCTL_MODEM_DUMP_UPDATE\n");
691                 return iod->link->dump_update(iod->link, iod, _arg);
692
693         case IOCTL_MODEM_GOTA_START:
694                 pr_debug("[GOTA] misc_ioctl : IOCTL_MODEM_GOTA_START\n");
695                 return iod->link->gota_start(iod->link, iod);
696
697         case IOCTL_MODEM_FW_UPDATE:
698                 pr_debug("[GOTA] misc_ioctl : IOCTL_MODEM_FW_UPDATE\n");
699                 return iod->link->modem_update(iod->link, iod, _arg);
700
701         default:
702                 return -EINVAL;
703         }
704 }
705
706 static ssize_t misc_write(struct file *filp, const char __user * buf,
707                         size_t count, loff_t *ppos)
708 {
709         struct io_device *iod = (struct io_device *)filp->private_data;
710         int frame_len = 0;
711         char frame_header_buf[sizeof(struct raw_hdr)];
712         struct sk_buff *skb;
713
714         /* TODO - check here flow control for only raw data */
715
716         if (iod->format == IPC_BOOT || iod->format == IPC_RAMDUMP)
717                 frame_len = count + get_header_size(iod);
718         else
719                 frame_len = count + SIZE_OF_HDLC_START + get_header_size(iod)
720                                         + SIZE_OF_HDLC_END;
721
722         skb = alloc_skb(frame_len, GFP_KERNEL);
723         if (!skb) {
724                 pr_err("[MODEM_IF] fail alloc skb (%d)\n", __LINE__);
725                 return -ENOMEM;
726         }
727
728         switch (iod->format) {
729         case IPC_BOOT:
730         case IPC_RAMDUMP:
731                 if (copy_from_user(skb_put(skb, count), buf, count) != 0) {
732                         dev_kfree_skb_any(skb);
733                         return -EFAULT;
734                 }
735                 break;
736
737         case IPC_RFS:
738                 memcpy(skb_put(skb, SIZE_OF_HDLC_START), hdlc_start,
739                                 SIZE_OF_HDLC_START);
740                 if (copy_from_user(skb_put(skb, count), buf, count) != 0) {
741                         dev_kfree_skb_any(skb);
742                         return -EFAULT;
743                 }
744                 memcpy(skb_put(skb, SIZE_OF_HDLC_END), hdlc_end,
745                                         SIZE_OF_HDLC_END);
746                 break;
747
748         default:
749                 memcpy(skb_put(skb, SIZE_OF_HDLC_START), hdlc_start,
750                                 SIZE_OF_HDLC_START);
751                 memcpy(skb_put(skb, get_header_size(iod)),
752                         get_header(iod, count, frame_header_buf),
753                         get_header_size(iod));
754                 if (copy_from_user(skb_put(skb, count), buf, count) != 0) {
755                         dev_kfree_skb_any(skb);
756                         return -EFAULT;
757                 }
758                 memcpy(skb_put(skb, SIZE_OF_HDLC_END), hdlc_end,
759                                         SIZE_OF_HDLC_END);
760                 break;
761         }
762
763         /* send data with sk_buff, link device will put sk_buff
764          * into the specific sk_buff_q and run work-q to send data
765          */
766         return iod->link->send(iod->link, iod, skb);
767 }
768
769 static ssize_t misc_read(struct file *filp, char *buf, size_t count,
770                         loff_t *f_pos)
771 {
772         struct io_device *iod = (struct io_device *)filp->private_data;
773         struct sk_buff *skb;
774         int pktsize = 0;
775
776         skb = skb_dequeue(&iod->sk_rx_q);
777         if (!skb) {
778                 printk_ratelimited(KERN_ERR "[MODEM_IF] no data from sk_rx_q, "
779                         "modem_state : %s(%s)\n",
780                         modem_state_name[iod->mc->phone_state], iod->name);
781                 return 0;
782         }
783
784         if (skb->len > count) {
785                 pr_err("[MODEM_IF] skb len is too big = %d,%d!(%d)\n",
786                                 count, skb->len, __LINE__);
787                 dev_kfree_skb_any(skb);
788                 return -EIO;
789         }
790         pr_debug("[MODEM_IF] skb len : %d\n", skb->len);
791
792         pktsize = skb->len;
793         if (copy_to_user(buf, skb->data, pktsize) != 0)
794                 return -EIO;
795         dev_kfree_skb_any(skb);
796
797         pr_debug("[MODEM_IF] copy to user : %d\n", pktsize);
798
799         return pktsize;
800 }
801
802 static const struct file_operations misc_io_fops = {
803         .owner = THIS_MODULE,
804         .open = misc_open,
805         .release = misc_release,
806         .poll = misc_poll,
807         .unlocked_ioctl = misc_ioctl,
808         .write = misc_write,
809         .read = misc_read,
810 };
811
812 static int vnet_open(struct net_device *ndev)
813 {
814         netif_start_queue(ndev);
815         return 0;
816 }
817
818 static int vnet_stop(struct net_device *ndev)
819 {
820         netif_stop_queue(ndev);
821         return 0;
822 }
823
824 static int vnet_xmit(struct sk_buff *skb, struct net_device *ndev)
825 {
826         int ret;
827         struct raw_hdr hd;
828         struct sk_buff *skb_new;
829         struct vnet *vnet = netdev_priv(ndev);
830         struct io_device *iod = vnet->iod;
831
832         /* umts doesn't need to discard ethernet header */
833         if (iod->net_typ != UMTS_NETWORK) {
834                 if (iod->id >= PSD_DATA_CHID_BEGIN &&
835                         iod->id <= PSD_DATA_CHID_END)
836                         skb_pull(skb, sizeof(struct ethhdr));
837         }
838
839         hd.len = skb->len + sizeof(hd);
840         hd.control = 0;
841         hd.channel = iod->id & 0x1F;
842
843         skb_new = skb_copy_expand(skb, sizeof(hd) + sizeof(hdlc_start),
844                                 sizeof(hdlc_end), GFP_ATOMIC);
845         if (!skb_new) {
846                 dev_kfree_skb_any(skb);
847                 return -ENOMEM;
848         }
849
850         memcpy(skb_push(skb_new, sizeof(hd)), &hd, sizeof(hd));
851         memcpy(skb_push(skb_new, sizeof(hdlc_start)), hdlc_start,
852                                 sizeof(hdlc_start));
853         memcpy(skb_put(skb_new, sizeof(hdlc_end)), hdlc_end, sizeof(hdlc_end));
854
855         ret = iod->link->send(iod->link, iod, skb_new);
856         if (ret < 0) {
857                 dev_kfree_skb_any(skb);
858                 return NETDEV_TX_BUSY;
859         }
860
861         ndev->stats.tx_packets++;
862         ndev->stats.tx_bytes += skb->len;
863         dev_kfree_skb_any(skb);
864
865         return NETDEV_TX_OK;
866 }
867
868 static struct net_device_ops vnet_ops = {
869         .ndo_open = vnet_open,
870         .ndo_stop = vnet_stop,
871         .ndo_start_xmit = vnet_xmit,
872 };
873
874 static void vnet_setup(struct net_device *ndev)
875 {
876         ndev->netdev_ops = &vnet_ops;
877         ndev->type = ARPHRD_PPP;
878         ndev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
879         ndev->addr_len = 0;
880         ndev->hard_header_len = 0;
881         ndev->tx_queue_len = 1000;
882         ndev->mtu = ETH_DATA_LEN;
883         ndev->watchdog_timeo = 5 * HZ;
884 }
885
886 static void vnet_setup_ether(struct net_device *ndev)
887 {
888         ndev->netdev_ops = &vnet_ops;
889         ndev->type = ARPHRD_ETHER;
890         ndev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST | IFF_SLAVE;
891         ndev->addr_len = ETH_ALEN;
892         random_ether_addr(ndev->dev_addr);
893         ndev->hard_header_len = 0;
894         ndev->tx_queue_len = 1000;
895         ndev->mtu = ETH_DATA_LEN;
896         ndev->watchdog_timeo = 5 * HZ;
897 }
898
899 int init_io_device(struct io_device *iod)
900 {
901         int ret = 0;
902         struct vnet *vnet;
903
904         /* get modem state from modem control device */
905         iod->modem_state_changed = io_dev_modem_state_changed;
906         /* get data from link device */
907         iod->recv = io_dev_recv_data_from_link_dev;
908
909         INIT_LIST_HEAD(&iod->list);
910
911         /* register misc or net drv */
912         switch (iod->io_typ) {
913         case IODEV_MISC:
914                 init_waitqueue_head(&iod->wq);
915                 skb_queue_head_init(&iod->sk_rx_q);
916                 INIT_DELAYED_WORK(&iod->rx_work, rx_iodev_work);
917
918                 iod->miscdev.minor = MISC_DYNAMIC_MINOR;
919                 iod->miscdev.name = iod->name;
920                 iod->miscdev.fops = &misc_io_fops;
921
922                 ret = misc_register(&iod->miscdev);
923                 if (ret)
924                         pr_err("failed to register misc io device : %s\n",
925                                                 iod->name);
926
927                 break;
928
929         case IODEV_NET:
930                 if (iod->net_typ == UMTS_NETWORK)
931                         iod->ndev = alloc_netdev(0, iod->name, vnet_setup);
932                 else
933                         iod->ndev = alloc_netdev(0, iod->name,
934                                                 vnet_setup_ether);
935                 if (!iod->ndev) {
936                         pr_err("failed to alloc netdev\n");
937                         return -ENOMEM;
938                 }
939
940                 ret = register_netdev(iod->ndev);
941                 if (ret)
942                         free_netdev(iod->ndev);
943
944                 pr_debug("%s: %d(iod:0x%p)\n", __func__, __LINE__, iod);
945                 vnet = netdev_priv(iod->ndev);
946                 pr_debug("%s: %d(vnet:0x%p)\n", __func__, __LINE__, vnet);
947                 vnet->iod = iod;
948
949                 break;
950
951         case IODEV_DUMMY:
952                 skb_queue_head_init(&iod->sk_rx_q);
953                 INIT_DELAYED_WORK(&iod->rx_work, rx_iodev_work);
954
955                 break;
956
957         default:
958                 pr_err("wrong io_type : %d\n", iod->io_typ);
959                 return -EINVAL;
960         }
961
962         pr_debug("[MODEM_IF] %s(%d) : init_io_device() done : %d\n",
963                                 iod->name, iod->io_typ, ret);
964         return ret;
965 }