garmin_gps.c 42 KB

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