cypress_m8.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  1. /*
  2. * USB Cypress M8 driver
  3. *
  4. * Copyright (C) 2004
  5. * Lonnie Mendez (dignome@gmail.com)
  6. * Copyright (C) 2003,2004
  7. * Neil Whelchel (koyama@firstlight.net)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * See Documentation/usb/usb-serial.txt for more information on using this
  15. * driver
  16. *
  17. * See http://geocities.com/i0xox0i for information on this driver and the
  18. * earthmate usb device.
  19. *
  20. * Lonnie Mendez <dignome@gmail.com>
  21. * 4-29-2005
  22. * Fixed problem where setting or retreiving the serial config would fail
  23. * with EPIPE. Removed CRTS toggling so the driver behaves more like
  24. * other usbserial adapters. Issued new interval of 1ms instead of the
  25. * default 10ms. As a result, transfer speed has been substantially
  26. * increased from avg. 850bps to avg. 3300bps. initial termios has also
  27. * been modified. Cleaned up code and formatting issues so it is more
  28. * readable. Replaced the C++ style comments.
  29. *
  30. * Lonnie Mendez <dignome@gmail.com>
  31. * 12-15-2004
  32. * Incorporated write buffering from pl2303 driver. Fixed bug with line
  33. * handling so both lines are raised in cypress_open. (was dropping rts)
  34. * Various code cleanups made as well along with other misc bug fixes.
  35. *
  36. * Lonnie Mendez <dignome@gmail.com>
  37. * 04-10-2004
  38. * Driver modified to support dynamic line settings. Various improvments
  39. * and features.
  40. *
  41. * Neil Whelchel
  42. * 10-2003
  43. * Driver first released.
  44. *
  45. */
  46. /* Thanks to Neil Whelchel for writing the first cypress m8 implementation
  47. for linux. */
  48. /* Thanks to cypress for providing references for the hid reports. */
  49. /* Thanks to Jiang Zhang for providing links and for general help. */
  50. /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
  51. #include <linux/kernel.h>
  52. #include <linux/errno.h>
  53. #include <linux/init.h>
  54. #include <linux/slab.h>
  55. #include <linux/tty.h>
  56. #include <linux/tty_driver.h>
  57. #include <linux/tty_flip.h>
  58. #include <linux/module.h>
  59. #include <linux/moduleparam.h>
  60. #include <linux/spinlock.h>
  61. #include <linux/usb.h>
  62. #include <linux/usb/serial.h>
  63. #include <linux/serial.h>
  64. #include <linux/delay.h>
  65. #include <linux/uaccess.h>
  66. #include "cypress_m8.h"
  67. #ifdef CONFIG_USB_SERIAL_DEBUG
  68. static int debug = 1;
  69. #else
  70. static int debug;
  71. #endif
  72. static int stats;
  73. static int interval;
  74. /*
  75. * Version Information
  76. */
  77. #define DRIVER_VERSION "v1.09"
  78. #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
  79. #define DRIVER_DESC "Cypress USB to Serial Driver"
  80. /* write buffer size defines */
  81. #define CYPRESS_BUF_SIZE 1024
  82. #define CYPRESS_CLOSING_WAIT (30*HZ)
  83. static struct usb_device_id id_table_earthmate [] = {
  84. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
  85. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
  86. { } /* Terminating entry */
  87. };
  88. static struct usb_device_id id_table_cyphidcomrs232 [] = {
  89. { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
  90. { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
  91. { } /* Terminating entry */
  92. };
  93. static struct usb_device_id id_table_nokiaca42v2 [] = {
  94. { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
  95. { } /* Terminating entry */
  96. };
  97. static struct usb_device_id id_table_combined [] = {
  98. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
  99. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
  100. { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
  101. { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
  102. { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
  103. { } /* Terminating entry */
  104. };
  105. MODULE_DEVICE_TABLE(usb, id_table_combined);
  106. static struct usb_driver cypress_driver = {
  107. .name = "cypress",
  108. .probe = usb_serial_probe,
  109. .disconnect = usb_serial_disconnect,
  110. .id_table = id_table_combined,
  111. .no_dynamic_id = 1,
  112. };
  113. enum packet_format {
  114. packet_format_1, /* b0:status, b1:payload count */
  115. packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
  116. };
  117. struct cypress_private {
  118. spinlock_t lock; /* private lock */
  119. int chiptype; /* identifier of device, for quirks/etc */
  120. int bytes_in; /* used for statistics */
  121. int bytes_out; /* used for statistics */
  122. int cmd_count; /* used for statistics */
  123. int cmd_ctrl; /* always set this to 1 before issuing a command */
  124. struct cypress_buf *buf; /* write buffer */
  125. int write_urb_in_use; /* write urb in use indicator */
  126. int write_urb_interval; /* interval to use for write urb */
  127. int read_urb_interval; /* interval to use for read urb */
  128. int comm_is_ok; /* true if communication is (still) ok */
  129. int termios_initialized;
  130. __u8 line_control; /* holds dtr / rts value */
  131. __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
  132. __u8 current_config; /* stores the current configuration byte */
  133. __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
  134. enum packet_format pkt_fmt; /* format to use for packet send / receive */
  135. int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
  136. int baud_rate; /* stores current baud rate in
  137. integer form */
  138. int isthrottled; /* if throttled, discard reads */
  139. wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
  140. char prev_status, diff_status; /* used for TIOCMIWAIT */
  141. /* we pass a pointer to this as the arguement sent to
  142. cypress_set_termios old_termios */
  143. struct ktermios tmp_termios; /* stores the old termios settings */
  144. };
  145. /* write buffer structure */
  146. struct cypress_buf {
  147. unsigned int buf_size;
  148. char *buf_buf;
  149. char *buf_get;
  150. char *buf_put;
  151. };
  152. /* function prototypes for the Cypress USB to serial device */
  153. static int cypress_earthmate_startup(struct usb_serial *serial);
  154. static int cypress_hidcom_startup(struct usb_serial *serial);
  155. static int cypress_ca42v2_startup(struct usb_serial *serial);
  156. static void cypress_shutdown(struct usb_serial *serial);
  157. static int cypress_open(struct tty_struct *tty,
  158. struct usb_serial_port *port, struct file *filp);
  159. static void cypress_close(struct tty_struct *tty,
  160. struct usb_serial_port *port, struct file *filp);
  161. static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
  162. const unsigned char *buf, int count);
  163. static void cypress_send(struct usb_serial_port *port);
  164. static int cypress_write_room(struct tty_struct *tty);
  165. static int cypress_ioctl(struct tty_struct *tty, struct file *file,
  166. unsigned int cmd, unsigned long arg);
  167. static void cypress_set_termios(struct tty_struct *tty,
  168. struct usb_serial_port *port, struct ktermios *old);
  169. static int cypress_tiocmget(struct tty_struct *tty, struct file *file);
  170. static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
  171. unsigned int set, unsigned int clear);
  172. static int cypress_chars_in_buffer(struct tty_struct *tty);
  173. static void cypress_throttle(struct tty_struct *tty);
  174. static void cypress_unthrottle(struct tty_struct *tty);
  175. static void cypress_set_dead(struct usb_serial_port *port);
  176. static void cypress_read_int_callback(struct urb *urb);
  177. static void cypress_write_int_callback(struct urb *urb);
  178. /* write buffer functions */
  179. static struct cypress_buf *cypress_buf_alloc(unsigned int size);
  180. static void cypress_buf_free(struct cypress_buf *cb);
  181. static void cypress_buf_clear(struct cypress_buf *cb);
  182. static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
  183. static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
  184. static unsigned int cypress_buf_put(struct cypress_buf *cb,
  185. const char *buf, unsigned int count);
  186. static unsigned int cypress_buf_get(struct cypress_buf *cb,
  187. char *buf, unsigned int count);
  188. static struct usb_serial_driver cypress_earthmate_device = {
  189. .driver = {
  190. .owner = THIS_MODULE,
  191. .name = "earthmate",
  192. },
  193. .description = "DeLorme Earthmate USB",
  194. .usb_driver = &cypress_driver,
  195. .id_table = id_table_earthmate,
  196. .num_ports = 1,
  197. .attach = cypress_earthmate_startup,
  198. .shutdown = cypress_shutdown,
  199. .open = cypress_open,
  200. .close = cypress_close,
  201. .write = cypress_write,
  202. .write_room = cypress_write_room,
  203. .ioctl = cypress_ioctl,
  204. .set_termios = cypress_set_termios,
  205. .tiocmget = cypress_tiocmget,
  206. .tiocmset = cypress_tiocmset,
  207. .chars_in_buffer = cypress_chars_in_buffer,
  208. .throttle = cypress_throttle,
  209. .unthrottle = cypress_unthrottle,
  210. .read_int_callback = cypress_read_int_callback,
  211. .write_int_callback = cypress_write_int_callback,
  212. };
  213. static struct usb_serial_driver cypress_hidcom_device = {
  214. .driver = {
  215. .owner = THIS_MODULE,
  216. .name = "cyphidcom",
  217. },
  218. .description = "HID->COM RS232 Adapter",
  219. .usb_driver = &cypress_driver,
  220. .id_table = id_table_cyphidcomrs232,
  221. .num_ports = 1,
  222. .attach = cypress_hidcom_startup,
  223. .shutdown = cypress_shutdown,
  224. .open = cypress_open,
  225. .close = cypress_close,
  226. .write = cypress_write,
  227. .write_room = cypress_write_room,
  228. .ioctl = cypress_ioctl,
  229. .set_termios = cypress_set_termios,
  230. .tiocmget = cypress_tiocmget,
  231. .tiocmset = cypress_tiocmset,
  232. .chars_in_buffer = cypress_chars_in_buffer,
  233. .throttle = cypress_throttle,
  234. .unthrottle = cypress_unthrottle,
  235. .read_int_callback = cypress_read_int_callback,
  236. .write_int_callback = cypress_write_int_callback,
  237. };
  238. static struct usb_serial_driver cypress_ca42v2_device = {
  239. .driver = {
  240. .owner = THIS_MODULE,
  241. .name = "nokiaca42v2",
  242. },
  243. .description = "Nokia CA-42 V2 Adapter",
  244. .usb_driver = &cypress_driver,
  245. .id_table = id_table_nokiaca42v2,
  246. .num_ports = 1,
  247. .attach = cypress_ca42v2_startup,
  248. .shutdown = cypress_shutdown,
  249. .open = cypress_open,
  250. .close = cypress_close,
  251. .write = cypress_write,
  252. .write_room = cypress_write_room,
  253. .ioctl = cypress_ioctl,
  254. .set_termios = cypress_set_termios,
  255. .tiocmget = cypress_tiocmget,
  256. .tiocmset = cypress_tiocmset,
  257. .chars_in_buffer = cypress_chars_in_buffer,
  258. .throttle = cypress_throttle,
  259. .unthrottle = cypress_unthrottle,
  260. .read_int_callback = cypress_read_int_callback,
  261. .write_int_callback = cypress_write_int_callback,
  262. };
  263. /*****************************************************************************
  264. * Cypress serial helper functions
  265. *****************************************************************************/
  266. static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
  267. {
  268. struct cypress_private *priv;
  269. priv = usb_get_serial_port_data(port);
  270. /*
  271. * The general purpose firmware for the Cypress M8 allows for
  272. * a maximum speed of 57600bps (I have no idea whether DeLorme
  273. * chose to use the general purpose firmware or not), if you
  274. * need to modify this speed setting for your own project
  275. * please add your own chiptype and modify the code likewise.
  276. * The Cypress HID->COM device will work successfully up to
  277. * 115200bps (but the actual throughput is around 3kBps).
  278. */
  279. if (port->serial->dev->speed == USB_SPEED_LOW) {
  280. /*
  281. * Mike Isely <isely@pobox.com> 2-Feb-2008: The
  282. * Cypress app note that describes this mechanism
  283. * states the the low-speed part can't handle more
  284. * than 800 bytes/sec, in which case 4800 baud is the
  285. * safest speed for a part like that.
  286. */
  287. if (new_rate > 4800) {
  288. dbg("%s - failed setting baud rate, device incapable "
  289. "speed %d", __func__, new_rate);
  290. return -1;
  291. }
  292. }
  293. switch (priv->chiptype) {
  294. case CT_EARTHMATE:
  295. if (new_rate <= 600) {
  296. /* 300 and 600 baud rates are supported under
  297. * the generic firmware, but are not used with
  298. * NMEA and SiRF protocols */
  299. dbg("%s - failed setting baud rate, unsupported speed "
  300. "of %d on Earthmate GPS", __func__, new_rate);
  301. return -1;
  302. }
  303. break;
  304. default:
  305. break;
  306. }
  307. return new_rate;
  308. }
  309. /* This function can either set or retrieve the current serial line settings */
  310. static int cypress_serial_control(struct tty_struct *tty,
  311. struct usb_serial_port *port, speed_t baud_rate, int data_bits,
  312. int stop_bits, int parity_enable, int parity_type, int reset,
  313. int cypress_request_type)
  314. {
  315. int new_baudrate = 0, retval = 0, tries = 0;
  316. struct cypress_private *priv;
  317. __u8 feature_buffer[5];
  318. unsigned long flags;
  319. dbg("%s", __func__);
  320. priv = usb_get_serial_port_data(port);
  321. if (!priv->comm_is_ok)
  322. return -ENODEV;
  323. switch (cypress_request_type) {
  324. case CYPRESS_SET_CONFIG:
  325. new_baudrate = priv->baud_rate;
  326. /* 0 means 'Hang up' so doesn't change the true bit rate */
  327. if (baud_rate == 0)
  328. new_baudrate = priv->baud_rate;
  329. /* Change of speed ? */
  330. else if (baud_rate != priv->baud_rate) {
  331. dbg("%s - baud rate is changing", __func__);
  332. retval = analyze_baud_rate(port, baud_rate);
  333. if (retval >= 0) {
  334. new_baudrate = retval;
  335. dbg("%s - New baud rate set to %d",
  336. __func__, new_baudrate);
  337. }
  338. }
  339. dbg("%s - baud rate is being sent as %d",
  340. __func__, new_baudrate);
  341. memset(feature_buffer, 0, sizeof(feature_buffer));
  342. /* fill the feature_buffer with new configuration */
  343. *((u_int32_t *)feature_buffer) = new_baudrate;
  344. feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
  345. /* 1 bit gap */
  346. feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
  347. feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
  348. feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
  349. /* 1 bit gap */
  350. feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
  351. dbg("%s - device is being sent this feature report:",
  352. __func__);
  353. dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
  354. feature_buffer[0], feature_buffer[1],
  355. feature_buffer[2], feature_buffer[3],
  356. feature_buffer[4]);
  357. do {
  358. retval = usb_control_msg(port->serial->dev,
  359. usb_sndctrlpipe(port->serial->dev, 0),
  360. HID_REQ_SET_REPORT,
  361. USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
  362. 0x0300, 0, feature_buffer,
  363. sizeof(feature_buffer), 500);
  364. if (tries++ >= 3)
  365. break;
  366. } while (retval != sizeof(feature_buffer) &&
  367. retval != -ENODEV);
  368. if (retval != sizeof(feature_buffer)) {
  369. dev_err(&port->dev, "%s - failed sending serial "
  370. "line settings - %d\n", __func__, retval);
  371. cypress_set_dead(port);
  372. } else {
  373. spin_lock_irqsave(&priv->lock, flags);
  374. priv->baud_rate = new_baudrate;
  375. priv->current_config = feature_buffer[4];
  376. spin_unlock_irqrestore(&priv->lock, flags);
  377. /* If we asked for a speed change encode it */
  378. if (baud_rate)
  379. tty_encode_baud_rate(tty,
  380. new_baudrate, new_baudrate);
  381. }
  382. break;
  383. case CYPRESS_GET_CONFIG:
  384. if (priv->get_cfg_unsafe) {
  385. /* Not implemented for this device,
  386. and if we try to do it we're likely
  387. to crash the hardware. */
  388. return -ENOTTY;
  389. }
  390. dbg("%s - retreiving serial line settings", __func__);
  391. /* set initial values in feature buffer */
  392. memset(feature_buffer, 0, sizeof(feature_buffer));
  393. do {
  394. retval = usb_control_msg(port->serial->dev,
  395. usb_rcvctrlpipe(port->serial->dev, 0),
  396. HID_REQ_GET_REPORT,
  397. USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
  398. 0x0300, 0, feature_buffer,
  399. sizeof(feature_buffer), 500);
  400. if (tries++ >= 3)
  401. break;
  402. } while (retval != sizeof(feature_buffer)
  403. && retval != -ENODEV);
  404. if (retval != sizeof(feature_buffer)) {
  405. dev_err(&port->dev, "%s - failed to retrieve serial "
  406. "line settings - %d\n", __func__, retval);
  407. cypress_set_dead(port);
  408. return retval;
  409. } else {
  410. spin_lock_irqsave(&priv->lock, flags);
  411. /* store the config in one byte, and later
  412. use bit masks to check values */
  413. priv->current_config = feature_buffer[4];
  414. priv->baud_rate = *((u_int32_t *)feature_buffer);
  415. spin_unlock_irqrestore(&priv->lock, flags);
  416. }
  417. }
  418. spin_lock_irqsave(&priv->lock, flags);
  419. ++priv->cmd_count;
  420. spin_unlock_irqrestore(&priv->lock, flags);
  421. return retval;
  422. } /* cypress_serial_control */
  423. static void cypress_set_dead(struct usb_serial_port *port)
  424. {
  425. struct cypress_private *priv = usb_get_serial_port_data(port);
  426. unsigned long flags;
  427. spin_lock_irqsave(&priv->lock, flags);
  428. if (!priv->comm_is_ok) {
  429. spin_unlock_irqrestore(&priv->lock, flags);
  430. return;
  431. }
  432. priv->comm_is_ok = 0;
  433. spin_unlock_irqrestore(&priv->lock, flags);
  434. dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
  435. "interval might be too short\n", port->number);
  436. }
  437. /*****************************************************************************
  438. * Cypress serial driver functions
  439. *****************************************************************************/
  440. static int generic_startup(struct usb_serial *serial)
  441. {
  442. struct cypress_private *priv;
  443. struct usb_serial_port *port = serial->port[0];
  444. dbg("%s - port %d", __func__, port->number);
  445. priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
  446. if (!priv)
  447. return -ENOMEM;
  448. priv->comm_is_ok = !0;
  449. spin_lock_init(&priv->lock);
  450. priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
  451. if (priv->buf == NULL) {
  452. kfree(priv);
  453. return -ENOMEM;
  454. }
  455. init_waitqueue_head(&priv->delta_msr_wait);
  456. usb_reset_configuration(serial->dev);
  457. priv->cmd_ctrl = 0;
  458. priv->line_control = 0;
  459. priv->termios_initialized = 0;
  460. priv->rx_flags = 0;
  461. /* Default packet format setting is determined by packet size.
  462. Anything with a size larger then 9 must have a separate
  463. count field since the 3 bit count field is otherwise too
  464. small. Otherwise we can use the slightly more compact
  465. format. This is in accordance with the cypress_m8 serial
  466. converter app note. */
  467. if (port->interrupt_out_size > 9)
  468. priv->pkt_fmt = packet_format_1;
  469. else
  470. priv->pkt_fmt = packet_format_2;
  471. if (interval > 0) {
  472. priv->write_urb_interval = interval;
  473. priv->read_urb_interval = interval;
  474. dbg("%s - port %d read & write intervals forced to %d",
  475. __func__, port->number, interval);
  476. } else {
  477. priv->write_urb_interval = port->interrupt_out_urb->interval;
  478. priv->read_urb_interval = port->interrupt_in_urb->interval;
  479. dbg("%s - port %d intervals: read=%d write=%d",
  480. __func__, port->number,
  481. priv->read_urb_interval, priv->write_urb_interval);
  482. }
  483. usb_set_serial_port_data(port, priv);
  484. return 0;
  485. }
  486. static int cypress_earthmate_startup(struct usb_serial *serial)
  487. {
  488. struct cypress_private *priv;
  489. struct usb_serial_port *port = serial->port[0];
  490. dbg("%s", __func__);
  491. if (generic_startup(serial)) {
  492. dbg("%s - Failed setting up port %d", __func__,
  493. port->number);
  494. return 1;
  495. }
  496. priv = usb_get_serial_port_data(port);
  497. priv->chiptype = CT_EARTHMATE;
  498. /* All Earthmate devices use the separated-count packet
  499. format! Idiotic. */
  500. priv->pkt_fmt = packet_format_1;
  501. if (serial->dev->descriptor.idProduct !=
  502. cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
  503. /* The old original USB Earthmate seemed able to
  504. handle GET_CONFIG requests; everything they've
  505. produced since that time crashes if this command is
  506. attempted :-( */
  507. dbg("%s - Marking this device as unsafe for GET_CONFIG "
  508. "commands", __func__);
  509. priv->get_cfg_unsafe = !0;
  510. }
  511. return 0;
  512. } /* cypress_earthmate_startup */
  513. static int cypress_hidcom_startup(struct usb_serial *serial)
  514. {
  515. struct cypress_private *priv;
  516. dbg("%s", __func__);
  517. if (generic_startup(serial)) {
  518. dbg("%s - Failed setting up port %d", __func__,
  519. serial->port[0]->number);
  520. return 1;
  521. }
  522. priv = usb_get_serial_port_data(serial->port[0]);
  523. priv->chiptype = CT_CYPHIDCOM;
  524. return 0;
  525. } /* cypress_hidcom_startup */
  526. static int cypress_ca42v2_startup(struct usb_serial *serial)
  527. {
  528. struct cypress_private *priv;
  529. dbg("%s", __func__);
  530. if (generic_startup(serial)) {
  531. dbg("%s - Failed setting up port %d", __func__,
  532. serial->port[0]->number);
  533. return 1;
  534. }
  535. priv = usb_get_serial_port_data(serial->port[0]);
  536. priv->chiptype = CT_CA42V2;
  537. return 0;
  538. } /* cypress_ca42v2_startup */
  539. static void cypress_shutdown(struct usb_serial *serial)
  540. {
  541. struct cypress_private *priv;
  542. dbg("%s - port %d", __func__, serial->port[0]->number);
  543. /* all open ports are closed at this point */
  544. priv = usb_get_serial_port_data(serial->port[0]);
  545. if (priv) {
  546. cypress_buf_free(priv->buf);
  547. kfree(priv);
  548. usb_set_serial_port_data(serial->port[0], NULL);
  549. }
  550. }
  551. static int cypress_open(struct tty_struct *tty,
  552. struct usb_serial_port *port, struct file *filp)
  553. {
  554. struct cypress_private *priv = usb_get_serial_port_data(port);
  555. struct usb_serial *serial = port->serial;
  556. unsigned long flags;
  557. int result = 0;
  558. dbg("%s - port %d", __func__, port->number);
  559. if (!priv->comm_is_ok)
  560. return -EIO;
  561. /* clear halts before open */
  562. usb_clear_halt(serial->dev, 0x81);
  563. usb_clear_halt(serial->dev, 0x02);
  564. spin_lock_irqsave(&priv->lock, flags);
  565. /* reset read/write statistics */
  566. priv->bytes_in = 0;
  567. priv->bytes_out = 0;
  568. priv->cmd_count = 0;
  569. priv->rx_flags = 0;
  570. spin_unlock_irqrestore(&priv->lock, flags);
  571. /* raise both lines and set termios */
  572. spin_lock_irqsave(&priv->lock, flags);
  573. priv->line_control = CONTROL_DTR | CONTROL_RTS;
  574. priv->cmd_ctrl = 1;
  575. spin_unlock_irqrestore(&priv->lock, flags);
  576. result = cypress_write(tty, port, NULL, 0);
  577. if (result) {
  578. dev_err(&port->dev,
  579. "%s - failed setting the control lines - error %d\n",
  580. __func__, result);
  581. return result;
  582. } else
  583. dbg("%s - success setting the control lines", __func__);
  584. if (tty)
  585. cypress_set_termios(tty, port, &priv->tmp_termios);
  586. /* setup the port and start reading from the device */
  587. if (!port->interrupt_in_urb) {
  588. dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
  589. __func__);
  590. return -1;
  591. }
  592. usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
  593. usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
  594. port->interrupt_in_urb->transfer_buffer,
  595. port->interrupt_in_urb->transfer_buffer_length,
  596. cypress_read_int_callback, port, priv->read_urb_interval);
  597. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  598. if (result) {
  599. dev_err(&port->dev,
  600. "%s - failed submitting read urb, error %d\n",
  601. __func__, result);
  602. cypress_set_dead(port);
  603. }
  604. return result;
  605. } /* cypress_open */
  606. static void cypress_close(struct tty_struct *tty,
  607. struct usb_serial_port *port, struct file *filp)
  608. {
  609. struct cypress_private *priv = usb_get_serial_port_data(port);
  610. unsigned int c_cflag;
  611. int bps;
  612. long timeout;
  613. wait_queue_t wait;
  614. dbg("%s - port %d", __func__, port->number);
  615. /* wait for data to drain from buffer */
  616. spin_lock_irq(&priv->lock);
  617. timeout = CYPRESS_CLOSING_WAIT;
  618. init_waitqueue_entry(&wait, current);
  619. add_wait_queue(&tty->write_wait, &wait);
  620. for (;;) {
  621. set_current_state(TASK_INTERRUPTIBLE);
  622. if (cypress_buf_data_avail(priv->buf) == 0
  623. || timeout == 0 || signal_pending(current)
  624. /* without mutex, allowed due to harmless failure mode */
  625. || port->serial->disconnected)
  626. break;
  627. spin_unlock_irq(&priv->lock);
  628. timeout = schedule_timeout(timeout);
  629. spin_lock_irq(&priv->lock);
  630. }
  631. set_current_state(TASK_RUNNING);
  632. remove_wait_queue(&tty->write_wait, &wait);
  633. /* clear out any remaining data in the buffer */
  634. cypress_buf_clear(priv->buf);
  635. spin_unlock_irq(&priv->lock);
  636. /* writing is potentially harmful, lock must be taken */
  637. mutex_lock(&port->serial->disc_mutex);
  638. if (port->serial->disconnected) {
  639. mutex_unlock(&port->serial->disc_mutex);
  640. return;
  641. }
  642. /* wait for characters to drain from device */
  643. if (tty) {
  644. bps = tty_get_baud_rate(tty);
  645. if (bps > 1200)
  646. timeout = max((HZ * 2560) / bps, HZ / 10);
  647. else
  648. timeout = 2 * HZ;
  649. schedule_timeout_interruptible(timeout);
  650. }
  651. dbg("%s - stopping urbs", __func__);
  652. usb_kill_urb(port->interrupt_in_urb);
  653. usb_kill_urb(port->interrupt_out_urb);
  654. if (tty) {
  655. c_cflag = tty->termios->c_cflag;
  656. if (c_cflag & HUPCL) {
  657. /* drop dtr and rts */
  658. priv = usb_get_serial_port_data(port);
  659. spin_lock_irq(&priv->lock);
  660. priv->line_control = 0;
  661. priv->cmd_ctrl = 1;
  662. spin_unlock_irq(&priv->lock);
  663. cypress_write(tty, port, NULL, 0);
  664. }
  665. }
  666. if (stats)
  667. dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
  668. priv->bytes_in, priv->bytes_out, priv->cmd_count);
  669. mutex_unlock(&port->serial->disc_mutex);
  670. } /* cypress_close */
  671. static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
  672. const unsigned char *buf, int count)
  673. {
  674. struct cypress_private *priv = usb_get_serial_port_data(port);
  675. unsigned long flags;
  676. dbg("%s - port %d, %d bytes", __func__, port->number, count);
  677. /* line control commands, which need to be executed immediately,
  678. are not put into the buffer for obvious reasons.
  679. */
  680. if (priv->cmd_ctrl) {
  681. count = 0;
  682. goto finish;
  683. }
  684. if (!count)
  685. return count;
  686. spin_lock_irqsave(&priv->lock, flags);
  687. count = cypress_buf_put(priv->buf, buf, count);
  688. spin_unlock_irqrestore(&priv->lock, flags);
  689. finish:
  690. cypress_send(port);
  691. return count;
  692. } /* cypress_write */
  693. static void cypress_send(struct usb_serial_port *port)
  694. {
  695. int count = 0, result, offset, actual_size;
  696. struct cypress_private *priv = usb_get_serial_port_data(port);
  697. unsigned long flags;
  698. if (!priv->comm_is_ok)
  699. return;
  700. dbg("%s - port %d", __func__, port->number);
  701. dbg("%s - interrupt out size is %d", __func__,
  702. port->interrupt_out_size);
  703. spin_lock_irqsave(&priv->lock, flags);
  704. if (priv->write_urb_in_use) {
  705. dbg("%s - can't write, urb in use", __func__);
  706. spin_unlock_irqrestore(&priv->lock, flags);
  707. return;
  708. }
  709. spin_unlock_irqrestore(&priv->lock, flags);
  710. /* clear buffer */
  711. memset(port->interrupt_out_urb->transfer_buffer, 0,
  712. port->interrupt_out_size);
  713. spin_lock_irqsave(&priv->lock, flags);
  714. switch (priv->pkt_fmt) {
  715. default:
  716. case packet_format_1:
  717. /* this is for the CY7C64013... */
  718. offset = 2;
  719. port->interrupt_out_buffer[0] = priv->line_control;
  720. break;
  721. case packet_format_2:
  722. /* this is for the CY7C63743... */
  723. offset = 1;
  724. port->interrupt_out_buffer[0] = priv->line_control;
  725. break;
  726. }
  727. if (priv->line_control & CONTROL_RESET)
  728. priv->line_control &= ~CONTROL_RESET;
  729. if (priv->cmd_ctrl) {
  730. priv->cmd_count++;
  731. dbg("%s - line control command being issued", __func__);
  732. spin_unlock_irqrestore(&priv->lock, flags);
  733. goto send;
  734. } else
  735. spin_unlock_irqrestore(&priv->lock, flags);
  736. count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
  737. port->interrupt_out_size-offset);
  738. if (count == 0)
  739. return;
  740. switch (priv->pkt_fmt) {
  741. default:
  742. case packet_format_1:
  743. port->interrupt_out_buffer[1] = count;
  744. break;
  745. case packet_format_2:
  746. port->interrupt_out_buffer[0] |= count;
  747. }
  748. dbg("%s - count is %d", __func__, count);
  749. send:
  750. spin_lock_irqsave(&priv->lock, flags);
  751. priv->write_urb_in_use = 1;
  752. spin_unlock_irqrestore(&priv->lock, flags);
  753. if (priv->cmd_ctrl)
  754. actual_size = 1;
  755. else
  756. actual_size = count +
  757. (priv->pkt_fmt == packet_format_1 ? 2 : 1);
  758. usb_serial_debug_data(debug, &port->dev, __func__,
  759. port->interrupt_out_size,
  760. port->interrupt_out_urb->transfer_buffer);
  761. usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
  762. usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
  763. port->interrupt_out_buffer, port->interrupt_out_size,
  764. cypress_write_int_callback, port, priv->write_urb_interval);
  765. result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
  766. if (result) {
  767. dev_err(&port->dev,
  768. "%s - failed submitting write urb, error %d\n",
  769. __func__, result);
  770. priv->write_urb_in_use = 0;
  771. cypress_set_dead(port);
  772. }
  773. spin_lock_irqsave(&priv->lock, flags);
  774. if (priv->cmd_ctrl)
  775. priv->cmd_ctrl = 0;
  776. /* do not count the line control and size bytes */
  777. priv->bytes_out += count;
  778. spin_unlock_irqrestore(&priv->lock, flags);
  779. usb_serial_port_softint(port);
  780. } /* cypress_send */
  781. /* returns how much space is available in the soft buffer */
  782. static int cypress_write_room(struct tty_struct *tty)
  783. {
  784. struct usb_serial_port *port = tty->driver_data;
  785. struct cypress_private *priv = usb_get_serial_port_data(port);
  786. int room = 0;
  787. unsigned long flags;
  788. dbg("%s - port %d", __func__, port->number);
  789. spin_lock_irqsave(&priv->lock, flags);
  790. room = cypress_buf_space_avail(priv->buf);
  791. spin_unlock_irqrestore(&priv->lock, flags);
  792. dbg("%s - returns %d", __func__, room);
  793. return room;
  794. }
  795. static int cypress_tiocmget(struct tty_struct *tty, struct file *file)
  796. {
  797. struct usb_serial_port *port = tty->driver_data;
  798. struct cypress_private *priv = usb_get_serial_port_data(port);
  799. __u8 status, control;
  800. unsigned int result = 0;
  801. unsigned long flags;
  802. dbg("%s - port %d", __func__, port->number);
  803. spin_lock_irqsave(&priv->lock, flags);
  804. control = priv->line_control;
  805. status = priv->current_status;
  806. spin_unlock_irqrestore(&priv->lock, flags);
  807. result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
  808. | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
  809. | ((status & UART_CTS) ? TIOCM_CTS : 0)
  810. | ((status & UART_DSR) ? TIOCM_DSR : 0)
  811. | ((status & UART_RI) ? TIOCM_RI : 0)
  812. | ((status & UART_CD) ? TIOCM_CD : 0);
  813. dbg("%s - result = %x", __func__, result);
  814. return result;
  815. }
  816. static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
  817. unsigned int set, unsigned int clear)
  818. {
  819. struct usb_serial_port *port = tty->driver_data;
  820. struct cypress_private *priv = usb_get_serial_port_data(port);
  821. unsigned long flags;
  822. dbg("%s - port %d", __func__, port->number);
  823. spin_lock_irqsave(&priv->lock, flags);
  824. if (set & TIOCM_RTS)
  825. priv->line_control |= CONTROL_RTS;
  826. if (set & TIOCM_DTR)
  827. priv->line_control |= CONTROL_DTR;
  828. if (clear & TIOCM_RTS)
  829. priv->line_control &= ~CONTROL_RTS;
  830. if (clear & TIOCM_DTR)
  831. priv->line_control &= ~CONTROL_DTR;
  832. priv->cmd_ctrl = 1;
  833. spin_unlock_irqrestore(&priv->lock, flags);
  834. return cypress_write(tty, port, NULL, 0);
  835. }
  836. static int cypress_ioctl(struct tty_struct *tty, struct file *file,
  837. unsigned int cmd, unsigned long arg)
  838. {
  839. struct usb_serial_port *port = tty->driver_data;
  840. struct cypress_private *priv = usb_get_serial_port_data(port);
  841. dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
  842. switch (cmd) {
  843. /* This code comes from drivers/char/serial.c and ftdi_sio.c */
  844. case TIOCMIWAIT:
  845. while (priv != NULL) {
  846. interruptible_sleep_on(&priv->delta_msr_wait);
  847. /* see if a signal did it */
  848. if (signal_pending(current))
  849. return -ERESTARTSYS;
  850. else {
  851. char diff = priv->diff_status;
  852. if (diff == 0)
  853. return -EIO; /* no change => error */
  854. /* consume all events */
  855. priv->diff_status = 0;
  856. /* return 0 if caller wanted to know about
  857. these bits */
  858. if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
  859. ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
  860. ((arg & TIOCM_CD) && (diff & UART_CD)) ||
  861. ((arg & TIOCM_CTS) && (diff & UART_CTS)))
  862. return 0;
  863. /* otherwise caller can't care less about what
  864. * happened, and so we continue to wait for
  865. * more events.
  866. */
  867. }
  868. }
  869. return 0;
  870. default:
  871. break;
  872. }
  873. dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
  874. return -ENOIOCTLCMD;
  875. } /* cypress_ioctl */
  876. static void cypress_set_termios(struct tty_struct *tty,
  877. struct usb_serial_port *port, struct ktermios *old_termios)
  878. {
  879. struct cypress_private *priv = usb_get_serial_port_data(port);
  880. int data_bits, stop_bits, parity_type, parity_enable;
  881. unsigned cflag, iflag;
  882. unsigned long flags;
  883. __u8 oldlines;
  884. int linechange = 0;
  885. dbg("%s - port %d", __func__, port->number);
  886. spin_lock_irqsave(&priv->lock, flags);
  887. if (!priv->termios_initialized) {
  888. if (priv->chiptype == CT_EARTHMATE) {
  889. *(tty->termios) = tty_std_termios;
  890. tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
  891. CLOCAL;
  892. tty->termios->c_ispeed = 4800;
  893. tty->termios->c_ospeed = 4800;
  894. } else if (priv->chiptype == CT_CYPHIDCOM) {
  895. *(tty->termios) = tty_std_termios;
  896. tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
  897. CLOCAL;
  898. tty->termios->c_ispeed = 9600;
  899. tty->termios->c_ospeed = 9600;
  900. } else if (priv->chiptype == CT_CA42V2) {
  901. *(tty->termios) = tty_std_termios;
  902. tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
  903. CLOCAL;
  904. tty->termios->c_ispeed = 9600;
  905. tty->termios->c_ospeed = 9600;
  906. }
  907. priv->termios_initialized = 1;
  908. }
  909. spin_unlock_irqrestore(&priv->lock, flags);
  910. /* Unsupported features need clearing */
  911. tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
  912. cflag = tty->termios->c_cflag;
  913. iflag = tty->termios->c_iflag;
  914. /* check if there are new settings */
  915. if (old_termios) {
  916. spin_lock_irqsave(&priv->lock, flags);
  917. priv->tmp_termios = *(tty->termios);
  918. spin_unlock_irqrestore(&priv->lock, flags);
  919. }
  920. /* set number of data bits, parity, stop bits */
  921. /* when parity is disabled the parity type bit is ignored */
  922. /* 1 means 2 stop bits, 0 means 1 stop bit */
  923. stop_bits = cflag & CSTOPB ? 1 : 0;
  924. if (cflag & PARENB) {
  925. parity_enable = 1;
  926. /* 1 means odd parity, 0 means even parity */
  927. parity_type = cflag & PARODD ? 1 : 0;
  928. } else
  929. parity_enable = parity_type = 0;
  930. switch (cflag & CSIZE) {
  931. case CS5:
  932. data_bits = 0;
  933. break;
  934. case CS6:
  935. data_bits = 1;
  936. break;
  937. case CS7:
  938. data_bits = 2;
  939. break;
  940. case CS8:
  941. data_bits = 3;
  942. break;
  943. default:
  944. dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
  945. __func__);
  946. data_bits = 3;
  947. }
  948. spin_lock_irqsave(&priv->lock, flags);
  949. oldlines = priv->line_control;
  950. if ((cflag & CBAUD) == B0) {
  951. /* drop dtr and rts */
  952. dbg("%s - dropping the lines, baud rate 0bps", __func__);
  953. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  954. } else
  955. priv->line_control = (CONTROL_DTR | CONTROL_RTS);
  956. spin_unlock_irqrestore(&priv->lock, flags);
  957. dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
  958. "%d data_bits (+5)", __func__, stop_bits,
  959. parity_enable, parity_type, data_bits);
  960. cypress_serial_control(tty, port, tty_get_baud_rate(tty),
  961. data_bits, stop_bits,
  962. parity_enable, parity_type,
  963. 0, CYPRESS_SET_CONFIG);
  964. /* we perform a CYPRESS_GET_CONFIG so that the current settings are
  965. * filled into the private structure this should confirm that all is
  966. * working if it returns what we just set */
  967. cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
  968. /* Here we can define custom tty settings for devices; the main tty
  969. * termios flag base comes from empeg.c */
  970. spin_lock_irqsave(&priv->lock, flags);
  971. if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
  972. dbg("Using custom termios settings for a baud rate of "
  973. "4800bps.");
  974. /* define custom termios settings for NMEA protocol */
  975. tty->termios->c_iflag /* input modes - */
  976. &= ~(IGNBRK /* disable ignore break */
  977. | BRKINT /* disable break causes interrupt */
  978. | PARMRK /* disable mark parity errors */
  979. | ISTRIP /* disable clear high bit of input char */
  980. | INLCR /* disable translate NL to CR */
  981. | IGNCR /* disable ignore CR */
  982. | ICRNL /* disable translate CR to NL */
  983. | IXON); /* disable enable XON/XOFF flow control */
  984. tty->termios->c_oflag /* output modes */
  985. &= ~OPOST; /* disable postprocess output char */
  986. tty->termios->c_lflag /* line discipline modes */
  987. &= ~(ECHO /* disable echo input characters */
  988. | ECHONL /* disable echo new line */
  989. | ICANON /* disable erase, kill, werase, and rprnt
  990. special characters */
  991. | ISIG /* disable interrupt, quit, and suspend
  992. special characters */
  993. | IEXTEN); /* disable non-POSIX special characters */
  994. } /* CT_CYPHIDCOM: Application should handle this for device */
  995. linechange = (priv->line_control != oldlines);
  996. spin_unlock_irqrestore(&priv->lock, flags);
  997. /* if necessary, set lines */
  998. if (linechange) {
  999. priv->cmd_ctrl = 1;
  1000. cypress_write(tty, port, NULL, 0);
  1001. }
  1002. } /* cypress_set_termios */
  1003. /* returns amount of data still left in soft buffer */
  1004. static int cypress_chars_in_buffer(struct tty_struct *tty)
  1005. {
  1006. struct usb_serial_port *port = tty->driver_data;
  1007. struct cypress_private *priv = usb_get_serial_port_data(port);
  1008. int chars = 0;
  1009. unsigned long flags;
  1010. dbg("%s - port %d", __func__, port->number);
  1011. spin_lock_irqsave(&priv->lock, flags);
  1012. chars = cypress_buf_data_avail(priv->buf);
  1013. spin_unlock_irqrestore(&priv->lock, flags);
  1014. dbg("%s - returns %d", __func__, chars);
  1015. return chars;
  1016. }
  1017. static void cypress_throttle(struct tty_struct *tty)
  1018. {
  1019. struct usb_serial_port *port = tty->driver_data;
  1020. struct cypress_private *priv = usb_get_serial_port_data(port);
  1021. unsigned long flags;
  1022. dbg("%s - port %d", __func__, port->number);
  1023. spin_lock_irqsave(&priv->lock, flags);
  1024. priv->rx_flags = THROTTLED;
  1025. spin_unlock_irqrestore(&priv->lock, flags);
  1026. }
  1027. static void cypress_unthrottle(struct tty_struct *tty)
  1028. {
  1029. struct usb_serial_port *port = tty->driver_data;
  1030. struct cypress_private *priv = usb_get_serial_port_data(port);
  1031. int actually_throttled, result;
  1032. unsigned long flags;
  1033. dbg("%s - port %d", __func__, port->number);
  1034. spin_lock_irqsave(&priv->lock, flags);
  1035. actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
  1036. priv->rx_flags = 0;
  1037. spin_unlock_irqrestore(&priv->lock, flags);
  1038. if (!priv->comm_is_ok)
  1039. return;
  1040. if (actually_throttled) {
  1041. port->interrupt_in_urb->dev = port->serial->dev;
  1042. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  1043. if (result) {
  1044. dev_err(&port->dev, "%s - failed submitting read urb, "
  1045. "error %d\n", __func__, result);
  1046. cypress_set_dead(port);
  1047. }
  1048. }
  1049. }
  1050. static void cypress_read_int_callback(struct urb *urb)
  1051. {
  1052. struct usb_serial_port *port = urb->context;
  1053. struct cypress_private *priv = usb_get_serial_port_data(port);
  1054. struct tty_struct *tty;
  1055. unsigned char *data = urb->transfer_buffer;
  1056. unsigned long flags;
  1057. char tty_flag = TTY_NORMAL;
  1058. int havedata = 0;
  1059. int bytes = 0;
  1060. int result;
  1061. int i = 0;
  1062. int status = urb->status;
  1063. dbg("%s - port %d", __func__, port->number);
  1064. switch (status) {
  1065. case 0: /* success */
  1066. break;
  1067. case -ECONNRESET:
  1068. case -ENOENT:
  1069. case -ESHUTDOWN:
  1070. /* precursor to disconnect so just go away */
  1071. return;
  1072. case -EPIPE:
  1073. usb_clear_halt(port->serial->dev, 0x81);
  1074. break;
  1075. default:
  1076. /* something ugly is going on... */
  1077. dev_err(&urb->dev->dev,
  1078. "%s - unexpected nonzero read status received: %d\n",
  1079. __func__, status);
  1080. cypress_set_dead(port);
  1081. return;
  1082. }
  1083. spin_lock_irqsave(&priv->lock, flags);
  1084. if (priv->rx_flags & THROTTLED) {
  1085. dbg("%s - now throttling", __func__);
  1086. priv->rx_flags |= ACTUALLY_THROTTLED;
  1087. spin_unlock_irqrestore(&priv->lock, flags);
  1088. return;
  1089. }
  1090. spin_unlock_irqrestore(&priv->lock, flags);
  1091. tty = tty_port_tty_get(&port->port);
  1092. if (!tty) {
  1093. dbg("%s - bad tty pointer - exiting", __func__);
  1094. return;
  1095. }
  1096. spin_lock_irqsave(&priv->lock, flags);
  1097. result = urb->actual_length;
  1098. switch (priv->pkt_fmt) {
  1099. default:
  1100. case packet_format_1:
  1101. /* This is for the CY7C64013... */
  1102. priv->current_status = data[0] & 0xF8;
  1103. bytes = data[1] + 2;
  1104. i = 2;
  1105. if (bytes > 2)
  1106. havedata = 1;
  1107. break;
  1108. case packet_format_2:
  1109. /* This is for the CY7C63743... */
  1110. priv->current_status = data[0] & 0xF8;
  1111. bytes = (data[0] & 0x07) + 1;
  1112. i = 1;
  1113. if (bytes > 1)
  1114. havedata = 1;
  1115. break;
  1116. }
  1117. spin_unlock_irqrestore(&priv->lock, flags);
  1118. if (result < bytes) {
  1119. dbg("%s - wrong packet size - received %d bytes but packet "
  1120. "said %d bytes", __func__, result, bytes);
  1121. goto continue_read;
  1122. }
  1123. usb_serial_debug_data(debug, &port->dev, __func__,
  1124. urb->actual_length, data);
  1125. spin_lock_irqsave(&priv->lock, flags);
  1126. /* check to see if status has changed */
  1127. if (priv->current_status != priv->prev_status) {
  1128. priv->diff_status |= priv->current_status ^
  1129. priv->prev_status;
  1130. wake_up_interruptible(&priv->delta_msr_wait);
  1131. priv->prev_status = priv->current_status;
  1132. }
  1133. spin_unlock_irqrestore(&priv->lock, flags);
  1134. /* hangup, as defined in acm.c... this might be a bad place for it
  1135. * though */
  1136. if (tty && !(tty->termios->c_cflag & CLOCAL) &&
  1137. !(priv->current_status & UART_CD)) {
  1138. dbg("%s - calling hangup", __func__);
  1139. tty_hangup(tty);
  1140. goto continue_read;
  1141. }
  1142. /* There is one error bit... I'm assuming it is a parity error
  1143. * indicator as the generic firmware will set this bit to 1 if a
  1144. * parity error occurs.
  1145. * I can not find reference to any other error events. */
  1146. spin_lock_irqsave(&priv->lock, flags);
  1147. if (priv->current_status & CYP_ERROR) {
  1148. spin_unlock_irqrestore(&priv->lock, flags);
  1149. tty_flag = TTY_PARITY;
  1150. dbg("%s - Parity Error detected", __func__);
  1151. } else
  1152. spin_unlock_irqrestore(&priv->lock, flags);
  1153. /* process read if there is data other than line status */
  1154. if (tty && (bytes > i)) {
  1155. bytes = tty_buffer_request_room(tty, bytes);
  1156. for (; i < bytes ; ++i) {
  1157. dbg("pushing byte number %d - %d - %c", i, data[i],
  1158. data[i]);
  1159. tty_insert_flip_char(tty, data[i], tty_flag);
  1160. }
  1161. tty_flip_buffer_push(tty);
  1162. }
  1163. spin_lock_irqsave(&priv->lock, flags);
  1164. /* control and status byte(s) are also counted */
  1165. priv->bytes_in += bytes;
  1166. spin_unlock_irqrestore(&priv->lock, flags);
  1167. continue_read:
  1168. tty_kref_put(tty);
  1169. /* Continue trying to always read... unless the port has closed. */
  1170. if (port->port.count > 0 && priv->comm_is_ok) {
  1171. usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
  1172. usb_rcvintpipe(port->serial->dev,
  1173. port->interrupt_in_endpointAddress),
  1174. port->interrupt_in_urb->transfer_buffer,
  1175. port->interrupt_in_urb->transfer_buffer_length,
  1176. cypress_read_int_callback, port,
  1177. priv->read_urb_interval);
  1178. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  1179. if (result) {
  1180. dev_err(&urb->dev->dev, "%s - failed resubmitting "
  1181. "read urb, error %d\n", __func__,
  1182. result);
  1183. cypress_set_dead(port);
  1184. }
  1185. }
  1186. return;
  1187. } /* cypress_read_int_callback */
  1188. static void cypress_write_int_callback(struct urb *urb)
  1189. {
  1190. struct usb_serial_port *port = urb->context;
  1191. struct cypress_private *priv = usb_get_serial_port_data(port);
  1192. int result;
  1193. int status = urb->status;
  1194. dbg("%s - port %d", __func__, port->number);
  1195. switch (status) {
  1196. case 0:
  1197. /* success */
  1198. break;
  1199. case -ECONNRESET:
  1200. case -ENOENT:
  1201. case -ESHUTDOWN:
  1202. /* this urb is terminated, clean up */
  1203. dbg("%s - urb shutting down with status: %d",
  1204. __func__, status);
  1205. priv->write_urb_in_use = 0;
  1206. return;
  1207. case -EPIPE: /* no break needed; clear halt and resubmit */
  1208. if (!priv->comm_is_ok)
  1209. break;
  1210. usb_clear_halt(port->serial->dev, 0x02);
  1211. /* error in the urb, so we have to resubmit it */
  1212. dbg("%s - nonzero write bulk status received: %d",
  1213. __func__, status);
  1214. port->interrupt_out_urb->transfer_buffer_length = 1;
  1215. port->interrupt_out_urb->dev = port->serial->dev;
  1216. result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
  1217. if (!result)
  1218. return;
  1219. dev_err(&urb->dev->dev,
  1220. "%s - failed resubmitting write urb, error %d\n",
  1221. __func__, result);
  1222. cypress_set_dead(port);
  1223. break;
  1224. default:
  1225. dev_err(&urb->dev->dev,
  1226. "%s - unexpected nonzero write status received: %d\n",
  1227. __func__, status);
  1228. cypress_set_dead(port);
  1229. break;
  1230. }
  1231. priv->write_urb_in_use = 0;
  1232. /* send any buffered data */
  1233. cypress_send(port);
  1234. }
  1235. /*****************************************************************************
  1236. * Write buffer functions - buffering code from pl2303 used
  1237. *****************************************************************************/
  1238. /*
  1239. * cypress_buf_alloc
  1240. *
  1241. * Allocate a circular buffer and all associated memory.
  1242. */
  1243. static struct cypress_buf *cypress_buf_alloc(unsigned int size)
  1244. {
  1245. struct cypress_buf *cb;
  1246. if (size == 0)
  1247. return NULL;
  1248. cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
  1249. if (cb == NULL)
  1250. return NULL;
  1251. cb->buf_buf = kmalloc(size, GFP_KERNEL);
  1252. if (cb->buf_buf == NULL) {
  1253. kfree(cb);
  1254. return NULL;
  1255. }
  1256. cb->buf_size = size;
  1257. cb->buf_get = cb->buf_put = cb->buf_buf;
  1258. return cb;
  1259. }
  1260. /*
  1261. * cypress_buf_free
  1262. *
  1263. * Free the buffer and all associated memory.
  1264. */
  1265. static void cypress_buf_free(struct cypress_buf *cb)
  1266. {
  1267. if (cb) {
  1268. kfree(cb->buf_buf);
  1269. kfree(cb);
  1270. }
  1271. }
  1272. /*
  1273. * cypress_buf_clear
  1274. *
  1275. * Clear out all data in the circular buffer.
  1276. */
  1277. static void cypress_buf_clear(struct cypress_buf *cb)
  1278. {
  1279. if (cb != NULL)
  1280. cb->buf_get = cb->buf_put;
  1281. /* equivalent to a get of all data available */
  1282. }
  1283. /*
  1284. * cypress_buf_data_avail
  1285. *
  1286. * Return the number of bytes of data available in the circular
  1287. * buffer.
  1288. */
  1289. static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
  1290. {
  1291. if (cb != NULL)
  1292. return (cb->buf_size + cb->buf_put - cb->buf_get)
  1293. % cb->buf_size;
  1294. else
  1295. return 0;
  1296. }
  1297. /*
  1298. * cypress_buf_space_avail
  1299. *
  1300. * Return the number of bytes of space available in the circular
  1301. * buffer.
  1302. */
  1303. static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
  1304. {
  1305. if (cb != NULL)
  1306. return (cb->buf_size + cb->buf_get - cb->buf_put - 1)
  1307. % cb->buf_size;
  1308. else
  1309. return 0;
  1310. }
  1311. /*
  1312. * cypress_buf_put
  1313. *
  1314. * Copy data data from a user buffer and put it into the circular buffer.
  1315. * Restrict to the amount of space available.
  1316. *
  1317. * Return the number of bytes copied.
  1318. */
  1319. static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
  1320. unsigned int count)
  1321. {
  1322. unsigned int len;
  1323. if (cb == NULL)
  1324. return 0;
  1325. len = cypress_buf_space_avail(cb);
  1326. if (count > len)
  1327. count = len;
  1328. if (count == 0)
  1329. return 0;
  1330. len = cb->buf_buf + cb->buf_size - cb->buf_put;
  1331. if (count > len) {
  1332. memcpy(cb->buf_put, buf, len);
  1333. memcpy(cb->buf_buf, buf+len, count - len);
  1334. cb->buf_put = cb->buf_buf + count - len;
  1335. } else {
  1336. memcpy(cb->buf_put, buf, count);
  1337. if (count < len)
  1338. cb->buf_put += count;
  1339. else /* count == len */
  1340. cb->buf_put = cb->buf_buf;
  1341. }
  1342. return count;
  1343. }
  1344. /*
  1345. * cypress_buf_get
  1346. *
  1347. * Get data from the circular buffer and copy to the given buffer.
  1348. * Restrict to the amount of data available.
  1349. *
  1350. * Return the number of bytes copied.
  1351. */
  1352. static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
  1353. unsigned int count)
  1354. {
  1355. unsigned int len;
  1356. if (cb == NULL)
  1357. return 0;
  1358. len = cypress_buf_data_avail(cb);
  1359. if (count > len)
  1360. count = len;
  1361. if (count == 0)
  1362. return 0;
  1363. len = cb->buf_buf + cb->buf_size - cb->buf_get;
  1364. if (count > len) {
  1365. memcpy(buf, cb->buf_get, len);
  1366. memcpy(buf+len, cb->buf_buf, count - len);
  1367. cb->buf_get = cb->buf_buf + count - len;
  1368. } else {
  1369. memcpy(buf, cb->buf_get, count);
  1370. if (count < len)
  1371. cb->buf_get += count;
  1372. else /* count == len */
  1373. cb->buf_get = cb->buf_buf;
  1374. }
  1375. return count;
  1376. }
  1377. /*****************************************************************************
  1378. * Module functions
  1379. *****************************************************************************/
  1380. static int __init cypress_init(void)
  1381. {
  1382. int retval;
  1383. dbg("%s", __func__);
  1384. retval = usb_serial_register(&cypress_earthmate_device);
  1385. if (retval)
  1386. goto failed_em_register;
  1387. retval = usb_serial_register(&cypress_hidcom_device);
  1388. if (retval)
  1389. goto failed_hidcom_register;
  1390. retval = usb_serial_register(&cypress_ca42v2_device);
  1391. if (retval)
  1392. goto failed_ca42v2_register;
  1393. retval = usb_register(&cypress_driver);
  1394. if (retval)
  1395. goto failed_usb_register;
  1396. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1397. DRIVER_DESC "\n");
  1398. return 0;
  1399. failed_usb_register:
  1400. usb_serial_deregister(&cypress_ca42v2_device);
  1401. failed_ca42v2_register:
  1402. usb_serial_deregister(&cypress_hidcom_device);
  1403. failed_hidcom_register:
  1404. usb_serial_deregister(&cypress_earthmate_device);
  1405. failed_em_register:
  1406. return retval;
  1407. }
  1408. static void __exit cypress_exit(void)
  1409. {
  1410. dbg("%s", __func__);
  1411. usb_deregister(&cypress_driver);
  1412. usb_serial_deregister(&cypress_earthmate_device);
  1413. usb_serial_deregister(&cypress_hidcom_device);
  1414. usb_serial_deregister(&cypress_ca42v2_device);
  1415. }
  1416. module_init(cypress_init);
  1417. module_exit(cypress_exit);
  1418. MODULE_AUTHOR(DRIVER_AUTHOR);
  1419. MODULE_DESCRIPTION(DRIVER_DESC);
  1420. MODULE_VERSION(DRIVER_VERSION);
  1421. MODULE_LICENSE("GPL");
  1422. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1423. MODULE_PARM_DESC(debug, "Debug enabled or not");
  1424. module_param(stats, bool, S_IRUGO | S_IWUSR);
  1425. MODULE_PARM_DESC(stats, "Enable statistics or not");
  1426. module_param(interval, int, S_IRUGO | S_IWUSR);
  1427. MODULE_PARM_DESC(interval, "Overrides interrupt interval");