basic functions added, problem: altsetting ...
authorhackbard <hackbard>
Sat, 25 Feb 2006 21:25:09 +0000 (21:25 +0000)
committerhackbard <hackbard>
Sat, 25 Feb 2006 21:25:09 +0000 (21:25 +0000)
gemtag/gemtag.c
gemtag/gemtag.h

index 28538c7..9656f01 100644 (file)
 #include <usb.h>
 #include "gemtag.h"
 
 #include <usb.h>
 #include "gemtag.h"
 
-/* variables */
+int hexdump(unsigned char *data,int len) {
+       int i;
 
 
-struct gemtag_handle {
-       struct usb_dev_handle *handle;
-};
+       printf("dump: ");
+       for(i=0;i<len;i++) printf("%02x ",data[i]);
+       printf("\n");
 
 
-/* functions */
+       return 0;
+}
 
 
-struct usb_device *find_device(unsigned short vendor, unsigned short device) {
+struct usb_device *find_device(unsigned short vendor,unsigned short device) {
        
        struct usb_bus *bus;
        struct usb_device *dev;
        
        struct usb_bus *bus;
        struct usb_device *dev;
@@ -51,6 +53,91 @@ struct usb_device *find_device(unsigned short vendor, unsigned short device) {
        return NULL;
 }
 
        return NULL;
 }
 
+u_int16_t gemtag_calc_crc(unsigned char *data,u_int16_t len) {
+
+       u_int16_t crc_polynom;
+       u_int16_t crc_preset;
+       u_int16_t crc;
+       int i,j;
+               
+       crc_polynom=0x8408;
+       crc_preset=0xffff;
+       crc=0xffff;
+
+       for(i=0;i<len;i++) {
+               crc^=data[i];
+               for(j=0;j<8;j++) {
+                       if(crc&0x0001)
+                               crc=(crc>>1)^crc_polynom;
+                       else
+                               crc=(crc>>1);
+               }
+       }
+       return crc;
+}
+
+int gemtag_transcieve(struct gemtag_handle *gh,unsigned char cmd,
+                         unsigned char *tx,unsigned int tx_len,
+                         unsigned char *rx,unsigned int *rx_len) {
+
+       unsigned char txbuf[256];
+       unsigned char rxbuf[256];
+       struct gemtag_cmd_hdr *txhdr;
+       struct gemtag_cmd_hdr *rxhdr;
+       u_int16_t *crcptr;
+       int ret,size;
+
+       txhdr=(struct gemtag_cmd_hdr *)txbuf;
+       rxhdr=(struct gemtag_cmd_hdr *)rxbuf;
+
+       txhdr->start=0xa5;
+       txhdr->seq=++(gh->seq);
+       txhdr->cmd=cmd;
+       txhdr->len=htons(tx_len);
+       size=sizeof(struct gemtag_cmd_hdr);
+       memcpy(txbuf+size,tx,tx_len);
+
+       /* crc check */
+       gh->capabilities=GEMTAG_CAP_CRC;
+       if(gh->capabilities&GEMTAG_CAP_CRC) {
+               size+=tx_len;
+               crcptr=(u_int16_t *)(txbuf+size);
+               *crcptr=gemtag_calc_crc(txbuf,size);
+               size+=2;
+       }
+
+       /* usb write */
+       hexdump(txbuf,size);
+       ret=usb_bulk_write(gh->handle,0x02,txbuf,size,0);
+       if(ret<0) {
+               perror("usb bulk write");
+               return ret;
+       }
+       printf("write of %d bytes successfull\n",ret);
+
+       /* usb read */
+       ret=usb_bulk_read(gh->handle,0x81,rxbuf,sizeof(rxbuf),0);
+       size=ret;
+       if(ret<0) {
+               perror("usb bulk read");
+               return ret;
+       }
+       printf("received %d bytes\n",ret);
+       
+       if(rxhdr->seq!=txhdr->seq)
+               puts("transmitted/recieved header are not equal");
+
+       /* crc check */
+       
+
+       *rx_len=ntohs(rxhdr->len);
+       memcpy(rx,rxbuf+sizeof(struct gemtag_cmd_hdr),
+              ret-sizeof(struct gemtag_cmd_hdr)+2);
+       hexdump(rxbuf,ret+2);
+
+       return 0;
+}
+
 struct gemtag_handle *gemtag_open(void) {
        struct usb_device *gemtag;
        unsigned char rbuf[16];
 struct gemtag_handle *gemtag_open(void) {
        struct usb_device *gemtag;
        unsigned char rbuf[16];
@@ -74,14 +161,14 @@ struct gemtag_handle *gemtag_open(void) {
        memset(gh,0,sizeof(struct gemtag_handle));
 
        numconf=gemtag->descriptor.bNumConfigurations;
        memset(gh,0,sizeof(struct gemtag_handle));
 
        numconf=gemtag->descriptor.bNumConfigurations;
-       printf("found gemtag, %u configurations\n",numconf);
+       printf("found gemtag, %u configuration(s)\n",numconf);
        for(i=0;i<numconf;i++) {
                numint=gemtag->config[i].bNumInterfaces;
        for(i=0;i<numconf;i++) {
                numint=gemtag->config[i].bNumInterfaces;
-               printf("config %u [nr %u] has %u interfaces\n",
+               printf("config %u [nr %u] has %u interface(s)\n",
                       i,gemtag->config[i].bConfigurationValue,
                       numint);
                for(j=0;j<numint;j++) {
                       i,gemtag->config[i].bConfigurationValue,
                       numint);
                for(j=0;j<numint;j++) {
-                       printf("config %u interface %u has %u altsettings\n",
+                       printf("config %u interface %u has %u altsetting(s)\n",
                               i,j,gemtag->config[i].interface[j].num_altsetting);
                }
        }
                               i,j,gemtag->config[i].interface[j].num_altsetting);
                }
        }
@@ -89,6 +176,32 @@ struct gemtag_handle *gemtag_open(void) {
        gh->handle=usb_open(gemtag);
        if(!gh->handle)
                goto out_free;
        gh->handle=usb_open(gemtag);
        if(!gh->handle)
                goto out_free;
+       puts("usb_open successfull");
+
+       if(usb_set_configuration(gh->handle,1)) {
+               perror("set config");
+               goto out_free;
+       }
+       puts("configuration 1 successfully set");
+
+       if(usb_claim_interface(gh->handle,0)) {
+               perror("claim interface");
+               goto out_free;
+       }
+       puts("interface 0 claimed");
+
+/*     if(usb_set_altinterface(gh->handle,1)) {
+               perror("set alt interface");
+               goto out_free;
+       }
+       puts("alt setting 1 selected");
+*/
+
+       gh->capabilities|=GEMTAG_CAP_CRC;
+
+       gemtag_transcieve(gh,0x22,NULL,0,rbuf,&rlen);
+
+       return gh;
 
 out_free:
        free(gh);
 
 out_free:
        free(gh);
index 6e01bd2..f832100 100644 (file)
 #define USB_VENDOR_GEMTAG      0x1394
 #define USB_DEVICE_X501                0x0501
 
 #define USB_VENDOR_GEMTAG      0x1394
 #define USB_DEVICE_X501                0x0501
 
+struct gemtag_handle {
+       struct usb_dev_handle *handle;
+       unsigned char seq;
+       unsigned char capabilities;
+       unsigned char snr[4];
+};
+
+#define GEMTAG_CAP_CRC 0x01
+
+struct gemtag_cmd_hdr {
+       unsigned char start;
+       unsigned char seq;
+       unsigned char cmd;
+       u_int16_t len;
+} __attribute__ ((packed));
+
+#define GEMTAG_CMD_GET_FW_VERSION      0x63
+#define GEMTAG_CMD_GET_SERIAL_NUMBER   0x22
+#define GEMTAG_CMD_GET_RIC_VERSION     0x64
+#define GEMTAG_CMD_PCD_SET_TMO         0x27
+#define GEMTAG_CMD_SET_CPU_TIMEOUT     0x88
+
 #endif
 #endif