GNU Linux-libre 4.14.313-gnu1
[releases.git] / drivers / usb / misc / uss720.c
1 /*****************************************************************************/
2
3 /*
4  *      uss720.c  --  USS720 USB Parport Cable.
5  *
6  *      Copyright (C) 1999, 2005, 2010
7  *          Thomas Sailer (t.sailer@alumni.ethz.ch)
8  *
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.
13  *
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.
18  *
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.
22  *
23  *  Based on parport_pc.c
24  *
25  *  History:
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
39  *
40  */
41
42 /*****************************************************************************/
43
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>
54
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"
57
58 /* --------------------------------------------------------------------- */
59
60 struct parport_uss720_private {
61         struct usb_device *usbdev;
62         struct parport *pp;
63         struct kref ref_count;
64         __u8 reg[7];  /* USB registers */
65         struct list_head asynclist;
66         spinlock_t asynclock;
67 };
68
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;
74         struct urb *urb;
75         struct usb_ctrlrequest *dr;
76         __u8 reg[7];
77 };
78
79 /* --------------------------------------------------------------------- */
80
81 static void destroy_priv(struct kref *kref)
82 {
83         struct parport_uss720_private *priv = container_of(kref, struct parport_uss720_private, ref_count);
84
85         dev_dbg(&priv->usbdev->dev, "destroying priv datastructure\n");
86         usb_put_dev(priv->usbdev);
87         priv->usbdev = NULL;
88         kfree(priv);
89 }
90
91 static void destroy_async(struct kref *kref)
92 {
93         struct uss720_async_request *rq = container_of(kref, struct uss720_async_request, ref_count);
94         struct parport_uss720_private *priv = rq->priv;
95         unsigned long flags;
96
97         if (likely(rq->urb))
98                 usb_free_urb(rq->urb);
99         kfree(rq->dr);
100         spin_lock_irqsave(&priv->asynclock, flags);
101         list_del_init(&rq->asynclist);
102         spin_unlock_irqrestore(&priv->asynclock, flags);
103         kfree(rq);
104         kref_put(&priv->ref_count, destroy_priv);
105 }
106
107 /* --------------------------------------------------------------------- */
108
109 static void async_complete(struct urb *urb)
110 {
111         struct uss720_async_request *rq;
112         struct parport *pp;
113         struct parport_uss720_private *priv;
114         int status = urb->status;
115
116         rq = urb->context;
117         priv = rq->priv;
118         pp = priv->pp;
119         if (status) {
120                 dev_err(&urb->dev->dev, "async_complete: urb error %d\n",
121                         status);
122         } else if (rq->dr->bRequest == 3) {
123                 memcpy(priv->reg, rq->reg, sizeof(priv->reg));
124 #if 0
125                 dev_dbg(&priv->usbdev->dev, "async_complete regs %7ph\n",
126                         priv->reg);
127 #endif
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);
131         }
132         complete(&rq->compl);
133         kref_put(&rq->ref_count, destroy_async);
134 }
135
136 static struct uss720_async_request *submit_async_request(struct parport_uss720_private *priv,
137                                                          __u8 request, __u8 requesttype, __u16 value, __u16 index,
138                                                          gfp_t mem_flags)
139 {
140         struct usb_device *usbdev;
141         struct uss720_async_request *rq;
142         unsigned long flags;
143         int ret;
144
145         if (!priv)
146                 return NULL;
147         usbdev = priv->usbdev;
148         if (!usbdev)
149                 return NULL;
150         rq = kzalloc(sizeof(struct uss720_async_request), mem_flags);
151         if (!rq)
152                 return NULL;
153         kref_init(&rq->ref_count);
154         INIT_LIST_HEAD(&rq->asynclist);
155         init_completion(&rq->compl);
156         kref_get(&priv->ref_count);
157         rq->priv = priv;
158         rq->urb = usb_alloc_urb(0, mem_flags);
159         if (!rq->urb) {
160                 kref_put(&rq->ref_count, destroy_async);
161                 return NULL;
162         }
163         rq->dr = kmalloc(sizeof(*rq->dr), mem_flags);
164         if (!rq->dr) {
165                 kref_put(&rq->ref_count, destroy_async);
166                 return NULL;
167         }
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);
182         if (!ret)
183                 return rq;
184         destroy_async(&rq->ref_count);
185         dev_err(&usbdev->dev, "submit_async_request submit_urb failed with %d\n", ret);
186         return NULL;
187 }
188
189 static unsigned int kill_all_async_requests_priv(struct parport_uss720_private *priv)
190 {
191         struct uss720_async_request *rq;
192         unsigned long flags;
193         unsigned int ret = 0;
194
195         spin_lock_irqsave(&priv->asynclock, flags);
196         list_for_each_entry(rq, &priv->asynclist, asynclist) {
197                 usb_unlink_urb(rq->urb);
198                 ret++;
199         }
200         spin_unlock_irqrestore(&priv->asynclock, flags);
201         return ret;
202 }
203
204 /* --------------------------------------------------------------------- */
205
206 static int get_1284_register(struct parport *pp, unsigned char reg, unsigned char *val, gfp_t mem_flags)
207 {
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
212         };
213         int ret;
214
215         if (!pp)
216                 return -EIO;
217         priv = pp->private_data;
218         rq = submit_async_request(priv, 3, 0xc0, ((unsigned int)reg) << 8, 0, mem_flags);
219         if (!rq) {
220                 dev_err(&priv->usbdev->dev, "get_1284_register(%u) failed",
221                         (unsigned int)reg);
222                 return -EIO;
223         }
224         if (!val) {
225                 kref_put(&rq->ref_count, destroy_async);
226                 return 0;
227         }
228         if (wait_for_completion_timeout(&rq->compl, HZ)) {
229                 ret = rq->urb->status;
230                 *val = priv->reg[(reg >= 9) ? 0 : regindex[reg]];
231                 if (ret)
232                         printk(KERN_WARNING "get_1284_register: "
233                                "usb error %d\n", ret);
234                 kref_put(&rq->ref_count, destroy_async);
235                 return ret;
236         }
237         printk(KERN_WARNING "get_1284_register timeout\n");
238         kill_all_async_requests_priv(priv);
239         return -EIO;
240 }
241
242 static int set_1284_register(struct parport *pp, unsigned char reg, unsigned char val, gfp_t mem_flags)
243 {
244         struct parport_uss720_private *priv;
245         struct uss720_async_request *rq;
246
247         if (!pp)
248                 return -EIO;
249         priv = pp->private_data;
250         rq = submit_async_request(priv, 4, 0x40, (((unsigned int)reg) << 8) | val, 0, mem_flags);
251         if (!rq) {
252                 dev_err(&priv->usbdev->dev, "set_1284_register(%u,%u) failed",
253                         (unsigned int)reg, (unsigned int)val);
254                 return -EIO;
255         }
256         kref_put(&rq->ref_count, destroy_async);
257         return 0;
258 }
259
260 /* --------------------------------------------------------------------- */
261
262 /* ECR modes */
263 #define ECR_SPP 00
264 #define ECR_PS2 01
265 #define ECR_PPF 02
266 #define ECR_ECP 03
267 #define ECR_EPP 04
268
269 /* Safely change the mode bits in the ECR */
270 static int change_mode(struct parport *pp, int m)
271 {
272         struct parport_uss720_private *priv = pp->private_data;
273         int mode;
274         __u8 reg;
275
276         if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
277                 return -EIO;
278         /* Bits <7:5> contain the mode. */
279         mode = (priv->reg[2] >> 5) & 0x7;
280         if (mode == m)
281                 return 0;
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))
285                         return -EIO;
286
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;
291                 switch (mode) {
292                 case ECR_PPF: /* Parallel Port FIFO mode */
293                 case ECR_ECP: /* ECP Parallel Port mode */
294                         /* Poll slowly. */
295                         for (;;) {
296                                 if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
297                                         return -EIO;
298                                 if (priv->reg[2] & 0x01)
299                                         break;
300                                 if (time_after_eq (jiffies, expire))
301                                         /* The FIFO is stuck. */
302                                         return -EBUSY;
303                                 msleep_interruptible(10);
304                                 if (signal_pending (current))
305                                         break;
306                         }
307                 }
308         }
309         /* Set the mode. */
310         if (set_1284_register(pp, 6, m << 5, GFP_KERNEL))
311                 return -EIO;
312         if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
313                 return -EIO;
314         return 0;
315 }
316
317 /*
318  * Clear TIMEOUT BIT in EPP MODE
319  */
320 static int clear_epp_timeout(struct parport *pp)
321 {
322         unsigned char stat;
323
324         if (get_1284_register(pp, 1, &stat, GFP_KERNEL))
325                 return 1;
326         return stat & 1;
327 }
328
329 /*
330  * Access functions.
331  */
332 #if 0
333 static int uss720_irq(int usbstatus, void *buffer, int len, void *dev_id)
334 {
335         struct parport *pp = (struct parport *)dev_id;
336         struct parport_uss720_private *priv = pp->private_data; 
337
338         if (usbstatus != 0 || len < 4 || !buffer)
339                 return 1;
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);
344         return 1;
345 }
346 #endif
347
348 static void parport_uss720_write_data(struct parport *pp, unsigned char d)
349 {
350         set_1284_register(pp, 0, d, GFP_KERNEL);
351 }
352
353 static unsigned char parport_uss720_read_data(struct parport *pp)
354 {
355         unsigned char ret;
356
357         if (get_1284_register(pp, 0, &ret, GFP_KERNEL))
358                 return 0;
359         return ret;
360 }
361
362 static void parport_uss720_write_control(struct parport *pp, unsigned char d)
363 {
364         struct parport_uss720_private *priv = pp->private_data; 
365
366         d = (d & 0xf) | (priv->reg[1] & 0xf0);
367         if (set_1284_register(pp, 2, d, GFP_KERNEL))
368                 return;
369         priv->reg[1] = d;
370 }
371
372 static unsigned char parport_uss720_read_control(struct parport *pp)
373 {
374         struct parport_uss720_private *priv = pp->private_data; 
375         return priv->reg[1] & 0xf; /* Use soft copy */
376 }
377
378 static unsigned char parport_uss720_frob_control(struct parport *pp, unsigned char mask, unsigned char val)
379 {
380         struct parport_uss720_private *priv = pp->private_data; 
381         unsigned char d;
382
383         mask &= 0x0f;
384         val &= 0x0f;
385         d = (priv->reg[1] & (~mask)) ^ val;
386         if (set_1284_register(pp, 2, d, GFP_ATOMIC))
387                 return 0;
388         priv->reg[1] = d;
389         return d & 0xf;
390 }
391
392 static unsigned char parport_uss720_read_status(struct parport *pp)
393 {
394         unsigned char ret;
395
396         if (get_1284_register(pp, 1, &ret, GFP_ATOMIC))
397                 return 0;
398         return ret & 0xf8;
399 }
400
401 static void parport_uss720_disable_irq(struct parport *pp)
402 {
403         struct parport_uss720_private *priv = pp->private_data; 
404         unsigned char d;
405
406         d = priv->reg[1] & ~0x10;
407         if (set_1284_register(pp, 2, d, GFP_KERNEL))
408                 return;
409         priv->reg[1] = d;
410 }
411
412 static void parport_uss720_enable_irq(struct parport *pp)
413 {
414         struct parport_uss720_private *priv = pp->private_data; 
415         unsigned char d;
416
417         d = priv->reg[1] | 0x10;
418         if (set_1284_register(pp, 2, d, GFP_KERNEL))
419                 return;
420         priv->reg[1] = d;
421 }
422
423 static void parport_uss720_data_forward (struct parport *pp)
424 {
425         struct parport_uss720_private *priv = pp->private_data; 
426         unsigned char d;
427
428         d = priv->reg[1] & ~0x20;
429         if (set_1284_register(pp, 2, d, GFP_KERNEL))
430                 return;
431         priv->reg[1] = d;
432 }
433
434 static void parport_uss720_data_reverse (struct parport *pp)
435 {
436         struct parport_uss720_private *priv = pp->private_data; 
437         unsigned char d;
438
439         d = priv->reg[1] | 0x20;
440         if (set_1284_register(pp, 2, d, GFP_KERNEL))
441                 return;
442         priv->reg[1] = d;
443 }
444
445 static void parport_uss720_init_state(struct pardevice *dev, struct parport_state *s)
446 {
447         s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
448         s->u.pc.ecr = 0x24;
449 }
450
451 static void parport_uss720_save_state(struct parport *pp, struct parport_state *s)
452 {
453         struct parport_uss720_private *priv = pp->private_data; 
454
455 #if 0
456         if (get_1284_register(pp, 2, NULL, GFP_ATOMIC))
457                 return;
458 #endif
459         s->u.pc.ctr = priv->reg[1];
460         s->u.pc.ecr = priv->reg[2];
461 }
462
463 static void parport_uss720_restore_state(struct parport *pp, struct parport_state *s)
464 {
465         struct parport_uss720_private *priv = pp->private_data;
466
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;
472 }
473
474 static size_t parport_uss720_epp_read_data(struct parport *pp, void *buf, size_t length, int flags)
475 {
476         struct parport_uss720_private *priv = pp->private_data; 
477         size_t got = 0;
478
479         if (change_mode(pp, ECR_EPP))
480                 return 0;
481         for (; got < length; got++) {
482                 if (get_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
483                         break;
484                 buf++;
485                 if (priv->reg[0] & 0x01) {
486                         clear_epp_timeout(pp);
487                         break;
488                 }
489         }
490         change_mode(pp, ECR_PS2);
491         return got;
492 }
493
494 static size_t parport_uss720_epp_write_data(struct parport *pp, const void *buf, size_t length, int flags)
495 {
496 #if 0
497         struct parport_uss720_private *priv = pp->private_data; 
498         size_t written = 0;
499
500         if (change_mode(pp, ECR_EPP))
501                 return 0;
502         for (; written < length; written++) {
503                 if (set_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
504                         break;
505                 ((char*)buf)++;
506                 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
507                         break;
508                 if (priv->reg[0] & 0x01) {
509                         clear_epp_timeout(pp);
510                         break;
511                 }
512         }
513         change_mode(pp, ECR_PS2);
514         return written;
515 #else
516         struct parport_uss720_private *priv = pp->private_data;
517         struct usb_device *usbdev = priv->usbdev;
518         int rlen;
519         int i;
520
521         if (!usbdev)
522                 return 0;
523         if (change_mode(pp, ECR_EPP))
524                 return 0;
525         i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buf, length, &rlen, 20000);
526         if (i)
527                 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %zu rlen %u\n", buf, length, rlen);
528         change_mode(pp, ECR_PS2);
529         return rlen;
530 #endif
531 }
532
533 static size_t parport_uss720_epp_read_addr(struct parport *pp, void *buf, size_t length, int flags)
534 {
535         struct parport_uss720_private *priv = pp->private_data; 
536         size_t got = 0;
537
538         if (change_mode(pp, ECR_EPP))
539                 return 0;
540         for (; got < length; got++) {
541                 if (get_1284_register(pp, 3, (char *)buf, GFP_KERNEL))
542                         break;
543                 buf++;
544                 if (priv->reg[0] & 0x01) {
545                         clear_epp_timeout(pp);
546                         break;
547                 }
548         }
549         change_mode(pp, ECR_PS2);
550         return got;
551 }
552
553 static size_t parport_uss720_epp_write_addr(struct parport *pp, const void *buf, size_t length, int flags)
554 {
555         struct parport_uss720_private *priv = pp->private_data; 
556         size_t written = 0;
557
558         if (change_mode(pp, ECR_EPP))
559                 return 0;
560         for (; written < length; written++) {
561                 if (set_1284_register(pp, 3, *(char *)buf, GFP_KERNEL))
562                         break;
563                 buf++;
564                 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
565                         break;
566                 if (priv->reg[0] & 0x01) {
567                         clear_epp_timeout(pp);
568                         break;
569                 }
570         }
571         change_mode(pp, ECR_PS2);
572         return written;
573 }
574
575 static size_t parport_uss720_ecp_write_data(struct parport *pp, const void *buffer, size_t len, int flags)
576 {
577         struct parport_uss720_private *priv = pp->private_data;
578         struct usb_device *usbdev = priv->usbdev;
579         int rlen;
580         int i;
581
582         if (!usbdev)
583                 return 0;
584         if (change_mode(pp, ECR_ECP))
585                 return 0;
586         i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
587         if (i)
588                 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %zu rlen %u\n", buffer, len, rlen);
589         change_mode(pp, ECR_PS2);
590         return rlen;
591 }
592
593 static size_t parport_uss720_ecp_read_data(struct parport *pp, void *buffer, size_t len, int flags)
594 {
595         struct parport_uss720_private *priv = pp->private_data;
596         struct usb_device *usbdev = priv->usbdev;
597         int rlen;
598         int i;
599
600         if (!usbdev)
601                 return 0;
602         if (change_mode(pp, ECR_ECP))
603                 return 0;
604         i = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), buffer, len, &rlen, 20000);
605         if (i)
606                 printk(KERN_ERR "uss720: recvbulk ep 2 buf %p len %zu rlen %u\n", buffer, len, rlen);
607         change_mode(pp, ECR_PS2);
608         return rlen;
609 }
610
611 static size_t parport_uss720_ecp_write_addr(struct parport *pp, const void *buffer, size_t len, int flags)
612 {
613         size_t written = 0;
614
615         if (change_mode(pp, ECR_ECP))
616                 return 0;
617         for (; written < len; written++) {
618                 if (set_1284_register(pp, 5, *(char *)buffer, GFP_KERNEL))
619                         break;
620                 buffer++;
621         }
622         change_mode(pp, ECR_PS2);
623         return written;
624 }
625
626 static size_t parport_uss720_write_compat(struct parport *pp, const void *buffer, size_t len, int flags)
627 {
628         struct parport_uss720_private *priv = pp->private_data;
629         struct usb_device *usbdev = priv->usbdev;
630         int rlen;
631         int i;
632
633         if (!usbdev)
634                 return 0;
635         if (change_mode(pp, ECR_PPF))
636                 return 0;
637         i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
638         if (i)
639                 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %zu rlen %u\n", buffer, len, rlen);
640         change_mode(pp, ECR_PS2);
641         return rlen;
642 }
643
644 /* --------------------------------------------------------------------- */
645
646 static struct parport_operations parport_uss720_ops = 
647 {
648         .owner =                THIS_MODULE,
649         .write_data =           parport_uss720_write_data,
650         .read_data =            parport_uss720_read_data,
651
652         .write_control =        parport_uss720_write_control,
653         .read_control =         parport_uss720_read_control,
654         .frob_control =         parport_uss720_frob_control,
655
656         .read_status =          parport_uss720_read_status,
657
658         .enable_irq =           parport_uss720_enable_irq,
659         .disable_irq =          parport_uss720_disable_irq,
660
661         .data_forward =         parport_uss720_data_forward,
662         .data_reverse =         parport_uss720_data_reverse,
663
664         .init_state =           parport_uss720_init_state,
665         .save_state =           parport_uss720_save_state,
666         .restore_state =        parport_uss720_restore_state,
667
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,
672
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,
676
677         .compat_write_data =    parport_uss720_write_compat,
678         .nibble_read_data =     parport_ieee1284_read_nibble,
679         .byte_read_data =       parport_ieee1284_read_byte,
680 };
681
682 /* --------------------------------------------------------------------- */
683
684 static int uss720_probe(struct usb_interface *intf,
685                         const struct usb_device_id *id)
686 {
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;
691         struct parport *pp;
692         unsigned char reg;
693         int i;
694
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));
698
699         /* our known interfaces have 3 alternate settings */
700         if (intf->num_altsetting != 3) {
701                 usb_put_dev(usbdev);
702                 return -ENODEV;
703         }
704         i = usb_set_interface(usbdev, intf->altsetting->desc.bInterfaceNumber, 2);
705         dev_dbg(&intf->dev, "set interface result %d\n", i);
706
707         interface = intf->cur_altsetting;
708
709         if (interface->desc.bNumEndpoints < 3) {
710                 usb_put_dev(usbdev);
711                 return -ENODEV;
712         }
713
714         /*
715          * Allocate parport interface 
716          */
717         priv = kzalloc(sizeof(struct parport_uss720_private), GFP_KERNEL);
718         if (!priv) {
719                 usb_put_dev(usbdev);
720                 return -ENOMEM;
721         }
722         priv->pp = NULL;
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);
728         if (!pp) {
729                 printk(KERN_WARNING "uss720: could not register parport\n");
730                 goto probe_abort;
731         }
732
733         priv->pp = pp;
734         pp->private_data = priv;
735         pp->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_TRISTATE | PARPORT_MODE_EPP | PARPORT_MODE_ECP | PARPORT_MODE_COMPAT;
736
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);
741         /* debugging */
742         get_1284_register(pp, 0, &reg, GFP_KERNEL);
743         dev_dbg(&intf->dev, "reg: %7ph\n", priv->reg);
744
745         i = usb_find_last_int_in_endpoint(interface, &epd);
746         if (!i) {
747                 dev_dbg(&intf->dev, "epaddr %d interval %d\n",
748                                 epd->bEndpointAddress, epd->bInterval);
749         }
750         parport_announce_port(pp);
751
752         usb_set_intfdata(intf, pp);
753         return 0;
754
755 probe_abort:
756         kill_all_async_requests_priv(priv);
757         kref_put(&priv->ref_count, destroy_priv);
758         return -ENODEV;
759 }
760
761 static void uss720_disconnect(struct usb_interface *intf)
762 {
763         struct parport *pp = usb_get_intfdata(intf);
764         struct parport_uss720_private *priv;
765         struct usb_device *usbdev;
766
767         dev_dbg(&intf->dev, "disconnect\n");
768         usb_set_intfdata(intf, NULL);
769         if (pp) {
770                 priv = pp->private_data;
771                 usbdev = priv->usbdev;
772                 priv->pp = NULL;
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);
778         }
779         dev_dbg(&intf->dev, "disconnect done\n");
780 }
781
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 */
790 };
791
792 MODULE_DEVICE_TABLE (usb, uss720_table);
793
794
795 static struct usb_driver uss720_driver = {
796         .name =         "uss720",
797         .probe =        uss720_probe,
798         .disconnect =   uss720_disconnect,
799         .id_table =     uss720_table,
800 };
801
802 /* --------------------------------------------------------------------- */
803
804 MODULE_AUTHOR(DRIVER_AUTHOR);
805 MODULE_DESCRIPTION(DRIVER_DESC);
806 MODULE_LICENSE("GPL");
807
808 static int __init uss720_init(void)
809 {
810         int retval;
811         retval = usb_register(&uss720_driver);
812         if (retval)
813                 goto out;
814
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");
822 out:
823         return retval;
824 }
825
826 static void __exit uss720_cleanup(void)
827 {
828         usb_deregister(&uss720_driver);
829 }
830
831 module_init(uss720_init);
832 module_exit(uss720_cleanup);
833
834 /* --------------------------------------------------------------------- */