📄 garmin_gps.c
字号:
/* * Garmin GPS driver * * Copyright (C) 2004 Hermann Kneissel herkne@users.sourceforge.net * * The latest version of the driver can be found at * http://sourceforge.net/projects/garmin-gps/ * * This driver has been derived from v2.1 of the visor driver. * * 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., 59 Temple Place - Suite 330, Boston, MA 02111 USA */#include <linux/config.h>#include <linux/kernel.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/timer.h>#include <linux/tty.h>#include <linux/tty_driver.h>#include <linux/tty_flip.h>#include <linux/module.h>#include <linux/spinlock.h>#include <asm/uaccess.h>#include <linux/usb.h>/* the mode to be set when the port ist opened */static int initial_mode = 1;/* debug flag */static int debug = 0;#include "usb-serial.h"#define GARMIN_VENDOR_ID 0x091E/* * Version Information */#define VERSION_MAJOR 0#define VERSION_MINOR 23#define _STR(s) #s#define _DRIVER_VERSION(a,b) "v" _STR(a) "." _STR(b)#define DRIVER_VERSION _DRIVER_VERSION(VERSION_MAJOR, VERSION_MINOR)#define DRIVER_AUTHOR "hermann kneissel"#define DRIVER_DESC "garmin gps driver"/* error codes returned by the driver */#define EINVPKT 1000 /* invalid packet structure */// size of the header of a packet using the usb protocol#define GARMIN_PKTHDR_LENGTH 12// max. possible size of a packet using the serial protocol #define MAX_SERIAL_PKT_SIZ (3+255+3)// max. possible size of a packet with worst case stuffing#define MAX_SERIAL_PKT_SIZ_STUFFED MAX_SERIAL_PKT_SIZ+256// size of a buffer able to hold a complete (no stuffing) packet// (the document protocol does not contain packets with a larger// size, but in theory a packet may be 64k+12 bytes - if in// later protocol versions larger packet sizes occur, this value// should be increased accordingly, so the input buffer is always // large enough the store a complete packet inclusive header)#define GPS_IN_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ) // size of a buffer able to hold a complete (incl. stuffing) packet#define GPS_OUT_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ_STUFFED) // where to place the packet id of a serial packet, so we can// prepend the usb-packet header without the need to move the// packets data#define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)// max. size of incoming private packets (header+1 param)#define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)#define GARMIN_LAYERID_TRANSPORT 0#define GARMIN_LAYERID_APPL 20// our own layer-id to use for some control mechanisms#define GARMIN_LAYERID_PRIVATE 0x01106E4B#define GARMIN_PKTID_PVT_DATA 51#define GARMIN_PKTID_L001_COMMAND_DATA 10#define CMND_ABORT_TRANSFER 0// packet ids used in private layer#define PRIV_PKTID_SET_DEBUG 1#define PRIV_PKTID_SET_MODE 2#define PRIV_PKTID_INFO_REQ 3#define PRIV_PKTID_INFO_RESP 4#define PRIV_PKTID_RESET_REQ 5#define PRIV_PKTID_SET_DEF_MODE 6#define ETX 0x03#define DLE 0x10#define ACK 0x06#define NAK 0x15/* structure used to queue incoming packets */struct garmin_packet { struct list_head list; int seq; int size; // the real size of the data array, always > 0 __u8 data[1];};/* structure used to keep the current state of the driver */struct garmin_data { __u8 state; __u16 flags; __u8 mode; __u8 ignorePkts; __u8 count; __u8 pkt_id; __u32 serial_num; struct timer_list timer; struct usb_serial_port *port; int seq_counter; int insize; int outsize; __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */ __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */ __u8 privpkt[4*6]; spinlock_t lock; struct list_head pktlist;};#define STATE_NEW 0#define STATE_INITIAL_DELAY 1#define STATE_TIMEOUT 2#define STATE_SESSION_REQ1 3#define STATE_SESSION_REQ2 4#define STATE_ACTIVE 5#define STATE_RESET 8#define STATE_DISCONNECTED 9#define STATE_WAIT_TTY_ACK 10#define STATE_GSP_WAIT_DATA 11#define MODE_NATIVE 0#define MODE_GARMIN_SERIAL 1// Flags used in garmin_data.flags:#define FLAGS_SESSION_REPLY_MASK 0x00C0#define FLAGS_SESSION_REPLY1_SEEN 0x0080#define FLAGS_SESSION_REPLY2_SEEN 0x0040#define FLAGS_BULK_IN_ACTIVE 0x0020#define FLAGS_THROTTLED 0x0010#define CLEAR_HALT_REQUIRED 0x0001#define FLAGS_QUEUING 0x0100#define FLAGS_APP_RESP_SEEN 0x0200#define FLAGS_APP_REQ_SEEN 0x0400#define FLAGS_DROP_DATA 0x0800#define FLAGS_GSP_SKIP 0x1000#define FLAGS_GSP_DLESEEN 0x2000/* function prototypes */static void gsp_next_packet(struct garmin_data * garmin_data_p);static int garmin_write_bulk(struct usb_serial_port *port, const unsigned char *buf, int count);/* some special packets to be send or received */static unsigned char const GARMIN_START_SESSION_REQ[] = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };static unsigned char const GARMIN_START_SESSION_REQ2[] = { 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };static unsigned char const GARMIN_START_SESSION_REPLY[] = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };static unsigned char const GARMIN_SESSION_ACTIVE_REPLY[] = { 0, 0, 0, 0, 17, 0, 0, 0, 4, 0, 0, 0, 0, 16, 0, 0 };static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[] = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };static unsigned char const GARMIN_APP_LAYER_REPLY[] = { 0x14, 0, 0, 0 };static unsigned char const GARMIN_START_PVT_REQ[] = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };static unsigned char const GARMIN_STOP_PVT_REQ[] = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };static unsigned char const GARMIN_STOP_TRANSFER_REQ[] = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[] = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };static unsigned char const PRIVATE_REQ[] = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };static struct usb_device_id id_table [] = { /* the same device id seems to be used by all usb enabled gps devices */ { USB_DEVICE(GARMIN_VENDOR_ID, 3 ) }, { } /* Terminating entry */};MODULE_DEVICE_TABLE (usb, id_table);static struct usb_driver garmin_driver = { .name = "garmin_gps", .probe = usb_serial_probe, .disconnect = usb_serial_disconnect, .id_table = id_table, .no_dynamic_id = 1,};static inline int noResponseFromAppLayer(struct garmin_data * garmin_data_p){ return ((garmin_data_p->flags & (FLAGS_APP_REQ_SEEN|FLAGS_APP_RESP_SEEN)) == FLAGS_APP_REQ_SEEN);}static inline int getLayerId(const __u8 *usbPacket){ return __le32_to_cpup((__le32 *)(usbPacket));}static inline int getPacketId(const __u8 *usbPacket){ return __le32_to_cpup((__le32 *)(usbPacket+4));}static inline int getDataLength(const __u8 *usbPacket){ return __le32_to_cpup((__le32 *)(usbPacket+8));}/* * check if the usb-packet in buf contains an abort-transfer command. * (if yes, all queued data will be dropped) */static inline int isAbortTrfCmnd(const unsigned char *buf){ if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ, sizeof(GARMIN_STOP_TRANSFER_REQ)) || 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2, sizeof(GARMIN_STOP_TRANSFER_REQ_V2))) return 1; else return 0;}static void send_to_tty(struct usb_serial_port *port, char *data, unsigned int actual_length){ struct tty_struct *tty = port->tty; if (tty && actual_length) { usb_serial_debug_data(debug, &port->dev, __FUNCTION__, actual_length, data); tty_buffer_request_room(tty, actual_length); tty_insert_flip_string(tty, data, actual_length); tty_flip_buffer_push(tty); }}/****************************************************************************** * packet queue handling ******************************************************************************//* * queue a received (usb-)packet for later processing */static int pkt_add(struct garmin_data * garmin_data_p, unsigned char *data, unsigned int data_length){ int result = 0; unsigned long flags; struct garmin_packet *pkt; /* process only packets containg data ... */ if (data_length) { garmin_data_p->flags |= FLAGS_QUEUING; pkt = kmalloc(sizeof(struct garmin_packet)+data_length, GFP_ATOMIC); if (pkt == NULL) { dev_err(&garmin_data_p->port->dev, "out of memory\n"); return 0; } pkt->size = data_length; memcpy(pkt->data, data, data_length); spin_lock_irqsave(&garmin_data_p->lock, flags); result = list_empty(&garmin_data_p->pktlist); pkt->seq = garmin_data_p->seq_counter++; list_add_tail(&pkt->list, &garmin_data_p->pktlist); spin_unlock_irqrestore(&garmin_data_p->lock, flags); /* in serial mode, if someone is waiting for data from the device, iconvert and send the next packet to tty. */ if (result && (garmin_data_p->state == STATE_GSP_WAIT_DATA)) { gsp_next_packet(garmin_data_p); } } return result;}/* get the next pending packet */static struct garmin_packet *pkt_pop(struct garmin_data * garmin_data_p){ unsigned long flags; struct garmin_packet *result = NULL; spin_lock_irqsave(&garmin_data_p->lock, flags); if (!list_empty(&garmin_data_p->pktlist)) { result = (struct garmin_packet *)garmin_data_p->pktlist.next; list_del(&result->list); } spin_unlock_irqrestore(&garmin_data_p->lock, flags); return result;}/* free up all queued data */static void pkt_clear(struct garmin_data * garmin_data_p){ unsigned long flags; struct garmin_packet *result = NULL; dbg("%s", __FUNCTION__); spin_lock_irqsave(&garmin_data_p->lock, flags); while (!list_empty(&garmin_data_p->pktlist)) { result = (struct garmin_packet *)garmin_data_p->pktlist.next; list_del(&result->list); kfree(result); } spin_unlock_irqrestore(&garmin_data_p->lock, flags);}/****************************************************************************** * garmin serial protocol handling handling ******************************************************************************//* send an ack packet back to the tty */static int gsp_send_ack(struct garmin_data * garmin_data_p, __u8 pkt_id){ __u8 pkt[10]; __u8 cksum = 0; __u8 *ptr = pkt; unsigned l = 0; dbg("%s - pkt-id: 0x%X.", __FUNCTION__, 0xFF & pkt_id); *ptr++ = DLE; *ptr++ = ACK; cksum += ACK; *ptr++ = 2; cksum += 2; *ptr++ = pkt_id; cksum += pkt_id; if (pkt_id == DLE) { *ptr++ = DLE; } *ptr++ = 0; *ptr++ = 0xFF & (-cksum); *ptr++ = DLE; *ptr++ = ETX; l = ptr-pkt; send_to_tty(garmin_data_p->port, pkt, l); return 0;}/* * called for a complete packet received from tty layer * * the complete packet (pkzid ... cksum) is in garmin_data_p->inbuf starting * at GSP_INITIAL_OFFSET. * * count - number of bytes in the input buffer including space reserved for * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet * (including pkt-id, data-length a. cksum) */static int gsp_rec_packet(struct garmin_data * garmin_data_p, int count){ const __u8* recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET; __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer; int cksum = 0; int n = 0; int pktid = recpkt[0]; int size = recpkt[1]; usb_serial_debug_data(debug, &garmin_data_p->port->dev, __FUNCTION__, count-GSP_INITIAL_OFFSET, recpkt); if (size != (count-GSP_INITIAL_OFFSET-3)) { dbg("%s - invalid size, expected %d bytes, got %d", __FUNCTION__, size, (count-GSP_INITIAL_OFFSET-3)); return -EINVPKT; } cksum += *recpkt++; cksum += *recpkt++; // sanity check, remove after test ... if ((__u8*)&(usbdata[3]) != recpkt) { dbg("%s - ptr mismatch %p - %p", __FUNCTION__, &(usbdata[4]), recpkt); return -EINVPKT; } while (n < size) { cksum += *recpkt++; n++; } if ((0xff & (cksum + *recpkt)) != 0) { dbg("%s - invalid checksum, expected %02x, got %02x", __FUNCTION__, 0xff & -cksum, 0xff & *recpkt); return -EINVPKT; } usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL); usbdata[1] = __cpu_to_le32(pktid); usbdata[2] = __cpu_to_le32(size); garmin_write_bulk (garmin_data_p->port, garmin_data_p->inbuffer, GARMIN_PKTHDR_LENGTH+size); /* if this was an abort-transfer command, flush all queued data. */ if (isAbortTrfCmnd(garmin_data_p->inbuffer)) { garmin_data_p->flags |= FLAGS_DROP_DATA; pkt_clear(garmin_data_p); } return count;}/* * Called for data received from tty * * buf contains the data read, it may span more than one packet or even * incomplete packets * * input record should be a serial-record, but it may not be complete. * Copy it into our local buffer, until an etx is seen (or an error * occurs). * Once the record is complete, convert into a usb packet and send it * to the bulk pipe, send an ack back to the tty. * * If the input is an ack, just send the last queued packet to the * tty layer. * * if the input is an abort command, drop all queued data. */static int gsp_receive(struct garmin_data * garmin_data_p, const unsigned char *buf, int count){ int offs = 0; int ack_or_nak_seen = 0; int i = 0; __u8 *dest = garmin_data_p->inbuffer; int size = garmin_data_p->insize; // dleSeen: set if last byte read was a DLE int dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN; // skip: if set, skip incoming data until possible start of // new packet int skip = garmin_data_p->flags & FLAGS_GSP_SKIP; __u8 data; dbg("%s - dle=%d skip=%d size=%d count=%d",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -