Loading...
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (C) 2021-2022, Intel Corporation. */
3
4#include "ice.h"
5#include "ice_lib.h"
6
7/**
8 * ice_gnss_do_write - Write data to internal GNSS receiver
9 * @pf: board private structure
10 * @buf: command buffer
11 * @size: command buffer size
12 *
13 * Write UBX command data to the GNSS receiver
14 *
15 * Return:
16 * * number of bytes written - success
17 * * negative - error code
18 */
19static int
20ice_gnss_do_write(struct ice_pf *pf, const unsigned char *buf, unsigned int size)
21{
22 struct ice_aqc_link_topo_addr link_topo;
23 struct ice_hw *hw = &pf->hw;
24 unsigned int offset = 0;
25 int err = 0;
26
27 memset(&link_topo, 0, sizeof(struct ice_aqc_link_topo_addr));
28 link_topo.topo_params.index = ICE_E810T_GNSS_I2C_BUS;
29 link_topo.topo_params.node_type_ctx |=
30 FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_CTX_M,
31 ICE_AQC_LINK_TOPO_NODE_CTX_OVERRIDE);
32
33 /* It's not possible to write a single byte to u-blox.
34 * Write all bytes in a loop until there are 6 or less bytes left. If
35 * there are exactly 6 bytes left, the last write would be only a byte.
36 * In this case, do 4+2 bytes writes instead of 5+1. Otherwise, do the
37 * last 2 to 5 bytes write.
38 */
39 while (size - offset > ICE_GNSS_UBX_WRITE_BYTES + 1) {
40 err = ice_aq_write_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
41 cpu_to_le16(buf[offset]),
42 ICE_MAX_I2C_WRITE_BYTES,
43 &buf[offset + 1], NULL);
44 if (err)
45 goto err_out;
46
47 offset += ICE_GNSS_UBX_WRITE_BYTES;
48 }
49
50 /* Single byte would be written. Write 4 bytes instead of 5. */
51 if (size - offset == ICE_GNSS_UBX_WRITE_BYTES + 1) {
52 err = ice_aq_write_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
53 cpu_to_le16(buf[offset]),
54 ICE_MAX_I2C_WRITE_BYTES - 1,
55 &buf[offset + 1], NULL);
56 if (err)
57 goto err_out;
58
59 offset += ICE_GNSS_UBX_WRITE_BYTES - 1;
60 }
61
62 /* Do the last write, 2 to 5 bytes. */
63 err = ice_aq_write_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
64 cpu_to_le16(buf[offset]), size - offset - 1,
65 &buf[offset + 1], NULL);
66 if (err)
67 goto err_out;
68
69 return size;
70
71err_out:
72 dev_err(ice_pf_to_dev(pf), "GNSS failed to write, offset=%u, size=%u, err=%d\n",
73 offset, size, err);
74
75 return err;
76}
77
78/**
79 * ice_gnss_read - Read data from internal GNSS module
80 * @work: GNSS read work structure
81 *
82 * Read the data from internal GNSS receiver, write it to gnss_dev.
83 */
84static void ice_gnss_read(struct kthread_work *work)
85{
86 struct gnss_serial *gnss = container_of(work, struct gnss_serial,
87 read_work.work);
88 unsigned long delay = ICE_GNSS_POLL_DATA_DELAY_TIME;
89 unsigned int i, bytes_read, data_len, count;
90 struct ice_aqc_link_topo_addr link_topo;
91 struct ice_pf *pf;
92 struct ice_hw *hw;
93 __be16 data_len_b;
94 char *buf = NULL;
95 u8 i2c_params;
96 int err = 0;
97
98 pf = gnss->back;
99 if (!pf || !test_bit(ICE_FLAG_GNSS, pf->flags))
100 return;
101
102 hw = &pf->hw;
103
104 memset(&link_topo, 0, sizeof(struct ice_aqc_link_topo_addr));
105 link_topo.topo_params.index = ICE_E810T_GNSS_I2C_BUS;
106 link_topo.topo_params.node_type_ctx |=
107 FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_CTX_M,
108 ICE_AQC_LINK_TOPO_NODE_CTX_OVERRIDE);
109
110 i2c_params = ICE_GNSS_UBX_DATA_LEN_WIDTH |
111 ICE_AQC_I2C_USE_REPEATED_START;
112
113 err = ice_aq_read_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
114 cpu_to_le16(ICE_GNSS_UBX_DATA_LEN_H),
115 i2c_params, (u8 *)&data_len_b, NULL);
116 if (err)
117 goto requeue;
118
119 data_len = be16_to_cpu(data_len_b);
120 if (data_len == 0 || data_len == U16_MAX)
121 goto requeue;
122
123 /* The u-blox has data_len bytes for us to read */
124
125 data_len = min_t(typeof(data_len), data_len, PAGE_SIZE);
126
127 buf = (char *)get_zeroed_page(GFP_KERNEL);
128 if (!buf) {
129 err = -ENOMEM;
130 goto requeue;
131 }
132
133 /* Read received data */
134 for (i = 0; i < data_len; i += bytes_read) {
135 unsigned int bytes_left = data_len - i;
136
137 bytes_read = min_t(typeof(bytes_left), bytes_left,
138 ICE_MAX_I2C_DATA_SIZE);
139
140 err = ice_aq_read_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
141 cpu_to_le16(ICE_GNSS_UBX_EMPTY_DATA),
142 bytes_read, &buf[i], NULL);
143 if (err)
144 goto free_buf;
145 }
146
147 count = gnss_insert_raw(pf->gnss_dev, buf, i);
148 if (count != i)
149 dev_warn(ice_pf_to_dev(pf),
150 "gnss_insert_raw ret=%d size=%d\n",
151 count, i);
152 delay = ICE_GNSS_TIMER_DELAY_TIME;
153free_buf:
154 free_page((unsigned long)buf);
155requeue:
156 kthread_queue_delayed_work(gnss->kworker, &gnss->read_work, delay);
157 if (err)
158 dev_dbg(ice_pf_to_dev(pf), "GNSS failed to read err=%d\n", err);
159}
160
161/**
162 * ice_gnss_struct_init - Initialize GNSS receiver
163 * @pf: Board private structure
164 *
165 * Initialize GNSS structures and workers.
166 *
167 * Return:
168 * * pointer to initialized gnss_serial struct - success
169 * * NULL - error
170 */
171static struct gnss_serial *ice_gnss_struct_init(struct ice_pf *pf)
172{
173 struct device *dev = ice_pf_to_dev(pf);
174 struct kthread_worker *kworker;
175 struct gnss_serial *gnss;
176
177 gnss = kzalloc(sizeof(*gnss), GFP_KERNEL);
178 if (!gnss)
179 return NULL;
180
181 gnss->back = pf;
182 pf->gnss_serial = gnss;
183
184 kthread_init_delayed_work(&gnss->read_work, ice_gnss_read);
185 kworker = kthread_create_worker(0, "ice-gnss-%s", dev_name(dev));
186 if (IS_ERR(kworker)) {
187 kfree(gnss);
188 return NULL;
189 }
190
191 gnss->kworker = kworker;
192
193 return gnss;
194}
195
196/**
197 * ice_gnss_open - Open GNSS device
198 * @gdev: pointer to the gnss device struct
199 *
200 * Open GNSS device and start filling the read buffer for consumer.
201 *
202 * Return:
203 * * 0 - success
204 * * negative - error code
205 */
206static int ice_gnss_open(struct gnss_device *gdev)
207{
208 struct ice_pf *pf = gnss_get_drvdata(gdev);
209 struct gnss_serial *gnss;
210
211 if (!pf)
212 return -EFAULT;
213
214 if (!test_bit(ICE_FLAG_GNSS, pf->flags))
215 return -EFAULT;
216
217 gnss = pf->gnss_serial;
218 if (!gnss)
219 return -ENODEV;
220
221 kthread_queue_delayed_work(gnss->kworker, &gnss->read_work, 0);
222
223 return 0;
224}
225
226/**
227 * ice_gnss_close - Close GNSS device
228 * @gdev: pointer to the gnss device struct
229 *
230 * Close GNSS device, cancel worker, stop filling the read buffer.
231 */
232static void ice_gnss_close(struct gnss_device *gdev)
233{
234 struct ice_pf *pf = gnss_get_drvdata(gdev);
235 struct gnss_serial *gnss;
236
237 if (!pf)
238 return;
239
240 gnss = pf->gnss_serial;
241 if (!gnss)
242 return;
243
244 kthread_cancel_delayed_work_sync(&gnss->read_work);
245}
246
247/**
248 * ice_gnss_write - Write to GNSS device
249 * @gdev: pointer to the gnss device struct
250 * @buf: pointer to the user data
251 * @count: size of the buffer to be sent to the GNSS device
252 *
253 * Return:
254 * * number of written bytes - success
255 * * negative - error code
256 */
257static int
258ice_gnss_write(struct gnss_device *gdev, const unsigned char *buf,
259 size_t count)
260{
261 struct ice_pf *pf = gnss_get_drvdata(gdev);
262 struct gnss_serial *gnss;
263
264 /* We cannot write a single byte using our I2C implementation. */
265 if (count <= 1 || count > ICE_GNSS_TTY_WRITE_BUF)
266 return -EINVAL;
267
268 if (!pf)
269 return -EFAULT;
270
271 if (!test_bit(ICE_FLAG_GNSS, pf->flags))
272 return -EFAULT;
273
274 gnss = pf->gnss_serial;
275 if (!gnss)
276 return -ENODEV;
277
278 return ice_gnss_do_write(pf, buf, count);
279}
280
281static const struct gnss_operations ice_gnss_ops = {
282 .open = ice_gnss_open,
283 .close = ice_gnss_close,
284 .write_raw = ice_gnss_write,
285};
286
287/**
288 * ice_gnss_register - Register GNSS receiver
289 * @pf: Board private structure
290 *
291 * Allocate and register GNSS receiver in the Linux GNSS subsystem.
292 *
293 * Return:
294 * * 0 - success
295 * * negative - error code
296 */
297static int ice_gnss_register(struct ice_pf *pf)
298{
299 struct gnss_device *gdev;
300 int ret;
301
302 gdev = gnss_allocate_device(ice_pf_to_dev(pf));
303 if (!gdev) {
304 dev_err(ice_pf_to_dev(pf),
305 "gnss_allocate_device returns NULL\n");
306 return -ENOMEM;
307 }
308
309 gdev->ops = &ice_gnss_ops;
310 gdev->type = GNSS_TYPE_UBX;
311 gnss_set_drvdata(gdev, pf);
312 ret = gnss_register_device(gdev);
313 if (ret) {
314 dev_err(ice_pf_to_dev(pf), "gnss_register_device err=%d\n",
315 ret);
316 gnss_put_device(gdev);
317 } else {
318 pf->gnss_dev = gdev;
319 }
320
321 return ret;
322}
323
324/**
325 * ice_gnss_deregister - Deregister GNSS receiver
326 * @pf: Board private structure
327 *
328 * Deregister GNSS receiver from the Linux GNSS subsystem,
329 * release its resources.
330 */
331static void ice_gnss_deregister(struct ice_pf *pf)
332{
333 if (pf->gnss_dev) {
334 gnss_deregister_device(pf->gnss_dev);
335 gnss_put_device(pf->gnss_dev);
336 pf->gnss_dev = NULL;
337 }
338}
339
340/**
341 * ice_gnss_init - Initialize GNSS support
342 * @pf: Board private structure
343 */
344void ice_gnss_init(struct ice_pf *pf)
345{
346 int ret;
347
348 pf->gnss_serial = ice_gnss_struct_init(pf);
349 if (!pf->gnss_serial)
350 return;
351
352 ret = ice_gnss_register(pf);
353 if (!ret) {
354 set_bit(ICE_FLAG_GNSS, pf->flags);
355 dev_info(ice_pf_to_dev(pf), "GNSS init successful\n");
356 } else {
357 ice_gnss_exit(pf);
358 dev_err(ice_pf_to_dev(pf), "GNSS init failure\n");
359 }
360}
361
362/**
363 * ice_gnss_exit - Disable GNSS TTY support
364 * @pf: Board private structure
365 */
366void ice_gnss_exit(struct ice_pf *pf)
367{
368 ice_gnss_deregister(pf);
369 clear_bit(ICE_FLAG_GNSS, pf->flags);
370
371 if (pf->gnss_serial) {
372 struct gnss_serial *gnss = pf->gnss_serial;
373
374 kthread_cancel_delayed_work_sync(&gnss->read_work);
375 kthread_destroy_worker(gnss->kworker);
376 gnss->kworker = NULL;
377
378 kfree(gnss);
379 pf->gnss_serial = NULL;
380 }
381}
382
383/**
384 * ice_gnss_is_gps_present - Check if GPS HW is present
385 * @hw: pointer to HW struct
386 */
387bool ice_gnss_is_gps_present(struct ice_hw *hw)
388{
389 if (!hw->func_caps.ts_func_info.src_tmr_owned)
390 return false;
391
392 if (!ice_is_gps_in_netlist(hw))
393 return false;
394
395#if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
396 if (ice_is_e810t(hw)) {
397 int err;
398 u8 data;
399
400 err = ice_read_pca9575_reg(hw, ICE_PCA9575_P0_IN, &data);
401 if (err || !!(data & ICE_P0_GNSS_PRSNT_N))
402 return false;
403 } else {
404 return false;
405 }
406#else
407 if (!ice_is_e810t(hw))
408 return false;
409#endif /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */
410
411 return true;
412}
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (C) 2021-2022, Intel Corporation. */
3
4#include "ice.h"
5#include "ice_lib.h"
6#include <linux/tty_driver.h>
7
8/**
9 * ice_gnss_do_write - Write data to internal GNSS
10 * @pf: board private structure
11 * @buf: command buffer
12 * @size: command buffer size
13 *
14 * Write UBX command data to the GNSS receiver
15 */
16static unsigned int
17ice_gnss_do_write(struct ice_pf *pf, unsigned char *buf, unsigned int size)
18{
19 struct ice_aqc_link_topo_addr link_topo;
20 struct ice_hw *hw = &pf->hw;
21 unsigned int offset = 0;
22 int err = 0;
23
24 memset(&link_topo, 0, sizeof(struct ice_aqc_link_topo_addr));
25 link_topo.topo_params.index = ICE_E810T_GNSS_I2C_BUS;
26 link_topo.topo_params.node_type_ctx |=
27 FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_CTX_M,
28 ICE_AQC_LINK_TOPO_NODE_CTX_OVERRIDE);
29
30 /* It's not possible to write a single byte to u-blox.
31 * Write all bytes in a loop until there are 6 or less bytes left. If
32 * there are exactly 6 bytes left, the last write would be only a byte.
33 * In this case, do 4+2 bytes writes instead of 5+1. Otherwise, do the
34 * last 2 to 5 bytes write.
35 */
36 while (size - offset > ICE_GNSS_UBX_WRITE_BYTES + 1) {
37 err = ice_aq_write_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
38 cpu_to_le16(buf[offset]),
39 ICE_MAX_I2C_WRITE_BYTES,
40 &buf[offset + 1], NULL);
41 if (err)
42 goto err_out;
43
44 offset += ICE_GNSS_UBX_WRITE_BYTES;
45 }
46
47 /* Single byte would be written. Write 4 bytes instead of 5. */
48 if (size - offset == ICE_GNSS_UBX_WRITE_BYTES + 1) {
49 err = ice_aq_write_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
50 cpu_to_le16(buf[offset]),
51 ICE_MAX_I2C_WRITE_BYTES - 1,
52 &buf[offset + 1], NULL);
53 if (err)
54 goto err_out;
55
56 offset += ICE_GNSS_UBX_WRITE_BYTES - 1;
57 }
58
59 /* Do the last write, 2 to 5 bytes. */
60 err = ice_aq_write_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
61 cpu_to_le16(buf[offset]), size - offset - 1,
62 &buf[offset + 1], NULL);
63 if (err)
64 goto err_out;
65
66 return size;
67
68err_out:
69 dev_err(ice_pf_to_dev(pf), "GNSS failed to write, offset=%u, size=%u, err=%d\n",
70 offset, size, err);
71
72 return offset;
73}
74
75/**
76 * ice_gnss_write_pending - Write all pending data to internal GNSS
77 * @work: GNSS write work structure
78 */
79static void ice_gnss_write_pending(struct kthread_work *work)
80{
81 struct gnss_serial *gnss = container_of(work, struct gnss_serial,
82 write_work);
83 struct ice_pf *pf = gnss->back;
84
85 if (!list_empty(&gnss->queue)) {
86 struct gnss_write_buf *write_buf = NULL;
87 unsigned int bytes;
88
89 write_buf = list_first_entry(&gnss->queue,
90 struct gnss_write_buf, queue);
91
92 bytes = ice_gnss_do_write(pf, write_buf->buf, write_buf->size);
93 dev_dbg(ice_pf_to_dev(pf), "%u bytes written to GNSS\n", bytes);
94
95 list_del(&write_buf->queue);
96 kfree(write_buf->buf);
97 kfree(write_buf);
98 }
99}
100
101/**
102 * ice_gnss_read - Read data from internal GNSS module
103 * @work: GNSS read work structure
104 *
105 * Read the data from internal GNSS receiver, number of bytes read will be
106 * returned in *read_data parameter.
107 */
108static void ice_gnss_read(struct kthread_work *work)
109{
110 struct gnss_serial *gnss = container_of(work, struct gnss_serial,
111 read_work.work);
112 struct ice_aqc_link_topo_addr link_topo;
113 unsigned int i, bytes_read, data_len;
114 struct tty_port *port;
115 struct ice_pf *pf;
116 struct ice_hw *hw;
117 __be16 data_len_b;
118 char *buf = NULL;
119 u8 i2c_params;
120 int err = 0;
121
122 pf = gnss->back;
123 if (!pf || !gnss->tty || !gnss->tty->port) {
124 err = -EFAULT;
125 goto exit;
126 }
127
128 hw = &pf->hw;
129 port = gnss->tty->port;
130
131 buf = (char *)get_zeroed_page(GFP_KERNEL);
132 if (!buf) {
133 err = -ENOMEM;
134 goto exit;
135 }
136
137 memset(&link_topo, 0, sizeof(struct ice_aqc_link_topo_addr));
138 link_topo.topo_params.index = ICE_E810T_GNSS_I2C_BUS;
139 link_topo.topo_params.node_type_ctx |=
140 FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_CTX_M,
141 ICE_AQC_LINK_TOPO_NODE_CTX_OVERRIDE);
142
143 i2c_params = ICE_GNSS_UBX_DATA_LEN_WIDTH |
144 ICE_AQC_I2C_USE_REPEATED_START;
145
146 /* Read data length in a loop, when it's not 0 the data is ready */
147 for (i = 0; i < ICE_MAX_UBX_READ_TRIES; i++) {
148 err = ice_aq_read_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
149 cpu_to_le16(ICE_GNSS_UBX_DATA_LEN_H),
150 i2c_params, (u8 *)&data_len_b, NULL);
151 if (err)
152 goto exit_buf;
153
154 data_len = be16_to_cpu(data_len_b);
155 if (data_len != 0 && data_len != U16_MAX)
156 break;
157
158 mdelay(10);
159 }
160
161 data_len = min_t(typeof(data_len), data_len, PAGE_SIZE);
162 data_len = tty_buffer_request_room(port, data_len);
163 if (!data_len) {
164 err = -ENOMEM;
165 goto exit_buf;
166 }
167
168 /* Read received data */
169 for (i = 0; i < data_len; i += bytes_read) {
170 unsigned int bytes_left = data_len - i;
171
172 bytes_read = min_t(typeof(bytes_left), bytes_left,
173 ICE_MAX_I2C_DATA_SIZE);
174
175 err = ice_aq_read_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
176 cpu_to_le16(ICE_GNSS_UBX_EMPTY_DATA),
177 bytes_read, &buf[i], NULL);
178 if (err)
179 goto exit_buf;
180 }
181
182 /* Send the data to the tty layer for users to read. This doesn't
183 * actually push the data through unless tty->low_latency is set.
184 */
185 tty_insert_flip_string(port, buf, i);
186 tty_flip_buffer_push(port);
187
188exit_buf:
189 free_page((unsigned long)buf);
190 kthread_queue_delayed_work(gnss->kworker, &gnss->read_work,
191 ICE_GNSS_TIMER_DELAY_TIME);
192exit:
193 if (err)
194 dev_dbg(ice_pf_to_dev(pf), "GNSS failed to read err=%d\n", err);
195}
196
197/**
198 * ice_gnss_struct_init - Initialize GNSS structure for the TTY
199 * @pf: Board private structure
200 * @index: TTY device index
201 */
202static struct gnss_serial *ice_gnss_struct_init(struct ice_pf *pf, int index)
203{
204 struct device *dev = ice_pf_to_dev(pf);
205 struct kthread_worker *kworker;
206 struct gnss_serial *gnss;
207
208 gnss = kzalloc(sizeof(*gnss), GFP_KERNEL);
209 if (!gnss)
210 return NULL;
211
212 mutex_init(&gnss->gnss_mutex);
213 gnss->open_count = 0;
214 gnss->back = pf;
215 pf->gnss_serial[index] = gnss;
216
217 kthread_init_delayed_work(&gnss->read_work, ice_gnss_read);
218 INIT_LIST_HEAD(&gnss->queue);
219 kthread_init_work(&gnss->write_work, ice_gnss_write_pending);
220 /* Allocate a kworker for handling work required for the GNSS TTY
221 * writes.
222 */
223 kworker = kthread_create_worker(0, "ice-gnss-%s", dev_name(dev));
224 if (IS_ERR(kworker)) {
225 kfree(gnss);
226 return NULL;
227 }
228
229 gnss->kworker = kworker;
230
231 return gnss;
232}
233
234/**
235 * ice_gnss_tty_open - Initialize GNSS structures on TTY device open
236 * @tty: pointer to the tty_struct
237 * @filp: pointer to the file
238 *
239 * This routine is mandatory. If this routine is not filled in, the attempted
240 * open will fail with ENODEV.
241 */
242static int ice_gnss_tty_open(struct tty_struct *tty, struct file *filp)
243{
244 struct gnss_serial *gnss;
245 struct ice_pf *pf;
246
247 pf = (struct ice_pf *)tty->driver->driver_state;
248 if (!pf)
249 return -EFAULT;
250
251 /* Clear the pointer in case something fails */
252 tty->driver_data = NULL;
253
254 /* Get the serial object associated with this tty pointer */
255 gnss = pf->gnss_serial[tty->index];
256 if (!gnss) {
257 /* Initialize GNSS struct on the first device open */
258 gnss = ice_gnss_struct_init(pf, tty->index);
259 if (!gnss)
260 return -ENOMEM;
261 }
262
263 mutex_lock(&gnss->gnss_mutex);
264
265 /* Save our structure within the tty structure */
266 tty->driver_data = gnss;
267 gnss->tty = tty;
268 gnss->open_count++;
269 kthread_queue_delayed_work(gnss->kworker, &gnss->read_work, 0);
270
271 mutex_unlock(&gnss->gnss_mutex);
272
273 return 0;
274}
275
276/**
277 * ice_gnss_tty_close - Cleanup GNSS structures on tty device close
278 * @tty: pointer to the tty_struct
279 * @filp: pointer to the file
280 */
281static void ice_gnss_tty_close(struct tty_struct *tty, struct file *filp)
282{
283 struct gnss_serial *gnss = tty->driver_data;
284 struct ice_pf *pf;
285
286 if (!gnss)
287 return;
288
289 pf = (struct ice_pf *)tty->driver->driver_state;
290 if (!pf)
291 return;
292
293 mutex_lock(&gnss->gnss_mutex);
294
295 if (!gnss->open_count) {
296 /* Port was never opened */
297 dev_err(ice_pf_to_dev(pf), "GNSS port not opened\n");
298 goto exit;
299 }
300
301 gnss->open_count--;
302 if (gnss->open_count <= 0) {
303 /* Port is in shutdown state */
304 kthread_cancel_delayed_work_sync(&gnss->read_work);
305 }
306exit:
307 mutex_unlock(&gnss->gnss_mutex);
308}
309
310/**
311 * ice_gnss_tty_write - Write GNSS data
312 * @tty: pointer to the tty_struct
313 * @buf: pointer to the user data
314 * @count: the number of characters queued to be sent to the HW
315 *
316 * The write function call is called by the user when there is data to be sent
317 * to the hardware. First the tty core receives the call, and then it passes the
318 * data on to the tty driver's write function. The tty core also tells the tty
319 * driver the size of the data being sent.
320 * If any errors happen during the write call, a negative error value should be
321 * returned instead of the number of characters queued to be written.
322 */
323static int
324ice_gnss_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
325{
326 struct gnss_write_buf *write_buf;
327 struct gnss_serial *gnss;
328 unsigned char *cmd_buf;
329 struct ice_pf *pf;
330 int err = count;
331
332 /* We cannot write a single byte using our I2C implementation. */
333 if (count <= 1 || count > ICE_GNSS_TTY_WRITE_BUF)
334 return -EINVAL;
335
336 gnss = tty->driver_data;
337 if (!gnss)
338 return -EFAULT;
339
340 pf = (struct ice_pf *)tty->driver->driver_state;
341 if (!pf)
342 return -EFAULT;
343
344 /* Only allow to write on TTY 0 */
345 if (gnss != pf->gnss_serial[0])
346 return -EIO;
347
348 mutex_lock(&gnss->gnss_mutex);
349
350 if (!gnss->open_count) {
351 err = -EINVAL;
352 goto exit;
353 }
354
355 cmd_buf = kcalloc(count, sizeof(*buf), GFP_KERNEL);
356 if (!cmd_buf) {
357 err = -ENOMEM;
358 goto exit;
359 }
360
361 memcpy(cmd_buf, buf, count);
362
363 /* Send the data out to a hardware port */
364 write_buf = kzalloc(sizeof(*write_buf), GFP_KERNEL);
365 if (!write_buf) {
366 kfree(cmd_buf);
367 err = -ENOMEM;
368 goto exit;
369 }
370
371 write_buf->buf = cmd_buf;
372 write_buf->size = count;
373 INIT_LIST_HEAD(&write_buf->queue);
374 list_add_tail(&write_buf->queue, &gnss->queue);
375 kthread_queue_work(gnss->kworker, &gnss->write_work);
376exit:
377 mutex_unlock(&gnss->gnss_mutex);
378 return err;
379}
380
381/**
382 * ice_gnss_tty_write_room - Returns the numbers of characters to be written.
383 * @tty: pointer to the tty_struct
384 *
385 * This routine returns the numbers of characters the tty driver will accept
386 * for queuing to be written or 0 if either the TTY is not open or user
387 * tries to write to the TTY other than the first.
388 */
389static unsigned int ice_gnss_tty_write_room(struct tty_struct *tty)
390{
391 struct gnss_serial *gnss = tty->driver_data;
392
393 /* Only allow to write on TTY 0 */
394 if (!gnss || gnss != gnss->back->gnss_serial[0])
395 return 0;
396
397 mutex_lock(&gnss->gnss_mutex);
398
399 if (!gnss->open_count) {
400 mutex_unlock(&gnss->gnss_mutex);
401 return 0;
402 }
403
404 mutex_unlock(&gnss->gnss_mutex);
405 return ICE_GNSS_TTY_WRITE_BUF;
406}
407
408static const struct tty_operations tty_gps_ops = {
409 .open = ice_gnss_tty_open,
410 .close = ice_gnss_tty_close,
411 .write = ice_gnss_tty_write,
412 .write_room = ice_gnss_tty_write_room,
413};
414
415/**
416 * ice_gnss_create_tty_driver - Create a TTY driver for GNSS
417 * @pf: Board private structure
418 */
419static struct tty_driver *ice_gnss_create_tty_driver(struct ice_pf *pf)
420{
421 struct device *dev = ice_pf_to_dev(pf);
422 const int ICE_TTYDRV_NAME_MAX = 14;
423 struct tty_driver *tty_driver;
424 char *ttydrv_name;
425 unsigned int i;
426 int err;
427
428 tty_driver = tty_alloc_driver(ICE_GNSS_TTY_MINOR_DEVICES,
429 TTY_DRIVER_REAL_RAW);
430 if (IS_ERR(tty_driver)) {
431 dev_err(dev, "Failed to allocate memory for GNSS TTY\n");
432 return NULL;
433 }
434
435 ttydrv_name = kzalloc(ICE_TTYDRV_NAME_MAX, GFP_KERNEL);
436 if (!ttydrv_name) {
437 tty_driver_kref_put(tty_driver);
438 return NULL;
439 }
440
441 snprintf(ttydrv_name, ICE_TTYDRV_NAME_MAX, "ttyGNSS_%02x%02x_",
442 (u8)pf->pdev->bus->number, (u8)PCI_SLOT(pf->pdev->devfn));
443
444 /* Initialize the tty driver*/
445 tty_driver->owner = THIS_MODULE;
446 tty_driver->driver_name = dev_driver_string(dev);
447 tty_driver->name = (const char *)ttydrv_name;
448 tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
449 tty_driver->subtype = SERIAL_TYPE_NORMAL;
450 tty_driver->init_termios = tty_std_termios;
451 tty_driver->init_termios.c_iflag &= ~INLCR;
452 tty_driver->init_termios.c_iflag |= IGNCR;
453 tty_driver->init_termios.c_oflag &= ~OPOST;
454 tty_driver->init_termios.c_lflag &= ~ICANON;
455 tty_driver->init_termios.c_cflag &= ~(CSIZE | CBAUD | CBAUDEX);
456 /* baud rate 9600 */
457 tty_termios_encode_baud_rate(&tty_driver->init_termios, 9600, 9600);
458 tty_driver->driver_state = pf;
459 tty_set_operations(tty_driver, &tty_gps_ops);
460
461 for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++) {
462 pf->gnss_tty_port[i] = kzalloc(sizeof(*pf->gnss_tty_port[i]),
463 GFP_KERNEL);
464 if (!pf->gnss_tty_port[i])
465 goto err_out;
466
467 pf->gnss_serial[i] = NULL;
468
469 tty_port_init(pf->gnss_tty_port[i]);
470 tty_port_link_device(pf->gnss_tty_port[i], tty_driver, i);
471 }
472
473 err = tty_register_driver(tty_driver);
474 if (err) {
475 dev_err(dev, "Failed to register TTY driver err=%d\n", err);
476 goto err_out;
477 }
478
479 for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++)
480 dev_info(dev, "%s%d registered\n", ttydrv_name, i);
481
482 return tty_driver;
483
484err_out:
485 while (i--) {
486 tty_port_destroy(pf->gnss_tty_port[i]);
487 kfree(pf->gnss_tty_port[i]);
488 }
489 kfree(ttydrv_name);
490 tty_driver_kref_put(pf->ice_gnss_tty_driver);
491
492 return NULL;
493}
494
495/**
496 * ice_gnss_init - Initialize GNSS TTY support
497 * @pf: Board private structure
498 */
499void ice_gnss_init(struct ice_pf *pf)
500{
501 struct tty_driver *tty_driver;
502
503 tty_driver = ice_gnss_create_tty_driver(pf);
504 if (!tty_driver)
505 return;
506
507 pf->ice_gnss_tty_driver = tty_driver;
508
509 set_bit(ICE_FLAG_GNSS, pf->flags);
510 dev_info(ice_pf_to_dev(pf), "GNSS TTY init successful\n");
511}
512
513/**
514 * ice_gnss_exit - Disable GNSS TTY support
515 * @pf: Board private structure
516 */
517void ice_gnss_exit(struct ice_pf *pf)
518{
519 unsigned int i;
520
521 if (!test_bit(ICE_FLAG_GNSS, pf->flags) || !pf->ice_gnss_tty_driver)
522 return;
523
524 for (i = 0; i < ICE_GNSS_TTY_MINOR_DEVICES; i++) {
525 if (pf->gnss_tty_port[i]) {
526 tty_port_destroy(pf->gnss_tty_port[i]);
527 kfree(pf->gnss_tty_port[i]);
528 }
529
530 if (pf->gnss_serial[i]) {
531 struct gnss_serial *gnss = pf->gnss_serial[i];
532
533 kthread_cancel_work_sync(&gnss->write_work);
534 kthread_cancel_delayed_work_sync(&gnss->read_work);
535 kfree(gnss);
536 pf->gnss_serial[i] = NULL;
537 }
538 }
539
540 tty_unregister_driver(pf->ice_gnss_tty_driver);
541 kfree(pf->ice_gnss_tty_driver->name);
542 tty_driver_kref_put(pf->ice_gnss_tty_driver);
543 pf->ice_gnss_tty_driver = NULL;
544}
545
546/**
547 * ice_gnss_is_gps_present - Check if GPS HW is present
548 * @hw: pointer to HW struct
549 */
550bool ice_gnss_is_gps_present(struct ice_hw *hw)
551{
552 if (!hw->func_caps.ts_func_info.src_tmr_owned)
553 return false;
554
555#if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
556 if (ice_is_e810t(hw)) {
557 int err;
558 u8 data;
559
560 err = ice_read_pca9575_reg_e810t(hw, ICE_PCA9575_P0_IN, &data);
561 if (err || !!(data & ICE_E810T_P0_GNSS_PRSNT_N))
562 return false;
563 } else {
564 return false;
565 }
566#else
567 if (!ice_is_e810t(hw))
568 return false;
569#endif /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */
570
571 return true;
572}