Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*****************************************************************************/
0003 
0004 /*
0005  *  uss720.c  --  USS720 USB Parport Cable.
0006  *
0007  *  Copyright (C) 1999, 2005, 2010
0008  *      Thomas Sailer (t.sailer@alumni.ethz.ch)
0009  *
0010  *  Based on parport_pc.c
0011  *
0012  *  History:
0013  *   0.1  04.08.1999  Created
0014  *   0.2  07.08.1999  Some fixes mainly suggested by Tim Waugh
0015  *            Interrupt handling currently disabled because
0016  *            usb_request_irq crashes somewhere within ohci.c
0017  *            for no apparent reason (that is for me, anyway)
0018  *            ECP currently untested
0019  *   0.3  10.08.1999  fixing merge errors
0020  *   0.4  13.08.1999  Added Vendor/Product ID of Brad Hard's cable
0021  *   0.5  20.09.1999  usb_control_msg wrapper used
0022  *        Nov01.2000  usb_device_table support by Adam J. Richter
0023  *        08.04.2001  Identify version on module load.  gb
0024  *   0.6  02.09.2005  Fix "scheduling in interrupt" problem by making save/restore
0025  *                    context asynchronous
0026  *
0027  */
0028 
0029 /*****************************************************************************/
0030 
0031 #include <linux/module.h>
0032 #include <linux/socket.h>
0033 #include <linux/parport.h>
0034 #include <linux/init.h>
0035 #include <linux/usb.h>
0036 #include <linux/delay.h>
0037 #include <linux/completion.h>
0038 #include <linux/kref.h>
0039 #include <linux/slab.h>
0040 #include <linux/sched/signal.h>
0041 
0042 #define DRIVER_AUTHOR "Thomas M. Sailer, t.sailer@alumni.ethz.ch"
0043 #define DRIVER_DESC "USB Parport Cable driver for Cables using the Lucent Technologies USS720 Chip"
0044 
0045 /* --------------------------------------------------------------------- */
0046 
0047 struct parport_uss720_private {
0048     struct usb_device *usbdev;
0049     struct parport *pp;
0050     struct kref ref_count;
0051     __u8 reg[7];  /* USB registers */
0052     struct list_head asynclist;
0053     spinlock_t asynclock;
0054 };
0055 
0056 struct uss720_async_request {
0057     struct parport_uss720_private *priv;
0058     struct kref ref_count;
0059     struct list_head asynclist;
0060     struct completion compl;
0061     struct urb *urb;
0062     struct usb_ctrlrequest *dr;
0063     __u8 reg[7];
0064 };
0065 
0066 /* --------------------------------------------------------------------- */
0067 
0068 static void destroy_priv(struct kref *kref)
0069 {
0070     struct parport_uss720_private *priv = container_of(kref, struct parport_uss720_private, ref_count);
0071 
0072     dev_dbg(&priv->usbdev->dev, "destroying priv datastructure\n");
0073     usb_put_dev(priv->usbdev);
0074     priv->usbdev = NULL;
0075     kfree(priv);
0076 }
0077 
0078 static void destroy_async(struct kref *kref)
0079 {
0080     struct uss720_async_request *rq = container_of(kref, struct uss720_async_request, ref_count);
0081     struct parport_uss720_private *priv = rq->priv;
0082     unsigned long flags;
0083 
0084     if (likely(rq->urb))
0085         usb_free_urb(rq->urb);
0086     kfree(rq->dr);
0087     spin_lock_irqsave(&priv->asynclock, flags);
0088     list_del_init(&rq->asynclist);
0089     spin_unlock_irqrestore(&priv->asynclock, flags);
0090     kfree(rq);
0091     kref_put(&priv->ref_count, destroy_priv);
0092 }
0093 
0094 /* --------------------------------------------------------------------- */
0095 
0096 static void async_complete(struct urb *urb)
0097 {
0098     struct uss720_async_request *rq;
0099     struct parport *pp;
0100     struct parport_uss720_private *priv;
0101     int status = urb->status;
0102 
0103     rq = urb->context;
0104     priv = rq->priv;
0105     pp = priv->pp;
0106     if (status) {
0107         dev_err(&urb->dev->dev, "async_complete: urb error %d\n",
0108             status);
0109     } else if (rq->dr->bRequest == 3) {
0110         memcpy(priv->reg, rq->reg, sizeof(priv->reg));
0111 #if 0
0112         dev_dbg(&priv->usbdev->dev, "async_complete regs %7ph\n",
0113             priv->reg);
0114 #endif
0115         /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
0116         if (rq->reg[2] & rq->reg[1] & 0x10 && pp)
0117             parport_generic_irq(pp);
0118     }
0119     complete(&rq->compl);
0120     kref_put(&rq->ref_count, destroy_async);
0121 }
0122 
0123 static struct uss720_async_request *submit_async_request(struct parport_uss720_private *priv,
0124                              __u8 request, __u8 requesttype, __u16 value, __u16 index,
0125                              gfp_t mem_flags)
0126 {
0127     struct usb_device *usbdev;
0128     struct uss720_async_request *rq;
0129     unsigned long flags;
0130     int ret;
0131 
0132     if (!priv)
0133         return NULL;
0134     usbdev = priv->usbdev;
0135     if (!usbdev)
0136         return NULL;
0137     rq = kzalloc(sizeof(struct uss720_async_request), mem_flags);
0138     if (!rq)
0139         return NULL;
0140     kref_init(&rq->ref_count);
0141     INIT_LIST_HEAD(&rq->asynclist);
0142     init_completion(&rq->compl);
0143     kref_get(&priv->ref_count);
0144     rq->priv = priv;
0145     rq->urb = usb_alloc_urb(0, mem_flags);
0146     if (!rq->urb) {
0147         kref_put(&rq->ref_count, destroy_async);
0148         return NULL;
0149     }
0150     rq->dr = kmalloc(sizeof(*rq->dr), mem_flags);
0151     if (!rq->dr) {
0152         kref_put(&rq->ref_count, destroy_async);
0153         return NULL;
0154     }
0155     rq->dr->bRequestType = requesttype;
0156     rq->dr->bRequest = request;
0157     rq->dr->wValue = cpu_to_le16(value);
0158     rq->dr->wIndex = cpu_to_le16(index);
0159     rq->dr->wLength = cpu_to_le16((request == 3) ? sizeof(rq->reg) : 0);
0160     usb_fill_control_urb(rq->urb, usbdev, (requesttype & 0x80) ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0),
0161                  (unsigned char *)rq->dr,
0162                  (request == 3) ? rq->reg : NULL, (request == 3) ? sizeof(rq->reg) : 0, async_complete, rq);
0163     /* rq->urb->transfer_flags |= URB_ASYNC_UNLINK; */
0164     spin_lock_irqsave(&priv->asynclock, flags);
0165     list_add_tail(&rq->asynclist, &priv->asynclist);
0166     spin_unlock_irqrestore(&priv->asynclock, flags);
0167     kref_get(&rq->ref_count);
0168     ret = usb_submit_urb(rq->urb, mem_flags);
0169     if (!ret)
0170         return rq;
0171     destroy_async(&rq->ref_count);
0172     dev_err(&usbdev->dev, "submit_async_request submit_urb failed with %d\n", ret);
0173     return NULL;
0174 }
0175 
0176 static unsigned int kill_all_async_requests_priv(struct parport_uss720_private *priv)
0177 {
0178     struct uss720_async_request *rq;
0179     unsigned long flags;
0180     unsigned int ret = 0;
0181 
0182     spin_lock_irqsave(&priv->asynclock, flags);
0183     list_for_each_entry(rq, &priv->asynclist, asynclist) {
0184         usb_unlink_urb(rq->urb);
0185         ret++;
0186     }
0187     spin_unlock_irqrestore(&priv->asynclock, flags);
0188     return ret;
0189 }
0190 
0191 /* --------------------------------------------------------------------- */
0192 
0193 static int get_1284_register(struct parport *pp, unsigned char reg, unsigned char *val, gfp_t mem_flags)
0194 {
0195     struct parport_uss720_private *priv;
0196     struct uss720_async_request *rq;
0197     static const unsigned char regindex[9] = {
0198         4, 0, 1, 5, 5, 0, 2, 3, 6
0199     };
0200     int ret;
0201 
0202     if (!pp)
0203         return -EIO;
0204     priv = pp->private_data;
0205     rq = submit_async_request(priv, 3, 0xc0, ((unsigned int)reg) << 8, 0, mem_flags);
0206     if (!rq) {
0207         dev_err(&priv->usbdev->dev, "get_1284_register(%u) failed",
0208             (unsigned int)reg);
0209         return -EIO;
0210     }
0211     if (!val) {
0212         kref_put(&rq->ref_count, destroy_async);
0213         return 0;
0214     }
0215     if (wait_for_completion_timeout(&rq->compl, HZ)) {
0216         ret = rq->urb->status;
0217         *val = priv->reg[(reg >= 9) ? 0 : regindex[reg]];
0218         if (ret)
0219             printk(KERN_WARNING "get_1284_register: "
0220                    "usb error %d\n", ret);
0221         kref_put(&rq->ref_count, destroy_async);
0222         return ret;
0223     }
0224     printk(KERN_WARNING "get_1284_register timeout\n");
0225     kill_all_async_requests_priv(priv);
0226     return -EIO;
0227 }
0228 
0229 static int set_1284_register(struct parport *pp, unsigned char reg, unsigned char val, gfp_t mem_flags)
0230 {
0231     struct parport_uss720_private *priv;
0232     struct uss720_async_request *rq;
0233 
0234     if (!pp)
0235         return -EIO;
0236     priv = pp->private_data;
0237     rq = submit_async_request(priv, 4, 0x40, (((unsigned int)reg) << 8) | val, 0, mem_flags);
0238     if (!rq) {
0239         dev_err(&priv->usbdev->dev, "set_1284_register(%u,%u) failed",
0240             (unsigned int)reg, (unsigned int)val);
0241         return -EIO;
0242     }
0243     kref_put(&rq->ref_count, destroy_async);
0244     return 0;
0245 }
0246 
0247 /* --------------------------------------------------------------------- */
0248 
0249 /* ECR modes */
0250 #define ECR_SPP 00
0251 #define ECR_PS2 01
0252 #define ECR_PPF 02
0253 #define ECR_ECP 03
0254 #define ECR_EPP 04
0255 
0256 /* Safely change the mode bits in the ECR */
0257 static int change_mode(struct parport *pp, int m)
0258 {
0259     struct parport_uss720_private *priv = pp->private_data;
0260     int mode;
0261     __u8 reg;
0262 
0263     if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
0264         return -EIO;
0265     /* Bits <7:5> contain the mode. */
0266     mode = (priv->reg[2] >> 5) & 0x7;
0267     if (mode == m)
0268         return 0;
0269     /* We have to go through mode 000 or 001 */
0270     if (mode > ECR_PS2 && m > ECR_PS2)
0271         if (change_mode(pp, ECR_PS2))
0272             return -EIO;
0273 
0274     if (m <= ECR_PS2 && !(priv->reg[1] & 0x20)) {
0275         /* This mode resets the FIFO, so we may
0276          * have to wait for it to drain first. */
0277         unsigned long expire = jiffies + pp->physport->cad->timeout;
0278         switch (mode) {
0279         case ECR_PPF: /* Parallel Port FIFO mode */
0280         case ECR_ECP: /* ECP Parallel Port mode */
0281             /* Poll slowly. */
0282             for (;;) {
0283                 if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
0284                     return -EIO;
0285                 if (priv->reg[2] & 0x01)
0286                     break;
0287                 if (time_after_eq (jiffies, expire))
0288                     /* The FIFO is stuck. */
0289                     return -EBUSY;
0290                 msleep_interruptible(10);
0291                 if (signal_pending (current))
0292                     break;
0293             }
0294         }
0295     }
0296     /* Set the mode. */
0297     if (set_1284_register(pp, 6, m << 5, GFP_KERNEL))
0298         return -EIO;
0299     if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
0300         return -EIO;
0301     return 0;
0302 }
0303 
0304 /*
0305  * Clear TIMEOUT BIT in EPP MODE
0306  */
0307 static int clear_epp_timeout(struct parport *pp)
0308 {
0309     unsigned char stat;
0310 
0311     if (get_1284_register(pp, 1, &stat, GFP_KERNEL))
0312         return 1;
0313     return stat & 1;
0314 }
0315 
0316 /*
0317  * Access functions.
0318  */
0319 #if 0
0320 static int uss720_irq(int usbstatus, void *buffer, int len, void *dev_id)
0321 {
0322     struct parport *pp = (struct parport *)dev_id;
0323     struct parport_uss720_private *priv = pp->private_data; 
0324 
0325     if (usbstatus != 0 || len < 4 || !buffer)
0326         return 1;
0327     memcpy(priv->reg, buffer, 4);
0328     /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
0329     if (priv->reg[2] & priv->reg[1] & 0x10)
0330         parport_generic_irq(pp);
0331     return 1;
0332 }
0333 #endif
0334 
0335 static void parport_uss720_write_data(struct parport *pp, unsigned char d)
0336 {
0337     set_1284_register(pp, 0, d, GFP_KERNEL);
0338 }
0339 
0340 static unsigned char parport_uss720_read_data(struct parport *pp)
0341 {
0342     unsigned char ret;
0343 
0344     if (get_1284_register(pp, 0, &ret, GFP_KERNEL))
0345         return 0;
0346     return ret;
0347 }
0348 
0349 static void parport_uss720_write_control(struct parport *pp, unsigned char d)
0350 {
0351     struct parport_uss720_private *priv = pp->private_data; 
0352 
0353     d = (d & 0xf) | (priv->reg[1] & 0xf0);
0354     if (set_1284_register(pp, 2, d, GFP_KERNEL))
0355         return;
0356     priv->reg[1] = d;
0357 }
0358 
0359 static unsigned char parport_uss720_read_control(struct parport *pp)
0360 {
0361     struct parport_uss720_private *priv = pp->private_data; 
0362     return priv->reg[1] & 0xf; /* Use soft copy */
0363 }
0364 
0365 static unsigned char parport_uss720_frob_control(struct parport *pp, unsigned char mask, unsigned char val)
0366 {
0367     struct parport_uss720_private *priv = pp->private_data; 
0368     unsigned char d;
0369 
0370     mask &= 0x0f;
0371     val &= 0x0f;
0372     d = (priv->reg[1] & (~mask)) ^ val;
0373     if (set_1284_register(pp, 2, d, GFP_ATOMIC))
0374         return 0;
0375     priv->reg[1] = d;
0376     return d & 0xf;
0377 }
0378 
0379 static unsigned char parport_uss720_read_status(struct parport *pp)
0380 {
0381     unsigned char ret;
0382 
0383     if (get_1284_register(pp, 1, &ret, GFP_ATOMIC))
0384         return 0;
0385     return ret & 0xf8;
0386 }
0387 
0388 static void parport_uss720_disable_irq(struct parport *pp)
0389 {
0390     struct parport_uss720_private *priv = pp->private_data; 
0391     unsigned char d;
0392 
0393     d = priv->reg[1] & ~0x10;
0394     if (set_1284_register(pp, 2, d, GFP_KERNEL))
0395         return;
0396     priv->reg[1] = d;
0397 }
0398 
0399 static void parport_uss720_enable_irq(struct parport *pp)
0400 {
0401     struct parport_uss720_private *priv = pp->private_data; 
0402     unsigned char d;
0403 
0404     d = priv->reg[1] | 0x10;
0405     if (set_1284_register(pp, 2, d, GFP_KERNEL))
0406         return;
0407     priv->reg[1] = d;
0408 }
0409 
0410 static void parport_uss720_data_forward (struct parport *pp)
0411 {
0412     struct parport_uss720_private *priv = pp->private_data; 
0413     unsigned char d;
0414 
0415     d = priv->reg[1] & ~0x20;
0416     if (set_1284_register(pp, 2, d, GFP_KERNEL))
0417         return;
0418     priv->reg[1] = d;
0419 }
0420 
0421 static void parport_uss720_data_reverse (struct parport *pp)
0422 {
0423     struct parport_uss720_private *priv = pp->private_data; 
0424     unsigned char d;
0425 
0426     d = priv->reg[1] | 0x20;
0427     if (set_1284_register(pp, 2, d, GFP_KERNEL))
0428         return;
0429     priv->reg[1] = d;
0430 }
0431 
0432 static void parport_uss720_init_state(struct pardevice *dev, struct parport_state *s)
0433 {
0434     s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
0435     s->u.pc.ecr = 0x24;
0436 }
0437 
0438 static void parport_uss720_save_state(struct parport *pp, struct parport_state *s)
0439 {
0440     struct parport_uss720_private *priv = pp->private_data; 
0441 
0442 #if 0
0443     if (get_1284_register(pp, 2, NULL, GFP_ATOMIC))
0444         return;
0445 #endif
0446     s->u.pc.ctr = priv->reg[1];
0447     s->u.pc.ecr = priv->reg[2];
0448 }
0449 
0450 static void parport_uss720_restore_state(struct parport *pp, struct parport_state *s)
0451 {
0452     struct parport_uss720_private *priv = pp->private_data;
0453 
0454     set_1284_register(pp, 2, s->u.pc.ctr, GFP_ATOMIC);
0455     set_1284_register(pp, 6, s->u.pc.ecr, GFP_ATOMIC);
0456     get_1284_register(pp, 2, NULL, GFP_ATOMIC);
0457     priv->reg[1] = s->u.pc.ctr;
0458     priv->reg[2] = s->u.pc.ecr;
0459 }
0460 
0461 static size_t parport_uss720_epp_read_data(struct parport *pp, void *buf, size_t length, int flags)
0462 {
0463     struct parport_uss720_private *priv = pp->private_data; 
0464     size_t got = 0;
0465 
0466     if (change_mode(pp, ECR_EPP))
0467         return 0;
0468     for (; got < length; got++) {
0469         if (get_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
0470             break;
0471         buf++;
0472         if (priv->reg[0] & 0x01) {
0473             clear_epp_timeout(pp);
0474             break;
0475         }
0476     }
0477     change_mode(pp, ECR_PS2);
0478     return got;
0479 }
0480 
0481 static size_t parport_uss720_epp_write_data(struct parport *pp, const void *buf, size_t length, int flags)
0482 {
0483 #if 0
0484     struct parport_uss720_private *priv = pp->private_data; 
0485     size_t written = 0;
0486 
0487     if (change_mode(pp, ECR_EPP))
0488         return 0;
0489     for (; written < length; written++) {
0490         if (set_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
0491             break;
0492         ((char*)buf)++;
0493         if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
0494             break;
0495         if (priv->reg[0] & 0x01) {
0496             clear_epp_timeout(pp);
0497             break;
0498         }
0499     }
0500     change_mode(pp, ECR_PS2);
0501     return written;
0502 #else
0503     struct parport_uss720_private *priv = pp->private_data;
0504     struct usb_device *usbdev = priv->usbdev;
0505     int rlen;
0506     int i;
0507 
0508     if (!usbdev)
0509         return 0;
0510     if (change_mode(pp, ECR_EPP))
0511         return 0;
0512     i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buf, length, &rlen, 20000);
0513     if (i)
0514         printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %zu rlen %u\n", buf, length, rlen);
0515     change_mode(pp, ECR_PS2);
0516     return rlen;
0517 #endif
0518 }
0519 
0520 static size_t parport_uss720_epp_read_addr(struct parport *pp, void *buf, size_t length, int flags)
0521 {
0522     struct parport_uss720_private *priv = pp->private_data; 
0523     size_t got = 0;
0524 
0525     if (change_mode(pp, ECR_EPP))
0526         return 0;
0527     for (; got < length; got++) {
0528         if (get_1284_register(pp, 3, (char *)buf, GFP_KERNEL))
0529             break;
0530         buf++;
0531         if (priv->reg[0] & 0x01) {
0532             clear_epp_timeout(pp);
0533             break;
0534         }
0535     }
0536     change_mode(pp, ECR_PS2);
0537     return got;
0538 }
0539 
0540 static size_t parport_uss720_epp_write_addr(struct parport *pp, const void *buf, size_t length, int flags)
0541 {
0542     struct parport_uss720_private *priv = pp->private_data; 
0543     size_t written = 0;
0544 
0545     if (change_mode(pp, ECR_EPP))
0546         return 0;
0547     for (; written < length; written++) {
0548         if (set_1284_register(pp, 3, *(char *)buf, GFP_KERNEL))
0549             break;
0550         buf++;
0551         if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
0552             break;
0553         if (priv->reg[0] & 0x01) {
0554             clear_epp_timeout(pp);
0555             break;
0556         }
0557     }
0558     change_mode(pp, ECR_PS2);
0559     return written;
0560 }
0561 
0562 static size_t parport_uss720_ecp_write_data(struct parport *pp, const void *buffer, size_t len, int flags)
0563 {
0564     struct parport_uss720_private *priv = pp->private_data;
0565     struct usb_device *usbdev = priv->usbdev;
0566     int rlen;
0567     int i;
0568 
0569     if (!usbdev)
0570         return 0;
0571     if (change_mode(pp, ECR_ECP))
0572         return 0;
0573     i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
0574     if (i)
0575         printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %zu rlen %u\n", buffer, len, rlen);
0576     change_mode(pp, ECR_PS2);
0577     return rlen;
0578 }
0579 
0580 static size_t parport_uss720_ecp_read_data(struct parport *pp, void *buffer, size_t len, int flags)
0581 {
0582     struct parport_uss720_private *priv = pp->private_data;
0583     struct usb_device *usbdev = priv->usbdev;
0584     int rlen;
0585     int i;
0586 
0587     if (!usbdev)
0588         return 0;
0589     if (change_mode(pp, ECR_ECP))
0590         return 0;
0591     i = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), buffer, len, &rlen, 20000);
0592     if (i)
0593         printk(KERN_ERR "uss720: recvbulk ep 2 buf %p len %zu rlen %u\n", buffer, len, rlen);
0594     change_mode(pp, ECR_PS2);
0595     return rlen;
0596 }
0597 
0598 static size_t parport_uss720_ecp_write_addr(struct parport *pp, const void *buffer, size_t len, int flags)
0599 {
0600     size_t written = 0;
0601 
0602     if (change_mode(pp, ECR_ECP))
0603         return 0;
0604     for (; written < len; written++) {
0605         if (set_1284_register(pp, 5, *(char *)buffer, GFP_KERNEL))
0606             break;
0607         buffer++;
0608     }
0609     change_mode(pp, ECR_PS2);
0610     return written;
0611 }
0612 
0613 static size_t parport_uss720_write_compat(struct parport *pp, const void *buffer, size_t len, int flags)
0614 {
0615     struct parport_uss720_private *priv = pp->private_data;
0616     struct usb_device *usbdev = priv->usbdev;
0617     int rlen;
0618     int i;
0619 
0620     if (!usbdev)
0621         return 0;
0622     if (change_mode(pp, ECR_PPF))
0623         return 0;
0624     i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
0625     if (i)
0626         printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %zu rlen %u\n", buffer, len, rlen);
0627     change_mode(pp, ECR_PS2);
0628     return rlen;
0629 }
0630 
0631 /* --------------------------------------------------------------------- */
0632 
0633 static struct parport_operations parport_uss720_ops = 
0634 {
0635     .owner =        THIS_MODULE,
0636     .write_data =       parport_uss720_write_data,
0637     .read_data =        parport_uss720_read_data,
0638 
0639     .write_control =    parport_uss720_write_control,
0640     .read_control =     parport_uss720_read_control,
0641     .frob_control =     parport_uss720_frob_control,
0642 
0643     .read_status =      parport_uss720_read_status,
0644 
0645     .enable_irq =       parport_uss720_enable_irq,
0646     .disable_irq =      parport_uss720_disable_irq,
0647 
0648     .data_forward =     parport_uss720_data_forward,
0649     .data_reverse =     parport_uss720_data_reverse,
0650 
0651     .init_state =       parport_uss720_init_state,
0652     .save_state =       parport_uss720_save_state,
0653     .restore_state =    parport_uss720_restore_state,
0654 
0655     .epp_write_data =   parport_uss720_epp_write_data,
0656     .epp_read_data =    parport_uss720_epp_read_data,
0657     .epp_write_addr =   parport_uss720_epp_write_addr,
0658     .epp_read_addr =    parport_uss720_epp_read_addr,
0659 
0660     .ecp_write_data =   parport_uss720_ecp_write_data,
0661     .ecp_read_data =    parport_uss720_ecp_read_data,
0662     .ecp_write_addr =   parport_uss720_ecp_write_addr,
0663 
0664     .compat_write_data =    parport_uss720_write_compat,
0665     .nibble_read_data = parport_ieee1284_read_nibble,
0666     .byte_read_data =   parport_ieee1284_read_byte,
0667 };
0668 
0669 /* --------------------------------------------------------------------- */
0670 
0671 static int uss720_probe(struct usb_interface *intf,
0672             const struct usb_device_id *id)
0673 {
0674     struct usb_device *usbdev = usb_get_dev(interface_to_usbdev(intf));
0675     struct usb_host_interface *interface;
0676     struct usb_endpoint_descriptor *epd;
0677     struct parport_uss720_private *priv;
0678     struct parport *pp;
0679     unsigned char reg;
0680     int i;
0681 
0682     dev_dbg(&intf->dev, "probe: vendor id 0x%x, device id 0x%x\n",
0683         le16_to_cpu(usbdev->descriptor.idVendor),
0684         le16_to_cpu(usbdev->descriptor.idProduct));
0685 
0686     /* our known interfaces have 3 alternate settings */
0687     if (intf->num_altsetting != 3) {
0688         usb_put_dev(usbdev);
0689         return -ENODEV;
0690     }
0691     i = usb_set_interface(usbdev, intf->altsetting->desc.bInterfaceNumber, 2);
0692     dev_dbg(&intf->dev, "set interface result %d\n", i);
0693 
0694     interface = intf->cur_altsetting;
0695 
0696     if (interface->desc.bNumEndpoints < 3) {
0697         usb_put_dev(usbdev);
0698         return -ENODEV;
0699     }
0700 
0701     /*
0702      * Allocate parport interface 
0703      */
0704     priv = kzalloc(sizeof(struct parport_uss720_private), GFP_KERNEL);
0705     if (!priv) {
0706         usb_put_dev(usbdev);
0707         return -ENOMEM;
0708     }
0709     priv->pp = NULL;
0710     priv->usbdev = usbdev;
0711     kref_init(&priv->ref_count);
0712     spin_lock_init(&priv->asynclock);
0713     INIT_LIST_HEAD(&priv->asynclist);
0714     pp = parport_register_port(0, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &parport_uss720_ops);
0715     if (!pp) {
0716         printk(KERN_WARNING "uss720: could not register parport\n");
0717         goto probe_abort;
0718     }
0719 
0720     priv->pp = pp;
0721     pp->private_data = priv;
0722     pp->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_TRISTATE | PARPORT_MODE_EPP | PARPORT_MODE_ECP | PARPORT_MODE_COMPAT;
0723 
0724     /* set the USS720 control register to manual mode, no ECP compression, enable all ints */
0725     set_1284_register(pp, 7, 0x00, GFP_KERNEL);
0726     set_1284_register(pp, 6, 0x30, GFP_KERNEL);  /* PS/2 mode */
0727     set_1284_register(pp, 2, 0x0c, GFP_KERNEL);
0728     /* debugging */
0729     get_1284_register(pp, 0, &reg, GFP_KERNEL);
0730     dev_dbg(&intf->dev, "reg: %7ph\n", priv->reg);
0731 
0732     i = usb_find_last_int_in_endpoint(interface, &epd);
0733     if (!i) {
0734         dev_dbg(&intf->dev, "epaddr %d interval %d\n",
0735                 epd->bEndpointAddress, epd->bInterval);
0736     }
0737     parport_announce_port(pp);
0738 
0739     usb_set_intfdata(intf, pp);
0740     return 0;
0741 
0742 probe_abort:
0743     kill_all_async_requests_priv(priv);
0744     kref_put(&priv->ref_count, destroy_priv);
0745     return -ENODEV;
0746 }
0747 
0748 static void uss720_disconnect(struct usb_interface *intf)
0749 {
0750     struct parport *pp = usb_get_intfdata(intf);
0751     struct parport_uss720_private *priv;
0752 
0753     dev_dbg(&intf->dev, "disconnect\n");
0754     usb_set_intfdata(intf, NULL);
0755     if (pp) {
0756         priv = pp->private_data;
0757         priv->pp = NULL;
0758         dev_dbg(&intf->dev, "parport_remove_port\n");
0759         parport_remove_port(pp);
0760         parport_put_port(pp);
0761         kill_all_async_requests_priv(priv);
0762         kref_put(&priv->ref_count, destroy_priv);
0763     }
0764     dev_dbg(&intf->dev, "disconnect done\n");
0765 }
0766 
0767 /* table of cables that work through this driver */
0768 static const struct usb_device_id uss720_table[] = {
0769     { USB_DEVICE(0x047e, 0x1001) },
0770     { USB_DEVICE(0x04b8, 0x0002) },
0771     { USB_DEVICE(0x04b8, 0x0003) },
0772     { USB_DEVICE(0x050d, 0x0002) },
0773     { USB_DEVICE(0x050d, 0x1202) },
0774     { USB_DEVICE(0x0557, 0x2001) },
0775     { USB_DEVICE(0x05ab, 0x0002) },
0776     { USB_DEVICE(0x06c6, 0x0100) },
0777     { USB_DEVICE(0x0729, 0x1284) },
0778     { USB_DEVICE(0x1293, 0x0002) },
0779     { }                     /* Terminating entry */
0780 };
0781 
0782 MODULE_DEVICE_TABLE (usb, uss720_table);
0783 
0784 
0785 static struct usb_driver uss720_driver = {
0786     .name =     "uss720",
0787     .probe =    uss720_probe,
0788     .disconnect =   uss720_disconnect,
0789     .id_table = uss720_table,
0790 };
0791 
0792 /* --------------------------------------------------------------------- */
0793 
0794 MODULE_AUTHOR(DRIVER_AUTHOR);
0795 MODULE_DESCRIPTION(DRIVER_DESC);
0796 MODULE_LICENSE("GPL");
0797 
0798 static int __init uss720_init(void)
0799 {
0800     int retval;
0801     retval = usb_register(&uss720_driver);
0802     if (retval)
0803         goto out;
0804 
0805     printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
0806     printk(KERN_INFO KBUILD_MODNAME ": NOTE: this is a special purpose "
0807            "driver to allow nonstandard\n");
0808     printk(KERN_INFO KBUILD_MODNAME ": protocols (eg. bitbang) over "
0809            "USS720 usb to parallel cables\n");
0810     printk(KERN_INFO KBUILD_MODNAME ": If you just want to connect to a "
0811            "printer, use usblp instead\n");
0812 out:
0813     return retval;
0814 }
0815 
0816 static void __exit uss720_cleanup(void)
0817 {
0818     usb_deregister(&uss720_driver);
0819 }
0820 
0821 module_init(uss720_init);
0822 module_exit(uss720_cleanup);
0823 
0824 /* --------------------------------------------------------------------- */