cypress_m8.c 47 KB

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