usbip_common.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  4. * Copyright (C) 2015-2016 Samsung Electronics
  5. * Krzysztof Opasiak <k.opasiak@samsung.com>
  6. */
  7. #include <asm/byteorder.h>
  8. #include <linux/file.h>
  9. #include <linux/fs.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/stat.h>
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <net/sock.h>
  16. #include "usbip_common.h"
  17. #define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>"
  18. #define DRIVER_DESC "USB/IP Core"
  19. #ifdef CONFIG_USBIP_DEBUG
  20. unsigned long usbip_debug_flag = 0xffffffff;
  21. #else
  22. unsigned long usbip_debug_flag;
  23. #endif
  24. EXPORT_SYMBOL_GPL(usbip_debug_flag);
  25. module_param(usbip_debug_flag, ulong, S_IRUGO|S_IWUSR);
  26. MODULE_PARM_DESC(usbip_debug_flag, "debug flags (defined in usbip_common.h)");
  27. /* FIXME */
  28. struct device_attribute dev_attr_usbip_debug;
  29. EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
  30. static ssize_t usbip_debug_show(struct device *dev,
  31. struct device_attribute *attr, char *buf)
  32. {
  33. return sprintf(buf, "%lx\n", usbip_debug_flag);
  34. }
  35. static ssize_t usbip_debug_store(struct device *dev,
  36. struct device_attribute *attr, const char *buf,
  37. size_t count)
  38. {
  39. if (sscanf(buf, "%lx", &usbip_debug_flag) != 1)
  40. return -EINVAL;
  41. return count;
  42. }
  43. DEVICE_ATTR_RW(usbip_debug);
  44. static void usbip_dump_buffer(char *buff, int bufflen)
  45. {
  46. print_hex_dump(KERN_DEBUG, "usbip-core", DUMP_PREFIX_OFFSET, 16, 4,
  47. buff, bufflen, false);
  48. }
  49. static void usbip_dump_pipe(unsigned int p)
  50. {
  51. unsigned char type = usb_pipetype(p);
  52. unsigned char ep = usb_pipeendpoint(p);
  53. unsigned char dev = usb_pipedevice(p);
  54. unsigned char dir = usb_pipein(p);
  55. pr_debug("dev(%d) ep(%d) [%s] ", dev, ep, dir ? "IN" : "OUT");
  56. switch (type) {
  57. case PIPE_ISOCHRONOUS:
  58. pr_debug("ISO\n");
  59. break;
  60. case PIPE_INTERRUPT:
  61. pr_debug("INT\n");
  62. break;
  63. case PIPE_CONTROL:
  64. pr_debug("CTRL\n");
  65. break;
  66. case PIPE_BULK:
  67. pr_debug("BULK\n");
  68. break;
  69. default:
  70. pr_debug("ERR\n");
  71. break;
  72. }
  73. }
  74. static void usbip_dump_usb_device(struct usb_device *udev)
  75. {
  76. struct device *dev = &udev->dev;
  77. int i;
  78. dev_dbg(dev, " devnum(%d) devpath(%s) usb speed(%s)",
  79. udev->devnum, udev->devpath, usb_speed_string(udev->speed));
  80. pr_debug("tt hub ttport %d\n", udev->ttport);
  81. dev_dbg(dev, " ");
  82. for (i = 0; i < 16; i++)
  83. pr_debug(" %2u", i);
  84. pr_debug("\n");
  85. dev_dbg(dev, " toggle0(IN) :");
  86. for (i = 0; i < 16; i++)
  87. pr_debug(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
  88. pr_debug("\n");
  89. dev_dbg(dev, " toggle1(OUT):");
  90. for (i = 0; i < 16; i++)
  91. pr_debug(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
  92. pr_debug("\n");
  93. dev_dbg(dev, " epmaxp_in :");
  94. for (i = 0; i < 16; i++) {
  95. if (udev->ep_in[i])
  96. pr_debug(" %2u",
  97. le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
  98. }
  99. pr_debug("\n");
  100. dev_dbg(dev, " epmaxp_out :");
  101. for (i = 0; i < 16; i++) {
  102. if (udev->ep_out[i])
  103. pr_debug(" %2u",
  104. le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
  105. }
  106. pr_debug("\n");
  107. dev_dbg(dev, "parent %s, bus %s\n", dev_name(&udev->parent->dev),
  108. udev->bus->bus_name);
  109. dev_dbg(dev, "have_langid %d, string_langid %d\n",
  110. udev->have_langid, udev->string_langid);
  111. dev_dbg(dev, "maxchild %d\n", udev->maxchild);
  112. }
  113. static void usbip_dump_request_type(__u8 rt)
  114. {
  115. switch (rt & USB_RECIP_MASK) {
  116. case USB_RECIP_DEVICE:
  117. pr_debug("DEVICE");
  118. break;
  119. case USB_RECIP_INTERFACE:
  120. pr_debug("INTERF");
  121. break;
  122. case USB_RECIP_ENDPOINT:
  123. pr_debug("ENDPOI");
  124. break;
  125. case USB_RECIP_OTHER:
  126. pr_debug("OTHER ");
  127. break;
  128. default:
  129. pr_debug("------");
  130. break;
  131. }
  132. }
  133. static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
  134. {
  135. if (!cmd) {
  136. pr_debug(" : null pointer\n");
  137. return;
  138. }
  139. pr_debug(" ");
  140. pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) wLength(%04X) ",
  141. cmd->bRequestType, cmd->bRequest,
  142. cmd->wValue, cmd->wIndex, cmd->wLength);
  143. pr_debug("\n ");
  144. if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  145. pr_debug("STANDARD ");
  146. switch (cmd->bRequest) {
  147. case USB_REQ_GET_STATUS:
  148. pr_debug("GET_STATUS\n");
  149. break;
  150. case USB_REQ_CLEAR_FEATURE:
  151. pr_debug("CLEAR_FEAT\n");
  152. break;
  153. case USB_REQ_SET_FEATURE:
  154. pr_debug("SET_FEAT\n");
  155. break;
  156. case USB_REQ_SET_ADDRESS:
  157. pr_debug("SET_ADDRRS\n");
  158. break;
  159. case USB_REQ_GET_DESCRIPTOR:
  160. pr_debug("GET_DESCRI\n");
  161. break;
  162. case USB_REQ_SET_DESCRIPTOR:
  163. pr_debug("SET_DESCRI\n");
  164. break;
  165. case USB_REQ_GET_CONFIGURATION:
  166. pr_debug("GET_CONFIG\n");
  167. break;
  168. case USB_REQ_SET_CONFIGURATION:
  169. pr_debug("SET_CONFIG\n");
  170. break;
  171. case USB_REQ_GET_INTERFACE:
  172. pr_debug("GET_INTERF\n");
  173. break;
  174. case USB_REQ_SET_INTERFACE:
  175. pr_debug("SET_INTERF\n");
  176. break;
  177. case USB_REQ_SYNCH_FRAME:
  178. pr_debug("SYNC_FRAME\n");
  179. break;
  180. default:
  181. pr_debug("REQ(%02X)\n", cmd->bRequest);
  182. break;
  183. }
  184. usbip_dump_request_type(cmd->bRequestType);
  185. } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
  186. pr_debug("CLASS\n");
  187. } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
  188. pr_debug("VENDOR\n");
  189. } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
  190. pr_debug("RESERVED\n");
  191. }
  192. }
  193. void usbip_dump_urb(struct urb *urb)
  194. {
  195. struct device *dev;
  196. if (!urb) {
  197. pr_debug("urb: null pointer!!\n");
  198. return;
  199. }
  200. if (!urb->dev) {
  201. pr_debug("urb->dev: null pointer!!\n");
  202. return;
  203. }
  204. dev = &urb->dev->dev;
  205. usbip_dump_usb_device(urb->dev);
  206. dev_dbg(dev, " pipe :%08x ", urb->pipe);
  207. usbip_dump_pipe(urb->pipe);
  208. dev_dbg(dev, " status :%d\n", urb->status);
  209. dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
  210. dev_dbg(dev, " transfer_buffer_length:%d\n",
  211. urb->transfer_buffer_length);
  212. dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
  213. if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
  214. usbip_dump_usb_ctrlrequest(
  215. (struct usb_ctrlrequest *)urb->setup_packet);
  216. dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
  217. dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
  218. dev_dbg(dev, " interval :%d\n", urb->interval);
  219. dev_dbg(dev, " error_count :%d\n", urb->error_count);
  220. }
  221. EXPORT_SYMBOL_GPL(usbip_dump_urb);
  222. void usbip_dump_header(struct usbip_header *pdu)
  223. {
  224. pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
  225. pdu->base.command,
  226. pdu->base.seqnum,
  227. pdu->base.devid,
  228. pdu->base.direction,
  229. pdu->base.ep);
  230. switch (pdu->base.command) {
  231. case USBIP_CMD_SUBMIT:
  232. pr_debug("USBIP_CMD_SUBMIT: x_flags %u x_len %u sf %u #p %d iv %d\n",
  233. pdu->u.cmd_submit.transfer_flags,
  234. pdu->u.cmd_submit.transfer_buffer_length,
  235. pdu->u.cmd_submit.start_frame,
  236. pdu->u.cmd_submit.number_of_packets,
  237. pdu->u.cmd_submit.interval);
  238. break;
  239. case USBIP_CMD_UNLINK:
  240. pr_debug("USBIP_CMD_UNLINK: seq %u\n",
  241. pdu->u.cmd_unlink.seqnum);
  242. break;
  243. case USBIP_RET_SUBMIT:
  244. pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
  245. pdu->u.ret_submit.status,
  246. pdu->u.ret_submit.actual_length,
  247. pdu->u.ret_submit.start_frame,
  248. pdu->u.ret_submit.number_of_packets,
  249. pdu->u.ret_submit.error_count);
  250. break;
  251. case USBIP_RET_UNLINK:
  252. pr_debug("USBIP_RET_UNLINK: status %d\n",
  253. pdu->u.ret_unlink.status);
  254. break;
  255. default:
  256. /* NOT REACHED */
  257. pr_err("unknown command\n");
  258. break;
  259. }
  260. }
  261. EXPORT_SYMBOL_GPL(usbip_dump_header);
  262. /* Receive data over TCP/IP. */
  263. int usbip_recv(struct socket *sock, void *buf, int size)
  264. {
  265. int result;
  266. struct kvec iov = {.iov_base = buf, .iov_len = size};
  267. struct msghdr msg = {.msg_flags = MSG_NOSIGNAL};
  268. int total = 0;
  269. if (!sock || !buf || !size)
  270. return -EINVAL;
  271. iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, size);
  272. usbip_dbg_xmit("enter\n");
  273. do {
  274. sock->sk->sk_allocation = GFP_NOIO;
  275. result = sock_recvmsg(sock, &msg, MSG_WAITALL);
  276. if (result <= 0)
  277. goto err;
  278. total += result;
  279. } while (msg_data_left(&msg));
  280. if (usbip_dbg_flag_xmit) {
  281. if (!in_interrupt())
  282. pr_debug("%-10s:", current->comm);
  283. else
  284. pr_debug("interrupt :");
  285. pr_debug("receiving....\n");
  286. usbip_dump_buffer(buf, size);
  287. pr_debug("received, osize %d ret %d size %zd total %d\n",
  288. size, result, msg_data_left(&msg), total);
  289. }
  290. return total;
  291. err:
  292. return result;
  293. }
  294. EXPORT_SYMBOL_GPL(usbip_recv);
  295. /* there may be more cases to tweak the flags. */
  296. static unsigned int tweak_transfer_flags(unsigned int flags)
  297. {
  298. flags &= ~URB_NO_TRANSFER_DMA_MAP;
  299. return flags;
  300. }
  301. static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
  302. int pack)
  303. {
  304. struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
  305. /*
  306. * Some members are not still implemented in usbip. I hope this issue
  307. * will be discussed when usbip is ported to other operating systems.
  308. */
  309. if (pack) {
  310. spdu->transfer_flags =
  311. tweak_transfer_flags(urb->transfer_flags);
  312. spdu->transfer_buffer_length = urb->transfer_buffer_length;
  313. spdu->start_frame = urb->start_frame;
  314. spdu->number_of_packets = urb->number_of_packets;
  315. spdu->interval = urb->interval;
  316. } else {
  317. urb->transfer_flags = spdu->transfer_flags;
  318. urb->transfer_buffer_length = spdu->transfer_buffer_length;
  319. urb->start_frame = spdu->start_frame;
  320. urb->number_of_packets = spdu->number_of_packets;
  321. urb->interval = spdu->interval;
  322. }
  323. }
  324. static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
  325. int pack)
  326. {
  327. struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
  328. if (pack) {
  329. rpdu->status = urb->status;
  330. rpdu->actual_length = urb->actual_length;
  331. rpdu->start_frame = urb->start_frame;
  332. rpdu->number_of_packets = urb->number_of_packets;
  333. rpdu->error_count = urb->error_count;
  334. } else {
  335. urb->status = rpdu->status;
  336. urb->actual_length = rpdu->actual_length;
  337. urb->start_frame = rpdu->start_frame;
  338. urb->number_of_packets = rpdu->number_of_packets;
  339. urb->error_count = rpdu->error_count;
  340. }
  341. }
  342. void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
  343. int pack)
  344. {
  345. switch (cmd) {
  346. case USBIP_CMD_SUBMIT:
  347. usbip_pack_cmd_submit(pdu, urb, pack);
  348. break;
  349. case USBIP_RET_SUBMIT:
  350. usbip_pack_ret_submit(pdu, urb, pack);
  351. break;
  352. default:
  353. /* NOT REACHED */
  354. pr_err("unknown command\n");
  355. break;
  356. }
  357. }
  358. EXPORT_SYMBOL_GPL(usbip_pack_pdu);
  359. static void correct_endian_basic(struct usbip_header_basic *base, int send)
  360. {
  361. if (send) {
  362. base->command = cpu_to_be32(base->command);
  363. base->seqnum = cpu_to_be32(base->seqnum);
  364. base->devid = cpu_to_be32(base->devid);
  365. base->direction = cpu_to_be32(base->direction);
  366. base->ep = cpu_to_be32(base->ep);
  367. } else {
  368. base->command = be32_to_cpu(base->command);
  369. base->seqnum = be32_to_cpu(base->seqnum);
  370. base->devid = be32_to_cpu(base->devid);
  371. base->direction = be32_to_cpu(base->direction);
  372. base->ep = be32_to_cpu(base->ep);
  373. }
  374. }
  375. static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
  376. int send)
  377. {
  378. if (send) {
  379. pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
  380. cpu_to_be32s(&pdu->transfer_buffer_length);
  381. cpu_to_be32s(&pdu->start_frame);
  382. cpu_to_be32s(&pdu->number_of_packets);
  383. cpu_to_be32s(&pdu->interval);
  384. } else {
  385. pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
  386. be32_to_cpus(&pdu->transfer_buffer_length);
  387. be32_to_cpus(&pdu->start_frame);
  388. be32_to_cpus(&pdu->number_of_packets);
  389. be32_to_cpus(&pdu->interval);
  390. }
  391. }
  392. static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
  393. int send)
  394. {
  395. if (send) {
  396. cpu_to_be32s(&pdu->status);
  397. cpu_to_be32s(&pdu->actual_length);
  398. cpu_to_be32s(&pdu->start_frame);
  399. cpu_to_be32s(&pdu->number_of_packets);
  400. cpu_to_be32s(&pdu->error_count);
  401. } else {
  402. be32_to_cpus(&pdu->status);
  403. be32_to_cpus(&pdu->actual_length);
  404. be32_to_cpus(&pdu->start_frame);
  405. be32_to_cpus(&pdu->number_of_packets);
  406. be32_to_cpus(&pdu->error_count);
  407. }
  408. }
  409. static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
  410. int send)
  411. {
  412. if (send)
  413. pdu->seqnum = cpu_to_be32(pdu->seqnum);
  414. else
  415. pdu->seqnum = be32_to_cpu(pdu->seqnum);
  416. }
  417. static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
  418. int send)
  419. {
  420. if (send)
  421. cpu_to_be32s(&pdu->status);
  422. else
  423. be32_to_cpus(&pdu->status);
  424. }
  425. void usbip_header_correct_endian(struct usbip_header *pdu, int send)
  426. {
  427. __u32 cmd = 0;
  428. if (send)
  429. cmd = pdu->base.command;
  430. correct_endian_basic(&pdu->base, send);
  431. if (!send)
  432. cmd = pdu->base.command;
  433. switch (cmd) {
  434. case USBIP_CMD_SUBMIT:
  435. correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
  436. break;
  437. case USBIP_RET_SUBMIT:
  438. correct_endian_ret_submit(&pdu->u.ret_submit, send);
  439. break;
  440. case USBIP_CMD_UNLINK:
  441. correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
  442. break;
  443. case USBIP_RET_UNLINK:
  444. correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
  445. break;
  446. default:
  447. /* NOT REACHED */
  448. pr_err("unknown command\n");
  449. break;
  450. }
  451. }
  452. EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
  453. static void usbip_iso_packet_correct_endian(
  454. struct usbip_iso_packet_descriptor *iso, int send)
  455. {
  456. /* does not need all members. but copy all simply. */
  457. if (send) {
  458. iso->offset = cpu_to_be32(iso->offset);
  459. iso->length = cpu_to_be32(iso->length);
  460. iso->status = cpu_to_be32(iso->status);
  461. iso->actual_length = cpu_to_be32(iso->actual_length);
  462. } else {
  463. iso->offset = be32_to_cpu(iso->offset);
  464. iso->length = be32_to_cpu(iso->length);
  465. iso->status = be32_to_cpu(iso->status);
  466. iso->actual_length = be32_to_cpu(iso->actual_length);
  467. }
  468. }
  469. static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
  470. struct usb_iso_packet_descriptor *uiso, int pack)
  471. {
  472. if (pack) {
  473. iso->offset = uiso->offset;
  474. iso->length = uiso->length;
  475. iso->status = uiso->status;
  476. iso->actual_length = uiso->actual_length;
  477. } else {
  478. uiso->offset = iso->offset;
  479. uiso->length = iso->length;
  480. uiso->status = iso->status;
  481. uiso->actual_length = iso->actual_length;
  482. }
  483. }
  484. /* must free buffer */
  485. struct usbip_iso_packet_descriptor*
  486. usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
  487. {
  488. struct usbip_iso_packet_descriptor *iso;
  489. int np = urb->number_of_packets;
  490. ssize_t size = np * sizeof(*iso);
  491. int i;
  492. iso = kzalloc(size, GFP_KERNEL);
  493. if (!iso)
  494. return NULL;
  495. for (i = 0; i < np; i++) {
  496. usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
  497. usbip_iso_packet_correct_endian(&iso[i], 1);
  498. }
  499. *bufflen = size;
  500. return iso;
  501. }
  502. EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
  503. /* some members of urb must be substituted before. */
  504. int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
  505. {
  506. void *buff;
  507. struct usbip_iso_packet_descriptor *iso;
  508. int np = urb->number_of_packets;
  509. int size = np * sizeof(*iso);
  510. int i;
  511. int ret;
  512. int total_length = 0;
  513. if (!usb_pipeisoc(urb->pipe))
  514. return 0;
  515. /* my Bluetooth dongle gets ISO URBs which are np = 0 */
  516. if (np == 0)
  517. return 0;
  518. buff = kzalloc(size, GFP_KERNEL);
  519. if (!buff)
  520. return -ENOMEM;
  521. ret = usbip_recv(ud->tcp_socket, buff, size);
  522. if (ret != size) {
  523. dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
  524. ret);
  525. kfree(buff);
  526. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
  527. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  528. else
  529. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  530. return -EPIPE;
  531. }
  532. iso = (struct usbip_iso_packet_descriptor *) buff;
  533. for (i = 0; i < np; i++) {
  534. usbip_iso_packet_correct_endian(&iso[i], 0);
  535. usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
  536. total_length += urb->iso_frame_desc[i].actual_length;
  537. }
  538. kfree(buff);
  539. if (total_length != urb->actual_length) {
  540. dev_err(&urb->dev->dev,
  541. "total length of iso packets %d not equal to actual length of buffer %d\n",
  542. total_length, urb->actual_length);
  543. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
  544. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  545. else
  546. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  547. return -EPIPE;
  548. }
  549. return ret;
  550. }
  551. EXPORT_SYMBOL_GPL(usbip_recv_iso);
  552. /*
  553. * This functions restores the padding which was removed for optimizing
  554. * the bandwidth during transfer over tcp/ip
  555. *
  556. * buffer and iso packets need to be stored and be in propeper endian in urb
  557. * before calling this function
  558. */
  559. void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
  560. {
  561. int np = urb->number_of_packets;
  562. int i;
  563. int actualoffset = urb->actual_length;
  564. if (!usb_pipeisoc(urb->pipe))
  565. return;
  566. /* if no packets or length of data is 0, then nothing to unpack */
  567. if (np == 0 || urb->actual_length == 0)
  568. return;
  569. /*
  570. * if actual_length is transfer_buffer_length then no padding is
  571. * present.
  572. */
  573. if (urb->actual_length == urb->transfer_buffer_length)
  574. return;
  575. /*
  576. * loop over all packets from last to first (to prevent overwriting
  577. * memory when padding) and move them into the proper place
  578. */
  579. for (i = np-1; i > 0; i--) {
  580. actualoffset -= urb->iso_frame_desc[i].actual_length;
  581. memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
  582. urb->transfer_buffer + actualoffset,
  583. urb->iso_frame_desc[i].actual_length);
  584. }
  585. }
  586. EXPORT_SYMBOL_GPL(usbip_pad_iso);
  587. /* some members of urb must be substituted before. */
  588. int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
  589. {
  590. struct scatterlist *sg;
  591. int ret = 0;
  592. int recv;
  593. int size;
  594. int copy;
  595. int i;
  596. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
  597. /* the direction of urb must be OUT. */
  598. if (usb_pipein(urb->pipe))
  599. return 0;
  600. size = urb->transfer_buffer_length;
  601. } else {
  602. /* the direction of urb must be IN. */
  603. if (usb_pipeout(urb->pipe))
  604. return 0;
  605. size = urb->actual_length;
  606. }
  607. /* no need to recv xbuff */
  608. if (!(size > 0))
  609. return 0;
  610. if (size > urb->transfer_buffer_length)
  611. /* should not happen, probably malicious packet */
  612. goto error;
  613. if (urb->num_sgs) {
  614. copy = size;
  615. for_each_sg(urb->sg, sg, urb->num_sgs, i) {
  616. int recv_size;
  617. if (copy < sg->length)
  618. recv_size = copy;
  619. else
  620. recv_size = sg->length;
  621. recv = usbip_recv(ud->tcp_socket, sg_virt(sg),
  622. recv_size);
  623. if (recv != recv_size)
  624. goto error;
  625. copy -= recv;
  626. ret += recv;
  627. if (!copy)
  628. break;
  629. }
  630. if (ret != size)
  631. goto error;
  632. } else {
  633. ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
  634. if (ret != size)
  635. goto error;
  636. }
  637. return ret;
  638. error:
  639. dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
  640. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
  641. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  642. else
  643. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  644. return -EPIPE;
  645. }
  646. EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
  647. static int __init usbip_core_init(void)
  648. {
  649. return usbip_init_eh();
  650. }
  651. static void __exit usbip_core_exit(void)
  652. {
  653. usbip_finish_eh();
  654. return;
  655. }
  656. module_init(usbip_core_init);
  657. module_exit(usbip_core_exit);
  658. MODULE_AUTHOR(DRIVER_AUTHOR);
  659. MODULE_DESCRIPTION(DRIVER_DESC);
  660. MODULE_LICENSE("GPL");