garmin_gps.c 42 KB

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