usblp.c 39 KB

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