>From 3b470f9cfd67ee99a6dc005915b4c86f809c8deb Mon Sep 17 00:00:00 2001 From: Gwenn Bourree <gwenn.bourree@xxxxxxxxx> Date: Mon, 29 Jun 2015 16:09:06 +0200 Subject: [PATCH] Add Debug define for extra levels Improve the debug print out by adding extra debug levels that allow to be verbose and ease the debugging. The new levels macros are clearer to understand than the previous numbers. Checkpatch script resulting errors and warning have been solved. Signed-off-by: Gwenn Bourree <gwenn.bourree@xxxxxxxxx> Signed-off-by: Mustapha Ben Zoubeir <mustaphax.ben.zoubeir@xxxxxxxxx> Signed-off-by: Nicolas LOUIS <nicolasx.louis@xxxxxxxxx> Reviewed-by: Arun Ravindran <arun.ravindran@xxxxxxxxx> --- drivers/tty/n_gsm.c | 103 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 71 insertions(+), 32 deletions(-) diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c index 382d3fc..c295065 100644 --- a/drivers/tty/n_gsm.c +++ b/drivers/tty/n_gsm.c @@ -66,17 +66,18 @@ static int debug; module_param(debug, int, 0600); -/* Defaults: these are from the specification */ +#define GSMDBG_VERBOSE_PACKET_REPORT(x) ((x) & BIT(0)) +#define GSMDBG_FORCE_CARRIER(x) ((x) & BIT(1)) +#define GSMDBG_DATA_FULL_REPORT(x) ((x) & BIT(2)) +#define GSMDBG_DLCI_STREAM_REPORT(x) ((x) & BIT(3)) +#define GSMDBG_DLCI_DATA_REPORT(x) ((x) & BIT(4)) +#define GSMDBG_DATA_LEN_REPORT(x) ((x) & BIT(5)) -#define T1 10 /* 100mS */ -#define T2 34 /* 333mS */ -#define N2 3 /* Retry 3 times */ +/* Defaults: these are from the specification */ -/* Use long timers for testing at low speed with debug on */ -#ifdef DEBUG_TIMING -#define T1 100 -#define T2 200 -#endif +#define T1 10 /* 100mS */ +#define T2 34 /* 333mS */ +#define N2 3 /* Retry 3 times */ /* * Semi-arbitrary buffer size limits. 0710 is normally run with 32-64 byte @@ -461,7 +462,7 @@ static u8 gsm_encode_modem(const struct gsm_dlci *dlci) static void gsm_print_packet(const char *hdr, int addr, int cr, u8 control, const u8 *data, int dlen) { - if (!(debug & 1)) + if (!GSMDBG_VERBOSE_PACKET_REPORT(debug)) return; pr_info("%s %d) %c: ", hdr, addr, "RC"[cr]); @@ -489,7 +490,8 @@ static void gsm_print_packet(const char *hdr, int addr, int cr, if (!(control & 0x01)) { pr_cont("I N(S)%d N(R)%d", (control & 0x0E) >> 1, (control & 0xE0) >> 5); - } else switch (control & 0x0F) { + } else + switch (control & 0x0F) { case RR: pr_cont("RR(%d)", (control & 0xE0) >> 5); break; @@ -511,6 +513,7 @@ static void gsm_print_packet(const char *hdr, int addr, int cr, if (dlen) { int ct = 0; + while (dlen--) { if (ct % 8 == 0) { pr_cont("\n"); @@ -542,6 +545,7 @@ static void gsm_print_packet(const char *hdr, int addr, int cr, static int gsm_stuff_frame(const u8 *input, u8 *output, int len) { int olen = 0; + while (len--) { if (*input == GSM1_SOF || *input == GSM1_ESCAPE || *input == XON || *input == XOFF) { @@ -695,12 +699,12 @@ static void gsm_data_kick(struct gsm_mux *gsm) len += 2; } else { gsm->txframe[0] = GSM0_SOF; - memcpy(gsm->txframe + 1 , msg->data, msg->len); + memcpy(gsm->txframe + 1, msg->data, msg->len); gsm->txframe[msg->len + 1] = GSM0_SOF; len = msg->len + 2; } - if (debug & 4) + if (GSMDBG_DATA_FULL_REPORT(debug)) print_hex_dump_bytes("gsm_data_kick: ", DUMP_PREFIX_OFFSET, gsm->txframe, len); @@ -750,7 +754,7 @@ static void __gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg) *--dp = (msg->addr << 2) | 2 | EA; else *--dp = (msg->addr << 2) | EA; - *fcs = gsm_fcs_add_block(INIT_FCS, dp , msg->data - dp); + *fcs = gsm_fcs_add_block(INIT_FCS, dp, msg->data - dp); /* Ugly protocol layering violation */ if (msg->ctrl == UI || msg->ctrl == (UI|PF)) *fcs = gsm_fcs_add_block(*fcs, msg->data, msg->len); @@ -783,6 +787,7 @@ static void __gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg) static void gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg) { unsigned long flags; + spin_lock_irqsave(&dlci->gsm->tx_lock, flags); __gsm_data_queue(dlci, msg); spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags); @@ -833,7 +838,8 @@ static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci) *dp++ = gsm_encode_modem(dlci); break; } - WARN_ON(kfifo_out_locked(dlci->fifo, dp , len, &dlci->lock) != len); + WARN_ON(kfifo_out_locked(dlci->fifo, dp, + len, &dlci->lock) != len); __gsm_data_queue(dlci, msg); total_size += size; } @@ -1005,6 +1011,7 @@ static void gsm_control_reply(struct gsm_mux *gsm, int cmd, u8 *data, int dlen) { struct gsm_msg *msg; + msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->ftype); if (msg == NULL) return; @@ -1308,6 +1315,7 @@ static void gsm_control_response(struct gsm_mux *gsm, unsigned int command, static void gsm_control_transmit(struct gsm_mux *gsm, struct gsm_control *ctrl) { struct gsm_msg *msg = gsm_data_alloc(gsm, 0, ctrl->len + 1, gsm->ftype); + if (msg == NULL) return; msg->data[0] = (ctrl->cmd << 1) | 2 | EA; /* command */ @@ -1331,6 +1339,7 @@ static void gsm_control_retransmit(unsigned long data) struct gsm_mux *gsm = (struct gsm_mux *)data; struct gsm_control *ctrl; unsigned long flags; + spin_lock_irqsave(&gsm->control_lock, flags); ctrl = gsm->pending_cmd; if (ctrl) { @@ -1367,6 +1376,7 @@ static struct gsm_control *gsm_control_send(struct gsm_mux *gsm, struct gsm_control *ctrl = kzalloc(sizeof(struct gsm_control), GFP_KERNEL); unsigned long flags; + if (ctrl == NULL) return NULL; retry: @@ -1400,6 +1410,7 @@ retry: static int gsm_control_wait(struct gsm_mux *gsm, struct gsm_control *control) { int err; + wait_event(gsm->event, control->done == 1); err = control->error; kfree(control); @@ -1426,7 +1437,7 @@ static int gsm_control_wait(struct gsm_mux *gsm, struct gsm_control *control) static void gsm_dlci_close(struct gsm_dlci *dlci) { del_timer(&dlci->t1); - if (debug & 8) + if (GSMDBG_DLCI_STREAM_REPORT(debug)) pr_debug("DLCI %d goes closed.\n", dlci->addr); dlci->state = DLCI_CLOSED; if (dlci->addr != 0) { @@ -1453,7 +1464,7 @@ static void gsm_dlci_open(struct gsm_dlci *dlci) del_timer(&dlci->t1); /* This will let a tty open continue */ dlci->state = DLCI_OPEN; - if (debug & 8) + if (GSMDBG_DLCI_STREAM_REPORT(debug)) pr_debug("DLCI %d goes open.\n", dlci->addr); wake_up(&dlci->gsm->event); } @@ -1507,6 +1518,7 @@ static void gsm_dlci_t1(unsigned long data) static void gsm_dlci_begin_open(struct gsm_dlci *dlci) { struct gsm_mux *gsm = dlci->gsm; + if (dlci->state == DLCI_OPEN || dlci->state == DLCI_OPENING) return; dlci->retries = gsm->n2; @@ -1529,6 +1541,7 @@ static void gsm_dlci_begin_open(struct gsm_dlci *dlci) static void gsm_dlci_begin_close(struct gsm_dlci *dlci) { struct gsm_mux *gsm = dlci->gsm; + if (dlci->state == DLCI_CLOSED || dlci->state == DLCI_CLOSING) return; dlci->retries = gsm->n2; @@ -1556,8 +1569,8 @@ static void gsm_dlci_data(struct gsm_dlci *dlci, u8 *data, int clen) unsigned int modem = 0; int len = clen; - if (debug & 16) - pr_debug("%d bytes for tty\n", len); + if (GSMDBG_DLCI_DATA_REPORT(debug)) + pr_debug("%s: %d bytes for tty\n", __func__, len); switch (dlci->adaption) { /* Unsupported types */ /* Packetised interruptible data */ @@ -1602,9 +1615,11 @@ static void gsm_dlci_command(struct gsm_dlci *dlci, u8 *data, int len) { /* See what command is involved */ unsigned int command = 0; + while (len-- > 0) { if (gsm_read_ea(&command, *data++) == 1) { int clen = *data++; + len--; /* FIXME: this is properly an EA */ clen >>= 1; @@ -1639,6 +1654,7 @@ static void gsm_dlci_command(struct gsm_dlci *dlci, u8 *data, int len) static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr) { struct gsm_dlci *dlci = kzalloc(sizeof(struct gsm_dlci), GFP_ATOMIC); + if (dlci == NULL) return NULL; spin_lock_init(&dlci->lock); @@ -1711,6 +1727,7 @@ static void gsm_destroy_network(struct gsm_dlci *dlci); static void gsm_dlci_release(struct gsm_dlci *dlci) { struct tty_struct *tty = tty_port_tty_get(&dlci->port); + if (tty) { mutex_lock(&dlci->mutex); gsm_destroy_network(dlci); @@ -1758,7 +1775,7 @@ static void gsm_queue(struct gsm_mux *gsm) } if (gsm->fcs != GOOD_FCS) { gsm->bad_fcs++; - if (debug & 4) + if (GSMDBG_DATA_FULL_REPORT(debug)) pr_debug("BAD FCS %02x\n", gsm->fcs); return; } @@ -1840,10 +1857,8 @@ static void gsm_queue(struct gsm_mux *gsm) return; invalid: gsm->malformed++; - return; } - /** * gsm0_receive - perform processing for non-transparency * @gsm: gsm data for this ldisc instance @@ -1922,6 +1937,7 @@ static void gsm0_receive(struct gsm_mux *gsm, unsigned char c) gsm->state = GSM_SEARCH; break; } + pr_debug("wait for GSM0_SOF, while got 0x%x\n", (u32)c); break; } } @@ -2140,6 +2156,7 @@ static void gsm_free_mux(struct gsm_mux *gsm) static void gsm_free_muxr(struct kref *ref) { struct gsm_mux *gsm = container_of(ref, struct gsm_mux, ref); + gsm_free_mux(gsm); } @@ -2162,6 +2179,7 @@ static inline void mux_put(struct gsm_mux *gsm) static struct gsm_mux *gsm_alloc_mux(void) { struct gsm_mux *gsm = kzalloc(sizeof(struct gsm_mux), GFP_KERNEL); + if (gsm == NULL) return NULL; gsm->buf = kmalloc(MAX_MRU + 1, GFP_KERNEL); @@ -2209,9 +2227,12 @@ static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len) set_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags); return -ENOSPC; } - if (debug & 4) - print_hex_dump_bytes("gsmld_output: ", DUMP_PREFIX_OFFSET, + if (GSMDBG_DATA_FULL_REPORT(debug)) + print_hex_dump_bytes(__func__, DUMP_PREFIX_OFFSET, data, len); + else if (GSMDBG_DATA_LEN_REPORT(debug)) + pr_debug("n_gsm: >> %d bytes\n", len); + gsm->tty->ops->write(gsm->tty, data, len); return len; } @@ -2276,9 +2297,11 @@ static void gsmld_receive_buf(struct tty_struct *tty, const unsigned char *cp, int i; char flags = TTY_NORMAL; - if (debug & 4) - print_hex_dump_bytes("gsmld_receive: ", DUMP_PREFIX_OFFSET, + if (GSMDBG_DATA_FULL_REPORT(debug)) + print_hex_dump_bytes(__func__, DUMP_PREFIX_OFFSET, cp, count); + else if (GSMDBG_DATA_LEN_REPORT(debug)) + pr_debug("n_gsm: << %d bytes\n", count); for (i = count, dp = cp, f = fp; i; i--, dp++) { if (f) @@ -2407,9 +2430,9 @@ static void gsmld_write_wakeup(struct tty_struct *tty) clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); spin_lock_irqsave(&gsm->tx_lock, flags); gsm_data_kick(gsm); - if (gsm->tx_bytes < TX_THRESH_LO) { + if (gsm->tx_bytes < TX_THRESH_LO) gsm_dlci_data_sweep(gsm); - } + spin_unlock_irqrestore(&gsm->tx_lock, flags); } @@ -2452,6 +2475,7 @@ static ssize_t gsmld_write(struct tty_struct *tty, struct file *file, const unsigned char *buf, size_t nr) { int space = tty_write_room(tty); + if (space >= nr) return tty->ops->write(tty, buf, nr); set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); @@ -2670,6 +2694,7 @@ static int gsm_mux_net_start_xmit(struct sk_buff *skb, { struct gsm_mux_net *mux_net = netdev_priv(net); struct gsm_dlci *dlci = mux_net->dlci; + muxnet_get(mux_net); skb_queue_head(&dlci->skb_list, skb); @@ -2698,6 +2723,7 @@ static void gsm_mux_rx_netchar(struct gsm_dlci *dlci, struct net_device *net = dlci->net; struct sk_buff *skb; struct gsm_mux_net *mux_net = netdev_priv(net); + muxnet_get(mux_net); /* Allocate an sk_buff */ @@ -2712,7 +2738,7 @@ static void gsm_mux_rx_netchar(struct gsm_dlci *dlci, memcpy(skb_put(skb, size), in_buf, size); skb->dev = net; - skb->protocol = __constant_htons(ETH_P_IP); + skb->protocol = htons(ETH_P_IP); /* Ship it off to the kernel */ netif_rx(skb); @@ -2721,12 +2747,12 @@ static void gsm_mux_rx_netchar(struct gsm_dlci *dlci, STATS(net).rx_packets++; STATS(net).rx_bytes += size; muxnet_put(mux_net); - return; } static int gsm_change_mtu(struct net_device *net, int new_mtu) { struct gsm_mux_net *mux_net = netdev_priv(net); + if ((new_mtu < 8) || (new_mtu > mux_net->dlci->gsm->mtu)) return -EINVAL; net->mtu = new_mtu; @@ -2871,7 +2897,7 @@ static int gsm_carrier_raised(struct tty_port *port) /* Not yet open so no carrier info */ if (dlci->state != DLCI_OPEN) return 0; - if (debug & 2) + if (GSMDBG_FORCE_CARRIER(debug)) return 1; return dlci->modem_rx & TIOCM_CD; } @@ -2880,6 +2906,7 @@ static void gsm_dtr_rts(struct tty_port *port, int onoff) { struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port); unsigned int modem_tx = dlci->modem_tx; + if (onoff) modem_tx |= TIOCM_DTR | TIOCM_RTS; else @@ -2992,12 +3019,13 @@ static void gsmtty_close(struct tty_struct *tty, struct file *filp) } tty_port_close_end(&dlci->port, tty); tty_port_tty_set(&dlci->port, NULL); - return; + } static void gsmtty_hangup(struct tty_struct *tty) { struct gsm_dlci *dlci = tty->driver_data; + if (dlci->state == DLCI_CLOSED) return; tty_port_hangup(&dlci->port); @@ -3009,6 +3037,7 @@ static int gsmtty_write(struct tty_struct *tty, const unsigned char *buf, { int sent; struct gsm_dlci *dlci = tty->driver_data; + if (dlci->state == DLCI_CLOSED) return -EINVAL; /* Stuff the bytes into the fifo queue */ @@ -3021,6 +3050,7 @@ static int gsmtty_write(struct tty_struct *tty, const unsigned char *buf, static int gsmtty_write_room(struct tty_struct *tty) { struct gsm_dlci *dlci = tty->driver_data; + if (dlci->state == DLCI_CLOSED) return -EINVAL; return TX_SIZE - kfifo_len(dlci->fifo); @@ -3029,6 +3059,7 @@ static int gsmtty_write_room(struct tty_struct *tty) static int gsmtty_chars_in_buffer(struct tty_struct *tty) { struct gsm_dlci *dlci = tty->driver_data; + if (dlci->state == DLCI_CLOSED) return -EINVAL; return kfifo_len(dlci->fifo); @@ -3037,6 +3068,7 @@ static int gsmtty_chars_in_buffer(struct tty_struct *tty) static void gsmtty_flush_buffer(struct tty_struct *tty) { struct gsm_dlci *dlci = tty->driver_data; + if (dlci->state == DLCI_CLOSED) return; /* Caution needed: If we implement reliable transport classes @@ -3057,6 +3089,7 @@ static void gsmtty_wait_until_sent(struct tty_struct *tty, int timeout) static int gsmtty_tiocmget(struct tty_struct *tty) { struct gsm_dlci *dlci = tty->driver_data; + if (dlci->state == DLCI_CLOSED) return -EINVAL; return dlci->modem_rx; @@ -3117,6 +3150,7 @@ static int gsmtty_ioctl(struct tty_struct *tty, static void gsmtty_set_termios(struct tty_struct *tty, struct ktermios *old) { struct gsm_dlci *dlci = tty->driver_data; + if (dlci->state == DLCI_CLOSED) return; /* For the moment its fixed. In actual fact the speed information @@ -3130,6 +3164,7 @@ static void gsmtty_set_termios(struct tty_struct *tty, struct ktermios *old) static void gsmtty_throttle(struct tty_struct *tty) { struct gsm_dlci *dlci = tty->driver_data; + if (dlci->state == DLCI_CLOSED) return; if (tty->termios.c_cflag & CRTSCTS) @@ -3142,6 +3177,7 @@ static void gsmtty_throttle(struct tty_struct *tty) static void gsmtty_unthrottle(struct tty_struct *tty) { struct gsm_dlci *dlci = tty->driver_data; + if (dlci->state == DLCI_CLOSED) return; if (tty->termios.c_cflag & CRTSCTS) @@ -3155,6 +3191,7 @@ static int gsmtty_break_ctl(struct tty_struct *tty, int state) { struct gsm_dlci *dlci = tty->driver_data; int encode = 0; /* Off */ + if (dlci->state == DLCI_CLOSED) return -EINVAL; @@ -3206,6 +3243,7 @@ static int __init gsm_init(void) { /* Fill in our line protocol discipline, and register it */ int status = tty_register_ldisc(N_GSM0710, &tty_ldisc_packet); + if (status != 0) { pr_err("n_gsm: can't register line discipline (err = %d)\n", status); @@ -3247,6 +3285,7 @@ static int __init gsm_init(void) static void __exit gsm_exit(void) { int status = tty_unregister_ldisc(N_GSM0710); + if (status != 0) pr_err("n_gsm: can't unregister line discipline (err = %d)\n", status); -- 1.9.1 --------------------------------------------------------------------- Intel Corporation SAS (French simplified joint stock company) Registered headquarters: "Les Montalets"- 2, rue de Paris, 92196 Meudon Cedex, France Registration Number: 302 456 199 R.C.S. NANTERRE Capital: 4,572,000 Euros This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies. ��.n��������+%������w��{.n�����{��ǫ����{ay�ʇڙ���f���h������_�(�階�ݢj"��������G����?���&��