tipar.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. int data;
  194. for (i = 3; i >= 0; i--) {
  195. outbyte(3, minor);
  196. outbyte(i, minor);
  197. udelay(delay);
  198. data = inbyte(minor) & 0x30;
  199. pr_debug("tipar: Probing -> %i: 0x%02x 0x%02x\n", i,
  200. data, seq[i]);
  201. if (data != seq[i]) {
  202. outbyte(3, minor);
  203. return -1;
  204. }
  205. }
  206. outbyte(3, minor);
  207. return 0;
  208. }
  209. /* ----- kernel module functions--------------------------------------- */
  210. static int
  211. tipar_open(struct inode *inode, struct file *file)
  212. {
  213. unsigned int minor = iminor(inode) - TIPAR_MINOR;
  214. if (tp_count == 0 || minor > tp_count - 1)
  215. return -ENXIO;
  216. if (test_and_set_bit(minor, &opened))
  217. return -EBUSY;
  218. if (!table[minor].dev) {
  219. printk(KERN_ERR "%s: NULL device for minor %u\n",
  220. __FUNCTION__, minor);
  221. return -ENXIO;
  222. }
  223. parport_claim_or_block(table[minor].dev);
  224. init_ti_parallel(minor);
  225. parport_release(table[minor].dev);
  226. return nonseekable_open(inode, file);
  227. }
  228. static int
  229. tipar_close(struct inode *inode, struct file *file)
  230. {
  231. unsigned int minor = iminor(inode) - TIPAR_MINOR;
  232. if (minor > tp_count - 1)
  233. return -ENXIO;
  234. clear_bit(minor, &opened);
  235. return 0;
  236. }
  237. static ssize_t
  238. tipar_write (struct file *file, const char __user *buf, size_t count,
  239. loff_t * ppos)
  240. {
  241. unsigned int minor = iminor(file->f_path.dentry->d_inode) - TIPAR_MINOR;
  242. ssize_t n;
  243. parport_claim_or_block(table[minor].dev);
  244. for (n = 0; n < count; n++) {
  245. unsigned char b;
  246. if (get_user(b, buf + n)) {
  247. n = -EFAULT;
  248. goto out;
  249. }
  250. if (put_ti_parallel(minor, b) == -1) {
  251. init_ti_parallel(minor);
  252. n = -ETIMEDOUT;
  253. goto out;
  254. }
  255. }
  256. out:
  257. parport_release(table[minor].dev);
  258. return n;
  259. }
  260. static ssize_t
  261. tipar_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
  262. {
  263. int b = 0;
  264. unsigned int minor = iminor(file->f_path.dentry->d_inode) - TIPAR_MINOR;
  265. ssize_t retval = 0;
  266. ssize_t n = 0;
  267. if (count == 0)
  268. return 0;
  269. parport_claim_or_block(table[minor].dev);
  270. while (n < count) {
  271. b = get_ti_parallel(minor);
  272. if (b == -1) {
  273. init_ti_parallel(minor);
  274. retval = -ETIMEDOUT;
  275. goto out;
  276. } else {
  277. if (put_user(b, buf + n)) {
  278. retval = -EFAULT;
  279. break;
  280. } else
  281. retval = ++n;
  282. }
  283. /* Non-blocking mode : try again ! */
  284. if (file->f_flags & O_NONBLOCK) {
  285. retval = -EAGAIN;
  286. goto out;
  287. }
  288. /* Signal pending, try again ! */
  289. if (signal_pending(current)) {
  290. retval = -ERESTARTSYS;
  291. goto out;
  292. }
  293. if (need_resched())
  294. schedule();
  295. }
  296. out:
  297. parport_release(table[minor].dev);
  298. return retval;
  299. }
  300. static int
  301. tipar_ioctl(struct inode *inode, struct file *file,
  302. unsigned int cmd, unsigned long arg)
  303. {
  304. int retval = 0;
  305. switch (cmd) {
  306. case IOCTL_TIPAR_DELAY:
  307. delay = (int)arg; //get_user(delay, &arg);
  308. break;
  309. case IOCTL_TIPAR_TIMEOUT:
  310. if (arg != 0)
  311. timeout = (int)arg;
  312. else
  313. retval = -EINVAL;
  314. break;
  315. default:
  316. retval = -ENOTTY;
  317. break;
  318. }
  319. return retval;
  320. }
  321. /* ----- kernel module registering ------------------------------------ */
  322. static const struct file_operations tipar_fops = {
  323. .owner = THIS_MODULE,
  324. .llseek = no_llseek,
  325. .read = tipar_read,
  326. .write = tipar_write,
  327. .ioctl = tipar_ioctl,
  328. .open = tipar_open,
  329. .release = tipar_close,
  330. };
  331. /* --- initialisation code ------------------------------------- */
  332. #ifndef MODULE
  333. /* You must set these - there is no sane way to probe for this cable.
  334. * You can use 'tipar=timeout,delay' to set these now. */
  335. static int __init
  336. tipar_setup(char *str)
  337. {
  338. int ints[3];
  339. str = get_options(str, ARRAY_SIZE(ints), ints);
  340. if (ints[0] > 0) {
  341. if (ints[1] != 0)
  342. timeout = ints[1];
  343. else
  344. printk(KERN_WARNING "tipar: bad timeout value (0), "
  345. "using default value instead");
  346. if (ints[0] > 1) {
  347. delay = ints[2];
  348. }
  349. }
  350. return 1;
  351. }
  352. #endif
  353. /*
  354. * Register our module into parport.
  355. * Pass also 2 callbacks functions to parport: a pre-emptive function and an
  356. * interrupt handler function (unused).
  357. * Display a message such "tipar0: using parport0 (polling)".
  358. */
  359. static int
  360. tipar_register(int nr, struct parport *port)
  361. {
  362. int err = 0;
  363. /* Register our module into parport */
  364. table[nr].dev = parport_register_device(port, "tipar",
  365. NULL, NULL, NULL, 0,
  366. (void *) &table[nr]);
  367. if (table[nr].dev == NULL) {
  368. err = 1;
  369. goto out;
  370. }
  371. class_device_create(tipar_class, NULL, MKDEV(TIPAR_MAJOR,
  372. TIPAR_MINOR + nr), NULL, "par%d", nr);
  373. /* Display informations */
  374. pr_info("tipar%d: using %s (%s)\n", nr, port->name, (port->irq ==
  375. PARPORT_IRQ_NONE) ? "polling" : "interrupt-driven");
  376. if (probe_ti_parallel(nr) != -1)
  377. pr_info("tipar%d: link cable found\n", nr);
  378. else
  379. pr_info("tipar%d: link cable not found\n", nr);
  380. err = 0;
  381. out:
  382. return err;
  383. }
  384. static void
  385. tipar_attach(struct parport *port)
  386. {
  387. if (tp_count == PP_NO) {
  388. pr_info("tipar: ignoring parallel port (max. %d)\n", PP_NO);
  389. return;
  390. }
  391. if (!tipar_register(tp_count, port))
  392. tp_count++;
  393. }
  394. static void
  395. tipar_detach(struct parport *port)
  396. {
  397. /* Nothing to do */
  398. }
  399. static struct parport_driver tipar_driver = {
  400. .name = "tipar",
  401. .attach = tipar_attach,
  402. .detach = tipar_detach,
  403. };
  404. static int __init
  405. tipar_init_module(void)
  406. {
  407. int err = 0;
  408. pr_info("tipar: parallel link cable driver, version %s\n",
  409. DRIVER_VERSION);
  410. if (register_chrdev(TIPAR_MAJOR, "tipar", &tipar_fops)) {
  411. printk(KERN_ERR "tipar: unable to get major %d\n", TIPAR_MAJOR);
  412. err = -EIO;
  413. goto out;
  414. }
  415. tipar_class = class_create(THIS_MODULE, "ticables");
  416. if (IS_ERR(tipar_class)) {
  417. err = PTR_ERR(tipar_class);
  418. goto out_chrdev;
  419. }
  420. if (parport_register_driver(&tipar_driver)) {
  421. printk(KERN_ERR "tipar: unable to register with parport\n");
  422. err = -EIO;
  423. goto out_class;
  424. }
  425. err = 0;
  426. goto out;
  427. out_class:
  428. class_destroy(tipar_class);
  429. out_chrdev:
  430. unregister_chrdev(TIPAR_MAJOR, "tipar");
  431. out:
  432. return err;
  433. }
  434. static void __exit
  435. tipar_cleanup_module(void)
  436. {
  437. unsigned int i;
  438. /* Unregistering module */
  439. parport_unregister_driver(&tipar_driver);
  440. unregister_chrdev(TIPAR_MAJOR, "tipar");
  441. for (i = 0; i < PP_NO; i++) {
  442. if (table[i].dev == NULL)
  443. continue;
  444. parport_unregister_device(table[i].dev);
  445. class_device_destroy(tipar_class, MKDEV(TIPAR_MAJOR, i));
  446. }
  447. class_destroy(tipar_class);
  448. pr_info("tipar: module unloaded\n");
  449. }
  450. /* --------------------------------------------------------------------- */
  451. __setup("tipar=", tipar_setup);
  452. module_init(tipar_init_module);
  453. module_exit(tipar_cleanup_module);
  454. MODULE_AUTHOR(DRIVER_AUTHOR);
  455. MODULE_DESCRIPTION(DRIVER_DESC);
  456. MODULE_LICENSE(DRIVER_LICENSE);
  457. module_param(timeout, int, 0);
  458. MODULE_PARM_DESC(timeout, "Timeout (default=1.5 seconds)");
  459. module_param(delay, int, 0);
  460. MODULE_PARM_DESC(delay, "Inter-bit delay (default=10 microseconds)");