garmin_gps.c 41 KB

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