1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#include <linux/module.h>
49#include <linux/kernel.h>
50#include <linux/sched.h>
51#include <linux/smp_lock.h>
52#include <linux/signal.h>
53#include <linux/poll.h>
54#include <linux/init.h>
55#include <linux/slab.h>
56#include <linux/lp.h>
57#include <linux/devfs_fs_kernel.h>
58#undef DEBUG
59#include <linux/usb.h>
60
61
62
63
64#define DRIVER_VERSION "v0.13"
65#define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
66#define DRIVER_DESC "USB Printer Device Class driver"
67
68#define USBLP_BUF_SIZE 8192
69#define USBLP_DEVICE_ID_SIZE 1024
70
71
72#define LPGETSTATUS 0x060b
73#define IOCNR_GET_DEVICE_ID 1
74#define IOCNR_GET_PROTOCOLS 2
75#define IOCNR_SET_PROTOCOL 3
76#define IOCNR_HP_SET_CHANNEL 4
77#define IOCNR_GET_BUS_ADDRESS 5
78#define IOCNR_GET_VID_PID 6
79
80#define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
81
82
83
84
85#define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
86
87#define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
88
89#define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
90
91#define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
92
93#define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
94
95
96
97
98
99
100
101
102
103
104
105
106
107#define USBLP_REQ_GET_ID 0x00
108#define USBLP_REQ_GET_STATUS 0x01
109#define USBLP_REQ_RESET 0x02
110#define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST 0x00
111
112#define USBLP_MINORS 16
113#define USBLP_MINOR_BASE 0
114
115#define USBLP_WRITE_TIMEOUT (5*HZ)
116
117#define USBLP_FIRST_PROTOCOL 1
118#define USBLP_LAST_PROTOCOL 3
119#define USBLP_MAX_PROTOCOLS (USBLP_LAST_PROTOCOL+1)
120
121
122
123
124
125#define STATUS_BUF_SIZE 8
126
127struct usblp {
128 struct usb_device *dev;
129 devfs_handle_t devfs;
130 struct semaphore sem;
131 char *writebuf;
132 char *readbuf;
133 char *statusbuf;
134 struct urb *readurb, *writeurb;
135 wait_queue_head_t wait;
136 int readcount;
137 int ifnum;
138
139
140 struct {
141 int alt_setting;
142 struct usb_endpoint_descriptor *epwrite;
143 struct usb_endpoint_descriptor *epread;
144 } protocol[USBLP_MAX_PROTOCOLS];
145 int current_protocol;
146 int minor;
147 int wcomplete;
148 int rcomplete;
149 unsigned int quirks;
150 unsigned char used;
151 unsigned char present;
152 unsigned char bidir;
153 unsigned char *device_id_string;
154
155};
156
157#ifdef DEBUG
158static void usblp_dump(struct usblp *usblp) {
159 int p;
160
161 dbg("usblp=0x%p", usblp);
162 dbg("dev=0x%p", usblp->dev);
163 dbg("present=%d", usblp->present);
164 dbg("readbuf=0x%p", usblp->readbuf);
165 dbg("writebuf=0x%p", usblp->writebuf);
166 dbg("readurb=0x%p", usblp->readurb);
167 dbg("writeurb=0x%p", usblp->writeurb);
168 dbg("readcount=%d", usblp->readcount);
169 dbg("ifnum=%d", usblp->ifnum);
170 for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
171 dbg("protocol[%d].alt_setting=%d", p, usblp->protocol[p].alt_setting);
172 dbg("protocol[%d].epwrite=%p", p, usblp->protocol[p].epwrite);
173 dbg("protocol[%d].epread=%p", p, usblp->protocol[p].epread);
174 }
175 dbg("current_protocol=%d", usblp->current_protocol);
176 dbg("minor=%d", usblp->minor);
177 dbg("wcomplete=%d", usblp->wcomplete);
178 dbg("rcomplete=%d", usblp->rcomplete);
179 dbg("quirks=%d", usblp->quirks);
180 dbg("used=%d", usblp->used);
181 dbg("bidir=%d", usblp->bidir);
182 dbg("device_id_string=\"%s\"",
183 usblp->device_id_string ?
184 usblp->device_id_string + 2 :
185 (unsigned char *)"(null)");
186}
187#endif
188
189extern devfs_handle_t usb_devfs_handle;
190
191static struct usblp *usblp_table[USBLP_MINORS];
192
193
194
195struct quirk_printer_struct {
196 __u16 vendorId;
197 __u16 productId;
198 unsigned int quirks;
199};
200
201#define USBLP_QUIRK_BIDIR 0x1
202#define USBLP_QUIRK_USB_INIT 0x2
203
204static struct quirk_printer_struct quirk_printers[] = {
205 { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR },
206 { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR },
207 { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR },
208 { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR },
209 { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR },
210 { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR },
211 { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR },
212 { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR },
213 { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR },
214 { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR },
215 { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR },
216 { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR },
217 { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR },
218 { 0, 0 }
219};
220
221static int usblp_select_alts(struct usblp *usblp);
222static int usblp_set_protocol(struct usblp *usblp, int protocol);
223static int usblp_cache_device_id_string(struct usblp *usblp);
224
225static DECLARE_MUTEX(usblp_sem);
226
227
228
229
230
231static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
232{
233 int retval;
234 int index = usblp->ifnum;
235
236
237
238
239 if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS)) {
240 index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
241 }
242
243 retval = usb_control_msg(usblp->dev,
244 dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
245 request, type | dir | recip, value, index, buf, len, USBLP_WRITE_TIMEOUT);
246 dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d",
247 request, !!dir, recip, value, index, len, retval);
248 return retval < 0 ? retval : 0;
249}
250
251#define usblp_read_status(usblp, status)\
252 usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
253#define usblp_get_id(usblp, config, id, maxlen)\
254 usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
255#define usblp_reset(usblp)\
256 usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
257
258#define usblp_hp_channel_change_request(usblp, channel, buffer) \
259 usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
260
261
262
263
264
265
266static int proto_bias = -1;
267
268
269
270
271
272static void usblp_bulk_read(struct urb *urb)
273{
274 struct usblp *usblp = urb->context;
275
276 if (!usblp || !usblp->dev || !usblp->used || !usblp->present)
277 return;
278
279 if (unlikely(urb->status))
280 warn("usblp%d: nonzero read/write bulk status received: %d",
281 usblp->minor, urb->status);
282 usblp->rcomplete = 1;
283 wake_up_interruptible(&usblp->wait);
284}
285
286static void usblp_bulk_write(struct urb *urb)
287{
288 struct usblp *usblp = urb->context;
289
290 if (!usblp || !usblp->dev || !usblp->used || !usblp->present)
291 return;
292
293 if (unlikely(urb->status))
294 warn("usblp%d: nonzero read/write bulk status received: %d",
295 usblp->minor, urb->status);
296 usblp->wcomplete = 1;
297 wake_up_interruptible(&usblp->wait);
298}
299
300
301
302
303
304static char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
305
306static int usblp_check_status(struct usblp *usblp, int err)
307{
308 unsigned char status, newerr = 0;
309 int error;
310
311 error = usblp_read_status (usblp, usblp->statusbuf);
312 if (error < 0) {
313 err("usblp%d: error %d reading printer status",
314 usblp->minor, error);
315 return 0;
316 }
317
318 status = *usblp->statusbuf;
319
320 if (~status & LP_PERRORP)
321 newerr = 3;
322 if (status & LP_POUTPA)
323 newerr = 1;
324 if (~status & LP_PSELECD)
325 newerr = 2;
326
327 if (newerr != err)
328 info("usblp%d: %s", usblp->minor, usblp_messages[newerr]);
329
330 return newerr;
331}
332
333
334
335
336
337static int usblp_open(struct inode *inode, struct file *file)
338{
339 int minor = MINOR(inode->i_rdev) - USBLP_MINOR_BASE;
340 struct usblp *usblp;
341 int retval;
342
343 if (minor < 0 || minor >= USBLP_MINORS)
344 return -ENODEV;
345
346 down (&usblp_sem);
347 usblp = usblp_table[minor];
348
349 retval = -ENODEV;
350 if (!usblp || !usblp->dev || !usblp->present)
351 goto out;
352
353 retval = -EBUSY;
354 if (usblp->used)
355 goto out;
356
357
358
359
360
361
362#if 0
363 if ((retval = usblp_check_status(usblp, 0))) {
364 retval = retval > 1 ? -EIO : -ENOSPC;
365 goto out;
366 }
367#else
368 retval = 0;
369#endif
370
371 usblp->used = 1;
372 file->private_data = usblp;
373
374 usblp->writeurb->transfer_buffer_length = 0;
375 usblp->wcomplete = 1;
376 usblp->rcomplete = 0;
377
378 if (usblp->bidir) {
379 usblp->readcount = 0;
380 usblp->readurb->dev = usblp->dev;
381 if (usb_submit_urb(usblp->readurb) < 0) {
382 retval = -EIO;
383 usblp->used = 0;
384 file->private_data = NULL;
385 }
386 }
387out:
388 up (&usblp_sem);
389 return retval;
390}
391
392static void usblp_cleanup (struct usblp *usblp)
393{
394 devfs_unregister (usblp->devfs);
395 usblp_table [usblp->minor] = NULL;
396 info("usblp%d: removed", usblp->minor);
397
398 kfree (usblp->writebuf);
399 kfree (usblp->readbuf);
400 kfree (usblp->device_id_string);
401 kfree (usblp->statusbuf);
402 usb_free_urb(usblp->writeurb);
403 usb_free_urb(usblp->readurb);
404 kfree (usblp);
405}
406
407static void usblp_unlink_urbs(struct usblp *usblp)
408{
409 usb_unlink_urb(usblp->writeurb);
410 if (usblp->bidir)
411 usb_unlink_urb(usblp->readurb);
412}
413
414static int usblp_release(struct inode *inode, struct file *file)
415{
416 struct usblp *usblp = file->private_data;
417
418 down (&usblp_sem);
419 usblp->used = 0;
420 if (usblp->present) {
421 usblp_unlink_urbs(usblp);
422 } else
423 usblp_cleanup (usblp);
424 up (&usblp_sem);
425 return 0;
426}
427
428
429static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
430{
431 struct usblp *usblp = file->private_data;
432 poll_wait(file, &usblp->wait, wait);
433 return ((!usblp->bidir || !usblp->rcomplete) ? 0 : POLLIN | POLLRDNORM)
434 | (!usblp->wcomplete ? 0 : POLLOUT | POLLWRNORM);
435}
436
437static int usblp_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
438{
439 struct usblp *usblp = file->private_data;
440 int length, err, i;
441 unsigned char newChannel;
442 int status;
443 int twoints[2];
444 int retval = 0;
445
446 down (&usblp->sem);
447 if (!usblp->present) {
448 retval = -ENODEV;
449 goto done;
450 }
451
452 if (_IOC_TYPE(cmd) == 'P')
453
454 switch (_IOC_NR(cmd)) {
455
456 case IOCNR_GET_DEVICE_ID:
457 if (_IOC_DIR(cmd) != _IOC_READ) {
458 retval = -EINVAL;
459 goto done;
460 }
461
462 length = usblp_cache_device_id_string(usblp);
463 if (length < 0) {
464 retval = length;
465 goto done;
466 }
467 if (length > _IOC_SIZE(cmd))
468 length = _IOC_SIZE(cmd);
469
470 if (copy_to_user((unsigned char *) arg,
471 usblp->device_id_string,
472 (unsigned long) length)) {
473 retval = -EFAULT;
474 goto done;
475 }
476
477 break;
478
479 case IOCNR_GET_PROTOCOLS:
480 if (_IOC_DIR(cmd) != _IOC_READ ||
481 _IOC_SIZE(cmd) < sizeof(twoints)) {
482 retval = -EINVAL;
483 goto done;
484 }
485
486 twoints[0] = usblp->current_protocol;
487 twoints[1] = 0;
488 for (i = USBLP_FIRST_PROTOCOL;
489 i <= USBLP_LAST_PROTOCOL; i++) {
490 if (usblp->protocol[i].alt_setting >= 0)
491 twoints[1] |= (1<<i);
492 }
493
494 if (copy_to_user((unsigned char *)arg,
495 (unsigned char *)twoints,
496 sizeof(twoints))) {
497 retval = -EFAULT;
498 goto done;
499 }
500
501 break;
502
503 case IOCNR_SET_PROTOCOL:
504 if (_IOC_DIR(cmd) != _IOC_WRITE) {
505 retval = -EINVAL;
506 goto done;
507 }
508
509#ifdef DEBUG
510 if (arg == -10) {
511 usblp_dump(usblp);
512 break;
513 }
514#endif
515
516 usblp_unlink_urbs(usblp);
517 retval = usblp_set_protocol(usblp, arg);
518 if (retval < 0) {
519 usblp_set_protocol(usblp,
520 usblp->current_protocol);
521 }
522 break;
523
524 case IOCNR_HP_SET_CHANNEL:
525 if (_IOC_DIR(cmd) != _IOC_WRITE ||
526 usblp->dev->descriptor.idVendor != 0x03F0 ||
527 usblp->quirks & USBLP_QUIRK_BIDIR) {
528 retval = -EINVAL;
529 goto done;
530 }
531
532 err = usblp_hp_channel_change_request(usblp,
533 arg, &newChannel);
534 if (err < 0) {
535 err("usblp%d: error = %d setting "
536 "HP channel",
537 usblp->minor, err);
538 retval = -EIO;
539 goto done;
540 }
541
542 dbg("usblp%d requested/got HP channel %ld/%d",
543 usblp->minor, arg, newChannel);
544 break;
545
546 case IOCNR_GET_BUS_ADDRESS:
547 if (_IOC_DIR(cmd) != _IOC_READ ||
548 _IOC_SIZE(cmd) < sizeof(twoints)) {
549 retval = -EINVAL;
550 goto done;
551 }
552
553 twoints[0] = usblp->dev->bus->busnum;
554 twoints[1] = usblp->dev->devnum;
555 if (copy_to_user((unsigned char *)arg,
556 (unsigned char *)twoints,
557 sizeof(twoints))) {
558 retval = -EFAULT;
559 goto done;
560 }
561
562 dbg("usblp%d is bus=%d, device=%d",
563 usblp->minor, twoints[0], twoints[1]);
564 break;
565
566 case IOCNR_GET_VID_PID:
567 if (_IOC_DIR(cmd) != _IOC_READ ||
568 _IOC_SIZE(cmd) < sizeof(twoints)) {
569 retval = -EINVAL;
570 goto done;
571 }
572
573 twoints[0] = usblp->dev->descriptor.idVendor;
574 twoints[1] = usblp->dev->descriptor.idProduct;
575 if (copy_to_user((unsigned char *)arg,
576 (unsigned char *)twoints,
577 sizeof(twoints))) {
578 retval = -EFAULT;
579 goto done;
580 }
581
582 dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
583 usblp->minor, twoints[0], twoints[1]);
584 break;
585
586 default:
587 retval = -ENOTTY;
588 }
589 else
590 switch (cmd) {
591
592 case LPGETSTATUS:
593 if (usblp_read_status(usblp, usblp->statusbuf)) {
594 err("usblp%d: failed reading printer status", usblp->minor);
595 retval = -EIO;
596 goto done;
597 }
598 status = *usblp->statusbuf;
599 if (copy_to_user ((int *)arg, &status, sizeof(int)))
600 retval = -EFAULT;
601 break;
602
603 default:
604 retval = -ENOTTY;
605 }
606
607done:
608 up (&usblp->sem);
609 return retval;
610}
611
612static ssize_t usblp_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
613{
614 DECLARE_WAITQUEUE(wait, current);
615 struct usblp *usblp = file->private_data;
616 int timeout, err = 0, transfer_length = 0;
617 size_t writecount = 0;
618
619 while (writecount < count) {
620 if (!usblp->wcomplete) {
621 barrier();
622 if (file->f_flags & O_NONBLOCK) {
623 writecount += transfer_length;
624 return writecount ? writecount : -EAGAIN;
625 }
626
627 timeout = USBLP_WRITE_TIMEOUT;
628 add_wait_queue(&usblp->wait, &wait);
629 while ( 1==1 ) {
630
631 if (signal_pending(current)) {
632 remove_wait_queue(&usblp->wait, &wait);
633 return writecount ? writecount : -EINTR;
634 }
635 set_current_state(TASK_INTERRUPTIBLE);
636 if (timeout && !usblp->wcomplete) {
637 timeout = schedule_timeout(timeout);
638 } else {
639 set_current_state(TASK_RUNNING);
640 break;
641 }
642 }
643 remove_wait_queue(&usblp->wait, &wait);
644 }
645
646 down (&usblp->sem);
647 if (!usblp->present) {
648 up (&usblp->sem);
649 return -ENODEV;
650 }
651
652 if (usblp->writeurb->status != 0) {
653 if (usblp->quirks & USBLP_QUIRK_BIDIR) {
654 if (!usblp->wcomplete)
655 err("usblp%d: error %d writing to printer",
656 usblp->minor, usblp->writeurb->status);
657 err = usblp->writeurb->status;
658 } else
659 err = usblp_check_status(usblp, err);
660 up (&usblp->sem);
661
662
663
664
665 schedule ();
666 continue;
667 }
668
669
670
671
672
673 writecount += transfer_length;
674 if (writecount == count) {
675 up (&usblp->sem);
676 break;
677 }
678
679 transfer_length = count - writecount;
680 if(transfer_length > USBLP_BUF_SIZE)
681 transfer_length = USBLP_BUF_SIZE;
682
683 usblp->writeurb->transfer_buffer_length = transfer_length;
684
685 if (copy_from_user(usblp->writeurb->transfer_buffer,
686 buffer + writecount, transfer_length)) {
687 up(&usblp->sem);
688 return writecount ? writecount : -EFAULT;
689 }
690
691 usblp->writeurb->dev = usblp->dev;
692 usblp->wcomplete = 0;
693 err = usb_submit_urb(usblp->writeurb);
694 if (err) {
695 if (err != -ENOMEM)
696 count = -EIO;
697 else
698 count = writecount ? writecount : -ENOMEM;
699 up (&usblp->sem);
700 break;
701 }
702 up (&usblp->sem);
703 }
704
705 return count;
706}
707
708static ssize_t usblp_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
709{
710 struct usblp *usblp = file->private_data;
711 DECLARE_WAITQUEUE(wait, current);
712
713 if (!usblp->bidir)
714 return -EINVAL;
715
716 down (&usblp->sem);
717 if (!usblp->present) {
718 count = -ENODEV;
719 goto done;
720 }
721
722 if (!usblp->rcomplete) {
723 barrier();
724
725 if (file->f_flags & O_NONBLOCK) {
726 count = -EAGAIN;
727 goto done;
728 }
729
730 add_wait_queue(&usblp->wait, &wait);
731 while (1==1) {
732 if (signal_pending(current)) {
733 count = -EINTR;
734 remove_wait_queue(&usblp->wait, &wait);
735 goto done;
736 }
737 up (&usblp->sem);
738 set_current_state(TASK_INTERRUPTIBLE);
739 if (!usblp->rcomplete) {
740 schedule();
741 } else {
742 set_current_state(TASK_RUNNING);
743 break;
744 }
745 down (&usblp->sem);
746 }
747 remove_wait_queue(&usblp->wait, &wait);
748 }
749
750 if (!usblp->present) {
751 count = -ENODEV;
752 goto done;
753 }
754
755 if (usblp->readurb->status) {
756 err("usblp%d: error %d reading from printer",
757 usblp->minor, usblp->readurb->status);
758 usblp->readurb->dev = usblp->dev;
759 usblp->readcount = 0;
760 usblp->rcomplete = 0;
761 if (usb_submit_urb(usblp->readurb) < 0)
762 dbg("error submitting urb");
763 count = -EIO;
764 goto done;
765 }
766
767 count = count < usblp->readurb->actual_length - usblp->readcount ?
768 count : usblp->readurb->actual_length - usblp->readcount;
769
770 if (copy_to_user(buffer, usblp->readurb->transfer_buffer + usblp->readcount, count)) {
771 count = -EFAULT;
772 goto done;
773 }
774
775 if ((usblp->readcount += count) == usblp->readurb->actual_length) {
776 usblp->readcount = 0;
777 usblp->readurb->dev = usblp->dev;
778 usblp->rcomplete = 0;
779 if (usb_submit_urb(usblp->readurb)) {
780 count = -EIO;
781 goto done;
782 }
783 }
784
785done:
786 up (&usblp->sem);
787 return count;
788}
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807static unsigned int usblp_quirks (__u16 vendor, __u16 product)
808{
809 int i;
810
811 for (i = 0; quirk_printers[i].vendorId; i++) {
812 if (vendor == quirk_printers[i].vendorId &&
813 product == quirk_printers[i].productId)
814 return quirk_printers[i].quirks;
815 }
816 return 0;
817}
818
819static struct file_operations usblp_fops = {
820 owner: THIS_MODULE,
821 read: usblp_read,
822 write: usblp_write,
823 poll: usblp_poll,
824 ioctl: usblp_ioctl,
825 open: usblp_open,
826 release: usblp_release,
827};
828
829static void *usblp_probe(struct usb_device *dev, unsigned int ifnum,
830 const struct usb_device_id *id)
831{
832 struct usblp *usblp = 0;
833 int protocol;
834 char name[6];
835
836
837
838 if (!(usblp = kmalloc(sizeof(struct usblp), GFP_KERNEL))) {
839 err("out of memory for usblp");
840 goto abort;
841 }
842 memset(usblp, 0, sizeof(struct usblp));
843 usblp->dev = dev;
844 init_MUTEX (&usblp->sem);
845 init_waitqueue_head(&usblp->wait);
846 usblp->ifnum = ifnum;
847
848
849 while (usblp_table[usblp->minor]) {
850 usblp->minor++;
851 if (usblp->minor >= USBLP_MINORS) {
852 err("no more free usblp devices");
853 goto abort;
854 }
855 }
856
857 usblp->writeurb = usb_alloc_urb(0);
858 if (!usblp->writeurb) {
859 err("out of memory");
860 goto abort;
861 }
862 usblp->readurb = usb_alloc_urb(0);
863 if (!usblp->readurb) {
864 err("out of memory");
865 goto abort;
866 }
867
868
869
870
871 if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
872 err("out of memory for device_id_string");
873 goto abort;
874 }
875
876 usblp->writebuf = usblp->readbuf = NULL;
877
878
879
880 if (!(usblp->writebuf = kmalloc(USBLP_BUF_SIZE, GFP_KERNEL))) {
881 err("out of memory for write buf");
882 goto abort;
883 }
884 if (!(usblp->readbuf = kmalloc(USBLP_BUF_SIZE, GFP_KERNEL))) {
885 err("out of memory for read buf");
886 goto abort;
887 }
888
889
890 usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
891 if (!usblp->statusbuf) {
892 err("out of memory for statusbuf");
893 goto abort;
894 }
895
896
897 usblp->quirks = usblp_quirks(
898 dev->descriptor.idVendor,
899 dev->descriptor.idProduct);
900
901
902 protocol = usblp_select_alts(usblp);
903 if (protocol < 0) {
904 dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
905 dev->descriptor.idVendor,
906 dev->descriptor.idProduct);
907 goto abort;
908 }
909
910
911 if (usblp_set_protocol(usblp, protocol) < 0)
912 goto abort;
913
914
915 usblp_cache_device_id_string(usblp);
916
917#ifdef DEBUG
918 usblp_check_status(usblp, 0);
919#endif
920
921 usblp_table[usblp->minor] = usblp;
922
923 sprintf(name, "lp%d", usblp->minor);
924 usblp->devfs = devfs_register(usb_devfs_handle, name,
925 DEVFS_FL_DEFAULT, USB_MAJOR,
926 USBLP_MINOR_BASE + usblp->minor,
927 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP |
928 S_IWGRP, &usblp_fops, NULL);
929
930 info("usblp%d: USB %sdirectional printer dev %d "
931 "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
932 usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
933 usblp->ifnum,
934 usblp->protocol[usblp->current_protocol].alt_setting,
935 usblp->current_protocol, usblp->dev->descriptor.idVendor,
936 usblp->dev->descriptor.idProduct);
937
938 usblp->present = 1;
939
940 return usblp;
941
942abort:
943 if (usblp) {
944 if (usblp->writebuf)
945 kfree (usblp->writebuf);
946 if (usblp->readbuf)
947 kfree (usblp->readbuf);
948 kfree(usblp->statusbuf);
949 kfree(usblp->device_id_string);
950 usb_free_urb(usblp->writeurb);
951 usb_free_urb(usblp->readurb);
952 kfree(usblp);
953 }
954 return NULL;
955}
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976static int usblp_select_alts(struct usblp *usblp)
977{
978 struct usb_interface *if_alt;
979 struct usb_interface_descriptor *ifd;
980 struct usb_endpoint_descriptor *epd, *epwrite, *epread;
981 int p, i, e;
982
983 if_alt = &usblp->dev->actconfig->interface[usblp->ifnum];
984
985 for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
986 usblp->protocol[p].alt_setting = -1;
987
988
989 for (i = 0; i < if_alt->num_altsetting; i++) {
990 ifd = &if_alt->altsetting[i];
991
992 if (ifd->bInterfaceClass != 7 || ifd->bInterfaceSubClass != 1)
993 continue;
994
995 if (ifd->bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
996 ifd->bInterfaceProtocol > USBLP_LAST_PROTOCOL)
997 continue;
998
999
1000 epwrite = epread = 0;
1001 for (e = 0; e < ifd->bNumEndpoints; e++) {
1002 epd = &ifd->endpoint[e];
1003
1004 if ((epd->bmAttributes&USB_ENDPOINT_XFERTYPE_MASK)!=
1005 USB_ENDPOINT_XFER_BULK)
1006 continue;
1007
1008 if (!(epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK)) {
1009 if (!epwrite) epwrite=epd;
1010
1011 } else {
1012 if (!epread) epread=epd;
1013 }
1014 }
1015
1016
1017 if (!epwrite || (ifd->bInterfaceProtocol > 1 && !epread))
1018 continue;
1019
1020
1021
1022 if (ifd->bInterfaceProtocol == 1) {
1023 epread = NULL;
1024 } else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
1025 info("Disabling reads from problem bidirectional "
1026 "printer on usblp%d", usblp->minor);
1027 epread = NULL;
1028 }
1029
1030 usblp->protocol[ifd->bInterfaceProtocol].alt_setting = i;
1031 usblp->protocol[ifd->bInterfaceProtocol].epwrite = epwrite;
1032 usblp->protocol[ifd->bInterfaceProtocol].epread = epread;
1033 }
1034
1035
1036 if (proto_bias >= USBLP_FIRST_PROTOCOL &&
1037 proto_bias <= USBLP_LAST_PROTOCOL &&
1038 usblp->protocol[proto_bias].alt_setting != -1)
1039 return proto_bias;
1040
1041
1042 if (usblp->protocol[2].alt_setting != -1) return 2;
1043 if (usblp->protocol[1].alt_setting != -1) return 1;
1044 if (usblp->protocol[3].alt_setting != -1) return 3;
1045
1046
1047 return -1;
1048}
1049
1050static int usblp_set_protocol(struct usblp *usblp, int protocol)
1051{
1052 int r, alts;
1053
1054 if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
1055 return -EINVAL;
1056
1057 alts = usblp->protocol[protocol].alt_setting;
1058 if (alts < 0) return -EINVAL;
1059 r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
1060 if (r < 0) {
1061 err("can't set desired altsetting %d on interface %d",
1062 alts, usblp->ifnum);
1063 return r;
1064 }
1065
1066 usb_fill_bulk_urb(usblp->writeurb, usblp->dev,
1067 usb_sndbulkpipe(usblp->dev,
1068 usblp->protocol[protocol].epwrite->bEndpointAddress),
1069 usblp->writebuf, 0,
1070 usblp_bulk_write, usblp);
1071
1072 usblp->bidir = (usblp->protocol[protocol].epread != 0);
1073 if (usblp->bidir)
1074 usb_fill_bulk_urb(usblp->readurb, usblp->dev,
1075 usb_rcvbulkpipe(usblp->dev,
1076 usblp->protocol[protocol].epread->bEndpointAddress),
1077 usblp->readbuf, USBLP_BUF_SIZE,
1078 usblp_bulk_read, usblp);
1079
1080 usblp->current_protocol = protocol;
1081 dbg("usblp%d set protocol %d", usblp->minor, protocol);
1082 return 0;
1083}
1084
1085
1086
1087
1088static int usblp_cache_device_id_string(struct usblp *usblp)
1089{
1090 int err, length;
1091
1092 err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
1093 if (err < 0) {
1094 dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
1095 usblp->minor, err);
1096 usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
1097 return -EIO;
1098 }
1099
1100
1101
1102
1103 length = (usblp->device_id_string[0] << 8) + usblp->device_id_string[1];
1104 if (length < 2)
1105 length = 2;
1106 else if (length >= USBLP_DEVICE_ID_SIZE)
1107 length = USBLP_DEVICE_ID_SIZE - 1;
1108 usblp->device_id_string[length] = '\0';
1109
1110 dbg("usblp%d Device ID string [len=%d]=\"%s\"",
1111 usblp->minor, length, &usblp->device_id_string[2]);
1112
1113 return length;
1114}
1115
1116static void usblp_disconnect(struct usb_device *dev, void *ptr)
1117{
1118 struct usblp *usblp = ptr;
1119
1120 if (!usblp || !usblp->dev) {
1121 err("bogus disconnect");
1122 BUG ();
1123 }
1124
1125 down (&usblp_sem);
1126 down (&usblp->sem);
1127 usblp->present = 0;
1128
1129 usblp_unlink_urbs(usblp);
1130 up (&usblp->sem);
1131
1132 if (!usblp->used)
1133 usblp_cleanup (usblp);
1134 up (&usblp_sem);
1135}
1136
1137static struct usb_device_id usblp_ids [] = {
1138 { USB_DEVICE_INFO(7, 1, 1) },
1139 { USB_DEVICE_INFO(7, 1, 2) },
1140 { USB_DEVICE_INFO(7, 1, 3) },
1141 { USB_INTERFACE_INFO(7, 1, 1) },
1142 { USB_INTERFACE_INFO(7, 1, 2) },
1143 { USB_INTERFACE_INFO(7, 1, 3) },
1144 { }
1145};
1146
1147MODULE_DEVICE_TABLE (usb, usblp_ids);
1148
1149static struct usb_driver usblp_driver = {
1150 name: "usblp",
1151 probe: usblp_probe,
1152 disconnect: usblp_disconnect,
1153 fops: &usblp_fops,
1154 minor: USBLP_MINOR_BASE,
1155 id_table: usblp_ids,
1156};
1157
1158static int __init usblp_init(void)
1159{
1160 if (usb_register(&usblp_driver))
1161 return -1;
1162 info(DRIVER_VERSION ": " DRIVER_DESC);
1163 return 0;
1164}
1165
1166static void __exit usblp_exit(void)
1167{
1168 usb_deregister(&usblp_driver);
1169}
1170
1171module_init(usblp_init);
1172module_exit(usblp_exit);
1173
1174MODULE_AUTHOR( DRIVER_AUTHOR );
1175MODULE_DESCRIPTION( DRIVER_DESC );
1176MODULE_PARM(proto_bias, "i");
1177MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
1178MODULE_LICENSE("GPL");
1179