usblp.c 35 KB

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