summaryrefslogtreecommitdiff
path: root/minix/drivers/bus/i2c/i2c.c
blob: 3714d8a421dc1e6ce190376c5954c9a067a280fe (plain)
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
/*
 * i2c - generic driver for Inter-Integrated Circuit bus (I2C).
 */

/* kernel headers */
#include <minix/chardriver.h>
#include <minix/drivers.h>
#include <minix/ds.h>
#include <minix/i2c.h>
#include <minix/log.h>
#include <minix/type.h>
#include <minix/board.h>

/* system headers */
#include <sys/mman.h>

/* usr headers */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

/* SoC specific headers - 1 for each SoC */
#include "omap_i2c.h"

/* local definitions */

/* i2c slave addresses can be up to 10 bits */
#define NR_I2CDEV (0x3ff)

/* local function prototypes */
static int do_reserve(endpoint_t endpt, int slave_addr);
static int check_reservation(endpoint_t endpt, int slave_addr);
static void update_reservation(endpoint_t endpt, char *key);
static void ds_event(void);

static int validate_ioctl_exec(minix_i2c_ioctl_exec_t * ioctl_exec);
static int do_i2c_ioctl_exec(endpoint_t caller, cp_grant_id_t grant_nr);

static int env_parse_instance(void);

/* libchardriver callbacks */
static int i2c_ioctl(devminor_t minor, unsigned long request, endpoint_t endpt,
	cp_grant_id_t grant, int flags, endpoint_t user_endpt, cdev_id_t id);
static void i2c_other(message * m, int ipc_status);

/* Globals  */

/* the bus that this instance of the driver is responsible for */
uint32_t i2c_bus_id;

/* Table of i2c device reservations. */
static struct i2cdev
{
	uint8_t inuse;
	endpoint_t endpt;
	char key[DS_MAX_KEYLEN];
} i2cdev[NR_I2CDEV];

/* Process a request for an i2c operation.
 * This is the interface that all hardware specific code must implement.
 */
int (*process) (minix_i2c_ioctl_exec_t * ioctl_exec);

/* logging - use with log_warn(), log_info(), log_debug(), log_trace() */
static struct log log = {
	.name = "i2c",
	.log_level = LEVEL_INFO,
	.log_func = default_log
};

/* Entry points to the i2c driver from libchardriver.
 * Only i2c_ioctl() and i2c_other() are implemented. The rest are no-op.
 */
static struct chardriver i2c_tab = {
	.cdr_ioctl	= i2c_ioctl,
	.cdr_other	= i2c_other
};

static int
sef_cb_lu_state_save(int UNUSED(result), int UNUSED(flags))
{
	int r;
	char key[DS_MAX_KEYLEN];

	memset(key, '\0', DS_MAX_KEYLEN);
	snprintf(key, DS_MAX_KEYLEN, "i2c.%d.i2cdev", i2c_bus_id + 1);
	r = ds_publish_mem(key, i2cdev, sizeof(i2cdev), DSF_OVERWRITE);
	if (r != OK) {
		log_warn(&log, "ds_publish_mem(%s) failed (r=%d)\n", key, r);
		return r;
	}

	log_debug(&log, "State Saved\n");

	return OK;
}

/*
 * Claim an unclaimed device for exclusive use by endpt. This function can
 * also be used to update the endpt if the endpt's label matches the label
 * already associated with the slave address. This is useful if a driver
 * shuts down unexpectedly and starts up with a new endpt and wants to reserve
 * the same device it reserved before.
 */
