garmin_gps.c 39 KB

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