daisy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * IEEE 1284.3 Parallel port daisy chain and multiplexor code
  3. *
  4. * Copyright (C) 1999, 2000 Tim Waugh <tim@cyberelk.demon.co.uk>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * ??-12-1998: Initial implementation.
  12. * 31-01-1999: Make port-cloning transparent.
  13. * 13-02-1999: Move DeviceID technique from parport_probe.
  14. * 13-03-1999: Get DeviceID from non-IEEE 1284.3 devices too.
  15. * 22-02-2000: Count devices that are actually detected.
  16. *
  17. * Any part of this program may be used in documents licensed under
  18. * the GNU Free Documentation License, Version 1.1 or any later version
  19. * published by the Free Software Foundation.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/parport.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/sched.h>
  26. #include <asm/current.h>
  27. #include <asm/uaccess.h>
  28. #undef DEBUG
  29. #ifdef DEBUG
  30. #define DPRINTK(stuff...) printk(stuff)
  31. #else
  32. #define DPRINTK(stuff...)
  33. #endif
  34. static struct daisydev {
  35. struct daisydev *next;
  36. struct parport *port;
  37. int daisy;
  38. int devnum;
  39. } *topology = NULL;
  40. static DEFINE_SPINLOCK(topology_lock);
  41. static int numdevs = 0;
  42. /* Forward-declaration of lower-level functions. */
  43. static int mux_present(struct parport *port);
  44. static int num_mux_ports(struct parport *port);
  45. static int select_port(struct parport *port);
  46. static int assign_addrs(struct parport *port);
  47. /* Add a device to the discovered topology. */
  48. static void add_dev(int devnum, struct parport *port, int daisy)
  49. {
  50. struct daisydev *newdev, **p;
  51. newdev = kmalloc(sizeof(struct daisydev), GFP_KERNEL);
  52. if (newdev) {
  53. newdev->port = port;
  54. newdev->daisy = daisy;
  55. newdev->devnum = devnum;
  56. spin_lock(&topology_lock);
  57. for (p = &topology; *p && (*p)->devnum<devnum; p = &(*p)->next)
  58. ;
  59. newdev->next = *p;
  60. *p = newdev;
  61. spin_unlock(&topology_lock);
  62. }
  63. }
  64. /* Clone a parport (actually, make an alias). */
  65. static struct parport *clone_parport(struct parport *real, int muxport)
  66. {
  67. struct parport *extra = parport_register_port(real->base,
  68. real->irq,
  69. real->dma,
  70. real->ops);
  71. if (extra) {
  72. extra->portnum = real->portnum;
  73. extra->physport = real;
  74. extra->muxport = muxport;
  75. real->slaves[muxport-1] = extra;
  76. }
  77. return extra;
  78. }
  79. /* Discover the IEEE1284.3 topology on a port -- muxes and daisy chains.
  80. * Return value is number of devices actually detected. */
  81. int parport_daisy_init(struct parport *port)
  82. {
  83. int detected = 0;
  84. char *deviceid;
  85. static const char *th[] = { /*0*/"th", "st", "nd", "rd", "th" };
  86. int num_ports;
  87. int i;
  88. int last_try = 0;
  89. again:
  90. /* Because this is called before any other devices exist,
  91. * we don't have to claim exclusive access. */
  92. /* If mux present on normal port, need to create new
  93. * parports for each extra port. */
  94. if (port->muxport < 0 && mux_present(port) &&
  95. /* don't be fooled: a mux must have 2 or 4 ports. */
  96. ((num_ports = num_mux_ports(port)) == 2 || num_ports == 4)) {
  97. /* Leave original as port zero. */
  98. port->muxport = 0;
  99. printk(KERN_INFO
  100. "%s: 1st (default) port of %d-way multiplexor\n",
  101. port->name, num_ports);
  102. for (i = 1; i < num_ports; i++) {
  103. /* Clone the port. */
  104. struct parport *extra = clone_parport(port, i);
  105. if (!extra) {
  106. if (signal_pending(current))
  107. break;
  108. schedule();
  109. continue;
  110. }
  111. printk(KERN_INFO
  112. "%s: %d%s port of %d-way multiplexor on %s\n",
  113. extra->name, i + 1, th[i + 1], num_ports,
  114. port->name);
  115. /* Analyse that port too. We won't recurse
  116. forever because of the 'port->muxport < 0'
  117. test above. */
  118. parport_daisy_init(extra);
  119. }
  120. }
  121. if (port->muxport >= 0)
  122. select_port(port);
  123. parport_daisy_deselect_all(port);
  124. detected += assign_addrs(port);
  125. /* Count the potential legacy device at the end. */
  126. add_dev(numdevs++, port, -1);
  127. /* Find out the legacy device's IEEE 1284 device ID. */
  128. deviceid = kmalloc(1024, GFP_KERNEL);
  129. if (deviceid) {
  130. if (parport_device_id(numdevs - 1, deviceid, 1024) > 2)
  131. detected++;
  132. kfree(deviceid);
  133. }
  134. if (!detected && !last_try) {
  135. /* No devices were detected. Perhaps they are in some
  136. funny state; let's try to reset them and see if
  137. they wake up. */
  138. parport_daisy_fini(port);
  139. parport_write_control(port, PARPORT_CONTROL_SELECT);
  140. udelay(50);
  141. parport_write_control(port,
  142. PARPORT_CONTROL_SELECT |
  143. PARPORT_CONTROL_INIT);
  144. udelay(50);
  145. last_try = 1;
  146. goto again;
  147. }
  148. return detected;
  149. }
  150. /* Forget about devices on a physical port. */
  151. void parport_daisy_fini(struct parport *port)
  152. {
  153. struct daisydev **p;
  154. spin_lock(&topology_lock);
  155. p = &topology;
  156. while (*p) {
  157. struct daisydev *dev = *p;
  158. if (dev->port != port) {
  159. p = &dev->next;
  160. continue;
  161. }
  162. *p = dev->next;
  163. kfree(dev);
  164. }
  165. /* Gaps in the numbering could be handled better. How should
  166. someone enumerate through all IEEE1284.3 devices in the
  167. topology?. */
  168. if (!topology) numdevs = 0;
  169. spin_unlock(&topology_lock);
  170. return;
  171. }
  172. /**
  173. * parport_open - find a device by canonical device number
  174. * @devnum: canonical device number
  175. * @name: name to associate with the device
  176. *
  177. * This function is similar to parport_register_device(), except
  178. * that it locates a device by its number rather than by the port
  179. * it is attached to.
  180. *
  181. * All parameters except for @devnum are the same as for
  182. * parport_register_device(). The return value is the same as
  183. * for parport_register_device().
  184. **/
  185. struct pardevice *parport_open(int devnum, const char *name)
  186. {
  187. struct daisydev *p = topology;
  188. struct parport *port;
  189. struct pardevice *dev;
  190. int daisy;
  191. spin_lock(&topology_lock);
  192. while (p && p->devnum != devnum)
  193. p = p->next;
  194. if (!p) {
  195. spin_unlock(&topology_lock);
  196. return NULL;
  197. }
  198. daisy = p->daisy;
  199. port = parport_get_port(p->port);
  200. spin_unlock(&topology_lock);
  201. dev = parport_register_device(port, name, NULL, NULL, NULL, 0, NULL);
  202. parport_put_port(port);
  203. if (!dev)
  204. return NULL;
  205. dev->daisy = daisy;
  206. /* Check that there really is a device to select. */
  207. if (daisy >= 0) {
  208. int selected;
  209. parport_claim_or_block(dev);
  210. selected = port->daisy;
  211. parport_release(dev);
  212. if (selected != daisy) {
  213. /* No corresponding device. */
  214. parport_unregister_device(dev);
  215. return NULL;
  216. }
  217. }
  218. return dev;
  219. }
  220. /**
  221. * parport_close - close a device opened with parport_open()
  222. * @dev: device to close
  223. *
  224. * This is to parport_open() as parport_unregister_device() is to
  225. * parport_register_device().
  226. **/
  227. void parport_close(struct pardevice *dev)
  228. {
  229. parport_unregister_device(dev);
  230. }
  231. /* Send a daisy-chain-style CPP command packet. */
  232. static int cpp_daisy(struct parport *port, int cmd)
  233. {
  234. unsigned char s;
  235. parport_data_forward(port);
  236. parport_write_data(port, 0xaa); udelay(2);
  237. parport_write_data(port, 0x55); udelay(2);
  238. parport_write_data(port, 0x00); udelay(2);
  239. parport_write_data(port, 0xff); udelay(2);
  240. s = parport_read_status(port) & (PARPORT_STATUS_BUSY
  241. | PARPORT_STATUS_PAPEROUT
  242. | PARPORT_STATUS_SELECT
  243. | PARPORT_STATUS_ERROR);
  244. if (s != (PARPORT_STATUS_BUSY
  245. | PARPORT_STATUS_PAPEROUT
  246. | PARPORT_STATUS_SELECT
  247. | PARPORT_STATUS_ERROR)) {
  248. DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff(%02x)\n",
  249. port->name, s);
  250. return -ENXIO;
  251. }
  252. parport_write_data(port, 0x87); udelay(2);
  253. s = parport_read_status(port) & (PARPORT_STATUS_BUSY
  254. | PARPORT_STATUS_PAPEROUT
  255. | PARPORT_STATUS_SELECT
  256. | PARPORT_STATUS_ERROR);
  257. if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
  258. DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff87(%02x)\n",
  259. port->name, s);
  260. return -ENXIO;
  261. }
  262. parport_write_data(port, 0x78); udelay(2);
  263. parport_write_data(port, cmd); udelay(2);
  264. parport_frob_control(port,
  265. PARPORT_CONTROL_STROBE,
  266. PARPORT_CONTROL_STROBE);
  267. udelay(1);
  268. s = parport_read_status(port);
  269. parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
  270. udelay(1);
  271. parport_write_data(port, 0xff); udelay(2);
  272. return s;
  273. }
  274. /* Send a mux-style CPP command packet. */
  275. static int cpp_mux(struct parport *port, int cmd)
  276. {
  277. unsigned char s;
  278. int rc;
  279. parport_data_forward(port);
  280. parport_write_data(port, 0xaa); udelay(2);
  281. parport_write_data(port, 0x55); udelay(2);
  282. parport_write_data(port, 0xf0); udelay(2);
  283. parport_write_data(port, 0x0f); udelay(2);
  284. parport_write_data(port, 0x52); udelay(2);
  285. parport_write_data(port, 0xad); udelay(2);
  286. parport_write_data(port, cmd); udelay(2);
  287. s = parport_read_status(port);
  288. if (!(s & PARPORT_STATUS_ACK)) {
  289. DPRINTK(KERN_DEBUG "%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
  290. port->name, cmd, s);
  291. return -EIO;
  292. }
  293. rc = (((s & PARPORT_STATUS_SELECT ? 1 : 0) << 0) |
  294. ((s & PARPORT_STATUS_PAPEROUT ? 1 : 0) << 1) |
  295. ((s & PARPORT_STATUS_BUSY ? 0 : 1) << 2) |
  296. ((s & PARPORT_STATUS_ERROR ? 0 : 1) << 3));
  297. return rc;
  298. }
  299. void parport_daisy_deselect_all(struct parport *port)
  300. {
  301. cpp_daisy(port, 0x30);
  302. }
  303. int parport_daisy_select(struct parport *port, int daisy, int mode)
  304. {
  305. switch (mode)
  306. {
  307. // For these modes we should switch to EPP mode:
  308. case IEEE1284_MODE_EPP:
  309. case IEEE1284_MODE_EPPSL:
  310. case IEEE1284_MODE_EPPSWE:
  311. return !(cpp_daisy(port, 0x20 + daisy) &
  312. PARPORT_STATUS_ERROR);
  313. // For these modes we should switch to ECP mode:
  314. case IEEE1284_MODE_ECP:
  315. case IEEE1284_MODE_ECPRLE:
  316. case IEEE1284_MODE_ECPSWE:
  317. return !(cpp_daisy(port, 0xd0 + daisy) &
  318. PARPORT_STATUS_ERROR);
  319. // Nothing was told for BECP in Daisy chain specification.
  320. // May be it's wise to use ECP?
  321. case IEEE1284_MODE_BECP:
  322. // Others use compat mode
  323. case IEEE1284_MODE_NIBBLE:
  324. case IEEE1284_MODE_BYTE:
  325. case IEEE1284_MODE_COMPAT:
  326. default:
  327. return !(cpp_daisy(port, 0xe0 + daisy) &
  328. PARPORT_STATUS_ERROR);
  329. }
  330. }
  331. static int mux_present(struct parport *port)
  332. {
  333. return cpp_mux(port, 0x51) == 3;
  334. }
  335. static int num_mux_ports(struct parport *port)
  336. {
  337. return cpp_mux(port, 0x58);
  338. }
  339. static int select_port(struct parport *port)
  340. {
  341. int muxport = port->muxport;
  342. return cpp_mux(port, 0x60 + muxport) == muxport;
  343. }
  344. static int assign_addrs(struct parport *port)
  345. {
  346. unsigned char s;
  347. unsigned char daisy;
  348. int thisdev = numdevs;
  349. int detected;
  350. char *deviceid;
  351. parport_data_forward(port);
  352. parport_write_data(port, 0xaa); udelay(2);
  353. parport_write_data(port, 0x55); udelay(2);
  354. parport_write_data(port, 0x00); udelay(2);
  355. parport_write_data(port, 0xff); udelay(2);
  356. s = parport_read_status(port) & (PARPORT_STATUS_BUSY
  357. | PARPORT_STATUS_PAPEROUT
  358. | PARPORT_STATUS_SELECT
  359. | PARPORT_STATUS_ERROR);
  360. if (s != (PARPORT_STATUS_BUSY
  361. | PARPORT_STATUS_PAPEROUT
  362. | PARPORT_STATUS_SELECT
  363. | PARPORT_STATUS_ERROR)) {
  364. DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff(%02x)\n",
  365. port->name, s);
  366. return 0;
  367. }
  368. parport_write_data(port, 0x87); udelay(2);
  369. s = parport_read_status(port) & (PARPORT_STATUS_BUSY
  370. | PARPORT_STATUS_PAPEROUT
  371. | PARPORT_STATUS_SELECT
  372. | PARPORT_STATUS_ERROR);
  373. if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
  374. DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff87(%02x)\n",
  375. port->name, s);
  376. return 0;
  377. }
  378. parport_write_data(port, 0x78); udelay(2);
  379. s = parport_read_status(port);
  380. for (daisy = 0;
  381. (s & (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT))
  382. == (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT)
  383. && daisy < 4;
  384. ++daisy) {
  385. parport_write_data(port, daisy);
  386. udelay(2);
  387. parport_frob_control(port,
  388. PARPORT_CONTROL_STROBE,
  389. PARPORT_CONTROL_STROBE);
  390. udelay(1);
  391. parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
  392. udelay(1);
  393. add_dev(numdevs++, port, daisy);
  394. /* See if this device thought it was the last in the
  395. * chain. */
  396. if (!(s & PARPORT_STATUS_BUSY))
  397. break;
  398. /* We are seeing pass through status now. We see
  399. last_dev from next device or if last_dev does not
  400. work status lines from some non-daisy chain
  401. device. */
  402. s = parport_read_status(port);
  403. }
  404. parport_write_data(port, 0xff); udelay(2);
  405. detected = numdevs - thisdev;
  406. DPRINTK(KERN_DEBUG "%s: Found %d daisy-chained devices\n", port->name,
  407. detected);
  408. /* Ask the new devices to introduce themselves. */
  409. deviceid = kmalloc(1024, GFP_KERNEL);
  410. if (!deviceid) return 0;
  411. for (daisy = 0; thisdev < numdevs; thisdev++, daisy++)
  412. parport_device_id(thisdev, deviceid, 1024);
  413. kfree(deviceid);
  414. return detected;
  415. }