usblp.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. /*
  2. * usblp.c Version 0.13
  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_VERSION "v0.13"
  62. #define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
  63. #define DRIVER_DESC "USB Printer Device Class driver"
  64. #define USBLP_BUF_SIZE 8192
  65. #define USBLP_DEVICE_ID_SIZE 1024
  66. /* ioctls: */
  67. #define LPGETSTATUS 0x060b /* same as in drivers/char/lp.c */
  68. #define IOCNR_GET_DEVICE_ID 1
  69. #define IOCNR_GET_PROTOCOLS 2
  70. #define IOCNR_SET_PROTOCOL 3
  71. #define IOCNR_HP_SET_CHANNEL 4
  72. #define IOCNR_GET_BUS_ADDRESS 5
  73. #define IOCNR_GET_VID_PID 6
  74. #define IOCNR_SOFT_RESET 7
  75. /* Get device_id string: */
  76. #define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
  77. /* The following ioctls were added for http://hpoj.sourceforge.net: */
  78. /* Get two-int array:
  79. * [0]=current protocol (1=7/1/1, 2=7/1/2, 3=7/1/3),
  80. * [1]=supported protocol mask (mask&(1<<n)!=0 means 7/1/n supported): */
  81. #define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
  82. /* Set protocol (arg: 1=7/1/1, 2=7/1/2, 3=7/1/3): */
  83. #define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
  84. /* Set channel number (HP Vendor-specific command): */
  85. #define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
  86. /* Get two-int array: [0]=bus number, [1]=device address: */
  87. #define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
  88. /* Get two-int array: [0]=vendor ID, [1]=product ID: */
  89. #define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
  90. /* Perform class specific soft reset */
  91. #define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0);
  92. /*
  93. * A DEVICE_ID string may include the printer's serial number.
  94. * It should end with a semi-colon (';').
  95. * An example from an HP 970C DeskJet printer is (this is one long string,
  96. * with the serial number changed):
  97. 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: ;
  98. */
  99. /*
  100. * USB Printer Requests
  101. */
  102. #define USBLP_REQ_GET_ID 0x00
  103. #define USBLP_REQ_GET_STATUS 0x01
  104. #define USBLP_REQ_RESET 0x02
  105. #define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST 0x00 /* HP Vendor-specific */
  106. #define USBLP_MINORS 16
  107. #define USBLP_MINOR_BASE 0
  108. #define USBLP_WRITE_TIMEOUT (5000) /* 5 seconds */
  109. #define USBLP_FIRST_PROTOCOL 1
  110. #define USBLP_LAST_PROTOCOL 3
  111. #define USBLP_MAX_PROTOCOLS (USBLP_LAST_PROTOCOL+1)
  112. /*
  113. * some arbitrary status buffer size;
  114. * need a status buffer that is allocated via kmalloc(), not on stack
  115. */
  116. #define STATUS_BUF_SIZE 8
  117. struct usblp {
  118. struct usb_device *dev; /* USB device */
  119. struct mutex mut; /* locks this struct, especially "dev" */
  120. char *writebuf; /* write transfer_buffer */
  121. char *readbuf; /* read transfer_buffer */
  122. char *statusbuf; /* status transfer_buffer */
  123. struct urb *readurb, *writeurb; /* The urbs */
  124. wait_queue_head_t wait; /* Zzzzz ... */
  125. int readcount; /* Counter for reads */
  126. int ifnum; /* Interface number */
  127. struct usb_interface *intf; /* The interface */
  128. /* Alternate-setting numbers and endpoints for each protocol
  129. * (7/1/{index=1,2,3}) that the device supports: */
  130. struct {
  131. int alt_setting;
  132. struct usb_endpoint_descriptor *epwrite;
  133. struct usb_endpoint_descriptor *epread;
  134. } protocol[USBLP_MAX_PROTOCOLS];
  135. int current_protocol;
  136. int minor; /* minor number of device */
  137. int wcomplete; /* writing is completed */
  138. int rcomplete; /* reading is completed */
  139. unsigned int quirks; /* quirks flags */
  140. unsigned char used; /* True if open */
  141. unsigned char present; /* True if not disconnected */
  142. unsigned char bidir; /* interface is bidirectional */
  143. unsigned char sleeping; /* interface is suspended */
  144. unsigned char *device_id_string; /* IEEE 1284 DEVICE ID string (ptr) */
  145. /* first 2 bytes are (big-endian) length */
  146. };
  147. #ifdef DEBUG
  148. static void usblp_dump(struct usblp *usblp) {
  149. int p;
  150. dbg("usblp=0x%p", usblp);
  151. dbg("dev=0x%p", usblp->dev);
  152. dbg("present=%d", usblp->present);
  153. dbg("readbuf=0x%p", usblp->readbuf);
  154. dbg("writebuf=0x%p", usblp->writebuf);
  155. dbg("readurb=0x%p", usblp->readurb);
  156. dbg("writeurb=0x%p", usblp->writeurb);
  157. dbg("readcount=%d", usblp->readcount);
  158. dbg("ifnum=%d", usblp->ifnum);
  159. for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
  160. dbg("protocol[%d].alt_setting=%d", p, usblp->protocol[p].alt_setting);
  161. dbg("protocol[%d].epwrite=%p", p, usblp->protocol[p].epwrite);
  162. dbg("protocol[%d].epread=%p", p, usblp->protocol[p].epread);
  163. }
  164. dbg("current_protocol=%d", usblp->current_protocol);
  165. dbg("minor=%d", usblp->minor);
  166. dbg("wcomplete=%d", usblp->wcomplete);
  167. dbg("rcomplete=%d", usblp->rcomplete);
  168. dbg("quirks=%d", usblp->quirks);
  169. dbg("used=%d", usblp->used);
  170. dbg("bidir=%d", usblp->bidir);
  171. dbg("sleeping=%d", usblp->sleeping);
  172. dbg("device_id_string=\"%s\"",
  173. usblp->device_id_string ?
  174. usblp->device_id_string + 2 :
  175. (unsigned char *)"(null)");
  176. }
  177. #endif
  178. /* Quirks: various printer quirks are handled by this table & its flags. */
  179. struct quirk_printer_struct {
  180. __u16 vendorId;
  181. __u16 productId;
  182. unsigned int quirks;
  183. };
  184. #define USBLP_QUIRK_BIDIR 0x1 /* reports bidir but requires unidirectional mode (no INs/reads) */
  185. #define USBLP_QUIRK_USB_INIT 0x2 /* needs vendor USB init string */
  186. #define USBLP_QUIRK_BAD_CLASS 0x4 /* descriptor uses vendor-specific Class or SubClass */
  187. static const struct quirk_printer_struct quirk_printers[] = {
  188. { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
  189. { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
  190. { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
  191. { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
  192. { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
  193. { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
  194. { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */
  195. { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */
  196. { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
  197. { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
  198. { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
  199. { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
  200. { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
  201. { 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
  202. { 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt Printer M129C */
  203. { 0, 0 }
  204. };
  205. static int usblp_select_alts(struct usblp *usblp);
  206. static int usblp_set_protocol(struct usblp *usblp, int protocol);
  207. static int usblp_cache_device_id_string(struct usblp *usblp);
  208. /* forward reference to make our lives easier */
  209. static struct usb_driver usblp_driver;
  210. static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */
  211. /*
  212. * Functions for usblp control messages.
  213. */
  214. static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
  215. {
  216. int retval;
  217. int index = usblp->ifnum;
  218. /* High byte has the interface index.
  219. Low byte has the alternate setting.
  220. */
  221. if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS)) {
  222. index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
  223. }
  224. retval = usb_control_msg(usblp->dev,
  225. dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
  226. request, type | dir | recip, value, index, buf, len, USBLP_WRITE_TIMEOUT);
  227. dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d",
  228. request, !!dir, recip, value, index, len, retval);
  229. return retval < 0 ? retval : 0;
  230. }
  231. #define usblp_read_status(usblp, status)\
  232. usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
  233. #define usblp_get_id(usblp, config, id, maxlen)\
  234. usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
  235. #define usblp_reset(usblp)\
  236. usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
  237. #define usblp_hp_channel_change_request(usblp, channel, buffer) \
  238. usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
  239. /*
  240. * See the description for usblp_select_alts() below for the usage
  241. * explanation. Look into your /proc/bus/usb/devices and dmesg in
  242. * case of any trouble.
  243. */
  244. static int proto_bias = -1;
  245. /*
  246. * URB callback.
  247. */
  248. static void usblp_bulk_read(struct urb *urb)
  249. {
  250. struct usblp *usblp = urb->context;
  251. if (unlikely(!usblp || !usblp->dev || !usblp->used))
  252. return;
  253. if (unlikely(!usblp->present))
  254. goto unplug;
  255. if (unlikely(urb->status))
  256. warn("usblp%d: nonzero read/write bulk status received: %d",
  257. usblp->minor, urb->status);
  258. usblp->rcomplete = 1;
  259. unplug:
  260. wake_up_interruptible(&usblp->wait);
  261. }
  262. static void usblp_bulk_write(struct urb *urb)
  263. {
  264. struct usblp *usblp = urb->context;
  265. if (unlikely(!usblp || !usblp->dev || !usblp->used))
  266. return;
  267. if (unlikely(!usblp->present))
  268. goto unplug;
  269. if (unlikely(urb->status))
  270. warn("usblp%d: nonzero read/write bulk status received: %d",
  271. usblp->minor, urb->status);
  272. usblp->wcomplete = 1;
  273. unplug:
  274. wake_up_interruptible(&usblp->wait);
  275. }
  276. /*
  277. * Get and print printer errors.
  278. */
  279. static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
  280. static int usblp_check_status(struct usblp *usblp, int err)
  281. {
  282. unsigned char status, newerr = 0;
  283. int error;
  284. error = usblp_read_status (usblp, usblp->statusbuf);
  285. if (error < 0) {
  286. if (printk_ratelimit())
  287. err("usblp%d: error %d reading printer status",
  288. usblp->minor, error);
  289. return 0;
  290. }
  291. status = *usblp->statusbuf;
  292. if (~status & LP_PERRORP)
  293. newerr = 3;
  294. if (status & LP_POUTPA)
  295. newerr = 1;
  296. if (~status & LP_PSELECD)
  297. newerr = 2;
  298. if (newerr != err)
  299. info("usblp%d: %s", usblp->minor, usblp_messages[newerr]);
  300. return newerr;
  301. }
  302. static int handle_bidir (struct usblp *usblp)
  303. {
  304. if (usblp->bidir && usblp->used && !usblp->sleeping) {
  305. usblp->readcount = 0;
  306. usblp->readurb->dev = usblp->dev;
  307. if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0) {
  308. usblp->used = 0;
  309. return -EIO;
  310. }
  311. }
  312. return 0;
  313. }
  314. /*
  315. * File op functions.
  316. */
  317. static int usblp_open(struct inode *inode, struct file *file)
  318. {
  319. int minor = iminor(inode);
  320. struct usblp *usblp;
  321. struct usb_interface *intf;
  322. int retval;
  323. if (minor < 0)
  324. return -ENODEV;
  325. mutex_lock (&usblp_mutex);
  326. retval = -ENODEV;
  327. intf = usb_find_interface(&usblp_driver, minor);
  328. if (!intf) {
  329. goto out;
  330. }
  331. usblp = usb_get_intfdata (intf);
  332. if (!usblp || !usblp->dev || !usblp->present)
  333. goto out;
  334. retval = -EBUSY;
  335. if (usblp->used)
  336. goto out;
  337. /*
  338. * TODO: need to implement LP_ABORTOPEN + O_NONBLOCK as in drivers/char/lp.c ???
  339. * This is #if 0-ed because we *don't* want to fail an open
  340. * just because the printer is off-line.
  341. */
  342. #if 0
  343. if ((retval = usblp_check_status(usblp, 0))) {
  344. retval = retval > 1 ? -EIO : -ENOSPC;
  345. goto out;
  346. }
  347. #else
  348. retval = 0;
  349. #endif
  350. retval = usb_autopm_get_interface(intf);
  351. if (retval < 0)
  352. goto out;
  353. usblp->used = 1;
  354. file->private_data = usblp;
  355. usblp->writeurb->transfer_buffer_length = 0;
  356. usblp->wcomplete = 1; /* we begin writeable */
  357. usblp->rcomplete = 0;
  358. usblp->writeurb->status = 0;
  359. usblp->readurb->status = 0;
  360. if (handle_bidir(usblp) < 0) {
  361. file->private_data = NULL;
  362. retval = -EIO;
  363. }
  364. out:
  365. mutex_unlock (&usblp_mutex);
  366. return retval;
  367. }
  368. static void usblp_cleanup (struct usblp *usblp)
  369. {
  370. info("usblp%d: removed", usblp->minor);
  371. kfree (usblp->device_id_string);
  372. kfree (usblp->statusbuf);
  373. usb_free_urb(usblp->writeurb);
  374. usb_free_urb(usblp->readurb);
  375. kfree (usblp);
  376. }
  377. static void usblp_unlink_urbs(struct usblp *usblp)
  378. {
  379. usb_kill_urb(usblp->writeurb);
  380. if (usblp->bidir)
  381. usb_kill_urb(usblp->readurb);
  382. }
  383. static int usblp_release(struct inode *inode, struct file *file)
  384. {
  385. struct usblp *usblp = file->private_data;
  386. mutex_lock (&usblp_mutex);
  387. usblp->used = 0;
  388. if (usblp->present) {
  389. usblp_unlink_urbs(usblp);
  390. usb_autopm_put_interface(usblp->intf);
  391. } else /* finish cleanup from disconnect */
  392. usblp_cleanup (usblp);
  393. mutex_unlock (&usblp_mutex);
  394. return 0;
  395. }
  396. /* No kernel lock - fine */
  397. static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
  398. {
  399. struct usblp *usblp = file->private_data;
  400. poll_wait(file, &usblp->wait, wait);
  401. return ((!usblp->bidir || !usblp->rcomplete) ? 0 : POLLIN | POLLRDNORM)
  402. | (!usblp->wcomplete ? 0 : POLLOUT | POLLWRNORM);
  403. }
  404. static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  405. {
  406. struct usblp *usblp = file->private_data;
  407. int length, err, i;
  408. unsigned char newChannel;
  409. int status;
  410. int twoints[2];
  411. int retval = 0;
  412. mutex_lock (&usblp->mut);
  413. if (!usblp->present) {
  414. retval = -ENODEV;
  415. goto done;
  416. }
  417. if (usblp->sleeping) {
  418. retval = -ENODEV;
  419. goto done;
  420. }
  421. dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd),
  422. _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd) );
  423. if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */
  424. switch (_IOC_NR(cmd)) {
  425. case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
  426. if (_IOC_DIR(cmd) != _IOC_READ) {
  427. retval = -EINVAL;
  428. goto done;
  429. }
  430. length = usblp_cache_device_id_string(usblp);
  431. if (length < 0) {
  432. retval = length;
  433. goto done;
  434. }
  435. if (length > _IOC_SIZE(cmd))
  436. length = _IOC_SIZE(cmd); /* truncate */
  437. if (copy_to_user((void __user *) arg,
  438. usblp->device_id_string,
  439. (unsigned long) length)) {
  440. retval = -EFAULT;
  441. goto done;
  442. }
  443. break;
  444. case IOCNR_GET_PROTOCOLS:
  445. if (_IOC_DIR(cmd) != _IOC_READ ||
  446. _IOC_SIZE(cmd) < sizeof(twoints)) {
  447. retval = -EINVAL;
  448. goto done;
  449. }
  450. twoints[0] = usblp->current_protocol;
  451. twoints[1] = 0;
  452. for (i = USBLP_FIRST_PROTOCOL;
  453. i <= USBLP_LAST_PROTOCOL; i++) {
  454. if (usblp->protocol[i].alt_setting >= 0)
  455. twoints[1] |= (1<<i);
  456. }
  457. if (copy_to_user((void __user *)arg,
  458. (unsigned char *)twoints,
  459. sizeof(twoints))) {
  460. retval = -EFAULT;
  461. goto done;
  462. }
  463. break;
  464. case IOCNR_SET_PROTOCOL:
  465. if (_IOC_DIR(cmd) != _IOC_WRITE) {
  466. retval = -EINVAL;
  467. goto done;
  468. }
  469. #ifdef DEBUG
  470. if (arg == -10) {
  471. usblp_dump(usblp);
  472. break;
  473. }
  474. #endif
  475. usblp_unlink_urbs(usblp);
  476. retval = usblp_set_protocol(usblp, arg);
  477. if (retval < 0) {
  478. usblp_set_protocol(usblp,
  479. usblp->current_protocol);
  480. }
  481. break;
  482. case IOCNR_HP_SET_CHANNEL:
  483. if (_IOC_DIR(cmd) != _IOC_WRITE ||
  484. le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
  485. usblp->quirks & USBLP_QUIRK_BIDIR) {
  486. retval = -EINVAL;
  487. goto done;
  488. }
  489. err = usblp_hp_channel_change_request(usblp,
  490. arg, &newChannel);
  491. if (err < 0) {
  492. err("usblp%d: error = %d setting "
  493. "HP channel",
  494. usblp->minor, err);
  495. retval = -EIO;
  496. goto done;
  497. }
  498. dbg("usblp%d requested/got HP channel %ld/%d",
  499. usblp->minor, arg, newChannel);
  500. break;
  501. case IOCNR_GET_BUS_ADDRESS:
  502. if (_IOC_DIR(cmd) != _IOC_READ ||
  503. _IOC_SIZE(cmd) < sizeof(twoints)) {
  504. retval = -EINVAL;
  505. goto done;
  506. }
  507. twoints[0] = usblp->dev->bus->busnum;
  508. twoints[1] = usblp->dev->devnum;
  509. if (copy_to_user((void __user *)arg,
  510. (unsigned char *)twoints,
  511. sizeof(twoints))) {
  512. retval = -EFAULT;
  513. goto done;
  514. }
  515. dbg("usblp%d is bus=%d, device=%d",
  516. usblp->minor, twoints[0], twoints[1]);
  517. break;
  518. case IOCNR_GET_VID_PID:
  519. if (_IOC_DIR(cmd) != _IOC_READ ||
  520. _IOC_SIZE(cmd) < sizeof(twoints)) {
  521. retval = -EINVAL;
  522. goto done;
  523. }
  524. twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
  525. twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
  526. if (copy_to_user((void __user *)arg,
  527. (unsigned char *)twoints,
  528. sizeof(twoints))) {
  529. retval = -EFAULT;
  530. goto done;
  531. }
  532. dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
  533. usblp->minor, twoints[0], twoints[1]);
  534. break;
  535. case IOCNR_SOFT_RESET:
  536. if (_IOC_DIR(cmd) != _IOC_NONE) {
  537. retval = -EINVAL;
  538. goto done;
  539. }
  540. retval = usblp_reset(usblp);
  541. break;
  542. default:
  543. retval = -ENOTTY;
  544. }
  545. else /* old-style ioctl value */
  546. switch (cmd) {
  547. case LPGETSTATUS:
  548. if (usblp_read_status(usblp, usblp->statusbuf)) {
  549. if (printk_ratelimit())
  550. err("usblp%d: failed reading printer status",
  551. usblp->minor);
  552. retval = -EIO;
  553. goto done;
  554. }
  555. status = *usblp->statusbuf;
  556. if (copy_to_user ((void __user *)arg, &status, sizeof(int)))
  557. retval = -EFAULT;
  558. break;
  559. default:
  560. retval = -ENOTTY;
  561. }
  562. done:
  563. mutex_unlock (&usblp->mut);
  564. return retval;
  565. }
  566. static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  567. {
  568. struct usblp *usblp = file->private_data;
  569. int timeout, intr, rv, err = 0, transfer_length = 0;
  570. size_t writecount = 0;
  571. while (writecount < count) {
  572. if (!usblp->wcomplete) {
  573. barrier();
  574. if (file->f_flags & O_NONBLOCK) {
  575. writecount += transfer_length;
  576. return writecount ? writecount : -EAGAIN;
  577. }
  578. timeout = USBLP_WRITE_TIMEOUT;
  579. rv = wait_event_interruptible_timeout(usblp->wait, usblp->wcomplete || !usblp->present , timeout);
  580. if (rv < 0)
  581. return writecount ? writecount : -EINTR;
  582. }
  583. intr = mutex_lock_interruptible (&usblp->mut);
  584. if (intr)
  585. return writecount ? writecount : -EINTR;
  586. if (!usblp->present) {
  587. mutex_unlock (&usblp->mut);
  588. return -ENODEV;
  589. }
  590. if (usblp->sleeping) {
  591. mutex_unlock (&usblp->mut);
  592. return writecount ? writecount : -ENODEV;
  593. }
  594. if (usblp->writeurb->status != 0) {
  595. if (usblp->quirks & USBLP_QUIRK_BIDIR) {
  596. if (!usblp->wcomplete)
  597. err("usblp%d: error %d writing to printer",
  598. usblp->minor, usblp->writeurb->status);
  599. err = usblp->writeurb->status;
  600. } else
  601. err = usblp_check_status(usblp, err);
  602. mutex_unlock (&usblp->mut);
  603. /* if the fault was due to disconnect, let khubd's
  604. * call to usblp_disconnect() grab usblp->mut ...
  605. */
  606. schedule ();
  607. continue;
  608. }
  609. /* We must increment writecount here, and not at the
  610. * end of the loop. Otherwise, the final loop iteration may
  611. * be skipped, leading to incomplete printer output.
  612. */
  613. writecount += transfer_length;
  614. if (writecount == count) {
  615. mutex_unlock(&usblp->mut);
  616. break;
  617. }
  618. transfer_length=(count - writecount);
  619. if (transfer_length > USBLP_BUF_SIZE)
  620. transfer_length = USBLP_BUF_SIZE;
  621. usblp->writeurb->transfer_buffer_length = transfer_length;
  622. if (copy_from_user(usblp->writeurb->transfer_buffer,
  623. buffer + writecount, transfer_length)) {
  624. mutex_unlock(&usblp->mut);
  625. return writecount ? writecount : -EFAULT;
  626. }
  627. usblp->writeurb->dev = usblp->dev;
  628. usblp->wcomplete = 0;
  629. err = usb_submit_urb(usblp->writeurb, GFP_KERNEL);
  630. if (err) {
  631. usblp->wcomplete = 1;
  632. if (err != -ENOMEM)
  633. count = -EIO;
  634. else
  635. count = writecount ? writecount : -ENOMEM;
  636. mutex_unlock (&usblp->mut);
  637. break;
  638. }
  639. mutex_unlock (&usblp->mut);
  640. }
  641. return count;
  642. }
  643. static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  644. {
  645. struct usblp *usblp = file->private_data;
  646. int rv, intr;
  647. if (!usblp->bidir)
  648. return -EINVAL;
  649. intr = mutex_lock_interruptible (&usblp->mut);
  650. if (intr)
  651. return -EINTR;
  652. if (!usblp->present) {
  653. count = -ENODEV;
  654. goto done;
  655. }
  656. if (!usblp->rcomplete) {
  657. barrier();
  658. if (file->f_flags & O_NONBLOCK) {
  659. count = -EAGAIN;
  660. goto done;
  661. }
  662. mutex_unlock(&usblp->mut);
  663. rv = wait_event_interruptible(usblp->wait, usblp->rcomplete || !usblp->present);
  664. mutex_lock(&usblp->mut);
  665. if (rv < 0) {
  666. count = -EINTR;
  667. goto done;
  668. }
  669. }
  670. if (!usblp->present) {
  671. count = -ENODEV;
  672. goto done;
  673. }
  674. if (usblp->sleeping) {
  675. count = -ENODEV;
  676. goto done;
  677. }
  678. if (usblp->readurb->status) {
  679. err("usblp%d: error %d reading from printer",
  680. usblp->minor, usblp->readurb->status);
  681. usblp->readurb->dev = usblp->dev;
  682. usblp->readcount = 0;
  683. usblp->rcomplete = 0;
  684. if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0)
  685. dbg("error submitting urb");
  686. count = -EIO;
  687. goto done;
  688. }
  689. count = count < usblp->readurb->actual_length - usblp->readcount ?
  690. count : usblp->readurb->actual_length - usblp->readcount;
  691. if (copy_to_user(buffer, usblp->readurb->transfer_buffer + usblp->readcount, count)) {
  692. count = -EFAULT;
  693. goto done;
  694. }
  695. if ((usblp->readcount += count) == usblp->readurb->actual_length) {
  696. usblp->readcount = 0;
  697. usblp->readurb->dev = usblp->dev;
  698. usblp->rcomplete = 0;
  699. if (usb_submit_urb(usblp->readurb, GFP_KERNEL)) {
  700. count = -EIO;
  701. goto done;
  702. }
  703. }
  704. done:
  705. mutex_unlock (&usblp->mut);
  706. return count;
  707. }
  708. /*
  709. * Checks for printers that have quirks, such as requiring unidirectional
  710. * communication but reporting bidirectional; currently some HP printers
  711. * have this flaw (HP 810, 880, 895, etc.), or needing an init string
  712. * sent at each open (like some Epsons).
  713. * Returns 1 if found, 0 if not found.
  714. *
  715. * HP recommended that we use the bidirectional interface but
  716. * don't attempt any bulk IN transfers from the IN endpoint.
  717. * Here's some more detail on the problem:
  718. * The problem is not that it isn't bidirectional though. The problem
  719. * is that if you request a device ID, or status information, while
  720. * the buffers are full, the return data will end up in the print data
  721. * buffer. For example if you make sure you never request the device ID
  722. * while you are sending print data, and you don't try to query the
  723. * printer status every couple of milliseconds, you will probably be OK.
  724. */
  725. static unsigned int usblp_quirks (__u16 vendor, __u16 product)
  726. {
  727. int i;
  728. for (i = 0; quirk_printers[i].vendorId; i++) {
  729. if (vendor == quirk_printers[i].vendorId &&
  730. product == quirk_printers[i].productId)
  731. return quirk_printers[i].quirks;
  732. }
  733. return 0;
  734. }
  735. static const struct file_operations usblp_fops = {
  736. .owner = THIS_MODULE,
  737. .read = usblp_read,
  738. .write = usblp_write,
  739. .poll = usblp_poll,
  740. .unlocked_ioctl = usblp_ioctl,
  741. .compat_ioctl = usblp_ioctl,
  742. .open = usblp_open,
  743. .release = usblp_release,
  744. };
  745. static struct usb_class_driver usblp_class = {
  746. .name = "lp%d",
  747. .fops = &usblp_fops,
  748. .minor_base = USBLP_MINOR_BASE,
  749. };
  750. static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf)
  751. {
  752. struct usb_interface *intf = to_usb_interface(dev);
  753. struct usblp *usblp = usb_get_intfdata (intf);
  754. if (usblp->device_id_string[0] == 0 &&
  755. usblp->device_id_string[1] == 0)
  756. return 0;
  757. return sprintf(buf, "%s", usblp->device_id_string+2);
  758. }
  759. static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL);
  760. static int usblp_probe(struct usb_interface *intf,
  761. const struct usb_device_id *id)
  762. {
  763. struct usb_device *dev = interface_to_usbdev (intf);
  764. struct usblp *usblp = NULL;
  765. int protocol;
  766. int retval;
  767. /* Malloc and start initializing usblp structure so we can use it
  768. * directly. */
  769. if (!(usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL))) {
  770. err("out of memory for usblp");
  771. goto abort;
  772. }
  773. usblp->dev = dev;
  774. mutex_init (&usblp->mut);
  775. init_waitqueue_head(&usblp->wait);
  776. usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  777. usblp->intf = intf;
  778. usblp->writeurb = usb_alloc_urb(0, GFP_KERNEL);
  779. if (!usblp->writeurb) {
  780. err("out of memory");
  781. goto abort;
  782. }
  783. usblp->readurb = usb_alloc_urb(0, GFP_KERNEL);
  784. if (!usblp->readurb) {
  785. err("out of memory");
  786. goto abort;
  787. }
  788. /* Malloc device ID string buffer to the largest expected length,
  789. * since we can re-query it on an ioctl and a dynamic string
  790. * could change in length. */
  791. if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
  792. err("out of memory for device_id_string");
  793. goto abort;
  794. }
  795. usblp->writebuf = usblp->readbuf = NULL;
  796. usblp->writeurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  797. usblp->readurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  798. /* Malloc write & read buffers. We somewhat wastefully
  799. * malloc both regardless of bidirectionality, because the
  800. * alternate setting can be changed later via an ioctl. */
  801. if (!(usblp->writebuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
  802. GFP_KERNEL, &usblp->writeurb->transfer_dma))) {
  803. err("out of memory for write buf");
  804. goto abort;
  805. }
  806. if (!(usblp->readbuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
  807. GFP_KERNEL, &usblp->readurb->transfer_dma))) {
  808. err("out of memory for read buf");
  809. goto abort;
  810. }
  811. /* Allocate buffer for printer status */
  812. usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
  813. if (!usblp->statusbuf) {
  814. err("out of memory for statusbuf");
  815. goto abort;
  816. }
  817. /* Lookup quirks for this printer. */
  818. usblp->quirks = usblp_quirks(
  819. le16_to_cpu(dev->descriptor.idVendor),
  820. le16_to_cpu(dev->descriptor.idProduct));
  821. /* Analyze and pick initial alternate settings and endpoints. */
  822. protocol = usblp_select_alts(usblp);
  823. if (protocol < 0) {
  824. dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
  825. le16_to_cpu(dev->descriptor.idVendor),
  826. le16_to_cpu(dev->descriptor.idProduct));
  827. goto abort;
  828. }
  829. /* Setup the selected alternate setting and endpoints. */
  830. if (usblp_set_protocol(usblp, protocol) < 0)
  831. goto abort;
  832. /* Retrieve and store the device ID string. */
  833. usblp_cache_device_id_string(usblp);
  834. retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id);
  835. if (retval)
  836. goto abort_intfdata;
  837. #ifdef DEBUG
  838. usblp_check_status(usblp, 0);
  839. #endif
  840. usb_set_intfdata (intf, usblp);
  841. usblp->present = 1;
  842. retval = usb_register_dev(intf, &usblp_class);
  843. if (retval) {
  844. err("Not able to get a minor for this device.");
  845. goto abort_intfdata;
  846. }
  847. usblp->minor = intf->minor;
  848. info("usblp%d: USB %sdirectional printer dev %d "
  849. "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
  850. usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
  851. usblp->ifnum,
  852. usblp->protocol[usblp->current_protocol].alt_setting,
  853. usblp->current_protocol,
  854. le16_to_cpu(usblp->dev->descriptor.idVendor),
  855. le16_to_cpu(usblp->dev->descriptor.idProduct));
  856. return 0;
  857. abort_intfdata:
  858. usb_set_intfdata (intf, NULL);
  859. device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
  860. abort:
  861. if (usblp) {
  862. if (usblp->writebuf)
  863. usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
  864. usblp->writebuf, usblp->writeurb->transfer_dma);
  865. if (usblp->readbuf)
  866. usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
  867. usblp->readbuf, usblp->writeurb->transfer_dma);
  868. kfree(usblp->statusbuf);
  869. kfree(usblp->device_id_string);
  870. usb_free_urb(usblp->writeurb);
  871. usb_free_urb(usblp->readurb);
  872. kfree(usblp);
  873. }
  874. return -EIO;
  875. }
  876. /*
  877. * We are a "new" style driver with usb_device_id table,
  878. * but our requirements are too intricate for simple match to handle.
  879. *
  880. * The "proto_bias" option may be used to specify the preferred protocol
  881. * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3). If the device
  882. * supports the preferred protocol, then we bind to it.
  883. *
  884. * The best interface for us is 7/1/2, because it is compatible
  885. * with a stream of characters. If we find it, we bind to it.
  886. *
  887. * Note that the people from hpoj.sourceforge.net need to be able to
  888. * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose.
  889. *
  890. * Failing 7/1/2, we look for 7/1/3, even though it's probably not
  891. * stream-compatible, because this matches the behaviour of the old code.
  892. *
  893. * If nothing else, we bind to 7/1/1 - the unidirectional interface.
  894. */
  895. static int usblp_select_alts(struct usblp *usblp)
  896. {
  897. struct usb_interface *if_alt;
  898. struct usb_host_interface *ifd;
  899. struct usb_endpoint_descriptor *epd, *epwrite, *epread;
  900. int p, i, e;
  901. if_alt = usblp->intf;
  902. for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
  903. usblp->protocol[p].alt_setting = -1;
  904. /* Find out what we have. */
  905. for (i = 0; i < if_alt->num_altsetting; i++) {
  906. ifd = &if_alt->altsetting[i];
  907. if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)
  908. if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS))
  909. continue;
  910. if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
  911. ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
  912. continue;
  913. /* Look for bulk OUT and IN endpoints. */
  914. epwrite = epread = NULL;
  915. for (e = 0; e < ifd->desc.bNumEndpoints; e++) {
  916. epd = &ifd->endpoint[e].desc;
  917. if (usb_endpoint_is_bulk_out(epd))
  918. if (!epwrite)
  919. epwrite = epd;
  920. if (usb_endpoint_is_bulk_in(epd))
  921. if (!epread)
  922. epread = epd;
  923. }
  924. /* Ignore buggy hardware without the right endpoints. */
  925. if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread))
  926. continue;
  927. /* Turn off reads for 7/1/1 (unidirectional) interfaces
  928. * and buggy bidirectional printers. */
  929. if (ifd->desc.bInterfaceProtocol == 1) {
  930. epread = NULL;
  931. } else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
  932. info("Disabling reads from problem bidirectional "
  933. "printer on usblp%d", usblp->minor);
  934. epread = NULL;
  935. }
  936. usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
  937. ifd->desc.bAlternateSetting;
  938. usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
  939. usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
  940. }
  941. /* If our requested protocol is supported, then use it. */
  942. if (proto_bias >= USBLP_FIRST_PROTOCOL &&
  943. proto_bias <= USBLP_LAST_PROTOCOL &&
  944. usblp->protocol[proto_bias].alt_setting != -1)
  945. return proto_bias;
  946. /* Ordering is important here. */
  947. if (usblp->protocol[2].alt_setting != -1)
  948. return 2;
  949. if (usblp->protocol[1].alt_setting != -1)
  950. return 1;
  951. if (usblp->protocol[3].alt_setting != -1)
  952. return 3;
  953. /* If nothing is available, then don't bind to this device. */
  954. return -1;
  955. }
  956. static int usblp_set_protocol(struct usblp *usblp, int protocol)
  957. {
  958. int r, alts;
  959. if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
  960. return -EINVAL;
  961. alts = usblp->protocol[protocol].alt_setting;
  962. if (alts < 0)
  963. return -EINVAL;
  964. r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
  965. if (r < 0) {
  966. err("can't set desired altsetting %d on interface %d",
  967. alts, usblp->ifnum);
  968. return r;
  969. }
  970. usb_fill_bulk_urb(usblp->writeurb, usblp->dev,
  971. usb_sndbulkpipe(usblp->dev,
  972. usblp->protocol[protocol].epwrite->bEndpointAddress),
  973. usblp->writebuf, 0,
  974. usblp_bulk_write, usblp);
  975. usblp->bidir = (usblp->protocol[protocol].epread != NULL);
  976. if (usblp->bidir)
  977. usb_fill_bulk_urb(usblp->readurb, usblp->dev,
  978. usb_rcvbulkpipe(usblp->dev,
  979. usblp->protocol[protocol].epread->bEndpointAddress),
  980. usblp->readbuf, USBLP_BUF_SIZE,
  981. usblp_bulk_read, usblp);
  982. usblp->current_protocol = protocol;
  983. dbg("usblp%d set protocol %d", usblp->minor, protocol);
  984. return 0;
  985. }
  986. /* Retrieves and caches device ID string.
  987. * Returns length, including length bytes but not null terminator.
  988. * On error, returns a negative errno value. */
  989. static int usblp_cache_device_id_string(struct usblp *usblp)
  990. {
  991. int err, length;
  992. err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
  993. if (err < 0) {
  994. dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
  995. usblp->minor, err);
  996. usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
  997. return -EIO;
  998. }
  999. /* First two bytes are length in big-endian.
  1000. * They count themselves, and we copy them into
  1001. * the user's buffer. */
  1002. length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
  1003. if (length < 2)
  1004. length = 2;
  1005. else if (length >= USBLP_DEVICE_ID_SIZE)
  1006. length = USBLP_DEVICE_ID_SIZE - 1;
  1007. usblp->device_id_string[length] = '\0';
  1008. dbg("usblp%d Device ID string [len=%d]=\"%s\"",
  1009. usblp->minor, length, &usblp->device_id_string[2]);
  1010. return length;
  1011. }
  1012. static void usblp_disconnect(struct usb_interface *intf)
  1013. {
  1014. struct usblp *usblp = usb_get_intfdata (intf);
  1015. usb_deregister_dev(intf, &usblp_class);
  1016. if (!usblp || !usblp->dev) {
  1017. err("bogus disconnect");
  1018. BUG ();
  1019. }
  1020. device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
  1021. mutex_lock (&usblp_mutex);
  1022. mutex_lock (&usblp->mut);
  1023. usblp->present = 0;
  1024. usb_set_intfdata (intf, NULL);
  1025. usblp_unlink_urbs(usblp);
  1026. usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
  1027. usblp->writebuf, usblp->writeurb->transfer_dma);
  1028. usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
  1029. usblp->readbuf, usblp->readurb->transfer_dma);
  1030. mutex_unlock (&usblp->mut);
  1031. if (!usblp->used)
  1032. usblp_cleanup (usblp);
  1033. mutex_unlock (&usblp_mutex);
  1034. }
  1035. static int usblp_suspend (struct usb_interface *intf, pm_message_t message)
  1036. {
  1037. struct usblp *usblp = usb_get_intfdata (intf);
  1038. /* we take no more IO */
  1039. usblp->sleeping = 1;
  1040. usblp_unlink_urbs(usblp);
  1041. return 0;
  1042. }
  1043. static int usblp_resume (struct usb_interface *intf)
  1044. {
  1045. struct usblp *usblp = usb_get_intfdata (intf);
  1046. int r;
  1047. usblp->sleeping = 0;
  1048. r = handle_bidir (usblp);
  1049. return r;
  1050. }
  1051. static struct usb_device_id usblp_ids [] = {
  1052. { USB_DEVICE_INFO(7, 1, 1) },
  1053. { USB_DEVICE_INFO(7, 1, 2) },
  1054. { USB_DEVICE_INFO(7, 1, 3) },
  1055. { USB_INTERFACE_INFO(7, 1, 1) },
  1056. { USB_INTERFACE_INFO(7, 1, 2) },
  1057. { USB_INTERFACE_INFO(7, 1, 3) },
  1058. { USB_DEVICE(0x04b8, 0x0202) }, /* Seiko Epson Receipt Printer M129C */
  1059. { } /* Terminating entry */
  1060. };
  1061. MODULE_DEVICE_TABLE (usb, usblp_ids);
  1062. static struct usb_driver usblp_driver = {
  1063. .name = "usblp",
  1064. .probe = usblp_probe,
  1065. .disconnect = usblp_disconnect,
  1066. .suspend = usblp_suspend,
  1067. .resume = usblp_resume,
  1068. .id_table = usblp_ids,
  1069. .supports_autosuspend = 1,
  1070. };
  1071. static int __init usblp_init(void)
  1072. {
  1073. int retval;
  1074. retval = usb_register(&usblp_driver);
  1075. if (!retval)
  1076. info(DRIVER_VERSION ": " DRIVER_DESC);
  1077. return retval;
  1078. }
  1079. static void __exit usblp_exit(void)
  1080. {
  1081. usb_deregister(&usblp_driver);
  1082. }
  1083. module_init(usblp_init);
  1084. module_exit(usblp_exit);
  1085. MODULE_AUTHOR( DRIVER_AUTHOR );
  1086. MODULE_DESCRIPTION( DRIVER_DESC );
  1087. module_param(proto_bias, int, S_IRUGO | S_IWUSR);
  1088. MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
  1089. MODULE_LICENSE("GPL");