garmin_gps.c 42 KB

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