Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 251237 | Differences between
and this patch

Collapse All | Expand All

(-)a/drivers/usb/class/usblp.c (-273 / +431 lines)
Lines 1-5 Link Here
1
/*
1
/*
2
 * usblp.c  Version 0.13
2
 * usblp.c
3
 *
3
 *
4
 * Copyright (c) 1999 Michael Gee	<michael@linuxspecific.com>
4
 * Copyright (c) 1999 Michael Gee	<michael@linuxspecific.com>
5
 * Copyright (c) 1999 Pavel Machek	<pavel@suse.cz>
5
 * Copyright (c) 1999 Pavel Machek	<pavel@suse.cz>
Lines 28-33 Link Here
28
 *	v0.12 - add hpoj.sourceforge.net ioctls (David Paschal)
28
 *	v0.12 - add hpoj.sourceforge.net ioctls (David Paschal)
29
 *	v0.13 - alloc space for statusbuf (<status> not on stack);
29
 *	v0.13 - alloc space for statusbuf (<status> not on stack);
30
 *		use usb_buffer_alloc() for read buf & write buf;
30
 *		use usb_buffer_alloc() for read buf & write buf;
31
 *      none  - Maintained in Linux kernel after v0.13
31
 */
32
 */
32
33
33
/*
34
/*
Lines 49-55 Link Here
49
#include <linux/module.h>
50
#include <linux/module.h>
50
#include <linux/kernel.h>
51
#include <linux/kernel.h>
51
#include <linux/sched.h>
52
#include <linux/sched.h>
52
#include <linux/smp_lock.h>
53
#include <linux/signal.h>
53
#include <linux/signal.h>
54
#include <linux/poll.h>
54
#include <linux/poll.h>
55
#include <linux/init.h>
55
#include <linux/init.h>
Lines 62-76 Link Here
62
/*
62
/*
63
 * Version Information
63
 * Version Information
64
 */
64
 */
65
#define DRIVER_VERSION "v0.13"
66
#define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
65
#define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
67
#define DRIVER_DESC "USB Printer Device Class driver"
66
#define DRIVER_DESC "USB Printer Device Class driver"
68
67
69
#define USBLP_BUF_SIZE		8192
68
#define USBLP_BUF_SIZE		8192
69
#define USBLP_BUF_SIZE_IN	1024
70
#define USBLP_DEVICE_ID_SIZE	1024
70
#define USBLP_DEVICE_ID_SIZE	1024
71
71
72
/* ioctls: */
72
/* ioctls: */
73
#define LPGETSTATUS		0x060b		/* same as in drivers/char/lp.c */
74
#define IOCNR_GET_DEVICE_ID		1
73
#define IOCNR_GET_DEVICE_ID		1
75
#define IOCNR_GET_PROTOCOLS		2
74
#define IOCNR_GET_PROTOCOLS		2
76
#define IOCNR_SET_PROTOCOL		3
75
#define IOCNR_SET_PROTOCOL		3
Lines 116-122 MFG:HEWLETT-PACKARD;MDL:DESKJET 970C;CMD:MLC,PCL,PML;CLASS:PRINTER;DESCRIPTION:H Link Here
116
#define USBLP_MINORS		16
115
#define USBLP_MINORS		16
117
#define USBLP_MINOR_BASE	0
116
#define USBLP_MINOR_BASE	0
118
117
119
#define USBLP_WRITE_TIMEOUT	(5000)			/* 5 seconds */
118
#define USBLP_CTL_TIMEOUT	5000			/* 5 seconds */
120
119
121
#define USBLP_FIRST_PROTOCOL	1
120
#define USBLP_FIRST_PROTOCOL	1
122
#define USBLP_LAST_PROTOCOL	3
121
#define USBLP_LAST_PROTOCOL	3
Lines 128-141 MFG:HEWLETT-PACKARD;MDL:DESKJET 970C;CMD:MLC,PCL,PML;CLASS:PRINTER;DESCRIPTION:H Link Here
128
 */
127
 */
129
#define STATUS_BUF_SIZE		8
128
#define STATUS_BUF_SIZE		8
130
129
130
/*
131
 * Locks down the locking order:
132
 * ->wmut locks wstatus.
133
 * ->mut locks the whole usblp, except [rw]complete, and thus, by indirection,
134
 * [rw]status. We only touch status when we know the side idle.
135
 * ->lock locks what interrupt accesses.
136
 */
131
struct usblp {
137
struct usblp {
132
	struct usb_device 	*dev;			/* USB device */
138
	struct usb_device 	*dev;			/* USB device */
133
	struct mutex		mut;			/* locks this struct, especially "dev" */
139
	struct mutex		wmut;
134
	char			*writebuf;		/* write transfer_buffer */
140
	struct mutex		mut;
141
	spinlock_t		lock;		/* locks rcomplete, wcomplete */
135
	char			*readbuf;		/* read transfer_buffer */
142
	char			*readbuf;		/* read transfer_buffer */
136
	char			*statusbuf;		/* status transfer_buffer */
143
	char			*statusbuf;		/* status transfer_buffer */
137
	struct urb		*readurb, *writeurb;	/* The urbs */
144
	struct usb_anchor	urbs;
138
	wait_queue_head_t	wait;			/* Zzzzz ... */
145
	wait_queue_head_t	rwait, wwait;
139
	int			readcount;		/* Counter for reads */
146
	int			readcount;		/* Counter for reads */
140
	int			ifnum;			/* Interface number */
147
	int			ifnum;			/* Interface number */
141
	struct usb_interface	*intf;			/* The interface */
148
	struct usb_interface	*intf;			/* The interface */
Lines 148-160 struct usblp { Link Here
148
	}			protocol[USBLP_MAX_PROTOCOLS];
155
	}			protocol[USBLP_MAX_PROTOCOLS];
149
	int			current_protocol;
156
	int			current_protocol;
150
	int			minor;			/* minor number of device */
157
	int			minor;			/* minor number of device */
151
	int			wcomplete;		/* writing is completed */
158
	int			wcomplete, rcomplete;
152
	int			rcomplete;		/* reading is completed */
159
	int			wstatus;	/* bytes written or error */
160
	int			rstatus;	/* bytes ready or error */
153
	unsigned int		quirks;			/* quirks flags */
161
	unsigned int		quirks;			/* quirks flags */
162
	unsigned int		flags;			/* mode flags */
154
	unsigned char		used;			/* True if open */
163
	unsigned char		used;			/* True if open */
155
	unsigned char		present;		/* True if not disconnected */
164
	unsigned char		present;		/* True if not disconnected */
156
	unsigned char		bidir;			/* interface is bidirectional */
165
	unsigned char		bidir;			/* interface is bidirectional */
157
	unsigned char		sleeping;		/* interface is suspended */
166
	unsigned char		sleeping;		/* interface is suspended */
167
	unsigned char		no_paper;		/* Paper Out happened */
158
	unsigned char		*device_id_string;	/* IEEE 1284 DEVICE ID string (ptr) */
168
	unsigned char		*device_id_string;	/* IEEE 1284 DEVICE ID string (ptr) */
159
							/* first 2 bytes are (big-endian) length */
169
							/* first 2 bytes are (big-endian) length */
160
};
170
};
Lines 167-175 static void usblp_dump(struct usblp *usblp) { Link Here
167
	dbg("dev=0x%p", usblp->dev);
177
	dbg("dev=0x%p", usblp->dev);
168
	dbg("present=%d", usblp->present);
178
	dbg("present=%d", usblp->present);
169
	dbg("readbuf=0x%p", usblp->readbuf);
179
	dbg("readbuf=0x%p", usblp->readbuf);
170
	dbg("writebuf=0x%p", usblp->writebuf);
171
	dbg("readurb=0x%p", usblp->readurb);
172
	dbg("writeurb=0x%p", usblp->writeurb);
173
	dbg("readcount=%d", usblp->readcount);
180
	dbg("readcount=%d", usblp->readcount);
174
	dbg("ifnum=%d", usblp->ifnum);
181
	dbg("ifnum=%d", usblp->ifnum);
175
    for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
182
    for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
Lines 179-186 static void usblp_dump(struct usblp *usblp) { Link Here
179
    }
186
    }
