| 
									
										
										
										
											2004-06-03 18:46:20 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * QEMU ADB support | 
					
						
							| 
									
										
										
										
											2007-09-16 21:08:06 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2004-06-03 18:46:20 +00:00
										 |  |  |  * Copyright (c) 2004 Fabrice Bellard | 
					
						
							| 
									
										
										
										
											2007-09-16 21:08:06 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2004-06-03 18:46:20 +00:00
										 |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | 
					
						
							|  |  |  |  * of this software and associated documentation files (the "Software"), to deal | 
					
						
							|  |  |  |  * in the Software without restriction, including without limitation the rights | 
					
						
							|  |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
					
						
							|  |  |  |  * copies of the Software, and to permit persons to whom the Software is | 
					
						
							|  |  |  |  * furnished to do so, subject to the following conditions: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The above copyright notice and this permission notice shall be included in | 
					
						
							|  |  |  |  * all copies or substantial portions of the Software. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
					
						
							|  |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | 
					
						
							|  |  |  |  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
					
						
							|  |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
					
						
							|  |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
					
						
							|  |  |  |  * THE SOFTWARE. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-05-23 16:35:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-26 18:17:30 +00:00
										 |  |  | #include "qemu/osdep.h"
 | 
					
						
							| 
									
										
										
										
											2013-02-05 17:06:20 +01:00
										 |  |  | #include "hw/input/adb.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 07:23:51 +02:00
										 |  |  | #include "hw/qdev-properties.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 07:23:45 +02:00
										 |  |  | #include "migration/vmstate.h"
 | 
					
						
							| 
									
										
										
										
											2019-05-23 16:35:07 +02:00
										 |  |  | #include "qemu/module.h"
 | 
					
						
							| 
									
										
										
										
											2017-12-20 13:14:06 +01:00
										 |  |  | #include "adb-internal.h"
 | 
					
						
							| 
									
										
										
										
											2004-06-03 18:46:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-12 20:15:26 +00:00
										 |  |  | /* error codes */ | 
					
						
							|  |  |  | #define ADB_RET_NOTPRESENT (-2)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-23 23:04:04 +00:00
										 |  |  | static void adb_device_reset(ADBDevice *d) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     qdev_reset_all(DEVICE(d)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-21 22:46:10 +00:00
										 |  |  | int adb_request(ADBBusState *s, uint8_t *obuf, const uint8_t *buf, int len) | 
					
						
							| 
									
										
										
										
											2004-06-03 18:46:20 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     ADBDevice *d; | 
					
						
							|  |  |  |     int devaddr, cmd, i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-21 16:47:13 +00:00
										 |  |  |     cmd = buf[0] & 0xf; | 
					
						
							| 
									
										
										
										
											2004-07-12 20:15:26 +00:00
										 |  |  |     if (cmd == ADB_BUSRESET) { | 
					
						
							|  |  |  |         for(i = 0; i < s->nb_devices; i++) { | 
					
						
							| 
									
										
										
										
											2013-01-23 23:04:04 +00:00
										 |  |  |             d = s->devices[i]; | 
					
						
							|  |  |  |             adb_device_reset(d); | 
					
						
							| 
									
										
										
										
											2004-07-12 20:15:26 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2004-06-03 18:46:20 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2004-07-12 20:15:26 +00:00
										 |  |  |     devaddr = buf[0] >> 4; | 
					
						
							| 
									
										
										
										
											2004-06-03 18:46:20 +00:00
										 |  |  |     for(i = 0; i < s->nb_devices; i++) { | 
					
						
							| 
									
										
										
										
											2013-01-23 23:04:04 +00:00
										 |  |  |         d = s->devices[i]; | 
					
						
							| 
									
										
										
										
											2004-06-03 18:46:20 +00:00
										 |  |  |         if (d->devaddr == devaddr) { | 
					
						
							| 
									
										
										
										
											2013-01-23 23:04:04 +00:00
										 |  |  |             ADBDeviceClass *adc = ADB_DEVICE_GET_CLASS(d); | 
					
						
							|  |  |  |             return adc->devreq(d, obuf, buf, len); | 
					
						
							| 
									
										
										
										
											2004-06-03 18:46:20 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2004-07-12 20:15:26 +00:00
										 |  |  |     return ADB_RET_NOTPRESENT; | 
					
						
							| 
									
										
										
										
											2004-06-21 22:46:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-12 20:15:26 +00:00
										 |  |  | /* XXX: move that to cuda ? */ | 
					
						
							| 
									
										
										
										
											2016-02-07 21:34:08 +01:00
										 |  |  | int adb_poll(ADBBusState *s, uint8_t *obuf, uint16_t poll_mask) | 
					
						
							| 
									
										
										
										
											2004-06-21 22:46:10 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     ADBDevice *d; | 
					
						
							|  |  |  |     int olen, i; | 
					
						
							| 
									
										
										
										
											2004-07-12 20:15:26 +00:00
										 |  |  |     uint8_t buf[1]; | 
					
						
							| 
									
										
										
										
											2004-06-21 22:46:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     olen = 0; | 
					
						
							|  |  |  |     for(i = 0; i < s->nb_devices; i++) { | 
					
						
							|  |  |  |         if (s->poll_index >= s->nb_devices) | 
					
						
							|  |  |  |             s->poll_index = 0; | 
					
						
							| 
									
										
										
										
											2013-01-23 23:04:04 +00:00
										 |  |  |         d = s->devices[s->poll_index]; | 
					
						
							| 
									
										
										
										
											2016-02-07 21:34:08 +01:00
										 |  |  |         if ((1 << d->devaddr) & poll_mask) { | 
					
						
							|  |  |  |             buf[0] = ADB_READREG | (d->devaddr << 4); | 
					
						
							|  |  |  |             olen = adb_request(s, obuf + 1, buf, 1); | 
					
						
							|  |  |  |             /* if there is data, we poll again the same device */ | 
					
						
							|  |  |  |             if (olen > 0) { | 
					
						
							|  |  |  |                 obuf[0] = buf[0]; | 
					
						
							|  |  |  |                 olen++; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2004-07-12 20:15:26 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         s->poll_index++; | 
					
						
							| 
									
										
										
										
											2004-06-21 22:46:10 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     return olen; | 
					
						
							| 
									
										
										
										
											2004-06-03 18:46:20 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-23 23:04:03 +00:00
										 |  |  | static const TypeInfo adb_bus_type_info = { | 
					
						
							|  |  |  |     .name = TYPE_ADB_BUS, | 
					
						
							|  |  |  |     .parent = TYPE_BUS, | 
					
						
							|  |  |  |     .instance_size = sizeof(ADBBusState), | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-20 13:14:06 +01:00
										 |  |  | const VMStateDescription vmstate_adb_device = { | 
					
						
							| 
									
										
										
										
											2015-02-09 22:40:45 +00:00
										 |  |  |     .name = "adb_device", | 
					
						
							|  |  |  |     .version_id = 0, | 
					
						
							|  |  |  |     .minimum_version_id = 0, | 
					
						
							|  |  |  |     .fields = (VMStateField[]) { | 
					
						
							|  |  |  |         VMSTATE_INT32(devaddr, ADBDevice), | 
					
						
							|  |  |  |         VMSTATE_INT32(handler, ADBDevice), | 
					
						
							|  |  |  |         VMSTATE_END_OF_LIST() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-23 23:04:04 +00:00
										 |  |  | static void adb_device_realizefn(DeviceState *dev, Error **errp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ADBDevice *d = ADB_DEVICE(dev); | 
					
						
							|  |  |  |     ADBBusState *bus = ADB_BUS(qdev_get_parent_bus(dev)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (bus->nb_devices >= MAX_ADB_DEVICES) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bus->devices[bus->nb_devices++] = d; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-12 17:44:01 +01:00
										 |  |  | static Property adb_device_properties[] = { | 
					
						
							|  |  |  |     DEFINE_PROP_BOOL("disable-direct-reg3-writes", ADBDevice, | 
					
						
							|  |  |  |                      disable_direct_reg3_writes, false), | 
					
						
							|  |  |  |     DEFINE_PROP_END_OF_LIST(), | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-23 23:04:04 +00:00
										 |  |  | static void adb_device_class_init(ObjectClass *oc, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     DeviceClass *dc = DEVICE_CLASS(oc); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dc->realize = adb_device_realizefn; | 
					
						
							| 
									
										
										
										
											2020-01-10 19:30:32 +04:00
										 |  |  |     device_class_set_props(dc, adb_device_properties); | 
					
						
							| 
									
										
										
										
											2013-01-23 23:04:04 +00:00
										 |  |  |     dc->bus_type = TYPE_ADB_BUS; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const TypeInfo adb_device_type_info = { | 
					
						
							|  |  |  |     .name = TYPE_ADB_DEVICE, | 
					
						
							|  |  |  |     .parent = TYPE_DEVICE, | 
					
						
							|  |  |  |     .instance_size = sizeof(ADBDevice), | 
					
						
							|  |  |  |     .abstract = true, | 
					
						
							|  |  |  |     .class_init = adb_device_class_init, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-23 23:04:03 +00:00
										 |  |  | static void adb_register_types(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     type_register_static(&adb_bus_type_info); | 
					
						
							| 
									
										
										
										
											2013-01-23 23:04:04 +00:00
										 |  |  |     type_register_static(&adb_device_type_info); | 
					
						
							| 
									
										
										
										
											2013-01-23 23:04:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type_init(adb_register_types) |