usblp.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * usblp.c
  4. *
  5. * Copyright (c) 1999 Michael Gee <michael@linuxspecific.com>
  6. * Copyright (c) 1999 Pavel Machek <pavel@ucw.cz>
  7. * Copyright (c) 2000 Randy Dunlap <rdunlap@xenotime.net>
  8. * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz>
  9. # Copyright (c) 2001 Pete Zaitcev <zaitcev@redhat.com>
  10. # Copyright (c) 2001 David Paschal <paschal@rcsis.com>
  11. * Copyright (c) 2006 Oliver Neukum <oliver@neukum.name>
  12. *
  13. * USB Printer Device Class driver for USB printers and printer cables
  14. *
  15. * Sponsored by SuSE
  16. *
  17. * ChangeLog:
  18. * v0.1 - thorough cleaning, URBification, almost a rewrite
  19. * v0.2 - some more cleanups
  20. * v0.3 - cleaner again, waitqueue fixes
  21. * v0.4 - fixes in unidirectional mode
  22. * v0.5 - add DEVICE_ID string support
  23. * v0.6 - never time out
  24. * v0.7 - fixed bulk-IN read and poll (David Paschal)
  25. * v0.8 - add devfs support
  26. * v0.9 - fix unplug-while-open paths
  27. * v0.10- remove sleep_on, fix error on oom (oliver@neukum.org)
  28. * v0.11 - add proto_bias option (Pete Zaitcev)
  29. * v0.12 - add hpoj.sourceforge.net ioctls (David Paschal)
  30. * v0.13 - alloc space for statusbuf (<status> not on stack);
  31. * use usb_alloc_coherent() for read buf & write buf;
  32. * none - Maintained in Linux kernel after v0.13
  33. */
  34. #include <linux/module.h>
  35. #include <linux/kernel.h>
  36. #include <linux/sched/signal.h>
  37. #include <linux/signal.h>
  38. #include <linux/poll.h>
  39. #include <linux/slab.h>
  40. #include <linux/lp.h>
  41. #include <linux/mutex.h>
  42. #undef DEBUG
  43. #include <linux/usb.h>
  44. #include <linux/usb/ch9.h>
  45. #include <linux/ratelimit.h>
  46. /*
  47. * Version Information
  48. */
  49. #define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
  50. #define DRIVER_DESC "USB Printer Device Class driver"
  51. #define USBLP_BUF_SIZE 8192
  52. #define USBLP_BUF_SIZE_IN 1024
  53. #define USBLP_DEVICE_ID_SIZE 1024
  54. /* ioctls: */
  55. #define IOCNR_GET_DEVICE_ID 1
  56. #define IOCNR_GET_PROTOCOLS 2
  57. #define IOCNR_SET_PROTOCOL 3
  58. #define IOCNR_HP_SET_CHANNEL 4
  59. #define IOCNR_GET_BUS_ADDRESS 5
  60. #define IOCNR_GET_VID_PID 6
  61. #define IOCNR_SOFT_RESET 7
  62. /* Get device_id string: */
  63. #define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
  64. /* The following ioctls were added for http://hpoj.sourceforge.net:
  65. * Get two-int array:
  66. * [0]=current protocol
  67. * (1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2,
  68. * 3=USB_CLASS_PRINTER/1/3),
  69. * [1]=supported protocol mask (mask&(1<<n)!=0 means
  70. * USB_CLASS_PRINTER/1/n supported):
  71. */
  72. #define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
  73. /*
  74. * Set protocol
  75. * (arg: 1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2,
  76. * 3=USB_CLASS_PRINTER/1/3):
  77. */
  78. #define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
  79. /* Set channel number (HP Vendor-specific command): */
  80. #define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
  81. /* Get two-int array: [0]=bus number, [1]=device address: */
  82. #define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
  83. /* Get two-int array: [0]=vendor ID, [1]=product ID: */
  84. #define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
  85. /* Perform class specific soft reset */
  86. #define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0);
  87. /*
  88. * A DEVICE_ID string may include the printer's serial number.
  89. * It should end with a semi-colon (';').
  90. * An example from an HP 970C DeskJet printer is (this is one long string,
  91. * with the serial number changed):
  92. MFG: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: ;
  93. */
  94. /*
  95. * USB Printer Requests
  96. */
  97. #define USBLP_REQ_GET_ID 0x00
  98. #define USBLP_REQ_GET_STATUS 0x01
  99. #define USBLP_REQ_RESET 0x02
  100. #define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST 0x00 /* HP Vendor-specific */
  101. #define USBLP_MINORS 16
  102. #define USBLP_MINOR_BASE 0
  103. #define USBLP_CTL_TIMEOUT 5000 /* 5 seconds */
  104. #define USBLP_FIRST_PROTOCOL 1
  105. #define USBLP_LAST_PROTOCOL 3
  106. #define USBLP_MAX_PROTOCOLS (USBLP_LAST_PROTOCOL+1)
  107. /*
  108. * some arbitrary status buffer size;
  109. * need a status buffer that is allocated via kmalloc(), not on stack
  110. */
  111. #define STATUS_BUF_SIZE 8
  112. /*
  113. * Locks down the locking order:
  114. * ->wmut locks wstatus.
  115. * ->mut locks the whole usblp, except [rw]complete, and thus, by indirection,
  116. * [rw]status. We only touch status when we know the side idle.
  117. * ->lock locks what interrupt accesses.
  118. */
  119. struct usblp {
  120. struct usb_device *dev; /* USB device */
  121. struct mutex wmut;
  122. struct mutex mut;
  123. spinlock_t lock; /* locks rcomplete, wcomplete */
  124. char *readbuf; /* read transfer_buffer */
  125. char *statusbuf; /* status transfer_buffer */
  126. struct usb_anchor urbs;
  127. wait_queue_head_t rwait, wwait;
  128. int readcount; /* Counter for reads */
  129. int ifnum; /* Interface number */
  130. struct usb_interface *intf; /* The interface */
  131. /*
  132. * Alternate-setting numbers and endpoints for each protocol
  133. * (USB_CLASS_PRINTER/1/{index=1,2,3}) that the device supports:
  134. */
  135. struct {
  136. int alt_setting;
  137. struct usb_endpoint_descriptor *epwrite;
  138. struct usb_endpoint_descriptor *epread;
  139. } protocol[USBLP_MAX_PROTOCOLS];
  140. int current_protocol;
  141. int minor; /* minor number of device */
  142. int wcomplete, rcomplete;
  143. int wstatus; /* bytes written or error */
  144. int rstatus; /* bytes ready or error */
  145. unsigned int quirks; /* quirks flags */
  146. unsigned int flags; /* mode flags */
  147. unsigned char used; /* True if open */
  148. unsigned char present; /* True if not disconnected */
  149. unsigned char bidir; /* interface is bidirectional */
  150. unsigned char no_paper; /* Paper Out happened */
  151. unsigned char *device_id_string; /* IEEE 1284 DEVICE ID string (ptr) */
  152. /* first 2 bytes are (big-endian) length */
  153. };
  154. #ifdef DEBUG
  155. static void usblp_dump(struct usblp *usblp)
  156. {
  157. struct device *dev = &usblp->intf->dev;
  158. int p;
  159. dev_dbg(dev, "usblp=0x%p\n", usblp);
  160. dev_dbg(dev, "dev=0x%p\n", usblp->dev);
  161. dev_dbg(dev, "present=%d\n", usblp->present);
  162. dev_dbg(dev, "readbuf=0x%p\n", usblp->readbuf);
  163. dev_dbg(dev, "readcount=%d\n", usblp->readcount);
  164. dev_dbg(dev, "ifnum=%d\n", usblp->ifnum);
  165. for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
  166. dev_dbg(dev, "protocol[%d].alt_setting=%d\n", p,
  167. usblp->protocol[p].alt_setting);
  168. dev_dbg(dev, "protocol[%d].epwrite=%p\n", p,
  169. usblp->protocol[p].epwrite);
  170. dev_dbg(dev, "protocol[%d].epread=%p\n", p,
  171. usblp->protocol[p].epread);
  172. }
  173. dev_dbg(dev, "current_protocol=%d\n", usblp->current_protocol);
  174. dev_dbg(dev, "minor=%d\n", usblp->minor);
  175. dev_dbg(dev, "wstatus=%d\n", usblp->wstatus);
  176. dev_dbg(dev, "rstatus=%d\n", usblp->rstatus);
  177. dev_dbg(dev, "quirks=%d\n", usblp->quirks);
  178. dev_dbg(dev, "used=%d\n", usblp->used);
  179. dev_dbg(dev, "bidir=%d\n", usblp->bidir);
  180. dev_dbg(dev, "device_id_string=\"%s\"\n",
  181. usblp->device_id_string ?
  182. usblp->device_id_string + 2 :
  183. (unsigned char *)"(null)");
  184. }
  185. #endif
  186. /* Quirks: various printer quirks are handled by this table & its flags. */
  187. struct quirk_printer_struct {
  188. __u16 vendorId;
  189. __u16 productId;
  190. unsigned int quirks;
  191. };
  192. #define USBLP_QUIRK_BIDIR 0x1 /* reports bidir but requires unidirectional mode (no INs/reads) */
  193. #define USBLP_QUIRK_USB_INIT 0x2 /* needs vendor USB init string */
  194. #define USBLP_QUIRK_BAD_CLASS 0x4 /* descriptor uses vendor-specific Class or SubClass */
  195. static const struct quirk_printer_struct quirk_printers[] = {
  196. { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
  197. { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
  198. { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
  199. { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
  200. { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
  201. { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
  202. { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */
  203. { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */
  204. { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
  205. { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
  206. { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
  207. { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
  208. { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
  209. { 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
  210. { 0x04f9, 0x000d, USBLP_QUIRK_BIDIR }, /* Brother Industries, Ltd HL-1440 Laser Printer */
  211. { 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt Printer M129C */
  212. { 0, 0 }
  213. };
  214. static int usblp_wwait(struct usblp *usblp, int nonblock);
  215. static int usblp_wtest(struct usblp *usblp, int nonblock);
  216. static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock);
  217. static int usblp_rtest(struct usblp *usblp, int nonblock);
  218. static int usblp_submit_read(struct usblp *usblp);
  219. static int usblp_select_alts(struct usblp *usblp);
  220. static int usblp_set_protocol(struct usblp *usblp, int protocol);
  221. static int usblp_cache_device_id_string(struct usblp *usblp);
  222. /* forward reference to make our lives easier */
  223. static struct usb_driver usblp_driver;
  224. static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */
  225. /*
  226. * Functions for usblp control messages.
  227. */
  228. static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
  229. {
  230. int retval;
  231. int index = usblp->ifnum;
  232. /* High byte has the interface index.
  233. Low byte has the alternate setting.
  234. */
  235. if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS))
  236. index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
  237. retval = usb_control_msg(usblp->dev,
  238. dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
  239. request, type | dir | recip, value, index, buf, len, USBLP_CTL_TIMEOUT);
  240. dev_dbg(&usblp->intf->dev,
  241. "usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d\n",
  242. request, !!dir, recip, value, index, len, retval);
  243. return retval < 0 ? retval : 0;
  244. }
  245. #define usblp_read_status(usblp, status)\
  246. usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
  247. #define usblp_get_id(usblp, config, id, maxlen)\
  248. usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
  249. #define usblp_reset(usblp)\
  250. usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
  251. #define usblp_hp_channel_change_request(usblp, channel, buffer) \
  252. usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
  253. /*
  254. * See the description for usblp_select_alts() below for the usage
  255. * explanation. Look into your /sys/kernel/debug/usb/devices and dmesg in
  256. * case of any trouble.
  257. */
  258. static int proto_bias = -1;
  259. /*
  260. * URB callback.
  261. */
  262. static void usblp_bulk_read(struct urb *urb)
  263. {
  264. struct usblp *usblp = urb->context;
  265. int status = urb->status;
  266. if (usblp->present && usblp->used) {
  267. if (status)
  268. printk(KERN_WARNING "usblp%d: "
  269. "nonzero read bulk status received: %d\n",
  270. usblp->minor, status);
  271. }
  272. spin_lock(&usblp->lock);
  273. if (status < 0)
  274. usblp->rstatus = status;
  275. else
  276. usblp->rstatus = urb->actual_length;
  277. usblp->rcomplete = 1;
  278. wake_up(&usblp->rwait);
  279. spin_unlock(&usblp->lock);
  280. usb_free_urb(urb);
  281. }
  282. static void usblp_bulk_write(struct urb *urb)
  283. {
  284. struct usblp *usblp = urb->context;
  285. int status = urb->status;
  286. if (usblp->present && usblp->used) {
  287. if (status)
  288. printk(KERN_WARNING "usblp%d: "
  289. "nonzero write bulk status received: %d\n",
  290. usblp->minor, status);
  291. }
  292. spin_lock(&usblp->lock);
  293. if (status < 0)
  294. usblp->wstatus = status;
  295. else
  296. usblp->wstatus = urb->actual_length;
  297. usblp->no_paper = 0;
  298. usblp->wcomplete = 1;
  299. wake_up(&usblp->wwait);
  300. spin_unlock(&usblp->lock);
  301. usb_free_urb(urb);
  302. }
  303. /*
  304. * Get and print printer errors.
  305. */
  306. static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
  307. static int usblp_check_status(struct usblp *usblp, int err)
  308. {
  309. unsigned char status, newerr = 0;
  310. int error;
  311. mutex_lock(&usblp->mut);
  312. if ((error = usblp_read_status(usblp, usblp->statusbuf)) < 0) {
  313. mutex_unlock(&usblp->mut);
  314. printk_ratelimited(KERN_ERR
  315. "usblp%d: error %d reading printer status\n",
  316. usblp->minor, error);
  317. return 0;
  318. }
  319. status = *usblp->statusbuf;
  320. mutex_unlock(&usblp->mut);
  321. if (~status & LP_PERRORP)
  322. newerr = 3;
  323. if (status & LP_POUTPA)
  324. newerr = 1;
  325. if (~status & LP_PSELECD)
  326. newerr = 2;
  327. if (newerr != err) {
  328. printk(KERN_INFO "usblp%d: %s\n",
  329. usblp->minor, usblp_messages[newerr]);
  330. }
  331. return newerr;
  332. }
  333. static int handle_bidir(struct usblp *usblp)
  334. {
  335. if (usblp->bidir && usblp->used) {
  336. if (usblp_submit_read(usblp) < 0)
  337. return -EIO;
  338. }
  339. return 0;
  340. }
  341. /*
  342. * File op functions.
  343. */
  344. static int usblp_open(struct inode *inode, struct file *file)
  345. {
  346. int minor = iminor(inode);
  347. struct usblp *usblp;
  348. struct usb_interface *intf;
  349. int retval;
  350. if (minor < 0)
  351. return -ENODEV;
  352. mutex_lock(&usblp_mutex);
  353. retval = -ENODEV;
  354. intf = usb_find_interface(&usblp_driver, minor);
  355. if (!intf)
  356. goto out;
  357. usblp = usb_get_intfdata(intf);
  358. if (!usblp || !usblp->dev || !usblp->present)
  359. goto out;
  360. retval = -EBUSY;
  361. if (usblp->used)
  362. goto out;
  363. /*
  364. * We do not implement LP_ABORTOPEN/LPABORTOPEN for two reasons:
  365. * - We do not want persistent state which close(2) does not clear
  366. * - It is not used anyway, according to CUPS people
  367. */
  368. retval = usb_autopm_get_interface(intf);
  369. if (retval < 0)
  370. goto out;
  371. usblp->used = 1;
  372. file->private_data = usblp;
  373. usblp->wcomplete = 1; /* we begin writeable */
  374. usblp->wstatus = 0;
  375. usblp->rcomplete = 0;
  376. if (handle_bidir(usblp) < 0) {
  377. usb_autopm_put_interface(intf);
  378. usblp->used = 0;
  379. file->private_data = NULL;
  380. retval = -EIO;
  381. }
  382. out:
  383. mutex_unlock(&usblp_mutex);
  384. return retval;
  385. }
  386. static void usblp_cleanup(struct usblp *usblp)
  387. {
  388. printk(KERN_INFO "usblp%d: removed\n", usblp->minor);
  389. kfree(usblp->readbuf);
  390. kfree(usblp->device_id_string);
  391. kfree(usblp->statusbuf);
  392. kfree(usblp);
  393. }
  394. static void usblp_unlink_urbs(struct usblp *usblp)
  395. {
  396. usb_kill_anchored_urbs(&usblp->urbs);
  397. }
  398. static int usblp_release(struct inode *inode, struct file *file)
  399. {
  400. struct usblp *usblp = file->private_data;
  401. usblp->flags &= ~LP_ABORT;
  402. mutex_lock(&usblp_mutex);
  403. usblp->used = 0;
  404. if (usblp->present) {
  405. usblp_unlink_urbs(usblp);
  406. usb_autopm_put_interface(usblp->intf);
  407. } else /* finish cleanup from disconnect */
  408. usblp_cleanup(usblp);
  409. mutex_unlock(&usblp_mutex);
  410. return 0;
  411. }
  412. /* No kernel lock - fine */
  413. static __poll_t usblp_poll(struct file *file, struct poll_table_struct *wait)
  414. {
  415. __poll_t ret;
  416. unsigned long flags;
  417. struct usblp *usblp = file->private_data;
  418. /* Should we check file->f_mode & FMODE_WRITE before poll_wait()? */
  419. poll_wait(file, &usblp->rwait, wait);
  420. poll_wait(file, &usblp->wwait, wait);
  421. spin_lock_irqsave(&usblp->lock, flags);
  422. ret = ((usblp->bidir && usblp->rcomplete) ? POLLIN | POLLRDNORM : 0) |
  423. ((usblp->no_paper || usblp->wcomplete) ? POLLOUT | POLLWRNORM : 0);
  424. spin_unlock_irqrestore(&usblp->lock, flags);
  425. return ret;
  426. }
  427. static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  428. {
  429. struct usblp *usblp = file->private_data;
  430. int length, err, i;
  431. unsigned char newChannel;
  432. int status;
  433. int twoints[2];
  434. int retval = 0;
  435. mutex_lock(&usblp->mut);
  436. if (!usblp->present) {
  437. retval = -ENODEV;
  438. goto done;
  439. }
  440. dev_dbg(&usblp->intf->dev,
  441. "usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)\n", cmd,
  442. _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd));
  443. if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */
  444. switch (_IOC_NR(cmd)) {
  445. case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
  446. if (_IOC_DIR(cmd) != _IOC_READ) {
  447. retval = -EINVAL;
  448. goto done;
  449. }
  450. length = usblp_cache_device_id_string(usblp);
  451. if (length < 0) {
  452. retval = length;
  453. goto done;
  454. }
  455. if (length > _IOC_SIZE(cmd))
  456. length = _IOC_SIZE(cmd); /* truncate */
  457. if (copy_to_user((void __user *) arg,
  458. usblp->device_id_string,
  459. (unsigned long) length)) {
  460. retval = -EFAULT;
  461. goto done;
  462. }
  463. break;
  464. case IOCNR_GET_PROTOCOLS:
  465. if (_IOC_DIR(cmd) != _IOC_READ ||
  466. _IOC_SIZE(cmd) < sizeof(twoints)) {
  467. retval = -EINVAL;
  468. goto done;
  469. }
  470. twoints[0] = usblp->current_protocol;
  471. twoints[1] = 0;
  472. for (i = USBLP_FIRST_PROTOCOL;
  473. i <= USBLP_LAST_PROTOCOL; i++) {
  474. if (usblp->protocol[i].alt_setting >= 0)
  475. twoints[1] |= (1<<i);
  476. }
  477. if (copy_to_user((void __user *)arg,
  478. (unsigned char *)twoints,
  479. sizeof(twoints))) {
  480. retval = -EFAULT;
  481. goto done;
  482. }
  483. break;
  484. case IOCNR_SET_PROTOCOL:
  485. if (_IOC_DIR(cmd) != _IOC_WRITE) {
  486. retval = -EINVAL;
  487. goto done;
  488. }
  489. #ifdef DEBUG
  490. if (arg == -10) {
  491. usblp_dump(usblp);
  492. break;
  493. }
  494. #endif
  495. usblp_unlink_urbs(usblp);
  496. retval = usblp_set_protocol(usblp, arg);
  497. if (retval < 0) {
  498. usblp_set_protocol(usblp,
  499. usblp->current_protocol);
  500. }
  501. break;
  502. case IOCNR_HP_SET_CHANNEL:
  503. if (_IOC_DIR(cmd) != _IOC_WRITE ||
  504. le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
  505. usblp->quirks & USBLP_QUIRK_BIDIR) {
  506. retval = -EINVAL;
  507. goto done;
  508. }
  509. err = usblp_hp_channel_change_request(usblp,
  510. arg, &newChannel);
  511. if (err < 0) {
  512. dev_err(&usblp->dev->dev,
  513. "usblp%d: error = %d setting "
  514. "HP channel\n",
  515. usblp->minor, err);
  516. retval = -EIO;
  517. goto done;
  518. }
  519. dev_dbg(&usblp->intf->dev,
  520. "usblp%d requested/got HP channel %ld/%d\n",
  521. usblp->minor, arg, newChannel);
  522. break;
  523. case IOCNR_GET_BUS_ADDRESS:
  524. if (_IOC_DIR(cmd) != _IOC_READ ||
  525. _IOC_SIZE(cmd) < sizeof(twoints)) {
  526. retval = -EINVAL;
  527. goto done;
  528. }
  529. twoints[0] = usblp->dev->bus->busnum;
  530. twoints[1] = usblp->dev->devnum;
  531. if (copy_to_user((void __user *)arg,
  532. (unsigned char *)twoints,
  533. sizeof(twoints))) {
  534. retval = -EFAULT;
  535. goto done;
  536. }
  537. dev_dbg(&usblp->intf->dev,
  538. "usblp%d is bus=%d, device=%d\n",
  539. usblp->minor, twoints[0], twoints[1]);
  540. break;
  541. case IOCNR_GET_VID_PID:
  542. if (_IOC_DIR(cmd) != _IOC_READ ||
  543. _IOC_SIZE(cmd) < sizeof(twoints)) {
  544. retval = -EINVAL;
  545. goto done;
  546. }
  547. twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
  548. twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
  549. if (copy_to_user((void __user *)arg,
  550. (unsigned char *)twoints,
  551. sizeof(twoints))) {
  552. retval = -EFAULT;
  553. goto done;
  554. }
  555. dev_dbg(&usblp->intf->dev,
  556. "usblp%d is VID=0x%4.4X, PID=0x%4.4X\n",
  557. usblp->minor, twoints[0], twoints[1]);
  558. break;
  559. case IOCNR_SOFT_RESET:
  560. if (_IOC_DIR(cmd) != _IOC_NONE) {
  561. retval = -EINVAL;
  562. goto done;
  563. }
  564. retval = usblp_reset(usblp);
  565. break;
  566. default:
  567. retval = -ENOTTY;
  568. }
  569. else /* old-style ioctl value */
  570. switch (cmd) {
  571. case LPGETSTATUS:
  572. retval = usblp_read_status(usblp, usblp->statusbuf);
  573. if (retval) {
  574. printk_ratelimited(KERN_ERR "usblp%d:"
  575. "failed reading printer status (%d)\n",
  576. usblp->minor, retval);
  577. retval = -EIO;
  578. goto done;
  579. }
  580. status = *usblp->statusbuf;
  581. if (copy_to_user((void __user *)arg, &status, sizeof(int)))
  582. retval = -EFAULT;
  583. break;
  584. case LPABORT:
  585. if (arg)
  586. usblp->flags |= LP_ABORT;
  587. else
  588. usblp->flags &= ~LP_ABORT;
  589. break;
  590. default:
  591. retval = -ENOTTY;
  592. }
  593. done:
  594. mutex_unlock(&usblp->mut);
  595. return retval;
  596. }
  597. static struct urb *usblp_new_writeurb(struct usblp *usblp, int transfer_length)
  598. {
  599. struct urb *urb;
  600. char *writebuf;
  601. writebuf = kmalloc(transfer_length, GFP_KERNEL);
  602. if (writebuf == NULL)
  603. return NULL;
  604. urb = usb_alloc_urb(0, GFP_KERNEL);
  605. if (urb == NULL) {
  606. kfree(writebuf);
  607. return NULL;
  608. }
  609. usb_fill_bulk_urb(urb, usblp->dev,
  610. usb_sndbulkpipe(usblp->dev,
  611. usblp->protocol[usblp->current_protocol].epwrite->bEndpointAddress),
  612. writebuf, transfer_length, usblp_bulk_write, usblp);
  613. urb->transfer_flags |= URB_FREE_BUFFER;
  614. return urb;
  615. }
  616. static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  617. {
  618. struct usblp *usblp = file->private_data;
  619. struct urb *writeurb;
  620. int rv;
  621. int transfer_length;
  622. ssize_t writecount = 0;
  623. if (mutex_lock_interruptible(&usblp->wmut)) {
  624. rv = -EINTR;
  625. goto raise_biglock;
  626. }
  627. if ((rv = usblp_wwait(usblp, !!(file->f_flags & O_NONBLOCK))) < 0)
  628. goto raise_wait;
  629. while (writecount < count) {
  630. /*
  631. * Step 1: Submit next block.
  632. */
  633. if ((transfer_length = count - writecount) > USBLP_BUF_SIZE)
  634. transfer_length = USBLP_BUF_SIZE;
  635. rv = -ENOMEM;
  636. writeurb = usblp_new_writeurb(usblp, transfer_length);
  637. if (writeurb == NULL)
  638. goto raise_urb;
  639. usb_anchor_urb(writeurb, &usblp->urbs);
  640. if (copy_from_user(writeurb->transfer_buffer,
  641. buffer + writecount, transfer_length)) {
  642. rv = -EFAULT;
  643. goto raise_badaddr;
  644. }
  645. spin_lock_irq(&usblp->lock);
  646. usblp->wcomplete = 0;
  647. spin_unlock_irq(&usblp->lock);
  648. if ((rv = usb_submit_urb(writeurb, GFP_KERNEL)) < 0) {
  649. usblp->wstatus = 0;
  650. spin_lock_irq(&usblp->lock);
  651. usblp->no_paper = 0;
  652. usblp->wcomplete = 1;
  653. wake_up(&usblp->wwait);
  654. spin_unlock_irq(&usblp->lock);
  655. if (rv != -ENOMEM)
  656. rv = -EIO;
  657. goto raise_submit;
  658. }
  659. /*
  660. * Step 2: Wait for transfer to end, collect results.
  661. */
  662. rv = usblp_wwait(usblp, !!(file->f_flags&O_NONBLOCK));
  663. if (rv < 0) {
  664. if (rv == -EAGAIN) {
  665. /* Presume that it's going to complete well. */
  666. writecount += transfer_length;
  667. }
  668. if (rv == -ENOSPC) {
  669. spin_lock_irq(&usblp->lock);
  670. usblp->no_paper = 1; /* Mark for poll(2) */
  671. spin_unlock_irq(&usblp->lock);
  672. writecount += transfer_length;
  673. }
  674. /* Leave URB dangling, to be cleaned on close. */
  675. goto collect_error;
  676. }
  677. if (usblp->wstatus < 0) {
  678. rv = -EIO;
  679. goto collect_error;
  680. }
  681. /*
  682. * This is critical: it must be our URB, not other writer's.
  683. * The wmut exists mainly to cover us here.
  684. */
  685. writecount += usblp->wstatus;
  686. }
  687. mutex_unlock(&usblp->wmut);
  688. return writecount;
  689. raise_submit:
  690. raise_badaddr:
  691. usb_unanchor_urb(writeurb);
  692. usb_free_urb(writeurb);
  693. raise_urb:
  694. raise_wait:
  695. collect_error: /* Out of raise sequence */
  696. mutex_unlock(&usblp->wmut);
  697. raise_biglock:
  698. return writecount ? writecount : rv;
  699. }
  700. /*
  701. * Notice that we fail to restart in a few cases: on EFAULT, on restart
  702. * error, etc. This is the historical behaviour. In all such cases we return
  703. * EIO, and applications loop in order to get the new read going.
  704. */
  705. static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, loff_t *ppos)
  706. {
  707. struct usblp *usblp = file->private_data;
  708. ssize_t count;
  709. ssize_t avail;
  710. int rv;
  711. if (!usblp->bidir)
  712. return -EINVAL;
  713. rv = usblp_rwait_and_lock(usblp, !!(file->f_flags & O_NONBLOCK));
  714. if (rv < 0)
  715. return rv;
  716. if ((avail = usblp->rstatus) < 0) {
  717. printk(KERN_ERR "usblp%d: error %d reading from printer\n",
  718. usblp->minor, (int)avail);
  719. usblp_submit_read(usblp);
  720. count = -EIO;
  721. goto done;
  722. }
  723. count = len < avail - usblp->readcount ? len : avail - usblp->readcount;
  724. if (count != 0 &&
  725. copy_to_user(buffer, usblp->readbuf + usblp->readcount, count)) {
  726. count = -EFAULT;
  727. goto done;
  728. }
  729. if ((usblp->readcount += count) == avail) {
  730. if (usblp_submit_read(usblp) < 0) {
  731. /* We don't want to leak USB return codes into errno. */
  732. if (count == 0)
  733. count = -EIO;
  734. goto done;
  735. }
  736. }
  737. done:
  738. mutex_unlock(&usblp->mut);
  739. return count;
  740. }
  741. /*
  742. * Wait for the write path to come idle.
  743. * This is called under the ->wmut, so the idle path stays idle.
  744. *
  745. * Our write path has a peculiar property: it does not buffer like a tty,
  746. * but waits for the write to succeed. This allows our ->release to bug out
  747. * without waiting for writes to drain. But it obviously does not work
  748. * when O_NONBLOCK is set. So, applications setting O_NONBLOCK must use
  749. * select(2) or poll(2) to wait for the buffer to drain before closing.
  750. * Alternatively, set blocking mode with fcntl and issue a zero-size write.
  751. */
  752. static int usblp_wwait(struct usblp *usblp, int nonblock)
  753. {
  754. DECLARE_WAITQUEUE(waita, current);
  755. int rc;
  756. int err = 0;
  757. add_wait_queue(&usblp->wwait, &waita);
  758. for (;;) {
  759. if (mutex_lock_interruptible(&usblp->mut)) {
  760. rc = -EINTR;
  761. break;
  762. }
  763. set_current_state(TASK_INTERRUPTIBLE);
  764. rc = usblp_wtest(usblp, nonblock);
  765. mutex_unlock(&usblp->mut);
  766. if (rc <= 0)
  767. break;
  768. if (schedule_timeout(msecs_to_jiffies(1500)) == 0) {
  769. if (usblp->flags & LP_ABORT) {
  770. err = usblp_check_status(usblp, err);
  771. if (err == 1) { /* Paper out */
  772. rc = -ENOSPC;
  773. break;
  774. }
  775. } else {
  776. /* Prod the printer, Gentoo#251237. */
  777. mutex_lock(&usblp->mut);
  778. usblp_read_status(usblp, usblp->statusbuf);
  779. mutex_unlock(&usblp->mut);
  780. }
  781. }
  782. }
  783. set_current_state(TASK_RUNNING);
  784. remove_wait_queue(&usblp->wwait, &waita);
  785. return rc;
  786. }
  787. static int usblp_wtest(struct usblp *usblp, int nonblock)
  788. {
  789. unsigned long flags;
  790. if (!usblp->present)
  791. return -ENODEV;
  792. if (signal_pending(current))
  793. return -EINTR;
  794. spin_lock_irqsave(&usblp->lock, flags);
  795. if (usblp->wcomplete) {
  796. spin_unlock_irqrestore(&usblp->lock, flags);
  797. return 0;
  798. }
  799. spin_unlock_irqrestore(&usblp->lock, flags);
  800. if (nonblock)
  801. return -EAGAIN;
  802. return 1;
  803. }
  804. /*
  805. * Wait for read bytes to become available. This probably should have been
  806. * called usblp_r_lock_and_wait(), because we lock first. But it's a traditional
  807. * name for functions which lock and return.
  808. *
  809. * We do not use wait_event_interruptible because it makes locking iffy.
  810. */
  811. static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock)
  812. {
  813. DECLARE_WAITQUEUE(waita, current);
  814. int rc;
  815. add_wait_queue(&usblp->rwait, &waita);
  816. for (;;) {
  817. if (mutex_lock_interruptible(&usblp->mut)) {
  818. rc = -EINTR;
  819. break;
  820. }
  821. set_current_state(TASK_INTERRUPTIBLE);
  822. if ((rc = usblp_rtest(usblp, nonblock)) < 0) {
  823. mutex_unlock(&usblp->mut);
  824. break;
  825. }
  826. if (rc == 0) /* Keep it locked */
  827. break;
  828. mutex_unlock(&usblp->mut);
  829. schedule();
  830. }
  831. set_current_state(TASK_RUNNING);
  832. remove_wait_queue(&usblp->rwait, &waita);
  833. return rc;
  834. }
  835. static int usblp_rtest(struct usblp *usblp, int nonblock)
  836. {
  837. unsigned long flags;
  838. if (!usblp->present)
  839. return -ENODEV;
  840. if (signal_pending(current))
  841. return -EINTR;
  842. spin_lock_irqsave(&usblp->lock, flags);
  843. if (usblp->rcomplete) {
  844. spin_unlock_irqrestore(&usblp->lock, flags);
  845. return 0;
  846. }
  847. spin_unlock_irqrestore(&usblp->lock, flags);
  848. if (nonblock)
  849. return -EAGAIN;
  850. return 1;
  851. }
  852. /*
  853. * Please check ->bidir and other such things outside for now.
  854. */
  855. static int usblp_submit_read(struct usblp *usblp)
  856. {
  857. struct urb *urb;
  858. unsigned long flags;
  859. int rc;
  860. rc = -ENOMEM;
  861. urb = usb_alloc_urb(0, GFP_KERNEL);
  862. if (urb == NULL)
  863. goto raise_urb;
  864. usb_fill_bulk_urb(urb, usblp->dev,
  865. usb_rcvbulkpipe(usblp->dev,
  866. usblp->protocol[usblp->current_protocol].epread->bEndpointAddress),
  867. usblp->readbuf, USBLP_BUF_SIZE_IN,
  868. usblp_bulk_read, usblp);
  869. usb_anchor_urb(urb, &usblp->urbs);
  870. spin_lock_irqsave(&usblp->lock, flags);
  871. usblp->readcount = 0; /* XXX Why here? */
  872. usblp->rcomplete = 0;
  873. spin_unlock_irqrestore(&usblp->lock, flags);
  874. if ((rc = usb_submit_urb(urb, GFP_KERNEL)) < 0) {
  875. dev_dbg(&usblp->intf->dev, "error submitting urb (%d)\n", rc);
  876. spin_lock_irqsave(&usblp->lock, flags);
  877. usblp->rstatus = rc;
  878. usblp->rcomplete = 1;
  879. spin_unlock_irqrestore(&usblp->lock, flags);
  880. goto raise_submit;
  881. }
  882. return 0;
  883. raise_submit:
  884. usb_unanchor_urb(urb);
  885. usb_free_urb(urb);
  886. raise_urb:
  887. return rc;
  888. }
  889. /*
  890. * Checks for printers that have quirks, such as requiring unidirectional
  891. * communication but reporting bidirectional; currently some HP printers
  892. * have this flaw (HP 810, 880, 895, etc.), or needing an init string
  893. * sent at each open (like some Epsons).
  894. * Returns 1 if found, 0 if not found.
  895. *
  896. * HP recommended that we use the bidirectional interface but
  897. * don't attempt any bulk IN transfers from the IN endpoint.
  898. * Here's some more detail on the problem:
  899. * The problem is not that it isn't bidirectional though. The problem
  900. * is that if you request a device ID, or status information, while
  901. * the buffers are full, the return data will end up in the print data
  902. * buffer. For example if you make sure you never request the device ID
  903. * while you are sending print data, and you don't try to query the
  904. * printer status every couple of milliseconds, you will probably be OK.
  905. */
  906. static unsigned int usblp_quirks(__u16 vendor, __u16 product)
  907. {
  908. int i;
  909. for (i = 0; quirk_printers[i].vendorId; i++) {
  910. if (vendor == quirk_printers[i].vendorId &&
  911. product == quirk_printers[i].productId)
  912. return quirk_printers[i].quirks;
  913. }
  914. return 0;
  915. }
  916. static const struct file_operations usblp_fops = {
  917. .owner = THIS_MODULE,
  918. .read = usblp_read,
  919. .write = usblp_write,
  920. .poll = usblp_poll,
  921. .unlocked_ioctl = usblp_ioctl,
  922. .compat_ioctl = usblp_ioctl,
  923. .open = usblp_open,
  924. .release = usblp_release,
  925. .llseek = noop_llseek,
  926. };
  927. static char *usblp_devnode(struct device *dev, umode_t *mode)
  928. {
  929. return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
  930. }
  931. static struct usb_class_driver usblp_class = {
  932. .name = "lp%d",
  933. .devnode = usblp_devnode,
  934. .fops = &usblp_fops,
  935. .minor_base = USBLP_MINOR_BASE,
  936. };
  937. static ssize_t ieee1284_id_show(struct device *dev, struct device_attribute *attr, char *buf)
  938. {
  939. struct usb_interface *intf = to_usb_interface(dev);
  940. struct usblp *usblp = usb_get_intfdata(intf);
  941. if (usblp->device_id_string[0] == 0 &&
  942. usblp->device_id_string[1] == 0)
  943. return 0;
  944. return sprintf(buf, "%s", usblp->device_id_string+2);
  945. }
  946. static DEVICE_ATTR_RO(ieee1284_id);
  947. static int usblp_probe(struct usb_interface *intf,
  948. const struct usb_device_id *id)
  949. {
  950. struct usb_device *dev = interface_to_usbdev(intf);
  951. struct usblp *usblp;
  952. int protocol;
  953. int retval;
  954. /* Malloc and start initializing usblp structure so we can use it
  955. * directly. */
  956. usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL);
  957. if (!usblp) {
  958. retval = -ENOMEM;
  959. goto abort_ret;
  960. }
  961. usblp->dev = dev;
  962. mutex_init(&usblp->wmut);
  963. mutex_init(&usblp->mut);
  964. spin_lock_init(&usblp->lock);
  965. init_waitqueue_head(&usblp->rwait);
  966. init_waitqueue_head(&usblp->wwait);
  967. init_usb_anchor(&usblp->urbs);
  968. usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  969. usblp->intf = intf;
  970. /* Malloc device ID string buffer to the largest expected length,
  971. * since we can re-query it on an ioctl and a dynamic string
  972. * could change in length. */
  973. if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
  974. retval = -ENOMEM;
  975. goto abort;
  976. }
  977. /*
  978. * Allocate read buffer. We somewhat wastefully
  979. * malloc both regardless of bidirectionality, because the
  980. * alternate setting can be changed later via an ioctl.
  981. */
  982. if (!(usblp->readbuf = kmalloc(USBLP_BUF_SIZE_IN, GFP_KERNEL))) {
  983. retval = -ENOMEM;
  984. goto abort;
  985. }
  986. /* Allocate buffer for printer status */
  987. usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
  988. if (!usblp->statusbuf) {
  989. retval = -ENOMEM;
  990. goto abort;
  991. }
  992. /* Lookup quirks for this printer. */
  993. usblp->quirks = usblp_quirks(
  994. le16_to_cpu(dev->descriptor.idVendor),
  995. le16_to_cpu(dev->descriptor.idProduct));
  996. /* Analyze and pick initial alternate settings and endpoints. */
  997. protocol = usblp_select_alts(usblp);
  998. if (protocol < 0) {
  999. dev_dbg(&intf->dev,
  1000. "incompatible printer-class device 0x%4.4X/0x%4.4X\n",
  1001. le16_to_cpu(dev->descriptor.idVendor),
  1002. le16_to_cpu(dev->descriptor.idProduct));
  1003. retval = -ENODEV;
  1004. goto abort;
  1005. }
  1006. /* Setup the selected alternate setting and endpoints. */
  1007. if (usblp_set_protocol(usblp, protocol) < 0) {
  1008. retval = -ENODEV; /* ->probe isn't ->ioctl */
  1009. goto abort;
  1010. }
  1011. /* Retrieve and store the device ID string. */
  1012. usblp_cache_device_id_string(usblp);
  1013. retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id);
  1014. if (retval)
  1015. goto abort_intfdata;
  1016. #ifdef DEBUG
  1017. usblp_check_status(usblp, 0);
  1018. #endif
  1019. usb_set_intfdata(intf, usblp);
  1020. usblp->present = 1;
  1021. retval = usb_register_dev(intf, &usblp_class);
  1022. if (retval) {
  1023. dev_err(&intf->dev,
  1024. "usblp: Not able to get a minor (base %u, slice default): %d\n",
  1025. USBLP_MINOR_BASE, retval);
  1026. goto abort_intfdata;
  1027. }
  1028. usblp->minor = intf->minor;
  1029. dev_info(&intf->dev,
  1030. "usblp%d: USB %sdirectional printer dev %d if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X\n",
  1031. usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
  1032. usblp->ifnum,
  1033. usblp->protocol[usblp->current_protocol].alt_setting,
  1034. usblp->current_protocol,
  1035. le16_to_cpu(usblp->dev->descriptor.idVendor),
  1036. le16_to_cpu(usblp->dev->descriptor.idProduct));
  1037. return 0;
  1038. abort_intfdata:
  1039. usb_set_intfdata(intf, NULL);
  1040. device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
  1041. abort:
  1042. kfree(usblp->readbuf);
  1043. kfree(usblp->statusbuf);
  1044. kfree(usblp->device_id_string);
  1045. kfree(usblp);
  1046. abort_ret:
  1047. return retval;
  1048. }
  1049. /*
  1050. * We are a "new" style driver with usb_device_id table,
  1051. * but our requirements are too intricate for simple match to handle.
  1052. *
  1053. * The "proto_bias" option may be used to specify the preferred protocol
  1054. * for all USB printers (1=USB_CLASS_PRINTER/1/1, 2=USB_CLASS_PRINTER/1/2,
  1055. * 3=USB_CLASS_PRINTER/1/3). If the device supports the preferred protocol,
  1056. * then we bind to it.
  1057. *
  1058. * The best interface for us is USB_CLASS_PRINTER/1/2, because it
  1059. * is compatible with a stream of characters. If we find it, we bind to it.
  1060. *
  1061. * Note that the people from hpoj.sourceforge.net need to be able to
  1062. * bind to USB_CLASS_PRINTER/1/3 (MLC/1284.4), so we provide them ioctls
  1063. * for this purpose.
  1064. *
  1065. * Failing USB_CLASS_PRINTER/1/2, we look for USB_CLASS_PRINTER/1/3,
  1066. * even though it's probably not stream-compatible, because this matches
  1067. * the behaviour of the old code.
  1068. *
  1069. * If nothing else, we bind to USB_CLASS_PRINTER/1/1
  1070. * - the unidirectional interface.
  1071. */
  1072. static int usblp_select_alts(struct usblp *usblp)
  1073. {
  1074. struct usb_interface *if_alt;
  1075. struct usb_host_interface *ifd;
  1076. struct usb_endpoint_descriptor *epwrite, *epread;
  1077. int p, i;
  1078. int res;
  1079. if_alt = usblp->intf;
  1080. for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
  1081. usblp->protocol[p].alt_setting = -1;
  1082. /* Find out what we have. */
  1083. for (i = 0; i < if_alt->num_altsetting; i++) {
  1084. ifd = &if_alt->altsetting[i];
  1085. if (ifd->desc.bInterfaceClass != USB_CLASS_PRINTER ||
  1086. ifd->desc.bInterfaceSubClass != 1)
  1087. if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS))
  1088. continue;
  1089. if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
  1090. ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
  1091. continue;
  1092. /* Look for the expected bulk endpoints. */
  1093. if (ifd->desc.bInterfaceProtocol > 1) {
  1094. res = usb_find_common_endpoints(ifd,
  1095. &epread, &epwrite, NULL, NULL);
  1096. } else {
  1097. epread = NULL;
  1098. res = usb_find_bulk_out_endpoint(ifd, &epwrite);
  1099. }
  1100. /* Ignore buggy hardware without the right endpoints. */
  1101. if (res)
  1102. continue;
  1103. /* Turn off reads for buggy bidirectional printers. */
  1104. if (usblp->quirks & USBLP_QUIRK_BIDIR) {
  1105. printk(KERN_INFO "usblp%d: Disabling reads from "
  1106. "problematic bidirectional printer\n",
  1107. usblp->minor);
  1108. epread = NULL;
  1109. }
  1110. usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
  1111. ifd->desc.bAlternateSetting;
  1112. usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
  1113. usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
  1114. }
  1115. /* If our requested protocol is supported, then use it. */
  1116. if (proto_bias >= USBLP_FIRST_PROTOCOL &&
  1117. proto_bias <= USBLP_LAST_PROTOCOL &&
  1118. usblp->protocol[proto_bias].alt_setting != -1)
  1119. return proto_bias;
  1120. /* Ordering is important here. */
  1121. if (usblp->protocol[2].alt_setting != -1)
  1122. return 2;
  1123. if (usblp->protocol[1].alt_setting != -1)
  1124. return 1;
  1125. if (usblp->protocol[3].alt_setting != -1)
  1126. return 3;
  1127. /* If nothing is available, then don't bind to this device. */
  1128. return -1;
  1129. }
  1130. static int usblp_set_protocol(struct usblp *usblp, int protocol)
  1131. {
  1132. int r, alts;
  1133. if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
  1134. return -EINVAL;
  1135. alts = usblp->protocol[protocol].alt_setting;
  1136. if (alts < 0)
  1137. return -EINVAL;
  1138. r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
  1139. if (r < 0) {
  1140. printk(KERN_ERR "usblp: can't set desired altsetting %d on interface %d\n",
  1141. alts, usblp->ifnum);
  1142. return r;
  1143. }
  1144. usblp->bidir = (usblp->protocol[protocol].epread != NULL);
  1145. usblp->current_protocol = protocol;
  1146. dev_dbg(&usblp->intf->dev, "usblp%d set protocol %d\n",
  1147. usblp->minor, protocol);
  1148. return 0;
  1149. }
  1150. /* Retrieves and caches device ID string.
  1151. * Returns length, including length bytes but not null terminator.
  1152. * On error, returns a negative errno value. */
  1153. static int usblp_cache_device_id_string(struct usblp *usblp)
  1154. {
  1155. int err, length;
  1156. err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
  1157. if (err < 0) {
  1158. dev_dbg(&usblp->intf->dev,
  1159. "usblp%d: error = %d reading IEEE-1284 Device ID string\n",
  1160. usblp->minor, err);
  1161. usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
  1162. return -EIO;
  1163. }
  1164. /* First two bytes are length in big-endian.
  1165. * They count themselves, and we copy them into
  1166. * the user's buffer. */
  1167. length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
  1168. if (length < 2)
  1169. length = 2;
  1170. else if (length >= USBLP_DEVICE_ID_SIZE)
  1171. length = USBLP_DEVICE_ID_SIZE - 1;
  1172. usblp->device_id_string[length] = '\0';
  1173. dev_dbg(&usblp->intf->dev, "usblp%d Device ID string [len=%d]=\"%s\"\n",
  1174. usblp->minor, length, &usblp->device_id_string[2]);
  1175. return length;
  1176. }
  1177. static void usblp_disconnect(struct usb_interface *intf)
  1178. {
  1179. struct usblp *usblp = usb_get_intfdata(intf);
  1180. usb_deregister_dev(intf, &usblp_class);
  1181. if (!usblp || !usblp->dev) {
  1182. dev_err(&intf->dev, "bogus disconnect\n");
  1183. BUG();
  1184. }
  1185. device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
  1186. mutex_lock(&usblp_mutex);
  1187. mutex_lock(&usblp->mut);
  1188. usblp->present = 0;
  1189. wake_up(&usblp->wwait);
  1190. wake_up(&usblp->rwait);
  1191. usb_set_intfdata(intf, NULL);
  1192. usblp_unlink_urbs(usblp);
  1193. mutex_unlock(&usblp->mut);
  1194. if (!usblp->used)
  1195. usblp_cleanup(usblp);
  1196. mutex_unlock(&usblp_mutex);
  1197. }
  1198. static int usblp_suspend(struct usb_interface *intf, pm_message_t message)
  1199. {
  1200. struct usblp *usblp = usb_get_intfdata(intf);
  1201. usblp_unlink_urbs(usblp);
  1202. #if 0 /* XXX Do we want this? What if someone is reading, should we fail? */
  1203. /* not strictly necessary, but just in case */
  1204. wake_up(&usblp->wwait);
  1205. wake_up(&usblp->rwait);
  1206. #endif
  1207. return 0;
  1208. }
  1209. static int usblp_resume(struct usb_interface *intf)
  1210. {
  1211. struct usblp *usblp = usb_get_intfdata(intf);
  1212. int r;
  1213. r = handle_bidir(usblp);
  1214. return r;
  1215. }
  1216. static const struct usb_device_id usblp_ids[] = {
  1217. { USB_DEVICE_INFO(USB_CLASS_PRINTER, 1, 1) },
  1218. { USB_DEVICE_INFO(USB_CLASS_PRINTER, 1, 2) },
  1219. { USB_DEVICE_INFO(USB_CLASS_PRINTER, 1, 3) },
  1220. { USB_INTERFACE_INFO(USB_CLASS_PRINTER, 1, 1) },
  1221. { USB_INTERFACE_INFO(USB_CLASS_PRINTER, 1, 2) },
  1222. { USB_INTERFACE_INFO(USB_CLASS_PRINTER, 1, 3) },
  1223. { USB_DEVICE(0x04b8, 0x0202) }, /* Seiko Epson Receipt Printer M129C */
  1224. { } /* Terminating entry */
  1225. };
  1226. MODULE_DEVICE_TABLE(usb, usblp_ids);
  1227. static struct usb_driver usblp_driver = {
  1228. .name = "usblp",
  1229. .probe = usblp_probe,
  1230. .disconnect = usblp_disconnect,
  1231. .suspend = usblp_suspend,
  1232. .resume = usblp_resume,
  1233. .id_table = usblp_ids,
  1234. .supports_autosuspend = 1,
  1235. };
  1236. module_usb_driver(usblp_driver);
  1237. MODULE_AUTHOR(DRIVER_AUTHOR);
  1238. MODULE_DESCRIPTION(DRIVER_DESC);
  1239. module_param(proto_bias, int, S_IRUGO | S_IWUSR);
  1240. MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
  1241. MODULE_LICENSE("GPL");