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;
119 err("async_complete: urb error %d", urb->status);
120 } else if (rq->dr.bRequest == 3) {
121 memcpy(priv->reg, rq->reg, sizeof(priv->reg));
123 dbg("async_complete regs %02x %02x %02x %02x %02x %02x %02x",
124 (unsigned int)priv->reg[0], (unsigned int)priv->reg[1], (unsigned int)priv->reg[2],
125 (unsigned int)priv->reg[3], (unsigned int)priv->reg[4], (unsigned int)priv->reg[5],
126 (unsigned int)priv->reg[6]);
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(0, 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 = kmalloc(sizeof(struct uss720_async_request), mem_flags);
152 err("submit_async_request out of memory");
155 kref_init(&rq->ref_count);
156 INIT_LIST_HEAD(&rq->asynclist);
157 init_completion(&rq->compl);
158 kref_get(&priv->ref_count);
160 rq->urb = usb_alloc_urb(0, mem_flags);
162 kref_put(&rq->ref_count, destroy_async);
163 err("submit_async_request out of memory");
166 rq->dr.bRequestType = requesttype;
167 rq->dr.bRequest = request;
168 rq->dr.wValue = cpu_to_le16(value);
169 rq->dr.wIndex = cpu_to_le16(index);
170 rq->dr.wLength = cpu_to_le16((request == 3) ? sizeof(rq->reg) : 0);
171 usb_fill_control_urb(rq->urb, usbdev, (requesttype & 0x80) ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0),
172 (unsigned char *)&rq->dr,
173 (request == 3) ? rq->reg : NULL, (request == 3) ? sizeof(rq->reg) : 0, async_complete, rq);
174 /* rq->urb->transfer_flags |= URB_ASYNC_UNLINK; */
175 spin_lock_irqsave(&priv->asynclock, flags);
176 list_add_tail(&rq->asynclist, &priv->asynclist);
177 spin_unlock_irqrestore(&priv->asynclock, flags);
178 ret = usb_submit_urb(rq->urb, mem_flags);
180 kref_get(&rq->ref_count);
183 kref_put(&rq->ref_count, destroy_async);
184 err("submit_async_request submit_urb failed with %d", 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 err("get_1284_register(%u) failed", (unsigned int)reg);
223 kref_put(&rq->ref_count, destroy_async);
226 if (wait_for_completion_timeout(&rq->compl, HZ)) {
227 ret = rq->urb->status;
228 *val = priv->reg[(reg >= 9) ? 0 : regindex[reg]];
230 warn("get_1284_register: usb error %d", ret);
231 kref_put(&rq->ref_count, destroy_async);
234 warn("get_1284_register timeout");
235 kill_all_async_requests_priv(priv);
239 static int set_1284_register(struct parport *pp, unsigned char reg, unsigned char val, gfp_t mem_flags)
241 struct parport_uss720_private *priv;
242 struct uss720_async_request *rq;
246 priv = pp->private_data;
247 rq = submit_async_request(priv, 4, 0x40, (((unsigned int)reg) << 8) | val, 0, mem_flags);
249 err("set_1284_register(%u,%u) failed", (unsigned int)reg, (unsigned int)val);
252 kref_put(&rq->ref_count, destroy_async);
256 /* --------------------------------------------------------------------- */
265 /* Safely change the mode bits in the ECR */
266 static int change_mode(struct parport *pp, int m)
268 struct parport_uss720_private *priv = pp->private_data;
272 if (get_1284_register(pp, 6, ®, GFP_KERNEL))
274 /* Bits <7:5> contain the mode. */
275 mode = (priv->reg[2] >> 5) & 0x7;
278 /* We have to go through mode 000 or 001 */
279 if (mode > ECR_PS2 && m > ECR_PS2)
280 if (change_mode(pp, ECR_PS2))
283 if (m <= ECR_PS2 && !(priv->reg[1] & 0x20)) {
284 /* This mode resets the FIFO, so we may
285 * have to wait for it to drain first. */
286 unsigned long expire = jiffies + pp->physport->cad->timeout;
288 case ECR_PPF: /* Parallel Port FIFO mode */
289 case ECR_ECP: /* ECP Parallel Port mode */
292 if (get_1284_register(pp, 6, ®, GFP_KERNEL))
294 if (priv->reg[2] & 0x01)
296 if (time_after_eq (jiffies, expire))
297 /* The FIFO is stuck. */
299 msleep_interruptible(10);
300 if (signal_pending (current))
306 if (set_1284_register(pp, 6, m << 5, GFP_KERNEL))
308 if (get_1284_register(pp, 6, ®, GFP_KERNEL))
314 * Clear TIMEOUT BIT in EPP MODE
316 static int clear_epp_timeout(struct parport *pp)
320 if (get_1284_register(pp, 1, &stat, GFP_KERNEL))
329 static int uss720_irq(int usbstatus, void *buffer, int len, void *dev_id)
331 struct parport *pp = (struct parport *)dev_id;
332 struct parport_uss720_private *priv = pp->private_data;
334 if (usbstatus != 0 || len < 4 || !buffer)
336 memcpy(priv->reg, buffer, 4);
337 /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
338 if (priv->reg[2] & priv->reg[1] & 0x10)
339 parport_generic_irq(0, pp, NULL);
344 static void parport_uss720_write_data(struct parport *pp, unsigned char d)
346 set_1284_register(pp, 0, d, GFP_KERNEL);
349 static unsigned char parport_uss720_read_data(struct parport *pp)
353 if (get_1284_register(pp, 0, &ret, GFP_KERNEL))
358 static void parport_uss720_write_control(struct parport *pp, unsigned char d)
360 struct parport_uss720_private *priv = pp->private_data;
362 d = (d & 0xf) | (priv->reg[1] & 0xf0);
363 if (set_1284_register(pp, 2, d, GFP_KERNEL))
368 static unsigned char parport_uss720_read_control(struct parport *pp)
370 struct parport_uss720_private *priv = pp->private_data;
371 return priv->reg[1] & 0xf; /* Use soft copy */
374 static unsigned char parport_uss720_frob_control(struct parport *pp, unsigned char mask, unsigned char val)
376 struct parport_uss720_private *priv = pp->private_data;
381 d = (priv->reg[1] & (~mask)) ^ val;
382 if (set_1284_register(pp, 2, d, GFP_KERNEL))
388 static unsigned char parport_uss720_read_status(struct parport *pp)
392 if (get_1284_register(pp, 1, &ret, GFP_KERNEL))
397 static void parport_uss720_disable_irq(struct parport *pp)
399 struct parport_uss720_private *priv = pp->private_data;
402 d = priv->reg[1] & ~0x10;
403 if (set_1284_register(pp, 2, d, GFP_KERNEL))
408 static void parport_uss720_enable_irq(struct parport *pp)
410 struct parport_uss720_private *priv = pp->private_data;
413 d = priv->reg[1] | 0x10;
414 if (set_1284_register(pp, 2, d, GFP_KERNEL))
419 static void parport_uss720_data_forward (struct parport *pp)
421 struct parport_uss720_private *priv = pp->private_data;
424 d = priv->reg[1] & ~0x20;
425 if (set_1284_register(pp, 2, d, GFP_KERNEL))
430 static void parport_uss720_data_reverse (struct parport *pp)
432 struct parport_uss720_private *priv = pp->private_data;
435 d = priv->reg[1] | 0x20;
436 if (set_1284_register(pp, 2, d, GFP_KERNEL))
441 static void parport_uss720_init_state(struct pardevice *dev, struct parport_state *s)
443 s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
447 static void parport_uss720_save_state(struct parport *pp, struct parport_state *s)
449 struct parport_uss720_private *priv = pp->private_data;
452 if (get_1284_register(pp, 2, NULL, GFP_ATOMIC))
455 s->u.pc.ctr = priv->reg[1];
456 s->u.pc.ecr = priv->reg[2];
459 static void parport_uss720_restore_state(struct parport *pp, struct parport_state *s)
461 struct parport_uss720_private *priv = pp->private_data;
463 set_1284_register(pp, 2, s->u.pc.ctr, GFP_ATOMIC);
464 set_1284_register(pp, 6, s->u.pc.ecr, GFP_ATOMIC);
465 get_1284_register(pp, 2, NULL, GFP_ATOMIC);
466 priv->reg[1] = s->u.pc.ctr;
467 priv->reg[2] = s->u.pc.ecr;
470 static size_t parport_uss720_epp_read_data(struct parport *pp, void *buf, size_t length, int flags)
472 struct parport_uss720_private *priv = pp->private_data;
475 if (change_mode(pp, ECR_EPP))
477 for (; got < length; got++) {
478 if (get_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
481 if (priv->reg[0] & 0x01) {
482 clear_epp_timeout(pp);
486 change_mode(pp, ECR_PS2);
490 static size_t parport_uss720_epp_write_data(struct parport *pp, const void *buf, size_t length, int flags)
493 struct parport_uss720_private *priv = pp->private_data;
496 if (change_mode(pp, ECR_EPP))
498 for (; written < length; written++) {
499 if (set_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
502 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
504 if (priv->reg[0] & 0x01) {
505 clear_epp_timeout(pp);
509 change_mode(pp, ECR_PS2);
512 struct parport_uss720_private *priv = pp->private_data;
513 struct usb_device *usbdev = priv->usbdev;
519 if (change_mode(pp, ECR_EPP))
521 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buf, length, &rlen, 20000);
523 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buf, length, rlen);
524 change_mode(pp, ECR_PS2);
529 static size_t parport_uss720_epp_read_addr(struct parport *pp, void *buf, size_t length, int flags)
531 struct parport_uss720_private *priv = pp->private_data;
534 if (change_mode(pp, ECR_EPP))
536 for (; got < length; got++) {
537 if (get_1284_register(pp, 3, (char *)buf, GFP_KERNEL))
540 if (priv->reg[0] & 0x01) {
541 clear_epp_timeout(pp);
545 change_mode(pp, ECR_PS2);
549 static size_t parport_uss720_epp_write_addr(struct parport *pp, const void *buf, size_t length, int flags)
551 struct parport_uss720_private *priv = pp->private_data;
554 if (change_mode(pp, ECR_EPP))
556 for (; written < length; written++) {
557 if (set_1284_register(pp, 3, *(char *)buf, GFP_KERNEL))
560 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
562 if (priv->reg[0] & 0x01) {
563 clear_epp_timeout(pp);
567 change_mode(pp, ECR_PS2);
571 static size_t parport_uss720_ecp_write_data(struct parport *pp, const void *buffer, size_t len, int flags)
573 struct parport_uss720_private *priv = pp->private_data;
574 struct usb_device *usbdev = priv->usbdev;
580 if (change_mode(pp, ECR_ECP))
582 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
584 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
585 change_mode(pp, ECR_PS2);
589 static size_t parport_uss720_ecp_read_data(struct parport *pp, void *buffer, size_t len, int flags)
591 struct parport_uss720_private *priv = pp->private_data;
592 struct usb_device *usbdev = priv->usbdev;
598 if (change_mode(pp, ECR_ECP))
600 i = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), buffer, len, &rlen, 20000);
602 printk(KERN_ERR "uss720: recvbulk ep 2 buf %p len %Zu rlen %u\n", buffer, len, rlen);
603 change_mode(pp, ECR_PS2);
607 static size_t parport_uss720_ecp_write_addr(struct parport *pp, const void *buffer, size_t len, int flags)
611 if (change_mode(pp, ECR_ECP))
613 for (; written < len; written++) {
614 if (set_1284_register(pp, 5, *(char *)buffer, GFP_KERNEL))
618 change_mode(pp, ECR_PS2);
622 static size_t parport_uss720_write_compat(struct parport *pp, const void *buffer, size_t len, int flags)
624 struct parport_uss720_private *priv = pp->private_data;
625 struct usb_device *usbdev = priv->usbdev;
631 if (change_mode(pp, ECR_PPF))
633 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
635 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
636 change_mode(pp, ECR_PS2);
640 /* --------------------------------------------------------------------- */
642 static struct parport_operations parport_uss720_ops =
644 .owner = THIS_MODULE,
645 .write_data = parport_uss720_write_data,
646 .read_data = parport_uss720_read_data,
648 .write_control = parport_uss720_write_control,
649 .read_control = parport_uss720_read_control,
650 .frob_control = parport_uss720_frob_control,
652 .read_status = parport_uss720_read_status,
654 .enable_irq = parport_uss720_enable_irq,
655 .disable_irq = parport_uss720_disable_irq,
657 .data_forward = parport_uss720_data_forward,
658 .data_reverse = parport_uss720_data_reverse,
660 .init_state = parport_uss720_init_state,
661 .save_state = parport_uss720_save_state,
662 .restore_state = parport_uss720_restore_state,
664 .epp_write_data = parport_uss720_epp_write_data,
665 .epp_read_data = parport_uss720_epp_read_data,
666 .epp_write_addr = parport_uss720_epp_write_addr,
667 .epp_read_addr = parport_uss720_epp_read_addr,
669 .ecp_write_data = parport_uss720_ecp_write_data,
670 .ecp_read_data = parport_uss720_ecp_read_data,
671 .ecp_write_addr = parport_uss720_ecp_write_addr,
673 .compat_write_data = parport_uss720_write_compat,
674 .nibble_read_data = parport_ieee1284_read_nibble,
675 .byte_read_data = parport_ieee1284_read_byte,
678 /* --------------------------------------------------------------------- */
680 static int uss720_probe(struct usb_interface *intf,
681 const struct usb_device_id *id)
683 struct usb_device *usbdev = usb_get_dev(interface_to_usbdev(intf));
684 struct usb_host_interface *interface;
685 struct usb_host_endpoint *endpoint;
686 struct parport_uss720_private *priv;
691 dbg("probe: vendor id 0x%x, device id 0x%x\n",
692 le16_to_cpu(usbdev->descriptor.idVendor),
693 le16_to_cpu(usbdev->descriptor.idProduct));
695 /* our known interfaces have 3 alternate settings */
696 if (intf->num_altsetting != 3) {
700 i = usb_set_interface(usbdev, intf->altsetting->desc.bInterfaceNumber, 2);
701 dbg("set inteface result %d", i);
703 interface = intf->cur_altsetting;
706 * Allocate parport interface
708 if (!(priv = kcalloc(sizeof(struct parport_uss720_private), 1, GFP_KERNEL))) {
713 priv->usbdev = usbdev;
714 kref_init(&priv->ref_count);
715 spin_lock_init(&priv->asynclock);
716 INIT_LIST_HEAD(&priv->asynclist);
717 if (!(pp = parport_register_port(0, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &parport_uss720_ops))) {
718 warn("could not register parport");
723 pp->private_data = priv;
724 pp->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_TRISTATE | PARPORT_MODE_EPP | PARPORT_MODE_ECP | PARPORT_MODE_COMPAT;
726 /* set the USS720 control register to manual mode, no ECP compression, enable all ints */
727 set_1284_register(pp, 7, 0x00, GFP_KERNEL);
728 set_1284_register(pp, 6, 0x30, GFP_KERNEL); /* PS/2 mode */
729 set_1284_register(pp, 2, 0x0c, GFP_KERNEL);
731 get_1284_register(pp, 0, ®, GFP_KERNEL);
732 dbg("reg: %02x %02x %02x %02x %02x %02x %02x",
733 priv->reg[0], priv->reg[1], priv->reg[2], priv->reg[3], priv->reg[4], priv->reg[5], priv->reg[6]);
735 endpoint = &interface->endpoint[2];
736 dbg("epaddr %d interval %d", endpoint->desc.bEndpointAddress, endpoint->desc.bInterval);
737 parport_announce_port(pp);
739 usb_set_intfdata(intf, pp);
743 kill_all_async_requests_priv(priv);
744 kref_put(&priv->ref_count, destroy_priv);
748 static void uss720_disconnect(struct usb_interface *intf)
750 struct parport *pp = usb_get_intfdata(intf);
751 struct parport_uss720_private *priv;
752 struct usb_device *usbdev;
755 usb_set_intfdata(intf, NULL);
757 priv = pp->private_data;
758 usbdev = priv->usbdev;
761 dbg("parport_remove_port");
762 parport_remove_port(pp);
763 parport_put_port(pp);
764 kill_all_async_requests_priv(priv);
765 kref_put(&priv->ref_count, destroy_priv);
767 dbg("disconnect done");
770 /* table of cables that work through this driver */
771 static struct usb_device_id uss720_table [] = {
772 { USB_DEVICE(0x047e, 0x1001) },
773 { USB_DEVICE(0x0557, 0x2001) },
774 { USB_DEVICE(0x0729, 0x1284) },
775 { USB_DEVICE(0x1293, 0x0002) },
776 { } /* Terminating entry */
779 MODULE_DEVICE_TABLE (usb, uss720_table);
782 static struct usb_driver uss720_driver = {
784 .probe = uss720_probe,
785 .disconnect = uss720_disconnect,
786 .id_table = uss720_table,
789 /* --------------------------------------------------------------------- */
791 MODULE_AUTHOR(DRIVER_AUTHOR);
792 MODULE_DESCRIPTION(DRIVER_DESC);
793 MODULE_LICENSE("GPL");
795 static int __init uss720_init(void)
798 retval = usb_register(&uss720_driver);
802 info(DRIVER_VERSION ":" DRIVER_DESC);
803 info("NOTE: this is a special purpose driver to allow nonstandard");
804 info("protocols (eg. bitbang) over USS720 usb to parallel cables");
805 info("If you just want to connect to a printer, use usblp instead");
810 static void __exit uss720_cleanup(void)
812 usb_deregister(&uss720_driver);
815 module_init(uss720_init);
816 module_exit(uss720_cleanup);
818 /* --------------------------------------------------------------------- */