1 /* -*- mode: C; c-file-style: "linux" -*-
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
6 * PROPRIETARY/CONFIDENTIAL
8 * This software is the confidential and proprietary information of
9 * SAMSUNG ELECTRONICS ("Confidential Information"). You agree and acknowledge
10 * that this software is owned by Samsung and you shall not disclose
11 * such Confidential Information and shall use it only in accordance with
12 * the terms of the license agreement you entered into with SAMSUNG ELECTRONICS.
13 * SAMSUNG make no representations or warranties about the suitability of
14 * the software, either express or implied, including but not limited to
15 * the implied warranties of merchantability, fitness for a particular purpose,
16 * or non-infringement. SAMSUNG shall not be liable for any damages suffered by
17 * licensee arising out of or related to this software.
21 #include <sys/ioctl.h>
26 #include "mtp_usb_driver.h"
27 #include "mtp_device.h"
28 #include "mtp_descs_strings.h"
29 #include "ptp_datacodes.h"
30 #include "mtp_support.h"
31 #include "ptp_container.h"
33 #include "mtp_thread.h"
34 #include "mtp_transport.h"
35 #include "mtp_event_handler.h"
36 #include <sys/prctl.h>
37 #include <systemd/sd-daemon.h>
40 * GLOBAL AND EXTERN VARIABLES
42 extern mtp_config_t g_conf;
45 * STATIC VARIABLES AND FUNCTIONS
48 /*PIMA15740-2000 spec*/
49 #define USB_PTPREQUEST_CANCELIO 0x64 /* Cancel request */
50 #define USB_PTPREQUEST_GETEVENT 0x65 /* Get extened event data */
51 #define USB_PTPREQUEST_RESET 0x66 /* Reset Device */
52 #define USB_PTPREQUEST_GETSTATUS 0x67 /* Get Device Status */
53 #define USB_PTPREQUEST_CANCELIO_SIZE 6
54 #define USB_PTPREQUEST_GETSTATUS_SIZE 12
56 static mtp_int32 g_usb_ep0 = -1; /* read (g_usb_ep0, ...) */
57 static mtp_int32 g_usb_ep_in = -1; /* write (g_usb_ep_in, ...) */
58 static mtp_int32 g_usb_ep_out = -1; /* read (g_usb_ep_out, ...) */
59 static mtp_int32 g_usb_ep_status = -1; /* write (g_usb_ep_status, ...) */
61 static mtp_max_pkt_size_t pkt_size;
62 static mtp_uint32 rx_mq_sz;
63 static mtp_uint32 tx_mq_sz;
64 static mtp_int32 __handle_usb_read_err(mtp_int32 err,
65 mtp_uchar *buf, mtp_int32 buf_len);
66 static void __clean_up_msg_queue(void *param);
67 static void __handle_control_request(mtp_int32 request);
73 static mtp_bool __io_init()
77 if (sd_listen_fds(0) >= 4) {
78 g_usb_ep0 = SD_LISTEN_FDS_START;
79 g_usb_ep_in = SD_LISTEN_FDS_START + 1;
80 g_usb_ep_out = SD_LISTEN_FDS_START + 2;
81 g_usb_ep_status = SD_LISTEN_FDS_START + 3;
86 g_usb_ep0 = open(MTP_EP0_PATH, O_RDWR);
90 DBG("Writing USB descriptors");
91 ret = write(g_usb_ep0, &descriptors, sizeof(descriptors));
93 ERR("Error writing descriptors");
97 DBG("Writing USB strings");
98 ret = write(g_usb_ep0, &strings, sizeof(strings));
100 ERR("Error writing strings");
104 g_usb_ep_in = open(MTP_EP_IN_PATH, O_RDWR);
105 if (g_usb_ep_in < 0) {
106 ERR("Error opening bulk-in");
110 g_usb_ep_out = open(MTP_EP_OUT_PATH, O_RDWR);
111 if (g_usb_ep_out < 0) {
112 ERR("Error opening bulk-out");
116 g_usb_ep_status = open(MTP_EP_STATUS_PATH, O_RDWR);
117 if (g_usb_ep_status < 0) {
118 ERR("Error opening status");
134 static mtp_bool ffs_transport_init_usb_device(void)
136 mtp_int32 status = 0;
140 DBG("Device Already open\n");
144 status = __io_init();
147 ERR("Device node [%s] open failed, errno [%s]\n",
148 MTP_EP0_PATH, strerror_r(errno, error, sizeof(error)));
152 pkt_size.rx = g_conf.read_usb_size;
153 pkt_size.tx = g_conf.write_usb_size;
155 DBG("Final : Tx pkt size:[%u], Rx pkt size:[%u]\n", pkt_size.tx, pkt_size.rx);
157 msg_size = sizeof(msgq_ptr_t) - sizeof(long);
158 rx_mq_sz = (g_conf.max_io_buf_size / g_conf.max_rx_ipc_size) * msg_size;
159 tx_mq_sz = (g_conf.max_io_buf_size / g_conf.max_tx_ipc_size) * msg_size;
161 DBG("RX MQ size :[%u], TX MQ size:[%u]\n", rx_mq_sz, tx_mq_sz);
166 static void ffs_transport_deinit_usb_device(void)
172 if (g_usb_ep_in >= 0)
176 if (g_usb_ep_out >= 0)
180 if (g_usb_ep_status >= 0)
181 close(g_usb_ep_status);
182 g_usb_ep_status = -1;
187 static mtp_uint32 ffs_get_tx_pkt_size(void)
192 static mtp_uint32 ffs_get_rx_pkt_size(void)
198 * static mtp_int32 ffs_transport_mq_init()
199 * This function create a message queue for MTP,
200 * A created message queue will be used to help data transfer between
201 * MTP module and usb buffer.
202 * @return This function returns TRUE on success or
203 * returns FALSE on failure.
205 static mtp_int32 ffs_transport_mq_init(msgq_id_t *rx_mqid, msgq_id_t *tx_mqid)
207 if (_util_msgq_init(rx_mqid, 0) == FALSE) {
208 ERR("RX MQ init Fail [%d]\n", errno);
212 if (_util_msgq_set_size(*rx_mqid, rx_mq_sz) == FALSE)
213 ERR("RX MQ setting size Fail [%d]\n", errno);
215 if (_util_msgq_init(tx_mqid, 0) == FALSE) {
216 ERR("TX MQ init Fail [%d]\n", errno);
217 _util_msgq_deinit(rx_mqid);
222 if (_util_msgq_set_size(*tx_mqid, tx_mq_sz) == FALSE)
223 ERR("TX MQ setting size Fail [%d]\n", errno);
228 static void *ffs_transport_thread_usb_write(void *arg)
230 mtp_int32 status = 0;
232 unsigned char *mtp_buf = NULL;
233 msg_type_t mtype = MTP_UNDEFINED_PACKET;
234 msgq_id_t *mqid = (msgq_id_t *)arg;
236 pthread_cleanup_push(__clean_up_msg_queue, mqid);
239 /* original LinuxThreads cancelation didn't work right
240 * so test for it explicitly.
242 pthread_testcancel();
244 _util_rcv_msg_from_mq(*mqid, &mtp_buf, &len, &mtype);
246 if (mtype == MTP_BULK_PACKET || mtype == MTP_DATA_PACKET) {
247 status = write(g_usb_ep_in, mtp_buf, len);
249 ERR("USB write fail : %d\n", errno);
250 if (errno == ENOMEM || errno == ECANCELED) {
252 __clean_up_msg_queue(mqid);
257 } else if (MTP_EVENT_PACKET == mtype) {
258 /* Handling the MTP Asynchronous Events */
259 DBG("Send Interrupt data to kernel via g_usb_ep_status\n");
260 status = write(g_usb_ep_status, mtp_buf, len);
263 } else if (MTP_ZLP_PACKET == mtype) {
264 DBG("Send ZLP data to kerne via g_usb_ep_in\n");
265 status = write(g_usb_ep_in, (void*)0xFEE1DEAD, 0);
267 DBG("mtype = %d is not valid\n", mtype);
272 ERR("write data to the device node Fail:\
273 status = %d\n", status);
276 } while (status >= 0);
278 DBG("exited Source thread with status %d\n", status);
279 pthread_cleanup_pop(1);
285 static int __setup(int ep0, struct usb_ctrlrequest *ctrl)
287 const char* requests[] = {
288 "CANCELIO", /* 0x64 */
289 "GETEVENT", /* 0x65 */
291 "GETSTATUS", /* 0x67 */
293 __u16 wIndex = le16_to_cpu(ctrl->wIndex);
294 __u16 wValue = le16_to_cpu(ctrl->wValue);
295 __u16 wLength = le16_to_cpu(ctrl->wLength);
296 int rc = -EOPNOTSUPP;
299 if ((ctrl->bRequestType & 0x7f) != (USB_TYPE_CLASS | USB_RECIP_INTERFACE)) {
300 DBG(__FILE__ "(%s):%d: Invalid request type: %d",
301 __func__, __LINE__, ctrl->bRequestType);
305 switch (((ctrl->bRequestType & 0x80) << 8) | ctrl->bRequest) {
306 case ((USB_DIR_OUT << 8) | USB_PTPREQUEST_CANCELIO):
308 DBG(__FILE__ "(%s):%d: USB_PTPREQUEST_%s",
309 __func__, __LINE__, requests[ctrl->bRequest-0x64]);
310 if (wValue != 0 || wIndex != 0 || wLength != 6) {
311 DBG("Invalid request parameters: wValue:%d wIndex:%d wLength:%d\n");
315 __handle_control_request(ctrl->bRequest);
318 case ((USB_DIR_IN << 8) | USB_PTPREQUEST_GETSTATUS):
319 case ((USB_DIR_OUT << 8) | USB_PTPREQUEST_RESET):
321 DBG(__FILE__ "(%s):%d: USB_PTPREQUEST_%s",
322 __func__, __LINE__, requests[ctrl->bRequest-0x64]);
323 __handle_control_request(ctrl->bRequest);
326 case ((USB_DIR_IN << 8) | USB_PTPREQUEST_GETEVENT):
328 /* Optional, may stall */
329 DBG(__FILE__ "(%s):%d: USB_PTPREQUEST_%s",
330 __func__, __LINE__, requests[ctrl->bRequest-0x64]);
336 DBG(__FILE__ "(%s):%d: Invalid request: %d", __func__,
337 __LINE__, ctrl->bRequest);
344 DBG(__FILE__"(%s):%d:stall %0x2x.%02x\n",
345 __func__, __LINE__, ctrl->bRequestType, ctrl->bRequest);
346 if ((ctrl->bRequestType & 0x80) == USB_DIR_IN)
347 status = read(g_usb_ep0, NULL, 0);
349 status = write(g_usb_ep0, NULL, 0);
351 if (status != -1 || errno != EL2HLT) {
352 ERR(__FILE__"(%s):%d:stall error\n",
353 __func__, __LINE__, ctrl->bRequestType, ctrl->bRequest);
359 static void *ffs_transport_thread_usb_read(void *arg)
361 mtp_int32 status = 0;
362 msgq_ptr_t pkt = {MTP_DATA_PACKET, 0, 0, NULL};
363 msgq_id_t *mqid = (msgq_id_t *)arg;
364 mtp_uint32 rx_size = _get_rx_pkt_size();
366 pthread_cleanup_push(__clean_up_msg_queue, mqid);
369 pthread_testcancel();
371 pkt.buffer = (mtp_uchar *)g_malloc(rx_size);
372 if (NULL == pkt.buffer) {
373 ERR("Sink thread: memalloc failed.\n");
377 status = read(g_usb_ep_out, pkt.buffer, rx_size);
379 status = __handle_usb_read_err(status, pkt.buffer, rx_size);
381 ERR("__handle_usb_read_err is failed\n");
388 if (FALSE == _util_msgq_send(*mqid, (void *)&pkt,
389 sizeof(msgq_ptr_t) - sizeof(long), 0)) {
393 } while (status > 0);
395 DBG("status[%d] errno[%d]\n", status, errno);
396 pthread_cleanup_pop(1);
401 static void *ffs_transport_thread_usb_control(void *arg)
403 mtp_int32 status = 0;
404 struct usb_functionfs_event event;
405 msgq_id_t *mqid = (msgq_id_t *)arg;
407 pthread_cleanup_push(__clean_up_msg_queue, mqid);
410 pthread_testcancel();
412 status = read(g_usb_ep0, &event, sizeof(event));
415 ERR("read from ep0 failed: %s",
416 strerror_r(errno, error, sizeof(error)));
419 DBG("FUNCTIONFS event received: %d", event.type);
421 switch (event.type) {
422 case FUNCTIONFS_SETUP:
423 DBG("SETUP: bmRequestType:%d bRequest:%d wValue:%d wIndex:%d wLength:%d\n",
424 event.u.setup.bRequestType,
425 event.u.setup.bRequest,
426 event.u.setup.wValue,
427 event.u.setup.wIndex,
428 event.u.setup.wLength);
429 __setup(g_usb_ep0, &event.u.setup);
432 } while (status > 0);
434 DBG("status[%d] errno[%d]\n", status, errno);
435 pthread_cleanup_pop(1);
440 static mtp_int32 __handle_usb_read_err(mtp_int32 err,
441 mtp_uchar *buf, mtp_int32 buf_len)
446 while (retry++ < MTP_USB_ERROR_MAX_RETRY) {
448 DBG("ZLP(Zero Length Packet). Skip");
449 } else if (err < 0 && errno == EINTR) {
450 DBG("read () is interrupted. Skip");
451 } else if (err < 0 && errno == EIO) {
454 if (MTP_PHONE_USB_CONNECTED !=
455 _util_get_local_usb_status()) {
456 ERR("USB is disconnected");
460 _transport_deinit_usb_device();
461 ret = _transport_init_usb_device();
463 ERR("_transport_init_usb_device Fail");
467 ERR("Unknown error : %d, errno [%d] \n", err, errno);
471 err = read(g_usb_ep_out, buf, buf_len);
477 ERR("USB error handling Fail");
482 static void __clean_up_msg_queue(void *mq_id)
485 msgq_ptr_t pkt = { 0 };
486 msgq_id_t l_mqid = *(msgq_id_t *)mq_id;
488 ret_if(mq_id == NULL);
490 _transport_set_control_event(PTP_EVENTCODE_CANCELTRANSACTION);
491 while (TRUE == _util_msgq_receive(l_mqid, (void *)&pkt,
492 sizeof(msgq_ptr_t) - sizeof(long), 1, &len)) {
494 memset(&pkt, 0, sizeof(msgq_ptr_t));
500 static void __handle_control_request(mtp_int32 request)
502 static mtp_bool kernel_reset = FALSE;
503 static mtp_bool host_cancel = FALSE;
504 mtp_int32 status = 0;
507 case USB_PTPREQUEST_CANCELIO:
508 // XXX: Convert cancel request data from little-endian
509 // before use: le32_to_cpu(x), le16_to_cpu(x).
510 DBG("USB_PTPREQUEST_CANCELIO\n");
511 cancel_req_t cancelreq_data;
514 _transport_set_control_event(PTP_EVENTCODE_CANCELTRANSACTION);
515 status = read(g_usb_ep0, &cancelreq_data, sizeof(cancelreq_data));
518 ERR("Failed to read data for CANCELIO request\n: %s",
519 strerror_r(errno, error, sizeof(error)));
523 case USB_PTPREQUEST_RESET:
525 DBG("USB_PTPREQUEST_RESET\n");
527 if (kernel_reset == FALSE) {
531 status = read(g_usb_ep0, NULL, 0);
533 ERR("IOCTL MTP_SEND_RESET_ACK Failed [%d]\n",
537 case USB_PTPREQUEST_GETSTATUS:
539 DBG("USB_PTPREQUEST_GETSTATUS");
541 /* Send busy status response just once. This flag is also for
542 * the case that mtp misses the cancel request packet.
544 static mtp_bool sent_busy = FALSE;
545 usb_status_req_t statusreq_data = { 0 };
546 mtp_dword num_param = 0;
548 memset(&statusreq_data, 0x00, sizeof(usb_status_req_t));
549 if (host_cancel == TRUE || (sent_busy == FALSE &&
550 kernel_reset == FALSE)) {
551 DBG("Send busy response, set host_cancel to FALSE");
552 statusreq_data.len = 0x08;
553 statusreq_data.code = PTP_RESPONSE_DEVICEBUSY;
555 } else if (_device_get_phase() == DEVICE_PHASE_NOTREADY) {
556 statusreq_data.code =
557 PTP_RESPONSE_TRANSACTIONCANCELLED;
558 DBG("PTP_RESPONSE_TRANSACTIONCANCELLED");
559 statusreq_data.len = (mtp_word)(sizeof(usb_status_req_t) +
560 (num_param - 2) * sizeof(mtp_dword));
561 } else if (_device_get_status() == DEVICE_STATUSOK) {
562 DBG("PTP_RESPONSE_OK");
563 statusreq_data.len = 0x08;
564 statusreq_data.code = PTP_RESPONSE_OK;
566 if (kernel_reset == TRUE)
567 kernel_reset = FALSE;
569 DBG("PTP_RESPONSE_GEN_ERROR");
570 statusreq_data.len = 0x08;
571 statusreq_data.code = PTP_RESPONSE_GEN_ERROR;
574 if (statusreq_data.code == PTP_RESPONSE_DEVICEBUSY) {
580 /* status = ioctl(g_usb_fd, MTP_SET_SETUP_DATA, &statusreq_data);
582 DBG("IOCTL MTP_SET_SETUP_DATA Fail [%d]\n",
589 case USB_PTPREQUEST_GETEVENT:
590 DBG("USB_PTPREQUEST_GETEVENT");
594 DBG("Invalid class specific setup request");
601 * mtp_bool ffs_transport_mq_deinit()
602 * This function destroy a message queue for MTP,
603 * @return This function returns TRUE on success or
604 * returns FALSE on failure.
606 static mtp_bool ffs_transport_mq_deinit(msgq_id_t *rx_mqid, msgq_id_t *tx_mqid)
608 mtp_int32 res = TRUE;
611 res = _util_msgq_deinit(rx_mqid);
613 ERR("rx_mqid deinit Fail [%d]\n", errno);
620 res = _util_msgq_deinit(tx_mqid);
622 ERR("tx_mqid deinit fail [%d]\n", errno);
631 static mtp_uint32 ffs_transport_get_usb_packet_len(void)
633 mtp_int32 status = -1;
634 static mtp_int32 usb_speed = 0;
636 if (usb_speed == 0) {
638 //status = ioctl(g_usb_fd, MTP_GET_HIGH_FULL_SPEED, &usb_speed);
640 ERR("MTP_GET_HIGH_FULL_SPEED Failed [%d]\n", status);
641 return MTP_MAX_PACKET_SIZE_SEND_FS;
645 if (usb_speed % MTP_MAX_PACKET_SIZE_SEND_HS) {
646 return MTP_MAX_PACKET_SIZE_SEND_FS;
649 return MTP_MAX_PACKET_SIZE_SEND_HS;
653 const mtp_usb_driver_t mtp_usb_driver_ffs = {
654 .transport_init_usb_device = ffs_transport_init_usb_device,
655 .transport_deinit_usb_device = ffs_transport_deinit_usb_device,
656 .transport_thread_usb_write = ffs_transport_thread_usb_write,
657 .transport_thread_usb_read = ffs_transport_thread_usb_read,
658 .transport_thread_usb_control = ffs_transport_thread_usb_control,
659 .transport_mq_init = ffs_transport_mq_init,
660 .transport_mq_deinit = ffs_transport_mq_deinit,
661 .transport_get_usb_packet_len = ffs_transport_get_usb_packet_len,
662 .get_tx_pkt_size = ffs_get_tx_pkt_size,
663 .get_rx_pkt_size = ffs_get_rx_pkt_size,