garmin_gps.c 39 KB

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