garmin_gps.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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(debug, &port->dev,
  211. __func__, actual_length, data);
  212. tty_insert_flip_string(tty, data, actual_length);
  213. tty_flip_buffer_push(tty);
  214. }
  215. tty_kref_put(tty);
  216. }
  217. /******************************************************************************
  218. * packet queue handling
  219. ******************************************************************************/
  220. /*
  221. * queue a received (usb-)packet for later processing
  222. */
  223. static int pkt_add(struct garmin_data *garmin_data_p,
  224. unsigned char *data, unsigned int data_length)
  225. {
  226. int state = 0;
  227. int result = 0;
  228. unsigned long flags;
  229. struct garmin_packet *pkt;
  230. /* process only packets containg data ... */
  231. if (data_length) {
  232. pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
  233. GFP_ATOMIC);
  234. if (pkt == NULL) {
  235. dev_err(&garmin_data_p->port->dev, "out of memory\n");
  236. return 0;
  237. }
  238. pkt->size = data_length;
  239. memcpy(pkt->data, data, data_length);
  240. spin_lock_irqsave(&garmin_data_p->lock, flags);
  241. garmin_data_p->flags |= FLAGS_QUEUING;
  242. result = list_empty(&garmin_data_p->pktlist);
  243. pkt->seq = garmin_data_p->seq_counter++;
  244. list_add_tail(&pkt->list, &garmin_data_p->pktlist);
  245. state = garmin_data_p->state;
  246. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  247. dbg("%s - added: pkt: %d - %d bytes",
  248. __func__, 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. dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id);
  293. *ptr++ = DLE;
  294. *ptr++ = ACK;
  295. cksum += ACK;
  296. *ptr++ = 2;
  297. cksum += 2;
  298. *ptr++ = pkt_id;
  299. cksum += pkt_id;
  300. if (pkt_id == DLE)
  301. *ptr++ = DLE;
  302. *ptr++ = 0;
  303. *ptr++ = 0xFF & (-cksum);
  304. *ptr++ = DLE;
  305. *ptr++ = ETX;
  306. l = ptr-pkt;
  307. send_to_tty(garmin_data_p->port, pkt, l);
  308. return 0;
  309. }
  310. /*
  311. * called for a complete packet received from tty layer
  312. *
  313. * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
  314. * at GSP_INITIAL_OFFSET.
  315. *
  316. * count - number of bytes in the input buffer including space reserved for
  317. * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
  318. * (including pkt-id, data-length a. cksum)
  319. */
  320. static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
  321. {
  322. unsigned long flags;
  323. const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
  324. __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
  325. int cksum = 0;
  326. int n = 0;
  327. int pktid = recpkt[0];
  328. int size = recpkt[1];
  329. usb_serial_debug_data(debug, &garmin_data_p->port->dev,
  330. __func__, count-GSP_INITIAL_OFFSET, recpkt);
  331. if (size != (count-GSP_INITIAL_OFFSET-3)) {
  332. dbg("%s - invalid size, expected %d bytes, got %d",
  333. __func__, size, (count-GSP_INITIAL_OFFSET-3));
  334. return -EINVPKT;
  335. }
  336. cksum += *recpkt++;
  337. cksum += *recpkt++;
  338. /* sanity check, remove after test ... */
  339. if ((__u8 *)&(usbdata[3]) != recpkt) {
  340. dbg("%s - ptr mismatch %p - %p",
  341. __func__, &(usbdata[4]), recpkt);
  342. return -EINVPKT;
  343. }
  344. while (n < size) {
  345. cksum += *recpkt++;
  346. n++;
  347. }
  348. if ((0xff & (cksum + *recpkt)) != 0) {
  349. dbg("%s - invalid checksum, expected %02x, got %02x",
  350. __func__, 0xff & -cksum, 0xff & *recpkt);
  351. return -EINVPKT;
  352. }
  353. usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
  354. usbdata[1] = __cpu_to_le32(pktid);
  355. usbdata[2] = __cpu_to_le32(size);
  356. garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
  357. GARMIN_PKTHDR_LENGTH+size, 0);
  358. /* if this was an abort-transfer command, flush all
  359. queued data. */
  360. if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
  361. spin_lock_irqsave(&garmin_data_p->lock, flags);
  362. garmin_data_p->flags |= FLAGS_DROP_DATA;
  363. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  364. pkt_clear(garmin_data_p);
  365. }
  366. return count;
  367. }
  368. /*
  369. * Called for data received from tty
  370. *
  371. * buf contains the data read, it may span more than one packet or even
  372. * incomplete packets
  373. *
  374. * input record should be a serial-record, but it may not be complete.
  375. * Copy it into our local buffer, until an etx is seen (or an error
  376. * occurs).
  377. * Once the record is complete, convert into a usb packet and send it
  378. * to the bulk pipe, send an ack back to the tty.
  379. *
  380. * If the input is an ack, just send the last queued packet to the
  381. * tty layer.
  382. *
  383. * if the input is an abort command, drop all queued data.
  384. */
  385. static int gsp_receive(struct garmin_data *garmin_data_p,
  386. const unsigned char *buf, int count)
  387. {
  388. unsigned long flags;
  389. int offs = 0;
  390. int ack_or_nak_seen = 0;
  391. __u8 *dest;
  392. int size;
  393. /* dleSeen: set if last byte read was a DLE */
  394. int dleSeen;
  395. /* skip: if set, skip incoming data until possible start of
  396. * new packet
  397. */
  398. int skip;
  399. __u8 data;
  400. spin_lock_irqsave(&garmin_data_p->lock, flags);
  401. dest = garmin_data_p->inbuffer;
  402. size = garmin_data_p->insize;
  403. dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
  404. skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
  405. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  406. /* dbg("%s - dle=%d skip=%d size=%d count=%d",
  407. __func__, dleSeen, skip, size, count); */
  408. if (size == 0)
  409. size = GSP_INITIAL_OFFSET;
  410. while (offs < count) {
  411. data = *(buf+offs);
  412. offs++;
  413. if (data == DLE) {
  414. if (skip) { /* start of a new pkt */
  415. skip = 0;
  416. size = GSP_INITIAL_OFFSET;
  417. dleSeen = 1;
  418. } else if (dleSeen) {
  419. dest[size++] = data;
  420. dleSeen = 0;
  421. } else {
  422. dleSeen = 1;
  423. }
  424. } else if (data == ETX) {
  425. if (dleSeen) {
  426. /* packet complete */
  427. data = dest[GSP_INITIAL_OFFSET];
  428. if (data == ACK) {
  429. ack_or_nak_seen = ACK;
  430. dbg("ACK packet complete.");
  431. } else if (data == NAK) {
  432. ack_or_nak_seen = NAK;
  433. dbg("NAK packet complete.");
  434. } else {
  435. dbg("packet complete - id=0x%X.",
  436. 0xFF & data);
  437. gsp_rec_packet(garmin_data_p, size);
  438. }
  439. skip = 1;
  440. size = GSP_INITIAL_OFFSET;
  441. dleSeen = 0;
  442. } else {
  443. dest[size++] = data;
  444. }
  445. } else if (!skip) {
  446. if (dleSeen) {
  447. size = GSP_INITIAL_OFFSET;
  448. dleSeen = 0;
  449. }
  450. dest[size++] = data;
  451. }
  452. if (size >= GPS_IN_BUFSIZ) {
  453. dbg("%s - packet too large.", __func__);
  454. skip = 1;
  455. size = GSP_INITIAL_OFFSET;
  456. dleSeen = 0;
  457. }
  458. }
  459. spin_lock_irqsave(&garmin_data_p->lock, flags);
  460. garmin_data_p->insize = size;
  461. /* copy flags back to structure */
  462. if (skip)
  463. garmin_data_p->flags |= FLAGS_GSP_SKIP;
  464. else
  465. garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
  466. if (dleSeen)
  467. garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
  468. else
  469. garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
  470. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  471. if (ack_or_nak_seen) {
  472. if (gsp_next_packet(garmin_data_p) > 0)
  473. garmin_data_p->state = STATE_ACTIVE;
  474. else
  475. garmin_data_p->state = STATE_GSP_WAIT_DATA;
  476. }
  477. return count;
  478. }
  479. /*
  480. * Sends a usb packet to the tty
  481. *
  482. * Assumes, that all packages and at an usb-packet boundary.
  483. *
  484. * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
  485. */
  486. static int gsp_send(struct garmin_data *garmin_data_p,
  487. const unsigned char *buf, int count)
  488. {
  489. const unsigned char *src;
  490. unsigned char *dst;
  491. int pktid = 0;
  492. int datalen = 0;
  493. int cksum = 0;
  494. int i = 0;
  495. int k;
  496. dbg("%s - state %d - %d bytes.", __func__,
  497. garmin_data_p->state, count);
  498. k = garmin_data_p->outsize;
  499. if ((k+count) > GPS_OUT_BUFSIZ) {
  500. dbg("packet too large");
  501. garmin_data_p->outsize = 0;
  502. return -4;
  503. }
  504. memcpy(garmin_data_p->outbuffer+k, buf, count);
  505. k += count;
  506. garmin_data_p->outsize = k;
  507. if (k >= GARMIN_PKTHDR_LENGTH) {
  508. pktid = getPacketId(garmin_data_p->outbuffer);
  509. datalen = getDataLength(garmin_data_p->outbuffer);
  510. i = GARMIN_PKTHDR_LENGTH + datalen;
  511. if (k < i)
  512. return 0;
  513. } else {
  514. return 0;
  515. }
  516. dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, k, i);
  517. /* garmin_data_p->outbuffer now contains a complete packet */
  518. usb_serial_debug_data(debug, &garmin_data_p->port->dev,
  519. __func__, k, garmin_data_p->outbuffer);
  520. garmin_data_p->outsize = 0;
  521. if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
  522. dbg("not an application packet (%d)",
  523. getLayerId(garmin_data_p->outbuffer));
  524. return -1;
  525. }
  526. if (pktid > 255) {
  527. dbg("packet-id %d too large", pktid);
  528. return -2;
  529. }
  530. if (datalen > 255) {
  531. dbg("packet-size %d too large", datalen);
  532. return -3;
  533. }
  534. /* the serial protocol should be able to handle this packet */
  535. k = 0;
  536. src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
  537. for (i = 0; i < datalen; i++) {
  538. if (*src++ == DLE)
  539. k++;
  540. }
  541. src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
  542. if (k > (GARMIN_PKTHDR_LENGTH-2)) {
  543. /* can't add stuffing DLEs in place, move data to end
  544. of buffer ... */
  545. dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
  546. memcpy(dst, src, datalen);
  547. src = dst;
  548. }
  549. dst = garmin_data_p->outbuffer;
  550. *dst++ = DLE;
  551. *dst++ = pktid;
  552. cksum += pktid;
  553. *dst++ = datalen;
  554. cksum += datalen;
  555. if (datalen == DLE)
  556. *dst++ = DLE;
  557. for (i = 0; i < datalen; i++) {
  558. __u8 c = *src++;
  559. *dst++ = c;
  560. cksum += c;
  561. if (c == DLE)
  562. *dst++ = DLE;
  563. }
  564. cksum = 0xFF & -cksum;
  565. *dst++ = cksum;
  566. if (cksum == DLE)
  567. *dst++ = DLE;
  568. *dst++ = DLE;
  569. *dst++ = ETX;
  570. i = dst-garmin_data_p->outbuffer;
  571. send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
  572. garmin_data_p->pkt_id = pktid;
  573. garmin_data_p->state = STATE_WAIT_TTY_ACK;
  574. return i;
  575. }
  576. /*
  577. * Process the next pending data packet - if there is one
  578. */
  579. static int gsp_next_packet(struct garmin_data *garmin_data_p)
  580. {
  581. int result = 0;
  582. struct garmin_packet *pkt = NULL;
  583. while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
  584. dbg("%s - next pkt: %d", __func__, pkt->seq);
  585. result = gsp_send(garmin_data_p, pkt->data, pkt->size);
  586. if (result > 0) {
  587. kfree(pkt);
  588. return result;
  589. }
  590. kfree(pkt);
  591. }
  592. return result;
  593. }
  594. /******************************************************************************
  595. * garmin native mode
  596. ******************************************************************************/
  597. /*
  598. * Called for data received from tty
  599. *
  600. * The input data is expected to be in garmin usb-packet format.
  601. *
  602. * buf contains the data read, it may span more than one packet
  603. * or even incomplete packets
  604. */
  605. static int nat_receive(struct garmin_data *garmin_data_p,
  606. const unsigned char *buf, int count)
  607. {
  608. unsigned long flags;
  609. __u8 *dest;
  610. int offs = 0;
  611. int result = count;
  612. int len;
  613. while (offs < count) {
  614. /* if buffer contains header, copy rest of data */
  615. if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
  616. len = GARMIN_PKTHDR_LENGTH
  617. +getDataLength(garmin_data_p->inbuffer);
  618. else
  619. len = GARMIN_PKTHDR_LENGTH;
  620. if (len >= GPS_IN_BUFSIZ) {
  621. /* seems to be an invalid packet, ignore rest
  622. of input */
  623. dbg("%s - packet size too large: %d", __func__, len);
  624. garmin_data_p->insize = 0;
  625. count = 0;
  626. result = -EINVPKT;
  627. } else {
  628. len -= garmin_data_p->insize;
  629. if (len > (count-offs))
  630. len = (count-offs);
  631. if (len > 0) {
  632. dest = garmin_data_p->inbuffer
  633. + garmin_data_p->insize;
  634. memcpy(dest, buf+offs, len);
  635. garmin_data_p->insize += len;
  636. offs += len;
  637. }
  638. }
  639. /* do we have a complete packet ? */
  640. if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
  641. len = GARMIN_PKTHDR_LENGTH+
  642. getDataLength(garmin_data_p->inbuffer);
  643. if (garmin_data_p->insize >= len) {
  644. garmin_write_bulk(garmin_data_p->port,
  645. garmin_data_p->inbuffer,
  646. len, 0);
  647. garmin_data_p->insize = 0;
  648. /* if this was an abort-transfer command,
  649. flush all queued data. */
  650. if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
  651. spin_lock_irqsave(&garmin_data_p->lock,
  652. flags);
  653. garmin_data_p->flags |= FLAGS_DROP_DATA;
  654. spin_unlock_irqrestore(
  655. &garmin_data_p->lock, flags);
  656. pkt_clear(garmin_data_p);
  657. }
  658. }
  659. }
  660. }
  661. return result;
  662. }
  663. /******************************************************************************
  664. * private packets
  665. ******************************************************************************/
  666. static void priv_status_resp(struct usb_serial_port *port)
  667. {
  668. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  669. __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
  670. pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
  671. pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
  672. pkt[2] = __cpu_to_le32(12);
  673. pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
  674. pkt[4] = __cpu_to_le32(garmin_data_p->mode);
  675. pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
  676. send_to_tty(port, (__u8 *)pkt, 6 * 4);
  677. }
  678. /******************************************************************************
  679. * Garmin specific driver functions
  680. ******************************************************************************/
  681. static int process_resetdev_request(struct usb_serial_port *port)
  682. {
  683. unsigned long flags;
  684. int status;
  685. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  686. spin_lock_irqsave(&garmin_data_p->lock, flags);
  687. garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
  688. garmin_data_p->state = STATE_RESET;
  689. garmin_data_p->serial_num = 0;
  690. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  691. usb_kill_urb(port->interrupt_in_urb);
  692. dbg("%s - usb_reset_device", __func__);
  693. status = usb_reset_device(port->serial->dev);
  694. if (status)
  695. dbg("%s - usb_reset_device failed: %d",
  696. __func__, status);
  697. return status;
  698. }
  699. /*
  700. * clear all cached data
  701. */
  702. static int garmin_clear(struct garmin_data *garmin_data_p)
  703. {
  704. unsigned long flags;
  705. int status = 0;
  706. /* flush all queued data */
  707. pkt_clear(garmin_data_p);
  708. spin_lock_irqsave(&garmin_data_p->lock, flags);
  709. garmin_data_p->insize = 0;
  710. garmin_data_p->outsize = 0;
  711. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  712. return status;
  713. }
  714. static int garmin_init_session(struct usb_serial_port *port)
  715. {
  716. struct usb_serial *serial = port->serial;
  717. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  718. int status = 0;
  719. int i = 0;
  720. if (status == 0) {
  721. usb_kill_urb(port->interrupt_in_urb);
  722. dbg("%s - adding interrupt input", __func__);
  723. status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  724. if (status)
  725. dev_err(&serial->dev->dev,
  726. "%s - failed submitting interrupt urb, error %d\n",
  727. __func__, status);
  728. }
  729. /*
  730. * using the initialization method from gpsbabel. See comments in
  731. * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
  732. */
  733. if (status == 0) {
  734. dbg("%s - starting session ...", __func__);
  735. garmin_data_p->state = STATE_ACTIVE;
  736. for (i = 0; i < 3; i++) {
  737. status = garmin_write_bulk(port,
  738. GARMIN_START_SESSION_REQ,
  739. sizeof(GARMIN_START_SESSION_REQ), 0);
  740. if (status < 0)
  741. break;
  742. }
  743. if (status > 0)
  744. status = 0;
  745. }
  746. return status;
  747. }
  748. static int garmin_open(struct tty_struct *tty, struct usb_serial_port *port)
  749. {
  750. unsigned long flags;
  751. int status = 0;
  752. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  753. spin_lock_irqsave(&garmin_data_p->lock, flags);
  754. garmin_data_p->mode = initial_mode;
  755. garmin_data_p->count = 0;
  756. garmin_data_p->flags &= FLAGS_SESSION_REPLY1_SEEN;
  757. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  758. /* shutdown any bulk reads that might be going on */
  759. usb_kill_urb(port->write_urb);
  760. usb_kill_urb(port->read_urb);
  761. if (garmin_data_p->state == STATE_RESET)
  762. status = garmin_init_session(port);
  763. garmin_data_p->state = STATE_ACTIVE;
  764. return status;
  765. }
  766. static void garmin_close(struct usb_serial_port *port)
  767. {
  768. struct usb_serial *serial = port->serial;
  769. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  770. dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__,
  771. port->number, garmin_data_p->mode,
  772. garmin_data_p->state, garmin_data_p->flags);
  773. if (!serial)
  774. return;
  775. mutex_lock(&port->serial->disc_mutex);
  776. if (!port->serial->disconnected)
  777. garmin_clear(garmin_data_p);
  778. /* shutdown our urbs */
  779. usb_kill_urb(port->read_urb);
  780. usb_kill_urb(port->write_urb);
  781. /* keep reset state so we know that we must start a new session */
  782. if (garmin_data_p->state != STATE_RESET)
  783. garmin_data_p->state = STATE_DISCONNECTED;
  784. mutex_unlock(&port->serial->disc_mutex);
  785. }
  786. static void garmin_write_bulk_callback(struct urb *urb)
  787. {
  788. struct usb_serial_port *port = urb->context;
  789. if (port) {
  790. struct garmin_data *garmin_data_p =
  791. usb_get_serial_port_data(port);
  792. if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)) {
  793. if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
  794. gsp_send_ack(garmin_data_p,
  795. ((__u8 *)urb->transfer_buffer)[4]);
  796. }
  797. }
  798. usb_serial_port_softint(port);
  799. }
  800. /* Ignore errors that resulted from garmin_write_bulk with
  801. dismiss_ack = 1 */
  802. /* free up the transfer buffer, as usb_free_urb() does not do this */
  803. kfree(urb->transfer_buffer);
  804. }
  805. static int garmin_write_bulk(struct usb_serial_port *port,
  806. const unsigned char *buf, int count,
  807. int dismiss_ack)
  808. {
  809. unsigned long flags;
  810. struct usb_serial *serial = port->serial;
  811. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  812. struct urb *urb;
  813. unsigned char *buffer;
  814. int status;
  815. spin_lock_irqsave(&garmin_data_p->lock, flags);
  816. garmin_data_p->flags &= ~FLAGS_DROP_DATA;
  817. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  818. buffer = kmalloc(count, GFP_ATOMIC);
  819. if (!buffer) {
  820. dev_err(&port->dev, "out of memory\n");
  821. return -ENOMEM;
  822. }
  823. urb = usb_alloc_urb(0, GFP_ATOMIC);
  824. if (!urb) {
  825. dev_err(&port->dev, "no more free urbs\n");
  826. kfree(buffer);
  827. return -ENOMEM;
  828. }
  829. memcpy(buffer, buf, count);
  830. usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
  831. usb_fill_bulk_urb(urb, serial->dev,
  832. usb_sndbulkpipe(serial->dev,
  833. port->bulk_out_endpointAddress),
  834. buffer, count,
  835. garmin_write_bulk_callback,
  836. dismiss_ack ? NULL : port);
  837. urb->transfer_flags |= URB_ZERO_PACKET;
  838. if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
  839. spin_lock_irqsave(&garmin_data_p->lock, flags);
  840. garmin_data_p->flags |= APP_REQ_SEEN;
  841. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  842. if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
  843. pkt_clear(garmin_data_p);
  844. garmin_data_p->state = STATE_GSP_WAIT_DATA;
  845. }
  846. }
  847. /* send it down the pipe */
  848. status = usb_submit_urb(urb, GFP_ATOMIC);
  849. if (status) {
  850. dev_err(&port->dev,
  851. "%s - usb_submit_urb(write bulk) failed with status = %d\n",
  852. __func__, status);
  853. count = status;
  854. }
  855. /* we are done with this urb, so let the host driver
  856. * really free it when it is finished with it */
  857. usb_free_urb(urb);
  858. return count;
  859. }
  860. static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
  861. const unsigned char *buf, int count)
  862. {
  863. int pktid, pktsiz, len;
  864. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  865. __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
  866. usb_serial_debug_data(debug, &port->dev, __func__, count, buf);
  867. if (garmin_data_p->state == STATE_RESET)
  868. return -EIO;
  869. /* check for our private packets */
  870. if (count >= GARMIN_PKTHDR_LENGTH) {
  871. len = PRIVPKTSIZ;
  872. if (count < len)
  873. len = count;
  874. memcpy(garmin_data_p->privpkt, buf, len);
  875. pktsiz = getDataLength(garmin_data_p->privpkt);
  876. pktid = getPacketId(garmin_data_p->privpkt);
  877. if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
  878. && GARMIN_LAYERID_PRIVATE ==
  879. getLayerId(garmin_data_p->privpkt)) {
  880. dbg("%s - processing private request %d",
  881. __func__, pktid);
  882. /* drop all unfinished transfers */
  883. garmin_clear(garmin_data_p);
  884. switch (pktid) {
  885. case PRIV_PKTID_SET_DEBUG:
  886. if (pktsiz != 4)
  887. return -EINVPKT;
  888. debug = __le32_to_cpu(privpkt[3]);
  889. dbg("%s - debug level set to 0x%X",
  890. __func__, debug);
  891. break;
  892. case PRIV_PKTID_SET_MODE:
  893. if (pktsiz != 4)
  894. return -EINVPKT;
  895. garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
  896. dbg("%s - mode set to %d",
  897. __func__, garmin_data_p->mode);
  898. break;
  899. case PRIV_PKTID_INFO_REQ:
  900. priv_status_resp(port);
  901. break;
  902. case PRIV_PKTID_RESET_REQ:
  903. process_resetdev_request(port);
  904. break;
  905. case PRIV_PKTID_SET_DEF_MODE:
  906. if (pktsiz != 4)
  907. return -EINVPKT;
  908. initial_mode = __le32_to_cpu(privpkt[3]);
  909. dbg("%s - initial_mode set to %d",
  910. __func__,
  911. garmin_data_p->mode);
  912. break;
  913. }
  914. return count;
  915. }
  916. }
  917. if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
  918. return gsp_receive(garmin_data_p, buf, count);
  919. } else { /* MODE_NATIVE */
  920. return nat_receive(garmin_data_p, buf, count);
  921. }
  922. }
  923. static int garmin_write_room(struct tty_struct *tty)
  924. {
  925. struct usb_serial_port *port = tty->driver_data;
  926. /*
  927. * Report back the bytes currently available in the output buffer.
  928. */
  929. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  930. return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
  931. }
  932. static void garmin_read_process(struct garmin_data *garmin_data_p,
  933. unsigned char *data, unsigned data_length,
  934. int bulk_data)
  935. {
  936. unsigned long flags;
  937. if (garmin_data_p->flags & FLAGS_DROP_DATA) {
  938. /* abort-transfer cmd is actice */
  939. dbg("%s - pkt dropped", __func__);
  940. } else if (garmin_data_p->state != STATE_DISCONNECTED &&
  941. garmin_data_p->state != STATE_RESET) {
  942. /* if throttling is active or postprecessing is required
  943. put the received data in the input queue, otherwise
  944. send it directly to the tty port */
  945. if (garmin_data_p->flags & FLAGS_QUEUING) {
  946. pkt_add(garmin_data_p, data, data_length);
  947. } else if (bulk_data ||
  948. getLayerId(data) == GARMIN_LAYERID_APPL) {
  949. spin_lock_irqsave(&garmin_data_p->lock, flags);
  950. garmin_data_p->flags |= APP_RESP_SEEN;
  951. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  952. if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
  953. pkt_add(garmin_data_p, data, data_length);
  954. } else {
  955. send_to_tty(garmin_data_p->port, data,
  956. data_length);
  957. }
  958. }
  959. /* ignore system layer packets ... */
  960. }
  961. }
  962. static void garmin_read_bulk_callback(struct urb *urb)
  963. {
  964. unsigned long flags;
  965. struct usb_serial_port *port = urb->context;
  966. struct usb_serial *serial = port->serial;
  967. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  968. unsigned char *data = urb->transfer_buffer;
  969. int status = urb->status;
  970. int retval;
  971. if (!serial) {
  972. dbg("%s - bad serial pointer, exiting", __func__);
  973. return;
  974. }
  975. if (status) {
  976. dbg("%s - nonzero read bulk status received: %d",
  977. __func__, status);
  978. return;
  979. }
  980. usb_serial_debug_data(debug, &port->dev,
  981. __func__, urb->actual_length, data);
  982. garmin_read_process(garmin_data_p, data, urb->actual_length, 1);
  983. if (urb->actual_length == 0 &&
  984. 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
  985. spin_lock_irqsave(&garmin_data_p->lock, flags);
  986. garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
  987. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  988. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  989. if (retval)
  990. dev_err(&port->dev,
  991. "%s - failed resubmitting read urb, error %d\n",
  992. __func__, retval);
  993. } else if (urb->actual_length > 0) {
  994. /* Continue trying to read until nothing more is received */
  995. if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
  996. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  997. if (retval)
  998. dev_err(&port->dev,
  999. "%s - failed resubmitting read urb, "
  1000. "error %d\n", __func__, retval);
  1001. }
  1002. } else {
  1003. dbg("%s - end of bulk data", __func__);
  1004. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1005. garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
  1006. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1007. }
  1008. }
  1009. static void garmin_read_int_callback(struct urb *urb)
  1010. {
  1011. unsigned long flags;
  1012. int retval;
  1013. struct usb_serial_port *port = urb->context;
  1014. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  1015. unsigned char *data = urb->transfer_buffer;
  1016. int status = urb->status;
  1017. switch (status) {
  1018. case 0:
  1019. /* success */
  1020. break;
  1021. case -ECONNRESET:
  1022. case -ENOENT:
  1023. case -ESHUTDOWN:
  1024. /* this urb is terminated, clean up */
  1025. dbg("%s - urb shutting down with status: %d",
  1026. __func__, status);
  1027. return;
  1028. default:
  1029. dbg("%s - nonzero urb status received: %d",
  1030. __func__, status);
  1031. return;
  1032. }
  1033. usb_serial_debug_data(debug, &port->dev, __func__,
  1034. urb->actual_length, urb->transfer_buffer);
  1035. if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
  1036. 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
  1037. sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
  1038. dbg("%s - bulk data available.", __func__);
  1039. if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
  1040. /* bulk data available */
  1041. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1042. if (retval) {
  1043. dev_err(&port->dev,
  1044. "%s - failed submitting read urb, error %d\n",
  1045. __func__, retval);
  1046. } else {
  1047. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1048. garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
  1049. spin_unlock_irqrestore(&garmin_data_p->lock,
  1050. flags);
  1051. }
  1052. } else {
  1053. /* bulk-in transfer still active */
  1054. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1055. garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
  1056. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1057. }
  1058. } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
  1059. && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
  1060. sizeof(GARMIN_START_SESSION_REPLY))) {
  1061. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1062. garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
  1063. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1064. /* save the serial number */
  1065. garmin_data_p->serial_num = __le32_to_cpup(
  1066. (__le32 *)(data+GARMIN_PKTHDR_LENGTH));
  1067. dbg("%s - start-of-session reply seen - serial %u.",
  1068. __func__, garmin_data_p->serial_num);
  1069. }
  1070. garmin_read_process(garmin_data_p, data, urb->actual_length, 0);
  1071. retval = usb_submit_urb(urb, GFP_ATOMIC);
  1072. if (retval)
  1073. dev_err(&urb->dev->dev,
  1074. "%s - Error %d submitting interrupt urb\n",
  1075. __func__, retval);
  1076. }
  1077. /*
  1078. * Sends the next queued packt to the tty port (garmin native mode only)
  1079. * and then sets a timer to call itself again until all queued data
  1080. * is sent.
  1081. */
  1082. static int garmin_flush_queue(struct garmin_data *garmin_data_p)
  1083. {
  1084. unsigned long flags;
  1085. struct garmin_packet *pkt;
  1086. if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
  1087. pkt = pkt_pop(garmin_data_p);
  1088. if (pkt != NULL) {
  1089. send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
  1090. kfree(pkt);
  1091. mod_timer(&garmin_data_p->timer, (1)+jiffies);
  1092. } else {
  1093. spin_lock_irqsave(&garmin_data_p->lock, flags);
  1094. garmin_data_p->flags &= ~FLAGS_QUEUING;
  1095. spin_unlock_irqrestore(&garmin_data_p->lock, flags);
  1096. }
  1097. }
  1098. return 0;
  1099. }
  1100. static void garmin_throttle(struct tty_struct *tty)
  1101. {
  1102. struct usb_serial_port *port = tty->driver_data;
  1103. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  1104. /* set flag, data received will be put into a queue
  1105. for later processing */
  1106. spin_lock_irq(&garmin_data_p->lock);
  1107. garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
  1108. spin_unlock_irq(&garmin_data_p->lock);
  1109. }
  1110. static void garmin_unthrottle(struct tty_struct *tty)
  1111. {
  1112. struct usb_serial_port *port = tty->driver_data;
  1113. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  1114. int status;
  1115. spin_lock_irq(&garmin_data_p->lock);
  1116. garmin_data_p->flags &= ~FLAGS_THROTTLED;
  1117. spin_unlock_irq(&garmin_data_p->lock);
  1118. /* in native mode send queued data to tty, in
  1119. serial mode nothing needs to be done here */
  1120. if (garmin_data_p->mode == MODE_NATIVE)
  1121. garmin_flush_queue(garmin_data_p);
  1122. if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
  1123. status = usb_submit_urb(port->read_urb, GFP_KERNEL);
  1124. if (status)
  1125. dev_err(&port->dev,
  1126. "%s - failed resubmitting read urb, error %d\n",
  1127. __func__, status);
  1128. }
  1129. }
  1130. /*
  1131. * The timer is currently only used to send queued packets to
  1132. * the tty in cases where the protocol provides no own handshaking
  1133. * to initiate the transfer.
  1134. */
  1135. static void timeout_handler(unsigned long data)
  1136. {
  1137. struct garmin_data *garmin_data_p = (struct garmin_data *) data;
  1138. /* send the next queued packet to the tty port */
  1139. if (garmin_data_p->mode == MODE_NATIVE)
  1140. if (garmin_data_p->flags & FLAGS_QUEUING)
  1141. garmin_flush_queue(garmin_data_p);
  1142. }
  1143. static int garmin_attach(struct usb_serial *serial)
  1144. {
  1145. int status = 0;
  1146. struct usb_serial_port *port = serial->port[0];
  1147. struct garmin_data *garmin_data_p = NULL;
  1148. garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
  1149. if (garmin_data_p == NULL) {
  1150. dev_err(&port->dev, "%s - Out of memory\n", __func__);
  1151. return -ENOMEM;
  1152. }
  1153. init_timer(&garmin_data_p->timer);
  1154. spin_lock_init(&garmin_data_p->lock);
  1155. INIT_LIST_HEAD(&garmin_data_p->pktlist);
  1156. /* garmin_data_p->timer.expires = jiffies + session_timeout; */
  1157. garmin_data_p->timer.data = (unsigned long)garmin_data_p;
  1158. garmin_data_p->timer.function = timeout_handler;
  1159. garmin_data_p->port = port;
  1160. garmin_data_p->state = 0;
  1161. garmin_data_p->flags = 0;
  1162. garmin_data_p->count = 0;
  1163. usb_set_serial_port_data(port, garmin_data_p);
  1164. status = garmin_init_session(port);
  1165. return status;
  1166. }
  1167. static void garmin_disconnect(struct usb_serial *serial)
  1168. {
  1169. struct usb_serial_port *port = serial->port[0];
  1170. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  1171. usb_kill_urb(port->interrupt_in_urb);
  1172. del_timer_sync(&garmin_data_p->timer);
  1173. }
  1174. static void garmin_release(struct usb_serial *serial)
  1175. {
  1176. struct usb_serial_port *port = serial->port[0];
  1177. struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
  1178. kfree(garmin_data_p);
  1179. }
  1180. /* All of the device info needed */
  1181. static struct usb_serial_driver garmin_device = {
  1182. .driver = {
  1183. .owner = THIS_MODULE,
  1184. .name = "garmin_gps",
  1185. },
  1186. .description = "Garmin GPS usb/tty",
  1187. .id_table = id_table,
  1188. .num_ports = 1,
  1189. .open = garmin_open,
  1190. .close = garmin_close,
  1191. .throttle = garmin_throttle,
  1192. .unthrottle = garmin_unthrottle,
  1193. .attach = garmin_attach,
  1194. .disconnect = garmin_disconnect,
  1195. .release = garmin_release,
  1196. .write = garmin_write,
  1197. .write_room = garmin_write_room,
  1198. .write_bulk_callback = garmin_write_bulk_callback,
  1199. .read_bulk_callback = garmin_read_bulk_callback,
  1200. .read_int_callback = garmin_read_int_callback,
  1201. };
  1202. static struct usb_serial_driver * const serial_drivers[] = {
  1203. &garmin_device, NULL
  1204. };
  1205. module_usb_serial_driver(serial_drivers, id_table);
  1206. MODULE_AUTHOR(DRIVER_AUTHOR);
  1207. MODULE_DESCRIPTION(DRIVER_DESC);
  1208. MODULE_LICENSE("GPL");
  1209. module_param(debug, bool, S_IWUSR | S_IRUGO);
  1210. MODULE_PARM_DESC(debug, "Debug enabled or not");
  1211. module_param(initial_mode, int, S_IRUGO);
  1212. MODULE_PARM_DESC(initial_mode, "Initial mode");