static void gs_flush_chars(struct tty_struct *tty) { struct gs_port *port = tty->driver_data; unsigned long flags; pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty); spin_lock_irqsave(&port->port_lock, flags); if (port->port_usb) gs_start_tx(port); spin_unlock_irqrestore(&port->port_lock, flags); }
static void gs_flush_chars(struct tty_struct *tty) { struct gs_port *port = tty->driver_data; unsigned long flags; #ifdef CONFIG_USB_G_LGE_ANDROID if(!port) return; #endif pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty); spin_lock_irqsave(&port->port_lock, flags); if (port->port_usb) gs_start_tx(port); spin_unlock_irqrestore(&port->port_lock, flags); }
static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count) { struct gs_port *port = tty->driver_data; unsigned long flags; pr_vdebug("gs_write: ttyGS%d (%p) writing %d bytes\n", port->port_num, tty, count); spin_lock_irqsave(&port->port_lock, flags); if (count) count = kfifo_in(&port->port_write_buf, buf, count); /* treat count == 0 as flush_chars() */ if (port->port_usb) gs_start_tx(port); spin_unlock_irqrestore(&port->port_lock, flags); return count; }
static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count) { struct gs_port *port = tty->driver_data; unsigned long flags; int status; pr_vdebug("gs_write: ttyGS%d (%p) writing %d bytes\n", port->port_num, tty, count); spin_lock_irqsave(&port->port_lock, flags); if (count) count = gs_buf_put(&port->port_write_buf, buf, count); if (port->port_usb) status = gs_start_tx(port); spin_unlock_irqrestore(&port->port_lock, flags); return count; }
static void gs_write_complete(struct usb_ep *ep, struct usb_request *req) { struct gs_port *port = ep->driver_data; unsigned long flags; pr_vdebug("%s: %d bytes\n", __func__, req->actual); spin_lock_irqsave(&port->port_lock, flags); list_add(&req->list, &port->write_pool); switch (req->status) { default: /* presumably a transient fault */ pr_warning("%s: unexpected %s status %d\n", __func__, ep->name, req->status); /* FALL THROUGH */ case 0: /* normal completion */ if (port->port_usb) gs_start_tx(port); break; case -ESHUTDOWN: /* disconnect */ printk("%s: ESHUTDOWN\n", __func__); break; case -ENODEV: spin_lock(&port->port_lock); printk("%s: ENODEV\n", __func__); list_add_tail(&req->list, &port->read_pool); /* Implemented handling in future if needed */ spin_unlock(&port->port_lock); break; spin_lock(&port->port_lock); list_add_tail(&req->list, &port->read_pool); printk(KERN_ERR "gs_read_complete: unexpected status error, status=%d\n", req->status); spin_unlock(&port->port_lock); /* goto requeue; */ break; } }
static void gs_flush_chars(struct tty_struct *tty) { struct gs_port *port = tty->driver_data; unsigned long flags; //ALPS00423739 if(!port) { printk("ERROR!!! port is closed!! %s, line %d: port = %p\n", __func__, __LINE__, port); /*abort immediately after disconnect */ return; } //ALPS00423739 pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty); spin_lock_irqsave(&port->port_lock, flags); if (port->port_usb) gs_start_tx(port); spin_unlock_irqrestore(&port->port_lock, flags); }
static void gs_flush_chars(struct tty_struct *tty) { struct gs_port *port = NULL; unsigned long flags; if(!tty ){ pr_err("%s error: wrong parameter! *tty=0x%x", __func__, (unsigned int) tty); return; } port = tty->driver_data; if(!port){ pr_err("%s error: port = NULL! already closed??\n", __func__); return; } pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty); spin_lock_irqsave(&port->port_lock, flags); if (port->port_usb) gs_start_tx(port); spin_unlock_irqrestore(&port->port_lock, flags); }
static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count) { struct gs_port *port = tty->driver_data; unsigned long flags; int status; pr_vdebug("gs_write: ttyGS%d (%p) writing %d bytes\n", port->port_num, tty, count); spin_lock_irqsave(&port->port_lock, flags); if (unlikely(!port->open_count)) { spin_unlock_irqrestore(&port->port_lock, flags); return -EINVAL; } if (unlikely(!port->port_usb || port->is_suspend)) { port->stat_write_not_conn++; spin_unlock_irqrestore(&port->port_lock, flags); return -ESHUTDOWN; } /* * we don't have enough room for some data * may be the host don't start read, so drop the data. * otherwise the printed thread may pending forever. */ if (!port->line_state_on && list_empty(&port->write_pool)) { port->stat_write_no_mem++; spin_unlock_irqrestore(&port->port_lock, flags); return -ENOMEM; } if (count) count = gs_buf_put(&port->port_write_buf, buf, count); /* treat count == 0 as flush_chars() */ if (port->port_usb) status = gs_start_tx(port);/* [false alarm]:fortify disable */ spin_unlock_irqrestore(&port->port_lock, flags); return count; }
static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count) { struct gs_port *port = tty->driver_data; unsigned long flags; int status; #ifdef CONFIG_USB_G_LGE_ANDROID if (!port) return 0; #endif pr_vdebug("gs_write: ttyGS%d (%p) writing %d bytes\n", port->port_num, tty, count); spin_lock_irqsave(&port->port_lock, flags); if (count) count = gs_buf_put(&port->port_write_buf, buf, count); /* treat count == 0 as flush_chars() */ if (port->port_usb) status = gs_start_tx(port); spin_unlock_irqrestore(&port->port_lock, flags); return count; }
/** * gserial_connect - notify TTY I/O glue that USB link is active * @gser: the function, set up with endpoints and descriptors * @port_num: which port is active * Context: any (usually from irq) * * This is called activate endpoints and let the TTY layer know that * the connection is active ... not unlike "carrier detect". It won't * necessarily start I/O queues; unless the TTY is held open by any * task, there would be no point. However, the endpoints will be * activated so the USB host can perform I/O, subject to basic USB * hardware flow control. * * Caller needs to have set up the endpoints and USB function in @dev * before calling this, as well as the appropriate (speed-specific) * endpoint descriptors, and also have set up the TTY driver by calling * @gserial_setup(). * * Returns negative errno or zero. * On success, ep->driver_data will be overwritten. */ int gserial_connect(struct gserial *gser, u8 port_num) { struct gs_port *port; unsigned long flags; int status; if (!gs_tty_driver || port_num >= n_ports) return -ENXIO; /* we "know" gserial_cleanup() hasn't been called */ port = ports[port_num].port; /* activate the endpoints */ status = usb_ep_enable(gser->in, gser->in_desc); if (status < 0) return status; gser->in->driver_data = port; status = usb_ep_enable(gser->out, gser->out_desc); if (status < 0) goto fail_out; gser->out->driver_data = port; /* then tell the tty glue that I/O can work */ spin_lock_irqsave(&port->port_lock, flags); gser->ioport = port; port->port_usb = gser; /* REVISIT unclear how best to handle this state... * we don't really couple it with the Linux TTY. */ gser->port_line_coding = port->port_line_coding; /* REVISIT if waiting on "carrier detect", signal. */ /* if it's already open, start I/O ... and notify the serial * protocol about open/close status (connect/disconnect). */ if (port->open_count) { pr_debug("gserial_connect: start ttyGS%d\n", port->port_num); gs_start_io(port); if (gser->connect) gser->connect(gser); } else { if (gser->disconnect) gser->disconnect(gser); } #ifdef CONFIG_USB_ANDROID_SH_SERIALS if (port->port_usb) status = gs_start_tx(port); #endif /* CONFIG_USB_ANDROID_SH_SERIALS */ spin_unlock_irqrestore(&port->port_lock, flags); return status; fail_out: usb_ep_disable(gser->in); gser->in->driver_data = NULL; return status; }
/* * gs_start_tx * * This function finds available write requests, calls * gs_send_packet to fill these packets with data, and * continues until either there are no more write requests * available or no more data to send. This function is * run whenever data arrives or write requests are available. * * Context: caller owns port_lock; port_usb is non-null. */ static int gs_start_tx(struct gs_port *port) /* __releases(&port->port_lock) __acquires(&port->port_lock) */ { struct list_head *pool = &port->write_pool; struct usb_ep *in = port->port_usb->in; int status = 0; static long prev_len; bool do_tty_wake = false; while (!list_empty(pool)) { struct usb_request *req; int len; req = list_entry(pool->next, struct usb_request, list); len = gs_send_packet(port, req->buf, TX_BUF_SIZE); if (len == 0) { /* Queue zero length packet */ #if 1 //QCT_SBA if (prev_len && (prev_len % in->maxpacket == 0)) { #else if (prev_len & (prev_len % in->maxpacket == 0)) { #endif req->length = 0; list_del(&req->list); spin_unlock(&port->port_lock); status = usb_ep_queue(in, req, GFP_ATOMIC); spin_lock(&port->port_lock); if (!port->port_usb) { gs_free_req(in, req); break; } if (status) { printk(KERN_ERR "%s: %s err %d\n", __func__, "queue", status); list_add(&req->list, pool); } prev_len = 0; } wake_up_interruptible(&port->drain_wait); break; } do_tty_wake = true; req->length = len; list_del(&req->list); pr_vdebug(PREFIX "%d: tx len=%d, 0x%02x 0x%02x 0x%02x ...\n", port->port_num, len, *((u8 *)req->buf), *((u8 *)req->buf+1), *((u8 *)req->buf+2)); /* Drop lock while we call out of driver; completions * could be issued while we do so. Disconnection may * happen too; maybe immediately before we queue this! * * NOTE that we may keep sending data for a while after * the TTY closed (dev->ioport->port_tty is NULL). */ spin_unlock(&port->port_lock); status = usb_ep_queue(in, req, GFP_ATOMIC); spin_lock(&port->port_lock); /* * If port_usb is NULL, gserial disconnect is called * while the spinlock is dropped and all requests are * freed. Free the current request here. */ if (!port->port_usb) { do_tty_wake = false; gs_free_req(in, req); break; } if (status) { pr_debug("%s: %s %s err %d\n", __func__, "queue", in->name, status); list_add(&req->list, pool); break; } prev_len = req->length; } if (do_tty_wake && port->port_tty) tty_wakeup(port->port_tty); return status; } /* * Context: caller owns port_lock, and port_usb is set */ static unsigned gs_start_rx(struct gs_port *port) /* __releases(&port->port_lock) __acquires(&port->port_lock) */ { struct list_head *pool = &port->read_pool; struct usb_ep *out = port->port_usb->out; unsigned started = 0; while (!list_empty(pool)) { struct usb_request *req; int status; struct tty_struct *tty; /* no more rx if closed */ tty = port->port_tty; if (!tty) break; req = list_entry(pool->next, struct usb_request, list); list_del(&req->list); req->length = RX_BUF_SIZE; /* drop lock while we call out; the controller driver * may need to call us back (e.g. for disconnect) */ spin_unlock(&port->port_lock); status = usb_ep_queue(out, req, GFP_ATOMIC); spin_lock(&port->port_lock); /* * If port_usb is NULL, gserial disconnect is called * while the spinlock is dropped and all requests are * freed. Free the current request here. */ if (!port->port_usb) { started = 0; gs_free_req(out, req); break; } if (status) { pr_debug("%s: %s %s err %d\n", __func__, "queue", out->name, status); list_add(&req->list, pool); break; } started++; } return started; } /* * RX work queue takes data out of the RX queue and hands it up to the TTY * layer until it refuses to take any more data (or is throttled back). * Then it issues reads for any further data. * * If the RX queue becomes full enough that no usb_request is queued, * the OUT endpoint may begin NAKing as soon as its FIFO fills up. * So QUEUE_SIZE packets plus however many the FIFO holds (usually two) * can be buffered before the TTY layer's buffers (currently 64 KB). */ static void gs_rx_push(struct work_struct *w) { struct gs_port *port = container_of(w, struct gs_port, push); struct tty_struct *tty; struct list_head *queue = &port->read_queue; bool disconnect = false; bool do_push = false; /* hand any queued data to the tty */ spin_lock_irq(&port->port_lock); tty = port->port_tty; while (!list_empty(queue)) { struct usb_request *req; req = list_first_entry(queue, struct usb_request, list); /* discard data if tty was closed */ if (!tty) goto recycle; /* leave data queued if tty was rx throttled */ if (test_bit(TTY_THROTTLED, &tty->flags)) break; switch (req->status) { case -ESHUTDOWN: disconnect = true; pr_vdebug(PREFIX "%d: shutdown\n", port->port_num); break; default: /* presumably a transient fault */ pr_warning(PREFIX "%d: unexpected RX status %d\n", port->port_num, req->status); /* FALLTHROUGH */ case 0: /* normal completion */ break; } /* push data to (open) tty */ if (req->actual) { char *packet = req->buf; unsigned size = req->actual; unsigned n; int count; /* we may have pushed part of this packet already... */ n = port->n_read; if (n) { packet += n; size -= n; } count = tty_insert_flip_string(tty, packet, size); if (count) do_push = true; if (count != size) { /* stop pushing; TTY layer can't handle more */ port->n_read += count; pr_vdebug(PREFIX "%d: rx block %d/%d\n", port->port_num, count, req->actual); break; } port->n_read = 0; } recycle: list_move(&req->list, &port->read_pool); } /* Push from tty to ldisc; this is immediate with low_latency, and * may trigger callbacks to this driver ... so drop the spinlock. */ if (tty && do_push) { spin_unlock_irq(&port->port_lock); tty_flip_buffer_push(tty); wake_up_interruptible(&tty->read_wait); spin_lock_irq(&port->port_lock); /* tty may have been closed */ tty = port->port_tty; } /* We want our data queue to become empty ASAP, keeping data * in the tty and ldisc (not here). If we couldn't push any * this time around, there may be trouble unless there's an * implicit tty_unthrottle() call on its way... * * REVISIT we should probably add a timer to keep the work queue * from starving ... but it's not clear that case ever happens. */ if (!list_empty(queue) && tty) { if (!test_bit(TTY_THROTTLED, &tty->flags)) { if (do_push) queue_work(gserial_wq, &port->push); else pr_warning(PREFIX "%d: RX not scheduled?\n", port->port_num); } } /* If we're still connected, refill the USB RX queue. */ if (!disconnect && port->port_usb) gs_start_rx(port); spin_unlock_irq(&port->port_lock); } static void gs_read_complete(struct usb_ep *ep, struct usb_request *req) { struct gs_port *port = ep->driver_data; unsigned long flags; /* Queue all received data until the tty layer is ready for it. */ spin_lock_irqsave(&port->port_lock, flags); list_add_tail(&req->list, &port->read_queue); queue_work(gserial_wq, &port->push); spin_unlock_irqrestore(&port->port_lock, flags); } static void gs_write_complete(struct usb_ep *ep, struct usb_request *req) { struct gs_port *port = ep->driver_data; unsigned long flags; spin_lock_irqsave(&port->port_lock, flags); list_add(&req->list, &port->write_pool); switch (req->status) { default: /* presumably a transient fault */ pr_warning("%s: unexpected %s status %d\n", __func__, ep->name, req->status); /* FALL THROUGH */ case 0: /* normal completion */ if (port->port_usb) gs_start_tx(port); break; case -ESHUTDOWN: /* disconnect */ pr_vdebug("%s: %s shutdown\n", __func__, ep->name); break; } spin_unlock_irqrestore(&port->port_lock, flags); } static void gs_free_requests(struct usb_ep *ep, struct list_head *head) { struct usb_request *req; while (!list_empty(head)) { req = list_entry(head->next, struct usb_request, list); list_del(&req->list); gs_free_req(ep, req); } } static int gs_alloc_requests(struct usb_ep *ep, struct list_head *head, int num, int size, void (*fn)(struct usb_ep *, struct usb_request *)) { int i; struct usb_request *req; /* Pre-allocate up to QUEUE_SIZE transfers, but if we can't * do quite that many this time, don't fail ... we just won't * be as speedy as we might otherwise be. */ for (i = 0; i < num; i++) { req = gs_alloc_req(ep, size, GFP_ATOMIC); if (!req) return list_empty(head) ? -ENOMEM : 0; req->complete = fn; list_add_tail(&req->list, head); } return 0; }
/* This stolen/adapted from serial/sn_console.c */ static void gs_console_write(struct console *cons, const char *buf, unsigned count) { unsigned long flags = 0; unsigned status; struct gs_port *port; port = cons->data; /* Just chuck everything if USB side is not up. */ if (!port->port_usb) return; /* somebody really wants this output, might be an * oops, kdb, panic, etc. make sure they get it. */ if (spin_is_locked(&port->port_lock)) { char *get = port->port_write_buf.buf_get; char *put = port->port_write_buf.buf_put; int got_lock = 0; int counter; /* * We attempt to determine if someone has died with the * lock. We wait ~20 secs after the head and tail ptrs * stop moving and assume the lock holder is not functional * and plow ahead. If the lock is freed within the time out * period we re-get the lock and go ahead normally. We also * remember if we have plowed ahead so that we don't have * to wait out the time out period again - the asumption * is that we will time out again. */ for (counter = 0; counter < 150; mdelay(125), counter++) { if (!spin_is_locked(&port->port_lock) || port->stole_lock) { if (!port->stole_lock) break; spin_lock_irqsave(&port->port_lock, flags); got_lock = 1; break; } /* still locked */ if ((get != port->port_write_buf.buf_get) || (put != port->port_write_buf.buf_put)) { put = port->port_write_buf.buf_put; get = port->port_write_buf.buf_get; counter = 0; } } /* Make space by flushing any waiting output */ if (port->port_usb) gs_start_tx(port); while (count) { count -= gs_buf_putr(&port->port_write_buf, buf, count); if (port->port_usb) break; /* Flush our stuff immediately */ if (!gs_start_tx(port)) mdelay(125); } if (got_lock) { spin_unlock_irqrestore(&port->port_lock, flags); port->stole_lock = 0; } else { /* fell thru */ port->stole_lock = 1; } return; } port->stole_lock = 0; spin_lock_irqsave(&port->port_lock, flags); /* Flush out any waiting output so we have as much space as possible */ if (port->port_usb) status = gs_start_tx(port); while (count) { count -= gs_buf_putr(&port->port_write_buf, buf, count); if (port->port_usb) break; /* Flush our stuff immediately */ if (!gs_start_tx(port)) mdelay(125); } spin_unlock_irqrestore(&port->port_lock, flags); }