Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * IEEE 1284.3 Parallel port daisy chain and multiplexor code
0003  * 
0004  * Copyright (C) 1999, 2000  Tim Waugh <tim@cyberelk.demon.co.uk>
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License
0008  * as published by the Free Software Foundation; either version
0009  * 2 of the License, or (at your option) any later version.
0010  *
0011  * ??-12-1998: Initial implementation.
0012  * 31-01-1999: Make port-cloning transparent.
0013  * 13-02-1999: Move DeviceID technique from parport_probe.
0014  * 13-03-1999: Get DeviceID from non-IEEE 1284.3 devices too.
0015  * 22-02-2000: Count devices that are actually detected.
0016  *
0017  * Any part of this program may be used in documents licensed under
0018  * the GNU Free Documentation License, Version 1.1 or any later version
0019  * published by the Free Software Foundation.
0020  */
0021 
0022 #include <linux/module.h>
0023 #include <linux/parport.h>
0024 #include <linux/delay.h>
0025 #include <linux/slab.h>
0026 #include <linux/sched/signal.h>
0027 
0028 #include <asm/current.h>
0029 #include <linux/uaccess.h>
0030 
0031 #undef DEBUG
0032 
0033 static struct daisydev {
0034     struct daisydev *next;
0035     struct parport *port;
0036     int daisy;
0037     int devnum;
0038 } *topology = NULL;
0039 static DEFINE_SPINLOCK(topology_lock);
0040 
0041 static int numdevs;
0042 static bool daisy_init_done;
0043 
0044 /* Forward-declaration of lower-level functions. */
0045 static int mux_present(struct parport *port);
0046 static int num_mux_ports(struct parport *port);
0047 static int select_port(struct parport *port);
0048 static int assign_addrs(struct parport *port);
0049 
0050 /* Add a device to the discovered topology. */
0051 static void add_dev(int devnum, struct parport *port, int daisy)
0052 {
0053     struct daisydev *newdev, **p;
0054     newdev = kmalloc(sizeof(struct daisydev), GFP_KERNEL);
0055     if (newdev) {
0056         newdev->port = port;
0057         newdev->daisy = daisy;
0058         newdev->devnum = devnum;
0059         spin_lock(&topology_lock);
0060         for (p = &topology; *p && (*p)->devnum<devnum; p = &(*p)->next)
0061             ;
0062         newdev->next = *p;
0063         *p = newdev;
0064         spin_unlock(&topology_lock);
0065     }
0066 }
0067 
0068 /* Clone a parport (actually, make an alias). */
0069 static struct parport *clone_parport(struct parport *real, int muxport)
0070 {
0071     struct parport *extra = parport_register_port(real->base,
0072                                real->irq,
0073                                real->dma,
0074                                real->ops);
0075     if (extra) {
0076         extra->portnum = real->portnum;
0077         extra->physport = real;
0078         extra->muxport = muxport;
0079         real->slaves[muxport-1] = extra;
0080     }
0081 
0082     return extra;
0083 }
0084 
0085 static int daisy_drv_probe(struct pardevice *par_dev)
0086 {
0087     struct device_driver *drv = par_dev->dev.driver;
0088 
0089     if (strcmp(drv->name, "daisy_drv"))
0090         return -ENODEV;
0091     if (strcmp(par_dev->name, daisy_dev_name))
0092         return -ENODEV;
0093 
0094     return 0;
0095 }
0096 
0097 static struct parport_driver daisy_driver = {
0098     .name = "daisy_drv",
0099     .probe = daisy_drv_probe,
0100     .devmodel = true,
0101 };
0102 
0103 /* Discover the IEEE1284.3 topology on a port -- muxes and daisy chains.
0104  * Return value is number of devices actually detected. */
0105 int parport_daisy_init(struct parport *port)
0106 {
0107     int detected = 0;
0108     char *deviceid;
0109     static const char *th[] = { /*0*/"th", "st", "nd", "rd", "th" };
0110     int num_ports;
0111     int i;
0112     int last_try = 0;
0113 
0114     if (!daisy_init_done) {
0115         /*
0116          * flag should be marked true first as
0117          * parport_register_driver() might try to load the low
0118          * level driver which will lead to announcing new ports
0119          * and which will again come back here at
0120          * parport_daisy_init()
0121          */
0122         daisy_init_done = true;
0123         i = parport_register_driver(&daisy_driver);
0124         if (i) {
0125             pr_err("daisy registration failed\n");
0126             daisy_init_done = false;
0127             return i;
0128         }
0129     }
0130 
0131 again:
0132     /* Because this is called before any other devices exist,
0133      * we don't have to claim exclusive access.  */
0134 
0135     /* If mux present on normal port, need to create new
0136      * parports for each extra port. */
0137     if (port->muxport < 0 && mux_present(port) &&
0138         /* don't be fooled: a mux must have 2 or 4 ports. */
0139         ((num_ports = num_mux_ports(port)) == 2 || num_ports == 4)) {
0140         /* Leave original as port zero. */
0141         port->muxport = 0;
0142         pr_info("%s: 1st (default) port of %d-way multiplexor\n",
0143             port->name, num_ports);
0144         for (i = 1; i < num_ports; i++) {
0145             /* Clone the port. */
0146             struct parport *extra = clone_parport(port, i);
0147             if (!extra) {
0148                 if (signal_pending(current))
0149                     break;
0150 
0151                 schedule();
0152                 continue;
0153             }
0154 
0155             pr_info("%s: %d%s port of %d-way multiplexor on %s\n",
0156                 extra->name, i + 1, th[i + 1], num_ports,
0157                 port->name);
0158 
0159             /* Analyse that port too.  We won't recurse
0160                forever because of the 'port->muxport < 0'
0161                test above. */
0162             parport_daisy_init(extra);
0163         }
0164     }
0165 
0166     if (port->muxport >= 0)
0167         select_port(port);
0168 
0169     parport_daisy_deselect_all(port);
0170     detected += assign_addrs(port);
0171 
0172     /* Count the potential legacy device at the end. */
0173     add_dev(numdevs++, port, -1);
0174 
0175     /* Find out the legacy device's IEEE 1284 device ID. */
0176     deviceid = kmalloc(1024, GFP_KERNEL);
0177     if (deviceid) {
0178         if (parport_device_id(numdevs - 1, deviceid, 1024) > 2)
0179             detected++;
0180 
0181         kfree(deviceid);
0182     }
0183 
0184     if (!detected && !last_try) {
0185         /* No devices were detected.  Perhaps they are in some
0186                    funny state; let's try to reset them and see if
0187                    they wake up. */
0188         parport_daisy_fini(port);
0189         parport_write_control(port, PARPORT_CONTROL_SELECT);
0190         udelay(50);
0191         parport_write_control(port,
0192                        PARPORT_CONTROL_SELECT |
0193                        PARPORT_CONTROL_INIT);
0194         udelay(50);
0195         last_try = 1;
0196         goto again;
0197     }
0198 
0199     return detected;
0200 }
0201 
0202 /* Forget about devices on a physical port. */
0203 void parport_daisy_fini(struct parport *port)
0204 {
0205     struct daisydev **p;
0206 
0207     spin_lock(&topology_lock);
0208     p = &topology;
0209     while (*p) {
0210         struct daisydev *dev = *p;
0211         if (dev->port != port) {
0212             p = &dev->next;
0213             continue;
0214         }
0215         *p = dev->next;
0216         kfree(dev);
0217     }
0218 
0219     /* Gaps in the numbering could be handled better.  How should
0220            someone enumerate through all IEEE1284.3 devices in the
0221            topology?. */
0222     if (!topology) numdevs = 0;
0223     spin_unlock(&topology_lock);
0224     return;
0225 }
0226 
0227 /**
0228  *  parport_open - find a device by canonical device number
0229  *  @devnum: canonical device number
0230  *  @name: name to associate with the device
0231  *
0232  *  This function is similar to parport_register_device(), except
0233  *  that it locates a device by its number rather than by the port
0234  *  it is attached to.
0235  *
0236  *  All parameters except for @devnum are the same as for
0237  *  parport_register_device().  The return value is the same as
0238  *  for parport_register_device().
0239  **/
0240 
0241 struct pardevice *parport_open(int devnum, const char *name)
0242 {
0243     struct daisydev *p = topology;
0244     struct pardev_cb par_cb;
0245     struct parport *port;
0246     struct pardevice *dev;
0247     int daisy;
0248 
0249     memset(&par_cb, 0, sizeof(par_cb));
0250     spin_lock(&topology_lock);
0251     while (p && p->devnum != devnum)
0252         p = p->next;
0253 
0254     if (!p) {
0255         spin_unlock(&topology_lock);
0256         return NULL;
0257     }
0258 
0259     daisy = p->daisy;
0260     port = parport_get_port(p->port);
0261     spin_unlock(&topology_lock);
0262 
0263     dev = parport_register_dev_model(port, name, &par_cb, devnum);
0264     parport_put_port(port);
0265     if (!dev)
0266         return NULL;
0267 
0268     dev->daisy = daisy;
0269 
0270     /* Check that there really is a device to select. */
0271     if (daisy >= 0) {
0272         int selected;
0273         parport_claim_or_block(dev);
0274         selected = port->daisy;
0275         parport_release(dev);
0276 
0277         if (selected != daisy) {
0278             /* No corresponding device. */
0279             parport_unregister_device(dev);
0280             return NULL;
0281         }
0282     }
0283 
0284     return dev;
0285 }
0286 
0287 /**
0288  *  parport_close - close a device opened with parport_open()
0289  *  @dev: device to close
0290  *
0291  *  This is to parport_open() as parport_unregister_device() is to
0292  *  parport_register_device().
0293  **/
0294 
0295 void parport_close(struct pardevice *dev)
0296 {
0297     parport_unregister_device(dev);
0298 }
0299 
0300 /* Send a daisy-chain-style CPP command packet. */
0301 static int cpp_daisy(struct parport *port, int cmd)
0302 {
0303     unsigned char s;
0304 
0305     parport_data_forward(port);
0306     parport_write_data(port, 0xaa); udelay(2);
0307     parport_write_data(port, 0x55); udelay(2);
0308     parport_write_data(port, 0x00); udelay(2);
0309     parport_write_data(port, 0xff); udelay(2);
0310     s = parport_read_status(port) & (PARPORT_STATUS_BUSY
0311                       | PARPORT_STATUS_PAPEROUT
0312                       | PARPORT_STATUS_SELECT
0313                       | PARPORT_STATUS_ERROR);
0314     if (s != (PARPORT_STATUS_BUSY
0315           | PARPORT_STATUS_PAPEROUT
0316           | PARPORT_STATUS_SELECT
0317           | PARPORT_STATUS_ERROR)) {
0318         pr_debug("%s: cpp_daisy: aa5500ff(%02x)\n", port->name, s);
0319         return -ENXIO;
0320     }
0321 
0322     parport_write_data(port, 0x87); udelay(2);
0323     s = parport_read_status(port) & (PARPORT_STATUS_BUSY
0324                       | PARPORT_STATUS_PAPEROUT
0325                       | PARPORT_STATUS_SELECT
0326                       | PARPORT_STATUS_ERROR);
0327     if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
0328         pr_debug("%s: cpp_daisy: aa5500ff87(%02x)\n", port->name, s);
0329         return -ENXIO;
0330     }
0331 
0332     parport_write_data(port, 0x78); udelay(2);
0333     parport_write_data(port, cmd); udelay(2);
0334     parport_frob_control(port,
0335                   PARPORT_CONTROL_STROBE,
0336                   PARPORT_CONTROL_STROBE);
0337     udelay(1);
0338     s = parport_read_status(port);
0339     parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
0340     udelay(1);
0341     parport_write_data(port, 0xff); udelay(2);
0342 
0343     return s;
0344 }
0345 
0346 /* Send a mux-style CPP command packet. */
0347 static int cpp_mux(struct parport *port, int cmd)
0348 {
0349     unsigned char s;
0350     int rc;
0351 
0352     parport_data_forward(port);
0353     parport_write_data(port, 0xaa); udelay(2);
0354     parport_write_data(port, 0x55); udelay(2);
0355     parport_write_data(port, 0xf0); udelay(2);
0356     parport_write_data(port, 0x0f); udelay(2);
0357     parport_write_data(port, 0x52); udelay(2);
0358     parport_write_data(port, 0xad); udelay(2);
0359     parport_write_data(port, cmd); udelay(2);
0360 
0361     s = parport_read_status(port);
0362     if (!(s & PARPORT_STATUS_ACK)) {
0363         pr_debug("%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
0364              port->name, cmd, s);
0365         return -EIO;
0366     }
0367 
0368     rc = (((s & PARPORT_STATUS_SELECT   ? 1 : 0) << 0) |
0369           ((s & PARPORT_STATUS_PAPEROUT ? 1 : 0) << 1) |
0370           ((s & PARPORT_STATUS_BUSY     ? 0 : 1) << 2) |
0371           ((s & PARPORT_STATUS_ERROR    ? 0 : 1) << 3));
0372 
0373     return rc;
0374 }
0375 
0376 void parport_daisy_deselect_all(struct parport *port)
0377 {
0378     cpp_daisy(port, 0x30);
0379 }
0380 
0381 int parport_daisy_select(struct parport *port, int daisy, int mode)
0382 {
0383     switch (mode)
0384     {
0385         // For these modes we should switch to EPP mode:
0386         case IEEE1284_MODE_EPP:
0387         case IEEE1284_MODE_EPPSL:
0388         case IEEE1284_MODE_EPPSWE:
0389             return !(cpp_daisy(port, 0x20 + daisy) &
0390                  PARPORT_STATUS_ERROR);
0391 
0392         // For these modes we should switch to ECP mode:
0393         case IEEE1284_MODE_ECP:
0394         case IEEE1284_MODE_ECPRLE:
0395         case IEEE1284_MODE_ECPSWE: 
0396             return !(cpp_daisy(port, 0xd0 + daisy) &
0397                  PARPORT_STATUS_ERROR);
0398 
0399         // Nothing was told for BECP in Daisy chain specification.
0400         // May be it's wise to use ECP?
0401         case IEEE1284_MODE_BECP:
0402         // Others use compat mode
0403         case IEEE1284_MODE_NIBBLE:
0404         case IEEE1284_MODE_BYTE:
0405         case IEEE1284_MODE_COMPAT:
0406         default:
0407             return !(cpp_daisy(port, 0xe0 + daisy) &
0408                  PARPORT_STATUS_ERROR);
0409     }
0410 }
0411 
0412 static int mux_present(struct parport *port)
0413 {
0414     return cpp_mux(port, 0x51) == 3;
0415 }
0416 
0417 static int num_mux_ports(struct parport *port)
0418 {
0419     return cpp_mux(port, 0x58);
0420 }
0421 
0422 static int select_port(struct parport *port)
0423 {
0424     int muxport = port->muxport;
0425     return cpp_mux(port, 0x60 + muxport) == muxport;
0426 }
0427 
0428 static int assign_addrs(struct parport *port)
0429 {
0430     unsigned char s;
0431     unsigned char daisy;
0432     int thisdev = numdevs;
0433     int detected;
0434     char *deviceid;
0435 
0436     parport_data_forward(port);
0437     parport_write_data(port, 0xaa); udelay(2);
0438     parport_write_data(port, 0x55); udelay(2);
0439     parport_write_data(port, 0x00); udelay(2);
0440     parport_write_data(port, 0xff); udelay(2);
0441     s = parport_read_status(port) & (PARPORT_STATUS_BUSY
0442                       | PARPORT_STATUS_PAPEROUT
0443                       | PARPORT_STATUS_SELECT
0444                       | PARPORT_STATUS_ERROR);
0445     if (s != (PARPORT_STATUS_BUSY
0446           | PARPORT_STATUS_PAPEROUT
0447           | PARPORT_STATUS_SELECT
0448           | PARPORT_STATUS_ERROR)) {
0449         pr_debug("%s: assign_addrs: aa5500ff(%02x)\n", port->name, s);
0450         return 0;
0451     }
0452 
0453     parport_write_data(port, 0x87); udelay(2);
0454     s = parport_read_status(port) & (PARPORT_STATUS_BUSY
0455                       | PARPORT_STATUS_PAPEROUT
0456                       | PARPORT_STATUS_SELECT
0457                       | PARPORT_STATUS_ERROR);
0458     if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
0459         pr_debug("%s: assign_addrs: aa5500ff87(%02x)\n", port->name, s);
0460         return 0;
0461     }
0462 
0463     parport_write_data(port, 0x78); udelay(2);
0464     s = parport_read_status(port);
0465 
0466     for (daisy = 0;
0467          (s & (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT))
0468              == (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT)
0469              && daisy < 4;
0470          ++daisy) {
0471         parport_write_data(port, daisy);
0472         udelay(2);
0473         parport_frob_control(port,
0474                       PARPORT_CONTROL_STROBE,
0475                       PARPORT_CONTROL_STROBE);
0476         udelay(1);
0477         parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
0478         udelay(1);
0479 
0480         add_dev(numdevs++, port, daisy);
0481 
0482         /* See if this device thought it was the last in the
0483          * chain. */
0484         if (!(s & PARPORT_STATUS_BUSY))
0485             break;
0486 
0487         /* We are seeing pass through status now. We see
0488            last_dev from next device or if last_dev does not
0489            work status lines from some non-daisy chain
0490            device. */
0491         s = parport_read_status(port);
0492     }
0493 
0494     parport_write_data(port, 0xff); udelay(2);
0495     detected = numdevs - thisdev;
0496     pr_debug("%s: Found %d daisy-chained devices\n", port->name, detected);
0497 
0498     /* Ask the new devices to introduce themselves. */
0499     deviceid = kmalloc(1024, GFP_KERNEL);
0500     if (!deviceid) return 0;
0501 
0502     for (daisy = 0; thisdev < numdevs; thisdev++, daisy++)
0503         parport_device_id(thisdev, deviceid, 1024);
0504 
0505     kfree(deviceid);
0506     return detected;
0507 }