usblp.c 39 KB

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