diff --git a/COPYING b/COPYING index 5ab7695..3a0a575 100644 --- a/COPYING +++ b/COPYING @@ -1,3 +1,13 @@ +NOTE: This prerelease version of libfprint is mostly LGPL-licensed (as in the +text below) but also includes code from libthinkfinger which is licensed under +the GPL. +This libfprint release is therefore licensed under the LGPL *and* the GPL. +The GPL terms can be found in the COPYING.GPL file. + +At release time, I will contact libthinkfinger authors and see if they will +be happy to relicense. I expect they will. + + GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999 diff --git a/COPYING.GPL b/COPYING.GPL new file mode 100644 index 0000000..3912109 --- /dev/null +++ b/COPYING.GPL @@ -0,0 +1,340 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/README b/README index e69de29..65620a3 100644 --- a/README +++ b/README @@ -0,0 +1,11 @@ +libfprint +http://www.reactivated.net/fprint + +libfprint is primarily licensed under the GNU LGPL. HOWEVER, THIS PRERELEASE +VERSION INCLUDES GPL CODE FROM LIBTHINKFINGER, therefore distribution is +subject to both the terms of the LGPL (see COPYING) *and* the GPL +(see COPYING.GPL). + +At release time, I will contact libthinkfinger authors and see if they will +be happy to relicense. I expect they will. + diff --git a/libfprint/drivers/upekts.c b/libfprint/drivers/upekts.c index fdf97a3..c83e200 100644 --- a/libfprint/drivers/upekts.c +++ b/libfprint/drivers/upekts.c @@ -2,31 +2,654 @@ * UPEK TouchStrip driver for libfprint * Copyright (C) 2007 Daniel Drake * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. + * Based in part on libthinkfinger: + * Copyright (C) 2006-2007 Timo Hoenig + * Copyright (C) 2006 Pavel Machek * - * This library is distributed in the hope that it will be useful, + * LGPL CRC code copied from GStreamer-0.10.10: + * Copyright (C) <1999> Erik Walthinsen + * Copyright (C) 2004,2006 Thomas Vander Stichele + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 - * Lesser General Public License for more details. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. * - * You should have received a copy of the GNU Lesser 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 + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#define FP_COMPONENT "upekts" + +#include +#include + +#include + #include +#define EP_IN (1 | USB_ENDPOINT_IN) +#define EP_OUT (2 | USB_ENDPOINT_OUT) +#define TIMEOUT 5000 + +struct upekts_dev { + uint8_t seq; +}; + +static const uint16_t crc_table[256] = { + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, + 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, + 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, + 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, + 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, + 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, + 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, + 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, + 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, + 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, + 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, + 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, + 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, + 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, + 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, + 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, + 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, + 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, + 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, + 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, + 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, + 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, + 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, + 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, + 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, + 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, + 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, + 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, + 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, + 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 +}; + +static uint16_t udf_crc(unsigned char *buffer, size_t size) +{ + uint16_t crc = 0; + while (size--) + crc = (uint16_t) ((crc << 8) ^ + crc_table[((crc >> 8) & 0x00ff) ^ *buffer++]); + return crc; +} + +/* + * MESSAGE FORMAT + * + * Messages to and from the device have the same format. + * + * Byte-wise: + * 'C' 'i' 'a' 'o' A B L C1 C2 + * + * Ciao prefixes all messages. L is the length of the data, is L bytes + * long. C1 and C2 are the UDF-CRC16 for the whole message minus the Ciao + * prefix. + * + * When the device wants to command the driver to do something, it sends + * a message where B=0 and A!=0. The A value indicates the type of command. + * If the system is expected to respond to the command, it sends a message back + * with B=0 and A incremented. + * + * When the driver sends a command to the device, A=0 and B is used as a + * sequence counter. It starts at 0, increments by 0x10 on each command, and + * wraps around. + * After each command is sent, the device responds with another message + * indicating completion of the command including any data that was requested. + * This message has the same A and B values. + * + * When the driver is sending commands as above, and when the device is + * responding, the seems to follow this structure: + * + * 28 L 0 0 0 S + * + * Where the length of is L-3, and S is some kind of subcommand + * code. In the device's response to a command, the subcommand code will be + * unchanged. + * + * After deducing and documenting the above, I found a few places where the + * above doesn't hold true. Those are marked with FIXME's below. + */ + +#define CMD_SEQ_INCREMENT 0x10 + +static int send_cmd(struct fp_dev *dev, unsigned char seq_a, + unsigned char seq_b, unsigned char *data, uint8_t len) +{ + int r; + uint16_t crc; + + /* 9 bytes extra for: 4 byte 'Ciao', 1 byte A, 1 byte B, 1 byte length, + * 2 byte CRC */ + size_t urblen = len + 9; + unsigned char *buf; + + if (!data && len > 0) { + fp_err("len>0 but no data?"); + return -EINVAL; + } + + buf = g_malloc(urblen); + + /* Write header */ + strncpy(buf, "Ciao", 4); + buf[4] = seq_a; + buf[5] = seq_b; + buf[6] = len; + + /* Copy data */ + if (data) + memcpy(buf + 7, data, len); + + /* Append CRC */ + crc = cpu_to_be16(udf_crc(buf + 4, urblen - 6)); + buf[urblen - 2] = crc >> 8; + buf[urblen - 1] = crc & 0xff; + + r = usb_bulk_write(dev->udev, EP_OUT, buf, urblen, TIMEOUT); + g_free(buf); + if (r < 0) { + fp_err("cmd write failed, code %d", r); + return r; + } else if ((unsigned int) r < urblen) { + fp_err("cmd write too short (%d/%d)", r, urblen); + return -EIO; + } + + return 0; +} + +static int send_cmd28(struct fp_dev *dev, unsigned char subcmd, + unsigned char *data, uint8_t innerlen) +{ + size_t len = innerlen + 6; + unsigned char *buf = g_malloc0(len); + struct upekts_dev *upekdev = dev->priv; + uint8_t seq = upekdev->seq + CMD_SEQ_INCREMENT; + int r; + + fp_dbg("seq=%02x subcmd=%02x with %d bytes of data", seq, subcmd, innerlen); + + buf[0] = 0x28; + buf[1] = innerlen + 3; + buf[5] = subcmd; + memcpy(buf + 6, data, innerlen); + + r = send_cmd(dev, 0, seq, buf, len); + if (r == 0) + upekdev->seq = seq; + + g_free(buf); + return r; +} + +static int send_cmdresponse(struct fp_dev *dev, unsigned char seq, + unsigned char *data, uint8_t len) +{ + fp_dbg("seq=%02x len=%d", seq, len); + return send_cmd(dev, seq, 0, data, len); +} + +static unsigned char *__read_msg(struct fp_dev *dev, size_t *data_len) +{ +#define MSG_READ_BUF_SIZE 0x40 +#define MAX_DATA_IN_READ_BUF (MSG_READ_BUF_SIZE - 9) + unsigned char *buf = g_malloc(MSG_READ_BUF_SIZE); + size_t buf_size = MSG_READ_BUF_SIZE; + uint16_t computed_crc, msg_crc; + uint8_t len; + int r; + + r = usb_bulk_read(dev->udev, EP_IN, buf, buf_size, TIMEOUT); + if (r < 0) { + fp_err("msg read failed, code %d", r); + goto err; + } else if (r < 9) { + fp_err("msg read too short (%d/%d)", r, buf_size); + goto err; + } + + if (strncmp(buf, "Ciao", 4) != 0) { + fp_err("no Ciao for you!!"); + goto err; + } + + len = buf[6]; + + if (r != MSG_READ_BUF_SIZE && (len + 9) < r) { + /* Check that the length claimed inside the message is in line with + * the amount of data that was transferred over USB. */ + fp_err("msg didn't include enough data, expected=%d recv=%d", + len + 9, r); + goto err; + } + + /* We use a 64 byte buffer for reading messages. However, sometimes + * messages are longer, in which case we have to do another USB bulk read + * to read the remainder. This is handled below. */ + if (len > MAX_DATA_IN_READ_BUF) { + int needed = len - MAX_DATA_IN_READ_BUF; + fp_dbg("didn't fit in buffer, need to extend by %d bytes", needed); + buf = g_realloc((gpointer) buf, MSG_READ_BUF_SIZE + needed); + r = usb_bulk_read(dev->udev, EP_IN, buf + MSG_READ_BUF_SIZE, needed, + TIMEOUT); + if (r < 0) { + fp_err("extended msg read failed, code %d", r); + goto err; + } else if (r < needed) { + fp_err("extended msg short read (%d/%d)", r, needed); + goto err; + } + buf_size += needed; + } + + computed_crc = udf_crc(buf + 4, len + 3); + msg_crc = le16_to_cpu((buf[len + 8] << 8) | buf[len + 7]); + if (computed_crc != msg_crc) { + fp_err("CRC failed, got %04x expected %04x", msg_crc, computed_crc); + goto err; + } + + *data_len = buf_size; + return buf; +err: + g_free(buf); + return NULL; +} + +enum read_msg_status { + READ_MSG_ERROR = -1, + READ_MSG_CMD = 1, + READ_MSG_RESPONSE = 2, +}; + +static enum read_msg_status read_msg(struct fp_dev *dev, uint8_t *seq, + unsigned char *subcmd, unsigned char **data, size_t *data_len) +{ +#define MSG_READ_BUF_SIZE 0x40 +#define MAX_DATA_IN_READ_BUF (MSG_READ_BUF_SIZE - 9) + unsigned char *buf; + size_t buf_size; + unsigned char code_a; + unsigned char code_b; + uint8_t len; + enum read_msg_status ret = READ_MSG_ERROR; + +retry: + buf = __read_msg(dev, &buf_size); + if (!buf) + return READ_MSG_ERROR; + + code_a = buf[4]; + code_b = buf[5]; + len = buf[6]; + fp_dbg("A=%02x B=%02x len=%d", code_a, code_b, len); + + if (code_a && !code_b) { + /* device sends command to driver */ + fp_dbg("cmd %x from device to driver", code_a); + + if (code_a == 0x08) { + fp_dbg("device busy, send busy-ack"); + send_cmdresponse(dev, 0x09, NULL, 0); + g_free(buf); + goto retry; + } + + if (seq) + *seq = code_a; + if (data) { + if (len > 0) { + unsigned char *tmp = g_malloc(len); + memcpy(tmp, buf + 7, len); + *data = tmp; + } + *data_len = len; + } + ret = READ_MSG_CMD; + } else if (!code_a) { + /* device sends response to a previously executed command */ + unsigned char *innerbuf = buf + 7; + unsigned char _subcmd; + uint8_t innerlen; + + if (len < 6) { + fp_err("cmd response too short (%d)", len); + goto out; + } + if (innerbuf[0] != 0x28) { + fp_err("cmd response without 28 byte?"); + goto out; + } + if (innerbuf[2] || innerbuf[3] || innerbuf[4]) { + fp_err("non-zero bytes in cmd response"); + goto out; + } + + innerlen = innerbuf[1] - 3; + _subcmd = innerbuf[5]; + fp_dbg("device responds to subcmd %x with %d bytes", _subcmd, innerlen); + if (seq) + *seq = code_b; + if (subcmd) + *subcmd = _subcmd; + if (data) { + if (innerlen > 0) { + unsigned char *tmp = g_malloc(innerlen); + memcpy(tmp, innerbuf + 6, innerlen); + *data = tmp; + } + *data_len = innerlen; + } + ret = READ_MSG_RESPONSE; + } else { + fp_err("don't know how to handle this message"); + } + +out: + g_free(buf); + return ret; +} + +static int read_msg28(struct fp_dev *dev, unsigned char subcmd, + unsigned char **data, size_t *data_len) +{ + struct upekts_dev *upekdev = dev->priv; + uint8_t _seq; + unsigned char _subcmd; + enum read_msg_status msgstat; + + msgstat = read_msg(dev, &_seq, &_subcmd, data, data_len); + if (msgstat != READ_MSG_RESPONSE) { + fp_err("expected response, got %d seq=%x", msgstat, _seq); + return -EPROTO; + } + if (_subcmd != subcmd) { + fp_warn("expected response to subcmd %02x, got response to %02x", + subcmd, _subcmd); + return -EPROTO; + } + if (_seq != upekdev->seq) { + fp_err("expected response to cmd seq=%02x, got response to %02x", + upekdev->seq, _seq); + return -EPROTO; + } + + return 0; +} + +static const unsigned char init_resp03[] = { + 0x01, 0x00, 0xe8, 0x03, 0x00, 0x00, 0xff, 0x07 +}; +static const unsigned char init28_08[] = { + 0x04, 0x83, 0x00, 0x2c, 0x22, 0x23, 0x97, 0xc9, 0xa7, 0x15, 0xa0, 0x8a, + 0xab, 0x3c, 0xd0, 0xbf, 0xdb, 0xf3, 0x92, 0x6f, 0xae, 0x3b, 0x1e, 0x44, + 0xc4 +}; +static const unsigned char init28_0c[] = { + 0x04, 0x03, 0x00, 0x00, 0x00 +}; +static const unsigned char init28_0b[] = { + 0x04, 0x03, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xf4, 0x01, 0x00, 0x00, 0x64, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x0a, + 0x00, 0x64, 0x00, 0xf4, 0x01, 0x32, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00 +}; + +static int dev_init(struct fp_dev *dev) +{ + struct upekts_dev *upekdev; + unsigned char dummy = 0x10; + enum read_msg_status msgstat; + uint8_t seq; + int r; + + r = usb_claim_interface(dev->udev, 0); + if (r < 0) + return r; + + r = usb_control_msg(dev->udev, USB_TYPE_VENDOR | USB_RECIP_DEVICE, + 0x0c, 0x100, 0x400, &dummy, sizeof(dummy), TIMEOUT); + if (r < 0) + return r; + + upekdev = g_malloc(sizeof(*upekdev)); + upekdev->seq = 0xf0; /* incremented to 0x00 before first cmd */ + dev->priv = upekdev; + dev->nr_enroll_stages = 3; + + msgstat = read_msg(dev, &seq, NULL, NULL, NULL); + if (msgstat != READ_MSG_CMD) { + fp_err("expected command, got %d seq=%x", msgstat, seq); + goto err; + } + if (seq != 3) { + fp_err("expected seq=3, got %x", seq); + goto err; + } + + r = send_cmdresponse(dev, ++seq, (unsigned char *) init_resp03, + sizeof(init_resp03)); + if (r < 0) + goto err; + + msgstat = read_msg(dev, &seq, NULL, NULL, NULL); + if (msgstat != READ_MSG_CMD) { + fp_err("expected command, got %d seq=%x", msgstat, seq); + goto err; + } + if (seq != 5) { + fp_err("expected seq=5, got %x", seq); + goto err; + } + + seq = 0; + dummy = 0x04; + r = send_cmd28(dev, 0x06, &dummy, 1); + if (r < 0) + goto err; + if (read_msg28(dev, 0x06, NULL, NULL) < 0) + goto err; + + dummy = 0x04; + r = send_cmd28(dev, 0x07, &dummy, 1); + if (r < 0) + goto err; + if (read_msg28(dev, 0x07, NULL, NULL) < 0) + goto err; + + r = send_cmd28(dev, 0x08, (unsigned char *) init28_08, + sizeof(init28_08)); + if (r < 0) + goto err; + if (read_msg28(dev, 0x08, NULL, NULL) < 0) + goto err; + + r = send_cmd28(dev, 0x0c, (unsigned char *) init28_0c, + sizeof(init28_0c)); + if (r < 0) + goto err; + if (read_msg28(dev, 0x0c, NULL, NULL) < 0) + goto err; + + r = send_cmd28(dev, 0x0b, (unsigned char *) init28_0b, + sizeof(init28_0b)); + if (r < 0) + goto err; + if (read_msg28(dev, 0x0b, NULL, NULL) < 0) + goto err; + + return 0; +err: + g_free(upekdev); + return -EPROTO; +} + +static void dev_exit(struct fp_dev *dev) +{ + unsigned char dummy = 0; + + /* FIXME: either i've misunderstood the message system or this is illegal + * here, since we arent responding to anything. */ + send_cmdresponse(dev, 0x07, &dummy, 1); + + // FIXME should read msg A=01 + + g_free(dev->priv); +} + +static const unsigned char enroll_init[] = { + 0x02, 0xc0, 0xd4, 0x01, 0x00, 0x04, 0x00, 0x08 +}; +static const unsigned char scan_comp[] = { + 0x12, 0xff, 0xff, 0xff, 0xff /* scan completion, prefixes print data */ +}; + +static enum fp_enroll_status enroll(struct fp_dev *dev, gboolean initial, + int stage, struct fp_print_data **_data) +{ + unsigned char poll_data[] = { 0x30, 0x01 }; + unsigned char *data; + size_t data_len; + int r; + int result = 0; + int passed = 0; + + if (initial) { + r = send_cmd28(dev, 0x02, (unsigned char *) enroll_init, + sizeof(enroll_init)); + if (r < 0) + return FP_ENROLL_FAIL; + /* FIXME: protocol misunderstanding here. device receives response + * to subcmd 0 after submitting subcmd 2? */ + if (read_msg28(dev, 0x00, NULL, NULL) < 0) + return FP_ENROLL_FAIL; + } + + while (!result) { + unsigned char status; + + r = send_cmd28(dev, 0x00, poll_data, sizeof(poll_data)); + if (r < 0) + return r; + if (read_msg28(dev, 0x00, &data, &data_len) < 0) + return FP_ENROLL_FAIL; + + if (data_len != 14) { + fp_err("received 3001 poll response of %d bytes?", data_len); + g_free(data); + return FP_ENROLL_FAIL; + } + + status = data[5]; + fp_dbg("poll result = %02x", status); + + /* These codes indicate that we're waiting for a finger scan, so poll + * again */ + switch (status) { + case 0x0c: + case 0x0d: + case 0x0e: + /* no news, poll again */ + if (passed) + result = FP_ENROLL_PASS; + break; + case 0x1c: /* FIXME what does this one mean? */ + case 0x0f: /* scan taking too long, remove finger and try again */ + result = FP_ENROLL_RETRY; + break; + case 0x1e: /* swipe too short */ + result = FP_ENROLL_RETRY_TOO_SHORT; + break; + case 0x24: /* finger not centered */ + result = FP_ENROLL_RETRY_CENTER_FINGER; + break; + case 0x20: + passed = 1; + break; + case 0x00: + if (passed) + result = FP_ENROLL_COMPLETE; + break; + default: + fp_err("unrecognised scan status code %02x", status); + result = FP_ENROLL_FAIL; + break; + } + + if (result != FP_ENROLL_COMPLETE) + g_free(data); + } + + /* FIXME: need to extend protocol research to handle the case when + * enrolment fails, e.g. you scan a different finger on each stage */ + + if (result == FP_ENROLL_COMPLETE) { + struct fp_print_data *fdata; + unsigned char *buf; + + r = send_cmd28(dev, 0x00, poll_data, sizeof(poll_data)); + if (r < 0) + return r; + /* FIXME: protocol misunderstanding here. device receives response + * to subcmd 0 after submitting subcmd 2? */ + if (read_msg28(dev, 0x02, &data, &data_len) < 0) + return FP_ENROLL_FAIL; + + if (data_len < sizeof(scan_comp)) { + fp_err("fingerprint data too short (%d bytes)", data_len); + return FP_ENROLL_FAIL; + } + if (memcmp(data, scan_comp, sizeof(scan_comp)) != 0) { + fp_err("unrecognised data prefix %x %x %x %x %x", + data[0], data[1], data[2], data[3], data[4]); + return FP_ENROLL_FAIL; + } + if (!_data) { + fp_err("complete but no data storage!"); + return FP_ENROLL_COMPLETE; + } + + fdata = fpi_print_data_new(dev, data_len - sizeof(scan_comp)); + buf = fpi_print_data_get_buffer(fdata); + memcpy(buf, data + sizeof(scan_comp), data_len - sizeof(scan_comp)); + *_data = fdata; + g_free(data); + } + + return result; +} + + static const struct usb_id id_table[] = { { .vendor = 0x0483, .product = 0x2016 }, - { 0 }, /* terminating entry */ + { 0, 0, 0, }, /* terminating entry */ }; const struct fp_driver upekts_driver = { - .name = "upekts", + .name = FP_COMPONENT, .full_name = "UPEK TouchStrip", .id_table = id_table, + .init = dev_init, + .exit = dev_exit, + .enroll = enroll, };