daisy.c 13 KB

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