usblp.c 39 KB

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