ipw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * IPWireless 3G UMTS TDD Modem driver (USB connected)
  3. *
  4. * Copyright (C) 2004 Roelf Diedericks <roelfd@inet.co.za>
  5. * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * All information about the device was acquired using SnoopyPro
  13. * on MSFT's O/S, and examing the MSFT drivers' debug output
  14. * (insanely left _on_ in the enduser version)
  15. *
  16. * It was written out of frustration with the IPWireless USB modem
  17. * supplied by Axity3G/Sentech South Africa not supporting
  18. * Linux whatsoever.
  19. *
  20. * Nobody provided any proprietary information that was not already
  21. * available for this device.
  22. *
  23. * The modem adheres to the "3GPP TS 27.007 AT command set for 3G
  24. * User Equipment (UE)" standard, available from
  25. * http://www.3gpp.org/ftp/Specs/html-info/27007.htm
  26. *
  27. * The code was only tested the IPWireless handheld modem distributed
  28. * in South Africa by Sentech.
  29. *
  30. * It may work for Woosh Inc in .nz too, as it appears they use the
  31. * same kit.
  32. *
  33. * There is still some work to be done in terms of handling
  34. * DCD, DTR, RTS, CTS which are currently faked.
  35. * It's good enough for PPP at this point. It's based off all kinds of
  36. * code found in usb/serial and usb/class
  37. *
  38. */
  39. #include <linux/kernel.h>
  40. #include <linux/errno.h>
  41. #include <linux/init.h>
  42. #include <linux/slab.h>
  43. #include <linux/tty.h>
  44. #include <linux/tty_flip.h>
  45. #include <linux/module.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/usb.h>
  48. #include <linux/usb/serial.h>
  49. #include <linux/uaccess.h>
  50. /*
  51. * Version Information
  52. */
  53. #define DRIVER_VERSION "v0.3"
  54. #define DRIVER_AUTHOR "Roelf Diedericks"
  55. #define DRIVER_DESC "IPWireless tty driver"
  56. #define IPW_TTY_MAJOR 240 /* real device node major id, experimental range */
  57. #define IPW_TTY_MINORS 256 /* we support 256 devices, dunno why, it'd be insane :) */
  58. #define USB_IPW_MAGIC 0x6d02 /* magic number for ipw struct */
  59. /* Message sizes */
  60. #define EVENT_BUFFER_SIZE 0xFF
  61. #define CHAR2INT16(c1, c0) (((u32)((c1) & 0xff) << 8) + (u32)((c0) & 0xff))
  62. #define NUM_BULK_URBS 24
  63. #define NUM_CONTROL_URBS 16
  64. /* vendor/product pairs that are known work with this driver*/
  65. #define IPW_VID 0x0bc3
  66. #define IPW_PID 0x0001
  67. /* Vendor commands: */
  68. /* baud rates */
  69. enum {
  70. ipw_sio_b256000 = 0x000e,
  71. ipw_sio_b128000 = 0x001d,
  72. ipw_sio_b115200 = 0x0020,
  73. ipw_sio_b57600 = 0x0040,
  74. ipw_sio_b56000 = 0x0042,
  75. ipw_sio_b38400 = 0x0060,
  76. ipw_sio_b19200 = 0x00c0,
  77. ipw_sio_b14400 = 0x0100,
  78. ipw_sio_b9600 = 0x0180,
  79. ipw_sio_b4800 = 0x0300,
  80. ipw_sio_b2400 = 0x0600,
  81. ipw_sio_b1200 = 0x0c00,
  82. ipw_sio_b600 = 0x1800
  83. };
  84. /* data bits */
  85. #define ipw_dtb_7 0x700
  86. #define ipw_dtb_8 0x810 /* ok so the define is misleading, I know, but forces 8,n,1 */
  87. /* I mean, is there a point to any other setting these days? :) */
  88. /* usb control request types : */
  89. #define IPW_SIO_RXCTL 0x00 /* control bulk rx channel transmissions, value=1/0 (on/off) */
  90. #define IPW_SIO_SET_BAUD 0x01 /* set baud, value=requested ipw_sio_bxxxx */
  91. #define IPW_SIO_SET_LINE 0x03 /* set databits, parity. value=ipw_dtb_x */
  92. #define IPW_SIO_SET_PIN 0x03 /* set/clear dtr/rts value=ipw_pin_xxx */
  93. #define IPW_SIO_POLL 0x08 /* get serial port status byte, call with value=0 */
  94. #define IPW_SIO_INIT 0x11 /* initializes ? value=0 (appears as first thing todo on open) */
  95. #define IPW_SIO_PURGE 0x12 /* purge all transmissions?, call with value=numchar_to_purge */
  96. #define IPW_SIO_HANDFLOW 0x13 /* set xon/xoff limits value=0, and a buffer of 0x10 bytes */
  97. #define IPW_SIO_SETCHARS 0x13 /* set the flowcontrol special chars, value=0, buf=6 bytes, */
  98. /* last 2 bytes contain flowcontrol chars e.g. 00 00 00 00 11 13 */
  99. /* values used for request IPW_SIO_SET_PIN */
  100. #define IPW_PIN_SETDTR 0x101
  101. #define IPW_PIN_SETRTS 0x202
  102. #define IPW_PIN_CLRDTR 0x100
  103. #define IPW_PIN_CLRRTS 0x200 /* unconfirmed */
  104. /* values used for request IPW_SIO_RXCTL */
  105. #define IPW_RXBULK_ON 1
  106. #define IPW_RXBULK_OFF 0
  107. /* various 16 byte hardcoded transferbuffers used by flow control */
  108. #define IPW_BYTES_FLOWINIT { 0x01, 0, 0, 0, 0x40, 0, 0, 0, \
  109. 0, 0, 0, 0, 0, 0, 0, 0 }
  110. /* Interpretation of modem status lines */
  111. /* These need sorting out by individually connecting pins and checking
  112. * results. FIXME!
  113. * When data is being sent we see 0x30 in the lower byte; this must
  114. * contain DSR and CTS ...
  115. */
  116. #define IPW_DSR ((1<<4) | (1<<5))
  117. #define IPW_CTS ((1<<5) | (1<<4))
  118. #define IPW_WANTS_TO_SEND 0x30
  119. static struct usb_device_id usb_ipw_ids[] = {
  120. { USB_DEVICE(IPW_VID, IPW_PID) },
  121. { },
  122. };
  123. MODULE_DEVICE_TABLE(usb, usb_ipw_ids);
  124. static struct usb_driver usb_ipw_driver = {
  125. .name = "ipwtty",
  126. .probe = usb_serial_probe,
  127. .disconnect = usb_serial_disconnect,
  128. .id_table = usb_ipw_ids,
  129. .no_dynamic_id = 1,
  130. };
  131. static int debug;
  132. static void ipw_read_bulk_callback(struct urb *urb)
  133. {
  134. struct usb_serial_port *port = urb->context;
  135. unsigned char *data = urb->transfer_buffer;
  136. struct tty_struct *tty;
  137. int result;
  138. int status = urb->status;
  139. dbg("%s - port %d", __func__, port->number);
  140. if (status) {
  141. dbg("%s - nonzero read bulk status received: %d",
  142. __func__, status);
  143. return;
  144. }
  145. usb_serial_debug_data(debug, &port->dev, __func__,
  146. urb->actual_length, data);
  147. tty = port->port.tty;
  148. if (tty && urb->actual_length) {
  149. tty_buffer_request_room(tty, urb->actual_length);
  150. tty_insert_flip_string(tty, data, urb->actual_length);
  151. tty_flip_buffer_push(tty);
  152. }
  153. /* Continue trying to always read */
  154. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  155. usb_rcvbulkpipe(port->serial->dev,
  156. port->bulk_in_endpointAddress),
  157. port->read_urb->transfer_buffer,
  158. port->read_urb->transfer_buffer_length,
  159. ipw_read_bulk_callback, port);
  160. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  161. if (result)
  162. dev_err(&port->dev,
  163. "%s - failed resubmitting read urb, error %d\n",
  164. __func__, result);
  165. return;
  166. }
  167. static int ipw_open(struct tty_struct *tty,
  168. struct usb_serial_port *port, struct file *filp)
  169. {
  170. struct usb_device *dev = port->serial->dev;
  171. u8 buf_flow_static[16] = IPW_BYTES_FLOWINIT;
  172. u8 *buf_flow_init;
  173. int result;
  174. dbg("%s", __func__);
  175. buf_flow_init = kmemdup(buf_flow_static, 16, GFP_KERNEL);
  176. if (!buf_flow_init)
  177. return -ENOMEM;
  178. if (tty)
  179. tty->low_latency = 1;
  180. /* --1: Tell the modem to initialize (we think) From sniffs this is
  181. * always the first thing that gets sent to the modem during
  182. * opening of the device */
  183. dbg("%s: Sending SIO_INIT (we guess)", __func__);
  184. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  185. IPW_SIO_INIT,
  186. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  187. 0,
  188. 0, /* index */
  189. NULL,
  190. 0,
  191. 100000);
  192. if (result < 0)
  193. dev_err(&port->dev,
  194. "Init of modem failed (error = %d)\n", result);
  195. /* reset the bulk pipes */
  196. usb_clear_halt(dev,
  197. usb_rcvbulkpipe(dev, port->bulk_in_endpointAddress));
  198. usb_clear_halt(dev,
  199. usb_sndbulkpipe(dev, port->bulk_out_endpointAddress));
  200. /*--2: Start reading from the device */
  201. dbg("%s: setting up bulk read callback", __func__);
  202. usb_fill_bulk_urb(port->read_urb, dev,
  203. usb_rcvbulkpipe(dev, port->bulk_in_endpointAddress),
  204. port->bulk_in_buffer,
  205. port->bulk_in_size,
  206. ipw_read_bulk_callback, port);
  207. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  208. if (result < 0)
  209. dbg("%s - usb_submit_urb(read bulk) failed with status %d",
  210. __func__, result);
  211. /*--3: Tell the modem to open the floodgates on the rx bulk channel */
  212. dbg("%s:asking modem for RxRead (RXBULK_ON)", __func__);
  213. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  214. IPW_SIO_RXCTL,
  215. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  216. IPW_RXBULK_ON,
  217. 0, /* index */
  218. NULL,
  219. 0,
  220. 100000);
  221. if (result < 0)
  222. dev_err(&port->dev,
  223. "Enabling bulk RxRead failed (error = %d)\n", result);
  224. /*--4: setup the initial flowcontrol */
  225. dbg("%s:setting init flowcontrol (%s)", __func__, buf_flow_init);
  226. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  227. IPW_SIO_HANDFLOW,
  228. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  229. 0,
  230. 0,
  231. buf_flow_init,
  232. 0x10,
  233. 200000);
  234. if (result < 0)
  235. dev_err(&port->dev,
  236. "initial flowcontrol failed (error = %d)\n", result);
  237. /*--5: raise the dtr */
  238. dbg("%s:raising dtr", __func__);
  239. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  240. IPW_SIO_SET_PIN,
  241. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  242. IPW_PIN_SETDTR,
  243. 0,
  244. NULL,
  245. 0,
  246. 200000);
  247. if (result < 0)
  248. dev_err(&port->dev,
  249. "setting dtr failed (error = %d)\n", result);
  250. /*--6: raise the rts */
  251. dbg("%s:raising rts", __func__);
  252. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  253. IPW_SIO_SET_PIN,
  254. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  255. IPW_PIN_SETRTS,
  256. 0,
  257. NULL,
  258. 0,
  259. 200000);
  260. if (result < 0)
  261. dev_err(&port->dev,
  262. "setting dtr failed (error = %d)\n", result);
  263. kfree(buf_flow_init);
  264. return 0;
  265. }
  266. static void ipw_close(struct tty_struct *tty,
  267. struct usb_serial_port *port, struct file *filp)
  268. {
  269. struct usb_device *dev = port->serial->dev;
  270. int result;
  271. if (tty_hung_up_p(filp)) {
  272. dbg("%s: tty_hung_up_p ...", __func__);
  273. return;
  274. }
  275. /*--1: drop the dtr */
  276. dbg("%s:dropping dtr", __func__);
  277. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  278. IPW_SIO_SET_PIN,
  279. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  280. IPW_PIN_CLRDTR,
  281. 0,
  282. NULL,
  283. 0,
  284. 200000);
  285. if (result < 0)
  286. dev_err(&port->dev, "dropping dtr failed (error = %d)\n",
  287. result);
  288. /*--2: drop the rts */
  289. dbg("%s:dropping rts", __func__);
  290. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  291. IPW_SIO_SET_PIN, USB_TYPE_VENDOR |
  292. USB_RECIP_INTERFACE | USB_DIR_OUT,
  293. IPW_PIN_CLRRTS,
  294. 0,
  295. NULL,
  296. 0,
  297. 200000);
  298. if (result < 0)
  299. dev_err(&port->dev,
  300. "dropping rts failed (error = %d)\n", result);
  301. /*--3: purge */
  302. dbg("%s:sending purge", __func__);
  303. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  304. IPW_SIO_PURGE, USB_TYPE_VENDOR |
  305. USB_RECIP_INTERFACE | USB_DIR_OUT,
  306. 0x03,
  307. 0,
  308. NULL,
  309. 0,
  310. 200000);
  311. if (result < 0)
  312. dev_err(&port->dev, "purge failed (error = %d)\n", result);
  313. /* send RXBULK_off (tell modem to stop transmitting bulk data on
  314. rx chan) */
  315. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  316. IPW_SIO_RXCTL,
  317. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  318. IPW_RXBULK_OFF,
  319. 0, /* index */
  320. NULL,
  321. 0,
  322. 100000);
  323. if (result < 0)
  324. dev_err(&port->dev,
  325. "Disabling bulk RxRead failed (error = %d)\n", result);
  326. /* shutdown any in-flight urbs that we know about */
  327. usb_kill_urb(port->read_urb);
  328. usb_kill_urb(port->write_urb);
  329. }
  330. static void ipw_write_bulk_callback(struct urb *urb)
  331. {
  332. struct usb_serial_port *port = urb->context;
  333. int status = urb->status;
  334. dbg("%s", __func__);
  335. port->write_urb_busy = 0;
  336. if (status)
  337. dbg("%s - nonzero write bulk status received: %d",
  338. __func__, status);
  339. usb_serial_port_softint(port);
  340. }
  341. static int ipw_write(struct tty_struct *tty, struct usb_serial_port *port,
  342. const unsigned char *buf, int count)
  343. {
  344. struct usb_device *dev = port->serial->dev;
  345. int ret;
  346. dbg("%s: TOP: count=%d, in_interrupt=%ld", __func__,
  347. count, in_interrupt());
  348. if (count == 0) {
  349. dbg("%s - write request of 0 bytes", __func__);
  350. return 0;
  351. }
  352. spin_lock_bh(&port->lock);
  353. if (port->write_urb_busy) {
  354. spin_unlock_bh(&port->lock);
  355. dbg("%s - already writing", __func__);
  356. return 0;
  357. }
  358. port->write_urb_busy = 1;
  359. spin_unlock_bh(&port->lock);
  360. count = min(count, port->bulk_out_size);
  361. memcpy(port->bulk_out_buffer, buf, count);
  362. dbg("%s count now:%d", __func__, count);
  363. usb_fill_bulk_urb(port->write_urb, dev,
  364. usb_sndbulkpipe(dev, port->bulk_out_endpointAddress),
  365. port->write_urb->transfer_buffer,
  366. count,
  367. ipw_write_bulk_callback,
  368. port);
  369. ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  370. if (ret != 0) {
  371. port->write_urb_busy = 0;
  372. dbg("%s - usb_submit_urb(write bulk) failed with error = %d",
  373. __func__, ret);
  374. return ret;
  375. }
  376. dbg("%s returning %d", __func__, count);
  377. return count;
  378. }
  379. static int ipw_probe(struct usb_serial_port *port)
  380. {
  381. return 0;
  382. }
  383. static int ipw_disconnect(struct usb_serial_port *port)
  384. {
  385. usb_set_serial_port_data(port, NULL);
  386. return 0;
  387. }
  388. static struct usb_serial_driver ipw_device = {
  389. .driver = {
  390. .owner = THIS_MODULE,
  391. .name = "ipw",
  392. },
  393. .description = "IPWireless converter",
  394. .usb_driver = &usb_ipw_driver,
  395. .id_table = usb_ipw_ids,
  396. .num_ports = 1,
  397. .open = ipw_open,
  398. .close = ipw_close,
  399. .port_probe = ipw_probe,
  400. .port_remove = ipw_disconnect,
  401. .write = ipw_write,
  402. .write_bulk_callback = ipw_write_bulk_callback,
  403. .read_bulk_callback = ipw_read_bulk_callback,
  404. };
  405. static int usb_ipw_init(void)
  406. {
  407. int retval;
  408. retval = usb_serial_register(&ipw_device);
  409. if (retval)
  410. return retval;
  411. retval = usb_register(&usb_ipw_driver);
  412. if (retval) {
  413. usb_serial_deregister(&ipw_device);
  414. return retval;
  415. }
  416. info(DRIVER_DESC " " DRIVER_VERSION);
  417. return 0;
  418. }
  419. static void usb_ipw_exit(void)
  420. {
  421. usb_deregister(&usb_ipw_driver);
  422. usb_serial_deregister(&ipw_device);
  423. }
  424. module_init(usb_ipw_init);
  425. module_exit(usb_ipw_exit);
  426. /* Module information */
  427. MODULE_AUTHOR(DRIVER_AUTHOR);
  428. MODULE_DESCRIPTION(DRIVER_DESC);
  429. MODULE_LICENSE("GPL");
  430. module_param(debug, bool, S_IRUGO | S_IWUSR);
  431. MODULE_PARM_DESC(debug, "Debug enabled or not");