garmin_gps.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  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 <linux/uaccess.h>
  36. #include <asm/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 int debug;
  43. #define GARMIN_VENDOR_ID 0x091E
  44. /*
  45. * Version Information
  46. */
  47. #define VERSION_MAJOR 0
  48. #define VERSION_MINOR 31
  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 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
  179. usb enabled GPS devices */
  180. { USB_DEVICE(GARMIN_VENDOR_ID, 3) },
  181. { } /* Terminating entry */
  182. };
  183. MODULE_DEVICE_TABLE(usb, id_table);
  184. static struct usb_driver garmin_driver = {
  185. .name = "garmin_gps",
  186. .probe = usb_serial_probe,
  187. .disconnect = usb_serial_disconnect,
  188. .id_table = id_table,
  189. .no_dynamic_id = 1,
  190. };
  191. static inline int noResponseFromAppLayer(struct garmin_data *garmin_data_p)
  192. {
  193. return atomic_read(&garmin_data_p->req_count) ==
  194. atomic_read(&garmin_data_p->resp_count);
  195. }
  196. static inline int getLayerId(const __u8 *usbPacket)
  197. {
  198. return __le32_to_cpup((__le32 *)(usbPacket));
  199. }
  200. static inline int getPacketId(const __u8 *usbPacket)
  201. {
  202. return __le32_to_cpup((__le32 *)(usbPacket+4));
  203. }
  204. static inline int getDataLength(const __u8 *usbPacket)
  205. {
  206. return __le32_to_cpup((__le32 *)(usbPacket+8));
  207. }
  208. /*
  209. * check if the usb-packet in buf contains an abort-transfer command.
  210. * (if yes, all queued data will be dropped)
  211. */
  212. static inline int isAbortTrfCmnd(const unsigned char *buf)
  213. {
  214. if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
  215. sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
  216. 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
  217. sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
  218. return 1;
  219. else
  220. return 0;
  221. }
  222. static void send_to_tty(struct usb_serial_port *port,
  223. char *data, unsigned int actual_length)
  224. {
  225. struct tty_struct *tty = port->port.tty;
  226. if (tty && actual_length) {
  227. usb_serial_debug_data(debug, &port->dev,
  228. __func__, actual_length, data);
  229. tty_buffer_request_room(tty, actual_length);
  230. tty_insert_flip_string(tty, data, actual_length);
  231. tty_flip_buffer_push(tty);
  232. }
  233. }
  234. /******************************************************************************
  235. * packet queue handling
  236. ******************************************************************************/
  237. /*
  238. * queue a received (usb-)packet for later processing
  239. */
  240. static int pkt_add(struct garmin_data *garmin_data_p,
  241. unsigned char *data, unsigned int data_length)
  242. {
  243. int state = 0;
  244. int result = 0;
  245. unsigned long flags;
  246. struct garmin_packet *pkt;
  247. /* process only packets containg data ... */
  248. if (data_length) {
  249. pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
  250. GFP_ATOMIC);
  251. if (pkt == NULL) {
  252. dev_err(&garmin_data_p->port->dev, "out of memory\n");
  253. return 0;
  254. }
  255. pkt->size = data_length;
  256. memcpy(pkt->data, data, data_length);
  257. spin_lock_irqsave(&garmin_data_p->lock, flags);
  258. garmin_data_p->flags |= FLAGS_QUEUING;
  259. result = list_empty(&garmin_data_p->pktlist);
  260. pkt->seq = garmin_data_p->seq_counter++;
  261. list_add_tail(&pkt->list, &garmin_data_p->pktlist);
  262. state = garmin_data_p->state;
  263. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  264. /* in serial mode, if someone is waiting for data from
  265. the device, iconvert and send the next packet to tty. */
  266. if (result && (state == STATE_GSP_WAIT_DATA))
  267. gsp_next_packet(garmin_data_p);
  268. }
  269. return result;
  270. }
  271. /* get the next pending packet */
  272. static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p)
  273. {
  274. unsigned long flags;
  275. struct garmin_packet *result = NULL;
  276. spin_lock_irqsave(&garmin_data_p->lock, flags);
  277. if (!list_empty(&garmin_data_p->pktlist)) {
  278. result = (struct garmin_packet *)garmin_data_p->pktlist.next;
  279. list_del(&result->list);
  280. }
  281. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  282. return result;
  283. }
  284. /* free up all queued data */
  285. static void pkt_clear(struct garmin_data *garmin_data_p)
  286. {
  287. unsigned long flags;
  288. struct garmin_packet *result = NULL;
  289. dbg("%s", __func__);
  290. spin_lock_irqsave(&garmin_data_p->lock, flags);
  291. while (!list_empty(&garmin_data_p->pktlist)) {
  292. result = (struct garmin_packet *)garmin_data_p->pktlist.next;
  293. list_del(&result->list);
  294. kfree(result);
  295. }
  296. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  297. }
  298. /******************************************************************************
  299. * garmin serial protocol handling handling
  300. ******************************************************************************/
  301. /* send an ack packet back to the tty */
  302. static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
  303. {
  304. __u8 pkt[10];
  305. __u8 cksum = 0;
  306. __u8 *ptr = pkt;
  307. unsigned l = 0;
  308. dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id);
  309. *ptr++ = DLE;
  310. *ptr++ = ACK;
  311. cksum += ACK;
  312. *ptr++ = 2;
  313. cksum += 2;
  314. *ptr++ = pkt_id;
  315. cksum += pkt_id;
  316. if (pkt_id == DLE)
  317. *ptr++ = DLE;
  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. __func__, count-GSP_INITIAL_OFFSET, recpkt);
  346. if (size != (count-GSP_INITIAL_OFFSET-3)) {
  347. dbg("%s - invalid size, expected %d bytes, got %d",
  348. __func__, 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. __func__, &(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. __func__, 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. */
  412. int skip;
  413. __u8 data;
  414. spin_lock_irqsave(&garmin_data_p->lock, flags);
  415. dest = garmin_data_p->inbuffer;
  416. size = garmin_data_p->insize;
  417. dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
  418. skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
  419. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  420. dbg("%s - dle=%d skip=%d size=%d count=%d",
  421. __func__, dleSeen, skip, size, count);
  422. if (size == 0)
  423. size = GSP_INITIAL_OFFSET;
  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 - id=0x%X.",
  450. 0xFF & data);
  451. gsp_rec_packet(garmin_data_p, size);
  452. }
  453. skip = 1;
  454. size = GSP_INITIAL_OFFSET;
  455. dleSeen = 0;
  456. } else {
  457. dest[size++] = data;
  458. }
  459. } else if (!skip) {
  460. if (dleSeen) {
  461. dbg("non-masked DLE at %d - restarting", i);
  462. size = GSP_INITIAL_OFFSET;
  463. dleSeen = 0;
  464. }
  465. dest[size++] = data;
  466. }
  467. if (size >= GPS_IN_BUFSIZ) {
  468. dbg("%s - packet too large.", __func__);
  469. skip = 1;
  470. size = GSP_INITIAL_OFFSET;
  471. dleSeen = 0;
  472. }
  473. }
  474. spin_lock_irqsave(&garmin_data_p->lock, flags);
  475. garmin_data_p->insize = size;
  476. /* copy flags back to structure */
  477. if (skip)
  478. garmin_data_p->flags |= FLAGS_GSP_SKIP;
  479. else
  480. garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
  481. if (dleSeen)
  482. garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
  483. else
  484. garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
  485. if (ack_or_nak_seen)
  486. garmin_data_p->state = STATE_GSP_WAIT_DATA;
  487. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  488. if (ack_or_nak_seen)
  489. gsp_next_packet(garmin_data_p);
  490. return count;
  491. }
  492. /*
  493. * Sends a usb packet to the tty
  494. *
  495. * Assumes, that all packages and at an usb-packet boundary.
  496. *
  497. * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
  498. */
  499. static int gsp_send(struct garmin_data *garmin_data_p,
  500. const unsigned char *buf, int count)
  501. {
  502. const unsigned char *src;
  503. unsigned char *dst;
  504. int pktid = 0;
  505. int datalen = 0;
  506. int cksum = 0;
  507. int i = 0;
  508. int k;
  509. dbg("%s - state %d - %d bytes.", __func__,
  510. garmin_data_p->state, count);
  511. k = garmin_data_p->outsize;
  512. if ((k+count) > GPS_OUT_BUFSIZ) {
  513. dbg("packet too large");
  514. garmin_data_p->outsize = 0;
  515. return -4;
  516. }
  517. memcpy(garmin_data_p->outbuffer+k, buf, count);
  518. k += count;
  519. garmin_data_p->outsize = k;
  520. if (k >= GARMIN_PKTHDR_LENGTH) {
  521. pktid = getPacketId(garmin_data_p->outbuffer);
  522. datalen = getDataLength(garmin_data_p->outbuffer);
  523. i = GARMIN_PKTHDR_LENGTH + datalen;
  524. if (k < i)
  525. return 0;
  526. } else {
  527. return 0;
  528. }
  529. dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, k, i);
  530. /* garmin_data_p->outbuffer now contains a complete packet */
  531. usb_serial_debug_data(debug, &garmin_data_p->port->dev,
  532. __func__, k, garmin_data_p->outbuffer);
  533. garmin_data_p->outsize = 0;
  534. if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
  535. dbg("not an application packet (%d)",
  536. getLayerId(garmin_data_p->outbuffer));
  537. return -1;
  538. }
  539. if (pktid > 255) {
  540. dbg("packet-id %d too large", pktid);
  541. return -2;
  542. }
  543. if (datalen > 255) {
  544. dbg("packet-size %d too large", datalen);
  545. return -3;
  546. }
  547. /* the serial protocol should be able to handle this packet */
  548. k = 0;
  549. src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
  550. for (i = 0; i < datalen; i++) {
  551. if (*src++ == DLE)
  552. k++;
  553. }
  554. src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
  555. if (k > (GARMIN_PKTHDR_LENGTH-2)) {
  556. /* can't add stuffing DLEs in place, move data to end
  557. of buffer ... */
  558. dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
  559. memcpy(dst, src, datalen);
  560. src = dst;
  561. }
  562. dst = garmin_data_p->outbuffer;
  563. *dst++ = DLE;
  564. *dst++ = pktid;
  565. cksum += pktid;
  566. *dst++ = datalen;
  567. cksum += datalen;
  568. if (datalen == DLE)
  569. *dst++ = DLE;
  570. for (i = 0; i < datalen; i++) {
  571. __u8 c = *src++;
  572. *dst++ = c;
  573. cksum += c;
  574. if (c == DLE)
  575. *dst++ = DLE;
  576. }
  577. cksum = 0xFF & -cksum;
  578. *dst++ = cksum;
  579. if (cksum == DLE)
  580. *dst++ = DLE;
  581. *dst++ = DLE;
  582. *dst++ = ETX;
  583. i = dst-garmin_data_p->outbuffer;
  584. send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
  585. garmin_data_p->pkt_id = pktid;
  586. garmin_data_p->state = STATE_WAIT_TTY_ACK;
  587. return i;
  588. }
  589. /*
  590. * Process the next pending data packet - if there is one
  591. */
  592. static void gsp_next_packet(struct garmin_data *garmin_data_p)
  593. {
  594. struct garmin_packet *pkt = NULL;
  595. while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
  596. dbg("%s - next pkt: %d", __func__, pkt->seq);
  597. if (gsp_send(garmin_data_p, pkt->data, pkt->size) > 0) {
  598. kfree(pkt);
  599. return;
  600. }
  601. kfree(pkt);
  602. }
  603. }
  604. /******************************************************************************
  605. * garmin native mode
  606. ******************************************************************************/
  607. /*
  608. * Called for data received from tty
  609. *
  610. * The input data is expected to be in garmin usb-packet format.
  611. *
  612. * buf contains the data read, it may span more than one packet
  613. * or even incomplete packets
  614. */
  615. static int nat_receive(struct garmin_data *garmin_data_p,
  616. const unsigned char *buf, int count)
  617. {
  618. unsigned long flags;
  619. __u8 *dest;
  620. int offs = 0;
  621. int result = count;
  622. int len;
  623. while (offs < count) {
  624. /* if buffer contains header, copy rest of data */
  625. if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
  626. len = GARMIN_PKTHDR_LENGTH
  627. +getDataLength(garmin_data_p->inbuffer);
  628. else
  629. len = GARMIN_PKTHDR_LENGTH;
  630. if (len >= GPS_IN_BUFSIZ) {
  631. /* seems to be an invalid packet, ignore rest
  632. of input */
  633. dbg("%s - packet size too large: %d", __func__, len);
  634. garmin_data_p->insize = 0;
  635. count = 0;
  636. result = -EINVPKT;
  637. } else {
  638. len -= garmin_data_p->insize;
  639. if (len > (count-offs))
  640. len = (count-offs);
  641. if (len > 0) {
  642. dest = garmin_data_p->inbuffer
  643. + garmin_data_p->insize;
  644. memcpy(dest, buf+offs, len);
  645. garmin_data_p->insize += len;
  646. offs += len;
  647. }
  648. }
  649. /* do we have a complete packet ? */
  650. if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
  651. len = GARMIN_PKTHDR_LENGTH+
  652. getDataLength(garmin_data_p->inbuffer);
  653. if (garmin_data_p->insize >= len) {
  654. garmin_write_bulk(garmin_data_p->port,
  655. garmin_data_p->inbuffer,
  656. len, 0);
  657. garmin_data_p->insize = 0;
  658. /* if this was an abort-transfer command,
  659. flush all queued data. */
  660. if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
  661. spin_lock_irqsave(&garmin_data_p->lock,
  662. flags);
  663. garmin_data_p->flags |= FLAGS_DROP_DATA;
  664. spin_unlock_irqrestore(
  665. &garmin_data_p->lock, flags);
  666. pkt_clear(garmin_data_p);
  667. }
  668. }
  669. }
  670. }
  671. return result;
  672. }
  673. /******************************************************************************
  674. * private packets
  675. ******************************************************************************/
  676. static void priv_status_resp(struct usb_serial_port *port)
  677. {
  678. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  679. __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
  680. pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
  681. pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
  682. pkt[2] = __cpu_to_le32(12);
  683. pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
  684. pkt[4] = __cpu_to_le32(garmin_data_p->mode);
  685. pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
  686. send_to_tty(port, (__u8 *)pkt, 6 * 4);
  687. }
  688. /******************************************************************************
  689. * Garmin specific driver functions
  690. ******************************************************************************/
  691. static int process_resetdev_request(struct usb_serial_port *port)
  692. {
  693. unsigned long flags;
  694. int status;
  695. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  696. spin_lock_irqsave(&garmin_data_p->lock, flags);
  697. garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
  698. garmin_data_p->state = STATE_RESET;
  699. garmin_data_p->serial_num = 0;
  700. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  701. usb_kill_urb(port->interrupt_in_urb);
  702. dbg("%s - usb_reset_device", __func__);
  703. status = usb_reset_device(port->serial->dev);
  704. if (status)
  705. dbg("%s - usb_reset_device failed: %d",
  706. __func__, status);
  707. return status;
  708. }
  709. /*
  710. * clear all cached data
  711. */
  712. static int garmin_clear(struct garmin_data *garmin_data_p)
  713. {
  714. unsigned long flags;
  715. int status = 0;
  716. struct usb_serial_port *port = garmin_data_p->port;
  717. if (port != NULL && atomic_read(&garmin_data_p->resp_count)) {
  718. /* send a terminate command */
  719. status = garmin_write_bulk(port, GARMIN_STOP_TRANSFER_REQ,
  720. sizeof(GARMIN_STOP_TRANSFER_REQ), 1);
  721. }
  722. /* flush all queued data */
  723. pkt_clear(garmin_data_p);
  724. spin_lock_irqsave(&garmin_data_p->lock, flags);
  725. garmin_data_p->insize = 0;
  726. garmin_data_p->outsize = 0;
  727. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  728. return status;
  729. }
  730. static int garmin_init_session(struct usb_serial_port *port)
  731. {
  732. unsigned long flags;
  733. struct usb_serial *serial = port->serial;
  734. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  735. int status = 0;
  736. if (status == 0) {
  737. usb_kill_urb(port->interrupt_in_urb);
  738. dbg("%s - adding interrupt input", __func__);
  739. port->interrupt_in_urb->dev = serial->dev;
  740. status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  741. if (status)
  742. dev_err(&serial->dev->dev,
  743. "%s - failed submitting interrupt urb, error %d\n",
  744. __func__, status);
  745. }
  746. if (status == 0) {
  747. dbg("%s - starting session ...", __func__);
  748. garmin_data_p->state = STATE_ACTIVE;
  749. status = garmin_write_bulk(port, GARMIN_START_SESSION_REQ,
  750. sizeof(GARMIN_START_SESSION_REQ), 0);
  751. if (status >= 0) {
  752. spin_lock_irqsave(&garmin_data_p->lock, flags);
  753. garmin_data_p->ignorePkts++;
  754. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  755. /* not needed, but the win32 driver does it too ... */
  756. status = garmin_write_bulk(port,
  757. GARMIN_START_SESSION_REQ2,
  758. sizeof(GARMIN_START_SESSION_REQ2), 0);
  759. if (status >= 0) {
  760. status = 0;
  761. spin_lock_irqsave(&garmin_data_p->lock, flags);
  762. garmin_data_p->ignorePkts++;
  763. spin_unlock_irqrestore(&garmin_data_p->lock,
  764. flags);
  765. }
  766. }
  767. }
  768. return status;
  769. }
  770. static int garmin_open(struct tty_struct *tty,
  771. struct usb_serial_port *port, struct file *filp)
  772. {
  773. unsigned long flags;
  774. int status = 0;
  775. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  776. dbg("%s - port %d", __func__, port->number);
  777. /*
  778. * Force low_latency on so that our tty_push actually forces the data
  779. * through, otherwise it is scheduled, and with high data rates (like
  780. * with OHCI) data can get lost.
  781. */
  782. if (tty)
  783. tty->low_latency = 1;
  784. spin_lock_irqsave(&garmin_data_p->lock, flags);
  785. garmin_data_p->mode = initial_mode;
  786. garmin_data_p->count = 0;
  787. garmin_data_p->flags = 0;
  788. atomic_set(&garmin_data_p->req_count, 0);
  789. atomic_set(&garmin_data_p->resp_count, 0);
  790. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  791. /* shutdown any bulk reads that might be going on */
  792. usb_kill_urb(port->write_urb);
  793. usb_kill_urb(port->read_urb);
  794. if (garmin_data_p->state == STATE_RESET)
  795. status = garmin_init_session(port);
  796. garmin_data_p->state = STATE_ACTIVE;
  797. return status;
  798. }
  799. static void garmin_close(struct tty_struct *tty,
  800. struct usb_serial_port *port, struct file *filp)
  801. {
  802. struct usb_serial *serial = port->serial;
  803. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  804. dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__,
  805. port->number, garmin_data_p->mode,
  806. garmin_data_p->state, garmin_data_p->flags);
  807. if (!serial)
  808. return;
  809. mutex_lock(&port->serial->disc_mutex);
  810. if (!port->serial->disconnected)
  811. garmin_clear(garmin_data_p);
  812. /* shutdown our urbs */
  813. usb_kill_urb(port->read_urb);
  814. usb_kill_urb(port->write_urb);
  815. if (!port->serial->disconnected) {
  816. if (noResponseFromAppLayer(garmin_data_p) ||
  817. ((garmin_data_p->flags & CLEAR_HALT_REQUIRED) != 0)) {
  818. process_resetdev_request(port);
  819. garmin_data_p->state = STATE_RESET;
  820. } else {
  821. garmin_data_p->state = STATE_DISCONNECTED;
  822. }
  823. } else {
  824. garmin_data_p->state = STATE_DISCONNECTED;
  825. }
  826. mutex_unlock(&port->serial->disc_mutex);
  827. }
  828. static void garmin_write_bulk_callback(struct urb *urb)
  829. {
  830. unsigned long flags;
  831. struct usb_serial_port *port = urb->context;
  832. int status = urb->status;
  833. if (port) {
  834. struct garmin_data *garmin_data_p =
  835. usb_get_serial_port_data(port);
  836. dbg("%s - port %d", __func__, port->number);
  837. if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)
  838. && (garmin_data_p->mode == MODE_GARMIN_SERIAL)) {
  839. gsp_send_ack(garmin_data_p,
  840. ((__u8 *)urb->transfer_buffer)[4]);
  841. }
  842. if (status) {
  843. dbg("%s - nonzero write bulk status received: %d",
  844. __func__, urb->status);
  845. spin_lock_irqsave(&garmin_data_p->lock, flags);
  846. garmin_data_p->flags |= CLEAR_HALT_REQUIRED;
  847. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  848. }
  849. usb_serial_port_softint(port);
  850. }
  851. /* Ignore errors that resulted from garmin_write_bulk with
  852. dismiss_ack = 1 */
  853. /* free up the transfer buffer, as usb_free_urb() does not do this */
  854. kfree(urb->transfer_buffer);
  855. }
  856. static int garmin_write_bulk(struct usb_serial_port *port,
  857. const unsigned char *buf, int count,
  858. int dismiss_ack)
  859. {
  860. unsigned long flags;
  861. struct usb_serial *serial = port->serial;
  862. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  863. struct urb *urb;
  864. unsigned char *buffer;
  865. int status;
  866. dbg("%s - port %d, state %d", __func__, port->number,
  867. garmin_data_p->state);
  868. spin_lock_irqsave(&garmin_data_p->lock, flags);
  869. garmin_data_p->flags &= ~FLAGS_DROP_DATA;
  870. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  871. buffer = kmalloc(count, GFP_ATOMIC);
  872. if (!buffer) {
  873. dev_err(&port->dev, "out of memory\n");
  874. return -ENOMEM;
  875. }
  876. urb = usb_alloc_urb(0, GFP_ATOMIC);
  877. if (!urb) {
  878. dev_err(&port->dev, "no more free urbs\n");
  879. kfree(buffer);
  880. return -ENOMEM;
  881. }
  882. memcpy(buffer, buf, count);
  883. usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
  884. usb_fill_bulk_urb(urb, serial->dev,
  885. usb_sndbulkpipe(serial->dev,
  886. port->bulk_out_endpointAddress),
  887. buffer, count,
  888. garmin_write_bulk_callback,
  889. dismiss_ack ? NULL : port);
  890. urb->transfer_flags |= URB_ZERO_PACKET;
  891. if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
  892. atomic_inc(&garmin_data_p->req_count);
  893. if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
  894. pkt_clear(garmin_data_p);
  895. garmin_data_p->state = STATE_GSP_WAIT_DATA;
  896. }
  897. }
  898. /* send it down the pipe */
  899. status = usb_submit_urb(urb, GFP_ATOMIC);
  900. if (status) {
  901. dev_err(&port->dev,
  902. "%s - usb_submit_urb(write bulk) failed with status = %d\n",
  903. __func__, status);
  904. count = status;
  905. }
  906. /* we are done with this urb, so let the host driver
  907. * really free it when it is finished with it */
  908. usb_free_urb(urb);
  909. return count;
  910. }
  911. static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
  912. const unsigned char *buf, int count)
  913. {
  914. int pktid, pktsiz, len;
  915. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  916. __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
  917. usb_serial_debug_data(debug, &port->dev, __func__, count, buf);
  918. /* check for our private packets */
  919. if (count >= GARMIN_PKTHDR_LENGTH) {
  920. len = PRIVPKTSIZ;
  921. if (count < len)
  922. len = count;
  923. memcpy(garmin_data_p->privpkt, buf, len);
  924. pktsiz = getDataLength(garmin_data_p->privpkt);
  925. pktid = getPacketId(garmin_data_p->privpkt);
  926. if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
  927. && GARMIN_LAYERID_PRIVATE ==
  928. getLayerId(garmin_data_p->privpkt)) {
  929. dbg("%s - processing private request %d",
  930. __func__, pktid);
  931. /* drop all unfinished transfers */
  932. garmin_clear(garmin_data_p);
  933. switch (pktid) {
  934. case PRIV_PKTID_SET_DEBUG:
  935. if (pktsiz != 4)
  936. return -EINVPKT;
  937. debug = __le32_to_cpu(privpkt[3]);
  938. dbg("%s - debug level set to 0x%X",
  939. __func__, debug);
  940. break;
  941. case PRIV_PKTID_SET_MODE:
  942. if (pktsiz != 4)
  943. return -EINVPKT;
  944. garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
  945. dbg("%s - mode set to %d",
  946. __func__, garmin_data_p->mode);
  947. break;
  948. case PRIV_PKTID_INFO_REQ:
  949. priv_status_resp(port);
  950. break;
  951. case PRIV_PKTID_RESET_REQ:
  952. atomic_inc(&garmin_data_p->req_count);
  953. break;
  954. case PRIV_PKTID_SET_DEF_MODE:
  955. if (pktsiz != 4)
  956. return -EINVPKT;
  957. initial_mode = __le32_to_cpu(privpkt[3]);
  958. dbg("%s - initial_mode set to %d",
  959. __func__,
  960. garmin_data_p->mode);
  961. break;
  962. }
  963. return count;
  964. }
  965. }
  966. garmin_data_p->ignorePkts = 0;
  967. if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
  968. return gsp_receive(garmin_data_p, buf, count);
  969. } else { /* MODE_NATIVE */
  970. return nat_receive(garmin_data_p, buf, count);
  971. }
  972. }
  973. static int garmin_write_room(struct tty_struct *tty)
  974. {
  975. struct usb_serial_port *port = tty->driver_data;
  976. /*
  977. * Report back the bytes currently available in the output buffer.
  978. */
  979. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  980. return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
  981. }
  982. static void garmin_read_process(struct garmin_data *garmin_data_p,
  983. unsigned char *data, unsigned data_length)
  984. {
  985. if (garmin_data_p->flags & FLAGS_DROP_DATA) {
  986. /* abort-transfer cmd is actice */
  987. dbg("%s - pkt dropped", __func__);
  988. } else if (garmin_data_p->state != STATE_DISCONNECTED &&
  989. garmin_data_p->state != STATE_RESET) {
  990. /* remember any appl.layer packets, so we know
  991. if a reset is required or not when closing
  992. the device */
  993. if (0 == memcmp(data, GARMIN_APP_LAYER_REPLY,
  994. sizeof(GARMIN_APP_LAYER_REPLY))) {
  995. atomic_inc(&garmin_data_p->resp_count);
  996. }
  997. /* if throttling is active or postprecessing is required
  998. put the received data in the input queue, otherwise
  999. send it directly to the tty port */
  1000. if (garmin_data_p->flags & FLAGS_QUEUING) {
  1001. pkt_add(garmin_data_p, data, data_length);
  1002. } else if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
  1003. if (getLayerId(data) == GARMIN_LAYERID_APPL)
  1004. pkt_add(garmin_data_p, data, data_length);
  1005. } else {
  1006. send_to_tty(garmin_data_p->port, data, data_length);
  1007. }
  1008. }
  1009. }
  1010. static void garmin_read_bulk_callback(struct urb *urb)
  1011. {
  1012. unsigned long flags;
  1013. struct usb_serial_port *port = urb->context;
  1014. struct usb_serial *serial = port->serial;
  1015. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  1016. unsigned char *data = urb->transfer_buffer;
  1017. int status = urb->status;
  1018. int retval;
  1019. dbg("%s - port %d", __func__, port->number);
  1020. if (!serial) {
  1021. dbg("%s - bad serial pointer, exiting", __func__);
  1022. return;
  1023. }
  1024. if (status) {
  1025. dbg("%s - nonzero read bulk status received: %d",
  1026. __func__, status);
  1027. return;
  1028. }
  1029. usb_serial_debug_data(debug, &port->dev,
  1030. __func__, urb->actual_length, data);
  1031. garmin_read_process(garmin_data_p, data, urb->actual_length);
  1032. if (urb->actual_length == 0 &&
  1033. 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
  1034. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1035. garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
  1036. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1037. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1038. if (retval)
  1039. dev_err(&port->dev,
  1040. "%s - failed resubmitting read urb, error %d\n",
  1041. __func__, retval);
  1042. } else if (urb->actual_length > 0) {
  1043. /* Continue trying to read until nothing more is received */
  1044. if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
  1045. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1046. if (retval)
  1047. dev_err(&port->dev,
  1048. "%s - failed resubmitting read urb, "
  1049. "error %d\n", __func__, retval);
  1050. }
  1051. } else {
  1052. dbg("%s - end of bulk data", __func__);
  1053. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1054. garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
  1055. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1056. }
  1057. return;
  1058. }
  1059. static void garmin_read_int_callback(struct urb *urb)
  1060. {
  1061. unsigned long flags;
  1062. int retval;
  1063. struct usb_serial_port *port = urb->context;
  1064. struct usb_serial *serial = port->serial;
  1065. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  1066. unsigned char *data = urb->transfer_buffer;
  1067. int status = urb->status;
  1068. switch (status) {
  1069. case 0:
  1070. /* success */
  1071. break;
  1072. case -ECONNRESET:
  1073. case -ENOENT:
  1074. case -ESHUTDOWN:
  1075. /* this urb is terminated, clean up */
  1076. dbg("%s - urb shutting down with status: %d",
  1077. __func__, status);
  1078. return;
  1079. default:
  1080. dbg("%s - nonzero urb status received: %d",
  1081. __func__, status);
  1082. return;
  1083. }
  1084. usb_serial_debug_data(debug, &port->dev, __func__,
  1085. urb->actual_length, urb->transfer_buffer);
  1086. if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
  1087. 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
  1088. sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
  1089. dbg("%s - bulk data available.", __func__);
  1090. if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
  1091. /* bulk data available */
  1092. usb_fill_bulk_urb(port->read_urb, serial->dev,
  1093. usb_rcvbulkpipe(serial->dev,
  1094. port->bulk_in_endpointAddress),
  1095. port->read_urb->transfer_buffer,
  1096. port->read_urb->transfer_buffer_length,
  1097. garmin_read_bulk_callback, port);
  1098. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1099. if (retval) {
  1100. dev_err(&port->dev,
  1101. "%s - failed submitting read urb, error %d\n",
  1102. __func__, retval);
  1103. } else {
  1104. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1105. garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
  1106. /* do not send this packet to the user */
  1107. garmin_data_p->ignorePkts = 1;
  1108. spin_unlock_irqrestore(&garmin_data_p->lock,
  1109. flags);
  1110. }
  1111. } else {
  1112. /* bulk-in transfer still active */
  1113. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1114. garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
  1115. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1116. }
  1117. } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
  1118. && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
  1119. sizeof(GARMIN_START_SESSION_REPLY))) {
  1120. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1121. garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
  1122. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1123. /* save the serial number */
  1124. garmin_data_p->serial_num = __le32_to_cpup(
  1125. (__le32 *)(data+GARMIN_PKTHDR_LENGTH));
  1126. dbg("%s - start-of-session reply seen - serial %u.",
  1127. __func__, garmin_data_p->serial_num);
  1128. }
  1129. if (garmin_data_p->ignorePkts) {
  1130. /* this reply belongs to a request generated by the driver,
  1131. ignore it. */
  1132. dbg("%s - pkt ignored (%d)",
  1133. __func__, garmin_data_p->ignorePkts);
  1134. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1135. garmin_data_p->ignorePkts--;
  1136. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1137. } else {
  1138. garmin_read_process(garmin_data_p, data, urb->actual_length);
  1139. }
  1140. port->interrupt_in_urb->dev = port->serial->dev;
  1141. retval = usb_submit_urb(urb, GFP_ATOMIC);
  1142. if (retval)
  1143. dev_err(&urb->dev->dev,
  1144. "%s - Error %d submitting interrupt urb\n",
  1145. __func__, retval);
  1146. }
  1147. /*
  1148. * Sends the next queued packt to the tty port (garmin native mode only)
  1149. * and then sets a timer to call itself again until all queued data
  1150. * is sent.
  1151. */
  1152. static int garmin_flush_queue(struct garmin_data *garmin_data_p)
  1153. {
  1154. unsigned long flags;
  1155. struct garmin_packet *pkt;
  1156. if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
  1157. pkt = pkt_pop(garmin_data_p);
  1158. if (pkt != NULL) {
  1159. send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
  1160. kfree(pkt);
  1161. mod_timer(&garmin_data_p->timer, (1)+jiffies);
  1162. } else {
  1163. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1164. garmin_data_p->flags &= ~FLAGS_QUEUING;
  1165. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1166. }
  1167. }
  1168. return 0;
  1169. }
  1170. static void garmin_throttle(struct tty_struct *tty)
  1171. {
  1172. struct usb_serial_port *port = tty->driver_data;
  1173. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  1174. unsigned long flags;
  1175. dbg("%s - port %d", __func__, port->number);
  1176. /* set flag, data received will be put into a queue
  1177. for later processing */
  1178. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1179. garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
  1180. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1181. }
  1182. static void garmin_unthrottle(struct tty_struct *tty)
  1183. {
  1184. struct usb_serial_port *port = tty->driver_data;
  1185. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  1186. unsigned long flags;
  1187. int status;
  1188. dbg("%s - port %d", __func__, port->number);
  1189. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1190. garmin_data_p->flags &= ~FLAGS_THROTTLED;
  1191. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1192. /* in native mode send queued data to tty, in
  1193. serial mode nothing needs to be done here */
  1194. if (garmin_data_p->mode == MODE_NATIVE)
  1195. garmin_flush_queue(garmin_data_p);
  1196. if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
  1197. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1198. if (status)
  1199. dev_err(&port->dev,
  1200. "%s - failed resubmitting read urb, error %d\n",
  1201. __func__, status);
  1202. }
  1203. }
  1204. /*
  1205. * The timer is currently only used to send queued packets to
  1206. * the tty in cases where the protocol provides no own handshaking
  1207. * to initiate the transfer.
  1208. */
  1209. static void timeout_handler(unsigned long data)
  1210. {
  1211. struct garmin_data *garmin_data_p = (struct garmin_data *) data;
  1212. /* send the next queued packet to the tty port */
  1213. if (garmin_data_p->mode == MODE_NATIVE)
  1214. if (garmin_data_p->flags & FLAGS_QUEUING)
  1215. garmin_flush_queue(garmin_data_p);
  1216. }
  1217. static int garmin_attach(struct usb_serial *serial)
  1218. {
  1219. int status = 0;
  1220. struct usb_serial_port *port = serial->port[0];
  1221. struct garmin_data *garmin_data_p = NULL;
  1222. dbg("%s", __func__);
  1223. garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
  1224. if (garmin_data_p == NULL) {
  1225. dev_err(&port->dev, "%s - Out of memory\n", __func__);
  1226. return -ENOMEM;
  1227. }
  1228. init_timer(&garmin_data_p->timer);
  1229. spin_lock_init(&garmin_data_p->lock);
  1230. INIT_LIST_HEAD(&garmin_data_p->pktlist);
  1231. /* garmin_data_p->timer.expires = jiffies + session_timeout; */
  1232. garmin_data_p->timer.data = (unsigned long)garmin_data_p;
  1233. garmin_data_p->timer.function = timeout_handler;
  1234. garmin_data_p->port = port;
  1235. garmin_data_p->state = 0;
  1236. garmin_data_p->count = 0;
  1237. usb_set_serial_port_data(port, garmin_data_p);
  1238. status = garmin_init_session(port);
  1239. return status;
  1240. }
  1241. static void garmin_shutdown(struct usb_serial *serial)
  1242. {
  1243. struct usb_serial_port *port = serial->port[0];
  1244. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  1245. dbg("%s", __func__);
  1246. usb_kill_urb(port->interrupt_in_urb);
  1247. del_timer_sync(&garmin_data_p->timer);
  1248. kfree(garmin_data_p);
  1249. usb_set_serial_port_data(port, NULL);
  1250. }
  1251. /* All of the device info needed */
  1252. static struct usb_serial_driver garmin_device = {
  1253. .driver = {
  1254. .owner = THIS_MODULE,
  1255. .name = "garmin_gps",
  1256. },
  1257. .description = "Garmin GPS usb/tty",
  1258. .usb_driver = &garmin_driver,
  1259. .id_table = id_table,
  1260. .num_ports = 1,
  1261. .open = garmin_open,
  1262. .close = garmin_close,
  1263. .throttle = garmin_throttle,
  1264. .unthrottle = garmin_unthrottle,
  1265. .attach = garmin_attach,
  1266. .shutdown = garmin_shutdown,
  1267. .write = garmin_write,
  1268. .write_room = garmin_write_room,
  1269. .write_bulk_callback = garmin_write_bulk_callback,
  1270. .read_bulk_callback = garmin_read_bulk_callback,
  1271. .read_int_callback = garmin_read_int_callback,
  1272. };
  1273. static int __init garmin_init(void)
  1274. {
  1275. int retval;
  1276. retval = usb_serial_register(&garmin_device);
  1277. if (retval)
  1278. goto failed_garmin_register;
  1279. retval = usb_register(&garmin_driver);
  1280. if (retval)
  1281. goto failed_usb_register;
  1282. info(DRIVER_DESC " " DRIVER_VERSION);
  1283. return 0;
  1284. failed_usb_register:
  1285. usb_serial_deregister(&garmin_device);
  1286. failed_garmin_register:
  1287. return retval;
  1288. }
  1289. static void __exit garmin_exit(void)
  1290. {
  1291. usb_deregister(&garmin_driver);
  1292. usb_serial_deregister(&garmin_device);
  1293. }
  1294. module_init(garmin_init);
  1295. module_exit(garmin_exit);
  1296. MODULE_AUTHOR(DRIVER_AUTHOR);
  1297. MODULE_DESCRIPTION(DRIVER_DESC);
  1298. MODULE_LICENSE("GPL");
  1299. module_param(debug, bool, S_IWUSR | S_IRUGO);
  1300. MODULE_PARM_DESC(debug, "Debug enabled or not");
  1301. module_param(initial_mode, int, S_IRUGO);
  1302. MODULE_PARM_DESC(initial_mode, "Initial mode");