garmin_gps.c 39 KB

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