usblp.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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. *
  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 const 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 const 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. if (printk_ratelimit())
  275. err("usblp%d: error %d reading printer status",
  276. usblp->minor, error);
  277. return 0;
  278. }
  279. status = *usblp->statusbuf;
  280. if (~status & LP_PERRORP)
  281. newerr = 3;
  282. if (status & LP_POUTPA)
  283. newerr = 1;
  284. if (~status & LP_PSELECD)
  285. newerr = 2;
  286. if (newerr != err)
  287. info("usblp%d: %s", usblp->minor, usblp_messages[newerr]);
  288. return newerr;
  289. }
  290. /*
  291. * File op functions.
  292. */
  293. static int usblp_open(struct inode *inode, struct file *file)
  294. {
  295. int minor = iminor(inode);
  296. struct usblp *usblp;
  297. struct usb_interface *intf;
  298. int retval;
  299. if (minor < 0)
  300. return -ENODEV;
  301. down (&usblp_sem);
  302. retval = -ENODEV;
  303. intf = usb_find_interface(&usblp_driver, minor);
  304. if (!intf) {
  305. goto out;
  306. }
  307. usblp = usb_get_intfdata (intf);
  308. if (!usblp || !usblp->dev || !usblp->present)
  309. goto out;
  310. retval = -EBUSY;
  311. if (usblp->used)
  312. goto out;
  313. /*
  314. * TODO: need to implement LP_ABORTOPEN + O_NONBLOCK as in drivers/char/lp.c ???
  315. * This is #if 0-ed because we *don't* want to fail an open
  316. * just because the printer is off-line.
  317. */
  318. #if 0
  319. if ((retval = usblp_check_status(usblp, 0))) {
  320. retval = retval > 1 ? -EIO : -ENOSPC;
  321. goto out;
  322. }
  323. #else
  324. retval = 0;
  325. #endif
  326. usblp->used = 1;
  327. file->private_data = usblp;
  328. usblp->writeurb->transfer_buffer_length = 0;
  329. usblp->wcomplete = 1; /* we begin writeable */
  330. usblp->rcomplete = 0;
  331. usblp->writeurb->status = 0;
  332. usblp->readurb->status = 0;
  333. if (usblp->bidir) {
  334. usblp->readcount = 0;
  335. usblp->readurb->dev = usblp->dev;
  336. if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0) {
  337. retval = -EIO;
  338. usblp->used = 0;
  339. file->private_data = NULL;
  340. }
  341. }
  342. out:
  343. up (&usblp_sem);
  344. return retval;
  345. }
  346. static void usblp_cleanup (struct usblp *usblp)
  347. {
  348. info("usblp%d: removed", usblp->minor);
  349. kfree (usblp->device_id_string);
  350. kfree (usblp->statusbuf);
  351. usb_free_urb(usblp->writeurb);
  352. usb_free_urb(usblp->readurb);
  353. kfree (usblp);
  354. }
  355. static void usblp_unlink_urbs(struct usblp *usblp)
  356. {
  357. usb_kill_urb(usblp->writeurb);
  358. if (usblp->bidir)
  359. usb_kill_urb(usblp->readurb);
  360. }
  361. static int usblp_release(struct inode *inode, struct file *file)
  362. {
  363. struct usblp *usblp = file->private_data;
  364. down (&usblp_sem);
  365. usblp->used = 0;
  366. if (usblp->present) {
  367. usblp_unlink_urbs(usblp);
  368. } else /* finish cleanup from disconnect */
  369. usblp_cleanup (usblp);
  370. up (&usblp_sem);
  371. return 0;
  372. }
  373. /* No kernel lock - fine */
  374. static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
  375. {
  376. struct usblp *usblp = file->private_data;
  377. poll_wait(file, &usblp->wait, wait);
  378. return ((!usblp->bidir || !usblp->rcomplete) ? 0 : POLLIN | POLLRDNORM)
  379. | (!usblp->wcomplete ? 0 : POLLOUT | POLLWRNORM);
  380. }
  381. static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  382. {
  383. struct usblp *usblp = file->private_data;
  384. int length, err, i;
  385. unsigned char newChannel;
  386. int status;
  387. int twoints[2];
  388. int retval = 0;
  389. down (&usblp->sem);
  390. if (!usblp->present) {
  391. retval = -ENODEV;
  392. goto done;
  393. }
  394. dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd),
  395. _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd) );
  396. if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */
  397. switch (_IOC_NR(cmd)) {
  398. case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
  399. if (_IOC_DIR(cmd) != _IOC_READ) {
  400. retval = -EINVAL;
  401. goto done;
  402. }
  403. length = usblp_cache_device_id_string(usblp);
  404. if (length < 0) {
  405. retval = length;
  406. goto done;
  407. }
  408. if (length > _IOC_SIZE(cmd))
  409. length = _IOC_SIZE(cmd); /* truncate */
  410. if (copy_to_user((void __user *) arg,
  411. usblp->device_id_string,
  412. (unsigned long) length)) {
  413. retval = -EFAULT;
  414. goto done;
  415. }
  416. break;
  417. case IOCNR_GET_PROTOCOLS:
  418. if (_IOC_DIR(cmd) != _IOC_READ ||
  419. _IOC_SIZE(cmd) < sizeof(twoints)) {
  420. retval = -EINVAL;
  421. goto done;
  422. }
  423. twoints[0] = usblp->current_protocol;
  424. twoints[1] = 0;
  425. for (i = USBLP_FIRST_PROTOCOL;
  426. i <= USBLP_LAST_PROTOCOL; i++) {
  427. if (usblp->protocol[i].alt_setting >= 0)
  428. twoints[1] |= (1<<i);
  429. }
  430. if (copy_to_user((void __user *)arg,
  431. (unsigned char *)twoints,
  432. sizeof(twoints))) {
  433. retval = -EFAULT;
  434. goto done;
  435. }
  436. break;
  437. case IOCNR_SET_PROTOCOL:
  438. if (_IOC_DIR(cmd) != _IOC_WRITE) {
  439. retval = -EINVAL;
  440. goto done;
  441. }
  442. #ifdef DEBUG
  443. if (arg == -10) {
  444. usblp_dump(usblp);
  445. break;
  446. }
  447. #endif
  448. usblp_unlink_urbs(usblp);
  449. retval = usblp_set_protocol(usblp, arg);
  450. if (retval < 0) {
  451. usblp_set_protocol(usblp,
  452. usblp->current_protocol);
  453. }
  454. break;
  455. case IOCNR_HP_SET_CHANNEL:
  456. if (_IOC_DIR(cmd) != _IOC_WRITE ||
  457. le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
  458. usblp->quirks & USBLP_QUIRK_BIDIR) {
  459. retval = -EINVAL;
  460. goto done;
  461. }
  462. err = usblp_hp_channel_change_request(usblp,
  463. arg, &newChannel);
  464. if (err < 0) {
  465. err("usblp%d: error = %d setting "
  466. "HP channel",
  467. usblp->minor, err);
  468. retval = -EIO;
  469. goto done;
  470. }
  471. dbg("usblp%d requested/got HP channel %ld/%d",
  472. usblp->minor, arg, newChannel);
  473. break;
  474. case IOCNR_GET_BUS_ADDRESS:
  475. if (_IOC_DIR(cmd) != _IOC_READ ||
  476. _IOC_SIZE(cmd) < sizeof(twoints)) {
  477. retval = -EINVAL;
  478. goto done;
  479. }
  480. twoints[0] = usblp->dev->bus->busnum;
  481. twoints[1] = usblp->dev->devnum;
  482. if (copy_to_user((void __user *)arg,
  483. (unsigned char *)twoints,
  484. sizeof(twoints))) {
  485. retval = -EFAULT;
  486. goto done;
  487. }
  488. dbg("usblp%d is bus=%d, device=%d",
  489. usblp->minor, twoints[0], twoints[1]);
  490. break;
  491. case IOCNR_GET_VID_PID:
  492. if (_IOC_DIR(cmd) != _IOC_READ ||
  493. _IOC_SIZE(cmd) < sizeof(twoints)) {
  494. retval = -EINVAL;
  495. goto done;
  496. }
  497. twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
  498. twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
  499. if (copy_to_user((void __user *)arg,
  500. (unsigned char *)twoints,
  501. sizeof(twoints))) {
  502. retval = -EFAULT;
  503. goto done;
  504. }
  505. dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
  506. usblp->minor, twoints[0], twoints[1]);
  507. break;
  508. case IOCNR_SOFT_RESET:
  509. if (_IOC_DIR(cmd) != _IOC_NONE) {
  510. retval = -EINVAL;
  511. goto done;
  512. }
  513. retval = usblp_reset(usblp);
  514. break;
  515. default:
  516. retval = -ENOTTY;
  517. }
  518. else /* old-style ioctl value */
  519. switch (cmd) {
  520. case LPGETSTATUS:
  521. if (usblp_read_status(usblp, usblp->statusbuf)) {
  522. if (printk_ratelimit())
  523. err("usblp%d: failed reading printer status",
  524. usblp->minor);
  525. retval = -EIO;
  526. goto done;
  527. }
  528. status = *usblp->statusbuf;
  529. if (copy_to_user ((void __user *)arg, &status, sizeof(int)))
  530. retval = -EFAULT;
  531. break;
  532. default:
  533. retval = -ENOTTY;
  534. }
  535. done:
  536. up (&usblp->sem);
  537. return retval;
  538. }
  539. static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  540. {
  541. DECLARE_WAITQUEUE(wait, current);
  542. struct usblp *usblp = file->private_data;
  543. int timeout, err = 0, transfer_length = 0;
  544. size_t writecount = 0;
  545. while (writecount < count) {
  546. if (!usblp->wcomplete) {
  547. barrier();
  548. if (file->f_flags & O_NONBLOCK) {
  549. writecount += transfer_length;
  550. return writecount ? writecount : -EAGAIN;
  551. }
  552. timeout = USBLP_WRITE_TIMEOUT;
  553. add_wait_queue(&usblp->wait, &wait);
  554. while ( 1==1 ) {
  555. if (signal_pending(current)) {
  556. remove_wait_queue(&usblp->wait, &wait);
  557. return writecount ? writecount : -EINTR;
  558. }
  559. set_current_state(TASK_INTERRUPTIBLE);
  560. if (timeout && !usblp->wcomplete) {
  561. timeout = schedule_timeout(timeout);
  562. } else {
  563. set_current_state(TASK_RUNNING);
  564. break;
  565. }
  566. }
  567. remove_wait_queue(&usblp->wait, &wait);
  568. }
  569. down (&usblp->sem);
  570. if (!usblp->present) {
  571. up (&usblp->sem);
  572. return -ENODEV;
  573. }
  574. if (usblp->writeurb->status != 0) {
  575. if (usblp->quirks & USBLP_QUIRK_BIDIR) {
  576. if (!usblp->wcomplete)
  577. err("usblp%d: error %d writing to printer",
  578. usblp->minor, usblp->writeurb->status);
  579. err = usblp->writeurb->status;
  580. } else
  581. err = usblp_check_status(usblp, err);
  582. up (&usblp->sem);
  583. /* if the fault was due to disconnect, let khubd's
  584. * call to usblp_disconnect() grab usblp->sem ...
  585. */
  586. schedule ();
  587. continue;
  588. }
  589. /* We must increment writecount here, and not at the
  590. * end of the loop. Otherwise, the final loop iteration may
  591. * be skipped, leading to incomplete printer output.
  592. */
  593. writecount += transfer_length;
  594. if (writecount == count) {
  595. up(&usblp->sem);
  596. break;
  597. }
  598. transfer_length=(count - writecount);
  599. if (transfer_length > USBLP_BUF_SIZE)
  600. transfer_length = USBLP_BUF_SIZE;
  601. usblp->writeurb->transfer_buffer_length = transfer_length;
  602. if (copy_from_user(usblp->writeurb->transfer_buffer,
  603. buffer + writecount, transfer_length)) {
  604. up(&usblp->sem);
  605. return writecount ? writecount : -EFAULT;
  606. }
  607. usblp->writeurb->dev = usblp->dev;
  608. usblp->wcomplete = 0;
  609. err = usb_submit_urb(usblp->writeurb, GFP_KERNEL);
  610. if (err) {
  611. if (err != -ENOMEM)
  612. count = -EIO;
  613. else
  614. count = writecount ? writecount : -ENOMEM;
  615. up (&usblp->sem);
  616. break;
  617. }
  618. up (&usblp->sem);
  619. }
  620. return count;
  621. }
  622. static ssize_t usblp_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  623. {
  624. struct usblp *usblp = file->private_data;
  625. DECLARE_WAITQUEUE(wait, current);
  626. if (!usblp->bidir)
  627. return -EINVAL;
  628. down (&usblp->sem);
  629. if (!usblp->present) {
  630. count = -ENODEV;
  631. goto done;
  632. }
  633. if (!usblp->rcomplete) {
  634. barrier();
  635. if (file->f_flags & O_NONBLOCK) {
  636. count = -EAGAIN;
  637. goto done;
  638. }
  639. add_wait_queue(&usblp->wait, &wait);
  640. while (1==1) {
  641. if (signal_pending(current)) {
  642. count = -EINTR;
  643. remove_wait_queue(&usblp->wait, &wait);
  644. goto done;
  645. }
  646. up (&usblp->sem);
  647. set_current_state(TASK_INTERRUPTIBLE);
  648. if (!usblp->rcomplete) {
  649. schedule();
  650. } else {
  651. set_current_state(TASK_RUNNING);
  652. down(&usblp->sem);
  653. break;
  654. }
  655. down (&usblp->sem);
  656. }
  657. remove_wait_queue(&usblp->wait, &wait);
  658. }
  659. if (!usblp->present) {
  660. count = -ENODEV;
  661. goto done;
  662. }
  663. if (usblp->readurb->status) {
  664. err("usblp%d: error %d reading from printer",
  665. usblp->minor, usblp->readurb->status);
  666. usblp->readurb->dev = usblp->dev;
  667. usblp->readcount = 0;
  668. usblp->rcomplete = 0;
  669. if (usb_submit_urb(usblp->readurb, GFP_KERNEL) < 0)
  670. dbg("error submitting urb");
  671. count = -EIO;
  672. goto done;
  673. }
  674. count = count < usblp->readurb->actual_length - usblp->readcount ?
  675. count : usblp->readurb->actual_length - usblp->readcount;
  676. if (copy_to_user(buffer, usblp->readurb->transfer_buffer + usblp->readcount, count)) {
  677. count = -EFAULT;
  678. goto done;
  679. }
  680. if ((usblp->readcount += count) == usblp->readurb->actual_length) {
  681. usblp->readcount = 0;
  682. usblp->readurb->dev = usblp->dev;
  683. usblp->rcomplete = 0;
  684. if (usb_submit_urb(usblp->readurb, GFP_KERNEL)) {
  685. count = -EIO;
  686. goto done;
  687. }
  688. }
  689. done:
  690. up (&usblp->sem);
  691. return count;
  692. }
  693. /*
  694. * Checks for printers that have quirks, such as requiring unidirectional
  695. * communication but reporting bidirectional; currently some HP printers
  696. * have this flaw (HP 810, 880, 895, etc.), or needing an init string
  697. * sent at each open (like some Epsons).
  698. * Returns 1 if found, 0 if not found.
  699. *
  700. * HP recommended that we use the bidirectional interface but
  701. * don't attempt any bulk IN transfers from the IN endpoint.
  702. * Here's some more detail on the problem:
  703. * The problem is not that it isn't bidirectional though. The problem
  704. * is that if you request a device ID, or status information, while
  705. * the buffers are full, the return data will end up in the print data
  706. * buffer. For example if you make sure you never request the device ID
  707. * while you are sending print data, and you don't try to query the
  708. * printer status every couple of milliseconds, you will probably be OK.
  709. */
  710. static unsigned int usblp_quirks (__u16 vendor, __u16 product)
  711. {
  712. int i;
  713. for (i = 0; quirk_printers[i].vendorId; i++) {
  714. if (vendor == quirk_printers[i].vendorId &&
  715. product == quirk_printers[i].productId)
  716. return quirk_printers[i].quirks;
  717. }
  718. return 0;
  719. }
  720. static struct file_operations usblp_fops = {
  721. .owner = THIS_MODULE,
  722. .read = usblp_read,
  723. .write = usblp_write,
  724. .poll = usblp_poll,
  725. .unlocked_ioctl = usblp_ioctl,
  726. .compat_ioctl = usblp_ioctl,
  727. .open = usblp_open,
  728. .release = usblp_release,
  729. };
  730. static struct usb_class_driver usblp_class = {
  731. .name = "lp%d",
  732. .fops = &usblp_fops,
  733. .minor_base = USBLP_MINOR_BASE,
  734. };
  735. static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf)
  736. {
  737. struct usb_interface *intf = to_usb_interface(dev);
  738. struct usblp *usblp = usb_get_intfdata (intf);
  739. if (usblp->device_id_string[0] == 0 &&
  740. usblp->device_id_string[1] == 0)
  741. return 0;
  742. return sprintf(buf, "%s", usblp->device_id_string+2);
  743. }
  744. static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL);
  745. static int usblp_probe(struct usb_interface *intf,
  746. const struct usb_device_id *id)
  747. {
  748. struct usb_device *dev = interface_to_usbdev (intf);
  749. struct usblp *usblp = NULL;
  750. int protocol;
  751. int retval;
  752. /* Malloc and start initializing usblp structure so we can use it
  753. * directly. */
  754. if (!(usblp = kmalloc(sizeof(struct usblp), GFP_KERNEL))) {
  755. err("out of memory for usblp");
  756. goto abort;
  757. }
  758. memset(usblp, 0, sizeof(struct usblp));
  759. usblp->dev = dev;
  760. init_MUTEX (&usblp->sem);
  761. init_waitqueue_head(&usblp->wait);
  762. usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
  763. usblp->intf = intf;
  764. usblp->writeurb = usb_alloc_urb(0, GFP_KERNEL);
  765. if (!usblp->writeurb) {
  766. err("out of memory");
  767. goto abort;
  768. }
  769. usblp->readurb = usb_alloc_urb(0, GFP_KERNEL);
  770. if (!usblp->readurb) {
  771. err("out of memory");
  772. goto abort;
  773. }
  774. /* Malloc device ID string buffer to the largest expected length,
  775. * since we can re-query it on an ioctl and a dynamic string
  776. * could change in length. */
  777. if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
  778. err("out of memory for device_id_string");
  779. goto abort;
  780. }
  781. usblp->writebuf = usblp->readbuf = NULL;
  782. usblp->writeurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  783. usblp->readurb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  784. /* Malloc write & read buffers. We somewhat wastefully
  785. * malloc both regardless of bidirectionality, because the
  786. * alternate setting can be changed later via an ioctl. */
  787. if (!(usblp->writebuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
  788. GFP_KERNEL, &usblp->writeurb->transfer_dma))) {
  789. err("out of memory for write buf");
  790. goto abort;
  791. }
  792. if (!(usblp->readbuf = usb_buffer_alloc(dev, USBLP_BUF_SIZE,
  793. GFP_KERNEL, &usblp->readurb->transfer_dma))) {
  794. err("out of memory for read buf");
  795. goto abort;
  796. }
  797. /* Allocate buffer for printer status */
  798. usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
  799. if (!usblp->statusbuf) {
  800. err("out of memory for statusbuf");
  801. goto abort;
  802. }
  803. /* Lookup quirks for this printer. */
  804. usblp->quirks = usblp_quirks(
  805. le16_to_cpu(dev->descriptor.idVendor),
  806. le16_to_cpu(dev->descriptor.idProduct));
  807. /* Analyze and pick initial alternate settings and endpoints. */
  808. protocol = usblp_select_alts(usblp);
  809. if (protocol < 0) {
  810. dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
  811. le16_to_cpu(dev->descriptor.idVendor),
  812. le16_to_cpu(dev->descriptor.idProduct));
  813. goto abort;
  814. }
  815. /* Setup the selected alternate setting and endpoints. */
  816. if (usblp_set_protocol(usblp, protocol) < 0)
  817. goto abort;
  818. /* Retrieve and store the device ID string. */
  819. usblp_cache_device_id_string(usblp);
  820. device_create_file(&intf->dev, &dev_attr_ieee1284_id);
  821. #ifdef DEBUG
  822. usblp_check_status(usblp, 0);
  823. #endif
  824. usb_set_intfdata (intf, usblp);
  825. usblp->present = 1;
  826. retval = usb_register_dev(intf, &usblp_class);
  827. if (retval) {
  828. err("Not able to get a minor for this device.");
  829. goto abort_intfdata;
  830. }
  831. usblp->minor = intf->minor;
  832. info("usblp%d: USB %sdirectional printer dev %d "
  833. "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X",
  834. usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
  835. usblp->ifnum,
  836. usblp->protocol[usblp->current_protocol].alt_setting,
  837. usblp->current_protocol,
  838. le16_to_cpu(usblp->dev->descriptor.idVendor),
  839. le16_to_cpu(usblp->dev->descriptor.idProduct));
  840. return 0;
  841. abort_intfdata:
  842. usb_set_intfdata (intf, NULL);
  843. device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
  844. abort:
  845. if (usblp) {
  846. if (usblp->writebuf)
  847. usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
  848. usblp->writebuf, usblp->writeurb->transfer_dma);
  849. if (usblp->readbuf)
  850. usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
  851. usblp->readbuf, usblp->writeurb->transfer_dma);
  852. kfree(usblp->statusbuf);
  853. kfree(usblp->device_id_string);
  854. usb_free_urb(usblp->writeurb);
  855. usb_free_urb(usblp->readurb);
  856. kfree(usblp);
  857. }
  858. return -EIO;
  859. }
  860. /*
  861. * We are a "new" style driver with usb_device_id table,
  862. * but our requirements are too intricate for simple match to handle.
  863. *
  864. * The "proto_bias" option may be used to specify the preferred protocol
  865. * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3). If the device
  866. * supports the preferred protocol, then we bind to it.
  867. *
  868. * The best interface for us is 7/1/2, because it is compatible
  869. * with a stream of characters. If we find it, we bind to it.
  870. *
  871. * Note that the people from hpoj.sourceforge.net need to be able to
  872. * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose.
  873. *
  874. * Failing 7/1/2, we look for 7/1/3, even though it's probably not
  875. * stream-compatible, because this matches the behaviour of the old code.
  876. *
  877. * If nothing else, we bind to 7/1/1 - the unidirectional interface.
  878. */
  879. static int usblp_select_alts(struct usblp *usblp)
  880. {
  881. struct usb_interface *if_alt;
  882. struct usb_host_interface *ifd;
  883. struct usb_endpoint_descriptor *epd, *epwrite, *epread;
  884. int p, i, e;
  885. if_alt = usblp->intf;
  886. for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
  887. usblp->protocol[p].alt_setting = -1;
  888. /* Find out what we have. */
  889. for (i = 0; i < if_alt->num_altsetting; i++) {
  890. ifd = &if_alt->altsetting[i];
  891. if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)
  892. continue;
  893. if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
  894. ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
  895. continue;
  896. /* Look for bulk OUT and IN endpoints. */
  897. epwrite = epread = NULL;
  898. for (e = 0; e < ifd->desc.bNumEndpoints; e++) {
  899. epd = &ifd->endpoint[e].desc;
  900. if ((epd->bmAttributes&USB_ENDPOINT_XFERTYPE_MASK)!=
  901. USB_ENDPOINT_XFER_BULK)
  902. continue;
  903. if (!(epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK)) {
  904. if (!epwrite)
  905. epwrite = epd;
  906. } else {
  907. if (!epread)
  908. epread = epd;
  909. }
  910. }
  911. /* Ignore buggy hardware without the right endpoints. */
  912. if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread))
  913. continue;
  914. /* Turn off reads for 7/1/1 (unidirectional) interfaces
  915. * and buggy bidirectional printers. */
  916. if (ifd->desc.bInterfaceProtocol == 1) {
  917. epread = NULL;
  918. } else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
  919. info("Disabling reads from problem bidirectional "
  920. "printer on usblp%d", usblp->minor);
  921. epread = NULL;
  922. }
  923. usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
  924. ifd->desc.bAlternateSetting;
  925. usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
  926. usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
  927. }
  928. /* If our requested protocol is supported, then use it. */
  929. if (proto_bias >= USBLP_FIRST_PROTOCOL &&
  930. proto_bias <= USBLP_LAST_PROTOCOL &&
  931. usblp->protocol[proto_bias].alt_setting != -1)
  932. return proto_bias;
  933. /* Ordering is important here. */
  934. if (usblp->protocol[2].alt_setting != -1)
  935. return 2;
  936. if (usblp->protocol[1].alt_setting != -1)
  937. return 1;
  938. if (usblp->protocol[3].alt_setting != -1)
  939. return 3;
  940. /* If nothing is available, then don't bind to this device. */
  941. return -1;
  942. }
  943. static int usblp_set_protocol(struct usblp *usblp, int protocol)
  944. {
  945. int r, alts;
  946. if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
  947. return -EINVAL;
  948. alts = usblp->protocol[protocol].alt_setting;
  949. if (alts < 0)
  950. return -EINVAL;
  951. r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
  952. if (r < 0) {
  953. err("can't set desired altsetting %d on interface %d",
  954. alts, usblp->ifnum);
  955. return r;
  956. }
  957. usb_fill_bulk_urb(usblp->writeurb, usblp->dev,
  958. usb_sndbulkpipe(usblp->dev,
  959. usblp->protocol[protocol].epwrite->bEndpointAddress),
  960. usblp->writebuf, 0,
  961. usblp_bulk_write, usblp);
  962. usblp->bidir = (usblp->protocol[protocol].epread != NULL);
  963. if (usblp->bidir)
  964. usb_fill_bulk_urb(usblp->readurb, usblp->dev,
  965. usb_rcvbulkpipe(usblp->dev,
  966. usblp->protocol[protocol].epread->bEndpointAddress),
  967. usblp->readbuf, USBLP_BUF_SIZE,
  968. usblp_bulk_read, usblp);
  969. usblp->current_protocol = protocol;
  970. dbg("usblp%d set protocol %d", usblp->minor, protocol);
  971. return 0;
  972. }
  973. /* Retrieves and caches device ID string.
  974. * Returns length, including length bytes but not null terminator.
  975. * On error, returns a negative errno value. */
  976. static int usblp_cache_device_id_string(struct usblp *usblp)
  977. {
  978. int err, length;
  979. err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
  980. if (err < 0) {
  981. dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
  982. usblp->minor, err);
  983. usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
  984. return -EIO;
  985. }
  986. /* First two bytes are length in big-endian.
  987. * They count themselves, and we copy them into
  988. * the user's buffer. */
  989. length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
  990. if (length < 2)
  991. length = 2;
  992. else if (length >= USBLP_DEVICE_ID_SIZE)
  993. length = USBLP_DEVICE_ID_SIZE - 1;
  994. usblp->device_id_string[length] = '\0';
  995. dbg("usblp%d Device ID string [len=%d]=\"%s\"",
  996. usblp->minor, length, &usblp->device_id_string[2]);
  997. return length;
  998. }
  999. static void usblp_disconnect(struct usb_interface *intf)
  1000. {
  1001. struct usblp *usblp = usb_get_intfdata (intf);
  1002. usb_deregister_dev(intf, &usblp_class);
  1003. if (!usblp || !usblp->dev) {
  1004. err("bogus disconnect");
  1005. BUG ();
  1006. }
  1007. device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
  1008. down (&usblp_sem);
  1009. down (&usblp->sem);
  1010. usblp->present = 0;
  1011. usb_set_intfdata (intf, NULL);
  1012. usblp_unlink_urbs(usblp);
  1013. usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
  1014. usblp->writebuf, usblp->writeurb->transfer_dma);
  1015. usb_buffer_free (usblp->dev, USBLP_BUF_SIZE,
  1016. usblp->readbuf, usblp->readurb->transfer_dma);
  1017. up (&usblp->sem);
  1018. if (!usblp->used)
  1019. usblp_cleanup (usblp);
  1020. up (&usblp_sem);
  1021. }
  1022. static struct usb_device_id usblp_ids [] = {
  1023. { USB_DEVICE_INFO(7, 1, 1) },
  1024. { USB_DEVICE_INFO(7, 1, 2) },
  1025. { USB_DEVICE_INFO(7, 1, 3) },
  1026. { USB_INTERFACE_INFO(7, 1, 1) },
  1027. { USB_INTERFACE_INFO(7, 1, 2) },
  1028. { USB_INTERFACE_INFO(7, 1, 3) },
  1029. { } /* Terminating entry */
  1030. };
  1031. MODULE_DEVICE_TABLE (usb, usblp_ids);
  1032. static struct usb_driver usblp_driver = {
  1033. .name = "usblp",
  1034. .probe = usblp_probe,
  1035. .disconnect = usblp_disconnect,
  1036. .id_table = usblp_ids,
  1037. };
  1038. static int __init usblp_init(void)
  1039. {
  1040. int retval;
  1041. retval = usb_register(&usblp_driver);
  1042. if (retval)
  1043. goto out;
  1044. info(DRIVER_VERSION ": " DRIVER_DESC);
  1045. out:
  1046. return retval;
  1047. }
  1048. static void __exit usblp_exit(void)
  1049. {
  1050. usb_deregister(&usblp_driver);
  1051. }
  1052. module_init(usblp_init);
  1053. module_exit(usblp_exit);
  1054. MODULE_AUTHOR( DRIVER_AUTHOR );
  1055. MODULE_DESCRIPTION( DRIVER_DESC );
  1056. module_param(proto_bias, int, S_IRUGO | S_IWUSR);
  1057. MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
  1058. MODULE_LICENSE("GPL");