usblp.c 39 KB

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