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);
90 static void destroy_async(struct kref *kref)
92 struct uss720_async_request *rq = container_of(kref, struct uss720_async_request, ref_count);
93 struct parport_uss720_private *priv = rq->priv;
97 usb_free_urb(rq->urb);
99 spin_lock_irqsave(&priv->asynclock, flags);
100 list_del_init(&rq->asynclist);
101 spin_unlock_irqrestore(&priv->asynclock, flags);
103 kref_put(&priv->ref_count, destroy_priv);
106 /* --------------------------------------------------------------------- */
108 static void async_complete(struct urb *urb)
110 struct uss720_async_request *rq;
112 struct parport_uss720_private *priv;
113 int status = urb->status;
119 dev_err(&urb->dev->dev, "async_complete: urb error %d\n",
121 } else if (rq->dr->bRequest == 3) {
122 memcpy(priv->reg, rq->reg, sizeof(priv->reg));
124 dev_dbg(&priv->usbdev->dev, "async_complete regs %7ph\n",
127 /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
128 if (rq->reg[2] & rq->reg[1] & 0x10 && pp)
129 parport_generic_irq(pp);
131 complete(&rq->compl);
132 kref_put(&rq->ref_count, destroy_async);
135 static struct uss720_async_request *submit_async_request(struct parport_uss720_private *priv,
136 __u8 request, __u8 requesttype, __u16 value, __u16 index,
139 struct usb_device *usbdev;
140 struct uss720_async_request *rq;
146 usbdev = priv->usbdev;
149 rq = kzalloc(sizeof(struct uss720_async_request), mem_flags);
152 kref_init(&rq->ref_count);
153 INIT_LIST_HEAD(&rq->asynclist);
154 init_completion(&rq->compl);
155 kref_get(&priv->ref_count);
157 rq->urb = usb_alloc_urb(0, mem_flags);
159 kref_put(&rq->ref_count, destroy_async);
162 rq->dr = kmalloc(sizeof(*rq->dr), mem_flags);
164 kref_put(&rq->ref_count, destroy_async);
167 rq->dr->bRequestType = requesttype;
168 rq->dr->bRequest = request;
169 rq->dr->wValue = cpu_to_le16(value);
170 rq->dr->wIndex = cpu_to_le16(index);
171 rq->dr->wLength = cpu_to_le16((request == 3) ? sizeof(rq->reg) : 0);
172 usb_fill_control_urb(rq->urb, usbdev, (requesttype & 0x80) ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0),
173 (unsigned char *)rq->dr,
174 (request == 3) ? rq->reg : NULL, (request == 3) ? sizeof(rq->reg) : 0, async_complete, rq);
175 /* rq->urb->transfer_flags |= URB_ASYNC_UNLINK; */
176 spin_lock_irqsave(&priv->asynclock, flags);
177 list_add_tail(&rq->asynclist, &priv->asynclist);
178 spin_unlock_irqrestore(&priv->asynclock, flags);
179 kref_get(&rq->ref_count);
180 ret = usb_submit_urb(rq->urb, mem_flags);
183 destroy_async(&rq->ref_count);
184 dev_err(&usbdev->dev, "submit_async_request submit_urb failed with %d\n", ret);
188 static unsigned int kill_all_async_requests_priv(struct parport_uss720_private *priv)
190 struct uss720_async_request *rq;
192 unsigned int ret = 0;
194 spin_lock_irqsave(&priv->asynclock, flags);
195 list_for_each_entry(rq, &priv->asynclist, asynclist) {
196 usb_unlink_urb(rq->urb);
199 spin_unlock_irqrestore(&priv->asynclock, flags);
203 /* --------------------------------------------------------------------- */
205 static int get_1284_register(struct parport *pp, unsigned char reg, unsigned char *val, gfp_t mem_flags)
207 struct parport_uss720_private *priv;
208 struct uss720_async_request *rq;
209 static const unsigned char regindex[9] = {
210 4, 0, 1, 5, 5, 0, 2, 3, 6
216 priv = pp->private_data;
217 rq = submit_async_request(priv, 3, 0xc0, ((unsigned int)reg) << 8, 0, mem_flags);
219 dev_err(&priv->usbdev->dev, "get_1284_register(%u) failed",
224 kref_put(&rq->ref_count, destroy_async);
227 if (wait_for_completion_timeout(&rq->compl, HZ)) {
228 ret = rq->urb->status;
229 *val = priv->reg[(reg >= 9) ? 0 : regindex[reg]];
231 printk(KERN_WARNING "get_1284_register: "
232 "usb error %d\n", ret);
233 kref_put(&rq->ref_count, destroy_async);
236 printk(KERN_WARNING "get_1284_register timeout\n");
237 kill_all_async_requests_priv(priv);
241 static int set_1284_register(struct parport *pp, unsigned char reg, unsigned char val, gfp_t mem_flags)
243 struct parport_uss720_private *priv;
244 struct uss720_async_request *rq;
248 priv = pp->private_data;
249 rq = submit_async_request(priv, 4, 0x40, (((unsigned int)reg) << 8) | val, 0, mem_flags);
251 dev_err(&priv->usbdev->dev, "set_1284_register(%u,%u) failed",
252 (unsigned int)reg, (unsigned int)val);
255 kref_put(&rq->ref_count, destroy_async);
259 /* --------------------------------------------------------------------- */
268 /* Safely change the mode bits in the ECR */
269 static int change_mode(struct parport *pp, int m)
271 struct parport_uss720_private *priv = pp->private_data;
275 if (get_1284_register(pp, 6, ®, GFP_KERNEL))
277 /* Bits <7:5> contain the mode. */
278 mode = (priv->reg[2] >> 5) & 0x7;
281 /* We have to go through mode 000 or 001 */
282 if (mode > ECR_PS2 && m > ECR_PS2)
283 if (change_mode(pp, ECR_PS2))
286 if (m <= ECR_PS2 && !(priv->reg[1] & 0x20)) {
287 /* This mode resets the FIFO, so we may
288 * have to wait for it to drain first. */
289 unsigned long expire = jiffies + pp->physport->cad->timeout;
291 case ECR_PPF: /* Parallel Port FIFO mode */
292 case ECR_ECP: /* ECP Parallel Port mode */
295 if (get_1284_register(pp, 6, ®, GFP_KERNEL))
297 if (priv->reg[2] & 0x01)
299 if (time_after_eq (jiffies, expire))
300 /* The FIFO is stuck. */
302 msleep_interruptible(10);
303 if (signal_pending (current))
309 if (set_1284_register(pp, 6, m << 5, GFP_KERNEL))
311 if (get_1284_register(pp, 6, ®, GFP_KERNEL))
317 * Clear TIMEOUT BIT in EPP MODE
319 static int clear_epp_timeout(struct parport *pp)
323 if (get_1284_register(pp, 1, &stat, GFP_KERNEL))
332 static int uss720_irq(int usbstatus, void *buffer, int len, void *dev_id)
334 struct parport *pp = (struct parport *)dev_id;
335 struct parport_uss720_private *priv = pp->private_data;
337 if (usbstatus != 0 || len < 4 || !buffer)
339 memcpy(priv->reg, buffer, 4);
340 /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
341 if (priv->reg[2] & priv->reg[1] & 0x10)
342 parport_generic_irq(pp);
347 static void parport_uss720_write_data(struct parport *pp, unsigned char d)
349 set_1284_register(pp, 0, d, GFP_KERNEL);
352 static unsigned char parport_uss720_read_data(struct parport *pp)
356 if (get_1284_register(pp, 0, &ret, GFP_KERNEL))
361 static void parport_uss720_write_control(struct parport *pp, unsigned char d)
363 struct parport_uss720_private *priv = pp->private_data;
365 d = (d & 0xf) | (priv->reg[1] & 0xf0);
366 if (set_1284_register(pp, 2, d, GFP_KERNEL))
371 static unsigned char parport_uss720_read_control(struct parport *pp)
373 struct parport_uss720_private *priv = pp->private_data;
374 return priv->reg[1] & 0xf; /* Use soft copy */
377 static unsigned char parport_uss720_frob_control(struct parport *pp, unsigned char mask, unsigned char val)
379 struct parport_uss720_private *priv = pp->private_data;
384 d = (priv->reg[1] & (~mask)) ^ val;
385 if (set_1284_register(pp, 2, d, GFP_ATOMIC))
391 static unsigned char parport_uss720_read_status(struct parport *pp)
395 if (get_1284_register(pp, 1, &ret, GFP_ATOMIC))
400 static void parport_uss720_disable_irq(struct parport *pp)
402 struct parport_uss720_private *priv = pp->private_data;
405 d = priv->reg[1] & ~0x10;
406 if (set_1284_register(pp, 2, d, GFP_KERNEL))
411 static void parport_uss720_enable_irq(struct parport *pp)
413 struct parport_uss720_private *priv = pp->private_data;
416 d = priv->reg[1] | 0x10;
417 if (set_1284_register(pp, 2, d, GFP_KERNEL))
422 static void parport_uss720_data_forward (struct parport *pp)
424 struct parport_uss720_private *priv = pp->private_data;
427 d = priv->reg[1] & ~0x20;
428 if (set_1284_register(pp, 2, d, GFP_KERNEL))
433 static void parport_uss720_data_reverse (struct parport *pp)
435 struct parport_uss720_private *priv = pp->private_data;
438 d = priv->reg[1] | 0x20;
439 if (set_1284_register(pp, 2, d, GFP_KERNEL))
444 static void parport_uss720_init_state(struct pardevice *dev, struct parport_state *s)
446 s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
450 static void parport_uss720_save_state(struct parport *pp, struct parport_state *s)
452 struct parport_uss720_private *priv = pp->private_data;
455 if (get_1284_register(pp, 2, NULL, GFP_ATOMIC))
458 s->u.pc.ctr = priv->reg[1];
459 s->u.pc.ecr = priv->reg[2];
462 static void parport_uss720_restore_state(struct parport *pp, struct parport_state *s)
464 struct parport_uss720_private *priv = pp->private_data;
466 set_1284_register(pp, 2, s->u.pc.ctr, GFP_ATOMIC);
467 set_1284_register(pp, 6, s->u.pc.ecr, GFP_ATOMIC);
468 get_1284_register(pp, 2, NULL, GFP_ATOMIC);
469 priv->reg[1] = s->u.pc.ctr;
470 priv->reg[2] = s->u.pc.ecr;
473 static size_t parport_uss720_epp_read_data(struct parport *pp, void *buf, size_t length, int flags)
475 struct parport_uss720_private *priv = pp->private_data;
478 if (change_mode(pp, ECR_EPP))
480 for (; got < length; got++) {
481 if (get_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
484 if (priv->reg[0] & 0x01) {
485 clear_epp_timeout(pp);
489 change_mode(pp, ECR_PS2);
493 static size_t parport_uss720_epp_write_data(struct parport *pp, const void *buf, size_t length, int flags)
496 struct parport_uss720_private *priv = pp->private_data;
499 if (change_mode(pp, ECR_EPP))
501 for (; written < length; written++) {
502 if (set_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
505 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
507 if (priv->reg[0] & 0x01) {
508 clear_epp_timeout(pp);
512 change_mode(pp, ECR_PS2);
515 struct parport_uss720_private *priv = pp->private_data;
516 struct usb_device *usbdev = priv->usbdev;
522 if (change_mode(pp, ECR_EPP))
524 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buf, length, &rlen, 20000);
526 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %zu rlen %u\n", buf, length, rlen);
527 change_mode(pp, ECR_PS2);
532 static size_t parport_uss720_epp_read_addr(struct parport *pp, void *buf, size_t length, int flags)
534 struct parport_uss720_private *priv = pp->private_data;
537 if (change_mode(pp, ECR_EPP))
539 for (; got < length; got++) {
540 if (get_1284_register(pp, 3, (char *)buf, GFP_KERNEL))
543 if (priv->reg[0] & 0x01) {
544 clear_epp_timeout(pp);
548 change_mode(pp, ECR_PS2);
552 static size_t parport_uss720_epp_write_addr(struct parport *pp, const void *buf, size_t length, int flags)
554 struct parport_uss720_private *priv = pp->private_data;
557 if (change_mode(pp, ECR_EPP))
559 for (; written < length; written++) {
560 if (set_1284_register(pp, 3, *(char *)buf, GFP_KERNEL))
563 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
565 if (priv->reg[0] & 0x01) {
566 clear_epp_timeout(pp);
570 change_mode(pp, ECR_PS2);
574 static size_t parport_uss720_ecp_write_data(struct parport *pp, const void *buffer, size_t len, int flags)
576 struct parport_uss720_private *priv = pp->private_data;
577 struct usb_device *usbdev = priv->usbdev;
583 if (change_mode(pp, ECR_ECP))
585 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
587 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %zu rlen %u\n", buffer, len, rlen);
588 change_mode(pp, ECR_PS2);
592 static size_t parport_uss720_ecp_read_data(struct parport *pp, void *buffer, size_t len, int flags)
594 struct parport_uss720_private *priv = pp->private_data;
595 struct usb_device *usbdev = priv->usbdev;
601 if (change_mode(pp, ECR_ECP))
603 i = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), buffer, len, &rlen, 20000);
605 printk(KERN_ERR "uss720: recvbulk ep 2 buf %p len %zu rlen %u\n", buffer, len, rlen);
606 change_mode(pp, ECR_PS2);
610 static size_t parport_uss720_ecp_write_addr(struct parport *pp, const void *buffer, size_t len, int flags)
614 if (change_mode(pp, ECR_ECP))
616 for (; written < len; written++) {
617 if (set_1284_register(pp, 5, *(char *)buffer, GFP_KERNEL))
621 change_mode(pp, ECR_PS2);
625 static size_t parport_uss720_write_compat(struct parport *pp, const void *buffer, size_t len, int flags)
627 struct parport_uss720_private *priv = pp->private_data;
628 struct usb_device *usbdev = priv->usbdev;
634 if (change_mode(pp, ECR_PPF))
636 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
638 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %zu rlen %u\n", buffer, len, rlen);
639 change_mode(pp, ECR_PS2);
643 /* --------------------------------------------------------------------- */
645 static struct parport_operations parport_uss720_ops =
647 .owner = THIS_MODULE,
648 .write_data = parport_uss720_write_data,
649 .read_data = parport_uss720_read_data,
651 .write_control = parport_uss720_write_control,
652 .read_control = parport_uss720_read_control,
653 .frob_control = parport_uss720_frob_control,
655 .read_status = parport_uss720_read_status,
657 .enable_irq = parport_uss720_enable_irq,
658 .disable_irq = parport_uss720_disable_irq,
660 .data_forward = parport_uss720_data_forward,
661 .data_reverse = parport_uss720_data_reverse,
663 .init_state = parport_uss720_init_state,
664 .save_state = parport_uss720_save_state,
665 .restore_state = parport_uss720_restore_state,
667 .epp_write_data = parport_uss720_epp_write_data,
668 .epp_read_data = parport_uss720_epp_read_data,
669 .epp_write_addr = parport_uss720_epp_write_addr,
670 .epp_read_addr = parport_uss720_epp_read_addr,
672 .ecp_write_data = parport_uss720_ecp_write_data,
673 .ecp_read_data = parport_uss720_ecp_read_data,
674 .ecp_write_addr = parport_uss720_ecp_write_addr,
676 .compat_write_data = parport_uss720_write_compat,
677 .nibble_read_data = parport_ieee1284_read_nibble,
678 .byte_read_data = parport_ieee1284_read_byte,
681 /* --------------------------------------------------------------------- */
683 static int uss720_probe(struct usb_interface *intf,
684 const struct usb_device_id *id)
686 struct usb_device *usbdev = usb_get_dev(interface_to_usbdev(intf));
687 struct usb_host_interface *interface;
688 struct usb_endpoint_descriptor *epd;
689 struct parport_uss720_private *priv;
694 dev_dbg(&intf->dev, "probe: vendor id 0x%x, device id 0x%x\n",
695 le16_to_cpu(usbdev->descriptor.idVendor),
696 le16_to_cpu(usbdev->descriptor.idProduct));
698 /* our known interfaces have 3 alternate settings */
699 if (intf->num_altsetting != 3) {
703 i = usb_set_interface(usbdev, intf->altsetting->desc.bInterfaceNumber, 2);
704 dev_dbg(&intf->dev, "set interface result %d\n", i);
706 interface = intf->cur_altsetting;
708 if (interface->desc.bNumEndpoints < 3) {
714 * Allocate parport interface
716 priv = kzalloc(sizeof(struct parport_uss720_private), GFP_KERNEL);
722 priv->usbdev = usbdev;
723 kref_init(&priv->ref_count);
724 spin_lock_init(&priv->asynclock);
725 INIT_LIST_HEAD(&priv->asynclist);
726 pp = parport_register_port(0, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &parport_uss720_ops);
728 printk(KERN_WARNING "uss720: could not register parport\n");
733 pp->private_data = priv;
734 pp->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_TRISTATE | PARPORT_MODE_EPP | PARPORT_MODE_ECP | PARPORT_MODE_COMPAT;
736 /* set the USS720 control register to manual mode, no ECP compression, enable all ints */
737 set_1284_register(pp, 7, 0x00, GFP_KERNEL);
738 set_1284_register(pp, 6, 0x30, GFP_KERNEL); /* PS/2 mode */
739 set_1284_register(pp, 2, 0x0c, GFP_KERNEL);
741 get_1284_register(pp, 0, ®, GFP_KERNEL);
742 dev_dbg(&intf->dev, "reg: %7ph\n", priv->reg);
744 i = usb_find_last_int_in_endpoint(interface, &epd);
746 dev_dbg(&intf->dev, "epaddr %d interval %d\n",
747 epd->bEndpointAddress, epd->bInterval);
749 parport_announce_port(pp);
751 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;
774 dev_dbg(&intf->dev, "parport_remove_port\n");
775 parport_remove_port(pp);
776 parport_put_port(pp);
777 kill_all_async_requests_priv(priv);
778 kref_put(&priv->ref_count, destroy_priv);
780 dev_dbg(&intf->dev, "disconnect done\n");
783 /* table of cables that work through this driver */
784 static const struct usb_device_id uss720_table[] = {
785 { USB_DEVICE(0x047e, 0x1001) },
786 { USB_DEVICE(0x0557, 0x2001) },
787 { USB_DEVICE(0x0729, 0x1284) },
788 { USB_DEVICE(0x1293, 0x0002) },
789 { USB_DEVICE(0x050d, 0x0002) },
790 { } /* Terminating entry */
793 MODULE_DEVICE_TABLE (usb, uss720_table);
796 static struct usb_driver uss720_driver = {
798 .probe = uss720_probe,
799 .disconnect = uss720_disconnect,
800 .id_table = uss720_table,
803 /* --------------------------------------------------------------------- */
805 MODULE_AUTHOR(DRIVER_AUTHOR);
806 MODULE_DESCRIPTION(DRIVER_DESC);
807 MODULE_LICENSE("GPL");
809 static int __init uss720_init(void)
812 retval = usb_register(&uss720_driver);
816 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
817 printk(KERN_INFO KBUILD_MODNAME ": NOTE: this is a special purpose "
818 "driver to allow nonstandard\n");
819 printk(KERN_INFO KBUILD_MODNAME ": protocols (eg. bitbang) over "
820 "USS720 usb to parallel cables\n");
821 printk(KERN_INFO KBUILD_MODNAME ": If you just want to connect to a "
822 "printer, use usblp instead\n");
827 static void __exit uss720_cleanup(void)
829 usb_deregister(&uss720_driver);
832 module_init(uss720_init);
833 module_exit(uss720_cleanup);
835 /* --------------------------------------------------------------------- */