usblp.c 39 KB

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