usbip_common.c 21 KB

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