source: remove integer underflow
[sdk/target/sdbd.git] / src / usb_linux_client.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21
22 #include <sys/ioctl.h>
23 #include <sys/types.h>
24 #include <dirent.h>
25 #include <errno.h>
26
27 #include "sysdeps.h"
28
29 #define   TRACE_TAG  TRACE_USB
30 #include "log.h"
31
32 #include "sdb.h"
33
34
35 struct usb_handle
36 {
37     int fd;
38     sdb_cond_t notify;
39     sdb_mutex_t lock;
40 };
41
42 static void *usb_open_thread(void *x)
43 {
44     struct usb_handle *usb = (struct usb_handle *)x;
45     int fd;
46
47     while (1) {
48         // wait until the USB device needs opening
49         sdb_mutex_lock(&usb->lock);
50         while (usb->fd != -1)
51             sdb_cond_wait(&usb->notify, &usb->lock);
52         sdb_mutex_unlock(&usb->lock);
53
54         D("[ usb_thread - opening device ]\n");
55         do {
56             /* XXX use inotify? */
57             fd = unix_open(USB_NODE_FILE, O_RDWR); /* tizen-specific */
58             if (fd < 0) {
59                 // to support older kernels
60                 //fd = unix_open("/dev/android", O_RDWR);
61                 D("[ opening %s device failed ]\n", USB_NODE_FILE);
62             }
63             if (fd < 0) {
64                 sdb_sleep_ms(1000);
65             }
66         } while (fd < 0);
67         D("[ opening device succeeded ]\n");
68
69         if (close_on_exec(fd) < 0) {
70             D("[closing fd exec failed ]\n");
71         }
72         usb->fd = fd;
73
74         D("[ usb_thread - registering device ]\n");
75         register_usb_transport(usb, 0, 1);
76     }
77
78     // never gets here
79     return 0;
80 }
81
82 // Public host/client interface
83
84 int linux_usb_write(usb_handle *h, const void *data, int len)
85 {
86     int n;
87
88     D("about to write (fd=%d, len=%d)\n", h->fd, len);
89     n = sdb_write(h->fd, data, len);
90     if(n != len) {
91         D("ERROR: fd = %d, n = %d, errno = %d\n",
92             h->fd, n, errno);
93         return -1;
94     }
95     D("[ done fd=%d ]\n", h->fd);
96     return 0;
97 }
98
99 int linux_usb_read(usb_handle *h, void *data, size_t len)
100 {
101     D("about to read (fd=%d, len=%zu)\n", h->fd, len);
102     int l = len;
103     while (l > 0) {
104         /* The sdb_read does not support read larger than 4096 bytes at once.
105            Read 4096 byte block repeatedly when reading data is larger than 4096 bytes. */
106         int bytes_to_read = l < 4096 ? l : 4096;
107         int n = sdb_read(h->fd, data, bytes_to_read);
108         if (n < 0) {
109             if (errno == EINTR) {
110                 continue;
111             } else {
112                 D("ERROR: fd = %d, n = %d, errno = %d\n", h->fd, n, errno);
113                 return -1;
114             }
115         }
116         l = (l >= n) ? l - n : 0;
117         data = ((char*)data) + n;
118     }
119     D("[ done fd=%d ]\n", h->fd);
120     return 0;
121 }
122
123 void linux_usb_init()
124 {
125     usb_handle *h;
126     sdb_thread_t tid;
127 //  int fd;
128
129     h = calloc(1, sizeof(usb_handle));
130     if (h == NULL) {
131         D("failed to allocate memory of usb_handle\n");
132         return;
133     }
134
135     h->fd = -1;
136     sdb_cond_init(&h->notify, 0);
137     sdb_mutex_init(&h->lock, 0);
138
139     // Open the file /dev/android_sdb_enable to trigger
140     // the enabling of the sdb USB function in the kernel.
141     // We never touch this file again - just leave it open
142     // indefinitely so the kernel will know when we are running
143     // and when we are not.
144 #if 0 /* tizen specific */
145     fd = unix_open("/dev/android_sdb_enable", O_RDWR);
146     if (fd < 0) {
147        D("failed to open /dev/android_sdb_enable\n");
148     } else {
149         close_on_exec(fd);
150     }
151 #endif
152     D("[ usb_init - starting thread ]\n");
153     if(sdb_thread_create(&tid, usb_open_thread, h)){
154         fatal_errno("cannot create usb thread");
155     }
156 }
157
158 void linux_usb_kick(usb_handle *h)
159 {
160     D("usb_kick\n");
161     sdb_mutex_lock(&h->lock);
162     sdb_close(h->fd);
163     h->fd = -1;
164
165     // notify usb_open_thread that we are disconnected
166     sdb_cond_signal(&h->notify);
167     sdb_mutex_unlock(&h->lock);
168 }
169
170 int linux_usb_close(usb_handle *h)
171 {
172     // nothing to do here
173     return 0;
174 }
175
176 void linux_usb_cleanup()
177 {
178     // nothing to do here
179 }