1 /*
2  * Copyright (C) 2012 The Android Open Source Project
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 <unistd.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/ioctl.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <pthread.h>
28 #include <time.h>
29 
30 #include <sys/inotify.h>
31 #include <linux/hidraw.h>
32 #include <usbhost/usbhost.h>
33 
34 #include "f_accessory.h"
35 #include "accessory.h"
36 
37 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
38 
39 static int next_id = 1;
40 
milli_sleep(int millis)41 static void milli_sleep(int millis) {
42     struct timespec tm;
43 
44     tm.tv_sec = 0;
45     tm.tv_nsec = millis * 1000000;
46     nanosleep(&tm, NULL);
47 }
48 
hid_thread(void * arg)49 static void* hid_thread(void* arg) {
50     int fd = (int)arg;
51     char buffer[4096];
52     int id, ret, offset;
53     struct usb_device *device;
54     struct usb_device_descriptor *device_desc;
55     int max_packet;
56     struct hidraw_report_descriptor desc;
57     int desc_length;
58 
59     fprintf(stderr, "hid_thread start fd: %d\n", fd);
60 
61     if (ioctl(fd, HIDIOCGRDESCSIZE, &desc_length)) {
62         fprintf(stderr, "HIDIOCGRDESCSIZE failed\n");
63         close(fd);
64         goto err;
65     }
66 
67     desc.size = HID_MAX_DESCRIPTOR_SIZE - 1;
68     if (ioctl(fd, HIDIOCGRDESC, &desc)) {
69         fprintf(stderr, "HIDIOCGRDESC failed\n");
70         close(fd);
71         goto err;
72     }
73 
74 wait_for_device:
75     fprintf(stderr, "waiting for device fd: %d\n", fd);
76     device = usb_wait_for_device();
77     max_packet = usb_device_get_device_descriptor(device)->bMaxPacketSize0;
78     // FIXME
79     max_packet--;
80 
81     // FIXME
82     milli_sleep(500);
83 
84     pthread_mutex_lock(&mutex);
85     id = next_id++;
86 
87     ret = usb_device_control_transfer(device, USB_DIR_OUT | USB_TYPE_VENDOR,
88             ACCESSORY_REGISTER_HID, id, desc_length, NULL, 0, 1000);
89     fprintf(stderr, "ACCESSORY_REGISTER_HID returned %d\n", ret);
90 
91     // FIXME
92     milli_sleep(500);
93 
94     for (offset = 0; offset < desc_length; ) {
95         int count = desc_length - offset;
96         if (count > max_packet) count = max_packet;
97 
98     fprintf(stderr, "sending ACCESSORY_SET_HID_REPORT_DESC offset: %d count: %d desc_length: %d\n",
99             offset, count, desc_length);
100         ret = usb_device_control_transfer(device, USB_DIR_OUT | USB_TYPE_VENDOR,
101                 ACCESSORY_SET_HID_REPORT_DESC, id, offset, &desc.value[offset], count, 1000);
102     fprintf(stderr, "ACCESSORY_SET_HID_REPORT_DESC returned %d errno %d\n", ret, errno);
103         offset += count;
104     }
105 
106     pthread_mutex_unlock(&mutex);
107 
108     while (1) {
109         ret = read(fd, buffer, sizeof(buffer));
110         if (ret < 0) {
111 fprintf(stderr, "read failed, errno: %d, fd: %d\n", errno, fd);
112             break;
113         }
114 
115         ret = usb_device_control_transfer(device, USB_DIR_OUT | USB_TYPE_VENDOR,
116                     ACCESSORY_SEND_HID_EVENT, id, 0, buffer, ret, 1000);
117         if (ret < 0 && errno != EPIPE) {
118 fprintf(stderr, "ACCESSORY_SEND_HID_EVENT returned %d errno: %d\n", ret, errno);
119             goto wait_for_device;
120         }
121     }
122 
123 fprintf(stderr, "ACCESSORY_UNREGISTER_HID\n");
124     ret = usb_device_control_transfer(device, USB_DIR_OUT | USB_TYPE_VENDOR,
125             ACCESSORY_UNREGISTER_HID, id, 0, NULL, 0, 1000);
126 
127 fprintf(stderr, "hid thread exiting\n");
128 err:
129     return NULL;
130 }
131 
open_hid(const char * name)132 static void open_hid(const char* name)
133 {
134     char path[100];
135 
136     snprintf(path, sizeof(path), "/dev/%s", name);
137     int fd = open(path, O_RDWR);
138     if (fd < 0) return;
139 
140     fprintf(stderr, "opened /dev/%s\n", name);
141     pthread_t th;
142     pthread_create(&th, NULL, hid_thread, (void *)(uintptr_t)fd);
143 }
144 
inotify_thread(void * arg)145 static void* inotify_thread(void* arg)
146 {
147     open_hid("hidraw0");
148     open_hid("hidraw1");
149     open_hid("hidraw2");
150     open_hid("hidraw3");
151     open_hid("hidraw4");
152     open_hid("hidraw5");
153     open_hid("hidraw6");
154     open_hid("hidraw7");
155     open_hid("hidraw8");
156     open_hid("hidraw9");
157 
158     int inotify_fd = inotify_init();
159     inotify_add_watch(inotify_fd, "/dev", IN_DELETE | IN_CREATE);
160 
161     while (1) {
162         char event_buf[512];
163         struct inotify_event *event;
164         int event_pos = 0;
165         int event_size;
166 
167         int count = read(inotify_fd, event_buf, sizeof(event_buf));
168         if (count < (int)sizeof(*event)) {
169             if(errno == EINTR)
170                 continue;
171             fprintf(stderr, "could not get event, %s\n", strerror(errno));
172             break;
173         }
174         while (count >= (int)sizeof(*event)) {
175             event = (struct inotify_event *)(event_buf + event_pos);
176             //fprintf(stderr, "%d: %08x \"%s\"\n", event->wd, event->mask,
177             //        event->len ? event->name : "");
178             if (event->len) {
179                 if(event->mask & IN_CREATE) {
180                     fprintf(stderr, "created %s\n", event->name);
181                     // FIXME
182                     milli_sleep(50);
183                     open_hid(event->name);
184                 } else {
185                     fprintf(stderr, "lost %s\n", event->name);
186                 }
187             }
188             event_size = sizeof(*event) + event->len;
189             count -= event_size;
190             event_pos += event_size;
191         }
192     }
193 
194     close(inotify_fd);
195     return NULL;
196 }
197 
init_hid()198 void init_hid()
199 {
200     pthread_t th;
201     pthread_create(&th, NULL, inotify_thread, NULL);
202 }
203