tizen 2.3.1 release
[framework/system/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 "sdb.h"
31
32
33 struct usb_handle
34 {
35     int fd;
36     sdb_cond_t notify;
37     sdb_mutex_t lock;
38 };
39
40 void usb_cleanup()
41 {
42     // nothing to do here
43 }
44
45 static void *usb_open_thread(void *x)
46 {
47     struct usb_handle *usb = (struct usb_handle *)x;
48     int fd;
49
50     while (1) {
51         // wait until the USB device needs opening
52         sdb_mutex_lock(&usb->lock);
53         while (usb->fd != -1)
54             sdb_cond_wait(&usb->notify, &usb->lock);
55         sdb_mutex_unlock(&usb->lock);
56
57         D("[ usb_thread - opening device ]\n");
58         do {
59             /* XXX use inotify? */
60             fd = unix_open(USB_NODE_FILE, O_RDWR); /* tizen-specific */
61             if (fd < 0) {
62                 // to support older kernels
63                 //fd = unix_open("/dev/android", O_RDWR);
64                 D("[ opening %s device failed ]\n", USB_NODE_FILE);
65             }
66             if (fd < 0) {
67                 sdb_sleep_ms(1000);
68             }
69         } while (fd < 0);
70         D("[ opening device succeeded ]\n");
71
72         if (close_on_exec(fd) < 0) {
73             D("[closing fd exec failed ]\n");
74         }
75         usb->fd = fd;
76
77         D("[ usb_thread - registering device ]\n");
78         register_usb_transport(usb, 0, 1);
79     }
80
81     // never gets here
82     return 0;
83 }
84
85 int usb_write(usb_handle *h, const void *data, int len)
86 {
87     int n;
88
89     D("about to write (fd=%d, len=%d)\n", h->fd, len);
90     n = sdb_write(h->fd, data, len);
91     if(n != len) {
92         D("ERROR: fd = %d, n = %d, errno = %d\n",
93             h->fd, n, errno);
94         return -1;
95     }
96     D("[ done fd=%d ]\n", h->fd);
97     return 0;
98 }
99
100 int usb_read(usb_handle *h, void *data, int len)
101 {
102     int n;
103
104     D("about to read (fd=%d, len=%d)\n", h->fd, len);
105     n = sdb_read(h->fd, data, len);
106     if(n != len) {
107         D("ERROR: fd = %d, n = %d, errno = %d\n",
108             h->fd, n, errno);
109         return -1;
110     }
111     D("[ done fd=%d ]\n", h->fd);
112     return 0;
113 }
114
115 void usb_init()
116 {
117     usb_handle *h;
118     sdb_thread_t tid;
119 //  int fd;
120
121     h = calloc(1, sizeof(usb_handle));
122     if (h == NULL) {
123         D("cannot allocate usb handle");
124         return;
125     }
126     h->fd = -1;
127     sdb_cond_init(&h->notify, 0);
128     sdb_mutex_init(&h->lock, 0);
129
130     // Open the file /dev/android_sdb_enable to trigger 
131     // the enabling of the sdb USB function in the kernel.
132     // We never touch this file again - just leave it open
133     // indefinitely so the kernel will know when we are running
134     // and when we are not.
135 #if 0 /* tizen specific */
136     fd = unix_open("/dev/android_sdb_enable", O_RDWR);
137     if (fd < 0) {
138        D("failed to open /dev/android_sdb_enable\n");
139     } else {
140         close_on_exec(fd);
141     }
142 #endif
143     D("[ usb_init - starting thread ]\n");
144     if(sdb_thread_create(&tid, usb_open_thread, h)){
145         fatal_errno("cannot create usb thread");
146     }
147 }
148
149 void usb_kick(usb_handle *h)
150 {
151     D("usb_kick\n");
152     sdb_mutex_lock(&h->lock);
153     sdb_close(h->fd);
154     h->fd = -1;
155
156     // notify usb_open_thread that we are disconnected
157     sdb_cond_signal(&h->notify);
158     sdb_mutex_unlock(&h->lock);
159 }
160
161 int usb_close(usb_handle *h)
162 {
163     // nothing to do here
164     return 0;
165 }