180
	dbg("current_protocol=%d", usblp->current_protocol);
187
	dbg("current_protocol=%d", usblp->current_protocol);
181
	dbg("minor=%d", usblp->minor);
188
	dbg("minor=%d", usblp->minor);
182
	dbg("wcomplete=%d", usblp->wcomplete);
189
	dbg("wstatus=%d", usblp->wstatus);
183
	dbg("rcomplete=%d", usblp->rcomplete);
190
	dbg("rstatus=%d", usblp->rstatus);
184
	dbg("quirks=%d", usblp->quirks);
191
	dbg("quirks=%d", usblp->quirks);
185
	dbg("used=%d", usblp->used);
192
	dbg("used=%d", usblp->used);
186
	dbg("bidir=%d", usblp->bidir);
193
	dbg("bidir=%d", usblp->bidir);
Lines 202-207 struct quirk_printer_struct { Link Here
202
209
203
#define USBLP_QUIRK_BIDIR	0x1	/* reports bidir but requires unidirectional mode (no INs/reads) */
210
#define USBLP_QUIRK_BIDIR	0x1	/* reports bidir but requires unidirectional mode (no INs/reads) */
204
#define USBLP_QUIRK_USB_INIT	0x2	/* needs vendor USB init string */
211
#define USBLP_QUIRK_USB_INIT	0x2	/* needs vendor USB init string */
212
#define USBLP_QUIRK_BAD_CLASS	0x4	/* descriptor uses vendor-specific Class or SubClass */
205
213
206
static const struct quirk_printer_struct quirk_printers[] = {
214
static const struct quirk_printer_struct quirk_printers[] = {
207
	{ 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
215
	{ 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
Lines 218-226 static const struct quirk_printer_struct quirk_printers[] = { Link Here
218
	{ 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
226
	{ 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
219
	{ 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
227
	{ 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
220
	{ 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
228
	{ 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
229
	{ 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt Printer M129C */
221
	{ 0, 0 }
230
	{ 0, 0 }
222
};
231
};
223
232
233
static int usblp_wwait(struct usblp *usblp, int nonblock);
234
static int usblp_wtest(struct usblp *usblp, int nonblock);
235
static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock);
236
static int usblp_rtest(struct usblp *usblp, int nonblock);
237
static int usblp_submit_read(struct usblp *usblp);
224
static int usblp_select_alts(struct usblp *usblp);
238
static int usblp_select_alts(struct usblp *usblp);
225
static int usblp_set_protocol(struct usblp *usblp, int protocol);
239
static int usblp_set_protocol(struct usblp *usblp, int protocol);
226
static int usblp_cache_device_id_string(struct usblp *usblp);
240
static int usblp_cache_device_id_string(struct usblp *usblp);
Lines 247-253 static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, i Link Here
247
261
248
	retval = usb_control_msg(usblp->dev,
262
	retval = usb_control_msg(usblp->dev,
249
		dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
263
		dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
250
		request, type | dir | recip, value, index, buf, len, USBLP_WRITE_TIMEOUT);
264
		request, type | dir | recip, value, index, buf, len, USBLP_CTL_TIMEOUT);
251
	dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d",
265
	dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d",
252
		request, !!dir, recip, value, index, len, retval);
266
		request, !!dir, recip, value, index, len, retval);
253
	return retval < 0 ? retval : 0;
267
	return retval < 0 ? retval : 0;
Lines 277-310 static int proto_bias = -1; Link Here
277
static void usblp_bulk_read(struct urb *urb)
291
static void usblp_bulk_read(struct urb *urb)
278
{
292
{
279
	struct usblp *usblp = urb->context;
293
	struct usblp *usblp = urb->context;
294
	int status = urb->status;
280
295
281
	if (unlikely(!usblp || !usblp->dev || !usblp->used))
296
	if (usblp->present && usblp->used) {
282
		return;
297
		if (status)
283
298
			printk(KERN_WARNING "usblp%d: "
284
	if (unlikely(!usblp->present))
299
			    "nonzero read bulk status received: %d\n",
285
		goto unplug;
300
			    usblp->minor, status);
286
	if (unlikely(urb->status))
301
	}
287
		warn("usblp%d: nonzero read/write bulk status received: %d",
302
	spin_lock(&usblp->lock);
288
			usblp->minor, urb->status);
303
	if (status < 0)
304
		usblp->rstatus = status;
305
	else
306
		usblp->rstatus = urb->actual_length;
289
	usblp->rcomplete = 1;
307
	usblp->rcomplete = 1;
290
unplug:
308
	wake_up(&usblp->rwait);
291
	wake_up_interruptible(&usblp->wait);
309
	spin_unlock(&usblp->lock);
310
311
	usb_free_urb(urb);
292
}
312
}
293
313
294
static void usblp_bulk_write(struct urb *urb)
314
static void usblp_bulk_write(struct urb *urb)
295
{
315
{
296
	struct usblp *usblp = urb->context;
316
	struct usblp *usblp = urb->context;
317
	int status = urb->status;
297
318
298
	if (unlikely(!usblp || !usblp->dev || !usblp->used))
319
	if (usblp->present && usblp->used) {
299
		return;
320
		if (status)
300
	if (unlikely(!usblp->present))
321
			printk(KERN_WARNING "usblp%d: "
301
		goto unplug;
322
			    "nonzero write bulk status received: %d\n",
302
	if (unlikely(urb->status))
323
			    usblp->minor, status);
303
		warn("usblp%d: nonzero read/write bulk status received: %d",
324
	}
304
			usblp->minor, urb->status);
325
	spin_lock(&usblp->lock);
326
	if (status < 0)
327
		usblp->wstatus = status;
328
	else
329
		usblp->wstatus = urb->actual_length;
330
	usblp->no_paper = 0;
305
	usblp->wcomplete = 1;
331
	usblp->wcomplete = 1;
306
unplug:
332
	wake_up(&usblp->wwait);
307
	wake_up_interruptible(&usblp->wait);
333
	spin_unlock(&usblp->lock);
334
335
	usb_free_urb(urb);
308
}
336
}
309
337
310
/*
338
/*
Lines 318-332 static int usblp_check_status(struct usblp *usblp, int err) Link Here
318
	unsigned char status, newerr = 0;
346
	unsigned char status, newerr = 0;
319
	int error;
347
	int error;
320
348
321
	error = usblp_read_status (usblp, usblp->statusbuf);
349
	mutex_lock(&usblp->mut);
322
	if (error < 0) {
350
	if ((error = usblp_read_status(usblp, usblp->statusbuf)) < 0) {
351
		mutex_unlock(&usblp->mut);
323
		if (printk_ratelimit())
352
		if (printk_ratelimit())
324
			err("usblp%d: error %d reading printer status",
353
			printk(KERN_ERR
354
				"usblp%d: error %d reading printer status\n",
325
				usblp->minor, error);
355
				usblp->minor, error);
326
		return 0;
356
		return 0;
327
	}
357
	}
328
329
	status = *usblp->statusbuf;
358
	status = *usblp->statusbuf;
359
	mutex_unlock(&usblp->mut);
330
360
331
	if (~status & LP_PERRORP)
361
	if (~status & LP_PERRORP)
332
		newerr = 3;
362
		newerr = 3;
Lines 335-342 static int usblp_check_status(struct usblp *usblp, int err) Link Here
335
	if (~status & LP_PSELECD)
365
	if (~status & LP_PSELECD)
336
		newerr = 2;
366
		newerr = 2;
337
367
338
	if (newerr != err)
368
	if (newerr != err) {
339
		info("usblp%d: %s", usblp->minor, usblp_messages[newerr]);
369
		printk(KERN_INFO "usblp%d: %s\n",
370
		   usblp->minor, usblp_messages[newerr]);
371
	}
340
372
341
	return newerr;
373
	return newerr;
342
}
374
}
Lines 344-357 static int usblp_check_status(struct usblp *usblp, int err) Link Here
344
static int handle_bidir (struct usblp *usblp)
376
static int handle_bidir (struct usblp *usblp)
345
{
377
{
346
	if (usblp->bidir && usblp->used && !usblp->sleeping) {
378
	if (usblp->bidir && usblp->used && !usblp->sleeping) {
347
		usblp->readcount = 0;
379
		if (usblp_submit_read(usblp) < 0)
348
		usblp->readurb->dev = usblp->dev;
349
		if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0) {
350
			usblp->used = 0;
351
			return -EIO;
380
			return -EIO;
352
		}
353
	}
381
	}
354
355
	return 0;
382
	return 0;
356
}
383
}
357
384
Lines 385-413 static int usblp_open(struct inode *inode, struct file *file) Link Here
385
		goto out;
412
		goto out;
386
413
387
	/*
414
	/*
388
	 * TODO: need to implement LP_ABORTOPEN + O_NONBLOCK as in drivers/char/lp.c ???
415
	 * We do not implement LP_ABORTOPEN/LPABORTOPEN for two reasons:
389
	 * This is #if 0-ed because we *don't* want to fail an open
416
	 *  - We do not want persistent state which close(2) does not clear
390
	 * just because the printer is off-line.
417
	 *  - It is not used anyway, according to CUPS people
391
	 */
418
	 */
392
#if 0
393
	if ((retval = usblp_check_status(usblp, 0))) {
394
		retval = retval > 1 ? -EIO : -ENOSPC;
395
		goto out;
396
	}
397
#else
398
	retval = 0;
399
#endif
400
419
420
	retval = usb_autopm_get_interface(intf);
421
	if (retval < 0)
422
		goto out;
401
	usblp->used = 1;
423
	usblp->used = 1;
402
	file->private_data = usblp;
424
	file->private_data = usblp;
403
425
404
	usblp->writeurb->transfer_buffer_length = 0;
405
	usblp->wcomplete = 1; /* we begin writeable */
426
	usblp->wcomplete = 1; /* we begin writeable */
427
	usblp->wstatus = 0;
406
	usblp->rcomplete = 0;
428
	usblp->rcomplete = 0;
407
	usblp->writeurb->status = 0;
408
	usblp->readurb->status = 0;
409
429
410
	if (handle_bidir(usblp) < 0) {
430
	if (handle_bidir(usblp) < 0) {
431
		usb_autopm_put_interface(intf);
432
		usblp->used = 0;
411
		file->private_data = NULL;
433
		file->private_data = NULL;
412
		retval = -EIO;
434
		retval = -EIO;
413
	}
435
	}
Lines 418-447 out: Link Here
418
440
419
static void usblp_cleanup (struct usblp *usblp)
441
static void usblp_cleanup (struct usblp *usblp)
420
{
442
{
421
	info("usblp%d: removed", usblp->minor);
443
	printk(KERN_INFO "usblp%d: removed\n", usblp->minor);
422
444
445
	kfree(usblp->readbuf);
423
	kfree (usblp->device_id_string);
446
	kfree (usblp->device_id_string);
424
	kfree (usblp->statusbuf);
447
	kfree (usblp->statusbuf);
425
	usb_free_urb(usblp->writeurb);
426
	usb_free_urb(usblp->readurb);
427
	kfree (usblp);
448
	kfree (usblp);
428
}
449
}
429
450
430
static void usblp_unlink_urbs(struct usblp *usblp)
451
static void usblp_unlink_urbs(struct usblp *usblp)
431
{
452
{
432
	usb_kill_urb(usblp->writeurb);
453
	usb_kill_anchored_urbs(&usblp->urbs);
433
	if (usblp->bidir)
434
		usb_kill_urb(usblp->readurb);
435
}
454
}
436
455
437
static int usblp_release(struct inode *inode, struct file *file)
456
static int usblp_release(struct inode *inode, struct file *file)
438
{
457
{
439
	struct usblp *usblp = file->private_data;
458
	struct usblp *usblp = file->private_data;
440
459
460
	usblp->flags &= ~LP_ABORT;
461
441
	mutex_lock (&usblp_mutex);
462
	mutex_lock (&usblp_mutex);
442
	usblp->used = 0;
463
	usblp->used = 0;
443
	if (usblp->present) {
464
	if (usblp->present) {
444
		usblp_unlink_urbs(usblp);
465
		usblp_unlink_urbs(usblp);
466
		usb_autopm_put_interface(usblp->intf);
445
	} else 		/* finish cleanup from disconnect */
467
	} else 		/* finish cleanup from disconnect */
446
		usblp_cleanup (usblp);
468
		usblp_cleanup (usblp);
447
	mutex_unlock (&usblp_mutex);
469
	mutex_unlock (&usblp_mutex);
Lines 451-460 static int usblp_release(struct inode *inode, struct file *file) Link Here
451
/* No kernel lock - fine */
473
/* No kernel lock - fine */
452
static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
474
static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
453
{
475
{
476
	int ret;
477
	unsigned long flags;
478
454
	struct usblp *usblp = file->private_data;
479
	struct usblp *usblp = file->private_data;
455
	poll_wait(file, &usblp->wait, wait);
480
	/* Should we check file->f_mode & FMODE_WRITE before poll_wait()? */
456
 	return ((!usblp->bidir || !usblp->rcomplete) ? 0 : POLLIN  | POLLRDNORM)
481
	poll_wait(file, &usblp->rwait, wait);
457
 			       | (!usblp->wcomplete ? 0 : POLLOUT | POLLWRNORM);
482
	poll_wait(file, &usblp->wwait, wait);
483
	spin_lock_irqsave(&usblp->lock, flags);
484
	ret = ((usblp->bidir && usblp->rcomplete) ? POLLIN  | POLLRDNORM : 0) |
485
	   ((usblp->no_paper || usblp->wcomplete) ? POLLOUT | POLLWRNORM : 0);
486
	spin_unlock_irqrestore(&usblp->lock, flags);
487
	return ret;
458
}
488
}
459
489
460
static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
490
static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Lines 628-637 static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) Link Here
628
		switch (cmd) {
658
		switch (cmd) {
629
659
630
			case LPGETSTATUS:
660
			case LPGETSTATUS:
631
				if (usblp_read_status(usblp, usblp->statusbuf)) {
661
				if ((retval = usblp_read_status(usblp, usblp->statusbuf))) {
632
					if (printk_ratelimit())
662
					if (printk_ratelimit())
633
						err("usblp%d: failed reading printer status",
663
						printk(KERN_ERR "usblp%d:"
634
							usblp->minor);
664
						    "failed reading printer status (%d)\n",
665
						    usblp->minor, retval);
635
					retval = -EIO;
666
					retval = -EIO;
636
					goto done;
667
					goto done;
637
				}
668
				}
Lines 640-645 static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) Link Here
640
					retval = -EFAULT;
671
					retval = -EFAULT;
641
				break;
672
				break;
642
673
674
			case LPABORT:
675
				if (arg)
676
					usblp->flags |= LP_ABORT;
677
				else
678
					usblp->flags &= ~LP_ABORT;
679
				break;
680
643
			default:
681
			default:
644
				retval = -ENOTTY;
682
				retval = -ENOTTY;
645
		}
683
		}
Lines 649-819 done: Link Here
649
	return retval;
687
	return retval;
650
}
688
}
651
689
652
static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
690
static struct urb *usblp_new_writeurb(struct usblp *usblp, int transfer_length)
653
{
691
{
654
	struct usblp *usblp = file->private_data;
692
	struct urb *urb;
655
	int timeout, intr, rv, err = 0, transfer_length = 0;
693
	char *writebuf;
656
	size_t writecount = 0;
694
657
695
	if ((writebuf = kmalloc(transfer_length, GFP_KERNEL)) == NULL)
658
	while (writecount < count) {
696
		return NULL;
659
		if (!usblp->wcomplete) {
697
	if ((urb = usb_alloc_urb(0, GFP_KERNEL)) == NULL) {
660
			barrier();
698
		kfree(writebuf);
661
			if (file->f_flags & O_NONBLOCK) {
699
		return NULL;
662
				writecount += transfer_length;
700
	}
663
				return writecount ? writecount : -EAGAIN;
664
			}
665
666
			timeout = USBLP_WRITE_TIMEOUT;
667
668
			rv = wait_event_interruptible_timeout(usblp->wait, usblp->wcomplete || !usblp->present , timeout);
669
			if (rv < 0)
670
				return writecount ? writecount : -EINTR;
671
		}
672
		intr = mutex_lock_interruptible (&usblp->mut);
673
		if (intr)
674
			return writecount ? writecount : -EINTR;
675
		if (!usblp->present) {
676
			mutex_unlock (&usblp->mut);
677
			return -ENODEV;
678
		}
679
701
680
		if (usblp->sleeping) {
702
	usb_fill_bulk_urb(urb, usblp->dev,
681
			mutex_unlock (&usblp->mut);
703
		usb_sndbulkpipe(usblp->dev,
682
			return writecount ? writecount : -ENODEV;
704
		 usblp->protocol[usblp->current_protocol].epwrite->bEndpointAddress),
683
		}
705
		writebuf, transfer_length, usblp_bulk_write, usblp);
706
	urb->transfer_flags |= URB_FREE_BUFFER;
684
707
685
		if (usblp->writeurb->status != 0) {
708
	return urb;
686
			if (usblp->quirks & USBLP_QUIRK_BIDIR) {
709
}
687
				if (!usblp->wcomplete)
688
					err("usblp%d: error %d writing to printer",
689
						usblp->minor, usblp->writeurb->status);
690
				err = usblp->writeurb->status;
691
			} else
692
				err = usblp_check_status(usblp, err);
693
			mutex_unlock (&usblp->mut);
694
710
695
			/* if the fault was due to disconnect, let khubd's
711
static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
696
			 * call to usblp_disconnect() grab usblp->mut ...
712
{
697
			 */
713
	struct usblp *usblp = file->private_data;
698
			schedule ();
714
	struct urb *writeurb;
699
			continue;
715
	int rv;
700
		}
716
	int transfer_length;
717
	ssize_t writecount = 0;
718
719
	if (mutex_lock_interruptible(&usblp->wmut)) {
720
		rv = -EINTR;
721
		goto raise_biglock;
722
	}
723
	if ((rv = usblp_wwait(usblp, !!(file->f_flags & O_NONBLOCK))) < 0)
724
		goto raise_wait;
701
725
702
		/* We must increment writecount here, and not at the
726
	while (writecount < count) {
703
		 * end of the loop. Otherwise, the final loop iteration may
727
		/*
704
		 * be skipped, leading to incomplete printer output.
728
		 * Step 1: Submit next block.
705
		 */
729
		 */
706
		writecount += transfer_length;
730
		if ((transfer_length = count - writecount) > USBLP_BUF_SIZE)
707
		if (writecount == count) {
708
			mutex_unlock(&usblp->mut);
709
			break;
710
		}
711
712
		transfer_length=(count - writecount);
713
		if (transfer_length > USBLP_BUF_SIZE)
714
			transfer_length = USBLP_BUF_SIZE;
731
			transfer_length = USBLP_BUF_SIZE;
715
732
716
		usblp->writeurb->transfer_buffer_length = transfer_length;
733
		rv = -ENOMEM;
734
		if ((writeurb = usblp_new_writeurb(usblp, transfer_length)) == NULL)
735
			goto raise_urb;
736
		usb_anchor_urb(writeurb, &usblp->urbs);
717
737
718
		if (copy_from_user(usblp->writeurb->transfer_buffer, 
738
		if (copy_from_user(writeurb->transfer_buffer,
719
				   buffer + writecount, transfer_length)) {
739
				   buffer + writecount, transfer_length)) {
720
			mutex_unlock(&usblp->mut);
740
			rv = -EFAULT;
721
			return writecount ? writecount : -EFAULT;
741
			goto raise_badaddr;
722
		}
742
		}
723
743
724
		usblp->writeurb->dev = usblp->dev;
744
		spin_lock_irq(&usblp->lock);
725
		usblp->wcomplete = 0;
745
		usblp->wcomplete = 0;
726
		err = usb_submit_urb(usblp->writeurb, GFP_KERNEL);
746
		spin_unlock_irq(&usblp->lock);
727
		if (err) {
747
		if ((rv = usb_submit_urb(writeurb, GFP_KERNEL)) < 0) {
748
			usblp->wstatus = 0;
749
			spin_lock_irq(&usblp->lock);
750
			usblp->no_paper = 0;
728
			usblp->wcomplete = 1;
751
			usblp->wcomplete = 1;
729
			if (err != -ENOMEM)
752
			wake_up(&usblp->wwait);
730
				count = -EIO;
753
			spin_unlock_irq(&usblp->lock);
731
			else
754
			if (rv != -ENOMEM)
732
				count = writecount ? writecount : -ENOMEM;
755
				rv = -EIO;
733
			mutex_unlock (&usblp->mut);
756
			goto raise_submit;
734
			break;
735
		}
757
		}
736
		mutex_unlock (&usblp->mut);
758
759
		/*
760
		 * Step 2: Wait for transfer to end, collect results.
761
		 */
762
		rv = usblp_wwait(usblp, !!(file->f_flags&O_NONBLOCK));
763
		if (rv < 0) {
764
			if (rv == -EAGAIN) {
765
				/* Presume that it's going to complete well. */
766
				writecount += transfer_length;
767
			}
768
			if (rv == -ENOSPC) {
769
				spin_lock_irq(&usblp->lock);
770
				usblp->no_paper = 1;	/* Mark for poll(2) */
771
				spin_unlock_irq(&usblp->lock);
772
				writecount += transfer_length;
773
			}
774
			/* Leave URB dangling, to be cleaned on close. */
775
			goto collect_error;
776
		}
777
778
		if (usblp->wstatus < 0) {
779
			rv = -EIO;
780
			goto collect_error;
781
		}
782
		/*
783
		 * This is critical: it must be our URB, not other writer's.
784
		 * The wmut exists mainly to cover us here.
785
		 */
786
		writecount += usblp->wstatus;
737
	}
787
	}
738
788
739
	return count;
789
	mutex_unlock(&usblp->wmut);
790
	return writecount;
791
792
raise_submit:
793
raise_badaddr:
794
	usb_unanchor_urb(writeurb);
795
	usb_free_urb(writeurb);
796
raise_urb:
797
raise_wait:
798
collect_error:		/* Out of raise sequence */
799
	mutex_unlock(&usblp->wmut);
800
raise_biglock:
801
	return writecount ? writecount : rv;
740
}
802
}
741
803
742
static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
804
/*
805
 * Notice that we fail to restart in a few cases: on EFAULT, on restart
806
 * error, etc. This is the historical behaviour. In all such cases we return
807
 * EIO, and applications loop in order to get the new read going.
808
 */
809
static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, loff_t *ppos)
743
{
810
{
744
	struct usblp *usblp = file->private_data;
811
	struct usblp *usblp = file->private_data;
745
	int rv, intr;
812
	ssize_t count;
813
	ssize_t avail;
814
	int rv;
746
815
747
	if (!usblp->bidir)
816
	if (!usblp->bidir)
748
		return -EINVAL;
817
		return -EINVAL;
749
818
750
	intr = mutex_lock_interruptible (&usblp->mut);
819
	rv = usblp_rwait_and_lock(usblp, !!(file->f_flags & O_NONBLOCK));
751
	if (intr)
820
	if (rv < 0)
752
		return -EINTR;
821
		return rv;
753
	if (!usblp->present) {
822
754
		count = -ENODEV;
823
	if ((avail = usblp->rstatus) < 0) {
824
		printk(KERN_ERR "usblp%d: error %d reading from printer\n",
825
		    usblp->minor, (int)avail);
826
		usblp_submit_read(usblp);
827
		count = -EIO;
755
		goto done;
828
		goto done;
756
	}
829
	}
757
830
758
	if (!usblp->rcomplete) {
831
	count = len < avail - usblp->readcount ? len : avail - usblp->readcount;
759
		barrier();
832
	if (count != 0 &&
833
	    copy_to_user(buffer, usblp->readbuf + usblp->readcount, count)) {
834
		count = -EFAULT;
835
		goto done;
836
	}
760
837
761
		if (file->f_flags & O_NONBLOCK) {
838
	if ((usblp->readcount += count) == avail) {
762
			count = -EAGAIN;
839
		if (usblp_submit_read(usblp) < 0) {
840
			/* We don't want to leak USB return codes into errno. */
841
			if (count == 0)
842
				count = -EIO;
763
			goto done;
843
			goto done;
764
		}
844
		}
845
	}
846
847
done:
848
	mutex_unlock (&usblp->mut);
849
	return count;
850
}
851
852
/*
853
 * Wait for the write path to come idle.
854
 * This is called under the ->wmut, so the idle path stays idle.
855
 *
856
 * Our write path has a peculiar property: it does not buffer like a tty,
857
 * but waits for the write to succeed. This allows our ->release to bug out
858
 * without waiting for writes to drain. But it obviously does not work
859
 * when O_NONBLOCK is set. So, applications setting O_NONBLOCK must use
860
 * select(2) or poll(2) to wait for the buffer to drain before closing.
861
 * Alternatively, set blocking mode with fcntl and issue a zero-size write.
862
 */
863
static int usblp_wwait(struct usblp *usblp, int nonblock)
864
{
865
	DECLARE_WAITQUEUE(waita, current);
866
	int rc;
867
	int err = 0;
868
869
	add_wait_queue(&usblp->wwait, &waita);
870
	for (;;) {
871
		set_current_state(TASK_INTERRUPTIBLE);
872
		if (mutex_lock_interruptible(&usblp->mut)) {
873
			rc = -EINTR;
874
			break;
875
		}
876
		rc = usblp_wtest(usblp, nonblock);
765
		mutex_unlock(&usblp->mut);
877
		mutex_unlock(&usblp->mut);
766
		rv = wait_event_interruptible(usblp->wait, usblp->rcomplete || !usblp->present);
878
		if (rc <= 0)
767
		mutex_lock(&usblp->mut);
879
			break;
768
		if (rv < 0) {
880
769
			count = -EINTR;
881
		if (usblp->flags & LP_ABORT) {
770
			goto done;
882
			if (schedule_timeout(msecs_to_jiffies(5000)) == 0) {
883
				err = usblp_check_status(usblp, err);
884
				if (err == 1) {	/* Paper out */
885
					rc = -ENOSPC;
886
					break;
887
				}
888
			}
889
		} else {
890
			schedule();
771
		}
891
		}
772
	}
892
	}
893
	set_current_state(TASK_RUNNING);
894
	remove_wait_queue(&usblp->wwait, &waita);
895
	return rc;
896
}
773
897
774
	if (!usblp->present) {
898
static int usblp_wtest(struct usblp *usblp, int nonblock)
775
		count = -ENODEV;
899
{
776
		goto done;
900
	unsigned long flags;
777
	}
778
901
779
	if (usblp->sleeping) {
902
	if (!usblp->present)
780
		count = -ENODEV;
903
		return -ENODEV;
781
		goto done;
904
	if (signal_pending(current))
905
		return -EINTR;
906
	spin_lock_irqsave(&usblp->lock, flags);
907
	if (usblp->wcomplete) {
908
		spin_unlock_irqrestore(&usblp->lock, flags);
909
		return 0;
782
	}
910
	}
911
	spin_unlock_irqrestore(&usblp->lock, flags);
912
	if (usblp->sleeping)
913
		return -ENODEV;
914
	if (nonblock)
915
		return -EAGAIN;
916
	return 1;
917
}
783
918
784
	if (usblp->readurb->status) {
919
/*
785
		err("usblp%d: error %d reading from printer",
920
 * Wait for read bytes to become available. This probably should have been
786
			usblp->minor, usblp->readurb->status);
921
 * called usblp_r_lock_and_wait(), because we lock first. But it's a traditional
787
		usblp->readurb->dev = usblp->dev;
922
 * name for functions which lock and return.
788
 		usblp->readcount = 0;
923
 *
789
		usblp->rcomplete = 0;
924
 * We do not use wait_event_interruptible because it makes locking iffy.
790
		if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0)
925
 */
791
			dbg("error submitting urb");
926
static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock)
792
		count = -EIO;
927
{
793
		goto done;
928
	DECLARE_WAITQUEUE(waita, current);
929
	int rc;
930
931
	add_wait_queue(&usblp->rwait, &waita);
932
	for (;;) {
933
		if (mutex_lock_interruptible(&usblp->mut)) {
934
			rc = -EINTR;
935
			break;
936
		}
937
		set_current_state(TASK_INTERRUPTIBLE);
938
		if ((rc = usblp_rtest(usblp, nonblock)) < 0) {
939
			mutex_unlock(&usblp->mut);
940
			break;
941
		}
942
		if (rc == 0)	/* Keep it locked */
943
			break;
944
		mutex_unlock(&usblp->mut);
945
		schedule();
794
	}
946
	}
947
	set_current_state(TASK_RUNNING);
948
	remove_wait_queue(&usblp->rwait, &waita);
949
	return rc;
950
}
795
951
796
	count = count < usblp->readurb->actual_length - usblp->readcount ?
952
static int usblp_rtest(struct usblp *usblp, int nonblock)
797
		count :	usblp->readurb->actual_length - usblp->readcount;
953
{
954
	unsigned long flags;
798
955
799
	if (copy_to_user(buffer, usblp->readurb->transfer_buffer + usblp->readcount, count)) {
956
	if (!usblp->present)
800
		count = -EFAULT;
957
		return -ENODEV;
801
		goto done;
958
	if (signal_pending(current))
959
		return -EINTR;
960
	spin_lock_irqsave(&usblp->lock, flags);
961
	if (usblp->rcomplete) {
962
		spin_unlock_irqrestore(&usblp->lock, flags);
963
		return 0;
802
	}
964
	}
965
	spin_unlock_irqrestore(&usblp->lock, flags);
966
	if (usblp->sleeping)
967
		return -ENODEV;
968
	if (nonblock)
969
		return -EAGAIN;
970
	return 1;
971
}
803
972
804
	if ((usblp->readcount += count) == usblp->readurb->actual_length) {
973
/*
805
		usblp->readcount = 0;
974
 * Please check ->bidir and other such things outside for now.
806
		usblp->readurb->dev = usblp->dev;
975
 */
807
		usblp->rcomplete = 0;
976
static int usblp_submit_read(struct usblp *usblp)
808
		if (usb_submit_urb(usblp->readurb, GFP_KERNEL)) {
977
{
809
			count = -EIO;
978
	struct urb *urb;
810
			goto done;
979
	unsigned long flags;
811
		}
980
	int rc;
981
982
	rc = -ENOMEM;
983
	if ((urb = usb_alloc_urb(0, GFP_KERNEL)) == NULL)
984
		goto raise_urb;
985
986
	usb_fill_bulk_urb(urb, usblp->dev,
987
		usb_rcvbulkpipe(usblp->dev,
988
		  usblp->protocol[usblp->current_protocol].epread->bEndpointAddress),
989
		usblp->readbuf, USBLP_BUF_SIZE_IN,
990
		usblp_bulk_read, usblp);
991
	usb_anchor_urb(urb, &usblp->urbs);
992
993
	spin_lock_irqsave(&usblp->lock, flags);
994
	usblp->readcount = 0; /* XXX Why here? */
995
	usblp->rcomplete = 0;
996
	spin_unlock_irqrestore(&usblp->lock, flags);
997
	if ((rc = usb_submit_urb(urb, GFP_KERNEL)) < 0) {
998
		dbg("error submitting urb (%d)", rc);
999
		spin_lock_irqsave(&usblp->lock, flags);
1000
		usblp->rstatus = rc;
1001
		usblp->rcomplete = 1;
1002
		spin_unlock_irqrestore(&usblp->lock, flags);
1003
		goto raise_submit;
812
	}
1004
	}
813
1005
814
done:
1006
	return 0;
815
	mutex_unlock (&usblp->mut);
1007
816
	return count;
1008
raise_submit:
1009
	usb_unanchor_urb(urb);
1010
	usb_free_urb(urb);
1011
raise_urb:
1012
	return rc;
817
}
1013
}
818
1014
819
/*
1015
/*
Lines 887-941 static int usblp_probe(struct usb_interface *intf, Link Here
887
	/* Malloc and start initializing usblp structure so we can use it
1083
	/* Malloc and start initializing usblp structure so we can use it
888
	 * directly. */
1084
	 * directly. */
889
	if (!(usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL))) {
1085
	if (!(usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL))) {
890
		err("out of memory for usblp");
1086
		retval = -ENOMEM;
891
		goto abort;
1087
		goto abort;
892
	}
1088
	}
