usblp.c 40 KB

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