2 * TeensyTP, Team Player/4-Player Adaptor implementation for Teensy3
4 * Copyright (c) 2015 notaz
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/select.h>
41 #include <linux/usbdevice_fs.h>
42 #include <linux/usb/ch9.h>
43 #include <linux/input.h>
46 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
56 /* return 1 if found, 0 if not, < 0 on error */
57 static int find_device(struct teensy_dev *dev,
58 uint16_t vendor, uint16_t product)
60 const char path_root[] = "/dev/bus/usb";
62 struct usb_descriptor_header hdr;
63 struct usb_device_descriptor d;
64 struct usb_config_descriptor c;
65 struct usb_interface_descriptor i;
66 struct usb_endpoint_descriptor e;
67 char space[0x100]; /* enough? */
69 char path_bus[256], path_dev[256];
70 struct dirent *ent, *ent_bus;
71 DIR *dir = NULL, *dir_bus = NULL;
77 memset(dev, 0xff, sizeof(*dev));
79 dir = opendir(path_root);
85 for (ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
86 /* should be a number like 000 */
87 if (sscanf(ent->d_name, "%03d", &num) != 1)
90 snprintf(path_bus, sizeof(path_bus), "%s/%s",
91 path_root, ent->d_name);
93 dir_bus = opendir(path_bus);
97 ent_bus = readdir(dir_bus);
98 for (; ent_bus != NULL; ent_bus = readdir(dir_bus)) {
99 if (sscanf(ent->d_name, "%03d", &num) != 1)
102 snprintf(path_dev, sizeof(path_dev), "%s/%s/%s",
103 path_root, ent->d_name, ent_bus->d_name);
105 fd = open(path_dev, O_RDWR);
109 ret = read(fd, &desc.d, sizeof(desc.d));
110 if (ret != sizeof(desc.d)) {
111 fprintf(stderr, "desc read: %d/%zd: ", ret, sizeof(desc.d));
116 if (desc.d.bDescriptorType != USB_DT_DEVICE) {
117 fprintf(stderr, "%s: bad DT: 0x%02x\n",
118 path_dev, desc.d.bDescriptorType);
122 if (desc.d.idVendor == vendor && desc.d.idProduct == product)
139 if (desc.d.bNumConfigurations != 1) {
140 fprintf(stderr, "unexpected bNumConfigurations: %u\n",
141 desc.d.bNumConfigurations);
145 /* walk through all descriptors */
148 ret = read(fd, &desc.hdr, sizeof(desc.hdr));
151 if (ret != sizeof(desc.hdr)) {
152 fprintf(stderr, "desc.hdr read: %d/%zd: ", ret, sizeof(desc.hdr));
157 ret = (int)lseek(fd, -sizeof(desc.hdr), SEEK_CUR);
163 ret = read(fd, &desc, desc.hdr.bLength);
164 if (ret != desc.hdr.bLength) {
165 fprintf(stderr, "desc read: %d/%u: ", ret, desc.hdr.bLength);
170 switch (desc.hdr.bDescriptorType) {
172 if (desc.c.bNumInterfaces != 2) {
173 fprintf(stderr, "unexpected bNumInterfaces: %u\n",
174 desc.c.bNumInterfaces);
179 case USB_DT_INTERFACE:
180 if (desc.i.bInterfaceClass != USB_CLASS_HID
181 || desc.i.bInterfaceSubClass != 0
182 || desc.i.bInterfaceProtocol != 0) {
183 fprintf(stderr, "unexpected interface %x:%x:%x\n",
184 desc.i.bInterfaceClass, desc.i.bInterfaceSubClass,
185 desc.i.bInterfaceProtocol);
188 if (desc.i.bNumEndpoints != 2) {
189 fprintf(stderr, "unexpected bNumEndpoints: %u\n",
190 desc.i.bNumEndpoints);
196 case USB_DT_ENDPOINT:
197 if (iface < 0 || iface >= ARRAY_SIZE(dev->ifaces)) {
198 fprintf(stderr, "bad iface: %d\n", iface);
201 if (desc.e.wMaxPacketSize != 64 && desc.e.wMaxPacketSize != 32) {
202 fprintf(stderr, "iface %d, EP %02x: "
203 "unexpected wMaxPacketSize: %u\n",
204 iface, desc.e.bEndpointAddress, desc.e.wMaxPacketSize);
207 if (desc.e.bEndpointAddress & 0x80)
208 dev->ifaces[iface].ep_in = desc.e.bEndpointAddress; // & 0x7F;
210 dev->ifaces[iface].ep_out = desc.e.bEndpointAddress;
218 fprintf(stderr, "skipping desc 0x%02x\n",
219 desc.hdr.bDescriptorType);
224 /* claim interfaces */
225 for (iface = 0; iface < ARRAY_SIZE(dev->ifaces); iface++) {
226 struct usbdevfs_ioctl usbio;
228 if (dev->ifaces[iface].ep_in == -1) {
229 fprintf(stderr, "missing ep_in, iface: %d\n", iface);
232 if (dev->ifaces[iface].ep_out == -1) {
233 fprintf(stderr, "missing ep_out, iface: %d\n", iface);
237 /* disconnect default driver */
238 memset(&usbio, 0, sizeof(usbio));
240 usbio.ioctl_code = USBDEVFS_DISCONNECT;
241 ret = ioctl(fd, USBDEVFS_IOCTL, &usbio);
242 if (ret != 0 && errno != ENODATA)
243 perror("USBDEVFS_DISCONNECT");
245 ret = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &iface);
247 perror("USBDEVFS_CLAIMINTERFACE");
265 static int enable_echo(int enable)
267 const char *portname = "/dev/tty";
273 memset(&tty, 0, sizeof(tty));
275 fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
277 fprintf(stderr, "open %s: ", portname);
282 ret = tcgetattr(fd, &tty);
288 // printf("lflag: 0%o\n", tty.c_lflag);
290 tty.c_lflag |= ECHO | ICANON;
292 tty.c_lflag &= ~(ECHO | ICANON);
293 tty.c_cc[VMIN] = tty.c_cc[VTIME] = 0;
296 ret = tcsetattr(fd, TCSANOW, &tty);
311 static void signal_handler(int sig)
314 signal(sig, SIG_DFL);
320 MDBTN_DOWN = (1 << 1),
321 MDBTN_LEFT = (1 << 2),
322 MDBTN_RIGHT = (1 << 3),
326 MDBTN_START = (1 << 7),
330 MDBTN_MODE = (1 << 11),
333 #define BTN_JOY BTN_JOYSTICK
335 static const uint32_t evdev_md_default_map[KEY_CNT] = {
337 [KEY_DOWN] = MDBTN_DOWN,
338 [KEY_LEFT] = MDBTN_LEFT,
339 [KEY_RIGHT] = MDBTN_RIGHT,
346 [KEY_F] = MDBTN_MODE,
347 [KEY_ENTER] = MDBTN_START,
349 [BTN_JOY + 0] = MDBTN_A,
350 [BTN_JOY + 1] = MDBTN_B,
351 [BTN_JOY + 2] = MDBTN_C,
352 [BTN_JOY + 3] = MDBTN_START,
354 [KEY_HOME] = MDBTN_A,
355 [KEY_PAGEDOWN] = MDBTN_B,
357 [KEY_LEFTALT] = MDBTN_START,
360 static struct player_state {
361 uint32_t kc_map[KEY_CNT];
366 static int do_evdev_input(int fd, unsigned int player_i)
368 struct player_state *player;
369 struct input_event ev;
370 uint32_t mask_clear = 0;
371 uint32_t mask_set = 0;
375 ret = read(fd, &ev, sizeof(ev));
376 if (ret != sizeof(ev)) {
377 fprintf(stderr, "evdev read %d/%zd: ", ret, sizeof(ev));
382 if (player_i >= ARRAY_SIZE(players)) {
383 fprintf(stderr, "bad player: %u\n", player_i);
386 player = &players[player_i];
387 old_state = player->state;
389 if (ev.type == EV_ABS) {
391 mask_clear = MDBTN_LEFT | MDBTN_RIGHT;
393 mask_set = MDBTN_LEFT;
394 else if (ev.value > 210)
395 mask_set = MDBTN_RIGHT;
398 mask_clear = MDBTN_UP | MDBTN_DOWN;
401 else if (ev.value > 210)
402 mask_set = MDBTN_DOWN;
404 printf("abs, s %x %x\n", mask_clear, mask_set);
406 else if (ev.type == EV_KEY) {
407 if (ev.value != 0 && ev.value != 1)
410 if ((uint32_t)ev.code >= ARRAY_SIZE(player->kc_map)) {
411 fprintf(stderr, "evdev read bad key: %u\n", ev.code);
415 if (ev.value) // press?
416 mask_set = player->kc_map[ev.code];
418 mask_clear = player->kc_map[ev.code];
421 player->state &= ~mask_clear;
422 player->state |= mask_set;
424 player->dirty |= old_state != player->state;
425 return player->dirty;
428 static int open_evdev(const char *name)
430 int evdev_support = 0;
433 fd = open(name, O_RDONLY);
435 fprintf(stderr, "open %s: ", name);
439 ret = ioctl(fd, EVIOCGBIT(0, sizeof(evdev_support)),
443 if (!(evdev_support & (1 << EV_KEY))) {
444 fprintf(stderr, "%s doesn't have keys\n", name);
452 static void do_stdin_input(uint32_t *mode, int *changed)
457 ret = read(STDIN_FILENO, &c, 1);
459 perror("read stdin");
465 printf("3btn mode\n");
466 *mode = OP_MODE_3BTN;
470 printf("6btn mode\n");
471 *mode = OP_MODE_6BTN;
475 printf("teamplayer mode\n");
476 *mode = OP_MODE_TEAMPLAYER;
482 static int parse_binds(unsigned int player_i, const char *binds)
487 static int submit_urb(int fd, struct usbdevfs_urb *urb, int ep,
488 void *buf, size_t buf_size)
490 memset(urb, 0, sizeof(*urb));
491 urb->type = USBDEVFS_URB_TYPE_INTERRUPT;
494 urb->buffer_length = buf_size;
496 return ioctl(fd, USBDEVFS_SUBMITURB, urb);
506 static void usage(const char *argv0)
508 fprintf(stderr, "usage:\n%s <-e player /dev/input/node>*\n"
509 " [-b <player> <keycode=mdbtn>[,keycode=mdbtn]]\n"
511 " mdbtn: int 0-11: UDLR ABCS XYZM)\n"
512 " mode: int 0-2: 3btn 6btn teamplayer\n", argv0);
516 static void bad_arg(char *argv[], int a)
518 fprintf(stderr, "bad arg %d: '%s'\n", a, argv[a]);
522 int main(int argc, char *argv[])
524 struct teensy_dev dev;
525 struct usbdevfs_urb urb[URB_CNT];
526 struct usbdevfs_urb *reaped_urb;
527 uint32_t evdev_players[16];
529 int evdev_fd_cnt = 0;
531 int pending_urbs = 0;
534 int mode_changed = 0;
535 char buf_dbg[64 + 1];
536 struct tp_pkt pkt_in;
537 struct tp_pkt pkt_out;
538 struct timeval *timeout = NULL;
540 uint32_t mode = OP_MODE_3BTN;
545 for (i = 0; i < ARRAY_SIZE(players); i++)
546 memcpy(&players[i].kc_map, evdev_md_default_map,
547 sizeof(players[i].kc_map));
549 for (i = 1; i < argc; i++) {
550 if (argv[i][0] == '-') {
551 switch (argv[i][1] | (argv[i][2] << 8)) {
553 if (argv[++i] == NULL)
555 player = strtoul(argv[i], NULL, 0);
556 if (player >= ARRAY_SIZE(players))
559 if (argv[++i] == NULL)
561 fd = open_evdev(argv[i]);
564 if (evdev_fd_cnt >= ARRAY_SIZE(evdev_fds)) {
565 fprintf(stderr, "too many evdevs\n");
568 evdev_players[evdev_fd_cnt] = player;
569 evdev_fds[evdev_fd_cnt] = fd;
573 if (argv[++i] == NULL)
575 player = strtoul(argv[i], NULL, 0);
576 if (player >= ARRAY_SIZE(players))
579 if (argv[++i] == NULL)
581 ret = parse_binds(player, argv[i]);
586 if (argv[++i] == NULL)
588 mode = strtoul(argv[i], NULL, 0);
596 if (evdev_fd_cnt == 0)
600 signal(SIGINT, signal_handler);
604 while (!g_exit || (pending_urbs & (1 << URB_DATA_OUT)))
607 ret = find_device(&dev, 0x16C0, 0x0486);
613 printf("waiting for device...\n");
623 /* we wait first, then send commands, but if teensy
624 * is started already, it won't send anything back */
630 if (!(pending_urbs & (1 << URB_DATA_IN))) {
631 memset(&pkt_in, 0, sizeof(pkt_in));
632 ret = submit_urb(dev.fd, &urb[URB_DATA_IN], dev.ifaces[0].ep_in,
633 &pkt_in, sizeof(pkt_in));
635 perror("USBDEVFS_SUBMITURB URB_DATA_IN");
639 pending_urbs |= 1 << URB_DATA_IN;
641 if (!(pending_urbs & (1 << URB_DBG_IN))) {
642 ret = submit_urb(dev.fd, &urb[URB_DBG_IN], dev.ifaces[1].ep_in,
643 buf_dbg, sizeof(buf_dbg) - 1);
645 perror("USBDEVFS_SUBMITURB URB_DBG_IN");
649 pending_urbs |= 1 << URB_DBG_IN;
653 FD_SET(STDIN_FILENO, &rfds);
654 for (i = 0; i < evdev_fd_cnt; i++)
655 FD_SET(evdev_fds[i], &rfds);
658 FD_SET(dev.fd, &wfds);
660 ret = select(dev.fd + 1, &rfds, &wfds, NULL, timeout);
667 /* something form stdin? */
668 if (FD_ISSET(STDIN_FILENO, &rfds))
669 do_stdin_input(&mode, &mode_changed);
671 /* something from input devices? */
673 for (i = 0; i < evdev_fd_cnt; i++) {
674 if (FD_ISSET(evdev_fds[i], &rfds)) {
675 do_evdev_input(evdev_fds[i], evdev_players[i]);
680 /* collect any other input changes before starting
681 * the slow USB transfer */
682 tout.tv_sec = tout.tv_usec = 0;
687 /* something from USB? */
688 if (FD_ISSET(dev.fd, &wfds))
690 unsigned int which_urb;
693 ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb);
697 perror("USBDEVFS_REAPURB");
700 which_urb = reaped_urb - urb;
701 if (which_urb < ARRAY_SIZE(urb))
702 pending_urbs &= ~(1 << which_urb);
704 fprintf(stderr, "reaped unknown urb: %p #%u",
705 reaped_urb, which_urb);
708 if (reaped_urb != NULL && reaped_urb->status != 0) {
709 errno = -reaped_urb->status;
710 fprintf(stderr, "urb #%u: ", which_urb);
712 if (reaped_urb->status == -EILSEQ) {
713 /* this is usually a sign of disconnect.. */
718 else if (reaped_urb == &urb[URB_DATA_IN])
720 /* some request from teensy */
721 printf("rx data?\n");
723 else if (reaped_urb == &urb[URB_DATA_OUT])
726 else if (reaped_urb == &urb[URB_DBG_IN])
729 buf_dbg[reaped_urb->actual_length] = 0;
730 printf("%s", buf_dbg);
732 // continue receiving debug before sending out stuff
739 fprintf(stderr, "reaped unknown urb? %p #%zu\n",
740 reaped_urb, reaped_urb - urb);
744 /* something to send? */
745 if (pending_urbs & (1 << URB_DATA_OUT))
746 // can't do that yet - out urb still busy
750 memset(&pkt_out, 0, sizeof(pkt_out));
751 pkt_out.type = PKT_UPD_MODE;
754 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
755 &pkt_out, sizeof(pkt_out));
757 perror("USBDEVFS_SUBMITURB PKT_STREAM_ENABLE");
760 pending_urbs |= 1 << URB_DATA_OUT;
765 /* send buttons if there were any changes */
766 memset(&pkt_out, 0, sizeof(pkt_out));
767 for (i = 0; i < ARRAY_SIZE(players); i++) {
768 if (players[i].dirty)
769 pkt_out.changed_players |= 1 << i;
770 players[i].dirty = 0;
772 pkt_out.bnts[i] = players[i].state;
774 if (pkt_out.changed_players != 0) {
775 pkt_out.type = PKT_UPD_BTNS;
777 ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out,
778 &pkt_out, sizeof(pkt_out));
780 perror("USBDEVFS_SUBMITURB PKT_FIXED_STATE");
783 pending_urbs |= 1 << URB_DATA_OUT;
797 /* deal with pending URBs */
798 if (pending_urbs & (1 << URB_DATA_IN))
799 ioctl(dev.fd, USBDEVFS_DISCARDURB, &urb[URB_DATA_IN]);
800 if (pending_urbs & (1 << URB_DBG_IN))
801 ioctl(dev.fd, USBDEVFS_DISCARDURB, &urb[URB_DBG_IN]);
802 for (i = 0; i < URB_CNT; i++) {
803 if (pending_urbs & (1 << i)) {
804 ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb);
806 perror("USBDEVFS_REAPURB");
816 // vim: ts=2:sw=2:expandtab