forked from apache/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfusb301.c
811 lines (661 loc) · 20.3 KB
/
fusb301.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/****************************************************************************
* drivers/usbmisc/fusb301.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <assert.h>
#include <errno.h>
#include <poll.h>
#include <debug.h>
#include <nuttx/fs/fs.h>
#include <nuttx/kmalloc.h>
#include <nuttx/mutex.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/usb/fusb301.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifdef CONFIG_DEBUG_FUSB301
# define fusb301_err(x, ...) _err(x, ##__VA_ARGS__)
# define fusb301_info(x, ...) _info(x, ##__VA_ARGS__)
#else
# define fusb301_err(x, ...) uerr(x, ##__VA_ARGS__)
# define fusb301_info(x, ...) uinfo(x, ##__VA_ARGS__)
#endif
#ifndef CONFIG_FUSB301_I2C_FREQUENCY
# define CONFIG_FUSB301_I2C_FREQUENCY 400000
#endif
/* Other macros */
#define FUSB301_I2C_RETRIES 10
/****************************************************************************
* Private Data Types
****************************************************************************/
struct fusb301_dev_s
{
FAR struct i2c_master_s *i2c; /* I2C interface */
uint8_t addr; /* I2C address */
volatile bool int_pending; /* Interrupt received but handled */
mutex_t devlock; /* Manages exclusive access */
FAR struct fusb301_config_s *config; /* Platform specific configuration */
FAR struct pollfd *fds[CONFIG_FUSB301_NPOLLWAITERS];
};
/****************************************************************************
* Private Function prototypes
****************************************************************************/
static int fusb301_open(FAR struct file *filep);
static int fusb301_close(FAR struct file *filep);
static ssize_t fusb301_read(FAR struct file *, FAR char *, size_t);
static ssize_t fusb301_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen);
static int fusb301_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
static int fusb301_poll(FAR struct file *filep, FAR struct pollfd *fds,
bool setup);
/****************************************************************************
* Private Data
****************************************************************************/
static const struct file_operations g_fusb301ops =
{
fusb301_open, /* open */
fusb301_close, /* close */
fusb301_read, /* read */
fusb301_write, /* write */
NULL, /* seek */
fusb301_ioctl, /* ioctl */
NULL, /* mmap */
NULL, /* truncate */
fusb301_poll /* poll */
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: fusb301_getreg
*
* Description:
* Read from an 8-bit FUSB301 register
*
* Input Parameters:
* priv - pointer to FUSB301 Private Structure
* reg - register to read
*
* Returned Value:
* Returns positive register value in case of success, otherwise ERROR
****************************************************************************/
static int fusb301_getreg(FAR struct fusb301_dev_s *priv, uint8_t reg)
{
int ret = -EIO;
int retries;
uint8_t regval;
struct i2c_msg_s msg[2];
DEBUGASSERT(priv);
msg[0].frequency = CONFIG_FUSB301_I2C_FREQUENCY;
msg[0].addr = priv->addr;
msg[0].flags = 0;
msg[0].buffer = ®
msg[0].length = 1;
msg[1].frequency = CONFIG_FUSB301_I2C_FREQUENCY;
msg[1].addr = priv->addr;
msg[1].flags = I2C_M_READ;
msg[1].buffer = ®val;
msg[1].length = 1;
/* Perform the transfer */
for (retries = 0; retries < FUSB301_I2C_RETRIES; retries++)
{
ret = I2C_TRANSFER(priv->i2c, msg, 2);
if (ret >= 0)
{
fusb301_info("reg:%02X, value:%02X\n", reg, regval);
return regval;
}
else
{
/* Some error. Try to reset I2C bus and keep trying. */
#ifdef CONFIG_I2C_RESET
if (retries == FUSB301_I2C_RETRIES - 1)
{
break;
}
ret = I2C_RESET(priv->i2c);
if (ret < 0)
{
fusb301_err("ERROR: I2C_RESET failed: %d\n", ret);
return ret;
}
#endif
}
}
fusb301_info("reg:%02X, error:%d\n", reg, ret);
return ret;
}
/****************************************************************************
* Name: fusb301_putreg
*
* Description:
* Write a value to an 8-bit FUSB301 register
*
* Input Parameters:
* priv - pointer to FUSB301 Private Structure
* regaddr - register to read
* regval - value to be written
*
* Returned Value:
* None
****************************************************************************/
static int fusb301_putreg(FAR struct fusb301_dev_s *priv, uint8_t regaddr,
uint8_t regval)
{
int ret = -EIO;
int retries;
struct i2c_msg_s msg;
uint8_t txbuffer[2];
/* Setup to the data to be transferred (register address and data). */
txbuffer[0] = regaddr;
txbuffer[1] = regval;
/* Setup 8-bit FUSB301 address write message */
msg.frequency = CONFIG_FUSB301_I2C_FREQUENCY;
msg.addr = priv->addr;
msg.flags = 0;
msg.buffer = txbuffer;
msg.length = 2;
/* Perform the transfer */
for (retries = 0; retries < FUSB301_I2C_RETRIES; retries++)
{
ret = I2C_TRANSFER(priv->i2c, &msg, 1);
if (ret == OK)
{
fusb301_info("reg:%02X, value:%02X\n", regaddr, regval);
return OK;
}
else
{
/* Some error. Try to reset I2C bus and keep trying. */
#ifdef CONFIG_I2C_RESET
if (retries == FUSB301_I2C_RETRIES - 1)
{
break;
}
ret = I2C_RESET(priv->i2c);
if (ret < 0)
{
fusb301_err("ERROR: I2C_RESET failed: %d\n", ret);
return ret;
}
#endif
}
}
fusb301_err("ERROR: failed reg:%02X, value:%02X, error:%d\n",
regaddr, regval, ret);
return ret;
}
/****************************************************************************
* Name: fusb301_read_device_id
*
* Description:
* Read device version and revision IDs
*
****************************************************************************/
static int fusb301_read_device_id(FAR struct fusb301_dev_s * priv,
FAR uint8_t * arg)
{
int ret;
ret = fusb301_getreg(priv, FUSB301_DEV_ID_REG);
if (ret < 0)
{
fusb301_err("ERROR: Failed to read device ID\n");
return -EIO;
}
*arg = ret;
return OK;
}
/****************************************************************************
* Name: fusb301_clear_interrupts
*
* Description:
* Clear interrupts from FUSB301 chip
*
****************************************************************************/
static int fusb301_clear_interrupts(FAR struct fusb301_dev_s *priv)
{
int ret = OK;
ret = fusb301_getreg(priv, FUSB301_INTERRUPT_REG);
if (ret < 0)
{
fusb301_err("ERROR: Failed to clear interrupts\n");
return -EIO;
}
return ret;
}
/****************************************************************************
* Name: fusb301_setup
*
* Description:
* Setup FUSB301 chip
*
****************************************************************************/
static int fusb301_setup(FAR struct fusb301_dev_s *priv,
struct fusb301_setup_s *setup)
{
int ret = OK;
fusb301_info("drp_tgl:%02X, host_curr:%02X, global_int:%X, mask:%02X\n",
setup->drp_toggle_timing, setup->host_current, setup->global_int_mask,
setup->int_mask);
ret = fusb301_putreg(priv, FUSB301_CONTROL_REG, setup->drp_toggle_timing |
setup->host_current | setup->global_int_mask);
if (ret < 0)
{
fusb301_err("ERROR: Failed to write control register\n");
goto err_out;
}
ret = fusb301_putreg(priv, FUSB301_MASK_REG, setup->int_mask);
if (ret < 0)
{
fusb301_err("ERROR: Failed to write mask register\n");
}
err_out:
return ret;
}
/****************************************************************************
* Name: fusb301_set_mode
*
* Description:
* Configure supported device modes (sink, source, DRP, accessory)
*
****************************************************************************/
static int fusb301_set_mode(FAR struct fusb301_dev_s *priv,
enum fusb301_mode_e mode)
{
int ret = OK;
if (mode > MODE_DRP_ACC)
{
return -EINVAL;
}
ret = fusb301_putreg(priv, FUSB301_MODE_REG, mode);
if (ret < 0)
{
fusb301_err("ERROR: Failed to write mode register\n");
ret = -EIO;
}
return ret;
}
/****************************************************************************
* Name: fusb301_set_state
*
* Description:
* Force device in specified state
*
****************************************************************************/
static int fusb301_set_state(FAR struct fusb301_dev_s *priv,
enum fusb301_manual_e state)
{
int ret = OK;
if (state > MANUAL_UNATT_SNK)
{
return -EINVAL;
}
ret = fusb301_putreg(priv, FUSB301_MANUAL_REG, state);
if (ret < 0)
{
fusb301_err("ERROR: Failed to write manual register\n");
ret = -EIO;
}
return ret;
}
/****************************************************************************
* Name: fusb301_read_status
*
* Description:
* Read status register
*
****************************************************************************/
static int fusb301_read_status(FAR struct fusb301_dev_s *priv,
FAR uint8_t *arg)
{
int ret;
ret = fusb301_getreg(priv, FUSB301_STATUS_REG);
if (ret < 0)
{
fusb301_err("ERROR: Failed to read status\n");
return -EIO;
}
*arg = ret;
return OK;
}
/****************************************************************************
* Name: fusb301_read_devtype
*
* Description:
* Read type of attached device
*
****************************************************************************/
static int fusb301_read_devtype(FAR struct fusb301_dev_s *priv,
FAR uint8_t *arg)
{
int ret;
ret = fusb301_getreg(priv, FUSB301_TYPE_REG);
if (ret < 0)
{
fusb301_err("ERROR: Failed to read type\n");
return -EIO;
}
*arg = ret;
return OK;
}
/****************************************************************************
* Name: fusb301_reset
*
* Description:
* Reset FUSB301 HW and clear I2C registers
*
****************************************************************************/
static int fusb301_reset(FAR struct fusb301_dev_s *priv)
{
int ret = OK;
ret = fusb301_putreg(priv, FUSB301_RESET_REG, RESET_SW_RES);
if (ret < 0)
{
fusb301_err("ERROR: Failed to write reset register\n");
ret = -EIO;
}
return ret;
}
/****************************************************************************
* Name: fusb301_open
*
* Description:
* This function is called whenever the FUSB301 device is opened.
*
****************************************************************************/
static int fusb301_open(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct fusb301_dev_s *priv = inode->i_private;
int ret = OK;
/* Probe device */
ret = fusb301_getreg(priv, FUSB301_DEV_ID_REG);
if (ret < 0)
{
fusb301_err("ERROR: No response at given address 0x%02X\n",
priv->addr);
ret = -EFAULT;
}
else
{
fusb301_info("device id: 0x%02X\n", ret);
fusb301_clear_interrupts(priv);
priv->config->irq_enable(priv->config, true);
}
/* Error exit */
return ret;
}
/****************************************************************************
* Name: fusb301_close
*
* Description:
* This routine is called when the FUSB301 device is closed.
*
****************************************************************************/
static int fusb301_close(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct fusb301_dev_s *priv = inode->i_private;
priv->config->irq_enable(priv->config, false);
return OK;
}
/****************************************************************************
* Name: fusb301_read
* Description:
* This routine is called when the FUSB301 device is read.
****************************************************************************/
static ssize_t fusb301_read(FAR struct file *filep,
FAR char *buffer,
size_t buflen)
{
FAR struct inode *inode = filep->f_inode;
FAR struct fusb301_dev_s *priv = inode->i_private;
FAR struct fusb301_result_s *ptr;
irqstate_t flags;
int ret;
if (buflen < sizeof(struct fusb301_result_s))
{
return 0;
}
ptr = (struct fusb301_result_s *)buffer;
ret = nxmutex_lock(&priv->devlock);
if (ret < 0)
{
return ret;
}
flags = enter_critical_section();
priv->int_pending = false;
leave_critical_section(flags);
fusb301_clear_interrupts(priv);
ptr->status = fusb301_getreg(priv, FUSB301_STATUS_REG);
ptr->dev_type = fusb301_getreg(priv, FUSB301_TYPE_REG);
nxmutex_unlock(&priv->devlock);
return sizeof(struct fusb301_result_s);
}
/****************************************************************************
* Name: fusb301_write
* Description:
* This routine is called when the FUSB301 device is written to.
****************************************************************************/
static ssize_t fusb301_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen)
{
ssize_t length = 0;
return length;
}
/****************************************************************************
* Name: fusb301_ioctl
* Description:
* This routine is called when ioctl function call is performed for
* the FUSB301 device.
****************************************************************************/
static int fusb301_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
FAR struct inode *inode = filep->f_inode;
FAR struct fusb301_dev_s *priv = inode->i_private;
int ret;
ret = nxmutex_lock(&priv->devlock);
if (ret < 0)
{
return ret;
}
fusb301_info("cmd: 0x%02X, arg:%lu\n", cmd, arg);
switch (cmd)
{
case USBCIOC_READ_DEVID:
{
ret = fusb301_read_device_id(priv, (FAR uint8_t *)arg);
}
break;
case USBCIOC_SETUP:
{
ret = fusb301_setup(priv, (struct fusb301_setup_s *)arg);
}
break;
case USBCIOC_SET_MODE:
{
ret = fusb301_set_mode(priv, (uint8_t)arg);
}
break;
case USBCIOC_SET_STATE:
{
ret = fusb301_set_state(priv, (uint8_t)arg);
}
break;
case USBCIOC_READ_STATUS:
{
ret = fusb301_read_status(priv, (FAR uint8_t *)arg);
}
break;
case USBCIOC_READ_DEVTYPE:
{
ret = fusb301_read_devtype(priv, (FAR uint8_t *)arg);
}
break;
case USBCIOC_RESET:
{
ret = fusb301_reset(priv);
}
break;
default:
{
fusb301_err("ERROR: Unrecognized cmd: %d\n", cmd);
ret = -ENOTTY;
}
break;
}
nxmutex_unlock(&priv->devlock);
return ret;
}
/****************************************************************************
* Name: fusb301_poll
* Description:
* This routine is called during FUSB301 device poll
****************************************************************************/
static int fusb301_poll(FAR struct file *filep,
FAR struct pollfd *fds,
bool setup)
{
FAR struct inode *inode;
FAR struct fusb301_dev_s *priv;
irqstate_t flags;
int ret = OK;
int i;
DEBUGASSERT(fds);
inode = filep->f_inode;
DEBUGASSERT(inode->i_private);
priv = inode->i_private;
ret = nxmutex_lock(&priv->devlock);
if (ret < 0)
{
return ret;
}
if (setup)
{
/* Ignore waits that do not include POLLIN */
if ((fds->events & POLLIN) == 0)
{
ret = -EDEADLK;
goto out;
}
/* This is a request to set up the poll. Find an available
* slot for the poll structure reference.
*/
for (i = 0; i < CONFIG_FUSB301_NPOLLWAITERS; i++)
{
/* Find an available slot */
if (!priv->fds[i])
{
/* Bind the poll structure and this slot */
priv->fds[i] = fds;
fds->priv = &priv->fds[i];
break;
}
}
if (i >= CONFIG_FUSB301_NPOLLWAITERS)
{
fds->priv = NULL;
ret = -EBUSY;
goto out;
}
flags = enter_critical_section();
if (priv->int_pending)
{
poll_notify(&fds, 1, POLLIN);
}
leave_critical_section(flags);
}
else if (fds->priv)
{
/* This is a request to tear down the poll. */
FAR struct pollfd **slot = (FAR struct pollfd **)fds->priv;
DEBUGASSERT(slot != NULL);
/* Remove all memory of the poll setup */
*slot = NULL;
fds->priv = NULL;
}
out:
nxmutex_unlock(&priv->devlock);
return ret;
}
/****************************************************************************
* Name: fusb301_callback
*
* Description:
* FUSB301 interrupt handler
*
****************************************************************************/
static int fusb301_int_handler(int irq, FAR void *context, FAR void *arg)
{
FAR struct fusb301_dev_s *priv = (FAR struct fusb301_dev_s *)arg;
irqstate_t flags;
DEBUGASSERT(priv != NULL);
flags = enter_critical_section();
priv->int_pending = true;
poll_notify(priv->fds, CONFIG_FUSB301_NPOLLWAITERS, POLLIN);
leave_critical_section(flags);
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
int fusb301_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
uint8_t addr, FAR struct fusb301_config_s *config)
{
FAR struct fusb301_dev_s *priv;
int ret;
DEBUGASSERT(devpath != NULL && i2c != NULL && config != NULL);
/* Initialize the FUSB301 device structure */
priv = (FAR struct fusb301_dev_s *)
kmm_zalloc(sizeof(struct fusb301_dev_s));
if (!priv)
{
fusb301_err("ERROR: Failed to allocate instance\n");
return -ENOMEM;
}
/* Initialize device structure mutex */
nxmutex_init(&priv->devlock);
priv->int_pending = false;
priv->i2c = i2c;
priv->addr = addr;
priv->config = config;
/* Register the character driver */
ret = register_driver(devpath, &g_fusb301ops, 0666, priv);
if (ret < 0)
{
fusb301_err("ERROR: Failed to register driver: %d\n", ret);
goto errout_with_priv;
}
/* Prepare interrupt line and handler. */
if (priv->config->irq_clear)
{
priv->config->irq_clear(config);
}
priv->config->irq_attach(config, fusb301_int_handler, priv);
priv->config->irq_enable(config, false);
return OK;
errout_with_priv:
nxmutex_destroy(&priv->devlock);
kmm_free(priv);
return ret;
}