garmin_gps.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  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, struct pt_regs *regs)
  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. /* free up the transfer buffer, as usb_free_urb() does not do this */
  826. kfree (urb->transfer_buffer);
  827. dbg("%s - port %d", __FUNCTION__, port->number);
  828. if (urb->status) {
  829. dbg("%s - nonzero write bulk status received: %d",
  830. __FUNCTION__, urb->status);
  831. spin_lock_irqsave(&garmin_data_p->lock, flags);
  832. garmin_data_p->flags |= CLEAR_HALT_REQUIRED;
  833. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  834. }
  835. usb_serial_port_softint(port);
  836. }
  837. static int garmin_write_bulk (struct usb_serial_port *port,
  838. const unsigned char *buf, int count)
  839. {
  840. unsigned long flags;
  841. struct usb_serial *serial = port->serial;
  842. struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
  843. struct urb *urb;
  844. unsigned char *buffer;
  845. int status;
  846. dbg("%s - port %d, state %d", __FUNCTION__, port->number,
  847. garmin_data_p->state);
  848. spin_lock_irqsave(&garmin_data_p->lock, flags);
  849. garmin_data_p->flags &= ~FLAGS_DROP_DATA;
  850. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  851. buffer = kmalloc (count, GFP_ATOMIC);
  852. if (!buffer) {
  853. dev_err(&port->dev, "out of memory\n");
  854. return -ENOMEM;
  855. }
  856. urb = usb_alloc_urb(0, GFP_ATOMIC);
  857. if (!urb) {
  858. dev_err(&port->dev, "no more free urbs\n");
  859. kfree (buffer);
  860. return -ENOMEM;
  861. }
  862. memcpy (buffer, buf, count);
  863. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
  864. usb_fill_bulk_urb (urb, serial->dev,
  865. usb_sndbulkpipe (serial->dev,
  866. port->bulk_out_endpointAddress),
  867. buffer, count,
  868. garmin_write_bulk_callback, port);
  869. urb->transfer_flags |= URB_ZERO_PACKET;
  870. if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
  871. spin_lock_irqsave(&garmin_data_p->lock, flags);
  872. garmin_data_p->flags |= FLAGS_APP_REQ_SEEN;
  873. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  874. if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
  875. pkt_clear(garmin_data_p);
  876. garmin_data_p->state = STATE_GSP_WAIT_DATA;
  877. }
  878. }
  879. /* send it down the pipe */
  880. status = usb_submit_urb(urb, GFP_ATOMIC);
  881. if (status) {
  882. dev_err(&port->dev,
  883. "%s - usb_submit_urb(write bulk) "
  884. "failed with status = %d\n",
  885. __FUNCTION__, status);
  886. count = status;
  887. } else {
  888. if (GARMIN_LAYERID_APPL == getLayerId(buffer)
  889. && (garmin_data_p->mode == MODE_GARMIN_SERIAL)) {
  890. gsp_send_ack(garmin_data_p, buffer[4]);
  891. }
  892. }
  893. /* we are done with this urb, so let the host driver
  894. * really free it when it is finished with it */
  895. usb_free_urb (urb);
  896. return count;
  897. }
  898. static int garmin_write (struct usb_serial_port *port,
  899. const unsigned char *buf, int count)
  900. {
  901. unsigned long flags;
  902. int pktid, pktsiz, len;
  903. struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
  904. __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
  905. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buf);
  906. /* check for our private packets */
  907. if (count >= GARMIN_PKTHDR_LENGTH) {
  908. len = PRIVPKTSIZ;
  909. if (count < len)
  910. len = count;
  911. memcpy(garmin_data_p->privpkt, buf, len);
  912. pktsiz = getDataLength(garmin_data_p->privpkt);
  913. pktid = getPacketId(garmin_data_p->privpkt);
  914. if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
  915. && GARMIN_LAYERID_PRIVATE == getLayerId(garmin_data_p->privpkt)) {
  916. dbg("%s - processing private request %d",
  917. __FUNCTION__, pktid);
  918. // drop all unfinished transfers
  919. garmin_clear(garmin_data_p);
  920. switch(pktid) {
  921. case PRIV_PKTID_SET_DEBUG:
  922. if (pktsiz != 4)
  923. return -EINVPKT;
  924. debug = __le32_to_cpu(privpkt[3]);
  925. dbg("%s - debug level set to 0x%X",
  926. __FUNCTION__, debug);
  927. break;
  928. case PRIV_PKTID_SET_MODE:
  929. if (pktsiz != 4)
  930. return -EINVPKT;
  931. garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
  932. dbg("%s - mode set to %d",
  933. __FUNCTION__, garmin_data_p->mode);
  934. break;
  935. case PRIV_PKTID_INFO_REQ:
  936. priv_status_resp(port);
  937. break;
  938. case PRIV_PKTID_RESET_REQ:
  939. spin_lock_irqsave(&garmin_data_p->lock, flags);
  940. garmin_data_p->flags |= FLAGS_APP_REQ_SEEN;
  941. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  942. break;
  943. case PRIV_PKTID_SET_DEF_MODE:
  944. if (pktsiz != 4)
  945. return -EINVPKT;
  946. initial_mode = __le32_to_cpu(privpkt[3]);
  947. dbg("%s - initial_mode set to %d",
  948. __FUNCTION__,
  949. garmin_data_p->mode);
  950. break;
  951. }
  952. return count;
  953. }
  954. }
  955. garmin_data_p->ignorePkts = 0;
  956. if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
  957. return gsp_receive(garmin_data_p, buf, count);
  958. } else { /* MODE_NATIVE */
  959. return nat_receive(garmin_data_p, buf, count);
  960. }
  961. }
  962. static int garmin_write_room (struct usb_serial_port *port)
  963. {
  964. /*
  965. * Report back the bytes currently available in the output buffer.
  966. */
  967. struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
  968. return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
  969. }
  970. static int garmin_chars_in_buffer (struct usb_serial_port *port)
  971. {
  972. /*
  973. * Report back the number of bytes currently in our input buffer.
  974. * Will this lock up the driver - the buffer contains an incomplete
  975. * package which will not be written to the device until it
  976. * has been completed ?
  977. */
  978. //struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
  979. //return garmin_data_p->insize;
  980. return 0;
  981. }
  982. static void garmin_read_process(struct garmin_data * garmin_data_p,
  983. unsigned char *data, unsigned data_length)
  984. {
  985. unsigned long flags;
  986. if (garmin_data_p->flags & FLAGS_DROP_DATA) {
  987. /* abort-transfer cmd is actice */
  988. dbg("%s - pkt dropped", __FUNCTION__);
  989. } else if (garmin_data_p->state != STATE_DISCONNECTED &&
  990. garmin_data_p->state != STATE_RESET ) {
  991. /* remember any appl.layer packets, so we know
  992. if a reset is required or not when closing
  993. the device */
  994. if (0 == memcmp(data, GARMIN_APP_LAYER_REPLY,
  995. sizeof(GARMIN_APP_LAYER_REPLY))) {
  996. spin_lock_irqsave(&garmin_data_p->lock, flags);
  997. garmin_data_p->flags |= FLAGS_APP_RESP_SEEN;
  998. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  999. }
  1000. /* if throttling is active or postprecessing is required
  1001. put the received data in the input queue, otherwise
  1002. send it directly to the tty port */
  1003. if (garmin_data_p->flags & FLAGS_QUEUING) {
  1004. pkt_add(garmin_data_p, data, data_length);
  1005. } else if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
  1006. if (getLayerId(data) == GARMIN_LAYERID_APPL) {
  1007. pkt_add(garmin_data_p, data, data_length);
  1008. }
  1009. } else {
  1010. send_to_tty(garmin_data_p->port, data, data_length);
  1011. }
  1012. }
  1013. }
  1014. static void garmin_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
  1015. {
  1016. unsigned long flags;
  1017. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  1018. struct usb_serial *serial = port->serial;
  1019. struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
  1020. unsigned char *data = urb->transfer_buffer;
  1021. int status;
  1022. dbg("%s - port %d", __FUNCTION__, port->number);
  1023. if (!serial) {
  1024. dbg("%s - bad serial pointer, exiting", __FUNCTION__);
  1025. return;
  1026. }
  1027. if (urb->status) {
  1028. dbg("%s - nonzero read bulk status received: %d",
  1029. __FUNCTION__, urb->status);
  1030. return;
  1031. }
  1032. usb_serial_debug_data(debug, &port->dev,
  1033. __FUNCTION__, urb->actual_length, data);
  1034. garmin_read_process(garmin_data_p, data, urb->actual_length);
  1035. if (urb->actual_length == 0 &&
  1036. 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
  1037. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1038. garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
  1039. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1040. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1041. if (status)
  1042. dev_err(&port->dev,
  1043. "%s - failed resubmitting read urb, error %d\n",
  1044. __FUNCTION__, status);
  1045. } else if (urb->actual_length > 0) {
  1046. /* Continue trying to read until nothing more is received */
  1047. if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
  1048. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1049. if (status)
  1050. dev_err(&port->dev,
  1051. "%s - failed resubmitting read urb, error %d\n",
  1052. __FUNCTION__, status);
  1053. }
  1054. } else {
  1055. dbg("%s - end of bulk data", __FUNCTION__);
  1056. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1057. garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
  1058. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1059. }
  1060. return;
  1061. }
  1062. static void garmin_read_int_callback (struct urb *urb, struct pt_regs *regs)
  1063. {
  1064. unsigned long flags;
  1065. int status;
  1066. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  1067. struct usb_serial *serial = port->serial;
  1068. struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
  1069. unsigned char *data = urb->transfer_buffer;
  1070. switch (urb->status) {
  1071. case 0:
  1072. /* success */
  1073. break;
  1074. case -ECONNRESET:
  1075. case -ENOENT:
  1076. case -ESHUTDOWN:
  1077. /* this urb is terminated, clean up */
  1078. dbg("%s - urb shutting down with status: %d",
  1079. __FUNCTION__, urb->status);
  1080. return;
  1081. default:
  1082. dbg("%s - nonzero urb status received: %d",
  1083. __FUNCTION__, urb->status);
  1084. return;
  1085. }
  1086. usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
  1087. urb->actual_length, urb->transfer_buffer);
  1088. if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
  1089. 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
  1090. sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
  1091. dbg("%s - bulk data available.", __FUNCTION__);
  1092. if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
  1093. /* bulk data available */
  1094. usb_fill_bulk_urb (port->read_urb, serial->dev,
  1095. usb_rcvbulkpipe (serial->dev,
  1096. port->bulk_in_endpointAddress),
  1097. port->read_urb->transfer_buffer,
  1098. port->read_urb->transfer_buffer_length,
  1099. garmin_read_bulk_callback, port);
  1100. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1101. if (status) {
  1102. dev_err(&port->dev,
  1103. "%s - failed submitting read urb, error %d\n",
  1104. __FUNCTION__, status);
  1105. } else {
  1106. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1107. garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
  1108. /* do not send this packet to the user */
  1109. garmin_data_p->ignorePkts = 1;
  1110. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1111. }
  1112. } else {
  1113. /* bulk-in transfer still active */
  1114. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1115. garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
  1116. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1117. }
  1118. } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
  1119. && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
  1120. sizeof(GARMIN_START_SESSION_REPLY))) {
  1121. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1122. garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
  1123. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1124. /* save the serial number */
  1125. garmin_data_p->serial_num
  1126. = __le32_to_cpup((__le32*)(data+GARMIN_PKTHDR_LENGTH));
  1127. dbg("%s - start-of-session reply seen - serial %u.",
  1128. __FUNCTION__, garmin_data_p->serial_num);
  1129. }
  1130. if (garmin_data_p->ignorePkts) {
  1131. /* this reply belongs to a request generated by the driver,
  1132. ignore it. */
  1133. dbg("%s - pkt ignored (%d)",
  1134. __FUNCTION__, garmin_data_p->ignorePkts);
  1135. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1136. garmin_data_p->ignorePkts--;
  1137. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1138. } else {
  1139. garmin_read_process(garmin_data_p, data, urb->actual_length);
  1140. }
  1141. port->interrupt_in_urb->dev = port->serial->dev;
  1142. status = usb_submit_urb (urb, GFP_ATOMIC);
  1143. if (status)
  1144. dev_err(&urb->dev->dev,
  1145. "%s - Error %d submitting interrupt urb\n",
  1146. __FUNCTION__, status);
  1147. }
  1148. /*
  1149. * Sends the next queued packt to the tty port (garmin native mode only)
  1150. * and then sets a timer to call itself again until all queued data
  1151. * is sent.
  1152. */
  1153. static int garmin_flush_queue(struct garmin_data * garmin_data_p)
  1154. {
  1155. unsigned long flags;
  1156. struct garmin_packet *pkt;
  1157. if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
  1158. pkt = pkt_pop(garmin_data_p);
  1159. if (pkt != NULL) {
  1160. send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
  1161. kfree(pkt);
  1162. mod_timer(&garmin_data_p->timer, (1)+jiffies);
  1163. } else {
  1164. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1165. garmin_data_p->flags &= ~FLAGS_QUEUING;
  1166. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1167. }
  1168. }
  1169. return 0;
  1170. }
  1171. static void garmin_throttle (struct usb_serial_port *port)
  1172. {
  1173. unsigned long flags;
  1174. struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
  1175. dbg("%s - port %d", __FUNCTION__, port->number);
  1176. /* set flag, data received will be put into a queue
  1177. for later processing */
  1178. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1179. garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
  1180. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1181. }
  1182. static void garmin_unthrottle (struct usb_serial_port *port)
  1183. {
  1184. unsigned long flags;
  1185. struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
  1186. int status;
  1187. dbg("%s - port %d", __FUNCTION__, port->number);
  1188. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1189. garmin_data_p->flags &= ~FLAGS_THROTTLED;
  1190. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1191. /* in native mode send queued data to tty, in
  1192. serial mode nothing needs to be done here */
  1193. if (garmin_data_p->mode == MODE_NATIVE)
  1194. garmin_flush_queue(garmin_data_p);
  1195. if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
  1196. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1197. if (status)
  1198. dev_err(&port->dev,
  1199. "%s - failed resubmitting read urb, error %d\n",
  1200. __FUNCTION__, status);
  1201. }
  1202. }
  1203. /*
  1204. * The timer is currently only used to send queued packets to
  1205. * the tty in cases where the protocol provides no own handshaking
  1206. * to initiate the transfer.
  1207. */
  1208. static void timeout_handler(unsigned long data)
  1209. {
  1210. struct garmin_data *garmin_data_p = (struct garmin_data *) data;
  1211. /* send the next queued packet to the tty port */
  1212. if (garmin_data_p->mode == MODE_NATIVE)
  1213. if (garmin_data_p->flags & FLAGS_QUEUING)
  1214. garmin_flush_queue(garmin_data_p);
  1215. }
  1216. static int garmin_attach (struct usb_serial *serial)
  1217. {
  1218. int status = 0;
  1219. struct usb_serial_port *port = serial->port[0];
  1220. struct garmin_data * garmin_data_p = NULL;
  1221. dbg("%s", __FUNCTION__);
  1222. garmin_data_p = kmalloc (sizeof(struct garmin_data), GFP_KERNEL);
  1223. if (garmin_data_p == NULL) {
  1224. dev_err(&port->dev, "%s - Out of memory\n", __FUNCTION__);
  1225. return -ENOMEM;
  1226. }
  1227. memset (garmin_data_p, 0, sizeof(struct garmin_data));
  1228. init_timer(&garmin_data_p->timer);
  1229. spin_lock_init(&garmin_data_p->lock);
  1230. INIT_LIST_HEAD(&garmin_data_p->pktlist);
  1231. //garmin_data_p->timer.expires = jiffies + session_timeout;
  1232. garmin_data_p->timer.data = (unsigned long)garmin_data_p;
  1233. garmin_data_p->timer.function = timeout_handler;
  1234. garmin_data_p->port = port;
  1235. garmin_data_p->state = 0;
  1236. garmin_data_p->count = 0;
  1237. usb_set_serial_port_data(port, garmin_data_p);
  1238. status = garmin_init_session(port);
  1239. return status;
  1240. }
  1241. static void garmin_shutdown (struct usb_serial *serial)
  1242. {
  1243. struct usb_serial_port *port = serial->port[0];
  1244. struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
  1245. dbg("%s", __FUNCTION__);
  1246. usb_kill_urb (port->interrupt_in_urb);
  1247. del_timer_sync(&garmin_data_p->timer);
  1248. kfree (garmin_data_p);
  1249. usb_set_serial_port_data(port, NULL);
  1250. }
  1251. /* All of the device info needed */
  1252. static struct usb_serial_driver garmin_device = {
  1253. .driver = {
  1254. .owner = THIS_MODULE,
  1255. .name = "garmin_gps",
  1256. },
  1257. .description = "Garmin GPS usb/tty",
  1258. .id_table = id_table,
  1259. .num_interrupt_in = 1,
  1260. .num_bulk_in = 1,
  1261. .num_bulk_out = 1,
  1262. .num_ports = 1,
  1263. .open = garmin_open,
  1264. .close = garmin_close,
  1265. .throttle = garmin_throttle,
  1266. .unthrottle = garmin_unthrottle,
  1267. .attach = garmin_attach,
  1268. .shutdown = garmin_shutdown,
  1269. .write = garmin_write,
  1270. .write_room = garmin_write_room,
  1271. .chars_in_buffer = garmin_chars_in_buffer,
  1272. .write_bulk_callback = garmin_write_bulk_callback,
  1273. .read_bulk_callback = garmin_read_bulk_callback,
  1274. .read_int_callback = garmin_read_int_callback,
  1275. };
  1276. static int __init garmin_init (void)
  1277. {
  1278. int retval;
  1279. retval = usb_serial_register(&garmin_device);
  1280. if (retval)
  1281. goto failed_garmin_register;
  1282. retval = usb_register(&garmin_driver);
  1283. if (retval)
  1284. goto failed_usb_register;
  1285. info(DRIVER_DESC " " DRIVER_VERSION);
  1286. return 0;
  1287. failed_usb_register:
  1288. usb_serial_deregister(&garmin_device);
  1289. failed_garmin_register:
  1290. return retval;
  1291. }
  1292. static void __exit garmin_exit (void)
  1293. {
  1294. usb_deregister (&garmin_driver);
  1295. usb_serial_deregister (&garmin_device);
  1296. }
  1297. module_init(garmin_init);
  1298. module_exit(garmin_exit);
  1299. MODULE_AUTHOR( DRIVER_AUTHOR );
  1300. MODULE_DESCRIPTION( DRIVER_DESC );
  1301. MODULE_LICENSE("GPL");
  1302. module_param(debug, bool, S_IWUSR | S_IRUGO);
  1303. MODULE_PARM_DESC(debug, "Debug enabled or not");
  1304. module_param(initial_mode, int, S_IRUGO);
  1305. MODULE_PARM_DESC(initial_mode, "Initial mode");