1 /*****************************************************************************/
4 * uss720.c -- USS720 USB Parport Cable.
6 * Copyright (C) 1999, 2005
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>
56 #define DRIVER_VERSION "v0.6"
57 #define DRIVER_AUTHOR "Thomas M. Sailer, t.sailer@alumni.ethz.ch"
58 #define DRIVER_DESC "USB Parport Cable driver for Cables using the Lucent Technologies USS720 Chip"
60 /* --------------------------------------------------------------------- */
62 struct parport_uss720_private {
63 struct usb_device *usbdev;
65 struct kref ref_count;
66 __u8 reg[7]; /* USB registers */
67 struct list_head asynclist;
71 struct uss720_async_request {
72 struct parport_uss720_private *priv;
73 struct kref ref_count;
74 struct list_head asynclist;
75 struct completion compl;
77 struct usb_ctrlrequest dr;
81 /* --------------------------------------------------------------------- */
83 static void destroy_priv(struct kref *kref)
85 struct parport_uss720_private *priv = container_of(kref, struct parport_uss720_private, ref_count);
87 usb_put_dev(priv->usbdev);
89 dbg("destroying priv datastructure");
92 static void destroy_async(struct kref *kref)
94 struct uss720_async_request *rq = container_of(kref, struct uss720_async_request, ref_count);
95 struct parport_uss720_private *priv = rq->priv;
99 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 err("async_complete: urb error %d", status);
121 } else if (rq->dr.bRequest == 3) {
122 memcpy(priv->reg, rq->reg, sizeof(priv->reg));
124 dbg("async_complete regs %02x %02x %02x %02x %02x %02x %02x",
125 (unsigned int)priv->reg[0], (unsigned int)priv->reg[1], (unsigned int)priv->reg[2],
126 (unsigned int)priv->reg[3], (unsigned int)priv->reg[4], (unsigned int)priv->reg[5],
127 (unsigned int)priv->reg[6]);
129 /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
130 if (rq->reg[2] & rq->reg[1] & 0x10 && pp)
131 parport_generic_irq(0, pp);
133 complete(&rq->compl);
134 kref_put(&rq->ref_count, destroy_async);
137 static struct uss720_async_request *submit_async_request(struct parport_uss720_private *priv,
138 __u8 request, __u8 requesttype, __u16 value, __u16 index,
141 struct usb_device *usbdev;
142 struct uss720_async_request *rq;
148 usbdev = priv->usbdev;
151 rq = kmalloc(sizeof(struct uss720_async_request), mem_flags);
153 err("submit_async_request out of memory");
156 kref_init(&rq->ref_count);
157 INIT_LIST_HEAD(&rq->asynclist);
158 init_completion(&rq->compl);
159 kref_get(&priv->ref_count);
161 rq->urb = usb_alloc_urb(0, mem_flags);
163 kref_put(&rq->ref_count, destroy_async);
164 err("submit_async_request out of memory");
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 ret = usb_submit_urb(rq->urb, mem_flags);
181 kref_get(&rq->ref_count);
184 kref_put(&rq->ref_count, destroy_async);
185 err("submit_async_request submit_urb failed with %d", 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 err("get_1284_register(%u) failed", (unsigned int)reg);
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 warn("get_1284_register: usb error %d", ret);
232 kref_put(&rq->ref_count, destroy_async);
235 warn("get_1284_register timeout");
236 kill_all_async_requests_priv(priv);
240 static int set_1284_register(struct parport *pp, unsigned char reg, unsigned char val, gfp_t mem_flags)
242 struct parport_uss720_private *priv;
243 struct uss720_async_request *rq;
247 priv = pp->private_data;
248 rq = submit_async_request(priv, 4, 0x40, (((unsigned int)reg) << 8) | val, 0, mem_flags);
250 err("set_1284_register(%u,%u) failed", (unsigned int)reg, (unsigned int)val);
253 kref_put(&rq->ref_count, destroy_async);
257 /* --------------------------------------------------------------------- */
266 /* Safely change the mode bits in the ECR */
267 static int change_mode(struct parport *pp, int m)
269 struct parport_uss720_private *priv = pp->private_data;
273 if (get_1284_register(pp, 6, ®, GFP_KERNEL))
275 /* Bits <7:5> contain the mode. */
276 mode = (priv->reg[2] >> 5) & 0x7;
279 /* We have to go through mode 000 or 001 */
280 if (mode > ECR_PS2 && m > ECR_PS2)
281 if (change_mode(pp, ECR_PS2))
284 if (m <= ECR_PS2 && !(priv->reg[1] & 0x20)) {
285 /* This mode resets the FIFO, so we may
286 * have to wait for it to drain first. */
287 unsigned long expire = jiffies + pp->physport->cad->timeout;
289 case ECR_PPF: /* Parallel Port FIFO mode */
290 case ECR_ECP: /* ECP Parallel Port mode */
293 if (get_1284_register(pp, 6, ®, GFP_KERNEL))
295 if (priv->reg[2] & 0x01)
297 if (time_after_eq (jiffies, expire))
298 /* The FIFO is stuck. */
300 msleep_interruptible(10);
301 if (signal_pending (current))
307 if (set_1284_register(pp, 6, m << 5, GFP_KERNEL))
309 if (get_1284_register(pp, 6, ®, GFP_KERNEL))
315 * Clear TIMEOUT BIT in EPP MODE
317 static int clear_epp_timeout(struct parport *pp)
321 if (get_1284_register(pp, 1, &stat, GFP_KERNEL))
330 static int uss720_irq(int usbstatus, void *buffer, int len, void *dev_id)
332 struct parport *pp = (struct parport *)dev_id;
333 struct parport_uss720_private *priv = pp->private_data;
335 if (usbstatus != 0 || len < 4 || !buffer)
337 memcpy(priv->reg, buffer, 4);
338 /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
339 if (priv->reg[2] & priv->reg[1] & 0x10)
340 parport_generic_irq(0, pp);
345 static void parport_uss720_write_data(struct parport *pp, unsigned char d)
347 set_1284_register(pp, 0, d, GFP_KERNEL);
350 static unsigned char parport_uss720_read_data(struct parport *pp)
354 if (get_1284_register(pp, 0, &ret, GFP_KERNEL))
359 static void parport_uss720_write_control(struct parport *pp, unsigned char d)
361 struct parport_uss720_private *priv = pp->private_data;
363 d = (d & 0xf) | (priv->reg[1] & 0xf0);
364 if (set_1284_register(pp, 2, d, GFP_KERNEL))
369 static unsigned char parport_uss720_read_control(struct parport *pp)
371 struct parport_uss720_private *priv = pp->private_data;
372 return priv->reg[1] & 0xf; /* Use soft copy */
375 static unsigned char parport_uss720_frob_control(struct parport *pp, unsigned char mask, unsigned char val)
377 struct parport_uss720_private *priv = pp->private_data;
382 d = (priv->reg[1] & (~mask)) ^ val;
383 if (set_1284_register(pp, 2, d, GFP_KERNEL))
389 static unsigned char parport_uss720_read_status(struct parport *pp)
393 if (get_1284_register(pp, 1, &ret, GFP_KERNEL))
398 static void parport_uss720_disable_irq(struct parport *pp)
400 struct parport_uss720_private *priv = pp->private_data;
403 d = priv->reg[1] & ~0x10;
404 if (set_1284_register(pp, 2, d, GFP_KERNEL))
409 static void parport_uss720_enable_irq(struct parport *pp)
411 struct parport_uss720_private *priv = pp->private_data;
414 d = priv->reg[1] | 0x10;
415 if (set_1284_register(pp, 2, d, GFP_KERNEL))
420 static void parport_uss720_data_forward (struct parport *pp)
422 struct parport_uss720_private *priv = pp->private_data;
425 d = priv->reg[1] & ~0x20;
426 if (set_1284_register(pp, 2, d, GFP_KERNEL))
431 static void parport_uss720_data_reverse (struct parport *pp)
433 struct parport_uss720_private *priv = pp->private_data;
436 d = priv->reg[1] | 0x20;
437 if (set_1284_register(pp, 2, d, GFP_KERNEL))
442 static void parport_uss720_init_state(struct pardevice *dev, struct parport_state *s)
444 s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
448 static void parport_uss720_save_state(struct parport *pp, struct parport_state *s)
450 struct parport_uss720_private *priv = pp->private_data;
453 if (get_1284_register(pp, 2, NULL, GFP_ATOMIC))
456 s->u.pc.ctr = priv->reg[1];
457 s->u.pc.ecr = priv->reg[2];
460 static void parport_uss720_restore_state(struct parport *pp, struct parport_state *s)
462 struct parport_uss720_private *priv = pp->private_data;
464 set_1284_register(pp, 2, s->u.pc.ctr, GFP_ATOMIC);
465 set_1284_register(pp, 6, s->u.pc.ecr, GFP_ATOMIC);
466 get_1284_register(pp, 2, NULL, GFP_ATOMIC);
467 priv->reg[1] = s->u.pc.ctr;
468 priv->reg[2] = s->u.pc.ecr;
471 static size_t parport_uss720_epp_read_data(struct parport *pp, void *buf, size_t length, int flags)
473 struct parport_uss720_private *priv = pp->private_data;
476 if (change_mode(pp, ECR_EPP))
478 for (; got < length; got++) {
479 if (get_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
482 if (priv->reg[0] & 0x01) {
483 clear_epp_timeout(pp);
487 change_mode(pp, ECR_PS2);
491 static size_t parport_uss720_epp_write_data(struct parport *pp, const void *buf, size_t length, int flags)
494 struct parport_uss720_private *priv = pp->private_data;
497 if (change_mode(pp, ECR_EPP))
499 for (; written < length; written++) {
500 if (set_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
503 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
505 if (priv->reg[0] & 0x01) {
506 clear_epp_timeout(pp);
510 change_mode(pp, ECR_PS2);
513 struct parport_uss720_private *priv = pp->private_data;
514 struct usb_device *usbdev = priv->usbdev;
520 if (change_mode(pp, ECR_EPP))
522 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buf, length, &rlen, 20000);
524 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buf, length, rlen);
525 change_mode(pp, ECR_PS2);
530 static size_t parport_uss720_epp_read_addr(struct parport *pp, void *buf, size_t length, int flags)
532 struct parport_uss720_private *priv = pp->private_data;
535 if (change_mode(pp, ECR_EPP))
537 for (; got < length; got++) {
538 if (get_1284_register(pp, 3, (char *)buf, GFP_KERNEL))
541 if (priv->reg[0] & 0x01) {
542 clear_epp_timeout(pp);
546 change_mode(pp, ECR_PS2);
550 static size_t parport_uss720_epp_write_addr(struct parport *pp, const void *buf, size_t length, int flags)
552 struct parport_uss720_private *priv = pp->private_data;
555 if (change_mode(pp, ECR_EPP))
557 for (; written < length; written++) {
558 if (set_1284_register(pp, 3, *(char *)buf, GFP_KERNEL))
561 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
563 if (priv->reg[0] & 0x01) {
564 clear_epp_timeout(pp);
568 change_mode(pp, ECR_PS2);
572 static size_t parport_uss720_ecp_write_data(struct parport *pp, const void *buffer, size_t len, int flags)
574 struct parport_uss720_private *priv = pp->private_data;
575 struct usb_device *usbdev = priv->usbdev;
581 if (change_mode(pp, ECR_ECP))
583 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
585 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
586 change_mode(pp, ECR_PS2);
590 static size_t parport_uss720_ecp_read_data(struct parport *pp, void *buffer, size_t len, int flags)
592 struct parport_uss720_private *priv = pp->private_data;
593 struct usb_device *usbdev = priv->usbdev;
599 if (change_mode(pp, ECR_ECP))
601 i = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), buffer, len, &rlen, 20000);
603 printk(KERN_ERR "uss720: recvbulk ep 2 buf %p len %Zu rlen %u\n", buffer, len, rlen);
604 change_mode(pp, ECR_PS2);
608 static size_t parport_uss720_ecp_write_addr(struct parport *pp, const void *buffer, size_t len, int flags)
612 if (change_mode(pp, ECR_ECP))
614 for (; written < len; written++) {
615 if (set_1284_register(pp, 5, *(char *)buffer, GFP_KERNEL))
619 change_mode(pp, ECR_PS2);
623 static size_t parport_uss720_write_compat(struct parport *pp, const void *buffer, size_t len, int flags)
625 struct parport_uss720_private *priv = pp->private_data;
626 struct usb_device *usbdev = priv->usbdev;
632 if (change_mode(pp, ECR_PPF))
634 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
636 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
637 change_mode(pp, ECR_PS2);
641 /* --------------------------------------------------------------------- */
643 static struct parport_operations parport_uss720_ops =
645 .owner = THIS_MODULE,
646 .write_data = parport_uss720_write_data,
647 .read_data = parport_uss720_read_data,
649 .write_control = parport_uss720_write_control,
650 .read_control = parport_uss720_read_control,
651 .frob_control = parport_uss720_frob_control,
653 .read_status = parport_uss720_read_status,
655 .enable_irq = parport_uss720_enable_irq,
656 .disable_irq = parport_uss720_disable_irq,
658 .data_forward = parport_uss720_data_forward,
659 .data_reverse = parport_uss720_data_reverse,
661 .init_state = parport_uss720_init_state,
662 .save_state = parport_uss720_save_state,
663 .restore_state = parport_uss720_restore_state,
665 .epp_write_data = parport_uss720_epp_write_data,
666 .epp_read_data = parport_uss720_epp_read_data,
667 .epp_write_addr = parport_uss720_epp_write_addr,
668 .epp_read_addr = parport_uss720_epp_read_addr,
670 .ecp_write_data = parport_uss720_ecp_write_data,
671 .ecp_read_data = parport_uss720_ecp_read_data,
672 .ecp_write_addr = parport_uss720_ecp_write_addr,
674 .compat_write_data = parport_uss720_write_compat,
675 .nibble_read_data = parport_ieee1284_read_nibble,
676 .byte_read_data = parport_ieee1284_read_byte,
679 /* --------------------------------------------------------------------- */
681 static int uss720_probe(struct usb_interface *intf,
682 const struct usb_device_id *id)
684 struct usb_device *usbdev = usb_get_dev(interface_to_usbdev(intf));
685 struct usb_host_interface *interface;
686 struct usb_host_endpoint *endpoint;
687 struct parport_uss720_private *priv;
692 dbg("probe: vendor id 0x%x, device id 0x%x\n",
693 le16_to_cpu(usbdev->descriptor.idVendor),
694 le16_to_cpu(usbdev->descriptor.idProduct));
696 /* our known interfaces have 3 alternate settings */
697 if (intf->num_altsetting != 3) {
701 i = usb_set_interface(usbdev, intf->altsetting->desc.bInterfaceNumber, 2);
702 dbg("set inteface result %d", i);
704 interface = intf->cur_altsetting;
707 * Allocate parport interface
709 if (!(priv = kzalloc(sizeof(struct parport_uss720_private), GFP_KERNEL))) {
714 priv->usbdev = usbdev;
715 kref_init(&priv->ref_count);
716 spin_lock_init(&priv->asynclock);
717 INIT_LIST_HEAD(&priv->asynclist);
718 if (!(pp = parport_register_port(0, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &parport_uss720_ops))) {
719 warn("could not register parport");
724 pp->private_data = priv;
725 pp->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_TRISTATE | PARPORT_MODE_EPP | PARPORT_MODE_ECP | PARPORT_MODE_COMPAT;
727 /* set the USS720 control register to manual mode, no ECP compression, enable all ints */
728 set_1284_register(pp, 7, 0x00, GFP_KERNEL);
729 set_1284_register(pp, 6, 0x30, GFP_KERNEL); /* PS/2 mode */
730 set_1284_register(pp, 2, 0x0c, GFP_KERNEL);
732 get_1284_register(pp, 0, ®, GFP_KERNEL);
733 dbg("reg: %02x %02x %02x %02x %02x %02x %02x",
734 priv->reg[0], priv->reg[1], priv->reg[2], priv->reg[3], priv->reg[4], priv->reg[5], priv->reg[6]);
736 endpoint = &interface->endpoint[2];
737 dbg("epaddr %d interval %d", endpoint->desc.bEndpointAddress, endpoint->desc.bInterval);
738 parport_announce_port(pp);
740 usb_set_intfdata(intf, pp);
744 kill_all_async_requests_priv(priv);
745 kref_put(&priv->ref_count, destroy_priv);
749 static void uss720_disconnect(struct usb_interface *intf)
751 struct parport *pp = usb_get_intfdata(intf);
752 struct parport_uss720_private *priv;
753 struct usb_device *usbdev;
756 usb_set_intfdata(intf, NULL);
758 priv = pp->private_data;
759 usbdev = priv->usbdev;
762 dbg("parport_remove_port");
763 parport_remove_port(pp);
764 parport_put_port(pp);
765 kill_all_async_requests_priv(priv);
766 kref_put(&priv->ref_count, destroy_priv);
768 dbg("disconnect done");
771 /* table of cables that work through this driver */
772 static struct usb_device_id uss720_table [] = {
773 { USB_DEVICE(0x047e, 0x1001) },
774 { USB_DEVICE(0x0557, 0x2001) },
775 { USB_DEVICE(0x0729, 0x1284) },
776 { USB_DEVICE(0x1293, 0x0002) },
777 { } /* Terminating entry */
780 MODULE_DEVICE_TABLE (usb, uss720_table);
783 static struct usb_driver uss720_driver = {
785 .probe = uss720_probe,
786 .disconnect = uss720_disconnect,
787 .id_table = uss720_table,
790 /* --------------------------------------------------------------------- */
792 MODULE_AUTHOR(DRIVER_AUTHOR);
793 MODULE_DESCRIPTION(DRIVER_DESC);
794 MODULE_LICENSE("GPL");
796 static int __init uss720_init(void)
799 retval = usb_register(&uss720_driver);
803 info(DRIVER_VERSION ":" DRIVER_DESC);
804 info("NOTE: this is a special purpose driver to allow nonstandard");
805 info("protocols (eg. bitbang) over USS720 usb to parallel cables");
806 info("If you just want to connect to a printer, use usblp instead");
811 static void __exit uss720_cleanup(void)
813 usb_deregister(&uss720_driver);
816 module_init(uss720_init);
817 module_exit(uss720_cleanup);
819 /* --------------------------------------------------------------------- */