blob: 7f64147ea22902bb4a33db6597f2ec7af629c5a4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************/
2
3/*
4 * uss720.c -- USS720 USB Parport Cable.
5 *
Thomas Sailerecc16242010-12-14 16:04:05 +01006 * Copyright (C) 1999, 2005, 2010
Thomas Sailer0f361632005-09-09 10:43:50 +02007 * Thomas Sailer (t.sailer@alumni.ethz.ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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:
Thomas Sailer0f361632005-09-09 10:43:50 +020026 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 *
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>
Thomas Sailer0f361632005-09-09 10:43:50 +020050#include <linux/completion.h>
51#include <linux/kref.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*
55 * Version Information
56 */
Thomas Sailer0f361632005-09-09 10:43:50 +020057#define DRIVER_VERSION "v0.6"
58#define DRIVER_AUTHOR "Thomas M. Sailer, t.sailer@alumni.ethz.ch"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define DRIVER_DESC "USB Parport Cable driver for Cables using the Lucent Technologies USS720 Chip"
60
61/* --------------------------------------------------------------------- */
62
63struct parport_uss720_private {
64 struct usb_device *usbdev;
Thomas Sailer0f361632005-09-09 10:43:50 +020065 struct parport *pp;
66 struct kref ref_count;
67 __u8 reg[7]; /* USB registers */
68 struct list_head asynclist;
69 spinlock_t asynclock;
70};
71
72struct uss720_async_request {
73 struct parport_uss720_private *priv;
74 struct kref ref_count;
75 struct list_head asynclist;
76 struct completion compl;
77 struct urb *urb;
78 struct usb_ctrlrequest dr;
79 __u8 reg[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
82/* --------------------------------------------------------------------- */
83
Thomas Sailer0f361632005-09-09 10:43:50 +020084static void destroy_priv(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Thomas Sailer0f361632005-09-09 10:43:50 +020086 struct parport_uss720_private *priv = container_of(kref, struct parport_uss720_private, ref_count);
87
88 usb_put_dev(priv->usbdev);
89 kfree(priv);
90 dbg("destroying priv datastructure");
91}
92
93static void destroy_async(struct kref *kref)
94{
95 struct uss720_async_request *rq = container_of(kref, struct uss720_async_request, ref_count);
96 struct parport_uss720_private *priv = rq->priv;
97 unsigned long flags;
98
99 if (likely(rq->urb))
100 usb_free_urb(rq->urb);
101 spin_lock_irqsave(&priv->asynclock, flags);
102 list_del_init(&rq->asynclist);
103 spin_unlock_irqrestore(&priv->asynclock, flags);
104 kfree(rq);
105 kref_put(&priv->ref_count, destroy_priv);
106}
107
108/* --------------------------------------------------------------------- */
109
David Howells7d12e782006-10-05 14:55:46 +0100110static void async_complete(struct urb *urb)
Thomas Sailer0f361632005-09-09 10:43:50 +0200111{
112 struct uss720_async_request *rq;
113 struct parport *pp;
114 struct parport_uss720_private *priv;
Greg Kroah-Hartman82210d32007-07-18 10:58:02 -0700115 int status = urb->status;
Thomas Sailer0f361632005-09-09 10:43:50 +0200116
117 rq = urb->context;
118 priv = rq->priv;
119 pp = priv->pp;
Greg Kroah-Hartman82210d32007-07-18 10:58:02 -0700120 if (status) {
Greg Kroah-Hartmand2b1ff72012-04-20 16:53:53 -0700121 dev_err(&urb->dev->dev, "async_complete: urb error %d\n",
122 status);
Thomas Sailer0f361632005-09-09 10:43:50 +0200123 } else if (rq->dr.bRequest == 3) {
124 memcpy(priv->reg, rq->reg, sizeof(priv->reg));
125#if 0
126 dbg("async_complete regs %02x %02x %02x %02x %02x %02x %02x",
127 (unsigned int)priv->reg[0], (unsigned int)priv->reg[1], (unsigned int)priv->reg[2],
128 (unsigned int)priv->reg[3], (unsigned int)priv->reg[4], (unsigned int)priv->reg[5],
129 (unsigned int)priv->reg[6]);
130#endif
131 /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
132 if (rq->reg[2] & rq->reg[1] & 0x10 && pp)
Jeff Garzikf230d102007-10-19 01:56:02 -0400133 parport_generic_irq(pp);
Thomas Sailer0f361632005-09-09 10:43:50 +0200134 }
135 complete(&rq->compl);
136 kref_put(&rq->ref_count, destroy_async);
137}
138
139static struct uss720_async_request *submit_async_request(struct parport_uss720_private *priv,
140 __u8 request, __u8 requesttype, __u16 value, __u16 index,
Al Viro55016f12005-10-21 03:21:58 -0400141 gfp_t mem_flags)
Thomas Sailer0f361632005-09-09 10:43:50 +0200142{
143 struct usb_device *usbdev;
144 struct uss720_async_request *rq;
145 unsigned long flags;
146 int ret;
147
148 if (!priv)
149 return NULL;
150 usbdev = priv->usbdev;
151 if (!usbdev)
152 return NULL;
153 rq = kmalloc(sizeof(struct uss720_async_request), mem_flags);
154 if (!rq) {
Greg Kroah-Hartmand2b1ff72012-04-20 16:53:53 -0700155 dev_err(&usbdev->dev, "submit_async_request out of memory\n");
Thomas Sailer0f361632005-09-09 10:43:50 +0200156 return NULL;
157 }
158 kref_init(&rq->ref_count);
159 INIT_LIST_HEAD(&rq->asynclist);
160 init_completion(&rq->compl);
161 kref_get(&priv->ref_count);
162 rq->priv = priv;
163 rq->urb = usb_alloc_urb(0, mem_flags);
164 if (!rq->urb) {
165 kref_put(&rq->ref_count, destroy_async);
Greg Kroah-Hartmand2b1ff72012-04-20 16:53:53 -0700166 dev_err(&usbdev->dev, "submit_async_request out of memory\n");
Thomas Sailer0f361632005-09-09 10:43:50 +0200167 return NULL;
168 }
169 rq->dr.bRequestType = requesttype;
170 rq->dr.bRequest = request;
171 rq->dr.wValue = cpu_to_le16(value);
172 rq->dr.wIndex = cpu_to_le16(index);
173 rq->dr.wLength = cpu_to_le16((request == 3) ? sizeof(rq->reg) : 0);
174 usb_fill_control_urb(rq->urb, usbdev, (requesttype & 0x80) ? usb_rcvctrlpipe(usbdev, 0) : usb_sndctrlpipe(usbdev, 0),
175 (unsigned char *)&rq->dr,
176 (request == 3) ? rq->reg : NULL, (request == 3) ? sizeof(rq->reg) : 0, async_complete, rq);
177 /* rq->urb->transfer_flags |= URB_ASYNC_UNLINK; */
178 spin_lock_irqsave(&priv->asynclock, flags);
179 list_add_tail(&rq->asynclist, &priv->asynclist);
180 spin_unlock_irqrestore(&priv->asynclock, flags);
Peter Holikadaa3c62011-03-18 18:47:44 +0100181 kref_get(&rq->ref_count);
Thomas Sailer0f361632005-09-09 10:43:50 +0200182 ret = usb_submit_urb(rq->urb, mem_flags);
Peter Holikadaa3c62011-03-18 18:47:44 +0100183 if (!ret)
Thomas Sailer0f361632005-09-09 10:43:50 +0200184 return rq;
Peter Holikadaa3c62011-03-18 18:47:44 +0100185 destroy_async(&rq->ref_count);
Greg Kroah-Hartmand2b1ff72012-04-20 16:53:53 -0700186 dev_err(&usbdev->dev, "submit_async_request submit_urb failed with %d\n", ret);
Thomas Sailer0f361632005-09-09 10:43:50 +0200187 return NULL;
188}
189
190static unsigned int kill_all_async_requests_priv(struct parport_uss720_private *priv)
191{
192 struct uss720_async_request *rq;
193 unsigned long flags;
194 unsigned int ret = 0;
195
196 spin_lock_irqsave(&priv->asynclock, flags);
197 list_for_each_entry(rq, &priv->asynclist, asynclist) {
198 usb_unlink_urb(rq->urb);
199 ret++;
200 }
201 spin_unlock_irqrestore(&priv->asynclock, flags);
202 return ret;
203}
204
205/* --------------------------------------------------------------------- */
206
Al Viro55016f12005-10-21 03:21:58 -0400207static int get_1284_register(struct parport *pp, unsigned char reg, unsigned char *val, gfp_t mem_flags)
Thomas Sailer0f361632005-09-09 10:43:50 +0200208{
209 struct parport_uss720_private *priv;
210 struct uss720_async_request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 static const unsigned char regindex[9] = {
212 4, 0, 1, 5, 5, 0, 2, 3, 6
213 };
214 int ret;
215
Thomas Sailer0f361632005-09-09 10:43:50 +0200216 if (!pp)
217 return -EIO;
218 priv = pp->private_data;
219 rq = submit_async_request(priv, 3, 0xc0, ((unsigned int)reg) << 8, 0, mem_flags);
220 if (!rq) {
Greg Kroah-Hartmand2b1ff72012-04-20 16:53:53 -0700221 dev_err(&priv->usbdev->dev, "get_1284_register(%u) failed",
222 (unsigned int)reg);
Thomas Sailer0f361632005-09-09 10:43:50 +0200223 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Thomas Sailer0f361632005-09-09 10:43:50 +0200225 if (!val) {
226 kref_put(&rq->ref_count, destroy_async);
227 return 0;
228 }
229 if (wait_for_completion_timeout(&rq->compl, HZ)) {
230 ret = rq->urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 *val = priv->reg[(reg >= 9) ? 0 : regindex[reg]];
Thomas Sailer0f361632005-09-09 10:43:50 +0200232 if (ret)
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700233 printk(KERN_WARNING "get_1284_register: "
234 "usb error %d\n", ret);
Thomas Sailer0f361632005-09-09 10:43:50 +0200235 kref_put(&rq->ref_count, destroy_async);
236 return ret;
237 }
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700238 printk(KERN_WARNING "get_1284_register timeout\n");
Thomas Sailer0f361632005-09-09 10:43:50 +0200239 kill_all_async_requests_priv(priv);
240 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Al Viro55016f12005-10-21 03:21:58 -0400243static int set_1284_register(struct parport *pp, unsigned char reg, unsigned char val, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Thomas Sailer0f361632005-09-09 10:43:50 +0200245 struct parport_uss720_private *priv;
246 struct uss720_async_request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Thomas Sailer0f361632005-09-09 10:43:50 +0200248 if (!pp)
249 return -EIO;
250 priv = pp->private_data;
251 rq = submit_async_request(priv, 4, 0x40, (((unsigned int)reg) << 8) | val, 0, mem_flags);
252 if (!rq) {
Greg Kroah-Hartmand2b1ff72012-04-20 16:53:53 -0700253 dev_err(&priv->usbdev->dev, "set_1284_register(%u,%u) failed",
254 (unsigned int)reg, (unsigned int)val);
Thomas Sailer0f361632005-09-09 10:43:50 +0200255 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Thomas Sailer0f361632005-09-09 10:43:50 +0200257 kref_put(&rq->ref_count, destroy_async);
258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/* --------------------------------------------------------------------- */
262
263/* ECR modes */
264#define ECR_SPP 00
265#define ECR_PS2 01
266#define ECR_PPF 02
267#define ECR_ECP 03
268#define ECR_EPP 04
269
270/* Safely change the mode bits in the ECR */
271static int change_mode(struct parport *pp, int m)
272{
273 struct parport_uss720_private *priv = pp->private_data;
274 int mode;
Thomas Sailer0f361632005-09-09 10:43:50 +0200275 __u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Thomas Sailer0f361632005-09-09 10:43:50 +0200277 if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return -EIO;
279 /* Bits <7:5> contain the mode. */
280 mode = (priv->reg[2] >> 5) & 0x7;
281 if (mode == m)
282 return 0;
283 /* We have to go through mode 000 or 001 */
284 if (mode > ECR_PS2 && m > ECR_PS2)
285 if (change_mode(pp, ECR_PS2))
286 return -EIO;
287
288 if (m <= ECR_PS2 && !(priv->reg[1] & 0x20)) {
289 /* This mode resets the FIFO, so we may
290 * have to wait for it to drain first. */
291 unsigned long expire = jiffies + pp->physport->cad->timeout;
292 switch (mode) {
293 case ECR_PPF: /* Parallel Port FIFO mode */
294 case ECR_ECP: /* ECP Parallel Port mode */
295 /* Poll slowly. */
296 for (;;) {
Thomas Sailer0f361632005-09-09 10:43:50 +0200297 if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return -EIO;
299 if (priv->reg[2] & 0x01)
300 break;
301 if (time_after_eq (jiffies, expire))
302 /* The FIFO is stuck. */
303 return -EBUSY;
304 msleep_interruptible(10);
305 if (signal_pending (current))
306 break;
307 }
308 }
309 }
310 /* Set the mode. */
Thomas Sailer0f361632005-09-09 10:43:50 +0200311 if (set_1284_register(pp, 6, m << 5, GFP_KERNEL))
312 return -EIO;
313 if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return -EIO;
315 return 0;
316}
317
318/*
319 * Clear TIMEOUT BIT in EPP MODE
320 */
321static int clear_epp_timeout(struct parport *pp)
322{
323 unsigned char stat;
324
Thomas Sailer0f361632005-09-09 10:43:50 +0200325 if (get_1284_register(pp, 1, &stat, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return 1;
327 return stat & 1;
328}
329
330/*
331 * Access functions.
332 */
333#if 0
334static int uss720_irq(int usbstatus, void *buffer, int len, void *dev_id)
335{
336 struct parport *pp = (struct parport *)dev_id;
337 struct parport_uss720_private *priv = pp->private_data;
338
339 if (usbstatus != 0 || len < 4 || !buffer)
340 return 1;
341 memcpy(priv->reg, buffer, 4);
342 /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */
343 if (priv->reg[2] & priv->reg[1] & 0x10)
Jeff Garzikf230d102007-10-19 01:56:02 -0400344 parport_generic_irq(pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 1;
346}
347#endif
348
349static void parport_uss720_write_data(struct parport *pp, unsigned char d)
350{
Thomas Sailer0f361632005-09-09 10:43:50 +0200351 set_1284_register(pp, 0, d, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354static unsigned char parport_uss720_read_data(struct parport *pp)
355{
356 unsigned char ret;
357
Thomas Sailer0f361632005-09-09 10:43:50 +0200358 if (get_1284_register(pp, 0, &ret, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return 0;
360 return ret;
361}
362
363static void parport_uss720_write_control(struct parport *pp, unsigned char d)
364{
365 struct parport_uss720_private *priv = pp->private_data;
366
367 d = (d & 0xf) | (priv->reg[1] & 0xf0);
Thomas Sailer0f361632005-09-09 10:43:50 +0200368 if (set_1284_register(pp, 2, d, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return;
370 priv->reg[1] = d;
371}
372
373static unsigned char parport_uss720_read_control(struct parport *pp)
374{
375 struct parport_uss720_private *priv = pp->private_data;
376 return priv->reg[1] & 0xf; /* Use soft copy */
377}
378
379static unsigned char parport_uss720_frob_control(struct parport *pp, unsigned char mask, unsigned char val)
380{
381 struct parport_uss720_private *priv = pp->private_data;
382 unsigned char d;
383
384 mask &= 0x0f;
385 val &= 0x0f;
386 d = (priv->reg[1] & (~mask)) ^ val;
Thomas Sailer0f361632005-09-09 10:43:50 +0200387 if (set_1284_register(pp, 2, d, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return 0;
389 priv->reg[1] = d;
390 return d & 0xf;
391}
392
393static unsigned char parport_uss720_read_status(struct parport *pp)
394{
395 unsigned char ret;
396
Thomas Sailer0f361632005-09-09 10:43:50 +0200397 if (get_1284_register(pp, 1, &ret, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 0;
399 return ret & 0xf8;
400}
401
402static void parport_uss720_disable_irq(struct parport *pp)
403{
404 struct parport_uss720_private *priv = pp->private_data;
405 unsigned char d;
406
407 d = priv->reg[1] & ~0x10;
Thomas Sailer0f361632005-09-09 10:43:50 +0200408 if (set_1284_register(pp, 2, d, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return;
410 priv->reg[1] = d;
411}
412
413static void parport_uss720_enable_irq(struct parport *pp)
414{
415 struct parport_uss720_private *priv = pp->private_data;
416 unsigned char d;
417
418 d = priv->reg[1] | 0x10;
Thomas Sailer0f361632005-09-09 10:43:50 +0200419 if (set_1284_register(pp, 2, d, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return;
421 priv->reg[1] = d;
422}
423
424static void parport_uss720_data_forward (struct parport *pp)
425{
426 struct parport_uss720_private *priv = pp->private_data;
427 unsigned char d;
428
429 d = priv->reg[1] & ~0x20;
Thomas Sailer0f361632005-09-09 10:43:50 +0200430 if (set_1284_register(pp, 2, d, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return;
432 priv->reg[1] = d;
433}
434
435static void parport_uss720_data_reverse (struct parport *pp)
436{
437 struct parport_uss720_private *priv = pp->private_data;
438 unsigned char d;
439
440 d = priv->reg[1] | 0x20;
Thomas Sailer0f361632005-09-09 10:43:50 +0200441 if (set_1284_register(pp, 2, d, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return;
443 priv->reg[1] = d;
444}
445
446static void parport_uss720_init_state(struct pardevice *dev, struct parport_state *s)
447{
448 s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
449 s->u.pc.ecr = 0x24;
450}
451
452static void parport_uss720_save_state(struct parport *pp, struct parport_state *s)
453{
454 struct parport_uss720_private *priv = pp->private_data;
455
Thomas Sailer0f361632005-09-09 10:43:50 +0200456#if 0
457 if (get_1284_register(pp, 2, NULL, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return;
Thomas Sailer0f361632005-09-09 10:43:50 +0200459#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 s->u.pc.ctr = priv->reg[1];
461 s->u.pc.ecr = priv->reg[2];
462}
463
464static void parport_uss720_restore_state(struct parport *pp, struct parport_state *s)
465{
Thomas Sailer0f361632005-09-09 10:43:50 +0200466 struct parport_uss720_private *priv = pp->private_data;
467
468 set_1284_register(pp, 2, s->u.pc.ctr, GFP_ATOMIC);
469 set_1284_register(pp, 6, s->u.pc.ecr, GFP_ATOMIC);
470 get_1284_register(pp, 2, NULL, GFP_ATOMIC);
471 priv->reg[1] = s->u.pc.ctr;
472 priv->reg[2] = s->u.pc.ecr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475static size_t parport_uss720_epp_read_data(struct parport *pp, void *buf, size_t length, int flags)
476{
477 struct parport_uss720_private *priv = pp->private_data;
478 size_t got = 0;
479
480 if (change_mode(pp, ECR_EPP))
481 return 0;
482 for (; got < length; got++) {
Thomas Sailer0f361632005-09-09 10:43:50 +0200483 if (get_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 buf++;
486 if (priv->reg[0] & 0x01) {
487 clear_epp_timeout(pp);
488 break;
489 }
490 }
491 change_mode(pp, ECR_PS2);
492 return got;
493}
494
495static size_t parport_uss720_epp_write_data(struct parport *pp, const void *buf, size_t length, int flags)
496{
497#if 0
498 struct parport_uss720_private *priv = pp->private_data;
499 size_t written = 0;
500
501 if (change_mode(pp, ECR_EPP))
502 return 0;
503 for (; written < length; written++) {
Thomas Sailer0f361632005-09-09 10:43:50 +0200504 if (set_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 break;
506 ((char*)buf)++;
Thomas Sailer0f361632005-09-09 10:43:50 +0200507 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 break;
509 if (priv->reg[0] & 0x01) {
510 clear_epp_timeout(pp);
511 break;
512 }
513 }
514 change_mode(pp, ECR_PS2);
515 return written;
516#else
517 struct parport_uss720_private *priv = pp->private_data;
518 struct usb_device *usbdev = priv->usbdev;
519 int rlen;
520 int i;
521
522 if (!usbdev)
523 return 0;
524 if (change_mode(pp, ECR_EPP))
525 return 0;
526 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buf, length, &rlen, 20000);
527 if (i)
528 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buf, length, rlen);
529 change_mode(pp, ECR_PS2);
530 return rlen;
531#endif
532}
533
534static size_t parport_uss720_epp_read_addr(struct parport *pp, void *buf, size_t length, int flags)
535{
536 struct parport_uss720_private *priv = pp->private_data;
537 size_t got = 0;
538
539 if (change_mode(pp, ECR_EPP))
540 return 0;
541 for (; got < length; got++) {
Thomas Sailer0f361632005-09-09 10:43:50 +0200542 if (get_1284_register(pp, 3, (char *)buf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 break;
544 buf++;
545 if (priv->reg[0] & 0x01) {
546 clear_epp_timeout(pp);
547 break;
548 }
549 }
550 change_mode(pp, ECR_PS2);
551 return got;
552}
553
554static size_t parport_uss720_epp_write_addr(struct parport *pp, const void *buf, size_t length, int flags)
555{
556 struct parport_uss720_private *priv = pp->private_data;
557 size_t written = 0;
558
559 if (change_mode(pp, ECR_EPP))
560 return 0;
561 for (; written < length; written++) {
Thomas Sailer0f361632005-09-09 10:43:50 +0200562 if (set_1284_register(pp, 3, *(char *)buf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
564 buf++;
Thomas Sailer0f361632005-09-09 10:43:50 +0200565 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 break;
567 if (priv->reg[0] & 0x01) {
568 clear_epp_timeout(pp);
569 break;
570 }
571 }
572 change_mode(pp, ECR_PS2);
573 return written;
574}
575
576static size_t parport_uss720_ecp_write_data(struct parport *pp, const void *buffer, size_t len, int flags)
577{
578 struct parport_uss720_private *priv = pp->private_data;
579 struct usb_device *usbdev = priv->usbdev;
580 int rlen;
581 int i;
582
583 if (!usbdev)
584 return 0;
585 if (change_mode(pp, ECR_ECP))
586 return 0;
587 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
588 if (i)
589 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
590 change_mode(pp, ECR_PS2);
591 return rlen;
592}
593
594static size_t parport_uss720_ecp_read_data(struct parport *pp, void *buffer, size_t len, int flags)
595{
596 struct parport_uss720_private *priv = pp->private_data;
597 struct usb_device *usbdev = priv->usbdev;
598 int rlen;
599 int i;
600
601 if (!usbdev)
602 return 0;
603 if (change_mode(pp, ECR_ECP))
604 return 0;
605 i = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), buffer, len, &rlen, 20000);
606 if (i)
607 printk(KERN_ERR "uss720: recvbulk ep 2 buf %p len %Zu rlen %u\n", buffer, len, rlen);
608 change_mode(pp, ECR_PS2);
609 return rlen;
610}
611
612static size_t parport_uss720_ecp_write_addr(struct parport *pp, const void *buffer, size_t len, int flags)
613{
614 size_t written = 0;
615
616 if (change_mode(pp, ECR_ECP))
617 return 0;
618 for (; written < len; written++) {
Thomas Sailer0f361632005-09-09 10:43:50 +0200619 if (set_1284_register(pp, 5, *(char *)buffer, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 break;
621 buffer++;
622 }
623 change_mode(pp, ECR_PS2);
624 return written;
625}
626
627static size_t parport_uss720_write_compat(struct parport *pp, const void *buffer, size_t len, int flags)
628{
629 struct parport_uss720_private *priv = pp->private_data;
630 struct usb_device *usbdev = priv->usbdev;
631 int rlen;
632 int i;
633
634 if (!usbdev)
635 return 0;
636 if (change_mode(pp, ECR_PPF))
637 return 0;
638 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
639 if (i)
640 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
641 change_mode(pp, ECR_PS2);
642 return rlen;
643}
644
645/* --------------------------------------------------------------------- */
646
647static struct parport_operations parport_uss720_ops =
648{
649 .owner = THIS_MODULE,
650 .write_data = parport_uss720_write_data,
651 .read_data = parport_uss720_read_data,
652
653 .write_control = parport_uss720_write_control,
654 .read_control = parport_uss720_read_control,
655 .frob_control = parport_uss720_frob_control,
656
657 .read_status = parport_uss720_read_status,
658
659 .enable_irq = parport_uss720_enable_irq,
660 .disable_irq = parport_uss720_disable_irq,
661
662 .data_forward = parport_uss720_data_forward,
663 .data_reverse = parport_uss720_data_reverse,
664
665 .init_state = parport_uss720_init_state,
666 .save_state = parport_uss720_save_state,
667 .restore_state = parport_uss720_restore_state,
668
669 .epp_write_data = parport_uss720_epp_write_data,
670 .epp_read_data = parport_uss720_epp_read_data,
671 .epp_write_addr = parport_uss720_epp_write_addr,
672 .epp_read_addr = parport_uss720_epp_read_addr,
673
674 .ecp_write_data = parport_uss720_ecp_write_data,
675 .ecp_read_data = parport_uss720_ecp_read_data,
676 .ecp_write_addr = parport_uss720_ecp_write_addr,
677
678 .compat_write_data = parport_uss720_write_compat,
679 .nibble_read_data = parport_ieee1284_read_nibble,
680 .byte_read_data = parport_ieee1284_read_byte,
681};
682
683/* --------------------------------------------------------------------- */
684
685static int uss720_probe(struct usb_interface *intf,
686 const struct usb_device_id *id)
687{
Thomas Sailer0f361632005-09-09 10:43:50 +0200688 struct usb_device *usbdev = usb_get_dev(interface_to_usbdev(intf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct usb_host_interface *interface;
690 struct usb_host_endpoint *endpoint;
691 struct parport_uss720_private *priv;
692 struct parport *pp;
Thomas Sailer0f361632005-09-09 10:43:50 +0200693 unsigned char reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 int i;
695
Thomas Sailer0f361632005-09-09 10:43:50 +0200696 dbg("probe: vendor id 0x%x, device id 0x%x\n",
697 le16_to_cpu(usbdev->descriptor.idVendor),
698 le16_to_cpu(usbdev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 /* our known interfaces have 3 alternate settings */
Thomas Sailer0f361632005-09-09 10:43:50 +0200701 if (intf->num_altsetting != 3) {
702 usb_put_dev(usbdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return -ENODEV;
Thomas Sailer0f361632005-09-09 10:43:50 +0200704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 i = usb_set_interface(usbdev, intf->altsetting->desc.bInterfaceNumber, 2);
Thomas Sailer0f361632005-09-09 10:43:50 +0200706 dbg("set inteface result %d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 interface = intf->cur_altsetting;
709
710 /*
711 * Allocate parport interface
712 */
Robert P. J. Daycd861282006-12-13 00:34:52 -0800713 if (!(priv = kzalloc(sizeof(struct parport_uss720_private), GFP_KERNEL))) {
Thomas Sailer0f361632005-09-09 10:43:50 +0200714 usb_put_dev(usbdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return -ENOMEM;
Thomas Sailer0f361632005-09-09 10:43:50 +0200716 }
717 priv->pp = NULL;
718 priv->usbdev = usbdev;
719 kref_init(&priv->ref_count);
720 spin_lock_init(&priv->asynclock);
721 INIT_LIST_HEAD(&priv->asynclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (!(pp = parport_register_port(0, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &parport_uss720_ops))) {
Greg Kroah-Hartman3b6004f2008-08-14 09:37:34 -0700723 printk(KERN_WARNING "uss720: could not register parport\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 goto probe_abort;
725 }
726
Thomas Sailer0f361632005-09-09 10:43:50 +0200727 priv->pp = pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 pp->private_data = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 pp->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_TRISTATE | PARPORT_MODE_EPP | PARPORT_MODE_ECP | PARPORT_MODE_COMPAT;
730
731 /* set the USS720 control register to manual mode, no ECP compression, enable all ints */
Thomas Sailer0f361632005-09-09 10:43:50 +0200732 set_1284_register(pp, 7, 0x00, GFP_KERNEL);
733 set_1284_register(pp, 6, 0x30, GFP_KERNEL); /* PS/2 mode */
734 set_1284_register(pp, 2, 0x0c, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /* debugging */
Thomas Sailer0f361632005-09-09 10:43:50 +0200736 get_1284_register(pp, 0, &reg, GFP_KERNEL);
737 dbg("reg: %02x %02x %02x %02x %02x %02x %02x",
738 priv->reg[0], priv->reg[1], priv->reg[2], priv->reg[3], priv->reg[4], priv->reg[5], priv->reg[6]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 endpoint = &interface->endpoint[2];
Thomas Sailer0f361632005-09-09 10:43:50 +0200741 dbg("epaddr %d interval %d", endpoint->desc.bEndpointAddress, endpoint->desc.bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 parport_announce_port(pp);
743
Thomas Sailer0f361632005-09-09 10:43:50 +0200744 usb_set_intfdata(intf, pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return 0;
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747probe_abort:
Thomas Sailer0f361632005-09-09 10:43:50 +0200748 kill_all_async_requests_priv(priv);
749 kref_put(&priv->ref_count, destroy_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return -ENODEV;
751}
752
753static void uss720_disconnect(struct usb_interface *intf)
754{
Thomas Sailer0f361632005-09-09 10:43:50 +0200755 struct parport *pp = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct parport_uss720_private *priv;
Thomas Sailer0f361632005-09-09 10:43:50 +0200757 struct usb_device *usbdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Thomas Sailer0f361632005-09-09 10:43:50 +0200759 dbg("disconnect");
760 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (pp) {
762 priv = pp->private_data;
Thomas Sailer0f361632005-09-09 10:43:50 +0200763 usbdev = priv->usbdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 priv->usbdev = NULL;
Thomas Sailer0f361632005-09-09 10:43:50 +0200765 priv->pp = NULL;
766 dbg("parport_remove_port");
767 parport_remove_port(pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 parport_put_port(pp);
Thomas Sailer0f361632005-09-09 10:43:50 +0200769 kill_all_async_requests_priv(priv);
770 kref_put(&priv->ref_count, destroy_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Thomas Sailer0f361632005-09-09 10:43:50 +0200772 dbg("disconnect done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775/* table of cables that work through this driver */
Németh Márton33b9e162010-01-10 15:34:45 +0100776static const struct usb_device_id uss720_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 { USB_DEVICE(0x047e, 0x1001) },
778 { USB_DEVICE(0x0557, 0x2001) },
779 { USB_DEVICE(0x0729, 0x1284) },
780 { USB_DEVICE(0x1293, 0x0002) },
Thomas Sailerecc16242010-12-14 16:04:05 +0100781 { USB_DEVICE(0x050d, 0x0002) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 { } /* Terminating entry */
783};
784
785MODULE_DEVICE_TABLE (usb, uss720_table);
786
787
788static struct usb_driver uss720_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 .name = "uss720",
790 .probe = uss720_probe,
791 .disconnect = uss720_disconnect,
792 .id_table = uss720_table,
793};
794
795/* --------------------------------------------------------------------- */
796
Thomas Sailer0f361632005-09-09 10:43:50 +0200797MODULE_AUTHOR(DRIVER_AUTHOR);
798MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799MODULE_LICENSE("GPL");
800
801static int __init uss720_init(void)
802{
803 int retval;
804 retval = usb_register(&uss720_driver);
805 if (retval)
806 goto out;
807
Greg Kroah-Hartman1b29a372008-08-18 13:21:04 -0700808 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
809 DRIVER_DESC "\n");
810 printk(KERN_INFO KBUILD_MODNAME ": NOTE: this is a special purpose "
811 "driver to allow nonstandard\n");
812 printk(KERN_INFO KBUILD_MODNAME ": protocols (eg. bitbang) over "
813 "USS720 usb to parallel cables\n");
814 printk(KERN_INFO KBUILD_MODNAME ": If you just want to connect to a "
815 "printer, use usblp instead\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816out:
817 return retval;
818}
819
820static void __exit uss720_cleanup(void)
821{
822 usb_deregister(&uss720_driver);
823}
824
825module_init(uss720_init);
826module_exit(uss720_cleanup);
827
828/* --------------------------------------------------------------------- */