| .. | .. |
|---|
| 41 | 41 | bool rts; |
|---|
| 42 | 42 | bool cts; |
|---|
| 43 | 43 | int outstanding_urbs; |
|---|
| 44 | + int outstanding_bytes; |
|---|
| 45 | + |
|---|
| 46 | + struct usb_anchor anchor; |
|---|
| 44 | 47 | }; |
|---|
| 45 | 48 | |
|---|
| 46 | 49 | |
|---|
| .. | .. |
|---|
| 149 | 152 | return res; |
|---|
| 150 | 153 | } |
|---|
| 151 | 154 | |
|---|
| 155 | +static void opticon_close(struct usb_serial_port *port) |
|---|
| 156 | +{ |
|---|
| 157 | + struct opticon_private *priv = usb_get_serial_port_data(port); |
|---|
| 158 | + |
|---|
| 159 | + usb_kill_anchored_urbs(&priv->anchor); |
|---|
| 160 | + |
|---|
| 161 | + usb_serial_generic_close(port); |
|---|
| 162 | +} |
|---|
| 163 | + |
|---|
| 152 | 164 | static void opticon_write_control_callback(struct urb *urb) |
|---|
| 153 | 165 | { |
|---|
| 154 | 166 | struct usb_serial_port *port = urb->context; |
|---|
| .. | .. |
|---|
| 169 | 181 | |
|---|
| 170 | 182 | spin_lock_irqsave(&priv->lock, flags); |
|---|
| 171 | 183 | --priv->outstanding_urbs; |
|---|
| 184 | + priv->outstanding_bytes -= urb->transfer_buffer_length; |
|---|
| 172 | 185 | spin_unlock_irqrestore(&priv->lock, flags); |
|---|
| 173 | 186 | |
|---|
| 174 | 187 | usb_serial_port_softint(port); |
|---|
| .. | .. |
|---|
| 182 | 195 | struct urb *urb; |
|---|
| 183 | 196 | unsigned char *buffer; |
|---|
| 184 | 197 | unsigned long flags; |
|---|
| 185 | | - int status; |
|---|
| 186 | 198 | struct usb_ctrlrequest *dr; |
|---|
| 199 | + int ret = -ENOMEM; |
|---|
| 187 | 200 | |
|---|
| 188 | 201 | spin_lock_irqsave(&priv->lock, flags); |
|---|
| 189 | 202 | if (priv->outstanding_urbs > URB_UPPER_LIMIT) { |
|---|
| .. | .. |
|---|
| 192 | 205 | return 0; |
|---|
| 193 | 206 | } |
|---|
| 194 | 207 | priv->outstanding_urbs++; |
|---|
| 208 | + priv->outstanding_bytes += count; |
|---|
| 195 | 209 | spin_unlock_irqrestore(&priv->lock, flags); |
|---|
| 196 | 210 | |
|---|
| 197 | 211 | buffer = kmalloc(count, GFP_ATOMIC); |
|---|
| 198 | | - if (!buffer) { |
|---|
| 199 | | - count = -ENOMEM; |
|---|
| 212 | + if (!buffer) |
|---|
| 200 | 213 | goto error_no_buffer; |
|---|
| 201 | | - } |
|---|
| 202 | 214 | |
|---|
| 203 | 215 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
|---|
| 204 | | - if (!urb) { |
|---|
| 205 | | - count = -ENOMEM; |
|---|
| 216 | + if (!urb) |
|---|
| 206 | 217 | goto error_no_urb; |
|---|
| 207 | | - } |
|---|
| 208 | 218 | |
|---|
| 209 | 219 | memcpy(buffer, buf, count); |
|---|
| 210 | 220 | |
|---|
| .. | .. |
|---|
| 213 | 223 | /* The connected devices do not have a bulk write endpoint, |
|---|
| 214 | 224 | * to transmit data to de barcode device the control endpoint is used */ |
|---|
| 215 | 225 | dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC); |
|---|
| 216 | | - if (!dr) { |
|---|
| 217 | | - count = -ENOMEM; |
|---|
| 226 | + if (!dr) |
|---|
| 218 | 227 | goto error_no_dr; |
|---|
| 219 | | - } |
|---|
| 220 | 228 | |
|---|
| 221 | 229 | dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT; |
|---|
| 222 | 230 | dr->bRequest = 0x01; |
|---|
| .. | .. |
|---|
| 229 | 237 | (unsigned char *)dr, buffer, count, |
|---|
| 230 | 238 | opticon_write_control_callback, port); |
|---|
| 231 | 239 | |
|---|
| 240 | + usb_anchor_urb(urb, &priv->anchor); |
|---|
| 241 | + |
|---|
| 232 | 242 | /* send it down the pipe */ |
|---|
| 233 | | - status = usb_submit_urb(urb, GFP_ATOMIC); |
|---|
| 234 | | - if (status) { |
|---|
| 235 | | - dev_err(&port->dev, |
|---|
| 236 | | - "%s - usb_submit_urb(write endpoint) failed status = %d\n", |
|---|
| 237 | | - __func__, status); |
|---|
| 238 | | - count = status; |
|---|
| 243 | + ret = usb_submit_urb(urb, GFP_ATOMIC); |
|---|
| 244 | + if (ret) { |
|---|
| 245 | + dev_err(&port->dev, "failed to submit write urb: %d\n", ret); |
|---|
| 246 | + usb_unanchor_urb(urb); |
|---|
| 239 | 247 | goto error; |
|---|
| 240 | 248 | } |
|---|
| 241 | 249 | |
|---|
| .. | .. |
|---|
| 253 | 261 | error_no_buffer: |
|---|
| 254 | 262 | spin_lock_irqsave(&priv->lock, flags); |
|---|
| 255 | 263 | --priv->outstanding_urbs; |
|---|
| 264 | + priv->outstanding_bytes -= count; |
|---|
| 256 | 265 | spin_unlock_irqrestore(&priv->lock, flags); |
|---|
| 257 | | - return count; |
|---|
| 266 | + |
|---|
| 267 | + return ret; |
|---|
| 258 | 268 | } |
|---|
| 259 | 269 | |
|---|
| 260 | 270 | static int opticon_write_room(struct tty_struct *tty) |
|---|
| .. | .. |
|---|
| 277 | 287 | spin_unlock_irqrestore(&priv->lock, flags); |
|---|
| 278 | 288 | |
|---|
| 279 | 289 | return 2048; |
|---|
| 290 | +} |
|---|
| 291 | + |
|---|
| 292 | +static int opticon_chars_in_buffer(struct tty_struct *tty) |
|---|
| 293 | +{ |
|---|
| 294 | + struct usb_serial_port *port = tty->driver_data; |
|---|
| 295 | + struct opticon_private *priv = usb_get_serial_port_data(port); |
|---|
| 296 | + unsigned long flags; |
|---|
| 297 | + int count; |
|---|
| 298 | + |
|---|
| 299 | + spin_lock_irqsave(&priv->lock, flags); |
|---|
| 300 | + count = priv->outstanding_bytes; |
|---|
| 301 | + spin_unlock_irqrestore(&priv->lock, flags); |
|---|
| 302 | + |
|---|
| 303 | + return count; |
|---|
| 280 | 304 | } |
|---|
| 281 | 305 | |
|---|
| 282 | 306 | static int opticon_tiocmget(struct tty_struct *tty) |
|---|
| .. | .. |
|---|
| 328 | 352 | return 0; |
|---|
| 329 | 353 | } |
|---|
| 330 | 354 | |
|---|
| 331 | | -static int get_serial_info(struct usb_serial_port *port, |
|---|
| 332 | | - struct serial_struct __user *serial) |
|---|
| 333 | | -{ |
|---|
| 334 | | - struct serial_struct tmp; |
|---|
| 335 | | - |
|---|
| 336 | | - memset(&tmp, 0x00, sizeof(tmp)); |
|---|
| 337 | | - |
|---|
| 338 | | - /* fake emulate a 16550 uart to make userspace code happy */ |
|---|
| 339 | | - tmp.type = PORT_16550A; |
|---|
| 340 | | - tmp.line = port->minor; |
|---|
| 341 | | - tmp.port = 0; |
|---|
| 342 | | - tmp.irq = 0; |
|---|
| 343 | | - tmp.xmit_fifo_size = 1024; |
|---|
| 344 | | - tmp.baud_base = 9600; |
|---|
| 345 | | - tmp.close_delay = 5*HZ; |
|---|
| 346 | | - tmp.closing_wait = 30*HZ; |
|---|
| 347 | | - |
|---|
| 348 | | - if (copy_to_user(serial, &tmp, sizeof(*serial))) |
|---|
| 349 | | - return -EFAULT; |
|---|
| 350 | | - return 0; |
|---|
| 351 | | -} |
|---|
| 352 | | - |
|---|
| 353 | | -static int opticon_ioctl(struct tty_struct *tty, |
|---|
| 354 | | - unsigned int cmd, unsigned long arg) |
|---|
| 355 | +static int get_serial_info(struct tty_struct *tty, |
|---|
| 356 | + struct serial_struct *ss) |
|---|
| 355 | 357 | { |
|---|
| 356 | 358 | struct usb_serial_port *port = tty->driver_data; |
|---|
| 357 | 359 | |
|---|
| 358 | | - switch (cmd) { |
|---|
| 359 | | - case TIOCGSERIAL: |
|---|
| 360 | | - return get_serial_info(port, |
|---|
| 361 | | - (struct serial_struct __user *)arg); |
|---|
| 362 | | - } |
|---|
| 363 | | - |
|---|
| 364 | | - return -ENOIOCTLCMD; |
|---|
| 360 | + /* fake emulate a 16550 uart to make userspace code happy */ |
|---|
| 361 | + ss->type = PORT_16550A; |
|---|
| 362 | + ss->line = port->minor; |
|---|
| 363 | + ss->port = 0; |
|---|
| 364 | + ss->irq = 0; |
|---|
| 365 | + ss->xmit_fifo_size = 1024; |
|---|
| 366 | + ss->baud_base = 9600; |
|---|
| 367 | + ss->close_delay = 5*HZ; |
|---|
| 368 | + ss->closing_wait = 30*HZ; |
|---|
| 369 | + return 0; |
|---|
| 365 | 370 | } |
|---|
| 366 | 371 | |
|---|
| 367 | 372 | static int opticon_port_probe(struct usb_serial_port *port) |
|---|
| .. | .. |
|---|
| 373 | 378 | return -ENOMEM; |
|---|
| 374 | 379 | |
|---|
| 375 | 380 | spin_lock_init(&priv->lock); |
|---|
| 381 | + init_usb_anchor(&priv->anchor); |
|---|
| 376 | 382 | |
|---|
| 377 | 383 | usb_set_serial_port_data(port, priv); |
|---|
| 378 | 384 | |
|---|
| .. | .. |
|---|
| 400 | 406 | .port_probe = opticon_port_probe, |
|---|
| 401 | 407 | .port_remove = opticon_port_remove, |
|---|
| 402 | 408 | .open = opticon_open, |
|---|
| 409 | + .close = opticon_close, |
|---|
| 403 | 410 | .write = opticon_write, |
|---|
| 404 | 411 | .write_room = opticon_write_room, |
|---|
| 412 | + .chars_in_buffer = opticon_chars_in_buffer, |
|---|
| 405 | 413 | .throttle = usb_serial_generic_throttle, |
|---|
| 406 | 414 | .unthrottle = usb_serial_generic_unthrottle, |
|---|
| 407 | | - .ioctl = opticon_ioctl, |
|---|
| 415 | + .get_serial = get_serial_info, |
|---|
| 408 | 416 | .tiocmget = opticon_tiocmget, |
|---|
| 409 | 417 | .tiocmset = opticon_tiocmset, |
|---|
| 410 | 418 | .process_read_urb = opticon_process_read_urb, |
|---|