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