static int
do_reserve(endpoint_t endpt, int slave_addr)
{
	int r;
	char key[DS_MAX_KEYLEN];
	char label[DS_MAX_KEYLEN];

	/* find the label for the endpoint */
	r = ds_retrieve_label_name(label, endpt);
	if (r != OK) {
		log_warn(&log, "Couldn't find label for endpt='0x%x'\n",
		    endpt);
		return r;
	}

	/* construct the key i2cdriver_announce published (saves an IPC call) */
	snprintf(key, DS_MAX_KEYLEN, "drv.i2c.%d.%s", i2c_bus_id + 1, label);

	if (slave_addr < 0 || slave_addr >= NR_I2CDEV) {
		log_debug(&log,
		    "slave address must be positive & no more than 10 bits\n");
		return EINVAL;
	}

	/* check if device is in use by another driver */
	if (i2cdev[slave_addr].inuse != 0
	    && strncmp(i2cdev[slave_addr].key, key, DS_MAX_KEYLEN) != 0) {
		log_debug(&log, "address in use by '%s'/0x%x\n",
		    i2cdev[slave_addr].key, i2cdev[slave_addr].endpt);
		return EBUSY;
	}

	/* device is free or already owned by us, claim it */
	i2cdev[slave_addr].inuse = 1;
	i2cdev[slave_addr].endpt = endpt;
	memcpy(i2cdev[slave_addr].key, key, DS_MAX_KEYLEN);

	sef_cb_lu_state_save(0, 0);	/* save reservations */

	log_debug(&log, "Device 0x%x claimed by 0x%x key='%s'\n",
	    slave_addr, endpt, key);

	return OK;
}

/*
 * All drivers must reserve their device(s) before doing operations on them
 * (read/write, etc). ioctl()'s from VFS (i.e. user programs) can only use
 * devices that haven't been reserved. A driver isn't allowed to access a
 * device that another driver has reserved (not even other instances of the
 * same driver).
 */
static int
check_reservation(endpoint_t endpt, int slave_addr)
{
	if (slave_addr < 0 || slave_addr >= NR_I2CDEV) {
		log_debug(&log,
		    "slave address must be positive & no more than 10 bits\n");
		return EINVAL;
	}

	if (endpt == VFS_PROC_NR && i2cdev[slave_addr].inuse == 0) {
		log_debug(&log,
		    "allowing ioctl() from VFS to access unclaimed device\n");
		return OK;
	}

	if (i2cdev[slave_addr].inuse && i2cdev[slave_addr].endpt != endpt) {
		log_debug(&log, "device reserved by another endpoint\n");
		return EBUSY;
	} else if (i2cdev[slave_addr].inuse == 0) {
		log_debug(&log,
		    "all drivers sending messages directly to this driver must reserve\n");
		return EPERM;
	} else {
		log_debug(&log, "allowing access to registered device\n");
		return OK;
	}
}

/*
 * i2c listens to updates from ds about i2c device drivers starting up.
 * When a driver comes back up with the same label, the endpt associated
 * with the reservation needs to be updated. This function does the updating.
 */
static void
update_reservation(endpoint_t endpt, char *key)
{
	int i;

	log_debug(&log, "Updating reservation for '%s' endpt=0x%x\n", key,
	    endpt);

	for (i = 0; i < NR_I2CDEV; i++) {

		/* find devices in use that the driver owns */
		if (i2cdev[i].inuse != 0
		    && strncmp(i2cdev[i].key, key, DS_MAX_KEYLEN) == 0) {
			/* update reservation with new endpoint */
			do_reserve(endpt, i);
			log_debug(&log, "Found device to update 0x%x\n", i);
		}
	}
}

/*
 * Checks a minix_i2c_ioctl_exec_t to see if the fields make sense.
 */
static int
validate_ioctl_exec(minix_i2c_ioctl_exec_t * ioctl_exec)
{
	i2c_op_t op;
	i2c_addr_t addr;
	size_t len;

	op = ioctl_exec->iie_op;
	if (op != I2C_OP_READ &&
	    op != I2C_OP_READ_WITH_STOP &&
	    op != I2C_OP_WRITE &&
	    op != I2C_OP_WRITE_WITH_STOP &&
	    op != I2C_OP_READ_BLOCK && op != I2C_OP_WRITE_BLOCK) {
		log_warn(&log, "iie_op value not valid\n");
		return EINVAL;
	}

	addr = ioctl_exec->iie_addr;
	if (addr < 0 || addr >= NR_I2CDEV) {
		log_warn(&log, "iie_addr out of range 0x0-0x%x\n", NR_I2CDEV);
		return EINVAL;
	}

	len = ioctl_exec->iie_cmdlen;
	if (len > I2C_EXEC_MAX_CMDLEN) {
		log_warn(&log,
		    "iie_cmdlen out of range 0-I2C_EXEC_MAX_CMDLEN\n");
		return EINVAL;
	}

	len = ioctl_exec->iie_buflen;
	if (len > I2C_EXEC_MAX_BUFLEN) {
		log_warn(&log,
		    "iie_buflen out of range 0-I2C_EXEC_MAX_BUFLEN\n");
		return EINVAL;
	}

	return OK;
}

