Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 1 | /* USB communication methods |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
| 16 | */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 17 | #include "board.h" |
| 18 | #include "llist_irqsafe.h" |
| 19 | #include "usb_buf.h" |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 20 | #include "utils.h" |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 21 | |
Harald Welte | 9d90d28 | 2018-06-29 22:25:42 +0200 | [diff] [blame] | 22 | #include <osmocom/core/linuxlist.h> |
| 23 | #include <osmocom/core/msgb.h> |
Harald Welte | 3d27c84 | 2016-03-17 20:07:45 +0100 | [diff] [blame] | 24 | #include <errno.h> |
| 25 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 26 | /*********************************************************************** |
| 27 | * USBD Integration API |
| 28 | ***********************************************************************/ |
Christina Quast | 2b8a18b | 2015-04-12 09:31:36 +0200 | [diff] [blame] | 29 | |
Harald Welte | 271be9d | 2019-12-14 18:58:58 +0100 | [diff] [blame] | 30 | /* call-back after (successful?) transfer of a write buffer on IN EP */ |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 31 | static void usb_write_cb(uint8_t *arg, uint8_t status, uint32_t transferred, |
| 32 | uint32_t remaining) |
Christina Quast | 2b8a18b | 2015-04-12 09:31:36 +0200 | [diff] [blame] | 33 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 34 | struct msgb *msg = (struct msgb *) arg; |
| 35 | struct usb_buffered_ep *bep = msg->dst; |
Harald Welte | 5b13602 | 2019-12-15 23:55:15 +0100 | [diff] [blame] | 36 | uint16_t ep_size = USBD_GetEndpointSize(bep->ep); |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 37 | unsigned long x; |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 38 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 39 | TRACE_DEBUG("%s (EP=0x%02x)\r\n", __func__, bep->ep); |
Harald Welte | a0cf200 | 2016-03-02 10:35:51 +0100 | [diff] [blame] | 40 | |
Harald Welte | 5b13602 | 2019-12-15 23:55:15 +0100 | [diff] [blame] | 41 | if (((msgb_length(msg) % ep_size) == 0) && (transferred == ep_size)) { |
| 42 | /* terminate with ZLP; pass in 'msg' again as 'arg' so we get |
| 43 | * called the second time and proceed with usb_buf_free below */ |
| 44 | USBD_Write(bep->ep, 0, 0, (TransferCallback) &usb_write_cb, msg); |
| 45 | return; |
| 46 | } |
| 47 | |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 48 | local_irq_save(x); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 49 | bep->in_progress--; |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 50 | local_irq_restore(x); |
Harald Welte | 342a7fe | 2019-12-14 11:53:46 +0100 | [diff] [blame] | 51 | TRACE_DEBUG("%u: in_progress=%lu\r\n", bep->ep, bep->in_progress); |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 52 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 53 | if (status != USBD_STATUS_SUCCESS) |
Harald Welte | 342a7fe | 2019-12-14 11:53:46 +0100 | [diff] [blame] | 54 | TRACE_ERROR("%s error, status=%d\r\n", __func__, status); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 55 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 56 | usb_buf_free(msg); |
Christina Quast | 2b8a18b | 2015-04-12 09:31:36 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Harald Welte | 271be9d | 2019-12-14 18:58:58 +0100 | [diff] [blame] | 59 | /* check if the spcified IN endpoint is idle and submit the next buffer from queue */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 60 | int usb_refill_to_host(uint8_t ep) |
Christina Quast | 2b8a18b | 2015-04-12 09:31:36 +0200 | [diff] [blame] | 61 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 62 | struct usb_buffered_ep *bep = usb_get_buf_ep(ep); |
| 63 | struct msgb *msg; |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 64 | unsigned long x; |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 65 | int rc; |
Christina Quast | 2b8a18b | 2015-04-12 09:31:36 +0200 | [diff] [blame] | 66 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 67 | #if 0 |
| 68 | if (bep->out_from_host) { |
| 69 | TRACE_ERROR("EP 0x%02x is not IN\r\n", bep->ep); |
| 70 | return -EINVAL; |
| 71 | } |
| 72 | #endif |
| 73 | |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 74 | local_irq_save(x); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 75 | if (bep->in_progress) { |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 76 | local_irq_restore(x); |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 77 | return 0; |
Harald Welte | a0cf200 | 2016-03-02 10:35:51 +0100 | [diff] [blame] | 78 | } |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 79 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 80 | if (llist_empty(&bep->queue)) { |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 81 | local_irq_restore(x); |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 82 | return 0; |
Harald Welte | a0cf200 | 2016-03-02 10:35:51 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 85 | bep->in_progress++; |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 86 | |
Harald Welte | f4a625b | 2019-12-14 19:07:57 +0100 | [diff] [blame] | 87 | msg = msgb_dequeue_count(&bep->queue, &bep->queue_len); |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 88 | |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 89 | local_irq_restore(x); |
Harald Welte | a0cf200 | 2016-03-02 10:35:51 +0100 | [diff] [blame] | 90 | |
Harald Welte | 3c003cc | 2019-12-14 11:39:14 +0100 | [diff] [blame] | 91 | TRACE_DEBUG("%s (EP=0x%02x), in_progress=%lu\r\n", __func__, ep, bep->in_progress); |
Harald Welte | a0cf200 | 2016-03-02 10:35:51 +0100 | [diff] [blame] | 92 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 93 | msg->dst = bep; |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 94 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 95 | rc = USBD_Write(ep, msgb_data(msg), msgb_length(msg), |
| 96 | (TransferCallback) &usb_write_cb, msg); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 97 | if (rc != USBD_STATUS_SUCCESS) { |
Harald Welte | 342a7fe | 2019-12-14 11:53:46 +0100 | [diff] [blame] | 98 | TRACE_ERROR("%s error %x\r\n", __func__, rc); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 99 | /* re-insert to head of queue */ |
| 100 | llist_add_irqsafe(&msg->list, &bep->queue); |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 101 | local_irq_save(x); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 102 | bep->in_progress--; |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 103 | local_irq_restore(x); |
Harald Welte | 342a7fe | 2019-12-14 11:53:46 +0100 | [diff] [blame] | 104 | TRACE_DEBUG("%02x: in_progress=%lu\r\n", bep->ep, bep->in_progress); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | return 1; |
Christina Quast | 2b8a18b | 2015-04-12 09:31:36 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Harald Welte | 271be9d | 2019-12-14 18:58:58 +0100 | [diff] [blame] | 111 | /* call-back after (successful?) read transfer of a buffer on OUT EP */ |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 112 | static void usb_read_cb(uint8_t *arg, uint8_t status, uint32_t transferred, |
| 113 | uint32_t remaining) |
Christina Quast | 2b8a18b | 2015-04-12 09:31:36 +0200 | [diff] [blame] | 114 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 115 | struct msgb *msg = (struct msgb *) arg; |
| 116 | struct usb_buffered_ep *bep = msg->dst; |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 117 | |
Harald Welte | 3c003cc | 2019-12-14 11:39:14 +0100 | [diff] [blame] | 118 | TRACE_DEBUG("%s (EP=%u, len=%lu, q=%p)\r\n", __func__, |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 119 | bep->ep, transferred, &bep->queue); |
Harald Welte | a0cf200 | 2016-03-02 10:35:51 +0100 | [diff] [blame] | 120 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 121 | bep->in_progress = 0; |
Christina Quast | 2b8a18b | 2015-04-12 09:31:36 +0200 | [diff] [blame] | 122 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 123 | if (status != USBD_STATUS_SUCCESS) { |
Harald Welte | 342a7fe | 2019-12-14 11:53:46 +0100 | [diff] [blame] | 124 | TRACE_ERROR("%s error, status=%d\r\n", __func__, status); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 125 | usb_buf_free(msg); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 126 | return; |
| 127 | } |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 128 | msgb_put(msg, transferred); |
| 129 | llist_add_tail_irqsafe(&msg->list, &bep->queue); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Harald Welte | 271be9d | 2019-12-14 18:58:58 +0100 | [diff] [blame] | 132 | /* refill the read queue for data received from host PC on OUT EP, if needed */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 133 | int usb_refill_from_host(uint8_t ep) |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 134 | { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 135 | struct usb_buffered_ep *bep = usb_get_buf_ep(ep); |
| 136 | struct msgb *msg; |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 137 | unsigned long x; |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 138 | int rc; |
| 139 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 140 | #if 0 |
| 141 | if (!bep->out_from_host) { |
| 142 | TRACE_ERROR("EP 0x%02x is not OUT\r\n", bep->ep); |
| 143 | return -EINVAL; |
| 144 | } |
| 145 | #endif |
| 146 | |
| 147 | if (bep->in_progress) |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 148 | return 0; |
| 149 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 150 | TRACE_DEBUG("%s (EP=0x%02x)\r\n", __func__, bep->ep); |
Harald Welte | a0cf200 | 2016-03-02 10:35:51 +0100 | [diff] [blame] | 151 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 152 | msg = usb_buf_alloc(bep->ep); |
| 153 | if (!msg) |
Harald Welte | 3d27c84 | 2016-03-17 20:07:45 +0100 | [diff] [blame] | 154 | return -ENOMEM; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 155 | msg->dst = bep; |
| 156 | msg->l1h = msg->head; |
Harald Welte | 3d27c84 | 2016-03-17 20:07:45 +0100 | [diff] [blame] | 157 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 158 | bep->in_progress = 1; |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 159 | |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 160 | rc = USBD_Read(ep, msg->head, msgb_tailroom(msg), |
| 161 | (TransferCallback) &usb_read_cb, msg); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 162 | if (rc != USBD_STATUS_SUCCESS) { |
Harald Welte | 342a7fe | 2019-12-14 11:53:46 +0100 | [diff] [blame] | 163 | TRACE_ERROR("%s error %d\r\n", __func__, rc); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 164 | usb_buf_free(msg); |
| 165 | bep->in_progress = 0; |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 168 | return 1; |
Christina Quast | 2b8a18b | 2015-04-12 09:31:36 +0200 | [diff] [blame] | 169 | } |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 170 | |
Harald Welte | 271be9d | 2019-12-14 18:58:58 +0100 | [diff] [blame] | 171 | /* drain any buffers from the queue of the endpoint and release their memory */ |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 172 | int usb_drain_queue(uint8_t ep) |
| 173 | { |
| 174 | struct usb_buffered_ep *bep = usb_get_buf_ep(ep); |
| 175 | struct msgb *msg; |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 176 | unsigned long x; |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 177 | int ret = 0; |
| 178 | |
| 179 | /* wait until no transfers are in progress anymore and block |
| 180 | * further interrupts */ |
| 181 | while (1) { |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 182 | local_irq_save(x); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 183 | if (!bep->in_progress) { |
| 184 | break; |
| 185 | } |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 186 | local_irq_restore(x); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 187 | /* retry */ |
| 188 | } |
| 189 | |
| 190 | /* free all queued msgbs */ |
Harald Welte | f4a625b | 2019-12-14 19:07:57 +0100 | [diff] [blame] | 191 | while ((msg = msgb_dequeue_count(&bep->queue, &bep->queue_len))) { |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 192 | usb_buf_free(msg); |
| 193 | ret++; |
| 194 | } |
| 195 | |
| 196 | /* re-enable interrupts and return number of free'd msgbs */ |
Harald Welte | 119624f | 2017-11-04 12:28:30 +0100 | [diff] [blame] | 197 | local_irq_restore(x); |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 198 | |
| 199 | return ret; |
| 200 | } |