Lines Matching +full:ctrl +full:- +full:len
1 // SPDX-License-Identifier: GPL-2.0
3 // QiHeng Electronics ch341a USB-to-SPI adapter driver
31 struct spi_controller *ctrl; member
45 spi_controller_get_devdata(spi->controller); in ch341_set_cs()
48 memset(ch341->tx_buf, 0, CH341_PACKET_LENGTH); in ch341_set_cs()
49 ch341->tx_buf[0] = CH341A_CMD_UIO_STREAM; in ch341_set_cs()
50 ch341->tx_buf[1] = CH341A_CMD_UIO_STM_OUT | (is_high ? 0x36 : 0x37); in ch341_set_cs()
53 ch341->tx_buf[2] = CH341A_CMD_UIO_STM_DIR | 0x3f; in ch341_set_cs()
54 ch341->tx_buf[3] = CH341A_CMD_UIO_STM_END; in ch341_set_cs()
56 ch341->tx_buf[2] = CH341A_CMD_UIO_STM_END; in ch341_set_cs()
59 err = usb_bulk_msg(ch341->udev, ch341->write_pipe, ch341->tx_buf, in ch341_set_cs()
62 dev_err(&spi->dev, in ch341_set_cs()
71 spi_controller_get_devdata(spi->controller); in ch341_transfer_one()
72 int len; in ch341_transfer_one() local
75 len = min(CH341_PACKET_LENGTH, trans->len + 1); in ch341_transfer_one()
77 memset(ch341->tx_buf, 0, CH341_PACKET_LENGTH); in ch341_transfer_one()
79 ch341->tx_buf[0] = CH341A_CMD_SPI_STREAM; in ch341_transfer_one()
81 memcpy(ch341->tx_buf + 1, trans->tx_buf, len); in ch341_transfer_one()
83 ret = usb_bulk_msg(ch341->udev, ch341->write_pipe, ch341->tx_buf, len, in ch341_transfer_one()
88 return usb_bulk_msg(ch341->udev, ch341->read_pipe, trans->rx_buf, in ch341_transfer_one()
89 len - 1, NULL, CH341_DEFAULT_TIMEOUT); in ch341_transfer_one()
94 struct ch341_spi_dev *ch341 = urb->context; in ch341_recv()
95 struct usb_device *udev = ch341->udev; in ch341_recv()
97 switch (urb->status) { in ch341_recv()
101 case -ENOENT: in ch341_recv()
102 case -ECONNRESET: in ch341_recv()
103 case -EPIPE: in ch341_recv()
104 case -ESHUTDOWN: in ch341_recv()
105 dev_dbg(&udev->dev, "rx urb terminated with status: %d\n", in ch341_recv()
106 urb->status); in ch341_recv()
109 dev_dbg(&udev->dev, "rx urb error: %d\n", urb->status); in ch341_recv()
116 memset(ch341->tx_buf, 0, CH341_PACKET_LENGTH); in ch341_config_stream()
117 ch341->tx_buf[0] = CH341A_CMD_I2C_STREAM; in ch341_config_stream()
118 ch341->tx_buf[1] = CH341A_CMD_I2C_STM_SET | CH341A_STM_I2C_100K; in ch341_config_stream()
119 ch341->tx_buf[2] = CH341A_CMD_I2C_STM_END; in ch341_config_stream()
121 return usb_bulk_msg(ch341->udev, ch341->write_pipe, ch341->tx_buf, 3, in ch341_config_stream()
127 memset(ch341->tx_buf, 0, CH341_PACKET_LENGTH); in ch341_enable_pins()
128 ch341->tx_buf[0] = CH341A_CMD_UIO_STREAM; in ch341_enable_pins()
129 ch341->tx_buf[1] = CH341A_CMD_UIO_STM_OUT | 0x37; in ch341_enable_pins()
130 ch341->tx_buf[2] = CH341A_CMD_UIO_STM_DIR | (enable ? 0x3f : 0x00); in ch341_enable_pins()
131 ch341->tx_buf[3] = CH341A_CMD_UIO_STM_END; in ch341_enable_pins()
133 return usb_bulk_msg(ch341->udev, ch341->write_pipe, ch341->tx_buf, 4, in ch341_enable_pins()
138 .modalias = "spi-ch341a",
147 struct spi_controller *ctrl; in ch341_probe() local
150 ret = usb_find_common_endpoints(intf->cur_altsetting, &in, &out, NULL, in ch341_probe()
155 ctrl = devm_spi_alloc_master(&udev->dev, sizeof(struct ch341_spi_dev)); in ch341_probe()
156 if (!ctrl) in ch341_probe()
157 return -ENOMEM; in ch341_probe()
159 ch341 = spi_controller_get_devdata(ctrl); in ch341_probe()
160 ch341->ctrl = ctrl; in ch341_probe()
161 ch341->udev = udev; in ch341_probe()
162 ch341->write_pipe = usb_sndbulkpipe(udev, usb_endpoint_num(out)); in ch341_probe()
163 ch341->read_pipe = usb_rcvbulkpipe(udev, usb_endpoint_num(in)); in ch341_probe()
165 ch341->rx_len = usb_endpoint_maxp(in); in ch341_probe()
166 ch341->rx_buf = devm_kzalloc(&udev->dev, ch341->rx_len, GFP_KERNEL); in ch341_probe()
167 if (!ch341->rx_buf) in ch341_probe()
168 return -ENOMEM; in ch341_probe()
170 ch341->rx_urb = usb_alloc_urb(0, GFP_KERNEL); in ch341_probe()
171 if (!ch341->rx_urb) in ch341_probe()
172 return -ENOMEM; in ch341_probe()
174 ch341->tx_buf = in ch341_probe()
175 devm_kzalloc(&udev->dev, CH341_PACKET_LENGTH, GFP_KERNEL); in ch341_probe()
176 if (!ch341->tx_buf) in ch341_probe()
177 return -ENOMEM; in ch341_probe()
179 usb_fill_bulk_urb(ch341->rx_urb, udev, ch341->read_pipe, ch341->rx_buf, in ch341_probe()
180 ch341->rx_len, ch341_recv, ch341); in ch341_probe()
182 ret = usb_submit_urb(ch341->rx_urb, GFP_KERNEL); in ch341_probe()
184 usb_free_urb(ch341->rx_urb); in ch341_probe()
185 return -ENOMEM; in ch341_probe()
188 ctrl->bus_num = -1; in ch341_probe()
189 ctrl->mode_bits = SPI_CPHA; in ch341_probe()
190 ctrl->transfer_one = ch341_transfer_one; in ch341_probe()
191 ctrl->set_cs = ch341_set_cs; in ch341_probe()
192 ctrl->auto_runtime_pm = false; in ch341_probe()
204 ret = spi_register_controller(ctrl); in ch341_probe()
208 ch341->spidev = spi_new_device(ctrl, &chip); in ch341_probe()
209 if (!ch341->spidev) in ch341_probe()
210 return -ENOMEM; in ch341_probe()
219 spi_unregister_device(ch341->spidev); in ch341_disconnect()
220 spi_unregister_controller(ch341->ctrl); in ch341_disconnect()
222 usb_free_urb(ch341->rx_urb); in ch341_disconnect()
232 .name = "spi-ch341",