893
	usblp->dev = dev;
1089
	usblp->dev = dev;
1090
	mutex_init(&usblp->wmut);
894
	mutex_init (&usblp->mut);
1091
	mutex_init (&usblp->mut);
895
	init_waitqueue_head(&usblp->wait);
1092
	spin_lock_init(&usblp->lock);
1093
	init_waitqueue_head(&usblp->rwait);
1094
	init_waitqueue_head(&usblp->wwait);
1095
	init_usb_anchor(&usblp->urbs);
896
	usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
1096
	usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
897
	usblp->intf = intf;
1097
	usblp->intf = intf;
898
1098
899
	usblp->writeurb = usb_alloc_urb(0, GFP_KERNEL);
900
	if (!usblp->writeurb) {
901
		err("out of memory");
902
		goto abort;
903
	}
904
	usblp->readurb = usb_alloc_urb(0, GFP_KERNEL);
905
	if (!usblp->readurb) {
906
		err("out of memory");
907
		goto abort;
908
	}
909
910
	/* Malloc device ID string buffer to the largest expected length,
1099
	/* Malloc device ID string buffer to the largest expected length,
911
	 * since we can re-query it on an ioctl and a dynamic string
1100
	 * since we can re-query it on an ioctl and a dynamic string
912
	 * could change in length. */
