usblp.c 39 KB

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