usblp.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /*
  2. * usblp.c Version 0.13
  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/smp_lock.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_VERSION "v0.13"
  63. #define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
  64. #define DRIVER_DESC "USB Printer Device Class driver"
  65. #define USBLP_BUF_SIZE 8192
  66. #define USBLP_DEVICE_ID_SIZE 1024
  67. /* ioctls: */
  68. #define LPGETSTATUS 0x060b /* same as in drivers/char/lp.c */
  69. #define IOCNR_GET_DEVICE_ID 1
  70. #define IOCNR_GET_PROTOCOLS 2
  71. #define IOCNR_SET_PROTOCOL 3
  72. #define IOCNR_HP_SET_CHANNEL 4
  73. #define IOCNR_GET_BUS_ADDRESS 5
  74. #define IOCNR_GET_VID_PID 6
  75. #define IOCNR_SOFT_RESET 7
  76. /* Get device_id string: */
  77. #define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
  78. /* The following ioctls were added for http://hpoj.sourceforge.net: */
  79. /* Get two-int array:
  80. * [0]=current protocol (1=7/1/1, 2=7/1/2, 3=7/1/3),
  81. * [1]=supported protocol mask (mask&(1<<n)!=0 means 7/1/n supported): */
  82. #define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
  83. /* Set protocol (arg: 1=7/1/1, 2=7/1/2, 3=7/1/3): */
  84. #define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
  85. /* Set channel number (HP Vendor-specific command): */
  86. #define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
  87. /* Get two-int array: [0]=bus number, [1]=device address: */
  88. #define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
  89. /* Get two-int array: [0]=vendor ID, [1]=product ID: */
  90. #define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
  91. /* Perform class specific soft reset */
  92. #define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0);
  93. /*
  94. * A DEVICE_ID string may include the printer's serial number.
  95. * It should end with a semi-colon (';').
  96. * An example from an HP 970C DeskJet printer is (this is one long string,
  97. * with the serial number changed):
  98. 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: ;
  99. */
  100. /*
  101. * USB Printer Requests
  102. */
  103. #define USBLP_REQ_GET_ID 0x00
  104. #define USBLP_REQ_GET_STATUS 0x01
  105. #define USBLP_REQ_RESET 0x02
  106. #define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST 0x00 /* HP Vendor-specific */
  107. #define USBLP_MINORS 16
  108. #define USBLP_MINOR_BASE 0
  109. #define USBLP_WRITE_TIMEOUT (5000) /* 5 seconds */
  110. #define USBLP_FIRST_PROTOCOL 1
  111. #define USBLP_LAST_PROTOCOL 3
  112. #define USBLP_MAX_PROTOCOLS (USBLP_LAST_PROTOCOL+1)
  113. /*
  114. * some arbitrary status buffer size;
  115. * need a status buffer that is allocated via kmalloc(), not on stack
  116. */
  117. #define STATUS_BUF_SIZE 8
  118. struct usblp {
  119. struct usb_device *dev; /* USB device */
  120. struct mutex mut; /* locks this struct, especially "dev" */
  121. char *writebuf; /* write transfer_buffer */
  122. char *readbuf; /* read transfer_buffer */
  123. char *statusbuf; /* status transfer_buffer */
  124. struct urb *readurb, *writeurb; /* The urbs */
  125. wait_queue_head_t wait; /* Zzzzz ... */
  126. int readcount; /* Counter for reads */
  127. int ifnum; /* Interface number */
  128. struct usb_interface *intf; /* The interface */
  129. /* Alternate-setting numbers and endpoints for each protocol
  130. * (7/1/{index=1,2,3}) that the device supports: */
  131. struct {
  132. int alt_setting;
  133. struct usb_endpoint_descriptor *epwrite;
  134. struct usb_endpoint_descriptor *epread;
  135. } protocol[USBLP_MAX_PROTOCOLS];
  136. int current_protocol;
  137. int minor; /* minor number of device */
  138. int wcomplete; /* writing is completed */
  139. int rcomplete; /* reading is completed */
  140. unsigned int quirks; /* quirks flags */
  141. unsigned char used; /* True if open */
  142. unsigned char present; /* True if not disconnected */
  143. unsigned char bidir; /* interface is bidirectional */
  144. unsigned char sleeping; /* interface is suspended */
  145. unsigned char *device_id_string; /* IEEE 1284 DEVICE ID string (ptr) */
  146. /* first 2 bytes are (big-endian) length */
  147. };
  148. #ifdef DEBUG
  149. static void usblp_dump(struct usblp *usblp) {
  150. int p;
  151. dbg("usblp=0x%p", usblp);
  152. dbg("dev=0x%p", usblp->dev);
  153. dbg("present=%d", usblp->present);
  154. dbg("readbuf=0x%p", usblp->readbuf);
  155. dbg("writebuf=0x%p", usblp->writebuf);
  156. dbg("readurb=0x%p", usblp->readurb);
  157. dbg("writeurb=0x%p", usblp->writeurb);
  158. dbg("readcount=%d", usblp->readcount);
  159. dbg("ifnum=%d", usblp->ifnum);
  160. for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
  161. dbg("protocol[%d].alt_setting=%d", p, usblp->protocol[p].alt_setting);
  162. dbg("protocol[%d].epwrite=%p", p, usblp->protocol[p].epwrite);
  163. dbg("protocol[%d].epread=%p", p, usblp->protocol[p].epread);
  164. }
  165. dbg("current_protocol=%d", usblp->current_protocol);
  166. dbg("minor=%d", usblp->minor);
  167. dbg("wcomplete=%d", usblp->wcomplete);
  168. dbg("rcomplete=%d", usblp->rcomplete);
  169. dbg("quirks=%d", usblp->quirks);
  170. dbg("used=%d", usblp->used);
  171. dbg("bidir=%d", usblp->bidir);
  172. dbg("sleeping=%d", usblp->sleeping);
  173. dbg("device_id_string=\"%s\"",
  174. usblp->device_id_string ?
  175. usblp->device_id_string + 2 :
  176. (unsigned char *)"(null)");
  177. }
  178. #endif
  179. /* Quirks: various printer quirks are handled by this table & its flags. */
  180. struct quirk_printer_struct {
  181. __u16 vendorId;
  182. __u16 productId;
  183. unsigned int quirks;
  184. };
  185. #define USBLP_QUIRK_BIDIR 0x1 /* reports bidir but requires unidirectional mode (no INs/reads) */
  186. #define USBLP_QUIRK_USB_INIT 0x2 /* needs vendor USB init string */
  187. #define USBLP_QUIRK_BAD_CLASS 0x4 /* descriptor uses vendor-specific Class or SubClass */
  188. static const struct quirk_printer_struct quirk_printers[] = {
  189. { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
  190. { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
  191. { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
  192. { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
  193. { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
  194. { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
  195. { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */
  196. { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */
  197. { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
  198. { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
  199. { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
  200. { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
  201. { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
  202. { 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
  203. { 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt Printer M129C */
  204. { 0, 0 }
  205. };
  206. static int usblp_select_alts(struct usblp *usblp);
  207. static int usblp_set_protocol(struct usblp *usblp, int protocol);
  208. static int usblp_cache_device_id_string(struct usblp *usblp);
  209. /* forward reference to make our lives easier */
  210. static struct usb_driver usblp_driver;
  211. static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */
  212. /*
  213. * Functions for usblp control messages.
  214. */
  215. static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
  216. {
  217. int retval;
  218. int index = usblp->ifnum;
  219. /* High byte has the interface index.
  220. Low byte has the alternate setting.
  221. */
  222. if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS)) {
  223. index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
  224. }
  225. retval = usb_control_msg(usblp->dev,
  226. dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
  227. request, type | dir | recip, value, index, buf, len, USBLP_WRITE_TIMEOUT);
  228. dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d",
  229. request, !!dir, recip, value, index, len, retval);
  230. return retval < 0 ? retval : 0;
  231. }
  232. #define usblp_read_status(usblp, status)\
  233. usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
  234. #define usblp_get_id(usblp, config, id, maxlen)\
  235. usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
  236. #define usblp_reset(usblp)\
  237. usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
  238. #define usblp_hp_channel_change_request(usblp, channel, buffer) \
  239. usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
  240. /*
  241. * See the description for usblp_select_alts() below for the usage
  242. * explanation. Look into your /proc/bus/usb/devices and dmesg in
  243. * case of any trouble.
  244. */
  245. static int proto_bias = -1;
  246. /*
  247. * URB callback.
  248. */
  249. static void usblp_bulk_read(struct urb *urb)
  250. {
  251. struct usblp *usblp = urb->context;
  252. if (unlikely(!usblp || !usblp->dev || !usblp->used))
  253. return;
  254. if (unlikely(!usblp->present))
  255. goto unplug;
  256. if (unlikely(urb->status))
  257. warn("usblp%d: nonzero read/write bulk status received: %d",
  258. usblp->minor, urb->status);
  259. usblp->rcomplete = 1;
  260. unplug:
  261. wake_up_interruptible(&usblp->wait);
  262. }
  263. static void usblp_bulk_write(struct urb *urb)
  264. {
  265. struct usblp *usblp = urb->context;
  266. if (unlikely(!usblp || !usblp->dev || !usblp->used))
  267. return;
  268. if (unlikely(!usblp->present))
  269. goto unplug;
  270. if (unlikely(urb->status))
  271. warn("usblp%d: nonzero read/write bulk status received: %d",
  272. usblp->minor, urb->status);
  273. usblp->wcomplete = 1;
  274. unplug:
  275. wake_up_interruptible(&usblp->wait);
  276. }
  277. /*
  278. * Get and print printer errors.
  279. */
  280. static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
  281. static int usblp_check_status(struct usblp *usblp, int err)
  282. {
  283. unsigned char status, newerr = 0;
  284. int error;
  285. error = usblp_read_status (usblp, usblp->statusbuf);
  286. if (error < 0) {
  287. if (printk_ratelimit())
  288. err("usblp%d: error %d reading printer status",
  289. usblp->minor, error);
  290. return 0;
  291. }
  292. status = *usblp->statusbuf;
  293. if (~status & LP_PERRORP)
  294. newerr = 3;
  295. if (status & LP_POUTPA)
  296. newerr = 1;
  297. if (~status & LP_PSELECD)
  298. newerr = 2;
  299. if (newerr != err)
  300. info("usblp%d: %s", usblp->minor, usblp_messages[newerr]);
  301. return newerr;
  302. }
  303. static int handle_bidir (struct usblp *usblp)
  304. {
  305. if (usblp->bidir && usblp->used && !usblp->sleeping) {
  306. usblp->readcount = 0;
  307. usblp->readurb->dev = usblp->dev;
  308. if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0) {
  309. usblp->used = 0;
  310. return -EIO;
  311. }
  312. }
  313. return 0;
  314. }
  315. /*
  316. * File op functions.
  317. */
  318. static int usblp_open(struct inode *inode, struct file *file)
  319. {
  320. int minor = iminor(inode);
  321. struct usblp *usblp;
  322. struct usb_interface *intf;
  323. int retval;
  324. if (minor < 0)
  325. return -ENODEV;
  326. mutex_lock (&usblp_mutex);
  327. retval = -ENODEV;
  328. intf = usb_find_interface(&usblp_driver, minor);
  329. if (!intf) {
  330. goto out;
  331. }
  332. usblp = usb_get_intfdata (intf);
  333. if (!usblp || !usblp->dev || !usblp->present)
  334. goto out;
  335. retval = -EBUSY;
  336. if (usblp->used)
  337. goto out;
  338. /*
  339. * TODO: need to implement LP_ABORTOPEN + O_NONBLOCK as in drivers/char/lp.c ???
  340. * This is #if 0-ed because we *don't* want to fail an open
  341. * just because the printer is off-line.
  342. */
  343. #if 0
  344. if ((retval = usblp_check_status(usblp, 0))) {
  345. retval = retval > 1 ? -EIO : -ENOSPC;
  346. goto out;
  347. }
  348. #else
  349. retval = 0;
  350. #endif
  351. retval = usb_autopm_get_interface(intf);
  352. if (retval < 0)
  353. goto out;
  354. usblp->used = 1;
  355. file->private_data = usblp;
  356. usblp->writeurb->transfer_buffer_length = 0;
  357. usblp->wcomplete = 1; /* we begin writeable */
  358. usblp->rcomplete = 0;
  359. usblp->writeurb->status = 0;
  360. usblp->readurb->status = 0;
  361. if (handle_bidir(usblp) < 0) {
  362. file->private_data = NULL;
  363. retval = -EIO;
  364. }
  365. out:
  366. mutex_unlock (&usblp_mutex);
  367. return retval;
  368. }
  369. static void usblp_cleanup (struct usblp *usblp)
  370. {
  371. info("usblp%d: removed", usblp->minor);
  372. kfree (usblp->device_id_string);
  373. kfree (usblp->statusbuf);
  374. usb_free_urb(usblp->writeurb);
  375. usb_free_urb(usblp->readurb);
  376. kfree (usblp);
  377. }
  378. static void usblp_unlink_urbs(struct usblp *usblp)
  379. {
  380. usb_kill_urb(usblp->writeurb);
  381. if (usblp->bidir)
  382. usb_kill_urb(usblp->readurb);
  383. }
  384. static int usblp_release(struct inode *inode, struct file *file)
  385. {
  386. struct usblp *usblp = file->private_data;
  387. mutex_lock (&usblp_mutex);
  388. usblp->used = 0;
  389. if (usblp->present) {
  390. usblp_unlink_urbs(usblp);
  391. usb_autopm_put_interface(usblp->intf);
  392. } else /* finish cleanup from disconnect */
  393. usblp_cleanup (usblp);
  394. mutex_unlock (&usblp_mutex);
  395. return 0;
  396. }
  397. /* No kernel lock - fine */
  398. static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
  399. {
  400. struct usblp *usblp = file->private_data;
  401. poll_wait(file, &usblp->wait, wait);
  402. return ((!usblp->bidir || !usblp->rcomplete) ? 0 : POLLIN | POLLRDNORM)
  403. | (!usblp->wcomplete ? 0 : POLLOUT | POLLWRNORM);
  404. }
  405. static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  406. {
  407. struct usblp *usblp = file->private_data;
  408. int length, err, i;
  409. unsigned char newChannel;
  410. int status;
  411. int twoints[2];
  412. int retval = 0;
  413. mutex_lock (&usblp->mut);
  414. if (!usblp->present) {
  415. retval = -ENODEV;
  416. goto done;
  417. }
  418. if (usblp->sleeping) {
  419. retval = -ENODEV;
  420. goto done;
  421. }
  422. dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd),
  423. _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd) );
  424. if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */
  425. switch (_IOC_NR(cmd)) {
  426. case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
  427. if (_IOC_DIR(cmd) != _IOC_READ) {
  428. retval = -EINVAL;
  429. goto done;
  430. }
  431. length = usblp_cache_device_id_string(usblp);
  432. if (length < 0) {
  433. retval = length;
  434. goto done;
  435. }
  436. if (length > _IOC_SIZE(cmd))
  437. length = _IOC_SIZE(cmd); /* truncate */
  438. if (copy_to_user((void __user *) arg,
  439. usblp->device_id_string,
  440. (unsigned long) length)) {
  441. retval = -EFAULT;
  442. goto done;
  443. }
  444. break;
  445. case IOCNR_GET_PROTOCOLS:
  446. if (_IOC_DIR(cmd) != _IOC_READ ||
  447. _IOC_SIZE(cmd) < sizeof(twoints)) {
  448. retval = -EINVAL;
  449. goto done;
  450. }
  451. twoints[0] = usblp->current_protocol;
  452. twoints[1] = 0;
  453. for (i = USBLP_FIRST_PROTOCOL;
  454. i <= USBLP_LAST_PROTOCOL; i++) {
  455. if (usblp->protocol[i].alt_setting >= 0)
  456. twoints[1] |= (1<<i);
  457. }
  458. if (copy_to_user((void __user *)arg,
  459. (unsigned char *)twoints,
  460. sizeof(twoints))) {
  461. retval = -EFAULT;
  462. goto done;
  463. }
  464. break;
  465. case IOCNR_SET_PROTOCOL:
  466. if (_IOC_DIR(cmd) != _IOC_WRITE) {
  467. retval = -EINVAL;
  468. goto done;
  469. }
  470. #ifdef DEBUG
  471. if (arg == -10) {
  472. usblp_dump(usblp);
  473. break;
  474. }
  475. #endif
  476. usblp_unlink_urbs(usblp);
  477. retval = usblp_set_protocol(usblp, arg);
  478. if (retval < 0) {
  479. usblp_set_protocol(usblp,
  480. usblp->current_protocol);
  481. }
  482. break;
  483. case IOCNR_HP_SET_CHANNEL:
  484. if (_IOC_DIR(cmd) != _IOC_WRITE ||
  485. le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
  486. usblp->quirks & USBLP_QUIRK_BIDIR) {
  487. retval = -EINVAL;
  488. goto done;
  489. }
  490. err = usblp_hp_channel_change_request(usblp,
  491. arg, &newChannel);
  492. if (err < 0) {
  493. err("usblp%d: error = %d setting "
  494. "HP channel",
  495. usblp->minor, err);
  496. retval = -EIO;
  497. goto done;
  498. }
  499. dbg("usblp%d requested/got HP channel %ld/%d",
  500. usblp->minor, arg, newChannel);
  501. break;
  502. case IOCNR_GET_BUS_ADDRESS:
  503. if (_IOC_DIR(cmd) != _IOC_READ ||
  504. _IOC_SIZE(cmd) < sizeof(twoints)) {
  505. retval = -EINVAL;
  506. goto done;
  507. }
  508. twoints[0] = usblp->dev->bus->busnum;
  509. twoints[1] = usblp->dev->devnum;
  510. if (copy_to_user((void __user *)arg,
  511. (unsigned char *)twoints,
  512. sizeof(twoints))) {
  513. retval = -EFAULT;
  514. goto done;
  515. }
  516. dbg("usblp%d is bus=%d, device=%d",
  517. usblp->minor, twoints[0], twoints[1]);
  518. break;
  519. case IOCNR_GET_VID_PID:
  520. if (_IOC_DIR(cmd) != _IOC_READ ||
  521. _IOC_SIZE(cmd) < sizeof(twoints)) {
  522. retval = -EINVAL;
  523. goto done;
  524. }
  525. twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
  526. twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
  527. if (copy_to_user((void __user *)arg,
  528. (unsigned char *)twoints,
  529. sizeof(twoints))) {
  530. retval = -EFAULT;
  531. goto done;
  532. }
  533. dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
  534. usblp->minor, twoints[0], twoints[1]);
  535. break;
  536. case IOCNR_SOFT_RESET:
  537. if (_IOC_DIR(cmd) != _IOC_NONE) {
  538. retval = -EINVAL;
  539. goto done;
  540. }
  541. retval = usblp_reset(usblp);
  542. break;
  543. default:
  544. retval = -ENOTTY;
  545. }
  546. else /* old-style ioctl value */
  547. switch (cmd) {
  548. case LPGETSTATUS:
  549. if (usblp_read_status(usblp, usblp->statusbuf)) {
  550. if (printk_ratelimit())
  551. err("usblp%d: failed reading printer status",
  552. usblp->minor);
  553. retval = -EIO;
  554. goto done;
  555. }
  556. status = *usblp->statusbuf;
  557. if (copy_to_user ((void __user *)arg, &status, sizeof(int)))
  558. retval = -EFAULT;
  559. break;
  560. default:
  561. retval = -ENOTTY;
  562. }
  563. done:
  564. mutex_unlock (&usblp->mut);
  565. return retval;
  566. }
  567. static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  568. {
  569. struct usblp *usblp = file->private_data;
  570. int timeout, intr, rv, err = 0, transfer_length = 0;
  571. size_t writecount = 0;
  572. while (writecount < count) {
  573. if (!usblp->wcomplete) {
  574. barrier();
  575. if (file->f_flags & O_NONBLOCK) {
  576. writecount += transfer_length;
  577. return writecount ? writecount : -EAGAIN;
  578. }
  579. timeout = USBLP_WRITE_TIMEOUT;
  580. rv = wait_event_interruptible_timeout(usblp->wait, usblp->wcomplete || !usblp->present , timeout);
  581. if (rv < 0)
  582. return writecount ? writecount : -EINTR;
  583. }
  584. intr = mutex_lock_interruptible (&usblp->mut);
  585. if (intr)
  586. return writecount ? writecount : -EINTR;
  587. if (!usblp->present) {
  588. mutex_unlock (&usblp->mut);
  589. return -ENODEV;
  590. }
  591. if (usblp->sleeping) {
  592. mutex_unlock (&usblp->mut);
  593. return writecount ? writecount : -ENODEV;
  594. }
  595. if (usblp->writeurb->status != 0) {
  596. if (usblp->quirks & USBLP_QUIRK_BIDIR) {
  597. if (!usblp->wcomplete)
  598. err("usblp%d: error %d writing to printer",
  599. usblp->minor, usblp->writeurb->status);
  600. err = usblp->writeurb->status;
  601. } else
  602. err = usblp_check_status(usblp, err);
  603. mutex_unlock (&usblp->mut);
  604. /* if the fault was due to disconnect, let khubd's
  605. * call to usblp_disconnect() grab usblp->mut ...
  606. */
  607. schedule ();
  608. continue;
  609. }
  610. /* We must increment writecount here, and not at the
  611. * end of the loop. Otherwise, the final loop iteration may
  612. * be skipped, leading to incomplete printer output.
  613. */
  614. writecount += transfer_length;
  615. if (writecount == count) {
  616. mutex_unlock(&usblp->mut);
  617. break;
  618. }
  619. transfer_length=(count - writecount);
  620. if (transfer_length > USBLP_BUF_SIZE)
  621. transfer_length = USBLP_BUF_SIZE;
  622. usblp->writeurb->transfer_buffer_length = transfer_length;
  623. if (copy_from_user(usblp->writeurb->transfer_buffer,
  624. buffer + writecount, transfer_length)) {
  625. mutex_unlock(&usblp->mut);
  626. return writecount ? writecount : -EFAULT;
  627. }
  628. usblp->writeurb->dev = usblp->dev;
  629. usblp->wcomplete = 0;
  630. err = usb_submit_urb(usblp->writeurb, GFP_KERNEL);
  631. if (err) {
  632. usblp->wcomplete = 1;
  633. if (err != -ENOMEM)
  634. count = -EIO;
  635. else
  636. count = writecount ? writecount : -ENOMEM;
  637. mutex_unlock (&usblp->mut);
  638. break;
  639. }
  640. mutex_unlock (&usblp->mut);
  641. }
  642. return count;
  643. }
  644. static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  645. {
  646. struct usblp *usblp = file->private_data;
  647. int rv, intr;
  648. if (!usblp->bidir)
  649. return -EINVAL;
  650. intr = mutex_lock_interruptible (&usblp->mut);
  651. if (intr)
  652. return -EINTR;
  653. if (!usblp->present) {
  654. count = -ENODEV;
  655. goto done;
  656. }
  657. if (!usblp->rcomplete) {
  658. barrier();
  659. if (file->f_flags & O_NONBLOCK) {
  660. count = -EAGAIN;
  661. goto done;
  662. }
  663. mutex_unlock(&usblp->mut);
  664. rv = wait_event_interruptible(usblp->wait, usblp->rcomplete || !usblp->present);
  665. mutex_lock(&usblp->mut);
  666. if (rv < 0) {
  667. count = -EINTR;
  668. goto done;
  669. }
  670. }
  671. if (!usblp->present) {
  672. count = -ENODEV;
  673. goto done;
  674. }
  675. if (usblp->sleeping) {
  676. count = -ENODEV;
  677. goto done;
  678. }
  679. if (usblp->readurb->status) {
  680. err("usblp%d: error %d reading from printer",
  681. usblp->minor, usblp->readurb->status);
  682. usblp->readurb->dev = usblp->dev;
  683. usblp->readcount = 0;
  684. usblp->rcomplete = 0;
  685. if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0)
  686. dbg("error submitting urb");
  687. count = -EIO;
  688. goto done;
  689. }
  690. count = count < usblp->readurb->actual_length - usblp->readcount ?
  691. count : usblp->readurb->actual_length - usblp->readcount;
  692. if (copy_to_user(buffer, usblp->readurb->transfer_buffer + usblp->readcount, count)) {
  693. count = -EFAULT;
  694. goto done;
  695. }
  696. if ((usblp->readcount += count) == usblp->readurb->actual_length) {
  697. usblp->readcount = 0;
  698. usblp->readurb->dev = usblp->dev;
  699. usblp->rcomplete = 0;
  700. if (usb_submit_urb(usblp->readurb, GFP_KERNEL)) {
  701. count = -EIO;
  702. goto done;
  703. }
  704. }
  705. done:
  706. mutex_unlock (&usblp->mut);
  707. return count;
  708. }
  709. /*
  710. * Checks for printers that have quirks, such as requiring unidirectional
  711. * communication but reporting bidirectional; currently some HP printers
  712. * have this flaw (HP 810, 880, 895, etc.), or needing an init string
  713. * sent at each open (like some Epsons).
  714. * Returns 1 if found, 0 if not found.
  715. *
  716. * HP recommended that we use the bidirectional interface but
  717. * don't attempt any bulk IN transfers from the IN endpoint.
  718. * Here's some more detail on the problem:
  719. * The problem is not that it isn't bidirectional though. The problem
  720. * is that if you request a device ID, or status information, while
  721. * the buffers are full, the return data will end up in the print data
  722. * buffer. For example if you make sure you never request the device ID
  723. * while you are sending print data, and you don't try to query the
  724. * printer status every couple of milliseconds, you will probably be OK.
  725. */
  726. static unsigned int usblp_quirks (__u16 vendor, __u16 product)
  727. {
  728. int i;
  729. for (i = 0; quirk_printers[i].vendorId; i++) {
  730. if (vendor == quirk_printers[i].vendorId &&
  731. product == quirk_printers[i].productId)
  732. return quirk_printers[i].quirks;
  733. }
  734. return 0;
  735. }
  736. static const struct file_operations usblp_fops = {
  737. .owner = THIS_MODULE,
  738. .read = usblp_read,
  739. .write = usblp_write,
  740. .poll = usblp_poll,
  741. .unlocked_ioctl = usblp_ioctl,
  742. .compat_ioctl = usblp_ioctl,
  743. .open = usblp_open,
  744. .release = usblp_release,
  745. };
  746. static struct usb_class_driver usblp_class = {
  747. .name = "lp%d",
  748. .fops = &usblp_fops,
  749. .minor_base = USBLP_MINOR_BASE,
  750. };
  751. static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf)
  752. {
  753. struct usb_interface *intf = to_usb_interface(dev);
  754. struct usblp *usblp = usb_get_intfdata (intf);
  755. if (usblp->device_id_string[0] == 0 &&
  756. usblp->device_id_string[1] == 0)
  757. return 0;
  758. return sprintf(buf, "%s", usblp->device_id_string+2);
  759. }
  760. static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL);
  761. static int usblp_probe(struct usb_interface *intf,
  762. const struct usb_device_id *id)
  763. {
  764. struct usb_device *dev = interface_to_usbdev (intf);
  765. struct usblp *usblp = NULL;
  766. int protocol;
  767. int retval;
  768. /* Malloc and start initializing usblp structure so we can use it
  769. * directly. */
  770. if (!(usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL))) {
  771. err("out of memory for usblp");
  772. goto abort;
  773. }
  774. usblp->dev = dev;
  775. mutex_init (&usblp->mut);
  776. init_waitqueue_head(&usblp->wait);
  777. usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  778. usblp->intf = intf;
  779. usblp->writeurb = usb_alloc_urb(0, GFP_KERNEL);
  780. if (!usblp->writeurb) {
  781. err("out of memory");
  782. goto abort;
  783. }
  784. usblp->readurb = usb_alloc_urb(0, GFP_KERNEL);
  785. if (!usblp->readurb) {
  786. err("out of memory");
  787. goto abort;
  788. }
  789. /* Malloc device ID string buffer to the largest expected length,
  790. * since we can re-query it on an ioctl and a dynamic string
  791. * could change in length. */
  792. if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
  793. err("out of memory for device_id_string");
  794. goto abort;
  795. }
  796. usblp->writebuf = usblp->readbuf = NULL;
  797. usblp->writeurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  798. usblp->readurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  799. /* Malloc write & read buffers. We somewhat wastefully
  800. * malloc both regardless of bidirectionality, because the
  801. * alternate setting can be changed later via an ioctl. */
  802. if (!(usblp->writebuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
  803. GFP_KERNEL, &usblp->writeurb->transfer_dma))) {
  804. err("out of memory for write buf");
  805. goto abort;
  806. }
  807. if (!(usblp->readbuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
  808. GFP_KERNEL, &usblp->readurb->transfer_dma))) {
  809. err("out of memory for read buf");
  810. goto abort;
  811. }
  812. /* Allocate buffer for printer status */
  813. usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
  814. if (!usblp->statusbuf) {
  815. err("out of memory for statusbuf");
  816. goto abort;
  817. }
  818. /* Lookup quirks for this printer. */
  819. usblp->quirks = usblp_quirks(
  820. le16_to_cpu(dev->descriptor.idVendor),
  821. le16_to_cpu(dev->descriptor.idProduct));
  822. /* Analyze and pick initial alternate settings and endpoints. */
  823. protocol = usblp_select_alts(usblp);
  824. if (protocol < 0) {
  825. dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
  826. le16_to_cpu(dev->descriptor.idVendor),
  827. le16_to_cpu(dev->descriptor.idProduct));
  828. goto abort;
  829. }
  830. /* Setup the selected alternate setting and endpoints. */
  831. if (usblp_set_protocol(usblp, protocol) < 0)
  832. goto abort;
  833. /* Retrieve and store the device ID string. */
  834. usblp_cache_device_id_string(usblp);
  835. retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id);
  836. if (retval)
  837. goto abort_intfdata;
  838. #ifdef DEBUG
  839. usblp_check_status(usblp, 0);
  840. #endif
  841. usb_set_intfdata (intf, usblp);
  842. usblp->present = 1;
  843. retval = usb_register_dev(intf, &usblp_class);
  844. if (retval) {
  845. err("Not able to get a minor for this device.");
  846. goto abort_intfdata;
  847. }
  848. usblp->minor = intf->minor;
  849. info("usblp%d: USB %sdirectional printer dev %d "
  850. "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
  851. usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
  852. usblp->ifnum,
  853. usblp->protocol[usblp->current_protocol].alt_setting,
  854. usblp->current_protocol,
  855. le16_to_cpu(usblp->dev->descriptor.idVendor),
  856. le16_to_cpu(usblp->dev->descriptor.idProduct));
  857. return 0;
  858. abort_intfdata:
  859. usb_set_intfdata (intf, NULL);
  860. device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
  861. abort:
  862. if (usblp) {
  863. if (usblp->writebuf)
  864. usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
  865. usblp->writebuf, usblp->writeurb->transfer_dma);
  866. if (usblp->readbuf)
  867. usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
  868. usblp->readbuf, usblp->writeurb->transfer_dma);
  869. kfree(usblp->statusbuf);
  870. kfree(usblp->device_id_string);
  871. usb_free_urb(usblp->writeurb);
  872. usb_free_urb(usblp->readurb);
  873. kfree(usblp);
  874. }
  875. return -EIO;
  876. }
  877. /*
  878. * We are a "new" style driver with usb_device_id table,
  879. * but our requirements are too intricate for simple match to handle.
  880. *
  881. * The "proto_bias" option may be used to specify the preferred protocol
  882. * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3). If the device
  883. * supports the preferred protocol, then we bind to it.
  884. *
  885. * The best interface for us is 7/1/2, because it is compatible
  886. * with a stream of characters. If we find it, we bind to it.
  887. *
  888. * Note that the people from hpoj.sourceforge.net need to be able to
  889. * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose.
  890. *
  891. * Failing 7/1/2, we look for 7/1/3, even though it's probably not
  892. * stream-compatible, because this matches the behaviour of the old code.
  893. *
  894. * If nothing else, we bind to 7/1/1 - the unidirectional interface.
  895. */
  896. static int usblp_select_alts(struct usblp *usblp)
  897. {
  898. struct usb_interface *if_alt;
  899. struct usb_host_interface *ifd;
  900. struct usb_endpoint_descriptor *epd, *epwrite, *epread;
  901. int p, i, e;
  902. if_alt = usblp->intf;
  903. for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
  904. usblp->protocol[p].alt_setting = -1;
  905. /* Find out what we have. */
  906. for (i = 0; i < if_alt->num_altsetting; i++) {
  907. ifd = &if_alt->altsetting[i];
  908. if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)
  909. if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS))
  910. continue;
  911. if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
  912. ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
  913. continue;
  914. /* Look for bulk OUT and IN endpoints. */
  915. epwrite = epread = NULL;
  916. for (e = 0; e < ifd->desc.bNumEndpoints; e++) {
  917. epd = &ifd->endpoint[e].desc;
  918. if (usb_endpoint_is_bulk_out(epd))
  919. if (!epwrite)
  920. epwrite = epd;
  921. if (usb_endpoint_is_bulk_in(epd))
  922. if (!epread)
  923. epread = epd;
  924. }
  925. /* Ignore buggy hardware without the right endpoints. */
  926. if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread))
  927. continue;
  928. /* Turn off reads for 7/1/1 (unidirectional) interfaces
  929. * and buggy bidirectional printers. */
  930. if (ifd->desc.bInterfaceProtocol == 1) {
  931. epread = NULL;
  932. } else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
  933. info("Disabling reads from problem bidirectional "
  934. "printer on usblp%d", usblp->minor);
  935. epread = NULL;
  936. }
  937. usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
  938. ifd->desc.bAlternateSetting;
  939. usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
  940. usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
  941. }
  942. /* If our requested protocol is supported, then use it. */
  943. if (proto_bias >= USBLP_FIRST_PROTOCOL &&
  944. proto_bias <= USBLP_LAST_PROTOCOL &&
  945. usblp->protocol[proto_bias].alt_setting != -1)
  946. return proto_bias;
  947. /* Ordering is important here. */
  948. if (usblp->protocol[2].alt_setting != -1)
  949. return 2;
  950. if (usblp->protocol[1].alt_setting != -1)
  951. return 1;
  952. if (usblp->protocol[3].alt_setting != -1)
  953. return 3;
  954. /* If nothing is available, then don't bind to this device. */
  955. return -1;
  956. }
  957. static int usblp_set_protocol(struct usblp *usblp, int protocol)
  958. {
  959. int r, alts;
  960. if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
  961. return -EINVAL;
  962. alts = usblp->protocol[protocol].alt_setting;
  963. if (alts < 0)
  964. return -EINVAL;
  965. r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
  966. if (r < 0) {
  967. err("can't set desired altsetting %d on interface %d",
  968. alts, usblp->ifnum);
  969. return r;
  970. }
  971. usb_fill_bulk_urb(usblp->writeurb, usblp->dev,
  972. usb_sndbulkpipe(usblp->dev,
  973. usblp->protocol[protocol].epwrite->bEndpointAddress),
  974. usblp->writebuf, 0,
  975. usblp_bulk_write, usblp);
  976. usblp->bidir = (usblp->protocol[protocol].epread != NULL);
  977. if (usblp->bidir)
  978. usb_fill_bulk_urb(usblp->readurb, usblp->dev,
  979. usb_rcvbulkpipe(usblp->dev,
  980. usblp->protocol[protocol].epread->bEndpointAddress),
  981. usblp->readbuf, USBLP_BUF_SIZE,
  982. usblp_bulk_read, usblp);
  983. usblp->current_protocol = protocol;
  984. dbg("usblp%d set protocol %d", usblp->minor, protocol);
  985. return 0;
  986. }
  987. /* Retrieves and caches device ID string.
  988. * Returns length, including length bytes but not null terminator.
  989. * On error, returns a negative errno value. */
  990. static int usblp_cache_device_id_string(struct usblp *usblp)
  991. {
  992. int err, length;
  993. err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
  994. if (err < 0) {
  995. dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
  996. usblp->minor, err);
  997. usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
  998. return -EIO;
  999. }
  1000. /* First two bytes are length in big-endian.
  1001. * They count themselves, and we copy them into
  1002. * the user's buffer. */
  1003. length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
  1004. if (length < 2)
  1005. length = 2;
  1006. else if (length >= USBLP_DEVICE_ID_SIZE)
  1007. length = USBLP_DEVICE_ID_SIZE - 1;
  1008. usblp->device_id_string[length] = '\0';
  1009. dbg("usblp%d Device ID string [len=%d]=\"%s\"",
  1010. usblp->minor, length, &usblp->device_id_string[2]);
  1011. return length;
  1012. }
  1013. static void usblp_disconnect(struct usb_interface *intf)
  1014. {
  1015. struct usblp *usblp = usb_get_intfdata (intf);
  1016. usb_deregister_dev(intf, &usblp_class);
  1017. if (!usblp || !usblp->dev) {
  1018. err("bogus disconnect");
  1019. BUG ();
  1020. }
  1021. device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
  1022. mutex_lock (&usblp_mutex);
  1023. mutex_lock (&usblp->mut);
  1024. usblp->present = 0;
  1025. usb_set_intfdata (intf, NULL);
  1026. usblp_unlink_urbs(usblp);
  1027. usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
  1028. usblp->writebuf, usblp->writeurb->transfer_dma);
  1029. usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
  1030. usblp->readbuf, usblp->readurb->transfer_dma);
  1031. mutex_unlock (&usblp->mut);
  1032. if (!usblp->used)
  1033. usblp_cleanup (usblp);
  1034. mutex_unlock (&usblp_mutex);
  1035. }
  1036. static int usblp_suspend (struct usb_interface *intf, pm_message_t message)
  1037. {
  1038. struct usblp *usblp = usb_get_intfdata (intf);
  1039. /* we take no more IO */
  1040. usblp->sleeping = 1;
  1041. usblp_unlink_urbs(usblp);
  1042. return 0;
  1043. }
  1044. static int usblp_resume (struct usb_interface *intf)
  1045. {
  1046. struct usblp *usblp = usb_get_intfdata (intf);
  1047. int r;
  1048. usblp->sleeping = 0;
  1049. r = handle_bidir (usblp);
  1050. return r;
  1051. }
  1052. static struct usb_device_id usblp_ids [] = {
  1053. { USB_DEVICE_INFO(7, 1, 1) },
  1054. { USB_DEVICE_INFO(7, 1, 2) },
  1055. { USB_DEVICE_INFO(7, 1, 3) },
  1056. { USB_INTERFACE_INFO(7, 1, 1) },
  1057. { USB_INTERFACE_INFO(7, 1, 2) },
  1058. { USB_INTERFACE_INFO(7, 1, 3) },
  1059. { USB_DEVICE(0x04b8, 0x0202) }, /* Seiko Epson Receipt Printer M129C */
  1060. { } /* Terminating entry */
  1061. };
  1062. MODULE_DEVICE_TABLE (usb, usblp_ids);
  1063. static struct usb_driver usblp_driver = {
  1064. .name = "usblp",
  1065. .probe = usblp_probe,
  1066. .disconnect = usblp_disconnect,
  1067. .suspend = usblp_suspend,
  1068. .resume = usblp_resume,
  1069. .id_table = usblp_ids,
  1070. .supports_autosuspend = 1,
  1071. };
  1072. static int __init usblp_init(void)
  1073. {
  1074. int retval;
  1075. retval = usb_register(&usblp_driver);
  1076. if (!retval)
  1077. info(DRIVER_VERSION ": " DRIVER_DESC);
  1078. return retval;
  1079. }
  1080. static void __exit usblp_exit(void)
  1081. {
  1082. usb_deregister(&usblp_driver);
  1083. }
  1084. module_init(usblp_init);
  1085. module_exit(usblp_exit);
  1086. MODULE_AUTHOR( DRIVER_AUTHOR );
  1087. MODULE_DESCRIPTION( DRIVER_DESC );
  1088. module_param(proto_bias, int, S_IRUGO | S_IWUSR);
  1089. MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
  1090. MODULE_LICENSE("GPL");