1101
	 * could change in length. */
913
	if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
1102
	if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
914
		err("out of memory for device_id_string");
1103
		retval = -ENOMEM;
915
		goto abort;
1104
		goto abort;
916
	}
1105
	}
917
1106
918
	usblp->writebuf = usblp->readbuf = NULL;
1107
	/*
919
	usblp->writeurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1108
	 * Allocate read buffer. We somewhat wastefully
920
	usblp->readurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
921
	/* Malloc write & read buffers.  We somewhat wastefully
922
	 * malloc both regardless of bidirectionality, because the
1109
	 * malloc both regardless of bidirectionality, because the
923
	 * alternate setting can be changed later via an ioctl. */
1110
	 * alternate setting can be changed later via an ioctl.
924
	if (!(usblp->writebuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
1111
	 */
925
				GFP_KERNEL, &usblp->writeurb->transfer_dma))) {
1112
	if (!(usblp->readbuf = kmalloc(USBLP_BUF_SIZE_IN, GFP_KERNEL))) {
926
		err("out of memory for write buf");
1113
		retval = -ENOMEM;
927
		goto abort;
928
	}
929
	if (!(usblp->readbuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
930
				GFP_KERNEL, &usblp->readurb->transfer_dma))) {
931
		err("out of memory for read buf");
932
		goto abort;
1114
		goto abort;
933
	}
