230-pps_support.patch 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152
  1. --- /dev/null
  2. +++ b/Documentation/pps/Makefile
  3. @@ -0,0 +1,27 @@
  4. +TARGETS = ppstest ppsctl
  5. +
  6. +CFLAGS += -Wall -O2 -D_GNU_SOURCE
  7. +CFLAGS += -I .
  8. +CFLAGS += -ggdb
  9. +
  10. +# -- Actions section ----------------------------------------------------------
  11. +
  12. +.PHONY : all depend dep
  13. +
  14. +all : .depend $(TARGETS)
  15. +
  16. +.depend depend dep :
  17. + $(CC) $(CFLAGS) -M $(TARGETS:=.c) > .depend
  18. +
  19. +ifeq (.depend,$(wildcard .depend))
  20. +include .depend
  21. +endif
  22. +
  23. +
  24. +# -- Clean section ------------------------------------------------------------
  25. +
  26. +.PHONY : clean
  27. +
  28. +clean :
  29. + rm -f *.o *~ core .depend
  30. + rm -f ${TARGETS}
  31. --- /dev/null
  32. +++ b/Documentation/pps/pps.txt
  33. @@ -0,0 +1,170 @@
  34. +
  35. + PPS - Pulse Per Second
  36. + ----------------------
  37. +
  38. +(C) Copyright 2007 Rodolfo Giometti <[email protected]>
  39. +
  40. +This program is free software; you can redistribute it and/or modify
  41. +it under the terms of the GNU General Public License as published by
  42. +the Free Software Foundation; either version 2 of the License, or
  43. +(at your option) any later version.
  44. +
  45. +This program is distributed in the hope that it will be useful,
  46. +but WITHOUT ANY WARRANTY; without even the implied warranty of
  47. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  48. +GNU General Public License for more details.
  49. +
  50. +
  51. +
  52. +Overview
  53. +--------
  54. +
  55. +LinuxPPS provides a programming interface (API) to define into the
  56. +system several PPS sources.
  57. +
  58. +PPS means "pulse per second" and a PPS source is just a device which
  59. +provides a high precision signal each second so that an application
  60. +can use it to adjust system clock time.
  61. +
  62. +A PPS source can be connected to a serial port (usually to the Data
  63. +Carrier Detect pin) or to a parallel port (ACK-pin) or to a special
  64. +CPU's GPIOs (this is the common case in embedded systems) but in each
  65. +case when a new pulse comes the system must apply to it a timestamp
  66. +and record it for the userland.
  67. +
  68. +Common use is the combination of the NTPD as userland program with a
  69. +GPS receiver as PPS source to obtain a wallclock-time with
  70. +sub-millisecond synchronisation to UTC.
  71. +
  72. +
  73. +RFC considerations
  74. +------------------
  75. +
  76. +While implementing a PPS API as RFC 2783 defines and using an embedded
  77. +CPU GPIO-Pin as physical link to the signal, I encountered a deeper
  78. +problem:
  79. +
  80. + At startup it needs a file descriptor as argument for the function
  81. + time_pps_create().
  82. +
  83. +This implies that the source has a /dev/... entry. This assumption is
  84. +ok for the serial and parallel port, where you can do something
  85. +useful besides(!) the gathering of timestamps as it is the central
  86. +task for a PPS-API. But this assumption does not work for a single
  87. +purpose GPIO line. In this case even basic file-related functionality
  88. +(like read() and write()) makes no sense at all and should not be a
  89. +precondition for the use of a PPS-API.
  90. +
  91. +The problem can be simply solved if you consider that a PPS source is
  92. +not always connected with a GPS data source.
  93. +
  94. +So your programs should check if the GPS data source (the serial port
  95. +for instance) is a PPS source too, otherwise they should provide the
  96. +possibility to open another device as PPS source.
  97. +
  98. +In LinuxPPS the PPS sources are simply char devices usually mapped
  99. +into files /dev/pps0, /dev/pps1, etc..
  100. +
  101. +
  102. +Coding example
  103. +--------------
  104. +
  105. +To register a PPS source into the kernel you should define a struct
  106. +pps_source_info_s as follow:
  107. +
  108. + static struct pps_source_info_s pps_ktimer_info = {
  109. + name : "ktimer",
  110. + path : "",
  111. + mode : PPS_CAPTUREASSERT | PPS_OFFSETASSERT | \
  112. + PPS_ECHOASSERT | \
  113. + PPS_CANWAIT | PPS_TSFMT_TSPEC,
  114. + echo : pps_ktimer_echo,
  115. + owner : THIS_MODULE,
  116. + };
  117. +
  118. +and then calling the function pps_register_source() in your
  119. +intialization routine as follow:
  120. +
  121. + source = pps_register_source(&pps_ktimer_info,
  122. + PPS_CAPTUREASSERT | PPS_OFFSETASSERT);
  123. +
  124. +The pps_register_source() prototype is:
  125. +
  126. + int pps_register_source(struct pps_source_info_s *info, int default_params)
  127. +
  128. +where "info" is a pointer to a structure that describes a particular
  129. +PPS source, "default_params" tells the system what the initial default
  130. +parameters for the device should be (is obvious that these parameters
  131. +must be a subset of ones defined into the struct
  132. +pps_source_info_s which describe the capabilities of the driver).
  133. +
  134. +Once you have registered a new PPS source into the system you can
  135. +signal an assert event (for example in the interrupt handler routine)
  136. +just using:
  137. +
  138. + pps_event(source, PPS_CAPTUREASSERT, ptr);
  139. +
  140. +The same function may also run the defined echo function
  141. +(pps_ktimer_echo(), passing to it the "ptr" pointer) if the user
  142. +asked for that... etc..
  143. +
  144. +Please see the file drivers/pps/clients/ktimer.c for an example code.
  145. +
  146. +
  147. +SYSFS support
  148. +-------------
  149. +
  150. +If the SYSFS filesystem is enabled in the kernel it provides a new class:
  151. +
  152. + $ ls /sys/class/pps/
  153. + pps0/ pps1/ pps2/
  154. +
  155. +Every directory is the ID of a PPS sources defined into the system and
  156. +inside you find several files:
  157. +
  158. + $ ls /sys/class/pps/pps0/
  159. + assert clear echo mode name path subsystem@ uevent
  160. +
  161. +Inside each "assert" and "clear" file you can find the timestamp and a
  162. +sequence number:
  163. +
  164. + $ cat /sys/class/pps/pps0/assert
  165. + 1170026870.983207967#8
  166. +
  167. +Where before the "#" is the timestamp in seconds and after it is the
  168. +sequence number. Other files are:
  169. +
  170. +* echo: reports if the PPS source has an echo function or not;
  171. +
  172. +* mode: reports available PPS functioning modes;
  173. +
  174. +* name: reports the PPS source's name;
  175. +
  176. +* path: reports the PPS source's device path, that is the device the
  177. + PPS source is connected to (if it exists).
  178. +
  179. +
  180. +Testing the PPS support
  181. +-----------------------
  182. +
  183. +In order to test the PPS support even without specific hardware you can use
  184. +the ktimer driver (see the client subsection in the PPS configuration menu)
  185. +and the userland tools provided into Documentaion/pps/ directory.
  186. +
  187. +Once you have enabled the compilation of ktimer just modprobe it (if
  188. +not statically compiled):
  189. +
  190. + # modprobe ktimer
  191. +
  192. +and the run ppstest as follow:
  193. +
  194. + $ ./ppstest /dev/pps0
  195. + trying PPS source "/dev/pps1"
  196. + found PPS source "/dev/pps1"
  197. + ok, found 1 source(s), now start fetching data...
  198. + source 0 - assert 1186592699.388832443, sequence: 364 - clear 0.000000000, sequence: 0
  199. + source 0 - assert 1186592700.388931295, sequence: 365 - clear 0.000000000, sequence: 0
  200. + source 0 - assert 1186592701.389032765, sequence: 366 - clear 0.000000000, sequence: 0
  201. +
  202. +Please, note that to compile userland programs you need the file timepps.h
  203. +(see Documentation/pps/).
  204. --- /dev/null
  205. +++ b/Documentation/pps/ppsctl.c
  206. @@ -0,0 +1,62 @@
  207. +#include <stdio.h>
  208. +#include <unistd.h>
  209. +#include <stdlib.h>
  210. +#include <errno.h>
  211. +#include <sys/ioctl.h>
  212. +#include <sys/types.h>
  213. +#include <sys/stat.h>
  214. +#include <fcntl.h>
  215. +#include <string.h>
  216. +#include <linux/serial.h>
  217. +
  218. +void usage(char *name)
  219. +{
  220. + fprintf(stderr, "usage: %s <ttyS> [enable|disable]\n", name);
  221. +
  222. + exit(EXIT_FAILURE);
  223. +}
  224. +
  225. +int main(int argc, char *argv[])
  226. +{
  227. + int fd;
  228. + int ret;
  229. + struct serial_struct ss;
  230. +
  231. + if (argc < 2)
  232. + usage(argv[0]);
  233. +
  234. + fd = open(argv[1], O_RDWR);
  235. + if (fd < 0) {
  236. + perror("open");
  237. + exit(EXIT_FAILURE);
  238. + }
  239. +
  240. + ret = ioctl(fd, TIOCGSERIAL, &ss);
  241. + if (ret < 0) {
  242. + perror("ioctl(TIOCGSERIAL)");
  243. + exit(EXIT_FAILURE);
  244. + }
  245. +
  246. + if (argc < 3) { /* just read PPS status */
  247. + printf("PPS is %sabled\n",
  248. + ss.flags & ASYNC_HARDPPS_CD ? "en" : "dis");
  249. + exit(EXIT_SUCCESS);
  250. + }
  251. +
  252. + if (argv[2][0] == 'e' || argv[2][0] == '1')
  253. + ss.flags |= ASYNC_HARDPPS_CD;
  254. + else if (argv[2][0] == 'd' || argv[2][0] == '0')
  255. + ss.flags &= ~ASYNC_HARDPPS_CD;
  256. + else {
  257. + fprintf(stderr, "invalid state argument \"%s\"\n", argv[2]);
  258. + exit(EXIT_FAILURE);
  259. + }
  260. +
  261. + ret = ioctl(fd, TIOCSSERIAL, &ss);
  262. + if (ret < 0) {
  263. + perror("ioctl(TIOCSSERIAL)");
  264. + exit(EXIT_FAILURE);
  265. + }
  266. +
  267. + return 0;
  268. +}
  269. --- /dev/null
  270. +++ b/Documentation/pps/ppsfind
  271. @@ -0,0 +1,17 @@
  272. +#!/bin/sh
  273. +
  274. +SYS="/sys/class/pps/"
  275. +
  276. +if [ $# -lt 1 ] ; then
  277. + echo "usage: ppsfind <name>" >&2
  278. + exit 1
  279. +fi
  280. +
  281. +for d in $(ls $SYS) ; do
  282. + if grep $1 $SYS/$d/name >& /dev/null || \
  283. + grep $1 $SYS/$d/path >& /dev/null ; then
  284. + echo "$d: name=$(cat $SYS/$d/name) path=$(cat $SYS/$d/path)"
  285. + fi
  286. +done
  287. +
  288. +exit 0
  289. --- /dev/null
  290. +++ b/Documentation/pps/ppstest.c
  291. @@ -0,0 +1,151 @@
  292. +#include <stdio.h>
  293. +#include <stdlib.h>
  294. +#include <unistd.h>
  295. +#include <errno.h>
  296. +#include <string.h>
  297. +#include <sys/types.h>
  298. +#include <sys/stat.h>
  299. +#include <fcntl.h>
  300. +
  301. +#include <timepps.h>
  302. +
  303. +int find_source(char *path, pps_handle_t *handle, int *avail_mode)
  304. +{
  305. + pps_params_t params;
  306. + int ret;
  307. +
  308. + printf("trying PPS source \"%s\"\n", path);
  309. +
  310. + /* Try to find the source by using the supplied "path" name */
  311. + ret = open(path, O_RDWR);
  312. + if (ret < 0) {
  313. + fprintf(stderr, "unable to open device \"%s\" (%m)\n", path);
  314. + return ret;
  315. + }
  316. +
  317. + /* Open the PPS source (and check the file descriptor) */
  318. + ret = time_pps_create(ret, handle);
  319. + if (ret < 0) {
  320. + fprintf(stderr, "cannot create a PPS source from device "
  321. + "\"%s\" (%m)\n", path);
  322. + return -1;
  323. + }
  324. + printf("found PPS source \"%s\"\n", path);
  325. +
  326. + /* Find out what features are supported */
  327. + ret = time_pps_getcap(*handle, avail_mode);
  328. + if (ret < 0) {
  329. + fprintf(stderr, "cannot get capabilities (%m)\n");
  330. + return -1;
  331. + }
  332. + if ((*avail_mode & PPS_CAPTUREASSERT) == 0) {
  333. + fprintf(stderr, "cannot CAPTUREASSERT\n");
  334. + return -1;
  335. + }
  336. + if ((*avail_mode & PPS_OFFSETASSERT) == 0) {
  337. + fprintf(stderr, "cannot OFFSETASSERT\n");
  338. + return -1;
  339. + }
  340. +
  341. + /* Capture assert timestamps, and compensate for a 675 nsec
  342. + * propagation delay */
  343. + ret = time_pps_getparams(*handle, &params);
  344. + if (ret < 0) {
  345. + fprintf(stderr, "cannot get parameters (%m)\n");
  346. + return -1;
  347. + }
  348. + params.assert_offset.tv_sec = 0;
  349. + params.assert_offset.tv_nsec = 675;
  350. + params.mode |= PPS_CAPTUREASSERT | PPS_OFFSETASSERT;
  351. + ret = time_pps_setparams(*handle, &params);
  352. + if (ret < 0) {
  353. + fprintf(stderr, "cannot set parameters (%m)\n");
  354. + return -1;
  355. + }
  356. +
  357. + return 0;
  358. +}
  359. +
  360. +int fetch_source(int i, pps_handle_t *handle, int *avail_mode)
  361. +{
  362. + struct timespec timeout;
  363. + pps_info_t infobuf;
  364. + int ret;
  365. +
  366. + /* create a zero-valued timeout */
  367. + timeout.tv_sec = 3;
  368. + timeout.tv_nsec = 0;
  369. +
  370. +retry:
  371. + if (*avail_mode & PPS_CANWAIT) /* waits for the next event */
  372. + ret = time_pps_fetch(*handle, PPS_TSFMT_TSPEC, &infobuf,
  373. + &timeout);
  374. + else {
  375. + sleep(1);
  376. + ret = time_pps_fetch(*handle, PPS_TSFMT_TSPEC, &infobuf,
  377. + &timeout);
  378. + }
  379. + if (ret < 0) {
  380. + if (ret == -EINTR) {
  381. + fprintf(stderr, "time_pps_fetch() got a signal!\n");
  382. + goto retry;
  383. + }
  384. +
  385. + fprintf(stderr, "time_pps_fetch() error %d (%m)\n", ret);
  386. + return -1;
  387. + }
  388. +
  389. + printf("source %d - "
  390. + "assert %ld.%09ld, sequence: %ld - "
  391. + "clear %ld.%09ld, sequence: %ld\n",
  392. + i,
  393. + infobuf.assert_timestamp.tv_sec,
  394. + infobuf.assert_timestamp.tv_nsec,
  395. + infobuf.assert_sequence,
  396. + infobuf.clear_timestamp.tv_sec,
  397. + infobuf.clear_timestamp.tv_nsec, infobuf.clear_sequence);
  398. +
  399. + return 0;
  400. +}
  401. +
  402. +void usage(char *name)
  403. +{
  404. + fprintf(stderr, "usage: %s <ppsdev> [<ppsdev> ...]\n", name);
  405. + exit(EXIT_FAILURE);
  406. +}
  407. +
  408. +int main(int argc, char *argv[])
  409. +{
  410. + int num;
  411. + pps_handle_t handle[4];
  412. + int avail_mode[4];
  413. + int i = 0;
  414. + int ret;
  415. +
  416. + /* Check the command line */
  417. + if (argc < 2)
  418. + usage(argv[0]);
  419. +
  420. + for (i = 1; i < argc && i <= 4; i++) {
  421. + ret = find_source(argv[i], &handle[i - 1], &avail_mode[i - 1]);
  422. + if (ret < 0)
  423. + exit(EXIT_FAILURE);
  424. + }
  425. +
  426. + num = i - 1;
  427. + printf("ok, found %d source(s), now start fetching data...\n", num);
  428. +
  429. + /* loop, printing the most recent timestamp every second or so */
  430. + while (1) {
  431. + for (i = 0; i < num; i++) {
  432. + ret = fetch_source(i, &handle[i], &avail_mode[i]);
  433. + if (ret < 0 && errno != ETIMEDOUT)
  434. + exit(EXIT_FAILURE);
  435. + }
  436. + }
  437. +
  438. + for (; i >= 0; i--)
  439. + time_pps_destroy(handle[i]);
  440. +
  441. + return 0;
  442. +}
  443. --- /dev/null
  444. +++ b/Documentation/pps/timepps.h
  445. @@ -0,0 +1,193 @@
  446. +/*
  447. + * timepps.h -- PPS API main header
  448. + *
  449. + * Copyright (C) 2005-2007 Rodolfo Giometti <[email protected]>
  450. + *
  451. + * This program is free software; you can redistribute it and/or modify
  452. + * it under the terms of the GNU General Public License as published by
  453. + * the Free Software Foundation; either version 2 of the License, or
  454. + * (at your option) any later version.
  455. + *
  456. + * This program is distributed in the hope that it will be useful,
  457. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  458. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  459. + * GNU General Public License for more details.
  460. + *
  461. + * You should have received a copy of the GNU General Public License
  462. + * along with this program; if not, write to the Free Software
  463. + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  464. + */
  465. +
  466. +#ifndef _SYS_TIMEPPS_H_
  467. +#define _SYS_TIMEPPS_H_
  468. +
  469. +#include <sys/syscall.h>
  470. +#include <unistd.h>
  471. +#include <errno.h>
  472. +#include <linux/pps.h>
  473. +
  474. +/*
  475. + * New data structures
  476. + */
  477. +
  478. +struct ntp_fp {
  479. + unsigned int integral;
  480. + unsigned int fractional;
  481. +};
  482. +
  483. +union pps_timeu {
  484. + struct timespec tspec;
  485. + struct ntp_fp ntpfp;
  486. + unsigned long longpad[3];
  487. +};
  488. +
  489. +struct pps_info {
  490. + unsigned long assert_sequence; /* seq. num. of assert event */
  491. + unsigned long clear_sequence; /* seq. num. of clear event */
  492. + union pps_timeu assert_tu; /* time of assert event */
  493. + union pps_timeu clear_tu; /* time of clear event */
  494. + int current_mode; /* current mode bits */
  495. +};
  496. +
  497. +struct pps_params {
  498. + int api_version; /* API version # */
  499. + int mode; /* mode bits */
  500. + union pps_timeu assert_off_tu; /* offset compensation for assert */
  501. + union pps_timeu clear_off_tu; /* offset compensation for clear */
  502. +};
  503. +
  504. +typedef int pps_handle_t; /* represents a PPS source */
  505. +typedef unsigned long pps_seq_t; /* sequence number */
  506. +typedef struct ntp_fp ntp_fp_t; /* NTP-compatible time stamp */
  507. +typedef union pps_timeu pps_timeu_t; /* generic data type for time stamps */
  508. +typedef struct pps_info pps_info_t;
  509. +typedef struct pps_params pps_params_t;
  510. +
  511. +#define assert_timestamp assert_tu.tspec
  512. +#define clear_timestamp clear_tu.tspec
  513. +
  514. +#define assert_timestamp_ntpfp assert_tu.ntpfp
  515. +#define clear_timestamp_ntpfp clear_tu.ntpfp
  516. +
  517. +#define assert_offset assert_off_tu.tspec
  518. +#define clear_offset clear_off_tu.tspec
  519. +
  520. +#define assert_offset_ntpfp assert_off_tu.ntpfp
  521. +#define clear_offset_ntpfp clear_off_tu.ntpfp
  522. +
  523. +/*
  524. + * The PPS API
  525. + */
  526. +
  527. +static __inline int time_pps_create(int source, pps_handle_t *handle)
  528. +{
  529. + int ret;
  530. +
  531. + if (!handle) {
  532. + errno = EINVAL;
  533. + return -1;
  534. + }
  535. +
  536. + /* First we check if current device is a PPS valid PPS one...
  537. + */
  538. + ret = ioctl(source, PPS_CHECK);
  539. + if (ret) {
  540. + errno = EOPNOTSUPP;
  541. + return -1;
  542. + }
  543. +
  544. + /* ... then since in LinuxPPS there are no differences between a
  545. + * "PPS source" and a "PPS handle", we simply return the same value.
  546. + */
  547. + *handle = source;
  548. +
  549. + return 0;
  550. +}
  551. +
  552. +static __inline int time_pps_destroy(pps_handle_t handle)
  553. +{
  554. + return close(handle);
  555. +}
  556. +
  557. +static __inline int time_pps_getparams(pps_handle_t handle,
  558. + pps_params_t *ppsparams)
  559. +{
  560. + int ret;
  561. + struct pps_kparams __ppsparams;
  562. +
  563. + ret = ioctl(handle, PPS_GETPARAMS, &__ppsparams);
  564. +
  565. + ppsparams->api_version = __ppsparams.api_version;
  566. + ppsparams->mode = __ppsparams.mode;
  567. + ppsparams->assert_off_tu.tspec.tv_sec = __ppsparams.assert_off_tu.sec;
  568. + ppsparams->assert_off_tu.tspec.tv_nsec = __ppsparams.assert_off_tu.nsec;
  569. + ppsparams->clear_off_tu.tspec.tv_sec = __ppsparams.clear_off_tu.sec;
  570. + ppsparams->clear_off_tu.tspec.tv_nsec = __ppsparams.clear_off_tu.nsec;
  571. +
  572. + return ret;
  573. +}
  574. +
  575. +static __inline int time_pps_setparams(pps_handle_t handle,
  576. + const pps_params_t *ppsparams)
  577. +{
  578. + struct pps_kparams __ppsparams;
  579. +
  580. + __ppsparams.api_version = ppsparams->api_version;
  581. + __ppsparams.mode = ppsparams->mode;
  582. + __ppsparams.assert_off_tu.sec = ppsparams->assert_off_tu.tspec.tv_sec;
  583. + __ppsparams.assert_off_tu.nsec = ppsparams->assert_off_tu.tspec.tv_nsec;
  584. + __ppsparams.clear_off_tu.sec = ppsparams->clear_off_tu.tspec.tv_sec;
  585. + __ppsparams.clear_off_tu.nsec = ppsparams->clear_off_tu.tspec.tv_nsec;
  586. +
  587. + return ioctl(handle, PPS_SETPARAMS, &__ppsparams);
  588. +}
  589. +
  590. +/* Get capabilities for handle */
  591. +static __inline int time_pps_getcap(pps_handle_t handle, int *mode)
  592. +{
  593. + return ioctl(handle, PPS_GETCAP, mode);
  594. +}
  595. +
  596. +static __inline int time_pps_fetch(pps_handle_t handle, const int tsformat,
  597. + pps_info_t *ppsinfobuf,
  598. + const struct timespec *timeout)
  599. +{
  600. + struct pps_fdata __fdata;
  601. + int ret;
  602. +
  603. + /* Sanity checks */
  604. + if (tsformat != PPS_TSFMT_TSPEC) {
  605. + errno = EINVAL;
  606. + return -1;
  607. + }
  608. +
  609. + if (timeout) {
  610. + __fdata.timeout.sec = timeout->tv_sec;
  611. + __fdata.timeout.nsec = timeout->tv_nsec;
  612. + __fdata.timeout.flags = ~PPS_TIME_INVALID;
  613. + } else
  614. + __fdata.timeout.flags = PPS_TIME_INVALID;
  615. +
  616. + ret = ioctl(handle, PPS_FETCH, &__fdata);
  617. +
  618. + ppsinfobuf->assert_sequence = __fdata.info.assert_sequence;
  619. + ppsinfobuf->clear_sequence = __fdata.info.clear_sequence;
  620. + ppsinfobuf->assert_tu.tspec.tv_sec = __fdata.info.assert_tu.sec;
  621. + ppsinfobuf->assert_tu.tspec.tv_nsec = __fdata.info.assert_tu.nsec;
  622. + ppsinfobuf->clear_tu.tspec.tv_sec = __fdata.info.clear_tu.sec;
  623. + ppsinfobuf->clear_tu.tspec.tv_nsec = __fdata.info.clear_tu.nsec;
  624. + ppsinfobuf->current_mode = __fdata.info.current_mode;
  625. +
  626. + return ret;
  627. +}
  628. +
  629. +static __inline int time_pps_kcbind(pps_handle_t handle,
  630. + const int kernel_consumer,
  631. + const int edge, const int tsformat)
  632. +{
  633. + /* LinuxPPS doesn't implement kernel consumer feature */
  634. + errno = EOPNOTSUPP;
  635. + return -1;
  636. +}
  637. +
  638. +#endif /* _SYS_TIMEPPS_H_ */
  639. --- a/MAINTAINERS
  640. +++ b/MAINTAINERS
  641. @@ -3011,6 +3011,13 @@ P: James Chapman
  642. M: [email protected]
  643. S: Maintained
  644. +PPS SUPPORT
  645. +P: Rodolfo Giometti
  646. +M: [email protected]
  647. +W: http://wiki.enneenne.com/index.php/LinuxPPS_support
  648. +L: [email protected]
  649. +S: Maintained
  650. +
  651. PREEMPTIBLE KERNEL
  652. P: Robert Love
  653. M: [email protected]
  654. --- a/drivers/Kconfig
  655. +++ b/drivers/Kconfig
  656. @@ -52,6 +52,8 @@ source "drivers/i2c/Kconfig"
  657. source "drivers/spi/Kconfig"
  658. +source "drivers/pps/Kconfig"
  659. +
  660. source "drivers/w1/Kconfig"
  661. source "drivers/power/Kconfig"
  662. --- a/drivers/Makefile
  663. +++ b/drivers/Makefile
  664. @@ -63,6 +63,7 @@ obj-$(CONFIG_INPUT) += input/
  665. obj-$(CONFIG_I2O) += message/
  666. obj-$(CONFIG_RTC_LIB) += rtc/
  667. obj-y += i2c/
  668. +obj-$(CONFIG_PPS) += pps/
  669. obj-$(CONFIG_W1) += w1/
  670. obj-$(CONFIG_POWER_SUPPLY) += power/
  671. obj-$(CONFIG_HWMON) += hwmon/
  672. --- a/drivers/char/lp.c
  673. +++ b/drivers/char/lp.c
  674. @@ -746,6 +746,27 @@ static struct console lpcons = {
  675. #endif /* console on line printer */
  676. +/* Support for PPS signal on the line printer */
  677. +
  678. +#ifdef CONFIG_PPS_CLIENT_LP
  679. +
  680. +static void lp_pps_echo(int source, int event, void *data)
  681. +{
  682. + struct parport *port = data;
  683. + unsigned char status = parport_read_status(port);
  684. +
  685. + /* echo event via SEL bit */
  686. + parport_write_control(port,
  687. + parport_read_control(port) | PARPORT_CONTROL_SELECT);
  688. +
  689. + /* signal no event */
  690. + if ((status & PARPORT_STATUS_ACK) != 0)
  691. + parport_write_control(port,
  692. + parport_read_control(port) & ~PARPORT_CONTROL_SELECT);
  693. +}
  694. +
  695. +#endif
  696. +
  697. /* --- initialisation code ------------------------------------- */
  698. static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
  699. @@ -817,6 +838,38 @@ static int lp_register(int nr, struct pa
  700. }
  701. #endif
  702. +#ifdef CONFIG_PPS_CLIENT_LP
  703. + port->pps_info.owner = THIS_MODULE;
  704. + port->pps_info.dev = port->dev;
  705. + snprintf(port->pps_info.path, PPS_MAX_NAME_LEN, "/dev/lp%d", nr);
  706. +
  707. + /* No PPS support if lp port has no IRQ line */
  708. + if (port->irq != PARPORT_IRQ_NONE) {
  709. + strncpy(port->pps_info.name, port->name, PPS_MAX_NAME_LEN);
  710. +
  711. + port->pps_info.mode = PPS_CAPTUREASSERT | PPS_OFFSETASSERT | \
  712. + PPS_ECHOASSERT | \
  713. + PPS_CANWAIT | PPS_TSFMT_TSPEC;
  714. +
  715. + port->pps_info.echo = lp_pps_echo;
  716. +
  717. + port->pps_source = pps_register_source(&(port->pps_info),
  718. + PPS_CAPTUREASSERT | PPS_OFFSETASSERT);
  719. + if (port->pps_source < 0)
  720. + dev_err(port->dev,
  721. + "cannot register PPS source \"%s\"\n",
  722. + port->pps_info.path);
  723. + else
  724. + dev_info(port->dev, "PPS source #%d \"%s\" added\n",
  725. + port->pps_source, port->pps_info.path);
  726. + } else {
  727. + port->pps_source = -1;
  728. + dev_err(port->dev, "PPS support disabled due port \"%s\" is "
  729. + "in polling mode\n",
  730. + port->pps_info.path);
  731. + }
  732. +#endif
  733. +
  734. return 0;
  735. }
  736. @@ -860,6 +913,14 @@ static void lp_detach (struct parport *p
  737. console_registered = NULL;
  738. }
  739. #endif /* CONFIG_LP_CONSOLE */
  740. +
  741. +#ifdef CONFIG_PPS_CLIENT_LP
  742. + if (port->pps_source >= 0) {
  743. + pps_unregister_source(port->pps_source);
  744. + dev_dbg(port->dev, "PPS source #%d \"%s\" removed\n",
  745. + port->pps_source, port->pps_info.path);
  746. + }
  747. +#endif
  748. }
  749. static struct parport_driver lp_driver = {
  750. --- /dev/null
  751. +++ b/drivers/pps/Kconfig
  752. @@ -0,0 +1,34 @@
  753. +#
  754. +# PPS support configuration
  755. +#
  756. +
  757. +menu "PPS support"
  758. +
  759. +config PPS
  760. + tristate "PPS support"
  761. + depends on EXPERIMENTAL
  762. + ---help---
  763. + PPS (Pulse Per Second) is a special pulse provided by some GPS
  764. + antennae. Userland can use it to get an high time reference.
  765. +
  766. + Some antennae's PPS signals are connected with the CD (Carrier
  767. + Detect) pin of the serial line they use to communicate with the
  768. + host. In this case use the SERIAL_LINE client support.
  769. +
  770. + Some antennae's PPS signals are connected with some special host
  771. + inputs so you have to enable the corresponding client support.
  772. +
  773. + To compile this driver as a module, choose M here: the module
  774. + will be called pps_core.ko.
  775. +
  776. +config PPS_DEBUG
  777. + bool "PPS debugging messages"
  778. + depends on PPS
  779. + help
  780. + Say Y here if you want the PPS support to produce a bunch of debug
  781. + messages to the system log. Select this if you are having a
  782. + problem with PPS support and want to see more of what is going on.
  783. +
  784. +source drivers/pps/clients/Kconfig
  785. +
  786. +endmenu
  787. --- /dev/null
  788. +++ b/drivers/pps/Makefile
  789. @@ -0,0 +1,11 @@
  790. +#
  791. +# Makefile for the PPS core.
  792. +#
  793. +
  794. +pps_core-objs += pps.o kapi.o sysfs.o
  795. +obj-$(CONFIG_PPS) += pps_core.o
  796. +obj-y += clients/
  797. +
  798. +ifeq ($(CONFIG_PPS_DEBUG),y)
  799. +EXTRA_CFLAGS += -DDEBUG
  800. +endif
  801. --- /dev/null
  802. +++ b/drivers/pps/clients/Kconfig
  803. @@ -0,0 +1,38 @@
  804. +#
  805. +# PPS clients configuration
  806. +#
  807. +
  808. +if PPS
  809. +
  810. +comment "PPS clients support"
  811. +
  812. +config PPS_CLIENT_KTIMER
  813. + tristate "Kernel timer client (Testing client, use for debug)"
  814. + help
  815. + If you say yes here you get support for a PPS debugging client
  816. + which uses a kernel timer to generate the PPS signal.
  817. +
  818. + This driver can also be built as a module. If so, the module
  819. + will be called ktimer.ko.
  820. +
  821. +comment "UART serial support (forced off)"
  822. + depends on ! (SERIAL_CORE != n && !(PPS = m && SERIAL_CORE = y))
  823. +
  824. +config PPS_CLIENT_UART
  825. + bool "UART serial support"
  826. + depends on SERIAL_CORE != n && !(PPS = m && SERIAL_CORE = y)
  827. + help
  828. + If you say yes here you get support for a PPS source connected
  829. + with the CD (Carrier Detect) pin of your serial port.
  830. +
  831. +comment "Parallel printer support (forced off)"
  832. + depends on !( PRINTER != n && !(PPS = m && PRINTER = y))
  833. +
  834. +config PPS_CLIENT_LP
  835. + bool "Parallel printer support"
  836. + depends on PRINTER != n && !(PPS = m && PRINTER = y)
  837. + help
  838. + If you say yes here you get support for a PPS source connected
  839. + with the interrupt pin of your parallel port.
  840. +
  841. +endif
  842. --- /dev/null
  843. +++ b/drivers/pps/clients/Makefile
  844. @@ -0,0 +1,9 @@
  845. +#
  846. +# Makefile for PPS clients.
  847. +#
  848. +
  849. +obj-$(CONFIG_PPS_CLIENT_KTIMER) += ktimer.o
  850. +
  851. +ifeq ($(CONFIG_PPS_DEBUG),y)
  852. +EXTRA_CFLAGS += -DDEBUG
  853. +endif
  854. --- /dev/null
  855. +++ b/drivers/pps/clients/ktimer.c
  856. @@ -0,0 +1,114 @@
  857. +/*
  858. + * ktimer.c -- kernel timer test client
  859. + *
  860. + *
  861. + * Copyright (C) 2005-2006 Rodolfo Giometti <[email protected]>
  862. + *
  863. + * This program is free software; you can redistribute it and/or modify
  864. + * it under the terms of the GNU General Public License as published by
  865. + * the Free Software Foundation; either version 2 of the License, or
  866. + * (at your option) any later version.
  867. + *
  868. + * This program is distributed in the hope that it will be useful,
  869. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  870. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  871. + * GNU General Public License for more details.
  872. + *
  873. + * You should have received a copy of the GNU General Public License
  874. + * along with this program; if not, write to the Free Software
  875. + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  876. + */
  877. +
  878. +
  879. +#include <linux/kernel.h>
  880. +#include <linux/module.h>
  881. +#include <linux/init.h>
  882. +#include <linux/time.h>
  883. +#include <linux/timer.h>
  884. +
  885. +#include <linux/pps.h>
  886. +
  887. +/*
  888. + * Global variables
  889. + */
  890. +
  891. +static int source;
  892. +static struct timer_list ktimer;
  893. +
  894. +/*
  895. + * The kernel timer
  896. + */
  897. +
  898. +static void pps_ktimer_event(unsigned long ptr)
  899. +{
  900. + pr_info("PPS event at %lu\n", jiffies);
  901. +
  902. + pps_event(source, PPS_CAPTUREASSERT, NULL);
  903. +
  904. + mod_timer(&ktimer, jiffies + HZ);
  905. +}
  906. +
  907. +/*
  908. + * The echo function
  909. + */
  910. +
  911. +static void pps_ktimer_echo(int source, int event, void *data)
  912. +{
  913. + pr_info("echo %s %s for source %d\n",
  914. + event & PPS_CAPTUREASSERT ? "assert" : "",
  915. + event & PPS_CAPTURECLEAR ? "clear" : "",
  916. + source);
  917. +}
  918. +
  919. +/*
  920. + * The PPS info struct
  921. + */
  922. +
  923. +static struct pps_source_info pps_ktimer_info = {
  924. + name : "ktimer",
  925. + path : "",
  926. + mode : PPS_CAPTUREASSERT | PPS_OFFSETASSERT | \
  927. + PPS_ECHOASSERT | \
  928. + PPS_CANWAIT | PPS_TSFMT_TSPEC,
  929. + echo : pps_ktimer_echo,
  930. + owner : THIS_MODULE,
  931. +};
  932. +
  933. +/*
  934. + * Module staff
  935. + */
  936. +
  937. +static void __exit pps_ktimer_exit(void)
  938. +{
  939. + del_timer_sync(&ktimer);
  940. + pps_unregister_source(source);
  941. +
  942. + pr_info("ktimer PPS source unregistered\n");
  943. +}
  944. +
  945. +static int __init pps_ktimer_init(void)
  946. +{
  947. + int ret;
  948. +
  949. + ret = pps_register_source(&pps_ktimer_info,
  950. + PPS_CAPTUREASSERT | PPS_OFFSETASSERT);
  951. + if (ret < 0) {
  952. + printk(KERN_ERR "cannot register ktimer source\n");
  953. + return ret;
  954. + }
  955. + source = ret;
  956. +
  957. + setup_timer(&ktimer, pps_ktimer_event, 0);
  958. + mod_timer(&ktimer, jiffies + HZ);
  959. +
  960. + pr_info("ktimer PPS source registered at %d\n", source);
  961. +
  962. + return 0;
  963. +}
  964. +
  965. +module_init(pps_ktimer_init);
  966. +module_exit(pps_ktimer_exit);
  967. +
  968. +MODULE_AUTHOR("Rodolfo Giometti <[email protected]>");
  969. +MODULE_DESCRIPTION("dummy PPS source by using a kernel timer (just for debug)");
  970. +MODULE_LICENSE("GPL");
  971. --- /dev/null
  972. +++ b/drivers/pps/kapi.c
  973. @@ -0,0 +1,271 @@
  974. +/*
  975. + * kapi.c -- kernel API
  976. + *
  977. + *
  978. + * Copyright (C) 2005-2007 Rodolfo Giometti <[email protected]>
  979. + *
  980. + * This program is free software; you can redistribute it and/or modify
  981. + * it under the terms of the GNU General Public License as published by
  982. + * the Free Software Foundation; either version 2 of the License, or
  983. + * (at your option) any later version.
  984. + *
  985. + * This program is distributed in the hope that it will be useful,
  986. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  987. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  988. + * GNU General Public License for more details.
  989. + *
  990. + * You should have received a copy of the GNU General Public License
  991. + * along with this program; if not, write to the Free Software
  992. + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  993. + */
  994. +
  995. +
  996. +#include <linux/kernel.h>
  997. +#include <linux/module.h>
  998. +#include <linux/init.h>
  999. +#include <linux/sched.h>
  1000. +#include <linux/time.h>
  1001. +#include <linux/spinlock.h>
  1002. +#include <linux/idr.h>
  1003. +#include <linux/fs.h>
  1004. +#include <linux/pps.h>
  1005. +
  1006. +/*
  1007. + * Local variables
  1008. + */
  1009. +
  1010. +static spinlock_t idr_lock = SPIN_LOCK_UNLOCKED;
  1011. +static DEFINE_IDR(pps_idr);
  1012. +
  1013. +/*
  1014. + * Local functions
  1015. + */
  1016. +
  1017. +static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset)
  1018. +{
  1019. + ts->nsec += offset->nsec;
  1020. + if (ts->nsec >= NSEC_PER_SEC) {
  1021. + ts->nsec -= NSEC_PER_SEC;
  1022. + ts->sec++;
  1023. + } else if (ts->nsec < 0) {
  1024. + ts->nsec += NSEC_PER_SEC;
  1025. + ts->sec--;
  1026. + }
  1027. + ts->sec += offset->sec;
  1028. +}
  1029. +
  1030. +/*
  1031. + * Exported functions
  1032. + */
  1033. +
  1034. +int pps_register_source(struct pps_source_info *info, int default_params)
  1035. +{
  1036. + struct pps_device *pps;
  1037. + int id;
  1038. + int err;
  1039. +
  1040. + /* Sanity checks */
  1041. + if ((info->mode & default_params) != default_params) {
  1042. + printk(KERN_ERR "pps: %s: unsupported default parameters\n",
  1043. + info->name);
  1044. + err = -EINVAL;
  1045. + goto pps_register_source_exit;
  1046. + }
  1047. + if ((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
  1048. + info->echo == NULL) {
  1049. + printk(KERN_ERR "pps: %s: echo function is not defined\n",
  1050. + info->name);
  1051. + err = -EINVAL;
  1052. + goto pps_register_source_exit;
  1053. + }
  1054. + if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
  1055. + printk(KERN_ERR "pps: %s: unspecified time format\n",
  1056. + info->name);
  1057. + err = -EINVAL;
  1058. + goto pps_register_source_exit;
  1059. + }
  1060. +
  1061. + /* Allocate memory for the new PPS source struct */
  1062. + pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
  1063. + if (pps == NULL) {
  1064. + err = -ENOMEM;
  1065. + goto pps_register_source_exit;
  1066. + }
  1067. +
  1068. + /* Get new ID for the new PPS source */
  1069. + if (idr_pre_get(&pps_idr, GFP_KERNEL) == 0) {
  1070. + err = -ENOMEM;
  1071. + goto kfree_pps;
  1072. + }
  1073. +
  1074. + spin_lock_irq(&idr_lock);
  1075. + err = idr_get_new(&pps_idr, pps, &id);
  1076. + spin_unlock_irq(&idr_lock);
  1077. +
  1078. + if (err < 0)
  1079. + goto kfree_pps;
  1080. +
  1081. + id = id & MAX_ID_MASK;
  1082. + if (id >= PPS_MAX_SOURCES) {
  1083. + printk(KERN_ERR "pps: %s: too much PPS sources in the system\n",
  1084. + info->name);
  1085. + err = -EBUSY;
  1086. + goto free_idr;
  1087. + }
  1088. +
  1089. + pps->id = id;
  1090. + pps->params.api_version = PPS_API_VERS;
  1091. + pps->params.mode = default_params;
  1092. + pps->info = *info;
  1093. +
  1094. + init_waitqueue_head(&pps->queue);
  1095. + spin_lock_init(&pps->lock);
  1096. + atomic_set(&pps->usage, 0);
  1097. + init_waitqueue_head(&pps->usage_queue);
  1098. +
  1099. + /* Create the char device */
  1100. + err = pps_register_cdev(pps);
  1101. + if (err < 0) {
  1102. + printk(KERN_ERR "pps: %s: unable to create char device\n",
  1103. + info->name);
  1104. + goto free_idr;
  1105. + }
  1106. +
  1107. + /* Create the sysfs entry */
  1108. + err = pps_sysfs_create_source_entry(pps);
  1109. + if (err < 0) {
  1110. + printk(KERN_ERR "pps: %s: unable to create sysfs entries\n",
  1111. + info->name);
  1112. + goto unregister_cdev;
  1113. + }
  1114. +
  1115. + pr_info("new PPS source %s at ID %d\n", info->name, id);
  1116. +
  1117. + return id;
  1118. +
  1119. +unregister_cdev:
  1120. + pps_unregister_cdev(pps);
  1121. +
  1122. +free_idr:
  1123. + spin_lock_irq(&idr_lock);
  1124. + idr_remove(&pps_idr, id);
  1125. + spin_unlock_irq(&idr_lock);
  1126. +
  1127. +kfree_pps:
  1128. + kfree(pps);
  1129. +
  1130. +pps_register_source_exit:
  1131. + printk(KERN_ERR "pps: %s: unable to register source\n", info->name);
  1132. +
  1133. + return err;
  1134. +}
  1135. +EXPORT_SYMBOL(pps_register_source);
  1136. +
  1137. +void pps_unregister_source(int source)
  1138. +{
  1139. + struct pps_device *pps;
  1140. +
  1141. + spin_lock_irq(&idr_lock);
  1142. + pps = idr_find(&pps_idr, source);
  1143. +
  1144. + if (!pps) {
  1145. + spin_unlock_irq(&idr_lock);
  1146. + return;
  1147. + }
  1148. +
  1149. + /* This should be done first in order to deny IRQ handlers
  1150. + * to access PPS structs
  1151. + */
  1152. +
  1153. + idr_remove(&pps_idr, pps->id);
  1154. + spin_unlock_irq(&idr_lock);
  1155. +
  1156. + wait_event(pps->usage_queue, atomic_read(&pps->usage) == 0);
  1157. +
  1158. + pps_sysfs_remove_source_entry(pps);
  1159. + pps_unregister_cdev(pps);
  1160. + kfree(pps);
  1161. +}
  1162. +EXPORT_SYMBOL(pps_unregister_source);
  1163. +
  1164. +void pps_event(int source, int event, void *data)
  1165. +{
  1166. + struct pps_device *pps;
  1167. + struct timespec __ts;
  1168. + struct pps_ktime ts;
  1169. + unsigned long flags;
  1170. +
  1171. + /* First of all we get the time stamp... */
  1172. + getnstimeofday(&__ts);
  1173. +
  1174. + /* ... and translate it to PPS time data struct */
  1175. + ts.sec = __ts.tv_sec;
  1176. + ts.nsec = __ts.tv_nsec;
  1177. +
  1178. + if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0 ) {
  1179. + printk(KERN_ERR "pps: unknown event (%x) for source %d\n",
  1180. + event, source);
  1181. + return;
  1182. + }
  1183. +
  1184. + spin_lock_irqsave(&idr_lock, flags);
  1185. + pps = idr_find(&pps_idr, source);
  1186. +
  1187. + /* If we find a valid PPS source we lock it before leaving
  1188. + * the lock!
  1189. + */
  1190. + if (pps)
  1191. + atomic_inc(&pps->usage);
  1192. + spin_unlock_irqrestore(&idr_lock, flags);
  1193. +
  1194. + if (!pps)
  1195. + return;
  1196. +
  1197. + pr_debug("PPS event on source %d at at %llu.%06u\n",
  1198. + pps->id, ts.sec, ts.nsec);
  1199. +
  1200. + spin_lock_irqsave(&pps->lock, flags);
  1201. +
  1202. + /* Must call the echo function? */
  1203. + if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
  1204. + pps->info.echo(source, event, data);
  1205. +
  1206. + /* Check the event */
  1207. + pps->current_mode = pps->params.mode;
  1208. + if (event & PPS_CAPTUREASSERT) {
  1209. + /* We have to add an offset? */
  1210. + if (pps->params.mode & PPS_OFFSETASSERT)
  1211. + pps_add_offset(&ts, &pps->params.assert_off_tu);
  1212. +
  1213. + /* Save the time stamp */
  1214. + pps->assert_tu = ts;
  1215. + pps->assert_sequence++;
  1216. + pr_debug("capture assert seq #%u for source %d\n",
  1217. + pps->assert_sequence, source);
  1218. + }
  1219. + if (event & PPS_CAPTURECLEAR) {
  1220. + /* We have to add an offset? */
  1221. + if (pps->params.mode & PPS_OFFSETCLEAR)
  1222. + pps_add_offset(&ts, &pps->params.clear_off_tu);
  1223. +
  1224. + /* Save the time stamp */
  1225. + pps->clear_tu = ts;
  1226. + pps->clear_sequence++;
  1227. + pr_debug("capture clear seq #%u for source %d\n",
  1228. + pps->clear_sequence, source);
  1229. + }
  1230. +
  1231. + pps->go = ~0;
  1232. + wake_up_interruptible(&pps->queue);
  1233. +
  1234. + kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
  1235. +
  1236. + spin_unlock_irqrestore(&pps->lock, flags);
  1237. +
  1238. + /* Now we can release the PPS source for (possible) deregistration */
  1239. + spin_lock_irqsave(&idr_lock, flags);
  1240. + atomic_dec(&pps->usage);
  1241. + wake_up_all(&pps->usage_queue);
  1242. + spin_unlock_irqrestore(&idr_lock, flags);
  1243. +}
  1244. +EXPORT_SYMBOL(pps_event);
  1245. --- /dev/null
  1246. +++ b/drivers/pps/pps.c
  1247. @@ -0,0 +1,332 @@
  1248. +/*
  1249. + * pps.c -- Main PPS support file
  1250. + *
  1251. + *
  1252. + * Copyright (C) 2005-2007 Rodolfo Giometti <[email protected]>
  1253. + *
  1254. + * This program is free software; you can redistribute it and/or modify
  1255. + * it under the terms of the GNU General Public License as published by
  1256. + * the Free Software Foundation; either version 2 of the License, or
  1257. + * (at your option) any later version.
  1258. + *
  1259. + * This program is distributed in the hope that it will be useful,
  1260. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1261. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1262. + * GNU General Public License for more details.
  1263. + *
  1264. + * You should have received a copy of the GNU General Public License
  1265. + * along with this program; if not, write to the Free Software
  1266. + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1267. + */
  1268. +
  1269. +
  1270. +#include <linux/kernel.h>
  1271. +#include <linux/version.h>
  1272. +#include <linux/module.h>
  1273. +#include <linux/init.h>
  1274. +#include <linux/sched.h>
  1275. +#include <linux/uaccess.h>
  1276. +#include <linux/cdev.h>
  1277. +#include <linux/poll.h>
  1278. +#include <linux/pps.h>
  1279. +
  1280. +/*
  1281. + * Local variables
  1282. + */
  1283. +
  1284. +static dev_t pps_devt;
  1285. +static struct class *pps_class;
  1286. +
  1287. +/*
  1288. + * Char device methods
  1289. + */
  1290. +
  1291. +static unsigned int pps_cdev_poll(struct file *file, poll_table *wait)
  1292. +{
  1293. + struct pps_device *pps = file->private_data;
  1294. +
  1295. + poll_wait(file, &pps->queue, wait);
  1296. +
  1297. + return POLLIN | POLLRDNORM;
  1298. +}
  1299. +
  1300. +static int pps_cdev_fasync(int fd, struct file *file, int on)
  1301. +{
  1302. + struct pps_device *pps = file->private_data;
  1303. + return fasync_helper(fd, file, on, &pps->async_queue);
  1304. +}
  1305. +
  1306. +static int pps_cdev_ioctl(struct inode *inode, struct file *file,
  1307. + unsigned int cmd, unsigned long arg)
  1308. +{
  1309. + struct pps_device *pps = file->private_data;
  1310. + struct pps_kparams params;
  1311. + struct pps_fdata fdata;
  1312. + unsigned long ticks;
  1313. + void __user *uarg = (void __user *) arg;
  1314. + int __user *iuarg = (int __user *) arg;
  1315. + int err;
  1316. +
  1317. + switch (cmd) {
  1318. + case PPS_CHECK:
  1319. +
  1320. + /* This does nothing but signal we are a PPS source... */
  1321. +
  1322. + return 0;
  1323. +
  1324. + case PPS_GETPARAMS:
  1325. + pr_debug("PPS_GETPARAMS: source %d\n", pps->id);
  1326. +
  1327. + /* Sanity checks */
  1328. + if (!uarg)
  1329. + return -EINVAL;
  1330. +
  1331. + /* Return current parameters */
  1332. + err = copy_to_user(uarg, &pps->params,
  1333. + sizeof(struct pps_kparams));
  1334. + if (err)
  1335. + return -EFAULT;
  1336. +
  1337. + break;
  1338. +
  1339. + case PPS_SETPARAMS:
  1340. + pr_debug("PPS_SETPARAMS: source %d\n", pps->id);
  1341. +
  1342. + /* Check the capabilities */
  1343. + if (!capable(CAP_SYS_TIME))
  1344. + return -EPERM;
  1345. +
  1346. + /* Sanity checks */
  1347. + if (!uarg)
  1348. + return -EINVAL;
  1349. + err = copy_from_user(&params, uarg, sizeof(struct pps_kparams));
  1350. + if (err)
  1351. + return -EFAULT;
  1352. + if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
  1353. + pr_debug("capture mode unspecified (%x)\n",
  1354. + params.mode);
  1355. + return -EINVAL;
  1356. + }
  1357. +
  1358. + /* Check for supported capabilities */
  1359. + if ((params.mode & ~pps->info.mode) != 0) {
  1360. + pr_debug("unsupported capabilities (%x)\n",
  1361. + params.mode);
  1362. + return -EINVAL;
  1363. + }
  1364. +
  1365. + spin_lock_irq(&pps->lock);
  1366. +
  1367. + /* Save the new parameters */
  1368. + pps->params = params;
  1369. +
  1370. + /* Restore the read only parameters */
  1371. + if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
  1372. + /* section 3.3 of RFC 2783 interpreted */
  1373. + pr_debug("time format unspecified (%x)\n",
  1374. + params.mode);
  1375. + pps->params.mode |= PPS_TSFMT_TSPEC;
  1376. + }
  1377. + if (pps->info.mode & PPS_CANWAIT)
  1378. + pps->params.mode |= PPS_CANWAIT;
  1379. + pps->params.api_version = PPS_API_VERS;
  1380. +
  1381. + spin_unlock_irq(&pps->lock);
  1382. +
  1383. + break;
  1384. +
  1385. + case PPS_GETCAP:
  1386. + pr_debug("PPS_GETCAP: source %d\n", pps->id);
  1387. +
  1388. + /* Sanity checks */
  1389. + if (!uarg)
  1390. + return -EINVAL;
  1391. +
  1392. + err = put_user(pps->info.mode, iuarg);
  1393. + if (err)
  1394. + return -EFAULT;
  1395. +
  1396. + break;
  1397. +
  1398. + case PPS_FETCH:
  1399. + pr_debug("PPS_FETCH: source %d\n", pps->id);
  1400. +
  1401. + if (!uarg)
  1402. + return -EINVAL;
  1403. + err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
  1404. + if (err)
  1405. + return -EFAULT;
  1406. +
  1407. + pps->go = 0;
  1408. +
  1409. + /* Manage the timeout */
  1410. + if (fdata.timeout.flags & PPS_TIME_INVALID)
  1411. + err = wait_event_interruptible(pps->queue, pps->go);
  1412. + else {
  1413. + pr_debug("timeout %lld.%09d\n",
  1414. + fdata.timeout.sec, fdata.timeout.nsec);
  1415. + ticks = fdata.timeout.sec * HZ;
  1416. + ticks += fdata.timeout.nsec / (NSEC_PER_SEC / HZ);
  1417. +
  1418. + if (ticks != 0) {
  1419. + err = wait_event_interruptible_timeout(
  1420. + pps->queue, pps->go, ticks);
  1421. + if (err == 0)
  1422. + return -ETIMEDOUT;
  1423. + }
  1424. + }
  1425. +
  1426. + /* Check for pending signals */
  1427. + if (err == -ERESTARTSYS) {
  1428. + pr_debug("pending signal caught\n");
  1429. + return -EINTR;
  1430. + }
  1431. +
  1432. + /* Return the fetched timestamp */
  1433. + spin_lock_irq(&pps->lock);
  1434. +
  1435. + fdata.info.assert_sequence = pps->assert_sequence;
  1436. + fdata.info.clear_sequence = pps->clear_sequence;
  1437. + fdata.info.assert_tu = pps->assert_tu;
  1438. + fdata.info.clear_tu = pps->clear_tu;
  1439. + fdata.info.current_mode = pps->current_mode;
  1440. +
  1441. + spin_unlock_irq(&pps->lock);
  1442. +
  1443. + err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata));
  1444. + if (err)
  1445. + return -EFAULT;
  1446. +
  1447. + break;
  1448. +
  1449. + default:
  1450. + return -ENOTTY;
  1451. + break;
  1452. + }
  1453. +
  1454. + return 0;
  1455. +}
  1456. +
  1457. +static int pps_cdev_open(struct inode *inode, struct file *file)
  1458. +{
  1459. + struct pps_device *pps = container_of(inode->i_cdev,
  1460. + struct pps_device, cdev);
  1461. +
  1462. + /* Lock the PPS source against (possible) deregistration */
  1463. + atomic_inc(&pps->usage);
  1464. +
  1465. + file->private_data = pps;
  1466. +
  1467. + return 0;
  1468. +}
  1469. +
  1470. +static int pps_cdev_release(struct inode *inode, struct file *file)
  1471. +{
  1472. + struct pps_device *pps = file->private_data;
  1473. +
  1474. + /* Free the PPS source and wake up (possible) deregistration */
  1475. + atomic_dec(&pps->usage);
  1476. + wake_up_all(&pps->usage_queue);
  1477. +
  1478. + return 0;
  1479. +}
  1480. +
  1481. +/*
  1482. + * Char device stuff
  1483. + */
  1484. +
  1485. +static const struct file_operations pps_cdev_fops = {
  1486. + .owner = THIS_MODULE,
  1487. + .llseek = no_llseek,
  1488. + .poll = pps_cdev_poll,
  1489. + .fasync = pps_cdev_fasync,
  1490. + .ioctl = pps_cdev_ioctl,
  1491. + .open = pps_cdev_open,
  1492. + .release = pps_cdev_release,
  1493. +};
  1494. +
  1495. +int pps_register_cdev(struct pps_device *pps)
  1496. +{
  1497. + int err;
  1498. +
  1499. + pps->devno = MKDEV(MAJOR(pps_devt), pps->id);
  1500. + cdev_init(&pps->cdev, &pps_cdev_fops);
  1501. + pps->cdev.owner = pps->info.owner;
  1502. +
  1503. + err = cdev_add(&pps->cdev, pps->devno, 1);
  1504. + if (err) {
  1505. + printk(KERN_ERR "pps: %s: failed to add char device %d:%d\n",
  1506. + pps->info.name, MAJOR(pps_devt), pps->id);
  1507. + return err;
  1508. + }
  1509. + pps->dev = device_create(pps_class, pps->info.dev, pps->devno,
  1510. + "pps%d", pps->id);
  1511. + if (err)
  1512. + goto del_cdev;
  1513. + dev_set_drvdata(pps->dev, pps);
  1514. +
  1515. + pr_debug("source %s got cdev (%d:%d)\n", pps->info.name,
  1516. + MAJOR(pps_devt), pps->id);
  1517. +
  1518. + return 0;
  1519. +
  1520. +del_cdev:
  1521. + cdev_del(&pps->cdev);
  1522. +
  1523. + return err;
  1524. +}
  1525. +
  1526. +void pps_unregister_cdev(struct pps_device *pps)
  1527. +{
  1528. + device_destroy(pps_class, pps->devno);
  1529. + cdev_del(&pps->cdev);
  1530. +}
  1531. +
  1532. +/*
  1533. + * Module staff
  1534. + */
  1535. +
  1536. +static void __exit pps_exit(void)
  1537. +{
  1538. + class_destroy(pps_class);
  1539. +
  1540. + if (pps_devt)
  1541. + unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES);
  1542. +
  1543. + pr_info("LinuxPPS API ver. %d removed\n", PPS_API_VERS);
  1544. +}
  1545. +
  1546. +static int __init pps_init(void)
  1547. +{
  1548. + int err;
  1549. +
  1550. + pps_class = class_create(THIS_MODULE, "pps");
  1551. + if (!pps_class) {
  1552. + printk(KERN_ERR "pps: ailed to allocate class\n");
  1553. + return -ENOMEM;
  1554. + }
  1555. +
  1556. + err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps");
  1557. + if (err < 0) {
  1558. + printk(KERN_ERR "pps: failed to allocate char device region\n");
  1559. + goto remove_class;
  1560. + }
  1561. +
  1562. + pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS);
  1563. + pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti "
  1564. + "<[email protected]>\n", PPS_VERSION);
  1565. +
  1566. + return 0;
  1567. +
  1568. +remove_class:
  1569. + class_destroy(pps_class);
  1570. +
  1571. + return err;
  1572. +}
  1573. +
  1574. +subsys_initcall(pps_init);
  1575. +module_exit(pps_exit);
  1576. +
  1577. +MODULE_AUTHOR("Rodolfo Giometti <[email protected]>");
  1578. +MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION);
  1579. +MODULE_LICENSE("GPL");
  1580. --- /dev/null
  1581. +++ b/drivers/pps/sysfs.c
  1582. @@ -0,0 +1,124 @@
  1583. +/*
  1584. + * sysfs.c -- sysfs support
  1585. + *
  1586. + *
  1587. + * Copyright (C) 2007 Rodolfo Giometti <[email protected]>
  1588. + *
  1589. + * This program is free software; you can redistribute it and/or modify
  1590. + * it under the terms of the GNU General Public License as published by
  1591. + * the Free Software Foundation; either version 2 of the License, or
  1592. + * (at your option) any later version.
  1593. + *
  1594. + * This program is distributed in the hope that it will be useful,
  1595. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1596. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1597. + * GNU General Public License for more details.
  1598. + *
  1599. + * You should have received a copy of the GNU General Public License
  1600. + * along with this program; if not, write to the Free Software
  1601. + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1602. + */
  1603. +
  1604. +
  1605. +#include <linux/device.h>
  1606. +#include <linux/module.h>
  1607. +#include <linux/string.h>
  1608. +#include <linux/pps.h>
  1609. +
  1610. +/*
  1611. + * Attribute functions
  1612. + */
  1613. +
  1614. +static ssize_t pps_show_assert(struct device *dev,
  1615. + struct device_attribute *attr, char *buf)
  1616. +{
  1617. + struct pps_device *pps = dev_get_drvdata(dev);
  1618. +
  1619. + return sprintf(buf, "%lld.%09d#%d\n",
  1620. + pps->assert_tu.sec, pps->assert_tu.nsec,
  1621. + pps->assert_sequence);
  1622. +}
  1623. +DEVICE_ATTR(assert, S_IRUGO, pps_show_assert, NULL);
  1624. +
  1625. +static ssize_t pps_show_clear(struct device *dev,
  1626. + struct device_attribute *attr, char *buf)
  1627. +{
  1628. + struct pps_device *pps = dev_get_drvdata(dev);
  1629. +
  1630. + return sprintf(buf, "%lld.%09d#%d\n",
  1631. + pps->clear_tu.sec, pps->clear_tu.nsec,
  1632. + pps->clear_sequence);
  1633. +}
  1634. +DEVICE_ATTR(clear, S_IRUGO, pps_show_clear, NULL);
  1635. +
  1636. +static ssize_t pps_show_mode(struct device *dev,
  1637. + struct device_attribute *attr, char *buf)
  1638. +{
  1639. + struct pps_device *pps = dev_get_drvdata(dev);
  1640. +
  1641. + return sprintf(buf, "%4x\n", pps->info.mode);
  1642. +}
  1643. +DEVICE_ATTR(mode, S_IRUGO, pps_show_mode, NULL);
  1644. +
  1645. +static ssize_t pps_show_echo(struct device *dev,
  1646. + struct device_attribute *attr, char *buf)
  1647. +{
  1648. + struct pps_device *pps = dev_get_drvdata(dev);
  1649. +
  1650. + return sprintf(buf, "%d\n", !!pps->info.echo);
  1651. +}
  1652. +DEVICE_ATTR(echo, S_IRUGO, pps_show_echo, NULL);
  1653. +
  1654. +static ssize_t pps_show_name(struct device *dev,
  1655. + struct device_attribute *attr, char *buf)
  1656. +{
  1657. + struct pps_device *pps = dev_get_drvdata(dev);
  1658. +
  1659. + return sprintf(buf, "%s\n", pps->info.name);
  1660. +}
  1661. +DEVICE_ATTR(name, S_IRUGO, pps_show_name, NULL);
  1662. +
  1663. +static ssize_t pps_show_path(struct device *dev,
  1664. + struct device_attribute *attr, char *buf)
  1665. +{
  1666. + struct pps_device *pps = dev_get_drvdata(dev);
  1667. +
  1668. + return sprintf(buf, "%s\n", pps->info.path);
  1669. +}
  1670. +DEVICE_ATTR(path, S_IRUGO, pps_show_path, NULL);
  1671. +
  1672. +/*
  1673. + * Public functions
  1674. + */
  1675. +
  1676. +void pps_sysfs_remove_source_entry(struct pps_device *pps)
  1677. +{
  1678. + /* Delete info files */
  1679. + if (pps->info.mode & PPS_CAPTUREASSERT)
  1680. + device_remove_file(pps->dev, &dev_attr_assert);
  1681. +
  1682. + if (pps->info.mode & PPS_CAPTURECLEAR)
  1683. + device_remove_file(pps->dev, &dev_attr_clear);
  1684. +
  1685. + device_remove_file(pps->dev, &dev_attr_mode);
  1686. + device_remove_file(pps->dev, &dev_attr_echo);
  1687. + device_remove_file(pps->dev, &dev_attr_name);
  1688. + device_remove_file(pps->dev, &dev_attr_path);
  1689. +}
  1690. +
  1691. +int pps_sysfs_create_source_entry(struct pps_device *pps)
  1692. +{
  1693. + /* Create file "assert" and "clear" according to source capability */
  1694. + if (pps->info.mode & PPS_CAPTUREASSERT)
  1695. + device_create_file(pps->dev, &dev_attr_assert);
  1696. +
  1697. + if (pps->info.mode & PPS_CAPTURECLEAR)
  1698. + device_create_file(pps->dev, &dev_attr_clear);
  1699. +
  1700. + device_create_file(pps->dev, &dev_attr_mode);
  1701. + device_create_file(pps->dev, &dev_attr_echo);
  1702. + device_create_file(pps->dev, &dev_attr_name);
  1703. + device_create_file(pps->dev, &dev_attr_path);
  1704. +
  1705. + return 0;
  1706. +}
  1707. --- a/drivers/serial/8250.c
  1708. +++ b/drivers/serial/8250.c
  1709. @@ -2118,6 +2118,8 @@ serial8250_set_termios(struct uart_port
  1710. up->ier |= UART_IER_MSI;
  1711. if (up->capabilities & UART_CAP_UUE)
  1712. up->ier |= UART_IER_UUE | UART_IER_RTOIE;
  1713. + if (up->port.flags & UPF_HARDPPS_CD)
  1714. + up->ier |= UART_IER_MSI; /* enable interrupts */
  1715. serial_out(up, UART_IER, up->ier);
  1716. --- a/drivers/serial/serial_core.c
  1717. +++ b/drivers/serial/serial_core.c
  1718. @@ -33,6 +33,7 @@
  1719. #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
  1720. #include <linux/delay.h>
  1721. #include <linux/mutex.h>
  1722. +#include <linux/pps.h>
  1723. #include <asm/irq.h>
  1724. #include <asm/uaccess.h>
  1725. @@ -633,6 +634,54 @@ static int uart_get_info(struct uart_sta
  1726. return 0;
  1727. }
  1728. +#ifdef CONFIG_PPS_CLIENT_UART
  1729. +
  1730. +static int
  1731. +uart_register_pps_port(struct uart_state *state, struct uart_port *port)
  1732. +{
  1733. + struct tty_driver *drv = port->info->tty->driver;
  1734. + int ret;
  1735. +
  1736. + state->pps_info.owner = THIS_MODULE;
  1737. + state->pps_info.dev = port->dev;
  1738. + snprintf(state->pps_info.name, PPS_MAX_NAME_LEN, "%s%d",
  1739. + drv->driver_name, port->line);
  1740. + snprintf(state->pps_info.path, PPS_MAX_NAME_LEN, "/dev/%s%d",
  1741. + drv->name, port->line);
  1742. +
  1743. + state->pps_info.mode = PPS_CAPTUREBOTH | \
  1744. + PPS_OFFSETASSERT | PPS_OFFSETCLEAR | \
  1745. + PPS_CANWAIT | PPS_TSFMT_TSPEC;
  1746. +
  1747. + ret = pps_register_source(&state->pps_info, PPS_CAPTUREBOTH | \
  1748. + PPS_OFFSETASSERT | PPS_OFFSETCLEAR);
  1749. + if (ret < 0) {
  1750. + dev_err(port->dev, "cannot register PPS source \"%s\"\n",
  1751. + state->pps_info.path);
  1752. + return ret;
  1753. + }
  1754. + port->pps_source = ret;
  1755. + dev_dbg(port->dev, "PPS source #%d \"%s\" added\n",
  1756. + port->pps_source, state->pps_info.path);
  1757. +
  1758. + return 0;
  1759. +}
  1760. +
  1761. +static void
  1762. +uart_unregister_pps_port(struct uart_state *state, struct uart_port *port)
  1763. +{
  1764. + pps_unregister_source(port->pps_source);
  1765. + dev_dbg(port->dev, "PPS source #%d \"%s\" removed\n",
  1766. + port->pps_source, state->pps_info.path);
  1767. +}
  1768. +
  1769. +#else
  1770. +
  1771. +#define uart_register_pps_port(state, port) do { } while (0)
  1772. +#define uart_unregister_pps_port(state, port) do { } while (0)
  1773. +
  1774. +#endif /* CONFIG_PPS_CLIENT_UART */
  1775. +
  1776. static int uart_set_info(struct uart_state *state,
  1777. struct serial_struct __user *newinfo)
  1778. {
  1779. @@ -807,11 +856,19 @@ static int uart_set_info(struct uart_sta
  1780. (port->flags & UPF_LOW_LATENCY) ? 1 : 0;
  1781. check_and_exit:
  1782. + /* PPS support enabled/disabled? */
  1783. + if ((old_flags & UPF_HARDPPS_CD) != (new_flags & UPF_HARDPPS_CD)) {
  1784. + if (new_flags & UPF_HARDPPS_CD)
  1785. + uart_register_pps_port(state, port);
  1786. + else
  1787. + uart_unregister_pps_port(state, port);
  1788. + }
  1789. +
  1790. retval = 0;
  1791. if (port->type == PORT_UNKNOWN)
  1792. goto exit;
  1793. if (state->info->flags & UIF_INITIALIZED) {
  1794. - if (((old_flags ^ port->flags) & UPF_SPD_MASK) ||
  1795. + if (((old_flags ^ port->flags) & (UPF_SPD_MASK|UPF_HARDPPS_CD)) ||
  1796. old_custom_divisor != port->custom_divisor) {
  1797. /*
  1798. * If they're setting up a custom divisor or speed,
  1799. @@ -2110,6 +2167,12 @@ uart_configure_port(struct uart_driver *
  1800. port->ops->config_port(port, flags);
  1801. }
  1802. + /*
  1803. + * Add the PPS support for the current port.
  1804. + */
  1805. + if (port->flags & UPF_HARDPPS_CD)
  1806. + uart_register_pps_port(state, port);
  1807. +
  1808. if (port->type != PORT_UNKNOWN) {
  1809. unsigned long flags;
  1810. @@ -2359,6 +2422,12 @@ int uart_remove_one_port(struct uart_dri
  1811. mutex_unlock(&state->mutex);
  1812. /*
  1813. + * Remove PPS support from the current port.
  1814. + */
  1815. + if (port->flags & UPF_HARDPPS_CD)
  1816. + uart_unregister_pps_port(state, port);
  1817. +
  1818. + /*
  1819. * Remove the devices from the tty layer
  1820. */
  1821. tty_unregister_device(drv->tty_driver, port->line);
  1822. --- a/include/linux/Kbuild
  1823. +++ b/include/linux/Kbuild
  1824. @@ -295,6 +295,7 @@ unifdef-y += pmu.h
  1825. unifdef-y += poll.h
  1826. unifdef-y += ppp_defs.h
  1827. unifdef-y += ppp-comp.h
  1828. +unifdef-y += pps.h
  1829. unifdef-y += ptrace.h
  1830. unifdef-y += qnx4_fs.h
  1831. unifdef-y += quota.h
  1832. --- a/include/linux/parport.h
  1833. +++ b/include/linux/parport.h
  1834. @@ -100,6 +100,7 @@ typedef enum {
  1835. #include <linux/proc_fs.h>
  1836. #include <linux/spinlock.h>
  1837. #include <linux/wait.h>
  1838. +#include <linux/pps.h>
  1839. #include <asm/system.h>
  1840. #include <asm/ptrace.h>
  1841. #include <asm/semaphore.h>
  1842. @@ -327,6 +328,11 @@ struct parport {
  1843. struct list_head full_list;
  1844. struct parport *slaves[3];
  1845. +
  1846. +#ifdef CONFIG_PPS_CLIENT_LP
  1847. + struct pps_source_info pps_info;
  1848. + int pps_source; /* PPS source ID */
  1849. +#endif
  1850. };
  1851. #define DEFAULT_SPIN_TIME 500 /* us */
  1852. @@ -517,6 +523,12 @@ extern int parport_daisy_select (struct
  1853. /* Lowlevel drivers _can_ call this support function to handle irqs. */
  1854. static __inline__ void parport_generic_irq(int irq, struct parport *port)
  1855. {
  1856. +#ifdef CONFIG_PPS_CLIENT_LP
  1857. + pps_event(port->pps_source, PPS_CAPTUREASSERT, port);
  1858. + dev_dbg(port->dev, "PPS assert at %lu on source #%d\n",
  1859. + jiffies, port->pps_source);
  1860. +#endif
  1861. +
  1862. parport_ieee1284_interrupt (irq, port);
  1863. read_lock(&port->cad_lock);
  1864. if (port->cad && port->cad->irq_func)
  1865. --- /dev/null
  1866. +++ b/include/linux/pps.h
  1867. @@ -0,0 +1,196 @@
  1868. +/*
  1869. + * pps.h -- PPS API kernel header.
  1870. + *
  1871. + *
  1872. + * Copyright (C) 2005-2007 Rodolfo Giometti <[email protected]>
  1873. + *
  1874. + * This program is free software; you can redistribute it and/or modify
  1875. + * it under the terms of the GNU General Public License as published by
  1876. + * the Free Software Foundation; either version 2 of the License, or
  1877. + * (at your option) any later version.
  1878. + *
  1879. + * This program is distributed in the hope that it will be useful,
  1880. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1881. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1882. + * GNU General Public License for more details.
  1883. + *
  1884. + * You should have received a copy of the GNU General Public License
  1885. + * along with this program; if not, write to the Free Software
  1886. + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1887. + */
  1888. +
  1889. +
  1890. +#ifndef _PPS_H_
  1891. +#define _PPS_H_
  1892. +
  1893. +/* Implementation note: the logical states ``assert'' and ``clear''
  1894. + * are implemented in terms of the chip register, i.e. ``assert''
  1895. + * means the bit is set. */
  1896. +
  1897. +/*
  1898. + * 3.2 New data structures
  1899. + */
  1900. +
  1901. +#ifndef __KERNEL__
  1902. +#include <linux/types.h>
  1903. +#include <sys/time.h>
  1904. +#include <sys/ioctl.h>
  1905. +#else
  1906. +#include <linux/time.h>
  1907. +#endif
  1908. +
  1909. +#define PPS_API_VERS 1
  1910. +#define PPS_MAX_NAME_LEN 32
  1911. +
  1912. +/* 32-bit vs. 64-bit compatibility.
  1913. + *
  1914. + * 0n i386, the alignment of a uint64_t is only 4 bytes, while on most other
  1915. + * architectures it's 8 bytes. On i386, there will be no padding between the
  1916. + * two consecutive 'struct pps_ktime' members of struct pps_kinfo and struct
  1917. + * pps_kparams. But on most platforms there will be padding to ensure correct
  1918. + * alignment.
  1919. + *
  1920. + * The simple fix is probably to add an explicit padding.
  1921. + * [David Woodhouse]
  1922. + */
  1923. +struct pps_ktime {
  1924. + __u64 sec;
  1925. + __u32 nsec;
  1926. + __u32 flags;
  1927. +};
  1928. +#define PPS_TIME_INVALID (1<<0) /* used to specify timeout==NULL */
  1929. +
  1930. +struct pps_kinfo {
  1931. + __u32 assert_sequence; /* seq. num. of assert event */
  1932. + __u32 clear_sequence; /* seq. num. of clear event */
  1933. + struct pps_ktime assert_tu; /* time of assert event */
  1934. + struct pps_ktime clear_tu; /* time of clear event */
  1935. + int current_mode; /* current mode bits */
  1936. +};
  1937. +
  1938. +struct pps_kparams {
  1939. + int api_version; /* API version # */
  1940. + int mode; /* mode bits */
  1941. + struct pps_ktime assert_off_tu; /* offset compensation for assert */
  1942. + struct pps_ktime clear_off_tu; /* offset compensation for clear */
  1943. +};
  1944. +
  1945. +/*
  1946. + * 3.3 Mode bit definitions
  1947. + */
  1948. +
  1949. +/* Device/implementation parameters */
  1950. +#define PPS_CAPTUREASSERT 0x01 /* capture assert events */
  1951. +#define PPS_CAPTURECLEAR 0x02 /* capture clear events */
  1952. +#define PPS_CAPTUREBOTH 0x03 /* capture assert and clear events */
  1953. +
  1954. +#define PPS_OFFSETASSERT 0x10 /* apply compensation for assert ev. */
  1955. +#define PPS_OFFSETCLEAR 0x20 /* apply compensation for clear ev. */
  1956. +
  1957. +#define PPS_CANWAIT 0x100 /* can we wait for an event? */
  1958. +#define PPS_CANPOLL 0x200 /* bit reserved for future use */
  1959. +
  1960. +/* Kernel actions */
  1961. +#define PPS_ECHOASSERT 0x40 /* feed back assert event to output */
  1962. +#define PPS_ECHOCLEAR 0x80 /* feed back clear event to output */
  1963. +
  1964. +/* Timestamp formats */
  1965. +#define PPS_TSFMT_TSPEC 0x1000 /* select timespec format */
  1966. +#define PPS_TSFMT_NTPFP 0x2000 /* select NTP format */
  1967. +
  1968. +/*
  1969. + * 3.4.4 New functions: disciplining the kernel timebase
  1970. + */
  1971. +
  1972. +/* Kernel consumers */
  1973. +#define PPS_KC_HARDPPS 0 /* hardpps() (or equivalent) */
  1974. +#define PPS_KC_HARDPPS_PLL 1 /* hardpps() constrained to
  1975. + use a phase-locked loop */
  1976. +#define PPS_KC_HARDPPS_FLL 2 /* hardpps() constrained to
  1977. + use a frequency-locked loop */
  1978. +/*
  1979. + * Here begins the implementation-specific part!
  1980. + */
  1981. +
  1982. +struct pps_fdata {
  1983. + struct pps_kinfo info;
  1984. + struct pps_ktime timeout;
  1985. +};
  1986. +
  1987. +#include <linux/ioctl.h>
  1988. +
  1989. +#define PPS_CHECK _IO('P', 0)
  1990. +#define PPS_GETPARAMS _IOR('P', 1, struct pps_kparams *)
  1991. +#define PPS_SETPARAMS _IOW('P', 2, struct pps_kparams *)
  1992. +#define PPS_GETCAP _IOR('P', 3, int *)
  1993. +#define PPS_FETCH _IOWR('P', 4, struct pps_fdata *)
  1994. +
  1995. +#ifdef __KERNEL__
  1996. +
  1997. +#include <linux/cdev.h>
  1998. +#include <linux/device.h>
  1999. +
  2000. +#define PPS_VERSION "5.0.0-rc2"
  2001. +#define PPS_MAX_SOURCES 16 /* should be enought... */
  2002. +
  2003. +/*
  2004. + * Global defines
  2005. + */
  2006. +
  2007. +/* The specific PPS source info */
  2008. +struct pps_source_info {
  2009. + char name[PPS_MAX_NAME_LEN]; /* simbolic name */
  2010. + char path[PPS_MAX_NAME_LEN]; /* path of connected device */
  2011. + int mode; /* PPS's allowed mode */
  2012. +
  2013. + void (*echo)(int source, int event, void *data); /* PPS echo function */
  2014. +
  2015. + struct module *owner;
  2016. + struct device *dev;
  2017. +};
  2018. +
  2019. +/* The main struct */
  2020. +struct pps_device {
  2021. + struct pps_source_info info; /* PSS source info */
  2022. +
  2023. + struct pps_kparams params; /* PPS's current params */
  2024. +
  2025. + __u32 assert_sequence; /* PPS' assert event seq # */
  2026. + __u32 clear_sequence; /* PPS' clear event seq # */
  2027. + struct pps_ktime assert_tu;
  2028. + struct pps_ktime clear_tu;
  2029. + int current_mode; /* PPS mode at event time */
  2030. +
  2031. + int go; /* PPS event is arrived? */
  2032. + wait_queue_head_t queue; /* PPS event queue */
  2033. +
  2034. + unsigned int id; /* PPS source unique ID */
  2035. + struct cdev cdev;
  2036. + struct device *dev;
  2037. + int devno;
  2038. + struct fasync_struct *async_queue; /* fasync method */
  2039. + spinlock_t lock;
  2040. +
  2041. + atomic_t usage; /* usage count */
  2042. + wait_queue_head_t usage_queue;
  2043. +
  2044. + struct class_device class_dev;
  2045. +};
  2046. +
  2047. +/*
  2048. + * Exported functions
  2049. + */
  2050. +
  2051. +extern int pps_register_source(struct pps_source_info *info,
  2052. + int default_params);
  2053. +extern void pps_unregister_source(int source);
  2054. +extern int pps_register_cdev(struct pps_device *pps);
  2055. +extern void pps_unregister_cdev(struct pps_device *pps);
  2056. +extern void pps_event(int source, int event, void *data);
  2057. +
  2058. +extern int pps_sysfs_create_source_entry(struct pps_device *pps);
  2059. +extern void pps_sysfs_remove_source_entry(struct pps_device *pps);
  2060. +
  2061. +#endif /* __KERNEL__ */
  2062. +
  2063. +#endif /* _PPS_H_ */
  2064. --- a/include/linux/serial_core.h
  2065. +++ b/include/linux/serial_core.h
  2066. @@ -157,6 +157,7 @@
  2067. #include <linux/tty.h>
  2068. #include <linux/mutex.h>
  2069. #include <linux/sysrq.h>
  2070. +#include <linux/pps.h>
  2071. struct uart_port;
  2072. struct uart_info;
  2073. @@ -236,6 +237,9 @@ struct uart_port {
  2074. unsigned char regshift; /* reg offset shift */
  2075. unsigned char iotype; /* io access style */
  2076. unsigned char unused1;
  2077. +#ifdef CONFIG_PPS_CLIENT_UART
  2078. + int pps_source; /* PPS source ID */
  2079. +#endif
  2080. #define UPIO_PORT (0)
  2081. #define UPIO_HUB6 (1)
  2082. @@ -280,7 +284,8 @@ struct uart_port {
  2083. #define UPF_IOREMAP ((__force upf_t) (1 << 31))
  2084. #define UPF_CHANGE_MASK ((__force upf_t) (0x17fff))
  2085. -#define UPF_USR_MASK ((__force upf_t) (UPF_SPD_MASK|UPF_LOW_LATENCY))
  2086. +#define UPF_USR_MASK ((__force upf_t) (UPF_SPD_MASK|UPF_LOW_LATENCY\
  2087. + |UPF_HARDPPS_CD))
  2088. unsigned int mctrl; /* current modem ctrl settings */
  2089. unsigned int timeout; /* character-based timeout */
  2090. @@ -312,6 +317,10 @@ struct uart_state {
  2091. struct uart_info *info;
  2092. struct uart_port *port;
  2093. +#ifdef CONFIG_PPS_CLIENT_UART
  2094. + struct pps_source_info pps_info;
  2095. +#endif
  2096. +
  2097. struct mutex mutex;
  2098. };
  2099. @@ -476,13 +485,22 @@ uart_handle_dcd_change(struct uart_port
  2100. {
  2101. struct uart_info *info = port->info;
  2102. - port->icount.dcd++;
  2103. -
  2104. -#ifdef CONFIG_HARD_PPS
  2105. - if ((port->flags & UPF_HARDPPS_CD) && status)
  2106. - hardpps();
  2107. +#ifdef CONFIG_PPS_CLIENT_UART
  2108. + if (port->flags & UPF_HARDPPS_CD) {
  2109. + if (status) {
  2110. + pps_event(port->pps_source, PPS_CAPTUREASSERT, port);
  2111. + dev_dbg(port->dev, "PPS assert at %lu on source #%d\n",
  2112. + jiffies, port->pps_source);
  2113. + } else {
  2114. + pps_event(port->pps_source, PPS_CAPTURECLEAR, port);
  2115. + dev_dbg(port->dev, "PPS clear at %lu on source #%d\n",
  2116. + jiffies, port->pps_source);
  2117. + }
  2118. + }
  2119. #endif
  2120. + port->icount.dcd++;
  2121. +
  2122. if (info->flags & UIF_CHECK_CD) {
  2123. if (status)
  2124. wake_up_interruptible(&info->open_wait);