Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Garmin GPS driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2006-2011 Hermann Kneissel herkne@gmx.de
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * The latest version of the driver can be found at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * http://sourceforge.net/projects/garmin-gps/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * This driver has been derived from v2.1 of the visor driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/tty_driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/tty_flip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/usb/serial.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) /* the mode to be set when the port ist opened */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) static int initial_mode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #define GARMIN_VENDOR_ID             0x091E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  * Version Information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define VERSION_MAJOR	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define VERSION_MINOR	36
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #define _STR(s) #s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #define _DRIVER_VERSION(a, b) "v" _STR(a) "." _STR(b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #define DRIVER_VERSION _DRIVER_VERSION(VERSION_MAJOR, VERSION_MINOR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #define DRIVER_AUTHOR "hermann kneissel"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #define DRIVER_DESC "garmin gps driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) /* error codes returned by the driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #define EINVPKT	1000	/* invalid packet structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) /* size of the header of a packet using the usb protocol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #define GARMIN_PKTHDR_LENGTH	12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) /* max. possible size of a packet using the serial protocol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) #define MAX_SERIAL_PKT_SIZ (3 + 255 + 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) /*  max. possible size of a packet with worst case stuffing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #define MAX_SERIAL_PKT_SIZ_STUFFED (MAX_SERIAL_PKT_SIZ + 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) /* size of a buffer able to hold a complete (no stuffing) packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * (the document protocol does not contain packets with a larger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  *  size, but in theory a packet may be 64k+12 bytes - if in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  *  later protocol versions larger packet sizes occur, this value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  *  should be increased accordingly, so the input buffer is always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  *  large enough the store a complete packet inclusive header) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #define GPS_IN_BUFSIZ  (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) /* size of a buffer able to hold a complete (incl. stuffing) packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) #define GPS_OUT_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ_STUFFED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) /* where to place the packet id of a serial packet, so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * prepend the usb-packet header without the need to move the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * packets data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) #define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) /* max. size of incoming private packets (header+1 param) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) #define GARMIN_LAYERID_TRANSPORT  0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) #define GARMIN_LAYERID_APPL      20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) /* our own layer-id to use for some control mechanisms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) #define GARMIN_LAYERID_PRIVATE	0x01106E4B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) #define GARMIN_PKTID_PVT_DATA	51
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) #define GARMIN_PKTID_L001_COMMAND_DATA 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) #define CMND_ABORT_TRANSFER 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) /* packet ids used in private layer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #define PRIV_PKTID_SET_DEBUG	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) #define PRIV_PKTID_SET_MODE	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) #define PRIV_PKTID_INFO_REQ	3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) #define PRIV_PKTID_INFO_RESP	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) #define PRIV_PKTID_RESET_REQ	5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) #define PRIV_PKTID_SET_DEF_MODE	6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) #define ETX	0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) #define DLE	0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) #define ACK	0x06
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) #define NAK	0x15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) /* structure used to queue incoming packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) struct garmin_packet {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	struct list_head  list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	int               seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	/* the real size of the data array, always > 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	int               size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	__u8              data[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) /* structure used to keep the current state of the driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) struct garmin_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	__u8   state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	__u16  flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	__u8   mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	__u8   count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	__u8   pkt_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	__u32  serial_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	struct timer_list timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	struct usb_serial_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	int    seq_counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	int    insize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	int    outsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	__u8   inbuffer [GPS_IN_BUFSIZ];  /* tty -> usb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	__u8   outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	__u8   privpkt[4*6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	struct list_head pktlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	struct usb_anchor write_urbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) #define STATE_NEW            0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) #define STATE_INITIAL_DELAY  1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) #define STATE_TIMEOUT        2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) #define STATE_SESSION_REQ1   3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) #define STATE_SESSION_REQ2   4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) #define STATE_ACTIVE         5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) #define STATE_RESET	     8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) #define STATE_DISCONNECTED   9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) #define STATE_WAIT_TTY_ACK  10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) #define STATE_GSP_WAIT_DATA 11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) #define MODE_NATIVE          0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) #define MODE_GARMIN_SERIAL   1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) /* Flags used in garmin_data.flags: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) #define FLAGS_SESSION_REPLY_MASK  0x00C0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) #define FLAGS_SESSION_REPLY1_SEEN 0x0080
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) #define FLAGS_SESSION_REPLY2_SEEN 0x0040
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) #define FLAGS_BULK_IN_ACTIVE      0x0020
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) #define FLAGS_BULK_IN_RESTART     0x0010
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) #define FLAGS_THROTTLED           0x0008
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) #define APP_REQ_SEEN              0x0004
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) #define APP_RESP_SEEN             0x0002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) #define CLEAR_HALT_REQUIRED       0x0001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) #define FLAGS_QUEUING             0x0100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) #define FLAGS_DROP_DATA           0x0800
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) #define FLAGS_GSP_SKIP            0x1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) #define FLAGS_GSP_DLESEEN         0x2000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) /* function prototypes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) static int gsp_next_packet(struct garmin_data *garmin_data_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) static int garmin_write_bulk(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 			     const unsigned char *buf, int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 			     int dismiss_ack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) /* some special packets to be send or received */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) static unsigned char const GARMIN_START_SESSION_REQ[]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	= { 0, 0, 0, 0,  5, 0, 0, 0, 0, 0, 0, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) static unsigned char const GARMIN_START_SESSION_REPLY[]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	= { 0, 0, 0, 0,  6, 0, 0, 0, 4, 0, 0, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	= { 0, 0, 0, 0,  2, 0, 0, 0, 0, 0, 0, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) static unsigned char const GARMIN_STOP_TRANSFER_REQ[]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	= { 20, 0, 0, 0,  10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	= { 20, 0, 0, 0,  10, 0, 0, 0, 1, 0, 0, 0, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) /* packets currently unused, left as documentation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) static unsigned char const GARMIN_APP_LAYER_REPLY[]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	= { 0x14, 0, 0, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) static unsigned char const GARMIN_START_PVT_REQ[]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	= { 20, 0, 0, 0,  10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) static unsigned char const GARMIN_STOP_PVT_REQ[]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	= { 20, 0, 0, 0,  10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) static unsigned char const PRIVATE_REQ[]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	=    { 0x4B, 0x6E, 0x10, 0x01,  0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) static const struct usb_device_id id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	/* the same device id seems to be used by all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	   usb enabled GPS devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	{ USB_DEVICE(GARMIN_VENDOR_ID, 3) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	{ }					/* Terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) MODULE_DEVICE_TABLE(usb, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) static inline int getLayerId(const __u8 *usbPacket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	return __le32_to_cpup((__le32 *)(usbPacket));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) static inline int getPacketId(const __u8 *usbPacket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	return __le32_to_cpup((__le32 *)(usbPacket+4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) static inline int getDataLength(const __u8 *usbPacket)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	return __le32_to_cpup((__le32 *)(usbPacket+8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226)  * check if the usb-packet in buf contains an abort-transfer command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227)  * (if yes, all queued data will be dropped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) static inline int isAbortTrfCmnd(const unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	if (memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 			sizeof(GARMIN_STOP_TRANSFER_REQ)) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	    memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 			sizeof(GARMIN_STOP_TRANSFER_REQ_V2)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) static void send_to_tty(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 			char *data, unsigned int actual_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	if (actual_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 		usb_serial_debug_data(&port->dev, __func__, actual_length, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		tty_insert_flip_string(&port->port, data, actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		tty_flip_buffer_push(&port->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) /******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254)  * packet queue handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255)  ******************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258)  * queue a received (usb-)packet for later processing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) static int pkt_add(struct garmin_data *garmin_data_p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		   unsigned char *data, unsigned int data_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	int state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	struct garmin_packet *pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	/* process only packets containing data ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	if (data_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 								GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		if (!pkt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		pkt->size = data_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		memcpy(pkt->data, data, data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		garmin_data_p->flags |= FLAGS_QUEUING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		result = list_empty(&garmin_data_p->pktlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		pkt->seq = garmin_data_p->seq_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		list_add_tail(&pkt->list, &garmin_data_p->pktlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		state = garmin_data_p->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		dev_dbg(&garmin_data_p->port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 			"%s - added: pkt: %d - %d bytes\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 			pkt->seq, data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		/* in serial mode, if someone is waiting for data from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		   the device, convert and send the next packet to tty. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		if (result && (state == STATE_GSP_WAIT_DATA))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 			gsp_next_packet(garmin_data_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) /* get the next pending packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	struct garmin_packet *result = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	if (!list_empty(&garmin_data_p->pktlist)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		result = (struct garmin_packet *)garmin_data_p->pktlist.next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		list_del(&result->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) /* free up all queued data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) static void pkt_clear(struct garmin_data *garmin_data_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	struct garmin_packet *result = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	while (!list_empty(&garmin_data_p->pktlist)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		result = (struct garmin_packet *)garmin_data_p->pktlist.next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		list_del(&result->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		kfree(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) /******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332)  * garmin serial protocol handling handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333)  ******************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) /* send an ack packet back to the tty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	__u8 pkt[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	__u8 cksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	__u8 *ptr = pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	unsigned  l = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	dev_dbg(&garmin_data_p->port->dev, "%s - pkt-id: 0x%X.\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 			pkt_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	*ptr++ = DLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	*ptr++ = ACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	cksum += ACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	*ptr++ = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	cksum += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	*ptr++ = pkt_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	cksum += pkt_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	if (pkt_id == DLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		*ptr++ = DLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	*ptr++ = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	*ptr++ = (-cksum) & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	*ptr++ = DLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	*ptr++ = ETX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	l = ptr-pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	send_to_tty(garmin_data_p->port, pkt, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373)  * called for a complete packet received from tty layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375)  * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376)  * at GSP_INITIAL_OFFSET.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378)  * count - number of bytes in the input buffer including space reserved for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379)  *         the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380)  *         (including pkt-id, data-length a. cksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	struct device *dev = &garmin_data_p->port->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	__le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	int cksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	int n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	int pktid = recpkt[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	int size = recpkt[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	usb_serial_debug_data(&garmin_data_p->port->dev, __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 			      count-GSP_INITIAL_OFFSET, recpkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	if (size != (count-GSP_INITIAL_OFFSET-3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		dev_dbg(dev, "%s - invalid size, expected %d bytes, got %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 			__func__, size, (count-GSP_INITIAL_OFFSET-3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		return -EINVPKT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	cksum += *recpkt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	cksum += *recpkt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	/* sanity check, remove after test ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	if ((__u8 *)&(usbdata[3]) != recpkt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		dev_dbg(dev, "%s - ptr mismatch %p - %p\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 			&(usbdata[4]), recpkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		return -EINVPKT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	while (n < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		cksum += *recpkt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	if (((cksum + *recpkt) & 0xff) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		dev_dbg(dev, "%s - invalid checksum, expected %02x, got %02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 			__func__, -cksum & 0xff, *recpkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		return -EINVPKT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	usbdata[1] = __cpu_to_le32(pktid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	usbdata[2] = __cpu_to_le32(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 			   GARMIN_PKTHDR_LENGTH+size, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	/* if this was an abort-transfer command, flush all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	   queued data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		garmin_data_p->flags |= FLAGS_DROP_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		pkt_clear(garmin_data_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445)  * Called for data received from tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447)  * buf contains the data read, it may span more than one packet or even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448)  * incomplete packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450)  * input record should be a serial-record, but it may not be complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451)  * Copy it into our local buffer, until an etx is seen (or an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452)  * occurs).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453)  * Once the record is complete, convert into a usb packet and send it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454)  * to the bulk pipe, send an ack back to the tty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456)  * If the input is an ack, just send the last queued packet to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457)  * tty layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459)  * if the input is an abort command, drop all queued data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) static int gsp_receive(struct garmin_data *garmin_data_p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		       const unsigned char *buf, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	struct device *dev = &garmin_data_p->port->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	int offs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	int ack_or_nak_seen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	__u8 *dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	/* dleSeen: set if last byte read was a DLE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	int dleSeen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	/* skip: if set, skip incoming data until possible start of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	 *       new packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	int skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	__u8 data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	dest = garmin_data_p->inbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	size = garmin_data_p->insize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	/* dev_dbg(dev, "%s - dle=%d skip=%d size=%d count=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 		__func__, dleSeen, skip, size, count); */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	if (size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 		size = GSP_INITIAL_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	while (offs < count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		data = *(buf+offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		offs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		if (data == DLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 			if (skip) { /* start of a new pkt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 				skip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 				size = GSP_INITIAL_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 				dleSeen = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 			} else if (dleSeen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 				dest[size++] = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 				dleSeen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 				dleSeen = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		} else if (data == ETX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 			if (dleSeen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 				/* packet complete */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 				data = dest[GSP_INITIAL_OFFSET];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 				if (data == ACK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 					ack_or_nak_seen = ACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 					dev_dbg(dev, "ACK packet complete.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 				} else if (data == NAK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 					ack_or_nak_seen = NAK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 					dev_dbg(dev, "NAK packet complete.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 					dev_dbg(dev, "packet complete - id=0x%X.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 							data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 					gsp_rec_packet(garmin_data_p, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 				skip = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 				size = GSP_INITIAL_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 				dleSeen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 				dest[size++] = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		} else if (!skip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 			if (dleSeen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 				size = GSP_INITIAL_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 				dleSeen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 			dest[size++] = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		if (size >= GPS_IN_BUFSIZ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 			dev_dbg(dev, "%s - packet too large.\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 			skip = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 			size = GSP_INITIAL_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 			dleSeen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	garmin_data_p->insize = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	/* copy flags back to structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	if (skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		garmin_data_p->flags |= FLAGS_GSP_SKIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	if (dleSeen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	if (ack_or_nak_seen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		if (gsp_next_packet(garmin_data_p) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 			garmin_data_p->state = STATE_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 			garmin_data_p->state = STATE_GSP_WAIT_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579)  * Sends a usb packet to the tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581)  * Assumes, that all packages and at an usb-packet boundary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583)  * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) static int gsp_send(struct garmin_data *garmin_data_p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		    const unsigned char *buf, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	struct device *dev = &garmin_data_p->port->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	const unsigned char *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	unsigned char *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	int pktid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	int datalen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	int cksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	int k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	dev_dbg(dev, "%s - state %d - %d bytes.\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		garmin_data_p->state, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	k = garmin_data_p->outsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	if ((k+count) > GPS_OUT_BUFSIZ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		dev_dbg(dev, "packet too large\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 		garmin_data_p->outsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		return -4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	memcpy(garmin_data_p->outbuffer+k, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	k += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	garmin_data_p->outsize = k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	if (k >= GARMIN_PKTHDR_LENGTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		pktid  = getPacketId(garmin_data_p->outbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		datalen = getDataLength(garmin_data_p->outbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		i = GARMIN_PKTHDR_LENGTH + datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		if (k < i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	dev_dbg(dev, "%s - %d bytes in buffer, %d bytes in pkt.\n", __func__, k, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	/* garmin_data_p->outbuffer now contains a complete packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	usb_serial_debug_data(&garmin_data_p->port->dev, __func__, k,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 			      garmin_data_p->outbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	garmin_data_p->outsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	if (getLayerId(garmin_data_p->outbuffer) != GARMIN_LAYERID_APPL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		dev_dbg(dev, "not an application packet (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 				getLayerId(garmin_data_p->outbuffer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	if (pktid > 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		dev_dbg(dev, "packet-id %d too large\n", pktid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		return -2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	if (datalen > 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		dev_dbg(dev, "packet-size %d too large\n", datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		return -3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	/* the serial protocol should be able to handle this packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	k = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	for (i = 0; i < datalen; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		if (*src++ == DLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 			k++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	if (k > (GARMIN_PKTHDR_LENGTH-2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		/* can't add stuffing DLEs in place, move data to end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		   of buffer ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		memcpy(dst, src, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		src = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	dst = garmin_data_p->outbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	*dst++ = DLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	*dst++ = pktid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	cksum += pktid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	*dst++ = datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	cksum += datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	if (datalen == DLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		*dst++ = DLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	for (i = 0; i < datalen; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		__u8 c = *src++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		*dst++ = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		cksum += c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		if (c == DLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 			*dst++ = DLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	cksum = -cksum & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	*dst++ = cksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	if (cksum == DLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		*dst++ = DLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	*dst++ = DLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	*dst++ = ETX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	i = dst-garmin_data_p->outbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	garmin_data_p->pkt_id = pktid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	garmin_data_p->state  = STATE_WAIT_TTY_ACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701)  * Process the next pending data packet - if there is one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) static int gsp_next_packet(struct garmin_data *garmin_data_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	struct garmin_packet *pkt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		dev_dbg(&garmin_data_p->port->dev, "%s - next pkt: %d\n", __func__, pkt->seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		result = gsp_send(garmin_data_p, pkt->data, pkt->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		if (result > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 			kfree(pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 			return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		kfree(pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) /******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723)  * garmin native mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724)  ******************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728)  * Called for data received from tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730)  * The input data is expected to be in garmin usb-packet format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732)  * buf contains the data read, it may span more than one packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733)  * or even incomplete packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) static int nat_receive(struct garmin_data *garmin_data_p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		       const unsigned char *buf, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	__u8 *dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	int offs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	int result = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	while (offs < count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		/* if buffer contains header, copy rest of data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 			len = GARMIN_PKTHDR_LENGTH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 			      +getDataLength(garmin_data_p->inbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			len = GARMIN_PKTHDR_LENGTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 		if (len >= GPS_IN_BUFSIZ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			/* seems to be an invalid packet, ignore rest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 			   of input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 			dev_dbg(&garmin_data_p->port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 				"%s - packet size too large: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 				__func__, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 			garmin_data_p->insize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 			count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 			result = -EINVPKT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 			len -= garmin_data_p->insize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 			if (len > (count-offs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 				len = (count-offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 			if (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 				dest = garmin_data_p->inbuffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 						+ garmin_data_p->insize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 				memcpy(dest, buf+offs, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 				garmin_data_p->insize += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 				offs += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		/* do we have a complete packet ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 			len = GARMIN_PKTHDR_LENGTH+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 			   getDataLength(garmin_data_p->inbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 			if (garmin_data_p->insize >= len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 				garmin_write_bulk(garmin_data_p->port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 						   garmin_data_p->inbuffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 						   len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 				garmin_data_p->insize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 				/* if this was an abort-transfer command,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 				   flush all queued data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 				if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 					spin_lock_irqsave(&garmin_data_p->lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 									flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 					garmin_data_p->flags |= FLAGS_DROP_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 					spin_unlock_irqrestore(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 						&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 					pkt_clear(garmin_data_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) /******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802)  * private packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803)  ******************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) static void priv_status_resp(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	__le32 *pkt = (__le32 *)garmin_data_p->privpkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	pkt[2] = __cpu_to_le32(12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	pkt[4] = __cpu_to_le32(garmin_data_p->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	send_to_tty(port, (__u8 *)pkt, 6 * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) /******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822)  * Garmin specific driver functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823)  ******************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) static int process_resetdev_request(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	garmin_data_p->state = STATE_RESET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	garmin_data_p->serial_num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	usb_kill_urb(port->interrupt_in_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	dev_dbg(&port->dev, "%s - usb_reset_device\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	status = usb_reset_device(port->serial->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		dev_dbg(&port->dev, "%s - usb_reset_device failed: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 			__func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849)  * clear all cached data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) static int garmin_clear(struct garmin_data *garmin_data_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	/* flush all queued data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	pkt_clear(garmin_data_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	garmin_data_p->insize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	garmin_data_p->outsize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) static int garmin_init_session(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	usb_kill_urb(port->interrupt_in_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 				status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	 * using the initialization method from gpsbabel. See comments in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	dev_dbg(&port->dev, "%s - starting session ...\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	garmin_data_p->state = STATE_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	for (i = 0; i < 3; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		status = garmin_write_bulk(port, GARMIN_START_SESSION_REQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 				sizeof(GARMIN_START_SESSION_REQ), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 			goto err_kill_urbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) err_kill_urbs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	usb_kill_anchored_urbs(&garmin_data_p->write_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	usb_kill_urb(port->interrupt_in_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) static int garmin_open(struct tty_struct *tty, struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	garmin_data_p->mode  = initial_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	garmin_data_p->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	garmin_data_p->flags &= FLAGS_SESSION_REPLY1_SEEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	/* shutdown any bulk reads that might be going on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	usb_kill_urb(port->read_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	if (garmin_data_p->state == STATE_RESET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		status = garmin_init_session(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	garmin_data_p->state = STATE_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) static void garmin_close(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	dev_dbg(&port->dev, "%s - mode=%d state=%d flags=0x%X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		__func__, garmin_data_p->mode, garmin_data_p->state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		garmin_data_p->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	garmin_clear(garmin_data_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	/* shutdown our urbs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	usb_kill_urb(port->read_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	usb_kill_anchored_urbs(&garmin_data_p->write_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	/* keep reset state so we know that we must start a new session */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	if (garmin_data_p->state != STATE_RESET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		garmin_data_p->state = STATE_DISCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) static void garmin_write_bulk_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	if (port) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		struct garmin_data *garmin_data_p =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 					usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		if (getLayerId(urb->transfer_buffer) == GARMIN_LAYERID_APPL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 			if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 				gsp_send_ack(garmin_data_p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 					((__u8 *)urb->transfer_buffer)[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		usb_serial_port_softint(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	/* Ignore errors that resulted from garmin_write_bulk with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	   dismiss_ack = 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	/* free up the transfer buffer, as usb_free_urb() does not do this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	kfree(urb->transfer_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) static int garmin_write_bulk(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 			      const unsigned char *buf, int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 			      int dismiss_ack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	unsigned char *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	garmin_data_p->flags &= ~FLAGS_DROP_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	buffer = kmalloc(count, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	urb = usb_alloc_urb(0, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	if (!urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	memcpy(buffer, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	usb_serial_debug_data(&port->dev, __func__, count, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	usb_fill_bulk_urb(urb, serial->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 				usb_sndbulkpipe(serial->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 					port->bulk_out_endpointAddress),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 				buffer, count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 				garmin_write_bulk_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 				dismiss_ack ? NULL : port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	urb->transfer_flags |= URB_ZERO_PACKET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	if (getLayerId(buffer) == GARMIN_LAYERID_APPL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		garmin_data_p->flags |= APP_REQ_SEEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		if (garmin_data_p->mode == MODE_GARMIN_SERIAL)  {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 			pkt_clear(garmin_data_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 			garmin_data_p->state = STATE_GSP_WAIT_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	/* send it down the pipe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	usb_anchor_urb(urb, &garmin_data_p->write_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	status = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		   "%s - usb_submit_urb(write bulk) failed with status = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 				__func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		count = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		usb_unanchor_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	/* we are done with this urb, so let the host driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	 * really free it when it is finished with it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 					 const unsigned char *buf, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	struct device *dev = &port->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	int pktid, pktsiz, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	__le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	usb_serial_debug_data(dev, __func__, count, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	if (garmin_data_p->state == STATE_RESET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	/* check for our private packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	if (count >= GARMIN_PKTHDR_LENGTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		len = PRIVPKTSIZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		if (count < len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 			len = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		memcpy(garmin_data_p->privpkt, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		pktsiz = getDataLength(garmin_data_p->privpkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		pktid  = getPacketId(garmin_data_p->privpkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		if (count == (GARMIN_PKTHDR_LENGTH + pktsiz) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 				getLayerId(garmin_data_p->privpkt) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 						GARMIN_LAYERID_PRIVATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			dev_dbg(dev, "%s - processing private request %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 				__func__, pktid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 			/* drop all unfinished transfers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 			garmin_clear(garmin_data_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 			switch (pktid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 			case PRIV_PKTID_SET_MODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 				if (pktsiz != 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 					return -EINVPKT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 				garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 				dev_dbg(dev, "%s - mode set to %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 					__func__, garmin_data_p->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 			case PRIV_PKTID_INFO_REQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 				priv_status_resp(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 			case PRIV_PKTID_RESET_REQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 				process_resetdev_request(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 			case PRIV_PKTID_SET_DEF_MODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 				if (pktsiz != 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 					return -EINVPKT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 				initial_mode = __le32_to_cpu(privpkt[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 				dev_dbg(dev, "%s - initial_mode set to %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 					__func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 					garmin_data_p->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 			return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		return gsp_receive(garmin_data_p, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	} else {	/* MODE_NATIVE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		return nat_receive(garmin_data_p, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) static int garmin_write_room(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	 * Report back the bytes currently available in the output buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) static void garmin_read_process(struct garmin_data *garmin_data_p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 				 unsigned char *data, unsigned data_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 				 int bulk_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	if (garmin_data_p->flags & FLAGS_DROP_DATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		/* abort-transfer cmd is active */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 		dev_dbg(&garmin_data_p->port->dev, "%s - pkt dropped\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	} else if (garmin_data_p->state != STATE_DISCONNECTED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		garmin_data_p->state != STATE_RESET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		/* if throttling is active or postprecessing is required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 		   put the received data in the input queue, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		   send it directly to the tty port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		if (garmin_data_p->flags & FLAGS_QUEUING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 			pkt_add(garmin_data_p, data, data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		} else if (bulk_data || (data_length >= sizeof(u32) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 				getLayerId(data) == GARMIN_LAYERID_APPL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 			spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 			garmin_data_p->flags |= APP_RESP_SEEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 			spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 			if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 				pkt_add(garmin_data_p, data, data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 				send_to_tty(garmin_data_p->port, data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 						data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		/* ignore system layer packets ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) static void garmin_read_bulk_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	unsigned char *data = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	int status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 			__func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	garmin_read_process(garmin_data_p, data, urb->actual_length, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	if (urb->actual_length == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 			(garmin_data_p->flags & FLAGS_BULK_IN_RESTART) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 		retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 			dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 				"%s - failed resubmitting read urb, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 				__func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	} else if (urb->actual_length > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		/* Continue trying to read until nothing more is received  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 			retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 			if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 				dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 					"%s - failed resubmitting read urb, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 					__func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		dev_dbg(&port->dev, "%s - end of bulk data\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 		spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) static void garmin_read_int_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	unsigned char *data = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	int status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	switch (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		/* success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	case -ECONNRESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 		/* this urb is terminated, clean up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 			__func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 			__func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	usb_serial_debug_data(&port->dev, __func__, urb->actual_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 			      urb->transfer_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 				sizeof(GARMIN_BULK_IN_AVAIL_REPLY)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		dev_dbg(&port->dev, "%s - bulk data available.\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		if ((garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 			/* bulk data available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 			retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 			if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 				dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 				 "%s - failed submitting read urb, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 							__func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 				spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 				garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 				spin_unlock_irqrestore(&garmin_data_p->lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 									flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 			/* bulk-in transfer still active */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 			spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 			garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 			spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	} else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 			 && memcmp(data, GARMIN_START_SESSION_REPLY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 				 sizeof(GARMIN_START_SESSION_REPLY)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 		garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 		spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		/* save the serial number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		garmin_data_p->serial_num = __le32_to_cpup(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 					(__le32 *)(data+GARMIN_PKTHDR_LENGTH));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		dev_dbg(&port->dev, "%s - start-of-session reply seen - serial %u.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 			__func__, garmin_data_p->serial_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	garmin_read_process(garmin_data_p, data, urb->actual_length, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	retval = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		dev_err(&urb->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 			"%s - Error %d submitting interrupt urb\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 			__func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293)  * Sends the next queued packt to the tty port (garmin native mode only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)  * and then sets a timer to call itself again until all queued data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295)  * is sent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) static int garmin_flush_queue(struct garmin_data *garmin_data_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	struct garmin_packet *pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		pkt = pkt_pop(garmin_data_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		if (pkt != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 			send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 			kfree(pkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 			mod_timer(&garmin_data_p->timer, (1)+jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 			spin_lock_irqsave(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 			garmin_data_p->flags &= ~FLAGS_QUEUING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 			spin_unlock_irqrestore(&garmin_data_p->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) static void garmin_throttle(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	/* set flag, data received will be put into a queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	   for later processing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	spin_lock_irq(&garmin_data_p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	spin_unlock_irq(&garmin_data_p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) static void garmin_unthrottle(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	spin_lock_irq(&garmin_data_p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	garmin_data_p->flags &= ~FLAGS_THROTTLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	spin_unlock_irq(&garmin_data_p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	/* in native mode send queued data to tty, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	   serial mode nothing needs to be done here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	if (garmin_data_p->mode == MODE_NATIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		garmin_flush_queue(garmin_data_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	if ((garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		status = usb_submit_urb(port->read_urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 			dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 				"%s - failed resubmitting read urb, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 				__func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357)  * The timer is currently only used to send queued packets to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)  * the tty in cases where the protocol provides no own handshaking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359)  * to initiate the transfer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) static void timeout_handler(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	struct garmin_data *garmin_data_p = from_timer(garmin_data_p, t, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	/* send the next queued packet to the tty port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	if (garmin_data_p->mode == MODE_NATIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		if (garmin_data_p->flags & FLAGS_QUEUING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 			garmin_flush_queue(garmin_data_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) static int garmin_port_probe(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	struct garmin_data *garmin_data_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	if (!garmin_data_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	timer_setup(&garmin_data_p->timer, timeout_handler, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	spin_lock_init(&garmin_data_p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 	INIT_LIST_HEAD(&garmin_data_p->pktlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	garmin_data_p->port = port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	garmin_data_p->state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	garmin_data_p->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	garmin_data_p->count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	init_usb_anchor(&garmin_data_p->write_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	usb_set_serial_port_data(port, garmin_data_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	status = garmin_init_session(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) err_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	kfree(garmin_data_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) static int garmin_port_remove(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	usb_kill_anchored_urbs(&garmin_data_p->write_urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	usb_kill_urb(port->interrupt_in_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	del_timer_sync(&garmin_data_p->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	kfree(garmin_data_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) /* All of the device info needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) static struct usb_serial_driver garmin_device = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		.owner       = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 		.name        = "garmin_gps",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	.description         = "Garmin GPS usb/tty",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	.id_table            = id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	.num_ports           = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	.open                = garmin_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	.close               = garmin_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	.throttle            = garmin_throttle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	.unthrottle          = garmin_unthrottle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	.port_probe		= garmin_port_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	.port_remove		= garmin_port_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	.write               = garmin_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	.write_room          = garmin_write_room,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	.write_bulk_callback = garmin_write_bulk_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	.read_bulk_callback  = garmin_read_bulk_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	.read_int_callback   = garmin_read_int_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) static struct usb_serial_driver * const serial_drivers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	&garmin_device, NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) module_usb_serial_driver(serial_drivers, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) MODULE_AUTHOR(DRIVER_AUTHOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) module_param(initial_mode, int, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) MODULE_PARM_DESC(initial_mode, "Initial mode");