1115
	}
934
1116
935
	/* Allocate buffer for printer status */
1117
	/* Allocate buffer for printer status */
936
	usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
1118
	usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
937
	if (!usblp->statusbuf) {
1119
	if (!usblp->statusbuf) {
938
		err("out of memory for statusbuf");
1120
		retval = -ENOMEM;
939
		goto abort;
1121
		goto abort;
940
	}
1122
	}
941
1123
Lines 950-961 static int usblp_probe(struct usb_interface *intf, Link Here
950
		dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
1132
		dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
951
			le16_to_cpu(dev->descriptor.idVendor),
1133
			le16_to_cpu(dev->descriptor.idVendor),
952
			le16_to_cpu(dev->descriptor.idProduct));
1134
			le16_to_cpu(dev->descriptor.idProduct));
1135
		retval = -ENODEV;
953
		goto abort;
1136
		goto abort;
954
	}
1137
	}
955
1138
956
	/* Setup the selected alternate setting and endpoints. */
1139
	/* Setup the selected alternate setting and endpoints. */
957
	if (usblp_set_protocol(usblp, protocol) < 0)
1140
	if (usblp_set_protocol(usblp, protocol) < 0) {
1141
		retval = -ENODEV;	/* ->probe isn't ->ioctl */
958
		goto abort;
1142
		goto abort;
1143
	}
