st5481_usb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. * Driver for ST5481 USB ISDN modem
  3. *
  4. * Author Frode Isaksen
  5. * Copyright 2001 by Frode Isaksen <fisaksen@bewan.com>
  6. * 2001 by Kai Germaschewski <kai.germaschewski@gmx.de>
  7. *
  8. * This software may be used and distributed according to the terms
  9. * of the GNU General Public License, incorporated herein by reference.
  10. *
  11. */
  12. #include <linux/init.h>
  13. #include <linux/usb.h>
  14. #include <linux/slab.h>
  15. #include "st5481.h"
  16. static int st5481_isoc_flatten(struct urb *urb);
  17. /* ======================================================================
  18. * control pipe
  19. */
  20. /*
  21. * Send the next endpoint 0 request stored in the FIFO.
  22. * Called either by the completion or by usb_ctrl_msg.
  23. */
  24. static void usb_next_ctrl_msg(struct urb *urb,
  25. struct st5481_adapter *adapter)
  26. {
  27. struct st5481_ctrl *ctrl = &adapter->ctrl;
  28. int r_index;
  29. if (test_and_set_bit(0, &ctrl->busy)) {
  30. return;
  31. }
  32. if ((r_index = fifo_remove(&ctrl->msg_fifo.f)) < 0) {
  33. test_and_clear_bit(0, &ctrl->busy);
  34. return;
  35. }
  36. urb->setup_packet =
  37. (unsigned char *)&ctrl->msg_fifo.data[r_index];
  38. DBG(1, "request=0x%02x,value=0x%04x,index=%x",
  39. ((struct ctrl_msg *)urb->setup_packet)->dr.bRequest,
  40. ((struct ctrl_msg *)urb->setup_packet)->dr.wValue,
  41. ((struct ctrl_msg *)urb->setup_packet)->dr.wIndex);
  42. // Prepare the URB
  43. urb->dev = adapter->usb_dev;
  44. SUBMIT_URB(urb, GFP_ATOMIC);
  45. }
  46. /*
  47. * Asynchronous endpoint 0 request (async version of usb_control_msg).
  48. * The request will be queued up in a FIFO if the endpoint is busy.
  49. */
  50. static void usb_ctrl_msg(struct st5481_adapter *adapter,
  51. u8 request, u8 requesttype, u16 value, u16 index,
  52. ctrl_complete_t complete, void *context)
  53. {
  54. struct st5481_ctrl *ctrl = &adapter->ctrl;
  55. int w_index;
  56. struct ctrl_msg *ctrl_msg;
  57. if ((w_index = fifo_add(&ctrl->msg_fifo.f)) < 0) {
  58. WARNING("control msg FIFO full");
  59. return;
  60. }
  61. ctrl_msg = &ctrl->msg_fifo.data[w_index];
  62. ctrl_msg->dr.bRequestType = requesttype;
  63. ctrl_msg->dr.bRequest = request;
  64. ctrl_msg->dr.wValue = cpu_to_le16p(&value);
  65. ctrl_msg->dr.wIndex = cpu_to_le16p(&index);
  66. ctrl_msg->dr.wLength = 0;
  67. ctrl_msg->complete = complete;
  68. ctrl_msg->context = context;
  69. usb_next_ctrl_msg(ctrl->urb, adapter);
  70. }
  71. /*
  72. * Asynchronous endpoint 0 device request.
  73. */
  74. void st5481_usb_device_ctrl_msg(struct st5481_adapter *adapter,
  75. u8 request, u16 value,
  76. ctrl_complete_t complete, void *context)
  77. {
  78. usb_ctrl_msg(adapter, request,
  79. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  80. value, 0, complete, context);
  81. }
  82. /*
  83. * Asynchronous pipe reset (async version of usb_clear_halt).
  84. */
  85. void st5481_usb_pipe_reset(struct st5481_adapter *adapter,
  86. u_char pipe,
  87. ctrl_complete_t complete, void *context)
  88. {
  89. DBG(1, "pipe=%02x", pipe);
  90. usb_ctrl_msg(adapter,
  91. USB_REQ_CLEAR_FEATURE, USB_DIR_OUT | USB_RECIP_ENDPOINT,
  92. 0, pipe, complete, context);
  93. }
  94. /*
  95. Physical level functions
  96. */
  97. void st5481_ph_command(struct st5481_adapter *adapter, unsigned int command)
  98. {
  99. DBG(8, "command=%s", ST5481_CMD_string(command));
  100. st5481_usb_device_ctrl_msg(adapter, TXCI, command, NULL, NULL);
  101. }
  102. /*
  103. * The request on endpoint 0 has completed.
  104. * Call the user provided completion routine and try
  105. * to send the next request.
  106. */
  107. static void usb_ctrl_complete(struct urb *urb)
  108. {
  109. struct st5481_adapter *adapter = urb->context;
  110. struct st5481_ctrl *ctrl = &adapter->ctrl;
  111. struct ctrl_msg *ctrl_msg;
  112. if (unlikely(urb->status < 0)) {
  113. switch (urb->status) {
  114. case -ENOENT:
  115. case -ESHUTDOWN:
  116. case -ECONNRESET:
  117. DBG(1, "urb killed status %d", urb->status);
  118. return; // Give up
  119. default:
  120. WARNING("urb status %d", urb->status);
  121. break;
  122. }
  123. }
  124. ctrl_msg = (struct ctrl_msg *)urb->setup_packet;
  125. if (ctrl_msg->dr.bRequest == USB_REQ_CLEAR_FEATURE) {
  126. /* Special case handling for pipe reset */
  127. le16_to_cpus(&ctrl_msg->dr.wIndex);
  128. usb_reset_endpoint(adapter->usb_dev, ctrl_msg->dr.wIndex);
  129. }
  130. if (ctrl_msg->complete)
  131. ctrl_msg->complete(ctrl_msg->context);
  132. clear_bit(0, &ctrl->busy);
  133. // Try to send next control message
  134. usb_next_ctrl_msg(urb, adapter);
  135. return;
  136. }
  137. /* ======================================================================
  138. * interrupt pipe
  139. */
  140. /*
  141. * The interrupt endpoint will be called when any
  142. * of the 6 registers changes state (depending on masks).
  143. * Decode the register values and schedule a private event.
  144. * Called at interrupt.
  145. */
  146. static void usb_int_complete(struct urb *urb)
  147. {
  148. u8 *data = urb->transfer_buffer;
  149. u8 irqbyte;
  150. struct st5481_adapter *adapter = urb->context;
  151. int j;
  152. int status;
  153. switch (urb->status) {
  154. case 0:
  155. /* success */
  156. break;
  157. case -ECONNRESET:
  158. case -ENOENT:
  159. case -ESHUTDOWN:
  160. /* this urb is terminated, clean up */
  161. DBG(2, "urb shutting down with status: %d", urb->status);
  162. return;
  163. default:
  164. WARNING("nonzero urb status received: %d", urb->status);
  165. goto exit;
  166. }
  167. DBG_PACKET(2, data, INT_PKT_SIZE);
  168. if (urb->actual_length == 0) {
  169. goto exit;
  170. }
  171. irqbyte = data[MPINT];
  172. if (irqbyte & DEN_INT)
  173. FsmEvent(&adapter->d_out.fsm, EV_DOUT_DEN, NULL);
  174. if (irqbyte & DCOLL_INT)
  175. FsmEvent(&adapter->d_out.fsm, EV_DOUT_COLL, NULL);
  176. irqbyte = data[FFINT_D];
  177. if (irqbyte & OUT_UNDERRUN)
  178. FsmEvent(&adapter->d_out.fsm, EV_DOUT_UNDERRUN, NULL);
  179. if (irqbyte & OUT_DOWN)
  180. ;// printk("OUT_DOWN\n");
  181. irqbyte = data[MPINT];
  182. if (irqbyte & RXCI_INT)
  183. FsmEvent(&adapter->l1m, data[CCIST] & 0x0f, NULL);
  184. for (j = 0; j < 2; j++)
  185. adapter->bcs[j].b_out.flow_event |= data[FFINT_B1 + j];
  186. urb->actual_length = 0;
  187. exit:
  188. status = usb_submit_urb(urb, GFP_ATOMIC);
  189. if (status)
  190. WARNING("usb_submit_urb failed with result %d", status);
  191. }
  192. /* ======================================================================
  193. * initialization
  194. */
  195. int st5481_setup_usb(struct st5481_adapter *adapter)
  196. {
  197. struct usb_device *dev = adapter->usb_dev;
  198. struct st5481_ctrl *ctrl = &adapter->ctrl;
  199. struct st5481_intr *intr = &adapter->intr;
  200. struct usb_interface *intf;
  201. struct usb_host_interface *altsetting = NULL;
  202. struct usb_host_endpoint *endpoint;
  203. int status;
  204. struct urb *urb;
  205. u8 *buf;
  206. DBG(2, "");
  207. if ((status = usb_reset_configuration(dev)) < 0) {
  208. WARNING("reset_configuration failed,status=%d", status);
  209. return status;
  210. }
  211. intf = usb_ifnum_to_if(dev, 0);
  212. if (intf)
  213. altsetting = usb_altnum_to_altsetting(intf, 3);
  214. if (!altsetting)
  215. return -ENXIO;
  216. // Check if the config is sane
  217. if (altsetting->desc.bNumEndpoints != 7) {
  218. WARNING("expecting 7 got %d endpoints!", altsetting->desc.bNumEndpoints);
  219. return -EINVAL;
  220. }
  221. // The descriptor is wrong for some early samples of the ST5481 chip
  222. altsetting->endpoint[3].desc.wMaxPacketSize = cpu_to_le16(32);
  223. altsetting->endpoint[4].desc.wMaxPacketSize = cpu_to_le16(32);
  224. // Use alternative setting 3 on interface 0 to have 2B+D
  225. if ((status = usb_set_interface(dev, 0, 3)) < 0) {
  226. WARNING("usb_set_interface failed,status=%d", status);
  227. return status;
  228. }
  229. // Allocate URB for control endpoint
  230. urb = usb_alloc_urb(0, GFP_KERNEL);
  231. if (!urb) {
  232. return -ENOMEM;
  233. }
  234. ctrl->urb = urb;
  235. // Fill the control URB
  236. usb_fill_control_urb(urb, dev,
  237. usb_sndctrlpipe(dev, 0),
  238. NULL, NULL, 0, usb_ctrl_complete, adapter);
  239. fifo_init(&ctrl->msg_fifo.f, ARRAY_SIZE(ctrl->msg_fifo.data));
  240. // Allocate URBs and buffers for interrupt endpoint
  241. urb = usb_alloc_urb(0, GFP_KERNEL);
  242. if (!urb) {
  243. goto err1;
  244. }
  245. intr->urb = urb;
  246. buf = kmalloc(INT_PKT_SIZE, GFP_KERNEL);
  247. if (!buf) {
  248. goto err2;
  249. }
  250. endpoint = &altsetting->endpoint[EP_INT-1];
  251. // Fill the interrupt URB
  252. usb_fill_int_urb(urb, dev,
  253. usb_rcvintpipe(dev, endpoint->desc.bEndpointAddress),
  254. buf, INT_PKT_SIZE,
  255. usb_int_complete, adapter,
  256. endpoint->desc.bInterval);
  257. return 0;
  258. err2:
  259. usb_free_urb(intr->urb);
  260. intr->urb = NULL;
  261. err1:
  262. usb_free_urb(ctrl->urb);
  263. ctrl->urb = NULL;
  264. return -ENOMEM;
  265. }
  266. /*
  267. * Release buffers and URBs for the interrupt and control
  268. * endpoint.
  269. */
  270. void st5481_release_usb(struct st5481_adapter *adapter)
  271. {
  272. struct st5481_intr *intr = &adapter->intr;
  273. struct st5481_ctrl *ctrl = &adapter->ctrl;
  274. DBG(1, "");
  275. // Stop and free Control and Interrupt URBs
  276. usb_kill_urb(ctrl->urb);
  277. kfree(ctrl->urb->transfer_buffer);
  278. usb_free_urb(ctrl->urb);
  279. ctrl->urb = NULL;
  280. usb_kill_urb(intr->urb);
  281. kfree(intr->urb->transfer_buffer);
  282. usb_free_urb(intr->urb);
  283. intr->urb = NULL;
  284. }
  285. /*
  286. * Initialize the adapter.
  287. */
  288. void st5481_start(struct st5481_adapter *adapter)
  289. {
  290. static const u8 init_cmd_table[] = {
  291. SET_DEFAULT, 0,
  292. STT, 0,
  293. SDA_MIN, 0x0d,
  294. SDA_MAX, 0x29,
  295. SDELAY_VALUE, 0x14,
  296. GPIO_DIR, 0x01,
  297. GPIO_OUT, RED_LED,
  298. // FFCTRL_OUT_D,4,
  299. // FFCTRH_OUT_D,12,
  300. FFCTRL_OUT_B1, 6,
  301. FFCTRH_OUT_B1, 20,
  302. FFCTRL_OUT_B2, 6,
  303. FFCTRH_OUT_B2, 20,
  304. MPMSK, RXCI_INT + DEN_INT + DCOLL_INT,
  305. 0
  306. };
  307. struct st5481_intr *intr = &adapter->intr;
  308. int i = 0;
  309. u8 request, value;
  310. DBG(8, "");
  311. adapter->leds = RED_LED;
  312. // Start receiving on the interrupt endpoint
  313. SUBMIT_URB(intr->urb, GFP_KERNEL);
  314. while ((request = init_cmd_table[i++])) {
  315. value = init_cmd_table[i++];
  316. st5481_usb_device_ctrl_msg(adapter, request, value, NULL, NULL);
  317. }
  318. st5481_ph_command(adapter, ST5481_CMD_PUP);
  319. }
  320. /*
  321. * Reset the adapter to default values.
  322. */
  323. void st5481_stop(struct st5481_adapter *adapter)
  324. {
  325. DBG(8, "");
  326. st5481_usb_device_ctrl_msg(adapter, SET_DEFAULT, 0, NULL, NULL);
  327. }
  328. /* ======================================================================
  329. * isochronous USB helpers
  330. */
  331. static void
  332. fill_isoc_urb(struct urb *urb, struct usb_device *dev,
  333. unsigned int pipe, void *buf, int num_packets,
  334. int packet_size, usb_complete_t complete,
  335. void *context)
  336. {
  337. int k;
  338. urb->dev = dev;
  339. urb->pipe = pipe;
  340. urb->interval = 1;
  341. urb->transfer_buffer = buf;
  342. urb->number_of_packets = num_packets;
  343. urb->transfer_buffer_length = num_packets * packet_size;
  344. urb->actual_length = 0;
  345. urb->complete = complete;
  346. urb->context = context;
  347. urb->transfer_flags = URB_ISO_ASAP;
  348. for (k = 0; k < num_packets; k++) {
  349. urb->iso_frame_desc[k].offset = packet_size * k;
  350. urb->iso_frame_desc[k].length = packet_size;
  351. urb->iso_frame_desc[k].actual_length = 0;
  352. }
  353. }
  354. int
  355. st5481_setup_isocpipes(struct urb *urb[2], struct usb_device *dev,
  356. unsigned int pipe, int num_packets,
  357. int packet_size, int buf_size,
  358. usb_complete_t complete, void *context)
  359. {
  360. int j, retval;
  361. unsigned char *buf;
  362. for (j = 0; j < 2; j++) {
  363. retval = -ENOMEM;
  364. urb[j] = usb_alloc_urb(num_packets, GFP_KERNEL);
  365. if (!urb[j])
  366. goto err;
  367. // Allocate memory for 2000bytes/sec (16Kb/s)
  368. buf = kmalloc(buf_size, GFP_KERNEL);
  369. if (!buf)
  370. goto err;
  371. // Fill the isochronous URB
  372. fill_isoc_urb(urb[j], dev, pipe, buf,
  373. num_packets, packet_size, complete,
  374. context);
  375. }
  376. return 0;
  377. err:
  378. for (j = 0; j < 2; j++) {
  379. if (urb[j]) {
  380. kfree(urb[j]->transfer_buffer);
  381. urb[j]->transfer_buffer = NULL;
  382. usb_free_urb(urb[j]);
  383. urb[j] = NULL;
  384. }
  385. }
  386. return retval;
  387. }
  388. void st5481_release_isocpipes(struct urb *urb[2])
  389. {
  390. int j;
  391. for (j = 0; j < 2; j++) {
  392. usb_kill_urb(urb[j]);
  393. kfree(urb[j]->transfer_buffer);
  394. usb_free_urb(urb[j]);
  395. urb[j] = NULL;
  396. }
  397. }
  398. /*
  399. * Decode frames received on the B/D channel.
  400. * Note that this function will be called continuously
  401. * with 64Kbit/s / 16Kbit/s of data and hence it will be
  402. * called 50 times per second with 20 ISOC descriptors.
  403. * Called at interrupt.
  404. */
  405. static void usb_in_complete(struct urb *urb)
  406. {
  407. struct st5481_in *in = urb->context;
  408. unsigned char *ptr;
  409. struct sk_buff *skb;
  410. int len, count, status;
  411. if (unlikely(urb->status < 0)) {
  412. switch (urb->status) {
  413. case -ENOENT:
  414. case -ESHUTDOWN:
  415. case -ECONNRESET:
  416. DBG(1, "urb killed status %d", urb->status);
  417. return; // Give up
  418. default:
  419. WARNING("urb status %d", urb->status);
  420. break;
  421. }
  422. }
  423. DBG_ISO_PACKET(0x80, urb);
  424. len = st5481_isoc_flatten(urb);
  425. ptr = urb->transfer_buffer;
  426. while (len > 0) {
  427. if (in->mode == L1_MODE_TRANS) {
  428. memcpy(in->rcvbuf, ptr, len);
  429. status = len;
  430. len = 0;
  431. } else {
  432. status = isdnhdlc_decode(&in->hdlc_state, ptr, len, &count,
  433. in->rcvbuf, in->bufsize);
  434. ptr += count;
  435. len -= count;
  436. }
  437. if (status > 0) {
  438. // Good frame received
  439. DBG(4, "count=%d", status);
  440. DBG_PACKET(0x400, in->rcvbuf, status);
  441. if (!(skb = dev_alloc_skb(status))) {
  442. WARNING("receive out of memory\n");
  443. break;
  444. }
  445. memcpy(skb_put(skb, status), in->rcvbuf, status);
  446. in->hisax_if->l1l2(in->hisax_if, PH_DATA | INDICATION, skb);
  447. } else if (status == -HDLC_CRC_ERROR) {
  448. INFO("CRC error");
  449. } else if (status == -HDLC_FRAMING_ERROR) {
  450. INFO("framing error");
  451. } else if (status == -HDLC_LENGTH_ERROR) {
  452. INFO("length error");
  453. }
  454. }
  455. // Prepare URB for next transfer
  456. urb->dev = in->adapter->usb_dev;
  457. urb->actual_length = 0;
  458. SUBMIT_URB(urb, GFP_ATOMIC);
  459. }
  460. int st5481_setup_in(struct st5481_in *in)
  461. {
  462. struct usb_device *dev = in->adapter->usb_dev;
  463. int retval;
  464. DBG(4, "");
  465. in->rcvbuf = kmalloc(in->bufsize, GFP_KERNEL);
  466. retval = -ENOMEM;
  467. if (!in->rcvbuf)
  468. goto err;
  469. retval = st5481_setup_isocpipes(in->urb, dev,
  470. usb_rcvisocpipe(dev, in->ep),
  471. in->num_packets, in->packet_size,
  472. in->num_packets * in->packet_size,
  473. usb_in_complete, in);
  474. if (retval)
  475. goto err_free;
  476. return 0;
  477. err_free:
  478. kfree(in->rcvbuf);
  479. err:
  480. return retval;
  481. }
  482. void st5481_release_in(struct st5481_in *in)
  483. {
  484. DBG(2, "");
  485. st5481_release_isocpipes(in->urb);
  486. }
  487. /*
  488. * Make the transfer_buffer contiguous by
  489. * copying from the iso descriptors if necessary.
  490. */
  491. static int st5481_isoc_flatten(struct urb *urb)
  492. {
  493. struct usb_iso_packet_descriptor *pipd, *pend;
  494. unsigned char *src, *dst;
  495. unsigned int len;
  496. if (urb->status < 0) {
  497. return urb->status;
  498. }
  499. for (pipd = &urb->iso_frame_desc[0],
  500. pend = &urb->iso_frame_desc[urb->number_of_packets],
  501. dst = urb->transfer_buffer;
  502. pipd < pend;
  503. pipd++) {
  504. if (pipd->status < 0) {
  505. return (pipd->status);
  506. }
  507. len = pipd->actual_length;
  508. pipd->actual_length = 0;
  509. src = urb->transfer_buffer + pipd->offset;
  510. if (src != dst) {
  511. // Need to copy since isoc buffers not full
  512. while (len--) {
  513. *dst++ = *src++;
  514. }
  515. } else {
  516. // No need to copy, just update destination buffer
  517. dst += len;
  518. }
  519. }
  520. // Return size of flattened buffer
  521. return (dst - (unsigned char *)urb->transfer_buffer);
  522. }
  523. static void st5481_start_rcv(void *context)
  524. {
  525. struct st5481_in *in = context;
  526. struct st5481_adapter *adapter = in->adapter;
  527. DBG(4, "");
  528. in->urb[0]->dev = adapter->usb_dev;
  529. SUBMIT_URB(in->urb[0], GFP_KERNEL);
  530. in->urb[1]->dev = adapter->usb_dev;
  531. SUBMIT_URB(in->urb[1], GFP_KERNEL);
  532. }
  533. void st5481_in_mode(struct st5481_in *in, int mode)
  534. {
  535. if (in->mode == mode)
  536. return;
  537. in->mode = mode;
  538. usb_unlink_urb(in->urb[0]);
  539. usb_unlink_urb(in->urb[1]);
  540. if (in->mode != L1_MODE_NULL) {
  541. if (in->mode != L1_MODE_TRANS) {
  542. u32 features = HDLC_BITREVERSE;
  543. if (in->mode == L1_MODE_HDLC_56K)
  544. features |= HDLC_56KBIT;
  545. isdnhdlc_rcv_init(&in->hdlc_state, features);
  546. }
  547. st5481_usb_pipe_reset(in->adapter, in->ep, NULL, NULL);
  548. st5481_usb_device_ctrl_msg(in->adapter, in->counter,
  549. in->packet_size,
  550. NULL, NULL);
  551. st5481_start_rcv(in);
  552. } else {
  553. st5481_usb_device_ctrl_msg(in->adapter, in->counter,
  554. 0, NULL, NULL);
  555. }
  556. }