usblp.c 39 KB

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