garmin_gps.c 39 KB

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