Initial commit of em100 utility
Change-Id: I747562f9a29f1c6b1e679a2d14645a77a9c81457
Signed-off-by: Stefan Reinauer <reinauer@google.com>
diff --git a/COPYING b/COPYING
new file mode 100644
index 0000000..d511905
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,339 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
+ 51 Franklin Street, 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 Lesser 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.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ 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 Street, 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.
+
+ <signature of Ty Coon>, 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 Lesser General
+Public License instead of this License.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..f32f072
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,37 @@
+#
+# Copyright (C) 2012 The Chromium OS Authors.
+#
+# 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; version 2 of the License.
+#
+# 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
+#
+
+CFLAGS=-O2 -Wall -g
+CC?=gcc
+
+em100: em100.c em100pro_chips.h
+ $(CC) $(CFLAGS) -o $@ $< $(shell pkg-config --cflags --libs libusb-1.0)
+
+makechips: makechips.c
+ $(CC) $(CFLAGS) -o $@ $<
+
+em100pro_chips.h: makechips
+ ./makechips.sh
+ ./makechips configs/*.cfg > $@
+
+clean:
+ rm -f em100
+
+distclean: clean
+ rm -rf configs makechips em100pro_chips.h
+
+.PHONY: clean distclean
diff --git a/README b/README
new file mode 100644
index 0000000..448b934
--- /dev/null
+++ b/README
@@ -0,0 +1,10 @@
+A simple utility to control a Dediprog EM100 from Linux.
+
+Usage:
+-c CHIP|--set CHIP: select CHIP emulation
+-d[ownload] FILE: upload FILE into em100
+-r|--start: em100 shall run
+-s|--stop: em100 shall stop
+-v|--verify: verify EM100 content matches the file
+-h|--help: this help text
+
diff --git a/em100.c b/em100.c
new file mode 100644
index 0000000..4609be1
--- /dev/null
+++ b/em100.c
@@ -0,0 +1,425 @@
+/*
+ * Copyright (C) 2012 The Chromium OS Authors.
+ *
+ * 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; version 2 of the License.
+ *
+ * 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
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <getopt.h>
+#include <libusb.h>
+
+/* SPI flash chips parameters definition */
+#include "em100pro_chips.h"
+
+struct em100 {
+ libusb_device_handle *dev;
+ libusb_context *ctx;
+};
+
+int check_status(libusb_device_handle *dev);
+int em100_attach(struct em100 *em100)
+{
+ libusb_device **devs;
+ libusb_device_handle *dev;
+ libusb_context *ctx = NULL;
+
+ if (libusb_init(&ctx) < 0) {
+ printf("Could not init libusb.\n");
+ return 0;
+ }
+
+ libusb_set_debug(ctx, 3);
+
+ if (libusb_get_device_list(ctx, &devs) < 0) {
+ printf("Could not find USB devices.\n");
+ return 0;
+ }
+
+ dev = libusb_open_device_with_vid_pid(ctx, 0x4b4, 0x1235);
+ if (!dev) {
+ printf("Could not find em100pro.\n");
+ return 0;
+ }
+
+ libusb_free_device_list(devs, 1);
+
+ if (libusb_kernel_driver_active(dev, 0) == 1) {
+ if (libusb_detach_kernel_driver(dev, 0) != 0) {
+ printf("Could not detach kernel driver.\n");
+ return 0;
+ }
+ }
+
+ if (libusb_claim_interface(dev, 0) < 0) {
+ printf("Could not claim interface.\n");
+ return 0;
+ }
+
+ if (!check_status(dev)) {
+ printf("Device status unknown.\n");
+ return 0;
+ }
+
+ em100->dev = dev;
+ em100->ctx = ctx;
+ return 1;
+}
+
+int em100_detach(struct em100 *em100)
+{
+ if (libusb_release_interface(em100->dev, 0) != 0) {
+ printf("releasing interface failed.\n");
+ return 1;
+ }
+
+ libusb_close(em100->dev);
+ libusb_exit(em100->ctx);
+
+ return 0;
+}
+
+int send_cmd(libusb_device_handle *dev, void *data)
+{
+ int actual;
+ int length = 16; /* haven't seen any other length yet */
+ libusb_bulk_transfer(dev, 1 | LIBUSB_ENDPOINT_OUT, data, length, &actual, 0);
+ return (actual == length);
+}
+
+int get_response(libusb_device_handle *dev, void *data, int length)
+{
+ int actual;
+ libusb_bulk_transfer(dev, 2 | LIBUSB_ENDPOINT_IN, data, length, &actual, 0);
+ return actual;
+}
+
+int set_chip_type(libusb_device_handle *dev, const chipdesc *desc)
+{
+ unsigned char cmd[16];
+ /* result counts unsuccessful send_cmd()s.
+ * These are then converted in a boolean success value
+ */
+ int result = 0;
+ int i = 0;
+ memset(cmd, 0, 16);
+ cmd[0] = 0x23;
+
+ cmd[1] = 0x32;
+ cmd[2] = desc->x23[i++];
+ cmd[3] = desc->x23[i++];
+ result += !send_cmd(dev, cmd);
+
+ cmd[1] = 0x3a;
+ cmd[2] = desc->x23[i++];
+ cmd[3] = desc->x23[i++];
+ result += !send_cmd(dev, cmd);
+
+ cmd[1] = 0x38;
+ cmd[2] = desc->x23[i++];
+ cmd[3] = desc->x23[i++];
+ result += !send_cmd(dev, cmd);
+
+ cmd[1] = 0x40;
+ cmd[2] = desc->x23[i++];
+ cmd[3] = desc->x23[i++];
+ result += !send_cmd(dev, cmd);
+
+ cmd[1] = 0x42;
+ cmd[2] = desc->x23[i++];
+ cmd[3] = desc->x23[i++];
+ result += !send_cmd(dev, cmd);
+
+ cmd[1] = 0x44;
+ cmd[2] = desc->x23[i++];
+ cmd[3] = desc->x23[i++];
+ result += !send_cmd(dev, cmd);
+
+ cmd[1] = 0x46;
+ cmd[2] = desc->x23[i++];
+ cmd[3] = desc->x23[i++];
+ result += !send_cmd(dev, cmd);
+
+ cmd[1] = 0x48;
+ cmd[2] = desc->x23[i++];
+ cmd[3] = desc->x23[i++];
+ result += !send_cmd(dev, cmd);
+
+ i = 0;
+ cmd[0] = 0x11;
+
+ cmd[1] = 0x02;
+ cmd[2] = desc->x11[i++];
+ cmd[3] = desc->x11[i++];
+ result += !send_cmd(dev, cmd);
+
+ cmd[1] = 0x03;
+ cmd[2] = desc->x11[i++];
+ cmd[3] = desc->x11[i++];
+ result += !send_cmd(dev, cmd);
+
+ cmd[1] = 0x04;
+ cmd[2] = desc->x11[i++];
+ cmd[3] = desc->x11[i++];
+ result += !send_cmd(dev, cmd);
+
+ return !result;
+}
+
+int set_state(libusb_device_handle *dev, int run)
+{
+ unsigned char cmd[16];
+ memset(cmd, 0, 16);
+ cmd[0] = 0x23;
+ cmd[1] = 0x28;
+ cmd[3] = run & 1;
+ return send_cmd(dev, cmd);
+}
+
+int get_state(libusb_device_handle *dev)
+{
+ unsigned char cmd[16];
+ unsigned char data[3];
+ memset(cmd, 0, 16);
+ cmd[0] = 0x22;
+ cmd[1] = 0x28;
+ if (!send_cmd(dev, cmd)) {
+ return -1; /* error */
+ }
+ if (!get_response(dev, data, 3)) {
+ return -2; /* error */
+ }
+ if ((data[0] == 0x02) && (data[1] == 0x00)) {
+ return data[2]; /* success */
+ }
+ return -3; /* error */
+}
+
+int check_status(libusb_device_handle *dev)
+{
+ unsigned char cmd[16];
+ unsigned char data[512];
+ memset(cmd, 0, 16);
+ cmd[0] = 0x30; /* status */
+ if (!send_cmd(dev, cmd)) {
+ return 0;
+ }
+ int len = get_response(dev, data, 512);
+ if ((len == 3) && (data[0] == 0x20) && (data[1] == 0x20) && (data[2] == 0x15))
+ return 1;
+ return 0;
+}
+
+int get_version(libusb_device_handle *dev, int *mcu, int *fpga)
+{
+ unsigned char cmd[16];
+ unsigned char data[512];
+ memset(cmd, 0, 16);
+ cmd[0] = 0x10; /* version */
+ if (!send_cmd(dev, cmd)) {
+ return 0;
+ }
+ int len = get_response(dev, data, 512);
+ if ((len == 5) && (data[0] == 4)) {
+ *mcu = (data[3]*100) + data[4];
+ *fpga = (data[1]*100) + data[2];
+ return 1;
+ }
+ return 0;
+}
+
+int read_data(libusb_device_handle *dev, void *data, int length)
+{
+ int actual;
+ unsigned char cmd[16];
+ memset(cmd, 0, 16);
+ cmd[0] = 0x41; /* em100-to-host eeprom data */
+ /* cmd 1-3 might be start address? Hard code 0 for now, haven't seen anything else in the logs */
+ /* when doing cmd 1-3, check if 4-6 are end address or length */
+ cmd[4] = length & 0xff;
+ cmd[5] = (length >> 8) & 0xff;
+ cmd[6] = (length >> 16) & 0xff;
+ if (!send_cmd(dev, cmd)) {
+ printf("error initiating host-to-em100 transfer.\n");
+ return 0;
+ }
+ libusb_bulk_transfer(dev, 2 | LIBUSB_ENDPOINT_IN, data, length, &actual, 0);
+ printf("tried reading %d bytes, got %d\n", length, actual);
+ return (actual == length);
+}
+
+int send_data(libusb_device_handle *dev, void *data, int length)
+{
+ int actual;
+ unsigned char cmd[16];
+ memset(cmd, 0, 16);
+ cmd[0] = 0x40; /* host-to-em100 eeprom data */
+ /* cmd 1-3 might be start address? Hard code 0 for now, haven't seen anything else in the logs */
+ /* when doing cmd 1-3, check if 4-6 are end address or length */
+ cmd[4] = length & 0xff;
+ cmd[5] = (length >> 8) & 0xff;
+ cmd[6] = (length >> 16) & 0xff;
+ if (!send_cmd(dev, cmd)) {
+ printf("error initiating host-to-em100 transfer.\n");
+ return 0;
+ }
+ libusb_bulk_transfer(dev, 1 | LIBUSB_ENDPOINT_OUT, data, length, &actual, 0);
+ printf("tried sending %d bytes, sent %d\n", length, actual);
+ return (actual == length);
+}
+
+static const struct option longopts[] = {
+ {"set", 1, 0, 'c'},
+ {"download", 1, 0, 'd'},
+ {"start", 0, 0, 'r'},
+ {"stop", 0, 0, 's'},
+ {"verify", 0, 0, 'v'},
+ {"help", 0, 0, 'h'},
+ {NULL, 0, 0, 0}
+};
+
+void usage(void)
+{
+ printf("em100: em100 client utility\n\n"
+ "-c CHIP|--set CHIP: select CHIP emulation\n"
+ "-d[ownload] FILE: upload FILE into em100\n"
+ "-r|--start: em100 shall run\n"
+ "-s|--stop: em100 shall stop\n"
+ "-v|--verify: verify EM100 content matches the file\n"
+ "-h|--help: this help text\n");
+}
+
+/* get MCU and FPGA version, *100 encoded */
+int main(int argc, char **argv)
+{
+ int opt, idx;
+ const char *desiredchip = NULL;
+ const char *filename = NULL;
+ int do_start = 0, do_stop = 0;
+ int verify = 0;
+ while ((opt = getopt_long(argc, argv, "c:d:rsvh",
+ longopts, &idx)) != -1) {
+ switch (opt) {
+ case 'c':
+ desiredchip = optarg;
+ break;
+ case 'd':
+ filename = optarg;
+ /* TODO: check that file exists */
+ break;
+ case 'r':
+ do_start = 1;
+ break;
+ case 's':
+ do_stop = 0;
+ break;
+ case 'v':
+ verify = 1;
+ break;
+ case 'h':
+ usage();
+ return 0;
+ }
+ }
+
+ const chipdesc *chip = chips;
+ if (desiredchip) {
+ do {
+ if (strcmp(desiredchip, chip->name) == 0) {
+ printf("will emulate '%s'\n", chip->name);
+ break;
+ }
+ } while ((++chip)->name);
+
+ if (chip->name == NULL) {
+ printf("could not find emulation for '%s'\n", desiredchip);
+ return 1;
+ }
+ }
+
+ struct em100 em100;
+ if (!em100_attach(&em100)) {
+ return 1;
+ }
+
+ int mcu, fpga;
+ if (!get_version(em100.dev, &mcu, &fpga)) {
+ printf("Failed fetching version information.\n");
+ return 0;
+ }
+
+ printf("MCU version: %d.%d\nFPGA version: %d.%d\n", mcu/100, mcu%100, fpga/100, fpga%100);
+
+ if (do_stop) {
+ set_state(em100.dev, 0);
+ }
+
+ if (desiredchip) {
+ if (!set_chip_type(em100.dev, chip)) {
+ printf("Failed configuring chip type.\n");
+ return 0;
+ }
+ }
+
+ if (filename) {
+ int maxlen = 0x800000; /* largest size, right? */
+ void *data = malloc(maxlen);
+ if (data == NULL) {
+ printf("FATAL: couldn't allocate memory\n");
+ return 1;
+ }
+ FILE *fdata = fopen(filename, "rb");
+ if (!fdata) {
+ perror("Could not open upload file");
+ return 1;
+ }
+
+ int length = 0;
+ while ((!feof(fdata)) && (length < maxlen)) {
+ int blocksize = 65536;
+ length += blocksize * fread(data+length, blocksize, 1, fdata);
+ }
+ fclose(fdata);
+
+ if (length > maxlen) {
+ printf("FATAL: length > maxlen\n");
+ return 1;
+ }
+
+ send_data(em100.dev, data, length);
+ if (verify) {
+ int done;
+ void *readback = malloc(length);
+ if (data == NULL) {
+ printf("FATAL: couldn't allocate memory\n");
+ return 1;
+ }
+ done = read_data(em100.dev, readback, length);
+ if (done && (memcmp(data, readback, length) == 0))
+ printf("Verify: PASS\n");
+ else
+ printf("Verify: FAIL\n");
+ free(readback);
+ }
+
+ free(data);
+ }
+
+ if (do_start) {
+ set_state(em100.dev, 1);
+ }
+
+ return em100_detach(&em100);
+}
diff --git a/makechips.c b/makechips.c
new file mode 100644
index 0000000..e30ddc1
--- /dev/null
+++ b/makechips.c
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2012 The Chromium OS Authors.
+ *
+ * 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; version 2 of the License.
+ *
+ * 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
+ */
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <getopt.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdint.h>
+
+/* Config file format v1.1:
+ * All files 1.1 are 176 bytes
+ * and data is stored little endian
+ *
+ * 0: Magic "Dcfg"
+ * 4: Version 01 00 01 00 --> 1.1
+ * non-pro config files:
+ * 00 00 01 00 --> 1.0 ( 2 byte commands)
+ * 8: offset of init sequence (0x80)
+ * 12: size of chip
+ * 16: offset of vendor name (0x20)
+ * 20: offset of device name (0x30)
+ * 24: offset ? (0x40)
+ *
+ * offsets below are just examples and need
+ * to be determined at run time:
+ * 0x20: vendor name
+ * 0x30: device name
+ * 0x80: init sequence x23
+ * 3 byte commands?
+ * 4 bytes cmd[3] cmd[2] cmd[1] 00
+ * ....
+ * ff ff ff ff end of list
+ * 0x94: init sequence x11
+ * 4 bytes cmd[3] cmd[2] cmd[1] 00
+ * ....
+ * end of file
+ */
+
+unsigned char cfg[176];
+
+int main(int argc, char *argv[])
+{
+ int i;
+ FILE *f;
+
+ printf("\n#ifndef EM100PRO_CHIPS_H\n");
+ printf("#define EM100PRO_CHIPS_H\n\n");
+ printf("typedef struct {\n\tconst char *name;\n\t");
+ printf("unsigned int size;\n\tconst char x23[16];\n\t");
+ printf("const char x11[6];\n} chipdesc;\n\n");
+ printf("const chipdesc chips[] = {\n");
+
+ for (i=1; i<argc; i++) {
+ f = fopen(argv[i], "r");
+ if (!f) {
+ perror(argv[i]);
+ exit(1);
+ }
+ size_t fread(void *ptr, size_t size, size_t nmemb, FILE
+ *stream);
+ if (fread(cfg, 176, 1, f) != 1) {
+ perror(argv[i]);
+ fclose(f);
+ exit(1);
+ }
+
+ //printf(" /* %s */\n", argv[i]);
+ printf("\t{ /* %s %s (%d kB) */\n", &cfg[0x20],
+ &cfg[0x30], *(uint32_t *)&cfg[0xc] / 1024);
+ printf("\t\t.name = \"%s\",\n", &cfg[0x30]);
+ printf("\t\t.size = 0x%x,\n", *(uint32_t *)&cfg[0xc]);
+ printf("\t\t.x23 = {\n");
+ printf("\t\t\t0x%02x, 0x%02x,\n", cfg[0x81], cfg[0x80]);
+ printf("\t\t\t0x%02x, 0x%02x,\n", cfg[0x85], cfg[0x84]);
+ printf("\t\t\t0x%02x, 0x%02x,\n", cfg[0x89], cfg[0x88]);
+ printf("\t\t\t0x%02x, 0x%02x,\n", cfg[0x8d], cfg[0x8c]);
+
+ printf("\t\t\t0x%02x, 0x%02x,\n", cfg[0x91], cfg[0x90]);
+ printf("\t\t\t0x%02x, 0x%02x,\n", cfg[0x95], cfg[0x94]);
+ printf("\t\t\t0x%02x, 0x%02x,\n", cfg[0x99], cfg[0x98]);
+ printf("\t\t\t0x%02x, 0x%02x},\n", cfg[0x9d], cfg[0x9c]);
+ printf("\t\t.x11 = {\n");
+ printf("\t\t\t0x%02x, 0x%02x,\n", cfg[0xa5], cfg[0xa4]);
+ printf("\t\t\t0x%02x, 0x%02x,\n", cfg[0xa9], cfg[0xa8]);
+ printf("\t\t\t0x%02x, 0x%02x}},\n", cfg[0xad], cfg[0xac]);
+
+ fclose(f);
+ }
+
+ printf("\n\t{ .name = NULL}\n};\n");
+ printf("#endif /* EM100PRO_CHIPS_H */\n");
+ return 0;
+}
+
diff --git a/makechips.sh b/makechips.sh
new file mode 100755
index 0000000..6282992
--- /dev/null
+++ b/makechips.sh
@@ -0,0 +1,53 @@
+#!/bin/bash
+#
+# Copyright (C) 2012 The Chromium OS Authors.
+#
+# 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; version 2 of the License.
+#
+# 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
+#
+
+URL=http://www.dediprog.com/files/81/2715/4/EM100_4.2.04.zip
+
+if ! which curl > /dev/null; then
+ echo "Install curl to run this script."
+ exit 1;
+fi
+if ! which 7z > /dev/null; then
+ echo "Install 7z to run this script."
+ exit 1
+fi
+if ! which cabextract > /dev/null; then
+ echo "Install cabextract to run this script."
+ exit 1
+fi
+
+FILE=$(basename $URL)
+TEMP=$(mktemp -d)
+WD=$(pwd)
+
+cd $TEMP
+echo Downloading...
+curl -s $URL -o $FILE || exit
+echo Unpacking...
+7z x $FILE ${FILE%.zip}.msi > /dev/null
+7z x ${FILE%.zip}.msi WixDediEM100Setup.cab > /dev/null
+cabextract -F PRO_* WixDediEM100Setup.cab > /dev/null
+echo Copying...
+mkdir -p $WD/configs
+for i in PRO_*; do
+ cp $i $WD/configs/${i#PRO_}.cfg
+done
+cd $WD
+rm -rf $TEMP
+echo Done...
+
diff --git a/protocol-notes.txt b/protocol-notes.txt
new file mode 100644
index 0000000..aee24e7
--- /dev/null
+++ b/protocol-notes.txt
@@ -0,0 +1,15 @@
+= Some protocol notes =
+ out 16 bytes: 0x23 x a0 a1: write chip reg x with a0.a1 (x is always even)
+ x = 0x32: always 0xff 0xff (?)
+ x = 0x3a: set chip size to (a1+1) * 0x10000
+ x = 0x38: various values (?)
+ x = 0x40: sometimes: a0 = lowbyte of device id (for 16bit did?), a1 = device id (for 8bit did) ?
+ x = 0x42: a0 = vendor id, a1 = highbyte of device id for 16bit did or device id for 8bit did (?)
+ x = 0x44: always 0x03 0x18 (?)
+ x = 0x46: a0 = various, a1 = always 0x30? (?)
+ x = 0x48: a0 = various, a1 = always 0xc0? (?)
+
+ out 16 bytes: 0x11 x a0 a1: write reg x with a0.a1
+ x = 2: various (?)
+ x = 3: various (?)
+ x = 4: various (?)