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