usbip_common.c 19 KB

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