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 /*****************************************************************************/
46 #include <linux/module.h>
47 #include <linux/socket.h>
48 #include <linux/parport.h>
49 #include <linux/init.h>
50 #include <linux/usb.h>
51 #include <linux/delay.h>
52 #include <linux/completion.h>
53 #include <linux/kref.h>
58 #define DRIVER_VERSION "v0.6"
59 #define DRIVER_AUTHOR "Thomas M. Sailer, t.sailer@alumni.ethz.ch"
60 #define DRIVER_DESC "USB Parport Cable driver for Cables using the Lucent Technologies USS720 Chip"
62 /* --------------------------------------------------------------------- */
64 struct parport_uss720_private {
65 struct usb_device *usbdev;
67 struct kref ref_count;
68 __u8 reg[7]; /* USB registers */
69 struct list_head asynclist;
73 struct uss720_async_request {
74 struct parport_uss720_private *priv;
75 struct kref ref_count;
76 struct list_head asynclist;
77 struct completion compl;
79 struct usb_ctrlrequest dr;
83 /* --------------------------------------------------------------------- */
85 static void destroy_priv(struct kref *kref)
87 struct parport_uss720_private *priv = container_of(kref, struct parport_uss720_private, ref_count);
89 usb_put_dev(priv->usbdev);
91 dbg("destroying priv datastructure");
94 static void destroy_async(struct kref *kref)
96 struct uss720_async_request *rq = container_of(kref, struct uss720_async_request, ref_count);
97 struct parport_uss720_private *priv = rq->priv;
101 usb_free_urb(rq->urb);
102 spin_lock_irqsave(&priv->asynclock, flags);
103 list_del_init(&rq->asynclist);
104 spin_unlock_irqrestore(&priv->asynclock, flags);
106 kref_put(&priv->ref_count, destroy_priv);
109 /* --------------------------------------------------------------------- */
111 static void async_complete(struct urb *urb, struct pt_regs *ptregs)
113 struct uss720_async_request *rq;
115 struct parport_uss720_private *priv;
121 err("async_complete: urb error %d", urb->status);
122 } else if (rq->dr.bRequest == 3) {
123 memcpy(priv->reg, rq->reg, sizeof(priv->reg));
125 dbg("async_complete regs %02x %02x %02x %02x %02x %02x %02x",
126 (unsigned int)priv->reg[0], (unsigned int)priv->reg[1], (unsigned int)priv->reg[2],
127 (unsigned int)priv->reg[3], (unsigned int)priv->reg[4], (unsigned int)priv->reg[5],
128 (unsigned int)priv->reg[6]);
130 /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
131 if (rq->reg[2] & rq->reg[1] & 0x10 && pp)
132 parport_generic_irq(0, pp, NULL);
134 complete(&rq->compl);
135 kref_put(&rq->ref_count, destroy_async);
138 static struct uss720_async_request *submit_async_request(struct parport_uss720_private *priv,
139 __u8 request, __u8 requesttype, __u16 value, __u16 index,
142 struct usb_device *usbdev;
143 struct uss720_async_request *rq;
149 usbdev = priv->usbdev;
152 rq = kmalloc(sizeof(struct uss720_async_request), mem_flags);
154 err("submit_async_request out of memory");
157 kref_init(&rq->ref_count);
158 INIT_LIST_HEAD(&rq->asynclist);
159 init_completion(&rq->compl);
160 kref_get(&priv->ref_count);
162 rq->urb = usb_alloc_urb(0, mem_flags);
164 kref_put(&rq->ref_count, destroy_async);
165 err("submit_async_request out of memory");
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 ret = usb_submit_urb(rq->urb, mem_flags);
182 kref_get(&rq->ref_count);
185 kref_put(&rq->ref_count, destroy_async);
186 err("submit_async_request submit_urb failed with %d", ret);
190 static unsigned int kill_all_async_requests_priv(struct parport_uss720_private *priv)
192 struct uss720_async_request *rq;
194 unsigned int ret = 0;
196 spin_lock_irqsave(&priv->asynclock, flags);
197 list_for_each_entry(rq, &priv->asynclist, asynclist) {
198 usb_unlink_urb(rq->urb);
201 spin_unlock_irqrestore(&priv->asynclock, flags);
205 /* --------------------------------------------------------------------- */
207 static int get_1284_register(struct parport *pp, unsigned char reg, unsigned char *val, gfp_t mem_flags)
209 struct parport_uss720_private *priv;
210 struct uss720_async_request *rq;
211 static const unsigned char regindex[9] = {
212 4, 0, 1, 5, 5, 0, 2, 3, 6
218 priv = pp->private_data;
219 rq = submit_async_request(priv, 3, 0xc0, ((unsigned int)reg) << 8, 0, mem_flags);
221 err("get_1284_register(%u) failed", (unsigned int)reg);
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 warn("get_1284_register: usb error %d", ret);
233 kref_put(&rq->ref_count, destroy_async);
236 warn("get_1284_register timeout");
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 err("set_1284_register(%u,%u) failed", (unsigned int)reg, (unsigned int)val);
254 kref_put(&rq->ref_count, destroy_async);
258 /* --------------------------------------------------------------------- */
267 /* Safely change the mode bits in the ECR */
268 static int change_mode(struct parport *pp, int m)
270 struct parport_uss720_private *priv = pp->private_data;
274 if (get_1284_register(pp, 6, ®, GFP_KERNEL))
276 /* Bits <7:5> contain the mode. */
277 mode = (priv->reg[2] >> 5) & 0x7;
280 /* We have to go through mode 000 or 001 */
281 if (mode > ECR_PS2 && m > ECR_PS2)
282 if (change_mode(pp, ECR_PS2))
285 if (m <= ECR_PS2 && !(priv->reg[1] & 0x20)) {
286 /* This mode resets the FIFO, so we may
287 * have to wait for it to drain first. */
288 unsigned long expire = jiffies + pp->physport->cad->timeout;
290 case ECR_PPF: /* Parallel Port FIFO mode */
291 case ECR_ECP: /* ECP Parallel Port mode */
294 if (get_1284_register(pp, 6, ®, GFP_KERNEL))
296 if (priv->reg[2] & 0x01)
298 if (time_after_eq (jiffies, expire))
299 /* The FIFO is stuck. */
301 msleep_interruptible(10);
302 if (signal_pending (current))
308 if (set_1284_register(pp, 6, m << 5, GFP_KERNEL))
310 if (get_1284_register(pp, 6, ®, GFP_KERNEL))
316 * Clear TIMEOUT BIT in EPP MODE
318 static int clear_epp_timeout(struct parport *pp)
322 if (get_1284_register(pp, 1, &stat, GFP_KERNEL))
331 static int uss720_irq(int usbstatus, void *buffer, int len, void *dev_id)
333 struct parport *pp = (struct parport *)dev_id;
334 struct parport_uss720_private *priv = pp->private_data;
336 if (usbstatus != 0 || len < 4 || !buffer)
338 memcpy(priv->reg, buffer, 4);
339 /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
340 if (priv->reg[2] & priv->reg[1] & 0x10)
341 parport_generic_irq(0, pp, NULL);
346 static void parport_uss720_write_data(struct parport *pp, unsigned char d)
348 set_1284_register(pp, 0, d, GFP_KERNEL);
351 static unsigned char parport_uss720_read_data(struct parport *pp)
355 if (get_1284_register(pp, 0, &ret, GFP_KERNEL))
360 static void parport_uss720_write_control(struct parport *pp, unsigned char d)
362 struct parport_uss720_private *priv = pp->private_data;
364 d = (d & 0xf) | (priv->reg[1] & 0xf0);
365 if (set_1284_register(pp, 2, d, GFP_KERNEL))
370 static unsigned char parport_uss720_read_control(struct parport *pp)
372 struct parport_uss720_private *priv = pp->private_data;
373 return priv->reg[1] & 0xf; /* Use soft copy */
376 static unsigned char parport_uss720_frob_control(struct parport *pp, unsigned char mask, unsigned char val)
378 struct parport_uss720_private *priv = pp->private_data;
383 d = (priv->reg[1] & (~mask)) ^ val;
384 if (set_1284_register(pp, 2, d, GFP_KERNEL))
390 static unsigned char parport_uss720_read_status(struct parport *pp)
394 if (get_1284_register(pp, 1, &ret, GFP_KERNEL))
399 static void parport_uss720_disable_irq(struct parport *pp)
401 struct parport_uss720_private *priv = pp->private_data;
404 d = priv->reg[1] & ~0x10;
405 if (set_1284_register(pp, 2, d, GFP_KERNEL))
410 static void parport_uss720_enable_irq(struct parport *pp)
412 struct parport_uss720_private *priv = pp->private_data;
415 d = priv->reg[1] | 0x10;
416 if (set_1284_register(pp, 2, d, GFP_KERNEL))
421 static void parport_uss720_data_forward (struct parport *pp)
423 struct parport_uss720_private *priv = pp->private_data;
426 d = priv->reg[1] & ~0x20;
427 if (set_1284_register(pp, 2, d, GFP_KERNEL))
432 static void parport_uss720_data_reverse (struct parport *pp)
434 struct parport_uss720_private *priv = pp->private_data;
437 d = priv->reg[1] | 0x20;
438 if (set_1284_register(pp, 2, d, GFP_KERNEL))
443 static void parport_uss720_init_state(struct pardevice *dev, struct parport_state *s)
445 s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
449 static void parport_uss720_save_state(struct parport *pp, struct parport_state *s)
451 struct parport_uss720_private *priv = pp->private_data;
454 if (get_1284_register(pp, 2, NULL, GFP_ATOMIC))
457 s->u.pc.ctr = priv->reg[1];
458 s->u.pc.ecr = priv->reg[2];
461 static void parport_uss720_restore_state(struct parport *pp, struct parport_state *s)
463 struct parport_uss720_private *priv = pp->private_data;
465 set_1284_register(pp, 2, s->u.pc.ctr, GFP_ATOMIC);
466 set_1284_register(pp, 6, s->u.pc.ecr, GFP_ATOMIC);
467 get_1284_register(pp, 2, NULL, GFP_ATOMIC);
468 priv->reg[1] = s->u.pc.ctr;
469 priv->reg[2] = s->u.pc.ecr;
472 static size_t parport_uss720_epp_read_data(struct parport *pp, void *buf, size_t length, int flags)
474 struct parport_uss720_private *priv = pp->private_data;
477 if (change_mode(pp, ECR_EPP))
479 for (; got < length; got++) {
480 if (get_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
483 if (priv->reg[0] & 0x01) {
484 clear_epp_timeout(pp);
488 change_mode(pp, ECR_PS2);
492 static size_t parport_uss720_epp_write_data(struct parport *pp, const void *buf, size_t length, int flags)
495 struct parport_uss720_private *priv = pp->private_data;
498 if (change_mode(pp, ECR_EPP))
500 for (; written < length; written++) {
501 if (set_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
504 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
506 if (priv->reg[0] & 0x01) {
507 clear_epp_timeout(pp);
511 change_mode(pp, ECR_PS2);
514 struct parport_uss720_private *priv = pp->private_data;
515 struct usb_device *usbdev = priv->usbdev;
521 if (change_mode(pp, ECR_EPP))
523 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buf, length, &rlen, 20000);
525 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buf, length, rlen);
526 change_mode(pp, ECR_PS2);
531 static size_t parport_uss720_epp_read_addr(struct parport *pp, void *buf, size_t length, int flags)
533 struct parport_uss720_private *priv = pp->private_data;
536 if (change_mode(pp, ECR_EPP))
538 for (; got < length; got++) {
539 if (get_1284_register(pp, 3, (char *)buf, GFP_KERNEL))
542 if (priv->reg[0] & 0x01) {
543 clear_epp_timeout(pp);
547 change_mode(pp, ECR_PS2);
551 static size_t parport_uss720_epp_write_addr(struct parport *pp, const void *buf, size_t length, int flags)
553 struct parport_uss720_private *priv = pp->private_data;
556 if (change_mode(pp, ECR_EPP))
558 for (; written < length; written++) {
559 if (set_1284_register(pp, 3, *(char *)buf, GFP_KERNEL))
562 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
564 if (priv->reg[0] & 0x01) {
565 clear_epp_timeout(pp);
569 change_mode(pp, ECR_PS2);
573 static size_t parport_uss720_ecp_write_data(struct parport *pp, const void *buffer, size_t len, int flags)
575 struct parport_uss720_private *priv = pp->private_data;
576 struct usb_device *usbdev = priv->usbdev;
582 if (change_mode(pp, ECR_ECP))
584 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
586 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
587 change_mode(pp, ECR_PS2);
591 static size_t parport_uss720_ecp_read_data(struct parport *pp, void *buffer, size_t len, int flags)
593 struct parport_uss720_private *priv = pp->private_data;
594 struct usb_device *usbdev = priv->usbdev;
600 if (change_mode(pp, ECR_ECP))
602 i = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), buffer, len, &rlen, 20000);
604 printk(KERN_ERR "uss720: recvbulk ep 2 buf %p len %Zu rlen %u\n", buffer, len, rlen);
605 change_mode(pp, ECR_PS2);
609 static size_t parport_uss720_ecp_write_addr(struct parport *pp, const void *buffer, size_t len, int flags)
613 if (change_mode(pp, ECR_ECP))
615 for (; written < len; written++) {
616 if (set_1284_register(pp, 5, *(char *)buffer, GFP_KERNEL))
620 change_mode(pp, ECR_PS2);
624 static size_t parport_uss720_write_compat(struct parport *pp, const void *buffer, size_t len, int flags)
626 struct parport_uss720_private *priv = pp->private_data;
627 struct usb_device *usbdev = priv->usbdev;
633 if (change_mode(pp, ECR_PPF))
635 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
637 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
638 change_mode(pp, ECR_PS2);
642 /* --------------------------------------------------------------------- */
644 static struct parport_operations parport_uss720_ops =
646 .owner = THIS_MODULE,
647 .write_data = parport_uss720_write_data,
648 .read_data = parport_uss720_read_data,
650 .write_control = parport_uss720_write_control,
651 .read_control = parport_uss720_read_control,
652 .frob_control = parport_uss720_frob_control,
654 .read_status = parport_uss720_read_status,
656 .enable_irq = parport_uss720_enable_irq,
657 .disable_irq = parport_uss720_disable_irq,
659 .data_forward = parport_uss720_data_forward,
660 .data_reverse = parport_uss720_data_reverse,
662 .init_state = parport_uss720_init_state,
663 .save_state = parport_uss720_save_state,
664 .restore_state = parport_uss720_restore_state,
666 .epp_write_data = parport_uss720_epp_write_data,
667 .epp_read_data = parport_uss720_epp_read_data,
668 .epp_write_addr = parport_uss720_epp_write_addr,
669 .epp_read_addr = parport_uss720_epp_read_addr,
671 .ecp_write_data = parport_uss720_ecp_write_data,
672 .ecp_read_data = parport_uss720_ecp_read_data,
673 .ecp_write_addr = parport_uss720_ecp_write_addr,
675 .compat_write_data = parport_uss720_write_compat,
676 .nibble_read_data = parport_ieee1284_read_nibble,
677 .byte_read_data = parport_ieee1284_read_byte,
680 /* --------------------------------------------------------------------- */
682 static int uss720_probe(struct usb_interface *intf,
683 const struct usb_device_id *id)
685 struct usb_device *usbdev = usb_get_dev(interface_to_usbdev(intf));
686 struct usb_host_interface *interface;
687 struct usb_host_endpoint *endpoint;
688 struct parport_uss720_private *priv;
693 dbg("probe: vendor id 0x%x, device id 0x%x\n",
694 le16_to_cpu(usbdev->descriptor.idVendor),
695 le16_to_cpu(usbdev->descriptor.idProduct));
697 /* our known interfaces have 3 alternate settings */
698 if (intf->num_altsetting != 3) {
702 i = usb_set_interface(usbdev, intf->altsetting->desc.bInterfaceNumber, 2);
703 dbg("set inteface result %d", i);
705 interface = intf->cur_altsetting;
708 * Allocate parport interface
710 if (!(priv = kcalloc(sizeof(struct parport_uss720_private), 1, GFP_KERNEL))) {
715 priv->usbdev = usbdev;
716 kref_init(&priv->ref_count);
717 spin_lock_init(&priv->asynclock);
718 INIT_LIST_HEAD(&priv->asynclist);
719 if (!(pp = parport_register_port(0, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &parport_uss720_ops))) {
720 warn("could not register parport");
725 pp->private_data = priv;
726 pp->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_TRISTATE | PARPORT_MODE_EPP | PARPORT_MODE_ECP | PARPORT_MODE_COMPAT;
728 /* set the USS720 control register to manual mode, no ECP compression, enable all ints */
729 set_1284_register(pp, 7, 0x00, GFP_KERNEL);
730 set_1284_register(pp, 6, 0x30, GFP_KERNEL); /* PS/2 mode */
731 set_1284_register(pp, 2, 0x0c, GFP_KERNEL);
733 get_1284_register(pp, 0, ®, GFP_KERNEL);
734 dbg("reg: %02x %02x %02x %02x %02x %02x %02x",
735 priv->reg[0], priv->reg[1], priv->reg[2], priv->reg[3], priv->reg[4], priv->reg[5], priv->reg[6]);
737 endpoint = &interface->endpoint[2];
738 dbg("epaddr %d interval %d", endpoint->desc.bEndpointAddress, endpoint->desc.bInterval);
739 parport_announce_port(pp);
741 usb_set_intfdata(intf, pp);
745 kill_all_async_requests_priv(priv);
746 kref_put(&priv->ref_count, destroy_priv);
750 static void uss720_disconnect(struct usb_interface *intf)
752 struct parport *pp = usb_get_intfdata(intf);
753 struct parport_uss720_private *priv;
754 struct usb_device *usbdev;
757 usb_set_intfdata(intf, NULL);
759 priv = pp->private_data;
760 usbdev = priv->usbdev;
763 dbg("parport_remove_port");
764 parport_remove_port(pp);
765 parport_put_port(pp);
766 kill_all_async_requests_priv(priv);
767 kref_put(&priv->ref_count, destroy_priv);
769 dbg("disconnect done");
772 /* table of cables that work through this driver */
773 static struct usb_device_id uss720_table [] = {
774 { USB_DEVICE(0x047e, 0x1001) },
775 { USB_DEVICE(0x0557, 0x2001) },
776 { USB_DEVICE(0x0729, 0x1284) },
777 { USB_DEVICE(0x1293, 0x0002) },
778 { } /* Terminating entry */
781 MODULE_DEVICE_TABLE (usb, uss720_table);
784 static struct usb_driver uss720_driver = {
785 .owner = THIS_MODULE,
787 .probe = uss720_probe,
788 .disconnect = uss720_disconnect,
789 .id_table = uss720_table,
792 /* --------------------------------------------------------------------- */
794 MODULE_AUTHOR(DRIVER_AUTHOR);
795 MODULE_DESCRIPTION(DRIVER_DESC);
796 MODULE_LICENSE("GPL");
798 static int __init uss720_init(void)
801 retval = usb_register(&uss720_driver);
805 info(DRIVER_VERSION ":" DRIVER_DESC);
806 info("NOTE: this is a special purpose driver to allow nonstandard");
807 info("protocols (eg. bitbang) over USS720 usb to parallel cables");
808 info("If you just want to connect to a printer, use usblp instead");
813 static void __exit uss720_cleanup(void)
815 usb_deregister(&uss720_driver);
818 module_init(uss720_init);
819 module_exit(uss720_cleanup);
821 /* --------------------------------------------------------------------- */