/*
 * Performs the action in minix_i2c_ioctl_exec_t.
 */
static int
do_i2c_ioctl_exec(endpoint_t caller, cp_grant_id_t grant_nr)
{
	int r;
	minix_i2c_ioctl_exec_t ioctl_exec;

	/* Copy the requested exection into the driver */
	r = sys_safecopyfrom(caller, grant_nr, (vir_bytes) 0,
	    (vir_bytes) & ioctl_exec, sizeof(ioctl_exec));
	if (r != OK) {
		log_warn(&log, "sys_safecopyfrom() failed\n");
		return r;
	}

	/* input validation */
	r = validate_ioctl_exec(&ioctl_exec);
	if (r != OK) {
		log_debug(&log, "Message validation failed\n");
		return r;
	}

	/* permission check */
	r = check_reservation(caller, ioctl_exec.iie_addr);
	if (r != OK) {
		log_debug(&log, "check_reservation() denied the request\n");
		return r;
	}

	/* Call the device specific code to execute the action */
	r = process(&ioctl_exec);
	if (r != OK) {
		log_debug(&log, "process() failed\n");
		return r;
	}

	/* Copy the results of the execution back to the calling process */
	r = sys_safecopyto(caller, grant_nr, (vir_bytes) 0,
	    (vir_bytes) & ioctl_exec, sizeof(ioctl_exec));
	if (r != OK) {
		log_warn(&log, "sys_safecopyto() failed\n");
		return r;
	}

	return OK;
}

static int
i2c_ioctl(devminor_t UNUSED(minor), unsigned long request, endpoint_t endpt,
	cp_grant_id_t grant, int UNUSED(flags), endpoint_t UNUSED(user_endpt),
	cdev_id_t UNUSED(id))
{
	int r;

	switch (request) {
	case MINIX_I2C_IOCTL_EXEC:
		r = do_i2c_ioctl_exec(endpt, grant);
		break;
	default:
		log_warn(&log, "Invalid ioctl() 0x%x\n", request);
		r = ENOTTY;
		break;
	}

	return r;
}

static void
i2c_other(message * m, int ipc_status)
{
	message m_reply;
	int r;

	if (is_ipc_notify(ipc_status)) {
		/* handle notifications about drivers changing state */
		if (m->m_source == DS_PROC_NR) {
			ds_event();
		}
		return;
	}

	switch (m->m_type) {
	case BUSC_I2C_RESERVE:
		/* reserve a device on the bus for exclusive access */
		r = do_reserve(m->m_source, m->m_li2cdriver_i2c_busc_i2c_reserve.addr);
		break;
	case BUSC_I2C_EXEC:
		/* handle request from another driver */
		r = do_i2c_ioctl_exec(m->m_source, m->m_li2cdriver_i2c_busc_i2c_exec.grant);
		break;
	default:
		log_warn(&log, "Invalid message type (0x%x)\n", m->m_type);
		r = EINVAL;
		break;
	}

	log_trace(&log, "i2c_other() returning r=%d\n", r);

	/* Send a reply. */
	memset(&m_reply, 0, sizeof(m_reply));
	m_reply.m_type = r;

	if ((r = ipc_send(m->m_source, &m_reply)) != OK)
		log_warn(&log, "ipc_send() to %d failed: %d\n", m->m_source, r);
}

/*
 * The bus drivers are subscribed to DS events about device drivers on their
 * bus. When the device drivers restart, DS sends a notification and this
 * function updates the reservation table with the device driver's new
 * endpoint.
 */
