4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Junhwan An <jh48.an@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
30 #include <sys/ioctl.h>
33 #include "legacy/TelUtility.h"
35 #include "vdpram_dump.h"
39 # define TIOCMODG TIOCMGET
42 # define TIOCMODG MCGETA
49 # define TIOCMODS TIOCMSET
52 # define TIOCMODS MCSETA
57 typedef struct _tty_old_setting_t{
59 struct termios termiosVal;
60 struct _tty_old_setting_t *next;
61 struct _tty_old_setting_t *prev;
64 #define VDPRAM_OPEN_PATH "/dev/vdpram0"
66 /* DPRAM ioctls for DPRAM tty devices */
67 #define IOC_MZ_MAGIC ('h')
68 #define HN_DPRAM_PHONE_ON _IO (IOC_MZ_MAGIC, 0xd0)
69 #define HN_DPRAM_PHONE_OFF _IO (IOC_MZ_MAGIC, 0xd1)
70 #define HN_DPRAM_PHONE_GETSTATUS _IOR(IOC_MZ_MAGIC, 0xd2, unsigned int)
72 static tty_old_setting_t *ttyold_head = NULL;
74 /* static functions */
75 static void __insert_tty_oldsetting(tty_old_setting_t *me)
77 dbg("Function Enterence.");
83 ttyold_head->prev = me;
85 me->next = ttyold_head;
90 static tty_old_setting_t *__search_tty_oldsetting(int fd)
92 tty_old_setting_t *tty = NULL;
94 dbg("Function Enterence.");
96 if (ttyold_head == NULL)
103 dbg("oldsetting for inputted fd is found");
107 if (tty->next == NULL) {
108 dbg("No oldsetting is found");
119 static void __remove_tty_oldsetting(tty_old_setting_t *me)
121 dbg( "Function Enterence.");
127 me->prev->next = me->next;
129 ttyold_head = me->next;
132 me->next->prev = me->prev;
135 /* Set hardware flow control.
137 static void __tty_sethwf(int fd, int on)
141 dbg("Function Enterence.");
143 if (tcgetattr(fd, &tty))
144 err("__tty_sethwf: tcgetattr:");
147 tty.c_cflag |= CRTSCTS;
149 tty.c_cflag &= ~CRTSCTS;
151 if (tcsetattr(fd, TCSANOW, &tty))
152 err("__tty_sethwf: tcsetattr:");
156 * Set RTS line. Sometimes dropped. Linux specific?
158 static int __tty_setrts(int fd)
162 dbg("Function Enterence.");
164 if (-1 == ioctl(fd, TIOCMODG, &mcs))
165 err("icotl: TIOCMODG");
169 if (-1 == ioctl(fd, TIOCMODS, &mcs))
170 err("icotl: TIOCMODS");
176 * Set baudrate, parity and number of bits.
178 static int __tty_setparms(int fd,
179 const char* baudr, const char* par,
180 const char* bits, const char* stop,
186 int stop_bit = stop[0];
189 tty_old_setting_t *old_setting = NULL;
191 dbg("Function Enterence.");
193 old_setting = calloc(sizeof(tty_old_setting_t), 1);
195 if (old_setting == NULL)
196 return TAPI_API_SYSTEM_OUT_OF_MEM;
198 old_setting->fd = fd;
200 if (tcgetattr(fd, &tty) < 0) {
202 return TAPI_API_TRANSPORT_LAYER_FAILURE;
205 if (tcgetattr(fd, &old_setting->termiosVal) < 0) {
207 return TAPI_API_TRANSPORT_LAYER_FAILURE;
210 __insert_tty_oldsetting(old_setting);
214 /* We generate mark and space parity ourself. */
215 if (bit == '7' && (par[0] == 'M' || par[0] == 'S'))
218 /* Check if 'baudr' is really a number */
219 if ((newbaud = (atol(baudr) / 100)) == 0 && baudr[0] != '0')
269 err("invaid baud rate");
274 cfsetospeed(&tty, (speed_t) spd);
275 cfsetispeed(&tty, (speed_t) spd);
281 tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS5;
285 tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS6;
289 tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS7;
294 tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
301 tty.c_cflag &= ~CSTOPB;
306 tty.c_cflag |= CSTOPB;
310 /* Set into raw, no echo mode */
311 tty.c_iflag = IGNBRK;
314 tty.c_cflag |= CLOCAL | CREAD;
319 tty.c_iflag |= IXON | IXOFF;
321 tty.c_iflag &= ~(IXON | IXOFF | IXANY);
323 tty.c_cflag &= ~(PARENB | PARODD);
326 tty.c_cflag |= PARENB;
327 else if (par[0] == 'O')
328 tty.c_cflag |= (PARENB | PARODD);
330 if (tcsetattr(fd, TCSANOW, &tty) < 0) {
332 return TAPI_API_TRANSPORT_LAYER_FAILURE;
336 __tty_sethwf(fd, hwf);
338 return TAPI_API_SUCCESS;
342 static int __tty_close(int fd)
344 tty_old_setting_t *old_setting = NULL;
346 dbg("Function Enterence.");
348 old_setting = __search_tty_oldsetting(fd);
349 if (old_setting == NULL)
350 return TAPI_API_SUCCESS;
352 if (tcsetattr(fd, TCSAFLUSH, &old_setting->termiosVal) < 0) {
354 return TAPI_API_TRANSPORT_LAYER_FAILURE;
357 __remove_tty_oldsetting(old_setting);
363 return TAPI_API_SUCCESS;
367 * restore the old settings before close.
369 int vdpram_close(int fd)
371 int ret = TAPI_API_SUCCESS;
373 dbg("Function Enterence.");
375 ret = __tty_close(fd);
381 * Open the vdpram fd.
383 int vdpram_open (void)
390 fd = open(VDPRAM_OPEN_PATH, O_RDWR);
393 err("#### Failed to open vdpram file: error no hex %x", errno);
397 dbg("#### Success to open vdpram file. fd:%d, path:%s", fd, VDPRAM_OPEN_PATH);
400 if (__tty_setparms(fd, "115200", "N", "8", "1", 0, 0) != TAPI_API_SUCCESS) {
405 dbg("#### Success set tty vdpram params. fd:%d", fd);
407 /*TODO: No need to check Status. Delete*/
408 cmd = HN_DPRAM_PHONE_GETSTATUS;
410 if (ioctl(fd, cmd, &val) < 0) {
411 err("#### ioctl failed fd:%d, cmd:%lu, val:%d", fd,cmd,val);
416 dbg("#### ioctl Success fd:%d, cmd:%lu, val:%d", fd,cmd,val);
423 * power on the phone.
425 int vdpram_poweron(int fd)
429 if (ioctl(fd, HN_DPRAM_PHONE_ON, NULL) < 0) {
430 err("Phone Power On failed (fd:%d)", fd);
434 dbg("Phone Power On success (fd:%d)", fd);
441 * Power Off the Phone.
443 int vdpram_poweroff(int fd)
447 if (ioctl(fd, HN_DPRAM_PHONE_OFF, NULL) < 0) {
448 err("Phone Power Off failed.");
452 dbg("Phone Power Off success.");
460 * Read data from vdpram.
463 int vdpram_tty_read(int nFd, void* buf, size_t nbytes)
467 if ((actual = read(nFd, buf, nbytes)) < 0) {
468 dbg("[TRANSPORT DPRAM]read failed.");
470 vdpram_hex_dump(IPC_RX, actual, buf);
475 static void __selectsleep(int sec,int msec)
480 select(0,NULL,NULL,NULL,&tv);
485 * Write data to vdpram.
487 int vdpram_tty_write(int nFd, void* buf, size_t nbytes)
494 vdpram_hex_dump(IPC_TX, nbytes, buf);
495 ret = write(nFd, (unsigned char* )buf, nbytes - actual);
497 if ((ret < 0 && errno == EAGAIN) || (ret < 0 && errno == EBUSY)) {
498 err("write failed. retry.. ret[%d] with errno[%d] ",ret, errno);
509 if (actual != nbytes)
510 err("write failed.ret[%d]",ret);
512 err("errno [%d]",errno);
519 } while(actual < nbytes);