usbip_common.c 20 KB

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