garmin_gps.c 38 KB

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