usblp.c 35 KB

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