2 /* Driver for USB Printers
4 * Copyright 1999 Michael Gee (michael@linuxspecific.com)
5 * Copyright 1999 Pavel Machek (pavel@suse.cz)
7 * Distribute under GPL version 2 or later.
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/signal.h>
14 #include <linux/errno.h>
15 #include <linux/miscdevice.h>
16 #include <linux/random.h>
17 #include <linux/poll.h>
18 #include <linux/init.h>
19 #include <linux/malloc.h>
21 #include <linux/spinlock.h>
25 #define NAK_TIMEOUT (HZ) /* stall wait for printer */
26 #define MAX_RETRY_COUNT ((60*60*HZ)/NAK_TIMEOUT) /* should not take 1 minute a page! */
28 #define BIG_BUF_SIZE 8192
31 * USB Printer Requests
33 #define USB_PRINTER_REQ_GET_DEVICE_ID 0
34 #define USB_PRINTER_REQ_GET_PORT_STATUS 1
35 #define USB_PRINTER_REQ_SOFT_RESET 2
37 #define MAX_PRINTERS 8
40 struct usb_device
*pusb_dev
;
41 __u8 isopen
; /* True if open */
42 __u8 noinput
; /* True if no input stream */
43 __u8 minor
; /* minor number of device */
44 __u8 status
; /* last status from device */
45 int maxin
, maxout
; /* max transfer size in and out */
46 char *obuf
; /* transfer buffer (out only) */
47 wait_queue_head_t wait_q
; /* for timeouts */
48 unsigned int last_error
; /* save for checking */
49 int bulk_in_ep
; /* Bulk IN endpoint */
50 int bulk_out_ep
; /* Bulk OUT endpoint */
51 int bulk_in_index
; /* endpoint[bulk_in_index] */
52 int bulk_out_index
; /* endpoint[bulk_out_index] */
55 static struct pp_usb_data
*minor_data
[MAX_PRINTERS
];
57 #define PPDATA(x) ((struct pp_usb_data *)(x))
59 static unsigned char printer_read_status(struct pp_usb_data
*p
)
62 struct usb_device
*dev
= p
->pusb_dev
;
64 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
,0),
65 USB_PRINTER_REQ_GET_PORT_STATUS
,
66 USB_TYPE_CLASS
| USB_RT_INTERFACE
| USB_DIR_IN
,
67 0, 0, &status
, 1, HZ
)) {
73 static int printer_check_status(struct pp_usb_data
*p
)
75 unsigned int last
= p
->last_error
;
76 unsigned char status
= printer_read_status(p
);
78 if (status
& LP_PERRORP
)
81 else if ((status
& LP_POUTPA
)) {
82 if (last
!= LP_POUTPA
) {
84 printk(KERN_INFO
"usblp%d out of paper\n", p
->minor
);
86 } else if (!(status
& LP_PSELECD
)) {
87 if (last
!= LP_PSELECD
) {
89 printk(KERN_INFO
"usblp%d off-line\n", p
->minor
);
92 if (last
!= LP_PERRORP
) {
94 printk(KERN_INFO
"usblp%d on fire\n", p
->minor
);
103 static void printer_reset(struct pp_usb_data
*p
)
105 struct usb_device
*dev
= p
->pusb_dev
;
107 usb_control_msg(dev
, usb_sndctrlpipe(dev
,0),
108 USB_PRINTER_REQ_SOFT_RESET
,
109 USB_TYPE_CLASS
| USB_RECIP_OTHER
,
113 static int open_printer(struct inode
* inode
, struct file
* file
)
115 struct pp_usb_data
*p
;
117 if (MINOR(inode
->i_rdev
) >= MAX_PRINTERS
||
118 !minor_data
[MINOR(inode
->i_rdev
)]) {
122 p
= minor_data
[MINOR(inode
->i_rdev
)];
123 p
->minor
= MINOR(inode
->i_rdev
);
128 if (!(p
->obuf
= (char *)__get_free_page(GFP_KERNEL
))) {
133 printer_check_status(p
);
135 file
->private_data
= p
;
137 init_waitqueue_head(&p
->wait_q
);
143 static int close_printer(struct inode
* inode
, struct file
* file
)
145 struct pp_usb_data
*p
= file
->private_data
;
147 free_page((unsigned long)p
->obuf
);
149 file
->private_data
= NULL
;
150 /* free the resources if the printer is no longer around */
152 minor_data
[p
->minor
] = NULL
;
159 static ssize_t
write_printer(struct file
* file
,
160 const char * buffer
, size_t count
, loff_t
*ppos
)
162 struct pp_usb_data
*p
= file
->private_data
;
163 unsigned long copy_size
;
164 unsigned long bytes_written
= 0;
165 unsigned long partial
;
166 int result
= USB_ST_NOERROR
;
170 char *obuf
= p
->obuf
;
171 unsigned long thistime
;
173 thistime
= copy_size
= (count
> p
->maxout
) ? p
->maxout
: count
;
174 if (copy_from_user(p
->obuf
, buffer
, copy_size
))
176 maxretry
= MAX_RETRY_COUNT
;
180 if (signal_pending(current
)) {
181 return bytes_written
? bytes_written
: -EINTR
;
183 result
= p
->pusb_dev
->bus
->op
->bulk_msg(p
->pusb_dev
,
184 usb_sndbulkpipe(p
->pusb_dev
, p
->bulk_out_ep
),
185 obuf
, thistime
, &partial
, HZ
*20);
189 maxretry
= MAX_RETRY_COUNT
;
191 if (result
== USB_ST_TIMEOUT
) { /* NAK - so hold for a while */
194 interruptible_sleep_on_timeout(&p
->wait_q
, NAK_TIMEOUT
);
196 } else if (!result
&& !partial
) {
201 /* whoops - let's reset and fail the request */
202 // printk("Whoops - %x\n", result);
204 interruptible_sleep_on_timeout(&p
->wait_q
, 5*HZ
); /* let reset do its stuff */
207 bytes_written
+= copy_size
;
210 } while ( count
> 0 );
212 return bytes_written
? bytes_written
: -EIO
;
215 static ssize_t
read_printer(struct file
* file
,
216 char * buffer
, size_t count
, loff_t
*ppos
)
218 struct pp_usb_data
*p
= file
->private_data
;
222 unsigned long partial
;
229 if (signal_pending(current
)) {
230 return read_count
? read_count
: -EINTR
;
234 this_read
= (count
> sizeof(buf
)) ? sizeof(buf
) : count
;
236 result
= p
->pusb_dev
->bus
->op
->bulk_msg(p
->pusb_dev
,
237 usb_rcvbulkpipe(p
->pusb_dev
, p
->bulk_in_ep
),
238 buf
, this_read
, &partial
, HZ
*20);
240 /* unlike writes, we don't retry a NAK, just stop now */
241 if (!result
& partial
)
242 count
= this_read
= partial
;
247 if (copy_to_user(buffer
, buf
, this_read
))
250 read_count
+= this_read
;
257 static void * printer_probe(struct usb_device
*dev
, unsigned int ifnum
)
259 struct usb_interface_descriptor
*interface
;
260 struct pp_usb_data
*pp
;
264 * FIXME - this will not cope with combined printer/scanners
266 if ((dev
->descriptor
.bDeviceClass
!= USB_CLASS_PRINTER
&&
267 dev
->descriptor
.bDeviceClass
!= 0) ||
268 dev
->descriptor
.bNumConfigurations
!= 1 ||
269 dev
->actconfig
->bNumInterfaces
!= 1) {
273 interface
= &dev
->actconfig
->interface
[ifnum
].altsetting
[0];
275 /* Let's be paranoid (for the moment). */
276 if (interface
->bInterfaceClass
!= USB_CLASS_PRINTER
||
277 interface
->bInterfaceSubClass
!= 1 ||
278 (interface
->bInterfaceProtocol
!= 2 && interface
->bInterfaceProtocol
!= 1) ||
279 interface
->bNumEndpoints
> 2) {
283 /* Does this (these) interface(s) support bulk transfers? */
284 if ((interface
->endpoint
[0].bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
285 != USB_ENDPOINT_XFER_BULK
) {
288 if ((interface
->bNumEndpoints
> 1) &&
289 ((interface
->endpoint
[1].bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
290 != USB_ENDPOINT_XFER_BULK
)) {
295 * Does this interface have at least one OUT endpoint
296 * that we can write to: endpoint index 0 or 1?
298 if ((interface
->endpoint
[0].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
300 (interface
->bNumEndpoints
> 1 &&
301 (interface
->endpoint
[1].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
306 for (i
=0; i
<MAX_PRINTERS
; i
++) {
310 if (i
>= MAX_PRINTERS
) {
311 printk("No minor table space available for USB Printer\n");
315 printk(KERN_INFO
"USB Printer found at address %d\n", dev
->devnum
);
317 if (!(pp
= kmalloc(sizeof(struct pp_usb_data
), GFP_KERNEL
))) {
318 printk(KERN_DEBUG
"usb_printer: no memory!\n");
322 memset(pp
, 0, sizeof(struct pp_usb_data
));
323 minor_data
[i
] = PPDATA(pp
);
324 minor_data
[i
]->minor
= i
;
325 minor_data
[i
]->pusb_dev
= dev
;
326 minor_data
[i
]->maxout
= (BIG_BUF_SIZE
> PAGE_SIZE
) ? PAGE_SIZE
: BIG_BUF_SIZE
;
327 if (interface
->bInterfaceProtocol
!= 2) /* if not bidirectional */
328 minor_data
[i
]->noinput
= 1;
330 minor_data
[i
]->bulk_out_index
=
331 ((interface
->endpoint
[0].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
332 == USB_DIR_OUT
) ? 0 : 1;
333 minor_data
[i
]->bulk_in_index
= minor_data
[i
]->noinput
? -1 :
334 (minor_data
[i
]->bulk_out_index
== 0) ? 1 : 0;
335 minor_data
[i
]->bulk_in_ep
= minor_data
[i
]->noinput
? -1 :
336 interface
->endpoint
[minor_data
[i
]->bulk_in_index
].bEndpointAddress
&
337 USB_ENDPOINT_NUMBER_MASK
;
338 minor_data
[i
]->bulk_out_ep
=
339 interface
->endpoint
[minor_data
[i
]->bulk_out_index
].bEndpointAddress
&
340 USB_ENDPOINT_NUMBER_MASK
;
341 if (interface
->bInterfaceProtocol
== 2) { /* if bidirectional */
342 minor_data
[i
]->maxin
=
343 interface
->endpoint
[minor_data
[i
]->bulk_in_index
].wMaxPacketSize
;
346 printk(KERN_INFO
"USB Printer Summary:\n");
347 printk(KERN_INFO
"index=%d, maxout=%d, noinput=%d\n",
348 i
, minor_data
[i
]->maxout
, minor_data
[i
]->noinput
);
349 printk(KERN_INFO
"bulk_in_ix=%d, bulk_in_ep=%d, bulk_out_ix=%d, bulk_out_ep=%d\n",
350 minor_data
[i
]->bulk_in_index
,
351 minor_data
[i
]->bulk_in_ep
,
352 minor_data
[i
]->bulk_out_index
,
353 minor_data
[i
]->bulk_out_ep
);
360 /* Let's get the device id if possible. */
361 if (usb_control_msg(dev
, usb_rcvctrlpipe(dev
,0),
362 USB_PRINTER_REQ_GET_DEVICE_ID
,
363 USB_TYPE_CLASS
| USB_RT_INTERFACE
| USB_DIR_IN
,
365 sizeof(ieee_id
)-1, HZ
) == 0) {
366 if (ieee_id
[1] < sizeof(ieee_id
) - 1)
367 ieee_id
[ieee_id
[1]+2] = '\0';
369 ieee_id
[sizeof(ieee_id
)-1] = '\0';
370 printk(KERN_INFO
" USB Printer ID is %s\n",
373 status
= printer_read_status(PPDATA(pp
));
374 printk(KERN_INFO
" Status is %s,%s,%s\n",
375 (status
& LP_PSELECD
) ? "Selected" : "Not Selected",
376 (status
& LP_POUTPA
) ? "No Paper" : "Paper",
377 (status
& LP_PERRORP
) ? "No Error" : "Error");
383 static void printer_disconnect(struct usb_device
*dev
, void *ptr
)
385 struct pp_usb_data
*pp
= ptr
;
388 /* better let it finish - the release will do whats needed */
392 minor_data
[pp
->minor
] = NULL
;
396 static struct file_operations usb_printer_fops
= {
401 NULL
, /* poll - out for the moment */
411 static struct usb_driver printer_driver
= {
420 int usb_printer_init(void)
422 if (usb_register(&printer_driver
)) {
423 printk(KERN_ERR
"USB Printer driver cannot register: "
424 "minor number %d already in use\n",
425 printer_driver
.minor
);
429 printk(KERN_INFO
"USB Printer support registered.\n");
434 int init_module(void)
437 return usb_printer_init();
440 void cleanup_module(void)
442 usb_deregister(&printer_driver
);