garmin_gps.c 40 KB

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