visor.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * USB HandSpring Visor, Palm m50x, and Sony Clie driver
  3. * (supports all of the Palm OS USB devices)
  4. *
  5. * Copyright (C) 1999 - 2004
  6. * Greg Kroah-Hartman (greg@kroah.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * See Documentation/usb/usb-serial.txt for more information on using this
  13. * driver
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_driver.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/serial.h>
  29. #include "visor.h"
  30. /*
  31. * Version Information
  32. */
  33. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
  34. #define DRIVER_DESC "USB HandSpring Visor / Palm OS driver"
  35. /* function prototypes for a handspring visor */
  36. static int visor_open(struct tty_struct *tty, struct usb_serial_port *port);
  37. static void visor_close(struct usb_serial_port *port);
  38. static int visor_write(struct tty_struct *tty, struct usb_serial_port *port,
  39. const unsigned char *buf, int count);
  40. static int visor_write_room(struct tty_struct *tty);
  41. static void visor_throttle(struct tty_struct *tty);
  42. static void visor_unthrottle(struct tty_struct *tty);
  43. static int visor_probe(struct usb_serial *serial,
  44. const struct usb_device_id *id);
  45. static int visor_calc_num_ports(struct usb_serial *serial);
  46. static void visor_release(struct usb_serial *serial);
  47. static void visor_write_bulk_callback(struct urb *urb);
  48. static void visor_read_bulk_callback(struct urb *urb);
  49. static void visor_read_int_callback(struct urb *urb);
  50. static int clie_3_5_startup(struct usb_serial *serial);
  51. static int treo_attach(struct usb_serial *serial);
  52. static int clie_5_attach(struct usb_serial *serial);
  53. static int palm_os_3_probe(struct usb_serial *serial,
  54. const struct usb_device_id *id);
  55. static int palm_os_4_probe(struct usb_serial *serial,
  56. const struct usb_device_id *id);
  57. /* Parameters that may be passed into the module. */
  58. static int debug;
  59. static __u16 vendor;
  60. static __u16 product;
  61. static struct usb_device_id id_table [] = {
  62. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID),
  63. .driver_info = (kernel_ulong_t)&palm_os_3_probe },
  64. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID),
  65. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  66. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID),
  67. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  68. { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID),
  69. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  70. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID),
  71. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  72. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID),
  73. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  74. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID),
  75. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  76. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID),
  77. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  78. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID),
  79. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  80. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID),
  81. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  82. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID),
  83. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  84. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID),
  85. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  86. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650),
  87. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  88. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID),
  89. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  90. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID),
  91. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  92. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID),
  93. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  94. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID),
  95. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  96. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID),
  97. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  98. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID),
  99. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  100. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID),
  101. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  102. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID),
  103. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  104. { USB_DEVICE(ACER_VENDOR_ID, ACER_S10_ID),
  105. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  106. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID),
  107. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  108. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID),
  109. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  110. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID),
  111. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  112. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID),
  113. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  114. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID),
  115. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  116. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID),
  117. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  118. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID),
  119. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  120. { }, /* optional parameter entry */
  121. { } /* Terminating entry */
  122. };
  123. static struct usb_device_id clie_id_5_table [] = {
  124. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID),
  125. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  126. { }, /* optional parameter entry */
  127. { } /* Terminating entry */
  128. };
  129. static struct usb_device_id clie_id_3_5_table [] = {
  130. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  131. { } /* Terminating entry */
  132. };
  133. static struct usb_device_id id_table_combined [] = {
  134. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
  135. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
  136. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
  137. { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID) },
  138. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
  139. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
  140. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
  141. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
  142. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
  143. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
  144. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
  145. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
  146. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650) },
  147. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
  148. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
  149. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  150. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
  151. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
  152. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
  153. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
  154. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
  155. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
  156. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
  157. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
  158. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID) },
  159. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID) },
  160. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
  161. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
  162. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID) },
  163. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID) },
  164. { }, /* optional parameter entry */
  165. { } /* Terminating entry */
  166. };
  167. MODULE_DEVICE_TABLE(usb, id_table_combined);
  168. static struct usb_driver visor_driver = {
  169. .name = "visor",
  170. .probe = usb_serial_probe,
  171. .disconnect = usb_serial_disconnect,
  172. .id_table = id_table_combined,
  173. .no_dynamic_id = 1,
  174. };
  175. /* All of the device info needed for the Handspring Visor,
  176. and Palm 4.0 devices */
  177. static struct usb_serial_driver handspring_device = {
  178. .driver = {
  179. .owner = THIS_MODULE,
  180. .name = "visor",
  181. },
  182. .description = "Handspring Visor / Palm OS",
  183. .usb_driver = &visor_driver,
  184. .id_table = id_table,
  185. .num_ports = 2,
  186. .open = visor_open,
  187. .close = visor_close,
  188. .throttle = visor_throttle,
  189. .unthrottle = visor_unthrottle,
  190. .attach = treo_attach,
  191. .probe = visor_probe,
  192. .calc_num_ports = visor_calc_num_ports,
  193. .release = visor_release,
  194. .write = visor_write,
  195. .write_room = visor_write_room,
  196. .write_bulk_callback = visor_write_bulk_callback,
  197. .read_bulk_callback = visor_read_bulk_callback,
  198. .read_int_callback = visor_read_int_callback,
  199. };
  200. /* All of the device info needed for the Clie UX50, TH55 Palm 5.0 devices */
  201. static struct usb_serial_driver clie_5_device = {
  202. .driver = {
  203. .owner = THIS_MODULE,
  204. .name = "clie_5",
  205. },
  206. .description = "Sony Clie 5.0",
  207. .usb_driver = &visor_driver,
  208. .id_table = clie_id_5_table,
  209. .num_ports = 2,
  210. .open = visor_open,
  211. .close = visor_close,
  212. .throttle = visor_throttle,
  213. .unthrottle = visor_unthrottle,
  214. .attach = clie_5_attach,
  215. .probe = visor_probe,
  216. .calc_num_ports = visor_calc_num_ports,
  217. .release = visor_release,
  218. .write = visor_write,
  219. .write_room = visor_write_room,
  220. .write_bulk_callback = visor_write_bulk_callback,
  221. .read_bulk_callback = visor_read_bulk_callback,
  222. .read_int_callback = visor_read_int_callback,
  223. };
  224. /* device info for the Sony Clie OS version 3.5 */
  225. static struct usb_serial_driver clie_3_5_device = {
  226. .driver = {
  227. .owner = THIS_MODULE,
  228. .name = "clie_3.5",
  229. },
  230. .description = "Sony Clie 3.5",
  231. .usb_driver = &visor_driver,
  232. .id_table = clie_id_3_5_table,
  233. .num_ports = 1,
  234. .open = visor_open,
  235. .close = visor_close,
  236. .throttle = visor_throttle,
  237. .unthrottle = visor_unthrottle,
  238. .attach = clie_3_5_startup,
  239. .write = visor_write,
  240. .write_room = visor_write_room,
  241. .write_bulk_callback = visor_write_bulk_callback,
  242. .read_bulk_callback = visor_read_bulk_callback,
  243. };
  244. struct visor_private {
  245. spinlock_t lock;
  246. int bytes_in;
  247. int bytes_out;
  248. int outstanding_urbs;
  249. unsigned char throttled;
  250. unsigned char actually_throttled;
  251. };
  252. /* number of outstanding urbs to prevent userspace DoS from happening */
  253. #define URB_UPPER_LIMIT 42
  254. static int stats;
  255. /******************************************************************************
  256. * Handspring Visor specific driver functions
  257. ******************************************************************************/
  258. static int visor_open(struct tty_struct *tty, struct usb_serial_port *port)
  259. {
  260. struct usb_serial *serial = port->serial;
  261. struct visor_private *priv = usb_get_serial_port_data(port);
  262. unsigned long flags;
  263. int result = 0;
  264. dbg("%s - port %d", __func__, port->number);
  265. if (!port->read_urb) {
  266. /* this is needed for some brain dead Sony devices */
  267. dev_err(&port->dev, "Device lied about number of ports, please use a lower one.\n");
  268. return -ENODEV;
  269. }
  270. spin_lock_irqsave(&priv->lock, flags);
  271. priv->bytes_in = 0;
  272. priv->bytes_out = 0;
  273. priv->throttled = 0;
  274. spin_unlock_irqrestore(&priv->lock, flags);
  275. /* Start reading from the device */
  276. usb_fill_bulk_urb(port->read_urb, serial->dev,
  277. usb_rcvbulkpipe(serial->dev,
  278. port->bulk_in_endpointAddress),
  279. port->read_urb->transfer_buffer,
  280. port->read_urb->transfer_buffer_length,
  281. visor_read_bulk_callback, port);
  282. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  283. if (result) {
  284. dev_err(&port->dev,
  285. "%s - failed submitting read urb, error %d\n",
  286. __func__, result);
  287. goto exit;
  288. }
  289. if (port->interrupt_in_urb) {
  290. dbg("%s - adding interrupt input for treo", __func__);
  291. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  292. if (result)
  293. dev_err(&port->dev,
  294. "%s - failed submitting interrupt urb, error %d\n",
  295. __func__, result);
  296. }
  297. exit:
  298. return result;
  299. }
  300. static void visor_close(struct usb_serial_port *port)
  301. {
  302. struct visor_private *priv = usb_get_serial_port_data(port);
  303. unsigned char *transfer_buffer;
  304. dbg("%s - port %d", __func__, port->number);
  305. /* shutdown our urbs */
  306. usb_kill_urb(port->read_urb);
  307. usb_kill_urb(port->interrupt_in_urb);
  308. mutex_lock(&port->serial->disc_mutex);
  309. if (!port->serial->disconnected) {
  310. /* Try to send shutdown message, unless the device is gone */
  311. transfer_buffer = kmalloc(0x12, GFP_KERNEL);
  312. if (transfer_buffer) {
  313. usb_control_msg(port->serial->dev,
  314. usb_rcvctrlpipe(port->serial->dev, 0),
  315. VISOR_CLOSE_NOTIFICATION, 0xc2,
  316. 0x0000, 0x0000,
  317. transfer_buffer, 0x12, 300);
  318. kfree(transfer_buffer);
  319. }
  320. }
  321. mutex_unlock(&port->serial->disc_mutex);
  322. if (stats)
  323. dev_info(&port->dev, "Bytes In = %d Bytes Out = %d\n",
  324. priv->bytes_in, priv->bytes_out);
  325. }
  326. static int visor_write(struct tty_struct *tty, struct usb_serial_port *port,
  327. const unsigned char *buf, int count)
  328. {
  329. struct visor_private *priv = usb_get_serial_port_data(port);
  330. struct usb_serial *serial = port->serial;
  331. struct urb *urb;
  332. unsigned char *buffer;
  333. unsigned long flags;
  334. int status;
  335. dbg("%s - port %d", __func__, port->number);
  336. spin_lock_irqsave(&priv->lock, flags);
  337. if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
  338. spin_unlock_irqrestore(&priv->lock, flags);
  339. dbg("%s - write limit hit\n", __func__);
  340. return 0;
  341. }
  342. priv->outstanding_urbs++;
  343. spin_unlock_irqrestore(&priv->lock, flags);
  344. buffer = kmalloc(count, GFP_ATOMIC);
  345. if (!buffer) {
  346. dev_err(&port->dev, "out of memory\n");
  347. count = -ENOMEM;
  348. goto error_no_buffer;
  349. }
  350. urb = usb_alloc_urb(0, GFP_ATOMIC);
  351. if (!urb) {
  352. dev_err(&port->dev, "no more free urbs\n");
  353. count = -ENOMEM;
  354. goto error_no_urb;
  355. }
  356. memcpy(buffer, buf, count);
  357. usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
  358. usb_fill_bulk_urb(urb, serial->dev,
  359. usb_sndbulkpipe(serial->dev,
  360. port->bulk_out_endpointAddress),
  361. buffer, count,
  362. visor_write_bulk_callback, port);
  363. /* send it down the pipe */
  364. status = usb_submit_urb(urb, GFP_ATOMIC);
  365. if (status) {
  366. dev_err(&port->dev,
  367. "%s - usb_submit_urb(write bulk) failed with status = %d\n",
  368. __func__, status);
  369. count = status;
  370. goto error;
  371. } else {
  372. spin_lock_irqsave(&priv->lock, flags);
  373. priv->bytes_out += count;
  374. spin_unlock_irqrestore(&priv->lock, flags);
  375. }
  376. /* we are done with this urb, so let the host driver
  377. * really free it when it is finished with it */
  378. usb_free_urb(urb);
  379. return count;
  380. error:
  381. usb_free_urb(urb);
  382. error_no_urb:
  383. kfree(buffer);
  384. error_no_buffer:
  385. spin_lock_irqsave(&priv->lock, flags);
  386. --priv->outstanding_urbs;
  387. spin_unlock_irqrestore(&priv->lock, flags);
  388. return count;
  389. }
  390. static int visor_write_room(struct tty_struct *tty)
  391. {
  392. struct usb_serial_port *port = tty->driver_data;
  393. struct visor_private *priv = usb_get_serial_port_data(port);
  394. unsigned long flags;
  395. dbg("%s - port %d", __func__, port->number);
  396. /*
  397. * We really can take anything the user throws at us
  398. * but let's pick a nice big number to tell the tty
  399. * layer that we have lots of free space, unless we don't.
  400. */
  401. spin_lock_irqsave(&priv->lock, flags);
  402. if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
  403. spin_unlock_irqrestore(&priv->lock, flags);
  404. dbg("%s - write limit hit\n", __func__);
  405. return 0;
  406. }
  407. spin_unlock_irqrestore(&priv->lock, flags);
  408. return 2048;
  409. }
  410. static void visor_write_bulk_callback(struct urb *urb)
  411. {
  412. struct usb_serial_port *port = urb->context;
  413. struct visor_private *priv = usb_get_serial_port_data(port);
  414. int status = urb->status;
  415. unsigned long flags;
  416. /* free up the transfer buffer, as usb_free_urb() does not do this */
  417. kfree(urb->transfer_buffer);
  418. dbg("%s - port %d", __func__, port->number);
  419. if (status)
  420. dbg("%s - nonzero write bulk status received: %d",
  421. __func__, status);
  422. spin_lock_irqsave(&priv->lock, flags);
  423. --priv->outstanding_urbs;
  424. spin_unlock_irqrestore(&priv->lock, flags);
  425. usb_serial_port_softint(port);
  426. }
  427. static void visor_read_bulk_callback(struct urb *urb)
  428. {
  429. struct usb_serial_port *port = urb->context;
  430. struct visor_private *priv = usb_get_serial_port_data(port);
  431. unsigned char *data = urb->transfer_buffer;
  432. int status = urb->status;
  433. struct tty_struct *tty;
  434. int result;
  435. int available_room = 0;
  436. dbg("%s - port %d", __func__, port->number);
  437. if (status) {
  438. dbg("%s - nonzero read bulk status received: %d",
  439. __func__, status);
  440. return;
  441. }
  442. usb_serial_debug_data(debug, &port->dev, __func__,
  443. urb->actual_length, data);
  444. if (urb->actual_length) {
  445. tty = tty_port_tty_get(&port->port);
  446. if (tty) {
  447. available_room = tty_buffer_request_room(tty,
  448. urb->actual_length);
  449. if (available_room) {
  450. tty_insert_flip_string(tty, data,
  451. available_room);
  452. tty_flip_buffer_push(tty);
  453. }
  454. tty_kref_put(tty);
  455. }
  456. spin_lock(&priv->lock);
  457. if (tty)
  458. priv->bytes_in += available_room;
  459. } else {
  460. spin_lock(&priv->lock);
  461. }
  462. /* Continue trying to always read if we should */
  463. if (!priv->throttled) {
  464. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  465. usb_rcvbulkpipe(port->serial->dev,
  466. port->bulk_in_endpointAddress),
  467. port->read_urb->transfer_buffer,
  468. port->read_urb->transfer_buffer_length,
  469. visor_read_bulk_callback, port);
  470. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  471. if (result)
  472. dev_err(&port->dev,
  473. "%s - failed resubmitting read urb, error %d\n",
  474. __func__, result);
  475. } else
  476. priv->actually_throttled = 1;
  477. spin_unlock(&priv->lock);
  478. }
  479. static void visor_read_int_callback(struct urb *urb)
  480. {
  481. struct usb_serial_port *port = urb->context;
  482. int status = urb->status;
  483. int result;
  484. switch (status) {
  485. case 0:
  486. /* success */
  487. break;
  488. case -ECONNRESET:
  489. case -ENOENT:
  490. case -ESHUTDOWN:
  491. /* this urb is terminated, clean up */
  492. dbg("%s - urb shutting down with status: %d",
  493. __func__, status);
  494. return;
  495. default:
  496. dbg("%s - nonzero urb status received: %d",
  497. __func__, status);
  498. goto exit;
  499. }
  500. /*
  501. * This information is still unknown what it can be used for.
  502. * If anyone has an idea, please let the author know...
  503. *
  504. * Rumor has it this endpoint is used to notify when data
  505. * is ready to be read from the bulk ones.
  506. */
  507. usb_serial_debug_data(debug, &port->dev, __func__,
  508. urb->actual_length, urb->transfer_buffer);
  509. exit:
  510. result = usb_submit_urb(urb, GFP_ATOMIC);
  511. if (result)
  512. dev_err(&urb->dev->dev,
  513. "%s - Error %d submitting interrupt urb\n",
  514. __func__, result);
  515. }
  516. static void visor_throttle(struct tty_struct *tty)
  517. {
  518. struct usb_serial_port *port = tty->driver_data;
  519. struct visor_private *priv = usb_get_serial_port_data(port);
  520. dbg("%s - port %d", __func__, port->number);
  521. spin_lock_irq(&priv->lock);
  522. priv->throttled = 1;
  523. spin_unlock_irq(&priv->lock);
  524. }
  525. static void visor_unthrottle(struct tty_struct *tty)
  526. {
  527. struct usb_serial_port *port = tty->driver_data;
  528. struct visor_private *priv = usb_get_serial_port_data(port);
  529. int result, was_throttled;
  530. dbg("%s - port %d", __func__, port->number);
  531. spin_lock_irq(&priv->lock);
  532. priv->throttled = 0;
  533. was_throttled = priv->actually_throttled;
  534. priv->actually_throttled = 0;
  535. spin_unlock_irq(&priv->lock);
  536. if (was_throttled) {
  537. port->read_urb->dev = port->serial->dev;
  538. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  539. if (result)
  540. dev_err(&port->dev,
  541. "%s - failed submitting read urb, error %d\n",
  542. __func__, result);
  543. }
  544. }
  545. static int palm_os_3_probe(struct usb_serial *serial,
  546. const struct usb_device_id *id)
  547. {
  548. struct device *dev = &serial->dev->dev;
  549. struct visor_connection_info *connection_info;
  550. unsigned char *transfer_buffer;
  551. char *string;
  552. int retval = 0;
  553. int i;
  554. int num_ports = 0;
  555. dbg("%s", __func__);
  556. transfer_buffer = kmalloc(sizeof(*connection_info), GFP_KERNEL);
  557. if (!transfer_buffer) {
  558. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __func__,
  559. sizeof(*connection_info));
  560. return -ENOMEM;
  561. }
  562. /* send a get connection info request */
  563. retval = usb_control_msg(serial->dev,
  564. usb_rcvctrlpipe(serial->dev, 0),
  565. VISOR_GET_CONNECTION_INFORMATION,
  566. 0xc2, 0x0000, 0x0000, transfer_buffer,
  567. sizeof(*connection_info), 300);
  568. if (retval < 0) {
  569. dev_err(dev, "%s - error %d getting connection information\n",
  570. __func__, retval);
  571. goto exit;
  572. }
  573. if (retval == sizeof(*connection_info)) {
  574. connection_info = (struct visor_connection_info *)
  575. transfer_buffer;
  576. num_ports = le16_to_cpu(connection_info->num_ports);
  577. for (i = 0; i < num_ports; ++i) {
  578. switch (
  579. connection_info->connections[i].port_function_id) {
  580. case VISOR_FUNCTION_GENERIC:
  581. string = "Generic";
  582. break;
  583. case VISOR_FUNCTION_DEBUGGER:
  584. string = "Debugger";
  585. break;
  586. case VISOR_FUNCTION_HOTSYNC:
  587. string = "HotSync";
  588. break;
  589. case VISOR_FUNCTION_CONSOLE:
  590. string = "Console";
  591. break;
  592. case VISOR_FUNCTION_REMOTE_FILE_SYS:
  593. string = "Remote File System";
  594. break;
  595. default:
  596. string = "unknown";
  597. break;
  598. }
  599. dev_info(dev, "%s: port %d, is for %s use\n",
  600. serial->type->description,
  601. connection_info->connections[i].port, string);
  602. }
  603. }
  604. /*
  605. * Handle devices that report invalid stuff here.
  606. */
  607. if (num_ports == 0 || num_ports > 2) {
  608. dev_warn(dev, "%s: No valid connect info available\n",
  609. serial->type->description);
  610. num_ports = 2;
  611. }
  612. dev_info(dev, "%s: Number of ports: %d\n", serial->type->description,
  613. num_ports);
  614. /*
  615. * save off our num_ports info so that we can use it in the
  616. * calc_num_ports callback
  617. */
  618. usb_set_serial_data(serial, (void *)(long)num_ports);
  619. /* ask for the number of bytes available, but ignore the
  620. response as it is broken */
  621. retval = usb_control_msg(serial->dev,
  622. usb_rcvctrlpipe(serial->dev, 0),
  623. VISOR_REQUEST_BYTES_AVAILABLE,
  624. 0xc2, 0x0000, 0x0005, transfer_buffer,
  625. 0x02, 300);
  626. if (retval < 0)
  627. dev_err(dev, "%s - error %d getting bytes available request\n",
  628. __func__, retval);
  629. retval = 0;
  630. exit:
  631. kfree(transfer_buffer);
  632. return retval;
  633. }
  634. static int palm_os_4_probe(struct usb_serial *serial,
  635. const struct usb_device_id *id)
  636. {
  637. struct device *dev = &serial->dev->dev;
  638. struct palm_ext_connection_info *connection_info;
  639. unsigned char *transfer_buffer;
  640. int retval;
  641. dbg("%s", __func__);
  642. transfer_buffer = kmalloc(sizeof(*connection_info), GFP_KERNEL);
  643. if (!transfer_buffer) {
  644. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __func__,
  645. sizeof(*connection_info));
  646. return -ENOMEM;
  647. }
  648. retval = usb_control_msg(serial->dev,
  649. usb_rcvctrlpipe(serial->dev, 0),
  650. PALM_GET_EXT_CONNECTION_INFORMATION,
  651. 0xc2, 0x0000, 0x0000, transfer_buffer,
  652. sizeof(*connection_info), 300);
  653. if (retval < 0)
  654. dev_err(dev, "%s - error %d getting connection info\n",
  655. __func__, retval);
  656. else
  657. usb_serial_debug_data(debug, &serial->dev->dev, __func__,
  658. retval, transfer_buffer);
  659. kfree(transfer_buffer);
  660. return 0;
  661. }
  662. static int visor_probe(struct usb_serial *serial,
  663. const struct usb_device_id *id)
  664. {
  665. int retval = 0;
  666. int (*startup)(struct usb_serial *serial,
  667. const struct usb_device_id *id);
  668. dbg("%s", __func__);
  669. if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
  670. dev_err(&serial->dev->dev, "active config #%d != 1 ??\n",
  671. serial->dev->actconfig->desc.bConfigurationValue);
  672. return -ENODEV;
  673. }
  674. if (id->driver_info) {
  675. startup = (void *)id->driver_info;
  676. retval = startup(serial, id);
  677. }
  678. return retval;
  679. }
  680. static int visor_calc_num_ports(struct usb_serial *serial)
  681. {
  682. int num_ports = (int)(long)(usb_get_serial_data(serial));
  683. if (num_ports)
  684. usb_set_serial_data(serial, NULL);
  685. return num_ports;
  686. }
  687. static int generic_startup(struct usb_serial *serial)
  688. {
  689. struct usb_serial_port **ports = serial->port;
  690. struct visor_private *priv;
  691. int i;
  692. for (i = 0; i < serial->num_ports; ++i) {
  693. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  694. if (!priv) {
  695. while (i-- != 0) {
  696. priv = usb_get_serial_port_data(ports[i]);
  697. usb_set_serial_port_data(ports[i], NULL);
  698. kfree(priv);
  699. }
  700. return -ENOMEM;
  701. }
  702. spin_lock_init(&priv->lock);
  703. usb_set_serial_port_data(ports[i], priv);
  704. }
  705. return 0;
  706. }
  707. static int clie_3_5_startup(struct usb_serial *serial)
  708. {
  709. struct device *dev = &serial->dev->dev;
  710. int result;
  711. u8 data;
  712. dbg("%s", __func__);
  713. /*
  714. * Note that PEG-300 series devices expect the following two calls.
  715. */
  716. /* get the config number */
  717. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  718. USB_REQ_GET_CONFIGURATION, USB_DIR_IN,
  719. 0, 0, &data, 1, 3000);
  720. if (result < 0) {
  721. dev_err(dev, "%s: get config number failed: %d\n",
  722. __func__, result);
  723. return result;
  724. }
  725. if (result != 1) {
  726. dev_err(dev, "%s: get config number bad return length: %d\n",
  727. __func__, result);
  728. return -EIO;
  729. }
  730. /* get the interface number */
  731. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  732. USB_REQ_GET_INTERFACE,
  733. USB_DIR_IN | USB_RECIP_INTERFACE,
  734. 0, 0, &data, 1, 3000);
  735. if (result < 0) {
  736. dev_err(dev, "%s: get interface number failed: %d\n",
  737. __func__, result);
  738. return result;
  739. }
  740. if (result != 1) {
  741. dev_err(dev,
  742. "%s: get interface number bad return length: %d\n",
  743. __func__, result);
  744. return -EIO;
  745. }
  746. return generic_startup(serial);
  747. }
  748. static int treo_attach(struct usb_serial *serial)
  749. {
  750. struct usb_serial_port *swap_port;
  751. /* Only do this endpoint hack for the Handspring devices with
  752. * interrupt in endpoints, which for now are the Treo devices. */
  753. if (!((le16_to_cpu(serial->dev->descriptor.idVendor)
  754. == HANDSPRING_VENDOR_ID) ||
  755. (le16_to_cpu(serial->dev->descriptor.idVendor)
  756. == KYOCERA_VENDOR_ID)) ||
  757. (serial->num_interrupt_in == 0))
  758. goto generic_startup;
  759. dbg("%s", __func__);
  760. /*
  761. * It appears that Treos and Kyoceras want to use the
  762. * 1st bulk in endpoint to communicate with the 2nd bulk out endpoint,
  763. * so let's swap the 1st and 2nd bulk in and interrupt endpoints.
  764. * Note that swapping the bulk out endpoints would break lots of
  765. * apps that want to communicate on the second port.
  766. */
  767. #define COPY_PORT(dest, src) \
  768. do { \
  769. dest->read_urb = src->read_urb; \
  770. dest->bulk_in_endpointAddress = src->bulk_in_endpointAddress;\
  771. dest->bulk_in_buffer = src->bulk_in_buffer; \
  772. dest->interrupt_in_urb = src->interrupt_in_urb; \
  773. dest->interrupt_in_endpointAddress = \
  774. src->interrupt_in_endpointAddress;\
  775. dest->interrupt_in_buffer = src->interrupt_in_buffer; \
  776. } while (0);
  777. swap_port = kmalloc(sizeof(*swap_port), GFP_KERNEL);
  778. if (!swap_port)
  779. return -ENOMEM;
  780. COPY_PORT(swap_port, serial->port[0]);
  781. COPY_PORT(serial->port[0], serial->port[1]);
  782. COPY_PORT(serial->port[1], swap_port);
  783. kfree(swap_port);
  784. generic_startup:
  785. return generic_startup(serial);
  786. }
  787. static int clie_5_attach(struct usb_serial *serial)
  788. {
  789. dbg("%s", __func__);
  790. /* TH55 registers 2 ports.
  791. Communication in from the UX50/TH55 uses bulk_in_endpointAddress
  792. from port 0. Communication out to the UX50/TH55 uses
  793. bulk_out_endpointAddress from port 1
  794. Lets do a quick and dirty mapping
  795. */
  796. /* some sanity check */
  797. if (serial->num_ports < 2)
  798. return -1;
  799. /* port 0 now uses the modified endpoint Address */
  800. serial->port[0]->bulk_out_endpointAddress =
  801. serial->port[1]->bulk_out_endpointAddress;
  802. return generic_startup(serial);
  803. }
  804. static void visor_release(struct usb_serial *serial)
  805. {
  806. struct visor_private *priv;
  807. int i;
  808. dbg("%s", __func__);
  809. for (i = 0; i < serial->num_ports; i++) {
  810. priv = usb_get_serial_port_data(serial->port[i]);
  811. kfree(priv);
  812. }
  813. }
  814. static int __init visor_init(void)
  815. {
  816. int i, retval;
  817. /* Only if parameters were passed to us */
  818. if (vendor > 0 && product > 0) {
  819. struct usb_device_id usb_dev_temp[] = {
  820. {
  821. USB_DEVICE(vendor, product),
  822. .driver_info =
  823. (kernel_ulong_t) &palm_os_4_probe
  824. }
  825. };
  826. /* Find the last entry in id_table */
  827. for (i = 0;; i++) {
  828. if (id_table[i].idVendor == 0) {
  829. id_table[i] = usb_dev_temp[0];
  830. break;
  831. }
  832. }
  833. /* Find the last entry in id_table_combined */
  834. for (i = 0;; i++) {
  835. if (id_table_combined[i].idVendor == 0) {
  836. id_table_combined[i] = usb_dev_temp[0];
  837. break;
  838. }
  839. }
  840. printk(KERN_INFO KBUILD_MODNAME
  841. ": Untested USB device specified at time of module insertion\n");
  842. printk(KERN_INFO KBUILD_MODNAME
  843. ": Warning: This is not guaranteed to work\n");
  844. printk(KERN_INFO KBUILD_MODNAME
  845. ": Using a newer kernel is preferred to this method\n");
  846. printk(KERN_INFO KBUILD_MODNAME
  847. ": Adding Palm OS protocol 4.x support for unknown device: 0x%x/0x%x\n",
  848. vendor, product);
  849. }
  850. retval = usb_serial_register(&handspring_device);
  851. if (retval)
  852. goto failed_handspring_register;
  853. retval = usb_serial_register(&clie_3_5_device);
  854. if (retval)
  855. goto failed_clie_3_5_register;
  856. retval = usb_serial_register(&clie_5_device);
  857. if (retval)
  858. goto failed_clie_5_register;
  859. retval = usb_register(&visor_driver);
  860. if (retval)
  861. goto failed_usb_register;
  862. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  863. return 0;
  864. failed_usb_register:
  865. usb_serial_deregister(&clie_5_device);
  866. failed_clie_5_register:
  867. usb_serial_deregister(&clie_3_5_device);
  868. failed_clie_3_5_register:
  869. usb_serial_deregister(&handspring_device);
  870. failed_handspring_register:
  871. return retval;
  872. }
  873. static void __exit visor_exit (void)
  874. {
  875. usb_deregister(&visor_driver);
  876. usb_serial_deregister(&handspring_device);
  877. usb_serial_deregister(&clie_3_5_device);
  878. usb_serial_deregister(&clie_5_device);
  879. }
  880. module_init(visor_init);
  881. module_exit(visor_exit);
  882. MODULE_AUTHOR(DRIVER_AUTHOR);
  883. MODULE_DESCRIPTION(DRIVER_DESC);
  884. MODULE_LICENSE("GPL");
  885. module_param(debug, bool, S_IRUGO | S_IWUSR);
  886. MODULE_PARM_DESC(debug, "Debug enabled or not");
  887. module_param(stats, bool, S_IRUGO | S_IWUSR);
  888. MODULE_PARM_DESC(stats, "Enables statistics or not");
  889. module_param(vendor, ushort, 0);
  890. MODULE_PARM_DESC(vendor, "User specified vendor ID");
  891. module_param(product, ushort, 0);
  892. MODULE_PARM_DESC(product, "User specified product ID");