50 #include <linux/module.h>
51 #include <linux/kernel.h>
52 #include <linux/sched.h>
53 #include <linux/signal.h>
54 #include <linux/poll.h>
56 #include <linux/slab.h>
66 #define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
67 #define DRIVER_DESC "USB Printer Device Class driver"
69 #define USBLP_BUF_SIZE 8192
70 #define USBLP_BUF_SIZE_IN 1024
71 #define USBLP_DEVICE_ID_SIZE 1024
74 #define IOCNR_GET_DEVICE_ID 1
75 #define IOCNR_GET_PROTOCOLS 2
76 #define IOCNR_SET_PROTOCOL 3
77 #define IOCNR_HP_SET_CHANNEL 4
78 #define IOCNR_GET_BUS_ADDRESS 5
79 #define IOCNR_GET_VID_PID 6
80 #define IOCNR_SOFT_RESET 7
82 #define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
87 #define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
89 #define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
91 #define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
93 #define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
95 #define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
97 #define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0);
111 #define USBLP_REQ_GET_ID 0x00
112 #define USBLP_REQ_GET_STATUS 0x01
113 #define USBLP_REQ_RESET 0x02
114 #define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST 0x00
116 #define USBLP_MINORS 16
117 #define USBLP_MINOR_BASE 0
119 #define USBLP_CTL_TIMEOUT 5000
121 #define USBLP_FIRST_PROTOCOL 1
122 #define USBLP_LAST_PROTOCOL 3
123 #define USBLP_MAX_PROTOCOLS (USBLP_LAST_PROTOCOL+1)
129 #define STATUS_BUF_SIZE 8
178 dev_dbg(dev,
"usblp=0x%p\n", usblp);
185 dev_dbg(dev,
"protocol[%d].alt_setting=%d\n", p,
187 dev_dbg(dev,
"protocol[%d].epwrite=%p\n", p,
189 dev_dbg(dev,
"protocol[%d].epread=%p\n", p,
199 dev_dbg(dev,
"device_id_string=\"%s\"\n",
202 (
unsigned char *)
"(null)");
214 #define USBLP_QUIRK_BIDIR 0x1
215 #define USBLP_QUIRK_USB_INIT 0x2
216 #define USBLP_QUIRK_BAD_CLASS 0x4
238 static int usblp_wwait(
struct usblp *
usblp,
int nonblock);
239 static int usblp_wtest(
struct usblp *
usblp,
int nonblock);
240 static int usblp_rwait_and_lock(
struct usblp *
usblp,
int nonblock);
241 static int usblp_rtest(
struct usblp *
usblp,
int nonblock);
242 static int usblp_submit_read(
struct usblp *
usblp);
243 static int usblp_select_alts(
struct usblp *
usblp);
245 static int usblp_cache_device_id_string(
struct usblp *
usblp);
248 static struct usb_driver usblp_driver;
267 dir ? usb_rcvctrlpipe(usblp->
dev, 0) : usb_sndctrlpipe(usblp->
dev, 0),
270 "usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d\n",
271 request, !!dir, recip, value, index, len, retval);
272 return retval < 0 ? retval : 0;
275 #define usblp_read_status(usblp, status)\
276 usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
277 #define usblp_get_id(usblp, config, id, maxlen)\
278 usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
279 #define usblp_reset(usblp)\
280 usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
282 #define usblp_hp_channel_change_request(usblp, channel, buffer) \
283 usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
290 static int proto_bias = -1;
296 static void usblp_bulk_read(
struct urb *
urb)
298 struct usblp *usblp = urb->context;
304 "nonzero read bulk status received: %d\n",
305 usblp->
minor, status);
307 spin_lock(&usblp->
lock);
311 usblp->
rstatus = urb->actual_length;
314 spin_unlock(&usblp->
lock);
319 static void usblp_bulk_write(
struct urb *
urb)
321 struct usblp *usblp = urb->context;
327 "nonzero write bulk status received: %d\n",
328 usblp->
minor, status);
330 spin_lock(&usblp->
lock);
334 usblp->
wstatus = urb->actual_length;
338 spin_unlock(&usblp->
lock);
347 static const char *usblp_messages[] = {
"ok",
"out of paper",
"off-line",
"on fire" };
349 static int usblp_check_status(
struct usblp *usblp,
int err)
351 unsigned char status, newerr = 0;
358 "usblp%d: error %d reading printer status\n",
359 usblp->
minor, error);
374 usblp->
minor, usblp_messages[newerr]);
380 static int handle_bidir(
struct usblp *usblp)
383 if (usblp_submit_read(usblp) < 0)
395 int minor = iminor(inode);
409 usblp = usb_get_intfdata(intf);
423 retval = usb_autopm_get_interface(intf);
433 if (handle_bidir(usblp) < 0) {
434 usb_autopm_put_interface(intf);
444 static void usblp_cleanup(
struct usblp *usblp)
454 static void usblp_unlink_urbs(
struct usblp *usblp)
459 static int usblp_release(
struct inode *inode,
struct file *file)
468 usblp_unlink_urbs(usblp);
469 usb_autopm_put_interface(usblp->
intf);
471 usblp_cleanup(usblp);
484 poll_wait(file, &usblp->
rwait, wait);
485 poll_wait(file, &usblp->
wwait, wait);
489 spin_unlock_irqrestore(&usblp->
lock, flags);
493 static long usblp_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
497 unsigned char newChannel;
509 "usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)\n", cmd,
522 length = usblp_cache_device_id_string(usblp);
532 (
unsigned long) length)) {
550 if (usblp->
protocol[i].alt_setting >= 0)
551 twoints[1] |= (1<<
i);
555 (
unsigned char *)twoints,
576 usblp_unlink_urbs(usblp);
577 retval = usblp_set_protocol(usblp, arg);
579 usblp_set_protocol(usblp,
596 "usblp%d: error = %d setting "
604 "usblp%d requested/got HP channel %ld/%d\n",
605 usblp->
minor, arg, newChannel);
615 twoints[0] = usblp->
dev->bus->busnum;
616 twoints[1] = usblp->
dev->devnum;
618 (
unsigned char *)twoints,
625 "usblp%d is bus=%d, device=%d\n",
626 usblp->
minor, twoints[0], twoints[1]);
639 (
unsigned char *)twoints,
646 "usblp%d is VID=0x%4.4X, PID=0x%4.4X\n",
647 usblp->
minor, twoints[0], twoints[1]);
666 "failed reading printer status (%d)\n",
667 usblp->
minor, retval);
672 if (
copy_to_user((
void __user *)arg, &status,
sizeof(
int)))
692 static struct urb *usblp_new_writeurb(
struct usblp *usblp,
int transfer_length)
704 usb_fill_bulk_urb(urb, usblp->
dev,
705 usb_sndbulkpipe(usblp->
dev,
707 writebuf, transfer_length, usblp_bulk_write, usblp);
708 urb->transfer_flags |= URB_FREE_BUFFER;
713 static ssize_t usblp_write(
struct file *file,
const char __user *
buffer,
size_t count, loff_t *ppos)
716 struct urb *writeurb;
728 while (writecount < count) {
736 if ((writeurb = usblp_new_writeurb(usblp, transfer_length)) ==
NULL)
741 buffer + writecount, transfer_length)) {
746 spin_lock_irq(&usblp->
lock);
748 spin_unlock_irq(&usblp->
lock);
751 spin_lock_irq(&usblp->
lock);
755 spin_unlock_irq(&usblp->
lock);
768 writecount += transfer_length;
771 spin_lock_irq(&usblp->
lock);
773 spin_unlock_irq(&usblp->
lock);
774 writecount += transfer_length;
803 return writecount ? writecount : rv;
811 static ssize_t usblp_read(
struct file *file,
char __user *buffer,
size_t len, loff_t *ppos)
825 if ((avail = usblp->
rstatus) < 0) {
827 usblp->
minor, (
int)avail);
828 usblp_submit_read(usblp);
840 if ((usblp->
readcount += count) == avail) {
841 if (usblp_submit_read(usblp) < 0) {
865 static int usblp_wwait(
struct usblp *usblp,
int nonblock)
878 rc = usblp_wtest(usblp, nonblock);
885 err = usblp_check_status(usblp, err);
903 static int usblp_wtest(
struct usblp *usblp,
int nonblock)
913 spin_unlock_irqrestore(&usblp->
lock, flags);
916 spin_unlock_irqrestore(&usblp->
lock, flags);
929 static int usblp_rwait_and_lock(
struct usblp *usblp,
int nonblock)
941 if ((rc = usblp_rtest(usblp, nonblock)) < 0) {
955 static int usblp_rtest(
struct usblp *usblp,
int nonblock)
965 spin_unlock_irqrestore(&usblp->
lock, flags);
968 spin_unlock_irqrestore(&usblp->
lock, flags);
977 static int usblp_submit_read(
struct usblp *usblp)
987 usb_fill_bulk_urb(urb, usblp->
dev,
988 usb_rcvbulkpipe(usblp->
dev,
991 usblp_bulk_read, usblp);
997 spin_unlock_irqrestore(&usblp->
lock, flags);
999 dev_dbg(&usblp->
intf->dev,
"error submitting urb (%d)\n", rc);
1003 spin_unlock_irqrestore(&usblp->
lock, flags);
1037 for (i = 0; quirk_printers[
i].
vendorId; i++) {
1038 if (vendor == quirk_printers[i].
vendorId &&
1039 product == quirk_printers[i].productId)
1040 return quirk_printers[
i].
quirks;
1048 .write = usblp_write,
1050 .unlocked_ioctl = usblp_ioctl,
1051 .compat_ioctl = usblp_ioctl,
1053 .release = usblp_release,
1062 static struct usb_class_driver usblp_class = {
1064 .devnode = usblp_devnode,
1065 .fops = &usblp_fops,
1072 struct usblp *usblp = usb_get_intfdata(intf);
1086 struct usb_device *dev = interface_to_usbdev(intf);
1087 struct usblp *usblp;
1093 usblp = kzalloc(
sizeof(
struct usblp),
GFP_KERNEL);
1104 init_usb_anchor(&usblp->
urbs);
1105 usblp->
ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
1134 usblp->
quirks = usblp_quirks(
1139 protocol = usblp_select_alts(usblp);
1142 "incompatible printer-class device 0x%4.4X/0x%4.4X\n",
1150 if (usblp_set_protocol(usblp, protocol) < 0) {
1156 usblp_cache_device_id_string(usblp);
1159 goto abort_intfdata;
1162 usblp_check_status(usblp, 0);
1165 usb_set_intfdata(intf, usblp);
1172 "usblp: Not able to get a minor (base %u, slice default): %d\n",
1174 goto abort_intfdata;
1176 usblp->
minor = intf->minor;
1178 "usblp%d: USB %sdirectional printer dev %d if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X\n",
1179 usblp->
minor, usblp->
bidir ?
"Bi" :
"Uni", dev->devnum,
1189 usb_set_intfdata(intf,
NULL);
1219 static int usblp_select_alts(
struct usblp *usblp)
1222 struct usb_host_interface *ifd;
1226 if_alt = usblp->
intf;
1229 usblp->
protocol[p].alt_setting = -1;
1232 for (i = 0; i < if_alt->num_altsetting; i++) {
1233 ifd = &if_alt->altsetting[
i];
1235 if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)
1244 epwrite = epread =
NULL;
1245 for (e = 0; e < ifd->desc.bNumEndpoints; e++) {
1246 epd = &ifd->endpoint[
e].desc;
1248 if (usb_endpoint_is_bulk_out(epd))
1252 if (usb_endpoint_is_bulk_in(epd))
1258 if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread))
1263 if (ifd->desc.bInterfaceProtocol == 1) {
1267 "problematic bidirectional printer\n",
1272 usblp->
protocol[ifd->desc.bInterfaceProtocol].alt_setting =
1273 ifd->desc.bAlternateSetting;
1274 usblp->
protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
1275 usblp->
protocol[ifd->desc.bInterfaceProtocol].epread = epread;
1281 usblp->
protocol[proto_bias].alt_setting != -1)
1285 if (usblp->
protocol[2].alt_setting != -1)
1287 if (usblp->
protocol[1].alt_setting != -1)
1289 if (usblp->
protocol[3].alt_setting != -1)
1296 static int usblp_set_protocol(
struct usblp *usblp,
int protocol)
1308 printk(
KERN_ERR "usblp: can't set desired altsetting %d on interface %d\n",
1309 alts, usblp->
ifnum);
1315 dev_dbg(&usblp->
intf->dev,
"usblp%d set protocol %d\n",
1316 usblp->
minor, protocol);
1323 static int usblp_cache_device_id_string(
struct usblp *usblp)
1330 "usblp%d: error = %d reading IEEE-1284 Device ID string\n",
1346 dev_dbg(&usblp->
intf->dev,
"usblp%d Device ID string [len=%d]=\"%s\"\n",
1354 struct usblp *usblp = usb_get_intfdata(intf);
1358 if (!usblp || !usblp->
dev) {
1359 dev_err(&intf->dev,
"bogus disconnect\n");
1370 usb_set_intfdata(intf,
NULL);
1372 usblp_unlink_urbs(usblp);
1376 usblp_cleanup(usblp);
1382 struct usblp *usblp = usb_get_intfdata(intf);
1384 usblp_unlink_urbs(usblp);
1396 struct usblp *usblp = usb_get_intfdata(intf);
1399 r = handle_bidir(usblp);
1405 { USB_DEVICE_INFO(7, 1, 1) },
1406 { USB_DEVICE_INFO(7, 1, 2) },
1407 { USB_DEVICE_INFO(7, 1, 3) },
1408 { USB_INTERFACE_INFO(7, 1, 1) },
1409 { USB_INTERFACE_INFO(7, 1, 2) },
1410 { USB_INTERFACE_INFO(7, 1, 3) },
1411 { USB_DEVICE(0x04b8, 0x0202) },
1417 static struct usb_driver usblp_driver = {
1419 .probe = usblp_probe,
1420 .disconnect = usblp_disconnect,
1421 .suspend = usblp_suspend,
1422 .resume = usblp_resume,
1423 .id_table = usblp_ids,
1424 .supports_autosuspend = 1,