usblp.c 39 KB

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