2 /* Driver for USB Printers
4 * (C) Michael Gee (michael@linuxspecific.com) 1999
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/signal.h>
12 #include <linux/errno.h>
13 #include <linux/miscdevice.h>
14 #include <linux/random.h>
15 #include <linux/poll.h>
16 #include <linux/init.h>
17 #include <linux/malloc.h>
20 #include <asm/spinlock.h>
24 #define NAK_TIMEOUT (HZ) /* stall wait for printer */
25 #define MAX_RETRY_COUNT ((60*60*HZ)/NAK_TIMEOUT) /* should not take 1 minute a page! */
27 #ifndef USB_PRINTER_MAJOR
28 #define USB_PRINTER_MAJOR 0
31 static int mymajor
= USB_PRINTER_MAJOR
;
33 #define MAX_PRINTERS 8
36 struct usb_device
*pusb_dev
;
37 __u8 isopen
; /* nz if open */
38 __u8 noinput
; /* nz if no input stream */
39 __u8 minor
; /* minor number of device */
40 __u8 status
; /* last status from device */
41 int maxin
, maxout
; /* max transfer size in and out */
42 char *obuf
; /* transfer buffer (out only) */
43 wait_queue_head_t wait_q
; /* for timeouts */
44 unsigned int last_error
; /* save for checking */
47 static struct pp_usb_data
*minor_data
[MAX_PRINTERS
];
49 #define PPDATA(x) ((struct pp_usb_data *)(x))
51 unsigned char printer_read_status(struct pp_usb_data
*p
)
55 struct usb_device
*dev
= p
->pusb_dev
;
57 dr
.requesttype
= USB_TYPE_CLASS
| USB_RT_INTERFACE
| 0x80;
62 if (dev
->bus
->op
->control_msg(dev
, usb_rcvctrlpipe(dev
,0), &dr
, &status
, 1)) {
68 static int printer_check_status(struct pp_usb_data
*p
)
70 unsigned int last
= p
->last_error
;
71 unsigned char status
= printer_read_status(p
);
73 if (status
& LP_PERRORP
)
76 else if ((status
& LP_POUTPA
)) {
77 if (last
!= LP_POUTPA
) {
79 printk(KERN_INFO
"usblp%d out of paper\n", p
->minor
);
81 } else if (!(status
& LP_PSELECD
)) {
82 if (last
!= LP_PSELECD
) {
84 printk(KERN_INFO
"usblp%d off-line\n", p
->minor
);
87 if (last
!= LP_PERRORP
) {
89 printk(KERN_INFO
"usblp%d on fire\n", p
->minor
);
98 void printer_reset(struct pp_usb_data
*p
)
101 struct usb_device
*dev
= p
->pusb_dev
;
103 dr
.requesttype
= USB_TYPE_CLASS
| USB_RECIP_OTHER
;
108 dev
->bus
->op
->control_msg(dev
, usb_sndctrlpipe(dev
,0), &dr
, NULL
, 0);
111 static int open_printer(struct inode
* inode
, struct file
* file
)
113 struct pp_usb_data
*p
;
115 if(MINOR(inode
->i_rdev
) >= MAX_PRINTERS
||
116 !minor_data
[MINOR(inode
->i_rdev
)]) {
120 p
= minor_data
[MINOR(inode
->i_rdev
)];
121 p
->minor
= MINOR(inode
->i_rdev
);
126 if (!(p
->obuf
= (char *)__get_free_page(GFP_KERNEL
))) {
130 printer_check_status(p
);
133 file
->private_data
= p
;
135 init_waitqueue_head(&p
->wait_q
);
139 static int close_printer(struct inode
* inode
, struct file
* file
)
141 struct pp_usb_data
*p
= file
->private_data
;
143 free_page((unsigned long)p
->obuf
);
145 file
->private_data
= NULL
;
147 minor_data
[p
->minor
] = NULL
;
156 static ssize_t
write_printer(struct file
* file
,
157 const char * buffer
, size_t count
, loff_t
*ppos
)
159 struct pp_usb_data
*p
= file
->private_data
;
160 unsigned long copy_size
;
161 unsigned long bytes_written
= 0;
162 unsigned long partial
;
167 char *obuf
= p
->obuf
;
168 unsigned long thistime
;
170 thistime
= copy_size
= (count
> p
->maxout
) ? p
->maxout
: count
;
171 if (copy_from_user(p
->obuf
, buffer
, copy_size
))
173 maxretry
= MAX_RETRY_COUNT
;
177 if (signal_pending(current
)) {
178 return bytes_written
? bytes_written
: -EINTR
;
180 result
= p
->pusb_dev
->bus
->op
->bulk_msg(p
->pusb_dev
,
181 usb_sndbulkpipe(p
->pusb_dev
, 1), obuf
, thistime
, &partial
);
182 if (result
& 0x08) { /* NAK - so hold for a while */
187 interruptible_sleep_on_timeout(&p
->wait_q
, NAK_TIMEOUT
);
193 /* whoops - let's reset and fail the request */
194 // printk("Whoops - %x\n", result);
196 interruptible_sleep_on_timeout(&p
->wait_q
, 5*HZ
); /* let reset do its stuff */
199 bytes_written
+= copy_size
;
202 } while ( count
> 0 );
204 return bytes_written
? bytes_written
: -EIO
;
207 static ssize_t
read_printer(struct file
* file
,
208 char * buffer
, size_t count
, loff_t
*ppos
)
210 struct pp_usb_data
*p
= file
->private_data
;
214 unsigned long partial
;
222 if (signal_pending(current
)) {
223 return read_count
? read_count
: -EINTR
;
227 this_read
= (count
> sizeof(buf
)) ? sizeof(buf
) : count
;
229 result
= p
->pusb_dev
->bus
->op
->bulk_msg(p
->pusb_dev
,
230 usb_rcvbulkpipe(p
->pusb_dev
, 2), buf
, this_read
, &partial
);
232 /* unlike writes, we don't retry a NAK, just stop now */
234 count
= this_read
= partial
;
239 if (copy_to_user(buffer
, p
->obuf
, this_read
))
242 read_count
+= this_read
;
249 static int printer_probe(struct usb_device
*dev
)
251 struct usb_interface_descriptor
*interface
;
255 * FIXME - this will not cope with combined printer/scanners
257 if (dev
->descriptor
.bDeviceClass
!= 7 ||
258 dev
->descriptor
.bNumConfigurations
!= 1 ||
259 dev
->config
[0].bNumInterfaces
!= 1) {
263 interface
= dev
->config
->interface
;
265 /* Lets be paranoid (for the moment)*/
266 if (interface
->bInterfaceClass
!= 7 ||
267 interface
->bInterfaceSubClass
!= 1 ||
268 (interface
->bInterfaceProtocol
!= 2 && interface
->bInterfaceProtocol
!= 1)||
269 interface
->bNumEndpoints
> 2) {
273 if (interface
->endpoint
[0].bEndpointAddress
!= 0x01 ||
274 interface
->endpoint
[0].bmAttributes
!= 0x02 ||
275 (interface
->bNumEndpoints
> 1 && (
276 interface
->endpoint
[1].bEndpointAddress
!= 0x82 ||
277 interface
->endpoint
[1].bmAttributes
!= 0x02))) {
281 for (i
=0; i
<MAX_PRINTERS
; i
++) {
285 if (i
>= MAX_PRINTERS
) {
289 printk(KERN_INFO
"USB Printer found at address %d\n", dev
->devnum
);
291 if (!(dev
->private = kmalloc(sizeof(struct pp_usb_data
), GFP_KERNEL
))) {
292 printk( KERN_DEBUG
"usb_printer: no memory!\n");
296 memset(dev
->private, 0, sizeof(struct pp_usb_data
));
297 minor_data
[i
] = PPDATA(dev
->private);
298 minor_data
[i
]->minor
= i
;
299 minor_data
[i
]->pusb_dev
= dev
;
300 minor_data
[i
]->maxout
= interface
->endpoint
[0].wMaxPacketSize
* 16;
301 if (minor_data
[i
]->maxout
> PAGE_SIZE
) {
302 minor_data
[i
]->maxout
= PAGE_SIZE
;
304 if (interface
->bInterfaceProtocol
!= 2)
305 minor_data
[i
]->noinput
= 1;
307 minor_data
[i
]->maxin
= interface
->endpoint
[1].wMaxPacketSize
;
310 if (usb_set_configuration(dev
, dev
->config
[0].bConfigurationValue
)) {
311 printk(KERN_INFO
" Failed to set configuration\n");
320 /* Lets get the device id if possible */
321 dr
.requesttype
= USB_TYPE_CLASS
| USB_RT_INTERFACE
| 0x80;
325 dr
.length
= sizeof(ieee_id
) - 1;
326 if (dev
->bus
->op
->control_msg(dev
, usb_rcvctrlpipe(dev
,0), &dr
, ieee_id
, sizeof(ieee_id
)-1) == 0) {
327 if (ieee_id
[1] < sizeof(ieee_id
) - 1)
328 ieee_id
[ieee_id
[1]+2] = '\0';
330 ieee_id
[sizeof(ieee_id
)-1] = '\0';
331 printk(KERN_INFO
" Printer ID is %s\n", &ieee_id
[2]);
333 status
= printer_read_status(PPDATA(dev
->private));
334 printk(KERN_INFO
" Status is %s,%s,%s\n",
335 (status
& 0x10) ? "Selected" : "Not Selected",
336 (status
& 0x20) ? "No Paper" : "Paper",
337 (status
& 0x08) ? "No Error" : "Error");
343 static void printer_disconnect(struct usb_device
*dev
)
345 struct pp_usb_data
*pp
= dev
->private;
348 /* better let it finish - the release will do whats needed */
352 minor_data
[pp
->minor
] = NULL
;
354 dev
->private = NULL
; /* just in case */
358 static struct usb_driver printer_driver
= {
365 static struct file_operations usb_printer_fops
= {
370 NULL
, /* poll - out for the moment */
380 int usb_printer_init(void)
386 if ((result
= register_chrdev(USB_PRINTER_MAJOR
, "usblp", &usb_printer_fops
)) < 0) {
387 printk(KERN_WARNING
"usbprinter: Cannot register device\n");
393 usb_register(&printer_driver
);
394 printk(KERN_INFO
"USB Printer support registered.\n");
399 int init_module(void)
402 return usb_printer_init();
405 void cleanup_module(void)
409 usb_deregister(&printer_driver
);
410 unregister_chrdev(mymajor
, "usblplp");