usblp.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  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. usblp->used = 0;
  379. file->private_data = NULL;
  380. retval = -EIO;
  381. }
  382. out:
  383. mutex_unlock (&usblp_mutex);
  384. return retval;
  385. }
  386. static void usblp_cleanup (struct usblp *usblp)
  387. {
  388. printk(KERN_INFO "usblp%d: removed\n", usblp->minor);
  389. kfree(usblp->readbuf);
  390. kfree (usblp->device_id_string);
  391. kfree (usblp->statusbuf);
  392. kfree (usblp);
  393. }
  394. static void usblp_unlink_urbs(struct usblp *usblp)
  395. {
  396. usb_kill_anchored_urbs(&usblp->urbs);
  397. }
  398. static int usblp_release(struct inode *inode, struct file *file)
  399. {
  400. struct usblp *usblp = file->private_data;
  401. usblp->flags &= ~LP_ABORT;
  402. mutex_lock (&usblp_mutex);
  403. usblp->used = 0;
  404. if (usblp->present) {
  405. usblp_unlink_urbs(usblp);
  406. usb_autopm_put_interface(usblp->intf);
  407. } else /* finish cleanup from disconnect */
  408. usblp_cleanup (usblp);
  409. mutex_unlock (&usblp_mutex);
  410. return 0;
  411. }
  412. /* No kernel lock - fine */
  413. static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
  414. {
  415. int ret;
  416. unsigned long flags;
  417. struct usblp *usblp = file->private_data;
  418. /* Should we check file->f_mode & FMODE_WRITE before poll_wait()? */
  419. poll_wait(file, &usblp->rwait, wait);
  420. poll_wait(file, &usblp->wwait, wait);
  421. spin_lock_irqsave(&usblp->lock, flags);
  422. ret = ((usblp->bidir && usblp->rcomplete) ? POLLIN | POLLRDNORM : 0) |
  423. ((usblp->no_paper || usblp->wcomplete) ? POLLOUT | POLLWRNORM : 0);
  424. spin_unlock_irqrestore(&usblp->lock, flags);
  425. return ret;
  426. }
  427. static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  428. {
  429. struct usblp *usblp = file->private_data;
  430. int length, err, i;
  431. unsigned char newChannel;
  432. int status;
  433. int twoints[2];
  434. int retval = 0;
  435. mutex_lock (&usblp->mut);
  436. if (!usblp->present) {
  437. retval = -ENODEV;
  438. goto done;
  439. }
  440. if (usblp->sleeping) {
  441. retval = -ENODEV;
  442. goto done;
  443. }
  444. dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd),
  445. _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd) );
  446. if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */
  447. switch (_IOC_NR(cmd)) {
  448. case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
  449. if (_IOC_DIR(cmd) != _IOC_READ) {
  450. retval = -EINVAL;
  451. goto done;
  452. }
  453. length = usblp_cache_device_id_string(usblp);
  454. if (length < 0) {
  455. retval = length;
  456. goto done;
  457. }
  458. if (length > _IOC_SIZE(cmd))
  459. length = _IOC_SIZE(cmd); /* truncate */
  460. if (copy_to_user((void __user *) arg,
  461. usblp->device_id_string,
  462. (unsigned long) length)) {
  463. retval = -EFAULT;
  464. goto done;
  465. }
  466. break;
  467. case IOCNR_GET_PROTOCOLS:
  468. if (_IOC_DIR(cmd) != _IOC_READ ||
  469. _IOC_SIZE(cmd) < sizeof(twoints)) {
  470. retval = -EINVAL;
  471. goto done;
  472. }
  473. twoints[0] = usblp->current_protocol;
  474. twoints[1] = 0;
  475. for (i = USBLP_FIRST_PROTOCOL;
  476. i <= USBLP_LAST_PROTOCOL; i++) {
  477. if (usblp->protocol[i].alt_setting >= 0)
  478. twoints[1] |= (1<<i);
  479. }
  480. if (copy_to_user((void __user *)arg,
  481. (unsigned char *)twoints,
  482. sizeof(twoints))) {
  483. retval = -EFAULT;
  484. goto done;
  485. }
  486. break;
  487. case IOCNR_SET_PROTOCOL:
  488. if (_IOC_DIR(cmd) != _IOC_WRITE) {
  489. retval = -EINVAL;
  490. goto done;
  491. }
  492. #ifdef DEBUG
  493. if (arg == -10) {
  494. usblp_dump(usblp);
  495. break;
  496. }
  497. #endif
  498. usblp_unlink_urbs(usblp);
  499. retval = usblp_set_protocol(usblp, arg);
  500. if (retval < 0) {
  501. usblp_set_protocol(usblp,
  502. usblp->current_protocol);
  503. }
  504. break;
  505. case IOCNR_HP_SET_CHANNEL:
  506. if (_IOC_DIR(cmd) != _IOC_WRITE ||
  507. le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
  508. usblp->quirks & USBLP_QUIRK_BIDIR) {
  509. retval = -EINVAL;
  510. goto done;
  511. }
  512. err = usblp_hp_channel_change_request(usblp,
  513. arg, &newChannel);
  514. if (err < 0) {
  515. err("usblp%d: error = %d setting "
  516. "HP channel",
  517. usblp->minor, err);
  518. retval = -EIO;
  519. goto done;
  520. }
  521. dbg("usblp%d requested/got HP channel %ld/%d",
  522. usblp->minor, arg, newChannel);
  523. break;
  524. case IOCNR_GET_BUS_ADDRESS:
  525. if (_IOC_DIR(cmd) != _IOC_READ ||
  526. _IOC_SIZE(cmd) < sizeof(twoints)) {
  527. retval = -EINVAL;
  528. goto done;
  529. }
  530. twoints[0] = usblp->dev->bus->busnum;
  531. twoints[1] = usblp->dev->devnum;
  532. if (copy_to_user((void __user *)arg,
  533. (unsigned char *)twoints,
  534. sizeof(twoints))) {
  535. retval = -EFAULT;
  536. goto done;
  537. }
  538. dbg("usblp%d is bus=%d, device=%d",
  539. usblp->minor, twoints[0], twoints[1]);
  540. break;
  541. case IOCNR_GET_VID_PID:
  542. if (_IOC_DIR(cmd) != _IOC_READ ||
  543. _IOC_SIZE(cmd) < sizeof(twoints)) {
  544. retval = -EINVAL;
  545. goto done;
  546. }
  547. twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
  548. twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
  549. if (copy_to_user((void __user *)arg,
  550. (unsigned char *)twoints,
  551. sizeof(twoints))) {
  552. retval = -EFAULT;
  553. goto done;
  554. }
  555. dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
  556. usblp->minor, twoints[0], twoints[1]);
  557. break;
  558. case IOCNR_SOFT_RESET:
  559. if (_IOC_DIR(cmd) != _IOC_NONE) {
  560. retval = -EINVAL;
  561. goto done;
  562. }
  563. retval = usblp_reset(usblp);
  564. break;
  565. default:
  566. retval = -ENOTTY;
  567. }
  568. else /* old-style ioctl value */
  569. switch (cmd) {
  570. case LPGETSTATUS:
  571. if ((retval = usblp_read_status(usblp, usblp->statusbuf))) {
  572. if (printk_ratelimit())
  573. printk(KERN_ERR "usblp%d:"
  574. "failed reading printer status (%d)\n",
  575. usblp->minor, retval);
  576. retval = -EIO;
  577. goto done;
  578. }
  579. status = *usblp->statusbuf;
  580. if (copy_to_user ((void __user *)arg, &status, sizeof(int)))
  581. retval = -EFAULT;
  582. break;
  583. case LPABORT:
  584. if (arg)
  585. usblp->flags |= LP_ABORT;
  586. else
  587. usblp->flags &= ~LP_ABORT;
  588. break;
  589. default:
  590. retval = -ENOTTY;
  591. }
  592. done:
  593. mutex_unlock (&usblp->mut);
  594. return retval;
  595. }
  596. static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  597. {
  598. struct usblp *usblp = file->private_data;
  599. char *writebuf;
  600. struct urb *writeurb;
  601. int rv;
  602. int transfer_length;
  603. ssize_t writecount = 0;
  604. if (mutex_lock_interruptible(&usblp->wmut)) {
  605. rv = -EINTR;
  606. goto raise_biglock;
  607. }
  608. if ((rv = usblp_wwait(usblp, !!(file->f_flags & O_NONBLOCK))) < 0)
  609. goto raise_wait;
  610. while (writecount < count) {
  611. /*
  612. * Step 1: Submit next block.
  613. */
  614. if ((transfer_length = count - writecount) > USBLP_BUF_SIZE)
  615. transfer_length = USBLP_BUF_SIZE;
  616. rv = -ENOMEM;
  617. if ((writebuf = kmalloc(USBLP_BUF_SIZE, GFP_KERNEL)) == NULL)
  618. goto raise_buf;
  619. if ((writeurb = usb_alloc_urb(0, GFP_KERNEL)) == NULL)
  620. goto raise_urb;
  621. usb_fill_bulk_urb(writeurb, usblp->dev,
  622. usb_sndbulkpipe(usblp->dev,
  623. usblp->protocol[usblp->current_protocol].epwrite->bEndpointAddress),
  624. writebuf, transfer_length, usblp_bulk_write, usblp);
  625. writeurb->transfer_flags |= URB_FREE_BUFFER;
  626. usb_anchor_urb(writeurb, &usblp->urbs);
  627. if (copy_from_user(writebuf,
  628. buffer + writecount, transfer_length)) {
  629. rv = -EFAULT;
  630. goto raise_badaddr;
  631. }
  632. spin_lock_irq(&usblp->lock);
  633. usblp->wcomplete = 0;
  634. spin_unlock_irq(&usblp->lock);
  635. if ((rv = usb_submit_urb(writeurb, GFP_KERNEL)) < 0) {
  636. usblp->wstatus = 0;
  637. spin_lock_irq(&usblp->lock);
  638. usblp->no_paper = 0;
  639. usblp->wcomplete = 1;
  640. wake_up(&usblp->wwait);
  641. spin_unlock_irq(&usblp->lock);
  642. if (rv != -ENOMEM)
  643. rv = -EIO;
  644. goto raise_submit;
  645. }
  646. /*
  647. * Step 2: Wait for transfer to end, collect results.
  648. */
  649. rv = usblp_wwait(usblp, !!(file->f_flags&O_NONBLOCK));
  650. if (rv < 0) {
  651. if (rv == -EAGAIN) {
  652. /* Presume that it's going to complete well. */
  653. writecount += transfer_length;
  654. }
  655. if (rv == -ENOSPC) {
  656. spin_lock_irq(&usblp->lock);
  657. usblp->no_paper = 1; /* Mark for poll(2) */
  658. spin_unlock_irq(&usblp->lock);
  659. writecount += transfer_length;
  660. }
  661. /* Leave URB dangling, to be cleaned on close. */
  662. goto collect_error;
  663. }
  664. if (usblp->wstatus < 0) {
  665. rv = -EIO;
  666. goto collect_error;
  667. }
  668. /*
  669. * This is critical: it must be our URB, not other writer's.
  670. * The wmut exists mainly to cover us here.
  671. */
  672. writecount += usblp->wstatus;
  673. }
  674. mutex_unlock(&usblp->wmut);
  675. return writecount;
  676. raise_submit:
  677. raise_badaddr:
  678. usb_unanchor_urb(writeurb);
  679. usb_free_urb(writeurb);
  680. raise_urb:
  681. kfree(writebuf);
  682. raise_buf:
  683. raise_wait:
  684. collect_error: /* Out of raise sequence */
  685. mutex_unlock(&usblp->wmut);
  686. raise_biglock:
  687. return writecount ? writecount : rv;
  688. }
  689. /*
  690. * Notice that we fail to restart in a few cases: on EFAULT, on restart
  691. * error, etc. This is the historical behaviour. In all such cases we return
  692. * EIO, and applications loop in order to get the new read going.
  693. */
  694. static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, loff_t *ppos)
  695. {
  696. struct usblp *usblp = file->private_data;
  697. ssize_t count;
  698. ssize_t avail;
  699. int rv;
  700. if (!usblp->bidir)
  701. return -EINVAL;
  702. rv = usblp_rwait_and_lock(usblp, !!(file->f_flags & O_NONBLOCK));
  703. if (rv < 0)
  704. return rv;
  705. if ((avail = usblp->rstatus) < 0) {
  706. printk(KERN_ERR "usblp%d: error %d reading from printer\n",
  707. usblp->minor, (int)avail);
  708. usblp_submit_read(usblp);
  709. count = -EIO;
  710. goto done;
  711. }
  712. count = len < avail - usblp->readcount ? len : avail - usblp->readcount;
  713. if (count != 0 &&
  714. copy_to_user(buffer, usblp->readbuf + usblp->readcount, count)) {
  715. count = -EFAULT;
  716. goto done;
  717. }
  718. if ((usblp->readcount += count) == avail) {
  719. if (usblp_submit_read(usblp) < 0) {
  720. /* We don't want to leak USB return codes into errno. */
  721. if (count == 0)
  722. count = -EIO;
  723. goto done;
  724. }
  725. }
  726. done:
  727. mutex_unlock (&usblp->mut);
  728. return count;
  729. }
  730. /*
  731. * Wait for the write path to come idle.
  732. * This is called under the ->wmut, so the idle path stays idle.
  733. *
  734. * Our write path has a peculiar property: it does not buffer like a tty,
  735. * but waits for the write to succeed. This allows our ->release to bug out
  736. * without waiting for writes to drain. But it obviously does not work
  737. * when O_NONBLOCK is set. So, applications setting O_NONBLOCK must use
  738. * select(2) or poll(2) to wait for the buffer to drain before closing.
  739. * Alternatively, set blocking mode with fcntl and issue a zero-size write.
  740. */
  741. static int usblp_wwait(struct usblp *usblp, int nonblock)
  742. {
  743. DECLARE_WAITQUEUE(waita, current);
  744. int rc;
  745. int err = 0;
  746. add_wait_queue(&usblp->wwait, &waita);
  747. for (;;) {
  748. set_current_state(TASK_INTERRUPTIBLE);
  749. if (mutex_lock_interruptible(&usblp->mut)) {
  750. rc = -EINTR;
  751. break;
  752. }
  753. rc = usblp_wtest(usblp, nonblock);
  754. mutex_unlock(&usblp->mut);
  755. if (rc <= 0)
  756. break;
  757. if (usblp->flags & LP_ABORT) {
  758. if (schedule_timeout(msecs_to_jiffies(5000)) == 0) {
  759. err = usblp_check_status(usblp, err);
  760. if (err == 1) { /* Paper out */
  761. rc = -ENOSPC;
  762. break;
  763. }
  764. }
  765. } else {
  766. schedule();
  767. }
  768. }
  769. set_current_state(TASK_RUNNING);
  770. remove_wait_queue(&usblp->wwait, &waita);
  771. return rc;
  772. }
  773. static int usblp_wtest(struct usblp *usblp, int nonblock)
  774. {
  775. unsigned long flags;
  776. if (!usblp->present)
  777. return -ENODEV;
  778. if (signal_pending(current))
  779. return -EINTR;
  780. spin_lock_irqsave(&usblp->lock, flags);
  781. if (usblp->wcomplete) {
  782. spin_unlock_irqrestore(&usblp->lock, flags);
  783. return 0;
  784. }
  785. spin_unlock_irqrestore(&usblp->lock, flags);
  786. if (usblp->sleeping)
  787. return -ENODEV;
  788. if (nonblock)
  789. return -EAGAIN;
  790. return 1;
  791. }
  792. /*
  793. * Wait for read bytes to become available. This probably should have been
  794. * called usblp_r_lock_and_wait(), because we lock first. But it's a traditional
  795. * name for functions which lock and return.
  796. *
  797. * We do not use wait_event_interruptible because it makes locking iffy.
  798. */
  799. static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock)
  800. {
  801. DECLARE_WAITQUEUE(waita, current);
  802. int rc;
  803. add_wait_queue(&usblp->rwait, &waita);
  804. for (;;) {
  805. if (mutex_lock_interruptible(&usblp->mut)) {
  806. rc = -EINTR;
  807. break;
  808. }
  809. set_current_state(TASK_INTERRUPTIBLE);
  810. if ((rc = usblp_rtest(usblp, nonblock)) < 0) {
  811. mutex_unlock(&usblp->mut);
  812. break;
  813. }
  814. if (rc == 0) /* Keep it locked */
  815. break;
  816. mutex_unlock(&usblp->mut);
  817. schedule();
  818. }
  819. set_current_state(TASK_RUNNING);
  820. remove_wait_queue(&usblp->rwait, &waita);
  821. return rc;
  822. }
  823. static int usblp_rtest(struct usblp *usblp, int nonblock)
  824. {
  825. unsigned long flags;
  826. if (!usblp->present)
  827. return -ENODEV;
  828. if (signal_pending(current))
  829. return -EINTR;
  830. spin_lock_irqsave(&usblp->lock, flags);
  831. if (usblp->rcomplete) {
  832. spin_unlock_irqrestore(&usblp->lock, flags);
  833. return 0;
  834. }
  835. spin_unlock_irqrestore(&usblp->lock, flags);
  836. if (usblp->sleeping)
  837. return -ENODEV;
  838. if (nonblock)
  839. return -EAGAIN;
  840. return 1;
  841. }
  842. /*
  843. * Please check ->bidir and other such things outside for now.
  844. */
  845. static int usblp_submit_read(struct usblp *usblp)
  846. {
  847. struct urb *urb;
  848. unsigned long flags;
  849. int rc;
  850. rc = -ENOMEM;
  851. if ((urb = usb_alloc_urb(0, GFP_KERNEL)) == NULL)
  852. goto raise_urb;
  853. usb_fill_bulk_urb(urb, usblp->dev,
  854. usb_rcvbulkpipe(usblp->dev,
  855. usblp->protocol[usblp->current_protocol].epread->bEndpointAddress),
  856. usblp->readbuf, USBLP_BUF_SIZE_IN,
  857. usblp_bulk_read, usblp);
  858. usb_anchor_urb(urb, &usblp->urbs);
  859. spin_lock_irqsave(&usblp->lock, flags);
  860. usblp->readcount = 0; /* XXX Why here? */
  861. usblp->rcomplete = 0;
  862. spin_unlock_irqrestore(&usblp->lock, flags);
  863. if ((rc = usb_submit_urb(urb, GFP_KERNEL)) < 0) {
  864. dbg("error submitting urb (%d)", rc);
  865. spin_lock_irqsave(&usblp->lock, flags);
  866. usblp->rstatus = rc;
  867. usblp->rcomplete = 1;
  868. spin_unlock_irqrestore(&usblp->lock, flags);
  869. goto raise_submit;
  870. }
  871. return 0;
  872. raise_submit:
  873. usb_unanchor_urb(urb);
  874. usb_free_urb(urb);
  875. raise_urb:
  876. return rc;
  877. }
  878. /*
  879. * Checks for printers that have quirks, such as requiring unidirectional
  880. * communication but reporting bidirectional; currently some HP printers
  881. * have this flaw (HP 810, 880, 895, etc.), or needing an init string
  882. * sent at each open (like some Epsons).
  883. * Returns 1 if found, 0 if not found.
  884. *
  885. * HP recommended that we use the bidirectional interface but
  886. * don't attempt any bulk IN transfers from the IN endpoint.
  887. * Here's some more detail on the problem:
  888. * The problem is not that it isn't bidirectional though. The problem
  889. * is that if you request a device ID, or status information, while
  890. * the buffers are full, the return data will end up in the print data
  891. * buffer. For example if you make sure you never request the device ID
  892. * while you are sending print data, and you don't try to query the
  893. * printer status every couple of milliseconds, you will probably be OK.
  894. */
  895. static unsigned int usblp_quirks (__u16 vendor, __u16 product)
  896. {
  897. int i;
  898. for (i = 0; quirk_printers[i].vendorId; i++) {
  899. if (vendor == quirk_printers[i].vendorId &&
  900. product == quirk_printers[i].productId)
  901. return quirk_printers[i].quirks;
  902. }
  903. return 0;
  904. }
  905. static const struct file_operations usblp_fops = {
  906. .owner = THIS_MODULE,
  907. .read = usblp_read,
  908. .write = usblp_write,
  909. .poll = usblp_poll,
  910. .unlocked_ioctl = usblp_ioctl,
  911. .compat_ioctl = usblp_ioctl,
  912. .open = usblp_open,
  913. .release = usblp_release,
  914. };
  915. static struct usb_class_driver usblp_class = {
  916. .name = "lp%d",
  917. .fops = &usblp_fops,
  918. .minor_base = USBLP_MINOR_BASE,
  919. };
  920. static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf)
  921. {
  922. struct usb_interface *intf = to_usb_interface(dev);
  923. struct usblp *usblp = usb_get_intfdata (intf);
  924. if (usblp->device_id_string[0] == 0 &&
  925. usblp->device_id_string[1] == 0)
  926. return 0;
  927. return sprintf(buf, "%s", usblp->device_id_string+2);
  928. }
  929. static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL);
  930. static int usblp_probe(struct usb_interface *intf,
  931. const struct usb_device_id *id)
  932. {
  933. struct usb_device *dev = interface_to_usbdev (intf);
  934. struct usblp *usblp = NULL;
  935. int protocol;
  936. int retval;
  937. /* Malloc and start initializing usblp structure so we can use it
  938. * directly. */
  939. if (!(usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL))) {
  940. retval = -ENOMEM;
  941. goto abort;
  942. }
  943. usblp->dev = dev;
  944. mutex_init(&usblp->wmut);
  945. mutex_init (&usblp->mut);
  946. spin_lock_init(&usblp->lock);
  947. init_waitqueue_head(&usblp->rwait);
  948. init_waitqueue_head(&usblp->wwait);
  949. init_usb_anchor(&usblp->urbs);
  950. usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  951. usblp->intf = intf;
  952. /* Malloc device ID string buffer to the largest expected length,
  953. * since we can re-query it on an ioctl and a dynamic string
  954. * could change in length. */
  955. if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
  956. retval = -ENOMEM;
  957. goto abort;
  958. }
  959. /*
  960. * Allocate read buffer. We somewhat wastefully
  961. * malloc both regardless of bidirectionality, because the
  962. * alternate setting can be changed later via an ioctl.
  963. */
  964. if (!(usblp->readbuf = kmalloc(USBLP_BUF_SIZE_IN, GFP_KERNEL))) {
  965. retval = -ENOMEM;
  966. goto abort;
  967. }
  968. /* Allocate buffer for printer status */
  969. usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
  970. if (!usblp->statusbuf) {
  971. retval = -ENOMEM;
  972. goto abort;
  973. }
  974. /* Lookup quirks for this printer. */
  975. usblp->quirks = usblp_quirks(
  976. le16_to_cpu(dev->descriptor.idVendor),
  977. le16_to_cpu(dev->descriptor.idProduct));
  978. /* Analyze and pick initial alternate settings and endpoints. */
  979. protocol = usblp_select_alts(usblp);
  980. if (protocol < 0) {
  981. dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
  982. le16_to_cpu(dev->descriptor.idVendor),
  983. le16_to_cpu(dev->descriptor.idProduct));
  984. retval = -ENODEV;
  985. goto abort;
  986. }
  987. /* Setup the selected alternate setting and endpoints. */
  988. if (usblp_set_protocol(usblp, protocol) < 0) {
  989. retval = -ENODEV; /* ->probe isn't ->ioctl */
  990. goto abort;
  991. }
  992. /* Retrieve and store the device ID string. */
  993. usblp_cache_device_id_string(usblp);
  994. retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id);
  995. if (retval)
  996. goto abort_intfdata;
  997. #ifdef DEBUG
  998. usblp_check_status(usblp, 0);
  999. #endif
  1000. usb_set_intfdata (intf, usblp);
  1001. usblp->present = 1;
  1002. retval = usb_register_dev(intf, &usblp_class);
  1003. if (retval) {
  1004. printk(KERN_ERR "usblp: Not able to get a minor"
  1005. " (base %u, slice default): %d\n",
  1006. USBLP_MINOR_BASE, retval);
  1007. goto abort_intfdata;
  1008. }
  1009. usblp->minor = intf->minor;
  1010. printk(KERN_INFO "usblp%d: USB %sdirectional printer dev %d "
  1011. "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X\n",
  1012. usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
  1013. usblp->ifnum,
  1014. usblp->protocol[usblp->current_protocol].alt_setting,
  1015. usblp->current_protocol,
  1016. le16_to_cpu(usblp->dev->descriptor.idVendor),
  1017. le16_to_cpu(usblp->dev->descriptor.idProduct));
  1018. return 0;
  1019. abort_intfdata:
  1020. usb_set_intfdata (intf, NULL);
  1021. device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
  1022. abort:
  1023. if (usblp) {
  1024. kfree(usblp->readbuf);
  1025. kfree(usblp->statusbuf);
  1026. kfree(usblp->device_id_string);
  1027. kfree(usblp);
  1028. }
  1029. return retval;
  1030. }
  1031. /*
  1032. * We are a "new" style driver with usb_device_id table,
  1033. * but our requirements are too intricate for simple match to handle.
  1034. *
  1035. * The "proto_bias" option may be used to specify the preferred protocol
  1036. * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3). If the device
  1037. * supports the preferred protocol, then we bind to it.
  1038. *
  1039. * The best interface for us is 7/1/2, because it is compatible
  1040. * with a stream of characters. If we find it, we bind to it.
  1041. *
  1042. * Note that the people from hpoj.sourceforge.net need to be able to
  1043. * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose.
  1044. *
  1045. * Failing 7/1/2, we look for 7/1/3, even though it's probably not
  1046. * stream-compatible, because this matches the behaviour of the old code.
  1047. *
  1048. * If nothing else, we bind to 7/1/1 - the unidirectional interface.
  1049. */
  1050. static int usblp_select_alts(struct usblp *usblp)
  1051. {
  1052. struct usb_interface *if_alt;
  1053. struct usb_host_interface *ifd;
  1054. struct usb_endpoint_descriptor *epd, *epwrite, *epread;
  1055. int p, i, e;
  1056. if_alt = usblp->intf;
  1057. for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
  1058. usblp->protocol[p].alt_setting = -1;
  1059. /* Find out what we have. */
  1060. for (i = 0; i < if_alt->num_altsetting; i++) {
  1061. ifd = &if_alt->altsetting[i];
  1062. if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)
  1063. if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS))
  1064. continue;
  1065. if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
  1066. ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
  1067. continue;
  1068. /* Look for bulk OUT and IN endpoints. */
  1069. epwrite = epread = NULL;
  1070. for (e = 0; e < ifd->desc.bNumEndpoints; e++) {
  1071. epd = &ifd->endpoint[e].desc;
  1072. if (usb_endpoint_is_bulk_out(epd))
  1073. if (!epwrite)
  1074. epwrite = epd;
  1075. if (usb_endpoint_is_bulk_in(epd))
  1076. if (!epread)
  1077. epread = epd;
  1078. }
  1079. /* Ignore buggy hardware without the right endpoints. */
  1080. if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread))
  1081. continue;
  1082. /* Turn off reads for 7/1/1 (unidirectional) interfaces
  1083. * and buggy bidirectional printers. */
  1084. if (ifd->desc.bInterfaceProtocol == 1) {
  1085. epread = NULL;
  1086. } else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
  1087. printk(KERN_INFO "usblp%d: Disabling reads from "
  1088. "problematic bidirectional printer\n",
  1089. usblp->minor);
  1090. epread = NULL;
  1091. }
  1092. usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
  1093. ifd->desc.bAlternateSetting;
  1094. usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
  1095. usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
  1096. }
  1097. /* If our requested protocol is supported, then use it. */
  1098. if (proto_bias >= USBLP_FIRST_PROTOCOL &&
  1099. proto_bias <= USBLP_LAST_PROTOCOL &&
  1100. usblp->protocol[proto_bias].alt_setting != -1)
  1101. return proto_bias;
  1102. /* Ordering is important here. */
  1103. if (usblp->protocol[2].alt_setting != -1)
  1104. return 2;
  1105. if (usblp->protocol[1].alt_setting != -1)
  1106. return 1;
  1107. if (usblp->protocol[3].alt_setting != -1)
  1108. return 3;
  1109. /* If nothing is available, then don't bind to this device. */
  1110. return -1;
  1111. }
  1112. static int usblp_set_protocol(struct usblp *usblp, int protocol)
  1113. {
  1114. int r, alts;
  1115. if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
  1116. return -EINVAL;
  1117. alts = usblp->protocol[protocol].alt_setting;
  1118. if (alts < 0)
  1119. return -EINVAL;
  1120. r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
  1121. if (r < 0) {
  1122. printk(KERN_ERR "usblp: can't set desired altsetting %d on interface %d\n",
  1123. alts, usblp->ifnum);
  1124. return r;
  1125. }
  1126. usblp->bidir = (usblp->protocol[protocol].epread != NULL);
  1127. usblp->current_protocol = protocol;
  1128. dbg("usblp%d set protocol %d", usblp->minor, protocol);
  1129. return 0;
  1130. }
  1131. /* Retrieves and caches device ID string.
  1132. * Returns length, including length bytes but not null terminator.
  1133. * On error, returns a negative errno value. */
  1134. static int usblp_cache_device_id_string(struct usblp *usblp)
  1135. {
  1136. int err, length;
  1137. err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
  1138. if (err < 0) {
  1139. dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
  1140. usblp->minor, err);
  1141. usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
  1142. return -EIO;
  1143. }
  1144. /* First two bytes are length in big-endian.
  1145. * They count themselves, and we copy them into
  1146. * the user's buffer. */
  1147. length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
  1148. if (length < 2)
  1149. length = 2;
  1150. else if (length >= USBLP_DEVICE_ID_SIZE)
  1151. length = USBLP_DEVICE_ID_SIZE - 1;
  1152. usblp->device_id_string[length] = '\0';
  1153. dbg("usblp%d Device ID string [len=%d]=\"%s\"",
  1154. usblp->minor, length, &usblp->device_id_string[2]);
  1155. return length;
  1156. }
  1157. static void usblp_disconnect(struct usb_interface *intf)
  1158. {
  1159. struct usblp *usblp = usb_get_intfdata (intf);
  1160. usb_deregister_dev(intf, &usblp_class);
  1161. if (!usblp || !usblp->dev) {
  1162. err("bogus disconnect");
  1163. BUG ();
  1164. }
  1165. device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
  1166. mutex_lock (&usblp_mutex);
  1167. mutex_lock (&usblp->mut);
  1168. usblp->present = 0;
  1169. wake_up(&usblp->wwait);
  1170. wake_up(&usblp->rwait);
  1171. usb_set_intfdata (intf, NULL);
  1172. usblp_unlink_urbs(usblp);
  1173. mutex_unlock (&usblp->mut);
  1174. if (!usblp->used)
  1175. usblp_cleanup (usblp);
  1176. mutex_unlock (&usblp_mutex);
  1177. }
  1178. static int usblp_suspend (struct usb_interface *intf, pm_message_t message)
  1179. {
  1180. struct usblp *usblp = usb_get_intfdata (intf);
  1181. /* we take no more IO */
  1182. usblp->sleeping = 1;
  1183. usblp_unlink_urbs(usblp);
  1184. #if 0 /* XXX Do we want this? What if someone is reading, should we fail? */
  1185. /* not strictly necessary, but just in case */
  1186. wake_up(&usblp->wwait);
  1187. wake_up(&usblp->rwait);
  1188. #endif
  1189. return 0;
  1190. }
  1191. static int usblp_resume (struct usb_interface *intf)
  1192. {
  1193. struct usblp *usblp = usb_get_intfdata (intf);
  1194. int r;
  1195. usblp->sleeping = 0;
  1196. r = handle_bidir (usblp);
  1197. return r;
  1198. }
  1199. static struct usb_device_id usblp_ids [] = {
  1200. { USB_DEVICE_INFO(7, 1, 1) },
  1201. { USB_DEVICE_INFO(7, 1, 2) },
  1202. { USB_DEVICE_INFO(7, 1, 3) },
  1203. { USB_INTERFACE_INFO(7, 1, 1) },
  1204. { USB_INTERFACE_INFO(7, 1, 2) },
  1205. { USB_INTERFACE_INFO(7, 1, 3) },
  1206. { USB_DEVICE(0x04b8, 0x0202) }, /* Seiko Epson Receipt Printer M129C */
  1207. { } /* Terminating entry */
  1208. };
  1209. MODULE_DEVICE_TABLE (usb, usblp_ids);
  1210. static struct usb_driver usblp_driver = {
  1211. .name = "usblp",
  1212. .probe = usblp_probe,
  1213. .disconnect = usblp_disconnect,
  1214. .suspend = usblp_suspend,
  1215. .resume = usblp_resume,
  1216. .id_table = usblp_ids,
  1217. .supports_autosuspend = 1,
  1218. };
  1219. static int __init usblp_init(void)
  1220. {
  1221. return usb_register(&usblp_driver);
  1222. }
  1223. static void __exit usblp_exit(void)
  1224. {
  1225. usb_deregister(&usblp_driver);
  1226. }
  1227. module_init(usblp_init);
  1228. module_exit(usblp_exit);
  1229. MODULE_AUTHOR( DRIVER_AUTHOR );
  1230. MODULE_DESCRIPTION( DRIVER_DESC );
  1231. module_param(proto_bias, int, S_IRUGO | S_IWUSR);
  1232. MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
  1233. MODULE_LICENSE("GPL");