1 /* $OpenBSD: ulpt.c,v 1.59 2024/05/13 01:15:53 jsg Exp $ */
2 /* $NetBSD: ulpt.c,v 1.57 2003/01/05 10:19:42 scw Exp $ */
3 /* $FreeBSD: src/sys/dev/usb/ulpt.c,v 1.24 1999/11/17 22:33:44 n_hibma Exp $ */
4
5 /*
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net) at
11 * Carlstedt Research & Technology.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * Printer Class spec:
37 * https://www.usb.org/sites/default/files/usbprint11a021811.pdf
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/device.h>
43 #include <sys/uio.h>
44 #include <sys/conf.h>
45 #include <sys/vnode.h>
46 #include <sys/syslog.h>
47 #include <sys/malloc.h>
48
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
52 #include <dev/usb/usbdevs.h>
53 #include <dev/usb/usb_quirks.h>
54
55 #define LPTPRI (PZERO+8)
56 #define ULPT_BSIZE 16384
57
58 #ifdef ULPT_DEBUG
59 #define DPRINTF(x) do { if (ulptdebug) printf x; } while (0)
60 #define DPRINTFN(n,x) do { if (ulptdebug>(n)) printf x; } while (0)
61 int ulptdebug = 0;
62 #else
63 #define DPRINTF(x)
64 #define DPRINTFN(n,x)
65 #endif
66
67 #define UR_GET_DEVICE_ID 0
68 #define UR_GET_PORT_STATUS 1
69 #define UR_SOFT_RESET 2
70
71 #define LPS_NERR 0x08 /* printer no error */
72 #define LPS_SELECT 0x10 /* printer selected */
73 #define LPS_NOPAPER 0x20 /* printer out of paper */
74 #define LPS_INVERT (LPS_SELECT|LPS_NERR)
75 #define LPS_MASK (LPS_SELECT|LPS_NERR|LPS_NOPAPER)
76
77 struct ulpt_softc {
78 struct device sc_dev;
79 struct usbd_device *sc_udev; /* device */
80 struct usbd_interface *sc_iface;/* interface */
81 int sc_ifaceno;
82
83 int sc_out;
84 struct usbd_pipe *sc_out_pipe; /* bulk out pipe */
85
86 int sc_in;
87 struct usbd_pipe *sc_in_pipe; /* bulk in pipe */
88 struct usbd_xfer *sc_in_xfer1;
89 struct usbd_xfer *sc_in_xfer2;
90 u_char sc_junk[64]; /* somewhere to dump input */
91
92 u_char sc_state;
93 #define ULPT_OPEN 0x01 /* device is open */
94 #define ULPT_OBUSY 0x02 /* printer is busy doing output */
95 #define ULPT_INIT 0x04 /* waiting to initialize for open */
96 u_char sc_flags;
97 #define ULPT_NOPRIME 0x40 /* don't prime on open */
98 #define ULPT_EFIRMWARE 0x80 /* error loading firmware */
99 u_char sc_laststatus;
100
101 int sc_refcnt;
102
103 struct ulpt_fwdev *sc_fwdev;
104 };
105
106 int ulpt_do_write(struct ulpt_softc *, struct uio *uio, int);
107 int ulpt_status(struct ulpt_softc *);
108 void ulpt_reset(struct ulpt_softc *);
109 int ulpt_statusmsg(u_char, struct ulpt_softc *);
110
111 /*
112 * Printers which need firmware uploads.
113 */
114 void ulpt_load_firmware(struct device *);
115 usbd_status ulpt_ucode_loader_hp(struct ulpt_softc *);
116 struct ulpt_fwdev {
117 struct usb_devno uv_dev;
118 char *ucode_name;
119 usbd_status (*ucode_loader)(struct ulpt_softc *);
120 } ulpt_fwdevs[] = {
121 {
122 { USB_VENDOR_HP, USB_PRODUCT_HP_1000 },
123 "ulpt-hp1000",
124 ulpt_ucode_loader_hp
125 },
126 {
127 { USB_VENDOR_HP, USB_PRODUCT_HP_1005 },
128 "ulpt-hp1005",
129 ulpt_ucode_loader_hp
130 },
131 {
132 { USB_VENDOR_HP, USB_PRODUCT_HP_1018 },
133 "ulpt-hp1018",
134 ulpt_ucode_loader_hp
135 },
136 {
137 { USB_VENDOR_HP, USB_PRODUCT_HP_1020 },
138 "ulpt-hp1020",
139 ulpt_ucode_loader_hp
140 },
141 };
142
143 #if 0
144 void ieee1284_print_id(char *);
145 #endif
146
147 #define ULPTUNIT(s) (minor(s) & 0x1f)
148 #define ULPTFLAGS(s) (minor(s) & 0xe0)
149
150
151 int ulpt_match(struct device *, void *, void *);
152 void ulpt_attach(struct device *, struct device *, void *);
153 int ulpt_detach(struct device *, int);
154
155 struct cfdriver ulpt_cd = {
156 NULL, "ulpt", DV_DULL
157 };
158
159 const struct cfattach ulpt_ca = {
160 sizeof(struct ulpt_softc), ulpt_match, ulpt_attach, ulpt_detach
161 };
162
163 int
ulpt_match(struct device * parent,void * match,void * aux)164 ulpt_match(struct device *parent, void *match, void *aux)
165 {
166 struct usb_attach_arg *uaa = aux;
167 usb_interface_descriptor_t *id;
168
169 DPRINTFN(10,("ulpt_match\n"));
170 if (uaa->iface == NULL)
171 return (UMATCH_NONE);
172 id = usbd_get_interface_descriptor(uaa->iface);
173 if (id != NULL &&
174 id->bInterfaceClass == UICLASS_PRINTER &&
175 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
176 ((id->bInterfaceProtocol == UIPROTO_PRINTER_UNI) ||
177 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI) ||
178 (id->bInterfaceProtocol == UIPROTO_PRINTER_1284)))
179 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
180 return (UMATCH_NONE);
181 }
182
183 void
ulpt_load_firmware(struct device * self)184 ulpt_load_firmware(struct device *self)
185 {
186 struct ulpt_softc *sc = (struct ulpt_softc *)self;
187 usbd_status err;
188
189 err = (sc->sc_fwdev->ucode_loader)(sc);
190 if (err != USBD_NORMAL_COMPLETION) {
191 sc->sc_flags |= ULPT_EFIRMWARE;
192 printf("%s: could not load firmware '%s'\n",
193 sc->sc_dev.dv_xname, sc->sc_fwdev->ucode_name);
194 } else
195 sc->sc_flags &= ~ULPT_EFIRMWARE;
196 }
197
198 #define ulpt_lookup(v, p) \
199 ((struct ulpt_fwdev *)usb_lookup(ulpt_fwdevs, v, p))
200
201 void
ulpt_attach(struct device * parent,struct device * self,void * aux)202 ulpt_attach(struct device *parent, struct device *self, void *aux)
203 {
204 struct ulpt_softc *sc = (struct ulpt_softc *)self;
205 struct usb_attach_arg *uaa = aux;
206 struct usbd_device *dev = uaa->device;
207 struct usbd_interface *iface = uaa->iface;
208 usb_interface_descriptor_t *ifcd = usbd_get_interface_descriptor(iface);
209 usb_interface_descriptor_t *id, *iend;
210 usb_config_descriptor_t *cdesc;
211 usbd_status err;
212 usb_endpoint_descriptor_t *ed;
213 int i, altno;
214
215 DPRINTFN(10,("ulpt_attach: sc=%p\n", sc));
216
217 //printf("%s: iclass %d/%d\n", sc->sc_dev.dv_xname,
218 // ifcd->bInterfaceClass, ifcd->bInterfaceSubClass);
219
220 /* XXX
221 * Stepping through the alternate settings needs to be abstracted out.
222 */
223 cdesc = usbd_get_config_descriptor(dev);
224 if (cdesc == NULL) {
225 printf("%s: failed to get configuration descriptor\n",
226 sc->sc_dev.dv_xname);
227 return;
228 }
229 iend = (usb_interface_descriptor_t *)
230 ((char *)cdesc + UGETW(cdesc->wTotalLength));
231 #ifdef DIAGNOSTIC
232 if (ifcd < (usb_interface_descriptor_t *)cdesc ||
233 ifcd >= iend)
234 panic("ulpt: iface desc out of range");
235 #endif
236 /* Step through all the descriptors looking for bidir mode */
237 for (id = ifcd, altno = 0;
238 id < iend;
239 id = (void *)((char *)id + id->bLength)) {
240 if (id->bDescriptorType == UDESC_INTERFACE &&
241 id->bInterfaceNumber == ifcd->bInterfaceNumber) {
242 if (id->bInterfaceClass == UICLASS_PRINTER &&
243 id->bInterfaceSubClass == UISUBCLASS_PRINTER &&
244 (id->bInterfaceProtocol == UIPROTO_PRINTER_BI /*||
245 id->bInterfaceProtocol == UIPROTO_PRINTER_1284*/))
246 goto found;
247 altno++;
248 }
249 }
250 id = ifcd; /* not found, use original */
251 found:
252 if (id != ifcd) {
253 /* Found a new bidir setting */
254 DPRINTF(("ulpt_attach: set altno = %d\n", altno));
255 err = usbd_set_interface(iface, altno);
256 if (err) {
257 printf("%s: setting alternate interface failed\n",
258 sc->sc_dev.dv_xname);
259 usbd_deactivate(sc->sc_udev);
260 return;
261 }
262 }
263
264
265 sc->sc_in = -1;
266 sc->sc_out = -1;
267
268 id = usbd_get_interface_descriptor(iface);
269 for (i = 0; i < id->bNumEndpoints; i++) {
270 ed = usbd_interface2endpoint_descriptor(iface, i);
271 if (ed == NULL) {
272 printf("%s: couldn't get ep %d\n",
273 sc->sc_dev.dv_xname, i);
274 return;
275 }
276 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
277 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
278 sc->sc_in = ed->bEndpointAddress;
279 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
280 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
281 sc->sc_out = ed->bEndpointAddress;
282 }
283 }
284 if (sc->sc_out == -1) {
285 printf("%s: could not find bulk out endpoint\n",
286 sc->sc_dev.dv_xname);
287 usbd_deactivate(sc->sc_udev);
288 return;
289 }
290
291 if (usbd_get_quirks(dev)->uq_flags & UQ_BROKEN_BIDIR) {
292 /* This device doesn't handle reading properly. */
293 sc->sc_in = -1;
294 }
295
296 printf("%s: using %s-directional mode\n", sc->sc_dev.dv_xname,
297 sc->sc_in >= 0 ? "bi" : "uni");
298
299 DPRINTFN(10, ("ulpt_attach: bulk=%d\n", sc->sc_out));
300
301 sc->sc_iface = iface;
302 sc->sc_ifaceno = id->bInterfaceNumber;
303 sc->sc_udev = dev;
304
305 /* maybe the device needs firmware */
306 sc->sc_fwdev = ulpt_lookup(uaa->vendor, uaa->product);
307 if (sc->sc_fwdev)
308 config_mountroot(self, ulpt_load_firmware);
309
310 #if 0
311 /*
312 * This code is disabled because for some mysterious reason it causes
313 * printing not to work. But only sometimes, and mostly with
314 * UHCI and less often with OHCI. *sigh*
315 */
316 {
317 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
318 usb_device_request_t req;
319 int len, alen;
320
321 req.bmRequestType = UT_READ_CLASS_INTERFACE;
322 req.bRequest = UR_GET_DEVICE_ID;
323 USETW(req.wValue, cd->bConfigurationValue);
324 USETW2(req.wIndex, id->bInterfaceNumber, id->bAlternateSetting);
325 USETW(req.wLength, DEVINFOSIZE - 1);
326 err = usbd_do_request_flags(dev, &req, devinfop, USBD_SHORT_XFER_OK,
327 &alen, USBD_DEFAULT_TIMEOUT);
328 if (err) {
329 printf("%s: cannot get device id\n", sc->sc_dev.dv_xname);
330 } else if (alen <= 2) {
331 printf("%s: empty device id, no printer connected?\n",
332 sc->sc_dev.dv_xname);
333 } else {
334 /* devinfop now contains an IEEE-1284 device ID */
335 len = ((devinfop[0] & 0xff) << 8) | (devinfop[1] & 0xff);
336 if (len > DEVINFOSIZE - 3)
337 len = DEVINFOSIZE - 3;
338 devinfo[len] = 0;
339 printf("%s: device id <", sc->sc_dev.dv_xname);
340 ieee1284_print_id(devinfop+2);
341 printf(">\n");
342 }
343 }
344 #endif
345 }
346
347 int
ulpt_detach(struct device * self,int flags)348 ulpt_detach(struct device *self, int flags)
349 {
350 struct ulpt_softc *sc = (struct ulpt_softc *)self;
351 int s;
352 int maj, mn;
353
354 DPRINTF(("ulpt_detach: sc=%p\n", sc));
355
356 if (sc->sc_out_pipe != NULL)
357 usbd_abort_pipe(sc->sc_out_pipe);
358 if (sc->sc_in_pipe != NULL)
359 usbd_abort_pipe(sc->sc_in_pipe);
360
361 s = splusb();
362 if (--sc->sc_refcnt >= 0) {
363 /* There is noone to wake, aborting the pipe is enough */
364 /* Wait for processes to go away. */
365 usb_detach_wait(&sc->sc_dev);
366 }
367 splx(s);
368
369 /* locate the major number */
370 for (maj = 0; maj < nchrdev; maj++)
371 if (cdevsw[maj].d_open == ulptopen)
372 break;
373
374 /* Nuke the vnodes for any open instances (calls close). */
375 mn = self->dv_unit;
376 vdevgone(maj, mn, mn, VCHR);
377 vdevgone(maj, mn | ULPT_NOPRIME , mn | ULPT_NOPRIME, VCHR);
378
379 return (0);
380 }
381
382 int
ulpt_status(struct ulpt_softc * sc)383 ulpt_status(struct ulpt_softc *sc)
384 {
385 usb_device_request_t req;
386 usbd_status err;
387 u_char status;
388
389 req.bmRequestType = UT_READ_CLASS_INTERFACE;
390 req.bRequest = UR_GET_PORT_STATUS;
391 USETW(req.wValue, 0);
392 USETW(req.wIndex, sc->sc_ifaceno);
393 USETW(req.wLength, 1);
394 err = usbd_do_request(sc->sc_udev, &req, &status);
395 DPRINTFN(1, ("ulpt_status: status=0x%02x err=%d\n", status, err));
396 if (!err)
397 return (status);
398 else
399 return (0);
400 }
401
402 void
ulpt_reset(struct ulpt_softc * sc)403 ulpt_reset(struct ulpt_softc *sc)
404 {
405 usb_device_request_t req;
406
407 DPRINTFN(1, ("ulpt_reset\n"));
408 req.bRequest = UR_SOFT_RESET;
409 USETW(req.wValue, 0);
410 USETW(req.wIndex, sc->sc_ifaceno);
411 USETW(req.wLength, 0);
412
413 /*
414 * There was a mistake in the USB printer 1.0 spec that gave the
415 * request type as UT_WRITE_CLASS_OTHER; it should have been
416 * UT_WRITE_CLASS_INTERFACE. Many printers use the old one,
417 * so we try both.
418 */
419 req.bmRequestType = UT_WRITE_CLASS_OTHER;
420 if (usbd_do_request(sc->sc_udev, &req, 0)) { /* 1.0 */
421 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
422 (void)usbd_do_request(sc->sc_udev, &req, 0); /* 1.1 */
423 }
424 }
425
426 static void
ulpt_input(struct usbd_xfer * xfer,void * priv,usbd_status status)427 ulpt_input(struct usbd_xfer *xfer, void *priv, usbd_status status)
428 {
429 struct ulpt_softc *sc = priv;
430
431 DPRINTFN(2,("ulpt_input: got some data\n"));
432 /* Do it again. */
433 if (xfer == sc->sc_in_xfer1)
434 usbd_transfer(sc->sc_in_xfer2);
435 else
436 usbd_transfer(sc->sc_in_xfer1);
437 }
438
439 int ulptusein = 1;
440
441 /*
442 * Reset the printer, then wait until it's selected and not busy.
443 */
444 int
ulptopen(dev_t dev,int flag,int mode,struct proc * p)445 ulptopen(dev_t dev, int flag, int mode, struct proc *p)
446 {
447 u_char flags = ULPTFLAGS(dev);
448 struct ulpt_softc *sc;
449 usbd_status err;
450 int error;
451
452 if (ULPTUNIT(dev) >= ulpt_cd.cd_ndevs)
453 return (ENXIO);
454 sc = ulpt_cd.cd_devs[ULPTUNIT(dev)];
455 if (sc == NULL)
456 return (ENXIO);
457
458 if (sc == NULL || sc->sc_iface == NULL || usbd_is_dying(sc->sc_udev))
459 return (ENXIO);
460
461 if (sc->sc_state)
462 return (EBUSY);
463
464 /* If a previous attempt to load firmware failed, retry. */
465 if (sc->sc_flags & ULPT_EFIRMWARE) {
466 ulpt_load_firmware(&sc->sc_dev);
467 if (sc->sc_flags & ULPT_EFIRMWARE)
468 return (EIO);
469 }
470
471 sc->sc_state = ULPT_INIT;
472 sc->sc_flags = flags;
473 DPRINTF(("ulptopen: flags=0x%x\n", (unsigned)flags));
474
475 error = 0;
476 sc->sc_refcnt++;
477
478 if ((flags & ULPT_NOPRIME) == 0) {
479 ulpt_reset(sc);
480 if (usbd_is_dying(sc->sc_udev)) {
481 error = ENXIO;
482 sc->sc_state = 0;
483 goto done;
484 }
485 }
486
487 err = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
488 if (err) {
489 sc->sc_state = 0;
490 error = EIO;
491 goto done;
492 }
493 if (ulptusein && sc->sc_in != -1) {
494 DPRINTF(("ulpt_open: open input pipe\n"));
495 err = usbd_open_pipe(sc->sc_iface, sc->sc_in,0,&sc->sc_in_pipe);
496 if (err) {
497 error = EIO;
498 usbd_close_pipe(sc->sc_out_pipe);
499 sc->sc_out_pipe = NULL;
500 sc->sc_state = 0;
501 goto done;
502 }
503 sc->sc_in_xfer1 = usbd_alloc_xfer(sc->sc_udev);
504 sc->sc_in_xfer2 = usbd_alloc_xfer(sc->sc_udev);
505 if (sc->sc_in_xfer1 == NULL || sc->sc_in_xfer2 == NULL) {
506 error = ENOMEM;
507 if (sc->sc_in_xfer1 != NULL) {
508 usbd_free_xfer(sc->sc_in_xfer1);
509 sc->sc_in_xfer1 = NULL;
510 }
511 if (sc->sc_in_xfer2 != NULL) {
512 usbd_free_xfer(sc->sc_in_xfer2);
513 sc->sc_in_xfer2 = NULL;
514 }
515 usbd_close_pipe(sc->sc_out_pipe);
516 sc->sc_out_pipe = NULL;
517 usbd_close_pipe(sc->sc_in_pipe);
518 sc->sc_in_pipe = NULL;
519 sc->sc_state = 0;
520 goto done;
521 }
522 usbd_setup_xfer(sc->sc_in_xfer1, sc->sc_in_pipe, sc,
523 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
524 USBD_NO_TIMEOUT, ulpt_input);
525 usbd_setup_xfer(sc->sc_in_xfer2, sc->sc_in_pipe, sc,
526 sc->sc_junk, sizeof sc->sc_junk, USBD_SHORT_XFER_OK,
527 USBD_NO_TIMEOUT, ulpt_input);
528 usbd_transfer(sc->sc_in_xfer1); /* ignore failed start */
529 }
530
531 sc->sc_state = ULPT_OPEN;
532
533 done:
534 if (--sc->sc_refcnt < 0)
535 usb_detach_wakeup(&sc->sc_dev);
536
537 DPRINTF(("ulptopen: done, error=%d\n", error));
538 return (error);
539 }
540
541 int
ulpt_statusmsg(u_char status,struct ulpt_softc * sc)542 ulpt_statusmsg(u_char status, struct ulpt_softc *sc)
543 {
544 u_char new;
545
546 status = (status ^ LPS_INVERT) & LPS_MASK;
547 new = status & ~sc->sc_laststatus;
548 sc->sc_laststatus = status;
549
550 if (new & LPS_SELECT)
551 log(LOG_NOTICE, "%s: offline\n", sc->sc_dev.dv_xname);
552 else if (new & LPS_NOPAPER)
553 log(LOG_NOTICE, "%s: out of paper\n", sc->sc_dev.dv_xname);
554 else if (new & LPS_NERR)
555 log(LOG_NOTICE, "%s: output error\n", sc->sc_dev.dv_xname);
556
557 return (status);
558 }
559
560 int
ulptclose(dev_t dev,int flag,int mode,struct proc * p)561 ulptclose(dev_t dev, int flag, int mode, struct proc *p)
562 {
563 struct ulpt_softc *sc;
564
565 sc = ulpt_cd.cd_devs[ULPTUNIT(dev)];
566
567 if (sc->sc_state != ULPT_OPEN)
568 /* We are being forced to close before the open completed. */
569 return (0);
570
571 if (sc->sc_out_pipe != NULL) {
572 usbd_close_pipe(sc->sc_out_pipe);
573 sc->sc_out_pipe = NULL;
574 }
575 if (sc->sc_in_pipe != NULL) {
576 usbd_close_pipe(sc->sc_in_pipe);
577 sc->sc_in_pipe = NULL;
578 if (sc->sc_in_xfer1 != NULL) {
579 usbd_free_xfer(sc->sc_in_xfer1);
580 sc->sc_in_xfer1 = NULL;
581 }
582 if (sc->sc_in_xfer2 != NULL) {
583 usbd_free_xfer(sc->sc_in_xfer2);
584 sc->sc_in_xfer2 = NULL;
585 }
586 }
587
588 sc->sc_state = 0;
589
590 DPRINTF(("ulptclose: closed\n"));
591 return (0);
592 }
593
594 int
ulpt_do_write(struct ulpt_softc * sc,struct uio * uio,int flags)595 ulpt_do_write(struct ulpt_softc *sc, struct uio *uio, int flags)
596 {
597 size_t n;
598 int error = 0;
599 void *bufp;
600 struct usbd_xfer *xfer;
601 usbd_status err;
602
603 DPRINTF(("ulptwrite\n"));
604 xfer = usbd_alloc_xfer(sc->sc_udev);
605 if (xfer == NULL)
606 return (ENOMEM);
607 bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
608 if (bufp == NULL) {
609 usbd_free_xfer(xfer);
610 return (ENOMEM);
611 }
612 while ((n = ulmin(ULPT_BSIZE, uio->uio_resid)) != 0) {
613 ulpt_statusmsg(ulpt_status(sc), sc);
614 error = uiomove(bufp, n, uio);
615 if (error)
616 break;
617 DPRINTFN(1, ("ulptwrite: transfer %zu bytes\n", n));
618 usbd_setup_xfer(xfer, sc->sc_out_pipe, 0, bufp, n,
619 USBD_NO_COPY | USBD_SYNCHRONOUS | USBD_CATCH, 0, NULL);
620 err = usbd_transfer(xfer);
621 if (err) {
622 usbd_clear_endpoint_stall(sc->sc_out_pipe);
623 DPRINTF(("ulptwrite: error=%d\n", err));
624 error = EIO;
625 break;
626 }
627 }
628 usbd_free_xfer(xfer);
629
630 return (error);
631 }
632
633 int
ulptwrite(dev_t dev,struct uio * uio,int flags)634 ulptwrite(dev_t dev, struct uio *uio, int flags)
635 {
636 struct ulpt_softc *sc;
637 int error;
638
639 sc = ulpt_cd.cd_devs[ULPTUNIT(dev)];
640
641 if (usbd_is_dying(sc->sc_udev) || (sc->sc_flags & ULPT_EFIRMWARE))
642 return (EIO);
643
644 sc->sc_refcnt++;
645 error = ulpt_do_write(sc, uio, flags);
646 if (--sc->sc_refcnt < 0)
647 usb_detach_wakeup(&sc->sc_dev);
648 return (error);
649 }
650
651 usbd_status
ulpt_ucode_loader_hp(struct ulpt_softc * sc)652 ulpt_ucode_loader_hp(struct ulpt_softc *sc)
653 {
654 usbd_status error;
655 int load_error;
656 uint8_t *ucode;
657 uint32_t len;
658 size_t ucode_size;
659 const char *ucode_name = sc->sc_fwdev->ucode_name;
660 int offset = 0, remain;
661 struct usbd_xfer *xfer;
662 void *bufp;
663
664 /* open microcode file */
665 load_error = loadfirmware(ucode_name, &ucode, &ucode_size);
666 if (load_error != 0) {
667 printf("%s: failed loadfirmware of file %s (error %d)\n",
668 sc->sc_dev.dv_xname, ucode_name, load_error);
669 return (USBD_INVAL);
670 }
671
672 /* upload microcode */
673 error = usbd_open_pipe(sc->sc_iface, sc->sc_out, 0, &sc->sc_out_pipe);
674 if (error)
675 goto free_ucode;
676 xfer = usbd_alloc_xfer(sc->sc_udev);
677 if (xfer == NULL)
678 goto close_pipe;
679 bufp = usbd_alloc_buffer(xfer, ULPT_BSIZE);
680 if (bufp == NULL) {
681 error = USBD_NOMEM;
682 goto free_xfer;
683 }
684 remain = ucode_size;
685 while (remain > 0) {
686 len = min(remain, ULPT_BSIZE);
687 memcpy(bufp, &ucode[offset], len);
688 usbd_setup_xfer(xfer, sc->sc_out_pipe, 0, bufp, len,
689 USBD_NO_COPY | USBD_SYNCHRONOUS, 0, NULL);
690 error = usbd_transfer(xfer);
691 if (error != USBD_NORMAL_COMPLETION) {
692 usbd_clear_endpoint_stall(sc->sc_out_pipe);
693 printf("%s: ucode upload error=%s!\n",
694 sc->sc_dev.dv_xname, usbd_errstr(error));
695 break;
696 }
697 DPRINTF(("%s: uploaded %d bytes ucode\n",
698 sc->sc_dev.dv_xname, len));
699
700 offset += len;
701 remain -= len;
702 }
703 free_xfer:
704 usbd_free_xfer(xfer);
705 close_pipe:
706 usbd_close_pipe(sc->sc_out_pipe);
707 sc->sc_out_pipe = NULL;
708 free_ucode:
709 free(ucode, M_DEVBUF, ucode_size);
710
711 return (error);
712 }
713
714 #if 0
715 /* XXX This does not belong here. */
716 /*
717 * Print select parts of a IEEE 1284 device ID.
718 */
719 void
720 ieee1284_print_id(char *str)
721 {
722 char *p, *q;
723
724 for (p = str-1; p; p = strchr(p, ';')) {
725 p++; /* skip ';' */
726 if (strncmp(p, "MFG:", 4) == 0 ||
727 strncmp(p, "MANUFACTURER:", 14) == 0 ||
728 strncmp(p, "MDL:", 4) == 0 ||
729 strncmp(p, "MODEL:", 6) == 0) {
730 q = strchr(p, ';');
731 if (q)
732 printf("%.*s", (int)(q - p + 1), p);
733 }
734 }
735 }
736 #endif
737