usblp.c 33 KB

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