959
1144
960
	/* Retrieve and store the device ID string. */
1145
	/* Retrieve and store the device ID string. */
961
	usblp_cache_device_id_string(usblp);
1146
	usblp_cache_device_id_string(usblp);
Lines 973-984 static int usblp_probe(struct usb_interface *intf, Link Here
973
1158
974
	retval = usb_register_dev(intf, &usblp_class);
1159
	retval = usb_register_dev(intf, &usblp_class);
975
	if (retval) {
1160
	if (retval) {
976
		err("Not able to get a minor for this device.");
1161
		printk(KERN_ERR "usblp: Not able to get a minor"
1162
		    " (base %u, slice default): %d\n",
1163
		    USBLP_MINOR_BASE, retval);
977
		goto abort_intfdata;
1164
		goto abort_intfdata;
978
	}
1165
	}
979
	usblp->minor = intf->minor;
1166
	usblp->minor = intf->minor;
980
	info("usblp%d: USB %sdirectional printer dev %d "
1167
	printk(KERN_INFO "usblp%d: USB %sdirectional printer dev %d "
981
		"if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
1168
		"if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X\n",
982
		usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
1169
		usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
983
		usblp->ifnum,
1170
		usblp->ifnum,
984
		usblp->protocol[usblp->current_protocol].alt_setting,
1171
		usblp->protocol[usblp->current_protocol].alt_setting,
Lines 993-1011 abort_intfdata: Link Here
993
	device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
1180
	device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
994
abort:
1181
abort:
995
	if (usblp) {
1182
	if (usblp) {
996
		if (usblp->writebuf)
1183
		kfree(usblp->readbuf);
997
			usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
998
				usblp->writebuf, usblp->writeurb->transfer_dma);
999
		if (usblp->readbuf)
1000
			usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1001
				usblp->readbuf, usblp->writeurb->transfer_dma);
1002
		kfree(usblp->statusbuf);
1184
		kfree(usblp->statusbuf);
1003
		kfree(usblp->device_id_string);
1185
		kfree(usblp->device_id_string);
1004
		usb_free_urb(usblp->writeurb);
1005
		usb_free_urb(usblp->readurb);
1006
		kfree(usblp);
1186
		kfree(usblp);
1007
	}
1187
	}
1008
	return -EIO;
1188
	return retval;
1009
}
1189
}
1010
1190
1011
/*
1191
/*
Lines 1044-1050 static int usblp_select_alts(struct usblp *usblp) Link Here
1044
		ifd = &if_alt->altsetting[i];
1224
		ifd = &if_alt->altsetting[i];
1045
1225
1046
		if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)
1226
		if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)
1047
			continue;
1227
			if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS))
1228
				continue;
1048
1229
1049
		if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
1230
		if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
1050
		    ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
1231
		    ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
Lines 1073-1080 static int usblp_select_alts(struct usblp *usblp) Link Here
1073
		if (ifd->desc.bInterfaceProtocol == 1) {
1254
		if (ifd->desc.bInterfaceProtocol == 1) {
1074
			epread = NULL;
1255
			epread = NULL;
1075
		} else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
1256
		} else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
1076
			info("Disabling reads from problem bidirectional "
1257
			printk(KERN_INFO "usblp%d: Disabling reads from "
1077
				"printer on usblp%d", usblp->minor);
1258
			    "problematic bidirectional printer\n",
1259
			    usblp->minor);
1078
			epread = NULL;
1260
			epread = NULL;
1079
		}
1261
		}
1080
1262
Lines 1114-1138 static int usblp_set_protocol(struct usblp *usblp, int protocol) Link Here
1114
		return -EINVAL;
1296
		return -EINVAL;
1115
	r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
1297
	r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
1116
	if (r < 0) {
1298
	if (r < 0) {
1117
		err("can't set desired altsetting %d on interface %d",
1299
		printk(KERN_ERR "usblp: can't set desired altsetting %d on interface %d\n",
1118
			alts, usblp->ifnum);
1300
			alts, usblp->ifnum);
1119
		return r;
1301
		return r;
1120
	}
1302
	}
1121
1303
1122
	usb_fill_bulk_urb(usblp->writeurb, usblp->dev,
1123
		usb_sndbulkpipe(usblp->dev,
1124
		  usblp->protocol[protocol].epwrite->bEndpointAddress),
1125
		usblp->writebuf, 0,
1126
		usblp_bulk_write, usblp);
1127
1128
	usblp->bidir = (usblp->protocol[protocol].epread != NULL);
1304
	usblp->bidir = (usblp->protocol[protocol].epread != NULL);
1129
	if (usblp->bidir)
1130
		usb_fill_bulk_urb(usblp->readurb, usblp->dev,
1131
			usb_rcvbulkpipe(usblp->dev,
1132
			  usblp->protocol[protocol].epread->bEndpointAddress),
1133
			usblp->readbuf, USBLP_BUF_SIZE,
1134
			usblp_bulk_read, usblp);
1135
1136
	usblp->current_protocol = protocol;
1305
	usblp->current_protocol = protocol;
1137
	dbg("usblp%d set protocol %d", usblp->minor, protocol);
1306
	dbg("usblp%d set protocol %d", usblp->minor, protocol);
1138
	return 0;
1307
	return 0;
Lines 1185-1197 static void usblp_disconnect(struct usb_interface *intf) Link Here
1185
	mutex_lock (&usblp_mutex);
1354
	mutex_lock (&usblp_mutex);
1186
	mutex_lock (&usblp->mut);
1355
	mutex_lock (&usblp->mut);
1187
	usblp->present = 0;
1356
	usblp->present = 0;
1357
	wake_up(&usblp->wwait);
1358
	wake_up(&usblp->rwait);
1188
	usb_set_intfdata (intf, NULL);
1359
	usb_set_intfdata (intf, NULL);
1189
1360
1190
	usblp_unlink_urbs(usblp);
1361
	usblp_unlink_urbs(usblp);
1191
	usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1192
			usblp->writebuf, usblp->writeurb->transfer_dma);
1193
	usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
1194
			usblp->readbuf, usblp->readurb->transfer_dma);
1195
	mutex_unlock (&usblp->mut);
1362
	mutex_unlock (&usblp->mut);
1196
1363
1197
	if (!usblp->used)
1364
	if (!usblp->used)
Lines 1203-1216 static int usblp_suspend (struct usb_interface *intf, pm_message_t message) Link Here
1203
{
1370
{
1204
	struct usblp *usblp = usb_get_intfdata (intf);
1371
	struct usblp *usblp = usb_get_intfdata (intf);
1205
1372
1206
	/* this races against normal access and open */