static void
ds_event(void)
{
	char key[DS_MAX_KEYLEN];
	u32_t value;
	int type;
	endpoint_t owner_endpoint;
	int r;

	/* check for pending events */
	while ((r = ds_check(key, &type, &owner_endpoint)) == OK) {

		r = ds_retrieve_u32(key, &value);
		if (r != OK) {
			log_warn(&log, "ds_retrieve_u32() failed r=%d\n", r);
			return;
		}

		log_debug(&log, "key='%s' owner_endpoint=0x%x\n", key,
		    owner_endpoint);

		if (value == DS_DRIVER_UP) {
			/* clean up any old reservations the driver had */
			log_debug(&log, "DS_DRIVER_UP\n");
			update_reservation(owner_endpoint, key);
		}
	}
}

static int
lu_state_restore(void)
{
	int r;
	char key[DS_MAX_KEYLEN];
	size_t size;

	env_parse_instance();

	size = sizeof(i2cdev);

	memset(key, '\0', DS_MAX_KEYLEN);
	snprintf(key, DS_MAX_KEYLEN, "i2c.%d.i2cdev", i2c_bus_id + 1);

	r = ds_retrieve_mem(key, (char *) i2cdev, &size);
	if (r != OK) {
		log_warn(&log, "ds_retrieve_mem(%s) failed (r=%d)\n", key, r);
		return r;
	}

	log_debug(&log, "State Restored\n");

	return OK;
}

static int
sef_cb_init(int type, sef_init_info_t * UNUSED(info))
{
	int r;
	char regex[DS_MAX_KEYLEN];
	struct machine machine;
	sys_getmachine(&machine);

	if (type != SEF_INIT_FRESH) {
		/* Restore a prior state. */
		lu_state_restore();
	}
	
	if (BOARD_IS_BBXM(machine.board_id) || BOARD_IS_BB(machine.board_id)){
		/* Set callback and initialize the bus */
		r = omap_interface_setup(&process, i2c_bus_id);
		if (r != OK) {
			return r;
		}
	} else {
		return ENODEV;
	}

	/* Announce we are up when necessary. */
	if (type != SEF_INIT_LU) {

		/* only capture events for this particular bus */
		snprintf(regex, DS_MAX_KEYLEN, "drv\\.i2c\\.%d\\..*",
		    i2c_bus_id + 1);

		/* Subscribe to driver events for i2c drivers */
		r = ds_subscribe(regex, DSF_INITIAL | DSF_OVERWRITE);
		if (r != OK) {
			log_warn(&log, "ds_subscribe() failed\n");
			return r;
		}

		chardriver_announce();
	}

	/* Save state */
	sef_cb_lu_state_save(0, 0);

	/* Initialization completed successfully. */
	return OK;
}

static void
sef_local_startup()
{
	/* Register init callbacks. */
	sef_setcb_init_fresh(sef_cb_init);
	sef_setcb_init_lu(sef_cb_init);
	sef_setcb_init_restart(sef_cb_init);

	/* Register live update callbacks */
	sef_setcb_lu_state_save(sef_cb_lu_state_save);

	/* Let SEF perform startup. */
	sef_startup();
}

static int
env_parse_instance(void)
{
	int r;
	long instance;

	/* Parse the instance number passed to service */
	instance = 0;
	r = env_parse("instance", "d", 0, &instance, 1, 3);
	if (r == -1) {
		log_warn(&log,
		    "Expecting '-arg instance=N' argument (N=1..3)\n");
		return EXIT_FAILURE;
	}

	/* Device files count from 1, hardware starts counting from 0 */
	i2c_bus_id = instance - 1;

	return OK;
}

int
main(int argc, char *argv[])
{
	int r;

	env_setargs(argc, argv);

	r = env_parse_instance();
	if (r != OK) {
		return r;
	}

	memset(i2cdev, '\0', sizeof(i2cdev));
	sef_local_startup();
	chardriver_task(&i2c_tab);

	return OK;
}