garmin_gps.c 39 KB

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