Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /*****************************************************************************/ |
2 | ||
3 | /* | |
4 | * uss720.c -- USS720 USB Parport Cable. | |
5 | * | |
0f36163d TS |
6 | * Copyright (C) 1999, 2005 |
7 | * Thomas Sailer (t.sailer@alumni.ethz.ch) | |
1da177e4 LT |
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: | |
0f36163d TS |
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 | |
1da177e4 LT |
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> | |
0f36163d TS |
50 | #include <linux/completion.h> |
51 | #include <linux/kref.h> | |
1da177e4 LT |
52 | |
53 | /* | |
54 | * Version Information | |
55 | */ | |
0f36163d TS |
56 | #define DRIVER_VERSION "v0.6" |
57 | #define DRIVER_AUTHOR "Thomas M. Sailer, t.sailer@alumni.ethz.ch" | |
1da177e4 LT |
58 | #define DRIVER_DESC "USB Parport Cable driver for Cables using the Lucent Technologies USS720 Chip" |
59 | ||
60 | /* --------------------------------------------------------------------- */ | |
61 | ||
62 | struct parport_uss720_private { | |
63 | struct usb_device *usbdev; | |
0f36163d TS |
64 | 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 | ||
71 | struct uss720_async_request { | |
72 | struct parport_uss720_private *priv; | |
73 | struct kref ref_count; | |
74 | struct list_head asynclist; | |
75 | struct completion compl; | |
76 | struct urb *urb; | |
77 | struct usb_ctrlrequest dr; | |
78 | __u8 reg[7]; | |
1da177e4 LT |
79 | }; |
80 | ||
81 | /* --------------------------------------------------------------------- */ | |
82 | ||
0f36163d | 83 | static void destroy_priv(struct kref *kref) |
1da177e4 | 84 | { |
0f36163d | 85 | struct parport_uss720_private *priv = container_of(kref, struct parport_uss720_private, ref_count); |
1da177e4 | 86 | |
0f36163d TS |
87 | usb_put_dev(priv->usbdev); |
88 | kfree(priv); | |
89 | dbg("destroying priv datastructure"); | |
90 | } | |
91 | ||
92 | static 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 | ||
7d12e780 | 109 | static void async_complete(struct urb *urb) |
0f36163d TS |
110 | { |
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)); | |
1da177e4 | 122 | #if 0 |
0f36163d TS |
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]); | |
1da177e4 LT |
127 | #endif |
128 | /* if nAck interrupts are enabled and we have an interrupt, call the interrupt procedure */ | |
0f36163d | 129 | if (rq->reg[2] & rq->reg[1] & 0x10 && pp) |
7d12e780 | 130 | parport_generic_irq(0, pp); |
1da177e4 | 131 | } |
0f36163d TS |
132 | complete(&rq->compl); |
133 | kref_put(&rq->ref_count, destroy_async); | |
1da177e4 LT |
134 | } |
135 | ||
0f36163d TS |
136 | static struct uss720_async_request *submit_async_request(struct parport_uss720_private *priv, |
137 | __u8 request, __u8 requesttype, __u16 value, __u16 index, | |
55016f10 | 138 | gfp_t mem_flags) |
1da177e4 | 139 | { |
0f36163d TS |
140 | struct usb_device *usbdev; |
141 | struct uss720_async_request *rq; | |
142 | unsigned long flags; | |
1da177e4 LT |
143 | int ret; |
144 | ||
0f36163d TS |
145 | if (!priv) |
146 | return NULL; | |
147 | usbdev = priv->usbdev; | |
1da177e4 | 148 | if (!usbdev) |
0f36163d TS |
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; | |
1da177e4 | 182 | } |
0f36163d TS |
183 | kref_put(&rq->ref_count, destroy_async); |
184 | err("submit_async_request submit_urb failed with %d", ret); | |
185 | return NULL; | |
186 | } | |
187 | ||
188 | static 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); | |
1da177e4 LT |
200 | return ret; |
201 | } | |
202 | ||
203 | /* --------------------------------------------------------------------- */ | |
204 | ||
55016f10 | 205 | static int get_1284_register(struct parport *pp, unsigned char reg, unsigned char *val, gfp_t mem_flags) |
0f36163d TS |
206 | { |
207 | struct parport_uss720_private *priv; | |
208 | struct uss720_async_request *rq; | |
209 | static const unsigned char regindex[9] = { | |
210 | 4, 0, 1, 5, 5, 0, 2, 3, 6 | |
211 | }; | |
212 | int ret; | |
213 | ||
214 | 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; | |
221 | } | |
222 | 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; | |
228 | *val = priv->reg[(reg >= 9) ? 0 : regindex[reg]]; | |
229 | 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; | |
237 | } | |
238 | ||
55016f10 | 239 | static int set_1284_register(struct parport *pp, unsigned char reg, unsigned char val, gfp_t mem_flags) |
0f36163d TS |
240 | { |
241 | struct parport_uss720_private *priv; | |
242 | struct uss720_async_request *rq; | |
243 | ||
244 | 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; | |
251 | } | |
252 | kref_put(&rq->ref_count, destroy_async); | |
253 | return 0; | |
254 | } | |
255 | ||
256 | /* --------------------------------------------------------------------- */ | |
257 | ||
1da177e4 LT |
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 */ | |
266 | static int change_mode(struct parport *pp, int m) | |
267 | { | |
268 | struct parport_uss720_private *priv = pp->private_data; | |
269 | int mode; | |
0f36163d | 270 | __u8 reg; |
1da177e4 | 271 | |
0f36163d | 272 | if (get_1284_register(pp, 6, ®, GFP_KERNEL)) |
1da177e4 LT |
273 | 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 (;;) { | |
0f36163d | 292 | if (get_1284_register(pp, 6, ®, GFP_KERNEL)) |
1da177e4 LT |
293 | 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. */ | |
0f36163d TS |
306 | if (set_1284_register(pp, 6, m << 5, GFP_KERNEL)) |
307 | return -EIO; | |
308 | if (get_1284_register(pp, 6, ®, GFP_KERNEL)) | |
1da177e4 LT |
309 | return -EIO; |
310 | return 0; | |
311 | } | |
312 | ||
313 | /* | |
314 | * Clear TIMEOUT BIT in EPP MODE | |
315 | */ | |
316 | static int clear_epp_timeout(struct parport *pp) | |
317 | { | |
318 | unsigned char stat; | |
319 | ||
0f36163d | 320 | if (get_1284_register(pp, 1, &stat, GFP_KERNEL)) |
1da177e4 LT |
321 | return 1; |
322 | return stat & 1; | |
323 | } | |
324 | ||
325 | /* | |
326 | * Access functions. | |
327 | */ | |
328 | #if 0 | |
329 | static 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 | ||
344 | static void parport_uss720_write_data(struct parport *pp, unsigned char d) | |
345 | { | |
0f36163d | 346 | set_1284_register(pp, 0, d, GFP_KERNEL); |
1da177e4 LT |
347 | } |
348 | ||
349 | static unsigned char parport_uss720_read_data(struct parport *pp) | |
350 | { | |
351 | unsigned char ret; | |
352 | ||
0f36163d | 353 | if (get_1284_register(pp, 0, &ret, GFP_KERNEL)) |
1da177e4 LT |
354 | return 0; |
355 | return ret; | |
356 | } | |
357 | ||
358 | static 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); | |
0f36163d | 363 | if (set_1284_register(pp, 2, d, GFP_KERNEL)) |
1da177e4 LT |
364 | return; |
365 | priv->reg[1] = d; | |
366 | } | |
367 | ||
368 | static 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 | ||
374 | static 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; | |
0f36163d | 382 | if (set_1284_register(pp, 2, d, GFP_KERNEL)) |
1da177e4 LT |
383 | return 0; |
384 | priv->reg[1] = d; | |
385 | return d & 0xf; | |
386 | } | |
387 | ||
388 | static unsigned char parport_uss720_read_status(struct parport *pp) | |
389 | { | |
390 | unsigned char ret; | |
391 | ||
0f36163d | 392 | if (get_1284_register(pp, 1, &ret, GFP_KERNEL)) |
1da177e4 LT |
393 | return 0; |
394 | return ret & 0xf8; | |
395 | } | |
396 | ||
397 | static 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; | |
0f36163d | 403 | if (set_1284_register(pp, 2, d, GFP_KERNEL)) |
1da177e4 LT |
404 | return; |
405 | priv->reg[1] = d; | |
406 | } | |
407 | ||
408 | static 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; | |
0f36163d | 414 | if (set_1284_register(pp, 2, d, GFP_KERNEL)) |
1da177e4 LT |
415 | return; |
416 | priv->reg[1] = d; | |
417 | } | |
418 | ||
419 | static 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; | |
0f36163d | 425 | if (set_1284_register(pp, 2, d, GFP_KERNEL)) |
1da177e4 LT |
426 | return; |
427 | priv->reg[1] = d; | |
428 | } | |
429 | ||
430 | static 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; | |
0f36163d | 436 | if (set_1284_register(pp, 2, d, GFP_KERNEL)) |
1da177e4 LT |
437 | return; |
438 | priv->reg[1] = d; | |
439 | } | |
440 | ||
441 | static 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 | ||
447 | static void parport_uss720_save_state(struct parport *pp, struct parport_state *s) | |
448 | { | |
449 | struct parport_uss720_private *priv = pp->private_data; | |
450 | ||
0f36163d TS |
451 | #if 0 |
452 | if (get_1284_register(pp, 2, NULL, GFP_ATOMIC)) | |
1da177e4 | 453 | return; |
0f36163d | 454 | #endif |
1da177e4 LT |
455 | s->u.pc.ctr = priv->reg[1]; |
456 | s->u.pc.ecr = priv->reg[2]; | |
457 | } | |
458 | ||
459 | static void parport_uss720_restore_state(struct parport *pp, struct parport_state *s) | |
460 | { | |
0f36163d TS |
461 | 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; | |
1da177e4 LT |
468 | } |
469 | ||
470 | static 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++) { | |
0f36163d | 478 | if (get_1284_register(pp, 4, (char *)buf, GFP_KERNEL)) |
1da177e4 LT |
479 | 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 | ||
490 | static 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++) { | |
0f36163d | 499 | if (set_1284_register(pp, 4, (char *)buf, GFP_KERNEL)) |
1da177e4 LT |
500 | break; |
501 | ((char*)buf)++; | |
0f36163d | 502 | if (get_1284_register(pp, 1, NULL, GFP_KERNEL)) |
1da177e4 LT |
503 | 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 | ||
529 | static 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++) { | |
0f36163d | 537 | if (get_1284_register(pp, 3, (char *)buf, GFP_KERNEL)) |
1da177e4 LT |
538 | 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 | ||
549 | static 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++) { | |
0f36163d | 557 | if (set_1284_register(pp, 3, *(char *)buf, GFP_KERNEL)) |
1da177e4 LT |
558 | break; |
559 | buf++; | |
0f36163d | 560 | if (get_1284_register(pp, 1, NULL, GFP_KERNEL)) |
1da177e4 LT |
561 | 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 | ||
571 | static 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 | ||
589 | static 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 | ||
607 | static 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++) { | |
0f36163d | 614 | if (set_1284_register(pp, 5, *(char *)buffer, GFP_KERNEL)) |
1da177e4 LT |
615 | break; |
616 | buffer++; | |
617 | } | |
618 | change_mode(pp, ECR_PS2); | |
619 | return written; | |
620 | } | |
621 | ||
622 | static 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 | ||
642 | static 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 | ||
680 | static int uss720_probe(struct usb_interface *intf, | |
681 | const struct usb_device_id *id) | |
682 | { | |
0f36163d | 683 | struct usb_device *usbdev = usb_get_dev(interface_to_usbdev(intf)); |
1da177e4 LT |
684 | struct usb_host_interface *interface; |
685 | struct usb_host_endpoint *endpoint; | |
686 | struct parport_uss720_private *priv; | |
687 | struct parport *pp; | |
0f36163d | 688 | unsigned char reg; |
1da177e4 LT |
689 | int i; |
690 | ||
0f36163d TS |
691 | dbg("probe: vendor id 0x%x, device id 0x%x\n", |
692 | le16_to_cpu(usbdev->descriptor.idVendor), | |
693 | le16_to_cpu(usbdev->descriptor.idProduct)); | |
1da177e4 LT |
694 | |
695 | /* our known interfaces have 3 alternate settings */ | |
0f36163d TS |
696 | if (intf->num_altsetting != 3) { |
697 | usb_put_dev(usbdev); | |
1da177e4 | 698 | return -ENODEV; |
0f36163d | 699 | } |
1da177e4 | 700 | i = usb_set_interface(usbdev, intf->altsetting->desc.bInterfaceNumber, 2); |
0f36163d | 701 | dbg("set inteface result %d", i); |
1da177e4 LT |
702 | |
703 | interface = intf->cur_altsetting; | |
704 | ||
705 | /* | |
706 | * Allocate parport interface | |
707 | */ | |
0f36163d TS |
708 | if (!(priv = kcalloc(sizeof(struct parport_uss720_private), 1, GFP_KERNEL))) { |
709 | usb_put_dev(usbdev); | |
1da177e4 | 710 | return -ENOMEM; |
0f36163d TS |
711 | } |
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); | |
1da177e4 | 717 | if (!(pp = parport_register_port(0, PARPORT_IRQ_NONE, PARPORT_DMA_NONE, &parport_uss720_ops))) { |
0f36163d | 718 | warn("could not register parport"); |
1da177e4 LT |
719 | goto probe_abort; |
720 | } | |
721 | ||
0f36163d | 722 | priv->pp = pp; |
1da177e4 | 723 | pp->private_data = priv; |
1da177e4 LT |
724 | 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 */ | |
0f36163d TS |
727 | set_1284_register(pp, 7, 0x00, GFP_KERNEL); |
728 | set_1284_register(pp, 6, 0x30, GFP_KERNEL); /* PS/2 mode */ | |
729 | set_1284_register(pp, 2, 0x0c, GFP_KERNEL); | |
1da177e4 | 730 | /* debugging */ |
0f36163d TS |
731 | get_1284_register(pp, 0, ®, GFP_KERNEL); |
732 | dbg("reg: %02x %02x %02x %02x %02x %02x %02x", | |
733 | priv->reg[0], priv->reg[1], priv->reg[2], priv->reg[3], priv->reg[4], priv->reg[5], priv->reg[6]); | |
1da177e4 LT |
734 | |
735 | endpoint = &interface->endpoint[2]; | |
0f36163d | 736 | dbg("epaddr %d interval %d", endpoint->desc.bEndpointAddress, endpoint->desc.bInterval); |
1da177e4 LT |
737 | parport_announce_port(pp); |
738 | ||
0f36163d | 739 | usb_set_intfdata(intf, pp); |
1da177e4 LT |
740 | return 0; |
741 | ||
1da177e4 | 742 | probe_abort: |
0f36163d TS |
743 | kill_all_async_requests_priv(priv); |
744 | kref_put(&priv->ref_count, destroy_priv); | |
1da177e4 LT |
745 | return -ENODEV; |
746 | } | |
747 | ||
748 | static void uss720_disconnect(struct usb_interface *intf) | |
749 | { | |
0f36163d | 750 | struct parport *pp = usb_get_intfdata(intf); |
1da177e4 | 751 | struct parport_uss720_private *priv; |
0f36163d | 752 | struct usb_device *usbdev; |
1da177e4 | 753 | |
0f36163d TS |
754 | dbg("disconnect"); |
755 | usb_set_intfdata(intf, NULL); | |
1da177e4 LT |
756 | if (pp) { |
757 | priv = pp->private_data; | |
0f36163d | 758 | usbdev = priv->usbdev; |
1da177e4 | 759 | priv->usbdev = NULL; |
0f36163d TS |
760 | priv->pp = NULL; |
761 | dbg("parport_remove_port"); | |
762 | parport_remove_port(pp); | |
1da177e4 | 763 | parport_put_port(pp); |
0f36163d TS |
764 | kill_all_async_requests_priv(priv); |
765 | kref_put(&priv->ref_count, destroy_priv); | |
1da177e4 | 766 | } |
0f36163d | 767 | dbg("disconnect done"); |
1da177e4 LT |
768 | } |
769 | ||
770 | /* table of cables that work through this driver */ | |
771 | static struct usb_device_id uss720_table [] = { | |
772 | { USB_DEVICE(0x047e, 0x1001) }, | |
773 | { USB_DEVICE(0x0557, 0x2001) }, | |
774 | { USB_DEVICE(0x0729, 0x1284) }, | |
775 | { USB_DEVICE(0x1293, 0x0002) }, | |
776 | { } /* Terminating entry */ | |
777 | }; | |
778 | ||
779 | MODULE_DEVICE_TABLE (usb, uss720_table); | |
780 | ||
781 | ||
782 | static struct usb_driver uss720_driver = { | |
1da177e4 LT |
783 | .name = "uss720", |
784 | .probe = uss720_probe, | |
785 | .disconnect = uss720_disconnect, | |
786 | .id_table = uss720_table, | |
787 | }; | |
788 | ||
789 | /* --------------------------------------------------------------------- */ | |
790 | ||
0f36163d TS |
791 | MODULE_AUTHOR(DRIVER_AUTHOR); |
792 | MODULE_DESCRIPTION(DRIVER_DESC); | |
1da177e4 LT |
793 | MODULE_LICENSE("GPL"); |
794 | ||
795 | static 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); | |
0f36163d TS |
803 | info("NOTE: this is a special purpose driver to allow nonstandard"); |
804 | info("protocols (eg. bitbang) over USS720 usb to parallel cables"); | |
805 | info("If you just want to connect to a printer, use usblp instead"); | |
1da177e4 LT |
806 | out: |
807 | return retval; | |
808 | } | |
809 | ||
810 | static void __exit uss720_cleanup(void) | |
811 | { | |
812 | usb_deregister(&uss720_driver); | |
813 | } | |
814 | ||
815 | module_init(uss720_init); | |
816 | module_exit(uss720_cleanup); | |
817 | ||
818 | /* --------------------------------------------------------------------- */ |