1207
	mutex_lock (&usblp_mutex);
1208
	mutex_lock (&usblp->mut);
1209
	/* we take no more IO */
1373
	/* we take no more IO */
1210
	usblp->sleeping = 1;
1374
	usblp->sleeping = 1;
1211
	usblp_unlink_urbs(usblp);
1375
	usblp_unlink_urbs(usblp);
1212
	mutex_unlock (&usblp->mut);
1376
#if 0 /* XXX Do we want this? What if someone is reading, should we fail? */
1213
	mutex_unlock (&usblp_mutex);
1377
	/* not strictly necessary, but just in case */
1378
	wake_up(&usblp->wwait);
1379
	wake_up(&usblp->rwait);
1380
#endif
1214
1381
1215
	return 0;
1382
	return 0;
1216
}
1383
}
Lines 1220-1234 static int usblp_resume (struct usb_interface *intf) Link Here
1220
	struct usblp *usblp = usb_get_intfdata (intf);
1387
	struct usblp *usblp = usb_get_intfdata (intf);
1221
	int r;
1388
	int r;
1222
1389
1223
	mutex_lock (&usblp_mutex);
1224
	mutex_lock (&usblp->mut);
1225
1226
	usblp->sleeping = 0;
1390
	usblp->sleeping = 0;
