2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/ioctl.h>
23 #include <sys/types.h>
29 #define TRACE_TAG TRACE_USB
42 static void *usb_open_thread(void *x)
44 struct usb_handle *usb = (struct usb_handle *)x;
48 // wait until the USB device needs opening
49 sdb_mutex_lock(&usb->lock);
51 sdb_cond_wait(&usb->notify, &usb->lock);
52 sdb_mutex_unlock(&usb->lock);
54 D("[ usb_thread - opening device ]\n");
56 /* XXX use inotify? */
57 fd = unix_open(USB_NODE_FILE, O_RDWR); /* tizen-specific */
59 // to support older kernels
60 //fd = unix_open("/dev/android", O_RDWR);
61 D("[ opening %s device failed ]\n", USB_NODE_FILE);
67 D("[ opening device succeeded ]\n");
69 if (close_on_exec(fd) < 0) {
70 D("[closing fd exec failed ]\n");
74 D("[ usb_thread - registering device ]\n");
75 register_usb_transport(usb, 0, 1);
82 // Public host/client interface
84 int linux_usb_write(usb_handle *h, const void *data, int len)
88 D("about to write (fd=%d, len=%d)\n", h->fd, len);
89 n = sdb_write(h->fd, data, len);
91 D("ERROR: fd = %d, n = %d, errno = %d\n",
95 D("[ done fd=%d ]\n", h->fd);
99 int linux_usb_read(usb_handle *h, void *data, size_t len)
101 D("about to read (fd=%d, len=%d)\n", h->fd, len);
103 /* The sdb_read does not support read larger than 4096 bytes at once.
104 Read 4096 byte block repeatedly when reading data is larger than 4096 bytes. */
105 int bytes_to_read = len < 4096 ? len : 4096;
106 int n = sdb_read(h->fd, data, bytes_to_read);
111 D("ERROR: fd = %d, n = %d, errno = %d\n", h->fd, n, errno);
116 data = ((char*) data) + n;
118 D("[ done fd=%d ]\n", h->fd);
122 void linux_usb_init()
128 h = calloc(1, sizeof(usb_handle));
130 D("failed to allocate memory of usb_handle\n");
135 sdb_cond_init(&h->notify, 0);
136 sdb_mutex_init(&h->lock, 0);
138 // Open the file /dev/android_sdb_enable to trigger
139 // the enabling of the sdb USB function in the kernel.
140 // We never touch this file again - just leave it open
141 // indefinitely so the kernel will know when we are running
142 // and when we are not.
143 #if 0 /* tizen specific */
144 fd = unix_open("/dev/android_sdb_enable", O_RDWR);
146 D("failed to open /dev/android_sdb_enable\n");
151 D("[ usb_init - starting thread ]\n");
152 if(sdb_thread_create(&tid, usb_open_thread, h)){
153 fatal_errno("cannot create usb thread");
157 void linux_usb_kick(usb_handle *h)
160 sdb_mutex_lock(&h->lock);
164 // notify usb_open_thread that we are disconnected
165 sdb_cond_signal(&h->notify);
166 sdb_mutex_unlock(&h->lock);
169 int linux_usb_close(usb_handle *h)
171 // nothing to do here
175 void linux_usb_cleanup()
177 // nothing to do here