2 * (C) 2006 by Frank Zirkelbach <hackbard@hackdaworld.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 int hexdump(unsigned char *data,int len) {
31 for(i=0;i<len;i++) printf("%02x ",data[i]);
37 struct usb_device *find_device(unsigned short vendor,unsigned short device) {
40 struct usb_device *dev;
46 if(dev->descriptor.idVendor==vendor &&
47 dev->descriptor.idProduct==device)
56 u_int16_t gemtag_calc_crc(unsigned char *data,u_int16_t len) {
58 u_int16_t crc_polynom;
71 crc=(crc>>1)^crc_polynom;
79 int gemtag_transcieve(struct gemtag_handle *gh,unsigned char cmd,
80 unsigned char *tx,unsigned int tx_len,
81 unsigned char *rx,unsigned int *rx_len) {
83 unsigned char txbuf[256];
84 unsigned char rxbuf[256];
85 struct gemtag_cmd_hdr *txhdr;
86 struct gemtag_cmd_hdr *rxhdr;
90 txhdr=(struct gemtag_cmd_hdr *)txbuf;
91 rxhdr=(struct gemtag_cmd_hdr *)rxbuf;
94 txhdr->seq=++(gh->seq);
96 txhdr->len=htons(tx_len);
97 size=sizeof(struct gemtag_cmd_hdr);
98 memcpy(txbuf+size,tx,tx_len);
101 gh->capabilities=GEMTAG_CAP_CRC;
102 if(gh->capabilities&GEMTAG_CAP_CRC) {
104 crcptr=(u_int16_t *)(txbuf+size);
105 *crcptr=gemtag_calc_crc(txbuf,size);
111 ret=usb_bulk_write(gh->handle,0x02,txbuf,size,0);
113 perror("usb bulk write");
116 printf("write of %d bytes successfull\n",ret);
119 ret=usb_bulk_read(gh->handle,0x81,rxbuf,sizeof(rxbuf),0);
122 perror("usb bulk read");
125 printf("received %d bytes\n",ret);
127 if(rxhdr->seq!=txhdr->seq)
128 puts("transmitted/recieved header are not equal");
133 *rx_len=ntohs(rxhdr->len);
134 memcpy(rx,rxbuf+sizeof(struct gemtag_cmd_hdr),
135 ret-sizeof(struct gemtag_cmd_hdr)+2);
136 hexdump(rxbuf,ret+2);
141 struct gemtag_handle *gemtag_open(void) {
142 struct usb_device *gemtag;
143 unsigned char rbuf[16];
145 unsigned int i,numconf;
146 unsigned int j,numint;
147 struct gemtag_handle *gh;
155 gemtag=find_device(USB_VENDOR_GEMTAG, USB_DEVICE_X501);
156 if(!gemtag) return NULL;
158 gh=malloc(sizeof(struct gemtag_handle));
161 memset(gh,0,sizeof(struct gemtag_handle));
163 numconf=gemtag->descriptor.bNumConfigurations;
164 printf("found gemtag, %u configuration(s)\n",numconf);
165 for(i=0;i<numconf;i++) {
166 numint=gemtag->config[i].bNumInterfaces;
167 printf("config %u [nr %u] has %u interface(s)\n",
168 i,gemtag->config[i].bConfigurationValue,
170 for(j=0;j<numint;j++) {
171 printf("config %u interface %u has %u altsetting(s)\n",
172 i,j,gemtag->config[i].interface[j].num_altsetting);
176 gh->handle=usb_open(gemtag);
179 puts("usb_open successfull");
181 if(usb_set_configuration(gh->handle,1)) {
182 perror("set config");
185 puts("configuration 1 successfully set");
187 if(usb_claim_interface(gh->handle,0)) {
188 perror("claim interface");
191 puts("interface 0 claimed");
193 /* if(usb_set_altinterface(gh->handle,1)) {
194 perror("set alt interface");
197 puts("alt setting 1 selected");
200 gh->capabilities|=GEMTAG_CAP_CRC;
202 gemtag_transcieve(gh,0x22,NULL,0,rbuf,&rlen);
211 int main(int argc, char **argv) {
213 struct gemtag_handle *gh;