pre-2.3.4..
[davej-history.git] / drivers / usb / printer.c
blobfb632cdc8c64c553d820be354069ef099cc1837c
2 /* Driver for USB Printers
3 *
4 * (C) Michael Gee (michael@linuxspecific.com) 1999
5 *
6 */
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>
18 #include <linux/lp.h>
20 #include <asm/spinlock.h>
22 #include "usb.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
29 #endif
31 static int mymajor = USB_PRINTER_MAJOR;
33 #define MAX_PRINTERS 8
35 struct pp_usb_data {
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)
53 __u8 status;
54 devrequest dr;
55 struct usb_device *dev = p->pusb_dev;
57 dr.requesttype = USB_TYPE_CLASS | USB_RT_INTERFACE | 0x80;
58 dr.request = 1;
59 dr.value = 0;
60 dr.index = 0;
61 dr.length = 1;
62 if (dev->bus->op->control_msg(dev, usb_rcvctrlpipe(dev,0), &dr, &status, 1)) {
63 return 0;
65 return status;
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)
74 /* No error. */
75 last = 0;
76 else if ((status & LP_POUTPA)) {
77 if (last != LP_POUTPA) {
78 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) {
83 last = LP_PSELECD;
84 printk(KERN_INFO "usblp%d off-line\n", p->minor);
86 } else {
87 if (last != LP_PERRORP) {
88 last = LP_PERRORP;
89 printk(KERN_INFO "usblp%d on fire\n", p->minor);
93 p->last_error = last;
95 return status;
98 void printer_reset(struct pp_usb_data *p)
100 devrequest dr;
101 struct usb_device *dev = p->pusb_dev;
103 dr.requesttype = USB_TYPE_CLASS | USB_RECIP_OTHER;
104 dr.request = 2;
105 dr.value = 0;
106 dr.index = 0;
107 dr.length = 0;
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)]) {
117 return -ENODEV;
120 p = minor_data[MINOR(inode->i_rdev)];
121 p->minor = MINOR(inode->i_rdev);
123 if (p->isopen++) {
124 return -EBUSY;
126 if (!(p->obuf = (char *)__get_free_page(GFP_KERNEL))) {
127 return -ENOMEM;
130 printer_check_status(p);
133 file->private_data = p;
134 // printer_reset(p);
135 init_waitqueue_head(&p->wait_q);
136 return 0;
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);
144 p->isopen = 0;
145 file->private_data = NULL;
146 if(!p->pusb_dev) {
147 minor_data[p->minor] = NULL;
148 kfree(p);
150 MOD_DEC_USE_COUNT;
153 return 0;
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;
163 int result;
164 int maxretry;
166 do {
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))
172 return -EFAULT;
173 maxretry = MAX_RETRY_COUNT;
174 while (thistime) {
175 if (!p->pusb_dev)
176 return -ENODEV;
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 */
183 obuf += partial;
184 thistime -= partial;
185 if(!maxretry--)
186 return -ETIME;
187 interruptible_sleep_on_timeout(&p->wait_q, NAK_TIMEOUT);
188 continue;
189 } else
190 break;
192 if (result) {
193 /* whoops - let's reset and fail the request */
194 // printk("Whoops - %x\n", result);
195 printer_reset(p);
196 interruptible_sleep_on_timeout(&p->wait_q, 5*HZ); /* let reset do its stuff */
197 return -EIO;
199 bytes_written += copy_size;
200 count -= copy_size;
201 buffer += 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;
211 int read_count;
212 int this_read;
213 char buf[64];
214 unsigned long partial;
215 int result;
217 if (p->noinput)
218 return -EINVAL;
220 read_count = 0;
221 while (count) {
222 if (signal_pending(current)) {
223 return read_count ? read_count : -EINTR;
225 if (!p->pusb_dev)
226 return -ENODEV;
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 */
233 if (result & 0x08)
234 count = this_read = partial;
235 else if (result)
236 return -EIO;
238 if (this_read) {
239 if (copy_to_user(buffer, p->obuf, this_read))
240 return -EFAULT;
241 count -= this_read;
242 read_count += this_read;
243 buffer += this_read;
246 return read_count;
249 static int printer_probe(struct usb_device *dev)
251 struct usb_interface_descriptor *interface;
252 int i;
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) {
260 return -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) {
270 return -1;
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))) {
278 return -1;
281 for (i=0; i<MAX_PRINTERS; i++) {
282 if (!minor_data[i])
283 break;
285 if (i >= MAX_PRINTERS) {
286 return -1;
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");
293 return -1;
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;
306 else {
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");
312 return -1;
314 #if 0
316 __u8 status;
317 __u8 ieee_id[64];
318 devrequest dr;
320 /* Lets get the device id if possible */
321 dr.requesttype = USB_TYPE_CLASS | USB_RT_INTERFACE | 0x80;
322 dr.request = 0;
323 dr.value = 0;
324 dr.index = 0;
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';
329 else
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");
339 #endif
340 return 0;
343 static void printer_disconnect(struct usb_device *dev)
345 struct pp_usb_data *pp = dev->private;
347 if (pp->isopen) {
348 /* better let it finish - the release will do whats needed */
349 pp->pusb_dev = NULL;
350 return;
352 minor_data[pp->minor] = NULL;
353 kfree(pp);
354 dev->private = NULL; /* just in case */
355 MOD_DEC_USE_COUNT;
358 static struct usb_driver printer_driver = {
359 "printer",
360 printer_probe,
361 printer_disconnect,
362 { NULL, NULL }
365 static struct file_operations usb_printer_fops = {
366 NULL, /* seek */
367 read_printer,
368 write_printer,
369 NULL, /* readdir */
370 NULL, /* poll - out for the moment */
371 NULL, /* ioctl */
372 NULL, /* mmap */
373 open_printer,
374 NULL, /* flush ? */
375 close_printer,
376 NULL,
377 NULL
380 int usb_printer_init(void)
382 int result;
384 MOD_INC_USE_COUNT;
386 if ((result = register_chrdev(USB_PRINTER_MAJOR, "usblp", &usb_printer_fops)) < 0) {
387 printk(KERN_WARNING "usbprinter: Cannot register device\n");
388 return result;
390 if (mymajor == 0) {
391 mymajor = result;
393 usb_register(&printer_driver);
394 printk(KERN_INFO "USB Printer support registered.\n");
395 return 0;
398 #ifdef MODULE
399 int init_module(void)
402 return usb_printer_init();
405 void cleanup_module(void)
407 unsigned int offset;
409 usb_deregister(&printer_driver);
410 unregister_chrdev(mymajor, "usblplp");
412 #endif