Import 2.3.25pre1
[davej-history.git] / drivers / usb / printer.c
blob8a66930616b3c54f3bfe409d1536adf4630a40aa
2 /* Driver for USB Printers
3 *
4 * Copyright 1999 Michael Gee (michael@linuxspecific.com)
5 * Copyright 1999 Pavel Machek (pavel@suse.cz)
7 * Distribute under GPL version 2 or later.
8 */
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>
20 #include <linux/lp.h>
21 #include <linux/spinlock.h>
23 #include "usb.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
39 struct pp_usb_data {
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)
61 __u8 status;
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)) {
68 return 0;
70 return status;
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)
79 /* No error. */
80 last = 0;
81 else if ((status & LP_POUTPA)) {
82 if (last != LP_POUTPA) {
83 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) {
88 last = LP_PSELECD;
89 printk(KERN_INFO "usblp%d off-line\n", p->minor);
91 } else {
92 if (last != LP_PERRORP) {
93 last = LP_PERRORP;
94 printk(KERN_INFO "usblp%d on fire\n", p->minor);
98 p->last_error = last;
100 return status;
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,
110 0, 0, NULL, 0, HZ);
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)]) {
119 return -ENODEV;
122 p = minor_data[MINOR(inode->i_rdev)];
123 p->minor = MINOR(inode->i_rdev);
125 if (p->isopen++) {
126 return -EBUSY;
128 if (!(p->obuf = (char *)__get_free_page(GFP_KERNEL))) {
129 p->isopen = 0;
130 return -ENOMEM;
133 printer_check_status(p);
135 file->private_data = p;
136 // printer_reset(p);
137 init_waitqueue_head(&p->wait_q);
139 MOD_INC_USE_COUNT;
140 return 0;
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);
148 p->isopen = 0;
149 file->private_data = NULL;
150 /* free the resources if the printer is no longer around */
151 if (!p->pusb_dev) {
152 minor_data[p->minor] = NULL;
153 kfree(p);
155 MOD_DEC_USE_COUNT;
156 return 0;
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;
167 int maxretry;
169 do {
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))
175 return -EFAULT;
176 maxretry = MAX_RETRY_COUNT;
177 while (thistime) {
178 if (!p->pusb_dev)
179 return -ENODEV;
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);
186 if (partial) {
187 obuf += partial;
188 thistime -= partial;
189 maxretry = MAX_RETRY_COUNT;
191 if (result == USB_ST_TIMEOUT) { /* NAK - so hold for a while */
192 if (!maxretry--)
193 return -ETIME;
194 interruptible_sleep_on_timeout(&p->wait_q, NAK_TIMEOUT);
195 continue;
196 } else if (!result && !partial) {
197 break;
200 if (result) {
201 /* whoops - let's reset and fail the request */
202 // printk("Whoops - %x\n", result);
203 printer_reset(p);
204 interruptible_sleep_on_timeout(&p->wait_q, 5*HZ); /* let reset do its stuff */
205 return -EIO;
207 bytes_written += copy_size;
208 count -= copy_size;
209 buffer += 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;
219 int read_count = 0;
220 int this_read;
221 char buf[64];
222 unsigned long partial;
223 int result;
225 if (p->noinput)
226 return -EINVAL;
228 while (count) {
229 if (signal_pending(current)) {
230 return read_count ? read_count : -EINTR;
232 if (!p->pusb_dev)
233 return -ENODEV;
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;
243 else if (result)
244 return -EIO;
246 if (this_read) {
247 if (copy_to_user(buffer, buf, this_read))
248 return -EFAULT;
249 count -= this_read;
250 read_count += this_read;
251 buffer += this_read;
254 return read_count;
257 static void * printer_probe(struct usb_device *dev, unsigned int ifnum)
259 struct usb_interface_descriptor *interface;
260 struct pp_usb_data *pp;
261 int i;
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) {
270 return NULL;
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) {
280 return NULL;
283 /* Does this (these) interface(s) support bulk transfers? */
284 if ((interface->endpoint[0].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
285 != USB_ENDPOINT_XFER_BULK) {
286 return NULL;
288 if ((interface->bNumEndpoints > 1) &&
289 ((interface->endpoint[1].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
290 != USB_ENDPOINT_XFER_BULK)) {
291 return NULL;
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)
299 != USB_DIR_OUT &&
300 (interface->bNumEndpoints > 1 &&
301 (interface->endpoint[1].bEndpointAddress & USB_ENDPOINT_DIR_MASK)
302 != USB_DIR_OUT)) {
303 return NULL;
306 for (i=0; i<MAX_PRINTERS; i++) {
307 if (!minor_data[i])
308 break;
310 if (i >= MAX_PRINTERS) {
311 printk("No minor table space available for USB Printer\n");
312 return NULL;
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");
319 return NULL;
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);
355 #if 0
357 __u8 status;
358 __u8 ieee_id[64];
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,
364 0, 0, ieee_id,
365 sizeof(ieee_id)-1, HZ) == 0) {
366 if (ieee_id[1] < sizeof(ieee_id) - 1)
367 ieee_id[ieee_id[1]+2] = '\0';
368 else
369 ieee_id[sizeof(ieee_id)-1] = '\0';
370 printk(KERN_INFO " USB Printer ID is %s\n",
371 &ieee_id[2]);
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");
379 #endif
380 return pp;
383 static void printer_disconnect(struct usb_device *dev, void *ptr)
385 struct pp_usb_data *pp = ptr;
387 if (pp->isopen) {
388 /* better let it finish - the release will do whats needed */
389 pp->pusb_dev = NULL;
390 return;
392 minor_data[pp->minor] = NULL;
393 kfree(pp);
396 static struct file_operations usb_printer_fops = {
397 NULL, /* seek */
398 read_printer,
399 write_printer,
400 NULL, /* readdir */
401 NULL, /* poll - out for the moment */
402 NULL, /* ioctl */
403 NULL, /* mmap */
404 open_printer,
405 NULL, /* flush ? */
406 close_printer,
407 NULL,
408 NULL
411 static struct usb_driver printer_driver = {
412 "printer",
413 printer_probe,
414 printer_disconnect,
415 { NULL, NULL },
416 &usb_printer_fops,
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);
426 return 1;
429 printk(KERN_INFO "USB Printer support registered.\n");
430 return 0;
433 #ifdef MODULE
434 int init_module(void)
437 return usb_printer_init();
440 void cleanup_module(void)
442 usb_deregister(&printer_driver);
444 #endif