4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
\r
6 * Contact: Junhwan An <jh48.an@samsung.com>
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
24 #include <termios.h>
\r
26 #include <sys/time.h>
\r
27 #include <sys/mman.h>
\r
30 #include <sys/ioctl.h>
\r
35 #include "legacy/TelUtility.h"
\r
37 #include "vdpram_dump.h"
\r
41 # define TIOCMODG TIOCMGET
\r
44 # define TIOCMODG MCGETA
\r
51 # define TIOCMODS TIOCMSET
\r
54 # define TIOCMODS MCSETA
\r
59 /* Retry parameters */
60 #define SLEEP_TIME_IN_SEC 0
61 #define SLEEP_TIME_IN_MSEC 50
63 #define MAX_RETRY_COUNT 10
65 typedef struct _tty_old_setting_t {
67 struct termios termiosVal;
\r
68 struct _tty_old_setting_t *next;
\r
69 struct _tty_old_setting_t *prev;
\r
70 } tty_old_setting_t;
\r
72 #define VDPRAM_OPEN_PATH "/dev/vdpram0"
\r
74 /* DPRAM ioctls for DPRAM tty devices */
\r
75 #define IOC_MZ_MAGIC ('h')
\r
76 #define HN_DPRAM_PHONE_ON _IO (IOC_MZ_MAGIC, 0xd0)
\r
77 #define HN_DPRAM_PHONE_OFF _IO (IOC_MZ_MAGIC, 0xd1)
\r
78 #define HN_DPRAM_PHONE_GETSTATUS _IOR(IOC_MZ_MAGIC, 0xd2, unsigned int)
\r
80 static tty_old_setting_t *ttyold_head = NULL;
\r
83 * Insert TTY old settings.
85 static void __insert_tty_oldsetting(tty_old_setting_t *me)
\r
87 dbg("Function Entrance");
93 ttyold_head->prev = me;
95 me->next = ttyold_head;
\r
101 * Search TTY old settings.
103 static tty_old_setting_t *__search_tty_oldsetting(int fd)
\r
105 tty_old_setting_t *tty = NULL;
\r
107 dbg("Function Entrance");
109 if (ttyold_head == NULL)
\r
115 if (tty->fd == fd) {
\r
116 dbg("oldsetting for inputted fd [%d] is found", fd);
119 if (tty->next == NULL) {
\r
120 err("No oldsetting found!!!");
132 * Remove TTY old settings.
134 static void __remove_tty_oldsetting(tty_old_setting_t *me)
\r
136 dbg( "Function Enterence.");
\r
142 me->prev->next = me->next;
144 ttyold_head = me->next;
147 me->next->prev = me->prev;
151 * Set hardware flow control.
153 static void __tty_sethwf(int fd, int on)
\r
155 struct termios tty;
\r
157 dbg("Function Entrance");
159 if (tcgetattr(fd, &tty))
\r
160 err("__tty_sethwf: tcgetattr:");
\r
163 tty.c_cflag |= CRTSCTS;
165 tty.c_cflag &= ~CRTSCTS;
167 if (tcsetattr(fd, TCSANOW, &tty))
\r
168 err("__tty_sethwf: tcsetattr:");
\r
172 * Set RTS line. Sometimes dropped. Linux specific?
174 static int __tty_setrts(int fd)
\r
178 dbg("Function Entrance");
180 if (-1 == ioctl(fd, TIOCMODG, &mcs))
\r
181 err("icotl: TIOCMODG");
\r
185 if (-1 == ioctl(fd, TIOCMODS, &mcs))
\r
186 err("icotl: TIOCMODS");
\r
192 * Set baudrate, parity and number of bits.
194 static int __tty_setparms(int fd, char* baudr, char* par, char* bits, char* stop, int hwf, int swf)
\r
199 int stop_bit = stop[0];
\r
201 struct termios tty;
\r
202 tty_old_setting_t *old_setting = NULL;
\r
204 dbg("Function Entrance");
206 old_setting = g_try_new0(tty_old_setting_t, 1);
208 if (old_setting == NULL)
\r
209 return TAPI_API_SYSTEM_OUT_OF_MEM;
\r
211 old_setting->fd = fd;
\r
213 if (tcgetattr(fd, &tty) < 0) {
\r
215 return TAPI_API_TRANSPORT_LAYER_FAILURE;
\r
218 if (tcgetattr(fd, &old_setting->termiosVal) < 0) {
\r
220 return TAPI_API_TRANSPORT_LAYER_FAILURE;
\r
223 __insert_tty_oldsetting(old_setting);
\r
227 /* We generate mark and space parity ourself. */
\r
228 if (bit == '7' && (par[0] == 'M' || par[0] == 'S'))
\r
231 /* Check if 'baudr' is really a number */
\r
232 if ((newbaud = (atol(baudr) / 100)) == 0 && baudr[0] != '0')
\r
281 err("invaid baud rate");
\r
286 cfsetospeed(&tty, (speed_t) spd);
287 cfsetispeed(&tty, (speed_t) spd);
292 tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS5;
296 tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS6;
300 tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS7;
305 tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
311 tty.c_cflag &= ~CSTOPB;
316 tty.c_cflag |= CSTOPB;
320 /* Set into raw, no echo mode */
\r
321 tty.c_iflag = IGNBRK;
\r
324 tty.c_cflag |= CLOCAL | CREAD;
\r
325 tty.c_cc[VMIN] = 1;
\r
326 tty.c_cc[VTIME] = 1;
\r
329 tty.c_iflag |= IXON | IXOFF;
331 tty.c_iflag &= ~(IXON | IXOFF | IXANY);
333 tty.c_cflag &= ~(PARENB | PARODD);
\r
336 tty.c_cflag |= PARENB;
337 else if (par[0] == 'O')
\r
338 tty.c_cflag |= (PARENB | PARODD);
340 if (tcsetattr(fd, TCSANOW, &tty) < 0) {
\r
342 return TAPI_API_TRANSPORT_LAYER_FAILURE;
346 __tty_sethwf(fd, hwf);
\r
348 return TAPI_API_SUCCESS;
\r
354 static int __tty_close(int fd)
\r
356 tty_old_setting_t *old_setting = NULL;
\r
358 dbg("Function Entrance");
360 /* Get previous settings */
361 old_setting = __search_tty_oldsetting(fd);
\r
362 if (old_setting == NULL) {
363 dbg("[VDPRAM] No previous settings found!!!");
364 return TAPI_API_SUCCESS;
\r
367 if (tcsetattr(fd, TCSAFLUSH, &old_setting->termiosVal) < 0) {
\r
368 err("[VDPRAM] Close failed");
369 return TAPI_API_TRANSPORT_LAYER_FAILURE;
\r
372 /* Remove the previous setting configured */
373 __remove_tty_oldsetting(old_setting);
\r
381 return TAPI_API_SUCCESS;
\r
387 static void __sleep(int sec, int msec)
394 select(0, NULL, NULL, NULL, &tv);
398 * Close the VDPRAM device
400 int vdpram_close(int fd)
\r
402 int ret = TAPI_API_SUCCESS;
\r
404 dbg("Function Entrance");
406 /* Close VDPRAM Device */
407 ret = __tty_close(fd);
\r
413 * Open the VDPRAM device
415 int vdpram_open (void)
\r
420 unsigned int cmd =0;
\r
422 dbg("Function Enterence.");
424 /* Open DPRAM device */
425 fd = open(VDPRAM_OPEN_PATH, O_RDWR);
\r
427 err("[VDPRAM] Open VDPRAM file - [FAIL] Error: [%s]", strerror(errno));
430 dbg("[VDPRAM] Open VDPRAM file - [SUCCESS] fd: [%d] path: [%s]",
431 fd, VDPRAM_OPEN_PATH);
434 /* Set device parameters */
435 if (__tty_setparms(fd, "115200", "N", "8", "1", 0, 0) != TAPI_API_SUCCESS) {
436 err("[VDPRAM] Set TTY device parameters - [FAIL]");
438 /* Close VDPRAM Device */
443 dbg("[VDPRAM] Set TTY device parameters - [SUCCESS]");
446 /* TODO: No need to check Status. Delete */
447 cmd = HN_DPRAM_PHONE_GETSTATUS;
\r
448 if (ioctl(fd, cmd, &val) < 0) {
449 err("[VDPRAM] Get Phone status - [FAIL] fd: [d] cmd: [%d] val: [%d]",
457 dbg("[VDPRAM] Get Phone status - [SUCCESS]");
464 * Power ON the Phone.
466 gboolean vdpram_poweron(int fd)
468 if (ioctl(fd, HN_DPRAM_PHONE_ON, NULL) < 0) {
\r
469 err("[VDPRAM] Phone Power ON [FAIL] - fd: [%d] Error: [%s]", fd, strerror(errno));
473 dbg("[VDPRAM] Phone Power ON [SUCCESS] - fd: [%d]", fd);
479 * Power OFF the Phone.
481 gboolean vdpram_poweroff(int fd)
483 if (ioctl(fd, HN_DPRAM_PHONE_OFF, NULL) < 0) {
\r
484 err("[VDPRAM] Phone Power OFF [FAIL] - fd: [%d] Error: [%s]", fd, strerror(errno));
488 dbg("[VDPRAM] Phone Power OFF [SUCCESS] - fd: [%d]", fd);
494 * Read data from VDPRAM.
496 int vdpram_tty_read(int nFd, void* buf, size_t nbytes)
\r
500 if ((actual = read(nFd, buf, nbytes)) < 0) {
\r
501 err("[VDPRAM] Read [FAIL] - fd: [%d] Error: [%s]", nFd, strerror(errno));
504 /* Dumping Read data */
505 vdpram_hex_dump(RX, actual, buf);
511 * Write data to VDPRAM.
513 int vdpram_tty_write(int nFd, void* buf, size_t nbytes)
\r
520 vdpram_hex_dump(TX, nbytes, buf);
522 /* Write to Device */
523 ret = write(nFd, (unsigned char* )buf, nbytes - actual);
\r
525 err("[VDPRAM] Write [FAIL] - fd: [%d] Error: [%s]",
526 nFd, strerror(errno));
528 if ((errno == EAGAIN) || (errno == EBUSY)) {
529 /* Sleep for 50 msecs */
530 __sleep(SLEEP_TIME_IN_SEC, SLEEP_TIME_IN_MSEC);
532 if (retry == MAX_RETRY_COUNT) {
533 err("[VDPRAM] Maximum retries completed!!!");
541 if (actual != nbytes)
542 err("[VDPRAM] Write [FAIL] - fd: [%d]", nFd);
544 err("[VDPRAM] Write [FAIL] - Error: [%s]", strerror(errno));
550 dbg("[VDPRAM] Write Actual bytes: [%d] Written bytes: [%d]", actual, ret);
551 } while(actual < nbytes);
\r