Commit 9908a32e94de2141463e104c9924279ed3509447

Authored by Greg Kroah-Hartman
1 parent 194343d936

USB: remove err() macro from usb class drivers

USB should not be having it's own printk macros, so remove err() and
use the system-wide standard of dev_err() wherever possible.  In the
few places that will not work out, use a basic printk().

Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

Showing 3 changed files with 48 additions and 30 deletions Inline Diff

drivers/usb/class/cdc-acm.c
1 /* 1 /*
2 * cdc-acm.c 2 * cdc-acm.c
3 * 3 *
4 * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de> 4 * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de>
5 * Copyright (c) 1999 Pavel Machek <pavel@suse.cz> 5 * Copyright (c) 1999 Pavel Machek <pavel@suse.cz>
6 * Copyright (c) 1999 Johannes Erdfelt <johannes@erdfelt.com> 6 * Copyright (c) 1999 Johannes Erdfelt <johannes@erdfelt.com>
7 * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz> 7 * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz>
8 * Copyright (c) 2004 Oliver Neukum <oliver@neukum.name> 8 * Copyright (c) 2004 Oliver Neukum <oliver@neukum.name>
9 * Copyright (c) 2005 David Kubicek <dave@awk.cz> 9 * Copyright (c) 2005 David Kubicek <dave@awk.cz>
10 * 10 *
11 * USB Abstract Control Model driver for USB modems and ISDN adapters 11 * USB Abstract Control Model driver for USB modems and ISDN adapters
12 * 12 *
13 * Sponsored by SuSE 13 * Sponsored by SuSE
14 * 14 *
15 * ChangeLog: 15 * ChangeLog:
16 * v0.9 - thorough cleaning, URBification, almost a rewrite 16 * v0.9 - thorough cleaning, URBification, almost a rewrite
17 * v0.10 - some more cleanups 17 * v0.10 - some more cleanups
18 * v0.11 - fixed flow control, read error doesn't stop reads 18 * v0.11 - fixed flow control, read error doesn't stop reads
19 * v0.12 - added TIOCM ioctls, added break handling, made struct acm kmalloced 19 * v0.12 - added TIOCM ioctls, added break handling, made struct acm kmalloced
20 * v0.13 - added termios, added hangup 20 * v0.13 - added termios, added hangup
21 * v0.14 - sized down struct acm 21 * v0.14 - sized down struct acm
22 * v0.15 - fixed flow control again - characters could be lost 22 * v0.15 - fixed flow control again - characters could be lost
23 * v0.16 - added code for modems with swapped data and control interfaces 23 * v0.16 - added code for modems with swapped data and control interfaces
24 * v0.17 - added new style probing 24 * v0.17 - added new style probing
25 * v0.18 - fixed new style probing for devices with more configurations 25 * v0.18 - fixed new style probing for devices with more configurations
26 * v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan) 26 * v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan)
27 * v0.20 - switched to probing on interface (rather than device) class 27 * v0.20 - switched to probing on interface (rather than device) class
28 * v0.21 - revert to probing on device for devices with multiple configs 28 * v0.21 - revert to probing on device for devices with multiple configs
29 * v0.22 - probe only the control interface. if usbcore doesn't choose the 29 * v0.22 - probe only the control interface. if usbcore doesn't choose the
30 * config we want, sysadmin changes bConfigurationValue in sysfs. 30 * config we want, sysadmin changes bConfigurationValue in sysfs.
31 * v0.23 - use softirq for rx processing, as needed by tty layer 31 * v0.23 - use softirq for rx processing, as needed by tty layer
32 * v0.24 - change probe method to evaluate CDC union descriptor 32 * v0.24 - change probe method to evaluate CDC union descriptor
33 * v0.25 - downstream tasks paralelized to maximize throughput 33 * v0.25 - downstream tasks paralelized to maximize throughput
34 * v0.26 - multiple write urbs, writesize increased 34 * v0.26 - multiple write urbs, writesize increased
35 */ 35 */
36 36
37 /* 37 /*
38 * This program is free software; you can redistribute it and/or modify 38 * This program is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by 39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation; either version 2 of the License, or 40 * the Free Software Foundation; either version 2 of the License, or
41 * (at your option) any later version. 41 * (at your option) any later version.
42 * 42 *
43 * This program is distributed in the hope that it will be useful, 43 * This program is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of 44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details. 46 * GNU General Public License for more details.
47 * 47 *
48 * You should have received a copy of the GNU General Public License 48 * You should have received a copy of the GNU General Public License
49 * along with this program; if not, write to the Free Software 49 * along with this program; if not, write to the Free Software
50 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 50 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
51 */ 51 */
52 52
53 #undef DEBUG 53 #undef DEBUG
54 #undef VERBOSE_DEBUG 54 #undef VERBOSE_DEBUG
55 55
56 #include <linux/kernel.h> 56 #include <linux/kernel.h>
57 #include <linux/errno.h> 57 #include <linux/errno.h>
58 #include <linux/init.h> 58 #include <linux/init.h>
59 #include <linux/slab.h> 59 #include <linux/slab.h>
60 #include <linux/tty.h> 60 #include <linux/tty.h>
61 #include <linux/tty_driver.h> 61 #include <linux/tty_driver.h>
62 #include <linux/tty_flip.h> 62 #include <linux/tty_flip.h>
63 #include <linux/module.h> 63 #include <linux/module.h>
64 #include <linux/mutex.h> 64 #include <linux/mutex.h>
65 #include <asm/uaccess.h> 65 #include <asm/uaccess.h>
66 #include <linux/usb.h> 66 #include <linux/usb.h>
67 #include <linux/usb/cdc.h> 67 #include <linux/usb/cdc.h>
68 #include <asm/byteorder.h> 68 #include <asm/byteorder.h>
69 #include <asm/unaligned.h> 69 #include <asm/unaligned.h>
70 #include <linux/list.h> 70 #include <linux/list.h>
71 71
72 #include "cdc-acm.h" 72 #include "cdc-acm.h"
73 73
74 74
75 #define ACM_CLOSE_TIMEOUT 15 /* seconds to let writes drain */ 75 #define ACM_CLOSE_TIMEOUT 15 /* seconds to let writes drain */
76 76
77 /* 77 /*
78 * Version Information 78 * Version Information
79 */ 79 */
80 #define DRIVER_VERSION "v0.26" 80 #define DRIVER_VERSION "v0.26"
81 #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek" 81 #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek"
82 #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters" 82 #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
83 83
84 static struct usb_driver acm_driver; 84 static struct usb_driver acm_driver;
85 static struct tty_driver *acm_tty_driver; 85 static struct tty_driver *acm_tty_driver;
86 static struct acm *acm_table[ACM_TTY_MINORS]; 86 static struct acm *acm_table[ACM_TTY_MINORS];
87 87
88 static DEFINE_MUTEX(open_mutex); 88 static DEFINE_MUTEX(open_mutex);
89 89
90 #define ACM_READY(acm) (acm && acm->dev && acm->used) 90 #define ACM_READY(acm) (acm && acm->dev && acm->used)
91 91
92 #ifdef VERBOSE_DEBUG 92 #ifdef VERBOSE_DEBUG
93 #define verbose 1 93 #define verbose 1
94 #else 94 #else
95 #define verbose 0 95 #define verbose 0
96 #endif 96 #endif
97 97
98 /* 98 /*
99 * Functions for ACM control messages. 99 * Functions for ACM control messages.
100 */ 100 */
101 101
102 static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len) 102 static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
103 { 103 {
104 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0), 104 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
105 request, USB_RT_ACM, value, 105 request, USB_RT_ACM, value,
106 acm->control->altsetting[0].desc.bInterfaceNumber, 106 acm->control->altsetting[0].desc.bInterfaceNumber,
107 buf, len, 5000); 107 buf, len, 5000);
108 dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval); 108 dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
109 return retval < 0 ? retval : 0; 109 return retval < 0 ? retval : 0;
110 } 110 }
111 111
112 /* devices aren't required to support these requests. 112 /* devices aren't required to support these requests.
113 * the cdc acm descriptor tells whether they do... 113 * the cdc acm descriptor tells whether they do...
114 */ 114 */
115 #define acm_set_control(acm, control) \ 115 #define acm_set_control(acm, control) \
116 acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0) 116 acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
117 #define acm_set_line(acm, line) \ 117 #define acm_set_line(acm, line) \
118 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line)) 118 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
119 #define acm_send_break(acm, ms) \ 119 #define acm_send_break(acm, ms) \
120 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0) 120 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
121 121
122 /* 122 /*
123 * Write buffer management. 123 * Write buffer management.
124 * All of these assume proper locks taken by the caller. 124 * All of these assume proper locks taken by the caller.
125 */ 125 */
126 126
127 static int acm_wb_alloc(struct acm *acm) 127 static int acm_wb_alloc(struct acm *acm)
128 { 128 {
129 int i, wbn; 129 int i, wbn;
130 struct acm_wb *wb; 130 struct acm_wb *wb;
131 131
132 wbn = 0; 132 wbn = 0;
133 i = 0; 133 i = 0;
134 for (;;) { 134 for (;;) {
135 wb = &acm->wb[wbn]; 135 wb = &acm->wb[wbn];
136 if (!wb->use) { 136 if (!wb->use) {
137 wb->use = 1; 137 wb->use = 1;
138 return wbn; 138 return wbn;
139 } 139 }
140 wbn = (wbn + 1) % ACM_NW; 140 wbn = (wbn + 1) % ACM_NW;
141 if (++i >= ACM_NW) 141 if (++i >= ACM_NW)
142 return -1; 142 return -1;
143 } 143 }
144 } 144 }
145 145
146 static int acm_wb_is_avail(struct acm *acm) 146 static int acm_wb_is_avail(struct acm *acm)
147 { 147 {
148 int i, n; 148 int i, n;
149 unsigned long flags; 149 unsigned long flags;
150 150
151 n = ACM_NW; 151 n = ACM_NW;
152 spin_lock_irqsave(&acm->write_lock, flags); 152 spin_lock_irqsave(&acm->write_lock, flags);
153 for (i = 0; i < ACM_NW; i++) { 153 for (i = 0; i < ACM_NW; i++) {
154 n -= acm->wb[i].use; 154 n -= acm->wb[i].use;
155 } 155 }
156 spin_unlock_irqrestore(&acm->write_lock, flags); 156 spin_unlock_irqrestore(&acm->write_lock, flags);
157 return n; 157 return n;
158 } 158 }
159 159
160 /* 160 /*
161 * Finish write. 161 * Finish write.
162 */ 162 */
163 static void acm_write_done(struct acm *acm, struct acm_wb *wb) 163 static void acm_write_done(struct acm *acm, struct acm_wb *wb)
164 { 164 {
165 unsigned long flags; 165 unsigned long flags;
166 166
167 spin_lock_irqsave(&acm->write_lock, flags); 167 spin_lock_irqsave(&acm->write_lock, flags);
168 wb->use = 0; 168 wb->use = 0;
169 acm->transmitting--; 169 acm->transmitting--;
170 spin_unlock_irqrestore(&acm->write_lock, flags); 170 spin_unlock_irqrestore(&acm->write_lock, flags);
171 } 171 }
172 172
173 /* 173 /*
174 * Poke write. 174 * Poke write.
175 * 175 *
176 * the caller is responsible for locking 176 * the caller is responsible for locking
177 */ 177 */
178 178
179 static int acm_start_wb(struct acm *acm, struct acm_wb *wb) 179 static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
180 { 180 {
181 int rc; 181 int rc;
182 182
183 acm->transmitting++; 183 acm->transmitting++;
184 184
185 wb->urb->transfer_buffer = wb->buf; 185 wb->urb->transfer_buffer = wb->buf;
186 wb->urb->transfer_dma = wb->dmah; 186 wb->urb->transfer_dma = wb->dmah;
187 wb->urb->transfer_buffer_length = wb->len; 187 wb->urb->transfer_buffer_length = wb->len;
188 wb->urb->dev = acm->dev; 188 wb->urb->dev = acm->dev;
189 189
190 if ((rc = usb_submit_urb(wb->urb, GFP_ATOMIC)) < 0) { 190 if ((rc = usb_submit_urb(wb->urb, GFP_ATOMIC)) < 0) {
191 dbg("usb_submit_urb(write bulk) failed: %d", rc); 191 dbg("usb_submit_urb(write bulk) failed: %d", rc);
192 acm_write_done(acm, wb); 192 acm_write_done(acm, wb);
193 } 193 }
194 return rc; 194 return rc;
195 } 195 }
196 196
197 static int acm_write_start(struct acm *acm, int wbn) 197 static int acm_write_start(struct acm *acm, int wbn)
198 { 198 {
199 unsigned long flags; 199 unsigned long flags;
200 struct acm_wb *wb = &acm->wb[wbn]; 200 struct acm_wb *wb = &acm->wb[wbn];
201 int rc; 201 int rc;
202 202
203 spin_lock_irqsave(&acm->write_lock, flags); 203 spin_lock_irqsave(&acm->write_lock, flags);
204 if (!acm->dev) { 204 if (!acm->dev) {
205 wb->use = 0; 205 wb->use = 0;
206 spin_unlock_irqrestore(&acm->write_lock, flags); 206 spin_unlock_irqrestore(&acm->write_lock, flags);
207 return -ENODEV; 207 return -ENODEV;
208 } 208 }
209 209
210 dbg("%s susp_count: %d", __func__, acm->susp_count); 210 dbg("%s susp_count: %d", __func__, acm->susp_count);
211 if (acm->susp_count) { 211 if (acm->susp_count) {
212 acm->delayed_wb = wb; 212 acm->delayed_wb = wb;
213 schedule_work(&acm->waker); 213 schedule_work(&acm->waker);
214 spin_unlock_irqrestore(&acm->write_lock, flags); 214 spin_unlock_irqrestore(&acm->write_lock, flags);
215 return 0; /* A white lie */ 215 return 0; /* A white lie */
216 } 216 }
217 usb_mark_last_busy(acm->dev); 217 usb_mark_last_busy(acm->dev);
218 218
219 rc = acm_start_wb(acm, wb); 219 rc = acm_start_wb(acm, wb);
220 spin_unlock_irqrestore(&acm->write_lock, flags); 220 spin_unlock_irqrestore(&acm->write_lock, flags);
221 221
222 return rc; 222 return rc;
223 223
224 } 224 }
225 /* 225 /*
226 * attributes exported through sysfs 226 * attributes exported through sysfs
227 */ 227 */
228 static ssize_t show_caps 228 static ssize_t show_caps
229 (struct device *dev, struct device_attribute *attr, char *buf) 229 (struct device *dev, struct device_attribute *attr, char *buf)
230 { 230 {
231 struct usb_interface *intf = to_usb_interface(dev); 231 struct usb_interface *intf = to_usb_interface(dev);
232 struct acm *acm = usb_get_intfdata(intf); 232 struct acm *acm = usb_get_intfdata(intf);
233 233
234 return sprintf(buf, "%d", acm->ctrl_caps); 234 return sprintf(buf, "%d", acm->ctrl_caps);
235 } 235 }
236 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL); 236 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
237 237
238 static ssize_t show_country_codes 238 static ssize_t show_country_codes
239 (struct device *dev, struct device_attribute *attr, char *buf) 239 (struct device *dev, struct device_attribute *attr, char *buf)
240 { 240 {
241 struct usb_interface *intf = to_usb_interface(dev); 241 struct usb_interface *intf = to_usb_interface(dev);
242 struct acm *acm = usb_get_intfdata(intf); 242 struct acm *acm = usb_get_intfdata(intf);
243 243
244 memcpy(buf, acm->country_codes, acm->country_code_size); 244 memcpy(buf, acm->country_codes, acm->country_code_size);
245 return acm->country_code_size; 245 return acm->country_code_size;
246 } 246 }
247 247
248 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL); 248 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
249 249
250 static ssize_t show_country_rel_date 250 static ssize_t show_country_rel_date
251 (struct device *dev, struct device_attribute *attr, char *buf) 251 (struct device *dev, struct device_attribute *attr, char *buf)
252 { 252 {
253 struct usb_interface *intf = to_usb_interface(dev); 253 struct usb_interface *intf = to_usb_interface(dev);
254 struct acm *acm = usb_get_intfdata(intf); 254 struct acm *acm = usb_get_intfdata(intf);
255 255
256 return sprintf(buf, "%d", acm->country_rel_date); 256 return sprintf(buf, "%d", acm->country_rel_date);
257 } 257 }
258 258
259 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL); 259 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
260 /* 260 /*
261 * Interrupt handlers for various ACM device responses 261 * Interrupt handlers for various ACM device responses
262 */ 262 */
263 263
264 /* control interface reports status changes with "interrupt" transfers */ 264 /* control interface reports status changes with "interrupt" transfers */
265 static void acm_ctrl_irq(struct urb *urb) 265 static void acm_ctrl_irq(struct urb *urb)
266 { 266 {
267 struct acm *acm = urb->context; 267 struct acm *acm = urb->context;
268 struct usb_cdc_notification *dr = urb->transfer_buffer; 268 struct usb_cdc_notification *dr = urb->transfer_buffer;
269 unsigned char *data; 269 unsigned char *data;
270 int newctrl; 270 int newctrl;
271 int retval; 271 int retval;
272 int status = urb->status; 272 int status = urb->status;
273 273
274 switch (status) { 274 switch (status) {
275 case 0: 275 case 0:
276 /* success */ 276 /* success */
277 break; 277 break;
278 case -ECONNRESET: 278 case -ECONNRESET:
279 case -ENOENT: 279 case -ENOENT:
280 case -ESHUTDOWN: 280 case -ESHUTDOWN:
281 /* this urb is terminated, clean up */ 281 /* this urb is terminated, clean up */
282 dbg("%s - urb shutting down with status: %d", __func__, status); 282 dbg("%s - urb shutting down with status: %d", __func__, status);
283 return; 283 return;
284 default: 284 default:
285 dbg("%s - nonzero urb status received: %d", __func__, status); 285 dbg("%s - nonzero urb status received: %d", __func__, status);
286 goto exit; 286 goto exit;
287 } 287 }
288 288
289 if (!ACM_READY(acm)) 289 if (!ACM_READY(acm))
290 goto exit; 290 goto exit;
291 291
292 data = (unsigned char *)(dr + 1); 292 data = (unsigned char *)(dr + 1);
293 switch (dr->bNotificationType) { 293 switch (dr->bNotificationType) {
294 294
295 case USB_CDC_NOTIFY_NETWORK_CONNECTION: 295 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
296 296
297 dbg("%s network", dr->wValue ? "connected to" : "disconnected from"); 297 dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
298 break; 298 break;
299 299
300 case USB_CDC_NOTIFY_SERIAL_STATE: 300 case USB_CDC_NOTIFY_SERIAL_STATE:
301 301
302 newctrl = get_unaligned_le16(data); 302 newctrl = get_unaligned_le16(data);
303 303
304 if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) { 304 if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
305 dbg("calling hangup"); 305 dbg("calling hangup");
306 tty_hangup(acm->tty); 306 tty_hangup(acm->tty);
307 } 307 }
308 308
309 acm->ctrlin = newctrl; 309 acm->ctrlin = newctrl;
310 310
311 dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c", 311 dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
312 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-', 312 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
313 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-', 313 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
314 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-', 314 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
315 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-'); 315 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
316 316
317 break; 317 break;
318 318
319 default: 319 default:
320 dbg("unknown notification %d received: index %d len %d data0 %d data1 %d", 320 dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
321 dr->bNotificationType, dr->wIndex, 321 dr->bNotificationType, dr->wIndex,
322 dr->wLength, data[0], data[1]); 322 dr->wLength, data[0], data[1]);
323 break; 323 break;
324 } 324 }
325 exit: 325 exit:
326 usb_mark_last_busy(acm->dev); 326 usb_mark_last_busy(acm->dev);
327 retval = usb_submit_urb (urb, GFP_ATOMIC); 327 retval = usb_submit_urb (urb, GFP_ATOMIC);
328 if (retval) 328 if (retval)
329 err ("%s - usb_submit_urb failed with result %d", 329 dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with "
330 __func__, retval); 330 "result %d", __func__, retval);
331 } 331 }
332 332
333 /* data interface returns incoming bytes, or we got unthrottled */ 333 /* data interface returns incoming bytes, or we got unthrottled */
334 static void acm_read_bulk(struct urb *urb) 334 static void acm_read_bulk(struct urb *urb)
335 { 335 {
336 struct acm_rb *buf; 336 struct acm_rb *buf;
337 struct acm_ru *rcv = urb->context; 337 struct acm_ru *rcv = urb->context;
338 struct acm *acm = rcv->instance; 338 struct acm *acm = rcv->instance;
339 int status = urb->status; 339 int status = urb->status;
340 340
341 dbg("Entering acm_read_bulk with status %d", status); 341 dbg("Entering acm_read_bulk with status %d", status);
342 342
343 if (!ACM_READY(acm)) { 343 if (!ACM_READY(acm)) {
344 dev_dbg(&acm->data->dev, "Aborting, acm not ready"); 344 dev_dbg(&acm->data->dev, "Aborting, acm not ready");
345 return; 345 return;
346 } 346 }
347 usb_mark_last_busy(acm->dev); 347 usb_mark_last_busy(acm->dev);
348 348
349 if (status) 349 if (status)
350 dev_dbg(&acm->data->dev, "bulk rx status %d\n", status); 350 dev_dbg(&acm->data->dev, "bulk rx status %d\n", status);
351 351
352 buf = rcv->buffer; 352 buf = rcv->buffer;
353 buf->size = urb->actual_length; 353 buf->size = urb->actual_length;
354 354
355 if (likely(status == 0)) { 355 if (likely(status == 0)) {
356 spin_lock(&acm->read_lock); 356 spin_lock(&acm->read_lock);
357 acm->processing++; 357 acm->processing++;
358 list_add_tail(&rcv->list, &acm->spare_read_urbs); 358 list_add_tail(&rcv->list, &acm->spare_read_urbs);
359 list_add_tail(&buf->list, &acm->filled_read_bufs); 359 list_add_tail(&buf->list, &acm->filled_read_bufs);
360 spin_unlock(&acm->read_lock); 360 spin_unlock(&acm->read_lock);
361 } else { 361 } else {
362 /* we drop the buffer due to an error */ 362 /* we drop the buffer due to an error */
363 spin_lock(&acm->read_lock); 363 spin_lock(&acm->read_lock);
364 list_add_tail(&rcv->list, &acm->spare_read_urbs); 364 list_add_tail(&rcv->list, &acm->spare_read_urbs);
365 list_add(&buf->list, &acm->spare_read_bufs); 365 list_add(&buf->list, &acm->spare_read_bufs);
366 spin_unlock(&acm->read_lock); 366 spin_unlock(&acm->read_lock);
367 /* nevertheless the tasklet must be kicked unconditionally 367 /* nevertheless the tasklet must be kicked unconditionally
368 so the queue cannot dry up */ 368 so the queue cannot dry up */
369 } 369 }
370 if (likely(!acm->susp_count)) 370 if (likely(!acm->susp_count))
371 tasklet_schedule(&acm->urb_task); 371 tasklet_schedule(&acm->urb_task);
372 } 372 }
373 373
374 static void acm_rx_tasklet(unsigned long _acm) 374 static void acm_rx_tasklet(unsigned long _acm)
375 { 375 {
376 struct acm *acm = (void *)_acm; 376 struct acm *acm = (void *)_acm;
377 struct acm_rb *buf; 377 struct acm_rb *buf;
378 struct tty_struct *tty = acm->tty; 378 struct tty_struct *tty = acm->tty;
379 struct acm_ru *rcv; 379 struct acm_ru *rcv;
380 unsigned long flags; 380 unsigned long flags;
381 unsigned char throttled; 381 unsigned char throttled;
382 382
383 dbg("Entering acm_rx_tasklet"); 383 dbg("Entering acm_rx_tasklet");
384 384
385 if (!ACM_READY(acm)) 385 if (!ACM_READY(acm))
386 { 386 {
387 dbg("acm_rx_tasklet: ACM not ready"); 387 dbg("acm_rx_tasklet: ACM not ready");
388 return; 388 return;
389 } 389 }
390 390
391 spin_lock_irqsave(&acm->throttle_lock, flags); 391 spin_lock_irqsave(&acm->throttle_lock, flags);
392 throttled = acm->throttle; 392 throttled = acm->throttle;
393 spin_unlock_irqrestore(&acm->throttle_lock, flags); 393 spin_unlock_irqrestore(&acm->throttle_lock, flags);
394 if (throttled) 394 if (throttled)
395 { 395 {
396 dbg("acm_rx_tasklet: throttled"); 396 dbg("acm_rx_tasklet: throttled");
397 return; 397 return;
398 } 398 }
399 399
400 next_buffer: 400 next_buffer:
401 spin_lock_irqsave(&acm->read_lock, flags); 401 spin_lock_irqsave(&acm->read_lock, flags);
402 if (list_empty(&acm->filled_read_bufs)) { 402 if (list_empty(&acm->filled_read_bufs)) {
403 spin_unlock_irqrestore(&acm->read_lock, flags); 403 spin_unlock_irqrestore(&acm->read_lock, flags);
404 goto urbs; 404 goto urbs;
405 } 405 }
406 buf = list_entry(acm->filled_read_bufs.next, 406 buf = list_entry(acm->filled_read_bufs.next,
407 struct acm_rb, list); 407 struct acm_rb, list);
408 list_del(&buf->list); 408 list_del(&buf->list);
409 spin_unlock_irqrestore(&acm->read_lock, flags); 409 spin_unlock_irqrestore(&acm->read_lock, flags);
410 410
411 dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size); 411 dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size);
412 412
413 tty_buffer_request_room(tty, buf->size); 413 tty_buffer_request_room(tty, buf->size);
414 spin_lock_irqsave(&acm->throttle_lock, flags); 414 spin_lock_irqsave(&acm->throttle_lock, flags);
415 throttled = acm->throttle; 415 throttled = acm->throttle;
416 spin_unlock_irqrestore(&acm->throttle_lock, flags); 416 spin_unlock_irqrestore(&acm->throttle_lock, flags);
417 if (!throttled) 417 if (!throttled)
418 tty_insert_flip_string(tty, buf->base, buf->size); 418 tty_insert_flip_string(tty, buf->base, buf->size);
419 tty_flip_buffer_push(tty); 419 tty_flip_buffer_push(tty);
420 420
421 if (throttled) { 421 if (throttled) {
422 dbg("Throttling noticed"); 422 dbg("Throttling noticed");
423 spin_lock_irqsave(&acm->read_lock, flags); 423 spin_lock_irqsave(&acm->read_lock, flags);
424 list_add(&buf->list, &acm->filled_read_bufs); 424 list_add(&buf->list, &acm->filled_read_bufs);
425 spin_unlock_irqrestore(&acm->read_lock, flags); 425 spin_unlock_irqrestore(&acm->read_lock, flags);
426 return; 426 return;
427 } 427 }
428 428
429 spin_lock_irqsave(&acm->read_lock, flags); 429 spin_lock_irqsave(&acm->read_lock, flags);
430 list_add(&buf->list, &acm->spare_read_bufs); 430 list_add(&buf->list, &acm->spare_read_bufs);
431 spin_unlock_irqrestore(&acm->read_lock, flags); 431 spin_unlock_irqrestore(&acm->read_lock, flags);
432 goto next_buffer; 432 goto next_buffer;
433 433
434 urbs: 434 urbs:
435 while (!list_empty(&acm->spare_read_bufs)) { 435 while (!list_empty(&acm->spare_read_bufs)) {
436 spin_lock_irqsave(&acm->read_lock, flags); 436 spin_lock_irqsave(&acm->read_lock, flags);
437 if (list_empty(&acm->spare_read_urbs)) { 437 if (list_empty(&acm->spare_read_urbs)) {
438 acm->processing = 0; 438 acm->processing = 0;
439 spin_unlock_irqrestore(&acm->read_lock, flags); 439 spin_unlock_irqrestore(&acm->read_lock, flags);
440 return; 440 return;
441 } 441 }
442 rcv = list_entry(acm->spare_read_urbs.next, 442 rcv = list_entry(acm->spare_read_urbs.next,
443 struct acm_ru, list); 443 struct acm_ru, list);
444 list_del(&rcv->list); 444 list_del(&rcv->list);
445 spin_unlock_irqrestore(&acm->read_lock, flags); 445 spin_unlock_irqrestore(&acm->read_lock, flags);
446 446
447 buf = list_entry(acm->spare_read_bufs.next, 447 buf = list_entry(acm->spare_read_bufs.next,
448 struct acm_rb, list); 448 struct acm_rb, list);
449 list_del(&buf->list); 449 list_del(&buf->list);
450 450
451 rcv->buffer = buf; 451 rcv->buffer = buf;
452 452
453 usb_fill_bulk_urb(rcv->urb, acm->dev, 453 usb_fill_bulk_urb(rcv->urb, acm->dev,
454 acm->rx_endpoint, 454 acm->rx_endpoint,
455 buf->base, 455 buf->base,
456 acm->readsize, 456 acm->readsize,
457 acm_read_bulk, rcv); 457 acm_read_bulk, rcv);
458 rcv->urb->transfer_dma = buf->dma; 458 rcv->urb->transfer_dma = buf->dma;
459 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 459 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
460 460
461 /* This shouldn't kill the driver as unsuccessful URBs are returned to the 461 /* This shouldn't kill the driver as unsuccessful URBs are returned to the
462 free-urbs-pool and resubmited ASAP */ 462 free-urbs-pool and resubmited ASAP */
463 spin_lock_irqsave(&acm->read_lock, flags); 463 spin_lock_irqsave(&acm->read_lock, flags);
464 if (acm->susp_count || usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) { 464 if (acm->susp_count || usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
465 list_add(&buf->list, &acm->spare_read_bufs); 465 list_add(&buf->list, &acm->spare_read_bufs);
466 list_add(&rcv->list, &acm->spare_read_urbs); 466 list_add(&rcv->list, &acm->spare_read_urbs);
467 acm->processing = 0; 467 acm->processing = 0;
468 spin_unlock_irqrestore(&acm->read_lock, flags); 468 spin_unlock_irqrestore(&acm->read_lock, flags);
469 return; 469 return;
470 } else { 470 } else {
471 spin_unlock_irqrestore(&acm->read_lock, flags); 471 spin_unlock_irqrestore(&acm->read_lock, flags);
472 dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf); 472 dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
473 } 473 }
474 } 474 }
475 spin_lock_irqsave(&acm->read_lock, flags); 475 spin_lock_irqsave(&acm->read_lock, flags);
476 acm->processing = 0; 476 acm->processing = 0;
477 spin_unlock_irqrestore(&acm->read_lock, flags); 477 spin_unlock_irqrestore(&acm->read_lock, flags);
478 } 478 }
479 479
480 /* data interface wrote those outgoing bytes */ 480 /* data interface wrote those outgoing bytes */
481 static void acm_write_bulk(struct urb *urb) 481 static void acm_write_bulk(struct urb *urb)
482 { 482 {
483 struct acm_wb *wb = urb->context; 483 struct acm_wb *wb = urb->context;
484 struct acm *acm = wb->instance; 484 struct acm *acm = wb->instance;
485 485
486 if (verbose || urb->status 486 if (verbose || urb->status
487 || (urb->actual_length != urb->transfer_buffer_length)) 487 || (urb->actual_length != urb->transfer_buffer_length))
488 dev_dbg(&acm->data->dev, "tx %d/%d bytes -- > %d\n", 488 dev_dbg(&acm->data->dev, "tx %d/%d bytes -- > %d\n",
489 urb->actual_length, 489 urb->actual_length,
490 urb->transfer_buffer_length, 490 urb->transfer_buffer_length,
491 urb->status); 491 urb->status);
492 492
493 acm_write_done(acm, wb); 493 acm_write_done(acm, wb);
494 if (ACM_READY(acm)) 494 if (ACM_READY(acm))
495 schedule_work(&acm->work); 495 schedule_work(&acm->work);
496 else 496 else
497 wake_up_interruptible(&acm->drain_wait); 497 wake_up_interruptible(&acm->drain_wait);
498 } 498 }
499 499
500 static void acm_softint(struct work_struct *work) 500 static void acm_softint(struct work_struct *work)
501 { 501 {
502 struct acm *acm = container_of(work, struct acm, work); 502 struct acm *acm = container_of(work, struct acm, work);
503 503
504 dev_vdbg(&acm->data->dev, "tx work\n"); 504 dev_vdbg(&acm->data->dev, "tx work\n");
505 if (!ACM_READY(acm)) 505 if (!ACM_READY(acm))
506 return; 506 return;
507 tty_wakeup(acm->tty); 507 tty_wakeup(acm->tty);
508 } 508 }
509 509
510 static void acm_waker(struct work_struct *waker) 510 static void acm_waker(struct work_struct *waker)
511 { 511 {
512 struct acm *acm = container_of(waker, struct acm, waker); 512 struct acm *acm = container_of(waker, struct acm, waker);
513 int rv; 513 int rv;
514 514
515 rv = usb_autopm_get_interface(acm->control); 515 rv = usb_autopm_get_interface(acm->control);
516 if (rv < 0) { 516 if (rv < 0) {
517 err("Autopm failure in %s", __func__); 517 dev_err(&acm->dev->dev, "Autopm failure in %s\n", __func__);
518 return; 518 return;
519 } 519 }
520 if (acm->delayed_wb) { 520 if (acm->delayed_wb) {
521 acm_start_wb(acm, acm->delayed_wb); 521 acm_start_wb(acm, acm->delayed_wb);
522 acm->delayed_wb = NULL; 522 acm->delayed_wb = NULL;
523 } 523 }
524 usb_autopm_put_interface(acm->control); 524 usb_autopm_put_interface(acm->control);
525 } 525 }
526 526
527 /* 527 /*
528 * TTY handlers 528 * TTY handlers
529 */ 529 */
530 530
531 static int acm_tty_open(struct tty_struct *tty, struct file *filp) 531 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
532 { 532 {
533 struct acm *acm; 533 struct acm *acm;
534 int rv = -EINVAL; 534 int rv = -EINVAL;
535 int i; 535 int i;
536 dbg("Entering acm_tty_open."); 536 dbg("Entering acm_tty_open.");
537 537
538 mutex_lock(&open_mutex); 538 mutex_lock(&open_mutex);
539 539
540 acm = acm_table[tty->index]; 540 acm = acm_table[tty->index];
541 if (!acm || !acm->dev) 541 if (!acm || !acm->dev)
542 goto err_out; 542 goto err_out;
543 else 543 else
544 rv = 0; 544 rv = 0;
545 545
546 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags); 546 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
547 tty->driver_data = acm; 547 tty->driver_data = acm;
548 acm->tty = tty; 548 acm->tty = tty;
549 549
550 /* force low_latency on so that our tty_push actually forces the data through, 550 /* force low_latency on so that our tty_push actually forces the data through,
551 otherwise it is scheduled, and with high data rates data can get lost. */ 551 otherwise it is scheduled, and with high data rates data can get lost. */
552 tty->low_latency = 1; 552 tty->low_latency = 1;
553 553
554 if (usb_autopm_get_interface(acm->control) < 0) 554 if (usb_autopm_get_interface(acm->control) < 0)
555 goto early_bail; 555 goto early_bail;
556 else 556 else
557 acm->control->needs_remote_wakeup = 1; 557 acm->control->needs_remote_wakeup = 1;
558 558
559 mutex_lock(&acm->mutex); 559 mutex_lock(&acm->mutex);
560 if (acm->used++) { 560 if (acm->used++) {
561 usb_autopm_put_interface(acm->control); 561 usb_autopm_put_interface(acm->control);
562 goto done; 562 goto done;
563 } 563 }
564 564
565 565
566 acm->ctrlurb->dev = acm->dev; 566 acm->ctrlurb->dev = acm->dev;
567 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) { 567 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
568 dbg("usb_submit_urb(ctrl irq) failed"); 568 dbg("usb_submit_urb(ctrl irq) failed");
569 goto bail_out; 569 goto bail_out;
570 } 570 }
571 571
572 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) && 572 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
573 (acm->ctrl_caps & USB_CDC_CAP_LINE)) 573 (acm->ctrl_caps & USB_CDC_CAP_LINE))
574 goto full_bailout; 574 goto full_bailout;
575 usb_autopm_put_interface(acm->control); 575 usb_autopm_put_interface(acm->control);
576 576
577 INIT_LIST_HEAD(&acm->spare_read_urbs); 577 INIT_LIST_HEAD(&acm->spare_read_urbs);
578 INIT_LIST_HEAD(&acm->spare_read_bufs); 578 INIT_LIST_HEAD(&acm->spare_read_bufs);
579 INIT_LIST_HEAD(&acm->filled_read_bufs); 579 INIT_LIST_HEAD(&acm->filled_read_bufs);
580 for (i = 0; i < acm->rx_buflimit; i++) { 580 for (i = 0; i < acm->rx_buflimit; i++) {
581 list_add(&(acm->ru[i].list), &acm->spare_read_urbs); 581 list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
582 } 582 }
583 for (i = 0; i < acm->rx_buflimit; i++) { 583 for (i = 0; i < acm->rx_buflimit; i++) {
584 list_add(&(acm->rb[i].list), &acm->spare_read_bufs); 584 list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
585 } 585 }
586 586
587 acm->throttle = 0; 587 acm->throttle = 0;
588 588
589 tasklet_schedule(&acm->urb_task); 589 tasklet_schedule(&acm->urb_task);
590 590
591 done: 591 done:
592 mutex_unlock(&acm->mutex); 592 mutex_unlock(&acm->mutex);
593 err_out: 593 err_out:
594 mutex_unlock(&open_mutex); 594 mutex_unlock(&open_mutex);
595 return rv; 595 return rv;
596 596
597 full_bailout: 597 full_bailout:
598 usb_kill_urb(acm->ctrlurb); 598 usb_kill_urb(acm->ctrlurb);
599 bail_out: 599 bail_out:
600 usb_autopm_put_interface(acm->control); 600 usb_autopm_put_interface(acm->control);
601 acm->used--; 601 acm->used--;
602 mutex_unlock(&acm->mutex); 602 mutex_unlock(&acm->mutex);
603 early_bail: 603 early_bail:
604 mutex_unlock(&open_mutex); 604 mutex_unlock(&open_mutex);
605 return -EIO; 605 return -EIO;
606 } 606 }
607 607
608 static void acm_tty_unregister(struct acm *acm) 608 static void acm_tty_unregister(struct acm *acm)
609 { 609 {
610 int i,nr; 610 int i,nr;
611 611
612 nr = acm->rx_buflimit; 612 nr = acm->rx_buflimit;
613 tty_unregister_device(acm_tty_driver, acm->minor); 613 tty_unregister_device(acm_tty_driver, acm->minor);
614 usb_put_intf(acm->control); 614 usb_put_intf(acm->control);
615 acm_table[acm->minor] = NULL; 615 acm_table[acm->minor] = NULL;
616 usb_free_urb(acm->ctrlurb); 616 usb_free_urb(acm->ctrlurb);
617 for (i = 0; i < ACM_NW; i++) 617 for (i = 0; i < ACM_NW; i++)
618 usb_free_urb(acm->wb[i].urb); 618 usb_free_urb(acm->wb[i].urb);
619 for (i = 0; i < nr; i++) 619 for (i = 0; i < nr; i++)
620 usb_free_urb(acm->ru[i].urb); 620 usb_free_urb(acm->ru[i].urb);
621 kfree(acm->country_codes); 621 kfree(acm->country_codes);
622 kfree(acm); 622 kfree(acm);
623 } 623 }
624 624
625 static int acm_tty_chars_in_buffer(struct tty_struct *tty); 625 static int acm_tty_chars_in_buffer(struct tty_struct *tty);
626 626
627 static void acm_tty_close(struct tty_struct *tty, struct file *filp) 627 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
628 { 628 {
629 struct acm *acm = tty->driver_data; 629 struct acm *acm = tty->driver_data;
630 int i,nr; 630 int i,nr;
631 631
632 if (!acm || !acm->used) 632 if (!acm || !acm->used)
633 return; 633 return;
634 634
635 nr = acm->rx_buflimit; 635 nr = acm->rx_buflimit;
636 mutex_lock(&open_mutex); 636 mutex_lock(&open_mutex);
637 if (!--acm->used) { 637 if (!--acm->used) {
638 if (acm->dev) { 638 if (acm->dev) {
639 usb_autopm_get_interface(acm->control); 639 usb_autopm_get_interface(acm->control);
640 acm_set_control(acm, acm->ctrlout = 0); 640 acm_set_control(acm, acm->ctrlout = 0);
641 641
642 /* try letting the last writes drain naturally */ 642 /* try letting the last writes drain naturally */
643 wait_event_interruptible_timeout(acm->drain_wait, 643 wait_event_interruptible_timeout(acm->drain_wait,
644 (ACM_NW == acm_wb_is_avail(acm)) 644 (ACM_NW == acm_wb_is_avail(acm))
645 || !acm->dev, 645 || !acm->dev,
646 ACM_CLOSE_TIMEOUT * HZ); 646 ACM_CLOSE_TIMEOUT * HZ);
647 647
648 usb_kill_urb(acm->ctrlurb); 648 usb_kill_urb(acm->ctrlurb);
649 for (i = 0; i < ACM_NW; i++) 649 for (i = 0; i < ACM_NW; i++)
650 usb_kill_urb(acm->wb[i].urb); 650 usb_kill_urb(acm->wb[i].urb);
651 for (i = 0; i < nr; i++) 651 for (i = 0; i < nr; i++)
652 usb_kill_urb(acm->ru[i].urb); 652 usb_kill_urb(acm->ru[i].urb);
653 acm->control->needs_remote_wakeup = 0; 653 acm->control->needs_remote_wakeup = 0;
654 usb_autopm_put_interface(acm->control); 654 usb_autopm_put_interface(acm->control);
655 } else 655 } else
656 acm_tty_unregister(acm); 656 acm_tty_unregister(acm);
657 } 657 }
658 mutex_unlock(&open_mutex); 658 mutex_unlock(&open_mutex);
659 } 659 }
660 660
661 static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) 661 static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
662 { 662 {
663 struct acm *acm = tty->driver_data; 663 struct acm *acm = tty->driver_data;
664 int stat; 664 int stat;
665 unsigned long flags; 665 unsigned long flags;
666 int wbn; 666 int wbn;
667 struct acm_wb *wb; 667 struct acm_wb *wb;
668 668
669 dbg("Entering acm_tty_write to write %d bytes,", count); 669 dbg("Entering acm_tty_write to write %d bytes,", count);
670 670
671 if (!ACM_READY(acm)) 671 if (!ACM_READY(acm))
672 return -EINVAL; 672 return -EINVAL;
673 if (!count) 673 if (!count)
674 return 0; 674 return 0;
675 675
676 spin_lock_irqsave(&acm->write_lock, flags); 676 spin_lock_irqsave(&acm->write_lock, flags);
677 if ((wbn = acm_wb_alloc(acm)) < 0) { 677 if ((wbn = acm_wb_alloc(acm)) < 0) {
678 spin_unlock_irqrestore(&acm->write_lock, flags); 678 spin_unlock_irqrestore(&acm->write_lock, flags);
679 return 0; 679 return 0;
680 } 680 }
681 wb = &acm->wb[wbn]; 681 wb = &acm->wb[wbn];
682 682
683 count = (count > acm->writesize) ? acm->writesize : count; 683 count = (count > acm->writesize) ? acm->writesize : count;
684 dbg("Get %d bytes...", count); 684 dbg("Get %d bytes...", count);
685 memcpy(wb->buf, buf, count); 685 memcpy(wb->buf, buf, count);
686 wb->len = count; 686 wb->len = count;
687 spin_unlock_irqrestore(&acm->write_lock, flags); 687 spin_unlock_irqrestore(&acm->write_lock, flags);
688 688
689 if ((stat = acm_write_start(acm, wbn)) < 0) 689 if ((stat = acm_write_start(acm, wbn)) < 0)
690 return stat; 690 return stat;
691 return count; 691 return count;
692 } 692 }
693 693
694 static int acm_tty_write_room(struct tty_struct *tty) 694 static int acm_tty_write_room(struct tty_struct *tty)
695 { 695 {
696 struct acm *acm = tty->driver_data; 696 struct acm *acm = tty->driver_data;
697 if (!ACM_READY(acm)) 697 if (!ACM_READY(acm))
698 return -EINVAL; 698 return -EINVAL;
699 /* 699 /*
700 * Do not let the line discipline to know that we have a reserve, 700 * Do not let the line discipline to know that we have a reserve,
701 * or it might get too enthusiastic. 701 * or it might get too enthusiastic.
702 */ 702 */
703 return acm_wb_is_avail(acm) ? acm->writesize : 0; 703 return acm_wb_is_avail(acm) ? acm->writesize : 0;
704 } 704 }
705 705
706 static int acm_tty_chars_in_buffer(struct tty_struct *tty) 706 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
707 { 707 {
708 struct acm *acm = tty->driver_data; 708 struct acm *acm = tty->driver_data;
709 if (!ACM_READY(acm)) 709 if (!ACM_READY(acm))
710 return -EINVAL; 710 return -EINVAL;
711 /* 711 /*
712 * This is inaccurate (overcounts), but it works. 712 * This is inaccurate (overcounts), but it works.
713 */ 713 */
714 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize; 714 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
715 } 715 }
716 716
717 static void acm_tty_throttle(struct tty_struct *tty) 717 static void acm_tty_throttle(struct tty_struct *tty)
718 { 718 {
719 struct acm *acm = tty->driver_data; 719 struct acm *acm = tty->driver_data;
720 if (!ACM_READY(acm)) 720 if (!ACM_READY(acm))
721 return; 721 return;
722 spin_lock_bh(&acm->throttle_lock); 722 spin_lock_bh(&acm->throttle_lock);
723 acm->throttle = 1; 723 acm->throttle = 1;
724 spin_unlock_bh(&acm->throttle_lock); 724 spin_unlock_bh(&acm->throttle_lock);
725 } 725 }
726 726
727 static void acm_tty_unthrottle(struct tty_struct *tty) 727 static void acm_tty_unthrottle(struct tty_struct *tty)
728 { 728 {
729 struct acm *acm = tty->driver_data; 729 struct acm *acm = tty->driver_data;
730 if (!ACM_READY(acm)) 730 if (!ACM_READY(acm))
731 return; 731 return;
732 spin_lock_bh(&acm->throttle_lock); 732 spin_lock_bh(&acm->throttle_lock);
733 acm->throttle = 0; 733 acm->throttle = 0;
734 spin_unlock_bh(&acm->throttle_lock); 734 spin_unlock_bh(&acm->throttle_lock);
735 tasklet_schedule(&acm->urb_task); 735 tasklet_schedule(&acm->urb_task);
736 } 736 }
737 737
738 static int acm_tty_break_ctl(struct tty_struct *tty, int state) 738 static int acm_tty_break_ctl(struct tty_struct *tty, int state)
739 { 739 {
740 struct acm *acm = tty->driver_data; 740 struct acm *acm = tty->driver_data;
741 int retval; 741 int retval;
742 if (!ACM_READY(acm)) 742 if (!ACM_READY(acm))
743 return -EINVAL; 743 return -EINVAL;
744 retval = acm_send_break(acm, state ? 0xffff : 0); 744 retval = acm_send_break(acm, state ? 0xffff : 0);
745 if (retval < 0) 745 if (retval < 0)
746 dbg("send break failed"); 746 dbg("send break failed");
747 return retval; 747 return retval;
748 } 748 }
749 749
750 static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file) 750 static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
751 { 751 {
752 struct acm *acm = tty->driver_data; 752 struct acm *acm = tty->driver_data;
753 753
754 if (!ACM_READY(acm)) 754 if (!ACM_READY(acm))
755 return -EINVAL; 755 return -EINVAL;
756 756
757 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) | 757 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
758 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) | 758 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
759 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) | 759 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
760 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) | 760 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
761 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) | 761 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
762 TIOCM_CTS; 762 TIOCM_CTS;
763 } 763 }
764 764
765 static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file, 765 static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
766 unsigned int set, unsigned int clear) 766 unsigned int set, unsigned int clear)
767 { 767 {
768 struct acm *acm = tty->driver_data; 768 struct acm *acm = tty->driver_data;
769 unsigned int newctrl; 769 unsigned int newctrl;
770 770
771 if (!ACM_READY(acm)) 771 if (!ACM_READY(acm))
772 return -EINVAL; 772 return -EINVAL;
773 773
774 newctrl = acm->ctrlout; 774 newctrl = acm->ctrlout;
775 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0); 775 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
776 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0); 776 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
777 777
778 newctrl = (newctrl & ~clear) | set; 778 newctrl = (newctrl & ~clear) | set;
779 779
780 if (acm->ctrlout == newctrl) 780 if (acm->ctrlout == newctrl)
781 return 0; 781 return 0;
782 return acm_set_control(acm, acm->ctrlout = newctrl); 782 return acm_set_control(acm, acm->ctrlout = newctrl);
783 } 783 }
784 784
785 static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg) 785 static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
786 { 786 {
787 struct acm *acm = tty->driver_data; 787 struct acm *acm = tty->driver_data;
788 788
789 if (!ACM_READY(acm)) 789 if (!ACM_READY(acm))
790 return -EINVAL; 790 return -EINVAL;
791 791
792 return -ENOIOCTLCMD; 792 return -ENOIOCTLCMD;
793 } 793 }
794 794
795 static const __u32 acm_tty_speed[] = { 795 static const __u32 acm_tty_speed[] = {
796 0, 50, 75, 110, 134, 150, 200, 300, 600, 796 0, 50, 75, 110, 134, 150, 200, 300, 600,
797 1200, 1800, 2400, 4800, 9600, 19200, 38400, 797 1200, 1800, 2400, 4800, 9600, 19200, 38400,
798 57600, 115200, 230400, 460800, 500000, 576000, 798 57600, 115200, 230400, 460800, 500000, 576000,
799 921600, 1000000, 1152000, 1500000, 2000000, 799 921600, 1000000, 1152000, 1500000, 2000000,
800 2500000, 3000000, 3500000, 4000000 800 2500000, 3000000, 3500000, 4000000
801 }; 801 };
802 802
803 static const __u8 acm_tty_size[] = { 803 static const __u8 acm_tty_size[] = {
804 5, 6, 7, 8 804 5, 6, 7, 8
805 }; 805 };
806 806
807 static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old) 807 static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old)
808 { 808 {
809 struct acm *acm = tty->driver_data; 809 struct acm *acm = tty->driver_data;
810 struct ktermios *termios = tty->termios; 810 struct ktermios *termios = tty->termios;
811 struct usb_cdc_line_coding newline; 811 struct usb_cdc_line_coding newline;
812 int newctrl = acm->ctrlout; 812 int newctrl = acm->ctrlout;
813 813
814 if (!ACM_READY(acm)) 814 if (!ACM_READY(acm))
815 return; 815 return;
816 816
817 newline.dwDTERate = cpu_to_le32p(acm_tty_speed + 817 newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
818 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0)); 818 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
819 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0; 819 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
820 newline.bParityType = termios->c_cflag & PARENB ? 820 newline.bParityType = termios->c_cflag & PARENB ?
821 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0; 821 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
822 newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4]; 822 newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
823 823
824 acm->clocal = ((termios->c_cflag & CLOCAL) != 0); 824 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
825 825
826 if (!newline.dwDTERate) { 826 if (!newline.dwDTERate) {
827 newline.dwDTERate = acm->line.dwDTERate; 827 newline.dwDTERate = acm->line.dwDTERate;
828 newctrl &= ~ACM_CTRL_DTR; 828 newctrl &= ~ACM_CTRL_DTR;
829 } else newctrl |= ACM_CTRL_DTR; 829 } else newctrl |= ACM_CTRL_DTR;
830 830
831 if (newctrl != acm->ctrlout) 831 if (newctrl != acm->ctrlout)
832 acm_set_control(acm, acm->ctrlout = newctrl); 832 acm_set_control(acm, acm->ctrlout = newctrl);
833 833
834 if (memcmp(&acm->line, &newline, sizeof newline)) { 834 if (memcmp(&acm->line, &newline, sizeof newline)) {
835 memcpy(&acm->line, &newline, sizeof newline); 835 memcpy(&acm->line, &newline, sizeof newline);
836 dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate), 836 dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
837 newline.bCharFormat, newline.bParityType, 837 newline.bCharFormat, newline.bParityType,
838 newline.bDataBits); 838 newline.bDataBits);
839 acm_set_line(acm, &acm->line); 839 acm_set_line(acm, &acm->line);
840 } 840 }
841 } 841 }
842 842
843 /* 843 /*
844 * USB probe and disconnect routines. 844 * USB probe and disconnect routines.
845 */ 845 */
846 846
847 /* Little helpers: write/read buffers free */ 847 /* Little helpers: write/read buffers free */
848 static void acm_write_buffers_free(struct acm *acm) 848 static void acm_write_buffers_free(struct acm *acm)
849 { 849 {
850 int i; 850 int i;
851 struct acm_wb *wb; 851 struct acm_wb *wb;
852 852
853 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) { 853 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
854 usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah); 854 usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah);
855 } 855 }
856 } 856 }
857 857
858 static void acm_read_buffers_free(struct acm *acm) 858 static void acm_read_buffers_free(struct acm *acm)
859 { 859 {
860 struct usb_device *usb_dev = interface_to_usbdev(acm->control); 860 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
861 int i, n = acm->rx_buflimit; 861 int i, n = acm->rx_buflimit;
862 862
863 for (i = 0; i < n; i++) 863 for (i = 0; i < n; i++)
864 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma); 864 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
865 } 865 }
866 866
867 /* Little helper: write buffers allocate */ 867 /* Little helper: write buffers allocate */
868 static int acm_write_buffers_alloc(struct acm *acm) 868 static int acm_write_buffers_alloc(struct acm *acm)
869 { 869 {
870 int i; 870 int i;
871 struct acm_wb *wb; 871 struct acm_wb *wb;
872 872
873 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) { 873 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
874 wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL, 874 wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
875 &wb->dmah); 875 &wb->dmah);
876 if (!wb->buf) { 876 if (!wb->buf) {
877 while (i != 0) { 877 while (i != 0) {
878 --i; 878 --i;
879 --wb; 879 --wb;
880 usb_buffer_free(acm->dev, acm->writesize, 880 usb_buffer_free(acm->dev, acm->writesize,
881 wb->buf, wb->dmah); 881 wb->buf, wb->dmah);
882 } 882 }
883 return -ENOMEM; 883 return -ENOMEM;
884 } 884 }
885 } 885 }
886 return 0; 886 return 0;
887 } 887 }
888 888
889 static int acm_probe (struct usb_interface *intf, 889 static int acm_probe (struct usb_interface *intf,
890 const struct usb_device_id *id) 890 const struct usb_device_id *id)
891 { 891 {
892 struct usb_cdc_union_desc *union_header = NULL; 892 struct usb_cdc_union_desc *union_header = NULL;
893 struct usb_cdc_country_functional_desc *cfd = NULL; 893 struct usb_cdc_country_functional_desc *cfd = NULL;
894 unsigned char *buffer = intf->altsetting->extra; 894 unsigned char *buffer = intf->altsetting->extra;
895 int buflen = intf->altsetting->extralen; 895 int buflen = intf->altsetting->extralen;
896 struct usb_interface *control_interface; 896 struct usb_interface *control_interface;
897 struct usb_interface *data_interface; 897 struct usb_interface *data_interface;
898 struct usb_endpoint_descriptor *epctrl; 898 struct usb_endpoint_descriptor *epctrl;
899 struct usb_endpoint_descriptor *epread; 899 struct usb_endpoint_descriptor *epread;
900 struct usb_endpoint_descriptor *epwrite; 900 struct usb_endpoint_descriptor *epwrite;
901 struct usb_device *usb_dev = interface_to_usbdev(intf); 901 struct usb_device *usb_dev = interface_to_usbdev(intf);
902 struct acm *acm; 902 struct acm *acm;
903 int minor; 903 int minor;
904 int ctrlsize,readsize; 904 int ctrlsize,readsize;
905 u8 *buf; 905 u8 *buf;
906 u8 ac_management_function = 0; 906 u8 ac_management_function = 0;
907 u8 call_management_function = 0; 907 u8 call_management_function = 0;
908 int call_interface_num = -1; 908 int call_interface_num = -1;
909 int data_interface_num; 909 int data_interface_num;
910 unsigned long quirks; 910 unsigned long quirks;
911 int num_rx_buf; 911 int num_rx_buf;
912 int i; 912 int i;
913 913
914 /* normal quirks */ 914 /* normal quirks */
915 quirks = (unsigned long)id->driver_info; 915 quirks = (unsigned long)id->driver_info;
916 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR; 916 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
917 917
918 /* handle quirks deadly to normal probing*/ 918 /* handle quirks deadly to normal probing*/
919 if (quirks == NO_UNION_NORMAL) { 919 if (quirks == NO_UNION_NORMAL) {
920 data_interface = usb_ifnum_to_if(usb_dev, 1); 920 data_interface = usb_ifnum_to_if(usb_dev, 1);
921 control_interface = usb_ifnum_to_if(usb_dev, 0); 921 control_interface = usb_ifnum_to_if(usb_dev, 0);
922 goto skip_normal_probe; 922 goto skip_normal_probe;
923 } 923 }
924 924
925 /* normal probing*/ 925 /* normal probing*/
926 if (!buffer) { 926 if (!buffer) {
927 err("Weird descriptor references\n"); 927 dev_err(&intf->dev, "Weird descriptor references\n");
928 return -EINVAL; 928 return -EINVAL;
929 } 929 }
930 930
931 if (!buflen) { 931 if (!buflen) {
932 if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) { 932 if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
933 dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n"); 933 dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
934 buflen = intf->cur_altsetting->endpoint->extralen; 934 buflen = intf->cur_altsetting->endpoint->extralen;
935 buffer = intf->cur_altsetting->endpoint->extra; 935 buffer = intf->cur_altsetting->endpoint->extra;
936 } else { 936 } else {
937 err("Zero length descriptor references\n"); 937 dev_err(&intf->dev,
938 "Zero length descriptor references\n");
938 return -EINVAL; 939 return -EINVAL;
939 } 940 }
940 } 941 }
941 942
942 while (buflen > 0) { 943 while (buflen > 0) {
943 if (buffer [1] != USB_DT_CS_INTERFACE) { 944 if (buffer [1] != USB_DT_CS_INTERFACE) {
944 err("skipping garbage\n"); 945 dev_err(&intf->dev, "skipping garbage\n");
945 goto next_desc; 946 goto next_desc;
946 } 947 }
947 948
948 switch (buffer [2]) { 949 switch (buffer [2]) {
949 case USB_CDC_UNION_TYPE: /* we've found it */ 950 case USB_CDC_UNION_TYPE: /* we've found it */
950 if (union_header) { 951 if (union_header) {
951 err("More than one union descriptor, skipping ..."); 952 dev_err(&intf->dev, "More than one "
953 "union descriptor, "
954 "skipping ...\n");
952 goto next_desc; 955 goto next_desc;
953 } 956 }
954 union_header = (struct usb_cdc_union_desc *) 957 union_header = (struct usb_cdc_union_desc *)
955 buffer; 958 buffer;
956 break; 959 break;
957 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/ 960 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
958 cfd = (struct usb_cdc_country_functional_desc *)buffer; 961 cfd = (struct usb_cdc_country_functional_desc *)buffer;
959 break; 962 break;
960 case USB_CDC_HEADER_TYPE: /* maybe check version */ 963 case USB_CDC_HEADER_TYPE: /* maybe check version */
961 break; /* for now we ignore it */ 964 break; /* for now we ignore it */
962 case USB_CDC_ACM_TYPE: 965 case USB_CDC_ACM_TYPE:
963 ac_management_function = buffer[3]; 966 ac_management_function = buffer[3];
964 break; 967 break;
965 case USB_CDC_CALL_MANAGEMENT_TYPE: 968 case USB_CDC_CALL_MANAGEMENT_TYPE:
966 call_management_function = buffer[3]; 969 call_management_function = buffer[3];
967 call_interface_num = buffer[4]; 970 call_interface_num = buffer[4];
968 if ((call_management_function & 3) != 3) 971 if ((call_management_function & 3) != 3)
969 err("This device cannot do calls on its own. It is no modem."); 972 dev_err(&intf->dev, "This device "
973 "cannot do calls on its own. "
974 "It is no modem.\n");
970 break; 975 break;
971 default: 976 default:
972 /* there are LOTS more CDC descriptors that 977 /* there are LOTS more CDC descriptors that
973 * could legitimately be found here. 978 * could legitimately be found here.
974 */ 979 */
975 dev_dbg(&intf->dev, "Ignoring descriptor: " 980 dev_dbg(&intf->dev, "Ignoring descriptor: "
976 "type %02x, length %d\n", 981 "type %02x, length %d\n",
977 buffer[2], buffer[0]); 982 buffer[2], buffer[0]);
978 break; 983 break;
979 } 984 }
980 next_desc: 985 next_desc:
981 buflen -= buffer[0]; 986 buflen -= buffer[0];
982 buffer += buffer[0]; 987 buffer += buffer[0];
983 } 988 }
984 989
985 if (!union_header) { 990 if (!union_header) {
986 if (call_interface_num > 0) { 991 if (call_interface_num > 0) {
987 dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n"); 992 dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
988 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num)); 993 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
989 control_interface = intf; 994 control_interface = intf;
990 } else { 995 } else {
991 dev_dbg(&intf->dev,"No union descriptor, giving up\n"); 996 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
992 return -ENODEV; 997 return -ENODEV;
993 } 998 }
994 } else { 999 } else {
995 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0); 1000 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
996 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0)); 1001 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
997 if (!control_interface || !data_interface) { 1002 if (!control_interface || !data_interface) {
998 dev_dbg(&intf->dev,"no interfaces\n"); 1003 dev_dbg(&intf->dev,"no interfaces\n");
999 return -ENODEV; 1004 return -ENODEV;
1000 } 1005 }
1001 } 1006 }
1002 1007
1003 if (data_interface_num != call_interface_num) 1008 if (data_interface_num != call_interface_num)
1004 dev_dbg(&intf->dev,"Separate call control interface. That is not fully supported.\n"); 1009 dev_dbg(&intf->dev,"Separate call control interface. That is not fully supported.\n");
1005 1010
1006 skip_normal_probe: 1011 skip_normal_probe:
1007 1012
1008 /*workaround for switched interfaces */ 1013 /*workaround for switched interfaces */
1009 if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) { 1014 if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
1010 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) { 1015 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
1011 struct usb_interface *t; 1016 struct usb_interface *t;
1012 dev_dbg(&intf->dev,"Your device has switched interfaces.\n"); 1017 dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
1013 1018
1014 t = control_interface; 1019 t = control_interface;
1015 control_interface = data_interface; 1020 control_interface = data_interface;
1016 data_interface = t; 1021 data_interface = t;
1017 } else { 1022 } else {
1018 return -EINVAL; 1023 return -EINVAL;
1019 } 1024 }
1020 } 1025 }
1021 1026
1022 /* Accept probe requests only for the control interface */ 1027 /* Accept probe requests only for the control interface */
1023 if (intf != control_interface) 1028 if (intf != control_interface)
1024 return -ENODEV; 1029 return -ENODEV;
1025 1030
1026 if (usb_interface_claimed(data_interface)) { /* valid in this context */ 1031 if (usb_interface_claimed(data_interface)) { /* valid in this context */
1027 dev_dbg(&intf->dev,"The data interface isn't available\n"); 1032 dev_dbg(&intf->dev,"The data interface isn't available\n");
1028 return -EBUSY; 1033 return -EBUSY;
1029 } 1034 }
1030 1035
1031 1036
1032 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2) 1037 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
1033 return -EINVAL; 1038 return -EINVAL;
1034 1039
1035 epctrl = &control_interface->cur_altsetting->endpoint[0].desc; 1040 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1036 epread = &data_interface->cur_altsetting->endpoint[0].desc; 1041 epread = &data_interface->cur_altsetting->endpoint[0].desc;
1037 epwrite = &data_interface->cur_altsetting->endpoint[1].desc; 1042 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1038 1043
1039 1044
1040 /* workaround for switched endpoints */ 1045 /* workaround for switched endpoints */
1041 if (!usb_endpoint_dir_in(epread)) { 1046 if (!usb_endpoint_dir_in(epread)) {
1042 /* descriptors are swapped */ 1047 /* descriptors are swapped */
1043 struct usb_endpoint_descriptor *t; 1048 struct usb_endpoint_descriptor *t;
1044 dev_dbg(&intf->dev,"The data interface has switched endpoints\n"); 1049 dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
1045 1050
1046 t = epread; 1051 t = epread;
1047 epread = epwrite; 1052 epread = epwrite;
1048 epwrite = t; 1053 epwrite = t;
1049 } 1054 }
1050 dbg("interfaces are valid"); 1055 dbg("interfaces are valid");
1051 for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++); 1056 for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
1052 1057
1053 if (minor == ACM_TTY_MINORS) { 1058 if (minor == ACM_TTY_MINORS) {
1054 err("no more free acm devices"); 1059 dev_err(&intf->dev, "no more free acm devices\n");
1055 return -ENODEV; 1060 return -ENODEV;
1056 } 1061 }
1057 1062
1058 if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) { 1063 if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) {
1059 dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n"); 1064 dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
1060 goto alloc_fail; 1065 goto alloc_fail;
1061 } 1066 }
1062 1067
1063 ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize); 1068 ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
1064 readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2); 1069 readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2);
1065 acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20; 1070 acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20;
1066 acm->control = control_interface; 1071 acm->control = control_interface;
1067 acm->data = data_interface; 1072 acm->data = data_interface;
1068 acm->minor = minor; 1073 acm->minor = minor;
1069 acm->dev = usb_dev; 1074 acm->dev = usb_dev;
1070 acm->ctrl_caps = ac_management_function; 1075 acm->ctrl_caps = ac_management_function;
1071 acm->ctrlsize = ctrlsize; 1076 acm->ctrlsize = ctrlsize;
1072 acm->readsize = readsize; 1077 acm->readsize = readsize;
1073 acm->rx_buflimit = num_rx_buf; 1078 acm->rx_buflimit = num_rx_buf;
1074 acm->urb_task.func = acm_rx_tasklet; 1079 acm->urb_task.func = acm_rx_tasklet;
1075 acm->urb_task.data = (unsigned long) acm; 1080 acm->urb_task.data = (unsigned long) acm;
1076 INIT_WORK(&acm->work, acm_softint); 1081 INIT_WORK(&acm->work, acm_softint);
1077 INIT_WORK(&acm->waker, acm_waker); 1082 INIT_WORK(&acm->waker, acm_waker);
1078 init_waitqueue_head(&acm->drain_wait); 1083 init_waitqueue_head(&acm->drain_wait);
1079 spin_lock_init(&acm->throttle_lock); 1084 spin_lock_init(&acm->throttle_lock);
1080 spin_lock_init(&acm->write_lock); 1085 spin_lock_init(&acm->write_lock);
1081 spin_lock_init(&acm->read_lock); 1086 spin_lock_init(&acm->read_lock);
1082 mutex_init(&acm->mutex); 1087 mutex_init(&acm->mutex);
1083 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress); 1088 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1084 1089
1085 buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma); 1090 buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
1086 if (!buf) { 1091 if (!buf) {
1087 dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n"); 1092 dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
1088 goto alloc_fail2; 1093 goto alloc_fail2;
1089 } 1094 }
1090 acm->ctrl_buffer = buf; 1095 acm->ctrl_buffer = buf;
1091 1096
1092 if (acm_write_buffers_alloc(acm) < 0) { 1097 if (acm_write_buffers_alloc(acm) < 0) {
1093 dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n"); 1098 dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
1094 goto alloc_fail4; 1099 goto alloc_fail4;
1095 } 1100 }
1096 1101
1097 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL); 1102 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1098 if (!acm->ctrlurb) { 1103 if (!acm->ctrlurb) {
1099 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n"); 1104 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
1100 goto alloc_fail5; 1105 goto alloc_fail5;
1101 } 1106 }
1102 for (i = 0; i < num_rx_buf; i++) { 1107 for (i = 0; i < num_rx_buf; i++) {
1103 struct acm_ru *rcv = &(acm->ru[i]); 1108 struct acm_ru *rcv = &(acm->ru[i]);
1104 1109
1105 if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) { 1110 if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) {
1106 dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n"); 1111 dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n");
1107 goto alloc_fail7; 1112 goto alloc_fail7;
1108 } 1113 }
1109 1114
1110 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1115 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1111 rcv->instance = acm; 1116 rcv->instance = acm;
1112 } 1117 }
1113 for (i = 0; i < num_rx_buf; i++) { 1118 for (i = 0; i < num_rx_buf; i++) {
1114 struct acm_rb *rb = &(acm->rb[i]); 1119 struct acm_rb *rb = &(acm->rb[i]);
1115 1120
1116 rb->base = usb_buffer_alloc(acm->dev, readsize, 1121 rb->base = usb_buffer_alloc(acm->dev, readsize,
1117 GFP_KERNEL, &rb->dma); 1122 GFP_KERNEL, &rb->dma);
1118 if (!rb->base) { 1123 if (!rb->base) {
1119 dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n"); 1124 dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n");
1120 goto alloc_fail7; 1125 goto alloc_fail7;
1121 } 1126 }
1122 } 1127 }
1123 for(i = 0; i < ACM_NW; i++) 1128 for(i = 0; i < ACM_NW; i++)
1124 { 1129 {
1125 struct acm_wb *snd = &(acm->wb[i]); 1130 struct acm_wb *snd = &(acm->wb[i]);
1126 1131
1127 if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) { 1132 if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) {
1128 dev_dbg(&intf->dev, "out of memory (write urbs usb_alloc_urb)"); 1133 dev_dbg(&intf->dev, "out of memory (write urbs usb_alloc_urb)");
1129 goto alloc_fail7; 1134 goto alloc_fail7;
1130 } 1135 }
1131 1136
1132 usb_fill_bulk_urb(snd->urb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress), 1137 usb_fill_bulk_urb(snd->urb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1133 NULL, acm->writesize, acm_write_bulk, snd); 1138 NULL, acm->writesize, acm_write_bulk, snd);
1134 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1139 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1135 snd->instance = acm; 1140 snd->instance = acm;
1136 } 1141 }
1137 1142
1138 usb_set_intfdata (intf, acm); 1143 usb_set_intfdata (intf, acm);
1139 1144
1140 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities); 1145 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1141 if (i < 0) 1146 if (i < 0)
1142 goto alloc_fail8; 1147 goto alloc_fail8;
1143 1148
1144 if (cfd) { /* export the country data */ 1149 if (cfd) { /* export the country data */
1145 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL); 1150 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1146 if (!acm->country_codes) 1151 if (!acm->country_codes)
1147 goto skip_countries; 1152 goto skip_countries;
1148 acm->country_code_size = cfd->bLength - 4; 1153 acm->country_code_size = cfd->bLength - 4;
1149 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4); 1154 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4);
1150 acm->country_rel_date = cfd->iCountryCodeRelDate; 1155 acm->country_rel_date = cfd->iCountryCodeRelDate;
1151 1156
1152 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes); 1157 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1153 if (i < 0) { 1158 if (i < 0) {
1154 kfree(acm->country_codes); 1159 kfree(acm->country_codes);
1155 goto skip_countries; 1160 goto skip_countries;
1156 } 1161 }
1157 1162
1158 i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate); 1163 i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate);
1159 if (i < 0) { 1164 if (i < 0) {
1160 kfree(acm->country_codes); 1165 kfree(acm->country_codes);
1161 goto skip_countries; 1166 goto skip_countries;
1162 } 1167 }
1163 } 1168 }
1164 1169
1165 skip_countries: 1170 skip_countries:
1166 usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress), 1171 usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1167 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval); 1172 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
1168 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1173 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1169 acm->ctrlurb->transfer_dma = acm->ctrl_dma; 1174 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1170 1175
1171 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor); 1176 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1172 1177
1173 acm_set_control(acm, acm->ctrlout); 1178 acm_set_control(acm, acm->ctrlout);
1174 1179
1175 acm->line.dwDTERate = cpu_to_le32(9600); 1180 acm->line.dwDTERate = cpu_to_le32(9600);
1176 acm->line.bDataBits = 8; 1181 acm->line.bDataBits = 8;
1177 acm_set_line(acm, &acm->line); 1182 acm_set_line(acm, &acm->line);
1178 1183
1179 usb_driver_claim_interface(&acm_driver, data_interface, acm); 1184 usb_driver_claim_interface(&acm_driver, data_interface, acm);
1180 usb_set_intfdata(data_interface, acm); 1185 usb_set_intfdata(data_interface, acm);
1181 1186
1182 usb_get_intf(control_interface); 1187 usb_get_intf(control_interface);
1183 tty_register_device(acm_tty_driver, minor, &control_interface->dev); 1188 tty_register_device(acm_tty_driver, minor, &control_interface->dev);
1184 1189
1185 acm_table[minor] = acm; 1190 acm_table[minor] = acm;
1186 1191
1187 return 0; 1192 return 0;
1188 alloc_fail8: 1193 alloc_fail8:
1189 for (i = 0; i < ACM_NW; i++) 1194 for (i = 0; i < ACM_NW; i++)
1190 usb_free_urb(acm->wb[i].urb); 1195 usb_free_urb(acm->wb[i].urb);
1191 alloc_fail7: 1196 alloc_fail7:
1192 acm_read_buffers_free(acm); 1197 acm_read_buffers_free(acm);
1193 for (i = 0; i < num_rx_buf; i++) 1198 for (i = 0; i < num_rx_buf; i++)
1194 usb_free_urb(acm->ru[i].urb); 1199 usb_free_urb(acm->ru[i].urb);
1195 usb_free_urb(acm->ctrlurb); 1200 usb_free_urb(acm->ctrlurb);
1196 alloc_fail5: 1201 alloc_fail5:
1197 acm_write_buffers_free(acm); 1202 acm_write_buffers_free(acm);
1198 alloc_fail4: 1203 alloc_fail4:
1199 usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma); 1204 usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1200 alloc_fail2: 1205 alloc_fail2:
1201 kfree(acm); 1206 kfree(acm);
1202 alloc_fail: 1207 alloc_fail:
1203 return -ENOMEM; 1208 return -ENOMEM;
1204 } 1209 }
1205 1210
1206 static void stop_data_traffic(struct acm *acm) 1211 static void stop_data_traffic(struct acm *acm)
1207 { 1212 {
1208 int i; 1213 int i;
1209 dbg("Entering stop_data_traffic"); 1214 dbg("Entering stop_data_traffic");
1210 1215
1211 tasklet_disable(&acm->urb_task); 1216 tasklet_disable(&acm->urb_task);
1212 1217
1213 usb_kill_urb(acm->ctrlurb); 1218 usb_kill_urb(acm->ctrlurb);
1214 for(i = 0; i < ACM_NW; i++) 1219 for(i = 0; i < ACM_NW; i++)
1215 usb_kill_urb(acm->wb[i].urb); 1220 usb_kill_urb(acm->wb[i].urb);
1216 for (i = 0; i < acm->rx_buflimit; i++) 1221 for (i = 0; i < acm->rx_buflimit; i++)
1217 usb_kill_urb(acm->ru[i].urb); 1222 usb_kill_urb(acm->ru[i].urb);
1218 1223
1219 tasklet_enable(&acm->urb_task); 1224 tasklet_enable(&acm->urb_task);
1220 1225
1221 cancel_work_sync(&acm->work); 1226 cancel_work_sync(&acm->work);
1222 cancel_work_sync(&acm->waker); 1227 cancel_work_sync(&acm->waker);
1223 } 1228 }
1224 1229
1225 static void acm_disconnect(struct usb_interface *intf) 1230 static void acm_disconnect(struct usb_interface *intf)
1226 { 1231 {
1227 struct acm *acm = usb_get_intfdata(intf); 1232 struct acm *acm = usb_get_intfdata(intf);
1228 struct usb_device *usb_dev = interface_to_usbdev(intf); 1233 struct usb_device *usb_dev = interface_to_usbdev(intf);
1229 1234
1230 /* sibling interface is already cleaning up */ 1235 /* sibling interface is already cleaning up */
1231 if (!acm) 1236 if (!acm)
1232 return; 1237 return;
1233 1238
1234 mutex_lock(&open_mutex); 1239 mutex_lock(&open_mutex);
1235 if (acm->country_codes){ 1240 if (acm->country_codes){
1236 device_remove_file(&acm->control->dev, 1241 device_remove_file(&acm->control->dev,
1237 &dev_attr_wCountryCodes); 1242 &dev_attr_wCountryCodes);
1238 device_remove_file(&acm->control->dev, 1243 device_remove_file(&acm->control->dev,
1239 &dev_attr_iCountryCodeRelDate); 1244 &dev_attr_iCountryCodeRelDate);
1240 } 1245 }
1241 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities); 1246 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1242 acm->dev = NULL; 1247 acm->dev = NULL;
1243 usb_set_intfdata(acm->control, NULL); 1248 usb_set_intfdata(acm->control, NULL);
1244 usb_set_intfdata(acm->data, NULL); 1249 usb_set_intfdata(acm->data, NULL);
1245 1250
1246 stop_data_traffic(acm); 1251 stop_data_traffic(acm);
1247 1252
1248 acm_write_buffers_free(acm); 1253 acm_write_buffers_free(acm);
1249 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma); 1254 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1250 acm_read_buffers_free(acm); 1255 acm_read_buffers_free(acm);
1251 1256
1252 usb_driver_release_interface(&acm_driver, intf == acm->control ? 1257 usb_driver_release_interface(&acm_driver, intf == acm->control ?
1253 acm->data : acm->control); 1258 acm->data : acm->control);
1254 1259
1255 if (!acm->used) { 1260 if (!acm->used) {
1256 acm_tty_unregister(acm); 1261 acm_tty_unregister(acm);
1257 mutex_unlock(&open_mutex); 1262 mutex_unlock(&open_mutex);
1258 return; 1263 return;
1259 } 1264 }
1260 1265
1261 mutex_unlock(&open_mutex); 1266 mutex_unlock(&open_mutex);
1262 1267
1263 if (acm->tty) 1268 if (acm->tty)
1264 tty_hangup(acm->tty); 1269 tty_hangup(acm->tty);
1265 } 1270 }
1266 1271
1267 #ifdef CONFIG_PM 1272 #ifdef CONFIG_PM
1268 static int acm_suspend(struct usb_interface *intf, pm_message_t message) 1273 static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1269 { 1274 {
1270 struct acm *acm = usb_get_intfdata(intf); 1275 struct acm *acm = usb_get_intfdata(intf);
1271 int cnt; 1276 int cnt;
1272 1277
1273 if (acm->dev->auto_pm) { 1278 if (acm->dev->auto_pm) {
1274 int b; 1279 int b;
1275 1280
1276 spin_lock_irq(&acm->read_lock); 1281 spin_lock_irq(&acm->read_lock);
1277 spin_lock(&acm->write_lock); 1282 spin_lock(&acm->write_lock);
1278 b = acm->processing + acm->transmitting; 1283 b = acm->processing + acm->transmitting;
1279 spin_unlock(&acm->write_lock); 1284 spin_unlock(&acm->write_lock);
1280 spin_unlock_irq(&acm->read_lock); 1285 spin_unlock_irq(&acm->read_lock);
1281 if (b) 1286 if (b)
1282 return -EBUSY; 1287 return -EBUSY;
1283 } 1288 }
1284 1289
1285 spin_lock_irq(&acm->read_lock); 1290 spin_lock_irq(&acm->read_lock);
1286 spin_lock(&acm->write_lock); 1291 spin_lock(&acm->write_lock);
1287 cnt = acm->susp_count++; 1292 cnt = acm->susp_count++;
1288 spin_unlock(&acm->write_lock); 1293 spin_unlock(&acm->write_lock);
1289 spin_unlock_irq(&acm->read_lock); 1294 spin_unlock_irq(&acm->read_lock);
1290 1295
1291 if (cnt) 1296 if (cnt)
1292 return 0; 1297 return 0;
1293 /* 1298 /*
1294 we treat opened interfaces differently, 1299 we treat opened interfaces differently,
1295 we must guard against open 1300 we must guard against open
1296 */ 1301 */
1297 mutex_lock(&acm->mutex); 1302 mutex_lock(&acm->mutex);
1298 1303
1299 if (acm->used) 1304 if (acm->used)
1300 stop_data_traffic(acm); 1305 stop_data_traffic(acm);
1301 1306
1302 mutex_unlock(&acm->mutex); 1307 mutex_unlock(&acm->mutex);
1303 return 0; 1308 return 0;
1304 } 1309 }
1305 1310
1306 static int acm_resume(struct usb_interface *intf) 1311 static int acm_resume(struct usb_interface *intf)
1307 { 1312 {
1308 struct acm *acm = usb_get_intfdata(intf); 1313 struct acm *acm = usb_get_intfdata(intf);
1309 int rv = 0; 1314 int rv = 0;
1310 int cnt; 1315 int cnt;
1311 1316
1312 spin_lock_irq(&acm->read_lock); 1317 spin_lock_irq(&acm->read_lock);
1313 acm->susp_count -= 1; 1318 acm->susp_count -= 1;
1314 cnt = acm->susp_count; 1319 cnt = acm->susp_count;
1315 spin_unlock_irq(&acm->read_lock); 1320 spin_unlock_irq(&acm->read_lock);
1316 1321
1317 if (cnt) 1322 if (cnt)
1318 return 0; 1323 return 0;
1319 1324
1320 mutex_lock(&acm->mutex); 1325 mutex_lock(&acm->mutex);
1321 if (acm->used) { 1326 if (acm->used) {
1322 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO); 1327 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
1323 if (rv < 0) 1328 if (rv < 0)
1324 goto err_out; 1329 goto err_out;
1325 1330
1326 tasklet_schedule(&acm->urb_task); 1331 tasklet_schedule(&acm->urb_task);
1327 } 1332 }
1328 1333
1329 err_out: 1334 err_out:
1330 mutex_unlock(&acm->mutex); 1335 mutex_unlock(&acm->mutex);
1331 return rv; 1336 return rv;
1332 } 1337 }
1333 1338
1334 #endif /* CONFIG_PM */ 1339 #endif /* CONFIG_PM */
1335 /* 1340 /*
1336 * USB driver structure. 1341 * USB driver structure.
1337 */ 1342 */
1338 1343
1339 static struct usb_device_id acm_ids[] = { 1344 static struct usb_device_id acm_ids[] = {
1340 /* quirky and broken devices */ 1345 /* quirky and broken devices */
1341 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */ 1346 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1342 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1347 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1343 }, 1348 },
1344 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */ 1349 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1345 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1350 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1346 }, 1351 },
1347 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */ 1352 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1348 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1353 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1349 }, 1354 },
1350 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */ 1355 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1351 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1356 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1352 }, 1357 },
1353 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */ 1358 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1354 .driver_info = SINGLE_RX_URB, /* firmware bug */ 1359 .driver_info = SINGLE_RX_URB, /* firmware bug */
1355 }, 1360 },
1356 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */ 1361 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1357 .driver_info = SINGLE_RX_URB, /* firmware bug */ 1362 .driver_info = SINGLE_RX_URB, /* firmware bug */
1358 }, 1363 },
1359 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */ 1364 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1360 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1365 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1361 }, 1366 },
1362 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */ 1367 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1363 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1368 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1364 }, 1369 },
1365 { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */ 1370 { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
1366 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1371 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1367 }, 1372 },
1368 1373
1369 /* control interfaces with various AT-command sets */ 1374 /* control interfaces with various AT-command sets */
1370 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1375 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1371 USB_CDC_ACM_PROTO_AT_V25TER) }, 1376 USB_CDC_ACM_PROTO_AT_V25TER) },
1372 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1377 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1373 USB_CDC_ACM_PROTO_AT_PCCA101) }, 1378 USB_CDC_ACM_PROTO_AT_PCCA101) },
1374 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1379 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1375 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) }, 1380 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1376 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1381 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1377 USB_CDC_ACM_PROTO_AT_GSM) }, 1382 USB_CDC_ACM_PROTO_AT_GSM) },
1378 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1383 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1379 USB_CDC_ACM_PROTO_AT_3G ) }, 1384 USB_CDC_ACM_PROTO_AT_3G ) },
1380 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1385 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1381 USB_CDC_ACM_PROTO_AT_CDMA) }, 1386 USB_CDC_ACM_PROTO_AT_CDMA) },
1382 1387
1383 /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */ 1388 /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
1384 { } 1389 { }
1385 }; 1390 };
1386 1391
1387 MODULE_DEVICE_TABLE (usb, acm_ids); 1392 MODULE_DEVICE_TABLE (usb, acm_ids);
1388 1393
1389 static struct usb_driver acm_driver = { 1394 static struct usb_driver acm_driver = {
1390 .name = "cdc_acm", 1395 .name = "cdc_acm",
1391 .probe = acm_probe, 1396 .probe = acm_probe,
1392 .disconnect = acm_disconnect, 1397 .disconnect = acm_disconnect,
1393 #ifdef CONFIG_PM 1398 #ifdef CONFIG_PM
1394 .suspend = acm_suspend, 1399 .suspend = acm_suspend,
1395 .resume = acm_resume, 1400 .resume = acm_resume,
1396 #endif 1401 #endif
1397 .id_table = acm_ids, 1402 .id_table = acm_ids,
1398 #ifdef CONFIG_PM 1403 #ifdef CONFIG_PM
1399 .supports_autosuspend = 1, 1404 .supports_autosuspend = 1,
1400 #endif 1405 #endif
1401 }; 1406 };
1402 1407
1403 /* 1408 /*
1404 * TTY driver structures. 1409 * TTY driver structures.
1405 */ 1410 */
1406 1411
1407 static const struct tty_operations acm_ops = { 1412 static const struct tty_operations acm_ops = {
1408 .open = acm_tty_open, 1413 .open = acm_tty_open,
1409 .close = acm_tty_close, 1414 .close = acm_tty_close,
1410 .write = acm_tty_write, 1415 .write = acm_tty_write,
1411 .write_room = acm_tty_write_room, 1416 .write_room = acm_tty_write_room,
1412 .ioctl = acm_tty_ioctl, 1417 .ioctl = acm_tty_ioctl,
1413 .throttle = acm_tty_throttle, 1418 .throttle = acm_tty_throttle,
1414 .unthrottle = acm_tty_unthrottle, 1419 .unthrottle = acm_tty_unthrottle,
1415 .chars_in_buffer = acm_tty_chars_in_buffer, 1420 .chars_in_buffer = acm_tty_chars_in_buffer,
1416 .break_ctl = acm_tty_break_ctl, 1421 .break_ctl = acm_tty_break_ctl,
1417 .set_termios = acm_tty_set_termios, 1422 .set_termios = acm_tty_set_termios,
1418 .tiocmget = acm_tty_tiocmget, 1423 .tiocmget = acm_tty_tiocmget,
1419 .tiocmset = acm_tty_tiocmset, 1424 .tiocmset = acm_tty_tiocmset,
1420 }; 1425 };
1421 1426
1422 /* 1427 /*
1423 * Init / exit. 1428 * Init / exit.
1424 */ 1429 */
1425 1430
1426 static int __init acm_init(void) 1431 static int __init acm_init(void)
1427 { 1432 {
1428 int retval; 1433 int retval;
1429 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS); 1434 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1430 if (!acm_tty_driver) 1435 if (!acm_tty_driver)
1431 return -ENOMEM; 1436 return -ENOMEM;
1432 acm_tty_driver->owner = THIS_MODULE, 1437 acm_tty_driver->owner = THIS_MODULE,
1433 acm_tty_driver->driver_name = "acm", 1438 acm_tty_driver->driver_name = "acm",
1434 acm_tty_driver->name = "ttyACM", 1439 acm_tty_driver->name = "ttyACM",
1435 acm_tty_driver->major = ACM_TTY_MAJOR, 1440 acm_tty_driver->major = ACM_TTY_MAJOR,
1436 acm_tty_driver->minor_start = 0, 1441 acm_tty_driver->minor_start = 0,
1437 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL, 1442 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1438 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL, 1443 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1439 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 1444 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1440 acm_tty_driver->init_termios = tty_std_termios; 1445 acm_tty_driver->init_termios = tty_std_termios;
1441 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; 1446 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1442 tty_set_operations(acm_tty_driver, &acm_ops); 1447 tty_set_operations(acm_tty_driver, &acm_ops);
1443 1448
1444 retval = tty_register_driver(acm_tty_driver); 1449 retval = tty_register_driver(acm_tty_driver);
1445 if (retval) { 1450 if (retval) {
1446 put_tty_driver(acm_tty_driver); 1451 put_tty_driver(acm_tty_driver);
1447 return retval; 1452 return retval;
1448 } 1453 }
1449 1454
1450 retval = usb_register(&acm_driver); 1455 retval = usb_register(&acm_driver);
1451 if (retval) { 1456 if (retval) {
1452 tty_unregister_driver(acm_tty_driver); 1457 tty_unregister_driver(acm_tty_driver);
1453 put_tty_driver(acm_tty_driver); 1458 put_tty_driver(acm_tty_driver);
1454 return retval; 1459 return retval;
1455 } 1460 }
1456 1461
1457 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" 1462 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1458 DRIVER_DESC "\n"); 1463 DRIVER_DESC "\n");
1459 1464
1460 return 0; 1465 return 0;
1461 } 1466 }
1462 1467
1463 static void __exit acm_exit(void) 1468 static void __exit acm_exit(void)
1464 { 1469 {
1465 usb_deregister(&acm_driver); 1470 usb_deregister(&acm_driver);
1466 tty_unregister_driver(acm_tty_driver); 1471 tty_unregister_driver(acm_tty_driver);
1467 put_tty_driver(acm_tty_driver); 1472 put_tty_driver(acm_tty_driver);
1468 } 1473 }
1469 1474
1470 module_init(acm_init); 1475 module_init(acm_init);
1471 module_exit(acm_exit); 1476 module_exit(acm_exit);
1472 1477
1473 MODULE_AUTHOR( DRIVER_AUTHOR ); 1478 MODULE_AUTHOR( DRIVER_AUTHOR );
1474 MODULE_DESCRIPTION( DRIVER_DESC ); 1479 MODULE_DESCRIPTION( DRIVER_DESC );
1475 MODULE_LICENSE("GPL"); 1480 MODULE_LICENSE("GPL");
1476 1481
1477 1482
drivers/usb/class/cdc-wdm.c
1 /* 1 /*
2 * cdc-wdm.c 2 * cdc-wdm.c
3 * 3 *
4 * This driver supports USB CDC WCM Device Management. 4 * This driver supports USB CDC WCM Device Management.
5 * 5 *
6 * Copyright (c) 2007-2008 Oliver Neukum 6 * Copyright (c) 2007-2008 Oliver Neukum
7 * 7 *
8 * Some code taken from cdc-acm.c 8 * Some code taken from cdc-acm.c
9 * 9 *
10 * Released under the GPLv2. 10 * Released under the GPLv2.
11 * 11 *
12 * Many thanks to Carl Nordbeck 12 * Many thanks to Carl Nordbeck
13 */ 13 */
14 #include <linux/kernel.h> 14 #include <linux/kernel.h>
15 #include <linux/errno.h> 15 #include <linux/errno.h>
16 #include <linux/slab.h> 16 #include <linux/slab.h>
17 #include <linux/module.h> 17 #include <linux/module.h>
18 #include <linux/smp_lock.h> 18 #include <linux/smp_lock.h>
19 #include <linux/mutex.h> 19 #include <linux/mutex.h>
20 #include <linux/uaccess.h> 20 #include <linux/uaccess.h>
21 #include <linux/bitops.h> 21 #include <linux/bitops.h>
22 #include <linux/poll.h> 22 #include <linux/poll.h>
23 #include <linux/usb.h> 23 #include <linux/usb.h>
24 #include <linux/usb/cdc.h> 24 #include <linux/usb/cdc.h>
25 #include <asm/byteorder.h> 25 #include <asm/byteorder.h>
26 #include <asm/unaligned.h> 26 #include <asm/unaligned.h>
27 27
28 /* 28 /*
29 * Version Information 29 * Version Information
30 */ 30 */
31 #define DRIVER_VERSION "v0.03" 31 #define DRIVER_VERSION "v0.03"
32 #define DRIVER_AUTHOR "Oliver Neukum" 32 #define DRIVER_AUTHOR "Oliver Neukum"
33 #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management" 33 #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
34 34
35 static struct usb_device_id wdm_ids[] = { 35 static struct usb_device_id wdm_ids[] = {
36 { 36 {
37 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS | 37 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
38 USB_DEVICE_ID_MATCH_INT_SUBCLASS, 38 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
39 .bInterfaceClass = USB_CLASS_COMM, 39 .bInterfaceClass = USB_CLASS_COMM,
40 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM 40 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
41 }, 41 },
42 { } 42 { }
43 }; 43 };
44 44
45 #define WDM_MINOR_BASE 176 45 #define WDM_MINOR_BASE 176
46 46
47 47
48 #define WDM_IN_USE 1 48 #define WDM_IN_USE 1
49 #define WDM_DISCONNECTING 2 49 #define WDM_DISCONNECTING 2
50 #define WDM_RESULT 3 50 #define WDM_RESULT 3
51 #define WDM_READ 4 51 #define WDM_READ 4
52 #define WDM_INT_STALL 5 52 #define WDM_INT_STALL 5
53 #define WDM_POLL_RUNNING 6 53 #define WDM_POLL_RUNNING 6
54 54
55 55
56 #define WDM_MAX 16 56 #define WDM_MAX 16
57 57
58 58
59 static DEFINE_MUTEX(wdm_mutex); 59 static DEFINE_MUTEX(wdm_mutex);
60 60
61 /* --- method tables --- */ 61 /* --- method tables --- */
62 62
63 struct wdm_device { 63 struct wdm_device {
64 u8 *inbuf; /* buffer for response */ 64 u8 *inbuf; /* buffer for response */
65 u8 *outbuf; /* buffer for command */ 65 u8 *outbuf; /* buffer for command */
66 u8 *sbuf; /* buffer for status */ 66 u8 *sbuf; /* buffer for status */
67 u8 *ubuf; /* buffer for copy to user space */ 67 u8 *ubuf; /* buffer for copy to user space */
68 68
69 struct urb *command; 69 struct urb *command;
70 struct urb *response; 70 struct urb *response;
71 struct urb *validity; 71 struct urb *validity;
72 struct usb_interface *intf; 72 struct usb_interface *intf;
73 struct usb_ctrlrequest *orq; 73 struct usb_ctrlrequest *orq;
74 struct usb_ctrlrequest *irq; 74 struct usb_ctrlrequest *irq;
75 spinlock_t iuspin; 75 spinlock_t iuspin;
76 76
77 unsigned long flags; 77 unsigned long flags;
78 u16 bufsize; 78 u16 bufsize;
79 u16 wMaxCommand; 79 u16 wMaxCommand;
80 u16 wMaxPacketSize; 80 u16 wMaxPacketSize;
81 u16 bMaxPacketSize0; 81 u16 bMaxPacketSize0;
82 __le16 inum; 82 __le16 inum;
83 int reslength; 83 int reslength;
84 int length; 84 int length;
85 int read; 85 int read;
86 int count; 86 int count;
87 dma_addr_t shandle; 87 dma_addr_t shandle;
88 dma_addr_t ihandle; 88 dma_addr_t ihandle;
89 struct mutex wlock; 89 struct mutex wlock;
90 struct mutex rlock; 90 struct mutex rlock;
91 struct mutex plock; 91 struct mutex plock;
92 wait_queue_head_t wait; 92 wait_queue_head_t wait;
93 struct work_struct rxwork; 93 struct work_struct rxwork;
94 int werr; 94 int werr;
95 int rerr; 95 int rerr;
96 }; 96 };
97 97
98 static struct usb_driver wdm_driver; 98 static struct usb_driver wdm_driver;
99 99
100 /* --- callbacks --- */ 100 /* --- callbacks --- */
101 static void wdm_out_callback(struct urb *urb) 101 static void wdm_out_callback(struct urb *urb)
102 { 102 {
103 struct wdm_device *desc; 103 struct wdm_device *desc;
104 desc = urb->context; 104 desc = urb->context;
105 spin_lock(&desc->iuspin); 105 spin_lock(&desc->iuspin);
106 desc->werr = urb->status; 106 desc->werr = urb->status;
107 spin_unlock(&desc->iuspin); 107 spin_unlock(&desc->iuspin);
108 clear_bit(WDM_IN_USE, &desc->flags); 108 clear_bit(WDM_IN_USE, &desc->flags);
109 kfree(desc->outbuf); 109 kfree(desc->outbuf);
110 wake_up(&desc->wait); 110 wake_up(&desc->wait);
111 } 111 }
112 112
113 static void wdm_in_callback(struct urb *urb) 113 static void wdm_in_callback(struct urb *urb)
114 { 114 {
115 struct wdm_device *desc = urb->context; 115 struct wdm_device *desc = urb->context;
116 int status = urb->status; 116 int status = urb->status;
117 117
118 spin_lock(&desc->iuspin); 118 spin_lock(&desc->iuspin);
119 119
120 if (status) { 120 if (status) {
121 switch (status) { 121 switch (status) {
122 case -ENOENT: 122 case -ENOENT:
123 dev_dbg(&desc->intf->dev, 123 dev_dbg(&desc->intf->dev,
124 "nonzero urb status received: -ENOENT"); 124 "nonzero urb status received: -ENOENT");
125 break; 125 break;
126 case -ECONNRESET: 126 case -ECONNRESET:
127 dev_dbg(&desc->intf->dev, 127 dev_dbg(&desc->intf->dev,
128 "nonzero urb status received: -ECONNRESET"); 128 "nonzero urb status received: -ECONNRESET");
129 break; 129 break;
130 case -ESHUTDOWN: 130 case -ESHUTDOWN:
131 dev_dbg(&desc->intf->dev, 131 dev_dbg(&desc->intf->dev,
132 "nonzero urb status received: -ESHUTDOWN"); 132 "nonzero urb status received: -ESHUTDOWN");
133 break; 133 break;
134 case -EPIPE: 134 case -EPIPE:
135 err("nonzero urb status received: -EPIPE"); 135 dev_err(&desc->intf->dev,
136 "nonzero urb status received: -EPIPE\n");
136 break; 137 break;
137 default: 138 default:
138 err("Unexpected error %d", status); 139 dev_err(&desc->intf->dev,
140 "Unexpected error %d\n", status);
139 break; 141 break;
140 } 142 }
141 } 143 }
142 144
143 desc->rerr = status; 145 desc->rerr = status;
144 desc->reslength = urb->actual_length; 146 desc->reslength = urb->actual_length;
145 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength); 147 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
146 desc->length += desc->reslength; 148 desc->length += desc->reslength;
147 wake_up(&desc->wait); 149 wake_up(&desc->wait);
148 150
149 set_bit(WDM_READ, &desc->flags); 151 set_bit(WDM_READ, &desc->flags);
150 spin_unlock(&desc->iuspin); 152 spin_unlock(&desc->iuspin);
151 } 153 }
152 154
153 static void wdm_int_callback(struct urb *urb) 155 static void wdm_int_callback(struct urb *urb)
154 { 156 {
155 int rv = 0; 157 int rv = 0;
156 int status = urb->status; 158 int status = urb->status;
157 struct wdm_device *desc; 159 struct wdm_device *desc;
158 struct usb_ctrlrequest *req; 160 struct usb_ctrlrequest *req;
159 struct usb_cdc_notification *dr; 161 struct usb_cdc_notification *dr;
160 162
161 desc = urb->context; 163 desc = urb->context;
162 req = desc->irq; 164 req = desc->irq;
163 dr = (struct usb_cdc_notification *)desc->sbuf; 165 dr = (struct usb_cdc_notification *)desc->sbuf;
164 166
165 if (status) { 167 if (status) {
166 switch (status) { 168 switch (status) {
167 case -ESHUTDOWN: 169 case -ESHUTDOWN:
168 case -ENOENT: 170 case -ENOENT:
169 case -ECONNRESET: 171 case -ECONNRESET:
170 return; /* unplug */ 172 return; /* unplug */
171 case -EPIPE: 173 case -EPIPE:
172 set_bit(WDM_INT_STALL, &desc->flags); 174 set_bit(WDM_INT_STALL, &desc->flags);
173 err("Stall on int endpoint"); 175 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
174 goto sw; /* halt is cleared in work */ 176 goto sw; /* halt is cleared in work */
175 default: 177 default:
176 err("nonzero urb status received: %d", status); 178 dev_err(&desc->intf->dev,
179 "nonzero urb status received: %d\n", status);
177 break; 180 break;
178 } 181 }
179 } 182 }
180 183
181 if (urb->actual_length < sizeof(struct usb_cdc_notification)) { 184 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
182 err("wdm_int_callback - %d bytes", urb->actual_length); 185 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
186 urb->actual_length);
183 goto exit; 187 goto exit;
184 } 188 }
185 189
186 switch (dr->bNotificationType) { 190 switch (dr->bNotificationType) {
187 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: 191 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
188 dev_dbg(&desc->intf->dev, 192 dev_dbg(&desc->intf->dev,
189 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d", 193 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
190 dr->wIndex, dr->wLength); 194 dr->wIndex, dr->wLength);
191 break; 195 break;
192 196
193 case USB_CDC_NOTIFY_NETWORK_CONNECTION: 197 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
194 198
195 dev_dbg(&desc->intf->dev, 199 dev_dbg(&desc->intf->dev,
196 "NOTIFY_NETWORK_CONNECTION %s network", 200 "NOTIFY_NETWORK_CONNECTION %s network",
197 dr->wValue ? "connected to" : "disconnected from"); 201 dr->wValue ? "connected to" : "disconnected from");
198 goto exit; 202 goto exit;
199 default: 203 default:
200 clear_bit(WDM_POLL_RUNNING, &desc->flags); 204 clear_bit(WDM_POLL_RUNNING, &desc->flags);
201 err("unknown notification %d received: index %d len %d", 205 dev_err(&desc->intf->dev,
206 "unknown notification %d received: index %d len %d\n",
202 dr->bNotificationType, dr->wIndex, dr->wLength); 207 dr->bNotificationType, dr->wIndex, dr->wLength);
203 goto exit; 208 goto exit;
204 } 209 }
205 210
206 req->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE); 211 req->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
207 req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE; 212 req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
208 req->wValue = 0; 213 req->wValue = 0;
209 req->wIndex = desc->inum; 214 req->wIndex = desc->inum;
210 req->wLength = cpu_to_le16(desc->wMaxCommand); 215 req->wLength = cpu_to_le16(desc->wMaxCommand);
211 216
212 usb_fill_control_urb( 217 usb_fill_control_urb(
213 desc->response, 218 desc->response,
214 interface_to_usbdev(desc->intf), 219 interface_to_usbdev(desc->intf),
215 /* using common endpoint 0 */ 220 /* using common endpoint 0 */
216 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0), 221 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
217 (unsigned char *)req, 222 (unsigned char *)req,
218 desc->inbuf, 223 desc->inbuf,
219 desc->wMaxCommand, 224 desc->wMaxCommand,
220 wdm_in_callback, 225 wdm_in_callback,
221 desc 226 desc
222 ); 227 );
223 desc->response->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 228 desc->response->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
224 spin_lock(&desc->iuspin); 229 spin_lock(&desc->iuspin);
225 clear_bit(WDM_READ, &desc->flags); 230 clear_bit(WDM_READ, &desc->flags);
226 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) { 231 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
227 rv = usb_submit_urb(desc->response, GFP_ATOMIC); 232 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
228 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d", 233 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
229 __func__, rv); 234 __func__, rv);
230 } 235 }
231 spin_unlock(&desc->iuspin); 236 spin_unlock(&desc->iuspin);
232 if (rv < 0) { 237 if (rv < 0) {
233 if (rv == -EPERM) 238 if (rv == -EPERM)
234 return; 239 return;
235 if (rv == -ENOMEM) { 240 if (rv == -ENOMEM) {
236 sw: 241 sw:
237 rv = schedule_work(&desc->rxwork); 242 rv = schedule_work(&desc->rxwork);
238 if (rv) 243 if (rv)
239 err("Cannot schedule work"); 244 dev_err(&desc->intf->dev,
245 "Cannot schedule work\n");
240 } 246 }
241 } 247 }
242 exit: 248 exit:
243 rv = usb_submit_urb(urb, GFP_ATOMIC); 249 rv = usb_submit_urb(urb, GFP_ATOMIC);
244 if (rv) 250 if (rv)
245 err("%s - usb_submit_urb failed with result %d", 251 dev_err(&desc->intf->dev,
246 __func__, rv); 252 "%s - usb_submit_urb failed with result %d\n",
253 __func__, rv);
247 254
248 } 255 }
249 256
250 static void kill_urbs(struct wdm_device *desc) 257 static void kill_urbs(struct wdm_device *desc)
251 { 258 {
252 /* the order here is essential */ 259 /* the order here is essential */
253 usb_kill_urb(desc->command); 260 usb_kill_urb(desc->command);
254 usb_kill_urb(desc->validity); 261 usb_kill_urb(desc->validity);
255 usb_kill_urb(desc->response); 262 usb_kill_urb(desc->response);
256 } 263 }
257 264
258 static void free_urbs(struct wdm_device *desc) 265 static void free_urbs(struct wdm_device *desc)
259 { 266 {
260 usb_free_urb(desc->validity); 267 usb_free_urb(desc->validity);
261 usb_free_urb(desc->response); 268 usb_free_urb(desc->response);
262 usb_free_urb(desc->command); 269 usb_free_urb(desc->command);
263 } 270 }
264 271
265 static void cleanup(struct wdm_device *desc) 272 static void cleanup(struct wdm_device *desc)
266 { 273 {
267 usb_buffer_free(interface_to_usbdev(desc->intf), 274 usb_buffer_free(interface_to_usbdev(desc->intf),
268 desc->wMaxPacketSize, 275 desc->wMaxPacketSize,
269 desc->sbuf, 276 desc->sbuf,
270 desc->validity->transfer_dma); 277 desc->validity->transfer_dma);
271 usb_buffer_free(interface_to_usbdev(desc->intf), 278 usb_buffer_free(interface_to_usbdev(desc->intf),
272 desc->wMaxCommand, 279 desc->wMaxCommand,
273 desc->inbuf, 280 desc->inbuf,
274 desc->response->transfer_dma); 281 desc->response->transfer_dma);
275 kfree(desc->orq); 282 kfree(desc->orq);
276 kfree(desc->irq); 283 kfree(desc->irq);
277 kfree(desc->ubuf); 284 kfree(desc->ubuf);
278 free_urbs(desc); 285 free_urbs(desc);
279 kfree(desc); 286 kfree(desc);
280 } 287 }
281 288
282 static ssize_t wdm_write 289 static ssize_t wdm_write
283 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos) 290 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
284 { 291 {
285 u8 *buf; 292 u8 *buf;
286 int rv = -EMSGSIZE, r, we; 293 int rv = -EMSGSIZE, r, we;
287 struct wdm_device *desc = file->private_data; 294 struct wdm_device *desc = file->private_data;
288 struct usb_ctrlrequest *req; 295 struct usb_ctrlrequest *req;
289 296
290 if (count > desc->wMaxCommand) 297 if (count > desc->wMaxCommand)
291 count = desc->wMaxCommand; 298 count = desc->wMaxCommand;
292 299
293 spin_lock_irq(&desc->iuspin); 300 spin_lock_irq(&desc->iuspin);
294 we = desc->werr; 301 we = desc->werr;
295 desc->werr = 0; 302 desc->werr = 0;
296 spin_unlock_irq(&desc->iuspin); 303 spin_unlock_irq(&desc->iuspin);
297 if (we < 0) 304 if (we < 0)
298 return -EIO; 305 return -EIO;
299 306
300 r = mutex_lock_interruptible(&desc->wlock); /* concurrent writes */ 307 r = mutex_lock_interruptible(&desc->wlock); /* concurrent writes */
301 rv = -ERESTARTSYS; 308 rv = -ERESTARTSYS;
302 if (r) 309 if (r)
303 goto outnl; 310 goto outnl;
304 311
305 r = usb_autopm_get_interface(desc->intf); 312 r = usb_autopm_get_interface(desc->intf);
306 if (r < 0) 313 if (r < 0)
307 goto outnp; 314 goto outnp;
308 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE, 315 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
309 &desc->flags)); 316 &desc->flags));
310 if (r < 0) 317 if (r < 0)
311 goto out; 318 goto out;
312 319
313 if (test_bit(WDM_DISCONNECTING, &desc->flags)) { 320 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
314 rv = -ENODEV; 321 rv = -ENODEV;
315 goto out; 322 goto out;
316 } 323 }
317 324
318 desc->outbuf = buf = kmalloc(count, GFP_KERNEL); 325 desc->outbuf = buf = kmalloc(count, GFP_KERNEL);
319 if (!buf) { 326 if (!buf) {
320 rv = -ENOMEM; 327 rv = -ENOMEM;
321 goto out; 328 goto out;
322 } 329 }
323 330
324 r = copy_from_user(buf, buffer, count); 331 r = copy_from_user(buf, buffer, count);
325 if (r > 0) { 332 if (r > 0) {
326 kfree(buf); 333 kfree(buf);
327 rv = -EFAULT; 334 rv = -EFAULT;
328 goto out; 335 goto out;
329 } 336 }
330 337
331 req = desc->orq; 338 req = desc->orq;
332 usb_fill_control_urb( 339 usb_fill_control_urb(
333 desc->command, 340 desc->command,
334 interface_to_usbdev(desc->intf), 341 interface_to_usbdev(desc->intf),
335 /* using common endpoint 0 */ 342 /* using common endpoint 0 */
336 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0), 343 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
337 (unsigned char *)req, 344 (unsigned char *)req,
338 buf, 345 buf,
339 count, 346 count,
340 wdm_out_callback, 347 wdm_out_callback,
341 desc 348 desc
342 ); 349 );
343 350
344 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS | 351 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
345 USB_RECIP_INTERFACE); 352 USB_RECIP_INTERFACE);
346 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND; 353 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
347 req->wValue = 0; 354 req->wValue = 0;
348 req->wIndex = desc->inum; 355 req->wIndex = desc->inum;
349 req->wLength = cpu_to_le16(count); 356 req->wLength = cpu_to_le16(count);
350 set_bit(WDM_IN_USE, &desc->flags); 357 set_bit(WDM_IN_USE, &desc->flags);
351 358
352 rv = usb_submit_urb(desc->command, GFP_KERNEL); 359 rv = usb_submit_urb(desc->command, GFP_KERNEL);
353 if (rv < 0) { 360 if (rv < 0) {
354 kfree(buf); 361 kfree(buf);
355 clear_bit(WDM_IN_USE, &desc->flags); 362 clear_bit(WDM_IN_USE, &desc->flags);
356 err("Tx URB error: %d", rv); 363 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
357 } else { 364 } else {
358 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d", 365 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
359 req->wIndex); 366 req->wIndex);
360 } 367 }
361 out: 368 out:
362 usb_autopm_put_interface(desc->intf); 369 usb_autopm_put_interface(desc->intf);
363 outnp: 370 outnp:
364 mutex_unlock(&desc->wlock); 371 mutex_unlock(&desc->wlock);
365 outnl: 372 outnl:
366 return rv < 0 ? rv : count; 373 return rv < 0 ? rv : count;
367 } 374 }
368 375
369 static ssize_t wdm_read 376 static ssize_t wdm_read
370 (struct file *file, char __user *buffer, size_t count, loff_t *ppos) 377 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
371 { 378 {
372 int rv, cntr; 379 int rv, cntr;
373 int i = 0; 380 int i = 0;
374 struct wdm_device *desc = file->private_data; 381 struct wdm_device *desc = file->private_data;
375 382
376 383
377 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */ 384 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
378 if (rv < 0) 385 if (rv < 0)
379 return -ERESTARTSYS; 386 return -ERESTARTSYS;
380 387
381 if (desc->length == 0) { 388 if (desc->length == 0) {
382 desc->read = 0; 389 desc->read = 0;
383 retry: 390 retry:
384 i++; 391 i++;
385 rv = wait_event_interruptible(desc->wait, 392 rv = wait_event_interruptible(desc->wait,
386 test_bit(WDM_READ, &desc->flags)); 393 test_bit(WDM_READ, &desc->flags));
387 394
388 if (test_bit(WDM_DISCONNECTING, &desc->flags)) { 395 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
389 rv = -ENODEV; 396 rv = -ENODEV;
390 goto err; 397 goto err;
391 } 398 }
392 usb_mark_last_busy(interface_to_usbdev(desc->intf)); 399 usb_mark_last_busy(interface_to_usbdev(desc->intf));
393 if (rv < 0) { 400 if (rv < 0) {
394 rv = -ERESTARTSYS; 401 rv = -ERESTARTSYS;
395 goto err; 402 goto err;
396 } 403 }
397 404
398 spin_lock_irq(&desc->iuspin); 405 spin_lock_irq(&desc->iuspin);
399 406
400 if (desc->rerr) { /* read completed, error happened */ 407 if (desc->rerr) { /* read completed, error happened */
401 int t = desc->rerr; 408 int t = desc->rerr;
402 desc->rerr = 0; 409 desc->rerr = 0;
403 spin_unlock_irq(&desc->iuspin); 410 spin_unlock_irq(&desc->iuspin);
404 err("reading had resulted in %d", t); 411 dev_err(&desc->intf->dev,
412 "reading had resulted in %d\n", t);
405 rv = -EIO; 413 rv = -EIO;
406 goto err; 414 goto err;
407 } 415 }
408 /* 416 /*
409 * recheck whether we've lost the race 417 * recheck whether we've lost the race
410 * against the completion handler 418 * against the completion handler
411 */ 419 */
412 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */ 420 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
413 spin_unlock_irq(&desc->iuspin); 421 spin_unlock_irq(&desc->iuspin);
414 goto retry; 422 goto retry;
415 } 423 }
416 if (!desc->reslength) { /* zero length read */ 424 if (!desc->reslength) { /* zero length read */
417 spin_unlock_irq(&desc->iuspin); 425 spin_unlock_irq(&desc->iuspin);
418 goto retry; 426 goto retry;
419 } 427 }
420 clear_bit(WDM_READ, &desc->flags); 428 clear_bit(WDM_READ, &desc->flags);
421 spin_unlock_irq(&desc->iuspin); 429 spin_unlock_irq(&desc->iuspin);
422 } 430 }
423 431
424 cntr = count > desc->length ? desc->length : count; 432 cntr = count > desc->length ? desc->length : count;
425 rv = copy_to_user(buffer, desc->ubuf, cntr); 433 rv = copy_to_user(buffer, desc->ubuf, cntr);
426 if (rv > 0) { 434 if (rv > 0) {
427 rv = -EFAULT; 435 rv = -EFAULT;
428 goto err; 436 goto err;
429 } 437 }
430 438
431 for (i = 0; i < desc->length - cntr; i++) 439 for (i = 0; i < desc->length - cntr; i++)
432 desc->ubuf[i] = desc->ubuf[i + cntr]; 440 desc->ubuf[i] = desc->ubuf[i + cntr];
433 441
434 desc->length -= cntr; 442 desc->length -= cntr;
435 /* in case we had outstanding data */ 443 /* in case we had outstanding data */
436 if (!desc->length) 444 if (!desc->length)
437 clear_bit(WDM_READ, &desc->flags); 445 clear_bit(WDM_READ, &desc->flags);
438 rv = cntr; 446 rv = cntr;
439 447
440 err: 448 err:
441 mutex_unlock(&desc->rlock); 449 mutex_unlock(&desc->rlock);
442 if (rv < 0) 450 if (rv < 0)
443 err("wdm_read: exit error"); 451 dev_err(&desc->intf->dev, "wdm_read: exit error\n");
444 return rv; 452 return rv;
445 } 453 }
446 454
447 static int wdm_flush(struct file *file, fl_owner_t id) 455 static int wdm_flush(struct file *file, fl_owner_t id)
448 { 456 {
449 struct wdm_device *desc = file->private_data; 457 struct wdm_device *desc = file->private_data;
450 458
451 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags)); 459 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
452 if (desc->werr < 0) 460 if (desc->werr < 0)
453 err("Error in flush path: %d", desc->werr); 461 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
462 desc->werr);
454 463
455 return desc->werr; 464 return desc->werr;
456 } 465 }
457 466
458 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait) 467 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
459 { 468 {
460 struct wdm_device *desc = file->private_data; 469 struct wdm_device *desc = file->private_data;
461 unsigned long flags; 470 unsigned long flags;
462 unsigned int mask = 0; 471 unsigned int mask = 0;
463 472
464 spin_lock_irqsave(&desc->iuspin, flags); 473 spin_lock_irqsave(&desc->iuspin, flags);
465 if (test_bit(WDM_DISCONNECTING, &desc->flags)) { 474 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
466 mask = POLLERR; 475 mask = POLLERR;
467 spin_unlock_irqrestore(&desc->iuspin, flags); 476 spin_unlock_irqrestore(&desc->iuspin, flags);
468 goto desc_out; 477 goto desc_out;
469 } 478 }
470 if (test_bit(WDM_READ, &desc->flags)) 479 if (test_bit(WDM_READ, &desc->flags))
471 mask = POLLIN | POLLRDNORM; 480 mask = POLLIN | POLLRDNORM;
472 if (desc->rerr || desc->werr) 481 if (desc->rerr || desc->werr)
473 mask |= POLLERR; 482 mask |= POLLERR;
474 if (!test_bit(WDM_IN_USE, &desc->flags)) 483 if (!test_bit(WDM_IN_USE, &desc->flags))
475 mask |= POLLOUT | POLLWRNORM; 484 mask |= POLLOUT | POLLWRNORM;
476 spin_unlock_irqrestore(&desc->iuspin, flags); 485 spin_unlock_irqrestore(&desc->iuspin, flags);
477 486
478 poll_wait(file, &desc->wait, wait); 487 poll_wait(file, &desc->wait, wait);
479 488
480 desc_out: 489 desc_out:
481 return mask; 490 return mask;
482 } 491 }
483 492
484 static int wdm_open(struct inode *inode, struct file *file) 493 static int wdm_open(struct inode *inode, struct file *file)
485 { 494 {
486 int minor = iminor(inode); 495 int minor = iminor(inode);
487 int rv = -ENODEV; 496 int rv = -ENODEV;
488 struct usb_interface *intf; 497 struct usb_interface *intf;
489 struct wdm_device *desc; 498 struct wdm_device *desc;
490 499
491 mutex_lock(&wdm_mutex); 500 mutex_lock(&wdm_mutex);
492 intf = usb_find_interface(&wdm_driver, minor); 501 intf = usb_find_interface(&wdm_driver, minor);
493 if (!intf) 502 if (!intf)
494 goto out; 503 goto out;
495 504
496 desc = usb_get_intfdata(intf); 505 desc = usb_get_intfdata(intf);
497 if (test_bit(WDM_DISCONNECTING, &desc->flags)) 506 if (test_bit(WDM_DISCONNECTING, &desc->flags))
498 goto out; 507 goto out;
499 508
500 ; 509 ;
501 file->private_data = desc; 510 file->private_data = desc;
502 511
503 rv = usb_autopm_get_interface(desc->intf); 512 rv = usb_autopm_get_interface(desc->intf);
504 if (rv < 0) { 513 if (rv < 0) {
505 err("Error autopm - %d", rv); 514 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
506 goto out; 515 goto out;
507 } 516 }
508 intf->needs_remote_wakeup = 1; 517 intf->needs_remote_wakeup = 1;
509 518
510 mutex_lock(&desc->plock); 519 mutex_lock(&desc->plock);
511 if (!desc->count++) { 520 if (!desc->count++) {
512 rv = usb_submit_urb(desc->validity, GFP_KERNEL); 521 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
513 if (rv < 0) { 522 if (rv < 0) {
514 desc->count--; 523 desc->count--;
515 err("Error submitting int urb - %d", rv); 524 dev_err(&desc->intf->dev,
525 "Error submitting int urb - %d\n", rv);
516 } 526 }
517 } else { 527 } else {
518 rv = 0; 528 rv = 0;
519 } 529 }
520 mutex_unlock(&desc->plock); 530 mutex_unlock(&desc->plock);
521 usb_autopm_put_interface(desc->intf); 531 usb_autopm_put_interface(desc->intf);
522 out: 532 out:
523 mutex_unlock(&wdm_mutex); 533 mutex_unlock(&wdm_mutex);
524 return rv; 534 return rv;
525 } 535 }
526 536
527 static int wdm_release(struct inode *inode, struct file *file) 537 static int wdm_release(struct inode *inode, struct file *file)
528 { 538 {
529 struct wdm_device *desc = file->private_data; 539 struct wdm_device *desc = file->private_data;
530 540
531 mutex_lock(&wdm_mutex); 541 mutex_lock(&wdm_mutex);
532 mutex_lock(&desc->plock); 542 mutex_lock(&desc->plock);
533 desc->count--; 543 desc->count--;
534 mutex_unlock(&desc->plock); 544 mutex_unlock(&desc->plock);
535 545
536 if (!desc->count) { 546 if (!desc->count) {
537 dev_dbg(&desc->intf->dev, "wdm_release: cleanup"); 547 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
538 kill_urbs(desc); 548 kill_urbs(desc);
539 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) 549 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
540 desc->intf->needs_remote_wakeup = 0; 550 desc->intf->needs_remote_wakeup = 0;
541 } 551 }
542 mutex_unlock(&wdm_mutex); 552 mutex_unlock(&wdm_mutex);
543 return 0; 553 return 0;
544 } 554 }
545 555
546 static const struct file_operations wdm_fops = { 556 static const struct file_operations wdm_fops = {
547 .owner = THIS_MODULE, 557 .owner = THIS_MODULE,
548 .read = wdm_read, 558 .read = wdm_read,
549 .write = wdm_write, 559 .write = wdm_write,
550 .open = wdm_open, 560 .open = wdm_open,
551 .flush = wdm_flush, 561 .flush = wdm_flush,
552 .release = wdm_release, 562 .release = wdm_release,
553 .poll = wdm_poll 563 .poll = wdm_poll
554 }; 564 };
555 565
556 static struct usb_class_driver wdm_class = { 566 static struct usb_class_driver wdm_class = {
557 .name = "cdc-wdm%d", 567 .name = "cdc-wdm%d",
558 .fops = &wdm_fops, 568 .fops = &wdm_fops,
559 .minor_base = WDM_MINOR_BASE, 569 .minor_base = WDM_MINOR_BASE,
560 }; 570 };
561 571
562 /* --- error handling --- */ 572 /* --- error handling --- */
563 static void wdm_rxwork(struct work_struct *work) 573 static void wdm_rxwork(struct work_struct *work)
564 { 574 {
565 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork); 575 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
566 unsigned long flags; 576 unsigned long flags;
567 int rv; 577 int rv;
568 578
569 spin_lock_irqsave(&desc->iuspin, flags); 579 spin_lock_irqsave(&desc->iuspin, flags);
570 if (test_bit(WDM_DISCONNECTING, &desc->flags)) { 580 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
571 spin_unlock_irqrestore(&desc->iuspin, flags); 581 spin_unlock_irqrestore(&desc->iuspin, flags);
572 } else { 582 } else {
573 spin_unlock_irqrestore(&desc->iuspin, flags); 583 spin_unlock_irqrestore(&desc->iuspin, flags);
574 rv = usb_submit_urb(desc->response, GFP_KERNEL); 584 rv = usb_submit_urb(desc->response, GFP_KERNEL);
575 if (rv < 0 && rv != -EPERM) { 585 if (rv < 0 && rv != -EPERM) {
576 spin_lock_irqsave(&desc->iuspin, flags); 586 spin_lock_irqsave(&desc->iuspin, flags);
577 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) 587 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
578 schedule_work(&desc->rxwork); 588 schedule_work(&desc->rxwork);
579 spin_unlock_irqrestore(&desc->iuspin, flags); 589 spin_unlock_irqrestore(&desc->iuspin, flags);
580 } 590 }
581 } 591 }
582 } 592 }
583 593
584 /* --- hotplug --- */ 594 /* --- hotplug --- */
585 595
586 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id) 596 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
587 { 597 {
588 int rv = -EINVAL; 598 int rv = -EINVAL;
589 struct usb_device *udev = interface_to_usbdev(intf); 599 struct usb_device *udev = interface_to_usbdev(intf);
590 struct wdm_device *desc; 600 struct wdm_device *desc;
591 struct usb_host_interface *iface; 601 struct usb_host_interface *iface;
592 struct usb_endpoint_descriptor *ep; 602 struct usb_endpoint_descriptor *ep;
593 struct usb_cdc_dmm_desc *dmhd; 603 struct usb_cdc_dmm_desc *dmhd;
594 u8 *buffer = intf->altsetting->extra; 604 u8 *buffer = intf->altsetting->extra;
595 int buflen = intf->altsetting->extralen; 605 int buflen = intf->altsetting->extralen;
596 u16 maxcom = 0; 606 u16 maxcom = 0;
597 607
598 if (!buffer) 608 if (!buffer)
599 goto out; 609 goto out;
600 610
601 while (buflen > 0) { 611 while (buflen > 0) {
602 if (buffer [1] != USB_DT_CS_INTERFACE) { 612 if (buffer [1] != USB_DT_CS_INTERFACE) {
603 err("skipping garbage"); 613 dev_err(&intf->dev, "skipping garbage\n");
604 goto next_desc; 614 goto next_desc;
605 } 615 }
606 616
607 switch (buffer [2]) { 617 switch (buffer [2]) {
608 case USB_CDC_HEADER_TYPE: 618 case USB_CDC_HEADER_TYPE:
609 break; 619 break;
610 case USB_CDC_DMM_TYPE: 620 case USB_CDC_DMM_TYPE:
611 dmhd = (struct usb_cdc_dmm_desc *)buffer; 621 dmhd = (struct usb_cdc_dmm_desc *)buffer;
612 maxcom = le16_to_cpu(dmhd->wMaxCommand); 622 maxcom = le16_to_cpu(dmhd->wMaxCommand);
613 dev_dbg(&intf->dev, 623 dev_dbg(&intf->dev,
614 "Finding maximum buffer length: %d", maxcom); 624 "Finding maximum buffer length: %d", maxcom);
615 break; 625 break;
616 default: 626 default:
617 err("Ignoring extra header, type %d, length %d", 627 dev_err(&intf->dev,
628 "Ignoring extra header, type %d, length %d\n",
618 buffer[2], buffer[0]); 629 buffer[2], buffer[0]);
619 break; 630 break;
620 } 631 }
621 next_desc: 632 next_desc:
622 buflen -= buffer[0]; 633 buflen -= buffer[0];
623 buffer += buffer[0]; 634 buffer += buffer[0];
624 } 635 }
625 636
626 rv = -ENOMEM; 637 rv = -ENOMEM;
627 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL); 638 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
628 if (!desc) 639 if (!desc)
629 goto out; 640 goto out;
630 mutex_init(&desc->wlock); 641 mutex_init(&desc->wlock);
631 mutex_init(&desc->rlock); 642 mutex_init(&desc->rlock);
632 mutex_init(&desc->plock); 643 mutex_init(&desc->plock);
633 spin_lock_init(&desc->iuspin); 644 spin_lock_init(&desc->iuspin);
634 init_waitqueue_head(&desc->wait); 645 init_waitqueue_head(&desc->wait);
635 desc->wMaxCommand = maxcom; 646 desc->wMaxCommand = maxcom;
636 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber); 647 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
637 desc->intf = intf; 648 desc->intf = intf;
638 INIT_WORK(&desc->rxwork, wdm_rxwork); 649 INIT_WORK(&desc->rxwork, wdm_rxwork);
639 650
640 iface = &intf->altsetting[0]; 651 iface = &intf->altsetting[0];
641 ep = &iface->endpoint[0].desc; 652 ep = &iface->endpoint[0].desc;
642 if (!usb_endpoint_is_int_in(ep)) { 653 if (!usb_endpoint_is_int_in(ep)) {
643 rv = -EINVAL; 654 rv = -EINVAL;
644 goto err; 655 goto err;
645 } 656 }
646 657
647 desc->wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize); 658 desc->wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize);
648 desc->bMaxPacketSize0 = udev->descriptor.bMaxPacketSize0; 659 desc->bMaxPacketSize0 = udev->descriptor.bMaxPacketSize0;
649 660
650 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); 661 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
651 if (!desc->orq) 662 if (!desc->orq)
652 goto err; 663 goto err;
653 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); 664 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
654 if (!desc->irq) 665 if (!desc->irq)
655 goto err; 666 goto err;
656 667
657 desc->validity = usb_alloc_urb(0, GFP_KERNEL); 668 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
658 if (!desc->validity) 669 if (!desc->validity)
659 goto err; 670 goto err;
660 671
661 desc->response = usb_alloc_urb(0, GFP_KERNEL); 672 desc->response = usb_alloc_urb(0, GFP_KERNEL);
662 if (!desc->response) 673 if (!desc->response)
663 goto err; 674 goto err;
664 675
665 desc->command = usb_alloc_urb(0, GFP_KERNEL); 676 desc->command = usb_alloc_urb(0, GFP_KERNEL);
666 if (!desc->command) 677 if (!desc->command)
667 goto err; 678 goto err;
668 679
669 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL); 680 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
670 if (!desc->ubuf) 681 if (!desc->ubuf)
671 goto err; 682 goto err;
672 683
673 desc->sbuf = usb_buffer_alloc(interface_to_usbdev(intf), 684 desc->sbuf = usb_buffer_alloc(interface_to_usbdev(intf),
674 desc->wMaxPacketSize, 685 desc->wMaxPacketSize,
675 GFP_KERNEL, 686 GFP_KERNEL,
676 &desc->validity->transfer_dma); 687 &desc->validity->transfer_dma);
677 if (!desc->sbuf) 688 if (!desc->sbuf)
678 goto err; 689 goto err;
679 690
680 desc->inbuf = usb_buffer_alloc(interface_to_usbdev(intf), 691 desc->inbuf = usb_buffer_alloc(interface_to_usbdev(intf),
681 desc->bMaxPacketSize0, 692 desc->bMaxPacketSize0,
682 GFP_KERNEL, 693 GFP_KERNEL,
683 &desc->response->transfer_dma); 694 &desc->response->transfer_dma);
684 if (!desc->inbuf) 695 if (!desc->inbuf)
685 goto err2; 696 goto err2;
686 697
687 usb_fill_int_urb( 698 usb_fill_int_urb(
688 desc->validity, 699 desc->validity,
689 interface_to_usbdev(intf), 700 interface_to_usbdev(intf),
690 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress), 701 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
691 desc->sbuf, 702 desc->sbuf,
692 desc->wMaxPacketSize, 703 desc->wMaxPacketSize,
693 wdm_int_callback, 704 wdm_int_callback,
694 desc, 705 desc,
695 ep->bInterval 706 ep->bInterval
696 ); 707 );
697 desc->validity->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 708 desc->validity->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
698 709
699 usb_set_intfdata(intf, desc); 710 usb_set_intfdata(intf, desc);
700 rv = usb_register_dev(intf, &wdm_class); 711 rv = usb_register_dev(intf, &wdm_class);
701 dev_info(&intf->dev, "cdc-wdm%d: USB WDM device\n", 712 dev_info(&intf->dev, "cdc-wdm%d: USB WDM device\n",
702 intf->minor - WDM_MINOR_BASE); 713 intf->minor - WDM_MINOR_BASE);
703 if (rv < 0) 714 if (rv < 0)
704 goto err; 715 goto err;
705 out: 716 out:
706 return rv; 717 return rv;
707 err2: 718 err2:
708 usb_buffer_free(interface_to_usbdev(desc->intf), 719 usb_buffer_free(interface_to_usbdev(desc->intf),
709 desc->wMaxPacketSize, 720 desc->wMaxPacketSize,
710 desc->sbuf, 721 desc->sbuf,
711 desc->validity->transfer_dma); 722 desc->validity->transfer_dma);
712 err: 723 err:
713 free_urbs(desc); 724 free_urbs(desc);
714 kfree(desc->ubuf); 725 kfree(desc->ubuf);
715 kfree(desc->orq); 726 kfree(desc->orq);
716 kfree(desc->irq); 727 kfree(desc->irq);
717 kfree(desc); 728 kfree(desc);
718 return rv; 729 return rv;
719 } 730 }
720 731
721 static void wdm_disconnect(struct usb_interface *intf) 732 static void wdm_disconnect(struct usb_interface *intf)
722 { 733 {
723 struct wdm_device *desc; 734 struct wdm_device *desc;
724 unsigned long flags; 735 unsigned long flags;
725 736
726 usb_deregister_dev(intf, &wdm_class); 737 usb_deregister_dev(intf, &wdm_class);
727 mutex_lock(&wdm_mutex); 738 mutex_lock(&wdm_mutex);
728 desc = usb_get_intfdata(intf); 739 desc = usb_get_intfdata(intf);
729 740
730 /* the spinlock makes sure no new urbs are generated in the callbacks */ 741 /* the spinlock makes sure no new urbs are generated in the callbacks */
731 spin_lock_irqsave(&desc->iuspin, flags); 742 spin_lock_irqsave(&desc->iuspin, flags);
732 set_bit(WDM_DISCONNECTING, &desc->flags); 743 set_bit(WDM_DISCONNECTING, &desc->flags);
733 set_bit(WDM_READ, &desc->flags); 744 set_bit(WDM_READ, &desc->flags);
734 /* to terminate pending flushes */ 745 /* to terminate pending flushes */
735 clear_bit(WDM_IN_USE, &desc->flags); 746 clear_bit(WDM_IN_USE, &desc->flags);
736 spin_unlock_irqrestore(&desc->iuspin, flags); 747 spin_unlock_irqrestore(&desc->iuspin, flags);
737 cancel_work_sync(&desc->rxwork); 748 cancel_work_sync(&desc->rxwork);
738 kill_urbs(desc); 749 kill_urbs(desc);
739 wake_up_all(&desc->wait); 750 wake_up_all(&desc->wait);
740 if (!desc->count) 751 if (!desc->count)
741 cleanup(desc); 752 cleanup(desc);
742 mutex_unlock(&wdm_mutex); 753 mutex_unlock(&wdm_mutex);
743 } 754 }
744 755
745 static int wdm_suspend(struct usb_interface *intf, pm_message_t message) 756 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
746 { 757 {
747 struct wdm_device *desc = usb_get_intfdata(intf); 758 struct wdm_device *desc = usb_get_intfdata(intf);
748 int rv = 0; 759 int rv = 0;
749 760
750 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor); 761 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
751 762
752 mutex_lock(&desc->plock); 763 mutex_lock(&desc->plock);
753 #ifdef CONFIG_PM 764 #ifdef CONFIG_PM
754 if (interface_to_usbdev(desc->intf)->auto_pm && test_bit(WDM_IN_USE, &desc->flags)) { 765 if (interface_to_usbdev(desc->intf)->auto_pm && test_bit(WDM_IN_USE, &desc->flags)) {
755 rv = -EBUSY; 766 rv = -EBUSY;
756 } else { 767 } else {
757 #endif 768 #endif
758 cancel_work_sync(&desc->rxwork); 769 cancel_work_sync(&desc->rxwork);
759 kill_urbs(desc); 770 kill_urbs(desc);
760 #ifdef CONFIG_PM 771 #ifdef CONFIG_PM
761 } 772 }
762 #endif 773 #endif
763 mutex_unlock(&desc->plock); 774 mutex_unlock(&desc->plock);
764 775
765 return rv; 776 return rv;
766 } 777 }
767 778
768 static int recover_from_urb_loss(struct wdm_device *desc) 779 static int recover_from_urb_loss(struct wdm_device *desc)
769 { 780 {
770 int rv = 0; 781 int rv = 0;
771 782
772 if (desc->count) { 783 if (desc->count) {
773 rv = usb_submit_urb(desc->validity, GFP_NOIO); 784 rv = usb_submit_urb(desc->validity, GFP_NOIO);
774 if (rv < 0) 785 if (rv < 0)
775 err("Error resume submitting int urb - %d", rv); 786 dev_err(&desc->intf->dev,
787 "Error resume submitting int urb - %d\n", rv);
776 } 788 }
777 return rv; 789 return rv;
778 } 790 }
779 static int wdm_resume(struct usb_interface *intf) 791 static int wdm_resume(struct usb_interface *intf)
780 { 792 {
781 struct wdm_device *desc = usb_get_intfdata(intf); 793 struct wdm_device *desc = usb_get_intfdata(intf);
782 int rv; 794 int rv;
783 795
784 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor); 796 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
785 mutex_lock(&desc->plock); 797 mutex_lock(&desc->plock);
786 rv = recover_from_urb_loss(desc); 798 rv = recover_from_urb_loss(desc);
787 mutex_unlock(&desc->plock); 799 mutex_unlock(&desc->plock);
788 return rv; 800 return rv;
789 } 801 }
790 802
791 static int wdm_pre_reset(struct usb_interface *intf) 803 static int wdm_pre_reset(struct usb_interface *intf)
792 { 804 {
793 struct wdm_device *desc = usb_get_intfdata(intf); 805 struct wdm_device *desc = usb_get_intfdata(intf);
794 806
795 mutex_lock(&desc->plock); 807 mutex_lock(&desc->plock);
796 return 0; 808 return 0;
797 } 809 }
798 810
799 static int wdm_post_reset(struct usb_interface *intf) 811 static int wdm_post_reset(struct usb_interface *intf)
800 { 812 {
801 struct wdm_device *desc = usb_get_intfdata(intf); 813 struct wdm_device *desc = usb_get_intfdata(intf);
802 int rv; 814 int rv;
803 815
804 rv = recover_from_urb_loss(desc); 816 rv = recover_from_urb_loss(desc);
805 mutex_unlock(&desc->plock); 817 mutex_unlock(&desc->plock);
806 return 0; 818 return 0;
807 } 819 }
808 820
809 static struct usb_driver wdm_driver = { 821 static struct usb_driver wdm_driver = {
810 .name = "cdc_wdm", 822 .name = "cdc_wdm",
811 .probe = wdm_probe, 823 .probe = wdm_probe,
812 .disconnect = wdm_disconnect, 824 .disconnect = wdm_disconnect,
813 .suspend = wdm_suspend, 825 .suspend = wdm_suspend,
814 .resume = wdm_resume, 826 .resume = wdm_resume,
815 .reset_resume = wdm_resume, 827 .reset_resume = wdm_resume,
816 .pre_reset = wdm_pre_reset, 828 .pre_reset = wdm_pre_reset,
817 .post_reset = wdm_post_reset, 829 .post_reset = wdm_post_reset,
818 .id_table = wdm_ids, 830 .id_table = wdm_ids,
819 .supports_autosuspend = 1, 831 .supports_autosuspend = 1,
820 }; 832 };
821 833
822 /* --- low level module stuff --- */ 834 /* --- low level module stuff --- */
823 835
824 static int __init wdm_init(void) 836 static int __init wdm_init(void)
825 { 837 {
826 int rv; 838 int rv;
827 839
828 rv = usb_register(&wdm_driver); 840 rv = usb_register(&wdm_driver);
829 841
830 return rv; 842 return rv;
831 } 843 }
832 844
833 static void __exit wdm_exit(void) 845 static void __exit wdm_exit(void)
834 { 846 {
835 usb_deregister(&wdm_driver); 847 usb_deregister(&wdm_driver);
836 } 848 }
837 849
838 module_init(wdm_init); 850 module_init(wdm_init);
839 module_exit(wdm_exit); 851 module_exit(wdm_exit);
840 852
841 MODULE_AUTHOR(DRIVER_AUTHOR); 853 MODULE_AUTHOR(DRIVER_AUTHOR);
842 MODULE_DESCRIPTION(DRIVER_DESC); 854 MODULE_DESCRIPTION(DRIVER_DESC);
843 MODULE_LICENSE("GPL"); 855 MODULE_LICENSE("GPL");
844 856
drivers/usb/class/usblp.c
1 /* 1 /*
2 * usblp.c 2 * usblp.c
3 * 3 *
4 * Copyright (c) 1999 Michael Gee <michael@linuxspecific.com> 4 * Copyright (c) 1999 Michael Gee <michael@linuxspecific.com>
5 * Copyright (c) 1999 Pavel Machek <pavel@suse.cz> 5 * Copyright (c) 1999 Pavel Machek <pavel@suse.cz>
6 * Copyright (c) 2000 Randy Dunlap <rdunlap@xenotime.net> 6 * Copyright (c) 2000 Randy Dunlap <rdunlap@xenotime.net>
7 * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz> 7 * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz>
8 # Copyright (c) 2001 Pete Zaitcev <zaitcev@redhat.com> 8 # Copyright (c) 2001 Pete Zaitcev <zaitcev@redhat.com>
9 # Copyright (c) 2001 David Paschal <paschal@rcsis.com> 9 # Copyright (c) 2001 David Paschal <paschal@rcsis.com>
10 * Copyright (c) 2006 Oliver Neukum <oliver@neukum.name> 10 * Copyright (c) 2006 Oliver Neukum <oliver@neukum.name>
11 * 11 *
12 * USB Printer Device Class driver for USB printers and printer cables 12 * USB Printer Device Class driver for USB printers and printer cables
13 * 13 *
14 * Sponsored by SuSE 14 * Sponsored by SuSE
15 * 15 *
16 * ChangeLog: 16 * ChangeLog:
17 * v0.1 - thorough cleaning, URBification, almost a rewrite 17 * v0.1 - thorough cleaning, URBification, almost a rewrite
18 * v0.2 - some more cleanups 18 * v0.2 - some more cleanups
19 * v0.3 - cleaner again, waitqueue fixes 19 * v0.3 - cleaner again, waitqueue fixes
20 * v0.4 - fixes in unidirectional mode 20 * v0.4 - fixes in unidirectional mode
21 * v0.5 - add DEVICE_ID string support 21 * v0.5 - add DEVICE_ID string support
22 * v0.6 - never time out 22 * v0.6 - never time out
23 * v0.7 - fixed bulk-IN read and poll (David Paschal) 23 * v0.7 - fixed bulk-IN read and poll (David Paschal)
24 * v0.8 - add devfs support 24 * v0.8 - add devfs support
25 * v0.9 - fix unplug-while-open paths 25 * v0.9 - fix unplug-while-open paths
26 * v0.10- remove sleep_on, fix error on oom (oliver@neukum.org) 26 * v0.10- remove sleep_on, fix error on oom (oliver@neukum.org)
27 * v0.11 - add proto_bias option (Pete Zaitcev) 27 * v0.11 - add proto_bias option (Pete Zaitcev)
28 * v0.12 - add hpoj.sourceforge.net ioctls (David Paschal) 28 * v0.12 - add hpoj.sourceforge.net ioctls (David Paschal)
29 * v0.13 - alloc space for statusbuf (<status> not on stack); 29 * v0.13 - alloc space for statusbuf (<status> not on stack);
30 * use usb_buffer_alloc() for read buf & write buf; 30 * use usb_buffer_alloc() for read buf & write buf;
31 * none - Maintained in Linux kernel after v0.13 31 * none - Maintained in Linux kernel after v0.13
32 */ 32 */
33 33
34 /* 34 /*
35 * This program is free software; you can redistribute it and/or modify 35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by 36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or 37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version. 38 * (at your option) any later version.
39 * 39 *
40 * This program is distributed in the hope that it will be useful, 40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of 41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Public License for more details. 43 * GNU General Public License for more details.
44 * 44 *
45 * You should have received a copy of the GNU General Public License 45 * You should have received a copy of the GNU General Public License
46 * along with this program; if not, write to the Free Software 46 * along with this program; if not, write to the Free Software
47 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 47 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
48 */ 48 */
49 49
50 #include <linux/module.h> 50 #include <linux/module.h>
51 #include <linux/kernel.h> 51 #include <linux/kernel.h>
52 #include <linux/sched.h> 52 #include <linux/sched.h>
53 #include <linux/signal.h> 53 #include <linux/signal.h>
54 #include <linux/poll.h> 54 #include <linux/poll.h>
55 #include <linux/init.h> 55 #include <linux/init.h>
56 #include <linux/slab.h> 56 #include <linux/slab.h>
57 #include <linux/lp.h> 57 #include <linux/lp.h>
58 #include <linux/mutex.h> 58 #include <linux/mutex.h>
59 #undef DEBUG 59 #undef DEBUG
60 #include <linux/usb.h> 60 #include <linux/usb.h>
61 61
62 /* 62 /*
63 * Version Information 63 * Version Information
64 */ 64 */
65 #define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal" 65 #define DRIVER_AUTHOR "Michael Gee, Pavel Machek, Vojtech Pavlik, Randy Dunlap, Pete Zaitcev, David Paschal"
66 #define DRIVER_DESC "USB Printer Device Class driver" 66 #define DRIVER_DESC "USB Printer Device Class driver"
67 67
68 #define USBLP_BUF_SIZE 8192 68 #define USBLP_BUF_SIZE 8192
69 #define USBLP_BUF_SIZE_IN 1024 69 #define USBLP_BUF_SIZE_IN 1024
70 #define USBLP_DEVICE_ID_SIZE 1024 70 #define USBLP_DEVICE_ID_SIZE 1024
71 71
72 /* ioctls: */ 72 /* ioctls: */
73 #define IOCNR_GET_DEVICE_ID 1 73 #define IOCNR_GET_DEVICE_ID 1
74 #define IOCNR_GET_PROTOCOLS 2 74 #define IOCNR_GET_PROTOCOLS 2
75 #define IOCNR_SET_PROTOCOL 3 75 #define IOCNR_SET_PROTOCOL 3
76 #define IOCNR_HP_SET_CHANNEL 4 76 #define IOCNR_HP_SET_CHANNEL 4
77 #define IOCNR_GET_BUS_ADDRESS 5 77 #define IOCNR_GET_BUS_ADDRESS 5
78 #define IOCNR_GET_VID_PID 6 78 #define IOCNR_GET_VID_PID 6
79 #define IOCNR_SOFT_RESET 7 79 #define IOCNR_SOFT_RESET 7
80 /* Get device_id string: */ 80 /* Get device_id string: */
81 #define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len) 81 #define LPIOC_GET_DEVICE_ID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
82 /* The following ioctls were added for http://hpoj.sourceforge.net: */ 82 /* The following ioctls were added for http://hpoj.sourceforge.net: */
83 /* Get two-int array: 83 /* Get two-int array:
84 * [0]=current protocol (1=7/1/1, 2=7/1/2, 3=7/1/3), 84 * [0]=current protocol (1=7/1/1, 2=7/1/2, 3=7/1/3),
85 * [1]=supported protocol mask (mask&(1<<n)!=0 means 7/1/n supported): */ 85 * [1]=supported protocol mask (mask&(1<<n)!=0 means 7/1/n supported): */
86 #define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len) 86 #define LPIOC_GET_PROTOCOLS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_PROTOCOLS, len)
87 /* Set protocol (arg: 1=7/1/1, 2=7/1/2, 3=7/1/3): */ 87 /* Set protocol (arg: 1=7/1/1, 2=7/1/2, 3=7/1/3): */
88 #define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0) 88 #define LPIOC_SET_PROTOCOL _IOC(_IOC_WRITE, 'P', IOCNR_SET_PROTOCOL, 0)
89 /* Set channel number (HP Vendor-specific command): */ 89 /* Set channel number (HP Vendor-specific command): */
90 #define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0) 90 #define LPIOC_HP_SET_CHANNEL _IOC(_IOC_WRITE, 'P', IOCNR_HP_SET_CHANNEL, 0)
91 /* Get two-int array: [0]=bus number, [1]=device address: */ 91 /* Get two-int array: [0]=bus number, [1]=device address: */
92 #define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len) 92 #define LPIOC_GET_BUS_ADDRESS(len) _IOC(_IOC_READ, 'P', IOCNR_GET_BUS_ADDRESS, len)
93 /* Get two-int array: [0]=vendor ID, [1]=product ID: */ 93 /* Get two-int array: [0]=vendor ID, [1]=product ID: */
94 #define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len) 94 #define LPIOC_GET_VID_PID(len) _IOC(_IOC_READ, 'P', IOCNR_GET_VID_PID, len)
95 /* Perform class specific soft reset */ 95 /* Perform class specific soft reset */
96 #define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0); 96 #define LPIOC_SOFT_RESET _IOC(_IOC_NONE, 'P', IOCNR_SOFT_RESET, 0);
97 97
98 /* 98 /*
99 * A DEVICE_ID string may include the printer's serial number. 99 * A DEVICE_ID string may include the printer's serial number.
100 * It should end with a semi-colon (';'). 100 * It should end with a semi-colon (';').
101 * An example from an HP 970C DeskJet printer is (this is one long string, 101 * An example from an HP 970C DeskJet printer is (this is one long string,
102 * with the serial number changed): 102 * with the serial number changed):
103 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: ; 103 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: ;
104 */ 104 */
105 105
106 /* 106 /*
107 * USB Printer Requests 107 * USB Printer Requests
108 */ 108 */
109 109
110 #define USBLP_REQ_GET_ID 0x00 110 #define USBLP_REQ_GET_ID 0x00
111 #define USBLP_REQ_GET_STATUS 0x01 111 #define USBLP_REQ_GET_STATUS 0x01
112 #define USBLP_REQ_RESET 0x02 112 #define USBLP_REQ_RESET 0x02
113 #define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST 0x00 /* HP Vendor-specific */ 113 #define USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST 0x00 /* HP Vendor-specific */
114 114
115 #define USBLP_MINORS 16 115 #define USBLP_MINORS 16
116 #define USBLP_MINOR_BASE 0 116 #define USBLP_MINOR_BASE 0
117 117
118 #define USBLP_CTL_TIMEOUT 5000 /* 5 seconds */ 118 #define USBLP_CTL_TIMEOUT 5000 /* 5 seconds */
119 119
120 #define USBLP_FIRST_PROTOCOL 1 120 #define USBLP_FIRST_PROTOCOL 1
121 #define USBLP_LAST_PROTOCOL 3 121 #define USBLP_LAST_PROTOCOL 3
122 #define USBLP_MAX_PROTOCOLS (USBLP_LAST_PROTOCOL+1) 122 #define USBLP_MAX_PROTOCOLS (USBLP_LAST_PROTOCOL+1)
123 123
124 /* 124 /*
125 * some arbitrary status buffer size; 125 * some arbitrary status buffer size;
126 * need a status buffer that is allocated via kmalloc(), not on stack 126 * need a status buffer that is allocated via kmalloc(), not on stack
127 */ 127 */
128 #define STATUS_BUF_SIZE 8 128 #define STATUS_BUF_SIZE 8
129 129
130 /* 130 /*
131 * Locks down the locking order: 131 * Locks down the locking order:
132 * ->wmut locks wstatus. 132 * ->wmut locks wstatus.
133 * ->mut locks the whole usblp, except [rw]complete, and thus, by indirection, 133 * ->mut locks the whole usblp, except [rw]complete, and thus, by indirection,
134 * [rw]status. We only touch status when we know the side idle. 134 * [rw]status. We only touch status when we know the side idle.
135 * ->lock locks what interrupt accesses. 135 * ->lock locks what interrupt accesses.
136 */ 136 */
137 struct usblp { 137 struct usblp {
138 struct usb_device *dev; /* USB device */ 138 struct usb_device *dev; /* USB device */
139 struct mutex wmut; 139 struct mutex wmut;
140 struct mutex mut; 140 struct mutex mut;
141 spinlock_t lock; /* locks rcomplete, wcomplete */ 141 spinlock_t lock; /* locks rcomplete, wcomplete */
142 char *readbuf; /* read transfer_buffer */ 142 char *readbuf; /* read transfer_buffer */
143 char *statusbuf; /* status transfer_buffer */ 143 char *statusbuf; /* status transfer_buffer */
144 struct usb_anchor urbs; 144 struct usb_anchor urbs;
145 wait_queue_head_t rwait, wwait; 145 wait_queue_head_t rwait, wwait;
146 int readcount; /* Counter for reads */ 146 int readcount; /* Counter for reads */
147 int ifnum; /* Interface number */ 147 int ifnum; /* Interface number */
148 struct usb_interface *intf; /* The interface */ 148 struct usb_interface *intf; /* The interface */
149 /* Alternate-setting numbers and endpoints for each protocol 149 /* Alternate-setting numbers and endpoints for each protocol
150 * (7/1/{index=1,2,3}) that the device supports: */ 150 * (7/1/{index=1,2,3}) that the device supports: */
151 struct { 151 struct {
152 int alt_setting; 152 int alt_setting;
153 struct usb_endpoint_descriptor *epwrite; 153 struct usb_endpoint_descriptor *epwrite;
154 struct usb_endpoint_descriptor *epread; 154 struct usb_endpoint_descriptor *epread;
155 } protocol[USBLP_MAX_PROTOCOLS]; 155 } protocol[USBLP_MAX_PROTOCOLS];
156 int current_protocol; 156 int current_protocol;
157 int minor; /* minor number of device */ 157 int minor; /* minor number of device */
158 int wcomplete, rcomplete; 158 int wcomplete, rcomplete;
159 int wstatus; /* bytes written or error */ 159 int wstatus; /* bytes written or error */
160 int rstatus; /* bytes ready or error */ 160 int rstatus; /* bytes ready or error */
161 unsigned int quirks; /* quirks flags */ 161 unsigned int quirks; /* quirks flags */
162 unsigned int flags; /* mode flags */ 162 unsigned int flags; /* mode flags */
163 unsigned char used; /* True if open */ 163 unsigned char used; /* True if open */
164 unsigned char present; /* True if not disconnected */ 164 unsigned char present; /* True if not disconnected */
165 unsigned char bidir; /* interface is bidirectional */ 165 unsigned char bidir; /* interface is bidirectional */
166 unsigned char sleeping; /* interface is suspended */ 166 unsigned char sleeping; /* interface is suspended */
167 unsigned char no_paper; /* Paper Out happened */ 167 unsigned char no_paper; /* Paper Out happened */
168 unsigned char *device_id_string; /* IEEE 1284 DEVICE ID string (ptr) */ 168 unsigned char *device_id_string; /* IEEE 1284 DEVICE ID string (ptr) */
169 /* first 2 bytes are (big-endian) length */ 169 /* first 2 bytes are (big-endian) length */
170 }; 170 };
171 171
172 #ifdef DEBUG 172 #ifdef DEBUG
173 static void usblp_dump(struct usblp *usblp) { 173 static void usblp_dump(struct usblp *usblp) {
174 int p; 174 int p;
175 175
176 dbg("usblp=0x%p", usblp); 176 dbg("usblp=0x%p", usblp);
177 dbg("dev=0x%p", usblp->dev); 177 dbg("dev=0x%p", usblp->dev);
178 dbg("present=%d", usblp->present); 178 dbg("present=%d", usblp->present);
179 dbg("readbuf=0x%p", usblp->readbuf); 179 dbg("readbuf=0x%p", usblp->readbuf);
180 dbg("readcount=%d", usblp->readcount); 180 dbg("readcount=%d", usblp->readcount);
181 dbg("ifnum=%d", usblp->ifnum); 181 dbg("ifnum=%d", usblp->ifnum);
182 for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) { 182 for (p = USBLP_FIRST_PROTOCOL; p <= USBLP_LAST_PROTOCOL; p++) {
183 dbg("protocol[%d].alt_setting=%d", p, usblp->protocol[p].alt_setting); 183 dbg("protocol[%d].alt_setting=%d", p, usblp->protocol[p].alt_setting);
184 dbg("protocol[%d].epwrite=%p", p, usblp->protocol[p].epwrite); 184 dbg("protocol[%d].epwrite=%p", p, usblp->protocol[p].epwrite);
185 dbg("protocol[%d].epread=%p", p, usblp->protocol[p].epread); 185 dbg("protocol[%d].epread=%p", p, usblp->protocol[p].epread);
186 } 186 }
187 dbg("current_protocol=%d", usblp->current_protocol); 187 dbg("current_protocol=%d", usblp->current_protocol);
188 dbg("minor=%d", usblp->minor); 188 dbg("minor=%d", usblp->minor);
189 dbg("wstatus=%d", usblp->wstatus); 189 dbg("wstatus=%d", usblp->wstatus);
190 dbg("rstatus=%d", usblp->rstatus); 190 dbg("rstatus=%d", usblp->rstatus);
191 dbg("quirks=%d", usblp->quirks); 191 dbg("quirks=%d", usblp->quirks);
192 dbg("used=%d", usblp->used); 192 dbg("used=%d", usblp->used);
193 dbg("bidir=%d", usblp->bidir); 193 dbg("bidir=%d", usblp->bidir);
194 dbg("sleeping=%d", usblp->sleeping); 194 dbg("sleeping=%d", usblp->sleeping);
195 dbg("device_id_string=\"%s\"", 195 dbg("device_id_string=\"%s\"",
196 usblp->device_id_string ? 196 usblp->device_id_string ?
197 usblp->device_id_string + 2 : 197 usblp->device_id_string + 2 :
198 (unsigned char *)"(null)"); 198 (unsigned char *)"(null)");
199 } 199 }
200 #endif 200 #endif
201 201
202 /* Quirks: various printer quirks are handled by this table & its flags. */ 202 /* Quirks: various printer quirks are handled by this table & its flags. */
203 203
204 struct quirk_printer_struct { 204 struct quirk_printer_struct {
205 __u16 vendorId; 205 __u16 vendorId;
206 __u16 productId; 206 __u16 productId;
207 unsigned int quirks; 207 unsigned int quirks;
208 }; 208 };
209 209
210 #define USBLP_QUIRK_BIDIR 0x1 /* reports bidir but requires unidirectional mode (no INs/reads) */ 210 #define USBLP_QUIRK_BIDIR 0x1 /* reports bidir but requires unidirectional mode (no INs/reads) */
211 #define USBLP_QUIRK_USB_INIT 0x2 /* needs vendor USB init string */ 211 #define USBLP_QUIRK_USB_INIT 0x2 /* needs vendor USB init string */
212 #define USBLP_QUIRK_BAD_CLASS 0x4 /* descriptor uses vendor-specific Class or SubClass */ 212 #define USBLP_QUIRK_BAD_CLASS 0x4 /* descriptor uses vendor-specific Class or SubClass */
213 213
214 static const struct quirk_printer_struct quirk_printers[] = { 214 static const struct quirk_printer_struct quirk_printers[] = {
215 { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */ 215 { 0x03f0, 0x0004, USBLP_QUIRK_BIDIR }, /* HP DeskJet 895C */
216 { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */ 216 { 0x03f0, 0x0104, USBLP_QUIRK_BIDIR }, /* HP DeskJet 880C */
217 { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */ 217 { 0x03f0, 0x0204, USBLP_QUIRK_BIDIR }, /* HP DeskJet 815C */
218 { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */ 218 { 0x03f0, 0x0304, USBLP_QUIRK_BIDIR }, /* HP DeskJet 810C/812C */
219 { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */ 219 { 0x03f0, 0x0404, USBLP_QUIRK_BIDIR }, /* HP DeskJet 830C */
220 { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */ 220 { 0x03f0, 0x0504, USBLP_QUIRK_BIDIR }, /* HP DeskJet 885C */
221 { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */ 221 { 0x03f0, 0x0604, USBLP_QUIRK_BIDIR }, /* HP DeskJet 840C */
222 { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */ 222 { 0x03f0, 0x0804, USBLP_QUIRK_BIDIR }, /* HP DeskJet 816C */
223 { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */ 223 { 0x03f0, 0x1104, USBLP_QUIRK_BIDIR }, /* HP Deskjet 959C */
224 { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */ 224 { 0x0409, 0xefbe, USBLP_QUIRK_BIDIR }, /* NEC Picty900 (HP OEM) */
225 { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */ 225 { 0x0409, 0xbef4, USBLP_QUIRK_BIDIR }, /* NEC Picty760 (HP OEM) */
226 { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */ 226 { 0x0409, 0xf0be, USBLP_QUIRK_BIDIR }, /* NEC Picty920 (HP OEM) */
227 { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */ 227 { 0x0409, 0xf1be, USBLP_QUIRK_BIDIR }, /* NEC Picty800 (HP OEM) */
228 { 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */ 228 { 0x0482, 0x0010, USBLP_QUIRK_BIDIR }, /* Kyocera Mita FS 820, by zut <kernel@zut.de> */
229 { 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt Printer M129C */ 229 { 0x04b8, 0x0202, USBLP_QUIRK_BAD_CLASS }, /* Seiko Epson Receipt Printer M129C */
230 { 0, 0 } 230 { 0, 0 }
231 }; 231 };
232 232
233 static int usblp_wwait(struct usblp *usblp, int nonblock); 233 static int usblp_wwait(struct usblp *usblp, int nonblock);
234 static int usblp_wtest(struct usblp *usblp, int nonblock); 234 static int usblp_wtest(struct usblp *usblp, int nonblock);
235 static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock); 235 static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock);
236 static int usblp_rtest(struct usblp *usblp, int nonblock); 236 static int usblp_rtest(struct usblp *usblp, int nonblock);
237 static int usblp_submit_read(struct usblp *usblp); 237 static int usblp_submit_read(struct usblp *usblp);
238 static int usblp_select_alts(struct usblp *usblp); 238 static int usblp_select_alts(struct usblp *usblp);
239 static int usblp_set_protocol(struct usblp *usblp, int protocol); 239 static int usblp_set_protocol(struct usblp *usblp, int protocol);
240 static int usblp_cache_device_id_string(struct usblp *usblp); 240 static int usblp_cache_device_id_string(struct usblp *usblp);
241 241
242 /* forward reference to make our lives easier */ 242 /* forward reference to make our lives easier */
243 static struct usb_driver usblp_driver; 243 static struct usb_driver usblp_driver;
244 static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */ 244 static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */
245 245
246 /* 246 /*
247 * Functions for usblp control messages. 247 * Functions for usblp control messages.
248 */ 248 */
249 249
250 static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len) 250 static int usblp_ctrl_msg(struct usblp *usblp, int request, int type, int dir, int recip, int value, void *buf, int len)
251 { 251 {
252 int retval; 252 int retval;
253 int index = usblp->ifnum; 253 int index = usblp->ifnum;
254 254
255 /* High byte has the interface index. 255 /* High byte has the interface index.
256 Low byte has the alternate setting. 256 Low byte has the alternate setting.
257 */ 257 */
258 if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS)) { 258 if ((request == USBLP_REQ_GET_ID) && (type == USB_TYPE_CLASS)) {
259 index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting; 259 index = (usblp->ifnum<<8)|usblp->protocol[usblp->current_protocol].alt_setting;
260 } 260 }
261 261
262 retval = usb_control_msg(usblp->dev, 262 retval = usb_control_msg(usblp->dev,
263 dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0), 263 dir ? usb_rcvctrlpipe(usblp->dev, 0) : usb_sndctrlpipe(usblp->dev, 0),
264 request, type | dir | recip, value, index, buf, len, USBLP_CTL_TIMEOUT); 264 request, type | dir | recip, value, index, buf, len, USBLP_CTL_TIMEOUT);
265 dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d", 265 dbg("usblp_control_msg: rq: 0x%02x dir: %d recip: %d value: %d idx: %d len: %#x result: %d",
266 request, !!dir, recip, value, index, len, retval); 266 request, !!dir, recip, value, index, len, retval);
267 return retval < 0 ? retval : 0; 267 return retval < 0 ? retval : 0;
268 } 268 }
269 269
270 #define usblp_read_status(usblp, status)\ 270 #define usblp_read_status(usblp, status)\
271 usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1) 271 usblp_ctrl_msg(usblp, USBLP_REQ_GET_STATUS, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, 0, status, 1)
272 #define usblp_get_id(usblp, config, id, maxlen)\ 272 #define usblp_get_id(usblp, config, id, maxlen)\
273 usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen) 273 usblp_ctrl_msg(usblp, USBLP_REQ_GET_ID, USB_TYPE_CLASS, USB_DIR_IN, USB_RECIP_INTERFACE, config, id, maxlen)
274 #define usblp_reset(usblp)\ 274 #define usblp_reset(usblp)\
275 usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0) 275 usblp_ctrl_msg(usblp, USBLP_REQ_RESET, USB_TYPE_CLASS, USB_DIR_OUT, USB_RECIP_OTHER, 0, NULL, 0)
276 276
277 #define usblp_hp_channel_change_request(usblp, channel, buffer) \ 277 #define usblp_hp_channel_change_request(usblp, channel, buffer) \
278 usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1) 278 usblp_ctrl_msg(usblp, USBLP_REQ_HP_CHANNEL_CHANGE_REQUEST, USB_TYPE_VENDOR, USB_DIR_IN, USB_RECIP_INTERFACE, channel, buffer, 1)
279 279
280 /* 280 /*
281 * See the description for usblp_select_alts() below for the usage 281 * See the description for usblp_select_alts() below for the usage
282 * explanation. Look into your /proc/bus/usb/devices and dmesg in 282 * explanation. Look into your /proc/bus/usb/devices and dmesg in
283 * case of any trouble. 283 * case of any trouble.
284 */ 284 */
285 static int proto_bias = -1; 285 static int proto_bias = -1;
286 286
287 /* 287 /*
288 * URB callback. 288 * URB callback.
289 */ 289 */
290 290
291 static void usblp_bulk_read(struct urb *urb) 291 static void usblp_bulk_read(struct urb *urb)
292 { 292 {
293 struct usblp *usblp = urb->context; 293 struct usblp *usblp = urb->context;
294 int status = urb->status; 294 int status = urb->status;
295 295
296 if (usblp->present && usblp->used) { 296 if (usblp->present && usblp->used) {
297 if (status) 297 if (status)
298 printk(KERN_WARNING "usblp%d: " 298 printk(KERN_WARNING "usblp%d: "
299 "nonzero read bulk status received: %d\n", 299 "nonzero read bulk status received: %d\n",
300 usblp->minor, status); 300 usblp->minor, status);
301 } 301 }
302 spin_lock(&usblp->lock); 302 spin_lock(&usblp->lock);
303 if (status < 0) 303 if (status < 0)
304 usblp->rstatus = status; 304 usblp->rstatus = status;
305 else 305 else
306 usblp->rstatus = urb->actual_length; 306 usblp->rstatus = urb->actual_length;
307 usblp->rcomplete = 1; 307 usblp->rcomplete = 1;
308 wake_up(&usblp->rwait); 308 wake_up(&usblp->rwait);
309 spin_unlock(&usblp->lock); 309 spin_unlock(&usblp->lock);
310 310
311 usb_free_urb(urb); 311 usb_free_urb(urb);
312 } 312 }
313 313
314 static void usblp_bulk_write(struct urb *urb) 314 static void usblp_bulk_write(struct urb *urb)
315 { 315 {
316 struct usblp *usblp = urb->context; 316 struct usblp *usblp = urb->context;
317 int status = urb->status; 317 int status = urb->status;
318 318
319 if (usblp->present && usblp->used) { 319 if (usblp->present && usblp->used) {
320 if (status) 320 if (status)
321 printk(KERN_WARNING "usblp%d: " 321 printk(KERN_WARNING "usblp%d: "
322 "nonzero write bulk status received: %d\n", 322 "nonzero write bulk status received: %d\n",
323 usblp->minor, status); 323 usblp->minor, status);
324 } 324 }
325 spin_lock(&usblp->lock); 325 spin_lock(&usblp->lock);
326 if (status < 0) 326 if (status < 0)
327 usblp->wstatus = status; 327 usblp->wstatus = status;
328 else 328 else
329 usblp->wstatus = urb->actual_length; 329 usblp->wstatus = urb->actual_length;
330 usblp->no_paper = 0; 330 usblp->no_paper = 0;
331 usblp->wcomplete = 1; 331 usblp->wcomplete = 1;
332 wake_up(&usblp->wwait); 332 wake_up(&usblp->wwait);
333 spin_unlock(&usblp->lock); 333 spin_unlock(&usblp->lock);
334 334
335 usb_free_urb(urb); 335 usb_free_urb(urb);
336 } 336 }
337 337
338 /* 338 /*
339 * Get and print printer errors. 339 * Get and print printer errors.
340 */ 340 */
341 341
342 static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" }; 342 static const char *usblp_messages[] = { "ok", "out of paper", "off-line", "on fire" };
343 343
344 static int usblp_check_status(struct usblp *usblp, int err) 344 static int usblp_check_status(struct usblp *usblp, int err)
345 { 345 {
346 unsigned char status, newerr = 0; 346 unsigned char status, newerr = 0;
347 int error; 347 int error;
348 348
349 mutex_lock(&usblp->mut); 349 mutex_lock(&usblp->mut);
350 if ((error = usblp_read_status(usblp, usblp->statusbuf)) < 0) { 350 if ((error = usblp_read_status(usblp, usblp->statusbuf)) < 0) {
351 mutex_unlock(&usblp->mut); 351 mutex_unlock(&usblp->mut);
352 if (printk_ratelimit()) 352 if (printk_ratelimit())
353 printk(KERN_ERR 353 printk(KERN_ERR
354 "usblp%d: error %d reading printer status\n", 354 "usblp%d: error %d reading printer status\n",
355 usblp->minor, error); 355 usblp->minor, error);
356 return 0; 356 return 0;
357 } 357 }
358 status = *usblp->statusbuf; 358 status = *usblp->statusbuf;
359 mutex_unlock(&usblp->mut); 359 mutex_unlock(&usblp->mut);
360 360
361 if (~status & LP_PERRORP) 361 if (~status & LP_PERRORP)
362 newerr = 3; 362 newerr = 3;
363 if (status & LP_POUTPA) 363 if (status & LP_POUTPA)
364 newerr = 1; 364 newerr = 1;
365 if (~status & LP_PSELECD) 365 if (~status & LP_PSELECD)
366 newerr = 2; 366 newerr = 2;
367 367
368 if (newerr != err) { 368 if (newerr != err) {
369 printk(KERN_INFO "usblp%d: %s\n", 369 printk(KERN_INFO "usblp%d: %s\n",
370 usblp->minor, usblp_messages[newerr]); 370 usblp->minor, usblp_messages[newerr]);
371 } 371 }
372 372
373 return newerr; 373 return newerr;
374 } 374 }
375 375
376 static int handle_bidir (struct usblp *usblp) 376 static int handle_bidir (struct usblp *usblp)
377 { 377 {
378 if (usblp->bidir && usblp->used && !usblp->sleeping) { 378 if (usblp->bidir && usblp->used && !usblp->sleeping) {
379 if (usblp_submit_read(usblp) < 0) 379 if (usblp_submit_read(usblp) < 0)
380 return -EIO; 380 return -EIO;
381 } 381 }
382 return 0; 382 return 0;
383 } 383 }
384 384
385 /* 385 /*
386 * File op functions. 386 * File op functions.
387 */ 387 */
388 388
389 static int usblp_open(struct inode *inode, struct file *file) 389 static int usblp_open(struct inode *inode, struct file *file)
390 { 390 {
391 int minor = iminor(inode); 391 int minor = iminor(inode);
392 struct usblp *usblp; 392 struct usblp *usblp;
393 struct usb_interface *intf; 393 struct usb_interface *intf;
394 int retval; 394 int retval;
395 395
396 if (minor < 0) 396 if (minor < 0)
397 return -ENODEV; 397 return -ENODEV;
398 398
399 mutex_lock (&usblp_mutex); 399 mutex_lock (&usblp_mutex);
400 400
401 retval = -ENODEV; 401 retval = -ENODEV;
402 intf = usb_find_interface(&usblp_driver, minor); 402 intf = usb_find_interface(&usblp_driver, minor);
403 if (!intf) { 403 if (!intf) {
404 goto out; 404 goto out;
405 } 405 }
406 usblp = usb_get_intfdata (intf); 406 usblp = usb_get_intfdata (intf);
407 if (!usblp || !usblp->dev || !usblp->present) 407 if (!usblp || !usblp->dev || !usblp->present)
408 goto out; 408 goto out;
409 409
410 retval = -EBUSY; 410 retval = -EBUSY;
411 if (usblp->used) 411 if (usblp->used)
412 goto out; 412 goto out;
413 413
414 /* 414 /*
415 * We do not implement LP_ABORTOPEN/LPABORTOPEN for two reasons: 415 * We do not implement LP_ABORTOPEN/LPABORTOPEN for two reasons:
416 * - We do not want persistent state which close(2) does not clear 416 * - We do not want persistent state which close(2) does not clear
417 * - It is not used anyway, according to CUPS people 417 * - It is not used anyway, according to CUPS people
418 */ 418 */
419 419
420 retval = usb_autopm_get_interface(intf); 420 retval = usb_autopm_get_interface(intf);
421 if (retval < 0) 421 if (retval < 0)
422 goto out; 422 goto out;
423 usblp->used = 1; 423 usblp->used = 1;
424 file->private_data = usblp; 424 file->private_data = usblp;
425 425
426 usblp->wcomplete = 1; /* we begin writeable */ 426 usblp->wcomplete = 1; /* we begin writeable */
427 usblp->wstatus = 0; 427 usblp->wstatus = 0;
428 usblp->rcomplete = 0; 428 usblp->rcomplete = 0;
429 429
430 if (handle_bidir(usblp) < 0) { 430 if (handle_bidir(usblp) < 0) {
431 usb_autopm_put_interface(intf); 431 usb_autopm_put_interface(intf);
432 usblp->used = 0; 432 usblp->used = 0;
433 file->private_data = NULL; 433 file->private_data = NULL;
434 retval = -EIO; 434 retval = -EIO;
435 } 435 }
436 out: 436 out:
437 mutex_unlock (&usblp_mutex); 437 mutex_unlock (&usblp_mutex);
438 return retval; 438 return retval;
439 } 439 }
440 440
441 static void usblp_cleanup (struct usblp *usblp) 441 static void usblp_cleanup (struct usblp *usblp)
442 { 442 {
443 printk(KERN_INFO "usblp%d: removed\n", usblp->minor); 443 printk(KERN_INFO "usblp%d: removed\n", usblp->minor);
444 444
445 kfree(usblp->readbuf); 445 kfree(usblp->readbuf);
446 kfree (usblp->device_id_string); 446 kfree (usblp->device_id_string);
447 kfree (usblp->statusbuf); 447 kfree (usblp->statusbuf);
448 kfree (usblp); 448 kfree (usblp);
449 } 449 }
450 450
451 static void usblp_unlink_urbs(struct usblp *usblp) 451 static void usblp_unlink_urbs(struct usblp *usblp)
452 { 452 {
453 usb_kill_anchored_urbs(&usblp->urbs); 453 usb_kill_anchored_urbs(&usblp->urbs);
454 } 454 }
455 455
456 static int usblp_release(struct inode *inode, struct file *file) 456 static int usblp_release(struct inode *inode, struct file *file)
457 { 457 {
458 struct usblp *usblp = file->private_data; 458 struct usblp *usblp = file->private_data;
459 459
460 usblp->flags &= ~LP_ABORT; 460 usblp->flags &= ~LP_ABORT;
461 461
462 mutex_lock (&usblp_mutex); 462 mutex_lock (&usblp_mutex);
463 usblp->used = 0; 463 usblp->used = 0;
464 if (usblp->present) { 464 if (usblp->present) {
465 usblp_unlink_urbs(usblp); 465 usblp_unlink_urbs(usblp);
466 usb_autopm_put_interface(usblp->intf); 466 usb_autopm_put_interface(usblp->intf);
467 } else /* finish cleanup from disconnect */ 467 } else /* finish cleanup from disconnect */
468 usblp_cleanup (usblp); 468 usblp_cleanup (usblp);
469 mutex_unlock (&usblp_mutex); 469 mutex_unlock (&usblp_mutex);
470 return 0; 470 return 0;
471 } 471 }
472 472
473 /* No kernel lock - fine */ 473 /* No kernel lock - fine */
474 static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait) 474 static unsigned int usblp_poll(struct file *file, struct poll_table_struct *wait)
475 { 475 {
476 int ret; 476 int ret;
477 unsigned long flags; 477 unsigned long flags;
478 478
479 struct usblp *usblp = file->private_data; 479 struct usblp *usblp = file->private_data;
480 /* Should we check file->f_mode & FMODE_WRITE before poll_wait()? */ 480 /* Should we check file->f_mode & FMODE_WRITE before poll_wait()? */
481 poll_wait(file, &usblp->rwait, wait); 481 poll_wait(file, &usblp->rwait, wait);
482 poll_wait(file, &usblp->wwait, wait); 482 poll_wait(file, &usblp->wwait, wait);
483 spin_lock_irqsave(&usblp->lock, flags); 483 spin_lock_irqsave(&usblp->lock, flags);
484 ret = ((usblp->bidir && usblp->rcomplete) ? POLLIN | POLLRDNORM : 0) | 484 ret = ((usblp->bidir && usblp->rcomplete) ? POLLIN | POLLRDNORM : 0) |
485 ((usblp->no_paper || usblp->wcomplete) ? POLLOUT | POLLWRNORM : 0); 485 ((usblp->no_paper || usblp->wcomplete) ? POLLOUT | POLLWRNORM : 0);
486 spin_unlock_irqrestore(&usblp->lock, flags); 486 spin_unlock_irqrestore(&usblp->lock, flags);
487 return ret; 487 return ret;
488 } 488 }
489 489
490 static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 490 static long usblp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
491 { 491 {
492 struct usblp *usblp = file->private_data; 492 struct usblp *usblp = file->private_data;
493 int length, err, i; 493 int length, err, i;
494 unsigned char newChannel; 494 unsigned char newChannel;
495 int status; 495 int status;
496 int twoints[2]; 496 int twoints[2];
497 int retval = 0; 497 int retval = 0;
498 498
499 mutex_lock (&usblp->mut); 499 mutex_lock (&usblp->mut);
500 if (!usblp->present) { 500 if (!usblp->present) {
501 retval = -ENODEV; 501 retval = -ENODEV;
502 goto done; 502 goto done;
503 } 503 }
504 504
505 if (usblp->sleeping) { 505 if (usblp->sleeping) {
506 retval = -ENODEV; 506 retval = -ENODEV;
507 goto done; 507 goto done;
508 } 508 }
509 509
510 dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd), 510 dbg("usblp_ioctl: cmd=0x%x (%c nr=%d len=%d dir=%d)", cmd, _IOC_TYPE(cmd),
511 _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd) ); 511 _IOC_NR(cmd), _IOC_SIZE(cmd), _IOC_DIR(cmd) );
512 512
513 if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */ 513 if (_IOC_TYPE(cmd) == 'P') /* new-style ioctl number */
514 514
515 switch (_IOC_NR(cmd)) { 515 switch (_IOC_NR(cmd)) {
516 516
517 case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */ 517 case IOCNR_GET_DEVICE_ID: /* get the DEVICE_ID string */
518 if (_IOC_DIR(cmd) != _IOC_READ) { 518 if (_IOC_DIR(cmd) != _IOC_READ) {
519 retval = -EINVAL; 519 retval = -EINVAL;
520 goto done; 520 goto done;
521 } 521 }
522 522
523 length = usblp_cache_device_id_string(usblp); 523 length = usblp_cache_device_id_string(usblp);
524 if (length < 0) { 524 if (length < 0) {
525 retval = length; 525 retval = length;
526 goto done; 526 goto done;
527 } 527 }
528 if (length > _IOC_SIZE(cmd)) 528 if (length > _IOC_SIZE(cmd))
529 length = _IOC_SIZE(cmd); /* truncate */ 529 length = _IOC_SIZE(cmd); /* truncate */
530 530
531 if (copy_to_user((void __user *) arg, 531 if (copy_to_user((void __user *) arg,
532 usblp->device_id_string, 532 usblp->device_id_string,
533 (unsigned long) length)) { 533 (unsigned long) length)) {
534 retval = -EFAULT; 534 retval = -EFAULT;
535 goto done; 535 goto done;
536 } 536 }
537 537
538 break; 538 break;
539 539
540 case IOCNR_GET_PROTOCOLS: 540 case IOCNR_GET_PROTOCOLS:
541 if (_IOC_DIR(cmd) != _IOC_READ || 541 if (_IOC_DIR(cmd) != _IOC_READ ||
542 _IOC_SIZE(cmd) < sizeof(twoints)) { 542 _IOC_SIZE(cmd) < sizeof(twoints)) {
543 retval = -EINVAL; 543 retval = -EINVAL;
544 goto done; 544 goto done;
545 } 545 }
546 546
547 twoints[0] = usblp->current_protocol; 547 twoints[0] = usblp->current_protocol;
548 twoints[1] = 0; 548 twoints[1] = 0;
549 for (i = USBLP_FIRST_PROTOCOL; 549 for (i = USBLP_FIRST_PROTOCOL;
550 i <= USBLP_LAST_PROTOCOL; i++) { 550 i <= USBLP_LAST_PROTOCOL; i++) {
551 if (usblp->protocol[i].alt_setting >= 0) 551 if (usblp->protocol[i].alt_setting >= 0)
552 twoints[1] |= (1<<i); 552 twoints[1] |= (1<<i);
553 } 553 }
554 554
555 if (copy_to_user((void __user *)arg, 555 if (copy_to_user((void __user *)arg,
556 (unsigned char *)twoints, 556 (unsigned char *)twoints,
557 sizeof(twoints))) { 557 sizeof(twoints))) {
558 retval = -EFAULT; 558 retval = -EFAULT;
559 goto done; 559 goto done;
560 } 560 }
561 561
562 break; 562 break;
563 563
564 case IOCNR_SET_PROTOCOL: 564 case IOCNR_SET_PROTOCOL:
565 if (_IOC_DIR(cmd) != _IOC_WRITE) { 565 if (_IOC_DIR(cmd) != _IOC_WRITE) {
566 retval = -EINVAL; 566 retval = -EINVAL;
567 goto done; 567 goto done;
568 } 568 }
569 569
570 #ifdef DEBUG 570 #ifdef DEBUG
571 if (arg == -10) { 571 if (arg == -10) {
572 usblp_dump(usblp); 572 usblp_dump(usblp);
573 break; 573 break;
574 } 574 }
575 #endif 575 #endif
576 576
577 usblp_unlink_urbs(usblp); 577 usblp_unlink_urbs(usblp);
578 retval = usblp_set_protocol(usblp, arg); 578 retval = usblp_set_protocol(usblp, arg);
579 if (retval < 0) { 579 if (retval < 0) {
580 usblp_set_protocol(usblp, 580 usblp_set_protocol(usblp,
581 usblp->current_protocol); 581 usblp->current_protocol);
582 } 582 }
583 break; 583 break;
584 584
585 case IOCNR_HP_SET_CHANNEL: 585 case IOCNR_HP_SET_CHANNEL:
586 if (_IOC_DIR(cmd) != _IOC_WRITE || 586 if (_IOC_DIR(cmd) != _IOC_WRITE ||
587 le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 || 587 le16_to_cpu(usblp->dev->descriptor.idVendor) != 0x03F0 ||
588 usblp->quirks & USBLP_QUIRK_BIDIR) { 588 usblp->quirks & USBLP_QUIRK_BIDIR) {
589 retval = -EINVAL; 589 retval = -EINVAL;
590 goto done; 590 goto done;
591 } 591 }
592 592
593 err = usblp_hp_channel_change_request(usblp, 593 err = usblp_hp_channel_change_request(usblp,
594 arg, &newChannel); 594 arg, &newChannel);
595 if (err < 0) { 595 if (err < 0) {
596 err("usblp%d: error = %d setting " 596 dev_err(&usblp->dev->dev,
597 "HP channel", 597 "usblp%d: error = %d setting "
598 "HP channel\n",
598 usblp->minor, err); 599 usblp->minor, err);
599 retval = -EIO; 600 retval = -EIO;
600 goto done; 601 goto done;
601 } 602 }
602 603
603 dbg("usblp%d requested/got HP channel %ld/%d", 604 dbg("usblp%d requested/got HP channel %ld/%d",
604 usblp->minor, arg, newChannel); 605 usblp->minor, arg, newChannel);
605 break; 606 break;
606 607
607 case IOCNR_GET_BUS_ADDRESS: 608 case IOCNR_GET_BUS_ADDRESS:
608 if (_IOC_DIR(cmd) != _IOC_READ || 609 if (_IOC_DIR(cmd) != _IOC_READ ||
609 _IOC_SIZE(cmd) < sizeof(twoints)) { 610 _IOC_SIZE(cmd) < sizeof(twoints)) {
610 retval = -EINVAL; 611 retval = -EINVAL;
611 goto done; 612 goto done;
612 } 613 }
613 614
614 twoints[0] = usblp->dev->bus->busnum; 615 twoints[0] = usblp->dev->bus->busnum;
615 twoints[1] = usblp->dev->devnum; 616 twoints[1] = usblp->dev->devnum;
616 if (copy_to_user((void __user *)arg, 617 if (copy_to_user((void __user *)arg,
617 (unsigned char *)twoints, 618 (unsigned char *)twoints,
618 sizeof(twoints))) { 619 sizeof(twoints))) {
619 retval = -EFAULT; 620 retval = -EFAULT;
620 goto done; 621 goto done;
621 } 622 }
622 623
623 dbg("usblp%d is bus=%d, device=%d", 624 dbg("usblp%d is bus=%d, device=%d",
624 usblp->minor, twoints[0], twoints[1]); 625 usblp->minor, twoints[0], twoints[1]);
625 break; 626 break;
626 627
627 case IOCNR_GET_VID_PID: 628 case IOCNR_GET_VID_PID:
628 if (_IOC_DIR(cmd) != _IOC_READ || 629 if (_IOC_DIR(cmd) != _IOC_READ ||
629 _IOC_SIZE(cmd) < sizeof(twoints)) { 630 _IOC_SIZE(cmd) < sizeof(twoints)) {
630 retval = -EINVAL; 631 retval = -EINVAL;
631 goto done; 632 goto done;
632 } 633 }
633 634
634 twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor); 635 twoints[0] = le16_to_cpu(usblp->dev->descriptor.idVendor);
635 twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct); 636 twoints[1] = le16_to_cpu(usblp->dev->descriptor.idProduct);
636 if (copy_to_user((void __user *)arg, 637 if (copy_to_user((void __user *)arg,
637 (unsigned char *)twoints, 638 (unsigned char *)twoints,
638 sizeof(twoints))) { 639 sizeof(twoints))) {
639 retval = -EFAULT; 640 retval = -EFAULT;
640 goto done; 641 goto done;
641 } 642 }
642 643
643 dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X", 644 dbg("usblp%d is VID=0x%4.4X, PID=0x%4.4X",
644 usblp->minor, twoints[0], twoints[1]); 645 usblp->minor, twoints[0], twoints[1]);
645 break; 646 break;
646 647
647 case IOCNR_SOFT_RESET: 648 case IOCNR_SOFT_RESET:
648 if (_IOC_DIR(cmd) != _IOC_NONE) { 649 if (_IOC_DIR(cmd) != _IOC_NONE) {
649 retval = -EINVAL; 650 retval = -EINVAL;
650 goto done; 651 goto done;
651 } 652 }
652 retval = usblp_reset(usblp); 653 retval = usblp_reset(usblp);
653 break; 654 break;
654 default: 655 default:
655 retval = -ENOTTY; 656 retval = -ENOTTY;
656 } 657 }
657 else /* old-style ioctl value */ 658 else /* old-style ioctl value */
658 switch (cmd) { 659 switch (cmd) {
659 660
660 case LPGETSTATUS: 661 case LPGETSTATUS:
661 if ((retval = usblp_read_status(usblp, usblp->statusbuf))) { 662 if ((retval = usblp_read_status(usblp, usblp->statusbuf))) {
662 if (printk_ratelimit()) 663 if (printk_ratelimit())
663 printk(KERN_ERR "usblp%d:" 664 printk(KERN_ERR "usblp%d:"
664 "failed reading printer status (%d)\n", 665 "failed reading printer status (%d)\n",
665 usblp->minor, retval); 666 usblp->minor, retval);
666 retval = -EIO; 667 retval = -EIO;
667 goto done; 668 goto done;
668 } 669 }
669 status = *usblp->statusbuf; 670 status = *usblp->statusbuf;
670 if (copy_to_user ((void __user *)arg, &status, sizeof(int))) 671 if (copy_to_user ((void __user *)arg, &status, sizeof(int)))
671 retval = -EFAULT; 672 retval = -EFAULT;
672 break; 673 break;
673 674
674 case LPABORT: 675 case LPABORT:
675 if (arg) 676 if (arg)
676 usblp->flags |= LP_ABORT; 677 usblp->flags |= LP_ABORT;
677 else 678 else
678 usblp->flags &= ~LP_ABORT; 679 usblp->flags &= ~LP_ABORT;
679 break; 680 break;
680 681
681 default: 682 default:
682 retval = -ENOTTY; 683 retval = -ENOTTY;
683 } 684 }
684 685
685 done: 686 done:
686 mutex_unlock (&usblp->mut); 687 mutex_unlock (&usblp->mut);
687 return retval; 688 return retval;
688 } 689 }
689 690
690 static struct urb *usblp_new_writeurb(struct usblp *usblp, int transfer_length) 691 static struct urb *usblp_new_writeurb(struct usblp *usblp, int transfer_length)
691 { 692 {
692 struct urb *urb; 693 struct urb *urb;
693 char *writebuf; 694 char *writebuf;
694 695
695 if ((writebuf = kmalloc(transfer_length, GFP_KERNEL)) == NULL) 696 if ((writebuf = kmalloc(transfer_length, GFP_KERNEL)) == NULL)
696 return NULL; 697 return NULL;
697 if ((urb = usb_alloc_urb(0, GFP_KERNEL)) == NULL) { 698 if ((urb = usb_alloc_urb(0, GFP_KERNEL)) == NULL) {
698 kfree(writebuf); 699 kfree(writebuf);
699 return NULL; 700 return NULL;
700 } 701 }
701 702
702 usb_fill_bulk_urb(urb, usblp->dev, 703 usb_fill_bulk_urb(urb, usblp->dev,
703 usb_sndbulkpipe(usblp->dev, 704 usb_sndbulkpipe(usblp->dev,
704 usblp->protocol[usblp->current_protocol].epwrite->bEndpointAddress), 705 usblp->protocol[usblp->current_protocol].epwrite->bEndpointAddress),
705 writebuf, transfer_length, usblp_bulk_write, usblp); 706 writebuf, transfer_length, usblp_bulk_write, usblp);
706 urb->transfer_flags |= URB_FREE_BUFFER; 707 urb->transfer_flags |= URB_FREE_BUFFER;
707 708
708 return urb; 709 return urb;
709 } 710 }
710 711
711 static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) 712 static ssize_t usblp_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
712 { 713 {
713 struct usblp *usblp = file->private_data; 714 struct usblp *usblp = file->private_data;
714 struct urb *writeurb; 715 struct urb *writeurb;
715 int rv; 716 int rv;
716 int transfer_length; 717 int transfer_length;
717 ssize_t writecount = 0; 718 ssize_t writecount = 0;
718 719
719 if (mutex_lock_interruptible(&usblp->wmut)) { 720 if (mutex_lock_interruptible(&usblp->wmut)) {
720 rv = -EINTR; 721 rv = -EINTR;
721 goto raise_biglock; 722 goto raise_biglock;
722 } 723 }
723 if ((rv = usblp_wwait(usblp, !!(file->f_flags & O_NONBLOCK))) < 0) 724 if ((rv = usblp_wwait(usblp, !!(file->f_flags & O_NONBLOCK))) < 0)
724 goto raise_wait; 725 goto raise_wait;
725 726
726 while (writecount < count) { 727 while (writecount < count) {
727 /* 728 /*
728 * Step 1: Submit next block. 729 * Step 1: Submit next block.
729 */ 730 */
730 if ((transfer_length = count - writecount) > USBLP_BUF_SIZE) 731 if ((transfer_length = count - writecount) > USBLP_BUF_SIZE)
731 transfer_length = USBLP_BUF_SIZE; 732 transfer_length = USBLP_BUF_SIZE;
732 733
733 rv = -ENOMEM; 734 rv = -ENOMEM;
734 if ((writeurb = usblp_new_writeurb(usblp, transfer_length)) == NULL) 735 if ((writeurb = usblp_new_writeurb(usblp, transfer_length)) == NULL)
735 goto raise_urb; 736 goto raise_urb;
736 usb_anchor_urb(writeurb, &usblp->urbs); 737 usb_anchor_urb(writeurb, &usblp->urbs);
737 738
738 if (copy_from_user(writeurb->transfer_buffer, 739 if (copy_from_user(writeurb->transfer_buffer,
739 buffer + writecount, transfer_length)) { 740 buffer + writecount, transfer_length)) {
740 rv = -EFAULT; 741 rv = -EFAULT;
741 goto raise_badaddr; 742 goto raise_badaddr;
742 } 743 }
743 744
744 spin_lock_irq(&usblp->lock); 745 spin_lock_irq(&usblp->lock);
745 usblp->wcomplete = 0; 746 usblp->wcomplete = 0;
746 spin_unlock_irq(&usblp->lock); 747 spin_unlock_irq(&usblp->lock);
747 if ((rv = usb_submit_urb(writeurb, GFP_KERNEL)) < 0) { 748 if ((rv = usb_submit_urb(writeurb, GFP_KERNEL)) < 0) {
748 usblp->wstatus = 0; 749 usblp->wstatus = 0;
749 spin_lock_irq(&usblp->lock); 750 spin_lock_irq(&usblp->lock);
750 usblp->no_paper = 0; 751 usblp->no_paper = 0;
751 usblp->wcomplete = 1; 752 usblp->wcomplete = 1;
752 wake_up(&usblp->wwait); 753 wake_up(&usblp->wwait);
753 spin_unlock_irq(&usblp->lock); 754 spin_unlock_irq(&usblp->lock);
754 if (rv != -ENOMEM) 755 if (rv != -ENOMEM)
755 rv = -EIO; 756 rv = -EIO;
756 goto raise_submit; 757 goto raise_submit;
757 } 758 }
758 759
759 /* 760 /*
760 * Step 2: Wait for transfer to end, collect results. 761 * Step 2: Wait for transfer to end, collect results.
761 */ 762 */
762 rv = usblp_wwait(usblp, !!(file->f_flags&O_NONBLOCK)); 763 rv = usblp_wwait(usblp, !!(file->f_flags&O_NONBLOCK));
763 if (rv < 0) { 764 if (rv < 0) {
764 if (rv == -EAGAIN) { 765 if (rv == -EAGAIN) {
765 /* Presume that it's going to complete well. */ 766 /* Presume that it's going to complete well. */
766 writecount += transfer_length; 767 writecount += transfer_length;
767 } 768 }
768 if (rv == -ENOSPC) { 769 if (rv == -ENOSPC) {
769 spin_lock_irq(&usblp->lock); 770 spin_lock_irq(&usblp->lock);
770 usblp->no_paper = 1; /* Mark for poll(2) */ 771 usblp->no_paper = 1; /* Mark for poll(2) */
771 spin_unlock_irq(&usblp->lock); 772 spin_unlock_irq(&usblp->lock);
772 writecount += transfer_length; 773 writecount += transfer_length;
773 } 774 }
774 /* Leave URB dangling, to be cleaned on close. */ 775 /* Leave URB dangling, to be cleaned on close. */
775 goto collect_error; 776 goto collect_error;
776 } 777 }
777 778
778 if (usblp->wstatus < 0) { 779 if (usblp->wstatus < 0) {
779 rv = -EIO; 780 rv = -EIO;
780 goto collect_error; 781 goto collect_error;
781 } 782 }
782 /* 783 /*
783 * This is critical: it must be our URB, not other writer's. 784 * This is critical: it must be our URB, not other writer's.
784 * The wmut exists mainly to cover us here. 785 * The wmut exists mainly to cover us here.
785 */ 786 */
786 writecount += usblp->wstatus; 787 writecount += usblp->wstatus;
787 } 788 }
788 789
789 mutex_unlock(&usblp->wmut); 790 mutex_unlock(&usblp->wmut);
790 return writecount; 791 return writecount;
791 792
792 raise_submit: 793 raise_submit:
793 raise_badaddr: 794 raise_badaddr:
794 usb_unanchor_urb(writeurb); 795 usb_unanchor_urb(writeurb);
795 usb_free_urb(writeurb); 796 usb_free_urb(writeurb);
796 raise_urb: 797 raise_urb:
797 raise_wait: 798 raise_wait:
798 collect_error: /* Out of raise sequence */ 799 collect_error: /* Out of raise sequence */
799 mutex_unlock(&usblp->wmut); 800 mutex_unlock(&usblp->wmut);
800 raise_biglock: 801 raise_biglock:
801 return writecount ? writecount : rv; 802 return writecount ? writecount : rv;
802 } 803 }
803 804
804 /* 805 /*
805 * Notice that we fail to restart in a few cases: on EFAULT, on restart 806 * Notice that we fail to restart in a few cases: on EFAULT, on restart
806 * error, etc. This is the historical behaviour. In all such cases we return 807 * error, etc. This is the historical behaviour. In all such cases we return
807 * EIO, and applications loop in order to get the new read going. 808 * EIO, and applications loop in order to get the new read going.
808 */ 809 */
809 static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, loff_t *ppos) 810 static ssize_t usblp_read(struct file *file, char __user *buffer, size_t len, loff_t *ppos)
810 { 811 {
811 struct usblp *usblp = file->private_data; 812 struct usblp *usblp = file->private_data;
812 ssize_t count; 813 ssize_t count;
813 ssize_t avail; 814 ssize_t avail;
814 int rv; 815 int rv;
815 816
816 if (!usblp->bidir) 817 if (!usblp->bidir)
817 return -EINVAL; 818 return -EINVAL;
818 819
819 rv = usblp_rwait_and_lock(usblp, !!(file->f_flags & O_NONBLOCK)); 820 rv = usblp_rwait_and_lock(usblp, !!(file->f_flags & O_NONBLOCK));
820 if (rv < 0) 821 if (rv < 0)
821 return rv; 822 return rv;
822 823
823 if ((avail = usblp->rstatus) < 0) { 824 if ((avail = usblp->rstatus) < 0) {
824 printk(KERN_ERR "usblp%d: error %d reading from printer\n", 825 printk(KERN_ERR "usblp%d: error %d reading from printer\n",
825 usblp->minor, (int)avail); 826 usblp->minor, (int)avail);
826 usblp_submit_read(usblp); 827 usblp_submit_read(usblp);
827 count = -EIO; 828 count = -EIO;
828 goto done; 829 goto done;
829 } 830 }
830 831
831 count = len < avail - usblp->readcount ? len : avail - usblp->readcount; 832 count = len < avail - usblp->readcount ? len : avail - usblp->readcount;
832 if (count != 0 && 833 if (count != 0 &&
833 copy_to_user(buffer, usblp->readbuf + usblp->readcount, count)) { 834 copy_to_user(buffer, usblp->readbuf + usblp->readcount, count)) {
834 count = -EFAULT; 835 count = -EFAULT;
835 goto done; 836 goto done;
836 } 837 }
837 838
838 if ((usblp->readcount += count) == avail) { 839 if ((usblp->readcount += count) == avail) {
839 if (usblp_submit_read(usblp) < 0) { 840 if (usblp_submit_read(usblp) < 0) {
840 /* We don't want to leak USB return codes into errno. */ 841 /* We don't want to leak USB return codes into errno. */
841 if (count == 0) 842 if (count == 0)
842 count = -EIO; 843 count = -EIO;
843 goto done; 844 goto done;
844 } 845 }
845 } 846 }
846 847
847 done: 848 done:
848 mutex_unlock (&usblp->mut); 849 mutex_unlock (&usblp->mut);
849 return count; 850 return count;
850 } 851 }
851 852
852 /* 853 /*
853 * Wait for the write path to come idle. 854 * Wait for the write path to come idle.
854 * This is called under the ->wmut, so the idle path stays idle. 855 * This is called under the ->wmut, so the idle path stays idle.
855 * 856 *
856 * Our write path has a peculiar property: it does not buffer like a tty, 857 * Our write path has a peculiar property: it does not buffer like a tty,
857 * but waits for the write to succeed. This allows our ->release to bug out 858 * but waits for the write to succeed. This allows our ->release to bug out
858 * without waiting for writes to drain. But it obviously does not work 859 * without waiting for writes to drain. But it obviously does not work
859 * when O_NONBLOCK is set. So, applications setting O_NONBLOCK must use 860 * when O_NONBLOCK is set. So, applications setting O_NONBLOCK must use
860 * select(2) or poll(2) to wait for the buffer to drain before closing. 861 * select(2) or poll(2) to wait for the buffer to drain before closing.
861 * Alternatively, set blocking mode with fcntl and issue a zero-size write. 862 * Alternatively, set blocking mode with fcntl and issue a zero-size write.
862 */ 863 */
863 static int usblp_wwait(struct usblp *usblp, int nonblock) 864 static int usblp_wwait(struct usblp *usblp, int nonblock)
864 { 865 {
865 DECLARE_WAITQUEUE(waita, current); 866 DECLARE_WAITQUEUE(waita, current);
866 int rc; 867 int rc;
867 int err = 0; 868 int err = 0;
868 869
869 add_wait_queue(&usblp->wwait, &waita); 870 add_wait_queue(&usblp->wwait, &waita);
870 for (;;) { 871 for (;;) {
871 set_current_state(TASK_INTERRUPTIBLE); 872 set_current_state(TASK_INTERRUPTIBLE);
872 if (mutex_lock_interruptible(&usblp->mut)) { 873 if (mutex_lock_interruptible(&usblp->mut)) {
873 rc = -EINTR; 874 rc = -EINTR;
874 break; 875 break;
875 } 876 }
876 rc = usblp_wtest(usblp, nonblock); 877 rc = usblp_wtest(usblp, nonblock);
877 mutex_unlock(&usblp->mut); 878 mutex_unlock(&usblp->mut);
878 if (rc <= 0) 879 if (rc <= 0)
879 break; 880 break;
880 881
881 if (usblp->flags & LP_ABORT) { 882 if (usblp->flags & LP_ABORT) {
882 if (schedule_timeout(msecs_to_jiffies(5000)) == 0) { 883 if (schedule_timeout(msecs_to_jiffies(5000)) == 0) {
883 err = usblp_check_status(usblp, err); 884 err = usblp_check_status(usblp, err);
884 if (err == 1) { /* Paper out */ 885 if (err == 1) { /* Paper out */
885 rc = -ENOSPC; 886 rc = -ENOSPC;
886 break; 887 break;
887 } 888 }
888 } 889 }
889 } else { 890 } else {
890 schedule(); 891 schedule();
891 } 892 }
892 } 893 }
893 set_current_state(TASK_RUNNING); 894 set_current_state(TASK_RUNNING);
894 remove_wait_queue(&usblp->wwait, &waita); 895 remove_wait_queue(&usblp->wwait, &waita);
895 return rc; 896 return rc;
896 } 897 }
897 898
898 static int usblp_wtest(struct usblp *usblp, int nonblock) 899 static int usblp_wtest(struct usblp *usblp, int nonblock)
899 { 900 {
900 unsigned long flags; 901 unsigned long flags;
901 902
902 if (!usblp->present) 903 if (!usblp->present)
903 return -ENODEV; 904 return -ENODEV;
904 if (signal_pending(current)) 905 if (signal_pending(current))
905 return -EINTR; 906 return -EINTR;
906 spin_lock_irqsave(&usblp->lock, flags); 907 spin_lock_irqsave(&usblp->lock, flags);
907 if (usblp->wcomplete) { 908 if (usblp->wcomplete) {
908 spin_unlock_irqrestore(&usblp->lock, flags); 909 spin_unlock_irqrestore(&usblp->lock, flags);
909 return 0; 910 return 0;
910 } 911 }
911 spin_unlock_irqrestore(&usblp->lock, flags); 912 spin_unlock_irqrestore(&usblp->lock, flags);
912 if (usblp->sleeping) 913 if (usblp->sleeping)
913 return -ENODEV; 914 return -ENODEV;
914 if (nonblock) 915 if (nonblock)
915 return -EAGAIN; 916 return -EAGAIN;
916 return 1; 917 return 1;
917 } 918 }
918 919
919 /* 920 /*
920 * Wait for read bytes to become available. This probably should have been 921 * Wait for read bytes to become available. This probably should have been
921 * called usblp_r_lock_and_wait(), because we lock first. But it's a traditional 922 * called usblp_r_lock_and_wait(), because we lock first. But it's a traditional
922 * name for functions which lock and return. 923 * name for functions which lock and return.
923 * 924 *
924 * We do not use wait_event_interruptible because it makes locking iffy. 925 * We do not use wait_event_interruptible because it makes locking iffy.
925 */ 926 */
926 static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock) 927 static int usblp_rwait_and_lock(struct usblp *usblp, int nonblock)
927 { 928 {
928 DECLARE_WAITQUEUE(waita, current); 929 DECLARE_WAITQUEUE(waita, current);
929 int rc; 930 int rc;
930 931
931 add_wait_queue(&usblp->rwait, &waita); 932 add_wait_queue(&usblp->rwait, &waita);
932 for (;;) { 933 for (;;) {
933 if (mutex_lock_interruptible(&usblp->mut)) { 934 if (mutex_lock_interruptible(&usblp->mut)) {
934 rc = -EINTR; 935 rc = -EINTR;
935 break; 936 break;
936 } 937 }
937 set_current_state(TASK_INTERRUPTIBLE); 938 set_current_state(TASK_INTERRUPTIBLE);
938 if ((rc = usblp_rtest(usblp, nonblock)) < 0) { 939 if ((rc = usblp_rtest(usblp, nonblock)) < 0) {
939 mutex_unlock(&usblp->mut); 940 mutex_unlock(&usblp->mut);
940 break; 941 break;
941 } 942 }
942 if (rc == 0) /* Keep it locked */ 943 if (rc == 0) /* Keep it locked */
943 break; 944 break;
944 mutex_unlock(&usblp->mut); 945 mutex_unlock(&usblp->mut);
945 schedule(); 946 schedule();
946 } 947 }
947 set_current_state(TASK_RUNNING); 948 set_current_state(TASK_RUNNING);
948 remove_wait_queue(&usblp->rwait, &waita); 949 remove_wait_queue(&usblp->rwait, &waita);
949 return rc; 950 return rc;
950 } 951 }
951 952
952 static int usblp_rtest(struct usblp *usblp, int nonblock) 953 static int usblp_rtest(struct usblp *usblp, int nonblock)
953 { 954 {
954 unsigned long flags; 955 unsigned long flags;
955 956
956 if (!usblp->present) 957 if (!usblp->present)
957 return -ENODEV; 958 return -ENODEV;
958 if (signal_pending(current)) 959 if (signal_pending(current))
959 return -EINTR; 960 return -EINTR;
960 spin_lock_irqsave(&usblp->lock, flags); 961 spin_lock_irqsave(&usblp->lock, flags);
961 if (usblp->rcomplete) { 962 if (usblp->rcomplete) {
962 spin_unlock_irqrestore(&usblp->lock, flags); 963 spin_unlock_irqrestore(&usblp->lock, flags);
963 return 0; 964 return 0;
964 } 965 }
965 spin_unlock_irqrestore(&usblp->lock, flags); 966 spin_unlock_irqrestore(&usblp->lock, flags);
966 if (usblp->sleeping) 967 if (usblp->sleeping)
967 return -ENODEV; 968 return -ENODEV;
968 if (nonblock) 969 if (nonblock)
969 return -EAGAIN; 970 return -EAGAIN;
970 return 1; 971 return 1;
971 } 972 }
972 973
973 /* 974 /*
974 * Please check ->bidir and other such things outside for now. 975 * Please check ->bidir and other such things outside for now.
975 */ 976 */
976 static int usblp_submit_read(struct usblp *usblp) 977 static int usblp_submit_read(struct usblp *usblp)
977 { 978 {
978 struct urb *urb; 979 struct urb *urb;
979 unsigned long flags; 980 unsigned long flags;
980 int rc; 981 int rc;
981 982
982 rc = -ENOMEM; 983 rc = -ENOMEM;
983 if ((urb = usb_alloc_urb(0, GFP_KERNEL)) == NULL) 984 if ((urb = usb_alloc_urb(0, GFP_KERNEL)) == NULL)
984 goto raise_urb; 985 goto raise_urb;
985 986
986 usb_fill_bulk_urb(urb, usblp->dev, 987 usb_fill_bulk_urb(urb, usblp->dev,
987 usb_rcvbulkpipe(usblp->dev, 988 usb_rcvbulkpipe(usblp->dev,
988 usblp->protocol[usblp->current_protocol].epread->bEndpointAddress), 989 usblp->protocol[usblp->current_protocol].epread->bEndpointAddress),
989 usblp->readbuf, USBLP_BUF_SIZE_IN, 990 usblp->readbuf, USBLP_BUF_SIZE_IN,
990 usblp_bulk_read, usblp); 991 usblp_bulk_read, usblp);
991 usb_anchor_urb(urb, &usblp->urbs); 992 usb_anchor_urb(urb, &usblp->urbs);
992 993
993 spin_lock_irqsave(&usblp->lock, flags); 994 spin_lock_irqsave(&usblp->lock, flags);
994 usblp->readcount = 0; /* XXX Why here? */ 995 usblp->readcount = 0; /* XXX Why here? */
995 usblp->rcomplete = 0; 996 usblp->rcomplete = 0;
996 spin_unlock_irqrestore(&usblp->lock, flags); 997 spin_unlock_irqrestore(&usblp->lock, flags);
997 if ((rc = usb_submit_urb(urb, GFP_KERNEL)) < 0) { 998 if ((rc = usb_submit_urb(urb, GFP_KERNEL)) < 0) {
998 dbg("error submitting urb (%d)", rc); 999 dbg("error submitting urb (%d)", rc);
999 spin_lock_irqsave(&usblp->lock, flags); 1000 spin_lock_irqsave(&usblp->lock, flags);
1000 usblp->rstatus = rc; 1001 usblp->rstatus = rc;
1001 usblp->rcomplete = 1; 1002 usblp->rcomplete = 1;
1002 spin_unlock_irqrestore(&usblp->lock, flags); 1003 spin_unlock_irqrestore(&usblp->lock, flags);
1003 goto raise_submit; 1004 goto raise_submit;
1004 } 1005 }
1005 1006
1006 return 0; 1007 return 0;
1007 1008
1008 raise_submit: 1009 raise_submit:
1009 usb_unanchor_urb(urb); 1010 usb_unanchor_urb(urb);
1010 usb_free_urb(urb); 1011 usb_free_urb(urb);
1011 raise_urb: 1012 raise_urb:
1012 return rc; 1013 return rc;
1013 } 1014 }
1014 1015
1015 /* 1016 /*
1016 * Checks for printers that have quirks, such as requiring unidirectional 1017 * Checks for printers that have quirks, such as requiring unidirectional
1017 * communication but reporting bidirectional; currently some HP printers 1018 * communication but reporting bidirectional; currently some HP printers
1018 * have this flaw (HP 810, 880, 895, etc.), or needing an init string 1019 * have this flaw (HP 810, 880, 895, etc.), or needing an init string
1019 * sent at each open (like some Epsons). 1020 * sent at each open (like some Epsons).
1020 * Returns 1 if found, 0 if not found. 1021 * Returns 1 if found, 0 if not found.
1021 * 1022 *
1022 * HP recommended that we use the bidirectional interface but 1023 * HP recommended that we use the bidirectional interface but
1023 * don't attempt any bulk IN transfers from the IN endpoint. 1024 * don't attempt any bulk IN transfers from the IN endpoint.
1024 * Here's some more detail on the problem: 1025 * Here's some more detail on the problem:
1025 * The problem is not that it isn't bidirectional though. The problem 1026 * The problem is not that it isn't bidirectional though. The problem
1026 * is that if you request a device ID, or status information, while 1027 * is that if you request a device ID, or status information, while
1027 * the buffers are full, the return data will end up in the print data 1028 * the buffers are full, the return data will end up in the print data
1028 * buffer. For example if you make sure you never request the device ID 1029 * buffer. For example if you make sure you never request the device ID
1029 * while you are sending print data, and you don't try to query the 1030 * while you are sending print data, and you don't try to query the
1030 * printer status every couple of milliseconds, you will probably be OK. 1031 * printer status every couple of milliseconds, you will probably be OK.
1031 */ 1032 */
1032 static unsigned int usblp_quirks (__u16 vendor, __u16 product) 1033 static unsigned int usblp_quirks (__u16 vendor, __u16 product)
1033 { 1034 {
1034 int i; 1035 int i;
1035 1036
1036 for (i = 0; quirk_printers[i].vendorId; i++) { 1037 for (i = 0; quirk_printers[i].vendorId; i++) {
1037 if (vendor == quirk_printers[i].vendorId && 1038 if (vendor == quirk_printers[i].vendorId &&
1038 product == quirk_printers[i].productId) 1039 product == quirk_printers[i].productId)
1039 return quirk_printers[i].quirks; 1040 return quirk_printers[i].quirks;
1040 } 1041 }
1041 return 0; 1042 return 0;
1042 } 1043 }
1043 1044
1044 static const struct file_operations usblp_fops = { 1045 static const struct file_operations usblp_fops = {
1045 .owner = THIS_MODULE, 1046 .owner = THIS_MODULE,
1046 .read = usblp_read, 1047 .read = usblp_read,
1047 .write = usblp_write, 1048 .write = usblp_write,
1048 .poll = usblp_poll, 1049 .poll = usblp_poll,
1049 .unlocked_ioctl = usblp_ioctl, 1050 .unlocked_ioctl = usblp_ioctl,
1050 .compat_ioctl = usblp_ioctl, 1051 .compat_ioctl = usblp_ioctl,
1051 .open = usblp_open, 1052 .open = usblp_open,
1052 .release = usblp_release, 1053 .release = usblp_release,
1053 }; 1054 };
1054 1055
1055 static struct usb_class_driver usblp_class = { 1056 static struct usb_class_driver usblp_class = {
1056 .name = "lp%d", 1057 .name = "lp%d",
1057 .fops = &usblp_fops, 1058 .fops = &usblp_fops,
1058 .minor_base = USBLP_MINOR_BASE, 1059 .minor_base = USBLP_MINOR_BASE,
1059 }; 1060 };
1060 1061
1061 static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf) 1062 static ssize_t usblp_show_ieee1284_id(struct device *dev, struct device_attribute *attr, char *buf)
1062 { 1063 {
1063 struct usb_interface *intf = to_usb_interface(dev); 1064 struct usb_interface *intf = to_usb_interface(dev);
1064 struct usblp *usblp = usb_get_intfdata (intf); 1065 struct usblp *usblp = usb_get_intfdata (intf);
1065 1066
1066 if (usblp->device_id_string[0] == 0 && 1067 if (usblp->device_id_string[0] == 0 &&
1067 usblp->device_id_string[1] == 0) 1068 usblp->device_id_string[1] == 0)
1068 return 0; 1069 return 0;
1069 1070
1070 return sprintf(buf, "%s", usblp->device_id_string+2); 1071 return sprintf(buf, "%s", usblp->device_id_string+2);
1071 } 1072 }
1072 1073
1073 static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL); 1074 static DEVICE_ATTR(ieee1284_id, S_IRUGO, usblp_show_ieee1284_id, NULL);
1074 1075
1075 static int usblp_probe(struct usb_interface *intf, 1076 static int usblp_probe(struct usb_interface *intf,
1076 const struct usb_device_id *id) 1077 const struct usb_device_id *id)
1077 { 1078 {
1078 struct usb_device *dev = interface_to_usbdev (intf); 1079 struct usb_device *dev = interface_to_usbdev (intf);
1079 struct usblp *usblp; 1080 struct usblp *usblp;
1080 int protocol; 1081 int protocol;
1081 int retval; 1082 int retval;
1082 1083
1083 /* Malloc and start initializing usblp structure so we can use it 1084 /* Malloc and start initializing usblp structure so we can use it
1084 * directly. */ 1085 * directly. */
1085 usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL); 1086 usblp = kzalloc(sizeof(struct usblp), GFP_KERNEL);
1086 if (!usblp) { 1087 if (!usblp) {
1087 retval = -ENOMEM; 1088 retval = -ENOMEM;
1088 goto abort_ret; 1089 goto abort_ret;
1089 } 1090 }
1090 usblp->dev = dev; 1091 usblp->dev = dev;
1091 mutex_init(&usblp->wmut); 1092 mutex_init(&usblp->wmut);
1092 mutex_init (&usblp->mut); 1093 mutex_init (&usblp->mut);
1093 spin_lock_init(&usblp->lock); 1094 spin_lock_init(&usblp->lock);
1094 init_waitqueue_head(&usblp->rwait); 1095 init_waitqueue_head(&usblp->rwait);
1095 init_waitqueue_head(&usblp->wwait); 1096 init_waitqueue_head(&usblp->wwait);
1096 init_usb_anchor(&usblp->urbs); 1097 init_usb_anchor(&usblp->urbs);
1097 usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber; 1098 usblp->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
1098 usblp->intf = intf; 1099 usblp->intf = intf;
1099 1100
1100 /* Malloc device ID string buffer to the largest expected length, 1101 /* Malloc device ID string buffer to the largest expected length,
1101 * since we can re-query it on an ioctl and a dynamic string 1102 * since we can re-query it on an ioctl and a dynamic string
1102 * could change in length. */ 1103 * could change in length. */
1103 if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) { 1104 if (!(usblp->device_id_string = kmalloc(USBLP_DEVICE_ID_SIZE, GFP_KERNEL))) {
1104 retval = -ENOMEM; 1105 retval = -ENOMEM;
1105 goto abort; 1106 goto abort;
1106 } 1107 }
1107 1108
1108 /* 1109 /*
1109 * Allocate read buffer. We somewhat wastefully 1110 * Allocate read buffer. We somewhat wastefully
1110 * malloc both regardless of bidirectionality, because the 1111 * malloc both regardless of bidirectionality, because the
1111 * alternate setting can be changed later via an ioctl. 1112 * alternate setting can be changed later via an ioctl.
1112 */ 1113 */
1113 if (!(usblp->readbuf = kmalloc(USBLP_BUF_SIZE_IN, GFP_KERNEL))) { 1114 if (!(usblp->readbuf = kmalloc(USBLP_BUF_SIZE_IN, GFP_KERNEL))) {
1114 retval = -ENOMEM; 1115 retval = -ENOMEM;
1115 goto abort; 1116 goto abort;
1116 } 1117 }
1117 1118
1118 /* Allocate buffer for printer status */ 1119 /* Allocate buffer for printer status */
1119 usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL); 1120 usblp->statusbuf = kmalloc(STATUS_BUF_SIZE, GFP_KERNEL);
1120 if (!usblp->statusbuf) { 1121 if (!usblp->statusbuf) {
1121 retval = -ENOMEM; 1122 retval = -ENOMEM;
1122 goto abort; 1123 goto abort;
1123 } 1124 }
1124 1125
1125 /* Lookup quirks for this printer. */ 1126 /* Lookup quirks for this printer. */
1126 usblp->quirks = usblp_quirks( 1127 usblp->quirks = usblp_quirks(
1127 le16_to_cpu(dev->descriptor.idVendor), 1128 le16_to_cpu(dev->descriptor.idVendor),
1128 le16_to_cpu(dev->descriptor.idProduct)); 1129 le16_to_cpu(dev->descriptor.idProduct));
1129 1130
1130 /* Analyze and pick initial alternate settings and endpoints. */ 1131 /* Analyze and pick initial alternate settings and endpoints. */
1131 protocol = usblp_select_alts(usblp); 1132 protocol = usblp_select_alts(usblp);
1132 if (protocol < 0) { 1133 if (protocol < 0) {
1133 dbg("incompatible printer-class device 0x%4.4X/0x%4.4X", 1134 dbg("incompatible printer-class device 0x%4.4X/0x%4.4X",
1134 le16_to_cpu(dev->descriptor.idVendor), 1135 le16_to_cpu(dev->descriptor.idVendor),
1135 le16_to_cpu(dev->descriptor.idProduct)); 1136 le16_to_cpu(dev->descriptor.idProduct));
1136 retval = -ENODEV; 1137 retval = -ENODEV;
1137 goto abort; 1138 goto abort;
1138 } 1139 }
1139 1140
1140 /* Setup the selected alternate setting and endpoints. */ 1141 /* Setup the selected alternate setting and endpoints. */
1141 if (usblp_set_protocol(usblp, protocol) < 0) { 1142 if (usblp_set_protocol(usblp, protocol) < 0) {
1142 retval = -ENODEV; /* ->probe isn't ->ioctl */ 1143 retval = -ENODEV; /* ->probe isn't ->ioctl */
1143 goto abort; 1144 goto abort;
1144 } 1145 }
1145 1146
1146 /* Retrieve and store the device ID string. */ 1147 /* Retrieve and store the device ID string. */
1147 usblp_cache_device_id_string(usblp); 1148 usblp_cache_device_id_string(usblp);
1148 retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id); 1149 retval = device_create_file(&intf->dev, &dev_attr_ieee1284_id);
1149 if (retval) 1150 if (retval)
1150 goto abort_intfdata; 1151 goto abort_intfdata;
1151 1152
1152 #ifdef DEBUG 1153 #ifdef DEBUG
1153 usblp_check_status(usblp, 0); 1154 usblp_check_status(usblp, 0);
1154 #endif 1155 #endif
1155 1156
1156 usb_set_intfdata (intf, usblp); 1157 usb_set_intfdata (intf, usblp);
1157 1158
1158 usblp->present = 1; 1159 usblp->present = 1;
1159 1160
1160 retval = usb_register_dev(intf, &usblp_class); 1161 retval = usb_register_dev(intf, &usblp_class);
1161 if (retval) { 1162 if (retval) {
1162 printk(KERN_ERR "usblp: Not able to get a minor" 1163 printk(KERN_ERR "usblp: Not able to get a minor"
1163 " (base %u, slice default): %d\n", 1164 " (base %u, slice default): %d\n",
1164 USBLP_MINOR_BASE, retval); 1165 USBLP_MINOR_BASE, retval);
1165 goto abort_intfdata; 1166 goto abort_intfdata;
1166 } 1167 }
1167 usblp->minor = intf->minor; 1168 usblp->minor = intf->minor;
1168 printk(KERN_INFO "usblp%d: USB %sdirectional printer dev %d " 1169 printk(KERN_INFO "usblp%d: USB %sdirectional printer dev %d "
1169 "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X\n", 1170 "if %d alt %d proto %d vid 0x%4.4X pid 0x%4.4X\n",
1170 usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum, 1171 usblp->minor, usblp->bidir ? "Bi" : "Uni", dev->devnum,
1171 usblp->ifnum, 1172 usblp->ifnum,
1172 usblp->protocol[usblp->current_protocol].alt_setting, 1173 usblp->protocol[usblp->current_protocol].alt_setting,
1173 usblp->current_protocol, 1174 usblp->current_protocol,
1174 le16_to_cpu(usblp->dev->descriptor.idVendor), 1175 le16_to_cpu(usblp->dev->descriptor.idVendor),
1175 le16_to_cpu(usblp->dev->descriptor.idProduct)); 1176 le16_to_cpu(usblp->dev->descriptor.idProduct));
1176 1177
1177 return 0; 1178 return 0;
1178 1179
1179 abort_intfdata: 1180 abort_intfdata:
1180 usb_set_intfdata (intf, NULL); 1181 usb_set_intfdata (intf, NULL);
1181 device_remove_file(&intf->dev, &dev_attr_ieee1284_id); 1182 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
1182 abort: 1183 abort:
1183 kfree(usblp->readbuf); 1184 kfree(usblp->readbuf);
1184 kfree(usblp->statusbuf); 1185 kfree(usblp->statusbuf);
1185 kfree(usblp->device_id_string); 1186 kfree(usblp->device_id_string);
1186 kfree(usblp); 1187 kfree(usblp);
1187 abort_ret: 1188 abort_ret:
1188 return retval; 1189 return retval;
1189 } 1190 }
1190 1191
1191 /* 1192 /*
1192 * We are a "new" style driver with usb_device_id table, 1193 * We are a "new" style driver with usb_device_id table,
1193 * but our requirements are too intricate for simple match to handle. 1194 * but our requirements are too intricate for simple match to handle.
1194 * 1195 *
1195 * The "proto_bias" option may be used to specify the preferred protocol 1196 * The "proto_bias" option may be used to specify the preferred protocol
1196 * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3). If the device 1197 * for all USB printers (1=7/1/1, 2=7/1/2, 3=7/1/3). If the device
1197 * supports the preferred protocol, then we bind to it. 1198 * supports the preferred protocol, then we bind to it.
1198 * 1199 *
1199 * The best interface for us is 7/1/2, because it is compatible 1200 * The best interface for us is 7/1/2, because it is compatible
1200 * with a stream of characters. If we find it, we bind to it. 1201 * with a stream of characters. If we find it, we bind to it.
1201 * 1202 *
1202 * Note that the people from hpoj.sourceforge.net need to be able to 1203 * Note that the people from hpoj.sourceforge.net need to be able to
1203 * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose. 1204 * bind to 7/1/3 (MLC/1284.4), so we provide them ioctls for this purpose.
1204 * 1205 *
1205 * Failing 7/1/2, we look for 7/1/3, even though it's probably not 1206 * Failing 7/1/2, we look for 7/1/3, even though it's probably not
1206 * stream-compatible, because this matches the behaviour of the old code. 1207 * stream-compatible, because this matches the behaviour of the old code.
1207 * 1208 *
1208 * If nothing else, we bind to 7/1/1 - the unidirectional interface. 1209 * If nothing else, we bind to 7/1/1 - the unidirectional interface.
1209 */ 1210 */
1210 static int usblp_select_alts(struct usblp *usblp) 1211 static int usblp_select_alts(struct usblp *usblp)
1211 { 1212 {
1212 struct usb_interface *if_alt; 1213 struct usb_interface *if_alt;
1213 struct usb_host_interface *ifd; 1214 struct usb_host_interface *ifd;
1214 struct usb_endpoint_descriptor *epd, *epwrite, *epread; 1215 struct usb_endpoint_descriptor *epd, *epwrite, *epread;
1215 int p, i, e; 1216 int p, i, e;
1216 1217
1217 if_alt = usblp->intf; 1218 if_alt = usblp->intf;
1218 1219
1219 for (p = 0; p < USBLP_MAX_PROTOCOLS; p++) 1220 for (p = 0; p < USBLP_MAX_PROTOCOLS; p++)
1220 usblp->protocol[p].alt_setting = -1; 1221 usblp->protocol[p].alt_setting = -1;
1221 1222
1222 /* Find out what we have. */ 1223 /* Find out what we have. */
1223 for (i = 0; i < if_alt->num_altsetting; i++) { 1224 for (i = 0; i < if_alt->num_altsetting; i++) {
1224 ifd = &if_alt->altsetting[i]; 1225 ifd = &if_alt->altsetting[i];
1225 1226
1226 if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1) 1227 if (ifd->desc.bInterfaceClass != 7 || ifd->desc.bInterfaceSubClass != 1)
1227 if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS)) 1228 if (!(usblp->quirks & USBLP_QUIRK_BAD_CLASS))
1228 continue; 1229 continue;
1229 1230
1230 if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL || 1231 if (ifd->desc.bInterfaceProtocol < USBLP_FIRST_PROTOCOL ||
1231 ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL) 1232 ifd->desc.bInterfaceProtocol > USBLP_LAST_PROTOCOL)
1232 continue; 1233 continue;
1233 1234
1234 /* Look for bulk OUT and IN endpoints. */ 1235 /* Look for bulk OUT and IN endpoints. */
1235 epwrite = epread = NULL; 1236 epwrite = epread = NULL;
1236 for (e = 0; e < ifd->desc.bNumEndpoints; e++) { 1237 for (e = 0; e < ifd->desc.bNumEndpoints; e++) {
1237 epd = &ifd->endpoint[e].desc; 1238 epd = &ifd->endpoint[e].desc;
1238 1239
1239 if (usb_endpoint_is_bulk_out(epd)) 1240 if (usb_endpoint_is_bulk_out(epd))
1240 if (!epwrite) 1241 if (!epwrite)
1241 epwrite = epd; 1242 epwrite = epd;
1242 1243
1243 if (usb_endpoint_is_bulk_in(epd)) 1244 if (usb_endpoint_is_bulk_in(epd))
1244 if (!epread) 1245 if (!epread)
1245 epread = epd; 1246 epread = epd;
1246 } 1247 }
1247 1248
1248 /* Ignore buggy hardware without the right endpoints. */ 1249 /* Ignore buggy hardware without the right endpoints. */
1249 if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread)) 1250 if (!epwrite || (ifd->desc.bInterfaceProtocol > 1 && !epread))
1250 continue; 1251 continue;
1251 1252
1252 /* Turn off reads for 7/1/1 (unidirectional) interfaces 1253 /* Turn off reads for 7/1/1 (unidirectional) interfaces
1253 * and buggy bidirectional printers. */ 1254 * and buggy bidirectional printers. */
1254 if (ifd->desc.bInterfaceProtocol == 1) { 1255 if (ifd->desc.bInterfaceProtocol == 1) {
1255 epread = NULL; 1256 epread = NULL;
1256 } else if (usblp->quirks & USBLP_QUIRK_BIDIR) { 1257 } else if (usblp->quirks & USBLP_QUIRK_BIDIR) {
1257 printk(KERN_INFO "usblp%d: Disabling reads from " 1258 printk(KERN_INFO "usblp%d: Disabling reads from "
1258 "problematic bidirectional printer\n", 1259 "problematic bidirectional printer\n",
1259 usblp->minor); 1260 usblp->minor);
1260 epread = NULL; 1261 epread = NULL;
1261 } 1262 }
1262 1263
1263 usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting = 1264 usblp->protocol[ifd->desc.bInterfaceProtocol].alt_setting =
1264 ifd->desc.bAlternateSetting; 1265 ifd->desc.bAlternateSetting;
1265 usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite; 1266 usblp->protocol[ifd->desc.bInterfaceProtocol].epwrite = epwrite;
1266 usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread; 1267 usblp->protocol[ifd->desc.bInterfaceProtocol].epread = epread;
1267 } 1268 }
1268 1269
1269 /* If our requested protocol is supported, then use it. */ 1270 /* If our requested protocol is supported, then use it. */
1270 if (proto_bias >= USBLP_FIRST_PROTOCOL && 1271 if (proto_bias >= USBLP_FIRST_PROTOCOL &&
1271 proto_bias <= USBLP_LAST_PROTOCOL && 1272 proto_bias <= USBLP_LAST_PROTOCOL &&
1272 usblp->protocol[proto_bias].alt_setting != -1) 1273 usblp->protocol[proto_bias].alt_setting != -1)
1273 return proto_bias; 1274 return proto_bias;
1274 1275
1275 /* Ordering is important here. */ 1276 /* Ordering is important here. */
1276 if (usblp->protocol[2].alt_setting != -1) 1277 if (usblp->protocol[2].alt_setting != -1)
1277 return 2; 1278 return 2;
1278 if (usblp->protocol[1].alt_setting != -1) 1279 if (usblp->protocol[1].alt_setting != -1)
1279 return 1; 1280 return 1;
1280 if (usblp->protocol[3].alt_setting != -1) 1281 if (usblp->protocol[3].alt_setting != -1)
1281 return 3; 1282 return 3;
1282 1283
1283 /* If nothing is available, then don't bind to this device. */ 1284 /* If nothing is available, then don't bind to this device. */
1284 return -1; 1285 return -1;
1285 } 1286 }
1286 1287
1287 static int usblp_set_protocol(struct usblp *usblp, int protocol) 1288 static int usblp_set_protocol(struct usblp *usblp, int protocol)
1288 { 1289 {
1289 int r, alts; 1290 int r, alts;
1290 1291
1291 if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL) 1292 if (protocol < USBLP_FIRST_PROTOCOL || protocol > USBLP_LAST_PROTOCOL)
1292 return -EINVAL; 1293 return -EINVAL;
1293 1294
1294 alts = usblp->protocol[protocol].alt_setting; 1295 alts = usblp->protocol[protocol].alt_setting;
1295 if (alts < 0) 1296 if (alts < 0)
1296 return -EINVAL; 1297 return -EINVAL;
1297 r = usb_set_interface(usblp->dev, usblp->ifnum, alts); 1298 r = usb_set_interface(usblp->dev, usblp->ifnum, alts);
1298 if (r < 0) { 1299 if (r < 0) {
1299 printk(KERN_ERR "usblp: can't set desired altsetting %d on interface %d\n", 1300 printk(KERN_ERR "usblp: can't set desired altsetting %d on interface %d\n",
1300 alts, usblp->ifnum); 1301 alts, usblp->ifnum);
1301 return r; 1302 return r;
1302 } 1303 }
1303 1304
1304 usblp->bidir = (usblp->protocol[protocol].epread != NULL); 1305 usblp->bidir = (usblp->protocol[protocol].epread != NULL);
1305 usblp->current_protocol = protocol; 1306 usblp->current_protocol = protocol;
1306 dbg("usblp%d set protocol %d", usblp->minor, protocol); 1307 dbg("usblp%d set protocol %d", usblp->minor, protocol);
1307 return 0; 1308 return 0;
1308 } 1309 }
1309 1310
1310 /* Retrieves and caches device ID string. 1311 /* Retrieves and caches device ID string.
1311 * Returns length, including length bytes but not null terminator. 1312 * Returns length, including length bytes but not null terminator.
1312 * On error, returns a negative errno value. */ 1313 * On error, returns a negative errno value. */
1313 static int usblp_cache_device_id_string(struct usblp *usblp) 1314 static int usblp_cache_device_id_string(struct usblp *usblp)
1314 { 1315 {
1315 int err, length; 1316 int err, length;
1316 1317
1317 err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1); 1318 err = usblp_get_id(usblp, 0, usblp->device_id_string, USBLP_DEVICE_ID_SIZE - 1);
1318 if (err < 0) { 1319 if (err < 0) {
1319 dbg("usblp%d: error = %d reading IEEE-1284 Device ID string", 1320 dbg("usblp%d: error = %d reading IEEE-1284 Device ID string",
1320 usblp->minor, err); 1321 usblp->minor, err);
1321 usblp->device_id_string[0] = usblp->device_id_string[1] = '\0'; 1322 usblp->device_id_string[0] = usblp->device_id_string[1] = '\0';
1322 return -EIO; 1323 return -EIO;
1323 } 1324 }
1324 1325
1325 /* First two bytes are length in big-endian. 1326 /* First two bytes are length in big-endian.
1326 * They count themselves, and we copy them into 1327 * They count themselves, and we copy them into
1327 * the user's buffer. */ 1328 * the user's buffer. */
1328 length = be16_to_cpu(*((__be16 *)usblp->device_id_string)); 1329 length = be16_to_cpu(*((__be16 *)usblp->device_id_string));
1329 if (length < 2) 1330 if (length < 2)
1330 length = 2; 1331 length = 2;
1331 else if (length >= USBLP_DEVICE_ID_SIZE) 1332 else if (length >= USBLP_DEVICE_ID_SIZE)
1332 length = USBLP_DEVICE_ID_SIZE - 1; 1333 length = USBLP_DEVICE_ID_SIZE - 1;
1333 usblp->device_id_string[length] = '\0'; 1334 usblp->device_id_string[length] = '\0';
1334 1335
1335 dbg("usblp%d Device ID string [len=%d]=\"%s\"", 1336 dbg("usblp%d Device ID string [len=%d]=\"%s\"",
1336 usblp->minor, length, &usblp->device_id_string[2]); 1337 usblp->minor, length, &usblp->device_id_string[2]);
1337 1338
1338 return length; 1339 return length;
1339 } 1340 }
1340 1341
1341 static void usblp_disconnect(struct usb_interface *intf) 1342 static void usblp_disconnect(struct usb_interface *intf)
1342 { 1343 {
1343 struct usblp *usblp = usb_get_intfdata (intf); 1344 struct usblp *usblp = usb_get_intfdata (intf);
1344 1345
1345 usb_deregister_dev(intf, &usblp_class); 1346 usb_deregister_dev(intf, &usblp_class);
1346 1347
1347 if (!usblp || !usblp->dev) { 1348 if (!usblp || !usblp->dev) {
1348 err("bogus disconnect"); 1349 dev_err(&intf->dev, "bogus disconnect\n");
1349 BUG (); 1350 BUG ();
1350 } 1351 }
1351 1352
1352 device_remove_file(&intf->dev, &dev_attr_ieee1284_id); 1353 device_remove_file(&intf->dev, &dev_attr_ieee1284_id);
1353 1354
1354 mutex_lock (&usblp_mutex); 1355 mutex_lock (&usblp_mutex);
1355 mutex_lock (&usblp->mut); 1356 mutex_lock (&usblp->mut);
1356 usblp->present = 0; 1357 usblp->present = 0;
1357 wake_up(&usblp->wwait); 1358 wake_up(&usblp->wwait);
1358 wake_up(&usblp->rwait); 1359 wake_up(&usblp->rwait);
1359 usb_set_intfdata (intf, NULL); 1360 usb_set_intfdata (intf, NULL);
1360 1361
1361 usblp_unlink_urbs(usblp); 1362 usblp_unlink_urbs(usblp);
1362 mutex_unlock (&usblp->mut); 1363 mutex_unlock (&usblp->mut);
1363 1364
1364 if (!usblp->used) 1365 if (!usblp->used)
1365 usblp_cleanup (usblp); 1366 usblp_cleanup (usblp);
1366 mutex_unlock (&usblp_mutex); 1367 mutex_unlock (&usblp_mutex);
1367 } 1368 }
1368 1369
1369 static int usblp_suspend (struct usb_interface *intf, pm_message_t message) 1370 static int usblp_suspend (struct usb_interface *intf, pm_message_t message)
1370 { 1371 {
1371 struct usblp *usblp = usb_get_intfdata (intf); 1372 struct usblp *usblp = usb_get_intfdata (intf);
1372 1373
1373 /* we take no more IO */ 1374 /* we take no more IO */
1374 usblp->sleeping = 1; 1375 usblp->sleeping = 1;
1375 usblp_unlink_urbs(usblp); 1376 usblp_unlink_urbs(usblp);
1376 #if 0 /* XXX Do we want this? What if someone is reading, should we fail? */ 1377 #if 0 /* XXX Do we want this? What if someone is reading, should we fail? */
1377 /* not strictly necessary, but just in case */ 1378 /* not strictly necessary, but just in case */
1378 wake_up(&usblp->wwait); 1379 wake_up(&usblp->wwait);
1379 wake_up(&usblp->rwait); 1380 wake_up(&usblp->rwait);
1380 #endif 1381 #endif
1381 1382
1382 return 0; 1383 return 0;
1383 } 1384 }
1384 1385
1385 static int usblp_resume (struct usb_interface *intf) 1386 static int usblp_resume (struct usb_interface *intf)
1386 { 1387 {
1387 struct usblp *usblp = usb_get_intfdata (intf); 1388 struct usblp *usblp = usb_get_intfdata (intf);
1388 int r; 1389 int r;
1389 1390
1390 usblp->sleeping = 0; 1391 usblp->sleeping = 0;
1391 r = handle_bidir (usblp); 1392 r = handle_bidir (usblp);
1392 1393
1393 return r; 1394 return r;
1394 } 1395 }
1395 1396
1396 static struct usb_device_id usblp_ids [] = { 1397 static struct usb_device_id usblp_ids [] = {
1397 { USB_DEVICE_INFO(7, 1, 1) }, 1398 { USB_DEVICE_INFO(7, 1, 1) },
1398 { USB_DEVICE_INFO(7, 1, 2) }, 1399 { USB_DEVICE_INFO(7, 1, 2) },
1399 { USB_DEVICE_INFO(7, 1, 3) }, 1400 { USB_DEVICE_INFO(7, 1, 3) },
1400 { USB_INTERFACE_INFO(7, 1, 1) }, 1401 { USB_INTERFACE_INFO(7, 1, 1) },
1401 { USB_INTERFACE_INFO(7, 1, 2) }, 1402 { USB_INTERFACE_INFO(7, 1, 2) },
1402 { USB_INTERFACE_INFO(7, 1, 3) }, 1403 { USB_INTERFACE_INFO(7, 1, 3) },
1403 { USB_DEVICE(0x04b8, 0x0202) }, /* Seiko Epson Receipt Printer M129C */ 1404 { USB_DEVICE(0x04b8, 0x0202) }, /* Seiko Epson Receipt Printer M129C */
1404 { } /* Terminating entry */ 1405 { } /* Terminating entry */
1405 }; 1406 };
1406 1407
1407 MODULE_DEVICE_TABLE (usb, usblp_ids); 1408 MODULE_DEVICE_TABLE (usb, usblp_ids);
1408 1409
1409 static struct usb_driver usblp_driver = { 1410 static struct usb_driver usblp_driver = {
1410 .name = "usblp", 1411 .name = "usblp",
1411 .probe = usblp_probe, 1412 .probe = usblp_probe,
1412 .disconnect = usblp_disconnect, 1413 .disconnect = usblp_disconnect,
1413 .suspend = usblp_suspend, 1414 .suspend = usblp_suspend,
1414 .resume = usblp_resume, 1415 .resume = usblp_resume,
1415 .id_table = usblp_ids, 1416 .id_table = usblp_ids,
1416 .supports_autosuspend = 1, 1417 .supports_autosuspend = 1,
1417 }; 1418 };
1418 1419
1419 static int __init usblp_init(void) 1420 static int __init usblp_init(void)
1420 { 1421 {
1421 return usb_register(&usblp_driver); 1422 return usb_register(&usblp_driver);
1422 } 1423 }
1423 1424
1424 static void __exit usblp_exit(void) 1425 static void __exit usblp_exit(void)
1425 { 1426 {
1426 usb_deregister(&usblp_driver); 1427 usb_deregister(&usblp_driver);
1427 } 1428 }
1428 1429
1429 module_init(usblp_init); 1430 module_init(usblp_init);
1430 module_exit(usblp_exit); 1431 module_exit(usblp_exit);
1431 1432
1432 MODULE_AUTHOR( DRIVER_AUTHOR ); 1433 MODULE_AUTHOR( DRIVER_AUTHOR );
1433 MODULE_DESCRIPTION( DRIVER_DESC ); 1434 MODULE_DESCRIPTION( DRIVER_DESC );
1434 module_param(proto_bias, int, S_IRUGO | S_IWUSR); 1435 module_param(proto_bias, int, S_IRUGO | S_IWUSR);
1435 MODULE_PARM_DESC(proto_bias, "Favourite protocol number"); 1436 MODULE_PARM_DESC(proto_bias, "Favourite protocol number");
1436 MODULE_LICENSE("GPL"); 1437 MODULE_LICENSE("GPL");
1437 1438