1 /*****************************************************************************/
4 * uss720.c -- USS720 USB Parport Cable.
6 * Copyright (C) 1999, 2005, 2010
7 * Thomas Sailer (t.sailer@alumni.ethz.ch)
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Based on parport_pc.c
26 * 0.1 04.08.1999 Created
27 * 0.2 07.08.1999 Some fixes mainly suggested by Tim Waugh
28 * Interrupt handling currently disabled because
29 * usb_request_irq crashes somewhere within ohci.c
30 * for no apparent reason (that is for me, anyway)
31 * ECP currently untested
32 * 0.3 10.08.1999 fixing merge errors
33 * 0.4 13.08.1999 Added Vendor/Product ID of Brad Hard's cable
34 * 0.5 20.09.1999 usb_control_msg wrapper used
35 * Nov01.2000 usb_device_table support by Adam J. Richter
36 * 08.04.2001 Identify version on module load. gb
37 * 0.6 02.09.2005 Fix "scheduling in interrupt" problem by making save/restore
38 * context asynchronous
42 /*****************************************************************************/
44 #include <linux/module.h>
45 #include <linux/socket.h>
46 #include <linux/parport.h>
47 #include <linux/init.h>
48 #include <linux/usb.h>
49 #include <linux/delay.h>
50 #include <linux/completion.h>
51 #include <linux/kref.h>
52 #include <linux/slab.h>
53 #include <linux/sched/signal.h>
55 #define DRIVER_AUTHOR "Thomas M. Sailer, t.sailer@alumni.ethz.ch"
56 #define DRIVER_DESC "USB Parport Cable driver for Cables using the Lucent Technologies USS720 Chip"
58 /* --------------------------------------------------------------------- */
60 struct parport_uss720_private {
61 struct usb_device *usbdev;
63 struct kref ref_count;
64 __u8 reg[7]; /* USB registers */
65 struct list_head asynclist;
69 struct uss720_async_request {
70 struct parport_uss720_private *priv;
71 struct kref ref_count;
72 struct list_head asynclist;
73 struct completion compl;
75 struct usb_ctrlrequest *dr;
79 /* --------------------------------------------------------------------- */
81 static void destroy_priv(struct kref *kref)
83 struct parport_uss720_private *priv = container_of(kref, struct parport_uss720_private, ref_count);
85 dev_dbg(&priv->usbdev->dev, "destroying priv datastructure\n");
86 usb_put_dev(priv->usbdev);
91 static void destroy_async(struct kref *kref)
93 struct uss720_async_request *rq = container_of(kref, struct uss720_async_request, ref_count);
94 struct parport_uss720_private *priv = rq->priv;
98 usb_free_urb(rq->urb);
100 spin_lock_irqsave(&priv->asynclock, flags);
101 list_del_init(&rq->asynclist);
102 spin_unlock_irqrestore(&priv->asynclock, flags);
104 kref_put(&priv->ref_count, destroy_priv);
107 /* --------------------------------------------------------------------- */
109 static void async_complete(struct urb *urb)
111 struct uss720_async_request *rq;
113 struct parport_uss720_private *priv;
114 int status = urb->status;
120 dev_err(&urb->dev->dev, "async_complete: urb error %d\n",
122 } else if (rq->dr->bRequest == 3) {
123 memcpy(priv->reg, rq->reg, sizeof(priv->reg));
125 dev_dbg(&priv->usbdev->dev, "async_complete regs %7ph\n",
128 /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
129 if (rq->reg[2] & rq->reg[1] & 0x10 && pp)
130 parport_generic_irq(pp);
132 complete(&rq->compl);
133 kref_put(&rq->ref_count, destroy_async);
136 static struct uss720_async_request *submit_async_request(struct parport_uss720_private *priv,
137 __u8 request, __u8 requesttype, __u16 value, __u16 index,
140 struct usb_device *usbdev;
141 struct uss720_async_request *rq;
147 usbdev = priv->usbdev;
150 rq = kzalloc(sizeof(struct uss720_async_request), mem_flags);
153 kref_init(&rq->ref_count);
154 INIT_LIST_HEAD(&rq->asynclist);
155 init_completion(&rq->compl);
156 kref_get(&priv->ref_count);
158 rq->urb = usb_alloc_urb(0, mem_flags);
160 kref_put(&rq->ref_count, destroy_async);
163 rq->dr = kmalloc(sizeof(*rq->dr), mem_flags);
165 kref_put(&rq->ref_count, destroy_async);
168 rq->dr->bRequestType = requesttype;
169 rq->dr->bRequest = request;
170 rq->dr->wValue = cpu_to_le16(value);
171 rq->dr->wIndex = cpu_to_le16(index);
172 rq->dr->wLength = cpu_to_le16((request == 3) ? sizeof(rq->reg) : 0);
173 usb_fill_control_urb(rq->urb, usbdev, (requesttype & 0x80) ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0),
174 (unsigned char *)rq->dr,
175 (request == 3) ? rq->reg : NULL, (request == 3) ? sizeof(rq->reg) : 0, async_complete, rq);
176 /* rq->urb->transfer_flags |= URB_ASYNC_UNLINK; */
177 spin_lock_irqsave(&priv->asynclock, flags);
178 list_add_tail(&rq->asynclist, &priv->asynclist);
179 spin_unlock_irqrestore(&priv->asynclock, flags);
180 kref_get(&rq->ref_count);
181 ret = usb_submit_urb(rq->urb, mem_flags);
184 destroy_async(&rq->ref_count);
185 dev_err(&usbdev->dev, "submit_async_request submit_urb failed with %d\n", ret);
189 static unsigned int kill_all_async_requests_priv(struct parport_uss720_private *priv)
191 struct uss720_async_request *rq;
193 unsigned int ret = 0;
195 spin_lock_irqsave(&priv->asynclock, flags);
196 list_for_each_entry(rq, &priv->asynclist, asynclist) {
197 usb_unlink_urb(rq->urb);
200 spin_unlock_irqrestore(&priv->asynclock, flags);
204 /* --------------------------------------------------------------------- */
206 static int get_1284_register(struct parport *pp, unsigned char reg, unsigned char *val, gfp_t mem_flags)
208 struct parport_uss720_private *priv;
209 struct uss720_async_request *rq;
210 static const unsigned char regindex[9] = {
211 4, 0, 1, 5, 5, 0, 2, 3, 6
217 priv = pp->private_data;
218 rq = submit_async_request(priv, 3, 0xc0, ((unsigned int)reg) << 8, 0, mem_flags);
220 dev_err(&priv->usbdev->dev, "get_1284_register(%u) failed",
225 kref_put(&rq->ref_count, destroy_async);
228 if (wait_for_completion_timeout(&rq->compl, HZ)) {
229 ret = rq->urb->status;
230 *val = priv->reg[(reg >= 9) ? 0 : regindex[reg]];
232 printk(KERN_WARNING "get_1284_register: "
233 "usb error %d\n", ret);
234 kref_put(&rq->ref_count, destroy_async);
237 printk(KERN_WARNING "get_1284_register timeout\n");
238 kill_all_async_requests_priv(priv);
242 static int set_1284_register(struct parport *pp, unsigned char reg, unsigned char val, gfp_t mem_flags)
244 struct parport_uss720_private *priv;
245 struct uss720_async_request *rq;
249 priv = pp->private_data;
250 rq = submit_async_request(priv, 4, 0x40, (((unsigned int)reg) << 8) | val, 0, mem_flags);
252 dev_err(&priv->usbdev->dev, "set_1284_register(%u,%u) failed",
253 (unsigned int)reg, (unsigned int)val);
256 kref_put(&rq->ref_count, destroy_async);
260 /* --------------------------------------------------------------------- */
269 /* Safely change the mode bits in the ECR */
270 static int change_mode(struct parport *pp, int m)
272 struct parport_uss720_private *priv = pp->private_data;
276 if (get_1284_register(pp, 6, ®, GFP_KERNEL))
278 /* Bits <7:5> contain the mode. */
279 mode = (priv->reg[2] >> 5) & 0x7;
282 /* We have to go through mode 000 or 001 */
283 if (mode > ECR_PS2 && m > ECR_PS2)
284 if (change_mode(pp, ECR_PS2))
287 if (m <= ECR_PS2 && !(priv->reg[1] & 0x20)) {
288 /* This mode resets the FIFO, so we may
289 * have to wait for it to drain first. */
290 unsigned long expire = jiffies + pp->physport->cad->timeout;
292 case ECR_PPF: /* Parallel Port FIFO mode */
293 case ECR_ECP: /* ECP Parallel Port mode */
296 if (get_1284_register(pp, 6, ®, GFP_KERNEL))
298 if (priv->reg[2] & 0x01)
300 if (time_after_eq (jiffies, expire))
301 /* The FIFO is stuck. */
303 msleep_interruptible(10);
304 if (signal_pending (current))
310 if (set_1284_register(pp, 6, m << 5, GFP_KERNEL))
312 if (get_1284_register(pp, 6, ®, GFP_KERNEL))
318 * Clear TIMEOUT BIT in EPP MODE
320 static int clear_epp_timeout(struct parport *pp)
324 if (get_1284_register(pp, 1, &stat, GFP_KERNEL))
333 static int uss720_irq(int usbstatus, void *buffer, int len, void *dev_id)
335 struct parport *pp = (struct parport *)dev_id;
336 struct parport_uss720_private *priv = pp->private_data;
338 if (usbstatus != 0 || len < 4 || !buffer)
340 memcpy(priv->reg, buffer, 4);
341 /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
342 if (priv->reg[2] & priv->reg[1] & 0x10)
343 parport_generic_irq(pp);
348 static void parport_uss720_write_data(struct parport *pp, unsigned char d)
350 set_1284_register(pp, 0, d, GFP_KERNEL);
353 static unsigned char parport_uss720_read_data(struct parport *pp)
357 if (get_1284_register(pp, 0, &ret, GFP_KERNEL))
362 static void parport_uss720_write_control(struct parport *pp, unsigned char d)
364 struct parport_uss720_private *priv = pp->private_data;
366 d = (d & 0xf) | (priv->reg[1] & 0xf0);
367 if (set_1284_register(pp, 2, d, GFP_KERNEL))
372 static unsigned char parport_uss720_read_control(struct parport *pp)
374 struct parport_uss720_private *priv = pp->private_data;
375 return priv->reg[1] & 0xf; /* Use soft copy */
378 static unsigned char parport_uss720_frob_control(struct parport *pp, unsigned char mask, unsigned char val)
380 struct parport_uss720_private *priv = pp->private_data;
385 d = (priv->reg[1] & (~mask)) ^ val;
386 if (set_1284_register(pp, 2, d, GFP_ATOMIC))
392 static unsigned char parport_uss720_read_status(struct parport *pp)
396 if (get_1284_register(pp, 1, &ret, GFP_ATOMIC))
401 static void parport_uss720_disable_irq(struct parport *pp)
403 struct parport_uss720_private *priv = pp->private_data;
406 d = priv->reg[1] & ~0x10;
407 if (set_1284_register(pp, 2, d, GFP_KERNEL))
412 static void parport_uss720_enable_irq(struct parport *pp)
414 struct parport_uss720_private *priv = pp->private_data;
417 d = priv->reg[1] | 0x10;
418 if (set_1284_register(pp, 2, d, GFP_KERNEL))
423 static void parport_uss720_data_forward (struct parport *pp)
425 struct parport_uss720_private *priv = pp->private_data;
428 d = priv->reg[1] & ~0x20;
429 if (set_1284_register(pp, 2, d, GFP_KERNEL))
434 static void parport_uss720_data_reverse (struct parport *pp)
436 struct parport_uss720_private *priv = pp->private_data;
439 d = priv->reg[1] | 0x20;
440 if (set_1284_register(pp, 2, d, GFP_KERNEL))
445 static void parport_uss720_init_state(struct pardevice *dev, struct parport_state *s)
447 s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
451 static void parport_uss720_save_state(struct parport *pp, struct parport_state *s)
453 struct parport_uss720_private *priv = pp->private_data;
456 if (get_1284_register(pp, 2, NULL, GFP_ATOMIC))
459 s->u.pc.ctr = priv->reg[1];
460 s->u.pc.ecr = priv->reg[2];
463 static void parport_uss720_restore_state(struct parport *pp, struct parport_state *s)
465 struct parport_uss720_private *priv = pp->private_data;
467 set_1284_register(pp, 2, s->u.pc.ctr, GFP_ATOMIC);
468 set_1284_register(pp, 6, s->u.pc.ecr, GFP_ATOMIC);
469 get_1284_register(pp, 2, NULL, GFP_ATOMIC);
470 priv->reg[1] = s->u.pc.ctr;
471 priv->reg[2] = s->u.pc.ecr;
474 static size_t parport_uss720_epp_read_data(struct parport *pp, void *buf, size_t length, int flags)
476 struct parport_uss720_private *priv = pp->private_data;
479 if (change_mode(pp, ECR_EPP))
481 for (; got < length; got++) {
482 if (get_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
485 if (priv->reg[0] & 0x01) {
486 clear_epp_timeout(pp);
490 change_mode(pp, ECR_PS2);
494 static size_t parport_uss720_epp_write_data(struct parport *pp, const void *buf, size_t length, int flags)
497 struct parport_uss720_private *priv = pp->private_data;
500 if (change_mode(pp, ECR_EPP))
502 for (; written < length; written++) {
503 if (set_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
506 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
508 if (priv->reg[0] & 0x01) {
509 clear_epp_timeout(pp);
513 change_mode(pp, ECR_PS2);
516 struct parport_uss720_private *priv = pp->private_data;
517 struct usb_device *usbdev = priv->usbdev;
523 if (change_mode(pp, ECR_EPP))
525 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buf, length, &rlen, 20000);
527 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %zu rlen %u\n", buf, length, rlen);
528 change_mode(pp, ECR_PS2);
533 static size_t parport_uss720_epp_read_addr(struct parport *pp, void *buf, size_t length, int flags)
535 struct parport_uss720_private *priv = pp->private_data;
538 if (change_mode(pp, ECR_EPP))
540 for (; got < length; got++) {
541 if (get_1284_register(pp, 3, (char *)buf, GFP_KERNEL))
544 if (priv->reg[0] & 0x01) {
545 clear_epp_timeout(pp);
549 change_mode(pp, ECR_PS2);
553 static size_t parport_uss720_epp_write_addr(struct parport *pp, const void *buf, size_t length, int flags)
555 struct parport_uss720_private *priv = pp->private_data;
558 if (change_mode(pp, ECR_EPP))
560 for (; written < length; written++) {
561 if (set_1284_register(pp, 3, *(char *)buf, GFP_KERNEL))
564 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
566 if (priv->reg[0] & 0x01) {
567 clear_epp_timeout(pp);
571 change_mode(pp, ECR_PS2);
575 static size_t parport_uss720_ecp_write_data(struct parport *pp, const void *buffer, size_t len, int flags)
577 struct parport_uss720_private *priv = pp->private_data;
578 struct usb_device *usbdev = priv->usbdev;
584 if (change_mode(pp, ECR_ECP))
586 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
588 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %zu rlen %u\n", buffer, len, rlen);
589 change_mode(pp, ECR_PS2);
593 static size_t parport_uss720_ecp_read_data(struct parport *pp, void *buffer, size_t len, int flags)
595 struct parport_uss720_private *priv = pp->private_data;
596 struct usb_device *usbdev = priv->usbdev;
602 if (change_mode(pp, ECR_ECP))
604 i = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), buffer, len, &rlen, 20000);
606 printk(KERN_ERR "uss720: recvbulk ep 2 buf %p len %zu rlen %u\n", buffer, len, rlen);
607 change_mode(pp, ECR_PS2);
611 static size_t parport_uss720_ecp_write_addr(struct parport *pp, const void *buffer, size_t len, int flags)
615 if (change_mode(pp, ECR_ECP))
617 for (; written < len; written++) {
618 if (set_1284_register(pp, 5, *(char *)buffer, GFP_KERNEL))
622 change_mode(pp, ECR_PS2);
626 static size_t parport_uss720_write_compat(struct parport *pp, const void *buffer, size_t len, int flags)
628 struct parport_uss720_private *priv = pp->private_data;
629 struct usb_device *usbdev = priv->usbdev;
635 if (change_mode(pp, ECR_PPF))
637 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
639 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %zu rlen %u\n", buffer, len, rlen);
640 change_mode(pp, ECR_PS2);
644 /* --------------------------------------------------------------------- */
646 static struct parport_operations parport_uss720_ops =
648 .owner = THIS_MODULE,
649 .write_data = parport_uss720_write_data,
650 .read_data = parport_uss720_read_data,
652 .write_control = parport_uss720_write_control,
653 .read_control = parport_uss720_read_control,
654 .frob_control = parport_uss720_frob_control,
656 .read_status = parport_uss720_read_status,
658 .enable_irq = parport_uss720_enable_irq,
659 .disable_irq = parport_uss720_disable_irq,
661 .data_forward = parport_uss720_data_forward,
662 .data_reverse = parport_uss720_data_reverse,
664 .init_state = parport_uss720_init_state,
665 .save_state = parport_uss720_save_state,
666 .restore_state = parport_uss720_restore_state,
668 .epp_write_data = parport_uss720_epp_write_data,
669 .epp_read_data = parport_uss720_epp_read_data,
670 .epp_write_addr = parport_uss720_epp_write_addr,
671 .epp_read_addr = parport_uss720_epp_read_addr,
673 .ecp_write_data = parport_uss720_ecp_write_data,
674 .ecp_read_data = parport_uss720_ecp_read_data,
675 .ecp_write_addr = parport_uss720_ecp_write_addr,
677 .compat_write_data = parport_uss720_write_compat,
678 .nibble_read_data = parport_ieee1284_read_nibble,
679 .byte_read_data = parport_ieee1284_read_byte,
682 /* --------------------------------------------------------------------- */
684 static int uss720_probe(struct usb_interface *intf,
685 const struct usb_device_id *id)
687 struct usb_device *usbdev = usb_get_dev(interface_to_usbdev(intf));
688 struct usb_host_interface *interface;
689 struct usb_endpoint_descriptor *epd;
690 struct parport_uss720_private *priv;
695 dev_dbg(&intf->dev, "probe: vendor id 0x%x, device id 0x%x\n",
696 le16_to_cpu(usbdev->descriptor.idVendor),
697 le16_to_cpu(usbdev->descriptor.idProduct));
699 /* our known interfaces have 3 alternate settings */
700 if (intf->num_altsetting != 3) {
704 i = usb_set_interface(usbdev, intf->altsetting->desc.bInterfaceNumber, 2);
705 dev_dbg(&intf->dev, "set interface result %d\n", i);
707 interface = intf->cur_altsetting;
709 if (interface->desc.bNumEndpoints < 3) {
715 * Allocate parport interface
717 priv = kzalloc(sizeof(struct parport_uss720_private), GFP_KERNEL);
723 priv->usbdev = usbdev;
724 kref_init(&priv->ref_count);
725 spin_lock_init(&priv->asynclock);
726 INIT_LIST_HEAD(&priv->asynclist);
727 pp = parport_register_port(0, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &parport_uss720_ops);
729 printk(KERN_WARNING "uss720: could not register parport\n");
734 pp->private_data = priv;
735 pp->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_TRISTATE | PARPORT_MODE_EPP | PARPORT_MODE_ECP | PARPORT_MODE_COMPAT;
737 /* set the USS720 control register to manual mode, no ECP compression, enable all ints */
738 set_1284_register(pp, 7, 0x00, GFP_KERNEL);
739 set_1284_register(pp, 6, 0x30, GFP_KERNEL); /* PS/2 mode */
740 set_1284_register(pp, 2, 0x0c, GFP_KERNEL);
742 get_1284_register(pp, 0, ®, GFP_KERNEL);
743 dev_dbg(&intf->dev, "reg: %7ph\n", priv->reg);
745 i = usb_find_last_int_in_endpoint(interface, &epd);
747 dev_dbg(&intf->dev, "epaddr %d interval %d\n",
748 epd->bEndpointAddress, epd->bInterval);
750 parport_announce_port(pp);
752 usb_set_intfdata(intf, pp);
756 kill_all_async_requests_priv(priv);
757 kref_put(&priv->ref_count, destroy_priv);
761 static void uss720_disconnect(struct usb_interface *intf)
763 struct parport *pp = usb_get_intfdata(intf);
764 struct parport_uss720_private *priv;
765 struct usb_device *usbdev;
767 dev_dbg(&intf->dev, "disconnect\n");
768 usb_set_intfdata(intf, NULL);
770 priv = pp->private_data;
771 usbdev = priv->usbdev;
773 dev_dbg(&intf->dev, "parport_remove_port\n");
774 parport_remove_port(pp);
775 parport_put_port(pp);
776 kill_all_async_requests_priv(priv);
777 kref_put(&priv->ref_count, destroy_priv);
779 dev_dbg(&intf->dev, "disconnect done\n");
782 /* table of cables that work through this driver */
783 static const struct usb_device_id uss720_table[] = {
784 { USB_DEVICE(0x047e, 0x1001) },
785 { USB_DEVICE(0x0557, 0x2001) },
786 { USB_DEVICE(0x0729, 0x1284) },
787 { USB_DEVICE(0x1293, 0x0002) },
788 { USB_DEVICE(0x050d, 0x0002) },
789 { } /* Terminating entry */
792 MODULE_DEVICE_TABLE (usb, uss720_table);
795 static struct usb_driver uss720_driver = {
797 .probe = uss720_probe,
798 .disconnect = uss720_disconnect,
799 .id_table = uss720_table,
802 /* --------------------------------------------------------------------- */
804 MODULE_AUTHOR(DRIVER_AUTHOR);
805 MODULE_DESCRIPTION(DRIVER_DESC);
806 MODULE_LICENSE("GPL");
808 static int __init uss720_init(void)
811 retval = usb_register(&uss720_driver);
815 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
816 printk(KERN_INFO KBUILD_MODNAME ": NOTE: this is a special purpose "
817 "driver to allow nonstandard\n");
818 printk(KERN_INFO KBUILD_MODNAME ": protocols (eg. bitbang) over "
819 "USS720 usb to parallel cables\n");
820 printk(KERN_INFO KBUILD_MODNAME ": If you just want to connect to a "
821 "printer, use usblp instead\n");
826 static void __exit uss720_cleanup(void)
828 usb_deregister(&uss720_driver);
831 module_init(uss720_init);
832 module_exit(uss720_cleanup);
834 /* --------------------------------------------------------------------- */