garmin_gps.c 40 KB

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