garmin_gps.c 37 KB

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