cypress_m8.c 46 KB

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