garmin_gps.c 39 KB

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