2 * i2c - generic driver for Inter-Integrated Circuit bus (I2C).
6 #include <minix/chardriver.h>
7 #include <minix/drivers.h>
10 #include <minix/log.h>
11 #include <minix/type.h>
12 #include <minix/board.h>
22 /* SoC specific headers - 1 for each SoC */
25 /* local definitions */
27 /* i2c slave addresses can be up to 10 bits */
28 #define NR_I2CDEV (0x3ff)
30 /* local function prototypes */
31 static int do_reserve(endpoint_t endpt
, int slave_addr
);
32 static int check_reservation(endpoint_t endpt
, int slave_addr
);
33 static void update_reservation(endpoint_t endpt
, char *key
);
34 static void ds_event(void);
36 static int validate_ioctl_exec(minix_i2c_ioctl_exec_t
* ioctl_exec
);
37 static int do_i2c_ioctl_exec(endpoint_t caller
, cp_grant_id_t grant_nr
);
39 static int env_parse_instance(void);
41 /* libchardriver callbacks */
42 static int i2c_ioctl(devminor_t minor
, unsigned long request
, endpoint_t endpt
,
43 cp_grant_id_t grant
, int flags
, endpoint_t user_endpt
, cdev_id_t id
);
44 static void i2c_other(message
* m
, int ipc_status
);
48 /* the bus that this instance of the driver is responsible for */
51 /* Table of i2c device reservations. */
56 char key
[DS_MAX_KEYLEN
];
59 /* Process a request for an i2c operation.
60 * This is the interface that all hardware specific code must implement.
62 int (*process
) (minix_i2c_ioctl_exec_t
* ioctl_exec
);
64 /* logging - use with log_warn(), log_info(), log_debug(), log_trace() */
65 static struct log log
= {
67 .log_level
= LEVEL_INFO
,
68 .log_func
= default_log
71 /* Entry points to the i2c driver from libchardriver.
72 * Only i2c_ioctl() and i2c_other() are implemented. The rest are no-op.
74 static struct chardriver i2c_tab
= {
75 .cdr_ioctl
= i2c_ioctl
,
76 .cdr_other
= i2c_other
80 sef_cb_lu_state_save(int UNUSED(result
), int UNUSED(flags
))
83 char key
[DS_MAX_KEYLEN
];
85 memset(key
, '\0', DS_MAX_KEYLEN
);
86 snprintf(key
, DS_MAX_KEYLEN
, "i2c.%d.i2cdev", i2c_bus_id
+ 1);
87 r
= ds_publish_mem(key
, i2cdev
, sizeof(i2cdev
), DSF_OVERWRITE
);
89 log_warn(&log
, "ds_publish_mem(%s) failed (r=%d)\n", key
, r
);
93 log_debug(&log
, "State Saved\n");
99 * Claim an unclaimed device for exclusive use by endpt. This function can
100 * also be used to update the endpt if the endpt's label matches the label
101 * already associated with the slave address. This is useful if a driver
102 * shuts down unexpectedly and starts up with a new endpt and wants to reserve
103 * the same device it reserved before.
106 do_reserve(endpoint_t endpt
, int slave_addr
)
109 char key
[DS_MAX_KEYLEN
];
110 char label
[DS_MAX_KEYLEN
];
112 /* find the label for the endpoint */
113 r
= ds_retrieve_label_name(label
, endpt
);
115 log_warn(&log
, "Couldn't find label for endpt='0x%x'\n",
120 /* construct the key i2cdriver_announce published (saves an IPC call) */
121 snprintf(key
, DS_MAX_KEYLEN
, "drv.i2c.%d.%s", i2c_bus_id
+ 1, label
);
123 if (slave_addr
< 0 || slave_addr
>= NR_I2CDEV
) {
125 "slave address must be positive & no more than 10 bits\n");
129 /* check if device is in use by another driver */
130 if (i2cdev
[slave_addr
].inuse
!= 0
131 && strncmp(i2cdev
[slave_addr
].key
, key
, DS_MAX_KEYLEN
) != 0) {
132 log_debug(&log
, "address in use by '%s'/0x%x\n",
133 i2cdev
[slave_addr
].key
, i2cdev
[slave_addr
].endpt
);
137 /* device is free or already owned by us, claim it */
138 i2cdev
[slave_addr
].inuse
= 1;
139 i2cdev
[slave_addr
].endpt
= endpt
;
140 memcpy(i2cdev
[slave_addr
].key
, key
, DS_MAX_KEYLEN
);
142 sef_cb_lu_state_save(0, 0); /* save reservations */
144 log_debug(&log
, "Device 0x%x claimed by 0x%x key='%s'\n",
145 slave_addr
, endpt
, key
);
151 * All drivers must reserve their device(s) before doing operations on them
152 * (read/write, etc). ioctl()'s from VFS (i.e. user programs) can only use
153 * devices that haven't been reserved. A driver isn't allowed to access a
154 * device that another driver has reserved (not even other instances of the
158 check_reservation(endpoint_t endpt
, int slave_addr
)
160 if (slave_addr
< 0 || slave_addr
>= NR_I2CDEV
) {
162 "slave address must be positive & no more than 10 bits\n");
166 if (endpt
== VFS_PROC_NR
&& i2cdev
[slave_addr
].inuse
== 0) {
168 "allowing ioctl() from VFS to access unclaimed device\n");
172 if (i2cdev
[slave_addr
].inuse
&& i2cdev
[slave_addr
].endpt
!= endpt
) {
173 log_debug(&log
, "device reserved by another endpoint\n");
175 } else if (i2cdev
[slave_addr
].inuse
== 0) {
177 "all drivers sending messages directly to this driver must reserve\n");
180 log_debug(&log
, "allowing access to registered device\n");
186 * i2c listens to updates from ds about i2c device drivers starting up.
187 * When a driver comes back up with the same label, the endpt associated
188 * with the reservation needs to be updated. This function does the updating.
191 update_reservation(endpoint_t endpt
, char *key
)
195 log_debug(&log
, "Updating reservation for '%s' endpt=0x%x\n", key
,
198 for (i
= 0; i
< NR_I2CDEV
; i
++) {
200 /* find devices in use that the driver owns */
201 if (i2cdev
[i
].inuse
!= 0
202 && strncmp(i2cdev
[i
].key
, key
, DS_MAX_KEYLEN
) == 0) {
203 /* update reservation with new endpoint */
204 do_reserve(endpt
, i
);
205 log_debug(&log
, "Found device to update 0x%x\n", i
);
211 * Checks a minix_i2c_ioctl_exec_t to see if the fields make sense.
214 validate_ioctl_exec(minix_i2c_ioctl_exec_t
* ioctl_exec
)
220 op
= ioctl_exec
->iie_op
;
221 if (op
!= I2C_OP_READ
&&
222 op
!= I2C_OP_READ_WITH_STOP
&&
223 op
!= I2C_OP_WRITE
&&
224 op
!= I2C_OP_WRITE_WITH_STOP
&&
225 op
!= I2C_OP_READ_BLOCK
&& op
!= I2C_OP_WRITE_BLOCK
) {
226 log_warn(&log
, "iie_op value not valid\n");
230 addr
= ioctl_exec
->iie_addr
;
231 if (addr
< 0 || addr
>= NR_I2CDEV
) {
232 log_warn(&log
, "iie_addr out of range 0x0-0x%x\n", NR_I2CDEV
);
236 len
= ioctl_exec
->iie_cmdlen
;
237 if (len
> I2C_EXEC_MAX_CMDLEN
) {
239 "iie_cmdlen out of range 0-I2C_EXEC_MAX_CMDLEN\n");
243 len
= ioctl_exec
->iie_buflen
;
244 if (len
> I2C_EXEC_MAX_BUFLEN
) {
246 "iie_buflen out of range 0-I2C_EXEC_MAX_BUFLEN\n");
254 * Performs the action in minix_i2c_ioctl_exec_t.
257 do_i2c_ioctl_exec(endpoint_t caller
, cp_grant_id_t grant_nr
)
260 minix_i2c_ioctl_exec_t ioctl_exec
;
262 /* Copy the requested exection into the driver */
263 r
= sys_safecopyfrom(caller
, grant_nr
, (vir_bytes
) 0,
264 (vir_bytes
) & ioctl_exec
, sizeof(ioctl_exec
));
266 log_warn(&log
, "sys_safecopyfrom() failed\n");
270 /* input validation */
271 r
= validate_ioctl_exec(&ioctl_exec
);
273 log_debug(&log
, "Message validation failed\n");
277 /* permission check */
278 r
= check_reservation(caller
, ioctl_exec
.iie_addr
);
280 log_debug(&log
, "check_reservation() denied the request\n");
284 /* Call the device specific code to execute the action */
285 r
= process(&ioctl_exec
);
287 log_debug(&log
, "process() failed\n");
291 /* Copy the results of the execution back to the calling process */
292 r
= sys_safecopyto(caller
, grant_nr
, (vir_bytes
) 0,
293 (vir_bytes
) & ioctl_exec
, sizeof(ioctl_exec
));
295 log_warn(&log
, "sys_safecopyto() failed\n");
303 i2c_ioctl(devminor_t
UNUSED(minor
), unsigned long request
, endpoint_t endpt
,
304 cp_grant_id_t grant
, int UNUSED(flags
), endpoint_t
UNUSED(user_endpt
),
305 cdev_id_t
UNUSED(id
))
310 case MINIX_I2C_IOCTL_EXEC
:
311 r
= do_i2c_ioctl_exec(endpt
, grant
);
314 log_warn(&log
, "Invalid ioctl() 0x%x\n", request
);
323 i2c_other(message
* m
, int ipc_status
)
328 if (is_ipc_notify(ipc_status
)) {
329 /* handle notifications about drivers changing state */
330 if (m
->m_source
== DS_PROC_NR
) {
337 case BUSC_I2C_RESERVE
:
338 /* reserve a device on the bus for exclusive access */
339 r
= do_reserve(m
->m_source
, m
->m_li2cdriver_i2c_busc_i2c_reserve
.addr
);
342 /* handle request from another driver */
343 r
= do_i2c_ioctl_exec(m
->m_source
, m
->m_li2cdriver_i2c_busc_i2c_exec
.grant
);
346 log_warn(&log
, "Invalid message type (0x%x)\n", m
->m_type
);
351 log_trace(&log
, "i2c_other() returning r=%d\n", r
);
354 memset(&m_reply
, 0, sizeof(m_reply
));
357 if ((r
= ipc_send(m
->m_source
, &m_reply
)) != OK
)
358 log_warn(&log
, "ipc_send() to %d failed: %d\n", m
->m_source
, r
);
362 * The bus drivers are subscribed to DS events about device drivers on their
363 * bus. When the device drivers restart, DS sends a notification and this
364 * function updates the reservation table with the device driver's new
370 char key
[DS_MAX_KEYLEN
];
373 endpoint_t owner_endpoint
;
376 /* check for pending events */
377 while ((r
= ds_check(key
, &type
, &owner_endpoint
)) == OK
) {
379 r
= ds_retrieve_u32(key
, &value
);
381 log_warn(&log
, "ds_retrieve_u32() failed r=%d\n", r
);
385 log_debug(&log
, "key='%s' owner_endpoint=0x%x\n", key
,
388 if (value
== DS_DRIVER_UP
) {
389 /* clean up any old reservations the driver had */
390 log_debug(&log
, "DS_DRIVER_UP\n");
391 update_reservation(owner_endpoint
, key
);
397 lu_state_restore(void)
400 char key
[DS_MAX_KEYLEN
];
403 env_parse_instance();
405 size
= sizeof(i2cdev
);
407 memset(key
, '\0', DS_MAX_KEYLEN
);
408 snprintf(key
, DS_MAX_KEYLEN
, "i2c.%d.i2cdev", i2c_bus_id
+ 1);
410 r
= ds_retrieve_mem(key
, (char *) i2cdev
, &size
);
412 log_warn(&log
, "ds_retrieve_mem(%s) failed (r=%d)\n", key
, r
);
416 log_debug(&log
, "State Restored\n");
422 sef_cb_init(int type
, sef_init_info_t
* UNUSED(info
))
425 char regex
[DS_MAX_KEYLEN
];
426 struct machine machine
;
427 sys_getmachine(&machine
);
429 if (type
!= SEF_INIT_FRESH
) {
430 /* Restore a prior state. */
434 if (BOARD_IS_BBXM(machine
.board_id
) || BOARD_IS_BB(machine
.board_id
)){
435 /* Set callback and initialize the bus */
436 r
= omap_interface_setup(&process
, i2c_bus_id
);
444 /* Announce we are up when necessary. */
445 if (type
!= SEF_INIT_LU
) {
447 /* only capture events for this particular bus */
448 snprintf(regex
, DS_MAX_KEYLEN
, "drv\\.i2c\\.%d\\..*",
451 /* Subscribe to driver events for i2c drivers */
452 r
= ds_subscribe(regex
, DSF_INITIAL
| DSF_OVERWRITE
);
454 log_warn(&log
, "ds_subscribe() failed\n");
458 chardriver_announce();
462 sef_cb_lu_state_save(0, 0);
464 /* Initialization completed successfully. */
471 /* Register init callbacks. */
472 sef_setcb_init_fresh(sef_cb_init
);
473 sef_setcb_init_lu(sef_cb_init
);
474 sef_setcb_init_restart(sef_cb_init
);
476 /* Register live update callbacks */
477 sef_setcb_lu_state_save(sef_cb_lu_state_save
);
479 /* Let SEF perform startup. */
484 env_parse_instance(void)
489 /* Parse the instance number passed to service */
491 r
= env_parse("instance", "d", 0, &instance
, 1, 3);
494 "Expecting '-arg instance=N' argument (N=1..3)\n");
498 /* Device files count from 1, hardware starts counting from 0 */
499 i2c_bus_id
= instance
- 1;
505 main(int argc
, char *argv
[])
509 env_setargs(argc
, argv
);
511 r
= env_parse_instance();
516 memset(i2cdev
, '\0', sizeof(i2cdev
));
518 chardriver_task(&i2c_tab
);