1/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation; either version 2 of
5 * the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
15 * MA 02111-1307 USA
16 */
17/*
18 * printer.c  Version 0.11
19 *
20 * Copyright (c) 1999 Michael Gee	<michael@linuxspecific.com>
21 * Copyright (c) 1999 Pavel Machek	<pavel@suse.cz>
22 * Copyright (c) 2000 Randy Dunlap	<randy.dunlap@intel.com>
23 * Copyright (c) 2000 Vojtech Pavlik	<vojtech@suse.cz>
24 # Copyright (c) 2001 Pete Zaitcev	<zaitcev@redhat.com>
25 # Copyright (c) 2001 David Paschal	<paschal@rcsis.com>
26 *
27 * USB Printer Device Class driver for USB printers and printer cables
28 *
29 * Sponsored by SuSE
30 *
31 * ChangeLog:
32 *	v0.1 - thorough cleaning, URBification, almost a rewrite
33 *	v0.2 - some more cleanups
34 *	v0.3 - cleaner again, waitqueue fixes
35 *	v0.4 - fixes in unidirectional mode
36 *	v0.5 - add DEVICE_ID string support
37 *	v0.6 - never time out
38 *	v0.7 - fixed bulk-IN read and poll (David Paschal)
39 *	v0.8 - add devfs support
40 *	v0.9 - fix unplug-while-open paths
41 *	v0.10 - add proto_bias option (Pete Zaitcev)
42 *	v0.11 - add hpoj.sourceforge.net ioctls (David Paschal)
43 */
44
45/*
46 * This program is free software; you can redistribute it and/or modify
47 * it under the terms of the GNU General Public License as published by
48 * the Free Software Foundation; either version 2 of the License, or
49 * (at your option) any later version.
50 *
51 * This program is distributed in the hope that it will be useful,
52 * but WITHOUT ANY WARRANTY; without even the implied warranty of
53 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
54 * GNU General Public License for more details.
55 *
56 * You should have received a copy of the GNU General Public License
57 * along with this program; if not, write to the Free Software
58 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
59 */
60
61#include <linux/module.h>
62#include <linux/kernel.h>
63#include <linux/sched.h>
64#include <linux/smp_lock.h>
65#include <linux/signal.h>
66#include <linux/poll.h>
67#include <linux/init.h>
68#include <linux/slab.h>
69#include <linux/lp.h>
70#include <linux/devfs_fs_kernel.h>
71#undef DEBUG
72#include <linux/usb.h>
73/* Added by PaN */
74#include <linux/proc_fs.h>
75#include <asm/uaccess.h>
76// End PaN
77
78/*
79 * Version Information
80 */
81#define DRIVER_VERSION "v0.11"
82#define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
83#define DRIVER_DESC "USB Printer Device Class driver"
84
85#define USBLP_BUF_SIZE		8192
86#define DEVICE_ID_SIZE		1024
87
88/* ioctls: */
89/****************add by JY 20031118*************************************/
90#define LPGETID                 0x0610		/* get printer's device ID */
91#define LPWRITEDATA     0x0613  /* write data to printer */
92#define LPWRITEADDR     0x0614  /* write address to printer */
93#define LPREADDATA      0x0615  /* read data from pinter */
94#define LPREADADDR      0x0616  /* read address from pinter */
95/*******************************************************/
96#define LPGETSTATUS		0x060b		/* same as in drivers/char/lp.c */
97#define IOCNR_GET_DEVICE_ID		1
98#define IOCNR_GET_PROTOCOLS		2
99#define IOCNR_SET_PROTOCOL		3
100#define IOCNR_HP_SET_CHANNEL		4
101#define IOCNR_GET_BUS_ADDRESS		5
102#define IOCNR_GET_VID_PID		6
103/* Get device_id string: */
104#define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
105/* The following ioctls were added for http://hpoj.sourceforge.net: */
106/* Get two-int array:
107 * [0]=current protocol (1=7/1/1, 2=7/1/2, 3=7/1/3),
108 * [1]=supported protocol mask (mask&(1<<n)!=0 means 7/1/n supported): */
109#define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
110/* Set protocol (arg: 1=7/1/1, 2=7/1/2, 3=7/1/3): */
111#define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
112/* Set channel number (HP Vendor-specific command): */
113#define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
114/* Get two-int array: [0]=bus number, [1]=device address: */
115#define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
116/* Get two-int array: [0]=vendor ID, [1]=product ID: */
117#define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
118
119/*
120 * A DEVICE_ID string may include the printer's serial number.
121 * It should end with a semi-colon (';').
122 * An example from an HP 970C DeskJet printer is (this is one long string,
123 * with the serial number changed):
124MFG:HEWLETT-PACKARD;MDL:DESKJET 970C;CMD:MLC,PCL,PML;CLASS:PRINTER;DESCRIPTION:Hewlett-Packard DeskJet 970C;SERN:US970CSEPROF;VSTATUS:$HB0$NC0,ff,DN,IDLE,CUT,K1,C0,DP,NR,KP000,CP027;VP:0800,FL,B0;VJ:                    ;
125 */
126
127/*
128 * USB Printer Requests
129 */
130
131#define USBLP_REQ_GET_ID			0x00
132#define USBLP_REQ_GET_STATUS			0x01
133#define USBLP_REQ_RESET				0x02
134#define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST	0x00	/* HP Vendor-specific */
135
136#define USBLP_MINORS		16
137#define USBLP_MINOR_BASE	0
138
139#define USBLP_WRITE_TIMEOUT	(5*HZ)			/* 5 seconds */
140
141#define USBLP_FIRST_PROTOCOL	1
142#define USBLP_LAST_PROTOCOL	3
143#define USBLP_MAX_PROTOCOLS	(USBLP_LAST_PROTOCOL+1)
144
145struct usblp {
146	struct usb_device 	*dev;			/* USB device */
147	devfs_handle_t		devfs;			/* devfs device */
148	struct semaphore	sem;			/* locks this struct, especially "dev" */
149	char			*buf;		/* writeurb.transfer_buffer */
150	struct urb		readurb, writeurb;	/* The urbs */
151	wait_queue_head_t	wait;			/* Zzzzz ... */
152	int			readcount;		/* Counter for reads */
153	int			ifnum;			/* Interface number */
154	/* Alternate-setting numbers and endpoints for each protocol
155	 * (7/1/{index=1,2,3}) that the device supports: */
156	struct {
157		int				alt_setting;
158		struct usb_endpoint_descriptor	*epwrite;
159		struct usb_endpoint_descriptor	*epread;
160	}			protocol[USBLP_MAX_PROTOCOLS];
161	int			current_protocol;
162	int			minor;			/* minor number of device */
163	unsigned int		quirks;			/* quirks flags */
164	unsigned char		used;			/* True if open */
165	unsigned char		bidir;			/* interface is bidirectional */
166	unsigned char		*device_id_string;	/* IEEE 1284 DEVICE ID string (ptr) */
167							/* first 2 bytes are (big-endian) length */
168};
169
170#ifdef DEBUG
171static void usblp_dump(struct usblp *usblp) {
172	int p;
173
174	dbg("usblp=0x%p", usblp);
175	dbg("dev=0x%p", usblp->dev);
176	dbg("devfs=0x%p", usblp->devfs);
177	dbg("buf=0x%p", usblp->buf);
178	dbg("readcount=%d", usblp->readcount);
179	dbg("ifnum=%d", usblp->ifnum);
180    for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
181	dbg("protocol[%d].alt_setting=%d", p, usblp->protocol[p].alt_setting);
182	dbg("protocol[%d].epwrite=%p", p, usblp->protocol[p].epwrite);
183	dbg("protocol[%d].epread=%p", p, usblp->protocol[p].epread);
184    }
185	dbg("current_protocol=%d", usblp->current_protocol);
186	dbg("minor=%d", usblp->minor);
187	dbg("quirks=%d", usblp->quirks);
188	dbg("used=%d", usblp->used);
189	dbg("bidir=%d", usblp->bidir);
190	dbg("device_id_string=\"%s\"",
191		usblp->device_id_string ?
192			usblp->device_id_string + 2 :
193			(unsigned char *)"(null)");
194}
195#endif
196
197
198struct print_buffer{
199	int len;
200	char *buf;
201};
202
203/* Added by PaN */
204#define MODULE_NAME "usblp"
205#define MAX_CLASS_NAME  16
206#define MAX_MFR         16
207#define MAX_MODEL       32
208#define MAX_DESCRIPT    64
209#define MAX_STATUS_TYPE 6
210
211static struct proc_dir_entry *usblp_dir, *usblpid_file;
212struct parport_splink_device_info {
213	char class_name[MAX_CLASS_NAME];
214	char mfr[MAX_MFR];
215	char model[MAX_MODEL];
216	char description[MAX_DESCRIPT];
217};
218static char *usblp_status_type[MAX_STATUS_TYPE]={ "Lexmark", "Canon", "Hp", "Epson", "EPSON", NULL};
219static int usblp_status_maping[MAX_STATUS_TYPE][4]={ {0,0,0,0},
220				       		     {0, LP_POUTPA, LP_PERRORP, LP_PBUSY},
221				       		     {0,0,0,0},
222				       		     {0,0,0,0},
223				       		     {0,0,0,0},
224				       		     {0,0,0,0}};
225
226static struct parport_splink_device_info usblpid_info;
227static ssize_t usblp_write(struct file *file, const char *buffer, size_t count, loff_t *ppos);
228static ssize_t usblp_read(struct file *file, char *buffer, size_t count, loff_t *ppos);
229static int usblp_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
230// END PaN
231
232
233extern devfs_handle_t usb_devfs_handle;			/* /dev/usb dir. */
234
235static struct usblp *usblp_table[USBLP_MINORS];
236
237/* Quirks: various printer quirks are handled by this table & its flags. */
238
239struct quirk_printer_struct {
240	__u16 vendorId;
241	__u16 productId;
242	unsigned int quirks;
243};
244
245#define USBLP_QUIRK_BIDIR	0x1	/* reports bidir but requires unidirectional mode (no INs/reads) */
246#define USBLP_QUIRK_USB_INIT	0x2	/* needs vendor USB init string */
247
248static struct quirk_printer_struct quirk_printers[] = {
249	{ 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
250	{ 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
251	{ 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
252	{ 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
253	{ 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
254	{ 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
255	{ 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */
256	{ 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */
257	{ 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
258	{ 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
259	{ 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
260	{ 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
261	{ 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
262	{ 0, 0 }
263};
264
265static int usblp_select_alts(struct usblp *usblp);
266static int usblp_set_protocol(struct usblp *usblp, int protocol);
267static int usblp_cache_device_id_string(struct usblp *usblp);
268
269
270/*
271 * Functions for usblp control messages.
272 */
273
274static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
275{
276	int retval = usb_control_msg(usblp->dev,
277		dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
278		request, type | dir | recip, value, usblp->ifnum, buf, len, USBLP_WRITE_TIMEOUT);
279	dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d len: %#x result: %d",
280		request, !!dir, recip, value, len, retval);
281	return retval < 0 ? retval : 0;
282}
283
284#define usblp_read_status(usblp, status)\
285	usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
286#define usblp_get_id(usblp, config, id, maxlen)\
287	usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
288#define usblp_reset(usblp)\
289	usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
290
291#define usblp_hp_channel_change_request(usblp, channel, buffer) \
292	usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
293
294/*
295 * See the description for usblp_select_alts() below for the usage
296 * explanation.  Look into your /proc/bus/usb/devices and dmesg in
297 * case of any trouble.
298 */
299static int proto_bias = -1;
300
301/*
302 * URB callback.
303 */
304
305static void usblp_bulk(struct urb *urb)
306{
307	struct usblp *usblp = urb->context;
308
309	if (!usblp || !usblp->dev || !usblp->used)
310		return;
311
312	if (urb->status)
313		warn("usblp%d: nonzero read/write bulk status received: %d",
314			usblp->minor, urb->status);
315
316	wake_up_interruptible(&usblp->wait);
317}
318
319/*
320 * Get and print printer errors.
321 */
322
323static char *usblp_messages[] = { "ok", "out of paper", "off-line", "unknown error" };
324
325
326/* Added by PaN */
327static int proc_read_usblpid(char *page, char **start, off_t off, int count, int *eof, void *data)
328{
329	int len=0;
330
331	len=sprintf(page, "Manufacturer=%s\nModel=%s\nClass=%s\nDescription=%s\n\n",
332	usblpid_info.mfr, usblpid_info.model, usblpid_info.class_name, usblpid_info.description);
333
334	return len;
335}
336static int proc_get_usblpid(struct usblp *usblp)
337{
338	char *strtmp, *str_dev_id, *strunknown="unknown"; // Added by PaN
339	int i, unk = 0; // Added by PaN
340	int length, err;
341	int retval = 0;
342
343	err = usblp_get_id(usblp, 0, usblp->device_id_string, DEVICE_ID_SIZE - 1);
344
345	if (err < 0) {
346		dbg ("usblp%d: error = %d reading IEEE-1284 Device ID string",
347			usblp->minor, err);
348			usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
349		retval = -EIO;
350		goto done;
351	}
352
353	length = (usblp->device_id_string[0] << 8) + usblp->device_id_string[1]; /* big-endian */
354	if (length < DEVICE_ID_SIZE)
355		usblp->device_id_string[length] = '\0';
356	else
357		usblp->device_id_string[DEVICE_ID_SIZE - 1] = '\0';
358
359	dbg ("usblp%d Device ID string [%d]='%s'",
360		usblp->minor, length, &usblp->device_id_string[2]);
361	info ("usblp%d Device ID string [%d]='%s'",
362		usblp->minor, length, &usblp->device_id_string[2]);
363
364	str_dev_id = &usblp->device_id_string[2];
365	if ( (strtmp = strstr(str_dev_id, "MFG:")) == NULL) {
366		if ( (strtmp = strstr(str_dev_id, "MANUFACTURE:")) == NULL) {
367			for (i=0; i<7; i++) {
368				usblpid_info.mfr[i] = strunknown[i];
369			}
370			usblpid_info.mfr[i]='\0';
371			unk=1;
372		}
373		else
374			strtmp+=12;
375	}
376	else
377		strtmp+=4;
378
379	i=0;
380	while (strtmp[i] != ';' && unk==0) {
381		usblpid_info.mfr[i] = strtmp[i];
382		i++;
383	}
384	usblpid_info.mfr[i]='\0';
385	unk=0;
386
387	if ( (strtmp = strstr(str_dev_id, "MDL:")) == NULL) {
388		if ( (strtmp = strstr(str_dev_id, "MODEL:")) == NULL) {
389			for (i=0; i<7; i++) {
390				usblpid_info.model[i] = strunknown[i];
391			}
392			usblpid_info.model[i]='\0';
393			unk=1;
394		}
395		else
396			strtmp+=6;
397		}
398	else
399		strtmp+=4;
400
401	i=0;
402	while (strtmp[i] != ';' && unk==0) {
403		usblpid_info.model[i] = strtmp[i];
404		i++;
405	}
406	usblpid_info.model[i]='\0';
407	unk=0;
408
409	if ( (strtmp = strstr(str_dev_id, "CLS:")) == NULL) {
410		if ( (strtmp = strstr(str_dev_id, "CLASS:")) == NULL) {
411			for (i=0; i<7; i++) {
412				usblpid_info.class_name[i] = strunknown[i];
413			}
414			usblpid_info.class_name[i]='\0';
415			unk=1;
416		}
417		else
418			strtmp+=6;
419	}
420	else
421		strtmp+=4;
422
423	i=0;
424	while (strtmp[i] != ';' && unk==0) {
425		usblpid_info.class_name[i]= strtmp[i];
426		i++;
427	}
428	usblpid_info.class_name[i]='\0';
429	unk=0;
430
431	if ( (strtmp = strstr(str_dev_id, "DES:")) == NULL) {
432		if ( (strtmp = strstr(str_dev_id, "DESCRIPTION:")) == NULL) {
433			for (i=0; i<7; i++) {
434				usblpid_info.description[i] = strunknown[i];
435			}
436			usblpid_info.description[i]='\0';
437			unk=1;
438		}
439		else
440			strtmp+=12;
441	}
442	else
443		strtmp+=4;
444
445	i=0;
446	while (strtmp[i] != ';' && unk==0) {
447		usblpid_info.description[i]= strtmp[i];
448		i++;
449	}
450	usblpid_info.description[i]='\0';
451done:
452	return retval;
453
454}
455// End PaN
456
457static int usblp_check_status(struct usblp *usblp, int err)
458{
459	unsigned char status, newerr = 0;
460	int error;
461
462	error = usblp_read_status (usblp, &status);
463	if (error < 0) {
464		err("usblp%d: error %d reading printer status",
465			usblp->minor, error);
466		return 0;
467	}
468
469	if (~status & LP_PERRORP) {
470		newerr = 3;
471		if (status & LP_POUTPA) newerr = 1;
472		if (~status & LP_PSELECD) newerr = 2;
473	}
474
475	if (newerr != err)
476		info("usblp%d: %s", usblp->minor, usblp_messages[newerr]);
477
478	return newerr;
479}
480
481/*
482 * File op functions.
483 */
484
485static int usblp_open(struct inode *inode, struct file *file)
486{
487	int minor = MINOR(inode->i_rdev) - USBLP_MINOR_BASE;
488	struct usblp *usblp;
489	int retval;
490	unsigned int arg = NULL, ioctl_retval;// Added by PaN
491
492	if (minor < 0 || minor >= USBLP_MINORS)
493		return -ENODEV;
494
495	lock_kernel();
496	usblp  = usblp_table[minor];
497
498	retval = -ENODEV;
499	if (!usblp || !usblp->dev)
500		goto out;
501
502	retval = -EBUSY;
503	if (usblp->used)
504		goto out;
505
506	/*
507	 * TODO: need to implement LP_ABORTOPEN + O_NONBLOCK as in drivers/char/lp.c ???
508	 * This is #if 0-ed because we *don't* want to fail an open
509	 * just because the printer is off-line.
510	 */
511	retval = 0;
512
513	usblp->used = 1;
514	file->private_data = usblp;
515
516	usblp->writeurb.transfer_buffer_length = 0;
517	usblp->writeurb.status = 0;
518
519	if (usblp->bidir) {
520		usblp->readcount = 0;
521		usblp->readurb.dev = usblp->dev;
522		if (usb_submit_urb(&usblp->readurb) < 0) {
523			retval = -EIO;
524			usblp->used = 0;
525			file->private_data = NULL;
526		}
527	}
528
529	/* Added by PaN  */
530	if ( (ioctl_retval=usblp_ioctl(inode, file, LPGETID, arg)) < 0)
531		info("Updtae device id failed!!");
532
533out:
534	unlock_kernel();
535	return retval;
536}
537
538static void usblp_cleanup (struct usblp *usblp)
539{
540	devfs_unregister (usblp->devfs);
541	usblp_table [usblp->minor] = NULL;
542	info("usblp%d: removed", usblp->minor);
543
544	/* Added by PaN  */
545	remove_proc_entry("usblpid", usblp_dir);
546	remove_proc_entry(MODULE_NAME, NULL);
547	// End PaN
548
549	kfree (usblp->writeurb.transfer_buffer);
550	kfree (usblp->device_id_string);
551	kfree (usblp);
552}
553
554static void usblp_unlink_urbs(struct usblp *usblp)
555{
556	usb_unlink_urb(&usblp->writeurb);
557	if (usblp->bidir)
558		usb_unlink_urb(&usblp->readurb);
559}
560
561static int usblp_release(struct inode *inode, struct file *file)
562{
563	struct usblp *usblp = file->private_data;
564
565	down (&usblp->sem);
566	lock_kernel();
567	usblp->used = 0;
568	if (usblp->dev) {
569		usblp_unlink_urbs(usblp);
570		up(&usblp->sem);
571	} else 		/* finish cleanup from disconnect */
572		usblp_cleanup (usblp);
573	unlock_kernel();
574	return 0;
575}
576
577/* No kernel lock - fine */
578static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
579{
580	struct usblp *usblp = file->private_data;
581	poll_wait(file, &usblp->wait, wait);
582 	return ((!usblp->bidir || usblp->readurb.status  == -EINPROGRESS) ? 0 : POLLIN  | POLLRDNORM)
583 			       | (usblp->writeurb.status == -EINPROGRESS  ? 0 : POLLOUT | POLLWRNORM);
584}
585
586static int usblp_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
587{
588	struct usblp *usblp = file->private_data;
589	struct parport_splink_device_info prn_info_tmp, *prn_info; // Added by PaN
590	struct print_buffer user_buf_tmp, *user_buf; // Added by PaN
591	char *strtmp, *str_dev_id, *strunknown="unknown"; // Added by PaN
592	//int i, unk=0; // Added by PaN
593	int unk=0; // Added by PaN ---remove declaration of i for i is declared below: JY
594	int length, err, i;
595	unsigned char lpstatus, newChannel;
596	int status;
597	int twoints[2];
598	int retval = 0;
599	prn_info= &prn_info_tmp; // Added by PaN
600
601	down (&usblp->sem);
602	if (!usblp->dev) {
603		retval = -ENODEV;
604		goto done;
605	}
606
607	if (_IOC_TYPE(cmd) == 'P')	/* new-style ioctl number */
608
609		switch (_IOC_NR(cmd)) {
610
611			case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
612				if (_IOC_DIR(cmd) != _IOC_READ) {
613					retval = -EINVAL;
614					goto done;
615				}
616
617				length = usblp_cache_device_id_string(usblp);
618				if (length < 0) {
619					retval = length;
620					goto done;
621				}
622				if (length > _IOC_SIZE(cmd))
623					length = _IOC_SIZE(cmd); /* truncate */
624
625				if (copy_to_user((unsigned char *) arg,
626						usblp->device_id_string,
627						(unsigned long) length)) {
628					retval = -EFAULT;
629					goto done;
630				}
631
632				break;
633
634			case IOCNR_GET_PROTOCOLS:
635				if (_IOC_DIR(cmd) != _IOC_READ ||
636				    _IOC_SIZE(cmd) < sizeof(twoints)) {
637					retval = -EINVAL;
638					goto done;
639				}
640
641				twoints[0] = usblp->current_protocol;
642				twoints[1] = 0;
643				for (i = USBLP_FIRST_PROTOCOL;
644				     i <= USBLP_LAST_PROTOCOL; i++) {
645					if (usblp->protocol[i].alt_setting >= 0)
646						twoints[1] |= (1<<i);
647				}
648
649				if (copy_to_user((unsigned char *)arg,
650						(unsigned char *)twoints,
651						sizeof(twoints))) {
652					retval = -EFAULT;
653					goto done;
654				}
655
656				break;
657
658			case IOCNR_SET_PROTOCOL:
659				if (_IOC_DIR(cmd) != _IOC_WRITE) {
660					retval = -EINVAL;
661					goto done;
662				}
663
664#ifdef DEBUG
665				if (arg == -10) {
666					usblp_dump(usblp);
667					break;
668				}
669#endif
670
671				usblp_unlink_urbs(usblp);
672				retval = usblp_set_protocol(usblp, arg);
673				if (retval < 0) {
674					usblp_set_protocol(usblp,
675						usblp->current_protocol);
676				}
677				break;
678
679			case IOCNR_HP_SET_CHANNEL:
680				if (_IOC_DIR(cmd) != _IOC_WRITE ||
681				    usblp->dev->descriptor.idVendor != 0x03F0 ||
682				    usblp->quirks & USBLP_QUIRK_BIDIR) {
683					retval = -EINVAL;
684					goto done;
685				}
686
687				err = usblp_hp_channel_change_request(usblp,
688					arg, &newChannel);
689				if (err < 0) {
690					err("usblp%d: error = %d setting "
691						"HP channel",
692						usblp->minor, err);
693					retval = -EIO;
694					goto done;
695				}
696
697				dbg("usblp%d requested/got HP channel %ld/%d",
698					usblp->minor, arg, newChannel);
699				break;
700
701			case IOCNR_GET_BUS_ADDRESS:
702				if (_IOC_DIR(cmd) != _IOC_READ ||
703				    _IOC_SIZE(cmd) < sizeof(twoints)) {
704					retval = -EINVAL;
705					goto done;
706				}
707
708				twoints[0] = usblp->dev->bus->busnum;
709				twoints[1] = usblp->dev->devnum;
710				if (copy_to_user((unsigned char *)arg,
711						(unsigned char *)twoints,
712						sizeof(twoints))) {
713					retval = -EFAULT;
714					goto done;
715				}
716
717				dbg("usblp%d is bus=%d, device=%d",
718					usblp->minor, twoints[0], twoints[1]);
719				break;
720
721			case IOCNR_GET_VID_PID:
722				if (_IOC_DIR(cmd) != _IOC_READ ||
723				    _IOC_SIZE(cmd) < sizeof(twoints)) {
724					retval = -EINVAL;
725					goto done;
726				}
727
728				twoints[0] = usblp->dev->descriptor.idVendor;
729				twoints[1] = usblp->dev->descriptor.idProduct;
730				if (copy_to_user((unsigned char *)arg,
731						(unsigned char *)twoints,
732						sizeof(twoints))) {
733					retval = -EFAULT;
734					goto done;
735				}
736
737				dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
738					usblp->minor, twoints[0], twoints[1]);
739				break;
740
741			default:
742				retval = -EINVAL;
743		}
744	else	/* old-style ioctl value */
745		switch (cmd) {
746/*=================================================================================== PaN */
747			case LPGETID: /* get the DEVICE_ID string */
748				err = usblp_get_id(usblp, 0, usblp->device_id_string, DEVICE_ID_SIZE - 1);
749				if (err < 0) {
750					dbg ("usblp%d: error = %d reading IEEE-1284 Device ID string",
751						usblp->minor, err);
752					usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
753					retval = -EIO;
754					goto done;
755				}
756
757				length = (usblp->device_id_string[0] << 8) + usblp->device_id_string[1]; /* big-endian */
758				if (length < DEVICE_ID_SIZE)
759					usblp->device_id_string[length] = '\0';
760				else
761					usblp->device_id_string[DEVICE_ID_SIZE - 1] = '\0';
762
763				dbg ("usblp%d Device ID string [%d/max %d]='%s'",
764					usblp->minor, length, cmd, &usblp->device_id_string[2]);
765				info ("usblp%d Device ID string [%d/max %d]='%s'",
766					usblp->minor, length, cmd, &usblp->device_id_string[2]);
767
768				str_dev_id = &usblp->device_id_string[2];
769				if ( (strtmp = strstr(str_dev_id, "MFG:")) == NULL) {
770					if ( (strtmp = strstr(str_dev_id, "MANUFACTURE:")) == NULL) {
771						for (i=0; i<7; i++) {
772							prn_info->mfr[i]= strunknown[i];
773							usblpid_info.mfr[i] = strunknown[i];
774						}
775						prn_info->mfr[i]= '\0';
776						usblpid_info.mfr[i]='\0';
777						unk=1;
778					}
779					else
780						strtmp+=12;
781				}
782				else
783					strtmp+=4;
784
785				i=0;
786				while (strtmp[i] != ';' && unk==0) {
787					prn_info->mfr[i]= strtmp[i];
788					usblpid_info.mfr[i] = strtmp[i];
789					i++;
790				}
791				prn_info->mfr[i]= '\0';
792				usblpid_info.mfr[i]='\0';
793				unk=0;
794
795				if ( (strtmp = strstr(str_dev_id, "MDL:")) == NULL) {
796					if ( (strtmp = strstr(str_dev_id, "MODEL:")) == NULL) {
797						for (i=0; i<7; i++) {
798							prn_info->model[i]= strunknown[i];
799							usblpid_info.model[i] = strunknown[i];
800						}
801						prn_info->model[i]= '\0';
802						usblpid_info.model[i]='\0';
803						unk=1;
804					}
805					else
806						strtmp+=6;
807				}
808				else
809					strtmp+=4;
810
811				i=0;
812				while (strtmp[i] != ';' && unk==0) {
813					prn_info->model[i]= strtmp[i];
814					usblpid_info.model[i] = strtmp[i];
815					i++;
816				}
817				prn_info->model[i]= '\0';
818				usblpid_info.model[i]='\0';
819				unk=0;
820
821				if ( (strtmp = strstr(str_dev_id, "CLS:")) == NULL) {
822					if ( (strtmp = strstr(str_dev_id, "CLASS:")) == NULL) {
823						for (i=0; i<7; i++) {
824							prn_info->class_name[i]= strunknown[i];
825							usblpid_info.class_name[i] = strunknown[i];
826						}
827						prn_info->class_name[i]= '\0';
828						usblpid_info.class_name[i]='\0';
829						unk=1;
830					}
831					else
832						strtmp+=6;
833				}
834				else
835					strtmp+=4;
836
837				i=0;
838				while (strtmp[i] != ';' && unk==0) {
839					prn_info->class_name[i]= strtmp[i];
840					usblpid_info.class_name[i]= strtmp[i];
841					i++;
842				}
843				prn_info->class_name[i]= '\0';
844				usblpid_info.class_name[i]='\0';
845				unk=0;
846
847				if ( (strtmp = strstr(str_dev_id, "DES:")) == NULL) {
848					if ( (strtmp = strstr(str_dev_id, "DESCRIPTION:")) == NULL) {
849						for (i=0; i<7; i++) {
850							prn_info->description[i]= strunknown[i];
851							usblpid_info.description[i] = strunknown[i];
852						}
853						prn_info->description[i]= '\0';
854						usblpid_info.description[i]='\0';
855						unk=1;
856					}
857					else
858						strtmp+=12;
859				}
860				else
861					strtmp+=4;
862
863				i=0;
864				while (strtmp[i] != ';' && unk==0) {
865					prn_info->description[i]= strtmp[i];
866					usblpid_info.description[i]= strtmp[i];
867					i++;
868				}
869				prn_info->description[i]= '\0';
870				usblpid_info.description[i]='\0';
871
872				info("Parsing USBLPID...");
873				if (copy_to_user((unsigned char *) arg,
874						prn_info, (unsigned long) length)) {
875					retval = -EFAULT;
876					goto done;
877				}
878				break;
879
880			case LPREADDATA:
881			        up (&usblp->sem);
882				user_buf = (struct print_buffer *)arg;
883				retval = usblp_read(file, user_buf->buf, user_buf->len, NULL);
884				down (&usblp->sem);
885	                        break;
886
887
888			case LPWRITEDATA:
889			        up (&usblp->sem);
890				user_buf = (struct print_buffer *)arg;
891				retval = usblp_write(file, user_buf->buf, user_buf->len, NULL);
892				down (&usblp->sem);
893	                        break;
894
895			case LPRESET:
896                                usblp_reset(usblp);
897				break;
898
899			case LPGETSTATUS:
900				/* OLD USB Code Removed by PaN for Printer Server
901				if (usblp_read_status(usblp, &status)) {
902					err("usblp%d: failed reading printer status", usblp->minor);
903					retval = -EIO;
904					goto done;
905				}
906				if (copy_to_user ((int *)arg, &status, 2))
907					retval = -EFAULT;
908				*/
909                                status = usblp_check_status(usblp, 0);
910#if 0
911				info("start=%s", usblpid_info.mfr);
912				for (i=0; i< MAX_STATUS_TYPE; i++) {
913				info("compare=%s", usblp_status_type[i]);
914					if ( !( strcmp(usblpid_info.mfr, usblp_status_type[i]) ) )
915						break;
916				}
917				info("%d=%s", i, usblp_status_type[i]);
918				status=usblp_status_maping[i][status];
919				info("STATUS=%x", status);
920#endif
921				status=0;
922				if (copy_to_user ((int *)arg, &status, 2))
923					retval = -EFAULT;
924				break;
925
926/*=================================================================== PaN for Printer Server */
927
928/* Marked by JY 20031118*/
929#if 0
930			case LPGETSTATUS:
931				if (usblp_read_status(usblp, &lpstatus)) {
932					err("usblp%d: failed reading printer status", usblp->minor);
933					retval = -EIO;
934					goto done;
935				}
936				status = lpstatus;
937				if (copy_to_user ((int *)arg, &status, sizeof(int)))
938					retval = -EFAULT;
939				break;
940#endif
941/* Marked by JY 20031118*/
942
943			default:
944				retval = -EINVAL;
945		}
946
947done:
948	up (&usblp->sem);
949	return retval;
950}
951
952static ssize_t usblp_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
953{
954	struct usblp *usblp = file->private_data;
955	int timeout, err = 0;
956	size_t writecount = 0;
957
958	while (writecount < count) {
959
960		// FIXME:  only use urb->status inside completion
961		// callbacks; this way is racey...
962		if (usblp->writeurb.status == -EINPROGRESS) {
963
964			if (file->f_flags & O_NONBLOCK)
965				return -EAGAIN;
966
967			timeout = USBLP_WRITE_TIMEOUT;
968			while (timeout && usblp->writeurb.status == -EINPROGRESS) {
969
970				if (signal_pending(current))
971					return writecount ? writecount : -EINTR;
972
973				timeout = interruptible_sleep_on_timeout(&usblp->wait, timeout);
974			}
975		}
976
977		down (&usblp->sem);
978		if (!usblp->dev) {
979			up (&usblp->sem);
980			return -ENODEV;
981		}
982
983		if (usblp->writeurb.status != 0) {
984			if (usblp->quirks & USBLP_QUIRK_BIDIR) {
985				if (usblp->writeurb.status != -EINPROGRESS)
986					err("usblp%d: error %d writing to printer",
987						usblp->minor, usblp->writeurb.status);
988				err = usblp->writeurb.status;
989			} else
990				err = usblp_check_status(usblp, err);
991			up (&usblp->sem);
992
993			/* if the fault was due to disconnect, let khubd's
994			 * call to usblp_disconnect() grab usblp->sem ...
995			 */
996			info("jmp to");
997			return writecount; // Added by PaN
998			// schedule (); // Removed by PaN
999			continue;
1000		}
1001
1002		writecount += usblp->writeurb.transfer_buffer_length;
1003		usblp->writeurb.transfer_buffer_length = 0;
1004
1005		if (writecount == count) {
1006			up (&usblp->sem);
1007			break;
1008		}
1009
1010		usblp->writeurb.transfer_buffer_length = (count - writecount) < USBLP_BUF_SIZE ?
1011							 (count - writecount) : USBLP_BUF_SIZE;
1012
1013		if (copy_from_user(usblp->writeurb.transfer_buffer, buffer + writecount,
1014				usblp->writeurb.transfer_buffer_length)) {
1015			up(&usblp->sem);
1016			return writecount ? writecount : -EFAULT;
1017		}
1018
1019		usblp->writeurb.dev = usblp->dev;
1020		usb_submit_urb(&usblp->writeurb);
1021		up (&usblp->sem);
1022	}
1023
1024	return count;
1025}
1026
1027static ssize_t usblp_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
1028{
1029	struct usblp *usblp = file->private_data;
1030
1031	if (!usblp->bidir)
1032		return -EINVAL;
1033
1034	down (&usblp->sem);
1035	if (!usblp->dev) {
1036		count = -ENODEV;
1037		goto done;
1038	}
1039
1040	if (usblp->readurb.status == -EINPROGRESS) {
1041
1042		if (file->f_flags & O_NONBLOCK) {
1043			count = -EAGAIN;
1044			goto done;
1045		}
1046
1047		// FIXME:  only use urb->status inside completion
1048		// callbacks; this way is racey...
1049		while (usblp->readurb.status == -EINPROGRESS) {
1050			if (signal_pending(current)) {
1051				count = -EINTR;
1052				goto done;
1053			}
1054			up (&usblp->sem);
1055			interruptible_sleep_on(&usblp->wait);
1056			down (&usblp->sem);
1057		}
1058	}
1059
1060	if (!usblp->dev) {
1061		count = -ENODEV;
1062		goto done;
1063	}
1064
1065	if (usblp->readurb.status) {
1066		err("usblp%d: error %d reading from printer",
1067			usblp->minor, usblp->readurb.status);
1068		usblp->readurb.dev = usblp->dev;
1069 		usblp->readcount = 0;
1070		usb_submit_urb(&usblp->readurb);
1071		count = -EIO;
1072		goto done;
1073	}
1074
1075	count = count < usblp->readurb.actual_length - usblp->readcount ?
1076		count :	usblp->readurb.actual_length - usblp->readcount;
1077
1078	if (copy_to_user(buffer, usblp->readurb.transfer_buffer + usblp->readcount, count)) {
1079		count = -EFAULT;
1080		goto done;
1081	}
1082
1083	if ((usblp->readcount += count) == usblp->readurb.actual_length) {
1084		usblp->readcount = 0;
1085		usblp->readurb.dev = usblp->dev;
1086		usb_submit_urb(&usblp->readurb);
1087	}
1088
1089done:
1090	up (&usblp->sem);
1091	return count;
1092}
1093
1094/*
1095 * Checks for printers that have quirks, such as requiring unidirectional
1096 * communication but reporting bidirectional; currently some HP printers
1097 * have this flaw (HP 810, 880, 895, etc.), or needing an init string
1098 * sent at each open (like some Epsons).
1099 * Returns 1 if found, 0 if not found.
1100 *
1101 * HP recommended that we use the bidirectional interface but
1102 * don't attempt any bulk IN transfers from the IN endpoint.
1103 * Here's some more detail on the problem:
1104 * The problem is not that it isn't bidirectional though. The problem
1105 * is that if you request a device ID, or status information, while
1106 * the buffers are full, the return data will end up in the print data
1107 * buffer. For example if you make sure you never request the device ID
1108 * while you are sending print data, and you don't try to query the
1109 * printer status every couple of milliseconds, you will probably be OK.
1110 */
1111static unsigned int usblp_quirks (__u16 vendor, __u16 product)
1112{
1113	int i;
1114
1115	for (i = 0; quirk_printers[i].vendorId; i++) {
1116		if (vendor == quirk_printers[i].vendorId &&
1117		    product == quirk_printers[i].productId)
1118			return quirk_printers[i].quirks;
1119 	}
1120	return 0;
1121}
1122
1123static struct file_operations usblp_fops = {
1124	owner:		THIS_MODULE,
1125	read:		usblp_read,
1126	write:		usblp_write,
1127	poll:		usblp_poll,
1128	ioctl:		usblp_ioctl,
1129	open:		usblp_open,
1130	release:	usblp_release,
1131};
1132
1133static void *usblp_probe(struct usb_device *dev, unsigned int ifnum,
1134			 const struct usb_device_id *id)
1135{
1136	struct usblp *usblp = 0;
1137	int protocol;
1138	char name[6];
1139
1140	/* Malloc and start initializing usblp structure so we can use it
1141	 * directly. */
1142	if (!(usblp = kmalloc(sizeof(struct usblp), GFP_KERNEL))) {
1143		err("out of memory for usblp");
1144		goto abort;
1145	}
1146	memset(usblp, 0, sizeof(struct usblp));
1147	usblp->dev = dev;
1148	init_MUTEX (&usblp->sem);
1149	init_waitqueue_head(&usblp->wait);
1150	usblp->ifnum = ifnum;
1151
1152	/* Look for a free usblp_table entry. */
1153	while (usblp_table[usblp->minor]) {
1154		usblp->minor++;
1155		if (usblp->minor >= USBLP_MINORS) {
1156			err("no more free usblp devices");
1157			goto abort;
1158		}
1159	}
1160
1161	/* Malloc device ID string buffer to the largest expected length,
1162	 * since we can re-query it on an ioctl and a dynamic string
1163	 * could change in length. */
1164	if (!(usblp->device_id_string = kmalloc(DEVICE_ID_SIZE, GFP_KERNEL))) {
1165		err("out of memory for device_id_string");
1166		goto abort;
1167	}
1168
1169	/* Malloc write/read buffers in one chunk.  We somewhat wastefully
1170	 * malloc both regardless of bidirectionality, because the
1171	 * alternate setting can be changed later via an ioctl. */
1172	if (!(usblp->buf = kmalloc(2 * USBLP_BUF_SIZE, GFP_KERNEL))) {
1173		err("out of memory for buf");
1174		goto abort;
1175	}
1176
1177	/* Lookup quirks for this printer. */
1178	usblp->quirks = usblp_quirks(
1179		dev->descriptor.idVendor,
1180		dev->descriptor.idProduct);
1181
1182	/* Analyze and pick initial alternate settings and endpoints. */
1183	protocol = usblp_select_alts(usblp);
1184	if (protocol < 0) {
1185		dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
1186			dev->descriptor.idVendor,
1187			dev->descriptor.idProduct);
1188		goto abort;
1189	}
1190
1191	/* Setup the selected alternate setting and endpoints. */
1192	if (usblp_set_protocol(usblp, protocol) < 0)
1193		goto abort;
1194
1195	/* Retrieve and store the device ID string. */
1196	usblp_cache_device_id_string(usblp);
1197
1198#ifdef DEBUG
1199	usblp_check_status(usblp, 0);
1200#endif
1201
1202	usblp_table[usblp->minor] = usblp;
1203	/* If we have devfs, create with perms=660. */
1204	sprintf(name, "lp%d", usblp->minor);
1205	usblp->devfs = devfs_register(usb_devfs_handle, name,
1206				      DEVFS_FL_DEFAULT, USB_MAJOR,
1207				      USBLP_MINOR_BASE + usblp->minor,
1208				      S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP |
1209				      S_IWGRP, &usblp_fops, NULL);
1210
1211	info("usblp%d: USB %sdirectional printer dev %d "
1212		"if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
1213		usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum, ifnum,
1214		usblp->protocol[usblp->current_protocol].alt_setting,
1215		usblp->current_protocol, usblp->dev->descriptor.idVendor,
1216		usblp->dev->descriptor.idProduct);
1217
1218	/* Added by PaN */
1219	/* create directory */
1220	usblp_dir = proc_mkdir(MODULE_NAME, NULL);
1221	if(usblp_dir == NULL) {
1222	        goto outpan;
1223	}
1224        usblp_dir->owner = THIS_MODULE;
1225
1226	usblpid_file = create_proc_read_entry("usblpid", 0444, usblp_dir, proc_read_usblpid, NULL);
1227	if(usblpid_file == NULL) {
1228		remove_proc_entry(MODULE_NAME, NULL);
1229		goto outpan;
1230	}
1231        usblpid_file->owner = THIS_MODULE;
1232	/* get device id */
1233	if (proc_get_usblpid(usblp) < 0)
1234		info("proc:get usblpid error!!");
1235
1236outpan:
1237	// End PaN
1238
1239	return usblp;
1240
1241abort:
1242	if (usblp) {
1243		if (usblp->buf) kfree(usblp->buf);
1244		if (usblp->device_id_string) kfree(usblp->device_id_string);
1245		kfree(usblp);
1246	}
1247	return NULL;
1248}
1249
1250/*
1251 * We are a "new" style driver with usb_device_id table,
1252 * but our requirements are too intricate for simple match to handle.
1253 *
1254 * The "proto_bias" option may be used to specify the preferred protocol
1255 * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3).  If the device
1256 * supports the preferred protocol, then we bind to it.
1257 *
1258 * The best interface for us is 7/1/2, because it is compatible
1259 * with a stream of characters. If we find it, we bind to it.
1260 *
1261 * Note that the people from hpoj.sourceforge.net need to be able to
1262 * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose.
1263 *
1264 * Failing 7/1/2, we look for 7/1/3, even though it's probably not
1265 * stream-compatible, because this matches the behaviour of the old code.
1266 *
1267 * If nothing else, we bind to 7/1/1 - the unidirectional interface.
1268 */
1269static int usblp_select_alts(struct usblp *usblp)
1270{
1271	struct usb_interface *if_alt;
1272	struct usb_interface_descriptor *ifd;
1273	struct usb_endpoint_descriptor *epd, *epwrite, *epread;
1274	int p, i, e;
1275
1276	if_alt = &usblp->dev->actconfig->interface[usblp->ifnum];
1277
1278	for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
1279		usblp->protocol[p].alt_setting = -1;
1280
1281	/* Find out what we have. */
1282	for (i = 0; i < if_alt->num_altsetting; i++) {
1283		ifd = &if_alt->altsetting[i];
1284
1285		if (ifd->bInterfaceClass != 7 || ifd->bInterfaceSubClass != 1)
1286			continue;
1287
1288		if (ifd->bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
1289		    ifd->bInterfaceProtocol > USBLP_LAST_PROTOCOL)
1290			continue;
1291
1292		/* Look for bulk OUT and IN endpoints. */
1293		epwrite = epread = 0;
1294		for (e = 0; e < ifd->bNumEndpoints; e++) {
1295			epd = &ifd->endpoint[e];
1296
1297			if ((epd->bmAttributes&USB_ENDPOINT_XFERTYPE_MASK)!=
1298			    USB_ENDPOINT_XFER_BULK)
1299				continue;
1300
1301			if (!(epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK)) {
1302				if (!epwrite) epwrite=epd;
1303
1304			} else {
1305				if (!epread) epread=epd;
1306			}
1307		}
1308
1309		/* Ignore buggy hardware without the right endpoints. */
1310		if (!epwrite || (ifd->bInterfaceProtocol > 1 && !epread))
1311			continue;
1312
1313		/* Turn off reads for 7/1/1 (unidirectional) interfaces
1314		 * and buggy bidirectional printers. */
1315		if (ifd->bInterfaceProtocol == 1) {
1316			epread = NULL;
1317		} else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
1318			info("Disabling reads from problem bidirectional "
1319				"printer on usblp%d", usblp->minor);
1320			epread = NULL;
1321		}
1322
1323		usblp->protocol[ifd->bInterfaceProtocol].alt_setting = i;
1324		usblp->protocol[ifd->bInterfaceProtocol].epwrite = epwrite;
1325		usblp->protocol[ifd->bInterfaceProtocol].epread = epread;
1326	}
1327
1328	/* If our requested protocol is supported, then use it. */
1329	if (proto_bias >= USBLP_FIRST_PROTOCOL &&
1330	    proto_bias <= USBLP_LAST_PROTOCOL &&
1331	    usblp->protocol[proto_bias].alt_setting != -1)
1332		return proto_bias;
1333
1334	/* Ordering is important here. */
1335	if (usblp->protocol[2].alt_setting != -1) return 2;
1336	if (usblp->protocol[1].alt_setting != -1) return 1;
1337	if (usblp->protocol[3].alt_setting != -1) return 3;
1338
1339	/* If nothing is available, then don't bind to this device. */
1340	return -1;
1341}
1342
1343static int usblp_set_protocol(struct usblp *usblp, int protocol)
1344{
1345	int r, alts;
1346
1347	if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
1348		return -EINVAL;
1349
1350	alts = usblp->protocol[protocol].alt_setting;
1351	if (alts < 0) return -EINVAL;
1352	r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
1353	if (r < 0) {
1354		err("can't set desired altsetting %d on interface %d",
1355			alts, usblp->ifnum);
1356		return r;
1357	}
1358
1359	FILL_BULK_URB(&usblp->writeurb, usblp->dev,
1360		usb_sndbulkpipe(usblp->dev,
1361		 usblp->protocol[protocol].epwrite->bEndpointAddress),
1362		usblp->buf, 0,
1363		usblp_bulk, usblp);
1364
1365	usblp->bidir = (usblp->protocol[protocol].epread != 0);
1366	if (usblp->bidir)
1367		FILL_BULK_URB(&usblp->readurb, usblp->dev,
1368			usb_rcvbulkpipe(usblp->dev,
1369			 usblp->protocol[protocol].epread->bEndpointAddress),
1370			usblp->buf + USBLP_BUF_SIZE, USBLP_BUF_SIZE,
1371			usblp_bulk, usblp);
1372
1373	usblp->current_protocol = protocol;
1374	dbg("usblp%d set protocol %d", usblp->minor, protocol);
1375	return 0;
1376}
1377
1378/* Retrieves and caches device ID string.
1379 * Returns length, including length bytes but not null terminator.
1380 * On error, returns a negative errno value. */
1381static int usblp_cache_device_id_string(struct usblp *usblp)
1382{
1383	int err, length;
1384
1385	err = usblp_get_id(usblp, 0, usblp->device_id_string, DEVICE_ID_SIZE - 1);
1386	if (err < 0) {
1387		dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
1388			usblp->minor, err);
1389		usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
1390		return -EIO;
1391	}
1392
1393	/* First two bytes are length in big-endian.
1394	 * They count themselves, and we copy them into
1395	 * the user's buffer. */
1396	length = (usblp->device_id_string[0] << 8) + usblp->device_id_string[1];
1397	if (length < 2)
1398		length = 2;
1399	else if (length >= DEVICE_ID_SIZE)
1400		length = DEVICE_ID_SIZE - 1;
1401	usblp->device_id_string[length] = '\0';
1402
1403	dbg("usblp%d Device ID string [len=%d]=\"%s\"",
1404		usblp->minor, length, &usblp->device_id_string[2]);
1405
1406	return length;
1407}
1408
1409static void usblp_disconnect(struct usb_device *dev, void *ptr)
1410{
1411	struct usblp *usblp = ptr;
1412
1413	if (!usblp || !usblp->dev) {
1414		err("bogus disconnect");
1415		BUG ();
1416	}
1417
1418	down (&usblp->sem);
1419	lock_kernel();
1420	usblp->dev = NULL;
1421
1422	usblp_unlink_urbs(usblp);
1423
1424	if (!usblp->used)
1425		usblp_cleanup (usblp);
1426	else 	/* cleanup later, on close */
1427		up (&usblp->sem);
1428	unlock_kernel();
1429}
1430
1431static struct usb_device_id usblp_ids [] = {
1432	{ USB_DEVICE_INFO(7, 1, 1) },
1433	{ USB_DEVICE_INFO(7, 1, 2) },
1434	{ USB_DEVICE_INFO(7, 1, 3) },
1435	{ USB_INTERFACE_INFO(7, 1, 1) },
1436	{ USB_INTERFACE_INFO(7, 1, 2) },
1437	{ USB_INTERFACE_INFO(7, 1, 3) },
1438	{ }						/* Terminating entry */
1439};
1440
1441MODULE_DEVICE_TABLE (usb, usblp_ids);
1442
1443static struct usb_driver usblp_driver = {
1444	name:		"usblp",
1445	probe:		usblp_probe,
1446	disconnect:	usblp_disconnect,
1447	fops:		&usblp_fops,
1448	minor:		USBLP_MINOR_BASE,
1449	id_table:	usblp_ids,
1450};
1451
1452static int __init usblp_init(void)
1453{
1454	if (usb_register(&usblp_driver))
1455		return -1;
1456	info(DRIVER_VERSION ": " DRIVER_DESC);
1457	return 0;
1458}
1459
1460static void __exit usblp_exit(void)
1461{
1462	usb_deregister(&usblp_driver);
1463}
1464
1465module_init(usblp_init);
1466module_exit(usblp_exit);
1467
1468MODULE_AUTHOR( DRIVER_AUTHOR );
1469MODULE_DESCRIPTION( DRIVER_DESC );
1470MODULE_PARM(proto_bias, "i");
1471MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
1472MODULE_LICENSE("GPL");
1473