garmin_gps.c 39 KB

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