1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
|
/*
* $Id: usb.c,v 1.57 2009/05/25 04:19:46 desrod Exp $
*
* usb.c: Interface layer to serial HotSync connections
*
* Copyright (c) 1996, 1997, D. Jeff Dionne & Kenneth Albanowski
* Copyright (c) 1999, Tilo Christ
* Copyright (c) 2005, Florent Pillet
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
* General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; if not, write to the Free Software Foundation,
* * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "pi-debug.h"
#include "pi-source.h"
#include "pi-usb.h"
#include "pi-net.h"
#include "pi-cmp.h"
#include "pi-error.h"
#include "pi-util.h"
pi_protocol_t *pi_usb_protocol_dup (pi_protocol_t *prot);
static int pi_usb_connect(pi_socket_t *ps, struct sockaddr *addr,
size_t addrlen);
static int pi_usb_bind(pi_socket_t *ps, struct sockaddr *addr,
size_t addrlen);
static int pi_usb_listen(pi_socket_t *ps, int backlog);
static int pi_usb_accept(pi_socket_t *ps, struct sockaddr *addr,
size_t *addrlen);
static int pi_usb_getsockopt(pi_socket_t *ps, int level, int option_name,
void *option_value, size_t *option_len);
static int pi_usb_setsockopt(pi_socket_t *ps, int level, int option_name,
const void *option_value, size_t *option_len);
static int pi_usb_close(pi_socket_t *ps);
static int USB_configure_visor (pi_usb_data_t *dev, u_int8_t *input_pipe, u_int8_t *output_pipe);
static int USB_configure_generic (pi_usb_data_t *dev, u_int8_t *input_pipe, u_int8_t *output_pipe);
int pi_socket_init(pi_socket_t *ps);
/* Protocol Functions */
/***********************************************************************
*
* Function: pi_usb_protocol_dup
*
* Summary: creates a new copy of a USB pi_protocol instance
*
* Parameters: pi_protocol_t*
*
* Returns: new pi_protocol_t* or NULL if operation failed
*
***********************************************************************/
pi_protocol_t *
pi_usb_protocol_dup (pi_protocol_t *prot)
{
pi_protocol_t *new_prot;
ASSERT(prot != NULL);
new_prot = (pi_protocol_t *)malloc (sizeof (pi_protocol_t));
if (new_prot != NULL) {
new_prot->level = prot->level;
new_prot->dup = prot->dup;
new_prot->free = prot->free;
new_prot->read = prot->read;
new_prot->write = prot->write;
new_prot->flush = prot->flush;
new_prot->getsockopt = prot->getsockopt;
new_prot->setsockopt = prot->setsockopt;
new_prot->data = NULL;
}
return new_prot;
}
/***********************************************************************
*
* Function: pi_usb_protocol_free
*
* Summary: frees USB pi_protocol instance
*
* Parameters: pi_protocol_t*
*
* Returns: void
*
***********************************************************************/
static void
pi_usb_protocol_free (pi_protocol_t *prot)
{
ASSERT(prot != NULL);
if (prot != NULL)
free(prot);
}
/***********************************************************************
*
* Function: pi_usb_protocol
*
* Summary: creates a new USB pi_protocol instance
*
* Parameters: pi_device_t*
*
* Returns: new pi_protocol_t* or NULL if operation failed
*
***********************************************************************/
static pi_protocol_t *
pi_usb_protocol (pi_device_t *dev)
{
pi_protocol_t *prot;
pi_usb_data_t *data;
ASSERT(dev != NULL);
data = dev->data;
prot = (pi_protocol_t *)malloc (sizeof (pi_protocol_t));
if (prot != NULL) {
prot->level = PI_LEVEL_DEV;
prot->dup = pi_usb_protocol_dup;
prot->free = pi_usb_protocol_free;
prot->read = data->impl.read;
prot->write = data->impl.write;
prot->flush = data->impl.flush;
prot->getsockopt = pi_usb_getsockopt;
prot->setsockopt = pi_usb_setsockopt;
prot->data = NULL;
}
return prot;
}
/***********************************************************************
*
* Function: pi_usb_device_free
*
* Summary: frees USB pi_device instance
*
* Parameters: pi_device_t*
*
* Returns: void
*
***********************************************************************/
static void
pi_usb_device_free (pi_device_t *dev)
{
pi_usb_data_t *data = (pi_usb_data_t *)dev->data;
ASSERT(dev != NULL);
if (data != NULL)
free(data);
if (dev != NULL)
free(dev);
}
/***********************************************************************
*
* Function: pi_usb_device
*
* Summary: creates a new USB pi_device instance
*
* Parameters: pi_device_t*
*
* Returns: new pi_device_t* or NULL if operation failed
*
***********************************************************************/
pi_device_t *
pi_usb_device (int type)
{
pi_device_t *dev;
pi_usb_data_t *data;
dev = (pi_device_t *)malloc (sizeof (pi_device_t));
if (dev != NULL) {
data = (pi_usb_data_t *)malloc (sizeof (struct pi_usb_data));
if (data == NULL) {
free(dev);
dev = NULL;
} else {
dev->free = pi_usb_device_free;
dev->protocol = pi_usb_protocol;
dev->bind = pi_usb_bind;
dev->listen = pi_usb_listen;
dev->accept = pi_usb_accept;
dev->connect = pi_usb_connect;
dev->close = pi_usb_close;
memset(data, 0, sizeof(struct pi_usb_data));
data->rate = -1;
data->establishrate = -1;
data->establishhighrate = 0;
pi_usb_impl_init (&data->impl);
dev->data = data;
}
}
return dev;
}
/***********************************************************************
*
* Function: pi_usb_connect
*
* Summary: Connect socket to a given address
*
* Parameters: pi_socket_t*, sockaddr*, socket length
*
* Returns: A negative number on error, 0 otherwise
*
***********************************************************************/
static int
pi_usb_connect(pi_socket_t *ps, struct sockaddr *addr, size_t addrlen)
{
struct pi_usb_data *data = (pi_usb_data_t *)ps->device->data;
struct pi_sockaddr *pa = (struct pi_sockaddr *) addr;
int result, timeout;
size_t size;
if (ps->type == PI_SOCK_STREAM) {
if (ps->protocol == PI_PF_SYS) {
data->establishrate = data->rate = 57600;
} else {
if (data->establishrate == -1)
get_pilot_rate(&data->establishrate, &data->establishhighrate);
/* Mandatory CMP connection rate */
data->rate = 9600;
}
} else if (ps->type == PI_SOCK_RAW) {
/* Mandatory SysPkt connection rate */
data->establishrate = data->rate = 57600;
}
result = data->impl.open(ps, pa, addrlen);
if (result < 0)
goto fail;
data->timeout = timeout = ps->accept_to * 1000;
if (data->impl.wait_for_device) {
result = data->impl.wait_for_device (ps, &timeout);
if (result <= 0)
goto fail;
}
ps->raddr = malloc(addrlen);
memcpy(ps->raddr, addr, addrlen);
ps->raddrlen = addrlen;
ps->laddr = malloc(addrlen);
memcpy(ps->laddr, addr, addrlen);
ps->laddrlen = addrlen;
if (ps->type == PI_SOCK_STREAM) {
switch (ps->cmd) {
case PI_CMD_CMP:
if ((result = cmp_tx_handshake(ps)) < 0)
goto fail;
size = sizeof(data->rate);
pi_getsockopt(ps->sd, PI_LEVEL_CMP, PI_CMP_BAUD,
&data->rate, &size);
if ((result = data->impl.changebaud(ps)) < 0)
goto fail;
break;
case PI_CMD_NET:
if ((result = net_tx_handshake(ps)) < 0)
goto fail;
break;
}
}
ps->state = PI_SOCK_CONN_INIT;
ps->command = 0;
fail:
return (result < 0) ? result : 0;
}
/***********************************************************************
*
* Function: pi_usb_bind
*
* Summary: Bind address to a local socket
*
* Parameters: pi_socket_t*, sockaddr*, socket length
*
* Returns: A negative number on error, 0 otherwise
*
***********************************************************************/
static int
pi_usb_bind(pi_socket_t *ps, struct sockaddr *addr, size_t addrlen)
{
struct pi_usb_data *data = (pi_usb_data_t *)ps->device->data;
struct pi_sockaddr *pa = (struct pi_sockaddr *) addr;
int result;
/* this rate stuff is only useful on platforms where USB-serial adapters
* connect through the USB layer, not through a serial tty
*/
if (ps->type == PI_SOCK_STREAM) {
if (data->establishrate == -1)
get_pilot_rate(&data->establishrate, &data->establishhighrate);
/* Mandatory CMP connection rate */
data->rate = 9600;
} else if (ps->type == PI_SOCK_RAW) {
/* Mandatory SysPkt connection rate */
data->establishrate = data->rate = 57600;
}
result = data->impl.open(ps, pa, addrlen);
if (result < 0)
return result;
ps->raddr = malloc(addrlen);
memcpy(ps->raddr, addr, addrlen);
ps->raddrlen = addrlen;
ps->laddr = malloc(addrlen);
memcpy(ps->laddr, addr, addrlen);
ps->laddrlen = addrlen;
return 0;
}
/***********************************************************************
*
* Function: pi_usb_listen
*
* Summary: Prepare for incoming connections
*
* Parameters: pi_socket_t*, backlog
*
* Returns: 0
*
***********************************************************************/
static int
pi_usb_listen(pi_socket_t *ps, int backlog)
{
ps->state = PI_SOCK_LISTEN;
return 0;
}
/***********************************************************************
*
* Function: pi_usb_accept
*
* Summary: Accept an incoming connection
*
* Parameters: pi_socket_t*, sockaddr*, socket length
*
* Returns: pi_socket descriptor or negative on error
*
***********************************************************************/
static int
pi_usb_accept(pi_socket_t *ps, struct sockaddr *addr, size_t *addrlen)
{
struct pi_usb_data *data = (pi_usb_data_t *)ps->device->data;
int result,
timeout;
size_t size;
data->timeout = timeout = ps->accept_to * 1000;
if (data->impl.wait_for_device) {
result = data->impl.wait_for_device (ps, &timeout);
if (result <= 0)
return result;
}
/* Wait for data */
#ifdef LINUX
/*
* Evilish kluge, some palm devices won't send the initial
* packets if we don't try and get them fairly quickly.
*
* Sending a 0 byte NET packet can get them talking again
* in some cases though.
*/
result = data->impl.poll(ps, 1000);
LOG((PI_DBG_DEV, PI_DBG_LVL_DEBUG, "%s: %d, poll result: %d.\n", __FILE__, __LINE__, result));
if (result <= 0) {
char buf[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
data->impl.write(ps, buf, sizeof (buf), 1000);
}
#endif
result = data->impl.poll(ps, timeout);
if (result <= 0) {
if (result == 0) {
return(PI_ERR_SOCK_LISTENER);
} else {
return result;
}
}
pi_socket_init(ps);
LOG((PI_DBG_DEV, PI_DBG_LVL_DEBUG, "%s: %d, prot: 0x%x, type: 0x%x, cmd: 0x%x.\n", __FILE__, __LINE__, ps->protocol, ps->type, ps->cmd));
if (ps->type == PI_SOCK_STREAM) {
struct timeval tv;
unsigned char cmp_flags;
switch (ps->cmd) {
case PI_CMD_CMP:
LOG((PI_DBG_DEV, PI_DBG_LVL_DEBUG, "%s: %d, cmp rx.\n", __FILE__, __LINE__));
if ((result = cmp_rx_handshake(ps, data->establishrate, data->establishhighrate)) < 0)
{
LOG((PI_DBG_DEV, PI_DBG_LVL_DEBUG, "usb.c: cmp_rx_handshake returned %d\n", result));
return result;
}
/* propagate the long packet format flag to both command and non-command stacks */
size = sizeof(cmp_flags);
pi_getsockopt(ps->sd, PI_LEVEL_CMP, PI_CMP_FLAGS, &cmp_flags, &size);
if (cmp_flags & CMP_FL_LONG_PACKET_SUPPORT) {
int use_long_format = 1;
size = sizeof(int);
pi_setsockopt(ps->sd, PI_LEVEL_PADP, PI_PADP_USE_LONG_FORMAT,
&use_long_format, &size);
ps->command ^= 1;
pi_setsockopt(ps->sd, PI_LEVEL_PADP, PI_PADP_USE_LONG_FORMAT,
&use_long_format, &size);
ps->command ^= 1;
}
/* reconfigure the port to match the negotiated speed */
size = sizeof(data->rate);
pi_getsockopt(ps->sd, PI_LEVEL_CMP, PI_CMP_BAUD, &data->rate, &size);
if (data->impl.changebaud != NULL) {
if ((result = data->impl.changebaud(ps)) < 0)
return result;
/* handheld needs some time to reconfigure its port */
tv.tv_sec = 0;
tv.tv_usec = 50000;
select(0, 0, 0, 0, &tv);
}
break;
case PI_CMD_NET:
LOG((PI_DBG_DEV, PI_DBG_LVL_DEBUG, "%s: %d, net rx.\n", __FILE__, __LINE__));
if ((result = net_rx_handshake(ps)) < 0)
{
LOG((PI_DBG_DEV, PI_DBG_LVL_DEBUG, "usb.c: cmp_rx_handshake returned %d\n", result));
return result;
}
break;
default:
LOG((PI_DBG_DEV, PI_DBG_LVL_ERR, "%s: %d, unknown rx %x.\n", __FILE__, __LINE__,ps->cmd));
break;
}
ps->dlprecord = 0;
}
data->timeout = 0;
ps->command = 0;
ps->state = PI_SOCK_CONN_ACCEPT;
return ps->sd;
}
/***********************************************************************
*
* Function: pi_usb_getsockopt
*
* Summary: get options on USB socket
*
* Parameters: pi_socket*, level, option name, option value, option length
*
* Returns: 0 for success, negative otherwise
*
***********************************************************************/
static int
pi_usb_getsockopt(pi_socket_t *ps, int level, int option_name,
void *option_value, size_t *option_len)
{
pi_usb_data_t *data = (pi_usb_data_t *)ps->device->data;
switch (option_name) {
case PI_DEV_RATE:
if (*option_len != sizeof (data->rate))
goto fail;
memcpy (option_value, &data->rate, sizeof (data->rate));
break;
case PI_DEV_ESTRATE:
if (*option_len != sizeof (data->establishrate))
goto fail;
memcpy (option_value, &data->establishrate,
sizeof (data->establishrate));
break;
case PI_DEV_HIGHRATE:
if (*option_len != sizeof (data->establishhighrate))
goto fail;
memcpy (option_value, &data->establishhighrate,
sizeof (data->establishhighrate));
break;
case PI_DEV_TIMEOUT:
if (*option_len != sizeof (data->timeout))
goto fail;
memcpy (option_value, &data->timeout,
sizeof (data->timeout));
break;
}
return 0;
fail:
errno = EINVAL;
return pi_set_error(ps->sd, PI_ERR_GENERIC_ARGUMENT);
}
/***********************************************************************
*
* Function: pi_usb_setsockopt
*
* Summary: set options on USB socket
*
* Parameters: pi_socket*, level, option name, option value, option length
*
* Returns: 0 for success, negative otherwise
*
***********************************************************************/
static int
pi_usb_setsockopt(pi_socket_t *ps, int level, int option_name,
const void *option_value, size_t *option_len)
{
pi_usb_data_t *data = (pi_usb_data_t *)ps->device->data;
switch (option_name) {
case PI_DEV_ESTRATE:
if (*option_len != sizeof (data->establishrate))
goto fail;
memcpy (&data->establishrate, option_value,
sizeof (data->establishrate));
break;
case PI_DEV_HIGHRATE:
if (*option_len != sizeof (data->establishhighrate))
goto fail;
memcpy (&data->establishhighrate, option_value,
sizeof (data->establishhighrate));
break;
case PI_DEV_TIMEOUT:
if (*option_len != sizeof (data->timeout))
goto fail;
memcpy (&data->timeout, option_value,
sizeof (data->timeout));
break;
}
return 0;
fail:
errno = EINVAL;
return pi_set_error(ps->sd, PI_ERR_GENERIC_ARGUMENT);
}
/***********************************************************************
*
* Function: pi_usb_close
*
* Summary: Close a connection, destroy the socket
*
* Parameters: pi_socket_t*
*
* Returns: 0
*
***********************************************************************/
static int
pi_usb_close(pi_socket_t *ps)
{
pi_usb_data_t *data = (pi_usb_data_t *)ps->device->data;
if (ps->sd != 0) {
data->impl.close (ps);
ps->sd = 0;
}
if (ps->laddr != NULL) {
free(ps->laddr);
ps->laddr = NULL;
}
if (ps->raddr != NULL) {
free(ps->raddr);
ps->raddr = NULL;
}
return 0;
}
/*
* Start of the identification and init code.
*/
/*
* This is the table of USB devices that we know about, what they are, and
* some flags.
*
* This table helps us determine whether a connecting USB device is one we'd
* like to talk to.
*
*/
pi_usb_dev_t known_devices[] = {
/* Sony */
{
.vendor = 0x054c,
.product = 0x0038,
.idstr = "Sony S S320 and other Palm OS 3.5 devices",
.flags = USB_INIT_SONY_CLIE,
},
{
.vendor = 0x054c,
.product = 0x0066,
.idstr = "Sony T, SJ series, and other Palm OS 4.0 devices",
.flags = USB_INIT_SONY_CLIE,
},
{
.vendor = 0x054c,
.product = 0x0095,
.idstr = "Sony S360",
},
{
.vendor = 0x054c,
.product = 0x000a,
.idstr = "Sony NR and other Palm OS 4.1 devices",
},
{
.vendor = 0x054c,
.product = 0x009a,
.idstr = "Sony NR70V/U",
.flags = USB_INIT_SONY_CLIE,
},
{
.vendor = 0x054c,
.product = 0x00da,
.idstr = "Sony NX",
},
{
.vendor = 0x054c,
.product = 0x00e9,
.idstr = "Sony NZ",
},
{
.vendor = 0x054c,
.product = 0x0144,
.idstr = "Sony UX",
},
{
.vendor = 0x054c,
.product = 0x0169,
.idstr = "Sony TJ",
.flags = USB_INIT_SONY_CLIE,
},
/* AlphaSmart */
{
.vendor = 0x081e,
.product = 0xdf00,
.idstr = "Alphasmart Dana",
},
/* HANDSPRING (vendor 0x082d) */
{
.vendor = 0x082d,
.product = 0x0100,
.idstr = "Visor, Treo 300",
.flags = USB_INIT_VISOR,
},
{
.vendor = 0x082d,
.product = 0x0200,
.idstr = "Treo",
},
{
.vendor = 0x082d,
.product = 0x0300,
.idstr = "Treo 600",
},
/* PalmOne, Palm Inc */
{
.vendor = 0x0830,
.product = 0x0001,
.idstr = "m500",
},
{
.vendor = 0x0830,
.product = 0x0002,
.idstr = "m505",
},
{
.vendor = 0x0830,
.product = 0x0003,
.idstr = "m515",
},
{
.vendor = 0x0830,
.product = 0x0010,
},
{
.vendor = 0x0830,
.product = 0x0011,
},
{
.vendor = 0x0830,
.product = 0x0020,
.idstr = "i705",
},
{
.vendor = 0x0830,
.product = 0x0030,
.idstr = "Tungsten|Z",
},
{
.vendor = 0x0830,
.product = 0x0031,
.idstr = "Tungsten|W",
},
{
.vendor = 0x0830,
.product = 0x0040,
.idstr = "m125",
},
{
.vendor = 0x0830,
.product = 0x0050,
.idstr = "m130",
},
{
.vendor = 0x0830,
.product = 0x0051,
},
{
.vendor = 0x0830,
.product = 0x0052,
},
{
.vendor = 0x0830,
.product = 0x0053,
},
{
.vendor = 0x0830,
.product = 0x0060,
.idstr = "Tungsten series, Zire 71",
},
{
.vendor = 0x0830,
.product = 0x0061,
.idstr = "Zire 31, 72, Z22",
.flags = USB_INIT_TAPWAVE,
},
{
.vendor = 0x0830,
.product = 0x0062,
},
{
.vendor = 0x0830,
.product = 0x0063,
},
{
.vendor = 0x0830,
.product = 0x0070,
.idstr = "Zire",
},
{
.vendor = 0x0830,
.product = 0x0071,
},
{
.vendor = 0x0830,
.product = 0x0080,
.idstr = "m100",
.flags = USB_INIT_NONE,
},
{
.vendor = 0x0830,
.product = 0x0099,
},
{
.vendor = 0x0830,
.product = 0x0100,
},
/* GARMIN */
{
.vendor = 0x091e,
.product = 0x0004,
.idstr = "IQUE 3600",
},
/* Kyocera */
{
.vendor = 0x0c88,
.product = 0x0021,
.idstr = "7135 Smartphone",
},
{
.vendor = 0x0c88,
.product = 0xa226,
.idstr = "6035 Smartphone",
},
/* Tapwave */
{
.vendor = 0x12ef,
.product = 0x0100,
.idstr = "Zodiac, Zodiac2",
.flags = USB_INIT_TAPWAVE,
},
/* ACEECA */
{
.vendor = 0x4766,
.product = 0x0001,
.idstr = "MEZ1000",
},
/* Samsung */
{
.vendor = 0x04e8,
.product = 0x8001,
.idstr = "i330",
},
};
int
USB_check_device (pi_usb_data_t *dev, u_int16_t vendor, u_int16_t product)
{
unsigned int i;
for (i = 0; i < (sizeof (known_devices) / sizeof (known_devices[0])); i++) {
if (known_devices[i].vendor == vendor) {
if (!known_devices[i].product ||
known_devices[i].product == product) {
dev->dev.flags |= known_devices[i].flags;
return 0;
}
}
}
return -1;
}
/*
* Device configuration, ugh.
*/
int
USB_configure_device (pi_usb_data_t *dev, u_int8_t *input_pipe, u_int8_t *output_pipe)
{
int ret;
u_int32_t flags = dev->dev.flags;
*input_pipe = 0xff;
*output_pipe = 0xff;
/*
* Device specific magic incantations
*
* Many devices agree on talking only if you say the "magic" incantation first.
* Usually, it's a control request or a sequence of control requests
*
*/
if (flags & USB_INIT_NONE)
return 0;
if (flags & USB_INIT_VISOR)
ret = USB_configure_visor (dev, input_pipe, output_pipe);
else if (flags & USB_INIT_SONY_CLIE) {
/* according to linux code, PEG S-300 awaits these two requests */
/* USB_REQ_GET_CONFIGURATION */
ret = dev->impl.control_request (dev, 0x80, 0x08, 0, 0, NULL, 1, 0);
if (ret < 0) {
LOG((PI_DBG_DEV, PI_DBG_LVL_ERR, "usb: Sony USB_REQ_GET_CONFIGURATION failed (err=%08x)\n", ret));
}
/* USB_REQ_GET_INTERFACE */
ret = dev->impl.control_request (dev, 0x80, 0x0A, 0, 0, NULL, 1, 0);
if (ret < 0) {
LOG((PI_DBG_DEV, PI_DBG_LVL_ERR, "usb: Sony USB_REQ_GET_INTERFACE failed (err=%08x)\n", ret));
}
} else {
/* other devices will either accept or deny this generic call */
ret = USB_configure_generic (dev, input_pipe, output_pipe);
if (ret < 0) {
return -1;
}
}
/* query bytes available. Not that we really care,
but most devices expect to receive this before
they agree on talking to us. */
if (!(flags & USB_INIT_TAPWAVE)) {
unsigned char ba[2] = { 0 };
ret = dev->impl.control_request (dev, 0xc2, GENERIC_REQUEST_BYTES_AVAILABLE, 0, 0, &ba[0], 2, 0);
if (ret < 0) {
LOG((PI_DBG_DEV, PI_DBG_LVL_ERR, "usb: GENERIC_REQUEST_BYTES_AVAILABLE failed (err=%08x)\n", ret));
/* configuration have to fail to skip this device - or LifeDrive(?) devices will hang */
return -1;
}
LOG((PI_DBG_DEV, PI_DBG_LVL_DEBUG, "GENERIC_REQUEST_BYTES_AVAILABLE returns 0x%02x%02x\n", ba[0], ba[1]));
}
return 0;
}
static int
USB_configure_visor (pi_usb_data_t *dev, u_int8_t *input_pipe, u_int8_t *output_pipe)
{
int i, ret;
visor_connection_info_t ci;
ret = dev->impl.control_request (dev, 0xc2, VISOR_GET_CONNECTION_INFORMATION, 0, 0, &ci, sizeof (ci), 0);
if (ret < 0) {
LOG((PI_DBG_DEV, PI_DBG_LVL_ERR, "usb: VISOR_GET_CONNECTION_INFORMATION failed (err=%08x)\n", ret));
} else {
LOG((PI_DBG_DEV, PI_DBG_LVL_DEBUG, "usb: VISOR_GET_CONNECTION_INFORMATION, num_ports=%d\n", ci.num_ports));
if (ci.num_ports > 2)
ci.num_ports = 2;
for (i=0; i < ci.num_ports; i++)
{
char *function_str;
switch (ci.connections[i].port_function_id)
{
case VISOR_FUNCTION_GENERIC:
function_str="GENERIC";
break;
case VISOR_FUNCTION_DEBUGGER:
function_str="DEBUGGER";
break;
case VISOR_FUNCTION_HOTSYNC:
function_str="HOTSYNC";
break;
case VISOR_FUNCTION_CONSOLE:
function_str="CONSOLE";
break;
case VISOR_FUNCTION_REMOTE_FILE_SYS:
function_str="REMOTE_FILE_SYSTEM";
break;
default:
function_str="UNKNOWN";
break;
}
LOG((PI_DBG_DEV, PI_DBG_LVL_DEBUG, "\t[%d] port_function_id=0x%02x (%s)\n", i,
ci.connections[i].port_function_id,
function_str));
LOG((PI_DBG_DEV, PI_DBG_LVL_DEBUG, "\t[%d] port=%d\n", i,
ci.connections[i].port));
}
}
return ret;
}
static int
USB_configure_generic (pi_usb_data_t *dev, u_int8_t *input_pipe, u_int8_t *output_pipe)
{
int i, ret;
int hotsync = 0;
palm_ext_connection_info_t ci;
u_int32_t flags = dev->dev.flags;
ret = dev->impl.control_request (dev, 0xc2, PALM_GET_EXT_CONNECTION_INFORMATION, 0, 0, &ci, sizeof (ci), 0);
if (ret < 0) {
LOG((PI_DBG_DEV, PI_DBG_LVL_ERR, "usb: PALM_GET_EXT_CONNECTION_INFORMATION failed (err=%08x)\n", ret));
} else {
LOG((PI_DBG_DEV, PI_DBG_LVL_DEBUG, "usb: PALM_GET_EXT_CONNECTION_INFORMATION, num_ports=%d, endpoint_numbers_different=%d\n",
ci.num_ports,
ci.endpoint_numbers_different));
for (i=0; i < ci.num_ports; i++) {
LOG((PI_DBG_DEV, PI_DBG_LVL_DEBUG, "\t[%d] port_function_id='%c%c%c%c'\n", i,
ci.connections[i].port_function_id[0],
ci.connections[i].port_function_id[1],
ci.connections[i].port_function_id[2],
ci.connections[i].port_function_id[3]));
LOG((PI_DBG_DEV, PI_DBG_LVL_DEBUG, "\t[%d] port=%d\n", i,
ci.connections[i].port));
LOG((PI_DBG_DEV, PI_DBG_LVL_DEBUG, "\t[%d] endpoint_info=%d\n", i,
ci.connections[i].endpoint_info));
if (!memcmp(ci.connections[i].port_function_id, "cnys", 4)) {
/* found hotsync port */
hotsync = 1;
/* 'sync': we found the pipes to use for synchronization force
find_interfaces to select this one rather than another one */
if (ci.endpoint_numbers_different) {
if (input_pipe)
*input_pipe = ci.connections[i].endpoint_info >> 4;
if (output_pipe)
*output_pipe = ci.connections[i].endpoint_info & 0x0f;
} else {
if (input_pipe)
*input_pipe = ci.connections[i].port;
if (output_pipe)
*output_pipe = ci.connections[i].port;
}
}
}
if (!hotsync) {
LOG((PI_DBG_DEV, PI_DBG_LVL_ERR, "usb: PALM_GET_EXT_CONNECTION_INFORMATION - no hotsync port found.\n", ret));
return -1;
}
}
if (flags & USB_INIT_TAPWAVE) {
/*
* Tapwave: for Zodiac, the TwUSBD.sys driver on Windows sends
* the ext-connection-info packet two additional times.
*/
ret = dev->impl.control_request (dev, 0xc2, PALM_GET_EXT_CONNECTION_INFORMATION, 0, 0, &ci, sizeof (ci), 0);
ret = dev->impl.control_request (dev, 0xc2, PALM_GET_EXT_CONNECTION_INFORMATION, 0, 0, &ci, sizeof (ci), 0);
}
return ret;
}
/* vi: set ts=8 sw=4 sts=4 noexpandtab: cin */
/* ex: set tabstop=4 expandtab: */
/* Local Variables: */
/* indent-tabs-mode: t */
/* c-basic-offset: 8 */
/* End: */
|