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