tipar.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /* Hey EMACS -*- linux-c -*-
  2. *
  3. * tipar - low level driver for handling a parallel link cable designed
  4. * for Texas Instruments graphing calculators (http://lpg.ticalc.org).
  5. * A part of the TiLP project.
  6. *
  7. * Copyright (C) 2000-2002, Romain Lievin <roms@lpg.ticalc.org>
  8. * under the terms of the GNU General Public License.
  9. *
  10. * Various fixes & clean-up from the Linux Kernel Mailing List
  11. * (Alan Cox, Richard B. Johnson, Christoph Hellwig).
  12. */
  13. /* This driver should, in theory, work with any parallel port that has an
  14. * appropriate low-level driver; all I/O is done through the parport
  15. * abstraction layer.
  16. *
  17. * If this driver is built into the kernel, you can configure it using the
  18. * kernel command-line. For example:
  19. *
  20. * tipar=timeout,delay (set timeout and delay)
  21. *
  22. * If the driver is loaded as a module, similar functionality is available
  23. * using module parameters. The equivalent of the above commands would be:
  24. *
  25. * # insmod tipar timeout=15 delay=10
  26. */
  27. /* COMPATIBILITY WITH OLD KERNELS
  28. *
  29. * Usually, parallel cables were bound to ports at
  30. * particular I/O addresses, as follows:
  31. *
  32. * tipar0 0x378
  33. * tipar1 0x278
  34. * tipar2 0x3bc
  35. *
  36. *
  37. * This driver, by default, binds tipar devices according to parport and
  38. * the minor number.
  39. *
  40. */
  41. #undef DEBUG /* change to #define to get debugging
  42. * output - for pr_debug() */
  43. #include <linux/module.h>
  44. #include <linux/types.h>
  45. #include <linux/errno.h>
  46. #include <linux/kernel.h>
  47. #include <linux/sched.h>
  48. #include <linux/delay.h>
  49. #include <linux/fcntl.h>
  50. #include <linux/fs.h>
  51. #include <linux/init.h>
  52. #include <asm/uaccess.h>
  53. #include <linux/ioport.h>
  54. #include <asm/io.h>
  55. #include <linux/bitops.h>
  56. #include <linux/parport.h> /* Our code depend on parport */
  57. #include <linux/device.h>
  58. /*
  59. * TI definitions
  60. */
  61. #include <linux/ticable.h>
  62. /*
  63. * Version Information
  64. */
  65. #define DRIVER_VERSION "1.19"
  66. #define DRIVER_AUTHOR "Romain Lievin <roms@lpg.ticalc.org>"
  67. #define DRIVER_DESC "Device driver for TI/PC parallel link cables"
  68. #define DRIVER_LICENSE "GPL"
  69. #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
  70. /* ----- global variables --------------------------------------------- */
  71. struct tipar_struct {
  72. struct pardevice *dev; /* Parport device entry */
  73. };
  74. #define PP_NO 3
  75. static struct tipar_struct table[PP_NO];
  76. static int delay = IO_DELAY; /* inter-bit delay in microseconds */
  77. static int timeout = TIMAXTIME; /* timeout in tenth of seconds */
  78. static unsigned int tp_count; /* tipar count */
  79. static unsigned long opened; /* opened devices */
  80. static struct class *tipar_class;
  81. /* --- macros for parport access -------------------------------------- */
  82. #define r_dtr(x) (parport_read_data(table[(x)].dev->port))
  83. #define r_str(x) (parport_read_status(table[(x)].dev->port))
  84. #define w_ctr(x,y) (parport_write_control(table[(x)].dev->port, (y)))
  85. #define w_dtr(x,y) (parport_write_data(table[(x)].dev->port, (y)))
  86. /* --- setting states on the D-bus with the right timing: ------------- */
  87. static inline void
  88. outbyte(int value, int minor)
  89. {
  90. w_dtr(minor, value);
  91. }
  92. static inline int
  93. inbyte(int minor)
  94. {
  95. return (r_str(minor));
  96. }
  97. static inline void
  98. init_ti_parallel(int minor)
  99. {
  100. outbyte(3, minor);
  101. }
  102. /* ----- global defines ----------------------------------------------- */
  103. #define START(x) { x = jiffies + (HZ * timeout) / 10; }
  104. #define WAIT(x) { \
  105. if (time_before((x), jiffies)) return -1; \
  106. if (need_resched()) schedule(); }
  107. /* ----- D-bus bit-banging functions ---------------------------------- */
  108. /* D-bus protocol (45kbit/s max):
  109. 1 0 0
  110. _______ ______|______ __________|________ __________
  111. Red : ________ | ____ | ____
  112. _ ____________|________ ______|__________ _____
  113. White: ________ | ______ | _______
  114. */
  115. /* Try to transmit a byte on the specified port (-1 if error). */
  116. static int
  117. put_ti_parallel(int minor, unsigned char data)
  118. {
  119. unsigned int bit;
  120. unsigned long max;
  121. for (bit = 0; bit < 8; bit++) {
  122. if (data & 1) {
  123. outbyte(2, minor);
  124. START(max);
  125. do {
  126. WAIT(max);
  127. } while (inbyte(minor) & 0x10);
  128. outbyte(3, minor);
  129. START(max);
  130. do {
  131. WAIT(max);
  132. } while (!(inbyte(minor) & 0x10));
  133. } else {
  134. outbyte(1, minor);
  135. START(max);
  136. do {
  137. WAIT(max);
  138. } while (inbyte(minor) & 0x20);
  139. outbyte(3, minor);
  140. START(max);
  141. do {
  142. WAIT(max);
  143. } while (!(inbyte(minor) & 0x20));
  144. }
  145. data >>= 1;
  146. udelay(delay);
  147. if (need_resched())
  148. schedule();
  149. }
  150. return 0;
  151. }
  152. /* Receive a byte on the specified port or -1 if error. */
  153. static int
  154. get_ti_parallel(int minor)
  155. {
  156. unsigned int bit;
  157. unsigned char v, data = 0;
  158. unsigned long max;
  159. for (bit = 0; bit < 8; bit++) {
  160. START(max);
  161. do {
  162. WAIT(max);
  163. } while ((v = inbyte(minor) & 0x30) == 0x30);
  164. if (v == 0x10) {
  165. data = (data >> 1) | 0x80;
  166. outbyte(1, minor);
  167. START(max);
  168. do {
  169. WAIT(max);
  170. } while (!(inbyte(minor) & 0x20));
  171. outbyte(3, minor);
  172. } else {
  173. data = data >> 1;
  174. outbyte(2, minor);
  175. START(max);
  176. do {
  177. WAIT(max);
  178. } while (!(inbyte(minor) & 0x10));
  179. outbyte(3, minor);
  180. }
  181. udelay(delay);
  182. if (need_resched())
  183. schedule();
  184. }
  185. return (int) data;
  186. }
  187. /* Try to detect a parallel link cable on the specified port */
  188. static int
  189. probe_ti_parallel(int minor)
  190. {
  191. int i;
  192. int seq[] = { 0x00, 0x20, 0x10, 0x30 };
  193. for (i = 3; i >= 0; i--) {
  194. outbyte(3, minor);
  195. outbyte(i, minor);
  196. udelay(delay);
  197. pr_debug("tipar: Probing -> %i: 0x%02x 0x%02x\n", i,
  198. data & 0x30, seq[i]);
  199. if ((inbyte(minor) & 0x30) != seq[i]) {
  200. outbyte(3, minor);
  201. return -1;
  202. }
  203. }
  204. outbyte(3, minor);
  205. return 0;
  206. }
  207. /* ----- kernel module functions--------------------------------------- */
  208. static int
  209. tipar_open(struct inode *inode, struct file *file)
  210. {
  211. unsigned int minor = iminor(inode) - TIPAR_MINOR;
  212. if (tp_count == 0 || minor > tp_count - 1)
  213. return -ENXIO;
  214. if (test_and_set_bit(minor, &opened))
  215. return -EBUSY;
  216. if (!table[minor].dev) {
  217. printk(KERN_ERR "%s: NULL device for minor %u\n",
  218. __FUNCTION__, minor);
  219. return -ENXIO;
  220. }
  221. parport_claim_or_block(table[minor].dev);
  222. init_ti_parallel(minor);
  223. parport_release(table[minor].dev);
  224. return nonseekable_open(inode, file);
  225. }
  226. static int
  227. tipar_close(struct inode *inode, struct file *file)
  228. {
  229. unsigned int minor = iminor(inode) - TIPAR_MINOR;
  230. if (minor > tp_count - 1)
  231. return -ENXIO;
  232. clear_bit(minor, &opened);
  233. return 0;
  234. }
  235. static ssize_t
  236. tipar_write (struct file *file, const char __user *buf, size_t count,
  237. loff_t * ppos)
  238. {
  239. unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
  240. ssize_t n;
  241. parport_claim_or_block(table[minor].dev);
  242. for (n = 0; n < count; n++) {
  243. unsigned char b;
  244. if (get_user(b, buf + n)) {
  245. n = -EFAULT;
  246. goto out;
  247. }
  248. if (put_ti_parallel(minor, b) == -1) {
  249. init_ti_parallel(minor);
  250. n = -ETIMEDOUT;
  251. goto out;
  252. }
  253. }
  254. out:
  255. parport_release(table[minor].dev);
  256. return n;
  257. }
  258. static ssize_t
  259. tipar_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
  260. {
  261. int b = 0;
  262. unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
  263. ssize_t retval = 0;
  264. ssize_t n = 0;
  265. if (count == 0)
  266. return 0;
  267. parport_claim_or_block(table[minor].dev);
  268. while (n < count) {
  269. b = get_ti_parallel(minor);
  270. if (b == -1) {
  271. init_ti_parallel(minor);
  272. retval = -ETIMEDOUT;
  273. goto out;
  274. } else {
  275. if (put_user(b, buf + n)) {
  276. retval = -EFAULT;
  277. break;
  278. } else
  279. retval = ++n;
  280. }
  281. /* Non-blocking mode : try again ! */
  282. if (file->f_flags & O_NONBLOCK) {
  283. retval = -EAGAIN;
  284. goto out;
  285. }
  286. /* Signal pending, try again ! */
  287. if (signal_pending(current)) {
  288. retval = -ERESTARTSYS;
  289. goto out;
  290. }
  291. if (need_resched())
  292. schedule();
  293. }
  294. out:
  295. parport_release(table[minor].dev);
  296. return retval;
  297. }
  298. static int
  299. tipar_ioctl(struct inode *inode, struct file *file,
  300. unsigned int cmd, unsigned long arg)
  301. {
  302. int retval = 0;
  303. switch (cmd) {
  304. case IOCTL_TIPAR_DELAY:
  305. delay = (int)arg; //get_user(delay, &arg);
  306. break;
  307. case IOCTL_TIPAR_TIMEOUT:
  308. if (arg != 0)
  309. timeout = (int)arg;
  310. else
  311. retval = -EINVAL;
  312. break;
  313. default:
  314. retval = -ENOTTY;
  315. break;
  316. }
  317. return retval;
  318. }
  319. /* ----- kernel module registering ------------------------------------ */
  320. static const struct file_operations tipar_fops = {
  321. .owner = THIS_MODULE,
  322. .llseek = no_llseek,
  323. .read = tipar_read,
  324. .write = tipar_write,
  325. .ioctl = tipar_ioctl,
  326. .open = tipar_open,
  327. .release = tipar_close,
  328. };
  329. /* --- initialisation code ------------------------------------- */
  330. #ifndef MODULE
  331. /* You must set these - there is no sane way to probe for this cable.
  332. * You can use 'tipar=timeout,delay' to set these now. */
  333. static int __init
  334. tipar_setup(char *str)
  335. {
  336. int ints[3];
  337. str = get_options(str, ARRAY_SIZE(ints), ints);
  338. if (ints[0] > 0) {
  339. if (ints[1] != 0)
  340. timeout = ints[1];
  341. else
  342. printk(KERN_WARNING "tipar: bad timeout value (0), "
  343. "using default value instead");
  344. if (ints[0] > 1) {
  345. delay = ints[2];
  346. }
  347. }
  348. return 1;
  349. }
  350. #endif
  351. /*
  352. * Register our module into parport.
  353. * Pass also 2 callbacks functions to parport: a pre-emptive function and an
  354. * interrupt handler function (unused).
  355. * Display a message such "tipar0: using parport0 (polling)".
  356. */
  357. static int
  358. tipar_register(int nr, struct parport *port)
  359. {
  360. int err = 0;
  361. /* Register our module into parport */
  362. table[nr].dev = parport_register_device(port, "tipar",
  363. NULL, NULL, NULL, 0,
  364. (void *) &table[nr]);
  365. if (table[nr].dev == NULL) {
  366. err = 1;
  367. goto out;
  368. }
  369. class_device_create(tipar_class, NULL, MKDEV(TIPAR_MAJOR,
  370. TIPAR_MINOR + nr), NULL, "par%d", nr);
  371. /* Display informations */
  372. pr_info("tipar%d: using %s (%s)\n", nr, port->name, (port->irq ==
  373. PARPORT_IRQ_NONE) ? "polling" : "interrupt-driven");
  374. if (probe_ti_parallel(nr) != -1)
  375. pr_info("tipar%d: link cable found\n", nr);
  376. else
  377. pr_info("tipar%d: link cable not found\n", nr);
  378. err = 0;
  379. out:
  380. return err;
  381. }
  382. static void
  383. tipar_attach(struct parport *port)
  384. {
  385. if (tp_count == PP_NO) {
  386. pr_info("tipar: ignoring parallel port (max. %d)\n", PP_NO);
  387. return;
  388. }
  389. if (!tipar_register(tp_count, port))
  390. tp_count++;
  391. }
  392. static void
  393. tipar_detach(struct parport *port)
  394. {
  395. /* Nothing to do */
  396. }
  397. static struct parport_driver tipar_driver = {
  398. .name = "tipar",
  399. .attach = tipar_attach,
  400. .detach = tipar_detach,
  401. };
  402. static int __init
  403. tipar_init_module(void)
  404. {
  405. int err = 0;
  406. pr_info("tipar: parallel link cable driver, version %s\n",
  407. DRIVER_VERSION);
  408. if (register_chrdev(TIPAR_MAJOR, "tipar", &tipar_fops)) {
  409. printk(KERN_ERR "tipar: unable to get major %d\n", TIPAR_MAJOR);
  410. err = -EIO;
  411. goto out;
  412. }
  413. tipar_class = class_create(THIS_MODULE, "ticables");
  414. if (IS_ERR(tipar_class)) {
  415. err = PTR_ERR(tipar_class);
  416. goto out_chrdev;
  417. }
  418. if (parport_register_driver(&tipar_driver)) {
  419. printk(KERN_ERR "tipar: unable to register with parport\n");
  420. err = -EIO;
  421. goto out_class;
  422. }
  423. err = 0;
  424. goto out;
  425. out_class:
  426. class_destroy(tipar_class);
  427. out_chrdev:
  428. unregister_chrdev(TIPAR_MAJOR, "tipar");
  429. out:
  430. return err;
  431. }
  432. static void __exit
  433. tipar_cleanup_module(void)
  434. {
  435. unsigned int i;
  436. /* Unregistering module */
  437. parport_unregister_driver(&tipar_driver);
  438. unregister_chrdev(TIPAR_MAJOR, "tipar");
  439. for (i = 0; i < PP_NO; i++) {
  440. if (table[i].dev == NULL)
  441. continue;
  442. parport_unregister_device(table[i].dev);
  443. class_device_destroy(tipar_class, MKDEV(TIPAR_MAJOR, i));
  444. }
  445. class_destroy(tipar_class);
  446. pr_info("tipar: module unloaded\n");
  447. }
  448. /* --------------------------------------------------------------------- */
  449. __setup("tipar=", tipar_setup);
  450. module_init(tipar_init_module);
  451. module_exit(tipar_cleanup_module);
  452. MODULE_AUTHOR(DRIVER_AUTHOR);
  453. MODULE_DESCRIPTION(DRIVER_DESC);
  454. MODULE_LICENSE(DRIVER_LICENSE);
  455. module_param(timeout, int, 0);
  456. MODULE_PARM_DESC(timeout, "Timeout (default=1.5 seconds)");
  457. module_param(delay, int, 0);
  458. MODULE_PARM_DESC(delay, "Inter-bit delay (default=10 microseconds)");