1227
	r = handle_bidir (usblp);
1391
	r = handle_bidir (usblp);
1228
1392
1229
	mutex_unlock (&usblp->mut);
1230
	mutex_unlock (&usblp_mutex);
1231
1232
	return r;
1393
	return r;
1233
}
1394
}
1234
1395
Lines 1239-1244 static struct usb_device_id usblp_ids [] = { Link Here
1239
	{ USB_INTERFACE_INFO(7, 1, 1) },
1400
	{ USB_INTERFACE_INFO(7, 1, 1) },
1240
	{ USB_INTERFACE_INFO(7, 1, 2) },
1401
	{ USB_INTERFACE_INFO(7, 1, 2) },
1241
	{ USB_INTERFACE_INFO(7, 1, 3) },
1402
	{ USB_INTERFACE_INFO(7, 1, 3) },
1403
	{ USB_DEVICE(0x04b8, 0x0202) },	/* Seiko Epson Receipt Printer M129C */
1242
	{ }						/* Terminating entry */
1404
	{ }						/* Terminating entry */
1243
};
1405
};
1244
1406
Lines 1251-1266 static struct usb_driver usblp_driver = { Link Here
1251
	.suspend =	usblp_suspend,
1413
	.suspend =	usblp_suspend,
1252
	.resume =	usblp_resume,
1414
	.resume =	usblp_resume,
1253
	.id_table =	usblp_ids,
1415
	.id_table =	usblp_ids,
1416
	.supports_autosuspend =	1,
1254
};
1417
};
1255
1418
1256
static int __init usblp_init(void)
1419
static int __init usblp_init(void)
1257
{
1420
{
1258
	int retval;
1421
	return usb_register(&usblp_driver);
1259
	retval = usb_register(&usblp_driver);
1260
	if (!retval)
1261
		info(DRIVER_VERSION ": " DRIVER_DESC);
1262
1263
	return retval;
1264
}
1422
}
1265
1423
1266
static void __exit usblp_exit(void)
1424
static void __exit usblp_exit(void)

Return to bug 251237