blob: 70250252ae2ae404d30f5583d4396a6e70f590fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************/
2
3/*
4 * uss720.c -- USS720 USB Parport Cable.
5 *
Thomas Sailer0f361632005-09-09 10:43:50 +02006 * Copyright (C) 1999, 2005
7 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*
54 * Version Information
55 */
Thomas Sailer0f361632005-09-09 10:43:50 +020056#define DRIVER_VERSION "v0.6"
57#define DRIVER_AUTHOR "Thomas M. Sailer, t.sailer@alumni.ethz.ch"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define DRIVER_DESC "USB Parport Cable driver for Cables using the Lucent Technologies USS720 Chip"
59
60/* --------------------------------------------------------------------- */
61
62struct parport_uss720_private {
63 struct usb_device *usbdev;
Thomas Sailer0f361632005-09-09 10:43:50 +020064 struct parport *pp;
65 struct kref ref_count;
66 __u8 reg[7]; /* USB registers */
67 struct list_head asynclist;
68 spinlock_t asynclock;
69};
70
71struct uss720_async_request {
72 struct parport_uss720_private *priv;
73 struct kref ref_count;
74 struct list_head asynclist;
75 struct completion compl;
76 struct urb *urb;
77 struct usb_ctrlrequest dr;
78 __u8 reg[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
81/* --------------------------------------------------------------------- */
82
Thomas Sailer0f361632005-09-09 10:43:50 +020083static void destroy_priv(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Thomas Sailer0f361632005-09-09 10:43:50 +020085 struct parport_uss720_private *priv = container_of(kref, struct parport_uss720_private, ref_count);
86
87 usb_put_dev(priv->usbdev);
88 kfree(priv);
89 dbg("destroying priv datastructure");
90}
91
92static void destroy_async(struct kref *kref)
93{
94 struct uss720_async_request *rq = container_of(kref, struct uss720_async_request, ref_count);
95 struct parport_uss720_private *priv = rq->priv;
96 unsigned long flags;
97
98 if (likely(rq->urb))
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);
103 kfree(rq);
104 kref_put(&priv->ref_count, destroy_priv);
105}
106
107/* --------------------------------------------------------------------- */
108
David Howells7d12e782006-10-05 14:55:46 +0100109static void async_complete(struct urb *urb)
Thomas Sailer0f361632005-09-09 10:43:50 +0200110{
111 struct uss720_async_request *rq;
112 struct parport *pp;
113 struct parport_uss720_private *priv;
114
115 rq = urb->context;
116 priv = rq->priv;
117 pp = priv->pp;
118 if (urb->status) {
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));
122#if 0
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]);
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)
David Howells7d12e782006-10-05 14:55:46 +0100130 parport_generic_irq(0, pp);
Thomas Sailer0f361632005-09-09 10:43:50 +0200131 }
132 complete(&rq->compl);
133 kref_put(&rq->ref_count, destroy_async);
134}
135
136static struct uss720_async_request *submit_async_request(struct parport_uss720_private *priv,
137 __u8 request, __u8 requesttype, __u16 value, __u16 index,
Al Viro55016f12005-10-21 03:21:58 -0400138 gfp_t mem_flags)
Thomas Sailer0f361632005-09-09 10:43:50 +0200139{
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 = kmalloc(sizeof(struct uss720_async_request), mem_flags);
151 if (!rq) {
152 err("submit_async_request out of memory");
153 return NULL;
154 }
155 kref_init(&rq->ref_count);
156 INIT_LIST_HEAD(&rq->asynclist);
157 init_completion(&rq->compl);
158 kref_get(&priv->ref_count);
159 rq->priv = priv;
160 rq->urb = usb_alloc_urb(0, mem_flags);
161 if (!rq->urb) {
162 kref_put(&rq->ref_count, destroy_async);
163 err("submit_async_request out of memory");
164 return NULL;
165 }
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);
179 if (!ret) {
180 kref_get(&rq->ref_count);
181 return rq;
182 }
183 kref_put(&rq->ref_count, destroy_async);
184 err("submit_async_request submit_urb failed with %d", ret);
185 return NULL;
186}
187
188static unsigned int kill_all_async_requests_priv(struct parport_uss720_private *priv)
189{
190 struct uss720_async_request *rq;
191 unsigned long flags;
192 unsigned int ret = 0;
193
194 spin_lock_irqsave(&priv->asynclock, flags);
195 list_for_each_entry(rq, &priv->asynclist, asynclist) {
196 usb_unlink_urb(rq->urb);
197 ret++;
198 }
199 spin_unlock_irqrestore(&priv->asynclock, flags);
200 return ret;
201}
202
203/* --------------------------------------------------------------------- */
204
Al Viro55016f12005-10-21 03:21:58 -0400205static int get_1284_register(struct parport *pp, unsigned char reg, unsigned char *val, gfp_t mem_flags)
Thomas Sailer0f361632005-09-09 10:43:50 +0200206{
207 struct parport_uss720_private *priv;
208 struct uss720_async_request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 static const unsigned char regindex[9] = {
210 4, 0, 1, 5, 5, 0, 2, 3, 6
211 };
212 int ret;
213
Thomas Sailer0f361632005-09-09 10:43:50 +0200214 if (!pp)
215 return -EIO;
216 priv = pp->private_data;
217 rq = submit_async_request(priv, 3, 0xc0, ((unsigned int)reg) << 8, 0, mem_flags);
218 if (!rq) {
219 err("get_1284_register(%u) failed", (unsigned int)reg);
220 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Thomas Sailer0f361632005-09-09 10:43:50 +0200222 if (!val) {
223 kref_put(&rq->ref_count, destroy_async);
224 return 0;
225 }
226 if (wait_for_completion_timeout(&rq->compl, HZ)) {
227 ret = rq->urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 *val = priv->reg[(reg >= 9) ? 0 : regindex[reg]];
Thomas Sailer0f361632005-09-09 10:43:50 +0200229 if (ret)
230 warn("get_1284_register: usb error %d", ret);
231 kref_put(&rq->ref_count, destroy_async);
232 return ret;
233 }
234 warn("get_1284_register timeout");
235 kill_all_async_requests_priv(priv);
236 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Al Viro55016f12005-10-21 03:21:58 -0400239static int set_1284_register(struct parport *pp, unsigned char reg, unsigned char val, gfp_t mem_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Thomas Sailer0f361632005-09-09 10:43:50 +0200241 struct parport_uss720_private *priv;
242 struct uss720_async_request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Thomas Sailer0f361632005-09-09 10:43:50 +0200244 if (!pp)
245 return -EIO;
246 priv = pp->private_data;
247 rq = submit_async_request(priv, 4, 0x40, (((unsigned int)reg) << 8) | val, 0, mem_flags);
248 if (!rq) {
249 err("set_1284_register(%u,%u) failed", (unsigned int)reg, (unsigned int)val);
250 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Thomas Sailer0f361632005-09-09 10:43:50 +0200252 kref_put(&rq->ref_count, destroy_async);
253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256/* --------------------------------------------------------------------- */
257
258/* ECR modes */
259#define ECR_SPP 00
260#define ECR_PS2 01
261#define ECR_PPF 02
262#define ECR_ECP 03
263#define ECR_EPP 04
264
265/* Safely change the mode bits in the ECR */
266static int change_mode(struct parport *pp, int m)
267{
268 struct parport_uss720_private *priv = pp->private_data;
269 int mode;
Thomas Sailer0f361632005-09-09 10:43:50 +0200270 __u8 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Thomas Sailer0f361632005-09-09 10:43:50 +0200272 if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return -EIO;
274 /* Bits <7:5> contain the mode. */
275 mode = (priv->reg[2] >> 5) & 0x7;
276 if (mode == m)
277 return 0;
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))
281 return -EIO;
282
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;
287 switch (mode) {
288 case ECR_PPF: /* Parallel Port FIFO mode */
289 case ECR_ECP: /* ECP Parallel Port mode */
290 /* Poll slowly. */
291 for (;;) {
Thomas Sailer0f361632005-09-09 10:43:50 +0200292 if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return -EIO;
294 if (priv->reg[2] & 0x01)
295 break;
296 if (time_after_eq (jiffies, expire))
297 /* The FIFO is stuck. */
298 return -EBUSY;
299 msleep_interruptible(10);
300 if (signal_pending (current))
301 break;
302 }
303 }
304 }
305 /* Set the mode. */
Thomas Sailer0f361632005-09-09 10:43:50 +0200306 if (set_1284_register(pp, 6, m << 5, GFP_KERNEL))
307 return -EIO;
308 if (get_1284_register(pp, 6, &reg, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return -EIO;
310 return 0;
311}
312
313/*
314 * Clear TIMEOUT BIT in EPP MODE
315 */
316static int clear_epp_timeout(struct parport *pp)
317{
318 unsigned char stat;
319
Thomas Sailer0f361632005-09-09 10:43:50 +0200320 if (get_1284_register(pp, 1, &stat, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return 1;
322 return stat & 1;
323}
324
325/*
326 * Access functions.
327 */
328#if 0
329static int uss720_irq(int usbstatus, void *buffer, int len, void *dev_id)
330{
331 struct parport *pp = (struct parport *)dev_id;
332 struct parport_uss720_private *priv = pp->private_data;
333
334 if (usbstatus != 0 || len < 4 || !buffer)
335 return 1;
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);
340 return 1;
341}
342#endif
343
344static void parport_uss720_write_data(struct parport *pp, unsigned char d)
345{
Thomas Sailer0f361632005-09-09 10:43:50 +0200346 set_1284_register(pp, 0, d, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349static unsigned char parport_uss720_read_data(struct parport *pp)
350{
351 unsigned char ret;
352
Thomas Sailer0f361632005-09-09 10:43:50 +0200353 if (get_1284_register(pp, 0, &ret, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355 return ret;
356}
357
358static void parport_uss720_write_control(struct parport *pp, unsigned char d)
359{
360 struct parport_uss720_private *priv = pp->private_data;
361
362 d = (d & 0xf) | (priv->reg[1] & 0xf0);
Thomas Sailer0f361632005-09-09 10:43:50 +0200363 if (set_1284_register(pp, 2, d, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return;
365 priv->reg[1] = d;
366}
367
368static unsigned char parport_uss720_read_control(struct parport *pp)
369{
370 struct parport_uss720_private *priv = pp->private_data;
371 return priv->reg[1] & 0xf; /* Use soft copy */
372}
373
374static unsigned char parport_uss720_frob_control(struct parport *pp, unsigned char mask, unsigned char val)
375{
376 struct parport_uss720_private *priv = pp->private_data;
377 unsigned char d;
378
379 mask &= 0x0f;
380 val &= 0x0f;
381 d = (priv->reg[1] & (~mask)) ^ val;
Thomas Sailer0f361632005-09-09 10:43:50 +0200382 if (set_1284_register(pp, 2, d, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return 0;
384 priv->reg[1] = d;
385 return d & 0xf;
386}
387
388static unsigned char parport_uss720_read_status(struct parport *pp)
389{
390 unsigned char ret;
391
Thomas Sailer0f361632005-09-09 10:43:50 +0200392 if (get_1284_register(pp, 1, &ret, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return 0;
394 return ret & 0xf8;
395}
396
397static void parport_uss720_disable_irq(struct parport *pp)
398{
399 struct parport_uss720_private *priv = pp->private_data;
400 unsigned char d;
401
402 d = priv->reg[1] & ~0x10;
Thomas Sailer0f361632005-09-09 10:43:50 +0200403 if (set_1284_register(pp, 2, d, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return;
405 priv->reg[1] = d;
406}
407
408static void parport_uss720_enable_irq(struct parport *pp)
409{
410 struct parport_uss720_private *priv = pp->private_data;
411 unsigned char d;
412
413 d = priv->reg[1] | 0x10;
Thomas Sailer0f361632005-09-09 10:43:50 +0200414 if (set_1284_register(pp, 2, d, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return;
416 priv->reg[1] = d;
417}
418
419static void parport_uss720_data_forward (struct parport *pp)
420{
421 struct parport_uss720_private *priv = pp->private_data;
422 unsigned char d;
423
424 d = priv->reg[1] & ~0x20;
Thomas Sailer0f361632005-09-09 10:43:50 +0200425 if (set_1284_register(pp, 2, d, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return;
427 priv->reg[1] = d;
428}
429
430static void parport_uss720_data_reverse (struct parport *pp)
431{
432 struct parport_uss720_private *priv = pp->private_data;
433 unsigned char d;
434
435 d = priv->reg[1] | 0x20;
Thomas Sailer0f361632005-09-09 10:43:50 +0200436 if (set_1284_register(pp, 2, d, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return;
438 priv->reg[1] = d;
439}
440
441static void parport_uss720_init_state(struct pardevice *dev, struct parport_state *s)
442{
443 s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
444 s->u.pc.ecr = 0x24;
445}
446
447static void parport_uss720_save_state(struct parport *pp, struct parport_state *s)
448{
449 struct parport_uss720_private *priv = pp->private_data;
450
Thomas Sailer0f361632005-09-09 10:43:50 +0200451#if 0
452 if (get_1284_register(pp, 2, NULL, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return;
Thomas Sailer0f361632005-09-09 10:43:50 +0200454#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 s->u.pc.ctr = priv->reg[1];
456 s->u.pc.ecr = priv->reg[2];
457}
458
459static void parport_uss720_restore_state(struct parport *pp, struct parport_state *s)
460{
Thomas Sailer0f361632005-09-09 10:43:50 +0200461 struct parport_uss720_private *priv = pp->private_data;
462
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470static size_t parport_uss720_epp_read_data(struct parport *pp, void *buf, size_t length, int flags)
471{
472 struct parport_uss720_private *priv = pp->private_data;
473 size_t got = 0;
474
475 if (change_mode(pp, ECR_EPP))
476 return 0;
477 for (; got < length; got++) {
Thomas Sailer0f361632005-09-09 10:43:50 +0200478 if (get_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 buf++;
481 if (priv->reg[0] & 0x01) {
482 clear_epp_timeout(pp);
483 break;
484 }
485 }
486 change_mode(pp, ECR_PS2);
487 return got;
488}
489
490static size_t parport_uss720_epp_write_data(struct parport *pp, const void *buf, size_t length, int flags)
491{
492#if 0
493 struct parport_uss720_private *priv = pp->private_data;
494 size_t written = 0;
495
496 if (change_mode(pp, ECR_EPP))
497 return 0;
498 for (; written < length; written++) {
Thomas Sailer0f361632005-09-09 10:43:50 +0200499 if (set_1284_register(pp, 4, (char *)buf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 break;
501 ((char*)buf)++;
Thomas Sailer0f361632005-09-09 10:43:50 +0200502 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
504 if (priv->reg[0] & 0x01) {
505 clear_epp_timeout(pp);
506 break;
507 }
508 }
509 change_mode(pp, ECR_PS2);
510 return written;
511#else
512 struct parport_uss720_private *priv = pp->private_data;
513 struct usb_device *usbdev = priv->usbdev;
514 int rlen;
515 int i;
516
517 if (!usbdev)
518 return 0;
519 if (change_mode(pp, ECR_EPP))
520 return 0;
521 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buf, length, &rlen, 20000);
522 if (i)
523 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buf, length, rlen);
524 change_mode(pp, ECR_PS2);
525 return rlen;
526#endif
527}
528
529static size_t parport_uss720_epp_read_addr(struct parport *pp, void *buf, size_t length, int flags)
530{
531 struct parport_uss720_private *priv = pp->private_data;
532 size_t got = 0;
533
534 if (change_mode(pp, ECR_EPP))
535 return 0;
536 for (; got < length; got++) {
Thomas Sailer0f361632005-09-09 10:43:50 +0200537 if (get_1284_register(pp, 3, (char *)buf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 break;
539 buf++;
540 if (priv->reg[0] & 0x01) {
541 clear_epp_timeout(pp);
542 break;
543 }
544 }
545 change_mode(pp, ECR_PS2);
546 return got;
547}
548
549static size_t parport_uss720_epp_write_addr(struct parport *pp, const void *buf, size_t length, int flags)
550{
551 struct parport_uss720_private *priv = pp->private_data;
552 size_t written = 0;
553
554 if (change_mode(pp, ECR_EPP))
555 return 0;
556 for (; written < length; written++) {
Thomas Sailer0f361632005-09-09 10:43:50 +0200557 if (set_1284_register(pp, 3, *(char *)buf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 break;
559 buf++;
Thomas Sailer0f361632005-09-09 10:43:50 +0200560 if (get_1284_register(pp, 1, NULL, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 break;
562 if (priv->reg[0] & 0x01) {
563 clear_epp_timeout(pp);
564 break;
565 }
566 }
567 change_mode(pp, ECR_PS2);
568 return written;
569}
570
571static size_t parport_uss720_ecp_write_data(struct parport *pp, const void *buffer, size_t len, int flags)
572{
573 struct parport_uss720_private *priv = pp->private_data;
574 struct usb_device *usbdev = priv->usbdev;
575 int rlen;
576 int i;
577
578 if (!usbdev)
579 return 0;
580 if (change_mode(pp, ECR_ECP))
581 return 0;
582 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
583 if (i)
584 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
585 change_mode(pp, ECR_PS2);
586 return rlen;
587}
588
589static size_t parport_uss720_ecp_read_data(struct parport *pp, void *buffer, size_t len, int flags)
590{
591 struct parport_uss720_private *priv = pp->private_data;
592 struct usb_device *usbdev = priv->usbdev;
593 int rlen;
594 int i;
595
596 if (!usbdev)
597 return 0;
598 if (change_mode(pp, ECR_ECP))
599 return 0;
600 i = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), buffer, len, &rlen, 20000);
601 if (i)
602 printk(KERN_ERR "uss720: recvbulk ep 2 buf %p len %Zu rlen %u\n", buffer, len, rlen);
603 change_mode(pp, ECR_PS2);
604 return rlen;
605}
606
607static size_t parport_uss720_ecp_write_addr(struct parport *pp, const void *buffer, size_t len, int flags)
608{
609 size_t written = 0;
610
611 if (change_mode(pp, ECR_ECP))
612 return 0;
613 for (; written < len; written++) {
Thomas Sailer0f361632005-09-09 10:43:50 +0200614 if (set_1284_register(pp, 5, *(char *)buffer, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 break;
616 buffer++;
617 }
618 change_mode(pp, ECR_PS2);
619 return written;
620}
621
622static size_t parport_uss720_write_compat(struct parport *pp, const void *buffer, size_t len, int flags)
623{
624 struct parport_uss720_private *priv = pp->private_data;
625 struct usb_device *usbdev = priv->usbdev;
626 int rlen;
627 int i;
628
629 if (!usbdev)
630 return 0;
631 if (change_mode(pp, ECR_PPF))
632 return 0;
633 i = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), (void *)buffer, len, &rlen, 20000);
634 if (i)
635 printk(KERN_ERR "uss720: sendbulk ep 1 buf %p len %Zu rlen %u\n", buffer, len, rlen);
636 change_mode(pp, ECR_PS2);
637 return rlen;
638}
639
640/* --------------------------------------------------------------------- */
641
642static struct parport_operations parport_uss720_ops =
643{
644 .owner = THIS_MODULE,
645 .write_data = parport_uss720_write_data,
646 .read_data = parport_uss720_read_data,
647
648 .write_control = parport_uss720_write_control,
649 .read_control = parport_uss720_read_control,
650 .frob_control = parport_uss720_frob_control,
651
652 .read_status = parport_uss720_read_status,
653
654 .enable_irq = parport_uss720_enable_irq,
655 .disable_irq = parport_uss720_disable_irq,
656
657 .data_forward = parport_uss720_data_forward,
658 .data_reverse = parport_uss720_data_reverse,
659
660 .init_state = parport_uss720_init_state,
661 .save_state = parport_uss720_save_state,
662 .restore_state = parport_uss720_restore_state,
663
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,
668
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,
672
673 .compat_write_data = parport_uss720_write_compat,
674 .nibble_read_data = parport_ieee1284_read_nibble,
675 .byte_read_data = parport_ieee1284_read_byte,
676};
677
678/* --------------------------------------------------------------------- */
679
680static int uss720_probe(struct usb_interface *intf,
681 const struct usb_device_id *id)
682{
Thomas Sailer0f361632005-09-09 10:43:50 +0200683 struct usb_device *usbdev = usb_get_dev(interface_to_usbdev(intf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct usb_host_interface *interface;
685 struct usb_host_endpoint *endpoint;
686 struct parport_uss720_private *priv;
687 struct parport *pp;
Thomas Sailer0f361632005-09-09 10:43:50 +0200688 unsigned char reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 int i;
690
Thomas Sailer0f361632005-09-09 10:43:50 +0200691 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /* our known interfaces have 3 alternate settings */
Thomas Sailer0f361632005-09-09 10:43:50 +0200696 if (intf->num_altsetting != 3) {
697 usb_put_dev(usbdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return -ENODEV;
Thomas Sailer0f361632005-09-09 10:43:50 +0200699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 i = usb_set_interface(usbdev, intf->altsetting->desc.bInterfaceNumber, 2);
Thomas Sailer0f361632005-09-09 10:43:50 +0200701 dbg("set inteface result %d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 interface = intf->cur_altsetting;
704
705 /*
706 * Allocate parport interface
707 */
Robert P. J. Daycd861282006-12-13 00:34:52 -0800708 if (!(priv = kzalloc(sizeof(struct parport_uss720_private), GFP_KERNEL))) {
Thomas Sailer0f361632005-09-09 10:43:50 +0200709 usb_put_dev(usbdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return -ENOMEM;
Thomas Sailer0f361632005-09-09 10:43:50 +0200711 }
712 priv->pp = NULL;
713 priv->usbdev = usbdev;
714 kref_init(&priv->ref_count);
715 spin_lock_init(&priv->asynclock);
716 INIT_LIST_HEAD(&priv->asynclist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (!(pp = parport_register_port(0, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &parport_uss720_ops))) {
Thomas Sailer0f361632005-09-09 10:43:50 +0200718 warn("could not register parport");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 goto probe_abort;
720 }
721
Thomas Sailer0f361632005-09-09 10:43:50 +0200722 priv->pp = pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 pp->private_data = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 pp->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_TRISTATE | PARPORT_MODE_EPP | PARPORT_MODE_ECP | PARPORT_MODE_COMPAT;
725
726 /* set the USS720 control register to manual mode, no ECP compression, enable all ints */
Thomas Sailer0f361632005-09-09 10:43:50 +0200727 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /* debugging */
Thomas Sailer0f361632005-09-09 10:43:50 +0200731 get_1284_register(pp, 0, &reg, 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 endpoint = &interface->endpoint[2];
Thomas Sailer0f361632005-09-09 10:43:50 +0200736 dbg("epaddr %d interval %d", endpoint->desc.bEndpointAddress, endpoint->desc.bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 parport_announce_port(pp);
738
Thomas Sailer0f361632005-09-09 10:43:50 +0200739 usb_set_intfdata(intf, pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return 0;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742probe_abort:
Thomas Sailer0f361632005-09-09 10:43:50 +0200743 kill_all_async_requests_priv(priv);
744 kref_put(&priv->ref_count, destroy_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return -ENODEV;
746}
747
748static void uss720_disconnect(struct usb_interface *intf)
749{
Thomas Sailer0f361632005-09-09 10:43:50 +0200750 struct parport *pp = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 struct parport_uss720_private *priv;
Thomas Sailer0f361632005-09-09 10:43:50 +0200752 struct usb_device *usbdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Thomas Sailer0f361632005-09-09 10:43:50 +0200754 dbg("disconnect");
755 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (pp) {
757 priv = pp->private_data;
Thomas Sailer0f361632005-09-09 10:43:50 +0200758 usbdev = priv->usbdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 priv->usbdev = NULL;
Thomas Sailer0f361632005-09-09 10:43:50 +0200760 priv->pp = NULL;
761 dbg("parport_remove_port");
762 parport_remove_port(pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 parport_put_port(pp);
Thomas Sailer0f361632005-09-09 10:43:50 +0200764 kill_all_async_requests_priv(priv);
765 kref_put(&priv->ref_count, destroy_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
Thomas Sailer0f361632005-09-09 10:43:50 +0200767 dbg("disconnect done");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770/* table of cables that work through this driver */
771static 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 */
777};
778
779MODULE_DEVICE_TABLE (usb, uss720_table);
780
781
782static struct usb_driver uss720_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 .name = "uss720",
784 .probe = uss720_probe,
785 .disconnect = uss720_disconnect,
786 .id_table = uss720_table,
787};
788
789/* --------------------------------------------------------------------- */
790
Thomas Sailer0f361632005-09-09 10:43:50 +0200791MODULE_AUTHOR(DRIVER_AUTHOR);
792MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793MODULE_LICENSE("GPL");
794
795static int __init uss720_init(void)
796{
797 int retval;
798 retval = usb_register(&uss720_driver);
799 if (retval)
800 goto out;
801
802 info(DRIVER_VERSION ":" DRIVER_DESC);
Thomas Sailer0f361632005-09-09 10:43:50 +0200803 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806out:
807 return retval;
808}
809
810static void __exit uss720_cleanup(void)
811{
812 usb_deregister(&uss720_driver);
813}
814
815module_init(uss720_init);
816module_exit(uss720_cleanup);
817
818/* --------------------------------------------------------------------- */