usblp.c 34 KB

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