garmin_gps.c 39 KB

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