1/*-
2 * Copyright (c) 2015 M. Warner Losh <imp@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29#ifndef DEV_OW_OWN_H
30#define DEV_OW_OWN_H 1
31
32#include "own_if.h"
33
34
35#define	READ_ROM	0x33
36#define	MATCH_ROM	0x55
37#define	SKIP_ROM	0xcc
38#define	ALARM_SEARCH	0xec
39#define	SEARCH_ROM	0xf0
40
41static inline int
42own_send_command(device_t pdev, struct ow_cmd *cmd)
43{
44	device_t ndev = device_get_parent(pdev);
45
46	return OWN_SEND_COMMAND(ndev, pdev, cmd);
47}
48
49/*
50 * How args for own_acquire_bus
51 */
52#define	OWN_WAIT	1
53#define	OWN_DONTWAIT	2
54
55static inline int
56own_acquire_bus(device_t pdev, int how)
57{
58	device_t ndev = device_get_parent(pdev);
59
60	return OWN_ACQUIRE_BUS(ndev, pdev, how);
61}
62
63static inline void
64own_release_bus(device_t pdev)
65{
66	device_t ndev = device_get_parent(pdev);
67
68	OWN_RELEASE_BUS(ndev, pdev);
69}
70
71static inline uint8_t
72own_crc(device_t pdev, uint8_t *buffer, size_t len)
73{
74	device_t ndev = device_get_parent(pdev);
75
76	return OWN_CRC(ndev, pdev, buffer, len);
77}
78
79static inline void
80own_self_command(device_t pdev, struct ow_cmd *cmd, uint8_t xpt_cmd)
81{
82	uint8_t *mep;
83
84	memset(cmd, 0, sizeof(*cmd));
85	mep = ow_get_romid(pdev);
86	cmd->rom_cmd[0] = MATCH_ROM;
87	memcpy(&cmd->rom_cmd[1], mep, sizeof(romid_t));
88	cmd->rom_len = 1 + sizeof(romid_t);
89	cmd->xpt_cmd[0] = xpt_cmd;
90	cmd->xpt_len = 1;
91}
92
93static inline int
94own_command_wait(device_t pdev, struct ow_cmd *cmd)
95{
96	int rv;
97
98	rv = own_acquire_bus(pdev, OWN_WAIT);
99	if (rv != 0)
100		return rv;
101	rv = own_send_command(pdev, cmd);
102	own_release_bus(pdev);
103	return rv;
104}
105
106#endif /* DEV_OW_OWLL_H */
107