usbip_common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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, ITER_DEST, &iov, 1, size);
  272. usbip_dbg_xmit("enter\n");
  273. do {
  274. sock->sk->sk_allocation = GFP_NOIO;
  275. sock->sk->sk_use_task_frag = false;
  276. result = sock_recvmsg(sock, &msg, MSG_WAITALL);
  277. if (result <= 0)
  278. goto err;
  279. total += result;
  280. } while (msg_data_left(&msg));
  281. if (usbip_dbg_flag_xmit) {
  282. pr_debug("receiving....\n");
  283. usbip_dump_buffer(buf, size);
  284. pr_debug("received, osize %d ret %d size %zd total %d\n",
  285. size, result, msg_data_left(&msg), total);
  286. }
  287. return total;
  288. err:
  289. return result;
  290. }
  291. EXPORT_SYMBOL_GPL(usbip_recv);
  292. /* there may be more cases to tweak the flags. */
  293. static unsigned int tweak_transfer_flags(unsigned int flags)
  294. {
  295. flags &= ~URB_NO_TRANSFER_DMA_MAP;
  296. return flags;
  297. }
  298. /*
  299. * USBIP driver packs URB transfer flags in PDUs that are exchanged
  300. * between Server (usbip_host) and Client (vhci_hcd). URB_* flags
  301. * are internal to kernel and could change. Where as USBIP URB flags
  302. * exchanged in PDUs are USBIP user API must not change.
  303. *
  304. * USBIP_URB* flags are exported as explicit API and client and server
  305. * do mapping from kernel flags to USBIP_URB*. Details as follows:
  306. *
  307. * Client tx path (USBIP_CMD_SUBMIT):
  308. * - Maps URB_* to USBIP_URB_* when it sends USBIP_CMD_SUBMIT packet.
  309. *
  310. * Server rx path (USBIP_CMD_SUBMIT):
  311. * - Maps USBIP_URB_* to URB_* when it receives USBIP_CMD_SUBMIT packet.
  312. *
  313. * Flags aren't included in USBIP_CMD_UNLINK and USBIP_RET_SUBMIT packets
  314. * and no special handling is needed for them in the following cases:
  315. * - Server rx path (USBIP_CMD_UNLINK)
  316. * - Client rx path & Server tx path (USBIP_RET_SUBMIT)
  317. *
  318. * Code paths:
  319. * usbip_pack_pdu() is the common routine that handles packing pdu from
  320. * urb and unpack pdu to an urb.
  321. *
  322. * usbip_pack_cmd_submit() and usbip_pack_ret_submit() handle
  323. * USBIP_CMD_SUBMIT and USBIP_RET_SUBMIT respectively.
  324. *
  325. * usbip_map_urb_to_usbip() and usbip_map_usbip_to_urb() are used
  326. * by usbip_pack_cmd_submit() and usbip_pack_ret_submit() to map
  327. * flags.
  328. */
  329. struct urb_to_usbip_flags {
  330. u32 urb_flag;
  331. u32 usbip_flag;
  332. };
  333. #define NUM_USBIP_FLAGS 17
  334. static const struct urb_to_usbip_flags flag_map[NUM_USBIP_FLAGS] = {
  335. {URB_SHORT_NOT_OK, USBIP_URB_SHORT_NOT_OK},
  336. {URB_ISO_ASAP, USBIP_URB_ISO_ASAP},
  337. {URB_NO_TRANSFER_DMA_MAP, USBIP_URB_NO_TRANSFER_DMA_MAP},
  338. {URB_ZERO_PACKET, USBIP_URB_ZERO_PACKET},
  339. {URB_NO_INTERRUPT, USBIP_URB_NO_INTERRUPT},
  340. {URB_FREE_BUFFER, USBIP_URB_FREE_BUFFER},
  341. {URB_DIR_IN, USBIP_URB_DIR_IN},
  342. {URB_DIR_OUT, USBIP_URB_DIR_OUT},
  343. {URB_DIR_MASK, USBIP_URB_DIR_MASK},
  344. {URB_DMA_MAP_SINGLE, USBIP_URB_DMA_MAP_SINGLE},
  345. {URB_DMA_MAP_PAGE, USBIP_URB_DMA_MAP_PAGE},
  346. {URB_DMA_MAP_SG, USBIP_URB_DMA_MAP_SG},
  347. {URB_MAP_LOCAL, USBIP_URB_MAP_LOCAL},
  348. {URB_SETUP_MAP_SINGLE, USBIP_URB_SETUP_MAP_SINGLE},
  349. {URB_SETUP_MAP_LOCAL, USBIP_URB_SETUP_MAP_LOCAL},
  350. {URB_DMA_SG_COMBINED, USBIP_URB_DMA_SG_COMBINED},
  351. {URB_ALIGNED_TEMP_BUFFER, USBIP_URB_ALIGNED_TEMP_BUFFER},
  352. };
  353. static unsigned int urb_to_usbip(unsigned int flags)
  354. {
  355. unsigned int map_flags = 0;
  356. int loop;
  357. for (loop = 0; loop < NUM_USBIP_FLAGS; loop++) {
  358. if (flags & flag_map[loop].urb_flag)
  359. map_flags |= flag_map[loop].usbip_flag;
  360. }
  361. return map_flags;
  362. }
  363. static unsigned int usbip_to_urb(unsigned int flags)
  364. {
  365. unsigned int map_flags = 0;
  366. int loop;
  367. for (loop = 0; loop < NUM_USBIP_FLAGS; loop++) {
  368. if (flags & flag_map[loop].usbip_flag)
  369. map_flags |= flag_map[loop].urb_flag;
  370. }
  371. return map_flags;
  372. }
  373. static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
  374. int pack)
  375. {
  376. struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
  377. /*
  378. * Some members are not still implemented in usbip. I hope this issue
  379. * will be discussed when usbip is ported to other operating systems.
  380. */
  381. if (pack) {
  382. /* map after tweaking the urb flags */
  383. spdu->transfer_flags = urb_to_usbip(tweak_transfer_flags(urb->transfer_flags));
  384. spdu->transfer_buffer_length = urb->transfer_buffer_length;
  385. spdu->start_frame = urb->start_frame;
  386. spdu->number_of_packets = urb->number_of_packets;
  387. spdu->interval = urb->interval;
  388. } else {
  389. urb->transfer_flags = usbip_to_urb(spdu->transfer_flags);
  390. urb->transfer_buffer_length = spdu->transfer_buffer_length;
  391. urb->start_frame = spdu->start_frame;
  392. urb->number_of_packets = spdu->number_of_packets;
  393. urb->interval = spdu->interval;
  394. }
  395. }
  396. static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
  397. int pack)
  398. {
  399. struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
  400. if (pack) {
  401. rpdu->status = urb->status;
  402. rpdu->actual_length = urb->actual_length;
  403. rpdu->start_frame = urb->start_frame;
  404. rpdu->number_of_packets = urb->number_of_packets;
  405. rpdu->error_count = urb->error_count;
  406. } else {
  407. urb->status = rpdu->status;
  408. urb->actual_length = rpdu->actual_length;
  409. urb->start_frame = rpdu->start_frame;
  410. urb->number_of_packets = rpdu->number_of_packets;
  411. urb->error_count = rpdu->error_count;
  412. }
  413. }
  414. void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
  415. int pack)
  416. {
  417. switch (cmd) {
  418. case USBIP_CMD_SUBMIT:
  419. usbip_pack_cmd_submit(pdu, urb, pack);
  420. break;
  421. case USBIP_RET_SUBMIT:
  422. usbip_pack_ret_submit(pdu, urb, pack);
  423. break;
  424. default:
  425. /* NOT REACHED */
  426. pr_err("unknown command\n");
  427. break;
  428. }
  429. }
  430. EXPORT_SYMBOL_GPL(usbip_pack_pdu);
  431. static void correct_endian_basic(struct usbip_header_basic *base, int send)
  432. {
  433. if (send) {
  434. base->command = cpu_to_be32(base->command);
  435. base->seqnum = cpu_to_be32(base->seqnum);
  436. base->devid = cpu_to_be32(base->devid);
  437. base->direction = cpu_to_be32(base->direction);
  438. base->ep = cpu_to_be32(base->ep);
  439. } else {
  440. base->command = be32_to_cpu(base->command);
  441. base->seqnum = be32_to_cpu(base->seqnum);
  442. base->devid = be32_to_cpu(base->devid);
  443. base->direction = be32_to_cpu(base->direction);
  444. base->ep = be32_to_cpu(base->ep);
  445. }
  446. }
  447. static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
  448. int send)
  449. {
  450. if (send) {
  451. pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
  452. cpu_to_be32s(&pdu->transfer_buffer_length);
  453. cpu_to_be32s(&pdu->start_frame);
  454. cpu_to_be32s(&pdu->number_of_packets);
  455. cpu_to_be32s(&pdu->interval);
  456. } else {
  457. pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
  458. be32_to_cpus(&pdu->transfer_buffer_length);
  459. be32_to_cpus(&pdu->start_frame);
  460. be32_to_cpus(&pdu->number_of_packets);
  461. be32_to_cpus(&pdu->interval);
  462. }
  463. }
  464. static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
  465. int send)
  466. {
  467. if (send) {
  468. cpu_to_be32s(&pdu->status);
  469. cpu_to_be32s(&pdu->actual_length);
  470. cpu_to_be32s(&pdu->start_frame);
  471. cpu_to_be32s(&pdu->number_of_packets);
  472. cpu_to_be32s(&pdu->error_count);
  473. } else {
  474. be32_to_cpus(&pdu->status);
  475. be32_to_cpus(&pdu->actual_length);
  476. be32_to_cpus(&pdu->start_frame);
  477. be32_to_cpus(&pdu->number_of_packets);
  478. be32_to_cpus(&pdu->error_count);
  479. }
  480. }
  481. static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
  482. int send)
  483. {
  484. if (send)
  485. pdu->seqnum = cpu_to_be32(pdu->seqnum);
  486. else
  487. pdu->seqnum = be32_to_cpu(pdu->seqnum);
  488. }
  489. static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
  490. int send)
  491. {
  492. if (send)
  493. cpu_to_be32s(&pdu->status);
  494. else
  495. be32_to_cpus(&pdu->status);
  496. }
  497. void usbip_header_correct_endian(struct usbip_header *pdu, int send)
  498. {
  499. __u32 cmd = 0;
  500. if (send)
  501. cmd = pdu->base.command;
  502. correct_endian_basic(&pdu->base, send);
  503. if (!send)
  504. cmd = pdu->base.command;
  505. switch (cmd) {
  506. case USBIP_CMD_SUBMIT:
  507. correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
  508. break;
  509. case USBIP_RET_SUBMIT:
  510. correct_endian_ret_submit(&pdu->u.ret_submit, send);
  511. break;
  512. case USBIP_CMD_UNLINK:
  513. correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
  514. break;
  515. case USBIP_RET_UNLINK:
  516. correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
  517. break;
  518. default:
  519. /* NOT REACHED */
  520. pr_err("unknown command\n");
  521. break;
  522. }
  523. }
  524. EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
  525. static void usbip_iso_packet_correct_endian(
  526. struct usbip_iso_packet_descriptor *iso, int send)
  527. {
  528. /* does not need all members. but copy all simply. */
  529. if (send) {
  530. iso->offset = cpu_to_be32(iso->offset);
  531. iso->length = cpu_to_be32(iso->length);
  532. iso->status = cpu_to_be32(iso->status);
  533. iso->actual_length = cpu_to_be32(iso->actual_length);
  534. } else {
  535. iso->offset = be32_to_cpu(iso->offset);
  536. iso->length = be32_to_cpu(iso->length);
  537. iso->status = be32_to_cpu(iso->status);
  538. iso->actual_length = be32_to_cpu(iso->actual_length);
  539. }
  540. }
  541. static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
  542. struct usb_iso_packet_descriptor *uiso, int pack)
  543. {
  544. if (pack) {
  545. iso->offset = uiso->offset;
  546. iso->length = uiso->length;
  547. iso->status = uiso->status;
  548. iso->actual_length = uiso->actual_length;
  549. } else {
  550. uiso->offset = iso->offset;
  551. uiso->length = iso->length;
  552. uiso->status = iso->status;
  553. uiso->actual_length = iso->actual_length;
  554. }
  555. }
  556. /* must free buffer */
  557. struct usbip_iso_packet_descriptor*
  558. usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
  559. {
  560. struct usbip_iso_packet_descriptor *iso;
  561. int np = urb->number_of_packets;
  562. ssize_t size = np * sizeof(*iso);
  563. int i;
  564. iso = kzalloc(size, GFP_KERNEL);
  565. if (!iso)
  566. return NULL;
  567. for (i = 0; i < np; i++) {
  568. usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
  569. usbip_iso_packet_correct_endian(&iso[i], 1);
  570. }
  571. *bufflen = size;
  572. return iso;
  573. }
  574. EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
  575. /* some members of urb must be substituted before. */
  576. int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
  577. {
  578. void *buff;
  579. struct usbip_iso_packet_descriptor *iso;
  580. int np = urb->number_of_packets;
  581. int size = np * sizeof(*iso);
  582. int i;
  583. int ret;
  584. int total_length = 0;
  585. if (!usb_pipeisoc(urb->pipe))
  586. return 0;
  587. /* my Bluetooth dongle gets ISO URBs which are np = 0 */
  588. if (np == 0)
  589. return 0;
  590. buff = kzalloc(size, GFP_KERNEL);
  591. if (!buff)
  592. return -ENOMEM;
  593. ret = usbip_recv(ud->tcp_socket, buff, size);
  594. if (ret != size) {
  595. dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
  596. ret);
  597. kfree(buff);
  598. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
  599. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  600. else
  601. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  602. return -EPIPE;
  603. }
  604. iso = (struct usbip_iso_packet_descriptor *) buff;
  605. for (i = 0; i < np; i++) {
  606. usbip_iso_packet_correct_endian(&iso[i], 0);
  607. usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
  608. total_length += urb->iso_frame_desc[i].actual_length;
  609. }
  610. kfree(buff);
  611. if (total_length != urb->actual_length) {
  612. dev_err(&urb->dev->dev,
  613. "total length of iso packets %d not equal to actual length of buffer %d\n",
  614. total_length, urb->actual_length);
  615. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
  616. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  617. else
  618. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  619. return -EPIPE;
  620. }
  621. return ret;
  622. }
  623. EXPORT_SYMBOL_GPL(usbip_recv_iso);
  624. /*
  625. * This functions restores the padding which was removed for optimizing
  626. * the bandwidth during transfer over tcp/ip
  627. *
  628. * buffer and iso packets need to be stored and be in propeper endian in urb
  629. * before calling this function
  630. */
  631. void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
  632. {
  633. int np = urb->number_of_packets;
  634. int i;
  635. int actualoffset = urb->actual_length;
  636. if (!usb_pipeisoc(urb->pipe))
  637. return;
  638. /* if no packets or length of data is 0, then nothing to unpack */
  639. if (np == 0 || urb->actual_length == 0)
  640. return;
  641. /*
  642. * if actual_length is transfer_buffer_length then no padding is
  643. * present.
  644. */
  645. if (urb->actual_length == urb->transfer_buffer_length)
  646. return;
  647. /*
  648. * loop over all packets from last to first (to prevent overwriting
  649. * memory when padding) and move them into the proper place
  650. */
  651. for (i = np-1; i > 0; i--) {
  652. actualoffset -= urb->iso_frame_desc[i].actual_length;
  653. memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
  654. urb->transfer_buffer + actualoffset,
  655. urb->iso_frame_desc[i].actual_length);
  656. }
  657. }
  658. EXPORT_SYMBOL_GPL(usbip_pad_iso);
  659. /* some members of urb must be substituted before. */
  660. int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
  661. {
  662. struct scatterlist *sg;
  663. int ret = 0;
  664. int recv;
  665. int size;
  666. int copy;
  667. int i;
  668. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
  669. /* the direction of urb must be OUT. */
  670. if (usb_pipein(urb->pipe))
  671. return 0;
  672. size = urb->transfer_buffer_length;
  673. } else {
  674. /* the direction of urb must be IN. */
  675. if (usb_pipeout(urb->pipe))
  676. return 0;
  677. size = urb->actual_length;
  678. }
  679. /* no need to recv xbuff */
  680. if (!(size > 0))
  681. return 0;
  682. if (size > urb->transfer_buffer_length)
  683. /* should not happen, probably malicious packet */
  684. goto error;
  685. if (urb->num_sgs) {
  686. copy = size;
  687. for_each_sg(urb->sg, sg, urb->num_sgs, i) {
  688. int recv_size;
  689. if (copy < sg->length)
  690. recv_size = copy;
  691. else
  692. recv_size = sg->length;
  693. recv = usbip_recv(ud->tcp_socket, sg_virt(sg),
  694. recv_size);
  695. if (recv != recv_size)
  696. goto error;
  697. copy -= recv;
  698. ret += recv;
  699. if (!copy)
  700. break;
  701. }
  702. if (ret != size)
  703. goto error;
  704. } else {
  705. ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
  706. if (ret != size)
  707. goto error;
  708. }
  709. return ret;
  710. error:
  711. dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
  712. if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
  713. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  714. else
  715. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  716. return -EPIPE;
  717. }
  718. EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
  719. static int __init usbip_core_init(void)
  720. {
  721. return usbip_init_eh();
  722. }
  723. static void __exit usbip_core_exit(void)
  724. {
  725. usbip_finish_eh();
  726. return;
  727. }
  728. module_init(usbip_core_init);
  729. module_exit(usbip_core_exit);
  730. MODULE_AUTHOR(DRIVER_AUTHOR);
  731. MODULE_DESCRIPTION(DRIVER_DESC);
  732. MODULE_LICENSE("GPL");