usblp.c 39 KB

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