garmin_gps.c 39 KB

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