usbip_common.c 19 KB

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