garmin_gps.c 42 KB

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